Greg and others:

This patch changes usb_string() and usb_get_string(), the two utility 
routines responsible for fetching string descriptors from devices.  
usb_get_string() now works just like usb_get_descriptor(): retrying up to 
3 times in the face of -EPIPE or 0-length results.

usb_string() is moved up in the file to right after usb_get_string(),
putting it together with all the other descriptor-reading utilities.  
More importantly, it no longer tries to read a string's length before
transferring the string.  In this respect it's more like it used to be
back in 2.4.

Windows is curiously inconsistent in this regard.  USB traces taken from 
Windows 2000 and Windows XP show that both operating systems initially 
request 255 bytes to read descriptor 0 (which contains the list of 
available language codes) and the product and serial-number strings.  
(Oddly enough, the vendor string wasn't read during my tests.)  But 
then later they retrieve descriptor 0 and the serial number using the 
two-step approach of reading the length first, then asking for only that 
much.  Perhaps this reflects two different APIs being used at different 
times by various parts of the system.  I would expect that all devices 
will have to cope with those 255-byte initial reads, whereas maybe only 
mass-storage devices (like the one I traced) get the two-step treatment.

It's possible that this patch will break interoperability with a few buggy
devices -- though only for string descriptor fetches.  If so, then those
devices probably don't work with Windows either.  If they do, it would be
interesting to know exactly what requests get sent.

I realize that we have discussed making this sort of change before,
although I don't remember coming to any definite conclusion.  Does anyone 
feel very strongly about not applying this?

Alan Stern



Signed-off-by: Alan Stern <[EMAIL PROTECTED]>

===== drivers/usb/core/message.c 1.95 vs edited =====
--- 1.95/drivers/usb/core/message.c     Thu Jun 24 16:43:27 2004
+++ edited/drivers/usb/core/message.c   Fri Jun 25 10:01:58 2004
@@ -607,10 +607,104 @@
  */
 int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char 
index, void *buf, int size)
 {
-       return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
-               USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
-               (USB_DT_STRING << 8) + index, langid, buf, size,
-               HZ * USB_CTRL_GET_TIMEOUT);
+       int i;
+       int result;
+
+       for (i = 0; i < 3; ++i) {
+               /* retry on length 0 or stall; some devices are flakey */
+               result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
+                       USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
+                       (USB_DT_STRING << 8) + index, langid, buf, size,
+                       HZ * USB_CTRL_GET_TIMEOUT);
+               if (!(result == 0 || result == -EPIPE))
+                       break;
+       }
+       return result;
+}
+
+/**
+ * usb_string - returns ISO 8859-1 version of a string descriptor
+ * @dev: the device whose string descriptor is being retrieved
+ * @index: the number of the descriptor
+ * @buf: where to put the string
+ * @size: how big is "buf"?
+ * Context: !in_interrupt ()
+ * 
+ * This converts the UTF-16LE encoded strings returned by devices, from
+ * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
+ * that are more usable in most kernel contexts.  Note that all characters
+ * in the chosen descriptor that can't be encoded using ISO-8859-1
+ * are converted to the question mark ("?") character, and this function
+ * chooses strings in the first language supported by the device.
+ *
+ * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
+ * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
+ * and is appropriate for use many uses of English and several other
+ * Western European languages.  (But it doesn't include the "Euro" symbol.)
+ *
+ * This call is synchronous, and may not be used in an interrupt context.
+ *
+ * Returns length of the string (>= 0) or usb_control_msg status (< 0).
+ */
+int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
+{
+       unsigned char *tbuf;
+       int err;
+       unsigned int u, idx;
+
+       if (size <= 0 || !buf || !index)
+               return -EINVAL;
+       buf[0] = 0;
+       tbuf = kmalloc(256, GFP_KERNEL);
+       if (!tbuf)
+               return -ENOMEM;
+
+       /* get langid for strings if it's not yet known */
+       if (!dev->have_langid) {
+               err = usb_get_descriptor(dev, USB_DT_STRING, 0, tbuf, 255);
+               if (err < 0) {
+                       dev_err (&dev->dev,
+                               "string descriptor 0 read error: %d\n",
+                               err);
+                       goto errout;
+               } else if (err < 4 || tbuf[0] < 4) {
+                       dev_err (&dev->dev, "string descriptor 0 too short\n");
+                       err = -EINVAL;
+                       goto errout;
+               } else {
+                       dev->have_langid = -1;
+                       dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
+                               /* always use the first langid listed */
+                       dev_dbg (&dev->dev, "default language 0x%04x\n",
+                               dev->string_langid);
+               }
+       }
+       
+       err = usb_get_string(dev, dev->string_langid, index, tbuf, 255);
+       if (err < 0)
+               goto errout;
+       if (tbuf[0] < 2) {
+               err = -EINVAL;
+               goto errout;
+       }
+       if (tbuf[0] < err)      /* be conservative with buggy devices */
+               err = tbuf[0];
+
+       size--;         /* leave room for trailing NULL char in output buffer */
+       for (idx = 0, u = 2; u < err; u += 2) {
+               if (idx >= size)
+                       break;
+               if (tbuf[u+1])                  /* high byte */
+                       buf[idx++] = '?';  /* non ISO-8859-1 character */
+               else
+                       buf[idx++] = tbuf[u];
+       }
+       buf[idx] = 0;
+       err = idx;
+
+ errout:
+       kfree(tbuf);
+       return err;
 }
 
 /**
@@ -1245,102 +1339,6 @@
        }
 
        return ret;
-}
-
-/**
- * usb_string - returns ISO 8859-1 version of a string descriptor
- * @dev: the device whose string descriptor is being retrieved
- * @index: the number of the descriptor
- * @buf: where to put the string
- * @size: how big is "buf"?
- * Context: !in_interrupt ()
- * 
- * This converts the UTF-16LE encoded strings returned by devices, from
- * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
- * that are more usable in most kernel contexts.  Note that all characters
- * in the chosen descriptor that can't be encoded using ISO-8859-1
- * are converted to the question mark ("?") character, and this function
- * chooses strings in the first language supported by the device.
- *
- * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
- * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
- * and is appropriate for use many uses of English and several other
- * Western European languages.  (But it doesn't include the "Euro" symbol.)
- *
- * This call is synchronous, and may not be used in an interrupt context.
- *
- * Returns length of the string (>= 0) or usb_control_msg status (< 0).
- */
-int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
-{
-       unsigned char *tbuf;
-       int err, len;
-       unsigned int u, idx;
-
-       if (size <= 0 || !buf || !index)
-               return -EINVAL;
-       buf[0] = 0;
-       tbuf = kmalloc(256, GFP_KERNEL);
-       if (!tbuf)
-               return -ENOMEM;
-
-       /* get langid for strings if it's not yet known */
-       if (!dev->have_langid) {
-               err = usb_get_descriptor(dev, USB_DT_STRING, 0, tbuf, 4);
-               if (err < 0) {
-                       dev_err (&dev->dev,
-                               "string descriptor 0 read error: %d\n",
-                               err);
-                       goto errout;
-               } else if (err < 4 || tbuf[0] < 4) {
-                       dev_err (&dev->dev, "string descriptor 0 too short\n");
-                       err = -EINVAL;
-                       goto errout;
-               } else {
-                       dev->have_langid = -1;
-                       dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
-                               /* always use the first langid listed */
-                       dev_dbg (&dev->dev, "default language 0x%04x\n",
-                               dev->string_langid);
-               }
-       }
-
-       /*
-        * ask for the length of the string 
-        */
-
-       err = usb_get_string(dev, dev->string_langid, index, tbuf, 2);
-       if (err == -EPIPE || err == 0) {
-               dev_dbg(&dev->dev, "RETRY string %d read/%d\n", index, 2);
-               err = usb_get_string(dev, dev->string_langid, index, tbuf, 2);
-       }
-       if(err<2)
-               goto errout;
-       len=tbuf[0];    
-       
-       err = usb_get_string(dev, dev->string_langid, index, tbuf, len);
-       if (err == -EPIPE || err == 0) {
-               dev_dbg(&dev->dev, "RETRY string %d read/%d\n", index, len);
-               err = usb_get_string(dev, dev->string_langid, index, tbuf, len);
-       }
-       if (err < 0)
-               goto errout;
-
-       size--;         /* leave room for trailing NULL char in output buffer */
-       for (idx = 0, u = 2; u < err; u += 2) {
-               if (idx >= size)
-                       break;
-               if (tbuf[u+1])                  /* high byte */
-                       buf[idx++] = '?';  /* non ISO-8859-1 character */
-               else
-                       buf[idx++] = tbuf[u];
-       }
-       buf[idx] = 0;
-       err = idx;
-
- errout:
-       kfree(tbuf);
-       return err;
 }
 
 // synchronous request completion model




-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
[EMAIL PROTECTED]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel

Reply via email to