Skip to contentSkip to conversation

fix for bugzilla #7544 (keyspan USB-to-serial converter)

PATCH In review Rainer Weikusat 2 participants 5 messages Jan 2, 2007 – Jan 3, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix for bugzilla #7544 (keyspan USB-to-serial converter)

At least the Keyspan USA-19HS USB-to-serial converter supports two different configurations, one where the input endpoints have interrupt transfer type and one where they are bulk endpoints. The default UHCI configuration uses the interrupt input endpoints. The keyspan driver, OTOH, assumes that the device has only bulk endpoints (all URBs are initialized by calling usb_fill_bulk_urb in keyspan.c/ keyspan_setup_urb). This causes the interval field of the input URBs to have a value of zero instead of one, which 'accidentally' worked with Linux at least up to 2.6.17.11 but stopped to with 2.6.18, which changed the UHCI support code handling URBs for interrupt endpoints. The patch below modifies to driver to initialize its input URBs either as interrupt or as bulk URBs, depending on the transfertype contained in the associated endpoint descriptor (only tested with the default configuration) enabling the driver to again receive data from the serial converter.

---

drivers/usb/serial/keyspan.c +42 -4
diff -pNur linux-2.6.20-rc3/drivers/usb/serial/keyspan.c linux-2.6.20-rc3-keyspan/drivers/usb/serial/keyspan.c
@@ -95,6 +95,7 @@
 */
 
 
+#include <linux/compiler.h>
 #include <linux/kernel.h>
 #include <linux/jiffies.h>
 #include <linux/errno.h>
@@ -1275,11 +1276,29 @@ static int keyspan_fake_startup (struct 
 }
 
 /* Helper functions used by keyspan_setup_urbs */
+static struct usb_endpoint_descriptor const *
+find_ep_desc_for(struct usb_serial const *serial, int endpoint)
+{
+	struct usb_host_endpoint const *p, *e;
+
+	p = serial->interface->cur_altsetting->endpoint;
+	e = p + serial->interface->cur_altsetting->desc.bNumEndpoints;
+	while (p < e && p->desc.bEndpointAddress != endpoint) ++p;
+	
+	if (unlikely(p == e)) panic("found no endpoint descriptor for "
+				    "endpoint %d\n", endpoint);
+
+	return &p->desc;
+}
+
 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
 				      int dir, void *ctx, char *buf, int len,
 				      void (*callback)(struct urb *))
 {
 	struct urb *urb;
+	struct usb_endpoint_descriptor const *ep_desc;
+	char const *ep_type_name;
+	unsigned ep_type;
 
 	if (endpoint == -1)
 		return NULL;		/* endpoint not needed */
@@ -1290,12 +1309,31 @@ static struct urb *keyspan_setup_urb (st
 		dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
 		return NULL;
 	}
+	
+	ep_desc = find_ep_desc_for(serial, endpoint);
+	ep_type = ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
+	switch (ep_type) {
+	case USB_ENDPOINT_XFER_INT:
+		ep_type_name = "INT";
+		usb_fill_int_urb(urb, serial->dev,
+				 usb_sndintpipe(serial->dev, endpoint) | dir,
+				 buf, len, callback, ctx,
+				 ep_desc->bInterval);
+		break;
+
+	case USB_ENDPOINT_XFER_BULK:
+		ep_type_name = "BULK";
+		usb_fill_bulk_urb(urb, serial->dev,
+				  usb_sndbulkpipe(serial->dev, endpoint) | dir,
+				  buf, len, callback, ctx);
+		break;
 
-		/* Fill URB using supplied data. */
-	usb_fill_bulk_urb(urb, serial->dev,
-		      usb_sndbulkpipe(serial->dev, endpoint) | dir,
-		      buf, len, callback, ctx);
+	default:
+		panic("unsupported endpoint type %d", ep_type);
+	}
 
+	dbg("%s - using urb %p for %s endpoint %d",
+	    __func__, urb, ep_type_name, endpoint);
 	return urb;
 }
 
#1 lore raw
Quoted 7 lines · Rainer Weikusat

