blob: 275245916ed5d1d53fe24ac835f5f70690eaca0a [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
David Brownell40982be2008-06-19 17:52:58 -07002/*
3 * composite.c - infrastructure for Composite USB Gadgets
4 *
5 * Copyright (C) 2006-2008 David Brownell
David Brownell40982be2008-06-19 17:52:58 -07006 */
7
8/* #define VERBOSE_DEBUG */
9
10#include <linux/kallsyms.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040013#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070014#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020015#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070016
17#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080018#include <linux/usb/otg.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030019#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070020
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020021#include "u_os_desc.h"
22
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020023/**
24 * struct usb_os_string - represents OS String to be reported by a gadget
25 * @bLength: total length of the entire descritor, always 0x12
26 * @bDescriptorType: USB_DT_STRING
27 * @qwSignature: the OS String proper
28 * @bMS_VendorCode: code used by the host for subsequent requests
29 * @bPad: not used, must be zero
30 */
31struct usb_os_string {
32 __u8 bLength;
33 __u8 bDescriptorType;
34 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
35 __u8 bMS_VendorCode;
36 __u8 bPad;
37} __packed;
38
David Brownell40982be2008-06-19 17:52:58 -070039/*
40 * The code in this file is utility code, used to build a gadget driver
41 * from one or more "function" drivers, one or more "configuration"
42 * objects, and a "usb_composite_driver" by gluing them together along
43 * with the relevant device-wide data.
44 */
45
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010046static struct usb_gadget_strings **get_containers_gs(
47 struct usb_gadget_string_container *uc)
48{
49 return (struct usb_gadget_strings **)uc->stash;
50}
51
Tatyana Brokhman48767a42011-06-28 16:33:49 +030052/**
John Younf3bdbe32016-02-05 17:07:03 -080053 * function_descriptors() - get function descriptors for speed
54 * @f: the function
55 * @speed: the speed
56 *
57 * Returns the descriptors or NULL if not set.
58 */
59static struct usb_descriptor_header **
60function_descriptors(struct usb_function *f,
61 enum usb_device_speed speed)
62{
63 struct usb_descriptor_header **descriptors;
64
Felipe Balbi08782632016-04-22 14:53:47 +030065 /*
66 * NOTE: we try to help gadget drivers which might not be setting
67 * max_speed appropriately.
68 */
69
John Younf3bdbe32016-02-05 17:07:03 -080070 switch (speed) {
71 case USB_SPEED_SUPER_PLUS:
72 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030073 if (descriptors)
74 break;
75 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080076 case USB_SPEED_SUPER:
77 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030078 if (descriptors)
79 break;
80 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080081 case USB_SPEED_HIGH:
82 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030083 if (descriptors)
84 break;
85 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080086 default:
87 descriptors = f->fs_descriptors;
88 }
89
Felipe Balbi08782632016-04-22 14:53:47 +030090 /*
91 * if we can't find any descriptors at all, then this gadget deserves to
92 * Oops with a NULL pointer dereference
93 */
94
John Younf3bdbe32016-02-05 17:07:03 -080095 return descriptors;
96}
97
98/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +030099 * next_ep_desc() - advance to the next EP descriptor
100 * @t: currect pointer within descriptor array
101 *
102 * Return: next EP descriptor or NULL
103 *
104 * Iterate over @t until either EP descriptor found or
105 * NULL (that indicates end of list) encountered
106 */
107static struct usb_descriptor_header**
108next_ep_desc(struct usb_descriptor_header **t)
109{
110 for (; *t; t++) {
111 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
112 return t;
113 }
114 return NULL;
115}
116
117/*
118 * for_each_ep_desc()- iterate over endpoint descriptors in the
119 * descriptors list
120 * @start: pointer within descriptor array.
121 * @ep_desc: endpoint descriptor to use as the loop cursor
122 */
123#define for_each_ep_desc(start, ep_desc) \
124 for (ep_desc = next_ep_desc(start); \
125 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
126
127/**
128 * config_ep_by_speed() - configures the given endpoint
129 * according to gadget speed.
130 * @g: pointer to the gadget
131 * @f: usb function
132 * @_ep: the endpoint to configure
133 *
134 * Return: error code, 0 on success
135 *
136 * This function chooses the right descriptors for a given
137 * endpoint according to gadget speed and saves it in the
138 * endpoint desc field. If the endpoint already has a descriptor
139 * assigned to it - overwrites it with currently corresponding
140 * descriptor. The endpoint maxpacket field is updated according
141 * to the chosen descriptor.
142 * Note: the supplied function should hold all the descriptors
143 * for supported speeds
144 */
145int config_ep_by_speed(struct usb_gadget *g,
146 struct usb_function *f,
147 struct usb_ep *_ep)
148{
149 struct usb_endpoint_descriptor *chosen_desc = NULL;
150 struct usb_descriptor_header **speed_desc = NULL;
151
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300152 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
153 int want_comp_desc = 0;
154
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300155 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
156
157 if (!g || !f || !_ep)
158 return -EIO;
159
160 /* select desired speed */
161 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800162 case USB_SPEED_SUPER_PLUS:
163 if (gadget_is_superspeed_plus(g)) {
164 speed_desc = f->ssp_descriptors;
165 want_comp_desc = 1;
166 break;
167 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500168 /* fall through */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300169 case USB_SPEED_SUPER:
170 if (gadget_is_superspeed(g)) {
171 speed_desc = f->ss_descriptors;
172 want_comp_desc = 1;
173 break;
174 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500175 /* fall through */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300176 case USB_SPEED_HIGH:
177 if (gadget_is_dualspeed(g)) {
178 speed_desc = f->hs_descriptors;
179 break;
180 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500181 /* fall through */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300182 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200183 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300184 }
185 /* find descriptors */
186 for_each_ep_desc(speed_desc, d_spd) {
187 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
188 if (chosen_desc->bEndpointAddress == _ep->address)
189 goto ep_found;
190 }
191 return -EIO;
192
193ep_found:
194 /* commit results */
Felipe Balbi9ad58772016-09-28 14:17:38 +0300195 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300196 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300197 _ep->comp_desc = NULL;
198 _ep->maxburst = 0;
Felipe Balbieaa496f2016-09-28 12:33:31 +0300199 _ep->mult = 1;
200
201 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
202 usb_endpoint_xfer_int(_ep->desc)))
203 _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
204
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300205 if (!want_comp_desc)
206 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300207
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300208 /*
209 * Companion descriptor should follow EP descriptor
210 * USB 3.0 spec, #9.6.7
211 */
212 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
213 if (!comp_desc ||
214 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
215 return -EIO;
216 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800217 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300218 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300219 case USB_ENDPOINT_XFER_ISOC:
220 /* mult: bits 1:0 of bmAttributes */
Felipe Balbieaa496f2016-09-28 12:33:31 +0300221 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500222 /* fall through */
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800223 case USB_ENDPOINT_XFER_BULK:
224 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300225 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300226 break;
227 default:
Colin Ian Kingb2fc0592017-11-14 16:18:28 +0000228 if (comp_desc->bMaxBurst != 0) {
229 struct usb_composite_dev *cdev;
230
231 cdev = get_gadget_data(g);
Felipe Balbib785ea72012-06-06 10:20:23 +0300232 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
Colin Ian Kingb2fc0592017-11-14 16:18:28 +0000233 }
Felipe Balbib785ea72012-06-06 10:20:23 +0300234 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300235 break;
236 }
237 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300238 return 0;
239}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200240EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700241
242/**
243 * usb_add_function() - add a function to a configuration
244 * @config: the configuration
245 * @function: the function being added
246 * Context: single threaded during gadget setup
247 *
248 * After initialization, each configuration must have one or more
249 * functions added to it. Adding a function involves calling its @bind()
250 * method to allocate resources such as interface and string identifiers
251 * and endpoints.
252 *
253 * This function returns the value of the function's bind(), which is
254 * zero for success else a negative errno value.
255 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200256int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700257 struct usb_function *function)
258{
259 int value = -EINVAL;
260
261 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
262 function->name, function,
263 config->label, config);
264
265 if (!function->set_alt || !function->disable)
266 goto done;
267
268 function->config = config;
Danny Segald6c40da2016-12-06 15:35:24 -0800269 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700270 list_add_tail(&function->list, &config->functions);
271
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200272 if (function->bind_deactivated) {
273 value = usb_function_deactivate(function);
274 if (value)
275 goto done;
276 }
277
David Brownell40982be2008-06-19 17:52:58 -0700278 /* REVISIT *require* function->bind? */
279 if (function->bind) {
280 value = function->bind(config, function);
281 if (value < 0) {
282 list_del(&function->list);
283 function->config = NULL;
284 }
285 } else
286 value = 0;
287
288 /* We allow configurations that don't work at both speeds.
289 * If we run into a lowspeed Linux system, treat it the same
290 * as full speed ... it's the function drivers that will need
291 * to avoid bulk and ISO transfers.
292 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200293 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700294 config->fullspeed = true;
295 if (!config->highspeed && function->hs_descriptors)
296 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300297 if (!config->superspeed && function->ss_descriptors)
298 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800299 if (!config->superspeed_plus && function->ssp_descriptors)
300 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700301
302done:
303 if (value)
304 DBG(config->cdev, "adding '%s'/%p --> %d\n",
305 function->name, function, value);
306 return value;
307}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200308EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700309
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100310void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
311{
312 if (f->disable)
313 f->disable(f);
314
315 bitmap_zero(f->endpoints, 32);
316 list_del(&f->list);
317 if (f->unbind)
318 f->unbind(c, f);
Felipe Balbi0e3e9752017-06-06 14:47:29 +0300319
320 if (f->bind_deactivated)
321 usb_function_activate(f);
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100322}
323EXPORT_SYMBOL_GPL(usb_remove_function);
324
David Brownell40982be2008-06-19 17:52:58 -0700325/**
David Brownell60beed92008-08-18 17:38:22 -0700326 * usb_function_deactivate - prevent function and gadget enumeration
327 * @function: the function that isn't yet ready to respond
328 *
329 * Blocks response of the gadget driver to host enumeration by
330 * preventing the data line pullup from being activated. This is
331 * normally called during @bind() processing to change from the
332 * initial "ready to respond" state, or when a required resource
333 * becomes available.
334 *
335 * For example, drivers that serve as a passthrough to a userspace
336 * daemon can block enumeration unless that daemon (such as an OBEX,
337 * MTP, or print server) is ready to handle host requests.
338 *
339 * Not all systems support software control of their USB peripheral
340 * data pullups.
341 *
342 * Returns zero on success, else negative errno.
343 */
344int usb_function_deactivate(struct usb_function *function)
345{
346 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200347 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700348 int status = 0;
349
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200350 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700351
352 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200353 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700354 if (status == 0)
355 cdev->deactivations++;
356
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200357 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700358 return status;
359}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200360EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700361
362/**
363 * usb_function_activate - allow function and gadget enumeration
364 * @function: function on which usb_function_activate() was called
365 *
366 * Reverses effect of usb_function_deactivate(). If no more functions
367 * are delaying their activation, the gadget driver will respond to
368 * host enumeration procedures.
369 *
370 * Returns zero on success, else negative errno.
371 */
372int usb_function_activate(struct usb_function *function)
373{
374 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200375 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700376 int status = 0;
377
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200378 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700379
380 if (WARN_ON(cdev->deactivations == 0))
381 status = -EINVAL;
382 else {
383 cdev->deactivations--;
384 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200385 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700386 }
387
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200388 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700389 return status;
390}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200391EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700392
393/**
David Brownell40982be2008-06-19 17:52:58 -0700394 * usb_interface_id() - allocate an unused interface ID
395 * @config: configuration associated with the interface
396 * @function: function handling the interface
397 * Context: single threaded during gadget setup
398 *
399 * usb_interface_id() is called from usb_function.bind() callbacks to
400 * allocate new interface IDs. The function driver will then store that
401 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300402 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700403 * particularly changing its altsetting via set_alt(). There may
404 * also be class-specific or vendor-specific requests to handle.
405 *
406 * All interface identifier should be allocated using this routine, to
407 * ensure that for example different functions don't wrongly assign
408 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300409 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700410 * one configuration (or more than once in a given configuration) need
411 * multiple versions of the relevant descriptors.
412 *
413 * Returns the interface ID which was allocated; or -ENODEV if no
414 * more interface IDs can be allocated.
415 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200416int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700417 struct usb_function *function)
418{
419 unsigned id = config->next_interface_id;
420
421 if (id < MAX_CONFIG_INTERFACES) {
422 config->interface[id] = function;
Danny Segald6c40da2016-12-06 15:35:24 -0800423 if (function->intf_id < 0)
424 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700425 config->next_interface_id = id + 1;
426 return id;
427 }
428 return -ENODEV;
429}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200430EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700431
Danny Segald6c40da2016-12-06 15:35:24 -0800432static int usb_func_wakeup_int(struct usb_function *func)
433{
434 int ret;
435 struct usb_gadget *gadget;
436
437 pr_debug("%s - %s function wakeup\n",
438 __func__, func->name ? func->name : "");
439
440 if (!func || !func->config || !func->config->cdev ||
441 !func->config->cdev->gadget)
442 return -EINVAL;
443
444 gadget = func->config->cdev->gadget;
445 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
446 DBG(func->config->cdev,
447 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
448 gadget->speed,
449 func->func_wakeup_allowed);
450
451 return -ENOTSUPP;
452 }
453
454 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
455
456 return ret;
457}
458
459int usb_func_wakeup(struct usb_function *func)
460{
461 int ret;
462 unsigned long flags;
463
464 pr_debug("%s function wakeup\n",
465 func->name ? func->name : "");
466
467 spin_lock_irqsave(&func->config->cdev->lock, flags);
468 ret = usb_func_wakeup_int(func);
469 if (ret == -EAGAIN) {
470 DBG(func->config->cdev,
471 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
472 func->name ? func->name : "");
473 ret = 0;
474 } else if (ret < 0 && ret != -ENOTSUPP) {
475 ERROR(func->config->cdev,
476 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
477 func->name ? func->name : "", ret);
478 }
479
480 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
481 return ret;
482}
483EXPORT_SYMBOL(usb_func_wakeup);
484
485int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
486 struct usb_request *req, gfp_t gfp_flags)
487{
488 int ret;
489 struct usb_gadget *gadget;
490
491 if (!func || !func->config || !func->config->cdev ||
492 !func->config->cdev->gadget || !ep || !req) {
493 ret = -EINVAL;
494 goto done;
495 }
496
497 pr_debug("Function %s queueing new data into ep %u\n",
498 func->name ? func->name : "", ep->address);
499
500 gadget = func->config->cdev->gadget;
501 if (func->func_is_suspended && func->func_wakeup_allowed) {
502 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
503 if (ret == -EAGAIN) {
504 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
505 func->name ? func->name : "");
506 } else if (ret < 0 && ret != -ENOTSUPP) {
507 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
508 func->name ? func->name : "", ret);
509 }
510 goto done;
511 }
512
513 if (!func->func_is_suspended)
514 ret = 0;
515
516 if (func->func_is_suspended && !func->func_wakeup_allowed) {
517 ret = -ENOTSUPP;
518 goto done;
519 }
520
521 ret = usb_ep_queue(ep, req, gfp_flags);
522done:
523 return ret;
524}
525EXPORT_SYMBOL(usb_func_ep_queue);
526
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100527static u8 encode_bMaxPower(enum usb_device_speed speed,
528 struct usb_configuration *c)
529{
530 unsigned val;
531
532 if (c->MaxPower)
533 val = c->MaxPower;
534 else
535 val = CONFIG_USB_GADGET_VBUS_DRAW;
536 if (!val)
537 return 0;
538 switch (speed) {
539 case USB_SPEED_SUPER:
540 return DIV_ROUND_UP(val, 8);
541 default:
Jack Phamc17588e2018-01-18 10:53:56 -0800542 /* only SuperSpeed and faster support > 500mA */
543 return DIV_ROUND_UP(min(val, 500U), 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700544 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100545}
546
David Brownell40982be2008-06-19 17:52:58 -0700547static int config_buf(struct usb_configuration *config,
548 enum usb_device_speed speed, void *buf, u8 type)
549{
550 struct usb_config_descriptor *c = buf;
551 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200552 int len;
David Brownell40982be2008-06-19 17:52:58 -0700553 struct usb_function *f;
554 int status;
555
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200556 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700557 /* write the config descriptor */
558 c = buf;
559 c->bLength = USB_DT_CONFIG_SIZE;
560 c->bDescriptorType = type;
561 /* wTotalLength is written later */
562 c->bNumInterfaces = config->next_interface_id;
563 c->bConfigurationValue = config->bConfigurationValue;
564 c->iConfiguration = config->iConfiguration;
565 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100566 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700567
568 /* There may be e.g. OTG descriptors */
569 if (config->descriptors) {
570 status = usb_descriptor_fillbuf(next, len,
571 config->descriptors);
572 if (status < 0)
573 return status;
574 len -= status;
575 next += status;
576 }
577
578 /* add each function's descriptors */
579 list_for_each_entry(f, &config->functions, list) {
580 struct usb_descriptor_header **descriptors;
581
John Younf3bdbe32016-02-05 17:07:03 -0800582 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700583 if (!descriptors)
584 continue;
585 status = usb_descriptor_fillbuf(next, len,
586 (const struct usb_descriptor_header **) descriptors);
587 if (status < 0)
588 return status;
589 len -= status;
590 next += status;
591 }
592
593 len = next - buf;
594 c->wTotalLength = cpu_to_le16(len);
595 return len;
596}
597
598static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
599{
600 struct usb_gadget *gadget = cdev->gadget;
601 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200602 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700603 u8 type = w_value >> 8;
604 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
605
John Youneae58202016-02-05 17:07:17 -0800606 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300607 speed = gadget->speed;
608 else if (gadget_is_dualspeed(gadget)) {
609 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700610 if (gadget->speed == USB_SPEED_HIGH)
611 hs = 1;
612 if (type == USB_DT_OTHER_SPEED_CONFIG)
613 hs = !hs;
614 if (hs)
615 speed = USB_SPEED_HIGH;
616
617 }
618
619 /* This is a lookup by config *INDEX* */
620 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200621
622 pos = &cdev->configs;
623 c = cdev->os_desc_config;
624 if (c)
625 goto check_config;
626
627 while ((pos = pos->next) != &cdev->configs) {
628 c = list_entry(pos, typeof(*c), list);
629
630 /* skip OS Descriptors config which is handled separately */
631 if (c == cdev->os_desc_config)
632 continue;
633
634check_config:
David Brownell40982be2008-06-19 17:52:58 -0700635 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300636 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800637 case USB_SPEED_SUPER_PLUS:
638 if (!c->superspeed_plus)
639 continue;
640 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300641 case USB_SPEED_SUPER:
642 if (!c->superspeed)
643 continue;
644 break;
645 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700646 if (!c->highspeed)
647 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300648 break;
649 default:
David Brownell40982be2008-06-19 17:52:58 -0700650 if (!c->fullspeed)
651 continue;
652 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300653
David Brownell40982be2008-06-19 17:52:58 -0700654 if (w_value == 0)
655 return config_buf(c, speed, cdev->req->buf, type);
656 w_value--;
657 }
658 return -EINVAL;
659}
660
661static int count_configs(struct usb_composite_dev *cdev, unsigned type)
662{
663 struct usb_gadget *gadget = cdev->gadget;
664 struct usb_configuration *c;
665 unsigned count = 0;
666 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300667 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800668 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700669
670 if (gadget_is_dualspeed(gadget)) {
671 if (gadget->speed == USB_SPEED_HIGH)
672 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300673 if (gadget->speed == USB_SPEED_SUPER)
674 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800675 if (gadget->speed == USB_SPEED_SUPER_PLUS)
676 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700677 if (type == USB_DT_DEVICE_QUALIFIER)
678 hs = !hs;
679 }
680 list_for_each_entry(c, &cdev->configs, list) {
681 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800682 if (ssp) {
683 if (!c->superspeed_plus)
684 continue;
685 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300686 if (!c->superspeed)
687 continue;
688 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700689 if (!c->highspeed)
690 continue;
691 } else {
692 if (!c->fullspeed)
693 continue;
694 }
695 count++;
696 }
697 return count;
698}
699
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300700/**
701 * bos_desc() - prepares the BOS descriptor.
702 * @cdev: pointer to usb_composite device to generate the bos
703 * descriptor for
704 *
705 * This function generates the BOS (Binary Device Object)
706 * descriptor and its device capabilities descriptors. The BOS
707 * descriptor should be supported by a SuperSpeed device.
708 */
709static int bos_desc(struct usb_composite_dev *cdev)
710{
711 struct usb_ext_cap_descriptor *usb_ext;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300712 struct usb_dcd_config_params dcd_config_params;
713 struct usb_bos_descriptor *bos = cdev->req->buf;
714
715 bos->bLength = USB_DT_BOS_SIZE;
716 bos->bDescriptorType = USB_DT_BOS;
717
718 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
719 bos->bNumDeviceCaps = 0;
720
721 /*
722 * A SuperSpeed device shall include the USB2.0 extension descriptor
723 * and shall support LPM when operating in USB2.0 HS mode.
724 */
725 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
726 bos->bNumDeviceCaps++;
727 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
728 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
729 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
730 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500731 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300732
733 /*
734 * The Superspeed USB Capability descriptor shall be implemented by all
735 * SuperSpeed devices.
736 */
John Youn0b67a6b2017-04-28 12:55:14 +0400737 if (gadget_is_superspeed(cdev->gadget)) {
738 struct usb_ss_cap_descriptor *ss_cap;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300739
John Youn0b67a6b2017-04-28 12:55:14 +0400740 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
741 bos->bNumDeviceCaps++;
742 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
743 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
744 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
745 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
746 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
747 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
748 USB_FULL_SPEED_OPERATION |
749 USB_HIGH_SPEED_OPERATION |
750 USB_5GBPS_OPERATION);
751 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
752
753 /* Get Controller configuration */
754 if (cdev->gadget->ops->get_config_params) {
755 cdev->gadget->ops->get_config_params(
756 &dcd_config_params);
757 } else {
758 dcd_config_params.bU1devExitLat =
759 USB_DEFAULT_U1_DEV_EXIT_LAT;
760 dcd_config_params.bU2DevExitLat =
761 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
762 }
763 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
764 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300765 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300766
John Younf228a8d2016-02-05 17:05:53 -0800767 /* The SuperSpeedPlus USB Device Capability descriptor */
768 if (gadget_is_superspeed_plus(cdev->gadget)) {
769 struct usb_ssp_cap_descriptor *ssp_cap;
770
771 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
772 bos->bNumDeviceCaps++;
773
774 /*
775 * Report typical values.
776 */
777
778 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
779 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
780 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
781 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700782 ssp_cap->bReserved = 0;
783 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800784
785 /* SSAC = 1 (2 attributes) */
786 ssp_cap->bmAttributes = cpu_to_le32(1);
787
788 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700789 ssp_cap->wFunctionalitySupport =
790 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800791
792 /*
793 * bmSublinkSpeedAttr[0]:
794 * ST = Symmetric, RX
795 * LSE = 3 (Gbps)
796 * LP = 1 (SuperSpeedPlus)
797 * LSM = 10 (10 Gbps)
798 */
799 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700800 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800801 /*
802 * bmSublinkSpeedAttr[1] =
803 * ST = Symmetric, TX
804 * LSE = 3 (Gbps)
805 * LP = 1 (SuperSpeedPlus)
806 * LSM = 10 (10 Gbps)
807 */
808 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700809 cpu_to_le32((3 << 4) | (1 << 14) |
810 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800811 }
812
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300813 return le16_to_cpu(bos->wTotalLength);
814}
815
David Brownell40982be2008-06-19 17:52:58 -0700816static void device_qual(struct usb_composite_dev *cdev)
817{
818 struct usb_qualifier_descriptor *qual = cdev->req->buf;
819
820 qual->bLength = sizeof(*qual);
821 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
822 /* POLICY: same bcdUSB and device type info at both speeds */
823 qual->bcdUSB = cdev->desc.bcdUSB;
824 qual->bDeviceClass = cdev->desc.bDeviceClass;
825 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
826 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
827 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200828 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700829 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700830 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700831}
832
833/*-------------------------------------------------------------------------*/
834
835static void reset_config(struct usb_composite_dev *cdev)
836{
837 struct usb_function *f;
838
839 DBG(cdev, "reset config\n");
840
841 list_for_each_entry(f, &cdev->config->functions, list) {
842 if (f->disable)
843 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200844
Danny Segald6c40da2016-12-06 15:35:24 -0800845 /* USB 3.0 addition */
846 f->func_is_suspended = false;
847 f->func_wakeup_allowed = false;
848 f->func_wakeup_pending = false;
849
Laurent Pinchart52426582009-10-21 00:03:38 +0200850 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700851 }
852 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100853 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700854}
855
856static int set_config(struct usb_composite_dev *cdev,
857 const struct usb_ctrlrequest *ctrl, unsigned number)
858{
859 struct usb_gadget *gadget = cdev->gadget;
860 struct usb_configuration *c = NULL;
861 int result = -EINVAL;
862 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
863 int tmp;
864
David Brownell40982be2008-06-19 17:52:58 -0700865 if (number) {
866 list_for_each_entry(c, &cdev->configs, list) {
867 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300868 /*
869 * We disable the FDs of the previous
870 * configuration only if the new configuration
871 * is a valid one
872 */
873 if (cdev->config)
874 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700875 result = 0;
876 break;
877 }
878 }
879 if (result < 0)
880 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300881 } else { /* Zero configuration value - need to reset the config */
882 if (cdev->config)
883 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700884 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300885 }
David Brownell40982be2008-06-19 17:52:58 -0700886
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200887 INFO(cdev, "%s config #%d: %s\n",
888 usb_speed_string(gadget->speed),
889 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700890
891 if (!c)
892 goto done;
893
Peter Chen6027f312014-04-29 13:26:28 +0800894 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700895 cdev->config = c;
896
897 /* Initialize all interfaces by setting them to altsetting zero. */
898 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
899 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200900 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700901
902 if (!f)
903 break;
904
Laurent Pinchart52426582009-10-21 00:03:38 +0200905 /*
906 * Record which endpoints are used by the function. This is used
907 * to dispatch control requests targeted at that endpoint to the
908 * function's setup callback instead of the current
909 * configuration's setup callback.
910 */
John Younf3bdbe32016-02-05 17:07:03 -0800911 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200912
913 for (; *descriptors; ++descriptors) {
914 struct usb_endpoint_descriptor *ep;
915 int addr;
916
917 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
918 continue;
919
920 ep = (struct usb_endpoint_descriptor *)*descriptors;
921 addr = ((ep->bEndpointAddress & 0x80) >> 3)
922 | (ep->bEndpointAddress & 0x0f);
923 set_bit(addr, f->endpoints);
924 }
925
David Brownell40982be2008-06-19 17:52:58 -0700926 result = f->set_alt(f, tmp, 0);
927 if (result < 0) {
928 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
929 tmp, f->name, f, result);
930
931 reset_config(cdev);
932 goto done;
933 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300934
935 if (result == USB_GADGET_DELAYED_STATUS) {
936 DBG(cdev,
937 "%s: interface %d (%s) requested delayed status\n",
938 __func__, tmp, f->name);
939 cdev->delayed_status++;
940 DBG(cdev, "delayed_status count %d\n",
941 cdev->delayed_status);
942 }
David Brownell40982be2008-06-19 17:52:58 -0700943 }
944
945 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100946 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700947done:
948 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300949 if (result >= 0 && cdev->delayed_status)
950 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700951 return result;
952}
953
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100954int usb_add_config_only(struct usb_composite_dev *cdev,
955 struct usb_configuration *config)
956{
957 struct usb_configuration *c;
958
959 if (!config->bConfigurationValue)
960 return -EINVAL;
961
962 /* Prevent duplicate configuration identifiers */
963 list_for_each_entry(c, &cdev->configs, list) {
964 if (c->bConfigurationValue == config->bConfigurationValue)
965 return -EBUSY;
966 }
967
968 config->cdev = cdev;
969 list_add_tail(&config->list, &cdev->configs);
970
971 INIT_LIST_HEAD(&config->functions);
972 config->next_interface_id = 0;
973 memset(config->interface, 0, sizeof(config->interface));
974
975 return 0;
976}
977EXPORT_SYMBOL_GPL(usb_add_config_only);
978
David Brownell40982be2008-06-19 17:52:58 -0700979/**
980 * usb_add_config() - add a configuration to a device.
981 * @cdev: wraps the USB gadget
982 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200983 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700984 * Context: single threaded during gadget setup
985 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200986 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700987 * add each of the configurations it supports, using this routine.
988 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200989 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700990 * is zero for success else a negative errno value. Binding configurations
991 * assigns global resources including string IDs, and per-configuration
992 * resources such as interface IDs and endpoints.
993 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200994int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200995 struct usb_configuration *config,
996 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700997{
998 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100999
1000 if (!bind)
1001 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001002
1003 DBG(cdev, "adding config #%u '%s'/%p\n",
1004 config->bConfigurationValue,
1005 config->label, config);
1006
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001007 status = usb_add_config_only(cdev, config);
1008 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001009 goto done;
1010
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001011 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001012 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001013 while (!list_empty(&config->functions)) {
1014 struct usb_function *f;
1015
1016 f = list_first_entry(&config->functions,
1017 struct usb_function, list);
1018 list_del(&f->list);
1019 if (f->unbind) {
1020 DBG(cdev, "unbind function '%s'/%p\n",
1021 f->name, f);
1022 f->unbind(config, f);
1023 /* may free memory for "f" */
1024 }
1025 }
David Brownell40982be2008-06-19 17:52:58 -07001026 list_del(&config->list);
1027 config->cdev = NULL;
1028 } else {
1029 unsigned i;
1030
John Youncd69cbe2016-02-05 17:07:44 -08001031 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001032 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001033 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001034 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001035 config->highspeed ? " high" : "",
1036 config->fullspeed
1037 ? (gadget_is_dualspeed(cdev->gadget)
1038 ? " full"
1039 : " full/low")
1040 : "");
1041
1042 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1043 struct usb_function *f = config->interface[i];
1044
1045 if (!f)
1046 continue;
1047 DBG(cdev, " interface %d = %s/%p\n",
1048 i, f->name, f);
1049 }
1050 }
1051
Robert Baldygaf871cb92015-09-16 12:10:39 +02001052 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001053 usb_ep_autoconfig_reset(cdev->gadget);
1054
1055done:
1056 if (status)
1057 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1058 config->bConfigurationValue, status);
1059 return status;
1060}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001061EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001062
Benoit Goby51cce6f2012-05-10 10:07:57 +02001063static void remove_config(struct usb_composite_dev *cdev,
1064 struct usb_configuration *config)
1065{
1066 while (!list_empty(&config->functions)) {
1067 struct usb_function *f;
1068
1069 f = list_first_entry(&config->functions,
1070 struct usb_function, list);
Felipe Balbi0e3e9752017-06-06 14:47:29 +03001071
1072 usb_remove_function(config, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001073 }
1074 list_del(&config->list);
1075 if (config->unbind) {
1076 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
1077 config->unbind(config);
1078 /* may free memory for "c" */
1079 }
1080}
1081
1082/**
1083 * usb_remove_config() - remove a configuration from a device.
1084 * @cdev: wraps the USB gadget
1085 * @config: the configuration
1086 *
1087 * Drivers must call usb_gadget_disconnect before calling this function
1088 * to disconnect the device from the host and make sure the host will not
1089 * try to enumerate the device while we are changing the config list.
1090 */
1091void usb_remove_config(struct usb_composite_dev *cdev,
1092 struct usb_configuration *config)
1093{
1094 unsigned long flags;
1095
1096 spin_lock_irqsave(&cdev->lock, flags);
1097
1098 if (cdev->config == config)
1099 reset_config(cdev);
1100
1101 spin_unlock_irqrestore(&cdev->lock, flags);
1102
1103 remove_config(cdev, config);
1104}
1105
David Brownell40982be2008-06-19 17:52:58 -07001106/*-------------------------------------------------------------------------*/
1107
1108/* We support strings in multiple languages ... string descriptor zero
1109 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001110 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001111 * the host side.
1112 */
1113
1114static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1115{
1116 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001117 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001118 __le16 *tmp;
1119
1120 while (*sp) {
1121 s = *sp;
1122 language = cpu_to_le16(s->language);
1123 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1124 if (*tmp == language)
1125 goto repeat;
1126 }
1127 *tmp++ = language;
1128repeat:
1129 sp++;
1130 }
1131}
1132
1133static int lookup_string(
1134 struct usb_gadget_strings **sp,
1135 void *buf,
1136 u16 language,
1137 int id
1138)
1139{
1140 struct usb_gadget_strings *s;
1141 int value;
1142
1143 while (*sp) {
1144 s = *sp++;
1145 if (s->language != language)
1146 continue;
1147 value = usb_gadget_get_string(s, id, buf);
1148 if (value > 0)
1149 return value;
1150 }
1151 return -EINVAL;
1152}
1153
1154static int get_string(struct usb_composite_dev *cdev,
1155 void *buf, u16 language, int id)
1156{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001157 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001158 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001159 struct usb_configuration *c;
1160 struct usb_function *f;
1161 int len;
1162
Diego Violaad4676a2015-05-31 15:52:41 -03001163 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001164 * folk will ever care about ... also, it's all supported here.
1165 * (Except for UTF8 support for Unicode's "Astral Planes".)
1166 */
1167
1168 /* 0 == report all available language codes */
1169 if (id == 0) {
1170 struct usb_string_descriptor *s = buf;
1171 struct usb_gadget_strings **sp;
1172
1173 memset(s, 0, 256);
1174 s->bDescriptorType = USB_DT_STRING;
1175
1176 sp = composite->strings;
1177 if (sp)
1178 collect_langs(sp, s->wData);
1179
1180 list_for_each_entry(c, &cdev->configs, list) {
1181 sp = c->strings;
1182 if (sp)
1183 collect_langs(sp, s->wData);
1184
1185 list_for_each_entry(f, &c->functions, list) {
1186 sp = f->strings;
1187 if (sp)
1188 collect_langs(sp, s->wData);
1189 }
1190 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001191 list_for_each_entry(uc, &cdev->gstrings, list) {
1192 struct usb_gadget_strings **sp;
1193
1194 sp = get_containers_gs(uc);
1195 collect_langs(sp, s->wData);
1196 }
David Brownell40982be2008-06-19 17:52:58 -07001197
Roel Kluin417b57b2009-08-06 16:09:51 -07001198 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001199 continue;
1200 if (!len)
1201 return -EINVAL;
1202
1203 s->bLength = 2 * (len + 1);
1204 return s->bLength;
1205 }
1206
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001207 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1208 struct usb_os_string *b = buf;
1209 b->bLength = sizeof(*b);
1210 b->bDescriptorType = USB_DT_STRING;
1211 compiletime_assert(
1212 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1213 "qwSignature size must be equal to qw_sign");
1214 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1215 b->bMS_VendorCode = cdev->b_vendor_code;
1216 b->bPad = 0;
1217 return sizeof(*b);
1218 }
1219
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001220 list_for_each_entry(uc, &cdev->gstrings, list) {
1221 struct usb_gadget_strings **sp;
1222
1223 sp = get_containers_gs(uc);
1224 len = lookup_string(sp, buf, language, id);
1225 if (len > 0)
1226 return len;
1227 }
1228
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001229 /* String IDs are device-scoped, so we look up each string
1230 * table we're told about. These lookups are infrequent;
1231 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001232 */
1233 if (composite->strings) {
1234 len = lookup_string(composite->strings, buf, language, id);
1235 if (len > 0)
1236 return len;
1237 }
1238 list_for_each_entry(c, &cdev->configs, list) {
1239 if (c->strings) {
1240 len = lookup_string(c->strings, buf, language, id);
1241 if (len > 0)
1242 return len;
1243 }
1244 list_for_each_entry(f, &c->functions, list) {
1245 if (!f->strings)
1246 continue;
1247 len = lookup_string(f->strings, buf, language, id);
1248 if (len > 0)
1249 return len;
1250 }
1251 }
1252 return -EINVAL;
1253}
1254
1255/**
1256 * usb_string_id() - allocate an unused string ID
1257 * @cdev: the device whose string descriptor IDs are being allocated
1258 * Context: single threaded during gadget setup
1259 *
1260 * @usb_string_id() is called from bind() callbacks to allocate
1261 * string IDs. Drivers for functions, configurations, or gadgets will
1262 * then store that ID in the appropriate descriptors and string table.
1263 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001264 * All string identifier should be allocated using this,
1265 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1266 * that for example different functions don't wrongly assign different
1267 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001268 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001269int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001270{
1271 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001272 /* string id 0 is reserved by USB spec for list of
1273 * supported languages */
1274 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001275 cdev->next_string_id++;
1276 return cdev->next_string_id;
1277 }
1278 return -ENODEV;
1279}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001280EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001281
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001282/**
1283 * usb_string_ids() - allocate unused string IDs in batch
1284 * @cdev: the device whose string descriptor IDs are being allocated
1285 * @str: an array of usb_string objects to assign numbers to
1286 * Context: single threaded during gadget setup
1287 *
1288 * @usb_string_ids() is called from bind() callbacks to allocate
1289 * string IDs. Drivers for functions, configurations, or gadgets will
1290 * then copy IDs from the string table to the appropriate descriptors
1291 * and string table for other languages.
1292 *
1293 * All string identifier should be allocated using this,
1294 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1295 * example different functions don't wrongly assign different meanings
1296 * to the same identifier.
1297 */
1298int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1299{
1300 int next = cdev->next_string_id;
1301
1302 for (; str->s; ++str) {
1303 if (unlikely(next >= 254))
1304 return -ENODEV;
1305 str->id = ++next;
1306 }
1307
1308 cdev->next_string_id = next;
1309
1310 return 0;
1311}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001312EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001313
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001314static struct usb_gadget_string_container *copy_gadget_strings(
1315 struct usb_gadget_strings **sp, unsigned n_gstrings,
1316 unsigned n_strings)
1317{
1318 struct usb_gadget_string_container *uc;
1319 struct usb_gadget_strings **gs_array;
1320 struct usb_gadget_strings *gs;
1321 struct usb_string *s;
1322 unsigned mem;
1323 unsigned n_gs;
1324 unsigned n_s;
1325 void *stash;
1326
1327 mem = sizeof(*uc);
1328 mem += sizeof(void *) * (n_gstrings + 1);
1329 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1330 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1331 uc = kmalloc(mem, GFP_KERNEL);
1332 if (!uc)
1333 return ERR_PTR(-ENOMEM);
1334 gs_array = get_containers_gs(uc);
1335 stash = uc->stash;
1336 stash += sizeof(void *) * (n_gstrings + 1);
1337 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1338 struct usb_string *org_s;
1339
1340 gs_array[n_gs] = stash;
1341 gs = gs_array[n_gs];
1342 stash += sizeof(struct usb_gadget_strings);
1343 gs->language = sp[n_gs]->language;
1344 gs->strings = stash;
1345 org_s = sp[n_gs]->strings;
1346
1347 for (n_s = 0; n_s < n_strings; n_s++) {
1348 s = stash;
1349 stash += sizeof(struct usb_string);
1350 if (org_s->s)
1351 s->s = org_s->s;
1352 else
1353 s->s = "";
1354 org_s++;
1355 }
1356 s = stash;
1357 s->s = NULL;
1358 stash += sizeof(struct usb_string);
1359
1360 }
1361 gs_array[n_gs] = NULL;
1362 return uc;
1363}
1364
1365/**
1366 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1367 * @cdev: the device whose string descriptor IDs are being allocated
1368 * and attached.
1369 * @sp: an array of usb_gadget_strings to attach.
1370 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1371 *
1372 * This function will create a deep copy of usb_gadget_strings and usb_string
1373 * and attach it to the cdev. The actual string (usb_string.s) will not be
1374 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001375 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001376 * The ->language pointer of each struct usb_gadget_strings has to contain the
1377 * same amount of entries.
1378 * 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 +09001379 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001380 * entry of en-US. Therefore both entries become the same id assign.
1381 */
1382struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1383 struct usb_gadget_strings **sp, unsigned n_strings)
1384{
1385 struct usb_gadget_string_container *uc;
1386 struct usb_gadget_strings **n_gs;
1387 unsigned n_gstrings = 0;
1388 unsigned i;
1389 int ret;
1390
1391 for (i = 0; sp[i]; i++)
1392 n_gstrings++;
1393
1394 if (!n_gstrings)
1395 return ERR_PTR(-EINVAL);
1396
1397 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1398 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001399 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001400
1401 n_gs = get_containers_gs(uc);
1402 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1403 if (ret)
1404 goto err;
1405
1406 for (i = 1; i < n_gstrings; i++) {
1407 struct usb_string *m_s;
1408 struct usb_string *s;
1409 unsigned n;
1410
1411 m_s = n_gs[0]->strings;
1412 s = n_gs[i]->strings;
1413 for (n = 0; n < n_strings; n++) {
1414 s->id = m_s->id;
1415 s++;
1416 m_s++;
1417 }
1418 }
1419 list_add_tail(&uc->list, &cdev->gstrings);
1420 return n_gs[0]->strings;
1421err:
1422 kfree(uc);
1423 return ERR_PTR(ret);
1424}
1425EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1426
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001427/**
1428 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001429 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001430 * @n: number of string IDs to allocate
1431 * Context: single threaded during gadget setup
1432 *
1433 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001434 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001435 * is, returns last requested ID which is now very useful information.
1436 *
1437 * @usb_string_ids_n() is called from bind() callbacks to allocate
1438 * string IDs. Drivers for functions, configurations, or gadgets will
1439 * then store that ID in the appropriate descriptors and string table.
1440 *
1441 * All string identifier should be allocated using this,
1442 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1443 * example different functions don't wrongly assign different meanings
1444 * to the same identifier.
1445 */
1446int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1447{
1448 unsigned next = c->next_string_id;
1449 if (unlikely(n > 254 || (unsigned)next + n > 254))
1450 return -ENODEV;
1451 c->next_string_id += n;
1452 return next + 1;
1453}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001454EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001455
David Brownell40982be2008-06-19 17:52:58 -07001456/*-------------------------------------------------------------------------*/
1457
1458static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1459{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001460 struct usb_composite_dev *cdev;
1461
David Brownell40982be2008-06-19 17:52:58 -07001462 if (req->status || req->actual != req->length)
1463 DBG((struct usb_composite_dev *) ep->driver_data,
1464 "setup complete --> %d, %d/%d\n",
1465 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001466
1467 /*
1468 * REVIST The same ep0 requests are shared with function drivers
1469 * so they don't have to maintain the same ->complete() stubs.
1470 *
1471 * Because of that, we need to check for the validity of ->context
1472 * here, even though we know we've set it to something useful.
1473 */
1474 if (!req->context)
1475 return;
1476
1477 cdev = req->context;
1478
1479 if (cdev->req == req)
1480 cdev->setup_pending = false;
1481 else if (cdev->os_desc_req == req)
1482 cdev->os_desc_pending = false;
1483 else
1484 WARN(1, "unknown request %p\n", req);
1485}
1486
1487static int composite_ep0_queue(struct usb_composite_dev *cdev,
1488 struct usb_request *req, gfp_t gfp_flags)
1489{
1490 int ret;
1491
1492 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1493 if (ret == 0) {
1494 if (cdev->req == req)
1495 cdev->setup_pending = true;
1496 else if (cdev->os_desc_req == req)
1497 cdev->os_desc_pending = true;
1498 else
1499 WARN(1, "unknown request %p\n", req);
1500 }
1501
1502 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001503}
1504
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001505static int count_ext_compat(struct usb_configuration *c)
1506{
1507 int i, res;
1508
1509 res = 0;
1510 for (i = 0; i < c->next_interface_id; ++i) {
1511 struct usb_function *f;
1512 int j;
1513
1514 f = c->interface[i];
1515 for (j = 0; j < f->os_desc_n; ++j) {
1516 struct usb_os_desc *d;
1517
1518 if (i != f->os_desc_table[j].if_id)
1519 continue;
1520 d = f->os_desc_table[j].os_desc;
1521 if (d && d->ext_compat_id)
1522 ++res;
1523 }
1524 }
1525 BUG_ON(res > 255);
1526 return res;
1527}
1528
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001529static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001530{
1531 int i, count;
1532
1533 count = 16;
Chris Dickens636ba132017-12-31 18:59:43 -08001534 buf += 16;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001535 for (i = 0; i < c->next_interface_id; ++i) {
1536 struct usb_function *f;
1537 int j;
1538
1539 f = c->interface[i];
1540 for (j = 0; j < f->os_desc_n; ++j) {
1541 struct usb_os_desc *d;
1542
1543 if (i != f->os_desc_table[j].if_id)
1544 continue;
1545 d = f->os_desc_table[j].os_desc;
1546 if (d && d->ext_compat_id) {
1547 *buf++ = i;
1548 *buf++ = 0x01;
1549 memcpy(buf, d->ext_compat_id, 16);
1550 buf += 22;
1551 } else {
1552 ++buf;
1553 *buf = 0x01;
1554 buf += 23;
1555 }
1556 count += 24;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001557 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1558 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001559 }
1560 }
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001561
1562 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001563}
1564
1565static int count_ext_prop(struct usb_configuration *c, int interface)
1566{
1567 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001568 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001569
1570 f = c->interface[interface];
1571 for (j = 0; j < f->os_desc_n; ++j) {
1572 struct usb_os_desc *d;
1573
1574 if (interface != f->os_desc_table[j].if_id)
1575 continue;
1576 d = f->os_desc_table[j].os_desc;
1577 if (d && d->ext_compat_id)
1578 return d->ext_prop_count;
1579 }
Julia Lawall849b1332014-05-19 06:31:07 +02001580 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001581}
1582
1583static int len_ext_prop(struct usb_configuration *c, int interface)
1584{
1585 struct usb_function *f;
1586 struct usb_os_desc *d;
1587 int j, res;
1588
1589 res = 10; /* header length */
1590 f = c->interface[interface];
1591 for (j = 0; j < f->os_desc_n; ++j) {
1592 if (interface != f->os_desc_table[j].if_id)
1593 continue;
1594 d = f->os_desc_table[j].os_desc;
1595 if (d)
1596 return min(res + d->ext_prop_len, 4096);
1597 }
1598 return res;
1599}
1600
1601static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1602{
1603 struct usb_function *f;
1604 struct usb_os_desc *d;
1605 struct usb_os_desc_ext_prop *ext_prop;
1606 int j, count, n, ret;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001607
1608 f = c->interface[interface];
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001609 count = 10; /* header length */
Chris Dickens636ba132017-12-31 18:59:43 -08001610 buf += 10;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001611 for (j = 0; j < f->os_desc_n; ++j) {
1612 if (interface != f->os_desc_table[j].if_id)
1613 continue;
1614 d = f->os_desc_table[j].os_desc;
1615 if (d)
1616 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001617 n = ext_prop->data_len +
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001618 ext_prop->name_len + 14;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001619 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1620 return count;
1621 usb_ext_prop_put_size(buf, n);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001622 usb_ext_prop_put_type(buf, ext_prop->type);
1623 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1624 ext_prop->name_len);
1625 if (ret < 0)
1626 return ret;
1627 switch (ext_prop->type) {
1628 case USB_EXT_PROP_UNICODE:
1629 case USB_EXT_PROP_UNICODE_ENV:
1630 case USB_EXT_PROP_UNICODE_LINK:
1631 usb_ext_prop_put_unicode(buf, ret,
1632 ext_prop->data,
1633 ext_prop->data_len);
1634 break;
1635 case USB_EXT_PROP_BINARY:
1636 usb_ext_prop_put_binary(buf, ret,
1637 ext_prop->data,
1638 ext_prop->data_len);
1639 break;
1640 case USB_EXT_PROP_LE32:
1641 /* not implemented */
1642 case USB_EXT_PROP_BE32:
1643 /* not implemented */
1644 default:
1645 return -EINVAL;
1646 }
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001647 buf += n;
1648 count += n;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001649 }
1650 }
1651
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001652 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001653}
1654
David Brownell40982be2008-06-19 17:52:58 -07001655/*
1656 * The setup() callback implements all the ep0 functionality that's
1657 * not handled lower down, in hardware or the hardware driver(like
1658 * device and endpoint feature flags, and their status). It's all
1659 * housekeeping for the gadget function we're implementing. Most of
1660 * the work is in config and function specific setup.
1661 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001662int
David Brownell40982be2008-06-19 17:52:58 -07001663composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1664{
1665 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1666 struct usb_request *req = cdev->req;
1667 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001668 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001669 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001670 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001671 u16 w_value = le16_to_cpu(ctrl->wValue);
1672 u16 w_length = le16_to_cpu(ctrl->wLength);
1673 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001674 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001675
1676 /* partial re-init of the response message; the function or the
1677 * gadget might need to intercept e.g. a control-OUT completion
1678 * when we delegate to it.
1679 */
1680 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001681 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001682 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301683 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001684 gadget->ep0->driver_data = cdev;
1685
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001686 /*
1687 * Don't let non-standard requests match any of the cases below
1688 * by accident.
1689 */
1690 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1691 goto unknown;
1692
David Brownell40982be2008-06-19 17:52:58 -07001693 switch (ctrl->bRequest) {
1694
1695 /* we handle all standard USB descriptors */
1696 case USB_REQ_GET_DESCRIPTOR:
1697 if (ctrl->bRequestType != USB_DIR_IN)
1698 goto unknown;
1699 switch (w_value >> 8) {
1700
1701 case USB_DT_DEVICE:
1702 cdev->desc.bNumConfigurations =
1703 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001704 cdev->desc.bMaxPacketSize0 =
1705 cdev->gadget->ep0->maxpacket;
1706 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001707 if (gadget->speed >= USB_SPEED_SUPER) {
Chunfeng Yun1ef6c422018-05-09 19:29:16 +08001708 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001709 cdev->desc.bMaxPacketSize0 = 9;
1710 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001711 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001712 }
Igor Kotrasinski5527e732015-08-20 10:00:01 +02001713 } else {
John Youna9548c52017-04-28 12:55:20 +04001714 if (gadget->lpm_capable)
1715 cdev->desc.bcdUSB = cpu_to_le16(0x0201);
1716 else
1717 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001718 }
1719
David Brownell40982be2008-06-19 17:52:58 -07001720 value = min(w_length, (u16) sizeof cdev->desc);
1721 memcpy(req->buf, &cdev->desc, value);
1722 break;
1723 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001724 if (!gadget_is_dualspeed(gadget) ||
1725 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001726 break;
1727 device_qual(cdev);
1728 value = min_t(int, w_length,
1729 sizeof(struct usb_qualifier_descriptor));
1730 break;
1731 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001732 if (!gadget_is_dualspeed(gadget) ||
1733 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001734 break;
1735 /* FALLTHROUGH */
1736 case USB_DT_CONFIG:
1737 value = config_desc(cdev, w_value);
1738 if (value >= 0)
1739 value = min(w_length, (u16) value);
1740 break;
1741 case USB_DT_STRING:
1742 value = get_string(cdev, req->buf,
1743 w_index, w_value & 0xff);
1744 if (value >= 0)
1745 value = min(w_length, (u16) value);
1746 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001747 case USB_DT_BOS:
John Youna9548c52017-04-28 12:55:20 +04001748 if (gadget_is_superspeed(gadget) ||
1749 gadget->lpm_capable) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001750 value = bos_desc(cdev);
1751 value = min(w_length, (u16) value);
1752 }
1753 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001754 case USB_DT_OTG:
1755 if (gadget_is_otg(gadget)) {
1756 struct usb_configuration *config;
1757 int otg_desc_len = 0;
1758
1759 if (cdev->config)
1760 config = cdev->config;
1761 else
1762 config = list_first_entry(
1763 &cdev->configs,
1764 struct usb_configuration, list);
1765 if (!config)
1766 goto done;
1767
1768 if (gadget->otg_caps &&
1769 (gadget->otg_caps->otg_rev >= 0x0200))
1770 otg_desc_len += sizeof(
1771 struct usb_otg20_descriptor);
1772 else
1773 otg_desc_len += sizeof(
1774 struct usb_otg_descriptor);
1775
1776 value = min_t(int, w_length, otg_desc_len);
1777 memcpy(req->buf, config->descriptors[0], value);
1778 }
1779 break;
David Brownell40982be2008-06-19 17:52:58 -07001780 }
1781 break;
1782
1783 /* any number of configs can work */
1784 case USB_REQ_SET_CONFIGURATION:
1785 if (ctrl->bRequestType != 0)
1786 goto unknown;
1787 if (gadget_is_otg(gadget)) {
1788 if (gadget->a_hnp_support)
1789 DBG(cdev, "HNP available\n");
1790 else if (gadget->a_alt_hnp_support)
1791 DBG(cdev, "HNP on another port\n");
1792 else
1793 VDBG(cdev, "HNP inactive\n");
1794 }
1795 spin_lock(&cdev->lock);
1796 value = set_config(cdev, ctrl, w_value);
1797 spin_unlock(&cdev->lock);
1798 break;
1799 case USB_REQ_GET_CONFIGURATION:
1800 if (ctrl->bRequestType != USB_DIR_IN)
1801 goto unknown;
1802 if (cdev->config)
1803 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1804 else
1805 *(u8 *)req->buf = 0;
1806 value = min(w_length, (u16) 1);
1807 break;
1808
Krzysztof Opasiak7e4da3f2016-12-20 19:52:16 +01001809 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001810 case USB_REQ_SET_INTERFACE:
1811 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1812 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001813 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001814 break;
Bryan Wu08889512009-01-08 00:21:19 +08001815 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001816 if (!f)
1817 break;
Krzysztof Opasiak7e4da3f2016-12-20 19:52:16 +01001818
1819 /*
1820 * If there's no get_alt() method, we know only altsetting zero
1821 * works. There is no need to check if set_alt() is not NULL
1822 * as we check this in usb_add_function().
1823 */
1824 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001825 break;
Chunfeng Yun980900d2018-05-25 17:24:57 +08001826
1827 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001828 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001829 if (value == USB_GADGET_DELAYED_STATUS) {
1830 DBG(cdev,
1831 "%s: interface %d (%s) requested delayed status\n",
1832 __func__, intf, f->name);
1833 cdev->delayed_status++;
1834 DBG(cdev, "delayed_status count %d\n",
1835 cdev->delayed_status);
1836 }
Chunfeng Yun980900d2018-05-25 17:24:57 +08001837 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001838 break;
1839 case USB_REQ_GET_INTERFACE:
1840 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1841 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001842 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001843 break;
Bryan Wu08889512009-01-08 00:21:19 +08001844 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001845 if (!f)
1846 break;
1847 /* lots of interfaces only need altsetting zero... */
1848 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1849 if (value < 0)
1850 break;
1851 *((u8 *)req->buf) = value;
1852 value = min(w_length, (u16) 1);
1853 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001854 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001855 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1856 (w_index == OTG_STS_SELECTOR)) {
1857 if (ctrl->bRequestType != (USB_DIR_IN |
1858 USB_RECIP_DEVICE))
1859 goto unknown;
1860 *((u8 *)req->buf) = gadget->host_request_flag;
1861 value = 1;
1862 break;
1863 }
1864
1865 /*
1866 * USB 3.0 additions:
1867 * Function driver should handle get_status request. If such cb
1868 * wasn't supplied we respond with default value = 0
1869 * Note: function driver should supply such cb only for the
1870 * first interface of the function
1871 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001872 if (!gadget_is_superspeed(gadget))
1873 goto unknown;
1874 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1875 goto unknown;
1876 value = 2; /* This is the length of the get_status reply */
1877 put_unaligned_le16(0, req->buf);
1878 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1879 break;
1880 f = cdev->config->interface[intf];
1881 if (!f)
1882 break;
1883 status = f->get_status ? f->get_status(f) : 0;
1884 if (status < 0)
1885 break;
1886 put_unaligned_le16(status & 0x0000ffff, req->buf);
1887 break;
1888 /*
1889 * Function drivers should handle SetFeature/ClearFeature
1890 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1891 * only for the first interface of the function
1892 */
1893 case USB_REQ_CLEAR_FEATURE:
1894 case USB_REQ_SET_FEATURE:
1895 if (!gadget_is_superspeed(gadget))
1896 goto unknown;
1897 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1898 goto unknown;
1899 switch (w_value) {
1900 case USB_INTRF_FUNC_SUSPEND:
1901 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1902 break;
1903 f = cdev->config->interface[intf];
1904 if (!f)
1905 break;
1906 value = 0;
Danny Segald6c40da2016-12-06 15:35:24 -08001907 if (f->func_suspend) {
1908 const u8 suspend_opt = w_index >> 8;
1909
1910 value = f->func_suspend(f, suspend_opt);
1911 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1912 f->name ? f->name : "", suspend_opt);
1913 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001914 if (value < 0) {
1915 ERROR(cdev,
1916 "func_suspend() returned error %d\n",
1917 value);
1918 value = 0;
1919 }
1920 break;
1921 }
1922 break;
David Brownell40982be2008-06-19 17:52:58 -07001923 default:
1924unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001925 /*
1926 * OS descriptors handling
1927 */
1928 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001929 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001930 ctrl->bRequest == cdev->b_vendor_code) {
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001931 struct usb_configuration *os_desc_cfg;
1932 u8 *buf;
1933 int interface;
1934 int count = 0;
1935
1936 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001937 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001938 req->complete = composite_setup_complete;
1939 buf = req->buf;
1940 os_desc_cfg = cdev->os_desc_config;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001941 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001942 memset(buf, 0, w_length);
1943 buf[5] = 0x01;
1944 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1945 case USB_RECIP_DEVICE:
1946 if (w_index != 0x4 || (w_value >> 8))
1947 break;
1948 buf[6] = w_index;
Chris Dickens636ba132017-12-31 18:59:43 -08001949 /* Number of ext compat interfaces */
1950 count = count_ext_compat(os_desc_cfg);
1951 buf[8] = count;
1952 count *= 24; /* 24 B/ext compat desc */
1953 count += 16; /* header */
1954 put_unaligned_le32(count, buf);
1955 value = w_length;
1956 if (w_length > 0x10) {
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001957 value = fill_ext_compat(os_desc_cfg, buf);
1958 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001959 }
1960 break;
1961 case USB_RECIP_INTERFACE:
1962 if (w_index != 0x5 || (w_value >> 8))
1963 break;
1964 interface = w_value & 0xFF;
1965 buf[6] = w_index;
Chris Dickens636ba132017-12-31 18:59:43 -08001966 count = count_ext_prop(os_desc_cfg,
1967 interface);
1968 put_unaligned_le16(count, buf + 8);
1969 count = len_ext_prop(os_desc_cfg,
1970 interface);
1971 put_unaligned_le32(count, buf);
1972 value = w_length;
1973 if (w_length > 0x0A) {
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001974 value = fill_ext_prop(os_desc_cfg,
1975 interface, buf);
Chris Dickens636ba132017-12-31 18:59:43 -08001976 if (value >= 0)
1977 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001978 }
1979 break;
1980 }
William Wu7e14f47a2016-05-13 18:30:42 +08001981
Chris Dickens636ba132017-12-31 18:59:43 -08001982 goto check_value;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001983 }
1984
David Brownell40982be2008-06-19 17:52:58 -07001985 VDBG(cdev,
1986 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1987 ctrl->bRequestType, ctrl->bRequest,
1988 w_value, w_index, w_length);
1989
Laurent Pinchart52426582009-10-21 00:03:38 +02001990 /* functions always handle their interfaces and endpoints...
1991 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001992 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07001993 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301994 if (cdev->config) {
1995 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b4572016-06-22 01:12:08 +02001996 if (f->req_match &&
1997 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301998 goto try_fun_setup;
Felix Hädicke1a00b4572016-06-22 01:12:08 +02001999 } else {
2000 struct usb_configuration *c;
2001 list_for_each_entry(c, &cdev->configs, list)
2002 list_for_each_entry(f, &c->functions, list)
2003 if (f->req_match &&
2004 f->req_match(f, ctrl, true))
2005 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302006 }
Felix Hädicke1a00b4572016-06-22 01:12:08 +02002007 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302008
Laurent Pinchart52426582009-10-21 00:03:38 +02002009 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2010 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002011 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302012 break;
2013 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002014 break;
2015
2016 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002017 if (!cdev->config)
2018 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002019 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2020 list_for_each_entry(f, &cdev->config->functions, list) {
2021 if (test_bit(endp, f->endpoints))
2022 break;
2023 }
2024 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002025 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002026 break;
David Brownell40982be2008-06-19 17:52:58 -07002027 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002028try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002029 if (f && f->setup)
2030 value = f->setup(f, ctrl);
2031 else {
David Brownell40982be2008-06-19 17:52:58 -07002032 struct usb_configuration *c;
2033
2034 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002035 if (!c)
2036 goto done;
2037
2038 /* try current config's setup */
2039 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002040 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002041 goto done;
2042 }
2043
2044 /* try the only function in the current config */
2045 if (!list_is_singular(&c->functions))
2046 goto done;
2047 f = list_first_entry(&c->functions, struct usb_function,
2048 list);
2049 if (f->setup)
2050 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002051 }
2052
2053 goto done;
2054 }
2055
Chris Dickens636ba132017-12-31 18:59:43 -08002056check_value:
David Brownell40982be2008-06-19 17:52:58 -07002057 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002058 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002059 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002060 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002061 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002062 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002063 if (value < 0) {
2064 DBG(cdev, "ep_queue --> %d\n", value);
2065 req->status = 0;
2066 composite_setup_complete(gadget->ep0, req);
2067 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002068 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2069 WARN(cdev,
2070 "%s: Delayed status not supported for w_length != 0",
2071 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002072 }
2073
2074done:
2075 /* device either stalls (value < 0) or reports success */
2076 return value;
2077}
2078
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002079void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002080{
2081 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2082 unsigned long flags;
2083
2084 /* REVISIT: should we have config and device level
2085 * disconnect callbacks?
2086 */
2087 spin_lock_irqsave(&cdev->lock, flags);
2088 if (cdev->config)
2089 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002090 if (cdev->driver->disconnect)
2091 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002092 spin_unlock_irqrestore(&cdev->lock, flags);
2093}
2094
2095/*-------------------------------------------------------------------------*/
2096
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002097static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2098 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002099{
2100 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2101 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2102
2103 return sprintf(buf, "%d\n", cdev->suspended);
2104}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002105static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002106
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002107static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002108{
2109 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasovaec17e12017-09-30 08:55:55 -07002110 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2111 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002112
2113 /* composite_disconnect() must already have been called
2114 * by the underlying peripheral controller driver!
2115 * so there's no i/o concurrency that could affect the
2116 * state protected by cdev->lock.
2117 */
2118 WARN_ON(cdev->config);
2119
2120 while (!list_empty(&cdev->configs)) {
2121 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002122 c = list_first_entry(&cdev->configs,
2123 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002124 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002125 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002126 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002127 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002128
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002129 composite_dev_cleanup(cdev);
2130
Andrew Gabbasovaec17e12017-09-30 08:55:55 -07002131 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2132 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2133
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002134 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002135 kfree(cdev);
2136 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002137}
2138
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002139static void composite_unbind(struct usb_gadget *gadget)
2140{
2141 __composite_unbind(gadget, true);
2142}
2143
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002144static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2145 const struct usb_device_descriptor *old)
2146{
2147 __le16 idVendor;
2148 __le16 idProduct;
2149 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002150 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002151 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002152 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002153
2154 /*
2155 * these variables may have been set in
2156 * usb_composite_overwrite_options()
2157 */
2158 idVendor = new->idVendor;
2159 idProduct = new->idProduct;
2160 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002161 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002162 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002163 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002164
2165 *new = *old;
2166 if (idVendor)
2167 new->idVendor = idVendor;
2168 if (idProduct)
2169 new->idProduct = idProduct;
2170 if (bcdDevice)
2171 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002172 else
2173 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002174 if (iSerialNumber)
2175 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002176 if (iManufacturer)
2177 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002178 if (iProduct)
2179 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002180}
2181
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002182int composite_dev_prepare(struct usb_composite_driver *composite,
2183 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002184{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002185 struct usb_gadget *gadget = cdev->gadget;
2186 int ret = -ENOMEM;
2187
2188 /* preallocate control response and buffer */
2189 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2190 if (!cdev->req)
2191 return -ENOMEM;
2192
2193 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2194 if (!cdev->req->buf)
2195 goto fail;
2196
2197 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2198 if (ret)
2199 goto fail_dev;
2200
2201 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002202 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002203 gadget->ep0->driver_data = cdev;
2204
2205 cdev->driver = composite;
2206
2207 /*
2208 * As per USB compliance update, a device that is actively drawing
2209 * more than 100mA from USB must report itself as bus-powered in
2210 * the GetStatus(DEVICE) call.
2211 */
2212 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2213 usb_gadget_set_selfpowered(gadget);
2214
2215 /* interface and string IDs start at zero via kzalloc.
2216 * we force endpoints to start unassigned; few controller
2217 * drivers will zero ep->driver_data.
2218 */
2219 usb_ep_autoconfig_reset(gadget);
2220 return 0;
2221fail_dev:
2222 kfree(cdev->req->buf);
2223fail:
2224 usb_ep_free_request(gadget->ep0, cdev->req);
2225 cdev->req = NULL;
2226 return ret;
2227}
2228
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002229int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2230 struct usb_ep *ep0)
2231{
2232 int ret = 0;
2233
2234 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2235 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002236 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002237 goto end;
2238 }
2239
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08002240 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2241 GFP_KERNEL);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002242 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002243 ret = -ENOMEM;
Christophe JAILLET990758c2017-01-04 06:30:16 +01002244 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002245 goto end;
2246 }
Felipe Balbi57943712014-09-18 09:54:54 -05002247 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002248 cdev->os_desc_req->complete = composite_setup_complete;
2249end:
2250 return ret;
2251}
2252
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002253void composite_dev_cleanup(struct usb_composite_dev *cdev)
2254{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002255 struct usb_gadget_string_container *uc, *tmp;
Benjamin Herrenschmidtaaeab022018-03-23 13:44:06 +11002256 struct usb_ep *ep, *tmp_ep;
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002257
2258 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2259 list_del(&uc->list);
2260 kfree(uc);
2261 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002262 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002263 if (cdev->os_desc_pending)
2264 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2265
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002266 kfree(cdev->os_desc_req->buf);
2267 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2268 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002269 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002270 if (cdev->setup_pending)
2271 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2272
Li Junbe0a8882014-08-28 21:44:11 +08002273 kfree(cdev->req->buf);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002274 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2275 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002276 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002277 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Benjamin Herrenschmidtaaeab022018-03-23 13:44:06 +11002278
2279 /*
2280 * Some UDC backends have a dynamic EP allocation scheme.
2281 *
2282 * In that case, the dispose() callback is used to notify the
2283 * backend that the EPs are no longer in use.
2284 *
2285 * Note: The UDC backend can remove the EP from the ep_list as
2286 * a result, so we need to use the _safe list iterator.
2287 */
2288 list_for_each_entry_safe(ep, tmp_ep,
2289 &cdev->gadget->ep_list, ep_list) {
2290 if (ep->ops->dispose)
2291 ep->ops->dispose(ep);
2292 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002293}
2294
2295static int composite_bind(struct usb_gadget *gadget,
2296 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002297{
2298 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002299 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002300 int status = -ENOMEM;
2301
2302 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2303 if (!cdev)
2304 return status;
2305
2306 spin_lock_init(&cdev->lock);
2307 cdev->gadget = gadget;
2308 set_gadget_data(gadget, cdev);
2309 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002310 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002311
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002312 status = composite_dev_prepare(composite, cdev);
2313 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002314 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002315
2316 /* composite gadget needs to assign strings for whole device (like
2317 * serial number), register function drivers, potentially update
2318 * power state and consumption, etc
2319 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002320 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002321 if (status < 0)
2322 goto fail;
2323
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002324 if (cdev->use_os_string) {
2325 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2326 if (status)
2327 goto fail;
2328 }
2329
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002330 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002331
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002332 /* has userspace failed to provide a serial number? */
2333 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2334 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2335
David Brownell40982be2008-06-19 17:52:58 -07002336 INFO(cdev, "%s ready\n", composite->name);
2337 return 0;
2338
2339fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002340 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002341 return status;
2342}
2343
2344/*-------------------------------------------------------------------------*/
2345
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002346void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002347{
2348 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2349 struct usb_function *f;
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002350 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002351
David Brownell89429392009-03-19 14:14:17 -07002352 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002353 * suspend/resume callbacks?
2354 */
2355 DBG(cdev, "suspend\n");
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002356 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002357 if (cdev->config) {
2358 list_for_each_entry(f, &cdev->config->functions, list) {
2359 if (f->suspend)
2360 f->suspend(f);
2361 }
2362 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002363 if (cdev->driver->suspend)
2364 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002365
2366 cdev->suspended = 1;
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002367 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002368
2369 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002370}
2371
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002372void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002373{
2374 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2375 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00002376 u16 maxpower;
Danny Segald6c40da2016-12-06 15:35:24 -08002377 int ret;
2378 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002379
David Brownell89429392009-03-19 14:14:17 -07002380 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002381 * suspend/resume callbacks?
2382 */
2383 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002384 if (cdev->driver->resume)
2385 cdev->driver->resume(cdev);
Danny Segald6c40da2016-12-06 15:35:24 -08002386
2387 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002388 if (cdev->config) {
2389 list_for_each_entry(f, &cdev->config->functions, list) {
Danny Segald6c40da2016-12-06 15:35:24 -08002390 ret = usb_func_wakeup_int(f);
2391 if (ret) {
2392 if (ret == -EAGAIN) {
2393 ERROR(f->config->cdev,
2394 "Function wakeup for %s could not complete due to suspend state.\n",
2395 f->name ? f->name : "");
2396 break;
2397 } else if (ret != -ENOTSUPP) {
2398 ERROR(f->config->cdev,
2399 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2400 f->name ? f->name : "",
2401 ret);
2402 }
2403 }
2404
David Brownell40982be2008-06-19 17:52:58 -07002405 if (f->resume)
2406 f->resume(f);
2407 }
Hao Wub23f2f92010-11-29 15:17:03 +08002408
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002409 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08002410
2411 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002412 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002413 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002414
Danny Segald6c40da2016-12-06 15:35:24 -08002415 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002416 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002417}
2418
2419/*-------------------------------------------------------------------------*/
2420
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002421static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002422 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002423 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002424
2425 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002426 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002427 .disconnect = composite_disconnect,
2428
2429 .suspend = composite_suspend,
2430 .resume = composite_resume,
2431
2432 .driver = {
2433 .owner = THIS_MODULE,
2434 },
2435};
2436
2437/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002438 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002439 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002440 *
David Brownell40982be2008-06-19 17:52:58 -07002441 * Context: single threaded during gadget setup
2442 *
2443 * This function is used to register drivers using the composite driver
2444 * framework. The return value is zero, or a negative errno value.
2445 * Those values normally come from the driver's @bind method, which does
2446 * all the work of setting up the driver to match the hardware.
2447 *
2448 * On successful return, the gadget is ready to respond to requests from
2449 * the host, unless one of its components invokes usb_gadget_disconnect()
2450 * while it was binding. That would usually be done in order to wait for
2451 * some userspace participation.
2452 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002453int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002454{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002455 struct usb_gadget_driver *gadget_driver;
2456
2457 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002458 return -EINVAL;
2459
2460 if (!driver->name)
2461 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002462
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002463 driver->gadget_driver = composite_driver_template;
2464 gadget_driver = &driver->gadget_driver;
2465
2466 gadget_driver->function = (char *) driver->name;
2467 gadget_driver->driver.name = driver->name;
2468 gadget_driver->max_speed = driver->max_speed;
2469
2470 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002471}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002472EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002473
2474/**
2475 * usb_composite_unregister() - unregister a composite driver
2476 * @driver: the driver to unregister
2477 *
2478 * This function is used to unregister drivers using the composite
2479 * driver framework.
2480 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002481void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002482{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002483 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002484}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002485EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002486
2487/**
2488 * usb_composite_setup_continue() - Continue with the control transfer
2489 * @cdev: the composite device who's control transfer was kept waiting
2490 *
2491 * This function must be called by the USB function driver to continue
2492 * with the control transfer's data/status stage in case it had requested to
2493 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2494 * can request the composite framework to delay the setup request's data/status
2495 * stages by returning USB_GADGET_DELAYED_STATUS.
2496 */
2497void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2498{
2499 int value;
2500 struct usb_request *req = cdev->req;
2501 unsigned long flags;
2502
2503 DBG(cdev, "%s\n", __func__);
2504 spin_lock_irqsave(&cdev->lock, flags);
2505
2506 if (cdev->delayed_status == 0) {
2507 WARN(cdev, "%s: Unexpected call\n", __func__);
2508
2509 } else if (--cdev->delayed_status == 0) {
2510 DBG(cdev, "%s: Completing delayed status\n", __func__);
2511 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002512 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002513 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002514 if (value < 0) {
2515 DBG(cdev, "ep_queue --> %d\n", value);
2516 req->status = 0;
2517 composite_setup_complete(cdev->gadget->ep0, req);
2518 }
2519 }
2520
2521 spin_unlock_irqrestore(&cdev->lock, flags);
2522}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002523EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002524
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002525static char *composite_default_mfr(struct usb_gadget *gadget)
2526{
Juergen Gross5002c932016-10-10 12:48:36 +02002527 return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
2528 init_utsname()->release, gadget->name);
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002529}
2530
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002531void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2532 struct usb_composite_overwrite *covr)
2533{
2534 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002535 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2536 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002537
2538 if (covr->idVendor)
2539 desc->idVendor = cpu_to_le16(covr->idVendor);
2540
2541 if (covr->idProduct)
2542 desc->idProduct = cpu_to_le16(covr->idProduct);
2543
2544 if (covr->bcdDevice)
2545 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002546
2547 if (covr->serial_number) {
2548 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2549 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2550 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002551 if (covr->manufacturer) {
2552 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2553 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002554
2555 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2556 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2557 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2558 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002559 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002560
2561 if (covr->product) {
2562 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2563 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2564 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002565}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002566EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002567
2568MODULE_LICENSE("GPL");
2569MODULE_AUTHOR("David Brownell");