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