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