blob: c77aca11ad538c3c5cf28de0ccea604c71c4b7fe [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>
17#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020018#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070019
20#include <linux/usb/composite.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030021#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070022
23/*
24 * The code in this file is utility code, used to build a gadget driver
25 * from one or more "function" drivers, one or more "configuration"
26 * objects, and a "usb_composite_driver" by gluing them together along
27 * with the relevant device-wide data.
28 */
29
30/* big enough to hold our biggest descriptor */
Robert Lukassendd0543e2010-03-30 14:14:01 +020031#define USB_BUFSIZ 1024
David Brownell40982be2008-06-19 17:52:58 -070032
33static struct usb_composite_driver *composite;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +020034static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -070035
Lucas De Marchi25985ed2011-03-30 22:57:33 -030036/* Some systems will need runtime overrides for the product identifiers
David Brownell40982be2008-06-19 17:52:58 -070037 * published in the device descriptor, either numbers or strings or both.
38 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
39 */
40
41static ushort idVendor;
42module_param(idVendor, ushort, 0);
43MODULE_PARM_DESC(idVendor, "USB Vendor ID");
44
45static ushort idProduct;
46module_param(idProduct, ushort, 0);
47MODULE_PARM_DESC(idProduct, "USB Product ID");
48
49static ushort bcdDevice;
50module_param(bcdDevice, ushort, 0);
51MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
52
53static char *iManufacturer;
54module_param(iManufacturer, charp, 0);
55MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
56
57static char *iProduct;
58module_param(iProduct, charp, 0);
59MODULE_PARM_DESC(iProduct, "USB Product string");
60
61static char *iSerialNumber;
62module_param(iSerialNumber, charp, 0);
63MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
64
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020065static char composite_manufacturer[50];
66
David Brownell40982be2008-06-19 17:52:58 -070067/*-------------------------------------------------------------------------*/
Tatyana Brokhman48767a42011-06-28 16:33:49 +030068/**
69 * next_ep_desc() - advance to the next EP descriptor
70 * @t: currect pointer within descriptor array
71 *
72 * Return: next EP descriptor or NULL
73 *
74 * Iterate over @t until either EP descriptor found or
75 * NULL (that indicates end of list) encountered
76 */
77static struct usb_descriptor_header**
78next_ep_desc(struct usb_descriptor_header **t)
79{
80 for (; *t; t++) {
81 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
82 return t;
83 }
84 return NULL;
85}
86
87/*
88 * for_each_ep_desc()- iterate over endpoint descriptors in the
89 * descriptors list
90 * @start: pointer within descriptor array.
91 * @ep_desc: endpoint descriptor to use as the loop cursor
92 */
93#define for_each_ep_desc(start, ep_desc) \
94 for (ep_desc = next_ep_desc(start); \
95 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
96
97/**
98 * config_ep_by_speed() - configures the given endpoint
99 * according to gadget speed.
100 * @g: pointer to the gadget
101 * @f: usb function
102 * @_ep: the endpoint to configure
103 *
104 * Return: error code, 0 on success
105 *
106 * This function chooses the right descriptors for a given
107 * endpoint according to gadget speed and saves it in the
108 * endpoint desc field. If the endpoint already has a descriptor
109 * assigned to it - overwrites it with currently corresponding
110 * descriptor. The endpoint maxpacket field is updated according
111 * to the chosen descriptor.
112 * Note: the supplied function should hold all the descriptors
113 * for supported speeds
114 */
115int config_ep_by_speed(struct usb_gadget *g,
116 struct usb_function *f,
117 struct usb_ep *_ep)
118{
119 struct usb_endpoint_descriptor *chosen_desc = NULL;
120 struct usb_descriptor_header **speed_desc = NULL;
121
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300122 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
123 int want_comp_desc = 0;
124
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300125 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
126
127 if (!g || !f || !_ep)
128 return -EIO;
129
130 /* select desired speed */
131 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300132 case USB_SPEED_SUPER:
133 if (gadget_is_superspeed(g)) {
134 speed_desc = f->ss_descriptors;
135 want_comp_desc = 1;
136 break;
137 }
138 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300139 case USB_SPEED_HIGH:
140 if (gadget_is_dualspeed(g)) {
141 speed_desc = f->hs_descriptors;
142 break;
143 }
144 /* else: fall through */
145 default:
146 speed_desc = f->descriptors;
147 }
148 /* find descriptors */
149 for_each_ep_desc(speed_desc, d_spd) {
150 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
151 if (chosen_desc->bEndpointAddress == _ep->address)
152 goto ep_found;
153 }
154 return -EIO;
155
156ep_found:
157 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700158 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300159 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300160 _ep->comp_desc = NULL;
161 _ep->maxburst = 0;
162 _ep->mult = 0;
163 if (!want_comp_desc)
164 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300165
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300166 /*
167 * Companion descriptor should follow EP descriptor
168 * USB 3.0 spec, #9.6.7
169 */
170 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
171 if (!comp_desc ||
172 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
173 return -EIO;
174 _ep->comp_desc = comp_desc;
175 if (g->speed == USB_SPEED_SUPER) {
176 switch (usb_endpoint_type(_ep->desc)) {
177 case USB_ENDPOINT_XFER_BULK:
178 case USB_ENDPOINT_XFER_INT:
179 _ep->maxburst = comp_desc->bMaxBurst;
180 break;
181 case USB_ENDPOINT_XFER_ISOC:
182 /* mult: bits 1:0 of bmAttributes */
183 _ep->mult = comp_desc->bmAttributes & 0x3;
184 break;
185 default:
186 /* Do nothing for control endpoints */
187 break;
188 }
189 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300190 return 0;
191}
David Brownell40982be2008-06-19 17:52:58 -0700192
193/**
194 * usb_add_function() - add a function to a configuration
195 * @config: the configuration
196 * @function: the function being added
197 * Context: single threaded during gadget setup
198 *
199 * After initialization, each configuration must have one or more
200 * functions added to it. Adding a function involves calling its @bind()
201 * method to allocate resources such as interface and string identifiers
202 * and endpoints.
203 *
204 * This function returns the value of the function's bind(), which is
205 * zero for success else a negative errno value.
206 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200207int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700208 struct usb_function *function)
209{
210 int value = -EINVAL;
211
212 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
213 function->name, function,
214 config->label, config);
215
216 if (!function->set_alt || !function->disable)
217 goto done;
218
219 function->config = config;
220 list_add_tail(&function->list, &config->functions);
221
222 /* REVISIT *require* function->bind? */
223 if (function->bind) {
224 value = function->bind(config, function);
225 if (value < 0) {
226 list_del(&function->list);
227 function->config = NULL;
228 }
229 } else
230 value = 0;
231
232 /* We allow configurations that don't work at both speeds.
233 * If we run into a lowspeed Linux system, treat it the same
234 * as full speed ... it's the function drivers that will need
235 * to avoid bulk and ISO transfers.
236 */
237 if (!config->fullspeed && function->descriptors)
238 config->fullspeed = true;
239 if (!config->highspeed && function->hs_descriptors)
240 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300241 if (!config->superspeed && function->ss_descriptors)
242 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700243
244done:
245 if (value)
246 DBG(config->cdev, "adding '%s'/%p --> %d\n",
247 function->name, function, value);
248 return value;
249}
250
251/**
David Brownell60beed92008-08-18 17:38:22 -0700252 * usb_function_deactivate - prevent function and gadget enumeration
253 * @function: the function that isn't yet ready to respond
254 *
255 * Blocks response of the gadget driver to host enumeration by
256 * preventing the data line pullup from being activated. This is
257 * normally called during @bind() processing to change from the
258 * initial "ready to respond" state, or when a required resource
259 * becomes available.
260 *
261 * For example, drivers that serve as a passthrough to a userspace
262 * daemon can block enumeration unless that daemon (such as an OBEX,
263 * MTP, or print server) is ready to handle host requests.
264 *
265 * Not all systems support software control of their USB peripheral
266 * data pullups.
267 *
268 * Returns zero on success, else negative errno.
269 */
270int usb_function_deactivate(struct usb_function *function)
271{
272 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200273 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700274 int status = 0;
275
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200276 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700277
278 if (cdev->deactivations == 0)
279 status = usb_gadget_disconnect(cdev->gadget);
280 if (status == 0)
281 cdev->deactivations++;
282
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200283 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700284 return status;
285}
286
287/**
288 * usb_function_activate - allow function and gadget enumeration
289 * @function: function on which usb_function_activate() was called
290 *
291 * Reverses effect of usb_function_deactivate(). If no more functions
292 * are delaying their activation, the gadget driver will respond to
293 * host enumeration procedures.
294 *
295 * Returns zero on success, else negative errno.
296 */
297int usb_function_activate(struct usb_function *function)
298{
299 struct usb_composite_dev *cdev = function->config->cdev;
300 int status = 0;
301
302 spin_lock(&cdev->lock);
303
304 if (WARN_ON(cdev->deactivations == 0))
305 status = -EINVAL;
306 else {
307 cdev->deactivations--;
308 if (cdev->deactivations == 0)
309 status = usb_gadget_connect(cdev->gadget);
310 }
311
312 spin_unlock(&cdev->lock);
313 return status;
314}
315
316/**
David Brownell40982be2008-06-19 17:52:58 -0700317 * usb_interface_id() - allocate an unused interface ID
318 * @config: configuration associated with the interface
319 * @function: function handling the interface
320 * Context: single threaded during gadget setup
321 *
322 * usb_interface_id() is called from usb_function.bind() callbacks to
323 * allocate new interface IDs. The function driver will then store that
324 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300325 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700326 * particularly changing its altsetting via set_alt(). There may
327 * also be class-specific or vendor-specific requests to handle.
328 *
329 * All interface identifier should be allocated using this routine, to
330 * ensure that for example different functions don't wrongly assign
331 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300332 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700333 * one configuration (or more than once in a given configuration) need
334 * multiple versions of the relevant descriptors.
335 *
336 * Returns the interface ID which was allocated; or -ENODEV if no
337 * more interface IDs can be allocated.
338 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200339int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700340 struct usb_function *function)
341{
342 unsigned id = config->next_interface_id;
343
344 if (id < MAX_CONFIG_INTERFACES) {
345 config->interface[id] = function;
346 config->next_interface_id = id + 1;
347 return id;
348 }
349 return -ENODEV;
350}
351
352static int config_buf(struct usb_configuration *config,
353 enum usb_device_speed speed, void *buf, u8 type)
354{
355 struct usb_config_descriptor *c = buf;
356 void *next = buf + USB_DT_CONFIG_SIZE;
357 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
358 struct usb_function *f;
359 int status;
360
361 /* write the config descriptor */
362 c = buf;
363 c->bLength = USB_DT_CONFIG_SIZE;
364 c->bDescriptorType = type;
365 /* wTotalLength is written later */
366 c->bNumInterfaces = config->next_interface_id;
367 c->bConfigurationValue = config->bConfigurationValue;
368 c->iConfiguration = config->iConfiguration;
369 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700370 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700371
372 /* There may be e.g. OTG descriptors */
373 if (config->descriptors) {
374 status = usb_descriptor_fillbuf(next, len,
375 config->descriptors);
376 if (status < 0)
377 return status;
378 len -= status;
379 next += status;
380 }
381
382 /* add each function's descriptors */
383 list_for_each_entry(f, &config->functions, list) {
384 struct usb_descriptor_header **descriptors;
385
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300386 switch (speed) {
387 case USB_SPEED_SUPER:
388 descriptors = f->ss_descriptors;
389 break;
390 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700391 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300392 break;
393 default:
David Brownell40982be2008-06-19 17:52:58 -0700394 descriptors = f->descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300395 }
396
David Brownell40982be2008-06-19 17:52:58 -0700397 if (!descriptors)
398 continue;
399 status = usb_descriptor_fillbuf(next, len,
400 (const struct usb_descriptor_header **) descriptors);
401 if (status < 0)
402 return status;
403 len -= status;
404 next += status;
405 }
406
407 len = next - buf;
408 c->wTotalLength = cpu_to_le16(len);
409 return len;
410}
411
412static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
413{
414 struct usb_gadget *gadget = cdev->gadget;
415 struct usb_configuration *c;
416 u8 type = w_value >> 8;
417 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
418
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300419 if (gadget->speed == USB_SPEED_SUPER)
420 speed = gadget->speed;
421 else if (gadget_is_dualspeed(gadget)) {
422 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700423 if (gadget->speed == USB_SPEED_HIGH)
424 hs = 1;
425 if (type == USB_DT_OTHER_SPEED_CONFIG)
426 hs = !hs;
427 if (hs)
428 speed = USB_SPEED_HIGH;
429
430 }
431
432 /* This is a lookup by config *INDEX* */
433 w_value &= 0xff;
434 list_for_each_entry(c, &cdev->configs, list) {
435 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300436 switch (speed) {
437 case USB_SPEED_SUPER:
438 if (!c->superspeed)
439 continue;
440 break;
441 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700442 if (!c->highspeed)
443 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300444 break;
445 default:
David Brownell40982be2008-06-19 17:52:58 -0700446 if (!c->fullspeed)
447 continue;
448 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300449
David Brownell40982be2008-06-19 17:52:58 -0700450 if (w_value == 0)
451 return config_buf(c, speed, cdev->req->buf, type);
452 w_value--;
453 }
454 return -EINVAL;
455}
456
457static int count_configs(struct usb_composite_dev *cdev, unsigned type)
458{
459 struct usb_gadget *gadget = cdev->gadget;
460 struct usb_configuration *c;
461 unsigned count = 0;
462 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300463 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700464
465 if (gadget_is_dualspeed(gadget)) {
466 if (gadget->speed == USB_SPEED_HIGH)
467 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300468 if (gadget->speed == USB_SPEED_SUPER)
469 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700470 if (type == USB_DT_DEVICE_QUALIFIER)
471 hs = !hs;
472 }
473 list_for_each_entry(c, &cdev->configs, list) {
474 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300475 if (ss) {
476 if (!c->superspeed)
477 continue;
478 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700479 if (!c->highspeed)
480 continue;
481 } else {
482 if (!c->fullspeed)
483 continue;
484 }
485 count++;
486 }
487 return count;
488}
489
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300490/**
491 * bos_desc() - prepares the BOS descriptor.
492 * @cdev: pointer to usb_composite device to generate the bos
493 * descriptor for
494 *
495 * This function generates the BOS (Binary Device Object)
496 * descriptor and its device capabilities descriptors. The BOS
497 * descriptor should be supported by a SuperSpeed device.
498 */
499static int bos_desc(struct usb_composite_dev *cdev)
500{
501 struct usb_ext_cap_descriptor *usb_ext;
502 struct usb_ss_cap_descriptor *ss_cap;
503 struct usb_dcd_config_params dcd_config_params;
504 struct usb_bos_descriptor *bos = cdev->req->buf;
505
506 bos->bLength = USB_DT_BOS_SIZE;
507 bos->bDescriptorType = USB_DT_BOS;
508
509 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
510 bos->bNumDeviceCaps = 0;
511
512 /*
513 * A SuperSpeed device shall include the USB2.0 extension descriptor
514 * and shall support LPM when operating in USB2.0 HS mode.
515 */
516 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
517 bos->bNumDeviceCaps++;
518 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
519 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
520 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
521 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
522 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
523
524 /*
525 * The Superspeed USB Capability descriptor shall be implemented by all
526 * SuperSpeed devices.
527 */
528 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
529 bos->bNumDeviceCaps++;
530 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
531 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
532 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
533 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
534 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
535 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
536 USB_FULL_SPEED_OPERATION |
537 USB_HIGH_SPEED_OPERATION |
538 USB_5GBPS_OPERATION);
539 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
540
541 /* Get Controller configuration */
542 if (cdev->gadget->ops->get_config_params)
543 cdev->gadget->ops->get_config_params(&dcd_config_params);
544 else {
545 dcd_config_params.bU1devExitLat = USB_DEFULT_U1_DEV_EXIT_LAT;
546 dcd_config_params.bU2DevExitLat =
547 cpu_to_le16(USB_DEFULT_U2_DEV_EXIT_LAT);
548 }
549 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
550 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
551
552 return le16_to_cpu(bos->wTotalLength);
553}
554
David Brownell40982be2008-06-19 17:52:58 -0700555static void device_qual(struct usb_composite_dev *cdev)
556{
557 struct usb_qualifier_descriptor *qual = cdev->req->buf;
558
559 qual->bLength = sizeof(*qual);
560 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
561 /* POLICY: same bcdUSB and device type info at both speeds */
562 qual->bcdUSB = cdev->desc.bcdUSB;
563 qual->bDeviceClass = cdev->desc.bDeviceClass;
564 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
565 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
566 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200567 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700568 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700569 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700570}
571
572/*-------------------------------------------------------------------------*/
573
574static void reset_config(struct usb_composite_dev *cdev)
575{
576 struct usb_function *f;
577
578 DBG(cdev, "reset config\n");
579
580 list_for_each_entry(f, &cdev->config->functions, list) {
581 if (f->disable)
582 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200583
584 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700585 }
586 cdev->config = NULL;
587}
588
589static int set_config(struct usb_composite_dev *cdev,
590 const struct usb_ctrlrequest *ctrl, unsigned number)
591{
592 struct usb_gadget *gadget = cdev->gadget;
593 struct usb_configuration *c = NULL;
594 int result = -EINVAL;
595 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
596 int tmp;
597
David Brownell40982be2008-06-19 17:52:58 -0700598 if (number) {
599 list_for_each_entry(c, &cdev->configs, list) {
600 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300601 /*
602 * We disable the FDs of the previous
603 * configuration only if the new configuration
604 * is a valid one
605 */
606 if (cdev->config)
607 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700608 result = 0;
609 break;
610 }
611 }
612 if (result < 0)
613 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300614 } else { /* Zero configuration value - need to reset the config */
615 if (cdev->config)
616 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700617 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300618 }
David Brownell40982be2008-06-19 17:52:58 -0700619
620 INFO(cdev, "%s speed config #%d: %s\n",
621 ({ char *speed;
622 switch (gadget->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300623 case USB_SPEED_LOW:
624 speed = "low";
625 break;
626 case USB_SPEED_FULL:
627 speed = "full";
628 break;
629 case USB_SPEED_HIGH:
630 speed = "high";
631 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300632 case USB_SPEED_SUPER:
633 speed = "super";
634 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300635 default:
636 speed = "?";
637 break;
David Brownell40982be2008-06-19 17:52:58 -0700638 } ; speed; }), number, c ? c->label : "unconfigured");
639
640 if (!c)
641 goto done;
642
643 cdev->config = c;
644
645 /* Initialize all interfaces by setting them to altsetting zero. */
646 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
647 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200648 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700649
650 if (!f)
651 break;
652
Laurent Pinchart52426582009-10-21 00:03:38 +0200653 /*
654 * Record which endpoints are used by the function. This is used
655 * to dispatch control requests targeted at that endpoint to the
656 * function's setup callback instead of the current
657 * configuration's setup callback.
658 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300659 switch (gadget->speed) {
660 case USB_SPEED_SUPER:
661 descriptors = f->ss_descriptors;
662 break;
663 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200664 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300665 break;
666 default:
Laurent Pinchart52426582009-10-21 00:03:38 +0200667 descriptors = f->descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300668 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200669
670 for (; *descriptors; ++descriptors) {
671 struct usb_endpoint_descriptor *ep;
672 int addr;
673
674 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
675 continue;
676
677 ep = (struct usb_endpoint_descriptor *)*descriptors;
678 addr = ((ep->bEndpointAddress & 0x80) >> 3)
679 | (ep->bEndpointAddress & 0x0f);
680 set_bit(addr, f->endpoints);
681 }
682
David Brownell40982be2008-06-19 17:52:58 -0700683 result = f->set_alt(f, tmp, 0);
684 if (result < 0) {
685 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
686 tmp, f->name, f, result);
687
688 reset_config(cdev);
689 goto done;
690 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300691
692 if (result == USB_GADGET_DELAYED_STATUS) {
693 DBG(cdev,
694 "%s: interface %d (%s) requested delayed status\n",
695 __func__, tmp, f->name);
696 cdev->delayed_status++;
697 DBG(cdev, "delayed_status count %d\n",
698 cdev->delayed_status);
699 }
David Brownell40982be2008-06-19 17:52:58 -0700700 }
701
702 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700703 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700704done:
705 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300706 if (result >= 0 && cdev->delayed_status)
707 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700708 return result;
709}
710
711/**
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;
731 struct usb_configuration *c;
732
733 DBG(cdev, "adding config #%u '%s'/%p\n",
734 config->bConfigurationValue,
735 config->label, config);
736
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200737 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700738 goto done;
739
740 /* Prevent duplicate configuration identifiers */
741 list_for_each_entry(c, &cdev->configs, list) {
742 if (c->bConfigurationValue == config->bConfigurationValue) {
743 status = -EBUSY;
744 goto done;
745 }
746 }
747
748 config->cdev = cdev;
749 list_add_tail(&config->list, &cdev->configs);
750
751 INIT_LIST_HEAD(&config->functions);
752 config->next_interface_id = 0;
753
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200754 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700755 if (status < 0) {
756 list_del(&config->list);
757 config->cdev = NULL;
758 } else {
759 unsigned i;
760
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300761 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700762 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300763 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700764 config->highspeed ? " high" : "",
765 config->fullspeed
766 ? (gadget_is_dualspeed(cdev->gadget)
767 ? " full"
768 : " full/low")
769 : "");
770
771 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
772 struct usb_function *f = config->interface[i];
773
774 if (!f)
775 continue;
776 DBG(cdev, " interface %d = %s/%p\n",
777 i, f->name, f);
778 }
779 }
780
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200781 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700782 * ep->driver_data as needed.
783 */
784 usb_ep_autoconfig_reset(cdev->gadget);
785
786done:
787 if (status)
788 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
789 config->bConfigurationValue, status);
790 return status;
791}
792
793/*-------------------------------------------------------------------------*/
794
795/* We support strings in multiple languages ... string descriptor zero
796 * says which languages are supported. The typical case will be that
797 * only one language (probably English) is used, with I18N handled on
798 * the host side.
799 */
800
801static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
802{
803 const struct usb_gadget_strings *s;
804 u16 language;
805 __le16 *tmp;
806
807 while (*sp) {
808 s = *sp;
809 language = cpu_to_le16(s->language);
810 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
811 if (*tmp == language)
812 goto repeat;
813 }
814 *tmp++ = language;
815repeat:
816 sp++;
817 }
818}
819
820static int lookup_string(
821 struct usb_gadget_strings **sp,
822 void *buf,
823 u16 language,
824 int id
825)
826{
827 struct usb_gadget_strings *s;
828 int value;
829
830 while (*sp) {
831 s = *sp++;
832 if (s->language != language)
833 continue;
834 value = usb_gadget_get_string(s, id, buf);
835 if (value > 0)
836 return value;
837 }
838 return -EINVAL;
839}
840
841static int get_string(struct usb_composite_dev *cdev,
842 void *buf, u16 language, int id)
843{
844 struct usb_configuration *c;
845 struct usb_function *f;
846 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200847 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700848
849 /* Yes, not only is USB's I18N support probably more than most
850 * folk will ever care about ... also, it's all supported here.
851 * (Except for UTF8 support for Unicode's "Astral Planes".)
852 */
853
854 /* 0 == report all available language codes */
855 if (id == 0) {
856 struct usb_string_descriptor *s = buf;
857 struct usb_gadget_strings **sp;
858
859 memset(s, 0, 256);
860 s->bDescriptorType = USB_DT_STRING;
861
862 sp = composite->strings;
863 if (sp)
864 collect_langs(sp, s->wData);
865
866 list_for_each_entry(c, &cdev->configs, list) {
867 sp = c->strings;
868 if (sp)
869 collect_langs(sp, s->wData);
870
871 list_for_each_entry(f, &c->functions, list) {
872 sp = f->strings;
873 if (sp)
874 collect_langs(sp, s->wData);
875 }
876 }
877
Roel Kluin417b57b2009-08-06 16:09:51 -0700878 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700879 continue;
880 if (!len)
881 return -EINVAL;
882
883 s->bLength = 2 * (len + 1);
884 return s->bLength;
885 }
886
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200887 /* Otherwise, look up and return a specified string. First
888 * check if the string has not been overridden.
889 */
890 if (cdev->manufacturer_override == id)
891 str = iManufacturer ?: composite->iManufacturer ?:
892 composite_manufacturer;
893 else if (cdev->product_override == id)
894 str = iProduct ?: composite->iProduct;
895 else if (cdev->serial_override == id)
896 str = iSerialNumber;
897 else
898 str = NULL;
899 if (str) {
900 struct usb_gadget_strings strings = {
901 .language = language,
902 .strings = &(struct usb_string) { 0xff, str }
903 };
904 return usb_gadget_get_string(&strings, 0xff, buf);
905 }
906
907 /* String IDs are device-scoped, so we look up each string
908 * table we're told about. These lookups are infrequent;
909 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700910 */
911 if (composite->strings) {
912 len = lookup_string(composite->strings, buf, language, id);
913 if (len > 0)
914 return len;
915 }
916 list_for_each_entry(c, &cdev->configs, list) {
917 if (c->strings) {
918 len = lookup_string(c->strings, buf, language, id);
919 if (len > 0)
920 return len;
921 }
922 list_for_each_entry(f, &c->functions, list) {
923 if (!f->strings)
924 continue;
925 len = lookup_string(f->strings, buf, language, id);
926 if (len > 0)
927 return len;
928 }
929 }
930 return -EINVAL;
931}
932
933/**
934 * usb_string_id() - allocate an unused string ID
935 * @cdev: the device whose string descriptor IDs are being allocated
936 * Context: single threaded during gadget setup
937 *
938 * @usb_string_id() is called from bind() callbacks to allocate
939 * string IDs. Drivers for functions, configurations, or gadgets will
940 * then store that ID in the appropriate descriptors and string table.
941 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200942 * All string identifier should be allocated using this,
943 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
944 * that for example different functions don't wrongly assign different
945 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700946 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200947int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700948{
949 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200950 /* string id 0 is reserved by USB spec for list of
951 * supported languages */
952 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700953 cdev->next_string_id++;
954 return cdev->next_string_id;
955 }
956 return -ENODEV;
957}
958
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200959/**
960 * usb_string_ids() - allocate unused string IDs in batch
961 * @cdev: the device whose string descriptor IDs are being allocated
962 * @str: an array of usb_string objects to assign numbers to
963 * Context: single threaded during gadget setup
964 *
965 * @usb_string_ids() is called from bind() callbacks to allocate
966 * string IDs. Drivers for functions, configurations, or gadgets will
967 * then copy IDs from the string table to the appropriate descriptors
968 * and string table for other languages.
969 *
970 * All string identifier should be allocated using this,
971 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
972 * example different functions don't wrongly assign different meanings
973 * to the same identifier.
974 */
975int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
976{
977 int next = cdev->next_string_id;
978
979 for (; str->s; ++str) {
980 if (unlikely(next >= 254))
981 return -ENODEV;
982 str->id = ++next;
983 }
984
985 cdev->next_string_id = next;
986
987 return 0;
988}
989
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}
1017
1018
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);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001314 if (composite->disconnect)
1315 composite->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;
1347
1348 c = list_first_entry(&cdev->configs,
1349 struct usb_configuration, list);
1350 while (!list_empty(&c->functions)) {
1351 struct usb_function *f;
1352
1353 f = list_first_entry(&c->functions,
1354 struct usb_function, list);
1355 list_del(&f->list);
1356 if (f->unbind) {
1357 DBG(cdev, "unbind function '%s'/%p\n",
1358 f->name, f);
1359 f->unbind(c, f);
1360 /* may free memory for "f" */
1361 }
1362 }
1363 list_del(&c->list);
1364 if (c->unbind) {
1365 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1366 c->unbind(c);
1367 /* may free memory for "c" */
1368 }
1369 }
1370 if (composite->unbind)
1371 composite->unbind(cdev);
1372
1373 if (cdev->req) {
1374 kfree(cdev->req->buf);
1375 usb_ep_free_request(gadget->ep0, cdev->req);
1376 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301377 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001378 kfree(cdev);
1379 set_gadget_data(gadget, NULL);
1380 composite = NULL;
1381}
1382
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001383static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001384{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001385 if (!*desc) {
1386 int ret = usb_string_id(cdev);
1387 if (unlikely(ret < 0))
1388 WARNING(cdev, "failed to override string ID\n");
1389 else
1390 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001391 }
David Brownell40982be2008-06-19 17:52:58 -07001392
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001393 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001394}
1395
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001396static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001397{
1398 struct usb_composite_dev *cdev;
1399 int status = -ENOMEM;
1400
1401 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1402 if (!cdev)
1403 return status;
1404
1405 spin_lock_init(&cdev->lock);
1406 cdev->gadget = gadget;
1407 set_gadget_data(gadget, cdev);
1408 INIT_LIST_HEAD(&cdev->configs);
1409
1410 /* preallocate control response and buffer */
1411 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1412 if (!cdev->req)
1413 goto fail;
1414 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1415 if (!cdev->req->buf)
1416 goto fail;
1417 cdev->req->complete = composite_setup_complete;
1418 gadget->ep0->driver_data = cdev;
1419
1420 cdev->bufsiz = USB_BUFSIZ;
1421 cdev->driver = composite;
1422
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301423 /*
1424 * As per USB compliance update, a device that is actively drawing
1425 * more than 100mA from USB must report itself as bus-powered in
1426 * the GetStatus(DEVICE) call.
1427 */
1428 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1429 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001430
1431 /* interface and string IDs start at zero via kzalloc.
1432 * we force endpoints to start unassigned; few controller
1433 * drivers will zero ep->driver_data.
1434 */
1435 usb_ep_autoconfig_reset(cdev->gadget);
1436
1437 /* composite gadget needs to assign strings for whole device (like
1438 * serial number), register function drivers, potentially update
1439 * power state and consumption, etc
1440 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001441 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001442 if (status < 0)
1443 goto fail;
1444
1445 cdev->desc = *composite->dev;
David Brownell40982be2008-06-19 17:52:58 -07001446
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001447 /* standardized runtime overrides for device ID data */
1448 if (idVendor)
1449 cdev->desc.idVendor = cpu_to_le16(idVendor);
1450 if (idProduct)
1451 cdev->desc.idProduct = cpu_to_le16(idProduct);
1452 if (bcdDevice)
1453 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1454
Marek Belisko78bff3c2010-10-27 10:19:01 +02001455 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001456 if (iManufacturer || !cdev->desc.iManufacturer) {
1457 if (!iManufacturer && !composite->iManufacturer &&
1458 !*composite_manufacturer)
1459 snprintf(composite_manufacturer,
1460 sizeof composite_manufacturer,
1461 "%s %s with %s",
1462 init_utsname()->sysname,
1463 init_utsname()->release,
1464 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001465
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001466 cdev->manufacturer_override =
1467 override_id(cdev, &cdev->desc.iManufacturer);
1468 }
1469
1470 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1471 cdev->product_override =
1472 override_id(cdev, &cdev->desc.iProduct);
1473
1474 if (iSerialNumber)
1475 cdev->serial_override =
1476 override_id(cdev, &cdev->desc.iSerialNumber);
1477
1478 /* has userspace failed to provide a serial number? */
1479 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1480 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1481
1482 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001483 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1484 if (status)
1485 goto fail;
1486
David Brownell40982be2008-06-19 17:52:58 -07001487 INFO(cdev, "%s ready\n", composite->name);
1488 return 0;
1489
1490fail:
1491 composite_unbind(gadget);
1492 return status;
1493}
1494
1495/*-------------------------------------------------------------------------*/
1496
1497static void
1498composite_suspend(struct usb_gadget *gadget)
1499{
1500 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1501 struct usb_function *f;
1502
David Brownell89429392009-03-19 14:14:17 -07001503 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001504 * suspend/resume callbacks?
1505 */
1506 DBG(cdev, "suspend\n");
1507 if (cdev->config) {
1508 list_for_each_entry(f, &cdev->config->functions, list) {
1509 if (f->suspend)
1510 f->suspend(f);
1511 }
1512 }
David Brownell89429392009-03-19 14:14:17 -07001513 if (composite->suspend)
1514 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001515
1516 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001517
1518 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001519}
1520
1521static void
1522composite_resume(struct usb_gadget *gadget)
1523{
1524 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1525 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001526 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001527
David Brownell89429392009-03-19 14:14:17 -07001528 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001529 * suspend/resume callbacks?
1530 */
1531 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001532 if (composite->resume)
1533 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001534 if (cdev->config) {
1535 list_for_each_entry(f, &cdev->config->functions, list) {
1536 if (f->resume)
1537 f->resume(f);
1538 }
Hao Wub23f2f92010-11-29 15:17:03 +08001539
1540 maxpower = cdev->config->bMaxPower;
1541
1542 usb_gadget_vbus_draw(gadget, maxpower ?
1543 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001544 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001545
1546 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001547}
1548
1549/*-------------------------------------------------------------------------*/
1550
1551static struct usb_gadget_driver composite_driver = {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001552#ifdef CONFIG_USB_GADGET_SUPERSPEED
1553 .speed = USB_SPEED_SUPER,
1554#else
David Brownell40982be2008-06-19 17:52:58 -07001555 .speed = USB_SPEED_HIGH,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001556#endif
David Brownell40982be2008-06-19 17:52:58 -07001557
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001558 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001559
1560 .setup = composite_setup,
1561 .disconnect = composite_disconnect,
1562
1563 .suspend = composite_suspend,
1564 .resume = composite_resume,
1565
1566 .driver = {
1567 .owner = THIS_MODULE,
1568 },
1569};
1570
1571/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001572 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001573 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001574 * @bind: the callback used to allocate resources that are shared across the
1575 * whole device, such as string IDs, and add its configurations using
1576 * @usb_add_config(). This may fail by returning a negative errno
1577 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001578 * Context: single threaded during gadget setup
1579 *
1580 * This function is used to register drivers using the composite driver
1581 * framework. The return value is zero, or a negative errno value.
1582 * Those values normally come from the driver's @bind method, which does
1583 * all the work of setting up the driver to match the hardware.
1584 *
1585 * On successful return, the gadget is ready to respond to requests from
1586 * the host, unless one of its components invokes usb_gadget_disconnect()
1587 * while it was binding. That would usually be done in order to wait for
1588 * some userspace participation.
1589 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001590int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001591 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001592{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001593 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001594 return -EINVAL;
1595
1596 if (!driver->name)
1597 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001598 if (!driver->iProduct)
1599 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001600 composite_driver.function = (char *) driver->name;
1601 composite_driver.driver.name = driver->name;
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +03001602 composite_driver.speed = min((u8)composite_driver.speed,
1603 (u8)driver->max_speed);
David Brownell40982be2008-06-19 17:52:58 -07001604 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001605 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001606
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001607 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001608}
1609
1610/**
1611 * usb_composite_unregister() - unregister a composite driver
1612 * @driver: the driver to unregister
1613 *
1614 * This function is used to unregister drivers using the composite
1615 * driver framework.
1616 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001617void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001618{
1619 if (composite != driver)
1620 return;
1621 usb_gadget_unregister_driver(&composite_driver);
1622}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001623
1624/**
1625 * usb_composite_setup_continue() - Continue with the control transfer
1626 * @cdev: the composite device who's control transfer was kept waiting
1627 *
1628 * This function must be called by the USB function driver to continue
1629 * with the control transfer's data/status stage in case it had requested to
1630 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1631 * can request the composite framework to delay the setup request's data/status
1632 * stages by returning USB_GADGET_DELAYED_STATUS.
1633 */
1634void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1635{
1636 int value;
1637 struct usb_request *req = cdev->req;
1638 unsigned long flags;
1639
1640 DBG(cdev, "%s\n", __func__);
1641 spin_lock_irqsave(&cdev->lock, flags);
1642
1643 if (cdev->delayed_status == 0) {
1644 WARN(cdev, "%s: Unexpected call\n", __func__);
1645
1646 } else if (--cdev->delayed_status == 0) {
1647 DBG(cdev, "%s: Completing delayed status\n", __func__);
1648 req->length = 0;
1649 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1650 if (value < 0) {
1651 DBG(cdev, "ep_queue --> %d\n", value);
1652 req->status = 0;
1653 composite_setup_complete(cdev->gadget->ep0, req);
1654 }
1655 }
1656
1657 spin_unlock_irqrestore(&cdev->lock, flags);
1658}
1659