1 #include <linux/config.h>
3 #ifdef CONFIG_USB_DEBUG
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <asm/byteorder.h>
15 #define USB_MAXALTSETTING 128 /* Hard limit */
16 #define USB_MAXENDPOINTS 30 /* Hard limit */
18 #define USB_MAXCONFIG 8 /* Arbitrary limit */
21 static int find_next_descriptor(unsigned char *buffer, int size,
22 int dt1, int dt2, int *num_skipped)
24 struct usb_descriptor_header *h;
26 unsigned char *buffer0 = buffer;
28 /* Find the next descriptor of type dt1 or dt2 */
29 while (size >= sizeof(struct usb_descriptor_header)) {
30 h = (struct usb_descriptor_header *) buffer;
31 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
38 /* Store the number of descriptors skipped and return the
39 * number of bytes skipped */
42 return buffer - buffer0;
45 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
46 int asnum, struct usb_host_endpoint *endpoint,
47 unsigned char *buffer, int size)
49 unsigned char *buffer0 = buffer;
50 struct usb_descriptor_header *header;
53 header = (struct usb_descriptor_header *)buffer;
54 if (header->bDescriptorType != USB_DT_ENDPOINT) {
55 dev_err(ddev, "config %d interface %d altsetting %d has an "
56 "unexpected descriptor of type 0x%X, "
57 "expecting endpoint type 0x%X\n",
59 header->bDescriptorType, USB_DT_ENDPOINT);
63 if (header->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
64 memcpy(&endpoint->desc, buffer, USB_DT_ENDPOINT_AUDIO_SIZE);
65 else if (header->bLength >= USB_DT_ENDPOINT_SIZE)
66 memcpy(&endpoint->desc, buffer, USB_DT_ENDPOINT_SIZE);
68 dev_err(ddev, "config %d interface %d altsetting %d has an "
69 "invalid endpoint descriptor of length %d\n",
70 cfgno, inum, asnum, header->bLength);
74 i = endpoint->desc.bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
75 if (i >= 16 || i == 0) {
76 dev_err(ddev, "config %d interface %d altsetting %d has an "
77 "invalid endpoint with address 0x%X\n",
78 cfgno, inum, asnum, endpoint->desc.bEndpointAddress);
82 le16_to_cpus(&endpoint->desc.wMaxPacketSize);
84 buffer += header->bLength;
85 size -= header->bLength;
87 /* Skip over any Class Specific or Vendor Specific descriptors;
88 * find the next endpoint or interface descriptor */
89 endpoint->extra = buffer;
90 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
91 USB_DT_INTERFACE, &n);
92 endpoint->extralen = i;
94 dev_dbg(ddev, "skipped %d class/vendor specific endpoint "
96 return buffer - buffer0 + i;
99 static void usb_free_intf(struct usb_interface *intf)
103 if (intf->altsetting) {
104 for (j = 0; j < intf->num_altsetting; j++) {
105 struct usb_host_interface *alt = &intf->altsetting[j];
107 kfree(alt->endpoint);
109 kfree(intf->altsetting);
114 static int usb_parse_interface(struct device *ddev, int cfgno,
115 struct usb_host_config *config, unsigned char *buffer, int size)
117 unsigned char *buffer0 = buffer;
118 struct usb_interface_descriptor *d;
120 struct usb_interface *interface;
121 struct usb_host_interface *alt;
125 d = (struct usb_interface_descriptor *) buffer;
126 buffer += d->bLength;
129 if (d->bDescriptorType != USB_DT_INTERFACE) {
130 dev_err(ddev, "config %d has an unexpected descriptor of type "
131 "0x%X, expecting interface type 0x%X\n",
132 cfgno, d->bDescriptorType, USB_DT_INTERFACE);
136 inum = d->bInterfaceNumber;
137 if (inum >= config->desc.bNumInterfaces)
138 goto skip_to_next_interface_descriptor;
140 interface = config->interface[inum];
141 asnum = d->bAlternateSetting;
142 if (asnum >= interface->num_altsetting) {
143 dev_err(ddev, "config %d interface %d has an invalid "
144 "alternate setting number: %d but max is %d\n",
145 cfgno, inum, asnum, interface->num_altsetting - 1);
149 alt = &interface->altsetting[asnum];
150 if (alt->desc.bLength) {
151 dev_err(ddev, "Duplicate descriptor for config %d "
152 "interface %d altsetting %d\n", cfgno, inum, asnum);
155 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
157 /* Skip over any Class Specific or Vendor Specific descriptors;
158 * find the first endpoint or interface descriptor */
160 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
161 USB_DT_INTERFACE, &n);
164 dev_dbg(ddev, "skipped %d class/vendor specific "
165 "interface descriptors\n", n);
169 if (alt->desc.bNumEndpoints > USB_MAXENDPOINTS) {
170 dev_err(ddev, "too many endpoints for config %d interface %d "
171 "altsetting %d: %d, maximum allowed: %d\n",
172 cfgno, inum, asnum, alt->desc.bNumEndpoints,
177 len = alt->desc.bNumEndpoints * sizeof(struct usb_host_endpoint);
178 alt->endpoint = kmalloc(len, GFP_KERNEL);
181 memset(alt->endpoint, 0, len);
183 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
184 if (size < USB_DT_ENDPOINT_SIZE) {
185 dev_err(ddev, "too few endpoint descriptors for "
186 "config %d interface %d altsetting %d\n",
191 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum,
192 alt->endpoint + i, buffer, size);
199 return buffer - buffer0;
201 skip_to_next_interface_descriptor:
202 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
203 USB_DT_INTERFACE, NULL);
204 return buffer - buffer0 + i;
207 int usb_parse_configuration(struct device *ddev, int cfgidx,
208 struct usb_host_config *config, unsigned char *buffer, int size)
211 int nintf, nintf_orig;
213 struct usb_interface *interface;
214 unsigned char *buffer2;
216 struct usb_descriptor_header *header;
219 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
220 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
221 config->desc.bLength < USB_DT_CONFIG_SIZE) {
222 dev_err(ddev, "invalid descriptor for config index %d: "
223 "type = 0x%X, length = %d\n", cfgidx,
224 config->desc.bDescriptorType, config->desc.bLength);
227 config->desc.wTotalLength = size;
228 cfgno = config->desc.bConfigurationValue;
230 buffer += config->desc.bLength;
231 size -= config->desc.bLength;
233 nintf = nintf_orig = config->desc.bNumInterfaces;
234 if (nintf > USB_MAXINTERFACES) {
235 dev_warn(ddev, "config %d has too many interfaces: %d, "
236 "using maximum allowed: %d\n",
237 cfgno, nintf, USB_MAXINTERFACES);
238 config->desc.bNumInterfaces = nintf = USB_MAXINTERFACES;
241 for (i = 0; i < nintf; ++i) {
242 interface = config->interface[i] =
243 kmalloc(sizeof(struct usb_interface), GFP_KERNEL);
246 memset(interface, 0, sizeof(struct usb_interface));
249 /* Go through the descriptors, checking their length and counting the
250 * number of altsettings for each interface */
251 for ((buffer2 = buffer, size2 = size);
252 size2 >= sizeof(struct usb_descriptor_header);
253 (buffer2 += header->bLength, size2 -= header->bLength)) {
255 header = (struct usb_descriptor_header *) buffer2;
256 if ((header->bLength > size2) || (header->bLength < 2)) {
257 dev_err(ddev, "config %d has an invalid descriptor "
258 "of length %d\n", cfgno, header->bLength);
262 if (header->bDescriptorType == USB_DT_INTERFACE) {
263 struct usb_interface_descriptor *d;
265 d = (struct usb_interface_descriptor *) header;
266 if (d->bLength < USB_DT_INTERFACE_SIZE) {
267 dev_err(ddev, "config %d has an invalid "
268 "interface descriptor of length %d\n",
273 i = d->bInterfaceNumber;
274 if (i >= nintf_orig) {
275 dev_err(ddev, "config %d has an invalid "
276 "interface number: %d but max is %d\n",
277 cfgno, i, nintf_orig - 1);
281 ++config->interface[i]->num_altsetting;
283 } else if (header->bDescriptorType == USB_DT_DEVICE ||
284 header->bDescriptorType == USB_DT_CONFIG) {
285 dev_err(ddev, "config %d contains an unexpected "
286 "descriptor of type 0x%X\n",
287 cfgno, header->bDescriptorType);
291 } /* for ((buffer2 = buffer, size2 = size); ...) */
293 /* Allocate the altsetting arrays */
294 for (i = 0; i < nintf; ++i) {
295 interface = config->interface[i];
296 if (interface->num_altsetting > USB_MAXALTSETTING) {
297 dev_err(ddev, "too many alternate settings for "
298 "config %d interface %d: %d, "
299 "maximum allowed: %d\n",
300 cfgno, i, interface->num_altsetting,
304 if (interface->num_altsetting == 0) {
305 dev_err(ddev, "config %d has no interface number "
310 len = sizeof(*interface->altsetting) *
311 interface->num_altsetting;
312 interface->altsetting = kmalloc(len, GFP_KERNEL);
313 if (!interface->altsetting)
315 memset(interface->altsetting, 0, len);
318 /* Skip over any Class Specific or Vendor Specific descriptors;
319 * find the first interface descriptor */
320 config->extra = buffer;
321 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
322 USB_DT_INTERFACE, &n);
323 config->extralen = i;
325 dev_dbg(ddev, "skipped %d class/vendor specific "
326 "configuration descriptors\n", n);
330 /* Parse all the interface/altsetting descriptors */
331 while (size >= sizeof(struct usb_descriptor_header)) {
332 retval = usb_parse_interface(ddev, cfgno, config,
341 /* Check for missing altsettings */
342 for (i = 0; i < nintf; ++i) {
343 interface = config->interface[i];
344 for (j = 0; j < interface->num_altsetting; ++j) {
345 if (!interface->altsetting[j].desc.bLength) {
346 dev_err(ddev, "config %d interface %d has no "
347 "altsetting %d\n", cfgno, i, j);
356 // hub-only!! ... and only exported for reset/reinit path.
357 // otherwise used internally on disconnect/destroy path
358 void usb_destroy_configuration(struct usb_device *dev)
365 if (dev->rawdescriptors) {
366 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
367 kfree(dev->rawdescriptors[i]);
369 kfree(dev->rawdescriptors);
370 dev->rawdescriptors = 0;
373 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
374 struct usb_host_config *cf = &dev->config[c];
376 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
377 struct usb_interface *ifp = cf->interface[i];
388 // hub-only!! ... and only in reset path, or usb_new_device()
389 // (used by real hubs and virtual root hubs)
390 int usb_get_configuration(struct usb_device *dev)
392 struct device *ddev = &dev->dev;
393 int ncfg = dev->descriptor.bNumConfigurations;
394 int result = -ENOMEM;
395 unsigned int cfgno, length;
396 unsigned char *buffer;
397 unsigned char *bigbuffer;
398 struct usb_config_descriptor *desc;
400 if (ncfg > USB_MAXCONFIG) {
401 dev_warn(ddev, "too many configurations: %d, "
402 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
403 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
407 dev_err(ddev, "no configurations\n");
411 length = ncfg * sizeof(struct usb_host_config);
412 dev->config = kmalloc(length, GFP_KERNEL);
415 memset(dev->config, 0, length);
417 length = ncfg * sizeof(char *);
418 dev->rawdescriptors = kmalloc(length, GFP_KERNEL);
419 if (!dev->rawdescriptors)
421 memset(dev->rawdescriptors, 0, length);
423 buffer = kmalloc(8, GFP_KERNEL);
426 desc = (struct usb_config_descriptor *)buffer;
428 for (cfgno = 0; cfgno < ncfg; cfgno++) {
429 /* We grab the first 8 bytes so we know how long the whole */
430 /* configuration is */
431 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
434 dev_err(ddev, "unable to read config index %d "
435 "descriptor\n", cfgno);
437 } else if (result < 8) {
438 dev_err(ddev, "config index %d descriptor too short "
439 "(expected %i, got %i)\n", cfgno, 8, result);
443 length = max((int) le16_to_cpu(desc->wTotalLength),
446 /* Now that we know the length, get the whole thing */
447 bigbuffer = kmalloc(length, GFP_KERNEL);
452 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
455 dev_err(ddev, "unable to read config index %d "
456 "descriptor\n", cfgno);
460 if (result < length) {
461 dev_err(ddev, "config index %d descriptor too short "
462 "(expected %i, got %i)\n", cfgno, length, result);
468 dev->rawdescriptors[cfgno] = bigbuffer;
470 result = usb_parse_configuration(&dev->dev, cfgno,
471 &dev->config[cfgno], bigbuffer, length);
473 dev_dbg(ddev, "config index %d descriptor has %d "
474 "excess byte(s)\n", cfgno, result);
475 else if (result < 0) {
484 dev->descriptor.bNumConfigurations = cfgno;
486 if (result == -ENOMEM)
487 dev_err(ddev, "out of memory\n");