]>
Commit | Line | Data |
---|---|---|
40982be5 DB |
1 | /* |
2 | * composite.c - infrastructure for Composite USB Gadgets | |
3 | * | |
4 | * Copyright (C) 2006-2008 David Brownell | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
40982be5 DB |
10 | */ |
11 | ||
12 | /* #define VERBOSE_DEBUG */ | |
13 | ||
14 | #include <linux/kallsyms.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/slab.h> | |
6eb0de82 | 17 | #include <linux/module.h> |
40982be5 | 18 | #include <linux/device.h> |
ad1a8102 | 19 | #include <linux/utsname.h> |
40982be5 DB |
20 | |
21 | #include <linux/usb/composite.h> | |
53e6242d | 22 | #include <linux/usb/otg.h> |
bdb64d72 | 23 | #include <asm/unaligned.h> |
40982be5 | 24 | |
37a3a533 AP |
25 | #include "u_os_desc.h" |
26 | ||
19824d5e AP |
27 | /** |
28 | * struct usb_os_string - represents OS String to be reported by a gadget | |
29 | * @bLength: total length of the entire descritor, always 0x12 | |
30 | * @bDescriptorType: USB_DT_STRING | |
31 | * @qwSignature: the OS String proper | |
32 | * @bMS_VendorCode: code used by the host for subsequent requests | |
33 | * @bPad: not used, must be zero | |
34 | */ | |
35 | struct usb_os_string { | |
36 | __u8 bLength; | |
37 | __u8 bDescriptorType; | |
38 | __u8 qwSignature[OS_STRING_QW_SIGN_LEN]; | |
39 | __u8 bMS_VendorCode; | |
40 | __u8 bPad; | |
41 | } __packed; | |
42 | ||
40982be5 DB |
43 | /* |
44 | * The code in this file is utility code, used to build a gadget driver | |
45 | * from one or more "function" drivers, one or more "configuration" | |
46 | * objects, and a "usb_composite_driver" by gluing them together along | |
47 | * with the relevant device-wide data. | |
48 | */ | |
49 | ||
9bb2859f SAS |
50 | static struct usb_gadget_strings **get_containers_gs( |
51 | struct usb_gadget_string_container *uc) | |
52 | { | |
53 | return (struct usb_gadget_strings **)uc->stash; | |
54 | } | |
55 | ||
f3bdbe36 JY |
56 | /** |
57 | * function_descriptors() - get function descriptors for speed | |
58 | * @f: the function | |
59 | * @speed: the speed | |
60 | * | |
61 | * Returns the descriptors or NULL if not set. | |
62 | */ | |
63 | static struct usb_descriptor_header ** | |
64 | function_descriptors(struct usb_function *f, | |
65 | enum usb_device_speed speed) | |
66 | { | |
67 | struct usb_descriptor_header **descriptors; | |
68 | ||
0878263b FB |
69 | /* |
70 | * NOTE: we try to help gadget drivers which might not be setting | |
71 | * max_speed appropriately. | |
72 | */ | |
73 | ||
f3bdbe36 JY |
74 | switch (speed) { |
75 | case USB_SPEED_SUPER_PLUS: | |
76 | descriptors = f->ssp_descriptors; | |
0878263b FB |
77 | if (descriptors) |
78 | break; | |
79 | /* FALLTHROUGH */ | |
f3bdbe36 JY |
80 | case USB_SPEED_SUPER: |
81 | descriptors = f->ss_descriptors; | |
0878263b FB |
82 | if (descriptors) |
83 | break; | |
84 | /* FALLTHROUGH */ | |
f3bdbe36 JY |
85 | case USB_SPEED_HIGH: |
86 | descriptors = f->hs_descriptors; | |
0878263b FB |
87 | if (descriptors) |
88 | break; | |
89 | /* FALLTHROUGH */ | |
f3bdbe36 JY |
90 | default: |
91 | descriptors = f->fs_descriptors; | |
92 | } | |
93 | ||
0878263b FB |
94 | /* |
95 | * if we can't find any descriptors at all, then this gadget deserves to | |
96 | * Oops with a NULL pointer dereference | |
97 | */ | |
98 | ||
f3bdbe36 JY |
99 | return descriptors; |
100 | } | |
101 | ||
48767a4e TB |
102 | /** |
103 | * next_ep_desc() - advance to the next EP descriptor | |
104 | * @t: currect pointer within descriptor array | |
105 | * | |
106 | * Return: next EP descriptor or NULL | |
107 | * | |
108 | * Iterate over @t until either EP descriptor found or | |
109 | * NULL (that indicates end of list) encountered | |
110 | */ | |
111 | static struct usb_descriptor_header** | |
112 | next_ep_desc(struct usb_descriptor_header **t) | |
113 | { | |
114 | for (; *t; t++) { | |
115 | if ((*t)->bDescriptorType == USB_DT_ENDPOINT) | |
116 | return t; | |
117 | } | |
118 | return NULL; | |
119 | } | |
120 | ||
121 | /* | |
122 | * for_each_ep_desc()- iterate over endpoint descriptors in the | |
123 | * descriptors list | |
124 | * @start: pointer within descriptor array. | |
125 | * @ep_desc: endpoint descriptor to use as the loop cursor | |
126 | */ | |
127 | #define for_each_ep_desc(start, ep_desc) \ | |
128 | for (ep_desc = next_ep_desc(start); \ | |
129 | ep_desc; ep_desc = next_ep_desc(ep_desc+1)) | |
130 | ||
131 | /** | |
132 | * config_ep_by_speed() - configures the given endpoint | |
133 | * according to gadget speed. | |
134 | * @g: pointer to the gadget | |
135 | * @f: usb function | |
136 | * @_ep: the endpoint to configure | |
137 | * | |
138 | * Return: error code, 0 on success | |
139 | * | |
140 | * This function chooses the right descriptors for a given | |
141 | * endpoint according to gadget speed and saves it in the | |
142 | * endpoint desc field. If the endpoint already has a descriptor | |
143 | * assigned to it - overwrites it with currently corresponding | |
144 | * descriptor. The endpoint maxpacket field is updated according | |
145 | * to the chosen descriptor. | |
146 | * Note: the supplied function should hold all the descriptors | |
147 | * for supported speeds | |
148 | */ | |
149 | int config_ep_by_speed(struct usb_gadget *g, | |
150 | struct usb_function *f, | |
151 | struct usb_ep *_ep) | |
152 | { | |
b785ea7c | 153 | struct usb_composite_dev *cdev = get_gadget_data(g); |
48767a4e TB |
154 | struct usb_endpoint_descriptor *chosen_desc = NULL; |
155 | struct usb_descriptor_header **speed_desc = NULL; | |
156 | ||
bdb64d72 TB |
157 | struct usb_ss_ep_comp_descriptor *comp_desc = NULL; |
158 | int want_comp_desc = 0; | |
159 | ||
48767a4e TB |
160 | struct usb_descriptor_header **d_spd; /* cursor for speed desc */ |
161 | ||
162 | if (!g || !f || !_ep) | |
163 | return -EIO; | |
164 | ||
165 | /* select desired speed */ | |
166 | switch (g->speed) { | |
4eb8e32d JY |
167 | case USB_SPEED_SUPER_PLUS: |
168 | if (gadget_is_superspeed_plus(g)) { | |
169 | speed_desc = f->ssp_descriptors; | |
170 | want_comp_desc = 1; | |
171 | break; | |
172 | } | |
173 | /* else: Fall trough */ | |
bdb64d72 TB |
174 | case USB_SPEED_SUPER: |
175 | if (gadget_is_superspeed(g)) { | |
176 | speed_desc = f->ss_descriptors; | |
177 | want_comp_desc = 1; | |
178 | break; | |
179 | } | |
180 | /* else: Fall trough */ | |
48767a4e TB |
181 | case USB_SPEED_HIGH: |
182 | if (gadget_is_dualspeed(g)) { | |
183 | speed_desc = f->hs_descriptors; | |
184 | break; | |
185 | } | |
186 | /* else: fall through */ | |
187 | default: | |
10287bae | 188 | speed_desc = f->fs_descriptors; |
48767a4e TB |
189 | } |
190 | /* find descriptors */ | |
191 | for_each_ep_desc(speed_desc, d_spd) { | |
192 | chosen_desc = (struct usb_endpoint_descriptor *)*d_spd; | |
193 | if (chosen_desc->bEndpointAddress == _ep->address) | |
194 | goto ep_found; | |
195 | } | |
196 | return -EIO; | |
197 | ||
198 | ep_found: | |
199 | /* commit results */ | |
29cc8897 | 200 | _ep->maxpacket = usb_endpoint_maxp(chosen_desc); |
48767a4e | 201 | _ep->desc = chosen_desc; |
bdb64d72 TB |
202 | _ep->comp_desc = NULL; |
203 | _ep->maxburst = 0; | |
204 | _ep->mult = 0; | |
205 | if (!want_comp_desc) | |
206 | return 0; | |
48767a4e | 207 | |
bdb64d72 TB |
208 | /* |
209 | * Companion descriptor should follow EP descriptor | |
210 | * USB 3.0 spec, #9.6.7 | |
211 | */ | |
212 | comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd); | |
213 | if (!comp_desc || | |
214 | (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP)) | |
215 | return -EIO; | |
216 | _ep->comp_desc = comp_desc; | |
4eb8e32d | 217 | if (g->speed >= USB_SPEED_SUPER) { |
bdb64d72 | 218 | switch (usb_endpoint_type(_ep->desc)) { |
bdb64d72 TB |
219 | case USB_ENDPOINT_XFER_ISOC: |
220 | /* mult: bits 1:0 of bmAttributes */ | |
221 | _ep->mult = comp_desc->bmAttributes & 0x3; | |
9e878a6b PZ |
222 | case USB_ENDPOINT_XFER_BULK: |
223 | case USB_ENDPOINT_XFER_INT: | |
b785ea7c | 224 | _ep->maxburst = comp_desc->bMaxBurst + 1; |
bdb64d72 TB |
225 | break; |
226 | default: | |
b785ea7c FB |
227 | if (comp_desc->bMaxBurst != 0) |
228 | ERROR(cdev, "ep0 bMaxBurst must be 0\n"); | |
229 | _ep->maxburst = 1; | |
bdb64d72 TB |
230 | break; |
231 | } | |
232 | } | |
48767a4e TB |
233 | return 0; |
234 | } | |
721e2e91 | 235 | EXPORT_SYMBOL_GPL(config_ep_by_speed); |
40982be5 DB |
236 | |
237 | /** | |
238 | * usb_add_function() - add a function to a configuration | |
239 | * @config: the configuration | |
240 | * @function: the function being added | |
241 | * Context: single threaded during gadget setup | |
242 | * | |
243 | * After initialization, each configuration must have one or more | |
244 | * functions added to it. Adding a function involves calling its @bind() | |
245 | * method to allocate resources such as interface and string identifiers | |
246 | * and endpoints. | |
247 | * | |
248 | * This function returns the value of the function's bind(), which is | |
249 | * zero for success else a negative errno value. | |
250 | */ | |
28824b18 | 251 | int usb_add_function(struct usb_configuration *config, |
40982be5 DB |
252 | struct usb_function *function) |
253 | { | |
254 | int value = -EINVAL; | |
255 | ||
256 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", | |
257 | function->name, function, | |
258 | config->label, config); | |
259 | ||
260 | if (!function->set_alt || !function->disable) | |
261 | goto done; | |
262 | ||
263 | function->config = config; | |
264 | list_add_tail(&function->list, &config->functions); | |
265 | ||
d5bb9b81 RB |
266 | if (function->bind_deactivated) { |
267 | value = usb_function_deactivate(function); | |
268 | if (value) | |
269 | goto done; | |
270 | } | |
271 | ||
40982be5 DB |
272 | /* REVISIT *require* function->bind? */ |
273 | if (function->bind) { | |
274 | value = function->bind(config, function); | |
275 | if (value < 0) { | |
276 | list_del(&function->list); | |
277 | function->config = NULL; | |
278 | } | |
279 | } else | |
280 | value = 0; | |
281 | ||
282 | /* We allow configurations that don't work at both speeds. | |
283 | * If we run into a lowspeed Linux system, treat it the same | |
284 | * as full speed ... it's the function drivers that will need | |
285 | * to avoid bulk and ISO transfers. | |
286 | */ | |
10287bae | 287 | if (!config->fullspeed && function->fs_descriptors) |
40982be5 DB |
288 | config->fullspeed = true; |
289 | if (!config->highspeed && function->hs_descriptors) | |
290 | config->highspeed = true; | |
bdb64d72 TB |
291 | if (!config->superspeed && function->ss_descriptors) |
292 | config->superspeed = true; | |
554eead5 JY |
293 | if (!config->superspeed_plus && function->ssp_descriptors) |
294 | config->superspeed_plus = true; | |
40982be5 DB |
295 | |
296 | done: | |
297 | if (value) | |
298 | DBG(config->cdev, "adding '%s'/%p --> %d\n", | |
299 | function->name, function, value); | |
300 | return value; | |
301 | } | |
721e2e91 | 302 | EXPORT_SYMBOL_GPL(usb_add_function); |
40982be5 | 303 | |
b4735778 SAS |
304 | void usb_remove_function(struct usb_configuration *c, struct usb_function *f) |
305 | { | |
306 | if (f->disable) | |
307 | f->disable(f); | |
308 | ||
309 | bitmap_zero(f->endpoints, 32); | |
310 | list_del(&f->list); | |
311 | if (f->unbind) | |
312 | f->unbind(c, f); | |
313 | } | |
314 | EXPORT_SYMBOL_GPL(usb_remove_function); | |
315 | ||
60beed95 DB |
316 | /** |
317 | * usb_function_deactivate - prevent function and gadget enumeration | |
318 | * @function: the function that isn't yet ready to respond | |
319 | * | |
320 | * Blocks response of the gadget driver to host enumeration by | |
321 | * preventing the data line pullup from being activated. This is | |
322 | * normally called during @bind() processing to change from the | |
323 | * initial "ready to respond" state, or when a required resource | |
324 | * becomes available. | |
325 | * | |
326 | * For example, drivers that serve as a passthrough to a userspace | |
327 | * daemon can block enumeration unless that daemon (such as an OBEX, | |
328 | * MTP, or print server) is ready to handle host requests. | |
329 | * | |
330 | * Not all systems support software control of their USB peripheral | |
331 | * data pullups. | |
332 | * | |
333 | * Returns zero on success, else negative errno. | |
334 | */ | |
335 | int usb_function_deactivate(struct usb_function *function) | |
336 | { | |
337 | struct usb_composite_dev *cdev = function->config->cdev; | |
b2bdf3a7 | 338 | unsigned long flags; |
60beed95 DB |
339 | int status = 0; |
340 | ||
b2bdf3a7 | 341 | spin_lock_irqsave(&cdev->lock, flags); |
60beed95 DB |
342 | |
343 | if (cdev->deactivations == 0) | |
5601250b | 344 | status = usb_gadget_deactivate(cdev->gadget); |
60beed95 DB |
345 | if (status == 0) |
346 | cdev->deactivations++; | |
347 | ||
b2bdf3a7 | 348 | spin_unlock_irqrestore(&cdev->lock, flags); |
60beed95 DB |
349 | return status; |
350 | } | |
721e2e91 | 351 | EXPORT_SYMBOL_GPL(usb_function_deactivate); |
60beed95 DB |
352 | |
353 | /** | |
354 | * usb_function_activate - allow function and gadget enumeration | |
355 | * @function: function on which usb_function_activate() was called | |
356 | * | |
357 | * Reverses effect of usb_function_deactivate(). If no more functions | |
358 | * are delaying their activation, the gadget driver will respond to | |
359 | * host enumeration procedures. | |
360 | * | |
361 | * Returns zero on success, else negative errno. | |
362 | */ | |
363 | int usb_function_activate(struct usb_function *function) | |
364 | { | |
365 | struct usb_composite_dev *cdev = function->config->cdev; | |
4fefe9f6 | 366 | unsigned long flags; |
60beed95 DB |
367 | int status = 0; |
368 | ||
4fefe9f6 | 369 | spin_lock_irqsave(&cdev->lock, flags); |
60beed95 DB |
370 | |
371 | if (WARN_ON(cdev->deactivations == 0)) | |
372 | status = -EINVAL; | |
373 | else { | |
374 | cdev->deactivations--; | |
375 | if (cdev->deactivations == 0) | |
5601250b | 376 | status = usb_gadget_activate(cdev->gadget); |
60beed95 DB |
377 | } |
378 | ||
4fefe9f6 | 379 | spin_unlock_irqrestore(&cdev->lock, flags); |
60beed95 DB |
380 | return status; |
381 | } | |
721e2e91 | 382 | EXPORT_SYMBOL_GPL(usb_function_activate); |
60beed95 | 383 | |
40982be5 DB |
384 | /** |
385 | * usb_interface_id() - allocate an unused interface ID | |
386 | * @config: configuration associated with the interface | |
387 | * @function: function handling the interface | |
388 | * Context: single threaded during gadget setup | |
389 | * | |
390 | * usb_interface_id() is called from usb_function.bind() callbacks to | |
391 | * allocate new interface IDs. The function driver will then store that | |
392 | * ID in interface, association, CDC union, and other descriptors. It | |
25985edc | 393 | * will also handle any control requests targeted at that interface, |
40982be5 DB |
394 | * particularly changing its altsetting via set_alt(). There may |
395 | * also be class-specific or vendor-specific requests to handle. | |
396 | * | |
397 | * All interface identifier should be allocated using this routine, to | |
398 | * ensure that for example different functions don't wrongly assign | |
399 | * different meanings to the same identifier. Note that since interface | |
25985edc | 400 | * identifiers are configuration-specific, functions used in more than |
40982be5 DB |
401 | * one configuration (or more than once in a given configuration) need |
402 | * multiple versions of the relevant descriptors. | |
403 | * | |
404 | * Returns the interface ID which was allocated; or -ENODEV if no | |
405 | * more interface IDs can be allocated. | |
406 | */ | |
28824b18 | 407 | int usb_interface_id(struct usb_configuration *config, |
40982be5 DB |
408 | struct usb_function *function) |
409 | { | |
410 | unsigned id = config->next_interface_id; | |
411 | ||
412 | if (id < MAX_CONFIG_INTERFACES) { | |
413 | config->interface[id] = function; | |
414 | config->next_interface_id = id + 1; | |
415 | return id; | |
416 | } | |
417 | return -ENODEV; | |
418 | } | |
721e2e91 | 419 | EXPORT_SYMBOL_GPL(usb_interface_id); |
40982be5 | 420 | |
8f900a9a SAS |
421 | static u8 encode_bMaxPower(enum usb_device_speed speed, |
422 | struct usb_configuration *c) | |
423 | { | |
424 | unsigned val; | |
425 | ||
426 | if (c->MaxPower) | |
427 | val = c->MaxPower; | |
428 | else | |
429 | val = CONFIG_USB_GADGET_VBUS_DRAW; | |
430 | if (!val) | |
431 | return 0; | |
432 | switch (speed) { | |
433 | case USB_SPEED_SUPER: | |
434 | return DIV_ROUND_UP(val, 8); | |
435 | default: | |
436 | return DIV_ROUND_UP(val, 2); | |
2b84f92b | 437 | } |
8f900a9a SAS |
438 | } |
439 | ||
40982be5 DB |
440 | static int config_buf(struct usb_configuration *config, |
441 | enum usb_device_speed speed, void *buf, u8 type) | |
442 | { | |
443 | struct usb_config_descriptor *c = buf; | |
444 | void *next = buf + USB_DT_CONFIG_SIZE; | |
e13f17ff | 445 | int len; |
40982be5 DB |
446 | struct usb_function *f; |
447 | int status; | |
448 | ||
e13f17ff | 449 | len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; |
40982be5 DB |
450 | /* write the config descriptor */ |
451 | c = buf; | |
452 | c->bLength = USB_DT_CONFIG_SIZE; | |
453 | c->bDescriptorType = type; | |
454 | /* wTotalLength is written later */ | |
455 | c->bNumInterfaces = config->next_interface_id; | |
456 | c->bConfigurationValue = config->bConfigurationValue; | |
457 | c->iConfiguration = config->iConfiguration; | |
458 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; | |
8f900a9a | 459 | c->bMaxPower = encode_bMaxPower(speed, config); |
40982be5 DB |
460 | |
461 | /* There may be e.g. OTG descriptors */ | |
462 | if (config->descriptors) { | |
463 | status = usb_descriptor_fillbuf(next, len, | |
464 | config->descriptors); | |
465 | if (status < 0) | |
466 | return status; | |
467 | len -= status; | |
468 | next += status; | |
469 | } | |
470 | ||
471 | /* add each function's descriptors */ | |
472 | list_for_each_entry(f, &config->functions, list) { | |
473 | struct usb_descriptor_header **descriptors; | |
474 | ||
f3bdbe36 | 475 | descriptors = function_descriptors(f, speed); |
40982be5 DB |
476 | if (!descriptors) |
477 | continue; | |
478 | status = usb_descriptor_fillbuf(next, len, | |
479 | (const struct usb_descriptor_header **) descriptors); | |
480 | if (status < 0) | |
481 | return status; | |
482 | len -= status; | |
483 | next += status; | |
484 | } | |
485 | ||
486 | len = next - buf; | |
487 | c->wTotalLength = cpu_to_le16(len); | |
488 | return len; | |
489 | } | |
490 | ||
491 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) | |
492 | { | |
493 | struct usb_gadget *gadget = cdev->gadget; | |
494 | struct usb_configuration *c; | |
37a3a533 | 495 | struct list_head *pos; |
40982be5 DB |
496 | u8 type = w_value >> 8; |
497 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; | |
498 | ||
eae5820b | 499 | if (gadget->speed >= USB_SPEED_SUPER) |
bdb64d72 TB |
500 | speed = gadget->speed; |
501 | else if (gadget_is_dualspeed(gadget)) { | |
502 | int hs = 0; | |
40982be5 DB |
503 | if (gadget->speed == USB_SPEED_HIGH) |
504 | hs = 1; | |
505 | if (type == USB_DT_OTHER_SPEED_CONFIG) | |
506 | hs = !hs; | |
507 | if (hs) | |
508 | speed = USB_SPEED_HIGH; | |
509 | ||
510 | } | |
511 | ||
512 | /* This is a lookup by config *INDEX* */ | |
513 | w_value &= 0xff; | |
37a3a533 AP |
514 | |
515 | pos = &cdev->configs; | |
516 | c = cdev->os_desc_config; | |
517 | if (c) | |
518 | goto check_config; | |
519 | ||
520 | while ((pos = pos->next) != &cdev->configs) { | |
521 | c = list_entry(pos, typeof(*c), list); | |
522 | ||
523 | /* skip OS Descriptors config which is handled separately */ | |
524 | if (c == cdev->os_desc_config) | |
525 | continue; | |
526 | ||
527 | check_config: | |
40982be5 | 528 | /* ignore configs that won't work at this speed */ |
bdb64d72 | 529 | switch (speed) { |
eae5820b JY |
530 | case USB_SPEED_SUPER_PLUS: |
531 | if (!c->superspeed_plus) | |
532 | continue; | |
533 | break; | |
bdb64d72 TB |
534 | case USB_SPEED_SUPER: |
535 | if (!c->superspeed) | |
536 | continue; | |
537 | break; | |
538 | case USB_SPEED_HIGH: | |
40982be5 DB |
539 | if (!c->highspeed) |
540 | continue; | |
bdb64d72 TB |
541 | break; |
542 | default: | |
40982be5 DB |
543 | if (!c->fullspeed) |
544 | continue; | |
545 | } | |
bdb64d72 | 546 | |
40982be5 DB |
547 | if (w_value == 0) |
548 | return config_buf(c, speed, cdev->req->buf, type); | |
549 | w_value--; | |
550 | } | |
551 | return -EINVAL; | |
552 | } | |
553 | ||
554 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) | |
555 | { | |
556 | struct usb_gadget *gadget = cdev->gadget; | |
557 | struct usb_configuration *c; | |
558 | unsigned count = 0; | |
559 | int hs = 0; | |
bdb64d72 | 560 | int ss = 0; |
a4afd012 | 561 | int ssp = 0; |
40982be5 DB |
562 | |
563 | if (gadget_is_dualspeed(gadget)) { | |
564 | if (gadget->speed == USB_SPEED_HIGH) | |
565 | hs = 1; | |
bdb64d72 TB |
566 | if (gadget->speed == USB_SPEED_SUPER) |
567 | ss = 1; | |
a4afd012 JY |
568 | if (gadget->speed == USB_SPEED_SUPER_PLUS) |
569 | ssp = 1; | |
40982be5 DB |
570 | if (type == USB_DT_DEVICE_QUALIFIER) |
571 | hs = !hs; | |
572 | } | |
573 | list_for_each_entry(c, &cdev->configs, list) { | |
574 | /* ignore configs that won't work at this speed */ | |
a4afd012 JY |
575 | if (ssp) { |
576 | if (!c->superspeed_plus) | |
577 | continue; | |
578 | } else if (ss) { | |
bdb64d72 TB |
579 | if (!c->superspeed) |
580 | continue; | |
581 | } else if (hs) { | |
40982be5 DB |
582 | if (!c->highspeed) |
583 | continue; | |
584 | } else { | |
585 | if (!c->fullspeed) | |
586 | continue; | |
587 | } | |
588 | count++; | |
589 | } | |
590 | return count; | |
591 | } | |
592 | ||
bdb64d72 TB |
593 | /** |
594 | * bos_desc() - prepares the BOS descriptor. | |
595 | * @cdev: pointer to usb_composite device to generate the bos | |
596 | * descriptor for | |
597 | * | |
598 | * This function generates the BOS (Binary Device Object) | |
599 | * descriptor and its device capabilities descriptors. The BOS | |
600 | * descriptor should be supported by a SuperSpeed device. | |
601 | */ | |
602 | static int bos_desc(struct usb_composite_dev *cdev) | |
603 | { | |
604 | struct usb_ext_cap_descriptor *usb_ext; | |
605 | struct usb_ss_cap_descriptor *ss_cap; | |
606 | struct usb_dcd_config_params dcd_config_params; | |
607 | struct usb_bos_descriptor *bos = cdev->req->buf; | |
608 | ||
609 | bos->bLength = USB_DT_BOS_SIZE; | |
610 | bos->bDescriptorType = USB_DT_BOS; | |
611 | ||
612 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); | |
613 | bos->bNumDeviceCaps = 0; | |
614 | ||
615 | /* | |
616 | * A SuperSpeed device shall include the USB2.0 extension descriptor | |
617 | * and shall support LPM when operating in USB2.0 HS mode. | |
618 | */ | |
619 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); | |
620 | bos->bNumDeviceCaps++; | |
621 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE); | |
622 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; | |
623 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; | |
624 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; | |
a6615937 | 625 | usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT); |
bdb64d72 TB |
626 | |
627 | /* | |
628 | * The Superspeed USB Capability descriptor shall be implemented by all | |
629 | * SuperSpeed devices. | |
630 | */ | |
631 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); | |
632 | bos->bNumDeviceCaps++; | |
633 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); | |
634 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; | |
635 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; | |
636 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; | |
637 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ | |
638 | ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | | |
639 | USB_FULL_SPEED_OPERATION | | |
640 | USB_HIGH_SPEED_OPERATION | | |
641 | USB_5GBPS_OPERATION); | |
642 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; | |
643 | ||
644 | /* Get Controller configuration */ | |
645 | if (cdev->gadget->ops->get_config_params) | |
646 | cdev->gadget->ops->get_config_params(&dcd_config_params); | |
647 | else { | |
089b837a | 648 | dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
bdb64d72 | 649 | dcd_config_params.bU2DevExitLat = |
089b837a | 650 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
bdb64d72 TB |
651 | } |
652 | ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; | |
653 | ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; | |
654 | ||
f228a8de JY |
655 | /* The SuperSpeedPlus USB Device Capability descriptor */ |
656 | if (gadget_is_superspeed_plus(cdev->gadget)) { | |
657 | struct usb_ssp_cap_descriptor *ssp_cap; | |
658 | ||
659 | ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); | |
660 | bos->bNumDeviceCaps++; | |
661 | ||
662 | /* | |
663 | * Report typical values. | |
664 | */ | |
665 | ||
666 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1)); | |
667 | ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1); | |
668 | ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; | |
669 | ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE; | |
138b8638 JY |
670 | ssp_cap->bReserved = 0; |
671 | ssp_cap->wReserved = 0; | |
f228a8de JY |
672 | |
673 | /* SSAC = 1 (2 attributes) */ | |
674 | ssp_cap->bmAttributes = cpu_to_le32(1); | |
675 | ||
676 | /* Min RX/TX Lane Count = 1 */ | |
08f8cabf JY |
677 | ssp_cap->wFunctionalitySupport = |
678 | cpu_to_le16((1 << 8) | (1 << 12)); | |
f228a8de JY |
679 | |
680 | /* | |
681 | * bmSublinkSpeedAttr[0]: | |
682 | * ST = Symmetric, RX | |
683 | * LSE = 3 (Gbps) | |
684 | * LP = 1 (SuperSpeedPlus) | |
685 | * LSM = 10 (10 Gbps) | |
686 | */ | |
687 | ssp_cap->bmSublinkSpeedAttr[0] = | |
08f8cabf | 688 | cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16)); |
f228a8de JY |
689 | /* |
690 | * bmSublinkSpeedAttr[1] = | |
691 | * ST = Symmetric, TX | |
692 | * LSE = 3 (Gbps) | |
693 | * LP = 1 (SuperSpeedPlus) | |
694 | * LSM = 10 (10 Gbps) | |
695 | */ | |
696 | ssp_cap->bmSublinkSpeedAttr[1] = | |
08f8cabf JY |
697 | cpu_to_le32((3 << 4) | (1 << 14) | |
698 | (0xa << 16) | (1 << 7)); | |
f228a8de JY |
699 | } |
700 | ||
bdb64d72 TB |
701 | return le16_to_cpu(bos->wTotalLength); |
702 | } | |
703 | ||
40982be5 DB |
704 | static void device_qual(struct usb_composite_dev *cdev) |
705 | { | |
706 | struct usb_qualifier_descriptor *qual = cdev->req->buf; | |
707 | ||
708 | qual->bLength = sizeof(*qual); | |
709 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; | |
710 | /* POLICY: same bcdUSB and device type info at both speeds */ | |
711 | qual->bcdUSB = cdev->desc.bcdUSB; | |
712 | qual->bDeviceClass = cdev->desc.bDeviceClass; | |
713 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; | |
714 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; | |
715 | /* ASSUME same EP0 fifo size at both speeds */ | |
765f5b83 | 716 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
40982be5 | 717 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
c24f4227 | 718 | qual->bRESERVED = 0; |
40982be5 DB |
719 | } |
720 | ||
721 | /*-------------------------------------------------------------------------*/ | |
722 | ||
723 | static void reset_config(struct usb_composite_dev *cdev) | |
724 | { | |
725 | struct usb_function *f; | |
726 | ||
727 | DBG(cdev, "reset config\n"); | |
728 | ||
729 | list_for_each_entry(f, &cdev->config->functions, list) { | |
730 | if (f->disable) | |
731 | f->disable(f); | |
5242658d LP |
732 | |
733 | bitmap_zero(f->endpoints, 32); | |
40982be5 DB |
734 | } |
735 | cdev->config = NULL; | |
2bac51a1 | 736 | cdev->delayed_status = 0; |
40982be5 DB |
737 | } |
738 | ||
739 | static int set_config(struct usb_composite_dev *cdev, | |
740 | const struct usb_ctrlrequest *ctrl, unsigned number) | |
741 | { | |
742 | struct usb_gadget *gadget = cdev->gadget; | |
743 | struct usb_configuration *c = NULL; | |
744 | int result = -EINVAL; | |
745 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; | |
746 | int tmp; | |
747 | ||
40982be5 DB |
748 | if (number) { |
749 | list_for_each_entry(c, &cdev->configs, list) { | |
750 | if (c->bConfigurationValue == number) { | |
bdb64d72 TB |
751 | /* |
752 | * We disable the FDs of the previous | |
753 | * configuration only if the new configuration | |
754 | * is a valid one | |
755 | */ | |
756 | if (cdev->config) | |
757 | reset_config(cdev); | |
40982be5 DB |
758 | result = 0; |
759 | break; | |
760 | } | |
761 | } | |
762 | if (result < 0) | |
763 | goto done; | |
bdb64d72 TB |
764 | } else { /* Zero configuration value - need to reset the config */ |
765 | if (cdev->config) | |
766 | reset_config(cdev); | |
40982be5 | 767 | result = 0; |
bdb64d72 | 768 | } |
40982be5 | 769 | |
e538dfda MN |
770 | INFO(cdev, "%s config #%d: %s\n", |
771 | usb_speed_string(gadget->speed), | |
772 | number, c ? c->label : "unconfigured"); | |
40982be5 DB |
773 | |
774 | if (!c) | |
775 | goto done; | |
776 | ||
6027f317 | 777 | usb_gadget_set_state(gadget, USB_STATE_CONFIGURED); |
40982be5 DB |
778 | cdev->config = c; |
779 | ||
780 | /* Initialize all interfaces by setting them to altsetting zero. */ | |
781 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { | |
782 | struct usb_function *f = c->interface[tmp]; | |
5242658d | 783 | struct usb_descriptor_header **descriptors; |
40982be5 DB |
784 | |
785 | if (!f) | |
786 | break; | |
787 | ||
5242658d LP |
788 | /* |
789 | * Record which endpoints are used by the function. This is used | |
790 | * to dispatch control requests targeted at that endpoint to the | |
791 | * function's setup callback instead of the current | |
792 | * configuration's setup callback. | |
793 | */ | |
f3bdbe36 | 794 | descriptors = function_descriptors(f, gadget->speed); |
5242658d LP |
795 | |
796 | for (; *descriptors; ++descriptors) { | |
797 | struct usb_endpoint_descriptor *ep; | |
798 | int addr; | |
799 | ||
800 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) | |
801 | continue; | |
802 | ||
803 | ep = (struct usb_endpoint_descriptor *)*descriptors; | |
804 | addr = ((ep->bEndpointAddress & 0x80) >> 3) | |
805 | | (ep->bEndpointAddress & 0x0f); | |
806 | set_bit(addr, f->endpoints); | |
807 | } | |
808 | ||
40982be5 DB |
809 | result = f->set_alt(f, tmp, 0); |
810 | if (result < 0) { | |
811 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", | |
812 | tmp, f->name, f, result); | |
813 | ||
814 | reset_config(cdev); | |
815 | goto done; | |
816 | } | |
1b9ba000 RQ |
817 | |
818 | if (result == USB_GADGET_DELAYED_STATUS) { | |
819 | DBG(cdev, | |
820 | "%s: interface %d (%s) requested delayed status\n", | |
821 | __func__, tmp, f->name); | |
822 | cdev->delayed_status++; | |
823 | DBG(cdev, "delayed_status count %d\n", | |
824 | cdev->delayed_status); | |
825 | } | |
40982be5 DB |
826 | } |
827 | ||
828 | /* when we return, be sure our power usage is valid */ | |
8f900a9a | 829 | power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW; |
40982be5 DB |
830 | done: |
831 | usb_gadget_vbus_draw(gadget, power); | |
1b9ba000 RQ |
832 | if (result >= 0 && cdev->delayed_status) |
833 | result = USB_GADGET_DELAYED_STATUS; | |
40982be5 DB |
834 | return result; |
835 | } | |
836 | ||
de53c254 SAS |
837 | int usb_add_config_only(struct usb_composite_dev *cdev, |
838 | struct usb_configuration *config) | |
839 | { | |
840 | struct usb_configuration *c; | |
841 | ||
842 | if (!config->bConfigurationValue) | |
843 | return -EINVAL; | |
844 | ||
845 | /* Prevent duplicate configuration identifiers */ | |
846 | list_for_each_entry(c, &cdev->configs, list) { | |
847 | if (c->bConfigurationValue == config->bConfigurationValue) | |
848 | return -EBUSY; | |
849 | } | |
850 | ||
851 | config->cdev = cdev; | |
852 | list_add_tail(&config->list, &cdev->configs); | |
853 | ||
854 | INIT_LIST_HEAD(&config->functions); | |
855 | config->next_interface_id = 0; | |
856 | memset(config->interface, 0, sizeof(config->interface)); | |
857 | ||
858 | return 0; | |
859 | } | |
860 | EXPORT_SYMBOL_GPL(usb_add_config_only); | |
861 | ||
40982be5 DB |
862 | /** |
863 | * usb_add_config() - add a configuration to a device. | |
864 | * @cdev: wraps the USB gadget | |
865 | * @config: the configuration, with bConfigurationValue assigned | |
c9bfff9c | 866 | * @bind: the configuration's bind function |
40982be5 DB |
867 | * Context: single threaded during gadget setup |
868 | * | |
c9bfff9c | 869 | * One of the main tasks of a composite @bind() routine is to |
40982be5 DB |
870 | * add each of the configurations it supports, using this routine. |
871 | * | |
c9bfff9c | 872 | * This function returns the value of the configuration's @bind(), which |
40982be5 DB |
873 | * is zero for success else a negative errno value. Binding configurations |
874 | * assigns global resources including string IDs, and per-configuration | |
875 | * resources such as interface IDs and endpoints. | |
876 | */ | |
28824b18 | 877 | int usb_add_config(struct usb_composite_dev *cdev, |
c9bfff9c UKK |
878 | struct usb_configuration *config, |
879 | int (*bind)(struct usb_configuration *)) | |
40982be5 DB |
880 | { |
881 | int status = -EINVAL; | |
de53c254 SAS |
882 | |
883 | if (!bind) | |
884 | goto done; | |
40982be5 DB |
885 | |
886 | DBG(cdev, "adding config #%u '%s'/%p\n", | |
887 | config->bConfigurationValue, | |
888 | config->label, config); | |
889 | ||
de53c254 SAS |
890 | status = usb_add_config_only(cdev, config); |
891 | if (status) | |
40982be5 DB |
892 | goto done; |
893 | ||
c9bfff9c | 894 | status = bind(config); |
40982be5 | 895 | if (status < 0) { |
124ef389 YO |
896 | while (!list_empty(&config->functions)) { |
897 | struct usb_function *f; | |
898 | ||
899 | f = list_first_entry(&config->functions, | |
900 | struct usb_function, list); | |
901 | list_del(&f->list); | |
902 | if (f->unbind) { | |
903 | DBG(cdev, "unbind function '%s'/%p\n", | |
904 | f->name, f); | |
905 | f->unbind(config, f); | |
906 | /* may free memory for "f" */ | |
907 | } | |
908 | } | |
40982be5 DB |
909 | list_del(&config->list); |
910 | config->cdev = NULL; | |
911 | } else { | |
912 | unsigned i; | |
913 | ||
cd69cbeb | 914 | DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n", |
40982be5 | 915 | config->bConfigurationValue, config, |
cd69cbeb | 916 | config->superspeed_plus ? " superplus" : "", |
bdb64d72 | 917 | config->superspeed ? " super" : "", |
40982be5 DB |
918 | config->highspeed ? " high" : "", |
919 | config->fullspeed | |
920 | ? (gadget_is_dualspeed(cdev->gadget) | |
921 | ? " full" | |
922 | : " full/low") | |
923 | : ""); | |
924 | ||
925 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { | |
926 | struct usb_function *f = config->interface[i]; | |
927 | ||
928 | if (!f) | |
929 | continue; | |
930 | DBG(cdev, " interface %d = %s/%p\n", | |
931 | i, f->name, f); | |
932 | } | |
933 | } | |
934 | ||
f871cb9b | 935 | /* set_alt(), or next bind(), sets up ep->claimed as needed */ |
40982be5 DB |
936 | usb_ep_autoconfig_reset(cdev->gadget); |
937 | ||
938 | done: | |
939 | if (status) | |
940 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, | |
941 | config->bConfigurationValue, status); | |
942 | return status; | |
943 | } | |
721e2e91 | 944 | EXPORT_SYMBOL_GPL(usb_add_config); |
40982be5 | 945 | |
51cce6fc BG |
946 | static void remove_config(struct usb_composite_dev *cdev, |
947 | struct usb_configuration *config) | |
948 | { | |
949 | while (!list_empty(&config->functions)) { | |
950 | struct usb_function *f; | |
951 | ||
952 | f = list_first_entry(&config->functions, | |
953 | struct usb_function, list); | |
954 | list_del(&f->list); | |
955 | if (f->unbind) { | |
956 | DBG(cdev, "unbind function '%s'/%p\n", f->name, f); | |
957 | f->unbind(config, f); | |
958 | /* may free memory for "f" */ | |
959 | } | |
960 | } | |
961 | list_del(&config->list); | |
962 | if (config->unbind) { | |
963 | DBG(cdev, "unbind config '%s'/%p\n", config->label, config); | |
964 | config->unbind(config); | |
965 | /* may free memory for "c" */ | |
966 | } | |
967 | } | |
968 | ||
969 | /** | |
970 | * usb_remove_config() - remove a configuration from a device. | |
971 | * @cdev: wraps the USB gadget | |
972 | * @config: the configuration | |
973 | * | |
974 | * Drivers must call usb_gadget_disconnect before calling this function | |
975 | * to disconnect the device from the host and make sure the host will not | |
976 | * try to enumerate the device while we are changing the config list. | |
977 | */ | |
978 | void usb_remove_config(struct usb_composite_dev *cdev, | |
979 | struct usb_configuration *config) | |
980 | { | |
981 | unsigned long flags; | |
982 | ||
983 | spin_lock_irqsave(&cdev->lock, flags); | |
984 | ||
985 | if (cdev->config == config) | |
986 | reset_config(cdev); | |
987 | ||
988 | spin_unlock_irqrestore(&cdev->lock, flags); | |
989 | ||
990 | remove_config(cdev, config); | |
991 | } | |
992 | ||
40982be5 DB |
993 | /*-------------------------------------------------------------------------*/ |
994 | ||
995 | /* We support strings in multiple languages ... string descriptor zero | |
996 | * says which languages are supported. The typical case will be that | |
ad4676ab | 997 | * only one language (probably English) is used, with i18n handled on |
40982be5 DB |
998 | * the host side. |
999 | */ | |
1000 | ||
1001 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) | |
1002 | { | |
1003 | const struct usb_gadget_strings *s; | |
20c5e74c | 1004 | __le16 language; |
40982be5 DB |
1005 | __le16 *tmp; |
1006 | ||
1007 | while (*sp) { | |
1008 | s = *sp; | |
1009 | language = cpu_to_le16(s->language); | |
1010 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { | |
1011 | if (*tmp == language) | |
1012 | goto repeat; | |
1013 | } | |
1014 | *tmp++ = language; | |
1015 | repeat: | |
1016 | sp++; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | static int lookup_string( | |
1021 | struct usb_gadget_strings **sp, | |
1022 | void *buf, | |
1023 | u16 language, | |
1024 | int id | |
1025 | ) | |
1026 | { | |
1027 | struct usb_gadget_strings *s; | |
1028 | int value; | |
1029 | ||
1030 | while (*sp) { | |
1031 | s = *sp++; | |
1032 | if (s->language != language) | |
1033 | continue; | |
1034 | value = usb_gadget_get_string(s, id, buf); | |
1035 | if (value > 0) | |
1036 | return value; | |
1037 | } | |
1038 | return -EINVAL; | |
1039 | } | |
1040 | ||
1041 | static int get_string(struct usb_composite_dev *cdev, | |
1042 | void *buf, u16 language, int id) | |
1043 | { | |
ffe0b335 | 1044 | struct usb_composite_driver *composite = cdev->driver; |
9bb2859f | 1045 | struct usb_gadget_string_container *uc; |
40982be5 DB |
1046 | struct usb_configuration *c; |
1047 | struct usb_function *f; | |
1048 | int len; | |
1049 | ||
ad4676ab | 1050 | /* Yes, not only is USB's i18n support probably more than most |
40982be5 DB |
1051 | * folk will ever care about ... also, it's all supported here. |
1052 | * (Except for UTF8 support for Unicode's "Astral Planes".) | |
1053 | */ | |
1054 | ||
1055 | /* 0 == report all available language codes */ | |
1056 | if (id == 0) { | |
1057 | struct usb_string_descriptor *s = buf; | |
1058 | struct usb_gadget_strings **sp; | |
1059 | ||
1060 | memset(s, 0, 256); | |
1061 | s->bDescriptorType = USB_DT_STRING; | |
1062 | ||
1063 | sp = composite->strings; | |
1064 | if (sp) | |
1065 | collect_langs(sp, s->wData); | |
1066 | ||
1067 | list_for_each_entry(c, &cdev->configs, list) { | |
1068 | sp = c->strings; | |
1069 | if (sp) | |
1070 | collect_langs(sp, s->wData); | |
1071 | ||
1072 | list_for_each_entry(f, &c->functions, list) { | |
1073 | sp = f->strings; | |
1074 | if (sp) | |
1075 | collect_langs(sp, s->wData); | |
1076 | } | |
1077 | } | |
27a46633 SAS |
1078 | list_for_each_entry(uc, &cdev->gstrings, list) { |
1079 | struct usb_gadget_strings **sp; | |
1080 | ||
1081 | sp = get_containers_gs(uc); | |
1082 | collect_langs(sp, s->wData); | |
1083 | } | |
40982be5 | 1084 | |
417b57b3 | 1085 | for (len = 0; len <= 126 && s->wData[len]; len++) |
40982be5 DB |
1086 | continue; |
1087 | if (!len) | |
1088 | return -EINVAL; | |
1089 | ||
1090 | s->bLength = 2 * (len + 1); | |
1091 | return s->bLength; | |
1092 | } | |
1093 | ||
19824d5e AP |
1094 | if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { |
1095 | struct usb_os_string *b = buf; | |
1096 | b->bLength = sizeof(*b); | |
1097 | b->bDescriptorType = USB_DT_STRING; | |
1098 | compiletime_assert( | |
1099 | sizeof(b->qwSignature) == sizeof(cdev->qw_sign), | |
1100 | "qwSignature size must be equal to qw_sign"); | |
1101 | memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); | |
1102 | b->bMS_VendorCode = cdev->b_vendor_code; | |
1103 | b->bPad = 0; | |
1104 | return sizeof(*b); | |
1105 | } | |
1106 | ||
9bb2859f SAS |
1107 | list_for_each_entry(uc, &cdev->gstrings, list) { |
1108 | struct usb_gadget_strings **sp; | |
1109 | ||
1110 | sp = get_containers_gs(uc); | |
1111 | len = lookup_string(sp, buf, language, id); | |
1112 | if (len > 0) | |
1113 | return len; | |
1114 | } | |
1115 | ||
ad1a8102 MN |
1116 | /* String IDs are device-scoped, so we look up each string |
1117 | * table we're told about. These lookups are infrequent; | |
1118 | * simpler-is-better here. | |
40982be5 DB |
1119 | */ |
1120 | if (composite->strings) { | |
1121 | len = lookup_string(composite->strings, buf, language, id); | |
1122 | if (len > 0) | |
1123 | return len; | |
1124 | } | |
1125 | list_for_each_entry(c, &cdev->configs, list) { | |
1126 | if (c->strings) { | |
1127 | len = lookup_string(c->strings, buf, language, id); | |
1128 | if (len > 0) | |
1129 | return len; | |
1130 | } | |
1131 | list_for_each_entry(f, &c->functions, list) { | |
1132 | if (!f->strings) | |
1133 | continue; | |
1134 | len = lookup_string(f->strings, buf, language, id); | |
1135 | if (len > 0) | |
1136 | return len; | |
1137 | } | |
1138 | } | |
1139 | return -EINVAL; | |
1140 | } | |
1141 | ||
1142 | /** | |
1143 | * usb_string_id() - allocate an unused string ID | |
1144 | * @cdev: the device whose string descriptor IDs are being allocated | |
1145 | * Context: single threaded during gadget setup | |
1146 | * | |
1147 | * @usb_string_id() is called from bind() callbacks to allocate | |
1148 | * string IDs. Drivers for functions, configurations, or gadgets will | |
1149 | * then store that ID in the appropriate descriptors and string table. | |
1150 | * | |
f2adc4f8 MN |
1151 | * All string identifier should be allocated using this, |
1152 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure | |
1153 | * that for example different functions don't wrongly assign different | |
1154 | * meanings to the same identifier. | |
40982be5 | 1155 | */ |
28824b18 | 1156 | int usb_string_id(struct usb_composite_dev *cdev) |
40982be5 DB |
1157 | { |
1158 | if (cdev->next_string_id < 254) { | |
f2adc4f8 MN |
1159 | /* string id 0 is reserved by USB spec for list of |
1160 | * supported languages */ | |
1161 | /* 255 reserved as well? -- mina86 */ | |
40982be5 DB |
1162 | cdev->next_string_id++; |
1163 | return cdev->next_string_id; | |
1164 | } | |
1165 | return -ENODEV; | |
1166 | } | |
721e2e91 | 1167 | EXPORT_SYMBOL_GPL(usb_string_id); |
40982be5 | 1168 | |
f2adc4f8 MN |
1169 | /** |
1170 | * usb_string_ids() - allocate unused string IDs in batch | |
1171 | * @cdev: the device whose string descriptor IDs are being allocated | |
1172 | * @str: an array of usb_string objects to assign numbers to | |
1173 | * Context: single threaded during gadget setup | |
1174 | * | |
1175 | * @usb_string_ids() is called from bind() callbacks to allocate | |
1176 | * string IDs. Drivers for functions, configurations, or gadgets will | |
1177 | * then copy IDs from the string table to the appropriate descriptors | |
1178 | * and string table for other languages. | |
1179 | * | |
1180 | * All string identifier should be allocated using this, | |
1181 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for | |
1182 | * example different functions don't wrongly assign different meanings | |
1183 | * to the same identifier. | |
1184 | */ | |
1185 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) | |
1186 | { | |
1187 | int next = cdev->next_string_id; | |
1188 | ||
1189 | for (; str->s; ++str) { | |
1190 | if (unlikely(next >= 254)) | |
1191 | return -ENODEV; | |
1192 | str->id = ++next; | |
1193 | } | |
1194 | ||
1195 | cdev->next_string_id = next; | |
1196 | ||
1197 | return 0; | |
1198 | } | |
721e2e91 | 1199 | EXPORT_SYMBOL_GPL(usb_string_ids_tab); |
f2adc4f8 | 1200 | |
9bb2859f SAS |
1201 | static struct usb_gadget_string_container *copy_gadget_strings( |
1202 | struct usb_gadget_strings **sp, unsigned n_gstrings, | |
1203 | unsigned n_strings) | |
1204 | { | |
1205 | struct usb_gadget_string_container *uc; | |
1206 | struct usb_gadget_strings **gs_array; | |
1207 | struct usb_gadget_strings *gs; | |
1208 | struct usb_string *s; | |
1209 | unsigned mem; | |
1210 | unsigned n_gs; | |
1211 | unsigned n_s; | |
1212 | void *stash; | |
1213 | ||
1214 | mem = sizeof(*uc); | |
1215 | mem += sizeof(void *) * (n_gstrings + 1); | |
1216 | mem += sizeof(struct usb_gadget_strings) * n_gstrings; | |
1217 | mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings); | |
1218 | uc = kmalloc(mem, GFP_KERNEL); | |
1219 | if (!uc) | |
1220 | return ERR_PTR(-ENOMEM); | |
1221 | gs_array = get_containers_gs(uc); | |
1222 | stash = uc->stash; | |
1223 | stash += sizeof(void *) * (n_gstrings + 1); | |
1224 | for (n_gs = 0; n_gs < n_gstrings; n_gs++) { | |
1225 | struct usb_string *org_s; | |
1226 | ||
1227 | gs_array[n_gs] = stash; | |
1228 | gs = gs_array[n_gs]; | |
1229 | stash += sizeof(struct usb_gadget_strings); | |
1230 | gs->language = sp[n_gs]->language; | |
1231 | gs->strings = stash; | |
1232 | org_s = sp[n_gs]->strings; | |
1233 | ||
1234 | for (n_s = 0; n_s < n_strings; n_s++) { | |
1235 | s = stash; | |
1236 | stash += sizeof(struct usb_string); | |
1237 | if (org_s->s) | |
1238 | s->s = org_s->s; | |
1239 | else | |
1240 | s->s = ""; | |
1241 | org_s++; | |
1242 | } | |
1243 | s = stash; | |
1244 | s->s = NULL; | |
1245 | stash += sizeof(struct usb_string); | |
1246 | ||
1247 | } | |
1248 | gs_array[n_gs] = NULL; | |
1249 | return uc; | |
1250 | } | |
1251 | ||
1252 | /** | |
1253 | * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids | |
1254 | * @cdev: the device whose string descriptor IDs are being allocated | |
1255 | * and attached. | |
1256 | * @sp: an array of usb_gadget_strings to attach. | |
1257 | * @n_strings: number of entries in each usb_strings array (sp[]->strings) | |
1258 | * | |
1259 | * This function will create a deep copy of usb_gadget_strings and usb_string | |
1260 | * and attach it to the cdev. The actual string (usb_string.s) will not be | |
1261 | * copied but only a referenced will be made. The struct usb_gadget_strings | |
06ed0de5 | 1262 | * array may contain multiple languages and should be NULL terminated. |
9bb2859f SAS |
1263 | * The ->language pointer of each struct usb_gadget_strings has to contain the |
1264 | * same amount of entries. | |
1265 | * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first | |
06ed0de5 | 1266 | * usb_string entry of es-ES contains the translation of the first usb_string |
9bb2859f SAS |
1267 | * entry of en-US. Therefore both entries become the same id assign. |
1268 | */ | |
1269 | struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, | |
1270 | struct usb_gadget_strings **sp, unsigned n_strings) | |
1271 | { | |
1272 | struct usb_gadget_string_container *uc; | |
1273 | struct usb_gadget_strings **n_gs; | |
1274 | unsigned n_gstrings = 0; | |
1275 | unsigned i; | |
1276 | int ret; | |
1277 | ||
1278 | for (i = 0; sp[i]; i++) | |
1279 | n_gstrings++; | |
1280 | ||
1281 | if (!n_gstrings) | |
1282 | return ERR_PTR(-EINVAL); | |
1283 | ||
1284 | uc = copy_gadget_strings(sp, n_gstrings, n_strings); | |
1285 | if (IS_ERR(uc)) | |
dad4babe | 1286 | return ERR_CAST(uc); |
9bb2859f SAS |
1287 | |
1288 | n_gs = get_containers_gs(uc); | |
1289 | ret = usb_string_ids_tab(cdev, n_gs[0]->strings); | |
1290 | if (ret) | |
1291 | goto err; | |
1292 | ||
1293 | for (i = 1; i < n_gstrings; i++) { | |
1294 | struct usb_string *m_s; | |
1295 | struct usb_string *s; | |
1296 | unsigned n; | |
1297 | ||
1298 | m_s = n_gs[0]->strings; | |
1299 | s = n_gs[i]->strings; | |
1300 | for (n = 0; n < n_strings; n++) { | |
1301 | s->id = m_s->id; | |
1302 | s++; | |
1303 | m_s++; | |
1304 | } | |
1305 | } | |
1306 | list_add_tail(&uc->list, &cdev->gstrings); | |
1307 | return n_gs[0]->strings; | |
1308 | err: | |
1309 | kfree(uc); | |
1310 | return ERR_PTR(ret); | |
1311 | } | |
1312 | EXPORT_SYMBOL_GPL(usb_gstrings_attach); | |
1313 | ||
f2adc4f8 MN |
1314 | /** |
1315 | * usb_string_ids_n() - allocate unused string IDs in batch | |
d187abb9 | 1316 | * @c: the device whose string descriptor IDs are being allocated |
f2adc4f8 MN |
1317 | * @n: number of string IDs to allocate |
1318 | * Context: single threaded during gadget setup | |
1319 | * | |
1320 | * Returns the first requested ID. This ID and next @n-1 IDs are now | |
d187abb9 | 1321 | * valid IDs. At least provided that @n is non-zero because if it |
f2adc4f8 MN |
1322 | * is, returns last requested ID which is now very useful information. |
1323 | * | |
1324 | * @usb_string_ids_n() is called from bind() callbacks to allocate | |
1325 | * string IDs. Drivers for functions, configurations, or gadgets will | |
1326 | * then store that ID in the appropriate descriptors and string table. | |
1327 | * | |
1328 | * All string identifier should be allocated using this, | |
1329 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for | |
1330 | * example different functions don't wrongly assign different meanings | |
1331 | * to the same identifier. | |
1332 | */ | |
1333 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) | |
1334 | { | |
1335 | unsigned next = c->next_string_id; | |
1336 | if (unlikely(n > 254 || (unsigned)next + n > 254)) | |
1337 | return -ENODEV; | |
1338 | c->next_string_id += n; | |
1339 | return next + 1; | |
1340 | } | |
721e2e91 | 1341 | EXPORT_SYMBOL_GPL(usb_string_ids_n); |
f2adc4f8 | 1342 | |
40982be5 DB |
1343 | /*-------------------------------------------------------------------------*/ |
1344 | ||
1345 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) | |
1346 | { | |
a7c12eaf FB |
1347 | struct usb_composite_dev *cdev; |
1348 | ||
40982be5 DB |
1349 | if (req->status || req->actual != req->length) |
1350 | DBG((struct usb_composite_dev *) ep->driver_data, | |
1351 | "setup complete --> %d, %d/%d\n", | |
1352 | req->status, req->actual, req->length); | |
a7c12eaf FB |
1353 | |
1354 | /* | |
1355 | * REVIST The same ep0 requests are shared with function drivers | |
1356 | * so they don't have to maintain the same ->complete() stubs. | |
1357 | * | |
1358 | * Because of that, we need to check for the validity of ->context | |
1359 | * here, even though we know we've set it to something useful. | |
1360 | */ | |
1361 | if (!req->context) | |
1362 | return; | |
1363 | ||
1364 | cdev = req->context; | |
1365 | ||
1366 | if (cdev->req == req) | |
1367 | cdev->setup_pending = false; | |
1368 | else if (cdev->os_desc_req == req) | |
1369 | cdev->os_desc_pending = false; | |
1370 | else | |
1371 | WARN(1, "unknown request %p\n", req); | |
1372 | } | |
1373 | ||
1374 | static int composite_ep0_queue(struct usb_composite_dev *cdev, | |
1375 | struct usb_request *req, gfp_t gfp_flags) | |
1376 | { | |
1377 | int ret; | |
1378 | ||
1379 | ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags); | |
1380 | if (ret == 0) { | |
1381 | if (cdev->req == req) | |
1382 | cdev->setup_pending = true; | |
1383 | else if (cdev->os_desc_req == req) | |
1384 | cdev->os_desc_pending = true; | |
1385 | else | |
1386 | WARN(1, "unknown request %p\n", req); | |
1387 | } | |
1388 | ||
1389 | return ret; | |
40982be5 DB |
1390 | } |
1391 | ||
37a3a533 AP |
1392 | static int count_ext_compat(struct usb_configuration *c) |
1393 | { | |
1394 | int i, res; | |
1395 | ||
1396 | res = 0; | |
1397 | for (i = 0; i < c->next_interface_id; ++i) { | |
1398 | struct usb_function *f; | |
1399 | int j; | |
1400 | ||
1401 | f = c->interface[i]; | |
1402 | for (j = 0; j < f->os_desc_n; ++j) { | |
1403 | struct usb_os_desc *d; | |
1404 | ||
1405 | if (i != f->os_desc_table[j].if_id) | |
1406 | continue; | |
1407 | d = f->os_desc_table[j].os_desc; | |
1408 | if (d && d->ext_compat_id) | |
1409 | ++res; | |
1410 | } | |
1411 | } | |
1412 | BUG_ON(res > 255); | |
1413 | return res; | |
1414 | } | |
1415 | ||
1416 | static void fill_ext_compat(struct usb_configuration *c, u8 *buf) | |
1417 | { | |
1418 | int i, count; | |
1419 | ||
1420 | count = 16; | |
1421 | for (i = 0; i < c->next_interface_id; ++i) { | |
1422 | struct usb_function *f; | |
1423 | int j; | |
1424 | ||
1425 | f = c->interface[i]; | |
1426 | for (j = 0; j < f->os_desc_n; ++j) { | |
1427 | struct usb_os_desc *d; | |
1428 | ||
1429 | if (i != f->os_desc_table[j].if_id) | |
1430 | continue; | |
1431 | d = f->os_desc_table[j].os_desc; | |
1432 | if (d && d->ext_compat_id) { | |
1433 | *buf++ = i; | |
1434 | *buf++ = 0x01; | |
1435 | memcpy(buf, d->ext_compat_id, 16); | |
1436 | buf += 22; | |
1437 | } else { | |
1438 | ++buf; | |
1439 | *buf = 0x01; | |
1440 | buf += 23; | |
1441 | } | |
1442 | count += 24; | |
1443 | if (count >= 4096) | |
1444 | return; | |
1445 | } | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | static int count_ext_prop(struct usb_configuration *c, int interface) | |
1450 | { | |
1451 | struct usb_function *f; | |
849b1333 | 1452 | int j; |
37a3a533 AP |
1453 | |
1454 | f = c->interface[interface]; | |
1455 | for (j = 0; j < f->os_desc_n; ++j) { | |
1456 | struct usb_os_desc *d; | |
1457 | ||
1458 | if (interface != f->os_desc_table[j].if_id) | |
1459 | continue; | |
1460 | d = f->os_desc_table[j].os_desc; | |
1461 | if (d && d->ext_compat_id) | |
1462 | return d->ext_prop_count; | |
1463 | } | |
849b1333 | 1464 | return 0; |
37a3a533 AP |
1465 | } |
1466 | ||
1467 | static int len_ext_prop(struct usb_configuration *c, int interface) | |
1468 | { | |
1469 | struct usb_function *f; | |
1470 | struct usb_os_desc *d; | |
1471 | int j, res; | |
1472 | ||
1473 | res = 10; /* header length */ | |
1474 | f = c->interface[interface]; | |
1475 | for (j = 0; j < f->os_desc_n; ++j) { | |
1476 | if (interface != f->os_desc_table[j].if_id) | |
1477 | continue; | |
1478 | d = f->os_desc_table[j].os_desc; | |
1479 | if (d) | |
1480 | return min(res + d->ext_prop_len, 4096); | |
1481 | } | |
1482 | return res; | |
1483 | } | |
1484 | ||
1485 | static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) | |
1486 | { | |
1487 | struct usb_function *f; | |
1488 | struct usb_os_desc *d; | |
1489 | struct usb_os_desc_ext_prop *ext_prop; | |
1490 | int j, count, n, ret; | |
1491 | u8 *start = buf; | |
1492 | ||
1493 | f = c->interface[interface]; | |
1494 | for (j = 0; j < f->os_desc_n; ++j) { | |
1495 | if (interface != f->os_desc_table[j].if_id) | |
1496 | continue; | |
1497 | d = f->os_desc_table[j].os_desc; | |
1498 | if (d) | |
1499 | list_for_each_entry(ext_prop, &d->ext_prop, entry) { | |
1500 | /* 4kB minus header length */ | |
1501 | n = buf - start; | |
1502 | if (n >= 4086) | |
1503 | return 0; | |
1504 | ||
1505 | count = ext_prop->data_len + | |
1506 | ext_prop->name_len + 14; | |
1507 | if (count > 4086 - n) | |
1508 | return -EINVAL; | |
1509 | usb_ext_prop_put_size(buf, count); | |
1510 | usb_ext_prop_put_type(buf, ext_prop->type); | |
1511 | ret = usb_ext_prop_put_name(buf, ext_prop->name, | |
1512 | ext_prop->name_len); | |
1513 | if (ret < 0) | |
1514 | return ret; | |
1515 | switch (ext_prop->type) { | |
1516 | case USB_EXT_PROP_UNICODE: | |
1517 | case USB_EXT_PROP_UNICODE_ENV: | |
1518 | case USB_EXT_PROP_UNICODE_LINK: | |
1519 | usb_ext_prop_put_unicode(buf, ret, | |
1520 | ext_prop->data, | |
1521 | ext_prop->data_len); | |
1522 | break; | |
1523 | case USB_EXT_PROP_BINARY: | |
1524 | usb_ext_prop_put_binary(buf, ret, | |
1525 | ext_prop->data, | |
1526 | ext_prop->data_len); | |
1527 | break; | |
1528 | case USB_EXT_PROP_LE32: | |
1529 | /* not implemented */ | |
1530 | case USB_EXT_PROP_BE32: | |
1531 | /* not implemented */ | |
1532 | default: | |
1533 | return -EINVAL; | |
1534 | } | |
1535 | buf += count; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | return 0; | |
1540 | } | |
1541 | ||
40982be5 DB |
1542 | /* |
1543 | * The setup() callback implements all the ep0 functionality that's | |
1544 | * not handled lower down, in hardware or the hardware driver(like | |
1545 | * device and endpoint feature flags, and their status). It's all | |
1546 | * housekeeping for the gadget function we're implementing. Most of | |
1547 | * the work is in config and function specific setup. | |
1548 | */ | |
2d5a8899 | 1549 | int |
40982be5 DB |
1550 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
1551 | { | |
1552 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1553 | struct usb_request *req = cdev->req; | |
1554 | int value = -EOPNOTSUPP; | |
bdb64d72 | 1555 | int status = 0; |
40982be5 | 1556 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
08889517 | 1557 | u8 intf = w_index & 0xFF; |
40982be5 DB |
1558 | u16 w_value = le16_to_cpu(ctrl->wValue); |
1559 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
1560 | struct usb_function *f = NULL; | |
5242658d | 1561 | u8 endp; |
40982be5 DB |
1562 | |
1563 | /* partial re-init of the response message; the function or the | |
1564 | * gadget might need to intercept e.g. a control-OUT completion | |
1565 | * when we delegate to it. | |
1566 | */ | |
1567 | req->zero = 0; | |
57943716 | 1568 | req->context = cdev; |
40982be5 | 1569 | req->complete = composite_setup_complete; |
2edb11cb | 1570 | req->length = 0; |
40982be5 DB |
1571 | gadget->ep0->driver_data = cdev; |
1572 | ||
232c0102 AP |
1573 | /* |
1574 | * Don't let non-standard requests match any of the cases below | |
1575 | * by accident. | |
1576 | */ | |
1577 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) | |
1578 | goto unknown; | |
1579 | ||
40982be5 DB |
1580 | switch (ctrl->bRequest) { |
1581 | ||
1582 | /* we handle all standard USB descriptors */ | |
1583 | case USB_REQ_GET_DESCRIPTOR: | |
1584 | if (ctrl->bRequestType != USB_DIR_IN) | |
1585 | goto unknown; | |
1586 | switch (w_value >> 8) { | |
1587 | ||
1588 | case USB_DT_DEVICE: | |
1589 | cdev->desc.bNumConfigurations = | |
1590 | count_configs(cdev, USB_DT_DEVICE); | |
bdb64d72 TB |
1591 | cdev->desc.bMaxPacketSize0 = |
1592 | cdev->gadget->ep0->maxpacket; | |
1593 | if (gadget_is_superspeed(gadget)) { | |
a8f21156 | 1594 | if (gadget->speed >= USB_SPEED_SUPER) { |
1a853291 | 1595 | cdev->desc.bcdUSB = cpu_to_le16(0x0310); |
a8f21156 SAS |
1596 | cdev->desc.bMaxPacketSize0 = 9; |
1597 | } else { | |
bdb64d72 | 1598 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
a8f21156 | 1599 | } |
5527e733 IK |
1600 | } else { |
1601 | cdev->desc.bcdUSB = cpu_to_le16(0x0200); | |
bdb64d72 TB |
1602 | } |
1603 | ||
40982be5 DB |
1604 | value = min(w_length, (u16) sizeof cdev->desc); |
1605 | memcpy(req->buf, &cdev->desc, value); | |
1606 | break; | |
1607 | case USB_DT_DEVICE_QUALIFIER: | |
bdb64d72 TB |
1608 | if (!gadget_is_dualspeed(gadget) || |
1609 | gadget->speed >= USB_SPEED_SUPER) | |
40982be5 DB |
1610 | break; |
1611 | device_qual(cdev); | |
1612 | value = min_t(int, w_length, | |
1613 | sizeof(struct usb_qualifier_descriptor)); | |
1614 | break; | |
1615 | case USB_DT_OTHER_SPEED_CONFIG: | |
bdb64d72 TB |
1616 | if (!gadget_is_dualspeed(gadget) || |
1617 | gadget->speed >= USB_SPEED_SUPER) | |
40982be5 DB |
1618 | break; |
1619 | /* FALLTHROUGH */ | |
1620 | case USB_DT_CONFIG: | |
1621 | value = config_desc(cdev, w_value); | |
1622 | if (value >= 0) | |
1623 | value = min(w_length, (u16) value); | |
1624 | break; | |
1625 | case USB_DT_STRING: | |
1626 | value = get_string(cdev, req->buf, | |
1627 | w_index, w_value & 0xff); | |
1628 | if (value >= 0) | |
1629 | value = min(w_length, (u16) value); | |
1630 | break; | |
bdb64d72 TB |
1631 | case USB_DT_BOS: |
1632 | if (gadget_is_superspeed(gadget)) { | |
1633 | value = bos_desc(cdev); | |
1634 | value = min(w_length, (u16) value); | |
1635 | } | |
1636 | break; | |
53e6242d ML |
1637 | case USB_DT_OTG: |
1638 | if (gadget_is_otg(gadget)) { | |
1639 | struct usb_configuration *config; | |
1640 | int otg_desc_len = 0; | |
1641 | ||
1642 | if (cdev->config) | |
1643 | config = cdev->config; | |
1644 | else | |
1645 | config = list_first_entry( | |
1646 | &cdev->configs, | |
1647 | struct usb_configuration, list); | |
1648 | if (!config) | |
1649 | goto done; | |
1650 | ||
1651 | if (gadget->otg_caps && | |
1652 | (gadget->otg_caps->otg_rev >= 0x0200)) | |
1653 | otg_desc_len += sizeof( | |
1654 | struct usb_otg20_descriptor); | |
1655 | else | |
1656 | otg_desc_len += sizeof( | |
1657 | struct usb_otg_descriptor); | |
1658 | ||
1659 | value = min_t(int, w_length, otg_desc_len); | |
1660 | memcpy(req->buf, config->descriptors[0], value); | |
1661 | } | |
1662 | break; | |
40982be5 DB |
1663 | } |
1664 | break; | |
1665 | ||
1666 | /* any number of configs can work */ | |
1667 | case USB_REQ_SET_CONFIGURATION: | |
1668 | if (ctrl->bRequestType != 0) | |
1669 | goto unknown; | |
1670 | if (gadget_is_otg(gadget)) { | |
1671 | if (gadget->a_hnp_support) | |
1672 | DBG(cdev, "HNP available\n"); | |
1673 | else if (gadget->a_alt_hnp_support) | |
1674 | DBG(cdev, "HNP on another port\n"); | |
1675 | else | |
1676 | VDBG(cdev, "HNP inactive\n"); | |
1677 | } | |
1678 | spin_lock(&cdev->lock); | |
1679 | value = set_config(cdev, ctrl, w_value); | |
1680 | spin_unlock(&cdev->lock); | |
1681 | break; | |
1682 | case USB_REQ_GET_CONFIGURATION: | |
1683 | if (ctrl->bRequestType != USB_DIR_IN) | |
1684 | goto unknown; | |
1685 | if (cdev->config) | |
1686 | *(u8 *)req->buf = cdev->config->bConfigurationValue; | |
1687 | else | |
1688 | *(u8 *)req->buf = 0; | |
1689 | value = min(w_length, (u16) 1); | |
1690 | break; | |
1691 | ||
1692 | /* function drivers must handle get/set altsetting; if there's | |
1693 | * no get() method, we know only altsetting zero works. | |
1694 | */ | |
1695 | case USB_REQ_SET_INTERFACE: | |
1696 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) | |
1697 | goto unknown; | |
ff085de7 | 1698 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
40982be5 | 1699 | break; |
08889517 | 1700 | f = cdev->config->interface[intf]; |
40982be5 DB |
1701 | if (!f) |
1702 | break; | |
dd4dff8b | 1703 | if (w_value && !f->set_alt) |
40982be5 DB |
1704 | break; |
1705 | value = f->set_alt(f, w_index, w_value); | |
1b9ba000 RQ |
1706 | if (value == USB_GADGET_DELAYED_STATUS) { |
1707 | DBG(cdev, | |
1708 | "%s: interface %d (%s) requested delayed status\n", | |
1709 | __func__, intf, f->name); | |
1710 | cdev->delayed_status++; | |
1711 | DBG(cdev, "delayed_status count %d\n", | |
1712 | cdev->delayed_status); | |
1713 | } | |
40982be5 DB |
1714 | break; |
1715 | case USB_REQ_GET_INTERFACE: | |
1716 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) | |
1717 | goto unknown; | |
ff085de7 | 1718 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
40982be5 | 1719 | break; |
08889517 | 1720 | f = cdev->config->interface[intf]; |
40982be5 DB |
1721 | if (!f) |
1722 | break; | |
1723 | /* lots of interfaces only need altsetting zero... */ | |
1724 | value = f->get_alt ? f->get_alt(f, w_index) : 0; | |
1725 | if (value < 0) | |
1726 | break; | |
1727 | *((u8 *)req->buf) = value; | |
1728 | value = min(w_length, (u16) 1); | |
1729 | break; | |
bdb64d72 | 1730 | case USB_REQ_GET_STATUS: |
c5348b62 LJ |
1731 | if (gadget_is_otg(gadget) && gadget->hnp_polling_support && |
1732 | (w_index == OTG_STS_SELECTOR)) { | |
1733 | if (ctrl->bRequestType != (USB_DIR_IN | | |
1734 | USB_RECIP_DEVICE)) | |
1735 | goto unknown; | |
1736 | *((u8 *)req->buf) = gadget->host_request_flag; | |
1737 | value = 1; | |
1738 | break; | |
1739 | } | |
1740 | ||
1741 | /* | |
1742 | * USB 3.0 additions: | |
1743 | * Function driver should handle get_status request. If such cb | |
1744 | * wasn't supplied we respond with default value = 0 | |
1745 | * Note: function driver should supply such cb only for the | |
1746 | * first interface of the function | |
1747 | */ | |
bdb64d72 TB |
1748 | if (!gadget_is_superspeed(gadget)) |
1749 | goto unknown; | |
1750 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) | |
1751 | goto unknown; | |
1752 | value = 2; /* This is the length of the get_status reply */ | |
1753 | put_unaligned_le16(0, req->buf); | |
1754 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) | |
1755 | break; | |
1756 | f = cdev->config->interface[intf]; | |
1757 | if (!f) | |
1758 | break; | |
1759 | status = f->get_status ? f->get_status(f) : 0; | |
1760 | if (status < 0) | |
1761 | break; | |
1762 | put_unaligned_le16(status & 0x0000ffff, req->buf); | |
1763 | break; | |
1764 | /* | |
1765 | * Function drivers should handle SetFeature/ClearFeature | |
1766 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied | |
1767 | * only for the first interface of the function | |
1768 | */ | |
1769 | case USB_REQ_CLEAR_FEATURE: | |
1770 | case USB_REQ_SET_FEATURE: | |
1771 | if (!gadget_is_superspeed(gadget)) | |
1772 | goto unknown; | |
1773 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) | |
1774 | goto unknown; | |
1775 | switch (w_value) { | |
1776 | case USB_INTRF_FUNC_SUSPEND: | |
1777 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) | |
1778 | break; | |
1779 | f = cdev->config->interface[intf]; | |
1780 | if (!f) | |
1781 | break; | |
1782 | value = 0; | |
1783 | if (f->func_suspend) | |
1784 | value = f->func_suspend(f, w_index >> 8); | |
1785 | if (value < 0) { | |
1786 | ERROR(cdev, | |
1787 | "func_suspend() returned error %d\n", | |
1788 | value); | |
1789 | value = 0; | |
1790 | } | |
1791 | break; | |
1792 | } | |
1793 | break; | |
40982be5 DB |
1794 | default: |
1795 | unknown: | |
37a3a533 AP |
1796 | /* |
1797 | * OS descriptors handling | |
1798 | */ | |
1799 | if (cdev->use_os_string && cdev->os_desc_config && | |
df6738d0 | 1800 | (ctrl->bRequestType & USB_TYPE_VENDOR) && |
37a3a533 AP |
1801 | ctrl->bRequest == cdev->b_vendor_code) { |
1802 | struct usb_request *req; | |
1803 | struct usb_configuration *os_desc_cfg; | |
1804 | u8 *buf; | |
1805 | int interface; | |
1806 | int count = 0; | |
1807 | ||
1808 | req = cdev->os_desc_req; | |
57943716 | 1809 | req->context = cdev; |
37a3a533 AP |
1810 | req->complete = composite_setup_complete; |
1811 | buf = req->buf; | |
1812 | os_desc_cfg = cdev->os_desc_config; | |
1813 | memset(buf, 0, w_length); | |
1814 | buf[5] = 0x01; | |
1815 | switch (ctrl->bRequestType & USB_RECIP_MASK) { | |
1816 | case USB_RECIP_DEVICE: | |
1817 | if (w_index != 0x4 || (w_value >> 8)) | |
1818 | break; | |
1819 | buf[6] = w_index; | |
1820 | if (w_length == 0x10) { | |
1821 | /* Number of ext compat interfaces */ | |
1822 | count = count_ext_compat(os_desc_cfg); | |
1823 | buf[8] = count; | |
1824 | count *= 24; /* 24 B/ext compat desc */ | |
1825 | count += 16; /* header */ | |
1826 | put_unaligned_le32(count, buf); | |
1827 | value = w_length; | |
1828 | } else { | |
1829 | /* "extended compatibility ID"s */ | |
1830 | count = count_ext_compat(os_desc_cfg); | |
1831 | buf[8] = count; | |
1832 | count *= 24; /* 24 B/ext compat desc */ | |
1833 | count += 16; /* header */ | |
1834 | put_unaligned_le32(count, buf); | |
1835 | buf += 16; | |
1836 | fill_ext_compat(os_desc_cfg, buf); | |
1837 | value = w_length; | |
1838 | } | |
1839 | break; | |
1840 | case USB_RECIP_INTERFACE: | |
1841 | if (w_index != 0x5 || (w_value >> 8)) | |
1842 | break; | |
1843 | interface = w_value & 0xFF; | |
1844 | buf[6] = w_index; | |
1845 | if (w_length == 0x0A) { | |
1846 | count = count_ext_prop(os_desc_cfg, | |
1847 | interface); | |
1848 | put_unaligned_le16(count, buf + 8); | |
1849 | count = len_ext_prop(os_desc_cfg, | |
1850 | interface); | |
1851 | put_unaligned_le32(count, buf); | |
1852 | ||
1853 | value = w_length; | |
1854 | } else { | |
1855 | count = count_ext_prop(os_desc_cfg, | |
1856 | interface); | |
1857 | put_unaligned_le16(count, buf + 8); | |
1858 | count = len_ext_prop(os_desc_cfg, | |
1859 | interface); | |
1860 | put_unaligned_le32(count, buf); | |
1861 | buf += 10; | |
1862 | value = fill_ext_prop(os_desc_cfg, | |
1863 | interface, buf); | |
1864 | if (value < 0) | |
1865 | return value; | |
1866 | ||
1867 | value = w_length; | |
1868 | } | |
1869 | break; | |
1870 | } | |
7e14f47a WW |
1871 | |
1872 | if (value >= 0) { | |
1873 | req->length = value; | |
1874 | req->context = cdev; | |
1875 | req->zero = value < w_length; | |
1876 | value = composite_ep0_queue(cdev, req, | |
1877 | GFP_ATOMIC); | |
1878 | if (value < 0) { | |
1879 | DBG(cdev, "ep_queue --> %d\n", value); | |
1880 | req->status = 0; | |
1881 | composite_setup_complete(gadget->ep0, | |
1882 | req); | |
1883 | } | |
37a3a533 AP |
1884 | } |
1885 | return value; | |
1886 | } | |
1887 | ||
40982be5 DB |
1888 | VDBG(cdev, |
1889 | "non-core control req%02x.%02x v%04x i%04x l%d\n", | |
1890 | ctrl->bRequestType, ctrl->bRequest, | |
1891 | w_value, w_index, w_length); | |
1892 | ||
5242658d LP |
1893 | /* functions always handle their interfaces and endpoints... |
1894 | * punt other recipients (other, WUSB, ...) to the current | |
40982be5 DB |
1895 | * configuration code. |
1896 | * | |
1897 | * REVISIT it could make sense to let the composite device | |
1898 | * take such requests too, if that's ever needed: to work | |
1899 | * in config 0, etc. | |
1900 | */ | |
b4c21f0b KVA |
1901 | if (cdev->config) { |
1902 | list_for_each_entry(f, &cdev->config->functions, list) | |
1903 | if (f->req_match && f->req_match(f, ctrl)) | |
1904 | goto try_fun_setup; | |
1905 | f = NULL; | |
1906 | } | |
1907 | ||
5242658d LP |
1908 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
1909 | case USB_RECIP_INTERFACE: | |
ff085de7 | 1910 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
3c47eb06 MM |
1911 | break; |
1912 | f = cdev->config->interface[intf]; | |
5242658d LP |
1913 | break; |
1914 | ||
1915 | case USB_RECIP_ENDPOINT: | |
1916 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); | |
1917 | list_for_each_entry(f, &cdev->config->functions, list) { | |
1918 | if (test_bit(endp, f->endpoints)) | |
1919 | break; | |
1920 | } | |
1921 | if (&f->list == &cdev->config->functions) | |
40982be5 | 1922 | f = NULL; |
5242658d | 1923 | break; |
40982be5 | 1924 | } |
f563d230 | 1925 | try_fun_setup: |
5242658d LP |
1926 | if (f && f->setup) |
1927 | value = f->setup(f, ctrl); | |
1928 | else { | |
40982be5 DB |
1929 | struct usb_configuration *c; |
1930 | ||
1931 | c = cdev->config; | |
a01091e5 AP |
1932 | if (!c) |
1933 | goto done; | |
1934 | ||
1935 | /* try current config's setup */ | |
1936 | if (c->setup) { | |
40982be5 | 1937 | value = c->setup(c, ctrl); |
a01091e5 AP |
1938 | goto done; |
1939 | } | |
1940 | ||
1941 | /* try the only function in the current config */ | |
1942 | if (!list_is_singular(&c->functions)) | |
1943 | goto done; | |
1944 | f = list_first_entry(&c->functions, struct usb_function, | |
1945 | list); | |
1946 | if (f->setup) | |
1947 | value = f->setup(f, ctrl); | |
40982be5 DB |
1948 | } |
1949 | ||
1950 | goto done; | |
1951 | } | |
1952 | ||
1953 | /* respond with data transfer before status phase? */ | |
1b9ba000 | 1954 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
40982be5 | 1955 | req->length = value; |
57943716 | 1956 | req->context = cdev; |
40982be5 | 1957 | req->zero = value < w_length; |
a7c12eaf | 1958 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
40982be5 DB |
1959 | if (value < 0) { |
1960 | DBG(cdev, "ep_queue --> %d\n", value); | |
1961 | req->status = 0; | |
1962 | composite_setup_complete(gadget->ep0, req); | |
1963 | } | |
1b9ba000 RQ |
1964 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
1965 | WARN(cdev, | |
1966 | "%s: Delayed status not supported for w_length != 0", | |
1967 | __func__); | |
40982be5 DB |
1968 | } |
1969 | ||
1970 | done: | |
1971 | /* device either stalls (value < 0) or reports success */ | |
1972 | return value; | |
1973 | } | |
1974 | ||
2d5a8899 | 1975 | void composite_disconnect(struct usb_gadget *gadget) |
40982be5 DB |
1976 | { |
1977 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1978 | unsigned long flags; | |
1979 | ||
1980 | /* REVISIT: should we have config and device level | |
1981 | * disconnect callbacks? | |
1982 | */ | |
1983 | spin_lock_irqsave(&cdev->lock, flags); | |
1984 | if (cdev->config) | |
1985 | reset_config(cdev); | |
ffe0b335 SAS |
1986 | if (cdev->driver->disconnect) |
1987 | cdev->driver->disconnect(cdev); | |
40982be5 DB |
1988 | spin_unlock_irqrestore(&cdev->lock, flags); |
1989 | } | |
1990 | ||
1991 | /*-------------------------------------------------------------------------*/ | |
1992 | ||
ce26bd23 GKH |
1993 | static ssize_t suspended_show(struct device *dev, struct device_attribute *attr, |
1994 | char *buf) | |
f48cf80f FC |
1995 | { |
1996 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); | |
1997 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1998 | ||
1999 | return sprintf(buf, "%d\n", cdev->suspended); | |
2000 | } | |
ce26bd23 | 2001 | static DEVICE_ATTR_RO(suspended); |
f48cf80f | 2002 | |
779d516c | 2003 | static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver) |
40982be5 DB |
2004 | { |
2005 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
2006 | ||
2007 | /* composite_disconnect() must already have been called | |
2008 | * by the underlying peripheral controller driver! | |
2009 | * so there's no i/o concurrency that could affect the | |
2010 | * state protected by cdev->lock. | |
2011 | */ | |
2012 | WARN_ON(cdev->config); | |
2013 | ||
2014 | while (!list_empty(&cdev->configs)) { | |
2015 | struct usb_configuration *c; | |
40982be5 DB |
2016 | c = list_first_entry(&cdev->configs, |
2017 | struct usb_configuration, list); | |
51cce6fc | 2018 | remove_config(cdev, c); |
40982be5 | 2019 | } |
779d516c | 2020 | if (cdev->driver->unbind && unbind_driver) |
ffe0b335 | 2021 | cdev->driver->unbind(cdev); |
40982be5 | 2022 | |
a5923340 SAS |
2023 | composite_dev_cleanup(cdev); |
2024 | ||
cc2683c3 | 2025 | kfree(cdev->def_manufacturer); |
40982be5 DB |
2026 | kfree(cdev); |
2027 | set_gadget_data(gadget, NULL); | |
40982be5 DB |
2028 | } |
2029 | ||
779d516c SAS |
2030 | static void composite_unbind(struct usb_gadget *gadget) |
2031 | { | |
2032 | __composite_unbind(gadget, true); | |
2033 | } | |
2034 | ||
7d16e8d3 SAS |
2035 | static void update_unchanged_dev_desc(struct usb_device_descriptor *new, |
2036 | const struct usb_device_descriptor *old) | |
2037 | { | |
2038 | __le16 idVendor; | |
2039 | __le16 idProduct; | |
2040 | __le16 bcdDevice; | |
1cf0d264 | 2041 | u8 iSerialNumber; |
03de9bf6 | 2042 | u8 iManufacturer; |
2d35ee47 | 2043 | u8 iProduct; |
7d16e8d3 SAS |
2044 | |
2045 | /* | |
2046 | * these variables may have been set in | |
2047 | * usb_composite_overwrite_options() | |
2048 | */ | |
2049 | idVendor = new->idVendor; | |
2050 | idProduct = new->idProduct; | |
2051 | bcdDevice = new->bcdDevice; | |
1cf0d264 | 2052 | iSerialNumber = new->iSerialNumber; |
03de9bf6 | 2053 | iManufacturer = new->iManufacturer; |
2d35ee47 | 2054 | iProduct = new->iProduct; |
7d16e8d3 SAS |
2055 | |
2056 | *new = *old; | |
2057 | if (idVendor) | |
2058 | new->idVendor = idVendor; | |
2059 | if (idProduct) | |
2060 | new->idProduct = idProduct; | |
2061 | if (bcdDevice) | |
2062 | new->bcdDevice = bcdDevice; | |
ed9cbda6 SAS |
2063 | else |
2064 | new->bcdDevice = cpu_to_le16(get_default_bcdDevice()); | |
1cf0d264 SAS |
2065 | if (iSerialNumber) |
2066 | new->iSerialNumber = iSerialNumber; | |
03de9bf6 SAS |
2067 | if (iManufacturer) |
2068 | new->iManufacturer = iManufacturer; | |
2d35ee47 SAS |
2069 | if (iProduct) |
2070 | new->iProduct = iProduct; | |
7d16e8d3 SAS |
2071 | } |
2072 | ||
a5923340 SAS |
2073 | int composite_dev_prepare(struct usb_composite_driver *composite, |
2074 | struct usb_composite_dev *cdev) | |
ffe0b335 | 2075 | { |
a5923340 SAS |
2076 | struct usb_gadget *gadget = cdev->gadget; |
2077 | int ret = -ENOMEM; | |
40982be5 DB |
2078 | |
2079 | /* preallocate control response and buffer */ | |
2080 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); | |
2081 | if (!cdev->req) | |
a5923340 SAS |
2082 | return -ENOMEM; |
2083 | ||
e13f17ff | 2084 | cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL); |
40982be5 DB |
2085 | if (!cdev->req->buf) |
2086 | goto fail; | |
a5923340 SAS |
2087 | |
2088 | ret = device_create_file(&gadget->dev, &dev_attr_suspended); | |
2089 | if (ret) | |
2090 | goto fail_dev; | |
2091 | ||
40982be5 | 2092 | cdev->req->complete = composite_setup_complete; |
57943716 | 2093 | cdev->req->context = cdev; |
40982be5 DB |
2094 | gadget->ep0->driver_data = cdev; |
2095 | ||
40982be5 DB |
2096 | cdev->driver = composite; |
2097 | ||
37b5801e PM |
2098 | /* |
2099 | * As per USB compliance update, a device that is actively drawing | |
2100 | * more than 100mA from USB must report itself as bus-powered in | |
2101 | * the GetStatus(DEVICE) call. | |
2102 | */ | |
2103 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) | |
2104 | usb_gadget_set_selfpowered(gadget); | |
40982be5 DB |
2105 | |
2106 | /* interface and string IDs start at zero via kzalloc. | |
2107 | * we force endpoints to start unassigned; few controller | |
2108 | * drivers will zero ep->driver_data. | |
2109 | */ | |
a5923340 SAS |
2110 | usb_ep_autoconfig_reset(gadget); |
2111 | return 0; | |
2112 | fail_dev: | |
2113 | kfree(cdev->req->buf); | |
2114 | fail: | |
2115 | usb_ep_free_request(gadget->ep0, cdev->req); | |
2116 | cdev->req = NULL; | |
2117 | return ret; | |
2118 | } | |
2119 | ||
37a3a533 AP |
2120 | int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, |
2121 | struct usb_ep *ep0) | |
2122 | { | |
2123 | int ret = 0; | |
2124 | ||
2125 | cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL); | |
2126 | if (!cdev->os_desc_req) { | |
2127 | ret = PTR_ERR(cdev->os_desc_req); | |
2128 | goto end; | |
2129 | } | |
2130 | ||
2131 | /* OS feature descriptor length <= 4kB */ | |
2132 | cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL); | |
2133 | if (!cdev->os_desc_req->buf) { | |
2134 | ret = PTR_ERR(cdev->os_desc_req->buf); | |
2135 | kfree(cdev->os_desc_req); | |
2136 | goto end; | |
2137 | } | |
57943716 | 2138 | cdev->os_desc_req->context = cdev; |
37a3a533 AP |
2139 | cdev->os_desc_req->complete = composite_setup_complete; |
2140 | end: | |
2141 | return ret; | |
2142 | } | |
2143 | ||
a5923340 SAS |
2144 | void composite_dev_cleanup(struct usb_composite_dev *cdev) |
2145 | { | |
27a46633 SAS |
2146 | struct usb_gadget_string_container *uc, *tmp; |
2147 | ||
2148 | list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) { | |
2149 | list_del(&uc->list); | |
2150 | kfree(uc); | |
2151 | } | |
37a3a533 | 2152 | if (cdev->os_desc_req) { |
a7c12eaf FB |
2153 | if (cdev->os_desc_pending) |
2154 | usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req); | |
2155 | ||
37a3a533 AP |
2156 | kfree(cdev->os_desc_req->buf); |
2157 | usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req); | |
2158 | } | |
a5923340 | 2159 | if (cdev->req) { |
a7c12eaf FB |
2160 | if (cdev->setup_pending) |
2161 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); | |
2162 | ||
be0a8887 | 2163 | kfree(cdev->req->buf); |
a5923340 SAS |
2164 | usb_ep_free_request(cdev->gadget->ep0, cdev->req); |
2165 | } | |
88af8bbe | 2166 | cdev->next_string_id = 0; |
a5923340 SAS |
2167 | device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); |
2168 | } | |
2169 | ||
2170 | static int composite_bind(struct usb_gadget *gadget, | |
2171 | struct usb_gadget_driver *gdriver) | |
2172 | { | |
2173 | struct usb_composite_dev *cdev; | |
2174 | struct usb_composite_driver *composite = to_cdriver(gdriver); | |
2175 | int status = -ENOMEM; | |
2176 | ||
2177 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); | |
2178 | if (!cdev) | |
2179 | return status; | |
2180 | ||
2181 | spin_lock_init(&cdev->lock); | |
2182 | cdev->gadget = gadget; | |
2183 | set_gadget_data(gadget, cdev); | |
2184 | INIT_LIST_HEAD(&cdev->configs); | |
9bb2859f | 2185 | INIT_LIST_HEAD(&cdev->gstrings); |
a5923340 SAS |
2186 | |
2187 | status = composite_dev_prepare(composite, cdev); | |
2188 | if (status) | |
2189 | goto fail; | |
40982be5 DB |
2190 | |
2191 | /* composite gadget needs to assign strings for whole device (like | |
2192 | * serial number), register function drivers, potentially update | |
2193 | * power state and consumption, etc | |
2194 | */ | |
fac3a43e | 2195 | status = composite->bind(cdev); |
40982be5 DB |
2196 | if (status < 0) |
2197 | goto fail; | |
2198 | ||
37a3a533 AP |
2199 | if (cdev->use_os_string) { |
2200 | status = composite_os_desc_req_prepare(cdev, gadget->ep0); | |
2201 | if (status) | |
2202 | goto fail; | |
2203 | } | |
2204 | ||
7d16e8d3 | 2205 | update_unchanged_dev_desc(&cdev->desc, composite->dev); |
dbb442b8 | 2206 | |
ad1a8102 MN |
2207 | /* has userspace failed to provide a serial number? */ |
2208 | if (composite->needs_serial && !cdev->desc.iSerialNumber) | |
2209 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); | |
40982be5 DB |
2210 | |
2211 | INFO(cdev, "%s ready\n", composite->name); | |
2212 | return 0; | |
2213 | ||
2214 | fail: | |
779d516c | 2215 | __composite_unbind(gadget, false); |
40982be5 DB |
2216 | return status; |
2217 | } | |
2218 | ||
2219 | /*-------------------------------------------------------------------------*/ | |
2220 | ||
3a571870 | 2221 | void composite_suspend(struct usb_gadget *gadget) |
40982be5 DB |
2222 | { |
2223 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
2224 | struct usb_function *f; | |
2225 | ||
8942939a | 2226 | /* REVISIT: should we have config level |
40982be5 DB |
2227 | * suspend/resume callbacks? |
2228 | */ | |
2229 | DBG(cdev, "suspend\n"); | |
2230 | if (cdev->config) { | |
2231 | list_for_each_entry(f, &cdev->config->functions, list) { | |
2232 | if (f->suspend) | |
2233 | f->suspend(f); | |
2234 | } | |
2235 | } | |
ffe0b335 SAS |
2236 | if (cdev->driver->suspend) |
2237 | cdev->driver->suspend(cdev); | |
f48cf80f FC |
2238 | |
2239 | cdev->suspended = 1; | |
b23f2f94 HW |
2240 | |
2241 | usb_gadget_vbus_draw(gadget, 2); | |
40982be5 DB |
2242 | } |
2243 | ||
3a571870 | 2244 | void composite_resume(struct usb_gadget *gadget) |
40982be5 DB |
2245 | { |
2246 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
2247 | struct usb_function *f; | |
56b1b909 | 2248 | u16 maxpower; |
40982be5 | 2249 | |
8942939a | 2250 | /* REVISIT: should we have config level |
40982be5 DB |
2251 | * suspend/resume callbacks? |
2252 | */ | |
2253 | DBG(cdev, "resume\n"); | |
ffe0b335 SAS |
2254 | if (cdev->driver->resume) |
2255 | cdev->driver->resume(cdev); | |
40982be5 DB |
2256 | if (cdev->config) { |
2257 | list_for_each_entry(f, &cdev->config->functions, list) { | |
2258 | if (f->resume) | |
2259 | f->resume(f); | |
2260 | } | |
b23f2f94 | 2261 | |
8f900a9a | 2262 | maxpower = cdev->config->MaxPower; |
b23f2f94 HW |
2263 | |
2264 | usb_gadget_vbus_draw(gadget, maxpower ? | |
8f900a9a | 2265 | maxpower : CONFIG_USB_GADGET_VBUS_DRAW); |
40982be5 | 2266 | } |
f48cf80f FC |
2267 | |
2268 | cdev->suspended = 0; | |
40982be5 DB |
2269 | } |
2270 | ||
2271 | /*-------------------------------------------------------------------------*/ | |
2272 | ||
ffe0b335 | 2273 | static const struct usb_gadget_driver composite_driver_template = { |
93952956 | 2274 | .bind = composite_bind, |
915c8bef | 2275 | .unbind = composite_unbind, |
40982be5 DB |
2276 | |
2277 | .setup = composite_setup, | |
d8a816fc | 2278 | .reset = composite_disconnect, |
40982be5 DB |
2279 | .disconnect = composite_disconnect, |
2280 | ||
2281 | .suspend = composite_suspend, | |
2282 | .resume = composite_resume, | |
2283 | ||
2284 | .driver = { | |
2285 | .owner = THIS_MODULE, | |
2286 | }, | |
2287 | }; | |
2288 | ||
2289 | /** | |
07a18bd7 | 2290 | * usb_composite_probe() - register a composite driver |
40982be5 | 2291 | * @driver: the driver to register |
43febb27 | 2292 | * |
40982be5 DB |
2293 | * Context: single threaded during gadget setup |
2294 | * | |
2295 | * This function is used to register drivers using the composite driver | |
2296 | * framework. The return value is zero, or a negative errno value. | |
2297 | * Those values normally come from the driver's @bind method, which does | |
2298 | * all the work of setting up the driver to match the hardware. | |
2299 | * | |
2300 | * On successful return, the gadget is ready to respond to requests from | |
2301 | * the host, unless one of its components invokes usb_gadget_disconnect() | |
2302 | * while it was binding. That would usually be done in order to wait for | |
2303 | * some userspace participation. | |
2304 | */ | |
03e42bd5 | 2305 | int usb_composite_probe(struct usb_composite_driver *driver) |
40982be5 | 2306 | { |
ffe0b335 SAS |
2307 | struct usb_gadget_driver *gadget_driver; |
2308 | ||
2309 | if (!driver || !driver->dev || !driver->bind) | |
40982be5 DB |
2310 | return -EINVAL; |
2311 | ||
2312 | if (!driver->name) | |
2313 | driver->name = "composite"; | |
40982be5 | 2314 | |
ffe0b335 SAS |
2315 | driver->gadget_driver = composite_driver_template; |
2316 | gadget_driver = &driver->gadget_driver; | |
2317 | ||
2318 | gadget_driver->function = (char *) driver->name; | |
2319 | gadget_driver->driver.name = driver->name; | |
2320 | gadget_driver->max_speed = driver->max_speed; | |
2321 | ||
2322 | return usb_gadget_probe_driver(gadget_driver); | |
40982be5 | 2323 | } |
721e2e91 | 2324 | EXPORT_SYMBOL_GPL(usb_composite_probe); |
40982be5 DB |
2325 | |
2326 | /** | |
2327 | * usb_composite_unregister() - unregister a composite driver | |
2328 | * @driver: the driver to unregister | |
2329 | * | |
2330 | * This function is used to unregister drivers using the composite | |
2331 | * driver framework. | |
2332 | */ | |
28824b18 | 2333 | void usb_composite_unregister(struct usb_composite_driver *driver) |
40982be5 | 2334 | { |
ffe0b335 | 2335 | usb_gadget_unregister_driver(&driver->gadget_driver); |
40982be5 | 2336 | } |
721e2e91 | 2337 | EXPORT_SYMBOL_GPL(usb_composite_unregister); |
1b9ba000 RQ |
2338 | |
2339 | /** | |
2340 | * usb_composite_setup_continue() - Continue with the control transfer | |
2341 | * @cdev: the composite device who's control transfer was kept waiting | |
2342 | * | |
2343 | * This function must be called by the USB function driver to continue | |
2344 | * with the control transfer's data/status stage in case it had requested to | |
2345 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) | |
2346 | * can request the composite framework to delay the setup request's data/status | |
2347 | * stages by returning USB_GADGET_DELAYED_STATUS. | |
2348 | */ | |
2349 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) | |
2350 | { | |
2351 | int value; | |
2352 | struct usb_request *req = cdev->req; | |
2353 | unsigned long flags; | |
2354 | ||
2355 | DBG(cdev, "%s\n", __func__); | |
2356 | spin_lock_irqsave(&cdev->lock, flags); | |
2357 | ||
2358 | if (cdev->delayed_status == 0) { | |
2359 | WARN(cdev, "%s: Unexpected call\n", __func__); | |
2360 | ||
2361 | } else if (--cdev->delayed_status == 0) { | |
2362 | DBG(cdev, "%s: Completing delayed status\n", __func__); | |
2363 | req->length = 0; | |
57943716 | 2364 | req->context = cdev; |
a7c12eaf | 2365 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
1b9ba000 RQ |
2366 | if (value < 0) { |
2367 | DBG(cdev, "ep_queue --> %d\n", value); | |
2368 | req->status = 0; | |
2369 | composite_setup_complete(cdev->gadget->ep0, req); | |
2370 | } | |
2371 | } | |
2372 | ||
2373 | spin_unlock_irqrestore(&cdev->lock, flags); | |
2374 | } | |
721e2e91 | 2375 | EXPORT_SYMBOL_GPL(usb_composite_setup_continue); |
1b9ba000 | 2376 | |
cc2683c3 SAS |
2377 | static char *composite_default_mfr(struct usb_gadget *gadget) |
2378 | { | |
2379 | char *mfr; | |
2380 | int len; | |
2381 | ||
2382 | len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname, | |
2383 | init_utsname()->release, gadget->name); | |
2384 | len++; | |
2385 | mfr = kmalloc(len, GFP_KERNEL); | |
2386 | if (!mfr) | |
2387 | return NULL; | |
2388 | snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname, | |
2389 | init_utsname()->release, gadget->name); | |
2390 | return mfr; | |
2391 | } | |
2392 | ||
7d16e8d3 SAS |
2393 | void usb_composite_overwrite_options(struct usb_composite_dev *cdev, |
2394 | struct usb_composite_overwrite *covr) | |
2395 | { | |
2396 | struct usb_device_descriptor *desc = &cdev->desc; | |
1cf0d264 SAS |
2397 | struct usb_gadget_strings *gstr = cdev->driver->strings[0]; |
2398 | struct usb_string *dev_str = gstr->strings; | |
7d16e8d3 SAS |
2399 | |
2400 | if (covr->idVendor) | |
2401 | desc->idVendor = cpu_to_le16(covr->idVendor); | |
2402 | ||
2403 | if (covr->idProduct) | |
2404 | desc->idProduct = cpu_to_le16(covr->idProduct); | |
2405 | ||
2406 | if (covr->bcdDevice) | |
2407 | desc->bcdDevice = cpu_to_le16(covr->bcdDevice); | |
1cf0d264 SAS |
2408 | |
2409 | if (covr->serial_number) { | |
2410 | desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id; | |
2411 | dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number; | |
2412 | } | |
03de9bf6 SAS |
2413 | if (covr->manufacturer) { |
2414 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; | |
2415 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer; | |
cc2683c3 SAS |
2416 | |
2417 | } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) { | |
2418 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; | |
2419 | cdev->def_manufacturer = composite_default_mfr(cdev->gadget); | |
2420 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer; | |
03de9bf6 | 2421 | } |
2d35ee47 SAS |
2422 | |
2423 | if (covr->product) { | |
2424 | desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id; | |
2425 | dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product; | |
2426 | } | |
7d16e8d3 | 2427 | } |
721e2e91 | 2428 | EXPORT_SYMBOL_GPL(usb_composite_overwrite_options); |
d80c304b SAS |
2429 | |
2430 | MODULE_LICENSE("GPL"); | |
2431 | MODULE_AUTHOR("David Brownell"); |