blob: 500878ba90aaacc289c25f03d6b58039967becc9 [file] [log] [blame]
Andrew Duggan052556f2014-04-16 11:32:30 -07001/*
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
Andrew Duggan4e811252014-04-03 15:17:57 -070018#include <stdio.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
Andrew Dugganbef9c2d2015-05-06 17:45:48 -070022#include <dirent.h>
Andrew Duggan4e811252014-04-03 15:17:57 -070023#include <errno.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/ioctl.h>
27#include <sys/select.h>
28
29#include <linux/types.h>
30#include <linux/input.h>
31#include <linux/hidraw.h>
32#include <signal.h>
33#include <stdlib.h>
34
35#include "hiddevice.h"
36
37#define RMI_WRITE_REPORT_ID 0x9 // Output Report
38#define RMI_READ_ADDR_REPORT_ID 0xa // Output Report
39#define RMI_READ_DATA_REPORT_ID 0xb // Input Report
40#define RMI_ATTN_REPORT_ID 0xc // Input Report
41#define RMI_SET_RMI_MODE_REPORT_ID 0xf // Feature Report
42
43enum rmi_hid_mode_type {
44 HID_RMI4_MODE_MOUSE = 0,
45 HID_RMI4_MODE_ATTN_REPORTS = 1,
46 HID_RMI4_MODE_NO_PACKED_ATTN_REPORTS = 2,
47};
48
Andrew Duggan8b774392014-06-18 13:11:49 -070049enum hid_report_type {
50 HID_REPORT_TYPE_UNKNOWN = 0x0,
51 HID_REPORT_TYPE_INPUT = 0x81,
52 HID_REPORT_TYPE_OUTPUT = 0x91,
53 HID_REPORT_TYPE_FEATURE = 0xb1,
54};
55
Andrew Duggan4e811252014-04-03 15:17:57 -070056#define HID_RMI4_REPORT_ID 0
57#define HID_RMI4_READ_INPUT_COUNT 1
58#define HID_RMI4_READ_INPUT_DATA 2
59#define HID_RMI4_READ_OUTPUT_ADDR 2
60#define HID_RMI4_READ_OUTPUT_COUNT 4
61#define HID_RMI4_WRITE_OUTPUT_COUNT 1
62#define HID_RMI4_WRITE_OUTPUT_ADDR 2
63#define HID_RMI4_WRITE_OUTPUT_DATA 4
64#define HID_RMI4_FEATURE_MODE 1
65#define HID_RMI4_ATTN_INTERUPT_SOURCES 1
66#define HID_RMI4_ATTN_DATA 2
67
Andrew Dugganfa10fc82014-07-07 17:36:33 -070068#define SYNAPTICS_VENDOR_ID 0x06cb
69
Andrew Duggan4e811252014-04-03 15:17:57 -070070int HIDDevice::Open(const char * filename)
71{
72 int rc;
73 int desc_size;
74
Andrew Duggan72163582014-04-03 16:25:12 -070075 if (!filename)
76 return -EINVAL;
77
Andrew Duggan4e811252014-04-03 15:17:57 -070078 m_fd = open(filename, O_RDWR);
79 if (m_fd < 0)
80 return -1;
81
82 memset(&m_rptDesc, 0, sizeof(m_rptDesc));
83 memset(&m_info, 0, sizeof(m_info));
84
85 rc = ioctl(m_fd, HIDIOCGRDESCSIZE, &desc_size);
86 if (rc < 0)
87 return rc;
88
89 m_rptDesc.size = desc_size;
90 rc = ioctl(m_fd, HIDIOCGRDESC, &m_rptDesc);
91 if (rc < 0)
92 return rc;
93
94 rc = ioctl(m_fd, HIDIOCGRAWINFO, &m_info);
95 if (rc < 0)
96 return rc;
97
Andrew Dugganfa10fc82014-07-07 17:36:33 -070098 if (m_info.vendor != SYNAPTICS_VENDOR_ID) {
99 errno = -ENODEV;
100 return -1;
101 }
102
Andrew Duggan8b774392014-06-18 13:11:49 -0700103 ParseReportSizes();
Andrew Duggan4e811252014-04-03 15:17:57 -0700104
105 m_inputReport = new unsigned char[m_inputReportSize]();
Andrew Dugganfa10fc82014-07-07 17:36:33 -0700106 if (!m_inputReport) {
107 errno = -ENOMEM;
108 return -1;
109 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700110
111 m_outputReport = new unsigned char[m_outputReportSize]();
Andrew Dugganfa10fc82014-07-07 17:36:33 -0700112 if (!m_outputReport) {
113 errno = -ENOMEM;
114 return -1;
115 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700116
117 m_readData = new unsigned char[m_inputReportSize]();
Andrew Dugganfa10fc82014-07-07 17:36:33 -0700118 if (!m_readData) {
119 errno = -ENOMEM;
120 return -1;
121 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700122
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800123 m_attnData = new unsigned char[m_inputReportSize]();
124 if (!m_attnData) {
Andrew Dugganfa10fc82014-07-07 17:36:33 -0700125 errno = -ENOMEM;
126 return -1;
127 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700128
129 m_deviceOpen = true;
130
131 rc = SetMode(HID_RMI4_MODE_ATTN_REPORTS);
132 if (rc)
133 return -1;
134
135 return 0;
136}
137
Andrew Duggan8b774392014-06-18 13:11:49 -0700138void HIDDevice::ParseReportSizes()
139{
140 bool isVendorSpecific = false;
141 bool isReport = false;
142 int totalReportSize = 0;
143 int reportSize = 0;
144 int reportCount = 0;
145 enum hid_report_type hidReportType = HID_REPORT_TYPE_UNKNOWN;
146
147 for (unsigned int i = 0; i < m_rptDesc.size; ++i) {
148 if (isVendorSpecific) {
149 if (m_rptDesc.value[i] == 0x85 || m_rptDesc.value[i] == 0xc0) {
150 if (isReport) {
151 // finish up data on the previous report
152 totalReportSize = (reportSize * reportCount) >> 3;
153
154 switch (hidReportType) {
155 case HID_REPORT_TYPE_INPUT:
156 m_inputReportSize = totalReportSize + 1;
157 break;
158 case HID_REPORT_TYPE_OUTPUT:
159 m_outputReportSize = totalReportSize + 1;
160 break;
161 case HID_REPORT_TYPE_FEATURE:
162 m_featureReportSize = totalReportSize + 1;
163 break;
164 case HID_REPORT_TYPE_UNKNOWN:
165 default:
166 break;
167 }
168 }
169
170 // reset values for the new report
171 totalReportSize = 0;
172 reportSize = 0;
173 reportCount = 0;
174 hidReportType = HID_REPORT_TYPE_UNKNOWN;
175
176 if (m_rptDesc.value[i] == 0x85)
177 isReport = true;
178 else
179 isReport = false;
180
181 if (m_rptDesc.value[i] == 0xc0)
182 isVendorSpecific = false;
183 }
184
185 if (isReport) {
186 if (m_rptDesc.value[i] == 0x75) {
Andrew de los Reyesec066ee2015-09-04 14:43:47 -0700187 if (i + 1 >= m_rptDesc.size)
188 return;
Andrew Duggan8b774392014-06-18 13:11:49 -0700189 reportSize = m_rptDesc.value[++i];
190 continue;
191 }
192
193 if (m_rptDesc.value[i] == 0x95) {
Andrew de los Reyesec066ee2015-09-04 14:43:47 -0700194 if (i + 1 >= m_rptDesc.size)
195 return;
Andrew Duggan8b774392014-06-18 13:11:49 -0700196 reportCount = m_rptDesc.value[++i];
197 continue;
198 }
199
200 if (m_rptDesc.value[i] == HID_REPORT_TYPE_INPUT)
201 hidReportType = HID_REPORT_TYPE_INPUT;
202
203 if (m_rptDesc.value[i] == HID_REPORT_TYPE_OUTPUT)
204 hidReportType = HID_REPORT_TYPE_OUTPUT;
205
206 if (m_rptDesc.value[i] == HID_REPORT_TYPE_FEATURE) {
207 hidReportType = HID_REPORT_TYPE_FEATURE;
208 }
209 }
210 }
211
Andrew de los Reyesec066ee2015-09-04 14:43:47 -0700212 if (i + 2 >= m_rptDesc.size)
213 return;
Andrew Duggan8b774392014-06-18 13:11:49 -0700214 if (m_rptDesc.value[i] == 0x06 && m_rptDesc.value[i + 1] == 0x00
215 && m_rptDesc.value[i + 2] == 0xFF) {
216 isVendorSpecific = true;
217 i += 2;
218 }
219 }
220}
221
Andrew Duggan4e811252014-04-03 15:17:57 -0700222int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
223{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700224 ssize_t count;
Andrew Duggan4e811252014-04-03 15:17:57 -0700225 size_t bytesReadPerRequest;
226 size_t bytesInDataReport;
227 size_t totalBytesRead;
228 size_t bytesPerRequest;
229 size_t bytesWritten;
230 size_t bytesToRequest;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800231 int reportId;
Andrew Duggan4e811252014-04-03 15:17:57 -0700232 int rc;
233
234 if (!m_deviceOpen)
235 return -1;
236
237 if (m_bytesPerReadRequest)
238 bytesPerRequest = m_bytesPerReadRequest;
239 else
240 bytesPerRequest = len;
241
242 for (totalBytesRead = 0; totalBytesRead < len; totalBytesRead += bytesReadPerRequest) {
243 count = 0;
244 if ((len - totalBytesRead) < bytesPerRequest)
245 bytesToRequest = len % bytesPerRequest;
246 else
247 bytesToRequest = bytesPerRequest;
248
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700249 if (m_outputReportSize < HID_RMI4_READ_OUTPUT_COUNT + 2) {
250 return -1;
251 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700252 m_outputReport[HID_RMI4_REPORT_ID] = RMI_READ_ADDR_REPORT_ID;
253 m_outputReport[1] = 0; /* old 1 byte read count */
254 m_outputReport[HID_RMI4_READ_OUTPUT_ADDR] = addr & 0xFF;
255 m_outputReport[HID_RMI4_READ_OUTPUT_ADDR + 1] = (addr >> 8) & 0xFF;
256 m_outputReport[HID_RMI4_READ_OUTPUT_COUNT] = bytesToRequest & 0xFF;
257 m_outputReport[HID_RMI4_READ_OUTPUT_COUNT + 1] = (bytesToRequest >> 8) & 0xFF;
258
259 m_dataBytesRead = 0;
260
261 for (bytesWritten = 0; bytesWritten < m_outputReportSize; bytesWritten += count) {
262 m_bCancel = false;
263 count = write(m_fd, m_outputReport + bytesWritten,
264 m_outputReportSize - bytesWritten);
265 if (count < 0) {
266 if (errno == EINTR && m_deviceOpen && !m_bCancel)
267 continue;
268 else
269 return count;
270 }
271 break;
272 }
273
274 bytesReadPerRequest = 0;
275 while (bytesReadPerRequest < bytesToRequest) {
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800276 rc = GetReport(&reportId);
277 if (rc > 0 && reportId == RMI_READ_DATA_REPORT_ID) {
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700278 if (static_cast<ssize_t>(m_inputReportSize) <
279 std::max(HID_RMI4_READ_INPUT_COUNT,
280 HID_RMI4_READ_INPUT_DATA))
281 return -1;
Andrew Duggan4e811252014-04-03 15:17:57 -0700282 bytesInDataReport = m_readData[HID_RMI4_READ_INPUT_COUNT];
283 memcpy(buf + bytesReadPerRequest, &m_readData[HID_RMI4_READ_INPUT_DATA],
284 bytesInDataReport);
285 bytesReadPerRequest += bytesInDataReport;
286 m_dataBytesRead = 0;
287 }
288 }
289 addr += bytesPerRequest;
290 }
291
292 return totalBytesRead;
293}
294
295int HIDDevice::Write(unsigned short addr, const unsigned char *buf, unsigned short len)
296{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700297 ssize_t count;
Andrew Duggan4e811252014-04-03 15:17:57 -0700298
299 if (!m_deviceOpen)
300 return -1;
301
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700302 if (static_cast<ssize_t>(m_outputReportSize) <
303 HID_RMI4_WRITE_OUTPUT_DATA + len)
304 return -1;
Andrew Duggan4e811252014-04-03 15:17:57 -0700305 m_outputReport[HID_RMI4_REPORT_ID] = RMI_WRITE_REPORT_ID;
306 m_outputReport[HID_RMI4_WRITE_OUTPUT_COUNT] = len;
307 m_outputReport[HID_RMI4_WRITE_OUTPUT_ADDR] = addr & 0xFF;
308 m_outputReport[HID_RMI4_WRITE_OUTPUT_ADDR + 1] = (addr >> 8) & 0xFF;
309 memcpy(&m_outputReport[HID_RMI4_WRITE_OUTPUT_DATA], buf, len);
310
311 for (;;) {
312 m_bCancel = false;
313 count = write(m_fd, m_outputReport, m_outputReportSize);
314 if (count < 0) {
315 if (errno == EINTR && m_deviceOpen && !m_bCancel)
316 continue;
317 else
318 return count;
319 }
320 return count;
321 }
322}
323
324int HIDDevice::SetMode(int mode)
325{
326 int rc;
327 char buf[2];
328
329 if (!m_deviceOpen)
330 return -1;
331
332 buf[0] = 0xF;
333 buf[1] = mode;
334 rc = ioctl(m_fd, HIDIOCSFEATURE(2), buf);
335 if (rc < 0) {
336 perror("HIDIOCSFEATURE");
337 return rc;
338 }
339
340 return 0;
341}
342
343void HIDDevice::Close()
344{
345 if (!m_deviceOpen)
346 return;
347
348 SetMode(HID_RMI4_MODE_MOUSE);
349 m_deviceOpen = false;
350 close(m_fd);
351 m_fd = -1;
352
353 delete[] m_inputReport;
354 m_inputReport = NULL;
355 delete[] m_outputReport;
356 m_outputReport = NULL;
357 delete[] m_readData;
358 m_readData = NULL;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800359 delete[] m_attnData;
360 m_attnData = NULL;
Andrew Duggan4e811252014-04-03 15:17:57 -0700361}
362
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800363int HIDDevice::WaitForAttention(struct timeval * timeout, unsigned int source_mask)
Andrew Duggan4e811252014-04-03 15:17:57 -0700364{
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800365 return GetAttentionReport(timeout, source_mask, NULL, NULL);
Andrew Duggan4e811252014-04-03 15:17:57 -0700366}
367
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800368int HIDDevice::GetAttentionReport(struct timeval * timeout, unsigned int source_mask,
369 unsigned char *buf, unsigned int *len)
Andrew Duggan4e811252014-04-03 15:17:57 -0700370{
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800371 int rc = 0;
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700372 unsigned int bytes = m_inputReportSize;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800373 int reportId;
Andrew Duggan4e811252014-04-03 15:17:57 -0700374
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700375 if (len && m_inputReportSize < *len) {
Andrew Duggan4e811252014-04-03 15:17:57 -0700376 bytes = *len;
377 *len = m_inputReportSize;
378 }
379
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800380 // Assume the Linux implementation of select with timeout set to the
381 // time remaining.
382 while (!timeout || (timeout->tv_sec != 0 || timeout->tv_usec != 0)) {
383 rc = GetReport(&reportId, timeout);
384 if (rc > 0) {
385 if (reportId == RMI_ATTN_REPORT_ID) {
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700386 if (buf) {
387 if (bytes > m_inputReportSize ||
388 m_inputReportSize < HID_RMI4_ATTN_INTERUPT_SOURCES + 1)
389 return -1;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800390 memcpy(buf, m_attnData, bytes);
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700391 }
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800392 if (source_mask & m_attnData[HID_RMI4_ATTN_INTERUPT_SOURCES])
393 return rc;
394 }
395 } else {
396 return rc;
397 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700398 }
399
400 return rc;
401}
402
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800403int HIDDevice::GetReport(int *reportId, struct timeval * timeout)
Andrew Duggan4e811252014-04-03 15:17:57 -0700404{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700405 ssize_t count = 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700406 fd_set fds;
407 int rc;
Andrew Duggan4e811252014-04-03 15:17:57 -0700408
409 if (!m_deviceOpen)
410 return -1;
411
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700412 if (m_inputReportSize < HID_RMI4_REPORT_ID + 1)
413 return -1;
414
Andrew Duggan4e811252014-04-03 15:17:57 -0700415 for (;;) {
416 FD_ZERO(&fds);
417 FD_SET(m_fd, &fds);
418
419 rc = select(m_fd + 1, &fds, NULL, NULL, timeout);
420 if (rc == 0) {
421 return -ETIMEDOUT;
422 } else if (rc < 0) {
423 if (errno == EINTR && m_deviceOpen && !m_bCancel)
424 continue;
425 else
426 return rc;
427 } else if (rc > 0 && FD_ISSET(m_fd, &fds)) {
428 size_t offset = 0;
429 for (;;) {
430 m_bCancel = false;
431 count = read(m_fd, m_inputReport + offset, m_inputReportSize - offset);
432 if (count < 0) {
433 if (errno == EINTR && m_deviceOpen && !m_bCancel)
434 continue;
435 else
436 return count;
437 }
438 offset += count;
439 if (offset == m_inputReportSize)
440 break;
441 }
442 }
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800443 break;
Andrew Duggan4e811252014-04-03 15:17:57 -0700444 }
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800445
446 if (reportId)
447 *reportId = m_inputReport[HID_RMI4_REPORT_ID];
448
449 if (m_inputReport[HID_RMI4_REPORT_ID] == RMI_ATTN_REPORT_ID) {
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700450 if (static_cast<ssize_t>(m_inputReportSize) < count)
451 return -1;
452 memcpy(m_attnData, m_inputReport, count /*offset?*/);
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800453 } else if (m_inputReport[HID_RMI4_REPORT_ID] == RMI_READ_DATA_REPORT_ID) {
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700454 if (static_cast<ssize_t>(m_inputReportSize) < count)
455 return -1;
456 memcpy(m_readData, m_inputReport, count /*offset?*/);
457 m_dataBytesRead = count /*offset?*/;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800458 }
459 return 1;
Andrew Duggan4e811252014-04-03 15:17:57 -0700460}
461
462void HIDDevice::PrintReport(const unsigned char *report)
463{
464 int i;
465 int len = 0;
466 const unsigned char * data;
467 int addr = 0;
468
469 switch (report[HID_RMI4_REPORT_ID]) {
470 case RMI_WRITE_REPORT_ID:
471 len = report[HID_RMI4_WRITE_OUTPUT_COUNT];
472 data = &report[HID_RMI4_WRITE_OUTPUT_DATA];
473 addr = (report[HID_RMI4_WRITE_OUTPUT_ADDR] & 0xFF)
474 | ((report[HID_RMI4_WRITE_OUTPUT_ADDR + 1] & 0xFF) << 8);
475 fprintf(stdout, "Write Report:\n");
476 fprintf(stdout, "Address = 0x%02X\n", addr);
477 fprintf(stdout, "Length = 0x%02X\n", len);
478 break;
479 case RMI_READ_ADDR_REPORT_ID:
480 addr = (report[HID_RMI4_READ_OUTPUT_ADDR] & 0xFF)
481 | ((report[HID_RMI4_READ_OUTPUT_ADDR + 1] & 0xFF) << 8);
482 len = (report[HID_RMI4_READ_OUTPUT_COUNT] & 0xFF)
483 | ((report[HID_RMI4_READ_OUTPUT_COUNT + 1] & 0xFF) << 8);
484 fprintf(stdout, "Read Request (Output Report):\n");
485 fprintf(stdout, "Address = 0x%02X\n", addr);
486 fprintf(stdout, "Length = 0x%02X\n", len);
487 return;
488 break;
489 case RMI_READ_DATA_REPORT_ID:
490 len = report[HID_RMI4_READ_INPUT_COUNT];
491 data = &report[HID_RMI4_READ_INPUT_DATA];
492 fprintf(stdout, "Read Data Report:\n");
493 fprintf(stdout, "Length = 0x%02X\n", len);
494 break;
495 case RMI_ATTN_REPORT_ID:
496 fprintf(stdout, "Attention Report:\n");
497 len = 28;
498 data = &report[HID_RMI4_ATTN_DATA];
499 fprintf(stdout, "Interrupt Sources: 0x%02X\n",
500 report[HID_RMI4_ATTN_INTERUPT_SOURCES]);
501 break;
502 default:
503 fprintf(stderr, "Unknown Report: ID 0x%02x\n", report[HID_RMI4_REPORT_ID]);
504 return;
505 }
506
507 fprintf(stdout, "Data:\n");
508 for (i = 0; i < len; ++i) {
509 fprintf(stdout, "0x%02X ", data[i]);
510 if (i % 8 == 7) {
511 fprintf(stdout, "\n");
512 }
513 }
514 fprintf(stdout, "\n\n");
Andrew Duggan64d6e592014-07-01 13:02:10 -0700515}
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700516
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700517// Print protocol specific device information
518void HIDDevice::PrintDeviceInfo()
519{
520 fprintf(stdout, "HID device info:\nBus: %s Vendor: 0x%04x Product: 0x%04x\n",
521 m_info.bustype == BUS_I2C ? "I2C" : "USB", m_info.vendor, m_info.product);
Andrew Dugganb6302c32015-05-07 11:07:22 -0700522 fprintf(stdout, "Report sizes: input: %ld output: %ld\n", (unsigned long)m_inputReportSize,
523 (unsigned long)m_outputReportSize);
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700524}
525
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700526bool WriteDeviceNameToFile(const char * file, const char * str)
527{
528 int fd;
529 ssize_t size;
530
531 fd = open(file, O_WRONLY);
532 if (fd < 0)
533 return false;
534
535 for (;;) {
536 size = write(fd, str, 19);
537 if (size < 0) {
538 if (errno == EINTR)
539 continue;
540
541 return false;
542 }
543 break;
544 }
545 close(fd);
546
547 return true;
548}
549
550void HIDDevice::RebindDriver()
551{
552 int bus = m_info.bustype;
553 int vendor = m_info.vendor;
554 int product = m_info.product;
555 std::string hidDeviceName;
556 std::string transportDeviceName;
557 std::string driverPath;
558 std::string bindFile;
559 std::string unbindFile;
560 std::string hidrawFile;
561
562 Close();
563
564 if (!LookupHidDeviceName(bus, vendor, product, hidDeviceName)) {
565 fprintf(stderr, "Failed to find HID device name for the specified device: bus (0x%x) vendor: (0x%x) product: (0x%x)\n",
566 bus, vendor, product);
567 return;
568 }
569
Andrew Dugganb6302c32015-05-07 11:07:22 -0700570 if (!FindTransportDevice(bus, hidDeviceName, transportDeviceName, driverPath)) {
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700571 fprintf(stderr, "Failed to find the transport device / driver for %s\n", hidDeviceName.c_str());
572 return;
573 }
574
575 bindFile = driverPath + "bind";
576 unbindFile = driverPath + "unbind";
577
578 if (!WriteDeviceNameToFile(unbindFile.c_str(), transportDeviceName.c_str())) {
579 fprintf(stderr, "Failed to unbind HID device %s: %s\n",
580 transportDeviceName.c_str(), strerror(errno));
581 return;
582 }
583
584 if (!WriteDeviceNameToFile(bindFile.c_str(), transportDeviceName.c_str())) {
585 fprintf(stderr, "Failed to bind HID device %s: %s\n",
586 transportDeviceName.c_str(), strerror(errno));
587 return;
588 }
589
Andrew Dugganb6302c32015-05-07 11:07:22 -0700590 // The hid device id has changed since this is now a new hid device. Now we have to look up the new name.
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700591 if (!LookupHidDeviceName(bus, vendor, product, hidDeviceName)) {
592 fprintf(stderr, "Failed to find HID device name for the specified device: bus (0x%x) vendor: (0x%x) product: (0x%x)\n",
593 bus, vendor, product);
594 return;
595 }
596
597 if (!FindHidRawFile(hidDeviceName, hidrawFile)) {
598 fprintf(stderr, "Failed to find the hidraw device file for %s\n", hidDeviceName.c_str());
599 return;
600 }
601
602 Open(hidrawFile.c_str());
603}
604
Andrew Dugganb6302c32015-05-07 11:07:22 -0700605bool HIDDevice::FindTransportDevice(int bus, std::string & hidDeviceName,
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700606 std::string & transportDeviceName, std::string & driverPath)
607{
608 std::string devicePrefix = "/sys/bus/";
609 std::string devicePath;
610 struct dirent * devicesDirEntry;
611 DIR * devicesDir;
612 struct dirent * devDirEntry;
613 DIR * devDir;
614 bool deviceFound = false;
615 ssize_t sz;
616
617 if (bus == BUS_I2C) {
618 devicePrefix += "i2c/";
619 driverPath = devicePrefix + "drivers/i2c_hid/";
620 } else {
621 devicePrefix += "usb/";
622 driverPath = devicePrefix + "drivers/usbhid/";
623 }
624 devicePath = devicePrefix + "devices/";
625
626 devicesDir = opendir(devicePath.c_str());
627 if (!devicesDir)
628 return false;
629
630 while((devicesDirEntry = readdir(devicesDir)) != NULL) {
631 if (devicesDirEntry->d_type != DT_LNK)
632 continue;
633
634 char buf[PATH_MAX];
635
636 sz = readlinkat(dirfd(devicesDir), devicesDirEntry->d_name, buf, PATH_MAX);
637 if (sz < 0)
638 continue;
639
640 buf[sz] = 0;
641
642 std::string fullLinkPath = devicePath + buf;
643 devDir = opendir(fullLinkPath.c_str());
644 if (!devDir) {
645 fprintf(stdout, "opendir failed\n");
646 continue;
647 }
648
649 while ((devDirEntry = readdir(devDir)) != NULL) {
650 if (!strcmp(devDirEntry->d_name, hidDeviceName.c_str())) {
651 transportDeviceName = devicesDirEntry->d_name;
652 deviceFound = true;
653 break;
654 }
655 }
656 closedir(devDir);
657
658 if (deviceFound)
659 break;
660 }
661 closedir(devicesDir);
662
663 return deviceFound;
664}
665
666bool HIDDevice::LookupHidDeviceName(int bus, int vendorId, int productId, std::string & deviceName)
667{
668 bool ret = false;
669 struct dirent * devDirEntry;
670 DIR * devDir;
671 char devicePrefix[15];
672
673 snprintf(devicePrefix, 15, "%04X:%04X:%04X", bus, vendorId, productId);
674
675 devDir = opendir("/sys/bus/hid/devices");
676 if (!devDir)
677 return false;
678
679 while ((devDirEntry = readdir(devDir)) != NULL) {
680 if (!strncmp(devDirEntry->d_name, devicePrefix, 14)) {
681 deviceName = devDirEntry->d_name;
682 ret = true;
683 break;
684 }
685 }
686 closedir(devDir);
687
688 return ret;
689}
690
691bool HIDDevice::FindHidRawFile(std::string & deviceName, std::string & hidrawFile)
692{
693 bool ret = false;
694 char hidrawDir[PATH_MAX];
695 struct dirent * devDirEntry;
696 DIR * devDir;
697
698 snprintf(hidrawDir, PATH_MAX, "/sys/bus/hid/devices/%s/hidraw", deviceName.c_str());
699
700 devDir = opendir(hidrawDir);
701 if (!devDir)
702 return false;
703
704 while ((devDirEntry = readdir(devDir)) != NULL) {
705 if (!strncmp(devDirEntry->d_name, "hidraw", 6)) {
706 hidrawFile = std::string("/dev/") + devDirEntry->d_name;
707 ret = true;
708 break;
709 }
710 }
711 closedir(devDir);
712
713 return ret;
Andrew de los Reyes242ea832015-09-04 14:40:06 -0700714}