blob: b938fcfee235e4f59832c374bbfa8bc43b7f5812 [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>
Macpaul Lin53e62422015-07-09 15:18:42 +080022#include <linux/usb/otg.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030023#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070024
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020025#include "u_os_desc.h"
26
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020027/**
28 * struct usb_os_string - represents OS String to be reported by a gadget
29 * @bLength: total length of the entire descritor, always 0x12
30 * @bDescriptorType: USB_DT_STRING
31 * @qwSignature: the OS String proper
32 * @bMS_VendorCode: code used by the host for subsequent requests
33 * @bPad: not used, must be zero
34 */
35struct usb_os_string {
36 __u8 bLength;
37 __u8 bDescriptorType;
38 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
39 __u8 bMS_VendorCode;
40 __u8 bPad;
41} __packed;
42
David Brownell40982be2008-06-19 17:52:58 -070043/*
44 * The code in this file is utility code, used to build a gadget driver
45 * from one or more "function" drivers, one or more "configuration"
46 * objects, and a "usb_composite_driver" by gluing them together along
47 * with the relevant device-wide data.
48 */
49
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010050static struct usb_gadget_strings **get_containers_gs(
51 struct usb_gadget_string_container *uc)
52{
53 return (struct usb_gadget_strings **)uc->stash;
54}
55
Tatyana Brokhman48767a42011-06-28 16:33:49 +030056/**
John Younf3bdbe32016-02-05 17:07:03 -080057 * function_descriptors() - get function descriptors for speed
58 * @f: the function
59 * @speed: the speed
60 *
61 * Returns the descriptors or NULL if not set.
62 */
63static struct usb_descriptor_header **
64function_descriptors(struct usb_function *f,
65 enum usb_device_speed speed)
66{
67 struct usb_descriptor_header **descriptors;
68
69 switch (speed) {
70 case USB_SPEED_SUPER_PLUS:
71 descriptors = f->ssp_descriptors;
72 break;
73 case USB_SPEED_SUPER:
74 descriptors = f->ss_descriptors;
75 break;
76 case USB_SPEED_HIGH:
77 descriptors = f->hs_descriptors;
78 break;
79 default:
80 descriptors = f->fs_descriptors;
81 }
82
83 return descriptors;
84}
85
86/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +030087 * next_ep_desc() - advance to the next EP descriptor
88 * @t: currect pointer within descriptor array
89 *
90 * Return: next EP descriptor or NULL
91 *
92 * Iterate over @t until either EP descriptor found or
93 * NULL (that indicates end of list) encountered
94 */
95static struct usb_descriptor_header**
96next_ep_desc(struct usb_descriptor_header **t)
97{
98 for (; *t; t++) {
99 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
100 return t;
101 }
102 return NULL;
103}
104
105/*
106 * for_each_ep_desc()- iterate over endpoint descriptors in the
107 * descriptors list
108 * @start: pointer within descriptor array.
109 * @ep_desc: endpoint descriptor to use as the loop cursor
110 */
111#define for_each_ep_desc(start, ep_desc) \
112 for (ep_desc = next_ep_desc(start); \
113 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
114
115/**
116 * config_ep_by_speed() - configures the given endpoint
117 * according to gadget speed.
118 * @g: pointer to the gadget
119 * @f: usb function
120 * @_ep: the endpoint to configure
121 *
122 * Return: error code, 0 on success
123 *
124 * This function chooses the right descriptors for a given
125 * endpoint according to gadget speed and saves it in the
126 * endpoint desc field. If the endpoint already has a descriptor
127 * assigned to it - overwrites it with currently corresponding
128 * descriptor. The endpoint maxpacket field is updated according
129 * to the chosen descriptor.
130 * Note: the supplied function should hold all the descriptors
131 * for supported speeds
132 */
133int config_ep_by_speed(struct usb_gadget *g,
134 struct usb_function *f,
135 struct usb_ep *_ep)
136{
Felipe Balbib785ea72012-06-06 10:20:23 +0300137 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300138 struct usb_endpoint_descriptor *chosen_desc = NULL;
139 struct usb_descriptor_header **speed_desc = NULL;
140
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300141 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
142 int want_comp_desc = 0;
143
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300144 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
145
146 if (!g || !f || !_ep)
147 return -EIO;
148
149 /* select desired speed */
150 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300151 case USB_SPEED_SUPER:
152 if (gadget_is_superspeed(g)) {
153 speed_desc = f->ss_descriptors;
154 want_comp_desc = 1;
155 break;
156 }
157 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300158 case USB_SPEED_HIGH:
159 if (gadget_is_dualspeed(g)) {
160 speed_desc = f->hs_descriptors;
161 break;
162 }
163 /* else: fall through */
164 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200165 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300166 }
167 /* find descriptors */
168 for_each_ep_desc(speed_desc, d_spd) {
169 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
170 if (chosen_desc->bEndpointAddress == _ep->address)
171 goto ep_found;
172 }
173 return -EIO;
174
175ep_found:
176 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700177 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300178 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300179 _ep->comp_desc = NULL;
180 _ep->maxburst = 0;
181 _ep->mult = 0;
182 if (!want_comp_desc)
183 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300184
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300185 /*
186 * Companion descriptor should follow EP descriptor
187 * USB 3.0 spec, #9.6.7
188 */
189 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
190 if (!comp_desc ||
191 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
192 return -EIO;
193 _ep->comp_desc = comp_desc;
194 if (g->speed == USB_SPEED_SUPER) {
195 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300196 case USB_ENDPOINT_XFER_ISOC:
197 /* mult: bits 1:0 of bmAttributes */
198 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800199 case USB_ENDPOINT_XFER_BULK:
200 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300201 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300202 break;
203 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300204 if (comp_desc->bMaxBurst != 0)
205 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
206 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300207 break;
208 }
209 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300210 return 0;
211}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200212EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700213
214/**
215 * usb_add_function() - add a function to a configuration
216 * @config: the configuration
217 * @function: the function being added
218 * Context: single threaded during gadget setup
219 *
220 * After initialization, each configuration must have one or more
221 * functions added to it. Adding a function involves calling its @bind()
222 * method to allocate resources such as interface and string identifiers
223 * and endpoints.
224 *
225 * This function returns the value of the function's bind(), which is
226 * zero for success else a negative errno value.
227 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200228int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700229 struct usb_function *function)
230{
231 int value = -EINVAL;
232
233 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
234 function->name, function,
235 config->label, config);
236
237 if (!function->set_alt || !function->disable)
238 goto done;
239
240 function->config = config;
241 list_add_tail(&function->list, &config->functions);
242
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200243 if (function->bind_deactivated) {
244 value = usb_function_deactivate(function);
245 if (value)
246 goto done;
247 }
248
David Brownell40982be2008-06-19 17:52:58 -0700249 /* REVISIT *require* function->bind? */
250 if (function->bind) {
251 value = function->bind(config, function);
252 if (value < 0) {
253 list_del(&function->list);
254 function->config = NULL;
255 }
256 } else
257 value = 0;
258
259 /* We allow configurations that don't work at both speeds.
260 * If we run into a lowspeed Linux system, treat it the same
261 * as full speed ... it's the function drivers that will need
262 * to avoid bulk and ISO transfers.
263 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200264 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700265 config->fullspeed = true;
266 if (!config->highspeed && function->hs_descriptors)
267 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300268 if (!config->superspeed && function->ss_descriptors)
269 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800270 if (!config->superspeed_plus && function->ssp_descriptors)
271 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700272
273done:
274 if (value)
275 DBG(config->cdev, "adding '%s'/%p --> %d\n",
276 function->name, function, value);
277 return value;
278}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200279EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700280
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100281void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
282{
283 if (f->disable)
284 f->disable(f);
285
286 bitmap_zero(f->endpoints, 32);
287 list_del(&f->list);
288 if (f->unbind)
289 f->unbind(c, f);
290}
291EXPORT_SYMBOL_GPL(usb_remove_function);
292
David Brownell40982be2008-06-19 17:52:58 -0700293/**
David Brownell60beed92008-08-18 17:38:22 -0700294 * usb_function_deactivate - prevent function and gadget enumeration
295 * @function: the function that isn't yet ready to respond
296 *
297 * Blocks response of the gadget driver to host enumeration by
298 * preventing the data line pullup from being activated. This is
299 * normally called during @bind() processing to change from the
300 * initial "ready to respond" state, or when a required resource
301 * becomes available.
302 *
303 * For example, drivers that serve as a passthrough to a userspace
304 * daemon can block enumeration unless that daemon (such as an OBEX,
305 * MTP, or print server) is ready to handle host requests.
306 *
307 * Not all systems support software control of their USB peripheral
308 * data pullups.
309 *
310 * Returns zero on success, else negative errno.
311 */
312int usb_function_deactivate(struct usb_function *function)
313{
314 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200315 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700316 int status = 0;
317
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200318 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700319
320 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200321 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700322 if (status == 0)
323 cdev->deactivations++;
324
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200325 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700326 return status;
327}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200328EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700329
330/**
331 * usb_function_activate - allow function and gadget enumeration
332 * @function: function on which usb_function_activate() was called
333 *
334 * Reverses effect of usb_function_deactivate(). If no more functions
335 * are delaying their activation, the gadget driver will respond to
336 * host enumeration procedures.
337 *
338 * Returns zero on success, else negative errno.
339 */
340int usb_function_activate(struct usb_function *function)
341{
342 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200343 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700344 int status = 0;
345
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200346 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700347
348 if (WARN_ON(cdev->deactivations == 0))
349 status = -EINVAL;
350 else {
351 cdev->deactivations--;
352 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200353 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700354 }
355
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200356 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700357 return status;
358}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200359EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700360
361/**
David Brownell40982be2008-06-19 17:52:58 -0700362 * usb_interface_id() - allocate an unused interface ID
363 * @config: configuration associated with the interface
364 * @function: function handling the interface
365 * Context: single threaded during gadget setup
366 *
367 * usb_interface_id() is called from usb_function.bind() callbacks to
368 * allocate new interface IDs. The function driver will then store that
369 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300370 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700371 * particularly changing its altsetting via set_alt(). There may
372 * also be class-specific or vendor-specific requests to handle.
373 *
374 * All interface identifier should be allocated using this routine, to
375 * ensure that for example different functions don't wrongly assign
376 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300377 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700378 * one configuration (or more than once in a given configuration) need
379 * multiple versions of the relevant descriptors.
380 *
381 * Returns the interface ID which was allocated; or -ENODEV if no
382 * more interface IDs can be allocated.
383 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200384int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700385 struct usb_function *function)
386{
387 unsigned id = config->next_interface_id;
388
389 if (id < MAX_CONFIG_INTERFACES) {
390 config->interface[id] = function;
391 config->next_interface_id = id + 1;
392 return id;
393 }
394 return -ENODEV;
395}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200396EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700397
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100398static u8 encode_bMaxPower(enum usb_device_speed speed,
399 struct usb_configuration *c)
400{
401 unsigned val;
402
403 if (c->MaxPower)
404 val = c->MaxPower;
405 else
406 val = CONFIG_USB_GADGET_VBUS_DRAW;
407 if (!val)
408 return 0;
409 switch (speed) {
410 case USB_SPEED_SUPER:
411 return DIV_ROUND_UP(val, 8);
412 default:
413 return DIV_ROUND_UP(val, 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700414 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100415}
416
David Brownell40982be2008-06-19 17:52:58 -0700417static int config_buf(struct usb_configuration *config,
418 enum usb_device_speed speed, void *buf, u8 type)
419{
420 struct usb_config_descriptor *c = buf;
421 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200422 int len;
David Brownell40982be2008-06-19 17:52:58 -0700423 struct usb_function *f;
424 int status;
425
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200426 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700427 /* write the config descriptor */
428 c = buf;
429 c->bLength = USB_DT_CONFIG_SIZE;
430 c->bDescriptorType = type;
431 /* wTotalLength is written later */
432 c->bNumInterfaces = config->next_interface_id;
433 c->bConfigurationValue = config->bConfigurationValue;
434 c->iConfiguration = config->iConfiguration;
435 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100436 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700437
438 /* There may be e.g. OTG descriptors */
439 if (config->descriptors) {
440 status = usb_descriptor_fillbuf(next, len,
441 config->descriptors);
442 if (status < 0)
443 return status;
444 len -= status;
445 next += status;
446 }
447
448 /* add each function's descriptors */
449 list_for_each_entry(f, &config->functions, list) {
450 struct usb_descriptor_header **descriptors;
451
John Younf3bdbe32016-02-05 17:07:03 -0800452 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700453 if (!descriptors)
454 continue;
455 status = usb_descriptor_fillbuf(next, len,
456 (const struct usb_descriptor_header **) descriptors);
457 if (status < 0)
458 return status;
459 len -= status;
460 next += status;
461 }
462
463 len = next - buf;
464 c->wTotalLength = cpu_to_le16(len);
465 return len;
466}
467
468static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
469{
470 struct usb_gadget *gadget = cdev->gadget;
471 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200472 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700473 u8 type = w_value >> 8;
474 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
475
John Youneae58202016-02-05 17:07:17 -0800476 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300477 speed = gadget->speed;
478 else if (gadget_is_dualspeed(gadget)) {
479 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700480 if (gadget->speed == USB_SPEED_HIGH)
481 hs = 1;
482 if (type == USB_DT_OTHER_SPEED_CONFIG)
483 hs = !hs;
484 if (hs)
485 speed = USB_SPEED_HIGH;
486
487 }
488
489 /* This is a lookup by config *INDEX* */
490 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200491
492 pos = &cdev->configs;
493 c = cdev->os_desc_config;
494 if (c)
495 goto check_config;
496
497 while ((pos = pos->next) != &cdev->configs) {
498 c = list_entry(pos, typeof(*c), list);
499
500 /* skip OS Descriptors config which is handled separately */
501 if (c == cdev->os_desc_config)
502 continue;
503
504check_config:
David Brownell40982be2008-06-19 17:52:58 -0700505 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300506 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800507 case USB_SPEED_SUPER_PLUS:
508 if (!c->superspeed_plus)
509 continue;
510 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300511 case USB_SPEED_SUPER:
512 if (!c->superspeed)
513 continue;
514 break;
515 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700516 if (!c->highspeed)
517 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300518 break;
519 default:
David Brownell40982be2008-06-19 17:52:58 -0700520 if (!c->fullspeed)
521 continue;
522 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300523
David Brownell40982be2008-06-19 17:52:58 -0700524 if (w_value == 0)
525 return config_buf(c, speed, cdev->req->buf, type);
526 w_value--;
527 }
528 return -EINVAL;
529}
530
531static int count_configs(struct usb_composite_dev *cdev, unsigned type)
532{
533 struct usb_gadget *gadget = cdev->gadget;
534 struct usb_configuration *c;
535 unsigned count = 0;
536 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300537 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800538 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700539
540 if (gadget_is_dualspeed(gadget)) {
541 if (gadget->speed == USB_SPEED_HIGH)
542 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300543 if (gadget->speed == USB_SPEED_SUPER)
544 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800545 if (gadget->speed == USB_SPEED_SUPER_PLUS)
546 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700547 if (type == USB_DT_DEVICE_QUALIFIER)
548 hs = !hs;
549 }
550 list_for_each_entry(c, &cdev->configs, list) {
551 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800552 if (ssp) {
553 if (!c->superspeed_plus)
554 continue;
555 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300556 if (!c->superspeed)
557 continue;
558 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700559 if (!c->highspeed)
560 continue;
561 } else {
562 if (!c->fullspeed)
563 continue;
564 }
565 count++;
566 }
567 return count;
568}
569
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300570/**
571 * bos_desc() - prepares the BOS descriptor.
572 * @cdev: pointer to usb_composite device to generate the bos
573 * descriptor for
574 *
575 * This function generates the BOS (Binary Device Object)
576 * descriptor and its device capabilities descriptors. The BOS
577 * descriptor should be supported by a SuperSpeed device.
578 */
579static int bos_desc(struct usb_composite_dev *cdev)
580{
581 struct usb_ext_cap_descriptor *usb_ext;
582 struct usb_ss_cap_descriptor *ss_cap;
583 struct usb_dcd_config_params dcd_config_params;
584 struct usb_bos_descriptor *bos = cdev->req->buf;
585
586 bos->bLength = USB_DT_BOS_SIZE;
587 bos->bDescriptorType = USB_DT_BOS;
588
589 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
590 bos->bNumDeviceCaps = 0;
591
592 /*
593 * A SuperSpeed device shall include the USB2.0 extension descriptor
594 * and shall support LPM when operating in USB2.0 HS mode.
595 */
596 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
597 bos->bNumDeviceCaps++;
598 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
599 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
600 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
601 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500602 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300603
604 /*
605 * The Superspeed USB Capability descriptor shall be implemented by all
606 * SuperSpeed devices.
607 */
608 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
609 bos->bNumDeviceCaps++;
610 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
611 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
612 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
613 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
614 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
615 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
616 USB_FULL_SPEED_OPERATION |
617 USB_HIGH_SPEED_OPERATION |
618 USB_5GBPS_OPERATION);
619 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
620
621 /* Get Controller configuration */
622 if (cdev->gadget->ops->get_config_params)
623 cdev->gadget->ops->get_config_params(&dcd_config_params);
624 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300625 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300626 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300627 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300628 }
629 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
630 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
631
John Younf228a8d2016-02-05 17:05:53 -0800632 /* The SuperSpeedPlus USB Device Capability descriptor */
633 if (gadget_is_superspeed_plus(cdev->gadget)) {
634 struct usb_ssp_cap_descriptor *ssp_cap;
635
636 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
637 bos->bNumDeviceCaps++;
638
639 /*
640 * Report typical values.
641 */
642
643 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
644 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
645 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
646 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
647
648 /* SSAC = 1 (2 attributes) */
649 ssp_cap->bmAttributes = cpu_to_le32(1);
650
651 /* Min RX/TX Lane Count = 1 */
652 ssp_cap->wFunctionalitySupport = (1 << 8) | (1 << 12);
653
654 /*
655 * bmSublinkSpeedAttr[0]:
656 * ST = Symmetric, RX
657 * LSE = 3 (Gbps)
658 * LP = 1 (SuperSpeedPlus)
659 * LSM = 10 (10 Gbps)
660 */
661 ssp_cap->bmSublinkSpeedAttr[0] =
662 (3 << 4) | (1 << 14) | (0xa << 16);
663 /*
664 * bmSublinkSpeedAttr[1] =
665 * ST = Symmetric, TX
666 * LSE = 3 (Gbps)
667 * LP = 1 (SuperSpeedPlus)
668 * LSM = 10 (10 Gbps)
669 */
670 ssp_cap->bmSublinkSpeedAttr[1] =
671 (3 << 4) | (1 << 14) | (0xa << 16) | (1 << 7);
672 }
673
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300674 return le16_to_cpu(bos->wTotalLength);
675}
676
David Brownell40982be2008-06-19 17:52:58 -0700677static void device_qual(struct usb_composite_dev *cdev)
678{
679 struct usb_qualifier_descriptor *qual = cdev->req->buf;
680
681 qual->bLength = sizeof(*qual);
682 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
683 /* POLICY: same bcdUSB and device type info at both speeds */
684 qual->bcdUSB = cdev->desc.bcdUSB;
685 qual->bDeviceClass = cdev->desc.bDeviceClass;
686 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
687 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
688 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200689 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700690 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700691 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700692}
693
694/*-------------------------------------------------------------------------*/
695
696static void reset_config(struct usb_composite_dev *cdev)
697{
698 struct usb_function *f;
699
700 DBG(cdev, "reset config\n");
701
702 list_for_each_entry(f, &cdev->config->functions, list) {
703 if (f->disable)
704 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200705
706 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700707 }
708 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100709 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700710}
711
712static int set_config(struct usb_composite_dev *cdev,
713 const struct usb_ctrlrequest *ctrl, unsigned number)
714{
715 struct usb_gadget *gadget = cdev->gadget;
716 struct usb_configuration *c = NULL;
717 int result = -EINVAL;
718 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
719 int tmp;
720
David Brownell40982be2008-06-19 17:52:58 -0700721 if (number) {
722 list_for_each_entry(c, &cdev->configs, list) {
723 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300724 /*
725 * We disable the FDs of the previous
726 * configuration only if the new configuration
727 * is a valid one
728 */
729 if (cdev->config)
730 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700731 result = 0;
732 break;
733 }
734 }
735 if (result < 0)
736 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300737 } else { /* Zero configuration value - need to reset the config */
738 if (cdev->config)
739 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700740 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300741 }
David Brownell40982be2008-06-19 17:52:58 -0700742
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200743 INFO(cdev, "%s config #%d: %s\n",
744 usb_speed_string(gadget->speed),
745 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700746
747 if (!c)
748 goto done;
749
Peter Chen6027f312014-04-29 13:26:28 +0800750 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700751 cdev->config = c;
752
753 /* Initialize all interfaces by setting them to altsetting zero. */
754 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
755 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200756 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700757
758 if (!f)
759 break;
760
Laurent Pinchart52426582009-10-21 00:03:38 +0200761 /*
762 * Record which endpoints are used by the function. This is used
763 * to dispatch control requests targeted at that endpoint to the
764 * function's setup callback instead of the current
765 * configuration's setup callback.
766 */
John Younf3bdbe32016-02-05 17:07:03 -0800767 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200768
769 for (; *descriptors; ++descriptors) {
770 struct usb_endpoint_descriptor *ep;
771 int addr;
772
773 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
774 continue;
775
776 ep = (struct usb_endpoint_descriptor *)*descriptors;
777 addr = ((ep->bEndpointAddress & 0x80) >> 3)
778 | (ep->bEndpointAddress & 0x0f);
779 set_bit(addr, f->endpoints);
780 }
781
David Brownell40982be2008-06-19 17:52:58 -0700782 result = f->set_alt(f, tmp, 0);
783 if (result < 0) {
784 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
785 tmp, f->name, f, result);
786
787 reset_config(cdev);
788 goto done;
789 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300790
791 if (result == USB_GADGET_DELAYED_STATUS) {
792 DBG(cdev,
793 "%s: interface %d (%s) requested delayed status\n",
794 __func__, tmp, f->name);
795 cdev->delayed_status++;
796 DBG(cdev, "delayed_status count %d\n",
797 cdev->delayed_status);
798 }
David Brownell40982be2008-06-19 17:52:58 -0700799 }
800
801 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100802 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700803done:
804 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300805 if (result >= 0 && cdev->delayed_status)
806 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700807 return result;
808}
809
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100810int usb_add_config_only(struct usb_composite_dev *cdev,
811 struct usb_configuration *config)
812{
813 struct usb_configuration *c;
814
815 if (!config->bConfigurationValue)
816 return -EINVAL;
817
818 /* Prevent duplicate configuration identifiers */
819 list_for_each_entry(c, &cdev->configs, list) {
820 if (c->bConfigurationValue == config->bConfigurationValue)
821 return -EBUSY;
822 }
823
824 config->cdev = cdev;
825 list_add_tail(&config->list, &cdev->configs);
826
827 INIT_LIST_HEAD(&config->functions);
828 config->next_interface_id = 0;
829 memset(config->interface, 0, sizeof(config->interface));
830
831 return 0;
832}
833EXPORT_SYMBOL_GPL(usb_add_config_only);
834
David Brownell40982be2008-06-19 17:52:58 -0700835/**
836 * usb_add_config() - add a configuration to a device.
837 * @cdev: wraps the USB gadget
838 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200839 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700840 * Context: single threaded during gadget setup
841 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200842 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700843 * add each of the configurations it supports, using this routine.
844 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200845 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700846 * is zero for success else a negative errno value. Binding configurations
847 * assigns global resources including string IDs, and per-configuration
848 * resources such as interface IDs and endpoints.
849 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200850int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200851 struct usb_configuration *config,
852 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700853{
854 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100855
856 if (!bind)
857 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700858
859 DBG(cdev, "adding config #%u '%s'/%p\n",
860 config->bConfigurationValue,
861 config->label, config);
862
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100863 status = usb_add_config_only(cdev, config);
864 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700865 goto done;
866
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200867 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700868 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900869 while (!list_empty(&config->functions)) {
870 struct usb_function *f;
871
872 f = list_first_entry(&config->functions,
873 struct usb_function, list);
874 list_del(&f->list);
875 if (f->unbind) {
876 DBG(cdev, "unbind function '%s'/%p\n",
877 f->name, f);
878 f->unbind(config, f);
879 /* may free memory for "f" */
880 }
881 }
David Brownell40982be2008-06-19 17:52:58 -0700882 list_del(&config->list);
883 config->cdev = NULL;
884 } else {
885 unsigned i;
886
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300887 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700888 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300889 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700890 config->highspeed ? " high" : "",
891 config->fullspeed
892 ? (gadget_is_dualspeed(cdev->gadget)
893 ? " full"
894 : " full/low")
895 : "");
896
897 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
898 struct usb_function *f = config->interface[i];
899
900 if (!f)
901 continue;
902 DBG(cdev, " interface %d = %s/%p\n",
903 i, f->name, f);
904 }
905 }
906
Robert Baldygaf871cb92015-09-16 12:10:39 +0200907 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -0700908 usb_ep_autoconfig_reset(cdev->gadget);
909
910done:
911 if (status)
912 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
913 config->bConfigurationValue, status);
914 return status;
915}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200916EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700917
Benoit Goby51cce6f2012-05-10 10:07:57 +0200918static void remove_config(struct usb_composite_dev *cdev,
919 struct usb_configuration *config)
920{
921 while (!list_empty(&config->functions)) {
922 struct usb_function *f;
923
924 f = list_first_entry(&config->functions,
925 struct usb_function, list);
926 list_del(&f->list);
927 if (f->unbind) {
928 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
929 f->unbind(config, f);
930 /* may free memory for "f" */
931 }
932 }
933 list_del(&config->list);
934 if (config->unbind) {
935 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
936 config->unbind(config);
937 /* may free memory for "c" */
938 }
939}
940
941/**
942 * usb_remove_config() - remove a configuration from a device.
943 * @cdev: wraps the USB gadget
944 * @config: the configuration
945 *
946 * Drivers must call usb_gadget_disconnect before calling this function
947 * to disconnect the device from the host and make sure the host will not
948 * try to enumerate the device while we are changing the config list.
949 */
950void usb_remove_config(struct usb_composite_dev *cdev,
951 struct usb_configuration *config)
952{
953 unsigned long flags;
954
955 spin_lock_irqsave(&cdev->lock, flags);
956
957 if (cdev->config == config)
958 reset_config(cdev);
959
960 spin_unlock_irqrestore(&cdev->lock, flags);
961
962 remove_config(cdev, config);
963}
964
David Brownell40982be2008-06-19 17:52:58 -0700965/*-------------------------------------------------------------------------*/
966
967/* We support strings in multiple languages ... string descriptor zero
968 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -0300969 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -0700970 * the host side.
971 */
972
973static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
974{
975 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300976 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700977 __le16 *tmp;
978
979 while (*sp) {
980 s = *sp;
981 language = cpu_to_le16(s->language);
982 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
983 if (*tmp == language)
984 goto repeat;
985 }
986 *tmp++ = language;
987repeat:
988 sp++;
989 }
990}
991
992static int lookup_string(
993 struct usb_gadget_strings **sp,
994 void *buf,
995 u16 language,
996 int id
997)
998{
999 struct usb_gadget_strings *s;
1000 int value;
1001
1002 while (*sp) {
1003 s = *sp++;
1004 if (s->language != language)
1005 continue;
1006 value = usb_gadget_get_string(s, id, buf);
1007 if (value > 0)
1008 return value;
1009 }
1010 return -EINVAL;
1011}
1012
1013static int get_string(struct usb_composite_dev *cdev,
1014 void *buf, u16 language, int id)
1015{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001016 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001017 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001018 struct usb_configuration *c;
1019 struct usb_function *f;
1020 int len;
1021
Diego Violaad4676a2015-05-31 15:52:41 -03001022 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001023 * folk will ever care about ... also, it's all supported here.
1024 * (Except for UTF8 support for Unicode's "Astral Planes".)
1025 */
1026
1027 /* 0 == report all available language codes */
1028 if (id == 0) {
1029 struct usb_string_descriptor *s = buf;
1030 struct usb_gadget_strings **sp;
1031
1032 memset(s, 0, 256);
1033 s->bDescriptorType = USB_DT_STRING;
1034
1035 sp = composite->strings;
1036 if (sp)
1037 collect_langs(sp, s->wData);
1038
1039 list_for_each_entry(c, &cdev->configs, list) {
1040 sp = c->strings;
1041 if (sp)
1042 collect_langs(sp, s->wData);
1043
1044 list_for_each_entry(f, &c->functions, list) {
1045 sp = f->strings;
1046 if (sp)
1047 collect_langs(sp, s->wData);
1048 }
1049 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001050 list_for_each_entry(uc, &cdev->gstrings, list) {
1051 struct usb_gadget_strings **sp;
1052
1053 sp = get_containers_gs(uc);
1054 collect_langs(sp, s->wData);
1055 }
David Brownell40982be2008-06-19 17:52:58 -07001056
Roel Kluin417b57b2009-08-06 16:09:51 -07001057 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001058 continue;
1059 if (!len)
1060 return -EINVAL;
1061
1062 s->bLength = 2 * (len + 1);
1063 return s->bLength;
1064 }
1065
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001066 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1067 struct usb_os_string *b = buf;
1068 b->bLength = sizeof(*b);
1069 b->bDescriptorType = USB_DT_STRING;
1070 compiletime_assert(
1071 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1072 "qwSignature size must be equal to qw_sign");
1073 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1074 b->bMS_VendorCode = cdev->b_vendor_code;
1075 b->bPad = 0;
1076 return sizeof(*b);
1077 }
1078
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001079 list_for_each_entry(uc, &cdev->gstrings, list) {
1080 struct usb_gadget_strings **sp;
1081
1082 sp = get_containers_gs(uc);
1083 len = lookup_string(sp, buf, language, id);
1084 if (len > 0)
1085 return len;
1086 }
1087
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001088 /* String IDs are device-scoped, so we look up each string
1089 * table we're told about. These lookups are infrequent;
1090 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001091 */
1092 if (composite->strings) {
1093 len = lookup_string(composite->strings, buf, language, id);
1094 if (len > 0)
1095 return len;
1096 }
1097 list_for_each_entry(c, &cdev->configs, list) {
1098 if (c->strings) {
1099 len = lookup_string(c->strings, buf, language, id);
1100 if (len > 0)
1101 return len;
1102 }
1103 list_for_each_entry(f, &c->functions, list) {
1104 if (!f->strings)
1105 continue;
1106 len = lookup_string(f->strings, buf, language, id);
1107 if (len > 0)
1108 return len;
1109 }
1110 }
1111 return -EINVAL;
1112}
1113
1114/**
1115 * usb_string_id() - allocate an unused string ID
1116 * @cdev: the device whose string descriptor IDs are being allocated
1117 * Context: single threaded during gadget setup
1118 *
1119 * @usb_string_id() is called from bind() callbacks to allocate
1120 * string IDs. Drivers for functions, configurations, or gadgets will
1121 * then store that ID in the appropriate descriptors and string table.
1122 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001123 * All string identifier should be allocated using this,
1124 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1125 * that for example different functions don't wrongly assign different
1126 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001127 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001128int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001129{
1130 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001131 /* string id 0 is reserved by USB spec for list of
1132 * supported languages */
1133 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001134 cdev->next_string_id++;
1135 return cdev->next_string_id;
1136 }
1137 return -ENODEV;
1138}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001139EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001140
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001141/**
1142 * usb_string_ids() - allocate unused string IDs in batch
1143 * @cdev: the device whose string descriptor IDs are being allocated
1144 * @str: an array of usb_string objects to assign numbers to
1145 * Context: single threaded during gadget setup
1146 *
1147 * @usb_string_ids() is called from bind() callbacks to allocate
1148 * string IDs. Drivers for functions, configurations, or gadgets will
1149 * then copy IDs from the string table to the appropriate descriptors
1150 * and string table for other languages.
1151 *
1152 * All string identifier should be allocated using this,
1153 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1154 * example different functions don't wrongly assign different meanings
1155 * to the same identifier.
1156 */
1157int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1158{
1159 int next = cdev->next_string_id;
1160
1161 for (; str->s; ++str) {
1162 if (unlikely(next >= 254))
1163 return -ENODEV;
1164 str->id = ++next;
1165 }
1166
1167 cdev->next_string_id = next;
1168
1169 return 0;
1170}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001171EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001172
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001173static struct usb_gadget_string_container *copy_gadget_strings(
1174 struct usb_gadget_strings **sp, unsigned n_gstrings,
1175 unsigned n_strings)
1176{
1177 struct usb_gadget_string_container *uc;
1178 struct usb_gadget_strings **gs_array;
1179 struct usb_gadget_strings *gs;
1180 struct usb_string *s;
1181 unsigned mem;
1182 unsigned n_gs;
1183 unsigned n_s;
1184 void *stash;
1185
1186 mem = sizeof(*uc);
1187 mem += sizeof(void *) * (n_gstrings + 1);
1188 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1189 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1190 uc = kmalloc(mem, GFP_KERNEL);
1191 if (!uc)
1192 return ERR_PTR(-ENOMEM);
1193 gs_array = get_containers_gs(uc);
1194 stash = uc->stash;
1195 stash += sizeof(void *) * (n_gstrings + 1);
1196 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1197 struct usb_string *org_s;
1198
1199 gs_array[n_gs] = stash;
1200 gs = gs_array[n_gs];
1201 stash += sizeof(struct usb_gadget_strings);
1202 gs->language = sp[n_gs]->language;
1203 gs->strings = stash;
1204 org_s = sp[n_gs]->strings;
1205
1206 for (n_s = 0; n_s < n_strings; n_s++) {
1207 s = stash;
1208 stash += sizeof(struct usb_string);
1209 if (org_s->s)
1210 s->s = org_s->s;
1211 else
1212 s->s = "";
1213 org_s++;
1214 }
1215 s = stash;
1216 s->s = NULL;
1217 stash += sizeof(struct usb_string);
1218
1219 }
1220 gs_array[n_gs] = NULL;
1221 return uc;
1222}
1223
1224/**
1225 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1226 * @cdev: the device whose string descriptor IDs are being allocated
1227 * and attached.
1228 * @sp: an array of usb_gadget_strings to attach.
1229 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1230 *
1231 * This function will create a deep copy of usb_gadget_strings and usb_string
1232 * and attach it to the cdev. The actual string (usb_string.s) will not be
1233 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001234 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001235 * The ->language pointer of each struct usb_gadget_strings has to contain the
1236 * same amount of entries.
1237 * 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 +09001238 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001239 * entry of en-US. Therefore both entries become the same id assign.
1240 */
1241struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1242 struct usb_gadget_strings **sp, unsigned n_strings)
1243{
1244 struct usb_gadget_string_container *uc;
1245 struct usb_gadget_strings **n_gs;
1246 unsigned n_gstrings = 0;
1247 unsigned i;
1248 int ret;
1249
1250 for (i = 0; sp[i]; i++)
1251 n_gstrings++;
1252
1253 if (!n_gstrings)
1254 return ERR_PTR(-EINVAL);
1255
1256 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1257 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001258 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001259
1260 n_gs = get_containers_gs(uc);
1261 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1262 if (ret)
1263 goto err;
1264
1265 for (i = 1; i < n_gstrings; i++) {
1266 struct usb_string *m_s;
1267 struct usb_string *s;
1268 unsigned n;
1269
1270 m_s = n_gs[0]->strings;
1271 s = n_gs[i]->strings;
1272 for (n = 0; n < n_strings; n++) {
1273 s->id = m_s->id;
1274 s++;
1275 m_s++;
1276 }
1277 }
1278 list_add_tail(&uc->list, &cdev->gstrings);
1279 return n_gs[0]->strings;
1280err:
1281 kfree(uc);
1282 return ERR_PTR(ret);
1283}
1284EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1285
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001286/**
1287 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001288 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001289 * @n: number of string IDs to allocate
1290 * Context: single threaded during gadget setup
1291 *
1292 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001293 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001294 * is, returns last requested ID which is now very useful information.
1295 *
1296 * @usb_string_ids_n() is called from bind() callbacks to allocate
1297 * string IDs. Drivers for functions, configurations, or gadgets will
1298 * then store that ID in the appropriate descriptors and string table.
1299 *
1300 * All string identifier should be allocated using this,
1301 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1302 * example different functions don't wrongly assign different meanings
1303 * to the same identifier.
1304 */
1305int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1306{
1307 unsigned next = c->next_string_id;
1308 if (unlikely(n > 254 || (unsigned)next + n > 254))
1309 return -ENODEV;
1310 c->next_string_id += n;
1311 return next + 1;
1312}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001313EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001314
David Brownell40982be2008-06-19 17:52:58 -07001315/*-------------------------------------------------------------------------*/
1316
1317static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1318{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001319 struct usb_composite_dev *cdev;
1320
David Brownell40982be2008-06-19 17:52:58 -07001321 if (req->status || req->actual != req->length)
1322 DBG((struct usb_composite_dev *) ep->driver_data,
1323 "setup complete --> %d, %d/%d\n",
1324 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001325
1326 /*
1327 * REVIST The same ep0 requests are shared with function drivers
1328 * so they don't have to maintain the same ->complete() stubs.
1329 *
1330 * Because of that, we need to check for the validity of ->context
1331 * here, even though we know we've set it to something useful.
1332 */
1333 if (!req->context)
1334 return;
1335
1336 cdev = req->context;
1337
1338 if (cdev->req == req)
1339 cdev->setup_pending = false;
1340 else if (cdev->os_desc_req == req)
1341 cdev->os_desc_pending = false;
1342 else
1343 WARN(1, "unknown request %p\n", req);
1344}
1345
1346static int composite_ep0_queue(struct usb_composite_dev *cdev,
1347 struct usb_request *req, gfp_t gfp_flags)
1348{
1349 int ret;
1350
1351 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1352 if (ret == 0) {
1353 if (cdev->req == req)
1354 cdev->setup_pending = true;
1355 else if (cdev->os_desc_req == req)
1356 cdev->os_desc_pending = true;
1357 else
1358 WARN(1, "unknown request %p\n", req);
1359 }
1360
1361 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001362}
1363
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001364static int count_ext_compat(struct usb_configuration *c)
1365{
1366 int i, res;
1367
1368 res = 0;
1369 for (i = 0; i < c->next_interface_id; ++i) {
1370 struct usb_function *f;
1371 int j;
1372
1373 f = c->interface[i];
1374 for (j = 0; j < f->os_desc_n; ++j) {
1375 struct usb_os_desc *d;
1376
1377 if (i != f->os_desc_table[j].if_id)
1378 continue;
1379 d = f->os_desc_table[j].os_desc;
1380 if (d && d->ext_compat_id)
1381 ++res;
1382 }
1383 }
1384 BUG_ON(res > 255);
1385 return res;
1386}
1387
1388static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1389{
1390 int i, count;
1391
1392 count = 16;
1393 for (i = 0; i < c->next_interface_id; ++i) {
1394 struct usb_function *f;
1395 int j;
1396
1397 f = c->interface[i];
1398 for (j = 0; j < f->os_desc_n; ++j) {
1399 struct usb_os_desc *d;
1400
1401 if (i != f->os_desc_table[j].if_id)
1402 continue;
1403 d = f->os_desc_table[j].os_desc;
1404 if (d && d->ext_compat_id) {
1405 *buf++ = i;
1406 *buf++ = 0x01;
1407 memcpy(buf, d->ext_compat_id, 16);
1408 buf += 22;
1409 } else {
1410 ++buf;
1411 *buf = 0x01;
1412 buf += 23;
1413 }
1414 count += 24;
1415 if (count >= 4096)
1416 return;
1417 }
1418 }
1419}
1420
1421static int count_ext_prop(struct usb_configuration *c, int interface)
1422{
1423 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001424 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001425
1426 f = c->interface[interface];
1427 for (j = 0; j < f->os_desc_n; ++j) {
1428 struct usb_os_desc *d;
1429
1430 if (interface != f->os_desc_table[j].if_id)
1431 continue;
1432 d = f->os_desc_table[j].os_desc;
1433 if (d && d->ext_compat_id)
1434 return d->ext_prop_count;
1435 }
Julia Lawall849b1332014-05-19 06:31:07 +02001436 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001437}
1438
1439static int len_ext_prop(struct usb_configuration *c, int interface)
1440{
1441 struct usb_function *f;
1442 struct usb_os_desc *d;
1443 int j, res;
1444
1445 res = 10; /* header length */
1446 f = c->interface[interface];
1447 for (j = 0; j < f->os_desc_n; ++j) {
1448 if (interface != f->os_desc_table[j].if_id)
1449 continue;
1450 d = f->os_desc_table[j].os_desc;
1451 if (d)
1452 return min(res + d->ext_prop_len, 4096);
1453 }
1454 return res;
1455}
1456
1457static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1458{
1459 struct usb_function *f;
1460 struct usb_os_desc *d;
1461 struct usb_os_desc_ext_prop *ext_prop;
1462 int j, count, n, ret;
1463 u8 *start = buf;
1464
1465 f = c->interface[interface];
1466 for (j = 0; j < f->os_desc_n; ++j) {
1467 if (interface != f->os_desc_table[j].if_id)
1468 continue;
1469 d = f->os_desc_table[j].os_desc;
1470 if (d)
1471 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1472 /* 4kB minus header length */
1473 n = buf - start;
1474 if (n >= 4086)
1475 return 0;
1476
1477 count = ext_prop->data_len +
1478 ext_prop->name_len + 14;
1479 if (count > 4086 - n)
1480 return -EINVAL;
1481 usb_ext_prop_put_size(buf, count);
1482 usb_ext_prop_put_type(buf, ext_prop->type);
1483 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1484 ext_prop->name_len);
1485 if (ret < 0)
1486 return ret;
1487 switch (ext_prop->type) {
1488 case USB_EXT_PROP_UNICODE:
1489 case USB_EXT_PROP_UNICODE_ENV:
1490 case USB_EXT_PROP_UNICODE_LINK:
1491 usb_ext_prop_put_unicode(buf, ret,
1492 ext_prop->data,
1493 ext_prop->data_len);
1494 break;
1495 case USB_EXT_PROP_BINARY:
1496 usb_ext_prop_put_binary(buf, ret,
1497 ext_prop->data,
1498 ext_prop->data_len);
1499 break;
1500 case USB_EXT_PROP_LE32:
1501 /* not implemented */
1502 case USB_EXT_PROP_BE32:
1503 /* not implemented */
1504 default:
1505 return -EINVAL;
1506 }
1507 buf += count;
1508 }
1509 }
1510
1511 return 0;
1512}
1513
David Brownell40982be2008-06-19 17:52:58 -07001514/*
1515 * The setup() callback implements all the ep0 functionality that's
1516 * not handled lower down, in hardware or the hardware driver(like
1517 * device and endpoint feature flags, and their status). It's all
1518 * housekeeping for the gadget function we're implementing. Most of
1519 * the work is in config and function specific setup.
1520 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001521int
David Brownell40982be2008-06-19 17:52:58 -07001522composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1523{
1524 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1525 struct usb_request *req = cdev->req;
1526 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001527 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001528 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001529 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001530 u16 w_value = le16_to_cpu(ctrl->wValue);
1531 u16 w_length = le16_to_cpu(ctrl->wLength);
1532 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001533 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001534
1535 /* partial re-init of the response message; the function or the
1536 * gadget might need to intercept e.g. a control-OUT completion
1537 * when we delegate to it.
1538 */
1539 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001540 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001541 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301542 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001543 gadget->ep0->driver_data = cdev;
1544
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001545 /*
1546 * Don't let non-standard requests match any of the cases below
1547 * by accident.
1548 */
1549 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1550 goto unknown;
1551
David Brownell40982be2008-06-19 17:52:58 -07001552 switch (ctrl->bRequest) {
1553
1554 /* we handle all standard USB descriptors */
1555 case USB_REQ_GET_DESCRIPTOR:
1556 if (ctrl->bRequestType != USB_DIR_IN)
1557 goto unknown;
1558 switch (w_value >> 8) {
1559
1560 case USB_DT_DEVICE:
1561 cdev->desc.bNumConfigurations =
1562 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001563 cdev->desc.bMaxPacketSize0 =
1564 cdev->gadget->ep0->maxpacket;
1565 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001566 if (gadget->speed >= USB_SPEED_SUPER) {
John Youn1a853292016-02-05 17:05:40 -08001567 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001568 cdev->desc.bMaxPacketSize0 = 9;
1569 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001570 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001571 }
Igor Kotrasinski5527e732015-08-20 10:00:01 +02001572 } else {
1573 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001574 }
1575
David Brownell40982be2008-06-19 17:52:58 -07001576 value = min(w_length, (u16) sizeof cdev->desc);
1577 memcpy(req->buf, &cdev->desc, value);
1578 break;
1579 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001580 if (!gadget_is_dualspeed(gadget) ||
1581 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001582 break;
1583 device_qual(cdev);
1584 value = min_t(int, w_length,
1585 sizeof(struct usb_qualifier_descriptor));
1586 break;
1587 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001588 if (!gadget_is_dualspeed(gadget) ||
1589 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001590 break;
1591 /* FALLTHROUGH */
1592 case USB_DT_CONFIG:
1593 value = config_desc(cdev, w_value);
1594 if (value >= 0)
1595 value = min(w_length, (u16) value);
1596 break;
1597 case USB_DT_STRING:
1598 value = get_string(cdev, req->buf,
1599 w_index, w_value & 0xff);
1600 if (value >= 0)
1601 value = min(w_length, (u16) value);
1602 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001603 case USB_DT_BOS:
1604 if (gadget_is_superspeed(gadget)) {
1605 value = bos_desc(cdev);
1606 value = min(w_length, (u16) value);
1607 }
1608 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001609 case USB_DT_OTG:
1610 if (gadget_is_otg(gadget)) {
1611 struct usb_configuration *config;
1612 int otg_desc_len = 0;
1613
1614 if (cdev->config)
1615 config = cdev->config;
1616 else
1617 config = list_first_entry(
1618 &cdev->configs,
1619 struct usb_configuration, list);
1620 if (!config)
1621 goto done;
1622
1623 if (gadget->otg_caps &&
1624 (gadget->otg_caps->otg_rev >= 0x0200))
1625 otg_desc_len += sizeof(
1626 struct usb_otg20_descriptor);
1627 else
1628 otg_desc_len += sizeof(
1629 struct usb_otg_descriptor);
1630
1631 value = min_t(int, w_length, otg_desc_len);
1632 memcpy(req->buf, config->descriptors[0], value);
1633 }
1634 break;
David Brownell40982be2008-06-19 17:52:58 -07001635 }
1636 break;
1637
1638 /* any number of configs can work */
1639 case USB_REQ_SET_CONFIGURATION:
1640 if (ctrl->bRequestType != 0)
1641 goto unknown;
1642 if (gadget_is_otg(gadget)) {
1643 if (gadget->a_hnp_support)
1644 DBG(cdev, "HNP available\n");
1645 else if (gadget->a_alt_hnp_support)
1646 DBG(cdev, "HNP on another port\n");
1647 else
1648 VDBG(cdev, "HNP inactive\n");
1649 }
1650 spin_lock(&cdev->lock);
1651 value = set_config(cdev, ctrl, w_value);
1652 spin_unlock(&cdev->lock);
1653 break;
1654 case USB_REQ_GET_CONFIGURATION:
1655 if (ctrl->bRequestType != USB_DIR_IN)
1656 goto unknown;
1657 if (cdev->config)
1658 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1659 else
1660 *(u8 *)req->buf = 0;
1661 value = min(w_length, (u16) 1);
1662 break;
1663
1664 /* function drivers must handle get/set altsetting; if there's
1665 * no get() method, we know only altsetting zero works.
1666 */
1667 case USB_REQ_SET_INTERFACE:
1668 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1669 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001670 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001671 break;
Bryan Wu08889512009-01-08 00:21:19 +08001672 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001673 if (!f)
1674 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001675 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001676 break;
1677 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001678 if (value == USB_GADGET_DELAYED_STATUS) {
1679 DBG(cdev,
1680 "%s: interface %d (%s) requested delayed status\n",
1681 __func__, intf, f->name);
1682 cdev->delayed_status++;
1683 DBG(cdev, "delayed_status count %d\n",
1684 cdev->delayed_status);
1685 }
David Brownell40982be2008-06-19 17:52:58 -07001686 break;
1687 case USB_REQ_GET_INTERFACE:
1688 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1689 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001690 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001691 break;
Bryan Wu08889512009-01-08 00:21:19 +08001692 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001693 if (!f)
1694 break;
1695 /* lots of interfaces only need altsetting zero... */
1696 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1697 if (value < 0)
1698 break;
1699 *((u8 *)req->buf) = value;
1700 value = min(w_length, (u16) 1);
1701 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001702
1703 /*
1704 * USB 3.0 additions:
1705 * Function driver should handle get_status request. If such cb
1706 * wasn't supplied we respond with default value = 0
1707 * Note: function driver should supply such cb only for the first
1708 * interface of the function
1709 */
1710 case USB_REQ_GET_STATUS:
1711 if (!gadget_is_superspeed(gadget))
1712 goto unknown;
1713 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1714 goto unknown;
1715 value = 2; /* This is the length of the get_status reply */
1716 put_unaligned_le16(0, req->buf);
1717 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1718 break;
1719 f = cdev->config->interface[intf];
1720 if (!f)
1721 break;
1722 status = f->get_status ? f->get_status(f) : 0;
1723 if (status < 0)
1724 break;
1725 put_unaligned_le16(status & 0x0000ffff, req->buf);
1726 break;
1727 /*
1728 * Function drivers should handle SetFeature/ClearFeature
1729 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1730 * only for the first interface of the function
1731 */
1732 case USB_REQ_CLEAR_FEATURE:
1733 case USB_REQ_SET_FEATURE:
1734 if (!gadget_is_superspeed(gadget))
1735 goto unknown;
1736 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1737 goto unknown;
1738 switch (w_value) {
1739 case USB_INTRF_FUNC_SUSPEND:
1740 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1741 break;
1742 f = cdev->config->interface[intf];
1743 if (!f)
1744 break;
1745 value = 0;
1746 if (f->func_suspend)
1747 value = f->func_suspend(f, w_index >> 8);
1748 if (value < 0) {
1749 ERROR(cdev,
1750 "func_suspend() returned error %d\n",
1751 value);
1752 value = 0;
1753 }
1754 break;
1755 }
1756 break;
David Brownell40982be2008-06-19 17:52:58 -07001757 default:
1758unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001759 /*
1760 * OS descriptors handling
1761 */
1762 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001763 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001764 ctrl->bRequest == cdev->b_vendor_code) {
1765 struct usb_request *req;
1766 struct usb_configuration *os_desc_cfg;
1767 u8 *buf;
1768 int interface;
1769 int count = 0;
1770
1771 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001772 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001773 req->complete = composite_setup_complete;
1774 buf = req->buf;
1775 os_desc_cfg = cdev->os_desc_config;
1776 memset(buf, 0, w_length);
1777 buf[5] = 0x01;
1778 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1779 case USB_RECIP_DEVICE:
1780 if (w_index != 0x4 || (w_value >> 8))
1781 break;
1782 buf[6] = w_index;
1783 if (w_length == 0x10) {
1784 /* Number of ext compat interfaces */
1785 count = count_ext_compat(os_desc_cfg);
1786 buf[8] = count;
1787 count *= 24; /* 24 B/ext compat desc */
1788 count += 16; /* header */
1789 put_unaligned_le32(count, buf);
1790 value = w_length;
1791 } else {
1792 /* "extended compatibility ID"s */
1793 count = count_ext_compat(os_desc_cfg);
1794 buf[8] = count;
1795 count *= 24; /* 24 B/ext compat desc */
1796 count += 16; /* header */
1797 put_unaligned_le32(count, buf);
1798 buf += 16;
1799 fill_ext_compat(os_desc_cfg, buf);
1800 value = w_length;
1801 }
1802 break;
1803 case USB_RECIP_INTERFACE:
1804 if (w_index != 0x5 || (w_value >> 8))
1805 break;
1806 interface = w_value & 0xFF;
1807 buf[6] = w_index;
1808 if (w_length == 0x0A) {
1809 count = count_ext_prop(os_desc_cfg,
1810 interface);
1811 put_unaligned_le16(count, buf + 8);
1812 count = len_ext_prop(os_desc_cfg,
1813 interface);
1814 put_unaligned_le32(count, buf);
1815
1816 value = w_length;
1817 } else {
1818 count = count_ext_prop(os_desc_cfg,
1819 interface);
1820 put_unaligned_le16(count, buf + 8);
1821 count = len_ext_prop(os_desc_cfg,
1822 interface);
1823 put_unaligned_le32(count, buf);
1824 buf += 10;
1825 value = fill_ext_prop(os_desc_cfg,
1826 interface, buf);
1827 if (value < 0)
1828 return value;
1829
1830 value = w_length;
1831 }
1832 break;
1833 }
1834 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001835 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001836 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001837 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001838 if (value < 0) {
1839 DBG(cdev, "ep_queue --> %d\n", value);
1840 req->status = 0;
1841 composite_setup_complete(gadget->ep0, req);
1842 }
1843 return value;
1844 }
1845
David Brownell40982be2008-06-19 17:52:58 -07001846 VDBG(cdev,
1847 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1848 ctrl->bRequestType, ctrl->bRequest,
1849 w_value, w_index, w_length);
1850
Laurent Pinchart52426582009-10-21 00:03:38 +02001851 /* functions always handle their interfaces and endpoints...
1852 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001853 * configuration code.
1854 *
1855 * REVISIT it could make sense to let the composite device
1856 * take such requests too, if that's ever needed: to work
1857 * in config 0, etc.
1858 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301859 if (cdev->config) {
1860 list_for_each_entry(f, &cdev->config->functions, list)
1861 if (f->req_match && f->req_match(f, ctrl))
1862 goto try_fun_setup;
1863 f = NULL;
1864 }
1865
Laurent Pinchart52426582009-10-21 00:03:38 +02001866 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1867 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001868 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301869 break;
1870 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001871 break;
1872
1873 case USB_RECIP_ENDPOINT:
1874 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1875 list_for_each_entry(f, &cdev->config->functions, list) {
1876 if (test_bit(endp, f->endpoints))
1877 break;
1878 }
1879 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001880 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001881 break;
David Brownell40982be2008-06-19 17:52:58 -07001882 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01001883try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02001884 if (f && f->setup)
1885 value = f->setup(f, ctrl);
1886 else {
David Brownell40982be2008-06-19 17:52:58 -07001887 struct usb_configuration *c;
1888
1889 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001890 if (!c)
1891 goto done;
1892
1893 /* try current config's setup */
1894 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07001895 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001896 goto done;
1897 }
1898
1899 /* try the only function in the current config */
1900 if (!list_is_singular(&c->functions))
1901 goto done;
1902 f = list_first_entry(&c->functions, struct usb_function,
1903 list);
1904 if (f->setup)
1905 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07001906 }
1907
1908 goto done;
1909 }
1910
1911 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001912 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001913 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001914 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001915 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001916 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07001917 if (value < 0) {
1918 DBG(cdev, "ep_queue --> %d\n", value);
1919 req->status = 0;
1920 composite_setup_complete(gadget->ep0, req);
1921 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001922 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1923 WARN(cdev,
1924 "%s: Delayed status not supported for w_length != 0",
1925 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001926 }
1927
1928done:
1929 /* device either stalls (value < 0) or reports success */
1930 return value;
1931}
1932
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001933void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001934{
1935 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1936 unsigned long flags;
1937
1938 /* REVISIT: should we have config and device level
1939 * disconnect callbacks?
1940 */
1941 spin_lock_irqsave(&cdev->lock, flags);
1942 if (cdev->config)
1943 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001944 if (cdev->driver->disconnect)
1945 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001946 spin_unlock_irqrestore(&cdev->lock, flags);
1947}
1948
1949/*-------------------------------------------------------------------------*/
1950
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001951static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1952 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001953{
1954 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1955 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1956
1957 return sprintf(buf, "%d\n", cdev->suspended);
1958}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001959static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001960
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001961static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07001962{
1963 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1964
1965 /* composite_disconnect() must already have been called
1966 * by the underlying peripheral controller driver!
1967 * so there's no i/o concurrency that could affect the
1968 * state protected by cdev->lock.
1969 */
1970 WARN_ON(cdev->config);
1971
1972 while (!list_empty(&cdev->configs)) {
1973 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001974 c = list_first_entry(&cdev->configs,
1975 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001976 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001977 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001978 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001979 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001980
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001981 composite_dev_cleanup(cdev);
1982
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001983 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001984 kfree(cdev);
1985 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001986}
1987
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001988static void composite_unbind(struct usb_gadget *gadget)
1989{
1990 __composite_unbind(gadget, true);
1991}
1992
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001993static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1994 const struct usb_device_descriptor *old)
1995{
1996 __le16 idVendor;
1997 __le16 idProduct;
1998 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001999 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002000 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002001 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002002
2003 /*
2004 * these variables may have been set in
2005 * usb_composite_overwrite_options()
2006 */
2007 idVendor = new->idVendor;
2008 idProduct = new->idProduct;
2009 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002010 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002011 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002012 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002013
2014 *new = *old;
2015 if (idVendor)
2016 new->idVendor = idVendor;
2017 if (idProduct)
2018 new->idProduct = idProduct;
2019 if (bcdDevice)
2020 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002021 else
2022 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002023 if (iSerialNumber)
2024 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002025 if (iManufacturer)
2026 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002027 if (iProduct)
2028 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002029}
2030
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002031int composite_dev_prepare(struct usb_composite_driver *composite,
2032 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002033{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002034 struct usb_gadget *gadget = cdev->gadget;
2035 int ret = -ENOMEM;
2036
2037 /* preallocate control response and buffer */
2038 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2039 if (!cdev->req)
2040 return -ENOMEM;
2041
2042 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2043 if (!cdev->req->buf)
2044 goto fail;
2045
2046 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2047 if (ret)
2048 goto fail_dev;
2049
2050 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002051 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002052 gadget->ep0->driver_data = cdev;
2053
2054 cdev->driver = composite;
2055
2056 /*
2057 * As per USB compliance update, a device that is actively drawing
2058 * more than 100mA from USB must report itself as bus-powered in
2059 * the GetStatus(DEVICE) call.
2060 */
2061 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2062 usb_gadget_set_selfpowered(gadget);
2063
2064 /* interface and string IDs start at zero via kzalloc.
2065 * we force endpoints to start unassigned; few controller
2066 * drivers will zero ep->driver_data.
2067 */
2068 usb_ep_autoconfig_reset(gadget);
2069 return 0;
2070fail_dev:
2071 kfree(cdev->req->buf);
2072fail:
2073 usb_ep_free_request(gadget->ep0, cdev->req);
2074 cdev->req = NULL;
2075 return ret;
2076}
2077
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002078int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2079 struct usb_ep *ep0)
2080{
2081 int ret = 0;
2082
2083 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2084 if (!cdev->os_desc_req) {
2085 ret = PTR_ERR(cdev->os_desc_req);
2086 goto end;
2087 }
2088
2089 /* OS feature descriptor length <= 4kB */
2090 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2091 if (!cdev->os_desc_req->buf) {
2092 ret = PTR_ERR(cdev->os_desc_req->buf);
2093 kfree(cdev->os_desc_req);
2094 goto end;
2095 }
Felipe Balbi57943712014-09-18 09:54:54 -05002096 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002097 cdev->os_desc_req->complete = composite_setup_complete;
2098end:
2099 return ret;
2100}
2101
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002102void composite_dev_cleanup(struct usb_composite_dev *cdev)
2103{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002104 struct usb_gadget_string_container *uc, *tmp;
2105
2106 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2107 list_del(&uc->list);
2108 kfree(uc);
2109 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002110 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002111 if (cdev->os_desc_pending)
2112 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2113
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002114 kfree(cdev->os_desc_req->buf);
2115 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2116 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002117 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002118 if (cdev->setup_pending)
2119 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2120
Li Junbe0a8882014-08-28 21:44:11 +08002121 kfree(cdev->req->buf);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002122 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2123 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002124 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002125 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002126}
2127
2128static int composite_bind(struct usb_gadget *gadget,
2129 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002130{
2131 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002132 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002133 int status = -ENOMEM;
2134
2135 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2136 if (!cdev)
2137 return status;
2138
2139 spin_lock_init(&cdev->lock);
2140 cdev->gadget = gadget;
2141 set_gadget_data(gadget, cdev);
2142 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002143 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002144
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002145 status = composite_dev_prepare(composite, cdev);
2146 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002147 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002148
2149 /* composite gadget needs to assign strings for whole device (like
2150 * serial number), register function drivers, potentially update
2151 * power state and consumption, etc
2152 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002153 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002154 if (status < 0)
2155 goto fail;
2156
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002157 if (cdev->use_os_string) {
2158 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2159 if (status)
2160 goto fail;
2161 }
2162
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002163 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002164
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002165 /* has userspace failed to provide a serial number? */
2166 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2167 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2168
David Brownell40982be2008-06-19 17:52:58 -07002169 INFO(cdev, "%s ready\n", composite->name);
2170 return 0;
2171
2172fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002173 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002174 return status;
2175}
2176
2177/*-------------------------------------------------------------------------*/
2178
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002179void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002180{
2181 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2182 struct usb_function *f;
2183
David Brownell89429392009-03-19 14:14:17 -07002184 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002185 * suspend/resume callbacks?
2186 */
2187 DBG(cdev, "suspend\n");
2188 if (cdev->config) {
2189 list_for_each_entry(f, &cdev->config->functions, list) {
2190 if (f->suspend)
2191 f->suspend(f);
2192 }
2193 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002194 if (cdev->driver->suspend)
2195 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002196
2197 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08002198
2199 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002200}
2201
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002202void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002203{
2204 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2205 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00002206 u16 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07002207
David Brownell89429392009-03-19 14:14:17 -07002208 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002209 * suspend/resume callbacks?
2210 */
2211 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002212 if (cdev->driver->resume)
2213 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002214 if (cdev->config) {
2215 list_for_each_entry(f, &cdev->config->functions, list) {
2216 if (f->resume)
2217 f->resume(f);
2218 }
Hao Wub23f2f92010-11-29 15:17:03 +08002219
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002220 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08002221
2222 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002223 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002224 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002225
2226 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002227}
2228
2229/*-------------------------------------------------------------------------*/
2230
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002231static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002232 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002233 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002234
2235 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002236 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002237 .disconnect = composite_disconnect,
2238
2239 .suspend = composite_suspend,
2240 .resume = composite_resume,
2241
2242 .driver = {
2243 .owner = THIS_MODULE,
2244 },
2245};
2246
2247/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002248 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002249 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002250 *
David Brownell40982be2008-06-19 17:52:58 -07002251 * Context: single threaded during gadget setup
2252 *
2253 * This function is used to register drivers using the composite driver
2254 * framework. The return value is zero, or a negative errno value.
2255 * Those values normally come from the driver's @bind method, which does
2256 * all the work of setting up the driver to match the hardware.
2257 *
2258 * On successful return, the gadget is ready to respond to requests from
2259 * the host, unless one of its components invokes usb_gadget_disconnect()
2260 * while it was binding. That would usually be done in order to wait for
2261 * some userspace participation.
2262 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002263int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002264{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002265 struct usb_gadget_driver *gadget_driver;
2266
2267 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002268 return -EINVAL;
2269
2270 if (!driver->name)
2271 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002272
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002273 driver->gadget_driver = composite_driver_template;
2274 gadget_driver = &driver->gadget_driver;
2275
2276 gadget_driver->function = (char *) driver->name;
2277 gadget_driver->driver.name = driver->name;
2278 gadget_driver->max_speed = driver->max_speed;
2279
2280 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002281}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002282EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002283
2284/**
2285 * usb_composite_unregister() - unregister a composite driver
2286 * @driver: the driver to unregister
2287 *
2288 * This function is used to unregister drivers using the composite
2289 * driver framework.
2290 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002291void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002292{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002293 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002294}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002295EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002296
2297/**
2298 * usb_composite_setup_continue() - Continue with the control transfer
2299 * @cdev: the composite device who's control transfer was kept waiting
2300 *
2301 * This function must be called by the USB function driver to continue
2302 * with the control transfer's data/status stage in case it had requested to
2303 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2304 * can request the composite framework to delay the setup request's data/status
2305 * stages by returning USB_GADGET_DELAYED_STATUS.
2306 */
2307void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2308{
2309 int value;
2310 struct usb_request *req = cdev->req;
2311 unsigned long flags;
2312
2313 DBG(cdev, "%s\n", __func__);
2314 spin_lock_irqsave(&cdev->lock, flags);
2315
2316 if (cdev->delayed_status == 0) {
2317 WARN(cdev, "%s: Unexpected call\n", __func__);
2318
2319 } else if (--cdev->delayed_status == 0) {
2320 DBG(cdev, "%s: Completing delayed status\n", __func__);
2321 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002322 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002323 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002324 if (value < 0) {
2325 DBG(cdev, "ep_queue --> %d\n", value);
2326 req->status = 0;
2327 composite_setup_complete(cdev->gadget->ep0, req);
2328 }
2329 }
2330
2331 spin_unlock_irqrestore(&cdev->lock, flags);
2332}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002333EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002334
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002335static char *composite_default_mfr(struct usb_gadget *gadget)
2336{
2337 char *mfr;
2338 int len;
2339
2340 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2341 init_utsname()->release, gadget->name);
2342 len++;
2343 mfr = kmalloc(len, GFP_KERNEL);
2344 if (!mfr)
2345 return NULL;
2346 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2347 init_utsname()->release, gadget->name);
2348 return mfr;
2349}
2350
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002351void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2352 struct usb_composite_overwrite *covr)
2353{
2354 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002355 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2356 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002357
2358 if (covr->idVendor)
2359 desc->idVendor = cpu_to_le16(covr->idVendor);
2360
2361 if (covr->idProduct)
2362 desc->idProduct = cpu_to_le16(covr->idProduct);
2363
2364 if (covr->bcdDevice)
2365 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002366
2367 if (covr->serial_number) {
2368 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2369 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2370 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002371 if (covr->manufacturer) {
2372 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2373 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002374
2375 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2376 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2377 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2378 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002379 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002380
2381 if (covr->product) {
2382 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2383 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2384 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002385}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002386EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002387
2388MODULE_LICENSE("GPL");
2389MODULE_AUTHOR("David Brownell");