blob: 3c1135292d0fd4245e970ce90ff223445cf6adbd [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>
Aniket Randive0a78a292020-03-18 11:22:09 +053020#include <soc/qcom/boot_stats.h>
David Brownell40982be2008-06-19 17:52:58 -070021
22#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080023#include <linux/usb/otg.h>
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +053024#include <linux/usb/msm_hsusb.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030025#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070026
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020027#include "u_os_desc.h"
28
Hemant Kumar7e115fd2017-07-19 12:08:16 -070029/* disable LPM by default */
Hemant Kumarc41d680a2018-02-14 16:37:37 -080030static bool disable_l1_for_hs;
Hemant Kumar7e115fd2017-07-19 12:08:16 -070031module_param(disable_l1_for_hs, bool, 0644);
32MODULE_PARM_DESC(disable_l1_for_hs,
33 "Disable support for L1 LPM for HS devices");
34
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020035/**
36 * struct usb_os_string - represents OS String to be reported by a gadget
37 * @bLength: total length of the entire descritor, always 0x12
38 * @bDescriptorType: USB_DT_STRING
39 * @qwSignature: the OS String proper
40 * @bMS_VendorCode: code used by the host for subsequent requests
41 * @bPad: not used, must be zero
42 */
43struct usb_os_string {
44 __u8 bLength;
45 __u8 bDescriptorType;
46 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
47 __u8 bMS_VendorCode;
48 __u8 bPad;
49} __packed;
50
David Brownell40982be2008-06-19 17:52:58 -070051/*
52 * The code in this file is utility code, used to build a gadget driver
53 * from one or more "function" drivers, one or more "configuration"
54 * objects, and a "usb_composite_driver" by gluing them together along
55 * with the relevant device-wide data.
56 */
57
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010058static struct usb_gadget_strings **get_containers_gs(
59 struct usb_gadget_string_container *uc)
60{
61 return (struct usb_gadget_strings **)uc->stash;
62}
63
Tatyana Brokhman48767a42011-06-28 16:33:49 +030064/**
John Younf3bdbe32016-02-05 17:07:03 -080065 * function_descriptors() - get function descriptors for speed
66 * @f: the function
67 * @speed: the speed
68 *
69 * Returns the descriptors or NULL if not set.
70 */
71static struct usb_descriptor_header **
72function_descriptors(struct usb_function *f,
73 enum usb_device_speed speed)
74{
75 struct usb_descriptor_header **descriptors;
76
Felipe Balbi08782632016-04-22 14:53:47 +030077 /*
78 * NOTE: we try to help gadget drivers which might not be setting
79 * max_speed appropriately.
80 */
81
John Younf3bdbe32016-02-05 17:07:03 -080082 switch (speed) {
83 case USB_SPEED_SUPER_PLUS:
84 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030085 if (descriptors)
86 break;
87 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080088 case USB_SPEED_SUPER:
89 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030090 if (descriptors)
91 break;
92 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080093 case USB_SPEED_HIGH:
94 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030095 if (descriptors)
96 break;
97 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080098 default:
99 descriptors = f->fs_descriptors;
100 }
101
Felipe Balbi08782632016-04-22 14:53:47 +0300102 /*
103 * if we can't find any descriptors at all, then this gadget deserves to
104 * Oops with a NULL pointer dereference
105 */
106
John Younf3bdbe32016-02-05 17:07:03 -0800107 return descriptors;
108}
109
110/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300111 * next_ep_desc() - advance to the next EP descriptor
112 * @t: currect pointer within descriptor array
113 *
114 * Return: next EP descriptor or NULL
115 *
116 * Iterate over @t until either EP descriptor found or
117 * NULL (that indicates end of list) encountered
118 */
119static struct usb_descriptor_header**
120next_ep_desc(struct usb_descriptor_header **t)
121{
122 for (; *t; t++) {
123 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
124 return t;
125 }
126 return NULL;
127}
128
129/*
130 * for_each_ep_desc()- iterate over endpoint descriptors in the
131 * descriptors list
132 * @start: pointer within descriptor array.
133 * @ep_desc: endpoint descriptor to use as the loop cursor
134 */
135#define for_each_ep_desc(start, ep_desc) \
136 for (ep_desc = next_ep_desc(start); \
137 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
138
139/**
140 * config_ep_by_speed() - configures the given endpoint
141 * according to gadget speed.
142 * @g: pointer to the gadget
143 * @f: usb function
144 * @_ep: the endpoint to configure
145 *
146 * Return: error code, 0 on success
147 *
148 * This function chooses the right descriptors for a given
149 * endpoint according to gadget speed and saves it in the
150 * endpoint desc field. If the endpoint already has a descriptor
151 * assigned to it - overwrites it with currently corresponding
152 * descriptor. The endpoint maxpacket field is updated according
153 * to the chosen descriptor.
154 * Note: the supplied function should hold all the descriptors
155 * for supported speeds
156 */
157int config_ep_by_speed(struct usb_gadget *g,
158 struct usb_function *f,
159 struct usb_ep *_ep)
160{
161 struct usb_endpoint_descriptor *chosen_desc = NULL;
162 struct usb_descriptor_header **speed_desc = NULL;
163
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300164 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
165 int want_comp_desc = 0;
166
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300167 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
168
169 if (!g || !f || !_ep)
170 return -EIO;
171
172 /* select desired speed */
173 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800174 case USB_SPEED_SUPER_PLUS:
175 if (gadget_is_superspeed_plus(g)) {
176 speed_desc = f->ssp_descriptors;
177 want_comp_desc = 1;
178 break;
179 }
180 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300181 case USB_SPEED_SUPER:
182 if (gadget_is_superspeed(g)) {
183 speed_desc = f->ss_descriptors;
184 want_comp_desc = 1;
185 break;
186 }
187 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300188 case USB_SPEED_HIGH:
189 if (gadget_is_dualspeed(g)) {
190 speed_desc = f->hs_descriptors;
191 break;
192 }
193 /* else: fall through */
194 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200195 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300196 }
197 /* find descriptors */
198 for_each_ep_desc(speed_desc, d_spd) {
199 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
200 if (chosen_desc->bEndpointAddress == _ep->address)
201 goto ep_found;
202 }
203 return -EIO;
204
205ep_found:
206 /* commit results */
Felipe Balbie0aa5ec2016-09-28 10:38:11 +0300207 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300208 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300209 _ep->comp_desc = NULL;
210 _ep->maxburst = 0;
Felipe Balbi3999c532016-09-28 12:33:31 +0300211 _ep->mult = 1;
212
213 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
214 usb_endpoint_xfer_int(_ep->desc)))
215 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
216
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300217 if (!want_comp_desc)
218 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300219
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300220 /*
221 * Companion descriptor should follow EP descriptor
222 * USB 3.0 spec, #9.6.7
223 */
224 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
225 if (!comp_desc ||
226 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
227 return -EIO;
228 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800229 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300230 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300231 case USB_ENDPOINT_XFER_ISOC:
232 /* mult: bits 1:0 of bmAttributes */
Felipe Balbi3999c532016-09-28 12:33:31 +0300233 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800234 case USB_ENDPOINT_XFER_BULK:
235 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300236 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300237 break;
238 default:
Colin Ian King0e216b02017-11-14 16:18:28 +0000239 if (comp_desc->bMaxBurst != 0) {
240 struct usb_composite_dev *cdev;
241
242 cdev = get_gadget_data(g);
Felipe Balbib785ea72012-06-06 10:20:23 +0300243 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
Colin Ian King0e216b02017-11-14 16:18:28 +0000244 }
Felipe Balbib785ea72012-06-06 10:20:23 +0300245 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300246 break;
247 }
248 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300249 return 0;
250}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200251EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700252
253/**
254 * usb_add_function() - add a function to a configuration
255 * @config: the configuration
256 * @function: the function being added
257 * Context: single threaded during gadget setup
258 *
259 * After initialization, each configuration must have one or more
260 * functions added to it. Adding a function involves calling its @bind()
261 * method to allocate resources such as interface and string identifiers
262 * and endpoints.
263 *
264 * This function returns the value of the function's bind(), which is
265 * zero for success else a negative errno value.
266 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200267int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700268 struct usb_function *function)
269{
270 int value = -EINVAL;
271
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530272 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700273 function->name, function,
274 config->label, config);
275
276 if (!function->set_alt || !function->disable)
277 goto done;
278
279 function->config = config;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700280 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700281 list_add_tail(&function->list, &config->functions);
282
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200283 if (function->bind_deactivated) {
284 value = usb_function_deactivate(function);
285 if (value)
286 goto done;
287 }
288
David Brownell40982be2008-06-19 17:52:58 -0700289 /* REVISIT *require* function->bind? */
290 if (function->bind) {
291 value = function->bind(config, function);
292 if (value < 0) {
293 list_del(&function->list);
294 function->config = NULL;
295 }
296 } else
297 value = 0;
298
299 /* We allow configurations that don't work at both speeds.
300 * If we run into a lowspeed Linux system, treat it the same
301 * as full speed ... it's the function drivers that will need
302 * to avoid bulk and ISO transfers.
303 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200304 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700305 config->fullspeed = true;
306 if (!config->highspeed && function->hs_descriptors)
307 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300308 if (!config->superspeed && function->ss_descriptors)
309 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800310 if (!config->superspeed_plus && function->ssp_descriptors)
311 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700312
313done:
314 if (value)
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530315 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700316 function->name, function, value);
317 return value;
318}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200319EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700320
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100321void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
322{
323 if (f->disable)
324 f->disable(f);
325
326 bitmap_zero(f->endpoints, 32);
327 list_del(&f->list);
328 if (f->unbind)
329 f->unbind(c, f);
330}
331EXPORT_SYMBOL_GPL(usb_remove_function);
332
David Brownell40982be2008-06-19 17:52:58 -0700333/**
David Brownell60beed92008-08-18 17:38:22 -0700334 * usb_function_deactivate - prevent function and gadget enumeration
335 * @function: the function that isn't yet ready to respond
336 *
337 * Blocks response of the gadget driver to host enumeration by
338 * preventing the data line pullup from being activated. This is
339 * normally called during @bind() processing to change from the
340 * initial "ready to respond" state, or when a required resource
341 * becomes available.
342 *
343 * For example, drivers that serve as a passthrough to a userspace
344 * daemon can block enumeration unless that daemon (such as an OBEX,
345 * MTP, or print server) is ready to handle host requests.
346 *
347 * Not all systems support software control of their USB peripheral
348 * data pullups.
349 *
350 * Returns zero on success, else negative errno.
351 */
352int usb_function_deactivate(struct usb_function *function)
353{
354 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200355 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700356 int status = 0;
357
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200358 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700359
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530360 if (cdev->deactivations == 0) {
361 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200362 status = usb_gadget_deactivate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530363 spin_lock_irqsave(&cdev->lock, flags);
364 }
David Brownell60beed92008-08-18 17:38:22 -0700365 if (status == 0)
366 cdev->deactivations++;
367
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200368 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700369 return status;
370}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200371EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700372
373/**
374 * usb_function_activate - allow function and gadget enumeration
375 * @function: function on which usb_function_activate() was called
376 *
377 * Reverses effect of usb_function_deactivate(). If no more functions
378 * are delaying their activation, the gadget driver will respond to
379 * host enumeration procedures.
380 *
381 * Returns zero on success, else negative errno.
382 */
383int usb_function_activate(struct usb_function *function)
384{
385 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200386 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700387 int status = 0;
388
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200389 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700390
391 if (WARN_ON(cdev->deactivations == 0))
392 status = -EINVAL;
393 else {
394 cdev->deactivations--;
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530395 if (cdev->deactivations == 0) {
396 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200397 status = usb_gadget_activate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530398 spin_lock_irqsave(&cdev->lock, flags);
399 }
David Brownell60beed92008-08-18 17:38:22 -0700400 }
401
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200402 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700403 return status;
404}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200405EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700406
407/**
David Brownell40982be2008-06-19 17:52:58 -0700408 * usb_interface_id() - allocate an unused interface ID
409 * @config: configuration associated with the interface
410 * @function: function handling the interface
411 * Context: single threaded during gadget setup
412 *
413 * usb_interface_id() is called from usb_function.bind() callbacks to
414 * allocate new interface IDs. The function driver will then store that
415 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300416 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700417 * particularly changing its altsetting via set_alt(). There may
418 * also be class-specific or vendor-specific requests to handle.
419 *
420 * All interface identifier should be allocated using this routine, to
421 * ensure that for example different functions don't wrongly assign
422 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300423 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700424 * one configuration (or more than once in a given configuration) need
425 * multiple versions of the relevant descriptors.
426 *
427 * Returns the interface ID which was allocated; or -ENODEV if no
428 * more interface IDs can be allocated.
429 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200430int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700431 struct usb_function *function)
432{
433 unsigned id = config->next_interface_id;
434
435 if (id < MAX_CONFIG_INTERFACES) {
436 config->interface[id] = function;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700437 if (function->intf_id < 0)
438 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700439 config->next_interface_id = id + 1;
440 return id;
441 }
442 return -ENODEV;
443}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200444EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700445
Mayank Ranab92dfd02014-11-25 15:29:58 -0800446static int usb_func_wakeup_int(struct usb_function *func)
Danny Segalf83e4512016-12-06 15:35:24 -0800447{
448 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800449 struct usb_gadget *gadget;
450
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800451 pr_debug("%s - %s function wakeup\n",
452 __func__, func->name ? func->name : "");
Danny Segalf83e4512016-12-06 15:35:24 -0800453
454 if (!func || !func->config || !func->config->cdev ||
455 !func->config->cdev->gadget)
456 return -EINVAL;
457
458 gadget = func->config->cdev->gadget;
459 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
460 DBG(func->config->cdev,
461 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
462 gadget->speed,
463 func->func_wakeup_allowed);
464
465 return -ENOTSUPP;
466 }
467
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800468 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300469
470 return ret;
471}
472
473int usb_func_wakeup(struct usb_function *func)
474{
475 int ret;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530476 unsigned long flags;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300477
478 pr_debug("%s function wakeup\n",
479 func->name ? func->name : "");
480
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530481 spin_lock_irqsave(&func->config->cdev->lock, flags);
Mayank Ranab92dfd02014-11-25 15:29:58 -0800482 ret = usb_func_wakeup_int(func);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530483 if (ret == -EACCES) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300484 DBG(func->config->cdev,
485 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
486 func->name ? func->name : "");
Danny Segalde7cd8d2014-07-28 18:08:33 +0300487 ret = 0;
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530488 func->func_wakeup_pending = 1;
489 } else if (ret == -EAGAIN) {
490 DBG(func->config->cdev,
491 "Function wakeup for %s sent.\n",
492 func->name ? func->name : "");
493 ret = 0;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +0530494 } else if (ret < 0 && ret != -ENOTSUPP) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300495 ERROR(func->config->cdev,
496 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
497 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800498 }
499
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530500 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300501 return ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800502}
503EXPORT_SYMBOL(usb_func_wakeup);
504
505int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
506 struct usb_request *req, gfp_t gfp_flags)
507{
wHemant Kumare9e13922016-12-19 14:41:28 -0800508 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800509 struct usb_gadget *gadget;
510
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800511 if (!func || !func->config || !func->config->cdev ||
wHemant Kumare9e13922016-12-19 14:41:28 -0800512 !func->config->cdev->gadget || !ep || !req) {
513 ret = -EINVAL;
514 goto done;
515 }
Danny Segalf83e4512016-12-06 15:35:24 -0800516
517 pr_debug("Function %s queueing new data into ep %u\n",
518 func->name ? func->name : "", ep->address);
519
520 gadget = func->config->cdev->gadget;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800521 if (func->func_is_suspended && func->func_wakeup_allowed) {
522 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530523 if (ret == -EACCES) {
524 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
525 func->name ? func->name : "");
526 func->func_wakeup_pending = 1;
527 ret = -EAGAIN;
528 } else if (ret == -EAGAIN) {
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800529 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
530 func->name ? func->name : "");
531 } else if (ret < 0 && ret != -ENOTSUPP) {
532 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
533 func->name ? func->name : "", ret);
Chandana Kishori Chiluveru712cfc02018-07-27 17:25:09 +0530534 } else {
535 /*
536 * Return -EAGAIN to queue the request from
537 * function driver wakeup function.
538 */
539 ret = -EAGAIN;
540 goto done;
Danny Segalf83e4512016-12-06 15:35:24 -0800541 }
542 }
543
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800544 if (!func->func_is_suspended)
545 ret = 0;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800546
wHemant Kumare9e13922016-12-19 14:41:28 -0800547 if (func->func_is_suspended && !func->func_wakeup_allowed) {
548 ret = -ENOTSUPP;
549 goto done;
550 }
551
552 ret = usb_ep_queue(ep, req, gfp_flags);
553done:
Danny Segalf83e4512016-12-06 15:35:24 -0800554 return ret;
555}
556EXPORT_SYMBOL(usb_func_ep_queue);
557
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100558static u8 encode_bMaxPower(enum usb_device_speed speed,
559 struct usb_configuration *c)
560{
jianzhou52d3ea82020-03-24 11:22:12 +0800561 unsigned val;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100562
jianzhou52d3ea82020-03-24 11:22:12 +0800563 if (c->MaxPower)
564 val = c->MaxPower;
565 else
566 val = CONFIG_USB_GADGET_VBUS_DRAW;
567 if (!val)
568 return 0;
Jack Phamf7a3c472020-01-30 19:10:35 -0800569 if (speed < USB_SPEED_SUPER)
Jack Pham65967542020-01-30 19:10:36 -0800570 return min(val, 500U) / 2;
Jack Phamf7a3c472020-01-30 19:10:35 -0800571 else
Jack Pham65967542020-01-30 19:10:36 -0800572 /*
573 * USB 3.x supports up to 900mA, but since 900 isn't divisible
574 * by 8 the integral division will effectively cap to 896mA.
575 */
576 return min(val, 900U) / 8;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100577}
578
David Brownell40982be2008-06-19 17:52:58 -0700579static int config_buf(struct usb_configuration *config,
580 enum usb_device_speed speed, void *buf, u8 type)
581{
582 struct usb_config_descriptor *c = buf;
583 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200584 int len;
David Brownell40982be2008-06-19 17:52:58 -0700585 struct usb_function *f;
586 int status;
587
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200588 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700589 /* write the config descriptor */
590 c = buf;
591 c->bLength = USB_DT_CONFIG_SIZE;
592 c->bDescriptorType = type;
593 /* wTotalLength is written later */
594 c->bNumInterfaces = config->next_interface_id;
595 c->bConfigurationValue = config->bConfigurationValue;
596 c->iConfiguration = config->iConfiguration;
597 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100598 c->bMaxPower = encode_bMaxPower(speed, config);
Vijayavardhan Vennapusadaeb5ac2018-07-13 13:04:17 +0530599 if (config->cdev->gadget->self_powered) {
600 c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER;
601 c->bMaxPower = 0;
602 }
David Brownell40982be2008-06-19 17:52:58 -0700603
604 /* There may be e.g. OTG descriptors */
605 if (config->descriptors) {
606 status = usb_descriptor_fillbuf(next, len,
607 config->descriptors);
608 if (status < 0)
609 return status;
610 len -= status;
611 next += status;
612 }
613
614 /* add each function's descriptors */
615 list_for_each_entry(f, &config->functions, list) {
616 struct usb_descriptor_header **descriptors;
617
John Younf3bdbe32016-02-05 17:07:03 -0800618 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700619 if (!descriptors)
620 continue;
621 status = usb_descriptor_fillbuf(next, len,
622 (const struct usb_descriptor_header **) descriptors);
623 if (status < 0)
624 return status;
625 len -= status;
626 next += status;
627 }
628
629 len = next - buf;
630 c->wTotalLength = cpu_to_le16(len);
631 return len;
632}
633
634static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
635{
636 struct usb_gadget *gadget = cdev->gadget;
637 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200638 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700639 u8 type = w_value >> 8;
640 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
641
John Youneae58202016-02-05 17:07:17 -0800642 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300643 speed = gadget->speed;
644 else if (gadget_is_dualspeed(gadget)) {
645 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700646 if (gadget->speed == USB_SPEED_HIGH)
647 hs = 1;
648 if (type == USB_DT_OTHER_SPEED_CONFIG)
649 hs = !hs;
650 if (hs)
651 speed = USB_SPEED_HIGH;
652
653 }
654
655 /* This is a lookup by config *INDEX* */
656 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200657
658 pos = &cdev->configs;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200659
660 while ((pos = pos->next) != &cdev->configs) {
661 c = list_entry(pos, typeof(*c), list);
662
David Brownell40982be2008-06-19 17:52:58 -0700663 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300664 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800665 case USB_SPEED_SUPER_PLUS:
666 if (!c->superspeed_plus)
667 continue;
668 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300669 case USB_SPEED_SUPER:
670 if (!c->superspeed)
671 continue;
672 break;
673 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700674 if (!c->highspeed)
675 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300676 break;
677 default:
David Brownell40982be2008-06-19 17:52:58 -0700678 if (!c->fullspeed)
679 continue;
680 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300681
David Brownell40982be2008-06-19 17:52:58 -0700682 if (w_value == 0)
683 return config_buf(c, speed, cdev->req->buf, type);
684 w_value--;
685 }
686 return -EINVAL;
687}
688
689static int count_configs(struct usb_composite_dev *cdev, unsigned type)
690{
691 struct usb_gadget *gadget = cdev->gadget;
692 struct usb_configuration *c;
693 unsigned count = 0;
694 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300695 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800696 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700697
698 if (gadget_is_dualspeed(gadget)) {
699 if (gadget->speed == USB_SPEED_HIGH)
700 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300701 if (gadget->speed == USB_SPEED_SUPER)
702 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800703 if (gadget->speed == USB_SPEED_SUPER_PLUS)
704 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700705 if (type == USB_DT_DEVICE_QUALIFIER)
706 hs = !hs;
707 }
708 list_for_each_entry(c, &cdev->configs, list) {
709 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800710 if (ssp) {
711 if (!c->superspeed_plus)
712 continue;
713 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300714 if (!c->superspeed)
715 continue;
716 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700717 if (!c->highspeed)
718 continue;
719 } else {
720 if (!c->fullspeed)
721 continue;
722 }
723 count++;
724 }
725 return count;
726}
727
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300728/**
729 * bos_desc() - prepares the BOS descriptor.
730 * @cdev: pointer to usb_composite device to generate the bos
731 * descriptor for
732 *
733 * This function generates the BOS (Binary Device Object)
734 * descriptor and its device capabilities descriptors. The BOS
735 * descriptor should be supported by a SuperSpeed device.
736 */
737static int bos_desc(struct usb_composite_dev *cdev)
738{
739 struct usb_ext_cap_descriptor *usb_ext;
740 struct usb_ss_cap_descriptor *ss_cap;
741 struct usb_dcd_config_params dcd_config_params;
742 struct usb_bos_descriptor *bos = cdev->req->buf;
743
744 bos->bLength = USB_DT_BOS_SIZE;
745 bos->bDescriptorType = USB_DT_BOS;
746
747 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
748 bos->bNumDeviceCaps = 0;
749
750 /*
751 * A SuperSpeed device shall include the USB2.0 extension descriptor
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800752 * and shall support LPM when operating in USB2.0 HS mode, as well as
753 * a HS device when operating in USB2.1 HS mode.
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300754 */
755 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
756 bos->bNumDeviceCaps++;
757 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
758 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
759 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
760 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500761 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300762
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800763 if (gadget_is_superspeed(cdev->gadget)) {
764 /*
765 * The Superspeed USB Capability descriptor shall be
766 * implemented by all SuperSpeed devices.
767 */
768 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
769 bos->bNumDeviceCaps++;
770 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
771 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
772 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
773 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
774 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
775 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
776 USB_FULL_SPEED_OPERATION |
777 USB_HIGH_SPEED_OPERATION |
778 USB_5GBPS_OPERATION);
779 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300780
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800781 /* Get Controller configuration */
782 if (cdev->gadget->ops->get_config_params)
783 cdev->gadget->ops->get_config_params
784 (&dcd_config_params);
785 else {
786 dcd_config_params.bU1devExitLat =
787 USB_DEFAULT_U1_DEV_EXIT_LAT;
788 dcd_config_params.bU2DevExitLat =
789 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
790 }
791 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
792 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300793 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300794
John Younf228a8d2016-02-05 17:05:53 -0800795 /* The SuperSpeedPlus USB Device Capability descriptor */
796 if (gadget_is_superspeed_plus(cdev->gadget)) {
797 struct usb_ssp_cap_descriptor *ssp_cap;
798
799 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
800 bos->bNumDeviceCaps++;
801
802 /*
803 * Report typical values.
804 */
805
806 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
807 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
808 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
809 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700810 ssp_cap->bReserved = 0;
811 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800812
813 /* SSAC = 1 (2 attributes) */
814 ssp_cap->bmAttributes = cpu_to_le32(1);
815
816 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700817 ssp_cap->wFunctionalitySupport =
818 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800819
820 /*
821 * bmSublinkSpeedAttr[0]:
822 * ST = Symmetric, RX
823 * LSE = 3 (Gbps)
824 * LP = 1 (SuperSpeedPlus)
825 * LSM = 10 (10 Gbps)
826 */
827 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700828 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800829 /*
830 * bmSublinkSpeedAttr[1] =
831 * ST = Symmetric, TX
832 * LSE = 3 (Gbps)
833 * LP = 1 (SuperSpeedPlus)
834 * LSM = 10 (10 Gbps)
835 */
836 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700837 cpu_to_le32((3 << 4) | (1 << 14) |
838 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800839 }
840
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300841 return le16_to_cpu(bos->wTotalLength);
842}
843
David Brownell40982be2008-06-19 17:52:58 -0700844static void device_qual(struct usb_composite_dev *cdev)
845{
846 struct usb_qualifier_descriptor *qual = cdev->req->buf;
847
848 qual->bLength = sizeof(*qual);
849 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
850 /* POLICY: same bcdUSB and device type info at both speeds */
851 qual->bcdUSB = cdev->desc.bcdUSB;
852 qual->bDeviceClass = cdev->desc.bDeviceClass;
853 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
854 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
855 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200856 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700857 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700858 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700859}
860
861/*-------------------------------------------------------------------------*/
862
863static void reset_config(struct usb_composite_dev *cdev)
864{
865 struct usb_function *f;
866
867 DBG(cdev, "reset config\n");
868
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +0530869 if (!cdev->config) {
870 pr_err("%s:cdev->config is already NULL\n", __func__);
871 return;
872 }
873
David Brownell40982be2008-06-19 17:52:58 -0700874 list_for_each_entry(f, &cdev->config->functions, list) {
875 if (f->disable)
876 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200877
Danny Segalf83e4512016-12-06 15:35:24 -0800878 /* USB 3.0 addition */
879 f->func_is_suspended = false;
880 f->func_wakeup_allowed = false;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300881 f->func_wakeup_pending = false;
Danny Segalf83e4512016-12-06 15:35:24 -0800882
Laurent Pinchart52426582009-10-21 00:03:38 +0200883 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700884 }
885 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100886 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700887}
888
889static int set_config(struct usb_composite_dev *cdev,
890 const struct usb_ctrlrequest *ctrl, unsigned number)
891{
892 struct usb_gadget *gadget = cdev->gadget;
893 struct usb_configuration *c = NULL;
894 int result = -EINVAL;
jianzhou21ae58e2020-03-24 11:07:44 +0800895 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
David Brownell40982be2008-06-19 17:52:58 -0700896 int tmp;
897
David Brownell40982be2008-06-19 17:52:58 -0700898 if (number) {
899 list_for_each_entry(c, &cdev->configs, list) {
900 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300901 /*
902 * We disable the FDs of the previous
903 * configuration only if the new configuration
904 * is a valid one
905 */
906 if (cdev->config)
907 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700908 result = 0;
909 break;
910 }
911 }
912 if (result < 0)
913 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300914 } else { /* Zero configuration value - need to reset the config */
915 if (cdev->config)
916 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700917 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300918 }
David Brownell40982be2008-06-19 17:52:58 -0700919
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200920 INFO(cdev, "%s config #%d: %s\n",
921 usb_speed_string(gadget->speed),
922 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700923
924 if (!c)
925 goto done;
926
Aniket Randive0a78a292020-03-18 11:22:09 +0530927 place_marker("M - USB Device is enumerated");
Peter Chen6027f312014-04-29 13:26:28 +0800928 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700929 cdev->config = c;
Jack Phamdb943d62016-12-16 11:21:16 -0800930 c->num_ineps_used = 0;
931 c->num_outeps_used = 0;
David Brownell40982be2008-06-19 17:52:58 -0700932
933 /* Initialize all interfaces by setting them to altsetting zero. */
934 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
935 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200936 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700937
938 if (!f)
939 break;
940
Laurent Pinchart52426582009-10-21 00:03:38 +0200941 /*
942 * Record which endpoints are used by the function. This is used
943 * to dispatch control requests targeted at that endpoint to the
944 * function's setup callback instead of the current
945 * configuration's setup callback.
946 */
John Younf3bdbe32016-02-05 17:07:03 -0800947 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200948
949 for (; *descriptors; ++descriptors) {
950 struct usb_endpoint_descriptor *ep;
951 int addr;
952
953 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
954 continue;
955
956 ep = (struct usb_endpoint_descriptor *)*descriptors;
957 addr = ((ep->bEndpointAddress & 0x80) >> 3)
958 | (ep->bEndpointAddress & 0x0f);
959 set_bit(addr, f->endpoints);
Jack Phamdb943d62016-12-16 11:21:16 -0800960 if (usb_endpoint_dir_in(ep))
961 c->num_ineps_used++;
962 else
963 c->num_outeps_used++;
Laurent Pinchart52426582009-10-21 00:03:38 +0200964 }
965
David Brownell40982be2008-06-19 17:52:58 -0700966 result = f->set_alt(f, tmp, 0);
967 if (result < 0) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530968 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700969 tmp, f->name, f, result);
970
971 reset_config(cdev);
972 goto done;
973 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300974
975 if (result == USB_GADGET_DELAYED_STATUS) {
976 DBG(cdev,
977 "%s: interface %d (%s) requested delayed status\n",
978 __func__, tmp, f->name);
979 cdev->delayed_status++;
980 DBG(cdev, "delayed_status count %d\n",
981 cdev->delayed_status);
982 }
David Brownell40982be2008-06-19 17:52:58 -0700983 }
984
jianzhou52d3ea82020-03-24 11:22:12 +0800985 /* when we return, be sure our power usage is valid */
986 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
Jack Pham65967542020-01-30 19:10:36 -0800987 if (gadget->speed < USB_SPEED_SUPER)
988 power = min(power, 500U);
989 else
990 power = min(power, 900U);
David Brownell40982be2008-06-19 17:52:58 -0700991done:
Thinh Nguyene3a0f742020-02-03 18:05:32 -0800992 if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
993 usb_gadget_set_selfpowered(gadget);
994 else
995 usb_gadget_clear_selfpowered(gadget);
996
jianzhou21ae58e2020-03-24 11:07:44 +0800997 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300998 if (result >= 0 && cdev->delayed_status)
999 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -07001000 return result;
1001}
1002
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001003int usb_add_config_only(struct usb_composite_dev *cdev,
1004 struct usb_configuration *config)
1005{
1006 struct usb_configuration *c;
1007
1008 if (!config->bConfigurationValue)
1009 return -EINVAL;
1010
1011 /* Prevent duplicate configuration identifiers */
1012 list_for_each_entry(c, &cdev->configs, list) {
1013 if (c->bConfigurationValue == config->bConfigurationValue)
1014 return -EBUSY;
1015 }
1016
1017 config->cdev = cdev;
1018 list_add_tail(&config->list, &cdev->configs);
1019
1020 INIT_LIST_HEAD(&config->functions);
1021 config->next_interface_id = 0;
1022 memset(config->interface, 0, sizeof(config->interface));
1023
1024 return 0;
1025}
1026EXPORT_SYMBOL_GPL(usb_add_config_only);
1027
David Brownell40982be2008-06-19 17:52:58 -07001028/**
1029 * usb_add_config() - add a configuration to a device.
1030 * @cdev: wraps the USB gadget
1031 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001032 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -07001033 * Context: single threaded during gadget setup
1034 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001035 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -07001036 * add each of the configurations it supports, using this routine.
1037 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001038 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -07001039 * is zero for success else a negative errno value. Binding configurations
1040 * assigns global resources including string IDs, and per-configuration
1041 * resources such as interface IDs and endpoints.
1042 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001043int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001044 struct usb_configuration *config,
1045 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001046{
1047 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001048
1049 if (!bind)
1050 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001051
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301052 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001053 config->bConfigurationValue,
1054 config->label, config);
1055
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001056 status = usb_add_config_only(cdev, config);
1057 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001058 goto done;
1059
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001060 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001061 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001062 while (!list_empty(&config->functions)) {
1063 struct usb_function *f;
1064
1065 f = list_first_entry(&config->functions,
1066 struct usb_function, list);
1067 list_del(&f->list);
1068 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301069 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001070 f->name, f);
1071 f->unbind(config, f);
1072 /* may free memory for "f" */
1073 }
1074 }
David Brownell40982be2008-06-19 17:52:58 -07001075 list_del(&config->list);
1076 config->cdev = NULL;
1077 } else {
1078 unsigned i;
1079
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301080 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001081 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001082 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001083 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001084 config->highspeed ? " high" : "",
1085 config->fullspeed
1086 ? (gadget_is_dualspeed(cdev->gadget)
1087 ? " full"
1088 : " full/low")
1089 : "");
1090
1091 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1092 struct usb_function *f = config->interface[i];
1093
1094 if (!f)
1095 continue;
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301096 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001097 i, f->name, f);
1098 }
1099 }
1100
Robert Baldygaf871cb92015-09-16 12:10:39 +02001101 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001102 usb_ep_autoconfig_reset(cdev->gadget);
1103
1104done:
1105 if (status)
1106 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1107 config->bConfigurationValue, status);
1108 return status;
1109}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001110EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001111
Benoit Goby51cce6f2012-05-10 10:07:57 +02001112static void remove_config(struct usb_composite_dev *cdev,
1113 struct usb_configuration *config)
1114{
1115 while (!list_empty(&config->functions)) {
1116 struct usb_function *f;
1117
1118 f = list_first_entry(&config->functions,
1119 struct usb_function, list);
1120 list_del(&f->list);
1121 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301122 DBG(cdev, "unbind function '%s'/%pK\n", f->name, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001123 f->unbind(config, f);
1124 /* may free memory for "f" */
1125 }
1126 }
1127 list_del(&config->list);
1128 if (config->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301129 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001130 config->unbind(config);
1131 /* may free memory for "c" */
1132 }
1133}
1134
1135/**
1136 * usb_remove_config() - remove a configuration from a device.
1137 * @cdev: wraps the USB gadget
1138 * @config: the configuration
1139 *
1140 * Drivers must call usb_gadget_disconnect before calling this function
1141 * to disconnect the device from the host and make sure the host will not
1142 * try to enumerate the device while we are changing the config list.
1143 */
1144void usb_remove_config(struct usb_composite_dev *cdev,
1145 struct usb_configuration *config)
1146{
1147 unsigned long flags;
1148
1149 spin_lock_irqsave(&cdev->lock, flags);
1150
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301151 if (cdev->config == config) {
1152 if (cdev->gadget->is_chipidea && !cdev->suspended) {
1153 spin_unlock_irqrestore(&cdev->lock, flags);
1154 msm_do_bam_disable_enable(CI_CTRL);
1155 spin_lock_irqsave(&cdev->lock, flags);
1156 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001157 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301158 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001159
1160 spin_unlock_irqrestore(&cdev->lock, flags);
1161
1162 remove_config(cdev, config);
1163}
1164
David Brownell40982be2008-06-19 17:52:58 -07001165/*-------------------------------------------------------------------------*/
1166
1167/* We support strings in multiple languages ... string descriptor zero
1168 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001169 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001170 * the host side.
1171 */
1172
1173static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1174{
1175 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001176 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001177 __le16 *tmp;
1178
1179 while (*sp) {
1180 s = *sp;
1181 language = cpu_to_le16(s->language);
1182 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1183 if (*tmp == language)
1184 goto repeat;
1185 }
1186 *tmp++ = language;
1187repeat:
1188 sp++;
1189 }
1190}
1191
1192static int lookup_string(
1193 struct usb_gadget_strings **sp,
1194 void *buf,
1195 u16 language,
1196 int id
1197)
1198{
1199 struct usb_gadget_strings *s;
1200 int value;
1201
1202 while (*sp) {
1203 s = *sp++;
1204 if (s->language != language)
1205 continue;
1206 value = usb_gadget_get_string(s, id, buf);
1207 if (value > 0)
1208 return value;
1209 }
1210 return -EINVAL;
1211}
1212
1213static int get_string(struct usb_composite_dev *cdev,
1214 void *buf, u16 language, int id)
1215{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001216 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001217 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001218 struct usb_configuration *c;
1219 struct usb_function *f;
1220 int len;
1221
Diego Violaad4676a2015-05-31 15:52:41 -03001222 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001223 * folk will ever care about ... also, it's all supported here.
1224 * (Except for UTF8 support for Unicode's "Astral Planes".)
1225 */
1226
1227 /* 0 == report all available language codes */
1228 if (id == 0) {
1229 struct usb_string_descriptor *s = buf;
1230 struct usb_gadget_strings **sp;
1231
1232 memset(s, 0, 256);
1233 s->bDescriptorType = USB_DT_STRING;
1234
1235 sp = composite->strings;
1236 if (sp)
1237 collect_langs(sp, s->wData);
1238
1239 list_for_each_entry(c, &cdev->configs, list) {
1240 sp = c->strings;
1241 if (sp)
1242 collect_langs(sp, s->wData);
1243
1244 list_for_each_entry(f, &c->functions, list) {
1245 sp = f->strings;
1246 if (sp)
1247 collect_langs(sp, s->wData);
1248 }
1249 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001250 list_for_each_entry(uc, &cdev->gstrings, list) {
1251 struct usb_gadget_strings **sp;
1252
1253 sp = get_containers_gs(uc);
1254 collect_langs(sp, s->wData);
1255 }
David Brownell40982be2008-06-19 17:52:58 -07001256
Roel Kluin417b57b2009-08-06 16:09:51 -07001257 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001258 continue;
1259 if (!len)
1260 return -EINVAL;
1261
1262 s->bLength = 2 * (len + 1);
1263 return s->bLength;
1264 }
1265
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001266 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1267 struct usb_os_string *b = buf;
1268 b->bLength = sizeof(*b);
1269 b->bDescriptorType = USB_DT_STRING;
1270 compiletime_assert(
1271 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1272 "qwSignature size must be equal to qw_sign");
1273 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1274 b->bMS_VendorCode = cdev->b_vendor_code;
1275 b->bPad = 0;
1276 return sizeof(*b);
1277 }
1278
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001279 list_for_each_entry(uc, &cdev->gstrings, list) {
1280 struct usb_gadget_strings **sp;
1281
1282 sp = get_containers_gs(uc);
1283 len = lookup_string(sp, buf, language, id);
1284 if (len > 0)
1285 return len;
1286 }
1287
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001288 /* String IDs are device-scoped, so we look up each string
1289 * table we're told about. These lookups are infrequent;
1290 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001291 */
1292 if (composite->strings) {
1293 len = lookup_string(composite->strings, buf, language, id);
1294 if (len > 0)
1295 return len;
1296 }
1297 list_for_each_entry(c, &cdev->configs, list) {
1298 if (c->strings) {
1299 len = lookup_string(c->strings, buf, language, id);
1300 if (len > 0)
1301 return len;
1302 }
1303 list_for_each_entry(f, &c->functions, list) {
1304 if (!f->strings)
1305 continue;
1306 len = lookup_string(f->strings, buf, language, id);
1307 if (len > 0)
1308 return len;
1309 }
1310 }
1311 return -EINVAL;
1312}
1313
1314/**
1315 * usb_string_id() - allocate an unused string ID
1316 * @cdev: the device whose string descriptor IDs are being allocated
1317 * Context: single threaded during gadget setup
1318 *
1319 * @usb_string_id() is called from bind() callbacks to allocate
1320 * string IDs. Drivers for functions, configurations, or gadgets will
1321 * then store that ID in the appropriate descriptors and string table.
1322 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001323 * All string identifier should be allocated using this,
1324 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1325 * that for example different functions don't wrongly assign different
1326 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001327 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001328int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001329{
1330 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001331 /* string id 0 is reserved by USB spec for list of
1332 * supported languages */
1333 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001334 cdev->next_string_id++;
1335 return cdev->next_string_id;
1336 }
1337 return -ENODEV;
1338}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001339EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001340
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001341/**
1342 * usb_string_ids() - allocate unused string IDs in batch
1343 * @cdev: the device whose string descriptor IDs are being allocated
1344 * @str: an array of usb_string objects to assign numbers to
1345 * Context: single threaded during gadget setup
1346 *
1347 * @usb_string_ids() is called from bind() callbacks to allocate
1348 * string IDs. Drivers for functions, configurations, or gadgets will
1349 * then copy IDs from the string table to the appropriate descriptors
1350 * and string table for other languages.
1351 *
1352 * All string identifier should be allocated using this,
1353 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1354 * example different functions don't wrongly assign different meanings
1355 * to the same identifier.
1356 */
1357int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1358{
1359 int next = cdev->next_string_id;
1360
1361 for (; str->s; ++str) {
1362 if (unlikely(next >= 254))
1363 return -ENODEV;
1364 str->id = ++next;
1365 }
1366
1367 cdev->next_string_id = next;
1368
1369 return 0;
1370}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001371EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001372
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001373static struct usb_gadget_string_container *copy_gadget_strings(
1374 struct usb_gadget_strings **sp, unsigned n_gstrings,
1375 unsigned n_strings)
1376{
1377 struct usb_gadget_string_container *uc;
1378 struct usb_gadget_strings **gs_array;
1379 struct usb_gadget_strings *gs;
1380 struct usb_string *s;
1381 unsigned mem;
1382 unsigned n_gs;
1383 unsigned n_s;
1384 void *stash;
1385
1386 mem = sizeof(*uc);
1387 mem += sizeof(void *) * (n_gstrings + 1);
1388 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1389 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1390 uc = kmalloc(mem, GFP_KERNEL);
1391 if (!uc)
1392 return ERR_PTR(-ENOMEM);
1393 gs_array = get_containers_gs(uc);
1394 stash = uc->stash;
1395 stash += sizeof(void *) * (n_gstrings + 1);
1396 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1397 struct usb_string *org_s;
1398
1399 gs_array[n_gs] = stash;
1400 gs = gs_array[n_gs];
1401 stash += sizeof(struct usb_gadget_strings);
1402 gs->language = sp[n_gs]->language;
1403 gs->strings = stash;
1404 org_s = sp[n_gs]->strings;
1405
1406 for (n_s = 0; n_s < n_strings; n_s++) {
1407 s = stash;
1408 stash += sizeof(struct usb_string);
1409 if (org_s->s)
1410 s->s = org_s->s;
1411 else
1412 s->s = "";
1413 org_s++;
1414 }
1415 s = stash;
1416 s->s = NULL;
1417 stash += sizeof(struct usb_string);
1418
1419 }
1420 gs_array[n_gs] = NULL;
1421 return uc;
1422}
1423
1424/**
1425 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1426 * @cdev: the device whose string descriptor IDs are being allocated
1427 * and attached.
1428 * @sp: an array of usb_gadget_strings to attach.
1429 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1430 *
1431 * This function will create a deep copy of usb_gadget_strings and usb_string
1432 * and attach it to the cdev. The actual string (usb_string.s) will not be
1433 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001434 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001435 * The ->language pointer of each struct usb_gadget_strings has to contain the
1436 * same amount of entries.
1437 * 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 +09001438 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001439 * entry of en-US. Therefore both entries become the same id assign.
1440 */
1441struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1442 struct usb_gadget_strings **sp, unsigned n_strings)
1443{
1444 struct usb_gadget_string_container *uc;
1445 struct usb_gadget_strings **n_gs;
1446 unsigned n_gstrings = 0;
1447 unsigned i;
1448 int ret;
1449
1450 for (i = 0; sp[i]; i++)
1451 n_gstrings++;
1452
1453 if (!n_gstrings)
1454 return ERR_PTR(-EINVAL);
1455
1456 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1457 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001458 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001459
1460 n_gs = get_containers_gs(uc);
1461 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1462 if (ret)
1463 goto err;
1464
1465 for (i = 1; i < n_gstrings; i++) {
1466 struct usb_string *m_s;
1467 struct usb_string *s;
1468 unsigned n;
1469
1470 m_s = n_gs[0]->strings;
1471 s = n_gs[i]->strings;
1472 for (n = 0; n < n_strings; n++) {
1473 s->id = m_s->id;
1474 s++;
1475 m_s++;
1476 }
1477 }
1478 list_add_tail(&uc->list, &cdev->gstrings);
1479 return n_gs[0]->strings;
1480err:
1481 kfree(uc);
1482 return ERR_PTR(ret);
1483}
1484EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1485
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001486/**
1487 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001488 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001489 * @n: number of string IDs to allocate
1490 * Context: single threaded during gadget setup
1491 *
1492 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001493 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001494 * is, returns last requested ID which is now very useful information.
1495 *
1496 * @usb_string_ids_n() is called from bind() callbacks to allocate
1497 * string IDs. Drivers for functions, configurations, or gadgets will
1498 * then store that ID in the appropriate descriptors and string table.
1499 *
1500 * All string identifier should be allocated using this,
1501 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1502 * example different functions don't wrongly assign different meanings
1503 * to the same identifier.
1504 */
1505int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1506{
1507 unsigned next = c->next_string_id;
1508 if (unlikely(n > 254 || (unsigned)next + n > 254))
1509 return -ENODEV;
1510 c->next_string_id += n;
1511 return next + 1;
1512}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001513EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001514
David Brownell40982be2008-06-19 17:52:58 -07001515/*-------------------------------------------------------------------------*/
1516
1517static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1518{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001519 struct usb_composite_dev *cdev;
1520
David Brownell40982be2008-06-19 17:52:58 -07001521 if (req->status || req->actual != req->length)
1522 DBG((struct usb_composite_dev *) ep->driver_data,
1523 "setup complete --> %d, %d/%d\n",
1524 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001525
1526 /*
1527 * REVIST The same ep0 requests are shared with function drivers
1528 * so they don't have to maintain the same ->complete() stubs.
1529 *
1530 * Because of that, we need to check for the validity of ->context
1531 * here, even though we know we've set it to something useful.
1532 */
1533 if (!req->context)
1534 return;
1535
1536 cdev = req->context;
1537
1538 if (cdev->req == req)
1539 cdev->setup_pending = false;
1540 else if (cdev->os_desc_req == req)
1541 cdev->os_desc_pending = false;
1542 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301543 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001544}
1545
1546static int composite_ep0_queue(struct usb_composite_dev *cdev,
1547 struct usb_request *req, gfp_t gfp_flags)
1548{
1549 int ret;
1550
1551 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1552 if (ret == 0) {
1553 if (cdev->req == req)
1554 cdev->setup_pending = true;
1555 else if (cdev->os_desc_req == req)
1556 cdev->os_desc_pending = true;
1557 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301558 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001559 }
1560
1561 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001562}
1563
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001564static int count_ext_compat(struct usb_configuration *c)
1565{
1566 int i, res;
1567
1568 res = 0;
1569 for (i = 0; i < c->next_interface_id; ++i) {
1570 struct usb_function *f;
1571 int j;
1572
1573 f = c->interface[i];
1574 for (j = 0; j < f->os_desc_n; ++j) {
1575 struct usb_os_desc *d;
1576
1577 if (i != f->os_desc_table[j].if_id)
1578 continue;
1579 d = f->os_desc_table[j].os_desc;
1580 if (d && d->ext_compat_id)
1581 ++res;
1582 }
1583 }
1584 BUG_ON(res > 255);
1585 return res;
1586}
1587
Chris Dickensbf54f312017-12-31 18:59:42 -08001588static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001589{
1590 int i, count;
1591
1592 count = 16;
1593 for (i = 0; i < c->next_interface_id; ++i) {
1594 struct usb_function *f;
1595 int j;
1596
1597 f = c->interface[i];
1598 for (j = 0; j < f->os_desc_n; ++j) {
1599 struct usb_os_desc *d;
1600
1601 if (i != f->os_desc_table[j].if_id)
1602 continue;
1603 d = f->os_desc_table[j].os_desc;
1604 if (d && d->ext_compat_id) {
1605 *buf++ = i;
1606 *buf++ = 0x01;
1607 memcpy(buf, d->ext_compat_id, 16);
1608 buf += 22;
1609 } else {
1610 ++buf;
1611 *buf = 0x01;
1612 buf += 23;
1613 }
1614 count += 24;
Chris Dickensbf54f312017-12-31 18:59:42 -08001615 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1616 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001617 }
1618 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001619
1620 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001621}
1622
1623static int count_ext_prop(struct usb_configuration *c, int interface)
1624{
1625 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001626 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001627
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301628 if (interface >= c->next_interface_id)
1629 return -EINVAL;
1630
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001631 f = c->interface[interface];
1632 for (j = 0; j < f->os_desc_n; ++j) {
1633 struct usb_os_desc *d;
1634
1635 if (interface != f->os_desc_table[j].if_id)
1636 continue;
1637 d = f->os_desc_table[j].os_desc;
1638 if (d && d->ext_compat_id)
1639 return d->ext_prop_count;
1640 }
Julia Lawall849b1332014-05-19 06:31:07 +02001641 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001642}
1643
1644static int len_ext_prop(struct usb_configuration *c, int interface)
1645{
1646 struct usb_function *f;
1647 struct usb_os_desc *d;
1648 int j, res;
1649
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301650 if (interface >= c->next_interface_id)
1651 return -EINVAL;
1652
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001653 res = 10; /* header length */
1654 f = c->interface[interface];
1655 for (j = 0; j < f->os_desc_n; ++j) {
1656 if (interface != f->os_desc_table[j].if_id)
1657 continue;
1658 d = f->os_desc_table[j].os_desc;
1659 if (d)
1660 return min(res + d->ext_prop_len, 4096);
1661 }
1662 return res;
1663}
1664
1665static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1666{
1667 struct usb_function *f;
1668 struct usb_os_desc *d;
1669 struct usb_os_desc_ext_prop *ext_prop;
1670 int j, count, n, ret;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001671
1672 f = c->interface[interface];
Chris Dickensbf54f312017-12-31 18:59:42 -08001673 count = 10; /* header length */
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001674 for (j = 0; j < f->os_desc_n; ++j) {
1675 if (interface != f->os_desc_table[j].if_id)
1676 continue;
1677 d = f->os_desc_table[j].os_desc;
1678 if (d)
1679 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
Chris Dickensbf54f312017-12-31 18:59:42 -08001680 n = ext_prop->data_len +
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001681 ext_prop->name_len + 14;
Chris Dickensbf54f312017-12-31 18:59:42 -08001682 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1683 return count;
1684 usb_ext_prop_put_size(buf, n);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001685 usb_ext_prop_put_type(buf, ext_prop->type);
1686 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1687 ext_prop->name_len);
1688 if (ret < 0)
1689 return ret;
1690 switch (ext_prop->type) {
1691 case USB_EXT_PROP_UNICODE:
1692 case USB_EXT_PROP_UNICODE_ENV:
1693 case USB_EXT_PROP_UNICODE_LINK:
1694 usb_ext_prop_put_unicode(buf, ret,
1695 ext_prop->data,
1696 ext_prop->data_len);
1697 break;
1698 case USB_EXT_PROP_BINARY:
1699 usb_ext_prop_put_binary(buf, ret,
1700 ext_prop->data,
1701 ext_prop->data_len);
1702 break;
1703 case USB_EXT_PROP_LE32:
1704 /* not implemented */
1705 case USB_EXT_PROP_BE32:
1706 /* not implemented */
1707 default:
1708 return -EINVAL;
1709 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001710 buf += n;
1711 count += n;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001712 }
1713 }
1714
Chris Dickensbf54f312017-12-31 18:59:42 -08001715 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001716}
1717
David Brownell40982be2008-06-19 17:52:58 -07001718/*
1719 * The setup() callback implements all the ep0 functionality that's
1720 * not handled lower down, in hardware or the hardware driver(like
1721 * device and endpoint feature flags, and their status). It's all
1722 * housekeeping for the gadget function we're implementing. Most of
1723 * the work is in config and function specific setup.
1724 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001725int
David Brownell40982be2008-06-19 17:52:58 -07001726composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1727{
1728 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1729 struct usb_request *req = cdev->req;
1730 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001731 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001732 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001733 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001734 u16 w_value = le16_to_cpu(ctrl->wValue);
1735 u16 w_length = le16_to_cpu(ctrl->wLength);
1736 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001737 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001738
Greg Kroah-Hartman8f0b3a02021-12-09 18:59:27 +01001739 if (w_length > USB_COMP_EP0_BUFSIZ) {
1740 if (ctrl->bRequestType == USB_DIR_OUT) {
1741 goto done;
1742 } else {
1743 /* Cast away the const, we are going to overwrite on purpose. */
1744 __le16 *temp = (__le16 *)&ctrl->wLength;
1745
1746 *temp = cpu_to_le16(USB_COMP_EP0_BUFSIZ);
1747 w_length = USB_COMP_EP0_BUFSIZ;
1748 }
1749 }
1750
David Brownell40982be2008-06-19 17:52:58 -07001751 /* partial re-init of the response message; the function or the
1752 * gadget might need to intercept e.g. a control-OUT completion
1753 * when we delegate to it.
1754 */
1755 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001756 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001757 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301758 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001759 gadget->ep0->driver_data = cdev;
1760
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001761 /*
1762 * Don't let non-standard requests match any of the cases below
1763 * by accident.
1764 */
1765 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1766 goto unknown;
1767
David Brownell40982be2008-06-19 17:52:58 -07001768 switch (ctrl->bRequest) {
1769
1770 /* we handle all standard USB descriptors */
1771 case USB_REQ_GET_DESCRIPTOR:
1772 if (ctrl->bRequestType != USB_DIR_IN)
1773 goto unknown;
1774 switch (w_value >> 8) {
1775
1776 case USB_DT_DEVICE:
1777 cdev->desc.bNumConfigurations =
1778 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001779 cdev->desc.bMaxPacketSize0 =
1780 cdev->gadget->ep0->maxpacket;
Mayank Rana72bf6d02016-12-19 14:48:38 -08001781 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001782 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001783 if (gadget->speed >= USB_SPEED_SUPER) {
Chunfeng Yun3069db12018-05-09 19:29:16 +08001784 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001785 cdev->desc.bMaxPacketSize0 = 9;
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301786 } else if (gadget->l1_supported
1787 && !disable_l1_for_hs) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001788 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001789 }
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301790 } else if (gadget->l1_supported
1791 && !disable_l1_for_hs) {
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001792 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1793 DBG(cdev, "Config HS device with LPM(L1)\n");
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001794 }
1795
David Brownell40982be2008-06-19 17:52:58 -07001796 value = min(w_length, (u16) sizeof cdev->desc);
1797 memcpy(req->buf, &cdev->desc, value);
1798 break;
1799 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001800 if (!gadget_is_dualspeed(gadget) ||
1801 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001802 break;
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301803 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001804 device_qual(cdev);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301805 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001806 value = min_t(int, w_length,
1807 sizeof(struct usb_qualifier_descriptor));
1808 break;
1809 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001810 if (!gadget_is_dualspeed(gadget) ||
1811 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001812 break;
1813 /* FALLTHROUGH */
1814 case USB_DT_CONFIG:
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301815 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001816 value = config_desc(cdev, w_value);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301817 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001818 if (value >= 0)
1819 value = min(w_length, (u16) value);
1820 break;
1821 case USB_DT_STRING:
1822 value = get_string(cdev, req->buf,
1823 w_index, w_value & 0xff);
1824 if (value >= 0)
1825 value = min(w_length, (u16) value);
1826 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001827 case USB_DT_BOS:
Sriharsha Allenki876ac062018-03-05 01:08:07 +05301828 if ((gadget_is_superspeed(gadget) &&
1829 (gadget->speed >= USB_SPEED_SUPER)) ||
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301830 (gadget->l1_supported
1831 && !disable_l1_for_hs)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001832 value = bos_desc(cdev);
1833 value = min(w_length, (u16) value);
1834 }
1835 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001836 case USB_DT_OTG:
1837 if (gadget_is_otg(gadget)) {
1838 struct usb_configuration *config;
1839 int otg_desc_len = 0;
1840
1841 if (cdev->config)
1842 config = cdev->config;
1843 else
1844 config = list_first_entry(
1845 &cdev->configs,
1846 struct usb_configuration, list);
1847 if (!config)
1848 goto done;
1849
1850 if (gadget->otg_caps &&
1851 (gadget->otg_caps->otg_rev >= 0x0200))
1852 otg_desc_len += sizeof(
1853 struct usb_otg20_descriptor);
1854 else
1855 otg_desc_len += sizeof(
1856 struct usb_otg_descriptor);
1857
1858 value = min_t(int, w_length, otg_desc_len);
1859 memcpy(req->buf, config->descriptors[0], value);
1860 }
1861 break;
David Brownell40982be2008-06-19 17:52:58 -07001862 }
1863 break;
1864
1865 /* any number of configs can work */
1866 case USB_REQ_SET_CONFIGURATION:
1867 if (ctrl->bRequestType != 0)
1868 goto unknown;
1869 if (gadget_is_otg(gadget)) {
1870 if (gadget->a_hnp_support)
1871 DBG(cdev, "HNP available\n");
1872 else if (gadget->a_alt_hnp_support)
1873 DBG(cdev, "HNP on another port\n");
1874 else
1875 VDBG(cdev, "HNP inactive\n");
1876 }
1877 spin_lock(&cdev->lock);
1878 value = set_config(cdev, ctrl, w_value);
1879 spin_unlock(&cdev->lock);
1880 break;
1881 case USB_REQ_GET_CONFIGURATION:
1882 if (ctrl->bRequestType != USB_DIR_IN)
1883 goto unknown;
1884 if (cdev->config)
1885 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1886 else
1887 *(u8 *)req->buf = 0;
1888 value = min(w_length, (u16) 1);
1889 break;
1890
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001891 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001892 case USB_REQ_SET_INTERFACE:
1893 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1894 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001895 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001896 break;
Bryan Wu08889512009-01-08 00:21:19 +08001897 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001898 if (!f)
1899 break;
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001900
1901 /*
1902 * If there's no get_alt() method, we know only altsetting zero
1903 * works. There is no need to check if set_alt() is not NULL
1904 * as we check this in usb_add_function().
1905 */
1906 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001907 break;
Chunfeng Yun121621e2018-05-25 17:24:57 +08001908
1909 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001910 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001911 if (value == USB_GADGET_DELAYED_STATUS) {
1912 DBG(cdev,
1913 "%s: interface %d (%s) requested delayed status\n",
1914 __func__, intf, f->name);
1915 cdev->delayed_status++;
1916 DBG(cdev, "delayed_status count %d\n",
1917 cdev->delayed_status);
1918 }
Chunfeng Yun121621e2018-05-25 17:24:57 +08001919 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001920 break;
1921 case USB_REQ_GET_INTERFACE:
1922 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1923 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001924 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001925 break;
Bryan Wu08889512009-01-08 00:21:19 +08001926 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001927 if (!f)
1928 break;
1929 /* lots of interfaces only need altsetting zero... */
1930 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1931 if (value < 0)
1932 break;
1933 *((u8 *)req->buf) = value;
1934 value = min(w_length, (u16) 1);
1935 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001936 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001937 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1938 (w_index == OTG_STS_SELECTOR)) {
1939 if (ctrl->bRequestType != (USB_DIR_IN |
1940 USB_RECIP_DEVICE))
1941 goto unknown;
1942 *((u8 *)req->buf) = gadget->host_request_flag;
1943 value = 1;
1944 break;
1945 }
1946
1947 /*
1948 * USB 3.0 additions:
1949 * Function driver should handle get_status request. If such cb
1950 * wasn't supplied we respond with default value = 0
1951 * Note: function driver should supply such cb only for the
1952 * first interface of the function
1953 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001954 if (!gadget_is_superspeed(gadget))
1955 goto unknown;
1956 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1957 goto unknown;
1958 value = 2; /* This is the length of the get_status reply */
1959 put_unaligned_le16(0, req->buf);
1960 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1961 break;
1962 f = cdev->config->interface[intf];
1963 if (!f)
1964 break;
1965 status = f->get_status ? f->get_status(f) : 0;
1966 if (status < 0)
1967 break;
1968 put_unaligned_le16(status & 0x0000ffff, req->buf);
1969 break;
1970 /*
1971 * Function drivers should handle SetFeature/ClearFeature
1972 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1973 * only for the first interface of the function
1974 */
1975 case USB_REQ_CLEAR_FEATURE:
1976 case USB_REQ_SET_FEATURE:
1977 if (!gadget_is_superspeed(gadget))
1978 goto unknown;
1979 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1980 goto unknown;
1981 switch (w_value) {
1982 case USB_INTRF_FUNC_SUSPEND:
1983 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1984 break;
1985 f = cdev->config->interface[intf];
1986 if (!f)
1987 break;
1988 value = 0;
Danny Segalf83e4512016-12-06 15:35:24 -08001989 if (f->func_suspend) {
1990 const u8 suspend_opt = w_index >> 8;
1991
1992 value = f->func_suspend(f, suspend_opt);
1993 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1994 f->name ? f->name : "", suspend_opt);
1995 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001996 if (value < 0) {
1997 ERROR(cdev,
1998 "func_suspend() returned error %d\n",
1999 value);
2000 value = 0;
2001 }
2002 break;
2003 }
2004 break;
David Brownell40982be2008-06-19 17:52:58 -07002005 default:
2006unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002007 /*
2008 * OS descriptors handling
2009 */
2010 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01002011 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002012 ctrl->bRequest == cdev->b_vendor_code) {
2013 struct usb_request *req;
2014 struct usb_configuration *os_desc_cfg;
2015 u8 *buf;
2016 int interface;
2017 int count = 0;
2018
2019 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05002020 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002021 req->complete = composite_setup_complete;
2022 buf = req->buf;
2023 os_desc_cfg = cdev->os_desc_config;
Chris Dickensbf54f312017-12-31 18:59:42 -08002024 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002025 memset(buf, 0, w_length);
2026 buf[5] = 0x01;
2027 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2028 case USB_RECIP_DEVICE:
2029 if (w_index != 0x4 || (w_value >> 8))
2030 break;
2031 buf[6] = w_index;
2032 if (w_length == 0x10) {
2033 /* Number of ext compat interfaces */
2034 count = count_ext_compat(os_desc_cfg);
2035 buf[8] = count;
2036 count *= 24; /* 24 B/ext compat desc */
2037 count += 16; /* header */
2038 put_unaligned_le32(count, buf);
2039 value = w_length;
2040 } else {
2041 /* "extended compatibility ID"s */
2042 count = count_ext_compat(os_desc_cfg);
2043 buf[8] = count;
2044 count *= 24; /* 24 B/ext compat desc */
2045 count += 16; /* header */
2046 put_unaligned_le32(count, buf);
2047 buf += 16;
Chris Dickensbf54f312017-12-31 18:59:42 -08002048 value = fill_ext_compat(os_desc_cfg, buf);
2049 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002050 }
2051 break;
2052 case USB_RECIP_INTERFACE:
2053 if (w_index != 0x5 || (w_value >> 8))
2054 break;
2055 interface = w_value & 0xFF;
2056 buf[6] = w_index;
2057 if (w_length == 0x0A) {
2058 count = count_ext_prop(os_desc_cfg,
2059 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302060 if (count < 0)
2061 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002062 put_unaligned_le16(count, buf + 8);
2063 count = len_ext_prop(os_desc_cfg,
2064 interface);
2065 put_unaligned_le32(count, buf);
2066
2067 value = w_length;
2068 } else {
2069 count = count_ext_prop(os_desc_cfg,
2070 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302071 if (count < 0)
2072 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002073 put_unaligned_le16(count, buf + 8);
2074 count = len_ext_prop(os_desc_cfg,
2075 interface);
2076 put_unaligned_le32(count, buf);
2077 buf += 10;
2078 value = fill_ext_prop(os_desc_cfg,
2079 interface, buf);
2080 if (value < 0)
2081 return value;
Chris Dickensbf54f312017-12-31 18:59:42 -08002082 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002083 }
2084 break;
2085 }
William Wu7e14f47a2016-05-13 18:30:42 +08002086
2087 if (value >= 0) {
2088 req->length = value;
2089 req->context = cdev;
2090 req->zero = value < w_length;
2091 value = composite_ep0_queue(cdev, req,
2092 GFP_ATOMIC);
2093 if (value < 0) {
2094 DBG(cdev, "ep_queue --> %d\n", value);
2095 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302096 if (value != -ESHUTDOWN)
2097 composite_setup_complete(
2098 gadget->ep0, req);
William Wu7e14f47a2016-05-13 18:30:42 +08002099 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002100 }
2101 return value;
2102 }
2103
David Brownell40982be2008-06-19 17:52:58 -07002104 VDBG(cdev,
2105 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2106 ctrl->bRequestType, ctrl->bRequest,
2107 w_value, w_index, w_length);
2108
Laurent Pinchart52426582009-10-21 00:03:38 +02002109 /* functions always handle their interfaces and endpoints...
2110 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002111 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002112 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302113 if (cdev->config) {
2114 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b452016-06-22 01:12:08 +02002115 if (f->req_match &&
2116 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302117 goto try_fun_setup;
Felix Hädicke1a00b452016-06-22 01:12:08 +02002118 } else {
2119 struct usb_configuration *c;
2120 list_for_each_entry(c, &cdev->configs, list)
2121 list_for_each_entry(f, &c->functions, list)
2122 if (f->req_match &&
2123 f->req_match(f, ctrl, true))
2124 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302125 }
Felix Hädicke1a00b452016-06-22 01:12:08 +02002126 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302127
Laurent Pinchart52426582009-10-21 00:03:38 +02002128 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2129 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002130 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302131 break;
2132 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002133 break;
2134
2135 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002136 if (!cdev->config)
2137 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002138 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2139 list_for_each_entry(f, &cdev->config->functions, list) {
2140 if (test_bit(endp, f->endpoints))
2141 break;
2142 }
2143 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002144 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002145 break;
David Brownell40982be2008-06-19 17:52:58 -07002146 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002147try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002148 if (f && f->setup)
2149 value = f->setup(f, ctrl);
2150 else {
David Brownell40982be2008-06-19 17:52:58 -07002151 struct usb_configuration *c;
2152
2153 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002154 if (!c)
2155 goto done;
2156
2157 /* try current config's setup */
2158 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002159 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002160 goto done;
2161 }
2162
2163 /* try the only function in the current config */
2164 if (!list_is_singular(&c->functions))
2165 goto done;
2166 f = list_first_entry(&c->functions, struct usb_function,
2167 list);
2168 if (f->setup)
2169 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002170 }
Vijayavardhan Vennapusa8edd67762013-10-17 15:00:02 +05302171 if (value == USB_GADGET_DELAYED_STATUS) {
2172 DBG(cdev,
2173 "%s: interface %d (%s) requested delayed status\n",
2174 __func__, intf, f->name);
2175 cdev->delayed_status++;
2176 DBG(cdev, "delayed_status count %d\n",
2177 cdev->delayed_status);
2178 }
David Brownell40982be2008-06-19 17:52:58 -07002179
2180 goto done;
2181 }
2182
2183 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002184 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002185 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002186 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002187 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002188 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002189 if (value < 0) {
2190 DBG(cdev, "ep_queue --> %d\n", value);
2191 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302192 if (value != -ESHUTDOWN)
2193 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002194 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002195 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2196 WARN(cdev,
2197 "%s: Delayed status not supported for w_length != 0",
2198 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002199 }
2200
2201done:
2202 /* device either stalls (value < 0) or reports success */
2203 return value;
2204}
2205
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002206void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002207{
2208 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2209 unsigned long flags;
2210
Badhri Jagan Sridharan9e954092015-05-06 13:40:15 -07002211 if (cdev == NULL) {
2212 WARN(1, "%s: Calling disconnect on a Gadget that is \
2213 not connected\n", __func__);
2214 return;
2215 }
2216
David Brownell40982be2008-06-19 17:52:58 -07002217 /* REVISIT: should we have config and device level
2218 * disconnect callbacks?
2219 */
2220 spin_lock_irqsave(&cdev->lock, flags);
Benjamin Herrenschmidtd0237652019-07-26 14:59:03 +10002221 cdev->suspended = 0;
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302222 if (cdev->config) {
2223 if (gadget->is_chipidea && !cdev->suspended) {
2224 spin_unlock_irqrestore(&cdev->lock, flags);
2225 msm_do_bam_disable_enable(CI_CTRL);
2226 spin_lock_irqsave(&cdev->lock, flags);
2227 }
David Brownell40982be2008-06-19 17:52:58 -07002228 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302229 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002230 if (cdev->driver->disconnect)
2231 cdev->driver->disconnect(cdev);
Pavankumar Kondeti47870672013-06-19 09:51:58 +05302232 if (cdev->delayed_status != 0) {
2233 INFO(cdev, "delayed status mismatch..resetting\n");
2234 cdev->delayed_status = 0;
2235 }
David Brownell40982be2008-06-19 17:52:58 -07002236 spin_unlock_irqrestore(&cdev->lock, flags);
2237}
2238
2239/*-------------------------------------------------------------------------*/
2240
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002241static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2242 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002243{
2244 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2245 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2246
Vijayavardhan Vennapusa5d2ac1a2017-09-13 15:41:20 +05302247 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002248}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002249static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002250
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002251static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002252{
2253 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002254 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2255 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002256
2257 /* composite_disconnect() must already have been called
2258 * by the underlying peripheral controller driver!
2259 * so there's no i/o concurrency that could affect the
2260 * state protected by cdev->lock.
2261 */
2262 WARN_ON(cdev->config);
2263
2264 while (!list_empty(&cdev->configs)) {
2265 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002266 c = list_first_entry(&cdev->configs,
2267 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002268 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002269 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002270 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002271 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002272
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002273 composite_dev_cleanup(cdev);
2274
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002275 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2276 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2277
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002278 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002279 kfree(cdev);
2280 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002281}
2282
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002283static void composite_unbind(struct usb_gadget *gadget)
2284{
2285 __composite_unbind(gadget, true);
2286}
2287
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002288static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2289 const struct usb_device_descriptor *old)
2290{
2291 __le16 idVendor;
2292 __le16 idProduct;
2293 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002294 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002295 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002296 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002297
2298 /*
2299 * these variables may have been set in
2300 * usb_composite_overwrite_options()
2301 */
2302 idVendor = new->idVendor;
2303 idProduct = new->idProduct;
2304 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002305 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002306 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002307 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002308
2309 *new = *old;
2310 if (idVendor)
2311 new->idVendor = idVendor;
2312 if (idProduct)
2313 new->idProduct = idProduct;
2314 if (bcdDevice)
2315 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002316 else
2317 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002318 if (iSerialNumber)
2319 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002320 if (iManufacturer)
2321 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002322 if (iProduct)
2323 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002324}
2325
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002326int composite_dev_prepare(struct usb_composite_driver *composite,
2327 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002328{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002329 struct usb_gadget *gadget = cdev->gadget;
2330 int ret = -ENOMEM;
2331
2332 /* preallocate control response and buffer */
2333 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2334 if (!cdev->req)
2335 return -ENOMEM;
2336
Sujeet Kumara6c0e992015-07-14 11:41:55 +05302337 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ +
2338 (gadget->extra_buf_alloc), GFP_KERNEL);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002339 if (!cdev->req->buf)
2340 goto fail;
2341
2342 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2343 if (ret)
2344 goto fail_dev;
2345
2346 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002347 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002348 gadget->ep0->driver_data = cdev;
2349
2350 cdev->driver = composite;
2351
2352 /*
2353 * As per USB compliance update, a device that is actively drawing
2354 * more than 100mA from USB must report itself as bus-powered in
2355 * the GetStatus(DEVICE) call.
2356 */
2357 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2358 usb_gadget_set_selfpowered(gadget);
2359
2360 /* interface and string IDs start at zero via kzalloc.
2361 * we force endpoints to start unassigned; few controller
2362 * drivers will zero ep->driver_data.
2363 */
2364 usb_ep_autoconfig_reset(gadget);
2365 return 0;
2366fail_dev:
2367 kfree(cdev->req->buf);
2368fail:
2369 usb_ep_free_request(gadget->ep0, cdev->req);
2370 cdev->req = NULL;
2371 return ret;
2372}
2373
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002374int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2375 struct usb_ep *ep0)
2376{
2377 int ret = 0;
2378
2379 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2380 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002381 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002382 goto end;
2383 }
2384
Chris Dickensbf54f312017-12-31 18:59:42 -08002385 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2386 GFP_KERNEL);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002387 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002388 ret = -ENOMEM;
Christophe JAILLETf0ee2032017-01-04 06:30:16 +01002389 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002390 goto end;
2391 }
Felipe Balbi57943712014-09-18 09:54:54 -05002392 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002393 cdev->os_desc_req->complete = composite_setup_complete;
2394end:
2395 return ret;
2396}
2397
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002398void composite_dev_cleanup(struct usb_composite_dev *cdev)
2399{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002400 struct usb_gadget_string_container *uc, *tmp;
2401
2402 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2403 list_del(&uc->list);
2404 kfree(uc);
2405 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002406 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002407 if (cdev->os_desc_pending)
2408 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2409
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002410 kfree(cdev->os_desc_req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002411 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002412 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002413 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002414 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002415 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002416 if (cdev->setup_pending)
2417 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2418
Li Junbe0a8882014-08-28 21:44:11 +08002419 kfree(cdev->req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002420 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002421 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002422 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002423 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002424 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002425 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002426}
2427
2428static int composite_bind(struct usb_gadget *gadget,
2429 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002430{
2431 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002432 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002433 int status = -ENOMEM;
2434
2435 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2436 if (!cdev)
2437 return status;
2438
2439 spin_lock_init(&cdev->lock);
2440 cdev->gadget = gadget;
2441 set_gadget_data(gadget, cdev);
2442 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002443 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002444
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002445 status = composite_dev_prepare(composite, cdev);
2446 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002447 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002448
2449 /* composite gadget needs to assign strings for whole device (like
2450 * serial number), register function drivers, potentially update
2451 * power state and consumption, etc
2452 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002453 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002454 if (status < 0)
2455 goto fail;
2456
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002457 if (cdev->use_os_string) {
2458 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2459 if (status)
2460 goto fail;
2461 }
2462
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002463 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002464
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002465 /* has userspace failed to provide a serial number? */
2466 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2467 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2468
David Brownell40982be2008-06-19 17:52:58 -07002469 INFO(cdev, "%s ready\n", composite->name);
2470 return 0;
2471
2472fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002473 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002474 return status;
2475}
2476
2477/*-------------------------------------------------------------------------*/
2478
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002479void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002480{
2481 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2482 struct usb_function *f;
Mayank Rana13e5c152015-10-05 19:08:47 -07002483 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002484
David Brownell89429392009-03-19 14:14:17 -07002485 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002486 * suspend/resume callbacks?
2487 */
2488 DBG(cdev, "suspend\n");
Mayank Rana13e5c152015-10-05 19:08:47 -07002489 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002490 if (cdev->config) {
2491 list_for_each_entry(f, &cdev->config->functions, list) {
2492 if (f->suspend)
2493 f->suspend(f);
2494 }
2495 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002496 if (cdev->driver->suspend)
2497 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002498
2499 cdev->suspended = 1;
Mayank Rana13e5c152015-10-05 19:08:47 -07002500 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002501
Thinh Nguyene3a0f742020-02-03 18:05:32 -08002502 usb_gadget_set_selfpowered(gadget);
Hao Wub23f2f92010-11-29 15:17:03 +08002503 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002504}
2505
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002506void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002507{
2508 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2509 struct usb_function *f;
Jack Pham65967542020-01-30 19:10:36 -08002510 unsigned maxpower;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302511 int ret;
2512 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002513
David Brownell89429392009-03-19 14:14:17 -07002514 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002515 * suspend/resume callbacks?
2516 */
Ajay Agarwal28022332020-06-29 13:39:29 +05302517 INFO(cdev, "USB Resume end\n");
Aniket Randive0a78a292020-03-18 11:22:09 +05302518 place_marker("M - USB device is resumed");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002519 if (cdev->driver->resume)
2520 cdev->driver->resume(cdev);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002521
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302522 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002523 if (cdev->config) {
2524 list_for_each_entry(f, &cdev->config->functions, list) {
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302525 if (f->func_wakeup_pending) {
2526 ret = usb_func_wakeup_int(f);
2527 if (ret) {
2528 if (ret == -EAGAIN) {
2529 ERROR(f->config->cdev,
2530 "Function wakeup for %s could not complete due to suspend state.\n",
2531 f->name ? f->name : "");
2532 } else if (ret != -ENOTSUPP) {
2533 ERROR(f->config->cdev,
2534 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2535 f->name ? f->name : "",
2536 ret);
2537 }
Danny Segalde7cd8d2014-07-28 18:08:33 +03002538 }
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302539 f->func_wakeup_pending = 0;
Danny Segal86cb50e2014-07-09 15:14:49 +03002540 }
2541
Sriharsha Allenki6ab82052018-09-24 18:21:44 +05302542 /*
2543 * Call function resume irrespective of the speed.
2544 * Individual function needs to retain the USB3 Function
2545 * suspend state through out the Device suspend entry
2546 * and exit process.
2547 */
2548 if (f->resume)
David Brownell40982be2008-06-19 17:52:58 -07002549 f->resume(f);
2550 }
Hao Wub23f2f92010-11-29 15:17:03 +08002551
Jack Pham65967542020-01-30 19:10:36 -08002552 maxpower = cdev->config->MaxPower ?
2553 cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
2554 if (gadget->speed < USB_SPEED_SUPER)
2555 maxpower = min(maxpower, 500U);
2556 else
2557 maxpower = min(maxpower, 900U);
jianzhou21ae58e2020-03-24 11:07:44 +08002558
Thinh Nguyene3a0f742020-02-03 18:05:32 -08002559 if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
2560 usb_gadget_clear_selfpowered(gadget);
2561
Jack Pham65967542020-01-30 19:10:36 -08002562 usb_gadget_vbus_draw(gadget, maxpower);
David Brownell40982be2008-06-19 17:52:58 -07002563 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002564
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302565 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002566 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002567}
2568
2569/*-------------------------------------------------------------------------*/
2570
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002571static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002572 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002573 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002574
2575 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002576 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002577 .disconnect = composite_disconnect,
2578
2579 .suspend = composite_suspend,
2580 .resume = composite_resume,
2581
2582 .driver = {
2583 .owner = THIS_MODULE,
2584 },
2585};
2586
2587/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002588 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002589 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002590 *
David Brownell40982be2008-06-19 17:52:58 -07002591 * Context: single threaded during gadget setup
2592 *
2593 * This function is used to register drivers using the composite driver
2594 * framework. The return value is zero, or a negative errno value.
2595 * Those values normally come from the driver's @bind method, which does
2596 * all the work of setting up the driver to match the hardware.
2597 *
2598 * On successful return, the gadget is ready to respond to requests from
2599 * the host, unless one of its components invokes usb_gadget_disconnect()
2600 * while it was binding. That would usually be done in order to wait for
2601 * some userspace participation.
2602 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002603int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002604{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002605 struct usb_gadget_driver *gadget_driver;
2606
2607 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002608 return -EINVAL;
2609
2610 if (!driver->name)
2611 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002612
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002613 driver->gadget_driver = composite_driver_template;
2614 gadget_driver = &driver->gadget_driver;
2615
2616 gadget_driver->function = (char *) driver->name;
2617 gadget_driver->driver.name = driver->name;
2618 gadget_driver->max_speed = driver->max_speed;
2619
2620 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002621}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002622EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002623
2624/**
2625 * usb_composite_unregister() - unregister a composite driver
2626 * @driver: the driver to unregister
2627 *
2628 * This function is used to unregister drivers using the composite
2629 * driver framework.
2630 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002631void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002632{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002633 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002634}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002635EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002636
2637/**
2638 * usb_composite_setup_continue() - Continue with the control transfer
2639 * @cdev: the composite device who's control transfer was kept waiting
2640 *
2641 * This function must be called by the USB function driver to continue
2642 * with the control transfer's data/status stage in case it had requested to
2643 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2644 * can request the composite framework to delay the setup request's data/status
2645 * stages by returning USB_GADGET_DELAYED_STATUS.
2646 */
2647void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2648{
2649 int value;
2650 struct usb_request *req = cdev->req;
2651 unsigned long flags;
2652
2653 DBG(cdev, "%s\n", __func__);
2654 spin_lock_irqsave(&cdev->lock, flags);
2655
2656 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302657 if (!cdev->config) {
2658 spin_unlock_irqrestore(&cdev->lock, flags);
2659 return;
2660 }
2661 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002662 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302663 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002664
2665 } else if (--cdev->delayed_status == 0) {
2666 DBG(cdev, "%s: Completing delayed status\n", __func__);
2667 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002668 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002669 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002670 if (value < 0) {
2671 DBG(cdev, "ep_queue --> %d\n", value);
2672 req->status = 0;
2673 composite_setup_complete(cdev->gadget->ep0, req);
2674 }
2675 }
2676
2677 spin_unlock_irqrestore(&cdev->lock, flags);
2678}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002679EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002680
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002681static char *composite_default_mfr(struct usb_gadget *gadget)
2682{
2683 char *mfr;
2684 int len;
2685
2686 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2687 init_utsname()->release, gadget->name);
2688 len++;
2689 mfr = kmalloc(len, GFP_KERNEL);
2690 if (!mfr)
2691 return NULL;
2692 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2693 init_utsname()->release, gadget->name);
2694 return mfr;
2695}
2696
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002697void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2698 struct usb_composite_overwrite *covr)
2699{
2700 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002701 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2702 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002703
2704 if (covr->idVendor)
2705 desc->idVendor = cpu_to_le16(covr->idVendor);
2706
2707 if (covr->idProduct)
2708 desc->idProduct = cpu_to_le16(covr->idProduct);
2709
2710 if (covr->bcdDevice)
2711 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002712
2713 if (covr->serial_number) {
2714 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2715 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2716 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002717 if (covr->manufacturer) {
2718 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2719 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002720
2721 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2722 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2723 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2724 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002725 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002726
2727 if (covr->product) {
2728 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2729 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2730 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002731}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002732EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002733
2734MODULE_LICENSE("GPL");
2735MODULE_AUTHOR("David Brownell");