blob: 4aa0e4652228a1de5b91137170b02a4415022e08 [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
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100323static 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
David Brownell40982be2008-06-19 17:52:58 -0700342static 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;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200347 int len;
David Brownell40982be2008-06-19 17:52:58 -0700348 struct usb_function *f;
349 int status;
350
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200351 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700352 /* 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;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100361 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700362
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
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300377 switch (speed) {
378 case USB_SPEED_SUPER:
379 descriptors = f->ss_descriptors;
380 break;
381 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700382 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300383 break;
384 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200385 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300386 }
387
David Brownell40982be2008-06-19 17:52:58 -0700388 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
403static 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
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300410 if (gadget->speed == USB_SPEED_SUPER)
411 speed = gadget->speed;
412 else if (gadget_is_dualspeed(gadget)) {
413 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700414 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 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300427 switch (speed) {
428 case USB_SPEED_SUPER:
429 if (!c->superspeed)
430 continue;
431 break;
432 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700433 if (!c->highspeed)
434 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300435 break;
436 default:
David Brownell40982be2008-06-19 17:52:58 -0700437 if (!c->fullspeed)
438 continue;
439 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300440
David Brownell40982be2008-06-19 17:52:58 -0700441 if (w_value == 0)
442 return config_buf(c, speed, cdev->req->buf, type);
443 w_value--;
444 }
445 return -EINVAL;
446}
447
448static 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;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300454 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700455
456 if (gadget_is_dualspeed(gadget)) {
457 if (gadget->speed == USB_SPEED_HIGH)
458 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300459 if (gadget->speed == USB_SPEED_SUPER)
460 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700461 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 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300466 if (ss) {
467 if (!c->superspeed)
468 continue;
469 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700470 if (!c->highspeed)
471 continue;
472 } else {
473 if (!c->fullspeed)
474 continue;
475 }
476 count++;
477 }
478 return count;
479}
480
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300481/**
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 */
490static 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 {
Felipe Balbi089b8372011-10-10 09:43:44 +0300536 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300537 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300538 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300539 }
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
David Brownell40982be2008-06-19 17:52:58 -0700546static 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 */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200558 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700559 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700560 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700561}
562
563/*-------------------------------------------------------------------------*/
564
565static 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);
Laurent Pinchart52426582009-10-21 00:03:38 +0200574
575 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700576 }
577 cdev->config = NULL;
578}
579
580static 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
David Brownell40982be2008-06-19 17:52:58 -0700589 if (number) {
590 list_for_each_entry(c, &cdev->configs, list) {
591 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300592 /*
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);
David Brownell40982be2008-06-19 17:52:58 -0700599 result = 0;
600 break;
601 }
602 }
603 if (result < 0)
604 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300605 } else { /* Zero configuration value - need to reset the config */
606 if (cdev->config)
607 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700608 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300609 }
David Brownell40982be2008-06-19 17:52:58 -0700610
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200611 INFO(cdev, "%s config #%d: %s\n",
612 usb_speed_string(gadget->speed),
613 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700614
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];
Laurent Pinchart52426582009-10-21 00:03:38 +0200623 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700624
625 if (!f)
626 break;
627
Laurent Pinchart52426582009-10-21 00:03:38 +0200628 /*
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 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300634 switch (gadget->speed) {
635 case USB_SPEED_SUPER:
636 descriptors = f->ss_descriptors;
637 break;
638 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200639 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300640 break;
641 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200642 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300643 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200644
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
David Brownell40982be2008-06-19 17:52:58 -0700658 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 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300666
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 }
David Brownell40982be2008-06-19 17:52:58 -0700675 }
676
677 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100678 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700679done:
680 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300681 if (result >= 0 && cdev->delayed_status)
682 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700683 return result;
684}
685
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100686int usb_add_config_only(struct usb_composite_dev *cdev,
687 struct usb_configuration *config)
688{
689 struct usb_configuration *c;
690
691 if (!config->bConfigurationValue)
692 return -EINVAL;
693
694 /* Prevent duplicate configuration identifiers */
695 list_for_each_entry(c, &cdev->configs, list) {
696 if (c->bConfigurationValue == config->bConfigurationValue)
697 return -EBUSY;
698 }
699
700 config->cdev = cdev;
701 list_add_tail(&config->list, &cdev->configs);
702
703 INIT_LIST_HEAD(&config->functions);
704 config->next_interface_id = 0;
705 memset(config->interface, 0, sizeof(config->interface));
706
707 return 0;
708}
709EXPORT_SYMBOL_GPL(usb_add_config_only);
710
David Brownell40982be2008-06-19 17:52:58 -0700711/**
712 * usb_add_config() - add a configuration to a device.
713 * @cdev: wraps the USB gadget
714 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200715 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700716 * Context: single threaded during gadget setup
717 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200718 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700719 * add each of the configurations it supports, using this routine.
720 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200721 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700722 * is zero for success else a negative errno value. Binding configurations
723 * assigns global resources including string IDs, and per-configuration
724 * resources such as interface IDs and endpoints.
725 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200726int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200727 struct usb_configuration *config,
728 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700729{
730 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100731
732 if (!bind)
733 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700734
735 DBG(cdev, "adding config #%u '%s'/%p\n",
736 config->bConfigurationValue,
737 config->label, config);
738
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100739 status = usb_add_config_only(cdev, config);
740 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700741 goto done;
742
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200743 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700744 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900745 while (!list_empty(&config->functions)) {
746 struct usb_function *f;
747
748 f = list_first_entry(&config->functions,
749 struct usb_function, list);
750 list_del(&f->list);
751 if (f->unbind) {
752 DBG(cdev, "unbind function '%s'/%p\n",
753 f->name, f);
754 f->unbind(config, f);
755 /* may free memory for "f" */
756 }
757 }
David Brownell40982be2008-06-19 17:52:58 -0700758 list_del(&config->list);
759 config->cdev = NULL;
760 } else {
761 unsigned i;
762
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300763 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700764 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300765 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700766 config->highspeed ? " high" : "",
767 config->fullspeed
768 ? (gadget_is_dualspeed(cdev->gadget)
769 ? " full"
770 : " full/low")
771 : "");
772
773 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
774 struct usb_function *f = config->interface[i];
775
776 if (!f)
777 continue;
778 DBG(cdev, " interface %d = %s/%p\n",
779 i, f->name, f);
780 }
781 }
782
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200783 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700784 * ep->driver_data as needed.
785 */
786 usb_ep_autoconfig_reset(cdev->gadget);
787
788done:
789 if (status)
790 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
791 config->bConfigurationValue, status);
792 return status;
793}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200794EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700795
Benoit Goby51cce6f2012-05-10 10:07:57 +0200796static void remove_config(struct usb_composite_dev *cdev,
797 struct usb_configuration *config)
798{
799 while (!list_empty(&config->functions)) {
800 struct usb_function *f;
801
802 f = list_first_entry(&config->functions,
803 struct usb_function, list);
804 list_del(&f->list);
805 if (f->unbind) {
806 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
807 f->unbind(config, f);
808 /* may free memory for "f" */
809 }
810 }
811 list_del(&config->list);
812 if (config->unbind) {
813 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
814 config->unbind(config);
815 /* may free memory for "c" */
816 }
817}
818
819/**
820 * usb_remove_config() - remove a configuration from a device.
821 * @cdev: wraps the USB gadget
822 * @config: the configuration
823 *
824 * Drivers must call usb_gadget_disconnect before calling this function
825 * to disconnect the device from the host and make sure the host will not
826 * try to enumerate the device while we are changing the config list.
827 */
828void usb_remove_config(struct usb_composite_dev *cdev,
829 struct usb_configuration *config)
830{
831 unsigned long flags;
832
833 spin_lock_irqsave(&cdev->lock, flags);
834
835 if (cdev->config == config)
836 reset_config(cdev);
837
838 spin_unlock_irqrestore(&cdev->lock, flags);
839
840 remove_config(cdev, config);
841}
842
David Brownell40982be2008-06-19 17:52:58 -0700843/*-------------------------------------------------------------------------*/
844
845/* We support strings in multiple languages ... string descriptor zero
846 * says which languages are supported. The typical case will be that
847 * only one language (probably English) is used, with I18N handled on
848 * the host side.
849 */
850
851static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
852{
853 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300854 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700855 __le16 *tmp;
856
857 while (*sp) {
858 s = *sp;
859 language = cpu_to_le16(s->language);
860 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
861 if (*tmp == language)
862 goto repeat;
863 }
864 *tmp++ = language;
865repeat:
866 sp++;
867 }
868}
869
870static int lookup_string(
871 struct usb_gadget_strings **sp,
872 void *buf,
873 u16 language,
874 int id
875)
876{
877 struct usb_gadget_strings *s;
878 int value;
879
880 while (*sp) {
881 s = *sp++;
882 if (s->language != language)
883 continue;
884 value = usb_gadget_get_string(s, id, buf);
885 if (value > 0)
886 return value;
887 }
888 return -EINVAL;
889}
890
891static int get_string(struct usb_composite_dev *cdev,
892 void *buf, u16 language, int id)
893{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200894 struct usb_composite_driver *composite = cdev->driver;
David Brownell40982be2008-06-19 17:52:58 -0700895 struct usb_configuration *c;
896 struct usb_function *f;
897 int len;
898
899 /* Yes, not only is USB's I18N support probably more than most
900 * folk will ever care about ... also, it's all supported here.
901 * (Except for UTF8 support for Unicode's "Astral Planes".)
902 */
903
904 /* 0 == report all available language codes */
905 if (id == 0) {
906 struct usb_string_descriptor *s = buf;
907 struct usb_gadget_strings **sp;
908
909 memset(s, 0, 256);
910 s->bDescriptorType = USB_DT_STRING;
911
912 sp = composite->strings;
913 if (sp)
914 collect_langs(sp, s->wData);
915
916 list_for_each_entry(c, &cdev->configs, list) {
917 sp = c->strings;
918 if (sp)
919 collect_langs(sp, s->wData);
920
921 list_for_each_entry(f, &c->functions, list) {
922 sp = f->strings;
923 if (sp)
924 collect_langs(sp, s->wData);
925 }
926 }
927
Roel Kluin417b57b2009-08-06 16:09:51 -0700928 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700929 continue;
930 if (!len)
931 return -EINVAL;
932
933 s->bLength = 2 * (len + 1);
934 return s->bLength;
935 }
936
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200937 /* String IDs are device-scoped, so we look up each string
938 * table we're told about. These lookups are infrequent;
939 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700940 */
941 if (composite->strings) {
942 len = lookup_string(composite->strings, buf, language, id);
943 if (len > 0)
944 return len;
945 }
946 list_for_each_entry(c, &cdev->configs, list) {
947 if (c->strings) {
948 len = lookup_string(c->strings, buf, language, id);
949 if (len > 0)
950 return len;
951 }
952 list_for_each_entry(f, &c->functions, list) {
953 if (!f->strings)
954 continue;
955 len = lookup_string(f->strings, buf, language, id);
956 if (len > 0)
957 return len;
958 }
959 }
960 return -EINVAL;
961}
962
963/**
964 * usb_string_id() - allocate an unused string ID
965 * @cdev: the device whose string descriptor IDs are being allocated
966 * Context: single threaded during gadget setup
967 *
968 * @usb_string_id() is called from bind() callbacks to allocate
969 * string IDs. Drivers for functions, configurations, or gadgets will
970 * then store that ID in the appropriate descriptors and string table.
971 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200972 * All string identifier should be allocated using this,
973 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
974 * that for example different functions don't wrongly assign different
975 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700976 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200977int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700978{
979 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200980 /* string id 0 is reserved by USB spec for list of
981 * supported languages */
982 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700983 cdev->next_string_id++;
984 return cdev->next_string_id;
985 }
986 return -ENODEV;
987}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200988EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -0700989
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200990/**
991 * usb_string_ids() - allocate unused string IDs in batch
992 * @cdev: the device whose string descriptor IDs are being allocated
993 * @str: an array of usb_string objects to assign numbers to
994 * Context: single threaded during gadget setup
995 *
996 * @usb_string_ids() is called from bind() callbacks to allocate
997 * string IDs. Drivers for functions, configurations, or gadgets will
998 * then copy IDs from the string table to the appropriate descriptors
999 * and string table for other languages.
1000 *
1001 * All string identifier should be allocated using this,
1002 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1003 * example different functions don't wrongly assign different meanings
1004 * to the same identifier.
1005 */
1006int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1007{
1008 int next = cdev->next_string_id;
1009
1010 for (; str->s; ++str) {
1011 if (unlikely(next >= 254))
1012 return -ENODEV;
1013 str->id = ++next;
1014 }
1015
1016 cdev->next_string_id = next;
1017
1018 return 0;
1019}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001020EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001021
1022/**
1023 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001024 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001025 * @n: number of string IDs to allocate
1026 * Context: single threaded during gadget setup
1027 *
1028 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001029 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001030 * is, returns last requested ID which is now very useful information.
1031 *
1032 * @usb_string_ids_n() is called from bind() callbacks to allocate
1033 * string IDs. Drivers for functions, configurations, or gadgets will
1034 * then store that ID in the appropriate descriptors and string table.
1035 *
1036 * All string identifier should be allocated using this,
1037 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1038 * example different functions don't wrongly assign different meanings
1039 * to the same identifier.
1040 */
1041int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1042{
1043 unsigned next = c->next_string_id;
1044 if (unlikely(n > 254 || (unsigned)next + n > 254))
1045 return -ENODEV;
1046 c->next_string_id += n;
1047 return next + 1;
1048}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001049EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001050
David Brownell40982be2008-06-19 17:52:58 -07001051/*-------------------------------------------------------------------------*/
1052
1053static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1054{
1055 if (req->status || req->actual != req->length)
1056 DBG((struct usb_composite_dev *) ep->driver_data,
1057 "setup complete --> %d, %d/%d\n",
1058 req->status, req->actual, req->length);
1059}
1060
1061/*
1062 * The setup() callback implements all the ep0 functionality that's
1063 * not handled lower down, in hardware or the hardware driver(like
1064 * device and endpoint feature flags, and their status). It's all
1065 * housekeeping for the gadget function we're implementing. Most of
1066 * the work is in config and function specific setup.
1067 */
1068static int
1069composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1070{
1071 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1072 struct usb_request *req = cdev->req;
1073 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001074 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001075 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001076 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001077 u16 w_value = le16_to_cpu(ctrl->wValue);
1078 u16 w_length = le16_to_cpu(ctrl->wLength);
1079 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001080 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001081
1082 /* partial re-init of the response message; the function or the
1083 * gadget might need to intercept e.g. a control-OUT completion
1084 * when we delegate to it.
1085 */
1086 req->zero = 0;
1087 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301088 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001089 gadget->ep0->driver_data = cdev;
1090
1091 switch (ctrl->bRequest) {
1092
1093 /* we handle all standard USB descriptors */
1094 case USB_REQ_GET_DESCRIPTOR:
1095 if (ctrl->bRequestType != USB_DIR_IN)
1096 goto unknown;
1097 switch (w_value >> 8) {
1098
1099 case USB_DT_DEVICE:
1100 cdev->desc.bNumConfigurations =
1101 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001102 cdev->desc.bMaxPacketSize0 =
1103 cdev->gadget->ep0->maxpacket;
1104 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001105 if (gadget->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001106 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001107 cdev->desc.bMaxPacketSize0 = 9;
1108 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001109 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001110 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001111 }
1112
David Brownell40982be2008-06-19 17:52:58 -07001113 value = min(w_length, (u16) sizeof cdev->desc);
1114 memcpy(req->buf, &cdev->desc, value);
1115 break;
1116 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001117 if (!gadget_is_dualspeed(gadget) ||
1118 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001119 break;
1120 device_qual(cdev);
1121 value = min_t(int, w_length,
1122 sizeof(struct usb_qualifier_descriptor));
1123 break;
1124 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001125 if (!gadget_is_dualspeed(gadget) ||
1126 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001127 break;
1128 /* FALLTHROUGH */
1129 case USB_DT_CONFIG:
1130 value = config_desc(cdev, w_value);
1131 if (value >= 0)
1132 value = min(w_length, (u16) value);
1133 break;
1134 case USB_DT_STRING:
1135 value = get_string(cdev, req->buf,
1136 w_index, w_value & 0xff);
1137 if (value >= 0)
1138 value = min(w_length, (u16) value);
1139 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001140 case USB_DT_BOS:
1141 if (gadget_is_superspeed(gadget)) {
1142 value = bos_desc(cdev);
1143 value = min(w_length, (u16) value);
1144 }
1145 break;
David Brownell40982be2008-06-19 17:52:58 -07001146 }
1147 break;
1148
1149 /* any number of configs can work */
1150 case USB_REQ_SET_CONFIGURATION:
1151 if (ctrl->bRequestType != 0)
1152 goto unknown;
1153 if (gadget_is_otg(gadget)) {
1154 if (gadget->a_hnp_support)
1155 DBG(cdev, "HNP available\n");
1156 else if (gadget->a_alt_hnp_support)
1157 DBG(cdev, "HNP on another port\n");
1158 else
1159 VDBG(cdev, "HNP inactive\n");
1160 }
1161 spin_lock(&cdev->lock);
1162 value = set_config(cdev, ctrl, w_value);
1163 spin_unlock(&cdev->lock);
1164 break;
1165 case USB_REQ_GET_CONFIGURATION:
1166 if (ctrl->bRequestType != USB_DIR_IN)
1167 goto unknown;
1168 if (cdev->config)
1169 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1170 else
1171 *(u8 *)req->buf = 0;
1172 value = min(w_length, (u16) 1);
1173 break;
1174
1175 /* function drivers must handle get/set altsetting; if there's
1176 * no get() method, we know only altsetting zero works.
1177 */
1178 case USB_REQ_SET_INTERFACE:
1179 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1180 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001181 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001182 break;
Bryan Wu08889512009-01-08 00:21:19 +08001183 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001184 if (!f)
1185 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001186 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001187 break;
1188 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001189 if (value == USB_GADGET_DELAYED_STATUS) {
1190 DBG(cdev,
1191 "%s: interface %d (%s) requested delayed status\n",
1192 __func__, intf, f->name);
1193 cdev->delayed_status++;
1194 DBG(cdev, "delayed_status count %d\n",
1195 cdev->delayed_status);
1196 }
David Brownell40982be2008-06-19 17:52:58 -07001197 break;
1198 case USB_REQ_GET_INTERFACE:
1199 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1200 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001201 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001202 break;
Bryan Wu08889512009-01-08 00:21:19 +08001203 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001204 if (!f)
1205 break;
1206 /* lots of interfaces only need altsetting zero... */
1207 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1208 if (value < 0)
1209 break;
1210 *((u8 *)req->buf) = value;
1211 value = min(w_length, (u16) 1);
1212 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001213
1214 /*
1215 * USB 3.0 additions:
1216 * Function driver should handle get_status request. If such cb
1217 * wasn't supplied we respond with default value = 0
1218 * Note: function driver should supply such cb only for the first
1219 * interface of the function
1220 */
1221 case USB_REQ_GET_STATUS:
1222 if (!gadget_is_superspeed(gadget))
1223 goto unknown;
1224 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1225 goto unknown;
1226 value = 2; /* This is the length of the get_status reply */
1227 put_unaligned_le16(0, req->buf);
1228 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1229 break;
1230 f = cdev->config->interface[intf];
1231 if (!f)
1232 break;
1233 status = f->get_status ? f->get_status(f) : 0;
1234 if (status < 0)
1235 break;
1236 put_unaligned_le16(status & 0x0000ffff, req->buf);
1237 break;
1238 /*
1239 * Function drivers should handle SetFeature/ClearFeature
1240 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1241 * only for the first interface of the function
1242 */
1243 case USB_REQ_CLEAR_FEATURE:
1244 case USB_REQ_SET_FEATURE:
1245 if (!gadget_is_superspeed(gadget))
1246 goto unknown;
1247 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1248 goto unknown;
1249 switch (w_value) {
1250 case USB_INTRF_FUNC_SUSPEND:
1251 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1252 break;
1253 f = cdev->config->interface[intf];
1254 if (!f)
1255 break;
1256 value = 0;
1257 if (f->func_suspend)
1258 value = f->func_suspend(f, w_index >> 8);
1259 if (value < 0) {
1260 ERROR(cdev,
1261 "func_suspend() returned error %d\n",
1262 value);
1263 value = 0;
1264 }
1265 break;
1266 }
1267 break;
David Brownell40982be2008-06-19 17:52:58 -07001268 default:
1269unknown:
1270 VDBG(cdev,
1271 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1272 ctrl->bRequestType, ctrl->bRequest,
1273 w_value, w_index, w_length);
1274
Laurent Pinchart52426582009-10-21 00:03:38 +02001275 /* functions always handle their interfaces and endpoints...
1276 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001277 * configuration code.
1278 *
1279 * REVISIT it could make sense to let the composite device
1280 * take such requests too, if that's ever needed: to work
1281 * in config 0, etc.
1282 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001283 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1284 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001285 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301286 break;
1287 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001288 break;
1289
1290 case USB_RECIP_ENDPOINT:
1291 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1292 list_for_each_entry(f, &cdev->config->functions, list) {
1293 if (test_bit(endp, f->endpoints))
1294 break;
1295 }
1296 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001297 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001298 break;
David Brownell40982be2008-06-19 17:52:58 -07001299 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001300
1301 if (f && f->setup)
1302 value = f->setup(f, ctrl);
1303 else {
David Brownell40982be2008-06-19 17:52:58 -07001304 struct usb_configuration *c;
1305
1306 c = cdev->config;
1307 if (c && c->setup)
1308 value = c->setup(c, ctrl);
1309 }
1310
1311 goto done;
1312 }
1313
1314 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001315 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001316 req->length = value;
1317 req->zero = value < w_length;
1318 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1319 if (value < 0) {
1320 DBG(cdev, "ep_queue --> %d\n", value);
1321 req->status = 0;
1322 composite_setup_complete(gadget->ep0, req);
1323 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001324 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1325 WARN(cdev,
1326 "%s: Delayed status not supported for w_length != 0",
1327 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001328 }
1329
1330done:
1331 /* device either stalls (value < 0) or reports success */
1332 return value;
1333}
1334
1335static void composite_disconnect(struct usb_gadget *gadget)
1336{
1337 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1338 unsigned long flags;
1339
1340 /* REVISIT: should we have config and device level
1341 * disconnect callbacks?
1342 */
1343 spin_lock_irqsave(&cdev->lock, flags);
1344 if (cdev->config)
1345 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001346 if (cdev->driver->disconnect)
1347 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001348 spin_unlock_irqrestore(&cdev->lock, flags);
1349}
1350
1351/*-------------------------------------------------------------------------*/
1352
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001353static ssize_t composite_show_suspended(struct device *dev,
1354 struct device_attribute *attr,
1355 char *buf)
1356{
1357 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1358 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1359
1360 return sprintf(buf, "%d\n", cdev->suspended);
1361}
1362
1363static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1364
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001365static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07001366{
1367 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1368
1369 /* composite_disconnect() must already have been called
1370 * by the underlying peripheral controller driver!
1371 * so there's no i/o concurrency that could affect the
1372 * state protected by cdev->lock.
1373 */
1374 WARN_ON(cdev->config);
1375
1376 while (!list_empty(&cdev->configs)) {
1377 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001378 c = list_first_entry(&cdev->configs,
1379 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001380 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001381 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001382 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001383 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001384
1385 if (cdev->req) {
1386 kfree(cdev->req->buf);
1387 usb_ep_free_request(gadget->ep0, cdev->req);
1388 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301389 device_remove_file(&gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001390 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001391 kfree(cdev);
1392 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001393}
1394
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001395static void composite_unbind(struct usb_gadget *gadget)
1396{
1397 __composite_unbind(gadget, true);
1398}
1399
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001400static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1401 const struct usb_device_descriptor *old)
1402{
1403 __le16 idVendor;
1404 __le16 idProduct;
1405 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001406 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001407 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001408 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001409
1410 /*
1411 * these variables may have been set in
1412 * usb_composite_overwrite_options()
1413 */
1414 idVendor = new->idVendor;
1415 idProduct = new->idProduct;
1416 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001417 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001418 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001419 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001420
1421 *new = *old;
1422 if (idVendor)
1423 new->idVendor = idVendor;
1424 if (idProduct)
1425 new->idProduct = idProduct;
1426 if (bcdDevice)
1427 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02001428 else
1429 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001430 if (iSerialNumber)
1431 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001432 if (iManufacturer)
1433 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001434 if (iProduct)
1435 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001436}
1437
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001438static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1439{
1440 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1441}
1442
1443static int composite_bind(struct usb_gadget *gadget,
1444 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07001445{
1446 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001447 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07001448 int status = -ENOMEM;
1449
1450 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1451 if (!cdev)
1452 return status;
1453
1454 spin_lock_init(&cdev->lock);
1455 cdev->gadget = gadget;
1456 set_gadget_data(gadget, cdev);
1457 INIT_LIST_HEAD(&cdev->configs);
1458
1459 /* preallocate control response and buffer */
1460 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1461 if (!cdev->req)
1462 goto fail;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +02001463 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
David Brownell40982be2008-06-19 17:52:58 -07001464 if (!cdev->req->buf)
1465 goto fail;
1466 cdev->req->complete = composite_setup_complete;
1467 gadget->ep0->driver_data = cdev;
1468
David Brownell40982be2008-06-19 17:52:58 -07001469 cdev->driver = composite;
1470
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301471 /*
1472 * As per USB compliance update, a device that is actively drawing
1473 * more than 100mA from USB must report itself as bus-powered in
1474 * the GetStatus(DEVICE) call.
1475 */
1476 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1477 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001478
1479 /* interface and string IDs start at zero via kzalloc.
1480 * we force endpoints to start unassigned; few controller
1481 * drivers will zero ep->driver_data.
1482 */
1483 usb_ep_autoconfig_reset(cdev->gadget);
1484
1485 /* composite gadget needs to assign strings for whole device (like
1486 * serial number), register function drivers, potentially update
1487 * power state and consumption, etc
1488 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02001489 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001490 if (status < 0)
1491 goto fail;
1492
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001493 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001494
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001495 /* has userspace failed to provide a serial number? */
1496 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1497 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1498
1499 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001500 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1501 if (status)
1502 goto fail;
1503
David Brownell40982be2008-06-19 17:52:58 -07001504 INFO(cdev, "%s ready\n", composite->name);
1505 return 0;
1506
1507fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001508 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07001509 return status;
1510}
1511
1512/*-------------------------------------------------------------------------*/
1513
1514static void
1515composite_suspend(struct usb_gadget *gadget)
1516{
1517 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1518 struct usb_function *f;
1519
David Brownell89429392009-03-19 14:14:17 -07001520 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001521 * suspend/resume callbacks?
1522 */
1523 DBG(cdev, "suspend\n");
1524 if (cdev->config) {
1525 list_for_each_entry(f, &cdev->config->functions, list) {
1526 if (f->suspend)
1527 f->suspend(f);
1528 }
1529 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001530 if (cdev->driver->suspend)
1531 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001532
1533 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001534
1535 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001536}
1537
1538static void
1539composite_resume(struct usb_gadget *gadget)
1540{
1541 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1542 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001543 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001544
David Brownell89429392009-03-19 14:14:17 -07001545 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001546 * suspend/resume callbacks?
1547 */
1548 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001549 if (cdev->driver->resume)
1550 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001551 if (cdev->config) {
1552 list_for_each_entry(f, &cdev->config->functions, list) {
1553 if (f->resume)
1554 f->resume(f);
1555 }
Hao Wub23f2f92010-11-29 15:17:03 +08001556
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01001557 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08001558
1559 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01001560 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001561 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001562
1563 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001564}
1565
1566/*-------------------------------------------------------------------------*/
1567
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001568static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001569 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001570 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001571
1572 .setup = composite_setup,
1573 .disconnect = composite_disconnect,
1574
1575 .suspend = composite_suspend,
1576 .resume = composite_resume,
1577
1578 .driver = {
1579 .owner = THIS_MODULE,
1580 },
1581};
1582
1583/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001584 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001585 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001586 * @bind: the callback used to allocate resources that are shared across the
1587 * whole device, such as string IDs, and add its configurations using
1588 * @usb_add_config(). This may fail by returning a negative errno
1589 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001590 * Context: single threaded during gadget setup
1591 *
1592 * This function is used to register drivers using the composite driver
1593 * framework. The return value is zero, or a negative errno value.
1594 * Those values normally come from the driver's @bind method, which does
1595 * all the work of setting up the driver to match the hardware.
1596 *
1597 * On successful return, the gadget is ready to respond to requests from
1598 * the host, unless one of its components invokes usb_gadget_disconnect()
1599 * while it was binding. That would usually be done in order to wait for
1600 * some userspace participation.
1601 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001602int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001603{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001604 struct usb_gadget_driver *gadget_driver;
1605
1606 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07001607 return -EINVAL;
1608
1609 if (!driver->name)
1610 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07001611
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001612 driver->gadget_driver = composite_driver_template;
1613 gadget_driver = &driver->gadget_driver;
1614
1615 gadget_driver->function = (char *) driver->name;
1616 gadget_driver->driver.name = driver->name;
1617 gadget_driver->max_speed = driver->max_speed;
1618
1619 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001620}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001621EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07001622
1623/**
1624 * usb_composite_unregister() - unregister a composite driver
1625 * @driver: the driver to unregister
1626 *
1627 * This function is used to unregister drivers using the composite
1628 * driver framework.
1629 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001630void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001631{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001632 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001633}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001634EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001635
1636/**
1637 * usb_composite_setup_continue() - Continue with the control transfer
1638 * @cdev: the composite device who's control transfer was kept waiting
1639 *
1640 * This function must be called by the USB function driver to continue
1641 * with the control transfer's data/status stage in case it had requested to
1642 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1643 * can request the composite framework to delay the setup request's data/status
1644 * stages by returning USB_GADGET_DELAYED_STATUS.
1645 */
1646void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1647{
1648 int value;
1649 struct usb_request *req = cdev->req;
1650 unsigned long flags;
1651
1652 DBG(cdev, "%s\n", __func__);
1653 spin_lock_irqsave(&cdev->lock, flags);
1654
1655 if (cdev->delayed_status == 0) {
1656 WARN(cdev, "%s: Unexpected call\n", __func__);
1657
1658 } else if (--cdev->delayed_status == 0) {
1659 DBG(cdev, "%s: Completing delayed status\n", __func__);
1660 req->length = 0;
1661 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1662 if (value < 0) {
1663 DBG(cdev, "ep_queue --> %d\n", value);
1664 req->status = 0;
1665 composite_setup_complete(cdev->gadget->ep0, req);
1666 }
1667 }
1668
1669 spin_unlock_irqrestore(&cdev->lock, flags);
1670}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001671EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001672
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001673static char *composite_default_mfr(struct usb_gadget *gadget)
1674{
1675 char *mfr;
1676 int len;
1677
1678 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
1679 init_utsname()->release, gadget->name);
1680 len++;
1681 mfr = kmalloc(len, GFP_KERNEL);
1682 if (!mfr)
1683 return NULL;
1684 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
1685 init_utsname()->release, gadget->name);
1686 return mfr;
1687}
1688
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001689void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1690 struct usb_composite_overwrite *covr)
1691{
1692 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001693 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1694 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001695
1696 if (covr->idVendor)
1697 desc->idVendor = cpu_to_le16(covr->idVendor);
1698
1699 if (covr->idProduct)
1700 desc->idProduct = cpu_to_le16(covr->idProduct);
1701
1702 if (covr->bcdDevice)
1703 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001704
1705 if (covr->serial_number) {
1706 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
1707 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
1708 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001709 if (covr->manufacturer) {
1710 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1711 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001712
1713 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
1714 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1715 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
1716 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001717 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001718
1719 if (covr->product) {
1720 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
1721 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
1722 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001723}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001724EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02001725
1726MODULE_LICENSE("GPL");
1727MODULE_AUTHOR("David Brownell");