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