usb 1.7.2 → 1.8.0-libusb.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/.gitmodules +1 -1
  2. package/Readme.md +2 -2
  3. package/binding.gyp +2 -2
  4. package/libusb/.private/pre-commit.sh +7 -1
  5. package/libusb/.travis.yml +49 -0
  6. package/libusb/AUTHORS +44 -3
  7. package/libusb/Brewfile +4 -0
  8. package/libusb/ChangeLog +74 -2
  9. package/libusb/README.md +32 -0
  10. package/libusb/TODO +1 -1
  11. package/libusb/Xcode/common.xcconfig +12 -0
  12. package/libusb/Xcode/config.h +25 -0
  13. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +959 -1
  14. package/libusb/android/README +4 -2
  15. package/libusb/android/config.h +75 -0
  16. package/libusb/appveyor.yml +41 -0
  17. package/libusb/appveyor_cygwin.bat +11 -0
  18. package/libusb/appveyor_minGW.bat +19 -0
  19. package/libusb/autogen.sh +1 -1
  20. package/libusb/bootstrap.sh +3 -16
  21. package/libusb/configure.ac +108 -80
  22. package/libusb/doc/doxygen.cfg.in +1785 -739
  23. package/libusb/examples/Makefile.am +1 -1
  24. package/libusb/examples/dpfp.c +3 -1
  25. package/libusb/examples/dpfp_threaded.c +23 -10
  26. package/libusb/examples/ezusb.c +3 -3
  27. package/libusb/examples/ezusb.h +2 -2
  28. package/libusb/examples/fxload.c +31 -9
  29. package/libusb/examples/hotplugtest.c +35 -7
  30. package/libusb/examples/listdevs.c +3 -1
  31. package/libusb/examples/sam3u_benchmark.c +3 -3
  32. package/libusb/examples/testlibusb.c +277 -0
  33. package/libusb/examples/xusb.c +40 -34
  34. package/libusb/libusb/Makefile.am +49 -23
  35. package/libusb/libusb/core.c +855 -457
  36. package/libusb/libusb/descriptor.c +72 -78
  37. package/libusb/libusb/hotplug.c +122 -76
  38. package/libusb/libusb/hotplug.h +42 -25
  39. package/libusb/libusb/io.c +625 -390
  40. package/libusb/libusb/libusb-1.0.def +12 -0
  41. package/libusb/libusb/libusb.h +218 -150
  42. package/libusb/libusb/libusbi.h +346 -176
  43. package/libusb/libusb/os/darwin_usb.c +604 -319
  44. package/libusb/libusb/os/darwin_usb.h +61 -20
  45. package/libusb/libusb/os/haiku_pollfs.cpp +367 -0
  46. package/libusb/libusb/os/haiku_usb.h +113 -0
  47. package/libusb/libusb/os/haiku_usb_backend.cpp +533 -0
  48. package/libusb/libusb/os/haiku_usb_raw.cpp +267 -0
  49. package/libusb/libusb/os/haiku_usb_raw.h +188 -0
  50. package/libusb/libusb/os/linux_netlink.c +186 -146
  51. package/libusb/libusb/os/linux_udev.c +36 -14
  52. package/libusb/libusb/os/linux_usbfs.c +426 -225
  53. package/libusb/libusb/os/linux_usbfs.h +5 -3
  54. package/libusb/libusb/os/netbsd_usb.c +21 -77
  55. package/libusb/libusb/os/openbsd_usb.c +32 -115
  56. package/libusb/libusb/os/poll_posix.c +38 -5
  57. package/libusb/libusb/os/poll_posix.h +3 -0
  58. package/libusb/libusb/os/poll_windows.c +277 -626
  59. package/libusb/libusb/os/poll_windows.h +11 -44
  60. package/libusb/libusb/os/sunos_usb.c +1695 -0
  61. package/libusb/libusb/os/sunos_usb.h +80 -0
  62. package/libusb/libusb/os/threads_posix.c +24 -26
  63. package/libusb/libusb/os/threads_posix.h +73 -21
  64. package/libusb/libusb/os/threads_windows.c +71 -157
  65. package/libusb/libusb/os/threads_windows.h +68 -44
  66. package/libusb/libusb/os/wince_usb.c +276 -420
  67. package/libusb/libusb/os/wince_usb.h +23 -28
  68. package/libusb/libusb/os/windows_common.h +78 -58
  69. package/libusb/libusb/os/windows_nt_common.c +1010 -0
  70. package/libusb/libusb/os/windows_nt_common.h +110 -0
  71. package/libusb/libusb/os/windows_nt_shared_types.h +147 -0
  72. package/libusb/libusb/os/windows_usbdk.c +830 -0
  73. package/libusb/libusb/os/windows_usbdk.h +103 -0
  74. package/libusb/libusb/os/windows_winusb.c +4391 -0
  75. package/libusb/libusb/os/windows_winusb.h +783 -0
  76. package/libusb/libusb/strerror.c +41 -7
  77. package/libusb/libusb/sync.c +41 -13
  78. package/libusb/libusb/version.h +1 -1
  79. package/libusb/libusb/version_nano.h +1 -1
  80. package/libusb/libusb-1.0.pc.in +1 -1
  81. package/libusb/msvc/appveyor.bat +27 -0
  82. package/libusb/msvc/config.h +5 -4
  83. package/libusb/msvc/ddk_build.cmd +87 -43
  84. package/libusb/msvc/fxload_2010.vcxproj +24 -104
  85. package/libusb/msvc/fxload_2012.vcxproj +24 -107
  86. package/libusb/msvc/fxload_2013.vcxproj +24 -107
  87. package/libusb/msvc/fxload_2015.vcxproj +91 -0
  88. package/libusb/msvc/fxload_2017.vcxproj +114 -0
  89. package/libusb/msvc/fxload_sources +1 -1
  90. package/libusb/msvc/getopt_2010.vcxproj +16 -75
  91. package/libusb/msvc/getopt_2012.vcxproj +16 -79
  92. package/libusb/msvc/getopt_2013.vcxproj +16 -79
  93. package/libusb/msvc/getopt_2015.vcxproj +73 -0
  94. package/libusb/msvc/getopt_2017.vcxproj +98 -0
  95. package/libusb/msvc/getopt_sources +6 -2
  96. package/libusb/msvc/hotplugtest_2010.vcxproj +18 -99
  97. package/libusb/msvc/hotplugtest_2012.vcxproj +18 -102
  98. package/libusb/msvc/hotplugtest_2013.vcxproj +18 -102
  99. package/libusb/msvc/hotplugtest_2015.vcxproj +83 -0
  100. package/libusb/msvc/hotplugtest_2017.vcxproj +106 -0
  101. package/libusb/msvc/hotplugtest_sources +1 -1
  102. package/libusb/msvc/libusb_2005.sln +20 -20
  103. package/libusb/msvc/libusb_2010.sln +57 -46
  104. package/libusb/msvc/libusb_2012.sln +57 -46
  105. package/libusb/msvc/libusb_2013.sln +57 -50
  106. package/libusb/msvc/libusb_2015.sln +59 -52
  107. package/libusb/msvc/libusb_2017.sln +186 -0
  108. package/libusb/msvc/libusb_dll.dsp +2 -2
  109. package/libusb/msvc/libusb_dll_2005.vcproj +30 -2
  110. package/libusb/msvc/libusb_dll_2010.vcxproj +26 -90
  111. package/libusb/msvc/libusb_dll_2012.vcxproj +28 -96
  112. package/libusb/msvc/libusb_dll_2013.vcxproj +28 -96
  113. package/libusb/msvc/libusb_dll_2015.vcxproj +107 -0
  114. package/libusb/msvc/libusb_dll_2017.vcxproj +134 -0
  115. package/libusb/msvc/libusb_dll_wince.vcproj +9 -1
  116. package/libusb/msvc/libusb_sources +10 -5
  117. package/libusb/msvc/libusb_static.dsp +2 -2
  118. package/libusb/msvc/libusb_static_2005.vcproj +32 -4
  119. package/libusb/msvc/libusb_static_2010.vcxproj +24 -83
  120. package/libusb/msvc/libusb_static_2012.vcxproj +25 -87
  121. package/libusb/msvc/libusb_static_2013.vcxproj +25 -87
  122. package/libusb/msvc/libusb_static_2015.vcxproj +98 -0
  123. package/libusb/msvc/libusb_static_2017.vcxproj +117 -0
  124. package/libusb/msvc/libusb_static_wince.vcproj +20 -26
  125. package/libusb/msvc/libusb_wince.sln +88 -88
  126. package/libusb/msvc/listdevs_2010.vcxproj +16 -99
  127. package/libusb/msvc/listdevs_2012.vcxproj +16 -102
  128. package/libusb/msvc/listdevs_2013.vcxproj +16 -102
  129. package/libusb/msvc/listdevs_2015.vcxproj +83 -0
  130. package/libusb/msvc/listdevs_2017.vcxproj +106 -0
  131. package/libusb/msvc/listdevs_sources +2 -1
  132. package/libusb/msvc/stress_2010.vcxproj +20 -101
  133. package/libusb/msvc/stress_2012.vcxproj +20 -104
  134. package/libusb/msvc/stress_2013.vcxproj +20 -104
  135. package/libusb/msvc/stress_2015.vcxproj +87 -0
  136. package/libusb/msvc/stress_2017.vcxproj +110 -0
  137. package/libusb/msvc/stress_sources +21 -0
  138. package/libusb/msvc/testlibusb_2010.vcxproj +82 -0
  139. package/libusb/msvc/testlibusb_2012.vcxproj +83 -0
  140. package/libusb/msvc/testlibusb_2013.vcxproj +83 -0
  141. package/libusb/msvc/testlibusb_2015.vcxproj +83 -0
  142. package/libusb/msvc/testlibusb_2017.vcxproj +106 -0
  143. package/libusb/msvc/testlibusb_sources +20 -0
  144. package/libusb/msvc/xusb_2010.vcxproj +17 -98
  145. package/libusb/msvc/xusb_2012.vcxproj +17 -101
  146. package/libusb/msvc/xusb_2013.vcxproj +17 -101
  147. package/libusb/msvc/xusb_2015.vcxproj +83 -0
  148. package/libusb/msvc/xusb_2017.vcxproj +106 -0
  149. package/libusb/msvc/xusb_sources +1 -1
  150. package/libusb/tests/stress.c +2 -2
  151. package/libusb/tests/testlib.c +0 -4
  152. package/libusb/travis-autogen.sh +39 -0
  153. package/libusb.gypi +13 -2
  154. package/package.json +20 -11
  155. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  156. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  157. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  158. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  159. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  160. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  161. package/prebuilds/linux-ia32/node.napi.node +0 -0
  162. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  163. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  164. package/prebuilds/win32-ia32/node.napi.node +0 -0
  165. package/prebuilds/win32-x64/node.napi.node +0 -0
  166. package/src/device.cc +1 -1
  167. package/usb.js +1 -1
  168. package/.github/workflows/prebuild.yml +0 -49
  169. package/libusb/INSTALL +0 -234
  170. package/libusb/README +0 -28
  171. package/libusb/libusb/os/windows_usb.c +0 -5347
  172. package/libusb/libusb/os/windows_usb.h +0 -971
  173. package/libusb/msvc/fxload_2010.vcxproj.filters +0 -25
  174. package/libusb/msvc/fxload_2012.vcxproj.filters +0 -25
  175. package/libusb/msvc/getopt_2010.vcxproj.filters +0 -26
  176. package/libusb/msvc/getopt_2012.vcxproj.filters +0 -26
  177. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +0 -14
  178. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +0 -14
  179. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +0 -81
  180. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +0 -84
  181. package/libusb/msvc/libusb_static_2010.vcxproj.filters +0 -74
  182. package/libusb/msvc/libusb_static_2012.vcxproj.filters +0 -74
  183. package/libusb/msvc/listdevs_2010.vcxproj.filters +0 -14
  184. package/libusb/msvc/listdevs_2012.vcxproj.filters +0 -14
  185. package/libusb/msvc/stress_2010.vcxproj.filters +0 -25
  186. package/libusb/msvc/stress_2012.vcxproj.filters +0 -25
  187. package/libusb/msvc/xusb_2010.vcxproj.filters +0 -14
  188. package/libusb/msvc/xusb_2012.vcxproj.filters +0 -14
