SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
hid-libusb.c
Go to the documentation of this file.
1 /*******************************************************
2  HIDAPI - Multi-Platform library for
3  communication with HID devices.
4 
5  Alan Ott
6  Signal 11 Software
7 
8  8/22/2009
9  Linux Version - 6/2/2010
10  Libusb Version - 8/13/2010
11  FreeBSD Version - 11/1/2011
12 
13  Copyright 2009, All Rights Reserved.
14 
15  At the discretion of the user of this library,
16  this software may be licensed under the terms of the
17  GNU General Public License v3, a BSD-Style license, or the
18  original HIDAPI license as outlined in the LICENSE.txt,
19  LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
20  files located at the root of the source distribution.
21  These files may also be found in the public source
22  code repository located at:
23  http://github.com/signal11/hidapi .
24 ********************************************************/
25 
26 #define _GNU_SOURCE /* needed for wcsdup() before glibc 2.10 */
27 
28 /* C */
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <locale.h>
34 #include <errno.h>
35 
36 /* Unix */
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #ifndef MINGW32
41 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
43 #endif
44 #include <fcntl.h>
45 #include <pthread.h>
46 #include <wchar.h>
47 
48 /* GNU / LibUSB */
49 #include <libusb.h>
50 #include "iconv.h"
51 
52 #include "../fcdhid/hidapi.h"
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #ifdef DEBUG_PRINTF
59 #define LOG(...) fprintf(stderr, __VA_ARGS__)
60 #else
61 #define LOG(...) do {} while (0)
62 #endif
63 
64 #ifndef __FreeBSD__
65 #define DETACH_KERNEL_DRIVER
66 #endif
67 
71 #ifdef __APPLE__
72 #include "apple_compat.h"
73 #endif
74 
75 /* Uncomment to enable the retrieval of Usage and Usage Page in
76 hid_enumerate(). Warning, on platforms different from FreeBSD
77 this is very invasive as it requires the detach
78 and re-attach of the kernel driver. See comments inside hid_enumerate().
79 libusb HIDAPI programs are encouraged to use the interface number
80 instead to differentiate between interfaces on a composite HID device. */
81 /*#define INVASIVE_GET_USAGE*/
82 
83 /* Linked List of input reports received from the device. */
84 struct input_report {
86  size_t len;
87  struct input_report *next;
88 };
89 
90 
91 struct hid_device_ {
92  /* Handle to the actual device. */
93  libusb_device_handle *device_handle;
94 
95  /* Endpoint information */
99 
100  /* The interface number of the HID */
102 
103  /* Indexes of Strings */
107 
108  /* Whether blocking reads are used */
109  int blocking; /* boolean */
110 
111  /* Read thread objects */
112  pthread_t thread;
113  pthread_mutex_t mutex; /* Protects input_reports */
114  pthread_cond_t condition;
115  pthread_barrier_t barrier; /* Ensures correct startup sequence */
118  struct libusb_transfer *transfer;
119 
120  /* List of received input reports. */
122 };
123 
124 static libusb_context *usb_context = NULL;
125 
127 static int return_data(hid_device *dev, unsigned char *data, size_t length);
128 
129 static hid_device *new_hid_device(void)
130 {
131  hid_device *dev = calloc(1, sizeof(hid_device));
132  dev->blocking = 1;
133 
134  pthread_mutex_init(&dev->mutex, NULL);
135  pthread_cond_init(&dev->condition, NULL);
136  pthread_barrier_init(&dev->barrier, NULL, 2);
137 
138  return dev;
139 }
140 
141 static void free_hid_device(hid_device *dev)
142 {
143  /* Clean up the thread objects */
144  pthread_barrier_destroy(&dev->barrier);
145  pthread_cond_destroy(&dev->condition);
146  pthread_mutex_destroy(&dev->mutex);
147 
148  /* Free the device itself */
149  free(dev);
150 }
151 
152 #if 0
153 /*TODO: Implement this funciton on hidapi/libusb.. */
154 static void register_error(hid_device *device, const char *op)
155 {
156 
157 }
158 #endif
159 
160 #ifdef INVASIVE_GET_USAGE
161 /* Get bytes from a HID Report Descriptor.
162  Only call with a num_bytes of 0, 1, 2, or 4. */
163 static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur)
164 {
165  /* Return if there aren't enough bytes. */
166  if (cur + num_bytes >= len)
167  return 0;
168 
169  if (num_bytes == 0)
170  return 0;
171  else if (num_bytes == 1) {
172  return rpt[cur+1];
173  }
174  else if (num_bytes == 2) {
175  return (rpt[cur+2] * 256 + rpt[cur+1]);
176  }
177  else if (num_bytes == 4) {
178  return (rpt[cur+4] * 0x01000000 +
179  rpt[cur+3] * 0x00010000 +
180  rpt[cur+2] * 0x00000100 +
181  rpt[cur+1] * 0x00000001);
182  }
183  else
184  return 0;
185 }
186 
187 /* Retrieves the device's Usage Page and Usage from the report
188  descriptor. The algorithm is simple, as it just returns the first
189  Usage and Usage Page that it finds in the descriptor.
190  The return value is 0 on success and -1 on failure. */
191 static int get_usage(uint8_t *report_descriptor, size_t size,
192  unsigned short *usage_page, unsigned short *usage)
193 {
194  unsigned int i = 0;
195  int size_code;
196  int data_len, key_size;
197  int usage_found = 0, usage_page_found = 0;
198 
199  while (i < size) {
200  int key = report_descriptor[i];
201  int key_cmd = key & 0xfc;
202 
203  //printf("key: %02hhx\n", key);
204 
205  if ((key & 0xf0) == 0xf0) {
206  /* This is a Long Item. The next byte contains the
207  length of the data section (value) for this key.
208  See the HID specification, version 1.11, section
209  6.2.2.3, titled "Long Items." */
210  if (i+1 < size)
211  data_len = report_descriptor[i+1];
212  else
213  data_len = 0; /* malformed report */
214  key_size = 3;
215  }
216  else {
217  /* This is a Short Item. The bottom two bits of the
218  key contain the size code for the data section
219  (value) for this key. Refer to the HID
220  specification, version 1.11, section 6.2.2.2,
221  titled "Short Items." */
222  size_code = key & 0x3;
223  switch (size_code) {
224  case 0:
225  case 1:
226  case 2:
227  data_len = size_code;
228  break;
229  case 3:
230  data_len = 4;
231  break;
232  default:
233  /* Can't ever happen since size_code is & 0x3 */
234  data_len = 0;
235  break;
236  };
237  key_size = 1;
238  }
239 
240  if (key_cmd == 0x4) {
241  *usage_page = get_bytes(report_descriptor, size, data_len, i);
242  usage_page_found = 1;
243  //printf("Usage Page: %x\n", (uint32_t)*usage_page);
244  }
245  if (key_cmd == 0x8) {
246  *usage = get_bytes(report_descriptor, size, data_len, i);
247  usage_found = 1;
248  //printf("Usage: %x\n", (uint32_t)*usage);
249  }
250 
251  if (usage_page_found && usage_found)
252  return 0; /* success */
253 
254  /* Skip over this key and it's associated data */
255  i += data_len + key_size;
256  }
257 
258  return -1; /* failure */
259 }
260 #endif /* INVASIVE_GET_USAGE */
261 
262 #ifdef __FreeBSD__
263 /* The FreeBSD version of libusb doesn't have this funciton. In mainline
264  libusb, it's inlined in libusb.h. This function will bear a striking
265  resemblence to that one, because there's about one way to code it.
266 
267  Note that the data parameter is Unicode in UTF-16LE encoding.
268  Return value is the number of bytes in data, or LIBUSB_ERROR_*.
269  */
270 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
271  uint8_t descriptor_index, uint16_t lang_id,
272  unsigned char *data, int length)
273 {
274  return libusb_control_transfer(dev,
275  LIBUSB_ENDPOINT_IN | 0x0, /* Endpoint 0 IN */
276  LIBUSB_REQUEST_GET_DESCRIPTOR,
277  (LIBUSB_DT_STRING << 8) | descriptor_index,
278  lang_id, data, (uint16_t) length, 1000);
279 }
280 
281 #endif
282 
283 
284 /* Get the first language the device says it reports. This comes from
285  USB string #0. */
286 static uint16_t get_first_language(libusb_device_handle *dev)
287 {
288  uint16_t buf[32];
289  int len;
290 
291  /* Get the string from libusb. */
292  len = libusb_get_string_descriptor(dev,
293  0x0, /* String ID */
294  0x0, /* Language */
295  (unsigned char*)buf,
296  sizeof(buf));
297  if (len < 4)
298  return 0x0;
299 
300  return buf[1]; /* First two bytes are len and descriptor type. */
301 }
302 
303 static int is_language_supported(libusb_device_handle *dev, uint16_t lang)
304 {
305  uint16_t buf[32];
306  int len;
307  int i;
308 
309  /* Get the string from libusb. */
310  len = libusb_get_string_descriptor(dev,
311  0x0, /* String ID */
312  0x0, /* Language */
313  (unsigned char*)buf,
314  sizeof(buf));
315  if (len < 4)
316  return 0x0;
317 
318 
319  len /= 2; /* language IDs are two-bytes each. */
320  /* Start at index 1 because there are two bytes of protocol data. */
321  for (i = 1; i < len; i++) {
322  if (buf[i] == lang)
323  return 1;
324  }
325 
326  return 0;
327 }
328 
329 
330 /* This function returns a newly allocated wide string containing the USB
331  device string numbered by the index. The returned string must be freed
332  by using free(). */
333 static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
334 {
335  char buf[512];
336  int len;
337  wchar_t *str = NULL;
338  wchar_t wbuf[256];
339 
340  /* iconv variables */
341  iconv_t ic;
342  size_t inbytes;
343  size_t outbytes;
344  size_t res;
345 #ifdef __FreeBSD__
346  const char *inptr;
347 #else
348  char *inptr;
349 #endif
350  char *outptr;
351 
352  /* Determine which language to use. */
353  uint16_t lang;
355  if (!is_language_supported(dev, lang))
356  lang = get_first_language(dev);
357 
358  /* Get the string from libusb. */
359  len = libusb_get_string_descriptor(dev,
360  idx,
361  lang,
362  (unsigned char*)buf,
363  sizeof(buf));
364  if (len < 0)
365  return NULL;
366 
367  /* buf does not need to be explicitly NULL-terminated because
368  it is only passed into iconv() which does not need it. */
369 
370  /* Initialize iconv. */
371  ic = iconv_open("WCHAR_T", "UTF-16LE");
372  if (ic == (iconv_t)-1) {
373  LOG("iconv_open() failed\n");
374  return NULL;
375  }
376 
377  /* Convert to native wchar_t (UTF-32 on glibc/BSD systems).
378  Skip the first character (2-bytes). */
379  inptr = buf+2;
380  inbytes = len-2;
381  outptr = (char*) wbuf;
382  outbytes = sizeof(wbuf);
383  res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
384  if (res == (size_t)-1) {
385  LOG("iconv() failed\n");
386  goto err;
387  }
388 
389  /* Write the terminating NULL. */
390  wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1] = 0x00000000;
391  if (outbytes >= sizeof(wbuf[0]))
392  *((wchar_t*)outptr) = 0x00000000;
393 
394  /* Allocate and copy the string. */
395  str = wcsdup(wbuf);
396 
397 err:
398  iconv_close(ic);
399 
400  return str;
401 }
402 
403 static char *make_path(libusb_device *dev, int interface_number)
404 {
405  char str[64];
406  snprintf(str, sizeof(str), "%04x:%04x:%02x",
407  libusb_get_bus_number(dev),
408  libusb_get_device_address(dev),
409  interface_number);
410  str[sizeof(str)-1] = '\0';
411 
412  return strdup(str);
413 }
414 
415 
417 {
418  if (!usb_context) {
419  const char *locale;
420 
421  /* Init Libusb */
422  if (libusb_init(&usb_context))
423  return -1;
424 
425  /* Set the locale if it's not set. */
426  locale = setlocale(LC_CTYPE, NULL);
427  if (!locale)
428  setlocale(LC_CTYPE, "");
429  }
430 
431  return 0;
432 }
433 
435 {
436  if (usb_context) {
437  libusb_exit(usb_context);
438  usb_context = NULL;
439  }
440 
441  return 0;
442 }
443 
444 struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
445 {
446  libusb_device **devs;
447  libusb_device *dev;
448  libusb_device_handle *handle;
449  ssize_t num_devs;
450  int i = 0;
451 
452  struct hid_device_info *root = NULL; /* return object */
453  struct hid_device_info *cur_dev = NULL;
454 
455  if(hid_init() < 0)
456  return NULL;
457 
458  num_devs = libusb_get_device_list(usb_context, &devs);
459  if (num_devs < 0)
460  return NULL;
461  while ((dev = devs[i++]) != NULL) {
462  struct libusb_device_descriptor desc;
463  struct libusb_config_descriptor *conf_desc = NULL;
464  int j, k;
465  int interface_num = 0;
466 
467  int res = libusb_get_device_descriptor(dev, &desc);
468  unsigned short dev_vid = desc.idVendor;
469  unsigned short dev_pid = desc.idProduct;
470 
471  res = libusb_get_active_config_descriptor(dev, &conf_desc);
472  if (res < 0)
473  libusb_get_config_descriptor(dev, 0, &conf_desc);
474  if (conf_desc) {
475  for (j = 0; j < conf_desc->bNumInterfaces; j++) {
476  const struct libusb_interface *intf = &conf_desc->interface[j];
477  for (k = 0; k < intf->num_altsetting; k++) {
478  const struct libusb_interface_descriptor *intf_desc;
479  intf_desc = &intf->altsetting[k];
480  if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID) {
481  interface_num = intf_desc->bInterfaceNumber;
482 
483  /* Check the VID/PID against the arguments */
484  if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
485  (product_id == 0x0 || product_id == dev_pid)) {
486  struct hid_device_info *tmp;
487 
488  /* VID/PID match. Create the record. */
489  tmp = calloc(1, sizeof(struct hid_device_info));
490  if (cur_dev) {
491  cur_dev->next = tmp;
492  }
493  else {
494  root = tmp;
495  }
496  cur_dev = tmp;
497 
498  /* Fill out the record */
499  cur_dev->next = NULL;
500  cur_dev->path = make_path(dev, interface_num);
501 
502  res = libusb_open(dev, &handle);
503 
504  if (res >= 0) {
505  /* Serial Number */
506  if (desc.iSerialNumber > 0)
507  cur_dev->serial_number =
508  get_usb_string(handle, desc.iSerialNumber);
509 
510  /* Manufacturer and Product strings */
511  if (desc.iManufacturer > 0)
512  cur_dev->manufacturer_string =
513  get_usb_string(handle, desc.iManufacturer);
514  if (desc.iProduct > 0)
515  cur_dev->product_string =
516  get_usb_string(handle, desc.iProduct);
517 
518 #ifdef INVASIVE_GET_USAGE
519 {
520  /*
521  This section is removed because it is too
522  invasive on the system. Getting a Usage Page
523  and Usage requires parsing the HID Report
524  descriptor. Getting a HID Report descriptor
525  involves claiming the interface. Claiming the
526  interface involves detaching the kernel driver.
527  Detaching the kernel driver is hard on the system
528  because it will unclaim interfaces (if another
529  app has them claimed) and the re-attachment of
530  the driver will sometimes change /dev entry names.
531  It is for these reasons that this section is
532  #if 0. For composite devices, use the interface
533  field in the hid_device_info struct to distinguish
534  between interfaces. */
535  unsigned char data[256];
536 #ifdef DETACH_KERNEL_DRIVER
537  int detached = 0;
538  /* Usage Page and Usage */
539  res = libusb_kernel_driver_active(handle, interface_num);
540  if (res == 1) {
541  res = libusb_detach_kernel_driver(handle, interface_num);
542  if (res < 0)
543  LOG("Couldn't detach kernel driver, even though a kernel driver was attached.");
544  else
545  detached = 1;
546  }
547 #endif
548  res = libusb_claim_interface(handle, interface_num);
549  if (res >= 0) {
550  /* Get the HID Report Descriptor. */
551  res = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_INTERFACE, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_REPORT << 8)|interface_num, 0, data, sizeof(data), 5000);
552  if (res >= 0) {
553  unsigned short page=0, usage=0;
554  /* Parse the usage and usage page
555  out of the report descriptor. */
556  get_usage(data, res, &page, &usage);
557  cur_dev->usage_page = page;
558  cur_dev->usage = usage;
559  }
560  else
561  LOG("libusb_control_transfer() for getting the HID report failed with %d\n", res);
562 
563  /* Release the interface */
564  res = libusb_release_interface(handle, interface_num);
565  if (res < 0)
566  LOG("Can't release the interface.\n");
567  }
568  else
569  LOG("Can't claim interface %d\n", res);
570 #ifdef DETACH_KERNEL_DRIVER
571  /* Re-attach kernel driver if necessary. */
572  if (detached) {
573  res = libusb_attach_kernel_driver(handle, interface_num);
574  if (res < 0)
575  LOG("Couldn't re-attach kernel driver.\n");
576  }
577 #endif
578 }
579 #endif /* INVASIVE_GET_USAGE */
580 
581  libusb_close(handle);
582  }
583  /* VID/PID */
584  cur_dev->vendor_id = dev_vid;
585  cur_dev->product_id = dev_pid;
586 
587  /* Release Number */
588  cur_dev->release_number = desc.bcdDevice;
589 
590  /* Interface Number */
591  cur_dev->interface_number = interface_num;
592  }
593  }
594  } /* altsettings */
595  } /* interfaces */
596  libusb_free_config_descriptor(conf_desc);
597  }
598  }
599 
600  libusb_free_device_list(devs, 1);
601 
602  return root;
603 }
604 
606 {
607  struct hid_device_info *d = devs;
608  while (d) {
609  struct hid_device_info *next = d->next;
610  free(d->path);
611  free(d->serial_number);
612  free(d->manufacturer_string);
613  free(d->product_string);
614  free(d);
615  d = next;
616  }
617 }
618 
619 hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
620 {
621  struct hid_device_info *devs, *cur_dev;
622  const char *path_to_open = NULL;
623  hid_device *handle = NULL;
624 
625  devs = hid_enumerate(vendor_id, product_id);
626  cur_dev = devs;
627  while (cur_dev) {
628  if (cur_dev->vendor_id == vendor_id &&
629  cur_dev->product_id == product_id) {
630  if (serial_number) {
631  if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
632  path_to_open = cur_dev->path;
633  break;
634  }
635  }
636  else {
637  path_to_open = cur_dev->path;
638  break;
639  }
640  }
641  cur_dev = cur_dev->next;
642  }
643 
644  if (path_to_open) {
645  /* Open the device */
646  handle = hid_open_path(path_to_open);
647  }
648 
649  hid_free_enumeration(devs);
650 
651  return handle;
652 }
653 
654 static void read_callback(struct libusb_transfer *transfer)
655 {
656  hid_device *dev = transfer->user_data;
657  int res;
658 
659  if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
660 
661  struct input_report *rpt = malloc(sizeof(*rpt));
662  rpt->data = malloc(transfer->actual_length);
663  memcpy(rpt->data, transfer->buffer, transfer->actual_length);
664  rpt->len = transfer->actual_length;
665  rpt->next = NULL;
666 
667  pthread_mutex_lock(&dev->mutex);
668 
669  /* Attach the new report object to the end of the list. */
670  if (dev->input_reports == NULL) {
671  /* The list is empty. Put it at the root. */
672  dev->input_reports = rpt;
673  pthread_cond_signal(&dev->condition);
674  }
675  else {
676  /* Find the end of the list and attach. */
677  struct input_report *cur = dev->input_reports;
678  int num_queued = 0;
679  while (cur->next != NULL) {
680  cur = cur->next;
681  num_queued++;
682  }
683  cur->next = rpt;
684 
685  /* Pop one off if we've reached 30 in the queue. This
686  way we don't grow forever if the user never reads
687  anything from the device. */
688  if (num_queued > 30) {
689  return_data(dev, NULL, 0);
690  }
691  }
692  pthread_mutex_unlock(&dev->mutex);
693  }
694  else if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
695  dev->shutdown_thread = 1;
696  dev->cancelled = 1;
697  return;
698  }
699  else if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
700  dev->shutdown_thread = 1;
701  dev->cancelled = 1;
702  return;
703  }
704  else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
705  //LOG("Timeout (normal)\n");
706  }
707  else {
708  LOG("Unknown transfer code: %d\n", transfer->status);
709  }
710 
711  /* Re-submit the transfer object. */
712  res = libusb_submit_transfer(transfer);
713  if (res != 0) {
714  LOG("Unable to submit URB. libusb error code: %d\n", res);
715  dev->shutdown_thread = 1;
716  dev->cancelled = 1;
717  }
718 }
719 
720 
721 static void *read_thread(void *param)
722 {
723  hid_device *dev = param;
724  unsigned char *buf;
725  const size_t length = dev->input_ep_max_packet_size;
726 
727  /* Set up the transfer object. */
728  buf = malloc(length);
729  dev->transfer = libusb_alloc_transfer(0);
730  libusb_fill_interrupt_transfer(dev->transfer,
731  dev->device_handle,
732  dev->input_endpoint,
733  buf,
734  length,
735  read_callback,
736  dev,
737  5000/*timeout*/);
738 
739  /* Make the first submission. Further submissions are made
740  from inside read_callback() */
741  libusb_submit_transfer(dev->transfer);
742 
743  /* Notify the main thread that the read thread is up and running. */
744  pthread_barrier_wait(&dev->barrier);
745 
746  /* Handle all the events. */
747  while (!dev->shutdown_thread) {
748  int res;
749  res = libusb_handle_events(usb_context);
750  if (res < 0) {
751  /* There was an error. */
752  LOG("read_thread(): libusb reports error # %d\n", res);
753 
754  /* Break out of this loop only on fatal error.*/
755  if (res != LIBUSB_ERROR_BUSY &&
756  res != LIBUSB_ERROR_TIMEOUT &&
757  res != LIBUSB_ERROR_OVERFLOW &&
758  res != LIBUSB_ERROR_INTERRUPTED) {
759  break;
760  }
761  }
762  }
763 
764  /* Cancel any transfer that may be pending. This call will fail
765  if no transfers are pending, but that's OK. */
766  libusb_cancel_transfer(dev->transfer);
767 
768  while (!dev->cancelled)
769  libusb_handle_events_completed(usb_context, &dev->cancelled);
770 
771  /* Now that the read thread is stopping, Wake any threads which are
772  waiting on data (in hid_read_timeout()). Do this under a mutex to
773  make sure that a thread which is about to go to sleep waiting on
774  the condition acutally will go to sleep before the condition is
775  signaled. */
776  pthread_mutex_lock(&dev->mutex);
777  pthread_cond_broadcast(&dev->condition);
778  pthread_mutex_unlock(&dev->mutex);
779 
780  /* The dev->transfer->buffer and dev->transfer objects are cleaned up
781  in hid_close(). They are not cleaned up here because this thread
782  could end either due to a disconnect or due to a user
783  call to hid_close(). In both cases the objects can be safely
784  cleaned up after the call to pthread_join() (in hid_close()), but
785  since hid_close() calls libusb_cancel_transfer(), on these objects,
786  they can not be cleaned up here. */
787 
788  return NULL;
789 }
790 
791 
793 {
794  hid_device *dev = NULL;
795 
796  libusb_device **devs;
797  libusb_device *usb_dev;
798  int res;
799  int d = 0;
800  int good_open = 0;
801 
802  if(hid_init() < 0)
803  return NULL;
804 
805  dev = new_hid_device();
806 
807  libusb_get_device_list(usb_context, &devs);
808  while ((usb_dev = devs[d++]) != NULL) {
809  struct libusb_device_descriptor desc;
810  struct libusb_config_descriptor *conf_desc = NULL;
811  int i,j,k;
812  libusb_get_device_descriptor(usb_dev, &desc);
813 
814  if (libusb_get_active_config_descriptor(usb_dev, &conf_desc) < 0)
815  continue;
816  for (j = 0; j < conf_desc->bNumInterfaces; j++) {
817  const struct libusb_interface *intf = &conf_desc->interface[j];
818  for (k = 0; k < intf->num_altsetting; k++) {
819  const struct libusb_interface_descriptor *intf_desc;
820  intf_desc = &intf->altsetting[k];
821  if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID) {
822  char *dev_path = make_path(usb_dev, intf_desc->bInterfaceNumber);
823  if (!strcmp(dev_path, path)) {
824  /* Matched Paths. Open this device */
825 
826  /* OPEN HERE */
827  res = libusb_open(usb_dev, &dev->device_handle);
828  if (res < 0) {
829  LOG("can't open device\n");
830  free(dev_path);
831  break;
832  }
833  good_open = 1;
834 #ifdef DETACH_KERNEL_DRIVER
835  /* Detach the kernel driver, but only if the
836  device is managed by the kernel */
837  if (libusb_kernel_driver_active(dev->device_handle, intf_desc->bInterfaceNumber) == 1) {
838  res = libusb_detach_kernel_driver(dev->device_handle, intf_desc->bInterfaceNumber);
839  if (res < 0) {
840  libusb_close(dev->device_handle);
841  LOG("Unable to detach Kernel Driver\n");
842  free(dev_path);
843  good_open = 0;
844  break;
845  }
846  }
847 #endif
848  res = libusb_claim_interface(dev->device_handle, intf_desc->bInterfaceNumber);
849  if (res < 0) {
850  LOG("can't claim interface %d: %d\n", intf_desc->bInterfaceNumber, res);
851  free(dev_path);
852  libusb_close(dev->device_handle);
853  good_open = 0;
854  break;
855  }
856 
857  /* Store off the string descriptor indexes */
858  dev->manufacturer_index = desc.iManufacturer;
859  dev->product_index = desc.iProduct;
860  dev->serial_index = desc.iSerialNumber;
861 
862  /* Store off the interface number */
863  dev->interface = intf_desc->bInterfaceNumber;
864 
865  /* Find the INPUT and OUTPUT endpoints. An
866  OUTPUT endpoint is not required. */
867  for (i = 0; i < intf_desc->bNumEndpoints; i++) {
868  const struct libusb_endpoint_descriptor *ep
869  = &intf_desc->endpoint[i];
870 
871  /* Determine the type and direction of this
872  endpoint. */
873  int is_interrupt =
874  (ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK)
875  == LIBUSB_TRANSFER_TYPE_INTERRUPT;
876  int is_output =
877  (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
878  == LIBUSB_ENDPOINT_OUT;
879  int is_input =
880  (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
881  == LIBUSB_ENDPOINT_IN;
882 
883  /* Decide whether to use it for intput or output. */
884  if (dev->input_endpoint == 0 &&
885  is_interrupt && is_input) {
886  /* Use this endpoint for INPUT */
887  dev->input_endpoint = ep->bEndpointAddress;
888  dev->input_ep_max_packet_size = ep->wMaxPacketSize;
889  }
890  if (dev->output_endpoint == 0 &&
891  is_interrupt && is_output) {
892  /* Use this endpoint for OUTPUT */
893  dev->output_endpoint = ep->bEndpointAddress;
894  }
895  }
896 
897  pthread_create(&dev->thread, NULL, read_thread, dev);
898 
899  /* Wait here for the read thread to be initialized. */
900  pthread_barrier_wait(&dev->barrier);
901 
902  }
903  free(dev_path);
904  }
905  }
906  }
907  libusb_free_config_descriptor(conf_desc);
908 
909  }
910 
911  libusb_free_device_list(devs, 1);
912 
913  /* If we have a good handle, return it. */
914  if (good_open) {
915  return dev;
916  }
917  else {
918  /* Unable to open any devices. */
919  free_hid_device(dev);
920  return NULL;
921  }
922 }
923 
924 
925 int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
926 {
927  int res;
928  int report_number = data[0];
929  int skipped_report_id = 0;
930 
931  if (report_number == 0x0) {
932  data++;
933  length--;
934  skipped_report_id = 1;
935  }
936 
937 
938  if (dev->output_endpoint <= 0) {
939  /* No interrput out endpoint. Use the Control Endpoint */
940  res = libusb_control_transfer(dev->device_handle,
941  LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT,
942  0x09/*HID Set_Report*/,
943  (2/*HID output*/ << 8) | report_number,
944  dev->interface,
945  (unsigned char *)data, length,
946  1000/*timeout millis*/);
947 
948  if (res < 0)
949  return -1;
950 
951  if (skipped_report_id)
952  length++;
953 
954  return length;
955  }
956  else {
957  /* Use the interrupt out endpoint */
958  int actual_length;
959  res = libusb_interrupt_transfer(dev->device_handle,
960  dev->output_endpoint,
961  (unsigned char*)data,
962  length,
963  &actual_length, 1000);
964 
965  if (res < 0)
966  return -1;
967 
968  if (skipped_report_id)
969  actual_length++;
970 
971  return actual_length;
972  }
973 }
974 
975 /* Helper function, to simplify hid_read().
976  This should be called with dev->mutex locked. */
977 static int return_data(hid_device *dev, unsigned char *data, size_t length)
978 {
979  /* Copy the data out of the linked list item (rpt) into the
980  return buffer (data), and delete the liked list item. */
981  struct input_report *rpt = dev->input_reports;
982  size_t len = (length < rpt->len)? length: rpt->len;
983  if (len > 0)
984  memcpy(data, rpt->data, len);
985  dev->input_reports = rpt->next;
986  free(rpt->data);
987  free(rpt);
988  return len;
989 }
990 
991 static void cleanup_mutex(void *param)
992 {
993  hid_device *dev = param;
994  pthread_mutex_unlock(&dev->mutex);
995 }
996 
997 
998 int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
999 {
1000  int bytes_read = -1;
1001 
1002 #if 0
1003  int transferred;
1004  int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
1005  LOG("transferred: %d\n", transferred);
1006  return transferred;
1007 #endif
1008 
1009  pthread_mutex_lock(&dev->mutex);
1010  pthread_cleanup_push(&cleanup_mutex, dev);
1011 
1012  /* There's an input report queued up. Return it. */
1013  if (dev->input_reports) {
1014  /* Return the first one */
1015  bytes_read = return_data(dev, data, length);
1016  goto ret;
1017  }
1018 
1019  if (dev->shutdown_thread) {
1020  /* This means the device has been disconnected.
1021  An error code of -1 should be returned. */
1022  bytes_read = -1;
1023  goto ret;
1024  }
1025 
1026  if (milliseconds == -1) {
1027  /* Blocking */
1028  while (!dev->input_reports && !dev->shutdown_thread) {
1029  pthread_cond_wait(&dev->condition, &dev->mutex);
1030  }
1031  if (dev->input_reports) {
1032  bytes_read = return_data(dev, data, length);
1033  }
1034  }
1035  else if (milliseconds > 0) {
1036  /* Non-blocking, but called with timeout. */
1037  int res;
1038  struct timespec ts;
1039  clock_gettime(CLOCK_REALTIME, &ts);
1040  ts.tv_sec += milliseconds / 1000;
1041  ts.tv_nsec += (milliseconds % 1000) * 1000000;
1042  if (ts.tv_nsec >= 1000000000L) {
1043  ts.tv_sec++;
1044  ts.tv_nsec -= 1000000000L;
1045  }
1046 
1047  while (!dev->input_reports && !dev->shutdown_thread) {
1048  res = pthread_cond_timedwait(&dev->condition, &dev->mutex, &ts);
1049  if (res == 0) {
1050  if (dev->input_reports) {
1051  bytes_read = return_data(dev, data, length);
1052  break;
1053  }
1054 
1055  /* If we're here, there was a spurious wake up
1056  or the read thread was shutdown. Run the
1057  loop again (ie: don't break). */
1058  }
1059  else if (res == ETIMEDOUT) {
1060  /* Timed out. */
1061  bytes_read = 0;
1062  break;
1063  }
1064  else {
1065  /* Error. */
1066  bytes_read = -1;
1067  break;
1068  }
1069  }
1070  }
1071  else {
1072  /* Purely non-blocking */
1073  bytes_read = 0;
1074  }
1075 
1076 ret:
1077  pthread_mutex_unlock(&dev->mutex);
1078  pthread_cleanup_pop(0);
1079 
1080  return bytes_read;
1081 }
1082 
1083 int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
1084 {
1085  return hid_read_timeout(dev, data, length, dev->blocking ? -1 : 0);
1086 }
1087 
1089 {
1090  dev->blocking = !nonblock;
1091 
1092  return 0;
1093 }
1094 
1095 
1096 int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
1097 {
1098  int res = -1;
1099  int skipped_report_id = 0;
1100  int report_number = data[0];
1101 
1102  if (report_number == 0x0) {
1103  data++;
1104  length--;
1105  skipped_report_id = 1;
1106  }
1107 
1108  res = libusb_control_transfer(dev->device_handle,
1109  LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT,
1110  0x09/*HID set_report*/,
1111  (3/*HID feature*/ << 8) | report_number,
1112  dev->interface,
1113  (unsigned char *)data, length,
1114  1000/*timeout millis*/);
1115 
1116  if (res < 0)
1117  return -1;
1118 
1119  /* Account for the report ID */
1120  if (skipped_report_id)
1121  length++;
1122 
1123  return length;
1124 }
1125 
1126 int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
1127 {
1128  int res = -1;
1129  int skipped_report_id = 0;
1130  int report_number = data[0];
1131 
1132  if (report_number == 0x0) {
1133  /* Offset the return buffer by 1, so that the report ID
1134  will remain in byte 0. */
1135  data++;
1136  length--;
1137  skipped_report_id = 1;
1138  }
1139  res = libusb_control_transfer(dev->device_handle,
1140  LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_IN,
1141  0x01/*HID get_report*/,
1142  (3/*HID feature*/ << 8) | report_number,
1143  dev->interface,
1144  (unsigned char *)data, length,
1145  1000/*timeout millis*/);
1146 
1147  if (res < 0)
1148  return -1;
1149 
1150  if (skipped_report_id)
1151  res++;
1152 
1153  return res;
1154 }
1155 
1156 
1158 {
1159  if (!dev)
1160  return;
1161 
1162  /* Cause read_thread() to stop. */
1163  dev->shutdown_thread = 1;
1164  libusb_cancel_transfer(dev->transfer);
1165 
1166  /* Wait for read_thread() to end. */
1167  pthread_join(dev->thread, NULL);
1168 
1169  /* Clean up the Transfer objects allocated in read_thread(). */
1170  free(dev->transfer->buffer);
1171  libusb_free_transfer(dev->transfer);
1172 
1173  /* release the interface */
1174  libusb_release_interface(dev->device_handle, dev->interface);
1175 
1176  /* Close the handle */
1177  libusb_close(dev->device_handle);
1178 
1179  /* Clear out the queue of received reports. */
1180  pthread_mutex_lock(&dev->mutex);
1181  while (dev->input_reports) {
1182  return_data(dev, NULL, 0);
1183  }
1184  pthread_mutex_unlock(&dev->mutex);
1185 
1186  free_hid_device(dev);
1187 }
1188 
1189 
1190 int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
1191 {
1192  return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
1193 }
1194 
1195 int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
1196 {
1197  return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
1198 }
1199 
1200 int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
1201 {
1202  return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
1203 }
1204 
1205 int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
1206 {
1207  wchar_t *str;
1208 
1209  str = get_usb_string(dev->device_handle, string_index);
1210  if (str) {
1211  wcsncpy(string, str, maxlen);
1212  string[maxlen-1] = L'\0';
1213  free(str);
1214  return 0;
1215  }
1216  else
1217  return -1;
1218 }
1219 
1220 
1222 {
1223  return NULL;
1224 }
1225 
1226 
1228  const char *name;
1229  const char *string_code;
1231 };
1232 
1233 #define LANG(name,code,usb_code) { name, code, usb_code }
1234 static struct lang_map_entry lang_map[] = {
1235  LANG("Afrikaans", "af", 0x0436),
1236  LANG("Albanian", "sq", 0x041C),
1237  LANG("Arabic - United Arab Emirates", "ar_ae", 0x3801),
1238  LANG("Arabic - Bahrain", "ar_bh", 0x3C01),
1239  LANG("Arabic - Algeria", "ar_dz", 0x1401),
1240  LANG("Arabic - Egypt", "ar_eg", 0x0C01),
1241  LANG("Arabic - Iraq", "ar_iq", 0x0801),
1242  LANG("Arabic - Jordan", "ar_jo", 0x2C01),
1243  LANG("Arabic - Kuwait", "ar_kw", 0x3401),
1244  LANG("Arabic - Lebanon", "ar_lb", 0x3001),
1245  LANG("Arabic - Libya", "ar_ly", 0x1001),
1246  LANG("Arabic - Morocco", "ar_ma", 0x1801),
1247  LANG("Arabic - Oman", "ar_om", 0x2001),
1248  LANG("Arabic - Qatar", "ar_qa", 0x4001),
1249  LANG("Arabic - Saudi Arabia", "ar_sa", 0x0401),
1250  LANG("Arabic - Syria", "ar_sy", 0x2801),
1251  LANG("Arabic - Tunisia", "ar_tn", 0x1C01),
1252  LANG("Arabic - Yemen", "ar_ye", 0x2401),
1253  LANG("Armenian", "hy", 0x042B),
1254  LANG("Azeri - Latin", "az_az", 0x042C),
1255  LANG("Azeri - Cyrillic", "az_az", 0x082C),
1256  LANG("Basque", "eu", 0x042D),
1257  LANG("Belarusian", "be", 0x0423),
1258  LANG("Bulgarian", "bg", 0x0402),
1259  LANG("Catalan", "ca", 0x0403),
1260  LANG("Chinese - China", "zh_cn", 0x0804),
1261  LANG("Chinese - Hong Kong SAR", "zh_hk", 0x0C04),
1262  LANG("Chinese - Macau SAR", "zh_mo", 0x1404),
1263  LANG("Chinese - Singapore", "zh_sg", 0x1004),
1264  LANG("Chinese - Taiwan", "zh_tw", 0x0404),
1265  LANG("Croatian", "hr", 0x041A),
1266  LANG("Czech", "cs", 0x0405),
1267  LANG("Danish", "da", 0x0406),
1268  LANG("Dutch - Netherlands", "nl_nl", 0x0413),
1269  LANG("Dutch - Belgium", "nl_be", 0x0813),
1270  LANG("English - Australia", "en_au", 0x0C09),
1271  LANG("English - Belize", "en_bz", 0x2809),
1272  LANG("English - Canada", "en_ca", 0x1009),
1273  LANG("English - Caribbean", "en_cb", 0x2409),
1274  LANG("English - Ireland", "en_ie", 0x1809),
1275  LANG("English - Jamaica", "en_jm", 0x2009),
1276  LANG("English - New Zealand", "en_nz", 0x1409),
1277  LANG("English - Phillippines", "en_ph", 0x3409),
1278  LANG("English - Southern Africa", "en_za", 0x1C09),
1279  LANG("English - Trinidad", "en_tt", 0x2C09),
1280  LANG("English - Great Britain", "en_gb", 0x0809),
1281  LANG("English - United States", "en_us", 0x0409),
1282  LANG("Estonian", "et", 0x0425),
1283  LANG("Farsi", "fa", 0x0429),
1284  LANG("Finnish", "fi", 0x040B),
1285  LANG("Faroese", "fo", 0x0438),
1286  LANG("French - France", "fr_fr", 0x040C),
1287  LANG("French - Belgium", "fr_be", 0x080C),
1288  LANG("French - Canada", "fr_ca", 0x0C0C),
1289  LANG("French - Luxembourg", "fr_lu", 0x140C),
1290  LANG("French - Switzerland", "fr_ch", 0x100C),
1291  LANG("Gaelic - Ireland", "gd_ie", 0x083C),
1292  LANG("Gaelic - Scotland", "gd", 0x043C),
1293  LANG("German - Germany", "de_de", 0x0407),
1294  LANG("German - Austria", "de_at", 0x0C07),
1295  LANG("German - Liechtenstein", "de_li", 0x1407),
1296  LANG("German - Luxembourg", "de_lu", 0x1007),
1297  LANG("German - Switzerland", "de_ch", 0x0807),
1298  LANG("Greek", "el", 0x0408),
1299  LANG("Hebrew", "he", 0x040D),
1300  LANG("Hindi", "hi", 0x0439),
1301  LANG("Hungarian", "hu", 0x040E),
1302  LANG("Icelandic", "is", 0x040F),
1303  LANG("Indonesian", "id", 0x0421),
1304  LANG("Italian - Italy", "it_it", 0x0410),
1305  LANG("Italian - Switzerland", "it_ch", 0x0810),
1306  LANG("Japanese", "ja", 0x0411),
1307  LANG("Korean", "ko", 0x0412),
1308  LANG("Latvian", "lv", 0x0426),
1309  LANG("Lithuanian", "lt", 0x0427),
1310  LANG("F.Y.R.O. Macedonia", "mk", 0x042F),
1311  LANG("Malay - Malaysia", "ms_my", 0x043E),
1312  LANG("Malay – Brunei", "ms_bn", 0x083E),
1313  LANG("Maltese", "mt", 0x043A),
1314  LANG("Marathi", "mr", 0x044E),
1315  LANG("Norwegian - Bokml", "no_no", 0x0414),
1316  LANG("Norwegian - Nynorsk", "no_no", 0x0814),
1317  LANG("Polish", "pl", 0x0415),
1318  LANG("Portuguese - Portugal", "pt_pt", 0x0816),
1319  LANG("Portuguese - Brazil", "pt_br", 0x0416),
1320  LANG("Raeto-Romance", "rm", 0x0417),
1321  LANG("Romanian - Romania", "ro", 0x0418),
1322  LANG("Romanian - Republic of Moldova", "ro_mo", 0x0818),
1323  LANG("Russian", "ru", 0x0419),
1324  LANG("Russian - Republic of Moldova", "ru_mo", 0x0819),
1325  LANG("Sanskrit", "sa", 0x044F),
1326  LANG("Serbian - Cyrillic", "sr_sp", 0x0C1A),
1327  LANG("Serbian - Latin", "sr_sp", 0x081A),
1328  LANG("Setsuana", "tn", 0x0432),
1329  LANG("Slovenian", "sl", 0x0424),
1330  LANG("Slovak", "sk", 0x041B),
1331  LANG("Sorbian", "sb", 0x042E),
1332  LANG("Spanish - Spain (Traditional)", "es_es", 0x040A),
1333  LANG("Spanish - Argentina", "es_ar", 0x2C0A),
1334  LANG("Spanish - Bolivia", "es_bo", 0x400A),
1335  LANG("Spanish - Chile", "es_cl", 0x340A),
1336  LANG("Spanish - Colombia", "es_co", 0x240A),
1337  LANG("Spanish - Costa Rica", "es_cr", 0x140A),
1338  LANG("Spanish - Dominican Republic", "es_do", 0x1C0A),
1339  LANG("Spanish - Ecuador", "es_ec", 0x300A),
1340  LANG("Spanish - Guatemala", "es_gt", 0x100A),
1341  LANG("Spanish - Honduras", "es_hn", 0x480A),
1342  LANG("Spanish - Mexico", "es_mx", 0x080A),
1343  LANG("Spanish - Nicaragua", "es_ni", 0x4C0A),
1344  LANG("Spanish - Panama", "es_pa", 0x180A),
1345  LANG("Spanish - Peru", "es_pe", 0x280A),
1346  LANG("Spanish - Puerto Rico", "es_pr", 0x500A),
1347  LANG("Spanish - Paraguay", "es_py", 0x3C0A),
1348  LANG("Spanish - El Salvador", "es_sv", 0x440A),
1349  LANG("Spanish - Uruguay", "es_uy", 0x380A),
1350  LANG("Spanish - Venezuela", "es_ve", 0x200A),
1351  LANG("Southern Sotho", "st", 0x0430),
1352  LANG("Swahili", "sw", 0x0441),
1353  LANG("Swedish - Sweden", "sv_se", 0x041D),
1354  LANG("Swedish - Finland", "sv_fi", 0x081D),
1355  LANG("Tamil", "ta", 0x0449),
1356  LANG("Tatar", "tt", 0X0444),
1357  LANG("Thai", "th", 0x041E),
1358  LANG("Turkish", "tr", 0x041F),
1359  LANG("Tsonga", "ts", 0x0431),
1360  LANG("Ukrainian", "uk", 0x0422),
1361  LANG("Urdu", "ur", 0x0420),
1362  LANG("Uzbek - Cyrillic", "uz_uz", 0x0843),
1363  LANG("Uzbek – Latin", "uz_uz", 0x0443),
1364  LANG("Vietnamese", "vi", 0x042A),
1365  LANG("Xhosa", "xh", 0x0434),
1366  LANG("Yiddish", "yi", 0x043D),
1367  LANG("Zulu", "zu", 0x0435),
1368  LANG(NULL, NULL, 0x0),
1369 };
1370 
1372 {
1373  char *locale;
1374  char search_string[64];
1375  char *ptr;
1376  struct lang_map_entry *lang;
1377 
1378  /* Get the current locale. */
1379  locale = setlocale(0, NULL);
1380  if (!locale)
1381  return 0x0;
1382 
1383  /* Make a copy of the current locale string. */
1384  strncpy(search_string, locale, sizeof(search_string));
1385  search_string[sizeof(search_string)-1] = '\0';
1386 
1387  /* Chop off the encoding part, and make it lower case. */
1388  ptr = search_string;
1389  while (*ptr) {
1390  *ptr = tolower(*ptr);
1391  if (*ptr == '.') {
1392  *ptr = '\0';
1393  break;
1394  }
1395  ptr++;
1396  }
1397 
1398  /* Find the entry which matches the string code of our locale. */
1399  lang = lang_map;
1400  while (lang->string_code) {
1401  if (!strcmp(lang->string_code, search_string)) {
1402  return lang->usb_code;
1403  }
1404  lang++;
1405  }
1406 
1407  /* There was no match. Find with just the language only. */
1408  /* Chop off the variant. Chop it off at the '_'. */
1409  ptr = search_string;
1410  while (*ptr) {
1411  *ptr = tolower(*ptr);
1412  if (*ptr == '_') {
1413  *ptr = '\0';
1414  break;
1415  }
1416  ptr++;
1417  }
1418 
1419 #if 0 /* TODO: Do we need this? */
1420  /* Find the entry which matches the string code of our language. */
1421  lang = lang_map;
1422  while (lang->string_code) {
1423  if (!strcmp(lang->string_code, search_string)) {
1424  return lang->usb_code;
1425  }
1426  lang++;
1427  }
1428 #endif
1429 
1430  /* Found nothing. */
1431  return 0x0;
1432 }
1433 
1434 #ifdef __cplusplus
1435 }
1436 #endif
hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number...
Definition: hid-libusb.c:619
int interface_number
Definition: hidapi.h:75
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
Definition: hid-libusb.c:1200
const char * name
Definition: hid-libusb.c:1228
#define LANG(name, code, usb_code)
Definition: hid-libusb.c:1233
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
Send a Feature report to the device.
Definition: hid-libusb.c:1096
#define HID_API_EXPORT
Definition: hidapi.h:36
pthread_mutex_t mutex
Definition: hid-libusb.c:113
int product_index
Definition: hid-libusb.c:105
int serial_index
Definition: hid-libusb.c:106
struct input_report * next
Definition: hid-libusb.c:87
wchar_t * manufacturer_string
Definition: hidapi.h:62
pthread_cond_t condition
Definition: hid-libusb.c:114
int shutdown_thread
Definition: hid-libusb.c:116
char * path
Definition: hidapi.h:51
struct input_report * input_reports
Definition: hid-libusb.c:121
void HID_API_EXPORT hid_close(hid_device *dev)
Close a HID device.
Definition: hid-libusb.c:1157
int manufacturer_index
Definition: hid-libusb.c:104
unsigned int uint32_t
Definition: rtptypes_win.h:46
Definition: hid-libusb.c:1227
int HID_API_EXPORT hid_exit(void)
Finalize the HIDAPI library.
Definition: hid-libusb.c:434
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
Read an Input report from a HID device.
Definition: hid-libusb.c:1083
struct libusb_transfer * transfer
Definition: hid-libusb.c:118
struct hid_device_info * next
Definition: hidapi.h:78
size_t len
Definition: hid-libusb.c:86
unsigned char uint8_t
Definition: rtptypes_win.h:42
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
Definition: hid-libusb.c:1195
unsigned short product_id
Definition: hidapi.h:55
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
Get a feature report from a HID device.
Definition: hid-libusb.c:1126
libusb_device_handle * device_handle
Definition: hid-libusb.c:93
hid_device *HID_API_EXPORT hid_open_path(const char *path)
Open a HID device by its path name.
Definition: hid-libusb.c:792
unsigned short uint16_t
Definition: rtptypes_win.h:44
uint16_t get_usb_code_for_current_locale(void)
Definition: hid-libusb.c:1371
int input_ep_max_packet_size
Definition: hid-libusb.c:98
wchar_t * serial_number
Definition: hidapi.h:57
pthread_barrier_t barrier
Definition: hid-libusb.c:115
#define LOG(...)
Definition: hid-libusb.c:61
int32_t i
Definition: decimators.h:244
const char * string_code
Definition: hid-libusb.c:1229
unsigned short vendor_id
Definition: hidapi.h:53
struct hid_device_info HID_API_EXPORT * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition: hid-libusb.c:444
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error(hid_device *dev)
Get a string describing the last error which occurred.
Definition: hid-libusb.c:1221
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
Definition: hid-libusb.c:1190
uint8_t * data
Definition: hid-libusb.c:85
wchar_t * product_string
Definition: hidapi.h:64
int HID_API_EXPORT hid_init(void)
Initialize the HIDAPI library.
Definition: hid-libusb.c:416
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
Read an Input report from a HID device with timeout.
Definition: hid-libusb.c:998
unsigned short usage
Definition: hidapi.h:70
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
Definition: hid-libusb.c:1205
pthread_t thread
Definition: hid-libusb.c:112
#define HID_API_EXPORT_CALL
Definition: hidapi.h:40
int input_endpoint
Definition: hid-libusb.c:96
unsigned short usage_page
Definition: hidapi.h:67
int output_endpoint
Definition: hid-libusb.c:97
uint16_t usb_code
Definition: hid-libusb.c:1230
void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
Definition: hid-libusb.c:605
#define HID_API_CALL
Definition: hidapi.h:37
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
Write an Output report to a HID device.
Definition: hid-libusb.c:925
unsigned short release_number
Definition: hidapi.h:60
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
Set the device handle to be non-blocking.
Definition: hid-libusb.c:1088