Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / usb / gadget / configfs.c
blob36ffb43f9c1a0541f8c4d84d7c02502567ac9ce2
1 // SPDX-License-Identifier: GPL-2.0
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"
13 int check_user_usb_string(const char *name,
14 struct usb_gadget_strings *stringtab_dev)
16 u16 num;
17 int ret;
19 ret = kstrtou16(name, 0, &num);
20 if (ret)
21 return ret;
23 if (!usb_validate_langid(num))
24 return -EINVAL;
26 stringtab_dev->language = num;
27 return 0;
30 #define MAX_NAME_LEN 40
31 #define MAX_USB_STRING_LANGS 2
33 static const struct usb_descriptor_header *otg_desc[2];
35 struct gadget_info {
36 struct config_group group;
37 struct config_group functions_group;
38 struct config_group configs_group;
39 struct config_group strings_group;
40 struct config_group os_desc_group;
42 struct mutex lock;
43 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
44 struct list_head string_list;
45 struct list_head available_func;
47 struct usb_composite_driver composite;
48 struct usb_composite_dev cdev;
49 bool use_os_desc;
50 char b_vendor_code;
51 char qw_sign[OS_STRING_QW_SIGN_LEN];
52 spinlock_t spinlock;
53 bool unbind;
56 static inline struct gadget_info *to_gadget_info(struct config_item *item)
58 return container_of(to_config_group(item), struct gadget_info, group);
61 struct config_usb_cfg {
62 struct config_group group;
63 struct config_group strings_group;
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];
70 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
72 return container_of(to_config_group(item), struct config_usb_cfg,
73 group);
76 struct gadget_strings {
77 struct usb_gadget_strings stringtab_dev;
78 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
79 char *manufacturer;
80 char *product;
81 char *serialnumber;
83 struct config_group group;
84 struct list_head list;
87 struct os_desc {
88 struct config_group group;
91 struct gadget_config_name {
92 struct usb_gadget_strings stringtab_dev;
93 struct usb_string strings;
94 char *configuration;
96 struct config_group group;
97 struct list_head list;
100 static int usb_string_copy(const char *s, char **s_copy)
102 int ret;
103 char *str;
104 char *copy = *s_copy;
105 ret = strlen(s);
106 if (ret > USB_MAX_STRING_LEN)
107 return -EOVERFLOW;
109 str = kstrdup(s, GFP_KERNEL);
110 if (!str)
111 return -ENOMEM;
112 if (str[ret - 1] == '\n')
113 str[ret - 1] = '\0';
114 kfree(copy);
115 *s_copy = str;
116 return 0;
119 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
120 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
121 char *page) \
123 return sprintf(page, "0x%02x\n", \
124 to_gadget_info(item)->cdev.desc.__name); \
127 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
128 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
129 char *page) \
131 return sprintf(page, "0x%04x\n", \
132 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
136 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
137 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
138 const char *page, size_t len) \
140 u8 val; \
141 int ret; \
142 ret = kstrtou8(page, 0, &val); \
143 if (ret) \
144 return ret; \
145 to_gadget_info(item)->cdev.desc._name = val; \
146 return len; \
149 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
150 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
151 const char *page, size_t len) \
153 u16 val; \
154 int ret; \
155 ret = kstrtou16(page, 0, &val); \
156 if (ret) \
157 return ret; \
158 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
159 return len; \
162 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
163 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
164 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
166 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
167 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
168 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
169 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
170 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
171 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
172 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
173 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
175 static ssize_t is_valid_bcd(u16 bcd_val)
177 if ((bcd_val & 0xf) > 9)
178 return -EINVAL;
179 if (((bcd_val >> 4) & 0xf) > 9)
180 return -EINVAL;
181 if (((bcd_val >> 8) & 0xf) > 9)
182 return -EINVAL;
183 if (((bcd_val >> 12) & 0xf) > 9)
184 return -EINVAL;
185 return 0;
188 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
189 const char *page, size_t len)
191 u16 bcdDevice;
192 int ret;
194 ret = kstrtou16(page, 0, &bcdDevice);
195 if (ret)
196 return ret;
197 ret = is_valid_bcd(bcdDevice);
198 if (ret)
199 return ret;
201 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
202 return len;
205 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
206 const char *page, size_t len)
208 u16 bcdUSB;
209 int ret;
211 ret = kstrtou16(page, 0, &bcdUSB);
212 if (ret)
213 return ret;
214 ret = is_valid_bcd(bcdUSB);
215 if (ret)
216 return ret;
218 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
219 return len;
222 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
224 struct gadget_info *gi = to_gadget_info(item);
225 char *udc_name;
226 int ret;
228 mutex_lock(&gi->lock);
229 udc_name = gi->composite.gadget_driver.udc_name;
230 ret = sprintf(page, "%s\n", udc_name ?: "");
231 mutex_unlock(&gi->lock);
233 return ret;
236 static int unregister_gadget(struct gadget_info *gi)
238 int ret;
240 if (!gi->composite.gadget_driver.udc_name)
241 return -ENODEV;
243 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
244 if (ret)
245 return ret;
246 kfree(gi->composite.gadget_driver.udc_name);
247 gi->composite.gadget_driver.udc_name = NULL;
248 return 0;
251 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
252 const char *page, size_t len)
254 struct gadget_info *gi = to_gadget_info(item);
255 char *name;
256 int ret;
258 if (strlen(page) < len)
259 return -EOVERFLOW;
261 name = kstrdup(page, GFP_KERNEL);
262 if (!name)
263 return -ENOMEM;
264 if (name[len - 1] == '\n')
265 name[len - 1] = '\0';
267 mutex_lock(&gi->lock);
269 if (!strlen(name)) {
270 ret = unregister_gadget(gi);
271 if (ret)
272 goto err;
273 kfree(name);
274 } else {
275 if (gi->composite.gadget_driver.udc_name) {
276 ret = -EBUSY;
277 goto err;
279 gi->composite.gadget_driver.udc_name = name;
280 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
281 if (ret) {
282 gi->composite.gadget_driver.udc_name = NULL;
283 goto err;
286 mutex_unlock(&gi->lock);
287 return len;
288 err:
289 kfree(name);
290 mutex_unlock(&gi->lock);
291 return ret;
294 static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
295 char *page)
297 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
299 return sprintf(page, "%s\n", usb_speed_string(speed));
302 static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
303 const char *page, size_t len)
305 struct gadget_info *gi = to_gadget_info(item);
307 mutex_lock(&gi->lock);
309 /* Prevent changing of max_speed after the driver is binded */
310 if (gi->composite.gadget_driver.udc_name)
311 goto err;
313 if (strncmp(page, "super-speed-plus", 16) == 0)
314 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
315 else if (strncmp(page, "super-speed", 11) == 0)
316 gi->composite.max_speed = USB_SPEED_SUPER;
317 else if (strncmp(page, "high-speed", 10) == 0)
318 gi->composite.max_speed = USB_SPEED_HIGH;
319 else if (strncmp(page, "full-speed", 10) == 0)
320 gi->composite.max_speed = USB_SPEED_FULL;
321 else if (strncmp(page, "low-speed", 9) == 0)
322 gi->composite.max_speed = USB_SPEED_LOW;
323 else
324 goto err;
326 gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
328 mutex_unlock(&gi->lock);
329 return len;
330 err:
331 mutex_unlock(&gi->lock);
332 return -EINVAL;
335 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
336 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
337 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
338 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
339 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
340 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
341 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
342 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
343 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
344 CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
346 static struct configfs_attribute *gadget_root_attrs[] = {
347 &gadget_dev_desc_attr_bDeviceClass,
348 &gadget_dev_desc_attr_bDeviceSubClass,
349 &gadget_dev_desc_attr_bDeviceProtocol,
350 &gadget_dev_desc_attr_bMaxPacketSize0,
351 &gadget_dev_desc_attr_idVendor,
352 &gadget_dev_desc_attr_idProduct,
353 &gadget_dev_desc_attr_bcdDevice,
354 &gadget_dev_desc_attr_bcdUSB,
355 &gadget_dev_desc_attr_UDC,
356 &gadget_dev_desc_attr_max_speed,
357 NULL,
360 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
362 return container_of(to_config_group(item), struct gadget_strings,
363 group);
366 static inline struct gadget_config_name *to_gadget_config_name(
367 struct config_item *item)
369 return container_of(to_config_group(item), struct gadget_config_name,
370 group);
373 static inline struct usb_function_instance *to_usb_function_instance(
374 struct config_item *item)
376 return container_of(to_config_group(item),
377 struct usb_function_instance, group);
380 static void gadget_info_attr_release(struct config_item *item)
382 struct gadget_info *gi = to_gadget_info(item);
384 WARN_ON(!list_empty(&gi->cdev.configs));
385 WARN_ON(!list_empty(&gi->string_list));
386 WARN_ON(!list_empty(&gi->available_func));
387 kfree(gi->composite.gadget_driver.function);
388 kfree(gi);
391 static struct configfs_item_operations gadget_root_item_ops = {
392 .release = gadget_info_attr_release,
395 static void gadget_config_attr_release(struct config_item *item)
397 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
399 WARN_ON(!list_empty(&cfg->c.functions));
400 list_del(&cfg->c.list);
401 kfree(cfg->c.label);
402 kfree(cfg);
405 static int config_usb_cfg_link(
406 struct config_item *usb_cfg_ci,
407 struct config_item *usb_func_ci)
409 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
410 struct usb_composite_dev *cdev = cfg->c.cdev;
411 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
413 struct config_group *group = to_config_group(usb_func_ci);
414 struct usb_function_instance *fi = container_of(group,
415 struct usb_function_instance, group);
416 struct usb_function_instance *a_fi;
417 struct usb_function *f;
418 int ret;
420 mutex_lock(&gi->lock);
422 * Make sure this function is from within our _this_ gadget and not
423 * from another gadget or a random directory.
424 * Also a function instance can only be linked once.
426 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
427 if (a_fi == fi)
428 break;
430 if (a_fi != fi) {
431 ret = -EINVAL;
432 goto out;
435 list_for_each_entry(f, &cfg->func_list, list) {
436 if (f->fi == fi) {
437 ret = -EEXIST;
438 goto out;
442 f = usb_get_function(fi);
443 if (IS_ERR(f)) {
444 ret = PTR_ERR(f);
445 goto out;
448 /* stash the function until we bind it to the gadget */
449 list_add_tail(&f->list, &cfg->func_list);
450 ret = 0;
451 out:
452 mutex_unlock(&gi->lock);
453 return ret;
456 static void config_usb_cfg_unlink(
457 struct config_item *usb_cfg_ci,
458 struct config_item *usb_func_ci)
460 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
461 struct usb_composite_dev *cdev = cfg->c.cdev;
462 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
464 struct config_group *group = to_config_group(usb_func_ci);
465 struct usb_function_instance *fi = container_of(group,
466 struct usb_function_instance, group);
467 struct usb_function *f;
470 * ideally I would like to forbid to unlink functions while a gadget is
471 * bound to an UDC. Since this isn't possible at the moment, we simply
472 * force an unbind, the function is available here and then we can
473 * remove the function.
475 mutex_lock(&gi->lock);
476 if (gi->composite.gadget_driver.udc_name)
477 unregister_gadget(gi);
478 WARN_ON(gi->composite.gadget_driver.udc_name);
480 list_for_each_entry(f, &cfg->func_list, list) {
481 if (f->fi == fi) {
482 list_del(&f->list);
483 usb_put_function(f);
484 mutex_unlock(&gi->lock);
485 return;
488 mutex_unlock(&gi->lock);
489 WARN(1, "Unable to locate function to unbind\n");
492 static struct configfs_item_operations gadget_config_item_ops = {
493 .release = gadget_config_attr_release,
494 .allow_link = config_usb_cfg_link,
495 .drop_link = config_usb_cfg_unlink,
499 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
500 char *page)
502 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
505 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
506 const char *page, size_t len)
508 u16 val;
509 int ret;
510 ret = kstrtou16(page, 0, &val);
511 if (ret)
512 return ret;
513 if (DIV_ROUND_UP(val, 8) > 0xff)
514 return -ERANGE;
515 to_config_usb_cfg(item)->c.MaxPower = val;
516 return len;
519 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
520 char *page)
522 return sprintf(page, "0x%02x\n",
523 to_config_usb_cfg(item)->c.bmAttributes);
526 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
527 const char *page, size_t len)
529 u8 val;
530 int ret;
531 ret = kstrtou8(page, 0, &val);
532 if (ret)
533 return ret;
534 if (!(val & USB_CONFIG_ATT_ONE))
535 return -EINVAL;
536 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
537 USB_CONFIG_ATT_WAKEUP))
538 return -EINVAL;
539 to_config_usb_cfg(item)->c.bmAttributes = val;
540 return len;
543 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
544 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
546 static struct configfs_attribute *gadget_config_attrs[] = {
547 &gadget_config_desc_attr_MaxPower,
548 &gadget_config_desc_attr_bmAttributes,
549 NULL,
552 static const struct config_item_type gadget_config_type = {
553 .ct_item_ops = &gadget_config_item_ops,
554 .ct_attrs = gadget_config_attrs,
555 .ct_owner = THIS_MODULE,
558 static const struct config_item_type gadget_root_type = {
559 .ct_item_ops = &gadget_root_item_ops,
560 .ct_attrs = gadget_root_attrs,
561 .ct_owner = THIS_MODULE,
564 static void composite_init_dev(struct usb_composite_dev *cdev)
566 spin_lock_init(&cdev->lock);
567 INIT_LIST_HEAD(&cdev->configs);
568 INIT_LIST_HEAD(&cdev->gstrings);
571 static struct config_group *function_make(
572 struct config_group *group,
573 const char *name)
575 struct gadget_info *gi;
576 struct usb_function_instance *fi;
577 char buf[MAX_NAME_LEN];
578 char *func_name;
579 char *instance_name;
580 int ret;
582 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
583 if (ret >= MAX_NAME_LEN)
584 return ERR_PTR(-ENAMETOOLONG);
586 func_name = buf;
587 instance_name = strchr(func_name, '.');
588 if (!instance_name) {
589 pr_err("Unable to locate . in FUNC.INSTANCE\n");
590 return ERR_PTR(-EINVAL);
592 *instance_name = '\0';
593 instance_name++;
595 fi = usb_get_function_instance(func_name);
596 if (IS_ERR(fi))
597 return ERR_CAST(fi);
599 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
600 if (ret) {
601 usb_put_function_instance(fi);
602 return ERR_PTR(ret);
604 if (fi->set_inst_name) {
605 ret = fi->set_inst_name(fi, instance_name);
606 if (ret) {
607 usb_put_function_instance(fi);
608 return ERR_PTR(ret);
612 gi = container_of(group, struct gadget_info, functions_group);
614 mutex_lock(&gi->lock);
615 list_add_tail(&fi->cfs_list, &gi->available_func);
616 mutex_unlock(&gi->lock);
617 return &fi->group;
620 static void function_drop(
621 struct config_group *group,
622 struct config_item *item)
624 struct usb_function_instance *fi = to_usb_function_instance(item);
625 struct gadget_info *gi;
627 gi = container_of(group, struct gadget_info, functions_group);
629 mutex_lock(&gi->lock);
630 list_del(&fi->cfs_list);
631 mutex_unlock(&gi->lock);
632 config_item_put(item);
635 static struct configfs_group_operations functions_ops = {
636 .make_group = &function_make,
637 .drop_item = &function_drop,
640 static const struct config_item_type functions_type = {
641 .ct_group_ops = &functions_ops,
642 .ct_owner = THIS_MODULE,
645 GS_STRINGS_RW(gadget_config_name, configuration);
647 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
648 &gadget_config_name_attr_configuration,
649 NULL,
652 static void gadget_config_name_attr_release(struct config_item *item)
654 struct gadget_config_name *cn = to_gadget_config_name(item);
656 kfree(cn->configuration);
658 list_del(&cn->list);
659 kfree(cn);
662 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
663 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
665 static struct config_group *config_desc_make(
666 struct config_group *group,
667 const char *name)
669 struct gadget_info *gi;
670 struct config_usb_cfg *cfg;
671 char buf[MAX_NAME_LEN];
672 char *num_str;
673 u8 num;
674 int ret;
676 gi = container_of(group, struct gadget_info, configs_group);
677 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
678 if (ret >= MAX_NAME_LEN)
679 return ERR_PTR(-ENAMETOOLONG);
681 num_str = strchr(buf, '.');
682 if (!num_str) {
683 pr_err("Unable to locate . in name.bConfigurationValue\n");
684 return ERR_PTR(-EINVAL);
687 *num_str = '\0';
688 num_str++;
690 if (!strlen(buf))
691 return ERR_PTR(-EINVAL);
693 ret = kstrtou8(num_str, 0, &num);
694 if (ret)
695 return ERR_PTR(ret);
697 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
698 if (!cfg)
699 return ERR_PTR(-ENOMEM);
700 cfg->c.label = kstrdup(buf, GFP_KERNEL);
701 if (!cfg->c.label) {
702 ret = -ENOMEM;
703 goto err;
705 cfg->c.bConfigurationValue = num;
706 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
707 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
708 INIT_LIST_HEAD(&cfg->string_list);
709 INIT_LIST_HEAD(&cfg->func_list);
711 config_group_init_type_name(&cfg->group, name,
712 &gadget_config_type);
714 config_group_init_type_name(&cfg->strings_group, "strings",
715 &gadget_config_name_strings_type);
716 configfs_add_default_group(&cfg->strings_group, &cfg->group);
718 ret = usb_add_config_only(&gi->cdev, &cfg->c);
719 if (ret)
720 goto err;
722 return &cfg->group;
723 err:
724 kfree(cfg->c.label);
725 kfree(cfg);
726 return ERR_PTR(ret);
729 static void config_desc_drop(
730 struct config_group *group,
731 struct config_item *item)
733 config_item_put(item);
736 static struct configfs_group_operations config_desc_ops = {
737 .make_group = &config_desc_make,
738 .drop_item = &config_desc_drop,
741 static const struct config_item_type config_desc_type = {
742 .ct_group_ops = &config_desc_ops,
743 .ct_owner = THIS_MODULE,
746 GS_STRINGS_RW(gadget_strings, manufacturer);
747 GS_STRINGS_RW(gadget_strings, product);
748 GS_STRINGS_RW(gadget_strings, serialnumber);
750 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
751 &gadget_strings_attr_manufacturer,
752 &gadget_strings_attr_product,
753 &gadget_strings_attr_serialnumber,
754 NULL,
757 static void gadget_strings_attr_release(struct config_item *item)
759 struct gadget_strings *gs = to_gadget_strings(item);
761 kfree(gs->manufacturer);
762 kfree(gs->product);
763 kfree(gs->serialnumber);
765 list_del(&gs->list);
766 kfree(gs);
769 USB_CONFIG_STRING_RW_OPS(gadget_strings);
770 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
772 static inline struct os_desc *to_os_desc(struct config_item *item)
774 return container_of(to_config_group(item), struct os_desc, group);
777 static inline struct gadget_info *os_desc_item_to_gadget_info(
778 struct config_item *item)
780 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
783 static ssize_t os_desc_use_show(struct config_item *item, char *page)
785 return sprintf(page, "%d\n",
786 os_desc_item_to_gadget_info(item)->use_os_desc);
789 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
790 size_t len)
792 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
793 int ret;
794 bool use;
796 mutex_lock(&gi->lock);
797 ret = strtobool(page, &use);
798 if (!ret) {
799 gi->use_os_desc = use;
800 ret = len;
802 mutex_unlock(&gi->lock);
804 return ret;
807 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
809 return sprintf(page, "0x%02x\n",
810 os_desc_item_to_gadget_info(item)->b_vendor_code);
813 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
814 const char *page, size_t len)
816 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
817 int ret;
818 u8 b_vendor_code;
820 mutex_lock(&gi->lock);
821 ret = kstrtou8(page, 0, &b_vendor_code);
822 if (!ret) {
823 gi->b_vendor_code = b_vendor_code;
824 ret = len;
826 mutex_unlock(&gi->lock);
828 return ret;
831 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
833 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
834 int res;
836 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
837 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
838 page[res++] = '\n';
840 return res;
843 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
844 size_t len)
846 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
847 int res, l;
849 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
850 if (page[l - 1] == '\n')
851 --l;
853 mutex_lock(&gi->lock);
854 res = utf8s_to_utf16s(page, l,
855 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
856 OS_STRING_QW_SIGN_LEN);
857 if (res > 0)
858 res = len;
859 mutex_unlock(&gi->lock);
861 return res;
864 CONFIGFS_ATTR(os_desc_, use);
865 CONFIGFS_ATTR(os_desc_, b_vendor_code);
866 CONFIGFS_ATTR(os_desc_, qw_sign);
868 static struct configfs_attribute *os_desc_attrs[] = {
869 &os_desc_attr_use,
870 &os_desc_attr_b_vendor_code,
871 &os_desc_attr_qw_sign,
872 NULL,
875 static void os_desc_attr_release(struct config_item *item)
877 struct os_desc *os_desc = to_os_desc(item);
878 kfree(os_desc);
881 static int os_desc_link(struct config_item *os_desc_ci,
882 struct config_item *usb_cfg_ci)
884 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
885 struct gadget_info, os_desc_group);
886 struct usb_composite_dev *cdev = &gi->cdev;
887 struct config_usb_cfg *c_target =
888 container_of(to_config_group(usb_cfg_ci),
889 struct config_usb_cfg, group);
890 struct usb_configuration *c;
891 int ret;
893 mutex_lock(&gi->lock);
894 list_for_each_entry(c, &cdev->configs, list) {
895 if (c == &c_target->c)
896 break;
898 if (c != &c_target->c) {
899 ret = -EINVAL;
900 goto out;
903 if (cdev->os_desc_config) {
904 ret = -EBUSY;
905 goto out;
908 cdev->os_desc_config = &c_target->c;
909 ret = 0;
911 out:
912 mutex_unlock(&gi->lock);
913 return ret;
916 static void os_desc_unlink(struct config_item *os_desc_ci,
917 struct config_item *usb_cfg_ci)
919 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
920 struct gadget_info, os_desc_group);
921 struct usb_composite_dev *cdev = &gi->cdev;
923 mutex_lock(&gi->lock);
924 if (gi->composite.gadget_driver.udc_name)
925 unregister_gadget(gi);
926 cdev->os_desc_config = NULL;
927 WARN_ON(gi->composite.gadget_driver.udc_name);
928 mutex_unlock(&gi->lock);
931 static struct configfs_item_operations os_desc_ops = {
932 .release = os_desc_attr_release,
933 .allow_link = os_desc_link,
934 .drop_link = os_desc_unlink,
937 static struct config_item_type os_desc_type = {
938 .ct_item_ops = &os_desc_ops,
939 .ct_attrs = os_desc_attrs,
940 .ct_owner = THIS_MODULE,
943 static inline struct usb_os_desc_ext_prop
944 *to_usb_os_desc_ext_prop(struct config_item *item)
946 return container_of(item, struct usb_os_desc_ext_prop, item);
949 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
951 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
954 static ssize_t ext_prop_type_store(struct config_item *item,
955 const char *page, size_t len)
957 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
958 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
959 u8 type;
960 int ret;
962 if (desc->opts_mutex)
963 mutex_lock(desc->opts_mutex);
964 ret = kstrtou8(page, 0, &type);
965 if (ret)
966 goto end;
967 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
968 ret = -EINVAL;
969 goto end;
972 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
973 ext_prop->type == USB_EXT_PROP_LE32 ||
974 ext_prop->type == USB_EXT_PROP_BE32) &&
975 (type == USB_EXT_PROP_UNICODE ||
976 type == USB_EXT_PROP_UNICODE_ENV ||
977 type == USB_EXT_PROP_UNICODE_LINK))
978 ext_prop->data_len <<= 1;
979 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
980 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
981 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
982 (type == USB_EXT_PROP_BINARY ||
983 type == USB_EXT_PROP_LE32 ||
984 type == USB_EXT_PROP_BE32))
985 ext_prop->data_len >>= 1;
986 ext_prop->type = type;
987 ret = len;
989 end:
990 if (desc->opts_mutex)
991 mutex_unlock(desc->opts_mutex);
992 return ret;
995 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
997 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
998 int len = ext_prop->data_len;
1000 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1001 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1002 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1003 len >>= 1;
1004 memcpy(page, ext_prop->data, len);
1006 return len;
1009 static ssize_t ext_prop_data_store(struct config_item *item,
1010 const char *page, size_t len)
1012 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1013 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1014 char *new_data;
1015 size_t ret_len = len;
1017 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1018 --len;
1019 new_data = kmemdup(page, len, GFP_KERNEL);
1020 if (!new_data)
1021 return -ENOMEM;
1023 if (desc->opts_mutex)
1024 mutex_lock(desc->opts_mutex);
1025 kfree(ext_prop->data);
1026 ext_prop->data = new_data;
1027 desc->ext_prop_len -= ext_prop->data_len;
1028 ext_prop->data_len = len;
1029 desc->ext_prop_len += ext_prop->data_len;
1030 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1031 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1032 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1033 desc->ext_prop_len -= ext_prop->data_len;
1034 ext_prop->data_len <<= 1;
1035 ext_prop->data_len += 2;
1036 desc->ext_prop_len += ext_prop->data_len;
1038 if (desc->opts_mutex)
1039 mutex_unlock(desc->opts_mutex);
1040 return ret_len;
1043 CONFIGFS_ATTR(ext_prop_, type);
1044 CONFIGFS_ATTR(ext_prop_, data);
1046 static struct configfs_attribute *ext_prop_attrs[] = {
1047 &ext_prop_attr_type,
1048 &ext_prop_attr_data,
1049 NULL,
1052 static void usb_os_desc_ext_prop_release(struct config_item *item)
1054 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1056 kfree(ext_prop); /* frees a whole chunk */
1059 static struct configfs_item_operations ext_prop_ops = {
1060 .release = usb_os_desc_ext_prop_release,
1063 static struct config_item *ext_prop_make(
1064 struct config_group *group,
1065 const char *name)
1067 struct usb_os_desc_ext_prop *ext_prop;
1068 struct config_item_type *ext_prop_type;
1069 struct usb_os_desc *desc;
1070 char *vlabuf;
1072 vla_group(data_chunk);
1073 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1074 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1076 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1077 if (!vlabuf)
1078 return ERR_PTR(-ENOMEM);
1080 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1081 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1083 desc = container_of(group, struct usb_os_desc, group);
1084 ext_prop_type->ct_item_ops = &ext_prop_ops;
1085 ext_prop_type->ct_attrs = ext_prop_attrs;
1086 ext_prop_type->ct_owner = desc->owner;
1088 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1090 ext_prop->name = kstrdup(name, GFP_KERNEL);
1091 if (!ext_prop->name) {
1092 kfree(vlabuf);
1093 return ERR_PTR(-ENOMEM);
1095 desc->ext_prop_len += 14;
1096 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1097 if (desc->opts_mutex)
1098 mutex_lock(desc->opts_mutex);
1099 desc->ext_prop_len += ext_prop->name_len;
1100 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1101 ++desc->ext_prop_count;
1102 if (desc->opts_mutex)
1103 mutex_unlock(desc->opts_mutex);
1105 return &ext_prop->item;
1108 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1110 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1111 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1113 if (desc->opts_mutex)
1114 mutex_lock(desc->opts_mutex);
1115 list_del(&ext_prop->entry);
1116 --desc->ext_prop_count;
1117 kfree(ext_prop->name);
1118 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1119 if (desc->opts_mutex)
1120 mutex_unlock(desc->opts_mutex);
1121 config_item_put(item);
1124 static struct configfs_group_operations interf_grp_ops = {
1125 .make_item = &ext_prop_make,
1126 .drop_item = &ext_prop_drop,
1129 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1130 char *page)
1132 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1133 return 8;
1136 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1137 const char *page, size_t len)
1139 struct usb_os_desc *desc = to_usb_os_desc(item);
1140 int l;
1142 l = min_t(int, 8, len);
1143 if (page[l - 1] == '\n')
1144 --l;
1145 if (desc->opts_mutex)
1146 mutex_lock(desc->opts_mutex);
1147 memcpy(desc->ext_compat_id, page, l);
1149 if (desc->opts_mutex)
1150 mutex_unlock(desc->opts_mutex);
1152 return len;
1155 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1156 char *page)
1158 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1159 return 8;
1162 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1163 const char *page, size_t len)
1165 struct usb_os_desc *desc = to_usb_os_desc(item);
1166 int l;
1168 l = min_t(int, 8, len);
1169 if (page[l - 1] == '\n')
1170 --l;
1171 if (desc->opts_mutex)
1172 mutex_lock(desc->opts_mutex);
1173 memcpy(desc->ext_compat_id + 8, page, l);
1175 if (desc->opts_mutex)
1176 mutex_unlock(desc->opts_mutex);
1178 return len;
1181 CONFIGFS_ATTR(interf_grp_, compatible_id);
1182 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1184 static struct configfs_attribute *interf_grp_attrs[] = {
1185 &interf_grp_attr_compatible_id,
1186 &interf_grp_attr_sub_compatible_id,
1187 NULL
1190 struct config_group *usb_os_desc_prepare_interf_dir(
1191 struct config_group *parent,
1192 int n_interf,
1193 struct usb_os_desc **desc,
1194 char **names,
1195 struct module *owner)
1197 struct config_group *os_desc_group;
1198 struct config_item_type *os_desc_type, *interface_type;
1200 vla_group(data_chunk);
1201 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1202 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1203 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1205 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1206 if (!vlabuf)
1207 return ERR_PTR(-ENOMEM);
1209 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1210 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1211 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1213 os_desc_type->ct_owner = owner;
1214 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1215 configfs_add_default_group(os_desc_group, parent);
1217 interface_type->ct_group_ops = &interf_grp_ops;
1218 interface_type->ct_attrs = interf_grp_attrs;
1219 interface_type->ct_owner = owner;
1221 while (n_interf--) {
1222 struct usb_os_desc *d;
1224 d = desc[n_interf];
1225 d->owner = owner;
1226 config_group_init_type_name(&d->group, "", interface_type);
1227 config_item_set_name(&d->group.cg_item, "interface.%s",
1228 names[n_interf]);
1229 configfs_add_default_group(&d->group, os_desc_group);
1232 return os_desc_group;
1234 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1236 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1238 WARN_ON(1);
1239 return -EINVAL;
1242 int composite_dev_prepare(struct usb_composite_driver *composite,
1243 struct usb_composite_dev *dev);
1245 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1246 struct usb_ep *ep0);
1248 static void purge_configs_funcs(struct gadget_info *gi)
1250 struct usb_configuration *c;
1252 list_for_each_entry(c, &gi->cdev.configs, list) {
1253 struct usb_function *f, *tmp;
1254 struct config_usb_cfg *cfg;
1256 cfg = container_of(c, struct config_usb_cfg, c);
1258 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1260 list_move(&f->list, &cfg->func_list);
1261 if (f->unbind) {
1262 dev_dbg(&gi->cdev.gadget->dev,
1263 "unbind function '%s'/%p\n",
1264 f->name, f);
1265 f->unbind(c, f);
1268 c->next_interface_id = 0;
1269 memset(c->interface, 0, sizeof(c->interface));
1270 c->superspeed_plus = 0;
1271 c->superspeed = 0;
1272 c->highspeed = 0;
1273 c->fullspeed = 0;
1277 static int configfs_composite_bind(struct usb_gadget *gadget,
1278 struct usb_gadget_driver *gdriver)
1280 struct usb_composite_driver *composite = to_cdriver(gdriver);
1281 struct gadget_info *gi = container_of(composite,
1282 struct gadget_info, composite);
1283 struct usb_composite_dev *cdev = &gi->cdev;
1284 struct usb_configuration *c;
1285 struct usb_string *s;
1286 unsigned i;
1287 int ret;
1289 /* the gi->lock is hold by the caller */
1290 gi->unbind = 0;
1291 cdev->gadget = gadget;
1292 set_gadget_data(gadget, cdev);
1293 ret = composite_dev_prepare(composite, cdev);
1294 if (ret)
1295 return ret;
1296 /* and now the gadget bind */
1297 ret = -EINVAL;
1299 if (list_empty(&gi->cdev.configs)) {
1300 pr_err("Need at least one configuration in %s.\n",
1301 gi->composite.name);
1302 goto err_comp_cleanup;
1306 list_for_each_entry(c, &gi->cdev.configs, list) {
1307 struct config_usb_cfg *cfg;
1309 cfg = container_of(c, struct config_usb_cfg, c);
1310 if (list_empty(&cfg->func_list)) {
1311 pr_err("Config %s/%d of %s needs at least one function.\n",
1312 c->label, c->bConfigurationValue,
1313 gi->composite.name);
1314 goto err_comp_cleanup;
1318 /* init all strings */
1319 if (!list_empty(&gi->string_list)) {
1320 struct gadget_strings *gs;
1322 i = 0;
1323 list_for_each_entry(gs, &gi->string_list, list) {
1325 gi->gstrings[i] = &gs->stringtab_dev;
1326 gs->stringtab_dev.strings = gs->strings;
1327 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1328 gs->manufacturer;
1329 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1330 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1331 i++;
1333 gi->gstrings[i] = NULL;
1334 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1335 USB_GADGET_FIRST_AVAIL_IDX);
1336 if (IS_ERR(s)) {
1337 ret = PTR_ERR(s);
1338 goto err_comp_cleanup;
1341 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1342 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1343 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1346 if (gi->use_os_desc) {
1347 cdev->use_os_string = true;
1348 cdev->b_vendor_code = gi->b_vendor_code;
1349 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1352 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1353 struct usb_descriptor_header *usb_desc;
1355 usb_desc = usb_otg_descriptor_alloc(gadget);
1356 if (!usb_desc) {
1357 ret = -ENOMEM;
1358 goto err_comp_cleanup;
1360 usb_otg_descriptor_init(gadget, usb_desc);
1361 otg_desc[0] = usb_desc;
1362 otg_desc[1] = NULL;
1365 /* Go through all configs, attach all functions */
1366 list_for_each_entry(c, &gi->cdev.configs, list) {
1367 struct config_usb_cfg *cfg;
1368 struct usb_function *f;
1369 struct usb_function *tmp;
1370 struct gadget_config_name *cn;
1372 if (gadget_is_otg(gadget))
1373 c->descriptors = otg_desc;
1375 cfg = container_of(c, struct config_usb_cfg, c);
1376 if (!list_empty(&cfg->string_list)) {
1377 i = 0;
1378 list_for_each_entry(cn, &cfg->string_list, list) {
1379 cfg->gstrings[i] = &cn->stringtab_dev;
1380 cn->stringtab_dev.strings = &cn->strings;
1381 cn->strings.s = cn->configuration;
1382 i++;
1384 cfg->gstrings[i] = NULL;
1385 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1386 if (IS_ERR(s)) {
1387 ret = PTR_ERR(s);
1388 goto err_comp_cleanup;
1390 c->iConfiguration = s[0].id;
1393 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1394 list_del(&f->list);
1395 ret = usb_add_function(c, f);
1396 if (ret) {
1397 list_add(&f->list, &cfg->func_list);
1398 goto err_purge_funcs;
1401 usb_ep_autoconfig_reset(cdev->gadget);
1403 if (cdev->use_os_string) {
1404 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1405 if (ret)
1406 goto err_purge_funcs;
1409 usb_ep_autoconfig_reset(cdev->gadget);
1410 return 0;
1412 err_purge_funcs:
1413 purge_configs_funcs(gi);
1414 err_comp_cleanup:
1415 composite_dev_cleanup(cdev);
1416 return ret;
1419 static void configfs_composite_unbind(struct usb_gadget *gadget)
1421 struct usb_composite_dev *cdev;
1422 struct gadget_info *gi;
1423 unsigned long flags;
1425 /* the gi->lock is hold by the caller */
1427 cdev = get_gadget_data(gadget);
1428 gi = container_of(cdev, struct gadget_info, cdev);
1429 spin_lock_irqsave(&gi->spinlock, flags);
1430 gi->unbind = 1;
1431 spin_unlock_irqrestore(&gi->spinlock, flags);
1433 kfree(otg_desc[0]);
1434 otg_desc[0] = NULL;
1435 purge_configs_funcs(gi);
1436 composite_dev_cleanup(cdev);
1437 usb_ep_autoconfig_reset(cdev->gadget);
1438 spin_lock_irqsave(&gi->spinlock, flags);
1439 cdev->gadget = NULL;
1440 set_gadget_data(gadget, NULL);
1441 spin_unlock_irqrestore(&gi->spinlock, flags);
1444 static int configfs_composite_setup(struct usb_gadget *gadget,
1445 const struct usb_ctrlrequest *ctrl)
1447 struct usb_composite_dev *cdev;
1448 struct gadget_info *gi;
1449 unsigned long flags;
1450 int ret;
1452 cdev = get_gadget_data(gadget);
1453 if (!cdev)
1454 return 0;
1456 gi = container_of(cdev, struct gadget_info, cdev);
1457 spin_lock_irqsave(&gi->spinlock, flags);
1458 cdev = get_gadget_data(gadget);
1459 if (!cdev || gi->unbind) {
1460 spin_unlock_irqrestore(&gi->spinlock, flags);
1461 return 0;
1464 ret = composite_setup(gadget, ctrl);
1465 spin_unlock_irqrestore(&gi->spinlock, flags);
1466 return ret;
1469 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1471 struct usb_composite_dev *cdev;
1472 struct gadget_info *gi;
1473 unsigned long flags;
1475 cdev = get_gadget_data(gadget);
1476 if (!cdev)
1477 return;
1479 gi = container_of(cdev, struct gadget_info, cdev);
1480 spin_lock_irqsave(&gi->spinlock, flags);
1481 cdev = get_gadget_data(gadget);
1482 if (!cdev || gi->unbind) {
1483 spin_unlock_irqrestore(&gi->spinlock, flags);
1484 return;
1487 composite_disconnect(gadget);
1488 spin_unlock_irqrestore(&gi->spinlock, flags);
1491 static void configfs_composite_suspend(struct usb_gadget *gadget)
1493 struct usb_composite_dev *cdev;
1494 struct gadget_info *gi;
1495 unsigned long flags;
1497 cdev = get_gadget_data(gadget);
1498 if (!cdev)
1499 return;
1501 gi = container_of(cdev, struct gadget_info, cdev);
1502 spin_lock_irqsave(&gi->spinlock, flags);
1503 cdev = get_gadget_data(gadget);
1504 if (!cdev || gi->unbind) {
1505 spin_unlock_irqrestore(&gi->spinlock, flags);
1506 return;
1509 composite_suspend(gadget);
1510 spin_unlock_irqrestore(&gi->spinlock, flags);
1513 static void configfs_composite_resume(struct usb_gadget *gadget)
1515 struct usb_composite_dev *cdev;
1516 struct gadget_info *gi;
1517 unsigned long flags;
1519 cdev = get_gadget_data(gadget);
1520 if (!cdev)
1521 return;
1523 gi = container_of(cdev, struct gadget_info, cdev);
1524 spin_lock_irqsave(&gi->spinlock, flags);
1525 cdev = get_gadget_data(gadget);
1526 if (!cdev || gi->unbind) {
1527 spin_unlock_irqrestore(&gi->spinlock, flags);
1528 return;
1531 composite_resume(gadget);
1532 spin_unlock_irqrestore(&gi->spinlock, flags);
1535 static const struct usb_gadget_driver configfs_driver_template = {
1536 .bind = configfs_composite_bind,
1537 .unbind = configfs_composite_unbind,
1539 .setup = configfs_composite_setup,
1540 .reset = configfs_composite_disconnect,
1541 .disconnect = configfs_composite_disconnect,
1543 .suspend = configfs_composite_suspend,
1544 .resume = configfs_composite_resume,
1546 .max_speed = USB_SPEED_SUPER_PLUS,
1547 .driver = {
1548 .owner = THIS_MODULE,
1549 .name = "configfs-gadget",
1551 .match_existing_only = 1,
1554 static struct config_group *gadgets_make(
1555 struct config_group *group,
1556 const char *name)
1558 struct gadget_info *gi;
1560 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1561 if (!gi)
1562 return ERR_PTR(-ENOMEM);
1564 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1566 config_group_init_type_name(&gi->functions_group, "functions",
1567 &functions_type);
1568 configfs_add_default_group(&gi->functions_group, &gi->group);
1570 config_group_init_type_name(&gi->configs_group, "configs",
1571 &config_desc_type);
1572 configfs_add_default_group(&gi->configs_group, &gi->group);
1574 config_group_init_type_name(&gi->strings_group, "strings",
1575 &gadget_strings_strings_type);
1576 configfs_add_default_group(&gi->strings_group, &gi->group);
1578 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1579 &os_desc_type);
1580 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1582 gi->composite.bind = configfs_do_nothing;
1583 gi->composite.unbind = configfs_do_nothing;
1584 gi->composite.suspend = NULL;
1585 gi->composite.resume = NULL;
1586 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1588 spin_lock_init(&gi->spinlock);
1589 mutex_init(&gi->lock);
1590 INIT_LIST_HEAD(&gi->string_list);
1591 INIT_LIST_HEAD(&gi->available_func);
1593 composite_init_dev(&gi->cdev);
1594 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1595 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1596 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1598 gi->composite.gadget_driver = configfs_driver_template;
1600 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1601 gi->composite.name = gi->composite.gadget_driver.function;
1603 if (!gi->composite.gadget_driver.function)
1604 goto err;
1606 return &gi->group;
1607 err:
1608 kfree(gi);
1609 return ERR_PTR(-ENOMEM);
1612 static void gadgets_drop(struct config_group *group, struct config_item *item)
1614 config_item_put(item);
1617 static struct configfs_group_operations gadgets_ops = {
1618 .make_group = &gadgets_make,
1619 .drop_item = &gadgets_drop,
1622 static const struct config_item_type gadgets_type = {
1623 .ct_group_ops = &gadgets_ops,
1624 .ct_owner = THIS_MODULE,
1627 static struct configfs_subsystem gadget_subsys = {
1628 .su_group = {
1629 .cg_item = {
1630 .ci_namebuf = "usb_gadget",
1631 .ci_type = &gadgets_type,
1634 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1637 void unregister_gadget_item(struct config_item *item)
1639 struct gadget_info *gi = to_gadget_info(item);
1641 mutex_lock(&gi->lock);
1642 unregister_gadget(gi);
1643 mutex_unlock(&gi->lock);
1645 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1647 static int __init gadget_cfs_init(void)
1649 int ret;
1651 config_group_init(&gadget_subsys.su_group);
1653 ret = configfs_register_subsystem(&gadget_subsys);
1654 return ret;
1656 module_init(gadget_cfs_init);
1658 static void __exit gadget_cfs_exit(void)
1660 configfs_unregister_subsystem(&gadget_subsys);
1662 module_exit(gadget_cfs_exit);