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