blob: c880bd8f133cf44c94585d947e45851e08e1cf9b [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070020
21#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080022#include <linux/usb/otg.h>
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +053023#include <linux/usb/msm_hsusb.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030024#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070025
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020026#include "u_os_desc.h"
Mayank Ranab6cadaa2015-10-15 17:51:32 -070027#define SSUSB_GADGET_VBUS_DRAW 900 /* in mA */
28#define SSUSB_GADGET_VBUS_DRAW_UNITS 8
29#define HSUSB_GADGET_VBUS_DRAW_UNITS 2
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020030
Hemant Kumar7e115fd2017-07-19 12:08:16 -070031/* disable LPM by default */
Hemant Kumarc41d680a2018-02-14 16:37:37 -080032static bool disable_l1_for_hs;
Hemant Kumar7e115fd2017-07-19 12:08:16 -070033module_param(disable_l1_for_hs, bool, 0644);
34MODULE_PARM_DESC(disable_l1_for_hs,
35 "Disable support for L1 LPM for HS devices");
36
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020037/**
38 * struct usb_os_string - represents OS String to be reported by a gadget
39 * @bLength: total length of the entire descritor, always 0x12
40 * @bDescriptorType: USB_DT_STRING
41 * @qwSignature: the OS String proper
42 * @bMS_VendorCode: code used by the host for subsequent requests
43 * @bPad: not used, must be zero
44 */
45struct usb_os_string {
46 __u8 bLength;
47 __u8 bDescriptorType;
48 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
49 __u8 bMS_VendorCode;
50 __u8 bPad;
51} __packed;
52
David Brownell40982be2008-06-19 17:52:58 -070053/*
54 * The code in this file is utility code, used to build a gadget driver
55 * from one or more "function" drivers, one or more "configuration"
56 * objects, and a "usb_composite_driver" by gluing them together along
57 * with the relevant device-wide data.
58 */
59
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010060static struct usb_gadget_strings **get_containers_gs(
61 struct usb_gadget_string_container *uc)
62{
63 return (struct usb_gadget_strings **)uc->stash;
64}
65
Tatyana Brokhman48767a42011-06-28 16:33:49 +030066/**
John Younf3bdbe32016-02-05 17:07:03 -080067 * function_descriptors() - get function descriptors for speed
68 * @f: the function
69 * @speed: the speed
70 *
71 * Returns the descriptors or NULL if not set.
72 */
73static struct usb_descriptor_header **
74function_descriptors(struct usb_function *f,
75 enum usb_device_speed speed)
76{
77 struct usb_descriptor_header **descriptors;
78
Felipe Balbi08782632016-04-22 14:53:47 +030079 /*
80 * NOTE: we try to help gadget drivers which might not be setting
81 * max_speed appropriately.
82 */
83
John Younf3bdbe32016-02-05 17:07:03 -080084 switch (speed) {
85 case USB_SPEED_SUPER_PLUS:
86 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030087 if (descriptors)
88 break;
89 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080090 case USB_SPEED_SUPER:
91 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030092 if (descriptors)
93 break;
94 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080095 case USB_SPEED_HIGH:
96 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030097 if (descriptors)
98 break;
99 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800100 default:
101 descriptors = f->fs_descriptors;
102 }
103
Felipe Balbi08782632016-04-22 14:53:47 +0300104 /*
105 * if we can't find any descriptors at all, then this gadget deserves to
106 * Oops with a NULL pointer dereference
107 */
108
John Younf3bdbe32016-02-05 17:07:03 -0800109 return descriptors;
110}
111
112/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300113 * next_ep_desc() - advance to the next EP descriptor
114 * @t: currect pointer within descriptor array
115 *
116 * Return: next EP descriptor or NULL
117 *
118 * Iterate over @t until either EP descriptor found or
119 * NULL (that indicates end of list) encountered
120 */
121static struct usb_descriptor_header**
122next_ep_desc(struct usb_descriptor_header **t)
123{
124 for (; *t; t++) {
125 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
126 return t;
127 }
128 return NULL;
129}
130
131/*
132 * for_each_ep_desc()- iterate over endpoint descriptors in the
133 * descriptors list
134 * @start: pointer within descriptor array.
135 * @ep_desc: endpoint descriptor to use as the loop cursor
136 */
137#define for_each_ep_desc(start, ep_desc) \
138 for (ep_desc = next_ep_desc(start); \
139 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
140
141/**
142 * config_ep_by_speed() - configures the given endpoint
143 * according to gadget speed.
144 * @g: pointer to the gadget
145 * @f: usb function
146 * @_ep: the endpoint to configure
147 *
148 * Return: error code, 0 on success
149 *
150 * This function chooses the right descriptors for a given
151 * endpoint according to gadget speed and saves it in the
152 * endpoint desc field. If the endpoint already has a descriptor
153 * assigned to it - overwrites it with currently corresponding
154 * descriptor. The endpoint maxpacket field is updated according
155 * to the chosen descriptor.
156 * Note: the supplied function should hold all the descriptors
157 * for supported speeds
158 */
159int config_ep_by_speed(struct usb_gadget *g,
160 struct usb_function *f,
161 struct usb_ep *_ep)
162{
163 struct usb_endpoint_descriptor *chosen_desc = NULL;
164 struct usb_descriptor_header **speed_desc = NULL;
165
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300166 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
167 int want_comp_desc = 0;
168
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300169 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
170
171 if (!g || !f || !_ep)
172 return -EIO;
173
174 /* select desired speed */
175 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800176 case USB_SPEED_SUPER_PLUS:
177 if (gadget_is_superspeed_plus(g)) {
178 speed_desc = f->ssp_descriptors;
179 want_comp_desc = 1;
180 break;
181 }
182 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300183 case USB_SPEED_SUPER:
184 if (gadget_is_superspeed(g)) {
185 speed_desc = f->ss_descriptors;
186 want_comp_desc = 1;
187 break;
188 }
189 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300190 case USB_SPEED_HIGH:
191 if (gadget_is_dualspeed(g)) {
192 speed_desc = f->hs_descriptors;
193 break;
194 }
195 /* else: fall through */
196 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200197 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300198 }
199 /* find descriptors */
200 for_each_ep_desc(speed_desc, d_spd) {
201 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
202 if (chosen_desc->bEndpointAddress == _ep->address)
203 goto ep_found;
204 }
205 return -EIO;
206
207ep_found:
208 /* commit results */
Felipe Balbie0aa5ec2016-09-28 10:38:11 +0300209 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300210 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300211 _ep->comp_desc = NULL;
212 _ep->maxburst = 0;
Felipe Balbi3999c532016-09-28 12:33:31 +0300213 _ep->mult = 1;
214
215 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
216 usb_endpoint_xfer_int(_ep->desc)))
217 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
218
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300219 if (!want_comp_desc)
220 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300221
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300222 /*
223 * Companion descriptor should follow EP descriptor
224 * USB 3.0 spec, #9.6.7
225 */
226 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
227 if (!comp_desc ||
228 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
229 return -EIO;
230 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800231 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300232 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300233 case USB_ENDPOINT_XFER_ISOC:
234 /* mult: bits 1:0 of bmAttributes */
Felipe Balbi3999c532016-09-28 12:33:31 +0300235 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800236 case USB_ENDPOINT_XFER_BULK:
237 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300238 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300239 break;
240 default:
Colin Ian King0e216b02017-11-14 16:18:28 +0000241 if (comp_desc->bMaxBurst != 0) {
242 struct usb_composite_dev *cdev;
243
244 cdev = get_gadget_data(g);
Felipe Balbib785ea72012-06-06 10:20:23 +0300245 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
Colin Ian King0e216b02017-11-14 16:18:28 +0000246 }
Felipe Balbib785ea72012-06-06 10:20:23 +0300247 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300248 break;
249 }
250 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300251 return 0;
252}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200253EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700254
255/**
256 * usb_add_function() - add a function to a configuration
257 * @config: the configuration
258 * @function: the function being added
259 * Context: single threaded during gadget setup
260 *
261 * After initialization, each configuration must have one or more
262 * functions added to it. Adding a function involves calling its @bind()
263 * method to allocate resources such as interface and string identifiers
264 * and endpoints.
265 *
266 * This function returns the value of the function's bind(), which is
267 * zero for success else a negative errno value.
268 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200269int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700270 struct usb_function *function)
271{
272 int value = -EINVAL;
273
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530274 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700275 function->name, function,
276 config->label, config);
277
278 if (!function->set_alt || !function->disable)
279 goto done;
280
281 function->config = config;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700282 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700283 list_add_tail(&function->list, &config->functions);
284
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200285 if (function->bind_deactivated) {
286 value = usb_function_deactivate(function);
287 if (value)
288 goto done;
289 }
290
David Brownell40982be2008-06-19 17:52:58 -0700291 /* REVISIT *require* function->bind? */
292 if (function->bind) {
293 value = function->bind(config, function);
294 if (value < 0) {
295 list_del(&function->list);
296 function->config = NULL;
297 }
298 } else
299 value = 0;
300
301 /* We allow configurations that don't work at both speeds.
302 * If we run into a lowspeed Linux system, treat it the same
303 * as full speed ... it's the function drivers that will need
304 * to avoid bulk and ISO transfers.
305 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200306 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700307 config->fullspeed = true;
308 if (!config->highspeed && function->hs_descriptors)
309 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300310 if (!config->superspeed && function->ss_descriptors)
311 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800312 if (!config->superspeed_plus && function->ssp_descriptors)
313 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700314
315done:
316 if (value)
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530317 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700318 function->name, function, value);
319 return value;
320}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200321EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700322
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100323void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
324{
325 if (f->disable)
326 f->disable(f);
327
328 bitmap_zero(f->endpoints, 32);
329 list_del(&f->list);
330 if (f->unbind)
331 f->unbind(c, f);
332}
333EXPORT_SYMBOL_GPL(usb_remove_function);
334
David Brownell40982be2008-06-19 17:52:58 -0700335/**
David Brownell60beed92008-08-18 17:38:22 -0700336 * usb_function_deactivate - prevent function and gadget enumeration
337 * @function: the function that isn't yet ready to respond
338 *
339 * Blocks response of the gadget driver to host enumeration by
340 * preventing the data line pullup from being activated. This is
341 * normally called during @bind() processing to change from the
342 * initial "ready to respond" state, or when a required resource
343 * becomes available.
344 *
345 * For example, drivers that serve as a passthrough to a userspace
346 * daemon can block enumeration unless that daemon (such as an OBEX,
347 * MTP, or print server) is ready to handle host requests.
348 *
349 * Not all systems support software control of their USB peripheral
350 * data pullups.
351 *
352 * Returns zero on success, else negative errno.
353 */
354int usb_function_deactivate(struct usb_function *function)
355{
356 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200357 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700358 int status = 0;
359
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200360 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700361
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530362 if (cdev->deactivations == 0) {
363 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200364 status = usb_gadget_deactivate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530365 spin_lock_irqsave(&cdev->lock, flags);
366 }
David Brownell60beed92008-08-18 17:38:22 -0700367 if (status == 0)
368 cdev->deactivations++;
369
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200370 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700371 return status;
372}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200373EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700374
375/**
376 * usb_function_activate - allow function and gadget enumeration
377 * @function: function on which usb_function_activate() was called
378 *
379 * Reverses effect of usb_function_deactivate(). If no more functions
380 * are delaying their activation, the gadget driver will respond to
381 * host enumeration procedures.
382 *
383 * Returns zero on success, else negative errno.
384 */
385int usb_function_activate(struct usb_function *function)
386{
387 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200388 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700389 int status = 0;
390
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200391 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700392
393 if (WARN_ON(cdev->deactivations == 0))
394 status = -EINVAL;
395 else {
396 cdev->deactivations--;
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530397 if (cdev->deactivations == 0) {
398 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200399 status = usb_gadget_activate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530400 spin_lock_irqsave(&cdev->lock, flags);
401 }
David Brownell60beed92008-08-18 17:38:22 -0700402 }
403
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200404 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700405 return status;
406}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200407EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700408
409/**
David Brownell40982be2008-06-19 17:52:58 -0700410 * usb_interface_id() - allocate an unused interface ID
411 * @config: configuration associated with the interface
412 * @function: function handling the interface
413 * Context: single threaded during gadget setup
414 *
415 * usb_interface_id() is called from usb_function.bind() callbacks to
416 * allocate new interface IDs. The function driver will then store that
417 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300418 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700419 * particularly changing its altsetting via set_alt(). There may
420 * also be class-specific or vendor-specific requests to handle.
421 *
422 * All interface identifier should be allocated using this routine, to
423 * ensure that for example different functions don't wrongly assign
424 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300425 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700426 * one configuration (or more than once in a given configuration) need
427 * multiple versions of the relevant descriptors.
428 *
429 * Returns the interface ID which was allocated; or -ENODEV if no
430 * more interface IDs can be allocated.
431 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200432int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700433 struct usb_function *function)
434{
435 unsigned id = config->next_interface_id;
436
437 if (id < MAX_CONFIG_INTERFACES) {
438 config->interface[id] = function;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700439 if (function->intf_id < 0)
440 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700441 config->next_interface_id = id + 1;
442 return id;
443 }
444 return -ENODEV;
445}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200446EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700447
Mayank Ranab92dfd02014-11-25 15:29:58 -0800448static int usb_func_wakeup_int(struct usb_function *func)
Danny Segalf83e4512016-12-06 15:35:24 -0800449{
450 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800451 struct usb_gadget *gadget;
452
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800453 pr_debug("%s - %s function wakeup\n",
454 __func__, func->name ? func->name : "");
Danny Segalf83e4512016-12-06 15:35:24 -0800455
456 if (!func || !func->config || !func->config->cdev ||
457 !func->config->cdev->gadget)
458 return -EINVAL;
459
460 gadget = func->config->cdev->gadget;
461 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
462 DBG(func->config->cdev,
463 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
464 gadget->speed,
465 func->func_wakeup_allowed);
466
467 return -ENOTSUPP;
468 }
469
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800470 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300471
472 return ret;
473}
474
475int usb_func_wakeup(struct usb_function *func)
476{
477 int ret;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530478 unsigned long flags;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300479
480 pr_debug("%s function wakeup\n",
481 func->name ? func->name : "");
482
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530483 spin_lock_irqsave(&func->config->cdev->lock, flags);
Mayank Ranab92dfd02014-11-25 15:29:58 -0800484 ret = usb_func_wakeup_int(func);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530485 if (ret == -EACCES) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300486 DBG(func->config->cdev,
487 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
488 func->name ? func->name : "");
Danny Segalde7cd8d2014-07-28 18:08:33 +0300489 ret = 0;
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530490 func->func_wakeup_pending = 1;
491 } else if (ret == -EAGAIN) {
492 DBG(func->config->cdev,
493 "Function wakeup for %s sent.\n",
494 func->name ? func->name : "");
495 ret = 0;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +0530496 } else if (ret < 0 && ret != -ENOTSUPP) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300497 ERROR(func->config->cdev,
498 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
499 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800500 }
501
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530502 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300503 return ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800504}
505EXPORT_SYMBOL(usb_func_wakeup);
506
507int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
508 struct usb_request *req, gfp_t gfp_flags)
509{
wHemant Kumare9e13922016-12-19 14:41:28 -0800510 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800511 struct usb_gadget *gadget;
512
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800513 if (!func || !func->config || !func->config->cdev ||
wHemant Kumare9e13922016-12-19 14:41:28 -0800514 !func->config->cdev->gadget || !ep || !req) {
515 ret = -EINVAL;
516 goto done;
517 }
Danny Segalf83e4512016-12-06 15:35:24 -0800518
519 pr_debug("Function %s queueing new data into ep %u\n",
520 func->name ? func->name : "", ep->address);
521
522 gadget = func->config->cdev->gadget;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800523 if (func->func_is_suspended && func->func_wakeup_allowed) {
524 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530525 if (ret == -EACCES) {
526 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
527 func->name ? func->name : "");
528 func->func_wakeup_pending = 1;
529 ret = -EAGAIN;
530 } else if (ret == -EAGAIN) {
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800531 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
532 func->name ? func->name : "");
533 } else if (ret < 0 && ret != -ENOTSUPP) {
534 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
535 func->name ? func->name : "", ret);
Chandana Kishori Chiluveru712cfc02018-07-27 17:25:09 +0530536 } else {
537 /*
538 * Return -EAGAIN to queue the request from
539 * function driver wakeup function.
540 */
541 ret = -EAGAIN;
542 goto done;
Danny Segalf83e4512016-12-06 15:35:24 -0800543 }
544 }
545
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800546 if (!func->func_is_suspended)
547 ret = 0;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800548
wHemant Kumare9e13922016-12-19 14:41:28 -0800549 if (func->func_is_suspended && !func->func_wakeup_allowed) {
550 ret = -ENOTSUPP;
551 goto done;
552 }
553
554 ret = usb_ep_queue(ep, req, gfp_flags);
555done:
Danny Segalf83e4512016-12-06 15:35:24 -0800556 return ret;
557}
558EXPORT_SYMBOL(usb_func_ep_queue);
559
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100560static u8 encode_bMaxPower(enum usb_device_speed speed,
561 struct usb_configuration *c)
562{
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700563 unsigned int val = CONFIG_USB_GADGET_VBUS_DRAW;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100564
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100565 switch (speed) {
566 case USB_SPEED_SUPER:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700567 /* with super-speed report 900mA */
568 val = SSUSB_GADGET_VBUS_DRAW;
569 return (u8)(val / SSUSB_GADGET_VBUS_DRAW_UNITS);
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100570 default:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700571 return DIV_ROUND_UP(val, HSUSB_GADGET_VBUS_DRAW_UNITS);
Joe Perches2b84f922013-10-08 16:01:37 -0700572 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100573}
574
David Brownell40982be2008-06-19 17:52:58 -0700575static int config_buf(struct usb_configuration *config,
576 enum usb_device_speed speed, void *buf, u8 type)
577{
578 struct usb_config_descriptor *c = buf;
579 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200580 int len;
David Brownell40982be2008-06-19 17:52:58 -0700581 struct usb_function *f;
582 int status;
583
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200584 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700585 /* write the config descriptor */
586 c = buf;
587 c->bLength = USB_DT_CONFIG_SIZE;
588 c->bDescriptorType = type;
589 /* wTotalLength is written later */
590 c->bNumInterfaces = config->next_interface_id;
591 c->bConfigurationValue = config->bConfigurationValue;
592 c->iConfiguration = config->iConfiguration;
593 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100594 c->bMaxPower = encode_bMaxPower(speed, config);
Vijayavardhan Vennapusadaeb5ac2018-07-13 13:04:17 +0530595 if (config->cdev->gadget->self_powered) {
596 c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER;
597 c->bMaxPower = 0;
598 }
David Brownell40982be2008-06-19 17:52:58 -0700599
600 /* There may be e.g. OTG descriptors */
601 if (config->descriptors) {
602 status = usb_descriptor_fillbuf(next, len,
603 config->descriptors);
604 if (status < 0)
605 return status;
606 len -= status;
607 next += status;
608 }
609
610 /* add each function's descriptors */
611 list_for_each_entry(f, &config->functions, list) {
612 struct usb_descriptor_header **descriptors;
613
John Younf3bdbe32016-02-05 17:07:03 -0800614 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700615 if (!descriptors)
616 continue;
617 status = usb_descriptor_fillbuf(next, len,
618 (const struct usb_descriptor_header **) descriptors);
619 if (status < 0)
620 return status;
621 len -= status;
622 next += status;
623 }
624
625 len = next - buf;
626 c->wTotalLength = cpu_to_le16(len);
627 return len;
628}
629
630static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
631{
632 struct usb_gadget *gadget = cdev->gadget;
633 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200634 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700635 u8 type = w_value >> 8;
636 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
637
John Youneae58202016-02-05 17:07:17 -0800638 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300639 speed = gadget->speed;
640 else if (gadget_is_dualspeed(gadget)) {
641 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700642 if (gadget->speed == USB_SPEED_HIGH)
643 hs = 1;
644 if (type == USB_DT_OTHER_SPEED_CONFIG)
645 hs = !hs;
646 if (hs)
647 speed = USB_SPEED_HIGH;
648
649 }
650
651 /* This is a lookup by config *INDEX* */
652 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200653
654 pos = &cdev->configs;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200655
656 while ((pos = pos->next) != &cdev->configs) {
657 c = list_entry(pos, typeof(*c), list);
658
David Brownell40982be2008-06-19 17:52:58 -0700659 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300660 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800661 case USB_SPEED_SUPER_PLUS:
662 if (!c->superspeed_plus)
663 continue;
664 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300665 case USB_SPEED_SUPER:
666 if (!c->superspeed)
667 continue;
668 break;
669 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700670 if (!c->highspeed)
671 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300672 break;
673 default:
David Brownell40982be2008-06-19 17:52:58 -0700674 if (!c->fullspeed)
675 continue;
676 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300677
David Brownell40982be2008-06-19 17:52:58 -0700678 if (w_value == 0)
679 return config_buf(c, speed, cdev->req->buf, type);
680 w_value--;
681 }
682 return -EINVAL;
683}
684
685static int count_configs(struct usb_composite_dev *cdev, unsigned type)
686{
687 struct usb_gadget *gadget = cdev->gadget;
688 struct usb_configuration *c;
689 unsigned count = 0;
690 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300691 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800692 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700693
694 if (gadget_is_dualspeed(gadget)) {
695 if (gadget->speed == USB_SPEED_HIGH)
696 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300697 if (gadget->speed == USB_SPEED_SUPER)
698 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800699 if (gadget->speed == USB_SPEED_SUPER_PLUS)
700 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700701 if (type == USB_DT_DEVICE_QUALIFIER)
702 hs = !hs;
703 }
704 list_for_each_entry(c, &cdev->configs, list) {
705 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800706 if (ssp) {
707 if (!c->superspeed_plus)
708 continue;
709 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300710 if (!c->superspeed)
711 continue;
712 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700713 if (!c->highspeed)
714 continue;
715 } else {
716 if (!c->fullspeed)
717 continue;
718 }
719 count++;
720 }
721 return count;
722}
723
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300724/**
725 * bos_desc() - prepares the BOS descriptor.
726 * @cdev: pointer to usb_composite device to generate the bos
727 * descriptor for
728 *
729 * This function generates the BOS (Binary Device Object)
730 * descriptor and its device capabilities descriptors. The BOS
731 * descriptor should be supported by a SuperSpeed device.
732 */
733static int bos_desc(struct usb_composite_dev *cdev)
734{
735 struct usb_ext_cap_descriptor *usb_ext;
736 struct usb_ss_cap_descriptor *ss_cap;
737 struct usb_dcd_config_params dcd_config_params;
738 struct usb_bos_descriptor *bos = cdev->req->buf;
739
740 bos->bLength = USB_DT_BOS_SIZE;
741 bos->bDescriptorType = USB_DT_BOS;
742
743 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
744 bos->bNumDeviceCaps = 0;
745
746 /*
747 * A SuperSpeed device shall include the USB2.0 extension descriptor
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800748 * and shall support LPM when operating in USB2.0 HS mode, as well as
749 * a HS device when operating in USB2.1 HS mode.
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300750 */
751 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
752 bos->bNumDeviceCaps++;
753 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
754 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
755 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
756 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500757 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300758
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800759 if (gadget_is_superspeed(cdev->gadget)) {
760 /*
761 * The Superspeed USB Capability descriptor shall be
762 * implemented by all SuperSpeed devices.
763 */
764 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
765 bos->bNumDeviceCaps++;
766 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
767 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
768 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
769 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
770 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
771 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
772 USB_FULL_SPEED_OPERATION |
773 USB_HIGH_SPEED_OPERATION |
774 USB_5GBPS_OPERATION);
775 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300776
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800777 /* Get Controller configuration */
778 if (cdev->gadget->ops->get_config_params)
779 cdev->gadget->ops->get_config_params
780 (&dcd_config_params);
781 else {
782 dcd_config_params.bU1devExitLat =
783 USB_DEFAULT_U1_DEV_EXIT_LAT;
784 dcd_config_params.bU2DevExitLat =
785 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
786 }
787 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
788 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300789 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300790
John Younf228a8d2016-02-05 17:05:53 -0800791 /* The SuperSpeedPlus USB Device Capability descriptor */
792 if (gadget_is_superspeed_plus(cdev->gadget)) {
793 struct usb_ssp_cap_descriptor *ssp_cap;
794
795 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
796 bos->bNumDeviceCaps++;
797
798 /*
799 * Report typical values.
800 */
801
802 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
803 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
804 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
805 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700806 ssp_cap->bReserved = 0;
807 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800808
809 /* SSAC = 1 (2 attributes) */
810 ssp_cap->bmAttributes = cpu_to_le32(1);
811
812 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700813 ssp_cap->wFunctionalitySupport =
814 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800815
816 /*
817 * bmSublinkSpeedAttr[0]:
818 * ST = Symmetric, RX
819 * LSE = 3 (Gbps)
820 * LP = 1 (SuperSpeedPlus)
821 * LSM = 10 (10 Gbps)
822 */
823 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700824 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800825 /*
826 * bmSublinkSpeedAttr[1] =
827 * ST = Symmetric, TX
828 * LSE = 3 (Gbps)
829 * LP = 1 (SuperSpeedPlus)
830 * LSM = 10 (10 Gbps)
831 */
832 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700833 cpu_to_le32((3 << 4) | (1 << 14) |
834 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800835 }
836
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300837 return le16_to_cpu(bos->wTotalLength);
838}
839
David Brownell40982be2008-06-19 17:52:58 -0700840static void device_qual(struct usb_composite_dev *cdev)
841{
842 struct usb_qualifier_descriptor *qual = cdev->req->buf;
843
844 qual->bLength = sizeof(*qual);
845 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
846 /* POLICY: same bcdUSB and device type info at both speeds */
847 qual->bcdUSB = cdev->desc.bcdUSB;
848 qual->bDeviceClass = cdev->desc.bDeviceClass;
849 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
850 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
851 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200852 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700853 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700854 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700855}
856
857/*-------------------------------------------------------------------------*/
858
859static void reset_config(struct usb_composite_dev *cdev)
860{
861 struct usb_function *f;
862
863 DBG(cdev, "reset config\n");
864
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +0530865 if (!cdev->config) {
866 pr_err("%s:cdev->config is already NULL\n", __func__);
867 return;
868 }
869
David Brownell40982be2008-06-19 17:52:58 -0700870 list_for_each_entry(f, &cdev->config->functions, list) {
871 if (f->disable)
872 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200873
Danny Segalf83e4512016-12-06 15:35:24 -0800874 /* USB 3.0 addition */
875 f->func_is_suspended = false;
876 f->func_wakeup_allowed = false;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300877 f->func_wakeup_pending = false;
Danny Segalf83e4512016-12-06 15:35:24 -0800878
Laurent Pinchart52426582009-10-21 00:03:38 +0200879 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700880 }
881 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100882 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700883}
884
885static int set_config(struct usb_composite_dev *cdev,
886 const struct usb_ctrlrequest *ctrl, unsigned number)
887{
888 struct usb_gadget *gadget = cdev->gadget;
889 struct usb_configuration *c = NULL;
890 int result = -EINVAL;
jianzhou21ae58e2020-03-24 11:07:44 +0800891 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
David Brownell40982be2008-06-19 17:52:58 -0700892 int tmp;
893
David Brownell40982be2008-06-19 17:52:58 -0700894 if (number) {
895 list_for_each_entry(c, &cdev->configs, list) {
896 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300897 /*
898 * We disable the FDs of the previous
899 * configuration only if the new configuration
900 * is a valid one
901 */
902 if (cdev->config)
903 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700904 result = 0;
905 break;
906 }
907 }
908 if (result < 0)
909 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300910 } else { /* Zero configuration value - need to reset the config */
911 if (cdev->config)
912 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700913 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300914 }
David Brownell40982be2008-06-19 17:52:58 -0700915
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200916 INFO(cdev, "%s config #%d: %s\n",
917 usb_speed_string(gadget->speed),
918 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700919
920 if (!c)
921 goto done;
922
Peter Chen6027f312014-04-29 13:26:28 +0800923 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700924 cdev->config = c;
Jack Phamdb943d62016-12-16 11:21:16 -0800925 c->num_ineps_used = 0;
926 c->num_outeps_used = 0;
David Brownell40982be2008-06-19 17:52:58 -0700927
928 /* Initialize all interfaces by setting them to altsetting zero. */
929 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
930 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200931 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700932
933 if (!f)
934 break;
935
Laurent Pinchart52426582009-10-21 00:03:38 +0200936 /*
937 * Record which endpoints are used by the function. This is used
938 * to dispatch control requests targeted at that endpoint to the
939 * function's setup callback instead of the current
940 * configuration's setup callback.
941 */
John Younf3bdbe32016-02-05 17:07:03 -0800942 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200943
944 for (; *descriptors; ++descriptors) {
945 struct usb_endpoint_descriptor *ep;
946 int addr;
947
948 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
949 continue;
950
951 ep = (struct usb_endpoint_descriptor *)*descriptors;
952 addr = ((ep->bEndpointAddress & 0x80) >> 3)
953 | (ep->bEndpointAddress & 0x0f);
954 set_bit(addr, f->endpoints);
Jack Phamdb943d62016-12-16 11:21:16 -0800955 if (usb_endpoint_dir_in(ep))
956 c->num_ineps_used++;
957 else
958 c->num_outeps_used++;
Laurent Pinchart52426582009-10-21 00:03:38 +0200959 }
960
David Brownell40982be2008-06-19 17:52:58 -0700961 result = f->set_alt(f, tmp, 0);
962 if (result < 0) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530963 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700964 tmp, f->name, f, result);
965
966 reset_config(cdev);
967 goto done;
968 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300969
970 if (result == USB_GADGET_DELAYED_STATUS) {
971 DBG(cdev,
972 "%s: interface %d (%s) requested delayed status\n",
973 __func__, tmp, f->name);
974 cdev->delayed_status++;
975 DBG(cdev, "delayed_status count %d\n",
976 cdev->delayed_status);
977 }
David Brownell40982be2008-06-19 17:52:58 -0700978 }
979
jianzhou21ae58e2020-03-24 11:07:44 +0800980 /* Allow 900mA to draw with Super-Speed */
981 if (gadget->speed == USB_SPEED_SUPER)
982 power = SSUSB_GADGET_VBUS_DRAW;
983 else
984 power = CONFIG_USB_GADGET_VBUS_DRAW;
985
David Brownell40982be2008-06-19 17:52:58 -0700986done:
jianzhou21ae58e2020-03-24 11:07:44 +0800987 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300988 if (result >= 0 && cdev->delayed_status)
989 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700990 return result;
991}
992
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100993int usb_add_config_only(struct usb_composite_dev *cdev,
994 struct usb_configuration *config)
995{
996 struct usb_configuration *c;
997
998 if (!config->bConfigurationValue)
999 return -EINVAL;
1000
1001 /* Prevent duplicate configuration identifiers */
1002 list_for_each_entry(c, &cdev->configs, list) {
1003 if (c->bConfigurationValue == config->bConfigurationValue)
1004 return -EBUSY;
1005 }
1006
1007 config->cdev = cdev;
1008 list_add_tail(&config->list, &cdev->configs);
1009
1010 INIT_LIST_HEAD(&config->functions);
1011 config->next_interface_id = 0;
1012 memset(config->interface, 0, sizeof(config->interface));
1013
1014 return 0;
1015}
1016EXPORT_SYMBOL_GPL(usb_add_config_only);
1017
David Brownell40982be2008-06-19 17:52:58 -07001018/**
1019 * usb_add_config() - add a configuration to a device.
1020 * @cdev: wraps the USB gadget
1021 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001022 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -07001023 * Context: single threaded during gadget setup
1024 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001025 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -07001026 * add each of the configurations it supports, using this routine.
1027 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001028 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -07001029 * is zero for success else a negative errno value. Binding configurations
1030 * assigns global resources including string IDs, and per-configuration
1031 * resources such as interface IDs and endpoints.
1032 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001033int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001034 struct usb_configuration *config,
1035 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001036{
1037 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001038
1039 if (!bind)
1040 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001041
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301042 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001043 config->bConfigurationValue,
1044 config->label, config);
1045
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001046 status = usb_add_config_only(cdev, config);
1047 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001048 goto done;
1049
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001050 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001051 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001052 while (!list_empty(&config->functions)) {
1053 struct usb_function *f;
1054
1055 f = list_first_entry(&config->functions,
1056 struct usb_function, list);
1057 list_del(&f->list);
1058 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301059 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001060 f->name, f);
1061 f->unbind(config, f);
1062 /* may free memory for "f" */
1063 }
1064 }
David Brownell40982be2008-06-19 17:52:58 -07001065 list_del(&config->list);
1066 config->cdev = NULL;
1067 } else {
1068 unsigned i;
1069
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301070 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001071 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001072 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001073 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001074 config->highspeed ? " high" : "",
1075 config->fullspeed
1076 ? (gadget_is_dualspeed(cdev->gadget)
1077 ? " full"
1078 : " full/low")
1079 : "");
1080
1081 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1082 struct usb_function *f = config->interface[i];
1083
1084 if (!f)
1085 continue;
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301086 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001087 i, f->name, f);
1088 }
1089 }
1090
Robert Baldygaf871cb92015-09-16 12:10:39 +02001091 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001092 usb_ep_autoconfig_reset(cdev->gadget);
1093
1094done:
1095 if (status)
1096 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1097 config->bConfigurationValue, status);
1098 return status;
1099}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001100EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001101
Benoit Goby51cce6f2012-05-10 10:07:57 +02001102static void remove_config(struct usb_composite_dev *cdev,
1103 struct usb_configuration *config)
1104{
1105 while (!list_empty(&config->functions)) {
1106 struct usb_function *f;
1107
1108 f = list_first_entry(&config->functions,
1109 struct usb_function, list);
1110 list_del(&f->list);
1111 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301112 DBG(cdev, "unbind function '%s'/%pK\n", f->name, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001113 f->unbind(config, f);
1114 /* may free memory for "f" */
1115 }
1116 }
1117 list_del(&config->list);
1118 if (config->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301119 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001120 config->unbind(config);
1121 /* may free memory for "c" */
1122 }
1123}
1124
1125/**
1126 * usb_remove_config() - remove a configuration from a device.
1127 * @cdev: wraps the USB gadget
1128 * @config: the configuration
1129 *
1130 * Drivers must call usb_gadget_disconnect before calling this function
1131 * to disconnect the device from the host and make sure the host will not
1132 * try to enumerate the device while we are changing the config list.
1133 */
1134void usb_remove_config(struct usb_composite_dev *cdev,
1135 struct usb_configuration *config)
1136{
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&cdev->lock, flags);
1140
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301141 if (cdev->config == config) {
1142 if (cdev->gadget->is_chipidea && !cdev->suspended) {
1143 spin_unlock_irqrestore(&cdev->lock, flags);
1144 msm_do_bam_disable_enable(CI_CTRL);
1145 spin_lock_irqsave(&cdev->lock, flags);
1146 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001147 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301148 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001149
1150 spin_unlock_irqrestore(&cdev->lock, flags);
1151
1152 remove_config(cdev, config);
1153}
1154
David Brownell40982be2008-06-19 17:52:58 -07001155/*-------------------------------------------------------------------------*/
1156
1157/* We support strings in multiple languages ... string descriptor zero
1158 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001159 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001160 * the host side.
1161 */
1162
1163static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1164{
1165 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001166 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001167 __le16 *tmp;
1168
1169 while (*sp) {
1170 s = *sp;
1171 language = cpu_to_le16(s->language);
1172 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1173 if (*tmp == language)
1174 goto repeat;
1175 }
1176 *tmp++ = language;
1177repeat:
1178 sp++;
1179 }
1180}
1181
1182static int lookup_string(
1183 struct usb_gadget_strings **sp,
1184 void *buf,
1185 u16 language,
1186 int id
1187)
1188{
1189 struct usb_gadget_strings *s;
1190 int value;
1191
1192 while (*sp) {
1193 s = *sp++;
1194 if (s->language != language)
1195 continue;
1196 value = usb_gadget_get_string(s, id, buf);
1197 if (value > 0)
1198 return value;
1199 }
1200 return -EINVAL;
1201}
1202
1203static int get_string(struct usb_composite_dev *cdev,
1204 void *buf, u16 language, int id)
1205{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001206 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001207 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001208 struct usb_configuration *c;
1209 struct usb_function *f;
1210 int len;
1211
Diego Violaad4676a2015-05-31 15:52:41 -03001212 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001213 * folk will ever care about ... also, it's all supported here.
1214 * (Except for UTF8 support for Unicode's "Astral Planes".)
1215 */
1216
1217 /* 0 == report all available language codes */
1218 if (id == 0) {
1219 struct usb_string_descriptor *s = buf;
1220 struct usb_gadget_strings **sp;
1221
1222 memset(s, 0, 256);
1223 s->bDescriptorType = USB_DT_STRING;
1224
1225 sp = composite->strings;
1226 if (sp)
1227 collect_langs(sp, s->wData);
1228
1229 list_for_each_entry(c, &cdev->configs, list) {
1230 sp = c->strings;
1231 if (sp)
1232 collect_langs(sp, s->wData);
1233
1234 list_for_each_entry(f, &c->functions, list) {
1235 sp = f->strings;
1236 if (sp)
1237 collect_langs(sp, s->wData);
1238 }
1239 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001240 list_for_each_entry(uc, &cdev->gstrings, list) {
1241 struct usb_gadget_strings **sp;
1242
1243 sp = get_containers_gs(uc);
1244 collect_langs(sp, s->wData);
1245 }
David Brownell40982be2008-06-19 17:52:58 -07001246
Roel Kluin417b57b2009-08-06 16:09:51 -07001247 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001248 continue;
1249 if (!len)
1250 return -EINVAL;
1251
1252 s->bLength = 2 * (len + 1);
1253 return s->bLength;
1254 }
1255
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001256 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1257 struct usb_os_string *b = buf;
1258 b->bLength = sizeof(*b);
1259 b->bDescriptorType = USB_DT_STRING;
1260 compiletime_assert(
1261 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1262 "qwSignature size must be equal to qw_sign");
1263 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1264 b->bMS_VendorCode = cdev->b_vendor_code;
1265 b->bPad = 0;
1266 return sizeof(*b);
1267 }
1268
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001269 list_for_each_entry(uc, &cdev->gstrings, list) {
1270 struct usb_gadget_strings **sp;
1271
1272 sp = get_containers_gs(uc);
1273 len = lookup_string(sp, buf, language, id);
1274 if (len > 0)
1275 return len;
1276 }
1277
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001278 /* String IDs are device-scoped, so we look up each string
1279 * table we're told about. These lookups are infrequent;
1280 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001281 */
1282 if (composite->strings) {
1283 len = lookup_string(composite->strings, buf, language, id);
1284 if (len > 0)
1285 return len;
1286 }
1287 list_for_each_entry(c, &cdev->configs, list) {
1288 if (c->strings) {
1289 len = lookup_string(c->strings, buf, language, id);
1290 if (len > 0)
1291 return len;
1292 }
1293 list_for_each_entry(f, &c->functions, list) {
1294 if (!f->strings)
1295 continue;
1296 len = lookup_string(f->strings, buf, language, id);
1297 if (len > 0)
1298 return len;
1299 }
1300 }
1301 return -EINVAL;
1302}
1303
1304/**
1305 * usb_string_id() - allocate an unused string ID
1306 * @cdev: the device whose string descriptor IDs are being allocated
1307 * Context: single threaded during gadget setup
1308 *
1309 * @usb_string_id() is called from bind() callbacks to allocate
1310 * string IDs. Drivers for functions, configurations, or gadgets will
1311 * then store that ID in the appropriate descriptors and string table.
1312 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001313 * All string identifier should be allocated using this,
1314 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1315 * that for example different functions don't wrongly assign different
1316 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001317 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001318int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001319{
1320 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001321 /* string id 0 is reserved by USB spec for list of
1322 * supported languages */
1323 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001324 cdev->next_string_id++;
1325 return cdev->next_string_id;
1326 }
1327 return -ENODEV;
1328}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001329EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001330
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001331/**
1332 * usb_string_ids() - allocate unused string IDs in batch
1333 * @cdev: the device whose string descriptor IDs are being allocated
1334 * @str: an array of usb_string objects to assign numbers to
1335 * Context: single threaded during gadget setup
1336 *
1337 * @usb_string_ids() is called from bind() callbacks to allocate
1338 * string IDs. Drivers for functions, configurations, or gadgets will
1339 * then copy IDs from the string table to the appropriate descriptors
1340 * and string table for other languages.
1341 *
1342 * All string identifier should be allocated using this,
1343 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1344 * example different functions don't wrongly assign different meanings
1345 * to the same identifier.
1346 */
1347int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1348{
1349 int next = cdev->next_string_id;
1350
1351 for (; str->s; ++str) {
1352 if (unlikely(next >= 254))
1353 return -ENODEV;
1354 str->id = ++next;
1355 }
1356
1357 cdev->next_string_id = next;
1358
1359 return 0;
1360}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001361EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001362
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001363static struct usb_gadget_string_container *copy_gadget_strings(
1364 struct usb_gadget_strings **sp, unsigned n_gstrings,
1365 unsigned n_strings)
1366{
1367 struct usb_gadget_string_container *uc;
1368 struct usb_gadget_strings **gs_array;
1369 struct usb_gadget_strings *gs;
1370 struct usb_string *s;
1371 unsigned mem;
1372 unsigned n_gs;
1373 unsigned n_s;
1374 void *stash;
1375
1376 mem = sizeof(*uc);
1377 mem += sizeof(void *) * (n_gstrings + 1);
1378 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1379 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1380 uc = kmalloc(mem, GFP_KERNEL);
1381 if (!uc)
1382 return ERR_PTR(-ENOMEM);
1383 gs_array = get_containers_gs(uc);
1384 stash = uc->stash;
1385 stash += sizeof(void *) * (n_gstrings + 1);
1386 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1387 struct usb_string *org_s;
1388
1389 gs_array[n_gs] = stash;
1390 gs = gs_array[n_gs];
1391 stash += sizeof(struct usb_gadget_strings);
1392 gs->language = sp[n_gs]->language;
1393 gs->strings = stash;
1394 org_s = sp[n_gs]->strings;
1395
1396 for (n_s = 0; n_s < n_strings; n_s++) {
1397 s = stash;
1398 stash += sizeof(struct usb_string);
1399 if (org_s->s)
1400 s->s = org_s->s;
1401 else
1402 s->s = "";
1403 org_s++;
1404 }
1405 s = stash;
1406 s->s = NULL;
1407 stash += sizeof(struct usb_string);
1408
1409 }
1410 gs_array[n_gs] = NULL;
1411 return uc;
1412}
1413
1414/**
1415 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1416 * @cdev: the device whose string descriptor IDs are being allocated
1417 * and attached.
1418 * @sp: an array of usb_gadget_strings to attach.
1419 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1420 *
1421 * This function will create a deep copy of usb_gadget_strings and usb_string
1422 * and attach it to the cdev. The actual string (usb_string.s) will not be
1423 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001424 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001425 * The ->language pointer of each struct usb_gadget_strings has to contain the
1426 * same amount of entries.
1427 * 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 +09001428 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001429 * entry of en-US. Therefore both entries become the same id assign.
1430 */
1431struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1432 struct usb_gadget_strings **sp, unsigned n_strings)
1433{
1434 struct usb_gadget_string_container *uc;
1435 struct usb_gadget_strings **n_gs;
1436 unsigned n_gstrings = 0;
1437 unsigned i;
1438 int ret;
1439
1440 for (i = 0; sp[i]; i++)
1441 n_gstrings++;
1442
1443 if (!n_gstrings)
1444 return ERR_PTR(-EINVAL);
1445
1446 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1447 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001448 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001449
1450 n_gs = get_containers_gs(uc);
1451 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1452 if (ret)
1453 goto err;
1454
1455 for (i = 1; i < n_gstrings; i++) {
1456 struct usb_string *m_s;
1457 struct usb_string *s;
1458 unsigned n;
1459
1460 m_s = n_gs[0]->strings;
1461 s = n_gs[i]->strings;
1462 for (n = 0; n < n_strings; n++) {
1463 s->id = m_s->id;
1464 s++;
1465 m_s++;
1466 }
1467 }
1468 list_add_tail(&uc->list, &cdev->gstrings);
1469 return n_gs[0]->strings;
1470err:
1471 kfree(uc);
1472 return ERR_PTR(ret);
1473}
1474EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1475
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001476/**
1477 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001478 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001479 * @n: number of string IDs to allocate
1480 * Context: single threaded during gadget setup
1481 *
1482 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001483 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001484 * is, returns last requested ID which is now very useful information.
1485 *
1486 * @usb_string_ids_n() is called from bind() callbacks to allocate
1487 * string IDs. Drivers for functions, configurations, or gadgets will
1488 * then store that ID in the appropriate descriptors and string table.
1489 *
1490 * All string identifier should be allocated using this,
1491 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1492 * example different functions don't wrongly assign different meanings
1493 * to the same identifier.
1494 */
1495int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1496{
1497 unsigned next = c->next_string_id;
1498 if (unlikely(n > 254 || (unsigned)next + n > 254))
1499 return -ENODEV;
1500 c->next_string_id += n;
1501 return next + 1;
1502}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001503EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001504
David Brownell40982be2008-06-19 17:52:58 -07001505/*-------------------------------------------------------------------------*/
1506
1507static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1508{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001509 struct usb_composite_dev *cdev;
1510
David Brownell40982be2008-06-19 17:52:58 -07001511 if (req->status || req->actual != req->length)
1512 DBG((struct usb_composite_dev *) ep->driver_data,
1513 "setup complete --> %d, %d/%d\n",
1514 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001515
1516 /*
1517 * REVIST The same ep0 requests are shared with function drivers
1518 * so they don't have to maintain the same ->complete() stubs.
1519 *
1520 * Because of that, we need to check for the validity of ->context
1521 * here, even though we know we've set it to something useful.
1522 */
1523 if (!req->context)
1524 return;
1525
1526 cdev = req->context;
1527
1528 if (cdev->req == req)
1529 cdev->setup_pending = false;
1530 else if (cdev->os_desc_req == req)
1531 cdev->os_desc_pending = false;
1532 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301533 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001534}
1535
1536static int composite_ep0_queue(struct usb_composite_dev *cdev,
1537 struct usb_request *req, gfp_t gfp_flags)
1538{
1539 int ret;
1540
1541 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1542 if (ret == 0) {
1543 if (cdev->req == req)
1544 cdev->setup_pending = true;
1545 else if (cdev->os_desc_req == req)
1546 cdev->os_desc_pending = true;
1547 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301548 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001549 }
1550
1551 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001552}
1553
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001554static int count_ext_compat(struct usb_configuration *c)
1555{
1556 int i, res;
1557
1558 res = 0;
1559 for (i = 0; i < c->next_interface_id; ++i) {
1560 struct usb_function *f;
1561 int j;
1562
1563 f = c->interface[i];
1564 for (j = 0; j < f->os_desc_n; ++j) {
1565 struct usb_os_desc *d;
1566
1567 if (i != f->os_desc_table[j].if_id)
1568 continue;
1569 d = f->os_desc_table[j].os_desc;
1570 if (d && d->ext_compat_id)
1571 ++res;
1572 }
1573 }
1574 BUG_ON(res > 255);
1575 return res;
1576}
1577
Chris Dickensbf54f312017-12-31 18:59:42 -08001578static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001579{
1580 int i, count;
1581
1582 count = 16;
1583 for (i = 0; i < c->next_interface_id; ++i) {
1584 struct usb_function *f;
1585 int j;
1586
1587 f = c->interface[i];
1588 for (j = 0; j < f->os_desc_n; ++j) {
1589 struct usb_os_desc *d;
1590
1591 if (i != f->os_desc_table[j].if_id)
1592 continue;
1593 d = f->os_desc_table[j].os_desc;
1594 if (d && d->ext_compat_id) {
1595 *buf++ = i;
1596 *buf++ = 0x01;
1597 memcpy(buf, d->ext_compat_id, 16);
1598 buf += 22;
1599 } else {
1600 ++buf;
1601 *buf = 0x01;
1602 buf += 23;
1603 }
1604 count += 24;
Chris Dickensbf54f312017-12-31 18:59:42 -08001605 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1606 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001607 }
1608 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001609
1610 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001611}
1612
1613static int count_ext_prop(struct usb_configuration *c, int interface)
1614{
1615 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001616 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001617
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301618 if (interface >= c->next_interface_id)
1619 return -EINVAL;
1620
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001621 f = c->interface[interface];
1622 for (j = 0; j < f->os_desc_n; ++j) {
1623 struct usb_os_desc *d;
1624
1625 if (interface != f->os_desc_table[j].if_id)
1626 continue;
1627 d = f->os_desc_table[j].os_desc;
1628 if (d && d->ext_compat_id)
1629 return d->ext_prop_count;
1630 }
Julia Lawall849b1332014-05-19 06:31:07 +02001631 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001632}
1633
1634static int len_ext_prop(struct usb_configuration *c, int interface)
1635{
1636 struct usb_function *f;
1637 struct usb_os_desc *d;
1638 int j, res;
1639
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301640 if (interface >= c->next_interface_id)
1641 return -EINVAL;
1642
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001643 res = 10; /* header length */
1644 f = c->interface[interface];
1645 for (j = 0; j < f->os_desc_n; ++j) {
1646 if (interface != f->os_desc_table[j].if_id)
1647 continue;
1648 d = f->os_desc_table[j].os_desc;
1649 if (d)
1650 return min(res + d->ext_prop_len, 4096);
1651 }
1652 return res;
1653}
1654
1655static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1656{
1657 struct usb_function *f;
1658 struct usb_os_desc *d;
1659 struct usb_os_desc_ext_prop *ext_prop;
1660 int j, count, n, ret;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001661
1662 f = c->interface[interface];
Chris Dickensbf54f312017-12-31 18:59:42 -08001663 count = 10; /* header length */
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001664 for (j = 0; j < f->os_desc_n; ++j) {
1665 if (interface != f->os_desc_table[j].if_id)
1666 continue;
1667 d = f->os_desc_table[j].os_desc;
1668 if (d)
1669 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
Chris Dickensbf54f312017-12-31 18:59:42 -08001670 n = ext_prop->data_len +
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001671 ext_prop->name_len + 14;
Chris Dickensbf54f312017-12-31 18:59:42 -08001672 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1673 return count;
1674 usb_ext_prop_put_size(buf, n);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001675 usb_ext_prop_put_type(buf, ext_prop->type);
1676 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1677 ext_prop->name_len);
1678 if (ret < 0)
1679 return ret;
1680 switch (ext_prop->type) {
1681 case USB_EXT_PROP_UNICODE:
1682 case USB_EXT_PROP_UNICODE_ENV:
1683 case USB_EXT_PROP_UNICODE_LINK:
1684 usb_ext_prop_put_unicode(buf, ret,
1685 ext_prop->data,
1686 ext_prop->data_len);
1687 break;
1688 case USB_EXT_PROP_BINARY:
1689 usb_ext_prop_put_binary(buf, ret,
1690 ext_prop->data,
1691 ext_prop->data_len);
1692 break;
1693 case USB_EXT_PROP_LE32:
1694 /* not implemented */
1695 case USB_EXT_PROP_BE32:
1696 /* not implemented */
1697 default:
1698 return -EINVAL;
1699 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001700 buf += n;
1701 count += n;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001702 }
1703 }
1704
Chris Dickensbf54f312017-12-31 18:59:42 -08001705 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001706}
1707
David Brownell40982be2008-06-19 17:52:58 -07001708/*
1709 * The setup() callback implements all the ep0 functionality that's
1710 * not handled lower down, in hardware or the hardware driver(like
1711 * device and endpoint feature flags, and their status). It's all
1712 * housekeeping for the gadget function we're implementing. Most of
1713 * the work is in config and function specific setup.
1714 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001715int
David Brownell40982be2008-06-19 17:52:58 -07001716composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1717{
1718 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1719 struct usb_request *req = cdev->req;
1720 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001721 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001722 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001723 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001724 u16 w_value = le16_to_cpu(ctrl->wValue);
1725 u16 w_length = le16_to_cpu(ctrl->wLength);
1726 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001727 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001728
1729 /* partial re-init of the response message; the function or the
1730 * gadget might need to intercept e.g. a control-OUT completion
1731 * when we delegate to it.
1732 */
1733 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001734 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001735 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301736 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001737 gadget->ep0->driver_data = cdev;
1738
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001739 /*
1740 * Don't let non-standard requests match any of the cases below
1741 * by accident.
1742 */
1743 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1744 goto unknown;
1745
David Brownell40982be2008-06-19 17:52:58 -07001746 switch (ctrl->bRequest) {
1747
1748 /* we handle all standard USB descriptors */
1749 case USB_REQ_GET_DESCRIPTOR:
1750 if (ctrl->bRequestType != USB_DIR_IN)
1751 goto unknown;
1752 switch (w_value >> 8) {
1753
1754 case USB_DT_DEVICE:
1755 cdev->desc.bNumConfigurations =
1756 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001757 cdev->desc.bMaxPacketSize0 =
1758 cdev->gadget->ep0->maxpacket;
Mayank Rana72bf6d02016-12-19 14:48:38 -08001759 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001760 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001761 if (gadget->speed >= USB_SPEED_SUPER) {
Chunfeng Yun3069db12018-05-09 19:29:16 +08001762 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001763 cdev->desc.bMaxPacketSize0 = 9;
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301764 } else if (gadget->l1_supported
1765 && !disable_l1_for_hs) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001766 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001767 }
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301768 } else if (gadget->l1_supported
1769 && !disable_l1_for_hs) {
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001770 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1771 DBG(cdev, "Config HS device with LPM(L1)\n");
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001772 }
1773
David Brownell40982be2008-06-19 17:52:58 -07001774 value = min(w_length, (u16) sizeof cdev->desc);
1775 memcpy(req->buf, &cdev->desc, value);
1776 break;
1777 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001778 if (!gadget_is_dualspeed(gadget) ||
1779 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001780 break;
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301781 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001782 device_qual(cdev);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301783 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001784 value = min_t(int, w_length,
1785 sizeof(struct usb_qualifier_descriptor));
1786 break;
1787 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001788 if (!gadget_is_dualspeed(gadget) ||
1789 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001790 break;
1791 /* FALLTHROUGH */
1792 case USB_DT_CONFIG:
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301793 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001794 value = config_desc(cdev, w_value);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301795 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001796 if (value >= 0)
1797 value = min(w_length, (u16) value);
1798 break;
1799 case USB_DT_STRING:
1800 value = get_string(cdev, req->buf,
1801 w_index, w_value & 0xff);
1802 if (value >= 0)
1803 value = min(w_length, (u16) value);
1804 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001805 case USB_DT_BOS:
Sriharsha Allenki876ac062018-03-05 01:08:07 +05301806 if ((gadget_is_superspeed(gadget) &&
1807 (gadget->speed >= USB_SPEED_SUPER)) ||
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301808 (gadget->l1_supported
1809 && !disable_l1_for_hs)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001810 value = bos_desc(cdev);
1811 value = min(w_length, (u16) value);
1812 }
1813 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001814 case USB_DT_OTG:
1815 if (gadget_is_otg(gadget)) {
1816 struct usb_configuration *config;
1817 int otg_desc_len = 0;
1818
1819 if (cdev->config)
1820 config = cdev->config;
1821 else
1822 config = list_first_entry(
1823 &cdev->configs,
1824 struct usb_configuration, list);
1825 if (!config)
1826 goto done;
1827
1828 if (gadget->otg_caps &&
1829 (gadget->otg_caps->otg_rev >= 0x0200))
1830 otg_desc_len += sizeof(
1831 struct usb_otg20_descriptor);
1832 else
1833 otg_desc_len += sizeof(
1834 struct usb_otg_descriptor);
1835
1836 value = min_t(int, w_length, otg_desc_len);
1837 memcpy(req->buf, config->descriptors[0], value);
1838 }
1839 break;
David Brownell40982be2008-06-19 17:52:58 -07001840 }
1841 break;
1842
1843 /* any number of configs can work */
1844 case USB_REQ_SET_CONFIGURATION:
1845 if (ctrl->bRequestType != 0)
1846 goto unknown;
1847 if (gadget_is_otg(gadget)) {
1848 if (gadget->a_hnp_support)
1849 DBG(cdev, "HNP available\n");
1850 else if (gadget->a_alt_hnp_support)
1851 DBG(cdev, "HNP on another port\n");
1852 else
1853 VDBG(cdev, "HNP inactive\n");
1854 }
1855 spin_lock(&cdev->lock);
1856 value = set_config(cdev, ctrl, w_value);
1857 spin_unlock(&cdev->lock);
1858 break;
1859 case USB_REQ_GET_CONFIGURATION:
1860 if (ctrl->bRequestType != USB_DIR_IN)
1861 goto unknown;
1862 if (cdev->config)
1863 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1864 else
1865 *(u8 *)req->buf = 0;
1866 value = min(w_length, (u16) 1);
1867 break;
1868
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001869 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001870 case USB_REQ_SET_INTERFACE:
1871 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1872 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001873 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001874 break;
Bryan Wu08889512009-01-08 00:21:19 +08001875 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001876 if (!f)
1877 break;
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001878
1879 /*
1880 * If there's no get_alt() method, we know only altsetting zero
1881 * works. There is no need to check if set_alt() is not NULL
1882 * as we check this in usb_add_function().
1883 */
1884 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001885 break;
Chunfeng Yun121621e2018-05-25 17:24:57 +08001886
1887 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001888 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001889 if (value == USB_GADGET_DELAYED_STATUS) {
1890 DBG(cdev,
1891 "%s: interface %d (%s) requested delayed status\n",
1892 __func__, intf, f->name);
1893 cdev->delayed_status++;
1894 DBG(cdev, "delayed_status count %d\n",
1895 cdev->delayed_status);
1896 }
Chunfeng Yun121621e2018-05-25 17:24:57 +08001897 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001898 break;
1899 case USB_REQ_GET_INTERFACE:
1900 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1901 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001902 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001903 break;
Bryan Wu08889512009-01-08 00:21:19 +08001904 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001905 if (!f)
1906 break;
1907 /* lots of interfaces only need altsetting zero... */
1908 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1909 if (value < 0)
1910 break;
1911 *((u8 *)req->buf) = value;
1912 value = min(w_length, (u16) 1);
1913 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001914 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001915 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1916 (w_index == OTG_STS_SELECTOR)) {
1917 if (ctrl->bRequestType != (USB_DIR_IN |
1918 USB_RECIP_DEVICE))
1919 goto unknown;
1920 *((u8 *)req->buf) = gadget->host_request_flag;
1921 value = 1;
1922 break;
1923 }
1924
1925 /*
1926 * USB 3.0 additions:
1927 * Function driver should handle get_status request. If such cb
1928 * wasn't supplied we respond with default value = 0
1929 * Note: function driver should supply such cb only for the
1930 * first interface of the function
1931 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001932 if (!gadget_is_superspeed(gadget))
1933 goto unknown;
1934 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1935 goto unknown;
1936 value = 2; /* This is the length of the get_status reply */
1937 put_unaligned_le16(0, req->buf);
1938 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1939 break;
1940 f = cdev->config->interface[intf];
1941 if (!f)
1942 break;
1943 status = f->get_status ? f->get_status(f) : 0;
1944 if (status < 0)
1945 break;
1946 put_unaligned_le16(status & 0x0000ffff, req->buf);
1947 break;
1948 /*
1949 * Function drivers should handle SetFeature/ClearFeature
1950 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1951 * only for the first interface of the function
1952 */
1953 case USB_REQ_CLEAR_FEATURE:
1954 case USB_REQ_SET_FEATURE:
1955 if (!gadget_is_superspeed(gadget))
1956 goto unknown;
1957 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1958 goto unknown;
1959 switch (w_value) {
1960 case USB_INTRF_FUNC_SUSPEND:
1961 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1962 break;
1963 f = cdev->config->interface[intf];
1964 if (!f)
1965 break;
1966 value = 0;
Danny Segalf83e4512016-12-06 15:35:24 -08001967 if (f->func_suspend) {
1968 const u8 suspend_opt = w_index >> 8;
1969
1970 value = f->func_suspend(f, suspend_opt);
1971 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1972 f->name ? f->name : "", suspend_opt);
1973 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001974 if (value < 0) {
1975 ERROR(cdev,
1976 "func_suspend() returned error %d\n",
1977 value);
1978 value = 0;
1979 }
1980 break;
1981 }
1982 break;
David Brownell40982be2008-06-19 17:52:58 -07001983 default:
1984unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001985 /*
1986 * OS descriptors handling
1987 */
1988 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001989 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001990 ctrl->bRequest == cdev->b_vendor_code) {
1991 struct usb_request *req;
1992 struct usb_configuration *os_desc_cfg;
1993 u8 *buf;
1994 int interface;
1995 int count = 0;
1996
1997 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001998 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001999 req->complete = composite_setup_complete;
2000 buf = req->buf;
2001 os_desc_cfg = cdev->os_desc_config;
Chris Dickensbf54f312017-12-31 18:59:42 -08002002 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002003 memset(buf, 0, w_length);
2004 buf[5] = 0x01;
2005 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2006 case USB_RECIP_DEVICE:
2007 if (w_index != 0x4 || (w_value >> 8))
2008 break;
2009 buf[6] = w_index;
2010 if (w_length == 0x10) {
2011 /* Number of ext compat interfaces */
2012 count = count_ext_compat(os_desc_cfg);
2013 buf[8] = count;
2014 count *= 24; /* 24 B/ext compat desc */
2015 count += 16; /* header */
2016 put_unaligned_le32(count, buf);
2017 value = w_length;
2018 } else {
2019 /* "extended compatibility ID"s */
2020 count = count_ext_compat(os_desc_cfg);
2021 buf[8] = count;
2022 count *= 24; /* 24 B/ext compat desc */
2023 count += 16; /* header */
2024 put_unaligned_le32(count, buf);
2025 buf += 16;
Chris Dickensbf54f312017-12-31 18:59:42 -08002026 value = fill_ext_compat(os_desc_cfg, buf);
2027 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002028 }
2029 break;
2030 case USB_RECIP_INTERFACE:
2031 if (w_index != 0x5 || (w_value >> 8))
2032 break;
2033 interface = w_value & 0xFF;
2034 buf[6] = w_index;
2035 if (w_length == 0x0A) {
2036 count = count_ext_prop(os_desc_cfg,
2037 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302038 if (count < 0)
2039 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002040 put_unaligned_le16(count, buf + 8);
2041 count = len_ext_prop(os_desc_cfg,
2042 interface);
2043 put_unaligned_le32(count, buf);
2044
2045 value = w_length;
2046 } else {
2047 count = count_ext_prop(os_desc_cfg,
2048 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302049 if (count < 0)
2050 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002051 put_unaligned_le16(count, buf + 8);
2052 count = len_ext_prop(os_desc_cfg,
2053 interface);
2054 put_unaligned_le32(count, buf);
2055 buf += 10;
2056 value = fill_ext_prop(os_desc_cfg,
2057 interface, buf);
2058 if (value < 0)
2059 return value;
Chris Dickensbf54f312017-12-31 18:59:42 -08002060 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002061 }
2062 break;
2063 }
William Wu7e14f47a2016-05-13 18:30:42 +08002064
2065 if (value >= 0) {
2066 req->length = value;
2067 req->context = cdev;
2068 req->zero = value < w_length;
2069 value = composite_ep0_queue(cdev, req,
2070 GFP_ATOMIC);
2071 if (value < 0) {
2072 DBG(cdev, "ep_queue --> %d\n", value);
2073 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302074 if (value != -ESHUTDOWN)
2075 composite_setup_complete(
2076 gadget->ep0, req);
William Wu7e14f47a2016-05-13 18:30:42 +08002077 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002078 }
2079 return value;
2080 }
2081
David Brownell40982be2008-06-19 17:52:58 -07002082 VDBG(cdev,
2083 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2084 ctrl->bRequestType, ctrl->bRequest,
2085 w_value, w_index, w_length);
2086
Laurent Pinchart52426582009-10-21 00:03:38 +02002087 /* functions always handle their interfaces and endpoints...
2088 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002089 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002090 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302091 if (cdev->config) {
2092 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b452016-06-22 01:12:08 +02002093 if (f->req_match &&
2094 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302095 goto try_fun_setup;
Felix Hädicke1a00b452016-06-22 01:12:08 +02002096 } else {
2097 struct usb_configuration *c;
2098 list_for_each_entry(c, &cdev->configs, list)
2099 list_for_each_entry(f, &c->functions, list)
2100 if (f->req_match &&
2101 f->req_match(f, ctrl, true))
2102 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302103 }
Felix Hädicke1a00b452016-06-22 01:12:08 +02002104 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302105
Laurent Pinchart52426582009-10-21 00:03:38 +02002106 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2107 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002108 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302109 break;
2110 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002111 break;
2112
2113 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002114 if (!cdev->config)
2115 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002116 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2117 list_for_each_entry(f, &cdev->config->functions, list) {
2118 if (test_bit(endp, f->endpoints))
2119 break;
2120 }
2121 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002122 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002123 break;
David Brownell40982be2008-06-19 17:52:58 -07002124 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002125try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002126 if (f && f->setup)
2127 value = f->setup(f, ctrl);
2128 else {
David Brownell40982be2008-06-19 17:52:58 -07002129 struct usb_configuration *c;
2130
2131 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002132 if (!c)
2133 goto done;
2134
2135 /* try current config's setup */
2136 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002137 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002138 goto done;
2139 }
2140
2141 /* try the only function in the current config */
2142 if (!list_is_singular(&c->functions))
2143 goto done;
2144 f = list_first_entry(&c->functions, struct usb_function,
2145 list);
2146 if (f->setup)
2147 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002148 }
Vijayavardhan Vennapusa8edd67762013-10-17 15:00:02 +05302149 if (value == USB_GADGET_DELAYED_STATUS) {
2150 DBG(cdev,
2151 "%s: interface %d (%s) requested delayed status\n",
2152 __func__, intf, f->name);
2153 cdev->delayed_status++;
2154 DBG(cdev, "delayed_status count %d\n",
2155 cdev->delayed_status);
2156 }
David Brownell40982be2008-06-19 17:52:58 -07002157
2158 goto done;
2159 }
2160
2161 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002162 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002163 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002164 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002165 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002166 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002167 if (value < 0) {
2168 DBG(cdev, "ep_queue --> %d\n", value);
2169 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302170 if (value != -ESHUTDOWN)
2171 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002172 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002173 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2174 WARN(cdev,
2175 "%s: Delayed status not supported for w_length != 0",
2176 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002177 }
2178
2179done:
2180 /* device either stalls (value < 0) or reports success */
2181 return value;
2182}
2183
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002184void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002185{
2186 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2187 unsigned long flags;
2188
Badhri Jagan Sridharan9e954092015-05-06 13:40:15 -07002189 if (cdev == NULL) {
2190 WARN(1, "%s: Calling disconnect on a Gadget that is \
2191 not connected\n", __func__);
2192 return;
2193 }
2194
David Brownell40982be2008-06-19 17:52:58 -07002195 /* REVISIT: should we have config and device level
2196 * disconnect callbacks?
2197 */
2198 spin_lock_irqsave(&cdev->lock, flags);
Benjamin Herrenschmidtd0237652019-07-26 14:59:03 +10002199 cdev->suspended = 0;
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302200 if (cdev->config) {
2201 if (gadget->is_chipidea && !cdev->suspended) {
2202 spin_unlock_irqrestore(&cdev->lock, flags);
2203 msm_do_bam_disable_enable(CI_CTRL);
2204 spin_lock_irqsave(&cdev->lock, flags);
2205 }
David Brownell40982be2008-06-19 17:52:58 -07002206 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302207 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002208 if (cdev->driver->disconnect)
2209 cdev->driver->disconnect(cdev);
Pavankumar Kondeti47870672013-06-19 09:51:58 +05302210 if (cdev->delayed_status != 0) {
2211 INFO(cdev, "delayed status mismatch..resetting\n");
2212 cdev->delayed_status = 0;
2213 }
David Brownell40982be2008-06-19 17:52:58 -07002214 spin_unlock_irqrestore(&cdev->lock, flags);
2215}
2216
2217/*-------------------------------------------------------------------------*/
2218
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002219static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2220 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002221{
2222 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2223 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2224
Vijayavardhan Vennapusa5d2ac1a2017-09-13 15:41:20 +05302225 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002226}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002227static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002228
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002229static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002230{
2231 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002232 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2233 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002234
2235 /* composite_disconnect() must already have been called
2236 * by the underlying peripheral controller driver!
2237 * so there's no i/o concurrency that could affect the
2238 * state protected by cdev->lock.
2239 */
2240 WARN_ON(cdev->config);
2241
2242 while (!list_empty(&cdev->configs)) {
2243 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002244 c = list_first_entry(&cdev->configs,
2245 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002246 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002247 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002248 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002249 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002250
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002251 composite_dev_cleanup(cdev);
2252
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002253 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2254 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2255
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002256 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002257 kfree(cdev);
2258 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002259}
2260
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002261static void composite_unbind(struct usb_gadget *gadget)
2262{
2263 __composite_unbind(gadget, true);
2264}
2265
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002266static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2267 const struct usb_device_descriptor *old)
2268{
2269 __le16 idVendor;
2270 __le16 idProduct;
2271 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002272 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002273 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002274 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002275
2276 /*
2277 * these variables may have been set in
2278 * usb_composite_overwrite_options()
2279 */
2280 idVendor = new->idVendor;
2281 idProduct = new->idProduct;
2282 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002283 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002284 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002285 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002286
2287 *new = *old;
2288 if (idVendor)
2289 new->idVendor = idVendor;
2290 if (idProduct)
2291 new->idProduct = idProduct;
2292 if (bcdDevice)
2293 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002294 else
2295 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002296 if (iSerialNumber)
2297 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002298 if (iManufacturer)
2299 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002300 if (iProduct)
2301 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002302}
2303
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002304int composite_dev_prepare(struct usb_composite_driver *composite,
2305 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002306{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002307 struct usb_gadget *gadget = cdev->gadget;
2308 int ret = -ENOMEM;
2309
2310 /* preallocate control response and buffer */
2311 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2312 if (!cdev->req)
2313 return -ENOMEM;
2314
Sujeet Kumara6c0e992015-07-14 11:41:55 +05302315 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ +
2316 (gadget->extra_buf_alloc), GFP_KERNEL);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002317 if (!cdev->req->buf)
2318 goto fail;
2319
2320 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2321 if (ret)
2322 goto fail_dev;
2323
2324 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002325 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002326 gadget->ep0->driver_data = cdev;
2327
2328 cdev->driver = composite;
2329
2330 /*
2331 * As per USB compliance update, a device that is actively drawing
2332 * more than 100mA from USB must report itself as bus-powered in
2333 * the GetStatus(DEVICE) call.
2334 */
2335 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2336 usb_gadget_set_selfpowered(gadget);
2337
2338 /* interface and string IDs start at zero via kzalloc.
2339 * we force endpoints to start unassigned; few controller
2340 * drivers will zero ep->driver_data.
2341 */
2342 usb_ep_autoconfig_reset(gadget);
2343 return 0;
2344fail_dev:
2345 kfree(cdev->req->buf);
2346fail:
2347 usb_ep_free_request(gadget->ep0, cdev->req);
2348 cdev->req = NULL;
2349 return ret;
2350}
2351
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002352int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2353 struct usb_ep *ep0)
2354{
2355 int ret = 0;
2356
2357 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2358 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002359 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002360 goto end;
2361 }
2362
Chris Dickensbf54f312017-12-31 18:59:42 -08002363 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2364 GFP_KERNEL);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002365 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002366 ret = -ENOMEM;
Christophe JAILLETf0ee2032017-01-04 06:30:16 +01002367 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002368 goto end;
2369 }
Felipe Balbi57943712014-09-18 09:54:54 -05002370 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002371 cdev->os_desc_req->complete = composite_setup_complete;
2372end:
2373 return ret;
2374}
2375
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002376void composite_dev_cleanup(struct usb_composite_dev *cdev)
2377{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002378 struct usb_gadget_string_container *uc, *tmp;
2379
2380 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2381 list_del(&uc->list);
2382 kfree(uc);
2383 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002384 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002385 if (cdev->os_desc_pending)
2386 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2387
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002388 kfree(cdev->os_desc_req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002389 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002390 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002391 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002392 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002393 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002394 if (cdev->setup_pending)
2395 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2396
Li Junbe0a8882014-08-28 21:44:11 +08002397 kfree(cdev->req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002398 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002399 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002400 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002401 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002402 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002403 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002404}
2405
2406static int composite_bind(struct usb_gadget *gadget,
2407 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002408{
2409 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002410 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002411 int status = -ENOMEM;
2412
2413 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2414 if (!cdev)
2415 return status;
2416
2417 spin_lock_init(&cdev->lock);
2418 cdev->gadget = gadget;
2419 set_gadget_data(gadget, cdev);
2420 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002421 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002422
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002423 status = composite_dev_prepare(composite, cdev);
2424 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002425 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002426
2427 /* composite gadget needs to assign strings for whole device (like
2428 * serial number), register function drivers, potentially update
2429 * power state and consumption, etc
2430 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002431 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002432 if (status < 0)
2433 goto fail;
2434
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002435 if (cdev->use_os_string) {
2436 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2437 if (status)
2438 goto fail;
2439 }
2440
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002441 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002442
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002443 /* has userspace failed to provide a serial number? */
2444 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2445 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2446
David Brownell40982be2008-06-19 17:52:58 -07002447 INFO(cdev, "%s ready\n", composite->name);
2448 return 0;
2449
2450fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002451 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002452 return status;
2453}
2454
2455/*-------------------------------------------------------------------------*/
2456
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002457void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002458{
2459 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2460 struct usb_function *f;
Mayank Rana13e5c152015-10-05 19:08:47 -07002461 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002462
David Brownell89429392009-03-19 14:14:17 -07002463 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002464 * suspend/resume callbacks?
2465 */
2466 DBG(cdev, "suspend\n");
Mayank Rana13e5c152015-10-05 19:08:47 -07002467 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002468 if (cdev->config) {
2469 list_for_each_entry(f, &cdev->config->functions, list) {
2470 if (f->suspend)
2471 f->suspend(f);
2472 }
2473 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002474 if (cdev->driver->suspend)
2475 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002476
2477 cdev->suspended = 1;
Mayank Rana13e5c152015-10-05 19:08:47 -07002478 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002479
2480 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002481}
2482
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002483void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002484{
2485 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2486 struct usb_function *f;
jianzhou21ae58e2020-03-24 11:07:44 +08002487 u16 maxpower;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302488 int ret;
2489 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002490
David Brownell89429392009-03-19 14:14:17 -07002491 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002492 * suspend/resume callbacks?
2493 */
2494 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002495 if (cdev->driver->resume)
2496 cdev->driver->resume(cdev);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002497
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302498 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002499 if (cdev->config) {
2500 list_for_each_entry(f, &cdev->config->functions, list) {
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302501 if (f->func_wakeup_pending) {
2502 ret = usb_func_wakeup_int(f);
2503 if (ret) {
2504 if (ret == -EAGAIN) {
2505 ERROR(f->config->cdev,
2506 "Function wakeup for %s could not complete due to suspend state.\n",
2507 f->name ? f->name : "");
2508 } else if (ret != -ENOTSUPP) {
2509 ERROR(f->config->cdev,
2510 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2511 f->name ? f->name : "",
2512 ret);
2513 }
Danny Segalde7cd8d2014-07-28 18:08:33 +03002514 }
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302515 f->func_wakeup_pending = 0;
Danny Segal86cb50e2014-07-09 15:14:49 +03002516 }
2517
Sriharsha Allenki6ab82052018-09-24 18:21:44 +05302518 /*
2519 * Call function resume irrespective of the speed.
2520 * Individual function needs to retain the USB3 Function
2521 * suspend state through out the Device suspend entry
2522 * and exit process.
2523 */
2524 if (f->resume)
David Brownell40982be2008-06-19 17:52:58 -07002525 f->resume(f);
2526 }
Hao Wub23f2f92010-11-29 15:17:03 +08002527
jianzhou21ae58e2020-03-24 11:07:44 +08002528 maxpower = cdev->config->MaxPower;
2529
2530 usb_gadget_vbus_draw(gadget, maxpower ?
2531 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002532 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002533
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302534 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002535 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002536}
2537
2538/*-------------------------------------------------------------------------*/
2539
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002540static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002541 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002542 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002543
2544 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002545 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002546 .disconnect = composite_disconnect,
2547
2548 .suspend = composite_suspend,
2549 .resume = composite_resume,
2550
2551 .driver = {
2552 .owner = THIS_MODULE,
2553 },
2554};
2555
2556/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002557 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002558 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002559 *
David Brownell40982be2008-06-19 17:52:58 -07002560 * Context: single threaded during gadget setup
2561 *
2562 * This function is used to register drivers using the composite driver
2563 * framework. The return value is zero, or a negative errno value.
2564 * Those values normally come from the driver's @bind method, which does
2565 * all the work of setting up the driver to match the hardware.
2566 *
2567 * On successful return, the gadget is ready to respond to requests from
2568 * the host, unless one of its components invokes usb_gadget_disconnect()
2569 * while it was binding. That would usually be done in order to wait for
2570 * some userspace participation.
2571 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002572int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002573{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002574 struct usb_gadget_driver *gadget_driver;
2575
2576 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002577 return -EINVAL;
2578
2579 if (!driver->name)
2580 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002581
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002582 driver->gadget_driver = composite_driver_template;
2583 gadget_driver = &driver->gadget_driver;
2584
2585 gadget_driver->function = (char *) driver->name;
2586 gadget_driver->driver.name = driver->name;
2587 gadget_driver->max_speed = driver->max_speed;
2588
2589 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002590}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002591EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002592
2593/**
2594 * usb_composite_unregister() - unregister a composite driver
2595 * @driver: the driver to unregister
2596 *
2597 * This function is used to unregister drivers using the composite
2598 * driver framework.
2599 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002600void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002601{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002602 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002603}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002604EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002605
2606/**
2607 * usb_composite_setup_continue() - Continue with the control transfer
2608 * @cdev: the composite device who's control transfer was kept waiting
2609 *
2610 * This function must be called by the USB function driver to continue
2611 * with the control transfer's data/status stage in case it had requested to
2612 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2613 * can request the composite framework to delay the setup request's data/status
2614 * stages by returning USB_GADGET_DELAYED_STATUS.
2615 */
2616void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2617{
2618 int value;
2619 struct usb_request *req = cdev->req;
2620 unsigned long flags;
2621
2622 DBG(cdev, "%s\n", __func__);
2623 spin_lock_irqsave(&cdev->lock, flags);
2624
2625 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302626 if (!cdev->config) {
2627 spin_unlock_irqrestore(&cdev->lock, flags);
2628 return;
2629 }
2630 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002631 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302632 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002633
2634 } else if (--cdev->delayed_status == 0) {
2635 DBG(cdev, "%s: Completing delayed status\n", __func__);
2636 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002637 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002638 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002639 if (value < 0) {
2640 DBG(cdev, "ep_queue --> %d\n", value);
2641 req->status = 0;
2642 composite_setup_complete(cdev->gadget->ep0, req);
2643 }
2644 }
2645
2646 spin_unlock_irqrestore(&cdev->lock, flags);
2647}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002648EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002649
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002650static char *composite_default_mfr(struct usb_gadget *gadget)
2651{
2652 char *mfr;
2653 int len;
2654
2655 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2656 init_utsname()->release, gadget->name);
2657 len++;
2658 mfr = kmalloc(len, GFP_KERNEL);
2659 if (!mfr)
2660 return NULL;
2661 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2662 init_utsname()->release, gadget->name);
2663 return mfr;
2664}
2665
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002666void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2667 struct usb_composite_overwrite *covr)
2668{
2669 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002670 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2671 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002672
2673 if (covr->idVendor)
2674 desc->idVendor = cpu_to_le16(covr->idVendor);
2675
2676 if (covr->idProduct)
2677 desc->idProduct = cpu_to_le16(covr->idProduct);
2678
2679 if (covr->bcdDevice)
2680 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002681
2682 if (covr->serial_number) {
2683 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2684 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2685 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002686 if (covr->manufacturer) {
2687 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2688 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002689
2690 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2691 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2692 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2693 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002694 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002695
2696 if (covr->product) {
2697 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2698 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2699 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002700}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002701EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002702
2703MODULE_LICENSE("GPL");
2704MODULE_AUTHOR("David Brownell");