libusb 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -2
- data/Gemfile +2 -0
- data/History.md +12 -0
- data/README.md +25 -14
- data/Rakefile +64 -109
- data/appveyor.yml +23 -0
- data/ext/extconf.rb +10 -12
- data/ext/libusb_recipe.rb +29 -0
- data/lib/libusb/call.rb +14 -11
- data/lib/libusb/compat.rb +9 -9
- data/lib/libusb/context.rb +16 -1
- data/lib/libusb/dependencies.rb +7 -0
- data/lib/libusb/dev_handle.rb +13 -3
- data/lib/libusb/eventmachine.rb +4 -4
- data/lib/libusb/transfer.rb +71 -10
- data/lib/libusb/version_gem.rb +1 -1
- data/libusb.gemspec +7 -5
- data/ports/archives/libusb-1.0.21.tar.bz2 +0 -0
- data/test/test_libusb_bulk_stream_transfer.rb +1 -1
- data/test/test_libusb_descriptors.rb +4 -4
- data/test/test_libusb_event_machine.rb +7 -7
- data/test/test_libusb_hotplug.rb +15 -3
- data/test/test_libusb_iso_transfer.rb +1 -1
- data/test/test_libusb_mass_storage.rb +19 -19
- data/test/test_libusb_mass_storage2.rb +1 -1
- data/test/test_libusb_structs.rb +13 -0
- data/test/test_libusb_threads.rb +2 -2
- data/wireshark-usb-sniffer.png +0 -0
- metadata +32 -156
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/ext/libusb-1.0.20/AUTHORS +0 -89
- data/ext/libusb-1.0.20/COPYING +0 -504
- data/ext/libusb-1.0.20/ChangeLog +0 -227
- data/ext/libusb-1.0.20/INSTALL +0 -234
- data/ext/libusb-1.0.20/Makefile.am +0 -28
- data/ext/libusb-1.0.20/Makefile.in +0 -897
- data/ext/libusb-1.0.20/NEWS +0 -2
- data/ext/libusb-1.0.20/PORTING +0 -94
- data/ext/libusb-1.0.20/README +0 -29
- data/ext/libusb-1.0.20/TODO +0 -2
- data/ext/libusb-1.0.20/Xcode/common.xcconfig +0 -49
- data/ext/libusb-1.0.20/Xcode/config.h +0 -28
- data/ext/libusb-1.0.20/Xcode/debug.xcconfig +0 -29
- data/ext/libusb-1.0.20/Xcode/libusb.xcconfig +0 -21
- data/ext/libusb-1.0.20/Xcode/libusb.xcodeproj/project.pbxproj +0 -865
- data/ext/libusb-1.0.20/Xcode/libusb_debug.xcconfig +0 -21
- data/ext/libusb-1.0.20/Xcode/libusb_release.xcconfig +0 -21
- data/ext/libusb-1.0.20/Xcode/release.xcconfig +0 -30
- data/ext/libusb-1.0.20/aclocal.m4 +0 -1193
- data/ext/libusb-1.0.20/android/README +0 -114
- data/ext/libusb-1.0.20/android/config.h +0 -81
- data/ext/libusb-1.0.20/android/jni/Android.mk +0 -23
- data/ext/libusb-1.0.20/android/jni/Application.mk +0 -24
- data/ext/libusb-1.0.20/android/jni/examples.mk +0 -134
- data/ext/libusb-1.0.20/android/jni/libusb.mk +0 -54
- data/ext/libusb-1.0.20/android/jni/tests.mk +0 -56
- data/ext/libusb-1.0.20/compile +0 -347
- data/ext/libusb-1.0.20/config.guess +0 -1421
- data/ext/libusb-1.0.20/config.h.in +0 -155
- data/ext/libusb-1.0.20/config.sub +0 -1807
- data/ext/libusb-1.0.20/configure +0 -15466
- data/ext/libusb-1.0.20/configure.ac +0 -326
- data/ext/libusb-1.0.20/depcomp +0 -791
- data/ext/libusb-1.0.20/doc/Makefile.am +0 -9
- data/ext/libusb-1.0.20/doc/Makefile.in +0 -456
- data/ext/libusb-1.0.20/doc/doxygen.cfg.in +0 -2334
- data/ext/libusb-1.0.20/examples/Makefile.am +0 -19
- data/ext/libusb-1.0.20/examples/Makefile.in +0 -713
- data/ext/libusb-1.0.20/examples/dpfp.c +0 -506
- data/ext/libusb-1.0.20/examples/dpfp_threaded.c +0 -549
- data/ext/libusb-1.0.20/examples/ezusb.c +0 -831
- data/ext/libusb-1.0.20/examples/ezusb.h +0 -120
- data/ext/libusb-1.0.20/examples/fxload.c +0 -308
- data/ext/libusb-1.0.20/examples/getopt/getopt.c +0 -1060
- data/ext/libusb-1.0.20/examples/getopt/getopt.h +0 -180
- data/ext/libusb-1.0.20/examples/getopt/getopt1.c +0 -188
- data/ext/libusb-1.0.20/examples/hotplugtest.c +0 -122
- data/ext/libusb-1.0.20/examples/listdevs.c +0 -71
- data/ext/libusb-1.0.20/examples/sam3u_benchmark.c +0 -193
- data/ext/libusb-1.0.20/examples/xusb.c +0 -1130
- data/ext/libusb-1.0.20/install-sh +0 -501
- data/ext/libusb-1.0.20/libusb-1.0.pc.in +0 -11
- data/ext/libusb-1.0.20/libusb/Makefile.am +0 -90
- data/ext/libusb-1.0.20/libusb/Makefile.in +0 -1053
- data/ext/libusb-1.0.20/libusb/core.c +0 -2452
- data/ext/libusb-1.0.20/libusb/descriptor.c +0 -1201
- data/ext/libusb-1.0.20/libusb/hotplug.c +0 -344
- data/ext/libusb-1.0.20/libusb/hotplug.h +0 -90
- data/ext/libusb-1.0.20/libusb/io.c +0 -2760
- data/ext/libusb-1.0.20/libusb/libusb-1.0.def +0 -168
- data/ext/libusb-1.0.20/libusb/libusb-1.0.rc +0 -61
- data/ext/libusb-1.0.20/libusb/libusb.h +0 -1999
- data/ext/libusb-1.0.20/libusb/libusbi.h +0 -1102
- data/ext/libusb-1.0.20/libusb/os/darwin_usb.c +0 -1969
- data/ext/libusb-1.0.20/libusb/os/darwin_usb.h +0 -158
- data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.am +0 -5
- data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.in +0 -810
- data/ext/libusb-1.0.20/libusb/os/haiku/aclocal.m4 +0 -1193
- data/ext/libusb-1.0.20/libusb/os/haiku/compile +0 -347
- data/ext/libusb-1.0.20/libusb/os/haiku/config.guess +0 -1421
- data/ext/libusb-1.0.20/libusb/os/haiku/config.sub +0 -1807
- data/ext/libusb-1.0.20/libusb/os/haiku/configure +0 -17579
- data/ext/libusb-1.0.20/libusb/os/haiku/configure.ac +0 -8
- data/ext/libusb-1.0.20/libusb/os/haiku/depcomp +0 -791
- data/ext/libusb-1.0.20/libusb/os/haiku/haiku_pollfs.cpp +0 -378
- data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb.h +0 -112
- data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_backend.cpp +0 -550
- data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.cpp +0 -255
- data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.h +0 -180
- data/ext/libusb-1.0.20/libusb/os/haiku/install-sh +0 -501
- data/ext/libusb-1.0.20/libusb/os/haiku/ltmain.sh +0 -9655
- data/ext/libusb-1.0.20/libusb/os/haiku/m4/libtool.m4 +0 -7992
- data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltoptions.m4 +0 -384
- data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltsugar.m4 +0 -123
- data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltversion.m4 +0 -23
- data/ext/libusb-1.0.20/libusb/os/haiku/m4/lt~obsolete.m4 +0 -98
- data/ext/libusb-1.0.20/libusb/os/haiku/missing +0 -215
- data/ext/libusb-1.0.20/libusb/os/linux_netlink.c +0 -369
- data/ext/libusb-1.0.20/libusb/os/linux_udev.c +0 -306
- data/ext/libusb-1.0.20/libusb/os/linux_usbfs.c +0 -2692
- data/ext/libusb-1.0.20/libusb/os/linux_usbfs.h +0 -192
- data/ext/libusb-1.0.20/libusb/os/netbsd_usb.c +0 -674
- data/ext/libusb-1.0.20/libusb/os/openbsd_usb.c +0 -768
- data/ext/libusb-1.0.20/libusb/os/poll_posix.c +0 -53
- data/ext/libusb-1.0.20/libusb/os/poll_posix.h +0 -11
- data/ext/libusb-1.0.20/libusb/os/poll_windows.c +0 -728
- data/ext/libusb-1.0.20/libusb/os/poll_windows.h +0 -131
- data/ext/libusb-1.0.20/libusb/os/threads_posix.c +0 -82
- data/ext/libusb-1.0.20/libusb/os/threads_posix.h +0 -50
- data/ext/libusb-1.0.20/libusb/os/threads_windows.c +0 -214
- data/ext/libusb-1.0.20/libusb/os/threads_windows.h +0 -87
- data/ext/libusb-1.0.20/libusb/os/wince_usb.c +0 -869
- data/ext/libusb-1.0.20/libusb/os/wince_usb.h +0 -131
- data/ext/libusb-1.0.20/libusb/os/windows_common.h +0 -108
- data/ext/libusb-1.0.20/libusb/os/windows_usb.c +0 -4643
- data/ext/libusb-1.0.20/libusb/os/windows_usb.h +0 -973
- data/ext/libusb-1.0.20/libusb/strerror.c +0 -202
- data/ext/libusb-1.0.20/libusb/sync.c +0 -308
- data/ext/libusb-1.0.20/libusb/version.h +0 -18
- data/ext/libusb-1.0.20/libusb/version_nano.h +0 -1
- data/ext/libusb-1.0.20/ltmain.sh +0 -9655
- data/ext/libusb-1.0.20/m4/libtool.m4 +0 -7992
- data/ext/libusb-1.0.20/m4/ltoptions.m4 +0 -384
- data/ext/libusb-1.0.20/m4/ltsugar.m4 +0 -123
- data/ext/libusb-1.0.20/m4/ltversion.m4 +0 -23
- data/ext/libusb-1.0.20/m4/lt~obsolete.m4 +0 -98
- data/ext/libusb-1.0.20/missing +0 -215
- data/ext/libusb-1.0.20/tests/Makefile.am +0 -6
- data/ext/libusb-1.0.20/tests/Makefile.in +0 -596
- data/ext/libusb-1.0.20/tests/libusb_testlib.h +0 -107
- data/ext/libusb-1.0.20/tests/stress.c +0 -160
- data/ext/libusb-1.0.20/tests/testlib.c +0 -277
- metadata.gz.sig +0 -0
@@ -1,306 +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 <config.h>
|
24
|
-
|
25
|
-
#include <assert.h>
|
26
|
-
#include <ctype.h>
|
27
|
-
#include <dirent.h>
|
28
|
-
#include <errno.h>
|
29
|
-
#include <fcntl.h>
|
30
|
-
#include <poll.h>
|
31
|
-
#include <stdio.h>
|
32
|
-
#include <stdlib.h>
|
33
|
-
#include <string.h>
|
34
|
-
#include <sys/ioctl.h>
|
35
|
-
#include <sys/stat.h>
|
36
|
-
#include <sys/types.h>
|
37
|
-
#include <sys/utsname.h>
|
38
|
-
#include <sys/socket.h>
|
39
|
-
#include <unistd.h>
|
40
|
-
#include <libudev.h>
|
41
|
-
|
42
|
-
#include "libusbi.h"
|
43
|
-
#include "linux_usbfs.h"
|
44
|
-
|
45
|
-
/* udev context */
|
46
|
-
static struct udev *udev_ctx = NULL;
|
47
|
-
static int udev_monitor_fd = -1;
|
48
|
-
static int udev_control_pipe[2] = {-1, -1};
|
49
|
-
static struct udev_monitor *udev_monitor = NULL;
|
50
|
-
static pthread_t linux_event_thread;
|
51
|
-
|
52
|
-
static void udev_hotplug_event(struct udev_device* udev_dev);
|
53
|
-
static void *linux_udev_event_thread_main(void *arg);
|
54
|
-
|
55
|
-
int linux_udev_start_event_monitor(void)
|
56
|
-
{
|
57
|
-
int r;
|
58
|
-
|
59
|
-
assert(udev_ctx == NULL);
|
60
|
-
udev_ctx = udev_new();
|
61
|
-
if (!udev_ctx) {
|
62
|
-
usbi_err(NULL, "could not create udev context");
|
63
|
-
goto err;
|
64
|
-
}
|
65
|
-
|
66
|
-
udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
|
67
|
-
if (!udev_monitor) {
|
68
|
-
usbi_err(NULL, "could not initialize udev monitor");
|
69
|
-
goto err_free_ctx;
|
70
|
-
}
|
71
|
-
|
72
|
-
r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
|
73
|
-
if (r) {
|
74
|
-
usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
|
75
|
-
goto err_free_monitor;
|
76
|
-
}
|
77
|
-
|
78
|
-
if (udev_monitor_enable_receiving(udev_monitor)) {
|
79
|
-
usbi_err(NULL, "failed to enable the udev monitor");
|
80
|
-
goto err_free_monitor;
|
81
|
-
}
|
82
|
-
|
83
|
-
udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
|
84
|
-
|
85
|
-
/* Some older versions of udev are not non-blocking by default,
|
86
|
-
* so make sure this is set */
|
87
|
-
r = fcntl(udev_monitor_fd, F_GETFL);
|
88
|
-
if (r == -1) {
|
89
|
-
usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
|
90
|
-
goto err_free_monitor;
|
91
|
-
}
|
92
|
-
r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
|
93
|
-
if (r) {
|
94
|
-
usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
|
95
|
-
goto err_free_monitor;
|
96
|
-
}
|
97
|
-
|
98
|
-
r = usbi_pipe(udev_control_pipe);
|
99
|
-
if (r) {
|
100
|
-
usbi_err(NULL, "could not create udev control pipe");
|
101
|
-
goto err_free_monitor;
|
102
|
-
}
|
103
|
-
|
104
|
-
r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
|
105
|
-
if (r) {
|
106
|
-
usbi_err(NULL, "creating hotplug event thread (%d)", r);
|
107
|
-
goto err_close_pipe;
|
108
|
-
}
|
109
|
-
|
110
|
-
return LIBUSB_SUCCESS;
|
111
|
-
|
112
|
-
err_close_pipe:
|
113
|
-
close(udev_control_pipe[0]);
|
114
|
-
close(udev_control_pipe[1]);
|
115
|
-
err_free_monitor:
|
116
|
-
udev_monitor_unref(udev_monitor);
|
117
|
-
udev_monitor = NULL;
|
118
|
-
udev_monitor_fd = -1;
|
119
|
-
err_free_ctx:
|
120
|
-
udev_unref(udev_ctx);
|
121
|
-
err:
|
122
|
-
udev_ctx = NULL;
|
123
|
-
return LIBUSB_ERROR_OTHER;
|
124
|
-
}
|
125
|
-
|
126
|
-
int linux_udev_stop_event_monitor(void)
|
127
|
-
{
|
128
|
-
char dummy = 1;
|
129
|
-
int r;
|
130
|
-
|
131
|
-
assert(udev_ctx != NULL);
|
132
|
-
assert(udev_monitor != NULL);
|
133
|
-
assert(udev_monitor_fd != -1);
|
134
|
-
|
135
|
-
/* Write some dummy data to the control pipe and
|
136
|
-
* wait for the thread to exit */
|
137
|
-
r = usbi_write(udev_control_pipe[1], &dummy, sizeof(dummy));
|
138
|
-
if (r <= 0) {
|
139
|
-
usbi_warn(NULL, "udev control pipe signal failed");
|
140
|
-
}
|
141
|
-
pthread_join(linux_event_thread, NULL);
|
142
|
-
|
143
|
-
/* Release the udev monitor */
|
144
|
-
udev_monitor_unref(udev_monitor);
|
145
|
-
udev_monitor = NULL;
|
146
|
-
udev_monitor_fd = -1;
|
147
|
-
|
148
|
-
/* Clean up the udev context */
|
149
|
-
udev_unref(udev_ctx);
|
150
|
-
udev_ctx = NULL;
|
151
|
-
|
152
|
-
/* close and reset control pipe */
|
153
|
-
close(udev_control_pipe[0]);
|
154
|
-
close(udev_control_pipe[1]);
|
155
|
-
udev_control_pipe[0] = -1;
|
156
|
-
udev_control_pipe[1] = -1;
|
157
|
-
|
158
|
-
return LIBUSB_SUCCESS;
|
159
|
-
}
|
160
|
-
|
161
|
-
static void *linux_udev_event_thread_main(void *arg)
|
162
|
-
{
|
163
|
-
char dummy;
|
164
|
-
int r;
|
165
|
-
struct udev_device* udev_dev;
|
166
|
-
struct pollfd fds[] = {
|
167
|
-
{.fd = udev_control_pipe[0],
|
168
|
-
.events = POLLIN},
|
169
|
-
{.fd = udev_monitor_fd,
|
170
|
-
.events = POLLIN},
|
171
|
-
};
|
172
|
-
|
173
|
-
usbi_dbg("udev event thread entering.");
|
174
|
-
|
175
|
-
while (poll(fds, 2, -1) >= 0) {
|
176
|
-
if (fds[0].revents & POLLIN) {
|
177
|
-
/* activity on control pipe, read the byte and exit */
|
178
|
-
r = usbi_read(udev_control_pipe[0], &dummy, sizeof(dummy));
|
179
|
-
if (r <= 0) {
|
180
|
-
usbi_warn(NULL, "udev control pipe read failed");
|
181
|
-
}
|
182
|
-
break;
|
183
|
-
}
|
184
|
-
if (fds[1].revents & POLLIN) {
|
185
|
-
usbi_mutex_static_lock(&linux_hotplug_lock);
|
186
|
-
udev_dev = udev_monitor_receive_device(udev_monitor);
|
187
|
-
if (udev_dev)
|
188
|
-
udev_hotplug_event(udev_dev);
|
189
|
-
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
190
|
-
}
|
191
|
-
}
|
192
|
-
|
193
|
-
usbi_dbg("udev event thread exiting");
|
194
|
-
|
195
|
-
return NULL;
|
196
|
-
}
|
197
|
-
|
198
|
-
static int udev_device_info(struct libusb_context *ctx, int detached,
|
199
|
-
struct udev_device *udev_dev, uint8_t *busnum,
|
200
|
-
uint8_t *devaddr, const char **sys_name) {
|
201
|
-
const char *dev_node;
|
202
|
-
|
203
|
-
dev_node = udev_device_get_devnode(udev_dev);
|
204
|
-
if (!dev_node) {
|
205
|
-
return LIBUSB_ERROR_OTHER;
|
206
|
-
}
|
207
|
-
|
208
|
-
*sys_name = udev_device_get_sysname(udev_dev);
|
209
|
-
if (!*sys_name) {
|
210
|
-
return LIBUSB_ERROR_OTHER;
|
211
|
-
}
|
212
|
-
|
213
|
-
return linux_get_device_address(ctx, detached, busnum, devaddr,
|
214
|
-
dev_node, *sys_name);
|
215
|
-
}
|
216
|
-
|
217
|
-
static void udev_hotplug_event(struct udev_device* udev_dev)
|
218
|
-
{
|
219
|
-
const char* udev_action;
|
220
|
-
const char* sys_name = NULL;
|
221
|
-
uint8_t busnum = 0, devaddr = 0;
|
222
|
-
int detached;
|
223
|
-
int r;
|
224
|
-
|
225
|
-
do {
|
226
|
-
udev_action = udev_device_get_action(udev_dev);
|
227
|
-
if (!udev_action) {
|
228
|
-
break;
|
229
|
-
}
|
230
|
-
|
231
|
-
detached = !strncmp(udev_action, "remove", 6);
|
232
|
-
|
233
|
-
r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
|
234
|
-
if (LIBUSB_SUCCESS != r) {
|
235
|
-
break;
|
236
|
-
}
|
237
|
-
|
238
|
-
usbi_dbg("udev hotplug event. action: %s.", udev_action);
|
239
|
-
|
240
|
-
if (strncmp(udev_action, "add", 3) == 0) {
|
241
|
-
linux_hotplug_enumerate(busnum, devaddr, sys_name);
|
242
|
-
} else if (detached) {
|
243
|
-
linux_device_disconnected(busnum, devaddr, sys_name);
|
244
|
-
} else {
|
245
|
-
usbi_err(NULL, "ignoring udev action %s", udev_action);
|
246
|
-
}
|
247
|
-
} while (0);
|
248
|
-
|
249
|
-
udev_device_unref(udev_dev);
|
250
|
-
}
|
251
|
-
|
252
|
-
int linux_udev_scan_devices(struct libusb_context *ctx)
|
253
|
-
{
|
254
|
-
struct udev_enumerate *enumerator;
|
255
|
-
struct udev_list_entry *devices, *entry;
|
256
|
-
struct udev_device *udev_dev;
|
257
|
-
const char *sys_name;
|
258
|
-
int r;
|
259
|
-
|
260
|
-
assert(udev_ctx != NULL);
|
261
|
-
|
262
|
-
enumerator = udev_enumerate_new(udev_ctx);
|
263
|
-
if (NULL == enumerator) {
|
264
|
-
usbi_err(ctx, "error creating udev enumerator");
|
265
|
-
return LIBUSB_ERROR_OTHER;
|
266
|
-
}
|
267
|
-
|
268
|
-
udev_enumerate_add_match_subsystem(enumerator, "usb");
|
269
|
-
udev_enumerate_scan_devices(enumerator);
|
270
|
-
devices = udev_enumerate_get_list_entry(enumerator);
|
271
|
-
|
272
|
-
udev_list_entry_foreach(entry, devices) {
|
273
|
-
const char *path = udev_list_entry_get_name(entry);
|
274
|
-
uint8_t busnum = 0, devaddr = 0;
|
275
|
-
|
276
|
-
udev_dev = udev_device_new_from_syspath(udev_ctx, path);
|
277
|
-
|
278
|
-
r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
|
279
|
-
if (r) {
|
280
|
-
udev_device_unref(udev_dev);
|
281
|
-
continue;
|
282
|
-
}
|
283
|
-
|
284
|
-
linux_enumerate_device(ctx, busnum, devaddr, sys_name);
|
285
|
-
udev_device_unref(udev_dev);
|
286
|
-
}
|
287
|
-
|
288
|
-
udev_enumerate_unref(enumerator);
|
289
|
-
|
290
|
-
return LIBUSB_SUCCESS;
|
291
|
-
}
|
292
|
-
|
293
|
-
void linux_udev_hotplug_poll(void)
|
294
|
-
{
|
295
|
-
struct udev_device* udev_dev;
|
296
|
-
|
297
|
-
usbi_mutex_static_lock(&linux_hotplug_lock);
|
298
|
-
do {
|
299
|
-
udev_dev = udev_monitor_receive_device(udev_monitor);
|
300
|
-
if (udev_dev) {
|
301
|
-
usbi_dbg("Handling hotplug event from hotplug_poll");
|
302
|
-
udev_hotplug_event(udev_dev);
|
303
|
-
}
|
304
|
-
} while (udev_dev);
|
305
|
-
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
306
|
-
}
|
@@ -1,2692 +0,0 @@
|
|
1
|
-
/* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
|
2
|
-
/*
|
3
|
-
* Linux usbfs backend for libusb
|
4
|
-
* Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
|
5
|
-
* Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
|
6
|
-
* Copyright © 2013 Nathan Hjelm <hjelmn@mac.com>
|
7
|
-
* Copyright © 2012-2013 Hans de Goede <hdegoede@redhat.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 "config.h"
|
25
|
-
|
26
|
-
#include <assert.h>
|
27
|
-
#include <ctype.h>
|
28
|
-
#include <dirent.h>
|
29
|
-
#include <errno.h>
|
30
|
-
#include <fcntl.h>
|
31
|
-
#include <poll.h>
|
32
|
-
#include <stdio.h>
|
33
|
-
#include <stdlib.h>
|
34
|
-
#include <string.h>
|
35
|
-
#include <sys/ioctl.h>
|
36
|
-
#include <sys/stat.h>
|
37
|
-
#include <sys/types.h>
|
38
|
-
#include <sys/utsname.h>
|
39
|
-
#include <unistd.h>
|
40
|
-
|
41
|
-
#include "libusbi.h"
|
42
|
-
#include "linux_usbfs.h"
|
43
|
-
|
44
|
-
/* sysfs vs usbfs:
|
45
|
-
* opening a usbfs node causes the device to be resumed, so we attempt to
|
46
|
-
* avoid this during enumeration.
|
47
|
-
*
|
48
|
-
* sysfs allows us to read the kernel's in-memory copies of device descriptors
|
49
|
-
* and so forth, avoiding the need to open the device:
|
50
|
-
* - The binary "descriptors" file contains all config descriptors since
|
51
|
-
* 2.6.26, commit 217a9081d8e69026186067711131b77f0ce219ed
|
52
|
-
* - The binary "descriptors" file was added in 2.6.23, commit
|
53
|
-
* 69d42a78f935d19384d1f6e4f94b65bb162b36df, but it only contains the
|
54
|
-
* active config descriptors
|
55
|
-
* - The "busnum" file was added in 2.6.22, commit
|
56
|
-
* 83f7d958eab2fbc6b159ee92bf1493924e1d0f72
|
57
|
-
* - The "devnum" file has been present since pre-2.6.18
|
58
|
-
* - the "bConfigurationValue" file has been present since pre-2.6.18
|
59
|
-
*
|
60
|
-
* If we have bConfigurationValue, busnum, and devnum, then we can determine
|
61
|
-
* the active configuration without having to open the usbfs node in RDWR mode.
|
62
|
-
* The busnum file is important as that is the only way we can relate sysfs
|
63
|
-
* devices to usbfs nodes.
|
64
|
-
*
|
65
|
-
* If we also have all descriptors, we can obtain the device descriptor and
|
66
|
-
* configuration without touching usbfs at all.
|
67
|
-
*/
|
68
|
-
|
69
|
-
/* endianness for multi-byte fields:
|
70
|
-
*
|
71
|
-
* Descriptors exposed by usbfs have the multi-byte fields in the device
|
72
|
-
* descriptor as host endian. Multi-byte fields in the other descriptors are
|
73
|
-
* bus-endian. The kernel documentation says otherwise, but it is wrong.
|
74
|
-
*
|
75
|
-
* In sysfs all descriptors are bus-endian.
|
76
|
-
*/
|
77
|
-
|
78
|
-
static const char *usbfs_path = NULL;
|
79
|
-
|
80
|
-
/* use usbdev*.* device names in /dev instead of the usbfs bus directories */
|
81
|
-
static int usbdev_names = 0;
|
82
|
-
|
83
|
-
/* Linux 2.6.32 adds support for a bulk continuation URB flag. this basically
|
84
|
-
* allows us to mark URBs as being part of a specific logical transfer when
|
85
|
-
* we submit them to the kernel. then, on any error except a cancellation, all
|
86
|
-
* URBs within that transfer will be cancelled and no more URBs will be
|
87
|
-
* accepted for the transfer, meaning that no more data can creep in.
|
88
|
-
*
|
89
|
-
* The BULK_CONTINUATION flag must be set on all URBs within a bulk transfer
|
90
|
-
* (in either direction) except the first.
|
91
|
-
* For IN transfers, we must also set SHORT_NOT_OK on all URBs except the
|
92
|
-
* last; it means that the kernel should treat a short reply as an error.
|
93
|
-
* For OUT transfers, SHORT_NOT_OK must not be set. it isn't needed (OUT
|
94
|
-
* transfers can't be short unless there's already some sort of error), and
|
95
|
-
* setting this flag is disallowed (a kernel with USB debugging enabled will
|
96
|
-
* reject such URBs).
|
97
|
-
*/
|
98
|
-
static int supports_flag_bulk_continuation = -1;
|
99
|
-
|
100
|
-
/* Linux 2.6.31 fixes support for the zero length packet URB flag. This
|
101
|
-
* allows us to mark URBs that should be followed by a zero length data
|
102
|
-
* packet, which can be required by device- or class-specific protocols.
|
103
|
-
*/
|
104
|
-
static int supports_flag_zero_packet = -1;
|
105
|
-
|
106
|
-
/* clock ID for monotonic clock, as not all clock sources are available on all
|
107
|
-
* systems. appropriate choice made at initialization time. */
|
108
|
-
static clockid_t monotonic_clkid = -1;
|
109
|
-
|
110
|
-
/* Linux 2.6.22 (commit 83f7d958eab2fbc6b159ee92bf1493924e1d0f72) adds a busnum
|
111
|
-
* to sysfs, so we can relate devices. This also implies that we can read
|
112
|
-
* the active configuration through bConfigurationValue */
|
113
|
-
static int sysfs_can_relate_devices = -1;
|
114
|
-
|
115
|
-
/* Linux 2.6.26 (commit 217a9081d8e69026186067711131b77f0ce219ed) adds all
|
116
|
-
* config descriptors (rather then just the active config) to the sysfs
|
117
|
-
* descriptors file, so from then on we can use them. */
|
118
|
-
static int sysfs_has_descriptors = -1;
|
119
|
-
|
120
|
-
/* how many times have we initted (and not exited) ? */
|
121
|
-
static int init_count = 0;
|
122
|
-
|
123
|
-
/* Serialize hotplug start/stop */
|
124
|
-
usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER;
|
125
|
-
/* Serialize scan-devices, event-thread, and poll */
|
126
|
-
usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
|
127
|
-
|
128
|
-
static int linux_start_event_monitor(void);
|
129
|
-
static int linux_stop_event_monitor(void);
|
130
|
-
static int linux_scan_devices(struct libusb_context *ctx);
|
131
|
-
static int sysfs_scan_device(struct libusb_context *ctx, const char *devname);
|
132
|
-
static int detach_kernel_driver_and_claim(struct libusb_device_handle *, int);
|
133
|
-
|
134
|
-
#if !defined(USE_UDEV)
|
135
|
-
static int linux_default_scan_devices (struct libusb_context *ctx);
|
136
|
-
#endif
|
137
|
-
|
138
|
-
struct linux_device_priv {
|
139
|
-
char *sysfs_dir;
|
140
|
-
unsigned char *descriptors;
|
141
|
-
int descriptors_len;
|
142
|
-
int active_config; /* cache val for !sysfs_can_relate_devices */
|
143
|
-
};
|
144
|
-
|
145
|
-
struct linux_device_handle_priv {
|
146
|
-
int fd;
|
147
|
-
uint32_t caps;
|
148
|
-
};
|
149
|
-
|
150
|
-
enum reap_action {
|
151
|
-
NORMAL = 0,
|
152
|
-
/* submission failed after the first URB, so await cancellation/completion
|
153
|
-
* of all the others */
|
154
|
-
SUBMIT_FAILED,
|
155
|
-
|
156
|
-
/* cancelled by user or timeout */
|
157
|
-
CANCELLED,
|
158
|
-
|
159
|
-
/* completed multi-URB transfer in non-final URB */
|
160
|
-
COMPLETED_EARLY,
|
161
|
-
|
162
|
-
/* one or more urbs encountered a low-level error */
|
163
|
-
ERROR,
|
164
|
-
};
|
165
|
-
|
166
|
-
struct linux_transfer_priv {
|
167
|
-
union {
|
168
|
-
struct usbfs_urb *urbs;
|
169
|
-
struct usbfs_urb **iso_urbs;
|
170
|
-
};
|
171
|
-
|
172
|
-
enum reap_action reap_action;
|
173
|
-
int num_urbs;
|
174
|
-
int num_retired;
|
175
|
-
enum libusb_transfer_status reap_status;
|
176
|
-
|
177
|
-
/* next iso packet in user-supplied transfer to be populated */
|
178
|
-
int iso_packet_offset;
|
179
|
-
};
|
180
|
-
|
181
|
-
static int _get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
|
182
|
-
{
|
183
|
-
struct libusb_context *ctx = DEVICE_CTX(dev);
|
184
|
-
char path[PATH_MAX];
|
185
|
-
int fd;
|
186
|
-
int delay = 10000;
|
187
|
-
|
188
|
-
if (usbdev_names)
|
189
|
-
snprintf(path, PATH_MAX, "%s/usbdev%d.%d",
|
190
|
-
usbfs_path, dev->bus_number, dev->device_address);
|
191
|
-
else
|
192
|
-
snprintf(path, PATH_MAX, "%s/%03d/%03d",
|
193
|
-
usbfs_path, dev->bus_number, dev->device_address);
|
194
|
-
|
195
|
-
fd = open(path, mode);
|
196
|
-
if (fd != -1)
|
197
|
-
return fd; /* Success */
|
198
|
-
|
199
|
-
if (errno == ENOENT) {
|
200
|
-
if (!silent)
|
201
|
-
usbi_err(ctx, "File doesn't exist, wait %d ms and try again", delay/1000);
|
202
|
-
|
203
|
-
/* Wait 10ms for USB device path creation.*/
|
204
|
-
usleep(delay);
|
205
|
-
|
206
|
-
fd = open(path, mode);
|
207
|
-
if (fd != -1)
|
208
|
-
return fd; /* Success */
|
209
|
-
}
|
210
|
-
|
211
|
-
if (!silent) {
|
212
|
-
usbi_err(ctx, "libusb couldn't open USB device %s: %s",
|
213
|
-
path, strerror(errno));
|
214
|
-
if (errno == EACCES && mode == O_RDWR)
|
215
|
-
usbi_err(ctx, "libusb requires write access to USB "
|
216
|
-
"device nodes.");
|
217
|
-
}
|
218
|
-
|
219
|
-
if (errno == EACCES)
|
220
|
-
return LIBUSB_ERROR_ACCESS;
|
221
|
-
if (errno == ENOENT)
|
222
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
223
|
-
return LIBUSB_ERROR_IO;
|
224
|
-
}
|
225
|
-
|
226
|
-
static struct linux_device_priv *_device_priv(struct libusb_device *dev)
|
227
|
-
{
|
228
|
-
return (struct linux_device_priv *) dev->os_priv;
|
229
|
-
}
|
230
|
-
|
231
|
-
static struct linux_device_handle_priv *_device_handle_priv(
|
232
|
-
struct libusb_device_handle *handle)
|
233
|
-
{
|
234
|
-
return (struct linux_device_handle_priv *) handle->os_priv;
|
235
|
-
}
|
236
|
-
|
237
|
-
/* check dirent for a /dev/usbdev%d.%d name
|
238
|
-
* optionally return bus/device on success */
|
239
|
-
static int _is_usbdev_entry(struct dirent *entry, int *bus_p, int *dev_p)
|
240
|
-
{
|
241
|
-
int busnum, devnum;
|
242
|
-
|
243
|
-
if (sscanf(entry->d_name, "usbdev%d.%d", &busnum, &devnum) != 2)
|
244
|
-
return 0;
|
245
|
-
|
246
|
-
usbi_dbg("found: %s", entry->d_name);
|
247
|
-
if (bus_p != NULL)
|
248
|
-
*bus_p = busnum;
|
249
|
-
if (dev_p != NULL)
|
250
|
-
*dev_p = devnum;
|
251
|
-
return 1;
|
252
|
-
}
|
253
|
-
|
254
|
-
static int check_usb_vfs(const char *dirname)
|
255
|
-
{
|
256
|
-
DIR *dir;
|
257
|
-
struct dirent *entry;
|
258
|
-
int found = 0;
|
259
|
-
|
260
|
-
dir = opendir(dirname);
|
261
|
-
if (!dir)
|
262
|
-
return 0;
|
263
|
-
|
264
|
-
while ((entry = readdir(dir)) != NULL) {
|
265
|
-
if (entry->d_name[0] == '.')
|
266
|
-
continue;
|
267
|
-
|
268
|
-
/* We assume if we find any files that it must be the right place */
|
269
|
-
found = 1;
|
270
|
-
break;
|
271
|
-
}
|
272
|
-
|
273
|
-
closedir(dir);
|
274
|
-
return found;
|
275
|
-
}
|
276
|
-
|
277
|
-
static const char *find_usbfs_path(void)
|
278
|
-
{
|
279
|
-
const char *path = "/dev/bus/usb";
|
280
|
-
const char *ret = NULL;
|
281
|
-
|
282
|
-
if (check_usb_vfs(path)) {
|
283
|
-
ret = path;
|
284
|
-
} else {
|
285
|
-
path = "/proc/bus/usb";
|
286
|
-
if (check_usb_vfs(path))
|
287
|
-
ret = path;
|
288
|
-
}
|
289
|
-
|
290
|
-
/* look for /dev/usbdev*.* if the normal places fail */
|
291
|
-
if (ret == NULL) {
|
292
|
-
struct dirent *entry;
|
293
|
-
DIR *dir;
|
294
|
-
|
295
|
-
path = "/dev";
|
296
|
-
dir = opendir(path);
|
297
|
-
if (dir != NULL) {
|
298
|
-
while ((entry = readdir(dir)) != NULL) {
|
299
|
-
if (_is_usbdev_entry(entry, NULL, NULL)) {
|
300
|
-
/* found one; that's enough */
|
301
|
-
ret = path;
|
302
|
-
usbdev_names = 1;
|
303
|
-
break;
|
304
|
-
}
|
305
|
-
}
|
306
|
-
closedir(dir);
|
307
|
-
}
|
308
|
-
}
|
309
|
-
|
310
|
-
/* On udev based systems without any usb-devices /dev/bus/usb will not
|
311
|
-
* exist. So if we've not found anything and we're using udev for hotplug
|
312
|
-
* simply assume /dev/bus/usb rather then making libusb_init fail. */
|
313
|
-
#if defined(USE_UDEV)
|
314
|
-
if (ret == NULL)
|
315
|
-
ret = "/dev/bus/usb";
|
316
|
-
#endif
|
317
|
-
|
318
|
-
if (ret != NULL)
|
319
|
-
usbi_dbg("found usbfs at %s", ret);
|
320
|
-
|
321
|
-
return ret;
|
322
|
-
}
|
323
|
-
|
324
|
-
/* the monotonic clock is not usable on all systems (e.g. embedded ones often
|
325
|
-
* seem to lack it). fall back to REALTIME if we have to. */
|
326
|
-
static clockid_t find_monotonic_clock(void)
|
327
|
-
{
|
328
|
-
#ifdef CLOCK_MONOTONIC
|
329
|
-
struct timespec ts;
|
330
|
-
int r;
|
331
|
-
|
332
|
-
/* Linux 2.6.28 adds CLOCK_MONOTONIC_RAW but we don't use it
|
333
|
-
* because it's not available through timerfd */
|
334
|
-
r = clock_gettime(CLOCK_MONOTONIC, &ts);
|
335
|
-
if (r == 0)
|
336
|
-
return CLOCK_MONOTONIC;
|
337
|
-
usbi_dbg("monotonic clock doesn't work, errno %d", errno);
|
338
|
-
#endif
|
339
|
-
|
340
|
-
return CLOCK_REALTIME;
|
341
|
-
}
|
342
|
-
|
343
|
-
static int kernel_version_ge(int major, int minor, int sublevel)
|
344
|
-
{
|
345
|
-
struct utsname uts;
|
346
|
-
int atoms, kmajor, kminor, ksublevel;
|
347
|
-
|
348
|
-
if (uname(&uts) < 0)
|
349
|
-
return -1;
|
350
|
-
atoms = sscanf(uts.release, "%d.%d.%d", &kmajor, &kminor, &ksublevel);
|
351
|
-
if (atoms < 1)
|
352
|
-
return -1;
|
353
|
-
|
354
|
-
if (kmajor > major)
|
355
|
-
return 1;
|
356
|
-
if (kmajor < major)
|
357
|
-
return 0;
|
358
|
-
|
359
|
-
/* kmajor == major */
|
360
|
-
if (atoms < 2)
|
361
|
-
return 0 == minor && 0 == sublevel;
|
362
|
-
if (kminor > minor)
|
363
|
-
return 1;
|
364
|
-
if (kminor < minor)
|
365
|
-
return 0;
|
366
|
-
|
367
|
-
/* kminor == minor */
|
368
|
-
if (atoms < 3)
|
369
|
-
return 0 == sublevel;
|
370
|
-
|
371
|
-
return ksublevel >= sublevel;
|
372
|
-
}
|
373
|
-
|
374
|
-
static int op_init(struct libusb_context *ctx)
|
375
|
-
{
|
376
|
-
struct stat statbuf;
|
377
|
-
int r;
|
378
|
-
|
379
|
-
usbfs_path = find_usbfs_path();
|
380
|
-
if (!usbfs_path) {
|
381
|
-
usbi_err(ctx, "could not find usbfs");
|
382
|
-
return LIBUSB_ERROR_OTHER;
|
383
|
-
}
|
384
|
-
|
385
|
-
if (monotonic_clkid == -1)
|
386
|
-
monotonic_clkid = find_monotonic_clock();
|
387
|
-
|
388
|
-
if (supports_flag_bulk_continuation == -1) {
|
389
|
-
/* bulk continuation URB flag available from Linux 2.6.32 */
|
390
|
-
supports_flag_bulk_continuation = kernel_version_ge(2,6,32);
|
391
|
-
if (supports_flag_bulk_continuation == -1) {
|
392
|
-
usbi_err(ctx, "error checking for bulk continuation support");
|
393
|
-
return LIBUSB_ERROR_OTHER;
|
394
|
-
}
|
395
|
-
}
|
396
|
-
|
397
|
-
if (supports_flag_bulk_continuation)
|
398
|
-
usbi_dbg("bulk continuation flag supported");
|
399
|
-
|
400
|
-
if (-1 == supports_flag_zero_packet) {
|
401
|
-
/* zero length packet URB flag fixed since Linux 2.6.31 */
|
402
|
-
supports_flag_zero_packet = kernel_version_ge(2,6,31);
|
403
|
-
if (-1 == supports_flag_zero_packet) {
|
404
|
-
usbi_err(ctx, "error checking for zero length packet support");
|
405
|
-
return LIBUSB_ERROR_OTHER;
|
406
|
-
}
|
407
|
-
}
|
408
|
-
|
409
|
-
if (supports_flag_zero_packet)
|
410
|
-
usbi_dbg("zero length packet flag supported");
|
411
|
-
|
412
|
-
if (-1 == sysfs_has_descriptors) {
|
413
|
-
/* sysfs descriptors has all descriptors since Linux 2.6.26 */
|
414
|
-
sysfs_has_descriptors = kernel_version_ge(2,6,26);
|
415
|
-
if (-1 == sysfs_has_descriptors) {
|
416
|
-
usbi_err(ctx, "error checking for sysfs descriptors");
|
417
|
-
return LIBUSB_ERROR_OTHER;
|
418
|
-
}
|
419
|
-
}
|
420
|
-
|
421
|
-
if (-1 == sysfs_can_relate_devices) {
|
422
|
-
/* sysfs has busnum since Linux 2.6.22 */
|
423
|
-
sysfs_can_relate_devices = kernel_version_ge(2,6,22);
|
424
|
-
if (-1 == sysfs_can_relate_devices) {
|
425
|
-
usbi_err(ctx, "error checking for sysfs busnum");
|
426
|
-
return LIBUSB_ERROR_OTHER;
|
427
|
-
}
|
428
|
-
}
|
429
|
-
|
430
|
-
if (sysfs_can_relate_devices || sysfs_has_descriptors) {
|
431
|
-
r = stat(SYSFS_DEVICE_PATH, &statbuf);
|
432
|
-
if (r != 0 || !S_ISDIR(statbuf.st_mode)) {
|
433
|
-
usbi_warn(ctx, "sysfs not mounted");
|
434
|
-
sysfs_can_relate_devices = 0;
|
435
|
-
sysfs_has_descriptors = 0;
|
436
|
-
}
|
437
|
-
}
|
438
|
-
|
439
|
-
if (sysfs_can_relate_devices)
|
440
|
-
usbi_dbg("sysfs can relate devices");
|
441
|
-
|
442
|
-
if (sysfs_has_descriptors)
|
443
|
-
usbi_dbg("sysfs has complete descriptors");
|
444
|
-
|
445
|
-
usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
|
446
|
-
r = LIBUSB_SUCCESS;
|
447
|
-
if (init_count == 0) {
|
448
|
-
/* start up hotplug event handler */
|
449
|
-
r = linux_start_event_monitor();
|
450
|
-
}
|
451
|
-
if (r == LIBUSB_SUCCESS) {
|
452
|
-
r = linux_scan_devices(ctx);
|
453
|
-
if (r == LIBUSB_SUCCESS)
|
454
|
-
init_count++;
|
455
|
-
else if (init_count == 0)
|
456
|
-
linux_stop_event_monitor();
|
457
|
-
} else
|
458
|
-
usbi_err(ctx, "error starting hotplug event monitor");
|
459
|
-
usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
|
460
|
-
|
461
|
-
return r;
|
462
|
-
}
|
463
|
-
|
464
|
-
static void op_exit(void)
|
465
|
-
{
|
466
|
-
usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
|
467
|
-
assert(init_count != 0);
|
468
|
-
if (!--init_count) {
|
469
|
-
/* tear down event handler */
|
470
|
-
(void)linux_stop_event_monitor();
|
471
|
-
}
|
472
|
-
usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
|
473
|
-
}
|
474
|
-
|
475
|
-
static int linux_start_event_monitor(void)
|
476
|
-
{
|
477
|
-
#if defined(USE_UDEV)
|
478
|
-
return linux_udev_start_event_monitor();
|
479
|
-
#else
|
480
|
-
return linux_netlink_start_event_monitor();
|
481
|
-
#endif
|
482
|
-
}
|
483
|
-
|
484
|
-
static int linux_stop_event_monitor(void)
|
485
|
-
{
|
486
|
-
#if defined(USE_UDEV)
|
487
|
-
return linux_udev_stop_event_monitor();
|
488
|
-
#else
|
489
|
-
return linux_netlink_stop_event_monitor();
|
490
|
-
#endif
|
491
|
-
}
|
492
|
-
|
493
|
-
static int linux_scan_devices(struct libusb_context *ctx)
|
494
|
-
{
|
495
|
-
int ret;
|
496
|
-
|
497
|
-
usbi_mutex_static_lock(&linux_hotplug_lock);
|
498
|
-
|
499
|
-
#if defined(USE_UDEV)
|
500
|
-
ret = linux_udev_scan_devices(ctx);
|
501
|
-
#else
|
502
|
-
ret = linux_default_scan_devices(ctx);
|
503
|
-
#endif
|
504
|
-
|
505
|
-
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
506
|
-
|
507
|
-
return ret;
|
508
|
-
}
|
509
|
-
|
510
|
-
static void op_hotplug_poll(void)
|
511
|
-
{
|
512
|
-
#if defined(USE_UDEV)
|
513
|
-
linux_udev_hotplug_poll();
|
514
|
-
#else
|
515
|
-
linux_netlink_hotplug_poll();
|
516
|
-
#endif
|
517
|
-
}
|
518
|
-
|
519
|
-
static int _open_sysfs_attr(struct libusb_device *dev, const char *attr)
|
520
|
-
{
|
521
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
522
|
-
char filename[PATH_MAX];
|
523
|
-
int fd;
|
524
|
-
|
525
|
-
snprintf(filename, PATH_MAX, "%s/%s/%s",
|
526
|
-
SYSFS_DEVICE_PATH, priv->sysfs_dir, attr);
|
527
|
-
fd = open(filename, O_RDONLY);
|
528
|
-
if (fd < 0) {
|
529
|
-
usbi_err(DEVICE_CTX(dev),
|
530
|
-
"open %s failed ret=%d errno=%d", filename, fd, errno);
|
531
|
-
return LIBUSB_ERROR_IO;
|
532
|
-
}
|
533
|
-
|
534
|
-
return fd;
|
535
|
-
}
|
536
|
-
|
537
|
-
/* Note only suitable for attributes which always read >= 0, < 0 is error */
|
538
|
-
static int __read_sysfs_attr(struct libusb_context *ctx,
|
539
|
-
const char *devname, const char *attr)
|
540
|
-
{
|
541
|
-
char filename[PATH_MAX];
|
542
|
-
FILE *f;
|
543
|
-
int r, value;
|
544
|
-
|
545
|
-
snprintf(filename, PATH_MAX, "%s/%s/%s", SYSFS_DEVICE_PATH,
|
546
|
-
devname, attr);
|
547
|
-
f = fopen(filename, "r");
|
548
|
-
if (f == NULL) {
|
549
|
-
if (errno == ENOENT) {
|
550
|
-
/* File doesn't exist. Assume the device has been
|
551
|
-
disconnected (see trac ticket #70). */
|
552
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
553
|
-
}
|
554
|
-
usbi_err(ctx, "open %s failed errno=%d", filename, errno);
|
555
|
-
return LIBUSB_ERROR_IO;
|
556
|
-
}
|
557
|
-
|
558
|
-
r = fscanf(f, "%d", &value);
|
559
|
-
fclose(f);
|
560
|
-
if (r != 1) {
|
561
|
-
usbi_err(ctx, "fscanf %s returned %d, errno=%d", attr, r, errno);
|
562
|
-
return LIBUSB_ERROR_NO_DEVICE; /* For unplug race (trac #70) */
|
563
|
-
}
|
564
|
-
if (value < 0) {
|
565
|
-
usbi_err(ctx, "%s contains a negative value", filename);
|
566
|
-
return LIBUSB_ERROR_IO;
|
567
|
-
}
|
568
|
-
|
569
|
-
return value;
|
570
|
-
}
|
571
|
-
|
572
|
-
static int op_get_device_descriptor(struct libusb_device *dev,
|
573
|
-
unsigned char *buffer, int *host_endian)
|
574
|
-
{
|
575
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
576
|
-
|
577
|
-
*host_endian = sysfs_has_descriptors ? 0 : 1;
|
578
|
-
memcpy(buffer, priv->descriptors, DEVICE_DESC_LENGTH);
|
579
|
-
|
580
|
-
return 0;
|
581
|
-
}
|
582
|
-
|
583
|
-
/* read the bConfigurationValue for a device */
|
584
|
-
static int sysfs_get_active_config(struct libusb_device *dev, int *config)
|
585
|
-
{
|
586
|
-
char *endptr;
|
587
|
-
char tmp[5] = {0, 0, 0, 0, 0};
|
588
|
-
long num;
|
589
|
-
int fd;
|
590
|
-
ssize_t r;
|
591
|
-
|
592
|
-
fd = _open_sysfs_attr(dev, "bConfigurationValue");
|
593
|
-
if (fd < 0)
|
594
|
-
return fd;
|
595
|
-
|
596
|
-
r = read(fd, tmp, sizeof(tmp));
|
597
|
-
close(fd);
|
598
|
-
if (r < 0) {
|
599
|
-
usbi_err(DEVICE_CTX(dev),
|
600
|
-
"read bConfigurationValue failed ret=%d errno=%d", r, errno);
|
601
|
-
return LIBUSB_ERROR_IO;
|
602
|
-
} else if (r == 0) {
|
603
|
-
usbi_dbg("device unconfigured");
|
604
|
-
*config = -1;
|
605
|
-
return 0;
|
606
|
-
}
|
607
|
-
|
608
|
-
if (tmp[sizeof(tmp) - 1] != 0) {
|
609
|
-
usbi_err(DEVICE_CTX(dev), "not null-terminated?");
|
610
|
-
return LIBUSB_ERROR_IO;
|
611
|
-
} else if (tmp[0] == 0) {
|
612
|
-
usbi_err(DEVICE_CTX(dev), "no configuration value?");
|
613
|
-
return LIBUSB_ERROR_IO;
|
614
|
-
}
|
615
|
-
|
616
|
-
num = strtol(tmp, &endptr, 10);
|
617
|
-
if (endptr == tmp) {
|
618
|
-
usbi_err(DEVICE_CTX(dev), "error converting '%s' to integer", tmp);
|
619
|
-
return LIBUSB_ERROR_IO;
|
620
|
-
}
|
621
|
-
|
622
|
-
*config = (int) num;
|
623
|
-
return 0;
|
624
|
-
}
|
625
|
-
|
626
|
-
int linux_get_device_address (struct libusb_context *ctx, int detached,
|
627
|
-
uint8_t *busnum, uint8_t *devaddr,const char *dev_node,
|
628
|
-
const char *sys_name)
|
629
|
-
{
|
630
|
-
int sysfs_attr;
|
631
|
-
|
632
|
-
usbi_dbg("getting address for device: %s detached: %d", sys_name, detached);
|
633
|
-
/* can't use sysfs to read the bus and device number if the
|
634
|
-
* device has been detached */
|
635
|
-
if (!sysfs_can_relate_devices || detached || NULL == sys_name) {
|
636
|
-
if (NULL == dev_node) {
|
637
|
-
return LIBUSB_ERROR_OTHER;
|
638
|
-
}
|
639
|
-
|
640
|
-
/* will this work with all supported kernel versions? */
|
641
|
-
if (!strncmp(dev_node, "/dev/bus/usb", 12)) {
|
642
|
-
sscanf (dev_node, "/dev/bus/usb/%hhu/%hhu", busnum, devaddr);
|
643
|
-
} else if (!strncmp(dev_node, "/proc/bus/usb", 13)) {
|
644
|
-
sscanf (dev_node, "/proc/bus/usb/%hhu/%hhu", busnum, devaddr);
|
645
|
-
}
|
646
|
-
|
647
|
-
return LIBUSB_SUCCESS;
|
648
|
-
}
|
649
|
-
|
650
|
-
usbi_dbg("scan %s", sys_name);
|
651
|
-
|
652
|
-
sysfs_attr = __read_sysfs_attr(ctx, sys_name, "busnum");
|
653
|
-
if (0 > sysfs_attr)
|
654
|
-
return sysfs_attr;
|
655
|
-
if (sysfs_attr > 255)
|
656
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
657
|
-
*busnum = (uint8_t) sysfs_attr;
|
658
|
-
|
659
|
-
sysfs_attr = __read_sysfs_attr(ctx, sys_name, "devnum");
|
660
|
-
if (0 > sysfs_attr)
|
661
|
-
return sysfs_attr;
|
662
|
-
if (sysfs_attr > 255)
|
663
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
664
|
-
|
665
|
-
*devaddr = (uint8_t) sysfs_attr;
|
666
|
-
|
667
|
-
usbi_dbg("bus=%d dev=%d", *busnum, *devaddr);
|
668
|
-
|
669
|
-
return LIBUSB_SUCCESS;
|
670
|
-
}
|
671
|
-
|
672
|
-
/* Return offset of the next descriptor with the given type */
|
673
|
-
static int seek_to_next_descriptor(struct libusb_context *ctx,
|
674
|
-
uint8_t descriptor_type, unsigned char *buffer, int size)
|
675
|
-
{
|
676
|
-
struct usb_descriptor_header header;
|
677
|
-
int i;
|
678
|
-
|
679
|
-
for (i = 0; size >= 0; i += header.bLength, size -= header.bLength) {
|
680
|
-
if (size == 0)
|
681
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
682
|
-
|
683
|
-
if (size < 2) {
|
684
|
-
usbi_err(ctx, "short descriptor read %d/2", size);
|
685
|
-
return LIBUSB_ERROR_IO;
|
686
|
-
}
|
687
|
-
usbi_parse_descriptor(buffer + i, "bb", &header, 0);
|
688
|
-
|
689
|
-
if (i && header.bDescriptorType == descriptor_type)
|
690
|
-
return i;
|
691
|
-
}
|
692
|
-
usbi_err(ctx, "bLength overflow by %d bytes", -size);
|
693
|
-
return LIBUSB_ERROR_IO;
|
694
|
-
}
|
695
|
-
|
696
|
-
/* Return offset to next config */
|
697
|
-
static int seek_to_next_config(struct libusb_context *ctx,
|
698
|
-
unsigned char *buffer, int size)
|
699
|
-
{
|
700
|
-
struct libusb_config_descriptor config;
|
701
|
-
|
702
|
-
if (size == 0)
|
703
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
704
|
-
|
705
|
-
if (size < LIBUSB_DT_CONFIG_SIZE) {
|
706
|
-
usbi_err(ctx, "short descriptor read %d/%d",
|
707
|
-
size, LIBUSB_DT_CONFIG_SIZE);
|
708
|
-
return LIBUSB_ERROR_IO;
|
709
|
-
}
|
710
|
-
|
711
|
-
usbi_parse_descriptor(buffer, "bbwbbbbb", &config, 0);
|
712
|
-
if (config.bDescriptorType != LIBUSB_DT_CONFIG) {
|
713
|
-
usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
|
714
|
-
config.bDescriptorType);
|
715
|
-
return LIBUSB_ERROR_IO;
|
716
|
-
}
|
717
|
-
|
718
|
-
/*
|
719
|
-
* In usbfs the config descriptors are config.wTotalLength bytes apart,
|
720
|
-
* with any short reads from the device appearing as holes in the file.
|
721
|
-
*
|
722
|
-
* In sysfs wTotalLength is ignored, instead the kernel returns a
|
723
|
-
* config descriptor with verified bLength fields, with descriptors
|
724
|
-
* with an invalid bLength removed.
|
725
|
-
*/
|
726
|
-
if (sysfs_has_descriptors) {
|
727
|
-
int next = seek_to_next_descriptor(ctx, LIBUSB_DT_CONFIG,
|
728
|
-
buffer, size);
|
729
|
-
if (next == LIBUSB_ERROR_NOT_FOUND)
|
730
|
-
next = size;
|
731
|
-
if (next < 0)
|
732
|
-
return next;
|
733
|
-
|
734
|
-
if (next != config.wTotalLength)
|
735
|
-
usbi_warn(ctx, "config length mismatch wTotalLength "
|
736
|
-
"%d real %d", config.wTotalLength, next);
|
737
|
-
return next;
|
738
|
-
} else {
|
739
|
-
if (config.wTotalLength < LIBUSB_DT_CONFIG_SIZE) {
|
740
|
-
usbi_err(ctx, "invalid wTotalLength %d",
|
741
|
-
config.wTotalLength);
|
742
|
-
return LIBUSB_ERROR_IO;
|
743
|
-
} else if (config.wTotalLength > size) {
|
744
|
-
usbi_warn(ctx, "short descriptor read %d/%d",
|
745
|
-
size, config.wTotalLength);
|
746
|
-
return size;
|
747
|
-
} else
|
748
|
-
return config.wTotalLength;
|
749
|
-
}
|
750
|
-
}
|
751
|
-
|
752
|
-
static int op_get_config_descriptor_by_value(struct libusb_device *dev,
|
753
|
-
uint8_t value, unsigned char **buffer, int *host_endian)
|
754
|
-
{
|
755
|
-
struct libusb_context *ctx = DEVICE_CTX(dev);
|
756
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
757
|
-
unsigned char *descriptors = priv->descriptors;
|
758
|
-
int size = priv->descriptors_len;
|
759
|
-
struct libusb_config_descriptor *config;
|
760
|
-
|
761
|
-
*buffer = NULL;
|
762
|
-
/* Unlike the device desc. config descs. are always in raw format */
|
763
|
-
*host_endian = 0;
|
764
|
-
|
765
|
-
/* Skip device header */
|
766
|
-
descriptors += DEVICE_DESC_LENGTH;
|
767
|
-
size -= DEVICE_DESC_LENGTH;
|
768
|
-
|
769
|
-
/* Seek till the config is found, or till "EOF" */
|
770
|
-
while (1) {
|
771
|
-
int next = seek_to_next_config(ctx, descriptors, size);
|
772
|
-
if (next < 0)
|
773
|
-
return next;
|
774
|
-
config = (struct libusb_config_descriptor *)descriptors;
|
775
|
-
if (config->bConfigurationValue == value) {
|
776
|
-
*buffer = descriptors;
|
777
|
-
return next;
|
778
|
-
}
|
779
|
-
size -= next;
|
780
|
-
descriptors += next;
|
781
|
-
}
|
782
|
-
}
|
783
|
-
|
784
|
-
static int op_get_active_config_descriptor(struct libusb_device *dev,
|
785
|
-
unsigned char *buffer, size_t len, int *host_endian)
|
786
|
-
{
|
787
|
-
int r, config;
|
788
|
-
unsigned char *config_desc;
|
789
|
-
|
790
|
-
if (sysfs_can_relate_devices) {
|
791
|
-
r = sysfs_get_active_config(dev, &config);
|
792
|
-
if (r < 0)
|
793
|
-
return r;
|
794
|
-
} else {
|
795
|
-
/* Use cached bConfigurationValue */
|
796
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
797
|
-
config = priv->active_config;
|
798
|
-
}
|
799
|
-
if (config == -1)
|
800
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
801
|
-
|
802
|
-
r = op_get_config_descriptor_by_value(dev, config, &config_desc,
|
803
|
-
host_endian);
|
804
|
-
if (r < 0)
|
805
|
-
return r;
|
806
|
-
|
807
|
-
len = MIN(len, r);
|
808
|
-
memcpy(buffer, config_desc, len);
|
809
|
-
return len;
|
810
|
-
}
|
811
|
-
|
812
|
-
static int op_get_config_descriptor(struct libusb_device *dev,
|
813
|
-
uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
|
814
|
-
{
|
815
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
816
|
-
unsigned char *descriptors = priv->descriptors;
|
817
|
-
int i, r, size = priv->descriptors_len;
|
818
|
-
|
819
|
-
/* Unlike the device desc. config descs. are always in raw format */
|
820
|
-
*host_endian = 0;
|
821
|
-
|
822
|
-
/* Skip device header */
|
823
|
-
descriptors += DEVICE_DESC_LENGTH;
|
824
|
-
size -= DEVICE_DESC_LENGTH;
|
825
|
-
|
826
|
-
/* Seek till the config is found, or till "EOF" */
|
827
|
-
for (i = 0; ; i++) {
|
828
|
-
r = seek_to_next_config(DEVICE_CTX(dev), descriptors, size);
|
829
|
-
if (r < 0)
|
830
|
-
return r;
|
831
|
-
if (i == config_index)
|
832
|
-
break;
|
833
|
-
size -= r;
|
834
|
-
descriptors += r;
|
835
|
-
}
|
836
|
-
|
837
|
-
len = MIN(len, r);
|
838
|
-
memcpy(buffer, descriptors, len);
|
839
|
-
return len;
|
840
|
-
}
|
841
|
-
|
842
|
-
/* send a control message to retrieve active configuration */
|
843
|
-
static int usbfs_get_active_config(struct libusb_device *dev, int fd)
|
844
|
-
{
|
845
|
-
unsigned char active_config = 0;
|
846
|
-
int r;
|
847
|
-
|
848
|
-
struct usbfs_ctrltransfer ctrl = {
|
849
|
-
.bmRequestType = LIBUSB_ENDPOINT_IN,
|
850
|
-
.bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
|
851
|
-
.wValue = 0,
|
852
|
-
.wIndex = 0,
|
853
|
-
.wLength = 1,
|
854
|
-
.timeout = 1000,
|
855
|
-
.data = &active_config
|
856
|
-
};
|
857
|
-
|
858
|
-
r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
|
859
|
-
if (r < 0) {
|
860
|
-
if (errno == ENODEV)
|
861
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
862
|
-
|
863
|
-
/* we hit this error path frequently with buggy devices :( */
|
864
|
-
usbi_warn(DEVICE_CTX(dev),
|
865
|
-
"get_configuration failed ret=%d errno=%d", r, errno);
|
866
|
-
return LIBUSB_ERROR_IO;
|
867
|
-
}
|
868
|
-
|
869
|
-
return active_config;
|
870
|
-
}
|
871
|
-
|
872
|
-
static int initialize_device(struct libusb_device *dev, uint8_t busnum,
|
873
|
-
uint8_t devaddr, const char *sysfs_dir)
|
874
|
-
{
|
875
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
876
|
-
struct libusb_context *ctx = DEVICE_CTX(dev);
|
877
|
-
int descriptors_size = 512; /* Begin with a 1024 byte alloc */
|
878
|
-
int fd, speed;
|
879
|
-
ssize_t r;
|
880
|
-
|
881
|
-
dev->bus_number = busnum;
|
882
|
-
dev->device_address = devaddr;
|
883
|
-
|
884
|
-
if (sysfs_dir) {
|
885
|
-
priv->sysfs_dir = malloc(strlen(sysfs_dir) + 1);
|
886
|
-
if (!priv->sysfs_dir)
|
887
|
-
return LIBUSB_ERROR_NO_MEM;
|
888
|
-
strcpy(priv->sysfs_dir, sysfs_dir);
|
889
|
-
|
890
|
-
/* Note speed can contain 1.5, in this case __read_sysfs_attr
|
891
|
-
will stop parsing at the '.' and return 1 */
|
892
|
-
speed = __read_sysfs_attr(DEVICE_CTX(dev), sysfs_dir, "speed");
|
893
|
-
if (speed >= 0) {
|
894
|
-
switch (speed) {
|
895
|
-
case 1: dev->speed = LIBUSB_SPEED_LOW; break;
|
896
|
-
case 12: dev->speed = LIBUSB_SPEED_FULL; break;
|
897
|
-
case 480: dev->speed = LIBUSB_SPEED_HIGH; break;
|
898
|
-
case 5000: dev->speed = LIBUSB_SPEED_SUPER; break;
|
899
|
-
default:
|
900
|
-
usbi_warn(DEVICE_CTX(dev), "Unknown device speed: %d Mbps", speed);
|
901
|
-
}
|
902
|
-
}
|
903
|
-
}
|
904
|
-
|
905
|
-
/* cache descriptors in memory */
|
906
|
-
if (sysfs_has_descriptors)
|
907
|
-
fd = _open_sysfs_attr(dev, "descriptors");
|
908
|
-
else
|
909
|
-
fd = _get_usbfs_fd(dev, O_RDONLY, 0);
|
910
|
-
if (fd < 0)
|
911
|
-
return fd;
|
912
|
-
|
913
|
-
do {
|
914
|
-
descriptors_size *= 2;
|
915
|
-
priv->descriptors = usbi_reallocf(priv->descriptors,
|
916
|
-
descriptors_size);
|
917
|
-
if (!priv->descriptors) {
|
918
|
-
close(fd);
|
919
|
-
return LIBUSB_ERROR_NO_MEM;
|
920
|
-
}
|
921
|
-
/* usbfs has holes in the file */
|
922
|
-
if (!sysfs_has_descriptors) {
|
923
|
-
memset(priv->descriptors + priv->descriptors_len,
|
924
|
-
0, descriptors_size - priv->descriptors_len);
|
925
|
-
}
|
926
|
-
r = read(fd, priv->descriptors + priv->descriptors_len,
|
927
|
-
descriptors_size - priv->descriptors_len);
|
928
|
-
if (r < 0) {
|
929
|
-
usbi_err(ctx, "read descriptor failed ret=%d errno=%d",
|
930
|
-
fd, errno);
|
931
|
-
close(fd);
|
932
|
-
return LIBUSB_ERROR_IO;
|
933
|
-
}
|
934
|
-
priv->descriptors_len += r;
|
935
|
-
} while (priv->descriptors_len == descriptors_size);
|
936
|
-
|
937
|
-
close(fd);
|
938
|
-
|
939
|
-
if (priv->descriptors_len < DEVICE_DESC_LENGTH) {
|
940
|
-
usbi_err(ctx, "short descriptor read (%d)",
|
941
|
-
priv->descriptors_len);
|
942
|
-
return LIBUSB_ERROR_IO;
|
943
|
-
}
|
944
|
-
|
945
|
-
if (sysfs_can_relate_devices)
|
946
|
-
return LIBUSB_SUCCESS;
|
947
|
-
|
948
|
-
/* cache active config */
|
949
|
-
fd = _get_usbfs_fd(dev, O_RDWR, 1);
|
950
|
-
if (fd < 0) {
|
951
|
-
/* cannot send a control message to determine the active
|
952
|
-
* config. just assume the first one is active. */
|
953
|
-
usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
|
954
|
-
"active configuration descriptor");
|
955
|
-
if (priv->descriptors_len >=
|
956
|
-
(DEVICE_DESC_LENGTH + LIBUSB_DT_CONFIG_SIZE)) {
|
957
|
-
struct libusb_config_descriptor config;
|
958
|
-
usbi_parse_descriptor(
|
959
|
-
priv->descriptors + DEVICE_DESC_LENGTH,
|
960
|
-
"bbwbbbbb", &config, 0);
|
961
|
-
priv->active_config = config.bConfigurationValue;
|
962
|
-
} else
|
963
|
-
priv->active_config = -1; /* No config dt */
|
964
|
-
|
965
|
-
return LIBUSB_SUCCESS;
|
966
|
-
}
|
967
|
-
|
968
|
-
r = usbfs_get_active_config(dev, fd);
|
969
|
-
if (r > 0) {
|
970
|
-
priv->active_config = r;
|
971
|
-
r = LIBUSB_SUCCESS;
|
972
|
-
} else if (r == 0) {
|
973
|
-
/* some buggy devices have a configuration 0, but we're
|
974
|
-
* reaching into the corner of a corner case here, so let's
|
975
|
-
* not support buggy devices in these circumstances.
|
976
|
-
* stick to the specs: a configuration value of 0 means
|
977
|
-
* unconfigured. */
|
978
|
-
usbi_dbg("active cfg 0? assuming unconfigured device");
|
979
|
-
priv->active_config = -1;
|
980
|
-
r = LIBUSB_SUCCESS;
|
981
|
-
} else if (r == LIBUSB_ERROR_IO) {
|
982
|
-
/* buggy devices sometimes fail to report their active config.
|
983
|
-
* assume unconfigured and continue the probing */
|
984
|
-
usbi_warn(ctx, "couldn't query active configuration, assuming"
|
985
|
-
" unconfigured");
|
986
|
-
priv->active_config = -1;
|
987
|
-
r = LIBUSB_SUCCESS;
|
988
|
-
} /* else r < 0, just return the error code */
|
989
|
-
|
990
|
-
close(fd);
|
991
|
-
return r;
|
992
|
-
}
|
993
|
-
|
994
|
-
static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_dir)
|
995
|
-
{
|
996
|
-
struct libusb_context *ctx = DEVICE_CTX(dev);
|
997
|
-
struct libusb_device *it;
|
998
|
-
char *parent_sysfs_dir, *tmp;
|
999
|
-
int ret, add_parent = 1;
|
1000
|
-
|
1001
|
-
/* XXX -- can we figure out the topology when using usbfs? */
|
1002
|
-
if (NULL == sysfs_dir || 0 == strncmp(sysfs_dir, "usb", 3)) {
|
1003
|
-
/* either using usbfs or finding the parent of a root hub */
|
1004
|
-
return LIBUSB_SUCCESS;
|
1005
|
-
}
|
1006
|
-
|
1007
|
-
parent_sysfs_dir = strdup(sysfs_dir);
|
1008
|
-
if (NULL == parent_sysfs_dir) {
|
1009
|
-
return LIBUSB_ERROR_NO_MEM;
|
1010
|
-
}
|
1011
|
-
if (NULL != (tmp = strrchr(parent_sysfs_dir, '.')) ||
|
1012
|
-
NULL != (tmp = strrchr(parent_sysfs_dir, '-'))) {
|
1013
|
-
dev->port_number = atoi(tmp + 1);
|
1014
|
-
*tmp = '\0';
|
1015
|
-
} else {
|
1016
|
-
usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
|
1017
|
-
parent_sysfs_dir);
|
1018
|
-
free (parent_sysfs_dir);
|
1019
|
-
return LIBUSB_SUCCESS;
|
1020
|
-
}
|
1021
|
-
|
1022
|
-
/* is the parent a root hub? */
|
1023
|
-
if (NULL == strchr(parent_sysfs_dir, '-')) {
|
1024
|
-
tmp = parent_sysfs_dir;
|
1025
|
-
ret = asprintf (&parent_sysfs_dir, "usb%s", tmp);
|
1026
|
-
free (tmp);
|
1027
|
-
if (0 > ret) {
|
1028
|
-
return LIBUSB_ERROR_NO_MEM;
|
1029
|
-
}
|
1030
|
-
}
|
1031
|
-
|
1032
|
-
retry:
|
1033
|
-
/* find the parent in the context */
|
1034
|
-
usbi_mutex_lock(&ctx->usb_devs_lock);
|
1035
|
-
list_for_each_entry(it, &ctx->usb_devs, list, struct libusb_device) {
|
1036
|
-
struct linux_device_priv *priv = _device_priv(it);
|
1037
|
-
if (0 == strcmp (priv->sysfs_dir, parent_sysfs_dir)) {
|
1038
|
-
dev->parent_dev = libusb_ref_device(it);
|
1039
|
-
break;
|
1040
|
-
}
|
1041
|
-
}
|
1042
|
-
usbi_mutex_unlock(&ctx->usb_devs_lock);
|
1043
|
-
|
1044
|
-
if (!dev->parent_dev && add_parent) {
|
1045
|
-
usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
|
1046
|
-
parent_sysfs_dir);
|
1047
|
-
sysfs_scan_device(ctx, parent_sysfs_dir);
|
1048
|
-
add_parent = 0;
|
1049
|
-
goto retry;
|
1050
|
-
}
|
1051
|
-
|
1052
|
-
usbi_dbg("Dev %p (%s) has parent %p (%s) port %d", dev, sysfs_dir,
|
1053
|
-
dev->parent_dev, parent_sysfs_dir, dev->port_number);
|
1054
|
-
|
1055
|
-
free (parent_sysfs_dir);
|
1056
|
-
|
1057
|
-
return LIBUSB_SUCCESS;
|
1058
|
-
}
|
1059
|
-
|
1060
|
-
int linux_enumerate_device(struct libusb_context *ctx,
|
1061
|
-
uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
|
1062
|
-
{
|
1063
|
-
unsigned long session_id;
|
1064
|
-
struct libusb_device *dev;
|
1065
|
-
int r = 0;
|
1066
|
-
|
1067
|
-
/* FIXME: session ID is not guaranteed unique as addresses can wrap and
|
1068
|
-
* will be reused. instead we should add a simple sysfs attribute with
|
1069
|
-
* a session ID. */
|
1070
|
-
session_id = busnum << 8 | devaddr;
|
1071
|
-
usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr,
|
1072
|
-
session_id);
|
1073
|
-
|
1074
|
-
dev = usbi_get_device_by_session_id(ctx, session_id);
|
1075
|
-
if (dev) {
|
1076
|
-
/* device already exists in the context */
|
1077
|
-
usbi_dbg("session_id %ld already exists", session_id);
|
1078
|
-
libusb_unref_device(dev);
|
1079
|
-
return LIBUSB_SUCCESS;
|
1080
|
-
}
|
1081
|
-
|
1082
|
-
usbi_dbg("allocating new device for %d/%d (session %ld)",
|
1083
|
-
busnum, devaddr, session_id);
|
1084
|
-
dev = usbi_alloc_device(ctx, session_id);
|
1085
|
-
if (!dev)
|
1086
|
-
return LIBUSB_ERROR_NO_MEM;
|
1087
|
-
|
1088
|
-
r = initialize_device(dev, busnum, devaddr, sysfs_dir);
|
1089
|
-
if (r < 0)
|
1090
|
-
goto out;
|
1091
|
-
r = usbi_sanitize_device(dev);
|
1092
|
-
if (r < 0)
|
1093
|
-
goto out;
|
1094
|
-
|
1095
|
-
r = linux_get_parent_info(dev, sysfs_dir);
|
1096
|
-
if (r < 0)
|
1097
|
-
goto out;
|
1098
|
-
out:
|
1099
|
-
if (r < 0)
|
1100
|
-
libusb_unref_device(dev);
|
1101
|
-
else
|
1102
|
-
usbi_connect_device(dev);
|
1103
|
-
|
1104
|
-
return r;
|
1105
|
-
}
|
1106
|
-
|
1107
|
-
void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_name)
|
1108
|
-
{
|
1109
|
-
struct libusb_context *ctx;
|
1110
|
-
|
1111
|
-
usbi_mutex_static_lock(&active_contexts_lock);
|
1112
|
-
list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
|
1113
|
-
linux_enumerate_device(ctx, busnum, devaddr, sys_name);
|
1114
|
-
}
|
1115
|
-
usbi_mutex_static_unlock(&active_contexts_lock);
|
1116
|
-
}
|
1117
|
-
|
1118
|
-
void linux_device_disconnected(uint8_t busnum, uint8_t devaddr, const char *sys_name)
|
1119
|
-
{
|
1120
|
-
struct libusb_context *ctx;
|
1121
|
-
struct libusb_device *dev;
|
1122
|
-
unsigned long session_id = busnum << 8 | devaddr;
|
1123
|
-
|
1124
|
-
usbi_mutex_static_lock(&active_contexts_lock);
|
1125
|
-
list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
|
1126
|
-
dev = usbi_get_device_by_session_id (ctx, session_id);
|
1127
|
-
if (NULL != dev) {
|
1128
|
-
usbi_disconnect_device (dev);
|
1129
|
-
libusb_unref_device(dev);
|
1130
|
-
} else {
|
1131
|
-
usbi_dbg("device not found for session %x", session_id);
|
1132
|
-
}
|
1133
|
-
}
|
1134
|
-
usbi_mutex_static_unlock(&active_contexts_lock);
|
1135
|
-
}
|
1136
|
-
|
1137
|
-
#if !defined(USE_UDEV)
|
1138
|
-
/* open a bus directory and adds all discovered devices to the context */
|
1139
|
-
static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
|
1140
|
-
{
|
1141
|
-
DIR *dir;
|
1142
|
-
char dirpath[PATH_MAX];
|
1143
|
-
struct dirent *entry;
|
1144
|
-
int r = LIBUSB_ERROR_IO;
|
1145
|
-
|
1146
|
-
snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum);
|
1147
|
-
usbi_dbg("%s", dirpath);
|
1148
|
-
dir = opendir(dirpath);
|
1149
|
-
if (!dir) {
|
1150
|
-
usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
|
1151
|
-
/* FIXME: should handle valid race conditions like hub unplugged
|
1152
|
-
* during directory iteration - this is not an error */
|
1153
|
-
return r;
|
1154
|
-
}
|
1155
|
-
|
1156
|
-
while ((entry = readdir(dir))) {
|
1157
|
-
int devaddr;
|
1158
|
-
|
1159
|
-
if (entry->d_name[0] == '.')
|
1160
|
-
continue;
|
1161
|
-
|
1162
|
-
devaddr = atoi(entry->d_name);
|
1163
|
-
if (devaddr == 0) {
|
1164
|
-
usbi_dbg("unknown dir entry %s", entry->d_name);
|
1165
|
-
continue;
|
1166
|
-
}
|
1167
|
-
|
1168
|
-
if (linux_enumerate_device(ctx, busnum, (uint8_t) devaddr, NULL)) {
|
1169
|
-
usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
|
1170
|
-
continue;
|
1171
|
-
}
|
1172
|
-
|
1173
|
-
r = 0;
|
1174
|
-
}
|
1175
|
-
|
1176
|
-
closedir(dir);
|
1177
|
-
return r;
|
1178
|
-
}
|
1179
|
-
|
1180
|
-
static int usbfs_get_device_list(struct libusb_context *ctx)
|
1181
|
-
{
|
1182
|
-
struct dirent *entry;
|
1183
|
-
DIR *buses = opendir(usbfs_path);
|
1184
|
-
int r = 0;
|
1185
|
-
|
1186
|
-
if (!buses) {
|
1187
|
-
usbi_err(ctx, "opendir buses failed errno=%d", errno);
|
1188
|
-
return LIBUSB_ERROR_IO;
|
1189
|
-
}
|
1190
|
-
|
1191
|
-
while ((entry = readdir(buses))) {
|
1192
|
-
int busnum;
|
1193
|
-
|
1194
|
-
if (entry->d_name[0] == '.')
|
1195
|
-
continue;
|
1196
|
-
|
1197
|
-
if (usbdev_names) {
|
1198
|
-
int devaddr;
|
1199
|
-
if (!_is_usbdev_entry(entry, &busnum, &devaddr))
|
1200
|
-
continue;
|
1201
|
-
|
1202
|
-
r = linux_enumerate_device(ctx, busnum, (uint8_t) devaddr, NULL);
|
1203
|
-
if (r < 0) {
|
1204
|
-
usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
|
1205
|
-
continue;
|
1206
|
-
}
|
1207
|
-
} else {
|
1208
|
-
busnum = atoi(entry->d_name);
|
1209
|
-
if (busnum == 0) {
|
1210
|
-
usbi_dbg("unknown dir entry %s", entry->d_name);
|
1211
|
-
continue;
|
1212
|
-
}
|
1213
|
-
|
1214
|
-
r = usbfs_scan_busdir(ctx, busnum);
|
1215
|
-
if (r < 0)
|
1216
|
-
break;
|
1217
|
-
}
|
1218
|
-
}
|
1219
|
-
|
1220
|
-
closedir(buses);
|
1221
|
-
return r;
|
1222
|
-
|
1223
|
-
}
|
1224
|
-
#endif
|
1225
|
-
|
1226
|
-
static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
|
1227
|
-
{
|
1228
|
-
uint8_t busnum, devaddr;
|
1229
|
-
int ret;
|
1230
|
-
|
1231
|
-
ret = linux_get_device_address (ctx, 0, &busnum, &devaddr, NULL, devname);
|
1232
|
-
if (LIBUSB_SUCCESS != ret) {
|
1233
|
-
return ret;
|
1234
|
-
}
|
1235
|
-
|
1236
|
-
return linux_enumerate_device(ctx, busnum & 0xff, devaddr & 0xff,
|
1237
|
-
devname);
|
1238
|
-
}
|
1239
|
-
|
1240
|
-
#if !defined(USE_UDEV)
|
1241
|
-
static int sysfs_get_device_list(struct libusb_context *ctx)
|
1242
|
-
{
|
1243
|
-
DIR *devices = opendir(SYSFS_DEVICE_PATH);
|
1244
|
-
struct dirent *entry;
|
1245
|
-
int r = LIBUSB_ERROR_IO;
|
1246
|
-
|
1247
|
-
if (!devices) {
|
1248
|
-
usbi_err(ctx, "opendir devices failed errno=%d", errno);
|
1249
|
-
return r;
|
1250
|
-
}
|
1251
|
-
|
1252
|
-
while ((entry = readdir(devices))) {
|
1253
|
-
if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
|
1254
|
-
|| strchr(entry->d_name, ':'))
|
1255
|
-
continue;
|
1256
|
-
|
1257
|
-
if (sysfs_scan_device(ctx, entry->d_name)) {
|
1258
|
-
usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
|
1259
|
-
continue;
|
1260
|
-
}
|
1261
|
-
|
1262
|
-
r = 0;
|
1263
|
-
}
|
1264
|
-
|
1265
|
-
closedir(devices);
|
1266
|
-
return r;
|
1267
|
-
}
|
1268
|
-
|
1269
|
-
static int linux_default_scan_devices (struct libusb_context *ctx)
|
1270
|
-
{
|
1271
|
-
/* we can retrieve device list and descriptors from sysfs or usbfs.
|
1272
|
-
* sysfs is preferable, because if we use usbfs we end up resuming
|
1273
|
-
* any autosuspended USB devices. however, sysfs is not available
|
1274
|
-
* everywhere, so we need a usbfs fallback too.
|
1275
|
-
*
|
1276
|
-
* as described in the "sysfs vs usbfs" comment at the top of this
|
1277
|
-
* file, sometimes we have sysfs but not enough information to
|
1278
|
-
* relate sysfs devices to usbfs nodes. op_init() determines the
|
1279
|
-
* adequacy of sysfs and sets sysfs_can_relate_devices.
|
1280
|
-
*/
|
1281
|
-
if (sysfs_can_relate_devices != 0)
|
1282
|
-
return sysfs_get_device_list(ctx);
|
1283
|
-
else
|
1284
|
-
return usbfs_get_device_list(ctx);
|
1285
|
-
}
|
1286
|
-
#endif
|
1287
|
-
|
1288
|
-
static int op_open(struct libusb_device_handle *handle)
|
1289
|
-
{
|
1290
|
-
struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
|
1291
|
-
int r;
|
1292
|
-
|
1293
|
-
hpriv->fd = _get_usbfs_fd(handle->dev, O_RDWR, 0);
|
1294
|
-
if (hpriv->fd < 0) {
|
1295
|
-
if (hpriv->fd == LIBUSB_ERROR_NO_DEVICE) {
|
1296
|
-
/* device will still be marked as attached if hotplug monitor thread
|
1297
|
-
* hasn't processed remove event yet */
|
1298
|
-
usbi_mutex_static_lock(&linux_hotplug_lock);
|
1299
|
-
if (handle->dev->attached) {
|
1300
|
-
usbi_dbg("open failed with no device, but device still attached");
|
1301
|
-
linux_device_disconnected(handle->dev->bus_number,
|
1302
|
-
handle->dev->device_address, NULL);
|
1303
|
-
}
|
1304
|
-
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
1305
|
-
}
|
1306
|
-
return hpriv->fd;
|
1307
|
-
}
|
1308
|
-
|
1309
|
-
r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
|
1310
|
-
if (r < 0) {
|
1311
|
-
if (errno == ENOTTY)
|
1312
|
-
usbi_dbg("getcap not available");
|
1313
|
-
else
|
1314
|
-
usbi_err(HANDLE_CTX(handle), "getcap failed (%d)", errno);
|
1315
|
-
hpriv->caps = 0;
|
1316
|
-
if (supports_flag_zero_packet)
|
1317
|
-
hpriv->caps |= USBFS_CAP_ZERO_PACKET;
|
1318
|
-
if (supports_flag_bulk_continuation)
|
1319
|
-
hpriv->caps |= USBFS_CAP_BULK_CONTINUATION;
|
1320
|
-
}
|
1321
|
-
|
1322
|
-
return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
|
1323
|
-
}
|
1324
|
-
|
1325
|
-
static void op_close(struct libusb_device_handle *dev_handle)
|
1326
|
-
{
|
1327
|
-
int fd = _device_handle_priv(dev_handle)->fd;
|
1328
|
-
usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd);
|
1329
|
-
close(fd);
|
1330
|
-
}
|
1331
|
-
|
1332
|
-
static int op_get_configuration(struct libusb_device_handle *handle,
|
1333
|
-
int *config)
|
1334
|
-
{
|
1335
|
-
int r;
|
1336
|
-
|
1337
|
-
if (sysfs_can_relate_devices) {
|
1338
|
-
r = sysfs_get_active_config(handle->dev, config);
|
1339
|
-
} else {
|
1340
|
-
r = usbfs_get_active_config(handle->dev,
|
1341
|
-
_device_handle_priv(handle)->fd);
|
1342
|
-
}
|
1343
|
-
if (r < 0)
|
1344
|
-
return r;
|
1345
|
-
|
1346
|
-
if (*config == -1) {
|
1347
|
-
usbi_err(HANDLE_CTX(handle), "device unconfigured");
|
1348
|
-
*config = 0;
|
1349
|
-
}
|
1350
|
-
|
1351
|
-
return 0;
|
1352
|
-
}
|
1353
|
-
|
1354
|
-
static int op_set_configuration(struct libusb_device_handle *handle, int config)
|
1355
|
-
{
|
1356
|
-
struct linux_device_priv *priv = _device_priv(handle->dev);
|
1357
|
-
int fd = _device_handle_priv(handle)->fd;
|
1358
|
-
int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config);
|
1359
|
-
if (r) {
|
1360
|
-
if (errno == EINVAL)
|
1361
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1362
|
-
else if (errno == EBUSY)
|
1363
|
-
return LIBUSB_ERROR_BUSY;
|
1364
|
-
else if (errno == ENODEV)
|
1365
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1366
|
-
|
1367
|
-
usbi_err(HANDLE_CTX(handle), "failed, error %d errno %d", r, errno);
|
1368
|
-
return LIBUSB_ERROR_OTHER;
|
1369
|
-
}
|
1370
|
-
|
1371
|
-
/* update our cached active config descriptor */
|
1372
|
-
priv->active_config = config;
|
1373
|
-
|
1374
|
-
return LIBUSB_SUCCESS;
|
1375
|
-
}
|
1376
|
-
|
1377
|
-
static int claim_interface(struct libusb_device_handle *handle, int iface)
|
1378
|
-
{
|
1379
|
-
int fd = _device_handle_priv(handle)->fd;
|
1380
|
-
int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);
|
1381
|
-
if (r) {
|
1382
|
-
if (errno == ENOENT)
|
1383
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1384
|
-
else if (errno == EBUSY)
|
1385
|
-
return LIBUSB_ERROR_BUSY;
|
1386
|
-
else if (errno == ENODEV)
|
1387
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1388
|
-
|
1389
|
-
usbi_err(HANDLE_CTX(handle),
|
1390
|
-
"claim interface failed, error %d errno %d", r, errno);
|
1391
|
-
return LIBUSB_ERROR_OTHER;
|
1392
|
-
}
|
1393
|
-
return 0;
|
1394
|
-
}
|
1395
|
-
|
1396
|
-
static int release_interface(struct libusb_device_handle *handle, int iface)
|
1397
|
-
{
|
1398
|
-
int fd = _device_handle_priv(handle)->fd;
|
1399
|
-
int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface);
|
1400
|
-
if (r) {
|
1401
|
-
if (errno == ENODEV)
|
1402
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1403
|
-
|
1404
|
-
usbi_err(HANDLE_CTX(handle),
|
1405
|
-
"release interface failed, error %d errno %d", r, errno);
|
1406
|
-
return LIBUSB_ERROR_OTHER;
|
1407
|
-
}
|
1408
|
-
return 0;
|
1409
|
-
}
|
1410
|
-
|
1411
|
-
static int op_set_interface(struct libusb_device_handle *handle, int iface,
|
1412
|
-
int altsetting)
|
1413
|
-
{
|
1414
|
-
int fd = _device_handle_priv(handle)->fd;
|
1415
|
-
struct usbfs_setinterface setintf;
|
1416
|
-
int r;
|
1417
|
-
|
1418
|
-
setintf.interface = iface;
|
1419
|
-
setintf.altsetting = altsetting;
|
1420
|
-
r = ioctl(fd, IOCTL_USBFS_SETINTF, &setintf);
|
1421
|
-
if (r) {
|
1422
|
-
if (errno == EINVAL)
|
1423
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1424
|
-
else if (errno == ENODEV)
|
1425
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1426
|
-
|
1427
|
-
usbi_err(HANDLE_CTX(handle),
|
1428
|
-
"setintf failed error %d errno %d", r, errno);
|
1429
|
-
return LIBUSB_ERROR_OTHER;
|
1430
|
-
}
|
1431
|
-
|
1432
|
-
return 0;
|
1433
|
-
}
|
1434
|
-
|
1435
|
-
static int op_clear_halt(struct libusb_device_handle *handle,
|
1436
|
-
unsigned char endpoint)
|
1437
|
-
{
|
1438
|
-
int fd = _device_handle_priv(handle)->fd;
|
1439
|
-
unsigned int _endpoint = endpoint;
|
1440
|
-
int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
|
1441
|
-
if (r) {
|
1442
|
-
if (errno == ENOENT)
|
1443
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1444
|
-
else if (errno == ENODEV)
|
1445
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1446
|
-
|
1447
|
-
usbi_err(HANDLE_CTX(handle),
|
1448
|
-
"clear_halt failed error %d errno %d", r, errno);
|
1449
|
-
return LIBUSB_ERROR_OTHER;
|
1450
|
-
}
|
1451
|
-
|
1452
|
-
return 0;
|
1453
|
-
}
|
1454
|
-
|
1455
|
-
static int op_reset_device(struct libusb_device_handle *handle)
|
1456
|
-
{
|
1457
|
-
int fd = _device_handle_priv(handle)->fd;
|
1458
|
-
int i, r, ret = 0;
|
1459
|
-
|
1460
|
-
/* Doing a device reset will cause the usbfs driver to get unbound
|
1461
|
-
from any interfaces it is bound to. By voluntarily unbinding
|
1462
|
-
the usbfs driver ourself, we stop the kernel from rebinding
|
1463
|
-
the interface after reset (which would end up with the interface
|
1464
|
-
getting bound to the in kernel driver if any). */
|
1465
|
-
for (i = 0; i < USB_MAXINTERFACES; i++) {
|
1466
|
-
if (handle->claimed_interfaces & (1L << i)) {
|
1467
|
-
release_interface(handle, i);
|
1468
|
-
}
|
1469
|
-
}
|
1470
|
-
|
1471
|
-
usbi_mutex_lock(&handle->lock);
|
1472
|
-
r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
|
1473
|
-
if (r) {
|
1474
|
-
if (errno == ENODEV) {
|
1475
|
-
ret = LIBUSB_ERROR_NOT_FOUND;
|
1476
|
-
goto out;
|
1477
|
-
}
|
1478
|
-
|
1479
|
-
usbi_err(HANDLE_CTX(handle),
|
1480
|
-
"reset failed error %d errno %d", r, errno);
|
1481
|
-
ret = LIBUSB_ERROR_OTHER;
|
1482
|
-
goto out;
|
1483
|
-
}
|
1484
|
-
|
1485
|
-
/* And re-claim any interfaces which were claimed before the reset */
|
1486
|
-
for (i = 0; i < USB_MAXINTERFACES; i++) {
|
1487
|
-
if (handle->claimed_interfaces & (1L << i)) {
|
1488
|
-
/*
|
1489
|
-
* A driver may have completed modprobing during
|
1490
|
-
* IOCTL_USBFS_RESET, and bound itself as soon as
|
1491
|
-
* IOCTL_USBFS_RESET released the device lock
|
1492
|
-
*/
|
1493
|
-
r = detach_kernel_driver_and_claim(handle, i);
|
1494
|
-
if (r) {
|
1495
|
-
usbi_warn(HANDLE_CTX(handle),
|
1496
|
-
"failed to re-claim interface %d after reset: %s",
|
1497
|
-
i, libusb_error_name(r));
|
1498
|
-
handle->claimed_interfaces &= ~(1L << i);
|
1499
|
-
ret = LIBUSB_ERROR_NOT_FOUND;
|
1500
|
-
}
|
1501
|
-
}
|
1502
|
-
}
|
1503
|
-
out:
|
1504
|
-
usbi_mutex_unlock(&handle->lock);
|
1505
|
-
return ret;
|
1506
|
-
}
|
1507
|
-
|
1508
|
-
static int do_streams_ioctl(struct libusb_device_handle *handle, long req,
|
1509
|
-
uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
|
1510
|
-
{
|
1511
|
-
int r, fd = _device_handle_priv(handle)->fd;
|
1512
|
-
struct usbfs_streams *streams;
|
1513
|
-
|
1514
|
-
if (num_endpoints > 30) /* Max 15 in + 15 out eps */
|
1515
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1516
|
-
|
1517
|
-
streams = malloc(sizeof(struct usbfs_streams) + num_endpoints);
|
1518
|
-
if (!streams)
|
1519
|
-
return LIBUSB_ERROR_NO_MEM;
|
1520
|
-
|
1521
|
-
streams->num_streams = num_streams;
|
1522
|
-
streams->num_eps = num_endpoints;
|
1523
|
-
memcpy(streams->eps, endpoints, num_endpoints);
|
1524
|
-
|
1525
|
-
r = ioctl(fd, req, streams);
|
1526
|
-
|
1527
|
-
free(streams);
|
1528
|
-
|
1529
|
-
if (r < 0) {
|
1530
|
-
if (errno == ENOTTY)
|
1531
|
-
return LIBUSB_ERROR_NOT_SUPPORTED;
|
1532
|
-
else if (errno == EINVAL)
|
1533
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1534
|
-
else if (errno == ENODEV)
|
1535
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1536
|
-
|
1537
|
-
usbi_err(HANDLE_CTX(handle),
|
1538
|
-
"streams-ioctl failed error %d errno %d", r, errno);
|
1539
|
-
return LIBUSB_ERROR_OTHER;
|
1540
|
-
}
|
1541
|
-
return r;
|
1542
|
-
}
|
1543
|
-
|
1544
|
-
static int op_alloc_streams(struct libusb_device_handle *handle,
|
1545
|
-
uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
|
1546
|
-
{
|
1547
|
-
return do_streams_ioctl(handle, IOCTL_USBFS_ALLOC_STREAMS,
|
1548
|
-
num_streams, endpoints, num_endpoints);
|
1549
|
-
}
|
1550
|
-
|
1551
|
-
static int op_free_streams(struct libusb_device_handle *handle,
|
1552
|
-
unsigned char *endpoints, int num_endpoints)
|
1553
|
-
{
|
1554
|
-
return do_streams_ioctl(handle, IOCTL_USBFS_FREE_STREAMS, 0,
|
1555
|
-
endpoints, num_endpoints);
|
1556
|
-
}
|
1557
|
-
|
1558
|
-
static int op_kernel_driver_active(struct libusb_device_handle *handle,
|
1559
|
-
int interface)
|
1560
|
-
{
|
1561
|
-
int fd = _device_handle_priv(handle)->fd;
|
1562
|
-
struct usbfs_getdriver getdrv;
|
1563
|
-
int r;
|
1564
|
-
|
1565
|
-
getdrv.interface = interface;
|
1566
|
-
r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
|
1567
|
-
if (r) {
|
1568
|
-
if (errno == ENODATA)
|
1569
|
-
return 0;
|
1570
|
-
else if (errno == ENODEV)
|
1571
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1572
|
-
|
1573
|
-
usbi_err(HANDLE_CTX(handle),
|
1574
|
-
"get driver failed error %d errno %d", r, errno);
|
1575
|
-
return LIBUSB_ERROR_OTHER;
|
1576
|
-
}
|
1577
|
-
|
1578
|
-
return (strcmp(getdrv.driver, "usbfs") == 0) ? 0 : 1;
|
1579
|
-
}
|
1580
|
-
|
1581
|
-
static int op_detach_kernel_driver(struct libusb_device_handle *handle,
|
1582
|
-
int interface)
|
1583
|
-
{
|
1584
|
-
int fd = _device_handle_priv(handle)->fd;
|
1585
|
-
struct usbfs_ioctl command;
|
1586
|
-
struct usbfs_getdriver getdrv;
|
1587
|
-
int r;
|
1588
|
-
|
1589
|
-
command.ifno = interface;
|
1590
|
-
command.ioctl_code = IOCTL_USBFS_DISCONNECT;
|
1591
|
-
command.data = NULL;
|
1592
|
-
|
1593
|
-
getdrv.interface = interface;
|
1594
|
-
r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
|
1595
|
-
if (r == 0 && strcmp(getdrv.driver, "usbfs") == 0)
|
1596
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1597
|
-
|
1598
|
-
r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
|
1599
|
-
if (r) {
|
1600
|
-
if (errno == ENODATA)
|
1601
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1602
|
-
else if (errno == EINVAL)
|
1603
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1604
|
-
else if (errno == ENODEV)
|
1605
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1606
|
-
|
1607
|
-
usbi_err(HANDLE_CTX(handle),
|
1608
|
-
"detach failed error %d errno %d", r, errno);
|
1609
|
-
return LIBUSB_ERROR_OTHER;
|
1610
|
-
}
|
1611
|
-
|
1612
|
-
return 0;
|
1613
|
-
}
|
1614
|
-
|
1615
|
-
static int op_attach_kernel_driver(struct libusb_device_handle *handle,
|
1616
|
-
int interface)
|
1617
|
-
{
|
1618
|
-
int fd = _device_handle_priv(handle)->fd;
|
1619
|
-
struct usbfs_ioctl command;
|
1620
|
-
int r;
|
1621
|
-
|
1622
|
-
command.ifno = interface;
|
1623
|
-
command.ioctl_code = IOCTL_USBFS_CONNECT;
|
1624
|
-
command.data = NULL;
|
1625
|
-
|
1626
|
-
r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
|
1627
|
-
if (r < 0) {
|
1628
|
-
if (errno == ENODATA)
|
1629
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1630
|
-
else if (errno == EINVAL)
|
1631
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1632
|
-
else if (errno == ENODEV)
|
1633
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1634
|
-
else if (errno == EBUSY)
|
1635
|
-
return LIBUSB_ERROR_BUSY;
|
1636
|
-
|
1637
|
-
usbi_err(HANDLE_CTX(handle),
|
1638
|
-
"attach failed error %d errno %d", r, errno);
|
1639
|
-
return LIBUSB_ERROR_OTHER;
|
1640
|
-
} else if (r == 0) {
|
1641
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
1642
|
-
}
|
1643
|
-
|
1644
|
-
return 0;
|
1645
|
-
}
|
1646
|
-
|
1647
|
-
static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
|
1648
|
-
int interface)
|
1649
|
-
{
|
1650
|
-
struct usbfs_disconnect_claim dc;
|
1651
|
-
int r, fd = _device_handle_priv(handle)->fd;
|
1652
|
-
|
1653
|
-
dc.interface = interface;
|
1654
|
-
strcpy(dc.driver, "usbfs");
|
1655
|
-
dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
|
1656
|
-
r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
|
1657
|
-
if (r == 0 || (r != 0 && errno != ENOTTY)) {
|
1658
|
-
if (r == 0)
|
1659
|
-
return 0;
|
1660
|
-
|
1661
|
-
switch (errno) {
|
1662
|
-
case EBUSY:
|
1663
|
-
return LIBUSB_ERROR_BUSY;
|
1664
|
-
case EINVAL:
|
1665
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1666
|
-
case ENODEV:
|
1667
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
1668
|
-
}
|
1669
|
-
usbi_err(HANDLE_CTX(handle),
|
1670
|
-
"disconnect-and-claim failed errno %d", errno);
|
1671
|
-
return LIBUSB_ERROR_OTHER;
|
1672
|
-
}
|
1673
|
-
|
1674
|
-
/* Fallback code for kernels which don't support the
|
1675
|
-
disconnect-and-claim ioctl */
|
1676
|
-
r = op_detach_kernel_driver(handle, interface);
|
1677
|
-
if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
|
1678
|
-
return r;
|
1679
|
-
|
1680
|
-
return claim_interface(handle, interface);
|
1681
|
-
}
|
1682
|
-
|
1683
|
-
static int op_claim_interface(struct libusb_device_handle *handle, int iface)
|
1684
|
-
{
|
1685
|
-
if (handle->auto_detach_kernel_driver)
|
1686
|
-
return detach_kernel_driver_and_claim(handle, iface);
|
1687
|
-
else
|
1688
|
-
return claim_interface(handle, iface);
|
1689
|
-
}
|
1690
|
-
|
1691
|
-
static int op_release_interface(struct libusb_device_handle *handle, int iface)
|
1692
|
-
{
|
1693
|
-
int r;
|
1694
|
-
|
1695
|
-
r = release_interface(handle, iface);
|
1696
|
-
if (r)
|
1697
|
-
return r;
|
1698
|
-
|
1699
|
-
if (handle->auto_detach_kernel_driver)
|
1700
|
-
op_attach_kernel_driver(handle, iface);
|
1701
|
-
|
1702
|
-
return 0;
|
1703
|
-
}
|
1704
|
-
|
1705
|
-
static void op_destroy_device(struct libusb_device *dev)
|
1706
|
-
{
|
1707
|
-
struct linux_device_priv *priv = _device_priv(dev);
|
1708
|
-
if (priv->descriptors)
|
1709
|
-
free(priv->descriptors);
|
1710
|
-
if (priv->sysfs_dir)
|
1711
|
-
free(priv->sysfs_dir);
|
1712
|
-
}
|
1713
|
-
|
1714
|
-
/* URBs are discarded in reverse order of submission to avoid races. */
|
1715
|
-
static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plus_one)
|
1716
|
-
{
|
1717
|
-
struct libusb_transfer *transfer =
|
1718
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
1719
|
-
struct linux_transfer_priv *tpriv =
|
1720
|
-
usbi_transfer_get_os_priv(itransfer);
|
1721
|
-
struct linux_device_handle_priv *dpriv =
|
1722
|
-
_device_handle_priv(transfer->dev_handle);
|
1723
|
-
int i, ret = 0;
|
1724
|
-
struct usbfs_urb *urb;
|
1725
|
-
|
1726
|
-
for (i = last_plus_one - 1; i >= first; i--) {
|
1727
|
-
if (LIBUSB_TRANSFER_TYPE_ISOCHRONOUS == transfer->type)
|
1728
|
-
urb = tpriv->iso_urbs[i];
|
1729
|
-
else
|
1730
|
-
urb = &tpriv->urbs[i];
|
1731
|
-
|
1732
|
-
if (0 == ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, urb))
|
1733
|
-
continue;
|
1734
|
-
|
1735
|
-
if (EINVAL == errno) {
|
1736
|
-
usbi_dbg("URB not found --> assuming ready to be reaped");
|
1737
|
-
if (i == (last_plus_one - 1))
|
1738
|
-
ret = LIBUSB_ERROR_NOT_FOUND;
|
1739
|
-
} else if (ENODEV == errno) {
|
1740
|
-
usbi_dbg("Device not found for URB --> assuming ready to be reaped");
|
1741
|
-
ret = LIBUSB_ERROR_NO_DEVICE;
|
1742
|
-
} else {
|
1743
|
-
usbi_warn(TRANSFER_CTX(transfer),
|
1744
|
-
"unrecognised discard errno %d", errno);
|
1745
|
-
ret = LIBUSB_ERROR_OTHER;
|
1746
|
-
}
|
1747
|
-
}
|
1748
|
-
return ret;
|
1749
|
-
}
|
1750
|
-
|
1751
|
-
static void free_iso_urbs(struct linux_transfer_priv *tpriv)
|
1752
|
-
{
|
1753
|
-
int i;
|
1754
|
-
for (i = 0; i < tpriv->num_urbs; i++) {
|
1755
|
-
struct usbfs_urb *urb = tpriv->iso_urbs[i];
|
1756
|
-
if (!urb)
|
1757
|
-
break;
|
1758
|
-
free(urb);
|
1759
|
-
}
|
1760
|
-
|
1761
|
-
free(tpriv->iso_urbs);
|
1762
|
-
tpriv->iso_urbs = NULL;
|
1763
|
-
}
|
1764
|
-
|
1765
|
-
static int submit_bulk_transfer(struct usbi_transfer *itransfer)
|
1766
|
-
{
|
1767
|
-
struct libusb_transfer *transfer =
|
1768
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
1769
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
1770
|
-
struct linux_device_handle_priv *dpriv =
|
1771
|
-
_device_handle_priv(transfer->dev_handle);
|
1772
|
-
struct usbfs_urb *urbs;
|
1773
|
-
int is_out = (transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK)
|
1774
|
-
== LIBUSB_ENDPOINT_OUT;
|
1775
|
-
int bulk_buffer_len, use_bulk_continuation;
|
1776
|
-
int r;
|
1777
|
-
int i;
|
1778
|
-
|
1779
|
-
if (is_out && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) &&
|
1780
|
-
!(dpriv->caps & USBFS_CAP_ZERO_PACKET))
|
1781
|
-
return LIBUSB_ERROR_NOT_SUPPORTED;
|
1782
|
-
|
1783
|
-
/*
|
1784
|
-
* Older versions of usbfs place a 16kb limit on bulk URBs. We work
|
1785
|
-
* around this by splitting large transfers into 16k blocks, and then
|
1786
|
-
* submit all urbs at once. it would be simpler to submit one urb at
|
1787
|
-
* a time, but there is a big performance gain doing it this way.
|
1788
|
-
*
|
1789
|
-
* Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
|
1790
|
-
* using arbritary large transfers can still be a bad idea though, as
|
1791
|
-
* the kernel needs to allocate physical contiguous memory for this,
|
1792
|
-
* which may fail for large buffers.
|
1793
|
-
*
|
1794
|
-
* The kernel solves this problem by splitting the transfer into
|
1795
|
-
* blocks itself when the host-controller is scatter-gather capable
|
1796
|
-
* (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
|
1797
|
-
*
|
1798
|
-
* Last, there is the issue of short-transfers when splitting, for
|
1799
|
-
* short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
|
1800
|
-
* is needed, but this is not always available.
|
1801
|
-
*/
|
1802
|
-
if (dpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
|
1803
|
-
/* Good! Just submit everything in one go */
|
1804
|
-
bulk_buffer_len = transfer->length ? transfer->length : 1;
|
1805
|
-
use_bulk_continuation = 0;
|
1806
|
-
} else if (dpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
|
1807
|
-
/* Split the transfers and use bulk-continuation to
|
1808
|
-
avoid issues with short-transfers */
|
1809
|
-
bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
|
1810
|
-
use_bulk_continuation = 1;
|
1811
|
-
} else if (dpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
|
1812
|
-
/* Don't split, assume the kernel can alloc the buffer
|
1813
|
-
(otherwise the submit will fail with -ENOMEM) */
|
1814
|
-
bulk_buffer_len = transfer->length ? transfer->length : 1;
|
1815
|
-
use_bulk_continuation = 0;
|
1816
|
-
} else {
|
1817
|
-
/* Bad, splitting without bulk-continuation, short transfers
|
1818
|
-
which end before the last urb will not work reliable! */
|
1819
|
-
/* Note we don't warn here as this is "normal" on kernels <
|
1820
|
-
2.6.32 and not a problem for most applications */
|
1821
|
-
bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
|
1822
|
-
use_bulk_continuation = 0;
|
1823
|
-
}
|
1824
|
-
|
1825
|
-
int num_urbs = transfer->length / bulk_buffer_len;
|
1826
|
-
int last_urb_partial = 0;
|
1827
|
-
|
1828
|
-
if (transfer->length == 0) {
|
1829
|
-
num_urbs = 1;
|
1830
|
-
} else if ((transfer->length % bulk_buffer_len) > 0) {
|
1831
|
-
last_urb_partial = 1;
|
1832
|
-
num_urbs++;
|
1833
|
-
}
|
1834
|
-
usbi_dbg("need %d urbs for new transfer with length %d", num_urbs,
|
1835
|
-
transfer->length);
|
1836
|
-
urbs = calloc(num_urbs, sizeof(struct usbfs_urb));
|
1837
|
-
if (!urbs)
|
1838
|
-
return LIBUSB_ERROR_NO_MEM;
|
1839
|
-
tpriv->urbs = urbs;
|
1840
|
-
tpriv->num_urbs = num_urbs;
|
1841
|
-
tpriv->num_retired = 0;
|
1842
|
-
tpriv->reap_action = NORMAL;
|
1843
|
-
tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
|
1844
|
-
|
1845
|
-
for (i = 0; i < num_urbs; i++) {
|
1846
|
-
struct usbfs_urb *urb = &urbs[i];
|
1847
|
-
urb->usercontext = itransfer;
|
1848
|
-
switch (transfer->type) {
|
1849
|
-
case LIBUSB_TRANSFER_TYPE_BULK:
|
1850
|
-
urb->type = USBFS_URB_TYPE_BULK;
|
1851
|
-
urb->stream_id = 0;
|
1852
|
-
break;
|
1853
|
-
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
1854
|
-
urb->type = USBFS_URB_TYPE_BULK;
|
1855
|
-
urb->stream_id = itransfer->stream_id;
|
1856
|
-
break;
|
1857
|
-
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
1858
|
-
urb->type = USBFS_URB_TYPE_INTERRUPT;
|
1859
|
-
break;
|
1860
|
-
}
|
1861
|
-
urb->endpoint = transfer->endpoint;
|
1862
|
-
urb->buffer = transfer->buffer + (i * bulk_buffer_len);
|
1863
|
-
/* don't set the short not ok flag for the last URB */
|
1864
|
-
if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
|
1865
|
-
urb->flags = USBFS_URB_SHORT_NOT_OK;
|
1866
|
-
if (i == num_urbs - 1 && last_urb_partial)
|
1867
|
-
urb->buffer_length = transfer->length % bulk_buffer_len;
|
1868
|
-
else if (transfer->length == 0)
|
1869
|
-
urb->buffer_length = 0;
|
1870
|
-
else
|
1871
|
-
urb->buffer_length = bulk_buffer_len;
|
1872
|
-
|
1873
|
-
if (i > 0 && use_bulk_continuation)
|
1874
|
-
urb->flags |= USBFS_URB_BULK_CONTINUATION;
|
1875
|
-
|
1876
|
-
/* we have already checked that the flag is supported */
|
1877
|
-
if (is_out && i == num_urbs - 1 &&
|
1878
|
-
transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
|
1879
|
-
urb->flags |= USBFS_URB_ZERO_PACKET;
|
1880
|
-
|
1881
|
-
r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
|
1882
|
-
if (r < 0) {
|
1883
|
-
if (errno == ENODEV) {
|
1884
|
-
r = LIBUSB_ERROR_NO_DEVICE;
|
1885
|
-
} else {
|
1886
|
-
usbi_err(TRANSFER_CTX(transfer),
|
1887
|
-
"submiturb failed error %d errno=%d", r, errno);
|
1888
|
-
r = LIBUSB_ERROR_IO;
|
1889
|
-
}
|
1890
|
-
|
1891
|
-
/* if the first URB submission fails, we can simply free up and
|
1892
|
-
* return failure immediately. */
|
1893
|
-
if (i == 0) {
|
1894
|
-
usbi_dbg("first URB failed, easy peasy");
|
1895
|
-
free(urbs);
|
1896
|
-
tpriv->urbs = NULL;
|
1897
|
-
return r;
|
1898
|
-
}
|
1899
|
-
|
1900
|
-
/* if it's not the first URB that failed, the situation is a bit
|
1901
|
-
* tricky. we may need to discard all previous URBs. there are
|
1902
|
-
* complications:
|
1903
|
-
* - discarding is asynchronous - discarded urbs will be reaped
|
1904
|
-
* later. the user must not have freed the transfer when the
|
1905
|
-
* discarded URBs are reaped, otherwise libusb will be using
|
1906
|
-
* freed memory.
|
1907
|
-
* - the earlier URBs may have completed successfully and we do
|
1908
|
-
* not want to throw away any data.
|
1909
|
-
* - this URB failing may be no error; EREMOTEIO means that
|
1910
|
-
* this transfer simply didn't need all the URBs we submitted
|
1911
|
-
* so, we report that the transfer was submitted successfully and
|
1912
|
-
* in case of error we discard all previous URBs. later when
|
1913
|
-
* the final reap completes we can report error to the user,
|
1914
|
-
* or success if an earlier URB was completed successfully.
|
1915
|
-
*/
|
1916
|
-
tpriv->reap_action = EREMOTEIO == errno ? COMPLETED_EARLY : SUBMIT_FAILED;
|
1917
|
-
|
1918
|
-
/* The URBs we haven't submitted yet we count as already
|
1919
|
-
* retired. */
|
1920
|
-
tpriv->num_retired += num_urbs - i;
|
1921
|
-
|
1922
|
-
/* If we completed short then don't try to discard. */
|
1923
|
-
if (COMPLETED_EARLY == tpriv->reap_action)
|
1924
|
-
return 0;
|
1925
|
-
|
1926
|
-
discard_urbs(itransfer, 0, i);
|
1927
|
-
|
1928
|
-
usbi_dbg("reporting successful submission but waiting for %d "
|
1929
|
-
"discards before reporting error", i);
|
1930
|
-
return 0;
|
1931
|
-
}
|
1932
|
-
}
|
1933
|
-
|
1934
|
-
return 0;
|
1935
|
-
}
|
1936
|
-
|
1937
|
-
static int submit_iso_transfer(struct usbi_transfer *itransfer)
|
1938
|
-
{
|
1939
|
-
struct libusb_transfer *transfer =
|
1940
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
1941
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
1942
|
-
struct linux_device_handle_priv *dpriv =
|
1943
|
-
_device_handle_priv(transfer->dev_handle);
|
1944
|
-
struct usbfs_urb **urbs;
|
1945
|
-
size_t alloc_size;
|
1946
|
-
int num_packets = transfer->num_iso_packets;
|
1947
|
-
int i;
|
1948
|
-
int this_urb_len = 0;
|
1949
|
-
int num_urbs = 1;
|
1950
|
-
int packet_offset = 0;
|
1951
|
-
unsigned int packet_len;
|
1952
|
-
unsigned char *urb_buffer = transfer->buffer;
|
1953
|
-
|
1954
|
-
/* usbfs places arbitrary limits on iso URBs. this limit has changed
|
1955
|
-
* at least three times, and it's difficult to accurately detect which
|
1956
|
-
* limit this running kernel might impose. so we attempt to submit
|
1957
|
-
* whatever the user has provided. if the kernel rejects the request
|
1958
|
-
* due to its size, we return an error indicating such to the user.
|
1959
|
-
*/
|
1960
|
-
|
1961
|
-
/* calculate how many URBs we need */
|
1962
|
-
for (i = 0; i < num_packets; i++) {
|
1963
|
-
unsigned int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_len;
|
1964
|
-
packet_len = transfer->iso_packet_desc[i].length;
|
1965
|
-
|
1966
|
-
if (packet_len > space_remaining) {
|
1967
|
-
num_urbs++;
|
1968
|
-
this_urb_len = packet_len;
|
1969
|
-
/* check that we can actually support this packet length */
|
1970
|
-
if (this_urb_len > MAX_ISO_BUFFER_LENGTH)
|
1971
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
1972
|
-
} else {
|
1973
|
-
this_urb_len += packet_len;
|
1974
|
-
}
|
1975
|
-
}
|
1976
|
-
usbi_dbg("need %d %dk URBs for transfer", num_urbs, MAX_ISO_BUFFER_LENGTH / 1024);
|
1977
|
-
|
1978
|
-
urbs = calloc(num_urbs, sizeof(*urbs));
|
1979
|
-
if (!urbs)
|
1980
|
-
return LIBUSB_ERROR_NO_MEM;
|
1981
|
-
|
1982
|
-
tpriv->iso_urbs = urbs;
|
1983
|
-
tpriv->num_urbs = num_urbs;
|
1984
|
-
tpriv->num_retired = 0;
|
1985
|
-
tpriv->reap_action = NORMAL;
|
1986
|
-
tpriv->iso_packet_offset = 0;
|
1987
|
-
|
1988
|
-
/* allocate + initialize each URB with the correct number of packets */
|
1989
|
-
for (i = 0; i < num_urbs; i++) {
|
1990
|
-
struct usbfs_urb *urb;
|
1991
|
-
unsigned int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH;
|
1992
|
-
int urb_packet_offset = 0;
|
1993
|
-
unsigned char *urb_buffer_orig = urb_buffer;
|
1994
|
-
int j;
|
1995
|
-
int k;
|
1996
|
-
|
1997
|
-
/* swallow up all the packets we can fit into this URB */
|
1998
|
-
while (packet_offset < transfer->num_iso_packets) {
|
1999
|
-
packet_len = transfer->iso_packet_desc[packet_offset].length;
|
2000
|
-
if (packet_len <= space_remaining_in_urb) {
|
2001
|
-
/* throw it in */
|
2002
|
-
urb_packet_offset++;
|
2003
|
-
packet_offset++;
|
2004
|
-
space_remaining_in_urb -= packet_len;
|
2005
|
-
urb_buffer += packet_len;
|
2006
|
-
} else {
|
2007
|
-
/* it can't fit, save it for the next URB */
|
2008
|
-
break;
|
2009
|
-
}
|
2010
|
-
}
|
2011
|
-
|
2012
|
-
alloc_size = sizeof(*urb)
|
2013
|
-
+ (urb_packet_offset * sizeof(struct usbfs_iso_packet_desc));
|
2014
|
-
urb = calloc(1, alloc_size);
|
2015
|
-
if (!urb) {
|
2016
|
-
free_iso_urbs(tpriv);
|
2017
|
-
return LIBUSB_ERROR_NO_MEM;
|
2018
|
-
}
|
2019
|
-
urbs[i] = urb;
|
2020
|
-
|
2021
|
-
/* populate packet lengths */
|
2022
|
-
for (j = 0, k = packet_offset - urb_packet_offset;
|
2023
|
-
k < packet_offset; k++, j++) {
|
2024
|
-
packet_len = transfer->iso_packet_desc[k].length;
|
2025
|
-
urb->iso_frame_desc[j].length = packet_len;
|
2026
|
-
}
|
2027
|
-
|
2028
|
-
urb->usercontext = itransfer;
|
2029
|
-
urb->type = USBFS_URB_TYPE_ISO;
|
2030
|
-
/* FIXME: interface for non-ASAP data? */
|
2031
|
-
urb->flags = USBFS_URB_ISO_ASAP;
|
2032
|
-
urb->endpoint = transfer->endpoint;
|
2033
|
-
urb->number_of_packets = urb_packet_offset;
|
2034
|
-
urb->buffer = urb_buffer_orig;
|
2035
|
-
}
|
2036
|
-
|
2037
|
-
/* submit URBs */
|
2038
|
-
for (i = 0; i < num_urbs; i++) {
|
2039
|
-
int r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
|
2040
|
-
if (r < 0) {
|
2041
|
-
if (errno == ENODEV) {
|
2042
|
-
r = LIBUSB_ERROR_NO_DEVICE;
|
2043
|
-
} else if (errno == EINVAL) {
|
2044
|
-
usbi_warn(TRANSFER_CTX(transfer),
|
2045
|
-
"submiturb failed, transfer too large");
|
2046
|
-
r = LIBUSB_ERROR_INVALID_PARAM;
|
2047
|
-
} else {
|
2048
|
-
usbi_err(TRANSFER_CTX(transfer),
|
2049
|
-
"submiturb failed error %d errno=%d", r, errno);
|
2050
|
-
r = LIBUSB_ERROR_IO;
|
2051
|
-
}
|
2052
|
-
|
2053
|
-
/* if the first URB submission fails, we can simply free up and
|
2054
|
-
* return failure immediately. */
|
2055
|
-
if (i == 0) {
|
2056
|
-
usbi_dbg("first URB failed, easy peasy");
|
2057
|
-
free_iso_urbs(tpriv);
|
2058
|
-
return r;
|
2059
|
-
}
|
2060
|
-
|
2061
|
-
/* if it's not the first URB that failed, the situation is a bit
|
2062
|
-
* tricky. we must discard all previous URBs. there are
|
2063
|
-
* complications:
|
2064
|
-
* - discarding is asynchronous - discarded urbs will be reaped
|
2065
|
-
* later. the user must not have freed the transfer when the
|
2066
|
-
* discarded URBs are reaped, otherwise libusb will be using
|
2067
|
-
* freed memory.
|
2068
|
-
* - the earlier URBs may have completed successfully and we do
|
2069
|
-
* not want to throw away any data.
|
2070
|
-
* so, in this case we discard all the previous URBs BUT we report
|
2071
|
-
* that the transfer was submitted successfully. then later when
|
2072
|
-
* the final discard completes we can report error to the user.
|
2073
|
-
*/
|
2074
|
-
tpriv->reap_action = SUBMIT_FAILED;
|
2075
|
-
|
2076
|
-
/* The URBs we haven't submitted yet we count as already
|
2077
|
-
* retired. */
|
2078
|
-
tpriv->num_retired = num_urbs - i;
|
2079
|
-
discard_urbs(itransfer, 0, i);
|
2080
|
-
|
2081
|
-
usbi_dbg("reporting successful submission but waiting for %d "
|
2082
|
-
"discards before reporting error", i);
|
2083
|
-
return 0;
|
2084
|
-
}
|
2085
|
-
}
|
2086
|
-
|
2087
|
-
return 0;
|
2088
|
-
}
|
2089
|
-
|
2090
|
-
static int submit_control_transfer(struct usbi_transfer *itransfer)
|
2091
|
-
{
|
2092
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2093
|
-
struct libusb_transfer *transfer =
|
2094
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2095
|
-
struct linux_device_handle_priv *dpriv =
|
2096
|
-
_device_handle_priv(transfer->dev_handle);
|
2097
|
-
struct usbfs_urb *urb;
|
2098
|
-
int r;
|
2099
|
-
|
2100
|
-
if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
|
2101
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
2102
|
-
|
2103
|
-
urb = calloc(1, sizeof(struct usbfs_urb));
|
2104
|
-
if (!urb)
|
2105
|
-
return LIBUSB_ERROR_NO_MEM;
|
2106
|
-
tpriv->urbs = urb;
|
2107
|
-
tpriv->num_urbs = 1;
|
2108
|
-
tpriv->reap_action = NORMAL;
|
2109
|
-
|
2110
|
-
urb->usercontext = itransfer;
|
2111
|
-
urb->type = USBFS_URB_TYPE_CONTROL;
|
2112
|
-
urb->endpoint = transfer->endpoint;
|
2113
|
-
urb->buffer = transfer->buffer;
|
2114
|
-
urb->buffer_length = transfer->length;
|
2115
|
-
|
2116
|
-
r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
|
2117
|
-
if (r < 0) {
|
2118
|
-
free(urb);
|
2119
|
-
tpriv->urbs = NULL;
|
2120
|
-
if (errno == ENODEV)
|
2121
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
2122
|
-
|
2123
|
-
usbi_err(TRANSFER_CTX(transfer),
|
2124
|
-
"submiturb failed error %d errno=%d", r, errno);
|
2125
|
-
return LIBUSB_ERROR_IO;
|
2126
|
-
}
|
2127
|
-
return 0;
|
2128
|
-
}
|
2129
|
-
|
2130
|
-
static int op_submit_transfer(struct usbi_transfer *itransfer)
|
2131
|
-
{
|
2132
|
-
struct libusb_transfer *transfer =
|
2133
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2134
|
-
|
2135
|
-
switch (transfer->type) {
|
2136
|
-
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
2137
|
-
return submit_control_transfer(itransfer);
|
2138
|
-
case LIBUSB_TRANSFER_TYPE_BULK:
|
2139
|
-
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
2140
|
-
return submit_bulk_transfer(itransfer);
|
2141
|
-
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
2142
|
-
return submit_bulk_transfer(itransfer);
|
2143
|
-
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
2144
|
-
return submit_iso_transfer(itransfer);
|
2145
|
-
default:
|
2146
|
-
usbi_err(TRANSFER_CTX(transfer),
|
2147
|
-
"unknown endpoint type %d", transfer->type);
|
2148
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
2149
|
-
}
|
2150
|
-
}
|
2151
|
-
|
2152
|
-
static int op_cancel_transfer(struct usbi_transfer *itransfer)
|
2153
|
-
{
|
2154
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2155
|
-
struct libusb_transfer *transfer =
|
2156
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2157
|
-
int r;
|
2158
|
-
|
2159
|
-
if (!tpriv->urbs)
|
2160
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
2161
|
-
|
2162
|
-
r = discard_urbs(itransfer, 0, tpriv->num_urbs);
|
2163
|
-
if (r != 0)
|
2164
|
-
return r;
|
2165
|
-
|
2166
|
-
switch (transfer->type) {
|
2167
|
-
case LIBUSB_TRANSFER_TYPE_BULK:
|
2168
|
-
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
2169
|
-
if (tpriv->reap_action == ERROR)
|
2170
|
-
break;
|
2171
|
-
/* else, fall through */
|
2172
|
-
default:
|
2173
|
-
tpriv->reap_action = CANCELLED;
|
2174
|
-
}
|
2175
|
-
|
2176
|
-
return 0;
|
2177
|
-
}
|
2178
|
-
|
2179
|
-
static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
|
2180
|
-
{
|
2181
|
-
struct libusb_transfer *transfer =
|
2182
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2183
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2184
|
-
|
2185
|
-
/* urbs can be freed also in submit_transfer so lock mutex first */
|
2186
|
-
switch (transfer->type) {
|
2187
|
-
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
2188
|
-
case LIBUSB_TRANSFER_TYPE_BULK:
|
2189
|
-
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
2190
|
-
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
2191
|
-
if (tpriv->urbs) {
|
2192
|
-
free(tpriv->urbs);
|
2193
|
-
tpriv->urbs = NULL;
|
2194
|
-
}
|
2195
|
-
break;
|
2196
|
-
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
2197
|
-
if (tpriv->iso_urbs) {
|
2198
|
-
free_iso_urbs(tpriv);
|
2199
|
-
tpriv->iso_urbs = NULL;
|
2200
|
-
}
|
2201
|
-
break;
|
2202
|
-
default:
|
2203
|
-
usbi_err(TRANSFER_CTX(transfer),
|
2204
|
-
"unknown endpoint type %d", transfer->type);
|
2205
|
-
}
|
2206
|
-
}
|
2207
|
-
|
2208
|
-
static int handle_bulk_completion(struct usbi_transfer *itransfer,
|
2209
|
-
struct usbfs_urb *urb)
|
2210
|
-
{
|
2211
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2212
|
-
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2213
|
-
int urb_idx = urb - tpriv->urbs;
|
2214
|
-
|
2215
|
-
usbi_mutex_lock(&itransfer->lock);
|
2216
|
-
usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
|
2217
|
-
urb_idx + 1, tpriv->num_urbs);
|
2218
|
-
|
2219
|
-
tpriv->num_retired++;
|
2220
|
-
|
2221
|
-
if (tpriv->reap_action != NORMAL) {
|
2222
|
-
/* cancelled, submit_fail, or completed early */
|
2223
|
-
usbi_dbg("abnormal reap: urb status %d", urb->status);
|
2224
|
-
|
2225
|
-
/* even though we're in the process of cancelling, it's possible that
|
2226
|
-
* we may receive some data in these URBs that we don't want to lose.
|
2227
|
-
* examples:
|
2228
|
-
* 1. while the kernel is cancelling all the packets that make up an
|
2229
|
-
* URB, a few of them might complete. so we get back a successful
|
2230
|
-
* cancellation *and* some data.
|
2231
|
-
* 2. we receive a short URB which marks the early completion condition,
|
2232
|
-
* so we start cancelling the remaining URBs. however, we're too
|
2233
|
-
* slow and another URB completes (or at least completes partially).
|
2234
|
-
* (this can't happen since we always use BULK_CONTINUATION.)
|
2235
|
-
*
|
2236
|
-
* When this happens, our objectives are not to lose any "surplus" data,
|
2237
|
-
* and also to stick it at the end of the previously-received data
|
2238
|
-
* (closing any holes), so that libusb reports the total amount of
|
2239
|
-
* transferred data and presents it in a contiguous chunk.
|
2240
|
-
*/
|
2241
|
-
if (urb->actual_length > 0) {
|
2242
|
-
unsigned char *target = transfer->buffer + itransfer->transferred;
|
2243
|
-
usbi_dbg("received %d bytes of surplus data", urb->actual_length);
|
2244
|
-
if (urb->buffer != target) {
|
2245
|
-
usbi_dbg("moving surplus data from offset %d to offset %d",
|
2246
|
-
(unsigned char *) urb->buffer - transfer->buffer,
|
2247
|
-
target - transfer->buffer);
|
2248
|
-
memmove(target, urb->buffer, urb->actual_length);
|
2249
|
-
}
|
2250
|
-
itransfer->transferred += urb->actual_length;
|
2251
|
-
}
|
2252
|
-
|
2253
|
-
if (tpriv->num_retired == tpriv->num_urbs) {
|
2254
|
-
usbi_dbg("abnormal reap: last URB handled, reporting");
|
2255
|
-
if (tpriv->reap_action != COMPLETED_EARLY &&
|
2256
|
-
tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
|
2257
|
-
tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
|
2258
|
-
goto completed;
|
2259
|
-
}
|
2260
|
-
goto out_unlock;
|
2261
|
-
}
|
2262
|
-
|
2263
|
-
itransfer->transferred += urb->actual_length;
|
2264
|
-
|
2265
|
-
/* Many of these errors can occur on *any* urb of a multi-urb
|
2266
|
-
* transfer. When they do, we tear down the rest of the transfer.
|
2267
|
-
*/
|
2268
|
-
switch (urb->status) {
|
2269
|
-
case 0:
|
2270
|
-
break;
|
2271
|
-
case -EREMOTEIO: /* short transfer */
|
2272
|
-
break;
|
2273
|
-
case -ENOENT: /* cancelled */
|
2274
|
-
case -ECONNRESET:
|
2275
|
-
break;
|
2276
|
-
case -ENODEV:
|
2277
|
-
case -ESHUTDOWN:
|
2278
|
-
usbi_dbg("device removed");
|
2279
|
-
tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE;
|
2280
|
-
goto cancel_remaining;
|
2281
|
-
case -EPIPE:
|
2282
|
-
usbi_dbg("detected endpoint stall");
|
2283
|
-
if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
|
2284
|
-
tpriv->reap_status = LIBUSB_TRANSFER_STALL;
|
2285
|
-
goto cancel_remaining;
|
2286
|
-
case -EOVERFLOW:
|
2287
|
-
/* overflow can only ever occur in the last urb */
|
2288
|
-
usbi_dbg("overflow, actual_length=%d", urb->actual_length);
|
2289
|
-
if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
|
2290
|
-
tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW;
|
2291
|
-
goto completed;
|
2292
|
-
case -ETIME:
|
2293
|
-
case -EPROTO:
|
2294
|
-
case -EILSEQ:
|
2295
|
-
case -ECOMM:
|
2296
|
-
case -ENOSR:
|
2297
|
-
usbi_dbg("low level error %d", urb->status);
|
2298
|
-
tpriv->reap_action = ERROR;
|
2299
|
-
goto cancel_remaining;
|
2300
|
-
default:
|
2301
|
-
usbi_warn(ITRANSFER_CTX(itransfer),
|
2302
|
-
"unrecognised urb status %d", urb->status);
|
2303
|
-
tpriv->reap_action = ERROR;
|
2304
|
-
goto cancel_remaining;
|
2305
|
-
}
|
2306
|
-
|
2307
|
-
/* if we're the last urb or we got less data than requested then we're
|
2308
|
-
* done */
|
2309
|
-
if (urb_idx == tpriv->num_urbs - 1) {
|
2310
|
-
usbi_dbg("last URB in transfer --> complete!");
|
2311
|
-
goto completed;
|
2312
|
-
} else if (urb->actual_length < urb->buffer_length) {
|
2313
|
-
usbi_dbg("short transfer %d/%d --> complete!",
|
2314
|
-
urb->actual_length, urb->buffer_length);
|
2315
|
-
if (tpriv->reap_action == NORMAL)
|
2316
|
-
tpriv->reap_action = COMPLETED_EARLY;
|
2317
|
-
} else
|
2318
|
-
goto out_unlock;
|
2319
|
-
|
2320
|
-
cancel_remaining:
|
2321
|
-
if (ERROR == tpriv->reap_action && LIBUSB_TRANSFER_COMPLETED == tpriv->reap_status)
|
2322
|
-
tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
|
2323
|
-
|
2324
|
-
if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */
|
2325
|
-
goto completed;
|
2326
|
-
|
2327
|
-
/* cancel remaining urbs and wait for their completion before
|
2328
|
-
* reporting results */
|
2329
|
-
discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs);
|
2330
|
-
|
2331
|
-
out_unlock:
|
2332
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2333
|
-
return 0;
|
2334
|
-
|
2335
|
-
completed:
|
2336
|
-
free(tpriv->urbs);
|
2337
|
-
tpriv->urbs = NULL;
|
2338
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2339
|
-
return CANCELLED == tpriv->reap_action ?
|
2340
|
-
usbi_handle_transfer_cancellation(itransfer) :
|
2341
|
-
usbi_handle_transfer_completion(itransfer, tpriv->reap_status);
|
2342
|
-
}
|
2343
|
-
|
2344
|
-
static int handle_iso_completion(struct usbi_transfer *itransfer,
|
2345
|
-
struct usbfs_urb *urb)
|
2346
|
-
{
|
2347
|
-
struct libusb_transfer *transfer =
|
2348
|
-
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2349
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2350
|
-
int num_urbs = tpriv->num_urbs;
|
2351
|
-
int urb_idx = 0;
|
2352
|
-
int i;
|
2353
|
-
enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
|
2354
|
-
|
2355
|
-
usbi_mutex_lock(&itransfer->lock);
|
2356
|
-
for (i = 0; i < num_urbs; i++) {
|
2357
|
-
if (urb == tpriv->iso_urbs[i]) {
|
2358
|
-
urb_idx = i + 1;
|
2359
|
-
break;
|
2360
|
-
}
|
2361
|
-
}
|
2362
|
-
if (urb_idx == 0) {
|
2363
|
-
usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
|
2364
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2365
|
-
return LIBUSB_ERROR_NOT_FOUND;
|
2366
|
-
}
|
2367
|
-
|
2368
|
-
usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
|
2369
|
-
urb_idx, num_urbs);
|
2370
|
-
|
2371
|
-
/* copy isochronous results back in */
|
2372
|
-
|
2373
|
-
for (i = 0; i < urb->number_of_packets; i++) {
|
2374
|
-
struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
|
2375
|
-
struct libusb_iso_packet_descriptor *lib_desc =
|
2376
|
-
&transfer->iso_packet_desc[tpriv->iso_packet_offset++];
|
2377
|
-
lib_desc->status = LIBUSB_TRANSFER_COMPLETED;
|
2378
|
-
switch (urb_desc->status) {
|
2379
|
-
case 0:
|
2380
|
-
break;
|
2381
|
-
case -ENOENT: /* cancelled */
|
2382
|
-
case -ECONNRESET:
|
2383
|
-
break;
|
2384
|
-
case -ENODEV:
|
2385
|
-
case -ESHUTDOWN:
|
2386
|
-
usbi_dbg("device removed");
|
2387
|
-
lib_desc->status = LIBUSB_TRANSFER_NO_DEVICE;
|
2388
|
-
break;
|
2389
|
-
case -EPIPE:
|
2390
|
-
usbi_dbg("detected endpoint stall");
|
2391
|
-
lib_desc->status = LIBUSB_TRANSFER_STALL;
|
2392
|
-
break;
|
2393
|
-
case -EOVERFLOW:
|
2394
|
-
usbi_dbg("overflow error");
|
2395
|
-
lib_desc->status = LIBUSB_TRANSFER_OVERFLOW;
|
2396
|
-
break;
|
2397
|
-
case -ETIME:
|
2398
|
-
case -EPROTO:
|
2399
|
-
case -EILSEQ:
|
2400
|
-
case -ECOMM:
|
2401
|
-
case -ENOSR:
|
2402
|
-
case -EXDEV:
|
2403
|
-
usbi_dbg("low-level USB error %d", urb_desc->status);
|
2404
|
-
lib_desc->status = LIBUSB_TRANSFER_ERROR;
|
2405
|
-
break;
|
2406
|
-
default:
|
2407
|
-
usbi_warn(TRANSFER_CTX(transfer),
|
2408
|
-
"unrecognised urb status %d", urb_desc->status);
|
2409
|
-
lib_desc->status = LIBUSB_TRANSFER_ERROR;
|
2410
|
-
break;
|
2411
|
-
}
|
2412
|
-
lib_desc->actual_length = urb_desc->actual_length;
|
2413
|
-
}
|
2414
|
-
|
2415
|
-
tpriv->num_retired++;
|
2416
|
-
|
2417
|
-
if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
|
2418
|
-
usbi_dbg("CANCEL: urb status %d", urb->status);
|
2419
|
-
|
2420
|
-
if (tpriv->num_retired == num_urbs) {
|
2421
|
-
usbi_dbg("CANCEL: last URB handled, reporting");
|
2422
|
-
free_iso_urbs(tpriv);
|
2423
|
-
if (tpriv->reap_action == CANCELLED) {
|
2424
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2425
|
-
return usbi_handle_transfer_cancellation(itransfer);
|
2426
|
-
} else {
|
2427
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2428
|
-
return usbi_handle_transfer_completion(itransfer,
|
2429
|
-
LIBUSB_TRANSFER_ERROR);
|
2430
|
-
}
|
2431
|
-
}
|
2432
|
-
goto out;
|
2433
|
-
}
|
2434
|
-
|
2435
|
-
switch (urb->status) {
|
2436
|
-
case 0:
|
2437
|
-
break;
|
2438
|
-
case -ENOENT: /* cancelled */
|
2439
|
-
case -ECONNRESET:
|
2440
|
-
break;
|
2441
|
-
case -ESHUTDOWN:
|
2442
|
-
usbi_dbg("device removed");
|
2443
|
-
status = LIBUSB_TRANSFER_NO_DEVICE;
|
2444
|
-
break;
|
2445
|
-
default:
|
2446
|
-
usbi_warn(TRANSFER_CTX(transfer),
|
2447
|
-
"unrecognised urb status %d", urb->status);
|
2448
|
-
status = LIBUSB_TRANSFER_ERROR;
|
2449
|
-
break;
|
2450
|
-
}
|
2451
|
-
|
2452
|
-
/* if we're the last urb then we're done */
|
2453
|
-
if (urb_idx == num_urbs) {
|
2454
|
-
usbi_dbg("last URB in transfer --> complete!");
|
2455
|
-
free_iso_urbs(tpriv);
|
2456
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2457
|
-
return usbi_handle_transfer_completion(itransfer, status);
|
2458
|
-
}
|
2459
|
-
|
2460
|
-
out:
|
2461
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2462
|
-
return 0;
|
2463
|
-
}
|
2464
|
-
|
2465
|
-
static int handle_control_completion(struct usbi_transfer *itransfer,
|
2466
|
-
struct usbfs_urb *urb)
|
2467
|
-
{
|
2468
|
-
struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
|
2469
|
-
int status;
|
2470
|
-
|
2471
|
-
usbi_mutex_lock(&itransfer->lock);
|
2472
|
-
usbi_dbg("handling completion status %d", urb->status);
|
2473
|
-
|
2474
|
-
itransfer->transferred += urb->actual_length;
|
2475
|
-
|
2476
|
-
if (tpriv->reap_action == CANCELLED) {
|
2477
|
-
if (urb->status != 0 && urb->status != -ENOENT)
|
2478
|
-
usbi_warn(ITRANSFER_CTX(itransfer),
|
2479
|
-
"cancel: unrecognised urb status %d", urb->status);
|
2480
|
-
free(tpriv->urbs);
|
2481
|
-
tpriv->urbs = NULL;
|
2482
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2483
|
-
return usbi_handle_transfer_cancellation(itransfer);
|
2484
|
-
}
|
2485
|
-
|
2486
|
-
switch (urb->status) {
|
2487
|
-
case 0:
|
2488
|
-
status = LIBUSB_TRANSFER_COMPLETED;
|
2489
|
-
break;
|
2490
|
-
case -ENOENT: /* cancelled */
|
2491
|
-
status = LIBUSB_TRANSFER_CANCELLED;
|
2492
|
-
break;
|
2493
|
-
case -ENODEV:
|
2494
|
-
case -ESHUTDOWN:
|
2495
|
-
usbi_dbg("device removed");
|
2496
|
-
status = LIBUSB_TRANSFER_NO_DEVICE;
|
2497
|
-
break;
|
2498
|
-
case -EPIPE:
|
2499
|
-
usbi_dbg("unsupported control request");
|
2500
|
-
status = LIBUSB_TRANSFER_STALL;
|
2501
|
-
break;
|
2502
|
-
case -EOVERFLOW:
|
2503
|
-
usbi_dbg("control overflow error");
|
2504
|
-
status = LIBUSB_TRANSFER_OVERFLOW;
|
2505
|
-
break;
|
2506
|
-
case -ETIME:
|
2507
|
-
case -EPROTO:
|
2508
|
-
case -EILSEQ:
|
2509
|
-
case -ECOMM:
|
2510
|
-
case -ENOSR:
|
2511
|
-
usbi_dbg("low-level bus error occurred");
|
2512
|
-
status = LIBUSB_TRANSFER_ERROR;
|
2513
|
-
break;
|
2514
|
-
default:
|
2515
|
-
usbi_warn(ITRANSFER_CTX(itransfer),
|
2516
|
-
"unrecognised urb status %d", urb->status);
|
2517
|
-
status = LIBUSB_TRANSFER_ERROR;
|
2518
|
-
break;
|
2519
|
-
}
|
2520
|
-
|
2521
|
-
free(tpriv->urbs);
|
2522
|
-
tpriv->urbs = NULL;
|
2523
|
-
usbi_mutex_unlock(&itransfer->lock);
|
2524
|
-
return usbi_handle_transfer_completion(itransfer, status);
|
2525
|
-
}
|
2526
|
-
|
2527
|
-
static int reap_for_handle(struct libusb_device_handle *handle)
|
2528
|
-
{
|
2529
|
-
struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
|
2530
|
-
int r;
|
2531
|
-
struct usbfs_urb *urb;
|
2532
|
-
struct usbi_transfer *itransfer;
|
2533
|
-
struct libusb_transfer *transfer;
|
2534
|
-
|
2535
|
-
r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
|
2536
|
-
if (r == -1 && errno == EAGAIN)
|
2537
|
-
return 1;
|
2538
|
-
if (r < 0) {
|
2539
|
-
if (errno == ENODEV)
|
2540
|
-
return LIBUSB_ERROR_NO_DEVICE;
|
2541
|
-
|
2542
|
-
usbi_err(HANDLE_CTX(handle), "reap failed error %d errno=%d",
|
2543
|
-
r, errno);
|
2544
|
-
return LIBUSB_ERROR_IO;
|
2545
|
-
}
|
2546
|
-
|
2547
|
-
itransfer = urb->usercontext;
|
2548
|
-
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
2549
|
-
|
2550
|
-
usbi_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
|
2551
|
-
urb->actual_length);
|
2552
|
-
|
2553
|
-
switch (transfer->type) {
|
2554
|
-
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
2555
|
-
return handle_iso_completion(itransfer, urb);
|
2556
|
-
case LIBUSB_TRANSFER_TYPE_BULK:
|
2557
|
-
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
2558
|
-
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
2559
|
-
return handle_bulk_completion(itransfer, urb);
|
2560
|
-
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
2561
|
-
return handle_control_completion(itransfer, urb);
|
2562
|
-
default:
|
2563
|
-
usbi_err(HANDLE_CTX(handle), "unrecognised endpoint type %x",
|
2564
|
-
transfer->type);
|
2565
|
-
return LIBUSB_ERROR_OTHER;
|
2566
|
-
}
|
2567
|
-
}
|
2568
|
-
|
2569
|
-
static int op_handle_events(struct libusb_context *ctx,
|
2570
|
-
struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
|
2571
|
-
{
|
2572
|
-
int r;
|
2573
|
-
unsigned int i = 0;
|
2574
|
-
|
2575
|
-
usbi_mutex_lock(&ctx->open_devs_lock);
|
2576
|
-
for (i = 0; i < nfds && num_ready > 0; i++) {
|
2577
|
-
struct pollfd *pollfd = &fds[i];
|
2578
|
-
struct libusb_device_handle *handle;
|
2579
|
-
struct linux_device_handle_priv *hpriv = NULL;
|
2580
|
-
|
2581
|
-
if (!pollfd->revents)
|
2582
|
-
continue;
|
2583
|
-
|
2584
|
-
num_ready--;
|
2585
|
-
list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
|
2586
|
-
hpriv = _device_handle_priv(handle);
|
2587
|
-
if (hpriv->fd == pollfd->fd)
|
2588
|
-
break;
|
2589
|
-
}
|
2590
|
-
|
2591
|
-
if (!hpriv || hpriv->fd != pollfd->fd) {
|
2592
|
-
usbi_err(ctx, "cannot find handle for fd %d",
|
2593
|
-
pollfd->fd);
|
2594
|
-
continue;
|
2595
|
-
}
|
2596
|
-
|
2597
|
-
if (pollfd->revents & POLLERR) {
|
2598
|
-
usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
|
2599
|
-
usbi_handle_disconnect(handle);
|
2600
|
-
/* device will still be marked as attached if hotplug monitor thread
|
2601
|
-
* hasn't processed remove event yet */
|
2602
|
-
usbi_mutex_static_lock(&linux_hotplug_lock);
|
2603
|
-
if (handle->dev->attached)
|
2604
|
-
linux_device_disconnected(handle->dev->bus_number,
|
2605
|
-
handle->dev->device_address, NULL);
|
2606
|
-
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
2607
|
-
continue;
|
2608
|
-
}
|
2609
|
-
|
2610
|
-
do {
|
2611
|
-
r = reap_for_handle(handle);
|
2612
|
-
} while (r == 0);
|
2613
|
-
if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
|
2614
|
-
continue;
|
2615
|
-
else if (r < 0)
|
2616
|
-
goto out;
|
2617
|
-
}
|
2618
|
-
|
2619
|
-
r = 0;
|
2620
|
-
out:
|
2621
|
-
usbi_mutex_unlock(&ctx->open_devs_lock);
|
2622
|
-
return r;
|
2623
|
-
}
|
2624
|
-
|
2625
|
-
static int op_clock_gettime(int clk_id, struct timespec *tp)
|
2626
|
-
{
|
2627
|
-
switch (clk_id) {
|
2628
|
-
case USBI_CLOCK_MONOTONIC:
|
2629
|
-
return clock_gettime(monotonic_clkid, tp);
|
2630
|
-
case USBI_CLOCK_REALTIME:
|
2631
|
-
return clock_gettime(CLOCK_REALTIME, tp);
|
2632
|
-
default:
|
2633
|
-
return LIBUSB_ERROR_INVALID_PARAM;
|
2634
|
-
}
|
2635
|
-
}
|
2636
|
-
|
2637
|
-
#ifdef USBI_TIMERFD_AVAILABLE
|
2638
|
-
static clockid_t op_get_timerfd_clockid(void)
|
2639
|
-
{
|
2640
|
-
return monotonic_clkid;
|
2641
|
-
|
2642
|
-
}
|
2643
|
-
#endif
|
2644
|
-
|
2645
|
-
const struct usbi_os_backend linux_usbfs_backend = {
|
2646
|
-
.name = "Linux usbfs",
|
2647
|
-
.caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
|
2648
|
-
.init = op_init,
|
2649
|
-
.exit = op_exit,
|
2650
|
-
.get_device_list = NULL,
|
2651
|
-
.hotplug_poll = op_hotplug_poll,
|
2652
|
-
.get_device_descriptor = op_get_device_descriptor,
|
2653
|
-
.get_active_config_descriptor = op_get_active_config_descriptor,
|
2654
|
-
.get_config_descriptor = op_get_config_descriptor,
|
2655
|
-
.get_config_descriptor_by_value = op_get_config_descriptor_by_value,
|
2656
|
-
|
2657
|
-
.open = op_open,
|
2658
|
-
.close = op_close,
|
2659
|
-
.get_configuration = op_get_configuration,
|
2660
|
-
.set_configuration = op_set_configuration,
|
2661
|
-
.claim_interface = op_claim_interface,
|
2662
|
-
.release_interface = op_release_interface,
|
2663
|
-
|
2664
|
-
.set_interface_altsetting = op_set_interface,
|
2665
|
-
.clear_halt = op_clear_halt,
|
2666
|
-
.reset_device = op_reset_device,
|
2667
|
-
|
2668
|
-
.alloc_streams = op_alloc_streams,
|
2669
|
-
.free_streams = op_free_streams,
|
2670
|
-
|
2671
|
-
.kernel_driver_active = op_kernel_driver_active,
|
2672
|
-
.detach_kernel_driver = op_detach_kernel_driver,
|
2673
|
-
.attach_kernel_driver = op_attach_kernel_driver,
|
2674
|
-
|
2675
|
-
.destroy_device = op_destroy_device,
|
2676
|
-
|
2677
|
-
.submit_transfer = op_submit_transfer,
|
2678
|
-
.cancel_transfer = op_cancel_transfer,
|
2679
|
-
.clear_transfer_priv = op_clear_transfer_priv,
|
2680
|
-
|
2681
|
-
.handle_events = op_handle_events,
|
2682
|
-
|
2683
|
-
.clock_gettime = op_clock_gettime,
|
2684
|
-
|
2685
|
-
#ifdef USBI_TIMERFD_AVAILABLE
|
2686
|
-
.get_timerfd_clockid = op_get_timerfd_clockid,
|
2687
|
-
#endif
|
2688
|
-
|
2689
|
-
.device_priv_size = sizeof(struct linux_device_priv),
|
2690
|
-
.device_handle_priv_size = sizeof(struct linux_device_handle_priv),
|
2691
|
-
.transfer_priv_size = sizeof(struct linux_transfer_priv),
|
2692
|
-
};
|