usb 2.17.0 → 3.0.0-alpha.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 (207) hide show
  1. package/LICENSE +18 -4
  2. package/README.md +25 -516
  3. package/dist/index.d.ts +67 -13
  4. package/dist/index.js +333 -64
  5. package/index.d.ts +89 -0
  6. package/index.js +583 -0
  7. package/package.json +53 -33
  8. package/CHANGELOG.md +0 -294
  9. package/binding.gyp +0 -128
  10. package/dist/index.js.map +0 -1
  11. package/dist/usb/bindings.d.ts +0 -266
  12. package/dist/usb/bindings.js +0 -10
  13. package/dist/usb/bindings.js.map +0 -1
  14. package/dist/usb/capability.d.ts +0 -13
  15. package/dist/usb/capability.js +0 -17
  16. package/dist/usb/capability.js.map +0 -1
  17. package/dist/usb/descriptors.d.ts +0 -128
  18. package/dist/usb/descriptors.js +0 -3
  19. package/dist/usb/descriptors.js.map +0 -1
  20. package/dist/usb/device.d.ts +0 -100
  21. package/dist/usb/device.js +0 -297
  22. package/dist/usb/device.js.map +0 -1
  23. package/dist/usb/endpoint.d.ts +0 -94
  24. package/dist/usb/endpoint.js +0 -219
  25. package/dist/usb/endpoint.js.map +0 -1
  26. package/dist/usb/index.d.ts +0 -31
  27. package/dist/usb/index.js +0 -116
  28. package/dist/usb/index.js.map +0 -1
  29. package/dist/usb/interface.d.ts +0 -80
  30. package/dist/usb/interface.js +0 -133
  31. package/dist/usb/interface.js.map +0 -1
  32. package/dist/webusb/index.d.ts +0 -64
  33. package/dist/webusb/index.js +0 -295
  34. package/dist/webusb/index.js.map +0 -1
  35. package/dist/webusb/webusb-device.d.ts +0 -54
  36. package/dist/webusb/webusb-device.js +0 -434
  37. package/dist/webusb/webusb-device.js.map +0 -1
  38. package/libusb/.clang-tidy +0 -34
  39. package/libusb/.codespellrc +0 -3
  40. package/libusb/.private/README.txt +0 -5
  41. package/libusb/.private/appveyor_build.sh +0 -26
  42. package/libusb/.private/bm.sh +0 -54
  43. package/libusb/.private/ci-build.sh +0 -92
  44. package/libusb/.private/ci-container-build.sh +0 -67
  45. package/libusb/.private/post-rewrite.sh +0 -32
  46. package/libusb/.private/pre-commit.sh +0 -52
  47. package/libusb/.private/wbs.txt +0 -43
  48. package/libusb/.travis.yml +0 -58
  49. package/libusb/AUTHORS +0 -231
  50. package/libusb/COPYING +0 -504
  51. package/libusb/ChangeLog +0 -365
  52. package/libusb/HACKING +0 -25
  53. package/libusb/INSTALL_WIN.txt +0 -52
  54. package/libusb/KEYS +0 -123
  55. package/libusb/Makefile.am +0 -50
  56. package/libusb/NEWS +0 -2
  57. package/libusb/PORTING +0 -94
  58. package/libusb/README +0 -29
  59. package/libusb/README.git +0 -41
  60. package/libusb/TODO +0 -2
  61. package/libusb/Xcode/common.xcconfig +0 -92
  62. package/libusb/Xcode/config.h +0 -31
  63. package/libusb/Xcode/debug.xcconfig +0 -32
  64. package/libusb/Xcode/libusb.xcconfig +0 -21
  65. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +0 -1391
  66. package/libusb/Xcode/libusb_debug.xcconfig +0 -21
  67. package/libusb/Xcode/libusb_release.xcconfig +0 -21
  68. package/libusb/Xcode/release.xcconfig +0 -30
  69. package/libusb/android/README +0 -152
  70. package/libusb/android/config.h +0 -55
  71. package/libusb/android/examples/unrooted_android.c +0 -301
  72. package/libusb/android/examples/unrooted_android.h +0 -36
  73. package/libusb/android/jni/Android.mk +0 -23
  74. package/libusb/android/jni/Application.mk +0 -40
  75. package/libusb/android/jni/examples.mk +0 -168
  76. package/libusb/android/jni/libusb.mk +0 -60
  77. package/libusb/android/jni/tests.mk +0 -45
  78. package/libusb/appveyor.yml +0 -108
  79. package/libusb/autogen.sh +0 -10
  80. package/libusb/bootstrap.sh +0 -10
  81. package/libusb/configure.ac +0 -450
  82. package/libusb/doc/Makefile.in +0 -22
  83. package/libusb/doc/doxygen.cfg.in +0 -2571
  84. package/libusb/doc/libusb.png +0 -0
  85. package/libusb/examples/Makefile.am +0 -12
  86. package/libusb/examples/dpfp.c +0 -711
  87. package/libusb/examples/ezusb.c +0 -846
  88. package/libusb/examples/ezusb.h +0 -109
  89. package/libusb/examples/fxload.c +0 -310
  90. package/libusb/examples/hotplugtest.c +0 -147
  91. package/libusb/examples/listdevs.c +0 -73
  92. package/libusb/examples/sam3u_benchmark.c +0 -228
  93. package/libusb/examples/testlibusb.c +0 -312
  94. package/libusb/examples/xusb.c +0 -1254
  95. package/libusb/libusb/Makefile.am +0 -98
  96. package/libusb/libusb/Makefile.am.extra +0 -26
  97. package/libusb/libusb/core.c +0 -2925
  98. package/libusb/libusb/descriptor.c +0 -1558
  99. package/libusb/libusb/hotplug.c +0 -489
  100. package/libusb/libusb/io.c +0 -2865
  101. package/libusb/libusb/libusb-1.0.def +0 -199
  102. package/libusb/libusb/libusb-1.0.rc +0 -53
  103. package/libusb/libusb/libusb.h +0 -2421
  104. package/libusb/libusb/libusbi.h +0 -1535
  105. package/libusb/libusb/os/darwin_usb.c +0 -2977
  106. package/libusb/libusb/os/darwin_usb.h +0 -156
  107. package/libusb/libusb/os/emscripten_webusb.cpp +0 -875
  108. package/libusb/libusb/os/events_posix.c +0 -340
  109. package/libusb/libusb/os/events_posix.h +0 -62
  110. package/libusb/libusb/os/events_windows.c +0 -214
  111. package/libusb/libusb/os/events_windows.h +0 -46
  112. package/libusb/libusb/os/haiku_pollfs.cpp +0 -372
  113. package/libusb/libusb/os/haiku_usb.h +0 -113
  114. package/libusb/libusb/os/haiku_usb_backend.cpp +0 -532
  115. package/libusb/libusb/os/haiku_usb_raw.cpp +0 -231
  116. package/libusb/libusb/os/haiku_usb_raw.h +0 -188
  117. package/libusb/libusb/os/linux_netlink.c +0 -401
  118. package/libusb/libusb/os/linux_udev.c +0 -321
  119. package/libusb/libusb/os/linux_usbfs.c +0 -2829
  120. package/libusb/libusb/os/linux_usbfs.h +0 -221
  121. package/libusb/libusb/os/netbsd_usb.c +0 -617
  122. package/libusb/libusb/os/null_usb.c +0 -111
  123. package/libusb/libusb/os/openbsd_usb.c +0 -700
  124. package/libusb/libusb/os/sunos_usb.c +0 -1619
  125. package/libusb/libusb/os/sunos_usb.h +0 -79
  126. package/libusb/libusb/os/threads_posix.c +0 -126
  127. package/libusb/libusb/os/threads_posix.h +0 -98
  128. package/libusb/libusb/os/threads_windows.c +0 -40
  129. package/libusb/libusb/os/threads_windows.h +0 -113
  130. package/libusb/libusb/os/windows_common.c +0 -923
  131. package/libusb/libusb/os/windows_common.h +0 -424
  132. package/libusb/libusb/os/windows_usbdk.c +0 -724
  133. package/libusb/libusb/os/windows_usbdk.h +0 -106
  134. package/libusb/libusb/os/windows_winusb.c +0 -4766
  135. package/libusb/libusb/os/windows_winusb.h +0 -787
  136. package/libusb/libusb/strerror.c +0 -223
  137. package/libusb/libusb/sync.c +0 -342
  138. package/libusb/libusb/version.h +0 -18
  139. package/libusb/libusb/version_nano.h +0 -1
  140. package/libusb/libusb-1.0.pc.in +0 -11
  141. package/libusb/msvc/Base.props +0 -60
  142. package/libusb/msvc/Configuration.Application.props +0 -7
  143. package/libusb/msvc/Configuration.Base.props +0 -47
  144. package/libusb/msvc/Configuration.DynamicLibrary.props +0 -21
  145. package/libusb/msvc/Configuration.StaticLibrary.props +0 -7
  146. package/libusb/msvc/ProjectConfigurations.Base.props +0 -69
  147. package/libusb/msvc/build_all.ps1 +0 -17
  148. package/libusb/msvc/config.h +0 -58
  149. package/libusb/msvc/dpfp.vcxproj +0 -33
  150. package/libusb/msvc/dpfp_threaded.vcxproj +0 -38
  151. package/libusb/msvc/fxload.vcxproj +0 -46
  152. package/libusb/msvc/getopt/getopt.c +0 -1060
  153. package/libusb/msvc/getopt/getopt.h +0 -180
  154. package/libusb/msvc/getopt/getopt1.c +0 -188
  155. package/libusb/msvc/getopt.vcxproj +0 -33
  156. package/libusb/msvc/hotplugtest.vcxproj +0 -32
  157. package/libusb/msvc/init_context.vcxproj +0 -35
  158. package/libusb/msvc/libusb.sln +0 -542
  159. package/libusb/msvc/libusb_dll.vcxproj +0 -61
  160. package/libusb/msvc/libusb_static.vcxproj +0 -49
  161. package/libusb/msvc/listdevs.vcxproj +0 -32
  162. package/libusb/msvc/sam3u_benchmark.vcxproj +0 -33
  163. package/libusb/msvc/set_option.vcxproj +0 -35
  164. package/libusb/msvc/stress.vcxproj +0 -35
  165. package/libusb/msvc/stress_mt.vcxproj +0 -33
  166. package/libusb/msvc/testlibusb.vcxproj +0 -32
  167. package/libusb/msvc/xusb.vcxproj +0 -38
  168. package/libusb/tests/Makefile.am +0 -40
  169. package/libusb/tests/init_context.c +0 -153
  170. package/libusb/tests/libusb_testlib.h +0 -76
  171. package/libusb/tests/macos.c +0 -130
  172. package/libusb/tests/set_option.c +0 -253
  173. package/libusb/tests/stress.c +0 -172
  174. package/libusb/tests/stress_mt.c +0 -267
  175. package/libusb/tests/testlib.c +0 -184
  176. package/libusb/tests/umockdev.c +0 -1175
  177. package/libusb/tests/webusb-test-shim/index.js +0 -12
  178. package/libusb/tests/webusb-test-shim/package-lock.json +0 -50
  179. package/libusb/tests/webusb-test-shim/package.json +0 -10
  180. package/libusb.gypi +0 -154
  181. package/libusb_config/config.h +0 -1
  182. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  183. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  184. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  185. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  186. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  187. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  188. package/prebuilds/linux-ia32/node.napi.node +0 -0
  189. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  190. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  191. package/prebuilds/win32-arm64/node.napi.node +0 -0
  192. package/prebuilds/win32-ia32/node.napi.node +0 -0
  193. package/prebuilds/win32-x64/node.napi.node +0 -0
  194. package/src/device.cc +0 -439
  195. package/src/helpers.h +0 -64
  196. package/src/hotplug/hotplug.h +0 -22
  197. package/src/hotplug/libusb.cc +0 -90
  198. package/src/hotplug/windows.cc +0 -168
  199. package/src/node_usb.cc +0 -314
  200. package/src/node_usb.h +0 -131
  201. package/src/thread_name.cc +0 -79
  202. package/src/thread_name.h +0 -11
  203. package/src/transfer.cc +0 -143
  204. package/src/uv_async_queue.h +0 -41
  205. package/test/usb.coffee +0 -250
  206. package/test/webusb.coffee +0 -227
  207. package/test/worker.cjs +0 -13
