Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / gadget / configfs.c
CommitLineData
88af8bbe
SAS
1#include <linux/configfs.h>
2#include <linux/module.h>
3#include <linux/slab.h>
4#include <linux/device.h>
5#include <linux/usb/composite.h>
6#include <linux/usb/gadget_configfs.h>
7
8int check_user_usb_string(const char *name,
9 struct usb_gadget_strings *stringtab_dev)
10{
11 unsigned primary_lang;
12 unsigned sub_lang;
13 u16 num;
14 int ret;
15
16 ret = kstrtou16(name, 0, &num);
17 if (ret)
18 return ret;
19
20 primary_lang = num & 0x3ff;
21 sub_lang = num >> 10;
22
23 /* simple sanity check for valid langid */
24 switch (primary_lang) {
25 case 0:
26 case 0x62 ... 0xfe:
27 case 0x100 ... 0x3ff:
28 return -EINVAL;
29 }
30 if (!sub_lang)
31 return -EINVAL;
32
33 stringtab_dev->language = num;
34 return 0;
35}
36
37#define MAX_NAME_LEN 40
38#define MAX_USB_STRING_LANGS 2
39
40struct gadget_info {
41 struct config_group group;
42 struct config_group functions_group;
43 struct config_group configs_group;
44 struct config_group strings_group;
45 struct config_group *default_groups[4];
46
47 struct mutex lock;
48 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
49 struct list_head string_list;
50 struct list_head available_func;
51
52 const char *udc_name;
53#ifdef CONFIG_USB_OTG
54 struct usb_otg_descriptor otg;
55#endif
56 struct usb_composite_driver composite;
57 struct usb_composite_dev cdev;
58};
59
60struct config_usb_cfg {
61 struct config_group group;
62 struct config_group strings_group;
63 struct config_group *default_groups[2];
64 struct list_head string_list;
65 struct usb_configuration c;
66 struct list_head func_list;
67 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
68};
69
70struct gadget_strings {
71 struct usb_gadget_strings stringtab_dev;
72 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
73 char *manufacturer;
74 char *product;
75 char *serialnumber;
76
77 struct config_group group;
78 struct list_head list;
79};
80
81struct gadget_config_name {
82 struct usb_gadget_strings stringtab_dev;
83 struct usb_string strings;
84 char *configuration;
85
86 struct config_group group;
87 struct list_head list;
88};
89
90static int usb_string_copy(const char *s, char **s_copy)
91{
92 int ret;
93 char *str;
94 char *copy = *s_copy;
95 ret = strlen(s);
96 if (ret > 126)
97 return -EOVERFLOW;
98
99 str = kstrdup(s, GFP_KERNEL);
100 if (!str)
101 return -ENOMEM;
102 if (str[ret - 1] == '\n')
103 str[ret - 1] = '\0';
104 kfree(copy);
105 *s_copy = str;
106 return 0;
107}
108
109CONFIGFS_ATTR_STRUCT(gadget_info);
110CONFIGFS_ATTR_STRUCT(config_usb_cfg);
111
112#define GI_DEVICE_DESC_ITEM_ATTR(name) \
113 static struct gadget_info_attribute gadget_cdev_desc_##name = \
114 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
115 gadget_dev_desc_##name##_show, \
116 gadget_dev_desc_##name##_store)
117
118#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
119 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
120 char *page) \
121{ \
122 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
123}
124
125#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
126 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
127 char *page) \
128{ \
129 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
130}
131
132
133#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
134 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
135 const char *page, size_t len) \
136{ \
137 u8 val; \
138 int ret; \
139 ret = kstrtou8(page, 0, &val); \
140 if (ret) \
141 return ret; \
142 gi->cdev.desc._name = val; \
143 return len; \
144}
145
146#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
147 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
148 const char *page, size_t len) \
149{ \
150 u16 val; \
151 int ret; \
152 ret = kstrtou16(page, 0, &val); \
153 if (ret) \
154 return ret; \
155 gi->cdev.desc._name = cpu_to_le16p(&val); \
156 return len; \
157}
158
159#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
160 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
161 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
162
163GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
164GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
165GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
166GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
167GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
168GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
169GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
170GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
171
172static ssize_t is_valid_bcd(u16 bcd_val)
173{
174 if ((bcd_val & 0xf) > 9)
175 return -EINVAL;
176 if (((bcd_val >> 4) & 0xf) > 9)
177 return -EINVAL;
178 if (((bcd_val >> 8) & 0xf) > 9)
179 return -EINVAL;
180 if (((bcd_val >> 12) & 0xf) > 9)
181 return -EINVAL;
182 return 0;
183}
184
185static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
186 const char *page, size_t len)
187{
188 u16 bcdDevice;
189 int ret;
190
191 ret = kstrtou16(page, 0, &bcdDevice);
192 if (ret)
193 return ret;
194 ret = is_valid_bcd(bcdDevice);
195 if (ret)
196 return ret;
197
198 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
199 return len;
200}
201
202static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
203 const char *page, size_t len)
204{
205 u16 bcdUSB;
206 int ret;
207
208 ret = kstrtou16(page, 0, &bcdUSB);
209 if (ret)
210 return ret;
211 ret = is_valid_bcd(bcdUSB);
212 if (ret)
213 return ret;
214
215 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
216 return len;
217}
218
219static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
220{
221 return sprintf(page, "%s\n", gi->udc_name ?: "");
222}
223
224static int unregister_gadget(struct gadget_info *gi)
225{
226 int ret;
227
228 if (!gi->udc_name)
229 return -ENODEV;
230
231 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
232 if (ret)
233 return ret;
234 kfree(gi->udc_name);
235 gi->udc_name = NULL;
236 return 0;
237}
238
239static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
240 const char *page, size_t len)
241{
242 char *name;
243 int ret;
244
245 name = kstrdup(page, GFP_KERNEL);
246 if (!name)
247 return -ENOMEM;
248 if (name[len - 1] == '\n')
249 name[len - 1] = '\0';
250
251 mutex_lock(&gi->lock);
252
253 if (!strlen(name)) {
254 ret = unregister_gadget(gi);
255 if (ret)
256 goto err;
257 } else {
258 if (gi->udc_name) {
259 ret = -EBUSY;
260 goto err;
261 }
262 ret = udc_attach_driver(name, &gi->composite.gadget_driver);
263 if (ret)
264 goto err;
265 gi->udc_name = name;
266 }
267 mutex_unlock(&gi->lock);
268 return len;
269err:
270 kfree(name);
271 mutex_unlock(&gi->lock);
272 return ret;
273}
274
275GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
276GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
277GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
278GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
279GI_DEVICE_DESC_ITEM_ATTR(idVendor);
280GI_DEVICE_DESC_ITEM_ATTR(idProduct);
281GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
282GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
283GI_DEVICE_DESC_ITEM_ATTR(UDC);
284
285static struct configfs_attribute *gadget_root_attrs[] = {
286 &gadget_cdev_desc_bDeviceClass.attr,
287 &gadget_cdev_desc_bDeviceSubClass.attr,
288 &gadget_cdev_desc_bDeviceProtocol.attr,
289 &gadget_cdev_desc_bMaxPacketSize0.attr,
290 &gadget_cdev_desc_idVendor.attr,
291 &gadget_cdev_desc_idProduct.attr,
292 &gadget_cdev_desc_bcdDevice.attr,
293 &gadget_cdev_desc_bcdUSB.attr,
294 &gadget_cdev_desc_UDC.attr,
295 NULL,
296};
297
298static inline struct gadget_info *to_gadget_info(struct config_item *item)
299{
300 return container_of(to_config_group(item), struct gadget_info, group);
301}
302
303static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
304{
305 return container_of(to_config_group(item), struct gadget_strings,
306 group);
307}
308
309static inline struct gadget_config_name *to_gadget_config_name(
310 struct config_item *item)
311{
312 return container_of(to_config_group(item), struct gadget_config_name,
313 group);
314}
315
316static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
317{
318 return container_of(to_config_group(item), struct config_usb_cfg,
319 group);
320}
321
322static inline struct usb_function_instance *to_usb_function_instance(
323 struct config_item *item)
324{
325 return container_of(to_config_group(item),
326 struct usb_function_instance, group);
327}
328
329static void gadget_info_attr_release(struct config_item *item)
330{
331 struct gadget_info *gi = to_gadget_info(item);
332
333 WARN_ON(!list_empty(&gi->cdev.configs));
334 WARN_ON(!list_empty(&gi->string_list));
335 WARN_ON(!list_empty(&gi->available_func));
336 kfree(gi->composite.gadget_driver.function);
337 kfree(gi);
338}
339
340CONFIGFS_ATTR_OPS(gadget_info);
341
342static struct configfs_item_operations gadget_root_item_ops = {
343 .release = gadget_info_attr_release,
344 .show_attribute = gadget_info_attr_show,
345 .store_attribute = gadget_info_attr_store,
346};
347
348static void gadget_config_attr_release(struct config_item *item)
349{
350 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
351
352 WARN_ON(!list_empty(&cfg->c.functions));
353 list_del(&cfg->c.list);
354 kfree(cfg->c.label);
355 kfree(cfg);
356}
357
358static int config_usb_cfg_link(
359 struct config_item *usb_cfg_ci,
360 struct config_item *usb_func_ci)
361{
362 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
363 struct usb_composite_dev *cdev = cfg->c.cdev;
364 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
365
366 struct config_group *group = to_config_group(usb_func_ci);
367 struct usb_function_instance *fi = container_of(group,
368 struct usb_function_instance, group);
369 struct usb_function_instance *a_fi;
370 struct usb_function *f;
371 int ret;
372
373 mutex_lock(&gi->lock);
374 /*
375 * Make sure this function is from within our _this_ gadget and not
376 * from another gadget or a random directory.
377 * Also a function instance can only be linked once.
378 */
379 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
380 if (a_fi == fi)
381 break;
382 }
383 if (a_fi != fi) {
384 ret = -EINVAL;
385 goto out;
386 }
387
388 list_for_each_entry(f, &cfg->func_list, list) {
389 if (f->fi == fi) {
390 ret = -EEXIST;
391 goto out;
392 }
393 }
394
395 f = usb_get_function(fi);
396 if (IS_ERR(f)) {
397 ret = PTR_ERR(f);
398 goto out;
399 }
400
401 /* stash the function until we bind it to the gadget */
402 list_add_tail(&f->list, &cfg->func_list);
403 ret = 0;
404out:
405 mutex_unlock(&gi->lock);
406 return ret;
407}
408
409static int config_usb_cfg_unlink(
410 struct config_item *usb_cfg_ci,
411 struct config_item *usb_func_ci)
412{
413 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
414 struct usb_composite_dev *cdev = cfg->c.cdev;
415 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
416
417 struct config_group *group = to_config_group(usb_func_ci);
418 struct usb_function_instance *fi = container_of(group,
419 struct usb_function_instance, group);
420 struct usb_function *f;
421
422 /*
423 * ideally I would like to forbid to unlink functions while a gadget is
424 * bound to an UDC. Since this isn't possible at the moment, we simply
425 * force an unbind, the function is available here and then we can
426 * remove the function.
427 */
428 mutex_lock(&gi->lock);
429 if (gi->udc_name)
430 unregister_gadget(gi);
431 WARN_ON(gi->udc_name);
432
433 list_for_each_entry(f, &cfg->func_list, list) {
434 if (f->fi == fi) {
435 list_del(&f->list);
436 usb_put_function(f);
437 mutex_unlock(&gi->lock);
438 return 0;
439 }
440 }
441 mutex_unlock(&gi->lock);
75bfe23a 442 WARN(1, "Unable to locate function to unbind\n");
88af8bbe
SAS
443 return 0;
444}
445
446CONFIGFS_ATTR_OPS(config_usb_cfg);
447
448static struct configfs_item_operations gadget_config_item_ops = {
449 .release = gadget_config_attr_release,
450 .show_attribute = config_usb_cfg_attr_show,
451 .store_attribute = config_usb_cfg_attr_store,
452 .allow_link = config_usb_cfg_link,
453 .drop_link = config_usb_cfg_unlink,
454};
455
456
457static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
458 char *page)
459{
460 return sprintf(page, "%u\n", cfg->c.MaxPower);
461}
462
463static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
464 const char *page, size_t len)
465{
466 u16 val;
467 int ret;
468 ret = kstrtou16(page, 0, &val);
469 if (ret)
470 return ret;
471 if (DIV_ROUND_UP(val, 8) > 0xff)
472 return -ERANGE;
473 cfg->c.MaxPower = val;
474 return len;
475}
476
477static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
478 char *page)
479{
480 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
481}
482
483static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
484 const char *page, size_t len)
485{
486 u8 val;
487 int ret;
488 ret = kstrtou8(page, 0, &val);
489 if (ret)
490 return ret;
491 if (!(val & USB_CONFIG_ATT_ONE))
492 return -EINVAL;
493 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
494 USB_CONFIG_ATT_WAKEUP))
495 return -EINVAL;
496 cfg->c.bmAttributes = val;
497 return len;
498}
499
500#define CFG_CONFIG_DESC_ITEM_ATTR(name) \
501 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
502 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
503 gadget_config_desc_##name##_show, \
504 gadget_config_desc_##name##_store)
505
506CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
507CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
508
509static struct configfs_attribute *gadget_config_attrs[] = {
510 &gadget_usb_cfg_MaxPower.attr,
511 &gadget_usb_cfg_bmAttributes.attr,
512 NULL,
513};
514
515static struct config_item_type gadget_config_type = {
516 .ct_item_ops = &gadget_config_item_ops,
517 .ct_attrs = gadget_config_attrs,
518 .ct_owner = THIS_MODULE,
519};
520
521static struct config_item_type gadget_root_type = {
522 .ct_item_ops = &gadget_root_item_ops,
523 .ct_attrs = gadget_root_attrs,
524 .ct_owner = THIS_MODULE,
525};
526
527static void composite_init_dev(struct usb_composite_dev *cdev)
528{
529 spin_lock_init(&cdev->lock);
530 INIT_LIST_HEAD(&cdev->configs);
531 INIT_LIST_HEAD(&cdev->gstrings);
532}
533
534static struct config_group *function_make(
535 struct config_group *group,
536 const char *name)
537{
538 struct gadget_info *gi;
539 struct usb_function_instance *fi;
540 char buf[MAX_NAME_LEN];
541 char *func_name;
542 char *instance_name;
543 int ret;
544
545 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
546 if (ret >= MAX_NAME_LEN)
547 return ERR_PTR(-ENAMETOOLONG);
548
549 func_name = buf;
550 instance_name = strchr(func_name, '.');
551 if (!instance_name) {
552 pr_err("Unable to locate . in FUNC.INSTANCE\n");
553 return ERR_PTR(-EINVAL);
554 }
555 *instance_name = '\0';
556 instance_name++;
557
558 fi = usb_get_function_instance(func_name);
559 if (IS_ERR(fi))
560 return ERR_PTR(PTR_ERR(fi));
561
562 ret = config_item_set_name(&fi->group.cg_item, name);
563 if (ret) {
564 usb_put_function_instance(fi);
565 return ERR_PTR(ret);
566 }
567
568 gi = container_of(group, struct gadget_info, functions_group);
569
570 mutex_lock(&gi->lock);
571 list_add_tail(&fi->cfs_list, &gi->available_func);
572 mutex_unlock(&gi->lock);
573 return &fi->group;
574}
575
576static void function_drop(
577 struct config_group *group,
578 struct config_item *item)
579{
580 struct usb_function_instance *fi = to_usb_function_instance(item);
581 struct gadget_info *gi;
582
583 gi = container_of(group, struct gadget_info, functions_group);
584
585 mutex_lock(&gi->lock);
586 list_del(&fi->cfs_list);
587 mutex_unlock(&gi->lock);
588 config_item_put(item);
589}
590
591static struct configfs_group_operations functions_ops = {
592 .make_group = &function_make,
593 .drop_item = &function_drop,
594};
595
596static struct config_item_type functions_type = {
597 .ct_group_ops = &functions_ops,
598 .ct_owner = THIS_MODULE,
599};
600
601CONFIGFS_ATTR_STRUCT(gadget_config_name);
602GS_STRINGS_RW(gadget_config_name, configuration);
603
604static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
605 &gadget_config_name_configuration.attr,
606 NULL,
607};
608
609static void gadget_config_name_attr_release(struct config_item *item)
610{
611 struct gadget_config_name *cn = to_gadget_config_name(item);
612
613 kfree(cn->configuration);
614
615 list_del(&cn->list);
616 kfree(cn);
617}
618
619USB_CONFIG_STRING_RW_OPS(gadget_config_name);
620USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
621
622static struct config_group *config_desc_make(
623 struct config_group *group,
624 const char *name)
625{
626 struct gadget_info *gi;
627 struct config_usb_cfg *cfg;
628 char buf[MAX_NAME_LEN];
629 char *num_str;
630 u8 num;
631 int ret;
632
633 gi = container_of(group, struct gadget_info, configs_group);
634 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
635 if (ret >= MAX_NAME_LEN)
636 return ERR_PTR(-ENAMETOOLONG);
637
638 num_str = strchr(buf, '.');
639 if (!num_str) {
640 pr_err("Unable to locate . in name.bConfigurationValue\n");
641 return ERR_PTR(-EINVAL);
642 }
643
644 *num_str = '\0';
645 num_str++;
646
647 if (!strlen(buf))
648 return ERR_PTR(-EINVAL);
649
650 ret = kstrtou8(num_str, 0, &num);
651 if (ret)
652 return ERR_PTR(ret);
653
654 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
655 if (!cfg)
656 return ERR_PTR(-ENOMEM);
657 cfg->c.label = kstrdup(buf, GFP_KERNEL);
658 if (!cfg->c.label) {
659 ret = -ENOMEM;
660 goto err;
661 }
662 cfg->c.bConfigurationValue = num;
663 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
664 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
665 INIT_LIST_HEAD(&cfg->string_list);
666 INIT_LIST_HEAD(&cfg->func_list);
667
668 cfg->group.default_groups = cfg->default_groups;
669 cfg->default_groups[0] = &cfg->strings_group;
670
671 config_group_init_type_name(&cfg->group, name,
672 &gadget_config_type);
673 config_group_init_type_name(&cfg->strings_group, "strings",
674 &gadget_config_name_strings_type);
675
676 ret = usb_add_config_only(&gi->cdev, &cfg->c);
677 if (ret)
678 goto err;
679
680 return &cfg->group;
681err:
682 kfree(cfg->c.label);
683 kfree(cfg);
684 return ERR_PTR(ret);
685}
686
687static void config_desc_drop(
688 struct config_group *group,
689 struct config_item *item)
690{
691 config_item_put(item);
692}
693
694static struct configfs_group_operations config_desc_ops = {
695 .make_group = &config_desc_make,
696 .drop_item = &config_desc_drop,
697};
698
699static struct config_item_type config_desc_type = {
700 .ct_group_ops = &config_desc_ops,
701 .ct_owner = THIS_MODULE,
702};
703
704CONFIGFS_ATTR_STRUCT(gadget_strings);
705GS_STRINGS_RW(gadget_strings, manufacturer);
706GS_STRINGS_RW(gadget_strings, product);
707GS_STRINGS_RW(gadget_strings, serialnumber);
708
709static struct configfs_attribute *gadget_strings_langid_attrs[] = {
710 &gadget_strings_manufacturer.attr,
711 &gadget_strings_product.attr,
712 &gadget_strings_serialnumber.attr,
713 NULL,
714};
715
716static void gadget_strings_attr_release(struct config_item *item)
717{
718 struct gadget_strings *gs = to_gadget_strings(item);
719
720 kfree(gs->manufacturer);
721 kfree(gs->product);
722 kfree(gs->serialnumber);
723
724 list_del(&gs->list);
725 kfree(gs);
726}
727
728USB_CONFIG_STRING_RW_OPS(gadget_strings);
729USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
730
731static int configfs_do_nothing(struct usb_composite_dev *cdev)
732{
75bfe23a 733 WARN_ON(1);
88af8bbe
SAS
734 return -EINVAL;
735}
736
737int composite_dev_prepare(struct usb_composite_driver *composite,
738 struct usb_composite_dev *dev);
739
740static void purge_configs_funcs(struct gadget_info *gi)
741{
742 struct usb_configuration *c;
743
744 list_for_each_entry(c, &gi->cdev.configs, list) {
745 struct usb_function *f, *tmp;
746 struct config_usb_cfg *cfg;
747
748 cfg = container_of(c, struct config_usb_cfg, c);
749
750 list_for_each_entry_safe(f, tmp, &c->functions, list) {
751
752 list_move_tail(&f->list, &cfg->func_list);
753 if (f->unbind) {
754 dev_err(&gi->cdev.gadget->dev, "unbind function"
755 " '%s'/%p\n", f->name, f);
756 f->unbind(c, f);
757 }
758 }
759 c->next_interface_id = 0;
760 c->superspeed = 0;
761 c->highspeed = 0;
762 c->fullspeed = 0;
763 }
764}
765
766static int configfs_composite_bind(struct usb_gadget *gadget,
767 struct usb_gadget_driver *gdriver)
768{
769 struct usb_composite_driver *composite = to_cdriver(gdriver);
770 struct gadget_info *gi = container_of(composite,
771 struct gadget_info, composite);
772 struct usb_composite_dev *cdev = &gi->cdev;
773 struct usb_configuration *c;
774 struct usb_string *s;
775 unsigned i;
776 int ret;
777
778 /* the gi->lock is hold by the caller */
779 cdev->gadget = gadget;
780 set_gadget_data(gadget, cdev);
781 ret = composite_dev_prepare(composite, cdev);
782 if (ret)
783 return ret;
784 /* and now the gadget bind */
785 ret = -EINVAL;
786
787 if (list_empty(&gi->cdev.configs)) {
788 pr_err("Need atleast one configuration in %s.\n",
789 gi->composite.name);
790 goto err_comp_cleanup;
791 }
792
793
794 list_for_each_entry(c, &gi->cdev.configs, list) {
795 struct config_usb_cfg *cfg;
796
797 cfg = container_of(c, struct config_usb_cfg, c);
798 if (list_empty(&cfg->func_list)) {
799 pr_err("Config %s/%d of %s needs atleast one function.\n",
800 c->label, c->bConfigurationValue,
801 gi->composite.name);
802 goto err_comp_cleanup;
803 }
804 }
805
806 /* init all strings */
807 if (!list_empty(&gi->string_list)) {
808 struct gadget_strings *gs;
809
810 i = 0;
811 list_for_each_entry(gs, &gi->string_list, list) {
812
813 gi->gstrings[i] = &gs->stringtab_dev;
814 gs->stringtab_dev.strings = gs->strings;
815 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
816 gs->manufacturer;
817 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
818 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
819 i++;
820 }
821 gi->gstrings[i] = NULL;
822 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
823 USB_GADGET_FIRST_AVAIL_IDX);
fea77077
WY
824 if (IS_ERR(s)) {
825 ret = PTR_ERR(s);
88af8bbe 826 goto err_comp_cleanup;
fea77077 827 }
88af8bbe
SAS
828
829 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
830 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
831 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
832 }
833
834 /* Go through all configs, attach all functions */
835 list_for_each_entry(c, &gi->cdev.configs, list) {
836 struct config_usb_cfg *cfg;
837 struct usb_function *f;
838 struct usb_function *tmp;
839 struct gadget_config_name *cn;
840
841 cfg = container_of(c, struct config_usb_cfg, c);
842 if (!list_empty(&cfg->string_list)) {
843 i = 0;
844 list_for_each_entry(cn, &cfg->string_list, list) {
845 cfg->gstrings[i] = &cn->stringtab_dev;
846 cn->stringtab_dev.strings = &cn->strings;
847 cn->strings.s = cn->configuration;
848 i++;
849 }
850 cfg->gstrings[i] = NULL;
851 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
fea77077
WY
852 if (IS_ERR(s)) {
853 ret = PTR_ERR(s);
88af8bbe 854 goto err_comp_cleanup;
fea77077 855 }
88af8bbe
SAS
856 c->iConfiguration = s[0].id;
857 }
858
859 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
860 list_del(&f->list);
861 ret = usb_add_function(c, f);
862 if (ret)
863 goto err_purge_funcs;
864 }
865 usb_ep_autoconfig_reset(cdev->gadget);
866 }
867 usb_ep_autoconfig_reset(cdev->gadget);
868 return 0;
869
870err_purge_funcs:
871 purge_configs_funcs(gi);
872err_comp_cleanup:
873 composite_dev_cleanup(cdev);
874 return ret;
875}
876
877static void configfs_composite_unbind(struct usb_gadget *gadget)
878{
879 struct usb_composite_dev *cdev;
880 struct gadget_info *gi;
881
882 /* the gi->lock is hold by the caller */
883
884 cdev = get_gadget_data(gadget);
885 gi = container_of(cdev, struct gadget_info, cdev);
886
887 purge_configs_funcs(gi);
888 composite_dev_cleanup(cdev);
889 usb_ep_autoconfig_reset(cdev->gadget);
890 cdev->gadget = NULL;
891 set_gadget_data(gadget, NULL);
892}
893
894static const struct usb_gadget_driver configfs_driver_template = {
895 .bind = configfs_composite_bind,
896 .unbind = configfs_composite_unbind,
897
898 .setup = composite_setup,
899 .disconnect = composite_disconnect,
900
901 .max_speed = USB_SPEED_SUPER,
902 .driver = {
903 .owner = THIS_MODULE,
904 .name = "configfs-gadget",
905 },
906};
907
908static struct config_group *gadgets_make(
909 struct config_group *group,
910 const char *name)
911{
912 struct gadget_info *gi;
913
914 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
915 if (!gi)
916 return ERR_PTR(-ENOMEM);
917
918 gi->group.default_groups = gi->default_groups;
919 gi->group.default_groups[0] = &gi->functions_group;
920 gi->group.default_groups[1] = &gi->configs_group;
921 gi->group.default_groups[2] = &gi->strings_group;
922
923 config_group_init_type_name(&gi->functions_group, "functions",
924 &functions_type);
925 config_group_init_type_name(&gi->configs_group, "configs",
926 &config_desc_type);
927 config_group_init_type_name(&gi->strings_group, "strings",
928 &gadget_strings_strings_type);
929
930 gi->composite.bind = configfs_do_nothing;
931 gi->composite.unbind = configfs_do_nothing;
932 gi->composite.suspend = NULL;
933 gi->composite.resume = NULL;
934 gi->composite.max_speed = USB_SPEED_SUPER;
935
936 mutex_init(&gi->lock);
937 INIT_LIST_HEAD(&gi->string_list);
938 INIT_LIST_HEAD(&gi->available_func);
939
940 composite_init_dev(&gi->cdev);
941 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
942 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
943 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
944
945 gi->composite.gadget_driver = configfs_driver_template;
946
947 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
948 gi->composite.name = gi->composite.gadget_driver.function;
949
950 if (!gi->composite.gadget_driver.function)
951 goto err;
952
953#ifdef CONFIG_USB_OTG
954 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
955 gi->otg.bDescriptorType = USB_DT_OTG;
956 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
957#endif
958
959 config_group_init_type_name(&gi->group, name,
960 &gadget_root_type);
961 return &gi->group;
962err:
963 kfree(gi);
964 return ERR_PTR(-ENOMEM);
965}
966
967static void gadgets_drop(struct config_group *group, struct config_item *item)
968{
969 config_item_put(item);
970}
971
972static struct configfs_group_operations gadgets_ops = {
973 .make_group = &gadgets_make,
974 .drop_item = &gadgets_drop,
975};
976
977static struct config_item_type gadgets_type = {
978 .ct_group_ops = &gadgets_ops,
979 .ct_owner = THIS_MODULE,
980};
981
982static struct configfs_subsystem gadget_subsys = {
983 .su_group = {
984 .cg_item = {
985 .ci_namebuf = "usb_gadget",
986 .ci_type = &gadgets_type,
987 },
988 },
989 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
990};
991
992static int __init gadget_cfs_init(void)
993{
994 int ret;
995
996 config_group_init(&gadget_subsys.su_group);
997
998 ret = configfs_register_subsystem(&gadget_subsys);
999 return ret;
1000}
1001module_init(gadget_cfs_init);
1002
1003static void __exit gadget_cfs_exit(void)
1004{
1005 configfs_unregister_subsystem(&gadget_subsys);
1006}
1007module_exit(gadget_cfs_exit);