root / usb-linux.c @ f36672ae
History | View | Annotate | Download (28.4 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 | 64838171 | aliguori | * Magor rewrite to support fully async operation
|
9 | 4b096fc9 | aliguori | *
|
10 | bb36d470 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | bb36d470 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
12 | bb36d470 | bellard | * in the Software without restriction, including without limitation the rights
|
13 | bb36d470 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | bb36d470 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
15 | bb36d470 | bellard | * furnished to do so, subject to the following conditions:
|
16 | bb36d470 | bellard | *
|
17 | bb36d470 | bellard | * The above copyright notice and this permission notice shall be included in
|
18 | bb36d470 | bellard | * all copies or substantial portions of the Software.
|
19 | bb36d470 | bellard | *
|
20 | bb36d470 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | bb36d470 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | bb36d470 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | bb36d470 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | bb36d470 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | bb36d470 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | bb36d470 | bellard | * THE SOFTWARE.
|
27 | bb36d470 | bellard | */
|
28 | 87ecb68b | pbrook | #include "qemu-common.h" |
29 | 1f3870ab | aliguori | #include "qemu-timer.h" |
30 | 87ecb68b | pbrook | #include "hw/usb.h" |
31 | 87ecb68b | pbrook | #include "console.h" |
32 | bb36d470 | bellard | |
33 | bb36d470 | bellard | #if defined(__linux__)
|
34 | bb36d470 | bellard | #include <dirent.h> |
35 | bb36d470 | bellard | #include <sys/ioctl.h> |
36 | bb36d470 | bellard | #include <linux/usbdevice_fs.h> |
37 | bb36d470 | bellard | #include <linux/version.h> |
38 | b9dc033c | balrog | #include <signal.h> |
39 | bb36d470 | bellard | |
40 | bb36d470 | bellard | /* We redefine it to avoid version problems */
|
41 | bb36d470 | bellard | struct usb_ctrltransfer {
|
42 | bb36d470 | bellard | uint8_t bRequestType; |
43 | bb36d470 | bellard | uint8_t bRequest; |
44 | bb36d470 | bellard | uint16_t wValue; |
45 | bb36d470 | bellard | uint16_t wIndex; |
46 | bb36d470 | bellard | uint16_t wLength; |
47 | bb36d470 | bellard | uint32_t timeout; |
48 | bb36d470 | bellard | void *data;
|
49 | bb36d470 | bellard | }; |
50 | bb36d470 | bellard | |
51 | a594cfbf | bellard | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
52 | 5fafdf24 | ths | int vendor_id, int product_id, |
53 | a594cfbf | bellard | const char *product_name, int speed); |
54 | 5fafdf24 | ths | static int usb_host_find_device(int *pbus_num, int *paddr, |
55 | 1f6e24e7 | bellard | char *product_name, int product_name_size, |
56 | a594cfbf | bellard | const char *devname); |
57 | bb36d470 | bellard | |
58 | a594cfbf | bellard | //#define DEBUG
|
59 | 64838171 | aliguori | |
60 | 64838171 | aliguori | #ifdef DEBUG
|
61 | 64838171 | aliguori | #define dprintf printf
|
62 | 64838171 | aliguori | #else
|
63 | 64838171 | aliguori | #define dprintf(...)
|
64 | 64838171 | aliguori | #endif
|
65 | bb36d470 | bellard | |
66 | bb36d470 | bellard | #define USBDEVFS_PATH "/proc/bus/usb" |
67 | 1f6e24e7 | bellard | #define PRODUCT_NAME_SZ 32 |
68 | b9dc033c | balrog | #define MAX_ENDPOINTS 16 |
69 | bb36d470 | bellard | |
70 | b9dc033c | balrog | struct sigaction sigact;
|
71 | b9dc033c | balrog | |
72 | b9dc033c | balrog | /* endpoint association data */
|
73 | b9dc033c | balrog | struct endp_data {
|
74 | b9dc033c | balrog | uint8_t type; |
75 | 64838171 | aliguori | uint8_t halted; |
76 | b9dc033c | balrog | }; |
77 | b9dc033c | balrog | |
78 | bb36d470 | bellard | typedef struct USBHostDevice { |
79 | bb36d470 | bellard | USBDevice dev; |
80 | 64838171 | aliguori | int fd;
|
81 | 64838171 | aliguori | |
82 | 64838171 | aliguori | uint8_t descr[1024];
|
83 | 64838171 | aliguori | int descr_len;
|
84 | 64838171 | aliguori | int configuration;
|
85 | 24772c1e | aliguori | int closing;
|
86 | 64838171 | aliguori | |
87 | b9dc033c | balrog | struct endp_data endp_table[MAX_ENDPOINTS];
|
88 | 4b096fc9 | aliguori | |
89 | 4b096fc9 | aliguori | /* Host side address */
|
90 | 4b096fc9 | aliguori | int bus_num;
|
91 | 4b096fc9 | aliguori | int addr;
|
92 | 4b096fc9 | aliguori | |
93 | 4b096fc9 | aliguori | struct USBHostDevice *next;
|
94 | bb36d470 | bellard | } USBHostDevice; |
95 | bb36d470 | bellard | |
96 | 64838171 | aliguori | static int is_isoc(USBHostDevice *s, int ep) |
97 | 64838171 | aliguori | { |
98 | 64838171 | aliguori | return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO; |
99 | 64838171 | aliguori | } |
100 | 64838171 | aliguori | |
101 | 64838171 | aliguori | static int is_halted(USBHostDevice *s, int ep) |
102 | 64838171 | aliguori | { |
103 | 64838171 | aliguori | return s->endp_table[ep - 1].halted; |
104 | 64838171 | aliguori | } |
105 | 64838171 | aliguori | |
106 | 64838171 | aliguori | static void clear_halt(USBHostDevice *s, int ep) |
107 | 64838171 | aliguori | { |
108 | 64838171 | aliguori | s->endp_table[ep - 1].halted = 0; |
109 | 64838171 | aliguori | } |
110 | 64838171 | aliguori | |
111 | 64838171 | aliguori | static void set_halt(USBHostDevice *s, int ep) |
112 | 64838171 | aliguori | { |
113 | 64838171 | aliguori | s->endp_table[ep - 1].halted = 1; |
114 | 64838171 | aliguori | } |
115 | 64838171 | aliguori | |
116 | 4b096fc9 | aliguori | static USBHostDevice *hostdev_list;
|
117 | 4b096fc9 | aliguori | |
118 | 4b096fc9 | aliguori | static void hostdev_link(USBHostDevice *dev) |
119 | 4b096fc9 | aliguori | { |
120 | 4b096fc9 | aliguori | dev->next = hostdev_list; |
121 | 4b096fc9 | aliguori | hostdev_list = dev; |
122 | 4b096fc9 | aliguori | } |
123 | 4b096fc9 | aliguori | |
124 | 4b096fc9 | aliguori | static void hostdev_unlink(USBHostDevice *dev) |
125 | 4b096fc9 | aliguori | { |
126 | 4b096fc9 | aliguori | USBHostDevice *pdev = hostdev_list; |
127 | 4b096fc9 | aliguori | USBHostDevice **prev = &hostdev_list; |
128 | 4b096fc9 | aliguori | |
129 | 4b096fc9 | aliguori | while (pdev) {
|
130 | 4b096fc9 | aliguori | if (pdev == dev) {
|
131 | 4b096fc9 | aliguori | *prev = dev->next; |
132 | 4b096fc9 | aliguori | return;
|
133 | 4b096fc9 | aliguori | } |
134 | 4b096fc9 | aliguori | |
135 | 4b096fc9 | aliguori | prev = &pdev->next; |
136 | 4b096fc9 | aliguori | pdev = pdev->next; |
137 | 4b096fc9 | aliguori | } |
138 | 4b096fc9 | aliguori | } |
139 | 4b096fc9 | aliguori | |
140 | 4b096fc9 | aliguori | static USBHostDevice *hostdev_find(int bus_num, int addr) |
141 | 4b096fc9 | aliguori | { |
142 | 4b096fc9 | aliguori | USBHostDevice *s = hostdev_list; |
143 | 4b096fc9 | aliguori | while (s) {
|
144 | 4b096fc9 | aliguori | if (s->bus_num == bus_num && s->addr == addr)
|
145 | 4b096fc9 | aliguori | return s;
|
146 | 4b096fc9 | aliguori | s = s->next; |
147 | 4b096fc9 | aliguori | } |
148 | 4b096fc9 | aliguori | return NULL; |
149 | 4b096fc9 | aliguori | } |
150 | 4b096fc9 | aliguori | |
151 | 64838171 | aliguori | /*
|
152 | 64838171 | aliguori | * Async URB state.
|
153 | 64838171 | aliguori | * We always allocate one isoc descriptor even for bulk transfers
|
154 | 64838171 | aliguori | * to simplify allocation and casts.
|
155 | 64838171 | aliguori | */
|
156 | 64838171 | aliguori | typedef struct AsyncURB |
157 | 64838171 | aliguori | { |
158 | 64838171 | aliguori | struct usbdevfs_urb urb;
|
159 | 64838171 | aliguori | struct usbdevfs_iso_packet_desc isocpd;
|
160 | b9dc033c | balrog | |
161 | 64838171 | aliguori | USBPacket *packet; |
162 | 64838171 | aliguori | USBHostDevice *hdev; |
163 | 64838171 | aliguori | } AsyncURB; |
164 | b9dc033c | balrog | |
165 | 64838171 | aliguori | static AsyncURB *async_alloc(void) |
166 | b9dc033c | balrog | { |
167 | 64838171 | aliguori | return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB)); |
168 | b9dc033c | balrog | } |
169 | b9dc033c | balrog | |
170 | 64838171 | aliguori | static void async_free(AsyncURB *aurb) |
171 | b9dc033c | balrog | { |
172 | 64838171 | aliguori | qemu_free(aurb); |
173 | 64838171 | aliguori | } |
174 | b9dc033c | balrog | |
175 | 64838171 | aliguori | static void async_complete(void *opaque) |
176 | 64838171 | aliguori | { |
177 | 64838171 | aliguori | USBHostDevice *s = opaque; |
178 | 64838171 | aliguori | AsyncURB *aurb; |
179 | 64838171 | aliguori | |
180 | 64838171 | aliguori | while (1) { |
181 | 64838171 | aliguori | USBPacket *p; |
182 | b9dc033c | balrog | |
183 | 64838171 | aliguori | int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
|
184 | 64838171 | aliguori | if (r < 0) { |
185 | 64838171 | aliguori | if (errno == EAGAIN)
|
186 | 64838171 | aliguori | return;
|
187 | 64838171 | aliguori | |
188 | 24772c1e | aliguori | if (errno == ENODEV && !s->closing) {
|
189 | 64838171 | aliguori | printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
|
190 | 64838171 | aliguori | usb_device_del_addr(0, s->dev.addr);
|
191 | 64838171 | aliguori | return;
|
192 | 64838171 | aliguori | } |
193 | 64838171 | aliguori | |
194 | 64838171 | aliguori | dprintf("husb: async. reap urb failed errno %d\n", errno);
|
195 | 64838171 | aliguori | return;
|
196 | b9dc033c | balrog | } |
197 | 64838171 | aliguori | |
198 | 64838171 | aliguori | p = aurb->packet; |
199 | 64838171 | aliguori | |
200 | 64838171 | aliguori | dprintf("husb: async completed. aurb %p status %d alen %d\n",
|
201 | 64838171 | aliguori | aurb, aurb->urb.status, aurb->urb.actual_length); |
202 | 64838171 | aliguori | |
203 | 64838171 | aliguori | if (p) {
|
204 | 64838171 | aliguori | switch (aurb->urb.status) {
|
205 | 64838171 | aliguori | case 0: |
206 | 64838171 | aliguori | p->len = aurb->urb.actual_length; |
207 | 64838171 | aliguori | break;
|
208 | 64838171 | aliguori | |
209 | 64838171 | aliguori | case -EPIPE:
|
210 | 64838171 | aliguori | set_halt(s, p->devep); |
211 | 64838171 | aliguori | /* fall through */
|
212 | 64838171 | aliguori | default:
|
213 | 64838171 | aliguori | p->len = USB_RET_NAK; |
214 | 64838171 | aliguori | break;
|
215 | 64838171 | aliguori | } |
216 | 64838171 | aliguori | |
217 | 64838171 | aliguori | usb_packet_complete(p); |
218 | 64838171 | aliguori | } |
219 | 64838171 | aliguori | |
220 | 64838171 | aliguori | async_free(aurb); |
221 | b9dc033c | balrog | } |
222 | b9dc033c | balrog | } |
223 | b9dc033c | balrog | |
224 | 64838171 | aliguori | static void async_cancel(USBPacket *unused, void *opaque) |
225 | b9dc033c | balrog | { |
226 | 64838171 | aliguori | AsyncURB *aurb = opaque; |
227 | 64838171 | aliguori | USBHostDevice *s = aurb->hdev; |
228 | b9dc033c | balrog | |
229 | 64838171 | aliguori | dprintf("husb: async cancel. aurb %p\n", aurb);
|
230 | 64838171 | aliguori | |
231 | 64838171 | aliguori | /* Mark it as dead (see async_complete above) */
|
232 | 64838171 | aliguori | aurb->packet = NULL;
|
233 | b9dc033c | balrog | |
234 | 64838171 | aliguori | int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
|
235 | 64838171 | aliguori | if (r < 0) { |
236 | 64838171 | aliguori | dprintf("husb: async. discard urb failed errno %d\n", errno);
|
237 | b9dc033c | balrog | } |
238 | b9dc033c | balrog | } |
239 | b9dc033c | balrog | |
240 | b9dc033c | balrog | static int usb_host_update_interfaces(USBHostDevice *dev, int configuration) |
241 | b9dc033c | balrog | { |
242 | b9dc033c | balrog | int dev_descr_len, config_descr_len;
|
243 | b9dc033c | balrog | int interface, nb_interfaces, nb_configurations;
|
244 | b9dc033c | balrog | int ret, i;
|
245 | b9dc033c | balrog | |
246 | b9dc033c | balrog | if (configuration == 0) /* address state - ignore */ |
247 | b9dc033c | balrog | return 1; |
248 | b9dc033c | balrog | |
249 | b9dc033c | balrog | i = 0;
|
250 | b9dc033c | balrog | dev_descr_len = dev->descr[0];
|
251 | b9dc033c | balrog | if (dev_descr_len > dev->descr_len)
|
252 | b9dc033c | balrog | goto fail;
|
253 | b9dc033c | balrog | nb_configurations = dev->descr[17];
|
254 | b9dc033c | balrog | |
255 | b9dc033c | balrog | i += dev_descr_len; |
256 | b9dc033c | balrog | while (i < dev->descr_len) {
|
257 | 64838171 | aliguori | dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
|
258 | b9dc033c | balrog | dev->descr[i], dev->descr[i+1]);
|
259 | 64838171 | aliguori | |
260 | b9dc033c | balrog | if (dev->descr[i+1] != USB_DT_CONFIG) { |
261 | b9dc033c | balrog | i += dev->descr[i]; |
262 | b9dc033c | balrog | continue;
|
263 | b9dc033c | balrog | } |
264 | b9dc033c | balrog | config_descr_len = dev->descr[i]; |
265 | b9dc033c | balrog | |
266 | 64838171 | aliguori | printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); |
267 | 1f3870ab | aliguori | |
268 | 1f3870ab | aliguori | if (configuration < 0 || configuration == dev->descr[i + 5]) |
269 | b9dc033c | balrog | break;
|
270 | b9dc033c | balrog | |
271 | b9dc033c | balrog | i += config_descr_len; |
272 | b9dc033c | balrog | } |
273 | b9dc033c | balrog | |
274 | b9dc033c | balrog | if (i >= dev->descr_len) {
|
275 | 0d380648 | aliguori | fprintf(stderr, "husb: update iface failed. no matching configuration\n");
|
276 | b9dc033c | balrog | goto fail;
|
277 | b9dc033c | balrog | } |
278 | b9dc033c | balrog | nb_interfaces = dev->descr[i + 4];
|
279 | b9dc033c | balrog | |
280 | b9dc033c | balrog | #ifdef USBDEVFS_DISCONNECT
|
281 | b9dc033c | balrog | /* earlier Linux 2.4 do not support that */
|
282 | b9dc033c | balrog | { |
283 | b9dc033c | balrog | struct usbdevfs_ioctl ctrl;
|
284 | b9dc033c | balrog | for (interface = 0; interface < nb_interfaces; interface++) { |
285 | b9dc033c | balrog | ctrl.ioctl_code = USBDEVFS_DISCONNECT; |
286 | b9dc033c | balrog | ctrl.ifno = interface; |
287 | b9dc033c | balrog | ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); |
288 | b9dc033c | balrog | if (ret < 0 && errno != ENODATA) { |
289 | b9dc033c | balrog | perror("USBDEVFS_DISCONNECT");
|
290 | b9dc033c | balrog | goto fail;
|
291 | b9dc033c | balrog | } |
292 | b9dc033c | balrog | } |
293 | b9dc033c | balrog | } |
294 | b9dc033c | balrog | #endif
|
295 | b9dc033c | balrog | |
296 | b9dc033c | balrog | /* XXX: only grab if all interfaces are free */
|
297 | b9dc033c | balrog | for (interface = 0; interface < nb_interfaces; interface++) { |
298 | b9dc033c | balrog | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
299 | b9dc033c | balrog | if (ret < 0) { |
300 | b9dc033c | balrog | if (errno == EBUSY) {
|
301 | 64838171 | aliguori | printf("husb: update iface. device already grabbed\n");
|
302 | b9dc033c | balrog | } else {
|
303 | 64838171 | aliguori | perror("husb: failed to claim interface");
|
304 | b9dc033c | balrog | } |
305 | b9dc033c | balrog | fail:
|
306 | b9dc033c | balrog | return 0; |
307 | b9dc033c | balrog | } |
308 | b9dc033c | balrog | } |
309 | b9dc033c | balrog | |
310 | 64838171 | aliguori | printf("husb: %d interfaces claimed for configuration %d\n",
|
311 | b9dc033c | balrog | nb_interfaces, configuration); |
312 | b9dc033c | balrog | |
313 | b9dc033c | balrog | return 1; |
314 | b9dc033c | balrog | } |
315 | b9dc033c | balrog | |
316 | 059809e4 | bellard | static void usb_host_handle_reset(USBDevice *dev) |
317 | bb36d470 | bellard | { |
318 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
319 | 64838171 | aliguori | |
320 | 64838171 | aliguori | dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
|
321 | 64838171 | aliguori | |
322 | bb36d470 | bellard | ioctl(s->fd, USBDEVFS_RESET); |
323 | 64838171 | aliguori | usb_host_update_interfaces(s, s->configuration); |
324 | 5fafdf24 | ths | } |
325 | bb36d470 | bellard | |
326 | 059809e4 | bellard | static void usb_host_handle_destroy(USBDevice *dev) |
327 | 059809e4 | bellard | { |
328 | 059809e4 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
329 | 059809e4 | bellard | |
330 | 24772c1e | aliguori | s->closing = 1;
|
331 | 24772c1e | aliguori | |
332 | 64838171 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
333 | 1f3870ab | aliguori | |
334 | 4b096fc9 | aliguori | hostdev_unlink(s); |
335 | 4b096fc9 | aliguori | |
336 | 64838171 | aliguori | async_complete(s); |
337 | 64838171 | aliguori | |
338 | 059809e4 | bellard | if (s->fd >= 0) |
339 | 059809e4 | bellard | close(s->fd); |
340 | 1f3870ab | aliguori | |
341 | 059809e4 | bellard | qemu_free(s); |
342 | 059809e4 | bellard | } |
343 | 059809e4 | bellard | |
344 | b9dc033c | balrog | static int usb_linux_update_endp_table(USBHostDevice *s); |
345 | b9dc033c | balrog | |
346 | bb36d470 | bellard | static int usb_host_handle_control(USBDevice *dev, |
347 | bb36d470 | bellard | int request,
|
348 | bb36d470 | bellard | int value,
|
349 | bb36d470 | bellard | int index,
|
350 | bb36d470 | bellard | int length,
|
351 | bb36d470 | bellard | uint8_t *data) |
352 | bb36d470 | bellard | { |
353 | bb36d470 | bellard | USBHostDevice *s = (USBHostDevice *)dev; |
354 | bb36d470 | bellard | struct usb_ctrltransfer ct;
|
355 | b9dc033c | balrog | struct usbdevfs_setinterface si;
|
356 | b9dc033c | balrog | int intf_update_required = 0; |
357 | bb36d470 | bellard | int ret;
|
358 | bb36d470 | bellard | |
359 | bb36d470 | bellard | if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
|
360 | bb36d470 | bellard | /* specific SET_ADDRESS support */
|
361 | bb36d470 | bellard | dev->addr = value; |
362 | bb36d470 | bellard | return 0; |
363 | b9dc033c | balrog | } else if (request == ((USB_RECIP_INTERFACE << 8) | |
364 | b9dc033c | balrog | USB_REQ_SET_INTERFACE)) { |
365 | b9dc033c | balrog | /* set alternate setting for the interface */
|
366 | b9dc033c | balrog | si.interface = index; |
367 | b9dc033c | balrog | si.altsetting = value; |
368 | b9dc033c | balrog | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); |
369 | b9dc033c | balrog | usb_linux_update_endp_table(s); |
370 | b9dc033c | balrog | } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) { |
371 | 64838171 | aliguori | dprintf("husb: ctrl set config %d\n", value & 0xff); |
372 | b9dc033c | balrog | if (s->configuration != (value & 0xff)) { |
373 | b9dc033c | balrog | s->configuration = (value & 0xff);
|
374 | b9dc033c | balrog | intf_update_required = 1;
|
375 | b9dc033c | balrog | } |
376 | b9dc033c | balrog | goto do_request;
|
377 | bb36d470 | bellard | } else {
|
378 | b9dc033c | balrog | do_request:
|
379 | bb36d470 | bellard | ct.bRequestType = request >> 8;
|
380 | bb36d470 | bellard | ct.bRequest = request; |
381 | bb36d470 | bellard | ct.wValue = value; |
382 | bb36d470 | bellard | ct.wIndex = index; |
383 | bb36d470 | bellard | ct.wLength = length; |
384 | bb36d470 | bellard | ct.timeout = 50;
|
385 | bb36d470 | bellard | ct.data = data; |
386 | bb36d470 | bellard | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
387 | 64838171 | aliguori | |
388 | 64838171 | aliguori | dprintf("husb: ctrl req 0x%x val 0x%x index %u len %u ret %d\n",
|
389 | 64838171 | aliguori | ct.bRequest, ct.wValue, ct.wIndex, ct.wLength, ret); |
390 | b9dc033c | balrog | } |
391 | b9dc033c | balrog | |
392 | b9dc033c | balrog | if (ret < 0) { |
393 | b9dc033c | balrog | switch(errno) {
|
394 | b9dc033c | balrog | case ETIMEDOUT:
|
395 | b9dc033c | balrog | return USB_RET_NAK;
|
396 | b9dc033c | balrog | default:
|
397 | b9dc033c | balrog | return USB_RET_STALL;
|
398 | bb36d470 | bellard | } |
399 | b9dc033c | balrog | } else {
|
400 | b9dc033c | balrog | if (intf_update_required) {
|
401 | 64838171 | aliguori | dprintf("husb: updating interfaces\n");
|
402 | b9dc033c | balrog | usb_host_update_interfaces(s, value & 0xff);
|
403 | b9dc033c | balrog | } |
404 | b9dc033c | balrog | return ret;
|
405 | b9dc033c | balrog | } |
406 | bb36d470 | bellard | } |
407 | bb36d470 | bellard | |
408 | 4d611c9a | pbrook | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
409 | bb36d470 | bellard | { |
410 | 64838171 | aliguori | USBHostDevice *s = (USBHostDevice *) dev; |
411 | 64838171 | aliguori | AsyncURB *aurb; |
412 | 64838171 | aliguori | struct usbdevfs_urb *urb;
|
413 | bb36d470 | bellard | int ret;
|
414 | bb36d470 | bellard | |
415 | 64838171 | aliguori | aurb = async_alloc(); |
416 | 64838171 | aliguori | if (!aurb) {
|
417 | 64838171 | aliguori | dprintf("husb: async malloc failed\n");
|
418 | 64838171 | aliguori | return USB_RET_NAK;
|
419 | b9dc033c | balrog | } |
420 | 64838171 | aliguori | aurb->hdev = s; |
421 | 64838171 | aliguori | aurb->packet = p; |
422 | 64838171 | aliguori | |
423 | 64838171 | aliguori | urb = &aurb->urb; |
424 | b9dc033c | balrog | |
425 | 4d611c9a | pbrook | if (p->pid == USB_TOKEN_IN)
|
426 | 64838171 | aliguori | urb->endpoint = p->devep | 0x80;
|
427 | 64838171 | aliguori | else
|
428 | 64838171 | aliguori | urb->endpoint = p->devep; |
429 | 64838171 | aliguori | |
430 | 64838171 | aliguori | if (is_halted(s, p->devep)) {
|
431 | 64838171 | aliguori | ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint); |
432 | 64838171 | aliguori | if (ret < 0) { |
433 | 64838171 | aliguori | dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
|
434 | 64838171 | aliguori | urb->endpoint, errno); |
435 | bb36d470 | bellard | return USB_RET_NAK;
|
436 | bb36d470 | bellard | } |
437 | 64838171 | aliguori | clear_halt(s, p->devep); |
438 | 4d043a09 | balrog | } |
439 | 4d043a09 | balrog | |
440 | 64838171 | aliguori | urb->buffer = p->data; |
441 | 64838171 | aliguori | urb->buffer_length = p->len; |
442 | b9dc033c | balrog | |
443 | 64838171 | aliguori | if (is_isoc(s, p->devep)) {
|
444 | 64838171 | aliguori | /* Setup ISOC transfer */
|
445 | 64838171 | aliguori | urb->type = USBDEVFS_URB_TYPE_ISO; |
446 | 64838171 | aliguori | urb->flags = USBDEVFS_URB_ISO_ASAP; |
447 | 64838171 | aliguori | urb->number_of_packets = 1;
|
448 | 64838171 | aliguori | urb->iso_frame_desc[0].length = p->len;
|
449 | 64838171 | aliguori | } else {
|
450 | 64838171 | aliguori | /* Setup bulk transfer */
|
451 | 64838171 | aliguori | urb->type = USBDEVFS_URB_TYPE_BULK; |
452 | b9dc033c | balrog | } |
453 | b9dc033c | balrog | |
454 | 64838171 | aliguori | urb->usercontext = s; |
455 | b9dc033c | balrog | |
456 | 64838171 | aliguori | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
457 | b9dc033c | balrog | |
458 | 64838171 | aliguori | dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
|
459 | b9dc033c | balrog | |
460 | 64838171 | aliguori | if (ret < 0) { |
461 | 64838171 | aliguori | dprintf("husb: submit failed. errno %d\n", errno);
|
462 | 64838171 | aliguori | async_free(aurb); |
463 | b9dc033c | balrog | |
464 | b9dc033c | balrog | switch(errno) {
|
465 | b9dc033c | balrog | case ETIMEDOUT:
|
466 | b9dc033c | balrog | return USB_RET_NAK;
|
467 | b9dc033c | balrog | case EPIPE:
|
468 | b9dc033c | balrog | default:
|
469 | b9dc033c | balrog | return USB_RET_STALL;
|
470 | b9dc033c | balrog | } |
471 | b9dc033c | balrog | } |
472 | 64838171 | aliguori | |
473 | 64838171 | aliguori | usb_defer_packet(p, async_cancel, aurb); |
474 | b9dc033c | balrog | return USB_RET_ASYNC;
|
475 | b9dc033c | balrog | } |
476 | b9dc033c | balrog | |
477 | b9dc033c | balrog | /* returns 1 on problem encountered or 0 for success */
|
478 | b9dc033c | balrog | static int usb_linux_update_endp_table(USBHostDevice *s) |
479 | b9dc033c | balrog | { |
480 | b9dc033c | balrog | uint8_t *descriptors; |
481 | b9dc033c | balrog | uint8_t devep, type, configuration, alt_interface; |
482 | b9dc033c | balrog | struct usb_ctrltransfer ct;
|
483 | b9dc033c | balrog | int interface, ret, length, i;
|
484 | b9dc033c | balrog | |
485 | b9dc033c | balrog | ct.bRequestType = USB_DIR_IN; |
486 | b9dc033c | balrog | ct.bRequest = USB_REQ_GET_CONFIGURATION; |
487 | b9dc033c | balrog | ct.wValue = 0;
|
488 | b9dc033c | balrog | ct.wIndex = 0;
|
489 | b9dc033c | balrog | ct.wLength = 1;
|
490 | b9dc033c | balrog | ct.data = &configuration; |
491 | b9dc033c | balrog | ct.timeout = 50;
|
492 | b9dc033c | balrog | |
493 | b9dc033c | balrog | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
494 | b9dc033c | balrog | if (ret < 0) { |
495 | b9dc033c | balrog | perror("usb_linux_update_endp_table");
|
496 | b9dc033c | balrog | return 1; |
497 | b9dc033c | balrog | } |
498 | b9dc033c | balrog | |
499 | b9dc033c | balrog | /* in address state */
|
500 | b9dc033c | balrog | if (configuration == 0) |
501 | b9dc033c | balrog | return 1; |
502 | b9dc033c | balrog | |
503 | b9dc033c | balrog | /* get the desired configuration, interface, and endpoint descriptors
|
504 | b9dc033c | balrog | * from device description */
|
505 | b9dc033c | balrog | descriptors = &s->descr[18];
|
506 | b9dc033c | balrog | length = s->descr_len - 18;
|
507 | b9dc033c | balrog | i = 0;
|
508 | b9dc033c | balrog | |
509 | b9dc033c | balrog | if (descriptors[i + 1] != USB_DT_CONFIG || |
510 | b9dc033c | balrog | descriptors[i + 5] != configuration) {
|
511 | 64838171 | aliguori | dprintf("invalid descriptor data - configuration\n");
|
512 | b9dc033c | balrog | return 1; |
513 | b9dc033c | balrog | } |
514 | b9dc033c | balrog | i += descriptors[i]; |
515 | b9dc033c | balrog | |
516 | b9dc033c | balrog | while (i < length) {
|
517 | b9dc033c | balrog | if (descriptors[i + 1] != USB_DT_INTERFACE || |
518 | b9dc033c | balrog | (descriptors[i + 1] == USB_DT_INTERFACE &&
|
519 | b9dc033c | balrog | descriptors[i + 4] == 0)) { |
520 | b9dc033c | balrog | i += descriptors[i]; |
521 | b9dc033c | balrog | continue;
|
522 | b9dc033c | balrog | } |
523 | b9dc033c | balrog | |
524 | b9dc033c | balrog | interface = descriptors[i + 2];
|
525 | b9dc033c | balrog | |
526 | b9dc033c | balrog | ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE; |
527 | b9dc033c | balrog | ct.bRequest = USB_REQ_GET_INTERFACE; |
528 | b9dc033c | balrog | ct.wValue = 0;
|
529 | b9dc033c | balrog | ct.wIndex = interface; |
530 | b9dc033c | balrog | ct.wLength = 1;
|
531 | b9dc033c | balrog | ct.data = &alt_interface; |
532 | b9dc033c | balrog | ct.timeout = 50;
|
533 | b9dc033c | balrog | |
534 | b9dc033c | balrog | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); |
535 | b9dc033c | balrog | if (ret < 0) { |
536 | b9dc033c | balrog | perror("usb_linux_update_endp_table");
|
537 | b9dc033c | balrog | return 1; |
538 | b9dc033c | balrog | } |
539 | b9dc033c | balrog | |
540 | b9dc033c | balrog | /* the current interface descriptor is the active interface
|
541 | b9dc033c | balrog | * and has endpoints */
|
542 | b9dc033c | balrog | if (descriptors[i + 3] != alt_interface) { |
543 | b9dc033c | balrog | i += descriptors[i]; |
544 | b9dc033c | balrog | continue;
|
545 | b9dc033c | balrog | } |
546 | b9dc033c | balrog | |
547 | b9dc033c | balrog | /* advance to the endpoints */
|
548 | b9dc033c | balrog | while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) |
549 | b9dc033c | balrog | i += descriptors[i]; |
550 | b9dc033c | balrog | |
551 | b9dc033c | balrog | if (i >= length)
|
552 | b9dc033c | balrog | break;
|
553 | b9dc033c | balrog | |
554 | b9dc033c | balrog | while (i < length) {
|
555 | b9dc033c | balrog | if (descriptors[i + 1] != USB_DT_ENDPOINT) |
556 | b9dc033c | balrog | break;
|
557 | b9dc033c | balrog | |
558 | b9dc033c | balrog | devep = descriptors[i + 2];
|
559 | b9dc033c | balrog | switch (descriptors[i + 3] & 0x3) { |
560 | b9dc033c | balrog | case 0x00: |
561 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_CONTROL; |
562 | b9dc033c | balrog | break;
|
563 | b9dc033c | balrog | case 0x01: |
564 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_ISO; |
565 | b9dc033c | balrog | break;
|
566 | b9dc033c | balrog | case 0x02: |
567 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_BULK; |
568 | b9dc033c | balrog | break;
|
569 | b9dc033c | balrog | case 0x03: |
570 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_INTERRUPT; |
571 | b9dc033c | balrog | break;
|
572 | b9dc033c | balrog | default:
|
573 | 64838171 | aliguori | dprintf("usb_host: malformed endpoint type\n");
|
574 | b9dc033c | balrog | type = USBDEVFS_URB_TYPE_BULK; |
575 | b9dc033c | balrog | } |
576 | b9dc033c | balrog | s->endp_table[(devep & 0xf) - 1].type = type; |
577 | 64838171 | aliguori | s->endp_table[(devep & 0xf) - 1].halted = 0; |
578 | b9dc033c | balrog | |
579 | b9dc033c | balrog | i += descriptors[i]; |
580 | b9dc033c | balrog | } |
581 | b9dc033c | balrog | } |
582 | b9dc033c | balrog | return 0; |
583 | b9dc033c | balrog | } |
584 | b9dc033c | balrog | |
585 | 4b096fc9 | aliguori | static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name) |
586 | bb36d470 | bellard | { |
587 | b9dc033c | balrog | int fd = -1, ret; |
588 | b9dc033c | balrog | USBHostDevice *dev = NULL;
|
589 | bb36d470 | bellard | struct usbdevfs_connectinfo ci;
|
590 | a594cfbf | bellard | char buf[1024]; |
591 | 1f3870ab | aliguori | |
592 | b9dc033c | balrog | dev = qemu_mallocz(sizeof(USBHostDevice));
|
593 | b9dc033c | balrog | if (!dev)
|
594 | b9dc033c | balrog | goto fail;
|
595 | b9dc033c | balrog | |
596 | 4b096fc9 | aliguori | dev->bus_num = bus_num; |
597 | 4b096fc9 | aliguori | dev->addr = addr; |
598 | 4b096fc9 | aliguori | |
599 | 64838171 | aliguori | printf("husb: open device %d.%d\n", bus_num, addr);
|
600 | 3b46e624 | ths | |
601 | 5fafdf24 | ths | snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d", |
602 | a594cfbf | bellard | bus_num, addr); |
603 | b9dc033c | balrog | fd = open(buf, O_RDWR | O_NONBLOCK); |
604 | bb36d470 | bellard | if (fd < 0) { |
605 | a594cfbf | bellard | perror(buf); |
606 | 1f3870ab | aliguori | goto fail;
|
607 | bb36d470 | bellard | } |
608 | bb36d470 | bellard | |
609 | b9dc033c | balrog | /* read the device description */
|
610 | b9dc033c | balrog | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
|
611 | b9dc033c | balrog | if (dev->descr_len <= 0) { |
612 | 64838171 | aliguori | perror("husb: reading device data failed");
|
613 | bb36d470 | bellard | goto fail;
|
614 | bb36d470 | bellard | } |
615 | 3b46e624 | ths | |
616 | b9dc033c | balrog | #ifdef DEBUG
|
617 | 868bfe2b | bellard | { |
618 | b9dc033c | balrog | int x;
|
619 | b9dc033c | balrog | printf("=== begin dumping device descriptor data ===\n");
|
620 | b9dc033c | balrog | for (x = 0; x < dev->descr_len; x++) |
621 | b9dc033c | balrog | printf("%02x ", dev->descr[x]);
|
622 | b9dc033c | balrog | printf("\n=== end dumping device descriptor data ===\n");
|
623 | bb36d470 | bellard | } |
624 | a594cfbf | bellard | #endif
|
625 | a594cfbf | bellard | |
626 | b9dc033c | balrog | dev->fd = fd; |
627 | b9dc033c | balrog | dev->configuration = 1;
|
628 | b9dc033c | balrog | |
629 | b9dc033c | balrog | /* XXX - do something about initial configuration */
|
630 | 1f3870ab | aliguori | if (!usb_host_update_interfaces(dev, -1)) |
631 | b9dc033c | balrog | goto fail;
|
632 | bb36d470 | bellard | |
633 | bb36d470 | bellard | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); |
634 | bb36d470 | bellard | if (ret < 0) { |
635 | 046833ea | balrog | perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
|
636 | bb36d470 | bellard | goto fail;
|
637 | bb36d470 | bellard | } |
638 | bb36d470 | bellard | |
639 | 64838171 | aliguori | printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
|
640 | bb36d470 | bellard | |
641 | b9dc033c | balrog | ret = usb_linux_update_endp_table(dev); |
642 | b9dc033c | balrog | if (ret)
|
643 | bb36d470 | bellard | goto fail;
|
644 | b9dc033c | balrog | |
645 | bb36d470 | bellard | if (ci.slow)
|
646 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_LOW; |
647 | bb36d470 | bellard | else
|
648 | bb36d470 | bellard | dev->dev.speed = USB_SPEED_HIGH; |
649 | a594cfbf | bellard | dev->dev.handle_packet = usb_generic_handle_packet; |
650 | bb36d470 | bellard | |
651 | bb36d470 | bellard | dev->dev.handle_reset = usb_host_handle_reset; |
652 | bb36d470 | bellard | dev->dev.handle_control = usb_host_handle_control; |
653 | bb36d470 | bellard | dev->dev.handle_data = usb_host_handle_data; |
654 | 059809e4 | bellard | dev->dev.handle_destroy = usb_host_handle_destroy; |
655 | 1f6e24e7 | bellard | |
656 | 4b096fc9 | aliguori | if (!prod_name || prod_name[0] == '\0') |
657 | 1f6e24e7 | bellard | snprintf(dev->dev.devname, sizeof(dev->dev.devname),
|
658 | 4b096fc9 | aliguori | "host:%d.%d", bus_num, addr);
|
659 | 1f6e24e7 | bellard | else
|
660 | 1f6e24e7 | bellard | pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
|
661 | 4b096fc9 | aliguori | prod_name); |
662 | 1f6e24e7 | bellard | |
663 | 64838171 | aliguori | /* USB devio uses 'write' flag to check for async completions */
|
664 | 64838171 | aliguori | qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
|
665 | 1f3870ab | aliguori | |
666 | 4b096fc9 | aliguori | hostdev_link(dev); |
667 | 4b096fc9 | aliguori | |
668 | 64838171 | aliguori | return (USBDevice *) dev;
|
669 | 4b096fc9 | aliguori | |
670 | b9dc033c | balrog | fail:
|
671 | 24772c1e | aliguori | if (dev)
|
672 | b9dc033c | balrog | qemu_free(dev); |
673 | 24772c1e | aliguori | |
674 | b9dc033c | balrog | close(fd); |
675 | b9dc033c | balrog | return NULL; |
676 | a594cfbf | bellard | } |
677 | bb36d470 | bellard | |
678 | 4b096fc9 | aliguori | USBDevice *usb_host_device_open(const char *devname) |
679 | 4b096fc9 | aliguori | { |
680 | 4b096fc9 | aliguori | int bus_num, addr;
|
681 | 4b096fc9 | aliguori | char product_name[PRODUCT_NAME_SZ];
|
682 | 4b096fc9 | aliguori | |
683 | 4b096fc9 | aliguori | if (usb_host_find_device(&bus_num, &addr,
|
684 | 4b096fc9 | aliguori | product_name, sizeof(product_name),
|
685 | 4b096fc9 | aliguori | devname) < 0)
|
686 | 4b096fc9 | aliguori | return NULL; |
687 | 4b096fc9 | aliguori | |
688 | 4b096fc9 | aliguori | if (hostdev_find(bus_num, addr)) {
|
689 | 64838171 | aliguori | term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
|
690 | 4b096fc9 | aliguori | return NULL; |
691 | 4b096fc9 | aliguori | } |
692 | 4b096fc9 | aliguori | |
693 | 4b096fc9 | aliguori | return usb_host_device_open_addr(bus_num, addr, product_name);
|
694 | 4b096fc9 | aliguori | } |
695 | 4b096fc9 | aliguori | |
696 | a594cfbf | bellard | static int get_tag_value(char *buf, int buf_size, |
697 | 5fafdf24 | ths | const char *str, const char *tag, |
698 | a594cfbf | bellard | const char *stopchars) |
699 | a594cfbf | bellard | { |
700 | a594cfbf | bellard | const char *p; |
701 | a594cfbf | bellard | char *q;
|
702 | a594cfbf | bellard | p = strstr(str, tag); |
703 | a594cfbf | bellard | if (!p)
|
704 | a594cfbf | bellard | return -1; |
705 | a594cfbf | bellard | p += strlen(tag); |
706 | a594cfbf | bellard | while (isspace(*p))
|
707 | a594cfbf | bellard | p++; |
708 | a594cfbf | bellard | q = buf; |
709 | a594cfbf | bellard | while (*p != '\0' && !strchr(stopchars, *p)) { |
710 | a594cfbf | bellard | if ((q - buf) < (buf_size - 1)) |
711 | a594cfbf | bellard | *q++ = *p; |
712 | a594cfbf | bellard | p++; |
713 | a594cfbf | bellard | } |
714 | a594cfbf | bellard | *q = '\0';
|
715 | a594cfbf | bellard | return q - buf;
|
716 | bb36d470 | bellard | } |
717 | bb36d470 | bellard | |
718 | a594cfbf | bellard | static int usb_host_scan(void *opaque, USBScanFunc *func) |
719 | bb36d470 | bellard | { |
720 | a594cfbf | bellard | FILE *f; |
721 | a594cfbf | bellard | char line[1024]; |
722 | bb36d470 | bellard | char buf[1024]; |
723 | a594cfbf | bellard | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
|
724 | a594cfbf | bellard | int ret;
|
725 | a594cfbf | bellard | char product_name[512]; |
726 | 3b46e624 | ths | |
727 | a594cfbf | bellard | f = fopen(USBDEVFS_PATH "/devices", "r"); |
728 | a594cfbf | bellard | if (!f) {
|
729 | 64838171 | aliguori | term_printf("husb: could not open %s\n", USBDEVFS_PATH "/devices"); |
730 | a594cfbf | bellard | return 0; |
731 | a594cfbf | bellard | } |
732 | a594cfbf | bellard | device_count = 0;
|
733 | a594cfbf | bellard | bus_num = addr = speed = class_id = product_id = vendor_id = 0;
|
734 | a594cfbf | bellard | ret = 0;
|
735 | bb36d470 | bellard | for(;;) {
|
736 | a594cfbf | bellard | if (fgets(line, sizeof(line), f) == NULL) |
737 | bb36d470 | bellard | break;
|
738 | a594cfbf | bellard | if (strlen(line) > 0) |
739 | a594cfbf | bellard | line[strlen(line) - 1] = '\0'; |
740 | a594cfbf | bellard | if (line[0] == 'T' && line[1] == ':') { |
741 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
742 | 38ca0f6d | pbrook | /* New device. Add the previously discovered device. */
|
743 | 5fafdf24 | ths | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
744 | a594cfbf | bellard | product_id, product_name, speed); |
745 | a594cfbf | bellard | if (ret)
|
746 | a594cfbf | bellard | goto the_end;
|
747 | a594cfbf | bellard | } |
748 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) |
749 | a594cfbf | bellard | goto fail;
|
750 | a594cfbf | bellard | bus_num = atoi(buf); |
751 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) |
752 | a594cfbf | bellard | goto fail;
|
753 | a594cfbf | bellard | addr = atoi(buf); |
754 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) |
755 | a594cfbf | bellard | goto fail;
|
756 | a594cfbf | bellard | if (!strcmp(buf, "480")) |
757 | a594cfbf | bellard | speed = USB_SPEED_HIGH; |
758 | a594cfbf | bellard | else if (!strcmp(buf, "1.5")) |
759 | a594cfbf | bellard | speed = USB_SPEED_LOW; |
760 | a594cfbf | bellard | else
|
761 | a594cfbf | bellard | speed = USB_SPEED_FULL; |
762 | a594cfbf | bellard | product_name[0] = '\0'; |
763 | a594cfbf | bellard | class_id = 0xff;
|
764 | a594cfbf | bellard | device_count++; |
765 | a594cfbf | bellard | product_id = 0;
|
766 | a594cfbf | bellard | vendor_id = 0;
|
767 | a594cfbf | bellard | } else if (line[0] == 'P' && line[1] == ':') { |
768 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) |
769 | a594cfbf | bellard | goto fail;
|
770 | a594cfbf | bellard | vendor_id = strtoul(buf, NULL, 16); |
771 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) |
772 | a594cfbf | bellard | goto fail;
|
773 | a594cfbf | bellard | product_id = strtoul(buf, NULL, 16); |
774 | a594cfbf | bellard | } else if (line[0] == 'S' && line[1] == ':') { |
775 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) |
776 | a594cfbf | bellard | goto fail;
|
777 | a594cfbf | bellard | pstrcpy(product_name, sizeof(product_name), buf);
|
778 | a594cfbf | bellard | } else if (line[0] == 'D' && line[1] == ':') { |
779 | a594cfbf | bellard | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) |
780 | a594cfbf | bellard | goto fail;
|
781 | a594cfbf | bellard | class_id = strtoul(buf, NULL, 16); |
782 | bb36d470 | bellard | } |
783 | a594cfbf | bellard | fail: ;
|
784 | a594cfbf | bellard | } |
785 | 38ca0f6d | pbrook | if (device_count && (vendor_id || product_id)) {
|
786 | 38ca0f6d | pbrook | /* Add the last device. */
|
787 | 5fafdf24 | ths | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
788 | a594cfbf | bellard | product_id, product_name, speed); |
789 | bb36d470 | bellard | } |
790 | a594cfbf | bellard | the_end:
|
791 | a594cfbf | bellard | fclose(f); |
792 | a594cfbf | bellard | return ret;
|
793 | bb36d470 | bellard | } |
794 | bb36d470 | bellard | |
795 | 4b096fc9 | aliguori | struct USBAutoFilter {
|
796 | 4b096fc9 | aliguori | struct USBAutoFilter *next;
|
797 | 4b096fc9 | aliguori | int bus_num;
|
798 | 4b096fc9 | aliguori | int addr;
|
799 | 4b096fc9 | aliguori | int vendor_id;
|
800 | 4b096fc9 | aliguori | int product_id;
|
801 | 4b096fc9 | aliguori | }; |
802 | 4b096fc9 | aliguori | |
803 | 4b096fc9 | aliguori | static QEMUTimer *usb_auto_timer;
|
804 | 4b096fc9 | aliguori | static struct USBAutoFilter *usb_auto_filter; |
805 | 4b096fc9 | aliguori | |
806 | 4b096fc9 | aliguori | static int usb_host_auto_scan(void *opaque, int bus_num, int addr, |
807 | 4b096fc9 | aliguori | int class_id, int vendor_id, int product_id, |
808 | 4b096fc9 | aliguori | const char *product_name, int speed) |
809 | 4b096fc9 | aliguori | { |
810 | 4b096fc9 | aliguori | struct USBAutoFilter *f;
|
811 | 4b096fc9 | aliguori | struct USBDevice *dev;
|
812 | 4b096fc9 | aliguori | |
813 | 4b096fc9 | aliguori | /* Ignore hubs */
|
814 | 4b096fc9 | aliguori | if (class_id == 9) |
815 | 4b096fc9 | aliguori | return 0; |
816 | 4b096fc9 | aliguori | |
817 | 4b096fc9 | aliguori | for (f = usb_auto_filter; f; f = f->next) {
|
818 | 4b096fc9 | aliguori | if (f->bus_num >= 0 && f->bus_num != bus_num) |
819 | 4b096fc9 | aliguori | continue;
|
820 | 4b096fc9 | aliguori | |
821 | 4b096fc9 | aliguori | if (f->addr >= 0 && f->addr != addr) |
822 | 4b096fc9 | aliguori | continue;
|
823 | 4b096fc9 | aliguori | |
824 | 4b096fc9 | aliguori | if (f->vendor_id >= 0 && f->vendor_id != vendor_id) |
825 | 4b096fc9 | aliguori | continue;
|
826 | 4b096fc9 | aliguori | |
827 | 4b096fc9 | aliguori | if (f->product_id >= 0 && f->product_id != product_id) |
828 | 4b096fc9 | aliguori | continue;
|
829 | 4b096fc9 | aliguori | |
830 | 4b096fc9 | aliguori | /* We got a match */
|
831 | 4b096fc9 | aliguori | |
832 | 4b096fc9 | aliguori | /* Allredy attached ? */
|
833 | 4b096fc9 | aliguori | if (hostdev_find(bus_num, addr))
|
834 | 4b096fc9 | aliguori | return 0; |
835 | 4b096fc9 | aliguori | |
836 | 64838171 | aliguori | dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
|
837 | 4b096fc9 | aliguori | |
838 | 4b096fc9 | aliguori | dev = usb_host_device_open_addr(bus_num, addr, product_name); |
839 | 4b096fc9 | aliguori | if (dev)
|
840 | 4b096fc9 | aliguori | usb_device_add_dev(dev); |
841 | 4b096fc9 | aliguori | } |
842 | 4b096fc9 | aliguori | |
843 | 4b096fc9 | aliguori | return 0; |
844 | 4b096fc9 | aliguori | } |
845 | 4b096fc9 | aliguori | |
846 | 4b096fc9 | aliguori | static void usb_host_auto_timer(void *unused) |
847 | 4b096fc9 | aliguori | { |
848 | 4b096fc9 | aliguori | usb_host_scan(NULL, usb_host_auto_scan);
|
849 | 4b096fc9 | aliguori | qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
|
850 | 4b096fc9 | aliguori | } |
851 | 4b096fc9 | aliguori | |
852 | 4b096fc9 | aliguori | /*
|
853 | 4b096fc9 | aliguori | * Add autoconnect filter
|
854 | 4b096fc9 | aliguori | * -1 means 'any' (device, vendor, etc)
|
855 | 4b096fc9 | aliguori | */
|
856 | 4b096fc9 | aliguori | static void usb_host_auto_add(int bus_num, int addr, int vendor_id, int product_id) |
857 | 4b096fc9 | aliguori | { |
858 | 4b096fc9 | aliguori | struct USBAutoFilter *f = qemu_mallocz(sizeof(*f)); |
859 | 4b096fc9 | aliguori | if (!f) {
|
860 | 0d380648 | aliguori | fprintf(stderr, "husb: failed to allocate auto filter\n");
|
861 | 0d380648 | aliguori | return;
|
862 | 4b096fc9 | aliguori | } |
863 | 4b096fc9 | aliguori | |
864 | 4b096fc9 | aliguori | f->bus_num = bus_num; |
865 | 4b096fc9 | aliguori | f->addr = addr; |
866 | 4b096fc9 | aliguori | f->vendor_id = vendor_id; |
867 | 4b096fc9 | aliguori | f->product_id = product_id; |
868 | 4b096fc9 | aliguori | |
869 | 4b096fc9 | aliguori | if (!usb_auto_filter) {
|
870 | 4b096fc9 | aliguori | /*
|
871 | 4b096fc9 | aliguori | * First entry. Init and start the monitor.
|
872 | 4b096fc9 | aliguori | * Right now we're using timer to check for new devices.
|
873 | 4b096fc9 | aliguori | * If this turns out to be too expensive we can move that into a
|
874 | 4b096fc9 | aliguori | * separate thread.
|
875 | 4b096fc9 | aliguori | */
|
876 | 4b096fc9 | aliguori | usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
|
877 | 4b096fc9 | aliguori | if (!usb_auto_timer) {
|
878 | 0d380648 | aliguori | fprintf(stderr, "husb: failed to allocate auto scan timer\n");
|
879 | 4b096fc9 | aliguori | qemu_free(f); |
880 | 4b096fc9 | aliguori | return;
|
881 | 4b096fc9 | aliguori | } |
882 | 4b096fc9 | aliguori | |
883 | 4b096fc9 | aliguori | /* Check for new devices every two seconds */
|
884 | 4b096fc9 | aliguori | qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
|
885 | 4b096fc9 | aliguori | } |
886 | 4b096fc9 | aliguori | |
887 | 64838171 | aliguori | dprintf("husb: auto filter: bus_num %d addr %d vid %d pid %d\n",
|
888 | 4b096fc9 | aliguori | bus_num, addr, vendor_id, product_id); |
889 | 4b096fc9 | aliguori | |
890 | 4b096fc9 | aliguori | f->next = usb_auto_filter; |
891 | 4b096fc9 | aliguori | usb_auto_filter = f; |
892 | 4b096fc9 | aliguori | } |
893 | 4b096fc9 | aliguori | |
894 | a594cfbf | bellard | typedef struct FindDeviceState { |
895 | a594cfbf | bellard | int vendor_id;
|
896 | a594cfbf | bellard | int product_id;
|
897 | a594cfbf | bellard | int bus_num;
|
898 | a594cfbf | bellard | int addr;
|
899 | 1f6e24e7 | bellard | char product_name[PRODUCT_NAME_SZ];
|
900 | a594cfbf | bellard | } FindDeviceState; |
901 | a594cfbf | bellard | |
902 | 5fafdf24 | ths | static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, |
903 | a594cfbf | bellard | int class_id,
|
904 | 5fafdf24 | ths | int vendor_id, int product_id, |
905 | a594cfbf | bellard | const char *product_name, int speed) |
906 | bb36d470 | bellard | { |
907 | a594cfbf | bellard | FindDeviceState *s = opaque; |
908 | 1f6e24e7 | bellard | if ((vendor_id == s->vendor_id &&
|
909 | 1f6e24e7 | bellard | product_id == s->product_id) || |
910 | 1f6e24e7 | bellard | (bus_num == s->bus_num && |
911 | 1f6e24e7 | bellard | addr == s->addr)) { |
912 | 1f6e24e7 | bellard | pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name); |
913 | a594cfbf | bellard | s->bus_num = bus_num; |
914 | a594cfbf | bellard | s->addr = addr; |
915 | a594cfbf | bellard | return 1; |
916 | a594cfbf | bellard | } else {
|
917 | a594cfbf | bellard | return 0; |
918 | a594cfbf | bellard | } |
919 | a594cfbf | bellard | } |
920 | bb36d470 | bellard | |
921 | 5fafdf24 | ths | /* the syntax is :
|
922 | 5fafdf24 | ths | 'bus.addr' (decimal numbers) or
|
923 | a594cfbf | bellard | 'vendor_id:product_id' (hexa numbers) */
|
924 | 5fafdf24 | ths | static int usb_host_find_device(int *pbus_num, int *paddr, |
925 | 1f6e24e7 | bellard | char *product_name, int product_name_size, |
926 | a594cfbf | bellard | const char *devname) |
927 | a594cfbf | bellard | { |
928 | a594cfbf | bellard | const char *p; |
929 | a594cfbf | bellard | int ret;
|
930 | a594cfbf | bellard | FindDeviceState fs; |
931 | a594cfbf | bellard | |
932 | a594cfbf | bellard | p = strchr(devname, '.');
|
933 | a594cfbf | bellard | if (p) {
|
934 | a594cfbf | bellard | *pbus_num = strtoul(devname, NULL, 0); |
935 | 4b096fc9 | aliguori | |
936 | 4b096fc9 | aliguori | if (*(p + 1) == '*') { |
937 | 4b096fc9 | aliguori | usb_host_auto_add(*pbus_num, -1, -1, -1); |
938 | 4b096fc9 | aliguori | return -1; |
939 | 4b096fc9 | aliguori | } |
940 | 4b096fc9 | aliguori | |
941 | a594cfbf | bellard | *paddr = strtoul(p + 1, NULL, 0); |
942 | 1f6e24e7 | bellard | fs.bus_num = *pbus_num; |
943 | 1f6e24e7 | bellard | fs.addr = *paddr; |
944 | 1f6e24e7 | bellard | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
945 | 1f6e24e7 | bellard | if (ret)
|
946 | 1f6e24e7 | bellard | pstrcpy(product_name, product_name_size, fs.product_name); |
947 | a594cfbf | bellard | return 0; |
948 | a594cfbf | bellard | } |
949 | a594cfbf | bellard | p = strchr(devname, ':');
|
950 | a594cfbf | bellard | if (p) {
|
951 | a594cfbf | bellard | fs.vendor_id = strtoul(devname, NULL, 16); |
952 | 4b096fc9 | aliguori | |
953 | 4b096fc9 | aliguori | if (*(p + 1) == '*') { |
954 | 4b096fc9 | aliguori | usb_host_auto_add(-1, -1, fs.vendor_id, -1); |
955 | 4b096fc9 | aliguori | return -1; |
956 | 4b096fc9 | aliguori | } |
957 | 4b096fc9 | aliguori | |
958 | a594cfbf | bellard | fs.product_id = strtoul(p + 1, NULL, 16); |
959 | a594cfbf | bellard | ret = usb_host_scan(&fs, usb_host_find_device_scan); |
960 | a594cfbf | bellard | if (ret) {
|
961 | a594cfbf | bellard | *pbus_num = fs.bus_num; |
962 | a594cfbf | bellard | *paddr = fs.addr; |
963 | 1f6e24e7 | bellard | pstrcpy(product_name, product_name_size, fs.product_name); |
964 | a594cfbf | bellard | return 0; |
965 | bb36d470 | bellard | } |
966 | bb36d470 | bellard | } |
967 | a594cfbf | bellard | return -1; |
968 | bb36d470 | bellard | } |
969 | bb36d470 | bellard | |
970 | a594cfbf | bellard | /**********************/
|
971 | a594cfbf | bellard | /* USB host device info */
|
972 | a594cfbf | bellard | |
973 | a594cfbf | bellard | struct usb_class_info {
|
974 | a594cfbf | bellard | int class;
|
975 | a594cfbf | bellard | const char *class_name; |
976 | a594cfbf | bellard | }; |
977 | a594cfbf | bellard | |
978 | a594cfbf | bellard | static const struct usb_class_info usb_class_info[] = { |
979 | a594cfbf | bellard | { USB_CLASS_AUDIO, "Audio"},
|
980 | a594cfbf | bellard | { USB_CLASS_COMM, "Communication"},
|
981 | a594cfbf | bellard | { USB_CLASS_HID, "HID"},
|
982 | a594cfbf | bellard | { USB_CLASS_HUB, "Hub" },
|
983 | a594cfbf | bellard | { USB_CLASS_PHYSICAL, "Physical" },
|
984 | a594cfbf | bellard | { USB_CLASS_PRINTER, "Printer" },
|
985 | a594cfbf | bellard | { USB_CLASS_MASS_STORAGE, "Storage" },
|
986 | a594cfbf | bellard | { USB_CLASS_CDC_DATA, "Data" },
|
987 | a594cfbf | bellard | { USB_CLASS_APP_SPEC, "Application Specific" },
|
988 | a594cfbf | bellard | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
|
989 | a594cfbf | bellard | { USB_CLASS_STILL_IMAGE, "Still Image" },
|
990 | b9dc033c | balrog | { USB_CLASS_CSCID, "Smart Card" },
|
991 | a594cfbf | bellard | { USB_CLASS_CONTENT_SEC, "Content Security" },
|
992 | a594cfbf | bellard | { -1, NULL } |
993 | a594cfbf | bellard | }; |
994 | a594cfbf | bellard | |
995 | a594cfbf | bellard | static const char *usb_class_str(uint8_t class) |
996 | bb36d470 | bellard | { |
997 | a594cfbf | bellard | const struct usb_class_info *p; |
998 | a594cfbf | bellard | for(p = usb_class_info; p->class != -1; p++) { |
999 | a594cfbf | bellard | if (p->class == class)
|
1000 | a594cfbf | bellard | break;
|
1001 | bb36d470 | bellard | } |
1002 | a594cfbf | bellard | return p->class_name;
|
1003 | a594cfbf | bellard | } |
1004 | a594cfbf | bellard | |
1005 | 9596ebb7 | pbrook | static void usb_info_device(int bus_num, int addr, int class_id, |
1006 | 9596ebb7 | pbrook | int vendor_id, int product_id, |
1007 | 9596ebb7 | pbrook | const char *product_name, |
1008 | 9596ebb7 | pbrook | int speed)
|
1009 | a594cfbf | bellard | { |
1010 | a594cfbf | bellard | const char *class_str, *speed_str; |
1011 | a594cfbf | bellard | |
1012 | a594cfbf | bellard | switch(speed) {
|
1013 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
1014 | 5fafdf24 | ths | speed_str = "1.5";
|
1015 | a594cfbf | bellard | break;
|
1016 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
1017 | 5fafdf24 | ths | speed_str = "12";
|
1018 | a594cfbf | bellard | break;
|
1019 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
1020 | 5fafdf24 | ths | speed_str = "480";
|
1021 | a594cfbf | bellard | break;
|
1022 | a594cfbf | bellard | default:
|
1023 | 5fafdf24 | ths | speed_str = "?";
|
1024 | a594cfbf | bellard | break;
|
1025 | a594cfbf | bellard | } |
1026 | a594cfbf | bellard | |
1027 | 5fafdf24 | ths | term_printf(" Device %d.%d, speed %s Mb/s\n",
|
1028 | a594cfbf | bellard | bus_num, addr, speed_str); |
1029 | a594cfbf | bellard | class_str = usb_class_str(class_id); |
1030 | 5fafdf24 | ths | if (class_str)
|
1031 | a594cfbf | bellard | term_printf(" %s:", class_str);
|
1032 | a594cfbf | bellard | else
|
1033 | a594cfbf | bellard | term_printf(" Class %02x:", class_id);
|
1034 | a594cfbf | bellard | term_printf(" USB device %04x:%04x", vendor_id, product_id);
|
1035 | a594cfbf | bellard | if (product_name[0] != '\0') |
1036 | a594cfbf | bellard | term_printf(", %s", product_name);
|
1037 | a594cfbf | bellard | term_printf("\n");
|
1038 | a594cfbf | bellard | } |
1039 | a594cfbf | bellard | |
1040 | 5fafdf24 | ths | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
1041 | a594cfbf | bellard | int class_id,
|
1042 | 5fafdf24 | ths | int vendor_id, int product_id, |
1043 | a594cfbf | bellard | const char *product_name, |
1044 | a594cfbf | bellard | int speed)
|
1045 | a594cfbf | bellard | { |
1046 | a594cfbf | bellard | usb_info_device(bus_num, addr, class_id, vendor_id, product_id, |
1047 | a594cfbf | bellard | product_name, speed); |
1048 | a594cfbf | bellard | return 0; |
1049 | a594cfbf | bellard | } |
1050 | a594cfbf | bellard | |
1051 | a594cfbf | bellard | void usb_host_info(void) |
1052 | a594cfbf | bellard | { |
1053 | a594cfbf | bellard | usb_host_scan(NULL, usb_host_info_device);
|
1054 | bb36d470 | bellard | } |
1055 | bb36d470 | bellard | |
1056 | bb36d470 | bellard | #else
|
1057 | bb36d470 | bellard | |
1058 | a594cfbf | bellard | void usb_host_info(void) |
1059 | a594cfbf | bellard | { |
1060 | a594cfbf | bellard | term_printf("USB host devices not supported\n");
|
1061 | a594cfbf | bellard | } |
1062 | a594cfbf | bellard | |
1063 | bb36d470 | bellard | /* XXX: modify configure to compile the right host driver */
|
1064 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname) |
1065 | bb36d470 | bellard | { |
1066 | bb36d470 | bellard | return NULL; |
1067 | bb36d470 | bellard | } |
1068 | bb36d470 | bellard | |
1069 | bb36d470 | bellard | #endif |