At least the Keyspan USA-19HS USB-to-serial converter supports two different configurations, one where the input endpoints have interrupt transfer type and one where they are bulk endpoints. The default UHCI configuration uses the interrupt input endpoints. The keyspan driver, OTOH, assumes that the device has only bulk endpoints (all URBs are initialized by calling usb_fill_bulk_urb in keyspan.c/ keyspan_setup_urb).

So, this means that Keyspan changed the USB configuration of this device?

Can you send me the output of 'cat /proc/bus/usb/devices' with this keyspan device plugged in? I'll compare it with the devices I have here.

Quoted 28 lines

This causes the interval field
of the input URBs to have a value of zero instead of one, which 'accidentally' worked with Linux at least up to 2.6.17.11 but stopped to with 2.6.18, which changed the UHCI support code handling URBs for interrupt endpoints. The patch below modifies to driver to initialize its input URBs either as interrupt or as bulk URBs, depending on the transfertype contained in the associated endpoint descriptor (only tested with the default configuration) enabling the driver to again receive data from the serial converter.

---
diff -pNur linux-2.6.20-rc3/drivers/usb/serial/keyspan.c linux-2.6.20-rc3-keyspan/drivers/usb/serial/keyspan.c

drivers/usb/serial/keyspan.c +2 -0
@@ -95,6 +95,7 @@
 */
 
 
+#include <linux/compiler.h>
 #include <linux/kernel.h>
 #include <linux/jiffies.h>
 #include <linux/errno.h>
@@ -1275,11 +1276,29 @@ static int keyspan_fake_startup (struct 
 }
 
 /* Helper functions used by keyspan_setup_urbs */
+static struct usb_endpoint_descriptor const *

