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. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | ||
21 | /* #define VERBOSE_DEBUG */ | |
22 | ||
23 | #include <linux/kallsyms.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/device.h> | |
ad1a8102 | 27 | #include <linux/utsname.h> |
40982be5 DB |
28 | |
29 | #include <linux/usb/composite.h> | |
30 | ||
31 | ||
32 | /* | |
33 | * The code in this file is utility code, used to build a gadget driver | |
34 | * from one or more "function" drivers, one or more "configuration" | |
35 | * objects, and a "usb_composite_driver" by gluing them together along | |
36 | * with the relevant device-wide data. | |
37 | */ | |
38 | ||
39 | /* big enough to hold our biggest descriptor */ | |
dd0543ec | 40 | #define USB_BUFSIZ 1024 |
40982be5 DB |
41 | |
42 | static struct usb_composite_driver *composite; | |
07a18bd7 | 43 | static int (*composite_gadget_bind)(struct usb_composite_dev *cdev); |
40982be5 | 44 | |
25985edc | 45 | /* Some systems will need runtime overrides for the product identifiers |
40982be5 DB |
46 | * published in the device descriptor, either numbers or strings or both. |
47 | * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
48 | */ | |
49 | ||
50 | static ushort idVendor; | |
51 | module_param(idVendor, ushort, 0); | |
52 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | |
53 | ||
54 | static ushort idProduct; | |
55 | module_param(idProduct, ushort, 0); | |
56 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | |
57 | ||
58 | static ushort bcdDevice; | |
59 | module_param(bcdDevice, ushort, 0); | |
60 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | |
61 | ||
62 | static char *iManufacturer; | |
63 | module_param(iManufacturer, charp, 0); | |
64 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | |
65 | ||
66 | static char *iProduct; | |
67 | module_param(iProduct, charp, 0); | |
68 | MODULE_PARM_DESC(iProduct, "USB Product string"); | |
69 | ||
70 | static char *iSerialNumber; | |
71 | module_param(iSerialNumber, charp, 0); | |
72 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); | |
73 | ||
ad1a8102 MN |
74 | static char composite_manufacturer[50]; |
75 | ||
40982be5 | 76 | /*-------------------------------------------------------------------------*/ |
48767a4e TB |
77 | /** |
78 | * next_ep_desc() - advance to the next EP descriptor | |
79 | * @t: currect pointer within descriptor array | |
80 | * | |
81 | * Return: next EP descriptor or NULL | |
82 | * | |
83 | * Iterate over @t until either EP descriptor found or | |
84 | * NULL (that indicates end of list) encountered | |
85 | */ | |
86 | static struct usb_descriptor_header** | |
87 | next_ep_desc(struct usb_descriptor_header **t) | |
88 | { | |
89 | for (; *t; t++) { | |
90 | if ((*t)->bDescriptorType == USB_DT_ENDPOINT) | |
91 | return t; | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | /* | |
97 | * for_each_ep_desc()- iterate over endpoint descriptors in the | |
98 | * descriptors list | |
99 | * @start: pointer within descriptor array. | |
100 | * @ep_desc: endpoint descriptor to use as the loop cursor | |
101 | */ | |
102 | #define for_each_ep_desc(start, ep_desc) \ | |
103 | for (ep_desc = next_ep_desc(start); \ | |
104 | ep_desc; ep_desc = next_ep_desc(ep_desc+1)) | |
105 | ||
106 | /** | |
107 | * config_ep_by_speed() - configures the given endpoint | |
108 | * according to gadget speed. | |
109 | * @g: pointer to the gadget | |
110 | * @f: usb function | |
111 | * @_ep: the endpoint to configure | |
112 | * | |
113 | * Return: error code, 0 on success | |
114 | * | |
115 | * This function chooses the right descriptors for a given | |
116 | * endpoint according to gadget speed and saves it in the | |
117 | * endpoint desc field. If the endpoint already has a descriptor | |
118 | * assigned to it - overwrites it with currently corresponding | |
119 | * descriptor. The endpoint maxpacket field is updated according | |
120 | * to the chosen descriptor. | |
121 | * Note: the supplied function should hold all the descriptors | |
122 | * for supported speeds | |
123 | */ | |
124 | int config_ep_by_speed(struct usb_gadget *g, | |
125 | struct usb_function *f, | |
126 | struct usb_ep *_ep) | |
127 | { | |
128 | struct usb_endpoint_descriptor *chosen_desc = NULL; | |
129 | struct usb_descriptor_header **speed_desc = NULL; | |
130 | ||
131 | struct usb_descriptor_header **d_spd; /* cursor for speed desc */ | |
132 | ||
133 | if (!g || !f || !_ep) | |
134 | return -EIO; | |
135 | ||
136 | /* select desired speed */ | |
137 | switch (g->speed) { | |
138 | case USB_SPEED_HIGH: | |
139 | if (gadget_is_dualspeed(g)) { | |
140 | speed_desc = f->hs_descriptors; | |
141 | break; | |
142 | } | |
143 | /* else: fall through */ | |
144 | default: | |
145 | speed_desc = f->descriptors; | |
146 | } | |
147 | /* find descriptors */ | |
148 | for_each_ep_desc(speed_desc, d_spd) { | |
149 | chosen_desc = (struct usb_endpoint_descriptor *)*d_spd; | |
150 | if (chosen_desc->bEndpointAddress == _ep->address) | |
151 | goto ep_found; | |
152 | } | |
153 | return -EIO; | |
154 | ||
155 | ep_found: | |
156 | /* commit results */ | |
157 | _ep->maxpacket = le16_to_cpu(chosen_desc->wMaxPacketSize); | |
158 | _ep->desc = chosen_desc; | |
159 | ||
160 | return 0; | |
161 | } | |
40982be5 DB |
162 | |
163 | /** | |
164 | * usb_add_function() - add a function to a configuration | |
165 | * @config: the configuration | |
166 | * @function: the function being added | |
167 | * Context: single threaded during gadget setup | |
168 | * | |
169 | * After initialization, each configuration must have one or more | |
170 | * functions added to it. Adding a function involves calling its @bind() | |
171 | * method to allocate resources such as interface and string identifiers | |
172 | * and endpoints. | |
173 | * | |
174 | * This function returns the value of the function's bind(), which is | |
175 | * zero for success else a negative errno value. | |
176 | */ | |
28824b18 | 177 | int usb_add_function(struct usb_configuration *config, |
40982be5 DB |
178 | struct usb_function *function) |
179 | { | |
180 | int value = -EINVAL; | |
181 | ||
182 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", | |
183 | function->name, function, | |
184 | config->label, config); | |
185 | ||
186 | if (!function->set_alt || !function->disable) | |
187 | goto done; | |
188 | ||
189 | function->config = config; | |
190 | list_add_tail(&function->list, &config->functions); | |
191 | ||
192 | /* REVISIT *require* function->bind? */ | |
193 | if (function->bind) { | |
194 | value = function->bind(config, function); | |
195 | if (value < 0) { | |
196 | list_del(&function->list); | |
197 | function->config = NULL; | |
198 | } | |
199 | } else | |
200 | value = 0; | |
201 | ||
202 | /* We allow configurations that don't work at both speeds. | |
203 | * If we run into a lowspeed Linux system, treat it the same | |
204 | * as full speed ... it's the function drivers that will need | |
205 | * to avoid bulk and ISO transfers. | |
206 | */ | |
207 | if (!config->fullspeed && function->descriptors) | |
208 | config->fullspeed = true; | |
209 | if (!config->highspeed && function->hs_descriptors) | |
210 | config->highspeed = true; | |
211 | ||
212 | done: | |
213 | if (value) | |
214 | DBG(config->cdev, "adding '%s'/%p --> %d\n", | |
215 | function->name, function, value); | |
216 | return value; | |
217 | } | |
218 | ||
60beed95 DB |
219 | /** |
220 | * usb_function_deactivate - prevent function and gadget enumeration | |
221 | * @function: the function that isn't yet ready to respond | |
222 | * | |
223 | * Blocks response of the gadget driver to host enumeration by | |
224 | * preventing the data line pullup from being activated. This is | |
225 | * normally called during @bind() processing to change from the | |
226 | * initial "ready to respond" state, or when a required resource | |
227 | * becomes available. | |
228 | * | |
229 | * For example, drivers that serve as a passthrough to a userspace | |
230 | * daemon can block enumeration unless that daemon (such as an OBEX, | |
231 | * MTP, or print server) is ready to handle host requests. | |
232 | * | |
233 | * Not all systems support software control of their USB peripheral | |
234 | * data pullups. | |
235 | * | |
236 | * Returns zero on success, else negative errno. | |
237 | */ | |
238 | int usb_function_deactivate(struct usb_function *function) | |
239 | { | |
240 | struct usb_composite_dev *cdev = function->config->cdev; | |
b2bdf3a7 | 241 | unsigned long flags; |
60beed95 DB |
242 | int status = 0; |
243 | ||
b2bdf3a7 | 244 | spin_lock_irqsave(&cdev->lock, flags); |
60beed95 DB |
245 | |
246 | if (cdev->deactivations == 0) | |
247 | status = usb_gadget_disconnect(cdev->gadget); | |
248 | if (status == 0) | |
249 | cdev->deactivations++; | |
250 | ||
b2bdf3a7 | 251 | spin_unlock_irqrestore(&cdev->lock, flags); |
60beed95 DB |
252 | return status; |
253 | } | |
254 | ||
255 | /** | |
256 | * usb_function_activate - allow function and gadget enumeration | |
257 | * @function: function on which usb_function_activate() was called | |
258 | * | |
259 | * Reverses effect of usb_function_deactivate(). If no more functions | |
260 | * are delaying their activation, the gadget driver will respond to | |
261 | * host enumeration procedures. | |
262 | * | |
263 | * Returns zero on success, else negative errno. | |
264 | */ | |
265 | int usb_function_activate(struct usb_function *function) | |
266 | { | |
267 | struct usb_composite_dev *cdev = function->config->cdev; | |
268 | int status = 0; | |
269 | ||
270 | spin_lock(&cdev->lock); | |
271 | ||
272 | if (WARN_ON(cdev->deactivations == 0)) | |
273 | status = -EINVAL; | |
274 | else { | |
275 | cdev->deactivations--; | |
276 | if (cdev->deactivations == 0) | |
277 | status = usb_gadget_connect(cdev->gadget); | |
278 | } | |
279 | ||
280 | spin_unlock(&cdev->lock); | |
281 | return status; | |
282 | } | |
283 | ||
40982be5 DB |
284 | /** |
285 | * usb_interface_id() - allocate an unused interface ID | |
286 | * @config: configuration associated with the interface | |
287 | * @function: function handling the interface | |
288 | * Context: single threaded during gadget setup | |
289 | * | |
290 | * usb_interface_id() is called from usb_function.bind() callbacks to | |
291 | * allocate new interface IDs. The function driver will then store that | |
292 | * ID in interface, association, CDC union, and other descriptors. It | |
25985edc | 293 | * will also handle any control requests targeted at that interface, |
40982be5 DB |
294 | * particularly changing its altsetting via set_alt(). There may |
295 | * also be class-specific or vendor-specific requests to handle. | |
296 | * | |
297 | * All interface identifier should be allocated using this routine, to | |
298 | * ensure that for example different functions don't wrongly assign | |
299 | * different meanings to the same identifier. Note that since interface | |
25985edc | 300 | * identifiers are configuration-specific, functions used in more than |
40982be5 DB |
301 | * one configuration (or more than once in a given configuration) need |
302 | * multiple versions of the relevant descriptors. | |
303 | * | |
304 | * Returns the interface ID which was allocated; or -ENODEV if no | |
305 | * more interface IDs can be allocated. | |
306 | */ | |
28824b18 | 307 | int usb_interface_id(struct usb_configuration *config, |
40982be5 DB |
308 | struct usb_function *function) |
309 | { | |
310 | unsigned id = config->next_interface_id; | |
311 | ||
312 | if (id < MAX_CONFIG_INTERFACES) { | |
313 | config->interface[id] = function; | |
314 | config->next_interface_id = id + 1; | |
315 | return id; | |
316 | } | |
317 | return -ENODEV; | |
318 | } | |
319 | ||
320 | static int config_buf(struct usb_configuration *config, | |
321 | enum usb_device_speed speed, void *buf, u8 type) | |
322 | { | |
323 | struct usb_config_descriptor *c = buf; | |
324 | void *next = buf + USB_DT_CONFIG_SIZE; | |
325 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; | |
326 | struct usb_function *f; | |
327 | int status; | |
328 | ||
329 | /* write the config descriptor */ | |
330 | c = buf; | |
331 | c->bLength = USB_DT_CONFIG_SIZE; | |
332 | c->bDescriptorType = type; | |
333 | /* wTotalLength is written later */ | |
334 | c->bNumInterfaces = config->next_interface_id; | |
335 | c->bConfigurationValue = config->bConfigurationValue; | |
336 | c->iConfiguration = config->iConfiguration; | |
337 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; | |
36e893d2 | 338 | c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); |
40982be5 DB |
339 | |
340 | /* There may be e.g. OTG descriptors */ | |
341 | if (config->descriptors) { | |
342 | status = usb_descriptor_fillbuf(next, len, | |
343 | config->descriptors); | |
344 | if (status < 0) | |
345 | return status; | |
346 | len -= status; | |
347 | next += status; | |
348 | } | |
349 | ||
350 | /* add each function's descriptors */ | |
351 | list_for_each_entry(f, &config->functions, list) { | |
352 | struct usb_descriptor_header **descriptors; | |
353 | ||
354 | if (speed == USB_SPEED_HIGH) | |
355 | descriptors = f->hs_descriptors; | |
356 | else | |
357 | descriptors = f->descriptors; | |
358 | if (!descriptors) | |
359 | continue; | |
360 | status = usb_descriptor_fillbuf(next, len, | |
361 | (const struct usb_descriptor_header **) descriptors); | |
362 | if (status < 0) | |
363 | return status; | |
364 | len -= status; | |
365 | next += status; | |
366 | } | |
367 | ||
368 | len = next - buf; | |
369 | c->wTotalLength = cpu_to_le16(len); | |
370 | return len; | |
371 | } | |
372 | ||
373 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) | |
374 | { | |
375 | struct usb_gadget *gadget = cdev->gadget; | |
376 | struct usb_configuration *c; | |
377 | u8 type = w_value >> 8; | |
378 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; | |
379 | ||
380 | if (gadget_is_dualspeed(gadget)) { | |
381 | int hs = 0; | |
382 | ||
383 | if (gadget->speed == USB_SPEED_HIGH) | |
384 | hs = 1; | |
385 | if (type == USB_DT_OTHER_SPEED_CONFIG) | |
386 | hs = !hs; | |
387 | if (hs) | |
388 | speed = USB_SPEED_HIGH; | |
389 | ||
390 | } | |
391 | ||
392 | /* This is a lookup by config *INDEX* */ | |
393 | w_value &= 0xff; | |
394 | list_for_each_entry(c, &cdev->configs, list) { | |
395 | /* ignore configs that won't work at this speed */ | |
396 | if (speed == USB_SPEED_HIGH) { | |
397 | if (!c->highspeed) | |
398 | continue; | |
399 | } else { | |
400 | if (!c->fullspeed) | |
401 | continue; | |
402 | } | |
403 | if (w_value == 0) | |
404 | return config_buf(c, speed, cdev->req->buf, type); | |
405 | w_value--; | |
406 | } | |
407 | return -EINVAL; | |
408 | } | |
409 | ||
410 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) | |
411 | { | |
412 | struct usb_gadget *gadget = cdev->gadget; | |
413 | struct usb_configuration *c; | |
414 | unsigned count = 0; | |
415 | int hs = 0; | |
416 | ||
417 | if (gadget_is_dualspeed(gadget)) { | |
418 | if (gadget->speed == USB_SPEED_HIGH) | |
419 | hs = 1; | |
420 | if (type == USB_DT_DEVICE_QUALIFIER) | |
421 | hs = !hs; | |
422 | } | |
423 | list_for_each_entry(c, &cdev->configs, list) { | |
424 | /* ignore configs that won't work at this speed */ | |
425 | if (hs) { | |
426 | if (!c->highspeed) | |
427 | continue; | |
428 | } else { | |
429 | if (!c->fullspeed) | |
430 | continue; | |
431 | } | |
432 | count++; | |
433 | } | |
434 | return count; | |
435 | } | |
436 | ||
437 | static void device_qual(struct usb_composite_dev *cdev) | |
438 | { | |
439 | struct usb_qualifier_descriptor *qual = cdev->req->buf; | |
440 | ||
441 | qual->bLength = sizeof(*qual); | |
442 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; | |
443 | /* POLICY: same bcdUSB and device type info at both speeds */ | |
444 | qual->bcdUSB = cdev->desc.bcdUSB; | |
445 | qual->bDeviceClass = cdev->desc.bDeviceClass; | |
446 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; | |
447 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; | |
448 | /* ASSUME same EP0 fifo size at both speeds */ | |
449 | qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0; | |
450 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); | |
c24f4227 | 451 | qual->bRESERVED = 0; |
40982be5 DB |
452 | } |
453 | ||
454 | /*-------------------------------------------------------------------------*/ | |
455 | ||
456 | static void reset_config(struct usb_composite_dev *cdev) | |
457 | { | |
458 | struct usb_function *f; | |
459 | ||
460 | DBG(cdev, "reset config\n"); | |
461 | ||
462 | list_for_each_entry(f, &cdev->config->functions, list) { | |
463 | if (f->disable) | |
464 | f->disable(f); | |
5242658d LP |
465 | |
466 | bitmap_zero(f->endpoints, 32); | |
40982be5 DB |
467 | } |
468 | cdev->config = NULL; | |
469 | } | |
470 | ||
471 | static int set_config(struct usb_composite_dev *cdev, | |
472 | const struct usb_ctrlrequest *ctrl, unsigned number) | |
473 | { | |
474 | struct usb_gadget *gadget = cdev->gadget; | |
475 | struct usb_configuration *c = NULL; | |
476 | int result = -EINVAL; | |
477 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; | |
478 | int tmp; | |
479 | ||
480 | if (cdev->config) | |
481 | reset_config(cdev); | |
482 | ||
483 | if (number) { | |
484 | list_for_each_entry(c, &cdev->configs, list) { | |
485 | if (c->bConfigurationValue == number) { | |
486 | result = 0; | |
487 | break; | |
488 | } | |
489 | } | |
490 | if (result < 0) | |
491 | goto done; | |
492 | } else | |
493 | result = 0; | |
494 | ||
495 | INFO(cdev, "%s speed config #%d: %s\n", | |
496 | ({ char *speed; | |
497 | switch (gadget->speed) { | |
7c884fe4 TB |
498 | case USB_SPEED_LOW: |
499 | speed = "low"; | |
500 | break; | |
501 | case USB_SPEED_FULL: | |
502 | speed = "full"; | |
503 | break; | |
504 | case USB_SPEED_HIGH: | |
505 | speed = "high"; | |
506 | break; | |
507 | default: | |
508 | speed = "?"; | |
509 | break; | |
40982be5 DB |
510 | } ; speed; }), number, c ? c->label : "unconfigured"); |
511 | ||
512 | if (!c) | |
513 | goto done; | |
514 | ||
515 | cdev->config = c; | |
516 | ||
517 | /* Initialize all interfaces by setting them to altsetting zero. */ | |
518 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { | |
519 | struct usb_function *f = c->interface[tmp]; | |
5242658d | 520 | struct usb_descriptor_header **descriptors; |
40982be5 DB |
521 | |
522 | if (!f) | |
523 | break; | |
524 | ||
5242658d LP |
525 | /* |
526 | * Record which endpoints are used by the function. This is used | |
527 | * to dispatch control requests targeted at that endpoint to the | |
528 | * function's setup callback instead of the current | |
529 | * configuration's setup callback. | |
530 | */ | |
531 | if (gadget->speed == USB_SPEED_HIGH) | |
532 | descriptors = f->hs_descriptors; | |
533 | else | |
534 | descriptors = f->descriptors; | |
535 | ||
536 | for (; *descriptors; ++descriptors) { | |
537 | struct usb_endpoint_descriptor *ep; | |
538 | int addr; | |
539 | ||
540 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) | |
541 | continue; | |
542 | ||
543 | ep = (struct usb_endpoint_descriptor *)*descriptors; | |
544 | addr = ((ep->bEndpointAddress & 0x80) >> 3) | |
545 | | (ep->bEndpointAddress & 0x0f); | |
546 | set_bit(addr, f->endpoints); | |
547 | } | |
548 | ||
40982be5 DB |
549 | result = f->set_alt(f, tmp, 0); |
550 | if (result < 0) { | |
551 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", | |
552 | tmp, f->name, f, result); | |
553 | ||
554 | reset_config(cdev); | |
555 | goto done; | |
556 | } | |
1b9ba000 RQ |
557 | |
558 | if (result == USB_GADGET_DELAYED_STATUS) { | |
559 | DBG(cdev, | |
560 | "%s: interface %d (%s) requested delayed status\n", | |
561 | __func__, tmp, f->name); | |
562 | cdev->delayed_status++; | |
563 | DBG(cdev, "delayed_status count %d\n", | |
564 | cdev->delayed_status); | |
565 | } | |
40982be5 DB |
566 | } |
567 | ||
568 | /* when we return, be sure our power usage is valid */ | |
36e893d2 | 569 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
40982be5 DB |
570 | done: |
571 | usb_gadget_vbus_draw(gadget, power); | |
1b9ba000 RQ |
572 | if (result >= 0 && cdev->delayed_status) |
573 | result = USB_GADGET_DELAYED_STATUS; | |
40982be5 DB |
574 | return result; |
575 | } | |
576 | ||
577 | /** | |
578 | * usb_add_config() - add a configuration to a device. | |
579 | * @cdev: wraps the USB gadget | |
580 | * @config: the configuration, with bConfigurationValue assigned | |
c9bfff9c | 581 | * @bind: the configuration's bind function |
40982be5 DB |
582 | * Context: single threaded during gadget setup |
583 | * | |
c9bfff9c | 584 | * One of the main tasks of a composite @bind() routine is to |
40982be5 DB |
585 | * add each of the configurations it supports, using this routine. |
586 | * | |
c9bfff9c | 587 | * This function returns the value of the configuration's @bind(), which |
40982be5 DB |
588 | * is zero for success else a negative errno value. Binding configurations |
589 | * assigns global resources including string IDs, and per-configuration | |
590 | * resources such as interface IDs and endpoints. | |
591 | */ | |
28824b18 | 592 | int usb_add_config(struct usb_composite_dev *cdev, |
c9bfff9c UKK |
593 | struct usb_configuration *config, |
594 | int (*bind)(struct usb_configuration *)) | |
40982be5 DB |
595 | { |
596 | int status = -EINVAL; | |
597 | struct usb_configuration *c; | |
598 | ||
599 | DBG(cdev, "adding config #%u '%s'/%p\n", | |
600 | config->bConfigurationValue, | |
601 | config->label, config); | |
602 | ||
c9bfff9c | 603 | if (!config->bConfigurationValue || !bind) |
40982be5 DB |
604 | goto done; |
605 | ||
606 | /* Prevent duplicate configuration identifiers */ | |
607 | list_for_each_entry(c, &cdev->configs, list) { | |
608 | if (c->bConfigurationValue == config->bConfigurationValue) { | |
609 | status = -EBUSY; | |
610 | goto done; | |
611 | } | |
612 | } | |
613 | ||
614 | config->cdev = cdev; | |
615 | list_add_tail(&config->list, &cdev->configs); | |
616 | ||
617 | INIT_LIST_HEAD(&config->functions); | |
618 | config->next_interface_id = 0; | |
619 | ||
c9bfff9c | 620 | status = bind(config); |
40982be5 DB |
621 | if (status < 0) { |
622 | list_del(&config->list); | |
623 | config->cdev = NULL; | |
624 | } else { | |
625 | unsigned i; | |
626 | ||
627 | DBG(cdev, "cfg %d/%p speeds:%s%s\n", | |
628 | config->bConfigurationValue, config, | |
629 | config->highspeed ? " high" : "", | |
630 | config->fullspeed | |
631 | ? (gadget_is_dualspeed(cdev->gadget) | |
632 | ? " full" | |
633 | : " full/low") | |
634 | : ""); | |
635 | ||
636 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { | |
637 | struct usb_function *f = config->interface[i]; | |
638 | ||
639 | if (!f) | |
640 | continue; | |
641 | DBG(cdev, " interface %d = %s/%p\n", | |
642 | i, f->name, f); | |
643 | } | |
644 | } | |
645 | ||
c9bfff9c | 646 | /* set_alt(), or next bind(), sets up |
40982be5 DB |
647 | * ep->driver_data as needed. |
648 | */ | |
649 | usb_ep_autoconfig_reset(cdev->gadget); | |
650 | ||
651 | done: | |
652 | if (status) | |
653 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, | |
654 | config->bConfigurationValue, status); | |
655 | return status; | |
656 | } | |
657 | ||
658 | /*-------------------------------------------------------------------------*/ | |
659 | ||
660 | /* We support strings in multiple languages ... string descriptor zero | |
661 | * says which languages are supported. The typical case will be that | |
662 | * only one language (probably English) is used, with I18N handled on | |
663 | * the host side. | |
664 | */ | |
665 | ||
666 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) | |
667 | { | |
668 | const struct usb_gadget_strings *s; | |
669 | u16 language; | |
670 | __le16 *tmp; | |
671 | ||
672 | while (*sp) { | |
673 | s = *sp; | |
674 | language = cpu_to_le16(s->language); | |
675 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { | |
676 | if (*tmp == language) | |
677 | goto repeat; | |
678 | } | |
679 | *tmp++ = language; | |
680 | repeat: | |
681 | sp++; | |
682 | } | |
683 | } | |
684 | ||
685 | static int lookup_string( | |
686 | struct usb_gadget_strings **sp, | |
687 | void *buf, | |
688 | u16 language, | |
689 | int id | |
690 | ) | |
691 | { | |
692 | struct usb_gadget_strings *s; | |
693 | int value; | |
694 | ||
695 | while (*sp) { | |
696 | s = *sp++; | |
697 | if (s->language != language) | |
698 | continue; | |
699 | value = usb_gadget_get_string(s, id, buf); | |
700 | if (value > 0) | |
701 | return value; | |
702 | } | |
703 | return -EINVAL; | |
704 | } | |
705 | ||
706 | static int get_string(struct usb_composite_dev *cdev, | |
707 | void *buf, u16 language, int id) | |
708 | { | |
709 | struct usb_configuration *c; | |
710 | struct usb_function *f; | |
711 | int len; | |
ad1a8102 | 712 | const char *str; |
40982be5 DB |
713 | |
714 | /* Yes, not only is USB's I18N support probably more than most | |
715 | * folk will ever care about ... also, it's all supported here. | |
716 | * (Except for UTF8 support for Unicode's "Astral Planes".) | |
717 | */ | |
718 | ||
719 | /* 0 == report all available language codes */ | |
720 | if (id == 0) { | |
721 | struct usb_string_descriptor *s = buf; | |
722 | struct usb_gadget_strings **sp; | |
723 | ||
724 | memset(s, 0, 256); | |
725 | s->bDescriptorType = USB_DT_STRING; | |
726 | ||
727 | sp = composite->strings; | |
728 | if (sp) | |
729 | collect_langs(sp, s->wData); | |
730 | ||
731 | list_for_each_entry(c, &cdev->configs, list) { | |
732 | sp = c->strings; | |
733 | if (sp) | |
734 | collect_langs(sp, s->wData); | |
735 | ||
736 | list_for_each_entry(f, &c->functions, list) { | |
737 | sp = f->strings; | |
738 | if (sp) | |
739 | collect_langs(sp, s->wData); | |
740 | } | |
741 | } | |
742 | ||
417b57b3 | 743 | for (len = 0; len <= 126 && s->wData[len]; len++) |
40982be5 DB |
744 | continue; |
745 | if (!len) | |
746 | return -EINVAL; | |
747 | ||
748 | s->bLength = 2 * (len + 1); | |
749 | return s->bLength; | |
750 | } | |
751 | ||
ad1a8102 MN |
752 | /* Otherwise, look up and return a specified string. First |
753 | * check if the string has not been overridden. | |
754 | */ | |
755 | if (cdev->manufacturer_override == id) | |
756 | str = iManufacturer ?: composite->iManufacturer ?: | |
757 | composite_manufacturer; | |
758 | else if (cdev->product_override == id) | |
759 | str = iProduct ?: composite->iProduct; | |
760 | else if (cdev->serial_override == id) | |
761 | str = iSerialNumber; | |
762 | else | |
763 | str = NULL; | |
764 | if (str) { | |
765 | struct usb_gadget_strings strings = { | |
766 | .language = language, | |
767 | .strings = &(struct usb_string) { 0xff, str } | |
768 | }; | |
769 | return usb_gadget_get_string(&strings, 0xff, buf); | |
770 | } | |
771 | ||
772 | /* String IDs are device-scoped, so we look up each string | |
773 | * table we're told about. These lookups are infrequent; | |
774 | * simpler-is-better here. | |
40982be5 DB |
775 | */ |
776 | if (composite->strings) { | |
777 | len = lookup_string(composite->strings, buf, language, id); | |
778 | if (len > 0) | |
779 | return len; | |
780 | } | |
781 | list_for_each_entry(c, &cdev->configs, list) { | |
782 | if (c->strings) { | |
783 | len = lookup_string(c->strings, buf, language, id); | |
784 | if (len > 0) | |
785 | return len; | |
786 | } | |
787 | list_for_each_entry(f, &c->functions, list) { | |
788 | if (!f->strings) | |
789 | continue; | |
790 | len = lookup_string(f->strings, buf, language, id); | |
791 | if (len > 0) | |
792 | return len; | |
793 | } | |
794 | } | |
795 | return -EINVAL; | |
796 | } | |
797 | ||
798 | /** | |
799 | * usb_string_id() - allocate an unused string ID | |
800 | * @cdev: the device whose string descriptor IDs are being allocated | |
801 | * Context: single threaded during gadget setup | |
802 | * | |
803 | * @usb_string_id() is called from bind() callbacks to allocate | |
804 | * string IDs. Drivers for functions, configurations, or gadgets will | |
805 | * then store that ID in the appropriate descriptors and string table. | |
806 | * | |
f2adc4f8 MN |
807 | * All string identifier should be allocated using this, |
808 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure | |
809 | * that for example different functions don't wrongly assign different | |
810 | * meanings to the same identifier. | |
40982be5 | 811 | */ |
28824b18 | 812 | int usb_string_id(struct usb_composite_dev *cdev) |
40982be5 DB |
813 | { |
814 | if (cdev->next_string_id < 254) { | |
f2adc4f8 MN |
815 | /* string id 0 is reserved by USB spec for list of |
816 | * supported languages */ | |
817 | /* 255 reserved as well? -- mina86 */ | |
40982be5 DB |
818 | cdev->next_string_id++; |
819 | return cdev->next_string_id; | |
820 | } | |
821 | return -ENODEV; | |
822 | } | |
823 | ||
f2adc4f8 MN |
824 | /** |
825 | * usb_string_ids() - allocate unused string IDs in batch | |
826 | * @cdev: the device whose string descriptor IDs are being allocated | |
827 | * @str: an array of usb_string objects to assign numbers to | |
828 | * Context: single threaded during gadget setup | |
829 | * | |
830 | * @usb_string_ids() is called from bind() callbacks to allocate | |
831 | * string IDs. Drivers for functions, configurations, or gadgets will | |
832 | * then copy IDs from the string table to the appropriate descriptors | |
833 | * and string table for other languages. | |
834 | * | |
835 | * All string identifier should be allocated using this, | |
836 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for | |
837 | * example different functions don't wrongly assign different meanings | |
838 | * to the same identifier. | |
839 | */ | |
840 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) | |
841 | { | |
842 | int next = cdev->next_string_id; | |
843 | ||
844 | for (; str->s; ++str) { | |
845 | if (unlikely(next >= 254)) | |
846 | return -ENODEV; | |
847 | str->id = ++next; | |
848 | } | |
849 | ||
850 | cdev->next_string_id = next; | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | /** | |
856 | * usb_string_ids_n() - allocate unused string IDs in batch | |
d187abb9 | 857 | * @c: the device whose string descriptor IDs are being allocated |
f2adc4f8 MN |
858 | * @n: number of string IDs to allocate |
859 | * Context: single threaded during gadget setup | |
860 | * | |
861 | * Returns the first requested ID. This ID and next @n-1 IDs are now | |
d187abb9 | 862 | * valid IDs. At least provided that @n is non-zero because if it |
f2adc4f8 MN |
863 | * is, returns last requested ID which is now very useful information. |
864 | * | |
865 | * @usb_string_ids_n() is called from bind() callbacks to allocate | |
866 | * string IDs. Drivers for functions, configurations, or gadgets will | |
867 | * then store that ID in the appropriate descriptors and string table. | |
868 | * | |
869 | * All string identifier should be allocated using this, | |
870 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for | |
871 | * example different functions don't wrongly assign different meanings | |
872 | * to the same identifier. | |
873 | */ | |
874 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) | |
875 | { | |
876 | unsigned next = c->next_string_id; | |
877 | if (unlikely(n > 254 || (unsigned)next + n > 254)) | |
878 | return -ENODEV; | |
879 | c->next_string_id += n; | |
880 | return next + 1; | |
881 | } | |
882 | ||
883 | ||
40982be5 DB |
884 | /*-------------------------------------------------------------------------*/ |
885 | ||
886 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) | |
887 | { | |
888 | if (req->status || req->actual != req->length) | |
889 | DBG((struct usb_composite_dev *) ep->driver_data, | |
890 | "setup complete --> %d, %d/%d\n", | |
891 | req->status, req->actual, req->length); | |
892 | } | |
893 | ||
894 | /* | |
895 | * The setup() callback implements all the ep0 functionality that's | |
896 | * not handled lower down, in hardware or the hardware driver(like | |
897 | * device and endpoint feature flags, and their status). It's all | |
898 | * housekeeping for the gadget function we're implementing. Most of | |
899 | * the work is in config and function specific setup. | |
900 | */ | |
901 | static int | |
902 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | |
903 | { | |
904 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
905 | struct usb_request *req = cdev->req; | |
906 | int value = -EOPNOTSUPP; | |
907 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
08889517 | 908 | u8 intf = w_index & 0xFF; |
40982be5 DB |
909 | u16 w_value = le16_to_cpu(ctrl->wValue); |
910 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
911 | struct usb_function *f = NULL; | |
5242658d | 912 | u8 endp; |
40982be5 DB |
913 | |
914 | /* partial re-init of the response message; the function or the | |
915 | * gadget might need to intercept e.g. a control-OUT completion | |
916 | * when we delegate to it. | |
917 | */ | |
918 | req->zero = 0; | |
919 | req->complete = composite_setup_complete; | |
2edb11cb | 920 | req->length = 0; |
40982be5 DB |
921 | gadget->ep0->driver_data = cdev; |
922 | ||
923 | switch (ctrl->bRequest) { | |
924 | ||
925 | /* we handle all standard USB descriptors */ | |
926 | case USB_REQ_GET_DESCRIPTOR: | |
927 | if (ctrl->bRequestType != USB_DIR_IN) | |
928 | goto unknown; | |
929 | switch (w_value >> 8) { | |
930 | ||
931 | case USB_DT_DEVICE: | |
932 | cdev->desc.bNumConfigurations = | |
933 | count_configs(cdev, USB_DT_DEVICE); | |
934 | value = min(w_length, (u16) sizeof cdev->desc); | |
935 | memcpy(req->buf, &cdev->desc, value); | |
936 | break; | |
937 | case USB_DT_DEVICE_QUALIFIER: | |
938 | if (!gadget_is_dualspeed(gadget)) | |
939 | break; | |
940 | device_qual(cdev); | |
941 | value = min_t(int, w_length, | |
942 | sizeof(struct usb_qualifier_descriptor)); | |
943 | break; | |
944 | case USB_DT_OTHER_SPEED_CONFIG: | |
945 | if (!gadget_is_dualspeed(gadget)) | |
946 | break; | |
947 | /* FALLTHROUGH */ | |
948 | case USB_DT_CONFIG: | |
949 | value = config_desc(cdev, w_value); | |
950 | if (value >= 0) | |
951 | value = min(w_length, (u16) value); | |
952 | break; | |
953 | case USB_DT_STRING: | |
954 | value = get_string(cdev, req->buf, | |
955 | w_index, w_value & 0xff); | |
956 | if (value >= 0) | |
957 | value = min(w_length, (u16) value); | |
958 | break; | |
959 | } | |
960 | break; | |
961 | ||
962 | /* any number of configs can work */ | |
963 | case USB_REQ_SET_CONFIGURATION: | |
964 | if (ctrl->bRequestType != 0) | |
965 | goto unknown; | |
966 | if (gadget_is_otg(gadget)) { | |
967 | if (gadget->a_hnp_support) | |
968 | DBG(cdev, "HNP available\n"); | |
969 | else if (gadget->a_alt_hnp_support) | |
970 | DBG(cdev, "HNP on another port\n"); | |
971 | else | |
972 | VDBG(cdev, "HNP inactive\n"); | |
973 | } | |
974 | spin_lock(&cdev->lock); | |
975 | value = set_config(cdev, ctrl, w_value); | |
976 | spin_unlock(&cdev->lock); | |
977 | break; | |
978 | case USB_REQ_GET_CONFIGURATION: | |
979 | if (ctrl->bRequestType != USB_DIR_IN) | |
980 | goto unknown; | |
981 | if (cdev->config) | |
982 | *(u8 *)req->buf = cdev->config->bConfigurationValue; | |
983 | else | |
984 | *(u8 *)req->buf = 0; | |
985 | value = min(w_length, (u16) 1); | |
986 | break; | |
987 | ||
988 | /* function drivers must handle get/set altsetting; if there's | |
989 | * no get() method, we know only altsetting zero works. | |
990 | */ | |
991 | case USB_REQ_SET_INTERFACE: | |
992 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) | |
993 | goto unknown; | |
ff085de7 | 994 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
40982be5 | 995 | break; |
08889517 | 996 | f = cdev->config->interface[intf]; |
40982be5 DB |
997 | if (!f) |
998 | break; | |
dd4dff8b | 999 | if (w_value && !f->set_alt) |
40982be5 DB |
1000 | break; |
1001 | value = f->set_alt(f, w_index, w_value); | |
1b9ba000 RQ |
1002 | if (value == USB_GADGET_DELAYED_STATUS) { |
1003 | DBG(cdev, | |
1004 | "%s: interface %d (%s) requested delayed status\n", | |
1005 | __func__, intf, f->name); | |
1006 | cdev->delayed_status++; | |
1007 | DBG(cdev, "delayed_status count %d\n", | |
1008 | cdev->delayed_status); | |
1009 | } | |
40982be5 DB |
1010 | break; |
1011 | case USB_REQ_GET_INTERFACE: | |
1012 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) | |
1013 | goto unknown; | |
ff085de7 | 1014 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
40982be5 | 1015 | break; |
08889517 | 1016 | f = cdev->config->interface[intf]; |
40982be5 DB |
1017 | if (!f) |
1018 | break; | |
1019 | /* lots of interfaces only need altsetting zero... */ | |
1020 | value = f->get_alt ? f->get_alt(f, w_index) : 0; | |
1021 | if (value < 0) | |
1022 | break; | |
1023 | *((u8 *)req->buf) = value; | |
1024 | value = min(w_length, (u16) 1); | |
1025 | break; | |
1026 | default: | |
1027 | unknown: | |
1028 | VDBG(cdev, | |
1029 | "non-core control req%02x.%02x v%04x i%04x l%d\n", | |
1030 | ctrl->bRequestType, ctrl->bRequest, | |
1031 | w_value, w_index, w_length); | |
1032 | ||
5242658d LP |
1033 | /* functions always handle their interfaces and endpoints... |
1034 | * punt other recipients (other, WUSB, ...) to the current | |
40982be5 DB |
1035 | * configuration code. |
1036 | * | |
1037 | * REVISIT it could make sense to let the composite device | |
1038 | * take such requests too, if that's ever needed: to work | |
1039 | * in config 0, etc. | |
1040 | */ | |
5242658d LP |
1041 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
1042 | case USB_RECIP_INTERFACE: | |
ff085de7 | 1043 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
3c47eb06 MM |
1044 | break; |
1045 | f = cdev->config->interface[intf]; | |
5242658d LP |
1046 | break; |
1047 | ||
1048 | case USB_RECIP_ENDPOINT: | |
1049 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); | |
1050 | list_for_each_entry(f, &cdev->config->functions, list) { | |
1051 | if (test_bit(endp, f->endpoints)) | |
1052 | break; | |
1053 | } | |
1054 | if (&f->list == &cdev->config->functions) | |
40982be5 | 1055 | f = NULL; |
5242658d | 1056 | break; |
40982be5 | 1057 | } |
5242658d LP |
1058 | |
1059 | if (f && f->setup) | |
1060 | value = f->setup(f, ctrl); | |
1061 | else { | |
40982be5 DB |
1062 | struct usb_configuration *c; |
1063 | ||
1064 | c = cdev->config; | |
1065 | if (c && c->setup) | |
1066 | value = c->setup(c, ctrl); | |
1067 | } | |
1068 | ||
1069 | goto done; | |
1070 | } | |
1071 | ||
1072 | /* respond with data transfer before status phase? */ | |
1b9ba000 | 1073 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
40982be5 DB |
1074 | req->length = value; |
1075 | req->zero = value < w_length; | |
1076 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | |
1077 | if (value < 0) { | |
1078 | DBG(cdev, "ep_queue --> %d\n", value); | |
1079 | req->status = 0; | |
1080 | composite_setup_complete(gadget->ep0, req); | |
1081 | } | |
1b9ba000 RQ |
1082 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
1083 | WARN(cdev, | |
1084 | "%s: Delayed status not supported for w_length != 0", | |
1085 | __func__); | |
40982be5 DB |
1086 | } |
1087 | ||
1088 | done: | |
1089 | /* device either stalls (value < 0) or reports success */ | |
1090 | return value; | |
1091 | } | |
1092 | ||
1093 | static void composite_disconnect(struct usb_gadget *gadget) | |
1094 | { | |
1095 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1096 | unsigned long flags; | |
1097 | ||
1098 | /* REVISIT: should we have config and device level | |
1099 | * disconnect callbacks? | |
1100 | */ | |
1101 | spin_lock_irqsave(&cdev->lock, flags); | |
1102 | if (cdev->config) | |
1103 | reset_config(cdev); | |
3f3e12d0 MN |
1104 | if (composite->disconnect) |
1105 | composite->disconnect(cdev); | |
40982be5 DB |
1106 | spin_unlock_irqrestore(&cdev->lock, flags); |
1107 | } | |
1108 | ||
1109 | /*-------------------------------------------------------------------------*/ | |
1110 | ||
f48cf80f FC |
1111 | static ssize_t composite_show_suspended(struct device *dev, |
1112 | struct device_attribute *attr, | |
1113 | char *buf) | |
1114 | { | |
1115 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); | |
1116 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1117 | ||
1118 | return sprintf(buf, "%d\n", cdev->suspended); | |
1119 | } | |
1120 | ||
1121 | static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL); | |
1122 | ||
28824b18 | 1123 | static void |
40982be5 DB |
1124 | composite_unbind(struct usb_gadget *gadget) |
1125 | { | |
1126 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1127 | ||
1128 | /* composite_disconnect() must already have been called | |
1129 | * by the underlying peripheral controller driver! | |
1130 | * so there's no i/o concurrency that could affect the | |
1131 | * state protected by cdev->lock. | |
1132 | */ | |
1133 | WARN_ON(cdev->config); | |
1134 | ||
1135 | while (!list_empty(&cdev->configs)) { | |
1136 | struct usb_configuration *c; | |
1137 | ||
1138 | c = list_first_entry(&cdev->configs, | |
1139 | struct usb_configuration, list); | |
1140 | while (!list_empty(&c->functions)) { | |
1141 | struct usb_function *f; | |
1142 | ||
1143 | f = list_first_entry(&c->functions, | |
1144 | struct usb_function, list); | |
1145 | list_del(&f->list); | |
1146 | if (f->unbind) { | |
1147 | DBG(cdev, "unbind function '%s'/%p\n", | |
1148 | f->name, f); | |
1149 | f->unbind(c, f); | |
1150 | /* may free memory for "f" */ | |
1151 | } | |
1152 | } | |
1153 | list_del(&c->list); | |
1154 | if (c->unbind) { | |
1155 | DBG(cdev, "unbind config '%s'/%p\n", c->label, c); | |
1156 | c->unbind(c); | |
1157 | /* may free memory for "c" */ | |
1158 | } | |
1159 | } | |
1160 | if (composite->unbind) | |
1161 | composite->unbind(cdev); | |
1162 | ||
1163 | if (cdev->req) { | |
1164 | kfree(cdev->req->buf); | |
1165 | usb_ep_free_request(gadget->ep0, cdev->req); | |
1166 | } | |
daba5803 | 1167 | device_remove_file(&gadget->dev, &dev_attr_suspended); |
40982be5 DB |
1168 | kfree(cdev); |
1169 | set_gadget_data(gadget, NULL); | |
1170 | composite = NULL; | |
1171 | } | |
1172 | ||
ad1a8102 | 1173 | static u8 override_id(struct usb_composite_dev *cdev, u8 *desc) |
40982be5 | 1174 | { |
ad1a8102 MN |
1175 | if (!*desc) { |
1176 | int ret = usb_string_id(cdev); | |
1177 | if (unlikely(ret < 0)) | |
1178 | WARNING(cdev, "failed to override string ID\n"); | |
1179 | else | |
1180 | *desc = ret; | |
40982be5 | 1181 | } |
40982be5 | 1182 | |
ad1a8102 | 1183 | return *desc; |
40982be5 DB |
1184 | } |
1185 | ||
28824b18 | 1186 | static int composite_bind(struct usb_gadget *gadget) |
40982be5 DB |
1187 | { |
1188 | struct usb_composite_dev *cdev; | |
1189 | int status = -ENOMEM; | |
1190 | ||
1191 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); | |
1192 | if (!cdev) | |
1193 | return status; | |
1194 | ||
1195 | spin_lock_init(&cdev->lock); | |
1196 | cdev->gadget = gadget; | |
1197 | set_gadget_data(gadget, cdev); | |
1198 | INIT_LIST_HEAD(&cdev->configs); | |
1199 | ||
1200 | /* preallocate control response and buffer */ | |
1201 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); | |
1202 | if (!cdev->req) | |
1203 | goto fail; | |
1204 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); | |
1205 | if (!cdev->req->buf) | |
1206 | goto fail; | |
1207 | cdev->req->complete = composite_setup_complete; | |
1208 | gadget->ep0->driver_data = cdev; | |
1209 | ||
1210 | cdev->bufsiz = USB_BUFSIZ; | |
1211 | cdev->driver = composite; | |
1212 | ||
37b5801e PM |
1213 | /* |
1214 | * As per USB compliance update, a device that is actively drawing | |
1215 | * more than 100mA from USB must report itself as bus-powered in | |
1216 | * the GetStatus(DEVICE) call. | |
1217 | */ | |
1218 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) | |
1219 | usb_gadget_set_selfpowered(gadget); | |
40982be5 DB |
1220 | |
1221 | /* interface and string IDs start at zero via kzalloc. | |
1222 | * we force endpoints to start unassigned; few controller | |
1223 | * drivers will zero ep->driver_data. | |
1224 | */ | |
1225 | usb_ep_autoconfig_reset(cdev->gadget); | |
1226 | ||
1227 | /* composite gadget needs to assign strings for whole device (like | |
1228 | * serial number), register function drivers, potentially update | |
1229 | * power state and consumption, etc | |
1230 | */ | |
07a18bd7 | 1231 | status = composite_gadget_bind(cdev); |
40982be5 DB |
1232 | if (status < 0) |
1233 | goto fail; | |
1234 | ||
1235 | cdev->desc = *composite->dev; | |
1236 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | |
1237 | ||
dbb442b8 GKH |
1238 | /* standardized runtime overrides for device ID data */ |
1239 | if (idVendor) | |
1240 | cdev->desc.idVendor = cpu_to_le16(idVendor); | |
1241 | if (idProduct) | |
1242 | cdev->desc.idProduct = cpu_to_le16(idProduct); | |
1243 | if (bcdDevice) | |
1244 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); | |
1245 | ||
78bff3c6 | 1246 | /* string overrides */ |
ad1a8102 MN |
1247 | if (iManufacturer || !cdev->desc.iManufacturer) { |
1248 | if (!iManufacturer && !composite->iManufacturer && | |
1249 | !*composite_manufacturer) | |
1250 | snprintf(composite_manufacturer, | |
1251 | sizeof composite_manufacturer, | |
1252 | "%s %s with %s", | |
1253 | init_utsname()->sysname, | |
1254 | init_utsname()->release, | |
1255 | gadget->name); | |
1256 | ||
1257 | cdev->manufacturer_override = | |
1258 | override_id(cdev, &cdev->desc.iManufacturer); | |
1259 | } | |
1260 | ||
1261 | if (iProduct || (!cdev->desc.iProduct && composite->iProduct)) | |
1262 | cdev->product_override = | |
1263 | override_id(cdev, &cdev->desc.iProduct); | |
1264 | ||
1265 | if (iSerialNumber) | |
1266 | cdev->serial_override = | |
1267 | override_id(cdev, &cdev->desc.iSerialNumber); | |
1268 | ||
1269 | /* has userspace failed to provide a serial number? */ | |
1270 | if (composite->needs_serial && !cdev->desc.iSerialNumber) | |
1271 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); | |
40982be5 | 1272 | |
ad1a8102 | 1273 | /* finish up */ |
f48cf80f FC |
1274 | status = device_create_file(&gadget->dev, &dev_attr_suspended); |
1275 | if (status) | |
1276 | goto fail; | |
1277 | ||
40982be5 DB |
1278 | INFO(cdev, "%s ready\n", composite->name); |
1279 | return 0; | |
1280 | ||
1281 | fail: | |
1282 | composite_unbind(gadget); | |
1283 | return status; | |
1284 | } | |
1285 | ||
1286 | /*-------------------------------------------------------------------------*/ | |
1287 | ||
1288 | static void | |
1289 | composite_suspend(struct usb_gadget *gadget) | |
1290 | { | |
1291 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1292 | struct usb_function *f; | |
1293 | ||
8942939a | 1294 | /* REVISIT: should we have config level |
40982be5 DB |
1295 | * suspend/resume callbacks? |
1296 | */ | |
1297 | DBG(cdev, "suspend\n"); | |
1298 | if (cdev->config) { | |
1299 | list_for_each_entry(f, &cdev->config->functions, list) { | |
1300 | if (f->suspend) | |
1301 | f->suspend(f); | |
1302 | } | |
1303 | } | |
8942939a DB |
1304 | if (composite->suspend) |
1305 | composite->suspend(cdev); | |
f48cf80f FC |
1306 | |
1307 | cdev->suspended = 1; | |
b23f2f94 HW |
1308 | |
1309 | usb_gadget_vbus_draw(gadget, 2); | |
40982be5 DB |
1310 | } |
1311 | ||
1312 | static void | |
1313 | composite_resume(struct usb_gadget *gadget) | |
1314 | { | |
1315 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | |
1316 | struct usb_function *f; | |
b23f2f94 | 1317 | u8 maxpower; |
40982be5 | 1318 | |
8942939a | 1319 | /* REVISIT: should we have config level |
40982be5 DB |
1320 | * suspend/resume callbacks? |
1321 | */ | |
1322 | DBG(cdev, "resume\n"); | |
8942939a DB |
1323 | if (composite->resume) |
1324 | composite->resume(cdev); | |
40982be5 DB |
1325 | if (cdev->config) { |
1326 | list_for_each_entry(f, &cdev->config->functions, list) { | |
1327 | if (f->resume) | |
1328 | f->resume(f); | |
1329 | } | |
b23f2f94 HW |
1330 | |
1331 | maxpower = cdev->config->bMaxPower; | |
1332 | ||
1333 | usb_gadget_vbus_draw(gadget, maxpower ? | |
1334 | (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW); | |
40982be5 | 1335 | } |
f48cf80f FC |
1336 | |
1337 | cdev->suspended = 0; | |
40982be5 DB |
1338 | } |
1339 | ||
1340 | /*-------------------------------------------------------------------------*/ | |
1341 | ||
1342 | static struct usb_gadget_driver composite_driver = { | |
1343 | .speed = USB_SPEED_HIGH, | |
1344 | ||
915c8bef | 1345 | .unbind = composite_unbind, |
40982be5 DB |
1346 | |
1347 | .setup = composite_setup, | |
1348 | .disconnect = composite_disconnect, | |
1349 | ||
1350 | .suspend = composite_suspend, | |
1351 | .resume = composite_resume, | |
1352 | ||
1353 | .driver = { | |
1354 | .owner = THIS_MODULE, | |
1355 | }, | |
1356 | }; | |
1357 | ||
1358 | /** | |
07a18bd7 | 1359 | * usb_composite_probe() - register a composite driver |
40982be5 | 1360 | * @driver: the driver to register |
07a18bd7 MN |
1361 | * @bind: the callback used to allocate resources that are shared across the |
1362 | * whole device, such as string IDs, and add its configurations using | |
1363 | * @usb_add_config(). This may fail by returning a negative errno | |
1364 | * value; it should return zero on successful initialization. | |
40982be5 DB |
1365 | * Context: single threaded during gadget setup |
1366 | * | |
1367 | * This function is used to register drivers using the composite driver | |
1368 | * framework. The return value is zero, or a negative errno value. | |
1369 | * Those values normally come from the driver's @bind method, which does | |
1370 | * all the work of setting up the driver to match the hardware. | |
1371 | * | |
1372 | * On successful return, the gadget is ready to respond to requests from | |
1373 | * the host, unless one of its components invokes usb_gadget_disconnect() | |
1374 | * while it was binding. That would usually be done in order to wait for | |
1375 | * some userspace participation. | |
1376 | */ | |
05c3eebd | 1377 | int usb_composite_probe(struct usb_composite_driver *driver, |
07a18bd7 | 1378 | int (*bind)(struct usb_composite_dev *cdev)) |
40982be5 | 1379 | { |
07a18bd7 | 1380 | if (!driver || !driver->dev || !bind || composite) |
40982be5 DB |
1381 | return -EINVAL; |
1382 | ||
1383 | if (!driver->name) | |
1384 | driver->name = "composite"; | |
05c3eebd JB |
1385 | if (!driver->iProduct) |
1386 | driver->iProduct = driver->name; | |
40982be5 DB |
1387 | composite_driver.function = (char *) driver->name; |
1388 | composite_driver.driver.name = driver->name; | |
35a0e0bf TB |
1389 | composite_driver.speed = min((u8)composite_driver.speed, |
1390 | (u8)driver->max_speed); | |
40982be5 | 1391 | composite = driver; |
07a18bd7 | 1392 | composite_gadget_bind = bind; |
40982be5 | 1393 | |
b0fca50f | 1394 | return usb_gadget_probe_driver(&composite_driver, composite_bind); |
40982be5 DB |
1395 | } |
1396 | ||
1397 | /** | |
1398 | * usb_composite_unregister() - unregister a composite driver | |
1399 | * @driver: the driver to unregister | |
1400 | * | |
1401 | * This function is used to unregister drivers using the composite | |
1402 | * driver framework. | |
1403 | */ | |
28824b18 | 1404 | void usb_composite_unregister(struct usb_composite_driver *driver) |
40982be5 DB |
1405 | { |
1406 | if (composite != driver) | |
1407 | return; | |
1408 | usb_gadget_unregister_driver(&composite_driver); | |
1409 | } | |
1b9ba000 RQ |
1410 | |
1411 | /** | |
1412 | * usb_composite_setup_continue() - Continue with the control transfer | |
1413 | * @cdev: the composite device who's control transfer was kept waiting | |
1414 | * | |
1415 | * This function must be called by the USB function driver to continue | |
1416 | * with the control transfer's data/status stage in case it had requested to | |
1417 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) | |
1418 | * can request the composite framework to delay the setup request's data/status | |
1419 | * stages by returning USB_GADGET_DELAYED_STATUS. | |
1420 | */ | |
1421 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) | |
1422 | { | |
1423 | int value; | |
1424 | struct usb_request *req = cdev->req; | |
1425 | unsigned long flags; | |
1426 | ||
1427 | DBG(cdev, "%s\n", __func__); | |
1428 | spin_lock_irqsave(&cdev->lock, flags); | |
1429 | ||
1430 | if (cdev->delayed_status == 0) { | |
1431 | WARN(cdev, "%s: Unexpected call\n", __func__); | |
1432 | ||
1433 | } else if (--cdev->delayed_status == 0) { | |
1434 | DBG(cdev, "%s: Completing delayed status\n", __func__); | |
1435 | req->length = 0; | |
1436 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
1437 | if (value < 0) { | |
1438 | DBG(cdev, "ep_queue --> %d\n", value); | |
1439 | req->status = 0; | |
1440 | composite_setup_complete(cdev->gadget->ep0, req); | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | spin_unlock_irqrestore(&cdev->lock, flags); | |
1445 | } | |
1446 |