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