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 <stdint.h> 24 #include "rmidevice.h" 25 26 enum rmi_hid_mode_type { 27 HID_RMI4_MODE_MOUSE = 0, 28 HID_RMI4_MODE_ATTN_REPORTS = 1, 29 HID_RMI4_MODE_NO_PACKED_ATTN_REPORTS = 2, 30 }; 31 32 class HIDDevice : public RMIDevice 33 { 34 public: HIDDevice()35 HIDDevice() : RMIDevice(), m_inputReport(NULL), m_outputReport(NULL), m_attnData(NULL), 36 m_readData(NULL), 37 m_inputReportSize(0), 38 m_outputReportSize(0), 39 m_featureReportSize(0), 40 m_deviceOpen(false), 41 m_mode(HID_RMI4_MODE_ATTN_REPORTS), 42 m_initialMode(HID_RMI4_MODE_MOUSE), 43 m_transportDeviceName(""), 44 m_driverPath("") 45 {} 46 virtual int Open(const char * filename); 47 virtual int Read(unsigned short addr, unsigned char *buf, 48 unsigned short len); 49 virtual int Write(unsigned short addr, const unsigned char *buf, 50 unsigned short len); 51 virtual int SetMode(int mode); 52 virtual int WaitForAttention(struct timeval * timeout = NULL, 53 unsigned int source_mask = RMI_INTERUPT_SOURCES_ALL_MASK); 54 virtual int GetAttentionReport(struct timeval * timeout, unsigned int source_mask, 55 unsigned char *buf, unsigned int *len); 56 virtual void Close(); 57 virtual void RebindDriver(); ~HIDDevice()58 ~HIDDevice() { Close(); } 59 60 virtual void PrintDeviceInfo(); 61 62 virtual bool FindDevice(enum RMIDeviceType type = RMI_DEVICE_TYPE_ANY); 63 virtual bool CheckABSEvent(); 64 65 private: 66 int m_fd; 67 68 struct hidraw_report_descriptor m_rptDesc; 69 struct hidraw_devinfo m_info; 70 71 unsigned char *m_inputReport; 72 unsigned char *m_outputReport; 73 74 unsigned char *m_attnData; 75 unsigned char *m_readData; 76 int m_dataBytesRead; 77 78 size_t m_inputReportSize; 79 size_t m_outputReportSize; 80 size_t m_featureReportSize; 81 82 bool m_deviceOpen; 83 84 rmi_hid_mode_type m_mode; 85 rmi_hid_mode_type m_initialMode; 86 87 std::string m_transportDeviceName; 88 std::string m_driverPath; 89 90 int GetReport(int *reportId, struct timeval * timeout = NULL); 91 void PrintReport(const unsigned char *report); 92 void ParseReportDescriptor(); 93 94 bool WaitForHidRawDevice(int notifyFd, std::string & hidraw); 95 96 // static HID utility functions 97 static bool LookupHidDeviceName(uint32_t bus, int16_t vendorId, int16_t productId, std::string &deviceName); 98 static bool LookupHidDriverName(std::string &deviceName, std::string &driverName); 99 static bool FindTransportDevice(uint32_t bus, std::string & hidDeviceName, 100 std::string & transportDeviceName, std::string & driverPath); 101 }; 102 103 #endif /* _HIDDEVICE_H_ */ 104