usb 1.7.2-prebuild

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 (183) hide show
  1. package/.github/workflows/prebuild.yml +62 -0
  2. package/.gitmodules +3 -0
  3. package/LICENSE +7 -0
  4. package/Readme.md +339 -0
  5. package/binding.gyp +90 -0
  6. package/libusb/.gitattributes +11 -0
  7. package/libusb/.private/README.txt +5 -0
  8. package/libusb/.private/bd.cmd +89 -0
  9. package/libusb/.private/bm.sh +54 -0
  10. package/libusb/.private/bwince.cmd +57 -0
  11. package/libusb/.private/post-rewrite.sh +28 -0
  12. package/libusb/.private/pre-commit.sh +42 -0
  13. package/libusb/.private/wbs.txt +61 -0
  14. package/libusb/.private/wbs_wince.txt +42 -0
  15. package/libusb/AUTHORS +78 -0
  16. package/libusb/COPYING +504 -0
  17. package/libusb/ChangeLog +211 -0
  18. package/libusb/INSTALL +234 -0
  19. package/libusb/INSTALL_WIN.txt +73 -0
  20. package/libusb/Makefile.am +28 -0
  21. package/libusb/NEWS +2 -0
  22. package/libusb/PORTING +94 -0
  23. package/libusb/README +28 -0
  24. package/libusb/README.git +41 -0
  25. package/libusb/TODO +2 -0
  26. package/libusb/Xcode/common.xcconfig +49 -0
  27. package/libusb/Xcode/debug.xcconfig +29 -0
  28. package/libusb/Xcode/libusb.xcconfig +21 -0
  29. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +1 -0
  30. package/libusb/Xcode/libusb_debug.xcconfig +21 -0
  31. package/libusb/Xcode/libusb_release.xcconfig +21 -0
  32. package/libusb/Xcode/release.xcconfig +30 -0
  33. package/libusb/android/README +114 -0
  34. package/libusb/android/jni/Android.mk +23 -0
  35. package/libusb/android/jni/Application.mk +24 -0
  36. package/libusb/android/jni/examples.mk +134 -0
  37. package/libusb/android/jni/libusb.mk +54 -0
  38. package/libusb/android/jni/tests.mk +56 -0
  39. package/libusb/autogen.sh +8 -0
  40. package/libusb/bootstrap.sh +19 -0
  41. package/libusb/configure.ac +304 -0
  42. package/libusb/doc/Makefile.am +9 -0
  43. package/libusb/doc/doxygen.cfg.in +1288 -0
  44. package/libusb/doc/libusb.png +0 -0
  45. package/libusb/examples/Makefile.am +19 -0
  46. package/libusb/examples/dpfp.c +506 -0
  47. package/libusb/examples/dpfp_threaded.c +544 -0
  48. package/libusb/examples/ezusb.c +831 -0
  49. package/libusb/examples/ezusb.h +120 -0
  50. package/libusb/examples/fxload.c +287 -0
  51. package/libusb/examples/getopt/getopt.c +1060 -0
  52. package/libusb/examples/getopt/getopt.h +180 -0
  53. package/libusb/examples/getopt/getopt1.c +188 -0
  54. package/libusb/examples/hotplugtest.c +104 -0
  55. package/libusb/examples/listdevs.c +71 -0
  56. package/libusb/examples/sam3u_benchmark.c +193 -0
  57. package/libusb/examples/xusb.c +1129 -0
  58. package/libusb/libusb/Makefile.am +75 -0
  59. package/libusb/libusb/core.c +2342 -0
  60. package/libusb/libusb/descriptor.c +1199 -0
  61. package/libusb/libusb/hotplug.c +327 -0
  62. package/libusb/libusb/hotplug.h +82 -0
  63. package/libusb/libusb/io.c +2631 -0
  64. package/libusb/libusb/libusb-1.0.def +166 -0
  65. package/libusb/libusb/libusb-1.0.rc +61 -0
  66. package/libusb/libusb/libusb.h +1998 -0
  67. package/libusb/libusb/libusbi.h +1040 -0
  68. package/libusb/libusb/os/darwin_usb.c +2009 -0
  69. package/libusb/libusb/os/darwin_usb.h +162 -0
  70. package/libusb/libusb/os/linux_netlink.c +369 -0
  71. package/libusb/libusb/os/linux_udev.c +307 -0
  72. package/libusb/libusb/os/linux_usbfs.c +2695 -0
  73. package/libusb/libusb/os/linux_usbfs.h +192 -0
  74. package/libusb/libusb/os/netbsd_usb.c +738 -0
  75. package/libusb/libusb/os/openbsd_usb.c +832 -0
  76. package/libusb/libusb/os/poll_posix.c +51 -0
  77. package/libusb/libusb/os/poll_posix.h +11 -0
  78. package/libusb/libusb/os/poll_windows.c +796 -0
  79. package/libusb/libusb/os/poll_windows.h +131 -0
  80. package/libusb/libusb/os/threads_posix.c +82 -0
  81. package/libusb/libusb/os/threads_posix.h +50 -0
  82. package/libusb/libusb/os/threads_windows.c +212 -0
  83. package/libusb/libusb/os/threads_windows.h +87 -0
  84. package/libusb/libusb/os/wince_usb.c +1032 -0
  85. package/libusb/libusb/os/wince_usb.h +131 -0
  86. package/libusb/libusb/os/windows_common.h +108 -0
  87. package/libusb/libusb/os/windows_usb.c +5347 -0
  88. package/libusb/libusb/os/windows_usb.h +971 -0
  89. package/libusb/libusb/strerror.c +199 -0
  90. package/libusb/libusb/sync.c +307 -0
  91. package/libusb/libusb/version.h +18 -0
  92. package/libusb/libusb/version_nano.h +1 -0
  93. package/libusb/libusb-1.0.pc.in +11 -0
  94. package/libusb/msvc/config.h +50 -0
  95. package/libusb/msvc/ddk_build.cmd +175 -0
  96. package/libusb/msvc/errno.h +102 -0
  97. package/libusb/msvc/fxload_2010.vcxproj +170 -0
  98. package/libusb/msvc/fxload_2010.vcxproj.filters +25 -0
  99. package/libusb/msvc/fxload_2012.vcxproj +174 -0
  100. package/libusb/msvc/fxload_2012.vcxproj.filters +25 -0
  101. package/libusb/msvc/fxload_2013.vcxproj +174 -0
  102. package/libusb/msvc/fxload_sources +23 -0
  103. package/libusb/msvc/getopt_2005.vcproj +288 -0
  104. package/libusb/msvc/getopt_2010.vcxproj +131 -0
  105. package/libusb/msvc/getopt_2010.vcxproj.filters +26 -0
  106. package/libusb/msvc/getopt_2012.vcxproj +136 -0
  107. package/libusb/msvc/getopt_2012.vcxproj.filters +26 -0
  108. package/libusb/msvc/getopt_2013.vcxproj +136 -0
  109. package/libusb/msvc/getopt_sources +20 -0
  110. package/libusb/msvc/hotplugtest_2010.vcxproj +163 -0
  111. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +14 -0
  112. package/libusb/msvc/hotplugtest_2012.vcxproj +167 -0
  113. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +14 -0
  114. package/libusb/msvc/hotplugtest_2013.vcxproj +167 -0
  115. package/libusb/msvc/hotplugtest_sources +20 -0
  116. package/libusb/msvc/inttypes.h +295 -0
  117. package/libusb/msvc/libusb.dsw +71 -0
  118. package/libusb/msvc/libusb_2005.sln +95 -0
  119. package/libusb/msvc/libusb_2010.sln +94 -0
  120. package/libusb/msvc/libusb_2012.sln +94 -0
  121. package/libusb/msvc/libusb_2013.sln +100 -0
  122. package/libusb/msvc/libusb_2015.sln +100 -0
  123. package/libusb/msvc/libusb_dll.dsp +194 -0
  124. package/libusb/msvc/libusb_dll_2005.vcproj +436 -0
  125. package/libusb/msvc/libusb_dll_2010.vcxproj +170 -0
  126. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +81 -0
  127. package/libusb/msvc/libusb_dll_2012.vcxproj +175 -0
  128. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +84 -0
  129. package/libusb/msvc/libusb_dll_2013.vcxproj +175 -0
  130. package/libusb/msvc/libusb_dll_wince.vcproj +1243 -0
  131. package/libusb/msvc/libusb_sources +38 -0
  132. package/libusb/msvc/libusb_static.dsp +174 -0
  133. package/libusb/msvc/libusb_static_2005.vcproj +362 -0
  134. package/libusb/msvc/libusb_static_2010.vcxproj +156 -0
  135. package/libusb/msvc/libusb_static_2010.vcxproj.filters +74 -0
  136. package/libusb/msvc/libusb_static_2012.vcxproj +160 -0
  137. package/libusb/msvc/libusb_static_2012.vcxproj.filters +74 -0
  138. package/libusb/msvc/libusb_static_2013.vcxproj +160 -0
  139. package/libusb/msvc/libusb_static_wince.vcproj +1185 -0
  140. package/libusb/msvc/libusb_wince.sln +246 -0
  141. package/libusb/msvc/listdevs.dsp +103 -0
  142. package/libusb/msvc/listdevs_2005.vcproj +360 -0
  143. package/libusb/msvc/listdevs_2010.vcxproj +165 -0
  144. package/libusb/msvc/listdevs_2010.vcxproj.filters +14 -0
  145. package/libusb/msvc/listdevs_2012.vcxproj +169 -0
  146. package/libusb/msvc/listdevs_2012.vcxproj.filters +14 -0
  147. package/libusb/msvc/listdevs_2013.vcxproj +169 -0
  148. package/libusb/msvc/listdevs_sources +19 -0
  149. package/libusb/msvc/listdevs_wince.vcproj +1120 -0
  150. package/libusb/msvc/missing.c +80 -0
  151. package/libusb/msvc/missing.h +32 -0
  152. package/libusb/msvc/stdint.h +256 -0
  153. package/libusb/msvc/stress_2005.vcproj +390 -0
  154. package/libusb/msvc/stress_2010.vcxproj +167 -0
  155. package/libusb/msvc/stress_2010.vcxproj.filters +25 -0
  156. package/libusb/msvc/stress_2012.vcxproj +171 -0
  157. package/libusb/msvc/stress_2012.vcxproj.filters +25 -0
  158. package/libusb/msvc/stress_2013.vcxproj +171 -0
  159. package/libusb/msvc/stress_wince.vcproj +1128 -0
  160. package/libusb/msvc/xusb.dsp +102 -0
  161. package/libusb/msvc/xusb_2005.vcproj +344 -0
  162. package/libusb/msvc/xusb_2010.vcxproj +163 -0
  163. package/libusb/msvc/xusb_2010.vcxproj.filters +14 -0
  164. package/libusb/msvc/xusb_2012.vcxproj +167 -0
  165. package/libusb/msvc/xusb_2012.vcxproj.filters +14 -0
  166. package/libusb/msvc/xusb_2013.vcxproj +167 -0
  167. package/libusb/msvc/xusb_sources +20 -0
  168. package/libusb/msvc/xusb_wince.vcproj +1120 -0
  169. package/libusb/tests/Makefile.am +6 -0
  170. package/libusb/tests/libusb_testlib.h +107 -0
  171. package/libusb/tests/stress.c +160 -0
  172. package/libusb/tests/testlib.c +281 -0
  173. package/libusb.gypi +136 -0
  174. package/libusb_config/config.h +1 -0
  175. package/package.json +69 -0
  176. package/src/device.cc +412 -0
  177. package/src/helpers.h +64 -0
  178. package/src/node_usb.cc +319 -0
  179. package/src/node_usb.h +120 -0
  180. package/src/transfer.cc +148 -0
  181. package/src/uv_async_queue.h +33 -0
  182. package/test/usb.coffee +191 -0
  183. package/usb.js +524 -0
