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