blob: b1d22d8c9f7ef296283a98986741639a53800b2e [file] [log] [blame]
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001#include <linux/configfs.h>
2#include <linux/module.h>
3#include <linux/slab.h>
4#include <linux/device.h>
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02005#include <linux/nls.h>
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01006#include <linux/usb/composite.h>
7#include <linux/usb/gadget_configfs.h>
Rashika Kheria0009e992013-12-19 15:37:37 +05308#include "configfs.h"
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02009#include "u_f.h"
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +020010#include "u_os_desc.h"
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010011
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -080012#ifdef CONFIG_USB_CONFIGFS_UEVENT
13#include <linux/platform_device.h>
14#include <linux/kdev_t.h>
15#include <linux/usb/ch9.h>
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -080016
17#ifdef CONFIG_USB_CONFIGFS_F_ACC
18extern int acc_ctrlrequest(struct usb_composite_dev *cdev,
19 const struct usb_ctrlrequest *ctrl);
20void acc_disconnect(void);
21#endif
22static struct class *android_class;
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -070023static struct device *android_device;
24static int index;
25
26struct device *create_function_device(char *name)
27{
28 if (android_device && !IS_ERR(android_device))
29 return device_create(android_class, android_device,
30 MKDEV(0, index++), NULL, name);
31 else
32 return ERR_PTR(-EINVAL);
33}
34EXPORT_SYMBOL_GPL(create_function_device);
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -080035#endif
36
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010037int check_user_usb_string(const char *name,
38 struct usb_gadget_strings *stringtab_dev)
39{
40 unsigned primary_lang;
41 unsigned sub_lang;
42 u16 num;
43 int ret;
44
45 ret = kstrtou16(name, 0, &num);
46 if (ret)
47 return ret;
48
49 primary_lang = num & 0x3ff;
50 sub_lang = num >> 10;
51
52 /* simple sanity check for valid langid */
53 switch (primary_lang) {
54 case 0:
55 case 0x62 ... 0xfe:
56 case 0x100 ... 0x3ff:
57 return -EINVAL;
58 }
59 if (!sub_lang)
60 return -EINVAL;
61
62 stringtab_dev->language = num;
63 return 0;
64}
65
66#define MAX_NAME_LEN 40
67#define MAX_USB_STRING_LANGS 2
68
Li Jun41ce84c2015-07-09 15:18:48 +080069static const struct usb_descriptor_header *otg_desc[2];
70
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010071struct gadget_info {
72 struct config_group group;
73 struct config_group functions_group;
74 struct config_group configs_group;
75 struct config_group strings_group;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020076 struct config_group os_desc_group;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010077
78 struct mutex lock;
79 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
80 struct list_head string_list;
81 struct list_head available_func;
82
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010083 struct usb_composite_driver composite;
84 struct usb_composite_dev cdev;
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +020085 bool use_os_desc;
86 char b_vendor_code;
87 char qw_sign[OS_STRING_QW_SIGN_LEN];
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -080088#ifdef CONFIG_USB_CONFIGFS_UEVENT
89 bool connected;
90 bool sw_connected;
91 struct work_struct work;
92 struct device *dev;
93#endif
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +010094};
95
Christoph Hellwig45b6a732015-10-03 15:32:38 +020096static inline struct gadget_info *to_gadget_info(struct config_item *item)
97{
98 return container_of(to_config_group(item), struct gadget_info, group);
99}
100
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100101struct config_usb_cfg {
102 struct config_group group;
103 struct config_group strings_group;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100104 struct list_head string_list;
105 struct usb_configuration c;
106 struct list_head func_list;
107 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
108};
109
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200110static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
111{
112 return container_of(to_config_group(item), struct config_usb_cfg,
113 group);
114}
115
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100116struct gadget_strings {
117 struct usb_gadget_strings stringtab_dev;
118 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
119 char *manufacturer;
120 char *product;
121 char *serialnumber;
122
123 struct config_group group;
124 struct list_head list;
125};
126
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200127struct os_desc {
128 struct config_group group;
129};
130
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100131struct gadget_config_name {
132 struct usb_gadget_strings stringtab_dev;
133 struct usb_string strings;
134 char *configuration;
135
136 struct config_group group;
137 struct list_head list;
138};
139
140static int usb_string_copy(const char *s, char **s_copy)
141{
142 int ret;
143 char *str;
144 char *copy = *s_copy;
145 ret = strlen(s);
146 if (ret > 126)
147 return -EOVERFLOW;
148
149 str = kstrdup(s, GFP_KERNEL);
150 if (!str)
151 return -ENOMEM;
152 if (str[ret - 1] == '\n')
153 str[ret - 1] = '\0';
154 kfree(copy);
155 *s_copy = str;
156 return 0;
157}
158
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100159#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200160static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100161 char *page) \
162{ \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200163 return sprintf(page, "0x%02x\n", \
164 to_gadget_info(item)->cdev.desc.__name); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100165}
166
167#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200168static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100169 char *page) \
170{ \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200171 return sprintf(page, "0x%04x\n", \
172 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100173}
174
175
176#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200177static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100178 const char *page, size_t len) \
179{ \
180 u8 val; \
181 int ret; \
182 ret = kstrtou8(page, 0, &val); \
183 if (ret) \
184 return ret; \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200185 to_gadget_info(item)->cdev.desc._name = val; \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100186 return len; \
187}
188
189#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200190static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100191 const char *page, size_t len) \
192{ \
193 u16 val; \
194 int ret; \
195 ret = kstrtou16(page, 0, &val); \
196 if (ret) \
197 return ret; \
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200198 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100199 return len; \
200}
201
202#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
203 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
204 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
205
206GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
207GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
208GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
209GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
210GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
211GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
212GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
213GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
214
215static ssize_t is_valid_bcd(u16 bcd_val)
216{
217 if ((bcd_val & 0xf) > 9)
218 return -EINVAL;
219 if (((bcd_val >> 4) & 0xf) > 9)
220 return -EINVAL;
221 if (((bcd_val >> 8) & 0xf) > 9)
222 return -EINVAL;
223 if (((bcd_val >> 12) & 0xf) > 9)
224 return -EINVAL;
225 return 0;
226}
227
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200228static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100229 const char *page, size_t len)
230{
231 u16 bcdDevice;
232 int ret;
233
234 ret = kstrtou16(page, 0, &bcdDevice);
235 if (ret)
236 return ret;
237 ret = is_valid_bcd(bcdDevice);
238 if (ret)
239 return ret;
240
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200241 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100242 return len;
243}
244
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200245static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100246 const char *page, size_t len)
247{
248 u16 bcdUSB;
249 int ret;
250
251 ret = kstrtou16(page, 0, &bcdUSB);
252 if (ret)
253 return ret;
254 ret = is_valid_bcd(bcdUSB);
255 if (ret)
256 return ret;
257
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200258 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100259 return len;
260}
261
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200262static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100263{
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100264 char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
265
266 return sprintf(page, "%s\n", udc_name ?: "");
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100267}
268
269static int unregister_gadget(struct gadget_info *gi)
270{
271 int ret;
272
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100273 if (!gi->composite.gadget_driver.udc_name)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100274 return -ENODEV;
275
276 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
277 if (ret)
278 return ret;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100279 kfree(gi->composite.gadget_driver.udc_name);
280 gi->composite.gadget_driver.udc_name = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100281 return 0;
282}
283
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200284static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100285 const char *page, size_t len)
286{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200287 struct gadget_info *gi = to_gadget_info(item);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100288 char *name;
289 int ret;
290
291 name = kstrdup(page, GFP_KERNEL);
292 if (!name)
293 return -ENOMEM;
294 if (name[len - 1] == '\n')
295 name[len - 1] = '\0';
296
297 mutex_lock(&gi->lock);
298
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -0800299 if (!strlen(name) || strcmp(name, "none") == 0) {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100300 ret = unregister_gadget(gi);
301 if (ret)
302 goto err;
John Keeping59682e72017-02-28 10:55:30 +0000303 kfree(name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100304 } else {
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100305 if (gi->composite.gadget_driver.udc_name) {
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100306 ret = -EBUSY;
307 goto err;
308 }
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100309 gi->composite.gadget_driver.udc_name = name;
310 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
311 if (ret) {
312 gi->composite.gadget_driver.udc_name = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100313 goto err;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100314 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100315 }
316 mutex_unlock(&gi->lock);
317 return len;
318err:
319 kfree(name);
320 mutex_unlock(&gi->lock);
321 return ret;
322}
323
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200324CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
325CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
326CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
327CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
328CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
329CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
330CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
331CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
332CONFIGFS_ATTR(gadget_dev_desc_, UDC);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100333
334static struct configfs_attribute *gadget_root_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200335 &gadget_dev_desc_attr_bDeviceClass,
336 &gadget_dev_desc_attr_bDeviceSubClass,
337 &gadget_dev_desc_attr_bDeviceProtocol,
338 &gadget_dev_desc_attr_bMaxPacketSize0,
339 &gadget_dev_desc_attr_idVendor,
340 &gadget_dev_desc_attr_idProduct,
341 &gadget_dev_desc_attr_bcdDevice,
342 &gadget_dev_desc_attr_bcdUSB,
343 &gadget_dev_desc_attr_UDC,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100344 NULL,
345};
346
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100347static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
348{
349 return container_of(to_config_group(item), struct gadget_strings,
350 group);
351}
352
353static inline struct gadget_config_name *to_gadget_config_name(
354 struct config_item *item)
355{
356 return container_of(to_config_group(item), struct gadget_config_name,
357 group);
358}
359
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100360static inline struct usb_function_instance *to_usb_function_instance(
361 struct config_item *item)
362{
363 return container_of(to_config_group(item),
364 struct usb_function_instance, group);
365}
366
367static void gadget_info_attr_release(struct config_item *item)
368{
369 struct gadget_info *gi = to_gadget_info(item);
370
371 WARN_ON(!list_empty(&gi->cdev.configs));
372 WARN_ON(!list_empty(&gi->string_list));
373 WARN_ON(!list_empty(&gi->available_func));
374 kfree(gi->composite.gadget_driver.function);
375 kfree(gi);
376}
377
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100378static struct configfs_item_operations gadget_root_item_ops = {
379 .release = gadget_info_attr_release,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100380};
381
382static void gadget_config_attr_release(struct config_item *item)
383{
384 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
385
386 WARN_ON(!list_empty(&cfg->c.functions));
387 list_del(&cfg->c.list);
388 kfree(cfg->c.label);
389 kfree(cfg);
390}
391
392static int config_usb_cfg_link(
393 struct config_item *usb_cfg_ci,
394 struct config_item *usb_func_ci)
395{
396 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
397 struct usb_composite_dev *cdev = cfg->c.cdev;
398 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
399
400 struct config_group *group = to_config_group(usb_func_ci);
401 struct usb_function_instance *fi = container_of(group,
402 struct usb_function_instance, group);
403 struct usb_function_instance *a_fi;
404 struct usb_function *f;
405 int ret;
406
407 mutex_lock(&gi->lock);
408 /*
409 * Make sure this function is from within our _this_ gadget and not
410 * from another gadget or a random directory.
411 * Also a function instance can only be linked once.
412 */
413 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
414 if (a_fi == fi)
415 break;
416 }
417 if (a_fi != fi) {
418 ret = -EINVAL;
419 goto out;
420 }
421
422 list_for_each_entry(f, &cfg->func_list, list) {
423 if (f->fi == fi) {
424 ret = -EEXIST;
425 goto out;
426 }
427 }
428
429 f = usb_get_function(fi);
430 if (IS_ERR(f)) {
431 ret = PTR_ERR(f);
432 goto out;
433 }
434
435 /* stash the function until we bind it to the gadget */
436 list_add_tail(&f->list, &cfg->func_list);
437 ret = 0;
438out:
439 mutex_unlock(&gi->lock);
440 return ret;
441}
442
443static int config_usb_cfg_unlink(
444 struct config_item *usb_cfg_ci,
445 struct config_item *usb_func_ci)
446{
447 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
448 struct usb_composite_dev *cdev = cfg->c.cdev;
449 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
450
451 struct config_group *group = to_config_group(usb_func_ci);
452 struct usb_function_instance *fi = container_of(group,
453 struct usb_function_instance, group);
454 struct usb_function *f;
455
456 /*
457 * ideally I would like to forbid to unlink functions while a gadget is
458 * bound to an UDC. Since this isn't possible at the moment, we simply
459 * force an unbind, the function is available here and then we can
460 * remove the function.
461 */
462 mutex_lock(&gi->lock);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100463 if (gi->composite.gadget_driver.udc_name)
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100464 unregister_gadget(gi);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100465 WARN_ON(gi->composite.gadget_driver.udc_name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100466
467 list_for_each_entry(f, &cfg->func_list, list) {
468 if (f->fi == fi) {
469 list_del(&f->list);
470 usb_put_function(f);
471 mutex_unlock(&gi->lock);
472 return 0;
473 }
474 }
475 mutex_unlock(&gi->lock);
David Rientjes75bfe232013-04-07 14:11:47 -0700476 WARN(1, "Unable to locate function to unbind\n");
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100477 return 0;
478}
479
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100480static struct configfs_item_operations gadget_config_item_ops = {
481 .release = gadget_config_attr_release,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100482 .allow_link = config_usb_cfg_link,
483 .drop_link = config_usb_cfg_unlink,
484};
485
486
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200487static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100488 char *page)
489{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200490 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100491}
492
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200493static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100494 const char *page, size_t len)
495{
496 u16 val;
497 int ret;
498 ret = kstrtou16(page, 0, &val);
499 if (ret)
500 return ret;
501 if (DIV_ROUND_UP(val, 8) > 0xff)
502 return -ERANGE;
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200503 to_config_usb_cfg(item)->c.MaxPower = val;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100504 return len;
505}
506
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200507static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100508 char *page)
509{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200510 return sprintf(page, "0x%02x\n",
511 to_config_usb_cfg(item)->c.bmAttributes);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100512}
513
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200514static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100515 const char *page, size_t len)
516{
517 u8 val;
518 int ret;
519 ret = kstrtou8(page, 0, &val);
520 if (ret)
521 return ret;
522 if (!(val & USB_CONFIG_ATT_ONE))
523 return -EINVAL;
524 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
525 USB_CONFIG_ATT_WAKEUP))
526 return -EINVAL;
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200527 to_config_usb_cfg(item)->c.bmAttributes = val;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100528 return len;
529}
530
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200531CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
532CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100533
534static struct configfs_attribute *gadget_config_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200535 &gadget_config_desc_attr_MaxPower,
536 &gadget_config_desc_attr_bmAttributes,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100537 NULL,
538};
539
540static struct config_item_type gadget_config_type = {
541 .ct_item_ops = &gadget_config_item_ops,
542 .ct_attrs = gadget_config_attrs,
543 .ct_owner = THIS_MODULE,
544};
545
546static struct config_item_type gadget_root_type = {
547 .ct_item_ops = &gadget_root_item_ops,
548 .ct_attrs = gadget_root_attrs,
549 .ct_owner = THIS_MODULE,
550};
551
552static void composite_init_dev(struct usb_composite_dev *cdev)
553{
554 spin_lock_init(&cdev->lock);
555 INIT_LIST_HEAD(&cdev->configs);
556 INIT_LIST_HEAD(&cdev->gstrings);
557}
558
559static struct config_group *function_make(
560 struct config_group *group,
561 const char *name)
562{
563 struct gadget_info *gi;
564 struct usb_function_instance *fi;
565 char buf[MAX_NAME_LEN];
566 char *func_name;
567 char *instance_name;
568 int ret;
569
570 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
571 if (ret >= MAX_NAME_LEN)
572 return ERR_PTR(-ENAMETOOLONG);
573
574 func_name = buf;
575 instance_name = strchr(func_name, '.');
576 if (!instance_name) {
577 pr_err("Unable to locate . in FUNC.INSTANCE\n");
578 return ERR_PTR(-EINVAL);
579 }
580 *instance_name = '\0';
581 instance_name++;
582
583 fi = usb_get_function_instance(func_name);
584 if (IS_ERR(fi))
Duan Jionga3469412013-09-26 15:55:25 +0800585 return ERR_CAST(fi);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100586
Nicolas Iooss3958b792015-07-17 16:23:45 -0700587 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100588 if (ret) {
589 usb_put_function_instance(fi);
590 return ERR_PTR(ret);
591 }
Andrzej Pietrasiewicz19338612013-12-03 15:15:21 +0100592 if (fi->set_inst_name) {
593 ret = fi->set_inst_name(fi, instance_name);
594 if (ret) {
595 usb_put_function_instance(fi);
596 return ERR_PTR(ret);
597 }
598 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100599
600 gi = container_of(group, struct gadget_info, functions_group);
601
602 mutex_lock(&gi->lock);
603 list_add_tail(&fi->cfs_list, &gi->available_func);
604 mutex_unlock(&gi->lock);
605 return &fi->group;
606}
607
608static void function_drop(
609 struct config_group *group,
610 struct config_item *item)
611{
612 struct usb_function_instance *fi = to_usb_function_instance(item);
613 struct gadget_info *gi;
614
615 gi = container_of(group, struct gadget_info, functions_group);
616
617 mutex_lock(&gi->lock);
618 list_del(&fi->cfs_list);
619 mutex_unlock(&gi->lock);
620 config_item_put(item);
621}
622
623static struct configfs_group_operations functions_ops = {
624 .make_group = &function_make,
625 .drop_item = &function_drop,
626};
627
628static struct config_item_type functions_type = {
629 .ct_group_ops = &functions_ops,
630 .ct_owner = THIS_MODULE,
631};
632
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100633GS_STRINGS_RW(gadget_config_name, configuration);
634
635static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200636 &gadget_config_name_attr_configuration,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100637 NULL,
638};
639
640static void gadget_config_name_attr_release(struct config_item *item)
641{
642 struct gadget_config_name *cn = to_gadget_config_name(item);
643
644 kfree(cn->configuration);
645
646 list_del(&cn->list);
647 kfree(cn);
648}
649
650USB_CONFIG_STRING_RW_OPS(gadget_config_name);
651USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
652
653static struct config_group *config_desc_make(
654 struct config_group *group,
655 const char *name)
656{
657 struct gadget_info *gi;
658 struct config_usb_cfg *cfg;
659 char buf[MAX_NAME_LEN];
660 char *num_str;
661 u8 num;
662 int ret;
663
664 gi = container_of(group, struct gadget_info, configs_group);
665 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
666 if (ret >= MAX_NAME_LEN)
667 return ERR_PTR(-ENAMETOOLONG);
668
669 num_str = strchr(buf, '.');
670 if (!num_str) {
671 pr_err("Unable to locate . in name.bConfigurationValue\n");
672 return ERR_PTR(-EINVAL);
673 }
674
675 *num_str = '\0';
676 num_str++;
677
678 if (!strlen(buf))
679 return ERR_PTR(-EINVAL);
680
681 ret = kstrtou8(num_str, 0, &num);
682 if (ret)
683 return ERR_PTR(ret);
684
685 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
686 if (!cfg)
687 return ERR_PTR(-ENOMEM);
688 cfg->c.label = kstrdup(buf, GFP_KERNEL);
689 if (!cfg->c.label) {
690 ret = -ENOMEM;
691 goto err;
692 }
693 cfg->c.bConfigurationValue = num;
694 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
695 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
696 INIT_LIST_HEAD(&cfg->string_list);
697 INIT_LIST_HEAD(&cfg->func_list);
698
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100699 config_group_init_type_name(&cfg->group, name,
700 &gadget_config_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100701
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100702 config_group_init_type_name(&cfg->strings_group, "strings",
703 &gadget_config_name_strings_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100704 configfs_add_default_group(&cfg->strings_group, &cfg->group);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100705
706 ret = usb_add_config_only(&gi->cdev, &cfg->c);
707 if (ret)
708 goto err;
709
710 return &cfg->group;
711err:
712 kfree(cfg->c.label);
713 kfree(cfg);
714 return ERR_PTR(ret);
715}
716
717static void config_desc_drop(
718 struct config_group *group,
719 struct config_item *item)
720{
721 config_item_put(item);
722}
723
724static struct configfs_group_operations config_desc_ops = {
725 .make_group = &config_desc_make,
726 .drop_item = &config_desc_drop,
727};
728
729static struct config_item_type config_desc_type = {
730 .ct_group_ops = &config_desc_ops,
731 .ct_owner = THIS_MODULE,
732};
733
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100734GS_STRINGS_RW(gadget_strings, manufacturer);
735GS_STRINGS_RW(gadget_strings, product);
736GS_STRINGS_RW(gadget_strings, serialnumber);
737
738static struct configfs_attribute *gadget_strings_langid_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200739 &gadget_strings_attr_manufacturer,
740 &gadget_strings_attr_product,
741 &gadget_strings_attr_serialnumber,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100742 NULL,
743};
744
745static void gadget_strings_attr_release(struct config_item *item)
746{
747 struct gadget_strings *gs = to_gadget_strings(item);
748
749 kfree(gs->manufacturer);
750 kfree(gs->product);
751 kfree(gs->serialnumber);
752
753 list_del(&gs->list);
754 kfree(gs);
755}
756
757USB_CONFIG_STRING_RW_OPS(gadget_strings);
758USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
759
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200760static inline struct os_desc *to_os_desc(struct config_item *item)
761{
762 return container_of(to_config_group(item), struct os_desc, group);
763}
764
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200765static inline struct gadget_info *os_desc_item_to_gadget_info(
766 struct config_item *item)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200767{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200768 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200769}
770
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200771static ssize_t os_desc_use_show(struct config_item *item, char *page)
772{
773 return sprintf(page, "%d",
774 os_desc_item_to_gadget_info(item)->use_os_desc);
775}
776
777static ssize_t os_desc_use_store(struct config_item *item, const char *page,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200778 size_t len)
779{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200780 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200781 int ret;
782 bool use;
783
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200784 mutex_lock(&gi->lock);
785 ret = strtobool(page, &use);
786 if (!ret) {
787 gi->use_os_desc = use;
788 ret = len;
789 }
790 mutex_unlock(&gi->lock);
791
792 return ret;
793}
794
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200795static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200796{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200797 return sprintf(page, "%d",
798 os_desc_item_to_gadget_info(item)->b_vendor_code);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200799}
800
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200801static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200802 const char *page, size_t len)
803{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200804 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200805 int ret;
806 u8 b_vendor_code;
807
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200808 mutex_lock(&gi->lock);
809 ret = kstrtou8(page, 0, &b_vendor_code);
810 if (!ret) {
811 gi->b_vendor_code = b_vendor_code;
812 ret = len;
813 }
814 mutex_unlock(&gi->lock);
815
816 return ret;
817}
818
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200819static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200820{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200821 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200822
823 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200824 return OS_STRING_QW_SIGN_LEN;
825}
826
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200827static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200828 size_t len)
829{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200830 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200831 int res, l;
832
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200833 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
834 if (page[l - 1] == '\n')
835 --l;
836
837 mutex_lock(&gi->lock);
838 res = utf8s_to_utf16s(page, l,
839 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
840 OS_STRING_QW_SIGN_LEN);
841 if (res > 0)
842 res = len;
843 mutex_unlock(&gi->lock);
844
845 return res;
846}
847
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200848CONFIGFS_ATTR(os_desc_, use);
849CONFIGFS_ATTR(os_desc_, b_vendor_code);
850CONFIGFS_ATTR(os_desc_, qw_sign);
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200851
852static struct configfs_attribute *os_desc_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200853 &os_desc_attr_use,
854 &os_desc_attr_b_vendor_code,
855 &os_desc_attr_qw_sign,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200856 NULL,
857};
858
859static void os_desc_attr_release(struct config_item *item)
860{
861 struct os_desc *os_desc = to_os_desc(item);
862 kfree(os_desc);
863}
864
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200865static int os_desc_link(struct config_item *os_desc_ci,
866 struct config_item *usb_cfg_ci)
867{
868 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
869 struct gadget_info, os_desc_group);
870 struct usb_composite_dev *cdev = &gi->cdev;
871 struct config_usb_cfg *c_target =
872 container_of(to_config_group(usb_cfg_ci),
873 struct config_usb_cfg, group);
874 struct usb_configuration *c;
875 int ret;
876
877 mutex_lock(&gi->lock);
878 list_for_each_entry(c, &cdev->configs, list) {
879 if (c == &c_target->c)
880 break;
881 }
882 if (c != &c_target->c) {
883 ret = -EINVAL;
884 goto out;
885 }
886
887 if (cdev->os_desc_config) {
888 ret = -EBUSY;
889 goto out;
890 }
891
892 cdev->os_desc_config = &c_target->c;
893 ret = 0;
894
895out:
896 mutex_unlock(&gi->lock);
897 return ret;
898}
899
900static int os_desc_unlink(struct config_item *os_desc_ci,
901 struct config_item *usb_cfg_ci)
902{
903 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
904 struct gadget_info, os_desc_group);
905 struct usb_composite_dev *cdev = &gi->cdev;
906
907 mutex_lock(&gi->lock);
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100908 if (gi->composite.gadget_driver.udc_name)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200909 unregister_gadget(gi);
910 cdev->os_desc_config = NULL;
Ruslan Bilovolafdaadc2015-11-23 09:56:36 +0100911 WARN_ON(gi->composite.gadget_driver.udc_name);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200912 mutex_unlock(&gi->lock);
913 return 0;
914}
915
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200916static struct configfs_item_operations os_desc_ops = {
917 .release = os_desc_attr_release,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +0200918 .allow_link = os_desc_link,
919 .drop_link = os_desc_unlink,
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +0200920};
921
922static struct config_item_type os_desc_type = {
923 .ct_item_ops = &os_desc_ops,
924 .ct_attrs = os_desc_attrs,
925 .ct_owner = THIS_MODULE,
926};
927
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200928static inline struct usb_os_desc_ext_prop
929*to_usb_os_desc_ext_prop(struct config_item *item)
930{
931 return container_of(item, struct usb_os_desc_ext_prop, item);
932}
933
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200934static ssize_t ext_prop_type_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200935{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200936 return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200937}
938
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200939static ssize_t ext_prop_type_store(struct config_item *item,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200940 const char *page, size_t len)
941{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200942 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200943 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
944 u8 type;
945 int ret;
946
947 if (desc->opts_mutex)
948 mutex_lock(desc->opts_mutex);
949 ret = kstrtou8(page, 0, &type);
950 if (ret)
951 goto end;
952 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
953 ret = -EINVAL;
954 goto end;
955 }
956
957 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
958 ext_prop->type == USB_EXT_PROP_LE32 ||
959 ext_prop->type == USB_EXT_PROP_BE32) &&
960 (type == USB_EXT_PROP_UNICODE ||
961 type == USB_EXT_PROP_UNICODE_ENV ||
962 type == USB_EXT_PROP_UNICODE_LINK))
963 ext_prop->data_len <<= 1;
964 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
965 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
966 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
967 (type == USB_EXT_PROP_BINARY ||
968 type == USB_EXT_PROP_LE32 ||
969 type == USB_EXT_PROP_BE32))
970 ext_prop->data_len >>= 1;
971 ext_prop->type = type;
972 ret = len;
973
974end:
975 if (desc->opts_mutex)
976 mutex_unlock(desc->opts_mutex);
977 return ret;
978}
979
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200980static ssize_t ext_prop_data_show(struct config_item *item, char *page)
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200981{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200982 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200983 int len = ext_prop->data_len;
984
985 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
986 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
987 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
988 len >>= 1;
989 memcpy(page, ext_prop->data, len);
990
991 return len;
992}
993
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200994static ssize_t ext_prop_data_store(struct config_item *item,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200995 const char *page, size_t len)
996{
Christoph Hellwig45b6a732015-10-03 15:32:38 +0200997 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +0200998 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
999 char *new_data;
1000 size_t ret_len = len;
1001
1002 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1003 --len;
Benoit Taine58b949e2014-05-26 17:21:20 +02001004 new_data = kmemdup(page, len, GFP_KERNEL);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001005 if (!new_data)
1006 return -ENOMEM;
1007
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001008 if (desc->opts_mutex)
1009 mutex_lock(desc->opts_mutex);
1010 kfree(ext_prop->data);
1011 ext_prop->data = new_data;
1012 desc->ext_prop_len -= ext_prop->data_len;
1013 ext_prop->data_len = len;
1014 desc->ext_prop_len += ext_prop->data_len;
1015 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1016 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1017 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1018 desc->ext_prop_len -= ext_prop->data_len;
1019 ext_prop->data_len <<= 1;
1020 ext_prop->data_len += 2;
1021 desc->ext_prop_len += ext_prop->data_len;
1022 }
1023 if (desc->opts_mutex)
1024 mutex_unlock(desc->opts_mutex);
1025 return ret_len;
1026}
1027
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001028CONFIGFS_ATTR(ext_prop_, type);
1029CONFIGFS_ATTR(ext_prop_, data);
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001030
1031static struct configfs_attribute *ext_prop_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001032 &ext_prop_attr_type,
1033 &ext_prop_attr_data,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001034 NULL,
1035};
1036
1037static void usb_os_desc_ext_prop_release(struct config_item *item)
1038{
1039 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1040
1041 kfree(ext_prop); /* frees a whole chunk */
1042}
1043
1044static struct configfs_item_operations ext_prop_ops = {
1045 .release = usb_os_desc_ext_prop_release,
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001046};
1047
1048static struct config_item *ext_prop_make(
1049 struct config_group *group,
1050 const char *name)
1051{
1052 struct usb_os_desc_ext_prop *ext_prop;
1053 struct config_item_type *ext_prop_type;
1054 struct usb_os_desc *desc;
1055 char *vlabuf;
1056
1057 vla_group(data_chunk);
1058 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1059 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1060
1061 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1062 if (!vlabuf)
1063 return ERR_PTR(-ENOMEM);
1064
1065 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1066 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1067
1068 desc = container_of(group, struct usb_os_desc, group);
1069 ext_prop_type->ct_item_ops = &ext_prop_ops;
1070 ext_prop_type->ct_attrs = ext_prop_attrs;
1071 ext_prop_type->ct_owner = desc->owner;
1072
1073 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1074
1075 ext_prop->name = kstrdup(name, GFP_KERNEL);
1076 if (!ext_prop->name) {
1077 kfree(vlabuf);
1078 return ERR_PTR(-ENOMEM);
1079 }
1080 desc->ext_prop_len += 14;
1081 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1082 if (desc->opts_mutex)
1083 mutex_lock(desc->opts_mutex);
1084 desc->ext_prop_len += ext_prop->name_len;
1085 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1086 ++desc->ext_prop_count;
1087 if (desc->opts_mutex)
1088 mutex_unlock(desc->opts_mutex);
1089
1090 return &ext_prop->item;
1091}
1092
1093static void ext_prop_drop(struct config_group *group, struct config_item *item)
1094{
1095 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1096 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1097
1098 if (desc->opts_mutex)
1099 mutex_lock(desc->opts_mutex);
1100 list_del(&ext_prop->entry);
1101 --desc->ext_prop_count;
1102 kfree(ext_prop->name);
1103 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1104 if (desc->opts_mutex)
1105 mutex_unlock(desc->opts_mutex);
1106 config_item_put(item);
1107}
1108
1109static struct configfs_group_operations interf_grp_ops = {
1110 .make_item = &ext_prop_make,
1111 .drop_item = &ext_prop_drop,
1112};
1113
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001114static ssize_t interf_grp_compatible_id_show(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001115 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001116{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001117 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001118 return 8;
1119}
1120
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001121static ssize_t interf_grp_compatible_id_store(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001122 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001123{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001124 struct usb_os_desc *desc = to_usb_os_desc(item);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001125 int l;
1126
1127 l = min_t(int, 8, len);
1128 if (page[l - 1] == '\n')
1129 --l;
1130 if (desc->opts_mutex)
1131 mutex_lock(desc->opts_mutex);
1132 memcpy(desc->ext_compat_id, page, l);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001133
1134 if (desc->opts_mutex)
1135 mutex_unlock(desc->opts_mutex);
1136
1137 return len;
1138}
1139
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001140static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001141 char *page)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001142{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001143 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001144 return 8;
1145}
1146
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001147static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
Andrzej Pietrasiewiczfe00b132014-06-18 14:24:48 +02001148 const char *page, size_t len)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001149{
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001150 struct usb_os_desc *desc = to_usb_os_desc(item);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001151 int l;
1152
1153 l = min_t(int, 8, len);
1154 if (page[l - 1] == '\n')
1155 --l;
1156 if (desc->opts_mutex)
1157 mutex_lock(desc->opts_mutex);
1158 memcpy(desc->ext_compat_id + 8, page, l);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001159
1160 if (desc->opts_mutex)
1161 mutex_unlock(desc->opts_mutex);
1162
1163 return len;
1164}
1165
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001166CONFIGFS_ATTR(interf_grp_, compatible_id);
1167CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001168
1169static struct configfs_attribute *interf_grp_attrs[] = {
Christoph Hellwig45b6a732015-10-03 15:32:38 +02001170 &interf_grp_attr_compatible_id,
1171 &interf_grp_attr_sub_compatible_id,
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001172 NULL
1173};
1174
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -07001175struct config_group *usb_os_desc_prepare_interf_dir(
1176 struct config_group *parent,
1177 int n_interf,
1178 struct usb_os_desc **desc,
1179 char **names,
1180 struct module *owner)
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001181{
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001182 struct config_group *os_desc_group;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001183 struct config_item_type *os_desc_type, *interface_type;
1184
1185 vla_group(data_chunk);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001186 vla_item(data_chunk, struct config_group, os_desc_group, 1);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001187 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1188 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1189
1190 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1191 if (!vlabuf)
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -07001192 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001193
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001194 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1195 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001196 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1197
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001198 os_desc_type->ct_owner = owner;
1199 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001200 configfs_add_default_group(os_desc_group, parent);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001201
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001202 interface_type->ct_group_ops = &interf_grp_ops;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001203 interface_type->ct_attrs = interf_grp_attrs;
1204 interface_type->ct_owner = owner;
1205
1206 while (n_interf--) {
1207 struct usb_os_desc *d;
1208
1209 d = desc[n_interf];
Andrzej Pietrasiewicz74194852014-05-08 14:06:28 +02001210 d->owner = owner;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001211 config_group_init_type_name(&d->group, "", interface_type);
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +02001212 config_item_set_name(&d->group.cg_item, "interface.%s",
1213 names[n_interf]);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001214 configfs_add_default_group(&d->group, os_desc_group);
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001215 }
1216
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -07001217 return os_desc_group;
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001218}
1219EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1220
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001221static int configfs_do_nothing(struct usb_composite_dev *cdev)
1222{
David Rientjes75bfe232013-04-07 14:11:47 -07001223 WARN_ON(1);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001224 return -EINVAL;
1225}
1226
1227int composite_dev_prepare(struct usb_composite_driver *composite,
1228 struct usb_composite_dev *dev);
1229
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001230int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1231 struct usb_ep *ep0);
1232
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001233static void purge_configs_funcs(struct gadget_info *gi)
1234{
1235 struct usb_configuration *c;
1236
1237 list_for_each_entry(c, &gi->cdev.configs, list) {
1238 struct usb_function *f, *tmp;
1239 struct config_usb_cfg *cfg;
1240
1241 cfg = container_of(c, struct config_usb_cfg, c);
1242
1243 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1244
1245 list_move_tail(&f->list, &cfg->func_list);
1246 if (f->unbind) {
Romain Izardda7b8952016-08-29 12:22:29 +03001247 dev_dbg(&gi->cdev.gadget->dev,
Romain Izarda08f5db2016-07-26 18:21:46 +02001248 "unbind function '%s'/%p\n",
1249 f->name, f);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001250 f->unbind(c, f);
1251 }
1252 }
1253 c->next_interface_id = 0;
Krzysztof Opasiak903124f2015-03-20 15:48:56 +01001254 memset(c->interface, 0, sizeof(c->interface));
John Youn554eead2016-02-05 17:06:35 -08001255 c->superspeed_plus = 0;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001256 c->superspeed = 0;
1257 c->highspeed = 0;
1258 c->fullspeed = 0;
1259 }
1260}
1261
1262static int configfs_composite_bind(struct usb_gadget *gadget,
1263 struct usb_gadget_driver *gdriver)
1264{
1265 struct usb_composite_driver *composite = to_cdriver(gdriver);
1266 struct gadget_info *gi = container_of(composite,
1267 struct gadget_info, composite);
1268 struct usb_composite_dev *cdev = &gi->cdev;
1269 struct usb_configuration *c;
1270 struct usb_string *s;
1271 unsigned i;
1272 int ret;
1273
1274 /* the gi->lock is hold by the caller */
1275 cdev->gadget = gadget;
1276 set_gadget_data(gadget, cdev);
1277 ret = composite_dev_prepare(composite, cdev);
1278 if (ret)
1279 return ret;
1280 /* and now the gadget bind */
1281 ret = -EINVAL;
1282
1283 if (list_empty(&gi->cdev.configs)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001284 pr_err("Need at least one configuration in %s.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001285 gi->composite.name);
1286 goto err_comp_cleanup;
1287 }
1288
1289
1290 list_for_each_entry(c, &gi->cdev.configs, list) {
1291 struct config_usb_cfg *cfg;
1292
1293 cfg = container_of(c, struct config_usb_cfg, c);
1294 if (list_empty(&cfg->func_list)) {
Peter Chen4d9f8722014-05-05 07:39:34 +08001295 pr_err("Config %s/%d of %s needs at least one function.\n",
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001296 c->label, c->bConfigurationValue,
1297 gi->composite.name);
1298 goto err_comp_cleanup;
1299 }
1300 }
1301
1302 /* init all strings */
1303 if (!list_empty(&gi->string_list)) {
1304 struct gadget_strings *gs;
1305
1306 i = 0;
1307 list_for_each_entry(gs, &gi->string_list, list) {
1308
1309 gi->gstrings[i] = &gs->stringtab_dev;
1310 gs->stringtab_dev.strings = gs->strings;
1311 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1312 gs->manufacturer;
1313 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1314 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1315 i++;
1316 }
1317 gi->gstrings[i] = NULL;
1318 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1319 USB_GADGET_FIRST_AVAIL_IDX);
Wei Yongjunfea77072013-05-07 19:50:31 +08001320 if (IS_ERR(s)) {
1321 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001322 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001323 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001324
1325 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1326 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1327 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1328 }
1329
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001330 if (gi->use_os_desc) {
1331 cdev->use_os_string = true;
1332 cdev->b_vendor_code = gi->b_vendor_code;
1333 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1334 }
1335
Li Jun41ce84c2015-07-09 15:18:48 +08001336 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1337 struct usb_descriptor_header *usb_desc;
1338
1339 usb_desc = usb_otg_descriptor_alloc(gadget);
1340 if (!usb_desc) {
1341 ret = -ENOMEM;
1342 goto err_comp_cleanup;
1343 }
1344 usb_otg_descriptor_init(gadget, usb_desc);
1345 otg_desc[0] = usb_desc;
1346 otg_desc[1] = NULL;
1347 }
1348
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001349 /* Go through all configs, attach all functions */
1350 list_for_each_entry(c, &gi->cdev.configs, list) {
1351 struct config_usb_cfg *cfg;
1352 struct usb_function *f;
1353 struct usb_function *tmp;
1354 struct gadget_config_name *cn;
1355
Li Jun41ce84c2015-07-09 15:18:48 +08001356 if (gadget_is_otg(gadget))
1357 c->descriptors = otg_desc;
1358
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001359 cfg = container_of(c, struct config_usb_cfg, c);
1360 if (!list_empty(&cfg->string_list)) {
1361 i = 0;
1362 list_for_each_entry(cn, &cfg->string_list, list) {
1363 cfg->gstrings[i] = &cn->stringtab_dev;
1364 cn->stringtab_dev.strings = &cn->strings;
1365 cn->strings.s = cn->configuration;
1366 i++;
1367 }
1368 cfg->gstrings[i] = NULL;
1369 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
Wei Yongjunfea77072013-05-07 19:50:31 +08001370 if (IS_ERR(s)) {
1371 ret = PTR_ERR(s);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001372 goto err_comp_cleanup;
Wei Yongjunfea77072013-05-07 19:50:31 +08001373 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001374 c->iConfiguration = s[0].id;
1375 }
1376
1377 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1378 list_del(&f->list);
1379 ret = usb_add_function(c, f);
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001380 if (ret) {
1381 list_add(&f->list, &cfg->func_list);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001382 goto err_purge_funcs;
Andrzej Pietrasiewicz5a68e9b2013-08-08 09:43:28 +02001383 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001384 }
1385 usb_ep_autoconfig_reset(cdev->gadget);
1386 }
Andrzej Pietrasiewiczda424312014-05-08 14:06:26 +02001387 if (cdev->use_os_string) {
1388 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1389 if (ret)
1390 goto err_purge_funcs;
1391 }
1392
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001393 usb_ep_autoconfig_reset(cdev->gadget);
1394 return 0;
1395
1396err_purge_funcs:
1397 purge_configs_funcs(gi);
1398err_comp_cleanup:
1399 composite_dev_cleanup(cdev);
1400 return ret;
1401}
1402
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001403#ifdef CONFIG_USB_CONFIGFS_UEVENT
1404static void android_work(struct work_struct *data)
1405{
1406 struct gadget_info *gi = container_of(data, struct gadget_info, work);
1407 struct usb_composite_dev *cdev = &gi->cdev;
1408 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
1409 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
1410 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
1411 /* 0-connected 1-configured 2-disconnected*/
1412 bool status[3] = { false, false, false };
1413 unsigned long flags;
1414 bool uevent_sent = false;
1415
1416 spin_lock_irqsave(&cdev->lock, flags);
1417 if (cdev->config)
1418 status[1] = true;
1419
1420 if (gi->connected != gi->sw_connected) {
1421 if (gi->connected)
1422 status[0] = true;
1423 else
1424 status[2] = true;
1425 gi->sw_connected = gi->connected;
1426 }
1427 spin_unlock_irqrestore(&cdev->lock, flags);
1428
1429 if (status[0]) {
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -07001430 kobject_uevent_env(&android_device->kobj,
1431 KOBJ_CHANGE, connected);
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001432 pr_info("%s: sent uevent %s\n", __func__, connected[0]);
1433 uevent_sent = true;
1434 }
1435
1436 if (status[1]) {
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -07001437 kobject_uevent_env(&android_device->kobj,
1438 KOBJ_CHANGE, configured);
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001439 pr_info("%s: sent uevent %s\n", __func__, configured[0]);
1440 uevent_sent = true;
1441 }
1442
1443 if (status[2]) {
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -07001444 kobject_uevent_env(&android_device->kobj,
1445 KOBJ_CHANGE, disconnected);
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001446 pr_info("%s: sent uevent %s\n", __func__, disconnected[0]);
1447 uevent_sent = true;
1448 }
1449
1450 if (!uevent_sent) {
1451 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
1452 gi->connected, gi->sw_connected, cdev->config);
1453 }
1454}
1455#endif
1456
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001457static void configfs_composite_unbind(struct usb_gadget *gadget)
1458{
1459 struct usb_composite_dev *cdev;
1460 struct gadget_info *gi;
1461
1462 /* the gi->lock is hold by the caller */
1463
1464 cdev = get_gadget_data(gadget);
1465 gi = container_of(cdev, struct gadget_info, cdev);
1466
Li Jun41ce84c2015-07-09 15:18:48 +08001467 kfree(otg_desc[0]);
1468 otg_desc[0] = NULL;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001469 purge_configs_funcs(gi);
1470 composite_dev_cleanup(cdev);
1471 usb_ep_autoconfig_reset(cdev->gadget);
1472 cdev->gadget = NULL;
1473 set_gadget_data(gadget, NULL);
1474}
1475
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001476#ifdef CONFIG_USB_CONFIGFS_UEVENT
1477static int android_setup(struct usb_gadget *gadget,
1478 const struct usb_ctrlrequest *c)
1479{
1480 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1481 unsigned long flags;
1482 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1483 int value = -EOPNOTSUPP;
1484 struct usb_function_instance *fi;
1485
1486 spin_lock_irqsave(&cdev->lock, flags);
1487 if (!gi->connected) {
1488 gi->connected = 1;
1489 schedule_work(&gi->work);
1490 }
1491 spin_unlock_irqrestore(&cdev->lock, flags);
1492 list_for_each_entry(fi, &gi->available_func, cfs_list) {
1493 if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) {
1494 value = fi->f->setup(fi->f, c);
1495 if (value >= 0)
1496 break;
1497 }
1498 }
1499
1500#ifdef CONFIG_USB_CONFIGFS_F_ACC
1501 if (value < 0)
1502 value = acc_ctrlrequest(cdev, c);
1503#endif
1504
1505 if (value < 0)
1506 value = composite_setup(gadget, c);
1507
1508 spin_lock_irqsave(&cdev->lock, flags);
1509 if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1510 cdev->config) {
1511 schedule_work(&gi->work);
1512 }
1513 spin_unlock_irqrestore(&cdev->lock, flags);
1514
1515 return value;
1516}
1517
1518static void android_disconnect(struct usb_gadget *gadget)
1519{
1520 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1521 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1522
Danilo Krummrich6e055352017-08-14 12:45:38 +02001523 /* FIXME: There's a race between usb_gadget_udc_stop() which is likely
1524 * to set the gadget driver to NULL in the udc driver and this drivers
1525 * gadget disconnect fn which likely checks for the gadget driver to
1526 * be a null ptr. It happens that unbind (doing set_gadget_data(NULL))
1527 * is called before the gadget driver is set to NULL and the udc driver
1528 * calls disconnect fn which results in cdev being a null ptr.
1529 */
1530 if (cdev == NULL) {
1531 WARN(1, "%s: gadget driver already disconnected\n", __func__);
1532 return;
1533 }
1534
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001535 /* accessory HID support can be active while the
1536 accessory function is not actually enabled,
1537 so we need to inform it when we are disconnected.
1538 */
1539
1540#ifdef CONFIG_USB_CONFIGFS_F_ACC
1541 acc_disconnect();
1542#endif
1543 gi->connected = 0;
1544 schedule_work(&gi->work);
1545 composite_disconnect(gadget);
1546}
1547#endif
1548
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001549static const struct usb_gadget_driver configfs_driver_template = {
1550 .bind = configfs_composite_bind,
1551 .unbind = configfs_composite_unbind,
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001552#ifdef CONFIG_USB_CONFIGFS_UEVENT
1553 .setup = android_setup,
Amit Pundir9214c892015-10-06 20:53:27 +05301554 .reset = android_disconnect,
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001555 .disconnect = android_disconnect,
1556#else
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001557 .setup = composite_setup,
Peter Chen02f751b2014-09-09 08:56:50 +08001558 .reset = composite_disconnect,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001559 .disconnect = composite_disconnect,
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001560#endif
Andrzej Pietrasiewicz3a571872014-10-08 12:03:36 +02001561 .suspend = composite_suspend,
1562 .resume = composite_resume,
1563
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001564 .max_speed = USB_SPEED_SUPER,
1565 .driver = {
1566 .owner = THIS_MODULE,
1567 .name = "configfs-gadget",
1568 },
Krzysztof Opasiakf1bddbb2016-05-05 10:46:05 +02001569 .match_existing_only = 1,
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001570};
1571
Badhri Jagan Sridharan9150ea12015-07-14 15:46:11 -07001572#ifdef CONFIG_USB_CONFIGFS_UEVENT
1573static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1574 char *buf)
1575{
1576 struct gadget_info *dev = dev_get_drvdata(pdev);
1577 struct usb_composite_dev *cdev;
1578 char *state = "DISCONNECTED";
1579 unsigned long flags;
1580
1581 if (!dev)
1582 goto out;
1583
1584 cdev = &dev->cdev;
1585
1586 if (!cdev)
1587 goto out;
1588
1589 spin_lock_irqsave(&cdev->lock, flags);
1590 if (cdev->config)
1591 state = "CONFIGURED";
1592 else if (dev->connected)
1593 state = "CONNECTED";
1594 spin_unlock_irqrestore(&cdev->lock, flags);
1595out:
1596 return sprintf(buf, "%s\n", state);
1597}
1598
1599static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1600
1601static struct device_attribute *android_usb_attributes[] = {
1602 &dev_attr_state,
1603 NULL
1604};
Badhri Jagan Sridharan560f0132015-08-09 15:12:50 -07001605
1606static int android_device_create(struct gadget_info *gi)
1607{
1608 struct device_attribute **attrs;
1609 struct device_attribute *attr;
1610
1611 INIT_WORK(&gi->work, android_work);
1612 android_device = device_create(android_class, NULL,
1613 MKDEV(0, 0), NULL, "android0");
1614 if (IS_ERR(android_device))
1615 return PTR_ERR(android_device);
1616
1617 dev_set_drvdata(android_device, gi);
1618
1619 attrs = android_usb_attributes;
1620 while ((attr = *attrs++)) {
1621 int err;
1622
1623 err = device_create_file(android_device, attr);
1624 if (err) {
1625 device_destroy(android_device->class,
1626 android_device->devt);
1627 return err;
1628 }
1629 }
1630
1631 return 0;
1632}
1633
1634static void android_device_destroy(void)
1635{
1636 struct device_attribute **attrs;
1637 struct device_attribute *attr;
1638
1639 attrs = android_usb_attributes;
1640 while ((attr = *attrs++))
1641 device_remove_file(android_device, attr);
1642 device_destroy(android_device->class, android_device->devt);
1643}
1644#else
1645static inline int android_device_create(struct gadget_info *gi)
1646{
1647 return 0;
1648}
1649
1650static inline void android_device_destroy(void)
1651{
1652}
Badhri Jagan Sridharan9150ea12015-07-14 15:46:11 -07001653#endif
1654
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001655static struct config_group *gadgets_make(
1656 struct config_group *group,
1657 const char *name)
1658{
1659 struct gadget_info *gi;
1660
1661 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1662 if (!gi)
1663 return ERR_PTR(-ENOMEM);
1664
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001665 config_group_init_type_name(&gi->group, name, &gadget_root_type);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001666
1667 config_group_init_type_name(&gi->functions_group, "functions",
1668 &functions_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001669 configfs_add_default_group(&gi->functions_group, &gi->group);
1670
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001671 config_group_init_type_name(&gi->configs_group, "configs",
1672 &config_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001673 configfs_add_default_group(&gi->configs_group, &gi->group);
1674
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001675 config_group_init_type_name(&gi->strings_group, "strings",
1676 &gadget_strings_strings_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001677 configfs_add_default_group(&gi->strings_group, &gi->group);
1678
Andrzej Pietrasiewicz87213d32014-05-08 14:06:25 +02001679 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1680 &os_desc_type);
Christoph Hellwig1ae16022016-02-26 11:02:14 +01001681 configfs_add_default_group(&gi->os_desc_group, &gi->group);
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001682
1683 gi->composite.bind = configfs_do_nothing;
1684 gi->composite.unbind = configfs_do_nothing;
1685 gi->composite.suspend = NULL;
1686 gi->composite.resume = NULL;
1687 gi->composite.max_speed = USB_SPEED_SUPER;
1688
1689 mutex_init(&gi->lock);
1690 INIT_LIST_HEAD(&gi->string_list);
1691 INIT_LIST_HEAD(&gi->available_func);
1692
1693 composite_init_dev(&gi->cdev);
1694 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1695 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1696 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1697
1698 gi->composite.gadget_driver = configfs_driver_template;
1699
1700 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1701 gi->composite.name = gi->composite.gadget_driver.function;
1702
Badhri Jagan Sridharan560f0132015-08-09 15:12:50 -07001703 if (!gi->composite.gadget_driver.function)
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -07001704 goto err;
Badhri Jagan Sridharan9150ea12015-07-14 15:46:11 -07001705
Badhri Jagan Sridharan560f0132015-08-09 15:12:50 -07001706 if (android_device_create(gi) < 0)
1707 goto err;
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001708
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001709 return &gi->group;
Badhri Jagan Sridharan9150ea12015-07-14 15:46:11 -07001710
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001711err:
1712 kfree(gi);
1713 return ERR_PTR(-ENOMEM);
1714}
1715
1716static void gadgets_drop(struct config_group *group, struct config_item *item)
1717{
1718 config_item_put(item);
Badhri Jagan Sridharan560f0132015-08-09 15:12:50 -07001719 android_device_destroy();
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001720}
1721
1722static struct configfs_group_operations gadgets_ops = {
1723 .make_group = &gadgets_make,
1724 .drop_item = &gadgets_drop,
1725};
1726
1727static struct config_item_type gadgets_type = {
1728 .ct_group_ops = &gadgets_ops,
1729 .ct_owner = THIS_MODULE,
1730};
1731
1732static struct configfs_subsystem gadget_subsys = {
1733 .su_group = {
1734 .cg_item = {
1735 .ci_namebuf = "usb_gadget",
1736 .ci_type = &gadgets_type,
1737 },
1738 },
1739 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1740};
1741
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001742void unregister_gadget_item(struct config_item *item)
1743{
1744 struct gadget_info *gi = to_gadget_info(item);
1745
Winter Wangcee51c32016-07-27 10:03:19 +08001746 mutex_lock(&gi->lock);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001747 unregister_gadget(gi);
Winter Wangcee51c32016-07-27 10:03:19 +08001748 mutex_unlock(&gi->lock);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001749}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001750EXPORT_SYMBOL_GPL(unregister_gadget_item);
Andrzej Pietrasiewicz092a4bd2013-09-26 14:38:15 +02001751
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001752static int __init gadget_cfs_init(void)
1753{
1754 int ret;
1755
1756 config_group_init(&gadget_subsys.su_group);
1757
1758 ret = configfs_register_subsystem(&gadget_subsys);
Badhri Jagan Sridharan21877382014-12-15 10:44:47 -08001759
1760#ifdef CONFIG_USB_CONFIGFS_UEVENT
1761 android_class = class_create(THIS_MODULE, "android_usb");
1762 if (IS_ERR(android_class))
1763 return PTR_ERR(android_class);
1764#endif
1765
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001766 return ret;
1767}
1768module_init(gadget_cfs_init);
1769
1770static void __exit gadget_cfs_exit(void)
1771{
1772 configfs_unregister_subsystem(&gadget_subsys);
Badhri Jagan Sridharand6b2d772015-03-27 14:15:19 -07001773#ifdef CONFIG_USB_CONFIGFS_UEVENT
1774 if (!IS_ERR(android_class))
1775 class_destroy(android_class);
1776#endif
1777
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001778}
1779module_exit(gadget_cfs_exit);