blob: 36c6f47642f8f3b73362d870bd1d75c0de8acac4 [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
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020024#include "u_os_desc.h"
25
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020026/**
27 * struct usb_os_string - represents OS String to be reported by a gadget
28 * @bLength: total length of the entire descritor, always 0x12
29 * @bDescriptorType: USB_DT_STRING
30 * @qwSignature: the OS String proper
31 * @bMS_VendorCode: code used by the host for subsequent requests
32 * @bPad: not used, must be zero
33 */
34struct usb_os_string {
35 __u8 bLength;
36 __u8 bDescriptorType;
37 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
38 __u8 bMS_VendorCode;
39 __u8 bPad;
40} __packed;
41
David Brownell40982be2008-06-19 17:52:58 -070042/*
43 * The code in this file is utility code, used to build a gadget driver
44 * from one or more "function" drivers, one or more "configuration"
45 * objects, and a "usb_composite_driver" by gluing them together along
46 * with the relevant device-wide data.
47 */
48
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010049static struct usb_gadget_strings **get_containers_gs(
50 struct usb_gadget_string_container *uc)
51{
52 return (struct usb_gadget_strings **)uc->stash;
53}
54
Tatyana Brokhman48767a42011-06-28 16:33:49 +030055/**
56 * next_ep_desc() - advance to the next EP descriptor
57 * @t: currect pointer within descriptor array
58 *
59 * Return: next EP descriptor or NULL
60 *
61 * Iterate over @t until either EP descriptor found or
62 * NULL (that indicates end of list) encountered
63 */
64static struct usb_descriptor_header**
65next_ep_desc(struct usb_descriptor_header **t)
66{
67 for (; *t; t++) {
68 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
69 return t;
70 }
71 return NULL;
72}
73
74/*
75 * for_each_ep_desc()- iterate over endpoint descriptors in the
76 * descriptors list
77 * @start: pointer within descriptor array.
78 * @ep_desc: endpoint descriptor to use as the loop cursor
79 */
80#define for_each_ep_desc(start, ep_desc) \
81 for (ep_desc = next_ep_desc(start); \
82 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
83
84/**
85 * config_ep_by_speed() - configures the given endpoint
86 * according to gadget speed.
87 * @g: pointer to the gadget
88 * @f: usb function
89 * @_ep: the endpoint to configure
90 *
91 * Return: error code, 0 on success
92 *
93 * This function chooses the right descriptors for a given
94 * endpoint according to gadget speed and saves it in the
95 * endpoint desc field. If the endpoint already has a descriptor
96 * assigned to it - overwrites it with currently corresponding
97 * descriptor. The endpoint maxpacket field is updated according
98 * to the chosen descriptor.
99 * Note: the supplied function should hold all the descriptors
100 * for supported speeds
101 */
102int config_ep_by_speed(struct usb_gadget *g,
103 struct usb_function *f,
104 struct usb_ep *_ep)
105{
Felipe Balbib785ea72012-06-06 10:20:23 +0300106 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300107 struct usb_endpoint_descriptor *chosen_desc = NULL;
108 struct usb_descriptor_header **speed_desc = NULL;
109
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300110 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
111 int want_comp_desc = 0;
112
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300113 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
114
115 if (!g || !f || !_ep)
116 return -EIO;
117
118 /* select desired speed */
119 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300120 case USB_SPEED_SUPER:
121 if (gadget_is_superspeed(g)) {
122 speed_desc = f->ss_descriptors;
123 want_comp_desc = 1;
124 break;
125 }
126 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300127 case USB_SPEED_HIGH:
128 if (gadget_is_dualspeed(g)) {
129 speed_desc = f->hs_descriptors;
130 break;
131 }
132 /* else: fall through */
133 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200134 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300135 }
136 /* find descriptors */
137 for_each_ep_desc(speed_desc, d_spd) {
138 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
139 if (chosen_desc->bEndpointAddress == _ep->address)
140 goto ep_found;
141 }
142 return -EIO;
143
144ep_found:
145 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700146 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300147 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300148 _ep->comp_desc = NULL;
149 _ep->maxburst = 0;
150 _ep->mult = 0;
151 if (!want_comp_desc)
152 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300153
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300154 /*
155 * Companion descriptor should follow EP descriptor
156 * USB 3.0 spec, #9.6.7
157 */
158 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
159 if (!comp_desc ||
160 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
161 return -EIO;
162 _ep->comp_desc = comp_desc;
163 if (g->speed == USB_SPEED_SUPER) {
164 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300165 case USB_ENDPOINT_XFER_ISOC:
166 /* mult: bits 1:0 of bmAttributes */
167 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800168 case USB_ENDPOINT_XFER_BULK:
169 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300170 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300171 break;
172 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300173 if (comp_desc->bMaxBurst != 0)
174 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
175 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300176 break;
177 }
178 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300179 return 0;
180}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200181EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700182
183/**
184 * usb_add_function() - add a function to a configuration
185 * @config: the configuration
186 * @function: the function being added
187 * Context: single threaded during gadget setup
188 *
189 * After initialization, each configuration must have one or more
190 * functions added to it. Adding a function involves calling its @bind()
191 * method to allocate resources such as interface and string identifiers
192 * and endpoints.
193 *
194 * This function returns the value of the function's bind(), which is
195 * zero for success else a negative errno value.
196 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200197int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700198 struct usb_function *function)
199{
200 int value = -EINVAL;
201
202 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
203 function->name, function,
204 config->label, config);
205
206 if (!function->set_alt || !function->disable)
207 goto done;
208
209 function->config = config;
210 list_add_tail(&function->list, &config->functions);
211
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200212 if (function->bind_deactivated) {
213 value = usb_function_deactivate(function);
214 if (value)
215 goto done;
216 }
217
David Brownell40982be2008-06-19 17:52:58 -0700218 /* REVISIT *require* function->bind? */
219 if (function->bind) {
220 value = function->bind(config, function);
221 if (value < 0) {
222 list_del(&function->list);
223 function->config = NULL;
224 }
225 } else
226 value = 0;
227
228 /* We allow configurations that don't work at both speeds.
229 * If we run into a lowspeed Linux system, treat it the same
230 * as full speed ... it's the function drivers that will need
231 * to avoid bulk and ISO transfers.
232 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200233 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700234 config->fullspeed = true;
235 if (!config->highspeed && function->hs_descriptors)
236 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300237 if (!config->superspeed && function->ss_descriptors)
238 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700239
240done:
241 if (value)
242 DBG(config->cdev, "adding '%s'/%p --> %d\n",
243 function->name, function, value);
244 return value;
245}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200246EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700247
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100248void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
249{
250 if (f->disable)
251 f->disable(f);
252
253 bitmap_zero(f->endpoints, 32);
254 list_del(&f->list);
255 if (f->unbind)
256 f->unbind(c, f);
257}
258EXPORT_SYMBOL_GPL(usb_remove_function);
259
David Brownell40982be2008-06-19 17:52:58 -0700260/**
David Brownell60beed92008-08-18 17:38:22 -0700261 * usb_function_deactivate - prevent function and gadget enumeration
262 * @function: the function that isn't yet ready to respond
263 *
264 * Blocks response of the gadget driver to host enumeration by
265 * preventing the data line pullup from being activated. This is
266 * normally called during @bind() processing to change from the
267 * initial "ready to respond" state, or when a required resource
268 * becomes available.
269 *
270 * For example, drivers that serve as a passthrough to a userspace
271 * daemon can block enumeration unless that daemon (such as an OBEX,
272 * MTP, or print server) is ready to handle host requests.
273 *
274 * Not all systems support software control of their USB peripheral
275 * data pullups.
276 *
277 * Returns zero on success, else negative errno.
278 */
279int usb_function_deactivate(struct usb_function *function)
280{
281 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200282 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700283 int status = 0;
284
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200285 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700286
287 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200288 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700289 if (status == 0)
290 cdev->deactivations++;
291
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200292 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700293 return status;
294}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200295EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700296
297/**
298 * usb_function_activate - allow function and gadget enumeration
299 * @function: function on which usb_function_activate() was called
300 *
301 * Reverses effect of usb_function_deactivate(). If no more functions
302 * are delaying their activation, the gadget driver will respond to
303 * host enumeration procedures.
304 *
305 * Returns zero on success, else negative errno.
306 */
307int usb_function_activate(struct usb_function *function)
308{
309 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200310 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700311 int status = 0;
312
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200313 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700314
315 if (WARN_ON(cdev->deactivations == 0))
316 status = -EINVAL;
317 else {
318 cdev->deactivations--;
319 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200320 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700321 }
322
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200323 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700324 return status;
325}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200326EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700327
328/**
David Brownell40982be2008-06-19 17:52:58 -0700329 * usb_interface_id() - allocate an unused interface ID
330 * @config: configuration associated with the interface
331 * @function: function handling the interface
332 * Context: single threaded during gadget setup
333 *
334 * usb_interface_id() is called from usb_function.bind() callbacks to
335 * allocate new interface IDs. The function driver will then store that
336 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300337 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700338 * particularly changing its altsetting via set_alt(). There may
339 * also be class-specific or vendor-specific requests to handle.
340 *
341 * All interface identifier should be allocated using this routine, to
342 * ensure that for example different functions don't wrongly assign
343 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300344 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700345 * one configuration (or more than once in a given configuration) need
346 * multiple versions of the relevant descriptors.
347 *
348 * Returns the interface ID which was allocated; or -ENODEV if no
349 * more interface IDs can be allocated.
350 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200351int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700352 struct usb_function *function)
353{
354 unsigned id = config->next_interface_id;
355
356 if (id < MAX_CONFIG_INTERFACES) {
357 config->interface[id] = function;
358 config->next_interface_id = id + 1;
359 return id;
360 }
361 return -ENODEV;
362}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200363EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700364
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100365static u8 encode_bMaxPower(enum usb_device_speed speed,
366 struct usb_configuration *c)
367{
368 unsigned val;
369
370 if (c->MaxPower)
371 val = c->MaxPower;
372 else
373 val = CONFIG_USB_GADGET_VBUS_DRAW;
374 if (!val)
375 return 0;
376 switch (speed) {
377 case USB_SPEED_SUPER:
378 return DIV_ROUND_UP(val, 8);
379 default:
380 return DIV_ROUND_UP(val, 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700381 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100382}
383
David Brownell40982be2008-06-19 17:52:58 -0700384static int config_buf(struct usb_configuration *config,
385 enum usb_device_speed speed, void *buf, u8 type)
386{
387 struct usb_config_descriptor *c = buf;
388 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200389 int len;
David Brownell40982be2008-06-19 17:52:58 -0700390 struct usb_function *f;
391 int status;
392
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200393 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700394 /* write the config descriptor */
395 c = buf;
396 c->bLength = USB_DT_CONFIG_SIZE;
397 c->bDescriptorType = type;
398 /* wTotalLength is written later */
399 c->bNumInterfaces = config->next_interface_id;
400 c->bConfigurationValue = config->bConfigurationValue;
401 c->iConfiguration = config->iConfiguration;
402 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100403 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700404
405 /* There may be e.g. OTG descriptors */
406 if (config->descriptors) {
407 status = usb_descriptor_fillbuf(next, len,
408 config->descriptors);
409 if (status < 0)
410 return status;
411 len -= status;
412 next += status;
413 }
414
415 /* add each function's descriptors */
416 list_for_each_entry(f, &config->functions, list) {
417 struct usb_descriptor_header **descriptors;
418
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300419 switch (speed) {
420 case USB_SPEED_SUPER:
421 descriptors = f->ss_descriptors;
422 break;
423 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700424 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300425 break;
426 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200427 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300428 }
429
David Brownell40982be2008-06-19 17:52:58 -0700430 if (!descriptors)
431 continue;
432 status = usb_descriptor_fillbuf(next, len,
433 (const struct usb_descriptor_header **) descriptors);
434 if (status < 0)
435 return status;
436 len -= status;
437 next += status;
438 }
439
440 len = next - buf;
441 c->wTotalLength = cpu_to_le16(len);
442 return len;
443}
444
445static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
446{
447 struct usb_gadget *gadget = cdev->gadget;
448 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200449 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700450 u8 type = w_value >> 8;
451 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
452
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300453 if (gadget->speed == USB_SPEED_SUPER)
454 speed = gadget->speed;
455 else if (gadget_is_dualspeed(gadget)) {
456 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700457 if (gadget->speed == USB_SPEED_HIGH)
458 hs = 1;
459 if (type == USB_DT_OTHER_SPEED_CONFIG)
460 hs = !hs;
461 if (hs)
462 speed = USB_SPEED_HIGH;
463
464 }
465
466 /* This is a lookup by config *INDEX* */
467 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200468
469 pos = &cdev->configs;
470 c = cdev->os_desc_config;
471 if (c)
472 goto check_config;
473
474 while ((pos = pos->next) != &cdev->configs) {
475 c = list_entry(pos, typeof(*c), list);
476
477 /* skip OS Descriptors config which is handled separately */
478 if (c == cdev->os_desc_config)
479 continue;
480
481check_config:
David Brownell40982be2008-06-19 17:52:58 -0700482 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300483 switch (speed) {
484 case USB_SPEED_SUPER:
485 if (!c->superspeed)
486 continue;
487 break;
488 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700489 if (!c->highspeed)
490 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300491 break;
492 default:
David Brownell40982be2008-06-19 17:52:58 -0700493 if (!c->fullspeed)
494 continue;
495 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300496
David Brownell40982be2008-06-19 17:52:58 -0700497 if (w_value == 0)
498 return config_buf(c, speed, cdev->req->buf, type);
499 w_value--;
500 }
501 return -EINVAL;
502}
503
504static int count_configs(struct usb_composite_dev *cdev, unsigned type)
505{
506 struct usb_gadget *gadget = cdev->gadget;
507 struct usb_configuration *c;
508 unsigned count = 0;
509 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300510 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700511
512 if (gadget_is_dualspeed(gadget)) {
513 if (gadget->speed == USB_SPEED_HIGH)
514 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300515 if (gadget->speed == USB_SPEED_SUPER)
516 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700517 if (type == USB_DT_DEVICE_QUALIFIER)
518 hs = !hs;
519 }
520 list_for_each_entry(c, &cdev->configs, list) {
521 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300522 if (ss) {
523 if (!c->superspeed)
524 continue;
525 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700526 if (!c->highspeed)
527 continue;
528 } else {
529 if (!c->fullspeed)
530 continue;
531 }
532 count++;
533 }
534 return count;
535}
536
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300537/**
538 * bos_desc() - prepares the BOS descriptor.
539 * @cdev: pointer to usb_composite device to generate the bos
540 * descriptor for
541 *
542 * This function generates the BOS (Binary Device Object)
543 * descriptor and its device capabilities descriptors. The BOS
544 * descriptor should be supported by a SuperSpeed device.
545 */
546static int bos_desc(struct usb_composite_dev *cdev)
547{
548 struct usb_ext_cap_descriptor *usb_ext;
549 struct usb_ss_cap_descriptor *ss_cap;
550 struct usb_dcd_config_params dcd_config_params;
551 struct usb_bos_descriptor *bos = cdev->req->buf;
552
553 bos->bLength = USB_DT_BOS_SIZE;
554 bos->bDescriptorType = USB_DT_BOS;
555
556 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
557 bos->bNumDeviceCaps = 0;
558
559 /*
560 * A SuperSpeed device shall include the USB2.0 extension descriptor
561 * and shall support LPM when operating in USB2.0 HS mode.
562 */
563 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
564 bos->bNumDeviceCaps++;
565 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
566 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
567 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
568 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500569 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300570
571 /*
572 * The Superspeed USB Capability descriptor shall be implemented by all
573 * SuperSpeed devices.
574 */
575 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
576 bos->bNumDeviceCaps++;
577 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
578 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
579 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
580 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
581 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
582 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
583 USB_FULL_SPEED_OPERATION |
584 USB_HIGH_SPEED_OPERATION |
585 USB_5GBPS_OPERATION);
586 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
587
588 /* Get Controller configuration */
589 if (cdev->gadget->ops->get_config_params)
590 cdev->gadget->ops->get_config_params(&dcd_config_params);
591 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300592 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300593 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300594 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300595 }
596 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
597 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
598
599 return le16_to_cpu(bos->wTotalLength);
600}
601
David Brownell40982be2008-06-19 17:52:58 -0700602static void device_qual(struct usb_composite_dev *cdev)
603{
604 struct usb_qualifier_descriptor *qual = cdev->req->buf;
605
606 qual->bLength = sizeof(*qual);
607 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
608 /* POLICY: same bcdUSB and device type info at both speeds */
609 qual->bcdUSB = cdev->desc.bcdUSB;
610 qual->bDeviceClass = cdev->desc.bDeviceClass;
611 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
612 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
613 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200614 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700615 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700616 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700617}
618
619/*-------------------------------------------------------------------------*/
620
621static void reset_config(struct usb_composite_dev *cdev)
622{
623 struct usb_function *f;
624
625 DBG(cdev, "reset config\n");
626
627 list_for_each_entry(f, &cdev->config->functions, list) {
628 if (f->disable)
629 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200630
631 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700632 }
633 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100634 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700635}
636
637static int set_config(struct usb_composite_dev *cdev,
638 const struct usb_ctrlrequest *ctrl, unsigned number)
639{
640 struct usb_gadget *gadget = cdev->gadget;
641 struct usb_configuration *c = NULL;
642 int result = -EINVAL;
643 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
644 int tmp;
645
David Brownell40982be2008-06-19 17:52:58 -0700646 if (number) {
647 list_for_each_entry(c, &cdev->configs, list) {
648 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300649 /*
650 * We disable the FDs of the previous
651 * configuration only if the new configuration
652 * is a valid one
653 */
654 if (cdev->config)
655 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700656 result = 0;
657 break;
658 }
659 }
660 if (result < 0)
661 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300662 } else { /* Zero configuration value - need to reset the config */
663 if (cdev->config)
664 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700665 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300666 }
David Brownell40982be2008-06-19 17:52:58 -0700667
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200668 INFO(cdev, "%s config #%d: %s\n",
669 usb_speed_string(gadget->speed),
670 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700671
672 if (!c)
673 goto done;
674
Peter Chen6027f312014-04-29 13:26:28 +0800675 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700676 cdev->config = c;
677
678 /* Initialize all interfaces by setting them to altsetting zero. */
679 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
680 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200681 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700682
683 if (!f)
684 break;
685
Laurent Pinchart52426582009-10-21 00:03:38 +0200686 /*
687 * Record which endpoints are used by the function. This is used
688 * to dispatch control requests targeted at that endpoint to the
689 * function's setup callback instead of the current
690 * configuration's setup callback.
691 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300692 switch (gadget->speed) {
693 case USB_SPEED_SUPER:
694 descriptors = f->ss_descriptors;
695 break;
696 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200697 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300698 break;
699 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200700 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300701 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200702
703 for (; *descriptors; ++descriptors) {
704 struct usb_endpoint_descriptor *ep;
705 int addr;
706
707 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
708 continue;
709
710 ep = (struct usb_endpoint_descriptor *)*descriptors;
711 addr = ((ep->bEndpointAddress & 0x80) >> 3)
712 | (ep->bEndpointAddress & 0x0f);
713 set_bit(addr, f->endpoints);
714 }
715
David Brownell40982be2008-06-19 17:52:58 -0700716 result = f->set_alt(f, tmp, 0);
717 if (result < 0) {
718 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
719 tmp, f->name, f, result);
720
721 reset_config(cdev);
722 goto done;
723 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300724
725 if (result == USB_GADGET_DELAYED_STATUS) {
726 DBG(cdev,
727 "%s: interface %d (%s) requested delayed status\n",
728 __func__, tmp, f->name);
729 cdev->delayed_status++;
730 DBG(cdev, "delayed_status count %d\n",
731 cdev->delayed_status);
732 }
David Brownell40982be2008-06-19 17:52:58 -0700733 }
734
735 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100736 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700737done:
738 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300739 if (result >= 0 && cdev->delayed_status)
740 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700741 return result;
742}
743
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100744int usb_add_config_only(struct usb_composite_dev *cdev,
745 struct usb_configuration *config)
746{
747 struct usb_configuration *c;
748
749 if (!config->bConfigurationValue)
750 return -EINVAL;
751
752 /* Prevent duplicate configuration identifiers */
753 list_for_each_entry(c, &cdev->configs, list) {
754 if (c->bConfigurationValue == config->bConfigurationValue)
755 return -EBUSY;
756 }
757
758 config->cdev = cdev;
759 list_add_tail(&config->list, &cdev->configs);
760
761 INIT_LIST_HEAD(&config->functions);
762 config->next_interface_id = 0;
763 memset(config->interface, 0, sizeof(config->interface));
764
765 return 0;
766}
767EXPORT_SYMBOL_GPL(usb_add_config_only);
768
David Brownell40982be2008-06-19 17:52:58 -0700769/**
770 * usb_add_config() - add a configuration to a device.
771 * @cdev: wraps the USB gadget
772 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200773 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700774 * Context: single threaded during gadget setup
775 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200776 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700777 * add each of the configurations it supports, using this routine.
778 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200779 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700780 * is zero for success else a negative errno value. Binding configurations
781 * assigns global resources including string IDs, and per-configuration
782 * resources such as interface IDs and endpoints.
783 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200784int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200785 struct usb_configuration *config,
786 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700787{
788 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100789
790 if (!bind)
791 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700792
793 DBG(cdev, "adding config #%u '%s'/%p\n",
794 config->bConfigurationValue,
795 config->label, config);
796
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100797 status = usb_add_config_only(cdev, config);
798 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700799 goto done;
800
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200801 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700802 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900803 while (!list_empty(&config->functions)) {
804 struct usb_function *f;
805
806 f = list_first_entry(&config->functions,
807 struct usb_function, list);
808 list_del(&f->list);
809 if (f->unbind) {
810 DBG(cdev, "unbind function '%s'/%p\n",
811 f->name, f);
812 f->unbind(config, f);
813 /* may free memory for "f" */
814 }
815 }
David Brownell40982be2008-06-19 17:52:58 -0700816 list_del(&config->list);
817 config->cdev = NULL;
818 } else {
819 unsigned i;
820
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300821 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700822 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300823 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700824 config->highspeed ? " high" : "",
825 config->fullspeed
826 ? (gadget_is_dualspeed(cdev->gadget)
827 ? " full"
828 : " full/low")
829 : "");
830
831 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
832 struct usb_function *f = config->interface[i];
833
834 if (!f)
835 continue;
836 DBG(cdev, " interface %d = %s/%p\n",
837 i, f->name, f);
838 }
839 }
840
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200841 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700842 * ep->driver_data as needed.
843 */
844 usb_ep_autoconfig_reset(cdev->gadget);
845
846done:
847 if (status)
848 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
849 config->bConfigurationValue, status);
850 return status;
851}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200852EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700853
Benoit Goby51cce6f2012-05-10 10:07:57 +0200854static void remove_config(struct usb_composite_dev *cdev,
855 struct usb_configuration *config)
856{
857 while (!list_empty(&config->functions)) {
858 struct usb_function *f;
859
860 f = list_first_entry(&config->functions,
861 struct usb_function, list);
862 list_del(&f->list);
863 if (f->unbind) {
864 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
865 f->unbind(config, f);
866 /* may free memory for "f" */
867 }
868 }
869 list_del(&config->list);
870 if (config->unbind) {
871 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
872 config->unbind(config);
873 /* may free memory for "c" */
874 }
875}
876
877/**
878 * usb_remove_config() - remove a configuration from a device.
879 * @cdev: wraps the USB gadget
880 * @config: the configuration
881 *
882 * Drivers must call usb_gadget_disconnect before calling this function
883 * to disconnect the device from the host and make sure the host will not
884 * try to enumerate the device while we are changing the config list.
885 */
886void usb_remove_config(struct usb_composite_dev *cdev,
887 struct usb_configuration *config)
888{
889 unsigned long flags;
890
891 spin_lock_irqsave(&cdev->lock, flags);
892
893 if (cdev->config == config)
894 reset_config(cdev);
895
896 spin_unlock_irqrestore(&cdev->lock, flags);
897
898 remove_config(cdev, config);
899}
900
David Brownell40982be2008-06-19 17:52:58 -0700901/*-------------------------------------------------------------------------*/
902
903/* We support strings in multiple languages ... string descriptor zero
904 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -0300905 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -0700906 * the host side.
907 */
908
909static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
910{
911 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300912 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700913 __le16 *tmp;
914
915 while (*sp) {
916 s = *sp;
917 language = cpu_to_le16(s->language);
918 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
919 if (*tmp == language)
920 goto repeat;
921 }
922 *tmp++ = language;
923repeat:
924 sp++;
925 }
926}
927
928static int lookup_string(
929 struct usb_gadget_strings **sp,
930 void *buf,
931 u16 language,
932 int id
933)
934{
935 struct usb_gadget_strings *s;
936 int value;
937
938 while (*sp) {
939 s = *sp++;
940 if (s->language != language)
941 continue;
942 value = usb_gadget_get_string(s, id, buf);
943 if (value > 0)
944 return value;
945 }
946 return -EINVAL;
947}
948
949static int get_string(struct usb_composite_dev *cdev,
950 void *buf, u16 language, int id)
951{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200952 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +0100953 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -0700954 struct usb_configuration *c;
955 struct usb_function *f;
956 int len;
957
Diego Violaad4676a2015-05-31 15:52:41 -0300958 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -0700959 * folk will ever care about ... also, it's all supported here.
960 * (Except for UTF8 support for Unicode's "Astral Planes".)
961 */
962
963 /* 0 == report all available language codes */
964 if (id == 0) {
965 struct usb_string_descriptor *s = buf;
966 struct usb_gadget_strings **sp;
967
968 memset(s, 0, 256);
969 s->bDescriptorType = USB_DT_STRING;
970
971 sp = composite->strings;
972 if (sp)
973 collect_langs(sp, s->wData);
974
975 list_for_each_entry(c, &cdev->configs, list) {
976 sp = c->strings;
977 if (sp)
978 collect_langs(sp, s->wData);
979
980 list_for_each_entry(f, &c->functions, list) {
981 sp = f->strings;
982 if (sp)
983 collect_langs(sp, s->wData);
984 }
985 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +0100986 list_for_each_entry(uc, &cdev->gstrings, list) {
987 struct usb_gadget_strings **sp;
988
989 sp = get_containers_gs(uc);
990 collect_langs(sp, s->wData);
991 }
David Brownell40982be2008-06-19 17:52:58 -0700992
Roel Kluin417b57b2009-08-06 16:09:51 -0700993 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700994 continue;
995 if (!len)
996 return -EINVAL;
997
998 s->bLength = 2 * (len + 1);
999 return s->bLength;
1000 }
1001
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001002 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1003 struct usb_os_string *b = buf;
1004 b->bLength = sizeof(*b);
1005 b->bDescriptorType = USB_DT_STRING;
1006 compiletime_assert(
1007 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1008 "qwSignature size must be equal to qw_sign");
1009 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1010 b->bMS_VendorCode = cdev->b_vendor_code;
1011 b->bPad = 0;
1012 return sizeof(*b);
1013 }
1014
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001015 list_for_each_entry(uc, &cdev->gstrings, list) {
1016 struct usb_gadget_strings **sp;
1017
1018 sp = get_containers_gs(uc);
1019 len = lookup_string(sp, buf, language, id);
1020 if (len > 0)
1021 return len;
1022 }
1023
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001024 /* String IDs are device-scoped, so we look up each string
1025 * table we're told about. These lookups are infrequent;
1026 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001027 */
1028 if (composite->strings) {
1029 len = lookup_string(composite->strings, buf, language, id);
1030 if (len > 0)
1031 return len;
1032 }
1033 list_for_each_entry(c, &cdev->configs, list) {
1034 if (c->strings) {
1035 len = lookup_string(c->strings, buf, language, id);
1036 if (len > 0)
1037 return len;
1038 }
1039 list_for_each_entry(f, &c->functions, list) {
1040 if (!f->strings)
1041 continue;
1042 len = lookup_string(f->strings, buf, language, id);
1043 if (len > 0)
1044 return len;
1045 }
1046 }
1047 return -EINVAL;
1048}
1049
1050/**
1051 * usb_string_id() - allocate an unused string ID
1052 * @cdev: the device whose string descriptor IDs are being allocated
1053 * Context: single threaded during gadget setup
1054 *
1055 * @usb_string_id() is called from bind() callbacks to allocate
1056 * string IDs. Drivers for functions, configurations, or gadgets will
1057 * then store that ID in the appropriate descriptors and string table.
1058 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001059 * All string identifier should be allocated using this,
1060 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1061 * that for example different functions don't wrongly assign different
1062 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001063 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001064int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001065{
1066 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001067 /* string id 0 is reserved by USB spec for list of
1068 * supported languages */
1069 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001070 cdev->next_string_id++;
1071 return cdev->next_string_id;
1072 }
1073 return -ENODEV;
1074}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001075EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001076
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001077/**
1078 * usb_string_ids() - allocate unused string IDs in batch
1079 * @cdev: the device whose string descriptor IDs are being allocated
1080 * @str: an array of usb_string objects to assign numbers to
1081 * Context: single threaded during gadget setup
1082 *
1083 * @usb_string_ids() is called from bind() callbacks to allocate
1084 * string IDs. Drivers for functions, configurations, or gadgets will
1085 * then copy IDs from the string table to the appropriate descriptors
1086 * and string table for other languages.
1087 *
1088 * All string identifier should be allocated using this,
1089 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1090 * example different functions don't wrongly assign different meanings
1091 * to the same identifier.
1092 */
1093int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1094{
1095 int next = cdev->next_string_id;
1096
1097 for (; str->s; ++str) {
1098 if (unlikely(next >= 254))
1099 return -ENODEV;
1100 str->id = ++next;
1101 }
1102
1103 cdev->next_string_id = next;
1104
1105 return 0;
1106}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001107EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001108
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001109static struct usb_gadget_string_container *copy_gadget_strings(
1110 struct usb_gadget_strings **sp, unsigned n_gstrings,
1111 unsigned n_strings)
1112{
1113 struct usb_gadget_string_container *uc;
1114 struct usb_gadget_strings **gs_array;
1115 struct usb_gadget_strings *gs;
1116 struct usb_string *s;
1117 unsigned mem;
1118 unsigned n_gs;
1119 unsigned n_s;
1120 void *stash;
1121
1122 mem = sizeof(*uc);
1123 mem += sizeof(void *) * (n_gstrings + 1);
1124 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1125 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1126 uc = kmalloc(mem, GFP_KERNEL);
1127 if (!uc)
1128 return ERR_PTR(-ENOMEM);
1129 gs_array = get_containers_gs(uc);
1130 stash = uc->stash;
1131 stash += sizeof(void *) * (n_gstrings + 1);
1132 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1133 struct usb_string *org_s;
1134
1135 gs_array[n_gs] = stash;
1136 gs = gs_array[n_gs];
1137 stash += sizeof(struct usb_gadget_strings);
1138 gs->language = sp[n_gs]->language;
1139 gs->strings = stash;
1140 org_s = sp[n_gs]->strings;
1141
1142 for (n_s = 0; n_s < n_strings; n_s++) {
1143 s = stash;
1144 stash += sizeof(struct usb_string);
1145 if (org_s->s)
1146 s->s = org_s->s;
1147 else
1148 s->s = "";
1149 org_s++;
1150 }
1151 s = stash;
1152 s->s = NULL;
1153 stash += sizeof(struct usb_string);
1154
1155 }
1156 gs_array[n_gs] = NULL;
1157 return uc;
1158}
1159
1160/**
1161 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1162 * @cdev: the device whose string descriptor IDs are being allocated
1163 * and attached.
1164 * @sp: an array of usb_gadget_strings to attach.
1165 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1166 *
1167 * This function will create a deep copy of usb_gadget_strings and usb_string
1168 * and attach it to the cdev. The actual string (usb_string.s) will not be
1169 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001170 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001171 * The ->language pointer of each struct usb_gadget_strings has to contain the
1172 * same amount of entries.
1173 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
Masanari Iida06ed0de2015-03-10 22:37:46 +09001174 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001175 * entry of en-US. Therefore both entries become the same id assign.
1176 */
1177struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1178 struct usb_gadget_strings **sp, unsigned n_strings)
1179{
1180 struct usb_gadget_string_container *uc;
1181 struct usb_gadget_strings **n_gs;
1182 unsigned n_gstrings = 0;
1183 unsigned i;
1184 int ret;
1185
1186 for (i = 0; sp[i]; i++)
1187 n_gstrings++;
1188
1189 if (!n_gstrings)
1190 return ERR_PTR(-EINVAL);
1191
1192 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1193 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001194 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001195
1196 n_gs = get_containers_gs(uc);
1197 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1198 if (ret)
1199 goto err;
1200
1201 for (i = 1; i < n_gstrings; i++) {
1202 struct usb_string *m_s;
1203 struct usb_string *s;
1204 unsigned n;
1205
1206 m_s = n_gs[0]->strings;
1207 s = n_gs[i]->strings;
1208 for (n = 0; n < n_strings; n++) {
1209 s->id = m_s->id;
1210 s++;
1211 m_s++;
1212 }
1213 }
1214 list_add_tail(&uc->list, &cdev->gstrings);
1215 return n_gs[0]->strings;
1216err:
1217 kfree(uc);
1218 return ERR_PTR(ret);
1219}
1220EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1221
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001222/**
1223 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001224 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001225 * @n: number of string IDs to allocate
1226 * Context: single threaded during gadget setup
1227 *
1228 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001229 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001230 * is, returns last requested ID which is now very useful information.
1231 *
1232 * @usb_string_ids_n() is called from bind() callbacks to allocate
1233 * string IDs. Drivers for functions, configurations, or gadgets will
1234 * then store that ID in the appropriate descriptors and string table.
1235 *
1236 * All string identifier should be allocated using this,
1237 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1238 * example different functions don't wrongly assign different meanings
1239 * to the same identifier.
1240 */
1241int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1242{
1243 unsigned next = c->next_string_id;
1244 if (unlikely(n > 254 || (unsigned)next + n > 254))
1245 return -ENODEV;
1246 c->next_string_id += n;
1247 return next + 1;
1248}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001249EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001250
David Brownell40982be2008-06-19 17:52:58 -07001251/*-------------------------------------------------------------------------*/
1252
1253static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1254{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001255 struct usb_composite_dev *cdev;
1256
David Brownell40982be2008-06-19 17:52:58 -07001257 if (req->status || req->actual != req->length)
1258 DBG((struct usb_composite_dev *) ep->driver_data,
1259 "setup complete --> %d, %d/%d\n",
1260 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001261
1262 /*
1263 * REVIST The same ep0 requests are shared with function drivers
1264 * so they don't have to maintain the same ->complete() stubs.
1265 *
1266 * Because of that, we need to check for the validity of ->context
1267 * here, even though we know we've set it to something useful.
1268 */
1269 if (!req->context)
1270 return;
1271
1272 cdev = req->context;
1273
1274 if (cdev->req == req)
1275 cdev->setup_pending = false;
1276 else if (cdev->os_desc_req == req)
1277 cdev->os_desc_pending = false;
1278 else
1279 WARN(1, "unknown request %p\n", req);
1280}
1281
1282static int composite_ep0_queue(struct usb_composite_dev *cdev,
1283 struct usb_request *req, gfp_t gfp_flags)
1284{
1285 int ret;
1286
1287 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1288 if (ret == 0) {
1289 if (cdev->req == req)
1290 cdev->setup_pending = true;
1291 else if (cdev->os_desc_req == req)
1292 cdev->os_desc_pending = true;
1293 else
1294 WARN(1, "unknown request %p\n", req);
1295 }
1296
1297 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001298}
1299
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001300static int count_ext_compat(struct usb_configuration *c)
1301{
1302 int i, res;
1303
1304 res = 0;
1305 for (i = 0; i < c->next_interface_id; ++i) {
1306 struct usb_function *f;
1307 int j;
1308
1309 f = c->interface[i];
1310 for (j = 0; j < f->os_desc_n; ++j) {
1311 struct usb_os_desc *d;
1312
1313 if (i != f->os_desc_table[j].if_id)
1314 continue;
1315 d = f->os_desc_table[j].os_desc;
1316 if (d && d->ext_compat_id)
1317 ++res;
1318 }
1319 }
1320 BUG_ON(res > 255);
1321 return res;
1322}
1323
1324static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1325{
1326 int i, count;
1327
1328 count = 16;
1329 for (i = 0; i < c->next_interface_id; ++i) {
1330 struct usb_function *f;
1331 int j;
1332
1333 f = c->interface[i];
1334 for (j = 0; j < f->os_desc_n; ++j) {
1335 struct usb_os_desc *d;
1336
1337 if (i != f->os_desc_table[j].if_id)
1338 continue;
1339 d = f->os_desc_table[j].os_desc;
1340 if (d && d->ext_compat_id) {
1341 *buf++ = i;
1342 *buf++ = 0x01;
1343 memcpy(buf, d->ext_compat_id, 16);
1344 buf += 22;
1345 } else {
1346 ++buf;
1347 *buf = 0x01;
1348 buf += 23;
1349 }
1350 count += 24;
1351 if (count >= 4096)
1352 return;
1353 }
1354 }
1355}
1356
1357static int count_ext_prop(struct usb_configuration *c, int interface)
1358{
1359 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001360 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001361
1362 f = c->interface[interface];
1363 for (j = 0; j < f->os_desc_n; ++j) {
1364 struct usb_os_desc *d;
1365
1366 if (interface != f->os_desc_table[j].if_id)
1367 continue;
1368 d = f->os_desc_table[j].os_desc;
1369 if (d && d->ext_compat_id)
1370 return d->ext_prop_count;
1371 }
Julia Lawall849b1332014-05-19 06:31:07 +02001372 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001373}
1374
1375static int len_ext_prop(struct usb_configuration *c, int interface)
1376{
1377 struct usb_function *f;
1378 struct usb_os_desc *d;
1379 int j, res;
1380
1381 res = 10; /* header length */
1382 f = c->interface[interface];
1383 for (j = 0; j < f->os_desc_n; ++j) {
1384 if (interface != f->os_desc_table[j].if_id)
1385 continue;
1386 d = f->os_desc_table[j].os_desc;
1387 if (d)
1388 return min(res + d->ext_prop_len, 4096);
1389 }
1390 return res;
1391}
1392
1393static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1394{
1395 struct usb_function *f;
1396 struct usb_os_desc *d;
1397 struct usb_os_desc_ext_prop *ext_prop;
1398 int j, count, n, ret;
1399 u8 *start = buf;
1400
1401 f = c->interface[interface];
1402 for (j = 0; j < f->os_desc_n; ++j) {
1403 if (interface != f->os_desc_table[j].if_id)
1404 continue;
1405 d = f->os_desc_table[j].os_desc;
1406 if (d)
1407 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1408 /* 4kB minus header length */
1409 n = buf - start;
1410 if (n >= 4086)
1411 return 0;
1412
1413 count = ext_prop->data_len +
1414 ext_prop->name_len + 14;
1415 if (count > 4086 - n)
1416 return -EINVAL;
1417 usb_ext_prop_put_size(buf, count);
1418 usb_ext_prop_put_type(buf, ext_prop->type);
1419 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1420 ext_prop->name_len);
1421 if (ret < 0)
1422 return ret;
1423 switch (ext_prop->type) {
1424 case USB_EXT_PROP_UNICODE:
1425 case USB_EXT_PROP_UNICODE_ENV:
1426 case USB_EXT_PROP_UNICODE_LINK:
1427 usb_ext_prop_put_unicode(buf, ret,
1428 ext_prop->data,
1429 ext_prop->data_len);
1430 break;
1431 case USB_EXT_PROP_BINARY:
1432 usb_ext_prop_put_binary(buf, ret,
1433 ext_prop->data,
1434 ext_prop->data_len);
1435 break;
1436 case USB_EXT_PROP_LE32:
1437 /* not implemented */
1438 case USB_EXT_PROP_BE32:
1439 /* not implemented */
1440 default:
1441 return -EINVAL;
1442 }
1443 buf += count;
1444 }
1445 }
1446
1447 return 0;
1448}
1449
David Brownell40982be2008-06-19 17:52:58 -07001450/*
1451 * The setup() callback implements all the ep0 functionality that's
1452 * not handled lower down, in hardware or the hardware driver(like
1453 * device and endpoint feature flags, and their status). It's all
1454 * housekeeping for the gadget function we're implementing. Most of
1455 * the work is in config and function specific setup.
1456 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001457int
David Brownell40982be2008-06-19 17:52:58 -07001458composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1459{
1460 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1461 struct usb_request *req = cdev->req;
1462 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001463 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001464 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001465 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001466 u16 w_value = le16_to_cpu(ctrl->wValue);
1467 u16 w_length = le16_to_cpu(ctrl->wLength);
1468 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001469 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001470
1471 /* partial re-init of the response message; the function or the
1472 * gadget might need to intercept e.g. a control-OUT completion
1473 * when we delegate to it.
1474 */
1475 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001476 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001477 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301478 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001479 gadget->ep0->driver_data = cdev;
1480
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001481 /*
1482 * Don't let non-standard requests match any of the cases below
1483 * by accident.
1484 */
1485 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1486 goto unknown;
1487
David Brownell40982be2008-06-19 17:52:58 -07001488 switch (ctrl->bRequest) {
1489
1490 /* we handle all standard USB descriptors */
1491 case USB_REQ_GET_DESCRIPTOR:
1492 if (ctrl->bRequestType != USB_DIR_IN)
1493 goto unknown;
1494 switch (w_value >> 8) {
1495
1496 case USB_DT_DEVICE:
1497 cdev->desc.bNumConfigurations =
1498 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001499 cdev->desc.bMaxPacketSize0 =
1500 cdev->gadget->ep0->maxpacket;
1501 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001502 if (gadget->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001503 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001504 cdev->desc.bMaxPacketSize0 = 9;
1505 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001506 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001507 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001508 }
1509
David Brownell40982be2008-06-19 17:52:58 -07001510 value = min(w_length, (u16) sizeof cdev->desc);
1511 memcpy(req->buf, &cdev->desc, value);
1512 break;
1513 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001514 if (!gadget_is_dualspeed(gadget) ||
1515 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001516 break;
1517 device_qual(cdev);
1518 value = min_t(int, w_length,
1519 sizeof(struct usb_qualifier_descriptor));
1520 break;
1521 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001522 if (!gadget_is_dualspeed(gadget) ||
1523 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001524 break;
1525 /* FALLTHROUGH */
1526 case USB_DT_CONFIG:
1527 value = config_desc(cdev, w_value);
1528 if (value >= 0)
1529 value = min(w_length, (u16) value);
1530 break;
1531 case USB_DT_STRING:
1532 value = get_string(cdev, req->buf,
1533 w_index, w_value & 0xff);
1534 if (value >= 0)
1535 value = min(w_length, (u16) value);
1536 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001537 case USB_DT_BOS:
1538 if (gadget_is_superspeed(gadget)) {
1539 value = bos_desc(cdev);
1540 value = min(w_length, (u16) value);
1541 }
1542 break;
David Brownell40982be2008-06-19 17:52:58 -07001543 }
1544 break;
1545
1546 /* any number of configs can work */
1547 case USB_REQ_SET_CONFIGURATION:
1548 if (ctrl->bRequestType != 0)
1549 goto unknown;
1550 if (gadget_is_otg(gadget)) {
1551 if (gadget->a_hnp_support)
1552 DBG(cdev, "HNP available\n");
1553 else if (gadget->a_alt_hnp_support)
1554 DBG(cdev, "HNP on another port\n");
1555 else
1556 VDBG(cdev, "HNP inactive\n");
1557 }
1558 spin_lock(&cdev->lock);
1559 value = set_config(cdev, ctrl, w_value);
1560 spin_unlock(&cdev->lock);
1561 break;
1562 case USB_REQ_GET_CONFIGURATION:
1563 if (ctrl->bRequestType != USB_DIR_IN)
1564 goto unknown;
1565 if (cdev->config)
1566 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1567 else
1568 *(u8 *)req->buf = 0;
1569 value = min(w_length, (u16) 1);
1570 break;
1571
1572 /* function drivers must handle get/set altsetting; if there's
1573 * no get() method, we know only altsetting zero works.
1574 */
1575 case USB_REQ_SET_INTERFACE:
1576 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1577 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001578 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001579 break;
Bryan Wu08889512009-01-08 00:21:19 +08001580 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001581 if (!f)
1582 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001583 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001584 break;
1585 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001586 if (value == USB_GADGET_DELAYED_STATUS) {
1587 DBG(cdev,
1588 "%s: interface %d (%s) requested delayed status\n",
1589 __func__, intf, f->name);
1590 cdev->delayed_status++;
1591 DBG(cdev, "delayed_status count %d\n",
1592 cdev->delayed_status);
1593 }
David Brownell40982be2008-06-19 17:52:58 -07001594 break;
1595 case USB_REQ_GET_INTERFACE:
1596 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1597 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001598 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001599 break;
Bryan Wu08889512009-01-08 00:21:19 +08001600 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001601 if (!f)
1602 break;
1603 /* lots of interfaces only need altsetting zero... */
1604 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1605 if (value < 0)
1606 break;
1607 *((u8 *)req->buf) = value;
1608 value = min(w_length, (u16) 1);
1609 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001610
1611 /*
1612 * USB 3.0 additions:
1613 * Function driver should handle get_status request. If such cb
1614 * wasn't supplied we respond with default value = 0
1615 * Note: function driver should supply such cb only for the first
1616 * interface of the function
1617 */
1618 case USB_REQ_GET_STATUS:
1619 if (!gadget_is_superspeed(gadget))
1620 goto unknown;
1621 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1622 goto unknown;
1623 value = 2; /* This is the length of the get_status reply */
1624 put_unaligned_le16(0, req->buf);
1625 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1626 break;
1627 f = cdev->config->interface[intf];
1628 if (!f)
1629 break;
1630 status = f->get_status ? f->get_status(f) : 0;
1631 if (status < 0)
1632 break;
1633 put_unaligned_le16(status & 0x0000ffff, req->buf);
1634 break;
1635 /*
1636 * Function drivers should handle SetFeature/ClearFeature
1637 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1638 * only for the first interface of the function
1639 */
1640 case USB_REQ_CLEAR_FEATURE:
1641 case USB_REQ_SET_FEATURE:
1642 if (!gadget_is_superspeed(gadget))
1643 goto unknown;
1644 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1645 goto unknown;
1646 switch (w_value) {
1647 case USB_INTRF_FUNC_SUSPEND:
1648 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1649 break;
1650 f = cdev->config->interface[intf];
1651 if (!f)
1652 break;
1653 value = 0;
1654 if (f->func_suspend)
1655 value = f->func_suspend(f, w_index >> 8);
1656 if (value < 0) {
1657 ERROR(cdev,
1658 "func_suspend() returned error %d\n",
1659 value);
1660 value = 0;
1661 }
1662 break;
1663 }
1664 break;
David Brownell40982be2008-06-19 17:52:58 -07001665 default:
1666unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001667 /*
1668 * OS descriptors handling
1669 */
1670 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001671 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001672 ctrl->bRequest == cdev->b_vendor_code) {
1673 struct usb_request *req;
1674 struct usb_configuration *os_desc_cfg;
1675 u8 *buf;
1676 int interface;
1677 int count = 0;
1678
1679 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001680 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001681 req->complete = composite_setup_complete;
1682 buf = req->buf;
1683 os_desc_cfg = cdev->os_desc_config;
1684 memset(buf, 0, w_length);
1685 buf[5] = 0x01;
1686 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1687 case USB_RECIP_DEVICE:
1688 if (w_index != 0x4 || (w_value >> 8))
1689 break;
1690 buf[6] = w_index;
1691 if (w_length == 0x10) {
1692 /* Number of ext compat interfaces */
1693 count = count_ext_compat(os_desc_cfg);
1694 buf[8] = count;
1695 count *= 24; /* 24 B/ext compat desc */
1696 count += 16; /* header */
1697 put_unaligned_le32(count, buf);
1698 value = w_length;
1699 } else {
1700 /* "extended compatibility ID"s */
1701 count = count_ext_compat(os_desc_cfg);
1702 buf[8] = count;
1703 count *= 24; /* 24 B/ext compat desc */
1704 count += 16; /* header */
1705 put_unaligned_le32(count, buf);
1706 buf += 16;
1707 fill_ext_compat(os_desc_cfg, buf);
1708 value = w_length;
1709 }
1710 break;
1711 case USB_RECIP_INTERFACE:
1712 if (w_index != 0x5 || (w_value >> 8))
1713 break;
1714 interface = w_value & 0xFF;
1715 buf[6] = w_index;
1716 if (w_length == 0x0A) {
1717 count = count_ext_prop(os_desc_cfg,
1718 interface);
1719 put_unaligned_le16(count, buf + 8);
1720 count = len_ext_prop(os_desc_cfg,
1721 interface);
1722 put_unaligned_le32(count, buf);
1723
1724 value = w_length;
1725 } else {
1726 count = count_ext_prop(os_desc_cfg,
1727 interface);
1728 put_unaligned_le16(count, buf + 8);
1729 count = len_ext_prop(os_desc_cfg,
1730 interface);
1731 put_unaligned_le32(count, buf);
1732 buf += 10;
1733 value = fill_ext_prop(os_desc_cfg,
1734 interface, buf);
1735 if (value < 0)
1736 return value;
1737
1738 value = w_length;
1739 }
1740 break;
1741 }
1742 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001743 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001744 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001745 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001746 if (value < 0) {
1747 DBG(cdev, "ep_queue --> %d\n", value);
1748 req->status = 0;
1749 composite_setup_complete(gadget->ep0, req);
1750 }
1751 return value;
1752 }
1753
David Brownell40982be2008-06-19 17:52:58 -07001754 VDBG(cdev,
1755 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1756 ctrl->bRequestType, ctrl->bRequest,
1757 w_value, w_index, w_length);
1758
Laurent Pinchart52426582009-10-21 00:03:38 +02001759 /* functions always handle their interfaces and endpoints...
1760 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001761 * configuration code.
1762 *
1763 * REVISIT it could make sense to let the composite device
1764 * take such requests too, if that's ever needed: to work
1765 * in config 0, etc.
1766 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301767 if (cdev->config) {
1768 list_for_each_entry(f, &cdev->config->functions, list)
1769 if (f->req_match && f->req_match(f, ctrl))
1770 goto try_fun_setup;
1771 f = NULL;
1772 }
1773
Laurent Pinchart52426582009-10-21 00:03:38 +02001774 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1775 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001776 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301777 break;
1778 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001779 break;
1780
1781 case USB_RECIP_ENDPOINT:
1782 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1783 list_for_each_entry(f, &cdev->config->functions, list) {
1784 if (test_bit(endp, f->endpoints))
1785 break;
1786 }
1787 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001788 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001789 break;
David Brownell40982be2008-06-19 17:52:58 -07001790 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01001791try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02001792 if (f && f->setup)
1793 value = f->setup(f, ctrl);
1794 else {
David Brownell40982be2008-06-19 17:52:58 -07001795 struct usb_configuration *c;
1796
1797 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001798 if (!c)
1799 goto done;
1800
1801 /* try current config's setup */
1802 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07001803 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001804 goto done;
1805 }
1806
1807 /* try the only function in the current config */
1808 if (!list_is_singular(&c->functions))
1809 goto done;
1810 f = list_first_entry(&c->functions, struct usb_function,
1811 list);
1812 if (f->setup)
1813 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07001814 }
1815
1816 goto done;
1817 }
1818
1819 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001820 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001821 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001822 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001823 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001824 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07001825 if (value < 0) {
1826 DBG(cdev, "ep_queue --> %d\n", value);
1827 req->status = 0;
1828 composite_setup_complete(gadget->ep0, req);
1829 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001830 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1831 WARN(cdev,
1832 "%s: Delayed status not supported for w_length != 0",
1833 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001834 }
1835
1836done:
1837 /* device either stalls (value < 0) or reports success */
1838 return value;
1839}
1840
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001841void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001842{
1843 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1844 unsigned long flags;
1845
1846 /* REVISIT: should we have config and device level
1847 * disconnect callbacks?
1848 */
1849 spin_lock_irqsave(&cdev->lock, flags);
1850 if (cdev->config)
1851 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001852 if (cdev->driver->disconnect)
1853 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001854 spin_unlock_irqrestore(&cdev->lock, flags);
1855}
1856
1857/*-------------------------------------------------------------------------*/
1858
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001859static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1860 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001861{
1862 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1863 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1864
1865 return sprintf(buf, "%d\n", cdev->suspended);
1866}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001867static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001868
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001869static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07001870{
1871 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1872
1873 /* composite_disconnect() must already have been called
1874 * by the underlying peripheral controller driver!
1875 * so there's no i/o concurrency that could affect the
1876 * state protected by cdev->lock.
1877 */
1878 WARN_ON(cdev->config);
1879
1880 while (!list_empty(&cdev->configs)) {
1881 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001882 c = list_first_entry(&cdev->configs,
1883 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001884 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001885 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001886 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001887 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001888
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001889 composite_dev_cleanup(cdev);
1890
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001891 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001892 kfree(cdev);
1893 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001894}
1895
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001896static void composite_unbind(struct usb_gadget *gadget)
1897{
1898 __composite_unbind(gadget, true);
1899}
1900
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001901static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1902 const struct usb_device_descriptor *old)
1903{
1904 __le16 idVendor;
1905 __le16 idProduct;
1906 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001907 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001908 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001909 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001910
1911 /*
1912 * these variables may have been set in
1913 * usb_composite_overwrite_options()
1914 */
1915 idVendor = new->idVendor;
1916 idProduct = new->idProduct;
1917 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001918 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001919 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001920 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001921
1922 *new = *old;
1923 if (idVendor)
1924 new->idVendor = idVendor;
1925 if (idProduct)
1926 new->idProduct = idProduct;
1927 if (bcdDevice)
1928 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02001929 else
1930 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001931 if (iSerialNumber)
1932 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001933 if (iManufacturer)
1934 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001935 if (iProduct)
1936 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001937}
1938
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001939int composite_dev_prepare(struct usb_composite_driver *composite,
1940 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001941{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001942 struct usb_gadget *gadget = cdev->gadget;
1943 int ret = -ENOMEM;
1944
1945 /* preallocate control response and buffer */
1946 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1947 if (!cdev->req)
1948 return -ENOMEM;
1949
1950 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
1951 if (!cdev->req->buf)
1952 goto fail;
1953
1954 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
1955 if (ret)
1956 goto fail_dev;
1957
1958 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05001959 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001960 gadget->ep0->driver_data = cdev;
1961
1962 cdev->driver = composite;
1963
1964 /*
1965 * As per USB compliance update, a device that is actively drawing
1966 * more than 100mA from USB must report itself as bus-powered in
1967 * the GetStatus(DEVICE) call.
1968 */
1969 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1970 usb_gadget_set_selfpowered(gadget);
1971
1972 /* interface and string IDs start at zero via kzalloc.
1973 * we force endpoints to start unassigned; few controller
1974 * drivers will zero ep->driver_data.
1975 */
1976 usb_ep_autoconfig_reset(gadget);
1977 return 0;
1978fail_dev:
1979 kfree(cdev->req->buf);
1980fail:
1981 usb_ep_free_request(gadget->ep0, cdev->req);
1982 cdev->req = NULL;
1983 return ret;
1984}
1985
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001986int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1987 struct usb_ep *ep0)
1988{
1989 int ret = 0;
1990
1991 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
1992 if (!cdev->os_desc_req) {
1993 ret = PTR_ERR(cdev->os_desc_req);
1994 goto end;
1995 }
1996
1997 /* OS feature descriptor length <= 4kB */
1998 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
1999 if (!cdev->os_desc_req->buf) {
2000 ret = PTR_ERR(cdev->os_desc_req->buf);
2001 kfree(cdev->os_desc_req);
2002 goto end;
2003 }
Felipe Balbi57943712014-09-18 09:54:54 -05002004 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002005 cdev->os_desc_req->complete = composite_setup_complete;
2006end:
2007 return ret;
2008}
2009
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002010void composite_dev_cleanup(struct usb_composite_dev *cdev)
2011{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002012 struct usb_gadget_string_container *uc, *tmp;
2013
2014 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2015 list_del(&uc->list);
2016 kfree(uc);
2017 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002018 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002019 if (cdev->os_desc_pending)
2020 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2021
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002022 kfree(cdev->os_desc_req->buf);
2023 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2024 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002025 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002026 if (cdev->setup_pending)
2027 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2028
Li Junbe0a8882014-08-28 21:44:11 +08002029 kfree(cdev->req->buf);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002030 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2031 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002032 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002033 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002034}
2035
2036static int composite_bind(struct usb_gadget *gadget,
2037 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002038{
2039 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002040 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002041 int status = -ENOMEM;
2042
2043 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2044 if (!cdev)
2045 return status;
2046
2047 spin_lock_init(&cdev->lock);
2048 cdev->gadget = gadget;
2049 set_gadget_data(gadget, cdev);
2050 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002051 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002052
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002053 status = composite_dev_prepare(composite, cdev);
2054 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002055 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002056
2057 /* composite gadget needs to assign strings for whole device (like
2058 * serial number), register function drivers, potentially update
2059 * power state and consumption, etc
2060 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002061 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002062 if (status < 0)
2063 goto fail;
2064
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002065 if (cdev->use_os_string) {
2066 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2067 if (status)
2068 goto fail;
2069 }
2070
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002071 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002072
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002073 /* has userspace failed to provide a serial number? */
2074 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2075 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2076
David Brownell40982be2008-06-19 17:52:58 -07002077 INFO(cdev, "%s ready\n", composite->name);
2078 return 0;
2079
2080fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002081 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002082 return status;
2083}
2084
2085/*-------------------------------------------------------------------------*/
2086
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002087void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002088{
2089 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2090 struct usb_function *f;
2091
David Brownell89429392009-03-19 14:14:17 -07002092 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002093 * suspend/resume callbacks?
2094 */
2095 DBG(cdev, "suspend\n");
2096 if (cdev->config) {
2097 list_for_each_entry(f, &cdev->config->functions, list) {
2098 if (f->suspend)
2099 f->suspend(f);
2100 }
2101 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002102 if (cdev->driver->suspend)
2103 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002104
2105 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08002106
2107 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002108}
2109
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002110void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002111{
2112 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2113 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00002114 u16 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07002115
David Brownell89429392009-03-19 14:14:17 -07002116 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002117 * suspend/resume callbacks?
2118 */
2119 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002120 if (cdev->driver->resume)
2121 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002122 if (cdev->config) {
2123 list_for_each_entry(f, &cdev->config->functions, list) {
2124 if (f->resume)
2125 f->resume(f);
2126 }
Hao Wub23f2f92010-11-29 15:17:03 +08002127
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002128 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08002129
2130 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002131 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002132 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002133
2134 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002135}
2136
2137/*-------------------------------------------------------------------------*/
2138
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002139static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002140 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002141 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002142
2143 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002144 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002145 .disconnect = composite_disconnect,
2146
2147 .suspend = composite_suspend,
2148 .resume = composite_resume,
2149
2150 .driver = {
2151 .owner = THIS_MODULE,
2152 },
2153};
2154
2155/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002156 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002157 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002158 *
David Brownell40982be2008-06-19 17:52:58 -07002159 * Context: single threaded during gadget setup
2160 *
2161 * This function is used to register drivers using the composite driver
2162 * framework. The return value is zero, or a negative errno value.
2163 * Those values normally come from the driver's @bind method, which does
2164 * all the work of setting up the driver to match the hardware.
2165 *
2166 * On successful return, the gadget is ready to respond to requests from
2167 * the host, unless one of its components invokes usb_gadget_disconnect()
2168 * while it was binding. That would usually be done in order to wait for
2169 * some userspace participation.
2170 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002171int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002172{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002173 struct usb_gadget_driver *gadget_driver;
2174
2175 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002176 return -EINVAL;
2177
2178 if (!driver->name)
2179 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002180
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002181 driver->gadget_driver = composite_driver_template;
2182 gadget_driver = &driver->gadget_driver;
2183
2184 gadget_driver->function = (char *) driver->name;
2185 gadget_driver->driver.name = driver->name;
2186 gadget_driver->max_speed = driver->max_speed;
2187
2188 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002189}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002190EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002191
2192/**
2193 * usb_composite_unregister() - unregister a composite driver
2194 * @driver: the driver to unregister
2195 *
2196 * This function is used to unregister drivers using the composite
2197 * driver framework.
2198 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002199void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002200{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002201 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002202}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002203EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002204
2205/**
2206 * usb_composite_setup_continue() - Continue with the control transfer
2207 * @cdev: the composite device who's control transfer was kept waiting
2208 *
2209 * This function must be called by the USB function driver to continue
2210 * with the control transfer's data/status stage in case it had requested to
2211 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2212 * can request the composite framework to delay the setup request's data/status
2213 * stages by returning USB_GADGET_DELAYED_STATUS.
2214 */
2215void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2216{
2217 int value;
2218 struct usb_request *req = cdev->req;
2219 unsigned long flags;
2220
2221 DBG(cdev, "%s\n", __func__);
2222 spin_lock_irqsave(&cdev->lock, flags);
2223
2224 if (cdev->delayed_status == 0) {
2225 WARN(cdev, "%s: Unexpected call\n", __func__);
2226
2227 } else if (--cdev->delayed_status == 0) {
2228 DBG(cdev, "%s: Completing delayed status\n", __func__);
2229 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002230 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002231 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002232 if (value < 0) {
2233 DBG(cdev, "ep_queue --> %d\n", value);
2234 req->status = 0;
2235 composite_setup_complete(cdev->gadget->ep0, req);
2236 }
2237 }
2238
2239 spin_unlock_irqrestore(&cdev->lock, flags);
2240}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002241EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002242
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002243static char *composite_default_mfr(struct usb_gadget *gadget)
2244{
2245 char *mfr;
2246 int len;
2247
2248 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2249 init_utsname()->release, gadget->name);
2250 len++;
2251 mfr = kmalloc(len, GFP_KERNEL);
2252 if (!mfr)
2253 return NULL;
2254 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2255 init_utsname()->release, gadget->name);
2256 return mfr;
2257}
2258
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002259void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2260 struct usb_composite_overwrite *covr)
2261{
2262 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002263 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2264 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002265
2266 if (covr->idVendor)
2267 desc->idVendor = cpu_to_le16(covr->idVendor);
2268
2269 if (covr->idProduct)
2270 desc->idProduct = cpu_to_le16(covr->idProduct);
2271
2272 if (covr->bcdDevice)
2273 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002274
2275 if (covr->serial_number) {
2276 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2277 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2278 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002279 if (covr->manufacturer) {
2280 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2281 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002282
2283 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2284 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2285 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2286 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002287 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002288
2289 if (covr->product) {
2290 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2291 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2292 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002293}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002294EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002295
2296MODULE_LICENSE("GPL");
2297MODULE_AUTHOR("David Brownell");