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 */
184 ret
= ioctl(fd
, USBDEVFS_DISCONNECT
);
185 if (ret
< 0 && errno
!= ENODATA
) {
186 perror("USBDEVFS_DISCONNECT");
191 /* XXX: only grab if all interfaces are free */
193 ret
= ioctl(fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
195 if (errno
== EBUSY
) {
196 fprintf(stderr
, "usb_host: device already grabbed\n");
198 perror("USBDEVFS_CLAIMINTERFACE");
205 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
207 perror("USBDEVFS_CONNECTINFO");
212 printf("host USB device %d.%d grabbed\n", bus_num
, addr
);
215 dev
= qemu_mallocz(sizeof(USBHostDevice
));
220 dev
->dev
.speed
= USB_SPEED_LOW
;
222 dev
->dev
.speed
= USB_SPEED_HIGH
;
223 dev
->dev
.handle_packet
= usb_generic_handle_packet
;
225 dev
->dev
.handle_reset
= usb_host_handle_reset
;
226 dev
->dev
.handle_control
= usb_host_handle_control
;
227 dev
->dev
.handle_data
= usb_host_handle_data
;
228 return (USBDevice
*)dev
;
231 static int get_tag_value(char *buf
, int buf_size
,
232 const char *str
, const char *tag
,
233 const char *stopchars
)
237 p
= strstr(str
, tag
);
244 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
245 if ((q
- buf
) < (buf_size
- 1))
253 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
258 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
260 char product_name
[512];
262 f
= fopen(USBDEVFS_PATH
"/devices", "r");
264 term_printf("Could not open %s\n", USBDEVFS_PATH
"/devices");
268 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
271 if (fgets(line
, sizeof(line
), f
) == NULL
)
273 if (strlen(line
) > 0)
274 line
[strlen(line
) - 1] = '\0';
275 if (line
[0] == 'T' && line
[1] == ':') {
277 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
278 product_id
, product_name
, speed
);
282 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
285 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
288 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
290 if (!strcmp(buf
, "480"))
291 speed
= USB_SPEED_HIGH
;
292 else if (!strcmp(buf
, "1.5"))
293 speed
= USB_SPEED_LOW
;
295 speed
= USB_SPEED_FULL
;
296 product_name
[0] = '\0';
301 } else if (line
[0] == 'P' && line
[1] == ':') {
302 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
304 vendor_id
= strtoul(buf
, NULL
, 16);
305 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
307 product_id
= strtoul(buf
, NULL
, 16);
308 } else if (line
[0] == 'S' && line
[1] == ':') {
309 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
311 pstrcpy(product_name
, sizeof(product_name
), buf
);
312 } else if (line
[0] == 'D' && line
[1] == ':') {
313 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
315 class_id
= strtoul(buf
, NULL
, 16);
320 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
321 product_id
, product_name
, speed
);
328 typedef struct FindDeviceState
{
335 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
337 int vendor_id
, int product_id
,
338 const char *product_name
, int speed
)
340 FindDeviceState
*s
= opaque
;
341 if (vendor_id
== s
->vendor_id
&&
342 product_id
== s
->product_id
) {
343 s
->bus_num
= bus_num
;
352 'bus.addr' (decimal numbers) or
353 'vendor_id:product_id' (hexa numbers) */
354 static int usb_host_find_device(int *pbus_num
, int *paddr
,
361 p
= strchr(devname
, '.');
363 *pbus_num
= strtoul(devname
, NULL
, 0);
364 *paddr
= strtoul(p
+ 1, NULL
, 0);
367 p
= strchr(devname
, ':');
369 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
370 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
371 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
373 *pbus_num
= fs
.bus_num
;
381 /**********************/
382 /* USB host device info */
384 struct usb_class_info
{
386 const char *class_name
;
389 static const struct usb_class_info usb_class_info
[] = {
390 { USB_CLASS_AUDIO
, "Audio"},
391 { USB_CLASS_COMM
, "Communication"},
392 { USB_CLASS_HID
, "HID"},
393 { USB_CLASS_HUB
, "Hub" },
394 { USB_CLASS_PHYSICAL
, "Physical" },
395 { USB_CLASS_PRINTER
, "Printer" },
396 { USB_CLASS_MASS_STORAGE
, "Storage" },
397 { USB_CLASS_CDC_DATA
, "Data" },
398 { USB_CLASS_APP_SPEC
, "Application Specific" },
399 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
400 { USB_CLASS_STILL_IMAGE
, "Still Image" },
401 { USB_CLASS_CSCID
, "Smart Card" },
402 { USB_CLASS_CONTENT_SEC
, "Content Security" },
406 static const char *usb_class_str(uint8_t class)
408 const struct usb_class_info
*p
;
409 for(p
= usb_class_info
; p
->class != -1; p
++) {
410 if (p
->class == class)
413 return p
->class_name
;
416 void usb_info_device(int bus_num
, int addr
, int class_id
,
417 int vendor_id
, int product_id
,
418 const char *product_name
,
421 const char *class_str
, *speed_str
;
438 term_printf(" Device %d.%d, speed %s Mb/s\n",
439 bus_num
, addr
, speed_str
);
440 class_str
= usb_class_str(class_id
);
442 term_printf(" %s:", class_str
);
444 term_printf(" Class %02x:", class_id
);
445 term_printf(" USB device %04x:%04x", vendor_id
, product_id
);
446 if (product_name
[0] != '\0')
447 term_printf(", %s", product_name
);
451 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
453 int vendor_id
, int product_id
,
454 const char *product_name
,
457 usb_info_device(bus_num
, addr
, class_id
, vendor_id
, product_id
,
458 product_name
, speed
);
462 void usb_host_info(void)
464 usb_host_scan(NULL
, usb_host_info_device
);
469 void usb_host_info(void)
471 term_printf("USB host devices not supported\n");
474 /* XXX: modify configure to compile the right host driver */
475 USBDevice
*usb_host_device_open(const char *devname
)