drivers/usb/serial/keyspan.c static int keyspan_fake_startup (struct · line 1289

Quoted 10 lines
drivers/usb/serial/keyspan.c +10 -0
+find_ep_desc_for(struct usb_serial const *serial, int endpoint)
+{
+	struct usb_host_endpoint const *p, *e;
+
+	p = serial->interface->cur_altsetting->endpoint;
+	e = p + serial->interface->cur_altsetting->desc.bNumEndpoints;
+	while (p < e && p->desc.bEndpointAddress != endpoint) ++p;
+	
+	if (unlikely(p == e)) panic("found no endpoint descriptor for "
+				    "endpoint %d\n", endpoint);

No need for "unlikely" on such a slow path.

Also, panic() should be on the next line for the proper coding style.

And, we don't want to panic() for such a trivial thing. Just abort the probe sequence at most, but never shut down the machine for an odd device that we find.

Quoted 34 lines
drivers/usb/serial/keyspan.c +22 -0
+
+	return &p->desc;
+}
+
 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
 				      int dir, void *ctx, char *buf, int len,
 				      void (*callback)(struct urb *))
 {
 	struct urb *urb;
+	struct usb_endpoint_descriptor const *ep_desc;
+	char const *ep_type_name;
+	unsigned ep_type;
 
 	if (endpoint == -1)
 		return NULL;		/* endpoint not needed */
@@ -1290,12 +1309,31 @@ static struct urb *keyspan_setup_urb (st
 		dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
 		return NULL;
 	}
+	
+	ep_desc = find_ep_desc_for(serial, endpoint);
+	ep_type = ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
+	switch (ep_type) {
+	case USB_ENDPOINT_XFER_INT:
+		ep_type_name = "INT";
+		usb_fill_int_urb(urb, serial->dev,
+				 usb_sndintpipe(serial->dev, endpoint) | dir,
+				 buf, len, callback, ctx,
+				 ep_desc->bInterval);
+		break;
+
+	case USB_ENDPOINT_XFER_BULK:
+		ep_type_name = "BULK";
+		usb_fill_bulk_urb(urb, serial->dev,

drivers/usb/serial/keyspan.c static struct urb *keyspan_setup_urb (st · line 1332

Quoted 10 lines
drivers/usb/serial/keyspan.c +5 -4
+				  usb_sndbulkpipe(serial->dev, endpoint) | dir,
+				  buf, len, callback, ctx);
+		break;
 
-		/* Fill URB using supplied data. */
-	usb_fill_bulk_urb(urb, serial->dev,
-		      usb_sndbulkpipe(serial->dev, endpoint) | dir,
-		      buf, len, callback, ctx);
+	default:
+		panic("unsupported endpoint type %d", ep_type);

Again, no usb driver should be calling panic().

thanks,

greg k-h

#2 lore raw
Quoted 11 lines · Greg KH
Quoted 7 lines · Rainer Weikusat

At least the Keyspan USA-19HS USB-to-serial converter supports two different configurations, one where the input endpoints have interrupt transfer type and one where they are bulk endpoints. The default UHCI configuration uses the interrupt input endpoints. The keyspan driver, OTOH, assumes that the device has only bulk endpoints (all URBs are initialized by calling usb_fill_bulk_urb in keyspan.c/ keyspan_setup_urb).

So, this means that Keyspan changed the USB configuration of this device?

No, it means what I have written several times: The UHCI code up to 2.6.17.11 accepted URBs initialized as bulk URBs for interrupt endpoints, the UHCI code since 2.6.18 doesn't anymore. This is all somewhat idealized, but these endpoints should be interrupt endpoints and the driver should initialize its URBs correctly.

Quoted 3 lines

Can you send me the output of 'cat /proc/bus/usb/devices' with this keyspan device plugged in? I'll compare it with the devices I have here.

In plain English, this means 'I am fairly convinced that the actual problem is that you are certainly too stupid to read'.

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12  MxCh= 0
D:  Ver= 1.10 Cls=ff(vend.) Sub=ff Prot=ff MxPS= 8 #Cfgs=  2
P:  Vendor=06cd ProdID=0121 Rev= 1.00
S:  Manufacturer=Keyspan, a division of InnoSys Inc.
S:  Product=Keyspan USA-19H 
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
I:  If#= 0 Alt= 0 #EPs= 5 Cls=ff(vend.) Sub=00 Prot=00 Driver=keyspan
E:  Ad=01(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=03(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=81(I) Atr=03(Int.) MxPS=  64 Ivl=1ms
E:  Ad=82(I) Atr=03(Int.) MxPS=  64 Ivl=1ms
C:  #Ifs= 1 Cfg#= 2 Atr=a0 MxPwr=100mA
I:  If#= 0 Alt= 0 #EPs= 5 Cls=ff(vend.) Sub=00 Prot=00 Driver=
E:  Ad=01(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=03(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=81(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=82(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms

NB: This indicates that the input buffer size (32) is wrong as well.

[...]

Quoted 6 lines
Quoted 6 lines

/* Helper functions used by keyspan_setup_urbs */

drivers/usb/serial/keyspan.c +5 -0
+static struct usb_endpoint_descriptor const *
+find_ep_desc_for(struct usb_serial const *serial, int endpoint)
+{
+	struct usb_host_endpoint const *p, *e;
+
Quoted 8 lines

drivers/usb/serial/keyspan.c static int keyspan_fake_startup (struct · line 1289

Quoted 6 lines
drivers/usb/serial/keyspan.c +6 -0
+	p = serial->interface->cur_altsetting->endpoint;
+	e = p + serial->interface->cur_altsetting->desc.bNumEndpoints;
+	while (p < e && p->desc.bEndpointAddress != endpoint) ++p;
+	
+	if (unlikely(p == e)) panic("found no endpoint descriptor for "
+				    "endpoint %d\n", endpoint);

No need for "unlikely" on such a slow path.

It doesn't hurt and documents that this is the proverbial impossible condition check.

Quoted 2 lines

Also, panic() should be on the next line for the proper coding style.

It is somewhat unreasonable to expect random people to 'just know' about things that aren't documented.

Quoted 3 lines

And, we don't want to panic() for such a trivial thing. Just abort the probe sequence at most, but never shut down the machine for an odd device that we find.

I actually thought about that this morning: Considering the path this came from, this is not 'an odd device' but rather something like 'kernel memory corruption' (the 'endpoint' value originally came from the exact same descriptor).

#3 lore raw

Rainer Weikusat <[email protected]> writes:

[...]

Quoted 8 lines
Quoted 3 lines

And, we don't want to panic() for such a trivial thing. Just abort the probe sequence at most, but never shut down the machine for an odd device that we find.

I actually thought about that this morning: Considering the path this came from, this is not 'an odd device' but rather something like 'kernel memory corruption' (the 'endpoint' value originally came from the exact same descriptor).

This turned out to be wrong: The values are hard-coded in keyspan.h.

#4 lore raw

PATCH 1/1 fix for bugzilla #7544 (keyspan USB-to-serial converter)

At least the Keyspan USA-19HS USB-to-serial converter supports two different configurations, one where the input endpoints have interrupt transfer type and one where they are bulk endpoints. The default UHCI configuration uses the interrupt input endpoints. The keyspan driver, OTOH, assumes that the device has only bulk endpoints (all URBs are initialized by calling usb_fill_bulk_urb in keyspan.c/ keyspan_setup_urb). This causes the interval field of the input URBs to have a value of zero instead of one, which 'accidentally' worked with Linux at least up to 2.6.17.11 but stopped to with 2.6.18, which changed the UHCI support code handling URBs for interrupt endpoints. The patch below modifies to driver to initialize its input URBs either as interrupt or as bulk URBs, depending on the transfertype contained in the associated endpoint descriptor (only tested with the default configuration) enabling the driver to again receive data from the serial converter.

---

Modified to no longer call panic, but return NULL after having logged a KERN_WARNING message first.

drivers/usb/serial/keyspan.c +47 -4
diff -purN linux-2.6.20-rc3/drivers/usb/serial/keyspan.c linux-2.6.20-rc3-keyspan/drivers/usb/serial/keyspan.c
@@ -1275,11 +1275,32 @@ static int keyspan_fake_startup (struct 
 }
 
 /* Helper functions used by keyspan_setup_urbs */
+static struct usb_endpoint_descriptor const *
+find_ep_desc_for(struct usb_serial const *serial, int endpoint)
+{
+	struct usb_host_endpoint const *p, *e;
+
+	p = serial->interface->cur_altsetting->endpoint;
+	e = p + serial->interface->cur_altsetting->desc.bNumEndpoints;
+	while (p < e && p->desc.bEndpointAddress != endpoint) ++p;
+	
+	if (p == e) {
+		printk(KERN_WARNING "%s - found no endpoint descriptor for "
+		       "endpoint %d\n", __func__, endpoint);
+		return NULL;
+	}
+
+	return &p->desc;
+}
+
 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
 				      int dir, void *ctx, char *buf, int len,
 				      void (*callback)(struct urb *))
 {
 	struct urb *urb;
+	struct usb_endpoint_descriptor const *ep_desc;
+	char const *ep_type_name;
+	unsigned ep_type;
 
 	if (endpoint == -1)
 		return NULL;		/* endpoint not needed */
@@ -1290,12 +1311,34 @@ static struct urb *keyspan_setup_urb (st
 		dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
 		return NULL;
 	}
+	
+	ep_desc = find_ep_desc_for(serial, endpoint);
+	ep_type = ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
+	switch (ep_type) {
+	case USB_ENDPOINT_XFER_INT:
+		ep_type_name = "INT";
+		usb_fill_int_urb(urb, serial->dev,
+				 usb_sndintpipe(serial->dev, endpoint) | dir,
+				 buf, len, callback, ctx,
+				 ep_desc->bInterval);
+		break;
 
-		/* Fill URB using supplied data. */
-	usb_fill_bulk_urb(urb, serial->dev,
-		      usb_sndbulkpipe(serial->dev, endpoint) | dir,
-		      buf, len, callback, ctx);
+	case USB_ENDPOINT_XFER_BULK:
+		ep_type_name = "BULK";
+		usb_fill_bulk_urb(urb, serial->dev,
+				  usb_sndbulkpipe(serial->dev, endpoint) | dir,
+				  buf, len, callback, ctx);
+		break;
+
+	default:
+		printk(KERN_WARNING "%s - unsupported endpoint type %d\n",
+		       __func__, ep_type);
+		usb_free_urb(urb);
+		return NULL;
+	}
 
+	dbg("%s - using urb %p for %s endpoint %d",
+	    __func__, urb, ep_type_name, endpoint);
 	return urb;
 }