@@ -0,0 +1,533 @@
1
+ /*
2
+ * Haiku Backend for libusb
3
+ * Copyright © 2014 Akshay Jaggi <akshay1994.leo@gmail.com>
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with this library; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+
21
+ #include <unistd.h>
22
+ #include <string.h>
23
+ #include <stdlib.h>
24
+ #include <new>
25
+ #include <vector>
26
+
27
+ #include "haiku_usb.h"
28
+
29
+ int _errno_to_libusb(int status)
30
+ {
31
+ return status;
32
+ }
33
+
34
+ USBTransfer::USBTransfer(struct usbi_transfer *itransfer, USBDevice *device)
35
+ {
36
+ fUsbiTransfer = itransfer;
37
+ fLibusbTransfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
38
+ fUSBDevice = device;
39
+ fCancelled = false;
40
+ }
41
+
42
+ USBTransfer::~USBTransfer()
43
+ {
44
+ }
45
+
46
+ struct usbi_transfer *
47
+ USBTransfer::UsbiTransfer()
48
+ {
49
+ return fUsbiTransfer;
50
+ }
51
+
52
+ void
53
+ USBTransfer::SetCancelled()
54
+ {
55
+ fCancelled = true;
56
+ }
57
+
58
+ bool
59
+ USBTransfer::IsCancelled()
60
+ {
61
+ return fCancelled;
62
+ }
63
+
64
+ void
65
+ USBTransfer::Do(int fRawFD)
66
+ {
67
+ switch (fLibusbTransfer->type) {
68
+ case LIBUSB_TRANSFER_TYPE_CONTROL:
69
+ {
70
+ struct libusb_control_setup *setup = (struct libusb_control_setup *)fLibusbTransfer->buffer;
71
+ usb_raw_command command;
72
+ command.control.request_type = setup->bmRequestType;
73
+ command.control.request = setup->bRequest;
74
+ command.control.value = setup->wValue;
75
+ command.control.index = setup->wIndex;
76
+ command.control.length = setup->wLength;
77
+ command.control.data = fLibusbTransfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
78
+ if (fCancelled)
79
+ break;
80
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_CONTROL_TRANSFER, &command, sizeof(command)) ||
81
+ command.control.status != B_USB_RAW_STATUS_SUCCESS) {
82
+ fUsbiTransfer->transferred = -1;
83
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "failed control transfer");
84
+ break;
85
+ }
86
+ fUsbiTransfer->transferred = command.control.length;
87
+ }
88
+ break;
89
+ case LIBUSB_TRANSFER_TYPE_BULK:
90
+ case LIBUSB_TRANSFER_TYPE_INTERRUPT:
91
+ {
92
+ usb_raw_command command;
93
+ command.transfer.interface = fUSBDevice->EndpointToInterface(fLibusbTransfer->endpoint);
94
+ command.transfer.endpoint = fUSBDevice->EndpointToIndex(fLibusbTransfer->endpoint);
95
+ command.transfer.data = fLibusbTransfer->buffer;
96
+ command.transfer.length = fLibusbTransfer->length;
97
+ if (fCancelled)
98
+ break;
99
+ if (fLibusbTransfer->type == LIBUSB_TRANSFER_TYPE_BULK) {
100
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_BULK_TRANSFER, &command, sizeof(command)) ||
101
+ command.transfer.status != B_USB_RAW_STATUS_SUCCESS) {
102
+ fUsbiTransfer->transferred = -1;
103
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "failed bulk transfer");
104
+ break;
105
+ }
106
+ }
107
+ else {
108
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_INTERRUPT_TRANSFER, &command, sizeof(command)) ||
109
+ command.transfer.status != B_USB_RAW_STATUS_SUCCESS) {
110
+ fUsbiTransfer->transferred = -1;
111
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "failed interrupt transfer");
112
+ break;
113
+ }
114
+ }
115
+ fUsbiTransfer->transferred = command.transfer.length;
116
+ }
117
+ break;
118
+ // IsochronousTransfers not tested
119
+ case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
120
+ {
121
+ usb_raw_command command;
122
+ command.isochronous.interface = fUSBDevice->EndpointToInterface(fLibusbTransfer->endpoint);
123
+ command.isochronous.endpoint = fUSBDevice->EndpointToIndex(fLibusbTransfer->endpoint);
124
+ command.isochronous.data = fLibusbTransfer->buffer;
125
+ command.isochronous.length = fLibusbTransfer->length;
126
+ command.isochronous.packet_count = fLibusbTransfer->num_iso_packets;
127
+ int i;
128
+ usb_iso_packet_descriptor *packetDescriptors = new usb_iso_packet_descriptor[fLibusbTransfer->num_iso_packets];
129
+ for (i = 0; i < fLibusbTransfer->num_iso_packets; i++) {
130
+ if ((int16)(fLibusbTransfer->iso_packet_desc[i]).length != (fLibusbTransfer->iso_packet_desc[i]).length) {
131
+ fUsbiTransfer->transferred = -1;
132
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "failed isochronous transfer");
133
+ break;
134
+ }
135
+ packetDescriptors[i].request_length = (int16)(fLibusbTransfer->iso_packet_desc[i]).length;
136
+ }
137
+ if (i < fLibusbTransfer->num_iso_packets)
138
+ break; // TODO Handle this error
139
+ command.isochronous.packet_descriptors = packetDescriptors;
140
+ if (fCancelled)
141
+ break;
142
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_ISOCHRONOUS_TRANSFER, &command, sizeof(command)) ||
143
+ command.isochronous.status != B_USB_RAW_STATUS_SUCCESS) {
144
+ fUsbiTransfer->transferred = -1;
145
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "failed isochronous transfer");
146
+ break;
147
+ }
148
+ for (i = 0; i < fLibusbTransfer->num_iso_packets; i++) {
149
+ (fLibusbTransfer->iso_packet_desc[i]).actual_length = packetDescriptors[i].actual_length;
150
+ switch (packetDescriptors[i].status) {
151
+ case B_OK:
152
+ (fLibusbTransfer->iso_packet_desc[i]).status = LIBUSB_TRANSFER_COMPLETED;
153
+ break;
154
+ default:
155
+ (fLibusbTransfer->iso_packet_desc[i]).status = LIBUSB_TRANSFER_ERROR;
156
+ break;
157
+ }
158
+ }
159
+ delete[] packetDescriptors;
160
+ // Do we put the length of transfer here, for isochronous transfers?
161
+ fUsbiTransfer->transferred = command.transfer.length;
162
+ }
163
+ break;
164
+ default:
165
+ usbi_err(TRANSFER_CTX(fLibusbTransfer), "Unknown type of transfer");
166
+ }
167
+ }
168
+
169
+ bool
170
+ USBDeviceHandle::InitCheck()
171
+ {
172
+ return fInitCheck;
173
+ }
174
+
175
+ status_t
176
+ USBDeviceHandle::TransfersThread(void *self)
177
+ {
178
+ USBDeviceHandle *handle = (USBDeviceHandle *)self;
179
+ handle->TransfersWorker();
180
+ return B_OK;
181
+ }
182
+
183
+ void
184
+ USBDeviceHandle::TransfersWorker()
185
+ {
186
+ while (true) {
187
+ status_t status = acquire_sem(fTransfersSem);
188
+ if (status == B_BAD_SEM_ID)
189
+ break;
190
+ if (status == B_INTERRUPTED)
191
+ continue;
192
+ fTransfersLock.Lock();
193
+ USBTransfer *fPendingTransfer = (USBTransfer *) fTransfers.RemoveItem((int32)0);
194
+ fTransfersLock.Unlock();
195
+ fPendingTransfer->Do(fRawFD);
196
+ usbi_signal_transfer_completion(fPendingTransfer->UsbiTransfer());
197
+ }
198
+ }
199
+
200
+ status_t
201
+ USBDeviceHandle::SubmitTransfer(struct usbi_transfer *itransfer)
202
+ {
203
+ USBTransfer *transfer = new USBTransfer(itransfer, fUSBDevice);
204
+ *((USBTransfer **)usbi_transfer_get_os_priv(itransfer)) = transfer;
205
+ BAutolock locker(fTransfersLock);
206
+ fTransfers.AddItem(transfer);
207
+ release_sem(fTransfersSem);
208
+ return LIBUSB_SUCCESS;
209
+ }
210
+
211
+ status_t
212
+ USBDeviceHandle::CancelTransfer(USBTransfer *transfer)
213
+ {
214
+ transfer->SetCancelled();
215
+ fTransfersLock.Lock();
216
+ bool removed = fTransfers.RemoveItem(transfer);
217
+ fTransfersLock.Unlock();
218
+ if(removed)
219
+ usbi_signal_transfer_completion(transfer->UsbiTransfer());
220
+ return LIBUSB_SUCCESS;
221
+ }
222
+
223
+ USBDeviceHandle::USBDeviceHandle(USBDevice *dev)
224
+ :
225
+ fTransfersThread(-1),
226
+ fUSBDevice(dev),
227
+ fClaimedInterfaces(0),
228
+ fInitCheck(false)
229
+ {
230
+ fRawFD = open(dev->Location(), O_RDWR | O_CLOEXEC);
231
+ if (fRawFD < 0) {
232
+ usbi_err(NULL,"failed to open device");
233
+ return;
234
+ }
235
+ fTransfersSem = create_sem(0, "Transfers Queue Sem");
236
+ fTransfersThread = spawn_thread(TransfersThread, "Transfer Worker", B_NORMAL_PRIORITY, this);
237
+ resume_thread(fTransfersThread);
238
+ fInitCheck = true;
239
+ }
240
+
241
+ USBDeviceHandle::~USBDeviceHandle()
242
+ {
243
+ if (fRawFD > 0)
244
+ close(fRawFD);
245
+ for(int i = 0; i < 32; i++) {
246
+ if (fClaimedInterfaces & (1U << i))
247
+ ReleaseInterface(i);
248
+ }
249
+ delete_sem(fTransfersSem);
250
+ if (fTransfersThread > 0)
251
+ wait_for_thread(fTransfersThread, NULL);
252
+ }
253
+
254
+ int
255
+ USBDeviceHandle::ClaimInterface(int inumber)
256
+ {
257
+ int status = fUSBDevice->ClaimInterface(inumber);
258
+ if (status == LIBUSB_SUCCESS)
259
+ fClaimedInterfaces |= (1U << inumber);
260
+ return status;
261
+ }
262
+
263
+ int
264
+ USBDeviceHandle::ReleaseInterface(int inumber)
265
+ {
266
+ fUSBDevice->ReleaseInterface(inumber);
267
+ fClaimedInterfaces &= ~(1U << inumber);
268
+ return LIBUSB_SUCCESS;
269
+ }
270
+
271
+ int
272
+ USBDeviceHandle::SetConfiguration(int config)
273
+ {
274
+ int config_index = fUSBDevice->CheckInterfacesFree(config);
275
+ if(config_index == LIBUSB_ERROR_BUSY || config_index == LIBUSB_ERROR_NOT_FOUND)
276
+ return config_index;
277
+ usb_raw_command command;
278
+ command.config.config_index = config_index;
279
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_SET_CONFIGURATION, &command, sizeof(command)) ||
280
+ command.config.status != B_USB_RAW_STATUS_SUCCESS) {
281
+ return _errno_to_libusb(command.config.status);
282
+ }
283
+ fUSBDevice->SetActiveConfiguration(config_index);
284
+ return LIBUSB_SUCCESS;
285
+ }
286
+
287
+ int
288
+ USBDeviceHandle::SetAltSetting(int inumber, int alt)
289
+ {
290
+ usb_raw_command command;
291
+ command.alternate.config_index = fUSBDevice->ActiveConfigurationIndex();
292
+ command.alternate.interface_index = inumber;
293
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_ACTIVE_ALT_INTERFACE_INDEX, &command, sizeof(command)) ||
294
+ command.alternate.status != B_USB_RAW_STATUS_SUCCESS) {
295
+ usbi_err(NULL, "Error retrieving active alternate interface");
296
+ return _errno_to_libusb(command.alternate.status);
297
+ }
298
+ if (command.alternate.alternate_info == alt) {
299
+ usbi_dbg("Setting alternate interface successful");
300
+ return LIBUSB_SUCCESS;
301
+ }
302
+ command.alternate.alternate_info = alt;
303
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_SET_ALT_INTERFACE, &command, sizeof(command)) ||
304
+ command.alternate.status != B_USB_RAW_STATUS_SUCCESS) { //IF IOCTL FAILS DEVICE DISONNECTED PROBABLY
305
+ usbi_err(NULL, "Error setting alternate interface");
306
+ return _errno_to_libusb(command.alternate.status);
307
+ }
308
+ usbi_dbg("Setting alternate interface successful");
309
+ return LIBUSB_SUCCESS;
310
+ }
311
+
312
+
313
+ int
314
+ USBDevice::ClearHalt(int endpoint)
315
+ {
316
+ usb_raw_command command;
317
+ command.control.request_type = USB_REQTYPE_ENDPOINT_OUT;
318
+ command.control.request = USB_REQUEST_CLEAR_FEATURE;
319
+ command.control.value = USB_FEATURE_ENDPOINT_HALT;
320
+ command.control.index = endpoint;
321
+ command.control.length = 0;
322
+
323
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_CONTROL_TRANSFER, &command, sizeof(command)) ||
324
+ command.control.status != B_USB_RAW_STATUS_SUCCESS) {
325
+ return _errno_to_libusb(command.control.status);
326
+ }
327
+ }
328
+
329
+
330
+ USBDevice::USBDevice(const char *path)
331
+ :
332
+ fPath(NULL),
333
+ fActiveConfiguration(0), //0?
334
+ fConfigurationDescriptors(NULL),
335
+ fClaimedInterfaces(0),
336
+ fEndpointToIndex(NULL),
337
+ fEndpointToInterface(NULL),
338
+ fInitCheck(false)
339
+ {
340
+ fPath=strdup(path);
341
+ Initialise();
342
+ }
343
+
344
+ USBDevice::~USBDevice()
345
+ {
346
+ free(fPath);
347
+ if (fConfigurationDescriptors) {
348
+ for(int i = 0; i < fDeviceDescriptor.num_configurations; i++) {
349
+ if (fConfigurationDescriptors[i])
350
+ delete fConfigurationDescriptors[i];
351
+ }
352
+ delete[] fConfigurationDescriptors;
353
+ }
354
+ if (fEndpointToIndex)
355
+ delete[] fEndpointToIndex;
356
+ if (fEndpointToInterface)
357
+ delete[] fEndpointToInterface;
358
+ }
359
+
360
+ bool
361
+ USBDevice::InitCheck()
362
+ {
363
+ return fInitCheck;
364
+ }
365
+
366
+ const char *
367
+ USBDevice::Location() const
368
+ {
369
+ return fPath;
370
+ }
371
+
372
+ uint8
373
+ USBDevice::CountConfigurations() const
374
+ {
375
+ return fDeviceDescriptor.num_configurations;
376
+ }
377
+
378
+ const usb_device_descriptor *
379
+ USBDevice::Descriptor() const
380
+ {
381
+ return &fDeviceDescriptor;
382
+ }
383
+
384
+ const usb_configuration_descriptor *
385
+ USBDevice::ConfigurationDescriptor(uint32 index) const
386
+ {
387
+ if (index > CountConfigurations())
388
+ return NULL;
389
+ return (usb_configuration_descriptor *) fConfigurationDescriptors[index];
390
+ }
391
+
392
+ const usb_configuration_descriptor *
393
+ USBDevice::ActiveConfiguration() const
394
+ {
395
+ return (usb_configuration_descriptor *) fConfigurationDescriptors[fActiveConfiguration];
396
+ }
397
+
398
+ int
399
+ USBDevice::ActiveConfigurationIndex() const
400
+ {
401
+ return fActiveConfiguration;
402
+ }
403
+
404
+ int USBDevice::ClaimInterface(int interface)
405
+ {
406
+ if (interface > ActiveConfiguration()->number_interfaces)
407
+ return LIBUSB_ERROR_NOT_FOUND;
408
+ if (fClaimedInterfaces & (1U << interface))
409
+ return LIBUSB_ERROR_BUSY;
410
+ fClaimedInterfaces |= (1U << interface);
411
+ return LIBUSB_SUCCESS;
412
+ }
413
+
414
+ int USBDevice::ReleaseInterface(int interface)
415
+ {
416
+ fClaimedInterfaces &= ~(1U << interface);
417
+ return LIBUSB_SUCCESS;
418
+ }
419
+
420
+ int
421
+ USBDevice::CheckInterfacesFree(int config)
422
+ {
423
+ if (fConfigToIndex.count(config) == 0)
424
+ return LIBUSB_ERROR_NOT_FOUND;
425
+ if (fClaimedInterfaces == 0)
426
+ return fConfigToIndex[(uint8)config];
427
+ return LIBUSB_ERROR_BUSY;
428
+ }
429
+
430
+ int
431
+ USBDevice::SetActiveConfiguration(int config_index)
432
+ {
433
+ fActiveConfiguration = config_index;
434
+ return LIBUSB_SUCCESS;
435
+ }
436
+
437
+ uint8
438
+ USBDevice::EndpointToIndex(uint8 address) const
439
+ {
440
+ return fEndpointToIndex[fActiveConfiguration][address];
441
+ }
442
+
443
+ uint8
444
+ USBDevice::EndpointToInterface(uint8 address) const
445
+ {
446
+ return fEndpointToInterface[fActiveConfiguration][address];
447
+ }
448
+
449
+ int
450
+ USBDevice::Initialise() //Do we need more error checking, etc? How to report?
451
+ {
452
+ int fRawFD = open(fPath, O_RDWR | O_CLOEXEC);
453
+ if (fRawFD < 0)
454
+ return B_ERROR;
455
+ usb_raw_command command;
456
+ command.device.descriptor = &fDeviceDescriptor;
457
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_DEVICE_DESCRIPTOR, &command, sizeof(command)) ||
458
+ command.device.status != B_USB_RAW_STATUS_SUCCESS) {
459
+ close(fRawFD);
460
+ return B_ERROR;
461
+ }
462
+
463
+ fConfigurationDescriptors = new(std::nothrow) unsigned char *[fDeviceDescriptor.num_configurations];
464
+ fEndpointToIndex = new(std::nothrow) map<uint8,uint8> [fDeviceDescriptor.num_configurations];
465
+ fEndpointToInterface = new(std::nothrow) map<uint8,uint8> [fDeviceDescriptor.num_configurations];
466
+ for (int i = 0; i < fDeviceDescriptor.num_configurations; i++) {
467
+ usb_configuration_descriptor tmp_config;
468
+ command.config.descriptor = &tmp_config;
469
+ command.config.config_index = i;
470
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR, &command, sizeof(command)) ||
471
+ command.config.status != B_USB_RAW_STATUS_SUCCESS) {
472
+ usbi_err(NULL, "failed retrieving configuration descriptor");
473
+ close(fRawFD);
474
+ return B_ERROR;
475
+ }
476
+ fConfigToIndex[tmp_config.configuration_value] = i;
477
+ fConfigurationDescriptors[i] = new(std::nothrow) unsigned char[tmp_config.total_length];
478
+
479
+ command.config_etc.descriptor = (usb_configuration_descriptor*)fConfigurationDescriptors[i];
480
+ command.config_etc.length = tmp_config.total_length;
481
+ command.config_etc.config_index = i;
482
+ if (ioctl(fRawFD, B_USB_COMMAND_GET_CONFIGURATION_DESCRIPTOR_ETC, &command, sizeof(command)) ||
483
+ command.config_etc.status != B_USB_RAW_STATUS_SUCCESS) {
484
+ usbi_err(NULL, "failed retrieving full configuration descriptor");
485
+ close(fRawFD);
486
+ return B_ERROR;
487
+ }
488
+
489
+ for (int j = 0; j < tmp_config.number_interfaces; j++) {
490
+ command.alternate.config_index = i;
491
+ command.alternate.interface_index = j;
492
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_ALT_INTERFACE_COUNT, &command, sizeof(command)) ||
493
+ command.config.status != B_USB_RAW_STATUS_SUCCESS) {
494
+ usbi_err(NULL, "failed retrieving number of alternate interfaces");
495
+ close(fRawFD);
496
+ return B_ERROR;
497
+ }
498
+ int num_alternate = command.alternate.alternate_info;
499
+ for (int k = 0; k < num_alternate; k++) {
500
+ usb_interface_descriptor tmp_interface;
501
+ command.interface_etc.config_index = i;
502
+ command.interface_etc.interface_index = j;
503
+ command.interface_etc.alternate_index = k;
504
+ command.interface_etc.descriptor = &tmp_interface;
505
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR_ETC, &command, sizeof(command)) ||
506
+ command.config.status != B_USB_RAW_STATUS_SUCCESS) {
507
+ usbi_err(NULL, "failed retrieving interface descriptor");
508
+ close(fRawFD);
509
+ return B_ERROR;
510
+ }
511
+ for (int l = 0; l < tmp_interface.num_endpoints; l++) {
512
+ usb_endpoint_descriptor tmp_endpoint;
513
+ command.endpoint_etc.config_index = i;
514
+ command.endpoint_etc.interface_index = j;
515
+ command.endpoint_etc.alternate_index = k;
516
+ command.endpoint_etc.endpoint_index = l;
517
+ command.endpoint_etc.descriptor = &tmp_endpoint;
518
+ if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_ENDPOINT_DESCRIPTOR_ETC, &command, sizeof(command)) ||
519
+ command.config.status != B_USB_RAW_STATUS_SUCCESS) {
520
+ usbi_err(NULL, "failed retrieving endpoint descriptor");
521
+ close(fRawFD);
522
+ return B_ERROR;
523
+ }
524
+ fEndpointToIndex[i][tmp_endpoint.endpoint_address] = l;
525
+ fEndpointToInterface[i][tmp_endpoint.endpoint_address] = j;
526
+ }
527
+ }
528
+ }
529
+ }
530
+ close(fRawFD);
531
+ fInitCheck = true;
532
+ return B_OK;
533
+ }