blob: 12331e5b13891fc5da01d12854c5ae469baa2af6 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
Aniket Randive0a78a292020-03-18 11:22:09 +053020#include <soc/qcom/boot_stats.h>
David Brownell40982be2008-06-19 17:52:58 -070021
22#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080023#include <linux/usb/otg.h>
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +053024#include <linux/usb/msm_hsusb.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030025#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070026
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020027#include "u_os_desc.h"
Mayank Ranab6cadaa2015-10-15 17:51:32 -070028#define SSUSB_GADGET_VBUS_DRAW 900 /* in mA */
29#define SSUSB_GADGET_VBUS_DRAW_UNITS 8
30#define HSUSB_GADGET_VBUS_DRAW_UNITS 2
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020031
Mayank Rana4be36022016-12-19 15:03:00 -080032/*
33 * Based on enumerated USB speed, draw power with set_config and resume
34 * HSUSB: 500mA, SSUSB: 900mA
35 */
36#define USB_VBUS_DRAW(speed)\
37 (speed == USB_SPEED_SUPER ?\
38 SSUSB_GADGET_VBUS_DRAW : CONFIG_USB_GADGET_VBUS_DRAW)
39
Hemant Kumar7e115fd2017-07-19 12:08:16 -070040/* disable LPM by default */
Hemant Kumarc41d680a2018-02-14 16:37:37 -080041static bool disable_l1_for_hs;
Hemant Kumar7e115fd2017-07-19 12:08:16 -070042module_param(disable_l1_for_hs, bool, 0644);
43MODULE_PARM_DESC(disable_l1_for_hs,
44 "Disable support for L1 LPM for HS devices");
45
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020046/**
47 * struct usb_os_string - represents OS String to be reported by a gadget
48 * @bLength: total length of the entire descritor, always 0x12
49 * @bDescriptorType: USB_DT_STRING
50 * @qwSignature: the OS String proper
51 * @bMS_VendorCode: code used by the host for subsequent requests
52 * @bPad: not used, must be zero
53 */
54struct usb_os_string {
55 __u8 bLength;
56 __u8 bDescriptorType;
57 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
58 __u8 bMS_VendorCode;
59 __u8 bPad;
60} __packed;
61
David Brownell40982be2008-06-19 17:52:58 -070062/*
63 * The code in this file is utility code, used to build a gadget driver
64 * from one or more "function" drivers, one or more "configuration"
65 * objects, and a "usb_composite_driver" by gluing them together along
66 * with the relevant device-wide data.
67 */
68
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010069static struct usb_gadget_strings **get_containers_gs(
70 struct usb_gadget_string_container *uc)
71{
72 return (struct usb_gadget_strings **)uc->stash;
73}
74
Tatyana Brokhman48767a42011-06-28 16:33:49 +030075/**
John Younf3bdbe32016-02-05 17:07:03 -080076 * function_descriptors() - get function descriptors for speed
77 * @f: the function
78 * @speed: the speed
79 *
80 * Returns the descriptors or NULL if not set.
81 */
82static struct usb_descriptor_header **
83function_descriptors(struct usb_function *f,
84 enum usb_device_speed speed)
85{
86 struct usb_descriptor_header **descriptors;
87
Felipe Balbi08782632016-04-22 14:53:47 +030088 /*
89 * NOTE: we try to help gadget drivers which might not be setting
90 * max_speed appropriately.
91 */
92
John Younf3bdbe32016-02-05 17:07:03 -080093 switch (speed) {
94 case USB_SPEED_SUPER_PLUS:
95 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030096 if (descriptors)
97 break;
98 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080099 case USB_SPEED_SUPER:
100 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +0300101 if (descriptors)
102 break;
103 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800104 case USB_SPEED_HIGH:
105 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +0300106 if (descriptors)
107 break;
108 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -0800109 default:
110 descriptors = f->fs_descriptors;
111 }
112
Felipe Balbi08782632016-04-22 14:53:47 +0300113 /*
114 * if we can't find any descriptors at all, then this gadget deserves to
115 * Oops with a NULL pointer dereference
116 */
117
John Younf3bdbe32016-02-05 17:07:03 -0800118 return descriptors;
119}
120
121/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300122 * next_ep_desc() - advance to the next EP descriptor
123 * @t: currect pointer within descriptor array
124 *
125 * Return: next EP descriptor or NULL
126 *
127 * Iterate over @t until either EP descriptor found or
128 * NULL (that indicates end of list) encountered
129 */
130static struct usb_descriptor_header**
131next_ep_desc(struct usb_descriptor_header **t)
132{
133 for (; *t; t++) {
134 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
135 return t;
136 }
137 return NULL;
138}
139
140/*
141 * for_each_ep_desc()- iterate over endpoint descriptors in the
142 * descriptors list
143 * @start: pointer within descriptor array.
144 * @ep_desc: endpoint descriptor to use as the loop cursor
145 */
146#define for_each_ep_desc(start, ep_desc) \
147 for (ep_desc = next_ep_desc(start); \
148 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
149
150/**
151 * config_ep_by_speed() - configures the given endpoint
152 * according to gadget speed.
153 * @g: pointer to the gadget
154 * @f: usb function
155 * @_ep: the endpoint to configure
156 *
157 * Return: error code, 0 on success
158 *
159 * This function chooses the right descriptors for a given
160 * endpoint according to gadget speed and saves it in the
161 * endpoint desc field. If the endpoint already has a descriptor
162 * assigned to it - overwrites it with currently corresponding
163 * descriptor. The endpoint maxpacket field is updated according
164 * to the chosen descriptor.
165 * Note: the supplied function should hold all the descriptors
166 * for supported speeds
167 */
168int config_ep_by_speed(struct usb_gadget *g,
169 struct usb_function *f,
170 struct usb_ep *_ep)
171{
172 struct usb_endpoint_descriptor *chosen_desc = NULL;
173 struct usb_descriptor_header **speed_desc = NULL;
174
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300175 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
176 int want_comp_desc = 0;
177
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300178 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
179
180 if (!g || !f || !_ep)
181 return -EIO;
182
183 /* select desired speed */
184 switch (g->speed) {
John Youn4eb8e322016-02-05 17:07:30 -0800185 case USB_SPEED_SUPER_PLUS:
186 if (gadget_is_superspeed_plus(g)) {
187 speed_desc = f->ssp_descriptors;
188 want_comp_desc = 1;
189 break;
190 }
191 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300192 case USB_SPEED_SUPER:
193 if (gadget_is_superspeed(g)) {
194 speed_desc = f->ss_descriptors;
195 want_comp_desc = 1;
196 break;
197 }
198 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300199 case USB_SPEED_HIGH:
200 if (gadget_is_dualspeed(g)) {
201 speed_desc = f->hs_descriptors;
202 break;
203 }
204 /* else: fall through */
205 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200206 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300207 }
208 /* find descriptors */
209 for_each_ep_desc(speed_desc, d_spd) {
210 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
211 if (chosen_desc->bEndpointAddress == _ep->address)
212 goto ep_found;
213 }
214 return -EIO;
215
216ep_found:
217 /* commit results */
Felipe Balbie0aa5ec2016-09-28 10:38:11 +0300218 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300219 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300220 _ep->comp_desc = NULL;
221 _ep->maxburst = 0;
Felipe Balbi3999c532016-09-28 12:33:31 +0300222 _ep->mult = 1;
223
224 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
225 usb_endpoint_xfer_int(_ep->desc)))
226 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
227
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300228 if (!want_comp_desc)
229 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300230
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300231 /*
232 * Companion descriptor should follow EP descriptor
233 * USB 3.0 spec, #9.6.7
234 */
235 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
236 if (!comp_desc ||
237 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
238 return -EIO;
239 _ep->comp_desc = comp_desc;
John Youn4eb8e322016-02-05 17:07:30 -0800240 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300241 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300242 case USB_ENDPOINT_XFER_ISOC:
243 /* mult: bits 1:0 of bmAttributes */
Felipe Balbi3999c532016-09-28 12:33:31 +0300244 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800245 case USB_ENDPOINT_XFER_BULK:
246 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300247 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300248 break;
249 default:
Colin Ian King0e216b02017-11-14 16:18:28 +0000250 if (comp_desc->bMaxBurst != 0) {
251 struct usb_composite_dev *cdev;
252
253 cdev = get_gadget_data(g);
Felipe Balbib785ea72012-06-06 10:20:23 +0300254 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
Colin Ian King0e216b02017-11-14 16:18:28 +0000255 }
Felipe Balbib785ea72012-06-06 10:20:23 +0300256 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300257 break;
258 }
259 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300260 return 0;
261}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200262EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700263
264/**
265 * usb_add_function() - add a function to a configuration
266 * @config: the configuration
267 * @function: the function being added
268 * Context: single threaded during gadget setup
269 *
270 * After initialization, each configuration must have one or more
271 * functions added to it. Adding a function involves calling its @bind()
272 * method to allocate resources such as interface and string identifiers
273 * and endpoints.
274 *
275 * This function returns the value of the function's bind(), which is
276 * zero for success else a negative errno value.
277 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200278int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700279 struct usb_function *function)
280{
281 int value = -EINVAL;
282
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530283 DBG(config->cdev, "adding '%s'/%pK to config '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -0700284 function->name, function,
285 config->label, config);
286
287 if (!function->set_alt || !function->disable)
288 goto done;
289
290 function->config = config;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700291 function->intf_id = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700292 list_add_tail(&function->list, &config->functions);
293
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200294 if (function->bind_deactivated) {
295 value = usb_function_deactivate(function);
296 if (value)
297 goto done;
298 }
299
David Brownell40982be2008-06-19 17:52:58 -0700300 /* REVISIT *require* function->bind? */
301 if (function->bind) {
302 value = function->bind(config, function);
303 if (value < 0) {
304 list_del(&function->list);
305 function->config = NULL;
306 }
307 } else
308 value = 0;
309
310 /* We allow configurations that don't work at both speeds.
311 * If we run into a lowspeed Linux system, treat it the same
312 * as full speed ... it's the function drivers that will need
313 * to avoid bulk and ISO transfers.
314 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200315 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700316 config->fullspeed = true;
317 if (!config->highspeed && function->hs_descriptors)
318 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300319 if (!config->superspeed && function->ss_descriptors)
320 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800321 if (!config->superspeed_plus && function->ssp_descriptors)
322 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700323
324done:
325 if (value)
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530326 DBG(config->cdev, "adding '%s'/%pK --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700327 function->name, function, value);
328 return value;
329}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200330EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700331
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100332void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
333{
334 if (f->disable)
335 f->disable(f);
336
337 bitmap_zero(f->endpoints, 32);
338 list_del(&f->list);
339 if (f->unbind)
340 f->unbind(c, f);
341}
342EXPORT_SYMBOL_GPL(usb_remove_function);
343
David Brownell40982be2008-06-19 17:52:58 -0700344/**
David Brownell60beed92008-08-18 17:38:22 -0700345 * usb_function_deactivate - prevent function and gadget enumeration
346 * @function: the function that isn't yet ready to respond
347 *
348 * Blocks response of the gadget driver to host enumeration by
349 * preventing the data line pullup from being activated. This is
350 * normally called during @bind() processing to change from the
351 * initial "ready to respond" state, or when a required resource
352 * becomes available.
353 *
354 * For example, drivers that serve as a passthrough to a userspace
355 * daemon can block enumeration unless that daemon (such as an OBEX,
356 * MTP, or print server) is ready to handle host requests.
357 *
358 * Not all systems support software control of their USB peripheral
359 * data pullups.
360 *
361 * Returns zero on success, else negative errno.
362 */
363int usb_function_deactivate(struct usb_function *function)
364{
365 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200366 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700367 int status = 0;
368
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200369 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700370
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530371 if (cdev->deactivations == 0) {
372 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200373 status = usb_gadget_deactivate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530374 spin_lock_irqsave(&cdev->lock, flags);
375 }
David Brownell60beed92008-08-18 17:38:22 -0700376 if (status == 0)
377 cdev->deactivations++;
378
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200379 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700380 return status;
381}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200382EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700383
384/**
385 * usb_function_activate - allow function and gadget enumeration
386 * @function: function on which usb_function_activate() was called
387 *
388 * Reverses effect of usb_function_deactivate(). If no more functions
389 * are delaying their activation, the gadget driver will respond to
390 * host enumeration procedures.
391 *
392 * Returns zero on success, else negative errno.
393 */
394int usb_function_activate(struct usb_function *function)
395{
396 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200397 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700398 int status = 0;
399
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200400 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700401
402 if (WARN_ON(cdev->deactivations == 0))
403 status = -EINVAL;
404 else {
405 cdev->deactivations--;
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530406 if (cdev->deactivations == 0) {
407 spin_unlock_irqrestore(&cdev->lock, flags);
Robert Baldyga56012502015-05-04 14:55:12 +0200408 status = usb_gadget_activate(cdev->gadget);
Sriharsha Allenkie341cbe2017-11-22 17:12:11 +0530409 spin_lock_irqsave(&cdev->lock, flags);
410 }
David Brownell60beed92008-08-18 17:38:22 -0700411 }
412
Michael Grzeschik4fefe9f2012-07-19 00:20:11 +0200413 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700414 return status;
415}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200416EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700417
418/**
David Brownell40982be2008-06-19 17:52:58 -0700419 * usb_interface_id() - allocate an unused interface ID
420 * @config: configuration associated with the interface
421 * @function: function handling the interface
422 * Context: single threaded during gadget setup
423 *
424 * usb_interface_id() is called from usb_function.bind() callbacks to
425 * allocate new interface IDs. The function driver will then store that
426 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300427 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700428 * particularly changing its altsetting via set_alt(). There may
429 * also be class-specific or vendor-specific requests to handle.
430 *
431 * All interface identifier should be allocated using this routine, to
432 * ensure that for example different functions don't wrongly assign
433 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300434 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700435 * one configuration (or more than once in a given configuration) need
436 * multiple versions of the relevant descriptors.
437 *
438 * Returns the interface ID which was allocated; or -ENODEV if no
439 * more interface IDs can be allocated.
440 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200441int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700442 struct usb_function *function)
443{
444 unsigned id = config->next_interface_id;
445
446 if (id < MAX_CONFIG_INTERFACES) {
447 config->interface[id] = function;
Hemant Kumar7a58c762015-03-20 21:17:28 -0700448 if (function->intf_id < 0)
449 function->intf_id = id;
David Brownell40982be2008-06-19 17:52:58 -0700450 config->next_interface_id = id + 1;
451 return id;
452 }
453 return -ENODEV;
454}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200455EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700456
Mayank Ranab92dfd02014-11-25 15:29:58 -0800457static int usb_func_wakeup_int(struct usb_function *func)
Danny Segalf83e4512016-12-06 15:35:24 -0800458{
459 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800460 struct usb_gadget *gadget;
461
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800462 pr_debug("%s - %s function wakeup\n",
463 __func__, func->name ? func->name : "");
Danny Segalf83e4512016-12-06 15:35:24 -0800464
465 if (!func || !func->config || !func->config->cdev ||
466 !func->config->cdev->gadget)
467 return -EINVAL;
468
469 gadget = func->config->cdev->gadget;
470 if ((gadget->speed != USB_SPEED_SUPER) || !func->func_wakeup_allowed) {
471 DBG(func->config->cdev,
472 "Function Wakeup is not possible. speed=%u, func_wakeup_allowed=%u\n",
473 gadget->speed,
474 func->func_wakeup_allowed);
475
476 return -ENOTSUPP;
477 }
478
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800479 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300480
481 return ret;
482}
483
484int usb_func_wakeup(struct usb_function *func)
485{
486 int ret;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530487 unsigned long flags;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300488
489 pr_debug("%s function wakeup\n",
490 func->name ? func->name : "");
491
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530492 spin_lock_irqsave(&func->config->cdev->lock, flags);
Mayank Ranab92dfd02014-11-25 15:29:58 -0800493 ret = usb_func_wakeup_int(func);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530494 if (ret == -EACCES) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300495 DBG(func->config->cdev,
496 "Function wakeup for %s could not complete due to suspend state. Delayed until after bus resume.\n",
497 func->name ? func->name : "");
Danny Segalde7cd8d2014-07-28 18:08:33 +0300498 ret = 0;
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530499 func->func_wakeup_pending = 1;
500 } else if (ret == -EAGAIN) {
501 DBG(func->config->cdev,
502 "Function wakeup for %s sent.\n",
503 func->name ? func->name : "");
504 ret = 0;
Pavankumar Kondeti80c81b22014-11-10 16:29:38 +0530505 } else if (ret < 0 && ret != -ENOTSUPP) {
Danny Segalde7cd8d2014-07-28 18:08:33 +0300506 ERROR(func->config->cdev,
507 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
508 func->name ? func->name : "", ret);
Danny Segalf83e4512016-12-06 15:35:24 -0800509 }
510
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +0530511 spin_unlock_irqrestore(&func->config->cdev->lock, flags);
Danny Segalde7cd8d2014-07-28 18:08:33 +0300512 return ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800513}
514EXPORT_SYMBOL(usb_func_wakeup);
515
516int usb_func_ep_queue(struct usb_function *func, struct usb_ep *ep,
517 struct usb_request *req, gfp_t gfp_flags)
518{
wHemant Kumare9e13922016-12-19 14:41:28 -0800519 int ret;
Danny Segalf83e4512016-12-06 15:35:24 -0800520 struct usb_gadget *gadget;
521
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800522 if (!func || !func->config || !func->config->cdev ||
wHemant Kumare9e13922016-12-19 14:41:28 -0800523 !func->config->cdev->gadget || !ep || !req) {
524 ret = -EINVAL;
525 goto done;
526 }
Danny Segalf83e4512016-12-06 15:35:24 -0800527
528 pr_debug("Function %s queueing new data into ep %u\n",
529 func->name ? func->name : "", ep->address);
530
531 gadget = func->config->cdev->gadget;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800532 if (func->func_is_suspended && func->func_wakeup_allowed) {
533 ret = usb_gadget_func_wakeup(gadget, func->intf_id);
Sriharsha Allenkif857a142017-11-16 18:53:37 +0530534 if (ret == -EACCES) {
535 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
536 func->name ? func->name : "");
537 func->func_wakeup_pending = 1;
538 ret = -EAGAIN;
539 } else if (ret == -EAGAIN) {
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800540 pr_debug("bus suspended func wakeup for %s delayed until bus resume.\n",
541 func->name ? func->name : "");
542 } else if (ret < 0 && ret != -ENOTSUPP) {
543 pr_err("Failed to wake function %s from suspend state. ret=%d.\n",
544 func->name ? func->name : "", ret);
Chandana Kishori Chiluveru712cfc02018-07-27 17:25:09 +0530545 } else {
546 /*
547 * Return -EAGAIN to queue the request from
548 * function driver wakeup function.
549 */
550 ret = -EAGAIN;
551 goto done;
Danny Segalf83e4512016-12-06 15:35:24 -0800552 }
553 }
554
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800555 if (!func->func_is_suspended)
556 ret = 0;
Hemant Kumar2f2ed9a2016-12-19 14:08:50 -0800557
wHemant Kumare9e13922016-12-19 14:41:28 -0800558 if (func->func_is_suspended && !func->func_wakeup_allowed) {
559 ret = -ENOTSUPP;
560 goto done;
561 }
562
563 ret = usb_ep_queue(ep, req, gfp_flags);
564done:
Danny Segalf83e4512016-12-06 15:35:24 -0800565 return ret;
566}
567EXPORT_SYMBOL(usb_func_ep_queue);
568
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100569static u8 encode_bMaxPower(enum usb_device_speed speed,
570 struct usb_configuration *c)
571{
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700572 unsigned int val = CONFIG_USB_GADGET_VBUS_DRAW;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100573
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100574 switch (speed) {
575 case USB_SPEED_SUPER:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700576 /* with super-speed report 900mA */
577 val = SSUSB_GADGET_VBUS_DRAW;
578 return (u8)(val / SSUSB_GADGET_VBUS_DRAW_UNITS);
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100579 default:
Mayank Ranab6cadaa2015-10-15 17:51:32 -0700580 return DIV_ROUND_UP(val, HSUSB_GADGET_VBUS_DRAW_UNITS);
Joe Perches2b84f922013-10-08 16:01:37 -0700581 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100582}
583
David Brownell40982be2008-06-19 17:52:58 -0700584static int config_buf(struct usb_configuration *config,
585 enum usb_device_speed speed, void *buf, u8 type)
586{
587 struct usb_config_descriptor *c = buf;
588 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200589 int len;
David Brownell40982be2008-06-19 17:52:58 -0700590 struct usb_function *f;
591 int status;
592
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200593 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700594 /* write the config descriptor */
595 c = buf;
596 c->bLength = USB_DT_CONFIG_SIZE;
597 c->bDescriptorType = type;
598 /* wTotalLength is written later */
599 c->bNumInterfaces = config->next_interface_id;
600 c->bConfigurationValue = config->bConfigurationValue;
601 c->iConfiguration = config->iConfiguration;
602 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100603 c->bMaxPower = encode_bMaxPower(speed, config);
Vijayavardhan Vennapusadaeb5ac2018-07-13 13:04:17 +0530604 if (config->cdev->gadget->self_powered) {
605 c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER;
606 c->bMaxPower = 0;
607 }
David Brownell40982be2008-06-19 17:52:58 -0700608
609 /* There may be e.g. OTG descriptors */
610 if (config->descriptors) {
611 status = usb_descriptor_fillbuf(next, len,
612 config->descriptors);
613 if (status < 0)
614 return status;
615 len -= status;
616 next += status;
617 }
618
619 /* add each function's descriptors */
620 list_for_each_entry(f, &config->functions, list) {
621 struct usb_descriptor_header **descriptors;
622
John Younf3bdbe32016-02-05 17:07:03 -0800623 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700624 if (!descriptors)
625 continue;
626 status = usb_descriptor_fillbuf(next, len,
627 (const struct usb_descriptor_header **) descriptors);
628 if (status < 0)
629 return status;
630 len -= status;
631 next += status;
632 }
633
634 len = next - buf;
635 c->wTotalLength = cpu_to_le16(len);
636 return len;
637}
638
639static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
640{
641 struct usb_gadget *gadget = cdev->gadget;
642 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200643 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700644 u8 type = w_value >> 8;
645 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
646
John Youneae58202016-02-05 17:07:17 -0800647 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300648 speed = gadget->speed;
649 else if (gadget_is_dualspeed(gadget)) {
650 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700651 if (gadget->speed == USB_SPEED_HIGH)
652 hs = 1;
653 if (type == USB_DT_OTHER_SPEED_CONFIG)
654 hs = !hs;
655 if (hs)
656 speed = USB_SPEED_HIGH;
657
658 }
659
660 /* This is a lookup by config *INDEX* */
661 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200662
663 pos = &cdev->configs;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200664
665 while ((pos = pos->next) != &cdev->configs) {
666 c = list_entry(pos, typeof(*c), list);
667
David Brownell40982be2008-06-19 17:52:58 -0700668 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300669 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800670 case USB_SPEED_SUPER_PLUS:
671 if (!c->superspeed_plus)
672 continue;
673 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300674 case USB_SPEED_SUPER:
675 if (!c->superspeed)
676 continue;
677 break;
678 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700679 if (!c->highspeed)
680 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300681 break;
682 default:
David Brownell40982be2008-06-19 17:52:58 -0700683 if (!c->fullspeed)
684 continue;
685 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300686
David Brownell40982be2008-06-19 17:52:58 -0700687 if (w_value == 0)
688 return config_buf(c, speed, cdev->req->buf, type);
689 w_value--;
690 }
691 return -EINVAL;
692}
693
694static int count_configs(struct usb_composite_dev *cdev, unsigned type)
695{
696 struct usb_gadget *gadget = cdev->gadget;
697 struct usb_configuration *c;
698 unsigned count = 0;
699 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300700 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800701 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700702
703 if (gadget_is_dualspeed(gadget)) {
704 if (gadget->speed == USB_SPEED_HIGH)
705 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300706 if (gadget->speed == USB_SPEED_SUPER)
707 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800708 if (gadget->speed == USB_SPEED_SUPER_PLUS)
709 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700710 if (type == USB_DT_DEVICE_QUALIFIER)
711 hs = !hs;
712 }
713 list_for_each_entry(c, &cdev->configs, list) {
714 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800715 if (ssp) {
716 if (!c->superspeed_plus)
717 continue;
718 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300719 if (!c->superspeed)
720 continue;
721 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700722 if (!c->highspeed)
723 continue;
724 } else {
725 if (!c->fullspeed)
726 continue;
727 }
728 count++;
729 }
730 return count;
731}
732
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300733/**
734 * bos_desc() - prepares the BOS descriptor.
735 * @cdev: pointer to usb_composite device to generate the bos
736 * descriptor for
737 *
738 * This function generates the BOS (Binary Device Object)
739 * descriptor and its device capabilities descriptors. The BOS
740 * descriptor should be supported by a SuperSpeed device.
741 */
742static int bos_desc(struct usb_composite_dev *cdev)
743{
744 struct usb_ext_cap_descriptor *usb_ext;
745 struct usb_ss_cap_descriptor *ss_cap;
746 struct usb_dcd_config_params dcd_config_params;
747 struct usb_bos_descriptor *bos = cdev->req->buf;
748
749 bos->bLength = USB_DT_BOS_SIZE;
750 bos->bDescriptorType = USB_DT_BOS;
751
752 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
753 bos->bNumDeviceCaps = 0;
754
755 /*
756 * A SuperSpeed device shall include the USB2.0 extension descriptor
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800757 * and shall support LPM when operating in USB2.0 HS mode, as well as
758 * a HS device when operating in USB2.1 HS mode.
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300759 */
760 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
761 bos->bNumDeviceCaps++;
762 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
763 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
764 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
765 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500766 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300767
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800768 if (gadget_is_superspeed(cdev->gadget)) {
769 /*
770 * The Superspeed USB Capability descriptor shall be
771 * implemented by all SuperSpeed devices.
772 */
773 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
774 bos->bNumDeviceCaps++;
775 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
776 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
777 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
778 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
779 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
780 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
781 USB_FULL_SPEED_OPERATION |
782 USB_HIGH_SPEED_OPERATION |
783 USB_5GBPS_OPERATION);
784 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300785
Shimrit Malichi184d6fd2016-12-19 14:46:18 -0800786 /* Get Controller configuration */
787 if (cdev->gadget->ops->get_config_params)
788 cdev->gadget->ops->get_config_params
789 (&dcd_config_params);
790 else {
791 dcd_config_params.bU1devExitLat =
792 USB_DEFAULT_U1_DEV_EXIT_LAT;
793 dcd_config_params.bU2DevExitLat =
794 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
795 }
796 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
797 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300798 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300799
John Younf228a8d2016-02-05 17:05:53 -0800800 /* The SuperSpeedPlus USB Device Capability descriptor */
801 if (gadget_is_superspeed_plus(cdev->gadget)) {
802 struct usb_ssp_cap_descriptor *ssp_cap;
803
804 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
805 bos->bNumDeviceCaps++;
806
807 /*
808 * Report typical values.
809 */
810
811 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
812 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
813 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
814 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700815 ssp_cap->bReserved = 0;
816 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800817
818 /* SSAC = 1 (2 attributes) */
819 ssp_cap->bmAttributes = cpu_to_le32(1);
820
821 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700822 ssp_cap->wFunctionalitySupport =
823 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800824
825 /*
826 * bmSublinkSpeedAttr[0]:
827 * ST = Symmetric, RX
828 * LSE = 3 (Gbps)
829 * LP = 1 (SuperSpeedPlus)
830 * LSM = 10 (10 Gbps)
831 */
832 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700833 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800834 /*
835 * bmSublinkSpeedAttr[1] =
836 * ST = Symmetric, TX
837 * LSE = 3 (Gbps)
838 * LP = 1 (SuperSpeedPlus)
839 * LSM = 10 (10 Gbps)
840 */
841 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700842 cpu_to_le32((3 << 4) | (1 << 14) |
843 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800844 }
845
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300846 return le16_to_cpu(bos->wTotalLength);
847}
848
David Brownell40982be2008-06-19 17:52:58 -0700849static void device_qual(struct usb_composite_dev *cdev)
850{
851 struct usb_qualifier_descriptor *qual = cdev->req->buf;
852
853 qual->bLength = sizeof(*qual);
854 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
855 /* POLICY: same bcdUSB and device type info at both speeds */
856 qual->bcdUSB = cdev->desc.bcdUSB;
857 qual->bDeviceClass = cdev->desc.bDeviceClass;
858 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
859 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
860 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200861 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700862 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700863 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700864}
865
866/*-------------------------------------------------------------------------*/
867
868static void reset_config(struct usb_composite_dev *cdev)
869{
870 struct usb_function *f;
871
872 DBG(cdev, "reset config\n");
873
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +0530874 if (!cdev->config) {
875 pr_err("%s:cdev->config is already NULL\n", __func__);
876 return;
877 }
878
David Brownell40982be2008-06-19 17:52:58 -0700879 list_for_each_entry(f, &cdev->config->functions, list) {
880 if (f->disable)
881 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200882
Danny Segalf83e4512016-12-06 15:35:24 -0800883 /* USB 3.0 addition */
884 f->func_is_suspended = false;
885 f->func_wakeup_allowed = false;
Danny Segalde7cd8d2014-07-28 18:08:33 +0300886 f->func_wakeup_pending = false;
Danny Segalf83e4512016-12-06 15:35:24 -0800887
Laurent Pinchart52426582009-10-21 00:03:38 +0200888 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700889 }
890 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100891 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700892}
893
894static int set_config(struct usb_composite_dev *cdev,
895 const struct usb_ctrlrequest *ctrl, unsigned number)
896{
897 struct usb_gadget *gadget = cdev->gadget;
898 struct usb_configuration *c = NULL;
899 int result = -EINVAL;
David Brownell40982be2008-06-19 17:52:58 -0700900 int tmp;
901
David Brownell40982be2008-06-19 17:52:58 -0700902 if (number) {
903 list_for_each_entry(c, &cdev->configs, list) {
904 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300905 /*
906 * We disable the FDs of the previous
907 * configuration only if the new configuration
908 * is a valid one
909 */
910 if (cdev->config)
911 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700912 result = 0;
913 break;
914 }
915 }
916 if (result < 0)
917 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300918 } else { /* Zero configuration value - need to reset the config */
919 if (cdev->config)
920 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700921 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300922 }
David Brownell40982be2008-06-19 17:52:58 -0700923
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200924 INFO(cdev, "%s config #%d: %s\n",
925 usb_speed_string(gadget->speed),
926 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700927
928 if (!c)
929 goto done;
930
Aniket Randive0a78a292020-03-18 11:22:09 +0530931 place_marker("M - USB Device is enumerated");
Peter Chen6027f312014-04-29 13:26:28 +0800932 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700933 cdev->config = c;
Jack Phamdb943d62016-12-16 11:21:16 -0800934 c->num_ineps_used = 0;
935 c->num_outeps_used = 0;
David Brownell40982be2008-06-19 17:52:58 -0700936
937 /* Initialize all interfaces by setting them to altsetting zero. */
938 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
939 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200940 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700941
942 if (!f)
943 break;
944
Laurent Pinchart52426582009-10-21 00:03:38 +0200945 /*
946 * Record which endpoints are used by the function. This is used
947 * to dispatch control requests targeted at that endpoint to the
948 * function's setup callback instead of the current
949 * configuration's setup callback.
950 */
John Younf3bdbe32016-02-05 17:07:03 -0800951 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200952
953 for (; *descriptors; ++descriptors) {
954 struct usb_endpoint_descriptor *ep;
955 int addr;
956
957 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
958 continue;
959
960 ep = (struct usb_endpoint_descriptor *)*descriptors;
961 addr = ((ep->bEndpointAddress & 0x80) >> 3)
962 | (ep->bEndpointAddress & 0x0f);
963 set_bit(addr, f->endpoints);
Jack Phamdb943d62016-12-16 11:21:16 -0800964 if (usb_endpoint_dir_in(ep))
965 c->num_ineps_used++;
966 else
967 c->num_outeps_used++;
Laurent Pinchart52426582009-10-21 00:03:38 +0200968 }
969
David Brownell40982be2008-06-19 17:52:58 -0700970 result = f->set_alt(f, tmp, 0);
971 if (result < 0) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +0530972 DBG(cdev, "interface %d (%s/%pK) alt 0 --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700973 tmp, f->name, f, result);
974
975 reset_config(cdev);
976 goto done;
977 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300978
979 if (result == USB_GADGET_DELAYED_STATUS) {
980 DBG(cdev,
981 "%s: interface %d (%s) requested delayed status\n",
982 __func__, tmp, f->name);
983 cdev->delayed_status++;
984 DBG(cdev, "delayed_status count %d\n",
985 cdev->delayed_status);
986 }
David Brownell40982be2008-06-19 17:52:58 -0700987 }
988
David Brownell40982be2008-06-19 17:52:58 -0700989done:
Mayank Rana4be36022016-12-19 15:03:00 -0800990 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
Roger Quadros1b9ba002011-05-09 13:08:06 +0300991 if (result >= 0 && cdev->delayed_status)
992 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700993 return result;
994}
995
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100996int usb_add_config_only(struct usb_composite_dev *cdev,
997 struct usb_configuration *config)
998{
999 struct usb_configuration *c;
1000
1001 if (!config->bConfigurationValue)
1002 return -EINVAL;
1003
1004 /* Prevent duplicate configuration identifiers */
1005 list_for_each_entry(c, &cdev->configs, list) {
1006 if (c->bConfigurationValue == config->bConfigurationValue)
1007 return -EBUSY;
1008 }
1009
1010 config->cdev = cdev;
1011 list_add_tail(&config->list, &cdev->configs);
1012
1013 INIT_LIST_HEAD(&config->functions);
1014 config->next_interface_id = 0;
1015 memset(config->interface, 0, sizeof(config->interface));
1016
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(usb_add_config_only);
1020
David Brownell40982be2008-06-19 17:52:58 -07001021/**
1022 * usb_add_config() - add a configuration to a device.
1023 * @cdev: wraps the USB gadget
1024 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001025 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -07001026 * Context: single threaded during gadget setup
1027 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001028 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -07001029 * add each of the configurations it supports, using this routine.
1030 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001031 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -07001032 * is zero for success else a negative errno value. Binding configurations
1033 * assigns global resources including string IDs, and per-configuration
1034 * resources such as interface IDs and endpoints.
1035 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001036int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001037 struct usb_configuration *config,
1038 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -07001039{
1040 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001041
1042 if (!bind)
1043 goto done;
David Brownell40982be2008-06-19 17:52:58 -07001044
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301045 DBG(cdev, "adding config #%u '%s'/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001046 config->bConfigurationValue,
1047 config->label, config);
1048
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +01001049 status = usb_add_config_only(cdev, config);
1050 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001051 goto done;
1052
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +02001053 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -07001054 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +09001055 while (!list_empty(&config->functions)) {
1056 struct usb_function *f;
1057
1058 f = list_first_entry(&config->functions,
1059 struct usb_function, list);
1060 list_del(&f->list);
1061 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301062 DBG(cdev, "unbind function '%s'/%pK\n",
Yongsul Oh124ef382012-03-20 10:38:38 +09001063 f->name, f);
1064 f->unbind(config, f);
1065 /* may free memory for "f" */
1066 }
1067 }
David Brownell40982be2008-06-19 17:52:58 -07001068 list_del(&config->list);
1069 config->cdev = NULL;
1070 } else {
1071 unsigned i;
1072
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301073 DBG(cdev, "cfg %d/%pK speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -07001074 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -08001075 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001076 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -07001077 config->highspeed ? " high" : "",
1078 config->fullspeed
1079 ? (gadget_is_dualspeed(cdev->gadget)
1080 ? " full"
1081 : " full/low")
1082 : "");
1083
1084 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1085 struct usb_function *f = config->interface[i];
1086
1087 if (!f)
1088 continue;
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301089 DBG(cdev, " interface %d = %s/%pK\n",
David Brownell40982be2008-06-19 17:52:58 -07001090 i, f->name, f);
1091 }
1092 }
1093
Robert Baldygaf871cb92015-09-16 12:10:39 +02001094 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -07001095 usb_ep_autoconfig_reset(cdev->gadget);
1096
1097done:
1098 if (status)
1099 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1100 config->bConfigurationValue, status);
1101 return status;
1102}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001103EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -07001104
Benoit Goby51cce6f2012-05-10 10:07:57 +02001105static void remove_config(struct usb_composite_dev *cdev,
1106 struct usb_configuration *config)
1107{
1108 while (!list_empty(&config->functions)) {
1109 struct usb_function *f;
1110
1111 f = list_first_entry(&config->functions,
1112 struct usb_function, list);
1113 list_del(&f->list);
1114 if (f->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301115 DBG(cdev, "unbind function '%s'/%pK\n", f->name, f);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001116 f->unbind(config, f);
1117 /* may free memory for "f" */
1118 }
1119 }
1120 list_del(&config->list);
1121 if (config->unbind) {
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301122 DBG(cdev, "unbind config '%s'/%pK\n", config->label, config);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001123 config->unbind(config);
1124 /* may free memory for "c" */
1125 }
1126}
1127
1128/**
1129 * usb_remove_config() - remove a configuration from a device.
1130 * @cdev: wraps the USB gadget
1131 * @config: the configuration
1132 *
1133 * Drivers must call usb_gadget_disconnect before calling this function
1134 * to disconnect the device from the host and make sure the host will not
1135 * try to enumerate the device while we are changing the config list.
1136 */
1137void usb_remove_config(struct usb_composite_dev *cdev,
1138 struct usb_configuration *config)
1139{
1140 unsigned long flags;
1141
1142 spin_lock_irqsave(&cdev->lock, flags);
1143
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301144 if (cdev->config == config) {
1145 if (cdev->gadget->is_chipidea && !cdev->suspended) {
1146 spin_unlock_irqrestore(&cdev->lock, flags);
1147 msm_do_bam_disable_enable(CI_CTRL);
1148 spin_lock_irqsave(&cdev->lock, flags);
1149 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001150 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05301151 }
Benoit Goby51cce6f2012-05-10 10:07:57 +02001152
1153 spin_unlock_irqrestore(&cdev->lock, flags);
1154
1155 remove_config(cdev, config);
1156}
1157
David Brownell40982be2008-06-19 17:52:58 -07001158/*-------------------------------------------------------------------------*/
1159
1160/* We support strings in multiple languages ... string descriptor zero
1161 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -03001162 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -07001163 * the host side.
1164 */
1165
1166static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1167{
1168 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001169 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001170 __le16 *tmp;
1171
1172 while (*sp) {
1173 s = *sp;
1174 language = cpu_to_le16(s->language);
1175 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1176 if (*tmp == language)
1177 goto repeat;
1178 }
1179 *tmp++ = language;
1180repeat:
1181 sp++;
1182 }
1183}
1184
1185static int lookup_string(
1186 struct usb_gadget_strings **sp,
1187 void *buf,
1188 u16 language,
1189 int id
1190)
1191{
1192 struct usb_gadget_strings *s;
1193 int value;
1194
1195 while (*sp) {
1196 s = *sp++;
1197 if (s->language != language)
1198 continue;
1199 value = usb_gadget_get_string(s, id, buf);
1200 if (value > 0)
1201 return value;
1202 }
1203 return -EINVAL;
1204}
1205
1206static int get_string(struct usb_composite_dev *cdev,
1207 void *buf, u16 language, int id)
1208{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001209 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001210 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001211 struct usb_configuration *c;
1212 struct usb_function *f;
1213 int len;
1214
Diego Violaad4676a2015-05-31 15:52:41 -03001215 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001216 * folk will ever care about ... also, it's all supported here.
1217 * (Except for UTF8 support for Unicode's "Astral Planes".)
1218 */
1219
1220 /* 0 == report all available language codes */
1221 if (id == 0) {
1222 struct usb_string_descriptor *s = buf;
1223 struct usb_gadget_strings **sp;
1224
1225 memset(s, 0, 256);
1226 s->bDescriptorType = USB_DT_STRING;
1227
1228 sp = composite->strings;
1229 if (sp)
1230 collect_langs(sp, s->wData);
1231
1232 list_for_each_entry(c, &cdev->configs, list) {
1233 sp = c->strings;
1234 if (sp)
1235 collect_langs(sp, s->wData);
1236
1237 list_for_each_entry(f, &c->functions, list) {
1238 sp = f->strings;
1239 if (sp)
1240 collect_langs(sp, s->wData);
1241 }
1242 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001243 list_for_each_entry(uc, &cdev->gstrings, list) {
1244 struct usb_gadget_strings **sp;
1245
1246 sp = get_containers_gs(uc);
1247 collect_langs(sp, s->wData);
1248 }
David Brownell40982be2008-06-19 17:52:58 -07001249
Roel Kluin417b57b2009-08-06 16:09:51 -07001250 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001251 continue;
1252 if (!len)
1253 return -EINVAL;
1254
1255 s->bLength = 2 * (len + 1);
1256 return s->bLength;
1257 }
1258
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001259 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1260 struct usb_os_string *b = buf;
1261 b->bLength = sizeof(*b);
1262 b->bDescriptorType = USB_DT_STRING;
1263 compiletime_assert(
1264 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1265 "qwSignature size must be equal to qw_sign");
1266 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1267 b->bMS_VendorCode = cdev->b_vendor_code;
1268 b->bPad = 0;
1269 return sizeof(*b);
1270 }
1271
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001272 list_for_each_entry(uc, &cdev->gstrings, list) {
1273 struct usb_gadget_strings **sp;
1274
1275 sp = get_containers_gs(uc);
1276 len = lookup_string(sp, buf, language, id);
1277 if (len > 0)
1278 return len;
1279 }
1280
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001281 /* String IDs are device-scoped, so we look up each string
1282 * table we're told about. These lookups are infrequent;
1283 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001284 */
1285 if (composite->strings) {
1286 len = lookup_string(composite->strings, buf, language, id);
1287 if (len > 0)
1288 return len;
1289 }
1290 list_for_each_entry(c, &cdev->configs, list) {
1291 if (c->strings) {
1292 len = lookup_string(c->strings, buf, language, id);
1293 if (len > 0)
1294 return len;
1295 }
1296 list_for_each_entry(f, &c->functions, list) {
1297 if (!f->strings)
1298 continue;
1299 len = lookup_string(f->strings, buf, language, id);
1300 if (len > 0)
1301 return len;
1302 }
1303 }
1304 return -EINVAL;
1305}
1306
1307/**
1308 * usb_string_id() - allocate an unused string ID
1309 * @cdev: the device whose string descriptor IDs are being allocated
1310 * Context: single threaded during gadget setup
1311 *
1312 * @usb_string_id() is called from bind() callbacks to allocate
1313 * string IDs. Drivers for functions, configurations, or gadgets will
1314 * then store that ID in the appropriate descriptors and string table.
1315 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001316 * All string identifier should be allocated using this,
1317 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1318 * that for example different functions don't wrongly assign different
1319 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001320 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001321int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001322{
1323 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001324 /* string id 0 is reserved by USB spec for list of
1325 * supported languages */
1326 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001327 cdev->next_string_id++;
1328 return cdev->next_string_id;
1329 }
1330 return -ENODEV;
1331}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001332EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001333
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001334/**
1335 * usb_string_ids() - allocate unused string IDs in batch
1336 * @cdev: the device whose string descriptor IDs are being allocated
1337 * @str: an array of usb_string objects to assign numbers to
1338 * Context: single threaded during gadget setup
1339 *
1340 * @usb_string_ids() is called from bind() callbacks to allocate
1341 * string IDs. Drivers for functions, configurations, or gadgets will
1342 * then copy IDs from the string table to the appropriate descriptors
1343 * and string table for other languages.
1344 *
1345 * All string identifier should be allocated using this,
1346 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1347 * example different functions don't wrongly assign different meanings
1348 * to the same identifier.
1349 */
1350int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1351{
1352 int next = cdev->next_string_id;
1353
1354 for (; str->s; ++str) {
1355 if (unlikely(next >= 254))
1356 return -ENODEV;
1357 str->id = ++next;
1358 }
1359
1360 cdev->next_string_id = next;
1361
1362 return 0;
1363}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001364EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001365
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001366static struct usb_gadget_string_container *copy_gadget_strings(
1367 struct usb_gadget_strings **sp, unsigned n_gstrings,
1368 unsigned n_strings)
1369{
1370 struct usb_gadget_string_container *uc;
1371 struct usb_gadget_strings **gs_array;
1372 struct usb_gadget_strings *gs;
1373 struct usb_string *s;
1374 unsigned mem;
1375 unsigned n_gs;
1376 unsigned n_s;
1377 void *stash;
1378
1379 mem = sizeof(*uc);
1380 mem += sizeof(void *) * (n_gstrings + 1);
1381 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1382 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1383 uc = kmalloc(mem, GFP_KERNEL);
1384 if (!uc)
1385 return ERR_PTR(-ENOMEM);
1386 gs_array = get_containers_gs(uc);
1387 stash = uc->stash;
1388 stash += sizeof(void *) * (n_gstrings + 1);
1389 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1390 struct usb_string *org_s;
1391
1392 gs_array[n_gs] = stash;
1393 gs = gs_array[n_gs];
1394 stash += sizeof(struct usb_gadget_strings);
1395 gs->language = sp[n_gs]->language;
1396 gs->strings = stash;
1397 org_s = sp[n_gs]->strings;
1398
1399 for (n_s = 0; n_s < n_strings; n_s++) {
1400 s = stash;
1401 stash += sizeof(struct usb_string);
1402 if (org_s->s)
1403 s->s = org_s->s;
1404 else
1405 s->s = "";
1406 org_s++;
1407 }
1408 s = stash;
1409 s->s = NULL;
1410 stash += sizeof(struct usb_string);
1411
1412 }
1413 gs_array[n_gs] = NULL;
1414 return uc;
1415}
1416
1417/**
1418 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1419 * @cdev: the device whose string descriptor IDs are being allocated
1420 * and attached.
1421 * @sp: an array of usb_gadget_strings to attach.
1422 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1423 *
1424 * This function will create a deep copy of usb_gadget_strings and usb_string
1425 * and attach it to the cdev. The actual string (usb_string.s) will not be
1426 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001427 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001428 * The ->language pointer of each struct usb_gadget_strings has to contain the
1429 * same amount of entries.
1430 * 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 +09001431 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001432 * entry of en-US. Therefore both entries become the same id assign.
1433 */
1434struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1435 struct usb_gadget_strings **sp, unsigned n_strings)
1436{
1437 struct usb_gadget_string_container *uc;
1438 struct usb_gadget_strings **n_gs;
1439 unsigned n_gstrings = 0;
1440 unsigned i;
1441 int ret;
1442
1443 for (i = 0; sp[i]; i++)
1444 n_gstrings++;
1445
1446 if (!n_gstrings)
1447 return ERR_PTR(-EINVAL);
1448
1449 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1450 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001451 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001452
1453 n_gs = get_containers_gs(uc);
1454 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1455 if (ret)
1456 goto err;
1457
1458 for (i = 1; i < n_gstrings; i++) {
1459 struct usb_string *m_s;
1460 struct usb_string *s;
1461 unsigned n;
1462
1463 m_s = n_gs[0]->strings;
1464 s = n_gs[i]->strings;
1465 for (n = 0; n < n_strings; n++) {
1466 s->id = m_s->id;
1467 s++;
1468 m_s++;
1469 }
1470 }
1471 list_add_tail(&uc->list, &cdev->gstrings);
1472 return n_gs[0]->strings;
1473err:
1474 kfree(uc);
1475 return ERR_PTR(ret);
1476}
1477EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1478
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001479/**
1480 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001481 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001482 * @n: number of string IDs to allocate
1483 * Context: single threaded during gadget setup
1484 *
1485 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001486 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001487 * is, returns last requested ID which is now very useful information.
1488 *
1489 * @usb_string_ids_n() is called from bind() callbacks to allocate
1490 * string IDs. Drivers for functions, configurations, or gadgets will
1491 * then store that ID in the appropriate descriptors and string table.
1492 *
1493 * All string identifier should be allocated using this,
1494 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1495 * example different functions don't wrongly assign different meanings
1496 * to the same identifier.
1497 */
1498int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1499{
1500 unsigned next = c->next_string_id;
1501 if (unlikely(n > 254 || (unsigned)next + n > 254))
1502 return -ENODEV;
1503 c->next_string_id += n;
1504 return next + 1;
1505}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001506EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001507
David Brownell40982be2008-06-19 17:52:58 -07001508/*-------------------------------------------------------------------------*/
1509
1510static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1511{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001512 struct usb_composite_dev *cdev;
1513
David Brownell40982be2008-06-19 17:52:58 -07001514 if (req->status || req->actual != req->length)
1515 DBG((struct usb_composite_dev *) ep->driver_data,
1516 "setup complete --> %d, %d/%d\n",
1517 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001518
1519 /*
1520 * REVIST The same ep0 requests are shared with function drivers
1521 * so they don't have to maintain the same ->complete() stubs.
1522 *
1523 * Because of that, we need to check for the validity of ->context
1524 * here, even though we know we've set it to something useful.
1525 */
1526 if (!req->context)
1527 return;
1528
1529 cdev = req->context;
1530
1531 if (cdev->req == req)
1532 cdev->setup_pending = false;
1533 else if (cdev->os_desc_req == req)
1534 cdev->os_desc_pending = false;
1535 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301536 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001537}
1538
1539static int composite_ep0_queue(struct usb_composite_dev *cdev,
1540 struct usb_request *req, gfp_t gfp_flags)
1541{
1542 int ret;
1543
1544 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1545 if (ret == 0) {
1546 if (cdev->req == req)
1547 cdev->setup_pending = true;
1548 else if (cdev->os_desc_req == req)
1549 cdev->os_desc_pending = true;
1550 else
Chandana Kishori Chiluveru7f5670a2017-10-28 23:05:45 +05301551 WARN(1, "unknown request %pK\n", req);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001552 }
1553
1554 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001555}
1556
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001557static int count_ext_compat(struct usb_configuration *c)
1558{
1559 int i, res;
1560
1561 res = 0;
1562 for (i = 0; i < c->next_interface_id; ++i) {
1563 struct usb_function *f;
1564 int j;
1565
1566 f = c->interface[i];
1567 for (j = 0; j < f->os_desc_n; ++j) {
1568 struct usb_os_desc *d;
1569
1570 if (i != f->os_desc_table[j].if_id)
1571 continue;
1572 d = f->os_desc_table[j].os_desc;
1573 if (d && d->ext_compat_id)
1574 ++res;
1575 }
1576 }
1577 BUG_ON(res > 255);
1578 return res;
1579}
1580
Chris Dickensbf54f312017-12-31 18:59:42 -08001581static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001582{
1583 int i, count;
1584
1585 count = 16;
1586 for (i = 0; i < c->next_interface_id; ++i) {
1587 struct usb_function *f;
1588 int j;
1589
1590 f = c->interface[i];
1591 for (j = 0; j < f->os_desc_n; ++j) {
1592 struct usb_os_desc *d;
1593
1594 if (i != f->os_desc_table[j].if_id)
1595 continue;
1596 d = f->os_desc_table[j].os_desc;
1597 if (d && d->ext_compat_id) {
1598 *buf++ = i;
1599 *buf++ = 0x01;
1600 memcpy(buf, d->ext_compat_id, 16);
1601 buf += 22;
1602 } else {
1603 ++buf;
1604 *buf = 0x01;
1605 buf += 23;
1606 }
1607 count += 24;
Chris Dickensbf54f312017-12-31 18:59:42 -08001608 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1609 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001610 }
1611 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001612
1613 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001614}
1615
1616static int count_ext_prop(struct usb_configuration *c, int interface)
1617{
1618 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001619 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001620
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301621 if (interface >= c->next_interface_id)
1622 return -EINVAL;
1623
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001624 f = c->interface[interface];
1625 for (j = 0; j < f->os_desc_n; ++j) {
1626 struct usb_os_desc *d;
1627
1628 if (interface != f->os_desc_table[j].if_id)
1629 continue;
1630 d = f->os_desc_table[j].os_desc;
1631 if (d && d->ext_compat_id)
1632 return d->ext_prop_count;
1633 }
Julia Lawall849b1332014-05-19 06:31:07 +02001634 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001635}
1636
1637static int len_ext_prop(struct usb_configuration *c, int interface)
1638{
1639 struct usb_function *f;
1640 struct usb_os_desc *d;
1641 int j, res;
1642
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05301643 if (interface >= c->next_interface_id)
1644 return -EINVAL;
1645
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001646 res = 10; /* header length */
1647 f = c->interface[interface];
1648 for (j = 0; j < f->os_desc_n; ++j) {
1649 if (interface != f->os_desc_table[j].if_id)
1650 continue;
1651 d = f->os_desc_table[j].os_desc;
1652 if (d)
1653 return min(res + d->ext_prop_len, 4096);
1654 }
1655 return res;
1656}
1657
1658static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1659{
1660 struct usb_function *f;
1661 struct usb_os_desc *d;
1662 struct usb_os_desc_ext_prop *ext_prop;
1663 int j, count, n, ret;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001664
1665 f = c->interface[interface];
Chris Dickensbf54f312017-12-31 18:59:42 -08001666 count = 10; /* header length */
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001667 for (j = 0; j < f->os_desc_n; ++j) {
1668 if (interface != f->os_desc_table[j].if_id)
1669 continue;
1670 d = f->os_desc_table[j].os_desc;
1671 if (d)
1672 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
Chris Dickensbf54f312017-12-31 18:59:42 -08001673 n = ext_prop->data_len +
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001674 ext_prop->name_len + 14;
Chris Dickensbf54f312017-12-31 18:59:42 -08001675 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1676 return count;
1677 usb_ext_prop_put_size(buf, n);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001678 usb_ext_prop_put_type(buf, ext_prop->type);
1679 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1680 ext_prop->name_len);
1681 if (ret < 0)
1682 return ret;
1683 switch (ext_prop->type) {
1684 case USB_EXT_PROP_UNICODE:
1685 case USB_EXT_PROP_UNICODE_ENV:
1686 case USB_EXT_PROP_UNICODE_LINK:
1687 usb_ext_prop_put_unicode(buf, ret,
1688 ext_prop->data,
1689 ext_prop->data_len);
1690 break;
1691 case USB_EXT_PROP_BINARY:
1692 usb_ext_prop_put_binary(buf, ret,
1693 ext_prop->data,
1694 ext_prop->data_len);
1695 break;
1696 case USB_EXT_PROP_LE32:
1697 /* not implemented */
1698 case USB_EXT_PROP_BE32:
1699 /* not implemented */
1700 default:
1701 return -EINVAL;
1702 }
Chris Dickensbf54f312017-12-31 18:59:42 -08001703 buf += n;
1704 count += n;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001705 }
1706 }
1707
Chris Dickensbf54f312017-12-31 18:59:42 -08001708 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001709}
1710
David Brownell40982be2008-06-19 17:52:58 -07001711/*
1712 * The setup() callback implements all the ep0 functionality that's
1713 * not handled lower down, in hardware or the hardware driver(like
1714 * device and endpoint feature flags, and their status). It's all
1715 * housekeeping for the gadget function we're implementing. Most of
1716 * the work is in config and function specific setup.
1717 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001718int
David Brownell40982be2008-06-19 17:52:58 -07001719composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1720{
1721 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1722 struct usb_request *req = cdev->req;
1723 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001724 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001725 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001726 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001727 u16 w_value = le16_to_cpu(ctrl->wValue);
1728 u16 w_length = le16_to_cpu(ctrl->wLength);
1729 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001730 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001731
1732 /* partial re-init of the response message; the function or the
1733 * gadget might need to intercept e.g. a control-OUT completion
1734 * when we delegate to it.
1735 */
1736 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001737 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001738 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301739 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001740 gadget->ep0->driver_data = cdev;
1741
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001742 /*
1743 * Don't let non-standard requests match any of the cases below
1744 * by accident.
1745 */
1746 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1747 goto unknown;
1748
David Brownell40982be2008-06-19 17:52:58 -07001749 switch (ctrl->bRequest) {
1750
1751 /* we handle all standard USB descriptors */
1752 case USB_REQ_GET_DESCRIPTOR:
1753 if (ctrl->bRequestType != USB_DIR_IN)
1754 goto unknown;
1755 switch (w_value >> 8) {
1756
1757 case USB_DT_DEVICE:
1758 cdev->desc.bNumConfigurations =
1759 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001760 cdev->desc.bMaxPacketSize0 =
1761 cdev->gadget->ep0->maxpacket;
Mayank Rana72bf6d02016-12-19 14:48:38 -08001762 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001763 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001764 if (gadget->speed >= USB_SPEED_SUPER) {
Chunfeng Yun3069db12018-05-09 19:29:16 +08001765 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001766 cdev->desc.bMaxPacketSize0 = 9;
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301767 } else if (gadget->l1_supported
1768 && !disable_l1_for_hs) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001769 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001770 }
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301771 } else if (gadget->l1_supported
1772 && !disable_l1_for_hs) {
Shimrit Malichi184d6fd2016-12-19 14:46:18 -08001773 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1774 DBG(cdev, "Config HS device with LPM(L1)\n");
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001775 }
1776
David Brownell40982be2008-06-19 17:52:58 -07001777 value = min(w_length, (u16) sizeof cdev->desc);
1778 memcpy(req->buf, &cdev->desc, value);
1779 break;
1780 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001781 if (!gadget_is_dualspeed(gadget) ||
1782 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001783 break;
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301784 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001785 device_qual(cdev);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301786 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001787 value = min_t(int, w_length,
1788 sizeof(struct usb_qualifier_descriptor));
1789 break;
1790 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001791 if (!gadget_is_dualspeed(gadget) ||
1792 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001793 break;
1794 /* FALLTHROUGH */
1795 case USB_DT_CONFIG:
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301796 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001797 value = config_desc(cdev, w_value);
Vijayavardhan Vennapusaa1291892016-08-18 11:57:18 +05301798 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001799 if (value >= 0)
1800 value = min(w_length, (u16) value);
1801 break;
1802 case USB_DT_STRING:
1803 value = get_string(cdev, req->buf,
1804 w_index, w_value & 0xff);
1805 if (value >= 0)
1806 value = min(w_length, (u16) value);
1807 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001808 case USB_DT_BOS:
Sriharsha Allenki876ac062018-03-05 01:08:07 +05301809 if ((gadget_is_superspeed(gadget) &&
1810 (gadget->speed >= USB_SPEED_SUPER)) ||
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05301811 (gadget->l1_supported
1812 && !disable_l1_for_hs)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001813 value = bos_desc(cdev);
1814 value = min(w_length, (u16) value);
1815 }
1816 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001817 case USB_DT_OTG:
1818 if (gadget_is_otg(gadget)) {
1819 struct usb_configuration *config;
1820 int otg_desc_len = 0;
1821
1822 if (cdev->config)
1823 config = cdev->config;
1824 else
1825 config = list_first_entry(
1826 &cdev->configs,
1827 struct usb_configuration, list);
1828 if (!config)
1829 goto done;
1830
1831 if (gadget->otg_caps &&
1832 (gadget->otg_caps->otg_rev >= 0x0200))
1833 otg_desc_len += sizeof(
1834 struct usb_otg20_descriptor);
1835 else
1836 otg_desc_len += sizeof(
1837 struct usb_otg_descriptor);
1838
1839 value = min_t(int, w_length, otg_desc_len);
1840 memcpy(req->buf, config->descriptors[0], value);
1841 }
1842 break;
David Brownell40982be2008-06-19 17:52:58 -07001843 }
1844 break;
1845
1846 /* any number of configs can work */
1847 case USB_REQ_SET_CONFIGURATION:
1848 if (ctrl->bRequestType != 0)
1849 goto unknown;
1850 if (gadget_is_otg(gadget)) {
1851 if (gadget->a_hnp_support)
1852 DBG(cdev, "HNP available\n");
1853 else if (gadget->a_alt_hnp_support)
1854 DBG(cdev, "HNP on another port\n");
1855 else
1856 VDBG(cdev, "HNP inactive\n");
1857 }
1858 spin_lock(&cdev->lock);
1859 value = set_config(cdev, ctrl, w_value);
1860 spin_unlock(&cdev->lock);
1861 break;
1862 case USB_REQ_GET_CONFIGURATION:
1863 if (ctrl->bRequestType != USB_DIR_IN)
1864 goto unknown;
1865 if (cdev->config)
1866 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1867 else
1868 *(u8 *)req->buf = 0;
1869 value = min(w_length, (u16) 1);
1870 break;
1871
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001872 /* function drivers must handle get/set altsetting */
David Brownell40982be2008-06-19 17:52:58 -07001873 case USB_REQ_SET_INTERFACE:
1874 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1875 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001876 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001877 break;
Bryan Wu08889512009-01-08 00:21:19 +08001878 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001879 if (!f)
1880 break;
Krzysztof Opasiak2b95c932016-12-20 19:52:16 +01001881
1882 /*
1883 * If there's no get_alt() method, we know only altsetting zero
1884 * works. There is no need to check if set_alt() is not NULL
1885 * as we check this in usb_add_function().
1886 */
1887 if (w_value && !f->get_alt)
David Brownell40982be2008-06-19 17:52:58 -07001888 break;
Chunfeng Yun121621e2018-05-25 17:24:57 +08001889
1890 spin_lock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001891 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001892 if (value == USB_GADGET_DELAYED_STATUS) {
1893 DBG(cdev,
1894 "%s: interface %d (%s) requested delayed status\n",
1895 __func__, intf, f->name);
1896 cdev->delayed_status++;
1897 DBG(cdev, "delayed_status count %d\n",
1898 cdev->delayed_status);
1899 }
Chunfeng Yun121621e2018-05-25 17:24:57 +08001900 spin_unlock(&cdev->lock);
David Brownell40982be2008-06-19 17:52:58 -07001901 break;
1902 case USB_REQ_GET_INTERFACE:
1903 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1904 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001905 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001906 break;
Bryan Wu08889512009-01-08 00:21:19 +08001907 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001908 if (!f)
1909 break;
1910 /* lots of interfaces only need altsetting zero... */
1911 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1912 if (value < 0)
1913 break;
1914 *((u8 *)req->buf) = value;
1915 value = min(w_length, (u16) 1);
1916 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001917 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001918 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1919 (w_index == OTG_STS_SELECTOR)) {
1920 if (ctrl->bRequestType != (USB_DIR_IN |
1921 USB_RECIP_DEVICE))
1922 goto unknown;
1923 *((u8 *)req->buf) = gadget->host_request_flag;
1924 value = 1;
1925 break;
1926 }
1927
1928 /*
1929 * USB 3.0 additions:
1930 * Function driver should handle get_status request. If such cb
1931 * wasn't supplied we respond with default value = 0
1932 * Note: function driver should supply such cb only for the
1933 * first interface of the function
1934 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001935 if (!gadget_is_superspeed(gadget))
1936 goto unknown;
1937 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1938 goto unknown;
1939 value = 2; /* This is the length of the get_status reply */
1940 put_unaligned_le16(0, req->buf);
1941 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1942 break;
1943 f = cdev->config->interface[intf];
1944 if (!f)
1945 break;
1946 status = f->get_status ? f->get_status(f) : 0;
1947 if (status < 0)
1948 break;
1949 put_unaligned_le16(status & 0x0000ffff, req->buf);
1950 break;
1951 /*
1952 * Function drivers should handle SetFeature/ClearFeature
1953 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1954 * only for the first interface of the function
1955 */
1956 case USB_REQ_CLEAR_FEATURE:
1957 case USB_REQ_SET_FEATURE:
1958 if (!gadget_is_superspeed(gadget))
1959 goto unknown;
1960 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1961 goto unknown;
1962 switch (w_value) {
1963 case USB_INTRF_FUNC_SUSPEND:
1964 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1965 break;
1966 f = cdev->config->interface[intf];
1967 if (!f)
1968 break;
1969 value = 0;
Danny Segalf83e4512016-12-06 15:35:24 -08001970 if (f->func_suspend) {
1971 const u8 suspend_opt = w_index >> 8;
1972
1973 value = f->func_suspend(f, suspend_opt);
1974 DBG(cdev, "%s function: FUNCTION_SUSPEND(%u)",
1975 f->name ? f->name : "", suspend_opt);
1976 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001977 if (value < 0) {
1978 ERROR(cdev,
1979 "func_suspend() returned error %d\n",
1980 value);
1981 value = 0;
1982 }
1983 break;
1984 }
1985 break;
David Brownell40982be2008-06-19 17:52:58 -07001986 default:
1987unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001988 /*
1989 * OS descriptors handling
1990 */
1991 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001992 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001993 ctrl->bRequest == cdev->b_vendor_code) {
1994 struct usb_request *req;
1995 struct usb_configuration *os_desc_cfg;
1996 u8 *buf;
1997 int interface;
1998 int count = 0;
1999
2000 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05002001 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002002 req->complete = composite_setup_complete;
2003 buf = req->buf;
2004 os_desc_cfg = cdev->os_desc_config;
Chris Dickensbf54f312017-12-31 18:59:42 -08002005 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002006 memset(buf, 0, w_length);
2007 buf[5] = 0x01;
2008 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2009 case USB_RECIP_DEVICE:
2010 if (w_index != 0x4 || (w_value >> 8))
2011 break;
2012 buf[6] = w_index;
2013 if (w_length == 0x10) {
2014 /* Number of ext compat interfaces */
2015 count = count_ext_compat(os_desc_cfg);
2016 buf[8] = count;
2017 count *= 24; /* 24 B/ext compat desc */
2018 count += 16; /* header */
2019 put_unaligned_le32(count, buf);
2020 value = w_length;
2021 } else {
2022 /* "extended compatibility ID"s */
2023 count = count_ext_compat(os_desc_cfg);
2024 buf[8] = count;
2025 count *= 24; /* 24 B/ext compat desc */
2026 count += 16; /* header */
2027 put_unaligned_le32(count, buf);
2028 buf += 16;
Chris Dickensbf54f312017-12-31 18:59:42 -08002029 value = fill_ext_compat(os_desc_cfg, buf);
2030 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002031 }
2032 break;
2033 case USB_RECIP_INTERFACE:
2034 if (w_index != 0x5 || (w_value >> 8))
2035 break;
2036 interface = w_value & 0xFF;
2037 buf[6] = w_index;
2038 if (w_length == 0x0A) {
2039 count = count_ext_prop(os_desc_cfg,
2040 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302041 if (count < 0)
2042 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002043 put_unaligned_le16(count, buf + 8);
2044 count = len_ext_prop(os_desc_cfg,
2045 interface);
2046 put_unaligned_le32(count, buf);
2047
2048 value = w_length;
2049 } else {
2050 count = count_ext_prop(os_desc_cfg,
2051 interface);
Chandana Kishori Chiluveru1ed237d2018-09-10 19:27:36 +05302052 if (count < 0)
2053 return count;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002054 put_unaligned_le16(count, buf + 8);
2055 count = len_ext_prop(os_desc_cfg,
2056 interface);
2057 put_unaligned_le32(count, buf);
2058 buf += 10;
2059 value = fill_ext_prop(os_desc_cfg,
2060 interface, buf);
2061 if (value < 0)
2062 return value;
Chris Dickensbf54f312017-12-31 18:59:42 -08002063 value = min_t(u16, w_length, value);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002064 }
2065 break;
2066 }
William Wu7e14f47a2016-05-13 18:30:42 +08002067
2068 if (value >= 0) {
2069 req->length = value;
2070 req->context = cdev;
2071 req->zero = value < w_length;
2072 value = composite_ep0_queue(cdev, req,
2073 GFP_ATOMIC);
2074 if (value < 0) {
2075 DBG(cdev, "ep_queue --> %d\n", value);
2076 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302077 if (value != -ESHUTDOWN)
2078 composite_setup_complete(
2079 gadget->ep0, req);
William Wu7e14f47a2016-05-13 18:30:42 +08002080 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002081 }
2082 return value;
2083 }
2084
David Brownell40982be2008-06-19 17:52:58 -07002085 VDBG(cdev,
2086 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2087 ctrl->bRequestType, ctrl->bRequest,
2088 w_value, w_index, w_length);
2089
Laurent Pinchart52426582009-10-21 00:03:38 +02002090 /* functions always handle their interfaces and endpoints...
2091 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07002092 * configuration code.
David Brownell40982be2008-06-19 17:52:58 -07002093 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302094 if (cdev->config) {
2095 list_for_each_entry(f, &cdev->config->functions, list)
Felix Hädicke1a00b452016-06-22 01:12:08 +02002096 if (f->req_match &&
2097 f->req_match(f, ctrl, false))
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302098 goto try_fun_setup;
Felix Hädicke1a00b452016-06-22 01:12:08 +02002099 } else {
2100 struct usb_configuration *c;
2101 list_for_each_entry(c, &cdev->configs, list)
2102 list_for_each_entry(f, &c->functions, list)
2103 if (f->req_match &&
2104 f->req_match(f, ctrl, true))
2105 goto try_fun_setup;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302106 }
Felix Hädicke1a00b452016-06-22 01:12:08 +02002107 f = NULL;
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05302108
Laurent Pinchart52426582009-10-21 00:03:38 +02002109 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2110 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09002111 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05302112 break;
2113 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02002114 break;
2115
2116 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08002117 if (!cdev->config)
2118 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02002119 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2120 list_for_each_entry(f, &cdev->config->functions, list) {
2121 if (test_bit(endp, f->endpoints))
2122 break;
2123 }
2124 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07002125 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02002126 break;
David Brownell40982be2008-06-19 17:52:58 -07002127 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01002128try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02002129 if (f && f->setup)
2130 value = f->setup(f, ctrl);
2131 else {
David Brownell40982be2008-06-19 17:52:58 -07002132 struct usb_configuration *c;
2133
2134 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002135 if (!c)
2136 goto done;
2137
2138 /* try current config's setup */
2139 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07002140 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01002141 goto done;
2142 }
2143
2144 /* try the only function in the current config */
2145 if (!list_is_singular(&c->functions))
2146 goto done;
2147 f = list_first_entry(&c->functions, struct usb_function,
2148 list);
2149 if (f->setup)
2150 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07002151 }
Vijayavardhan Vennapusa8edd67762013-10-17 15:00:02 +05302152 if (value == USB_GADGET_DELAYED_STATUS) {
2153 DBG(cdev,
2154 "%s: interface %d (%s) requested delayed status\n",
2155 __func__, intf, f->name);
2156 cdev->delayed_status++;
2157 DBG(cdev, "delayed_status count %d\n",
2158 cdev->delayed_status);
2159 }
David Brownell40982be2008-06-19 17:52:58 -07002160
2161 goto done;
2162 }
2163
2164 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03002165 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07002166 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05002167 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07002168 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002169 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07002170 if (value < 0) {
2171 DBG(cdev, "ep_queue --> %d\n", value);
2172 req->status = 0;
Vijayavardhan Vennapusaf860e7452017-03-02 16:07:13 +05302173 if (value != -ESHUTDOWN)
2174 composite_setup_complete(gadget->ep0, req);
David Brownell40982be2008-06-19 17:52:58 -07002175 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03002176 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2177 WARN(cdev,
2178 "%s: Delayed status not supported for w_length != 0",
2179 __func__);
David Brownell40982be2008-06-19 17:52:58 -07002180 }
2181
2182done:
2183 /* device either stalls (value < 0) or reports success */
2184 return value;
2185}
2186
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01002187void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002188{
2189 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2190 unsigned long flags;
2191
Badhri Jagan Sridharan9e954092015-05-06 13:40:15 -07002192 if (cdev == NULL) {
2193 WARN(1, "%s: Calling disconnect on a Gadget that is \
2194 not connected\n", __func__);
2195 return;
2196 }
2197
David Brownell40982be2008-06-19 17:52:58 -07002198 /* REVISIT: should we have config and device level
2199 * disconnect callbacks?
2200 */
2201 spin_lock_irqsave(&cdev->lock, flags);
Benjamin Herrenschmidtd0237652019-07-26 14:59:03 +10002202 cdev->suspended = 0;
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302203 if (cdev->config) {
2204 if (gadget->is_chipidea && !cdev->suspended) {
2205 spin_unlock_irqrestore(&cdev->lock, flags);
2206 msm_do_bam_disable_enable(CI_CTRL);
2207 spin_lock_irqsave(&cdev->lock, flags);
2208 }
David Brownell40982be2008-06-19 17:52:58 -07002209 reset_config(cdev);
Vijayavardhan Vennapusa2f66a262016-07-08 11:29:52 +05302210 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002211 if (cdev->driver->disconnect)
2212 cdev->driver->disconnect(cdev);
Pavankumar Kondeti47870672013-06-19 09:51:58 +05302213 if (cdev->delayed_status != 0) {
2214 INFO(cdev, "delayed status mismatch..resetting\n");
2215 cdev->delayed_status = 0;
2216 }
David Brownell40982be2008-06-19 17:52:58 -07002217 spin_unlock_irqrestore(&cdev->lock, flags);
2218}
2219
2220/*-------------------------------------------------------------------------*/
2221
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002222static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2223 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002224{
2225 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2226 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2227
Vijayavardhan Vennapusa5d2ac1a2017-09-13 15:41:20 +05302228 return snprintf(buf, PAGE_SIZE, "%d\n", cdev->suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002229}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002230static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002231
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002232static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002233{
2234 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002235 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2236 struct usb_string *dev_str = gstr->strings;
David Brownell40982be2008-06-19 17:52:58 -07002237
2238 /* composite_disconnect() must already have been called
2239 * by the underlying peripheral controller driver!
2240 * so there's no i/o concurrency that could affect the
2241 * state protected by cdev->lock.
2242 */
2243 WARN_ON(cdev->config);
2244
2245 while (!list_empty(&cdev->configs)) {
2246 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002247 c = list_first_entry(&cdev->configs,
2248 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002249 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002250 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002251 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002252 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002253
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002254 composite_dev_cleanup(cdev);
2255
Andrew Gabbasov3941ee22017-09-30 08:55:55 -07002256 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2257 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2258
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002259 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002260 kfree(cdev);
2261 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002262}
2263
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002264static void composite_unbind(struct usb_gadget *gadget)
2265{
2266 __composite_unbind(gadget, true);
2267}
2268
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002269static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2270 const struct usb_device_descriptor *old)
2271{
2272 __le16 idVendor;
2273 __le16 idProduct;
2274 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002275 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002276 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002277 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002278
2279 /*
2280 * these variables may have been set in
2281 * usb_composite_overwrite_options()
2282 */
2283 idVendor = new->idVendor;
2284 idProduct = new->idProduct;
2285 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002286 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002287 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002288 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002289
2290 *new = *old;
2291 if (idVendor)
2292 new->idVendor = idVendor;
2293 if (idProduct)
2294 new->idProduct = idProduct;
2295 if (bcdDevice)
2296 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002297 else
2298 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002299 if (iSerialNumber)
2300 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002301 if (iManufacturer)
2302 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002303 if (iProduct)
2304 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002305}
2306
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002307int composite_dev_prepare(struct usb_composite_driver *composite,
2308 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002309{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002310 struct usb_gadget *gadget = cdev->gadget;
2311 int ret = -ENOMEM;
2312
2313 /* preallocate control response and buffer */
2314 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2315 if (!cdev->req)
2316 return -ENOMEM;
2317
Sujeet Kumara6c0e992015-07-14 11:41:55 +05302318 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ +
2319 (gadget->extra_buf_alloc), GFP_KERNEL);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002320 if (!cdev->req->buf)
2321 goto fail;
2322
2323 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2324 if (ret)
2325 goto fail_dev;
2326
2327 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002328 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002329 gadget->ep0->driver_data = cdev;
2330
2331 cdev->driver = composite;
2332
2333 /*
2334 * As per USB compliance update, a device that is actively drawing
2335 * more than 100mA from USB must report itself as bus-powered in
2336 * the GetStatus(DEVICE) call.
2337 */
2338 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2339 usb_gadget_set_selfpowered(gadget);
2340
2341 /* interface and string IDs start at zero via kzalloc.
2342 * we force endpoints to start unassigned; few controller
2343 * drivers will zero ep->driver_data.
2344 */
2345 usb_ep_autoconfig_reset(gadget);
2346 return 0;
2347fail_dev:
2348 kfree(cdev->req->buf);
2349fail:
2350 usb_ep_free_request(gadget->ep0, cdev->req);
2351 cdev->req = NULL;
2352 return ret;
2353}
2354
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002355int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2356 struct usb_ep *ep0)
2357{
2358 int ret = 0;
2359
2360 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2361 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002362 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002363 goto end;
2364 }
2365
Chris Dickensbf54f312017-12-31 18:59:42 -08002366 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2367 GFP_KERNEL);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002368 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002369 ret = -ENOMEM;
Christophe JAILLETf0ee2032017-01-04 06:30:16 +01002370 usb_ep_free_request(ep0, cdev->os_desc_req);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002371 goto end;
2372 }
Felipe Balbi57943712014-09-18 09:54:54 -05002373 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002374 cdev->os_desc_req->complete = composite_setup_complete;
2375end:
2376 return ret;
2377}
2378
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002379void composite_dev_cleanup(struct usb_composite_dev *cdev)
2380{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002381 struct usb_gadget_string_container *uc, *tmp;
2382
2383 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2384 list_del(&uc->list);
2385 kfree(uc);
2386 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002387 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002388 if (cdev->os_desc_pending)
2389 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2390
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002391 kfree(cdev->os_desc_req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002392 cdev->os_desc_req->buf = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002393 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002394 cdev->os_desc_req = NULL;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002395 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002396 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002397 if (cdev->setup_pending)
2398 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2399
Li Junbe0a8882014-08-28 21:44:11 +08002400 kfree(cdev->req->buf);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002401 cdev->req->buf = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002402 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
Hemant Kumarde9c2222016-05-04 18:22:14 -07002403 cdev->req = NULL;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002404 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002405 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002406 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002407}
2408
2409static int composite_bind(struct usb_gadget *gadget,
2410 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002411{
2412 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002413 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002414 int status = -ENOMEM;
2415
2416 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2417 if (!cdev)
2418 return status;
2419
2420 spin_lock_init(&cdev->lock);
2421 cdev->gadget = gadget;
2422 set_gadget_data(gadget, cdev);
2423 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002424 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002425
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002426 status = composite_dev_prepare(composite, cdev);
2427 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002428 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002429
2430 /* composite gadget needs to assign strings for whole device (like
2431 * serial number), register function drivers, potentially update
2432 * power state and consumption, etc
2433 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002434 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002435 if (status < 0)
2436 goto fail;
2437
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002438 if (cdev->use_os_string) {
2439 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2440 if (status)
2441 goto fail;
2442 }
2443
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002444 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002445
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002446 /* has userspace failed to provide a serial number? */
2447 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2448 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2449
David Brownell40982be2008-06-19 17:52:58 -07002450 INFO(cdev, "%s ready\n", composite->name);
2451 return 0;
2452
2453fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002454 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002455 return status;
2456}
2457
2458/*-------------------------------------------------------------------------*/
2459
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002460void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002461{
2462 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2463 struct usb_function *f;
Mayank Rana13e5c152015-10-05 19:08:47 -07002464 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002465
David Brownell89429392009-03-19 14:14:17 -07002466 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002467 * suspend/resume callbacks?
2468 */
2469 DBG(cdev, "suspend\n");
Mayank Rana13e5c152015-10-05 19:08:47 -07002470 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002471 if (cdev->config) {
2472 list_for_each_entry(f, &cdev->config->functions, list) {
2473 if (f->suspend)
2474 f->suspend(f);
2475 }
2476 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002477 if (cdev->driver->suspend)
2478 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002479
2480 cdev->suspended = 1;
Mayank Rana13e5c152015-10-05 19:08:47 -07002481 spin_unlock_irqrestore(&cdev->lock, flags);
Hao Wub23f2f92010-11-29 15:17:03 +08002482
2483 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002484}
2485
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002486void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002487{
2488 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2489 struct usb_function *f;
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302490 int ret;
2491 unsigned long flags;
David Brownell40982be2008-06-19 17:52:58 -07002492
David Brownell89429392009-03-19 14:14:17 -07002493 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002494 * suspend/resume callbacks?
2495 */
2496 DBG(cdev, "resume\n");
Aniket Randive0a78a292020-03-18 11:22:09 +05302497 place_marker("M - USB device is resumed");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002498 if (cdev->driver->resume)
2499 cdev->driver->resume(cdev);
Danny Segalde7cd8d2014-07-28 18:08:33 +03002500
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302501 spin_lock_irqsave(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07002502 if (cdev->config) {
2503 list_for_each_entry(f, &cdev->config->functions, list) {
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302504 if (f->func_wakeup_pending) {
2505 ret = usb_func_wakeup_int(f);
2506 if (ret) {
2507 if (ret == -EAGAIN) {
2508 ERROR(f->config->cdev,
2509 "Function wakeup for %s could not complete due to suspend state.\n",
2510 f->name ? f->name : "");
2511 } else if (ret != -ENOTSUPP) {
2512 ERROR(f->config->cdev,
2513 "Failed to wake function %s from suspend state. ret=%d. Canceling USB request.\n",
2514 f->name ? f->name : "",
2515 ret);
2516 }
Danny Segalde7cd8d2014-07-28 18:08:33 +03002517 }
Sriharsha Allenkif857a142017-11-16 18:53:37 +05302518 f->func_wakeup_pending = 0;
Danny Segal86cb50e2014-07-09 15:14:49 +03002519 }
2520
Sriharsha Allenki6ab82052018-09-24 18:21:44 +05302521 /*
2522 * Call function resume irrespective of the speed.
2523 * Individual function needs to retain the USB3 Function
2524 * suspend state through out the Device suspend entry
2525 * and exit process.
2526 */
2527 if (f->resume)
David Brownell40982be2008-06-19 17:52:58 -07002528 f->resume(f);
2529 }
Hao Wub23f2f92010-11-29 15:17:03 +08002530
Mayank Rana4be36022016-12-19 15:03:00 -08002531 usb_gadget_vbus_draw(gadget, USB_VBUS_DRAW(gadget->speed));
David Brownell40982be2008-06-19 17:52:58 -07002532 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002533
Vijayavardhan Vennapusa73beaea2015-02-25 10:56:20 +05302534 spin_unlock_irqrestore(&cdev->lock, flags);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002535 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002536}
2537
2538/*-------------------------------------------------------------------------*/
2539
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002540static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002541 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002542 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002543
2544 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002545 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002546 .disconnect = composite_disconnect,
2547
2548 .suspend = composite_suspend,
2549 .resume = composite_resume,
2550
2551 .driver = {
2552 .owner = THIS_MODULE,
2553 },
2554};
2555
2556/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002557 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002558 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002559 *
David Brownell40982be2008-06-19 17:52:58 -07002560 * Context: single threaded during gadget setup
2561 *
2562 * This function is used to register drivers using the composite driver
2563 * framework. The return value is zero, or a negative errno value.
2564 * Those values normally come from the driver's @bind method, which does
2565 * all the work of setting up the driver to match the hardware.
2566 *
2567 * On successful return, the gadget is ready to respond to requests from
2568 * the host, unless one of its components invokes usb_gadget_disconnect()
2569 * while it was binding. That would usually be done in order to wait for
2570 * some userspace participation.
2571 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002572int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002573{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002574 struct usb_gadget_driver *gadget_driver;
2575
2576 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002577 return -EINVAL;
2578
2579 if (!driver->name)
2580 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002581
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002582 driver->gadget_driver = composite_driver_template;
2583 gadget_driver = &driver->gadget_driver;
2584
2585 gadget_driver->function = (char *) driver->name;
2586 gadget_driver->driver.name = driver->name;
2587 gadget_driver->max_speed = driver->max_speed;
2588
2589 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002590}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002591EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002592
2593/**
2594 * usb_composite_unregister() - unregister a composite driver
2595 * @driver: the driver to unregister
2596 *
2597 * This function is used to unregister drivers using the composite
2598 * driver framework.
2599 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002600void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002601{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002602 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002603}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002604EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002605
2606/**
2607 * usb_composite_setup_continue() - Continue with the control transfer
2608 * @cdev: the composite device who's control transfer was kept waiting
2609 *
2610 * This function must be called by the USB function driver to continue
2611 * with the control transfer's data/status stage in case it had requested to
2612 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2613 * can request the composite framework to delay the setup request's data/status
2614 * stages by returning USB_GADGET_DELAYED_STATUS.
2615 */
2616void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2617{
2618 int value;
2619 struct usb_request *req = cdev->req;
2620 unsigned long flags;
2621
2622 DBG(cdev, "%s\n", __func__);
2623 spin_lock_irqsave(&cdev->lock, flags);
2624
2625 if (cdev->delayed_status == 0) {
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302626 if (!cdev->config) {
2627 spin_unlock_irqrestore(&cdev->lock, flags);
2628 return;
2629 }
2630 spin_unlock_irqrestore(&cdev->lock, flags);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002631 WARN(cdev, "%s: Unexpected call\n", __func__);
Chandana Kishori Chiluverua4d7fc82017-10-31 11:33:23 +05302632 return;
Roger Quadros1b9ba002011-05-09 13:08:06 +03002633
2634 } else if (--cdev->delayed_status == 0) {
2635 DBG(cdev, "%s: Completing delayed status\n", __func__);
2636 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002637 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002638 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002639 if (value < 0) {
2640 DBG(cdev, "ep_queue --> %d\n", value);
2641 req->status = 0;
2642 composite_setup_complete(cdev->gadget->ep0, req);
2643 }
2644 }
2645
2646 spin_unlock_irqrestore(&cdev->lock, flags);
2647}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002648EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002649
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002650static char *composite_default_mfr(struct usb_gadget *gadget)
2651{
2652 char *mfr;
2653 int len;
2654
2655 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2656 init_utsname()->release, gadget->name);
2657 len++;
2658 mfr = kmalloc(len, GFP_KERNEL);
2659 if (!mfr)
2660 return NULL;
2661 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2662 init_utsname()->release, gadget->name);
2663 return mfr;
2664}
2665
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002666void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2667 struct usb_composite_overwrite *covr)
2668{
2669 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002670 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2671 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002672
2673 if (covr->idVendor)
2674 desc->idVendor = cpu_to_le16(covr->idVendor);
2675
2676 if (covr->idProduct)
2677 desc->idProduct = cpu_to_le16(covr->idProduct);
2678
2679 if (covr->bcdDevice)
2680 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002681
2682 if (covr->serial_number) {
2683 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2684 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2685 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002686 if (covr->manufacturer) {
2687 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2688 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002689
2690 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2691 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2692 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2693 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002694 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002695
2696 if (covr->product) {
2697 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2698 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2699 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002700}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002701EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002702
2703MODULE_LICENSE("GPL");
2704MODULE_AUTHOR("David Brownell");