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