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