| // Copyright 2018 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. | |
| #pragma once | |
| #include <threads.h> | |
| #include <ddk/driver.h> | |
| #include <ddktl/protocol/i2cimpl.h> | |
| #include <fbl/mutex.h> | |
| #include <lib/sync/completion.h> | |
| #include "proxy-protocol.h" | |
| namespace platform_bus { | |
| class PlatformI2cBus { | |
| public: | |
| explicit PlatformI2cBus(const i2c_impl_protocol_t* i2c, uint32_t bus_id); | |
| zx_status_t Start(); | |
| zx_status_t Transact(uint32_t txid, rpc_i2c_req_t* req, uint16_t address, | |
| zx_handle_t channel_handle); | |
| private: | |
| // struct representing an I2C transaction. | |
| struct I2cTxn { | |
| uint32_t txid; | |
| zx_handle_t channel_handle; | |
| list_node_t node; | |
| uint16_t address; | |
| i2c_transact_callback transact_cb; | |
| void* cookie; | |
| size_t length; | |
| size_t cnt; | |
| }; | |
| void Complete(I2cTxn* txn, zx_status_t status, const uint8_t* data, | |
| size_t data_length); | |
| int I2cThread(); | |
| ddk::I2cImplProtocolClient i2c_; | |
| const uint32_t bus_id_; | |
| size_t max_transfer_; | |
| list_node_t queued_txns_ __TA_GUARDED(mutex_); | |
| list_node_t free_txns_ __TA_GUARDED(mutex_); | |
| sync_completion_t txn_signal_; | |
| thrd_t thread_; | |
| fbl::Mutex mutex_; | |
| }; | |
| } // namespace platform_bus |