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