blob: 97142146eead170243c131a7e9ad0356dac63444 [file] [log] [blame]
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001#include <linux/configfs.h>
2#include <linux/module.h>
3#include <linux/slab.h>
4#include <linux/device.h>
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02005#include <linux/nls.h>
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01006#include <linux/usb/composite.h>
7#include <linux/usb/gadget_configfs.h>
Rashika Kheria0009e992013-12-19 15:37:37 +05308#include "configfs.h"
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02009#include "u_f.h"
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +020010#include "u_os_desc.h"
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010011
12int check_user_usb_string(const char *name,
13 struct usb_gadget_strings *stringtab_dev)
14{
15 unsigned primary_lang;
16 unsigned sub_lang;
17 u16 num;
18 int ret;
19
20 ret = kstrtou16(name, 0, &num);
21 if (ret)
22 return ret;
23
24 primary_lang = num & 0x3ff;
25 sub_lang = num >> 10;
26
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;
33 }
34 if (!sub_lang)
35 return -EINVAL;
36
37 stringtab_dev->language = num;
38 return 0;
39}
40
41#define MAX_NAME_LEN 40
42#define MAX_USB_STRING_LANGS 2
43
44struct 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;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020049 struct config_group os_desc_group;
50 struct config_group *default_groups[5];
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010051
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;
56
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;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020063 bool use_os_desc;
64 char b_vendor_code;
65 char qw_sign[OS_STRING_QW_SIGN_LEN];
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010066};
67
68struct 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];
76};
77
78struct 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;
84
85 struct config_group group;
86 struct list_head list;
87};
88
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020089struct os_desc {
90 struct config_group group;
91};
92
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010093struct gadget_config_name {
94 struct usb_gadget_strings stringtab_dev;
95 struct usb_string strings;
96 char *configuration;
97
98 struct config_group group;
99 struct list_head list;
100};
101
102static int usb_string_copy(const char *s, char **s_copy)
103{
104 int ret;
105 char *str;
106 char *copy = *s_copy;
107 ret = strlen(s);
108 if (ret > 126)
109 return -EOVERFLOW;
110
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;
119}
120
121CONFIGFS_ATTR_STRUCT(gadget_info);
122CONFIGFS_ATTR_STRUCT(config_usb_cfg);
123
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)
129
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) \
133{ \
134 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
135}
136
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) \
140{ \
141 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
142}
143
144
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) \
148{ \
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; \
156}
157
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) \
161{ \
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; \
169}
170
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)
174
175GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
176GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
177GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
178GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
179GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
180GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
181GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
182GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
183
184static ssize_t is_valid_bcd(u16 bcd_val)
185{
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;
195}
196
197static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
198 const char *page, size_t len)
199{
200 u16 bcdDevice;
201 int ret;
202
203 ret = kstrtou16(page, 0, &bcdDevice);
204 if (ret)
205 return ret;
206 ret = is_valid_bcd(bcdDevice);
207 if (ret)
208 return ret;
209
210 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
211 return len;
212}
213
214static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
215 const char *page, size_t len)
216{
217 u16 bcdUSB;
218 int ret;
219
220 ret = kstrtou16(page, 0, &bcdUSB);
221 if (ret)
222 return ret;
223 ret = is_valid_bcd(bcdUSB);
224 if (ret)
225 return ret;
226
227 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
228 return len;
229}
230
231static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
232{
233 return sprintf(page, "%s\n", gi->udc_name ?: "");
234}
235
236static int unregister_gadget(struct gadget_info *gi)
237{
238 int ret;
239
240 if (!gi->udc_name)
241 return -ENODEV;
242
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;
249}
250
251static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
252 const char *page, size_t len)
253{
254 char *name;
255 int ret;
256
257 name = kstrdup(page, GFP_KERNEL);
258 if (!name)
259 return -ENOMEM;
260 if (name[len - 1] == '\n')
261 name[len - 1] = '\0';
262
263 mutex_lock(&gi->lock);
264
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;
273 }
274 ret = udc_attach_driver(name, &gi->composite.gadget_driver);
275 if (ret)
276 goto err;
277 gi->udc_name = name;
278 }
279 mutex_unlock(&gi->lock);
280 return len;
281err:
282 kfree(name);
283 mutex_unlock(&gi->lock);
284 return ret;
285}
286
287GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
288GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
289GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
290GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
291GI_DEVICE_DESC_ITEM_ATTR(idVendor);
292GI_DEVICE_DESC_ITEM_ATTR(idProduct);
293GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
294GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
295GI_DEVICE_DESC_ITEM_ATTR(UDC);
296
297static 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,
308};
309
310static inline struct gadget_info *to_gadget_info(struct config_item *item)
311{
312 return container_of(to_config_group(item), struct gadget_info, group);
313}
314
315static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
316{
317 return container_of(to_config_group(item), struct gadget_strings,
318 group);
319}
320
321static inline struct gadget_config_name *to_gadget_config_name(
322 struct config_item *item)
323{
324 return container_of(to_config_group(item), struct gadget_config_name,
325 group);
326}
327
328static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
329{
330 return container_of(to_config_group(item), struct config_usb_cfg,
331 group);
332}
333
334static inline struct usb_function_instance *to_usb_function_instance(
335 struct config_item *item)
336{
337 return container_of(to_config_group(item),
338 struct usb_function_instance, group);
339}
340
341static void gadget_info_attr_release(struct config_item *item)
342{
343 struct gadget_info *gi = to_gadget_info(item);
344
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);
350}
351
352CONFIGFS_ATTR_OPS(gadget_info);
353
354static 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,
358};
359
360static void gadget_config_attr_release(struct config_item *item)
361{
362 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
363
364 WARN_ON(!list_empty(&cfg->c.functions));
365 list_del(&cfg->c.list);
366 kfree(cfg->c.label);
367 kfree(cfg);
368}
369
370static int config_usb_cfg_link(
371 struct config_item *usb_cfg_ci,
372 struct config_item *usb_func_ci)
373{
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);
377
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;
384
385 mutex_lock(&gi->lock);
386 /*
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.
390 */
391 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
392 if (a_fi == fi)
393 break;
394 }
395 if (a_fi != fi) {
396 ret = -EINVAL;
397 goto out;
398 }
399
400 list_for_each_entry(f, &cfg->func_list, list) {
401 if (f->fi == fi) {
402 ret = -EEXIST;
403 goto out;
404 }
405 }
406
407 f = usb_get_function(fi);
408 if (IS_ERR(f)) {
409 ret = PTR_ERR(f);
410 goto out;
411 }
412
413 /* stash the function until we bind it to the gadget */
414 list_add_tail(&f->list, &cfg->func_list);
415 ret = 0;
416out:
417 mutex_unlock(&gi->lock);
418 return ret;
419}
420
421static int config_usb_cfg_unlink(
422 struct config_item *usb_cfg_ci,
423 struct config_item *usb_func_ci)
424{
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);
428
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;
433
434 /*
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.
439 */
440 mutex_lock(&gi->lock);
441 if (gi->udc_name)
442 unregister_gadget(gi);
443 WARN_ON(gi->udc_name);
444
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;
451 }
452 }
453 mutex_unlock(&gi->lock);
David Rientjes75bfe232013-04-07 14:11:47 -0700454 WARN(1, "Unable to locate function to unbind\n");
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100455 return 0;
456}
457
458CONFIGFS_ATTR_OPS(config_usb_cfg);
459
460static 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,
466};
467
468
469static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
470 char *page)
471{
472 return sprintf(page, "%u\n", cfg->c.MaxPower);
473}
474
475static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
476 const char *page, size_t len)
477{
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;
487}
488
489static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
490 char *page)
491{
492 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
493}
494
495static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
496 const char *page, size_t len)
497{
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;
510}
511
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)
517
518CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
519CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
520
521static struct configfs_attribute *gadget_config_attrs[] = {
522 &gadget_usb_cfg_MaxPower.attr,
523 &gadget_usb_cfg_bmAttributes.attr,
524 NULL,
525};
526
527static 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,
531};
532
533static 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,
537};
538
539static void composite_init_dev(struct usb_composite_dev *cdev)
540{
541 spin_lock_init(&cdev->lock);
542 INIT_LIST_HEAD(&cdev->configs);
543 INIT_LIST_HEAD(&cdev->gstrings);
544}
545
546static struct config_group *function_make(
547 struct config_group *group,
548 const char *name)
549{
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;
556
557 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
558 if (ret >= MAX_NAME_LEN)
559 return ERR_PTR(-ENAMETOOLONG);
560
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);
566 }
567 *instance_name = '\0';
568 instance_name++;
569
570 fi = usb_get_function_instance(func_name);
571 if (IS_ERR(fi))
Duan Jionga3469412013-09-26 15:55:25 +0800572 return ERR_CAST(fi);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100573
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);
578 }
Andrzej Pietrasiewicz19338612013-12-03 15:15:21 +0100579 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);
584 }
585 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100586
587 gi = container_of(group, struct gadget_info, functions_group);
588
589 mutex_lock(&gi->lock);
590 list_add_tail(&fi->cfs_list, &gi->available_func);
591 mutex_unlock(&gi->lock);
592 return &fi->group;
593}
594
595static void function_drop(
596 struct config_group *group,
597 struct config_item *item)
598{
599 struct usb_function_instance *fi = to_usb_function_instance(item);
600 struct gadget_info *gi;
601
602 gi = container_of(group, struct gadget_info, functions_group);
603
604 mutex_lock(&gi->lock);
605 list_del(&fi->cfs_list);
606 mutex_unlock(&gi->lock);
607 config_item_put(item);
608}
609
610static struct configfs_group_operations functions_ops = {
611 .make_group = &function_make,
612 .drop_item = &function_drop,
613};
614
615static struct config_item_type functions_type = {
616 .ct_group_ops = &functions_ops,
617 .ct_owner = THIS_MODULE,
618};
619
620CONFIGFS_ATTR_STRUCT(gadget_config_name);
621GS_STRINGS_RW(gadget_config_name, configuration);
622
623static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
624 &gadget_config_name_configuration.attr,
625 NULL,
626};
627
628static void gadget_config_name_attr_release(struct config_item *item)
629{
630 struct gadget_config_name *cn = to_gadget_config_name(item);
631
632 kfree(cn->configuration);
633
634 list_del(&cn->list);
635 kfree(cn);
636}
637
638USB_CONFIG_STRING_RW_OPS(gadget_config_name);
639USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
640
641static struct config_group *config_desc_make(
642 struct config_group *group,
643 const char *name)
644{
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;
651
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);
656
657 num_str = strchr(buf, '.');
658 if (!num_str) {
659 pr_err("Unable to locate . in name.bConfigurationValue\n");
660 return ERR_PTR(-EINVAL);
661 }
662
663 *num_str = '\0';
664 num_str++;
665
666 if (!strlen(buf))
667 return ERR_PTR(-EINVAL);
668
669 ret = kstrtou8(num_str, 0, &num);
670 if (ret)
671 return ERR_PTR(ret);
672
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;
680 }
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);
686
687 cfg->group.default_groups = cfg->default_groups;
688 cfg->default_groups[0] = &cfg->strings_group;
689
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);
694
695 ret = usb_add_config_only(&gi->cdev, &cfg->c);
696 if (ret)
697 goto err;
698
699 return &cfg->group;
700err:
701 kfree(cfg->c.label);
702 kfree(cfg);
703 return ERR_PTR(ret);
704}
705
706static void config_desc_drop(
707 struct config_group *group,
708 struct config_item *item)
709{
710 config_item_put(item);
711}
712
713static struct configfs_group_operations config_desc_ops = {
714 .make_group = &config_desc_make,
715 .drop_item = &config_desc_drop,
716};
717
718static struct config_item_type config_desc_type = {
719 .ct_group_ops = &config_desc_ops,
720 .ct_owner = THIS_MODULE,
721};
722
723CONFIGFS_ATTR_STRUCT(gadget_strings);
724GS_STRINGS_RW(gadget_strings, manufacturer);
725GS_STRINGS_RW(gadget_strings, product);
726GS_STRINGS_RW(gadget_strings, serialnumber);
727
728static struct configfs_attribute *gadget_strings_langid_attrs[] = {
729 &gadget_strings_manufacturer.attr,
730 &gadget_strings_product.attr,
731 &gadget_strings_serialnumber.attr,
732 NULL,
733};
734
735static void gadget_strings_attr_release(struct config_item *item)
736{
737 struct gadget_strings *gs = to_gadget_strings(item);
738
739 kfree(gs->manufacturer);
740 kfree(gs->product);
741 kfree(gs->serialnumber);
742
743 list_del(&gs->list);
744 kfree(gs);
745}
746
747USB_CONFIG_STRING_RW_OPS(gadget_strings);
748USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
749
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200750static inline struct os_desc *to_os_desc(struct config_item *item)
751{
752 return container_of(to_config_group(item), struct os_desc, group);
753}
754
755CONFIGFS_ATTR_STRUCT(os_desc);
756CONFIGFS_ATTR_OPS(os_desc);
757
758static ssize_t os_desc_use_show(struct os_desc *os_desc, char *page)
759{
760 struct gadget_info *gi;
761
762 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
763
764 return sprintf(page, "%d", gi->use_os_desc);
765}
766
767static ssize_t os_desc_use_store(struct os_desc *os_desc, const char *page,
768 size_t len)
769{
770 struct gadget_info *gi;
771 int ret;
772 bool use;
773
774 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
775
776 mutex_lock(&gi->lock);
777 ret = strtobool(page, &use);
778 if (!ret) {
779 gi->use_os_desc = use;
780 ret = len;
781 }
782 mutex_unlock(&gi->lock);
783
784 return ret;
785}
786
787static 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);
791
792static ssize_t os_desc_b_vendor_code_show(struct os_desc *os_desc, char *page)
793{
794 struct gadget_info *gi;
795
796 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
797
798 return sprintf(page, "%d", gi->b_vendor_code);
799}
800
801static ssize_t os_desc_b_vendor_code_store(struct os_desc *os_desc,
802 const char *page, size_t len)
803{
804 struct gadget_info *gi;
805 int ret;
806 u8 b_vendor_code;
807
808 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
809
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;
815 }
816 mutex_unlock(&gi->lock);
817
818 return ret;
819}
820
821static 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);
825
826static ssize_t os_desc_qw_sign_show(struct os_desc *os_desc, char *page)
827{
828 struct gadget_info *gi;
829
830 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
831
832 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
833
834 return OS_STRING_QW_SIGN_LEN;
835}
836
837static ssize_t os_desc_qw_sign_store(struct os_desc *os_desc, const char *page,
838 size_t len)
839{
840 struct gadget_info *gi;
841 int res, l;
842
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;
847
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);
855
856 return res;
857}
858
859static 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);
863
864static 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,
869};
870
871static void os_desc_attr_release(struct config_item *item)
872{
873 struct os_desc *os_desc = to_os_desc(item);
874 kfree(os_desc);
875}
876
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200877static int os_desc_link(struct config_item *os_desc_ci,
878 struct config_item *usb_cfg_ci)
879{
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;
888
889 mutex_lock(&gi->lock);
890 list_for_each_entry(c, &cdev->configs, list) {
891 if (c == &c_target->c)
892 break;
893 }
894 if (c != &c_target->c) {
895 ret = -EINVAL;
896 goto out;
897 }
898
899 if (cdev->os_desc_config) {
900 ret = -EBUSY;
901 goto out;
902 }
903
904 cdev->os_desc_config = &c_target->c;
905 ret = 0;
906
907out:
908 mutex_unlock(&gi->lock);
909 return ret;
910}
911
912static int os_desc_unlink(struct config_item *os_desc_ci,
913 struct config_item *usb_cfg_ci)
914{
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;
918
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;
926}
927
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200928static 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,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200932 .allow_link = os_desc_link,
933 .drop_link = os_desc_unlink,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200934};
935
936static 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,
940};
941
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200942CONFIGFS_ATTR_STRUCT(usb_os_desc);
943CONFIGFS_ATTR_OPS(usb_os_desc);
944
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200945
946static inline struct usb_os_desc_ext_prop
947*to_usb_os_desc_ext_prop(struct config_item *item)
948{
949 return container_of(item, struct usb_os_desc_ext_prop, item);
950}
951
952CONFIGFS_ATTR_STRUCT(usb_os_desc_ext_prop);
953CONFIGFS_ATTR_OPS(usb_os_desc_ext_prop);
954
955static ssize_t ext_prop_type_show(struct usb_os_desc_ext_prop *ext_prop,
956 char *page)
957{
958 return sprintf(page, "%d", ext_prop->type);
959}
960
961static ssize_t ext_prop_type_store(struct usb_os_desc_ext_prop *ext_prop,
962 const char *page, size_t len)
963{
964 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
965 u8 type;
966 int ret;
967
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;
976 }
977
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;
994
995end:
996 if (desc->opts_mutex)
997 mutex_unlock(desc->opts_mutex);
998 return ret;
999}
1000
1001static ssize_t ext_prop_data_show(struct usb_os_desc_ext_prop *ext_prop,
1002 char *page)
1003{
1004 int len = ext_prop->data_len;
1005
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);
1011
1012 return len;
1013}
1014
1015static ssize_t ext_prop_data_store(struct usb_os_desc_ext_prop *ext_prop,
1016 const char *page, size_t len)
1017{
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;
1021
1022 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1023 --len;
1024 new_data = kzalloc(len, GFP_KERNEL);
1025 if (!new_data)
1026 return -ENOMEM;
1027
1028 memcpy(new_data, page, len);
1029
1030 if (desc->opts_mutex)
1031 mutex_lock(desc->opts_mutex);
1032 kfree(ext_prop->data);
1033 ext_prop->data = new_data;
1034 desc->ext_prop_len -= ext_prop->data_len;
1035 ext_prop->data_len = len;
1036 desc->ext_prop_len += ext_prop->data_len;
1037 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1038 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1039 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1040 desc->ext_prop_len -= ext_prop->data_len;
1041 ext_prop->data_len <<= 1;
1042 ext_prop->data_len += 2;
1043 desc->ext_prop_len += ext_prop->data_len;
1044 }
1045 if (desc->opts_mutex)
1046 mutex_unlock(desc->opts_mutex);
1047 return ret_len;
1048}
1049
1050static struct usb_os_desc_ext_prop_attribute ext_prop_type =
1051 __CONFIGFS_ATTR(type, S_IRUGO | S_IWUSR,
1052 ext_prop_type_show, ext_prop_type_store);
1053
1054static struct usb_os_desc_ext_prop_attribute ext_prop_data =
1055 __CONFIGFS_ATTR(data, S_IRUGO | S_IWUSR,
1056 ext_prop_data_show, ext_prop_data_store);
1057
1058static struct configfs_attribute *ext_prop_attrs[] = {
1059 &ext_prop_type.attr,
1060 &ext_prop_data.attr,
1061 NULL,
1062};
1063
1064static void usb_os_desc_ext_prop_release(struct config_item *item)
1065{
1066 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1067
1068 kfree(ext_prop); /* frees a whole chunk */
1069}
1070
1071static struct configfs_item_operations ext_prop_ops = {
1072 .release = usb_os_desc_ext_prop_release,
1073 .show_attribute = usb_os_desc_ext_prop_attr_show,
1074 .store_attribute = usb_os_desc_ext_prop_attr_store,
1075};
1076
1077static struct config_item *ext_prop_make(
1078 struct config_group *group,
1079 const char *name)
1080{
1081 struct usb_os_desc_ext_prop *ext_prop;
1082 struct config_item_type *ext_prop_type;
1083 struct usb_os_desc *desc;
1084 char *vlabuf;
1085
1086 vla_group(data_chunk);
1087 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1088 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1089
1090 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1091 if (!vlabuf)
1092 return ERR_PTR(-ENOMEM);
1093
1094 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1095 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1096
1097 desc = container_of(group, struct usb_os_desc, group);
1098 ext_prop_type->ct_item_ops = &ext_prop_ops;
1099 ext_prop_type->ct_attrs = ext_prop_attrs;
1100 ext_prop_type->ct_owner = desc->owner;
1101
1102 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1103
1104 ext_prop->name = kstrdup(name, GFP_KERNEL);
1105 if (!ext_prop->name) {
1106 kfree(vlabuf);
1107 return ERR_PTR(-ENOMEM);
1108 }
1109 desc->ext_prop_len += 14;
1110 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1111 if (desc->opts_mutex)
1112 mutex_lock(desc->opts_mutex);
1113 desc->ext_prop_len += ext_prop->name_len;
1114 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1115 ++desc->ext_prop_count;
1116 if (desc->opts_mutex)
1117 mutex_unlock(desc->opts_mutex);
1118
1119 return &ext_prop->item;
1120}
1121
1122static void ext_prop_drop(struct config_group *group, struct config_item *item)
1123{
1124 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1125 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1126
1127 if (desc->opts_mutex)
1128 mutex_lock(desc->opts_mutex);
1129 list_del(&ext_prop->entry);
1130 --desc->ext_prop_count;
1131 kfree(ext_prop->name);
1132 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1133 if (desc->opts_mutex)
1134 mutex_unlock(desc->opts_mutex);
1135 config_item_put(item);
1136}
1137
1138static struct configfs_group_operations interf_grp_ops = {
1139 .make_item = &ext_prop_make,
1140 .drop_item = &ext_prop_drop,
1141};
1142
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001143static struct configfs_item_operations interf_item_ops = {
1144 .show_attribute = usb_os_desc_attr_show,
1145 .store_attribute = usb_os_desc_attr_store,
1146};
1147
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001148static ssize_t interf_grp_compatible_id_show(struct usb_os_desc *desc,
1149 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001150{
1151 memcpy(page, desc->ext_compat_id, 8);
1152 return 8;
1153}
1154
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001155static ssize_t interf_grp_compatible_id_store(struct usb_os_desc *desc,
1156 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001157{
1158 int l;
1159
1160 l = min_t(int, 8, len);
1161 if (page[l - 1] == '\n')
1162 --l;
1163 if (desc->opts_mutex)
1164 mutex_lock(desc->opts_mutex);
1165 memcpy(desc->ext_compat_id, page, l);
1166 desc->ext_compat_id[l] = '\0';
1167
1168 if (desc->opts_mutex)
1169 mutex_unlock(desc->opts_mutex);
1170
1171 return len;
1172}
1173
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001174static struct usb_os_desc_attribute interf_grp_attr_compatible_id =
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001175 __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001176 interf_grp_compatible_id_show,
1177 interf_grp_compatible_id_store);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001178
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001179static ssize_t interf_grp_sub_compatible_id_show(struct usb_os_desc *desc,
1180 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001181{
1182 memcpy(page, desc->ext_compat_id + 8, 8);
1183 return 8;
1184}
1185
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001186static ssize_t interf_grp_sub_compatible_id_store(struct usb_os_desc *desc,
1187 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001188{
1189 int l;
1190
1191 l = min_t(int, 8, len);
1192 if (page[l - 1] == '\n')
1193 --l;
1194 if (desc->opts_mutex)
1195 mutex_lock(desc->opts_mutex);
1196 memcpy(desc->ext_compat_id + 8, page, l);
1197 desc->ext_compat_id[l + 8] = '\0';
1198
1199 if (desc->opts_mutex)
1200 mutex_unlock(desc->opts_mutex);
1201
1202 return len;
1203}
1204
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001205static struct usb_os_desc_attribute interf_grp_attr_sub_compatible_id =
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001206 __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001207 interf_grp_sub_compatible_id_show,
1208 interf_grp_sub_compatible_id_store);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001209
1210static struct configfs_attribute *interf_grp_attrs[] = {
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001211 &interf_grp_attr_compatible_id.attr,
1212 &interf_grp_attr_sub_compatible_id.attr,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001213 NULL
1214};
1215
1216int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1217 int n_interf,
1218 struct usb_os_desc **desc,
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +02001219 char **names,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001220 struct module *owner)
1221{
1222 struct config_group **f_default_groups, *os_desc_group,
1223 **interface_groups;
1224 struct config_item_type *os_desc_type, *interface_type;
1225
1226 vla_group(data_chunk);
1227 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1228 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1229 vla_item(data_chunk, struct config_group *, interface_groups,
1230 n_interf + 1);
1231 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1232 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1233
1234 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1235 if (!vlabuf)
1236 return -ENOMEM;
1237
1238 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1239 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1240 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1241 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1242 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1243
1244 parent->default_groups = f_default_groups;
1245 os_desc_type->ct_owner = owner;
1246 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1247 f_default_groups[0] = os_desc_group;
1248
1249 os_desc_group->default_groups = interface_groups;
1250 interface_type->ct_item_ops = &interf_item_ops;
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001251 interface_type->ct_group_ops = &interf_grp_ops;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001252 interface_type->ct_attrs = interf_grp_attrs;
1253 interface_type->ct_owner = owner;
1254
1255 while (n_interf--) {
1256 struct usb_os_desc *d;
1257
1258 d = desc[n_interf];
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001259 d->owner = owner;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001260 config_group_init_type_name(&d->group, "", interface_type);
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +02001261 config_item_set_name(&d->group.cg_item, "interface.%s",
1262 names[n_interf]);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001263 interface_groups[n_interf] = &d->group;
1264 }
1265
1266 return 0;
1267}
1268EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1269
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001270static int configfs_do_nothing(struct usb_composite_dev *cdev)
1271{
David Rientjes75bfe232013-04-07 14:11:47 -07001272 WARN_ON(1);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001273 return -EINVAL;
1274}
1275
1276int composite_dev_prepare(struct usb_composite_driver *composite,
1277 struct usb_composite_dev *dev);
1278
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001279int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1280 struct usb_ep *ep0);
1281
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001282static void purge_configs_funcs(struct gadget_info *gi)
1283{
1284 struct usb_configuration *c;
1285
1286 list_for_each_entry(c, &gi->cdev.configs, list) {
1287 struct usb_function *f, *tmp;
1288 struct config_usb_cfg *cfg;
1289
1290 cfg = container_of(c, struct config_usb_cfg, c);
1291
1292 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1293
1294 list_move_tail(&f->list, &cfg->func_list);
1295 if (f->unbind) {
1296 dev_err(&gi->cdev.gadget->dev, "unbind function"
1297 " '%s'/%p\n", f->name, f);
1298 f->unbind(c, f);
1299 }
1300 }
1301 c->next_interface_id = 0;
1302 c->superspeed = 0;
1303 c->highspeed = 0;
1304 c->fullspeed = 0;
1305 }
1306}
1307
1308static int configfs_composite_bind(struct usb_gadget *gadget,
1309 struct usb_gadget_driver *gdriver)
1310{
1311 struct usb_composite_driver *composite = to_cdriver(gdriver);
1312 struct gadget_info *gi = container_of(composite,
1313 struct gadget_info, composite);
1314 struct usb_composite_dev *cdev = &gi->cdev;
1315 struct usb_configuration *c;
1316 struct usb_string *s;
1317 unsigned i;
1318 int ret;
1319
1320 /* the gi->lock is hold by the caller */
1321 cdev->gadget = gadget;
1322 set_gadget_data(gadget, cdev);
1323 ret = composite_dev_prepare(composite, cdev);
1324 if (ret)
1325 return ret;
1326 /* and now the gadget bind */
1327 ret = -EINVAL;
1328
1329 if (list_empty(&gi->cdev.configs)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001330 pr_err("Need at least one configuration in %s.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001331 gi->composite.name);
1332 goto err_comp_cleanup;
1333 }
1334
1335
1336 list_for_each_entry(c, &gi->cdev.configs, list) {
1337 struct config_usb_cfg *cfg;
1338
1339 cfg = container_of(c, struct config_usb_cfg, c);
1340 if (list_empty(&cfg->func_list)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001341 pr_err("Config %s/%d of %s needs at least one function.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001342 c->label, c->bConfigurationValue,
1343 gi->composite.name);
1344 goto err_comp_cleanup;
1345 }
1346 }
1347
1348 /* init all strings */
1349 if (!list_empty(&gi->string_list)) {
1350 struct gadget_strings *gs;
1351
1352 i = 0;
1353 list_for_each_entry(gs, &gi->string_list, list) {
1354
1355 gi->gstrings[i] = &gs->stringtab_dev;
1356 gs->stringtab_dev.strings = gs->strings;
1357 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1358 gs->manufacturer;
1359 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1360 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1361 i++;
1362 }
1363 gi->gstrings[i] = NULL;
1364 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1365 USB_GADGET_FIRST_AVAIL_IDX);
Wei Yongjunfea77072013-05-07 19:50:31 +08001366 if (IS_ERR(s)) {
1367 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001368 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001369 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001370
1371 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1372 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1373 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1374 }
1375
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001376 if (gi->use_os_desc) {
1377 cdev->use_os_string = true;
1378 cdev->b_vendor_code = gi->b_vendor_code;
1379 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1380 }
1381
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001382 /* Go through all configs, attach all functions */
1383 list_for_each_entry(c, &gi->cdev.configs, list) {
1384 struct config_usb_cfg *cfg;
1385 struct usb_function *f;
1386 struct usb_function *tmp;
1387 struct gadget_config_name *cn;
1388
1389 cfg = container_of(c, struct config_usb_cfg, c);
1390 if (!list_empty(&cfg->string_list)) {
1391 i = 0;
1392 list_for_each_entry(cn, &cfg->string_list, list) {
1393 cfg->gstrings[i] = &cn->stringtab_dev;
1394 cn->stringtab_dev.strings = &cn->strings;
1395 cn->strings.s = cn->configuration;
1396 i++;
1397 }
1398 cfg->gstrings[i] = NULL;
1399 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
Wei Yongjunfea77072013-05-07 19:50:31 +08001400 if (IS_ERR(s)) {
1401 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001402 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001403 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001404 c->iConfiguration = s[0].id;
1405 }
1406
1407 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1408 list_del(&f->list);
1409 ret = usb_add_function(c, f);
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001410 if (ret) {
1411 list_add(&f->list, &cfg->func_list);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001412 goto err_purge_funcs;
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001413 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001414 }
1415 usb_ep_autoconfig_reset(cdev->gadget);
1416 }
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001417 if (cdev->use_os_string) {
1418 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1419 if (ret)
1420 goto err_purge_funcs;
1421 }
1422
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001423 usb_ep_autoconfig_reset(cdev->gadget);
1424 return 0;
1425
1426err_purge_funcs:
1427 purge_configs_funcs(gi);
1428err_comp_cleanup:
1429 composite_dev_cleanup(cdev);
1430 return ret;
1431}
1432
1433static void configfs_composite_unbind(struct usb_gadget *gadget)
1434{
1435 struct usb_composite_dev *cdev;
1436 struct gadget_info *gi;
1437
1438 /* the gi->lock is hold by the caller */
1439
1440 cdev = get_gadget_data(gadget);
1441 gi = container_of(cdev, struct gadget_info, cdev);
1442
1443 purge_configs_funcs(gi);
1444 composite_dev_cleanup(cdev);
1445 usb_ep_autoconfig_reset(cdev->gadget);
1446 cdev->gadget = NULL;
1447 set_gadget_data(gadget, NULL);
1448}
1449
1450static const struct usb_gadget_driver configfs_driver_template = {
1451 .bind = configfs_composite_bind,
1452 .unbind = configfs_composite_unbind,
1453
1454 .setup = composite_setup,
1455 .disconnect = composite_disconnect,
1456
1457 .max_speed = USB_SPEED_SUPER,
1458 .driver = {
1459 .owner = THIS_MODULE,
1460 .name = "configfs-gadget",
1461 },
1462};
1463
1464static struct config_group *gadgets_make(
1465 struct config_group *group,
1466 const char *name)
1467{
1468 struct gadget_info *gi;
1469
1470 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1471 if (!gi)
1472 return ERR_PTR(-ENOMEM);
1473
1474 gi->group.default_groups = gi->default_groups;
1475 gi->group.default_groups[0] = &gi->functions_group;
1476 gi->group.default_groups[1] = &gi->configs_group;
1477 gi->group.default_groups[2] = &gi->strings_group;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001478 gi->group.default_groups[3] = &gi->os_desc_group;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001479
1480 config_group_init_type_name(&gi->functions_group, "functions",
1481 &functions_type);
1482 config_group_init_type_name(&gi->configs_group, "configs",
1483 &config_desc_type);
1484 config_group_init_type_name(&gi->strings_group, "strings",
1485 &gadget_strings_strings_type);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001486 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1487 &os_desc_type);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001488
1489 gi->composite.bind = configfs_do_nothing;
1490 gi->composite.unbind = configfs_do_nothing;
1491 gi->composite.suspend = NULL;
1492 gi->composite.resume = NULL;
1493 gi->composite.max_speed = USB_SPEED_SUPER;
1494
1495 mutex_init(&gi->lock);
1496 INIT_LIST_HEAD(&gi->string_list);
1497 INIT_LIST_HEAD(&gi->available_func);
1498
1499 composite_init_dev(&gi->cdev);
1500 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1501 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1502 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1503
1504 gi->composite.gadget_driver = configfs_driver_template;
1505
1506 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1507 gi->composite.name = gi->composite.gadget_driver.function;
1508
1509 if (!gi->composite.gadget_driver.function)
1510 goto err;
1511
1512#ifdef CONFIG_USB_OTG
1513 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
1514 gi->otg.bDescriptorType = USB_DT_OTG;
1515 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
1516#endif
1517
1518 config_group_init_type_name(&gi->group, name,
1519 &gadget_root_type);
1520 return &gi->group;
1521err:
1522 kfree(gi);
1523 return ERR_PTR(-ENOMEM);
1524}
1525
1526static void gadgets_drop(struct config_group *group, struct config_item *item)
1527{
1528 config_item_put(item);
1529}
1530
1531static struct configfs_group_operations gadgets_ops = {
1532 .make_group = &gadgets_make,
1533 .drop_item = &gadgets_drop,
1534};
1535
1536static struct config_item_type gadgets_type = {
1537 .ct_group_ops = &gadgets_ops,
1538 .ct_owner = THIS_MODULE,
1539};
1540
1541static struct configfs_subsystem gadget_subsys = {
1542 .su_group = {
1543 .cg_item = {
1544 .ci_namebuf = "usb_gadget",
1545 .ci_type = &gadgets_type,
1546 },
1547 },
1548 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1549};
1550
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001551void unregister_gadget_item(struct config_item *item)
1552{
1553 struct gadget_info *gi = to_gadget_info(item);
1554
1555 unregister_gadget(gi);
1556}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001557EXPORT_SYMBOL_GPL(unregister_gadget_item);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001558
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001559static int __init gadget_cfs_init(void)
1560{
1561 int ret;
1562
1563 config_group_init(&gadget_subsys.su_group);
1564
1565 ret = configfs_register_subsystem(&gadget_subsys);
1566 return ret;
1567}
1568module_init(gadget_cfs_init);
1569
1570static void __exit gadget_cfs_exit(void)
1571{
1572 configfs_unregister_subsystem(&gadget_subsys);
1573}
1574module_exit(gadget_cfs_exit);