]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/usb/gadget/configfs.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / drivers / usb / gadget / configfs.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
88af8bbe
SAS
2#include <linux/configfs.h>
3#include <linux/module.h>
4#include <linux/slab.h>
5#include <linux/device.h>
87213d38 6#include <linux/nls.h>
88af8bbe
SAS
7#include <linux/usb/composite.h>
8#include <linux/usb/gadget_configfs.h>
0009e99a 9#include "configfs.h"
da424314 10#include "u_f.h"
7419485f 11#include "u_os_desc.h"
88af8bbe
SAS
12
13int check_user_usb_string(const char *name,
14 struct usb_gadget_strings *stringtab_dev)
15{
16 unsigned primary_lang;
17 unsigned sub_lang;
18 u16 num;
19 int ret;
20
21 ret = kstrtou16(name, 0, &num);
22 if (ret)
23 return ret;
24
25 primary_lang = num & 0x3ff;
26 sub_lang = num >> 10;
27
28 /* simple sanity check for valid langid */
29 switch (primary_lang) {
30 case 0:
31 case 0x62 ... 0xfe:
32 case 0x100 ... 0x3ff:
33 return -EINVAL;
34 }
35 if (!sub_lang)
36 return -EINVAL;
37
38 stringtab_dev->language = num;
39 return 0;
40}
41
42#define MAX_NAME_LEN 40
43#define MAX_USB_STRING_LANGS 2
44
41ce84c8
LJ
45static const struct usb_descriptor_header *otg_desc[2];
46
88af8bbe
SAS
47struct gadget_info {
48 struct config_group group;
49 struct config_group functions_group;
50 struct config_group configs_group;
51 struct config_group strings_group;
87213d38 52 struct config_group os_desc_group;
88af8bbe
SAS
53
54 struct mutex lock;
55 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 struct list_head string_list;
57 struct list_head available_func;
58
88af8bbe
SAS
59 struct usb_composite_driver composite;
60 struct usb_composite_dev cdev;
87213d38
AP
61 bool use_os_desc;
62 char b_vendor_code;
63 char qw_sign[OS_STRING_QW_SIGN_LEN];
1a1c851b
PC
64 spinlock_t spinlock;
65 bool unbind;
88af8bbe
SAS
66};
67
45b6a73f
CH
68static inline struct gadget_info *to_gadget_info(struct config_item *item)
69{
70 return container_of(to_config_group(item), struct gadget_info, group);
71}
72
88af8bbe
SAS
73struct config_usb_cfg {
74 struct config_group group;
75 struct config_group strings_group;
88af8bbe
SAS
76 struct list_head string_list;
77 struct usb_configuration c;
78 struct list_head func_list;
79 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
80};
81
45b6a73f
CH
82static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
83{
84 return container_of(to_config_group(item), struct config_usb_cfg,
85 group);
86}
87
88af8bbe
SAS
88struct gadget_strings {
89 struct usb_gadget_strings stringtab_dev;
90 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
91 char *manufacturer;
92 char *product;
93 char *serialnumber;
94
95 struct config_group group;
96 struct list_head list;
97};
98
87213d38
AP
99struct os_desc {
100 struct config_group group;
101};
102
88af8bbe
SAS
103struct gadget_config_name {
104 struct usb_gadget_strings stringtab_dev;
105 struct usb_string strings;
106 char *configuration;
107
108 struct config_group group;
109 struct list_head list;
110};
111
112static int usb_string_copy(const char *s, char **s_copy)
113{
114 int ret;
115 char *str;
116 char *copy = *s_copy;
117 ret = strlen(s);
118 if (ret > 126)
119 return -EOVERFLOW;
120
121 str = kstrdup(s, GFP_KERNEL);
122 if (!str)
123 return -ENOMEM;
124 if (str[ret - 1] == '\n')
125 str[ret - 1] = '\0';
126 kfree(copy);
127 *s_copy = str;
128 return 0;
129}
130
88af8bbe 131#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
45b6a73f 132static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
88af8bbe
SAS
133 char *page) \
134{ \
45b6a73f
CH
135 return sprintf(page, "0x%02x\n", \
136 to_gadget_info(item)->cdev.desc.__name); \
88af8bbe
SAS
137}
138
139#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
45b6a73f 140static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
88af8bbe
SAS
141 char *page) \
142{ \
45b6a73f
CH
143 return sprintf(page, "0x%04x\n", \
144 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
88af8bbe
SAS
145}
146
147
148#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
45b6a73f 149static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
88af8bbe
SAS
150 const char *page, size_t len) \
151{ \
152 u8 val; \
153 int ret; \
154 ret = kstrtou8(page, 0, &val); \
155 if (ret) \
156 return ret; \
45b6a73f 157 to_gadget_info(item)->cdev.desc._name = val; \
88af8bbe
SAS
158 return len; \
159}
160
161#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
45b6a73f 162static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
88af8bbe
SAS
163 const char *page, size_t len) \
164{ \
165 u16 val; \
166 int ret; \
167 ret = kstrtou16(page, 0, &val); \
168 if (ret) \
169 return ret; \
45b6a73f 170 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
88af8bbe
SAS
171 return len; \
172}
173
174#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
175 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
176 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
177
178GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
179GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
180GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
181GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
182GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
183GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
184GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
185GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
186
187static ssize_t is_valid_bcd(u16 bcd_val)
188{
189 if ((bcd_val & 0xf) > 9)
190 return -EINVAL;
191 if (((bcd_val >> 4) & 0xf) > 9)
192 return -EINVAL;
193 if (((bcd_val >> 8) & 0xf) > 9)
194 return -EINVAL;
195 if (((bcd_val >> 12) & 0xf) > 9)
196 return -EINVAL;
197 return 0;
198}
199
45b6a73f 200static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
88af8bbe
SAS
201 const char *page, size_t len)
202{
203 u16 bcdDevice;
204 int ret;
205
206 ret = kstrtou16(page, 0, &bcdDevice);
207 if (ret)
208 return ret;
209 ret = is_valid_bcd(bcdDevice);
210 if (ret)
211 return ret;
212
45b6a73f 213 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
88af8bbe
SAS
214 return len;
215}
216
45b6a73f 217static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
88af8bbe
SAS
218 const char *page, size_t len)
219{
220 u16 bcdUSB;
221 int ret;
222
223 ret = kstrtou16(page, 0, &bcdUSB);
224 if (ret)
225 return ret;
226 ret = is_valid_bcd(bcdUSB);
227 if (ret)
228 return ret;
229
45b6a73f 230 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
88af8bbe
SAS
231 return len;
232}
233
45b6a73f 234static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
88af8bbe 235{
afdaadc3
RB
236 char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
237
238 return sprintf(page, "%s\n", udc_name ?: "");
88af8bbe
SAS
239}
240
241static int unregister_gadget(struct gadget_info *gi)
242{
243 int ret;
244
afdaadc3 245 if (!gi->composite.gadget_driver.udc_name)
88af8bbe
SAS
246 return -ENODEV;
247
248 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
249 if (ret)
250 return ret;
afdaadc3
RB
251 kfree(gi->composite.gadget_driver.udc_name);
252 gi->composite.gadget_driver.udc_name = NULL;
88af8bbe
SAS
253 return 0;
254}
255
45b6a73f 256static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
88af8bbe
SAS
257 const char *page, size_t len)
258{
45b6a73f 259 struct gadget_info *gi = to_gadget_info(item);
88af8bbe
SAS
260 char *name;
261 int ret;
262
15753588
KK
263 if (strlen(page) < len)
264 return -EOVERFLOW;
265
88af8bbe
SAS
266 name = kstrdup(page, GFP_KERNEL);
267 if (!name)
268 return -ENOMEM;
269 if (name[len - 1] == '\n')
270 name[len - 1] = '\0';
271
272 mutex_lock(&gi->lock);
273
274 if (!strlen(name)) {
275 ret = unregister_gadget(gi);
276 if (ret)
277 goto err;
38355b2a 278 kfree(name);
88af8bbe 279 } else {
afdaadc3 280 if (gi->composite.gadget_driver.udc_name) {
88af8bbe
SAS
281 ret = -EBUSY;
282 goto err;
283 }
afdaadc3
RB
284 gi->composite.gadget_driver.udc_name = name;
285 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
286 if (ret) {
287 gi->composite.gadget_driver.udc_name = NULL;
88af8bbe 288 goto err;
afdaadc3 289 }
88af8bbe
SAS
290 }
291 mutex_unlock(&gi->lock);
292 return len;
293err:
294 kfree(name);
295 mutex_unlock(&gi->lock);
296 return ret;
297}
298
a0249703
TN
299static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
300 char *page)
301{
302 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
303
304 return sprintf(page, "%s\n", usb_speed_string(speed));
305}
306
307static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
308 const char *page, size_t len)
309{
310 struct gadget_info *gi = to_gadget_info(item);
311
312 mutex_lock(&gi->lock);
313
314 /* Prevent changing of max_speed after the driver is binded */
315 if (gi->composite.gadget_driver.udc_name)
316 goto err;
317
318 if (strncmp(page, "super-speed-plus", 16) == 0)
319 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
320 else if (strncmp(page, "super-speed", 11) == 0)
321 gi->composite.max_speed = USB_SPEED_SUPER;
322 else if (strncmp(page, "high-speed", 10) == 0)
323 gi->composite.max_speed = USB_SPEED_HIGH;
324 else if (strncmp(page, "full-speed", 10) == 0)
325 gi->composite.max_speed = USB_SPEED_FULL;
326 else if (strncmp(page, "low-speed", 9) == 0)
327 gi->composite.max_speed = USB_SPEED_LOW;
328 else
329 goto err;
330
331 gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
332
333 mutex_unlock(&gi->lock);
334 return len;
335err:
336 mutex_unlock(&gi->lock);
337 return -EINVAL;
338}
339
45b6a73f
CH
340CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
341CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
342CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
343CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
344CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
345CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
346CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
347CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
348CONFIGFS_ATTR(gadget_dev_desc_, UDC);
a0249703 349CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
88af8bbe
SAS
350
351static struct configfs_attribute *gadget_root_attrs[] = {
45b6a73f
CH
352 &gadget_dev_desc_attr_bDeviceClass,
353 &gadget_dev_desc_attr_bDeviceSubClass,
354 &gadget_dev_desc_attr_bDeviceProtocol,
355 &gadget_dev_desc_attr_bMaxPacketSize0,
356 &gadget_dev_desc_attr_idVendor,
357 &gadget_dev_desc_attr_idProduct,
358 &gadget_dev_desc_attr_bcdDevice,
359 &gadget_dev_desc_attr_bcdUSB,
360 &gadget_dev_desc_attr_UDC,
a0249703 361 &gadget_dev_desc_attr_max_speed,
88af8bbe
SAS
362 NULL,
363};
364
88af8bbe
SAS
365static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
366{
367 return container_of(to_config_group(item), struct gadget_strings,
368 group);
369}
370
371static inline struct gadget_config_name *to_gadget_config_name(
372 struct config_item *item)
373{
374 return container_of(to_config_group(item), struct gadget_config_name,
375 group);
376}
377
88af8bbe
SAS
378static inline struct usb_function_instance *to_usb_function_instance(
379 struct config_item *item)
380{
381 return container_of(to_config_group(item),
382 struct usb_function_instance, group);
383}
384
385static void gadget_info_attr_release(struct config_item *item)
386{
387 struct gadget_info *gi = to_gadget_info(item);
388
389 WARN_ON(!list_empty(&gi->cdev.configs));
390 WARN_ON(!list_empty(&gi->string_list));
391 WARN_ON(!list_empty(&gi->available_func));
392 kfree(gi->composite.gadget_driver.function);
393 kfree(gi);
394}
395
88af8bbe
SAS
396static struct configfs_item_operations gadget_root_item_ops = {
397 .release = gadget_info_attr_release,
88af8bbe
SAS
398};
399
400static void gadget_config_attr_release(struct config_item *item)
401{
402 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
403
404 WARN_ON(!list_empty(&cfg->c.functions));
405 list_del(&cfg->c.list);
406 kfree(cfg->c.label);
407 kfree(cfg);
408}
409
410static int config_usb_cfg_link(
411 struct config_item *usb_cfg_ci,
412 struct config_item *usb_func_ci)
413{
414 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
415 struct usb_composite_dev *cdev = cfg->c.cdev;
416 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
417
418 struct config_group *group = to_config_group(usb_func_ci);
419 struct usb_function_instance *fi = container_of(group,
420 struct usb_function_instance, group);
421 struct usb_function_instance *a_fi;
422 struct usb_function *f;
423 int ret;
424
425 mutex_lock(&gi->lock);
426 /*
427 * Make sure this function is from within our _this_ gadget and not
428 * from another gadget or a random directory.
429 * Also a function instance can only be linked once.
430 */
431 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
432 if (a_fi == fi)
433 break;
434 }
435 if (a_fi != fi) {
436 ret = -EINVAL;
437 goto out;
438 }
439
440 list_for_each_entry(f, &cfg->func_list, list) {
441 if (f->fi == fi) {
442 ret = -EEXIST;
443 goto out;
444 }
445 }
446
447 f = usb_get_function(fi);
448 if (IS_ERR(f)) {
449 ret = PTR_ERR(f);
450 goto out;
451 }
452
453 /* stash the function until we bind it to the gadget */
454 list_add_tail(&f->list, &cfg->func_list);
455 ret = 0;
456out:
457 mutex_unlock(&gi->lock);
458 return ret;
459}
460
e16769d4 461static void config_usb_cfg_unlink(
88af8bbe
SAS
462 struct config_item *usb_cfg_ci,
463 struct config_item *usb_func_ci)
464{
465 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
466 struct usb_composite_dev *cdev = cfg->c.cdev;
467 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
468
469 struct config_group *group = to_config_group(usb_func_ci);
470 struct usb_function_instance *fi = container_of(group,
471 struct usb_function_instance, group);
472 struct usb_function *f;
473
474 /*
475 * ideally I would like to forbid to unlink functions while a gadget is
476 * bound to an UDC. Since this isn't possible at the moment, we simply
477 * force an unbind, the function is available here and then we can
478 * remove the function.
479 */
480 mutex_lock(&gi->lock);
afdaadc3 481 if (gi->composite.gadget_driver.udc_name)
88af8bbe 482 unregister_gadget(gi);
afdaadc3 483 WARN_ON(gi->composite.gadget_driver.udc_name);
88af8bbe
SAS
484
485 list_for_each_entry(f, &cfg->func_list, list) {
486 if (f->fi == fi) {
487 list_del(&f->list);
488 usb_put_function(f);
489 mutex_unlock(&gi->lock);
e16769d4 490 return;
88af8bbe
SAS
491 }
492 }
493 mutex_unlock(&gi->lock);
75bfe23a 494 WARN(1, "Unable to locate function to unbind\n");
88af8bbe
SAS
495}
496
88af8bbe
SAS
497static struct configfs_item_operations gadget_config_item_ops = {
498 .release = gadget_config_attr_release,
88af8bbe
SAS
499 .allow_link = config_usb_cfg_link,
500 .drop_link = config_usb_cfg_unlink,
501};
502
503
45b6a73f 504static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
88af8bbe
SAS
505 char *page)
506{
45b6a73f 507 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
88af8bbe
SAS
508}
509
45b6a73f 510static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
88af8bbe
SAS
511 const char *page, size_t len)
512{
513 u16 val;
514 int ret;
515 ret = kstrtou16(page, 0, &val);
516 if (ret)
517 return ret;
518 if (DIV_ROUND_UP(val, 8) > 0xff)
519 return -ERANGE;
45b6a73f 520 to_config_usb_cfg(item)->c.MaxPower = val;
88af8bbe
SAS
521 return len;
522}
523
45b6a73f 524static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
88af8bbe
SAS
525 char *page)
526{
45b6a73f
CH
527 return sprintf(page, "0x%02x\n",
528 to_config_usb_cfg(item)->c.bmAttributes);
88af8bbe
SAS
529}
530
45b6a73f 531static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
88af8bbe
SAS
532 const char *page, size_t len)
533{
534 u8 val;
535 int ret;
536 ret = kstrtou8(page, 0, &val);
537 if (ret)
538 return ret;
539 if (!(val & USB_CONFIG_ATT_ONE))
540 return -EINVAL;
541 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
542 USB_CONFIG_ATT_WAKEUP))
543 return -EINVAL;
45b6a73f 544 to_config_usb_cfg(item)->c.bmAttributes = val;
88af8bbe
SAS
545 return len;
546}
547
45b6a73f
CH
548CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
549CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
88af8bbe
SAS
550
551static struct configfs_attribute *gadget_config_attrs[] = {
45b6a73f
CH
552 &gadget_config_desc_attr_MaxPower,
553 &gadget_config_desc_attr_bmAttributes,
88af8bbe
SAS
554 NULL,
555};
556
4ad01412 557static const struct config_item_type gadget_config_type = {
88af8bbe
SAS
558 .ct_item_ops = &gadget_config_item_ops,
559 .ct_attrs = gadget_config_attrs,
560 .ct_owner = THIS_MODULE,
561};
562
4ad01412 563static const struct config_item_type gadget_root_type = {
88af8bbe
SAS
564 .ct_item_ops = &gadget_root_item_ops,
565 .ct_attrs = gadget_root_attrs,
566 .ct_owner = THIS_MODULE,
567};
568
569static void composite_init_dev(struct usb_composite_dev *cdev)
570{
571 spin_lock_init(&cdev->lock);
572 INIT_LIST_HEAD(&cdev->configs);
573 INIT_LIST_HEAD(&cdev->gstrings);
574}
575
576static struct config_group *function_make(
577 struct config_group *group,
578 const char *name)
579{
580 struct gadget_info *gi;
581 struct usb_function_instance *fi;
582 char buf[MAX_NAME_LEN];
583 char *func_name;
584 char *instance_name;
585 int ret;
586
587 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
588 if (ret >= MAX_NAME_LEN)
589 return ERR_PTR(-ENAMETOOLONG);
590
591 func_name = buf;
592 instance_name = strchr(func_name, '.');
593 if (!instance_name) {
594 pr_err("Unable to locate . in FUNC.INSTANCE\n");
595 return ERR_PTR(-EINVAL);
596 }
597 *instance_name = '\0';
598 instance_name++;
599
600 fi = usb_get_function_instance(func_name);
601 if (IS_ERR(fi))
a3469411 602 return ERR_CAST(fi);
88af8bbe 603
3958b792 604 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
88af8bbe
SAS
605 if (ret) {
606 usb_put_function_instance(fi);
607 return ERR_PTR(ret);
608 }
1933861d
AP
609 if (fi->set_inst_name) {
610 ret = fi->set_inst_name(fi, instance_name);
611 if (ret) {
612 usb_put_function_instance(fi);
613 return ERR_PTR(ret);
614 }
615 }
88af8bbe
SAS
616
617 gi = container_of(group, struct gadget_info, functions_group);
618
619 mutex_lock(&gi->lock);
620 list_add_tail(&fi->cfs_list, &gi->available_func);
621 mutex_unlock(&gi->lock);
622 return &fi->group;
623}
624
625static void function_drop(
626 struct config_group *group,
627 struct config_item *item)
628{
629 struct usb_function_instance *fi = to_usb_function_instance(item);
630 struct gadget_info *gi;
631
632 gi = container_of(group, struct gadget_info, functions_group);
633
634 mutex_lock(&gi->lock);
635 list_del(&fi->cfs_list);
636 mutex_unlock(&gi->lock);
637 config_item_put(item);
638}
639
640static struct configfs_group_operations functions_ops = {
641 .make_group = &function_make,
642 .drop_item = &function_drop,
643};
644
4ad01412 645static const struct config_item_type functions_type = {
88af8bbe
SAS
646 .ct_group_ops = &functions_ops,
647 .ct_owner = THIS_MODULE,
648};
649
88af8bbe
SAS
650GS_STRINGS_RW(gadget_config_name, configuration);
651
652static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
45b6a73f 653 &gadget_config_name_attr_configuration,
88af8bbe
SAS
654 NULL,
655};
656
657static void gadget_config_name_attr_release(struct config_item *item)
658{
659 struct gadget_config_name *cn = to_gadget_config_name(item);
660
661 kfree(cn->configuration);
662
663 list_del(&cn->list);
664 kfree(cn);
665}
666
667USB_CONFIG_STRING_RW_OPS(gadget_config_name);
668USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
669
670static struct config_group *config_desc_make(
671 struct config_group *group,
672 const char *name)
673{
674 struct gadget_info *gi;
675 struct config_usb_cfg *cfg;
676 char buf[MAX_NAME_LEN];
677 char *num_str;
678 u8 num;
679 int ret;
680
681 gi = container_of(group, struct gadget_info, configs_group);
682 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
683 if (ret >= MAX_NAME_LEN)
684 return ERR_PTR(-ENAMETOOLONG);
685
686 num_str = strchr(buf, '.');
687 if (!num_str) {
688 pr_err("Unable to locate . in name.bConfigurationValue\n");
689 return ERR_PTR(-EINVAL);
690 }
691
692 *num_str = '\0';
693 num_str++;
694
695 if (!strlen(buf))
696 return ERR_PTR(-EINVAL);
697
698 ret = kstrtou8(num_str, 0, &num);
699 if (ret)
700 return ERR_PTR(ret);
701
702 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
703 if (!cfg)
704 return ERR_PTR(-ENOMEM);
705 cfg->c.label = kstrdup(buf, GFP_KERNEL);
706 if (!cfg->c.label) {
707 ret = -ENOMEM;
708 goto err;
709 }
710 cfg->c.bConfigurationValue = num;
711 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
712 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
713 INIT_LIST_HEAD(&cfg->string_list);
714 INIT_LIST_HEAD(&cfg->func_list);
715
88af8bbe
SAS
716 config_group_init_type_name(&cfg->group, name,
717 &gadget_config_type);
1ae1602d 718
88af8bbe
SAS
719 config_group_init_type_name(&cfg->strings_group, "strings",
720 &gadget_config_name_strings_type);
1ae1602d 721 configfs_add_default_group(&cfg->strings_group, &cfg->group);
88af8bbe
SAS
722
723 ret = usb_add_config_only(&gi->cdev, &cfg->c);
724 if (ret)
725 goto err;
726
727 return &cfg->group;
728err:
729 kfree(cfg->c.label);
730 kfree(cfg);
731 return ERR_PTR(ret);
732}
733
734static void config_desc_drop(
735 struct config_group *group,
736 struct config_item *item)
737{
738 config_item_put(item);
739}
740
741static struct configfs_group_operations config_desc_ops = {
742 .make_group = &config_desc_make,
743 .drop_item = &config_desc_drop,
744};
745
4ad01412 746static const struct config_item_type config_desc_type = {
88af8bbe
SAS
747 .ct_group_ops = &config_desc_ops,
748 .ct_owner = THIS_MODULE,
749};
750
88af8bbe
SAS
751GS_STRINGS_RW(gadget_strings, manufacturer);
752GS_STRINGS_RW(gadget_strings, product);
753GS_STRINGS_RW(gadget_strings, serialnumber);
754
755static struct configfs_attribute *gadget_strings_langid_attrs[] = {
45b6a73f
CH
756 &gadget_strings_attr_manufacturer,
757 &gadget_strings_attr_product,
758 &gadget_strings_attr_serialnumber,
88af8bbe
SAS
759 NULL,
760};
761
762static void gadget_strings_attr_release(struct config_item *item)
763{
764 struct gadget_strings *gs = to_gadget_strings(item);
765
766 kfree(gs->manufacturer);
767 kfree(gs->product);
768 kfree(gs->serialnumber);
769
770 list_del(&gs->list);
771 kfree(gs);
772}
773
774USB_CONFIG_STRING_RW_OPS(gadget_strings);
775USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
776
87213d38
AP
777static inline struct os_desc *to_os_desc(struct config_item *item)
778{
779 return container_of(to_config_group(item), struct os_desc, group);
780}
781
45b6a73f
CH
782static inline struct gadget_info *os_desc_item_to_gadget_info(
783 struct config_item *item)
87213d38 784{
45b6a73f
CH
785 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
786}
87213d38 787
45b6a73f
CH
788static ssize_t os_desc_use_show(struct config_item *item, char *page)
789{
e800e8cb 790 return sprintf(page, "%d\n",
45b6a73f 791 os_desc_item_to_gadget_info(item)->use_os_desc);
87213d38
AP
792}
793
45b6a73f 794static ssize_t os_desc_use_store(struct config_item *item, const char *page,
87213d38
AP
795 size_t len)
796{
45b6a73f 797 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
87213d38
AP
798 int ret;
799 bool use;
800
87213d38
AP
801 mutex_lock(&gi->lock);
802 ret = strtobool(page, &use);
803 if (!ret) {
804 gi->use_os_desc = use;
805 ret = len;
806 }
807 mutex_unlock(&gi->lock);
808
809 return ret;
810}
811
45b6a73f 812static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
87213d38 813{
e800e8cb 814 return sprintf(page, "0x%02x\n",
45b6a73f 815 os_desc_item_to_gadget_info(item)->b_vendor_code);
87213d38
AP
816}
817
45b6a73f 818static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
87213d38
AP
819 const char *page, size_t len)
820{
45b6a73f 821 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
87213d38
AP
822 int ret;
823 u8 b_vendor_code;
824
87213d38
AP
825 mutex_lock(&gi->lock);
826 ret = kstrtou8(page, 0, &b_vendor_code);
827 if (!ret) {
828 gi->b_vendor_code = b_vendor_code;
829 ret = len;
830 }
831 mutex_unlock(&gi->lock);
832
833 return ret;
834}
835
45b6a73f 836static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
87213d38 837{
45b6a73f 838 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
76180d71 839 int res;
87213d38 840
76180d71
SA
841 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
842 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
843 page[res++] = '\n';
844
845 return res;
87213d38
AP
846}
847
45b6a73f 848static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
87213d38
AP
849 size_t len)
850{
45b6a73f 851 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
87213d38
AP
852 int res, l;
853
87213d38
AP
854 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
855 if (page[l - 1] == '\n')
856 --l;
857
858 mutex_lock(&gi->lock);
859 res = utf8s_to_utf16s(page, l,
860 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
861 OS_STRING_QW_SIGN_LEN);
862 if (res > 0)
863 res = len;
864 mutex_unlock(&gi->lock);
865
866 return res;
867}
868
45b6a73f
CH
869CONFIGFS_ATTR(os_desc_, use);
870CONFIGFS_ATTR(os_desc_, b_vendor_code);
871CONFIGFS_ATTR(os_desc_, qw_sign);
87213d38
AP
872
873static struct configfs_attribute *os_desc_attrs[] = {
45b6a73f
CH
874 &os_desc_attr_use,
875 &os_desc_attr_b_vendor_code,
876 &os_desc_attr_qw_sign,
87213d38
AP
877 NULL,
878};
879
880static void os_desc_attr_release(struct config_item *item)
881{
882 struct os_desc *os_desc = to_os_desc(item);
883 kfree(os_desc);
884}
885
da424314
AP
886static int os_desc_link(struct config_item *os_desc_ci,
887 struct config_item *usb_cfg_ci)
888{
889 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
890 struct gadget_info, os_desc_group);
891 struct usb_composite_dev *cdev = &gi->cdev;
892 struct config_usb_cfg *c_target =
893 container_of(to_config_group(usb_cfg_ci),
894 struct config_usb_cfg, group);
895 struct usb_configuration *c;
896 int ret;
897
898 mutex_lock(&gi->lock);
899 list_for_each_entry(c, &cdev->configs, list) {
900 if (c == &c_target->c)
901 break;
902 }
903 if (c != &c_target->c) {
904 ret = -EINVAL;
905 goto out;
906 }
907
908 if (cdev->os_desc_config) {
909 ret = -EBUSY;
910 goto out;
911 }
912
913 cdev->os_desc_config = &c_target->c;
914 ret = 0;
915
916out:
917 mutex_unlock(&gi->lock);
918 return ret;
919}
920
e16769d4 921static void os_desc_unlink(struct config_item *os_desc_ci,
da424314
AP
922 struct config_item *usb_cfg_ci)
923{
924 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
925 struct gadget_info, os_desc_group);
926 struct usb_composite_dev *cdev = &gi->cdev;
927
928 mutex_lock(&gi->lock);
afdaadc3 929 if (gi->composite.gadget_driver.udc_name)
da424314
AP
930 unregister_gadget(gi);
931 cdev->os_desc_config = NULL;
afdaadc3 932 WARN_ON(gi->composite.gadget_driver.udc_name);
da424314 933 mutex_unlock(&gi->lock);
da424314
AP
934}
935
87213d38
AP
936static struct configfs_item_operations os_desc_ops = {
937 .release = os_desc_attr_release,
da424314
AP
938 .allow_link = os_desc_link,
939 .drop_link = os_desc_unlink,
87213d38
AP
940};
941
942static struct config_item_type os_desc_type = {
943 .ct_item_ops = &os_desc_ops,
944 .ct_attrs = os_desc_attrs,
945 .ct_owner = THIS_MODULE,
946};
947
7419485f
AP
948static inline struct usb_os_desc_ext_prop
949*to_usb_os_desc_ext_prop(struct config_item *item)
950{
951 return container_of(item, struct usb_os_desc_ext_prop, item);
952}
953
45b6a73f 954static ssize_t ext_prop_type_show(struct config_item *item, char *page)
7419485f 955{
e800e8cb 956 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
7419485f
AP
957}
958
45b6a73f 959static ssize_t ext_prop_type_store(struct config_item *item,
7419485f
AP
960 const char *page, size_t len)
961{
45b6a73f 962 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
7419485f
AP
963 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
964 u8 type;
965 int ret;
966
967 if (desc->opts_mutex)
968 mutex_lock(desc->opts_mutex);
969 ret = kstrtou8(page, 0, &type);
970 if (ret)
971 goto end;
972 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
973 ret = -EINVAL;
974 goto end;
975 }
976
977 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
978 ext_prop->type == USB_EXT_PROP_LE32 ||
979 ext_prop->type == USB_EXT_PROP_BE32) &&
980 (type == USB_EXT_PROP_UNICODE ||
981 type == USB_EXT_PROP_UNICODE_ENV ||
982 type == USB_EXT_PROP_UNICODE_LINK))
983 ext_prop->data_len <<= 1;
984 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
985 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
986 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
987 (type == USB_EXT_PROP_BINARY ||
988 type == USB_EXT_PROP_LE32 ||
989 type == USB_EXT_PROP_BE32))
990 ext_prop->data_len >>= 1;
991 ext_prop->type = type;
992 ret = len;
993
994end:
995 if (desc->opts_mutex)
996 mutex_unlock(desc->opts_mutex);
997 return ret;
998}
999
45b6a73f 1000static ssize_t ext_prop_data_show(struct config_item *item, char *page)
7419485f 1001{
45b6a73f 1002 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
7419485f
AP
1003 int len = ext_prop->data_len;
1004
1005 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1006 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1007 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1008 len >>= 1;
1009 memcpy(page, ext_prop->data, len);
1010
1011 return len;
1012}
1013
45b6a73f 1014static ssize_t ext_prop_data_store(struct config_item *item,
7419485f
AP
1015 const char *page, size_t len)
1016{
45b6a73f 1017 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
7419485f
AP
1018 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1019 char *new_data;
1020 size_t ret_len = len;
1021
1022 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1023 --len;
58b949e0 1024 new_data = kmemdup(page, len, GFP_KERNEL);
7419485f
AP
1025 if (!new_data)
1026 return -ENOMEM;
1027
7419485f
AP
1028 if (desc->opts_mutex)
1029 mutex_lock(desc->opts_mutex);
1030 kfree(ext_prop->data);
1031 ext_prop->data = new_data;
1032 desc->ext_prop_len -= ext_prop->data_len;
1033 ext_prop->data_len = len;
1034 desc->ext_prop_len += ext_prop->data_len;
1035 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1036 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1037 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1038 desc->ext_prop_len -= ext_prop->data_len;
1039 ext_prop->data_len <<= 1;
1040 ext_prop->data_len += 2;
1041 desc->ext_prop_len += ext_prop->data_len;
1042 }
1043 if (desc->opts_mutex)
1044 mutex_unlock(desc->opts_mutex);
1045 return ret_len;
1046}
1047
45b6a73f
CH
1048CONFIGFS_ATTR(ext_prop_, type);
1049CONFIGFS_ATTR(ext_prop_, data);
7419485f
AP
1050
1051static struct configfs_attribute *ext_prop_attrs[] = {
45b6a73f
CH
1052 &ext_prop_attr_type,
1053 &ext_prop_attr_data,
7419485f
AP
1054 NULL,
1055};
1056
1057static void usb_os_desc_ext_prop_release(struct config_item *item)
1058{
1059 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1060
1061 kfree(ext_prop); /* frees a whole chunk */
1062}
1063
1064static struct configfs_item_operations ext_prop_ops = {
1065 .release = usb_os_desc_ext_prop_release,
7419485f
AP
1066};
1067
1068static struct config_item *ext_prop_make(
1069 struct config_group *group,
1070 const char *name)
1071{
1072 struct usb_os_desc_ext_prop *ext_prop;
1073 struct config_item_type *ext_prop_type;
1074 struct usb_os_desc *desc;
1075 char *vlabuf;
1076
1077 vla_group(data_chunk);
1078 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1079 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1080
1081 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1082 if (!vlabuf)
1083 return ERR_PTR(-ENOMEM);
1084
1085 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1086 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1087
1088 desc = container_of(group, struct usb_os_desc, group);
1089 ext_prop_type->ct_item_ops = &ext_prop_ops;
1090 ext_prop_type->ct_attrs = ext_prop_attrs;
1091 ext_prop_type->ct_owner = desc->owner;
1092
1093 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1094
1095 ext_prop->name = kstrdup(name, GFP_KERNEL);
1096 if (!ext_prop->name) {
1097 kfree(vlabuf);
1098 return ERR_PTR(-ENOMEM);
1099 }
1100 desc->ext_prop_len += 14;
1101 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1102 if (desc->opts_mutex)
1103 mutex_lock(desc->opts_mutex);
1104 desc->ext_prop_len += ext_prop->name_len;
1105 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1106 ++desc->ext_prop_count;
1107 if (desc->opts_mutex)
1108 mutex_unlock(desc->opts_mutex);
1109
1110 return &ext_prop->item;
1111}
1112
1113static void ext_prop_drop(struct config_group *group, struct config_item *item)
1114{
1115 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1116 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1117
1118 if (desc->opts_mutex)
1119 mutex_lock(desc->opts_mutex);
1120 list_del(&ext_prop->entry);
1121 --desc->ext_prop_count;
1122 kfree(ext_prop->name);
1123 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1124 if (desc->opts_mutex)
1125 mutex_unlock(desc->opts_mutex);
1126 config_item_put(item);
1127}
1128
1129static struct configfs_group_operations interf_grp_ops = {
1130 .make_item = &ext_prop_make,
1131 .drop_item = &ext_prop_drop,
1132};
1133
45b6a73f 1134static ssize_t interf_grp_compatible_id_show(struct config_item *item,
fe00b138 1135 char *page)
da424314 1136{
45b6a73f 1137 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
da424314
AP
1138 return 8;
1139}
1140
45b6a73f 1141static ssize_t interf_grp_compatible_id_store(struct config_item *item,
fe00b138 1142 const char *page, size_t len)
da424314 1143{
45b6a73f 1144 struct usb_os_desc *desc = to_usb_os_desc(item);
da424314
AP
1145 int l;
1146
1147 l = min_t(int, 8, len);
1148 if (page[l - 1] == '\n')
1149 --l;
1150 if (desc->opts_mutex)
1151 mutex_lock(desc->opts_mutex);
1152 memcpy(desc->ext_compat_id, page, l);
da424314
AP
1153
1154 if (desc->opts_mutex)
1155 mutex_unlock(desc->opts_mutex);
1156
1157 return len;
1158}
1159
45b6a73f 1160static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
fe00b138 1161 char *page)
da424314 1162{
45b6a73f 1163 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
da424314
AP
1164 return 8;
1165}
1166
45b6a73f 1167static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
fe00b138 1168 const char *page, size_t len)
da424314 1169{
45b6a73f 1170 struct usb_os_desc *desc = to_usb_os_desc(item);
da424314
AP
1171 int l;
1172
1173 l = min_t(int, 8, len);
1174 if (page[l - 1] == '\n')
1175 --l;
1176 if (desc->opts_mutex)
1177 mutex_lock(desc->opts_mutex);
1178 memcpy(desc->ext_compat_id + 8, page, l);
da424314
AP
1179
1180 if (desc->opts_mutex)
1181 mutex_unlock(desc->opts_mutex);
1182
1183 return len;
1184}
1185
45b6a73f
CH
1186CONFIGFS_ATTR(interf_grp_, compatible_id);
1187CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
da424314
AP
1188
1189static struct configfs_attribute *interf_grp_attrs[] = {
45b6a73f
CH
1190 &interf_grp_attr_compatible_id,
1191 &interf_grp_attr_sub_compatible_id,
da424314
AP
1192 NULL
1193};
1194
ff74745e
AG
1195struct config_group *usb_os_desc_prepare_interf_dir(
1196 struct config_group *parent,
1197 int n_interf,
1198 struct usb_os_desc **desc,
1199 char **names,
1200 struct module *owner)
da424314 1201{
1ae1602d 1202 struct config_group *os_desc_group;
da424314
AP
1203 struct config_item_type *os_desc_type, *interface_type;
1204
1205 vla_group(data_chunk);
da424314 1206 vla_item(data_chunk, struct config_group, os_desc_group, 1);
da424314
AP
1207 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1208 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1209
1210 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1211 if (!vlabuf)
ff74745e 1212 return ERR_PTR(-ENOMEM);
da424314 1213
da424314
AP
1214 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1215 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
da424314
AP
1216 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1217
da424314
AP
1218 os_desc_type->ct_owner = owner;
1219 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1ae1602d 1220 configfs_add_default_group(os_desc_group, parent);
da424314 1221
7419485f 1222 interface_type->ct_group_ops = &interf_grp_ops;
da424314
AP
1223 interface_type->ct_attrs = interf_grp_attrs;
1224 interface_type->ct_owner = owner;
1225
1226 while (n_interf--) {
1227 struct usb_os_desc *d;
1228
1229 d = desc[n_interf];
7419485f 1230 d->owner = owner;
da424314 1231 config_group_init_type_name(&d->group, "", interface_type);
14574b54
AP
1232 config_item_set_name(&d->group.cg_item, "interface.%s",
1233 names[n_interf]);
1ae1602d 1234 configfs_add_default_group(&d->group, os_desc_group);
da424314
AP
1235 }
1236
ff74745e 1237 return os_desc_group;
da424314
AP
1238}
1239EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1240
88af8bbe
SAS
1241static int configfs_do_nothing(struct usb_composite_dev *cdev)
1242{
75bfe23a 1243 WARN_ON(1);
88af8bbe
SAS
1244 return -EINVAL;
1245}
1246
1247int composite_dev_prepare(struct usb_composite_driver *composite,
1248 struct usb_composite_dev *dev);
1249
da424314
AP
1250int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1251 struct usb_ep *ep0);
1252
88af8bbe
SAS
1253static void purge_configs_funcs(struct gadget_info *gi)
1254{
1255 struct usb_configuration *c;
1256
1257 list_for_each_entry(c, &gi->cdev.configs, list) {
1258 struct usb_function *f, *tmp;
1259 struct config_usb_cfg *cfg;
1260
1261 cfg = container_of(c, struct config_usb_cfg, c);
1262
1263 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1264
1265 list_move_tail(&f->list, &cfg->func_list);
1266 if (f->unbind) {
da7b895d 1267 dev_dbg(&gi->cdev.gadget->dev,
ad22a666
PS
1268 "unbind function '%s'/%p\n",
1269 f->name, f);
88af8bbe
SAS
1270 f->unbind(c, f);
1271 }
1272 }
1273 c->next_interface_id = 0;
903124fe 1274 memset(c->interface, 0, sizeof(c->interface));
554eead5 1275 c->superspeed_plus = 0;
88af8bbe
SAS
1276 c->superspeed = 0;
1277 c->highspeed = 0;
1278 c->fullspeed = 0;
1279 }
1280}
1281
1282static int configfs_composite_bind(struct usb_gadget *gadget,
1283 struct usb_gadget_driver *gdriver)
1284{
1285 struct usb_composite_driver *composite = to_cdriver(gdriver);
1286 struct gadget_info *gi = container_of(composite,
1287 struct gadget_info, composite);
1288 struct usb_composite_dev *cdev = &gi->cdev;
1289 struct usb_configuration *c;
1290 struct usb_string *s;
1291 unsigned i;
1292 int ret;
1293
1294 /* the gi->lock is hold by the caller */
1a1c851b 1295 gi->unbind = 0;
88af8bbe
SAS
1296 cdev->gadget = gadget;
1297 set_gadget_data(gadget, cdev);
1298 ret = composite_dev_prepare(composite, cdev);
1299 if (ret)
1300 return ret;
1301 /* and now the gadget bind */
1302 ret = -EINVAL;
1303
1304 if (list_empty(&gi->cdev.configs)) {
4d9f872c 1305 pr_err("Need at least one configuration in %s.\n",
88af8bbe
SAS
1306 gi->composite.name);
1307 goto err_comp_cleanup;
1308 }
1309
1310
1311 list_for_each_entry(c, &gi->cdev.configs, list) {
1312 struct config_usb_cfg *cfg;
1313
1314 cfg = container_of(c, struct config_usb_cfg, c);
1315 if (list_empty(&cfg->func_list)) {
4d9f872c 1316 pr_err("Config %s/%d of %s needs at least one function.\n",
88af8bbe
SAS
1317 c->label, c->bConfigurationValue,
1318 gi->composite.name);
1319 goto err_comp_cleanup;
1320 }
1321 }
1322
1323 /* init all strings */
1324 if (!list_empty(&gi->string_list)) {
1325 struct gadget_strings *gs;
1326
1327 i = 0;
1328 list_for_each_entry(gs, &gi->string_list, list) {
1329
1330 gi->gstrings[i] = &gs->stringtab_dev;
1331 gs->stringtab_dev.strings = gs->strings;
1332 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1333 gs->manufacturer;
1334 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1335 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1336 i++;
1337 }
1338 gi->gstrings[i] = NULL;
1339 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1340 USB_GADGET_FIRST_AVAIL_IDX);
fea77077
WY
1341 if (IS_ERR(s)) {
1342 ret = PTR_ERR(s);
88af8bbe 1343 goto err_comp_cleanup;
fea77077 1344 }
88af8bbe
SAS
1345
1346 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1347 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1348 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1349 }
1350
87213d38
AP
1351 if (gi->use_os_desc) {
1352 cdev->use_os_string = true;
1353 cdev->b_vendor_code = gi->b_vendor_code;
1354 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1355 }
1356
41ce84c8
LJ
1357 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1358 struct usb_descriptor_header *usb_desc;
1359
1360 usb_desc = usb_otg_descriptor_alloc(gadget);
1361 if (!usb_desc) {
1362 ret = -ENOMEM;
1363 goto err_comp_cleanup;
1364 }
1365 usb_otg_descriptor_init(gadget, usb_desc);
1366 otg_desc[0] = usb_desc;
1367 otg_desc[1] = NULL;
1368 }
1369
88af8bbe
SAS
1370 /* Go through all configs, attach all functions */
1371 list_for_each_entry(c, &gi->cdev.configs, list) {
1372 struct config_usb_cfg *cfg;
1373 struct usb_function *f;
1374 struct usb_function *tmp;
1375 struct gadget_config_name *cn;
1376
41ce84c8
LJ
1377 if (gadget_is_otg(gadget))
1378 c->descriptors = otg_desc;
1379
88af8bbe
SAS
1380 cfg = container_of(c, struct config_usb_cfg, c);
1381 if (!list_empty(&cfg->string_list)) {
1382 i = 0;
1383 list_for_each_entry(cn, &cfg->string_list, list) {
1384 cfg->gstrings[i] = &cn->stringtab_dev;
1385 cn->stringtab_dev.strings = &cn->strings;
1386 cn->strings.s = cn->configuration;
1387 i++;
1388 }
1389 cfg->gstrings[i] = NULL;
1390 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
fea77077
WY
1391 if (IS_ERR(s)) {
1392 ret = PTR_ERR(s);
88af8bbe 1393 goto err_comp_cleanup;
fea77077 1394 }
88af8bbe
SAS
1395 c->iConfiguration = s[0].id;
1396 }
1397
1398 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1399 list_del(&f->list);
1400 ret = usb_add_function(c, f);
5a68e9b5
AP
1401 if (ret) {
1402 list_add(&f->list, &cfg->func_list);
88af8bbe 1403 goto err_purge_funcs;
5a68e9b5 1404 }
88af8bbe
SAS
1405 }
1406 usb_ep_autoconfig_reset(cdev->gadget);
1407 }
da424314
AP
1408 if (cdev->use_os_string) {
1409 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1410 if (ret)
1411 goto err_purge_funcs;
1412 }
1413
88af8bbe
SAS
1414 usb_ep_autoconfig_reset(cdev->gadget);
1415 return 0;
1416
1417err_purge_funcs:
1418 purge_configs_funcs(gi);
1419err_comp_cleanup:
1420 composite_dev_cleanup(cdev);
1421 return ret;
1422}
1423
1424static void configfs_composite_unbind(struct usb_gadget *gadget)
1425{
1426 struct usb_composite_dev *cdev;
1427 struct gadget_info *gi;
1a1c851b 1428 unsigned long flags;
88af8bbe
SAS
1429
1430 /* the gi->lock is hold by the caller */
1431
1432 cdev = get_gadget_data(gadget);
1433 gi = container_of(cdev, struct gadget_info, cdev);
1a1c851b
PC
1434 spin_lock_irqsave(&gi->spinlock, flags);
1435 gi->unbind = 1;
1436 spin_unlock_irqrestore(&gi->spinlock, flags);
88af8bbe 1437
41ce84c8
LJ
1438 kfree(otg_desc[0]);
1439 otg_desc[0] = NULL;
88af8bbe
SAS
1440 purge_configs_funcs(gi);
1441 composite_dev_cleanup(cdev);
1442 usb_ep_autoconfig_reset(cdev->gadget);
1a1c851b 1443 spin_lock_irqsave(&gi->spinlock, flags);
88af8bbe
SAS
1444 cdev->gadget = NULL;
1445 set_gadget_data(gadget, NULL);
1a1c851b
PC
1446 spin_unlock_irqrestore(&gi->spinlock, flags);
1447}
1448
1449static int configfs_composite_setup(struct usb_gadget *gadget,
1450 const struct usb_ctrlrequest *ctrl)
1451{
1452 struct usb_composite_dev *cdev;
1453 struct gadget_info *gi;
1454 unsigned long flags;
1455 int ret;
1456
1457 cdev = get_gadget_data(gadget);
1458 if (!cdev)
1459 return 0;
1460
1461 gi = container_of(cdev, struct gadget_info, cdev);
1462 spin_lock_irqsave(&gi->spinlock, flags);
1463 cdev = get_gadget_data(gadget);
1464 if (!cdev || gi->unbind) {
1465 spin_unlock_irqrestore(&gi->spinlock, flags);
1466 return 0;
1467 }
1468
1469 ret = composite_setup(gadget, ctrl);
1470 spin_unlock_irqrestore(&gi->spinlock, flags);
1471 return ret;
1472}
1473
1474static void configfs_composite_disconnect(struct usb_gadget *gadget)
1475{
1476 struct usb_composite_dev *cdev;
1477 struct gadget_info *gi;
1478 unsigned long flags;
1479
1480 cdev = get_gadget_data(gadget);
1481 if (!cdev)
1482 return;
1483
1484 gi = container_of(cdev, struct gadget_info, cdev);
1485 spin_lock_irqsave(&gi->spinlock, flags);
1486 cdev = get_gadget_data(gadget);
1487 if (!cdev || gi->unbind) {
1488 spin_unlock_irqrestore(&gi->spinlock, flags);
1489 return;
1490 }
1491
1492 composite_disconnect(gadget);
1493 spin_unlock_irqrestore(&gi->spinlock, flags);
1494}
1495
1496static void configfs_composite_suspend(struct usb_gadget *gadget)
1497{
1498 struct usb_composite_dev *cdev;
1499 struct gadget_info *gi;
1500 unsigned long flags;
1501
1502 cdev = get_gadget_data(gadget);
1503 if (!cdev)
1504 return;
1505
1506 gi = container_of(cdev, struct gadget_info, cdev);
1507 spin_lock_irqsave(&gi->spinlock, flags);
1508 cdev = get_gadget_data(gadget);
1509 if (!cdev || gi->unbind) {
1510 spin_unlock_irqrestore(&gi->spinlock, flags);
1511 return;
1512 }
1513
1514 composite_suspend(gadget);
1515 spin_unlock_irqrestore(&gi->spinlock, flags);
1516}
1517
1518static void configfs_composite_resume(struct usb_gadget *gadget)
1519{
1520 struct usb_composite_dev *cdev;
1521 struct gadget_info *gi;
1522 unsigned long flags;
1523
1524 cdev = get_gadget_data(gadget);
1525 if (!cdev)
1526 return;
1527
1528 gi = container_of(cdev, struct gadget_info, cdev);
1529 spin_lock_irqsave(&gi->spinlock, flags);
1530 cdev = get_gadget_data(gadget);
1531 if (!cdev || gi->unbind) {
1532 spin_unlock_irqrestore(&gi->spinlock, flags);
1533 return;
1534 }
1535
1536 composite_resume(gadget);
1537 spin_unlock_irqrestore(&gi->spinlock, flags);
88af8bbe
SAS
1538}
1539
1540static const struct usb_gadget_driver configfs_driver_template = {
1541 .bind = configfs_composite_bind,
1542 .unbind = configfs_composite_unbind,
1543
1a1c851b
PC
1544 .setup = configfs_composite_setup,
1545 .reset = configfs_composite_disconnect,
1546 .disconnect = configfs_composite_disconnect,
88af8bbe 1547
1a1c851b
PC
1548 .suspend = configfs_composite_suspend,
1549 .resume = configfs_composite_resume,
3a571870 1550
88af8bbe
SAS
1551 .max_speed = USB_SPEED_SUPER,
1552 .driver = {
1553 .owner = THIS_MODULE,
1554 .name = "configfs-gadget",
1555 },
f1bddbb3 1556 .match_existing_only = 1,
88af8bbe
SAS
1557};
1558
1559static struct config_group *gadgets_make(
1560 struct config_group *group,
1561 const char *name)
1562{
1563 struct gadget_info *gi;
1564
1565 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1566 if (!gi)
1567 return ERR_PTR(-ENOMEM);
1568
1ae1602d 1569 config_group_init_type_name(&gi->group, name, &gadget_root_type);
88af8bbe
SAS
1570
1571 config_group_init_type_name(&gi->functions_group, "functions",
1572 &functions_type);
1ae1602d
CH
1573 configfs_add_default_group(&gi->functions_group, &gi->group);
1574
88af8bbe
SAS
1575 config_group_init_type_name(&gi->configs_group, "configs",
1576 &config_desc_type);
1ae1602d
CH
1577 configfs_add_default_group(&gi->configs_group, &gi->group);
1578
88af8bbe
SAS
1579 config_group_init_type_name(&gi->strings_group, "strings",
1580 &gadget_strings_strings_type);
1ae1602d
CH
1581 configfs_add_default_group(&gi->strings_group, &gi->group);
1582
87213d38
AP
1583 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1584 &os_desc_type);
1ae1602d 1585 configfs_add_default_group(&gi->os_desc_group, &gi->group);
88af8bbe
SAS
1586
1587 gi->composite.bind = configfs_do_nothing;
1588 gi->composite.unbind = configfs_do_nothing;
1589 gi->composite.suspend = NULL;
1590 gi->composite.resume = NULL;
1591 gi->composite.max_speed = USB_SPEED_SUPER;
1592
093edc2b 1593 spin_lock_init(&gi->spinlock);
88af8bbe
SAS
1594 mutex_init(&gi->lock);
1595 INIT_LIST_HEAD(&gi->string_list);
1596 INIT_LIST_HEAD(&gi->available_func);
1597
1598 composite_init_dev(&gi->cdev);
1599 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1600 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1601 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1602
1603 gi->composite.gadget_driver = configfs_driver_template;
1604
1605 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1606 gi->composite.name = gi->composite.gadget_driver.function;
1607
1608 if (!gi->composite.gadget_driver.function)
1609 goto err;
1610
88af8bbe
SAS
1611 return &gi->group;
1612err:
1613 kfree(gi);
1614 return ERR_PTR(-ENOMEM);
1615}
1616
1617static void gadgets_drop(struct config_group *group, struct config_item *item)
1618{
1619 config_item_put(item);
1620}
1621
1622static struct configfs_group_operations gadgets_ops = {
1623 .make_group = &gadgets_make,
1624 .drop_item = &gadgets_drop,
1625};
1626
4ad01412 1627static const struct config_item_type gadgets_type = {
88af8bbe
SAS
1628 .ct_group_ops = &gadgets_ops,
1629 .ct_owner = THIS_MODULE,
1630};
1631
1632static struct configfs_subsystem gadget_subsys = {
1633 .su_group = {
1634 .cg_item = {
1635 .ci_namebuf = "usb_gadget",
1636 .ci_type = &gadgets_type,
1637 },
1638 },
1639 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1640};
1641
092a4bd0
AP
1642void unregister_gadget_item(struct config_item *item)
1643{
1644 struct gadget_info *gi = to_gadget_info(item);
1645
cee51c33 1646 mutex_lock(&gi->lock);
092a4bd0 1647 unregister_gadget(gi);
cee51c33 1648 mutex_unlock(&gi->lock);
092a4bd0 1649}
0700faaf 1650EXPORT_SYMBOL_GPL(unregister_gadget_item);
092a4bd0 1651
88af8bbe
SAS
1652static int __init gadget_cfs_init(void)
1653{
1654 int ret;
1655
1656 config_group_init(&gadget_subsys.su_group);
1657
1658 ret = configfs_register_subsystem(&gadget_subsys);
1659 return ret;
1660}
1661module_init(gadget_cfs_init);
1662
1663static void __exit gadget_cfs_exit(void)
1664{
1665 configfs_unregister_subsystem(&gadget_subsys);
1666}
1667module_exit(gadget_cfs_exit);