root / usb-linux.c @ 5557d820
History | View | Annotate | Download (48.6 kB)
1 | bb36d470 | bellard | /*
|
---|---|---|---|
2 | bb36d470 | bellard | * Linux host USB redirector
|
3 | bb36d470 | bellard | *
|
4 | bb36d470 | bellard | * Copyright (c) 2005 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 64838171 | aliguori | * Copyright (c) 2008 Max Krasnyansky
|
7 | 64838171 | aliguori | * Support for host device auto connect & disconnect
|
8 | 5d0c5750 | aliguori | * Major rewrite to support fully async operation
|
9 | 4b096fc9 | aliguori | *
|
10 | 0f431527 | aliguori | * Copyright 2008 TJ <linux@tjworld.net>
|
11 | 0f431527 | aliguori | * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
|
12 | 0f431527 | aliguori | * to the legacy /proc/bus/usb USB device discovery and handling
|
13 | 0f431527 | aliguori | *
|
14 | bb36d470 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
15 | bb36d470 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
16 | bb36d470 | bellard | * in the Software without restriction, including without limitation the rights
|
17 | bb36d470 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
18 | bb36d470 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
19 | bb36d470 | bellard | * furnished to do so, subject to the following conditions:
|
20 | bb36d470 | bellard | *
|
21 | bb36d470 | bellard | * The above copyright notice and this permission notice shall be included in
|
22 | bb36d470 | bellard | * all copies or substantial portions of the Software.
|
23 | bb36d470 | bellard | *
|
24 | bb36d470 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
25 | bb36d470 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
26 | bb36d470 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
27 | bb36d470 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
28 | bb36d470 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
29 | bb36d470 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
30 | bb36d470 | bellard | * THE SOFTWARE.
|
31 | bb36d470 | bellard | */
|
32 | 446ab128 | aliguori | |
33 | 87ecb68b | pbrook | #include "qemu-common.h" |
34 | 1f3870ab | aliguori | #include "qemu-timer.h" |
35 | 376253ec | aliguori | #include "monitor.h" |
36 | b373a63a | Shahar Havivi | #include "sysemu.h" |
37 | bb36d470 | bellard | |
38 | bb36d470 | bellard | #include <dirent.h> |
39 | bb36d470 | bellard | #include <sys/ioctl.h> |
40 | b9dc033c | balrog | #include <signal.h> |
41 | bb36d470 | bellard | |
42 | 446ab128 | aliguori | #include <linux/usbdevice_fs.h> |
43 | 446ab128 | aliguori | #include <linux/version.h> |
44 | 446ab128 | aliguori | #include "hw/usb.h" |
45 | bb36d470 | bellard | |
46 | d9cf1578 | blueswir1 | /* We redefine it to avoid version problems */
|
47 | d9cf1578 | blueswir1 | struct usb_ctrltransfer {
|
48 | d9cf1578 | blueswir1 | uint8_t bRequestType; |
49 | d9cf1578 | blueswir1 | uint8_t bRequest; |
50 | d9cf1578 | blueswir1 | uint16_t wValue; |
51 | d9cf1578 | blueswir1 | uint16_t wIndex; |
52 | d9cf1578 | blueswir1 | uint16_t wLength; |
53 | d9cf1578 | blueswir1 | uint32_t timeout; |
54 | d9cf1578 | blueswir1 | void *data;
|
55 | d9cf1578 | blueswir1 | }; |
56 | d9cf1578 | blueswir1 | |
57 | 5557d820 | Gerd Hoffmann | typedef int USBScanFunc(void *opaque, int bus_num, int addr, char *port, |
58 | 0f5160d1 | Hans de Goede | int class_id, int vendor_id, int product_id, |
59 | a594cfbf | bellard | const char *product_name, int speed); |
60 | 26a9e82a | Gerd Hoffmann | |
61 | 0745eb1e | Markus Armbruster | //#define DEBUG
|
62 | 64838171 | aliguori | |
63 | 64838171 | aliguori | #ifdef DEBUG
|
64 | d0f2c4c6 | malc | #define DPRINTF printf
|
65 | 64838171 | aliguori | #else
|
66 | d0f2c4c6 | malc | #define DPRINTF(...)
|
67 | 64838171 | aliguori | #endif
|
68 | bb36d470 | bellard | |
69 | 5be8e1f2 | blueswir1 | #define USBDBG_DEVOPENED "husb: opened %s/devices\n" |
70 | 5be8e1f2 | blueswir1 | |
71 | 0f431527 | aliguori | #define USBPROCBUS_PATH "/proc/bus/usb" |
72 | 1f6e24e7 | bellard | #define PRODUCT_NAME_SZ 32 |
73 | 3a4854b3 | Hans de Goede | #define MAX_ENDPOINTS 15 |
74 | 5557d820 | Gerd Hoffmann | #define MAX_PORTLEN 16 |
75 | 0f431527 | aliguori | #define USBDEVBUS_PATH "/dev/bus/usb" |
76 | 0f431527 | aliguori | #define USBSYSBUS_PATH "/sys/bus/usb" |
77 | 0f431527 | aliguori | |
78 | 0f431527 | aliguori | static char *usb_host_device_path; |
79 | 0f431527 | aliguori | |
80 | 0f431527 | aliguori | #define USB_FS_NONE 0 |
81 | 0f431527 | aliguori | #define USB_FS_PROC 1 |
82 | 0f431527 | aliguori | #define USB_FS_DEV 2 |
83 | 0f431527 | aliguori | #define USB_FS_SYS 3 |
84 | 0f431527 | aliguori | |
85 | 0f431527 | aliguori | static int usb_fs_type; |
86 | bb36d470 | bellard | |
87 | b9dc033c | balrog | /* endpoint association data */
|
88 | 060dc841 | Hans de Goede | #define ISO_FRAME_DESC_PER_URB 32 |
89 | 060dc841 | Hans de Goede | #define ISO_URB_COUNT 3 |
90 | a0b5fece | Hans de Goede | #define INVALID_EP_TYPE 255 |
91 | 060dc841 | Hans de Goede | |
92 | 060dc841 | Hans de Goede | typedef struct AsyncURB AsyncURB; |
93 | 060dc841 | Hans de Goede | |
94 | b9dc033c | balrog | struct endp_data {
|
95 | b9dc033c | balrog | uint8_t type; |
96 | 64838171 | aliguori | uint8_t halted; |
97 | bb6d5498 | Hans de Goede | uint8_t iso_started; |
98 | 060dc841 | Hans de Goede | AsyncURB *iso_urb; |
99 | 060dc841 | Hans de Goede | int iso_urb_idx;
|
100 | bb6d5498 | Hans de Goede | int iso_buffer_used;
|
101 | 060dc841 | Hans de Goede | int max_packet_size;
|
102 | b9dc033c | balrog | }; |
103 | b9dc033c | balrog | |
104 | 26a9e82a | Gerd Hoffmann | struct USBAutoFilter {
|
105 | 26a9e82a | Gerd Hoffmann | uint32_t bus_num; |
106 | 26a9e82a | Gerd Hoffmann | uint32_t addr; |
107 | 26a9e82a | Gerd Hoffmann | uint32_t vendor_id; |
108 | 26a9e82a | Gerd Hoffmann | uint32_t product_id; |
109 | 26a9e82a | Gerd Hoffmann | }; |
110 | 26a9e82a | Gerd Hoffmann | |
111 | bb36d470 | bellard | typedef struct USBHostDevice { |
112 | bb36d470 | bellard | USBDevice dev; |
113 | 64838171 | aliguori | int fd;
|
114 | 64838171 | aliguori | |
115 | 64838171 | aliguori | uint8_t descr[1024];
|
116 | 64838171 | aliguori | int descr_len;
|
117 | 64838171 | aliguori | int configuration;
|
118 | 446ab128 | aliguori | int ninterfaces;
|
119 | 24772c1e | aliguori | int closing;
|
120 | b373a63a | Shahar Havivi | Notifier exit; |
121 | 64838171 | aliguori | |
122 | b9dc033c | balrog | struct endp_data endp_table[MAX_ENDPOINTS];
|
123 | 4b096fc9 | aliguori | |
124 | 4b096fc9 | aliguori | /* Host side address */
|
125 | 4b096fc9 | aliguori | int bus_num;
|
126 | 4b096fc9 | aliguori | int addr;
|
127 | 5557d820 | Gerd Hoffmann | char port[MAX_PORTLEN];
|
128 | 26a9e82a | Gerd Hoffmann | struct USBAutoFilter match;
|
129 | 4b096fc9 | aliguori | |
130 | 26a9e82a | Gerd Hoffmann | QTAILQ_ENTRY(USBHostDevice) next; |
131 | bb36d470 | bellard | } USBHostDevice; |
132 | bb36d470 | bellard | |
133 | 26a9e82a | Gerd Hoffmann | static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
|
134 | 26a9e82a | Gerd Hoffmann | |
135 | 26a9e82a | Gerd Hoffmann | static int usb_host_close(USBHostDevice *dev); |
136 | 26a9e82a | Gerd Hoffmann | static int parse_filter(const char *spec, struct USBAutoFilter *f); |
137 | 26a9e82a | Gerd Hoffmann | static void usb_host_auto_check(void *unused); |
138 | 2cc59d8c | Hans de Goede | static int usb_host_read_file(char *line, size_t line_size, |
139 | 2cc59d8c | Hans de Goede | const char *device_file, const char *device_name); |
140 | 26a9e82a | Gerd Hoffmann | |
141 | 64838171 | aliguori | static int is_isoc(USBHostDevice *s, int ep) |
142 | 64838171 | aliguori | { |
143 | 64838171 | aliguori | return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO; |
144 | 64838171 | aliguori | } |
145 | 64838171 | aliguori | |
146 | a0b5fece | Hans de Goede | static int is_valid(USBHostDevice *s, int ep) |
147 | a0b5fece | Hans de Goede | { |
148 | a0b5fece | Hans de Goede | return s->endp_table[ep - 1].type != INVALID_EP_TYPE; |
149 | a0b5fece | Hans de Goede | } |
150 | a0b5fece | Hans de Goede | |
151 | 64838171 | aliguori | static int is_halted(USBHostDevice *s, int ep) |
152 | 64838171 | aliguori | { |
153 | 64838171 | aliguori | return s->endp_table[ep - 1].halted; |
154 | 64838171 | aliguori | } |
155 | 64838171 | aliguori | |
156 | 64838171 | aliguori | static void clear_halt(USBHostDevice *s, int ep) |
157 | 64838171 | aliguori | { |
158 | 64838171 | aliguori | s->endp_table[ep - 1].halted = 0; |
159 | 64838171 | aliguori | } |
160 | 64838171 | aliguori | |
161 | 64838171 | aliguori | static void set_halt(USBHostDevice *s, int ep) |
162 | 64838171 | aliguori | { |
163 | 64838171 | aliguori | s->endp_table[ep - 1].halted = 1; |
164 | 64838171 | aliguori | } |
165 | 64838171 | aliguori | |
166 | bb6d5498 | Hans de Goede | static int is_iso_started(USBHostDevice *s, int ep) |
167 | bb6d5498 | Hans de Goede | { |
168 | bb6d5498 | Hans de Goede | return s->endp_table[ep - 1].iso_started; |
169 | bb6d5498 | Hans de Goede | } |
170 | bb6d5498 | Hans de Goede | |
171 | bb6d5498 | Hans de Goede | static void clear_iso_started(USBHostDevice *s, int ep) |
172 | bb6d5498 | Hans de Goede | { |
173 | bb6d5498 | Hans de Goede | s->endp_table[ep - 1].iso_started = 0; |
174 | bb6d5498 | Hans de Goede | } |
175 | bb6d5498 | Hans de Goede | |
176 | bb6d5498 | Hans de Goede | static void set_iso_started(USBHostDevice *s, int ep) |
177 | bb6d5498 | Hans de Goede | { |
178 | bb6d5498 | Hans de Goede | s->endp_table[ep - 1].iso_started = 1; |
179 | bb6d5498 | Hans de Goede | } |
180 | bb6d5498 | Hans de Goede | |
181 | 060dc841 | Hans de Goede | static void set_iso_urb(USBHostDevice *s, int ep, AsyncURB *iso_urb) |
182 | 060dc841 | Hans de Goede | { |
183 | 060dc841 | Hans de Goede | s->endp_table[ep - 1].iso_urb = iso_urb;
|
184 | 060dc841 | Hans de Goede | } |
185 | 060dc841 | Hans de Goede | |
186 | 060dc841 | Hans de Goede | static AsyncURB *get_iso_urb(USBHostDevice *s, int ep) |
187 | 060dc841 | Hans de Goede | { |
188 | 060dc841 | Hans de Goede | return s->endp_table[ep - 1].iso_urb; |
189 | 060dc841 | Hans de Goede | } |
190 | 060dc841 | Hans de Goede | |
191 | 060dc841 | Hans de Goede | static void set_iso_urb_idx(USBHostDevice *s, int ep, int i) |
192 | 060dc841 | Hans de Goede | { |
193 | 060dc841 | Hans de Goede | s->endp_table[ep - 1].iso_urb_idx = i;
|
194 | 060dc841 | Hans de Goede | } |
195 | 060dc841 | Hans de Goede | |
196 | 060dc841 | Hans de Goede | static int get_iso_urb_idx(USBHostDevice *s, int ep) |
197 | 060dc841 | Hans de Goede | { |
198 | 060dc841 | Hans de Goede | return s->endp_table[ep - 1].iso_urb_idx; |
199 | 060dc841 | Hans de Goede | } |
200 | 060dc841 | Hans de Goede | |
201 | bb6d5498 | Hans de Goede | static void set_iso_buffer_used(USBHostDevice *s, int ep, int i) |
202 | bb6d5498 | Hans de Goede | { |
203 | bb6d5498 | Hans de Goede | s->endp_table[ep - 1].iso_buffer_used = i;
|
204 | bb6d5498 | Hans de Goede | } |
205 | bb6d5498 | Hans de Goede | |
206 | bb6d5498 | Hans de Goede | static int get_iso_buffer_used(USBHostDevice *s, int ep) |
207 | bb6d5498 | Hans de Goede | { |
208 | bb6d5498 | Hans de Goede | return s->endp_table[ep - 1].iso_buffer_used; |
209 | bb6d5498 | Hans de Goede | } |
210 | bb6d5498 | Hans de Goede | |
211 | 060dc841 | Hans de Goede | static int get_max_packet_size(USBHostDevice *s, int ep) |
212 | 060dc841 | Hans de Goede | { |
213 | 060dc841 | Hans de Goede | return s->endp_table[ep - 1].max_packet_size; |
214 | 060dc841 | Hans de Goede | } |
215 | 060dc841 | Hans de Goede | |
216 | 2791104c | David Ahern | /*
|
217 | 64838171 | aliguori | * Async URB state.
|
218 | 060dc841 | Hans de Goede | * We always allocate iso packet descriptors even for bulk transfers
|
219 | 2791104c | David Ahern | * to simplify allocation and casts.
|
220 | 64838171 | aliguori | */
|
221 | 060dc841 | Hans de Goede | struct AsyncURB
|
222 | 64838171 | aliguori | { |
223 | 64838171 | aliguori | struct usbdevfs_urb urb;
|
224 | 060dc841 | Hans de Goede | struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
|
225 | b9dc033c | balrog | |
226 | 060dc841 | Hans de Goede | /* For regular async urbs */
|
227 | 64838171 | aliguori | USBPacket *packet; |
228 | 64838171 | aliguori | USBHostDevice *hdev; |
229 | 060dc841 | Hans de Goede | |
230 | 060dc841 | Hans de Goede | /* For buffered iso handling */
|
231 | 060dc841 | Hans de Goede | int iso_frame_idx; /* -1 means in flight */ |
232 | 060dc841 | Hans de Goede | }; |
233 | b9dc033c | balrog | |
234 | 64838171 | aliguori | static AsyncURB *async_alloc(void) |
235 | b9dc033c | balrog | { |
236 | 64838171 | aliguori | return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB)); |
237 | b9dc033c | balrog | } |
238 | b9dc033c | balrog | |
239 | 64838171 | aliguori | static void async_free(AsyncURB *aurb) |
240 | b9dc033c | balrog | { |
241 | 64838171 | aliguori | qemu_free(aurb); |
242 | 64838171 | aliguori | } |
243 | b9dc033c | balrog | |
244 | 64838171 | aliguori | static void async_complete(void *opaque) |
245 | 64838171 | aliguori | { |
246 | 64838171 | aliguori | USBHostDevice *s = opaque; |
247 | 64838171 | aliguori | AsyncURB *aurb; |
248 | 64838171 | aliguori | |
249 | 64838171 | aliguori | while (1) { |
250 | 2791104c | David Ahern | USBPacket *p; |
251 | b9dc033c | balrog | |
252 | 2791104c | David Ahern | int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
|
253 | 64838171 | aliguori | if (r < 0) { |
254 | 2791104c | David Ahern | if (errno == EAGAIN) {
|
255 | 64838171 | aliguori | return;
|
256 | 2791104c | David Ahern | } |
257 | 24772c1e | aliguori | if (errno == ENODEV && !s->closing) {
|
258 | 2791104c | David Ahern | printf("husb: device %d.%d disconnected\n",
|
259 | 2791104c | David Ahern | s->bus_num, s->addr); |
260 | 26a9e82a | Gerd Hoffmann | usb_host_close(s); |
261 | 26a9e82a | Gerd Hoffmann | usb_host_auto_check(NULL);
|
262 | 64838171 | aliguori | return;
|
263 | 64838171 | aliguori | } |
264 | 64838171 | aliguori | |
265 | d0f2c4c6 | malc | DPRINTF("husb: async. reap urb failed errno %d\n", errno);
|
266 | 64838171 | aliguori | return;
|
267 | b9dc033c | balrog | } |
268 | 64838171 | aliguori | |
269 | 2791104c | David Ahern | DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
|
270 | 64838171 | aliguori | aurb, aurb->urb.status, aurb->urb.actual_length); |
271 | 64838171 | aliguori | |
272 | 060dc841 | Hans de Goede | /* If this is a buffered iso urb mark it as complete and don't do
|
273 | 060dc841 | Hans de Goede | anything else (it is handled further in usb_host_handle_iso_data) */
|
274 | 060dc841 | Hans de Goede | if (aurb->iso_frame_idx == -1) { |
275 | 060dc841 | Hans de Goede | if (aurb->urb.status == -EPIPE) {
|
276 | 060dc841 | Hans de Goede | set_halt(s, aurb->urb.endpoint & 0xf);
|
277 | 060dc841 | Hans de Goede | } |
278 | 060dc841 | Hans de Goede | aurb->iso_frame_idx = 0;
|
279 | 060dc841 | Hans de Goede | continue;
|
280 | 060dc841 | Hans de Goede | } |
281 | 060dc841 | Hans de Goede | |
282 | 060dc841 | Hans de Goede | p = aurb->packet; |
283 | 060dc841 | Hans de Goede | |
284 | 2791104c | David Ahern | if (p) {
|
285 | 64838171 | aliguori | switch (aurb->urb.status) {
|
286 | 64838171 | aliguori | case 0: |
287 | 64838171 | aliguori | p->len = aurb->urb.actual_length; |
288 | 64838171 | aliguori | break;
|
289 | 64838171 | aliguori | |
290 | 64838171 | aliguori | case -EPIPE:
|
291 | 64838171 | aliguori | set_halt(s, p->devep); |
292 | 2791104c | David Ahern | p->len = USB_RET_STALL; |
293 | 2791104c | David Ahern | break;
|
294 | dcc7e25f | Paul Bolle | |
295 | 64838171 | aliguori | default:
|
296 | 64838171 | aliguori | p->len = USB_RET_NAK; |
297 | 64838171 | aliguori | break;
|
298 | 64838171 | aliguori | } |
299 | 64838171 | aliguori | |
300 | 50b7963e | Hans de Goede | if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
|
301 | 50b7963e | Hans de Goede | usb_generic_async_ctrl_complete(&s->dev, p); |
302 | 50b7963e | Hans de Goede | } else {
|
303 | 50b7963e | Hans de Goede | usb_packet_complete(&s->dev, p); |
304 | 50b7963e | Hans de Goede | } |
305 | 2791104c | David Ahern | } |
306 | 64838171 | aliguori | |
307 | 64838171 | aliguori | async_free(aurb); |
308 | b9dc033c | balrog | } |
309 | b9dc033c | balrog | } |
310 | b9dc033c | balrog | |
311 | 64838171 | aliguori | static void async_cancel(USBPacket *unused, void *opaque) |
312 | b9dc033c | balrog | { |
313 | 64838171 | aliguori | AsyncURB *aurb = opaque; |
314 | 64838171 | aliguori | USBHostDevice *s = aurb->hdev; |
315 | b9dc033c | balrog | |
316 | d0f2c4c6 | malc | DPRINTF("husb: async cancel. aurb %p\n", aurb);
|
317 | 64838171 | aliguori | |
318 | 64838171 | aliguori | /* Mark it as dead (see async_complete above) */
|
319 | 64838171 | aliguori | aurb->packet = NULL;
|
320 | b9dc033c | balrog | |
321 | 64838171 | aliguori | int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
|
322 | 64838171 | aliguori | if (r < 0) { |
323 | d0f2c4c6 | malc | DPRINTF("husb: async. discard urb failed errno %d\n", errno);
|
324 | b9dc033c | balrog | } |
325 | b9dc033c | balrog | } |
326 | b9dc033c | balrog | |
327 | 446ab128 | aliguori | static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) |
328 | b9dc033c | balrog | { |
329 | b9dc033c | balrog | int dev_descr_len, config_descr_len;
|
330 | d4c4e6fd | Blue Swirl | int interface, nb_interfaces;
|
331 | b9dc033c | balrog | int ret, i;
|
332 | b9dc033c | balrog | |
333 | b9dc033c | balrog | if (configuration == 0) /* address state - ignore */ |
334 | b9dc033c | balrog | return 1; |
335 | b9dc033c | balrog | |
336 | d0f2c4c6 | malc | DPRINTF("husb: claiming interfaces. config %d\n", configuration);
|
337 | 446ab128 | aliguori | |
338 | b9dc033c | balrog | i = 0;
|
339 | b9dc033c | balrog | dev_descr_len = dev->descr[0];
|
340 | 2791104c | David Ahern | if (dev_descr_len > dev->descr_len) {
|
341 | b9dc033c | balrog | goto fail;
|
342 | 2791104c | David Ahern | } |
343 | b9dc033c | balrog | |
344 | b9dc033c | balrog | i += dev_descr_len; |
345 | b9dc033c | balrog | while (i < dev->descr_len) {
|
346 | 2791104c | David Ahern | DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
|
347 | 2791104c | David Ahern | i, dev->descr_len, |
348 | b9dc033c | balrog | dev->descr[i], dev->descr[i+1]);
|
349 | 64838171 | aliguori | |
350 | b9dc033c | balrog | if (dev->descr[i+1] != USB_DT_CONFIG) { |
351 | b9dc033c | balrog | i += dev->descr[i]; |
352 | b9dc033c | balrog | continue;
|
353 | b9dc033c | balrog | } |
354 | b9dc033c | balrog | config_descr_len = dev->descr[i]; |
355 | b9dc033c | balrog | |
356 | 2791104c | David Ahern | printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); |
357 | 1f3870ab | aliguori | |
358 | 446ab128 | aliguori | if (configuration < 0 || configuration == dev->descr[i + 5]) { |
359 | 446ab128 | aliguori | configuration = dev->descr[i + 5];
|
360 | b9dc033c | balrog | break;
|
361 | 446ab128 | aliguori | } |
362 | b9dc033c | balrog | |
363 | b9dc033c | balrog | i += config_descr_len; |
364 | b9dc033c | balrog | } |
365 | b9dc033c | balrog | |
366 | b9dc033c | balrog | if (i >= dev->descr_len) {
|
367 | 2791104c | David Ahern | fprintf(stderr, |
368 | 2791104c | David Ahern | "husb: update iface failed. no matching configuration\n");
|
369 | b9dc033c | balrog | goto fail;
|
370 | b9dc033c | balrog | } |
371 | b9dc033c | balrog | nb_interfaces = dev->descr[i + 4];
|
372 | b9dc033c | balrog | |
373 | b9dc033c | balrog | #ifdef USBDEVFS_DISCONNECT
|
374 | b9dc033c | balrog | /* earlier Linux 2.4 do not support that */
|
375 | b9dc033c | balrog | { |
376 | b9dc033c | balrog | struct usbdevfs_ioctl ctrl;
|
377 | b9dc033c | balrog | for (interface = 0; interface < nb_interfaces; interface++) { |
378 | b9dc033c | balrog | ctrl.ioctl_code = USBDEVFS_DISCONNECT; |
379 | b9dc033c | balrog | ctrl.ifno = interface; |
380 | 021730f7 | Brad Hards | ctrl.data = 0;
|
381 | b9dc033c | balrog | ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); |
382 | b9dc033c | balrog | if (ret < 0 && errno != ENODATA) { |
383 | b9dc033c | balrog | perror("USBDEVFS_DISCONNECT");
|
384 | b9dc033c | balrog | goto fail;
|
385 | b9dc033c | balrog | } |
386 | b9dc033c | balrog | } |
387 | b9dc033c | balrog | } |
388 | b9dc033c | balrog | #endif
|
389 | b9dc033c | balrog | |
390 | b9dc033c | balrog | /* XXX: only grab if all interfaces are free */
|
391 | b9dc033c | balrog | for (interface = 0; interface < nb_interfaces; interface++) { |
392 | b9dc033c | balrog | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
393 | b9dc033c | balrog | if (ret < 0) { |
394 | b9dc033c | balrog | if (errno == EBUSY) {
|
395 | 64838171 | aliguori | printf("husb: update iface. device already grabbed\n");
|
396 | b9dc033c | balrog | } else {
|
397 | 64838171 | aliguori | perror("husb: failed to claim interface");
|
398 | b9dc033c | balrog | } |
399 | b9dc033c | balrog | fail:
|
400 | b9dc033c | balrog | return 0; |
401 | b9dc033c | balrog | } |
402 | b9dc033c | balrog | } |
403 | b9dc033c | balrog | |
404 | 64838171 | aliguori | printf("husb: %d interfaces claimed for configuration %d\n",
|
405 | b9dc033c | balrog | nb_interfaces, configuration); |
406 | b9dc033c | balrog | |
407 | 446ab128 | aliguori | dev->ninterfaces = nb_interfaces; |
408 | 446ab128 | aliguori | dev->configuration = configuration; |
409 | 446ab128 | aliguori | return 1; |
410 | 446ab128 | aliguori | } |
411 | 446ab128 | aliguori | |
412 | 446ab128 | aliguori | static int usb_host_release_interfaces(USBHostDevice *s) |
413 | 446ab128 | aliguori | { |
414 | 446ab128 | aliguori | int ret, i;
|
415 | 446ab128 | aliguori | |
416 | d0f2c4c6 | malc | DPRINTF("husb: releasing interfaces\n");
|
417 | 446ab128 | aliguori | |
418 | 446ab128 | aliguori | for (i = 0; i < s->ninterfaces; i++) { |
419 | 446ab128 | aliguori | ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); |
420 | 446ab128 | aliguori | if (ret < 0) { |
421 | 446ab128 | aliguori | perror("husb: failed to release interface");
|
422 | 446ab128 | aliguori | return 0; |
423 | 446ab128 | aliguori | } |
424 | 446ab128 | aliguori | } |
425 | 446ab128 | aliguori | |
426 | b9dc033c | balrog | return 1; |
427 | b9dc033c | balrog | } |
428 | b9dc033c | balrog | |
429 | 059809e4 | bellard | static void usb_host_handle_reset(USBDevice *dev) |
430 | bb36d470 | bellard | { |
431 | 26a9e82a | Gerd Hoffmann | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
432 | 64838171 | aliguori | |
433 | d0f2c4c6 | malc | DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
|
434 | 64838171 | aliguori | |
435 | bb36d470 | bellard | ioctl(s->fd, USBDEVFS_RESET); |
436 | 446ab128 | aliguori | |
437 | 446ab128 | aliguori | usb_host_claim_interfaces(s, s->configuration); |
438 | 5fafdf24 | ths | } |
439 | bb36d470 | bellard | |
440 | 059809e4 | bellard | static void usb_host_handle_destroy(USBDevice *dev) |
441 | 059809e4 | bellard | { |
442 | 059809e4 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
443 | 059809e4 | bellard | |
444 | 26a9e82a | Gerd Hoffmann | usb_host_close(s); |
445 | 26a9e82a | Gerd Hoffmann | QTAILQ_REMOVE(&hostdevs, s, next); |
446 | b373a63a | Shahar Havivi | qemu_remove_exit_notifier(&s->exit); |
447 | 059809e4 | bellard | } |
448 | 059809e4 | bellard | |
449 | b9dc033c | balrog | static int usb_linux_update_endp_table(USBHostDevice *s); |
450 | b9dc033c | balrog | |
451 | 060dc841 | Hans de Goede | /* iso data is special, we need to keep enough urbs in flight to make sure
|
452 | 060dc841 | Hans de Goede | that the controller never runs out of them, otherwise the device will
|
453 | 060dc841 | Hans de Goede | likely suffer a buffer underrun / overrun. */
|
454 | 060dc841 | Hans de Goede | static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in) |
455 | 060dc841 | Hans de Goede | { |
456 | 060dc841 | Hans de Goede | AsyncURB *aurb; |
457 | 060dc841 | Hans de Goede | int i, j, len = get_max_packet_size(s, ep);
|
458 | 060dc841 | Hans de Goede | |
459 | 060dc841 | Hans de Goede | aurb = qemu_mallocz(ISO_URB_COUNT * sizeof(*aurb));
|
460 | 060dc841 | Hans de Goede | for (i = 0; i < ISO_URB_COUNT; i++) { |
461 | 060dc841 | Hans de Goede | aurb[i].urb.endpoint = ep; |
462 | 060dc841 | Hans de Goede | aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len; |
463 | 060dc841 | Hans de Goede | aurb[i].urb.buffer = qemu_malloc(aurb[i].urb.buffer_length); |
464 | 060dc841 | Hans de Goede | aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO; |
465 | 060dc841 | Hans de Goede | aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP; |
466 | 060dc841 | Hans de Goede | aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB; |
467 | 060dc841 | Hans de Goede | for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++) |
468 | 060dc841 | Hans de Goede | aurb[i].urb.iso_frame_desc[j].length = len; |
469 | 060dc841 | Hans de Goede | if (in) {
|
470 | 060dc841 | Hans de Goede | aurb[i].urb.endpoint |= 0x80;
|
471 | 060dc841 | Hans de Goede | /* Mark as fully consumed (idle) */
|
472 | 060dc841 | Hans de Goede | aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB; |
473 | 060dc841 | Hans de Goede | } |
474 | 060dc841 | Hans de Goede | } |
475 | 060dc841 | Hans de Goede | set_iso_urb(s, ep, aurb); |
476 | 060dc841 | Hans de Goede | |
477 | 060dc841 | Hans de Goede | return aurb;
|
478 | 060dc841 | Hans de Goede | } |
479 | 060dc841 | Hans de Goede | |
480 | 060dc841 | Hans de Goede | static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep) |
481 | 060dc841 | Hans de Goede | { |
482 | 060dc841 | Hans de Goede | AsyncURB *aurb; |
483 | 060dc841 | Hans de Goede | int i, ret, killed = 0, free = 1; |
484 | 060dc841 | Hans de Goede | |
485 | 060dc841 | Hans de Goede | aurb = get_iso_urb(s, ep); |
486 | 060dc841 | Hans de Goede | if (!aurb) {
|
487 | 060dc841 | Hans de Goede | return;
|
488 | 060dc841 | Hans de Goede | } |
489 | 060dc841 | Hans de Goede | |
490 | 060dc841 | Hans de Goede | for (i = 0; i < ISO_URB_COUNT; i++) { |
491 | 060dc841 | Hans de Goede | /* in flight? */
|
492 | 060dc841 | Hans de Goede | if (aurb[i].iso_frame_idx == -1) { |
493 | 060dc841 | Hans de Goede | ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]); |
494 | 060dc841 | Hans de Goede | if (ret < 0) { |
495 | 060dc841 | Hans de Goede | printf("husb: discard isoc in urb failed errno %d\n", errno);
|
496 | 060dc841 | Hans de Goede | free = 0;
|
497 | 060dc841 | Hans de Goede | continue;
|
498 | 060dc841 | Hans de Goede | } |
499 | 060dc841 | Hans de Goede | killed++; |
500 | 060dc841 | Hans de Goede | } |
501 | 060dc841 | Hans de Goede | } |
502 | 060dc841 | Hans de Goede | |
503 | 060dc841 | Hans de Goede | /* Make sure any urbs we've killed are reaped before we free them */
|
504 | 060dc841 | Hans de Goede | if (killed) {
|
505 | 060dc841 | Hans de Goede | async_complete(s); |
506 | 060dc841 | Hans de Goede | } |
507 | 060dc841 | Hans de Goede | |
508 | 060dc841 | Hans de Goede | for (i = 0; i < ISO_URB_COUNT; i++) { |
509 | 060dc841 | Hans de Goede | qemu_free(aurb[i].urb.buffer); |
510 | 060dc841 | Hans de Goede | } |
511 | 060dc841 | Hans de Goede | |
512 | 060dc841 | Hans de Goede | if (free)
|
513 | 060dc841 | Hans de Goede | qemu_free(aurb); |
514 | 060dc841 | Hans de Goede | else
|
515 | 060dc841 | Hans de Goede | printf("husb: leaking iso urbs because of discard failure\n");
|
516 | 060dc841 | Hans de Goede | set_iso_urb(s, ep, NULL);
|
517 | bb6d5498 | Hans de Goede | set_iso_urb_idx(s, ep, 0);
|
518 | bb6d5498 | Hans de Goede | clear_iso_started(s, ep); |
519 | 060dc841 | Hans de Goede | } |
520 | 060dc841 | Hans de Goede | |
521 | 060dc841 | Hans de Goede | static int urb_status_to_usb_ret(int status) |
522 | 060dc841 | Hans de Goede | { |
523 | 060dc841 | Hans de Goede | switch (status) {
|
524 | 060dc841 | Hans de Goede | case -EPIPE:
|
525 | 060dc841 | Hans de Goede | return USB_RET_STALL;
|
526 | 060dc841 | Hans de Goede | default:
|
527 | 060dc841 | Hans de Goede | return USB_RET_NAK;
|
528 | 060dc841 | Hans de Goede | } |
529 | 060dc841 | Hans de Goede | } |
530 | 060dc841 | Hans de Goede | |
531 | bb6d5498 | Hans de Goede | static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) |
532 | 060dc841 | Hans de Goede | { |
533 | 060dc841 | Hans de Goede | AsyncURB *aurb; |
534 | bb6d5498 | Hans de Goede | int i, j, ret, max_packet_size, offset, len = 0; |
535 | 975f2998 | Hans de Goede | |
536 | 975f2998 | Hans de Goede | max_packet_size = get_max_packet_size(s, p->devep); |
537 | 975f2998 | Hans de Goede | if (max_packet_size == 0) |
538 | 975f2998 | Hans de Goede | return USB_RET_NAK;
|
539 | 060dc841 | Hans de Goede | |
540 | 060dc841 | Hans de Goede | aurb = get_iso_urb(s, p->devep); |
541 | 060dc841 | Hans de Goede | if (!aurb) {
|
542 | bb6d5498 | Hans de Goede | aurb = usb_host_alloc_iso(s, p->devep, in); |
543 | 060dc841 | Hans de Goede | } |
544 | 060dc841 | Hans de Goede | |
545 | 060dc841 | Hans de Goede | i = get_iso_urb_idx(s, p->devep); |
546 | 060dc841 | Hans de Goede | j = aurb[i].iso_frame_idx; |
547 | 060dc841 | Hans de Goede | if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) { |
548 | bb6d5498 | Hans de Goede | if (in) {
|
549 | bb6d5498 | Hans de Goede | /* Check urb status */
|
550 | bb6d5498 | Hans de Goede | if (aurb[i].urb.status) {
|
551 | bb6d5498 | Hans de Goede | len = urb_status_to_usb_ret(aurb[i].urb.status); |
552 | bb6d5498 | Hans de Goede | /* Move to the next urb */
|
553 | bb6d5498 | Hans de Goede | aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
|
554 | bb6d5498 | Hans de Goede | /* Check frame status */
|
555 | bb6d5498 | Hans de Goede | } else if (aurb[i].urb.iso_frame_desc[j].status) { |
556 | bb6d5498 | Hans de Goede | len = urb_status_to_usb_ret( |
557 | bb6d5498 | Hans de Goede | aurb[i].urb.iso_frame_desc[j].status); |
558 | bb6d5498 | Hans de Goede | /* Check the frame fits */
|
559 | bb6d5498 | Hans de Goede | } else if (aurb[i].urb.iso_frame_desc[j].actual_length > p->len) { |
560 | bb6d5498 | Hans de Goede | printf("husb: received iso data is larger then packet\n");
|
561 | bb6d5498 | Hans de Goede | len = USB_RET_NAK; |
562 | bb6d5498 | Hans de Goede | /* All good copy data over */
|
563 | bb6d5498 | Hans de Goede | } else {
|
564 | bb6d5498 | Hans de Goede | len = aurb[i].urb.iso_frame_desc[j].actual_length; |
565 | bb6d5498 | Hans de Goede | memcpy(p->data, |
566 | bb6d5498 | Hans de Goede | aurb[i].urb.buffer + |
567 | bb6d5498 | Hans de Goede | j * aurb[i].urb.iso_frame_desc[0].length,
|
568 | bb6d5498 | Hans de Goede | len); |
569 | bb6d5498 | Hans de Goede | } |
570 | 060dc841 | Hans de Goede | } else {
|
571 | bb6d5498 | Hans de Goede | len = p->len; |
572 | bb6d5498 | Hans de Goede | offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->devep); |
573 | bb6d5498 | Hans de Goede | |
574 | bb6d5498 | Hans de Goede | /* Check the frame fits */
|
575 | bb6d5498 | Hans de Goede | if (len > max_packet_size) {
|
576 | bb6d5498 | Hans de Goede | printf("husb: send iso data is larger then max packet size\n");
|
577 | bb6d5498 | Hans de Goede | return USB_RET_NAK;
|
578 | bb6d5498 | Hans de Goede | } |
579 | bb6d5498 | Hans de Goede | |
580 | bb6d5498 | Hans de Goede | /* All good copy data over */
|
581 | bb6d5498 | Hans de Goede | memcpy(aurb[i].urb.buffer + offset, p->data, len); |
582 | bb6d5498 | Hans de Goede | aurb[i].urb.iso_frame_desc[j].length = len; |
583 | bb6d5498 | Hans de Goede | offset += len; |
584 | bb6d5498 | Hans de Goede | set_iso_buffer_used(s, p->devep, offset); |
585 | bb6d5498 | Hans de Goede | |
586 | bb6d5498 | Hans de Goede | /* Start the stream once we have buffered enough data */
|
587 | bb6d5498 | Hans de Goede | if (!is_iso_started(s, p->devep) && i == 1 && j == 8) { |
588 | bb6d5498 | Hans de Goede | set_iso_started(s, p->devep); |
589 | bb6d5498 | Hans de Goede | } |
590 | 060dc841 | Hans de Goede | } |
591 | 060dc841 | Hans de Goede | aurb[i].iso_frame_idx++; |
592 | 060dc841 | Hans de Goede | if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
|
593 | 060dc841 | Hans de Goede | i = (i + 1) % ISO_URB_COUNT;
|
594 | 060dc841 | Hans de Goede | set_iso_urb_idx(s, p->devep, i); |
595 | 060dc841 | Hans de Goede | } |
596 | bb6d5498 | Hans de Goede | } else {
|
597 | bb6d5498 | Hans de Goede | if (in) {
|
598 | bb6d5498 | Hans de Goede | set_iso_started(s, p->devep); |
599 | bb6d5498 | Hans de Goede | } else {
|
600 | bb6d5498 | Hans de Goede | DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
|
601 | bb6d5498 | Hans de Goede | } |
602 | 060dc841 | Hans de Goede | } |
603 | 060dc841 | Hans de Goede | |
604 | bb6d5498 | Hans de Goede | if (is_iso_started(s, p->devep)) {
|
605 | bb6d5498 | Hans de Goede | /* (Re)-submit all fully consumed / filled urbs */
|
606 | bb6d5498 | Hans de Goede | for (i = 0; i < ISO_URB_COUNT; i++) { |
607 | bb6d5498 | Hans de Goede | if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
|
608 | bb6d5498 | Hans de Goede | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]); |
609 | bb6d5498 | Hans de Goede | if (ret < 0) { |
610 | bb6d5498 | Hans de Goede | printf("husb error submitting iso urb %d: %d\n", i, errno);
|
611 | bb6d5498 | Hans de Goede | if (!in || len == 0) { |
612 | bb6d5498 | Hans de Goede | switch(errno) {
|
613 | bb6d5498 | Hans de Goede | case ETIMEDOUT:
|
614 | bb6d5498 | Hans de Goede | len = USB_RET_NAK; |
615 | 0225e254 | Stefan Weil | break;
|
616 | bb6d5498 | Hans de Goede | case EPIPE:
|
617 | bb6d5498 | Hans de Goede | default:
|
618 | bb6d5498 | Hans de Goede | len = USB_RET_STALL; |
619 | bb6d5498 | Hans de Goede | } |
620 | 060dc841 | Hans de Goede | } |
621 | bb6d5498 | Hans de Goede | break;
|
622 | 060dc841 | Hans de Goede | } |
623 | bb6d5498 | Hans de Goede | aurb[i].iso_frame_idx = -1;
|
624 | 060dc841 | Hans de Goede | } |
625 | 060dc841 | Hans de Goede | } |
626 | 060dc841 | Hans de Goede | } |
627 | 060dc841 | Hans de Goede | |
628 | 060dc841 | Hans de Goede | return len;
|
629 | 060dc841 | Hans de Goede | } |
630 | 060dc841 | Hans de Goede | |
631 | 50b7963e | Hans de Goede | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
632 | bb36d470 | bellard | { |
633 | 50b7963e | Hans de Goede | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
634 | 64838171 | aliguori | struct usbdevfs_urb *urb;
|
635 | 446ab128 | aliguori | AsyncURB *aurb; |
636 | bb36d470 | bellard | int ret;
|
637 | 060dc841 | Hans de Goede | uint8_t ep; |
638 | b9dc033c | balrog | |
639 | a0b5fece | Hans de Goede | if (!is_valid(s, p->devep)) {
|
640 | a0b5fece | Hans de Goede | return USB_RET_NAK;
|
641 | a0b5fece | Hans de Goede | } |
642 | a0b5fece | Hans de Goede | |
643 | 2791104c | David Ahern | if (p->pid == USB_TOKEN_IN) {
|
644 | 060dc841 | Hans de Goede | ep = p->devep | 0x80;
|
645 | 2791104c | David Ahern | } else {
|
646 | 060dc841 | Hans de Goede | ep = p->devep; |
647 | 2791104c | David Ahern | } |
648 | 64838171 | aliguori | |
649 | 64838171 | aliguori | if (is_halted(s, p->devep)) {
|
650 | 060dc841 | Hans de Goede | ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &ep); |
651 | 64838171 | aliguori | if (ret < 0) { |
652 | 2791104c | David Ahern | DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
|
653 | 060dc841 | Hans de Goede | ep, errno); |
654 | bb36d470 | bellard | return USB_RET_NAK;
|
655 | bb36d470 | bellard | } |
656 | 64838171 | aliguori | clear_halt(s, p->devep); |
657 | 4d043a09 | balrog | } |
658 | 4d043a09 | balrog | |
659 | bb6d5498 | Hans de Goede | if (is_isoc(s, p->devep)) {
|
660 | bb6d5498 | Hans de Goede | return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
|
661 | bb6d5498 | Hans de Goede | } |
662 | 060dc841 | Hans de Goede | |
663 | 060dc841 | Hans de Goede | aurb = async_alloc(); |
664 | 060dc841 | Hans de Goede | aurb->hdev = s; |
665 | 060dc841 | Hans de Goede | aurb->packet = p; |
666 | 060dc841 | Hans de Goede | |
667 | 060dc841 | Hans de Goede | urb = &aurb->urb; |
668 | 060dc841 | Hans de Goede | |
669 | 060dc841 | Hans de Goede | urb->endpoint = ep; |
670 | 64838171 | aliguori | urb->buffer = p->data; |
671 | 64838171 | aliguori | urb->buffer_length = p->len; |
672 | bb6d5498 | Hans de Goede | urb->type = USBDEVFS_URB_TYPE_BULK; |
673 | bb6d5498 | Hans de Goede | urb->usercontext = s; |
674 | b9dc033c | balrog | |
675 | 64838171 | aliguori | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
676 | b9dc033c | balrog | |
677 | 2791104c | David Ahern | DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
|
678 | 2791104c | David Ahern | urb->endpoint, p->len, aurb); |
679 | b9dc033c | balrog | |
680 | 64838171 | aliguori | if (ret < 0) { |
681 | d0f2c4c6 | malc | DPRINTF("husb: submit failed. errno %d\n", errno);
|
682 | 64838171 | aliguori | async_free(aurb); |
683 | b9dc033c | balrog | |
684 | b9dc033c | balrog | switch(errno) {
|
685 | b9dc033c | balrog | case ETIMEDOUT:
|
686 | b9dc033c | balrog | return USB_RET_NAK;
|
687 | b9dc033c | balrog | case EPIPE:
|
688 | b9dc033c | balrog | default:
|
689 | b9dc033c | balrog | return USB_RET_STALL;
|
690 | b9dc033c | balrog | } |
691 | b9dc033c | balrog | } |
692 | 64838171 | aliguori | |
693 | 64838171 | aliguori | usb_defer_packet(p, async_cancel, aurb); |
694 | b9dc033c | balrog | return USB_RET_ASYNC;
|
695 | b9dc033c | balrog | } |
696 | b9dc033c | balrog | |
697 | 446ab128 | aliguori | static int ctrl_error(void) |
698 | 446ab128 | aliguori | { |
699 | 2791104c | David Ahern | if (errno == ETIMEDOUT) {
|
700 | 446ab128 | aliguori | return USB_RET_NAK;
|
701 | 2791104c | David Ahern | } else {
|
702 | 446ab128 | aliguori | return USB_RET_STALL;
|
703 | 2791104c | David Ahern | } |
704 | 446ab128 | aliguori | } |
705 | 446ab128 | aliguori | |
706 | 446ab128 | aliguori | static int usb_host_set_address(USBHostDevice *s, int addr) |
707 | 446ab128 | aliguori | { |
708 | d0f2c4c6 | malc | DPRINTF("husb: ctrl set addr %u\n", addr);
|
709 | 446ab128 | aliguori | s->dev.addr = addr; |
710 | 446ab128 | aliguori | return 0; |
711 | 446ab128 | aliguori | } |
712 | 446ab128 | aliguori | |
713 | 446ab128 | aliguori | static int usb_host_set_config(USBHostDevice *s, int config) |
714 | 446ab128 | aliguori | { |
715 | 446ab128 | aliguori | usb_host_release_interfaces(s); |
716 | 446ab128 | aliguori | |
717 | 446ab128 | aliguori | int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
|
718 | 2791104c | David Ahern | |
719 | d0f2c4c6 | malc | DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
|
720 | 2791104c | David Ahern | |
721 | 2791104c | David Ahern | if (ret < 0) { |
722 | 446ab128 | aliguori | return ctrl_error();
|
723 | 2791104c | David Ahern | } |
724 | 446ab128 | aliguori | usb_host_claim_interfaces(s, config); |
725 | 446ab128 | aliguori | return 0; |
726 | 446ab128 | aliguori | } |
727 | 446ab128 | aliguori | |
728 | 446ab128 | aliguori | static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) |
729 | 446ab128 | aliguori | { |
730 | 446ab128 | aliguori | struct usbdevfs_setinterface si;
|
731 | 060dc841 | Hans de Goede | int i, ret;
|
732 | 060dc841 | Hans de Goede | |
733 | 3a4854b3 | Hans de Goede | for (i = 1; i <= MAX_ENDPOINTS; i++) { |
734 | 060dc841 | Hans de Goede | if (is_isoc(s, i)) {
|
735 | 060dc841 | Hans de Goede | usb_host_stop_n_free_iso(s, i); |
736 | 060dc841 | Hans de Goede | } |
737 | 060dc841 | Hans de Goede | } |
738 | 446ab128 | aliguori | |
739 | 446ab128 | aliguori | si.interface = iface; |
740 | 446ab128 | aliguori | si.altsetting = alt; |
741 | 446ab128 | aliguori | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); |
742 | 446ab128 | aliguori | |
743 | 2791104c | David Ahern | DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
|
744 | 2791104c | David Ahern | iface, alt, ret, errno); |
745 | 2791104c | David Ahern | |
746 | 2791104c | David Ahern | if (ret < 0) { |
747 | 2791104c | David Ahern | return ctrl_error();
|
748 | 2791104c | David Ahern | } |
749 | 446ab128 | aliguori | usb_linux_update_endp_table(s); |
750 | 446ab128 | aliguori | return 0; |
751 | 446ab128 | aliguori | } |
752 | 446ab128 | aliguori | |
753 | 50b7963e | Hans de Goede | static int usb_host_handle_control(USBDevice *dev, USBPacket *p, |
754 | 50b7963e | Hans de Goede | int request, int value, int index, int length, uint8_t *data) |
755 | 446ab128 | aliguori | { |
756 | 50b7963e | Hans de Goede | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
757 | 446ab128 | aliguori | struct usbdevfs_urb *urb;
|
758 | 446ab128 | aliguori | AsyncURB *aurb; |
759 | 50b7963e | Hans de Goede | int ret;
|
760 | 446ab128 | aliguori | |
761 | 2791104c | David Ahern | /*
|
762 | 446ab128 | aliguori | * Process certain standard device requests.
|
763 | 446ab128 | aliguori | * These are infrequent and are processed synchronously.
|
764 | 446ab128 | aliguori | */
|
765 | 446ab128 | aliguori | |
766 | 50b7963e | Hans de Goede | /* Note request is (bRequestType << 8) | bRequest */
|
767 | d0f2c4c6 | malc | DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
|
768 | 50b7963e | Hans de Goede | request >> 8, request & 0xff, value, index, length); |
769 | 446ab128 | aliguori | |
770 | 50b7963e | Hans de Goede | switch (request) {
|
771 | 50b7963e | Hans de Goede | case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
772 | 50b7963e | Hans de Goede | return usb_host_set_address(s, value);
|
773 | 446ab128 | aliguori | |
774 | 50b7963e | Hans de Goede | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
775 | 50b7963e | Hans de Goede | return usb_host_set_config(s, value & 0xff); |
776 | 446ab128 | aliguori | |
777 | 50b7963e | Hans de Goede | case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
778 | 446ab128 | aliguori | return usb_host_set_interface(s, index, value);
|
779 | 2791104c | David Ahern | } |
780 | 446ab128 | aliguori | |
781 | 446ab128 | aliguori | /* The rest are asynchronous */
|
782 | 446ab128 | aliguori | |
783 | 50b7963e | Hans de Goede | if (length > sizeof(dev->data_buf)) { |
784 | 50b7963e | Hans de Goede | fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
|
785 | 50b7963e | Hans de Goede | length, sizeof(dev->data_buf));
|
786 | b2e3b6e9 | malc | return USB_RET_STALL;
|
787 | c4c0e236 | Jim Paris | } |
788 | c4c0e236 | Jim Paris | |
789 | 446ab128 | aliguori | aurb = async_alloc(); |
790 | 446ab128 | aliguori | aurb->hdev = s; |
791 | 446ab128 | aliguori | aurb->packet = p; |
792 | 446ab128 | aliguori | |
793 | 2791104c | David Ahern | /*
|
794 | 446ab128 | aliguori | * Setup ctrl transfer.
|
795 | 446ab128 | aliguori | *
|
796 | a0102082 | Brad Hards | * s->ctrl is laid out such that data buffer immediately follows
|
797 | 446ab128 | aliguori | * 'req' struct which is exactly what usbdevfs expects.
|
798 | 2791104c | David Ahern | */
|
799 | 446ab128 | aliguori | urb = &aurb->urb; |
800 | 446ab128 | aliguori | |
801 | 446ab128 | aliguori | urb->type = USBDEVFS_URB_TYPE_CONTROL; |
802 | 446ab128 | aliguori | urb->endpoint = p->devep; |
803 | 446ab128 | aliguori | |
804 | 50b7963e | Hans de Goede | urb->buffer = &dev->setup_buf; |
805 | 50b7963e | Hans de Goede | urb->buffer_length = length + 8;
|
806 | 446ab128 | aliguori | |
807 | 446ab128 | aliguori | urb->usercontext = s; |
808 | 446ab128 | aliguori | |
809 | 446ab128 | aliguori | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
810 | 446ab128 | aliguori | |
811 | d0f2c4c6 | malc | DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
|
812 | 446ab128 | aliguori | |
813 | 446ab128 | aliguori | if (ret < 0) { |
814 | d0f2c4c6 | malc | DPRINTF("husb: submit failed. errno %d\n", errno);
|
815 | 446ab128 | aliguori | async_free(aurb); |
816 | 446ab128 | aliguori | |
817 | 446ab128 | aliguori | switch(errno) {
|
818 | 446ab128 | aliguori | case ETIMEDOUT:
|
819 | 446ab128 | aliguori | return USB_RET_NAK;
|
820 | 446ab128 | aliguori | case EPIPE:
|
821 | 446ab128 | aliguori | default:
|
822 | 446ab128 | aliguori | return USB_RET_STALL;
|
823 | 446ab128 | aliguori | } |
824 | 446ab128 | aliguori | } |
825 | 446ab128 | aliguori | |
826 | 446ab128 | aliguori | usb_defer_packet(p, async_cancel, aurb); |
827 | 446ab128 | aliguori | return USB_RET_ASYNC;
|
828 | 446ab128 | aliguori | } |
829 | 446ab128 | aliguori | |
830 | 71d71bbd | Hans de Goede | static int usb_linux_get_configuration(USBHostDevice *s) |
831 | b9dc033c | balrog | { |
832 | 71d71bbd | Hans de Goede | uint8_t configuration; |
833 | e41b3910 | pbrook | struct usb_ctrltransfer ct;
|
834 | 71d71bbd | Hans de Goede | int ret;
|
835 | b9dc033c | balrog | |
836 | 2cc59d8c | Hans de Goede | if (usb_fs_type == USB_FS_SYS) {
|
837 | 2cc59d8c | Hans de Goede | char device_name[32], line[1024]; |
838 | 2cc59d8c | Hans de Goede | int configuration;
|
839 | 2cc59d8c | Hans de Goede | |
840 | 5557d820 | Gerd Hoffmann | sprintf(device_name, "%d-%s", s->bus_num, s->port);
|
841 | 2cc59d8c | Hans de Goede | |
842 | 2cc59d8c | Hans de Goede | if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue", |
843 | 2cc59d8c | Hans de Goede | device_name)) { |
844 | 2cc59d8c | Hans de Goede | goto usbdevfs;
|
845 | 2cc59d8c | Hans de Goede | } |
846 | 2cc59d8c | Hans de Goede | if (sscanf(line, "%d", &configuration) != 1) { |
847 | 2cc59d8c | Hans de Goede | goto usbdevfs;
|
848 | 2cc59d8c | Hans de Goede | } |
849 | 2cc59d8c | Hans de Goede | return configuration;
|
850 | 2cc59d8c | Hans de Goede | } |
851 | 2cc59d8c | Hans de Goede | |
852 | 2cc59d8c | Hans de Goede | usbdevfs:
|
853 | b9dc033c | balrog | ct.bRequestType = USB_DIR_IN; |
854 | b9dc033c | balrog | ct.bRequest = USB_REQ_GET_CONFIGURATION; |
855 | b9dc033c | balrog | ct.wValue = 0;
|
856 | b9dc033c | balrog | ct.wIndex = 0;
|
857 | b9dc033c | balrog | ct.wLength = 1;
|
858 | b9dc033c | balrog | ct.data = &configuration; |
859 | b9dc033c | balrog | ct.timeout = 50;
|
860 | b9dc033c | balrog | |
861 | b9dc033c | balrog | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
862 | b9dc033c | balrog | if (ret < 0) { |
863 | 71d71bbd | Hans de Goede | perror("usb_linux_get_configuration");
|
864 | 71d71bbd | Hans de Goede | return -1; |
865 | b9dc033c | balrog | } |
866 | b9dc033c | balrog | |
867 | b9dc033c | balrog | /* in address state */
|
868 | 2791104c | David Ahern | if (configuration == 0) { |
869 | 71d71bbd | Hans de Goede | return -1; |
870 | 2791104c | David Ahern | } |
871 | b9dc033c | balrog | |
872 | 71d71bbd | Hans de Goede | return configuration;
|
873 | 71d71bbd | Hans de Goede | } |
874 | 71d71bbd | Hans de Goede | |
875 | ed3a328d | Hans de Goede | static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
|
876 | ed3a328d | Hans de Goede | uint8_t configuration, uint8_t interface) |
877 | ed3a328d | Hans de Goede | { |
878 | ed3a328d | Hans de Goede | uint8_t alt_setting; |
879 | ed3a328d | Hans de Goede | struct usb_ctrltransfer ct;
|
880 | ed3a328d | Hans de Goede | int ret;
|
881 | ed3a328d | Hans de Goede | |
882 | c43831fb | Hans de Goede | if (usb_fs_type == USB_FS_SYS) {
|
883 | c43831fb | Hans de Goede | char device_name[64], line[1024]; |
884 | c43831fb | Hans de Goede | int alt_setting;
|
885 | c43831fb | Hans de Goede | |
886 | 5557d820 | Gerd Hoffmann | sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
|
887 | c43831fb | Hans de Goede | (int)configuration, (int)interface); |
888 | c43831fb | Hans de Goede | |
889 | c43831fb | Hans de Goede | if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting", |
890 | c43831fb | Hans de Goede | device_name)) { |
891 | c43831fb | Hans de Goede | goto usbdevfs;
|
892 | c43831fb | Hans de Goede | } |
893 | c43831fb | Hans de Goede | if (sscanf(line, "%d", &alt_setting) != 1) { |
894 | c43831fb | Hans de Goede | goto usbdevfs;
|
895 | c43831fb | Hans de Goede | } |
896 | c43831fb | Hans de Goede | return alt_setting;
|
897 | c43831fb | Hans de Goede | } |
898 | c43831fb | Hans de Goede | |
899 | c43831fb | Hans de Goede | usbdevfs:
|
900 | ed3a328d | Hans de Goede | ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE; |
901 | ed3a328d | Hans de Goede | ct.bRequest = USB_REQ_GET_INTERFACE; |
902 | ed3a328d | Hans de Goede | ct.wValue = 0;
|
903 | ed3a328d | Hans de Goede | ct.wIndex = interface; |
904 | ed3a328d | Hans de Goede | ct.wLength = 1;
|
905 | ed3a328d | Hans de Goede | ct.data = &alt_setting; |
906 | ed3a328d | Hans de Goede | ct.timeout = 50;
|
907 | ed3a328d | Hans de Goede | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
908 | ed3a328d | Hans de Goede | if (ret < 0) { |
909 | ed3a328d | Hans de Goede | /* Assume alt 0 on error */
|
910 | ed3a328d | Hans de Goede | return 0; |
911 | ed3a328d | Hans de Goede | } |
912 | ed3a328d | Hans de Goede | |
913 | ed3a328d | Hans de Goede | return alt_setting;
|
914 | ed3a328d | Hans de Goede | } |
915 | ed3a328d | Hans de Goede | |
916 | 71d71bbd | Hans de Goede | /* returns 1 on problem encountered or 0 for success */
|
917 | 71d71bbd | Hans de Goede | static int usb_linux_update_endp_table(USBHostDevice *s) |
918 | 71d71bbd | Hans de Goede | { |
919 | 71d71bbd | Hans de Goede | uint8_t *descriptors; |
920 | 71d71bbd | Hans de Goede | uint8_t devep, type, configuration, alt_interface; |
921 | ed3a328d | Hans de Goede | int interface, length, i;
|
922 | 71d71bbd | Hans de Goede | |
923 | a0b5fece | Hans de Goede | for (i = 0; i < MAX_ENDPOINTS; i++) |
924 | a0b5fece | Hans de Goede | s->endp_table[i].type = INVALID_EP_TYPE; |
925 | a0b5fece | Hans de Goede | |
926 | 71d71bbd | Hans de Goede | i = usb_linux_get_configuration(s); |
927 | 71d71bbd | Hans de Goede | if (i < 0) |
928 | 71d71bbd | Hans de Goede | return 1; |
929 | 71d71bbd | Hans de Goede | configuration = i; |
930 | 71d71bbd | Hans de Goede | |
931 | b9dc033c | balrog | /* get the desired configuration, interface, and endpoint descriptors
|
932 | b9dc033c | balrog | * from device description */
|
933 | b9dc033c | balrog | descriptors = &s->descr[18];
|
934 | b9dc033c | balrog | length = s->descr_len - 18;
|
935 | b9dc033c | balrog | i = 0;
|
936 | b9dc033c | balrog | |
937 | b9dc033c | balrog | if (descriptors[i + 1] != USB_DT_CONFIG || |
938 | b9dc033c | balrog | descriptors[i + 5] != configuration) {
|
939 | d0f2c4c6 | malc | DPRINTF("invalid descriptor data - configuration\n");
|
940 | b9dc033c | balrog | return 1; |
941 | b9dc033c | balrog | } |
942 | b9dc033c | balrog | i += descriptors[i]; |
943 | b9dc033c | balrog | |
944 | b9dc033c | balrog | while (i < length) {
|
945 | b9dc033c | balrog | if (descriptors[i + 1] != USB_DT_INTERFACE || |
946 | b9dc033c | balrog | (descriptors[i + 1] == USB_DT_INTERFACE &&
|
947 | b9dc033c | balrog | descriptors[i + 4] == 0)) { |
948 | b9dc033c | balrog | i += descriptors[i]; |
949 | b9dc033c | balrog | continue;
|
950 | b9dc033c | balrog | } |
951 | b9dc033c | balrog | |
952 | b9dc033c | balrog | interface = descriptors[i + 2];
|
953 | ed3a328d | Hans de Goede | alt_interface = usb_linux_get_alt_setting(s, configuration, interface); |
954 | b9dc033c | balrog | |
955 | b9dc033c | balrog | /* the current interface descriptor is the active interface
|
956 | b9dc033c | balrog | * and has endpoints */
|
957 | b9dc033c | balrog | if (descriptors[i + 3] != alt_interface) { |
958 | b9dc033c | balrog | i += descriptors[i]; |
959 | b9dc033c | balrog | continue;
|
960 | b9dc033c | balrog | } |
961 | b9dc033c | balrog | |
962 | b9dc033c | balrog | /* advance to the endpoints */
|
963 | 2791104c | David Ahern | while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) { |
964 | b9dc033c | balrog | i += descriptors[i]; |
965 | 2791104c | David Ahern | } |
966 | b9dc033c | balrog | |
967 | b9dc033c | balrog | if (i >= length)
|
968 | b9dc033c | balrog | break;
|
969 | b9dc033c | balrog | |
970 | b9dc033c | balrog | while (i < length) {
|
971 | 2791104c | David Ahern | if (descriptors[i + 1] != USB_DT_ENDPOINT) { |
972 | b9dc033c | balrog | break;
|
973 | 2791104c | David Ahern | } |
974 | b9dc033c | balrog | |
975 | b9dc033c | balrog | devep = descriptors[i + 2];
|
976 | b9dc033c | balrog | switch (descriptors[i + 3] & 0x3) { |
977 | b9dc033c | balrog | case 0x00: |
978 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_CONTROL; |
979 | b9dc033c | balrog | break;
|
980 | b9dc033c | balrog | case 0x01: |
981 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_ISO; |
982 | 060dc841 | Hans de Goede | s->endp_table[(devep & 0xf) - 1].max_packet_size = |
983 | 060dc841 | Hans de Goede | descriptors[i + 4] + (descriptors[i + 5] << 8); |
984 | b9dc033c | balrog | break;
|
985 | b9dc033c | balrog | case 0x02: |
986 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_BULK; |
987 | b9dc033c | balrog | break;
|
988 | b9dc033c | balrog | case 0x03: |
989 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_INTERRUPT; |
990 | b9dc033c | balrog | break;
|
991 | ddbda432 | Anthony Liguori | default:
|
992 | ddbda432 | Anthony Liguori | DPRINTF("usb_host: malformed endpoint type\n");
|
993 | ddbda432 | Anthony Liguori | type = USBDEVFS_URB_TYPE_BULK; |
994 | b9dc033c | balrog | } |
995 | b9dc033c | balrog | s->endp_table[(devep & 0xf) - 1].type = type; |
996 | 64838171 | aliguori | s->endp_table[(devep & 0xf) - 1].halted = 0; |
997 | b9dc033c | balrog | |
998 | b9dc033c | balrog | i += descriptors[i]; |
999 | b9dc033c | balrog | } |
1000 | b9dc033c | balrog | } |
1001 | b9dc033c | balrog | return 0; |
1002 | b9dc033c | balrog | } |
1003 | b9dc033c | balrog | |
1004 | 26a9e82a | Gerd Hoffmann | static int usb_host_open(USBHostDevice *dev, int bus_num, |
1005 | 5557d820 | Gerd Hoffmann | int addr, char *port, const char *prod_name) |
1006 | bb36d470 | bellard | { |
1007 | b9dc033c | balrog | int fd = -1, ret; |
1008 | bb36d470 | bellard | struct usbdevfs_connectinfo ci;
|
1009 | a594cfbf | bellard | char buf[1024]; |
1010 | 1f3870ab | aliguori | |
1011 | 2791104c | David Ahern | if (dev->fd != -1) { |
1012 | 26a9e82a | Gerd Hoffmann | goto fail;
|
1013 | 2791104c | David Ahern | } |
1014 | 64838171 | aliguori | printf("husb: open device %d.%d\n", bus_num, addr);
|
1015 | 3b46e624 | ths | |
1016 | 0f431527 | aliguori | if (!usb_host_device_path) {
|
1017 | 0f431527 | aliguori | perror("husb: USB Host Device Path not set");
|
1018 | 0f431527 | aliguori | goto fail;
|
1019 | 0f431527 | aliguori | } |
1020 | 0f431527 | aliguori | snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path, |
1021 | a594cfbf | bellard | bus_num, addr); |
1022 | b9dc033c | balrog | fd = open(buf, O_RDWR | O_NONBLOCK); |
1023 | bb36d470 | bellard | if (fd < 0) { |
1024 | a594cfbf | bellard | perror(buf); |
1025 | 1f3870ab | aliguori | goto fail;
|
1026 | bb36d470 | bellard | } |
1027 | d0f2c4c6 | malc | DPRINTF("husb: opened %s\n", buf);
|
1028 | bb36d470 | bellard | |
1029 | 806b6024 | Gerd Hoffmann | dev->bus_num = bus_num; |
1030 | 806b6024 | Gerd Hoffmann | dev->addr = addr; |
1031 | 5557d820 | Gerd Hoffmann | strcpy(dev->port, port); |
1032 | 22f84e73 | Gerd Hoffmann | dev->fd = fd; |
1033 | 806b6024 | Gerd Hoffmann | |
1034 | b9dc033c | balrog | /* read the device description */
|
1035 | b9dc033c | balrog | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
|
1036 | b9dc033c | balrog | if (dev->descr_len <= 0) { |
1037 | 64838171 | aliguori | perror("husb: reading device data failed");
|
1038 | bb36d470 | bellard | goto fail;
|
1039 | bb36d470 | bellard | } |
1040 | 3b46e624 | ths | |
1041 | b9dc033c | balrog | #ifdef DEBUG
|
1042 | 868bfe2b | bellard | { |
1043 | b9dc033c | balrog | int x;
|
1044 | b9dc033c | balrog | printf("=== begin dumping device descriptor data ===\n");
|
1045 | 2791104c | David Ahern | for (x = 0; x < dev->descr_len; x++) { |
1046 | b9dc033c | balrog | printf("%02x ", dev->descr[x]);
|
1047 | 2791104c | David Ahern | } |
1048 | b9dc033c | balrog | printf("\n=== end dumping device descriptor data ===\n");
|
1049 | bb36d470 | bellard | } |
1050 | a594cfbf | bellard | #endif
|
1051 | a594cfbf | bellard | |
1052 | b9dc033c | balrog | |
1053 | 2791104c | David Ahern | /*
|
1054 | 2791104c | David Ahern | * Initial configuration is -1 which makes us claim first
|
1055 | 446ab128 | aliguori | * available config. We used to start with 1, which does not
|
1056 | 2791104c | David Ahern | * always work. I've seen devices where first config starts
|
1057 | 446ab128 | aliguori | * with 2.
|
1058 | 446ab128 | aliguori | */
|
1059 | 2791104c | David Ahern | if (!usb_host_claim_interfaces(dev, -1)) { |
1060 | b9dc033c | balrog | goto fail;
|
1061 | 2791104c | David Ahern | } |
1062 | bb36d470 | bellard | |
1063 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); |
1064 | bb36d470 | bellard | if (ret < 0) { |
1065 | 046833ea | balrog | perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
|
1066 | bb36d470 | bellard | goto fail;
|
1067 | bb36d470 | bellard | } |
1068 | bb36d470 | bellard | |
1069 | 64838171 | aliguori | printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
|
1070 | bb36d470 | bellard | |
1071 | b9dc033c | balrog | ret = usb_linux_update_endp_table(dev); |
1072 | 2791104c | David Ahern | if (ret) {
|
1073 | bb36d470 | bellard | goto fail;
|
1074 | 2791104c | David Ahern | } |
1075 | b9dc033c | balrog | |
1076 | 2791104c | David Ahern | if (ci.slow) {
|
1077 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_LOW; |
1078 | 2791104c | David Ahern | } else {
|
1079 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_HIGH; |
1080 | 2791104c | David Ahern | } |
1081 | bb36d470 | bellard | |
1082 | 2791104c | David Ahern | if (!prod_name || prod_name[0] == '\0') { |
1083 | 0fe6d12e | Markus Armbruster | snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
|
1084 | 4b096fc9 | aliguori | "host:%d.%d", bus_num, addr);
|
1085 | 2791104c | David Ahern | } else {
|
1086 | 0fe6d12e | Markus Armbruster | pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
|
1087 | 4b096fc9 | aliguori | prod_name); |
1088 | 2791104c | David Ahern | } |
1089 | 1f6e24e7 | bellard | |
1090 | 64838171 | aliguori | /* USB devio uses 'write' flag to check for async completions */
|
1091 | 64838171 | aliguori | qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
|
1092 | 1f3870ab | aliguori | |
1093 | 26a9e82a | Gerd Hoffmann | usb_device_attach(&dev->dev); |
1094 | 26a9e82a | Gerd Hoffmann | return 0; |
1095 | 4b096fc9 | aliguori | |
1096 | b9dc033c | balrog | fail:
|
1097 | 26a9e82a | Gerd Hoffmann | dev->fd = -1;
|
1098 | 2791104c | David Ahern | if (fd != -1) { |
1099 | 806b6024 | Gerd Hoffmann | close(fd); |
1100 | 2791104c | David Ahern | } |
1101 | 26a9e82a | Gerd Hoffmann | return -1; |
1102 | 26a9e82a | Gerd Hoffmann | } |
1103 | 26a9e82a | Gerd Hoffmann | |
1104 | 26a9e82a | Gerd Hoffmann | static int usb_host_close(USBHostDevice *dev) |
1105 | 26a9e82a | Gerd Hoffmann | { |
1106 | 060dc841 | Hans de Goede | int i;
|
1107 | 060dc841 | Hans de Goede | |
1108 | 2791104c | David Ahern | if (dev->fd == -1) { |
1109 | 26a9e82a | Gerd Hoffmann | return -1; |
1110 | 2791104c | David Ahern | } |
1111 | 26a9e82a | Gerd Hoffmann | |
1112 | 26a9e82a | Gerd Hoffmann | qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); |
1113 | 26a9e82a | Gerd Hoffmann | dev->closing = 1;
|
1114 | 3a4854b3 | Hans de Goede | for (i = 1; i <= MAX_ENDPOINTS; i++) { |
1115 | 060dc841 | Hans de Goede | if (is_isoc(dev, i)) {
|
1116 | 060dc841 | Hans de Goede | usb_host_stop_n_free_iso(dev, i); |
1117 | 060dc841 | Hans de Goede | } |
1118 | 060dc841 | Hans de Goede | } |
1119 | 26a9e82a | Gerd Hoffmann | async_complete(dev); |
1120 | 26a9e82a | Gerd Hoffmann | dev->closing = 0;
|
1121 | 26a9e82a | Gerd Hoffmann | usb_device_detach(&dev->dev); |
1122 | 00ff227a | Shahar Havivi | ioctl(dev->fd, USBDEVFS_RESET); |
1123 | 26a9e82a | Gerd Hoffmann | close(dev->fd); |
1124 | 26a9e82a | Gerd Hoffmann | dev->fd = -1;
|
1125 | 26a9e82a | Gerd Hoffmann | return 0; |
1126 | 26a9e82a | Gerd Hoffmann | } |
1127 | 26a9e82a | Gerd Hoffmann | |
1128 | b373a63a | Shahar Havivi | static void usb_host_exit_notifier(struct Notifier* n) |
1129 | b373a63a | Shahar Havivi | { |
1130 | b373a63a | Shahar Havivi | USBHostDevice *s = container_of(n, USBHostDevice, exit); |
1131 | b373a63a | Shahar Havivi | |
1132 | b373a63a | Shahar Havivi | if (s->fd != -1) { |
1133 | b373a63a | Shahar Havivi | ioctl(s->fd, USBDEVFS_RESET); |
1134 | b373a63a | Shahar Havivi | } |
1135 | b373a63a | Shahar Havivi | } |
1136 | b373a63a | Shahar Havivi | |
1137 | 26a9e82a | Gerd Hoffmann | static int usb_host_initfn(USBDevice *dev) |
1138 | 26a9e82a | Gerd Hoffmann | { |
1139 | 26a9e82a | Gerd Hoffmann | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
1140 | 26a9e82a | Gerd Hoffmann | |
1141 | 26a9e82a | Gerd Hoffmann | dev->auto_attach = 0;
|
1142 | 26a9e82a | Gerd Hoffmann | s->fd = -1;
|
1143 | 26a9e82a | Gerd Hoffmann | QTAILQ_INSERT_TAIL(&hostdevs, s, next); |
1144 | b373a63a | Shahar Havivi | s->exit.notify = usb_host_exit_notifier; |
1145 | b373a63a | Shahar Havivi | qemu_add_exit_notifier(&s->exit); |
1146 | 26a9e82a | Gerd Hoffmann | usb_host_auto_check(NULL);
|
1147 | 26a9e82a | Gerd Hoffmann | return 0; |
1148 | a594cfbf | bellard | } |
1149 | bb36d470 | bellard | |
1150 | 806b6024 | Gerd Hoffmann | static struct USBDeviceInfo usb_host_dev_info = { |
1151 | 06384698 | Markus Armbruster | .product_desc = "USB Host Device",
|
1152 | 556cd098 | Markus Armbruster | .qdev.name = "usb-host",
|
1153 | 806b6024 | Gerd Hoffmann | .qdev.size = sizeof(USBHostDevice),
|
1154 | 806b6024 | Gerd Hoffmann | .init = usb_host_initfn, |
1155 | 50b7963e | Hans de Goede | .handle_packet = usb_generic_handle_packet, |
1156 | 50b7963e | Hans de Goede | .handle_data = usb_host_handle_data, |
1157 | 50b7963e | Hans de Goede | .handle_control = usb_host_handle_control, |
1158 | 806b6024 | Gerd Hoffmann | .handle_reset = usb_host_handle_reset, |
1159 | 806b6024 | Gerd Hoffmann | .handle_destroy = usb_host_handle_destroy, |
1160 | 26a9e82a | Gerd Hoffmann | .usbdevice_name = "host",
|
1161 | 26a9e82a | Gerd Hoffmann | .usbdevice_init = usb_host_device_open, |
1162 | 26a9e82a | Gerd Hoffmann | .qdev.props = (Property[]) { |
1163 | 26a9e82a | Gerd Hoffmann | DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0), |
1164 | 26a9e82a | Gerd Hoffmann | DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0), |
1165 | 26a9e82a | Gerd Hoffmann | DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0), |
1166 | 26a9e82a | Gerd Hoffmann | DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0), |
1167 | 26a9e82a | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
1168 | 26a9e82a | Gerd Hoffmann | }, |
1169 | 806b6024 | Gerd Hoffmann | }; |
1170 | 806b6024 | Gerd Hoffmann | |
1171 | 806b6024 | Gerd Hoffmann | static void usb_host_register_devices(void) |
1172 | 806b6024 | Gerd Hoffmann | { |
1173 | 806b6024 | Gerd Hoffmann | usb_qdev_register(&usb_host_dev_info); |
1174 | 806b6024 | Gerd Hoffmann | } |
1175 | 806b6024 | Gerd Hoffmann | device_init(usb_host_register_devices) |
1176 | 806b6024 | Gerd Hoffmann | |
1177 | 4b096fc9 | aliguori | USBDevice *usb_host_device_open(const char *devname) |
1178 | 4b096fc9 | aliguori | { |
1179 | 0745eb1e | Markus Armbruster | struct USBAutoFilter filter;
|
1180 | 26a9e82a | Gerd Hoffmann | USBDevice *dev; |
1181 | 26a9e82a | Gerd Hoffmann | char *p;
|
1182 | 26a9e82a | Gerd Hoffmann | |
1183 | 556cd098 | Markus Armbruster | dev = usb_create(NULL /* FIXME */, "usb-host"); |
1184 | 4b096fc9 | aliguori | |
1185 | 5d0c5750 | aliguori | if (strstr(devname, "auto:")) { |
1186 | 2791104c | David Ahern | if (parse_filter(devname, &filter) < 0) { |
1187 | 26a9e82a | Gerd Hoffmann | goto fail;
|
1188 | 2791104c | David Ahern | } |
1189 | 26a9e82a | Gerd Hoffmann | } else {
|
1190 | 26a9e82a | Gerd Hoffmann | if ((p = strchr(devname, '.'))) { |
1191 | 0745eb1e | Markus Armbruster | filter.bus_num = strtoul(devname, NULL, 0); |
1192 | 0745eb1e | Markus Armbruster | filter.addr = strtoul(p + 1, NULL, 0); |
1193 | 0745eb1e | Markus Armbruster | filter.vendor_id = 0;
|
1194 | 0745eb1e | Markus Armbruster | filter.product_id = 0;
|
1195 | 26a9e82a | Gerd Hoffmann | } else if ((p = strchr(devname, ':'))) { |
1196 | 0745eb1e | Markus Armbruster | filter.bus_num = 0;
|
1197 | 0745eb1e | Markus Armbruster | filter.addr = 0;
|
1198 | 26a9e82a | Gerd Hoffmann | filter.vendor_id = strtoul(devname, NULL, 16); |
1199 | 0745eb1e | Markus Armbruster | filter.product_id = strtoul(p + 1, NULL, 16); |
1200 | 26a9e82a | Gerd Hoffmann | } else {
|
1201 | 26a9e82a | Gerd Hoffmann | goto fail;
|
1202 | 26a9e82a | Gerd Hoffmann | } |
1203 | 5d0c5750 | aliguori | } |
1204 | 4b096fc9 | aliguori | |
1205 | 0745eb1e | Markus Armbruster | qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
|
1206 | 0745eb1e | Markus Armbruster | qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
|
1207 | 26a9e82a | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
|
1208 | 26a9e82a | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
|
1209 | beb6f0de | Kevin Wolf | qdev_init_nofail(&dev->qdev); |
1210 | 26a9e82a | Gerd Hoffmann | return dev;
|
1211 | 5d0c5750 | aliguori | |
1212 | 26a9e82a | Gerd Hoffmann | fail:
|
1213 | 26a9e82a | Gerd Hoffmann | qdev_free(&dev->qdev); |
1214 | 26a9e82a | Gerd Hoffmann | return NULL; |
1215 | 4b096fc9 | aliguori | } |
1216 | 5d0c5750 | aliguori | |
1217 | 5d0c5750 | aliguori | int usb_host_device_close(const char *devname) |
1218 | 5d0c5750 | aliguori | { |
1219 | 26a9e82a | Gerd Hoffmann | #if 0
|
1220 | 5d0c5750 | aliguori | char product_name[PRODUCT_NAME_SZ];
|
1221 | 5d0c5750 | aliguori | int bus_num, addr;
|
1222 | 5d0c5750 | aliguori | USBHostDevice *s;
|
1223 | 5d0c5750 | aliguori | |
1224 | 2791104c | David Ahern | if (strstr(devname, "auto:")) {
|
1225 | 5d0c5750 | aliguori | return usb_host_auto_del(devname);
|
1226 | 2791104c | David Ahern | }
|
1227 | 2791104c | David Ahern | if (usb_host_find_device(&bus_num, &addr, product_name,
|
1228 | 2791104c | David Ahern | sizeof(product_name), devname) < 0) {
|
1229 | 5d0c5750 | aliguori | return -1;
|
1230 | 2791104c | David Ahern | }
|
1231 | 5d0c5750 | aliguori | s = hostdev_find(bus_num, addr);
|
1232 | 5d0c5750 | aliguori | if (s) {
|
1233 | a5d2f727 | Gerd Hoffmann | usb_device_delete_addr(s->bus_num, s->dev.addr);
|
1234 | 5d0c5750 | aliguori | return 0;
|
1235 | 5d0c5750 | aliguori | }
|
1236 | 26a9e82a | Gerd Hoffmann | #endif
|
1237 | 5d0c5750 | aliguori | |
1238 | 5d0c5750 | aliguori | return -1; |
1239 | 5d0c5750 | aliguori | } |
1240 | a5d2f727 | Gerd Hoffmann | |
1241 | a594cfbf | bellard | static int get_tag_value(char *buf, int buf_size, |
1242 | 5fafdf24 | ths | const char *str, const char *tag, |
1243 | a594cfbf | bellard | const char *stopchars) |
1244 | a594cfbf | bellard | { |
1245 | a594cfbf | bellard | const char *p; |
1246 | a594cfbf | bellard | char *q;
|
1247 | a594cfbf | bellard | p = strstr(str, tag); |
1248 | 2791104c | David Ahern | if (!p) {
|
1249 | a594cfbf | bellard | return -1; |
1250 | 2791104c | David Ahern | } |
1251 | a594cfbf | bellard | p += strlen(tag); |
1252 | 2791104c | David Ahern | while (qemu_isspace(*p)) {
|
1253 | a594cfbf | bellard | p++; |
1254 | 2791104c | David Ahern | } |
1255 | a594cfbf | bellard | q = buf; |
1256 | a594cfbf | bellard | while (*p != '\0' && !strchr(stopchars, *p)) { |
1257 | 2791104c | David Ahern | if ((q - buf) < (buf_size - 1)) { |
1258 | a594cfbf | bellard | *q++ = *p; |
1259 | 2791104c | David Ahern | } |
1260 | a594cfbf | bellard | p++; |
1261 | a594cfbf | bellard | } |
1262 | a594cfbf | bellard | *q = '\0';
|
1263 | a594cfbf | bellard | return q - buf;
|
1264 | bb36d470 | bellard | } |
1265 | bb36d470 | bellard | |
1266 | 0f431527 | aliguori | /*
|
1267 | 0f431527 | aliguori | * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
|
1268 | 0f431527 | aliguori | * host's USB devices. This is legacy support since many distributions
|
1269 | 0f431527 | aliguori | * are moving to /sys/bus/usb
|
1270 | 0f431527 | aliguori | */
|
1271 | 0f431527 | aliguori | static int usb_host_scan_dev(void *opaque, USBScanFunc *func) |
1272 | bb36d470 | bellard | { |
1273 | 660f11be | Blue Swirl | FILE *f = NULL;
|
1274 | a594cfbf | bellard | char line[1024]; |
1275 | bb36d470 | bellard | char buf[1024]; |
1276 | a594cfbf | bellard | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
|
1277 | a594cfbf | bellard | char product_name[512]; |
1278 | 0f431527 | aliguori | int ret = 0; |
1279 | 3b46e624 | ths | |
1280 | 0f431527 | aliguori | if (!usb_host_device_path) {
|
1281 | 0f431527 | aliguori | perror("husb: USB Host Device Path not set");
|
1282 | 0f431527 | aliguori | goto the_end;
|
1283 | 0f431527 | aliguori | } |
1284 | 0f431527 | aliguori | snprintf(line, sizeof(line), "%s/devices", usb_host_device_path); |
1285 | 0f431527 | aliguori | f = fopen(line, "r");
|
1286 | a594cfbf | bellard | if (!f) {
|
1287 | 0f431527 | aliguori | perror("husb: cannot open devices file");
|
1288 | 0f431527 | aliguori | goto the_end;
|
1289 | a594cfbf | bellard | } |
1290 | 0f431527 | aliguori | |
1291 | a594cfbf | bellard | device_count = 0;
|
1292 | a594cfbf | bellard | bus_num = addr = speed = class_id = product_id = vendor_id = 0;
|
1293 | bb36d470 | bellard | for(;;) {
|
1294 | 2791104c | David Ahern | if (fgets(line, sizeof(line), f) == NULL) { |
1295 | bb36d470 | bellard | break;
|
1296 | 2791104c | David Ahern | } |
1297 | 2791104c | David Ahern | if (strlen(line) > 0) { |
1298 | a594cfbf | bellard | line[strlen(line) - 1] = '\0'; |
1299 | 2791104c | David Ahern | } |
1300 | a594cfbf | bellard | if (line[0] == 'T' && line[1] == ':') { |
1301 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
1302 | 38ca0f6d | pbrook | /* New device. Add the previously discovered device. */
|
1303 | 0f5160d1 | Hans de Goede | ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
|
1304 | a594cfbf | bellard | product_id, product_name, speed); |
1305 | 2791104c | David Ahern | if (ret) {
|
1306 | a594cfbf | bellard | goto the_end;
|
1307 | 2791104c | David Ahern | } |
1308 | a594cfbf | bellard | } |
1309 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) { |
1310 | a594cfbf | bellard | goto fail;
|
1311 | 2791104c | David Ahern | } |
1312 | a594cfbf | bellard | bus_num = atoi(buf); |
1313 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) { |
1314 | a594cfbf | bellard | goto fail;
|
1315 | 2791104c | David Ahern | } |
1316 | a594cfbf | bellard | addr = atoi(buf); |
1317 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) { |
1318 | a594cfbf | bellard | goto fail;
|
1319 | 2791104c | David Ahern | } |
1320 | 2791104c | David Ahern | if (!strcmp(buf, "480")) { |
1321 | a594cfbf | bellard | speed = USB_SPEED_HIGH; |
1322 | 2791104c | David Ahern | } else if (!strcmp(buf, "1.5")) { |
1323 | a594cfbf | bellard | speed = USB_SPEED_LOW; |
1324 | 2791104c | David Ahern | } else {
|
1325 | a594cfbf | bellard | speed = USB_SPEED_FULL; |
1326 | 2791104c | David Ahern | } |
1327 | a594cfbf | bellard | product_name[0] = '\0'; |
1328 | a594cfbf | bellard | class_id = 0xff;
|
1329 | a594cfbf | bellard | device_count++; |
1330 | a594cfbf | bellard | product_id = 0;
|
1331 | a594cfbf | bellard | vendor_id = 0;
|
1332 | a594cfbf | bellard | } else if (line[0] == 'P' && line[1] == ':') { |
1333 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) { |
1334 | a594cfbf | bellard | goto fail;
|
1335 | 2791104c | David Ahern | } |
1336 | a594cfbf | bellard | vendor_id = strtoul(buf, NULL, 16); |
1337 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) { |
1338 | a594cfbf | bellard | goto fail;
|
1339 | 2791104c | David Ahern | } |
1340 | a594cfbf | bellard | product_id = strtoul(buf, NULL, 16); |
1341 | a594cfbf | bellard | } else if (line[0] == 'S' && line[1] == ':') { |
1342 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) { |
1343 | a594cfbf | bellard | goto fail;
|
1344 | 2791104c | David Ahern | } |
1345 | a594cfbf | bellard | pstrcpy(product_name, sizeof(product_name), buf);
|
1346 | a594cfbf | bellard | } else if (line[0] == 'D' && line[1] == ':') { |
1347 | 2791104c | David Ahern | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) { |
1348 | a594cfbf | bellard | goto fail;
|
1349 | 2791104c | David Ahern | } |
1350 | a594cfbf | bellard | class_id = strtoul(buf, NULL, 16); |
1351 | bb36d470 | bellard | } |
1352 | a594cfbf | bellard | fail: ;
|
1353 | a594cfbf | bellard | } |
1354 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
1355 | 38ca0f6d | pbrook | /* Add the last device. */
|
1356 | 0f5160d1 | Hans de Goede | ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
|
1357 | a594cfbf | bellard | product_id, product_name, speed); |
1358 | bb36d470 | bellard | } |
1359 | a594cfbf | bellard | the_end:
|
1360 | 2791104c | David Ahern | if (f) {
|
1361 | 0f431527 | aliguori | fclose(f); |
1362 | 2791104c | David Ahern | } |
1363 | 0f431527 | aliguori | return ret;
|
1364 | 0f431527 | aliguori | } |
1365 | 0f431527 | aliguori | |
1366 | 0f431527 | aliguori | /*
|
1367 | 0f431527 | aliguori | * Read sys file-system device file
|
1368 | 0f431527 | aliguori | *
|
1369 | 0f431527 | aliguori | * @line address of buffer to put file contents in
|
1370 | 0f431527 | aliguori | * @line_size size of line
|
1371 | 0f431527 | aliguori | * @device_file path to device file (printf format string)
|
1372 | 0f431527 | aliguori | * @device_name device being opened (inserted into device_file)
|
1373 | 0f431527 | aliguori | *
|
1374 | 0f431527 | aliguori | * @return 0 failed, 1 succeeded ('line' contains data)
|
1375 | 0f431527 | aliguori | */
|
1376 | 2791104c | David Ahern | static int usb_host_read_file(char *line, size_t line_size, |
1377 | 2791104c | David Ahern | const char *device_file, const char *device_name) |
1378 | 0f431527 | aliguori | { |
1379 | 0f431527 | aliguori | FILE *f; |
1380 | 0f431527 | aliguori | int ret = 0; |
1381 | 0f431527 | aliguori | char filename[PATH_MAX];
|
1382 | 0f431527 | aliguori | |
1383 | b4e237aa | blueswir1 | snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
|
1384 | b4e237aa | blueswir1 | device_file); |
1385 | 0f431527 | aliguori | f = fopen(filename, "r");
|
1386 | 0f431527 | aliguori | if (f) {
|
1387 | 9f99cee7 | Kirill A. Shutemov | ret = fgets(line, line_size, f) != NULL;
|
1388 | 0f431527 | aliguori | fclose(f); |
1389 | 0f431527 | aliguori | } |
1390 | 0f431527 | aliguori | |
1391 | 0f431527 | aliguori | return ret;
|
1392 | 0f431527 | aliguori | } |
1393 | 0f431527 | aliguori | |
1394 | 0f431527 | aliguori | /*
|
1395 | 0f431527 | aliguori | * Use /sys/bus/usb/devices/ directory to determine host's USB
|
1396 | 0f431527 | aliguori | * devices.
|
1397 | 0f431527 | aliguori | *
|
1398 | 0f431527 | aliguori | * This code is based on Robert Schiele's original patches posted to
|
1399 | 0f431527 | aliguori | * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
|
1400 | 0f431527 | aliguori | */
|
1401 | 0f431527 | aliguori | static int usb_host_scan_sys(void *opaque, USBScanFunc *func) |
1402 | 0f431527 | aliguori | { |
1403 | 660f11be | Blue Swirl | DIR *dir = NULL;
|
1404 | 0f431527 | aliguori | char line[1024]; |
1405 | 5557d820 | Gerd Hoffmann | int bus_num, addr, speed, class_id, product_id, vendor_id;
|
1406 | 0f431527 | aliguori | int ret = 0; |
1407 | 5557d820 | Gerd Hoffmann | char port[MAX_PORTLEN];
|
1408 | 0f431527 | aliguori | char product_name[512]; |
1409 | 0f431527 | aliguori | struct dirent *de;
|
1410 | 0f431527 | aliguori | |
1411 | 0f431527 | aliguori | dir = opendir(USBSYSBUS_PATH "/devices");
|
1412 | 0f431527 | aliguori | if (!dir) {
|
1413 | 0f431527 | aliguori | perror("husb: cannot open devices directory");
|
1414 | 0f431527 | aliguori | goto the_end;
|
1415 | 0f431527 | aliguori | } |
1416 | 0f431527 | aliguori | |
1417 | 0f431527 | aliguori | while ((de = readdir(dir))) {
|
1418 | 0f431527 | aliguori | if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) { |
1419 | 5557d820 | Gerd Hoffmann | if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) { |
1420 | 5557d820 | Gerd Hoffmann | continue;
|
1421 | 0f5160d1 | Hans de Goede | } |
1422 | 0f431527 | aliguori | |
1423 | 2791104c | David Ahern | if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) { |
1424 | 0f431527 | aliguori | goto the_end;
|
1425 | 2791104c | David Ahern | } |
1426 | 2791104c | David Ahern | if (sscanf(line, "%d", &addr) != 1) { |
1427 | 0f431527 | aliguori | goto the_end;
|
1428 | 2791104c | David Ahern | } |
1429 | b4e237aa | blueswir1 | if (!usb_host_read_file(line, sizeof(line), "bDeviceClass", |
1430 | 2791104c | David Ahern | de->d_name)) { |
1431 | 0f431527 | aliguori | goto the_end;
|
1432 | 2791104c | David Ahern | } |
1433 | 2791104c | David Ahern | if (sscanf(line, "%x", &class_id) != 1) { |
1434 | 0f431527 | aliguori | goto the_end;
|
1435 | 2791104c | David Ahern | } |
1436 | 0f431527 | aliguori | |
1437 | 2791104c | David Ahern | if (!usb_host_read_file(line, sizeof(line), "idVendor", |
1438 | 2791104c | David Ahern | de->d_name)) { |
1439 | 0f431527 | aliguori | goto the_end;
|
1440 | 2791104c | David Ahern | } |
1441 | 2791104c | David Ahern | if (sscanf(line, "%x", &vendor_id) != 1) { |
1442 | 0f431527 | aliguori | goto the_end;
|
1443 | 2791104c | David Ahern | } |
1444 | b4e237aa | blueswir1 | if (!usb_host_read_file(line, sizeof(line), "idProduct", |
1445 | 2791104c | David Ahern | de->d_name)) { |
1446 | 0f431527 | aliguori | goto the_end;
|
1447 | 2791104c | David Ahern | } |
1448 | 2791104c | David Ahern | if (sscanf(line, "%x", &product_id) != 1) { |
1449 | 0f431527 | aliguori | goto the_end;
|
1450 | 2791104c | David Ahern | } |
1451 | b4e237aa | blueswir1 | if (!usb_host_read_file(line, sizeof(line), "product", |
1452 | b4e237aa | blueswir1 | de->d_name)) { |
1453 | 0f431527 | aliguori | *product_name = 0;
|
1454 | 0f431527 | aliguori | } else {
|
1455 | 2791104c | David Ahern | if (strlen(line) > 0) { |
1456 | 0f431527 | aliguori | line[strlen(line) - 1] = '\0'; |
1457 | 2791104c | David Ahern | } |
1458 | 0f431527 | aliguori | pstrcpy(product_name, sizeof(product_name), line);
|
1459 | 0f431527 | aliguori | } |
1460 | 0f431527 | aliguori | |
1461 | 2791104c | David Ahern | if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) { |
1462 | 0f431527 | aliguori | goto the_end;
|
1463 | 2791104c | David Ahern | } |
1464 | 2791104c | David Ahern | if (!strcmp(line, "480\n")) { |
1465 | 0f431527 | aliguori | speed = USB_SPEED_HIGH; |
1466 | 2791104c | David Ahern | } else if (!strcmp(line, "1.5\n")) { |
1467 | 0f431527 | aliguori | speed = USB_SPEED_LOW; |
1468 | 2791104c | David Ahern | } else {
|
1469 | 0f431527 | aliguori | speed = USB_SPEED_FULL; |
1470 | 2791104c | David Ahern | } |
1471 | 0f431527 | aliguori | |
1472 | 5557d820 | Gerd Hoffmann | ret = func(opaque, bus_num, addr, port, class_id, vendor_id, |
1473 | 0f431527 | aliguori | product_id, product_name, speed); |
1474 | 2791104c | David Ahern | if (ret) {
|
1475 | 0f431527 | aliguori | goto the_end;
|
1476 | 2791104c | David Ahern | } |
1477 | 0f431527 | aliguori | } |
1478 | 0f431527 | aliguori | } |
1479 | 0f431527 | aliguori | the_end:
|
1480 | 2791104c | David Ahern | if (dir) {
|
1481 | 0f431527 | aliguori | closedir(dir); |
1482 | 2791104c | David Ahern | } |
1483 | 0f431527 | aliguori | return ret;
|
1484 | 0f431527 | aliguori | } |
1485 | 0f431527 | aliguori | |
1486 | 0f431527 | aliguori | /*
|
1487 | 0f431527 | aliguori | * Determine how to access the host's USB devices and call the
|
1488 | 0f431527 | aliguori | * specific support function.
|
1489 | 0f431527 | aliguori | */
|
1490 | 0f431527 | aliguori | static int usb_host_scan(void *opaque, USBScanFunc *func) |
1491 | 0f431527 | aliguori | { |
1492 | 376253ec | aliguori | Monitor *mon = cur_mon; |
1493 | 660f11be | Blue Swirl | FILE *f = NULL;
|
1494 | 660f11be | Blue Swirl | DIR *dir = NULL;
|
1495 | 0f431527 | aliguori | int ret = 0; |
1496 | 0f431527 | aliguori | const char *fs_type[] = {"unknown", "proc", "dev", "sys"}; |
1497 | 0f431527 | aliguori | char devpath[PATH_MAX];
|
1498 | 0f431527 | aliguori | |
1499 | 0f431527 | aliguori | /* only check the host once */
|
1500 | 0f431527 | aliguori | if (!usb_fs_type) {
|
1501 | 55496240 | Mark McLoughlin | dir = opendir(USBSYSBUS_PATH "/devices");
|
1502 | 55496240 | Mark McLoughlin | if (dir) {
|
1503 | 55496240 | Mark McLoughlin | /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
|
1504 | 55496240 | Mark McLoughlin | strcpy(devpath, USBDEVBUS_PATH); |
1505 | 55496240 | Mark McLoughlin | usb_fs_type = USB_FS_SYS; |
1506 | 55496240 | Mark McLoughlin | closedir(dir); |
1507 | d0f2c4c6 | malc | DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH); |
1508 | 55496240 | Mark McLoughlin | goto found_devices;
|
1509 | 55496240 | Mark McLoughlin | } |
1510 | 0f431527 | aliguori | f = fopen(USBPROCBUS_PATH "/devices", "r"); |
1511 | 0f431527 | aliguori | if (f) {
|
1512 | 0f431527 | aliguori | /* devices found in /proc/bus/usb/ */
|
1513 | 0f431527 | aliguori | strcpy(devpath, USBPROCBUS_PATH); |
1514 | 0f431527 | aliguori | usb_fs_type = USB_FS_PROC; |
1515 | 0f431527 | aliguori | fclose(f); |
1516 | d0f2c4c6 | malc | DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH); |
1517 | f16a0db3 | aliguori | goto found_devices;
|
1518 | 0f431527 | aliguori | } |
1519 | 0f431527 | aliguori | /* try additional methods if an access method hasn't been found yet */
|
1520 | 0f431527 | aliguori | f = fopen(USBDEVBUS_PATH "/devices", "r"); |
1521 | f16a0db3 | aliguori | if (f) {
|
1522 | 0f431527 | aliguori | /* devices found in /dev/bus/usb/ */
|
1523 | 0f431527 | aliguori | strcpy(devpath, USBDEVBUS_PATH); |
1524 | 0f431527 | aliguori | usb_fs_type = USB_FS_DEV; |
1525 | 0f431527 | aliguori | fclose(f); |
1526 | d0f2c4c6 | malc | DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH); |
1527 | f16a0db3 | aliguori | goto found_devices;
|
1528 | 0f431527 | aliguori | } |
1529 | f16a0db3 | aliguori | found_devices:
|
1530 | 22babebb | aliguori | if (!usb_fs_type) {
|
1531 | 2791104c | David Ahern | if (mon) {
|
1532 | eba6fe87 | Gerd Hoffmann | monitor_printf(mon, "husb: unable to access USB devices\n");
|
1533 | 2791104c | David Ahern | } |
1534 | f16a0db3 | aliguori | return -ENOENT;
|
1535 | 0f431527 | aliguori | } |
1536 | 0f431527 | aliguori | |
1537 | 0f431527 | aliguori | /* the module setting (used later for opening devices) */
|
1538 | 0f431527 | aliguori | usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
|
1539 | 1eec614b | aliguori | strcpy(usb_host_device_path, devpath); |
1540 | 2791104c | David Ahern | if (mon) {
|
1541 | eba6fe87 | Gerd Hoffmann | monitor_printf(mon, "husb: using %s file-system with %s\n",
|
1542 | eba6fe87 | Gerd Hoffmann | fs_type[usb_fs_type], usb_host_device_path); |
1543 | 2791104c | David Ahern | } |
1544 | 0f431527 | aliguori | } |
1545 | 0f431527 | aliguori | |
1546 | 0f431527 | aliguori | switch (usb_fs_type) {
|
1547 | 0f431527 | aliguori | case USB_FS_PROC:
|
1548 | 0f431527 | aliguori | case USB_FS_DEV:
|
1549 | 0f431527 | aliguori | ret = usb_host_scan_dev(opaque, func); |
1550 | 0f431527 | aliguori | break;
|
1551 | 0f431527 | aliguori | case USB_FS_SYS:
|
1552 | 0f431527 | aliguori | ret = usb_host_scan_sys(opaque, func); |
1553 | 0f431527 | aliguori | break;
|
1554 | f16a0db3 | aliguori | default:
|
1555 | f16a0db3 | aliguori | ret = -EINVAL; |
1556 | f16a0db3 | aliguori | break;
|
1557 | 0f431527 | aliguori | } |
1558 | a594cfbf | bellard | return ret;
|
1559 | bb36d470 | bellard | } |
1560 | bb36d470 | bellard | |
1561 | 4b096fc9 | aliguori | static QEMUTimer *usb_auto_timer;
|
1562 | 4b096fc9 | aliguori | |
1563 | 5557d820 | Gerd Hoffmann | static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port, |
1564 | 26a9e82a | Gerd Hoffmann | int class_id, int vendor_id, int product_id, |
1565 | 26a9e82a | Gerd Hoffmann | const char *product_name, int speed) |
1566 | 4b096fc9 | aliguori | { |
1567 | 4b096fc9 | aliguori | struct USBAutoFilter *f;
|
1568 | 26a9e82a | Gerd Hoffmann | struct USBHostDevice *s;
|
1569 | 4b096fc9 | aliguori | |
1570 | 4b096fc9 | aliguori | /* Ignore hubs */
|
1571 | 4b096fc9 | aliguori | if (class_id == 9) |
1572 | 4b096fc9 | aliguori | return 0; |
1573 | 4b096fc9 | aliguori | |
1574 | 26a9e82a | Gerd Hoffmann | QTAILQ_FOREACH(s, &hostdevs, next) { |
1575 | 26a9e82a | Gerd Hoffmann | f = &s->match; |
1576 | 26a9e82a | Gerd Hoffmann | |
1577 | 2791104c | David Ahern | if (f->bus_num > 0 && f->bus_num != bus_num) { |
1578 | 4b096fc9 | aliguori | continue;
|
1579 | 2791104c | David Ahern | } |
1580 | 2791104c | David Ahern | if (f->addr > 0 && f->addr != addr) { |
1581 | 4b096fc9 | aliguori | continue;
|
1582 | 2791104c | David Ahern | } |
1583 | 4b096fc9 | aliguori | |
1584 | 2791104c | David Ahern | if (f->vendor_id > 0 && f->vendor_id != vendor_id) { |
1585 | 4b096fc9 | aliguori | continue;
|
1586 | 2791104c | David Ahern | } |
1587 | 4b096fc9 | aliguori | |
1588 | 2791104c | David Ahern | if (f->product_id > 0 && f->product_id != product_id) { |
1589 | 4b096fc9 | aliguori | continue;
|
1590 | 2791104c | David Ahern | } |
1591 | 4b096fc9 | aliguori | /* We got a match */
|
1592 | 4b096fc9 | aliguori | |
1593 | 33e66b86 | Markus Armbruster | /* Already attached ? */
|
1594 | 2791104c | David Ahern | if (s->fd != -1) { |
1595 | 4b096fc9 | aliguori | return 0; |
1596 | 2791104c | David Ahern | } |
1597 | d0f2c4c6 | malc | DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
|
1598 | 4b096fc9 | aliguori | |
1599 | 5557d820 | Gerd Hoffmann | usb_host_open(s, bus_num, addr, port, product_name); |
1600 | 4b096fc9 | aliguori | } |
1601 | 4b096fc9 | aliguori | |
1602 | 4b096fc9 | aliguori | return 0; |
1603 | 4b096fc9 | aliguori | } |
1604 | 4b096fc9 | aliguori | |
1605 | 26a9e82a | Gerd Hoffmann | static void usb_host_auto_check(void *unused) |
1606 | 4b096fc9 | aliguori | { |
1607 | 26a9e82a | Gerd Hoffmann | struct USBHostDevice *s;
|
1608 | 26a9e82a | Gerd Hoffmann | int unconnected = 0; |
1609 | 26a9e82a | Gerd Hoffmann | |
1610 | 4b096fc9 | aliguori | usb_host_scan(NULL, usb_host_auto_scan);
|
1611 | 26a9e82a | Gerd Hoffmann | |
1612 | 26a9e82a | Gerd Hoffmann | QTAILQ_FOREACH(s, &hostdevs, next) { |
1613 | 2791104c | David Ahern | if (s->fd == -1) { |
1614 | 26a9e82a | Gerd Hoffmann | unconnected++; |
1615 | 2791104c | David Ahern | } |
1616 | 26a9e82a | Gerd Hoffmann | } |
1617 | 26a9e82a | Gerd Hoffmann | |
1618 | 26a9e82a | Gerd Hoffmann | if (unconnected == 0) { |
1619 | 26a9e82a | Gerd Hoffmann | /* nothing to watch */
|
1620 | 2791104c | David Ahern | if (usb_auto_timer) {
|
1621 | 26a9e82a | Gerd Hoffmann | qemu_del_timer(usb_auto_timer); |
1622 | 2791104c | David Ahern | } |
1623 | 26a9e82a | Gerd Hoffmann | return;
|
1624 | 26a9e82a | Gerd Hoffmann | } |
1625 | 26a9e82a | Gerd Hoffmann | |
1626 | 26a9e82a | Gerd Hoffmann | if (!usb_auto_timer) {
|
1627 | 7bd427d8 | Paolo Bonzini | usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
|
1628 | 2791104c | David Ahern | if (!usb_auto_timer) {
|
1629 | 26a9e82a | Gerd Hoffmann | return;
|
1630 | 2791104c | David Ahern | } |
1631 | 26a9e82a | Gerd Hoffmann | } |
1632 | 7bd427d8 | Paolo Bonzini | qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
|
1633 | 4b096fc9 | aliguori | } |
1634 | 4b096fc9 | aliguori | |
1635 | 4b096fc9 | aliguori | /*
|
1636 | 5d0c5750 | aliguori | * Autoconnect filter
|
1637 | 5d0c5750 | aliguori | * Format:
|
1638 | 5d0c5750 | aliguori | * auto:bus:dev[:vid:pid]
|
1639 | 5d0c5750 | aliguori | * auto:bus.dev[:vid:pid]
|
1640 | 5d0c5750 | aliguori | *
|
1641 | 5d0c5750 | aliguori | * bus - bus number (dec, * means any)
|
1642 | 5d0c5750 | aliguori | * dev - device number (dec, * means any)
|
1643 | 5d0c5750 | aliguori | * vid - vendor id (hex, * means any)
|
1644 | 5d0c5750 | aliguori | * pid - product id (hex, * means any)
|
1645 | 5d0c5750 | aliguori | *
|
1646 | 5d0c5750 | aliguori | * See 'lsusb' output.
|
1647 | 4b096fc9 | aliguori | */
|
1648 | 5d0c5750 | aliguori | static int parse_filter(const char *spec, struct USBAutoFilter *f) |
1649 | 4b096fc9 | aliguori | { |
1650 | 5d0c5750 | aliguori | enum { BUS, DEV, VID, PID, DONE };
|
1651 | 5d0c5750 | aliguori | const char *p = spec; |
1652 | 5d0c5750 | aliguori | int i;
|
1653 | 5d0c5750 | aliguori | |
1654 | 0745eb1e | Markus Armbruster | f->bus_num = 0;
|
1655 | 0745eb1e | Markus Armbruster | f->addr = 0;
|
1656 | 0745eb1e | Markus Armbruster | f->vendor_id = 0;
|
1657 | 0745eb1e | Markus Armbruster | f->product_id = 0;
|
1658 | 5d0c5750 | aliguori | |
1659 | 5d0c5750 | aliguori | for (i = BUS; i < DONE; i++) {
|
1660 | 2791104c | David Ahern | p = strpbrk(p, ":.");
|
1661 | 2791104c | David Ahern | if (!p) {
|
1662 | 2791104c | David Ahern | break;
|
1663 | 2791104c | David Ahern | } |
1664 | 5d0c5750 | aliguori | p++; |
1665 | 5d0c5750 | aliguori | |
1666 | 2791104c | David Ahern | if (*p == '*') { |
1667 | 2791104c | David Ahern | continue;
|
1668 | 2791104c | David Ahern | } |
1669 | 5d0c5750 | aliguori | switch(i) {
|
1670 | 5d0c5750 | aliguori | case BUS: f->bus_num = strtol(p, NULL, 10); break; |
1671 | 5d0c5750 | aliguori | case DEV: f->addr = strtol(p, NULL, 10); break; |
1672 | 5d0c5750 | aliguori | case VID: f->vendor_id = strtol(p, NULL, 16); break; |
1673 | 5d0c5750 | aliguori | case PID: f->product_id = strtol(p, NULL, 16); break; |
1674 | 5d0c5750 | aliguori | } |
1675 | 5d0c5750 | aliguori | } |
1676 | 5d0c5750 | aliguori | |
1677 | 5d0c5750 | aliguori | if (i < DEV) {
|
1678 | 5d0c5750 | aliguori | fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
|
1679 | 5d0c5750 | aliguori | return -1; |
1680 | 5d0c5750 | aliguori | } |
1681 | 5d0c5750 | aliguori | |
1682 | 5d0c5750 | aliguori | return 0; |
1683 | 5d0c5750 | aliguori | } |
1684 | 5d0c5750 | aliguori | |
1685 | a594cfbf | bellard | /**********************/
|
1686 | a594cfbf | bellard | /* USB host device info */
|
1687 | a594cfbf | bellard | |
1688 | a594cfbf | bellard | struct usb_class_info {
|
1689 | a594cfbf | bellard | int class;
|
1690 | a594cfbf | bellard | const char *class_name; |
1691 | a594cfbf | bellard | }; |
1692 | a594cfbf | bellard | |
1693 | a594cfbf | bellard | static const struct usb_class_info usb_class_info[] = { |
1694 | a594cfbf | bellard | { USB_CLASS_AUDIO, "Audio"},
|
1695 | a594cfbf | bellard | { USB_CLASS_COMM, "Communication"},
|
1696 | a594cfbf | bellard | { USB_CLASS_HID, "HID"},
|
1697 | a594cfbf | bellard | { USB_CLASS_HUB, "Hub" },
|
1698 | a594cfbf | bellard | { USB_CLASS_PHYSICAL, "Physical" },
|
1699 | a594cfbf | bellard | { USB_CLASS_PRINTER, "Printer" },
|
1700 | a594cfbf | bellard | { USB_CLASS_MASS_STORAGE, "Storage" },
|
1701 | a594cfbf | bellard | { USB_CLASS_CDC_DATA, "Data" },
|
1702 | a594cfbf | bellard | { USB_CLASS_APP_SPEC, "Application Specific" },
|
1703 | a594cfbf | bellard | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
1704 | a594cfbf | bellard | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
1705 | b9dc033c | balrog | { USB_CLASS_CSCID, "Smart Card" },
|
1706 | a594cfbf | bellard | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
1707 | a594cfbf | bellard | { -1, NULL } |
1708 | a594cfbf | bellard | }; |
1709 | a594cfbf | bellard | |
1710 | a594cfbf | bellard | static const char *usb_class_str(uint8_t class) |
1711 | bb36d470 | bellard | { |
1712 | a594cfbf | bellard | const struct usb_class_info *p; |
1713 | a594cfbf | bellard | for(p = usb_class_info; p->class != -1; p++) { |
1714 | 2791104c | David Ahern | if (p->class == class) {
|
1715 | a594cfbf | bellard | break;
|
1716 | 2791104c | David Ahern | } |
1717 | bb36d470 | bellard | } |
1718 | a594cfbf | bellard | return p->class_name;
|
1719 | a594cfbf | bellard | } |
1720 | a594cfbf | bellard | |
1721 | 5557d820 | Gerd Hoffmann | static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port, |
1722 | 5557d820 | Gerd Hoffmann | int class_id, int vendor_id, int product_id, |
1723 | 9596ebb7 | pbrook | const char *product_name, |
1724 | 9596ebb7 | pbrook | int speed)
|
1725 | a594cfbf | bellard | { |
1726 | a594cfbf | bellard | const char *class_str, *speed_str; |
1727 | a594cfbf | bellard | |
1728 | a594cfbf | bellard | switch(speed) {
|
1729 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
1730 | 5fafdf24 | ths | speed_str = "1.5";
|
1731 | a594cfbf | bellard | break;
|
1732 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
1733 | 5fafdf24 | ths | speed_str = "12";
|
1734 | a594cfbf | bellard | break;
|
1735 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
1736 | 5fafdf24 | ths | speed_str = "480";
|
1737 | a594cfbf | bellard | break;
|
1738 | a594cfbf | bellard | default:
|
1739 | 5fafdf24 | ths | speed_str = "?";
|
1740 | a594cfbf | bellard | break;
|
1741 | a594cfbf | bellard | } |
1742 | a594cfbf | bellard | |
1743 | 5557d820 | Gerd Hoffmann | monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
|
1744 | 5557d820 | Gerd Hoffmann | bus_num, addr, port, speed_str); |
1745 | a594cfbf | bellard | class_str = usb_class_str(class_id); |
1746 | 2791104c | David Ahern | if (class_str) {
|
1747 | 376253ec | aliguori | monitor_printf(mon, " %s:", class_str);
|
1748 | 2791104c | David Ahern | } else {
|
1749 | 376253ec | aliguori | monitor_printf(mon, " Class %02x:", class_id);
|
1750 | 2791104c | David Ahern | } |
1751 | 376253ec | aliguori | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
|
1752 | 2791104c | David Ahern | if (product_name[0] != '\0') { |
1753 | 376253ec | aliguori | monitor_printf(mon, ", %s", product_name);
|
1754 | 2791104c | David Ahern | } |
1755 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
1756 | a594cfbf | bellard | } |
1757 | a594cfbf | bellard | |
1758 | 5fafdf24 | ths | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
1759 | 5557d820 | Gerd Hoffmann | char *path, int class_id, |
1760 | 5fafdf24 | ths | int vendor_id, int product_id, |
1761 | a594cfbf | bellard | const char *product_name, |
1762 | a594cfbf | bellard | int speed)
|
1763 | a594cfbf | bellard | { |
1764 | 179da8af | Blue Swirl | Monitor *mon = opaque; |
1765 | 179da8af | Blue Swirl | |
1766 | 5557d820 | Gerd Hoffmann | usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id, |
1767 | a594cfbf | bellard | product_name, speed); |
1768 | a594cfbf | bellard | return 0; |
1769 | a594cfbf | bellard | } |
1770 | a594cfbf | bellard | |
1771 | ac4ffb5a | aliguori | static void dec2str(int val, char *str, size_t size) |
1772 | 5d0c5750 | aliguori | { |
1773 | 2791104c | David Ahern | if (val == 0) { |
1774 | ac4ffb5a | aliguori | snprintf(str, size, "*");
|
1775 | 2791104c | David Ahern | } else {
|
1776 | 2791104c | David Ahern | snprintf(str, size, "%d", val);
|
1777 | 2791104c | David Ahern | } |
1778 | 5d0c5750 | aliguori | } |
1779 | 5d0c5750 | aliguori | |
1780 | ac4ffb5a | aliguori | static void hex2str(int val, char *str, size_t size) |
1781 | 5d0c5750 | aliguori | { |
1782 | 2791104c | David Ahern | if (val == 0) { |
1783 | ac4ffb5a | aliguori | snprintf(str, size, "*");
|
1784 | 2791104c | David Ahern | } else {
|
1785 | 26a9e82a | Gerd Hoffmann | snprintf(str, size, "%04x", val);
|
1786 | 2791104c | David Ahern | } |
1787 | 5d0c5750 | aliguori | } |
1788 | 5d0c5750 | aliguori | |
1789 | 376253ec | aliguori | void usb_host_info(Monitor *mon)
|
1790 | a594cfbf | bellard | { |
1791 | 5d0c5750 | aliguori | struct USBAutoFilter *f;
|
1792 | 26a9e82a | Gerd Hoffmann | struct USBHostDevice *s;
|
1793 | 5d0c5750 | aliguori | |
1794 | 179da8af | Blue Swirl | usb_host_scan(mon, usb_host_info_device); |
1795 | 5d0c5750 | aliguori | |
1796 | 2791104c | David Ahern | if (QTAILQ_EMPTY(&hostdevs)) {
|
1797 | 26a9e82a | Gerd Hoffmann | return;
|
1798 | 2791104c | David Ahern | } |
1799 | 2791104c | David Ahern | |
1800 | 26a9e82a | Gerd Hoffmann | monitor_printf(mon, " Auto filters:\n");
|
1801 | 26a9e82a | Gerd Hoffmann | QTAILQ_FOREACH(s, &hostdevs, next) { |
1802 | 5d0c5750 | aliguori | char bus[10], addr[10], vid[10], pid[10]; |
1803 | 26a9e82a | Gerd Hoffmann | f = &s->match; |
1804 | ac4ffb5a | aliguori | dec2str(f->bus_num, bus, sizeof(bus));
|
1805 | ac4ffb5a | aliguori | dec2str(f->addr, addr, sizeof(addr));
|
1806 | ac4ffb5a | aliguori | hex2str(f->vendor_id, vid, sizeof(vid));
|
1807 | ac4ffb5a | aliguori | hex2str(f->product_id, pid, sizeof(pid));
|
1808 | 376253ec | aliguori | monitor_printf(mon, " Device %s.%s ID %s:%s\n",
|
1809 | 376253ec | aliguori | bus, addr, vid, pid); |
1810 | 5d0c5750 | aliguori | } |
1811 | bb36d470 | bellard | } |