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