2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer
{
57 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, char *port
,
58 int class_id
, int vendor_id
, int product_id
,
59 const char *product_name
, int speed
);
64 #define DPRINTF printf
69 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
71 #define USBPROCBUS_PATH "/proc/bus/usb"
72 #define PRODUCT_NAME_SZ 32
73 #define MAX_ENDPOINTS 15
74 #define MAX_PORTLEN 16
75 #define USBDEVBUS_PATH "/dev/bus/usb"
76 #define USBSYSBUS_PATH "/sys/bus/usb"
78 static char *usb_host_device_path
;
85 static int usb_fs_type
;
87 /* endpoint association data */
88 #define ISO_FRAME_DESC_PER_URB 32
89 #define INVALID_EP_TYPE 255
91 /* devio.c limits single requests to 16k */
92 #define MAX_USBFS_BUFFER_SIZE 16384
94 typedef struct AsyncURB AsyncURB
;
107 struct USBAutoFilter
{
115 typedef struct USBHostDevice
{
124 uint32_t iso_urb_count
;
127 struct endp_data endp_table
[MAX_ENDPOINTS
];
128 QLIST_HEAD(, AsyncURB
) aurbs
;
130 /* Host side address */
133 char port
[MAX_PORTLEN
];
134 struct USBAutoFilter match
;
136 QTAILQ_ENTRY(USBHostDevice
) next
;
139 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
141 static int usb_host_close(USBHostDevice
*dev
);
142 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
143 static void usb_host_auto_check(void *unused
);
144 static int usb_host_read_file(char *line
, size_t line_size
,
145 const char *device_file
, const char *device_name
);
147 static struct endp_data
*get_endp(USBHostDevice
*s
, int ep
)
149 return s
->endp_table
+ ep
- 1;
152 static int is_isoc(USBHostDevice
*s
, int ep
)
154 return get_endp(s
, ep
)->type
== USBDEVFS_URB_TYPE_ISO
;
157 static int is_valid(USBHostDevice
*s
, int ep
)
159 return get_endp(s
, ep
)->type
!= INVALID_EP_TYPE
;
162 static int is_halted(USBHostDevice
*s
, int ep
)
164 return get_endp(s
, ep
)->halted
;
167 static void clear_halt(USBHostDevice
*s
, int ep
)
169 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
170 get_endp(s
, ep
)->halted
= 0;
173 static void set_halt(USBHostDevice
*s
, int ep
)
175 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
176 get_endp(s
, ep
)->halted
= 1;
179 static int is_iso_started(USBHostDevice
*s
, int ep
)
181 return get_endp(s
, ep
)->iso_started
;
184 static void clear_iso_started(USBHostDevice
*s
, int ep
)
186 trace_usb_host_ep_stop_iso(s
->bus_num
, s
->addr
, ep
);
187 get_endp(s
, ep
)->iso_started
= 0;
190 static void set_iso_started(USBHostDevice
*s
, int ep
)
192 struct endp_data
*e
= get_endp(s
, ep
);
194 trace_usb_host_ep_start_iso(s
->bus_num
, s
->addr
, ep
);
195 if (!e
->iso_started
) {
201 static int change_iso_inflight(USBHostDevice
*s
, int ep
, int value
)
203 struct endp_data
*e
= get_endp(s
, ep
);
205 e
->inflight
+= value
;
209 static void set_iso_urb(USBHostDevice
*s
, int ep
, AsyncURB
*iso_urb
)
211 get_endp(s
, ep
)->iso_urb
= iso_urb
;
214 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int ep
)
216 return get_endp(s
, ep
)->iso_urb
;
219 static void set_iso_urb_idx(USBHostDevice
*s
, int ep
, int i
)
221 get_endp(s
, ep
)->iso_urb_idx
= i
;
224 static int get_iso_urb_idx(USBHostDevice
*s
, int ep
)
226 return get_endp(s
, ep
)->iso_urb_idx
;
229 static void set_iso_buffer_used(USBHostDevice
*s
, int ep
, int i
)
231 get_endp(s
, ep
)->iso_buffer_used
= i
;
234 static int get_iso_buffer_used(USBHostDevice
*s
, int ep
)
236 return get_endp(s
, ep
)->iso_buffer_used
;
239 static void set_max_packet_size(USBHostDevice
*s
, int ep
, uint8_t *descriptor
)
241 int raw
= descriptor
[4] + (descriptor
[5] << 8);
242 int size
, microframes
;
245 switch ((raw
>> 11) & 3) {
246 case 1: microframes
= 2; break;
247 case 2: microframes
= 3; break;
248 default: microframes
= 1; break;
250 get_endp(s
, ep
)->max_packet_size
= size
* microframes
;
253 static int get_max_packet_size(USBHostDevice
*s
, int ep
)
255 return get_endp(s
, ep
)->max_packet_size
;
260 * We always allocate iso packet descriptors even for bulk transfers
261 * to simplify allocation and casts.
265 struct usbdevfs_urb urb
;
266 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
268 QLIST_ENTRY(AsyncURB
) next
;
270 /* For regular async urbs */
272 int more
; /* large transfer, more urbs follow */
274 /* For buffered iso handling */
275 int iso_frame_idx
; /* -1 means in flight */
278 static AsyncURB
*async_alloc(USBHostDevice
*s
)
280 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
282 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
286 static void async_free(AsyncURB
*aurb
)
288 QLIST_REMOVE(aurb
, next
);
292 static void do_disconnect(USBHostDevice
*s
)
295 usb_host_auto_check(NULL
);
298 static void async_complete(void *opaque
)
300 USBHostDevice
*s
= opaque
;
307 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
309 if (errno
== EAGAIN
) {
311 fprintf(stderr
, "husb: %d iso urbs finished at once\n", urbs
);
315 if (errno
== ENODEV
&& !s
->closing
) {
316 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
321 perror("USBDEVFS_REAPURBNDELAY");
325 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
326 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
328 /* If this is a buffered iso urb mark it as complete and don't do
329 anything else (it is handled further in usb_host_handle_iso_data) */
330 if (aurb
->iso_frame_idx
== -1) {
332 if (aurb
->urb
.status
== -EPIPE
) {
333 set_halt(s
, aurb
->urb
.endpoint
& 0xf);
335 aurb
->iso_frame_idx
= 0;
337 inflight
= change_iso_inflight(s
, aurb
->urb
.endpoint
& 0xf, -1);
338 if (inflight
== 0 && is_iso_started(s
, aurb
->urb
.endpoint
& 0xf)) {
339 fprintf(stderr
, "husb: out of buffers for iso stream\n");
345 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
346 aurb
->urb
.actual_length
, aurb
->more
);
349 switch (aurb
->urb
.status
) {
351 p
->result
+= aurb
->urb
.actual_length
;
355 set_halt(s
, p
->devep
);
356 p
->result
= USB_RET_STALL
;
360 p
->result
= USB_RET_NAK
;
364 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
365 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
366 usb_generic_async_ctrl_complete(&s
->dev
, p
);
367 } else if (!aurb
->more
) {
368 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
369 usb_packet_complete(&s
->dev
, p
);
377 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
379 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
382 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
383 if (p
!= aurb
->packet
) {
387 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p
, aurb
);
389 /* Mark it as dead (see async_complete above) */
392 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
394 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
399 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
401 const char *op
= NULL
;
402 int dev_descr_len
, config_descr_len
;
403 int interface
, nb_interfaces
;
406 if (configuration
== 0) /* address state - ignore */
409 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
412 dev_descr_len
= dev
->descr
[0];
413 if (dev_descr_len
> dev
->descr_len
) {
414 fprintf(stderr
, "husb: update iface failed. descr too short\n");
419 while (i
< dev
->descr_len
) {
420 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
422 dev
->descr
[i
], dev
->descr
[i
+1]);
424 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
428 config_descr_len
= dev
->descr
[i
];
430 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
432 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
433 configuration
= dev
->descr
[i
+ 5];
437 i
+= config_descr_len
;
440 if (i
>= dev
->descr_len
) {
442 "husb: update iface failed. no matching configuration\n");
445 nb_interfaces
= dev
->descr
[i
+ 4];
447 #ifdef USBDEVFS_DISCONNECT
448 /* earlier Linux 2.4 do not support that */
450 struct usbdevfs_ioctl ctrl
;
451 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
452 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
453 ctrl
.ifno
= interface
;
455 op
= "USBDEVFS_DISCONNECT";
456 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
457 if (ret
< 0 && errno
!= ENODATA
) {
464 /* XXX: only grab if all interfaces are free */
465 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
466 op
= "USBDEVFS_CLAIMINTERFACE";
467 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
473 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
474 nb_interfaces
, configuration
);
476 dev
->ninterfaces
= nb_interfaces
;
477 dev
->configuration
= configuration
;
481 if (errno
== ENODEV
) {
488 static int usb_host_release_interfaces(USBHostDevice
*s
)
492 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
494 for (i
= 0; i
< s
->ninterfaces
; i
++) {
495 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
497 perror("USBDEVFS_RELEASEINTERFACE");
504 static void usb_host_handle_reset(USBDevice
*dev
)
506 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
508 trace_usb_host_reset(s
->bus_num
, s
->addr
);
510 ioctl(s
->fd
, USBDEVFS_RESET
);
512 usb_host_claim_interfaces(s
, s
->configuration
);
515 static void usb_host_handle_destroy(USBDevice
*dev
)
517 USBHostDevice
*s
= (USBHostDevice
*)dev
;
520 QTAILQ_REMOVE(&hostdevs
, s
, next
);
521 qemu_remove_exit_notifier(&s
->exit
);
524 static int usb_linux_update_endp_table(USBHostDevice
*s
);
526 /* iso data is special, we need to keep enough urbs in flight to make sure
527 that the controller never runs out of them, otherwise the device will
528 likely suffer a buffer underrun / overrun. */
529 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, uint8_t ep
, int in
)
532 int i
, j
, len
= get_max_packet_size(s
, ep
);
534 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
535 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
536 aurb
[i
].urb
.endpoint
= ep
;
537 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
538 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
539 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
540 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
541 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
542 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
543 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
545 aurb
[i
].urb
.endpoint
|= 0x80;
546 /* Mark as fully consumed (idle) */
547 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
550 set_iso_urb(s
, ep
, aurb
);
555 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, uint8_t ep
)
558 int i
, ret
, killed
= 0, free
= 1;
560 aurb
= get_iso_urb(s
, ep
);
565 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
567 if (aurb
[i
].iso_frame_idx
== -1) {
568 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
570 perror("USBDEVFS_DISCARDURB");
578 /* Make sure any urbs we've killed are reaped before we free them */
583 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
584 g_free(aurb
[i
].urb
.buffer
);
590 printf("husb: leaking iso urbs because of discard failure\n");
591 set_iso_urb(s
, ep
, NULL
);
592 set_iso_urb_idx(s
, ep
, 0);
593 clear_iso_started(s
, ep
);
596 static int urb_status_to_usb_ret(int status
)
600 return USB_RET_STALL
;
606 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
609 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
612 max_packet_size
= get_max_packet_size(s
, p
->devep
);
613 if (max_packet_size
== 0)
616 aurb
= get_iso_urb(s
, p
->devep
);
618 aurb
= usb_host_alloc_iso(s
, p
->devep
, in
);
621 i
= get_iso_urb_idx(s
, p
->devep
);
622 j
= aurb
[i
].iso_frame_idx
;
623 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
625 /* Check urb status */
626 if (aurb
[i
].urb
.status
) {
627 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
628 /* Move to the next urb */
629 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
630 /* Check frame status */
631 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
632 len
= urb_status_to_usb_ret(
633 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
634 /* Check the frame fits */
635 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
637 printf("husb: received iso data is larger then packet\n");
639 /* All good copy data over */
641 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
642 buf
= aurb
[i
].urb
.buffer
+
643 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
644 usb_packet_copy(p
, buf
, len
);
648 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->devep
);
650 /* Check the frame fits */
651 if (len
> max_packet_size
) {
652 printf("husb: send iso data is larger then max packet size\n");
656 /* All good copy data over */
657 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
658 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
660 set_iso_buffer_used(s
, p
->devep
, offset
);
662 /* Start the stream once we have buffered enough data */
663 if (!is_iso_started(s
, p
->devep
) && i
== 1 && j
== 8) {
664 set_iso_started(s
, p
->devep
);
667 aurb
[i
].iso_frame_idx
++;
668 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
669 i
= (i
+ 1) % s
->iso_urb_count
;
670 set_iso_urb_idx(s
, p
->devep
, i
);
674 set_iso_started(s
, p
->devep
);
676 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
680 if (is_iso_started(s
, p
->devep
)) {
681 /* (Re)-submit all fully consumed / filled urbs */
682 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
683 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
684 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
686 perror("USBDEVFS_SUBMITURB");
687 if (!in
|| len
== 0) {
699 aurb
[i
].iso_frame_idx
= -1;
700 change_iso_inflight(s
, p
->devep
, +1);
708 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
710 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
711 struct usbdevfs_urb
*urb
;
713 int ret
, rem
, prem
, v
;
717 trace_usb_host_req_data(s
->bus_num
, s
->addr
,
718 p
->pid
== USB_TOKEN_IN
,
719 p
->devep
, p
->iov
.size
);
721 if (!is_valid(s
, p
->devep
)) {
722 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
726 if (p
->pid
== USB_TOKEN_IN
) {
727 ep
= p
->devep
| 0x80;
732 if (is_halted(s
, p
->devep
)) {
733 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &ep
);
735 perror("USBDEVFS_CLEAR_HALT");
736 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
739 clear_halt(s
, p
->devep
);
742 if (is_isoc(s
, p
->devep
)) {
743 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
747 prem
= p
->iov
.iov
[v
].iov_len
;
748 pbuf
= p
->iov
.iov
[v
].iov_base
;
753 assert(v
< p
->iov
.niov
);
754 prem
= p
->iov
.iov
[v
].iov_len
;
755 pbuf
= p
->iov
.iov
[v
].iov_base
;
758 aurb
= async_alloc(s
);
763 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
764 urb
->usercontext
= s
;
766 urb
->buffer_length
= prem
;
768 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
769 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
771 pbuf
+= urb
->buffer_length
;
772 prem
-= urb
->buffer_length
;
773 rem
-= urb
->buffer_length
;
778 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
779 urb
->buffer_length
, aurb
->more
);
780 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
782 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
783 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
786 perror("USBDEVFS_SUBMITURB");
791 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
795 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_STALL
);
796 return USB_RET_STALL
;
801 return USB_RET_ASYNC
;
804 static int ctrl_error(void)
806 if (errno
== ETIMEDOUT
) {
809 return USB_RET_STALL
;
813 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
815 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
820 static int usb_host_set_config(USBHostDevice
*s
, int config
)
822 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
824 usb_host_release_interfaces(s
);
826 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
828 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
833 usb_host_claim_interfaces(s
, config
);
837 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
839 struct usbdevfs_setinterface si
;
842 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
844 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
846 usb_host_stop_n_free_iso(s
, i
);
850 si
.interface
= iface
;
852 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
854 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
855 iface
, alt
, ret
, errno
);
860 usb_linux_update_endp_table(s
);
864 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
865 int request
, int value
, int index
, int length
, uint8_t *data
)
867 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
868 struct usbdevfs_urb
*urb
;
873 * Process certain standard device requests.
874 * These are infrequent and are processed synchronously.
877 /* Note request is (bRequestType << 8) | bRequest */
878 trace_usb_host_req_control(s
->bus_num
, s
->addr
, request
, value
, index
);
881 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
882 return usb_host_set_address(s
, value
);
884 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
885 return usb_host_set_config(s
, value
& 0xff);
887 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
888 return usb_host_set_interface(s
, index
, value
);
891 /* The rest are asynchronous */
893 if (length
> sizeof(dev
->data_buf
)) {
894 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
895 length
, sizeof(dev
->data_buf
));
896 return USB_RET_STALL
;
899 aurb
= async_alloc(s
);
903 * Setup ctrl transfer.
905 * s->ctrl is laid out such that data buffer immediately follows
906 * 'req' struct which is exactly what usbdevfs expects.
910 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
911 urb
->endpoint
= p
->devep
;
913 urb
->buffer
= &dev
->setup_buf
;
914 urb
->buffer_length
= length
+ 8;
916 urb
->usercontext
= s
;
918 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
919 urb
->buffer_length
, aurb
->more
);
920 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
922 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
925 DPRINTF("husb: submit failed. errno %d\n", errno
);
933 return USB_RET_STALL
;
937 return USB_RET_ASYNC
;
940 static int usb_linux_get_configuration(USBHostDevice
*s
)
942 uint8_t configuration
;
943 struct usb_ctrltransfer ct
;
946 if (usb_fs_type
== USB_FS_SYS
) {
947 char device_name
[32], line
[1024];
950 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
952 if (!usb_host_read_file(line
, sizeof(line
), "bConfigurationValue",
956 if (sscanf(line
, "%d", &configuration
) != 1) {
959 return configuration
;
963 ct
.bRequestType
= USB_DIR_IN
;
964 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
968 ct
.data
= &configuration
;
971 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
973 perror("usb_linux_get_configuration");
977 /* in address state */
978 if (configuration
== 0) {
982 return configuration
;
985 static uint8_t usb_linux_get_alt_setting(USBHostDevice
*s
,
986 uint8_t configuration
, uint8_t interface
)
989 struct usb_ctrltransfer ct
;
992 if (usb_fs_type
== USB_FS_SYS
) {
993 char device_name
[64], line
[1024];
996 sprintf(device_name
, "%d-%s:%d.%d", s
->bus_num
, s
->port
,
997 (int)configuration
, (int)interface
);
999 if (!usb_host_read_file(line
, sizeof(line
), "bAlternateSetting",
1003 if (sscanf(line
, "%d", &alt_setting
) != 1) {
1010 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
1011 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
1013 ct
.wIndex
= interface
;
1015 ct
.data
= &alt_setting
;
1017 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
1019 /* Assume alt 0 on error */
1026 /* returns 1 on problem encountered or 0 for success */
1027 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1029 uint8_t *descriptors
;
1030 uint8_t devep
, type
, configuration
, alt_interface
;
1031 int interface
, length
, i
;
1033 for (i
= 0; i
< MAX_ENDPOINTS
; i
++)
1034 s
->endp_table
[i
].type
= INVALID_EP_TYPE
;
1036 i
= usb_linux_get_configuration(s
);
1041 /* get the desired configuration, interface, and endpoint descriptors
1042 * from device description */
1043 descriptors
= &s
->descr
[18];
1044 length
= s
->descr_len
- 18;
1047 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
1048 descriptors
[i
+ 5] != configuration
) {
1049 DPRINTF("invalid descriptor data - configuration\n");
1052 i
+= descriptors
[i
];
1054 while (i
< length
) {
1055 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
1056 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
1057 descriptors
[i
+ 4] == 0)) {
1058 i
+= descriptors
[i
];
1062 interface
= descriptors
[i
+ 2];
1063 alt_interface
= usb_linux_get_alt_setting(s
, configuration
, interface
);
1065 /* the current interface descriptor is the active interface
1066 * and has endpoints */
1067 if (descriptors
[i
+ 3] != alt_interface
) {
1068 i
+= descriptors
[i
];
1072 /* advance to the endpoints */
1073 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
1074 i
+= descriptors
[i
];
1080 while (i
< length
) {
1081 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
1085 devep
= descriptors
[i
+ 2];
1086 if ((devep
& 0x0f) == 0) {
1087 fprintf(stderr
, "usb-linux: invalid ep descriptor, ep == 0\n");
1091 switch (descriptors
[i
+ 3] & 0x3) {
1093 type
= USBDEVFS_URB_TYPE_CONTROL
;
1096 type
= USBDEVFS_URB_TYPE_ISO
;
1097 set_max_packet_size(s
, (devep
& 0xf), descriptors
+ i
);
1100 type
= USBDEVFS_URB_TYPE_BULK
;
1103 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
1106 DPRINTF("usb_host: malformed endpoint type\n");
1107 type
= USBDEVFS_URB_TYPE_BULK
;
1109 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
1110 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
1112 i
+= descriptors
[i
];
1119 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1120 * this function assumes this is safe, if:
1121 * 1) There are no isoc endpoints
1122 * 2) There are no interrupt endpoints with a max_packet_size > 64
1123 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1124 * usb1 compatible, but in practice this seems to work fine.
1126 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1131 * usb_linux_update_endp_table only registers info about ep in the current
1132 * interface altsettings, so we need to parse the descriptors again.
1134 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1135 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1136 switch (dev
->descr
[i
+ 3] & 0x3) {
1137 case 0x00: /* CONTROL */
1139 case 0x01: /* ISO */
1141 case 0x02: /* BULK */
1143 case 0x03: /* INTERRUPT */
1144 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1145 if (packet_size
> 64)
1154 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1155 int addr
, char *port
, const char *prod_name
, int speed
)
1160 trace_usb_host_open_started(bus_num
, addr
);
1162 if (dev
->fd
!= -1) {
1166 if (!usb_host_device_path
) {
1167 perror("husb: USB Host Device Path not set");
1170 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
1172 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
1177 DPRINTF("husb: opened %s\n", buf
);
1179 dev
->bus_num
= bus_num
;
1181 strcpy(dev
->port
, port
);
1184 /* read the device description */
1185 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1186 if (dev
->descr_len
<= 0) {
1187 perror("husb: reading device data failed");
1194 printf("=== begin dumping device descriptor data ===\n");
1195 for (x
= 0; x
< dev
->descr_len
; x
++) {
1196 printf("%02x ", dev
->descr
[x
]);
1198 printf("\n=== end dumping device descriptor data ===\n");
1204 * Initial configuration is -1 which makes us claim first
1205 * available config. We used to start with 1, which does not
1206 * always work. I've seen devices where first config starts
1209 if (!usb_host_claim_interfaces(dev
, -1)) {
1213 ret
= usb_linux_update_endp_table(dev
);
1219 struct usbdevfs_connectinfo ci
;
1221 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1223 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1228 speed
= USB_SPEED_LOW
;
1230 speed
= USB_SPEED_HIGH
;
1233 dev
->dev
.speed
= speed
;
1234 dev
->dev
.speedmask
= (1 << speed
);
1235 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1236 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1239 trace_usb_host_open_success(bus_num
, addr
);
1241 if (!prod_name
|| prod_name
[0] == '\0') {
1242 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1243 "host:%d.%d", bus_num
, addr
);
1245 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1249 ret
= usb_device_attach(&dev
->dev
);
1254 /* USB devio uses 'write' flag to check for async completions */
1255 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1260 trace_usb_host_open_failure(bus_num
, addr
);
1261 if (dev
->fd
!= -1) {
1268 static int usb_host_close(USBHostDevice
*dev
)
1272 if (dev
->fd
== -1 || !dev
->dev
.attached
) {
1276 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1278 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1280 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
1281 if (is_isoc(dev
, i
)) {
1282 usb_host_stop_n_free_iso(dev
, i
);
1285 async_complete(dev
);
1287 usb_device_detach(&dev
->dev
);
1288 ioctl(dev
->fd
, USBDEVFS_RESET
);
1294 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1296 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1299 ioctl(s
->fd
, USBDEVFS_RESET
);
1303 static int usb_host_initfn(USBDevice
*dev
)
1305 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1307 dev
->auto_attach
= 0;
1309 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1310 s
->exit
.notify
= usb_host_exit_notifier
;
1311 qemu_add_exit_notifier(&s
->exit
);
1312 usb_host_auto_check(NULL
);
1316 static struct USBDeviceInfo usb_host_dev_info
= {
1317 .product_desc
= "USB Host Device",
1318 .qdev
.name
= "usb-host",
1319 .qdev
.size
= sizeof(USBHostDevice
),
1320 .init
= usb_host_initfn
,
1321 .handle_packet
= usb_generic_handle_packet
,
1322 .cancel_packet
= usb_host_async_cancel
,
1323 .handle_data
= usb_host_handle_data
,
1324 .handle_control
= usb_host_handle_control
,
1325 .handle_reset
= usb_host_handle_reset
,
1326 .handle_destroy
= usb_host_handle_destroy
,
1327 .usbdevice_name
= "host",
1328 .usbdevice_init
= usb_host_device_open
,
1329 .qdev
.props
= (Property
[]) {
1330 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1331 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1332 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1333 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1334 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1335 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1336 DEFINE_PROP_END_OF_LIST(),
1340 static void usb_host_register_devices(void)
1342 usb_qdev_register(&usb_host_dev_info
);
1344 device_init(usb_host_register_devices
)
1346 USBDevice
*usb_host_device_open(const char *devname
)
1348 struct USBAutoFilter filter
;
1352 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1354 if (strstr(devname
, "auto:")) {
1355 if (parse_filter(devname
, &filter
) < 0) {
1359 if ((p
= strchr(devname
, '.'))) {
1360 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1361 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1362 filter
.vendor_id
= 0;
1363 filter
.product_id
= 0;
1364 } else if ((p
= strchr(devname
, ':'))) {
1367 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1368 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1374 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1375 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1376 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1377 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1378 qdev_init_nofail(&dev
->qdev
);
1382 qdev_free(&dev
->qdev
);
1386 int usb_host_device_close(const char *devname
)
1389 char product_name
[PRODUCT_NAME_SZ
];
1393 if (strstr(devname
, "auto:")) {
1394 return usb_host_auto_del(devname
);
1396 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1397 sizeof(product_name
), devname
) < 0) {
1400 s
= hostdev_find(bus_num
, addr
);
1402 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1410 static int get_tag_value(char *buf
, int buf_size
,
1411 const char *str
, const char *tag
,
1412 const char *stopchars
)
1416 p
= strstr(str
, tag
);
1421 while (qemu_isspace(*p
)) {
1425 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1426 if ((q
- buf
) < (buf_size
- 1)) {
1436 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1437 * host's USB devices. This is legacy support since many distributions
1438 * are moving to /sys/bus/usb
1440 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1445 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1446 char product_name
[512];
1449 if (!usb_host_device_path
) {
1450 perror("husb: USB Host Device Path not set");
1453 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1454 f
= fopen(line
, "r");
1456 perror("husb: cannot open devices file");
1461 bus_num
= addr
= class_id
= product_id
= vendor_id
= 0;
1462 speed
= -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1464 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1467 if (strlen(line
) > 0) {
1468 line
[strlen(line
) - 1] = '\0';
1470 if (line
[0] == 'T' && line
[1] == ':') {
1471 if (device_count
&& (vendor_id
|| product_id
)) {
1472 /* New device. Add the previously discovered device. */
1473 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1474 product_id
, product_name
, speed
);
1479 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1482 bus_num
= atoi(buf
);
1483 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1487 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1490 if (!strcmp(buf
, "5000")) {
1491 speed
= USB_SPEED_SUPER
;
1492 } else if (!strcmp(buf
, "480")) {
1493 speed
= USB_SPEED_HIGH
;
1494 } else if (!strcmp(buf
, "1.5")) {
1495 speed
= USB_SPEED_LOW
;
1497 speed
= USB_SPEED_FULL
;
1499 product_name
[0] = '\0';
1504 } else if (line
[0] == 'P' && line
[1] == ':') {
1505 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1508 vendor_id
= strtoul(buf
, NULL
, 16);
1509 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1512 product_id
= strtoul(buf
, NULL
, 16);
1513 } else if (line
[0] == 'S' && line
[1] == ':') {
1514 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1517 pstrcpy(product_name
, sizeof(product_name
), buf
);
1518 } else if (line
[0] == 'D' && line
[1] == ':') {
1519 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1522 class_id
= strtoul(buf
, NULL
, 16);
1526 if (device_count
&& (vendor_id
|| product_id
)) {
1527 /* Add the last device. */
1528 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1529 product_id
, product_name
, speed
);
1539 * Read sys file-system device file
1541 * @line address of buffer to put file contents in
1542 * @line_size size of line
1543 * @device_file path to device file (printf format string)
1544 * @device_name device being opened (inserted into device_file)
1546 * @return 0 failed, 1 succeeded ('line' contains data)
1548 static int usb_host_read_file(char *line
, size_t line_size
,
1549 const char *device_file
, const char *device_name
)
1553 char filename
[PATH_MAX
];
1555 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1557 f
= fopen(filename
, "r");
1559 ret
= fgets(line
, line_size
, f
) != NULL
;
1567 * Use /sys/bus/usb/devices/ directory to determine host's USB
1570 * This code is based on Robert Schiele's original patches posted to
1571 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1573 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1577 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1579 char port
[MAX_PORTLEN
];
1580 char product_name
[512];
1583 dir
= opendir(USBSYSBUS_PATH
"/devices");
1585 perror("husb: cannot open devices directory");
1589 while ((de
= readdir(dir
))) {
1590 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1591 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1595 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1598 if (sscanf(line
, "%d", &addr
) != 1) {
1601 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1605 if (sscanf(line
, "%x", &class_id
) != 1) {
1609 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1613 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1616 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1620 if (sscanf(line
, "%x", &product_id
) != 1) {
1623 if (!usb_host_read_file(line
, sizeof(line
), "product",
1627 if (strlen(line
) > 0) {
1628 line
[strlen(line
) - 1] = '\0';
1630 pstrcpy(product_name
, sizeof(product_name
), line
);
1633 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1636 if (!strcmp(line
, "5000\n")) {
1637 speed
= USB_SPEED_SUPER
;
1638 } else if (!strcmp(line
, "480\n")) {
1639 speed
= USB_SPEED_HIGH
;
1640 } else if (!strcmp(line
, "1.5\n")) {
1641 speed
= USB_SPEED_LOW
;
1643 speed
= USB_SPEED_FULL
;
1646 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1647 product_id
, product_name
, speed
);
1661 * Determine how to access the host's USB devices and call the
1662 * specific support function.
1664 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1666 Monitor
*mon
= cur_mon
;
1670 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1671 char devpath
[PATH_MAX
];
1673 /* only check the host once */
1675 dir
= opendir(USBSYSBUS_PATH
"/devices");
1677 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1678 strcpy(devpath
, USBDEVBUS_PATH
);
1679 usb_fs_type
= USB_FS_SYS
;
1681 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1684 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1686 /* devices found in /proc/bus/usb/ */
1687 strcpy(devpath
, USBPROCBUS_PATH
);
1688 usb_fs_type
= USB_FS_PROC
;
1690 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1693 /* try additional methods if an access method hasn't been found yet */
1694 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1696 /* devices found in /dev/bus/usb/ */
1697 strcpy(devpath
, USBDEVBUS_PATH
);
1698 usb_fs_type
= USB_FS_DEV
;
1700 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1706 monitor_printf(mon
, "husb: unable to access USB devices\n");
1711 /* the module setting (used later for opening devices) */
1712 usb_host_device_path
= g_malloc0(strlen(devpath
)+1);
1713 strcpy(usb_host_device_path
, devpath
);
1715 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1716 fs_type
[usb_fs_type
], usb_host_device_path
);
1720 switch (usb_fs_type
) {
1723 ret
= usb_host_scan_dev(opaque
, func
);
1726 ret
= usb_host_scan_sys(opaque
, func
);
1735 static QEMUTimer
*usb_auto_timer
;
1737 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
, char *port
,
1738 int class_id
, int vendor_id
, int product_id
,
1739 const char *product_name
, int speed
)
1741 struct USBAutoFilter
*f
;
1742 struct USBHostDevice
*s
;
1748 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1751 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1754 if (f
->addr
> 0 && f
->addr
!= addr
) {
1757 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1761 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1765 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1768 /* We got a match */
1770 /* Already attached ? */
1774 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1776 usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
);
1783 static void usb_host_auto_check(void *unused
)
1785 struct USBHostDevice
*s
;
1786 int unconnected
= 0;
1788 usb_host_scan(NULL
, usb_host_auto_scan
);
1790 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1796 if (unconnected
== 0) {
1797 /* nothing to watch */
1798 if (usb_auto_timer
) {
1799 qemu_del_timer(usb_auto_timer
);
1800 trace_usb_host_auto_scan_disabled();
1805 if (!usb_auto_timer
) {
1806 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1807 if (!usb_auto_timer
) {
1810 trace_usb_host_auto_scan_enabled();
1812 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1816 * Autoconnect filter
1818 * auto:bus:dev[:vid:pid]
1819 * auto:bus.dev[:vid:pid]
1821 * bus - bus number (dec, * means any)
1822 * dev - device number (dec, * means any)
1823 * vid - vendor id (hex, * means any)
1824 * pid - product id (hex, * means any)
1826 * See 'lsusb' output.
1828 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1830 enum { BUS
, DEV
, VID
, PID
, DONE
};
1831 const char *p
= spec
;
1839 for (i
= BUS
; i
< DONE
; i
++) {
1840 p
= strpbrk(p
, ":.");
1850 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1851 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1852 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1853 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1858 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1865 /**********************/
1866 /* USB host device info */
1868 struct usb_class_info
{
1870 const char *class_name
;
1873 static const struct usb_class_info usb_class_info
[] = {
1874 { USB_CLASS_AUDIO
, "Audio"},
1875 { USB_CLASS_COMM
, "Communication"},
1876 { USB_CLASS_HID
, "HID"},
1877 { USB_CLASS_HUB
, "Hub" },
1878 { USB_CLASS_PHYSICAL
, "Physical" },
1879 { USB_CLASS_PRINTER
, "Printer" },
1880 { USB_CLASS_MASS_STORAGE
, "Storage" },
1881 { USB_CLASS_CDC_DATA
, "Data" },
1882 { USB_CLASS_APP_SPEC
, "Application Specific" },
1883 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1884 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1885 { USB_CLASS_CSCID
, "Smart Card" },
1886 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1890 static const char *usb_class_str(uint8_t class)
1892 const struct usb_class_info
*p
;
1893 for(p
= usb_class_info
; p
->class != -1; p
++) {
1894 if (p
->class == class) {
1898 return p
->class_name
;
1901 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, char *port
,
1902 int class_id
, int vendor_id
, int product_id
,
1903 const char *product_name
,
1906 const char *class_str
, *speed_str
;
1912 case USB_SPEED_FULL
:
1915 case USB_SPEED_HIGH
:
1918 case USB_SPEED_SUPER
:
1926 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1927 bus_num
, addr
, port
, speed_str
);
1928 class_str
= usb_class_str(class_id
);
1930 monitor_printf(mon
, " %s:", class_str
);
1932 monitor_printf(mon
, " Class %02x:", class_id
);
1934 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1935 if (product_name
[0] != '\0') {
1936 monitor_printf(mon
, ", %s", product_name
);
1938 monitor_printf(mon
, "\n");
1941 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1942 char *path
, int class_id
,
1943 int vendor_id
, int product_id
,
1944 const char *product_name
,
1947 Monitor
*mon
= opaque
;
1949 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1950 product_name
, speed
);
1954 static void dec2str(int val
, char *str
, size_t size
)
1957 snprintf(str
, size
, "*");
1959 snprintf(str
, size
, "%d", val
);
1963 static void hex2str(int val
, char *str
, size_t size
)
1966 snprintf(str
, size
, "*");
1968 snprintf(str
, size
, "%04x", val
);
1972 void usb_host_info(Monitor
*mon
)
1974 struct USBAutoFilter
*f
;
1975 struct USBHostDevice
*s
;
1977 usb_host_scan(mon
, usb_host_info_device
);
1979 if (QTAILQ_EMPTY(&hostdevs
)) {
1983 monitor_printf(mon
, " Auto filters:\n");
1984 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1985 char bus
[10], addr
[10], vid
[10], pid
[10];
1987 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1988 dec2str(f
->addr
, addr
, sizeof(addr
));
1989 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1990 hex2str(f
->product_id
, pid
, sizeof(pid
));
1991 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1992 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);