1 /* 2 * Copyright 2020, Jérôme Duval, jerome.duval@gmail.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef I2C_PRIVATE_H 6 #define I2C_PRIVATE_H 7 8 9 #include <new> 10 #include <stdio.h> 11 #include <string.h> 12 13 #include <lock.h> 14 #include <util/AutoLock.h> 15 #include <i2c.h> 16 17 18 //#define I2C_TRACE 19 #ifdef I2C_TRACE 20 # define TRACE(x...) dprintf("\33[33mi2c:\33[0m " x) 21 #else 22 # define TRACE(x...) 23 #endif 24 #define TRACE_ALWAYS(x...) dprintf("\33[33mi2c:\33[0m " x) 25 #define ERROR(x...) TRACE_ALWAYS(x) 26 #define CALLED() TRACE("CALLED %s\n", __PRETTY_FUNCTION__) 27 28 29 #define I2C_SIM_MODULE_NAME "bus_managers/i2c/sim/driver_v1" 30 31 #define I2C_PATHID_GENERATOR "i2c/path_id" 32 #define I2C_BUS_RAW_MODULE_NAME "bus_managers/i2c/bus/raw/device_v1" 33 34 35 class I2CBus; 36 class I2CDevice; 37 38 extern device_manager_info *gDeviceManager; 39 extern struct device_module_info gI2CBusRawModule; 40 extern i2c_bus_interface gI2CBusModule; 41 extern i2c_device_interface gI2CDeviceModule; 42 43 class I2CDevice { 44 public: 45 I2CDevice(device_node* node, I2CBus* bus, 46 i2c_addr slaveAddress); 47 ~I2CDevice(); 48 49 status_t InitCheck(); 50 51 status_t ExecCommand(i2c_op op, const void *cmdBuffer, 52 size_t cmdLength, void* dataBuffer, 53 size_t dataLength); 54 status_t AcquireBus(); 55 void ReleaseBus(); 56 57 private: 58 device_node* fNode; 59 I2CBus* fBus; 60 i2c_addr fSlaveAddress; 61 }; 62 63 64 class I2CBus { 65 public: 66 I2CBus(device_node *node, uint8 id); 67 ~I2CBus(); 68 69 status_t InitCheck(); 70 71 status_t ExecCommand(i2c_op op, i2c_addr slaveAddress, 72 const void *cmdBuffer, size_t cmdLength, 73 void* dataBuffer, size_t dataLength); 74 status_t Scan(); 75 status_t RegisterDevice(i2c_addr slaveAddress, 76 char* hid, char** cid, 77 acpi_handle acpiHandle); 78 status_t AcquireBus(); 79 void ReleaseBus(); 80 81 private: 82 device_node* fNode; 83 uint8 fID; 84 i2c_sim_interface* fController; 85 void* fCookie; 86 }; 87 88 89 90 #endif // I2C_PRIVATE_H 91