usb-host: bugfixes
cover usb-host: bugfixes
Hi,
Here comes a bunch of bugfixes for the usb host (aka usb pass-thru) driver. Gets some of my devices working which didn't work before, so anyone who had trouble with it recently is encouraged to re-test with these patches applied.
cheers,
Gerd
Gerd Hoffmann (7):
usb-host: start tracing support
usb-host: reapurb error report fix
usb-host: fix halted endpoints
usb-host: limit open retries
usb-host: fix configuration tracking.
usb-host: claim port
usb: fix use after free
hw/usb.c | 2 +- trace-events | 19 +++++ usb-linux.c | 236 +++++++++++++++++++++++++++++++++++----------------------- 3 files changed, 163 insertions(+), 94 deletions(-)
PATCH 1/7 usb-host: start tracing support
Add a bunch of trace points to usb-linux.c Drop a bunch of DPRINTK's in favor of the trace points. Also cleanup error reporting a bit while being at it.
--- trace-events | 18 ++++++++++++++ usb-linux.c | 73 ++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 66 insertions(+), 25 deletions(-)
trace-events +18 -0
@@ -243,6 +243,24 @@ disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d disable usb_clear_device_feature(int addr, int feature, int ret) "dev %d, feature %d, ret %d" disable usb_set_device_feature(int addr, int feature, int ret) "dev %d, feature %d, ret %d" +# usb-linux.c +usb_host_open(int bus, int addr, const char *state) "dev %d:%d, %s" +usb_host_disconnect(int bus, int addr) "dev %d:%d" +usb_host_close(int bus, int addr) "dev %d:%d" +usb_host_set_address(int bus, int addr, int config) "dev %d:%d, address %d" +usb_host_set_config(int bus, int addr, int config) "dev %d:%d, config %d" +usb_host_set_interface(int bus, int addr, int interface, int alt) "dev %d:%d, interface %d, alt %d" +usb_host_claim_interfaces(int bus, int addr, int config, int nif) "dev %d:%d, config %d, nif %d" +usb_host_release_interfaces(int bus, int addr) "dev %d:%d" +usb_host_req_control(int bus, int addr, int req, int value, int index) "dev %d:%d, req 0x%x, value %d, index %d" +usb_host_req_data(int bus, int addr, const char *dir, int ep, int size) "dev %d:%d, %s, ep %d, size %d" +usb_host_req_complete(int bus, int addr, int status) "dev %d:%d, status %d" +usb_host_urb_submit(int bus, int addr, void *aurb, int length, int more) "dev %d:%d, aurb %p, length %d, more %d" +usb_host_urb_complete(int bus, int addr, void *aurb, int status, int length, int more) "dev %d:%d, aurb %p, status %d, length %d, more %d" +usb_host_ep_op(int bus, int addr, int ep, const char *op) "dev %d:%d, ep %d, %s" +usb_host_reset(int bus, int addr) "dev %d:%d" +usb_host_auto_scan(const char *state) "%s" + # hw/scsi-bus.c disable scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d" disable scsi_req_data(int target, int lun, int tag, int len) "target %d lun %d tag %d len %d"
usb-linux.c +48 -25
280 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [Qemu-devel] [PATCH 1/7] usb-host: start tracing support
Quoted 18 lines · Gerd Hoffmann
Add a bunch of trace points to
usb-linux.cDrop a bunch of DPRINTK's in favor of the trace points. Also cleanup error reporting a bit while being at it.Signed-off-by Gerd Hoffmann <[email protected]>---
trace-events | 18 ++++++++++++++
usb-linux.c | 73 ++++++++++++++++++++++++++++++++++++++--------------------2 files changed, 66 insertions(+), 25 deletions(-)trace-events +0
-0@@ -243,6 +243,24 @@ disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %ddisable
usb_clear_device_feature(int addr, int feature, int ret)"dev %d, feature %d, ret %d" disableusb_set_device_feature(int addr, int feature, int ret)"dev %d, feature %d, ret %d"
trace-events disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d · line 247
Quoted 2 lines
trace-events +2
-0+# usb-linux.c +usb_host_open(int bus, int addr, const char *state) "dev %d:%d, %s"
Please do not use strings. It works for stderr but may not be supported by other backends. The simple backend only logs 64-bit arguments and does not dereference into strings/arrays. I can't remember the status of dtrace or ust, but I'd ask you to avoid using a string, if possible.
trace-events disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d · line 256
Quoted 9 lines
trace-events +9
-0+usb_host_disconnect(int bus, int addr) "dev %d:%d" +usb_host_close(int bus, int addr) "dev %d:%d" +usb_host_set_address(int bus, int addr, int config) "dev %d:%d, address %d" +usb_host_set_config(int bus, int addr, int config) "dev %d:%d, config %d" +usb_host_set_interface(int bus, int addr, int interface, int alt) "dev %d:%d, interface %d, alt %d" +usb_host_claim_interfaces(int bus, int addr, int config, int nif) "dev %d:%d, config %d, nif %d" +usb_host_release_interfaces(int bus, int addr) "dev %d:%d" +usb_host_req_control(int bus, int addr, int req, int value, int index) "dev %d:%d, req 0x%x, value %d, index %d" +usb_host_req_data(int bus, int addr, const char *dir, int ep, int size) "dev %d:%d, %s, ep %d, size %d"
String
trace-events disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d · line 260
Quoted 4 lines
trace-events +4
-0+usb_host_req_complete(int bus, int addr, int status) "dev %d:%d, status %d" +usb_host_urb_submit(int bus, int addr, void *aurb, int length, int more) "dev %d:%d, aurb %p, length %d, more %d" +usb_host_urb_complete(int bus, int addr, void *aurb, int status, int length, int more) "dev %d:%d, aurb %p, status %d, length %d, more %d" +usb_host_ep_op(int bus, int addr, int ep, const char *op) "dev %d:%d, ep %d, %s"
String
trace-events disable usb_set_config(int addr, int config, int ret) "dev %d, config %d, ret %d · line 262
Quoted 2 lines
trace-events +2
-0+usb_host_reset(int bus, int addr) "dev %d:%d" +usb_host_auto_scan(const char *state) "%s"
String
The rest looks fine. In some cases you can simply split the trace event into multiple independent events which contains the status/operation/etc value that you're trying to pretty-print.
Stefan
PATCH 2/7 usb-host: reapurb error report fix
In reply to Gerd HoffmannDon't report errors on devices which are in disconnected and closing state.
--- usb-linux.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
usb-linux.c +5 -3
@@ -312,9 +312,11 @@ static void async_complete(void *opaque) }return ; } -if (errno ==ENODEV && !s->closing) { -trace_usb_host_disconnect (s->bus_num, s->addr); -do_disconnect (s); +if (errno ==ENODEV ) { +if (!s->closing) { +trace_usb_host_disconnect (s->bus_num, s->addr); +do_disconnect (s); + }return ; }
Signature · 1 line
1.7.1
PATCH 3/7 usb-host: fix halted endpoints
In reply to Gerd HoffmannTwo fixes for the price of one ;)
First, reinitialize the endpoint table after device reset. This is needed anyway as the reset might have switched interfaces. It also clears the endpoint halted state.
Second the CLEAR_HALT ioctl wants a unsigned int passed in as argument, not uint8_t.
This gets my usb sd card reader (sandisk micromate) going.
--- usb-linux.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
usb-linux.c +4 -3
@@ -143,6 +143,7 @@ static int parse_filter(const char *spec, struct USBAutoFilter *f);static void usb_host_auto_check (void *unused);static int usb_host_read_file (char *line,size_t line_size,const char *device_file,const char *device_name); +static int usb_linux_update_endp_table (USBHostDevice *s);static struct endp_data *get_endp (USBHostDevice *s,int ep) {
@@ -512,6 +513,7 @@ static void usb_host_handle_reset(USBDevice *dev)ioctl (s->fd,USBDEVFS_RESET );usb_host_claim_interfaces (s, s->configuration); +usb_linux_update_endp_table (s); }static void usb_host_handle_destroy (USBDevice *dev)
@@ -523,8 +525,6 @@ static void usb_host_handle_destroy(USBDevice *dev)qemu_remove_exit_notifier (&s->exit); } -static int usb_linux_update_endp_table (USBHostDevice *s); -/* iso data is special, we need to keep enough urbs in flight to make sure that the controller never runs out of them, otherwise the device will likely suffer a buffer underrun / overrun. */
@@ -732,7 +732,8 @@ static int usb_host_handle_data(USBDevice *dev, USBPacket *p) }if (is_halted (s, p->devep)) { - ret =ioctl (s->fd,USBDEVFS_CLEAR_HALT , &ep); +unsigned int arg = ep; + ret =ioctl (s->fd,USBDEVFS_CLEAR_HALT , &arg);if (ret <0 ) {perror ("USBDEVFS_CLEAR_HALT" );trace_usb_host_req_complete (s->bus_num, s->addr,USB_RET_NAK );
Signature · 1 line
1.7.1
PATCH 4/7 usb-host: limit open retries
In reply to Gerd HoffmannLimit the number of times qemu tries to open host devices to three. Reset error counter when the device goes away, after un-plugging and re-plugging the device qemu will try again three times.
--- usb-linux.c | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-)
usb-linux.c +12 -1
@@ -132,6 +132,7 @@ typedef struct USBHostDevice {int addr;char port[MAX_PORTLEN ];struct USBAutoFilter match; +int seen, errcount;QTAILQ_ENTRY (USBHostDevice) next; } USBHostDevice;
@@ -1769,6 +1770,10 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,continue ; }/* We got a match */ + s->seen++; +if (s->errcount >=3 ) { +return 0 ; + }/* Already attached ? */ if (s->fd !=-1 ) {
@@ -1776,7 +1781,9 @@ static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port, }DPRINTF ("husb: auto open: bus_num %d addr %d\n" , bus_num, addr); -usb_host_open (s, bus_num, addr, port, product_name, speed); +if (usb_host_open (s, bus_num, addr, port, product_name, speed) <0 ) { + s->errcount++; + }break ; }
@@ -1794,6 +1801,10 @@ static void usb_host_auto_check(void *unused)if (s->fd ==-1 ) { unconnected++; } +if (s->seen ==0 ) { + s->errcount =0 ; + } + s->seen =0 ; }if (unconnected ==0 ) {
Signature · 1 line
1.7.1
PATCH 5/7 usb-host: fix configuration tracking.
In reply to Gerd HoffmannIt is perfectly fine to leave the usb device in unconfigured state (USBHostDevice->configuration == 0). Just do that and wait for the guest to explicitly set a configuration. This is closer to what real hardware does and it also simplifies the device initialization. There is no need to figure how the device is configured on the host.
--- usb-linux.c | 80 +++++++++++++--------------------------------------------- 1 files changed, 18 insertions(+), 62 deletions(-)
usb-linux.c +18 -62
@@ -407,8 +407,11 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)int interface, nb_interfaces;int ret, i; -if (configuration ==0 )/* address state - ignore */ +if (configuration ==0 ) {/* address state - ignore */ + dev->ninterfaces =0 ; + dev->configuration =0 ;return 1 ; + }DPRINTF ("husb: claiming interfaces. config %d\n" , configuration);
@@ -433,7 +436,7 @@ static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)DPRINTF ("husb: config #%d need %d\n" , dev->descr[i +5 ], configuration); -if (configuration <0 || configuration == dev->descr[i +5 ]) { +if (configuration == dev->descr[i +5 ]) { configuration = dev->descr[i +5 ];break ; }
@@ -835,6 +838,7 @@ static int usb_host_set_config(USBHostDevice *s, int config)return ctrl_error (); }usb_host_claim_interfaces (s, config); +usb_linux_update_endp_table (s);return 0 ; }
@@ -941,51 +945,6 @@ static int usb_host_handle_control(USBDevice *dev, USBPacket *p,return USB_RET_ASYNC ; } -static int usb_linux_get_configuration (USBHostDevice *s) -{ -uint8_t configuration; -struct usb_ctrltransfer ct; -int ret; - -if (usb_fs_type ==USB_FS_SYS ) { -char device_name[32 ], line[1024 ]; -int configuration; - -sprintf (device_name,"%d-%s" , s->bus_num, s->port); - -if (!usb_host_read_file (line,sizeof (line),"bConfigurationValue" , - device_name)) { - goto usbdevfs; - } -if (sscanf (line,"%d" , &configuration) !=1 ) { - goto usbdevfs; - } -return configuration; - } - -usbdevfs: - ct.bRequestType =USB_DIR_IN ; - ct.bRequest =USB_REQ_GET_CONFIGURATION ; - ct.wValue =0 ; - ct.wIndex =0 ; - ct.wLength =1 ; - ct.data = &configuration; - ct.timeout =50 ; - - ret =ioctl (s->fd,USBDEVFS_CONTROL , &ct); -if (ret <0 ) { -perror ("usb_linux_get_configuration" ); -return -1 ; - } - -/* in address state */ -if (configuration ==0 ) { -return -1 ; - } - -return configuration; -} -static uint8_t usb_linux_get_alt_setting (USBHostDevice *s,uint8_t configuration,uint8_t interface) {
@@ -1031,16 +990,16 @@ usbdevfs:static int usb_linux_update_endp_table (USBHostDevice *s) {uint8_t *descriptors; -uint8_t devep, type, configuration, alt_interface; +uint8_t devep, type, alt_interface;int interface, length, i;for (i =0 ; i <MAX_ENDPOINTS ; i++) s->endp_table[i].type =INVALID_EP_TYPE ; - i =usb_linux_get_configuration (s); -if (i <0 ) -return 1 ; - configuration = i; +if (s->configuration ==0 ) { +/* not configured yet -- leave all endpoints disabled */ +return 0 ; + }/* get the desired configuration, interface, and endpoint descriptors * from device description */
@@ -1049,8 +1008,9 @@ static int usb_linux_update_endp_table(USBHostDevice *s) i =0 ;if (descriptors[i +1 ] !=USB_DT_CONFIG || - descriptors[i +5 ] != configuration) { -DPRINTF ("invalid descriptor data - configuration\n" ); + descriptors[i +5 ] != s->configuration) { +fprintf (stderr,"invalid descriptor data - configuration %d\n" , + s->configuration);return 1 ; } i += descriptors[i];
@@ -1064,7 +1024,8 @@ static int usb_linux_update_endp_table(USBHostDevice *s) } interface = descriptors[i +2 ]; - alt_interface =usb_linux_get_alt_setting (s, configuration, interface); + alt_interface =usb_linux_get_alt_setting (s, s->configuration, + interface);/* the current interface descriptor is the active interface * and has endpoints */
@@ -1204,13 +1165,8 @@ static int usb_host_open(USBHostDevice *dev, int bus_num,#endif -/* -* Initial configuration is -1 which makes us claim first -* available config. We used to start with 1, which does not -* always work. I've seen devices where first config starts -* with 2. -*/ -if (!usb_host_claim_interfaces (dev,-1 )) { +/* start unconfigured -- we'll wait for the guest to set a configuration */ +if (!usb_host_claim_interfaces (dev,0 )) { goto fail; }
Signature · 1 line
1.7.1
PATCH 6/7 usb-host: claim port
In reply to Gerd HoffmannWhen configured to pass through a specific host port (using hostbus and hostport properties), try to claim the port if supported by the kernel. That will avoid any kernel drivers binding to devices plugged into that port. It will not stop any userspace apps (such as usb_modeswitch) access the device via usbfs though.
--- trace-events | 1 + usb-linux.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 0 deletions(-)
trace-events +1 -0
@@ -260,6 +260,7 @@ usb_host_urb_complete(int bus, int addr, void *aurb, int status, int length, int usb_host_ep_op(int bus, int addr, int ep, const char *op) "dev %d:%d, ep %d, %s" usb_host_reset(int bus, int addr) "dev %d:%d" usb_host_auto_scan(const char *state) "%s" +usb_host_claim_port(int bus, int hub, int port) "bus %d, hub addr %d, port %d" # hw/scsi-bus.c disable scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d"
usb-linux.c +57 -0
79 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [Qemu-devel] [PATCH 6/7] usb-host: claim port
Hi Gerd,
is this related / the fix to my question regarding the recurring claimed messages? (See my mail dated 2011-08-21)
Best regards,
Erik
Quoted 110 lines · Gerd Hoffmann
When configured to pass through a specific host port (using hostbus and hostport properties), try to claim the port if supported by the kernel. That will avoid any kernel drivers binding to devices plugged into that port. It will not stop any userspace apps (such as
usb_modeswitch) access the device via usbfs though.Signed-off-by Gerd Hoffmann<[email protected]>--- trace-events | 1 + usb-linux.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 0 deletions(-)trace-events +1
-0@@ -260,6 +260,7 @@ usb_host_urb_complete(int bus, int addr, void *aurb, int status, int length, int usb_host_ep_op(int bus, int addr, int ep, const char *op) "dev %d:%d, ep %d, %s" usb_host_reset(int bus, int addr) "dev %d:%d" usb_host_auto_scan(const char *state) "%s" +usb_host_claim_port(int bus, int hub, int port) "bus %d, hub addr %d, port %d" # hw/scsi-bus.c disable scsi_req_alloc(int target, int lun, int tag) "target %d lun %d tag %d"usb-linux.c +57
-0@@ -115,6 +115,7 @@ struct USBAutoFilter { typedef struct USBHostDevice { USBDevice dev; int fd; + int hub_fd; uint8_t descr[8192]; int descr_len;@@ -525,6 +526,9 @@ static void usb_host_handle_destroy(USBDevice *dev)USBHostDevice *s = (USBHostDevice *)dev;usb_host_close (s); +if (s->hub_fd !=-1 ) { +close (s->hub_fd); + }QTAILQ_REMOVE (&hostdevs, s, next);qemu_remove_exit_notifier (&s->exit); }@@ -1266,10 +1270,63 @@ static int usb_host_initfn(USBDevice *dev) dev->auto_attach =0 ; s->fd =-1 ; + s->hub_fd =-1 ; +QTAILQ_INSERT_TAIL (&hostdevs, s, next); s->exit.notify = usb_host_exit_notifier;qemu_add_exit_notifier (&s->exit);usb_host_auto_check (NULL ); + +#ifdef USBDEVFS_CLAIM_PORT +if (s->match.bus_num !=0 && s->match.port !=NULL ) { +char *h, hub_name[64 ], line[1024 ]; +int hub_addr, portnr, ret; + +snprintf (hub_name,sizeof (hub_name),"%d-%s" , + s->match.bus_num, s->match.port); + +/* try strip off last ".$portnr" to get hub */ + h =strrchr (hub_name,'.' ); +if (h !=NULL ) { + portnr =atoi (h+1 ); + *h ='\0' ; + }else { +/* no dot in there -> it is the root hub */ +snprintf (hub_name,sizeof (hub_name),"usb%d" , + s->match.bus_num); + portnr =atoi (s->match.port); + } + +if (!usb_host_read_file (line,sizeof (line),"devnum" , + hub_name)) { + goto out; + } +if (sscanf (line,"%d" ,&hub_addr) !=1 ) { + goto out; + } + +if (!usb_host_device_path) { + goto out; + } +snprintf (line,sizeof (line),"%s/%03d/%03d" , + usb_host_device_path, s->match.bus_num, hub_addr); + s->hub_fd =open (line,O_RDWR |O_NONBLOCK ); +if (s->hub_fd<0 ) { + goto out; + } + + ret =ioctl (s->hub_fd,USBDEVFS_CLAIM_PORT ,&portnr); +if (ret<0 ) { +close (s->hub_fd); + s->hub_fd =-1 ; + goto out; + } + +trace_usb_host_claim_port (s->match.bus_num, hub_addr, portnr); + } +out: +#endif +return 0 ; }
Re: [Qemu-devel] [PATCH 6/7] usb-host: claim port
Quoted 4 lines · Erik Rull
Hi Gerd,
is this related / the fix to my question regarding the recurring claimed messages? (See my mail dated 2011-08-21)
No, but patches #3 + #5 hopefully help there.
cheers,
Gerd
PATCH 7/7 usb: fix use after free
In reply to Gerd HoffmannThe ->complete() callback might have released the USBPacket (uhci actually does), so we must not touch it after the callback returns.
--- hw/usb.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/usb.c +1 -1
@@ -338,8 +338,8 @@ void usb_packet_complete(USBDevice *dev, USBPacket *p) {/* Note: p->owner != dev is possible in case dev is a hub */ assert (p->owner !=NULL ); - dev->port->ops->complete (dev->port, p); p->owner =NULL ; + dev->port->ops->complete (dev->port, p); }/* Cancel an active packet. The packed must have been deferred by
Signature · 1 line
1.7.1