1 /*******************************************************
2 HIDAPI - Multi-Platform library for
3 communication with HID devices.
9 Linux Version - 6/2/2009
11 Copyright 2009, All Rights Reserved.
13 At the discretion of the user of this library,
14 this software may be licensed under the terms of the
15 GNU General Public License v3, a BSD-Style license, or the
16 original HIDAPI license as outlined in the LICENSE.txt,
17 LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
18 files located at the root of the source distribution.
19 These files may also be found in the public source
20 code repository located at:
21 http://github.com/signal11/hidapi .
22 ********************************************************/
24 #define _POSIX_C_SOURCE 200809L
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/utsname.h>
43 #include <linux/hidraw.h>
44 #include <linux/version.h>
45 #include <linux/input.h>
50 /* Definitions from linux/hidraw.h. Since these are new, some distros
51 may not have header files which contain them. */
52 #ifndef HIDIOCSFEATURE
53 #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
55 #ifndef HIDIOCGFEATURE
56 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
60 /* USB HID device property names */
61 const char *device_string_names[] = {
67 /* Symbolic names for the properties above */
68 enum device_string_id {
69 DEVICE_STRING_MANUFACTURER,
70 DEVICE_STRING_PRODUCT,
79 int uses_numbered_reports;
83 static __u32 kernel_version = 0;
85 static __u32 detect_kernel_version(void)
88 int major, minor, release;
92 ret = sscanf(name.release, "%d.%d.%d", &major, &minor, &release);
94 return KERNEL_VERSION(major, minor, release);
97 ret = sscanf(name.release, "%d.%d", &major, &minor);
99 return KERNEL_VERSION(major, minor, 0);
102 printf("Couldn't determine kernel version from version string \"%s\"\n", name.release);
106 static hid_device *new_hid_device(void)
108 hid_device *dev = calloc(1, sizeof(hid_device));
109 dev->device_handle = -1;
111 dev->uses_numbered_reports = 0;
117 /* The caller must free the returned string with free(). */
118 static wchar_t *utf8_to_wchar_t(const char *utf8)
123 size_t wlen = mbstowcs(NULL, utf8, 0);
124 if ((size_t) -1 == wlen) {
127 ret = calloc(wlen+1, sizeof(wchar_t));
128 mbstowcs(ret, utf8, wlen+1);
135 /* Get an attribute value from a udev_device and return it as a whar_t
136 string. The returned string must be freed with free() when done.*/
137 static wchar_t *copy_udev_string(struct udev_device *dev, const char *udev_name)
139 return utf8_to_wchar_t(udev_device_get_sysattr_value(dev, udev_name));
142 /* uses_numbered_reports() returns 1 if report_descriptor describes a device
143 which contains numbered reports. */
144 static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
147 int data_len, key_size;
150 int key = report_descriptor[i];
152 /* Check for the Report ID key */
153 if (key == 0x85/*Report ID*/) {
154 /* This device has a Report ID, which means it uses
159 //printf("key: %02hhx\n", key);
161 if ((key & 0xf0) == 0xf0) {
162 /* This is a Long Item. The next byte contains the
163 length of the data section (value) for this key.
164 See the HID specification, version 1.11, section
165 6.2.2.3, titled "Long Items." */
167 data_len = report_descriptor[i+1];
169 data_len = 0; /* malformed report */
173 /* This is a Short Item. The bottom two bits of the
174 key contain the size code for the data section
175 (value) for this key. Refer to the HID
176 specification, version 1.11, section 6.2.2.2,
177 titled "Short Items." */
178 size_code = key & 0x3;
183 data_len = size_code;
189 /* Can't ever happen since size_code is & 0x3 */
196 /* Skip over this key and it's associated data */
197 i += data_len + key_size;
200 /* Didn't find a Report ID key. Device doesn't use numbered reports. */
205 * The caller is responsible for free()ing the (newly-allocated) character
206 * strings pointed to by serial_number_utf8 and product_name_utf8 after use.
209 parse_uevent_info(const char *uevent, int *bus_type,
210 unsigned short *vendor_id, unsigned short *product_id,
211 char **serial_number_utf8, char **product_name_utf8)
213 char *tmp = strdup(uevent);
214 char *saveptr = NULL;
220 int found_serial = 0;
223 line = strtok_r(tmp, "\n", &saveptr);
224 while (line != NULL) {
225 /* line: "KEY=value" */
227 value = strchr(line, '=');
234 if (strcmp(key, "HID_ID") == 0) {
236 * type vendor product
237 * HID_ID=0003:000005AC:00008242
239 int ret = sscanf(value, "%x:%hx:%hx", bus_type, vendor_id, product_id);
243 } else if (strcmp(key, "HID_NAME") == 0) {
244 /* The caller has to free the product name */
245 *product_name_utf8 = strdup(value);
247 } else if (strcmp(key, "HID_UNIQ") == 0) {
248 /* The caller has to free the serial number */
249 *serial_number_utf8 = strdup(value);
254 line = strtok_r(NULL, "\n", &saveptr);
258 return (found_id && found_name && found_serial);
262 static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t *string, size_t maxlen)
265 struct udev_device *udev_dev, *parent, *hid_dev;
268 char *serial_number_utf8 = NULL;
269 char *product_name_utf8 = NULL;
271 /* Create the udev object */
274 printf("Can't create udev\n");
278 /* Get the dev_t (major/minor numbers) from the file handle. */
279 ret = fstat(dev->device_handle, &s);
282 /* Open a udev device from the dev_t. 'c' means character device. */
283 udev_dev = udev_device_new_from_devnum(udev, 'c', s.st_rdev);
285 hid_dev = udev_device_get_parent_with_subsystem_devtype(
290 unsigned short dev_vid;
291 unsigned short dev_pid;
295 ret = parse_uevent_info(
296 udev_device_get_sysattr_value(hid_dev, "uevent"),
303 if (bus_type == BUS_BLUETOOTH) {
305 case DEVICE_STRING_MANUFACTURER:
306 wcsncpy(string, L"", maxlen);
309 case DEVICE_STRING_PRODUCT:
310 retm = mbstowcs(string, product_name_utf8, maxlen);
311 ret = (retm == (size_t)-1)? -1: 0;
313 case DEVICE_STRING_SERIAL:
314 retm = mbstowcs(string, serial_number_utf8, maxlen);
315 ret = (retm == (size_t)-1)? -1: 0;
317 case DEVICE_STRING_COUNT:
324 /* This is a USB device. Find its parent USB Device node. */
325 parent = udev_device_get_parent_with_subsystem_devtype(
331 const char *key_str = NULL;
333 if (key >= 0 && key < DEVICE_STRING_COUNT) {
334 key_str = device_string_names[key];
340 str = udev_device_get_sysattr_value(parent, key_str);
342 /* Convert the string from UTF-8 to wchar_t */
343 retm = mbstowcs(string, str, maxlen);
344 ret = (retm == (size_t)-1)? -1: 0;
353 free(serial_number_utf8);
354 free(product_name_utf8);
356 udev_device_unref(udev_dev);
357 /* parent and hid_dev don't need to be (and can't be) unref'd.
358 I'm not sure why, but they'll throw double-free() errors. */
364 int HID_API_EXPORT hid_init(void)
368 /* Set the locale if it's not set. */
369 locale = setlocale(LC_CTYPE, NULL);
371 setlocale(LC_CTYPE, "");
373 kernel_version = detect_kernel_version();
378 int HID_API_EXPORT hid_exit(void)
380 /* Nothing to do for this in the Linux/hidraw implementation. */
385 struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
388 struct udev_enumerate *enumerate;
389 struct udev_list_entry *devices, *dev_list_entry;
391 struct hid_device_info *root = NULL; /* return object */
392 struct hid_device_info *cur_dev = NULL;
393 struct hid_device_info *prev_dev = NULL; /* previous device */
397 /* Create the udev object */
400 printf("Can't create udev\n");
404 /* Create a list of the devices in the 'hidraw' subsystem. */
405 enumerate = udev_enumerate_new(udev);
406 udev_enumerate_add_match_subsystem(enumerate, "hidraw");
407 udev_enumerate_scan_devices(enumerate);
408 devices = udev_enumerate_get_list_entry(enumerate);
409 /* For each item, see if it matches the vid/pid, and if so
410 create a udev_device record for it */
411 udev_list_entry_foreach(dev_list_entry, devices) {
412 const char *sysfs_path;
413 const char *dev_path;
415 struct udev_device *raw_dev; /* The device's hidraw udev node. */
416 struct udev_device *hid_dev; /* The device's HID udev node. */
417 struct udev_device *usb_dev; /* The device's USB udev node. */
418 struct udev_device *intf_dev; /* The device's interface (in the USB sense). */
419 unsigned short dev_vid;
420 unsigned short dev_pid;
421 char *serial_number_utf8 = NULL;
422 char *product_name_utf8 = NULL;
426 /* Get the filename of the /sys entry for the device
427 and create a udev_device object (dev) representing it */
428 sysfs_path = udev_list_entry_get_name(dev_list_entry);
429 raw_dev = udev_device_new_from_syspath(udev, sysfs_path);
430 dev_path = udev_device_get_devnode(raw_dev);
432 hid_dev = udev_device_get_parent_with_subsystem_devtype(
438 /* Unable to find parent hid device. */
442 result = parse_uevent_info(
443 udev_device_get_sysattr_value(hid_dev, "uevent"),
451 /* parse_uevent_info() failed for at least one field. */
455 if (bus_type != BUS_USB && bus_type != BUS_BLUETOOTH) {
456 /* We only know how to handle USB and BT devices. */
460 /* Check the VID/PID against the arguments */
461 if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
462 (product_id == 0x0 || product_id == dev_pid)) {
463 struct hid_device_info *tmp;
465 /* VID/PID match. Create the record. */
466 tmp = malloc(sizeof(struct hid_device_info));
476 /* Fill out the record */
477 cur_dev->next = NULL;
478 cur_dev->path = dev_path? strdup(dev_path): NULL;
481 cur_dev->vendor_id = dev_vid;
482 cur_dev->product_id = dev_pid;
485 cur_dev->serial_number = utf8_to_wchar_t(serial_number_utf8);
488 cur_dev->release_number = 0x0;
490 /* Interface Number */
491 cur_dev->interface_number = -1;
495 /* The device pointed to by raw_dev contains information about
496 the hidraw device. In order to get information about the
497 USB device, get the parent device with the
498 subsystem/devtype pair of "usb"/"usb_device". This will
499 be several levels up the tree, but the function will find
501 usb_dev = udev_device_get_parent_with_subsystem_devtype(
507 /* Free this device */
508 free(cur_dev->serial_number);
512 /* Take it off the device list. */
514 prev_dev->next = NULL;
518 cur_dev = root = NULL;
524 /* Manufacturer and Product strings */
525 cur_dev->manufacturer_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_MANUFACTURER]);
526 cur_dev->product_string = copy_udev_string(usb_dev, device_string_names[DEVICE_STRING_PRODUCT]);
529 str = udev_device_get_sysattr_value(usb_dev, "bcdDevice");
530 cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
532 /* Get a handle to the interface's udev node. */
533 intf_dev = udev_device_get_parent_with_subsystem_devtype(
538 str = udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
539 cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
545 /* Manufacturer and Product strings */
546 cur_dev->manufacturer_string = wcsdup(L"");
547 cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
552 /* Unknown device type - this should never happen, as we
553 * check for USB and Bluetooth devices above */
559 free(serial_number_utf8);
560 free(product_name_utf8);
561 udev_device_unref(raw_dev);
562 /* hid_dev, usb_dev and intf_dev don't need to be (and can't be)
563 unref()d. It will cause a double-free() error. I'm not
566 /* Free the enumerator and udev objects. */
567 udev_enumerate_unref(enumerate);
573 void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
575 struct hid_device_info *d = devs;
577 struct hid_device_info *next = d->next;
579 free(d->serial_number);
580 free(d->manufacturer_string);
581 free(d->product_string);
587 hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
589 struct hid_device_info *devs, *cur_dev;
590 const char *path_to_open = NULL;
591 hid_device *handle = NULL;
593 devs = hid_enumerate(vendor_id, product_id);
596 if (cur_dev->vendor_id == vendor_id &&
597 cur_dev->product_id == product_id) {
599 if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
600 path_to_open = cur_dev->path;
605 path_to_open = cur_dev->path;
609 cur_dev = cur_dev->next;
613 /* Open the device */
614 handle = hid_open_path(path_to_open);
617 hid_free_enumeration(devs);
622 hid_device * HID_API_EXPORT hid_open_path(const char *path)
624 hid_device *dev = NULL;
628 dev = new_hid_device();
631 dev->device_handle = open(path, O_RDWR);
633 /* If we have a good handle, return it. */
634 if (dev->device_handle > 0) {
636 /* Get the report descriptor */
637 int res, desc_size = 0;
638 struct hidraw_report_descriptor rpt_desc;
640 memset(&rpt_desc, 0x0, sizeof(rpt_desc));
642 /* Get Report Descriptor Size */
643 res = ioctl(dev->device_handle, HIDIOCGRDESCSIZE, &desc_size);
645 perror("HIDIOCGRDESCSIZE");
648 /* Get Report Descriptor */
649 rpt_desc.size = desc_size;
650 res = ioctl(dev->device_handle, HIDIOCGRDESC, &rpt_desc);
652 perror("HIDIOCGRDESC");
654 /* Determine if this device uses numbered reports. */
655 dev->uses_numbered_reports =
656 uses_numbered_reports(rpt_desc.value,
663 /* Unable to open any devices. */
670 int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
674 bytes_written = write(dev->device_handle, data, length);
676 return bytes_written;
680 int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
684 if (milliseconds >= 0) {
685 /* Milliseconds is either 0 (non-blocking) or > 0 (contains
686 a valid timeout). In both cases we want to call poll()
687 and wait for data to arrive. Don't rely on non-blocking
688 operation (O_NONBLOCK) since some kernels don't seem to
689 properly report device disconnection through read() when
690 in non-blocking mode. */
694 fds.fd = dev->device_handle;
697 ret = poll(&fds, 1, milliseconds);
698 if (ret == -1 || ret == 0) {
699 /* Error or timeout */
703 /* Check for errors on the file descriptor. This will
704 indicate a device disconnection. */
705 if (fds.revents & (POLLERR | POLLHUP | POLLNVAL))
710 bytes_read = read(dev->device_handle, data, length);
711 if (bytes_read < 0 && (errno == EAGAIN || errno == EINPROGRESS))
714 if (bytes_read >= 0 &&
715 kernel_version != 0 &&
716 kernel_version < KERNEL_VERSION(2,6,34) &&
717 dev->uses_numbered_reports) {
718 /* Work around a kernel bug. Chop off the first byte. */
719 memmove(data, data+1, bytes_read);
726 int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
728 return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
731 int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
733 /* Do all non-blocking in userspace using poll(), since it looks
734 like there's a bug in the kernel in some versions where
735 read() will not return -1 on disconnection of the USB device */
737 dev->blocking = !nonblock;
738 return 0; /* Success */
742 int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
746 res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data);
748 perror("ioctl (SFEATURE)");
753 int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
757 res = ioctl(dev->device_handle, HIDIOCGFEATURE(length), data);
759 perror("ioctl (GFEATURE)");
766 void HID_API_EXPORT hid_close(hid_device *dev)
770 close(dev->device_handle);
775 int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
777 return get_device_string(dev, DEVICE_STRING_MANUFACTURER, string, maxlen);
780 int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
782 return get_device_string(dev, DEVICE_STRING_PRODUCT, string, maxlen);
785 int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
787 return get_device_string(dev, DEVICE_STRING_SERIAL, string, maxlen);
790 int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
796 HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)