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