blob: 11d243349ebff1461d397fc05904d63e0281b50b [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/**
57 * next_ep_desc() - advance to the next EP descriptor
58 * @t: currect pointer within descriptor array
59 *
60 * Return: next EP descriptor or NULL
61 *
62 * Iterate over @t until either EP descriptor found or
63 * NULL (that indicates end of list) encountered
64 */
65static struct usb_descriptor_header**
66next_ep_desc(struct usb_descriptor_header **t)
67{
68 for (; *t; t++) {
69 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
70 return t;
71 }
72 return NULL;
73}
74
75/*
76 * for_each_ep_desc()- iterate over endpoint descriptors in the
77 * descriptors list
78 * @start: pointer within descriptor array.
79 * @ep_desc: endpoint descriptor to use as the loop cursor
80 */
81#define for_each_ep_desc(start, ep_desc) \
82 for (ep_desc = next_ep_desc(start); \
83 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
84
85/**
86 * config_ep_by_speed() - configures the given endpoint
87 * according to gadget speed.
88 * @g: pointer to the gadget
89 * @f: usb function
90 * @_ep: the endpoint to configure
91 *
92 * Return: error code, 0 on success
93 *
94 * This function chooses the right descriptors for a given
95 * endpoint according to gadget speed and saves it in the
96 * endpoint desc field. If the endpoint already has a descriptor
97 * assigned to it - overwrites it with currently corresponding
98 * descriptor. The endpoint maxpacket field is updated according
99 * to the chosen descriptor.
100 * Note: the supplied function should hold all the descriptors
101 * for supported speeds
102 */
103int config_ep_by_speed(struct usb_gadget *g,
104 struct usb_function *f,
105 struct usb_ep *_ep)
106{
Felipe Balbib785ea72012-06-06 10:20:23 +0300107 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300108 struct usb_endpoint_descriptor *chosen_desc = NULL;
109 struct usb_descriptor_header **speed_desc = NULL;
110
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300111 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
112 int want_comp_desc = 0;
113
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300114 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
115
116 if (!g || !f || !_ep)
117 return -EIO;
118
119 /* select desired speed */
120 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300121 case USB_SPEED_SUPER:
122 if (gadget_is_superspeed(g)) {
123 speed_desc = f->ss_descriptors;
124 want_comp_desc = 1;
125 break;
126 }
127 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300128 case USB_SPEED_HIGH:
129 if (gadget_is_dualspeed(g)) {
130 speed_desc = f->hs_descriptors;
131 break;
132 }
133 /* else: fall through */
134 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200135 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300136 }
137 /* find descriptors */
138 for_each_ep_desc(speed_desc, d_spd) {
139 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
140 if (chosen_desc->bEndpointAddress == _ep->address)
141 goto ep_found;
142 }
143 return -EIO;
144
145ep_found:
146 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700147 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300148 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300149 _ep->comp_desc = NULL;
150 _ep->maxburst = 0;
151 _ep->mult = 0;
152 if (!want_comp_desc)
153 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300154
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300155 /*
156 * Companion descriptor should follow EP descriptor
157 * USB 3.0 spec, #9.6.7
158 */
159 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
160 if (!comp_desc ||
161 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
162 return -EIO;
163 _ep->comp_desc = comp_desc;
164 if (g->speed == USB_SPEED_SUPER) {
165 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300166 case USB_ENDPOINT_XFER_ISOC:
167 /* mult: bits 1:0 of bmAttributes */
168 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800169 case USB_ENDPOINT_XFER_BULK:
170 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300171 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300172 break;
173 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300174 if (comp_desc->bMaxBurst != 0)
175 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
176 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300177 break;
178 }
179 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300180 return 0;
181}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200182EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700183
184/**
185 * usb_add_function() - add a function to a configuration
186 * @config: the configuration
187 * @function: the function being added
188 * Context: single threaded during gadget setup
189 *
190 * After initialization, each configuration must have one or more
191 * functions added to it. Adding a function involves calling its @bind()
192 * method to allocate resources such as interface and string identifiers
193 * and endpoints.
194 *
195 * This function returns the value of the function's bind(), which is
196 * zero for success else a negative errno value.
197 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200198int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700199 struct usb_function *function)
200{
201 int value = -EINVAL;
202
203 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
204 function->name, function,
205 config->label, config);
206
207 if (!function->set_alt || !function->disable)
208 goto done;
209
210 function->config = config;
211 list_add_tail(&function->list, &config->functions);
212
Robert Baldygad5bb9b82015-05-04 14:55:13 +0200213 if (function->bind_deactivated) {
214 value = usb_function_deactivate(function);
215 if (value)
216 goto done;
217 }
218
David Brownell40982be2008-06-19 17:52:58 -0700219 /* REVISIT *require* function->bind? */
220 if (function->bind) {
221 value = function->bind(config, function);
222 if (value < 0) {
223 list_del(&function->list);
224 function->config = NULL;
225 }
226 } else
227 value = 0;
228
229 /* We allow configurations that don't work at both speeds.
230 * If we run into a lowspeed Linux system, treat it the same
231 * as full speed ... it's the function drivers that will need
232 * to avoid bulk and ISO transfers.
233 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200234 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700235 config->fullspeed = true;
236 if (!config->highspeed && function->hs_descriptors)
237 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300238 if (!config->superspeed && function->ss_descriptors)
239 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700240
241done:
242 if (value)
243 DBG(config->cdev, "adding '%s'/%p --> %d\n",
244 function->name, function, value);
245 return value;
246}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200247EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700248
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100249void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
250{
251 if (f->disable)
252 f->disable(f);
253
254 bitmap_zero(f->endpoints, 32);
255 list_del(&f->list);
256 if (f->unbind)
257 f->unbind(c, f);
258}
259EXPORT_SYMBOL_GPL(usb_remove_function);
260
David Brownell40982be2008-06-19 17:52:58 -0700261/**
David Brownell60beed92008-08-18 17:38:22 -0700262 * usb_function_deactivate - prevent function and gadget enumeration
263 * @function: the function that isn't yet ready to respond
264 *
265 * Blocks response of the gadget driver to host enumeration by
266 * preventing the data line pullup from being activated. This is
267 * normally called during @bind() processing to change from the
268 * initial "ready to respond" state, or when a required resource
269 * becomes available.
270 *
271 * For example, drivers that serve as a passthrough to a userspace
272 * daemon can block enumeration unless that daemon (such as an OBEX,
273 * MTP, or print server) is ready to handle host requests.
274 *
275 * Not all systems support software control of their USB peripheral
276 * data pullups.
277 *
278 * Returns zero on success, else negative errno.
279 */
280int usb_function_deactivate(struct usb_function *function)
281{
282 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200283 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700284 int status = 0;
285
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200286 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700287
288 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200289 status = usb_gadget_deactivate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700290 if (status == 0)
291 cdev->deactivations++;
292
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200293 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700294 return status;
295}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200296EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700297
298/**
299 * usb_function_activate - allow function and gadget enumeration
300 * @function: function on which usb_function_activate() was called
301 *
302 * Reverses effect of usb_function_deactivate(). If no more functions
303 * are delaying their activation, the gadget driver will respond to
304 * host enumeration procedures.
305 *
306 * Returns zero on success, else negative errno.
307 */
308int usb_function_activate(struct usb_function *function)
309{
310 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200311 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700312 int status = 0;
313
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200314 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700315
316 if (WARN_ON(cdev->deactivations == 0))
317 status = -EINVAL;
318 else {
319 cdev->deactivations--;
320 if (cdev->deactivations == 0)
Robert Baldyga56012502015-05-04 14:55:12 +0200321 status = usb_gadget_activate(cdev->gadget);
David Brownell60beed92008-08-18 17:38:22 -0700322 }
323
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200324 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700325 return status;
326}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200327EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700328
329/**
David Brownell40982be2008-06-19 17:52:58 -0700330 * usb_interface_id() - allocate an unused interface ID
331 * @config: configuration associated with the interface
332 * @function: function handling the interface
333 * Context: single threaded during gadget setup
334 *
335 * usb_interface_id() is called from usb_function.bind() callbacks to
336 * allocate new interface IDs. The function driver will then store that
337 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300338 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700339 * particularly changing its altsetting via set_alt(). There may
340 * also be class-specific or vendor-specific requests to handle.
341 *
342 * All interface identifier should be allocated using this routine, to
343 * ensure that for example different functions don't wrongly assign
344 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300345 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700346 * one configuration (or more than once in a given configuration) need
347 * multiple versions of the relevant descriptors.
348 *
349 * Returns the interface ID which was allocated; or -ENODEV if no
350 * more interface IDs can be allocated.
351 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200352int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700353 struct usb_function *function)
354{
355 unsigned id = config->next_interface_id;
356
357 if (id < MAX_CONFIG_INTERFACES) {
358 config->interface[id] = function;
359 config->next_interface_id = id + 1;
360 return id;
361 }
362 return -ENODEV;
363}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200364EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700365
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100366static u8 encode_bMaxPower(enum usb_device_speed speed,
367 struct usb_configuration *c)
368{
369 unsigned val;
370
371 if (c->MaxPower)
372 val = c->MaxPower;
373 else
374 val = CONFIG_USB_GADGET_VBUS_DRAW;
375 if (!val)
376 return 0;
377 switch (speed) {
378 case USB_SPEED_SUPER:
379 return DIV_ROUND_UP(val, 8);
380 default:
381 return DIV_ROUND_UP(val, 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700382 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100383}
384
David Brownell40982be2008-06-19 17:52:58 -0700385static int config_buf(struct usb_configuration *config,
386 enum usb_device_speed speed, void *buf, u8 type)
387{
388 struct usb_config_descriptor *c = buf;
389 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200390 int len;
David Brownell40982be2008-06-19 17:52:58 -0700391 struct usb_function *f;
392 int status;
393
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200394 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700395 /* write the config descriptor */
396 c = buf;
397 c->bLength = USB_DT_CONFIG_SIZE;
398 c->bDescriptorType = type;
399 /* wTotalLength is written later */
400 c->bNumInterfaces = config->next_interface_id;
401 c->bConfigurationValue = config->bConfigurationValue;
402 c->iConfiguration = config->iConfiguration;
403 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100404 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700405
406 /* There may be e.g. OTG descriptors */
407 if (config->descriptors) {
408 status = usb_descriptor_fillbuf(next, len,
409 config->descriptors);
410 if (status < 0)
411 return status;
412 len -= status;
413 next += status;
414 }
415
416 /* add each function's descriptors */
417 list_for_each_entry(f, &config->functions, list) {
418 struct usb_descriptor_header **descriptors;
419
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300420 switch (speed) {
421 case USB_SPEED_SUPER:
422 descriptors = f->ss_descriptors;
423 break;
424 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700425 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300426 break;
427 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200428 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300429 }
430
David Brownell40982be2008-06-19 17:52:58 -0700431 if (!descriptors)
432 continue;
433 status = usb_descriptor_fillbuf(next, len,
434 (const struct usb_descriptor_header **) descriptors);
435 if (status < 0)
436 return status;
437 len -= status;
438 next += status;
439 }
440
441 len = next - buf;
442 c->wTotalLength = cpu_to_le16(len);
443 return len;
444}
445
446static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
447{
448 struct usb_gadget *gadget = cdev->gadget;
449 struct usb_configuration *c;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200450 struct list_head *pos;
David Brownell40982be2008-06-19 17:52:58 -0700451 u8 type = w_value >> 8;
452 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
453
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300454 if (gadget->speed == USB_SPEED_SUPER)
455 speed = gadget->speed;
456 else if (gadget_is_dualspeed(gadget)) {
457 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700458 if (gadget->speed == USB_SPEED_HIGH)
459 hs = 1;
460 if (type == USB_DT_OTHER_SPEED_CONFIG)
461 hs = !hs;
462 if (hs)
463 speed = USB_SPEED_HIGH;
464
465 }
466
467 /* This is a lookup by config *INDEX* */
468 w_value &= 0xff;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +0200469
470 pos = &cdev->configs;
471 c = cdev->os_desc_config;
472 if (c)
473 goto check_config;
474
475 while ((pos = pos->next) != &cdev->configs) {
476 c = list_entry(pos, typeof(*c), list);
477
478 /* skip OS Descriptors config which is handled separately */
479 if (c == cdev->os_desc_config)
480 continue;
481
482check_config:
David Brownell40982be2008-06-19 17:52:58 -0700483 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300484 switch (speed) {
485 case USB_SPEED_SUPER:
486 if (!c->superspeed)
487 continue;
488 break;
489 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700490 if (!c->highspeed)
491 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300492 break;
493 default:
David Brownell40982be2008-06-19 17:52:58 -0700494 if (!c->fullspeed)
495 continue;
496 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300497
David Brownell40982be2008-06-19 17:52:58 -0700498 if (w_value == 0)
499 return config_buf(c, speed, cdev->req->buf, type);
500 w_value--;
501 }
502 return -EINVAL;
503}
504
505static int count_configs(struct usb_composite_dev *cdev, unsigned type)
506{
507 struct usb_gadget *gadget = cdev->gadget;
508 struct usb_configuration *c;
509 unsigned count = 0;
510 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300511 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700512
513 if (gadget_is_dualspeed(gadget)) {
514 if (gadget->speed == USB_SPEED_HIGH)
515 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300516 if (gadget->speed == USB_SPEED_SUPER)
517 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700518 if (type == USB_DT_DEVICE_QUALIFIER)
519 hs = !hs;
520 }
521 list_for_each_entry(c, &cdev->configs, list) {
522 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300523 if (ss) {
524 if (!c->superspeed)
525 continue;
526 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700527 if (!c->highspeed)
528 continue;
529 } else {
530 if (!c->fullspeed)
531 continue;
532 }
533 count++;
534 }
535 return count;
536}
537
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300538/**
539 * bos_desc() - prepares the BOS descriptor.
540 * @cdev: pointer to usb_composite device to generate the bos
541 * descriptor for
542 *
543 * This function generates the BOS (Binary Device Object)
544 * descriptor and its device capabilities descriptors. The BOS
545 * descriptor should be supported by a SuperSpeed device.
546 */
547static int bos_desc(struct usb_composite_dev *cdev)
548{
549 struct usb_ext_cap_descriptor *usb_ext;
550 struct usb_ss_cap_descriptor *ss_cap;
551 struct usb_dcd_config_params dcd_config_params;
552 struct usb_bos_descriptor *bos = cdev->req->buf;
553
554 bos->bLength = USB_DT_BOS_SIZE;
555 bos->bDescriptorType = USB_DT_BOS;
556
557 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
558 bos->bNumDeviceCaps = 0;
559
560 /*
561 * A SuperSpeed device shall include the USB2.0 extension descriptor
562 * and shall support LPM when operating in USB2.0 HS mode.
563 */
564 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
565 bos->bNumDeviceCaps++;
566 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
567 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
568 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
569 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
Felipe Balbia6615932014-09-30 16:08:03 -0500570 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300571
572 /*
573 * The Superspeed USB Capability descriptor shall be implemented by all
574 * SuperSpeed devices.
575 */
576 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
577 bos->bNumDeviceCaps++;
578 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
579 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
580 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
581 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
582 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
583 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
584 USB_FULL_SPEED_OPERATION |
585 USB_HIGH_SPEED_OPERATION |
586 USB_5GBPS_OPERATION);
587 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
588
589 /* Get Controller configuration */
590 if (cdev->gadget->ops->get_config_params)
591 cdev->gadget->ops->get_config_params(&dcd_config_params);
592 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300593 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300594 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300595 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300596 }
597 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
598 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
599
John Younf228a8d2016-02-05 17:05:53 -0800600 /* The SuperSpeedPlus USB Device Capability descriptor */
601 if (gadget_is_superspeed_plus(cdev->gadget)) {
602 struct usb_ssp_cap_descriptor *ssp_cap;
603
604 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
605 bos->bNumDeviceCaps++;
606
607 /*
608 * Report typical values.
609 */
610
611 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
612 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
613 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
614 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
615
616 /* SSAC = 1 (2 attributes) */
617 ssp_cap->bmAttributes = cpu_to_le32(1);
618
619 /* Min RX/TX Lane Count = 1 */
620 ssp_cap->wFunctionalitySupport = (1 << 8) | (1 << 12);
621
622 /*
623 * bmSublinkSpeedAttr[0]:
624 * ST = Symmetric, RX
625 * LSE = 3 (Gbps)
626 * LP = 1 (SuperSpeedPlus)
627 * LSM = 10 (10 Gbps)
628 */
629 ssp_cap->bmSublinkSpeedAttr[0] =
630 (3 << 4) | (1 << 14) | (0xa << 16);
631 /*
632 * bmSublinkSpeedAttr[1] =
633 * ST = Symmetric, TX
634 * LSE = 3 (Gbps)
635 * LP = 1 (SuperSpeedPlus)
636 * LSM = 10 (10 Gbps)
637 */
638 ssp_cap->bmSublinkSpeedAttr[1] =
639 (3 << 4) | (1 << 14) | (0xa << 16) | (1 << 7);
640 }
641
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300642 return le16_to_cpu(bos->wTotalLength);
643}
644
David Brownell40982be2008-06-19 17:52:58 -0700645static void device_qual(struct usb_composite_dev *cdev)
646{
647 struct usb_qualifier_descriptor *qual = cdev->req->buf;
648
649 qual->bLength = sizeof(*qual);
650 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
651 /* POLICY: same bcdUSB and device type info at both speeds */
652 qual->bcdUSB = cdev->desc.bcdUSB;
653 qual->bDeviceClass = cdev->desc.bDeviceClass;
654 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
655 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
656 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200657 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700658 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700659 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700660}
661
662/*-------------------------------------------------------------------------*/
663
664static void reset_config(struct usb_composite_dev *cdev)
665{
666 struct usb_function *f;
667
668 DBG(cdev, "reset config\n");
669
670 list_for_each_entry(f, &cdev->config->functions, list) {
671 if (f->disable)
672 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200673
674 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700675 }
676 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100677 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700678}
679
680static int set_config(struct usb_composite_dev *cdev,
681 const struct usb_ctrlrequest *ctrl, unsigned number)
682{
683 struct usb_gadget *gadget = cdev->gadget;
684 struct usb_configuration *c = NULL;
685 int result = -EINVAL;
686 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
687 int tmp;
688
David Brownell40982be2008-06-19 17:52:58 -0700689 if (number) {
690 list_for_each_entry(c, &cdev->configs, list) {
691 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300692 /*
693 * We disable the FDs of the previous
694 * configuration only if the new configuration
695 * is a valid one
696 */
697 if (cdev->config)
698 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700699 result = 0;
700 break;
701 }
702 }
703 if (result < 0)
704 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300705 } else { /* Zero configuration value - need to reset the config */
706 if (cdev->config)
707 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700708 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300709 }
David Brownell40982be2008-06-19 17:52:58 -0700710
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200711 INFO(cdev, "%s config #%d: %s\n",
712 usb_speed_string(gadget->speed),
713 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700714
715 if (!c)
716 goto done;
717
Peter Chen6027f312014-04-29 13:26:28 +0800718 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700719 cdev->config = c;
720
721 /* Initialize all interfaces by setting them to altsetting zero. */
722 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
723 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200724 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700725
726 if (!f)
727 break;
728
Laurent Pinchart52426582009-10-21 00:03:38 +0200729 /*
730 * Record which endpoints are used by the function. This is used
731 * to dispatch control requests targeted at that endpoint to the
732 * function's setup callback instead of the current
733 * configuration's setup callback.
734 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300735 switch (gadget->speed) {
736 case USB_SPEED_SUPER:
737 descriptors = f->ss_descriptors;
738 break;
739 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200740 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300741 break;
742 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200743 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300744 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200745
746 for (; *descriptors; ++descriptors) {
747 struct usb_endpoint_descriptor *ep;
748 int addr;
749
750 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
751 continue;
752
753 ep = (struct usb_endpoint_descriptor *)*descriptors;
754 addr = ((ep->bEndpointAddress & 0x80) >> 3)
755 | (ep->bEndpointAddress & 0x0f);
756 set_bit(addr, f->endpoints);
757 }
758
David Brownell40982be2008-06-19 17:52:58 -0700759 result = f->set_alt(f, tmp, 0);
760 if (result < 0) {
761 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
762 tmp, f->name, f, result);
763
764 reset_config(cdev);
765 goto done;
766 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300767
768 if (result == USB_GADGET_DELAYED_STATUS) {
769 DBG(cdev,
770 "%s: interface %d (%s) requested delayed status\n",
771 __func__, tmp, f->name);
772 cdev->delayed_status++;
773 DBG(cdev, "delayed_status count %d\n",
774 cdev->delayed_status);
775 }
David Brownell40982be2008-06-19 17:52:58 -0700776 }
777
778 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100779 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700780done:
781 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300782 if (result >= 0 && cdev->delayed_status)
783 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700784 return result;
785}
786
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100787int usb_add_config_only(struct usb_composite_dev *cdev,
788 struct usb_configuration *config)
789{
790 struct usb_configuration *c;
791
792 if (!config->bConfigurationValue)
793 return -EINVAL;
794
795 /* Prevent duplicate configuration identifiers */
796 list_for_each_entry(c, &cdev->configs, list) {
797 if (c->bConfigurationValue == config->bConfigurationValue)
798 return -EBUSY;
799 }
800
801 config->cdev = cdev;
802 list_add_tail(&config->list, &cdev->configs);
803
804 INIT_LIST_HEAD(&config->functions);
805 config->next_interface_id = 0;
806 memset(config->interface, 0, sizeof(config->interface));
807
808 return 0;
809}
810EXPORT_SYMBOL_GPL(usb_add_config_only);
811
David Brownell40982be2008-06-19 17:52:58 -0700812/**
813 * usb_add_config() - add a configuration to a device.
814 * @cdev: wraps the USB gadget
815 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200816 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700817 * Context: single threaded during gadget setup
818 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200819 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700820 * add each of the configurations it supports, using this routine.
821 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200822 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700823 * is zero for success else a negative errno value. Binding configurations
824 * assigns global resources including string IDs, and per-configuration
825 * resources such as interface IDs and endpoints.
826 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200827int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200828 struct usb_configuration *config,
829 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700830{
831 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100832
833 if (!bind)
834 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700835
836 DBG(cdev, "adding config #%u '%s'/%p\n",
837 config->bConfigurationValue,
838 config->label, config);
839
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100840 status = usb_add_config_only(cdev, config);
841 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700842 goto done;
843
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200844 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700845 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900846 while (!list_empty(&config->functions)) {
847 struct usb_function *f;
848
849 f = list_first_entry(&config->functions,
850 struct usb_function, list);
851 list_del(&f->list);
852 if (f->unbind) {
853 DBG(cdev, "unbind function '%s'/%p\n",
854 f->name, f);
855 f->unbind(config, f);
856 /* may free memory for "f" */
857 }
858 }
David Brownell40982be2008-06-19 17:52:58 -0700859 list_del(&config->list);
860 config->cdev = NULL;
861 } else {
862 unsigned i;
863
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300864 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700865 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300866 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700867 config->highspeed ? " high" : "",
868 config->fullspeed
869 ? (gadget_is_dualspeed(cdev->gadget)
870 ? " full"
871 : " full/low")
872 : "");
873
874 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
875 struct usb_function *f = config->interface[i];
876
877 if (!f)
878 continue;
879 DBG(cdev, " interface %d = %s/%p\n",
880 i, f->name, f);
881 }
882 }
883
Robert Baldygaf871cb92015-09-16 12:10:39 +0200884 /* set_alt(), or next bind(), sets up ep->claimed as needed */
David Brownell40982be2008-06-19 17:52:58 -0700885 usb_ep_autoconfig_reset(cdev->gadget);
886
887done:
888 if (status)
889 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
890 config->bConfigurationValue, status);
891 return status;
892}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200893EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700894
Benoit Goby51cce6f2012-05-10 10:07:57 +0200895static void remove_config(struct usb_composite_dev *cdev,
896 struct usb_configuration *config)
897{
898 while (!list_empty(&config->functions)) {
899 struct usb_function *f;
900
901 f = list_first_entry(&config->functions,
902 struct usb_function, list);
903 list_del(&f->list);
904 if (f->unbind) {
905 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
906 f->unbind(config, f);
907 /* may free memory for "f" */
908 }
909 }
910 list_del(&config->list);
911 if (config->unbind) {
912 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
913 config->unbind(config);
914 /* may free memory for "c" */
915 }
916}
917
918/**
919 * usb_remove_config() - remove a configuration from a device.
920 * @cdev: wraps the USB gadget
921 * @config: the configuration
922 *
923 * Drivers must call usb_gadget_disconnect before calling this function
924 * to disconnect the device from the host and make sure the host will not
925 * try to enumerate the device while we are changing the config list.
926 */
927void usb_remove_config(struct usb_composite_dev *cdev,
928 struct usb_configuration *config)
929{
930 unsigned long flags;
931
932 spin_lock_irqsave(&cdev->lock, flags);
933
934 if (cdev->config == config)
935 reset_config(cdev);
936
937 spin_unlock_irqrestore(&cdev->lock, flags);
938
939 remove_config(cdev, config);
940}
941
David Brownell40982be2008-06-19 17:52:58 -0700942/*-------------------------------------------------------------------------*/
943
944/* We support strings in multiple languages ... string descriptor zero
945 * says which languages are supported. The typical case will be that
Diego Violaad4676a2015-05-31 15:52:41 -0300946 * only one language (probably English) is used, with i18n handled on
David Brownell40982be2008-06-19 17:52:58 -0700947 * the host side.
948 */
949
950static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
951{
952 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300953 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700954 __le16 *tmp;
955
956 while (*sp) {
957 s = *sp;
958 language = cpu_to_le16(s->language);
959 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
960 if (*tmp == language)
961 goto repeat;
962 }
963 *tmp++ = language;
964repeat:
965 sp++;
966 }
967}
968
969static int lookup_string(
970 struct usb_gadget_strings **sp,
971 void *buf,
972 u16 language,
973 int id
974)
975{
976 struct usb_gadget_strings *s;
977 int value;
978
979 while (*sp) {
980 s = *sp++;
981 if (s->language != language)
982 continue;
983 value = usb_gadget_get_string(s, id, buf);
984 if (value > 0)
985 return value;
986 }
987 return -EINVAL;
988}
989
990static int get_string(struct usb_composite_dev *cdev,
991 void *buf, u16 language, int id)
992{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200993 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +0100994 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -0700995 struct usb_configuration *c;
996 struct usb_function *f;
997 int len;
998
Diego Violaad4676a2015-05-31 15:52:41 -0300999 /* Yes, not only is USB's i18n support probably more than most
David Brownell40982be2008-06-19 17:52:58 -07001000 * folk will ever care about ... also, it's all supported here.
1001 * (Except for UTF8 support for Unicode's "Astral Planes".)
1002 */
1003
1004 /* 0 == report all available language codes */
1005 if (id == 0) {
1006 struct usb_string_descriptor *s = buf;
1007 struct usb_gadget_strings **sp;
1008
1009 memset(s, 0, 256);
1010 s->bDescriptorType = USB_DT_STRING;
1011
1012 sp = composite->strings;
1013 if (sp)
1014 collect_langs(sp, s->wData);
1015
1016 list_for_each_entry(c, &cdev->configs, list) {
1017 sp = c->strings;
1018 if (sp)
1019 collect_langs(sp, s->wData);
1020
1021 list_for_each_entry(f, &c->functions, list) {
1022 sp = f->strings;
1023 if (sp)
1024 collect_langs(sp, s->wData);
1025 }
1026 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001027 list_for_each_entry(uc, &cdev->gstrings, list) {
1028 struct usb_gadget_strings **sp;
1029
1030 sp = get_containers_gs(uc);
1031 collect_langs(sp, s->wData);
1032 }
David Brownell40982be2008-06-19 17:52:58 -07001033
Roel Kluin417b57b2009-08-06 16:09:51 -07001034 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -07001035 continue;
1036 if (!len)
1037 return -EINVAL;
1038
1039 s->bLength = 2 * (len + 1);
1040 return s->bLength;
1041 }
1042
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +02001043 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1044 struct usb_os_string *b = buf;
1045 b->bLength = sizeof(*b);
1046 b->bDescriptorType = USB_DT_STRING;
1047 compiletime_assert(
1048 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1049 "qwSignature size must be equal to qw_sign");
1050 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1051 b->bMS_VendorCode = cdev->b_vendor_code;
1052 b->bPad = 0;
1053 return sizeof(*b);
1054 }
1055
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001056 list_for_each_entry(uc, &cdev->gstrings, list) {
1057 struct usb_gadget_strings **sp;
1058
1059 sp = get_containers_gs(uc);
1060 len = lookup_string(sp, buf, language, id);
1061 if (len > 0)
1062 return len;
1063 }
1064
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001065 /* String IDs are device-scoped, so we look up each string
1066 * table we're told about. These lookups are infrequent;
1067 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001068 */
1069 if (composite->strings) {
1070 len = lookup_string(composite->strings, buf, language, id);
1071 if (len > 0)
1072 return len;
1073 }
1074 list_for_each_entry(c, &cdev->configs, list) {
1075 if (c->strings) {
1076 len = lookup_string(c->strings, buf, language, id);
1077 if (len > 0)
1078 return len;
1079 }
1080 list_for_each_entry(f, &c->functions, list) {
1081 if (!f->strings)
1082 continue;
1083 len = lookup_string(f->strings, buf, language, id);
1084 if (len > 0)
1085 return len;
1086 }
1087 }
1088 return -EINVAL;
1089}
1090
1091/**
1092 * usb_string_id() - allocate an unused string ID
1093 * @cdev: the device whose string descriptor IDs are being allocated
1094 * Context: single threaded during gadget setup
1095 *
1096 * @usb_string_id() is called from bind() callbacks to allocate
1097 * string IDs. Drivers for functions, configurations, or gadgets will
1098 * then store that ID in the appropriate descriptors and string table.
1099 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001100 * All string identifier should be allocated using this,
1101 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1102 * that for example different functions don't wrongly assign different
1103 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001104 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001105int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001106{
1107 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001108 /* string id 0 is reserved by USB spec for list of
1109 * supported languages */
1110 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001111 cdev->next_string_id++;
1112 return cdev->next_string_id;
1113 }
1114 return -ENODEV;
1115}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001116EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001117
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001118/**
1119 * usb_string_ids() - allocate unused string IDs in batch
1120 * @cdev: the device whose string descriptor IDs are being allocated
1121 * @str: an array of usb_string objects to assign numbers to
1122 * Context: single threaded during gadget setup
1123 *
1124 * @usb_string_ids() is called from bind() callbacks to allocate
1125 * string IDs. Drivers for functions, configurations, or gadgets will
1126 * then copy IDs from the string table to the appropriate descriptors
1127 * and string table for other languages.
1128 *
1129 * All string identifier should be allocated using this,
1130 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1131 * example different functions don't wrongly assign different meanings
1132 * to the same identifier.
1133 */
1134int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1135{
1136 int next = cdev->next_string_id;
1137
1138 for (; str->s; ++str) {
1139 if (unlikely(next >= 254))
1140 return -ENODEV;
1141 str->id = ++next;
1142 }
1143
1144 cdev->next_string_id = next;
1145
1146 return 0;
1147}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001148EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001149
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001150static struct usb_gadget_string_container *copy_gadget_strings(
1151 struct usb_gadget_strings **sp, unsigned n_gstrings,
1152 unsigned n_strings)
1153{
1154 struct usb_gadget_string_container *uc;
1155 struct usb_gadget_strings **gs_array;
1156 struct usb_gadget_strings *gs;
1157 struct usb_string *s;
1158 unsigned mem;
1159 unsigned n_gs;
1160 unsigned n_s;
1161 void *stash;
1162
1163 mem = sizeof(*uc);
1164 mem += sizeof(void *) * (n_gstrings + 1);
1165 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1166 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1167 uc = kmalloc(mem, GFP_KERNEL);
1168 if (!uc)
1169 return ERR_PTR(-ENOMEM);
1170 gs_array = get_containers_gs(uc);
1171 stash = uc->stash;
1172 stash += sizeof(void *) * (n_gstrings + 1);
1173 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1174 struct usb_string *org_s;
1175
1176 gs_array[n_gs] = stash;
1177 gs = gs_array[n_gs];
1178 stash += sizeof(struct usb_gadget_strings);
1179 gs->language = sp[n_gs]->language;
1180 gs->strings = stash;
1181 org_s = sp[n_gs]->strings;
1182
1183 for (n_s = 0; n_s < n_strings; n_s++) {
1184 s = stash;
1185 stash += sizeof(struct usb_string);
1186 if (org_s->s)
1187 s->s = org_s->s;
1188 else
1189 s->s = "";
1190 org_s++;
1191 }
1192 s = stash;
1193 s->s = NULL;
1194 stash += sizeof(struct usb_string);
1195
1196 }
1197 gs_array[n_gs] = NULL;
1198 return uc;
1199}
1200
1201/**
1202 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1203 * @cdev: the device whose string descriptor IDs are being allocated
1204 * and attached.
1205 * @sp: an array of usb_gadget_strings to attach.
1206 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1207 *
1208 * This function will create a deep copy of usb_gadget_strings and usb_string
1209 * and attach it to the cdev. The actual string (usb_string.s) will not be
1210 * copied but only a referenced will be made. The struct usb_gadget_strings
Masanari Iida06ed0de2015-03-10 22:37:46 +09001211 * array may contain multiple languages and should be NULL terminated.
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001212 * The ->language pointer of each struct usb_gadget_strings has to contain the
1213 * same amount of entries.
1214 * 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 +09001215 * usb_string entry of es-ES contains the translation of the first usb_string
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001216 * entry of en-US. Therefore both entries become the same id assign.
1217 */
1218struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1219 struct usb_gadget_strings **sp, unsigned n_strings)
1220{
1221 struct usb_gadget_string_container *uc;
1222 struct usb_gadget_strings **n_gs;
1223 unsigned n_gstrings = 0;
1224 unsigned i;
1225 int ret;
1226
1227 for (i = 0; sp[i]; i++)
1228 n_gstrings++;
1229
1230 if (!n_gstrings)
1231 return ERR_PTR(-EINVAL);
1232
1233 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1234 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001235 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001236
1237 n_gs = get_containers_gs(uc);
1238 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1239 if (ret)
1240 goto err;
1241
1242 for (i = 1; i < n_gstrings; i++) {
1243 struct usb_string *m_s;
1244 struct usb_string *s;
1245 unsigned n;
1246
1247 m_s = n_gs[0]->strings;
1248 s = n_gs[i]->strings;
1249 for (n = 0; n < n_strings; n++) {
1250 s->id = m_s->id;
1251 s++;
1252 m_s++;
1253 }
1254 }
1255 list_add_tail(&uc->list, &cdev->gstrings);
1256 return n_gs[0]->strings;
1257err:
1258 kfree(uc);
1259 return ERR_PTR(ret);
1260}
1261EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1262
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001263/**
1264 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001265 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001266 * @n: number of string IDs to allocate
1267 * Context: single threaded during gadget setup
1268 *
1269 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001270 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001271 * is, returns last requested ID which is now very useful information.
1272 *
1273 * @usb_string_ids_n() is called from bind() callbacks to allocate
1274 * string IDs. Drivers for functions, configurations, or gadgets will
1275 * then store that ID in the appropriate descriptors and string table.
1276 *
1277 * All string identifier should be allocated using this,
1278 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1279 * example different functions don't wrongly assign different meanings
1280 * to the same identifier.
1281 */
1282int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1283{
1284 unsigned next = c->next_string_id;
1285 if (unlikely(n > 254 || (unsigned)next + n > 254))
1286 return -ENODEV;
1287 c->next_string_id += n;
1288 return next + 1;
1289}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001290EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001291
David Brownell40982be2008-06-19 17:52:58 -07001292/*-------------------------------------------------------------------------*/
1293
1294static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1295{
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001296 struct usb_composite_dev *cdev;
1297
David Brownell40982be2008-06-19 17:52:58 -07001298 if (req->status || req->actual != req->length)
1299 DBG((struct usb_composite_dev *) ep->driver_data,
1300 "setup complete --> %d, %d/%d\n",
1301 req->status, req->actual, req->length);
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001302
1303 /*
1304 * REVIST The same ep0 requests are shared with function drivers
1305 * so they don't have to maintain the same ->complete() stubs.
1306 *
1307 * Because of that, we need to check for the validity of ->context
1308 * here, even though we know we've set it to something useful.
1309 */
1310 if (!req->context)
1311 return;
1312
1313 cdev = req->context;
1314
1315 if (cdev->req == req)
1316 cdev->setup_pending = false;
1317 else if (cdev->os_desc_req == req)
1318 cdev->os_desc_pending = false;
1319 else
1320 WARN(1, "unknown request %p\n", req);
1321}
1322
1323static int composite_ep0_queue(struct usb_composite_dev *cdev,
1324 struct usb_request *req, gfp_t gfp_flags)
1325{
1326 int ret;
1327
1328 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1329 if (ret == 0) {
1330 if (cdev->req == req)
1331 cdev->setup_pending = true;
1332 else if (cdev->os_desc_req == req)
1333 cdev->os_desc_pending = true;
1334 else
1335 WARN(1, "unknown request %p\n", req);
1336 }
1337
1338 return ret;
David Brownell40982be2008-06-19 17:52:58 -07001339}
1340
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001341static int count_ext_compat(struct usb_configuration *c)
1342{
1343 int i, res;
1344
1345 res = 0;
1346 for (i = 0; i < c->next_interface_id; ++i) {
1347 struct usb_function *f;
1348 int j;
1349
1350 f = c->interface[i];
1351 for (j = 0; j < f->os_desc_n; ++j) {
1352 struct usb_os_desc *d;
1353
1354 if (i != f->os_desc_table[j].if_id)
1355 continue;
1356 d = f->os_desc_table[j].os_desc;
1357 if (d && d->ext_compat_id)
1358 ++res;
1359 }
1360 }
1361 BUG_ON(res > 255);
1362 return res;
1363}
1364
1365static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1366{
1367 int i, count;
1368
1369 count = 16;
1370 for (i = 0; i < c->next_interface_id; ++i) {
1371 struct usb_function *f;
1372 int j;
1373
1374 f = c->interface[i];
1375 for (j = 0; j < f->os_desc_n; ++j) {
1376 struct usb_os_desc *d;
1377
1378 if (i != f->os_desc_table[j].if_id)
1379 continue;
1380 d = f->os_desc_table[j].os_desc;
1381 if (d && d->ext_compat_id) {
1382 *buf++ = i;
1383 *buf++ = 0x01;
1384 memcpy(buf, d->ext_compat_id, 16);
1385 buf += 22;
1386 } else {
1387 ++buf;
1388 *buf = 0x01;
1389 buf += 23;
1390 }
1391 count += 24;
1392 if (count >= 4096)
1393 return;
1394 }
1395 }
1396}
1397
1398static int count_ext_prop(struct usb_configuration *c, int interface)
1399{
1400 struct usb_function *f;
Julia Lawall849b1332014-05-19 06:31:07 +02001401 int j;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001402
1403 f = c->interface[interface];
1404 for (j = 0; j < f->os_desc_n; ++j) {
1405 struct usb_os_desc *d;
1406
1407 if (interface != f->os_desc_table[j].if_id)
1408 continue;
1409 d = f->os_desc_table[j].os_desc;
1410 if (d && d->ext_compat_id)
1411 return d->ext_prop_count;
1412 }
Julia Lawall849b1332014-05-19 06:31:07 +02001413 return 0;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001414}
1415
1416static int len_ext_prop(struct usb_configuration *c, int interface)
1417{
1418 struct usb_function *f;
1419 struct usb_os_desc *d;
1420 int j, res;
1421
1422 res = 10; /* header length */
1423 f = c->interface[interface];
1424 for (j = 0; j < f->os_desc_n; ++j) {
1425 if (interface != f->os_desc_table[j].if_id)
1426 continue;
1427 d = f->os_desc_table[j].os_desc;
1428 if (d)
1429 return min(res + d->ext_prop_len, 4096);
1430 }
1431 return res;
1432}
1433
1434static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1435{
1436 struct usb_function *f;
1437 struct usb_os_desc *d;
1438 struct usb_os_desc_ext_prop *ext_prop;
1439 int j, count, n, ret;
1440 u8 *start = buf;
1441
1442 f = c->interface[interface];
1443 for (j = 0; j < f->os_desc_n; ++j) {
1444 if (interface != f->os_desc_table[j].if_id)
1445 continue;
1446 d = f->os_desc_table[j].os_desc;
1447 if (d)
1448 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1449 /* 4kB minus header length */
1450 n = buf - start;
1451 if (n >= 4086)
1452 return 0;
1453
1454 count = ext_prop->data_len +
1455 ext_prop->name_len + 14;
1456 if (count > 4086 - n)
1457 return -EINVAL;
1458 usb_ext_prop_put_size(buf, count);
1459 usb_ext_prop_put_type(buf, ext_prop->type);
1460 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1461 ext_prop->name_len);
1462 if (ret < 0)
1463 return ret;
1464 switch (ext_prop->type) {
1465 case USB_EXT_PROP_UNICODE:
1466 case USB_EXT_PROP_UNICODE_ENV:
1467 case USB_EXT_PROP_UNICODE_LINK:
1468 usb_ext_prop_put_unicode(buf, ret,
1469 ext_prop->data,
1470 ext_prop->data_len);
1471 break;
1472 case USB_EXT_PROP_BINARY:
1473 usb_ext_prop_put_binary(buf, ret,
1474 ext_prop->data,
1475 ext_prop->data_len);
1476 break;
1477 case USB_EXT_PROP_LE32:
1478 /* not implemented */
1479 case USB_EXT_PROP_BE32:
1480 /* not implemented */
1481 default:
1482 return -EINVAL;
1483 }
1484 buf += count;
1485 }
1486 }
1487
1488 return 0;
1489}
1490
David Brownell40982be2008-06-19 17:52:58 -07001491/*
1492 * The setup() callback implements all the ep0 functionality that's
1493 * not handled lower down, in hardware or the hardware driver(like
1494 * device and endpoint feature flags, and their status). It's all
1495 * housekeeping for the gadget function we're implementing. Most of
1496 * the work is in config and function specific setup.
1497 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001498int
David Brownell40982be2008-06-19 17:52:58 -07001499composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1500{
1501 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1502 struct usb_request *req = cdev->req;
1503 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001504 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001505 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001506 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001507 u16 w_value = le16_to_cpu(ctrl->wValue);
1508 u16 w_length = le16_to_cpu(ctrl->wLength);
1509 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001510 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001511
1512 /* partial re-init of the response message; the function or the
1513 * gadget might need to intercept e.g. a control-OUT completion
1514 * when we delegate to it.
1515 */
1516 req->zero = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05001517 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001518 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301519 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001520 gadget->ep0->driver_data = cdev;
1521
Andrzej Pietrasiewicz232c0102015-03-03 10:52:04 +01001522 /*
1523 * Don't let non-standard requests match any of the cases below
1524 * by accident.
1525 */
1526 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1527 goto unknown;
1528
David Brownell40982be2008-06-19 17:52:58 -07001529 switch (ctrl->bRequest) {
1530
1531 /* we handle all standard USB descriptors */
1532 case USB_REQ_GET_DESCRIPTOR:
1533 if (ctrl->bRequestType != USB_DIR_IN)
1534 goto unknown;
1535 switch (w_value >> 8) {
1536
1537 case USB_DT_DEVICE:
1538 cdev->desc.bNumConfigurations =
1539 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001540 cdev->desc.bMaxPacketSize0 =
1541 cdev->gadget->ep0->maxpacket;
1542 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001543 if (gadget->speed >= USB_SPEED_SUPER) {
John Youn1a853292016-02-05 17:05:40 -08001544 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001545 cdev->desc.bMaxPacketSize0 = 9;
1546 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001547 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001548 }
Igor Kotrasinski5527e732015-08-20 10:00:01 +02001549 } else {
1550 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001551 }
1552
David Brownell40982be2008-06-19 17:52:58 -07001553 value = min(w_length, (u16) sizeof cdev->desc);
1554 memcpy(req->buf, &cdev->desc, value);
1555 break;
1556 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001557 if (!gadget_is_dualspeed(gadget) ||
1558 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001559 break;
1560 device_qual(cdev);
1561 value = min_t(int, w_length,
1562 sizeof(struct usb_qualifier_descriptor));
1563 break;
1564 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001565 if (!gadget_is_dualspeed(gadget) ||
1566 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001567 break;
1568 /* FALLTHROUGH */
1569 case USB_DT_CONFIG:
1570 value = config_desc(cdev, w_value);
1571 if (value >= 0)
1572 value = min(w_length, (u16) value);
1573 break;
1574 case USB_DT_STRING:
1575 value = get_string(cdev, req->buf,
1576 w_index, w_value & 0xff);
1577 if (value >= 0)
1578 value = min(w_length, (u16) value);
1579 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001580 case USB_DT_BOS:
1581 if (gadget_is_superspeed(gadget)) {
1582 value = bos_desc(cdev);
1583 value = min(w_length, (u16) value);
1584 }
1585 break;
Macpaul Lin53e62422015-07-09 15:18:42 +08001586 case USB_DT_OTG:
1587 if (gadget_is_otg(gadget)) {
1588 struct usb_configuration *config;
1589 int otg_desc_len = 0;
1590
1591 if (cdev->config)
1592 config = cdev->config;
1593 else
1594 config = list_first_entry(
1595 &cdev->configs,
1596 struct usb_configuration, list);
1597 if (!config)
1598 goto done;
1599
1600 if (gadget->otg_caps &&
1601 (gadget->otg_caps->otg_rev >= 0x0200))
1602 otg_desc_len += sizeof(
1603 struct usb_otg20_descriptor);
1604 else
1605 otg_desc_len += sizeof(
1606 struct usb_otg_descriptor);
1607
1608 value = min_t(int, w_length, otg_desc_len);
1609 memcpy(req->buf, config->descriptors[0], value);
1610 }
1611 break;
David Brownell40982be2008-06-19 17:52:58 -07001612 }
1613 break;
1614
1615 /* any number of configs can work */
1616 case USB_REQ_SET_CONFIGURATION:
1617 if (ctrl->bRequestType != 0)
1618 goto unknown;
1619 if (gadget_is_otg(gadget)) {
1620 if (gadget->a_hnp_support)
1621 DBG(cdev, "HNP available\n");
1622 else if (gadget->a_alt_hnp_support)
1623 DBG(cdev, "HNP on another port\n");
1624 else
1625 VDBG(cdev, "HNP inactive\n");
1626 }
1627 spin_lock(&cdev->lock);
1628 value = set_config(cdev, ctrl, w_value);
1629 spin_unlock(&cdev->lock);
1630 break;
1631 case USB_REQ_GET_CONFIGURATION:
1632 if (ctrl->bRequestType != USB_DIR_IN)
1633 goto unknown;
1634 if (cdev->config)
1635 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1636 else
1637 *(u8 *)req->buf = 0;
1638 value = min(w_length, (u16) 1);
1639 break;
1640
1641 /* function drivers must handle get/set altsetting; if there's
1642 * no get() method, we know only altsetting zero works.
1643 */
1644 case USB_REQ_SET_INTERFACE:
1645 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1646 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001647 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001648 break;
Bryan Wu08889512009-01-08 00:21:19 +08001649 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001650 if (!f)
1651 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001652 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001653 break;
1654 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001655 if (value == USB_GADGET_DELAYED_STATUS) {
1656 DBG(cdev,
1657 "%s: interface %d (%s) requested delayed status\n",
1658 __func__, intf, f->name);
1659 cdev->delayed_status++;
1660 DBG(cdev, "delayed_status count %d\n",
1661 cdev->delayed_status);
1662 }
David Brownell40982be2008-06-19 17:52:58 -07001663 break;
1664 case USB_REQ_GET_INTERFACE:
1665 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1666 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001667 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001668 break;
Bryan Wu08889512009-01-08 00:21:19 +08001669 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001670 if (!f)
1671 break;
1672 /* lots of interfaces only need altsetting zero... */
1673 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1674 if (value < 0)
1675 break;
1676 *((u8 *)req->buf) = value;
1677 value = min(w_length, (u16) 1);
1678 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001679
1680 /*
1681 * USB 3.0 additions:
1682 * Function driver should handle get_status request. If such cb
1683 * wasn't supplied we respond with default value = 0
1684 * Note: function driver should supply such cb only for the first
1685 * interface of the function
1686 */
1687 case USB_REQ_GET_STATUS:
1688 if (!gadget_is_superspeed(gadget))
1689 goto unknown;
1690 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1691 goto unknown;
1692 value = 2; /* This is the length of the get_status reply */
1693 put_unaligned_le16(0, req->buf);
1694 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1695 break;
1696 f = cdev->config->interface[intf];
1697 if (!f)
1698 break;
1699 status = f->get_status ? f->get_status(f) : 0;
1700 if (status < 0)
1701 break;
1702 put_unaligned_le16(status & 0x0000ffff, req->buf);
1703 break;
1704 /*
1705 * Function drivers should handle SetFeature/ClearFeature
1706 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1707 * only for the first interface of the function
1708 */
1709 case USB_REQ_CLEAR_FEATURE:
1710 case USB_REQ_SET_FEATURE:
1711 if (!gadget_is_superspeed(gadget))
1712 goto unknown;
1713 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1714 goto unknown;
1715 switch (w_value) {
1716 case USB_INTRF_FUNC_SUSPEND:
1717 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1718 break;
1719 f = cdev->config->interface[intf];
1720 if (!f)
1721 break;
1722 value = 0;
1723 if (f->func_suspend)
1724 value = f->func_suspend(f, w_index >> 8);
1725 if (value < 0) {
1726 ERROR(cdev,
1727 "func_suspend() returned error %d\n",
1728 value);
1729 value = 0;
1730 }
1731 break;
1732 }
1733 break;
David Brownell40982be2008-06-19 17:52:58 -07001734 default:
1735unknown:
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001736 /*
1737 * OS descriptors handling
1738 */
1739 if (cdev->use_os_string && cdev->os_desc_config &&
Mario Schuknechtdf6738d2015-01-26 20:30:27 +01001740 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001741 ctrl->bRequest == cdev->b_vendor_code) {
1742 struct usb_request *req;
1743 struct usb_configuration *os_desc_cfg;
1744 u8 *buf;
1745 int interface;
1746 int count = 0;
1747
1748 req = cdev->os_desc_req;
Felipe Balbi57943712014-09-18 09:54:54 -05001749 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001750 req->complete = composite_setup_complete;
1751 buf = req->buf;
1752 os_desc_cfg = cdev->os_desc_config;
1753 memset(buf, 0, w_length);
1754 buf[5] = 0x01;
1755 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1756 case USB_RECIP_DEVICE:
1757 if (w_index != 0x4 || (w_value >> 8))
1758 break;
1759 buf[6] = w_index;
1760 if (w_length == 0x10) {
1761 /* Number of ext compat interfaces */
1762 count = count_ext_compat(os_desc_cfg);
1763 buf[8] = count;
1764 count *= 24; /* 24 B/ext compat desc */
1765 count += 16; /* header */
1766 put_unaligned_le32(count, buf);
1767 value = w_length;
1768 } else {
1769 /* "extended compatibility ID"s */
1770 count = count_ext_compat(os_desc_cfg);
1771 buf[8] = count;
1772 count *= 24; /* 24 B/ext compat desc */
1773 count += 16; /* header */
1774 put_unaligned_le32(count, buf);
1775 buf += 16;
1776 fill_ext_compat(os_desc_cfg, buf);
1777 value = w_length;
1778 }
1779 break;
1780 case USB_RECIP_INTERFACE:
1781 if (w_index != 0x5 || (w_value >> 8))
1782 break;
1783 interface = w_value & 0xFF;
1784 buf[6] = w_index;
1785 if (w_length == 0x0A) {
1786 count = count_ext_prop(os_desc_cfg,
1787 interface);
1788 put_unaligned_le16(count, buf + 8);
1789 count = len_ext_prop(os_desc_cfg,
1790 interface);
1791 put_unaligned_le32(count, buf);
1792
1793 value = w_length;
1794 } else {
1795 count = count_ext_prop(os_desc_cfg,
1796 interface);
1797 put_unaligned_le16(count, buf + 8);
1798 count = len_ext_prop(os_desc_cfg,
1799 interface);
1800 put_unaligned_le32(count, buf);
1801 buf += 10;
1802 value = fill_ext_prop(os_desc_cfg,
1803 interface, buf);
1804 if (value < 0)
1805 return value;
1806
1807 value = w_length;
1808 }
1809 break;
1810 }
1811 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001812 req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001813 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001814 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02001815 if (value < 0) {
1816 DBG(cdev, "ep_queue --> %d\n", value);
1817 req->status = 0;
1818 composite_setup_complete(gadget->ep0, req);
1819 }
1820 return value;
1821 }
1822
David Brownell40982be2008-06-19 17:52:58 -07001823 VDBG(cdev,
1824 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1825 ctrl->bRequestType, ctrl->bRequest,
1826 w_value, w_index, w_length);
1827
Laurent Pinchart52426582009-10-21 00:03:38 +02001828 /* functions always handle their interfaces and endpoints...
1829 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001830 * configuration code.
1831 *
1832 * REVISIT it could make sense to let the composite device
1833 * take such requests too, if that's ever needed: to work
1834 * in config 0, etc.
1835 */
Kishon Vijay Abraham Ib4c21f02015-06-11 22:12:11 +05301836 if (cdev->config) {
1837 list_for_each_entry(f, &cdev->config->functions, list)
1838 if (f->req_match && f->req_match(f, ctrl))
1839 goto try_fun_setup;
1840 f = NULL;
1841 }
1842
Laurent Pinchart52426582009-10-21 00:03:38 +02001843 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1844 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001845 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301846 break;
1847 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001848 break;
1849
1850 case USB_RECIP_ENDPOINT:
1851 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1852 list_for_each_entry(f, &cdev->config->functions, list) {
1853 if (test_bit(endp, f->endpoints))
1854 break;
1855 }
1856 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001857 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001858 break;
David Brownell40982be2008-06-19 17:52:58 -07001859 }
Andrzej Pietrasiewiczf563d232015-03-03 10:52:23 +01001860try_fun_setup:
Laurent Pinchart52426582009-10-21 00:03:38 +02001861 if (f && f->setup)
1862 value = f->setup(f, ctrl);
1863 else {
David Brownell40982be2008-06-19 17:52:58 -07001864 struct usb_configuration *c;
1865
1866 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001867 if (!c)
1868 goto done;
1869
1870 /* try current config's setup */
1871 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07001872 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001873 goto done;
1874 }
1875
1876 /* try the only function in the current config */
1877 if (!list_is_singular(&c->functions))
1878 goto done;
1879 f = list_first_entry(&c->functions, struct usb_function,
1880 list);
1881 if (f->setup)
1882 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07001883 }
1884
1885 goto done;
1886 }
1887
1888 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001889 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001890 req->length = value;
Felipe Balbi57943712014-09-18 09:54:54 -05001891 req->context = cdev;
David Brownell40982be2008-06-19 17:52:58 -07001892 req->zero = value < w_length;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05001893 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
David Brownell40982be2008-06-19 17:52:58 -07001894 if (value < 0) {
1895 DBG(cdev, "ep_queue --> %d\n", value);
1896 req->status = 0;
1897 composite_setup_complete(gadget->ep0, req);
1898 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001899 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1900 WARN(cdev,
1901 "%s: Delayed status not supported for w_length != 0",
1902 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001903 }
1904
1905done:
1906 /* device either stalls (value < 0) or reports success */
1907 return value;
1908}
1909
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001910void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001911{
1912 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1913 unsigned long flags;
1914
1915 /* REVISIT: should we have config and device level
1916 * disconnect callbacks?
1917 */
1918 spin_lock_irqsave(&cdev->lock, flags);
1919 if (cdev->config)
1920 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001921 if (cdev->driver->disconnect)
1922 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001923 spin_unlock_irqrestore(&cdev->lock, flags);
1924}
1925
1926/*-------------------------------------------------------------------------*/
1927
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001928static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1929 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001930{
1931 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1932 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1933
1934 return sprintf(buf, "%d\n", cdev->suspended);
1935}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001936static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001937
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001938static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07001939{
1940 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1941
1942 /* composite_disconnect() must already have been called
1943 * by the underlying peripheral controller driver!
1944 * so there's no i/o concurrency that could affect the
1945 * state protected by cdev->lock.
1946 */
1947 WARN_ON(cdev->config);
1948
1949 while (!list_empty(&cdev->configs)) {
1950 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001951 c = list_first_entry(&cdev->configs,
1952 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001953 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001954 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001955 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001956 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001957
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001958 composite_dev_cleanup(cdev);
1959
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001960 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001961 kfree(cdev);
1962 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001963}
1964
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001965static void composite_unbind(struct usb_gadget *gadget)
1966{
1967 __composite_unbind(gadget, true);
1968}
1969
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001970static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1971 const struct usb_device_descriptor *old)
1972{
1973 __le16 idVendor;
1974 __le16 idProduct;
1975 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001976 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001977 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001978 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001979
1980 /*
1981 * these variables may have been set in
1982 * usb_composite_overwrite_options()
1983 */
1984 idVendor = new->idVendor;
1985 idProduct = new->idProduct;
1986 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001987 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001988 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001989 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001990
1991 *new = *old;
1992 if (idVendor)
1993 new->idVendor = idVendor;
1994 if (idProduct)
1995 new->idProduct = idProduct;
1996 if (bcdDevice)
1997 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02001998 else
1999 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002000 if (iSerialNumber)
2001 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002002 if (iManufacturer)
2003 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002004 if (iProduct)
2005 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002006}
2007
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002008int composite_dev_prepare(struct usb_composite_driver *composite,
2009 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002010{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002011 struct usb_gadget *gadget = cdev->gadget;
2012 int ret = -ENOMEM;
2013
2014 /* preallocate control response and buffer */
2015 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2016 if (!cdev->req)
2017 return -ENOMEM;
2018
2019 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2020 if (!cdev->req->buf)
2021 goto fail;
2022
2023 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2024 if (ret)
2025 goto fail_dev;
2026
2027 cdev->req->complete = composite_setup_complete;
Felipe Balbi57943712014-09-18 09:54:54 -05002028 cdev->req->context = cdev;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002029 gadget->ep0->driver_data = cdev;
2030
2031 cdev->driver = composite;
2032
2033 /*
2034 * As per USB compliance update, a device that is actively drawing
2035 * more than 100mA from USB must report itself as bus-powered in
2036 * the GetStatus(DEVICE) call.
2037 */
2038 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2039 usb_gadget_set_selfpowered(gadget);
2040
2041 /* interface and string IDs start at zero via kzalloc.
2042 * we force endpoints to start unassigned; few controller
2043 * drivers will zero ep->driver_data.
2044 */
2045 usb_ep_autoconfig_reset(gadget);
2046 return 0;
2047fail_dev:
2048 kfree(cdev->req->buf);
2049fail:
2050 usb_ep_free_request(gadget->ep0, cdev->req);
2051 cdev->req = NULL;
2052 return ret;
2053}
2054
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002055int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2056 struct usb_ep *ep0)
2057{
2058 int ret = 0;
2059
2060 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2061 if (!cdev->os_desc_req) {
2062 ret = PTR_ERR(cdev->os_desc_req);
2063 goto end;
2064 }
2065
2066 /* OS feature descriptor length <= 4kB */
2067 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2068 if (!cdev->os_desc_req->buf) {
2069 ret = PTR_ERR(cdev->os_desc_req->buf);
2070 kfree(cdev->os_desc_req);
2071 goto end;
2072 }
Felipe Balbi57943712014-09-18 09:54:54 -05002073 cdev->os_desc_req->context = cdev;
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002074 cdev->os_desc_req->complete = composite_setup_complete;
2075end:
2076 return ret;
2077}
2078
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002079void composite_dev_cleanup(struct usb_composite_dev *cdev)
2080{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01002081 struct usb_gadget_string_container *uc, *tmp;
2082
2083 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2084 list_del(&uc->list);
2085 kfree(uc);
2086 }
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002087 if (cdev->os_desc_req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002088 if (cdev->os_desc_pending)
2089 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2090
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002091 kfree(cdev->os_desc_req->buf);
2092 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2093 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002094 if (cdev->req) {
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002095 if (cdev->setup_pending)
2096 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2097
Li Junbe0a8882014-08-28 21:44:11 +08002098 kfree(cdev->req->buf);
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002099 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2100 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002101 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002102 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002103}
2104
2105static int composite_bind(struct usb_gadget *gadget,
2106 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07002107{
2108 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002109 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07002110 int status = -ENOMEM;
2111
2112 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2113 if (!cdev)
2114 return status;
2115
2116 spin_lock_init(&cdev->lock);
2117 cdev->gadget = gadget;
2118 set_gadget_data(gadget, cdev);
2119 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01002120 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07002121
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01002122 status = composite_dev_prepare(composite, cdev);
2123 if (status)
David Brownell40982be2008-06-19 17:52:58 -07002124 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07002125
2126 /* composite gadget needs to assign strings for whole device (like
2127 * serial number), register function drivers, potentially update
2128 * power state and consumption, etc
2129 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02002130 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002131 if (status < 0)
2132 goto fail;
2133
Andrzej Pietrasiewicz37a3a532014-05-08 14:06:23 +02002134 if (cdev->use_os_string) {
2135 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2136 if (status)
2137 goto fail;
2138 }
2139
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002140 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08002141
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02002142 /* has userspace failed to provide a serial number? */
2143 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2144 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2145
David Brownell40982be2008-06-19 17:52:58 -07002146 INFO(cdev, "%s ready\n", composite->name);
2147 return 0;
2148
2149fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01002150 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07002151 return status;
2152}
2153
2154/*-------------------------------------------------------------------------*/
2155
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002156void composite_suspend(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002157{
2158 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2159 struct usb_function *f;
2160
David Brownell89429392009-03-19 14:14:17 -07002161 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002162 * suspend/resume callbacks?
2163 */
2164 DBG(cdev, "suspend\n");
2165 if (cdev->config) {
2166 list_for_each_entry(f, &cdev->config->functions, list) {
2167 if (f->suspend)
2168 f->suspend(f);
2169 }
2170 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002171 if (cdev->driver->suspend)
2172 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002173
2174 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08002175
2176 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07002177}
2178
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02002179void composite_resume(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07002180{
2181 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2182 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00002183 u16 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07002184
David Brownell89429392009-03-19 14:14:17 -07002185 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07002186 * suspend/resume callbacks?
2187 */
2188 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002189 if (cdev->driver->resume)
2190 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07002191 if (cdev->config) {
2192 list_for_each_entry(f, &cdev->config->functions, list) {
2193 if (f->resume)
2194 f->resume(f);
2195 }
Hao Wub23f2f92010-11-29 15:17:03 +08002196
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002197 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08002198
2199 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01002200 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07002201 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02002202
2203 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07002204}
2205
2206/*-------------------------------------------------------------------------*/
2207
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002208static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02002209 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01002210 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07002211
2212 .setup = composite_setup,
Peter Chend8a816f2014-09-09 08:56:49 +08002213 .reset = composite_disconnect,
David Brownell40982be2008-06-19 17:52:58 -07002214 .disconnect = composite_disconnect,
2215
2216 .suspend = composite_suspend,
2217 .resume = composite_resume,
2218
2219 .driver = {
2220 .owner = THIS_MODULE,
2221 },
2222};
2223
2224/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02002225 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07002226 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06002227 *
David Brownell40982be2008-06-19 17:52:58 -07002228 * Context: single threaded during gadget setup
2229 *
2230 * This function is used to register drivers using the composite driver
2231 * framework. The return value is zero, or a negative errno value.
2232 * Those values normally come from the driver's @bind method, which does
2233 * all the work of setting up the driver to match the hardware.
2234 *
2235 * On successful return, the gadget is ready to respond to requests from
2236 * the host, unless one of its components invokes usb_gadget_disconnect()
2237 * while it was binding. That would usually be done in order to wait for
2238 * some userspace participation.
2239 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02002240int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002241{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002242 struct usb_gadget_driver *gadget_driver;
2243
2244 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07002245 return -EINVAL;
2246
2247 if (!driver->name)
2248 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07002249
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002250 driver->gadget_driver = composite_driver_template;
2251 gadget_driver = &driver->gadget_driver;
2252
2253 gadget_driver->function = (char *) driver->name;
2254 gadget_driver->driver.name = driver->name;
2255 gadget_driver->max_speed = driver->max_speed;
2256
2257 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002258}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002259EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07002260
2261/**
2262 * usb_composite_unregister() - unregister a composite driver
2263 * @driver: the driver to unregister
2264 *
2265 * This function is used to unregister drivers using the composite
2266 * driver framework.
2267 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002268void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07002269{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002270 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07002271}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002272EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002273
2274/**
2275 * usb_composite_setup_continue() - Continue with the control transfer
2276 * @cdev: the composite device who's control transfer was kept waiting
2277 *
2278 * This function must be called by the USB function driver to continue
2279 * with the control transfer's data/status stage in case it had requested to
2280 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2281 * can request the composite framework to delay the setup request's data/status
2282 * stages by returning USB_GADGET_DELAYED_STATUS.
2283 */
2284void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2285{
2286 int value;
2287 struct usb_request *req = cdev->req;
2288 unsigned long flags;
2289
2290 DBG(cdev, "%s\n", __func__);
2291 spin_lock_irqsave(&cdev->lock, flags);
2292
2293 if (cdev->delayed_status == 0) {
2294 WARN(cdev, "%s: Unexpected call\n", __func__);
2295
2296 } else if (--cdev->delayed_status == 0) {
2297 DBG(cdev, "%s: Completing delayed status\n", __func__);
2298 req->length = 0;
Felipe Balbi57943712014-09-18 09:54:54 -05002299 req->context = cdev;
Felipe Balbia7c12ea2014-09-18 10:01:55 -05002300 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002301 if (value < 0) {
2302 DBG(cdev, "ep_queue --> %d\n", value);
2303 req->status = 0;
2304 composite_setup_complete(cdev->gadget->ep0, req);
2305 }
2306 }
2307
2308 spin_unlock_irqrestore(&cdev->lock, flags);
2309}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002310EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03002311
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002312static char *composite_default_mfr(struct usb_gadget *gadget)
2313{
2314 char *mfr;
2315 int len;
2316
2317 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2318 init_utsname()->release, gadget->name);
2319 len++;
2320 mfr = kmalloc(len, GFP_KERNEL);
2321 if (!mfr)
2322 return NULL;
2323 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2324 init_utsname()->release, gadget->name);
2325 return mfr;
2326}
2327
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002328void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2329 struct usb_composite_overwrite *covr)
2330{
2331 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002332 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2333 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002334
2335 if (covr->idVendor)
2336 desc->idVendor = cpu_to_le16(covr->idVendor);
2337
2338 if (covr->idProduct)
2339 desc->idProduct = cpu_to_le16(covr->idProduct);
2340
2341 if (covr->bcdDevice)
2342 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02002343
2344 if (covr->serial_number) {
2345 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2346 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2347 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002348 if (covr->manufacturer) {
2349 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2350 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02002351
2352 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2353 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2354 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2355 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02002356 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02002357
2358 if (covr->product) {
2359 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2360 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2361 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02002362}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02002363EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02002364
2365MODULE_LICENSE("GPL");
2366MODULE_AUTHOR("David Brownell");