usb: gadget: f_rndis: OS Descriptors configfs support
[deliverable/linux.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>
87213d38 5#include <linux/nls.h>
88af8bbe
SAS
6#include <linux/usb/composite.h>
7#include <linux/usb/gadget_configfs.h>
0009e99a 8#include "configfs.h"
da424314 9#include "u_f.h"
88af8bbe
SAS
10
11int check_user_usb_string(const char *name,
12 struct usb_gadget_strings *stringtab_dev)
13{
14 unsigned primary_lang;
15 unsigned sub_lang;
16 u16 num;
17 int ret;
18
19 ret = kstrtou16(name, 0, &num);
20 if (ret)
21 return ret;
22
23 primary_lang = num & 0x3ff;
24 sub_lang = num >> 10;
25
26 /* simple sanity check for valid langid */
27 switch (primary_lang) {
28 case 0:
29 case 0x62 ... 0xfe:
30 case 0x100 ... 0x3ff:
31 return -EINVAL;
32 }
33 if (!sub_lang)
34 return -EINVAL;
35
36 stringtab_dev->language = num;
37 return 0;
38}
39
40#define MAX_NAME_LEN 40
41#define MAX_USB_STRING_LANGS 2
42
43struct gadget_info {
44 struct config_group group;
45 struct config_group functions_group;
46 struct config_group configs_group;
47 struct config_group strings_group;
87213d38
AP
48 struct config_group os_desc_group;
49 struct config_group *default_groups[5];
88af8bbe
SAS
50
51 struct mutex lock;
52 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
53 struct list_head string_list;
54 struct list_head available_func;
55
56 const char *udc_name;
57#ifdef CONFIG_USB_OTG
58 struct usb_otg_descriptor otg;
59#endif
60 struct usb_composite_driver composite;
61 struct usb_composite_dev cdev;
87213d38
AP
62 bool use_os_desc;
63 char b_vendor_code;
64 char qw_sign[OS_STRING_QW_SIGN_LEN];
88af8bbe
SAS
65};
66
67struct config_usb_cfg {
68 struct config_group group;
69 struct config_group strings_group;
70 struct config_group *default_groups[2];
71 struct list_head string_list;
72 struct usb_configuration c;
73 struct list_head func_list;
74 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
75};
76
77struct gadget_strings {
78 struct usb_gadget_strings stringtab_dev;
79 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
80 char *manufacturer;
81 char *product;
82 char *serialnumber;
83
84 struct config_group group;
85 struct list_head list;
86};
87
87213d38
AP
88struct os_desc {
89 struct config_group group;
90};
91
88af8bbe
SAS
92struct gadget_config_name {
93 struct usb_gadget_strings stringtab_dev;
94 struct usb_string strings;
95 char *configuration;
96
97 struct config_group group;
98 struct list_head list;
99};
100
101static int usb_string_copy(const char *s, char **s_copy)
102{
103 int ret;
104 char *str;
105 char *copy = *s_copy;
106 ret = strlen(s);
107 if (ret > 126)
108 return -EOVERFLOW;
109
110 str = kstrdup(s, GFP_KERNEL);
111 if (!str)
112 return -ENOMEM;
113 if (str[ret - 1] == '\n')
114 str[ret - 1] = '\0';
115 kfree(copy);
116 *s_copy = str;
117 return 0;
118}
119
120CONFIGFS_ATTR_STRUCT(gadget_info);
121CONFIGFS_ATTR_STRUCT(config_usb_cfg);
122
123#define GI_DEVICE_DESC_ITEM_ATTR(name) \
124 static struct gadget_info_attribute gadget_cdev_desc_##name = \
125 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
126 gadget_dev_desc_##name##_show, \
127 gadget_dev_desc_##name##_store)
128
129#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
130 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
131 char *page) \
132{ \
133 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
134}
135
136#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
137 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
138 char *page) \
139{ \
140 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
141}
142
143
144#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
145 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
146 const char *page, size_t len) \
147{ \
148 u8 val; \
149 int ret; \
150 ret = kstrtou8(page, 0, &val); \
151 if (ret) \
152 return ret; \
153 gi->cdev.desc._name = val; \
154 return len; \
155}
156
157#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
158 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
159 const char *page, size_t len) \
160{ \
161 u16 val; \
162 int ret; \
163 ret = kstrtou16(page, 0, &val); \
164 if (ret) \
165 return ret; \
166 gi->cdev.desc._name = cpu_to_le16p(&val); \
167 return len; \
168}
169
170#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
171 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
172 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
173
174GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
175GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
176GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
177GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
178GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
179GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
180GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
181GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
182
183static ssize_t is_valid_bcd(u16 bcd_val)
184{
185 if ((bcd_val & 0xf) > 9)
186 return -EINVAL;
187 if (((bcd_val >> 4) & 0xf) > 9)
188 return -EINVAL;
189 if (((bcd_val >> 8) & 0xf) > 9)
190 return -EINVAL;
191 if (((bcd_val >> 12) & 0xf) > 9)
192 return -EINVAL;
193 return 0;
194}
195
196static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
197 const char *page, size_t len)
198{
199 u16 bcdDevice;
200 int ret;
201
202 ret = kstrtou16(page, 0, &bcdDevice);
203 if (ret)
204 return ret;
205 ret = is_valid_bcd(bcdDevice);
206 if (ret)
207 return ret;
208
209 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
210 return len;
211}
212
213static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
214 const char *page, size_t len)
215{
216 u16 bcdUSB;
217 int ret;
218
219 ret = kstrtou16(page, 0, &bcdUSB);
220 if (ret)
221 return ret;
222 ret = is_valid_bcd(bcdUSB);
223 if (ret)
224 return ret;
225
226 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
227 return len;
228}
229
230static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
231{
232 return sprintf(page, "%s\n", gi->udc_name ?: "");
233}
234
235static int unregister_gadget(struct gadget_info *gi)
236{
237 int ret;
238
239 if (!gi->udc_name)
240 return -ENODEV;
241
242 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
243 if (ret)
244 return ret;
245 kfree(gi->udc_name);
246 gi->udc_name = NULL;
247 return 0;
248}
249
250static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
251 const char *page, size_t len)
252{
253 char *name;
254 int ret;
255
256 name = kstrdup(page, GFP_KERNEL);
257 if (!name)
258 return -ENOMEM;
259 if (name[len - 1] == '\n')
260 name[len - 1] = '\0';
261
262 mutex_lock(&gi->lock);
263
264 if (!strlen(name)) {
265 ret = unregister_gadget(gi);
266 if (ret)
267 goto err;
268 } else {
269 if (gi->udc_name) {
270 ret = -EBUSY;
271 goto err;
272 }
273 ret = udc_attach_driver(name, &gi->composite.gadget_driver);
274 if (ret)
275 goto err;
276 gi->udc_name = name;
277 }
278 mutex_unlock(&gi->lock);
279 return len;
280err:
281 kfree(name);
282 mutex_unlock(&gi->lock);
283 return ret;
284}
285
286GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
287GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
288GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
289GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
290GI_DEVICE_DESC_ITEM_ATTR(idVendor);
291GI_DEVICE_DESC_ITEM_ATTR(idProduct);
292GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
293GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
294GI_DEVICE_DESC_ITEM_ATTR(UDC);
295
296static struct configfs_attribute *gadget_root_attrs[] = {
297 &gadget_cdev_desc_bDeviceClass.attr,
298 &gadget_cdev_desc_bDeviceSubClass.attr,
299 &gadget_cdev_desc_bDeviceProtocol.attr,
300 &gadget_cdev_desc_bMaxPacketSize0.attr,
301 &gadget_cdev_desc_idVendor.attr,
302 &gadget_cdev_desc_idProduct.attr,
303 &gadget_cdev_desc_bcdDevice.attr,
304 &gadget_cdev_desc_bcdUSB.attr,
305 &gadget_cdev_desc_UDC.attr,
306 NULL,
307};
308
309static inline struct gadget_info *to_gadget_info(struct config_item *item)
310{
311 return container_of(to_config_group(item), struct gadget_info, group);
312}
313
314static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
315{
316 return container_of(to_config_group(item), struct gadget_strings,
317 group);
318}
319
320static inline struct gadget_config_name *to_gadget_config_name(
321 struct config_item *item)
322{
323 return container_of(to_config_group(item), struct gadget_config_name,
324 group);
325}
326
327static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
328{
329 return container_of(to_config_group(item), struct config_usb_cfg,
330 group);
331}
332
333static inline struct usb_function_instance *to_usb_function_instance(
334 struct config_item *item)
335{
336 return container_of(to_config_group(item),
337 struct usb_function_instance, group);
338}
339
340static void gadget_info_attr_release(struct config_item *item)
341{
342 struct gadget_info *gi = to_gadget_info(item);
343
344 WARN_ON(!list_empty(&gi->cdev.configs));
345 WARN_ON(!list_empty(&gi->string_list));
346 WARN_ON(!list_empty(&gi->available_func));
347 kfree(gi->composite.gadget_driver.function);
348 kfree(gi);
349}
350
351CONFIGFS_ATTR_OPS(gadget_info);
352
353static struct configfs_item_operations gadget_root_item_ops = {
354 .release = gadget_info_attr_release,
355 .show_attribute = gadget_info_attr_show,
356 .store_attribute = gadget_info_attr_store,
357};
358
359static void gadget_config_attr_release(struct config_item *item)
360{
361 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
362
363 WARN_ON(!list_empty(&cfg->c.functions));
364 list_del(&cfg->c.list);
365 kfree(cfg->c.label);
366 kfree(cfg);
367}
368
369static int config_usb_cfg_link(
370 struct config_item *usb_cfg_ci,
371 struct config_item *usb_func_ci)
372{
373 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
374 struct usb_composite_dev *cdev = cfg->c.cdev;
375 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
376
377 struct config_group *group = to_config_group(usb_func_ci);
378 struct usb_function_instance *fi = container_of(group,
379 struct usb_function_instance, group);
380 struct usb_function_instance *a_fi;
381 struct usb_function *f;
382 int ret;
383
384 mutex_lock(&gi->lock);
385 /*
386 * Make sure this function is from within our _this_ gadget and not
387 * from another gadget or a random directory.
388 * Also a function instance can only be linked once.
389 */
390 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
391 if (a_fi == fi)
392 break;
393 }
394 if (a_fi != fi) {
395 ret = -EINVAL;
396 goto out;
397 }
398
399 list_for_each_entry(f, &cfg->func_list, list) {
400 if (f->fi == fi) {
401 ret = -EEXIST;
402 goto out;
403 }
404 }
405
406 f = usb_get_function(fi);
407 if (IS_ERR(f)) {
408 ret = PTR_ERR(f);
409 goto out;
410 }
411
412 /* stash the function until we bind it to the gadget */
413 list_add_tail(&f->list, &cfg->func_list);
414 ret = 0;
415out:
416 mutex_unlock(&gi->lock);
417 return ret;
418}
419
420static int config_usb_cfg_unlink(
421 struct config_item *usb_cfg_ci,
422 struct config_item *usb_func_ci)
423{
424 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
425 struct usb_composite_dev *cdev = cfg->c.cdev;
426 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
427
428 struct config_group *group = to_config_group(usb_func_ci);
429 struct usb_function_instance *fi = container_of(group,
430 struct usb_function_instance, group);
431 struct usb_function *f;
432
433 /*
434 * ideally I would like to forbid to unlink functions while a gadget is
435 * bound to an UDC. Since this isn't possible at the moment, we simply
436 * force an unbind, the function is available here and then we can
437 * remove the function.
438 */
439 mutex_lock(&gi->lock);
440 if (gi->udc_name)
441 unregister_gadget(gi);
442 WARN_ON(gi->udc_name);
443
444 list_for_each_entry(f, &cfg->func_list, list) {
445 if (f->fi == fi) {
446 list_del(&f->list);
447 usb_put_function(f);
448 mutex_unlock(&gi->lock);
449 return 0;
450 }
451 }
452 mutex_unlock(&gi->lock);
75bfe23a 453 WARN(1, "Unable to locate function to unbind\n");
88af8bbe
SAS
454 return 0;
455}
456
457CONFIGFS_ATTR_OPS(config_usb_cfg);
458
459static struct configfs_item_operations gadget_config_item_ops = {
460 .release = gadget_config_attr_release,
461 .show_attribute = config_usb_cfg_attr_show,
462 .store_attribute = config_usb_cfg_attr_store,
463 .allow_link = config_usb_cfg_link,
464 .drop_link = config_usb_cfg_unlink,
465};
466
467
468static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
469 char *page)
470{
471 return sprintf(page, "%u\n", cfg->c.MaxPower);
472}
473
474static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
475 const char *page, size_t len)
476{
477 u16 val;
478 int ret;
479 ret = kstrtou16(page, 0, &val);
480 if (ret)
481 return ret;
482 if (DIV_ROUND_UP(val, 8) > 0xff)
483 return -ERANGE;
484 cfg->c.MaxPower = val;
485 return len;
486}
487
488static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
489 char *page)
490{
491 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
492}
493
494static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
495 const char *page, size_t len)
496{
497 u8 val;
498 int ret;
499 ret = kstrtou8(page, 0, &val);
500 if (ret)
501 return ret;
502 if (!(val & USB_CONFIG_ATT_ONE))
503 return -EINVAL;
504 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
505 USB_CONFIG_ATT_WAKEUP))
506 return -EINVAL;
507 cfg->c.bmAttributes = val;
508 return len;
509}
510
511#define CFG_CONFIG_DESC_ITEM_ATTR(name) \
512 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
513 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
514 gadget_config_desc_##name##_show, \
515 gadget_config_desc_##name##_store)
516
517CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
518CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
519
520static struct configfs_attribute *gadget_config_attrs[] = {
521 &gadget_usb_cfg_MaxPower.attr,
522 &gadget_usb_cfg_bmAttributes.attr,
523 NULL,
524};
525
526static struct config_item_type gadget_config_type = {
527 .ct_item_ops = &gadget_config_item_ops,
528 .ct_attrs = gadget_config_attrs,
529 .ct_owner = THIS_MODULE,
530};
531
532static struct config_item_type gadget_root_type = {
533 .ct_item_ops = &gadget_root_item_ops,
534 .ct_attrs = gadget_root_attrs,
535 .ct_owner = THIS_MODULE,
536};
537
538static void composite_init_dev(struct usb_composite_dev *cdev)
539{
540 spin_lock_init(&cdev->lock);
541 INIT_LIST_HEAD(&cdev->configs);
542 INIT_LIST_HEAD(&cdev->gstrings);
543}
544
545static struct config_group *function_make(
546 struct config_group *group,
547 const char *name)
548{
549 struct gadget_info *gi;
550 struct usb_function_instance *fi;
551 char buf[MAX_NAME_LEN];
552 char *func_name;
553 char *instance_name;
554 int ret;
555
556 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
557 if (ret >= MAX_NAME_LEN)
558 return ERR_PTR(-ENAMETOOLONG);
559
560 func_name = buf;
561 instance_name = strchr(func_name, '.');
562 if (!instance_name) {
563 pr_err("Unable to locate . in FUNC.INSTANCE\n");
564 return ERR_PTR(-EINVAL);
565 }
566 *instance_name = '\0';
567 instance_name++;
568
569 fi = usb_get_function_instance(func_name);
570 if (IS_ERR(fi))
a3469411 571 return ERR_CAST(fi);
88af8bbe
SAS
572
573 ret = config_item_set_name(&fi->group.cg_item, name);
574 if (ret) {
575 usb_put_function_instance(fi);
576 return ERR_PTR(ret);
577 }
1933861d
AP
578 if (fi->set_inst_name) {
579 ret = fi->set_inst_name(fi, instance_name);
580 if (ret) {
581 usb_put_function_instance(fi);
582 return ERR_PTR(ret);
583 }
584 }
88af8bbe
SAS
585
586 gi = container_of(group, struct gadget_info, functions_group);
587
588 mutex_lock(&gi->lock);
589 list_add_tail(&fi->cfs_list, &gi->available_func);
590 mutex_unlock(&gi->lock);
591 return &fi->group;
592}
593
594static void function_drop(
595 struct config_group *group,
596 struct config_item *item)
597{
598 struct usb_function_instance *fi = to_usb_function_instance(item);
599 struct gadget_info *gi;
600
601 gi = container_of(group, struct gadget_info, functions_group);
602
603 mutex_lock(&gi->lock);
604 list_del(&fi->cfs_list);
605 mutex_unlock(&gi->lock);
606 config_item_put(item);
607}
608
609static struct configfs_group_operations functions_ops = {
610 .make_group = &function_make,
611 .drop_item = &function_drop,
612};
613
614static struct config_item_type functions_type = {
615 .ct_group_ops = &functions_ops,
616 .ct_owner = THIS_MODULE,
617};
618
619CONFIGFS_ATTR_STRUCT(gadget_config_name);
620GS_STRINGS_RW(gadget_config_name, configuration);
621
622static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
623 &gadget_config_name_configuration.attr,
624 NULL,
625};
626
627static void gadget_config_name_attr_release(struct config_item *item)
628{
629 struct gadget_config_name *cn = to_gadget_config_name(item);
630
631 kfree(cn->configuration);
632
633 list_del(&cn->list);
634 kfree(cn);
635}
636
637USB_CONFIG_STRING_RW_OPS(gadget_config_name);
638USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
639
640static struct config_group *config_desc_make(
641 struct config_group *group,
642 const char *name)
643{
644 struct gadget_info *gi;
645 struct config_usb_cfg *cfg;
646 char buf[MAX_NAME_LEN];
647 char *num_str;
648 u8 num;
649 int ret;
650
651 gi = container_of(group, struct gadget_info, configs_group);
652 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
653 if (ret >= MAX_NAME_LEN)
654 return ERR_PTR(-ENAMETOOLONG);
655
656 num_str = strchr(buf, '.');
657 if (!num_str) {
658 pr_err("Unable to locate . in name.bConfigurationValue\n");
659 return ERR_PTR(-EINVAL);
660 }
661
662 *num_str = '\0';
663 num_str++;
664
665 if (!strlen(buf))
666 return ERR_PTR(-EINVAL);
667
668 ret = kstrtou8(num_str, 0, &num);
669 if (ret)
670 return ERR_PTR(ret);
671
672 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
673 if (!cfg)
674 return ERR_PTR(-ENOMEM);
675 cfg->c.label = kstrdup(buf, GFP_KERNEL);
676 if (!cfg->c.label) {
677 ret = -ENOMEM;
678 goto err;
679 }
680 cfg->c.bConfigurationValue = num;
681 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
682 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
683 INIT_LIST_HEAD(&cfg->string_list);
684 INIT_LIST_HEAD(&cfg->func_list);
685
686 cfg->group.default_groups = cfg->default_groups;
687 cfg->default_groups[0] = &cfg->strings_group;
688
689 config_group_init_type_name(&cfg->group, name,
690 &gadget_config_type);
691 config_group_init_type_name(&cfg->strings_group, "strings",
692 &gadget_config_name_strings_type);
693
694 ret = usb_add_config_only(&gi->cdev, &cfg->c);
695 if (ret)
696 goto err;
697
698 return &cfg->group;
699err:
700 kfree(cfg->c.label);
701 kfree(cfg);
702 return ERR_PTR(ret);
703}
704
705static void config_desc_drop(
706 struct config_group *group,
707 struct config_item *item)
708{
709 config_item_put(item);
710}
711
712static struct configfs_group_operations config_desc_ops = {
713 .make_group = &config_desc_make,
714 .drop_item = &config_desc_drop,
715};
716
717static struct config_item_type config_desc_type = {
718 .ct_group_ops = &config_desc_ops,
719 .ct_owner = THIS_MODULE,
720};
721
722CONFIGFS_ATTR_STRUCT(gadget_strings);
723GS_STRINGS_RW(gadget_strings, manufacturer);
724GS_STRINGS_RW(gadget_strings, product);
725GS_STRINGS_RW(gadget_strings, serialnumber);
726
727static struct configfs_attribute *gadget_strings_langid_attrs[] = {
728 &gadget_strings_manufacturer.attr,
729 &gadget_strings_product.attr,
730 &gadget_strings_serialnumber.attr,
731 NULL,
732};
733
734static void gadget_strings_attr_release(struct config_item *item)
735{
736 struct gadget_strings *gs = to_gadget_strings(item);
737
738 kfree(gs->manufacturer);
739 kfree(gs->product);
740 kfree(gs->serialnumber);
741
742 list_del(&gs->list);
743 kfree(gs);
744}
745
746USB_CONFIG_STRING_RW_OPS(gadget_strings);
747USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
748
87213d38
AP
749static inline struct os_desc *to_os_desc(struct config_item *item)
750{
751 return container_of(to_config_group(item), struct os_desc, group);
752}
753
754CONFIGFS_ATTR_STRUCT(os_desc);
755CONFIGFS_ATTR_OPS(os_desc);
756
757static ssize_t os_desc_use_show(struct os_desc *os_desc, char *page)
758{
759 struct gadget_info *gi;
760
761 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
762
763 return sprintf(page, "%d", gi->use_os_desc);
764}
765
766static ssize_t os_desc_use_store(struct os_desc *os_desc, const char *page,
767 size_t len)
768{
769 struct gadget_info *gi;
770 int ret;
771 bool use;
772
773 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
774
775 mutex_lock(&gi->lock);
776 ret = strtobool(page, &use);
777 if (!ret) {
778 gi->use_os_desc = use;
779 ret = len;
780 }
781 mutex_unlock(&gi->lock);
782
783 return ret;
784}
785
786static struct os_desc_attribute os_desc_use =
787 __CONFIGFS_ATTR(use, S_IRUGO | S_IWUSR,
788 os_desc_use_show,
789 os_desc_use_store);
790
791static ssize_t os_desc_b_vendor_code_show(struct os_desc *os_desc, char *page)
792{
793 struct gadget_info *gi;
794
795 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
796
797 return sprintf(page, "%d", gi->b_vendor_code);
798}
799
800static ssize_t os_desc_b_vendor_code_store(struct os_desc *os_desc,
801 const char *page, size_t len)
802{
803 struct gadget_info *gi;
804 int ret;
805 u8 b_vendor_code;
806
807 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
808
809 mutex_lock(&gi->lock);
810 ret = kstrtou8(page, 0, &b_vendor_code);
811 if (!ret) {
812 gi->b_vendor_code = b_vendor_code;
813 ret = len;
814 }
815 mutex_unlock(&gi->lock);
816
817 return ret;
818}
819
820static struct os_desc_attribute os_desc_b_vendor_code =
821 __CONFIGFS_ATTR(b_vendor_code, S_IRUGO | S_IWUSR,
822 os_desc_b_vendor_code_show,
823 os_desc_b_vendor_code_store);
824
825static ssize_t os_desc_qw_sign_show(struct os_desc *os_desc, char *page)
826{
827 struct gadget_info *gi;
828
829 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
830
831 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
832
833 return OS_STRING_QW_SIGN_LEN;
834}
835
836static ssize_t os_desc_qw_sign_store(struct os_desc *os_desc, const char *page,
837 size_t len)
838{
839 struct gadget_info *gi;
840 int res, l;
841
842 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
843 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
844 if (page[l - 1] == '\n')
845 --l;
846
847 mutex_lock(&gi->lock);
848 res = utf8s_to_utf16s(page, l,
849 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
850 OS_STRING_QW_SIGN_LEN);
851 if (res > 0)
852 res = len;
853 mutex_unlock(&gi->lock);
854
855 return res;
856}
857
858static struct os_desc_attribute os_desc_qw_sign =
859 __CONFIGFS_ATTR(qw_sign, S_IRUGO | S_IWUSR,
860 os_desc_qw_sign_show,
861 os_desc_qw_sign_store);
862
863static struct configfs_attribute *os_desc_attrs[] = {
864 &os_desc_use.attr,
865 &os_desc_b_vendor_code.attr,
866 &os_desc_qw_sign.attr,
867 NULL,
868};
869
870static void os_desc_attr_release(struct config_item *item)
871{
872 struct os_desc *os_desc = to_os_desc(item);
873 kfree(os_desc);
874}
875
da424314
AP
876static int os_desc_link(struct config_item *os_desc_ci,
877 struct config_item *usb_cfg_ci)
878{
879 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
880 struct gadget_info, os_desc_group);
881 struct usb_composite_dev *cdev = &gi->cdev;
882 struct config_usb_cfg *c_target =
883 container_of(to_config_group(usb_cfg_ci),
884 struct config_usb_cfg, group);
885 struct usb_configuration *c;
886 int ret;
887
888 mutex_lock(&gi->lock);
889 list_for_each_entry(c, &cdev->configs, list) {
890 if (c == &c_target->c)
891 break;
892 }
893 if (c != &c_target->c) {
894 ret = -EINVAL;
895 goto out;
896 }
897
898 if (cdev->os_desc_config) {
899 ret = -EBUSY;
900 goto out;
901 }
902
903 cdev->os_desc_config = &c_target->c;
904 ret = 0;
905
906out:
907 mutex_unlock(&gi->lock);
908 return ret;
909}
910
911static int os_desc_unlink(struct config_item *os_desc_ci,
912 struct config_item *usb_cfg_ci)
913{
914 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
915 struct gadget_info, os_desc_group);
916 struct usb_composite_dev *cdev = &gi->cdev;
917
918 mutex_lock(&gi->lock);
919 if (gi->udc_name)
920 unregister_gadget(gi);
921 cdev->os_desc_config = NULL;
922 WARN_ON(gi->udc_name);
923 mutex_unlock(&gi->lock);
924 return 0;
925}
926
87213d38
AP
927static struct configfs_item_operations os_desc_ops = {
928 .release = os_desc_attr_release,
929 .show_attribute = os_desc_attr_show,
930 .store_attribute = os_desc_attr_store,
da424314
AP
931 .allow_link = os_desc_link,
932 .drop_link = os_desc_unlink,
87213d38
AP
933};
934
935static struct config_item_type os_desc_type = {
936 .ct_item_ops = &os_desc_ops,
937 .ct_attrs = os_desc_attrs,
938 .ct_owner = THIS_MODULE,
939};
940
da424314
AP
941CONFIGFS_ATTR_STRUCT(usb_os_desc);
942CONFIGFS_ATTR_OPS(usb_os_desc);
943
944static struct configfs_item_operations interf_item_ops = {
945 .show_attribute = usb_os_desc_attr_show,
946 .store_attribute = usb_os_desc_attr_store,
947};
948
949static ssize_t rndis_grp_compatible_id_show(struct usb_os_desc *desc,
950 char *page)
951{
952 memcpy(page, desc->ext_compat_id, 8);
953 return 8;
954}
955
956static ssize_t rndis_grp_compatible_id_store(struct usb_os_desc *desc,
957 const char *page, size_t len)
958{
959 int l;
960
961 l = min_t(int, 8, len);
962 if (page[l - 1] == '\n')
963 --l;
964 if (desc->opts_mutex)
965 mutex_lock(desc->opts_mutex);
966 memcpy(desc->ext_compat_id, page, l);
967 desc->ext_compat_id[l] = '\0';
968
969 if (desc->opts_mutex)
970 mutex_unlock(desc->opts_mutex);
971
972 return len;
973}
974
975static struct usb_os_desc_attribute rndis_grp_attr_compatible_id =
976 __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR,
977 rndis_grp_compatible_id_show,
978 rndis_grp_compatible_id_store);
979
980static ssize_t rndis_grp_sub_compatible_id_show(struct usb_os_desc *desc,
981 char *page)
982{
983 memcpy(page, desc->ext_compat_id + 8, 8);
984 return 8;
985}
986
987static ssize_t rndis_grp_sub_compatible_id_store(struct usb_os_desc *desc,
988 const char *page, size_t len)
989{
990 int l;
991
992 l = min_t(int, 8, len);
993 if (page[l - 1] == '\n')
994 --l;
995 if (desc->opts_mutex)
996 mutex_lock(desc->opts_mutex);
997 memcpy(desc->ext_compat_id + 8, page, l);
998 desc->ext_compat_id[l + 8] = '\0';
999
1000 if (desc->opts_mutex)
1001 mutex_unlock(desc->opts_mutex);
1002
1003 return len;
1004}
1005
1006static struct usb_os_desc_attribute rndis_grp_attr_sub_compatible_id =
1007 __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR,
1008 rndis_grp_sub_compatible_id_show,
1009 rndis_grp_sub_compatible_id_store);
1010
1011static struct configfs_attribute *interf_grp_attrs[] = {
1012 &rndis_grp_attr_compatible_id.attr,
1013 &rndis_grp_attr_sub_compatible_id.attr,
1014 NULL
1015};
1016
1017int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1018 int n_interf,
1019 struct usb_os_desc **desc,
1020 struct module *owner)
1021{
1022 struct config_group **f_default_groups, *os_desc_group,
1023 **interface_groups;
1024 struct config_item_type *os_desc_type, *interface_type;
1025
1026 vla_group(data_chunk);
1027 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1028 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1029 vla_item(data_chunk, struct config_group *, interface_groups,
1030 n_interf + 1);
1031 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1032 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1033
1034 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1035 if (!vlabuf)
1036 return -ENOMEM;
1037
1038 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1039 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1040 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1041 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1042 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1043
1044 parent->default_groups = f_default_groups;
1045 os_desc_type->ct_owner = owner;
1046 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1047 f_default_groups[0] = os_desc_group;
1048
1049 os_desc_group->default_groups = interface_groups;
1050 interface_type->ct_item_ops = &interf_item_ops;
1051 interface_type->ct_attrs = interf_grp_attrs;
1052 interface_type->ct_owner = owner;
1053
1054 while (n_interf--) {
1055 struct usb_os_desc *d;
1056
1057 d = desc[n_interf];
1058 config_group_init_type_name(&d->group, "", interface_type);
1059 config_item_set_name(&d->group.cg_item, "interface.%d",
1060 n_interf);
1061 interface_groups[n_interf] = &d->group;
1062 }
1063
1064 return 0;
1065}
1066EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1067
88af8bbe
SAS
1068static int configfs_do_nothing(struct usb_composite_dev *cdev)
1069{
75bfe23a 1070 WARN_ON(1);
88af8bbe
SAS
1071 return -EINVAL;
1072}
1073
1074int composite_dev_prepare(struct usb_composite_driver *composite,
1075 struct usb_composite_dev *dev);
1076
da424314
AP
1077int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1078 struct usb_ep *ep0);
1079
88af8bbe
SAS
1080static void purge_configs_funcs(struct gadget_info *gi)
1081{
1082 struct usb_configuration *c;
1083
1084 list_for_each_entry(c, &gi->cdev.configs, list) {
1085 struct usb_function *f, *tmp;
1086 struct config_usb_cfg *cfg;
1087
1088 cfg = container_of(c, struct config_usb_cfg, c);
1089
1090 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1091
1092 list_move_tail(&f->list, &cfg->func_list);
1093 if (f->unbind) {
1094 dev_err(&gi->cdev.gadget->dev, "unbind function"
1095 " '%s'/%p\n", f->name, f);
1096 f->unbind(c, f);
1097 }
1098 }
1099 c->next_interface_id = 0;
1100 c->superspeed = 0;
1101 c->highspeed = 0;
1102 c->fullspeed = 0;
1103 }
1104}
1105
1106static int configfs_composite_bind(struct usb_gadget *gadget,
1107 struct usb_gadget_driver *gdriver)
1108{
1109 struct usb_composite_driver *composite = to_cdriver(gdriver);
1110 struct gadget_info *gi = container_of(composite,
1111 struct gadget_info, composite);
1112 struct usb_composite_dev *cdev = &gi->cdev;
1113 struct usb_configuration *c;
1114 struct usb_string *s;
1115 unsigned i;
1116 int ret;
1117
1118 /* the gi->lock is hold by the caller */
1119 cdev->gadget = gadget;
1120 set_gadget_data(gadget, cdev);
1121 ret = composite_dev_prepare(composite, cdev);
1122 if (ret)
1123 return ret;
1124 /* and now the gadget bind */
1125 ret = -EINVAL;
1126
1127 if (list_empty(&gi->cdev.configs)) {
4d9f872c 1128 pr_err("Need at least one configuration in %s.\n",
88af8bbe
SAS
1129 gi->composite.name);
1130 goto err_comp_cleanup;
1131 }
1132
1133
1134 list_for_each_entry(c, &gi->cdev.configs, list) {
1135 struct config_usb_cfg *cfg;
1136
1137 cfg = container_of(c, struct config_usb_cfg, c);
1138 if (list_empty(&cfg->func_list)) {
4d9f872c 1139 pr_err("Config %s/%d of %s needs at least one function.\n",
88af8bbe
SAS
1140 c->label, c->bConfigurationValue,
1141 gi->composite.name);
1142 goto err_comp_cleanup;
1143 }
1144 }
1145
1146 /* init all strings */
1147 if (!list_empty(&gi->string_list)) {
1148 struct gadget_strings *gs;
1149
1150 i = 0;
1151 list_for_each_entry(gs, &gi->string_list, list) {
1152
1153 gi->gstrings[i] = &gs->stringtab_dev;
1154 gs->stringtab_dev.strings = gs->strings;
1155 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1156 gs->manufacturer;
1157 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1158 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1159 i++;
1160 }
1161 gi->gstrings[i] = NULL;
1162 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1163 USB_GADGET_FIRST_AVAIL_IDX);
fea77077
WY
1164 if (IS_ERR(s)) {
1165 ret = PTR_ERR(s);
88af8bbe 1166 goto err_comp_cleanup;
fea77077 1167 }
88af8bbe
SAS
1168
1169 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1170 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1171 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1172 }
1173
87213d38
AP
1174 if (gi->use_os_desc) {
1175 cdev->use_os_string = true;
1176 cdev->b_vendor_code = gi->b_vendor_code;
1177 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1178 }
1179
88af8bbe
SAS
1180 /* Go through all configs, attach all functions */
1181 list_for_each_entry(c, &gi->cdev.configs, list) {
1182 struct config_usb_cfg *cfg;
1183 struct usb_function *f;
1184 struct usb_function *tmp;
1185 struct gadget_config_name *cn;
1186
1187 cfg = container_of(c, struct config_usb_cfg, c);
1188 if (!list_empty(&cfg->string_list)) {
1189 i = 0;
1190 list_for_each_entry(cn, &cfg->string_list, list) {
1191 cfg->gstrings[i] = &cn->stringtab_dev;
1192 cn->stringtab_dev.strings = &cn->strings;
1193 cn->strings.s = cn->configuration;
1194 i++;
1195 }
1196 cfg->gstrings[i] = NULL;
1197 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
fea77077
WY
1198 if (IS_ERR(s)) {
1199 ret = PTR_ERR(s);
88af8bbe 1200 goto err_comp_cleanup;
fea77077 1201 }
88af8bbe
SAS
1202 c->iConfiguration = s[0].id;
1203 }
1204
1205 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1206 list_del(&f->list);
1207 ret = usb_add_function(c, f);
5a68e9b5
AP
1208 if (ret) {
1209 list_add(&f->list, &cfg->func_list);
88af8bbe 1210 goto err_purge_funcs;
5a68e9b5 1211 }
88af8bbe
SAS
1212 }
1213 usb_ep_autoconfig_reset(cdev->gadget);
1214 }
da424314
AP
1215 if (cdev->use_os_string) {
1216 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1217 if (ret)
1218 goto err_purge_funcs;
1219 }
1220
88af8bbe
SAS
1221 usb_ep_autoconfig_reset(cdev->gadget);
1222 return 0;
1223
1224err_purge_funcs:
1225 purge_configs_funcs(gi);
1226err_comp_cleanup:
1227 composite_dev_cleanup(cdev);
1228 return ret;
1229}
1230
1231static void configfs_composite_unbind(struct usb_gadget *gadget)
1232{
1233 struct usb_composite_dev *cdev;
1234 struct gadget_info *gi;
1235
1236 /* the gi->lock is hold by the caller */
1237
1238 cdev = get_gadget_data(gadget);
1239 gi = container_of(cdev, struct gadget_info, cdev);
1240
1241 purge_configs_funcs(gi);
1242 composite_dev_cleanup(cdev);
1243 usb_ep_autoconfig_reset(cdev->gadget);
1244 cdev->gadget = NULL;
1245 set_gadget_data(gadget, NULL);
1246}
1247
1248static const struct usb_gadget_driver configfs_driver_template = {
1249 .bind = configfs_composite_bind,
1250 .unbind = configfs_composite_unbind,
1251
1252 .setup = composite_setup,
1253 .disconnect = composite_disconnect,
1254
1255 .max_speed = USB_SPEED_SUPER,
1256 .driver = {
1257 .owner = THIS_MODULE,
1258 .name = "configfs-gadget",
1259 },
1260};
1261
1262static struct config_group *gadgets_make(
1263 struct config_group *group,
1264 const char *name)
1265{
1266 struct gadget_info *gi;
1267
1268 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1269 if (!gi)
1270 return ERR_PTR(-ENOMEM);
1271
1272 gi->group.default_groups = gi->default_groups;
1273 gi->group.default_groups[0] = &gi->functions_group;
1274 gi->group.default_groups[1] = &gi->configs_group;
1275 gi->group.default_groups[2] = &gi->strings_group;
87213d38 1276 gi->group.default_groups[3] = &gi->os_desc_group;
88af8bbe
SAS
1277
1278 config_group_init_type_name(&gi->functions_group, "functions",
1279 &functions_type);
1280 config_group_init_type_name(&gi->configs_group, "configs",
1281 &config_desc_type);
1282 config_group_init_type_name(&gi->strings_group, "strings",
1283 &gadget_strings_strings_type);
87213d38
AP
1284 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1285 &os_desc_type);
88af8bbe
SAS
1286
1287 gi->composite.bind = configfs_do_nothing;
1288 gi->composite.unbind = configfs_do_nothing;
1289 gi->composite.suspend = NULL;
1290 gi->composite.resume = NULL;
1291 gi->composite.max_speed = USB_SPEED_SUPER;
1292
1293 mutex_init(&gi->lock);
1294 INIT_LIST_HEAD(&gi->string_list);
1295 INIT_LIST_HEAD(&gi->available_func);
1296
1297 composite_init_dev(&gi->cdev);
1298 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1299 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1300 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1301
1302 gi->composite.gadget_driver = configfs_driver_template;
1303
1304 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1305 gi->composite.name = gi->composite.gadget_driver.function;
1306
1307 if (!gi->composite.gadget_driver.function)
1308 goto err;
1309
1310#ifdef CONFIG_USB_OTG
1311 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
1312 gi->otg.bDescriptorType = USB_DT_OTG;
1313 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
1314#endif
1315
1316 config_group_init_type_name(&gi->group, name,
1317 &gadget_root_type);
1318 return &gi->group;
1319err:
1320 kfree(gi);
1321 return ERR_PTR(-ENOMEM);
1322}
1323
1324static void gadgets_drop(struct config_group *group, struct config_item *item)
1325{
1326 config_item_put(item);
1327}
1328
1329static struct configfs_group_operations gadgets_ops = {
1330 .make_group = &gadgets_make,
1331 .drop_item = &gadgets_drop,
1332};
1333
1334static struct config_item_type gadgets_type = {
1335 .ct_group_ops = &gadgets_ops,
1336 .ct_owner = THIS_MODULE,
1337};
1338
1339static struct configfs_subsystem gadget_subsys = {
1340 .su_group = {
1341 .cg_item = {
1342 .ci_namebuf = "usb_gadget",
1343 .ci_type = &gadgets_type,
1344 },
1345 },
1346 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1347};
1348
092a4bd0
AP
1349void unregister_gadget_item(struct config_item *item)
1350{
1351 struct gadget_info *gi = to_gadget_info(item);
1352
1353 unregister_gadget(gi);
1354}
0700faaf 1355EXPORT_SYMBOL_GPL(unregister_gadget_item);
092a4bd0 1356
88af8bbe
SAS
1357static int __init gadget_cfs_init(void)
1358{
1359 int ret;
1360
1361 config_group_init(&gadget_subsys.su_group);
1362
1363 ret = configfs_register_subsystem(&gadget_subsys);
1364 return ret;
1365}
1366module_init(gadget_cfs_init);
1367
1368static void __exit gadget_cfs_exit(void)
1369{
1370 configfs_unregister_subsystem(&gadget_subsys);
1371}
1372module_exit(gadget_cfs_exit);
This page took 0.141157 seconds and 5 git commands to generate.