blob: 32b637e3e1fa2b5bdccb0ba9f001a96246f751f5 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01002#include <linux/configfs.h>
3#include <linux/module.h>
4#include <linux/slab.h>
5#include <linux/device.h>
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02006#include <linux/nls.h>
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01007#include <linux/usb/composite.h>
8#include <linux/usb/gadget_configfs.h>
Rashika Kheria0009e992013-12-19 15:37:37 +05309#include "configfs.h"
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +020010#include "u_f.h"
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +020011#include "u_os_desc.h"
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010012
13int check_user_usb_string(const char *name,
14 struct usb_gadget_strings *stringtab_dev)
15{
16 unsigned primary_lang;
17 unsigned sub_lang;
18 u16 num;
19 int ret;
20
21 ret = kstrtou16(name, 0, &num);
22 if (ret)
23 return ret;
24
25 primary_lang = num & 0x3ff;
26 sub_lang = num >> 10;
27
28 /* simple sanity check for valid langid */
29 switch (primary_lang) {
30 case 0:
31 case 0x62 ... 0xfe:
32 case 0x100 ... 0x3ff:
33 return -EINVAL;
34 }
35 if (!sub_lang)
36 return -EINVAL;
37
38 stringtab_dev->language = num;
39 return 0;
40}
41
42#define MAX_NAME_LEN 40
43#define MAX_USB_STRING_LANGS 2
44
Li Jun41ce84c2015-07-09 15:18:48 +080045static const struct usb_descriptor_header *otg_desc[2];
46
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010047struct gadget_info {
48 struct config_group group;
49 struct config_group functions_group;
50 struct config_group configs_group;
51 struct config_group strings_group;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020052 struct config_group os_desc_group;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010053
54 struct mutex lock;
55 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 struct list_head string_list;
57 struct list_head available_func;
58
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010059 struct usb_composite_driver composite;
60 struct usb_composite_dev cdev;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020061 bool use_os_desc;
62 char b_vendor_code;
63 char qw_sign[OS_STRING_QW_SIGN_LEN];
Peter Chen1a1c851b2019-08-26 15:10:55 -040064 spinlock_t spinlock;
65 bool unbind;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010066};
67
Christoph Hellwig45b6a732015-10-03 15:32:38 +020068static inline struct gadget_info *to_gadget_info(struct config_item *item)
69{
70 return container_of(to_config_group(item), struct gadget_info, group);
71}
72
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010073struct config_usb_cfg {
74 struct config_group group;
75 struct config_group strings_group;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010076 struct list_head string_list;
77 struct usb_configuration c;
78 struct list_head func_list;
79 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
80};
81
Christoph Hellwig45b6a732015-10-03 15:32:38 +020082static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
83{
84 return container_of(to_config_group(item), struct config_usb_cfg,
85 group);
86}
87
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010088struct gadget_strings {
89 struct usb_gadget_strings stringtab_dev;
90 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
91 char *manufacturer;
92 char *product;
93 char *serialnumber;
94
95 struct config_group group;
96 struct list_head list;
97};
98
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020099struct os_desc {
100 struct config_group group;
101};
102
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100103struct gadget_config_name {
104 struct usb_gadget_strings stringtab_dev;
105 struct usb_string strings;
106 char *configuration;
107
108 struct config_group group;
109 struct list_head list;
110};
111
112static int usb_string_copy(const char *s, char **s_copy)
113{
114 int ret;
115 char *str;
116 char *copy = *s_copy;
117 ret = strlen(s);
118 if (ret > 126)
119 return -EOVERFLOW;
120
121 str = kstrdup(s, GFP_KERNEL);
122 if (!str)
123 return -ENOMEM;
124 if (str[ret - 1] == '\n')
125 str[ret - 1] = '\0';
126 kfree(copy);
127 *s_copy = str;
128 return 0;
129}
130
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100131#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200132static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100133 char *page) \
134{ \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200135 return sprintf(page, "0x%02x\n", \
136 to_gadget_info(item)->cdev.desc.__name); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100137}
138
139#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200140static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100141 char *page) \
142{ \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200143 return sprintf(page, "0x%04x\n", \
144 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100145}
146
147
148#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200149static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100150 const char *page, size_t len) \
151{ \
152 u8 val; \
153 int ret; \
154 ret = kstrtou8(page, 0, &val); \
155 if (ret) \
156 return ret; \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200157 to_gadget_info(item)->cdev.desc._name = val; \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100158 return len; \
159}
160
161#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200162static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100163 const char *page, size_t len) \
164{ \
165 u16 val; \
166 int ret; \
167 ret = kstrtou16(page, 0, &val); \
168 if (ret) \
169 return ret; \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200170 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100171 return len; \
172}
173
174#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
175 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
176 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
177
178GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
179GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
180GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
181GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
182GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
183GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
184GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
185GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
186
187static ssize_t is_valid_bcd(u16 bcd_val)
188{
189 if ((bcd_val & 0xf) > 9)
190 return -EINVAL;
191 if (((bcd_val >> 4) & 0xf) > 9)
192 return -EINVAL;
193 if (((bcd_val >> 8) & 0xf) > 9)
194 return -EINVAL;
195 if (((bcd_val >> 12) & 0xf) > 9)
196 return -EINVAL;
197 return 0;
198}
199
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200200static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100201 const char *page, size_t len)
202{
203 u16 bcdDevice;
204 int ret;
205
206 ret = kstrtou16(page, 0, &bcdDevice);
207 if (ret)
208 return ret;
209 ret = is_valid_bcd(bcdDevice);
210 if (ret)
211 return ret;
212
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200213 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100214 return len;
215}
216
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200217static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100218 const char *page, size_t len)
219{
220 u16 bcdUSB;
221 int ret;
222
223 ret = kstrtou16(page, 0, &bcdUSB);
224 if (ret)
225 return ret;
226 ret = is_valid_bcd(bcdUSB);
227 if (ret)
228 return ret;
229
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200230 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100231 return len;
232}
233
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200234static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100235{
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100236 char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
237
238 return sprintf(page, "%s\n", udc_name ?: "");
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100239}
240
241static int unregister_gadget(struct gadget_info *gi)
242{
243 int ret;
244
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100245 if (!gi->composite.gadget_driver.udc_name)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100246 return -ENODEV;
247
248 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
249 if (ret)
250 return ret;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100251 kfree(gi->composite.gadget_driver.udc_name);
252 gi->composite.gadget_driver.udc_name = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100253 return 0;
254}
255
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200256static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100257 const char *page, size_t len)
258{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200259 struct gadget_info *gi = to_gadget_info(item);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100260 char *name;
261 int ret;
262
263 name = kstrdup(page, GFP_KERNEL);
264 if (!name)
265 return -ENOMEM;
266 if (name[len - 1] == '\n')
267 name[len - 1] = '\0';
268
269 mutex_lock(&gi->lock);
270
271 if (!strlen(name)) {
272 ret = unregister_gadget(gi);
273 if (ret)
274 goto err;
John Keeping38355b22017-02-28 10:55:30 +0000275 kfree(name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100276 } else {
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100277 if (gi->composite.gadget_driver.udc_name) {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100278 ret = -EBUSY;
279 goto err;
280 }
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100281 gi->composite.gadget_driver.udc_name = name;
282 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
283 if (ret) {
284 gi->composite.gadget_driver.udc_name = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100285 goto err;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100286 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100287 }
288 mutex_unlock(&gi->lock);
289 return len;
290err:
291 kfree(name);
292 mutex_unlock(&gi->lock);
293 return ret;
294}
295
Thinh Nguyena0249702020-01-08 18:38:10 -0800296static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
297 char *page)
298{
299 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
300
301 return sprintf(page, "%s\n", usb_speed_string(speed));
302}
303
304static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
305 const char *page, size_t len)
306{
307 struct gadget_info *gi = to_gadget_info(item);
308
309 mutex_lock(&gi->lock);
310
311 /* Prevent changing of max_speed after the driver is binded */
312 if (gi->composite.gadget_driver.udc_name)
313 goto err;
314
315 if (strncmp(page, "super-speed-plus", 16) == 0)
316 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
317 else if (strncmp(page, "super-speed", 11) == 0)
318 gi->composite.max_speed = USB_SPEED_SUPER;
319 else if (strncmp(page, "high-speed", 10) == 0)
320 gi->composite.max_speed = USB_SPEED_HIGH;
321 else if (strncmp(page, "full-speed", 10) == 0)
322 gi->composite.max_speed = USB_SPEED_FULL;
323 else if (strncmp(page, "low-speed", 9) == 0)
324 gi->composite.max_speed = USB_SPEED_LOW;
325 else
326 goto err;
327
328 gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
329
330 mutex_unlock(&gi->lock);
331 return len;
332err:
333 mutex_unlock(&gi->lock);
334 return -EINVAL;
335}
336
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200337CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
338CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
339CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
340CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
341CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
342CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
343CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
344CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
345CONFIGFS_ATTR(gadget_dev_desc_, UDC);
Thinh Nguyena0249702020-01-08 18:38:10 -0800346CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100347
348static struct configfs_attribute *gadget_root_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200349 &gadget_dev_desc_attr_bDeviceClass,
350 &gadget_dev_desc_attr_bDeviceSubClass,
351 &gadget_dev_desc_attr_bDeviceProtocol,
352 &gadget_dev_desc_attr_bMaxPacketSize0,
353 &gadget_dev_desc_attr_idVendor,
354 &gadget_dev_desc_attr_idProduct,
355 &gadget_dev_desc_attr_bcdDevice,
356 &gadget_dev_desc_attr_bcdUSB,
357 &gadget_dev_desc_attr_UDC,
Thinh Nguyena0249702020-01-08 18:38:10 -0800358 &gadget_dev_desc_attr_max_speed,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100359 NULL,
360};
361
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100362static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
363{
364 return container_of(to_config_group(item), struct gadget_strings,
365 group);
366}
367
368static inline struct gadget_config_name *to_gadget_config_name(
369 struct config_item *item)
370{
371 return container_of(to_config_group(item), struct gadget_config_name,
372 group);
373}
374
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100375static inline struct usb_function_instance *to_usb_function_instance(
376 struct config_item *item)
377{
378 return container_of(to_config_group(item),
379 struct usb_function_instance, group);
380}
381
382static void gadget_info_attr_release(struct config_item *item)
383{
384 struct gadget_info *gi = to_gadget_info(item);
385
386 WARN_ON(!list_empty(&gi->cdev.configs));
387 WARN_ON(!list_empty(&gi->string_list));
388 WARN_ON(!list_empty(&gi->available_func));
389 kfree(gi->composite.gadget_driver.function);
390 kfree(gi);
391}
392
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100393static struct configfs_item_operations gadget_root_item_ops = {
394 .release = gadget_info_attr_release,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100395};
396
397static void gadget_config_attr_release(struct config_item *item)
398{
399 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
400
401 WARN_ON(!list_empty(&cfg->c.functions));
402 list_del(&cfg->c.list);
403 kfree(cfg->c.label);
404 kfree(cfg);
405}
406
407static int config_usb_cfg_link(
408 struct config_item *usb_cfg_ci,
409 struct config_item *usb_func_ci)
410{
411 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
412 struct usb_composite_dev *cdev = cfg->c.cdev;
413 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
414
415 struct config_group *group = to_config_group(usb_func_ci);
416 struct usb_function_instance *fi = container_of(group,
417 struct usb_function_instance, group);
418 struct usb_function_instance *a_fi;
419 struct usb_function *f;
420 int ret;
421
422 mutex_lock(&gi->lock);
423 /*
424 * Make sure this function is from within our _this_ gadget and not
425 * from another gadget or a random directory.
426 * Also a function instance can only be linked once.
427 */
428 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
429 if (a_fi == fi)
430 break;
431 }
432 if (a_fi != fi) {
433 ret = -EINVAL;
434 goto out;
435 }
436
437 list_for_each_entry(f, &cfg->func_list, list) {
438 if (f->fi == fi) {
439 ret = -EEXIST;
440 goto out;
441 }
442 }
443
444 f = usb_get_function(fi);
445 if (IS_ERR(f)) {
446 ret = PTR_ERR(f);
447 goto out;
448 }
449
450 /* stash the function until we bind it to the gadget */
451 list_add_tail(&f->list, &cfg->func_list);
452 ret = 0;
453out:
454 mutex_unlock(&gi->lock);
455 return ret;
456}
457
Andrzej Pietrasiewicze16769d2016-11-28 13:22:42 +0100458static void config_usb_cfg_unlink(
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100459 struct config_item *usb_cfg_ci,
460 struct config_item *usb_func_ci)
461{
462 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
463 struct usb_composite_dev *cdev = cfg->c.cdev;
464 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
465
466 struct config_group *group = to_config_group(usb_func_ci);
467 struct usb_function_instance *fi = container_of(group,
468 struct usb_function_instance, group);
469 struct usb_function *f;
470
471 /*
472 * ideally I would like to forbid to unlink functions while a gadget is
473 * bound to an UDC. Since this isn't possible at the moment, we simply
474 * force an unbind, the function is available here and then we can
475 * remove the function.
476 */
477 mutex_lock(&gi->lock);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100478 if (gi->composite.gadget_driver.udc_name)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100479 unregister_gadget(gi);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100480 WARN_ON(gi->composite.gadget_driver.udc_name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100481
482 list_for_each_entry(f, &cfg->func_list, list) {
483 if (f->fi == fi) {
484 list_del(&f->list);
485 usb_put_function(f);
486 mutex_unlock(&gi->lock);
Andrzej Pietrasiewicze16769d2016-11-28 13:22:42 +0100487 return;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100488 }
489 }
490 mutex_unlock(&gi->lock);
David Rientjes75bfe232013-04-07 14:11:47 -0700491 WARN(1, "Unable to locate function to unbind\n");
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100492}
493
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100494static struct configfs_item_operations gadget_config_item_ops = {
495 .release = gadget_config_attr_release,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100496 .allow_link = config_usb_cfg_link,
497 .drop_link = config_usb_cfg_unlink,
498};
499
500
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200501static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100502 char *page)
503{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200504 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100505}
506
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200507static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100508 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;
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200517 to_config_usb_cfg(item)->c.MaxPower = val;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100518 return len;
519}
520
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200521static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100522 char *page)
523{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200524 return sprintf(page, "0x%02x\n",
525 to_config_usb_cfg(item)->c.bmAttributes);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100526}
527
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200528static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100529 const char *page, size_t len)
530{
531 u8 val;
532 int ret;
533 ret = kstrtou8(page, 0, &val);
534 if (ret)
535 return ret;
536 if (!(val & USB_CONFIG_ATT_ONE))
537 return -EINVAL;
538 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
539 USB_CONFIG_ATT_WAKEUP))
540 return -EINVAL;
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200541 to_config_usb_cfg(item)->c.bmAttributes = val;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100542 return len;
543}
544
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200545CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
546CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100547
548static struct configfs_attribute *gadget_config_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200549 &gadget_config_desc_attr_MaxPower,
550 &gadget_config_desc_attr_bmAttributes,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100551 NULL,
552};
553
Bhumika Goyal4ad01412017-10-16 17:18:46 +0200554static const struct config_item_type gadget_config_type = {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100555 .ct_item_ops = &gadget_config_item_ops,
556 .ct_attrs = gadget_config_attrs,
557 .ct_owner = THIS_MODULE,
558};
559
Bhumika Goyal4ad01412017-10-16 17:18:46 +0200560static const struct config_item_type gadget_root_type = {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100561 .ct_item_ops = &gadget_root_item_ops,
562 .ct_attrs = gadget_root_attrs,
563 .ct_owner = THIS_MODULE,
564};
565
566static void composite_init_dev(struct usb_composite_dev *cdev)
567{
568 spin_lock_init(&cdev->lock);
569 INIT_LIST_HEAD(&cdev->configs);
570 INIT_LIST_HEAD(&cdev->gstrings);
571}
572
573static struct config_group *function_make(
574 struct config_group *group,
575 const char *name)
576{
577 struct gadget_info *gi;
578 struct usb_function_instance *fi;
579 char buf[MAX_NAME_LEN];
580 char *func_name;
581 char *instance_name;
582 int ret;
583
584 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
585 if (ret >= MAX_NAME_LEN)
586 return ERR_PTR(-ENAMETOOLONG);
587
588 func_name = buf;
589 instance_name = strchr(func_name, '.');
590 if (!instance_name) {
591 pr_err("Unable to locate . in FUNC.INSTANCE\n");
592 return ERR_PTR(-EINVAL);
593 }
594 *instance_name = '\0';
595 instance_name++;
596
597 fi = usb_get_function_instance(func_name);
598 if (IS_ERR(fi))
Duan Jionga3469412013-09-26 15:55:25 +0800599 return ERR_CAST(fi);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100600
Nicolas Iooss3958b792015-07-17 16:23:45 -0700601 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100602 if (ret) {
603 usb_put_function_instance(fi);
604 return ERR_PTR(ret);
605 }
Andrzej Pietrasiewicz19338612013-12-03 15:15:21 +0100606 if (fi->set_inst_name) {
607 ret = fi->set_inst_name(fi, instance_name);
608 if (ret) {
609 usb_put_function_instance(fi);
610 return ERR_PTR(ret);
611 }
612 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100613
614 gi = container_of(group, struct gadget_info, functions_group);
615
616 mutex_lock(&gi->lock);
617 list_add_tail(&fi->cfs_list, &gi->available_func);
618 mutex_unlock(&gi->lock);
619 return &fi->group;
620}
621
622static void function_drop(
623 struct config_group *group,
624 struct config_item *item)
625{
626 struct usb_function_instance *fi = to_usb_function_instance(item);
627 struct gadget_info *gi;
628
629 gi = container_of(group, struct gadget_info, functions_group);
630
631 mutex_lock(&gi->lock);
632 list_del(&fi->cfs_list);
633 mutex_unlock(&gi->lock);
634 config_item_put(item);
635}
636
637static struct configfs_group_operations functions_ops = {
638 .make_group = &function_make,
639 .drop_item = &function_drop,
640};
641
Bhumika Goyal4ad01412017-10-16 17:18:46 +0200642static const struct config_item_type functions_type = {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100643 .ct_group_ops = &functions_ops,
644 .ct_owner = THIS_MODULE,
645};
646
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100647GS_STRINGS_RW(gadget_config_name, configuration);
648
649static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200650 &gadget_config_name_attr_configuration,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100651 NULL,
652};
653
654static void gadget_config_name_attr_release(struct config_item *item)
655{
656 struct gadget_config_name *cn = to_gadget_config_name(item);
657
658 kfree(cn->configuration);
659
660 list_del(&cn->list);
661 kfree(cn);
662}
663
664USB_CONFIG_STRING_RW_OPS(gadget_config_name);
665USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
666
667static struct config_group *config_desc_make(
668 struct config_group *group,
669 const char *name)
670{
671 struct gadget_info *gi;
672 struct config_usb_cfg *cfg;
673 char buf[MAX_NAME_LEN];
674 char *num_str;
675 u8 num;
676 int ret;
677
678 gi = container_of(group, struct gadget_info, configs_group);
679 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
680 if (ret >= MAX_NAME_LEN)
681 return ERR_PTR(-ENAMETOOLONG);
682
683 num_str = strchr(buf, '.');
684 if (!num_str) {
685 pr_err("Unable to locate . in name.bConfigurationValue\n");
686 return ERR_PTR(-EINVAL);
687 }
688
689 *num_str = '\0';
690 num_str++;
691
692 if (!strlen(buf))
693 return ERR_PTR(-EINVAL);
694
695 ret = kstrtou8(num_str, 0, &num);
696 if (ret)
697 return ERR_PTR(ret);
698
699 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
700 if (!cfg)
701 return ERR_PTR(-ENOMEM);
702 cfg->c.label = kstrdup(buf, GFP_KERNEL);
703 if (!cfg->c.label) {
704 ret = -ENOMEM;
705 goto err;
706 }
707 cfg->c.bConfigurationValue = num;
708 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
709 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
710 INIT_LIST_HEAD(&cfg->string_list);
711 INIT_LIST_HEAD(&cfg->func_list);
712
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100713 config_group_init_type_name(&cfg->group, name,
714 &gadget_config_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100715
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100716 config_group_init_type_name(&cfg->strings_group, "strings",
717 &gadget_config_name_strings_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100718 configfs_add_default_group(&cfg->strings_group, &cfg->group);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100719
720 ret = usb_add_config_only(&gi->cdev, &cfg->c);
721 if (ret)
722 goto err;
723
724 return &cfg->group;
725err:
726 kfree(cfg->c.label);
727 kfree(cfg);
728 return ERR_PTR(ret);
729}
730
731static void config_desc_drop(
732 struct config_group *group,
733 struct config_item *item)
734{
735 config_item_put(item);
736}
737
738static struct configfs_group_operations config_desc_ops = {
739 .make_group = &config_desc_make,
740 .drop_item = &config_desc_drop,
741};
742
Bhumika Goyal4ad01412017-10-16 17:18:46 +0200743static const struct config_item_type config_desc_type = {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100744 .ct_group_ops = &config_desc_ops,
745 .ct_owner = THIS_MODULE,
746};
747
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100748GS_STRINGS_RW(gadget_strings, manufacturer);
749GS_STRINGS_RW(gadget_strings, product);
750GS_STRINGS_RW(gadget_strings, serialnumber);
751
752static struct configfs_attribute *gadget_strings_langid_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200753 &gadget_strings_attr_manufacturer,
754 &gadget_strings_attr_product,
755 &gadget_strings_attr_serialnumber,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100756 NULL,
757};
758
759static void gadget_strings_attr_release(struct config_item *item)
760{
761 struct gadget_strings *gs = to_gadget_strings(item);
762
763 kfree(gs->manufacturer);
764 kfree(gs->product);
765 kfree(gs->serialnumber);
766
767 list_del(&gs->list);
768 kfree(gs);
769}
770
771USB_CONFIG_STRING_RW_OPS(gadget_strings);
772USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
773
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200774static inline struct os_desc *to_os_desc(struct config_item *item)
775{
776 return container_of(to_config_group(item), struct os_desc, group);
777}
778
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200779static inline struct gadget_info *os_desc_item_to_gadget_info(
780 struct config_item *item)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200781{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200782 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200783}
784
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200785static ssize_t os_desc_use_show(struct config_item *item, char *page)
786{
Stefan Agnere800e8cb2017-04-14 18:35:09 -0700787 return sprintf(page, "%d\n",
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200788 os_desc_item_to_gadget_info(item)->use_os_desc);
789}
790
791static ssize_t os_desc_use_store(struct config_item *item, const char *page,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200792 size_t len)
793{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200794 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200795 int ret;
796 bool use;
797
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200798 mutex_lock(&gi->lock);
799 ret = strtobool(page, &use);
800 if (!ret) {
801 gi->use_os_desc = use;
802 ret = len;
803 }
804 mutex_unlock(&gi->lock);
805
806 return ret;
807}
808
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200809static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200810{
Stefan Agnere800e8cb2017-04-14 18:35:09 -0700811 return sprintf(page, "0x%02x\n",
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200812 os_desc_item_to_gadget_info(item)->b_vendor_code);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200813}
814
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200815static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200816 const char *page, size_t len)
817{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200818 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200819 int ret;
820 u8 b_vendor_code;
821
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200822 mutex_lock(&gi->lock);
823 ret = kstrtou8(page, 0, &b_vendor_code);
824 if (!ret) {
825 gi->b_vendor_code = b_vendor_code;
826 ret = len;
827 }
828 mutex_unlock(&gi->lock);
829
830 return ret;
831}
832
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200833static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200834{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200835 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Stefan Agner76180d72017-04-14 18:35:08 -0700836 int res;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200837
Stefan Agner76180d72017-04-14 18:35:08 -0700838 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
839 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
840 page[res++] = '\n';
841
842 return res;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200843}
844
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200845static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200846 size_t len)
847{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200848 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200849 int res, l;
850
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200851 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
852 if (page[l - 1] == '\n')
853 --l;
854
855 mutex_lock(&gi->lock);
856 res = utf8s_to_utf16s(page, l,
857 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
858 OS_STRING_QW_SIGN_LEN);
859 if (res > 0)
860 res = len;
861 mutex_unlock(&gi->lock);
862
863 return res;
864}
865
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200866CONFIGFS_ATTR(os_desc_, use);
867CONFIGFS_ATTR(os_desc_, b_vendor_code);
868CONFIGFS_ATTR(os_desc_, qw_sign);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200869
870static struct configfs_attribute *os_desc_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200871 &os_desc_attr_use,
872 &os_desc_attr_b_vendor_code,
873 &os_desc_attr_qw_sign,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200874 NULL,
875};
876
877static void os_desc_attr_release(struct config_item *item)
878{
879 struct os_desc *os_desc = to_os_desc(item);
880 kfree(os_desc);
881}
882
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200883static int os_desc_link(struct config_item *os_desc_ci,
884 struct config_item *usb_cfg_ci)
885{
886 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
887 struct gadget_info, os_desc_group);
888 struct usb_composite_dev *cdev = &gi->cdev;
889 struct config_usb_cfg *c_target =
890 container_of(to_config_group(usb_cfg_ci),
891 struct config_usb_cfg, group);
892 struct usb_configuration *c;
893 int ret;
894
895 mutex_lock(&gi->lock);
896 list_for_each_entry(c, &cdev->configs, list) {
897 if (c == &c_target->c)
898 break;
899 }
900 if (c != &c_target->c) {
901 ret = -EINVAL;
902 goto out;
903 }
904
905 if (cdev->os_desc_config) {
906 ret = -EBUSY;
907 goto out;
908 }
909
910 cdev->os_desc_config = &c_target->c;
911 ret = 0;
912
913out:
914 mutex_unlock(&gi->lock);
915 return ret;
916}
917
Andrzej Pietrasiewicze16769d2016-11-28 13:22:42 +0100918static void os_desc_unlink(struct config_item *os_desc_ci,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200919 struct config_item *usb_cfg_ci)
920{
921 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
922 struct gadget_info, os_desc_group);
923 struct usb_composite_dev *cdev = &gi->cdev;
924
925 mutex_lock(&gi->lock);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100926 if (gi->composite.gadget_driver.udc_name)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200927 unregister_gadget(gi);
928 cdev->os_desc_config = NULL;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100929 WARN_ON(gi->composite.gadget_driver.udc_name);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200930 mutex_unlock(&gi->lock);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200931}
932
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200933static struct configfs_item_operations os_desc_ops = {
934 .release = os_desc_attr_release,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200935 .allow_link = os_desc_link,
936 .drop_link = os_desc_unlink,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200937};
938
939static struct config_item_type os_desc_type = {
940 .ct_item_ops = &os_desc_ops,
941 .ct_attrs = os_desc_attrs,
942 .ct_owner = THIS_MODULE,
943};
944
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200945static inline struct usb_os_desc_ext_prop
946*to_usb_os_desc_ext_prop(struct config_item *item)
947{
948 return container_of(item, struct usb_os_desc_ext_prop, item);
949}
950
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200951static ssize_t ext_prop_type_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200952{
Stefan Agnere800e8cb2017-04-14 18:35:09 -0700953 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200954}
955
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200956static ssize_t ext_prop_type_store(struct config_item *item,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200957 const char *page, size_t len)
958{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200959 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200960 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
961 u8 type;
962 int ret;
963
964 if (desc->opts_mutex)
965 mutex_lock(desc->opts_mutex);
966 ret = kstrtou8(page, 0, &type);
967 if (ret)
968 goto end;
969 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
970 ret = -EINVAL;
971 goto end;
972 }
973
974 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
975 ext_prop->type == USB_EXT_PROP_LE32 ||
976 ext_prop->type == USB_EXT_PROP_BE32) &&
977 (type == USB_EXT_PROP_UNICODE ||
978 type == USB_EXT_PROP_UNICODE_ENV ||
979 type == USB_EXT_PROP_UNICODE_LINK))
980 ext_prop->data_len <<= 1;
981 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
982 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
983 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
984 (type == USB_EXT_PROP_BINARY ||
985 type == USB_EXT_PROP_LE32 ||
986 type == USB_EXT_PROP_BE32))
987 ext_prop->data_len >>= 1;
988 ext_prop->type = type;
989 ret = len;
990
991end:
992 if (desc->opts_mutex)
993 mutex_unlock(desc->opts_mutex);
994 return ret;
995}
996
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200997static ssize_t ext_prop_data_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200998{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200999 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001000 int len = ext_prop->data_len;
1001
1002 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1003 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1004 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1005 len >>= 1;
1006 memcpy(page, ext_prop->data, len);
1007
1008 return len;
1009}
1010
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001011static ssize_t ext_prop_data_store(struct config_item *item,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001012 const char *page, size_t len)
1013{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001014 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001015 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1016 char *new_data;
1017 size_t ret_len = len;
1018
1019 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1020 --len;
Benoit Taine58b949e2014-05-26 17:21:20 +02001021 new_data = kmemdup(page, len, GFP_KERNEL);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001022 if (!new_data)
1023 return -ENOMEM;
1024
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001025 if (desc->opts_mutex)
1026 mutex_lock(desc->opts_mutex);
1027 kfree(ext_prop->data);
1028 ext_prop->data = new_data;
1029 desc->ext_prop_len -= ext_prop->data_len;
1030 ext_prop->data_len = len;
1031 desc->ext_prop_len += ext_prop->data_len;
1032 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1033 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1034 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1035 desc->ext_prop_len -= ext_prop->data_len;
1036 ext_prop->data_len <<= 1;
1037 ext_prop->data_len += 2;
1038 desc->ext_prop_len += ext_prop->data_len;
1039 }
1040 if (desc->opts_mutex)
1041 mutex_unlock(desc->opts_mutex);
1042 return ret_len;
1043}
1044
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001045CONFIGFS_ATTR(ext_prop_, type);
1046CONFIGFS_ATTR(ext_prop_, data);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001047
1048static struct configfs_attribute *ext_prop_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001049 &ext_prop_attr_type,
1050 &ext_prop_attr_data,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001051 NULL,
1052};
1053
1054static void usb_os_desc_ext_prop_release(struct config_item *item)
1055{
1056 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1057
1058 kfree(ext_prop); /* frees a whole chunk */
1059}
1060
1061static struct configfs_item_operations ext_prop_ops = {
1062 .release = usb_os_desc_ext_prop_release,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001063};
1064
1065static struct config_item *ext_prop_make(
1066 struct config_group *group,
1067 const char *name)
1068{
1069 struct usb_os_desc_ext_prop *ext_prop;
1070 struct config_item_type *ext_prop_type;
1071 struct usb_os_desc *desc;
1072 char *vlabuf;
1073
1074 vla_group(data_chunk);
1075 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1076 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1077
1078 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1079 if (!vlabuf)
1080 return ERR_PTR(-ENOMEM);
1081
1082 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1083 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1084
1085 desc = container_of(group, struct usb_os_desc, group);
1086 ext_prop_type->ct_item_ops = &ext_prop_ops;
1087 ext_prop_type->ct_attrs = ext_prop_attrs;
1088 ext_prop_type->ct_owner = desc->owner;
1089
1090 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1091
1092 ext_prop->name = kstrdup(name, GFP_KERNEL);
1093 if (!ext_prop->name) {
1094 kfree(vlabuf);
1095 return ERR_PTR(-ENOMEM);
1096 }
1097 desc->ext_prop_len += 14;
1098 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1099 if (desc->opts_mutex)
1100 mutex_lock(desc->opts_mutex);
1101 desc->ext_prop_len += ext_prop->name_len;
1102 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1103 ++desc->ext_prop_count;
1104 if (desc->opts_mutex)
1105 mutex_unlock(desc->opts_mutex);
1106
1107 return &ext_prop->item;
1108}
1109
1110static void ext_prop_drop(struct config_group *group, struct config_item *item)
1111{
1112 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1113 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1114
1115 if (desc->opts_mutex)
1116 mutex_lock(desc->opts_mutex);
1117 list_del(&ext_prop->entry);
1118 --desc->ext_prop_count;
1119 kfree(ext_prop->name);
1120 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1121 if (desc->opts_mutex)
1122 mutex_unlock(desc->opts_mutex);
1123 config_item_put(item);
1124}
1125
1126static struct configfs_group_operations interf_grp_ops = {
1127 .make_item = &ext_prop_make,
1128 .drop_item = &ext_prop_drop,
1129};
1130
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001131static ssize_t interf_grp_compatible_id_show(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001132 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001133{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001134 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001135 return 8;
1136}
1137
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001138static ssize_t interf_grp_compatible_id_store(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001139 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001140{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001141 struct usb_os_desc *desc = to_usb_os_desc(item);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001142 int l;
1143
1144 l = min_t(int, 8, len);
1145 if (page[l - 1] == '\n')
1146 --l;
1147 if (desc->opts_mutex)
1148 mutex_lock(desc->opts_mutex);
1149 memcpy(desc->ext_compat_id, page, l);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001150
1151 if (desc->opts_mutex)
1152 mutex_unlock(desc->opts_mutex);
1153
1154 return len;
1155}
1156
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001157static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001158 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001159{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001160 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001161 return 8;
1162}
1163
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001164static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001165 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001166{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001167 struct usb_os_desc *desc = to_usb_os_desc(item);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001168 int l;
1169
1170 l = min_t(int, 8, len);
1171 if (page[l - 1] == '\n')
1172 --l;
1173 if (desc->opts_mutex)
1174 mutex_lock(desc->opts_mutex);
1175 memcpy(desc->ext_compat_id + 8, page, l);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001176
1177 if (desc->opts_mutex)
1178 mutex_unlock(desc->opts_mutex);
1179
1180 return len;
1181}
1182
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001183CONFIGFS_ATTR(interf_grp_, compatible_id);
1184CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001185
1186static struct configfs_attribute *interf_grp_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001187 &interf_grp_attr_compatible_id,
1188 &interf_grp_attr_sub_compatible_id,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001189 NULL
1190};
1191
Andrew Gabbasovff747452017-09-30 08:54:52 -07001192struct config_group *usb_os_desc_prepare_interf_dir(
1193 struct config_group *parent,
1194 int n_interf,
1195 struct usb_os_desc **desc,
1196 char **names,
1197 struct module *owner)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001198{
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001199 struct config_group *os_desc_group;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001200 struct config_item_type *os_desc_type, *interface_type;
1201
1202 vla_group(data_chunk);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001203 vla_item(data_chunk, struct config_group, os_desc_group, 1);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001204 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1205 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1206
1207 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1208 if (!vlabuf)
Andrew Gabbasovff747452017-09-30 08:54:52 -07001209 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001210
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001211 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1212 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001213 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1214
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001215 os_desc_type->ct_owner = owner;
1216 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001217 configfs_add_default_group(os_desc_group, parent);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001218
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001219 interface_type->ct_group_ops = &interf_grp_ops;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001220 interface_type->ct_attrs = interf_grp_attrs;
1221 interface_type->ct_owner = owner;
1222
1223 while (n_interf--) {
1224 struct usb_os_desc *d;
1225
1226 d = desc[n_interf];
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001227 d->owner = owner;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001228 config_group_init_type_name(&d->group, "", interface_type);
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +02001229 config_item_set_name(&d->group.cg_item, "interface.%s",
1230 names[n_interf]);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001231 configfs_add_default_group(&d->group, os_desc_group);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001232 }
1233
Andrew Gabbasovff747452017-09-30 08:54:52 -07001234 return os_desc_group;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001235}
1236EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1237
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001238static int configfs_do_nothing(struct usb_composite_dev *cdev)
1239{
David Rientjes75bfe232013-04-07 14:11:47 -07001240 WARN_ON(1);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001241 return -EINVAL;
1242}
1243
1244int composite_dev_prepare(struct usb_composite_driver *composite,
1245 struct usb_composite_dev *dev);
1246
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001247int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1248 struct usb_ep *ep0);
1249
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001250static void purge_configs_funcs(struct gadget_info *gi)
1251{
1252 struct usb_configuration *c;
1253
1254 list_for_each_entry(c, &gi->cdev.configs, list) {
1255 struct usb_function *f, *tmp;
1256 struct config_usb_cfg *cfg;
1257
1258 cfg = container_of(c, struct config_usb_cfg, c);
1259
1260 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1261
1262 list_move_tail(&f->list, &cfg->func_list);
1263 if (f->unbind) {
Romain Izardda7b8952016-08-29 12:22:29 +03001264 dev_dbg(&gi->cdev.gadget->dev,
Parth Y Shahad22a662018-06-14 12:02:46 +05301265 "unbind function '%s'/%p\n",
1266 f->name, f);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001267 f->unbind(c, f);
1268 }
1269 }
1270 c->next_interface_id = 0;
Krzysztof Opasiak903124f2015-03-20 15:48:56 +01001271 memset(c->interface, 0, sizeof(c->interface));
John Youn554eead2016-02-05 17:06:35 -08001272 c->superspeed_plus = 0;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001273 c->superspeed = 0;
1274 c->highspeed = 0;
1275 c->fullspeed = 0;
1276 }
1277}
1278
1279static int configfs_composite_bind(struct usb_gadget *gadget,
1280 struct usb_gadget_driver *gdriver)
1281{
1282 struct usb_composite_driver *composite = to_cdriver(gdriver);
1283 struct gadget_info *gi = container_of(composite,
1284 struct gadget_info, composite);
1285 struct usb_composite_dev *cdev = &gi->cdev;
1286 struct usb_configuration *c;
1287 struct usb_string *s;
1288 unsigned i;
1289 int ret;
1290
1291 /* the gi->lock is hold by the caller */
Peter Chen1a1c851b2019-08-26 15:10:55 -04001292 gi->unbind = 0;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001293 cdev->gadget = gadget;
1294 set_gadget_data(gadget, cdev);
1295 ret = composite_dev_prepare(composite, cdev);
1296 if (ret)
1297 return ret;
1298 /* and now the gadget bind */
1299 ret = -EINVAL;
1300
1301 if (list_empty(&gi->cdev.configs)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001302 pr_err("Need at least one configuration in %s.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001303 gi->composite.name);
1304 goto err_comp_cleanup;
1305 }
1306
1307
1308 list_for_each_entry(c, &gi->cdev.configs, list) {
1309 struct config_usb_cfg *cfg;
1310
1311 cfg = container_of(c, struct config_usb_cfg, c);
1312 if (list_empty(&cfg->func_list)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001313 pr_err("Config %s/%d of %s needs at least one function.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001314 c->label, c->bConfigurationValue,
1315 gi->composite.name);
1316 goto err_comp_cleanup;
1317 }
1318 }
1319
1320 /* init all strings */
1321 if (!list_empty(&gi->string_list)) {
1322 struct gadget_strings *gs;
1323
1324 i = 0;
1325 list_for_each_entry(gs, &gi->string_list, list) {
1326
1327 gi->gstrings[i] = &gs->stringtab_dev;
1328 gs->stringtab_dev.strings = gs->strings;
1329 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1330 gs->manufacturer;
1331 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1332 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1333 i++;
1334 }
1335 gi->gstrings[i] = NULL;
1336 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1337 USB_GADGET_FIRST_AVAIL_IDX);
Wei Yongjunfea77072013-05-07 19:50:31 +08001338 if (IS_ERR(s)) {
1339 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001340 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001341 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001342
1343 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1344 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1345 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1346 }
1347
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001348 if (gi->use_os_desc) {
1349 cdev->use_os_string = true;
1350 cdev->b_vendor_code = gi->b_vendor_code;
1351 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1352 }
1353
Li Jun41ce84c2015-07-09 15:18:48 +08001354 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1355 struct usb_descriptor_header *usb_desc;
1356
1357 usb_desc = usb_otg_descriptor_alloc(gadget);
1358 if (!usb_desc) {
1359 ret = -ENOMEM;
1360 goto err_comp_cleanup;
1361 }
1362 usb_otg_descriptor_init(gadget, usb_desc);
1363 otg_desc[0] = usb_desc;
1364 otg_desc[1] = NULL;
1365 }
1366
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001367 /* Go through all configs, attach all functions */
1368 list_for_each_entry(c, &gi->cdev.configs, list) {
1369 struct config_usb_cfg *cfg;
1370 struct usb_function *f;
1371 struct usb_function *tmp;
1372 struct gadget_config_name *cn;
1373
Li Jun41ce84c2015-07-09 15:18:48 +08001374 if (gadget_is_otg(gadget))
1375 c->descriptors = otg_desc;
1376
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001377 cfg = container_of(c, struct config_usb_cfg, c);
1378 if (!list_empty(&cfg->string_list)) {
1379 i = 0;
1380 list_for_each_entry(cn, &cfg->string_list, list) {
1381 cfg->gstrings[i] = &cn->stringtab_dev;
1382 cn->stringtab_dev.strings = &cn->strings;
1383 cn->strings.s = cn->configuration;
1384 i++;
1385 }
1386 cfg->gstrings[i] = NULL;
1387 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
Wei Yongjunfea77072013-05-07 19:50:31 +08001388 if (IS_ERR(s)) {
1389 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001390 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001391 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001392 c->iConfiguration = s[0].id;
1393 }
1394
1395 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1396 list_del(&f->list);
1397 ret = usb_add_function(c, f);
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001398 if (ret) {
1399 list_add(&f->list, &cfg->func_list);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001400 goto err_purge_funcs;
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001401 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001402 }
1403 usb_ep_autoconfig_reset(cdev->gadget);
1404 }
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001405 if (cdev->use_os_string) {
1406 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1407 if (ret)
1408 goto err_purge_funcs;
1409 }
1410
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001411 usb_ep_autoconfig_reset(cdev->gadget);
1412 return 0;
1413
1414err_purge_funcs:
1415 purge_configs_funcs(gi);
1416err_comp_cleanup:
1417 composite_dev_cleanup(cdev);
1418 return ret;
1419}
1420
1421static void configfs_composite_unbind(struct usb_gadget *gadget)
1422{
1423 struct usb_composite_dev *cdev;
1424 struct gadget_info *gi;
Peter Chen1a1c851b2019-08-26 15:10:55 -04001425 unsigned long flags;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001426
1427 /* the gi->lock is hold by the caller */
1428
1429 cdev = get_gadget_data(gadget);
1430 gi = container_of(cdev, struct gadget_info, cdev);
Peter Chen1a1c851b2019-08-26 15:10:55 -04001431 spin_lock_irqsave(&gi->spinlock, flags);
1432 gi->unbind = 1;
1433 spin_unlock_irqrestore(&gi->spinlock, flags);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001434
Li Jun41ce84c2015-07-09 15:18:48 +08001435 kfree(otg_desc[0]);
1436 otg_desc[0] = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001437 purge_configs_funcs(gi);
1438 composite_dev_cleanup(cdev);
1439 usb_ep_autoconfig_reset(cdev->gadget);
Peter Chen1a1c851b2019-08-26 15:10:55 -04001440 spin_lock_irqsave(&gi->spinlock, flags);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001441 cdev->gadget = NULL;
1442 set_gadget_data(gadget, NULL);
Peter Chen1a1c851b2019-08-26 15:10:55 -04001443 spin_unlock_irqrestore(&gi->spinlock, flags);
1444}
1445
1446static int configfs_composite_setup(struct usb_gadget *gadget,
1447 const struct usb_ctrlrequest *ctrl)
1448{
1449 struct usb_composite_dev *cdev;
1450 struct gadget_info *gi;
1451 unsigned long flags;
1452 int ret;
1453
1454 cdev = get_gadget_data(gadget);
1455 if (!cdev)
1456 return 0;
1457
1458 gi = container_of(cdev, struct gadget_info, cdev);
1459 spin_lock_irqsave(&gi->spinlock, flags);
1460 cdev = get_gadget_data(gadget);
1461 if (!cdev || gi->unbind) {
1462 spin_unlock_irqrestore(&gi->spinlock, flags);
1463 return 0;
1464 }
1465
1466 ret = composite_setup(gadget, ctrl);
1467 spin_unlock_irqrestore(&gi->spinlock, flags);
1468 return ret;
1469}
1470
1471static void configfs_composite_disconnect(struct usb_gadget *gadget)
1472{
1473 struct usb_composite_dev *cdev;
1474 struct gadget_info *gi;
1475 unsigned long flags;
1476
1477 cdev = get_gadget_data(gadget);
1478 if (!cdev)
1479 return;
1480
1481 gi = container_of(cdev, struct gadget_info, cdev);
1482 spin_lock_irqsave(&gi->spinlock, flags);
1483 cdev = get_gadget_data(gadget);
1484 if (!cdev || gi->unbind) {
1485 spin_unlock_irqrestore(&gi->spinlock, flags);
1486 return;
1487 }
1488
1489 composite_disconnect(gadget);
1490 spin_unlock_irqrestore(&gi->spinlock, flags);
1491}
1492
1493static void configfs_composite_suspend(struct usb_gadget *gadget)
1494{
1495 struct usb_composite_dev *cdev;
1496 struct gadget_info *gi;
1497 unsigned long flags;
1498
1499 cdev = get_gadget_data(gadget);
1500 if (!cdev)
1501 return;
1502
1503 gi = container_of(cdev, struct gadget_info, cdev);
1504 spin_lock_irqsave(&gi->spinlock, flags);
1505 cdev = get_gadget_data(gadget);
1506 if (!cdev || gi->unbind) {
1507 spin_unlock_irqrestore(&gi->spinlock, flags);
1508 return;
1509 }
1510
1511 composite_suspend(gadget);
1512 spin_unlock_irqrestore(&gi->spinlock, flags);
1513}
1514
1515static void configfs_composite_resume(struct usb_gadget *gadget)
1516{
1517 struct usb_composite_dev *cdev;
1518 struct gadget_info *gi;
1519 unsigned long flags;
1520
1521 cdev = get_gadget_data(gadget);
1522 if (!cdev)
1523 return;
1524
1525 gi = container_of(cdev, struct gadget_info, cdev);
1526 spin_lock_irqsave(&gi->spinlock, flags);
1527 cdev = get_gadget_data(gadget);
1528 if (!cdev || gi->unbind) {
1529 spin_unlock_irqrestore(&gi->spinlock, flags);
1530 return;
1531 }
1532
1533 composite_resume(gadget);
1534 spin_unlock_irqrestore(&gi->spinlock, flags);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001535}
1536
1537static const struct usb_gadget_driver configfs_driver_template = {
1538 .bind = configfs_composite_bind,
1539 .unbind = configfs_composite_unbind,
1540
Peter Chen1a1c851b2019-08-26 15:10:55 -04001541 .setup = configfs_composite_setup,
1542 .reset = configfs_composite_disconnect,
1543 .disconnect = configfs_composite_disconnect,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001544
Peter Chen1a1c851b2019-08-26 15:10:55 -04001545 .suspend = configfs_composite_suspend,
1546 .resume = configfs_composite_resume,
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02001547
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001548 .max_speed = USB_SPEED_SUPER,
1549 .driver = {
1550 .owner = THIS_MODULE,
1551 .name = "configfs-gadget",
1552 },
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001553 .match_existing_only = 1,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001554};
1555
1556static struct config_group *gadgets_make(
1557 struct config_group *group,
1558 const char *name)
1559{
1560 struct gadget_info *gi;
1561
1562 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1563 if (!gi)
1564 return ERR_PTR(-ENOMEM);
1565
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001566 config_group_init_type_name(&gi->group, name, &gadget_root_type);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001567
1568 config_group_init_type_name(&gi->functions_group, "functions",
1569 &functions_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001570 configfs_add_default_group(&gi->functions_group, &gi->group);
1571
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001572 config_group_init_type_name(&gi->configs_group, "configs",
1573 &config_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001574 configfs_add_default_group(&gi->configs_group, &gi->group);
1575
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001576 config_group_init_type_name(&gi->strings_group, "strings",
1577 &gadget_strings_strings_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001578 configfs_add_default_group(&gi->strings_group, &gi->group);
1579
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001580 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1581 &os_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001582 configfs_add_default_group(&gi->os_desc_group, &gi->group);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001583
1584 gi->composite.bind = configfs_do_nothing;
1585 gi->composite.unbind = configfs_do_nothing;
1586 gi->composite.suspend = NULL;
1587 gi->composite.resume = NULL;
1588 gi->composite.max_speed = USB_SPEED_SUPER;
1589
Wei Yongjun093edc22019-10-30 03:40:46 +00001590 spin_lock_init(&gi->spinlock);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001591 mutex_init(&gi->lock);
1592 INIT_LIST_HEAD(&gi->string_list);
1593 INIT_LIST_HEAD(&gi->available_func);
1594
1595 composite_init_dev(&gi->cdev);
1596 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1597 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1598 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1599
1600 gi->composite.gadget_driver = configfs_driver_template;
1601
1602 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1603 gi->composite.name = gi->composite.gadget_driver.function;
1604
1605 if (!gi->composite.gadget_driver.function)
1606 goto err;
1607
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001608 return &gi->group;
1609err:
1610 kfree(gi);
1611 return ERR_PTR(-ENOMEM);
1612}
1613
1614static void gadgets_drop(struct config_group *group, struct config_item *item)
1615{
1616 config_item_put(item);
1617}
1618
1619static struct configfs_group_operations gadgets_ops = {
1620 .make_group = &gadgets_make,
1621 .drop_item = &gadgets_drop,
1622};
1623
Bhumika Goyal4ad01412017-10-16 17:18:46 +02001624static const struct config_item_type gadgets_type = {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001625 .ct_group_ops = &gadgets_ops,
1626 .ct_owner = THIS_MODULE,
1627};
1628
1629static struct configfs_subsystem gadget_subsys = {
1630 .su_group = {
1631 .cg_item = {
1632 .ci_namebuf = "usb_gadget",
1633 .ci_type = &gadgets_type,
1634 },
1635 },
1636 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1637};
1638
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001639void unregister_gadget_item(struct config_item *item)
1640{
1641 struct gadget_info *gi = to_gadget_info(item);
1642
Winter Wangcee51c32016-07-27 10:03:19 +08001643 mutex_lock(&gi->lock);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001644 unregister_gadget(gi);
Winter Wangcee51c32016-07-27 10:03:19 +08001645 mutex_unlock(&gi->lock);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001646}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001647EXPORT_SYMBOL_GPL(unregister_gadget_item);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001648
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001649static int __init gadget_cfs_init(void)
1650{
1651 int ret;
1652
1653 config_group_init(&gadget_subsys.su_group);
1654
1655 ret = configfs_register_subsystem(&gadget_subsys);
1656 return ret;
1657}
1658module_init(gadget_cfs_init);
1659
1660static void __exit gadget_cfs_exit(void)
1661{
1662 configfs_unregister_subsystem(&gadget_subsys);
1663}
1664module_exit(gadget_cfs_exit);