2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #if defined(__linux__)
28 #include <sys/ioctl.h>
29 #include <linux/usbdevice_fs.h>
30 #include <linux/version.h>
32 /* We redefine it to avoid version problems */
33 struct usb_ctrltransfer {
43 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
44 int vendor_id, int product_id,
45 const char *product_name, int speed);
46 static int usb_host_find_device(int *pbus_num, int *paddr,
51 #define USBDEVFS_PATH "/proc/bus/usb"
53 typedef struct USBHostDevice {
58 static void usb_host_handle_reset(USBDevice *dev)
61 USBHostDevice *s = (USBHostDevice *)dev;
62 /* USBDEVFS_RESET, but not the first time as it has already be
63 done by the host OS */
64 ioctl(s->fd, USBDEVFS_RESET);
68 static int usb_host_handle_control(USBDevice *dev,
75 USBHostDevice *s = (USBHostDevice *)dev;
76 struct usb_ctrltransfer ct;
79 if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
80 /* specific SET_ADDRESS support */
84 ct.bRequestType = request >> 8;
85 ct.bRequest = request;
91 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
105 static int usb_host_handle_data(USBDevice *dev, int pid,
107 uint8_t *data, int len)
109 USBHostDevice *s = (USBHostDevice *)dev;
110 struct usbdevfs_bulktransfer bt;
113 /* XXX: optimize and handle all data types by looking at the
115 if (pid == USB_TOKEN_IN)
121 ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
129 printf("handle_data: errno=%d\n", errno);
131 return USB_RET_STALL;
138 /* XXX: exclude high speed devices or implement EHCI */
139 USBDevice *usb_host_device_open(const char *devname)
141 int fd, interface, ret, i;
143 struct usbdevfs_connectinfo ci;
146 int descr_len, dev_descr_len, config_descr_len, nb_interfaces;
149 if (usb_host_find_device(&bus_num, &addr, devname) < 0)
152 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
154 fd = open(buf, O_RDWR);
160 /* read the config description */
161 descr_len = read(fd, descr, sizeof(descr));
162 if (descr_len <= 0) {
163 perror("read descr");
168 dev_descr_len = descr[0];
169 if (dev_descr_len > descr_len)
172 config_descr_len = descr[i];
173 if (i + config_descr_len > descr_len)
175 nb_interfaces = descr[i + 4];
176 if (nb_interfaces != 1) {
177 /* NOTE: currently we grab only one interface */
178 fprintf(stderr, "usb_host: only one interface supported\n");
182 #ifdef USBDEVFS_DISCONNECT
183 /* earlier Linux 2.4 do not support that */
185 struct usbdevfs_ioctl ctrl;
186 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
188 ret = ioctl(fd, USBDEVFS_IOCTL, &ctrl);
189 if (ret < 0 && errno != ENODATA) {
190 perror("USBDEVFS_DISCONNECT");
196 /* XXX: only grab if all interfaces are free */
198 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface);
200 if (errno == EBUSY) {
201 fprintf(stderr, "usb_host: device already grabbed\n");
203 perror("USBDEVFS_CLAIMINTERFACE");
210 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
212 perror("USBDEVFS_CONNECTINFO");
217 printf("host USB device %d.%d grabbed\n", bus_num, addr);
220 dev = qemu_mallocz(sizeof(USBHostDevice));
225 dev->dev.speed = USB_SPEED_LOW;
227 dev->dev.speed = USB_SPEED_HIGH;
228 dev->dev.handle_packet = usb_generic_handle_packet;
230 dev->dev.handle_reset = usb_host_handle_reset;
231 dev->dev.handle_control = usb_host_handle_control;
232 dev->dev.handle_data = usb_host_handle_data;
233 return (USBDevice *)dev;
236 static int get_tag_value(char *buf, int buf_size,
237 const char *str, const char *tag,
238 const char *stopchars)
242 p = strstr(str, tag);
249 while (*p != '\0' && !strchr(stopchars, *p)) {
250 if ((q - buf) < (buf_size - 1))
258 static int usb_host_scan(void *opaque, USBScanFunc *func)
263 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
265 char product_name[512];
267 f = fopen(USBDEVFS_PATH "/devices", "r");
269 term_printf("Could not open %s\n", USBDEVFS_PATH "/devices");
273 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
276 if (fgets(line, sizeof(line), f) == NULL)
278 if (strlen(line) > 0)
279 line[strlen(line) - 1] = '\0';
280 if (line[0] == 'T' && line[1] == ':') {
281 if (device_count && (vendor_id || product_id)) {
282 /* New device. Add the previously discovered device. */
283 ret = func(opaque, bus_num, addr, class_id, vendor_id,
284 product_id, product_name, speed);
288 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
291 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
294 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
296 if (!strcmp(buf, "480"))
297 speed = USB_SPEED_HIGH;
298 else if (!strcmp(buf, "1.5"))
299 speed = USB_SPEED_LOW;
301 speed = USB_SPEED_FULL;
302 product_name[0] = '\0';
307 } else if (line[0] == 'P' && line[1] == ':') {
308 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
310 vendor_id = strtoul(buf, NULL, 16);
311 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
313 product_id = strtoul(buf, NULL, 16);
314 } else if (line[0] == 'S' && line[1] == ':') {
315 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
317 pstrcpy(product_name, sizeof(product_name), buf);
318 } else if (line[0] == 'D' && line[1] == ':') {
319 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
321 class_id = strtoul(buf, NULL, 16);
325 if (device_count && (vendor_id || product_id)) {
326 /* Add the last device. */
327 ret = func(opaque, bus_num, addr, class_id, vendor_id,
328 product_id, product_name, speed);
335 typedef struct FindDeviceState {
342 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
344 int vendor_id, int product_id,
345 const char *product_name, int speed)
347 FindDeviceState *s = opaque;
348 if (vendor_id == s->vendor_id &&
349 product_id == s->product_id) {
350 s->bus_num = bus_num;
359 'bus.addr' (decimal numbers) or
360 'vendor_id:product_id' (hexa numbers) */
361 static int usb_host_find_device(int *pbus_num, int *paddr,
368 p = strchr(devname, '.');
370 *pbus_num = strtoul(devname, NULL, 0);
371 *paddr = strtoul(p + 1, NULL, 0);
374 p = strchr(devname, ':');
376 fs.vendor_id = strtoul(devname, NULL, 16);
377 fs.product_id = strtoul(p + 1, NULL, 16);
378 ret = usb_host_scan(&fs, usb_host_find_device_scan);
380 *pbus_num = fs.bus_num;
388 /**********************/
389 /* USB host device info */
391 struct usb_class_info {
393 const char *class_name;
396 static const struct usb_class_info usb_class_info[] = {
397 { USB_CLASS_AUDIO, "Audio"},
398 { USB_CLASS_COMM, "Communication"},
399 { USB_CLASS_HID, "HID"},
400 { USB_CLASS_HUB, "Hub" },
401 { USB_CLASS_PHYSICAL, "Physical" },
402 { USB_CLASS_PRINTER, "Printer" },
403 { USB_CLASS_MASS_STORAGE, "Storage" },
404 { USB_CLASS_CDC_DATA, "Data" },
405 { USB_CLASS_APP_SPEC, "Application Specific" },
406 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
407 { USB_CLASS_STILL_IMAGE, "Still Image" },
408 { USB_CLASS_CSCID, "Smart Card" },
409 { USB_CLASS_CONTENT_SEC, "Content Security" },
413 static const char *usb_class_str(uint8_t class)
415 const struct usb_class_info *p;
416 for(p = usb_class_info; p->class != -1; p++) {
417 if (p->class == class)
420 return p->class_name;
423 void usb_info_device(int bus_num, int addr, int class_id,
424 int vendor_id, int product_id,
425 const char *product_name,
428 const char *class_str, *speed_str;
445 term_printf(" Device %d.%d, speed %s Mb/s\n",
446 bus_num, addr, speed_str);
447 class_str = usb_class_str(class_id);
449 term_printf(" %s:", class_str);
451 term_printf(" Class %02x:", class_id);
452 term_printf(" USB device %04x:%04x", vendor_id, product_id);
453 if (product_name[0] != '\0')
454 term_printf(", %s", product_name);
458 static int usb_host_info_device(void *opaque, int bus_num, int addr,
460 int vendor_id, int product_id,
461 const char *product_name,
464 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
465 product_name, speed);
469 void usb_host_info(void)
471 usb_host_scan(NULL, usb_host_info_device);
476 void usb_host_info(void)
478 term_printf("USB host devices not supported\n");
481 /* XXX: modify configure to compile the right host driver */
482 USBDevice *usb_host_device_open(const char *devname)