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