1 /* 2 * Copyright (C) 2014 Andrew Duggan 3 * Copyright (C) 2014 Synaptics Inc 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _HIDDEVICE_H_ 19 #define _HIDDEVICE_H_ 20 21 #include <linux/hidraw.h> 22 #include <string> 23 #include <fstream> 24 #include <stdint.h> 25 #include "rmidevice.h" 26 27 enum rmi_hid_mode_type { 28 HID_RMI4_MODE_MOUSE = 0, 29 HID_RMI4_MODE_ATTN_REPORTS = 1, 30 HID_RMI4_MODE_NO_PACKED_ATTN_REPORTS = 2, 31 }; 32 33 class HIDDevice : public RMIDevice 34 { 35 public: HIDDevice()36 HIDDevice() : RMIDevice(), m_inputReport(NULL), m_outputReport(NULL), m_attnData(NULL), 37 m_readData(NULL), 38 m_inputReportSize(0), 39 m_outputReportSize(0), 40 m_featureReportSize(0), 41 m_deviceOpen(false), 42 m_mode(HID_RMI4_MODE_ATTN_REPORTS), 43 m_initialMode(HID_RMI4_MODE_MOUSE), 44 m_transportDeviceName(""), 45 m_driverPath(""), 46 hasVendorDefineLIDMode(false) 47 {} 48 virtual int Open(const char * filename); 49 virtual int Read(unsigned short addr, unsigned char *buf, 50 unsigned short len); 51 virtual int Write(unsigned short addr, const unsigned char *buf, 52 unsigned short len); 53 virtual int SetMode(int mode); 54 virtual int ToggleInterruptMask(bool enable); 55 virtual int WaitForAttention(struct timeval * timeout = NULL, 56 unsigned int source_mask = RMI_INTERUPT_SOURCES_ALL_MASK); 57 virtual int GetAttentionReport(struct timeval * timeout, unsigned int source_mask, 58 unsigned char *buf, unsigned int *len); 59 virtual void Close(); 60 virtual void RebindDriver(); ~HIDDevice()61 ~HIDDevice() { Close(); } 62 63 virtual void PrintDeviceInfo(); 64 65 virtual bool FindDevice(enum RMIDeviceType type = RMI_DEVICE_TYPE_ANY); 66 virtual bool CheckABSEvent(); 67 68 private: 69 int m_fd; 70 71 struct hidraw_report_descriptor m_rptDesc; 72 struct hidraw_devinfo m_info; 73 74 unsigned char *m_inputReport; 75 unsigned char *m_outputReport; 76 77 unsigned char *m_attnData; 78 unsigned char *m_readData; 79 int m_dataBytesRead; 80 81 size_t m_inputReportSize; 82 size_t m_outputReportSize; 83 size_t m_featureReportSize; 84 85 bool m_deviceOpen; 86 87 rmi_hid_mode_type m_mode; 88 rmi_hid_mode_type m_initialMode; 89 90 std::string m_transportDeviceName; 91 std::string m_driverPath; 92 93 bool hasVendorDefineLIDMode; 94 95 int GetReport(int *reportId, struct timeval * timeout = NULL); 96 void PrintReport(const unsigned char *report); 97 void ParseReportDescriptor(); 98 99 bool WaitForHidRawDevice(int notifyFd, std::string & hidraw); 100 101 // static HID utility functions 102 static bool LookupHidDeviceName(uint32_t bus, int16_t vendorId, int16_t productId, std::string &deviceName); 103 static bool LookupHidDriverName(std::string &deviceName, std::string &driverName); 104 static bool FindTransportDevice(uint32_t bus, std::string & hidDeviceName, 105 std::string & transportDeviceName, std::string & driverPath); 106 }; 107 108 #endif /* _HIDDEVICE_H_ */ 109