blob: 4aee8c8b434cd20915ef90f2af77bc41b33abdea [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070020
21#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080022#include <linux/usb/otg.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030023#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070024
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020025#include "u_os_desc.h"
Mayank Ranab6cadaa2015-10-15 17:51:32 -070026#define SSUSB_GADGET_VBUS_DRAW 900 /* in mA */
27#define SSUSB_GADGET_VBUS_DRAW_UNITS 8
28#define HSUSB_GADGET_VBUS_DRAW_UNITS 2
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020029
Mayank Rana4be36022016-12-19 15:03:00 -080030/*
31 * Based on enumerated USB speed, draw power with set_config and resume
32 * HSUSB: 500mA, SSUSB: 900mA
33 */
34#define USB_VBUS_DRAW(speed)\
35 (speed == USB_SPEED_SUPER ?\
36 SSUSB_GADGET_VBUS_DRAW : CONFIG_USB_GADGET_VBUS_DRAW)
37
Hemant Kumar7e115fd2017-07-19 12:08:16 -070038/* disable LPM by default */
39static bool disable_l1_for_hs = true;
40module_param(disable_l1_for_hs, bool, 0644);
41MODULE_PARM_DESC(disable_l1_for_hs,
42 "Disable support for L1 LPM for HS devices");
43
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020044/**
45 * struct usb_os_string - represents OS String to be reported by a gadget
46 * @bLength: total length of the entire descritor, always 0x12
47 * @bDescriptorType: USB_DT_STRING
48 * @qwSignature: the OS String proper
49 * @bMS_VendorCode: code used by the host for subsequent requests
50 * @bPad: not used, must be zero
51 */
52struct usb_os_string {
53 __u8 bLength;
54 __u8 bDescriptorType;
55 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
56 __u8 bMS_VendorCode;
57 __u8 bPad;
58} __packed;
59
David Brownell40982be2008-06-19 17:52:58 -070060/*
61 * The code in this file is utility code, used to build a gadget driver
62 * from one or more "function" drivers, one or more "configuration"
63 * objects, and a "usb_composite_driver" by gluing them together along
64 * with the relevant device-wide data.
65 */
66
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010067static struct usb_gadget_strings **get_containers_gs(
68 struct usb_gadget_string_container *uc)
69{
70 return (struct usb_gadget_strings **)uc->stash;
71}
72
Tatyana Brokhman48767a42011-06-28 16:33:49 +030073/**
John Younf3bdbe32016-02-05 17:07:03 -080074 * function_descriptors() - get function descriptors for speed
75 * @f: the function
76 * @speed: the speed
77 *
78 * Returns the descriptors or NULL if not set.
79 */
80static struct usb_descriptor_header **
81function_descriptors(struct usb_function *f,
82 enum usb_device_speed speed)
83{
84 struct usb_descriptor_header **descriptors;
85
Felipe Balbi08782632016-04-22 14:53:47 +030086 /*
87 * NOTE: we try to help gadget drivers which might not be setting
88 * max_speed appropriately.
89 */
90
John Younf3bdbe32016-02-05 17:07:03 -080091 switch (speed) {
92 case USB_SPEED_SUPER_PLUS:
93 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030094 if (descriptors)
95 break;
96 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080097 case USB_SPEED_SUPER:
98 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030099 if (descriptors)
100 break;
101 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800102 case USB_SPEED_HIGH:
103 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +0300104 if (descriptors)
105 break;
106 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800107 default:
108 descriptors = f->fs_descriptors;
109 }
110
Felipe Balbi08782632016-04-22 14:53:47 +0300111 /*
112 * if we can't find any descriptors at all, then this gadget deserves to
113 * Oops with a NULL pointer dereference
114 */
115
John Younf3bdbe32016-02-05 17:07:03 -0800116 return descriptors;
117}
118
119/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300120 * next_ep_desc() - advance to the next EP descriptor
121 * @t: currect pointer within descriptor array
122 *
123 * Return: next EP descriptor or NULL
124 *
125 * Iterate over @t until either EP descriptor found or
126 * NULL (that indicates end of list) encountered
127 */
128static struct usb_descriptor_header**
129next_ep_desc(struct usb_descriptor_header **t)
130{
131 for (; *t; t++) {
132 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
133 return t;
134 }
135 return NULL;
136}
137
138/*
139 * for_each_ep_desc()- iterate over endpoint descriptors in the
140 * descriptors list
141 * @start: pointer within descriptor array.
142 * @ep_desc: endpoint descriptor to use as the loop cursor
143 */
144#define for_each_ep_desc(start, ep_desc) \
145 for (ep_desc = next_ep_desc(start); \
146 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
147
148/**
149 * config_ep_by_speed() - configures the given endpoint
150 * according to gadget speed.
151 * @g: pointer to the gadget
152 * @f: usb function
153 * @_ep: the endpoint to configure
154 *
155 * Return: error code, 0 on success
156 *
157 * This function chooses the right descriptors for a given
158 * endpoint according to gadget speed and saves it in the
159 * endpoint desc field. If the endpoint already has a descriptor
160 * assigned to it - overwrites it with currently corresponding
161 * descriptor. The endpoint maxpacket field is updated according
162 * to the chosen descriptor.
163 * Note: the supplied function should hold all the descriptors
164 * for supported speeds
165 */
166int config_ep_by_speed(struct usb_gadget *g,
167 struct usb_function *f,
168 struct usb_ep *_ep)
169{
Felipe Balbib785ea72012-06-06 10:20:23 +0300170 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300171 struct usb_endpoint_descriptor *chosen_desc = NULL;
172 struct usb_descriptor_header **speed_desc = NULL;
173
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300174 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
175 int want_comp_desc = 0;
176
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300177 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
178
179 if (!g || !f || !_ep)
180 return -EIO;
181
182 /* select desired speed */
183 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800184 case USB_SPEED_SUPER_PLUS:
185 if (gadget_is_superspeed_plus(g)) {
186 speed_desc = f->ssp_descriptors;
187 want_comp_desc = 1;
188 break;
189 }
190 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300191 case USB_SPEED_SUPER:
192 if (gadget_is_superspeed(g)) {
193 speed_desc = f->ss_descriptors;
194 want_comp_desc = 1;
195 break;
196 }
197 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300198 case USB_SPEED_HIGH:
199 if (gadget_is_dualspeed(g)) {
200 speed_desc = f->hs_descriptors;
201 break;
202 }
203 /* else: fall through */
204 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200205 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300206 }
207 /* find descriptors */
208 for_each_ep_desc(speed_desc, d_spd) {
209 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
210 if (chosen_desc->bEndpointAddress == _ep->address)
211 goto ep_found;
212 }
213 return -EIO;
214
215ep_found:
216 /* commit results */
Felipe Balbie0aa5ec2016-09-28 10:38:11 +0300217 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300218 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300219 _ep->comp_desc = NULL;
220 _ep->maxburst = 0;
Felipe Balbi3999c532016-09-28 12:33:31 +0300221 _ep->mult = 1;
222
223 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
224 usb_endpoint_xfer_int(_ep->desc)))
225 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
226
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300227 if (!want_comp_desc)
228 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300229
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300230 /*
231 * Companion descriptor should follow EP descriptor
232 * USB 3.0 spec, #9.6.7
233 */
234 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
235 if (!comp_desc ||
236 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
237 return -EIO;
238 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800239 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300240 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300241 case USB_ENDPOINT_XFER_ISOC:
242 /* mult: bits 1:0 of bmAttributes */
Felipe Balbi3999c532016-09-28 12:33:31 +0300243 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800244 case USB_ENDPOINT_XFER_BULK:
245 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300246 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300247 break;
248 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300249 if (comp_desc->bMaxBurst != 0)
250 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
251 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300252 break;
253 }
254 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300255 return 0;
256}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200257EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700258
259/**
260 * usb_add_function() - add a function to a configuration
261 * @config: the configuration
262 * @function: the function being added
263 * Context: single threaded during gadget setup
264 *
265 * After initialization, each configuration must have one or more
266 * functions added to it. Adding a function involves calling its @bind()
267 * method to allocate resources such as interface and string identifiers
268 * and endpoints.
269 *
270 * This function returns the value of the function's bind(), which is
271 * zero for success else a negative errno value.
272 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200273int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700274 struct usb_function *function)
275{
276 int value = -EINVAL;
277
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530278 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700279 function->name, function,
280 config->label, config);
281
282 if (!function->set_alt || !function->disable)
283 goto done;
284
285 function->config = config;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700286 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700287 list_add_tail(&function->list, &config->functions);
288
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200289 if (function->bind_deactivated) {
290 value = usb_function_deactivate(function);
291 if (value)
292 goto done;
293 }
294
David Brownell40982be2008-06-19 17:52:58 -0700295 /* REVISIT *require* function->bind? */
296 if (function->bind) {
297 value = function->bind(config, function);
298 if (value < 0) {
299 list_del(&function->list);
300 function->config = NULL;
301 }
302 } else
303 value = 0;
304
305 /* We allow configurations that don't work at both speeds.
306 * If we run into a lowspeed Linux system, treat it the same
307 * as full speed ... it's the function drivers that will need
308 * to avoid bulk and ISO transfers.
309 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200310 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700311 config->fullspeed = true;
312 if (!config->highspeed && function->hs_descriptors)
313 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300314 if (!config->superspeed && function->ss_descriptors)
315 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800316 if (!config->superspeed_plus && function->ssp_descriptors)
317 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700318
319done:
320 if (value)
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530321 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700322 function->name, function, value);
323 return value;
324}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200325EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700326
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100327void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
328{
329 if (f->disable)
330 f->disable(f);
331
332 bitmap_zero(f->endpoints, 32);
333 list_del(&f->list);
334 if (f->unbind)
335 f->unbind(c, f);
336}
337EXPORT_SYMBOL_GPL(usb_remove_function);
338
David Brownell40982be2008-06-19 17:52:58 -0700339/**
David Brownell60beed92008-08-18 17:38:22 -0700340 * usb_function_deactivate - prevent function and gadget enumeration
341 * @function: the function that isn't yet ready to respond
342 *
343 * Blocks response of the gadget driver to host enumeration by
344 * preventing the data line pullup from being activated. This is
345 * normally called during @bind() processing to change from the
346 * initial "ready to respond" state, or when a required resource
347 * becomes available.
348 *
349 * For example, drivers that serve as a passthrough to a userspace
350 * daemon can block enumeration unless that daemon (such as an OBEX,
351 * MTP, or print server) is ready to handle host requests.
352 *
353 * Not all systems support software control of their USB peripheral
354 * data pullups.
355 *
356 * Returns zero on success, else negative errno.
357 */
358int usb_function_deactivate(struct usb_function *function)
359{
360 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200361 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700362 int status = 0;
363
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200364 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700365
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530366 if (cdev->deactivations == 0) {
367 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200368 status = usb_gadget_deactivate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530369 spin_lock_irqsave(&cdev->lock, flags);
370 }
David Brownell60beed92008-08-18 17:38:22 -0700371 if (status == 0)
372 cdev->deactivations++;
373
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200374 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700375 return status;
376}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200377EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700378
379/**
380 * usb_function_activate - allow function and gadget enumeration
381 * @function: function on which usb_function_activate() was called
382 *
383 * Reverses effect of usb_function_deactivate(). If no more functions
384 * are delaying their activation, the gadget driver will respond to
385 * host enumeration procedures.
386 *
387 * Returns zero on success, else negative errno.
388 */
389int usb_function_activate(struct usb_function *function)
390{
391 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200392 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700393 int status = 0;
394
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200395 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700396
397 if (WARN_ON(cdev->deactivations == 0))
398 status = -EINVAL;
399 else {
400 cdev->deactivations--;
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530401 if (cdev->deactivations == 0) {
402 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200403 status = usb_gadget_activate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530404 spin_lock_irqsave(&cdev->lock, flags);
405 }
David Brownell60beed92008-08-18 17:38:22 -0700406 }
407
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200408 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700409 return status;
410}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200411EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700412
413/**
David Brownell40982be2008-06-19 17:52:58 -0700414 * usb_interface_id() - allocate an unused interface ID
415 * @config: configuration associated with the interface
416 * @function: function handling the interface
417 * Context: single threaded during gadget setup
418 *
419 * usb_interface_id() is called from usb_function.bind() callbacks to
420 * allocate new interface IDs. The function driver will then store that
421 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300422 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700423 * particularly changing its altsetting via set_alt(). There may
424 * also be class-specific or vendor-specific requests to handle.
425 *
426 * All interface identifier should be allocated using this routine, to
427 * ensure that for example different functions don't wrongly assign
428 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700430 * one configuration (or more than once in a given configuration) need
431 * multiple versions of the relevant descriptors.
432 *
433 * Returns the interface ID which was allocated; or -ENODEV if no
434 * more interface IDs can be allocated.
435 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200436int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700437 struct usb_function *function)
438{
439 unsigned id = config->next_interface_id;
440
441 if (id < MAX_CONFIG_INTERFACES) {
442 config->interface[id] = function;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700443 if (function->intf_id < 0)
444 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700445 config->next_interface_id = id + 1;
446 return id;
447 }
448 return -ENODEV;
449}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200450EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700451
Mayank Ranab92dfd02014-11-25 15:29:58 -0800452static int usb_func_wakeup_int(struct usb_function *func)
Danny Segalf83e4512016-12-06 15:35:24 -0800453{
454 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800455 struct usb_gadget *gadget;
456
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800457 pr_debug("%s - %s function wakeup\n",
458 __func__, func->name ? func->name : "");
Danny Segalf83e4512016-12-06 15:35:24 -0800459
460 if (!func || !func->config || !func->config->cdev ||
461 !func->config->cdev->gadget)
462 return -EINVAL;
463
464 gadget = func->config->cdev->gadget;
465 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
466 DBG(func->config->cdev,
467 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
468 gadget->speed,
469 func->func_wakeup_allowed);
470
471 return -ENOTSUPP;
472 }
473
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800474 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300475
476 return ret;
477}
478
479int usb_func_wakeup(struct usb_function *func)
480{
481 int ret;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530482 unsigned long flags;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300483
484 pr_debug("%s function wakeup\n",
485 func->name ? func->name : "");
486
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530487 spin_lock_irqsave(&func->config->cdev->lock, flags);
Mayank Ranab92dfd02014-11-25 15:29:58 -0800488 ret = usb_func_wakeup_int(func);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300489 if (ret == -EAGAIN) {
490 DBG(func->config->cdev,
491 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
492 func->name ? func->name : "");
Danny Segalde7cd8d2014-07-28 18:08:33 +0300493 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);
523 if (ret == -EAGAIN) {
524 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
525 func->name ? func->name : "");
526 } else if (ret < 0 && ret != -ENOTSUPP) {
527 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
528 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800529 }
wHemant Kumare9e13922016-12-19 14:41:28 -0800530 goto done;
Danny Segalf83e4512016-12-06 15:35:24 -0800531 }
532
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800533 if (!func->func_is_suspended)
534 ret = 0;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800535
wHemant Kumare9e13922016-12-19 14:41:28 -0800536 if (func->func_is_suspended && !func->func_wakeup_allowed) {
537 ret = -ENOTSUPP;
538 goto done;
539 }
540
541 ret = usb_ep_queue(ep, req, gfp_flags);
542done:
Danny Segalf83e4512016-12-06 15:35:24 -0800543 return ret;
544}
545EXPORT_SYMBOL(usb_func_ep_queue);
546
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100547static u8 encode_bMaxPower(enum usb_device_speed speed,
548 struct usb_configuration *c)
549{
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700550 unsigned int val = CONFIG_USB_GADGET_VBUS_DRAW;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100551
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100552 switch (speed) {
553 case USB_SPEED_SUPER:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700554 /* with super-speed report 900mA */
555 val = SSUSB_GADGET_VBUS_DRAW;
556 return (u8)(val / SSUSB_GADGET_VBUS_DRAW_UNITS);
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100557 default:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700558 return DIV_ROUND_UP(val, HSUSB_GADGET_VBUS_DRAW_UNITS);
Joe Perches2b84f922013-10-08 16:01:37 -0700559 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100560}
561
David Brownell40982be2008-06-19 17:52:58 -0700562static int config_buf(struct usb_configuration *config,
563 enum usb_device_speed speed, void *buf, u8 type)
564{
565 struct usb_config_descriptor *c = buf;
566 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200567 int len;
David Brownell40982be2008-06-19 17:52:58 -0700568 struct usb_function *f;
569 int status;
570
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200571 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700572 /* write the config descriptor */
573 c = buf;
574 c->bLength = USB_DT_CONFIG_SIZE;
575 c->bDescriptorType = type;
576 /* wTotalLength is written later */
577 c->bNumInterfaces = config->next_interface_id;
578 c->bConfigurationValue = config->bConfigurationValue;
579 c->iConfiguration = config->iConfiguration;
580 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100581 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700582
583 /* There may be e.g. OTG descriptors */
584 if (config->descriptors) {
585 status = usb_descriptor_fillbuf(next, len,
586 config->descriptors);
587 if (status < 0)
588 return status;
589 len -= status;
590 next += status;
591 }
592
593 /* add each function's descriptors */
594 list_for_each_entry(f, &config->functions, list) {
595 struct usb_descriptor_header **descriptors;
596
John Younf3bdbe32016-02-05 17:07:03 -0800597 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700598 if (!descriptors)
599 continue;
600 status = usb_descriptor_fillbuf(next, len,
601 (const struct usb_descriptor_header **) descriptors);
602 if (status < 0)
603 return status;
604 len -= status;
605 next += status;
606 }
607
608 len = next - buf;
609 c->wTotalLength = cpu_to_le16(len);
610 return len;
611}
612
613static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
614{
615 struct usb_gadget *gadget = cdev->gadget;
616 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200617 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700618 u8 type = w_value >> 8;
619 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
620
John Youneae58202016-02-05 17:07:17 -0800621 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300622 speed = gadget->speed;
623 else if (gadget_is_dualspeed(gadget)) {
624 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700625 if (gadget->speed == USB_SPEED_HIGH)
626 hs = 1;
627 if (type == USB_DT_OTHER_SPEED_CONFIG)
628 hs = !hs;
629 if (hs)
630 speed = USB_SPEED_HIGH;
631
632 }
633
634 /* This is a lookup by config *INDEX* */
635 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200636
637 pos = &cdev->configs;
638 c = cdev->os_desc_config;
639 if (c)
640 goto check_config;
641
642 while ((pos = pos->next) != &cdev->configs) {
643 c = list_entry(pos, typeof(*c), list);
644
645 /* skip OS Descriptors config which is handled separately */
646 if (c == cdev->os_desc_config)
647 continue;
648
649check_config:
David Brownell40982be2008-06-19 17:52:58 -0700650 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300651 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800652 case USB_SPEED_SUPER_PLUS:
653 if (!c->superspeed_plus)
654 continue;
655 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300656 case USB_SPEED_SUPER:
657 if (!c->superspeed)
658 continue;
659 break;
660 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700661 if (!c->highspeed)
662 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300663 break;
664 default:
David Brownell40982be2008-06-19 17:52:58 -0700665 if (!c->fullspeed)
666 continue;
667 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300668
David Brownell40982be2008-06-19 17:52:58 -0700669 if (w_value == 0)
670 return config_buf(c, speed, cdev->req->buf, type);
671 w_value--;
672 }
673 return -EINVAL;
674}
675
676static int count_configs(struct usb_composite_dev *cdev, unsigned type)
677{
678 struct usb_gadget *gadget = cdev->gadget;
679 struct usb_configuration *c;
680 unsigned count = 0;
681 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300682 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800683 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700684
685 if (gadget_is_dualspeed(gadget)) {
686 if (gadget->speed == USB_SPEED_HIGH)
687 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300688 if (gadget->speed == USB_SPEED_SUPER)
689 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800690 if (gadget->speed == USB_SPEED_SUPER_PLUS)
691 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700692 if (type == USB_DT_DEVICE_QUALIFIER)
693 hs = !hs;
694 }
695 list_for_each_entry(c, &cdev->configs, list) {
696 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800697 if (ssp) {
698 if (!c->superspeed_plus)
699 continue;
700 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300701 if (!c->superspeed)
702 continue;
703 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700704 if (!c->highspeed)
705 continue;
706 } else {
707 if (!c->fullspeed)
708 continue;
709 }
710 count++;
711 }
712 return count;
713}
714
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300715/**
716 * bos_desc() - prepares the BOS descriptor.
717 * @cdev: pointer to usb_composite device to generate the bos
718 * descriptor for
719 *
720 * This function generates the BOS (Binary Device Object)
721 * descriptor and its device capabilities descriptors. The BOS
722 * descriptor should be supported by a SuperSpeed device.
723 */
724static int bos_desc(struct usb_composite_dev *cdev)
725{
726 struct usb_ext_cap_descriptor *usb_ext;
727 struct usb_ss_cap_descriptor *ss_cap;
728 struct usb_dcd_config_params dcd_config_params;
729 struct usb_bos_descriptor *bos = cdev->req->buf;
730
731 bos->bLength = USB_DT_BOS_SIZE;
732 bos->bDescriptorType = USB_DT_BOS;
733
734 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
735 bos->bNumDeviceCaps = 0;
736
737 /*
738 * A SuperSpeed device shall include the USB2.0 extension descriptor
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800739 * and shall support LPM when operating in USB2.0 HS mode, as well as
740 * a HS device when operating in USB2.1 HS mode.
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300741 */
742 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
743 bos->bNumDeviceCaps++;
744 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
745 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
746 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
747 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500748 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300749
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800750 if (gadget_is_superspeed(cdev->gadget)) {
751 /*
752 * The Superspeed USB Capability descriptor shall be
753 * implemented by all SuperSpeed devices.
754 */
755 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
756 bos->bNumDeviceCaps++;
757 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
758 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
759 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
760 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
761 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
762 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
763 USB_FULL_SPEED_OPERATION |
764 USB_HIGH_SPEED_OPERATION |
765 USB_5GBPS_OPERATION);
766 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300767
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800768 /* Get Controller configuration */
769 if (cdev->gadget->ops->get_config_params)
770 cdev->gadget->ops->get_config_params
771 (&dcd_config_params);
772 else {
773 dcd_config_params.bU1devExitLat =
774 USB_DEFAULT_U1_DEV_EXIT_LAT;
775 dcd_config_params.bU2DevExitLat =
776 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
777 }
778 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
779 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300780 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300781
John Younf228a8d2016-02-05 17:05:53 -0800782 /* The SuperSpeedPlus USB Device Capability descriptor */
783 if (gadget_is_superspeed_plus(cdev->gadget)) {
784 struct usb_ssp_cap_descriptor *ssp_cap;
785
786 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
787 bos->bNumDeviceCaps++;
788
789 /*
790 * Report typical values.
791 */
792
793 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
794 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
795 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
796 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700797 ssp_cap->bReserved = 0;
798 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800799
800 /* SSAC = 1 (2 attributes) */
801 ssp_cap->bmAttributes = cpu_to_le32(1);
802
803 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700804 ssp_cap->wFunctionalitySupport =
805 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800806
807 /*
808 * bmSublinkSpeedAttr[0]:
809 * ST = Symmetric, RX
810 * LSE = 3 (Gbps)
811 * LP = 1 (SuperSpeedPlus)
812 * LSM = 10 (10 Gbps)
813 */
814 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700815 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800816 /*
817 * bmSublinkSpeedAttr[1] =
818 * ST = Symmetric, TX
819 * LSE = 3 (Gbps)
820 * LP = 1 (SuperSpeedPlus)
821 * LSM = 10 (10 Gbps)
822 */
823 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700824 cpu_to_le32((3 << 4) | (1 << 14) |
825 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800826 }
827
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300828 return le16_to_cpu(bos->wTotalLength);
829}
830
David Brownell40982be2008-06-19 17:52:58 -0700831static void device_qual(struct usb_composite_dev *cdev)
832{
833 struct usb_qualifier_descriptor *qual = cdev->req->buf;
834
835 qual->bLength = sizeof(*qual);
836 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
837 /* POLICY: same bcdUSB and device type info at both speeds */
838 qual->bcdUSB = cdev->desc.bcdUSB;
839 qual->bDeviceClass = cdev->desc.bDeviceClass;
840 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
841 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
842 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200843 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700844 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700845 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700846}
847
848/*-------------------------------------------------------------------------*/
849
850static void reset_config(struct usb_composite_dev *cdev)
851{
852 struct usb_function *f;
853
854 DBG(cdev, "reset config\n");
855
856 list_for_each_entry(f, &cdev->config->functions, list) {
857 if (f->disable)
858 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200859
Danny Segalf83e4512016-12-06 15:35:24 -0800860 /* USB 3.0 addition */
861 f->func_is_suspended = false;
862 f->func_wakeup_allowed = false;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300863 f->func_wakeup_pending = false;
Danny Segalf83e4512016-12-06 15:35:24 -0800864
Laurent Pinchart52426582009-10-21 00:03:38 +0200865 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700866 }
867 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100868 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700869}
870
871static int set_config(struct usb_composite_dev *cdev,
872 const struct usb_ctrlrequest *ctrl, unsigned number)
873{
874 struct usb_gadget *gadget = cdev->gadget;
875 struct usb_configuration *c = NULL;
876 int result = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700877 int tmp;
878
David Brownell40982be2008-06-19 17:52:58 -0700879 if (number) {
880 list_for_each_entry(c, &cdev->configs, list) {
881 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300882 /*
883 * We disable the FDs of the previous
884 * configuration only if the new configuration
885 * is a valid one
886 */
887 if (cdev->config)
888 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700889 result = 0;
890 break;
891 }
892 }
893 if (result < 0)
894 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300895 } else { /* Zero configuration value - need to reset the config */
896 if (cdev->config)
897 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700898 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300899 }
David Brownell40982be2008-06-19 17:52:58 -0700900
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200901 INFO(cdev, "%s config #%d: %s\n",
902 usb_speed_string(gadget->speed),
903 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700904
905 if (!c)
906 goto done;
907
Peter Chen6027f312014-04-29 13:26:28 +0800908 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700909 cdev->config = c;
Jack Phamdb943d62016-12-16 11:21:16 -0800910 c->num_ineps_used = 0;
911 c->num_outeps_used = 0;
David Brownell40982be2008-06-19 17:52:58 -0700912
913 /* Initialize all interfaces by setting them to altsetting zero. */
914 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
915 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200916 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700917
918 if (!f)
919 break;
920
Laurent Pinchart52426582009-10-21 00:03:38 +0200921 /*
922 * Record which endpoints are used by the function. This is used
923 * to dispatch control requests targeted at that endpoint to the
924 * function's setup callback instead of the current
925 * configuration's setup callback.
926 */
John Younf3bdbe32016-02-05 17:07:03 -0800927 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200928
929 for (; *descriptors; ++descriptors) {
930 struct usb_endpoint_descriptor *ep;
931 int addr;
932
933 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
934 continue;
935
936 ep = (struct usb_endpoint_descriptor *)*descriptors;
937 addr = ((ep->bEndpointAddress & 0x80) >> 3)
938 | (ep->bEndpointAddress & 0x0f);
939 set_bit(addr, f->endpoints);
Jack Phamdb943d62016-12-16 11:21:16 -0800940 if (usb_endpoint_dir_in(ep))
941 c->num_ineps_used++;
942 else
943 c->num_outeps_used++;
Laurent Pinchart52426582009-10-21 00:03:38 +0200944 }
945
David Brownell40982be2008-06-19 17:52:58 -0700946 result = f->set_alt(f, tmp, 0);
947 if (result < 0) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530948 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700949 tmp, f->name, f, result);
950
951 reset_config(cdev);
952 goto done;
953 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300954
955 if (result == USB_GADGET_DELAYED_STATUS) {
956 DBG(cdev,
957 "%s: interface %d (%s) requested delayed status\n",
958 __func__, tmp, f->name);
959 cdev->delayed_status++;
960 DBG(cdev, "delayed_status count %d\n",
961 cdev->delayed_status);
962 }
David Brownell40982be2008-06-19 17:52:58 -0700963 }
964
David Brownell40982be2008-06-19 17:52:58 -0700965done:
Mayank Rana4be36022016-12-19 15:03:00 -0800966 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
Roger Quadros1b9ba002011-05-09 13:08:06 +0300967 if (result >= 0 && cdev->delayed_status)
968 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700969 return result;
970}
971
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100972int usb_add_config_only(struct usb_composite_dev *cdev,
973 struct usb_configuration *config)
974{
975 struct usb_configuration *c;
976
977 if (!config->bConfigurationValue)
978 return -EINVAL;
979
980 /* Prevent duplicate configuration identifiers */
981 list_for_each_entry(c, &cdev->configs, list) {
982 if (c->bConfigurationValue == config->bConfigurationValue)
983 return -EBUSY;
984 }
985
986 config->cdev = cdev;
987 list_add_tail(&config->list, &cdev->configs);
988
989 INIT_LIST_HEAD(&config->functions);
990 config->next_interface_id = 0;
991 memset(config->interface, 0, sizeof(config->interface));
992
993 return 0;
994}
995EXPORT_SYMBOL_GPL(usb_add_config_only);
996
David Brownell40982be2008-06-19 17:52:58 -0700997/**
998 * usb_add_config() - add a configuration to a device.
999 * @cdev: wraps the USB gadget
1000 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001001 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -07001002 * Context: single threaded during gadget setup
1003 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001004 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -07001005 * add each of the configurations it supports, using this routine.
1006 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001007 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -07001008 * is zero for success else a negative errno value. Binding configurations
1009 * assigns global resources including string IDs, and per-configuration
1010 * resources such as interface IDs and endpoints.
1011 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001012int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001013 struct usb_configuration *config,
1014 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001015{
1016 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001017
1018 if (!bind)
1019 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001020
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301021 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001022 config->bConfigurationValue,
1023 config->label, config);
1024
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001025 status = usb_add_config_only(cdev, config);
1026 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001027 goto done;
1028
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001029 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001030 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001031 while (!list_empty(&config->functions)) {
1032 struct usb_function *f;
1033
1034 f = list_first_entry(&config->functions,
1035 struct usb_function, list);
1036 list_del(&f->list);
1037 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301038 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001039 f->name, f);
1040 f->unbind(config, f);
1041 /* may free memory for "f" */
1042 }
1043 }
David Brownell40982be2008-06-19 17:52:58 -07001044 list_del(&config->list);
1045 config->cdev = NULL;
1046 } else {
1047 unsigned i;
1048
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301049 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001050 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001051 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001052 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001053 config->highspeed ? " high" : "",
1054 config->fullspeed
1055 ? (gadget_is_dualspeed(cdev->gadget)
1056 ? " full"
1057 : " full/low")
1058 : "");
1059
1060 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1061 struct usb_function *f = config->interface[i];
1062
1063 if (!f)
1064 continue;
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301065 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001066 i, f->name, f);
1067 }
1068 }
1069
Robert Baldygaf871cb92015-09-16 12:10:39 +02001070 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001071 usb_ep_autoconfig_reset(cdev->gadget);
1072
1073done:
1074 if (status)
1075 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1076 config->bConfigurationValue, status);
1077 return status;
1078}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001079EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001080
Benoit Goby51cce6f2012-05-10 10:07:57 +02001081static void remove_config(struct usb_composite_dev *cdev,
1082 struct usb_configuration *config)
1083{
1084 while (!list_empty(&config->functions)) {
1085 struct usb_function *f;
1086
1087 f = list_first_entry(&config->functions,
1088 struct usb_function, list);
1089 list_del(&f->list);
1090 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301091 DBG(cdev, "unbind function '%s'/%pK\n", f->name, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001092 f->unbind(config, f);
1093 /* may free memory for "f" */
1094 }
1095 }
1096 list_del(&config->list);
1097 if (config->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301098 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001099 config->unbind(config);
1100 /* may free memory for "c" */
1101 }
1102}
1103
1104/**
1105 * usb_remove_config() - remove a configuration from a device.
1106 * @cdev: wraps the USB gadget
1107 * @config: the configuration
1108 *
1109 * Drivers must call usb_gadget_disconnect before calling this function
1110 * to disconnect the device from the host and make sure the host will not
1111 * try to enumerate the device while we are changing the config list.
1112 */
1113void usb_remove_config(struct usb_composite_dev *cdev,
1114 struct usb_configuration *config)
1115{
1116 unsigned long flags;
1117
1118 spin_lock_irqsave(&cdev->lock, flags);
1119
1120 if (cdev->config == config)
1121 reset_config(cdev);
1122
1123 spin_unlock_irqrestore(&cdev->lock, flags);
1124
1125 remove_config(cdev, config);
1126}
1127
David Brownell40982be2008-06-19 17:52:58 -07001128/*-------------------------------------------------------------------------*/
1129
1130/* We support strings in multiple languages ... string descriptor zero
1131 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001132 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001133 * the host side.
1134 */
1135
1136static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1137{
1138 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001139 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001140 __le16 *tmp;
1141
1142 while (*sp) {
1143 s = *sp;
1144 language = cpu_to_le16(s->language);
1145 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1146 if (*tmp == language)
1147 goto repeat;
1148 }
1149 *tmp++ = language;
1150repeat:
1151 sp++;
1152 }
1153}
1154
1155static int lookup_string(
1156 struct usb_gadget_strings **sp,
1157 void *buf,
1158 u16 language,
1159 int id
1160)
1161{
1162 struct usb_gadget_strings *s;
1163 int value;
1164
1165 while (*sp) {
1166 s = *sp++;
1167 if (s->language != language)
1168 continue;
1169 value = usb_gadget_get_string(s, id, buf);
1170 if (value > 0)
1171 return value;
1172 }
1173 return -EINVAL;
1174}
1175
1176static int get_string(struct usb_composite_dev *cdev,
1177 void *buf, u16 language, int id)
1178{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001179 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001180 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001181 struct usb_configuration *c;
1182 struct usb_function *f;
1183 int len;
1184
Diego Violaad4676a2015-05-31 15:52:41 -03001185 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001186 * folk will ever care about ... also, it's all supported here.
1187 * (Except for UTF8 support for Unicode's "Astral Planes".)
1188 */
1189
1190 /* 0 == report all available language codes */
1191 if (id == 0) {
1192 struct usb_string_descriptor *s = buf;
1193 struct usb_gadget_strings **sp;
1194
1195 memset(s, 0, 256);
1196 s->bDescriptorType = USB_DT_STRING;
1197
1198 sp = composite->strings;
1199 if (sp)
1200 collect_langs(sp, s->wData);
1201
1202 list_for_each_entry(c, &cdev->configs, list) {
1203 sp = c->strings;
1204 if (sp)
1205 collect_langs(sp, s->wData);
1206
1207 list_for_each_entry(f, &c->functions, list) {
1208 sp = f->strings;
1209 if (sp)
1210 collect_langs(sp, s->wData);
1211 }
1212 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001213 list_for_each_entry(uc, &cdev->gstrings, list) {
1214 struct usb_gadget_strings **sp;
1215
1216 sp = get_containers_gs(uc);
1217 collect_langs(sp, s->wData);
1218 }
David Brownell40982be2008-06-19 17:52:58 -07001219
Roel Kluin417b57b2009-08-06 16:09:51 -07001220 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001221 continue;
1222 if (!len)
1223 return -EINVAL;
1224
1225 s->bLength = 2 * (len + 1);
1226 return s->bLength;
1227 }
1228
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001229 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1230 struct usb_os_string *b = buf;
1231 b->bLength = sizeof(*b);
1232 b->bDescriptorType = USB_DT_STRING;
1233 compiletime_assert(
1234 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1235 "qwSignature size must be equal to qw_sign");
1236 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1237 b->bMS_VendorCode = cdev->b_vendor_code;
1238 b->bPad = 0;
1239 return sizeof(*b);
1240 }
1241
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001242 list_for_each_entry(uc, &cdev->gstrings, list) {
1243 struct usb_gadget_strings **sp;
1244
1245 sp = get_containers_gs(uc);
1246 len = lookup_string(sp, buf, language, id);
1247 if (len > 0)
1248 return len;
1249 }
1250
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001251 /* String IDs are device-scoped, so we look up each string
1252 * table we're told about. These lookups are infrequent;
1253 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001254 */
1255 if (composite->strings) {
1256 len = lookup_string(composite->strings, buf, language, id);
1257 if (len > 0)
1258 return len;
1259 }
1260 list_for_each_entry(c, &cdev->configs, list) {
1261 if (c->strings) {
1262 len = lookup_string(c->strings, buf, language, id);
1263 if (len > 0)
1264 return len;
1265 }
1266 list_for_each_entry(f, &c->functions, list) {
1267 if (!f->strings)
1268 continue;
1269 len = lookup_string(f->strings, buf, language, id);
1270 if (len > 0)
1271 return len;
1272 }
1273 }
1274 return -EINVAL;
1275}
1276
1277/**
1278 * usb_string_id() - allocate an unused string ID
1279 * @cdev: the device whose string descriptor IDs are being allocated
1280 * Context: single threaded during gadget setup
1281 *
1282 * @usb_string_id() is called from bind() callbacks to allocate
1283 * string IDs. Drivers for functions, configurations, or gadgets will
1284 * then store that ID in the appropriate descriptors and string table.
1285 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001286 * All string identifier should be allocated using this,
1287 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1288 * that for example different functions don't wrongly assign different
1289 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001290 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001291int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001292{
1293 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001294 /* string id 0 is reserved by USB spec for list of
1295 * supported languages */
1296 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001297 cdev->next_string_id++;
1298 return cdev->next_string_id;
1299 }
1300 return -ENODEV;
1301}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001302EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001303
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001304/**
1305 * usb_string_ids() - allocate unused string IDs in batch
1306 * @cdev: the device whose string descriptor IDs are being allocated
1307 * @str: an array of usb_string objects to assign numbers to
1308 * Context: single threaded during gadget setup
1309 *
1310 * @usb_string_ids() is called from bind() callbacks to allocate
1311 * string IDs. Drivers for functions, configurations, or gadgets will
1312 * then copy IDs from the string table to the appropriate descriptors
1313 * and string table for other languages.
1314 *
1315 * All string identifier should be allocated using this,
1316 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1317 * example different functions don't wrongly assign different meanings
1318 * to the same identifier.
1319 */
1320int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1321{
1322 int next = cdev->next_string_id;
1323
1324 for (; str->s; ++str) {
1325 if (unlikely(next >= 254))
1326 return -ENODEV;
1327 str->id = ++next;
1328 }
1329
1330 cdev->next_string_id = next;
1331
1332 return 0;
1333}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001334EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001335
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001336static struct usb_gadget_string_container *copy_gadget_strings(
1337 struct usb_gadget_strings **sp, unsigned n_gstrings,
1338 unsigned n_strings)
1339{
1340 struct usb_gadget_string_container *uc;
1341 struct usb_gadget_strings **gs_array;
1342 struct usb_gadget_strings *gs;
1343 struct usb_string *s;
1344 unsigned mem;
1345 unsigned n_gs;
1346 unsigned n_s;
1347 void *stash;
1348
1349 mem = sizeof(*uc);
1350 mem += sizeof(void *) * (n_gstrings + 1);
1351 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1352 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1353 uc = kmalloc(mem, GFP_KERNEL);
1354 if (!uc)
1355 return ERR_PTR(-ENOMEM);
1356 gs_array = get_containers_gs(uc);
1357 stash = uc->stash;
1358 stash += sizeof(void *) * (n_gstrings + 1);
1359 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1360 struct usb_string *org_s;
1361
1362 gs_array[n_gs] = stash;
1363 gs = gs_array[n_gs];
1364 stash += sizeof(struct usb_gadget_strings);
1365 gs->language = sp[n_gs]->language;
1366 gs->strings = stash;
1367 org_s = sp[n_gs]->strings;
1368
1369 for (n_s = 0; n_s < n_strings; n_s++) {
1370 s = stash;
1371 stash += sizeof(struct usb_string);
1372 if (org_s->s)
1373 s->s = org_s->s;
1374 else
1375 s->s = "";
1376 org_s++;
1377 }
1378 s = stash;
1379 s->s = NULL;
1380 stash += sizeof(struct usb_string);
1381
1382 }
1383 gs_array[n_gs] = NULL;
1384 return uc;
1385}
1386
1387/**
1388 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1389 * @cdev: the device whose string descriptor IDs are being allocated
1390 * and attached.
1391 * @sp: an array of usb_gadget_strings to attach.
1392 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1393 *
1394 * This function will create a deep copy of usb_gadget_strings and usb_string
1395 * and attach it to the cdev. The actual string (usb_string.s) will not be
1396 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001397 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001398 * The ->language pointer of each struct usb_gadget_strings has to contain the
1399 * same amount of entries.
1400 * 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 +09001401 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001402 * entry of en-US. Therefore both entries become the same id assign.
1403 */
1404struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1405 struct usb_gadget_strings **sp, unsigned n_strings)
1406{
1407 struct usb_gadget_string_container *uc;
1408 struct usb_gadget_strings **n_gs;
1409 unsigned n_gstrings = 0;
1410 unsigned i;
1411 int ret;
1412
1413 for (i = 0; sp[i]; i++)
1414 n_gstrings++;
1415
1416 if (!n_gstrings)
1417 return ERR_PTR(-EINVAL);
1418
1419 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1420 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001421 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001422
1423 n_gs = get_containers_gs(uc);
1424 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1425 if (ret)
1426 goto err;
1427
1428 for (i = 1; i < n_gstrings; i++) {
1429 struct usb_string *m_s;
1430 struct usb_string *s;
1431 unsigned n;
1432
1433 m_s = n_gs[0]->strings;
1434 s = n_gs[i]->strings;
1435 for (n = 0; n < n_strings; n++) {
1436 s->id = m_s->id;
1437 s++;
1438 m_s++;
1439 }
1440 }
1441 list_add_tail(&uc->list, &cdev->gstrings);
1442 return n_gs[0]->strings;
1443err:
1444 kfree(uc);
1445 return ERR_PTR(ret);
1446}
1447EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1448
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001449/**
1450 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001451 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001452 * @n: number of string IDs to allocate
1453 * Context: single threaded during gadget setup
1454 *
1455 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001456 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001457 * is, returns last requested ID which is now very useful information.
1458 *
1459 * @usb_string_ids_n() is called from bind() callbacks to allocate
1460 * string IDs. Drivers for functions, configurations, or gadgets will
1461 * then store that ID in the appropriate descriptors and string table.
1462 *
1463 * All string identifier should be allocated using this,
1464 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1465 * example different functions don't wrongly assign different meanings
1466 * to the same identifier.
1467 */
1468int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1469{
1470 unsigned next = c->next_string_id;
1471 if (unlikely(n > 254 || (unsigned)next + n > 254))
1472 return -ENODEV;
1473 c->next_string_id += n;
1474 return next + 1;
1475}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001476EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001477
David Brownell40982be2008-06-19 17:52:58 -07001478/*-------------------------------------------------------------------------*/
1479
1480static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1481{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001482 struct usb_composite_dev *cdev;
1483
David Brownell40982be2008-06-19 17:52:58 -07001484 if (req->status || req->actual != req->length)
1485 DBG((struct usb_composite_dev *) ep->driver_data,
1486 "setup complete --> %d, %d/%d\n",
1487 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001488
1489 /*
1490 * REVIST The same ep0 requests are shared with function drivers
1491 * so they don't have to maintain the same ->complete() stubs.
1492 *
1493 * Because of that, we need to check for the validity of ->context
1494 * here, even though we know we've set it to something useful.
1495 */
1496 if (!req->context)
1497 return;
1498
1499 cdev = req->context;
1500
1501 if (cdev->req == req)
1502 cdev->setup_pending = false;
1503 else if (cdev->os_desc_req == req)
1504 cdev->os_desc_pending = false;
1505 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301506 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001507}
1508
1509static int composite_ep0_queue(struct usb_composite_dev *cdev,
1510 struct usb_request *req, gfp_t gfp_flags)
1511{
1512 int ret;
1513
1514 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1515 if (ret == 0) {
1516 if (cdev->req == req)
1517 cdev->setup_pending = true;
1518 else if (cdev->os_desc_req == req)
1519 cdev->os_desc_pending = true;
1520 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301521 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001522 }
1523
1524 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001525}
1526
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001527static int count_ext_compat(struct usb_configuration *c)
1528{
1529 int i, res;
1530
1531 res = 0;
1532 for (i = 0; i < c->next_interface_id; ++i) {
1533 struct usb_function *f;
1534 int j;
1535
1536 f = c->interface[i];
1537 for (j = 0; j < f->os_desc_n; ++j) {
1538 struct usb_os_desc *d;
1539
1540 if (i != f->os_desc_table[j].if_id)
1541 continue;
1542 d = f->os_desc_table[j].os_desc;
1543 if (d && d->ext_compat_id)
1544 ++res;
1545 }
1546 }
1547 BUG_ON(res > 255);
1548 return res;
1549}
1550
1551static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1552{
1553 int i, count;
1554
1555 count = 16;
1556 for (i = 0; i < c->next_interface_id; ++i) {
1557 struct usb_function *f;
1558 int j;
1559
1560 f = c->interface[i];
1561 for (j = 0; j < f->os_desc_n; ++j) {
1562 struct usb_os_desc *d;
1563
1564 if (i != f->os_desc_table[j].if_id)
1565 continue;
1566 d = f->os_desc_table[j].os_desc;
1567 if (d && d->ext_compat_id) {
1568 *buf++ = i;
1569 *buf++ = 0x01;
1570 memcpy(buf, d->ext_compat_id, 16);
1571 buf += 22;
1572 } else {
1573 ++buf;
1574 *buf = 0x01;
1575 buf += 23;
1576 }
1577 count += 24;
1578 if (count >= 4096)
1579 return;
1580 }
1581 }
1582}
1583
1584static int count_ext_prop(struct usb_configuration *c, int interface)
1585{
1586 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001587 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001588
1589 f = c->interface[interface];
1590 for (j = 0; j < f->os_desc_n; ++j) {
1591 struct usb_os_desc *d;
1592
1593 if (interface != f->os_desc_table[j].if_id)
1594 continue;
1595 d = f->os_desc_table[j].os_desc;
1596 if (d && d->ext_compat_id)
1597 return d->ext_prop_count;
1598 }
Julia Lawall849b1332014-05-19 06:31:07 +02001599 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001600}
1601
1602static int len_ext_prop(struct usb_configuration *c, int interface)
1603{
1604 struct usb_function *f;
1605 struct usb_os_desc *d;
1606 int j, res;
1607
1608 res = 10; /* header length */
1609 f = c->interface[interface];
1610 for (j = 0; j < f->os_desc_n; ++j) {
1611 if (interface != f->os_desc_table[j].if_id)
1612 continue;
1613 d = f->os_desc_table[j].os_desc;
1614 if (d)
1615 return min(res + d->ext_prop_len, 4096);
1616 }
1617 return res;
1618}
1619
1620static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1621{
1622 struct usb_function *f;
1623 struct usb_os_desc *d;
1624 struct usb_os_desc_ext_prop *ext_prop;
1625 int j, count, n, ret;
1626 u8 *start = buf;
1627
1628 f = c->interface[interface];
1629 for (j = 0; j < f->os_desc_n; ++j) {
1630 if (interface != f->os_desc_table[j].if_id)
1631 continue;
1632 d = f->os_desc_table[j].os_desc;
1633 if (d)
1634 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1635 /* 4kB minus header length */
1636 n = buf - start;
1637 if (n >= 4086)
1638 return 0;
1639
1640 count = ext_prop->data_len +
1641 ext_prop->name_len + 14;
1642 if (count > 4086 - n)
1643 return -EINVAL;
1644 usb_ext_prop_put_size(buf, count);
1645 usb_ext_prop_put_type(buf, ext_prop->type);
1646 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1647 ext_prop->name_len);
1648 if (ret < 0)
1649 return ret;
1650 switch (ext_prop->type) {
1651 case USB_EXT_PROP_UNICODE:
1652 case USB_EXT_PROP_UNICODE_ENV:
1653 case USB_EXT_PROP_UNICODE_LINK:
1654 usb_ext_prop_put_unicode(buf, ret,
1655 ext_prop->data,
1656 ext_prop->data_len);
1657 break;
1658 case USB_EXT_PROP_BINARY:
1659 usb_ext_prop_put_binary(buf, ret,
1660 ext_prop->data,
1661 ext_prop->data_len);
1662 break;
1663 case USB_EXT_PROP_LE32:
1664 /* not implemented */
1665 case USB_EXT_PROP_BE32:
1666 /* not implemented */
1667 default:
1668 return -EINVAL;
1669 }
1670 buf += count;
1671 }
1672 }
1673
1674 return 0;
1675}
1676
David Brownell40982be2008-06-19 17:52:58 -07001677/*
1678 * The setup() callback implements all the ep0 functionality that's
1679 * not handled lower down, in hardware or the hardware driver(like
1680 * device and endpoint feature flags, and their status). It's all
1681 * housekeeping for the gadget function we're implementing. Most of
1682 * the work is in config and function specific setup.
1683 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001684int
David Brownell40982be2008-06-19 17:52:58 -07001685composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1686{
1687 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1688 struct usb_request *req = cdev->req;
1689 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001690 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001691 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001692 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001693 u16 w_value = le16_to_cpu(ctrl->wValue);
1694 u16 w_length = le16_to_cpu(ctrl->wLength);
1695 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001696 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001697
1698 /* partial re-init of the response message; the function or the
1699 * gadget might need to intercept e.g. a control-OUT completion
1700 * when we delegate to it.
1701 */
1702 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001703 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001704 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301705 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001706 gadget->ep0->driver_data = cdev;
1707
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001708 /*
1709 * Don't let non-standard requests match any of the cases below
1710 * by accident.
1711 */
1712 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1713 goto unknown;
1714
David Brownell40982be2008-06-19 17:52:58 -07001715 switch (ctrl->bRequest) {
1716
1717 /* we handle all standard USB descriptors */
1718 case USB_REQ_GET_DESCRIPTOR:
1719 if (ctrl->bRequestType != USB_DIR_IN)
1720 goto unknown;
1721 switch (w_value >> 8) {
1722
1723 case USB_DT_DEVICE:
1724 cdev->desc.bNumConfigurations =
1725 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001726 cdev->desc.bMaxPacketSize0 =
1727 cdev->gadget->ep0->maxpacket;
Mayank Rana72bf6d02016-12-19 14:48:38 -08001728 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001729 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001730 if (gadget->speed >= USB_SPEED_SUPER) {
John Youn1a853292016-02-05 17:05:40 -08001731 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001732 cdev->desc.bMaxPacketSize0 = 9;
Hemant Kumar7e115fd2017-07-19 12:08:16 -07001733 } else if (!disable_l1_for_hs) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001734 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001735 }
Hemant Kumar7e115fd2017-07-19 12:08:16 -07001736 } else if (!disable_l1_for_hs) {
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001737 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1738 DBG(cdev, "Config HS device with LPM(L1)\n");
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001739 }
1740
David Brownell40982be2008-06-19 17:52:58 -07001741 value = min(w_length, (u16) sizeof cdev->desc);
1742 memcpy(req->buf, &cdev->desc, value);
1743 break;
1744 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001745 if (!gadget_is_dualspeed(gadget) ||
1746 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001747 break;
1748 device_qual(cdev);
1749 value = min_t(int, w_length,
1750 sizeof(struct usb_qualifier_descriptor));
1751 break;
1752 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001753 if (!gadget_is_dualspeed(gadget) ||
1754 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001755 break;
1756 /* FALLTHROUGH */
1757 case USB_DT_CONFIG:
1758 value = config_desc(cdev, w_value);
1759 if (value >= 0)
1760 value = min(w_length, (u16) value);
1761 break;
1762 case USB_DT_STRING:
1763 value = get_string(cdev, req->buf,
1764 w_index, w_value & 0xff);
1765 if (value >= 0)
1766 value = min(w_length, (u16) value);
1767 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001768 case USB_DT_BOS:
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001769 if (gadget_is_superspeed(gadget) ||
Hemant Kumar7e115fd2017-07-19 12:08:16 -07001770 !disable_l1_for_hs) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001771 value = bos_desc(cdev);
1772 value = min(w_length, (u16) value);
1773 }
1774 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001775 case USB_DT_OTG:
1776 if (gadget_is_otg(gadget)) {
1777 struct usb_configuration *config;
1778 int otg_desc_len = 0;
1779
1780 if (cdev->config)
1781 config = cdev->config;
1782 else
1783 config = list_first_entry(
1784 &cdev->configs,
1785 struct usb_configuration, list);
1786 if (!config)
1787 goto done;
1788
1789 if (gadget->otg_caps &&
1790 (gadget->otg_caps->otg_rev >= 0x0200))
1791 otg_desc_len += sizeof(
1792 struct usb_otg20_descriptor);
1793 else
1794 otg_desc_len += sizeof(
1795 struct usb_otg_descriptor);
1796
1797 value = min_t(int, w_length, otg_desc_len);
1798 memcpy(req->buf, config->descriptors[0], value);
1799 }
1800 break;
David Brownell40982be2008-06-19 17:52:58 -07001801 }
1802 break;
1803
1804 /* any number of configs can work */
1805 case USB_REQ_SET_CONFIGURATION:
1806 if (ctrl->bRequestType != 0)
1807 goto unknown;
1808 if (gadget_is_otg(gadget)) {
1809 if (gadget->a_hnp_support)
1810 DBG(cdev, "HNP available\n");
1811 else if (gadget->a_alt_hnp_support)
1812 DBG(cdev, "HNP on another port\n");
1813 else
1814 VDBG(cdev, "HNP inactive\n");
1815 }
1816 spin_lock(&cdev->lock);
1817 value = set_config(cdev, ctrl, w_value);
1818 spin_unlock(&cdev->lock);
1819 break;
1820 case USB_REQ_GET_CONFIGURATION:
1821 if (ctrl->bRequestType != USB_DIR_IN)
1822 goto unknown;
1823 if (cdev->config)
1824 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1825 else
1826 *(u8 *)req->buf = 0;
1827 value = min(w_length, (u16) 1);
1828 break;
1829
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001830 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001831 case USB_REQ_SET_INTERFACE:
1832 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1833 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001834 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001835 break;
Bryan Wu08889512009-01-08 00:21:19 +08001836 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001837 if (!f)
1838 break;
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001839
1840 /*
1841 * If there's no get_alt() method, we know only altsetting zero
1842 * works. There is no need to check if set_alt() is not NULL
1843 * as we check this in usb_add_function().
1844 */
1845 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001846 break;
1847 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001848 if (value == USB_GADGET_DELAYED_STATUS) {
1849 DBG(cdev,
1850 "%s: interface %d (%s) requested delayed status\n",
1851 __func__, intf, f->name);
1852 cdev->delayed_status++;
1853 DBG(cdev, "delayed_status count %d\n",
1854 cdev->delayed_status);
1855 }
David Brownell40982be2008-06-19 17:52:58 -07001856 break;
1857 case USB_REQ_GET_INTERFACE:
1858 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1859 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001860 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001861 break;
Bryan Wu08889512009-01-08 00:21:19 +08001862 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001863 if (!f)
1864 break;
1865 /* lots of interfaces only need altsetting zero... */
1866 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1867 if (value < 0)
1868 break;
1869 *((u8 *)req->buf) = value;
1870 value = min(w_length, (u16) 1);
1871 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001872 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001873 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1874 (w_index == OTG_STS_SELECTOR)) {
1875 if (ctrl->bRequestType != (USB_DIR_IN |
1876 USB_RECIP_DEVICE))
1877 goto unknown;
1878 *((u8 *)req->buf) = gadget->host_request_flag;
1879 value = 1;
1880 break;
1881 }
1882
1883 /*
1884 * USB 3.0 additions:
1885 * Function driver should handle get_status request. If such cb
1886 * wasn't supplied we respond with default value = 0
1887 * Note: function driver should supply such cb only for the
1888 * first interface of the function
1889 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001890 if (!gadget_is_superspeed(gadget))
1891 goto unknown;
1892 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1893 goto unknown;
1894 value = 2; /* This is the length of the get_status reply */
1895 put_unaligned_le16(0, req->buf);
1896 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1897 break;
1898 f = cdev->config->interface[intf];
1899 if (!f)
1900 break;
1901 status = f->get_status ? f->get_status(f) : 0;
1902 if (status < 0)
1903 break;
1904 put_unaligned_le16(status & 0x0000ffff, req->buf);
1905 break;
1906 /*
1907 * Function drivers should handle SetFeature/ClearFeature
1908 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1909 * only for the first interface of the function
1910 */
1911 case USB_REQ_CLEAR_FEATURE:
1912 case USB_REQ_SET_FEATURE:
1913 if (!gadget_is_superspeed(gadget))
1914 goto unknown;
1915 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1916 goto unknown;
1917 switch (w_value) {
1918 case USB_INTRF_FUNC_SUSPEND:
1919 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1920 break;
1921 f = cdev->config->interface[intf];
1922 if (!f)
1923 break;
1924 value = 0;
Danny Segalf83e4512016-12-06 15:35:24 -08001925 if (f->func_suspend) {
1926 const u8 suspend_opt = w_index >> 8;
1927
1928 value = f->func_suspend(f, suspend_opt);
1929 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1930 f->name ? f->name : "", suspend_opt);
1931 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001932 if (value < 0) {
1933 ERROR(cdev,
1934 "func_suspend() returned error %d\n",
1935 value);
1936 value = 0;
1937 }
1938 break;
1939 }
1940 break;
David Brownell40982be2008-06-19 17:52:58 -07001941 default:
1942unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001943 /*
1944 * OS descriptors handling
1945 */
1946 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001947 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001948 ctrl->bRequest == cdev->b_vendor_code) {
1949 struct usb_request *req;
1950 struct usb_configuration *os_desc_cfg;
1951 u8 *buf;
1952 int interface;
1953 int count = 0;
1954
1955 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001956 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001957 req->complete = composite_setup_complete;
1958 buf = req->buf;
1959 os_desc_cfg = cdev->os_desc_config;
1960 memset(buf, 0, w_length);
1961 buf[5] = 0x01;
1962 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1963 case USB_RECIP_DEVICE:
1964 if (w_index != 0x4 || (w_value >> 8))
1965 break;
1966 buf[6] = w_index;
1967 if (w_length == 0x10) {
1968 /* Number of ext compat interfaces */
1969 count = count_ext_compat(os_desc_cfg);
1970 buf[8] = count;
1971 count *= 24; /* 24 B/ext compat desc */
1972 count += 16; /* header */
1973 put_unaligned_le32(count, buf);
1974 value = w_length;
1975 } else {
1976 /* "extended compatibility ID"s */
1977 count = count_ext_compat(os_desc_cfg);
1978 buf[8] = count;
1979 count *= 24; /* 24 B/ext compat desc */
1980 count += 16; /* header */
1981 put_unaligned_le32(count, buf);
1982 buf += 16;
1983 fill_ext_compat(os_desc_cfg, buf);
1984 value = w_length;
1985 }
1986 break;
1987 case USB_RECIP_INTERFACE:
1988 if (w_index != 0x5 || (w_value >> 8))
1989 break;
1990 interface = w_value & 0xFF;
1991 buf[6] = w_index;
1992 if (w_length == 0x0A) {
1993 count = count_ext_prop(os_desc_cfg,
1994 interface);
1995 put_unaligned_le16(count, buf + 8);
1996 count = len_ext_prop(os_desc_cfg,
1997 interface);
1998 put_unaligned_le32(count, buf);
1999
2000 value = w_length;
2001 } else {
2002 count = count_ext_prop(os_desc_cfg,
2003 interface);
2004 put_unaligned_le16(count, buf + 8);
2005 count = len_ext_prop(os_desc_cfg,
2006 interface);
2007 put_unaligned_le32(count, buf);
2008 buf += 10;
2009 value = fill_ext_prop(os_desc_cfg,
2010 interface, buf);
2011 if (value < 0)
2012 return value;
2013
2014 value = w_length;
2015 }
2016 break;
2017 }
William Wu7e14f47a2016-05-13 18:30:42 +08002018
2019 if (value >= 0) {
2020 req->length = value;
2021 req->context = cdev;
2022 req->zero = value < w_length;
2023 value = composite_ep0_queue(cdev, req,
2024 GFP_ATOMIC);
2025 if (value < 0) {
2026 DBG(cdev, "ep_queue --> %d\n", value);
2027 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302028 if (value != -ESHUTDOWN)
2029 composite_setup_complete(
2030 gadget->ep0, req);
William Wu7e14f47a2016-05-13 18:30:42 +08002031 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002032 }
2033 return value;
2034 }
2035
David Brownell40982be2008-06-19 17:52:58 -07002036 VDBG(cdev,
2037 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2038 ctrl->bRequestType, ctrl->bRequest,
2039 w_value, w_index, w_length);
2040
Laurent Pinchart52426582009-10-21 00:03:38 +02002041 /* functions always handle their interfaces and endpoints...
2042 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002043 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002044 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302045 if (cdev->config) {
2046 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b452016-06-22 01:12:08 +02002047 if (f->req_match &&
2048 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302049 goto try_fun_setup;
Felix Hädicke1a00b452016-06-22 01:12:08 +02002050 } else {
2051 struct usb_configuration *c;
2052 list_for_each_entry(c, &cdev->configs, list)
2053 list_for_each_entry(f, &c->functions, list)
2054 if (f->req_match &&
2055 f->req_match(f, ctrl, true))
2056 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302057 }
Felix Hädicke1a00b452016-06-22 01:12:08 +02002058 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302059
Laurent Pinchart52426582009-10-21 00:03:38 +02002060 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2061 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002062 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302063 break;
2064 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002065 break;
2066
2067 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002068 if (!cdev->config)
2069 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002070 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2071 list_for_each_entry(f, &cdev->config->functions, list) {
2072 if (test_bit(endp, f->endpoints))
2073 break;
2074 }
2075 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002076 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002077 break;
David Brownell40982be2008-06-19 17:52:58 -07002078 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002079try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002080 if (f && f->setup)
2081 value = f->setup(f, ctrl);
2082 else {
David Brownell40982be2008-06-19 17:52:58 -07002083 struct usb_configuration *c;
2084
2085 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002086 if (!c)
2087 goto done;
2088
2089 /* try current config's setup */
2090 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002091 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002092 goto done;
2093 }
2094
2095 /* try the only function in the current config */
2096 if (!list_is_singular(&c->functions))
2097 goto done;
2098 f = list_first_entry(&c->functions, struct usb_function,
2099 list);
2100 if (f->setup)
2101 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002102 }
Vijayavardhan Vennapusa8edd67762013-10-17 15:00:02 +05302103 if (value == USB_GADGET_DELAYED_STATUS) {
2104 DBG(cdev,
2105 "%s: interface %d (%s) requested delayed status\n",
2106 __func__, intf, f->name);
2107 cdev->delayed_status++;
2108 DBG(cdev, "delayed_status count %d\n",
2109 cdev->delayed_status);
2110 }
David Brownell40982be2008-06-19 17:52:58 -07002111
2112 goto done;
2113 }
2114
2115 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002116 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002117 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002118 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002119 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002120 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002121 if (value < 0) {
2122 DBG(cdev, "ep_queue --> %d\n", value);
2123 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302124 if (value != -ESHUTDOWN)
2125 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002126 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002127 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2128 WARN(cdev,
2129 "%s: Delayed status not supported for w_length != 0",
2130 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002131 }
2132
2133done:
2134 /* device either stalls (value < 0) or reports success */
2135 return value;
2136}
2137
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002138void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002139{
2140 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2141 unsigned long flags;
2142
Badhri Jagan Sridharan9e954092015-05-06 13:40:15 -07002143 if (cdev == NULL) {
2144 WARN(1, "%s: Calling disconnect on a Gadget that is \
2145 not connected\n", __func__);
2146 return;
2147 }
2148
David Brownell40982be2008-06-19 17:52:58 -07002149 /* REVISIT: should we have config and device level
2150 * disconnect callbacks?
2151 */
2152 spin_lock_irqsave(&cdev->lock, flags);
2153 if (cdev->config)
2154 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002155 if (cdev->driver->disconnect)
2156 cdev->driver->disconnect(cdev);
Pavankumar Kondeti47870672013-06-19 09:51:58 +05302157 if (cdev->delayed_status != 0) {
2158 INFO(cdev, "delayed status mismatch..resetting\n");
2159 cdev->delayed_status = 0;
2160 }
David Brownell40982be2008-06-19 17:52:58 -07002161 spin_unlock_irqrestore(&cdev->lock, flags);
2162}
2163
2164/*-------------------------------------------------------------------------*/
2165
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002166static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2167 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002168{
2169 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2170 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2171
Vijayavardhan Vennapusa5d2ac1a2017-09-13 15:41:20 +05302172 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002173}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002174static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002175
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002176static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002177{
2178 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002179 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2180 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002181
2182 /* composite_disconnect() must already have been called
2183 * by the underlying peripheral controller driver!
2184 * so there's no i/o concurrency that could affect the
2185 * state protected by cdev->lock.
2186 */
2187 WARN_ON(cdev->config);
2188
2189 while (!list_empty(&cdev->configs)) {
2190 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002191 c = list_first_entry(&cdev->configs,
2192 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002193 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002194 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002195 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002196 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002197
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002198 composite_dev_cleanup(cdev);
2199
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002200 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2201 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2202
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002203 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002204 kfree(cdev);
2205 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002206}
2207
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002208static void composite_unbind(struct usb_gadget *gadget)
2209{
2210 __composite_unbind(gadget, true);
2211}
2212
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002213static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2214 const struct usb_device_descriptor *old)
2215{
2216 __le16 idVendor;
2217 __le16 idProduct;
2218 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002219 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002220 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002221 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002222
2223 /*
2224 * these variables may have been set in
2225 * usb_composite_overwrite_options()
2226 */
2227 idVendor = new->idVendor;
2228 idProduct = new->idProduct;
2229 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002230 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002231 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002232 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002233
2234 *new = *old;
2235 if (idVendor)
2236 new->idVendor = idVendor;
2237 if (idProduct)
2238 new->idProduct = idProduct;
2239 if (bcdDevice)
2240 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002241 else
2242 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002243 if (iSerialNumber)
2244 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002245 if (iManufacturer)
2246 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002247 if (iProduct)
2248 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002249}
2250
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002251int composite_dev_prepare(struct usb_composite_driver *composite,
2252 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002253{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002254 struct usb_gadget *gadget = cdev->gadget;
2255 int ret = -ENOMEM;
2256
2257 /* preallocate control response and buffer */
2258 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2259 if (!cdev->req)
2260 return -ENOMEM;
2261
2262 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2263 if (!cdev->req->buf)
2264 goto fail;
2265
2266 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2267 if (ret)
2268 goto fail_dev;
2269
2270 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002271 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002272 gadget->ep0->driver_data = cdev;
2273
2274 cdev->driver = composite;
2275
2276 /*
2277 * As per USB compliance update, a device that is actively drawing
2278 * more than 100mA from USB must report itself as bus-powered in
2279 * the GetStatus(DEVICE) call.
2280 */
2281 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2282 usb_gadget_set_selfpowered(gadget);
2283
2284 /* interface and string IDs start at zero via kzalloc.
2285 * we force endpoints to start unassigned; few controller
2286 * drivers will zero ep->driver_data.
2287 */
2288 usb_ep_autoconfig_reset(gadget);
2289 return 0;
2290fail_dev:
2291 kfree(cdev->req->buf);
2292fail:
2293 usb_ep_free_request(gadget->ep0, cdev->req);
2294 cdev->req = NULL;
2295 return ret;
2296}
2297
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002298int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2299 struct usb_ep *ep0)
2300{
2301 int ret = 0;
2302
2303 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2304 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002305 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002306 goto end;
2307 }
2308
2309 /* OS feature descriptor length <= 4kB */
2310 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2311 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002312 ret = -ENOMEM;
Christophe JAILLETf0ee2032017-01-04 06:30:16 +01002313 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002314 goto end;
2315 }
Felipe Balbi57943712014-09-18 09:54:54 -05002316 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002317 cdev->os_desc_req->complete = composite_setup_complete;
2318end:
2319 return ret;
2320}
2321
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002322void composite_dev_cleanup(struct usb_composite_dev *cdev)
2323{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002324 struct usb_gadget_string_container *uc, *tmp;
2325
2326 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2327 list_del(&uc->list);
2328 kfree(uc);
2329 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002330 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002331 if (cdev->os_desc_pending)
2332 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2333
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002334 kfree(cdev->os_desc_req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002335 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002336 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002337 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002338 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002339 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002340 if (cdev->setup_pending)
2341 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2342
Li Junbe0a8882014-08-28 21:44:11 +08002343 kfree(cdev->req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002344 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002345 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002346 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002347 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002348 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002349 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002350}
2351
2352static int composite_bind(struct usb_gadget *gadget,
2353 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002354{
2355 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002356 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002357 int status = -ENOMEM;
2358
2359 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2360 if (!cdev)
2361 return status;
2362
2363 spin_lock_init(&cdev->lock);
2364 cdev->gadget = gadget;
2365 set_gadget_data(gadget, cdev);
2366 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002367 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002368
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002369 status = composite_dev_prepare(composite, cdev);
2370 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002371 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002372
2373 /* composite gadget needs to assign strings for whole device (like
2374 * serial number), register function drivers, potentially update
2375 * power state and consumption, etc
2376 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002377 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002378 if (status < 0)
2379 goto fail;
2380
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002381 if (cdev->use_os_string) {
2382 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2383 if (status)
2384 goto fail;
2385 }
2386
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002387 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002388
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002389 /* has userspace failed to provide a serial number? */
2390 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2391 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2392
David Brownell40982be2008-06-19 17:52:58 -07002393 INFO(cdev, "%s ready\n", composite->name);
2394 return 0;
2395
2396fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002397 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002398 return status;
2399}
2400
2401/*-------------------------------------------------------------------------*/
2402
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002403void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002404{
2405 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2406 struct usb_function *f;
Mayank Rana13e5c152015-10-05 19:08:47 -07002407 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002408
David Brownell89429392009-03-19 14:14:17 -07002409 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002410 * suspend/resume callbacks?
2411 */
2412 DBG(cdev, "suspend\n");
Mayank Rana13e5c152015-10-05 19:08:47 -07002413 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002414 if (cdev->config) {
2415 list_for_each_entry(f, &cdev->config->functions, list) {
2416 if (f->suspend)
2417 f->suspend(f);
2418 }
2419 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002420 if (cdev->driver->suspend)
2421 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002422
2423 cdev->suspended = 1;
Mayank Rana13e5c152015-10-05 19:08:47 -07002424 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002425
2426 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002427}
2428
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002429void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002430{
2431 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2432 struct usb_function *f;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302433 int ret;
2434 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002435
David Brownell89429392009-03-19 14:14:17 -07002436 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002437 * suspend/resume callbacks?
2438 */
2439 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002440 if (cdev->driver->resume)
2441 cdev->driver->resume(cdev);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002442
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302443 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002444 if (cdev->config) {
2445 list_for_each_entry(f, &cdev->config->functions, list) {
Mayank Ranab92dfd02014-11-25 15:29:58 -08002446 ret = usb_func_wakeup_int(f);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002447 if (ret) {
2448 if (ret == -EAGAIN) {
2449 ERROR(f->config->cdev,
2450 "Function wakeup for %s could not complete due to suspend state.\n",
2451 f->name ? f->name : "");
2452 break;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +05302453 } else if (ret != -ENOTSUPP) {
2454 ERROR(f->config->cdev,
2455 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2456 f->name ? f->name : "",
2457 ret);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002458 }
Danny Segal86cb50e2014-07-09 15:14:49 +03002459 }
2460
David Brownell40982be2008-06-19 17:52:58 -07002461 if (f->resume)
2462 f->resume(f);
2463 }
Hao Wub23f2f92010-11-29 15:17:03 +08002464
Mayank Rana4be36022016-12-19 15:03:00 -08002465 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
David Brownell40982be2008-06-19 17:52:58 -07002466 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002467
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302468 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002469 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002470}
2471
2472/*-------------------------------------------------------------------------*/
2473
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002474static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002475 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002476 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002477
2478 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002479 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002480 .disconnect = composite_disconnect,
2481
2482 .suspend = composite_suspend,
2483 .resume = composite_resume,
2484
2485 .driver = {
2486 .owner = THIS_MODULE,
2487 },
2488};
2489
2490/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002491 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002492 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002493 *
David Brownell40982be2008-06-19 17:52:58 -07002494 * Context: single threaded during gadget setup
2495 *
2496 * This function is used to register drivers using the composite driver
2497 * framework. The return value is zero, or a negative errno value.
2498 * Those values normally come from the driver's @bind method, which does
2499 * all the work of setting up the driver to match the hardware.
2500 *
2501 * On successful return, the gadget is ready to respond to requests from
2502 * the host, unless one of its components invokes usb_gadget_disconnect()
2503 * while it was binding. That would usually be done in order to wait for
2504 * some userspace participation.
2505 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002506int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002507{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002508 struct usb_gadget_driver *gadget_driver;
2509
2510 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002511 return -EINVAL;
2512
2513 if (!driver->name)
2514 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002515
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002516 driver->gadget_driver = composite_driver_template;
2517 gadget_driver = &driver->gadget_driver;
2518
2519 gadget_driver->function = (char *) driver->name;
2520 gadget_driver->driver.name = driver->name;
2521 gadget_driver->max_speed = driver->max_speed;
2522
2523 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002524}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002525EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002526
2527/**
2528 * usb_composite_unregister() - unregister a composite driver
2529 * @driver: the driver to unregister
2530 *
2531 * This function is used to unregister drivers using the composite
2532 * driver framework.
2533 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002534void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002535{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002536 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002537}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002538EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002539
2540/**
2541 * usb_composite_setup_continue() - Continue with the control transfer
2542 * @cdev: the composite device who's control transfer was kept waiting
2543 *
2544 * This function must be called by the USB function driver to continue
2545 * with the control transfer's data/status stage in case it had requested to
2546 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2547 * can request the composite framework to delay the setup request's data/status
2548 * stages by returning USB_GADGET_DELAYED_STATUS.
2549 */
2550void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2551{
2552 int value;
2553 struct usb_request *req = cdev->req;
2554 unsigned long flags;
2555
2556 DBG(cdev, "%s\n", __func__);
2557 spin_lock_irqsave(&cdev->lock, flags);
2558
2559 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302560 if (!cdev->config) {
2561 spin_unlock_irqrestore(&cdev->lock, flags);
2562 return;
2563 }
2564 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002565 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302566 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002567
2568 } else if (--cdev->delayed_status == 0) {
2569 DBG(cdev, "%s: Completing delayed status\n", __func__);
2570 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002571 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002572 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002573 if (value < 0) {
2574 DBG(cdev, "ep_queue --> %d\n", value);
2575 req->status = 0;
2576 composite_setup_complete(cdev->gadget->ep0, req);
2577 }
2578 }
2579
2580 spin_unlock_irqrestore(&cdev->lock, flags);
2581}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002582EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002583
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002584static char *composite_default_mfr(struct usb_gadget *gadget)
2585{
2586 char *mfr;
2587 int len;
2588
2589 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2590 init_utsname()->release, gadget->name);
2591 len++;
2592 mfr = kmalloc(len, GFP_KERNEL);
2593 if (!mfr)
2594 return NULL;
2595 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2596 init_utsname()->release, gadget->name);
2597 return mfr;
2598}
2599
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002600void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2601 struct usb_composite_overwrite *covr)
2602{
2603 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002604 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2605 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002606
2607 if (covr->idVendor)
2608 desc->idVendor = cpu_to_le16(covr->idVendor);
2609
2610 if (covr->idProduct)
2611 desc->idProduct = cpu_to_le16(covr->idProduct);
2612
2613 if (covr->bcdDevice)
2614 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002615
2616 if (covr->serial_number) {
2617 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2618 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2619 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002620 if (covr->manufacturer) {
2621 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2622 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002623
2624 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2625 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2626 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2627 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002628 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002629
2630 if (covr->product) {
2631 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2632 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2633 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002634}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002635EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002636
2637MODULE_LICENSE("GPL");
2638MODULE_AUTHOR("David Brownell");