bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / usb / gadget / configfs.c
blob0495c94a23d7e96a9c3554b99893b6b0f63e7302
1 #include <linux/configfs.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
5 #include <linux/nls.h>
6 #include <linux/usb/composite.h>
7 #include <linux/usb/gadget_configfs.h>
8 #include "configfs.h"
9 #include "u_f.h"
10 #include "u_os_desc.h"
12 int check_user_usb_string(const char *name,
13 struct usb_gadget_strings *stringtab_dev)
15 unsigned primary_lang;
16 unsigned sub_lang;
17 u16 num;
18 int ret;
20 ret = kstrtou16(name, 0, &num);
21 if (ret)
22 return ret;
24 primary_lang = num & 0x3ff;
25 sub_lang = num >> 10;
27 /* simple sanity check for valid langid */
28 switch (primary_lang) {
29 case 0:
30 case 0x62 ... 0xfe:
31 case 0x100 ... 0x3ff:
32 return -EINVAL;
34 if (!sub_lang)
35 return -EINVAL;
37 stringtab_dev->language = num;
38 return 0;
41 #define MAX_NAME_LEN 40
42 #define MAX_USB_STRING_LANGS 2
44 struct gadget_info {
45 struct config_group group;
46 struct config_group functions_group;
47 struct config_group configs_group;
48 struct config_group strings_group;
49 struct config_group os_desc_group;
50 struct config_group *default_groups[5];
52 struct mutex lock;
53 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
54 struct list_head string_list;
55 struct list_head available_func;
57 const char *udc_name;
58 #ifdef CONFIG_USB_OTG
59 struct usb_otg_descriptor otg;
60 #endif
61 struct usb_composite_driver composite;
62 struct usb_composite_dev cdev;
63 bool use_os_desc;
64 char b_vendor_code;
65 char qw_sign[OS_STRING_QW_SIGN_LEN];
68 struct config_usb_cfg {
69 struct config_group group;
70 struct config_group strings_group;
71 struct config_group *default_groups[2];
72 struct list_head string_list;
73 struct usb_configuration c;
74 struct list_head func_list;
75 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
78 struct gadget_strings {
79 struct usb_gadget_strings stringtab_dev;
80 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
81 char *manufacturer;
82 char *product;
83 char *serialnumber;
85 struct config_group group;
86 struct list_head list;
89 struct os_desc {
90 struct config_group group;
93 struct gadget_config_name {
94 struct usb_gadget_strings stringtab_dev;
95 struct usb_string strings;
96 char *configuration;
98 struct config_group group;
99 struct list_head list;
102 static int usb_string_copy(const char *s, char **s_copy)
104 int ret;
105 char *str;
106 char *copy = *s_copy;
107 ret = strlen(s);
108 if (ret > 126)
109 return -EOVERFLOW;
111 str = kstrdup(s, GFP_KERNEL);
112 if (!str)
113 return -ENOMEM;
114 if (str[ret - 1] == '\n')
115 str[ret - 1] = '\0';
116 kfree(copy);
117 *s_copy = str;
118 return 0;
121 CONFIGFS_ATTR_STRUCT(gadget_info);
122 CONFIGFS_ATTR_STRUCT(config_usb_cfg);
124 #define GI_DEVICE_DESC_ITEM_ATTR(name) \
125 static struct gadget_info_attribute gadget_cdev_desc_##name = \
126 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
127 gadget_dev_desc_##name##_show, \
128 gadget_dev_desc_##name##_store)
130 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
131 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
132 char *page) \
134 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
137 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
138 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
139 char *page) \
141 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
145 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
146 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
147 const char *page, size_t len) \
149 u8 val; \
150 int ret; \
151 ret = kstrtou8(page, 0, &val); \
152 if (ret) \
153 return ret; \
154 gi->cdev.desc._name = val; \
155 return len; \
158 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
159 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
160 const char *page, size_t len) \
162 u16 val; \
163 int ret; \
164 ret = kstrtou16(page, 0, &val); \
165 if (ret) \
166 return ret; \
167 gi->cdev.desc._name = cpu_to_le16p(&val); \
168 return len; \
171 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
172 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
173 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
175 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
176 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
177 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
178 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
179 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
180 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
181 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
182 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
184 static ssize_t is_valid_bcd(u16 bcd_val)
186 if ((bcd_val & 0xf) > 9)
187 return -EINVAL;
188 if (((bcd_val >> 4) & 0xf) > 9)
189 return -EINVAL;
190 if (((bcd_val >> 8) & 0xf) > 9)
191 return -EINVAL;
192 if (((bcd_val >> 12) & 0xf) > 9)
193 return -EINVAL;
194 return 0;
197 static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
198 const char *page, size_t len)
200 u16 bcdDevice;
201 int ret;
203 ret = kstrtou16(page, 0, &bcdDevice);
204 if (ret)
205 return ret;
206 ret = is_valid_bcd(bcdDevice);
207 if (ret)
208 return ret;
210 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
211 return len;
214 static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
215 const char *page, size_t len)
217 u16 bcdUSB;
218 int ret;
220 ret = kstrtou16(page, 0, &bcdUSB);
221 if (ret)
222 return ret;
223 ret = is_valid_bcd(bcdUSB);
224 if (ret)
225 return ret;
227 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
228 return len;
231 static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
233 return sprintf(page, "%s\n", gi->udc_name ?: "");
236 static int unregister_gadget(struct gadget_info *gi)
238 int ret;
240 if (!gi->udc_name)
241 return -ENODEV;
243 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
244 if (ret)
245 return ret;
246 kfree(gi->udc_name);
247 gi->udc_name = NULL;
248 return 0;
251 static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
252 const char *page, size_t len)
254 char *name;
255 int ret;
257 name = kstrdup(page, GFP_KERNEL);
258 if (!name)
259 return -ENOMEM;
260 if (name[len - 1] == '\n')
261 name[len - 1] = '\0';
263 mutex_lock(&gi->lock);
265 if (!strlen(name)) {
266 ret = unregister_gadget(gi);
267 if (ret)
268 goto err;
269 } else {
270 if (gi->udc_name) {
271 ret = -EBUSY;
272 goto err;
274 ret = usb_udc_attach_driver(name, &gi->composite.gadget_driver);
275 if (ret)
276 goto err;
277 gi->udc_name = name;
279 mutex_unlock(&gi->lock);
280 return len;
281 err:
282 kfree(name);
283 mutex_unlock(&gi->lock);
284 return ret;
287 GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
288 GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
289 GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
290 GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
291 GI_DEVICE_DESC_ITEM_ATTR(idVendor);
292 GI_DEVICE_DESC_ITEM_ATTR(idProduct);
293 GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
294 GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
295 GI_DEVICE_DESC_ITEM_ATTR(UDC);
297 static struct configfs_attribute *gadget_root_attrs[] = {
298 &gadget_cdev_desc_bDeviceClass.attr,
299 &gadget_cdev_desc_bDeviceSubClass.attr,
300 &gadget_cdev_desc_bDeviceProtocol.attr,
301 &gadget_cdev_desc_bMaxPacketSize0.attr,
302 &gadget_cdev_desc_idVendor.attr,
303 &gadget_cdev_desc_idProduct.attr,
304 &gadget_cdev_desc_bcdDevice.attr,
305 &gadget_cdev_desc_bcdUSB.attr,
306 &gadget_cdev_desc_UDC.attr,
307 NULL,
310 static inline struct gadget_info *to_gadget_info(struct config_item *item)
312 return container_of(to_config_group(item), struct gadget_info, group);
315 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
317 return container_of(to_config_group(item), struct gadget_strings,
318 group);
321 static inline struct gadget_config_name *to_gadget_config_name(
322 struct config_item *item)
324 return container_of(to_config_group(item), struct gadget_config_name,
325 group);
328 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
330 return container_of(to_config_group(item), struct config_usb_cfg,
331 group);
334 static inline struct usb_function_instance *to_usb_function_instance(
335 struct config_item *item)
337 return container_of(to_config_group(item),
338 struct usb_function_instance, group);
341 static void gadget_info_attr_release(struct config_item *item)
343 struct gadget_info *gi = to_gadget_info(item);
345 WARN_ON(!list_empty(&gi->cdev.configs));
346 WARN_ON(!list_empty(&gi->string_list));
347 WARN_ON(!list_empty(&gi->available_func));
348 kfree(gi->composite.gadget_driver.function);
349 kfree(gi);
352 CONFIGFS_ATTR_OPS(gadget_info);
354 static struct configfs_item_operations gadget_root_item_ops = {
355 .release = gadget_info_attr_release,
356 .show_attribute = gadget_info_attr_show,
357 .store_attribute = gadget_info_attr_store,
360 static void gadget_config_attr_release(struct config_item *item)
362 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
364 WARN_ON(!list_empty(&cfg->c.functions));
365 list_del(&cfg->c.list);
366 kfree(cfg->c.label);
367 kfree(cfg);
370 static int config_usb_cfg_link(
371 struct config_item *usb_cfg_ci,
372 struct config_item *usb_func_ci)
374 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
375 struct usb_composite_dev *cdev = cfg->c.cdev;
376 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
378 struct config_group *group = to_config_group(usb_func_ci);
379 struct usb_function_instance *fi = container_of(group,
380 struct usb_function_instance, group);
381 struct usb_function_instance *a_fi;
382 struct usb_function *f;
383 int ret;
385 mutex_lock(&gi->lock);
387 * Make sure this function is from within our _this_ gadget and not
388 * from another gadget or a random directory.
389 * Also a function instance can only be linked once.
391 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
392 if (a_fi == fi)
393 break;
395 if (a_fi != fi) {
396 ret = -EINVAL;
397 goto out;
400 list_for_each_entry(f, &cfg->func_list, list) {
401 if (f->fi == fi) {
402 ret = -EEXIST;
403 goto out;
407 f = usb_get_function(fi);
408 if (IS_ERR(f)) {
409 ret = PTR_ERR(f);
410 goto out;
413 /* stash the function until we bind it to the gadget */
414 list_add_tail(&f->list, &cfg->func_list);
415 ret = 0;
416 out:
417 mutex_unlock(&gi->lock);
418 return ret;
421 static int config_usb_cfg_unlink(
422 struct config_item *usb_cfg_ci,
423 struct config_item *usb_func_ci)
425 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
426 struct usb_composite_dev *cdev = cfg->c.cdev;
427 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
429 struct config_group *group = to_config_group(usb_func_ci);
430 struct usb_function_instance *fi = container_of(group,
431 struct usb_function_instance, group);
432 struct usb_function *f;
435 * ideally I would like to forbid to unlink functions while a gadget is
436 * bound to an UDC. Since this isn't possible at the moment, we simply
437 * force an unbind, the function is available here and then we can
438 * remove the function.
440 mutex_lock(&gi->lock);
441 if (gi->udc_name)
442 unregister_gadget(gi);
443 WARN_ON(gi->udc_name);
445 list_for_each_entry(f, &cfg->func_list, list) {
446 if (f->fi == fi) {
447 list_del(&f->list);
448 usb_put_function(f);
449 mutex_unlock(&gi->lock);
450 return 0;
453 mutex_unlock(&gi->lock);
454 WARN(1, "Unable to locate function to unbind\n");
455 return 0;
458 CONFIGFS_ATTR_OPS(config_usb_cfg);
460 static struct configfs_item_operations gadget_config_item_ops = {
461 .release = gadget_config_attr_release,
462 .show_attribute = config_usb_cfg_attr_show,
463 .store_attribute = config_usb_cfg_attr_store,
464 .allow_link = config_usb_cfg_link,
465 .drop_link = config_usb_cfg_unlink,
469 static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
470 char *page)
472 return sprintf(page, "%u\n", cfg->c.MaxPower);
475 static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
476 const char *page, size_t len)
478 u16 val;
479 int ret;
480 ret = kstrtou16(page, 0, &val);
481 if (ret)
482 return ret;
483 if (DIV_ROUND_UP(val, 8) > 0xff)
484 return -ERANGE;
485 cfg->c.MaxPower = val;
486 return len;
489 static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
490 char *page)
492 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
495 static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
496 const char *page, size_t len)
498 u8 val;
499 int ret;
500 ret = kstrtou8(page, 0, &val);
501 if (ret)
502 return ret;
503 if (!(val & USB_CONFIG_ATT_ONE))
504 return -EINVAL;
505 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
506 USB_CONFIG_ATT_WAKEUP))
507 return -EINVAL;
508 cfg->c.bmAttributes = val;
509 return len;
512 #define CFG_CONFIG_DESC_ITEM_ATTR(name) \
513 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
514 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
515 gadget_config_desc_##name##_show, \
516 gadget_config_desc_##name##_store)
518 CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
519 CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
521 static struct configfs_attribute *gadget_config_attrs[] = {
522 &gadget_usb_cfg_MaxPower.attr,
523 &gadget_usb_cfg_bmAttributes.attr,
524 NULL,
527 static struct config_item_type gadget_config_type = {
528 .ct_item_ops = &gadget_config_item_ops,
529 .ct_attrs = gadget_config_attrs,
530 .ct_owner = THIS_MODULE,
533 static struct config_item_type gadget_root_type = {
534 .ct_item_ops = &gadget_root_item_ops,
535 .ct_attrs = gadget_root_attrs,
536 .ct_owner = THIS_MODULE,
539 static void composite_init_dev(struct usb_composite_dev *cdev)
541 spin_lock_init(&cdev->lock);
542 INIT_LIST_HEAD(&cdev->configs);
543 INIT_LIST_HEAD(&cdev->gstrings);
546 static struct config_group *function_make(
547 struct config_group *group,
548 const char *name)
550 struct gadget_info *gi;
551 struct usb_function_instance *fi;
552 char buf[MAX_NAME_LEN];
553 char *func_name;
554 char *instance_name;
555 int ret;
557 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
558 if (ret >= MAX_NAME_LEN)
559 return ERR_PTR(-ENAMETOOLONG);
561 func_name = buf;
562 instance_name = strchr(func_name, '.');
563 if (!instance_name) {
564 pr_err("Unable to locate . in FUNC.INSTANCE\n");
565 return ERR_PTR(-EINVAL);
567 *instance_name = '\0';
568 instance_name++;
570 fi = usb_get_function_instance(func_name);
571 if (IS_ERR(fi))
572 return ERR_CAST(fi);
574 ret = config_item_set_name(&fi->group.cg_item, name);
575 if (ret) {
576 usb_put_function_instance(fi);
577 return ERR_PTR(ret);
579 if (fi->set_inst_name) {
580 ret = fi->set_inst_name(fi, instance_name);
581 if (ret) {
582 usb_put_function_instance(fi);
583 return ERR_PTR(ret);
587 gi = container_of(group, struct gadget_info, functions_group);
589 mutex_lock(&gi->lock);
590 list_add_tail(&fi->cfs_list, &gi->available_func);
591 mutex_unlock(&gi->lock);
592 return &fi->group;
595 static void function_drop(
596 struct config_group *group,
597 struct config_item *item)
599 struct usb_function_instance *fi = to_usb_function_instance(item);
600 struct gadget_info *gi;
602 gi = container_of(group, struct gadget_info, functions_group);
604 mutex_lock(&gi->lock);
605 list_del(&fi->cfs_list);
606 mutex_unlock(&gi->lock);
607 config_item_put(item);
610 static struct configfs_group_operations functions_ops = {
611 .make_group = &function_make,
612 .drop_item = &function_drop,
615 static struct config_item_type functions_type = {
616 .ct_group_ops = &functions_ops,
617 .ct_owner = THIS_MODULE,
620 CONFIGFS_ATTR_STRUCT(gadget_config_name);
621 GS_STRINGS_RW(gadget_config_name, configuration);
623 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
624 &gadget_config_name_configuration.attr,
625 NULL,
628 static void gadget_config_name_attr_release(struct config_item *item)
630 struct gadget_config_name *cn = to_gadget_config_name(item);
632 kfree(cn->configuration);
634 list_del(&cn->list);
635 kfree(cn);
638 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
639 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
641 static struct config_group *config_desc_make(
642 struct config_group *group,
643 const char *name)
645 struct gadget_info *gi;
646 struct config_usb_cfg *cfg;
647 char buf[MAX_NAME_LEN];
648 char *num_str;
649 u8 num;
650 int ret;
652 gi = container_of(group, struct gadget_info, configs_group);
653 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
654 if (ret >= MAX_NAME_LEN)
655 return ERR_PTR(-ENAMETOOLONG);
657 num_str = strchr(buf, '.');
658 if (!num_str) {
659 pr_err("Unable to locate . in name.bConfigurationValue\n");
660 return ERR_PTR(-EINVAL);
663 *num_str = '\0';
664 num_str++;
666 if (!strlen(buf))
667 return ERR_PTR(-EINVAL);
669 ret = kstrtou8(num_str, 0, &num);
670 if (ret)
671 return ERR_PTR(ret);
673 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
674 if (!cfg)
675 return ERR_PTR(-ENOMEM);
676 cfg->c.label = kstrdup(buf, GFP_KERNEL);
677 if (!cfg->c.label) {
678 ret = -ENOMEM;
679 goto err;
681 cfg->c.bConfigurationValue = num;
682 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
683 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
684 INIT_LIST_HEAD(&cfg->string_list);
685 INIT_LIST_HEAD(&cfg->func_list);
687 cfg->group.default_groups = cfg->default_groups;
688 cfg->default_groups[0] = &cfg->strings_group;
690 config_group_init_type_name(&cfg->group, name,
691 &gadget_config_type);
692 config_group_init_type_name(&cfg->strings_group, "strings",
693 &gadget_config_name_strings_type);
695 ret = usb_add_config_only(&gi->cdev, &cfg->c);
696 if (ret)
697 goto err;
699 return &cfg->group;
700 err:
701 kfree(cfg->c.label);
702 kfree(cfg);
703 return ERR_PTR(ret);
706 static void config_desc_drop(
707 struct config_group *group,
708 struct config_item *item)
710 config_item_put(item);
713 static struct configfs_group_operations config_desc_ops = {
714 .make_group = &config_desc_make,
715 .drop_item = &config_desc_drop,
718 static struct config_item_type config_desc_type = {
719 .ct_group_ops = &config_desc_ops,
720 .ct_owner = THIS_MODULE,
723 CONFIGFS_ATTR_STRUCT(gadget_strings);
724 GS_STRINGS_RW(gadget_strings, manufacturer);
725 GS_STRINGS_RW(gadget_strings, product);
726 GS_STRINGS_RW(gadget_strings, serialnumber);
728 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
729 &gadget_strings_manufacturer.attr,
730 &gadget_strings_product.attr,
731 &gadget_strings_serialnumber.attr,
732 NULL,
735 static void gadget_strings_attr_release(struct config_item *item)
737 struct gadget_strings *gs = to_gadget_strings(item);
739 kfree(gs->manufacturer);
740 kfree(gs->product);
741 kfree(gs->serialnumber);
743 list_del(&gs->list);
744 kfree(gs);
747 USB_CONFIG_STRING_RW_OPS(gadget_strings);
748 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
750 static inline struct os_desc *to_os_desc(struct config_item *item)
752 return container_of(to_config_group(item), struct os_desc, group);
755 CONFIGFS_ATTR_STRUCT(os_desc);
756 CONFIGFS_ATTR_OPS(os_desc);
758 static ssize_t os_desc_use_show(struct os_desc *os_desc, char *page)
760 struct gadget_info *gi;
762 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
764 return sprintf(page, "%d", gi->use_os_desc);
767 static ssize_t os_desc_use_store(struct os_desc *os_desc, const char *page,
768 size_t len)
770 struct gadget_info *gi;
771 int ret;
772 bool use;
774 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
776 mutex_lock(&gi->lock);
777 ret = strtobool(page, &use);
778 if (!ret) {
779 gi->use_os_desc = use;
780 ret = len;
782 mutex_unlock(&gi->lock);
784 return ret;
787 static struct os_desc_attribute os_desc_use =
788 __CONFIGFS_ATTR(use, S_IRUGO | S_IWUSR,
789 os_desc_use_show,
790 os_desc_use_store);
792 static ssize_t os_desc_b_vendor_code_show(struct os_desc *os_desc, char *page)
794 struct gadget_info *gi;
796 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
798 return sprintf(page, "%d", gi->b_vendor_code);
801 static ssize_t os_desc_b_vendor_code_store(struct os_desc *os_desc,
802 const char *page, size_t len)
804 struct gadget_info *gi;
805 int ret;
806 u8 b_vendor_code;
808 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
810 mutex_lock(&gi->lock);
811 ret = kstrtou8(page, 0, &b_vendor_code);
812 if (!ret) {
813 gi->b_vendor_code = b_vendor_code;
814 ret = len;
816 mutex_unlock(&gi->lock);
818 return ret;
821 static struct os_desc_attribute os_desc_b_vendor_code =
822 __CONFIGFS_ATTR(b_vendor_code, S_IRUGO | S_IWUSR,
823 os_desc_b_vendor_code_show,
824 os_desc_b_vendor_code_store);
826 static ssize_t os_desc_qw_sign_show(struct os_desc *os_desc, char *page)
828 struct gadget_info *gi;
830 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
832 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
834 return OS_STRING_QW_SIGN_LEN;
837 static ssize_t os_desc_qw_sign_store(struct os_desc *os_desc, const char *page,
838 size_t len)
840 struct gadget_info *gi;
841 int res, l;
843 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
844 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
845 if (page[l - 1] == '\n')
846 --l;
848 mutex_lock(&gi->lock);
849 res = utf8s_to_utf16s(page, l,
850 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
851 OS_STRING_QW_SIGN_LEN);
852 if (res > 0)
853 res = len;
854 mutex_unlock(&gi->lock);
856 return res;
859 static struct os_desc_attribute os_desc_qw_sign =
860 __CONFIGFS_ATTR(qw_sign, S_IRUGO | S_IWUSR,
861 os_desc_qw_sign_show,
862 os_desc_qw_sign_store);
864 static struct configfs_attribute *os_desc_attrs[] = {
865 &os_desc_use.attr,
866 &os_desc_b_vendor_code.attr,
867 &os_desc_qw_sign.attr,
868 NULL,
871 static void os_desc_attr_release(struct config_item *item)
873 struct os_desc *os_desc = to_os_desc(item);
874 kfree(os_desc);
877 static int os_desc_link(struct config_item *os_desc_ci,
878 struct config_item *usb_cfg_ci)
880 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
881 struct gadget_info, os_desc_group);
882 struct usb_composite_dev *cdev = &gi->cdev;
883 struct config_usb_cfg *c_target =
884 container_of(to_config_group(usb_cfg_ci),
885 struct config_usb_cfg, group);
886 struct usb_configuration *c;
887 int ret;
889 mutex_lock(&gi->lock);
890 list_for_each_entry(c, &cdev->configs, list) {
891 if (c == &c_target->c)
892 break;
894 if (c != &c_target->c) {
895 ret = -EINVAL;
896 goto out;
899 if (cdev->os_desc_config) {
900 ret = -EBUSY;
901 goto out;
904 cdev->os_desc_config = &c_target->c;
905 ret = 0;
907 out:
908 mutex_unlock(&gi->lock);
909 return ret;
912 static int os_desc_unlink(struct config_item *os_desc_ci,
913 struct config_item *usb_cfg_ci)
915 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
916 struct gadget_info, os_desc_group);
917 struct usb_composite_dev *cdev = &gi->cdev;
919 mutex_lock(&gi->lock);
920 if (gi->udc_name)
921 unregister_gadget(gi);
922 cdev->os_desc_config = NULL;
923 WARN_ON(gi->udc_name);
924 mutex_unlock(&gi->lock);
925 return 0;
928 static struct configfs_item_operations os_desc_ops = {
929 .release = os_desc_attr_release,
930 .show_attribute = os_desc_attr_show,
931 .store_attribute = os_desc_attr_store,
932 .allow_link = os_desc_link,
933 .drop_link = os_desc_unlink,
936 static struct config_item_type os_desc_type = {
937 .ct_item_ops = &os_desc_ops,
938 .ct_attrs = os_desc_attrs,
939 .ct_owner = THIS_MODULE,
942 CONFIGFS_ATTR_STRUCT(usb_os_desc);
943 CONFIGFS_ATTR_OPS(usb_os_desc);
946 static inline struct usb_os_desc_ext_prop
947 *to_usb_os_desc_ext_prop(struct config_item *item)
949 return container_of(item, struct usb_os_desc_ext_prop, item);
952 CONFIGFS_ATTR_STRUCT(usb_os_desc_ext_prop);
953 CONFIGFS_ATTR_OPS(usb_os_desc_ext_prop);
955 static ssize_t ext_prop_type_show(struct usb_os_desc_ext_prop *ext_prop,
956 char *page)
958 return sprintf(page, "%d", ext_prop->type);
961 static ssize_t ext_prop_type_store(struct usb_os_desc_ext_prop *ext_prop,
962 const char *page, size_t len)
964 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
965 u8 type;
966 int ret;
968 if (desc->opts_mutex)
969 mutex_lock(desc->opts_mutex);
970 ret = kstrtou8(page, 0, &type);
971 if (ret)
972 goto end;
973 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
974 ret = -EINVAL;
975 goto end;
978 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
979 ext_prop->type == USB_EXT_PROP_LE32 ||
980 ext_prop->type == USB_EXT_PROP_BE32) &&
981 (type == USB_EXT_PROP_UNICODE ||
982 type == USB_EXT_PROP_UNICODE_ENV ||
983 type == USB_EXT_PROP_UNICODE_LINK))
984 ext_prop->data_len <<= 1;
985 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
986 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
987 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
988 (type == USB_EXT_PROP_BINARY ||
989 type == USB_EXT_PROP_LE32 ||
990 type == USB_EXT_PROP_BE32))
991 ext_prop->data_len >>= 1;
992 ext_prop->type = type;
993 ret = len;
995 end:
996 if (desc->opts_mutex)
997 mutex_unlock(desc->opts_mutex);
998 return ret;
1001 static ssize_t ext_prop_data_show(struct usb_os_desc_ext_prop *ext_prop,
1002 char *page)
1004 int len = ext_prop->data_len;
1006 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1007 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1008 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1009 len >>= 1;
1010 memcpy(page, ext_prop->data, len);
1012 return len;
1015 static ssize_t ext_prop_data_store(struct usb_os_desc_ext_prop *ext_prop,
1016 const char *page, size_t len)
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;
1022 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1023 --len;
1024 new_data = kmemdup(page, len, GFP_KERNEL);
1025 if (!new_data)
1026 return -ENOMEM;
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;
1043 if (desc->opts_mutex)
1044 mutex_unlock(desc->opts_mutex);
1045 return ret_len;
1048 static struct usb_os_desc_ext_prop_attribute ext_prop_type =
1049 __CONFIGFS_ATTR(type, S_IRUGO | S_IWUSR,
1050 ext_prop_type_show, ext_prop_type_store);
1052 static struct usb_os_desc_ext_prop_attribute ext_prop_data =
1053 __CONFIGFS_ATTR(data, S_IRUGO | S_IWUSR,
1054 ext_prop_data_show, ext_prop_data_store);
1056 static struct configfs_attribute *ext_prop_attrs[] = {
1057 &ext_prop_type.attr,
1058 &ext_prop_data.attr,
1059 NULL,
1062 static void usb_os_desc_ext_prop_release(struct config_item *item)
1064 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1066 kfree(ext_prop); /* frees a whole chunk */
1069 static struct configfs_item_operations ext_prop_ops = {
1070 .release = usb_os_desc_ext_prop_release,
1071 .show_attribute = usb_os_desc_ext_prop_attr_show,
1072 .store_attribute = usb_os_desc_ext_prop_attr_store,
1075 static struct config_item *ext_prop_make(
1076 struct config_group *group,
1077 const char *name)
1079 struct usb_os_desc_ext_prop *ext_prop;
1080 struct config_item_type *ext_prop_type;
1081 struct usb_os_desc *desc;
1082 char *vlabuf;
1084 vla_group(data_chunk);
1085 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1086 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1088 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1089 if (!vlabuf)
1090 return ERR_PTR(-ENOMEM);
1092 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1093 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1095 desc = container_of(group, struct usb_os_desc, group);
1096 ext_prop_type->ct_item_ops = &ext_prop_ops;
1097 ext_prop_type->ct_attrs = ext_prop_attrs;
1098 ext_prop_type->ct_owner = desc->owner;
1100 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1102 ext_prop->name = kstrdup(name, GFP_KERNEL);
1103 if (!ext_prop->name) {
1104 kfree(vlabuf);
1105 return ERR_PTR(-ENOMEM);
1107 desc->ext_prop_len += 14;
1108 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1109 if (desc->opts_mutex)
1110 mutex_lock(desc->opts_mutex);
1111 desc->ext_prop_len += ext_prop->name_len;
1112 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1113 ++desc->ext_prop_count;
1114 if (desc->opts_mutex)
1115 mutex_unlock(desc->opts_mutex);
1117 return &ext_prop->item;
1120 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1122 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1123 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1125 if (desc->opts_mutex)
1126 mutex_lock(desc->opts_mutex);
1127 list_del(&ext_prop->entry);
1128 --desc->ext_prop_count;
1129 kfree(ext_prop->name);
1130 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1131 if (desc->opts_mutex)
1132 mutex_unlock(desc->opts_mutex);
1133 config_item_put(item);
1136 static struct configfs_group_operations interf_grp_ops = {
1137 .make_item = &ext_prop_make,
1138 .drop_item = &ext_prop_drop,
1141 static struct configfs_item_operations interf_item_ops = {
1142 .show_attribute = usb_os_desc_attr_show,
1143 .store_attribute = usb_os_desc_attr_store,
1146 static ssize_t interf_grp_compatible_id_show(struct usb_os_desc *desc,
1147 char *page)
1149 memcpy(page, desc->ext_compat_id, 8);
1150 return 8;
1153 static ssize_t interf_grp_compatible_id_store(struct usb_os_desc *desc,
1154 const char *page, size_t len)
1156 int l;
1158 l = min_t(int, 8, len);
1159 if (page[l - 1] == '\n')
1160 --l;
1161 if (desc->opts_mutex)
1162 mutex_lock(desc->opts_mutex);
1163 memcpy(desc->ext_compat_id, page, l);
1165 if (desc->opts_mutex)
1166 mutex_unlock(desc->opts_mutex);
1168 return len;
1171 static struct usb_os_desc_attribute interf_grp_attr_compatible_id =
1172 __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR,
1173 interf_grp_compatible_id_show,
1174 interf_grp_compatible_id_store);
1176 static ssize_t interf_grp_sub_compatible_id_show(struct usb_os_desc *desc,
1177 char *page)
1179 memcpy(page, desc->ext_compat_id + 8, 8);
1180 return 8;
1183 static ssize_t interf_grp_sub_compatible_id_store(struct usb_os_desc *desc,
1184 const char *page, size_t len)
1186 int l;
1188 l = min_t(int, 8, len);
1189 if (page[l - 1] == '\n')
1190 --l;
1191 if (desc->opts_mutex)
1192 mutex_lock(desc->opts_mutex);
1193 memcpy(desc->ext_compat_id + 8, page, l);
1195 if (desc->opts_mutex)
1196 mutex_unlock(desc->opts_mutex);
1198 return len;
1201 static struct usb_os_desc_attribute interf_grp_attr_sub_compatible_id =
1202 __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR,
1203 interf_grp_sub_compatible_id_show,
1204 interf_grp_sub_compatible_id_store);
1206 static struct configfs_attribute *interf_grp_attrs[] = {
1207 &interf_grp_attr_compatible_id.attr,
1208 &interf_grp_attr_sub_compatible_id.attr,
1209 NULL
1212 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1213 int n_interf,
1214 struct usb_os_desc **desc,
1215 char **names,
1216 struct module *owner)
1218 struct config_group **f_default_groups, *os_desc_group,
1219 **interface_groups;
1220 struct config_item_type *os_desc_type, *interface_type;
1222 vla_group(data_chunk);
1223 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1224 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1225 vla_item(data_chunk, struct config_group *, interface_groups,
1226 n_interf + 1);
1227 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1228 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1230 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1231 if (!vlabuf)
1232 return -ENOMEM;
1234 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1235 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1236 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1237 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1238 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1240 parent->default_groups = f_default_groups;
1241 os_desc_type->ct_owner = owner;
1242 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1243 f_default_groups[0] = os_desc_group;
1245 os_desc_group->default_groups = interface_groups;
1246 interface_type->ct_item_ops = &interf_item_ops;
1247 interface_type->ct_group_ops = &interf_grp_ops;
1248 interface_type->ct_attrs = interf_grp_attrs;
1249 interface_type->ct_owner = owner;
1251 while (n_interf--) {
1252 struct usb_os_desc *d;
1254 d = desc[n_interf];
1255 d->owner = owner;
1256 config_group_init_type_name(&d->group, "", interface_type);
1257 config_item_set_name(&d->group.cg_item, "interface.%s",
1258 names[n_interf]);
1259 interface_groups[n_interf] = &d->group;
1262 return 0;
1264 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1266 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1268 WARN_ON(1);
1269 return -EINVAL;
1272 int composite_dev_prepare(struct usb_composite_driver *composite,
1273 struct usb_composite_dev *dev);
1275 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1276 struct usb_ep *ep0);
1278 static void purge_configs_funcs(struct gadget_info *gi)
1280 struct usb_configuration *c;
1282 list_for_each_entry(c, &gi->cdev.configs, list) {
1283 struct usb_function *f, *tmp;
1284 struct config_usb_cfg *cfg;
1286 cfg = container_of(c, struct config_usb_cfg, c);
1288 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1290 list_move_tail(&f->list, &cfg->func_list);
1291 if (f->unbind) {
1292 dev_err(&gi->cdev.gadget->dev, "unbind function"
1293 " '%s'/%p\n", f->name, f);
1294 f->unbind(c, f);
1297 c->next_interface_id = 0;
1298 memset(c->interface, 0, sizeof(c->interface));
1299 c->superspeed = 0;
1300 c->highspeed = 0;
1301 c->fullspeed = 0;
1305 static int configfs_composite_bind(struct usb_gadget *gadget,
1306 struct usb_gadget_driver *gdriver)
1308 struct usb_composite_driver *composite = to_cdriver(gdriver);
1309 struct gadget_info *gi = container_of(composite,
1310 struct gadget_info, composite);
1311 struct usb_composite_dev *cdev = &gi->cdev;
1312 struct usb_configuration *c;
1313 struct usb_string *s;
1314 unsigned i;
1315 int ret;
1317 /* the gi->lock is hold by the caller */
1318 cdev->gadget = gadget;
1319 set_gadget_data(gadget, cdev);
1320 ret = composite_dev_prepare(composite, cdev);
1321 if (ret)
1322 return ret;
1323 /* and now the gadget bind */
1324 ret = -EINVAL;
1326 if (list_empty(&gi->cdev.configs)) {
1327 pr_err("Need at least one configuration in %s.\n",
1328 gi->composite.name);
1329 goto err_comp_cleanup;
1333 list_for_each_entry(c, &gi->cdev.configs, list) {
1334 struct config_usb_cfg *cfg;
1336 cfg = container_of(c, struct config_usb_cfg, c);
1337 if (list_empty(&cfg->func_list)) {
1338 pr_err("Config %s/%d of %s needs at least one function.\n",
1339 c->label, c->bConfigurationValue,
1340 gi->composite.name);
1341 goto err_comp_cleanup;
1345 /* init all strings */
1346 if (!list_empty(&gi->string_list)) {
1347 struct gadget_strings *gs;
1349 i = 0;
1350 list_for_each_entry(gs, &gi->string_list, list) {
1352 gi->gstrings[i] = &gs->stringtab_dev;
1353 gs->stringtab_dev.strings = gs->strings;
1354 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1355 gs->manufacturer;
1356 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1357 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1358 i++;
1360 gi->gstrings[i] = NULL;
1361 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1362 USB_GADGET_FIRST_AVAIL_IDX);
1363 if (IS_ERR(s)) {
1364 ret = PTR_ERR(s);
1365 goto err_comp_cleanup;
1368 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1369 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1370 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1373 if (gi->use_os_desc) {
1374 cdev->use_os_string = true;
1375 cdev->b_vendor_code = gi->b_vendor_code;
1376 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1379 /* Go through all configs, attach all functions */
1380 list_for_each_entry(c, &gi->cdev.configs, list) {
1381 struct config_usb_cfg *cfg;
1382 struct usb_function *f;
1383 struct usb_function *tmp;
1384 struct gadget_config_name *cn;
1386 cfg = container_of(c, struct config_usb_cfg, c);
1387 if (!list_empty(&cfg->string_list)) {
1388 i = 0;
1389 list_for_each_entry(cn, &cfg->string_list, list) {
1390 cfg->gstrings[i] = &cn->stringtab_dev;
1391 cn->stringtab_dev.strings = &cn->strings;
1392 cn->strings.s = cn->configuration;
1393 i++;
1395 cfg->gstrings[i] = NULL;
1396 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1397 if (IS_ERR(s)) {
1398 ret = PTR_ERR(s);
1399 goto err_comp_cleanup;
1401 c->iConfiguration = s[0].id;
1404 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1405 list_del(&f->list);
1406 ret = usb_add_function(c, f);
1407 if (ret) {
1408 list_add(&f->list, &cfg->func_list);
1409 goto err_purge_funcs;
1412 usb_ep_autoconfig_reset(cdev->gadget);
1414 if (cdev->use_os_string) {
1415 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1416 if (ret)
1417 goto err_purge_funcs;
1420 usb_ep_autoconfig_reset(cdev->gadget);
1421 return 0;
1423 err_purge_funcs:
1424 purge_configs_funcs(gi);
1425 err_comp_cleanup:
1426 composite_dev_cleanup(cdev);
1427 return ret;
1430 static void configfs_composite_unbind(struct usb_gadget *gadget)
1432 struct usb_composite_dev *cdev;
1433 struct gadget_info *gi;
1435 /* the gi->lock is hold by the caller */
1437 cdev = get_gadget_data(gadget);
1438 gi = container_of(cdev, struct gadget_info, cdev);
1440 purge_configs_funcs(gi);
1441 composite_dev_cleanup(cdev);
1442 usb_ep_autoconfig_reset(cdev->gadget);
1443 cdev->gadget = NULL;
1444 set_gadget_data(gadget, NULL);
1447 static const struct usb_gadget_driver configfs_driver_template = {
1448 .bind = configfs_composite_bind,
1449 .unbind = configfs_composite_unbind,
1451 .setup = composite_setup,
1452 .reset = composite_disconnect,
1453 .disconnect = composite_disconnect,
1455 .suspend = composite_suspend,
1456 .resume = composite_resume,
1458 .max_speed = USB_SPEED_SUPER,
1459 .driver = {
1460 .owner = THIS_MODULE,
1461 .name = "configfs-gadget",
1465 static struct config_group *gadgets_make(
1466 struct config_group *group,
1467 const char *name)
1469 struct gadget_info *gi;
1471 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1472 if (!gi)
1473 return ERR_PTR(-ENOMEM);
1475 gi->group.default_groups = gi->default_groups;
1476 gi->group.default_groups[0] = &gi->functions_group;
1477 gi->group.default_groups[1] = &gi->configs_group;
1478 gi->group.default_groups[2] = &gi->strings_group;
1479 gi->group.default_groups[3] = &gi->os_desc_group;
1481 config_group_init_type_name(&gi->functions_group, "functions",
1482 &functions_type);
1483 config_group_init_type_name(&gi->configs_group, "configs",
1484 &config_desc_type);
1485 config_group_init_type_name(&gi->strings_group, "strings",
1486 &gadget_strings_strings_type);
1487 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1488 &os_desc_type);
1490 gi->composite.bind = configfs_do_nothing;
1491 gi->composite.unbind = configfs_do_nothing;
1492 gi->composite.suspend = NULL;
1493 gi->composite.resume = NULL;
1494 gi->composite.max_speed = USB_SPEED_SUPER;
1496 mutex_init(&gi->lock);
1497 INIT_LIST_HEAD(&gi->string_list);
1498 INIT_LIST_HEAD(&gi->available_func);
1500 composite_init_dev(&gi->cdev);
1501 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1502 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1503 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1505 gi->composite.gadget_driver = configfs_driver_template;
1507 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1508 gi->composite.name = gi->composite.gadget_driver.function;
1510 if (!gi->composite.gadget_driver.function)
1511 goto err;
1513 #ifdef CONFIG_USB_OTG
1514 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
1515 gi->otg.bDescriptorType = USB_DT_OTG;
1516 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
1517 #endif
1519 config_group_init_type_name(&gi->group, name,
1520 &gadget_root_type);
1521 return &gi->group;
1522 err:
1523 kfree(gi);
1524 return ERR_PTR(-ENOMEM);
1527 static void gadgets_drop(struct config_group *group, struct config_item *item)
1529 config_item_put(item);
1532 static struct configfs_group_operations gadgets_ops = {
1533 .make_group = &gadgets_make,
1534 .drop_item = &gadgets_drop,
1537 static struct config_item_type gadgets_type = {
1538 .ct_group_ops = &gadgets_ops,
1539 .ct_owner = THIS_MODULE,
1542 static struct configfs_subsystem gadget_subsys = {
1543 .su_group = {
1544 .cg_item = {
1545 .ci_namebuf = "usb_gadget",
1546 .ci_type = &gadgets_type,
1549 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1552 void unregister_gadget_item(struct config_item *item)
1554 struct gadget_info *gi = to_gadget_info(item);
1556 unregister_gadget(gi);
1558 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1560 static int __init gadget_cfs_init(void)
1562 int ret;
1564 config_group_init(&gadget_subsys.su_group);
1566 ret = configfs_register_subsystem(&gadget_subsys);
1567 return ret;
1569 module_init(gadget_cfs_init);
1571 static void __exit gadget_cfs_exit(void)
1573 configfs_unregister_subsystem(&gadget_subsys);
1575 module_exit(gadget_cfs_exit);