blob: 9086b6486830d1c2e4c4747d6bfdfe8b719ee825 [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{
Hemant Kumar08901c62018-09-18 12:01:17 -0700149 struct usb_composite_dev *cdev;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300150 struct usb_endpoint_descriptor *chosen_desc = NULL;
151 struct usb_descriptor_header **speed_desc = NULL;
152
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300153 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
154 int want_comp_desc = 0;
155
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300156 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
157
158 if (!g || !f || !_ep)
159 return -EIO;
160
Hemant Kumar08901c62018-09-18 12:01:17 -0700161 cdev = get_gadget_data(g);
162
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300163 /* select desired speed */
164 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800165 case USB_SPEED_SUPER_PLUS:
166 if (gadget_is_superspeed_plus(g)) {
167 speed_desc = f->ssp_descriptors;
168 want_comp_desc = 1;
169 break;
170 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500171 /* fall through */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300172 case USB_SPEED_SUPER:
173 if (gadget_is_superspeed(g)) {
174 speed_desc = f->ss_descriptors;
175 want_comp_desc = 1;
176 break;
177 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500178 /* fall through */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300179 case USB_SPEED_HIGH:
180 if (gadget_is_dualspeed(g)) {
181 speed_desc = f->hs_descriptors;
182 break;
183 }
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500184 /* fall through */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300185 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200186 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300187 }
Hemant Kumar08901c62018-09-18 12:01:17 -0700188
189 if (!speed_desc) {
190 DBG(cdev, "%s desc not present for function %s\n",
191 usb_speed_string(g->speed), f->name);
192 return -EIO;
193 }
194
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300195 /* find descriptors */
196 for_each_ep_desc(speed_desc, d_spd) {
197 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
198 if (chosen_desc->bEndpointAddress == _ep->address)
199 goto ep_found;
200 }
201 return -EIO;
202
203ep_found:
204 /* commit results */
Felipe Balbi9ad58772016-09-28 14:17:38 +0300205 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300206 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300207 _ep->comp_desc = NULL;
208 _ep->maxburst = 0;
Felipe Balbieaa496f2016-09-28 12:33:31 +0300209 _ep->mult = 1;
210
211 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
212 usb_endpoint_xfer_int(_ep->desc)))
213 _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
214
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300215 if (!want_comp_desc)
216 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300217
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300218 /*
219 * Companion descriptor should follow EP descriptor
220 * USB 3.0 spec, #9.6.7
221 */
222 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
223 if (!comp_desc ||
224 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
225 return -EIO;
226 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800227 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300228 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300229 case USB_ENDPOINT_XFER_ISOC:
230 /* mult: bits 1:0 of bmAttributes */
Felipe Balbieaa496f2016-09-28 12:33:31 +0300231 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Gustavo A. R. Silva624916a2017-10-25 12:22:50 -0500232 /* fall through */
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800233 case USB_ENDPOINT_XFER_BULK:
234 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300235 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300236 break;
237 default:
Hemant Kumar08901c62018-09-18 12:01:17 -0700238 if (comp_desc->bMaxBurst != 0)
Felipe Balbib785ea72012-06-06 10:20:23 +0300239 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
Hemant Kumar08901c62018-09-18 12:01:17 -0700240
Felipe Balbib785ea72012-06-06 10:20:23 +0300241 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300242 break;
243 }
244 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300245 return 0;
246}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200247EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700248
249/**
250 * usb_add_function() - add a function to a configuration
251 * @config: the configuration
252 * @function: the function being added
253 * Context: single threaded during gadget setup
254 *
255 * After initialization, each configuration must have one or more
256 * functions added to it. Adding a function involves calling its @bind()
257 * method to allocate resources such as interface and string identifiers
258 * and endpoints.
259 *
260 * This function returns the value of the function's bind(), which is
261 * zero for success else a negative errno value.
262 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200263int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700264 struct usb_function *function)
265{
266 int value = -EINVAL;
267
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +0530268 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700269 function->name, function,
270 config->label, config);
271
272 if (!function->set_alt || !function->disable)
273 goto done;
274
275 function->config = config;
Danny Segald6c40da2016-12-06 15:35:24 -0800276 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700277 list_add_tail(&function->list, &config->functions);
278
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200279 if (function->bind_deactivated) {
280 value = usb_function_deactivate(function);
281 if (value)
282 goto done;
283 }
284
David Brownell40982be2008-06-19 17:52:58 -0700285 /* REVISIT *require* function->bind? */
286 if (function->bind) {
287 value = function->bind(config, function);
288 if (value < 0) {
289 list_del(&function->list);
290 function->config = NULL;
291 }
292 } else
293 value = 0;
294
295 /* We allow configurations that don't work at both speeds.
296 * If we run into a lowspeed Linux system, treat it the same
297 * as full speed ... it's the function drivers that will need
298 * to avoid bulk and ISO transfers.
299 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200300 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700301 config->fullspeed = true;
302 if (!config->highspeed && function->hs_descriptors)
303 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300304 if (!config->superspeed && function->ss_descriptors)
305 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800306 if (!config->superspeed_plus && function->ssp_descriptors)
307 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700308
309done:
310 if (value)
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +0530311 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700312 function->name, function, value);
313 return value;
314}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200315EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700316
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100317void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
318{
319 if (f->disable)
320 f->disable(f);
321
322 bitmap_zero(f->endpoints, 32);
323 list_del(&f->list);
324 if (f->unbind)
325 f->unbind(c, f);
Felipe Balbi0e3e9752017-06-06 14:47:29 +0300326
327 if (f->bind_deactivated)
328 usb_function_activate(f);
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100329}
330EXPORT_SYMBOL_GPL(usb_remove_function);
331
David Brownell40982be2008-06-19 17:52:58 -0700332/**
David Brownell60beed92008-08-18 17:38:22 -0700333 * usb_function_deactivate - prevent function and gadget enumeration
334 * @function: the function that isn't yet ready to respond
335 *
336 * Blocks response of the gadget driver to host enumeration by
337 * preventing the data line pullup from being activated. This is
338 * normally called during @bind() processing to change from the
339 * initial "ready to respond" state, or when a required resource
340 * becomes available.
341 *
342 * For example, drivers that serve as a passthrough to a userspace
343 * daemon can block enumeration unless that daemon (such as an OBEX,
344 * MTP, or print server) is ready to handle host requests.
345 *
346 * Not all systems support software control of their USB peripheral
347 * data pullups.
348 *
349 * Returns zero on success, else negative errno.
350 */
351int usb_function_deactivate(struct usb_function *function)
352{
353 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200354 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700355 int status = 0;
356
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200357 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700358
Sriharsha Allenkif5f7d652017-11-22 17:12:11 +0530359 if (cdev->deactivations == 0) {
360 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200361 status = usb_gadget_deactivate(cdev->gadget);
Sriharsha Allenkif5f7d652017-11-22 17:12:11 +0530362 spin_lock_irqsave(&cdev->lock, flags);
363 }
David Brownell60beed92008-08-18 17:38:22 -0700364 if (status == 0)
365 cdev->deactivations++;
366
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200367 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700368 return status;
369}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200370EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700371
372/**
373 * usb_function_activate - allow function and gadget enumeration
374 * @function: function on which usb_function_activate() was called
375 *
376 * Reverses effect of usb_function_deactivate(). If no more functions
377 * are delaying their activation, the gadget driver will respond to
378 * host enumeration procedures.
379 *
380 * Returns zero on success, else negative errno.
381 */
382int usb_function_activate(struct usb_function *function)
383{
384 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200385 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700386 int status = 0;
387
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200388 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700389
390 if (WARN_ON(cdev->deactivations == 0))
391 status = -EINVAL;
392 else {
393 cdev->deactivations--;
Sriharsha Allenkif5f7d652017-11-22 17:12:11 +0530394 if (cdev->deactivations == 0) {
395 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200396 status = usb_gadget_activate(cdev->gadget);
Sriharsha Allenkif5f7d652017-11-22 17:12:11 +0530397 spin_lock_irqsave(&cdev->lock, flags);
398 }
David Brownell60beed92008-08-18 17:38:22 -0700399 }
400
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200401 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700402 return status;
403}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200404EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700405
406/**
David Brownell40982be2008-06-19 17:52:58 -0700407 * usb_interface_id() - allocate an unused interface ID
408 * @config: configuration associated with the interface
409 * @function: function handling the interface
410 * Context: single threaded during gadget setup
411 *
412 * usb_interface_id() is called from usb_function.bind() callbacks to
413 * allocate new interface IDs. The function driver will then store that
414 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300415 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700416 * particularly changing its altsetting via set_alt(). There may
417 * also be class-specific or vendor-specific requests to handle.
418 *
419 * All interface identifier should be allocated using this routine, to
420 * ensure that for example different functions don't wrongly assign
421 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300422 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700423 * one configuration (or more than once in a given configuration) need
424 * multiple versions of the relevant descriptors.
425 *
426 * Returns the interface ID which was allocated; or -ENODEV if no
427 * more interface IDs can be allocated.
428 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200429int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700430 struct usb_function *function)
431{
432 unsigned id = config->next_interface_id;
433
434 if (id < MAX_CONFIG_INTERFACES) {
435 config->interface[id] = function;
Danny Segald6c40da2016-12-06 15:35:24 -0800436 if (function->intf_id < 0)
437 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700438 config->next_interface_id = id + 1;
439 return id;
440 }
441 return -ENODEV;
442}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200443EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700444
Danny Segald6c40da2016-12-06 15:35:24 -0800445static int usb_func_wakeup_int(struct usb_function *func)
446{
447 int ret;
448 struct usb_gadget *gadget;
449
450 pr_debug("%s - %s function wakeup\n",
451 __func__, func->name ? func->name : "");
452
453 if (!func || !func->config || !func->config->cdev ||
454 !func->config->cdev->gadget)
455 return -EINVAL;
456
457 gadget = func->config->cdev->gadget;
458 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
459 DBG(func->config->cdev,
460 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
461 gadget->speed,
462 func->func_wakeup_allowed);
463
464 return -ENOTSUPP;
465 }
466
467 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
468
469 return ret;
470}
471
472int usb_func_wakeup(struct usb_function *func)
473{
474 int ret;
475 unsigned long flags;
476
477 pr_debug("%s function wakeup\n",
478 func->name ? func->name : "");
479
480 spin_lock_irqsave(&func->config->cdev->lock, flags);
481 ret = usb_func_wakeup_int(func);
482 if (ret == -EAGAIN) {
483 DBG(func->config->cdev,
484 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
485 func->name ? func->name : "");
486 ret = 0;
487 } else if (ret < 0 && ret != -ENOTSUPP) {
488 ERROR(func->config->cdev,
489 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
490 func->name ? func->name : "", ret);
491 }
492
493 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
494 return ret;
495}
496EXPORT_SYMBOL(usb_func_wakeup);
497
498int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
499 struct usb_request *req, gfp_t gfp_flags)
500{
501 int ret;
502 struct usb_gadget *gadget;
503
504 if (!func || !func->config || !func->config->cdev ||
505 !func->config->cdev->gadget || !ep || !req) {
506 ret = -EINVAL;
507 goto done;
508 }
509
510 pr_debug("Function %s queueing new data into ep %u\n",
511 func->name ? func->name : "", ep->address);
512
513 gadget = func->config->cdev->gadget;
514 if (func->func_is_suspended && func->func_wakeup_allowed) {
515 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
516 if (ret == -EAGAIN) {
517 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
518 func->name ? func->name : "");
519 } else if (ret < 0 && ret != -ENOTSUPP) {
520 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
521 func->name ? func->name : "", ret);
522 }
523 goto done;
524 }
525
526 if (!func->func_is_suspended)
527 ret = 0;
528
529 if (func->func_is_suspended && !func->func_wakeup_allowed) {
530 ret = -ENOTSUPP;
531 goto done;
532 }
533
534 ret = usb_ep_queue(ep, req, gfp_flags);
535done:
536 return ret;
537}
538EXPORT_SYMBOL(usb_func_ep_queue);
539
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100540static u8 encode_bMaxPower(enum usb_device_speed speed,
541 struct usb_configuration *c)
542{
543 unsigned val;
544
545 if (c->MaxPower)
546 val = c->MaxPower;
547 else
548 val = CONFIG_USB_GADGET_VBUS_DRAW;
549 if (!val)
550 return 0;
551 switch (speed) {
552 case USB_SPEED_SUPER:
Mayank Ranab0895972018-04-03 16:23:32 -0700553 case USB_SPEED_SUPER_PLUS:
Mayank Ranaea262b42018-04-02 15:48:52 -0700554 return (u8)(val / 8);
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100555 default:
Jack Phamc17588e2018-01-18 10:53:56 -0800556 /* only SuperSpeed and faster support > 500mA */
557 return DIV_ROUND_UP(min(val, 500U), 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700558 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100559}
560
David Brownell40982be2008-06-19 17:52:58 -0700561static int config_buf(struct usb_configuration *config,
562 enum usb_device_speed speed, void *buf, u8 type)
563{
564 struct usb_config_descriptor *c = buf;
565 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200566 int len;
David Brownell40982be2008-06-19 17:52:58 -0700567 struct usb_function *f;
568 int status;
569
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200570 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700571 /* write the config descriptor */
572 c = buf;
573 c->bLength = USB_DT_CONFIG_SIZE;
574 c->bDescriptorType = type;
575 /* wTotalLength is written later */
576 c->bNumInterfaces = config->next_interface_id;
577 c->bConfigurationValue = config->bConfigurationValue;
578 c->iConfiguration = config->iConfiguration;
579 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100580 c->bMaxPower = encode_bMaxPower(speed, config);
Vijayavardhan Vennapusa29ffacc2018-08-28 11:06:16 +0530581 if (config->cdev->gadget->is_selfpowered) {
582 c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER;
583 c->bMaxPower = 0;
584 }
David Brownell40982be2008-06-19 17:52:58 -0700585
586 /* There may be e.g. OTG descriptors */
587 if (config->descriptors) {
588 status = usb_descriptor_fillbuf(next, len,
589 config->descriptors);
590 if (status < 0)
591 return status;
592 len -= status;
593 next += status;
594 }
595
596 /* add each function's descriptors */
597 list_for_each_entry(f, &config->functions, list) {
598 struct usb_descriptor_header **descriptors;
599
John Younf3bdbe32016-02-05 17:07:03 -0800600 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700601 if (!descriptors)
602 continue;
603 status = usb_descriptor_fillbuf(next, len,
604 (const struct usb_descriptor_header **) descriptors);
605 if (status < 0)
606 return status;
607 len -= status;
608 next += status;
609 }
610
611 len = next - buf;
612 c->wTotalLength = cpu_to_le16(len);
613 return len;
614}
615
616static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
617{
618 struct usb_gadget *gadget = cdev->gadget;
619 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200620 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700621 u8 type = w_value >> 8;
622 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
623
John Youneae58202016-02-05 17:07:17 -0800624 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300625 speed = gadget->speed;
626 else if (gadget_is_dualspeed(gadget)) {
627 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700628 if (gadget->speed == USB_SPEED_HIGH)
629 hs = 1;
630 if (type == USB_DT_OTHER_SPEED_CONFIG)
631 hs = !hs;
632 if (hs)
633 speed = USB_SPEED_HIGH;
634
635 }
636
637 /* This is a lookup by config *INDEX* */
638 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200639
640 pos = &cdev->configs;
641 c = cdev->os_desc_config;
642 if (c)
643 goto check_config;
644
645 while ((pos = pos->next) != &cdev->configs) {
646 c = list_entry(pos, typeof(*c), list);
647
648 /* skip OS Descriptors config which is handled separately */
649 if (c == cdev->os_desc_config)
650 continue;
651
652check_config:
David Brownell40982be2008-06-19 17:52:58 -0700653 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300654 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800655 case USB_SPEED_SUPER_PLUS:
656 if (!c->superspeed_plus)
657 continue;
658 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300659 case USB_SPEED_SUPER:
660 if (!c->superspeed)
661 continue;
662 break;
663 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700664 if (!c->highspeed)
665 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300666 break;
667 default:
David Brownell40982be2008-06-19 17:52:58 -0700668 if (!c->fullspeed)
669 continue;
670 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300671
David Brownell40982be2008-06-19 17:52:58 -0700672 if (w_value == 0)
673 return config_buf(c, speed, cdev->req->buf, type);
674 w_value--;
675 }
676 return -EINVAL;
677}
678
679static int count_configs(struct usb_composite_dev *cdev, unsigned type)
680{
681 struct usb_gadget *gadget = cdev->gadget;
682 struct usb_configuration *c;
683 unsigned count = 0;
684 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300685 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800686 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700687
688 if (gadget_is_dualspeed(gadget)) {
689 if (gadget->speed == USB_SPEED_HIGH)
690 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300691 if (gadget->speed == USB_SPEED_SUPER)
692 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800693 if (gadget->speed == USB_SPEED_SUPER_PLUS)
694 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700695 if (type == USB_DT_DEVICE_QUALIFIER)
696 hs = !hs;
697 }
698 list_for_each_entry(c, &cdev->configs, list) {
699 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800700 if (ssp) {
701 if (!c->superspeed_plus)
702 continue;
703 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300704 if (!c->superspeed)
705 continue;
706 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700707 if (!c->highspeed)
708 continue;
709 } else {
710 if (!c->fullspeed)
711 continue;
712 }
713 count++;
714 }
715 return count;
716}
717
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300718/**
719 * bos_desc() - prepares the BOS descriptor.
720 * @cdev: pointer to usb_composite device to generate the bos
721 * descriptor for
722 *
723 * This function generates the BOS (Binary Device Object)
724 * descriptor and its device capabilities descriptors. The BOS
725 * descriptor should be supported by a SuperSpeed device.
726 */
727static int bos_desc(struct usb_composite_dev *cdev)
728{
729 struct usb_ext_cap_descriptor *usb_ext;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300730 struct usb_dcd_config_params dcd_config_params;
731 struct usb_bos_descriptor *bos = cdev->req->buf;
732
733 bos->bLength = USB_DT_BOS_SIZE;
734 bos->bDescriptorType = USB_DT_BOS;
735
736 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
737 bos->bNumDeviceCaps = 0;
738
739 /*
740 * A SuperSpeed device shall include the USB2.0 extension descriptor
741 * and shall support LPM when operating in USB2.0 HS mode.
742 */
743 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
744 bos->bNumDeviceCaps++;
745 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
746 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
747 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
748 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500749 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300750
751 /*
752 * The Superspeed USB Capability descriptor shall be implemented by all
753 * SuperSpeed devices.
754 */
John Youn0b67a6b2017-04-28 12:55:14 +0400755 if (gadget_is_superspeed(cdev->gadget)) {
756 struct usb_ss_cap_descriptor *ss_cap;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300757
John Youn0b67a6b2017-04-28 12:55:14 +0400758 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
759 bos->bNumDeviceCaps++;
760 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
761 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
762 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
763 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
764 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
765 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
766 USB_FULL_SPEED_OPERATION |
767 USB_HIGH_SPEED_OPERATION |
768 USB_5GBPS_OPERATION);
769 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
770
771 /* Get Controller configuration */
772 if (cdev->gadget->ops->get_config_params) {
773 cdev->gadget->ops->get_config_params(
774 &dcd_config_params);
775 } else {
776 dcd_config_params.bU1devExitLat =
777 USB_DEFAULT_U1_DEV_EXIT_LAT;
778 dcd_config_params.bU2DevExitLat =
779 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
780 }
781 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
782 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300783 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300784
John Younf228a8d2016-02-05 17:05:53 -0800785 /* The SuperSpeedPlus USB Device Capability descriptor */
786 if (gadget_is_superspeed_plus(cdev->gadget)) {
787 struct usb_ssp_cap_descriptor *ssp_cap;
788
789 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
790 bos->bNumDeviceCaps++;
791
792 /*
793 * Report typical values.
794 */
795
796 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
797 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
798 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
799 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700800 ssp_cap->bReserved = 0;
801 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800802
803 /* SSAC = 1 (2 attributes) */
804 ssp_cap->bmAttributes = cpu_to_le32(1);
805
806 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700807 ssp_cap->wFunctionalitySupport =
808 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800809
810 /*
811 * bmSublinkSpeedAttr[0]:
812 * ST = Symmetric, RX
813 * LSE = 3 (Gbps)
814 * LP = 1 (SuperSpeedPlus)
815 * LSM = 10 (10 Gbps)
816 */
817 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700818 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800819 /*
820 * bmSublinkSpeedAttr[1] =
821 * ST = Symmetric, TX
822 * LSE = 3 (Gbps)
823 * LP = 1 (SuperSpeedPlus)
824 * LSM = 10 (10 Gbps)
825 */
826 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700827 cpu_to_le32((3 << 4) | (1 << 14) |
828 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800829 }
830
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300831 return le16_to_cpu(bos->wTotalLength);
832}
833
David Brownell40982be2008-06-19 17:52:58 -0700834static void device_qual(struct usb_composite_dev *cdev)
835{
836 struct usb_qualifier_descriptor *qual = cdev->req->buf;
837
838 qual->bLength = sizeof(*qual);
839 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
840 /* POLICY: same bcdUSB and device type info at both speeds */
841 qual->bcdUSB = cdev->desc.bcdUSB;
842 qual->bDeviceClass = cdev->desc.bDeviceClass;
843 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
844 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
845 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200846 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700847 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700848 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700849}
850
851/*-------------------------------------------------------------------------*/
852
853static void reset_config(struct usb_composite_dev *cdev)
854{
855 struct usb_function *f;
856
857 DBG(cdev, "reset config\n");
858
859 list_for_each_entry(f, &cdev->config->functions, list) {
860 if (f->disable)
861 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200862
Danny Segald6c40da2016-12-06 15:35:24 -0800863 /* USB 3.0 addition */
864 f->func_is_suspended = false;
865 f->func_wakeup_allowed = false;
866 f->func_wakeup_pending = false;
867
Laurent Pinchart52426582009-10-21 00:03:38 +0200868 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700869 }
870 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100871 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700872}
873
874static int set_config(struct usb_composite_dev *cdev,
875 const struct usb_ctrlrequest *ctrl, unsigned number)
876{
877 struct usb_gadget *gadget = cdev->gadget;
878 struct usb_configuration *c = NULL;
879 int result = -EINVAL;
880 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
881 int tmp;
882
David Brownell40982be2008-06-19 17:52:58 -0700883 if (number) {
884 list_for_each_entry(c, &cdev->configs, list) {
885 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300886 /*
887 * We disable the FDs of the previous
888 * configuration only if the new configuration
889 * is a valid one
890 */
891 if (cdev->config)
892 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700893 result = 0;
894 break;
895 }
896 }
897 if (result < 0)
898 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300899 } else { /* Zero configuration value - need to reset the config */
900 if (cdev->config)
901 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700902 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300903 }
David Brownell40982be2008-06-19 17:52:58 -0700904
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200905 INFO(cdev, "%s config #%d: %s\n",
906 usb_speed_string(gadget->speed),
907 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700908
909 if (!c)
910 goto done;
911
Peter Chen6027f312014-04-29 13:26:28 +0800912 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700913 cdev->config = c;
914
915 /* Initialize all interfaces by setting them to altsetting zero. */
916 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
917 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200918 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700919
920 if (!f)
921 break;
922
Laurent Pinchart52426582009-10-21 00:03:38 +0200923 /*
924 * Record which endpoints are used by the function. This is used
925 * to dispatch control requests targeted at that endpoint to the
926 * function's setup callback instead of the current
927 * configuration's setup callback.
928 */
John Younf3bdbe32016-02-05 17:07:03 -0800929 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200930
931 for (; *descriptors; ++descriptors) {
932 struct usb_endpoint_descriptor *ep;
933 int addr;
934
935 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
936 continue;
937
938 ep = (struct usb_endpoint_descriptor *)*descriptors;
939 addr = ((ep->bEndpointAddress & 0x80) >> 3)
940 | (ep->bEndpointAddress & 0x0f);
941 set_bit(addr, f->endpoints);
942 }
943
David Brownell40982be2008-06-19 17:52:58 -0700944 result = f->set_alt(f, tmp, 0);
945 if (result < 0) {
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +0530946 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700947 tmp, f->name, f, result);
948
949 reset_config(cdev);
950 goto done;
951 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300952
953 if (result == USB_GADGET_DELAYED_STATUS) {
954 DBG(cdev,
955 "%s: interface %d (%s) requested delayed status\n",
956 __func__, tmp, f->name);
957 cdev->delayed_status++;
958 DBG(cdev, "delayed_status count %d\n",
959 cdev->delayed_status);
960 }
David Brownell40982be2008-06-19 17:52:58 -0700961 }
962
963 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100964 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
Mayank Ranae9e128b2018-03-19 11:44:35 -0700965 if (gadget->speed < USB_SPEED_SUPER)
966 power = min(power, 500U);
967
David Brownell40982be2008-06-19 17:52:58 -0700968done:
969 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300970 if (result >= 0 && cdev->delayed_status)
971 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700972 return result;
973}
974
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100975int usb_add_config_only(struct usb_composite_dev *cdev,
976 struct usb_configuration *config)
977{
978 struct usb_configuration *c;
979
980 if (!config->bConfigurationValue)
981 return -EINVAL;
982
983 /* Prevent duplicate configuration identifiers */
984 list_for_each_entry(c, &cdev->configs, list) {
985 if (c->bConfigurationValue == config->bConfigurationValue)
986 return -EBUSY;
987 }
988
989 config->cdev = cdev;
990 list_add_tail(&config->list, &cdev->configs);
991
992 INIT_LIST_HEAD(&config->functions);
993 config->next_interface_id = 0;
994 memset(config->interface, 0, sizeof(config->interface));
995
996 return 0;
997}
998EXPORT_SYMBOL_GPL(usb_add_config_only);
999
David Brownell40982be2008-06-19 17:52:58 -07001000/**
1001 * usb_add_config() - add a configuration to a device.
1002 * @cdev: wraps the USB gadget
1003 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001004 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -07001005 * Context: single threaded during gadget setup
1006 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001007 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -07001008 * add each of the configurations it supports, using this routine.
1009 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001010 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -07001011 * is zero for success else a negative errno value. Binding configurations
1012 * assigns global resources including string IDs, and per-configuration
1013 * resources such as interface IDs and endpoints.
1014 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001015int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001016 struct usb_configuration *config,
1017 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001018{
1019 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001020
1021 if (!bind)
1022 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001023
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301024 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001025 config->bConfigurationValue,
1026 config->label, config);
1027
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001028 status = usb_add_config_only(cdev, config);
1029 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001030 goto done;
1031
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001032 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001033 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001034 while (!list_empty(&config->functions)) {
1035 struct usb_function *f;
1036
1037 f = list_first_entry(&config->functions,
1038 struct usb_function, list);
1039 list_del(&f->list);
1040 if (f->unbind) {
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301041 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001042 f->name, f);
1043 f->unbind(config, f);
1044 /* may free memory for "f" */
1045 }
1046 }
David Brownell40982be2008-06-19 17:52:58 -07001047 list_del(&config->list);
1048 config->cdev = NULL;
1049 } else {
1050 unsigned i;
1051
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301052 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001053 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001054 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001055 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001056 config->highspeed ? " high" : "",
1057 config->fullspeed
1058 ? (gadget_is_dualspeed(cdev->gadget)
1059 ? " full"
1060 : " full/low")
1061 : "");
1062
1063 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1064 struct usb_function *f = config->interface[i];
1065
1066 if (!f)
1067 continue;
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301068 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001069 i, f->name, f);
1070 }
1071 }
1072
Robert Baldygaf871cb92015-09-16 12:10:39 +02001073 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001074 usb_ep_autoconfig_reset(cdev->gadget);
1075
1076done:
1077 if (status)
1078 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1079 config->bConfigurationValue, status);
1080 return status;
1081}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001082EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001083
Benoit Goby51cce6f2012-05-10 10:07:57 +02001084static void remove_config(struct usb_composite_dev *cdev,
1085 struct usb_configuration *config)
1086{
1087 while (!list_empty(&config->functions)) {
1088 struct usb_function *f;
1089
1090 f = list_first_entry(&config->functions,
1091 struct usb_function, list);
Felipe Balbi0e3e9752017-06-06 14:47:29 +03001092
1093 usb_remove_function(config, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001094 }
1095 list_del(&config->list);
1096 if (config->unbind) {
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301097 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001098 config->unbind(config);
1099 /* may free memory for "c" */
1100 }
1101}
1102
1103/**
1104 * usb_remove_config() - remove a configuration from a device.
1105 * @cdev: wraps the USB gadget
1106 * @config: the configuration
1107 *
1108 * Drivers must call usb_gadget_disconnect before calling this function
1109 * to disconnect the device from the host and make sure the host will not
1110 * try to enumerate the device while we are changing the config list.
1111 */
1112void usb_remove_config(struct usb_composite_dev *cdev,
1113 struct usb_configuration *config)
1114{
1115 unsigned long flags;
1116
1117 spin_lock_irqsave(&cdev->lock, flags);
1118
1119 if (cdev->config == config)
1120 reset_config(cdev);
1121
1122 spin_unlock_irqrestore(&cdev->lock, flags);
1123
1124 remove_config(cdev, config);
1125}
1126
David Brownell40982be2008-06-19 17:52:58 -07001127/*-------------------------------------------------------------------------*/
1128
1129/* We support strings in multiple languages ... string descriptor zero
1130 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001131 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001132 * the host side.
1133 */
1134
1135static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1136{
1137 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001138 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001139 __le16 *tmp;
1140
1141 while (*sp) {
1142 s = *sp;
1143 language = cpu_to_le16(s->language);
1144 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1145 if (*tmp == language)
1146 goto repeat;
1147 }
1148 *tmp++ = language;
1149repeat:
1150 sp++;
1151 }
1152}
1153
1154static int lookup_string(
1155 struct usb_gadget_strings **sp,
1156 void *buf,
1157 u16 language,
1158 int id
1159)
1160{
1161 struct usb_gadget_strings *s;
1162 int value;
1163
1164 while (*sp) {
1165 s = *sp++;
1166 if (s->language != language)
1167 continue;
1168 value = usb_gadget_get_string(s, id, buf);
1169 if (value > 0)
1170 return value;
1171 }
1172 return -EINVAL;
1173}
1174
1175static int get_string(struct usb_composite_dev *cdev,
1176 void *buf, u16 language, int id)
1177{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001178 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001179 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001180 struct usb_configuration *c;
1181 struct usb_function *f;
1182 int len;
1183
Diego Violaad4676a2015-05-31 15:52:41 -03001184 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001185 * folk will ever care about ... also, it's all supported here.
1186 * (Except for UTF8 support for Unicode's "Astral Planes".)
1187 */
1188
1189 /* 0 == report all available language codes */
1190 if (id == 0) {
1191 struct usb_string_descriptor *s = buf;
1192 struct usb_gadget_strings **sp;
1193
1194 memset(s, 0, 256);
1195 s->bDescriptorType = USB_DT_STRING;
1196
1197 sp = composite->strings;
1198 if (sp)
1199 collect_langs(sp, s->wData);
1200
1201 list_for_each_entry(c, &cdev->configs, list) {
1202 sp = c->strings;
1203 if (sp)
1204 collect_langs(sp, s->wData);
1205
1206 list_for_each_entry(f, &c->functions, list) {
1207 sp = f->strings;
1208 if (sp)
1209 collect_langs(sp, s->wData);
1210 }
1211 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001212 list_for_each_entry(uc, &cdev->gstrings, list) {
1213 struct usb_gadget_strings **sp;
1214
1215 sp = get_containers_gs(uc);
1216 collect_langs(sp, s->wData);
1217 }
David Brownell40982be2008-06-19 17:52:58 -07001218
Roel Kluin417b57b2009-08-06 16:09:51 -07001219 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001220 continue;
1221 if (!len)
1222 return -EINVAL;
1223
1224 s->bLength = 2 * (len + 1);
1225 return s->bLength;
1226 }
1227
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001228 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1229 struct usb_os_string *b = buf;
1230 b->bLength = sizeof(*b);
1231 b->bDescriptorType = USB_DT_STRING;
1232 compiletime_assert(
1233 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1234 "qwSignature size must be equal to qw_sign");
1235 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1236 b->bMS_VendorCode = cdev->b_vendor_code;
1237 b->bPad = 0;
1238 return sizeof(*b);
1239 }
1240
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001241 list_for_each_entry(uc, &cdev->gstrings, list) {
1242 struct usb_gadget_strings **sp;
1243
1244 sp = get_containers_gs(uc);
1245 len = lookup_string(sp, buf, language, id);
1246 if (len > 0)
1247 return len;
1248 }
1249
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001250 /* String IDs are device-scoped, so we look up each string
1251 * table we're told about. These lookups are infrequent;
1252 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001253 */
1254 if (composite->strings) {
1255 len = lookup_string(composite->strings, buf, language, id);
1256 if (len > 0)
1257 return len;
1258 }
1259 list_for_each_entry(c, &cdev->configs, list) {
1260 if (c->strings) {
1261 len = lookup_string(c->strings, buf, language, id);
1262 if (len > 0)
1263 return len;
1264 }
1265 list_for_each_entry(f, &c->functions, list) {
1266 if (!f->strings)
1267 continue;
1268 len = lookup_string(f->strings, buf, language, id);
1269 if (len > 0)
1270 return len;
1271 }
1272 }
1273 return -EINVAL;
1274}
1275
1276/**
1277 * usb_string_id() - allocate an unused string ID
1278 * @cdev: the device whose string descriptor IDs are being allocated
1279 * Context: single threaded during gadget setup
1280 *
1281 * @usb_string_id() is called from bind() callbacks to allocate
1282 * string IDs. Drivers for functions, configurations, or gadgets will
1283 * then store that ID in the appropriate descriptors and string table.
1284 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001285 * All string identifier should be allocated using this,
1286 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1287 * that for example different functions don't wrongly assign different
1288 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001289 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001290int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001291{
1292 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001293 /* string id 0 is reserved by USB spec for list of
1294 * supported languages */
1295 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001296 cdev->next_string_id++;
1297 return cdev->next_string_id;
1298 }
1299 return -ENODEV;
1300}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001301EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001302
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001303/**
1304 * usb_string_ids() - allocate unused string IDs in batch
1305 * @cdev: the device whose string descriptor IDs are being allocated
1306 * @str: an array of usb_string objects to assign numbers to
1307 * Context: single threaded during gadget setup
1308 *
1309 * @usb_string_ids() is called from bind() callbacks to allocate
1310 * string IDs. Drivers for functions, configurations, or gadgets will
1311 * then copy IDs from the string table to the appropriate descriptors
1312 * and string table for other languages.
1313 *
1314 * All string identifier should be allocated using this,
1315 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1316 * example different functions don't wrongly assign different meanings
1317 * to the same identifier.
1318 */
1319int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1320{
1321 int next = cdev->next_string_id;
1322
1323 for (; str->s; ++str) {
1324 if (unlikely(next >= 254))
1325 return -ENODEV;
1326 str->id = ++next;
1327 }
1328
1329 cdev->next_string_id = next;
1330
1331 return 0;
1332}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001333EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001334
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001335static struct usb_gadget_string_container *copy_gadget_strings(
1336 struct usb_gadget_strings **sp, unsigned n_gstrings,
1337 unsigned n_strings)
1338{
1339 struct usb_gadget_string_container *uc;
1340 struct usb_gadget_strings **gs_array;
1341 struct usb_gadget_strings *gs;
1342 struct usb_string *s;
1343 unsigned mem;
1344 unsigned n_gs;
1345 unsigned n_s;
1346 void *stash;
1347
1348 mem = sizeof(*uc);
1349 mem += sizeof(void *) * (n_gstrings + 1);
1350 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1351 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1352 uc = kmalloc(mem, GFP_KERNEL);
1353 if (!uc)
1354 return ERR_PTR(-ENOMEM);
1355 gs_array = get_containers_gs(uc);
1356 stash = uc->stash;
1357 stash += sizeof(void *) * (n_gstrings + 1);
1358 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1359 struct usb_string *org_s;
1360
1361 gs_array[n_gs] = stash;
1362 gs = gs_array[n_gs];
1363 stash += sizeof(struct usb_gadget_strings);
1364 gs->language = sp[n_gs]->language;
1365 gs->strings = stash;
1366 org_s = sp[n_gs]->strings;
1367
1368 for (n_s = 0; n_s < n_strings; n_s++) {
1369 s = stash;
1370 stash += sizeof(struct usb_string);
1371 if (org_s->s)
1372 s->s = org_s->s;
1373 else
1374 s->s = "";
1375 org_s++;
1376 }
1377 s = stash;
1378 s->s = NULL;
1379 stash += sizeof(struct usb_string);
1380
1381 }
1382 gs_array[n_gs] = NULL;
1383 return uc;
1384}
1385
1386/**
1387 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1388 * @cdev: the device whose string descriptor IDs are being allocated
1389 * and attached.
1390 * @sp: an array of usb_gadget_strings to attach.
1391 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1392 *
1393 * This function will create a deep copy of usb_gadget_strings and usb_string
1394 * and attach it to the cdev. The actual string (usb_string.s) will not be
1395 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001396 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001397 * The ->language pointer of each struct usb_gadget_strings has to contain the
1398 * same amount of entries.
1399 * 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 +09001400 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001401 * entry of en-US. Therefore both entries become the same id assign.
1402 */
1403struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1404 struct usb_gadget_strings **sp, unsigned n_strings)
1405{
1406 struct usb_gadget_string_container *uc;
1407 struct usb_gadget_strings **n_gs;
1408 unsigned n_gstrings = 0;
1409 unsigned i;
1410 int ret;
1411
1412 for (i = 0; sp[i]; i++)
1413 n_gstrings++;
1414
1415 if (!n_gstrings)
1416 return ERR_PTR(-EINVAL);
1417
1418 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1419 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001420 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001421
1422 n_gs = get_containers_gs(uc);
1423 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1424 if (ret)
1425 goto err;
1426
1427 for (i = 1; i < n_gstrings; i++) {
1428 struct usb_string *m_s;
1429 struct usb_string *s;
1430 unsigned n;
1431
1432 m_s = n_gs[0]->strings;
1433 s = n_gs[i]->strings;
1434 for (n = 0; n < n_strings; n++) {
1435 s->id = m_s->id;
1436 s++;
1437 m_s++;
1438 }
1439 }
1440 list_add_tail(&uc->list, &cdev->gstrings);
1441 return n_gs[0]->strings;
1442err:
1443 kfree(uc);
1444 return ERR_PTR(ret);
1445}
1446EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1447
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001448/**
1449 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001450 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001451 * @n: number of string IDs to allocate
1452 * Context: single threaded during gadget setup
1453 *
1454 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001455 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001456 * is, returns last requested ID which is now very useful information.
1457 *
1458 * @usb_string_ids_n() is called from bind() callbacks to allocate
1459 * string IDs. Drivers for functions, configurations, or gadgets will
1460 * then store that ID in the appropriate descriptors and string table.
1461 *
1462 * All string identifier should be allocated using this,
1463 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1464 * example different functions don't wrongly assign different meanings
1465 * to the same identifier.
1466 */
1467int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1468{
1469 unsigned next = c->next_string_id;
1470 if (unlikely(n > 254 || (unsigned)next + n > 254))
1471 return -ENODEV;
1472 c->next_string_id += n;
1473 return next + 1;
1474}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001475EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001476
David Brownell40982be2008-06-19 17:52:58 -07001477/*-------------------------------------------------------------------------*/
1478
1479static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1480{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001481 struct usb_composite_dev *cdev;
1482
David Brownell40982be2008-06-19 17:52:58 -07001483 if (req->status || req->actual != req->length)
1484 DBG((struct usb_composite_dev *) ep->driver_data,
1485 "setup complete --> %d, %d/%d\n",
1486 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001487
1488 /*
1489 * REVIST The same ep0 requests are shared with function drivers
1490 * so they don't have to maintain the same ->complete() stubs.
1491 *
1492 * Because of that, we need to check for the validity of ->context
1493 * here, even though we know we've set it to something useful.
1494 */
1495 if (!req->context)
1496 return;
1497
1498 cdev = req->context;
1499
1500 if (cdev->req == req)
1501 cdev->setup_pending = false;
1502 else if (cdev->os_desc_req == req)
1503 cdev->os_desc_pending = false;
1504 else
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301505 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001506}
1507
1508static int composite_ep0_queue(struct usb_composite_dev *cdev,
1509 struct usb_request *req, gfp_t gfp_flags)
1510{
1511 int ret;
1512
1513 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1514 if (ret == 0) {
1515 if (cdev->req == req)
1516 cdev->setup_pending = true;
1517 else if (cdev->os_desc_req == req)
1518 cdev->os_desc_pending = true;
1519 else
Chandana Kishori Chiluveruffdb2d32017-10-28 23:05:45 +05301520 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001521 }
1522
1523 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001524}
1525
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001526static int count_ext_compat(struct usb_configuration *c)
1527{
1528 int i, res;
1529
1530 res = 0;
1531 for (i = 0; i < c->next_interface_id; ++i) {
1532 struct usb_function *f;
1533 int j;
1534
1535 f = c->interface[i];
1536 for (j = 0; j < f->os_desc_n; ++j) {
1537 struct usb_os_desc *d;
1538
1539 if (i != f->os_desc_table[j].if_id)
1540 continue;
1541 d = f->os_desc_table[j].os_desc;
1542 if (d && d->ext_compat_id)
1543 ++res;
1544 }
1545 }
1546 BUG_ON(res > 255);
1547 return res;
1548}
1549
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001550static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001551{
1552 int i, count;
1553
1554 count = 16;
Chris Dickens636ba132017-12-31 18:59:43 -08001555 buf += 16;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001556 for (i = 0; i < c->next_interface_id; ++i) {
1557 struct usb_function *f;
1558 int j;
1559
1560 f = c->interface[i];
1561 for (j = 0; j < f->os_desc_n; ++j) {
1562 struct usb_os_desc *d;
1563
1564 if (i != f->os_desc_table[j].if_id)
1565 continue;
1566 d = f->os_desc_table[j].os_desc;
1567 if (d && d->ext_compat_id) {
1568 *buf++ = i;
1569 *buf++ = 0x01;
1570 memcpy(buf, d->ext_compat_id, 16);
1571 buf += 22;
1572 } else {
1573 ++buf;
1574 *buf = 0x01;
1575 buf += 23;
1576 }
1577 count += 24;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001578 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1579 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001580 }
1581 }
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001582
1583 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001584}
1585
1586static int count_ext_prop(struct usb_configuration *c, int interface)
1587{
1588 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001589 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001590
Chandana Kishori Chiluverubb93c432018-09-10 19:27:36 +05301591 if (interface >= c->next_interface_id)
1592 return -EINVAL;
1593
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001594 f = c->interface[interface];
1595 for (j = 0; j < f->os_desc_n; ++j) {
1596 struct usb_os_desc *d;
1597
1598 if (interface != f->os_desc_table[j].if_id)
1599 continue;
1600 d = f->os_desc_table[j].os_desc;
1601 if (d && d->ext_compat_id)
1602 return d->ext_prop_count;
1603 }
Julia Lawall849b1332014-05-19 06:31:07 +02001604 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001605}
1606
1607static int len_ext_prop(struct usb_configuration *c, int interface)
1608{
1609 struct usb_function *f;
1610 struct usb_os_desc *d;
1611 int j, res;
1612
Chandana Kishori Chiluverubb93c432018-09-10 19:27:36 +05301613 if (interface >= c->next_interface_id)
1614 return -EINVAL;
1615
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001616 res = 10; /* header length */
1617 f = c->interface[interface];
1618 for (j = 0; j < f->os_desc_n; ++j) {
1619 if (interface != f->os_desc_table[j].if_id)
1620 continue;
1621 d = f->os_desc_table[j].os_desc;
1622 if (d)
1623 return min(res + d->ext_prop_len, 4096);
1624 }
1625 return res;
1626}
1627
1628static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1629{
1630 struct usb_function *f;
1631 struct usb_os_desc *d;
1632 struct usb_os_desc_ext_prop *ext_prop;
1633 int j, count, n, ret;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001634
1635 f = c->interface[interface];
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001636 count = 10; /* header length */
Chris Dickens636ba132017-12-31 18:59:43 -08001637 buf += 10;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001638 for (j = 0; j < f->os_desc_n; ++j) {
1639 if (interface != f->os_desc_table[j].if_id)
1640 continue;
1641 d = f->os_desc_table[j].os_desc;
1642 if (d)
1643 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001644 n = ext_prop->data_len +
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001645 ext_prop->name_len + 14;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001646 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1647 return count;
1648 usb_ext_prop_put_size(buf, n);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001649 usb_ext_prop_put_type(buf, ext_prop->type);
1650 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1651 ext_prop->name_len);
1652 if (ret < 0)
1653 return ret;
1654 switch (ext_prop->type) {
1655 case USB_EXT_PROP_UNICODE:
1656 case USB_EXT_PROP_UNICODE_ENV:
1657 case USB_EXT_PROP_UNICODE_LINK:
1658 usb_ext_prop_put_unicode(buf, ret,
1659 ext_prop->data,
1660 ext_prop->data_len);
1661 break;
1662 case USB_EXT_PROP_BINARY:
1663 usb_ext_prop_put_binary(buf, ret,
1664 ext_prop->data,
1665 ext_prop->data_len);
1666 break;
1667 case USB_EXT_PROP_LE32:
1668 /* not implemented */
1669 case USB_EXT_PROP_BE32:
1670 /* not implemented */
1671 default:
1672 return -EINVAL;
1673 }
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001674 buf += n;
1675 count += n;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001676 }
1677 }
1678
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001679 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001680}
1681
David Brownell40982be2008-06-19 17:52:58 -07001682/*
1683 * The setup() callback implements all the ep0 functionality that's
1684 * not handled lower down, in hardware or the hardware driver(like
1685 * device and endpoint feature flags, and their status). It's all
1686 * housekeeping for the gadget function we're implementing. Most of
1687 * the work is in config and function specific setup.
1688 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001689int
David Brownell40982be2008-06-19 17:52:58 -07001690composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1691{
1692 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1693 struct usb_request *req = cdev->req;
1694 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001695 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001696 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001697 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001698 u16 w_value = le16_to_cpu(ctrl->wValue);
1699 u16 w_length = le16_to_cpu(ctrl->wLength);
1700 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001701 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001702
1703 /* partial re-init of the response message; the function or the
1704 * gadget might need to intercept e.g. a control-OUT completion
1705 * when we delegate to it.
1706 */
1707 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001708 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001709 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301710 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001711 gadget->ep0->driver_data = cdev;
1712
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001713 /*
1714 * Don't let non-standard requests match any of the cases below
1715 * by accident.
1716 */
1717 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1718 goto unknown;
1719
David Brownell40982be2008-06-19 17:52:58 -07001720 switch (ctrl->bRequest) {
1721
1722 /* we handle all standard USB descriptors */
1723 case USB_REQ_GET_DESCRIPTOR:
1724 if (ctrl->bRequestType != USB_DIR_IN)
1725 goto unknown;
1726 switch (w_value >> 8) {
1727
1728 case USB_DT_DEVICE:
1729 cdev->desc.bNumConfigurations =
1730 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001731 cdev->desc.bMaxPacketSize0 =
1732 cdev->gadget->ep0->maxpacket;
1733 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001734 if (gadget->speed >= USB_SPEED_SUPER) {
Chunfeng Yun1ef6c422018-05-09 19:29:16 +08001735 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001736 cdev->desc.bMaxPacketSize0 = 9;
1737 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001738 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001739 }
Igor Kotrasinski5527e732015-08-20 10:00:01 +02001740 } else {
John Youna9548c52017-04-28 12:55:20 +04001741 if (gadget->lpm_capable)
1742 cdev->desc.bcdUSB = cpu_to_le16(0x0201);
1743 else
1744 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001745 }
1746
David Brownell40982be2008-06-19 17:52:58 -07001747 value = min(w_length, (u16) sizeof cdev->desc);
1748 memcpy(req->buf, &cdev->desc, value);
1749 break;
1750 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001751 if (!gadget_is_dualspeed(gadget) ||
1752 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001753 break;
1754 device_qual(cdev);
1755 value = min_t(int, w_length,
1756 sizeof(struct usb_qualifier_descriptor));
1757 break;
1758 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001759 if (!gadget_is_dualspeed(gadget) ||
1760 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001761 break;
1762 /* FALLTHROUGH */
1763 case USB_DT_CONFIG:
1764 value = config_desc(cdev, w_value);
1765 if (value >= 0)
1766 value = min(w_length, (u16) value);
1767 break;
1768 case USB_DT_STRING:
1769 value = get_string(cdev, req->buf,
1770 w_index, w_value & 0xff);
1771 if (value >= 0)
1772 value = min(w_length, (u16) value);
1773 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001774 case USB_DT_BOS:
John Youna9548c52017-04-28 12:55:20 +04001775 if (gadget_is_superspeed(gadget) ||
1776 gadget->lpm_capable) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001777 value = bos_desc(cdev);
1778 value = min(w_length, (u16) value);
1779 }
1780 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001781 case USB_DT_OTG:
1782 if (gadget_is_otg(gadget)) {
1783 struct usb_configuration *config;
1784 int otg_desc_len = 0;
1785
1786 if (cdev->config)
1787 config = cdev->config;
1788 else
1789 config = list_first_entry(
1790 &cdev->configs,
1791 struct usb_configuration, list);
1792 if (!config)
1793 goto done;
1794
1795 if (gadget->otg_caps &&
1796 (gadget->otg_caps->otg_rev >= 0x0200))
1797 otg_desc_len += sizeof(
1798 struct usb_otg20_descriptor);
1799 else
1800 otg_desc_len += sizeof(
1801 struct usb_otg_descriptor);
1802
1803 value = min_t(int, w_length, otg_desc_len);
1804 memcpy(req->buf, config->descriptors[0], value);
1805 }
1806 break;
David Brownell40982be2008-06-19 17:52:58 -07001807 }
1808 break;
1809
1810 /* any number of configs can work */
1811 case USB_REQ_SET_CONFIGURATION:
1812 if (ctrl->bRequestType != 0)
1813 goto unknown;
1814 if (gadget_is_otg(gadget)) {
1815 if (gadget->a_hnp_support)
1816 DBG(cdev, "HNP available\n");
1817 else if (gadget->a_alt_hnp_support)
1818 DBG(cdev, "HNP on another port\n");
1819 else
1820 VDBG(cdev, "HNP inactive\n");
1821 }
1822 spin_lock(&cdev->lock);
1823 value = set_config(cdev, ctrl, w_value);
1824 spin_unlock(&cdev->lock);
1825 break;
1826 case USB_REQ_GET_CONFIGURATION:
1827 if (ctrl->bRequestType != USB_DIR_IN)
1828 goto unknown;
1829 if (cdev->config)
1830 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1831 else
1832 *(u8 *)req->buf = 0;
1833 value = min(w_length, (u16) 1);
1834 break;
1835
Krzysztof Opasiak7e4da3f2016-12-20 19:52:16 +01001836 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001837 case USB_REQ_SET_INTERFACE:
1838 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1839 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001840 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001841 break;
Bryan Wu08889512009-01-08 00:21:19 +08001842 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001843 if (!f)
1844 break;
Krzysztof Opasiak7e4da3f2016-12-20 19:52:16 +01001845
1846 /*
1847 * If there's no get_alt() method, we know only altsetting zero
1848 * works. There is no need to check if set_alt() is not NULL
1849 * as we check this in usb_add_function().
1850 */
1851 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001852 break;
Chunfeng Yun980900d2018-05-25 17:24:57 +08001853
1854 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001855 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001856 if (value == USB_GADGET_DELAYED_STATUS) {
1857 DBG(cdev,
1858 "%s: interface %d (%s) requested delayed status\n",
1859 __func__, intf, f->name);
1860 cdev->delayed_status++;
1861 DBG(cdev, "delayed_status count %d\n",
1862 cdev->delayed_status);
1863 }
Chunfeng Yun980900d2018-05-25 17:24:57 +08001864 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001865 break;
1866 case USB_REQ_GET_INTERFACE:
1867 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1868 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001869 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001870 break;
Bryan Wu08889512009-01-08 00:21:19 +08001871 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001872 if (!f)
1873 break;
1874 /* lots of interfaces only need altsetting zero... */
1875 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1876 if (value < 0)
1877 break;
1878 *((u8 *)req->buf) = value;
1879 value = min(w_length, (u16) 1);
1880 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001881 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001882 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1883 (w_index == OTG_STS_SELECTOR)) {
1884 if (ctrl->bRequestType != (USB_DIR_IN |
1885 USB_RECIP_DEVICE))
1886 goto unknown;
1887 *((u8 *)req->buf) = gadget->host_request_flag;
1888 value = 1;
1889 break;
1890 }
1891
1892 /*
1893 * USB 3.0 additions:
1894 * Function driver should handle get_status request. If such cb
1895 * wasn't supplied we respond with default value = 0
1896 * Note: function driver should supply such cb only for the
1897 * first interface of the function
1898 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001899 if (!gadget_is_superspeed(gadget))
1900 goto unknown;
1901 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1902 goto unknown;
1903 value = 2; /* This is the length of the get_status reply */
1904 put_unaligned_le16(0, req->buf);
1905 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1906 break;
1907 f = cdev->config->interface[intf];
1908 if (!f)
1909 break;
Pratham Pratapcc1b2892019-12-13 12:22:13 +05301910
1911 if (USB_CONFIG_ATT_WAKEUP & cdev->config->bmAttributes)
1912 status = f->get_status ? f->get_status(f) : 0;
1913 else
1914 status = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001915 if (status < 0)
1916 break;
1917 put_unaligned_le16(status & 0x0000ffff, req->buf);
1918 break;
1919 /*
1920 * Function drivers should handle SetFeature/ClearFeature
1921 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1922 * only for the first interface of the function
1923 */
1924 case USB_REQ_CLEAR_FEATURE:
1925 case USB_REQ_SET_FEATURE:
1926 if (!gadget_is_superspeed(gadget))
1927 goto unknown;
1928 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1929 goto unknown;
1930 switch (w_value) {
1931 case USB_INTRF_FUNC_SUSPEND:
1932 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1933 break;
1934 f = cdev->config->interface[intf];
1935 if (!f)
1936 break;
1937 value = 0;
Danny Segald6c40da2016-12-06 15:35:24 -08001938 if (f->func_suspend) {
1939 const u8 suspend_opt = w_index >> 8;
1940
1941 value = f->func_suspend(f, suspend_opt);
1942 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1943 f->name ? f->name : "", suspend_opt);
1944 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001945 if (value < 0) {
1946 ERROR(cdev,
1947 "func_suspend() returned error %d\n",
1948 value);
1949 value = 0;
1950 }
1951 break;
1952 }
1953 break;
David Brownell40982be2008-06-19 17:52:58 -07001954 default:
1955unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001956 /*
1957 * OS descriptors handling
1958 */
1959 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001960 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001961 ctrl->bRequest == cdev->b_vendor_code) {
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001962 struct usb_configuration *os_desc_cfg;
1963 u8 *buf;
1964 int interface;
1965 int count = 0;
1966
1967 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001968 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001969 req->complete = composite_setup_complete;
1970 buf = req->buf;
1971 os_desc_cfg = cdev->os_desc_config;
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001972 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001973 memset(buf, 0, w_length);
1974 buf[5] = 0x01;
1975 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1976 case USB_RECIP_DEVICE:
1977 if (w_index != 0x4 || (w_value >> 8))
1978 break;
1979 buf[6] = w_index;
Chris Dickens636ba132017-12-31 18:59:43 -08001980 /* Number of ext compat interfaces */
1981 count = count_ext_compat(os_desc_cfg);
Chandana Kishori Chiluveru7cd4d872019-01-25 15:22:35 +05301982 /*
1983 * Bailout if device does not
1984 * have ext_compat interfaces.
1985 */
1986 if (count == 0)
1987 break;
Chris Dickens636ba132017-12-31 18:59:43 -08001988 buf[8] = count;
1989 count *= 24; /* 24 B/ext compat desc */
1990 count += 16; /* header */
1991 put_unaligned_le32(count, buf);
1992 value = w_length;
1993 if (w_length > 0x10) {
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08001994 value = fill_ext_compat(os_desc_cfg, buf);
1995 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001996 }
1997 break;
1998 case USB_RECIP_INTERFACE:
1999 if (w_index != 0x5 || (w_value >> 8))
2000 break;
2001 interface = w_value & 0xFF;
2002 buf[6] = w_index;
Chris Dickens636ba132017-12-31 18:59:43 -08002003 count = count_ext_prop(os_desc_cfg,
2004 interface);
Chandana Kishori Chiluverubb93c432018-09-10 19:27:36 +05302005 if (count < 0)
2006 return count;
Chris Dickens636ba132017-12-31 18:59:43 -08002007 put_unaligned_le16(count, buf + 8);
2008 count = len_ext_prop(os_desc_cfg,
2009 interface);
2010 put_unaligned_le32(count, buf);
2011 value = w_length;
2012 if (w_length > 0x0A) {
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002013 value = fill_ext_prop(os_desc_cfg,
2014 interface, buf);
Chris Dickens636ba132017-12-31 18:59:43 -08002015 if (value >= 0)
2016 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002017 }
2018 break;
2019 }
William Wu7e14f47a2016-05-13 18:30:42 +08002020
Chris Dickens636ba132017-12-31 18:59:43 -08002021 goto check_value;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002022 }
2023
David Brownell40982be2008-06-19 17:52:58 -07002024 VDBG(cdev,
2025 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2026 ctrl->bRequestType, ctrl->bRequest,
2027 w_value, w_index, w_length);
2028
Laurent Pinchart52426582009-10-21 00:03:38 +02002029 /* functions always handle their interfaces and endpoints...
2030 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002031 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002032 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302033 if (cdev->config) {
2034 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b4572016-06-22 01:12:08 +02002035 if (f->req_match &&
2036 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302037 goto try_fun_setup;
Felix Hädicke1a00b4572016-06-22 01:12:08 +02002038 } else {
2039 struct usb_configuration *c;
2040 list_for_each_entry(c, &cdev->configs, list)
2041 list_for_each_entry(f, &c->functions, list)
2042 if (f->req_match &&
2043 f->req_match(f, ctrl, true))
2044 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302045 }
Felix Hädicke1a00b4572016-06-22 01:12:08 +02002046 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302047
Laurent Pinchart52426582009-10-21 00:03:38 +02002048 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2049 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002050 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302051 break;
2052 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002053 break;
2054
2055 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002056 if (!cdev->config)
2057 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002058 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2059 list_for_each_entry(f, &cdev->config->functions, list) {
2060 if (test_bit(endp, f->endpoints))
2061 break;
2062 }
2063 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002064 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002065 break;
David Brownell40982be2008-06-19 17:52:58 -07002066 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002067try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002068 if (f && f->setup)
2069 value = f->setup(f, ctrl);
2070 else {
David Brownell40982be2008-06-19 17:52:58 -07002071 struct usb_configuration *c;
2072
2073 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002074 if (!c)
2075 goto done;
2076
2077 /* try current config's setup */
2078 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002079 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002080 goto done;
2081 }
2082
2083 /* try the only function in the current config */
2084 if (!list_is_singular(&c->functions))
2085 goto done;
2086 f = list_first_entry(&c->functions, struct usb_function,
2087 list);
2088 if (f->setup)
2089 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002090 }
2091
2092 goto done;
2093 }
2094
Chris Dickens636ba132017-12-31 18:59:43 -08002095check_value:
David Brownell40982be2008-06-19 17:52:58 -07002096 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002097 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002098 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002099 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002100 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002101 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002102 if (value < 0) {
2103 DBG(cdev, "ep_queue --> %d\n", value);
2104 req->status = 0;
Vijayavardhan Vennapusa2e03c392017-03-02 16:07:13 +05302105 if (value != -ESHUTDOWN)
2106 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002107 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002108 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2109 WARN(cdev,
2110 "%s: Delayed status not supported for w_length != 0",
2111 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002112 }
2113
2114done:
2115 /* device either stalls (value < 0) or reports success */
2116 return value;
2117}
2118
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002119void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002120{
2121 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2122 unsigned long flags;
2123
Badhri Jagan Sridharan80532ba2015-05-06 13:40:15 -07002124 if (cdev == NULL) {
2125 WARN(1, "%s: Calling disconnect on a Gadget that is \
2126 not connected\n", __func__);
2127 return;
2128 }
2129
David Brownell40982be2008-06-19 17:52:58 -07002130 /* REVISIT: should we have config and device level
2131 * disconnect callbacks?
2132 */
2133 spin_lock_irqsave(&cdev->lock, flags);
Benjamin Herrenschmidt122ab8e2019-07-26 14:59:03 +10002134 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002135 if (cdev->config)
2136 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002137 if (cdev->driver->disconnect)
2138 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002139 spin_unlock_irqrestore(&cdev->lock, flags);
2140}
2141
2142/*-------------------------------------------------------------------------*/
2143
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002144static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2145 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002146{
2147 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2148 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2149
Vijayavardhan Vennapusac9ac60d2017-09-13 15:41:20 +05302150 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002151}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002152static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002153
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002154static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002155{
2156 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasovaec17e12017-09-30 08:55:55 -07002157 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2158 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002159
2160 /* composite_disconnect() must already have been called
2161 * by the underlying peripheral controller driver!
2162 * so there's no i/o concurrency that could affect the
2163 * state protected by cdev->lock.
2164 */
2165 WARN_ON(cdev->config);
2166
2167 while (!list_empty(&cdev->configs)) {
2168 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002169 c = list_first_entry(&cdev->configs,
2170 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002171 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002172 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002173 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002174 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002175
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002176 composite_dev_cleanup(cdev);
2177
Andrew Gabbasovaec17e12017-09-30 08:55:55 -07002178 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2179 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2180
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002181 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002182 kfree(cdev);
2183 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002184}
2185
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002186static void composite_unbind(struct usb_gadget *gadget)
2187{
2188 __composite_unbind(gadget, true);
2189}
2190
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002191static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2192 const struct usb_device_descriptor *old)
2193{
2194 __le16 idVendor;
2195 __le16 idProduct;
2196 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002197 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002198 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002199 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002200
2201 /*
2202 * these variables may have been set in
2203 * usb_composite_overwrite_options()
2204 */
2205 idVendor = new->idVendor;
2206 idProduct = new->idProduct;
2207 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002208 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002209 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002210 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002211
2212 *new = *old;
2213 if (idVendor)
2214 new->idVendor = idVendor;
2215 if (idProduct)
2216 new->idProduct = idProduct;
2217 if (bcdDevice)
2218 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002219 else
2220 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002221 if (iSerialNumber)
2222 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002223 if (iManufacturer)
2224 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002225 if (iProduct)
2226 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002227}
2228
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002229int composite_dev_prepare(struct usb_composite_driver *composite,
2230 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002231{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002232 struct usb_gadget *gadget = cdev->gadget;
2233 int ret = -ENOMEM;
2234
2235 /* preallocate control response and buffer */
2236 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2237 if (!cdev->req)
2238 return -ENOMEM;
2239
2240 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2241 if (!cdev->req->buf)
2242 goto fail;
2243
2244 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2245 if (ret)
2246 goto fail_dev;
2247
2248 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002249 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002250 gadget->ep0->driver_data = cdev;
2251
2252 cdev->driver = composite;
2253
2254 /*
2255 * As per USB compliance update, a device that is actively drawing
2256 * more than 100mA from USB must report itself as bus-powered in
2257 * the GetStatus(DEVICE) call.
2258 */
2259 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2260 usb_gadget_set_selfpowered(gadget);
2261
2262 /* interface and string IDs start at zero via kzalloc.
2263 * we force endpoints to start unassigned; few controller
2264 * drivers will zero ep->driver_data.
2265 */
2266 usb_ep_autoconfig_reset(gadget);
2267 return 0;
2268fail_dev:
2269 kfree(cdev->req->buf);
2270fail:
2271 usb_ep_free_request(gadget->ep0, cdev->req);
2272 cdev->req = NULL;
2273 return ret;
2274}
2275
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002276int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2277 struct usb_ep *ep0)
2278{
2279 int ret = 0;
2280
2281 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2282 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002283 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002284 goto end;
2285 }
2286
Chris Dickens5d6ae4f2017-12-31 18:59:42 -08002287 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2288 GFP_KERNEL);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002289 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002290 ret = -ENOMEM;
Christophe JAILLET990758c2017-01-04 06:30:16 +01002291 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002292 goto end;
2293 }
Felipe Balbi57943712014-09-18 09:54:54 -05002294 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002295 cdev->os_desc_req->complete = composite_setup_complete;
2296end:
2297 return ret;
2298}
2299
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002300void composite_dev_cleanup(struct usb_composite_dev *cdev)
2301{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002302 struct usb_gadget_string_container *uc, *tmp;
Benjamin Herrenschmidtaaeab022018-03-23 13:44:06 +11002303 struct usb_ep *ep, *tmp_ep;
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002304
2305 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2306 list_del(&uc->list);
2307 kfree(uc);
2308 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002309 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002310 if (cdev->os_desc_pending)
2311 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2312
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002313 kfree(cdev->os_desc_req->buf);
Hemant Kumar7f2b6d62016-05-04 18:22:14 -07002314 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002315 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumar7f2b6d62016-05-04 18:22:14 -07002316 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002317 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002318 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002319 if (cdev->setup_pending)
2320 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2321
Li Junbe0a8882014-08-28 21:44:11 +08002322 kfree(cdev->req->buf);
Hemant Kumar7f2b6d62016-05-04 18:22:14 -07002323 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002324 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumar7f2b6d62016-05-04 18:22:14 -07002325 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002326 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002327 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002328 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Benjamin Herrenschmidtaaeab022018-03-23 13:44:06 +11002329
2330 /*
2331 * Some UDC backends have a dynamic EP allocation scheme.
2332 *
2333 * In that case, the dispose() callback is used to notify the
2334 * backend that the EPs are no longer in use.
2335 *
2336 * Note: The UDC backend can remove the EP from the ep_list as
2337 * a result, so we need to use the _safe list iterator.
2338 */
2339 list_for_each_entry_safe(ep, tmp_ep,
2340 &cdev->gadget->ep_list, ep_list) {
2341 if (ep->ops->dispose)
2342 ep->ops->dispose(ep);
2343 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002344}
2345
2346static int composite_bind(struct usb_gadget *gadget,
2347 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002348{
2349 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002350 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002351 int status = -ENOMEM;
2352
2353 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2354 if (!cdev)
2355 return status;
2356
2357 spin_lock_init(&cdev->lock);
2358 cdev->gadget = gadget;
2359 set_gadget_data(gadget, cdev);
2360 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002361 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002362
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002363 status = composite_dev_prepare(composite, cdev);
2364 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002365 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002366
2367 /* composite gadget needs to assign strings for whole device (like
2368 * serial number), register function drivers, potentially update
2369 * power state and consumption, etc
2370 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002371 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002372 if (status < 0)
2373 goto fail;
2374
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002375 if (cdev->use_os_string) {
2376 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2377 if (status)
2378 goto fail;
2379 }
2380
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002381 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002382
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002383 /* has userspace failed to provide a serial number? */
2384 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2385 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2386
David Brownell40982be2008-06-19 17:52:58 -07002387 INFO(cdev, "%s ready\n", composite->name);
2388 return 0;
2389
2390fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002391 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002392 return status;
2393}
2394
2395/*-------------------------------------------------------------------------*/
2396
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002397void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002398{
2399 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2400 struct usb_function *f;
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002401 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002402
David Brownell89429392009-03-19 14:14:17 -07002403 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002404 * suspend/resume callbacks?
2405 */
2406 DBG(cdev, "suspend\n");
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002407 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002408 if (cdev->config) {
2409 list_for_each_entry(f, &cdev->config->functions, list) {
2410 if (f->suspend)
2411 f->suspend(f);
2412 }
2413 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002414 if (cdev->driver->suspend)
2415 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002416
2417 cdev->suspended = 1;
Mayank Rana19d6c4d2015-10-05 19:08:47 -07002418 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002419
2420 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002421}
2422
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002423void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002424{
2425 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2426 struct usb_function *f;
Hemant Kumar37cf30d2018-11-14 14:44:29 -08002427 unsigned int maxpower;
Danny Segald6c40da2016-12-06 15:35:24 -08002428 int ret;
2429 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002430
David Brownell89429392009-03-19 14:14:17 -07002431 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002432 * suspend/resume callbacks?
2433 */
2434 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002435 if (cdev->driver->resume)
2436 cdev->driver->resume(cdev);
Danny Segald6c40da2016-12-06 15:35:24 -08002437
2438 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002439 if (cdev->config) {
2440 list_for_each_entry(f, &cdev->config->functions, list) {
Danny Segald6c40da2016-12-06 15:35:24 -08002441 ret = usb_func_wakeup_int(f);
2442 if (ret) {
2443 if (ret == -EAGAIN) {
2444 ERROR(f->config->cdev,
2445 "Function wakeup for %s could not complete due to suspend state.\n",
2446 f->name ? f->name : "");
2447 break;
2448 } else if (ret != -ENOTSUPP) {
2449 ERROR(f->config->cdev,
2450 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2451 f->name ? f->name : "",
2452 ret);
2453 }
2454 }
2455
David Brownell40982be2008-06-19 17:52:58 -07002456 if (f->resume)
2457 f->resume(f);
2458 }
Hao Wub23f2f92010-11-29 15:17:03 +08002459
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002460 maxpower = cdev->config->MaxPower;
Hemant Kumar37cf30d2018-11-14 14:44:29 -08002461 maxpower = maxpower ? maxpower : CONFIG_USB_GADGET_VBUS_DRAW;
2462 if (gadget->speed < USB_SPEED_SUPER)
2463 maxpower = min(maxpower, 500U);
2464 usb_gadget_vbus_draw(gadget, maxpower);
David Brownell40982be2008-06-19 17:52:58 -07002465 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002466
Danny Segald6c40da2016-12-06 15:35:24 -08002467 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002468 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002469}
2470
2471/*-------------------------------------------------------------------------*/
2472
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002473static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002474 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002475 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002476
2477 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002478 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002479 .disconnect = composite_disconnect,
2480
2481 .suspend = composite_suspend,
2482 .resume = composite_resume,
2483
2484 .driver = {
2485 .owner = THIS_MODULE,
2486 },
2487};
2488
2489/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002490 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002491 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002492 *
David Brownell40982be2008-06-19 17:52:58 -07002493 * Context: single threaded during gadget setup
2494 *
2495 * This function is used to register drivers using the composite driver
2496 * framework. The return value is zero, or a negative errno value.
2497 * Those values normally come from the driver's @bind method, which does
2498 * all the work of setting up the driver to match the hardware.
2499 *
2500 * On successful return, the gadget is ready to respond to requests from
2501 * the host, unless one of its components invokes usb_gadget_disconnect()
2502 * while it was binding. That would usually be done in order to wait for
2503 * some userspace participation.
2504 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002505int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002506{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002507 struct usb_gadget_driver *gadget_driver;
2508
2509 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002510 return -EINVAL;
2511
2512 if (!driver->name)
2513 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002514
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002515 driver->gadget_driver = composite_driver_template;
2516 gadget_driver = &driver->gadget_driver;
2517
2518 gadget_driver->function = (char *) driver->name;
2519 gadget_driver->driver.name = driver->name;
2520 gadget_driver->max_speed = driver->max_speed;
2521
2522 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002523}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002524EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002525
2526/**
2527 * usb_composite_unregister() - unregister a composite driver
2528 * @driver: the driver to unregister
2529 *
2530 * This function is used to unregister drivers using the composite
2531 * driver framework.
2532 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002533void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002534{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002535 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002536}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002537EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002538
2539/**
2540 * usb_composite_setup_continue() - Continue with the control transfer
2541 * @cdev: the composite device who's control transfer was kept waiting
2542 *
2543 * This function must be called by the USB function driver to continue
2544 * with the control transfer's data/status stage in case it had requested to
2545 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2546 * can request the composite framework to delay the setup request's data/status
2547 * stages by returning USB_GADGET_DELAYED_STATUS.
2548 */
2549void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2550{
2551 int value;
2552 struct usb_request *req = cdev->req;
2553 unsigned long flags;
2554
2555 DBG(cdev, "%s\n", __func__);
2556 spin_lock_irqsave(&cdev->lock, flags);
2557
2558 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverud457f332017-10-31 11:33:23 +05302559 if (!cdev->config) {
2560 spin_unlock_irqrestore(&cdev->lock, flags);
2561 return;
2562 }
2563 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002564 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverud457f332017-10-31 11:33:23 +05302565 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002566
2567 } else if (--cdev->delayed_status == 0) {
2568 DBG(cdev, "%s: Completing delayed status\n", __func__);
2569 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002570 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002571 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002572 if (value < 0) {
2573 DBG(cdev, "ep_queue --> %d\n", value);
2574 req->status = 0;
2575 composite_setup_complete(cdev->gadget->ep0, req);
2576 }
2577 }
2578
2579 spin_unlock_irqrestore(&cdev->lock, flags);
2580}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002581EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002582
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002583static char *composite_default_mfr(struct usb_gadget *gadget)
2584{
Juergen Gross5002c932016-10-10 12:48:36 +02002585 return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
2586 init_utsname()->release, gadget->name);
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002587}
2588
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002589void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2590 struct usb_composite_overwrite *covr)
2591{
2592 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002593 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2594 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002595
2596 if (covr->idVendor)
2597 desc->idVendor = cpu_to_le16(covr->idVendor);
2598
2599 if (covr->idProduct)
2600 desc->idProduct = cpu_to_le16(covr->idProduct);
2601
2602 if (covr->bcdDevice)
2603 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002604
2605 if (covr->serial_number) {
2606 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2607 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2608 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002609 if (covr->manufacturer) {
2610 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2611 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002612
2613 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2614 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2615 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2616 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002617 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002618
2619 if (covr->product) {
2620 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2621 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2622 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002623}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002624EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002625
2626MODULE_LICENSE("GPL");
2627MODULE_AUTHOR("David Brownell");