usb 2.0.0-alpha.1 → 2.0.3

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 (262) hide show
  1. package/.gitattributes +1 -0
  2. package/.gitmodules +1 -1
  3. package/CHANGELOG.md +91 -0
  4. package/README.md +570 -0
  5. package/binding.gyp +2 -2
  6. package/dist/index.d.ts +16 -5
  7. package/dist/index.js +135 -5
  8. package/dist/index.js.map +1 -1
  9. package/dist/usb/bindings.d.ts +13 -0
  10. package/dist/usb/bindings.js +2 -1
  11. package/dist/usb/bindings.js.map +1 -1
  12. package/dist/usb/index.d.ts +0 -2
  13. package/dist/usb/index.js +75 -112
  14. package/dist/usb/index.js.map +1 -1
  15. package/dist/usb/interface.js.map +1 -1
  16. package/dist/webusb/index.d.ts +28 -19
  17. package/dist/webusb/index.js +152 -66
  18. package/dist/webusb/index.js.map +1 -1
  19. package/dist/webusb/webusb-device.d.ts +1 -1
  20. package/dist/webusb/webusb-device.js +15 -6
  21. package/dist/webusb/webusb-device.js.map +1 -1
  22. package/libusb/.private/pre-commit.sh +7 -1
  23. package/libusb/.travis.yml +49 -0
  24. package/libusb/AUTHORS +44 -3
  25. package/libusb/Brewfile +4 -0
  26. package/libusb/ChangeLog +74 -2
  27. package/libusb/README.md +32 -0
  28. package/libusb/TODO +1 -1
  29. package/libusb/Xcode/common.xcconfig +12 -0
  30. package/libusb/Xcode/config.h +0 -3
  31. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +959 -1
  32. package/libusb/android/README +4 -2
  33. package/libusb/android/config.h +0 -6
  34. package/libusb/appveyor.yml +41 -0
  35. package/libusb/appveyor_cygwin.bat +11 -0
  36. package/libusb/appveyor_minGW.bat +19 -0
  37. package/libusb/autogen.sh +1 -1
  38. package/libusb/bootstrap.sh +3 -16
  39. package/libusb/configure.ac +108 -80
  40. package/libusb/doc/doxygen.cfg.in +1785 -739
  41. package/libusb/examples/Makefile.am +1 -1
  42. package/libusb/examples/dpfp.c +3 -1
  43. package/libusb/examples/dpfp_threaded.c +23 -10
  44. package/libusb/examples/ezusb.c +3 -3
  45. package/libusb/examples/ezusb.h +2 -2
  46. package/libusb/examples/fxload.c +31 -9
  47. package/libusb/examples/hotplugtest.c +35 -7
  48. package/libusb/examples/listdevs.c +3 -1
  49. package/libusb/examples/sam3u_benchmark.c +3 -3
  50. package/libusb/examples/testlibusb.c +277 -0
  51. package/libusb/examples/xusb.c +40 -34
  52. package/libusb/libusb/Makefile.am +49 -23
  53. package/libusb/libusb/core.c +855 -457
  54. package/libusb/libusb/descriptor.c +72 -78
  55. package/libusb/libusb/hotplug.c +122 -76
  56. package/libusb/libusb/hotplug.h +42 -25
  57. package/libusb/libusb/io.c +625 -390
  58. package/libusb/libusb/libusb-1.0.def +12 -0
  59. package/libusb/libusb/libusb.h +218 -150
  60. package/libusb/libusb/libusbi.h +346 -176
  61. package/libusb/libusb/os/darwin_usb.c +604 -319
  62. package/libusb/libusb/os/darwin_usb.h +61 -20
  63. package/libusb/libusb/os/haiku_pollfs.cpp +367 -0
  64. package/libusb/libusb/os/haiku_usb.h +113 -0
  65. package/libusb/libusb/os/haiku_usb_backend.cpp +533 -0
  66. package/libusb/libusb/os/haiku_usb_raw.cpp +267 -0
  67. package/libusb/libusb/os/haiku_usb_raw.h +188 -0
  68. package/libusb/libusb/os/linux_netlink.c +186 -146
  69. package/libusb/libusb/os/linux_udev.c +36 -14
  70. package/libusb/libusb/os/linux_usbfs.c +426 -225
  71. package/libusb/libusb/os/linux_usbfs.h +5 -3
  72. package/libusb/libusb/os/netbsd_usb.c +21 -77
  73. package/libusb/libusb/os/openbsd_usb.c +32 -115
  74. package/libusb/libusb/os/poll_posix.c +38 -5
  75. package/libusb/libusb/os/poll_posix.h +3 -0
  76. package/libusb/libusb/os/poll_windows.c +277 -626
  77. package/libusb/libusb/os/poll_windows.h +11 -44
  78. package/libusb/libusb/os/sunos_usb.c +1695 -0
  79. package/libusb/libusb/os/sunos_usb.h +80 -0
  80. package/libusb/libusb/os/threads_posix.c +24 -26
  81. package/libusb/libusb/os/threads_posix.h +73 -21
  82. package/libusb/libusb/os/threads_windows.c +71 -157
  83. package/libusb/libusb/os/threads_windows.h +68 -44
  84. package/libusb/libusb/os/wince_usb.c +276 -420
  85. package/libusb/libusb/os/wince_usb.h +23 -28
  86. package/libusb/libusb/os/windows_common.h +78 -58
  87. package/libusb/libusb/os/windows_nt_common.c +1010 -0
  88. package/libusb/libusb/os/windows_nt_common.h +110 -0
  89. package/libusb/libusb/os/windows_nt_shared_types.h +147 -0
  90. package/libusb/libusb/os/windows_usbdk.c +830 -0
  91. package/libusb/libusb/os/windows_usbdk.h +103 -0
  92. package/libusb/libusb/os/windows_winusb.c +4391 -0
  93. package/libusb/libusb/os/windows_winusb.h +783 -0
  94. package/libusb/libusb/strerror.c +41 -7
  95. package/libusb/libusb/sync.c +41 -13
  96. package/libusb/libusb/version.h +1 -1
  97. package/libusb/libusb/version_nano.h +1 -1
  98. package/libusb/libusb-1.0.pc.in +1 -1
  99. package/libusb/msvc/appveyor.bat +27 -0
  100. package/libusb/msvc/config.h +5 -4
  101. package/libusb/msvc/ddk_build.cmd +87 -43
  102. package/libusb/msvc/fxload_2010.vcxproj +24 -104
  103. package/libusb/msvc/fxload_2012.vcxproj +24 -107
  104. package/libusb/msvc/fxload_2013.vcxproj +24 -107
  105. package/libusb/msvc/fxload_2015.vcxproj +91 -0
  106. package/libusb/msvc/fxload_2017.vcxproj +114 -0
  107. package/libusb/msvc/fxload_sources +1 -1
  108. package/libusb/msvc/getopt_2010.vcxproj +16 -75
  109. package/libusb/msvc/getopt_2012.vcxproj +16 -79
  110. package/libusb/msvc/getopt_2013.vcxproj +16 -79
  111. package/libusb/msvc/getopt_2015.vcxproj +73 -0
  112. package/libusb/msvc/getopt_2017.vcxproj +98 -0
  113. package/libusb/msvc/getopt_sources +6 -2
  114. package/libusb/msvc/hotplugtest_2010.vcxproj +18 -99
  115. package/libusb/msvc/hotplugtest_2012.vcxproj +18 -102
  116. package/libusb/msvc/hotplugtest_2013.vcxproj +18 -102
  117. package/libusb/msvc/hotplugtest_2015.vcxproj +83 -0
  118. package/libusb/msvc/hotplugtest_2017.vcxproj +106 -0
  119. package/libusb/msvc/hotplugtest_sources +1 -1
  120. package/libusb/msvc/libusb_2005.sln +20 -20
  121. package/libusb/msvc/libusb_2010.sln +57 -46
  122. package/libusb/msvc/libusb_2012.sln +57 -46
  123. package/libusb/msvc/libusb_2013.sln +57 -50
  124. package/libusb/msvc/libusb_2015.sln +59 -52
  125. package/libusb/msvc/libusb_2017.sln +186 -0
  126. package/libusb/msvc/libusb_dll.dsp +2 -2
  127. package/libusb/msvc/libusb_dll_2005.vcproj +30 -2
  128. package/libusb/msvc/libusb_dll_2010.vcxproj +26 -90
  129. package/libusb/msvc/libusb_dll_2012.vcxproj +28 -96
  130. package/libusb/msvc/libusb_dll_2013.vcxproj +28 -96
  131. package/libusb/msvc/libusb_dll_2015.vcxproj +107 -0
  132. package/libusb/msvc/libusb_dll_2017.vcxproj +134 -0
  133. package/libusb/msvc/libusb_dll_wince.vcproj +9 -1
  134. package/libusb/msvc/libusb_sources +10 -5
  135. package/libusb/msvc/libusb_static.dsp +2 -2
  136. package/libusb/msvc/libusb_static_2005.vcproj +32 -4
  137. package/libusb/msvc/libusb_static_2010.vcxproj +24 -83
  138. package/libusb/msvc/libusb_static_2012.vcxproj +25 -87
  139. package/libusb/msvc/libusb_static_2013.vcxproj +25 -87
  140. package/libusb/msvc/libusb_static_2015.vcxproj +98 -0
  141. package/libusb/msvc/libusb_static_2017.vcxproj +117 -0
  142. package/libusb/msvc/libusb_static_wince.vcproj +20 -26
  143. package/libusb/msvc/libusb_wince.sln +88 -88
  144. package/libusb/msvc/listdevs_2010.vcxproj +16 -99
  145. package/libusb/msvc/listdevs_2012.vcxproj +16 -102
  146. package/libusb/msvc/listdevs_2013.vcxproj +16 -102
  147. package/libusb/msvc/listdevs_2015.vcxproj +83 -0
  148. package/libusb/msvc/listdevs_2017.vcxproj +106 -0
  149. package/libusb/msvc/listdevs_sources +2 -1
  150. package/libusb/msvc/stress_2010.vcxproj +20 -101
  151. package/libusb/msvc/stress_2012.vcxproj +20 -104
  152. package/libusb/msvc/stress_2013.vcxproj +20 -104
  153. package/libusb/msvc/stress_2015.vcxproj +87 -0
  154. package/libusb/msvc/stress_2017.vcxproj +110 -0
  155. package/libusb/msvc/stress_sources +21 -0
  156. package/libusb/msvc/testlibusb_2010.vcxproj +82 -0
  157. package/libusb/msvc/testlibusb_2012.vcxproj +83 -0
  158. package/libusb/msvc/testlibusb_2013.vcxproj +83 -0
  159. package/libusb/msvc/testlibusb_2015.vcxproj +83 -0
  160. package/libusb/msvc/testlibusb_2017.vcxproj +106 -0
  161. package/libusb/msvc/testlibusb_sources +20 -0
  162. package/libusb/msvc/xusb_2010.vcxproj +17 -98
  163. package/libusb/msvc/xusb_2012.vcxproj +17 -101
  164. package/libusb/msvc/xusb_2013.vcxproj +17 -101
  165. package/libusb/msvc/xusb_2015.vcxproj +83 -0
  166. package/libusb/msvc/xusb_2017.vcxproj +106 -0
  167. package/libusb/msvc/xusb_sources +1 -1
  168. package/libusb/tests/stress.c +2 -2
  169. package/libusb/tests/testlib.c +0 -4
  170. package/libusb/travis-autogen.sh +39 -0
  171. package/libusb.gypi +13 -2
  172. package/package.json +23 -13
  173. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  174. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  175. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  176. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  177. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  178. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  179. package/prebuilds/linux-ia32/node.napi.node +0 -0
  180. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  181. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  182. package/prebuilds/win32-ia32/node.napi.node +0 -0
  183. package/prebuilds/win32-x64/node.napi.node +0 -0
  184. package/src/device.cc +7 -7
  185. package/src/node_usb.cc +43 -1
  186. package/src/node_usb.h +1 -0
  187. package/src/uv_async_queue.h +33 -25
  188. package/test/usb.coffee +12 -4
  189. package/tsc/index.ts +68 -8
  190. package/tsc/usb/bindings.ts +19 -1
  191. package/tsc/usb/index.ts +66 -68
  192. package/tsc/usb/interface.ts +2 -2
  193. package/tsc/webusb/index.ts +187 -75
  194. package/tsc/webusb/webusb-device.ts +28 -17
  195. package/.github/workflows/prebuild.yml +0 -48
  196. package/Readme.md +0 -339
  197. package/dist/webusb/typed-event-target.d.ts +0 -11
  198. package/dist/webusb/typed-event-target.js +0 -21
  199. package/dist/webusb/typed-event-target.js.map +0 -1
  200. package/docs/assets/css/main.css +0 -2660
  201. package/docs/assets/images/icons.png +0 -0
  202. package/docs/assets/images/icons@2x.png +0 -0
  203. package/docs/assets/images/widgets.png +0 -0
  204. package/docs/assets/images/widgets@2x.png +0 -0
  205. package/docs/assets/js/main.js +0 -248
  206. package/docs/assets/js/search.js +0 -1
  207. package/docs/classes/usb_bindings.device.html +0 -1338
  208. package/docs/classes/usb_bindings.libusbexception.html +0 -234
  209. package/docs/classes/usb_bindings.transfer.html +0 -344
  210. package/docs/classes/usb_capability.capability.html +0 -297
  211. package/docs/classes/usb_device.extendeddevice.html +0 -766
  212. package/docs/classes/usb_endpoint.endpoint.html +0 -472
  213. package/docs/classes/usb_endpoint.inendpoint.html +0 -766
  214. package/docs/classes/usb_endpoint.outendpoint.html +0 -582
  215. package/docs/classes/usb_interface.interface.html +0 -648
  216. package/docs/classes/webusb.webusb-1.html +0 -615
  217. package/docs/classes/webusb_mutex.mutex.html +0 -270
  218. package/docs/classes/webusb_typed_event_target.typedeventtarget.html +0 -443
  219. package/docs/classes/webusb_webusb_device.webusbdevice.html +0 -904
  220. package/docs/index.html +0 -500
  221. package/docs/interfaces/usb.deviceevents.html +0 -242
  222. package/docs/interfaces/usb_descriptors.bosdescriptor.html +0 -293
  223. package/docs/interfaces/usb_descriptors.capabilitydescriptor.html +0 -274
  224. package/docs/interfaces/usb_descriptors.configdescriptor.html +0 -388
  225. package/docs/interfaces/usb_descriptors.devicedescriptor.html +0 -464
  226. package/docs/interfaces/usb_descriptors.endpointdescriptor.html +0 -370
  227. package/docs/interfaces/usb_descriptors.interfacedescriptor.html +0 -407
  228. package/docs/interfaces/webusb.usboptions.html +0 -226
  229. package/docs/modules/index.html +0 -173
  230. package/docs/modules/usb.html +0 -173
  231. package/docs/modules/usb_bindings.html +0 -2319
  232. package/docs/modules/usb_capability.html +0 -156
  233. package/docs/modules/usb_descriptors.html +0 -176
  234. package/docs/modules/usb_device.html +0 -156
  235. package/docs/modules/usb_endpoint.html +0 -164
  236. package/docs/modules/usb_interface.html +0 -156
  237. package/docs/modules/webusb.html +0 -165
  238. package/docs/modules/webusb_mutex.html +0 -156
  239. package/docs/modules/webusb_typed_event_target.html +0 -156
  240. package/docs/modules/webusb_webusb_device.html +0 -156
  241. package/docs/modules.html +0 -156
  242. package/libusb/INSTALL +0 -234
  243. package/libusb/README +0 -28
  244. package/libusb/libusb/os/windows_usb.c +0 -5347
  245. package/libusb/libusb/os/windows_usb.h +0 -971
  246. package/libusb/msvc/fxload_2010.vcxproj.filters +0 -25
  247. package/libusb/msvc/fxload_2012.vcxproj.filters +0 -25
  248. package/libusb/msvc/getopt_2010.vcxproj.filters +0 -26
  249. package/libusb/msvc/getopt_2012.vcxproj.filters +0 -26
  250. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +0 -14
  251. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +0 -14
  252. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +0 -81
  253. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +0 -84
  254. package/libusb/msvc/libusb_static_2010.vcxproj.filters +0 -74
  255. package/libusb/msvc/libusb_static_2012.vcxproj.filters +0 -74
  256. package/libusb/msvc/listdevs_2010.vcxproj.filters +0 -14
  257. package/libusb/msvc/listdevs_2012.vcxproj.filters +0 -14
  258. package/libusb/msvc/stress_2010.vcxproj.filters +0 -25
  259. package/libusb/msvc/stress_2012.vcxproj.filters +0 -25
  260. package/libusb/msvc/xusb_2010.vcxproj.filters +0 -14
  261. package/libusb/msvc/xusb_2012.vcxproj.filters +0 -14
  262. package/tsc/webusb/typed-event-target.ts +0 -23
@@ -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
+ }