blob: e4b956cd27d40a82296beba9941dacd6d5b0e7cb [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) {
187 reportSize = m_rptDesc.value[++i];
188 continue;
189 }
190
191 if (m_rptDesc.value[i] == 0x95) {
192 reportCount = m_rptDesc.value[++i];
193 continue;
194 }
195
196 if (m_rptDesc.value[i] == HID_REPORT_TYPE_INPUT)
197 hidReportType = HID_REPORT_TYPE_INPUT;
198
199 if (m_rptDesc.value[i] == HID_REPORT_TYPE_OUTPUT)
200 hidReportType = HID_REPORT_TYPE_OUTPUT;
201
202 if (m_rptDesc.value[i] == HID_REPORT_TYPE_FEATURE) {
203 hidReportType = HID_REPORT_TYPE_FEATURE;
204 }
205 }
206 }
207
208 if (m_rptDesc.value[i] == 0x06 && m_rptDesc.value[i + 1] == 0x00
209 && m_rptDesc.value[i + 2] == 0xFF) {
210 isVendorSpecific = true;
211 i += 2;
212 }
213 }
214}
215
Andrew Duggan4e811252014-04-03 15:17:57 -0700216int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
217{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700218 ssize_t count;
Andrew Duggan4e811252014-04-03 15:17:57 -0700219 size_t bytesReadPerRequest;
220 size_t bytesInDataReport;
221 size_t totalBytesRead;
222 size_t bytesPerRequest;
223 size_t bytesWritten;
224 size_t bytesToRequest;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800225 int reportId;
Andrew Duggan4e811252014-04-03 15:17:57 -0700226 int rc;
227
228 if (!m_deviceOpen)
229 return -1;
230
231 if (m_bytesPerReadRequest)
232 bytesPerRequest = m_bytesPerReadRequest;
233 else
234 bytesPerRequest = len;
235
236 for (totalBytesRead = 0; totalBytesRead < len; totalBytesRead += bytesReadPerRequest) {
237 count = 0;
238 if ((len - totalBytesRead) < bytesPerRequest)
239 bytesToRequest = len % bytesPerRequest;
240 else
241 bytesToRequest = bytesPerRequest;
242
243 m_outputReport[HID_RMI4_REPORT_ID] = RMI_READ_ADDR_REPORT_ID;
244 m_outputReport[1] = 0; /* old 1 byte read count */
245 m_outputReport[HID_RMI4_READ_OUTPUT_ADDR] = addr & 0xFF;
246 m_outputReport[HID_RMI4_READ_OUTPUT_ADDR + 1] = (addr >> 8) & 0xFF;
247 m_outputReport[HID_RMI4_READ_OUTPUT_COUNT] = bytesToRequest & 0xFF;
248 m_outputReport[HID_RMI4_READ_OUTPUT_COUNT + 1] = (bytesToRequest >> 8) & 0xFF;
249
250 m_dataBytesRead = 0;
251
252 for (bytesWritten = 0; bytesWritten < m_outputReportSize; bytesWritten += count) {
253 m_bCancel = false;
254 count = write(m_fd, m_outputReport + bytesWritten,
255 m_outputReportSize - bytesWritten);
256 if (count < 0) {
257 if (errno == EINTR && m_deviceOpen && !m_bCancel)
258 continue;
259 else
260 return count;
261 }
262 break;
263 }
264
265 bytesReadPerRequest = 0;
266 while (bytesReadPerRequest < bytesToRequest) {
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800267 rc = GetReport(&reportId);
268 if (rc > 0 && reportId == RMI_READ_DATA_REPORT_ID) {
Andrew Duggan4e811252014-04-03 15:17:57 -0700269 bytesInDataReport = m_readData[HID_RMI4_READ_INPUT_COUNT];
270 memcpy(buf + bytesReadPerRequest, &m_readData[HID_RMI4_READ_INPUT_DATA],
271 bytesInDataReport);
272 bytesReadPerRequest += bytesInDataReport;
273 m_dataBytesRead = 0;
274 }
275 }
276 addr += bytesPerRequest;
277 }
278
279 return totalBytesRead;
280}
281
282int HIDDevice::Write(unsigned short addr, const unsigned char *buf, unsigned short len)
283{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700284 ssize_t count;
Andrew Duggan4e811252014-04-03 15:17:57 -0700285
286 if (!m_deviceOpen)
287 return -1;
288
289 m_outputReport[HID_RMI4_REPORT_ID] = RMI_WRITE_REPORT_ID;
290 m_outputReport[HID_RMI4_WRITE_OUTPUT_COUNT] = len;
291 m_outputReport[HID_RMI4_WRITE_OUTPUT_ADDR] = addr & 0xFF;
292 m_outputReport[HID_RMI4_WRITE_OUTPUT_ADDR + 1] = (addr >> 8) & 0xFF;
293 memcpy(&m_outputReport[HID_RMI4_WRITE_OUTPUT_DATA], buf, len);
294
295 for (;;) {
296 m_bCancel = false;
297 count = write(m_fd, m_outputReport, m_outputReportSize);
298 if (count < 0) {
299 if (errno == EINTR && m_deviceOpen && !m_bCancel)
300 continue;
301 else
302 return count;
303 }
304 return count;
305 }
306}
307
308int HIDDevice::SetMode(int mode)
309{
310 int rc;
311 char buf[2];
312
313 if (!m_deviceOpen)
314 return -1;
315
316 buf[0] = 0xF;
317 buf[1] = mode;
318 rc = ioctl(m_fd, HIDIOCSFEATURE(2), buf);
319 if (rc < 0) {
320 perror("HIDIOCSFEATURE");
321 return rc;
322 }
323
324 return 0;
325}
326
327void HIDDevice::Close()
328{
329 if (!m_deviceOpen)
330 return;
331
332 SetMode(HID_RMI4_MODE_MOUSE);
333 m_deviceOpen = false;
334 close(m_fd);
335 m_fd = -1;
336
337 delete[] m_inputReport;
338 m_inputReport = NULL;
339 delete[] m_outputReport;
340 m_outputReport = NULL;
341 delete[] m_readData;
342 m_readData = NULL;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800343 delete[] m_attnData;
344 m_attnData = NULL;
Andrew Duggan4e811252014-04-03 15:17:57 -0700345}
346
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800347int HIDDevice::WaitForAttention(struct timeval * timeout, unsigned int source_mask)
Andrew Duggan4e811252014-04-03 15:17:57 -0700348{
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800349 return GetAttentionReport(timeout, source_mask, NULL, NULL);
Andrew Duggan4e811252014-04-03 15:17:57 -0700350}
351
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800352int HIDDevice::GetAttentionReport(struct timeval * timeout, unsigned int source_mask,
353 unsigned char *buf, unsigned int *len)
Andrew Duggan4e811252014-04-03 15:17:57 -0700354{
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800355 int rc = 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700356 int bytes = m_inputReportSize;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800357 int reportId;
Andrew Duggan4e811252014-04-03 15:17:57 -0700358
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700359 if (len && m_inputReportSize < *len) {
Andrew Duggan4e811252014-04-03 15:17:57 -0700360 bytes = *len;
361 *len = m_inputReportSize;
362 }
363
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800364 // Assume the Linux implementation of select with timeout set to the
365 // time remaining.
366 while (!timeout || (timeout->tv_sec != 0 || timeout->tv_usec != 0)) {
367 rc = GetReport(&reportId, timeout);
368 if (rc > 0) {
369 if (reportId == RMI_ATTN_REPORT_ID) {
370 if (buf)
371 memcpy(buf, m_attnData, bytes);
372 if (source_mask & m_attnData[HID_RMI4_ATTN_INTERUPT_SOURCES])
373 return rc;
374 }
375 } else {
376 return rc;
377 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700378 }
379
380 return rc;
381}
382
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800383int HIDDevice::GetReport(int *reportId, struct timeval * timeout)
Andrew Duggan4e811252014-04-03 15:17:57 -0700384{
Andrew Duggan64d6e592014-07-01 13:02:10 -0700385 ssize_t count = 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700386 fd_set fds;
387 int rc;
Andrew Duggan4e811252014-04-03 15:17:57 -0700388
389 if (!m_deviceOpen)
390 return -1;
391
392 for (;;) {
393 FD_ZERO(&fds);
394 FD_SET(m_fd, &fds);
395
396 rc = select(m_fd + 1, &fds, NULL, NULL, timeout);
397 if (rc == 0) {
398 return -ETIMEDOUT;
399 } else if (rc < 0) {
400 if (errno == EINTR && m_deviceOpen && !m_bCancel)
401 continue;
402 else
403 return rc;
404 } else if (rc > 0 && FD_ISSET(m_fd, &fds)) {
405 size_t offset = 0;
406 for (;;) {
407 m_bCancel = false;
408 count = read(m_fd, m_inputReport + offset, m_inputReportSize - offset);
409 if (count < 0) {
410 if (errno == EINTR && m_deviceOpen && !m_bCancel)
411 continue;
412 else
413 return count;
414 }
415 offset += count;
416 if (offset == m_inputReportSize)
417 break;
418 }
419 }
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800420 break;
Andrew Duggan4e811252014-04-03 15:17:57 -0700421 }
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800422
423 if (reportId)
424 *reportId = m_inputReport[HID_RMI4_REPORT_ID];
425
426 if (m_inputReport[HID_RMI4_REPORT_ID] == RMI_ATTN_REPORT_ID) {
427 memcpy(m_attnData, m_inputReport, count);
428 } else if (m_inputReport[HID_RMI4_REPORT_ID] == RMI_READ_DATA_REPORT_ID) {
429 memcpy(m_readData, m_inputReport, count);
430 m_dataBytesRead = count;
431 }
432 return 1;
Andrew Duggan4e811252014-04-03 15:17:57 -0700433}
434
435void HIDDevice::PrintReport(const unsigned char *report)
436{
437 int i;
438 int len = 0;
439 const unsigned char * data;
440 int addr = 0;
441
442 switch (report[HID_RMI4_REPORT_ID]) {
443 case RMI_WRITE_REPORT_ID:
444 len = report[HID_RMI4_WRITE_OUTPUT_COUNT];
445 data = &report[HID_RMI4_WRITE_OUTPUT_DATA];
446 addr = (report[HID_RMI4_WRITE_OUTPUT_ADDR] & 0xFF)
447 | ((report[HID_RMI4_WRITE_OUTPUT_ADDR + 1] & 0xFF) << 8);
448 fprintf(stdout, "Write Report:\n");
449 fprintf(stdout, "Address = 0x%02X\n", addr);
450 fprintf(stdout, "Length = 0x%02X\n", len);
451 break;
452 case RMI_READ_ADDR_REPORT_ID:
453 addr = (report[HID_RMI4_READ_OUTPUT_ADDR] & 0xFF)
454 | ((report[HID_RMI4_READ_OUTPUT_ADDR + 1] & 0xFF) << 8);
455 len = (report[HID_RMI4_READ_OUTPUT_COUNT] & 0xFF)
456 | ((report[HID_RMI4_READ_OUTPUT_COUNT + 1] & 0xFF) << 8);
457 fprintf(stdout, "Read Request (Output Report):\n");
458 fprintf(stdout, "Address = 0x%02X\n", addr);
459 fprintf(stdout, "Length = 0x%02X\n", len);
460 return;
461 break;
462 case RMI_READ_DATA_REPORT_ID:
463 len = report[HID_RMI4_READ_INPUT_COUNT];
464 data = &report[HID_RMI4_READ_INPUT_DATA];
465 fprintf(stdout, "Read Data Report:\n");
466 fprintf(stdout, "Length = 0x%02X\n", len);
467 break;
468 case RMI_ATTN_REPORT_ID:
469 fprintf(stdout, "Attention Report:\n");
470 len = 28;
471 data = &report[HID_RMI4_ATTN_DATA];
472 fprintf(stdout, "Interrupt Sources: 0x%02X\n",
473 report[HID_RMI4_ATTN_INTERUPT_SOURCES]);
474 break;
475 default:
476 fprintf(stderr, "Unknown Report: ID 0x%02x\n", report[HID_RMI4_REPORT_ID]);
477 return;
478 }
479
480 fprintf(stdout, "Data:\n");
481 for (i = 0; i < len; ++i) {
482 fprintf(stdout, "0x%02X ", data[i]);
483 if (i % 8 == 7) {
484 fprintf(stdout, "\n");
485 }
486 }
487 fprintf(stdout, "\n\n");
Andrew Duggan64d6e592014-07-01 13:02:10 -0700488}
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700489
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700490// Print protocol specific device information
491void HIDDevice::PrintDeviceInfo()
492{
493 fprintf(stdout, "HID device info:\nBus: %s Vendor: 0x%04x Product: 0x%04x\n",
494 m_info.bustype == BUS_I2C ? "I2C" : "USB", m_info.vendor, m_info.product);
495 fprintf(stdout, "Report sizes: input: %ld output: %ld\n", m_inputReportSize, m_outputReportSize);
496}
497
Andrew Dugganbef9c2d2015-05-06 17:45:48 -0700498bool WriteDeviceNameToFile(const char * file, const char * str)
499{
500 int fd;
501 ssize_t size;
502
503 fd = open(file, O_WRONLY);
504 if (fd < 0)
505 return false;
506
507 for (;;) {
508 size = write(fd, str, 19);
509 if (size < 0) {
510 if (errno == EINTR)
511 continue;
512
513 return false;
514 }
515 break;
516 }
517 close(fd);
518
519 return true;
520}
521
522void HIDDevice::RebindDriver()
523{
524 int bus = m_info.bustype;
525 int vendor = m_info.vendor;
526 int product = m_info.product;
527 std::string hidDeviceName;
528 std::string transportDeviceName;
529 std::string driverPath;
530 std::string bindFile;
531 std::string unbindFile;
532 std::string hidrawFile;
533
534 Close();
535
536 if (!LookupHidDeviceName(bus, vendor, product, hidDeviceName)) {
537 fprintf(stderr, "Failed to find HID device name for the specified device: bus (0x%x) vendor: (0x%x) product: (0x%x)\n",
538 bus, vendor, product);
539 return;
540 }
541
542 if (!FindTransportDriver(bus, hidDeviceName, transportDeviceName, driverPath)) {
543 fprintf(stderr, "Failed to find the transport device / driver for %s\n", hidDeviceName.c_str());
544 return;
545 }
546
547 bindFile = driverPath + "bind";
548 unbindFile = driverPath + "unbind";
549
550 if (!WriteDeviceNameToFile(unbindFile.c_str(), transportDeviceName.c_str())) {
551 fprintf(stderr, "Failed to unbind HID device %s: %s\n",
552 transportDeviceName.c_str(), strerror(errno));
553 return;
554 }
555
556 if (!WriteDeviceNameToFile(bindFile.c_str(), transportDeviceName.c_str())) {
557 fprintf(stderr, "Failed to bind HID device %s: %s\n",
558 transportDeviceName.c_str(), strerror(errno));
559 return;
560 }
561
562 // The hid device id has changed this is now a new hid device so we have to look up the new name
563 if (!LookupHidDeviceName(bus, vendor, product, hidDeviceName)) {
564 fprintf(stderr, "Failed to find HID device name for the specified device: bus (0x%x) vendor: (0x%x) product: (0x%x)\n",
565 bus, vendor, product);
566 return;
567 }
568
569 if (!FindHidRawFile(hidDeviceName, hidrawFile)) {
570 fprintf(stderr, "Failed to find the hidraw device file for %s\n", hidDeviceName.c_str());
571 return;
572 }
573
574 Open(hidrawFile.c_str());
575}
576
577bool HIDDevice::FindTransportDriver(int bus, std::string & hidDeviceName,
578 std::string & transportDeviceName, std::string & driverPath)
579{
580 std::string devicePrefix = "/sys/bus/";
581 std::string devicePath;
582 struct dirent * devicesDirEntry;
583 DIR * devicesDir;
584 struct dirent * devDirEntry;
585 DIR * devDir;
586 bool deviceFound = false;
587 ssize_t sz;
588
589 if (bus == BUS_I2C) {
590 devicePrefix += "i2c/";
591 driverPath = devicePrefix + "drivers/i2c_hid/";
592 } else {
593 devicePrefix += "usb/";
594 driverPath = devicePrefix + "drivers/usbhid/";
595 }
596 devicePath = devicePrefix + "devices/";
597
598 devicesDir = opendir(devicePath.c_str());
599 if (!devicesDir)
600 return false;
601
602 while((devicesDirEntry = readdir(devicesDir)) != NULL) {
603 if (devicesDirEntry->d_type != DT_LNK)
604 continue;
605
606 char buf[PATH_MAX];
607
608 sz = readlinkat(dirfd(devicesDir), devicesDirEntry->d_name, buf, PATH_MAX);
609 if (sz < 0)
610 continue;
611
612 buf[sz] = 0;
613
614 std::string fullLinkPath = devicePath + buf;
615 devDir = opendir(fullLinkPath.c_str());
616 if (!devDir) {
617 fprintf(stdout, "opendir failed\n");
618 continue;
619 }
620
621 while ((devDirEntry = readdir(devDir)) != NULL) {
622 if (!strcmp(devDirEntry->d_name, hidDeviceName.c_str())) {
623 transportDeviceName = devicesDirEntry->d_name;
624 deviceFound = true;
625 break;
626 }
627 }
628 closedir(devDir);
629
630 if (deviceFound)
631 break;
632 }
633 closedir(devicesDir);
634
635 return deviceFound;
636}
637
638bool HIDDevice::LookupHidDeviceName(int bus, int vendorId, int productId, std::string & deviceName)
639{
640 bool ret = false;
641 struct dirent * devDirEntry;
642 DIR * devDir;
643 char devicePrefix[15];
644
645 snprintf(devicePrefix, 15, "%04X:%04X:%04X", bus, vendorId, productId);
646
647 devDir = opendir("/sys/bus/hid/devices");
648 if (!devDir)
649 return false;
650
651 while ((devDirEntry = readdir(devDir)) != NULL) {
652 if (!strncmp(devDirEntry->d_name, devicePrefix, 14)) {
653 deviceName = devDirEntry->d_name;
654 ret = true;
655 break;
656 }
657 }
658 closedir(devDir);
659
660 return ret;
661}
662
663bool HIDDevice::FindHidRawFile(std::string & deviceName, std::string & hidrawFile)
664{
665 bool ret = false;
666 char hidrawDir[PATH_MAX];
667 struct dirent * devDirEntry;
668 DIR * devDir;
669
670 snprintf(hidrawDir, PATH_MAX, "/sys/bus/hid/devices/%s/hidraw", deviceName.c_str());
671
672 devDir = opendir(hidrawDir);
673 if (!devDir)
674 return false;
675
676 while ((devDirEntry = readdir(devDir)) != NULL) {
677 if (!strncmp(devDirEntry->d_name, "hidraw", 6)) {
678 hidrawFile = std::string("/dev/") + devDirEntry->d_name;
679 ret = true;
680 break;
681 }
682 }
683 closedir(devDir);
684
685 return ret;
686}