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