@@ -1,401 +0,0 @@
1
- /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
2
- /*
3
- * Linux usbfs backend for libusb
4
- * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5
- * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6
- * Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.com>
7
- * Copyright (c) 2016 Chris Dickens <christopher.a.dickens@gmail.com>
8
- *
9
- * This library is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU Lesser General Public
11
- * License as published by the Free Software Foundation; either
12
- * version 2.1 of the License, or (at your option) any later version.
13
- *
14
- * This library is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
- * Lesser General Public License for more details.
18
- *
19
- * You should have received a copy of the GNU Lesser General Public
20
- * License along with this library; if not, write to the Free Software
21
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
- */
23
-
24
- #include "libusbi.h"
25
- #include "linux_usbfs.h"
26
-
27
- #include <errno.h>
28
- #include <fcntl.h>
29
- #include <poll.h>
30
- #include <pthread.h>
31
- #include <string.h>
32
- #include <unistd.h>
33
-
34
- #ifdef HAVE_ASM_TYPES_H
35
- #include <asm/types.h>
36
- #endif
37
- #include <sys/socket.h>
38
- #include <linux/netlink.h>
39
-
40
- #define NL_GROUP_KERNEL 1
41
-
42
- #ifndef SOCK_CLOEXEC
43
- #define SOCK_CLOEXEC 0
44
- #endif
45
-
46
- #ifndef SOCK_NONBLOCK
47
- #define SOCK_NONBLOCK 0
48
- #endif
49
-
50
- static int linux_netlink_socket = -1;
51
- static usbi_event_t netlink_control_event = USBI_INVALID_EVENT;
52
- static pthread_t libusb_linux_event_thread;
53
-
54
- static void *linux_netlink_event_thread_main(void *arg);
55
-
56
- static int set_fd_cloexec_nb(int fd, int socktype)
57
- {
58
- int flags;
59
-
60
- #if defined(FD_CLOEXEC)
61
- /* Make sure the netlink socket file descriptor is marked as CLOEXEC */
62
- if (!(socktype & SOCK_CLOEXEC)) {
63
- flags = fcntl(fd, F_GETFD);
64
- if (flags == -1) {
65
- usbi_err(NULL, "failed to get netlink fd flags, errno=%d", errno);
66
- return -1;
67
- }
68
-
69
- if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
70
- usbi_err(NULL, "failed to set netlink fd flags, errno=%d", errno);
71
- return -1;
72
- }
73
- }
74
- #endif
75
-
76
- /* Make sure the netlink socket is non-blocking */
77
- if (!(socktype & SOCK_NONBLOCK)) {
78
- flags = fcntl(fd, F_GETFL);
79
- if (flags == -1) {
80
- usbi_err(NULL, "failed to get netlink fd status flags, errno=%d", errno);
81
- return -1;
82
- }
83
-
84
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
85
- usbi_err(NULL, "failed to set netlink fd status flags, errno=%d", errno);
86
- return -1;
87
- }
88
- }
89
-
90
- return 0;
91
- }
92
-
93
- int linux_netlink_start_event_monitor(void)
94
- {
95
- struct sockaddr_nl sa_nl = { .nl_family = AF_NETLINK, .nl_groups = NL_GROUP_KERNEL };
96
- int socktype = SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC;
97
- int opt = 1;
98
- int ret;
99
-
100
- linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
101
- if (linux_netlink_socket == -1 && errno == EINVAL) {
102
- usbi_dbg(NULL, "failed to create netlink socket of type %d, attempting SOCK_RAW", socktype);
103
- socktype = SOCK_RAW;
104
- linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
105
- }
106
-
107
- if (linux_netlink_socket == -1) {
108
- usbi_err(NULL, "failed to create netlink socket, errno=%d", errno);
109
- goto err;
110
- }
111
-
112
- ret = set_fd_cloexec_nb(linux_netlink_socket, socktype);
113
- if (ret == -1)
114
- goto err_close_socket;
115
-
116
- ret = bind(linux_netlink_socket, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
117
- if (ret == -1) {
118
- usbi_err(NULL, "failed to bind netlink socket, errno=%d", errno);
119
- goto err_close_socket;
120
- }
121
-
122
- ret = setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
123
- if (ret == -1) {
124
- usbi_err(NULL, "failed to set netlink socket SO_PASSCRED option, errno=%d", errno);
125
- goto err_close_socket;
126
- }
127
-
128
- ret = usbi_create_event(&netlink_control_event);
129
- if (ret) {
130
- usbi_err(NULL, "failed to create netlink control event");
131
- goto err_close_socket;
132
- }
133
-
134
- ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
135
- if (ret != 0) {
136
- usbi_err(NULL, "failed to create netlink event thread (%d)", ret);
137
- goto err_destroy_event;
138
- }
139
-
140
- return LIBUSB_SUCCESS;
141
-
142
- err_destroy_event:
143
- usbi_destroy_event(&netlink_control_event);
144
- netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
145
- err_close_socket:
146
- close(linux_netlink_socket);
147
- linux_netlink_socket = -1;
148
- err:
149
- return LIBUSB_ERROR_OTHER;
150
- }
151
-
152
- int linux_netlink_stop_event_monitor(void)
153
- {
154
- int ret;
155
-
156
- assert(linux_netlink_socket != -1);
157
-
158
- /* Signal the control event and wait for the thread to exit */
159
- usbi_signal_event(&netlink_control_event);
160
-
161
- ret = pthread_join(libusb_linux_event_thread, NULL);
162
- if (ret)
163
- usbi_warn(NULL, "failed to join netlink event thread (%d)", ret);
164
-
165
- usbi_destroy_event(&netlink_control_event);
166
- netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
167
-
168
- close(linux_netlink_socket);
169
- linux_netlink_socket = -1;
170
-
171
- return LIBUSB_SUCCESS;
172
- }
173
-
174
- static const char *netlink_message_parse(const char *buffer, size_t len, const char *key)
175
- {
176
- const char *end = buffer + len;
177
- size_t keylen = strlen(key);
178
-
179
- while (buffer < end && *buffer) {
180
- if (strncmp(buffer, key, keylen) == 0 && buffer[keylen] == '=')
181
- return buffer + keylen + 1;
182
- buffer += strlen(buffer) + 1;
183
- }
184
-
185
- return NULL;
186
- }
187
-
188
- /* parse parts of netlink message common to both libudev and the kernel */
189
- static int linux_netlink_parse(const char *buffer, size_t len, int *detached,
190
- const char **sys_name, uint8_t *busnum, uint8_t *devaddr)
191
- {
192
- const char *tmp, *slash;
193
-
194
- errno = 0;
195
-
196
- *sys_name = NULL;
197
- *detached = 0;
198
- *busnum = 0;
199
- *devaddr = 0;
200
-
201
- tmp = netlink_message_parse(buffer, len, "ACTION");
202
- if (!tmp) {
203
- return -1;
204
- } else if (strcmp(tmp, "remove") == 0) {
205
- *detached = 1;
206
- } else if (strcmp(tmp, "add") != 0) {
207
- usbi_dbg(NULL, "unknown device action %s", tmp);
208
- return -1;
209
- }
210
-
211
- /* check that this is a usb message */
212
- tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
213
- if (!tmp || strcmp(tmp, "usb") != 0) {
214
- /* not usb. ignore */
215
- return -1;
216
- }
217
-
218
- /* check that this is an actual usb device */
219
- tmp = netlink_message_parse(buffer, len, "DEVTYPE");
220
- if (!tmp || strcmp(tmp, "usb_device") != 0) {
221
- /* not usb. ignore */
222
- return -1;
223
- }
224
-
225
- tmp = netlink_message_parse(buffer, len, "BUSNUM");
226
- if (tmp) {
227
- *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
228
- if (errno) {
229
- errno = 0;
230
- return -1;
231
- }
232
-
233
- tmp = netlink_message_parse(buffer, len, "DEVNUM");
234
- if (NULL == tmp)
235
- return -1;
236
-
237
- *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
238
- if (errno) {
239
- errno = 0;
240
- return -1;
241
- }
242
- } else {
243
- /* no bus number. try "DEVICE" */
244
- tmp = netlink_message_parse(buffer, len, "DEVICE");
245
- if (!tmp) {
246
- /* not usb. ignore */
247
- return -1;
248
- }
249
-
250
- /* Parse a device path such as /dev/bus/usb/003/004 */
251
- slash = strrchr(tmp, '/');
252
- if (!slash)
253
- return -1;
254
-
255
- *busnum = (uint8_t)(strtoul(slash - 3, NULL, 10) & 0xff);
256
- if (errno) {
257
- errno = 0;
258
- return -1;
259
- }
260
-
261
- *devaddr = (uint8_t)(strtoul(slash + 1, NULL, 10) & 0xff);
262
- if (errno) {
263
- errno = 0;
264
- return -1;
265
- }
266
-
267
- return 0;
268
- }
269
-
270
- tmp = netlink_message_parse(buffer, len, "DEVPATH");
271
- if (!tmp)
272
- return -1;
273
-
274
- slash = strrchr(tmp, '/');
275
- if (slash)
276
- *sys_name = slash + 1;
277
-
278
- /* found a usb device */
279
- return 0;
280
- }
281
-
282
- static int linux_netlink_read_message(void)
283
- {
284
- char cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
285
- char msg_buffer[2048];
286
- const char *sys_name = NULL;
287
- uint8_t busnum, devaddr;
288
- int detached, r;
289
- ssize_t len;
290
- struct cmsghdr *cmsg;
291
- struct ucred *cred;
292
- struct sockaddr_nl sa_nl;
293
- struct iovec iov = { .iov_base = msg_buffer, .iov_len = sizeof(msg_buffer) };
294
- struct msghdr msg = {
295
- .msg_iov = &iov, .msg_iovlen = 1,
296
- .msg_control = cred_buffer, .msg_controllen = sizeof(cred_buffer),
297
- .msg_name = &sa_nl, .msg_namelen = sizeof(sa_nl)
298
- };
299
-
300
- /* read netlink message */
301
- len = recvmsg(linux_netlink_socket, &msg, 0);
302
- if (len == -1) {
303
- if (errno != EAGAIN && errno != EINTR)
304
- usbi_err(NULL, "error receiving message from netlink, errno=%d", errno);
305
- return -1;
306
- }
307
-
308
- if (len < 32 || (msg.msg_flags & MSG_TRUNC)) {
309
- usbi_err(NULL, "invalid netlink message length");
310
- return -1;
311
- }
312
-
313
- if (sa_nl.nl_groups != NL_GROUP_KERNEL || sa_nl.nl_pid != 0) {
314
- usbi_dbg(NULL, "ignoring netlink message from unknown group/PID (%u/%u)",
315
- (unsigned int)sa_nl.nl_groups, (unsigned int)sa_nl.nl_pid);
316
- return -1;
317
- }
318
-
319
- cmsg = CMSG_FIRSTHDR(&msg);
320
- if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
321
- usbi_dbg(NULL, "ignoring netlink message with no sender credentials");
322
- return -1;
323
- }
324
-
325
- cred = (struct ucred *)CMSG_DATA(cmsg);
326
- if (cred->uid != 0) {
327
- usbi_dbg(NULL, "ignoring netlink message with non-zero sender UID %u", (unsigned int)cred->uid);
328
- return -1;
329
- }
330
-
331
- r = linux_netlink_parse(msg_buffer, (size_t)len, &detached, &sys_name, &busnum, &devaddr);
332
- if (r)
333
- return r;
334
-
335
- usbi_dbg(NULL, "netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
336
- busnum, devaddr, sys_name, detached ? "yes" : "no");
337
-
338
- /* signal device is available (or not) to all contexts */
339
- if (detached)
340
- linux_device_disconnected(busnum, devaddr);
341
- else
342
- linux_hotplug_enumerate(busnum, devaddr, sys_name);
343
-
344
- return 0;
345
- }
346
-
347
- static void *linux_netlink_event_thread_main(void *arg)
348
- {
349
- struct pollfd fds[] = {
350
- { .fd = USBI_EVENT_OS_HANDLE(&netlink_control_event),
351
- .events = USBI_EVENT_POLL_EVENTS },
352
- { .fd = linux_netlink_socket,
353
- .events = POLLIN },
354
- };
355
- int r;
356
-
357
- UNUSED(arg);
358
-
359
- #if defined(HAVE_PTHREAD_SETNAME_NP)
360
- r = pthread_setname_np(pthread_self(), "libusb_event");
361
- if (r)
362
- usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
363
- #endif
364
-
365
- usbi_dbg(NULL, "netlink event thread entering");
366
-
367
- while (1) {
368
- r = poll(fds, 2, -1);
369
- if (r == -1) {
370
- /* check for temporary failure */
371
- if (errno == EINTR)
372
- continue;
373
- usbi_err(NULL, "poll() failed, errno=%d", errno);
374
- break;
375
- }
376
- if (fds[0].revents) {
377
- /* activity on control event, exit */
378
- break;
379
- }
380
- if (fds[1].revents) {
381
- usbi_mutex_static_lock(&linux_hotplug_lock);
382
- linux_netlink_read_message();
383
- usbi_mutex_static_unlock(&linux_hotplug_lock);
384
- }
385
- }
386
-
387
- usbi_dbg(NULL, "netlink event thread exiting");
388
-
389
- return NULL;
390
- }
391
-
392
- void linux_netlink_hotplug_poll(void)
393
- {
394
- int r;
395
-
396
- usbi_mutex_static_lock(&linux_hotplug_lock);
397
- do {
398
- r = linux_netlink_read_message();
399
- } while (r == 0);
400
- usbi_mutex_static_unlock(&linux_hotplug_lock);
401
- }
@@ -1,321 +0,0 @@
1
- /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
2
- /*
3
- * Linux usbfs backend for libusb
4
- * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5
- * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6
- * Copyright (c) 2012-2013 Nathan Hjelm <hjelmn@mac.com>
7
- *
8
- * This library is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU Lesser General Public
10
- * License as published by the Free Software Foundation; either
11
- * version 2.1 of the License, or (at your option) any later version.
12
- *
13
- * This library is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
- * Lesser General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU Lesser General Public
19
- * License along with this library; if not, write to the Free Software
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
- */
22
-
23
- #include "libusbi.h"
24
- #include "linux_usbfs.h"
25
-
26
- #include <errno.h>
27
- #include <fcntl.h>
28
- #include <libudev.h>
29
- #include <poll.h>
30
- #include <pthread.h>
31
- #include <string.h>
32
- #include <unistd.h>
33
-
34
- /* udev context */
35
- static struct udev *udev_ctx = NULL;
36
- static int udev_monitor_fd = -1;
37
- static usbi_event_t udev_control_event = USBI_INVALID_EVENT;
38
- static struct udev_monitor *udev_monitor = NULL;
39
- static pthread_t linux_event_thread;
40
-
41
- static void udev_hotplug_event(struct udev_device *udev_dev);
42
- static void *linux_udev_event_thread_main(void *arg);
43
-
44
- int linux_udev_start_event_monitor(void)
45
- {
46
- int r;
47
-
48
- assert(udev_ctx == NULL);
49
- udev_ctx = udev_new();
50
- if (!udev_ctx) {
51
- usbi_err(NULL, "could not create udev context");
52
- goto err;
53
- }
54
-
55
- udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
56
- if (!udev_monitor) {
57
- usbi_err(NULL, "could not initialize udev monitor");
58
- goto err_free_ctx;
59
- }
60
-
61
- r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
62
- if (r) {
63
- usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
64
- goto err_free_monitor;
65
- }
66
-
67
- if (udev_monitor_enable_receiving(udev_monitor)) {
68
- usbi_err(NULL, "failed to enable the udev monitor");
69
- goto err_free_monitor;
70
- }
71
-
72
- udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
73
-
74
- #if defined(FD_CLOEXEC)
75
- /* Make sure the udev file descriptor is marked as CLOEXEC */
76
- r = fcntl(udev_monitor_fd, F_GETFD);
77
- if (r == -1) {
78
- usbi_err(NULL, "failed to get udev monitor fd flags, errno=%d", errno);
79
- goto err_free_monitor;
80
- }
81
- if (!(r & FD_CLOEXEC)) {
82
- if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
83
- usbi_err(NULL, "failed to set udev monitor fd flags, errno=%d", errno);
84
- goto err_free_monitor;
85
- }
86
- }
87
- #endif
88
-
89
- /* Some older versions of udev are not non-blocking by default,
90
- * so make sure this is set */
91
- r = fcntl(udev_monitor_fd, F_GETFL);
92
- if (r == -1) {
93
- usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
94
- goto err_free_monitor;
95
- }
96
- if (!(r & O_NONBLOCK)) {
97
- if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
98
- usbi_err(NULL, "failed to set udev monitor fd status flags, errno=%d", errno);
99
- goto err_free_monitor;
100
- }
101
- }
102
-
103
- r = usbi_create_event(&udev_control_event);
104
- if (r) {
105
- usbi_err(NULL, "failed to create udev control event");
106
- goto err_free_monitor;
107
- }
108
-
109
- r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
110
- if (r) {
111
- usbi_err(NULL, "failed to create hotplug event thread (%d)", r);
112
- goto err_destroy_event;
113
- }
114
-
115
- return LIBUSB_SUCCESS;
116
-
117
- err_destroy_event:
118
- usbi_destroy_event(&udev_control_event);
119
- udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
120
- err_free_monitor:
121
- udev_monitor_unref(udev_monitor);
122
- udev_monitor = NULL;
123
- udev_monitor_fd = -1;
124
- err_free_ctx:
125
- udev_unref(udev_ctx);
126
- err:
127
- udev_ctx = NULL;
128
- return LIBUSB_ERROR_OTHER;
129
- }
130
-
131
- int linux_udev_stop_event_monitor(void)
132
- {
133
- int r;
134
-
135
- assert(udev_ctx != NULL);
136
- assert(udev_monitor != NULL);
137
- assert(udev_monitor_fd != -1);
138
-
139
- /* Signal the control event and wait for the thread to exit */
140
- usbi_signal_event(&udev_control_event);
141
-
142
- r = pthread_join(linux_event_thread, NULL);
143
- if (r)
144
- usbi_warn(NULL, "failed to join hotplug event thread (%d)", r);
145
-
146
- usbi_destroy_event(&udev_control_event);
147
- udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
148
-
149
- /* Release the udev monitor */
150
- udev_monitor_unref(udev_monitor);
151
- udev_monitor = NULL;
152
- udev_monitor_fd = -1;
153
-
154
- /* Clean up the udev context */
155
- udev_unref(udev_ctx);
156
- udev_ctx = NULL;
157
-
158
- return LIBUSB_SUCCESS;
159
- }
160
-
161
- static void *linux_udev_event_thread_main(void *arg)
162
- {
163
- struct pollfd fds[] = {
164
- { .fd = USBI_EVENT_OS_HANDLE(&udev_control_event),
165
- .events = USBI_EVENT_POLL_EVENTS },
166
- { .fd = udev_monitor_fd,
167
- .events = POLLIN },
168
- };
169
- struct udev_device *udev_dev;
170
- int r;
171
-
172
- UNUSED(arg);
173
-
174
- #if defined(HAVE_PTHREAD_SETNAME_NP)
175
- r = pthread_setname_np(pthread_self(), "libusb_event");
176
- if (r)
177
- usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
178
- #endif
179
-
180
- usbi_dbg(NULL, "udev event thread entering");
181
-
182
- while (1) {
183
- r = poll(fds, 2, -1);
184
- if (r == -1) {
185
- /* check for temporary failure */
186
- if (errno == EINTR)
187
- continue;
188
- usbi_err(NULL, "poll() failed, errno=%d", errno);
189
- break;
190
- }
191
- if (fds[0].revents) {
192
- /* activity on control event, exit */
193
- break;
194
- }
195
- if (fds[1].revents) {
196
- usbi_mutex_static_lock(&linux_hotplug_lock);
197
- udev_dev = udev_monitor_receive_device(udev_monitor);
198
- if (udev_dev)
199
- udev_hotplug_event(udev_dev);
200
- usbi_mutex_static_unlock(&linux_hotplug_lock);
201
- }
202
- }
203
-
204
- usbi_dbg(NULL, "udev event thread exiting");
205
-
206
- return NULL;
207
- }
208
-
209
- static int udev_device_info(struct libusb_context *ctx, int detached,
210
- struct udev_device *udev_dev, uint8_t *busnum,
211
- uint8_t *devaddr, const char **sys_name) {
212
- const char *dev_node;
213
-
214
- dev_node = udev_device_get_devnode(udev_dev);
215
- if (!dev_node) {
216
- return LIBUSB_ERROR_OTHER;
217
- }
218
-
219
- *sys_name = udev_device_get_sysname(udev_dev);
220
- if (!*sys_name) {
221
- return LIBUSB_ERROR_OTHER;
222
- }
223
-
224
- return linux_get_device_address(ctx, detached, busnum, devaddr,
225
- dev_node, *sys_name, -1);
226
- }
227
-
228
- static void udev_hotplug_event(struct udev_device *udev_dev)
229
- {
230
- const char *udev_action;
231
- const char *sys_name = NULL;
232
- uint8_t busnum = 0, devaddr = 0;
233
- int detached;
234
- int r;
235
-
236
- do {
237
- udev_action = udev_device_get_action(udev_dev);
238
- if (!udev_action) {
239
- break;
240
- }
241
-
242
- detached = !strncmp(udev_action, "remove", 6);
243
-
244
- r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
245
- if (LIBUSB_SUCCESS != r) {
246
- break;
247
- }
248
-
249
- usbi_dbg(NULL, "udev hotplug event. action: %s.", udev_action);
250
-
251
- if (strncmp(udev_action, "add", 3) == 0) {
252
- linux_hotplug_enumerate(busnum, devaddr, sys_name);
253
- } else if (detached) {
254
- linux_device_disconnected(busnum, devaddr);
255
- } else if (strncmp(udev_action, "bind", 4) == 0) {
256
- /* silently ignore "known unhandled" action */
257
- } else {
258
- usbi_err(NULL, "ignoring udev action %s", udev_action);
259
- }
260
- } while (0);
261
-
262
- udev_device_unref(udev_dev);
263
- }
264
-
265
- int linux_udev_scan_devices(struct libusb_context *ctx)
266
- {
267
- struct udev_enumerate *enumerator;
268
- struct udev_list_entry *devices, *entry;
269
- struct udev_device *udev_dev;
270
- const char *sys_name;
271
- int r;
272
-
273
- assert(udev_ctx != NULL);
274
-
275
- enumerator = udev_enumerate_new(udev_ctx);
276
- if (NULL == enumerator) {
277
- usbi_err(ctx, "error creating udev enumerator");
278
- return LIBUSB_ERROR_OTHER;
279
- }
280
-
281
- udev_enumerate_add_match_subsystem(enumerator, "usb");
282
- udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
283
- udev_enumerate_scan_devices(enumerator);
284
- devices = udev_enumerate_get_list_entry(enumerator);
285
-
286
- entry = NULL;
287
- udev_list_entry_foreach(entry, devices) {
288
- const char *path = udev_list_entry_get_name(entry);
289
- uint8_t busnum = 0, devaddr = 0;
290
-
291
- udev_dev = udev_device_new_from_syspath(udev_ctx, path);
292
-
293
- r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
294
- if (r) {
295
- udev_device_unref(udev_dev);
296
- continue;
297
- }
298
-
299
- linux_enumerate_device(ctx, busnum, devaddr, sys_name);
300
- udev_device_unref(udev_dev);
301
- }
302
-
303
- udev_enumerate_unref(enumerator);
304
-
305
- return LIBUSB_SUCCESS;
306
- }
307
-
308
- void linux_udev_hotplug_poll(void)
309
- {
310
- struct udev_device *udev_dev;
311
-
312
- usbi_mutex_static_lock(&linux_hotplug_lock);
313
- do {
314
- udev_dev = udev_monitor_receive_device(udev_monitor);
315
- if (udev_dev) {
316
- usbi_dbg(NULL, "Handling hotplug event from hotplug_poll");
317
- udev_hotplug_event(udev_dev);
318
- }
319
- } while (udev_dev);
320
- usbi_mutex_static_unlock(&linux_hotplug_lock);
321
- }