1 #include <linux/configfs.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
5 #include <linux/nls.h>
6 #include <linux/usb/composite.h>
7 #include <linux/usb/gadget_configfs.h>
8 #include "configfs.h"
9 #include "u_f.h"
10 #include "u_os_desc.h"
11
12 #ifdef CONFIG_USB_CONFIGFS_UEVENT
13 #include <linux/platform_device.h>
14 #include <linux/kdev_t.h>
15 #include <linux/usb/ch9.h>
16
17 #ifdef CONFIG_USB_CONFIGFS_F_ACC
18 extern int acc_ctrlrequest(struct usb_composite_dev *cdev,
19 const struct usb_ctrlrequest *ctrl);
20 void acc_disconnect(void);
21 #endif
22 static struct class *android_class;
23 static struct device *android_device;
24 static int index;
25
create_function_device(char * name)26 struct device *create_function_device(char *name)
27 {
28 if (android_device && !IS_ERR(android_device))
29 return device_create(android_class, android_device,
30 MKDEV(0, index++), NULL, name);
31 else
32 return ERR_PTR(-EINVAL);
33 }
34 EXPORT_SYMBOL_GPL(create_function_device);
35 #endif
36
check_user_usb_string(const char * name,struct usb_gadget_strings * stringtab_dev)37 int check_user_usb_string(const char *name,
38 struct usb_gadget_strings *stringtab_dev)
39 {
40 unsigned primary_lang;
41 unsigned sub_lang;
42 u16 num;
43 int ret;
44
45 ret = kstrtou16(name, 0, &num);
46 if (ret)
47 return ret;
48
49 primary_lang = num & 0x3ff;
50 sub_lang = num >> 10;
51
52 /* simple sanity check for valid langid */
53 switch (primary_lang) {
54 case 0:
55 case 0x62 ... 0xfe:
56 case 0x100 ... 0x3ff:
57 return -EINVAL;
58 }
59 if (!sub_lang)
60 return -EINVAL;
61
62 stringtab_dev->language = num;
63 return 0;
64 }
65
66 #define MAX_NAME_LEN 40
67 #define MAX_USB_STRING_LANGS 2
68
69 static const struct usb_descriptor_header *otg_desc[2];
70
71 struct gadget_info {
72 struct config_group group;
73 struct config_group functions_group;
74 struct config_group configs_group;
75 struct config_group strings_group;
76 struct config_group os_desc_group;
77 struct config_group *default_groups[5];
78
79 struct mutex lock;
80 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
81 struct list_head string_list;
82 struct list_head available_func;
83
84 const char *udc_name;
85 struct usb_composite_driver composite;
86 struct usb_composite_dev cdev;
87 bool use_os_desc;
88 char b_vendor_code;
89 char qw_sign[OS_STRING_QW_SIGN_LEN];
90 spinlock_t spinlock;
91 bool unbind;
92 #ifdef CONFIG_USB_CONFIGFS_UEVENT
93 bool connected;
94 bool sw_connected;
95 struct work_struct work;
96 struct device *dev;
97 #endif
98 };
99
to_gadget_info(struct config_item * item)100 static inline struct gadget_info *to_gadget_info(struct config_item *item)
101 {
102 return container_of(to_config_group(item), struct gadget_info, group);
103 }
104
105 struct config_usb_cfg {
106 struct config_group group;
107 struct config_group strings_group;
108 struct config_group *default_groups[2];
109 struct list_head string_list;
110 struct usb_configuration c;
111 struct list_head func_list;
112 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
113 };
114
to_config_usb_cfg(struct config_item * item)115 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
116 {
117 return container_of(to_config_group(item), struct config_usb_cfg,
118 group);
119 }
120
121 struct gadget_strings {
122 struct usb_gadget_strings stringtab_dev;
123 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
124 char *manufacturer;
125 char *product;
126 char *serialnumber;
127
128 struct config_group group;
129 struct list_head list;
130 };
131
132 struct os_desc {
133 struct config_group group;
134 };
135
136 struct gadget_config_name {
137 struct usb_gadget_strings stringtab_dev;
138 struct usb_string strings;
139 char *configuration;
140
141 struct config_group group;
142 struct list_head list;
143 };
144
145 #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
146
usb_string_copy(const char * s,char ** s_copy)147 static int usb_string_copy(const char *s, char **s_copy)
148 {
149 int ret;
150 char *str;
151 char *copy = *s_copy;
152 ret = strlen(s);
153 if (ret > USB_MAX_STRING_LEN)
154 return -EOVERFLOW;
155
156 if (copy) {
157 str = copy;
158 } else {
159 str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
160 if (!str)
161 return -ENOMEM;
162 }
163 strcpy(str, s);
164 if (str[ret - 1] == '\n')
165 str[ret - 1] = '\0';
166 *s_copy = str;
167 return 0;
168 }
169
170 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
171 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
172 char *page) \
173 { \
174 return sprintf(page, "0x%02x\n", \
175 to_gadget_info(item)->cdev.desc.__name); \
176 }
177
178 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
179 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
180 char *page) \
181 { \
182 return sprintf(page, "0x%04x\n", \
183 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
184 }
185
186
187 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
188 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
189 const char *page, size_t len) \
190 { \
191 u8 val; \
192 int ret; \
193 ret = kstrtou8(page, 0, &val); \
194 if (ret) \
195 return ret; \
196 to_gadget_info(item)->cdev.desc._name = val; \
197 return len; \
198 }
199
200 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
201 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
202 const char *page, size_t len) \
203 { \
204 u16 val; \
205 int ret; \
206 ret = kstrtou16(page, 0, &val); \
207 if (ret) \
208 return ret; \
209 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
210 return len; \
211 }
212
213 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
214 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
215 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
216
217 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
218 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
219 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
220 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
221 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
222 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
223 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
224 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
225
is_valid_bcd(u16 bcd_val)226 static ssize_t is_valid_bcd(u16 bcd_val)
227 {
228 if ((bcd_val & 0xf) > 9)
229 return -EINVAL;
230 if (((bcd_val >> 4) & 0xf) > 9)
231 return -EINVAL;
232 if (((bcd_val >> 8) & 0xf) > 9)
233 return -EINVAL;
234 if (((bcd_val >> 12) & 0xf) > 9)
235 return -EINVAL;
236 return 0;
237 }
238
gadget_dev_desc_bcdDevice_store(struct config_item * item,const char * page,size_t len)239 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
240 const char *page, size_t len)
241 {
242 u16 bcdDevice;
243 int ret;
244
245 ret = kstrtou16(page, 0, &bcdDevice);
246 if (ret)
247 return ret;
248 ret = is_valid_bcd(bcdDevice);
249 if (ret)
250 return ret;
251
252 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
253 return len;
254 }
255
gadget_dev_desc_bcdUSB_store(struct config_item * item,const char * page,size_t len)256 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
257 const char *page, size_t len)
258 {
259 u16 bcdUSB;
260 int ret;
261
262 ret = kstrtou16(page, 0, &bcdUSB);
263 if (ret)
264 return ret;
265 ret = is_valid_bcd(bcdUSB);
266 if (ret)
267 return ret;
268
269 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
270 return len;
271 }
272
gadget_dev_desc_UDC_show(struct config_item * item,char * page)273 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
274 {
275 return sprintf(page, "%s\n", to_gadget_info(item)->udc_name ?: "");
276 }
277
unregister_gadget(struct gadget_info * gi)278 static int unregister_gadget(struct gadget_info *gi)
279 {
280 int ret;
281
282 if (!gi->udc_name)
283 return -ENODEV;
284
285 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
286 if (ret)
287 return ret;
288 kfree(gi->udc_name);
289 gi->udc_name = NULL;
290 return 0;
291 }
292
gadget_dev_desc_UDC_store(struct config_item * item,const char * page,size_t len)293 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
294 const char *page, size_t len)
295 {
296 struct gadget_info *gi = to_gadget_info(item);
297 char *name;
298 int ret;
299
300 if (strlen(page) < len)
301 return -EOVERFLOW;
302
303 name = kstrdup(page, GFP_KERNEL);
304 if (!name)
305 return -ENOMEM;
306 if (name[len - 1] == '\n')
307 name[len - 1] = '\0';
308
309 mutex_lock(&gi->lock);
310
311 if (!strlen(name) || strcmp(name, "none") == 0) {
312 ret = unregister_gadget(gi);
313 if (ret)
314 goto err;
315 kfree(name);
316 } else {
317 if (gi->udc_name) {
318 ret = -EBUSY;
319 goto err;
320 }
321 ret = usb_udc_attach_driver(name, &gi->composite.gadget_driver);
322 if (ret)
323 goto err;
324 gi->udc_name = name;
325 }
326 mutex_unlock(&gi->lock);
327 return len;
328 err:
329 kfree(name);
330 mutex_unlock(&gi->lock);
331 return ret;
332 }
333
334 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
335 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
336 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
337 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
338 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
339 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
340 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
341 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
342 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
343
344 static struct configfs_attribute *gadget_root_attrs[] = {
345 &gadget_dev_desc_attr_bDeviceClass,
346 &gadget_dev_desc_attr_bDeviceSubClass,
347 &gadget_dev_desc_attr_bDeviceProtocol,
348 &gadget_dev_desc_attr_bMaxPacketSize0,
349 &gadget_dev_desc_attr_idVendor,
350 &gadget_dev_desc_attr_idProduct,
351 &gadget_dev_desc_attr_bcdDevice,
352 &gadget_dev_desc_attr_bcdUSB,
353 &gadget_dev_desc_attr_UDC,
354 NULL,
355 };
356
to_gadget_strings(struct config_item * item)357 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
358 {
359 return container_of(to_config_group(item), struct gadget_strings,
360 group);
361 }
362
to_gadget_config_name(struct config_item * item)363 static inline struct gadget_config_name *to_gadget_config_name(
364 struct config_item *item)
365 {
366 return container_of(to_config_group(item), struct gadget_config_name,
367 group);
368 }
369
to_usb_function_instance(struct config_item * item)370 static inline struct usb_function_instance *to_usb_function_instance(
371 struct config_item *item)
372 {
373 return container_of(to_config_group(item),
374 struct usb_function_instance, group);
375 }
376
gadget_info_attr_release(struct config_item * item)377 static void gadget_info_attr_release(struct config_item *item)
378 {
379 struct gadget_info *gi = to_gadget_info(item);
380
381 WARN_ON(!list_empty(&gi->cdev.configs));
382 WARN_ON(!list_empty(&gi->string_list));
383 WARN_ON(!list_empty(&gi->available_func));
384 kfree(gi->composite.gadget_driver.function);
385 kfree(gi);
386 }
387
388 static struct configfs_item_operations gadget_root_item_ops = {
389 .release = gadget_info_attr_release,
390 };
391
gadget_config_attr_release(struct config_item * item)392 static void gadget_config_attr_release(struct config_item *item)
393 {
394 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
395
396 WARN_ON(!list_empty(&cfg->c.functions));
397 list_del(&cfg->c.list);
398 kfree(cfg->c.label);
399 kfree(cfg);
400 }
401
config_usb_cfg_link(struct config_item * usb_cfg_ci,struct config_item * usb_func_ci)402 static int config_usb_cfg_link(
403 struct config_item *usb_cfg_ci,
404 struct config_item *usb_func_ci)
405 {
406 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
407 struct usb_composite_dev *cdev = cfg->c.cdev;
408 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
409
410 struct config_group *group = to_config_group(usb_func_ci);
411 struct usb_function_instance *fi = container_of(group,
412 struct usb_function_instance, group);
413 struct usb_function_instance *a_fi;
414 struct usb_function *f;
415 int ret;
416
417 mutex_lock(&gi->lock);
418 /*
419 * Make sure this function is from within our _this_ gadget and not
420 * from another gadget or a random directory.
421 * Also a function instance can only be linked once.
422 */
423 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
424 if (a_fi == fi)
425 break;
426 }
427 if (a_fi != fi) {
428 ret = -EINVAL;
429 goto out;
430 }
431
432 list_for_each_entry(f, &cfg->func_list, list) {
433 if (f->fi == fi) {
434 ret = -EEXIST;
435 goto out;
436 }
437 }
438
439 f = usb_get_function(fi);
440 if (IS_ERR(f)) {
441 ret = PTR_ERR(f);
442 goto out;
443 }
444
445 /* stash the function until we bind it to the gadget */
446 list_add_tail(&f->list, &cfg->func_list);
447 ret = 0;
448 out:
449 mutex_unlock(&gi->lock);
450 return ret;
451 }
452
config_usb_cfg_unlink(struct config_item * usb_cfg_ci,struct config_item * usb_func_ci)453 static int config_usb_cfg_unlink(
454 struct config_item *usb_cfg_ci,
455 struct config_item *usb_func_ci)
456 {
457 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
458 struct usb_composite_dev *cdev = cfg->c.cdev;
459 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
460
461 struct config_group *group = to_config_group(usb_func_ci);
462 struct usb_function_instance *fi = container_of(group,
463 struct usb_function_instance, group);
464 struct usb_function *f;
465
466 /*
467 * ideally I would like to forbid to unlink functions while a gadget is
468 * bound to an UDC. Since this isn't possible at the moment, we simply
469 * force an unbind, the function is available here and then we can
470 * remove the function.
471 */
472 mutex_lock(&gi->lock);
473 if (gi->udc_name)
474 unregister_gadget(gi);
475 WARN_ON(gi->udc_name);
476
477 list_for_each_entry(f, &cfg->func_list, list) {
478 if (f->fi == fi) {
479 list_del(&f->list);
480 usb_put_function(f);
481 mutex_unlock(&gi->lock);
482 return 0;
483 }
484 }
485 mutex_unlock(&gi->lock);
486 WARN(1, "Unable to locate function to unbind\n");
487 return 0;
488 }
489
490 static struct configfs_item_operations gadget_config_item_ops = {
491 .release = gadget_config_attr_release,
492 .allow_link = config_usb_cfg_link,
493 .drop_link = config_usb_cfg_unlink,
494 };
495
496
gadget_config_desc_MaxPower_show(struct config_item * item,char * page)497 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
498 char *page)
499 {
500 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
501 }
502
gadget_config_desc_MaxPower_store(struct config_item * item,const char * page,size_t len)503 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
504 const char *page, size_t len)
505 {
506 u16 val;
507 int ret;
508 ret = kstrtou16(page, 0, &val);
509 if (ret)
510 return ret;
511 if (DIV_ROUND_UP(val, 8) > 0xff)
512 return -ERANGE;
513 to_config_usb_cfg(item)->c.MaxPower = val;
514 return len;
515 }
516
gadget_config_desc_bmAttributes_show(struct config_item * item,char * page)517 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
518 char *page)
519 {
520 return sprintf(page, "0x%02x\n",
521 to_config_usb_cfg(item)->c.bmAttributes);
522 }
523
gadget_config_desc_bmAttributes_store(struct config_item * item,const char * page,size_t len)524 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
525 const char *page, size_t len)
526 {
527 u8 val;
528 int ret;
529 ret = kstrtou8(page, 0, &val);
530 if (ret)
531 return ret;
532 if (!(val & USB_CONFIG_ATT_ONE))
533 return -EINVAL;
534 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
535 USB_CONFIG_ATT_WAKEUP))
536 return -EINVAL;
537 to_config_usb_cfg(item)->c.bmAttributes = val;
538 return len;
539 }
540
541 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
542 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
543
544 static struct configfs_attribute *gadget_config_attrs[] = {
545 &gadget_config_desc_attr_MaxPower,
546 &gadget_config_desc_attr_bmAttributes,
547 NULL,
548 };
549
550 static struct config_item_type gadget_config_type = {
551 .ct_item_ops = &gadget_config_item_ops,
552 .ct_attrs = gadget_config_attrs,
553 .ct_owner = THIS_MODULE,
554 };
555
556 static struct config_item_type gadget_root_type = {
557 .ct_item_ops = &gadget_root_item_ops,
558 .ct_attrs = gadget_root_attrs,
559 .ct_owner = THIS_MODULE,
560 };
561
composite_init_dev(struct usb_composite_dev * cdev)562 static void composite_init_dev(struct usb_composite_dev *cdev)
563 {
564 spin_lock_init(&cdev->lock);
565 INIT_LIST_HEAD(&cdev->configs);
566 INIT_LIST_HEAD(&cdev->gstrings);
567 }
568
function_make(struct config_group * group,const char * name)569 static struct config_group *function_make(
570 struct config_group *group,
571 const char *name)
572 {
573 struct gadget_info *gi;
574 struct usb_function_instance *fi;
575 char buf[MAX_NAME_LEN];
576 char *func_name;
577 char *instance_name;
578 int ret;
579
580 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
581 if (ret >= MAX_NAME_LEN)
582 return ERR_PTR(-ENAMETOOLONG);
583
584 func_name = buf;
585 instance_name = strchr(func_name, '.');
586 if (!instance_name) {
587 pr_err("Unable to locate . in FUNC.INSTANCE\n");
588 return ERR_PTR(-EINVAL);
589 }
590 *instance_name = '\0';
591 instance_name++;
592
593 fi = usb_get_function_instance(func_name);
594 if (IS_ERR(fi))
595 return ERR_CAST(fi);
596
597 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
598 if (ret) {
599 usb_put_function_instance(fi);
600 return ERR_PTR(ret);
601 }
602 if (fi->set_inst_name) {
603 ret = fi->set_inst_name(fi, instance_name);
604 if (ret) {
605 usb_put_function_instance(fi);
606 return ERR_PTR(ret);
607 }
608 }
609
610 gi = container_of(group, struct gadget_info, functions_group);
611
612 mutex_lock(&gi->lock);
613 list_add_tail(&fi->cfs_list, &gi->available_func);
614 mutex_unlock(&gi->lock);
615 return &fi->group;
616 }
617
function_drop(struct config_group * group,struct config_item * item)618 static void function_drop(
619 struct config_group *group,
620 struct config_item *item)
621 {
622 struct usb_function_instance *fi = to_usb_function_instance(item);
623 struct gadget_info *gi;
624
625 gi = container_of(group, struct gadget_info, functions_group);
626
627 mutex_lock(&gi->lock);
628 list_del(&fi->cfs_list);
629 mutex_unlock(&gi->lock);
630 config_item_put(item);
631 }
632
633 static struct configfs_group_operations functions_ops = {
634 .make_group = &function_make,
635 .drop_item = &function_drop,
636 };
637
638 static struct config_item_type functions_type = {
639 .ct_group_ops = &functions_ops,
640 .ct_owner = THIS_MODULE,
641 };
642
643 GS_STRINGS_RW(gadget_config_name, configuration);
644
645 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
646 &gadget_config_name_attr_configuration,
647 NULL,
648 };
649
gadget_config_name_attr_release(struct config_item * item)650 static void gadget_config_name_attr_release(struct config_item *item)
651 {
652 struct gadget_config_name *cn = to_gadget_config_name(item);
653
654 kfree(cn->configuration);
655
656 list_del(&cn->list);
657 kfree(cn);
658 }
659
660 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
661 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
662
config_desc_make(struct config_group * group,const char * name)663 static struct config_group *config_desc_make(
664 struct config_group *group,
665 const char *name)
666 {
667 struct gadget_info *gi;
668 struct config_usb_cfg *cfg;
669 char buf[MAX_NAME_LEN];
670 char *num_str;
671 u8 num;
672 int ret;
673
674 gi = container_of(group, struct gadget_info, configs_group);
675 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
676 if (ret >= MAX_NAME_LEN)
677 return ERR_PTR(-ENAMETOOLONG);
678
679 num_str = strchr(buf, '.');
680 if (!num_str) {
681 pr_err("Unable to locate . in name.bConfigurationValue\n");
682 return ERR_PTR(-EINVAL);
683 }
684
685 *num_str = '\0';
686 num_str++;
687
688 if (!strlen(buf))
689 return ERR_PTR(-EINVAL);
690
691 ret = kstrtou8(num_str, 0, &num);
692 if (ret)
693 return ERR_PTR(ret);
694
695 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
696 if (!cfg)
697 return ERR_PTR(-ENOMEM);
698 cfg->c.label = kstrdup(buf, GFP_KERNEL);
699 if (!cfg->c.label) {
700 ret = -ENOMEM;
701 goto err;
702 }
703 cfg->c.bConfigurationValue = num;
704 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
705 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
706 INIT_LIST_HEAD(&cfg->string_list);
707 INIT_LIST_HEAD(&cfg->func_list);
708
709 cfg->group.default_groups = cfg->default_groups;
710 cfg->default_groups[0] = &cfg->strings_group;
711
712 config_group_init_type_name(&cfg->group, name,
713 &gadget_config_type);
714 config_group_init_type_name(&cfg->strings_group, "strings",
715 &gadget_config_name_strings_type);
716
717 ret = usb_add_config_only(&gi->cdev, &cfg->c);
718 if (ret)
719 goto err;
720
721 return &cfg->group;
722 err:
723 kfree(cfg->c.label);
724 kfree(cfg);
725 return ERR_PTR(ret);
726 }
727
config_desc_drop(struct config_group * group,struct config_item * item)728 static void config_desc_drop(
729 struct config_group *group,
730 struct config_item *item)
731 {
732 config_item_put(item);
733 }
734
735 static struct configfs_group_operations config_desc_ops = {
736 .make_group = &config_desc_make,
737 .drop_item = &config_desc_drop,
738 };
739
740 static struct config_item_type config_desc_type = {
741 .ct_group_ops = &config_desc_ops,
742 .ct_owner = THIS_MODULE,
743 };
744
745 GS_STRINGS_RW(gadget_strings, manufacturer);
746 GS_STRINGS_RW(gadget_strings, product);
747 GS_STRINGS_RW(gadget_strings, serialnumber);
748
749 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
750 &gadget_strings_attr_manufacturer,
751 &gadget_strings_attr_product,
752 &gadget_strings_attr_serialnumber,
753 NULL,
754 };
755
gadget_strings_attr_release(struct config_item * item)756 static void gadget_strings_attr_release(struct config_item *item)
757 {
758 struct gadget_strings *gs = to_gadget_strings(item);
759
760 kfree(gs->manufacturer);
761 kfree(gs->product);
762 kfree(gs->serialnumber);
763
764 list_del(&gs->list);
765 kfree(gs);
766 }
767
768 USB_CONFIG_STRING_RW_OPS(gadget_strings);
769 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
770
to_os_desc(struct config_item * item)771 static inline struct os_desc *to_os_desc(struct config_item *item)
772 {
773 return container_of(to_config_group(item), struct os_desc, group);
774 }
775
os_desc_item_to_gadget_info(struct config_item * item)776 static inline struct gadget_info *os_desc_item_to_gadget_info(
777 struct config_item *item)
778 {
779 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
780 }
781
os_desc_use_show(struct config_item * item,char * page)782 static ssize_t os_desc_use_show(struct config_item *item, char *page)
783 {
784 return sprintf(page, "%d",
785 os_desc_item_to_gadget_info(item)->use_os_desc);
786 }
787
os_desc_use_store(struct config_item * item,const char * page,size_t len)788 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
789 size_t len)
790 {
791 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
792 int ret;
793 bool use;
794
795 mutex_lock(&gi->lock);
796 ret = strtobool(page, &use);
797 if (!ret) {
798 gi->use_os_desc = use;
799 ret = len;
800 }
801 mutex_unlock(&gi->lock);
802
803 return ret;
804 }
805
os_desc_b_vendor_code_show(struct config_item * item,char * page)806 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
807 {
808 return sprintf(page, "%d",
809 os_desc_item_to_gadget_info(item)->b_vendor_code);
810 }
811
os_desc_b_vendor_code_store(struct config_item * item,const char * page,size_t len)812 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
813 const char *page, size_t len)
814 {
815 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
816 int ret;
817 u8 b_vendor_code;
818
819 mutex_lock(&gi->lock);
820 ret = kstrtou8(page, 0, &b_vendor_code);
821 if (!ret) {
822 gi->b_vendor_code = b_vendor_code;
823 ret = len;
824 }
825 mutex_unlock(&gi->lock);
826
827 return ret;
828 }
829
os_desc_qw_sign_show(struct config_item * item,char * page)830 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
831 {
832 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
833
834 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
835 return OS_STRING_QW_SIGN_LEN;
836 }
837
os_desc_qw_sign_store(struct config_item * item,const char * page,size_t len)838 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
839 size_t len)
840 {
841 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
842 int res, l;
843
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
859 CONFIGFS_ATTR(os_desc_, use);
860 CONFIGFS_ATTR(os_desc_, b_vendor_code);
861 CONFIGFS_ATTR(os_desc_, qw_sign);
862
863 static struct configfs_attribute *os_desc_attrs[] = {
864 &os_desc_attr_use,
865 &os_desc_attr_b_vendor_code,
866 &os_desc_attr_qw_sign,
867 NULL,
868 };
869
os_desc_attr_release(struct config_item * item)870 static 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
os_desc_link(struct config_item * os_desc_ci,struct config_item * usb_cfg_ci)876 static 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
906 out:
907 mutex_unlock(&gi->lock);
908 return ret;
909 }
910
os_desc_unlink(struct config_item * os_desc_ci,struct config_item * usb_cfg_ci)911 static 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
927 static struct configfs_item_operations os_desc_ops = {
928 .release = os_desc_attr_release,
929 .allow_link = os_desc_link,
930 .drop_link = os_desc_unlink,
931 };
932
933 static struct config_item_type os_desc_type = {
934 .ct_item_ops = &os_desc_ops,
935 .ct_attrs = os_desc_attrs,
936 .ct_owner = THIS_MODULE,
937 };
938
939 static inline struct usb_os_desc_ext_prop
to_usb_os_desc_ext_prop(struct config_item * item)940 *to_usb_os_desc_ext_prop(struct config_item *item)
941 {
942 return container_of(item, struct usb_os_desc_ext_prop, item);
943 }
944
ext_prop_type_show(struct config_item * item,char * page)945 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
946 {
947 return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
948 }
949
ext_prop_type_store(struct config_item * item,const char * page,size_t len)950 static ssize_t ext_prop_type_store(struct config_item *item,
951 const char *page, size_t len)
952 {
953 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
954 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
955 u8 type;
956 int ret;
957
958 if (desc->opts_mutex)
959 mutex_lock(desc->opts_mutex);
960 ret = kstrtou8(page, 0, &type);
961 if (ret)
962 goto end;
963 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
964 ret = -EINVAL;
965 goto end;
966 }
967
968 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
969 ext_prop->type == USB_EXT_PROP_LE32 ||
970 ext_prop->type == USB_EXT_PROP_BE32) &&
971 (type == USB_EXT_PROP_UNICODE ||
972 type == USB_EXT_PROP_UNICODE_ENV ||
973 type == USB_EXT_PROP_UNICODE_LINK))
974 ext_prop->data_len <<= 1;
975 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
976 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
977 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
978 (type == USB_EXT_PROP_BINARY ||
979 type == USB_EXT_PROP_LE32 ||
980 type == USB_EXT_PROP_BE32))
981 ext_prop->data_len >>= 1;
982 ext_prop->type = type;
983 ret = len;
984
985 end:
986 if (desc->opts_mutex)
987 mutex_unlock(desc->opts_mutex);
988 return ret;
989 }
990
ext_prop_data_show(struct config_item * item,char * page)991 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
992 {
993 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
994 int len = ext_prop->data_len;
995
996 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
997 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
998 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
999 len >>= 1;
1000 memcpy(page, ext_prop->data, len);
1001
1002 return len;
1003 }
1004
ext_prop_data_store(struct config_item * item,const char * page,size_t len)1005 static ssize_t ext_prop_data_store(struct config_item *item,
1006 const char *page, size_t len)
1007 {
1008 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1009 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1010 char *new_data;
1011 size_t ret_len = len;
1012
1013 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1014 --len;
1015 new_data = kmemdup(page, len, GFP_KERNEL);
1016 if (!new_data)
1017 return -ENOMEM;
1018
1019 if (desc->opts_mutex)
1020 mutex_lock(desc->opts_mutex);
1021 kfree(ext_prop->data);
1022 ext_prop->data = new_data;
1023 desc->ext_prop_len -= ext_prop->data_len;
1024 ext_prop->data_len = len;
1025 desc->ext_prop_len += ext_prop->data_len;
1026 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1027 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1028 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1029 desc->ext_prop_len -= ext_prop->data_len;
1030 ext_prop->data_len <<= 1;
1031 ext_prop->data_len += 2;
1032 desc->ext_prop_len += ext_prop->data_len;
1033 }
1034 if (desc->opts_mutex)
1035 mutex_unlock(desc->opts_mutex);
1036 return ret_len;
1037 }
1038
1039 CONFIGFS_ATTR(ext_prop_, type);
1040 CONFIGFS_ATTR(ext_prop_, data);
1041
1042 static struct configfs_attribute *ext_prop_attrs[] = {
1043 &ext_prop_attr_type,
1044 &ext_prop_attr_data,
1045 NULL,
1046 };
1047
usb_os_desc_ext_prop_release(struct config_item * item)1048 static void usb_os_desc_ext_prop_release(struct config_item *item)
1049 {
1050 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1051
1052 kfree(ext_prop); /* frees a whole chunk */
1053 }
1054
1055 static struct configfs_item_operations ext_prop_ops = {
1056 .release = usb_os_desc_ext_prop_release,
1057 };
1058
ext_prop_make(struct config_group * group,const char * name)1059 static struct config_item *ext_prop_make(
1060 struct config_group *group,
1061 const char *name)
1062 {
1063 struct usb_os_desc_ext_prop *ext_prop;
1064 struct config_item_type *ext_prop_type;
1065 struct usb_os_desc *desc;
1066 char *vlabuf;
1067
1068 vla_group(data_chunk);
1069 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1070 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1071
1072 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1073 if (!vlabuf)
1074 return ERR_PTR(-ENOMEM);
1075
1076 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1077 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1078
1079 desc = container_of(group, struct usb_os_desc, group);
1080 ext_prop_type->ct_item_ops = &ext_prop_ops;
1081 ext_prop_type->ct_attrs = ext_prop_attrs;
1082 ext_prop_type->ct_owner = desc->owner;
1083
1084 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1085
1086 ext_prop->name = kstrdup(name, GFP_KERNEL);
1087 if (!ext_prop->name) {
1088 kfree(vlabuf);
1089 return ERR_PTR(-ENOMEM);
1090 }
1091 desc->ext_prop_len += 14;
1092 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1093 if (desc->opts_mutex)
1094 mutex_lock(desc->opts_mutex);
1095 desc->ext_prop_len += ext_prop->name_len;
1096 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1097 ++desc->ext_prop_count;
1098 if (desc->opts_mutex)
1099 mutex_unlock(desc->opts_mutex);
1100
1101 return &ext_prop->item;
1102 }
1103
ext_prop_drop(struct config_group * group,struct config_item * item)1104 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1105 {
1106 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1107 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1108
1109 if (desc->opts_mutex)
1110 mutex_lock(desc->opts_mutex);
1111 list_del(&ext_prop->entry);
1112 --desc->ext_prop_count;
1113 kfree(ext_prop->name);
1114 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1115 if (desc->opts_mutex)
1116 mutex_unlock(desc->opts_mutex);
1117 config_item_put(item);
1118 }
1119
1120 static struct configfs_group_operations interf_grp_ops = {
1121 .make_item = &ext_prop_make,
1122 .drop_item = &ext_prop_drop,
1123 };
1124
interf_grp_compatible_id_show(struct config_item * item,char * page)1125 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1126 char *page)
1127 {
1128 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1129 return 8;
1130 }
1131
interf_grp_compatible_id_store(struct config_item * item,const char * page,size_t len)1132 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1133 const char *page, size_t len)
1134 {
1135 struct usb_os_desc *desc = to_usb_os_desc(item);
1136 int l;
1137
1138 l = min_t(int, 8, len);
1139 if (page[l - 1] == '\n')
1140 --l;
1141 if (desc->opts_mutex)
1142 mutex_lock(desc->opts_mutex);
1143 memcpy(desc->ext_compat_id, page, l);
1144
1145 if (desc->opts_mutex)
1146 mutex_unlock(desc->opts_mutex);
1147
1148 return len;
1149 }
1150
interf_grp_sub_compatible_id_show(struct config_item * item,char * page)1151 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1152 char *page)
1153 {
1154 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1155 return 8;
1156 }
1157
interf_grp_sub_compatible_id_store(struct config_item * item,const char * page,size_t len)1158 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1159 const char *page, size_t len)
1160 {
1161 struct usb_os_desc *desc = to_usb_os_desc(item);
1162 int l;
1163
1164 l = min_t(int, 8, len);
1165 if (page[l - 1] == '\n')
1166 --l;
1167 if (desc->opts_mutex)
1168 mutex_lock(desc->opts_mutex);
1169 memcpy(desc->ext_compat_id + 8, page, l);
1170
1171 if (desc->opts_mutex)
1172 mutex_unlock(desc->opts_mutex);
1173
1174 return len;
1175 }
1176
1177 CONFIGFS_ATTR(interf_grp_, compatible_id);
1178 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1179
1180 static struct configfs_attribute *interf_grp_attrs[] = {
1181 &interf_grp_attr_compatible_id,
1182 &interf_grp_attr_sub_compatible_id,
1183 NULL
1184 };
1185
usb_os_desc_prepare_interf_dir(struct config_group * parent,int n_interf,struct usb_os_desc ** desc,char ** names,struct module * owner)1186 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1187 int n_interf,
1188 struct usb_os_desc **desc,
1189 char **names,
1190 struct module *owner)
1191 {
1192 struct config_group **f_default_groups, *os_desc_group,
1193 **interface_groups;
1194 struct config_item_type *os_desc_type, *interface_type;
1195
1196 vla_group(data_chunk);
1197 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1198 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1199 vla_item(data_chunk, struct config_group *, interface_groups,
1200 n_interf + 1);
1201 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1202 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1203
1204 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1205 if (!vlabuf)
1206 return -ENOMEM;
1207
1208 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
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_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1212 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1213
1214 parent->default_groups = f_default_groups;
1215 os_desc_type->ct_owner = owner;
1216 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1217 f_default_groups[0] = os_desc_group;
1218
1219 os_desc_group->default_groups = interface_groups;
1220 interface_type->ct_group_ops = &interf_grp_ops;
1221 interface_type->ct_attrs = interf_grp_attrs;
1222 interface_type->ct_owner = owner;
1223
1224 while (n_interf--) {
1225 struct usb_os_desc *d;
1226
1227 d = desc[n_interf];
1228 d->owner = owner;
1229 config_group_init_type_name(&d->group, "", interface_type);
1230 config_item_set_name(&d->group.cg_item, "interface.%s",
1231 names[n_interf]);
1232 interface_groups[n_interf] = &d->group;
1233 }
1234
1235 return 0;
1236 }
1237 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1238
configfs_do_nothing(struct usb_composite_dev * cdev)1239 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1240 {
1241 WARN_ON(1);
1242 return -EINVAL;
1243 }
1244
1245 int composite_dev_prepare(struct usb_composite_driver *composite,
1246 struct usb_composite_dev *dev);
1247
1248 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1249 struct usb_ep *ep0);
1250
purge_configs_funcs(struct gadget_info * gi)1251 static void purge_configs_funcs(struct gadget_info *gi)
1252 {
1253 struct usb_configuration *c;
1254
1255 list_for_each_entry(c, &gi->cdev.configs, list) {
1256 struct usb_function *f, *tmp;
1257 struct config_usb_cfg *cfg;
1258
1259 cfg = container_of(c, struct config_usb_cfg, c);
1260
1261 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1262
1263 list_move(&f->list, &cfg->func_list);
1264 if (f->unbind) {
1265 dev_err(&gi->cdev.gadget->dev, "unbind function"
1266 " '%s'/%p\n", f->name, f);
1267 f->unbind(c, f);
1268 }
1269 }
1270 c->next_interface_id = 0;
1271 memset(c->interface, 0, sizeof(c->interface));
1272 c->superspeed = 0;
1273 c->highspeed = 0;
1274 c->fullspeed = 0;
1275 }
1276 }
1277
configfs_composite_bind(struct usb_gadget * gadget,struct usb_gadget_driver * gdriver)1278 static int configfs_composite_bind(struct usb_gadget *gadget,
1279 struct usb_gadget_driver *gdriver)
1280 {
1281 struct usb_composite_driver *composite = to_cdriver(gdriver);
1282 struct gadget_info *gi = container_of(composite,
1283 struct gadget_info, composite);
1284 struct usb_composite_dev *cdev = &gi->cdev;
1285 struct usb_configuration *c;
1286 struct usb_string *s;
1287 unsigned i;
1288 int ret;
1289
1290 /* the gi->lock is hold by the caller */
1291 gi->unbind = 0;
1292 cdev->gadget = gadget;
1293 set_gadget_data(gadget, cdev);
1294 ret = composite_dev_prepare(composite, cdev);
1295 if (ret)
1296 return ret;
1297 /* and now the gadget bind */
1298 ret = -EINVAL;
1299
1300 if (list_empty(&gi->cdev.configs)) {
1301 pr_err("Need at least one configuration in %s.\n",
1302 gi->composite.name);
1303 goto err_comp_cleanup;
1304 }
1305
1306
1307 list_for_each_entry(c, &gi->cdev.configs, list) {
1308 struct config_usb_cfg *cfg;
1309
1310 cfg = container_of(c, struct config_usb_cfg, c);
1311 if (list_empty(&cfg->func_list)) {
1312 pr_err("Config %s/%d of %s needs at least one function.\n",
1313 c->label, c->bConfigurationValue,
1314 gi->composite.name);
1315 goto err_comp_cleanup;
1316 }
1317 }
1318
1319 /* init all strings */
1320 if (!list_empty(&gi->string_list)) {
1321 struct gadget_strings *gs;
1322
1323 i = 0;
1324 list_for_each_entry(gs, &gi->string_list, list) {
1325
1326 gi->gstrings[i] = &gs->stringtab_dev;
1327 gs->stringtab_dev.strings = gs->strings;
1328 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1329 gs->manufacturer;
1330 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1331 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1332 i++;
1333 }
1334 gi->gstrings[i] = NULL;
1335 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1336 USB_GADGET_FIRST_AVAIL_IDX);
1337 if (IS_ERR(s)) {
1338 ret = PTR_ERR(s);
1339 goto err_comp_cleanup;
1340 }
1341
1342 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1343 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1344 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1345 }
1346
1347 if (gi->use_os_desc) {
1348 cdev->use_os_string = true;
1349 cdev->b_vendor_code = gi->b_vendor_code;
1350 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1351 }
1352
1353 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1354 struct usb_descriptor_header *usb_desc;
1355
1356 usb_desc = usb_otg_descriptor_alloc(gadget);
1357 if (!usb_desc) {
1358 ret = -ENOMEM;
1359 goto err_comp_cleanup;
1360 }
1361 usb_otg_descriptor_init(gadget, usb_desc);
1362 otg_desc[0] = usb_desc;
1363 otg_desc[1] = NULL;
1364 }
1365
1366 /* Go through all configs, attach all functions */
1367 list_for_each_entry(c, &gi->cdev.configs, list) {
1368 struct config_usb_cfg *cfg;
1369 struct usb_function *f;
1370 struct usb_function *tmp;
1371 struct gadget_config_name *cn;
1372
1373 if (gadget_is_otg(gadget))
1374 c->descriptors = otg_desc;
1375
1376 cfg = container_of(c, struct config_usb_cfg, c);
1377 if (!list_empty(&cfg->string_list)) {
1378 i = 0;
1379 list_for_each_entry(cn, &cfg->string_list, list) {
1380 cfg->gstrings[i] = &cn->stringtab_dev;
1381 cn->stringtab_dev.strings = &cn->strings;
1382 cn->strings.s = cn->configuration;
1383 i++;
1384 }
1385 cfg->gstrings[i] = NULL;
1386 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1387 if (IS_ERR(s)) {
1388 ret = PTR_ERR(s);
1389 goto err_comp_cleanup;
1390 }
1391 c->iConfiguration = s[0].id;
1392 }
1393
1394 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1395 list_del(&f->list);
1396 ret = usb_add_function(c, f);
1397 if (ret) {
1398 list_add(&f->list, &cfg->func_list);
1399 goto err_purge_funcs;
1400 }
1401 }
1402 usb_ep_autoconfig_reset(cdev->gadget);
1403 }
1404 if (cdev->use_os_string) {
1405 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1406 if (ret)
1407 goto err_purge_funcs;
1408 }
1409
1410 usb_ep_autoconfig_reset(cdev->gadget);
1411 return 0;
1412
1413 err_purge_funcs:
1414 purge_configs_funcs(gi);
1415 err_comp_cleanup:
1416 composite_dev_cleanup(cdev);
1417 return ret;
1418 }
1419
1420 #ifdef CONFIG_USB_CONFIGFS_UEVENT
android_work(struct work_struct * data)1421 static void android_work(struct work_struct *data)
1422 {
1423 struct gadget_info *gi = container_of(data, struct gadget_info, work);
1424 struct usb_composite_dev *cdev = &gi->cdev;
1425 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
1426 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
1427 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
1428 /* 0-connected 1-configured 2-disconnected*/
1429 bool status[3] = { false, false, false };
1430 unsigned long flags;
1431 bool uevent_sent = false;
1432
1433 spin_lock_irqsave(&cdev->lock, flags);
1434 if (cdev->config)
1435 status[1] = true;
1436
1437 if (gi->connected != gi->sw_connected) {
1438 if (gi->connected)
1439 status[0] = true;
1440 else
1441 status[2] = true;
1442 gi->sw_connected = gi->connected;
1443 }
1444 spin_unlock_irqrestore(&cdev->lock, flags);
1445
1446 if (status[0]) {
1447 kobject_uevent_env(&android_device->kobj,
1448 KOBJ_CHANGE, connected);
1449 pr_info("%s: sent uevent %s\n", __func__, connected[0]);
1450 uevent_sent = true;
1451 }
1452
1453 if (status[1]) {
1454 kobject_uevent_env(&android_device->kobj,
1455 KOBJ_CHANGE, configured);
1456 pr_info("%s: sent uevent %s\n", __func__, configured[0]);
1457 uevent_sent = true;
1458 }
1459
1460 if (status[2]) {
1461 kobject_uevent_env(&android_device->kobj,
1462 KOBJ_CHANGE, disconnected);
1463 pr_info("%s: sent uevent %s\n", __func__, disconnected[0]);
1464 uevent_sent = true;
1465 }
1466
1467 if (!uevent_sent) {
1468 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
1469 gi->connected, gi->sw_connected, cdev->config);
1470 }
1471 }
1472 #endif
1473
configfs_composite_unbind(struct usb_gadget * gadget)1474 static void configfs_composite_unbind(struct usb_gadget *gadget)
1475 {
1476 struct usb_composite_dev *cdev;
1477 struct gadget_info *gi;
1478 unsigned long flags;
1479
1480 /* the gi->lock is hold by the caller */
1481
1482 cdev = get_gadget_data(gadget);
1483 gi = container_of(cdev, struct gadget_info, cdev);
1484 spin_lock_irqsave(&gi->spinlock, flags);
1485 gi->unbind = 1;
1486 spin_unlock_irqrestore(&gi->spinlock, flags);
1487
1488 kfree(otg_desc[0]);
1489 otg_desc[0] = NULL;
1490 purge_configs_funcs(gi);
1491 composite_dev_cleanup(cdev);
1492 usb_ep_autoconfig_reset(cdev->gadget);
1493 spin_lock_irqsave(&gi->spinlock, flags);
1494 cdev->gadget = NULL;
1495 set_gadget_data(gadget, NULL);
1496 spin_unlock_irqrestore(&gi->spinlock, flags);
1497 }
1498
configfs_composite_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)1499 static int configfs_composite_setup(struct usb_gadget *gadget,
1500 const struct usb_ctrlrequest *ctrl)
1501 {
1502 struct usb_composite_dev *cdev;
1503 struct gadget_info *gi;
1504 unsigned long flags;
1505 int ret;
1506
1507 cdev = get_gadget_data(gadget);
1508 if (!cdev)
1509 return 0;
1510
1511 gi = container_of(cdev, struct gadget_info, cdev);
1512 spin_lock_irqsave(&gi->spinlock, flags);
1513 cdev = get_gadget_data(gadget);
1514 if (!cdev || gi->unbind) {
1515 spin_unlock_irqrestore(&gi->spinlock, flags);
1516 return 0;
1517 }
1518
1519 ret = composite_setup(gadget, ctrl);
1520 spin_unlock_irqrestore(&gi->spinlock, flags);
1521 return ret;
1522 }
1523
configfs_composite_disconnect(struct usb_gadget * gadget)1524 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1525 {
1526 struct usb_composite_dev *cdev;
1527 struct gadget_info *gi;
1528 unsigned long flags;
1529
1530 cdev = get_gadget_data(gadget);
1531 if (!cdev)
1532 return;
1533
1534 gi = container_of(cdev, struct gadget_info, cdev);
1535 spin_lock_irqsave(&gi->spinlock, flags);
1536 cdev = get_gadget_data(gadget);
1537 if (!cdev || gi->unbind) {
1538 spin_unlock_irqrestore(&gi->spinlock, flags);
1539 return;
1540 }
1541
1542 composite_disconnect(gadget);
1543 spin_unlock_irqrestore(&gi->spinlock, flags);
1544 }
1545
configfs_composite_suspend(struct usb_gadget * gadget)1546 static void configfs_composite_suspend(struct usb_gadget *gadget)
1547 {
1548 struct usb_composite_dev *cdev;
1549 struct gadget_info *gi;
1550 unsigned long flags;
1551
1552 cdev = get_gadget_data(gadget);
1553 if (!cdev)
1554 return;
1555
1556 gi = container_of(cdev, struct gadget_info, cdev);
1557 spin_lock_irqsave(&gi->spinlock, flags);
1558 cdev = get_gadget_data(gadget);
1559 if (!cdev || gi->unbind) {
1560 spin_unlock_irqrestore(&gi->spinlock, flags);
1561 return;
1562 }
1563
1564 composite_suspend(gadget);
1565 spin_unlock_irqrestore(&gi->spinlock, flags);
1566 }
1567
configfs_composite_resume(struct usb_gadget * gadget)1568 static void configfs_composite_resume(struct usb_gadget *gadget)
1569 {
1570 struct usb_composite_dev *cdev;
1571 struct gadget_info *gi;
1572 unsigned long flags;
1573
1574 cdev = get_gadget_data(gadget);
1575 if (!cdev)
1576 return;
1577
1578 gi = container_of(cdev, struct gadget_info, cdev);
1579 spin_lock_irqsave(&gi->spinlock, flags);
1580 cdev = get_gadget_data(gadget);
1581 if (!cdev || gi->unbind) {
1582 spin_unlock_irqrestore(&gi->spinlock, flags);
1583 return;
1584 }
1585
1586 composite_resume(gadget);
1587 spin_unlock_irqrestore(&gi->spinlock, flags);
1588 }
1589
1590 #ifdef CONFIG_USB_CONFIGFS_UEVENT
android_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * c)1591 static int android_setup(struct usb_gadget *gadget,
1592 const struct usb_ctrlrequest *c)
1593 {
1594 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1595 unsigned long flags;
1596 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1597 int value = -EOPNOTSUPP;
1598 struct usb_function_instance *fi;
1599
1600 spin_lock_irqsave(&cdev->lock, flags);
1601 if (!gi->connected) {
1602 gi->connected = 1;
1603 schedule_work(&gi->work);
1604 }
1605 spin_unlock_irqrestore(&cdev->lock, flags);
1606 list_for_each_entry(fi, &gi->available_func, cfs_list) {
1607 if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) {
1608 value = fi->f->setup(fi->f, c);
1609 if (value >= 0)
1610 break;
1611 }
1612 }
1613
1614 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1615 if (value < 0)
1616 value = acc_ctrlrequest(cdev, c);
1617 #endif
1618
1619 if (value < 0)
1620 value = composite_setup(gadget, c);
1621
1622 spin_lock_irqsave(&cdev->lock, flags);
1623 if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1624 cdev->config) {
1625 schedule_work(&gi->work);
1626 }
1627 spin_unlock_irqrestore(&cdev->lock, flags);
1628
1629 return value;
1630 }
1631
android_disconnect(struct usb_gadget * gadget)1632 static void android_disconnect(struct usb_gadget *gadget)
1633 {
1634 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1635 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1636
1637 /* FIXME: There's a race between usb_gadget_udc_stop() which is likely
1638 * to set the gadget driver to NULL in the udc driver and this drivers
1639 * gadget disconnect fn which likely checks for the gadget driver to
1640 * be a null ptr. It happens that unbind (doing set_gadget_data(NULL))
1641 * is called before the gadget driver is set to NULL and the udc driver
1642 * calls disconnect fn which results in cdev being a null ptr.
1643 */
1644 if (cdev == NULL) {
1645 WARN(1, "%s: gadget driver already disconnected\n", __func__);
1646 return;
1647 }
1648
1649 /* accessory HID support can be active while the
1650 accessory function is not actually enabled,
1651 so we need to inform it when we are disconnected.
1652 */
1653
1654 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1655 acc_disconnect();
1656 #endif
1657 gi->connected = 0;
1658 schedule_work(&gi->work);
1659 composite_disconnect(gadget);
1660 }
1661 #endif
1662
1663 static const struct usb_gadget_driver configfs_driver_template = {
1664 .bind = configfs_composite_bind,
1665 .unbind = configfs_composite_unbind,
1666 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1667 .setup = android_setup,
1668 .reset = android_disconnect,
1669 .disconnect = android_disconnect,
1670 #else
1671 .setup = configfs_composite_setup,
1672 .reset = configfs_composite_disconnect,
1673 .disconnect = configfs_composite_disconnect,
1674 #endif
1675 .suspend = configfs_composite_suspend,
1676 .resume = configfs_composite_resume,
1677
1678 .max_speed = USB_SPEED_SUPER_PLUS,
1679 .driver = {
1680 .owner = THIS_MODULE,
1681 .name = "configfs-gadget",
1682 },
1683 };
1684
1685 #ifdef CONFIG_USB_CONFIGFS_UEVENT
state_show(struct device * pdev,struct device_attribute * attr,char * buf)1686 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1687 char *buf)
1688 {
1689 struct gadget_info *dev = dev_get_drvdata(pdev);
1690 struct usb_composite_dev *cdev;
1691 char *state = "DISCONNECTED";
1692 unsigned long flags;
1693
1694 if (!dev)
1695 goto out;
1696
1697 cdev = &dev->cdev;
1698
1699 if (!cdev)
1700 goto out;
1701
1702 spin_lock_irqsave(&cdev->lock, flags);
1703 if (cdev->config)
1704 state = "CONFIGURED";
1705 else if (dev->connected)
1706 state = "CONNECTED";
1707 spin_unlock_irqrestore(&cdev->lock, flags);
1708 out:
1709 return sprintf(buf, "%s\n", state);
1710 }
1711
1712 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1713
1714 static struct device_attribute *android_usb_attributes[] = {
1715 &dev_attr_state,
1716 NULL
1717 };
1718
android_device_create(struct gadget_info * gi)1719 static int android_device_create(struct gadget_info *gi)
1720 {
1721 struct device_attribute **attrs;
1722 struct device_attribute *attr;
1723
1724 INIT_WORK(&gi->work, android_work);
1725 android_device = device_create(android_class, NULL,
1726 MKDEV(0, 0), NULL, "android0");
1727 if (IS_ERR(android_device))
1728 return PTR_ERR(android_device);
1729
1730 dev_set_drvdata(android_device, gi);
1731
1732 attrs = android_usb_attributes;
1733 while ((attr = *attrs++)) {
1734 int err;
1735
1736 err = device_create_file(android_device, attr);
1737 if (err) {
1738 device_destroy(android_device->class,
1739 android_device->devt);
1740 return err;
1741 }
1742 }
1743
1744 return 0;
1745 }
1746
android_device_destroy(void)1747 static void android_device_destroy(void)
1748 {
1749 struct device_attribute **attrs;
1750 struct device_attribute *attr;
1751
1752 attrs = android_usb_attributes;
1753 while ((attr = *attrs++))
1754 device_remove_file(android_device, attr);
1755 device_destroy(android_device->class, android_device->devt);
1756 }
1757 #else
android_device_create(struct gadget_info * gi)1758 static inline int android_device_create(struct gadget_info *gi)
1759 {
1760 return 0;
1761 }
1762
android_device_destroy(void)1763 static inline void android_device_destroy(void)
1764 {
1765 }
1766 #endif
1767
gadgets_make(struct config_group * group,const char * name)1768 static struct config_group *gadgets_make(
1769 struct config_group *group,
1770 const char *name)
1771 {
1772 struct gadget_info *gi;
1773
1774 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1775 if (!gi)
1776 return ERR_PTR(-ENOMEM);
1777 gi->group.default_groups = gi->default_groups;
1778 gi->group.default_groups[0] = &gi->functions_group;
1779 gi->group.default_groups[1] = &gi->configs_group;
1780 gi->group.default_groups[2] = &gi->strings_group;
1781 gi->group.default_groups[3] = &gi->os_desc_group;
1782
1783 config_group_init_type_name(&gi->functions_group, "functions",
1784 &functions_type);
1785 config_group_init_type_name(&gi->configs_group, "configs",
1786 &config_desc_type);
1787 config_group_init_type_name(&gi->strings_group, "strings",
1788 &gadget_strings_strings_type);
1789 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1790 &os_desc_type);
1791
1792 gi->composite.bind = configfs_do_nothing;
1793 gi->composite.unbind = configfs_do_nothing;
1794 gi->composite.suspend = NULL;
1795 gi->composite.resume = NULL;
1796 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1797
1798 spin_lock_init(&gi->spinlock);
1799 mutex_init(&gi->lock);
1800 INIT_LIST_HEAD(&gi->string_list);
1801 INIT_LIST_HEAD(&gi->available_func);
1802
1803 composite_init_dev(&gi->cdev);
1804 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1805 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1806 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1807
1808 gi->composite.gadget_driver = configfs_driver_template;
1809
1810 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1811 gi->composite.name = gi->composite.gadget_driver.function;
1812
1813 if (!gi->composite.gadget_driver.function)
1814 goto err;
1815
1816 if (android_device_create(gi) < 0)
1817 goto err;
1818
1819 config_group_init_type_name(&gi->group, name,
1820 &gadget_root_type);
1821 return &gi->group;
1822
1823 err:
1824 kfree(gi);
1825 return ERR_PTR(-ENOMEM);
1826 }
1827
gadgets_drop(struct config_group * group,struct config_item * item)1828 static void gadgets_drop(struct config_group *group, struct config_item *item)
1829 {
1830 config_item_put(item);
1831 android_device_destroy();
1832 }
1833
1834 static struct configfs_group_operations gadgets_ops = {
1835 .make_group = &gadgets_make,
1836 .drop_item = &gadgets_drop,
1837 };
1838
1839 static struct config_item_type gadgets_type = {
1840 .ct_group_ops = &gadgets_ops,
1841 .ct_owner = THIS_MODULE,
1842 };
1843
1844 static struct configfs_subsystem gadget_subsys = {
1845 .su_group = {
1846 .cg_item = {
1847 .ci_namebuf = "usb_gadget",
1848 .ci_type = &gadgets_type,
1849 },
1850 },
1851 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1852 };
1853
unregister_gadget_item(struct config_item * item)1854 void unregister_gadget_item(struct config_item *item)
1855 {
1856 struct gadget_info *gi = to_gadget_info(item);
1857
1858 mutex_lock(&gi->lock);
1859 unregister_gadget(gi);
1860 mutex_unlock(&gi->lock);
1861 }
1862 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1863
gadget_cfs_init(void)1864 static int __init gadget_cfs_init(void)
1865 {
1866 int ret;
1867
1868 config_group_init(&gadget_subsys.su_group);
1869
1870 ret = configfs_register_subsystem(&gadget_subsys);
1871
1872 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1873 android_class = class_create(THIS_MODULE, "android_usb");
1874 if (IS_ERR(android_class))
1875 return PTR_ERR(android_class);
1876 #endif
1877
1878 return ret;
1879 }
1880 module_init(gadget_cfs_init);
1881
gadget_cfs_exit(void)1882 static void __exit gadget_cfs_exit(void)
1883 {
1884 configfs_unregister_subsystem(&gadget_subsys);
1885 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1886 if (!IS_ERR(android_class))
1887 class_destroy(android_class);
1888 #endif
1889
1890 }
1891 module_exit(gadget_cfs_exit);
1892