blob: 31b273181261e707efaa5989bd0aa728207fc063 [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
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020038/**
39 * struct usb_os_string - represents OS String to be reported by a gadget
40 * @bLength: total length of the entire descritor, always 0x12
41 * @bDescriptorType: USB_DT_STRING
42 * @qwSignature: the OS String proper
43 * @bMS_VendorCode: code used by the host for subsequent requests
44 * @bPad: not used, must be zero
45 */
46struct usb_os_string {
47 __u8 bLength;
48 __u8 bDescriptorType;
49 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
50 __u8 bMS_VendorCode;
51 __u8 bPad;
52} __packed;
53
David Brownell40982be2008-06-19 17:52:58 -070054/*
55 * The code in this file is utility code, used to build a gadget driver
56 * from one or more "function" drivers, one or more "configuration"
57 * objects, and a "usb_composite_driver" by gluing them together along
58 * with the relevant device-wide data.
59 */
60
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010061static struct usb_gadget_strings **get_containers_gs(
62 struct usb_gadget_string_container *uc)
63{
64 return (struct usb_gadget_strings **)uc->stash;
65}
66
Tatyana Brokhman48767a42011-06-28 16:33:49 +030067/**
John Younf3bdbe32016-02-05 17:07:03 -080068 * function_descriptors() - get function descriptors for speed
69 * @f: the function
70 * @speed: the speed
71 *
72 * Returns the descriptors or NULL if not set.
73 */
74static struct usb_descriptor_header **
75function_descriptors(struct usb_function *f,
76 enum usb_device_speed speed)
77{
78 struct usb_descriptor_header **descriptors;
79
Felipe Balbi08782632016-04-22 14:53:47 +030080 /*
81 * NOTE: we try to help gadget drivers which might not be setting
82 * max_speed appropriately.
83 */
84
John Younf3bdbe32016-02-05 17:07:03 -080085 switch (speed) {
86 case USB_SPEED_SUPER_PLUS:
87 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030088 if (descriptors)
89 break;
90 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080091 case USB_SPEED_SUPER:
92 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030093 if (descriptors)
94 break;
95 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080096 case USB_SPEED_HIGH:
97 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030098 if (descriptors)
99 break;
100 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800101 default:
102 descriptors = f->fs_descriptors;
103 }
104
Felipe Balbi08782632016-04-22 14:53:47 +0300105 /*
106 * if we can't find any descriptors at all, then this gadget deserves to
107 * Oops with a NULL pointer dereference
108 */
109
John Younf3bdbe32016-02-05 17:07:03 -0800110 return descriptors;
111}
112
113/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300114 * next_ep_desc() - advance to the next EP descriptor
115 * @t: currect pointer within descriptor array
116 *
117 * Return: next EP descriptor or NULL
118 *
119 * Iterate over @t until either EP descriptor found or
120 * NULL (that indicates end of list) encountered
121 */
122static struct usb_descriptor_header**
123next_ep_desc(struct usb_descriptor_header **t)
124{
125 for (; *t; t++) {
126 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
127 return t;
128 }
129 return NULL;
130}
131
132/*
133 * for_each_ep_desc()- iterate over endpoint descriptors in the
134 * descriptors list
135 * @start: pointer within descriptor array.
136 * @ep_desc: endpoint descriptor to use as the loop cursor
137 */
138#define for_each_ep_desc(start, ep_desc) \
139 for (ep_desc = next_ep_desc(start); \
140 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
141
142/**
143 * config_ep_by_speed() - configures the given endpoint
144 * according to gadget speed.
145 * @g: pointer to the gadget
146 * @f: usb function
147 * @_ep: the endpoint to configure
148 *
149 * Return: error code, 0 on success
150 *
151 * This function chooses the right descriptors for a given
152 * endpoint according to gadget speed and saves it in the
153 * endpoint desc field. If the endpoint already has a descriptor
154 * assigned to it - overwrites it with currently corresponding
155 * descriptor. The endpoint maxpacket field is updated according
156 * to the chosen descriptor.
157 * Note: the supplied function should hold all the descriptors
158 * for supported speeds
159 */
160int config_ep_by_speed(struct usb_gadget *g,
161 struct usb_function *f,
162 struct usb_ep *_ep)
163{
Felipe Balbib785ea72012-06-06 10:20:23 +0300164 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300165 struct usb_endpoint_descriptor *chosen_desc = NULL;
166 struct usb_descriptor_header **speed_desc = NULL;
167
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300168 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
169 int want_comp_desc = 0;
170
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300171 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
172
173 if (!g || !f || !_ep)
174 return -EIO;
175
176 /* select desired speed */
177 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800178 case USB_SPEED_SUPER_PLUS:
179 if (gadget_is_superspeed_plus(g)) {
180 speed_desc = f->ssp_descriptors;
181 want_comp_desc = 1;
182 break;
183 }
184 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300185 case USB_SPEED_SUPER:
186 if (gadget_is_superspeed(g)) {
187 speed_desc = f->ss_descriptors;
188 want_comp_desc = 1;
189 break;
190 }
191 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300192 case USB_SPEED_HIGH:
193 if (gadget_is_dualspeed(g)) {
194 speed_desc = f->hs_descriptors;
195 break;
196 }
197 /* else: fall through */
198 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200199 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300200 }
201 /* find descriptors */
202 for_each_ep_desc(speed_desc, d_spd) {
203 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
204 if (chosen_desc->bEndpointAddress == _ep->address)
205 goto ep_found;
206 }
207 return -EIO;
208
209ep_found:
210 /* commit results */
Felipe Balbie0aa5ec2016-09-28 10:38:11 +0300211 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300212 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300213 _ep->comp_desc = NULL;
214 _ep->maxburst = 0;
Felipe Balbi3999c532016-09-28 12:33:31 +0300215 _ep->mult = 1;
216
217 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
218 usb_endpoint_xfer_int(_ep->desc)))
219 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
220
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300221 if (!want_comp_desc)
222 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300223
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300224 /*
225 * Companion descriptor should follow EP descriptor
226 * USB 3.0 spec, #9.6.7
227 */
228 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
229 if (!comp_desc ||
230 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
231 return -EIO;
232 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800233 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300234 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300235 case USB_ENDPOINT_XFER_ISOC:
236 /* mult: bits 1:0 of bmAttributes */
Felipe Balbi3999c532016-09-28 12:33:31 +0300237 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800238 case USB_ENDPOINT_XFER_BULK:
239 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300240 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300241 break;
242 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300243 if (comp_desc->bMaxBurst != 0)
244 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
245 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300246 break;
247 }
248 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300249 return 0;
250}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200251EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700252
253/**
254 * usb_add_function() - add a function to a configuration
255 * @config: the configuration
256 * @function: the function being added
257 * Context: single threaded during gadget setup
258 *
259 * After initialization, each configuration must have one or more
260 * functions added to it. Adding a function involves calling its @bind()
261 * method to allocate resources such as interface and string identifiers
262 * and endpoints.
263 *
264 * This function returns the value of the function's bind(), which is
265 * zero for success else a negative errno value.
266 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200267int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700268 struct usb_function *function)
269{
270 int value = -EINVAL;
271
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530272 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700273 function->name, function,
274 config->label, config);
275
276 if (!function->set_alt || !function->disable)
277 goto done;
278
279 function->config = config;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700280 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700281 list_add_tail(&function->list, &config->functions);
282
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200283 if (function->bind_deactivated) {
284 value = usb_function_deactivate(function);
285 if (value)
286 goto done;
287 }
288
David Brownell40982be2008-06-19 17:52:58 -0700289 /* REVISIT *require* function->bind? */
290 if (function->bind) {
291 value = function->bind(config, function);
292 if (value < 0) {
293 list_del(&function->list);
294 function->config = NULL;
295 }
296 } else
297 value = 0;
298
299 /* We allow configurations that don't work at both speeds.
300 * If we run into a lowspeed Linux system, treat it the same
301 * as full speed ... it's the function drivers that will need
302 * to avoid bulk and ISO transfers.
303 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200304 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700305 config->fullspeed = true;
306 if (!config->highspeed && function->hs_descriptors)
307 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300308 if (!config->superspeed && function->ss_descriptors)
309 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800310 if (!config->superspeed_plus && function->ssp_descriptors)
311 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700312
313done:
314 if (value)
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530315 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700316 function->name, function, value);
317 return value;
318}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200319EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700320
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100321void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
322{
323 if (f->disable)
324 f->disable(f);
325
326 bitmap_zero(f->endpoints, 32);
327 list_del(&f->list);
328 if (f->unbind)
329 f->unbind(c, f);
330}
331EXPORT_SYMBOL_GPL(usb_remove_function);
332
David Brownell40982be2008-06-19 17:52:58 -0700333/**
David Brownell60beed92008-08-18 17:38:22 -0700334 * usb_function_deactivate - prevent function and gadget enumeration
335 * @function: the function that isn't yet ready to respond
336 *
337 * Blocks response of the gadget driver to host enumeration by
338 * preventing the data line pullup from being activated. This is
339 * normally called during @bind() processing to change from the
340 * initial "ready to respond" state, or when a required resource
341 * becomes available.
342 *
343 * For example, drivers that serve as a passthrough to a userspace
344 * daemon can block enumeration unless that daemon (such as an OBEX,
345 * MTP, or print server) is ready to handle host requests.
346 *
347 * Not all systems support software control of their USB peripheral
348 * data pullups.
349 *
350 * Returns zero on success, else negative errno.
351 */
352int usb_function_deactivate(struct usb_function *function)
353{
354 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200355 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700356 int status = 0;
357
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200358 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700359
360 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200361 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700362 if (status == 0)
363 cdev->deactivations++;
364
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200365 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700366 return status;
367}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200368EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700369
370/**
371 * usb_function_activate - allow function and gadget enumeration
372 * @function: function on which usb_function_activate() was called
373 *
374 * Reverses effect of usb_function_deactivate(). If no more functions
375 * are delaying their activation, the gadget driver will respond to
376 * host enumeration procedures.
377 *
378 * Returns zero on success, else negative errno.
379 */
380int usb_function_activate(struct usb_function *function)
381{
382 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200383 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700384 int status = 0;
385
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200386 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700387
388 if (WARN_ON(cdev->deactivations == 0))
389 status = -EINVAL;
390 else {
391 cdev->deactivations--;
392 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200393 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700394 }
395
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200396 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700397 return status;
398}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200399EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700400
401/**
David Brownell40982be2008-06-19 17:52:58 -0700402 * usb_interface_id() - allocate an unused interface ID
403 * @config: configuration associated with the interface
404 * @function: function handling the interface
405 * Context: single threaded during gadget setup
406 *
407 * usb_interface_id() is called from usb_function.bind() callbacks to
408 * allocate new interface IDs. The function driver will then store that
409 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300410 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700411 * particularly changing its altsetting via set_alt(). There may
412 * also be class-specific or vendor-specific requests to handle.
413 *
414 * All interface identifier should be allocated using this routine, to
415 * ensure that for example different functions don't wrongly assign
416 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300417 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700418 * one configuration (or more than once in a given configuration) need
419 * multiple versions of the relevant descriptors.
420 *
421 * Returns the interface ID which was allocated; or -ENODEV if no
422 * more interface IDs can be allocated.
423 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200424int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700425 struct usb_function *function)
426{
427 unsigned id = config->next_interface_id;
428
429 if (id < MAX_CONFIG_INTERFACES) {
430 config->interface[id] = function;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700431 if (function->intf_id < 0)
432 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700433 config->next_interface_id = id + 1;
434 return id;
435 }
436 return -ENODEV;
437}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200438EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700439
Mayank Ranab92dfd02014-11-25 15:29:58 -0800440static int usb_func_wakeup_int(struct usb_function *func)
Danny Segalf83e4512016-12-06 15:35:24 -0800441{
442 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800443 struct usb_gadget *gadget;
444
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800445 pr_debug("%s - %s function wakeup\n",
446 __func__, func->name ? func->name : "");
Danny Segalf83e4512016-12-06 15:35:24 -0800447
448 if (!func || !func->config || !func->config->cdev ||
449 !func->config->cdev->gadget)
450 return -EINVAL;
451
452 gadget = func->config->cdev->gadget;
453 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
454 DBG(func->config->cdev,
455 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
456 gadget->speed,
457 func->func_wakeup_allowed);
458
459 return -ENOTSUPP;
460 }
461
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800462 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300463
464 return ret;
465}
466
467int usb_func_wakeup(struct usb_function *func)
468{
469 int ret;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530470 unsigned long flags;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300471
472 pr_debug("%s function wakeup\n",
473 func->name ? func->name : "");
474
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530475 spin_lock_irqsave(&func->config->cdev->lock, flags);
Mayank Ranab92dfd02014-11-25 15:29:58 -0800476 ret = usb_func_wakeup_int(func);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300477 if (ret == -EAGAIN) {
478 DBG(func->config->cdev,
479 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
480 func->name ? func->name : "");
Danny Segalde7cd8d2014-07-28 18:08:33 +0300481 ret = 0;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +0530482 } else if (ret < 0 && ret != -ENOTSUPP) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300483 ERROR(func->config->cdev,
484 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
485 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800486 }
487
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530488 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300489 return ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800490}
491EXPORT_SYMBOL(usb_func_wakeup);
492
493int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
494 struct usb_request *req, gfp_t gfp_flags)
495{
wHemant Kumare9e13922016-12-19 14:41:28 -0800496 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800497 struct usb_gadget *gadget;
498
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800499 if (!func || !func->config || !func->config->cdev ||
wHemant Kumare9e13922016-12-19 14:41:28 -0800500 !func->config->cdev->gadget || !ep || !req) {
501 ret = -EINVAL;
502 goto done;
503 }
Danny Segalf83e4512016-12-06 15:35:24 -0800504
505 pr_debug("Function %s queueing new data into ep %u\n",
506 func->name ? func->name : "", ep->address);
507
508 gadget = func->config->cdev->gadget;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800509 if (func->func_is_suspended && func->func_wakeup_allowed) {
510 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
511 if (ret == -EAGAIN) {
512 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
513 func->name ? func->name : "");
514 } else if (ret < 0 && ret != -ENOTSUPP) {
515 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
516 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800517 }
wHemant Kumare9e13922016-12-19 14:41:28 -0800518 goto done;
Danny Segalf83e4512016-12-06 15:35:24 -0800519 }
520
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800521 if (!func->func_is_suspended)
522 ret = 0;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800523
wHemant Kumare9e13922016-12-19 14:41:28 -0800524 if (func->func_is_suspended && !func->func_wakeup_allowed) {
525 ret = -ENOTSUPP;
526 goto done;
527 }
528
529 ret = usb_ep_queue(ep, req, gfp_flags);
530done:
Danny Segalf83e4512016-12-06 15:35:24 -0800531 return ret;
532}
533EXPORT_SYMBOL(usb_func_ep_queue);
534
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100535static u8 encode_bMaxPower(enum usb_device_speed speed,
536 struct usb_configuration *c)
537{
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700538 unsigned int val = CONFIG_USB_GADGET_VBUS_DRAW;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100539
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100540 switch (speed) {
541 case USB_SPEED_SUPER:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700542 /* with super-speed report 900mA */
543 val = SSUSB_GADGET_VBUS_DRAW;
544 return (u8)(val / SSUSB_GADGET_VBUS_DRAW_UNITS);
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100545 default:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700546 return DIV_ROUND_UP(val, HSUSB_GADGET_VBUS_DRAW_UNITS);
Joe Perches2b84f922013-10-08 16:01:37 -0700547 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100548}
549
David Brownell40982be2008-06-19 17:52:58 -0700550static int config_buf(struct usb_configuration *config,
551 enum usb_device_speed speed, void *buf, u8 type)
552{
553 struct usb_config_descriptor *c = buf;
554 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200555 int len;
David Brownell40982be2008-06-19 17:52:58 -0700556 struct usb_function *f;
557 int status;
558
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200559 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700560 /* write the config descriptor */
561 c = buf;
562 c->bLength = USB_DT_CONFIG_SIZE;
563 c->bDescriptorType = type;
564 /* wTotalLength is written later */
565 c->bNumInterfaces = config->next_interface_id;
566 c->bConfigurationValue = config->bConfigurationValue;
567 c->iConfiguration = config->iConfiguration;
568 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100569 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700570
571 /* There may be e.g. OTG descriptors */
572 if (config->descriptors) {
573 status = usb_descriptor_fillbuf(next, len,
574 config->descriptors);
575 if (status < 0)
576 return status;
577 len -= status;
578 next += status;
579 }
580
581 /* add each function's descriptors */
582 list_for_each_entry(f, &config->functions, list) {
583 struct usb_descriptor_header **descriptors;
584
John Younf3bdbe32016-02-05 17:07:03 -0800585 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700586 if (!descriptors)
587 continue;
588 status = usb_descriptor_fillbuf(next, len,
589 (const struct usb_descriptor_header **) descriptors);
590 if (status < 0)
591 return status;
592 len -= status;
593 next += status;
594 }
595
596 len = next - buf;
597 c->wTotalLength = cpu_to_le16(len);
598 return len;
599}
600
601static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
602{
603 struct usb_gadget *gadget = cdev->gadget;
604 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200605 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700606 u8 type = w_value >> 8;
607 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
608
John Youneae58202016-02-05 17:07:17 -0800609 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300610 speed = gadget->speed;
611 else if (gadget_is_dualspeed(gadget)) {
612 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700613 if (gadget->speed == USB_SPEED_HIGH)
614 hs = 1;
615 if (type == USB_DT_OTHER_SPEED_CONFIG)
616 hs = !hs;
617 if (hs)
618 speed = USB_SPEED_HIGH;
619
620 }
621
622 /* This is a lookup by config *INDEX* */
623 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200624
625 pos = &cdev->configs;
626 c = cdev->os_desc_config;
627 if (c)
628 goto check_config;
629
630 while ((pos = pos->next) != &cdev->configs) {
631 c = list_entry(pos, typeof(*c), list);
632
633 /* skip OS Descriptors config which is handled separately */
634 if (c == cdev->os_desc_config)
635 continue;
636
637check_config:
David Brownell40982be2008-06-19 17:52:58 -0700638 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300639 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800640 case USB_SPEED_SUPER_PLUS:
641 if (!c->superspeed_plus)
642 continue;
643 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300644 case USB_SPEED_SUPER:
645 if (!c->superspeed)
646 continue;
647 break;
648 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700649 if (!c->highspeed)
650 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300651 break;
652 default:
David Brownell40982be2008-06-19 17:52:58 -0700653 if (!c->fullspeed)
654 continue;
655 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300656
David Brownell40982be2008-06-19 17:52:58 -0700657 if (w_value == 0)
658 return config_buf(c, speed, cdev->req->buf, type);
659 w_value--;
660 }
661 return -EINVAL;
662}
663
664static int count_configs(struct usb_composite_dev *cdev, unsigned type)
665{
666 struct usb_gadget *gadget = cdev->gadget;
667 struct usb_configuration *c;
668 unsigned count = 0;
669 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300670 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800671 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700672
673 if (gadget_is_dualspeed(gadget)) {
674 if (gadget->speed == USB_SPEED_HIGH)
675 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300676 if (gadget->speed == USB_SPEED_SUPER)
677 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800678 if (gadget->speed == USB_SPEED_SUPER_PLUS)
679 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700680 if (type == USB_DT_DEVICE_QUALIFIER)
681 hs = !hs;
682 }
683 list_for_each_entry(c, &cdev->configs, list) {
684 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800685 if (ssp) {
686 if (!c->superspeed_plus)
687 continue;
688 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300689 if (!c->superspeed)
690 continue;
691 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700692 if (!c->highspeed)
693 continue;
694 } else {
695 if (!c->fullspeed)
696 continue;
697 }
698 count++;
699 }
700 return count;
701}
702
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300703/**
704 * bos_desc() - prepares the BOS descriptor.
705 * @cdev: pointer to usb_composite device to generate the bos
706 * descriptor for
707 *
708 * This function generates the BOS (Binary Device Object)
709 * descriptor and its device capabilities descriptors. The BOS
710 * descriptor should be supported by a SuperSpeed device.
711 */
712static int bos_desc(struct usb_composite_dev *cdev)
713{
714 struct usb_ext_cap_descriptor *usb_ext;
715 struct usb_ss_cap_descriptor *ss_cap;
716 struct usb_dcd_config_params dcd_config_params;
717 struct usb_bos_descriptor *bos = cdev->req->buf;
718
719 bos->bLength = USB_DT_BOS_SIZE;
720 bos->bDescriptorType = USB_DT_BOS;
721
722 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
723 bos->bNumDeviceCaps = 0;
724
725 /*
726 * A SuperSpeed device shall include the USB2.0 extension descriptor
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800727 * and shall support LPM when operating in USB2.0 HS mode, as well as
728 * a HS device when operating in USB2.1 HS mode.
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300729 */
730 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
731 bos->bNumDeviceCaps++;
732 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
733 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
734 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
735 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500736 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300737
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800738 if (gadget_is_superspeed(cdev->gadget)) {
739 /*
740 * The Superspeed USB Capability descriptor shall be
741 * implemented by all SuperSpeed devices.
742 */
743 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
744 bos->bNumDeviceCaps++;
745 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
746 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
747 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
748 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
749 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
750 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
751 USB_FULL_SPEED_OPERATION |
752 USB_HIGH_SPEED_OPERATION |
753 USB_5GBPS_OPERATION);
754 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300755
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800756 /* Get Controller configuration */
757 if (cdev->gadget->ops->get_config_params)
758 cdev->gadget->ops->get_config_params
759 (&dcd_config_params);
760 else {
761 dcd_config_params.bU1devExitLat =
762 USB_DEFAULT_U1_DEV_EXIT_LAT;
763 dcd_config_params.bU2DevExitLat =
764 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
765 }
766 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
767 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300768 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300769
John Younf228a8d2016-02-05 17:05:53 -0800770 /* The SuperSpeedPlus USB Device Capability descriptor */
771 if (gadget_is_superspeed_plus(cdev->gadget)) {
772 struct usb_ssp_cap_descriptor *ssp_cap;
773
774 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
775 bos->bNumDeviceCaps++;
776
777 /*
778 * Report typical values.
779 */
780
781 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
782 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
783 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
784 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700785 ssp_cap->bReserved = 0;
786 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800787
788 /* SSAC = 1 (2 attributes) */
789 ssp_cap->bmAttributes = cpu_to_le32(1);
790
791 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700792 ssp_cap->wFunctionalitySupport =
793 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800794
795 /*
796 * bmSublinkSpeedAttr[0]:
797 * ST = Symmetric, RX
798 * LSE = 3 (Gbps)
799 * LP = 1 (SuperSpeedPlus)
800 * LSM = 10 (10 Gbps)
801 */
802 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700803 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800804 /*
805 * bmSublinkSpeedAttr[1] =
806 * ST = Symmetric, TX
807 * LSE = 3 (Gbps)
808 * LP = 1 (SuperSpeedPlus)
809 * LSM = 10 (10 Gbps)
810 */
811 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700812 cpu_to_le32((3 << 4) | (1 << 14) |
813 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800814 }
815
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300816 return le16_to_cpu(bos->wTotalLength);
817}
818
David Brownell40982be2008-06-19 17:52:58 -0700819static void device_qual(struct usb_composite_dev *cdev)
820{
821 struct usb_qualifier_descriptor *qual = cdev->req->buf;
822
823 qual->bLength = sizeof(*qual);
824 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
825 /* POLICY: same bcdUSB and device type info at both speeds */
826 qual->bcdUSB = cdev->desc.bcdUSB;
827 qual->bDeviceClass = cdev->desc.bDeviceClass;
828 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
829 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
830 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200831 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700832 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700833 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700834}
835
836/*-------------------------------------------------------------------------*/
837
838static void reset_config(struct usb_composite_dev *cdev)
839{
840 struct usb_function *f;
841
842 DBG(cdev, "reset config\n");
843
844 list_for_each_entry(f, &cdev->config->functions, list) {
845 if (f->disable)
846 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200847
Danny Segalf83e4512016-12-06 15:35:24 -0800848 /* USB 3.0 addition */
849 f->func_is_suspended = false;
850 f->func_wakeup_allowed = false;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300851 f->func_wakeup_pending = false;
Danny Segalf83e4512016-12-06 15:35:24 -0800852
Laurent Pinchart52426582009-10-21 00:03:38 +0200853 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700854 }
855 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100856 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700857}
858
859static int set_config(struct usb_composite_dev *cdev,
860 const struct usb_ctrlrequest *ctrl, unsigned number)
861{
862 struct usb_gadget *gadget = cdev->gadget;
863 struct usb_configuration *c = NULL;
864 int result = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700865 int tmp;
866
David Brownell40982be2008-06-19 17:52:58 -0700867 if (number) {
868 list_for_each_entry(c, &cdev->configs, list) {
869 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300870 /*
871 * We disable the FDs of the previous
872 * configuration only if the new configuration
873 * is a valid one
874 */
875 if (cdev->config)
876 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700877 result = 0;
878 break;
879 }
880 }
881 if (result < 0)
882 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300883 } else { /* Zero configuration value - need to reset the config */
884 if (cdev->config)
885 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700886 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300887 }
David Brownell40982be2008-06-19 17:52:58 -0700888
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200889 INFO(cdev, "%s config #%d: %s\n",
890 usb_speed_string(gadget->speed),
891 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700892
893 if (!c)
894 goto done;
895
Peter Chen6027f312014-04-29 13:26:28 +0800896 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700897 cdev->config = c;
Jack Phamdb943d62016-12-16 11:21:16 -0800898 c->num_ineps_used = 0;
899 c->num_outeps_used = 0;
David Brownell40982be2008-06-19 17:52:58 -0700900
901 /* Initialize all interfaces by setting them to altsetting zero. */
902 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
903 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200904 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700905
906 if (!f)
907 break;
908
Laurent Pinchart52426582009-10-21 00:03:38 +0200909 /*
910 * Record which endpoints are used by the function. This is used
911 * to dispatch control requests targeted at that endpoint to the
912 * function's setup callback instead of the current
913 * configuration's setup callback.
914 */
John Younf3bdbe32016-02-05 17:07:03 -0800915 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200916
917 for (; *descriptors; ++descriptors) {
918 struct usb_endpoint_descriptor *ep;
919 int addr;
920
921 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
922 continue;
923
924 ep = (struct usb_endpoint_descriptor *)*descriptors;
925 addr = ((ep->bEndpointAddress & 0x80) >> 3)
926 | (ep->bEndpointAddress & 0x0f);
927 set_bit(addr, f->endpoints);
Jack Phamdb943d62016-12-16 11:21:16 -0800928 if (usb_endpoint_dir_in(ep))
929 c->num_ineps_used++;
930 else
931 c->num_outeps_used++;
Laurent Pinchart52426582009-10-21 00:03:38 +0200932 }
933
David Brownell40982be2008-06-19 17:52:58 -0700934 result = f->set_alt(f, tmp, 0);
935 if (result < 0) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530936 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700937 tmp, f->name, f, result);
938
939 reset_config(cdev);
940 goto done;
941 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300942
943 if (result == USB_GADGET_DELAYED_STATUS) {
944 DBG(cdev,
945 "%s: interface %d (%s) requested delayed status\n",
946 __func__, tmp, f->name);
947 cdev->delayed_status++;
948 DBG(cdev, "delayed_status count %d\n",
949 cdev->delayed_status);
950 }
David Brownell40982be2008-06-19 17:52:58 -0700951 }
952
David Brownell40982be2008-06-19 17:52:58 -0700953done:
Mayank Rana4be36022016-12-19 15:03:00 -0800954 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
Roger Quadros1b9ba002011-05-09 13:08:06 +0300955 if (result >= 0 && cdev->delayed_status)
956 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700957 return result;
958}
959
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100960int usb_add_config_only(struct usb_composite_dev *cdev,
961 struct usb_configuration *config)
962{
963 struct usb_configuration *c;
964
965 if (!config->bConfigurationValue)
966 return -EINVAL;
967
968 /* Prevent duplicate configuration identifiers */
969 list_for_each_entry(c, &cdev->configs, list) {
970 if (c->bConfigurationValue == config->bConfigurationValue)
971 return -EBUSY;
972 }
973
974 config->cdev = cdev;
975 list_add_tail(&config->list, &cdev->configs);
976
977 INIT_LIST_HEAD(&config->functions);
978 config->next_interface_id = 0;
979 memset(config->interface, 0, sizeof(config->interface));
980
981 return 0;
982}
983EXPORT_SYMBOL_GPL(usb_add_config_only);
984
David Brownell40982be2008-06-19 17:52:58 -0700985/**
986 * usb_add_config() - add a configuration to a device.
987 * @cdev: wraps the USB gadget
988 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200989 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700990 * Context: single threaded during gadget setup
991 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200992 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700993 * add each of the configurations it supports, using this routine.
994 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200995 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700996 * is zero for success else a negative errno value. Binding configurations
997 * assigns global resources including string IDs, and per-configuration
998 * resources such as interface IDs and endpoints.
999 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001000int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001001 struct usb_configuration *config,
1002 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001003{
1004 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001005
1006 if (!bind)
1007 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001008
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301009 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001010 config->bConfigurationValue,
1011 config->label, config);
1012
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001013 status = usb_add_config_only(cdev, config);
1014 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001015 goto done;
1016
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001017 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001018 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001019 while (!list_empty(&config->functions)) {
1020 struct usb_function *f;
1021
1022 f = list_first_entry(&config->functions,
1023 struct usb_function, list);
1024 list_del(&f->list);
1025 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301026 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001027 f->name, f);
1028 f->unbind(config, f);
1029 /* may free memory for "f" */
1030 }
1031 }
David Brownell40982be2008-06-19 17:52:58 -07001032 list_del(&config->list);
1033 config->cdev = NULL;
1034 } else {
1035 unsigned i;
1036
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301037 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001038 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001039 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001040 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001041 config->highspeed ? " high" : "",
1042 config->fullspeed
1043 ? (gadget_is_dualspeed(cdev->gadget)
1044 ? " full"
1045 : " full/low")
1046 : "");
1047
1048 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1049 struct usb_function *f = config->interface[i];
1050
1051 if (!f)
1052 continue;
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301053 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001054 i, f->name, f);
1055 }
1056 }
1057
Robert Baldygaf871cb92015-09-16 12:10:39 +02001058 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001059 usb_ep_autoconfig_reset(cdev->gadget);
1060
1061done:
1062 if (status)
1063 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1064 config->bConfigurationValue, status);
1065 return status;
1066}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001067EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001068
Benoit Goby51cce6f2012-05-10 10:07:57 +02001069static void remove_config(struct usb_composite_dev *cdev,
1070 struct usb_configuration *config)
1071{
1072 while (!list_empty(&config->functions)) {
1073 struct usb_function *f;
1074
1075 f = list_first_entry(&config->functions,
1076 struct usb_function, list);
1077 list_del(&f->list);
1078 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301079 DBG(cdev, "unbind function '%s'/%pK\n", f->name, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001080 f->unbind(config, f);
1081 /* may free memory for "f" */
1082 }
1083 }
1084 list_del(&config->list);
1085 if (config->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301086 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001087 config->unbind(config);
1088 /* may free memory for "c" */
1089 }
1090}
1091
1092/**
1093 * usb_remove_config() - remove a configuration from a device.
1094 * @cdev: wraps the USB gadget
1095 * @config: the configuration
1096 *
1097 * Drivers must call usb_gadget_disconnect before calling this function
1098 * to disconnect the device from the host and make sure the host will not
1099 * try to enumerate the device while we are changing the config list.
1100 */
1101void usb_remove_config(struct usb_composite_dev *cdev,
1102 struct usb_configuration *config)
1103{
1104 unsigned long flags;
1105
1106 spin_lock_irqsave(&cdev->lock, flags);
1107
1108 if (cdev->config == config)
1109 reset_config(cdev);
1110
1111 spin_unlock_irqrestore(&cdev->lock, flags);
1112
1113 remove_config(cdev, config);
1114}
1115
David Brownell40982be2008-06-19 17:52:58 -07001116/*-------------------------------------------------------------------------*/
1117
1118/* We support strings in multiple languages ... string descriptor zero
1119 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001120 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001121 * the host side.
1122 */
1123
1124static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1125{
1126 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001127 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001128 __le16 *tmp;
1129
1130 while (*sp) {
1131 s = *sp;
1132 language = cpu_to_le16(s->language);
1133 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1134 if (*tmp == language)
1135 goto repeat;
1136 }
1137 *tmp++ = language;
1138repeat:
1139 sp++;
1140 }
1141}
1142
1143static int lookup_string(
1144 struct usb_gadget_strings **sp,
1145 void *buf,
1146 u16 language,
1147 int id
1148)
1149{
1150 struct usb_gadget_strings *s;
1151 int value;
1152
1153 while (*sp) {
1154 s = *sp++;
1155 if (s->language != language)
1156 continue;
1157 value = usb_gadget_get_string(s, id, buf);
1158 if (value > 0)
1159 return value;
1160 }
1161 return -EINVAL;
1162}
1163
1164static int get_string(struct usb_composite_dev *cdev,
1165 void *buf, u16 language, int id)
1166{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001167 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001168 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001169 struct usb_configuration *c;
1170 struct usb_function *f;
1171 int len;
1172
Diego Violaad4676a2015-05-31 15:52:41 -03001173 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001174 * folk will ever care about ... also, it's all supported here.
1175 * (Except for UTF8 support for Unicode's "Astral Planes".)
1176 */
1177
1178 /* 0 == report all available language codes */
1179 if (id == 0) {
1180 struct usb_string_descriptor *s = buf;
1181 struct usb_gadget_strings **sp;
1182
1183 memset(s, 0, 256);
1184 s->bDescriptorType = USB_DT_STRING;
1185
1186 sp = composite->strings;
1187 if (sp)
1188 collect_langs(sp, s->wData);
1189
1190 list_for_each_entry(c, &cdev->configs, list) {
1191 sp = c->strings;
1192 if (sp)
1193 collect_langs(sp, s->wData);
1194
1195 list_for_each_entry(f, &c->functions, list) {
1196 sp = f->strings;
1197 if (sp)
1198 collect_langs(sp, s->wData);
1199 }
1200 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001201 list_for_each_entry(uc, &cdev->gstrings, list) {
1202 struct usb_gadget_strings **sp;
1203
1204 sp = get_containers_gs(uc);
1205 collect_langs(sp, s->wData);
1206 }
David Brownell40982be2008-06-19 17:52:58 -07001207
Roel Kluin417b57b2009-08-06 16:09:51 -07001208 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001209 continue;
1210 if (!len)
1211 return -EINVAL;
1212
1213 s->bLength = 2 * (len + 1);
1214 return s->bLength;
1215 }
1216
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001217 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1218 struct usb_os_string *b = buf;
1219 b->bLength = sizeof(*b);
1220 b->bDescriptorType = USB_DT_STRING;
1221 compiletime_assert(
1222 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1223 "qwSignature size must be equal to qw_sign");
1224 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1225 b->bMS_VendorCode = cdev->b_vendor_code;
1226 b->bPad = 0;
1227 return sizeof(*b);
1228 }
1229
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001230 list_for_each_entry(uc, &cdev->gstrings, list) {
1231 struct usb_gadget_strings **sp;
1232
1233 sp = get_containers_gs(uc);
1234 len = lookup_string(sp, buf, language, id);
1235 if (len > 0)
1236 return len;
1237 }
1238
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001239 /* String IDs are device-scoped, so we look up each string
1240 * table we're told about. These lookups are infrequent;
1241 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001242 */
1243 if (composite->strings) {
1244 len = lookup_string(composite->strings, buf, language, id);
1245 if (len > 0)
1246 return len;
1247 }
1248 list_for_each_entry(c, &cdev->configs, list) {
1249 if (c->strings) {
1250 len = lookup_string(c->strings, buf, language, id);
1251 if (len > 0)
1252 return len;
1253 }
1254 list_for_each_entry(f, &c->functions, list) {
1255 if (!f->strings)
1256 continue;
1257 len = lookup_string(f->strings, buf, language, id);
1258 if (len > 0)
1259 return len;
1260 }
1261 }
1262 return -EINVAL;
1263}
1264
1265/**
1266 * usb_string_id() - allocate an unused string ID
1267 * @cdev: the device whose string descriptor IDs are being allocated
1268 * Context: single threaded during gadget setup
1269 *
1270 * @usb_string_id() is called from bind() callbacks to allocate
1271 * string IDs. Drivers for functions, configurations, or gadgets will
1272 * then store that ID in the appropriate descriptors and string table.
1273 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001274 * All string identifier should be allocated using this,
1275 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1276 * that for example different functions don't wrongly assign different
1277 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001278 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001279int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001280{
1281 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001282 /* string id 0 is reserved by USB spec for list of
1283 * supported languages */
1284 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001285 cdev->next_string_id++;
1286 return cdev->next_string_id;
1287 }
1288 return -ENODEV;
1289}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001290EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001291
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001292/**
1293 * usb_string_ids() - allocate unused string IDs in batch
1294 * @cdev: the device whose string descriptor IDs are being allocated
1295 * @str: an array of usb_string objects to assign numbers to
1296 * Context: single threaded during gadget setup
1297 *
1298 * @usb_string_ids() is called from bind() callbacks to allocate
1299 * string IDs. Drivers for functions, configurations, or gadgets will
1300 * then copy IDs from the string table to the appropriate descriptors
1301 * and string table for other languages.
1302 *
1303 * All string identifier should be allocated using this,
1304 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1305 * example different functions don't wrongly assign different meanings
1306 * to the same identifier.
1307 */
1308int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1309{
1310 int next = cdev->next_string_id;
1311
1312 for (; str->s; ++str) {
1313 if (unlikely(next >= 254))
1314 return -ENODEV;
1315 str->id = ++next;
1316 }
1317
1318 cdev->next_string_id = next;
1319
1320 return 0;
1321}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001322EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001323
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001324static struct usb_gadget_string_container *copy_gadget_strings(
1325 struct usb_gadget_strings **sp, unsigned n_gstrings,
1326 unsigned n_strings)
1327{
1328 struct usb_gadget_string_container *uc;
1329 struct usb_gadget_strings **gs_array;
1330 struct usb_gadget_strings *gs;
1331 struct usb_string *s;
1332 unsigned mem;
1333 unsigned n_gs;
1334 unsigned n_s;
1335 void *stash;
1336
1337 mem = sizeof(*uc);
1338 mem += sizeof(void *) * (n_gstrings + 1);
1339 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1340 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1341 uc = kmalloc(mem, GFP_KERNEL);
1342 if (!uc)
1343 return ERR_PTR(-ENOMEM);
1344 gs_array = get_containers_gs(uc);
1345 stash = uc->stash;
1346 stash += sizeof(void *) * (n_gstrings + 1);
1347 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1348 struct usb_string *org_s;
1349
1350 gs_array[n_gs] = stash;
1351 gs = gs_array[n_gs];
1352 stash += sizeof(struct usb_gadget_strings);
1353 gs->language = sp[n_gs]->language;
1354 gs->strings = stash;
1355 org_s = sp[n_gs]->strings;
1356
1357 for (n_s = 0; n_s < n_strings; n_s++) {
1358 s = stash;
1359 stash += sizeof(struct usb_string);
1360 if (org_s->s)
1361 s->s = org_s->s;
1362 else
1363 s->s = "";
1364 org_s++;
1365 }
1366 s = stash;
1367 s->s = NULL;
1368 stash += sizeof(struct usb_string);
1369
1370 }
1371 gs_array[n_gs] = NULL;
1372 return uc;
1373}
1374
1375/**
1376 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1377 * @cdev: the device whose string descriptor IDs are being allocated
1378 * and attached.
1379 * @sp: an array of usb_gadget_strings to attach.
1380 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1381 *
1382 * This function will create a deep copy of usb_gadget_strings and usb_string
1383 * and attach it to the cdev. The actual string (usb_string.s) will not be
1384 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001385 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001386 * The ->language pointer of each struct usb_gadget_strings has to contain the
1387 * same amount of entries.
1388 * 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 +09001389 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001390 * entry of en-US. Therefore both entries become the same id assign.
1391 */
1392struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1393 struct usb_gadget_strings **sp, unsigned n_strings)
1394{
1395 struct usb_gadget_string_container *uc;
1396 struct usb_gadget_strings **n_gs;
1397 unsigned n_gstrings = 0;
1398 unsigned i;
1399 int ret;
1400
1401 for (i = 0; sp[i]; i++)
1402 n_gstrings++;
1403
1404 if (!n_gstrings)
1405 return ERR_PTR(-EINVAL);
1406
1407 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1408 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001409 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001410
1411 n_gs = get_containers_gs(uc);
1412 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1413 if (ret)
1414 goto err;
1415
1416 for (i = 1; i < n_gstrings; i++) {
1417 struct usb_string *m_s;
1418 struct usb_string *s;
1419 unsigned n;
1420
1421 m_s = n_gs[0]->strings;
1422 s = n_gs[i]->strings;
1423 for (n = 0; n < n_strings; n++) {
1424 s->id = m_s->id;
1425 s++;
1426 m_s++;
1427 }
1428 }
1429 list_add_tail(&uc->list, &cdev->gstrings);
1430 return n_gs[0]->strings;
1431err:
1432 kfree(uc);
1433 return ERR_PTR(ret);
1434}
1435EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1436
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001437/**
1438 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001439 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001440 * @n: number of string IDs to allocate
1441 * Context: single threaded during gadget setup
1442 *
1443 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001444 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001445 * is, returns last requested ID which is now very useful information.
1446 *
1447 * @usb_string_ids_n() is called from bind() callbacks to allocate
1448 * string IDs. Drivers for functions, configurations, or gadgets will
1449 * then store that ID in the appropriate descriptors and string table.
1450 *
1451 * All string identifier should be allocated using this,
1452 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1453 * example different functions don't wrongly assign different meanings
1454 * to the same identifier.
1455 */
1456int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1457{
1458 unsigned next = c->next_string_id;
1459 if (unlikely(n > 254 || (unsigned)next + n > 254))
1460 return -ENODEV;
1461 c->next_string_id += n;
1462 return next + 1;
1463}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001464EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001465
David Brownell40982be2008-06-19 17:52:58 -07001466/*-------------------------------------------------------------------------*/
1467
1468static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1469{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001470 struct usb_composite_dev *cdev;
1471
David Brownell40982be2008-06-19 17:52:58 -07001472 if (req->status || req->actual != req->length)
1473 DBG((struct usb_composite_dev *) ep->driver_data,
1474 "setup complete --> %d, %d/%d\n",
1475 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001476
1477 /*
1478 * REVIST The same ep0 requests are shared with function drivers
1479 * so they don't have to maintain the same ->complete() stubs.
1480 *
1481 * Because of that, we need to check for the validity of ->context
1482 * here, even though we know we've set it to something useful.
1483 */
1484 if (!req->context)
1485 return;
1486
1487 cdev = req->context;
1488
1489 if (cdev->req == req)
1490 cdev->setup_pending = false;
1491 else if (cdev->os_desc_req == req)
1492 cdev->os_desc_pending = false;
1493 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301494 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001495}
1496
1497static int composite_ep0_queue(struct usb_composite_dev *cdev,
1498 struct usb_request *req, gfp_t gfp_flags)
1499{
1500 int ret;
1501
1502 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1503 if (ret == 0) {
1504 if (cdev->req == req)
1505 cdev->setup_pending = true;
1506 else if (cdev->os_desc_req == req)
1507 cdev->os_desc_pending = true;
1508 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301509 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001510 }
1511
1512 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001513}
1514
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001515static int count_ext_compat(struct usb_configuration *c)
1516{
1517 int i, res;
1518
1519 res = 0;
1520 for (i = 0; i < c->next_interface_id; ++i) {
1521 struct usb_function *f;
1522 int j;
1523
1524 f = c->interface[i];
1525 for (j = 0; j < f->os_desc_n; ++j) {
1526 struct usb_os_desc *d;
1527
1528 if (i != f->os_desc_table[j].if_id)
1529 continue;
1530 d = f->os_desc_table[j].os_desc;
1531 if (d && d->ext_compat_id)
1532 ++res;
1533 }
1534 }
1535 BUG_ON(res > 255);
1536 return res;
1537}
1538
1539static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1540{
1541 int i, count;
1542
1543 count = 16;
1544 for (i = 0; i < c->next_interface_id; ++i) {
1545 struct usb_function *f;
1546 int j;
1547
1548 f = c->interface[i];
1549 for (j = 0; j < f->os_desc_n; ++j) {
1550 struct usb_os_desc *d;
1551
1552 if (i != f->os_desc_table[j].if_id)
1553 continue;
1554 d = f->os_desc_table[j].os_desc;
1555 if (d && d->ext_compat_id) {
1556 *buf++ = i;
1557 *buf++ = 0x01;
1558 memcpy(buf, d->ext_compat_id, 16);
1559 buf += 22;
1560 } else {
1561 ++buf;
1562 *buf = 0x01;
1563 buf += 23;
1564 }
1565 count += 24;
1566 if (count >= 4096)
1567 return;
1568 }
1569 }
1570}
1571
1572static int count_ext_prop(struct usb_configuration *c, int interface)
1573{
1574 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001575 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001576
1577 f = c->interface[interface];
1578 for (j = 0; j < f->os_desc_n; ++j) {
1579 struct usb_os_desc *d;
1580
1581 if (interface != f->os_desc_table[j].if_id)
1582 continue;
1583 d = f->os_desc_table[j].os_desc;
1584 if (d && d->ext_compat_id)
1585 return d->ext_prop_count;
1586 }
Julia Lawall849b1332014-05-19 06:31:07 +02001587 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001588}
1589
1590static int len_ext_prop(struct usb_configuration *c, int interface)
1591{
1592 struct usb_function *f;
1593 struct usb_os_desc *d;
1594 int j, res;
1595
1596 res = 10; /* header length */
1597 f = c->interface[interface];
1598 for (j = 0; j < f->os_desc_n; ++j) {
1599 if (interface != f->os_desc_table[j].if_id)
1600 continue;
1601 d = f->os_desc_table[j].os_desc;
1602 if (d)
1603 return min(res + d->ext_prop_len, 4096);
1604 }
1605 return res;
1606}
1607
1608static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1609{
1610 struct usb_function *f;
1611 struct usb_os_desc *d;
1612 struct usb_os_desc_ext_prop *ext_prop;
1613 int j, count, n, ret;
1614 u8 *start = buf;
1615
1616 f = c->interface[interface];
1617 for (j = 0; j < f->os_desc_n; ++j) {
1618 if (interface != f->os_desc_table[j].if_id)
1619 continue;
1620 d = f->os_desc_table[j].os_desc;
1621 if (d)
1622 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1623 /* 4kB minus header length */
1624 n = buf - start;
1625 if (n >= 4086)
1626 return 0;
1627
1628 count = ext_prop->data_len +
1629 ext_prop->name_len + 14;
1630 if (count > 4086 - n)
1631 return -EINVAL;
1632 usb_ext_prop_put_size(buf, count);
1633 usb_ext_prop_put_type(buf, ext_prop->type);
1634 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1635 ext_prop->name_len);
1636 if (ret < 0)
1637 return ret;
1638 switch (ext_prop->type) {
1639 case USB_EXT_PROP_UNICODE:
1640 case USB_EXT_PROP_UNICODE_ENV:
1641 case USB_EXT_PROP_UNICODE_LINK:
1642 usb_ext_prop_put_unicode(buf, ret,
1643 ext_prop->data,
1644 ext_prop->data_len);
1645 break;
1646 case USB_EXT_PROP_BINARY:
1647 usb_ext_prop_put_binary(buf, ret,
1648 ext_prop->data,
1649 ext_prop->data_len);
1650 break;
1651 case USB_EXT_PROP_LE32:
1652 /* not implemented */
1653 case USB_EXT_PROP_BE32:
1654 /* not implemented */
1655 default:
1656 return -EINVAL;
1657 }
1658 buf += count;
1659 }
1660 }
1661
1662 return 0;
1663}
1664
David Brownell40982be2008-06-19 17:52:58 -07001665/*
1666 * The setup() callback implements all the ep0 functionality that's
1667 * not handled lower down, in hardware or the hardware driver(like
1668 * device and endpoint feature flags, and their status). It's all
1669 * housekeeping for the gadget function we're implementing. Most of
1670 * the work is in config and function specific setup.
1671 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001672int
David Brownell40982be2008-06-19 17:52:58 -07001673composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1674{
1675 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1676 struct usb_request *req = cdev->req;
1677 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001678 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001679 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001680 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001681 u16 w_value = le16_to_cpu(ctrl->wValue);
1682 u16 w_length = le16_to_cpu(ctrl->wLength);
1683 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001684 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001685
1686 /* partial re-init of the response message; the function or the
1687 * gadget might need to intercept e.g. a control-OUT completion
1688 * when we delegate to it.
1689 */
1690 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001691 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001692 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301693 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001694 gadget->ep0->driver_data = cdev;
1695
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001696 /*
1697 * Don't let non-standard requests match any of the cases below
1698 * by accident.
1699 */
1700 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1701 goto unknown;
1702
David Brownell40982be2008-06-19 17:52:58 -07001703 switch (ctrl->bRequest) {
1704
1705 /* we handle all standard USB descriptors */
1706 case USB_REQ_GET_DESCRIPTOR:
1707 if (ctrl->bRequestType != USB_DIR_IN)
1708 goto unknown;
1709 switch (w_value >> 8) {
1710
1711 case USB_DT_DEVICE:
1712 cdev->desc.bNumConfigurations =
1713 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001714 cdev->desc.bMaxPacketSize0 =
1715 cdev->gadget->ep0->maxpacket;
Mayank Rana72bf6d02016-12-19 14:48:38 -08001716 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001717 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001718 if (gadget->speed >= USB_SPEED_SUPER) {
John Youn1a853292016-02-05 17:05:40 -08001719 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001720 cdev->desc.bMaxPacketSize0 = 9;
1721 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001722 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001723 }
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001724 } else if (gadget->l1_supported) {
1725 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1726 DBG(cdev, "Config HS device with LPM(L1)\n");
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001727 }
1728
David Brownell40982be2008-06-19 17:52:58 -07001729 value = min(w_length, (u16) sizeof cdev->desc);
1730 memcpy(req->buf, &cdev->desc, value);
1731 break;
1732 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001733 if (!gadget_is_dualspeed(gadget) ||
1734 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001735 break;
1736 device_qual(cdev);
1737 value = min_t(int, w_length,
1738 sizeof(struct usb_qualifier_descriptor));
1739 break;
1740 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001741 if (!gadget_is_dualspeed(gadget) ||
1742 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001743 break;
1744 /* FALLTHROUGH */
1745 case USB_DT_CONFIG:
1746 value = config_desc(cdev, w_value);
1747 if (value >= 0)
1748 value = min(w_length, (u16) value);
1749 break;
1750 case USB_DT_STRING:
1751 value = get_string(cdev, req->buf,
1752 w_index, w_value & 0xff);
1753 if (value >= 0)
1754 value = min(w_length, (u16) value);
1755 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001756 case USB_DT_BOS:
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001757 if (gadget_is_superspeed(gadget) ||
1758 gadget->l1_supported) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001759 value = bos_desc(cdev);
1760 value = min(w_length, (u16) value);
1761 }
1762 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001763 case USB_DT_OTG:
1764 if (gadget_is_otg(gadget)) {
1765 struct usb_configuration *config;
1766 int otg_desc_len = 0;
1767
1768 if (cdev->config)
1769 config = cdev->config;
1770 else
1771 config = list_first_entry(
1772 &cdev->configs,
1773 struct usb_configuration, list);
1774 if (!config)
1775 goto done;
1776
1777 if (gadget->otg_caps &&
1778 (gadget->otg_caps->otg_rev >= 0x0200))
1779 otg_desc_len += sizeof(
1780 struct usb_otg20_descriptor);
1781 else
1782 otg_desc_len += sizeof(
1783 struct usb_otg_descriptor);
1784
1785 value = min_t(int, w_length, otg_desc_len);
1786 memcpy(req->buf, config->descriptors[0], value);
1787 }
1788 break;
David Brownell40982be2008-06-19 17:52:58 -07001789 }
1790 break;
1791
1792 /* any number of configs can work */
1793 case USB_REQ_SET_CONFIGURATION:
1794 if (ctrl->bRequestType != 0)
1795 goto unknown;
1796 if (gadget_is_otg(gadget)) {
1797 if (gadget->a_hnp_support)
1798 DBG(cdev, "HNP available\n");
1799 else if (gadget->a_alt_hnp_support)
1800 DBG(cdev, "HNP on another port\n");
1801 else
1802 VDBG(cdev, "HNP inactive\n");
1803 }
1804 spin_lock(&cdev->lock);
1805 value = set_config(cdev, ctrl, w_value);
1806 spin_unlock(&cdev->lock);
1807 break;
1808 case USB_REQ_GET_CONFIGURATION:
1809 if (ctrl->bRequestType != USB_DIR_IN)
1810 goto unknown;
1811 if (cdev->config)
1812 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1813 else
1814 *(u8 *)req->buf = 0;
1815 value = min(w_length, (u16) 1);
1816 break;
1817
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001818 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001819 case USB_REQ_SET_INTERFACE:
1820 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1821 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001822 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001823 break;
Bryan Wu08889512009-01-08 00:21:19 +08001824 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001825 if (!f)
1826 break;
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001827
1828 /*
1829 * If there's no get_alt() method, we know only altsetting zero
1830 * works. There is no need to check if set_alt() is not NULL
1831 * as we check this in usb_add_function().
1832 */
1833 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001834 break;
1835 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001836 if (value == USB_GADGET_DELAYED_STATUS) {
1837 DBG(cdev,
1838 "%s: interface %d (%s) requested delayed status\n",
1839 __func__, intf, f->name);
1840 cdev->delayed_status++;
1841 DBG(cdev, "delayed_status count %d\n",
1842 cdev->delayed_status);
1843 }
David Brownell40982be2008-06-19 17:52:58 -07001844 break;
1845 case USB_REQ_GET_INTERFACE:
1846 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1847 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001848 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001849 break;
Bryan Wu08889512009-01-08 00:21:19 +08001850 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001851 if (!f)
1852 break;
1853 /* lots of interfaces only need altsetting zero... */
1854 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1855 if (value < 0)
1856 break;
1857 *((u8 *)req->buf) = value;
1858 value = min(w_length, (u16) 1);
1859 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001860 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001861 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1862 (w_index == OTG_STS_SELECTOR)) {
1863 if (ctrl->bRequestType != (USB_DIR_IN |
1864 USB_RECIP_DEVICE))
1865 goto unknown;
1866 *((u8 *)req->buf) = gadget->host_request_flag;
1867 value = 1;
1868 break;
1869 }
1870
1871 /*
1872 * USB 3.0 additions:
1873 * Function driver should handle get_status request. If such cb
1874 * wasn't supplied we respond with default value = 0
1875 * Note: function driver should supply such cb only for the
1876 * first interface of the function
1877 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001878 if (!gadget_is_superspeed(gadget))
1879 goto unknown;
1880 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1881 goto unknown;
1882 value = 2; /* This is the length of the get_status reply */
1883 put_unaligned_le16(0, req->buf);
1884 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1885 break;
1886 f = cdev->config->interface[intf];
1887 if (!f)
1888 break;
1889 status = f->get_status ? f->get_status(f) : 0;
1890 if (status < 0)
1891 break;
1892 put_unaligned_le16(status & 0x0000ffff, req->buf);
1893 break;
1894 /*
1895 * Function drivers should handle SetFeature/ClearFeature
1896 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1897 * only for the first interface of the function
1898 */
1899 case USB_REQ_CLEAR_FEATURE:
1900 case USB_REQ_SET_FEATURE:
1901 if (!gadget_is_superspeed(gadget))
1902 goto unknown;
1903 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1904 goto unknown;
1905 switch (w_value) {
1906 case USB_INTRF_FUNC_SUSPEND:
1907 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1908 break;
1909 f = cdev->config->interface[intf];
1910 if (!f)
1911 break;
1912 value = 0;
Danny Segalf83e4512016-12-06 15:35:24 -08001913 if (f->func_suspend) {
1914 const u8 suspend_opt = w_index >> 8;
1915
1916 value = f->func_suspend(f, suspend_opt);
1917 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1918 f->name ? f->name : "", suspend_opt);
1919 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001920 if (value < 0) {
1921 ERROR(cdev,
1922 "func_suspend() returned error %d\n",
1923 value);
1924 value = 0;
1925 }
1926 break;
1927 }
1928 break;
David Brownell40982be2008-06-19 17:52:58 -07001929 default:
1930unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001931 /*
1932 * OS descriptors handling
1933 */
1934 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001935 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001936 ctrl->bRequest == cdev->b_vendor_code) {
1937 struct usb_request *req;
1938 struct usb_configuration *os_desc_cfg;
1939 u8 *buf;
1940 int interface;
1941 int count = 0;
1942
1943 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001944 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001945 req->complete = composite_setup_complete;
1946 buf = req->buf;
1947 os_desc_cfg = cdev->os_desc_config;
1948 memset(buf, 0, w_length);
1949 buf[5] = 0x01;
1950 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1951 case USB_RECIP_DEVICE:
1952 if (w_index != 0x4 || (w_value >> 8))
1953 break;
1954 buf[6] = w_index;
1955 if (w_length == 0x10) {
1956 /* Number of ext compat interfaces */
1957 count = count_ext_compat(os_desc_cfg);
1958 buf[8] = count;
1959 count *= 24; /* 24 B/ext compat desc */
1960 count += 16; /* header */
1961 put_unaligned_le32(count, buf);
1962 value = w_length;
1963 } else {
1964 /* "extended compatibility ID"s */
1965 count = count_ext_compat(os_desc_cfg);
1966 buf[8] = count;
1967 count *= 24; /* 24 B/ext compat desc */
1968 count += 16; /* header */
1969 put_unaligned_le32(count, buf);
1970 buf += 16;
1971 fill_ext_compat(os_desc_cfg, buf);
1972 value = w_length;
1973 }
1974 break;
1975 case USB_RECIP_INTERFACE:
1976 if (w_index != 0x5 || (w_value >> 8))
1977 break;
1978 interface = w_value & 0xFF;
1979 buf[6] = w_index;
1980 if (w_length == 0x0A) {
1981 count = count_ext_prop(os_desc_cfg,
1982 interface);
1983 put_unaligned_le16(count, buf + 8);
1984 count = len_ext_prop(os_desc_cfg,
1985 interface);
1986 put_unaligned_le32(count, buf);
1987
1988 value = w_length;
1989 } else {
1990 count = count_ext_prop(os_desc_cfg,
1991 interface);
1992 put_unaligned_le16(count, buf + 8);
1993 count = len_ext_prop(os_desc_cfg,
1994 interface);
1995 put_unaligned_le32(count, buf);
1996 buf += 10;
1997 value = fill_ext_prop(os_desc_cfg,
1998 interface, buf);
1999 if (value < 0)
2000 return value;
2001
2002 value = w_length;
2003 }
2004 break;
2005 }
William Wu7e14f47a2016-05-13 18:30:42 +08002006
2007 if (value >= 0) {
2008 req->length = value;
2009 req->context = cdev;
2010 req->zero = value < w_length;
2011 value = composite_ep0_queue(cdev, req,
2012 GFP_ATOMIC);
2013 if (value < 0) {
2014 DBG(cdev, "ep_queue --> %d\n", value);
2015 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302016 if (value != -ESHUTDOWN)
2017 composite_setup_complete(
2018 gadget->ep0, req);
William Wu7e14f47a2016-05-13 18:30:42 +08002019 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002020 }
2021 return value;
2022 }
2023
David Brownell40982be2008-06-19 17:52:58 -07002024 VDBG(cdev,
2025 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2026 ctrl->bRequestType, ctrl->bRequest,
2027 w_value, w_index, w_length);
2028
Laurent Pinchart52426582009-10-21 00:03:38 +02002029 /* functions always handle their interfaces and endpoints...
2030 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002031 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002032 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302033 if (cdev->config) {
2034 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b452016-06-22 01:12:08 +02002035 if (f->req_match &&
2036 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302037 goto try_fun_setup;
Felix Hädicke1a00b452016-06-22 01:12:08 +02002038 } else {
2039 struct usb_configuration *c;
2040 list_for_each_entry(c, &cdev->configs, list)
2041 list_for_each_entry(f, &c->functions, list)
2042 if (f->req_match &&
2043 f->req_match(f, ctrl, true))
2044 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302045 }
Felix Hädicke1a00b452016-06-22 01:12:08 +02002046 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302047
Laurent Pinchart52426582009-10-21 00:03:38 +02002048 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2049 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002050 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302051 break;
2052 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002053 break;
2054
2055 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002056 if (!cdev->config)
2057 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002058 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2059 list_for_each_entry(f, &cdev->config->functions, list) {
2060 if (test_bit(endp, f->endpoints))
2061 break;
2062 }
2063 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002064 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002065 break;
David Brownell40982be2008-06-19 17:52:58 -07002066 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002067try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002068 if (f && f->setup)
2069 value = f->setup(f, ctrl);
2070 else {
David Brownell40982be2008-06-19 17:52:58 -07002071 struct usb_configuration *c;
2072
2073 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002074 if (!c)
2075 goto done;
2076
2077 /* try current config's setup */
2078 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002079 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002080 goto done;
2081 }
2082
2083 /* try the only function in the current config */
2084 if (!list_is_singular(&c->functions))
2085 goto done;
2086 f = list_first_entry(&c->functions, struct usb_function,
2087 list);
2088 if (f->setup)
2089 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002090 }
Vijayavardhan Vennapusa8edd67762013-10-17 15:00:02 +05302091 if (value == USB_GADGET_DELAYED_STATUS) {
2092 DBG(cdev,
2093 "%s: interface %d (%s) requested delayed status\n",
2094 __func__, intf, f->name);
2095 cdev->delayed_status++;
2096 DBG(cdev, "delayed_status count %d\n",
2097 cdev->delayed_status);
2098 }
David Brownell40982be2008-06-19 17:52:58 -07002099
2100 goto done;
2101 }
2102
2103 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002104 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002105 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002106 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002107 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002108 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002109 if (value < 0) {
2110 DBG(cdev, "ep_queue --> %d\n", value);
2111 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302112 if (value != -ESHUTDOWN)
2113 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002114 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002115 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2116 WARN(cdev,
2117 "%s: Delayed status not supported for w_length != 0",
2118 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002119 }
2120
2121done:
2122 /* device either stalls (value < 0) or reports success */
2123 return value;
2124}
2125
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002126void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002127{
2128 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2129 unsigned long flags;
2130
Badhri Jagan Sridharan9e954092015-05-06 13:40:15 -07002131 if (cdev == NULL) {
2132 WARN(1, "%s: Calling disconnect on a Gadget that is \
2133 not connected\n", __func__);
2134 return;
2135 }
2136
David Brownell40982be2008-06-19 17:52:58 -07002137 /* REVISIT: should we have config and device level
2138 * disconnect callbacks?
2139 */
2140 spin_lock_irqsave(&cdev->lock, flags);
2141 if (cdev->config)
2142 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002143 if (cdev->driver->disconnect)
2144 cdev->driver->disconnect(cdev);
Pavankumar Kondeti47870672013-06-19 09:51:58 +05302145 if (cdev->delayed_status != 0) {
2146 INFO(cdev, "delayed status mismatch..resetting\n");
2147 cdev->delayed_status = 0;
2148 }
David Brownell40982be2008-06-19 17:52:58 -07002149 spin_unlock_irqrestore(&cdev->lock, flags);
2150}
2151
2152/*-------------------------------------------------------------------------*/
2153
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002154static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2155 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002156{
2157 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2158 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2159
Vijayavardhan Vennapusa5d2ac1a2017-09-13 15:41:20 +05302160 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002161}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002162static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002163
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002164static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002165{
2166 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002167 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2168 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002169
2170 /* composite_disconnect() must already have been called
2171 * by the underlying peripheral controller driver!
2172 * so there's no i/o concurrency that could affect the
2173 * state protected by cdev->lock.
2174 */
2175 WARN_ON(cdev->config);
2176
2177 while (!list_empty(&cdev->configs)) {
2178 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002179 c = list_first_entry(&cdev->configs,
2180 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002181 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002182 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002183 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002184 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002185
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002186 composite_dev_cleanup(cdev);
2187
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002188 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2189 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2190
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002191 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002192 kfree(cdev);
2193 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002194}
2195
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002196static void composite_unbind(struct usb_gadget *gadget)
2197{
2198 __composite_unbind(gadget, true);
2199}
2200
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002201static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2202 const struct usb_device_descriptor *old)
2203{
2204 __le16 idVendor;
2205 __le16 idProduct;
2206 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002207 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002208 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002209 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002210
2211 /*
2212 * these variables may have been set in
2213 * usb_composite_overwrite_options()
2214 */
2215 idVendor = new->idVendor;
2216 idProduct = new->idProduct;
2217 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002218 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002219 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002220 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002221
2222 *new = *old;
2223 if (idVendor)
2224 new->idVendor = idVendor;
2225 if (idProduct)
2226 new->idProduct = idProduct;
2227 if (bcdDevice)
2228 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002229 else
2230 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002231 if (iSerialNumber)
2232 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002233 if (iManufacturer)
2234 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002235 if (iProduct)
2236 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002237}
2238
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002239int composite_dev_prepare(struct usb_composite_driver *composite,
2240 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002241{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002242 struct usb_gadget *gadget = cdev->gadget;
2243 int ret = -ENOMEM;
2244
2245 /* preallocate control response and buffer */
2246 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2247 if (!cdev->req)
2248 return -ENOMEM;
2249
2250 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2251 if (!cdev->req->buf)
2252 goto fail;
2253
2254 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2255 if (ret)
2256 goto fail_dev;
2257
2258 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002259 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002260 gadget->ep0->driver_data = cdev;
2261
2262 cdev->driver = composite;
2263
2264 /*
2265 * As per USB compliance update, a device that is actively drawing
2266 * more than 100mA from USB must report itself as bus-powered in
2267 * the GetStatus(DEVICE) call.
2268 */
2269 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2270 usb_gadget_set_selfpowered(gadget);
2271
2272 /* interface and string IDs start at zero via kzalloc.
2273 * we force endpoints to start unassigned; few controller
2274 * drivers will zero ep->driver_data.
2275 */
2276 usb_ep_autoconfig_reset(gadget);
2277 return 0;
2278fail_dev:
2279 kfree(cdev->req->buf);
2280fail:
2281 usb_ep_free_request(gadget->ep0, cdev->req);
2282 cdev->req = NULL;
2283 return ret;
2284}
2285
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002286int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2287 struct usb_ep *ep0)
2288{
2289 int ret = 0;
2290
2291 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2292 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002293 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002294 goto end;
2295 }
2296
2297 /* OS feature descriptor length <= 4kB */
2298 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2299 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002300 ret = -ENOMEM;
Christophe JAILLETf0ee2032017-01-04 06:30:16 +01002301 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002302 goto end;
2303 }
Felipe Balbi57943712014-09-18 09:54:54 -05002304 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002305 cdev->os_desc_req->complete = composite_setup_complete;
2306end:
2307 return ret;
2308}
2309
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002310void composite_dev_cleanup(struct usb_composite_dev *cdev)
2311{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002312 struct usb_gadget_string_container *uc, *tmp;
2313
2314 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2315 list_del(&uc->list);
2316 kfree(uc);
2317 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002318 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002319 if (cdev->os_desc_pending)
2320 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2321
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002322 kfree(cdev->os_desc_req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002323 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002324 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002325 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002326 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002327 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002328 if (cdev->setup_pending)
2329 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2330
Li Junbe0a8882014-08-28 21:44:11 +08002331 kfree(cdev->req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002332 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002333 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002334 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002335 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002336 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002337 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002338}
2339
2340static int composite_bind(struct usb_gadget *gadget,
2341 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002342{
2343 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002344 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002345 int status = -ENOMEM;
2346
2347 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2348 if (!cdev)
2349 return status;
2350
2351 spin_lock_init(&cdev->lock);
2352 cdev->gadget = gadget;
2353 set_gadget_data(gadget, cdev);
2354 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002355 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002356
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002357 status = composite_dev_prepare(composite, cdev);
2358 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002359 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002360
2361 /* composite gadget needs to assign strings for whole device (like
2362 * serial number), register function drivers, potentially update
2363 * power state and consumption, etc
2364 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002365 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002366 if (status < 0)
2367 goto fail;
2368
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002369 if (cdev->use_os_string) {
2370 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2371 if (status)
2372 goto fail;
2373 }
2374
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002375 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002376
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002377 /* has userspace failed to provide a serial number? */
2378 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2379 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2380
David Brownell40982be2008-06-19 17:52:58 -07002381 INFO(cdev, "%s ready\n", composite->name);
2382 return 0;
2383
2384fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002385 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002386 return status;
2387}
2388
2389/*-------------------------------------------------------------------------*/
2390
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002391void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002392{
2393 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2394 struct usb_function *f;
Mayank Rana13e5c152015-10-05 19:08:47 -07002395 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002396
David Brownell89429392009-03-19 14:14:17 -07002397 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002398 * suspend/resume callbacks?
2399 */
2400 DBG(cdev, "suspend\n");
Mayank Rana13e5c152015-10-05 19:08:47 -07002401 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002402 if (cdev->config) {
2403 list_for_each_entry(f, &cdev->config->functions, list) {
2404 if (f->suspend)
2405 f->suspend(f);
2406 }
2407 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002408 if (cdev->driver->suspend)
2409 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002410
2411 cdev->suspended = 1;
Mayank Rana13e5c152015-10-05 19:08:47 -07002412 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002413
2414 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002415}
2416
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002417void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002418{
2419 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2420 struct usb_function *f;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302421 int ret;
2422 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002423
David Brownell89429392009-03-19 14:14:17 -07002424 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002425 * suspend/resume callbacks?
2426 */
2427 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002428 if (cdev->driver->resume)
2429 cdev->driver->resume(cdev);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002430
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302431 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002432 if (cdev->config) {
2433 list_for_each_entry(f, &cdev->config->functions, list) {
Mayank Ranab92dfd02014-11-25 15:29:58 -08002434 ret = usb_func_wakeup_int(f);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002435 if (ret) {
2436 if (ret == -EAGAIN) {
2437 ERROR(f->config->cdev,
2438 "Function wakeup for %s could not complete due to suspend state.\n",
2439 f->name ? f->name : "");
2440 break;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +05302441 } else if (ret != -ENOTSUPP) {
2442 ERROR(f->config->cdev,
2443 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2444 f->name ? f->name : "",
2445 ret);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002446 }
Danny Segal86cb50e2014-07-09 15:14:49 +03002447 }
2448
David Brownell40982be2008-06-19 17:52:58 -07002449 if (f->resume)
2450 f->resume(f);
2451 }
Hao Wub23f2f92010-11-29 15:17:03 +08002452
Mayank Rana4be36022016-12-19 15:03:00 -08002453 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
David Brownell40982be2008-06-19 17:52:58 -07002454 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002455
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302456 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002457 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002458}
2459
2460/*-------------------------------------------------------------------------*/
2461
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002462static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002463 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002464 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002465
2466 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002467 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002468 .disconnect = composite_disconnect,
2469
2470 .suspend = composite_suspend,
2471 .resume = composite_resume,
2472
2473 .driver = {
2474 .owner = THIS_MODULE,
2475 },
2476};
2477
2478/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002479 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002480 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002481 *
David Brownell40982be2008-06-19 17:52:58 -07002482 * Context: single threaded during gadget setup
2483 *
2484 * This function is used to register drivers using the composite driver
2485 * framework. The return value is zero, or a negative errno value.
2486 * Those values normally come from the driver's @bind method, which does
2487 * all the work of setting up the driver to match the hardware.
2488 *
2489 * On successful return, the gadget is ready to respond to requests from
2490 * the host, unless one of its components invokes usb_gadget_disconnect()
2491 * while it was binding. That would usually be done in order to wait for
2492 * some userspace participation.
2493 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002494int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002495{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002496 struct usb_gadget_driver *gadget_driver;
2497
2498 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002499 return -EINVAL;
2500
2501 if (!driver->name)
2502 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002503
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002504 driver->gadget_driver = composite_driver_template;
2505 gadget_driver = &driver->gadget_driver;
2506
2507 gadget_driver->function = (char *) driver->name;
2508 gadget_driver->driver.name = driver->name;
2509 gadget_driver->max_speed = driver->max_speed;
2510
2511 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002512}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002513EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002514
2515/**
2516 * usb_composite_unregister() - unregister a composite driver
2517 * @driver: the driver to unregister
2518 *
2519 * This function is used to unregister drivers using the composite
2520 * driver framework.
2521 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002522void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002523{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002524 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002525}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002526EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002527
2528/**
2529 * usb_composite_setup_continue() - Continue with the control transfer
2530 * @cdev: the composite device who's control transfer was kept waiting
2531 *
2532 * This function must be called by the USB function driver to continue
2533 * with the control transfer's data/status stage in case it had requested to
2534 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2535 * can request the composite framework to delay the setup request's data/status
2536 * stages by returning USB_GADGET_DELAYED_STATUS.
2537 */
2538void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2539{
2540 int value;
2541 struct usb_request *req = cdev->req;
2542 unsigned long flags;
2543
2544 DBG(cdev, "%s\n", __func__);
2545 spin_lock_irqsave(&cdev->lock, flags);
2546
2547 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302548 if (!cdev->config) {
2549 spin_unlock_irqrestore(&cdev->lock, flags);
2550 return;
2551 }
2552 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002553 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302554 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002555
2556 } else if (--cdev->delayed_status == 0) {
2557 DBG(cdev, "%s: Completing delayed status\n", __func__);
2558 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002559 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002560 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002561 if (value < 0) {
2562 DBG(cdev, "ep_queue --> %d\n", value);
2563 req->status = 0;
2564 composite_setup_complete(cdev->gadget->ep0, req);
2565 }
2566 }
2567
2568 spin_unlock_irqrestore(&cdev->lock, flags);
2569}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002570EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002571
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002572static char *composite_default_mfr(struct usb_gadget *gadget)
2573{
2574 char *mfr;
2575 int len;
2576
2577 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2578 init_utsname()->release, gadget->name);
2579 len++;
2580 mfr = kmalloc(len, GFP_KERNEL);
2581 if (!mfr)
2582 return NULL;
2583 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2584 init_utsname()->release, gadget->name);
2585 return mfr;
2586}
2587
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002588void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2589 struct usb_composite_overwrite *covr)
2590{
2591 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002592 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2593 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002594
2595 if (covr->idVendor)
2596 desc->idVendor = cpu_to_le16(covr->idVendor);
2597
2598 if (covr->idProduct)
2599 desc->idProduct = cpu_to_le16(covr->idProduct);
2600
2601 if (covr->bcdDevice)
2602 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002603
2604 if (covr->serial_number) {
2605 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2606 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2607 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002608 if (covr->manufacturer) {
2609 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2610 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002611
2612 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2613 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2614 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2615 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002616 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002617
2618 if (covr->product) {
2619 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2620 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2621 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002622}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002623EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002624
2625MODULE_LICENSE("GPL");
2626MODULE_AUTHOR("David Brownell");