@@ -0,0 +1,832 @@
1
+ /*
2
+ * Copyright © 2011-2013 Martin Pieuchot <mpi@openbsd.org>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ */
18
+
19
+ #include <sys/time.h>
20
+ #include <sys/types.h>
21
+
22
+ #include <errno.h>
23
+ #include <fcntl.h>
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+ #include <string.h>
27
+ #include <unistd.h>
28
+
29
+ #include <dev/usb/usb.h>
30
+
31
+ #include "libusb.h"
32
+ #include "libusbi.h"
33
+
34
+ struct device_priv {
35
+ char *devname; /* name of the ugen(4) node */
36
+ int fd; /* device file descriptor */
37
+
38
+ unsigned char *cdesc; /* active config descriptor */
39
+ usb_device_descriptor_t ddesc; /* usb device descriptor */
40
+ };
41
+
42
+ struct handle_priv {
43
+ int pipe[2]; /* for event notification */
44
+ int endpoints[USB_MAX_ENDPOINTS];
45
+ };
46
+
47
+ /*
48
+ * Backend functions
49
+ */
50
+ static int obsd_get_device_list(struct libusb_context *,
51
+ struct discovered_devs **);
52
+ static int obsd_open(struct libusb_device_handle *);
53
+ static void obsd_close(struct libusb_device_handle *);
54
+
55
+ static int obsd_get_device_descriptor(struct libusb_device *, unsigned char *,
56
+ int *);
57
+ static int obsd_get_active_config_descriptor(struct libusb_device *,
58
+ unsigned char *, size_t, int *);
59
+ static int obsd_get_config_descriptor(struct libusb_device *, uint8_t,
60
+ unsigned char *, size_t, int *);
61
+
62
+ static int obsd_get_configuration(struct libusb_device_handle *, int *);
63
+ static int obsd_set_configuration(struct libusb_device_handle *, int);
64
+
65
+ static int obsd_claim_interface(struct libusb_device_handle *, int);
66
+ static int obsd_release_interface(struct libusb_device_handle *, int);
67
+
68
+ static int obsd_set_interface_altsetting(struct libusb_device_handle *, int,
69
+ int);
70
+ static int obsd_clear_halt(struct libusb_device_handle *, unsigned char);
71
+ static int obsd_reset_device(struct libusb_device_handle *);
72
+ static void obsd_destroy_device(struct libusb_device *);
73
+
74
+ static int obsd_submit_transfer(struct usbi_transfer *);
75
+ static int obsd_cancel_transfer(struct usbi_transfer *);
76
+ static void obsd_clear_transfer_priv(struct usbi_transfer *);
77
+ static int obsd_handle_events(struct libusb_context *ctx, struct pollfd *,
78
+ nfds_t, int);
79
+ static int obsd_clock_gettime(int, struct timespec *);
80
+
81
+ /*
82
+ * Private functions
83
+ */
84
+ static int _errno_to_libusb(int);
85
+ static int _cache_active_config_descriptor(struct libusb_device *);
86
+ static int _sync_control_transfer(struct usbi_transfer *);
87
+ static int _sync_gen_transfer(struct usbi_transfer *);
88
+ static int _access_endpoint(struct libusb_transfer *);
89
+
90
+ static int _bus_open(int);
91
+
92
+
93
+ const struct usbi_os_backend openbsd_backend = {
94
+ "Synchronous OpenBSD backend",
95
+ USBI_CAP_HAS_POLLABLE_DEVICE_FD,
96
+ NULL, /* init() */
97
+ NULL, /* exit() */
98
+ obsd_get_device_list,
99
+ NULL, /* hotplug_poll */
100
+ obsd_open,
101
+ obsd_close,
102
+
103
+ obsd_get_device_descriptor,
104
+ obsd_get_active_config_descriptor,
105
+ obsd_get_config_descriptor,
106
+ NULL, /* get_config_descriptor_by_value() */
107
+
108
+ obsd_get_configuration,
109
+ obsd_set_configuration,
110
+
111
+ obsd_claim_interface,
112
+ obsd_release_interface,
113
+
114
+ obsd_set_interface_altsetting,
115
+ obsd_clear_halt,
116
+ obsd_reset_device,
117
+
118
+ NULL, /* alloc_streams */
119
+ NULL, /* free_streams */
120
+
121
+ NULL, /* kernel_driver_active() */
122
+ NULL, /* detach_kernel_driver() */
123
+ NULL, /* attach_kernel_driver() */
124
+
125
+ obsd_destroy_device,
126
+
127
+ obsd_submit_transfer,
128
+ obsd_cancel_transfer,
129
+ obsd_clear_transfer_priv,
130
+
131
+ obsd_handle_events,
132
+
133
+ obsd_clock_gettime,
134
+ sizeof(struct device_priv),
135
+ sizeof(struct handle_priv),
136
+ 0, /* transfer_priv_size */
137
+ 0, /* add_iso_packet_size */
138
+ };
139
+
140
+ #define DEVPATH "/dev/"
141
+ #define USBDEV DEVPATH "usb"
142
+
143
+ int
144
+ obsd_get_device_list(struct libusb_context * ctx,
145
+ struct discovered_devs **discdevs)
146
+ {
147
+ struct discovered_devs *ddd;
148
+ struct libusb_device *dev;
149
+ struct device_priv *dpriv;
150
+ struct usb_device_info di;
151
+ struct usb_device_ddesc dd;
152
+ unsigned long session_id;
153
+ char devices[USB_MAX_DEVICES];
154
+ char busnode[16];
155
+ char *udevname;
156
+ int fd, addr, i, j;
157
+
158
+ usbi_dbg("");
159
+
160
+ for (i = 0; i < 8; i++) {
161
+ snprintf(busnode, sizeof(busnode), USBDEV "%d", i);
162
+
163
+ if ((fd = open(busnode, O_RDWR)) < 0) {
164
+ if (errno != ENOENT && errno != ENXIO)
165
+ usbi_err(ctx, "could not open %s", busnode);
166
+ continue;
167
+ }
168
+
169
+ bzero(devices, sizeof(devices));
170
+ for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
171
+ if (devices[addr])
172
+ continue;
173
+
174
+ di.udi_addr = addr;
175
+ if (ioctl(fd, USB_DEVICEINFO, &di) < 0)
176
+ continue;
177
+
178
+ /*
179
+ * XXX If ugen(4) is attached to the USB device
180
+ * it will be used.
181
+ */
182
+ udevname = NULL;
183
+ for (j = 0; j < USB_MAX_DEVNAMES; j++)
184
+ if (!strncmp("ugen", di.udi_devnames[j], 4)) {
185
+ udevname = strdup(di.udi_devnames[j]);
186
+ break;
187
+ }
188
+
189
+ session_id = (di.udi_bus << 8 | di.udi_addr);
190
+ dev = usbi_get_device_by_session_id(ctx, session_id);
191
+
192
+ if (dev == NULL) {
193
+ dev = usbi_alloc_device(ctx, session_id);
194
+ if (dev == NULL) {
195
+ close(fd);
196
+ return (LIBUSB_ERROR_NO_MEM);
197
+ }
198
+
199
+ dev->bus_number = di.udi_bus;
200
+ dev->device_address = di.udi_addr;
201
+ dev->speed = di.udi_speed;
202
+
203
+ dpriv = (struct device_priv *)dev->os_priv;
204
+ dpriv->fd = -1;
205
+ dpriv->cdesc = NULL;
206
+ dpriv->devname = udevname;
207
+
208
+ dd.udd_bus = di.udi_bus;
209
+ dd.udd_addr = di.udi_addr;
210
+ if (ioctl(fd, USB_DEVICE_GET_DDESC, &dd) < 0) {
211
+ libusb_unref_device(dev);
212
+ continue;
213
+ }
214
+ dpriv->ddesc = dd.udd_desc;
215
+
216
+ if (_cache_active_config_descriptor(dev)) {
217
+ libusb_unref_device(dev);
218
+ continue;
219
+ }
220
+
221
+ if (usbi_sanitize_device(dev)) {
222
+ libusb_unref_device(dev);
223
+ continue;
224
+ }
225
+ }
226
+
227
+ ddd = discovered_devs_append(*discdevs, dev);
228
+ if (ddd == NULL) {
229
+ close(fd);
230
+ return (LIBUSB_ERROR_NO_MEM);
231
+ }
232
+ libusb_unref_device(dev);
233
+
234
+ *discdevs = ddd;
235
+ devices[addr] = 1;
236
+ }
237
+
238
+ close(fd);
239
+ }
240
+
241
+ return (LIBUSB_SUCCESS);
242
+ }
243
+
244
+ int
245
+ obsd_open(struct libusb_device_handle *handle)
246
+ {
247
+ struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
248
+ struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
249
+ char devnode[16];
250
+
251
+ if (dpriv->devname) {
252
+ /*
253
+ * Only open ugen(4) attached devices read-write, all
254
+ * read-only operations are done through the bus node.
255
+ */
256
+ snprintf(devnode, sizeof(devnode), DEVPATH "%s.00",
257
+ dpriv->devname);
258
+ dpriv->fd = open(devnode, O_RDWR);
259
+ if (dpriv->fd < 0)
260
+ return _errno_to_libusb(errno);
261
+
262
+ usbi_dbg("open %s: fd %d", devnode, dpriv->fd);
263
+ }
264
+
265
+ if (pipe(hpriv->pipe) < 0)
266
+ return _errno_to_libusb(errno);
267
+
268
+ return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
269
+ }
270
+
271
+ void
272
+ obsd_close(struct libusb_device_handle *handle)
273
+ {
274
+ struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
275
+ struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
276
+
277
+ if (dpriv->devname) {
278
+ usbi_dbg("close: fd %d", dpriv->fd);
279
+
280
+ close(dpriv->fd);
281
+ dpriv->fd = -1;
282
+ }
283
+
284
+ usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
285
+
286
+ close(hpriv->pipe[0]);
287
+ close(hpriv->pipe[1]);
288
+ }
289
+
290
+ int
291
+ obsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
292
+ int *host_endian)
293
+ {
294
+ struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
295
+
296
+ usbi_dbg("");
297
+
298
+ memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
299
+
300
+ *host_endian = 0;
301
+
302
+ return (LIBUSB_SUCCESS);
303
+ }
304
+
305
+ int
306
+ obsd_get_active_config_descriptor(struct libusb_device *dev,
307
+ unsigned char *buf, size_t len, int *host_endian)
308
+ {
309
+ struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
310
+ usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
311
+
312
+ len = MIN(len, UGETW(ucd->wTotalLength));
313
+
314
+ usbi_dbg("len %d", len);
315
+
316
+ memcpy(buf, dpriv->cdesc, len);
317
+
318
+ *host_endian = 0;
319
+
320
+ return (len);
321
+ }
322
+
323
+ int
324
+ obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
325
+ unsigned char *buf, size_t len, int *host_endian)
326
+ {
327
+ struct usb_device_fdesc udf;
328
+ int fd, err;
329
+
330
+ if ((fd = _bus_open(dev->bus_number)) < 0)
331
+ return _errno_to_libusb(errno);
332
+
333
+ udf.udf_bus = dev->bus_number;
334
+ udf.udf_addr = dev->device_address;
335
+ udf.udf_config_index = idx;
336
+ udf.udf_size = len;
337
+ udf.udf_data = buf;
338
+
339
+ usbi_dbg("index %d, len %d", udf.udf_config_index, len);
340
+
341
+ if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
342
+ err = errno;
343
+ close(fd);
344
+ return _errno_to_libusb(err);
345
+ }
346
+ close(fd);
347
+
348
+ *host_endian = 0;
349
+
350
+ return (len);
351
+ }
352
+
353
+ int
354
+ obsd_get_configuration(struct libusb_device_handle *handle, int *config)
355
+ {
356
+ struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
357
+ usb_config_descriptor_t *ucd = (usb_config_descriptor_t *)dpriv->cdesc;
358
+
359
+ *config = ucd->bConfigurationValue;
360
+
361
+ usbi_dbg("bConfigurationValue %d", *config);
362
+
363
+ return (LIBUSB_SUCCESS);
364
+ }
365
+
366
+ int
367
+ obsd_set_configuration(struct libusb_device_handle *handle, int config)
368
+ {
369
+ struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
370
+
371
+ if (dpriv->devname == NULL)
372
+ return (LIBUSB_ERROR_NOT_SUPPORTED);
373
+
374
+ usbi_dbg("bConfigurationValue %d", config);
375
+
376
+ if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
377
+ return _errno_to_libusb(errno);
378
+
379
+ return _cache_active_config_descriptor(handle->dev);
380
+ }
381
+
382
+ int
383
+ obsd_claim_interface(struct libusb_device_handle *handle, int iface)
384
+ {
385
+ struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
386
+ int i;
387
+
388
+ for (i = 0; i < USB_MAX_ENDPOINTS; i++)
389
+ hpriv->endpoints[i] = -1;
390
+
391
+ return (LIBUSB_SUCCESS);
392
+ }
393
+
394
+ int
395
+ obsd_release_interface(struct libusb_device_handle *handle, int iface)
396
+ {
397
+ struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
398
+ int i;
399
+
400
+ for (i = 0; i < USB_MAX_ENDPOINTS; i++)
401
+ if (hpriv->endpoints[i] >= 0)
402
+ close(hpriv->endpoints[i]);
403
+
404
+ return (LIBUSB_SUCCESS);
405
+ }
406
+
407
+ int
408
+ obsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
409
+ int altsetting)
410
+ {
411
+ struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
412
+ struct usb_alt_interface intf;
413
+
414
+ if (dpriv->devname == NULL)
415
+ return (LIBUSB_ERROR_NOT_SUPPORTED);
416
+
417
+ usbi_dbg("iface %d, setting %d", iface, altsetting);
418
+
419
+ memset(&intf, 0, sizeof(intf));
420
+
421
+ intf.uai_interface_index = iface;
422
+ intf.uai_alt_no = altsetting;
423
+
424
+ if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
425
+ return _errno_to_libusb(errno);
426
+
427
+ return (LIBUSB_SUCCESS);
428
+ }
429
+
430
+ int
431
+ obsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
432
+ {
433
+ struct usb_ctl_request req;
434
+ int fd, err;
435
+
436
+ if ((fd = _bus_open(handle->dev->bus_number)) < 0)
437
+ return _errno_to_libusb(errno);
438
+
439
+ usbi_dbg("");
440
+
441
+ req.ucr_addr = handle->dev->device_address;
442
+ req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
443
+ req.ucr_request.bRequest = UR_CLEAR_FEATURE;
444
+ USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
445
+ USETW(req.ucr_request.wIndex, endpoint);
446
+ USETW(req.ucr_request.wLength, 0);
447
+
448
+ if (ioctl(fd, USB_REQUEST, &req) < 0) {
449
+ err = errno;
450
+ close(fd);
451
+ return _errno_to_libusb(err);
452
+ }
453
+ close(fd);
454
+
455
+ return (LIBUSB_SUCCESS);
456
+ }
457
+
458
+ int
459
+ obsd_reset_device(struct libusb_device_handle *handle)
460
+ {
461
+ usbi_dbg("");
462
+
463
+ return (LIBUSB_ERROR_NOT_SUPPORTED);
464
+ }
465
+
466
+ void
467
+ obsd_destroy_device(struct libusb_device *dev)
468
+ {
469
+ struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
470
+
471
+ usbi_dbg("");
472
+
473
+ free(dpriv->cdesc);
474
+ free(dpriv->devname);
475
+ }
476
+
477
+ int
478
+ obsd_submit_transfer(struct usbi_transfer *itransfer)
479
+ {
480
+ struct libusb_transfer *transfer;
481
+ struct handle_priv *hpriv;
482
+ int err = 0;
483
+
484
+ usbi_dbg("");
485
+
486
+ transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
487
+ hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
488
+
489
+ switch (transfer->type) {
490
+ case LIBUSB_TRANSFER_TYPE_CONTROL:
491
+ err = _sync_control_transfer(itransfer);
492
+ break;
493
+ case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
494
+ if (IS_XFEROUT(transfer)) {
495
+ /* Isochronous write is not supported */
496
+ err = LIBUSB_ERROR_NOT_SUPPORTED;
497
+ break;
498
+ }
499
+ err = _sync_gen_transfer(itransfer);
500
+ break;
501
+ case LIBUSB_TRANSFER_TYPE_BULK:
502
+ case LIBUSB_TRANSFER_TYPE_INTERRUPT:
503
+ if (IS_XFEROUT(transfer) &&
504
+ transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
505
+ err = LIBUSB_ERROR_NOT_SUPPORTED;
506
+ break;
507
+ }
508
+ err = _sync_gen_transfer(itransfer);
509
+ break;
510
+ case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
511
+ err = LIBUSB_ERROR_NOT_SUPPORTED;
512
+ break;
513
+ }
514
+
515
+ if (err)
516
+ return (err);
517
+
518
+ if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
519
+ return _errno_to_libusb(errno);
520
+
521
+ return (LIBUSB_SUCCESS);
522
+ }
523
+
524
+ int
525
+ obsd_cancel_transfer(struct usbi_transfer *itransfer)
526
+ {
527
+ usbi_dbg("");
528
+
529
+ return (LIBUSB_ERROR_NOT_SUPPORTED);
530
+ }
531
+
532
+ void
533
+ obsd_clear_transfer_priv(struct usbi_transfer *itransfer)
534
+ {
535
+ usbi_dbg("");
536
+
537
+ /* Nothing to do */
538
+ }
539
+
540
+ int
541
+ obsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
542
+ int num_ready)
543
+ {
544
+ struct libusb_device_handle *handle;
545
+ struct handle_priv *hpriv = NULL;
546
+ struct usbi_transfer *itransfer;
547
+ struct pollfd *pollfd;
548
+ int i, err = 0;
549
+
550
+ usbi_dbg("");
551
+
552
+ pthread_mutex_lock(&ctx->open_devs_lock);
553
+ for (i = 0; i < nfds && num_ready > 0; i++) {
554
+ pollfd = &fds[i];
555
+
556
+ if (!pollfd->revents)
557
+ continue;
558
+
559
+ hpriv = NULL;
560
+ num_ready--;
561
+ list_for_each_entry(handle, &ctx->open_devs, list,
562
+ struct libusb_device_handle) {
563
+ hpriv = (struct handle_priv *)handle->os_priv;
564
+
565
+ if (hpriv->pipe[0] == pollfd->fd)
566
+ break;
567
+
568
+ hpriv = NULL;
569
+ }
570
+
571
+ if (NULL == hpriv) {
572
+ usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
573
+ err = ENOENT;
574
+ break;
575
+ }
576
+
577
+ if (pollfd->revents & POLLERR) {
578
+ usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
579
+ usbi_handle_disconnect(handle);
580
+ continue;
581
+ }
582
+
583
+ if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
584
+ err = errno;
585
+ break;
586
+ }
587
+
588
+ if ((err = usbi_handle_transfer_completion(itransfer,
589
+ LIBUSB_TRANSFER_COMPLETED)))
590
+ break;
591
+ }
592
+ pthread_mutex_unlock(&ctx->open_devs_lock);
593
+
594
+ if (err)
595
+ return _errno_to_libusb(err);
596
+
597
+ return (LIBUSB_SUCCESS);
598
+ }
599
+
600
+ int
601
+ obsd_clock_gettime(int clkid, struct timespec *tp)
602
+ {
603
+ usbi_dbg("clock %d", clkid);
604
+
605
+ if (clkid == USBI_CLOCK_REALTIME)
606
+ return clock_gettime(CLOCK_REALTIME, tp);
607
+
608
+ if (clkid == USBI_CLOCK_MONOTONIC)
609
+ return clock_gettime(CLOCK_MONOTONIC, tp);
610
+
611
+ return (LIBUSB_ERROR_INVALID_PARAM);
612
+ }
613
+
614
+ int
615
+ _errno_to_libusb(int err)
616
+ {
617
+ usbi_dbg("error: %s (%d)", strerror(err), err);
618
+
619
+ switch (err) {
620
+ case EIO:
621
+ return (LIBUSB_ERROR_IO);
622
+ case EACCES:
623
+ return (LIBUSB_ERROR_ACCESS);
624
+ case ENOENT:
625
+ return (LIBUSB_ERROR_NO_DEVICE);
626
+ case ENOMEM:
627
+ return (LIBUSB_ERROR_NO_MEM);
628
+ case ETIMEDOUT:
629
+ return (LIBUSB_ERROR_TIMEOUT);
630
+ }
631
+
632
+ return (LIBUSB_ERROR_OTHER);
633
+ }
634
+
635
+ int
636
+ _cache_active_config_descriptor(struct libusb_device *dev)
637
+ {
638
+ struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
639
+ struct usb_device_cdesc udc;
640
+ struct usb_device_fdesc udf;
641
+ unsigned char* buf;
642
+ int fd, len, err;
643
+
644
+ if ((fd = _bus_open(dev->bus_number)) < 0)
645
+ return _errno_to_libusb(errno);
646
+
647
+ usbi_dbg("fd %d, addr %d", fd, dev->device_address);
648
+
649
+ udc.udc_bus = dev->bus_number;
650
+ udc.udc_addr = dev->device_address;
651
+ udc.udc_config_index = USB_CURRENT_CONFIG_INDEX;
652
+ if (ioctl(fd, USB_DEVICE_GET_CDESC, &udc) < 0) {
653
+ err = errno;
654
+ close(fd);
655
+ return _errno_to_libusb(errno);
656
+ }
657
+
658
+ usbi_dbg("active bLength %d", udc.udc_desc.bLength);
659
+
660
+ len = UGETW(udc.udc_desc.wTotalLength);
661
+ buf = malloc(len);
662
+ if (buf == NULL)
663
+ return (LIBUSB_ERROR_NO_MEM);
664
+
665
+ udf.udf_bus = dev->bus_number;
666
+ udf.udf_addr = dev->device_address;
667
+ udf.udf_config_index = udc.udc_config_index;
668
+ udf.udf_size = len;
669
+ udf.udf_data = buf;
670
+
671
+ usbi_dbg("index %d, len %d", udf.udf_config_index, len);
672
+
673
+ if (ioctl(fd, USB_DEVICE_GET_FDESC, &udf) < 0) {
674
+ err = errno;
675
+ close(fd);
676
+ free(buf);
677
+ return _errno_to_libusb(err);
678
+ }
679
+ close(fd);
680
+
681
+ if (dpriv->cdesc)
682
+ free(dpriv->cdesc);
683
+ dpriv->cdesc = buf;
684
+
685
+ return (LIBUSB_SUCCESS);
686
+ }
687
+
688
+ int
689
+ _sync_control_transfer(struct usbi_transfer *itransfer)
690
+ {
691
+ struct libusb_transfer *transfer;
692
+ struct libusb_control_setup *setup;
693
+ struct device_priv *dpriv;
694
+ struct usb_ctl_request req;
695
+
696
+ transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
697
+ dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
698
+ setup = (struct libusb_control_setup *)transfer->buffer;
699
+
700
+ usbi_dbg("type %x request %x value %x index %d length %d timeout %d",
701
+ setup->bmRequestType, setup->bRequest,
702
+ libusb_le16_to_cpu(setup->wValue),
703
+ libusb_le16_to_cpu(setup->wIndex),
704
+ libusb_le16_to_cpu(setup->wLength), transfer->timeout);
705
+
706
+ req.ucr_addr = transfer->dev_handle->dev->device_address;
707
+ req.ucr_request.bmRequestType = setup->bmRequestType;
708
+ req.ucr_request.bRequest = setup->bRequest;
709
+ /* Don't use USETW, libusb already deals with the endianness */
710
+ (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
711
+ (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
712
+ (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
713
+ req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
714
+
715
+ if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
716
+ req.ucr_flags = USBD_SHORT_XFER_OK;
717
+
718
+ if (dpriv->devname == NULL) {
719
+ /*
720
+ * XXX If the device is not attached to ugen(4) it is
721
+ * XXX still possible to submit a control transfer but
722
+ * XXX with the default timeout only.
723
+ */
724
+ int fd, err;
725
+
726
+ if ((fd = _bus_open(transfer->dev_handle->dev->bus_number)) < 0)
727
+ return _errno_to_libusb(errno);
728
+
729
+ if ((ioctl(fd, USB_REQUEST, &req)) < 0) {
730
+ err = errno;
731
+ close(fd);
732
+ return _errno_to_libusb(err);
733
+ }
734
+ close(fd);
735
+ } else {
736
+ if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
737
+ return _errno_to_libusb(errno);
738
+
739
+ if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
740
+ return _errno_to_libusb(errno);
741
+ }
742
+
743
+ itransfer->transferred = req.ucr_actlen;
744
+
745
+ usbi_dbg("transferred %d", itransfer->transferred);
746
+
747
+ return (0);
748
+ }
749
+
750
+ int
751
+ _access_endpoint(struct libusb_transfer *transfer)
752
+ {
753
+ struct handle_priv *hpriv;
754
+ struct device_priv *dpriv;
755
+ char devnode[16];
756
+ int fd, endpt;
757
+ mode_t mode;
758
+
759
+ hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
760
+ dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
761
+
762
+ endpt = UE_GET_ADDR(transfer->endpoint);
763
+ mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
764
+
765
+ usbi_dbg("endpoint %d mode %d", endpt, mode);
766
+
767
+ if (hpriv->endpoints[endpt] < 0) {
768
+ /* Pick the right endpoint node */
769
+ snprintf(devnode, sizeof(devnode), DEVPATH "%s.%02d",
770
+ dpriv->devname, endpt);
771
+
772
+ /* We may need to read/write to the same endpoint later. */
773
+ if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
774
+ if ((fd = open(devnode, mode)) < 0)
775
+ return (-1);
776
+
777
+ hpriv->endpoints[endpt] = fd;
778
+ }
779
+
780
+ return (hpriv->endpoints[endpt]);
781
+ }
782
+
783
+ int
784
+ _sync_gen_transfer(struct usbi_transfer *itransfer)
785
+ {
786
+ struct libusb_transfer *transfer;
787
+ struct device_priv *dpriv;
788
+ int fd, nr = 1;
789
+
790
+ transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
791
+ dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
792
+
793
+ if (dpriv->devname == NULL)
794
+ return (LIBUSB_ERROR_NOT_SUPPORTED);
795
+
796
+ /*
797
+ * Bulk, Interrupt or Isochronous transfer depends on the
798
+ * endpoint and thus the node to open.
799
+ */
800
+ if ((fd = _access_endpoint(transfer)) < 0)
801
+ return _errno_to_libusb(errno);
802
+
803
+ if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
804
+ return _errno_to_libusb(errno);
805
+
806
+ if (IS_XFERIN(transfer)) {
807
+ if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
808
+ if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
809
+ return _errno_to_libusb(errno);
810
+
811
+ nr = read(fd, transfer->buffer, transfer->length);
812
+ } else {
813
+ nr = write(fd, transfer->buffer, transfer->length);
814
+ }
815
+
816
+ if (nr < 0)
817
+ return _errno_to_libusb(errno);
818
+
819
+ itransfer->transferred = nr;
820
+
821
+ return (0);
822
+ }
823
+
824
+ int
825
+ _bus_open(int number)
826
+ {
827
+ char busnode[16];
828
+
829
+ snprintf(busnode, sizeof(busnode), USBDEV "%d", number);
830
+
831
+ return open(busnode, O_RDWR);
832
+ }