aboutsummaryrefslogtreecommitdiff
path: root/lib/usb/usb_standard.c
diff options
context:
space:
mode:
authorAndrey Smirnov2012-11-06 16:46:55 -0800
committerAndrey Smirnov2012-11-06 16:46:55 -0800
commit7a5da60e2669c57d7b615aabe16ab851606f8bf1 (patch)
treebd28597a5b20d7f21379cd3640898ac58a57d3a3 /lib/usb/usb_standard.c
parent74405de4a5a1beab4023731de41c9f6e1c7f69a4 (diff)
Change USB strings handling code
This commit add an extra field to the _usbd_device, that allows to keep track of the number of USB strings which allows simplify boundaries checking code in usb_standard_get_descriptor. This commit also changes the index base for strings in usb_standard_get_descriptor which allows to get rid of necessity to have a dummy one-character string in a strings array.
Diffstat (limited to 'lib/usb/usb_standard.c')
-rw-r--r--lib/usb/usb_standard.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/usb/usb_standard.c b/lib/usb/usb_standard.c
index 2d7c619..1c8b952 100644
--- a/lib/usb/usb_standard.c
+++ b/lib/usb/usb_standard.c
@@ -90,7 +90,7 @@ static u16 build_config_descriptor(u8 index, u8 *buf, u16 len)
static int usb_standard_get_descriptor(struct usb_setup_data *req,
u8 **buf, u16 *len)
{
- int i;
+ int i, index;
struct usb_string_descriptor *sd;
switch (req->wValue >> 8) {
@@ -105,16 +105,20 @@ static int usb_standard_get_descriptor(struct usb_setup_data *req,
case USB_DT_STRING:
sd = (struct usb_string_descriptor *)_usbd_device.ctrl_buf;
+ /* Send sane Language ID descriptor... */
+ if ((req->wValue & 0xff) == 0)
+ sd->wData[0] = 0x409;
+
+ index = (req->wValue & 0xff) - 1;
+
if (!_usbd_device.strings)
return 0; /* Device doesn't support strings. */
/* Check that string index is in range. */
- for (i = 0; i <= (req->wValue & 0xff); i++)
- if (_usbd_device.strings[i] == NULL)
- return 0;
+ if (index >= _usbd_device.num_strings)
+ return 0;
- sd->bLength = strlen(_usbd_device.strings[req->wValue & 0xff])
- * 2 + 2;
+ sd->bLength = strlen(_usbd_device.strings[index]) * 2 + 2;
sd->bDescriptorType = USB_DT_STRING;
*buf = (u8 *)sd;
@@ -122,11 +126,7 @@ static int usb_standard_get_descriptor(struct usb_setup_data *req,
for (i = 0; i < (*len / 2) - 1; i++)
sd->wData[i] =
- _usbd_device.strings[req->wValue & 0xff][i];
-
- /* Send sane Language ID descriptor... */
- if ((req->wValue & 0xff) == 0)
- sd->wData[0] = 0x409;
+ _usbd_device.strings[index][i];
return 1;
}