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