1 /* 2 * Copyright 2020, Jérôme Duval, jerome.duval@gmail.com. 3 * Copyright 2008-2011, Michael Lotz <mmlr@mlotz.ch> 4 * Distributed under the terms of the MIT license. 5 */ 6 #ifndef I2C_HID_DEVICE_H 7 #define I2C_HID_DEVICE_H 8 9 10 #include <i2c.h> 11 12 #include "HIDParser.h" 13 14 15 /* 5.1.1 - HID Descriptor Format */ 16 typedef struct i2c_hid_descriptor { 17 uint16 wHIDDescLength; 18 uint16 bcdVersion; 19 uint16 wReportDescLength; 20 uint16 wReportDescRegister; 21 uint16 wInputRegister; 22 uint16 wMaxInputLength; 23 uint16 wOutputRegister; 24 uint16 wMaxOutputLength; 25 uint16 wCommandRegister; 26 uint16 wDataRegister; 27 uint16 wVendorID; 28 uint16 wProductID; 29 uint16 wVersionID; 30 uint32 reserved; 31 } _PACKED i2c_hid_descriptor; 32 33 34 enum { 35 I2C_HID_CMD_RESET = 0x1, 36 I2C_HID_CMD_GET_REPORT = 0x2, 37 I2C_HID_CMD_SET_REPORT = 0x3, 38 I2C_HID_CMD_GET_IDLE = 0x4, 39 I2C_HID_CMD_SET_IDLE = 0x5, 40 I2C_HID_CMD_GET_PROTOCOL = 0x6, 41 I2C_HID_CMD_SET_PROTOCOL = 0x7, 42 I2C_HID_CMD_SET_POWER = 0x8, 43 }; 44 45 enum { 46 I2C_HID_POWER_ON = 0x0, 47 I2C_HID_POWER_OFF = 0x1, 48 }; 49 50 51 class ProtocolHandler; 52 53 54 class HIDDevice { 55 public: 56 HIDDevice(uint16 descriptorAddress, i2c_device_interface* i2c, 57 i2c_device i2cCookie); 58 ~HIDDevice(); 59 60 status_t InitCheck() const { return fStatus; } 61 62 bool IsOpen() const { return fOpenCount > 0; } 63 status_t Open(ProtocolHandler *handler, uint32 flags); 64 status_t Close(ProtocolHandler *handler); 65 int32 OpenCount() const { return fOpenCount; } 66 67 void Removed(); 68 bool IsRemoved() const { return fRemoved; } 69 70 status_t MaybeScheduleTransfer(HIDReport *report); 71 72 status_t SendReport(HIDReport *report); 73 74 HIDParser & Parser() { return fParser; } 75 ProtocolHandler * ProtocolHandlerAt(uint32 index) const; 76 77 private: 78 static void _TransferCallback(void *cookie, 79 status_t status, void *data, 80 size_t actualLength); 81 static void _UnstallCallback(void *cookie, 82 status_t status, void *data, 83 size_t actualLength); 84 85 status_t _Reset(); 86 status_t _SetPower(uint8 power); 87 status_t _FetchBuffer(uint8* cmd, size_t cmdLength, 88 void* buffer, size_t bufferLength); 89 status_t _FetchReport(uint8 type, uint8 id, 90 size_t reportSize); 91 status_t _ExecCommand(i2c_op op, uint8* cmd, 92 size_t cmdLength, void* buffer, 93 size_t bufferLength); 94 95 private: 96 status_t fStatus; 97 98 99 bigtime_t fTransferLastschedule; 100 int32 fTransferScheduled; 101 size_t fTransferBufferSize; 102 uint8 * fTransferBuffer; 103 104 int32 fOpenCount; 105 bool fRemoved; 106 107 HIDParser fParser; 108 109 uint32 fProtocolHandlerCount; 110 ProtocolHandler * fProtocolHandlerList; 111 112 uint16 fDescriptorAddress; 113 i2c_hid_descriptor fDescriptor; 114 115 uint8* fReportDescriptor; 116 117 i2c_device_interface* fI2C; 118 i2c_device fI2CCookie; 119 120 }; 121 122 123 #endif // I2C_HID_DEVICE_H 124