blob: 5ebe6af7976ec4189363651dfe505cf6ec79fb12 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070020
21#include <linux/usb/composite.h>
Macpaul Lin53e62422015-07-09 15:18:42 +080022#include <linux/usb/otg.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030023#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070024
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +020025#include "u_os_desc.h"
26
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020027/**
28 * struct usb_os_string - represents OS String to be reported by a gadget
29 * @bLength: total length of the entire descritor, always 0x12
30 * @bDescriptorType: USB_DT_STRING
31 * @qwSignature: the OS String proper
32 * @bMS_VendorCode: code used by the host for subsequent requests
33 * @bPad: not used, must be zero
34 */
35struct usb_os_string {
36 __u8 bLength;
37 __u8 bDescriptorType;
38 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
39 __u8 bMS_VendorCode;
40 __u8 bPad;
41} __packed;
42
David Brownell40982be2008-06-19 17:52:58 -070043/*
44 * The code in this file is utility code, used to build a gadget driver
45 * from one or more "function" drivers, one or more "configuration"
46 * objects, and a "usb_composite_driver" by gluing them together along
47 * with the relevant device-wide data.
48 */
49
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010050static struct usb_gadget_strings **get_containers_gs(
51 struct usb_gadget_string_container *uc)
52{
53 return (struct usb_gadget_strings **)uc->stash;
54}
55
Tatyana Brokhman48767a42011-06-28 16:33:49 +030056/**
John Younf3bdbe32016-02-05 17:07:03 -080057 * function_descriptors() - get function descriptors for speed
58 * @f: the function
59 * @speed: the speed
60 *
61 * Returns the descriptors or NULL if not set.
62 */
63static struct usb_descriptor_header **
64function_descriptors(struct usb_function *f,
65 enum usb_device_speed speed)
66{
67 struct usb_descriptor_header **descriptors;
68
Felipe Balbi08782632016-04-22 14:53:47 +030069 /*
70 * NOTE: we try to help gadget drivers which might not be setting
71 * max_speed appropriately.
72 */
73
John Younf3bdbe32016-02-05 17:07:03 -080074 switch (speed) {
75 case USB_SPEED_SUPER_PLUS:
76 descriptors = f->ssp_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030077 if (descriptors)
78 break;
79 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080080 case USB_SPEED_SUPER:
81 descriptors = f->ss_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030082 if (descriptors)
83 break;
84 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080085 case USB_SPEED_HIGH:
86 descriptors = f->hs_descriptors;
Felipe Balbi08782632016-04-22 14:53:47 +030087 if (descriptors)
88 break;
89 /* FALLTHROUGH */
John Younf3bdbe32016-02-05 17:07:03 -080090 default:
91 descriptors = f->fs_descriptors;
92 }
93
Felipe Balbi08782632016-04-22 14:53:47 +030094 /*
95 * if we can't find any descriptors at all, then this gadget deserves to
96 * Oops with a NULL pointer dereference
97 */
98
John Younf3bdbe32016-02-05 17:07:03 -080099 return descriptors;
100}
101
102/**
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300103 * next_ep_desc() - advance to the next EP descriptor
104 * @t: currect pointer within descriptor array
105 *
106 * Return: next EP descriptor or NULL
107 *
108 * Iterate over @t until either EP descriptor found or
109 * NULL (that indicates end of list) encountered
110 */
111static struct usb_descriptor_header**
112next_ep_desc(struct usb_descriptor_header **t)
113{
114 for (; *t; t++) {
115 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
116 return t;
117 }
118 return NULL;
119}
120
121/*
122 * for_each_ep_desc()- iterate over endpoint descriptors in the
123 * descriptors list
124 * @start: pointer within descriptor array.
125 * @ep_desc: endpoint descriptor to use as the loop cursor
126 */
127#define for_each_ep_desc(start, ep_desc) \
128 for (ep_desc = next_ep_desc(start); \
129 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
130
131/**
132 * config_ep_by_speed() - configures the given endpoint
133 * according to gadget speed.
134 * @g: pointer to the gadget
135 * @f: usb function
136 * @_ep: the endpoint to configure
137 *
138 * Return: error code, 0 on success
139 *
140 * This function chooses the right descriptors for a given
141 * endpoint according to gadget speed and saves it in the
142 * endpoint desc field. If the endpoint already has a descriptor
143 * assigned to it - overwrites it with currently corresponding
144 * descriptor. The endpoint maxpacket field is updated according
145 * to the chosen descriptor.
146 * Note: the supplied function should hold all the descriptors
147 * for supported speeds
148 */
149int config_ep_by_speed(struct usb_gadget *g,
150 struct usb_function *f,
151 struct usb_ep *_ep)
152{
Felipe Balbib785ea72012-06-06 10:20:23 +0300153 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300154 struct usb_endpoint_descriptor *chosen_desc = NULL;
155 struct usb_descriptor_header **speed_desc = NULL;
156
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300157 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
158 int want_comp_desc = 0;
159
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300160 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
161
162 if (!g || !f || !_ep)
163 return -EIO;
164
165 /* select desired speed */
166 switch (g->speed) {
John Youn4eb8e32d2016-02-05 17:07:30 -0800167 case USB_SPEED_SUPER_PLUS:
168 if (gadget_is_superspeed_plus(g)) {
169 speed_desc = f->ssp_descriptors;
170 want_comp_desc = 1;
171 break;
172 }
173 /* else: Fall trough */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300174 case USB_SPEED_SUPER:
175 if (gadget_is_superspeed(g)) {
176 speed_desc = f->ss_descriptors;
177 want_comp_desc = 1;
178 break;
179 }
180 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300181 case USB_SPEED_HIGH:
182 if (gadget_is_dualspeed(g)) {
183 speed_desc = f->hs_descriptors;
184 break;
185 }
186 /* else: fall through */
187 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200188 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300189 }
190 /* find descriptors */
191 for_each_ep_desc(speed_desc, d_spd) {
192 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
193 if (chosen_desc->bEndpointAddress == _ep->address)
194 goto ep_found;
195 }
196 return -EIO;
197
198ep_found:
199 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700200 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300201 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300202 _ep->comp_desc = NULL;
203 _ep->maxburst = 0;
204 _ep->mult = 0;
205 if (!want_comp_desc)
206 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300207
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300208 /*
209 * Companion descriptor should follow EP descriptor
210 * USB 3.0 spec, #9.6.7
211 */
212 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
213 if (!comp_desc ||
214 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
215 return -EIO;
216 _ep->comp_desc = comp_desc;
John Youn4eb8e32d2016-02-05 17:07:30 -0800217 if (g->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300218 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300219 case USB_ENDPOINT_XFER_ISOC:
220 /* mult: bits 1:0 of bmAttributes */
221 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800222 case USB_ENDPOINT_XFER_BULK:
223 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300224 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300225 break;
226 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300227 if (comp_desc->bMaxBurst != 0)
228 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
229 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300230 break;
231 }
232 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300233 return 0;
234}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200235EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700236
237/**
238 * usb_add_function() - add a function to a configuration
239 * @config: the configuration
240 * @function: the function being added
241 * Context: single threaded during gadget setup
242 *
243 * After initialization, each configuration must have one or more
244 * functions added to it. Adding a function involves calling its @bind()
245 * method to allocate resources such as interface and string identifiers
246 * and endpoints.
247 *
248 * This function returns the value of the function's bind(), which is
249 * zero for success else a negative errno value.
250 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200251int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700252 struct usb_function *function)
253{
254 int value = -EINVAL;
255
256 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
257 function->name, function,
258 config->label, config);
259
260 if (!function->set_alt || !function->disable)
261 goto done;
262
263 function->config = config;
264 list_add_tail(&function->list, &config->functions);
265
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200266 if (function->bind_deactivated) {
267 value = usb_function_deactivate(function);
268 if (value)
269 goto done;
270 }
271
David Brownell40982be2008-06-19 17:52:58 -0700272 /* REVISIT *require* function->bind? */
273 if (function->bind) {
274 value = function->bind(config, function);
275 if (value < 0) {
276 list_del(&function->list);
277 function->config = NULL;
278 }
279 } else
280 value = 0;
281
282 /* We allow configurations that don't work at both speeds.
283 * If we run into a lowspeed Linux system, treat it the same
284 * as full speed ... it's the function drivers that will need
285 * to avoid bulk and ISO transfers.
286 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200287 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700288 config->fullspeed = true;
289 if (!config->highspeed && function->hs_descriptors)
290 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300291 if (!config->superspeed && function->ss_descriptors)
292 config->superspeed = true;
John Youn554eead2016-02-05 17:06:35 -0800293 if (!config->superspeed_plus && function->ssp_descriptors)
294 config->superspeed_plus = true;
David Brownell40982be2008-06-19 17:52:58 -0700295
296done:
297 if (value)
298 DBG(config->cdev, "adding '%s'/%p --> %d\n",
299 function->name, function, value);
300 return value;
301}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200302EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700303
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100304void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
305{
306 if (f->disable)
307 f->disable(f);
308
309 bitmap_zero(f->endpoints, 32);
310 list_del(&f->list);
311 if (f->unbind)
312 f->unbind(c, f);
313}
314EXPORT_SYMBOL_GPL(usb_remove_function);
315
David Brownell40982be2008-06-19 17:52:58 -0700316/**
David Brownell60beed92008-08-18 17:38:22 -0700317 * usb_function_deactivate - prevent function and gadget enumeration
318 * @function: the function that isn't yet ready to respond
319 *
320 * Blocks response of the gadget driver to host enumeration by
321 * preventing the data line pullup from being activated. This is
322 * normally called during @bind() processing to change from the
323 * initial "ready to respond" state, or when a required resource
324 * becomes available.
325 *
326 * For example, drivers that serve as a passthrough to a userspace
327 * daemon can block enumeration unless that daemon (such as an OBEX,
328 * MTP, or print server) is ready to handle host requests.
329 *
330 * Not all systems support software control of their USB peripheral
331 * data pullups.
332 *
333 * Returns zero on success, else negative errno.
334 */
335int usb_function_deactivate(struct usb_function *function)
336{
337 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200338 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700339 int status = 0;
340
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200341 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700342
343 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200344 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700345 if (status == 0)
346 cdev->deactivations++;
347
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200348 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700349 return status;
350}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200351EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700352
353/**
354 * usb_function_activate - allow function and gadget enumeration
355 * @function: function on which usb_function_activate() was called
356 *
357 * Reverses effect of usb_function_deactivate(). If no more functions
358 * are delaying their activation, the gadget driver will respond to
359 * host enumeration procedures.
360 *
361 * Returns zero on success, else negative errno.
362 */
363int usb_function_activate(struct usb_function *function)
364{
365 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200366 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700367 int status = 0;
368
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200369 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700370
371 if (WARN_ON(cdev->deactivations == 0))
372 status = -EINVAL;
373 else {
374 cdev->deactivations--;
375 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200376 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700377 }
378
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +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_activate);
David Brownell60beed92008-08-18 17:38:22 -0700383
384/**
David Brownell40982be2008-06-19 17:52:58 -0700385 * usb_interface_id() - allocate an unused interface ID
386 * @config: configuration associated with the interface
387 * @function: function handling the interface
388 * Context: single threaded during gadget setup
389 *
390 * usb_interface_id() is called from usb_function.bind() callbacks to
391 * allocate new interface IDs. The function driver will then store that
392 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300393 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700394 * particularly changing its altsetting via set_alt(). There may
395 * also be class-specific or vendor-specific requests to handle.
396 *
397 * All interface identifier should be allocated using this routine, to
398 * ensure that for example different functions don't wrongly assign
399 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300400 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700401 * one configuration (or more than once in a given configuration) need
402 * multiple versions of the relevant descriptors.
403 *
404 * Returns the interface ID which was allocated; or -ENODEV if no
405 * more interface IDs can be allocated.
406 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200407int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700408 struct usb_function *function)
409{
410 unsigned id = config->next_interface_id;
411
412 if (id < MAX_CONFIG_INTERFACES) {
413 config->interface[id] = function;
414 config->next_interface_id = id + 1;
415 return id;
416 }
417 return -ENODEV;
418}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200419EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700420
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100421static u8 encode_bMaxPower(enum usb_device_speed speed,
422 struct usb_configuration *c)
423{
424 unsigned val;
425
426 if (c->MaxPower)
427 val = c->MaxPower;
428 else
429 val = CONFIG_USB_GADGET_VBUS_DRAW;
430 if (!val)
431 return 0;
432 switch (speed) {
433 case USB_SPEED_SUPER:
434 return DIV_ROUND_UP(val, 8);
435 default:
436 return DIV_ROUND_UP(val, 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700437 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100438}
439
David Brownell40982be2008-06-19 17:52:58 -0700440static int config_buf(struct usb_configuration *config,
441 enum usb_device_speed speed, void *buf, u8 type)
442{
443 struct usb_config_descriptor *c = buf;
444 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200445 int len;
David Brownell40982be2008-06-19 17:52:58 -0700446 struct usb_function *f;
447 int status;
448
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200449 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700450 /* write the config descriptor */
451 c = buf;
452 c->bLength = USB_DT_CONFIG_SIZE;
453 c->bDescriptorType = type;
454 /* wTotalLength is written later */
455 c->bNumInterfaces = config->next_interface_id;
456 c->bConfigurationValue = config->bConfigurationValue;
457 c->iConfiguration = config->iConfiguration;
458 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100459 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700460
461 /* There may be e.g. OTG descriptors */
462 if (config->descriptors) {
463 status = usb_descriptor_fillbuf(next, len,
464 config->descriptors);
465 if (status < 0)
466 return status;
467 len -= status;
468 next += status;
469 }
470
471 /* add each function's descriptors */
472 list_for_each_entry(f, &config->functions, list) {
473 struct usb_descriptor_header **descriptors;
474
John Younf3bdbe32016-02-05 17:07:03 -0800475 descriptors = function_descriptors(f, speed);
David Brownell40982be2008-06-19 17:52:58 -0700476 if (!descriptors)
477 continue;
478 status = usb_descriptor_fillbuf(next, len,
479 (const struct usb_descriptor_header **) descriptors);
480 if (status < 0)
481 return status;
482 len -= status;
483 next += status;
484 }
485
486 len = next - buf;
487 c->wTotalLength = cpu_to_le16(len);
488 return len;
489}
490
491static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
492{
493 struct usb_gadget *gadget = cdev->gadget;
494 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200495 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700496 u8 type = w_value >> 8;
497 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
498
John Youneae58202016-02-05 17:07:17 -0800499 if (gadget->speed >= USB_SPEED_SUPER)
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300500 speed = gadget->speed;
501 else if (gadget_is_dualspeed(gadget)) {
502 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700503 if (gadget->speed == USB_SPEED_HIGH)
504 hs = 1;
505 if (type == USB_DT_OTHER_SPEED_CONFIG)
506 hs = !hs;
507 if (hs)
508 speed = USB_SPEED_HIGH;
509
510 }
511
512 /* This is a lookup by config *INDEX* */
513 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200514
515 pos = &cdev->configs;
516 c = cdev->os_desc_config;
517 if (c)
518 goto check_config;
519
520 while ((pos = pos->next) != &cdev->configs) {
521 c = list_entry(pos, typeof(*c), list);
522
523 /* skip OS Descriptors config which is handled separately */
524 if (c == cdev->os_desc_config)
525 continue;
526
527check_config:
David Brownell40982be2008-06-19 17:52:58 -0700528 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300529 switch (speed) {
John Youneae58202016-02-05 17:07:17 -0800530 case USB_SPEED_SUPER_PLUS:
531 if (!c->superspeed_plus)
532 continue;
533 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300534 case USB_SPEED_SUPER:
535 if (!c->superspeed)
536 continue;
537 break;
538 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700539 if (!c->highspeed)
540 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300541 break;
542 default:
David Brownell40982be2008-06-19 17:52:58 -0700543 if (!c->fullspeed)
544 continue;
545 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300546
David Brownell40982be2008-06-19 17:52:58 -0700547 if (w_value == 0)
548 return config_buf(c, speed, cdev->req->buf, type);
549 w_value--;
550 }
551 return -EINVAL;
552}
553
554static int count_configs(struct usb_composite_dev *cdev, unsigned type)
555{
556 struct usb_gadget *gadget = cdev->gadget;
557 struct usb_configuration *c;
558 unsigned count = 0;
559 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300560 int ss = 0;
John Youna4afd012016-02-05 17:06:49 -0800561 int ssp = 0;
David Brownell40982be2008-06-19 17:52:58 -0700562
563 if (gadget_is_dualspeed(gadget)) {
564 if (gadget->speed == USB_SPEED_HIGH)
565 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300566 if (gadget->speed == USB_SPEED_SUPER)
567 ss = 1;
John Youna4afd012016-02-05 17:06:49 -0800568 if (gadget->speed == USB_SPEED_SUPER_PLUS)
569 ssp = 1;
David Brownell40982be2008-06-19 17:52:58 -0700570 if (type == USB_DT_DEVICE_QUALIFIER)
571 hs = !hs;
572 }
573 list_for_each_entry(c, &cdev->configs, list) {
574 /* ignore configs that won't work at this speed */
John Youna4afd012016-02-05 17:06:49 -0800575 if (ssp) {
576 if (!c->superspeed_plus)
577 continue;
578 } else if (ss) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300579 if (!c->superspeed)
580 continue;
581 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700582 if (!c->highspeed)
583 continue;
584 } else {
585 if (!c->fullspeed)
586 continue;
587 }
588 count++;
589 }
590 return count;
591}
592
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300593/**
594 * bos_desc() - prepares the BOS descriptor.
595 * @cdev: pointer to usb_composite device to generate the bos
596 * descriptor for
597 *
598 * This function generates the BOS (Binary Device Object)
599 * descriptor and its device capabilities descriptors. The BOS
600 * descriptor should be supported by a SuperSpeed device.
601 */
602static int bos_desc(struct usb_composite_dev *cdev)
603{
604 struct usb_ext_cap_descriptor *usb_ext;
605 struct usb_ss_cap_descriptor *ss_cap;
606 struct usb_dcd_config_params dcd_config_params;
607 struct usb_bos_descriptor *bos = cdev->req->buf;
608
609 bos->bLength = USB_DT_BOS_SIZE;
610 bos->bDescriptorType = USB_DT_BOS;
611
612 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
613 bos->bNumDeviceCaps = 0;
614
615 /*
616 * A SuperSpeed device shall include the USB2.0 extension descriptor
617 * and shall support LPM when operating in USB2.0 HS mode.
618 */
619 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
620 bos->bNumDeviceCaps++;
621 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
622 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
623 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
624 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500625 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300626
627 /*
628 * The Superspeed USB Capability descriptor shall be implemented by all
629 * SuperSpeed devices.
630 */
631 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
632 bos->bNumDeviceCaps++;
633 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
634 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
635 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
636 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
637 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
638 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
639 USB_FULL_SPEED_OPERATION |
640 USB_HIGH_SPEED_OPERATION |
641 USB_5GBPS_OPERATION);
642 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
643
644 /* Get Controller configuration */
645 if (cdev->gadget->ops->get_config_params)
646 cdev->gadget->ops->get_config_params(&dcd_config_params);
647 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300648 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300649 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300650 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300651 }
652 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
653 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
654
John Younf228a8d2016-02-05 17:05:53 -0800655 /* The SuperSpeedPlus USB Device Capability descriptor */
656 if (gadget_is_superspeed_plus(cdev->gadget)) {
657 struct usb_ssp_cap_descriptor *ssp_cap;
658
659 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
660 bos->bNumDeviceCaps++;
661
662 /*
663 * Report typical values.
664 */
665
666 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
667 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
668 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
669 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
John Youn138b8632016-04-08 14:46:31 -0700670 ssp_cap->bReserved = 0;
671 ssp_cap->wReserved = 0;
John Younf228a8d2016-02-05 17:05:53 -0800672
673 /* SSAC = 1 (2 attributes) */
674 ssp_cap->bmAttributes = cpu_to_le32(1);
675
676 /* Min RX/TX Lane Count = 1 */
John Youn08f8cab2016-03-28 16:12:24 -0700677 ssp_cap->wFunctionalitySupport =
678 cpu_to_le16((1 << 8) | (1 << 12));
John Younf228a8d2016-02-05 17:05:53 -0800679
680 /*
681 * bmSublinkSpeedAttr[0]:
682 * ST = Symmetric, RX
683 * LSE = 3 (Gbps)
684 * LP = 1 (SuperSpeedPlus)
685 * LSM = 10 (10 Gbps)
686 */
687 ssp_cap->bmSublinkSpeedAttr[0] =
John Youn08f8cab2016-03-28 16:12:24 -0700688 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
John Younf228a8d2016-02-05 17:05:53 -0800689 /*
690 * bmSublinkSpeedAttr[1] =
691 * ST = Symmetric, TX
692 * LSE = 3 (Gbps)
693 * LP = 1 (SuperSpeedPlus)
694 * LSM = 10 (10 Gbps)
695 */
696 ssp_cap->bmSublinkSpeedAttr[1] =
John Youn08f8cab2016-03-28 16:12:24 -0700697 cpu_to_le32((3 << 4) | (1 << 14) |
698 (0xa << 16) | (1 << 7));
John Younf228a8d2016-02-05 17:05:53 -0800699 }
700
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300701 return le16_to_cpu(bos->wTotalLength);
702}
703
David Brownell40982be2008-06-19 17:52:58 -0700704static void device_qual(struct usb_composite_dev *cdev)
705{
706 struct usb_qualifier_descriptor *qual = cdev->req->buf;
707
708 qual->bLength = sizeof(*qual);
709 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
710 /* POLICY: same bcdUSB and device type info at both speeds */
711 qual->bcdUSB = cdev->desc.bcdUSB;
712 qual->bDeviceClass = cdev->desc.bDeviceClass;
713 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
714 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
715 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200716 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700717 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700718 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700719}
720
721/*-------------------------------------------------------------------------*/
722
723static void reset_config(struct usb_composite_dev *cdev)
724{
725 struct usb_function *f;
726
727 DBG(cdev, "reset config\n");
728
729 list_for_each_entry(f, &cdev->config->functions, list) {
730 if (f->disable)
731 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200732
733 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700734 }
735 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100736 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700737}
738
739static int set_config(struct usb_composite_dev *cdev,
740 const struct usb_ctrlrequest *ctrl, unsigned number)
741{
742 struct usb_gadget *gadget = cdev->gadget;
743 struct usb_configuration *c = NULL;
744 int result = -EINVAL;
745 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
746 int tmp;
747
David Brownell40982be2008-06-19 17:52:58 -0700748 if (number) {
749 list_for_each_entry(c, &cdev->configs, list) {
750 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300751 /*
752 * We disable the FDs of the previous
753 * configuration only if the new configuration
754 * is a valid one
755 */
756 if (cdev->config)
757 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700758 result = 0;
759 break;
760 }
761 }
762 if (result < 0)
763 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300764 } else { /* Zero configuration value - need to reset the config */
765 if (cdev->config)
766 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700767 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300768 }
David Brownell40982be2008-06-19 17:52:58 -0700769
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200770 INFO(cdev, "%s config #%d: %s\n",
771 usb_speed_string(gadget->speed),
772 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700773
774 if (!c)
775 goto done;
776
Peter Chen6027f312014-04-29 13:26:28 +0800777 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700778 cdev->config = c;
779
780 /* Initialize all interfaces by setting them to altsetting zero. */
781 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
782 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200783 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700784
785 if (!f)
786 break;
787
Laurent Pinchart52426582009-10-21 00:03:38 +0200788 /*
789 * Record which endpoints are used by the function. This is used
790 * to dispatch control requests targeted at that endpoint to the
791 * function's setup callback instead of the current
792 * configuration's setup callback.
793 */
John Younf3bdbe32016-02-05 17:07:03 -0800794 descriptors = function_descriptors(f, gadget->speed);
Laurent Pinchart52426582009-10-21 00:03:38 +0200795
796 for (; *descriptors; ++descriptors) {
797 struct usb_endpoint_descriptor *ep;
798 int addr;
799
800 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
801 continue;
802
803 ep = (struct usb_endpoint_descriptor *)*descriptors;
804 addr = ((ep->bEndpointAddress & 0x80) >> 3)
805 | (ep->bEndpointAddress & 0x0f);
806 set_bit(addr, f->endpoints);
807 }
808
David Brownell40982be2008-06-19 17:52:58 -0700809 result = f->set_alt(f, tmp, 0);
810 if (result < 0) {
811 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
812 tmp, f->name, f, result);
813
814 reset_config(cdev);
815 goto done;
816 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300817
818 if (result == USB_GADGET_DELAYED_STATUS) {
819 DBG(cdev,
820 "%s: interface %d (%s) requested delayed status\n",
821 __func__, tmp, f->name);
822 cdev->delayed_status++;
823 DBG(cdev, "delayed_status count %d\n",
824 cdev->delayed_status);
825 }
David Brownell40982be2008-06-19 17:52:58 -0700826 }
827
828 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100829 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700830done:
831 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300832 if (result >= 0 && cdev->delayed_status)
833 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700834 return result;
835}
836
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100837int usb_add_config_only(struct usb_composite_dev *cdev,
838 struct usb_configuration *config)
839{
840 struct usb_configuration *c;
841
842 if (!config->bConfigurationValue)
843 return -EINVAL;
844
845 /* Prevent duplicate configuration identifiers */
846 list_for_each_entry(c, &cdev->configs, list) {
847 if (c->bConfigurationValue == config->bConfigurationValue)
848 return -EBUSY;
849 }
850
851 config->cdev = cdev;
852 list_add_tail(&config->list, &cdev->configs);
853
854 INIT_LIST_HEAD(&config->functions);
855 config->next_interface_id = 0;
856 memset(config->interface, 0, sizeof(config->interface));
857
858 return 0;
859}
860EXPORT_SYMBOL_GPL(usb_add_config_only);
861
David Brownell40982be2008-06-19 17:52:58 -0700862/**
863 * usb_add_config() - add a configuration to a device.
864 * @cdev: wraps the USB gadget
865 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200866 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700867 * Context: single threaded during gadget setup
868 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200869 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700870 * add each of the configurations it supports, using this routine.
871 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200872 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700873 * is zero for success else a negative errno value. Binding configurations
874 * assigns global resources including string IDs, and per-configuration
875 * resources such as interface IDs and endpoints.
876 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200877int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200878 struct usb_configuration *config,
879 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700880{
881 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100882
883 if (!bind)
884 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700885
886 DBG(cdev, "adding config #%u '%s'/%p\n",
887 config->bConfigurationValue,
888 config->label, config);
889
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100890 status = usb_add_config_only(cdev, config);
891 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700892 goto done;
893
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200894 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700895 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900896 while (!list_empty(&config->functions)) {
897 struct usb_function *f;
898
899 f = list_first_entry(&config->functions,
900 struct usb_function, list);
901 list_del(&f->list);
902 if (f->unbind) {
903 DBG(cdev, "unbind function '%s'/%p\n",
904 f->name, f);
905 f->unbind(config, f);
906 /* may free memory for "f" */
907 }
908 }
David Brownell40982be2008-06-19 17:52:58 -0700909 list_del(&config->list);
910 config->cdev = NULL;
911 } else {
912 unsigned i;
913
John Youncd69cbe2016-02-05 17:07:44 -0800914 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700915 config->bConfigurationValue, config,
John Youncd69cbe2016-02-05 17:07:44 -0800916 config->superspeed_plus ? " superplus" : "",
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300917 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700918 config->highspeed ? " high" : "",
919 config->fullspeed
920 ? (gadget_is_dualspeed(cdev->gadget)
921 ? " full"
922 : " full/low")
923 : "");
924
925 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
926 struct usb_function *f = config->interface[i];
927
928 if (!f)
929 continue;
930 DBG(cdev, " interface %d = %s/%p\n",
931 i, f->name, f);
932 }
933 }
934
Robert Baldygaf871cb92015-09-16 12:10:39 +0200935 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -0700936 usb_ep_autoconfig_reset(cdev->gadget);
937
938done:
939 if (status)
940 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
941 config->bConfigurationValue, status);
942 return status;
943}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200944EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700945
Benoit Goby51cce6f2012-05-10 10:07:57 +0200946static void remove_config(struct usb_composite_dev *cdev,
947 struct usb_configuration *config)
948{
949 while (!list_empty(&config->functions)) {
950 struct usb_function *f;
951
952 f = list_first_entry(&config->functions,
953 struct usb_function, list);
954 list_del(&f->list);
955 if (f->unbind) {
956 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
957 f->unbind(config, f);
958 /* may free memory for "f" */
959 }
960 }
961 list_del(&config->list);
962 if (config->unbind) {
963 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
964 config->unbind(config);
965 /* may free memory for "c" */
966 }
967}
968
969/**
970 * usb_remove_config() - remove a configuration from a device.
971 * @cdev: wraps the USB gadget
972 * @config: the configuration
973 *
974 * Drivers must call usb_gadget_disconnect before calling this function
975 * to disconnect the device from the host and make sure the host will not
976 * try to enumerate the device while we are changing the config list.
977 */
978void usb_remove_config(struct usb_composite_dev *cdev,
979 struct usb_configuration *config)
980{
981 unsigned long flags;
982
983 spin_lock_irqsave(&cdev->lock, flags);
984
985 if (cdev->config == config)
986 reset_config(cdev);
987
988 spin_unlock_irqrestore(&cdev->lock, flags);
989
990 remove_config(cdev, config);
991}
992
David Brownell40982be2008-06-19 17:52:58 -0700993/*-------------------------------------------------------------------------*/
994
995/* We support strings in multiple languages ... string descriptor zero
996 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -0300997 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -0700998 * the host side.
999 */
1000
1001static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1002{
1003 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +03001004 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -07001005 __le16 *tmp;
1006
1007 while (*sp) {
1008 s = *sp;
1009 language = cpu_to_le16(s->language);
1010 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1011 if (*tmp == language)
1012 goto repeat;
1013 }
1014 *tmp++ = language;
1015repeat:
1016 sp++;
1017 }
1018}
1019
1020static int lookup_string(
1021 struct usb_gadget_strings **sp,
1022 void *buf,
1023 u16 language,
1024 int id
1025)
1026{
1027 struct usb_gadget_strings *s;
1028 int value;
1029
1030 while (*sp) {
1031 s = *sp++;
1032 if (s->language != language)
1033 continue;
1034 value = usb_gadget_get_string(s, id, buf);
1035 if (value > 0)
1036 return value;
1037 }
1038 return -EINVAL;
1039}
1040
1041static int get_string(struct usb_composite_dev *cdev,
1042 void *buf, u16 language, int id)
1043{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001044 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001045 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -07001046 struct usb_configuration *c;
1047 struct usb_function *f;
1048 int len;
1049
Diego Violaad4676a2015-05-31 15:52:41 -03001050 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001051 * folk will ever care about ... also, it's all supported here.
1052 * (Except for UTF8 support for Unicode's "Astral Planes".)
1053 */
1054
1055 /* 0 == report all available language codes */
1056 if (id == 0) {
1057 struct usb_string_descriptor *s = buf;
1058 struct usb_gadget_strings **sp;
1059
1060 memset(s, 0, 256);
1061 s->bDescriptorType = USB_DT_STRING;
1062
1063 sp = composite->strings;
1064 if (sp)
1065 collect_langs(sp, s->wData);
1066
1067 list_for_each_entry(c, &cdev->configs, list) {
1068 sp = c->strings;
1069 if (sp)
1070 collect_langs(sp, s->wData);
1071
1072 list_for_each_entry(f, &c->functions, list) {
1073 sp = f->strings;
1074 if (sp)
1075 collect_langs(sp, s->wData);
1076 }
1077 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001078 list_for_each_entry(uc, &cdev->gstrings, list) {
1079 struct usb_gadget_strings **sp;
1080
1081 sp = get_containers_gs(uc);
1082 collect_langs(sp, s->wData);
1083 }
David Brownell40982be2008-06-19 17:52:58 -07001084
Roel Kluin417b57b2009-08-06 16:09:51 -07001085 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001086 continue;
1087 if (!len)
1088 return -EINVAL;
1089
1090 s->bLength = 2 * (len + 1);
1091 return s->bLength;
1092 }
1093
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001094 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1095 struct usb_os_string *b = buf;
1096 b->bLength = sizeof(*b);
1097 b->bDescriptorType = USB_DT_STRING;
1098 compiletime_assert(
1099 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1100 "qwSignature size must be equal to qw_sign");
1101 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1102 b->bMS_VendorCode = cdev->b_vendor_code;
1103 b->bPad = 0;
1104 return sizeof(*b);
1105 }
1106
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001107 list_for_each_entry(uc, &cdev->gstrings, list) {
1108 struct usb_gadget_strings **sp;
1109
1110 sp = get_containers_gs(uc);
1111 len = lookup_string(sp, buf, language, id);
1112 if (len > 0)
1113 return len;
1114 }
1115
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001116 /* String IDs are device-scoped, so we look up each string
1117 * table we're told about. These lookups are infrequent;
1118 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001119 */
1120 if (composite->strings) {
1121 len = lookup_string(composite->strings, buf, language, id);
1122 if (len > 0)
1123 return len;
1124 }
1125 list_for_each_entry(c, &cdev->configs, list) {
1126 if (c->strings) {
1127 len = lookup_string(c->strings, buf, language, id);
1128 if (len > 0)
1129 return len;
1130 }
1131 list_for_each_entry(f, &c->functions, list) {
1132 if (!f->strings)
1133 continue;
1134 len = lookup_string(f->strings, buf, language, id);
1135 if (len > 0)
1136 return len;
1137 }
1138 }
1139 return -EINVAL;
1140}
1141
1142/**
1143 * usb_string_id() - allocate an unused string ID
1144 * @cdev: the device whose string descriptor IDs are being allocated
1145 * Context: single threaded during gadget setup
1146 *
1147 * @usb_string_id() is called from bind() callbacks to allocate
1148 * string IDs. Drivers for functions, configurations, or gadgets will
1149 * then store that ID in the appropriate descriptors and string table.
1150 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001151 * All string identifier should be allocated using this,
1152 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1153 * that for example different functions don't wrongly assign different
1154 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001155 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001156int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001157{
1158 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001159 /* string id 0 is reserved by USB spec for list of
1160 * supported languages */
1161 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001162 cdev->next_string_id++;
1163 return cdev->next_string_id;
1164 }
1165 return -ENODEV;
1166}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001167EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001168
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001169/**
1170 * usb_string_ids() - allocate unused string IDs in batch
1171 * @cdev: the device whose string descriptor IDs are being allocated
1172 * @str: an array of usb_string objects to assign numbers to
1173 * Context: single threaded during gadget setup
1174 *
1175 * @usb_string_ids() is called from bind() callbacks to allocate
1176 * string IDs. Drivers for functions, configurations, or gadgets will
1177 * then copy IDs from the string table to the appropriate descriptors
1178 * and string table for other languages.
1179 *
1180 * All string identifier should be allocated using this,
1181 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1182 * example different functions don't wrongly assign different meanings
1183 * to the same identifier.
1184 */
1185int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1186{
1187 int next = cdev->next_string_id;
1188
1189 for (; str->s; ++str) {
1190 if (unlikely(next >= 254))
1191 return -ENODEV;
1192 str->id = ++next;
1193 }
1194
1195 cdev->next_string_id = next;
1196
1197 return 0;
1198}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001199EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001200
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001201static struct usb_gadget_string_container *copy_gadget_strings(
1202 struct usb_gadget_strings **sp, unsigned n_gstrings,
1203 unsigned n_strings)
1204{
1205 struct usb_gadget_string_container *uc;
1206 struct usb_gadget_strings **gs_array;
1207 struct usb_gadget_strings *gs;
1208 struct usb_string *s;
1209 unsigned mem;
1210 unsigned n_gs;
1211 unsigned n_s;
1212 void *stash;
1213
1214 mem = sizeof(*uc);
1215 mem += sizeof(void *) * (n_gstrings + 1);
1216 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1217 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1218 uc = kmalloc(mem, GFP_KERNEL);
1219 if (!uc)
1220 return ERR_PTR(-ENOMEM);
1221 gs_array = get_containers_gs(uc);
1222 stash = uc->stash;
1223 stash += sizeof(void *) * (n_gstrings + 1);
1224 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1225 struct usb_string *org_s;
1226
1227 gs_array[n_gs] = stash;
1228 gs = gs_array[n_gs];
1229 stash += sizeof(struct usb_gadget_strings);
1230 gs->language = sp[n_gs]->language;
1231 gs->strings = stash;
1232 org_s = sp[n_gs]->strings;
1233
1234 for (n_s = 0; n_s < n_strings; n_s++) {
1235 s = stash;
1236 stash += sizeof(struct usb_string);
1237 if (org_s->s)
1238 s->s = org_s->s;
1239 else
1240 s->s = "";
1241 org_s++;
1242 }
1243 s = stash;
1244 s->s = NULL;
1245 stash += sizeof(struct usb_string);
1246
1247 }
1248 gs_array[n_gs] = NULL;
1249 return uc;
1250}
1251
1252/**
1253 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1254 * @cdev: the device whose string descriptor IDs are being allocated
1255 * and attached.
1256 * @sp: an array of usb_gadget_strings to attach.
1257 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1258 *
1259 * This function will create a deep copy of usb_gadget_strings and usb_string
1260 * and attach it to the cdev. The actual string (usb_string.s) will not be
1261 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001262 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001263 * The ->language pointer of each struct usb_gadget_strings has to contain the
1264 * same amount of entries.
1265 * 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 +09001266 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001267 * entry of en-US. Therefore both entries become the same id assign.
1268 */
1269struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1270 struct usb_gadget_strings **sp, unsigned n_strings)
1271{
1272 struct usb_gadget_string_container *uc;
1273 struct usb_gadget_strings **n_gs;
1274 unsigned n_gstrings = 0;
1275 unsigned i;
1276 int ret;
1277
1278 for (i = 0; sp[i]; i++)
1279 n_gstrings++;
1280
1281 if (!n_gstrings)
1282 return ERR_PTR(-EINVAL);
1283
1284 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1285 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001286 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001287
1288 n_gs = get_containers_gs(uc);
1289 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1290 if (ret)
1291 goto err;
1292
1293 for (i = 1; i < n_gstrings; i++) {
1294 struct usb_string *m_s;
1295 struct usb_string *s;
1296 unsigned n;
1297
1298 m_s = n_gs[0]->strings;
1299 s = n_gs[i]->strings;
1300 for (n = 0; n < n_strings; n++) {
1301 s->id = m_s->id;
1302 s++;
1303 m_s++;
1304 }
1305 }
1306 list_add_tail(&uc->list, &cdev->gstrings);
1307 return n_gs[0]->strings;
1308err:
1309 kfree(uc);
1310 return ERR_PTR(ret);
1311}
1312EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1313
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001314/**
1315 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001316 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001317 * @n: number of string IDs to allocate
1318 * Context: single threaded during gadget setup
1319 *
1320 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001321 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001322 * is, returns last requested ID which is now very useful information.
1323 *
1324 * @usb_string_ids_n() is called from bind() callbacks to allocate
1325 * string IDs. Drivers for functions, configurations, or gadgets will
1326 * then store that ID in the appropriate descriptors and string table.
1327 *
1328 * All string identifier should be allocated using this,
1329 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1330 * example different functions don't wrongly assign different meanings
1331 * to the same identifier.
1332 */
1333int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1334{
1335 unsigned next = c->next_string_id;
1336 if (unlikely(n > 254 || (unsigned)next + n > 254))
1337 return -ENODEV;
1338 c->next_string_id += n;
1339 return next + 1;
1340}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001341EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001342
David Brownell40982be2008-06-19 17:52:58 -07001343/*-------------------------------------------------------------------------*/
1344
1345static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1346{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001347 struct usb_composite_dev *cdev;
1348
David Brownell40982be2008-06-19 17:52:58 -07001349 if (req->status || req->actual != req->length)
1350 DBG((struct usb_composite_dev *) ep->driver_data,
1351 "setup complete --> %d, %d/%d\n",
1352 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001353
1354 /*
1355 * REVIST The same ep0 requests are shared with function drivers
1356 * so they don't have to maintain the same ->complete() stubs.
1357 *
1358 * Because of that, we need to check for the validity of ->context
1359 * here, even though we know we've set it to something useful.
1360 */
1361 if (!req->context)
1362 return;
1363
1364 cdev = req->context;
1365
1366 if (cdev->req == req)
1367 cdev->setup_pending = false;
1368 else if (cdev->os_desc_req == req)
1369 cdev->os_desc_pending = false;
1370 else
1371 WARN(1, "unknown request %p\n", req);
1372}
1373
1374static int composite_ep0_queue(struct usb_composite_dev *cdev,
1375 struct usb_request *req, gfp_t gfp_flags)
1376{
1377 int ret;
1378
1379 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1380 if (ret == 0) {
1381 if (cdev->req == req)
1382 cdev->setup_pending = true;
1383 else if (cdev->os_desc_req == req)
1384 cdev->os_desc_pending = true;
1385 else
1386 WARN(1, "unknown request %p\n", req);
1387 }
1388
1389 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001390}
1391
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001392static int count_ext_compat(struct usb_configuration *c)
1393{
1394 int i, res;
1395
1396 res = 0;
1397 for (i = 0; i < c->next_interface_id; ++i) {
1398 struct usb_function *f;
1399 int j;
1400
1401 f = c->interface[i];
1402 for (j = 0; j < f->os_desc_n; ++j) {
1403 struct usb_os_desc *d;
1404
1405 if (i != f->os_desc_table[j].if_id)
1406 continue;
1407 d = f->os_desc_table[j].os_desc;
1408 if (d && d->ext_compat_id)
1409 ++res;
1410 }
1411 }
1412 BUG_ON(res > 255);
1413 return res;
1414}
1415
1416static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1417{
1418 int i, count;
1419
1420 count = 16;
1421 for (i = 0; i < c->next_interface_id; ++i) {
1422 struct usb_function *f;
1423 int j;
1424
1425 f = c->interface[i];
1426 for (j = 0; j < f->os_desc_n; ++j) {
1427 struct usb_os_desc *d;
1428
1429 if (i != f->os_desc_table[j].if_id)
1430 continue;
1431 d = f->os_desc_table[j].os_desc;
1432 if (d && d->ext_compat_id) {
1433 *buf++ = i;
1434 *buf++ = 0x01;
1435 memcpy(buf, d->ext_compat_id, 16);
1436 buf += 22;
1437 } else {
1438 ++buf;
1439 *buf = 0x01;
1440 buf += 23;
1441 }
1442 count += 24;
1443 if (count >= 4096)
1444 return;
1445 }
1446 }
1447}
1448
1449static int count_ext_prop(struct usb_configuration *c, int interface)
1450{
1451 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001452 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001453
1454 f = c->interface[interface];
1455 for (j = 0; j < f->os_desc_n; ++j) {
1456 struct usb_os_desc *d;
1457
1458 if (interface != f->os_desc_table[j].if_id)
1459 continue;
1460 d = f->os_desc_table[j].os_desc;
1461 if (d && d->ext_compat_id)
1462 return d->ext_prop_count;
1463 }
Julia Lawall849b1332014-05-19 06:31:07 +02001464 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001465}
1466
1467static int len_ext_prop(struct usb_configuration *c, int interface)
1468{
1469 struct usb_function *f;
1470 struct usb_os_desc *d;
1471 int j, res;
1472
1473 res = 10; /* header length */
1474 f = c->interface[interface];
1475 for (j = 0; j < f->os_desc_n; ++j) {
1476 if (interface != f->os_desc_table[j].if_id)
1477 continue;
1478 d = f->os_desc_table[j].os_desc;
1479 if (d)
1480 return min(res + d->ext_prop_len, 4096);
1481 }
1482 return res;
1483}
1484
1485static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1486{
1487 struct usb_function *f;
1488 struct usb_os_desc *d;
1489 struct usb_os_desc_ext_prop *ext_prop;
1490 int j, count, n, ret;
1491 u8 *start = buf;
1492
1493 f = c->interface[interface];
1494 for (j = 0; j < f->os_desc_n; ++j) {
1495 if (interface != f->os_desc_table[j].if_id)
1496 continue;
1497 d = f->os_desc_table[j].os_desc;
1498 if (d)
1499 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1500 /* 4kB minus header length */
1501 n = buf - start;
1502 if (n >= 4086)
1503 return 0;
1504
1505 count = ext_prop->data_len +
1506 ext_prop->name_len + 14;
1507 if (count > 4086 - n)
1508 return -EINVAL;
1509 usb_ext_prop_put_size(buf, count);
1510 usb_ext_prop_put_type(buf, ext_prop->type);
1511 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1512 ext_prop->name_len);
1513 if (ret < 0)
1514 return ret;
1515 switch (ext_prop->type) {
1516 case USB_EXT_PROP_UNICODE:
1517 case USB_EXT_PROP_UNICODE_ENV:
1518 case USB_EXT_PROP_UNICODE_LINK:
1519 usb_ext_prop_put_unicode(buf, ret,
1520 ext_prop->data,
1521 ext_prop->data_len);
1522 break;
1523 case USB_EXT_PROP_BINARY:
1524 usb_ext_prop_put_binary(buf, ret,
1525 ext_prop->data,
1526 ext_prop->data_len);
1527 break;
1528 case USB_EXT_PROP_LE32:
1529 /* not implemented */
1530 case USB_EXT_PROP_BE32:
1531 /* not implemented */
1532 default:
1533 return -EINVAL;
1534 }
1535 buf += count;
1536 }
1537 }
1538
1539 return 0;
1540}
1541
David Brownell40982be2008-06-19 17:52:58 -07001542/*
1543 * The setup() callback implements all the ep0 functionality that's
1544 * not handled lower down, in hardware or the hardware driver(like
1545 * device and endpoint feature flags, and their status). It's all
1546 * housekeeping for the gadget function we're implementing. Most of
1547 * the work is in config and function specific setup.
1548 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001549int
David Brownell40982be2008-06-19 17:52:58 -07001550composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1551{
1552 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1553 struct usb_request *req = cdev->req;
1554 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001555 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001556 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001557 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001558 u16 w_value = le16_to_cpu(ctrl->wValue);
1559 u16 w_length = le16_to_cpu(ctrl->wLength);
1560 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001561 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001562
1563 /* partial re-init of the response message; the function or the
1564 * gadget might need to intercept e.g. a control-OUT completion
1565 * when we delegate to it.
1566 */
1567 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001568 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001569 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301570 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001571 gadget->ep0->driver_data = cdev;
1572
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001573 /*
1574 * Don't let non-standard requests match any of the cases below
1575 * by accident.
1576 */
1577 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1578 goto unknown;
1579
David Brownell40982be2008-06-19 17:52:58 -07001580 switch (ctrl->bRequest) {
1581
1582 /* we handle all standard USB descriptors */
1583 case USB_REQ_GET_DESCRIPTOR:
1584 if (ctrl->bRequestType != USB_DIR_IN)
1585 goto unknown;
1586 switch (w_value >> 8) {
1587
1588 case USB_DT_DEVICE:
1589 cdev->desc.bNumConfigurations =
1590 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001591 cdev->desc.bMaxPacketSize0 =
1592 cdev->gadget->ep0->maxpacket;
1593 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001594 if (gadget->speed >= USB_SPEED_SUPER) {
John Youn1a853292016-02-05 17:05:40 -08001595 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001596 cdev->desc.bMaxPacketSize0 = 9;
1597 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001598 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001599 }
Igor Kotrasinski5527e732015-08-20 10:00:01 +02001600 } else {
1601 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001602 }
1603
David Brownell40982be2008-06-19 17:52:58 -07001604 value = min(w_length, (u16) sizeof cdev->desc);
1605 memcpy(req->buf, &cdev->desc, value);
1606 break;
1607 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001608 if (!gadget_is_dualspeed(gadget) ||
1609 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001610 break;
1611 device_qual(cdev);
1612 value = min_t(int, w_length,
1613 sizeof(struct usb_qualifier_descriptor));
1614 break;
1615 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001616 if (!gadget_is_dualspeed(gadget) ||
1617 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001618 break;
1619 /* FALLTHROUGH */
1620 case USB_DT_CONFIG:
1621 value = config_desc(cdev, w_value);
1622 if (value >= 0)
1623 value = min(w_length, (u16) value);
1624 break;
1625 case USB_DT_STRING:
1626 value = get_string(cdev, req->buf,
1627 w_index, w_value & 0xff);
1628 if (value >= 0)
1629 value = min(w_length, (u16) value);
1630 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001631 case USB_DT_BOS:
1632 if (gadget_is_superspeed(gadget)) {
1633 value = bos_desc(cdev);
1634 value = min(w_length, (u16) value);
1635 }
1636 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001637 case USB_DT_OTG:
1638 if (gadget_is_otg(gadget)) {
1639 struct usb_configuration *config;
1640 int otg_desc_len = 0;
1641
1642 if (cdev->config)
1643 config = cdev->config;
1644 else
1645 config = list_first_entry(
1646 &cdev->configs,
1647 struct usb_configuration, list);
1648 if (!config)
1649 goto done;
1650
1651 if (gadget->otg_caps &&
1652 (gadget->otg_caps->otg_rev >= 0x0200))
1653 otg_desc_len += sizeof(
1654 struct usb_otg20_descriptor);
1655 else
1656 otg_desc_len += sizeof(
1657 struct usb_otg_descriptor);
1658
1659 value = min_t(int, w_length, otg_desc_len);
1660 memcpy(req->buf, config->descriptors[0], value);
1661 }
1662 break;
David Brownell40982be2008-06-19 17:52:58 -07001663 }
1664 break;
1665
1666 /* any number of configs can work */
1667 case USB_REQ_SET_CONFIGURATION:
1668 if (ctrl->bRequestType != 0)
1669 goto unknown;
1670 if (gadget_is_otg(gadget)) {
1671 if (gadget->a_hnp_support)
1672 DBG(cdev, "HNP available\n");
1673 else if (gadget->a_alt_hnp_support)
1674 DBG(cdev, "HNP on another port\n");
1675 else
1676 VDBG(cdev, "HNP inactive\n");
1677 }
1678 spin_lock(&cdev->lock);
1679 value = set_config(cdev, ctrl, w_value);
1680 spin_unlock(&cdev->lock);
1681 break;
1682 case USB_REQ_GET_CONFIGURATION:
1683 if (ctrl->bRequestType != USB_DIR_IN)
1684 goto unknown;
1685 if (cdev->config)
1686 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1687 else
1688 *(u8 *)req->buf = 0;
1689 value = min(w_length, (u16) 1);
1690 break;
1691
1692 /* function drivers must handle get/set altsetting; if there's
1693 * no get() method, we know only altsetting zero works.
1694 */
1695 case USB_REQ_SET_INTERFACE:
1696 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1697 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001698 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001699 break;
Bryan Wu08889512009-01-08 00:21:19 +08001700 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001701 if (!f)
1702 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001703 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001704 break;
1705 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001706 if (value == USB_GADGET_DELAYED_STATUS) {
1707 DBG(cdev,
1708 "%s: interface %d (%s) requested delayed status\n",
1709 __func__, intf, f->name);
1710 cdev->delayed_status++;
1711 DBG(cdev, "delayed_status count %d\n",
1712 cdev->delayed_status);
1713 }
David Brownell40982be2008-06-19 17:52:58 -07001714 break;
1715 case USB_REQ_GET_INTERFACE:
1716 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1717 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001718 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001719 break;
Bryan Wu08889512009-01-08 00:21:19 +08001720 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001721 if (!f)
1722 break;
1723 /* lots of interfaces only need altsetting zero... */
1724 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1725 if (value < 0)
1726 break;
1727 *((u8 *)req->buf) = value;
1728 value = min(w_length, (u16) 1);
1729 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001730 case USB_REQ_GET_STATUS:
Li Junc5348b62016-02-19 10:04:44 +08001731 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1732 (w_index == OTG_STS_SELECTOR)) {
1733 if (ctrl->bRequestType != (USB_DIR_IN |
1734 USB_RECIP_DEVICE))
1735 goto unknown;
1736 *((u8 *)req->buf) = gadget->host_request_flag;
1737 value = 1;
1738 break;
1739 }
1740
1741 /*
1742 * USB 3.0 additions:
1743 * Function driver should handle get_status request. If such cb
1744 * wasn't supplied we respond with default value = 0
1745 * Note: function driver should supply such cb only for the
1746 * first interface of the function
1747 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001748 if (!gadget_is_superspeed(gadget))
1749 goto unknown;
1750 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1751 goto unknown;
1752 value = 2; /* This is the length of the get_status reply */
1753 put_unaligned_le16(0, req->buf);
1754 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1755 break;
1756 f = cdev->config->interface[intf];
1757 if (!f)
1758 break;
1759 status = f->get_status ? f->get_status(f) : 0;
1760 if (status < 0)
1761 break;
1762 put_unaligned_le16(status & 0x0000ffff, req->buf);
1763 break;
1764 /*
1765 * Function drivers should handle SetFeature/ClearFeature
1766 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1767 * only for the first interface of the function
1768 */
1769 case USB_REQ_CLEAR_FEATURE:
1770 case USB_REQ_SET_FEATURE:
1771 if (!gadget_is_superspeed(gadget))
1772 goto unknown;
1773 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1774 goto unknown;
1775 switch (w_value) {
1776 case USB_INTRF_FUNC_SUSPEND:
1777 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1778 break;
1779 f = cdev->config->interface[intf];
1780 if (!f)
1781 break;
1782 value = 0;
1783 if (f->func_suspend)
1784 value = f->func_suspend(f, w_index >> 8);
1785 if (value < 0) {
1786 ERROR(cdev,
1787 "func_suspend() returned error %d\n",
1788 value);
1789 value = 0;
1790 }
1791 break;
1792 }
1793 break;
David Brownell40982be2008-06-19 17:52:58 -07001794 default:
1795unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001796 /*
1797 * OS descriptors handling
1798 */
1799 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001800 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001801 ctrl->bRequest == cdev->b_vendor_code) {
1802 struct usb_request *req;
1803 struct usb_configuration *os_desc_cfg;
1804 u8 *buf;
1805 int interface;
1806 int count = 0;
1807
1808 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001809 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001810 req->complete = composite_setup_complete;
1811 buf = req->buf;
1812 os_desc_cfg = cdev->os_desc_config;
1813 memset(buf, 0, w_length);
1814 buf[5] = 0x01;
1815 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1816 case USB_RECIP_DEVICE:
1817 if (w_index != 0x4 || (w_value >> 8))
1818 break;
1819 buf[6] = w_index;
1820 if (w_length == 0x10) {
1821 /* Number of ext compat interfaces */
1822 count = count_ext_compat(os_desc_cfg);
1823 buf[8] = count;
1824 count *= 24; /* 24 B/ext compat desc */
1825 count += 16; /* header */
1826 put_unaligned_le32(count, buf);
1827 value = w_length;
1828 } else {
1829 /* "extended compatibility ID"s */
1830 count = count_ext_compat(os_desc_cfg);
1831 buf[8] = count;
1832 count *= 24; /* 24 B/ext compat desc */
1833 count += 16; /* header */
1834 put_unaligned_le32(count, buf);
1835 buf += 16;
1836 fill_ext_compat(os_desc_cfg, buf);
1837 value = w_length;
1838 }
1839 break;
1840 case USB_RECIP_INTERFACE:
1841 if (w_index != 0x5 || (w_value >> 8))
1842 break;
1843 interface = w_value & 0xFF;
1844 buf[6] = w_index;
1845 if (w_length == 0x0A) {
1846 count = count_ext_prop(os_desc_cfg,
1847 interface);
1848 put_unaligned_le16(count, buf + 8);
1849 count = len_ext_prop(os_desc_cfg,
1850 interface);
1851 put_unaligned_le32(count, buf);
1852
1853 value = w_length;
1854 } else {
1855 count = count_ext_prop(os_desc_cfg,
1856 interface);
1857 put_unaligned_le16(count, buf + 8);
1858 count = len_ext_prop(os_desc_cfg,
1859 interface);
1860 put_unaligned_le32(count, buf);
1861 buf += 10;
1862 value = fill_ext_prop(os_desc_cfg,
1863 interface, buf);
1864 if (value < 0)
1865 return value;
1866
1867 value = w_length;
1868 }
1869 break;
1870 }
William Wu7e14f47a2016-05-13 18:30:42 +08001871
1872 if (value >= 0) {
1873 req->length = value;
1874 req->context = cdev;
1875 req->zero = value < w_length;
1876 value = composite_ep0_queue(cdev, req,
1877 GFP_ATOMIC);
1878 if (value < 0) {
1879 DBG(cdev, "ep_queue --> %d\n", value);
1880 req->status = 0;
1881 composite_setup_complete(gadget->ep0,
1882 req);
1883 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001884 }
1885 return value;
1886 }
1887
David Brownell40982be2008-06-19 17:52:58 -07001888 VDBG(cdev,
1889 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1890 ctrl->bRequestType, ctrl->bRequest,
1891 w_value, w_index, w_length);
1892
Laurent Pinchart52426582009-10-21 00:03:38 +02001893 /* functions always handle their interfaces and endpoints...
1894 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001895 * configuration code.
1896 *
1897 * REVISIT it could make sense to let the composite device
1898 * take such requests too, if that's ever needed: to work
1899 * in config 0, etc.
1900 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301901 if (cdev->config) {
1902 list_for_each_entry(f, &cdev->config->functions, list)
1903 if (f->req_match && f->req_match(f, ctrl))
1904 goto try_fun_setup;
1905 f = NULL;
1906 }
1907
Laurent Pinchart52426582009-10-21 00:03:38 +02001908 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1909 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001910 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301911 break;
1912 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001913 break;
1914
1915 case USB_RECIP_ENDPOINT:
Peter Chenc526c622016-07-01 15:33:28 +08001916 if (!cdev->config)
1917 break;
Laurent Pinchart52426582009-10-21 00:03:38 +02001918 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1919 list_for_each_entry(f, &cdev->config->functions, list) {
1920 if (test_bit(endp, f->endpoints))
1921 break;
1922 }
1923 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001924 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001925 break;
David Brownell40982be2008-06-19 17:52:58 -07001926 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01001927try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02001928 if (f && f->setup)
1929 value = f->setup(f, ctrl);
1930 else {
David Brownell40982be2008-06-19 17:52:58 -07001931 struct usb_configuration *c;
1932
1933 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001934 if (!c)
1935 goto done;
1936
1937 /* try current config's setup */
1938 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07001939 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001940 goto done;
1941 }
1942
1943 /* try the only function in the current config */
1944 if (!list_is_singular(&c->functions))
1945 goto done;
1946 f = list_first_entry(&c->functions, struct usb_function,
1947 list);
1948 if (f->setup)
1949 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07001950 }
1951
1952 goto done;
1953 }
1954
1955 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001956 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001957 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001958 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001959 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001960 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07001961 if (value < 0) {
1962 DBG(cdev, "ep_queue --> %d\n", value);
1963 req->status = 0;
1964 composite_setup_complete(gadget->ep0, req);
1965 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001966 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1967 WARN(cdev,
1968 "%s: Delayed status not supported for w_length != 0",
1969 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001970 }
1971
1972done:
1973 /* device either stalls (value < 0) or reports success */
1974 return value;
1975}
1976
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001977void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001978{
1979 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1980 unsigned long flags;
1981
1982 /* REVISIT: should we have config and device level
1983 * disconnect callbacks?
1984 */
1985 spin_lock_irqsave(&cdev->lock, flags);
1986 if (cdev->config)
1987 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001988 if (cdev->driver->disconnect)
1989 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001990 spin_unlock_irqrestore(&cdev->lock, flags);
1991}
1992
1993/*-------------------------------------------------------------------------*/
1994
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001995static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1996 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001997{
1998 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1999 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2000
2001 return sprintf(buf, "%d\n", cdev->suspended);
2002}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002003static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002004
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002005static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07002006{
2007 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2008
2009 /* composite_disconnect() must already have been called
2010 * by the underlying peripheral controller driver!
2011 * so there's no i/o concurrency that could affect the
2012 * state protected by cdev->lock.
2013 */
2014 WARN_ON(cdev->config);
2015
2016 while (!list_empty(&cdev->configs)) {
2017 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07002018 c = list_first_entry(&cdev->configs,
2019 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02002020 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07002021 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002022 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002023 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002024
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002025 composite_dev_cleanup(cdev);
2026
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002027 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07002028 kfree(cdev);
2029 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07002030}
2031
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002032static void composite_unbind(struct usb_gadget *gadget)
2033{
2034 __composite_unbind(gadget, true);
2035}
2036
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002037static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2038 const struct usb_device_descriptor *old)
2039{
2040 __le16 idVendor;
2041 __le16 idProduct;
2042 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002043 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002044 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002045 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002046
2047 /*
2048 * these variables may have been set in
2049 * usb_composite_overwrite_options()
2050 */
2051 idVendor = new->idVendor;
2052 idProduct = new->idProduct;
2053 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002054 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002055 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002056 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002057
2058 *new = *old;
2059 if (idVendor)
2060 new->idVendor = idVendor;
2061 if (idProduct)
2062 new->idProduct = idProduct;
2063 if (bcdDevice)
2064 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02002065 else
2066 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002067 if (iSerialNumber)
2068 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002069 if (iManufacturer)
2070 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002071 if (iProduct)
2072 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002073}
2074
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002075int composite_dev_prepare(struct usb_composite_driver *composite,
2076 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002077{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002078 struct usb_gadget *gadget = cdev->gadget;
2079 int ret = -ENOMEM;
2080
2081 /* preallocate control response and buffer */
2082 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2083 if (!cdev->req)
2084 return -ENOMEM;
2085
2086 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2087 if (!cdev->req->buf)
2088 goto fail;
2089
2090 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2091 if (ret)
2092 goto fail_dev;
2093
2094 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002095 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002096 gadget->ep0->driver_data = cdev;
2097
2098 cdev->driver = composite;
2099
2100 /*
2101 * As per USB compliance update, a device that is actively drawing
2102 * more than 100mA from USB must report itself as bus-powered in
2103 * the GetStatus(DEVICE) call.
2104 */
2105 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2106 usb_gadget_set_selfpowered(gadget);
2107
2108 /* interface and string IDs start at zero via kzalloc.
2109 * we force endpoints to start unassigned; few controller
2110 * drivers will zero ep->driver_data.
2111 */
2112 usb_ep_autoconfig_reset(gadget);
2113 return 0;
2114fail_dev:
2115 kfree(cdev->req->buf);
2116fail:
2117 usb_ep_free_request(gadget->ep0, cdev->req);
2118 cdev->req = NULL;
2119 return ret;
2120}
2121
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002122int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2123 struct usb_ep *ep0)
2124{
2125 int ret = 0;
2126
2127 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2128 if (!cdev->os_desc_req) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002129 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002130 goto end;
2131 }
2132
2133 /* OS feature descriptor length <= 4kB */
2134 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2135 if (!cdev->os_desc_req->buf) {
Christophe JAILLET3887db52016-07-16 08:34:33 +02002136 ret = -ENOMEM;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002137 kfree(cdev->os_desc_req);
2138 goto end;
2139 }
Felipe Balbi57943712014-09-18 09:54:54 -05002140 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002141 cdev->os_desc_req->complete = composite_setup_complete;
2142end:
2143 return ret;
2144}
2145
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002146void composite_dev_cleanup(struct usb_composite_dev *cdev)
2147{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002148 struct usb_gadget_string_container *uc, *tmp;
2149
2150 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2151 list_del(&uc->list);
2152 kfree(uc);
2153 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002154 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002155 if (cdev->os_desc_pending)
2156 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2157
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002158 kfree(cdev->os_desc_req->buf);
2159 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2160 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002161 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002162 if (cdev->setup_pending)
2163 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2164
Li Junbe0a8882014-08-28 21:44:11 +08002165 kfree(cdev->req->buf);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002166 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2167 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002168 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002169 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002170}
2171
2172static int composite_bind(struct usb_gadget *gadget,
2173 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002174{
2175 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002176 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002177 int status = -ENOMEM;
2178
2179 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2180 if (!cdev)
2181 return status;
2182
2183 spin_lock_init(&cdev->lock);
2184 cdev->gadget = gadget;
2185 set_gadget_data(gadget, cdev);
2186 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002187 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002188
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002189 status = composite_dev_prepare(composite, cdev);
2190 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002191 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002192
2193 /* composite gadget needs to assign strings for whole device (like
2194 * serial number), register function drivers, potentially update
2195 * power state and consumption, etc
2196 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002197 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002198 if (status < 0)
2199 goto fail;
2200
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002201 if (cdev->use_os_string) {
2202 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2203 if (status)
2204 goto fail;
2205 }
2206
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002207 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002208
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002209 /* has userspace failed to provide a serial number? */
2210 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2211 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2212
David Brownell40982be2008-06-19 17:52:58 -07002213 INFO(cdev, "%s ready\n", composite->name);
2214 return 0;
2215
2216fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002217 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002218 return status;
2219}
2220
2221/*-------------------------------------------------------------------------*/
2222
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002223void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002224{
2225 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2226 struct usb_function *f;
2227
David Brownell89429392009-03-19 14:14:17 -07002228 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002229 * suspend/resume callbacks?
2230 */
2231 DBG(cdev, "suspend\n");
2232 if (cdev->config) {
2233 list_for_each_entry(f, &cdev->config->functions, list) {
2234 if (f->suspend)
2235 f->suspend(f);
2236 }
2237 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002238 if (cdev->driver->suspend)
2239 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002240
2241 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08002242
2243 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002244}
2245
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002246void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002247{
2248 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2249 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00002250 u16 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07002251
David Brownell89429392009-03-19 14:14:17 -07002252 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002253 * suspend/resume callbacks?
2254 */
2255 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002256 if (cdev->driver->resume)
2257 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002258 if (cdev->config) {
2259 list_for_each_entry(f, &cdev->config->functions, list) {
2260 if (f->resume)
2261 f->resume(f);
2262 }
Hao Wub23f2f92010-11-29 15:17:03 +08002263
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002264 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08002265
2266 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002267 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002268 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002269
2270 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002271}
2272
2273/*-------------------------------------------------------------------------*/
2274
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002275static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002276 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002277 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002278
2279 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002280 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002281 .disconnect = composite_disconnect,
2282
2283 .suspend = composite_suspend,
2284 .resume = composite_resume,
2285
2286 .driver = {
2287 .owner = THIS_MODULE,
2288 },
2289};
2290
2291/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002292 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002293 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002294 *
David Brownell40982be2008-06-19 17:52:58 -07002295 * Context: single threaded during gadget setup
2296 *
2297 * This function is used to register drivers using the composite driver
2298 * framework. The return value is zero, or a negative errno value.
2299 * Those values normally come from the driver's @bind method, which does
2300 * all the work of setting up the driver to match the hardware.
2301 *
2302 * On successful return, the gadget is ready to respond to requests from
2303 * the host, unless one of its components invokes usb_gadget_disconnect()
2304 * while it was binding. That would usually be done in order to wait for
2305 * some userspace participation.
2306 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002307int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002308{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002309 struct usb_gadget_driver *gadget_driver;
2310
2311 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002312 return -EINVAL;
2313
2314 if (!driver->name)
2315 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002316
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002317 driver->gadget_driver = composite_driver_template;
2318 gadget_driver = &driver->gadget_driver;
2319
2320 gadget_driver->function = (char *) driver->name;
2321 gadget_driver->driver.name = driver->name;
2322 gadget_driver->max_speed = driver->max_speed;
2323
2324 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002325}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002326EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002327
2328/**
2329 * usb_composite_unregister() - unregister a composite driver
2330 * @driver: the driver to unregister
2331 *
2332 * This function is used to unregister drivers using the composite
2333 * driver framework.
2334 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002335void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002336{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002337 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002338}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002339EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002340
2341/**
2342 * usb_composite_setup_continue() - Continue with the control transfer
2343 * @cdev: the composite device who's control transfer was kept waiting
2344 *
2345 * This function must be called by the USB function driver to continue
2346 * with the control transfer's data/status stage in case it had requested to
2347 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2348 * can request the composite framework to delay the setup request's data/status
2349 * stages by returning USB_GADGET_DELAYED_STATUS.
2350 */
2351void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2352{
2353 int value;
2354 struct usb_request *req = cdev->req;
2355 unsigned long flags;
2356
2357 DBG(cdev, "%s\n", __func__);
2358 spin_lock_irqsave(&cdev->lock, flags);
2359
2360 if (cdev->delayed_status == 0) {
2361 WARN(cdev, "%s: Unexpected call\n", __func__);
2362
2363 } else if (--cdev->delayed_status == 0) {
2364 DBG(cdev, "%s: Completing delayed status\n", __func__);
2365 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002366 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002367 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002368 if (value < 0) {
2369 DBG(cdev, "ep_queue --> %d\n", value);
2370 req->status = 0;
2371 composite_setup_complete(cdev->gadget->ep0, req);
2372 }
2373 }
2374
2375 spin_unlock_irqrestore(&cdev->lock, flags);
2376}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002377EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002378
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002379static char *composite_default_mfr(struct usb_gadget *gadget)
2380{
2381 char *mfr;
2382 int len;
2383
2384 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2385 init_utsname()->release, gadget->name);
2386 len++;
2387 mfr = kmalloc(len, GFP_KERNEL);
2388 if (!mfr)
2389 return NULL;
2390 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2391 init_utsname()->release, gadget->name);
2392 return mfr;
2393}
2394
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002395void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2396 struct usb_composite_overwrite *covr)
2397{
2398 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002399 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2400 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002401
2402 if (covr->idVendor)
2403 desc->idVendor = cpu_to_le16(covr->idVendor);
2404
2405 if (covr->idProduct)
2406 desc->idProduct = cpu_to_le16(covr->idProduct);
2407
2408 if (covr->bcdDevice)
2409 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002410
2411 if (covr->serial_number) {
2412 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2413 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2414 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002415 if (covr->manufacturer) {
2416 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2417 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002418
2419 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2420 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2421 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2422 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002423 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002424
2425 if (covr->product) {
2426 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2427 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2428 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002429}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002430EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002431
2432MODULE_LICENSE("GPL");
2433MODULE_AUTHOR("David Brownell");