blob: e5a5f9be23585a8782614852d3d73036aefce088 [file] [log] [blame] [edit]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/devices/internal/drivers/fragment/fragment.h"
#include <lib/ddk/binding_driver.h>
#include <lib/ddk/debug.h>
#include <lib/ddk/trace/event.h>
#include <stdio.h>
#include <string.h>
#include <zircon/assert.h>
#include <zircon/errors.h>
#include <atomic>
#include <iterator>
#include <memory>
#include <fbl/algorithm.h>
#include "src/devices/internal/drivers/fragment/proxy-protocol.h"
namespace fragment {
namespace {
void MakeUniqueName(char name[ZX_DEVICE_NAME_MAX + 1]) {
static std::atomic<size_t> unique_id = 0;
snprintf(name, ZX_DEVICE_NAME_MAX + 1, "fragment-%zu", unique_id.fetch_add(1));
}
} // namespace
template <typename ProtoClientType, typename ProtoType>
ProtocolClient<ProtoClientType, ProtoType>::ProtocolClient(zx_device_t* parent, uint32_t proto_id) {
ProtoClientType* protoptr = &proto_client_;
device_get_protocol(parent, proto_id, &proto_);
*protoptr = ProtoClientType(&proto_);
}
zx_status_t Fragment::Bind(void* ctx, zx_device_t* parent) {
char name[ZX_DEVICE_NAME_MAX + 1];
MakeUniqueName(name);
auto dev = std::make_unique<Fragment>(parent, fdf::Dispatcher::GetCurrent()->async_dispatcher());
zx::result client_end = fidl::CreateEndpoints(&dev->server_endpoint_);
if (client_end.is_error()) {
zxlogf(ERROR, "Failed to create directory endpoints: %s", client_end.status_string());
return client_end.status_value();
}
// The thing before the comma will become the process name, if a new process
// is created
auto status = dev->DdkAdd(ddk::DeviceAddArgs(name)
.set_flags(DEVICE_ADD_NON_BINDABLE | DEVICE_ADD_MUST_ISOLATE)
.set_outgoing_dir(client_end->TakeChannel()));
if (status == ZX_OK) {
// devmgr owns the memory now
[[maybe_unused]] auto ptr = dev.release();
}
return status;
}
void Fragment::DdkInit(ddk::InitTxn init_txn) {
zx::result result = outgoing_->AddUnmanagedProtocol(
[this](zx::channel server) {
rpc_channel_ = std::move(server);
rpc_wait_.set_object(rpc_channel_.get());
rpc_wait_.set_trigger(ZX_CHANNEL_READABLE);
rpc_wait_.set_handler([this](async_dispatcher_t* dispatcher, async::Wait* wait,
zx_status_t status, const zx_packet_signal_t* signal) {
ReadFidlFromChannel();
rpc_wait_.Begin(dispatcher_);
});
rpc_wait_.Begin(dispatcher_);
},
"proxy_channel");
if (result.status_value() != ZX_OK) {
zxlogf(ERROR, "Failed to add outgoing protocol: %s", result.status_string());
return init_txn.Reply(result.status_value());
}
result = outgoing_->Serve(std::move(server_endpoint_));
if (result.status_value() != ZX_OK) {
zxlogf(ERROR, "Failed to service the outgoing directory %s", result.status_string());
return init_txn.Reply(result.status_value());
}
init_txn.Reply(ZX_OK);
}
zx_status_t Fragment::RpcGpio(const uint8_t* req_buf, uint32_t req_size, uint8_t* resp_buf,
uint32_t* out_resp_size, zx::handle* req_handles,
uint32_t req_handle_count, zx::handle* resp_handles,
uint32_t* resp_handle_count) {
if (!gpio_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
auto* req = reinterpret_cast<const GpioProxyRequest*>(req_buf);
if (req_size < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu", __func__, req_size, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto* resp = reinterpret_cast<GpioProxyResponse*>(resp_buf);
*out_resp_size = sizeof(*resp);
switch (req->op) {
case GpioOp::CONFIG_IN:
return gpio_client_.proto_client().ConfigIn(req->flags);
case GpioOp::CONFIG_OUT:
return gpio_client_.proto_client().ConfigOut(req->value);
case GpioOp::SET_ALT_FUNCTION:
return gpio_client_.proto_client().SetAltFunction(req->alt_function);
case GpioOp::READ:
return gpio_client_.proto_client().Read(&resp->value);
case GpioOp::WRITE:
return gpio_client_.proto_client().Write(req->value);
case GpioOp::GET_INTERRUPT: {
zx::interrupt irq;
auto status = gpio_client_.proto_client().GetInterrupt(req->flags, &irq);
if (status == ZX_OK) {
resp_handles[0] = std::move(irq);
*resp_handle_count = 1;
}
return status;
}
case GpioOp::RELEASE_INTERRUPT:
return gpio_client_.proto_client().ReleaseInterrupt();
case GpioOp::SET_POLARITY:
return gpio_client_.proto_client().SetPolarity(req->polarity);
case GpioOp::SET_DRIVE_STRENGTH:
return gpio_client_.proto_client().SetDriveStrength(req->ds_ua, &resp->out_actual_ds_ua);
default:
zxlogf(ERROR, "%s: unknown GPIO op %u", __func__, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
}
zx_status_t Fragment::RpcPdev(const uint8_t* req_buf, uint32_t req_size, uint8_t* resp_buf,
uint32_t* out_resp_size, zx::handle* req_handles,
uint32_t req_handle_count, zx::handle* resp_handles,
uint32_t* resp_handle_count) {
if (!pdev_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
auto* req = reinterpret_cast<const PdevProxyRequest*>(req_buf);
if (req_size < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu", __func__, req_size, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto* resp = reinterpret_cast<PdevProxyResponse*>(resp_buf);
*out_resp_size = sizeof(*resp);
switch (req->op) {
case PdevOp::GET_MMIO: {
pdev_mmio_t mmio;
auto status = pdev_client_.proto_client().GetMmio(req->index, &mmio);
if (status == ZX_OK) {
resp->offset = mmio.offset;
resp->size = mmio.size;
resp_handles[0].reset(mmio.vmo);
*resp_handle_count = 1;
}
return status;
}
case PdevOp::GET_INTERRUPT: {
zx::interrupt irq;
auto status = pdev_client_.proto_client().GetInterrupt(req->index, req->flags, &irq);
if (status == ZX_OK) {
resp_handles[0] = std::move(irq);
*resp_handle_count = 1;
}
return status;
}
case PdevOp::GET_BTI: {
zx::bti bti;
auto status = pdev_client_.proto_client().GetBti(req->index, &bti);
if (status == ZX_OK) {
resp_handles[0] = std::move(bti);
*resp_handle_count = 1;
}
return status;
}
case PdevOp::GET_SMC: {
zx::resource resource;
auto status = pdev_client_.proto_client().GetSmc(req->index, &resource);
if (status == ZX_OK) {
resp_handles[0] = std::move(resource);
*resp_handle_count = 1;
}
return status;
}
case PdevOp::GET_DEVICE_INFO:
return pdev_client_.proto_client().GetDeviceInfo(&resp->device_info);
case PdevOp::GET_BOARD_INFO:
return pdev_client_.proto_client().GetBoardInfo(&resp->board_info);
default:
zxlogf(ERROR, "%s: unknown pdev op %u", __func__, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
}
zx_status_t Fragment::RpcSpi(const uint8_t* req_buf, uint32_t req_size, uint8_t* resp_buf,
uint32_t* out_resp_size, zx::handle* req_handles,
uint32_t req_handle_count, zx::handle* resp_handles,
uint32_t* resp_handle_count) {
if (!spi_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
auto req = reinterpret_cast<const SpiProxyRequest*>(req_buf);
if (req_size < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu", __func__, req_size, sizeof(*req));
return ZX_ERR_INTERNAL;
}
auto resp = reinterpret_cast<SpiProxyResponse*>(resp_buf);
*out_resp_size = sizeof(*resp);
auto txbuf = reinterpret_cast<const uint8_t*>(&req[1]);
auto rxbuf = reinterpret_cast<uint8_t*>(&resp[1]);
switch (req->op) {
case SpiOp::TRANSMIT: {
return spi_client_.proto_client().Transmit(txbuf, req->length);
}
case SpiOp::RECEIVE: {
size_t actual;
*out_resp_size += static_cast<uint32_t>(req->length);
return spi_client_.proto_client().Receive(static_cast<uint32_t>(req->length), rxbuf,
req->length, &actual);
}
case SpiOp::EXCHANGE: {
size_t actual;
*out_resp_size += static_cast<uint32_t>(req->length);
return spi_client_.proto_client().Exchange(txbuf, req->length, rxbuf, req->length, &actual);
}
default:
zxlogf(ERROR, "%s: unknown SPI op %u", __func__, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
}
zx_status_t Fragment::RpcSysmem(const uint8_t* req_buf, uint32_t req_size, uint8_t* resp_buf,
uint32_t* out_resp_size, zx::handle* req_handles,
uint32_t req_handle_count, zx::handle* resp_handles,
uint32_t* resp_handle_count) {
if (!sysmem_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
auto* req = reinterpret_cast<const SysmemProxyRequest*>(req_buf);
if (req_size < sizeof(*req)) {
zxlogf(ERROR, "%s received %u, expecting %zu", __func__, req_size, sizeof(*req));
return ZX_ERR_INTERNAL;
}
uint32_t expected_handle_count;
switch (req->op) {
case SysmemOp::CONNECT:
case SysmemOp::REGISTER_HEAP:
case SysmemOp::REGISTER_SECURE_MEM:
expected_handle_count = 1;
break;
case SysmemOp::UNREGISTER_SECURE_MEM:
expected_handle_count = 0;
break;
}
if (req_handle_count != expected_handle_count) {
zxlogf(ERROR, "%s received %u handles, expecting %u op %u", __func__, req_handle_count,
expected_handle_count, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
*out_resp_size = sizeof(ProxyResponse);
switch (req->op) {
case SysmemOp::CONNECT:
return sysmem_client_.proto_client().Connect(zx::channel(std::move(req_handles[0])));
case SysmemOp::REGISTER_HEAP:
return sysmem_client_.proto_client().RegisterHeap(req->heap,
zx::channel(std::move(req_handles[0])));
case SysmemOp::REGISTER_SECURE_MEM:
return sysmem_client_.proto_client().RegisterSecureMem(
zx::channel(std::move(req_handles[0])));
case SysmemOp::UNREGISTER_SECURE_MEM:
return sysmem_client_.proto_client().UnregisterSecureMem();
default:
zxlogf(ERROR, "%s: unknown sysmem op %u", __func__, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
}
zx_status_t Fragment::RpcDai(const uint8_t* req_buf, uint32_t req_size, uint8_t* resp_buf,
uint32_t* out_resp_size, zx::handle* req_handles,
uint32_t req_handle_count, zx::handle* resp_handles,
uint32_t* resp_handle_count) {
if (!dai_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
auto* req = reinterpret_cast<const DaiProxyRequest*>(req_buf);
auto* resp = reinterpret_cast<DaiProxyResponse*>(resp_buf);
*out_resp_size = sizeof(*resp);
switch (req->op) {
case DaiOp::GET_CHANNEL:
if (req_handle_count != 1) {
zxlogf(ERROR, "%s received %u handles, expecting 1", __func__, req_handle_count);
return ZX_ERR_INTERNAL;
}
return dai_client_.proto_client().Connect(zx::channel(std::move(req_handles[0])));
default:
zxlogf(ERROR, "%s: unknown DAI op %u", __func__, static_cast<uint32_t>(req->op));
return ZX_ERR_INTERNAL;
}
}
zx_status_t Fragment::ReadFidlFromChannel() {
if (!rpc_channel_.is_valid()) {
// This driver is stateless, so we don't need to reset anything here
return ZX_OK;
}
uint8_t req_buf[kProxyMaxTransferSize];
// Ensure all response messages are fully initialized.
uint8_t resp_buf[kProxyMaxTransferSize] = {};
auto* req_header = reinterpret_cast<ProxyRequest*>(&req_buf);
auto* resp_header = reinterpret_cast<ProxyResponse*>(&resp_buf);
uint32_t actual;
zx_handle_t req_handles_raw[ZX_CHANNEL_MAX_MSG_HANDLES];
uint32_t req_handle_count;
auto status = zx_channel_read(rpc_channel_.get(), 0, &req_buf, req_handles_raw, sizeof(req_buf),
std::size(req_handles_raw), &actual, &req_handle_count);
if (status != ZX_OK) {
zxlogf(ERROR, "platform_dev_rxrpc: zx_channel_read failed %d", status);
return status;
}
// There is some expense in constructing/destructing these. If that becomes an issue, we could
// create an incremental construction array type to prevent constructing the extras.
zx::handle req_handles[ZX_CHANNEL_MAX_MSG_HANDLES];
for (uint32_t handle_index = 0; handle_index < req_handle_count; ++handle_index) {
// req_handles_raw handle values are ignored after this point, so no need to clear them.
req_handles[handle_index].reset(req_handles_raw[handle_index]);
}
constexpr uint32_t kMaxRespHandles = 1;
zx::handle resp_handles[kMaxRespHandles];
uint32_t resp_handle_count = 0;
resp_header->txid = req_header->txid;
uint32_t resp_len = 0;
switch (req_header->proto_id) {
case ZX_PROTOCOL_GPIO:
status = RpcGpio(req_buf, actual, resp_buf, &resp_len, req_handles, req_handle_count,
resp_handles, &resp_handle_count);
break;
case ZX_PROTOCOL_PDEV:
status = RpcPdev(req_buf, actual, resp_buf, &resp_len, req_handles, req_handle_count,
resp_handles, &resp_handle_count);
break;
case ZX_PROTOCOL_SPI:
status = RpcSpi(req_buf, actual, resp_buf, &resp_len, req_handles, req_handle_count,
resp_handles, &resp_handle_count);
break;
case ZX_PROTOCOL_SYSMEM:
status = RpcSysmem(req_buf, actual, resp_buf, &resp_len, req_handles, req_handle_count,
resp_handles, &resp_handle_count);
break;
case ZX_PROTOCOL_DAI:
status = RpcDai(req_buf, actual, resp_buf, &resp_len, req_handles, req_handle_count,
resp_handles, &resp_handle_count);
break;
default:
zxlogf(ERROR, "%s: unknown protocol %u", __func__, req_header->proto_id);
return ZX_ERR_INTERNAL;
}
ZX_DEBUG_ASSERT(resp_handle_count <= kMaxRespHandles);
zx_handle_t resp_handles_raw[kMaxRespHandles];
for (uint32_t handle_index = 0; handle_index < resp_handle_count; ++handle_index) {
// Will be transferred or closed by zx_channel_write().
resp_handles_raw[handle_index] = resp_handles[handle_index].release();
}
// set op to match request so zx_channel_write will return our response
resp_header->status = status;
status = zx_channel_write(rpc_channel_.get(), 0, resp_header, resp_len,
(resp_handle_count ? resp_handles_raw : nullptr), resp_handle_count);
if (status != ZX_OK) {
zxlogf(ERROR, "platform_dev_rxrpc: zx_channel_write failed %d", status);
}
return status;
}
zx_status_t Fragment::DdkGetProtocol(uint32_t proto_id, void* out_protocol) {
switch (proto_id) {
case ZX_PROTOCOL_GPIO: {
if (!gpio_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
gpio_client_.proto_client().GetProto(static_cast<gpio_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_DAI: {
if (!dai_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
dai_client_.proto_client().GetProto(static_cast<dai_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_PDEV: {
if (!pdev_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
pdev_client_.proto_client().GetProto(static_cast<pdev_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_SPI: {
if (!spi_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
spi_client_.proto_client().GetProto(static_cast<spi_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_SYSMEM: {
if (!sysmem_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
sysmem_client_.proto_client().GetProto(static_cast<sysmem_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_POWER_IMPL: {
if (!power_impl_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
power_impl_client_.proto_client().GetProto(static_cast<power_impl_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_DSI_IMPL: {
if (!dsi_impl_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
dsi_impl_client_.proto_client().GetProto(static_cast<dsi_impl_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_SDIO: {
if (!sdio_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
sdio_client_.proto_client().GetProto(static_cast<sdio_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_THERMAL: {
if (!thermal_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
thermal_client_.proto_client().GetProto(static_cast<thermal_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_ISP: {
if (!isp_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
isp_client_.proto_client().GetProto(static_cast<isp_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_SHARED_DMA: {
if (!shared_dma_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
shared_dma_client_.proto_client().GetProto(static_cast<shared_dma_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_USB_PHY: {
if (!usb_phy_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
usb_phy_client_.proto_client().GetProto(static_cast<usb_phy_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_MIPI_CSI: {
if (!mipi_csi_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
mipi_csi_client_.proto_client().GetProto(static_cast<mipi_csi_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_CAMERA_SENSOR2: {
if (!camera_sensor2_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
camera_sensor2_client_.proto_client().GetProto(
static_cast<camera_sensor2_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_SCPI: {
if (!scpi_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
scpi_client_.proto_client().GetProto(static_cast<scpi_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_GDC: {
if (!gdc_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
gdc_client_.proto_client().GetProto(static_cast<gdc_protocol_t*>(out_protocol));
return ZX_OK;
}
case ZX_PROTOCOL_GE2D: {
if (!ge2d_client_.proto_client().is_valid()) {
return ZX_ERR_NOT_SUPPORTED;
}
ge2d_client_.proto_client().GetProto(static_cast<ge2d_protocol_t*>(out_protocol));
return ZX_OK;
}
default:
return ZX_ERR_NOT_SUPPORTED;
}
}
void Fragment::DdkRelease() { delete this; }
void Fragment::DdkUnbind(ddk::UnbindTxn txn) {
zx_status_t status = rpc_wait_.Cancel();
if (status != ZX_OK && status != ZX_ERR_NOT_FOUND) {
zxlogf(ERROR, "fragment: failed to cancel rpc_wait_: %s", zx_status_get_string(status));
}
outgoing_.reset();
txn.Reply();
}
const zx_driver_ops_t driver_ops = []() {
zx_driver_ops_t ops = {};
ops.version = DRIVER_OPS_VERSION;
ops.bind = Fragment::Bind;
return ops;
}();
} // namespace fragment
ZIRCON_DRIVER(fragment, fragment::driver_ops, "zircon", "0.1");