Commit | Line | Data |
---|---|---|
1da177e4 | 1 | #include <linux/usb.h> |
615ae11b | 2 | #include <linux/usb/ch9.h> |
27729aad | 3 | #include <linux/usb/hcd.h> |
317149c6 | 4 | #include <linux/usb/quirks.h> |
1da177e4 LT |
5 | #include <linux/module.h> |
6 | #include <linux/init.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/device.h> | |
9 | #include <asm/byteorder.h> | |
6d5e8254 | 10 | #include "usb.h" |
27729aad | 11 | |
1da177e4 LT |
12 | |
13 | #define USB_MAXALTSETTING 128 /* Hard limit */ | |
14 | #define USB_MAXENDPOINTS 30 /* Hard limit */ | |
15 | ||
16 | #define USB_MAXCONFIG 8 /* Arbitrary limit */ | |
17 | ||
18 | ||
19 | static inline const char *plural(int n) | |
20 | { | |
21 | return (n == 1 ? "" : "s"); | |
22 | } | |
23 | ||
24 | static int find_next_descriptor(unsigned char *buffer, int size, | |
25 | int dt1, int dt2, int *num_skipped) | |
26 | { | |
27 | struct usb_descriptor_header *h; | |
28 | int n = 0; | |
29 | unsigned char *buffer0 = buffer; | |
30 | ||
31 | /* Find the next descriptor of type dt1 or dt2 */ | |
32 | while (size > 0) { | |
33 | h = (struct usb_descriptor_header *) buffer; | |
34 | if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2) | |
35 | break; | |
36 | buffer += h->bLength; | |
37 | size -= h->bLength; | |
38 | ++n; | |
39 | } | |
40 | ||
41 | /* Store the number of descriptors skipped and return the | |
42 | * number of bytes skipped */ | |
43 | if (num_skipped) | |
44 | *num_skipped = n; | |
45 | return buffer - buffer0; | |
46 | } | |
47 | ||
842f1690 | 48 | static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno, |
663c30d0 | 49 | int inum, int asnum, struct usb_host_endpoint *ep, |
842f1690 | 50 | unsigned char *buffer, int size) |
663c30d0 | 51 | { |
842f1690 | 52 | struct usb_ss_ep_comp_descriptor *desc; |
663c30d0 | 53 | int max_tx; |
663c30d0 | 54 | |
842f1690 AS |
55 | /* The SuperSpeed endpoint companion descriptor is supposed to |
56 | * be the first thing immediately following the endpoint descriptor. | |
57 | */ | |
f0058c62 | 58 | desc = (struct usb_ss_ep_comp_descriptor *) buffer; |
842f1690 AS |
59 | if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP || |
60 | size < USB_DT_SS_EP_COMP_SIZE) { | |
663c30d0 SS |
61 | dev_warn(ddev, "No SuperSpeed endpoint companion for config %d " |
62 | " interface %d altsetting %d ep %d: " | |
63 | "using minimum values\n", | |
64 | cfgno, inum, asnum, ep->desc.bEndpointAddress); | |
842f1690 AS |
65 | |
66 | /* Fill in some default values. | |
67 | * Leave bmAttributes as zero, which will mean no streams for | |
68 | * bulk, and isoc won't support multiple bursts of packets. | |
69 | * With bursts of only one packet, and a Mult of 1, the max | |
70 | * amount of data moved per endpoint service interval is one | |
71 | * packet. | |
663c30d0 | 72 | */ |
842f1690 AS |
73 | ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE; |
74 | ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP; | |
75 | if (usb_endpoint_xfer_isoc(&ep->desc) || | |
76 | usb_endpoint_xfer_int(&ep->desc)) | |
77 | ep->ss_ep_comp.wBytesPerInterval = | |
78 | ep->desc.wMaxPacketSize; | |
79 | return; | |
663c30d0 | 80 | } |
663c30d0 | 81 | |
842f1690 | 82 | memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE); |
663c30d0 SS |
83 | |
84 | /* Check the various values */ | |
85 | if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) { | |
86 | dev_warn(ddev, "Control endpoint with bMaxBurst = %d in " | |
87 | "config %d interface %d altsetting %d ep %d: " | |
88 | "setting to zero\n", desc->bMaxBurst, | |
89 | cfgno, inum, asnum, ep->desc.bEndpointAddress); | |
842f1690 AS |
90 | ep->ss_ep_comp.bMaxBurst = 0; |
91 | } else if (desc->bMaxBurst > 15) { | |
663c30d0 SS |
92 | dev_warn(ddev, "Endpoint with bMaxBurst = %d in " |
93 | "config %d interface %d altsetting %d ep %d: " | |
94 | "setting to 15\n", desc->bMaxBurst, | |
95 | cfgno, inum, asnum, ep->desc.bEndpointAddress); | |
842f1690 | 96 | ep->ss_ep_comp.bMaxBurst = 15; |
663c30d0 | 97 | } |
842f1690 AS |
98 | |
99 | if ((usb_endpoint_xfer_control(&ep->desc) || | |
100 | usb_endpoint_xfer_int(&ep->desc)) && | |
101 | desc->bmAttributes != 0) { | |
663c30d0 SS |
102 | dev_warn(ddev, "%s endpoint with bmAttributes = %d in " |
103 | "config %d interface %d altsetting %d ep %d: " | |
104 | "setting to zero\n", | |
105 | usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk", | |
106 | desc->bmAttributes, | |
107 | cfgno, inum, asnum, ep->desc.bEndpointAddress); | |
842f1690 AS |
108 | ep->ss_ep_comp.bmAttributes = 0; |
109 | } else if (usb_endpoint_xfer_bulk(&ep->desc) && | |
110 | desc->bmAttributes > 16) { | |
663c30d0 SS |
111 | dev_warn(ddev, "Bulk endpoint with more than 65536 streams in " |
112 | "config %d interface %d altsetting %d ep %d: " | |
113 | "setting to max\n", | |
114 | cfgno, inum, asnum, ep->desc.bEndpointAddress); | |
842f1690 AS |
115 | ep->ss_ep_comp.bmAttributes = 16; |
116 | } else if (usb_endpoint_xfer_isoc(&ep->desc) && | |
560fac95 | 117 | USB_SS_MULT(desc->bmAttributes) > 3) { |
663c30d0 SS |
118 | dev_warn(ddev, "Isoc endpoint has Mult of %d in " |
119 | "config %d interface %d altsetting %d ep %d: " | |
7541d74e BH |
120 | "setting to 3\n", |
121 | USB_SS_MULT(desc->bmAttributes), | |
663c30d0 | 122 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
842f1690 | 123 | ep->ss_ep_comp.bmAttributes = 2; |
663c30d0 | 124 | } |
842f1690 AS |
125 | |
126 | if (usb_endpoint_xfer_isoc(&ep->desc)) | |
560fac95 MN |
127 | max_tx = (desc->bMaxBurst + 1) * |
128 | (USB_SS_MULT(desc->bmAttributes)) * | |
29cc8897 | 129 | usb_endpoint_maxp(&ep->desc); |
842f1690 | 130 | else if (usb_endpoint_xfer_int(&ep->desc)) |
29cc8897 | 131 | max_tx = usb_endpoint_maxp(&ep->desc) * |
7de7c7d2 | 132 | (desc->bMaxBurst + 1); |
842f1690 AS |
133 | else |
134 | max_tx = 999999; | |
64b3c304 | 135 | if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) { |
663c30d0 SS |
136 | dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in " |
137 | "config %d interface %d altsetting %d ep %d: " | |
138 | "setting to %d\n", | |
139 | usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int", | |
7de7c7d2 | 140 | le16_to_cpu(desc->wBytesPerInterval), |
663c30d0 SS |
141 | cfgno, inum, asnum, ep->desc.bEndpointAddress, |
142 | max_tx); | |
7de7c7d2 | 143 | ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx); |
663c30d0 | 144 | } |
663c30d0 SS |
145 | } |
146 | ||
2226a520 AS |
147 | static const unsigned short low_speed_maxpacket_maxes[4] = { |
148 | [USB_ENDPOINT_XFER_CONTROL] = 8, | |
149 | [USB_ENDPOINT_XFER_ISOC] = 0, | |
150 | [USB_ENDPOINT_XFER_BULK] = 0, | |
151 | [USB_ENDPOINT_XFER_INT] = 8, | |
152 | }; | |
153 | static const unsigned short full_speed_maxpacket_maxes[4] = { | |
154 | [USB_ENDPOINT_XFER_CONTROL] = 64, | |
155 | [USB_ENDPOINT_XFER_ISOC] = 1023, | |
156 | [USB_ENDPOINT_XFER_BULK] = 64, | |
157 | [USB_ENDPOINT_XFER_INT] = 64, | |
158 | }; | |
159 | static const unsigned short high_speed_maxpacket_maxes[4] = { | |
160 | [USB_ENDPOINT_XFER_CONTROL] = 64, | |
161 | [USB_ENDPOINT_XFER_ISOC] = 1024, | |
162 | [USB_ENDPOINT_XFER_BULK] = 512, | |
bb9a7c51 | 163 | [USB_ENDPOINT_XFER_INT] = 1024, |
2226a520 AS |
164 | }; |
165 | static const unsigned short super_speed_maxpacket_maxes[4] = { | |
166 | [USB_ENDPOINT_XFER_CONTROL] = 512, | |
167 | [USB_ENDPOINT_XFER_ISOC] = 1024, | |
168 | [USB_ENDPOINT_XFER_BULK] = 1024, | |
169 | [USB_ENDPOINT_XFER_INT] = 1024, | |
170 | }; | |
171 | ||
1da177e4 LT |
172 | static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum, |
173 | int asnum, struct usb_host_interface *ifp, int num_ep, | |
174 | unsigned char *buffer, int size) | |
175 | { | |
176 | unsigned char *buffer0 = buffer; | |
177 | struct usb_endpoint_descriptor *d; | |
178 | struct usb_host_endpoint *endpoint; | |
663c30d0 | 179 | int n, i, j, retval; |
2226a520 AS |
180 | unsigned int maxp; |
181 | const unsigned short *maxpacket_maxes; | |
1da177e4 LT |
182 | |
183 | d = (struct usb_endpoint_descriptor *) buffer; | |
184 | buffer += d->bLength; | |
185 | size -= d->bLength; | |
186 | ||
187 | if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE) | |
188 | n = USB_DT_ENDPOINT_AUDIO_SIZE; | |
189 | else if (d->bLength >= USB_DT_ENDPOINT_SIZE) | |
190 | n = USB_DT_ENDPOINT_SIZE; | |
191 | else { | |
192 | dev_warn(ddev, "config %d interface %d altsetting %d has an " | |
193 | "invalid endpoint descriptor of length %d, skipping\n", | |
194 | cfgno, inum, asnum, d->bLength); | |
195 | goto skip_to_next_endpoint_or_interface_descriptor; | |
196 | } | |
197 | ||
198 | i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK; | |
199 | if (i >= 16 || i == 0) { | |
200 | dev_warn(ddev, "config %d interface %d altsetting %d has an " | |
201 | "invalid endpoint with address 0x%X, skipping\n", | |
202 | cfgno, inum, asnum, d->bEndpointAddress); | |
203 | goto skip_to_next_endpoint_or_interface_descriptor; | |
204 | } | |
205 | ||
206 | /* Only store as many endpoints as we have room for */ | |
207 | if (ifp->desc.bNumEndpoints >= num_ep) | |
208 | goto skip_to_next_endpoint_or_interface_descriptor; | |
209 | ||
210 | endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints]; | |
211 | ++ifp->desc.bNumEndpoints; | |
212 | ||
213 | memcpy(&endpoint->desc, d, n); | |
214 | INIT_LIST_HEAD(&endpoint->urb_list); | |
215 | ||
300871cd LP |
216 | /* Fix up bInterval values outside the legal range. Use 32 ms if no |
217 | * proper value can be guessed. */ | |
615ae11b AS |
218 | i = 0; /* i = min, j = max, n = default */ |
219 | j = 255; | |
220 | if (usb_endpoint_xfer_int(d)) { | |
221 | i = 1; | |
222 | switch (to_usb_device(ddev)->speed) { | |
6b403b02 | 223 | case USB_SPEED_SUPER: |
615ae11b | 224 | case USB_SPEED_HIGH: |
300871cd LP |
225 | /* Many device manufacturers are using full-speed |
226 | * bInterval values in high-speed interrupt endpoint | |
227 | * descriptors. Try to fix those and fall back to a | |
228 | * 32 ms default value otherwise. */ | |
229 | n = fls(d->bInterval*8); | |
230 | if (n == 0) | |
231 | n = 9; /* 32 ms = 2^(9-1) uframes */ | |
615ae11b AS |
232 | j = 16; |
233 | break; | |
234 | default: /* USB_SPEED_FULL or _LOW */ | |
235 | /* For low-speed, 10 ms is the official minimum. | |
236 | * But some "overclocked" devices might want faster | |
237 | * polling so we'll allow it. */ | |
238 | n = 32; | |
239 | break; | |
240 | } | |
241 | } else if (usb_endpoint_xfer_isoc(d)) { | |
242 | i = 1; | |
243 | j = 16; | |
244 | switch (to_usb_device(ddev)->speed) { | |
245 | case USB_SPEED_HIGH: | |
246 | n = 9; /* 32 ms = 2^(9-1) uframes */ | |
247 | break; | |
248 | default: /* USB_SPEED_FULL */ | |
249 | n = 6; /* 32 ms = 2^(6-1) frames */ | |
250 | break; | |
251 | } | |
252 | } | |
253 | if (d->bInterval < i || d->bInterval > j) { | |
254 | dev_warn(ddev, "config %d interface %d altsetting %d " | |
255 | "endpoint 0x%X has an invalid bInterval %d, " | |
256 | "changing to %d\n", | |
257 | cfgno, inum, asnum, | |
258 | d->bEndpointAddress, d->bInterval, n); | |
259 | endpoint->desc.bInterval = n; | |
260 | } | |
261 | ||
60aac1ec AS |
262 | /* Some buggy low-speed devices have Bulk endpoints, which is |
263 | * explicitly forbidden by the USB spec. In an attempt to make | |
264 | * them usable, we will try treating them as Interrupt endpoints. | |
265 | */ | |
266 | if (to_usb_device(ddev)->speed == USB_SPEED_LOW && | |
267 | usb_endpoint_xfer_bulk(d)) { | |
268 | dev_warn(ddev, "config %d interface %d altsetting %d " | |
269 | "endpoint 0x%X is Bulk; changing to Interrupt\n", | |
270 | cfgno, inum, asnum, d->bEndpointAddress); | |
271 | endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT; | |
272 | endpoint->desc.bInterval = 1; | |
29cc8897 | 273 | if (usb_endpoint_maxp(&endpoint->desc) > 8) |
60aac1ec AS |
274 | endpoint->desc.wMaxPacketSize = cpu_to_le16(8); |
275 | } | |
276 | ||
2226a520 AS |
277 | /* Validate the wMaxPacketSize field */ |
278 | maxp = usb_endpoint_maxp(&endpoint->desc); | |
279 | ||
280 | /* Find the highest legal maxpacket size for this endpoint */ | |
281 | i = 0; /* additional transactions per microframe */ | |
282 | switch (to_usb_device(ddev)->speed) { | |
283 | case USB_SPEED_LOW: | |
284 | maxpacket_maxes = low_speed_maxpacket_maxes; | |
285 | break; | |
286 | case USB_SPEED_FULL: | |
287 | maxpacket_maxes = full_speed_maxpacket_maxes; | |
288 | break; | |
289 | case USB_SPEED_HIGH: | |
290 | /* Bits 12..11 are allowed only for HS periodic endpoints */ | |
291 | if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) { | |
292 | i = maxp & (BIT(12) | BIT(11)); | |
293 | maxp &= ~i; | |
294 | } | |
295 | /* fallthrough */ | |
296 | default: | |
297 | maxpacket_maxes = high_speed_maxpacket_maxes; | |
298 | break; | |
299 | case USB_SPEED_SUPER: | |
300 | maxpacket_maxes = super_speed_maxpacket_maxes; | |
301 | break; | |
302 | } | |
303 | j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)]; | |
304 | ||
305 | if (maxp > j) { | |
306 | dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n", | |
307 | cfgno, inum, asnum, d->bEndpointAddress, maxp, j); | |
308 | maxp = j; | |
309 | endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp); | |
310 | } | |
311 | ||
caa9ef67 DB |
312 | /* |
313 | * Some buggy high speed devices have bulk endpoints using | |
314 | * maxpacket sizes other than 512. High speed HCDs may not | |
315 | * be able to handle that particular bug, so let's warn... | |
316 | */ | |
317 | if (to_usb_device(ddev)->speed == USB_SPEED_HIGH | |
318 | && usb_endpoint_xfer_bulk(d)) { | |
caa9ef67 DB |
319 | if (maxp != 512) |
320 | dev_warn(ddev, "config %d interface %d altsetting %d " | |
321 | "bulk endpoint 0x%X has invalid maxpacket %d\n", | |
322 | cfgno, inum, asnum, d->bEndpointAddress, | |
323 | maxp); | |
324 | } | |
663c30d0 | 325 | |
842f1690 AS |
326 | /* Parse a possible SuperSpeed endpoint companion descriptor */ |
327 | if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) | |
328 | usb_parse_ss_endpoint_companion(ddev, cfgno, | |
329 | inum, asnum, endpoint, buffer, size); | |
9f8e4438 | 330 | |
842f1690 AS |
331 | /* Skip over any Class Specific or Vendor Specific descriptors; |
332 | * find the next endpoint or interface descriptor */ | |
333 | endpoint->extra = buffer; | |
334 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, | |
335 | USB_DT_INTERFACE, &n); | |
336 | endpoint->extralen = i; | |
337 | retval = buffer - buffer0 + i; | |
1da177e4 LT |
338 | if (n > 0) |
339 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", | |
340 | n, plural(n), "endpoint"); | |
663c30d0 | 341 | return retval; |
1da177e4 LT |
342 | |
343 | skip_to_next_endpoint_or_interface_descriptor: | |
344 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, | |
345 | USB_DT_INTERFACE, NULL); | |
346 | return buffer - buffer0 + i; | |
347 | } | |
348 | ||
349 | void usb_release_interface_cache(struct kref *ref) | |
350 | { | |
351 | struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref); | |
352 | int j; | |
353 | ||
4f62efe6 AS |
354 | for (j = 0; j < intfc->num_altsetting; j++) { |
355 | struct usb_host_interface *alt = &intfc->altsetting[j]; | |
356 | ||
357 | kfree(alt->endpoint); | |
358 | kfree(alt->string); | |
359 | } | |
1da177e4 LT |
360 | kfree(intfc); |
361 | } | |
362 | ||
363 | static int usb_parse_interface(struct device *ddev, int cfgno, | |
364 | struct usb_host_config *config, unsigned char *buffer, int size, | |
365 | u8 inums[], u8 nalts[]) | |
366 | { | |
367 | unsigned char *buffer0 = buffer; | |
368 | struct usb_interface_descriptor *d; | |
369 | int inum, asnum; | |
370 | struct usb_interface_cache *intfc; | |
371 | struct usb_host_interface *alt; | |
372 | int i, n; | |
373 | int len, retval; | |
374 | int num_ep, num_ep_orig; | |
375 | ||
376 | d = (struct usb_interface_descriptor *) buffer; | |
377 | buffer += d->bLength; | |
378 | size -= d->bLength; | |
379 | ||
380 | if (d->bLength < USB_DT_INTERFACE_SIZE) | |
381 | goto skip_to_next_interface_descriptor; | |
382 | ||
383 | /* Which interface entry is this? */ | |
384 | intfc = NULL; | |
385 | inum = d->bInterfaceNumber; | |
386 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { | |
387 | if (inums[i] == inum) { | |
388 | intfc = config->intf_cache[i]; | |
389 | break; | |
390 | } | |
391 | } | |
392 | if (!intfc || intfc->num_altsetting >= nalts[i]) | |
393 | goto skip_to_next_interface_descriptor; | |
394 | ||
395 | /* Check for duplicate altsetting entries */ | |
396 | asnum = d->bAlternateSetting; | |
397 | for ((i = 0, alt = &intfc->altsetting[0]); | |
398 | i < intfc->num_altsetting; | |
399 | (++i, ++alt)) { | |
400 | if (alt->desc.bAlternateSetting == asnum) { | |
401 | dev_warn(ddev, "Duplicate descriptor for config %d " | |
402 | "interface %d altsetting %d, skipping\n", | |
403 | cfgno, inum, asnum); | |
404 | goto skip_to_next_interface_descriptor; | |
405 | } | |
406 | } | |
407 | ||
408 | ++intfc->num_altsetting; | |
409 | memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE); | |
410 | ||
411 | /* Skip over any Class Specific or Vendor Specific descriptors; | |
412 | * find the first endpoint or interface descriptor */ | |
413 | alt->extra = buffer; | |
414 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, | |
415 | USB_DT_INTERFACE, &n); | |
416 | alt->extralen = i; | |
417 | if (n > 0) | |
418 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", | |
419 | n, plural(n), "interface"); | |
420 | buffer += i; | |
421 | size -= i; | |
422 | ||
423 | /* Allocate space for the right(?) number of endpoints */ | |
424 | num_ep = num_ep_orig = alt->desc.bNumEndpoints; | |
2c044a48 | 425 | alt->desc.bNumEndpoints = 0; /* Use as a counter */ |
1da177e4 LT |
426 | if (num_ep > USB_MAXENDPOINTS) { |
427 | dev_warn(ddev, "too many endpoints for config %d interface %d " | |
428 | "altsetting %d: %d, using maximum allowed: %d\n", | |
429 | cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS); | |
430 | num_ep = USB_MAXENDPOINTS; | |
431 | } | |
432 | ||
2c044a48 GKH |
433 | if (num_ep > 0) { |
434 | /* Can't allocate 0 bytes */ | |
57a21c1b AS |
435 | len = sizeof(struct usb_host_endpoint) * num_ep; |
436 | alt->endpoint = kzalloc(len, GFP_KERNEL); | |
437 | if (!alt->endpoint) | |
438 | return -ENOMEM; | |
439 | } | |
1da177e4 LT |
440 | |
441 | /* Parse all the endpoint descriptors */ | |
442 | n = 0; | |
443 | while (size > 0) { | |
444 | if (((struct usb_descriptor_header *) buffer)->bDescriptorType | |
445 | == USB_DT_INTERFACE) | |
446 | break; | |
447 | retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt, | |
448 | num_ep, buffer, size); | |
449 | if (retval < 0) | |
450 | return retval; | |
451 | ++n; | |
452 | ||
453 | buffer += retval; | |
454 | size -= retval; | |
455 | } | |
456 | ||
457 | if (n != num_ep_orig) | |
458 | dev_warn(ddev, "config %d interface %d altsetting %d has %d " | |
459 | "endpoint descriptor%s, different from the interface " | |
460 | "descriptor's value: %d\n", | |
461 | cfgno, inum, asnum, n, plural(n), num_ep_orig); | |
462 | return buffer - buffer0; | |
463 | ||
464 | skip_to_next_interface_descriptor: | |
465 | i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, | |
466 | USB_DT_INTERFACE, NULL); | |
467 | return buffer - buffer0 + i; | |
468 | } | |
469 | ||
317149c6 | 470 | static int usb_parse_configuration(struct usb_device *dev, int cfgidx, |
1da177e4 LT |
471 | struct usb_host_config *config, unsigned char *buffer, int size) |
472 | { | |
317149c6 | 473 | struct device *ddev = &dev->dev; |
1da177e4 LT |
474 | unsigned char *buffer0 = buffer; |
475 | int cfgno; | |
476 | int nintf, nintf_orig; | |
477 | int i, j, n; | |
478 | struct usb_interface_cache *intfc; | |
479 | unsigned char *buffer2; | |
480 | int size2; | |
481 | struct usb_descriptor_header *header; | |
482 | int len, retval; | |
483 | u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES]; | |
165fe97e | 484 | unsigned iad_num = 0; |
1da177e4 LT |
485 | |
486 | memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE); | |
487 | if (config->desc.bDescriptorType != USB_DT_CONFIG || | |
ef7198be HG |
488 | config->desc.bLength < USB_DT_CONFIG_SIZE || |
489 | config->desc.bLength > size) { | |
1da177e4 LT |
490 | dev_err(ddev, "invalid descriptor for config index %d: " |
491 | "type = 0x%X, length = %d\n", cfgidx, | |
492 | config->desc.bDescriptorType, config->desc.bLength); | |
493 | return -EINVAL; | |
494 | } | |
495 | cfgno = config->desc.bConfigurationValue; | |
496 | ||
497 | buffer += config->desc.bLength; | |
498 | size -= config->desc.bLength; | |
499 | ||
500 | nintf = nintf_orig = config->desc.bNumInterfaces; | |
501 | if (nintf > USB_MAXINTERFACES) { | |
502 | dev_warn(ddev, "config %d has too many interfaces: %d, " | |
503 | "using maximum allowed: %d\n", | |
504 | cfgno, nintf, USB_MAXINTERFACES); | |
505 | nintf = USB_MAXINTERFACES; | |
506 | } | |
507 | ||
508 | /* Go through the descriptors, checking their length and counting the | |
509 | * number of altsettings for each interface */ | |
510 | n = 0; | |
511 | for ((buffer2 = buffer, size2 = size); | |
512 | size2 > 0; | |
513 | (buffer2 += header->bLength, size2 -= header->bLength)) { | |
514 | ||
515 | if (size2 < sizeof(struct usb_descriptor_header)) { | |
516 | dev_warn(ddev, "config %d descriptor has %d excess " | |
517 | "byte%s, ignoring\n", | |
518 | cfgno, size2, plural(size2)); | |
519 | break; | |
520 | } | |
521 | ||
522 | header = (struct usb_descriptor_header *) buffer2; | |
523 | if ((header->bLength > size2) || (header->bLength < 2)) { | |
524 | dev_warn(ddev, "config %d has an invalid descriptor " | |
525 | "of length %d, skipping remainder of the config\n", | |
526 | cfgno, header->bLength); | |
527 | break; | |
528 | } | |
529 | ||
530 | if (header->bDescriptorType == USB_DT_INTERFACE) { | |
531 | struct usb_interface_descriptor *d; | |
532 | int inum; | |
533 | ||
534 | d = (struct usb_interface_descriptor *) header; | |
535 | if (d->bLength < USB_DT_INTERFACE_SIZE) { | |
536 | dev_warn(ddev, "config %d has an invalid " | |
537 | "interface descriptor of length %d, " | |
538 | "skipping\n", cfgno, d->bLength); | |
539 | continue; | |
540 | } | |
541 | ||
542 | inum = d->bInterfaceNumber; | |
317149c6 HG |
543 | |
544 | if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) && | |
545 | n >= nintf_orig) { | |
546 | dev_warn(ddev, "config %d has more interface " | |
547 | "descriptors, than it declares in " | |
548 | "bNumInterfaces, ignoring interface " | |
549 | "number: %d\n", cfgno, inum); | |
550 | continue; | |
551 | } | |
552 | ||
1da177e4 LT |
553 | if (inum >= nintf_orig) |
554 | dev_warn(ddev, "config %d has an invalid " | |
555 | "interface number: %d but max is %d\n", | |
556 | cfgno, inum, nintf_orig - 1); | |
557 | ||
558 | /* Have we already encountered this interface? | |
559 | * Count its altsettings */ | |
560 | for (i = 0; i < n; ++i) { | |
561 | if (inums[i] == inum) | |
562 | break; | |
563 | } | |
564 | if (i < n) { | |
565 | if (nalts[i] < 255) | |
566 | ++nalts[i]; | |
567 | } else if (n < USB_MAXINTERFACES) { | |
568 | inums[n] = inum; | |
569 | nalts[n] = 1; | |
570 | ++n; | |
571 | } | |
572 | ||
165fe97e CN |
573 | } else if (header->bDescriptorType == |
574 | USB_DT_INTERFACE_ASSOCIATION) { | |
575 | if (iad_num == USB_MAXIADS) { | |
576 | dev_warn(ddev, "found more Interface " | |
577 | "Association Descriptors " | |
578 | "than allocated for in " | |
579 | "configuration %d\n", cfgno); | |
580 | } else { | |
581 | config->intf_assoc[iad_num] = | |
582 | (struct usb_interface_assoc_descriptor | |
583 | *)header; | |
584 | iad_num++; | |
585 | } | |
586 | ||
1da177e4 LT |
587 | } else if (header->bDescriptorType == USB_DT_DEVICE || |
588 | header->bDescriptorType == USB_DT_CONFIG) | |
589 | dev_warn(ddev, "config %d contains an unexpected " | |
590 | "descriptor of type 0x%X, skipping\n", | |
591 | cfgno, header->bDescriptorType); | |
592 | ||
593 | } /* for ((buffer2 = buffer, size2 = size); ...) */ | |
594 | size = buffer2 - buffer; | |
595 | config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0); | |
596 | ||
597 | if (n != nintf) | |
598 | dev_warn(ddev, "config %d has %d interface%s, different from " | |
599 | "the descriptor's value: %d\n", | |
600 | cfgno, n, plural(n), nintf_orig); | |
601 | else if (n == 0) | |
602 | dev_warn(ddev, "config %d has no interfaces?\n", cfgno); | |
603 | config->desc.bNumInterfaces = nintf = n; | |
604 | ||
605 | /* Check for missing interface numbers */ | |
606 | for (i = 0; i < nintf; ++i) { | |
607 | for (j = 0; j < nintf; ++j) { | |
608 | if (inums[j] == i) | |
609 | break; | |
610 | } | |
611 | if (j >= nintf) | |
612 | dev_warn(ddev, "config %d has no interface number " | |
613 | "%d\n", cfgno, i); | |
614 | } | |
615 | ||
616 | /* Allocate the usb_interface_caches and altsetting arrays */ | |
617 | for (i = 0; i < nintf; ++i) { | |
618 | j = nalts[i]; | |
619 | if (j > USB_MAXALTSETTING) { | |
620 | dev_warn(ddev, "too many alternate settings for " | |
621 | "config %d interface %d: %d, " | |
622 | "using maximum allowed: %d\n", | |
623 | cfgno, inums[i], j, USB_MAXALTSETTING); | |
624 | nalts[i] = j = USB_MAXALTSETTING; | |
625 | } | |
626 | ||
627 | len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j; | |
0a1ef3b5 | 628 | config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL); |
1da177e4 LT |
629 | if (!intfc) |
630 | return -ENOMEM; | |
1da177e4 LT |
631 | kref_init(&intfc->ref); |
632 | } | |
633 | ||
663c30d0 SS |
634 | /* FIXME: parse the BOS descriptor */ |
635 | ||
1da177e4 LT |
636 | /* Skip over any Class Specific or Vendor Specific descriptors; |
637 | * find the first interface descriptor */ | |
638 | config->extra = buffer; | |
639 | i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, | |
640 | USB_DT_INTERFACE, &n); | |
641 | config->extralen = i; | |
642 | if (n > 0) | |
643 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", | |
644 | n, plural(n), "configuration"); | |
645 | buffer += i; | |
646 | size -= i; | |
647 | ||
648 | /* Parse all the interface/altsetting descriptors */ | |
649 | while (size > 0) { | |
650 | retval = usb_parse_interface(ddev, cfgno, config, | |
651 | buffer, size, inums, nalts); | |
652 | if (retval < 0) | |
653 | return retval; | |
654 | ||
655 | buffer += retval; | |
656 | size -= retval; | |
657 | } | |
658 | ||
659 | /* Check for missing altsettings */ | |
660 | for (i = 0; i < nintf; ++i) { | |
661 | intfc = config->intf_cache[i]; | |
662 | for (j = 0; j < intfc->num_altsetting; ++j) { | |
663 | for (n = 0; n < intfc->num_altsetting; ++n) { | |
664 | if (intfc->altsetting[n].desc. | |
665 | bAlternateSetting == j) | |
666 | break; | |
667 | } | |
668 | if (n >= intfc->num_altsetting) | |
669 | dev_warn(ddev, "config %d interface %d has no " | |
670 | "altsetting %d\n", cfgno, inums[i], j); | |
671 | } | |
672 | } | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
2c044a48 GKH |
677 | /* hub-only!! ... and only exported for reset/reinit path. |
678 | * otherwise used internally on disconnect/destroy path | |
679 | */ | |
1da177e4 LT |
680 | void usb_destroy_configuration(struct usb_device *dev) |
681 | { | |
682 | int c, i; | |
683 | ||
684 | if (!dev->config) | |
685 | return; | |
686 | ||
687 | if (dev->rawdescriptors) { | |
688 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) | |
689 | kfree(dev->rawdescriptors[i]); | |
690 | ||
691 | kfree(dev->rawdescriptors); | |
692 | dev->rawdescriptors = NULL; | |
693 | } | |
694 | ||
695 | for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { | |
696 | struct usb_host_config *cf = &dev->config[c]; | |
697 | ||
698 | kfree(cf->string); | |
1da177e4 LT |
699 | for (i = 0; i < cf->desc.bNumInterfaces; i++) { |
700 | if (cf->intf_cache[i]) | |
2c044a48 | 701 | kref_put(&cf->intf_cache[i]->ref, |
1da177e4 LT |
702 | usb_release_interface_cache); |
703 | } | |
704 | } | |
705 | kfree(dev->config); | |
706 | dev->config = NULL; | |
707 | } | |
708 | ||
709 | ||
1145065c IPG |
710 | /* |
711 | * Get the USB config descriptors, cache and parse'em | |
712 | * | |
713 | * hub-only!! ... and only in reset path, or usb_new_device() | |
714 | * (used by real hubs and virtual root hubs) | |
1145065c | 715 | */ |
1da177e4 LT |
716 | int usb_get_configuration(struct usb_device *dev) |
717 | { | |
718 | struct device *ddev = &dev->dev; | |
719 | int ncfg = dev->descriptor.bNumConfigurations; | |
1145065c | 720 | int result = 0; |
1da177e4 | 721 | unsigned int cfgno, length; |
1da177e4 | 722 | unsigned char *bigbuffer; |
2c044a48 | 723 | struct usb_config_descriptor *desc; |
1da177e4 | 724 | |
1145065c | 725 | cfgno = 0; |
1145065c | 726 | result = -ENOMEM; |
1da177e4 LT |
727 | if (ncfg > USB_MAXCONFIG) { |
728 | dev_warn(ddev, "too many configurations: %d, " | |
729 | "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG); | |
730 | dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG; | |
731 | } | |
732 | ||
733 | if (ncfg < 1) { | |
734 | dev_err(ddev, "no configurations\n"); | |
735 | return -EINVAL; | |
736 | } | |
737 | ||
738 | length = ncfg * sizeof(struct usb_host_config); | |
0a1ef3b5 | 739 | dev->config = kzalloc(length, GFP_KERNEL); |
1da177e4 LT |
740 | if (!dev->config) |
741 | goto err2; | |
1da177e4 LT |
742 | |
743 | length = ncfg * sizeof(char *); | |
0a1ef3b5 | 744 | dev->rawdescriptors = kzalloc(length, GFP_KERNEL); |
1da177e4 LT |
745 | if (!dev->rawdescriptors) |
746 | goto err2; | |
1da177e4 | 747 | |
e8f4af30 MN |
748 | desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL); |
749 | if (!desc) | |
1da177e4 | 750 | goto err2; |
1da177e4 | 751 | |
1145065c IPG |
752 | result = 0; |
753 | for (; cfgno < ncfg; cfgno++) { | |
1da177e4 LT |
754 | /* We grab just the first descriptor so we know how long |
755 | * the whole configuration is */ | |
756 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, | |
e8f4af30 | 757 | desc, USB_DT_CONFIG_SIZE); |
1da177e4 LT |
758 | if (result < 0) { |
759 | dev_err(ddev, "unable to read config index %d " | |
1145065c | 760 | "descriptor/%s: %d\n", cfgno, "start", result); |
3a22b872 LT |
761 | if (result != -EPIPE) |
762 | goto err; | |
cb4c8fe5 IPG |
763 | dev_err(ddev, "chopping to %d config(s)\n", cfgno); |
764 | dev->descriptor.bNumConfigurations = cfgno; | |
765 | break; | |
1da177e4 LT |
766 | } else if (result < 4) { |
767 | dev_err(ddev, "config index %d descriptor too short " | |
768 | "(expected %i, got %i)\n", cfgno, | |
769 | USB_DT_CONFIG_SIZE, result); | |
770 | result = -EINVAL; | |
771 | goto err; | |
772 | } | |
773 | length = max((int) le16_to_cpu(desc->wTotalLength), | |
774 | USB_DT_CONFIG_SIZE); | |
775 | ||
776 | /* Now that we know the length, get the whole thing */ | |
777 | bigbuffer = kmalloc(length, GFP_KERNEL); | |
778 | if (!bigbuffer) { | |
779 | result = -ENOMEM; | |
780 | goto err; | |
781 | } | |
3ec34db2 JW |
782 | |
783 | if (dev->quirks & USB_QUIRK_DELAY_INIT) | |
784 | msleep(100); | |
785 | ||
1da177e4 LT |
786 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, |
787 | bigbuffer, length); | |
788 | if (result < 0) { | |
789 | dev_err(ddev, "unable to read config index %d " | |
790 | "descriptor/%s\n", cfgno, "all"); | |
791 | kfree(bigbuffer); | |
792 | goto err; | |
793 | } | |
794 | if (result < length) { | |
795 | dev_warn(ddev, "config index %d descriptor too short " | |
796 | "(expected %i, got %i)\n", cfgno, length, result); | |
797 | length = result; | |
798 | } | |
799 | ||
800 | dev->rawdescriptors[cfgno] = bigbuffer; | |
801 | ||
317149c6 | 802 | result = usb_parse_configuration(dev, cfgno, |
1da177e4 LT |
803 | &dev->config[cfgno], bigbuffer, length); |
804 | if (result < 0) { | |
805 | ++cfgno; | |
806 | goto err; | |
807 | } | |
808 | } | |
809 | result = 0; | |
810 | ||
811 | err: | |
e8f4af30 | 812 | kfree(desc); |
1da177e4 LT |
813 | dev->descriptor.bNumConfigurations = cfgno; |
814 | err2: | |
815 | if (result == -ENOMEM) | |
816 | dev_err(ddev, "out of memory\n"); | |
817 | return result; | |
818 | } | |
3148bf04 AX |
819 | |
820 | void usb_release_bos_descriptor(struct usb_device *dev) | |
821 | { | |
822 | if (dev->bos) { | |
823 | kfree(dev->bos->desc); | |
824 | kfree(dev->bos); | |
825 | dev->bos = NULL; | |
826 | } | |
827 | } | |
828 | ||
829 | /* Get BOS descriptor set */ | |
830 | int usb_get_bos_descriptor(struct usb_device *dev) | |
831 | { | |
832 | struct device *ddev = &dev->dev; | |
833 | struct usb_bos_descriptor *bos; | |
834 | struct usb_dev_cap_header *cap; | |
835 | unsigned char *buffer; | |
836 | int length, total_len, num, i; | |
837 | int ret; | |
838 | ||
839 | bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL); | |
840 | if (!bos) | |
841 | return -ENOMEM; | |
842 | ||
843 | /* Get BOS descriptor */ | |
844 | ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE); | |
845 | if (ret < USB_DT_BOS_SIZE) { | |
846 | dev_err(ddev, "unable to get BOS descriptor\n"); | |
847 | if (ret >= 0) | |
848 | ret = -ENOMSG; | |
849 | kfree(bos); | |
850 | return ret; | |
851 | } | |
852 | ||
853 | length = bos->bLength; | |
854 | total_len = le16_to_cpu(bos->wTotalLength); | |
855 | num = bos->bNumDeviceCaps; | |
856 | kfree(bos); | |
857 | if (total_len < length) | |
858 | return -EINVAL; | |
859 | ||
860 | dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL); | |
861 | if (!dev->bos) | |
862 | return -ENOMEM; | |
863 | ||
864 | /* Now let's get the whole BOS descriptor set */ | |
865 | buffer = kzalloc(total_len, GFP_KERNEL); | |
866 | if (!buffer) { | |
867 | ret = -ENOMEM; | |
868 | goto err; | |
869 | } | |
870 | dev->bos->desc = (struct usb_bos_descriptor *)buffer; | |
871 | ||
872 | ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len); | |
873 | if (ret < total_len) { | |
874 | dev_err(ddev, "unable to get BOS descriptor set\n"); | |
875 | if (ret >= 0) | |
876 | ret = -ENOMSG; | |
877 | goto err; | |
878 | } | |
879 | total_len -= length; | |
880 | ||
881 | for (i = 0; i < num; i++) { | |
882 | buffer += length; | |
883 | cap = (struct usb_dev_cap_header *)buffer; | |
884 | length = cap->bLength; | |
885 | ||
886 | if (total_len < length) | |
887 | break; | |
888 | total_len -= length; | |
889 | ||
890 | if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) { | |
891 | dev_warn(ddev, "descriptor type invalid, skip\n"); | |
892 | continue; | |
893 | } | |
894 | ||
895 | switch (cap->bDevCapabilityType) { | |
896 | case USB_CAP_TYPE_WIRELESS_USB: | |
897 | /* Wireless USB cap descriptor is handled by wusb */ | |
898 | break; | |
899 | case USB_CAP_TYPE_EXT: | |
900 | dev->bos->ext_cap = | |
901 | (struct usb_ext_cap_descriptor *)buffer; | |
902 | break; | |
903 | case USB_SS_CAP_TYPE: | |
904 | dev->bos->ss_cap = | |
905 | (struct usb_ss_cap_descriptor *)buffer; | |
906 | break; | |
907 | case CONTAINER_ID_TYPE: | |
908 | dev->bos->ss_id = | |
909 | (struct usb_ss_container_id_descriptor *)buffer; | |
910 | break; | |
911 | default: | |
912 | break; | |
913 | } | |
914 | } | |
915 | ||
916 | return 0; | |
917 | ||
918 | err: | |
919 | usb_release_bos_descriptor(dev); | |
920 | return ret; | |
921 | } |