blob: 2a6bfe759c2971435a3fbb10203e5e3f0a3ee3c1 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
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.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070020
21#include <linux/usb/composite.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030022#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070023
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
Tatyana Brokhman48767a42011-06-28 16:33:49 +030031/**
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 */
40static struct usb_descriptor_header**
41next_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 */
78int config_ep_by_speed(struct usb_gadget *g,
79 struct usb_function *f,
80 struct usb_ep *_ep)
81{
Felipe Balbib785ea72012-06-06 10:20:23 +030082 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +030083 struct usb_endpoint_descriptor *chosen_desc = NULL;
84 struct usb_descriptor_header **speed_desc = NULL;
85
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030086 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
87 int want_comp_desc = 0;
88
Tatyana Brokhman48767a42011-06-28 16:33:49 +030089 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) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030096 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 */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300103 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:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200110 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300111 }
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
120ep_found:
121 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700122 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300123 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300124 _ep->comp_desc = NULL;
125 _ep->maxburst = 0;
126 _ep->mult = 0;
127 if (!want_comp_desc)
128 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300129
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300130 /*
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)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300141 case USB_ENDPOINT_XFER_ISOC:
142 /* mult: bits 1:0 of bmAttributes */
143 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800144 case USB_ENDPOINT_XFER_BULK:
145 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300146 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300147 break;
148 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300149 if (comp_desc->bMaxBurst != 0)
150 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
151 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300152 break;
153 }
154 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300155 return 0;
156}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200157EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700158
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 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200173int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700174 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 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200203 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700204 config->fullspeed = true;
205 if (!config->highspeed && function->hs_descriptors)
206 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300207 if (!config->superspeed && function->ss_descriptors)
208 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700209
210done:
211 if (value)
212 DBG(config->cdev, "adding '%s'/%p --> %d\n",
213 function->name, function, value);
214 return value;
215}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200216EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700217
218/**
David Brownell60beed92008-08-18 17:38:22 -0700219 * 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 */
237int usb_function_deactivate(struct usb_function *function)
238{
239 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200240 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700241 int status = 0;
242
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200243 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700244
245 if (cdev->deactivations == 0)
246 status = usb_gadget_disconnect(cdev->gadget);
247 if (status == 0)
248 cdev->deactivations++;
249
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200250 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700251 return status;
252}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200253EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700254
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 */
265int usb_function_activate(struct usb_function *function)
266{
267 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200268 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700269 int status = 0;
270
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200271 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700272
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
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200281 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700282 return status;
283}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200284EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700285
286/**
David Brownell40982be2008-06-19 17:52:58 -0700287 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300295 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700296 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300302 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700303 * 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 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200309int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700310 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}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200321EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700322
323static int config_buf(struct usb_configuration *config,
324 enum usb_device_speed speed, void *buf, u8 type)
325{
326 struct usb_config_descriptor *c = buf;
327 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200328 int len;
David Brownell40982be2008-06-19 17:52:58 -0700329 struct usb_function *f;
330 int status;
331
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200332 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700333 /* write the config descriptor */
334 c = buf;
335 c->bLength = USB_DT_CONFIG_SIZE;
336 c->bDescriptorType = type;
337 /* wTotalLength is written later */
338 c->bNumInterfaces = config->next_interface_id;
339 c->bConfigurationValue = config->bConfigurationValue;
340 c->iConfiguration = config->iConfiguration;
341 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700342 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700343
344 /* There may be e.g. OTG descriptors */
345 if (config->descriptors) {
346 status = usb_descriptor_fillbuf(next, len,
347 config->descriptors);
348 if (status < 0)
349 return status;
350 len -= status;
351 next += status;
352 }
353
354 /* add each function's descriptors */
355 list_for_each_entry(f, &config->functions, list) {
356 struct usb_descriptor_header **descriptors;
357
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300358 switch (speed) {
359 case USB_SPEED_SUPER:
360 descriptors = f->ss_descriptors;
361 break;
362 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700363 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300364 break;
365 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200366 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300367 }
368
David Brownell40982be2008-06-19 17:52:58 -0700369 if (!descriptors)
370 continue;
371 status = usb_descriptor_fillbuf(next, len,
372 (const struct usb_descriptor_header **) descriptors);
373 if (status < 0)
374 return status;
375 len -= status;
376 next += status;
377 }
378
379 len = next - buf;
380 c->wTotalLength = cpu_to_le16(len);
381 return len;
382}
383
384static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
385{
386 struct usb_gadget *gadget = cdev->gadget;
387 struct usb_configuration *c;
388 u8 type = w_value >> 8;
389 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
390
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300391 if (gadget->speed == USB_SPEED_SUPER)
392 speed = gadget->speed;
393 else if (gadget_is_dualspeed(gadget)) {
394 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700395 if (gadget->speed == USB_SPEED_HIGH)
396 hs = 1;
397 if (type == USB_DT_OTHER_SPEED_CONFIG)
398 hs = !hs;
399 if (hs)
400 speed = USB_SPEED_HIGH;
401
402 }
403
404 /* This is a lookup by config *INDEX* */
405 w_value &= 0xff;
406 list_for_each_entry(c, &cdev->configs, list) {
407 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300408 switch (speed) {
409 case USB_SPEED_SUPER:
410 if (!c->superspeed)
411 continue;
412 break;
413 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700414 if (!c->highspeed)
415 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300416 break;
417 default:
David Brownell40982be2008-06-19 17:52:58 -0700418 if (!c->fullspeed)
419 continue;
420 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300421
David Brownell40982be2008-06-19 17:52:58 -0700422 if (w_value == 0)
423 return config_buf(c, speed, cdev->req->buf, type);
424 w_value--;
425 }
426 return -EINVAL;
427}
428
429static int count_configs(struct usb_composite_dev *cdev, unsigned type)
430{
431 struct usb_gadget *gadget = cdev->gadget;
432 struct usb_configuration *c;
433 unsigned count = 0;
434 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300435 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700436
437 if (gadget_is_dualspeed(gadget)) {
438 if (gadget->speed == USB_SPEED_HIGH)
439 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300440 if (gadget->speed == USB_SPEED_SUPER)
441 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700442 if (type == USB_DT_DEVICE_QUALIFIER)
443 hs = !hs;
444 }
445 list_for_each_entry(c, &cdev->configs, list) {
446 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300447 if (ss) {
448 if (!c->superspeed)
449 continue;
450 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700451 if (!c->highspeed)
452 continue;
453 } else {
454 if (!c->fullspeed)
455 continue;
456 }
457 count++;
458 }
459 return count;
460}
461
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300462/**
463 * bos_desc() - prepares the BOS descriptor.
464 * @cdev: pointer to usb_composite device to generate the bos
465 * descriptor for
466 *
467 * This function generates the BOS (Binary Device Object)
468 * descriptor and its device capabilities descriptors. The BOS
469 * descriptor should be supported by a SuperSpeed device.
470 */
471static int bos_desc(struct usb_composite_dev *cdev)
472{
473 struct usb_ext_cap_descriptor *usb_ext;
474 struct usb_ss_cap_descriptor *ss_cap;
475 struct usb_dcd_config_params dcd_config_params;
476 struct usb_bos_descriptor *bos = cdev->req->buf;
477
478 bos->bLength = USB_DT_BOS_SIZE;
479 bos->bDescriptorType = USB_DT_BOS;
480
481 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
482 bos->bNumDeviceCaps = 0;
483
484 /*
485 * A SuperSpeed device shall include the USB2.0 extension descriptor
486 * and shall support LPM when operating in USB2.0 HS mode.
487 */
488 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
489 bos->bNumDeviceCaps++;
490 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
491 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
492 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
493 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
494 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
495
496 /*
497 * The Superspeed USB Capability descriptor shall be implemented by all
498 * SuperSpeed devices.
499 */
500 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
501 bos->bNumDeviceCaps++;
502 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
503 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
504 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
505 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
506 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
507 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
508 USB_FULL_SPEED_OPERATION |
509 USB_HIGH_SPEED_OPERATION |
510 USB_5GBPS_OPERATION);
511 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
512
513 /* Get Controller configuration */
514 if (cdev->gadget->ops->get_config_params)
515 cdev->gadget->ops->get_config_params(&dcd_config_params);
516 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300517 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300518 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300519 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300520 }
521 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
522 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
523
524 return le16_to_cpu(bos->wTotalLength);
525}
526
David Brownell40982be2008-06-19 17:52:58 -0700527static void device_qual(struct usb_composite_dev *cdev)
528{
529 struct usb_qualifier_descriptor *qual = cdev->req->buf;
530
531 qual->bLength = sizeof(*qual);
532 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
533 /* POLICY: same bcdUSB and device type info at both speeds */
534 qual->bcdUSB = cdev->desc.bcdUSB;
535 qual->bDeviceClass = cdev->desc.bDeviceClass;
536 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
537 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
538 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200539 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700540 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700541 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700542}
543
544/*-------------------------------------------------------------------------*/
545
546static void reset_config(struct usb_composite_dev *cdev)
547{
548 struct usb_function *f;
549
550 DBG(cdev, "reset config\n");
551
552 list_for_each_entry(f, &cdev->config->functions, list) {
553 if (f->disable)
554 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200555
556 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700557 }
558 cdev->config = NULL;
559}
560
561static int set_config(struct usb_composite_dev *cdev,
562 const struct usb_ctrlrequest *ctrl, unsigned number)
563{
564 struct usb_gadget *gadget = cdev->gadget;
565 struct usb_configuration *c = NULL;
566 int result = -EINVAL;
567 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
568 int tmp;
569
David Brownell40982be2008-06-19 17:52:58 -0700570 if (number) {
571 list_for_each_entry(c, &cdev->configs, list) {
572 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300573 /*
574 * We disable the FDs of the previous
575 * configuration only if the new configuration
576 * is a valid one
577 */
578 if (cdev->config)
579 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700580 result = 0;
581 break;
582 }
583 }
584 if (result < 0)
585 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300586 } else { /* Zero configuration value - need to reset the config */
587 if (cdev->config)
588 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700589 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300590 }
David Brownell40982be2008-06-19 17:52:58 -0700591
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200592 INFO(cdev, "%s config #%d: %s\n",
593 usb_speed_string(gadget->speed),
594 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700595
596 if (!c)
597 goto done;
598
599 cdev->config = c;
600
601 /* Initialize all interfaces by setting them to altsetting zero. */
602 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
603 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200604 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700605
606 if (!f)
607 break;
608
Laurent Pinchart52426582009-10-21 00:03:38 +0200609 /*
610 * Record which endpoints are used by the function. This is used
611 * to dispatch control requests targeted at that endpoint to the
612 * function's setup callback instead of the current
613 * configuration's setup callback.
614 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300615 switch (gadget->speed) {
616 case USB_SPEED_SUPER:
617 descriptors = f->ss_descriptors;
618 break;
619 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200620 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300621 break;
622 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200623 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300624 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200625
626 for (; *descriptors; ++descriptors) {
627 struct usb_endpoint_descriptor *ep;
628 int addr;
629
630 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
631 continue;
632
633 ep = (struct usb_endpoint_descriptor *)*descriptors;
634 addr = ((ep->bEndpointAddress & 0x80) >> 3)
635 | (ep->bEndpointAddress & 0x0f);
636 set_bit(addr, f->endpoints);
637 }
638
David Brownell40982be2008-06-19 17:52:58 -0700639 result = f->set_alt(f, tmp, 0);
640 if (result < 0) {
641 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
642 tmp, f->name, f, result);
643
644 reset_config(cdev);
645 goto done;
646 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300647
648 if (result == USB_GADGET_DELAYED_STATUS) {
649 DBG(cdev,
650 "%s: interface %d (%s) requested delayed status\n",
651 __func__, tmp, f->name);
652 cdev->delayed_status++;
653 DBG(cdev, "delayed_status count %d\n",
654 cdev->delayed_status);
655 }
David Brownell40982be2008-06-19 17:52:58 -0700656 }
657
658 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700659 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700660done:
661 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300662 if (result >= 0 && cdev->delayed_status)
663 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700664 return result;
665}
666
667/**
668 * usb_add_config() - add a configuration to a device.
669 * @cdev: wraps the USB gadget
670 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200671 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700672 * Context: single threaded during gadget setup
673 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200674 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700675 * add each of the configurations it supports, using this routine.
676 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200677 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700678 * is zero for success else a negative errno value. Binding configurations
679 * assigns global resources including string IDs, and per-configuration
680 * resources such as interface IDs and endpoints.
681 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200682int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200683 struct usb_configuration *config,
684 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700685{
686 int status = -EINVAL;
687 struct usb_configuration *c;
688
689 DBG(cdev, "adding config #%u '%s'/%p\n",
690 config->bConfigurationValue,
691 config->label, config);
692
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200693 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700694 goto done;
695
696 /* Prevent duplicate configuration identifiers */
697 list_for_each_entry(c, &cdev->configs, list) {
698 if (c->bConfigurationValue == config->bConfigurationValue) {
699 status = -EBUSY;
700 goto done;
701 }
702 }
703
704 config->cdev = cdev;
705 list_add_tail(&config->list, &cdev->configs);
706
707 INIT_LIST_HEAD(&config->functions);
708 config->next_interface_id = 0;
Benoit Goby02e81612012-05-10 10:07:58 +0200709 memset(config->interface, 0, sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700710
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200711 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700712 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900713 while (!list_empty(&config->functions)) {
714 struct usb_function *f;
715
716 f = list_first_entry(&config->functions,
717 struct usb_function, list);
718 list_del(&f->list);
719 if (f->unbind) {
720 DBG(cdev, "unbind function '%s'/%p\n",
721 f->name, f);
722 f->unbind(config, f);
723 /* may free memory for "f" */
724 }
725 }
David Brownell40982be2008-06-19 17:52:58 -0700726 list_del(&config->list);
727 config->cdev = NULL;
728 } else {
729 unsigned i;
730
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300731 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700732 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300733 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700734 config->highspeed ? " high" : "",
735 config->fullspeed
736 ? (gadget_is_dualspeed(cdev->gadget)
737 ? " full"
738 : " full/low")
739 : "");
740
741 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
742 struct usb_function *f = config->interface[i];
743
744 if (!f)
745 continue;
746 DBG(cdev, " interface %d = %s/%p\n",
747 i, f->name, f);
748 }
749 }
750
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200751 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700752 * ep->driver_data as needed.
753 */
754 usb_ep_autoconfig_reset(cdev->gadget);
755
756done:
757 if (status)
758 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
759 config->bConfigurationValue, status);
760 return status;
761}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200762EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700763
Benoit Goby51cce6f2012-05-10 10:07:57 +0200764static void remove_config(struct usb_composite_dev *cdev,
765 struct usb_configuration *config)
766{
767 while (!list_empty(&config->functions)) {
768 struct usb_function *f;
769
770 f = list_first_entry(&config->functions,
771 struct usb_function, list);
772 list_del(&f->list);
773 if (f->unbind) {
774 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
775 f->unbind(config, f);
776 /* may free memory for "f" */
777 }
778 }
779 list_del(&config->list);
780 if (config->unbind) {
781 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
782 config->unbind(config);
783 /* may free memory for "c" */
784 }
785}
786
787/**
788 * usb_remove_config() - remove a configuration from a device.
789 * @cdev: wraps the USB gadget
790 * @config: the configuration
791 *
792 * Drivers must call usb_gadget_disconnect before calling this function
793 * to disconnect the device from the host and make sure the host will not
794 * try to enumerate the device while we are changing the config list.
795 */
796void usb_remove_config(struct usb_composite_dev *cdev,
797 struct usb_configuration *config)
798{
799 unsigned long flags;
800
801 spin_lock_irqsave(&cdev->lock, flags);
802
803 if (cdev->config == config)
804 reset_config(cdev);
805
806 spin_unlock_irqrestore(&cdev->lock, flags);
807
808 remove_config(cdev, config);
809}
810
David Brownell40982be2008-06-19 17:52:58 -0700811/*-------------------------------------------------------------------------*/
812
813/* We support strings in multiple languages ... string descriptor zero
814 * says which languages are supported. The typical case will be that
815 * only one language (probably English) is used, with I18N handled on
816 * the host side.
817 */
818
819static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
820{
821 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300822 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700823 __le16 *tmp;
824
825 while (*sp) {
826 s = *sp;
827 language = cpu_to_le16(s->language);
828 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
829 if (*tmp == language)
830 goto repeat;
831 }
832 *tmp++ = language;
833repeat:
834 sp++;
835 }
836}
837
838static int lookup_string(
839 struct usb_gadget_strings **sp,
840 void *buf,
841 u16 language,
842 int id
843)
844{
845 struct usb_gadget_strings *s;
846 int value;
847
848 while (*sp) {
849 s = *sp++;
850 if (s->language != language)
851 continue;
852 value = usb_gadget_get_string(s, id, buf);
853 if (value > 0)
854 return value;
855 }
856 return -EINVAL;
857}
858
859static int get_string(struct usb_composite_dev *cdev,
860 void *buf, u16 language, int id)
861{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200862 struct usb_composite_driver *composite = cdev->driver;
David Brownell40982be2008-06-19 17:52:58 -0700863 struct usb_configuration *c;
864 struct usb_function *f;
865 int len;
866
867 /* Yes, not only is USB's I18N support probably more than most
868 * folk will ever care about ... also, it's all supported here.
869 * (Except for UTF8 support for Unicode's "Astral Planes".)
870 */
871
872 /* 0 == report all available language codes */
873 if (id == 0) {
874 struct usb_string_descriptor *s = buf;
875 struct usb_gadget_strings **sp;
876
877 memset(s, 0, 256);
878 s->bDescriptorType = USB_DT_STRING;
879
880 sp = composite->strings;
881 if (sp)
882 collect_langs(sp, s->wData);
883
884 list_for_each_entry(c, &cdev->configs, list) {
885 sp = c->strings;
886 if (sp)
887 collect_langs(sp, s->wData);
888
889 list_for_each_entry(f, &c->functions, list) {
890 sp = f->strings;
891 if (sp)
892 collect_langs(sp, s->wData);
893 }
894 }
895
Roel Kluin417b57b2009-08-06 16:09:51 -0700896 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700897 continue;
898 if (!len)
899 return -EINVAL;
900
901 s->bLength = 2 * (len + 1);
902 return s->bLength;
903 }
904
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200905 /* String IDs are device-scoped, so we look up each string
906 * table we're told about. These lookups are infrequent;
907 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700908 */
909 if (composite->strings) {
910 len = lookup_string(composite->strings, buf, language, id);
911 if (len > 0)
912 return len;
913 }
914 list_for_each_entry(c, &cdev->configs, list) {
915 if (c->strings) {
916 len = lookup_string(c->strings, buf, language, id);
917 if (len > 0)
918 return len;
919 }
920 list_for_each_entry(f, &c->functions, list) {
921 if (!f->strings)
922 continue;
923 len = lookup_string(f->strings, buf, language, id);
924 if (len > 0)
925 return len;
926 }
927 }
928 return -EINVAL;
929}
930
931/**
932 * usb_string_id() - allocate an unused string ID
933 * @cdev: the device whose string descriptor IDs are being allocated
934 * Context: single threaded during gadget setup
935 *
936 * @usb_string_id() is called from bind() callbacks to allocate
937 * string IDs. Drivers for functions, configurations, or gadgets will
938 * then store that ID in the appropriate descriptors and string table.
939 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200940 * All string identifier should be allocated using this,
941 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
942 * that for example different functions don't wrongly assign different
943 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700944 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200945int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700946{
947 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200948 /* string id 0 is reserved by USB spec for list of
949 * supported languages */
950 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700951 cdev->next_string_id++;
952 return cdev->next_string_id;
953 }
954 return -ENODEV;
955}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200956EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -0700957
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200958/**
959 * usb_string_ids() - allocate unused string IDs in batch
960 * @cdev: the device whose string descriptor IDs are being allocated
961 * @str: an array of usb_string objects to assign numbers to
962 * Context: single threaded during gadget setup
963 *
964 * @usb_string_ids() is called from bind() callbacks to allocate
965 * string IDs. Drivers for functions, configurations, or gadgets will
966 * then copy IDs from the string table to the appropriate descriptors
967 * and string table for other languages.
968 *
969 * All string identifier should be allocated using this,
970 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
971 * example different functions don't wrongly assign different meanings
972 * to the same identifier.
973 */
974int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
975{
976 int next = cdev->next_string_id;
977
978 for (; str->s; ++str) {
979 if (unlikely(next >= 254))
980 return -ENODEV;
981 str->id = ++next;
982 }
983
984 cdev->next_string_id = next;
985
986 return 0;
987}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200988EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200989
990/**
991 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700992 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200993 * @n: number of string IDs to allocate
994 * Context: single threaded during gadget setup
995 *
996 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700997 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200998 * is, returns last requested ID which is now very useful information.
999 *
1000 * @usb_string_ids_n() is called from bind() callbacks to allocate
1001 * string IDs. Drivers for functions, configurations, or gadgets will
1002 * then store that ID in the appropriate descriptors and string table.
1003 *
1004 * All string identifier should be allocated using this,
1005 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1006 * example different functions don't wrongly assign different meanings
1007 * to the same identifier.
1008 */
1009int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1010{
1011 unsigned next = c->next_string_id;
1012 if (unlikely(n > 254 || (unsigned)next + n > 254))
1013 return -ENODEV;
1014 c->next_string_id += n;
1015 return next + 1;
1016}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001017EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001018
David Brownell40982be2008-06-19 17:52:58 -07001019/*-------------------------------------------------------------------------*/
1020
1021static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1022{
1023 if (req->status || req->actual != req->length)
1024 DBG((struct usb_composite_dev *) ep->driver_data,
1025 "setup complete --> %d, %d/%d\n",
1026 req->status, req->actual, req->length);
1027}
1028
1029/*
1030 * The setup() callback implements all the ep0 functionality that's
1031 * not handled lower down, in hardware or the hardware driver(like
1032 * device and endpoint feature flags, and their status). It's all
1033 * housekeeping for the gadget function we're implementing. Most of
1034 * the work is in config and function specific setup.
1035 */
1036static int
1037composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1038{
1039 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1040 struct usb_request *req = cdev->req;
1041 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001042 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001043 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001044 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001045 u16 w_value = le16_to_cpu(ctrl->wValue);
1046 u16 w_length = le16_to_cpu(ctrl->wLength);
1047 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001048 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001049
1050 /* partial re-init of the response message; the function or the
1051 * gadget might need to intercept e.g. a control-OUT completion
1052 * when we delegate to it.
1053 */
1054 req->zero = 0;
1055 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301056 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001057 gadget->ep0->driver_data = cdev;
1058
1059 switch (ctrl->bRequest) {
1060
1061 /* we handle all standard USB descriptors */
1062 case USB_REQ_GET_DESCRIPTOR:
1063 if (ctrl->bRequestType != USB_DIR_IN)
1064 goto unknown;
1065 switch (w_value >> 8) {
1066
1067 case USB_DT_DEVICE:
1068 cdev->desc.bNumConfigurations =
1069 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001070 cdev->desc.bMaxPacketSize0 =
1071 cdev->gadget->ep0->maxpacket;
1072 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001073 if (gadget->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001074 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001075 cdev->desc.bMaxPacketSize0 = 9;
1076 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001077 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001078 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001079 }
1080
David Brownell40982be2008-06-19 17:52:58 -07001081 value = min(w_length, (u16) sizeof cdev->desc);
1082 memcpy(req->buf, &cdev->desc, value);
1083 break;
1084 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001085 if (!gadget_is_dualspeed(gadget) ||
1086 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001087 break;
1088 device_qual(cdev);
1089 value = min_t(int, w_length,
1090 sizeof(struct usb_qualifier_descriptor));
1091 break;
1092 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001093 if (!gadget_is_dualspeed(gadget) ||
1094 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001095 break;
1096 /* FALLTHROUGH */
1097 case USB_DT_CONFIG:
1098 value = config_desc(cdev, w_value);
1099 if (value >= 0)
1100 value = min(w_length, (u16) value);
1101 break;
1102 case USB_DT_STRING:
1103 value = get_string(cdev, req->buf,
1104 w_index, w_value & 0xff);
1105 if (value >= 0)
1106 value = min(w_length, (u16) value);
1107 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001108 case USB_DT_BOS:
1109 if (gadget_is_superspeed(gadget)) {
1110 value = bos_desc(cdev);
1111 value = min(w_length, (u16) value);
1112 }
1113 break;
David Brownell40982be2008-06-19 17:52:58 -07001114 }
1115 break;
1116
1117 /* any number of configs can work */
1118 case USB_REQ_SET_CONFIGURATION:
1119 if (ctrl->bRequestType != 0)
1120 goto unknown;
1121 if (gadget_is_otg(gadget)) {
1122 if (gadget->a_hnp_support)
1123 DBG(cdev, "HNP available\n");
1124 else if (gadget->a_alt_hnp_support)
1125 DBG(cdev, "HNP on another port\n");
1126 else
1127 VDBG(cdev, "HNP inactive\n");
1128 }
1129 spin_lock(&cdev->lock);
1130 value = set_config(cdev, ctrl, w_value);
1131 spin_unlock(&cdev->lock);
1132 break;
1133 case USB_REQ_GET_CONFIGURATION:
1134 if (ctrl->bRequestType != USB_DIR_IN)
1135 goto unknown;
1136 if (cdev->config)
1137 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1138 else
1139 *(u8 *)req->buf = 0;
1140 value = min(w_length, (u16) 1);
1141 break;
1142
1143 /* function drivers must handle get/set altsetting; if there's
1144 * no get() method, we know only altsetting zero works.
1145 */
1146 case USB_REQ_SET_INTERFACE:
1147 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1148 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001149 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001150 break;
Bryan Wu08889512009-01-08 00:21:19 +08001151 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001152 if (!f)
1153 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001154 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001155 break;
1156 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001157 if (value == USB_GADGET_DELAYED_STATUS) {
1158 DBG(cdev,
1159 "%s: interface %d (%s) requested delayed status\n",
1160 __func__, intf, f->name);
1161 cdev->delayed_status++;
1162 DBG(cdev, "delayed_status count %d\n",
1163 cdev->delayed_status);
1164 }
David Brownell40982be2008-06-19 17:52:58 -07001165 break;
1166 case USB_REQ_GET_INTERFACE:
1167 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1168 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001169 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001170 break;
Bryan Wu08889512009-01-08 00:21:19 +08001171 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001172 if (!f)
1173 break;
1174 /* lots of interfaces only need altsetting zero... */
1175 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1176 if (value < 0)
1177 break;
1178 *((u8 *)req->buf) = value;
1179 value = min(w_length, (u16) 1);
1180 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001181
1182 /*
1183 * USB 3.0 additions:
1184 * Function driver should handle get_status request. If such cb
1185 * wasn't supplied we respond with default value = 0
1186 * Note: function driver should supply such cb only for the first
1187 * interface of the function
1188 */
1189 case USB_REQ_GET_STATUS:
1190 if (!gadget_is_superspeed(gadget))
1191 goto unknown;
1192 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1193 goto unknown;
1194 value = 2; /* This is the length of the get_status reply */
1195 put_unaligned_le16(0, req->buf);
1196 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1197 break;
1198 f = cdev->config->interface[intf];
1199 if (!f)
1200 break;
1201 status = f->get_status ? f->get_status(f) : 0;
1202 if (status < 0)
1203 break;
1204 put_unaligned_le16(status & 0x0000ffff, req->buf);
1205 break;
1206 /*
1207 * Function drivers should handle SetFeature/ClearFeature
1208 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1209 * only for the first interface of the function
1210 */
1211 case USB_REQ_CLEAR_FEATURE:
1212 case USB_REQ_SET_FEATURE:
1213 if (!gadget_is_superspeed(gadget))
1214 goto unknown;
1215 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1216 goto unknown;
1217 switch (w_value) {
1218 case USB_INTRF_FUNC_SUSPEND:
1219 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1220 break;
1221 f = cdev->config->interface[intf];
1222 if (!f)
1223 break;
1224 value = 0;
1225 if (f->func_suspend)
1226 value = f->func_suspend(f, w_index >> 8);
1227 if (value < 0) {
1228 ERROR(cdev,
1229 "func_suspend() returned error %d\n",
1230 value);
1231 value = 0;
1232 }
1233 break;
1234 }
1235 break;
David Brownell40982be2008-06-19 17:52:58 -07001236 default:
1237unknown:
1238 VDBG(cdev,
1239 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1240 ctrl->bRequestType, ctrl->bRequest,
1241 w_value, w_index, w_length);
1242
Laurent Pinchart52426582009-10-21 00:03:38 +02001243 /* functions always handle their interfaces and endpoints...
1244 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001245 * configuration code.
1246 *
1247 * REVISIT it could make sense to let the composite device
1248 * take such requests too, if that's ever needed: to work
1249 * in config 0, etc.
1250 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001251 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1252 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001253 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301254 break;
1255 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001256 break;
1257
1258 case USB_RECIP_ENDPOINT:
1259 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1260 list_for_each_entry(f, &cdev->config->functions, list) {
1261 if (test_bit(endp, f->endpoints))
1262 break;
1263 }
1264 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001265 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001266 break;
David Brownell40982be2008-06-19 17:52:58 -07001267 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001268
1269 if (f && f->setup)
1270 value = f->setup(f, ctrl);
1271 else {
David Brownell40982be2008-06-19 17:52:58 -07001272 struct usb_configuration *c;
1273
1274 c = cdev->config;
1275 if (c && c->setup)
1276 value = c->setup(c, ctrl);
1277 }
1278
1279 goto done;
1280 }
1281
1282 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001283 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001284 req->length = value;
1285 req->zero = value < w_length;
1286 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1287 if (value < 0) {
1288 DBG(cdev, "ep_queue --> %d\n", value);
1289 req->status = 0;
1290 composite_setup_complete(gadget->ep0, req);
1291 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001292 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1293 WARN(cdev,
1294 "%s: Delayed status not supported for w_length != 0",
1295 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001296 }
1297
1298done:
1299 /* device either stalls (value < 0) or reports success */
1300 return value;
1301}
1302
1303static void composite_disconnect(struct usb_gadget *gadget)
1304{
1305 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1306 unsigned long flags;
1307
1308 /* REVISIT: should we have config and device level
1309 * disconnect callbacks?
1310 */
1311 spin_lock_irqsave(&cdev->lock, flags);
1312 if (cdev->config)
1313 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001314 if (cdev->driver->disconnect)
1315 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001316 spin_unlock_irqrestore(&cdev->lock, flags);
1317}
1318
1319/*-------------------------------------------------------------------------*/
1320
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001321static ssize_t composite_show_suspended(struct device *dev,
1322 struct device_attribute *attr,
1323 char *buf)
1324{
1325 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1326 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1327
1328 return sprintf(buf, "%d\n", cdev->suspended);
1329}
1330
1331static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1332
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001333static void
David Brownell40982be2008-06-19 17:52:58 -07001334composite_unbind(struct usb_gadget *gadget)
1335{
1336 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1337
1338 /* composite_disconnect() must already have been called
1339 * by the underlying peripheral controller driver!
1340 * so there's no i/o concurrency that could affect the
1341 * state protected by cdev->lock.
1342 */
1343 WARN_ON(cdev->config);
1344
1345 while (!list_empty(&cdev->configs)) {
1346 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001347 c = list_first_entry(&cdev->configs,
1348 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001349 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001350 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001351 if (cdev->driver->unbind)
1352 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001353
1354 if (cdev->req) {
1355 kfree(cdev->req->buf);
1356 usb_ep_free_request(gadget->ep0, cdev->req);
1357 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301358 device_remove_file(&gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001359 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001360 kfree(cdev);
1361 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001362}
1363
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001364static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1365 const struct usb_device_descriptor *old)
1366{
1367 __le16 idVendor;
1368 __le16 idProduct;
1369 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001370 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001371 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001372 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001373
1374 /*
1375 * these variables may have been set in
1376 * usb_composite_overwrite_options()
1377 */
1378 idVendor = new->idVendor;
1379 idProduct = new->idProduct;
1380 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001381 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001382 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001383 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001384
1385 *new = *old;
1386 if (idVendor)
1387 new->idVendor = idVendor;
1388 if (idProduct)
1389 new->idProduct = idProduct;
1390 if (bcdDevice)
1391 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02001392 else
1393 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001394 if (iSerialNumber)
1395 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001396 if (iManufacturer)
1397 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001398 if (iProduct)
1399 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001400}
1401
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001402static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1403{
1404 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1405}
1406
1407static int composite_bind(struct usb_gadget *gadget,
1408 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07001409{
1410 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001411 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07001412 int status = -ENOMEM;
1413
1414 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1415 if (!cdev)
1416 return status;
1417
1418 spin_lock_init(&cdev->lock);
1419 cdev->gadget = gadget;
1420 set_gadget_data(gadget, cdev);
1421 INIT_LIST_HEAD(&cdev->configs);
1422
1423 /* preallocate control response and buffer */
1424 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1425 if (!cdev->req)
1426 goto fail;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +02001427 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
David Brownell40982be2008-06-19 17:52:58 -07001428 if (!cdev->req->buf)
1429 goto fail;
1430 cdev->req->complete = composite_setup_complete;
1431 gadget->ep0->driver_data = cdev;
1432
David Brownell40982be2008-06-19 17:52:58 -07001433 cdev->driver = composite;
1434
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301435 /*
1436 * As per USB compliance update, a device that is actively drawing
1437 * more than 100mA from USB must report itself as bus-powered in
1438 * the GetStatus(DEVICE) call.
1439 */
1440 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1441 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001442
1443 /* interface and string IDs start at zero via kzalloc.
1444 * we force endpoints to start unassigned; few controller
1445 * drivers will zero ep->driver_data.
1446 */
1447 usb_ep_autoconfig_reset(cdev->gadget);
1448
1449 /* composite gadget needs to assign strings for whole device (like
1450 * serial number), register function drivers, potentially update
1451 * power state and consumption, etc
1452 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02001453 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001454 if (status < 0)
1455 goto fail;
1456
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001457 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001458
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001459 /* has userspace failed to provide a serial number? */
1460 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1461 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1462
1463 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001464 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1465 if (status)
1466 goto fail;
1467
David Brownell40982be2008-06-19 17:52:58 -07001468 INFO(cdev, "%s ready\n", composite->name);
1469 return 0;
1470
1471fail:
1472 composite_unbind(gadget);
1473 return status;
1474}
1475
1476/*-------------------------------------------------------------------------*/
1477
1478static void
1479composite_suspend(struct usb_gadget *gadget)
1480{
1481 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1482 struct usb_function *f;
1483
David Brownell89429392009-03-19 14:14:17 -07001484 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001485 * suspend/resume callbacks?
1486 */
1487 DBG(cdev, "suspend\n");
1488 if (cdev->config) {
1489 list_for_each_entry(f, &cdev->config->functions, list) {
1490 if (f->suspend)
1491 f->suspend(f);
1492 }
1493 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001494 if (cdev->driver->suspend)
1495 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001496
1497 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001498
1499 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001500}
1501
1502static void
1503composite_resume(struct usb_gadget *gadget)
1504{
1505 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1506 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001507 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001508
David Brownell89429392009-03-19 14:14:17 -07001509 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001510 * suspend/resume callbacks?
1511 */
1512 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001513 if (cdev->driver->resume)
1514 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001515 if (cdev->config) {
1516 list_for_each_entry(f, &cdev->config->functions, list) {
1517 if (f->resume)
1518 f->resume(f);
1519 }
Hao Wub23f2f92010-11-29 15:17:03 +08001520
1521 maxpower = cdev->config->bMaxPower;
1522
1523 usb_gadget_vbus_draw(gadget, maxpower ?
1524 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001525 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001526
1527 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001528}
1529
1530/*-------------------------------------------------------------------------*/
1531
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001532static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001533 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001534 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001535
1536 .setup = composite_setup,
1537 .disconnect = composite_disconnect,
1538
1539 .suspend = composite_suspend,
1540 .resume = composite_resume,
1541
1542 .driver = {
1543 .owner = THIS_MODULE,
1544 },
1545};
1546
1547/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001548 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001549 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001550 * @bind: the callback used to allocate resources that are shared across the
1551 * whole device, such as string IDs, and add its configurations using
1552 * @usb_add_config(). This may fail by returning a negative errno
1553 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001554 * Context: single threaded during gadget setup
1555 *
1556 * This function is used to register drivers using the composite driver
1557 * framework. The return value is zero, or a negative errno value.
1558 * Those values normally come from the driver's @bind method, which does
1559 * all the work of setting up the driver to match the hardware.
1560 *
1561 * On successful return, the gadget is ready to respond to requests from
1562 * the host, unless one of its components invokes usb_gadget_disconnect()
1563 * while it was binding. That would usually be done in order to wait for
1564 * some userspace participation.
1565 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001566int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001567{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001568 struct usb_gadget_driver *gadget_driver;
1569
1570 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07001571 return -EINVAL;
1572
1573 if (!driver->name)
1574 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07001575
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001576 driver->gadget_driver = composite_driver_template;
1577 gadget_driver = &driver->gadget_driver;
1578
1579 gadget_driver->function = (char *) driver->name;
1580 gadget_driver->driver.name = driver->name;
1581 gadget_driver->max_speed = driver->max_speed;
1582
1583 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001584}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001585EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07001586
1587/**
1588 * usb_composite_unregister() - unregister a composite driver
1589 * @driver: the driver to unregister
1590 *
1591 * This function is used to unregister drivers using the composite
1592 * driver framework.
1593 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001594void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001595{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001596 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001597}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001598EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001599
1600/**
1601 * usb_composite_setup_continue() - Continue with the control transfer
1602 * @cdev: the composite device who's control transfer was kept waiting
1603 *
1604 * This function must be called by the USB function driver to continue
1605 * with the control transfer's data/status stage in case it had requested to
1606 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1607 * can request the composite framework to delay the setup request's data/status
1608 * stages by returning USB_GADGET_DELAYED_STATUS.
1609 */
1610void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1611{
1612 int value;
1613 struct usb_request *req = cdev->req;
1614 unsigned long flags;
1615
1616 DBG(cdev, "%s\n", __func__);
1617 spin_lock_irqsave(&cdev->lock, flags);
1618
1619 if (cdev->delayed_status == 0) {
1620 WARN(cdev, "%s: Unexpected call\n", __func__);
1621
1622 } else if (--cdev->delayed_status == 0) {
1623 DBG(cdev, "%s: Completing delayed status\n", __func__);
1624 req->length = 0;
1625 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1626 if (value < 0) {
1627 DBG(cdev, "ep_queue --> %d\n", value);
1628 req->status = 0;
1629 composite_setup_complete(cdev->gadget->ep0, req);
1630 }
1631 }
1632
1633 spin_unlock_irqrestore(&cdev->lock, flags);
1634}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001635EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001636
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001637static char *composite_default_mfr(struct usb_gadget *gadget)
1638{
1639 char *mfr;
1640 int len;
1641
1642 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
1643 init_utsname()->release, gadget->name);
1644 len++;
1645 mfr = kmalloc(len, GFP_KERNEL);
1646 if (!mfr)
1647 return NULL;
1648 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
1649 init_utsname()->release, gadget->name);
1650 return mfr;
1651}
1652
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001653void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1654 struct usb_composite_overwrite *covr)
1655{
1656 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001657 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1658 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001659
1660 if (covr->idVendor)
1661 desc->idVendor = cpu_to_le16(covr->idVendor);
1662
1663 if (covr->idProduct)
1664 desc->idProduct = cpu_to_le16(covr->idProduct);
1665
1666 if (covr->bcdDevice)
1667 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001668
1669 if (covr->serial_number) {
1670 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
1671 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
1672 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001673 if (covr->manufacturer) {
1674 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1675 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001676
1677 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
1678 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1679 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
1680 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001681 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001682
1683 if (covr->product) {
1684 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
1685 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
1686 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001687}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001688EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02001689
1690MODULE_LICENSE("GPL");
1691MODULE_AUTHOR("David Brownell");