Diff markup
>> 1 #include <linux/config.h>
>> 2
>> 3 #ifdef CONFIG_USB_DEBUG
>> 4 #define DEBUG
>> 5 #endif
>> 6
1 #include <linux/usb.h> 7 #include <linux/usb.h>
2 #include <linux/usb/ch9.h> <<
3 #include <linux/module.h> 8 #include <linux/module.h>
4 #include <linux/init.h> 9 #include <linux/init.h>
5 #include <linux/slab.h> 10 #include <linux/slab.h>
6 #include <linux/device.h> 11 #include <linux/device.h>
7 #include <asm/byteorder.h> 12 #include <asm/byteorder.h>
8 #include "usb.h" !! 13
9 #include "hcd.h" <<
10 14
11 #define USB_MAXALTSETTING 128 15 #define USB_MAXALTSETTING 128 /* Hard limit */
12 #define USB_MAXENDPOINTS 30 16 #define USB_MAXENDPOINTS 30 /* Hard limit */
13 17
14 #define USB_MAXCONFIG 8 18 #define USB_MAXCONFIG 8 /* Arbitrary limit */
15 19
16 20
17 static inline const char *plural(int n) 21 static inline const char *plural(int n)
18 { 22 {
19 return (n == 1 ? "" : "s"); 23 return (n == 1 ? "" : "s");
20 } 24 }
21 25
22 static int find_next_descriptor(unsigned char 26 static int find_next_descriptor(unsigned char *buffer, int size,
23 int dt1, int dt2, int *num_skipped) 27 int dt1, int dt2, int *num_skipped)
24 { 28 {
25 struct usb_descriptor_header *h; 29 struct usb_descriptor_header *h;
26 int n = 0; 30 int n = 0;
27 unsigned char *buffer0 = buffer; 31 unsigned char *buffer0 = buffer;
28 32
29 /* Find the next descriptor of type dt 33 /* Find the next descriptor of type dt1 or dt2 */
30 while (size > 0) { 34 while (size > 0) {
31 h = (struct usb_descriptor_hea 35 h = (struct usb_descriptor_header *) buffer;
32 if (h->bDescriptorType == dt1 36 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
33 break; 37 break;
34 buffer += h->bLength; 38 buffer += h->bLength;
35 size -= h->bLength; 39 size -= h->bLength;
36 ++n; 40 ++n;
37 } 41 }
38 42
39 /* Store the number of descriptors ski 43 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */ 44 * number of bytes skipped */
41 if (num_skipped) 45 if (num_skipped)
42 *num_skipped = n; 46 *num_skipped = n;
43 return buffer - buffer0; 47 return buffer - buffer0;
44 } 48 }
45 49
46 static int usb_parse_endpoint(struct device *d 50 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
47 int asnum, struct usb_host_interface *ifp, 51 int asnum, struct usb_host_interface *ifp, int num_ep,
48 unsigned char *buffer, int size) 52 unsigned char *buffer, int size)
49 { 53 {
50 unsigned char *buffer0 = buffer; 54 unsigned char *buffer0 = buffer;
51 struct usb_endpoint_descriptor *d; 55 struct usb_endpoint_descriptor *d;
52 struct usb_host_endpoint *endpoint; 56 struct usb_host_endpoint *endpoint;
53 int n, i, j; !! 57 int n, i;
54 58
55 d = (struct usb_endpoint_descriptor *) 59 d = (struct usb_endpoint_descriptor *) buffer;
56 buffer += d->bLength; 60 buffer += d->bLength;
57 size -= d->bLength; 61 size -= d->bLength;
58 62
59 if (d->bLength >= USB_DT_ENDPOINT_AUDI 63 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
60 n = USB_DT_ENDPOINT_AUDIO_SIZE 64 n = USB_DT_ENDPOINT_AUDIO_SIZE;
61 else if (d->bLength >= USB_DT_ENDPOINT 65 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
62 n = USB_DT_ENDPOINT_SIZE; 66 n = USB_DT_ENDPOINT_SIZE;
63 else { 67 else {
64 dev_warn(ddev, "config %d inte 68 dev_warn(ddev, "config %d interface %d altsetting %d has an "
65 "invalid endpoint descript 69 "invalid endpoint descriptor of length %d, skipping\n",
66 cfgno, inum, asnum, d->bLe 70 cfgno, inum, asnum, d->bLength);
67 goto skip_to_next_endpoint_or_ 71 goto skip_to_next_endpoint_or_interface_descriptor;
68 } 72 }
69 73
70 i = d->bEndpointAddress & ~USB_ENDPOIN 74 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
71 if (i >= 16 || i == 0) { 75 if (i >= 16 || i == 0) {
72 dev_warn(ddev, "config %d inte 76 dev_warn(ddev, "config %d interface %d altsetting %d has an "
73 "invalid endpoint with add 77 "invalid endpoint with address 0x%X, skipping\n",
74 cfgno, inum, asnum, d->bEn 78 cfgno, inum, asnum, d->bEndpointAddress);
75 goto skip_to_next_endpoint_or_ 79 goto skip_to_next_endpoint_or_interface_descriptor;
76 } 80 }
77 81
78 /* Only store as many endpoints as we 82 /* Only store as many endpoints as we have room for */
79 if (ifp->desc.bNumEndpoints >= num_ep) 83 if (ifp->desc.bNumEndpoints >= num_ep)
80 goto skip_to_next_endpoint_or_ 84 goto skip_to_next_endpoint_or_interface_descriptor;
81 85
82 endpoint = &ifp->endpoint[ifp->desc.bN 86 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
83 ++ifp->desc.bNumEndpoints; 87 ++ifp->desc.bNumEndpoints;
84 88
85 memcpy(&endpoint->desc, d, n); 89 memcpy(&endpoint->desc, d, n);
86 INIT_LIST_HEAD(&endpoint->urb_list); 90 INIT_LIST_HEAD(&endpoint->urb_list);
87 91
88 /* Fix up bInterval values outside the <<
89 * proper value can be guessed. */ <<
90 i = 0; /* i = min, j = max, n <<
91 j = 255; <<
92 if (usb_endpoint_xfer_int(d)) { <<
93 i = 1; <<
94 switch (to_usb_device(ddev)->s <<
95 case USB_SPEED_HIGH: <<
96 /* Many device manufac <<
97 * bInterval values in <<
98 * descriptors. Try to <<
99 * 32 ms default value <<
100 n = fls(d->bInterval*8 <<
101 if (n == 0) <<
102 n = 9; /* 32 <<
103 j = 16; <<
104 break; <<
105 default: /* USB <<
106 /* For low-speed, 10 m <<
107 * But some "overclock <<
108 * polling so we'll al <<
109 n = 32; <<
110 break; <<
111 } <<
112 } else if (usb_endpoint_xfer_isoc(d)) <<
113 i = 1; <<
114 j = 16; <<
115 switch (to_usb_device(ddev)->s <<
116 case USB_SPEED_HIGH: <<
117 n = 9; /* 32 <<
118 break; <<
119 default: /* USB <<
120 n = 6; /* 32 <<
121 break; <<
122 } <<
123 } <<
124 if (d->bInterval < i || d->bInterval > <<
125 dev_warn(ddev, "config %d inte <<
126 "endpoint 0x%X has an inva <<
127 "changing to %d\n", <<
128 cfgno, inum, asnum, <<
129 d->bEndpointAddress, d->bI <<
130 endpoint->desc.bInterval = n; <<
131 } <<
132 <<
133 /* Some buggy low-speed devices have B <<
134 * explicitly forbidden by the USB spe <<
135 * them usable, we will try treating t <<
136 */ <<
137 if (to_usb_device(ddev)->speed == USB_ <<
138 usb_endpoint_xfer_bulk <<
139 dev_warn(ddev, "config %d inte <<
140 "endpoint 0x%X is Bulk; ch <<
141 cfgno, inum, asnum, d->bEn <<
142 endpoint->desc.bmAttributes = <<
143 endpoint->desc.bInterval = 1; <<
144 if (le16_to_cpu(endpoint->desc <<
145 endpoint->desc.wMaxPac <<
146 } <<
147 <<
148 /* Skip over any Class Specific or Ven 92 /* Skip over any Class Specific or Vendor Specific descriptors;
149 * find the next endpoint or interface 93 * find the next endpoint or interface descriptor */
150 endpoint->extra = buffer; 94 endpoint->extra = buffer;
151 i = find_next_descriptor(buffer, size, 95 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
152 USB_DT_INTERFACE, &n); 96 USB_DT_INTERFACE, &n);
153 endpoint->extralen = i; 97 endpoint->extralen = i;
154 if (n > 0) 98 if (n > 0)
155 dev_dbg(ddev, "skipped %d desc 99 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
156 n, plural(n), "endpoint"); 100 n, plural(n), "endpoint");
157 return buffer - buffer0 + i; 101 return buffer - buffer0 + i;
158 102
159 skip_to_next_endpoint_or_interface_descriptor: 103 skip_to_next_endpoint_or_interface_descriptor:
160 i = find_next_descriptor(buffer, size, 104 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
161 USB_DT_INTERFACE, NULL); 105 USB_DT_INTERFACE, NULL);
162 return buffer - buffer0 + i; 106 return buffer - buffer0 + i;
163 } 107 }
164 108
165 void usb_release_interface_cache(struct kref * 109 void usb_release_interface_cache(struct kref *ref)
166 { 110 {
167 struct usb_interface_cache *intfc = re 111 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
168 int j; 112 int j;
169 113
170 for (j = 0; j < intfc->num_altsetting; !! 114 for (j = 0; j < intfc->num_altsetting; j++)
171 struct usb_host_interface *alt !! 115 kfree(intfc->altsetting[j].endpoint);
172 <<
173 kfree(alt->endpoint); <<
174 kfree(alt->string); <<
175 } <<
176 kfree(intfc); 116 kfree(intfc);
177 } 117 }
178 118
179 static int usb_parse_interface(struct device * 119 static int usb_parse_interface(struct device *ddev, int cfgno,
180 struct usb_host_config *config, unsigned c 120 struct usb_host_config *config, unsigned char *buffer, int size,
181 u8 inums[], u8 nalts[]) 121 u8 inums[], u8 nalts[])
182 { 122 {
183 unsigned char *buffer0 = buffer; 123 unsigned char *buffer0 = buffer;
184 struct usb_interface_descriptor *d; 124 struct usb_interface_descriptor *d;
185 int inum, asnum; 125 int inum, asnum;
186 struct usb_interface_cache *intfc; 126 struct usb_interface_cache *intfc;
187 struct usb_host_interface *alt; 127 struct usb_host_interface *alt;
188 int i, n; 128 int i, n;
189 int len, retval; 129 int len, retval;
190 int num_ep, num_ep_orig; 130 int num_ep, num_ep_orig;
191 131
192 d = (struct usb_interface_descriptor * 132 d = (struct usb_interface_descriptor *) buffer;
193 buffer += d->bLength; 133 buffer += d->bLength;
194 size -= d->bLength; 134 size -= d->bLength;
195 135
196 if (d->bLength < USB_DT_INTERFACE_SIZE 136 if (d->bLength < USB_DT_INTERFACE_SIZE)
197 goto skip_to_next_interface_de 137 goto skip_to_next_interface_descriptor;
198 138
199 /* Which interface entry is this? */ 139 /* Which interface entry is this? */
200 intfc = NULL; 140 intfc = NULL;
201 inum = d->bInterfaceNumber; 141 inum = d->bInterfaceNumber;
202 for (i = 0; i < config->desc.bNumInter 142 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
203 if (inums[i] == inum) { 143 if (inums[i] == inum) {
204 intfc = config->intf_c 144 intfc = config->intf_cache[i];
205 break; 145 break;
206 } 146 }
207 } 147 }
208 if (!intfc || intfc->num_altsetting >= 148 if (!intfc || intfc->num_altsetting >= nalts[i])
209 goto skip_to_next_interface_de 149 goto skip_to_next_interface_descriptor;
210 150
211 /* Check for duplicate altsetting entr 151 /* Check for duplicate altsetting entries */
212 asnum = d->bAlternateSetting; 152 asnum = d->bAlternateSetting;
213 for ((i = 0, alt = &intfc->altsetting[ 153 for ((i = 0, alt = &intfc->altsetting[0]);
214 i < intfc->num_altsetting; 154 i < intfc->num_altsetting;
215 (++i, ++alt)) { 155 (++i, ++alt)) {
216 if (alt->desc.bAlternateSettin 156 if (alt->desc.bAlternateSetting == asnum) {
217 dev_warn(ddev, "Duplic 157 dev_warn(ddev, "Duplicate descriptor for config %d "
218 "interface %d alts 158 "interface %d altsetting %d, skipping\n",
219 cfgno, inum, asnum 159 cfgno, inum, asnum);
220 goto skip_to_next_inte 160 goto skip_to_next_interface_descriptor;
221 } 161 }
222 } 162 }
223 163
224 ++intfc->num_altsetting; 164 ++intfc->num_altsetting;
225 memcpy(&alt->desc, d, USB_DT_INTERFACE 165 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
226 166
227 /* Skip over any Class Specific or Ven 167 /* Skip over any Class Specific or Vendor Specific descriptors;
228 * find the first endpoint or interfac 168 * find the first endpoint or interface descriptor */
229 alt->extra = buffer; 169 alt->extra = buffer;
230 i = find_next_descriptor(buffer, size, 170 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
231 USB_DT_INTERFACE, &n); 171 USB_DT_INTERFACE, &n);
232 alt->extralen = i; 172 alt->extralen = i;
233 if (n > 0) 173 if (n > 0)
234 dev_dbg(ddev, "skipped %d desc 174 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
235 n, plural(n), "interface") 175 n, plural(n), "interface");
236 buffer += i; 176 buffer += i;
237 size -= i; 177 size -= i;
238 178
239 /* Allocate space for the right(?) num 179 /* Allocate space for the right(?) number of endpoints */
240 num_ep = num_ep_orig = alt->desc.bNumE 180 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
241 alt->desc.bNumEndpoints = 0; !! 181 alt->desc.bNumEndpoints = 0; // Use as a counter
242 if (num_ep > USB_MAXENDPOINTS) { 182 if (num_ep > USB_MAXENDPOINTS) {
243 dev_warn(ddev, "too many endpo 183 dev_warn(ddev, "too many endpoints for config %d interface %d "
244 "altsetting %d: %d, using 184 "altsetting %d: %d, using maximum allowed: %d\n",
245 cfgno, inum, asnum, num_ep 185 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
246 num_ep = USB_MAXENDPOINTS; 186 num_ep = USB_MAXENDPOINTS;
247 } 187 }
248 188
249 if (num_ep > 0) { !! 189 len = sizeof(struct usb_host_endpoint) * num_ep;
250 /* Can't allocate 0 bytes */ !! 190 alt->endpoint = kmalloc(len, GFP_KERNEL);
251 len = sizeof(struct usb_host_e !! 191 if (!alt->endpoint)
252 alt->endpoint = kzalloc(len, G !! 192 return -ENOMEM;
253 if (!alt->endpoint) !! 193 memset(alt->endpoint, 0, len);
254 return -ENOMEM; <<
255 } <<
256 194
257 /* Parse all the endpoint descriptors 195 /* Parse all the endpoint descriptors */
258 n = 0; 196 n = 0;
259 while (size > 0) { 197 while (size > 0) {
260 if (((struct usb_descriptor_he 198 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
261 == USB_DT_INTERFACE) 199 == USB_DT_INTERFACE)
262 break; 200 break;
263 retval = usb_parse_endpoint(dd 201 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
264 num_ep, buffer, size); 202 num_ep, buffer, size);
265 if (retval < 0) 203 if (retval < 0)
266 return retval; 204 return retval;
267 ++n; 205 ++n;
268 206
269 buffer += retval; 207 buffer += retval;
270 size -= retval; 208 size -= retval;
271 } 209 }
272 210
273 if (n != num_ep_orig) 211 if (n != num_ep_orig)
274 dev_warn(ddev, "config %d inte 212 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
275 "endpoint descriptor%s, di 213 "endpoint descriptor%s, different from the interface "
276 "descriptor's value: %d\n" 214 "descriptor's value: %d\n",
277 cfgno, inum, asnum, n, plu 215 cfgno, inum, asnum, n, plural(n), num_ep_orig);
278 return buffer - buffer0; 216 return buffer - buffer0;
279 217
280 skip_to_next_interface_descriptor: 218 skip_to_next_interface_descriptor:
281 i = find_next_descriptor(buffer, size, 219 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
282 USB_DT_INTERFACE, NULL); 220 USB_DT_INTERFACE, NULL);
283 return buffer - buffer0 + i; 221 return buffer - buffer0 + i;
284 } 222 }
285 223
286 static int usb_parse_configuration(struct devi !! 224 int usb_parse_configuration(struct device *ddev, int cfgidx,
287 struct usb_host_config *config, unsigned c 225 struct usb_host_config *config, unsigned char *buffer, int size)
288 { 226 {
289 unsigned char *buffer0 = buffer; 227 unsigned char *buffer0 = buffer;
290 int cfgno; 228 int cfgno;
291 int nintf, nintf_orig; 229 int nintf, nintf_orig;
292 int i, j, n; 230 int i, j, n;
293 struct usb_interface_cache *intfc; 231 struct usb_interface_cache *intfc;
294 unsigned char *buffer2; 232 unsigned char *buffer2;
295 int size2; 233 int size2;
296 struct usb_descriptor_header *header; 234 struct usb_descriptor_header *header;
297 int len, retval; 235 int len, retval;
298 u8 inums[USB_MAXINTERFACES], nalts[USB 236 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
299 unsigned iad_num = 0; <<
300 237
301 memcpy(&config->desc, buffer, USB_DT_C 238 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
302 if (config->desc.bDescriptorType != US 239 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
303 config->desc.bLength < USB_DT_CONF 240 config->desc.bLength < USB_DT_CONFIG_SIZE) {
304 dev_err(ddev, "invalid descrip 241 dev_err(ddev, "invalid descriptor for config index %d: "
305 "type = 0x%X, length = %d\ 242 "type = 0x%X, length = %d\n", cfgidx,
306 config->desc.bDescriptorTy 243 config->desc.bDescriptorType, config->desc.bLength);
307 return -EINVAL; 244 return -EINVAL;
308 } 245 }
309 cfgno = config->desc.bConfigurationVal 246 cfgno = config->desc.bConfigurationValue;
310 247
311 buffer += config->desc.bLength; 248 buffer += config->desc.bLength;
312 size -= config->desc.bLength; 249 size -= config->desc.bLength;
313 250
314 nintf = nintf_orig = config->desc.bNum 251 nintf = nintf_orig = config->desc.bNumInterfaces;
315 if (nintf > USB_MAXINTERFACES) { 252 if (nintf > USB_MAXINTERFACES) {
316 dev_warn(ddev, "config %d has 253 dev_warn(ddev, "config %d has too many interfaces: %d, "
317 "using maximum allowed: %d 254 "using maximum allowed: %d\n",
318 cfgno, nintf, USB_MAXINTER 255 cfgno, nintf, USB_MAXINTERFACES);
319 nintf = USB_MAXINTERFACES; 256 nintf = USB_MAXINTERFACES;
320 } 257 }
321 258
322 /* Go through the descriptors, checkin 259 /* Go through the descriptors, checking their length and counting the
323 * number of altsettings for each inte 260 * number of altsettings for each interface */
324 n = 0; 261 n = 0;
325 for ((buffer2 = buffer, size2 = size); 262 for ((buffer2 = buffer, size2 = size);
326 size2 > 0; 263 size2 > 0;
327 (buffer2 += header->bLength, size 264 (buffer2 += header->bLength, size2 -= header->bLength)) {
328 265
329 if (size2 < sizeof(struct usb_ 266 if (size2 < sizeof(struct usb_descriptor_header)) {
330 dev_warn(ddev, "config 267 dev_warn(ddev, "config %d descriptor has %d excess "
331 "byte%s, ignoring\ 268 "byte%s, ignoring\n",
332 cfgno, size2, plur 269 cfgno, size2, plural(size2));
333 break; 270 break;
334 } 271 }
335 272
336 header = (struct usb_descripto 273 header = (struct usb_descriptor_header *) buffer2;
337 if ((header->bLength > size2) 274 if ((header->bLength > size2) || (header->bLength < 2)) {
338 dev_warn(ddev, "config 275 dev_warn(ddev, "config %d has an invalid descriptor "
339 "of length %d, ski 276 "of length %d, skipping remainder of the config\n",
340 cfgno, header->bLe 277 cfgno, header->bLength);
341 break; 278 break;
342 } 279 }
343 280
344 if (header->bDescriptorType == 281 if (header->bDescriptorType == USB_DT_INTERFACE) {
345 struct usb_interface_d 282 struct usb_interface_descriptor *d;
346 int inum; 283 int inum;
347 284
348 d = (struct usb_interf 285 d = (struct usb_interface_descriptor *) header;
349 if (d->bLength < USB_D 286 if (d->bLength < USB_DT_INTERFACE_SIZE) {
350 dev_warn(ddev, 287 dev_warn(ddev, "config %d has an invalid "
351 "interface 288 "interface descriptor of length %d, "
352 "skipping\ 289 "skipping\n", cfgno, d->bLength);
353 continue; 290 continue;
354 } 291 }
355 292
356 inum = d->bInterfaceNu 293 inum = d->bInterfaceNumber;
357 if (inum >= nintf_orig 294 if (inum >= nintf_orig)
358 dev_warn(ddev, 295 dev_warn(ddev, "config %d has an invalid "
359 "interface 296 "interface number: %d but max is %d\n",
360 cfgno, inu 297 cfgno, inum, nintf_orig - 1);
361 298
362 /* Have we already enc 299 /* Have we already encountered this interface?
363 * Count its altsettin 300 * Count its altsettings */
364 for (i = 0; i < n; ++i 301 for (i = 0; i < n; ++i) {
365 if (inums[i] = 302 if (inums[i] == inum)
366 break; 303 break;
367 } 304 }
368 if (i < n) { 305 if (i < n) {
369 if (nalts[i] < 306 if (nalts[i] < 255)
370 ++nalt 307 ++nalts[i];
371 } else if (n < USB_MAX 308 } else if (n < USB_MAXINTERFACES) {
372 inums[n] = inu 309 inums[n] = inum;
373 nalts[n] = 1; 310 nalts[n] = 1;
374 ++n; 311 ++n;
375 } 312 }
376 313
377 } else if (header->bDescriptor <<
378 USB_DT_INTERFA <<
379 if (iad_num == USB_MAX <<
380 dev_warn(ddev, <<
381 <<
382 <<
383 <<
384 } else { <<
385 config->intf_a <<
386 (struc <<
387 *)head <<
388 iad_num++; <<
389 } <<
390 <<
391 } else if (header->bDescriptor 314 } else if (header->bDescriptorType == USB_DT_DEVICE ||
392 header->bDescripto 315 header->bDescriptorType == USB_DT_CONFIG)
393 dev_warn(ddev, "config 316 dev_warn(ddev, "config %d contains an unexpected "
394 "descriptor of typ 317 "descriptor of type 0x%X, skipping\n",
395 cfgno, header->bDe 318 cfgno, header->bDescriptorType);
396 319
397 } /* for ((buffer2 = buffer, siz 320 } /* for ((buffer2 = buffer, size2 = size); ...) */
398 size = buffer2 - buffer; 321 size = buffer2 - buffer;
399 config->desc.wTotalLength = cpu_to_le1 322 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
400 323
401 if (n != nintf) 324 if (n != nintf)
402 dev_warn(ddev, "config %d has 325 dev_warn(ddev, "config %d has %d interface%s, different from "
403 "the descriptor's value: % 326 "the descriptor's value: %d\n",
404 cfgno, n, plural(n), nintf 327 cfgno, n, plural(n), nintf_orig);
405 else if (n == 0) 328 else if (n == 0)
406 dev_warn(ddev, "config %d has 329 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
407 config->desc.bNumInterfaces = nintf = 330 config->desc.bNumInterfaces = nintf = n;
408 331
409 /* Check for missing interface numbers 332 /* Check for missing interface numbers */
410 for (i = 0; i < nintf; ++i) { 333 for (i = 0; i < nintf; ++i) {
411 for (j = 0; j < nintf; ++j) { 334 for (j = 0; j < nintf; ++j) {
412 if (inums[j] == i) 335 if (inums[j] == i)
413 break; 336 break;
414 } 337 }
415 if (j >= nintf) 338 if (j >= nintf)
416 dev_warn(ddev, "config 339 dev_warn(ddev, "config %d has no interface number "
417 "%d\n", cfgno, i); 340 "%d\n", cfgno, i);
418 } 341 }
419 342
420 /* Allocate the usb_interface_caches a 343 /* Allocate the usb_interface_caches and altsetting arrays */
421 for (i = 0; i < nintf; ++i) { 344 for (i = 0; i < nintf; ++i) {
422 j = nalts[i]; 345 j = nalts[i];
423 if (j > USB_MAXALTSETTING) { 346 if (j > USB_MAXALTSETTING) {
424 dev_warn(ddev, "too ma 347 dev_warn(ddev, "too many alternate settings for "
425 "config %d interfa 348 "config %d interface %d: %d, "
426 "using maximum all 349 "using maximum allowed: %d\n",
427 cfgno, inums[i], j 350 cfgno, inums[i], j, USB_MAXALTSETTING);
428 nalts[i] = j = USB_MAX 351 nalts[i] = j = USB_MAXALTSETTING;
429 } 352 }
430 353
431 len = sizeof(*intfc) + sizeof( 354 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
432 config->intf_cache[i] = intfc !! 355 config->intf_cache[i] = intfc = kmalloc(len, GFP_KERNEL);
433 if (!intfc) 356 if (!intfc)
434 return -ENOMEM; 357 return -ENOMEM;
>> 358 memset(intfc, 0, len);
435 kref_init(&intfc->ref); 359 kref_init(&intfc->ref);
436 } 360 }
437 361
438 /* Skip over any Class Specific or Ven 362 /* Skip over any Class Specific or Vendor Specific descriptors;
439 * find the first interface descriptor 363 * find the first interface descriptor */
440 config->extra = buffer; 364 config->extra = buffer;
441 i = find_next_descriptor(buffer, size, 365 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
442 USB_DT_INTERFACE, &n); 366 USB_DT_INTERFACE, &n);
443 config->extralen = i; 367 config->extralen = i;
444 if (n > 0) 368 if (n > 0)
445 dev_dbg(ddev, "skipped %d desc 369 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
446 n, plural(n), "configurati 370 n, plural(n), "configuration");
447 buffer += i; 371 buffer += i;
448 size -= i; 372 size -= i;
449 373
450 /* Parse all the interface/altsetting 374 /* Parse all the interface/altsetting descriptors */
451 while (size > 0) { 375 while (size > 0) {
452 retval = usb_parse_interface(d 376 retval = usb_parse_interface(ddev, cfgno, config,
453 buffer, size, inums, nalts 377 buffer, size, inums, nalts);
454 if (retval < 0) 378 if (retval < 0)
455 return retval; 379 return retval;
456 380
457 buffer += retval; 381 buffer += retval;
458 size -= retval; 382 size -= retval;
459 } 383 }
460 384
461 /* Check for missing altsettings */ 385 /* Check for missing altsettings */
462 for (i = 0; i < nintf; ++i) { 386 for (i = 0; i < nintf; ++i) {
463 intfc = config->intf_cache[i]; 387 intfc = config->intf_cache[i];
464 for (j = 0; j < intfc->num_alt 388 for (j = 0; j < intfc->num_altsetting; ++j) {
465 for (n = 0; n < intfc- 389 for (n = 0; n < intfc->num_altsetting; ++n) {
466 if (intfc->alt 390 if (intfc->altsetting[n].desc.
467 bAlternate 391 bAlternateSetting == j)
468 break; 392 break;
469 } 393 }
470 if (n >= intfc->num_al 394 if (n >= intfc->num_altsetting)
471 dev_warn(ddev, 395 dev_warn(ddev, "config %d interface %d has no "
472 "altsettin 396 "altsetting %d\n", cfgno, inums[i], j);
473 } 397 }
474 } 398 }
475 399
476 return 0; 400 return 0;
477 } 401 }
478 402
479 /* hub-only!! ... and only exported for reset/ !! 403 // hub-only!! ... and only exported for reset/reinit path.
480 * otherwise used internally on disconnect/des !! 404 // otherwise used internally on disconnect/destroy path
481 */ <<
482 void usb_destroy_configuration(struct usb_devi 405 void usb_destroy_configuration(struct usb_device *dev)
483 { 406 {
484 int c, i; 407 int c, i;
485 408
486 if (!dev->config) 409 if (!dev->config)
487 return; 410 return;
488 411
489 if (dev->rawdescriptors) { 412 if (dev->rawdescriptors) {
490 for (i = 0; i < dev->descripto 413 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
491 kfree(dev->rawdescript 414 kfree(dev->rawdescriptors[i]);
492 415
493 kfree(dev->rawdescriptors); 416 kfree(dev->rawdescriptors);
494 dev->rawdescriptors = NULL; 417 dev->rawdescriptors = NULL;
495 } 418 }
496 419
497 for (c = 0; c < dev->descriptor.bNumCo 420 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
498 struct usb_host_config *cf = & 421 struct usb_host_config *cf = &dev->config[c];
499 422
500 kfree(cf->string); <<
501 for (i = 0; i < cf->desc.bNumI 423 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
502 if (cf->intf_cache[i]) 424 if (cf->intf_cache[i])
503 kref_put(&cf-> !! 425 kref_put(&cf->intf_cache[i]->ref,
504 usb_ 426 usb_release_interface_cache);
505 } 427 }
506 } 428 }
507 kfree(dev->config); 429 kfree(dev->config);
508 dev->config = NULL; 430 dev->config = NULL;
509 } 431 }
510 432
511 433
512 /* !! 434 // hub-only!! ... and only in reset path, or usb_new_device()
513 * Get the USB config descriptors, cache and p !! 435 // (used by real hubs and virtual root hubs)
514 * <<
515 * hub-only!! ... and only in reset path, or u <<
516 * (used by real hubs and virtual root hubs) <<
517 * <<
518 * NOTE: if this is a WUSB device and is not a <<
519 * whole thing. A non-authorized USB dev <<
520 * configurations. <<
521 */ <<
522 int usb_get_configuration(struct usb_device *d 436 int usb_get_configuration(struct usb_device *dev)
523 { 437 {
524 struct device *ddev = &dev->dev; 438 struct device *ddev = &dev->dev;
525 int ncfg = dev->descriptor.bNumConfigu 439 int ncfg = dev->descriptor.bNumConfigurations;
526 int result = 0; !! 440 int result = -ENOMEM;
527 unsigned int cfgno, length; 441 unsigned int cfgno, length;
528 unsigned char *buffer; 442 unsigned char *buffer;
529 unsigned char *bigbuffer; 443 unsigned char *bigbuffer;
530 struct usb_config_descriptor *desc; !! 444 struct usb_config_descriptor *desc;
531 445
532 cfgno = 0; <<
533 if (dev->authorized == 0) /* Not <<
534 goto out_not_authorized; <<
535 result = -ENOMEM; <<
536 if (ncfg > USB_MAXCONFIG) { 446 if (ncfg > USB_MAXCONFIG) {
537 dev_warn(ddev, "too many confi 447 dev_warn(ddev, "too many configurations: %d, "
538 "using maximum allowed: %d 448 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
539 dev->descriptor.bNumConfigurat 449 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
540 } 450 }
541 451
542 if (ncfg < 1) { 452 if (ncfg < 1) {
543 dev_err(ddev, "no configuratio 453 dev_err(ddev, "no configurations\n");
544 return -EINVAL; 454 return -EINVAL;
545 } 455 }
546 456
547 length = ncfg * sizeof(struct usb_host 457 length = ncfg * sizeof(struct usb_host_config);
548 dev->config = kzalloc(length, GFP_KERN !! 458 dev->config = kmalloc(length, GFP_KERNEL);
549 if (!dev->config) 459 if (!dev->config)
550 goto err2; 460 goto err2;
>> 461 memset(dev->config, 0, length);
551 462
552 length = ncfg * sizeof(char *); 463 length = ncfg * sizeof(char *);
553 dev->rawdescriptors = kzalloc(length, !! 464 dev->rawdescriptors = kmalloc(length, GFP_KERNEL);
554 if (!dev->rawdescriptors) 465 if (!dev->rawdescriptors)
555 goto err2; 466 goto err2;
>> 467 memset(dev->rawdescriptors, 0, length);
556 468
557 buffer = kmalloc(USB_DT_CONFIG_SIZE, G 469 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
558 if (!buffer) 470 if (!buffer)
559 goto err2; 471 goto err2;
560 desc = (struct usb_config_descriptor * 472 desc = (struct usb_config_descriptor *)buffer;
561 473
562 result = 0; !! 474 for (cfgno = 0; cfgno < ncfg; cfgno++) {
563 for (; cfgno < ncfg; cfgno++) { <<
564 /* We grab just the first desc 475 /* We grab just the first descriptor so we know how long
565 * the whole configuration is 476 * the whole configuration is */
566 result = usb_get_descriptor(de 477 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
567 buffer, USB_DT_CONFIG_SIZE 478 buffer, USB_DT_CONFIG_SIZE);
568 if (result < 0) { 479 if (result < 0) {
569 dev_err(ddev, "unable 480 dev_err(ddev, "unable to read config index %d "
570 "descriptor/%s: %d !! 481 "descriptor/%s\n", cfgno, "start");
571 dev_err(ddev, "choppin !! 482 goto err;
572 dev->descriptor.bNumCo <<
573 break; <<
574 } else if (result < 4) { 483 } else if (result < 4) {
575 dev_err(ddev, "config 484 dev_err(ddev, "config index %d descriptor too short "
576 "(expected %i, got 485 "(expected %i, got %i)\n", cfgno,
577 USB_DT_CONFIG_SIZE 486 USB_DT_CONFIG_SIZE, result);
578 result = -EINVAL; 487 result = -EINVAL;
579 goto err; 488 goto err;
580 } 489 }
581 length = max((int) le16_to_cpu 490 length = max((int) le16_to_cpu(desc->wTotalLength),
582 USB_DT_CONFIG_SIZE); 491 USB_DT_CONFIG_SIZE);
583 492
584 /* Now that we know the length 493 /* Now that we know the length, get the whole thing */
585 bigbuffer = kmalloc(length, GF 494 bigbuffer = kmalloc(length, GFP_KERNEL);
586 if (!bigbuffer) { 495 if (!bigbuffer) {
587 result = -ENOMEM; 496 result = -ENOMEM;
588 goto err; 497 goto err;
589 } 498 }
590 result = usb_get_descriptor(de 499 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
591 bigbuffer, length); 500 bigbuffer, length);
592 if (result < 0) { 501 if (result < 0) {
593 dev_err(ddev, "unable 502 dev_err(ddev, "unable to read config index %d "
594 "descriptor/%s\n", 503 "descriptor/%s\n", cfgno, "all");
595 kfree(bigbuffer); 504 kfree(bigbuffer);
596 goto err; 505 goto err;
597 } 506 }
598 if (result < length) { 507 if (result < length) {
599 dev_warn(ddev, "config 508 dev_warn(ddev, "config index %d descriptor too short "
600 "(expected %i, got 509 "(expected %i, got %i)\n", cfgno, length, result);
601 length = result; 510 length = result;
602 } 511 }
603 512
604 dev->rawdescriptors[cfgno] = b 513 dev->rawdescriptors[cfgno] = bigbuffer;
605 514
606 result = usb_parse_configurati 515 result = usb_parse_configuration(&dev->dev, cfgno,
607 &dev->config[cfgno], bigbu 516 &dev->config[cfgno], bigbuffer, length);
608 if (result < 0) { 517 if (result < 0) {
609 ++cfgno; 518 ++cfgno;
610 goto err; 519 goto err;
611 } 520 }
612 } 521 }
613 result = 0; 522 result = 0;
614 523
615 err: 524 err:
616 kfree(buffer); 525 kfree(buffer);
617 out_not_authorized: <<
618 dev->descriptor.bNumConfigurations = c 526 dev->descriptor.bNumConfigurations = cfgno;
619 err2: 527 err2:
620 if (result == -ENOMEM) 528 if (result == -ENOMEM)
621 dev_err(ddev, "out of memory\n 529 dev_err(ddev, "out of memory\n");
622 return result; 530 return result;
623 } 531 }
624 532
|
This page was automatically generated by the
LXR engine.
|