Commit | Line | Data |
---|---|---|
88af8bbe SAS |
1 | #include <linux/configfs.h> |
2 | #include <linux/module.h> | |
3 | #include <linux/slab.h> | |
4 | #include <linux/device.h> | |
87213d38 | 5 | #include <linux/nls.h> |
88af8bbe SAS |
6 | #include <linux/usb/composite.h> |
7 | #include <linux/usb/gadget_configfs.h> | |
0009e99a | 8 | #include "configfs.h" |
da424314 | 9 | #include "u_f.h" |
7419485f | 10 | #include "u_os_desc.h" |
88af8bbe SAS |
11 | |
12 | int check_user_usb_string(const char *name, | |
13 | struct usb_gadget_strings *stringtab_dev) | |
14 | { | |
15 | unsigned primary_lang; | |
16 | unsigned sub_lang; | |
17 | u16 num; | |
18 | int ret; | |
19 | ||
20 | ret = kstrtou16(name, 0, &num); | |
21 | if (ret) | |
22 | return ret; | |
23 | ||
24 | primary_lang = num & 0x3ff; | |
25 | sub_lang = num >> 10; | |
26 | ||
27 | /* simple sanity check for valid langid */ | |
28 | switch (primary_lang) { | |
29 | case 0: | |
30 | case 0x62 ... 0xfe: | |
31 | case 0x100 ... 0x3ff: | |
32 | return -EINVAL; | |
33 | } | |
34 | if (!sub_lang) | |
35 | return -EINVAL; | |
36 | ||
37 | stringtab_dev->language = num; | |
38 | return 0; | |
39 | } | |
40 | ||
41 | #define MAX_NAME_LEN 40 | |
42 | #define MAX_USB_STRING_LANGS 2 | |
43 | ||
41ce84c8 LJ |
44 | static const struct usb_descriptor_header *otg_desc[2]; |
45 | ||
88af8bbe SAS |
46 | struct gadget_info { |
47 | struct config_group group; | |
48 | struct config_group functions_group; | |
49 | struct config_group configs_group; | |
50 | struct config_group strings_group; | |
87213d38 AP |
51 | struct config_group os_desc_group; |
52 | struct config_group *default_groups[5]; | |
88af8bbe SAS |
53 | |
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 | ||
59 | const char *udc_name; | |
88af8bbe SAS |
60 | struct usb_composite_driver composite; |
61 | struct usb_composite_dev cdev; | |
87213d38 AP |
62 | bool use_os_desc; |
63 | char b_vendor_code; | |
64 | char qw_sign[OS_STRING_QW_SIGN_LEN]; | |
88af8bbe SAS |
65 | }; |
66 | ||
67 | struct config_usb_cfg { | |
68 | struct config_group group; | |
69 | struct config_group strings_group; | |
70 | struct config_group *default_groups[2]; | |
71 | struct list_head string_list; | |
72 | struct usb_configuration c; | |
73 | struct list_head func_list; | |
74 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; | |
75 | }; | |
76 | ||
77 | struct gadget_strings { | |
78 | struct usb_gadget_strings stringtab_dev; | |
79 | struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; | |
80 | char *manufacturer; | |
81 | char *product; | |
82 | char *serialnumber; | |
83 | ||
84 | struct config_group group; | |
85 | struct list_head list; | |
86 | }; | |
87 | ||
87213d38 AP |
88 | struct os_desc { |
89 | struct config_group group; | |
90 | }; | |
91 | ||
88af8bbe SAS |
92 | struct gadget_config_name { |
93 | struct usb_gadget_strings stringtab_dev; | |
94 | struct usb_string strings; | |
95 | char *configuration; | |
96 | ||
97 | struct config_group group; | |
98 | struct list_head list; | |
99 | }; | |
100 | ||
101 | static int usb_string_copy(const char *s, char **s_copy) | |
102 | { | |
103 | int ret; | |
104 | char *str; | |
105 | char *copy = *s_copy; | |
106 | ret = strlen(s); | |
107 | if (ret > 126) | |
108 | return -EOVERFLOW; | |
109 | ||
110 | str = kstrdup(s, GFP_KERNEL); | |
111 | if (!str) | |
112 | return -ENOMEM; | |
113 | if (str[ret - 1] == '\n') | |
114 | str[ret - 1] = '\0'; | |
115 | kfree(copy); | |
116 | *s_copy = str; | |
117 | return 0; | |
118 | } | |
119 | ||
120 | CONFIGFS_ATTR_STRUCT(gadget_info); | |
121 | CONFIGFS_ATTR_STRUCT(config_usb_cfg); | |
122 | ||
123 | #define GI_DEVICE_DESC_ITEM_ATTR(name) \ | |
124 | static struct gadget_info_attribute gadget_cdev_desc_##name = \ | |
125 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
126 | gadget_dev_desc_##name##_show, \ | |
127 | gadget_dev_desc_##name##_store) | |
128 | ||
129 | #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ | |
130 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ | |
131 | char *page) \ | |
132 | { \ | |
133 | return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \ | |
134 | } | |
135 | ||
136 | #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ | |
137 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ | |
138 | char *page) \ | |
139 | { \ | |
140 | return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \ | |
141 | } | |
142 | ||
143 | ||
144 | #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ | |
145 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ | |
146 | const char *page, size_t len) \ | |
147 | { \ | |
148 | u8 val; \ | |
149 | int ret; \ | |
150 | ret = kstrtou8(page, 0, &val); \ | |
151 | if (ret) \ | |
152 | return ret; \ | |
153 | gi->cdev.desc._name = val; \ | |
154 | return len; \ | |
155 | } | |
156 | ||
157 | #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ | |
158 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ | |
159 | const char *page, size_t len) \ | |
160 | { \ | |
161 | u16 val; \ | |
162 | int ret; \ | |
163 | ret = kstrtou16(page, 0, &val); \ | |
164 | if (ret) \ | |
165 | return ret; \ | |
166 | gi->cdev.desc._name = cpu_to_le16p(&val); \ | |
167 | return len; \ | |
168 | } | |
169 | ||
170 | #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ | |
171 | GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ | |
172 | GI_DEVICE_DESC_SIMPLE_W_##_type(_name) | |
173 | ||
174 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); | |
175 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); | |
176 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); | |
177 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); | |
178 | GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); | |
179 | GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); | |
180 | GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); | |
181 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); | |
182 | ||
183 | static ssize_t is_valid_bcd(u16 bcd_val) | |
184 | { | |
185 | if ((bcd_val & 0xf) > 9) | |
186 | return -EINVAL; | |
187 | if (((bcd_val >> 4) & 0xf) > 9) | |
188 | return -EINVAL; | |
189 | if (((bcd_val >> 8) & 0xf) > 9) | |
190 | return -EINVAL; | |
191 | if (((bcd_val >> 12) & 0xf) > 9) | |
192 | return -EINVAL; | |
193 | return 0; | |
194 | } | |
195 | ||
196 | static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi, | |
197 | const char *page, size_t len) | |
198 | { | |
199 | u16 bcdDevice; | |
200 | int ret; | |
201 | ||
202 | ret = kstrtou16(page, 0, &bcdDevice); | |
203 | if (ret) | |
204 | return ret; | |
205 | ret = is_valid_bcd(bcdDevice); | |
206 | if (ret) | |
207 | return ret; | |
208 | ||
209 | gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); | |
210 | return len; | |
211 | } | |
212 | ||
213 | static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi, | |
214 | const char *page, size_t len) | |
215 | { | |
216 | u16 bcdUSB; | |
217 | int ret; | |
218 | ||
219 | ret = kstrtou16(page, 0, &bcdUSB); | |
220 | if (ret) | |
221 | return ret; | |
222 | ret = is_valid_bcd(bcdUSB); | |
223 | if (ret) | |
224 | return ret; | |
225 | ||
226 | gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); | |
227 | return len; | |
228 | } | |
229 | ||
230 | static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page) | |
231 | { | |
232 | return sprintf(page, "%s\n", gi->udc_name ?: ""); | |
233 | } | |
234 | ||
235 | static int unregister_gadget(struct gadget_info *gi) | |
236 | { | |
237 | int ret; | |
238 | ||
239 | if (!gi->udc_name) | |
240 | return -ENODEV; | |
241 | ||
242 | ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); | |
243 | if (ret) | |
244 | return ret; | |
245 | kfree(gi->udc_name); | |
246 | gi->udc_name = NULL; | |
247 | return 0; | |
248 | } | |
249 | ||
250 | static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi, | |
251 | const char *page, size_t len) | |
252 | { | |
253 | char *name; | |
254 | int ret; | |
255 | ||
256 | name = kstrdup(page, GFP_KERNEL); | |
257 | if (!name) | |
258 | return -ENOMEM; | |
259 | if (name[len - 1] == '\n') | |
260 | name[len - 1] = '\0'; | |
261 | ||
262 | mutex_lock(&gi->lock); | |
263 | ||
264 | if (!strlen(name)) { | |
265 | ret = unregister_gadget(gi); | |
266 | if (ret) | |
267 | goto err; | |
268 | } else { | |
269 | if (gi->udc_name) { | |
270 | ret = -EBUSY; | |
271 | goto err; | |
272 | } | |
02e8c966 | 273 | ret = usb_udc_attach_driver(name, &gi->composite.gadget_driver); |
88af8bbe SAS |
274 | if (ret) |
275 | goto err; | |
276 | gi->udc_name = name; | |
277 | } | |
278 | mutex_unlock(&gi->lock); | |
279 | return len; | |
280 | err: | |
281 | kfree(name); | |
282 | mutex_unlock(&gi->lock); | |
283 | return ret; | |
284 | } | |
285 | ||
286 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass); | |
287 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass); | |
288 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol); | |
289 | GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0); | |
290 | GI_DEVICE_DESC_ITEM_ATTR(idVendor); | |
291 | GI_DEVICE_DESC_ITEM_ATTR(idProduct); | |
292 | GI_DEVICE_DESC_ITEM_ATTR(bcdDevice); | |
293 | GI_DEVICE_DESC_ITEM_ATTR(bcdUSB); | |
294 | GI_DEVICE_DESC_ITEM_ATTR(UDC); | |
295 | ||
296 | static struct configfs_attribute *gadget_root_attrs[] = { | |
297 | &gadget_cdev_desc_bDeviceClass.attr, | |
298 | &gadget_cdev_desc_bDeviceSubClass.attr, | |
299 | &gadget_cdev_desc_bDeviceProtocol.attr, | |
300 | &gadget_cdev_desc_bMaxPacketSize0.attr, | |
301 | &gadget_cdev_desc_idVendor.attr, | |
302 | &gadget_cdev_desc_idProduct.attr, | |
303 | &gadget_cdev_desc_bcdDevice.attr, | |
304 | &gadget_cdev_desc_bcdUSB.attr, | |
305 | &gadget_cdev_desc_UDC.attr, | |
306 | NULL, | |
307 | }; | |
308 | ||
309 | static inline struct gadget_info *to_gadget_info(struct config_item *item) | |
310 | { | |
311 | return container_of(to_config_group(item), struct gadget_info, group); | |
312 | } | |
313 | ||
314 | static inline struct gadget_strings *to_gadget_strings(struct config_item *item) | |
315 | { | |
316 | return container_of(to_config_group(item), struct gadget_strings, | |
317 | group); | |
318 | } | |
319 | ||
320 | static inline struct gadget_config_name *to_gadget_config_name( | |
321 | struct config_item *item) | |
322 | { | |
323 | return container_of(to_config_group(item), struct gadget_config_name, | |
324 | group); | |
325 | } | |
326 | ||
327 | static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) | |
328 | { | |
329 | return container_of(to_config_group(item), struct config_usb_cfg, | |
330 | group); | |
331 | } | |
332 | ||
333 | static inline struct usb_function_instance *to_usb_function_instance( | |
334 | struct config_item *item) | |
335 | { | |
336 | return container_of(to_config_group(item), | |
337 | struct usb_function_instance, group); | |
338 | } | |
339 | ||
340 | static void gadget_info_attr_release(struct config_item *item) | |
341 | { | |
342 | struct gadget_info *gi = to_gadget_info(item); | |
343 | ||
344 | WARN_ON(!list_empty(&gi->cdev.configs)); | |
345 | WARN_ON(!list_empty(&gi->string_list)); | |
346 | WARN_ON(!list_empty(&gi->available_func)); | |
347 | kfree(gi->composite.gadget_driver.function); | |
348 | kfree(gi); | |
349 | } | |
350 | ||
351 | CONFIGFS_ATTR_OPS(gadget_info); | |
352 | ||
353 | static struct configfs_item_operations gadget_root_item_ops = { | |
354 | .release = gadget_info_attr_release, | |
355 | .show_attribute = gadget_info_attr_show, | |
356 | .store_attribute = gadget_info_attr_store, | |
357 | }; | |
358 | ||
359 | static void gadget_config_attr_release(struct config_item *item) | |
360 | { | |
361 | struct config_usb_cfg *cfg = to_config_usb_cfg(item); | |
362 | ||
363 | WARN_ON(!list_empty(&cfg->c.functions)); | |
364 | list_del(&cfg->c.list); | |
365 | kfree(cfg->c.label); | |
366 | kfree(cfg); | |
367 | } | |
368 | ||
369 | static int config_usb_cfg_link( | |
370 | struct config_item *usb_cfg_ci, | |
371 | struct config_item *usb_func_ci) | |
372 | { | |
373 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); | |
374 | struct usb_composite_dev *cdev = cfg->c.cdev; | |
375 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); | |
376 | ||
377 | struct config_group *group = to_config_group(usb_func_ci); | |
378 | struct usb_function_instance *fi = container_of(group, | |
379 | struct usb_function_instance, group); | |
380 | struct usb_function_instance *a_fi; | |
381 | struct usb_function *f; | |
382 | int ret; | |
383 | ||
384 | mutex_lock(&gi->lock); | |
385 | /* | |
386 | * Make sure this function is from within our _this_ gadget and not | |
387 | * from another gadget or a random directory. | |
388 | * Also a function instance can only be linked once. | |
389 | */ | |
390 | list_for_each_entry(a_fi, &gi->available_func, cfs_list) { | |
391 | if (a_fi == fi) | |
392 | break; | |
393 | } | |
394 | if (a_fi != fi) { | |
395 | ret = -EINVAL; | |
396 | goto out; | |
397 | } | |
398 | ||
399 | list_for_each_entry(f, &cfg->func_list, list) { | |
400 | if (f->fi == fi) { | |
401 | ret = -EEXIST; | |
402 | goto out; | |
403 | } | |
404 | } | |
405 | ||
406 | f = usb_get_function(fi); | |
407 | if (IS_ERR(f)) { | |
408 | ret = PTR_ERR(f); | |
409 | goto out; | |
410 | } | |
411 | ||
412 | /* stash the function until we bind it to the gadget */ | |
413 | list_add_tail(&f->list, &cfg->func_list); | |
414 | ret = 0; | |
415 | out: | |
416 | mutex_unlock(&gi->lock); | |
417 | return ret; | |
418 | } | |
419 | ||
420 | static int config_usb_cfg_unlink( | |
421 | struct config_item *usb_cfg_ci, | |
422 | struct config_item *usb_func_ci) | |
423 | { | |
424 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); | |
425 | struct usb_composite_dev *cdev = cfg->c.cdev; | |
426 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); | |
427 | ||
428 | struct config_group *group = to_config_group(usb_func_ci); | |
429 | struct usb_function_instance *fi = container_of(group, | |
430 | struct usb_function_instance, group); | |
431 | struct usb_function *f; | |
432 | ||
433 | /* | |
434 | * ideally I would like to forbid to unlink functions while a gadget is | |
435 | * bound to an UDC. Since this isn't possible at the moment, we simply | |
436 | * force an unbind, the function is available here and then we can | |
437 | * remove the function. | |
438 | */ | |
439 | mutex_lock(&gi->lock); | |
440 | if (gi->udc_name) | |
441 | unregister_gadget(gi); | |
442 | WARN_ON(gi->udc_name); | |
443 | ||
444 | list_for_each_entry(f, &cfg->func_list, list) { | |
445 | if (f->fi == fi) { | |
446 | list_del(&f->list); | |
447 | usb_put_function(f); | |
448 | mutex_unlock(&gi->lock); | |
449 | return 0; | |
450 | } | |
451 | } | |
452 | mutex_unlock(&gi->lock); | |
75bfe23a | 453 | WARN(1, "Unable to locate function to unbind\n"); |
88af8bbe SAS |
454 | return 0; |
455 | } | |
456 | ||
457 | CONFIGFS_ATTR_OPS(config_usb_cfg); | |
458 | ||
459 | static struct configfs_item_operations gadget_config_item_ops = { | |
460 | .release = gadget_config_attr_release, | |
461 | .show_attribute = config_usb_cfg_attr_show, | |
462 | .store_attribute = config_usb_cfg_attr_store, | |
463 | .allow_link = config_usb_cfg_link, | |
464 | .drop_link = config_usb_cfg_unlink, | |
465 | }; | |
466 | ||
467 | ||
468 | static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg, | |
469 | char *page) | |
470 | { | |
471 | return sprintf(page, "%u\n", cfg->c.MaxPower); | |
472 | } | |
473 | ||
474 | static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg, | |
475 | const char *page, size_t len) | |
476 | { | |
477 | u16 val; | |
478 | int ret; | |
479 | ret = kstrtou16(page, 0, &val); | |
480 | if (ret) | |
481 | return ret; | |
482 | if (DIV_ROUND_UP(val, 8) > 0xff) | |
483 | return -ERANGE; | |
484 | cfg->c.MaxPower = val; | |
485 | return len; | |
486 | } | |
487 | ||
488 | static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg, | |
489 | char *page) | |
490 | { | |
491 | return sprintf(page, "0x%02x\n", cfg->c.bmAttributes); | |
492 | } | |
493 | ||
494 | static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg, | |
495 | const char *page, size_t len) | |
496 | { | |
497 | u8 val; | |
498 | int ret; | |
499 | ret = kstrtou8(page, 0, &val); | |
500 | if (ret) | |
501 | return ret; | |
502 | if (!(val & USB_CONFIG_ATT_ONE)) | |
503 | return -EINVAL; | |
504 | if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | | |
505 | USB_CONFIG_ATT_WAKEUP)) | |
506 | return -EINVAL; | |
507 | cfg->c.bmAttributes = val; | |
508 | return len; | |
509 | } | |
510 | ||
511 | #define CFG_CONFIG_DESC_ITEM_ATTR(name) \ | |
512 | static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \ | |
513 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
514 | gadget_config_desc_##name##_show, \ | |
515 | gadget_config_desc_##name##_store) | |
516 | ||
517 | CFG_CONFIG_DESC_ITEM_ATTR(MaxPower); | |
518 | CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes); | |
519 | ||
520 | static struct configfs_attribute *gadget_config_attrs[] = { | |
521 | &gadget_usb_cfg_MaxPower.attr, | |
522 | &gadget_usb_cfg_bmAttributes.attr, | |
523 | NULL, | |
524 | }; | |
525 | ||
526 | static struct config_item_type gadget_config_type = { | |
527 | .ct_item_ops = &gadget_config_item_ops, | |
528 | .ct_attrs = gadget_config_attrs, | |
529 | .ct_owner = THIS_MODULE, | |
530 | }; | |
531 | ||
532 | static struct config_item_type gadget_root_type = { | |
533 | .ct_item_ops = &gadget_root_item_ops, | |
534 | .ct_attrs = gadget_root_attrs, | |
535 | .ct_owner = THIS_MODULE, | |
536 | }; | |
537 | ||
538 | static void composite_init_dev(struct usb_composite_dev *cdev) | |
539 | { | |
540 | spin_lock_init(&cdev->lock); | |
541 | INIT_LIST_HEAD(&cdev->configs); | |
542 | INIT_LIST_HEAD(&cdev->gstrings); | |
543 | } | |
544 | ||
545 | static struct config_group *function_make( | |
546 | struct config_group *group, | |
547 | const char *name) | |
548 | { | |
549 | struct gadget_info *gi; | |
550 | struct usb_function_instance *fi; | |
551 | char buf[MAX_NAME_LEN]; | |
552 | char *func_name; | |
553 | char *instance_name; | |
554 | int ret; | |
555 | ||
556 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); | |
557 | if (ret >= MAX_NAME_LEN) | |
558 | return ERR_PTR(-ENAMETOOLONG); | |
559 | ||
560 | func_name = buf; | |
561 | instance_name = strchr(func_name, '.'); | |
562 | if (!instance_name) { | |
563 | pr_err("Unable to locate . in FUNC.INSTANCE\n"); | |
564 | return ERR_PTR(-EINVAL); | |
565 | } | |
566 | *instance_name = '\0'; | |
567 | instance_name++; | |
568 | ||
569 | fi = usb_get_function_instance(func_name); | |
570 | if (IS_ERR(fi)) | |
a3469411 | 571 | return ERR_CAST(fi); |
88af8bbe | 572 | |
3958b792 | 573 | ret = config_item_set_name(&fi->group.cg_item, "%s", name); |
88af8bbe SAS |
574 | if (ret) { |
575 | usb_put_function_instance(fi); | |
576 | return ERR_PTR(ret); | |
577 | } | |
1933861d AP |
578 | if (fi->set_inst_name) { |
579 | ret = fi->set_inst_name(fi, instance_name); | |
580 | if (ret) { | |
581 | usb_put_function_instance(fi); | |
582 | return ERR_PTR(ret); | |
583 | } | |
584 | } | |
88af8bbe SAS |
585 | |
586 | gi = container_of(group, struct gadget_info, functions_group); | |
587 | ||
588 | mutex_lock(&gi->lock); | |
589 | list_add_tail(&fi->cfs_list, &gi->available_func); | |
590 | mutex_unlock(&gi->lock); | |
591 | return &fi->group; | |
592 | } | |
593 | ||
594 | static void function_drop( | |
595 | struct config_group *group, | |
596 | struct config_item *item) | |
597 | { | |
598 | struct usb_function_instance *fi = to_usb_function_instance(item); | |
599 | struct gadget_info *gi; | |
600 | ||
601 | gi = container_of(group, struct gadget_info, functions_group); | |
602 | ||
603 | mutex_lock(&gi->lock); | |
604 | list_del(&fi->cfs_list); | |
605 | mutex_unlock(&gi->lock); | |
606 | config_item_put(item); | |
607 | } | |
608 | ||
609 | static struct configfs_group_operations functions_ops = { | |
610 | .make_group = &function_make, | |
611 | .drop_item = &function_drop, | |
612 | }; | |
613 | ||
614 | static struct config_item_type functions_type = { | |
615 | .ct_group_ops = &functions_ops, | |
616 | .ct_owner = THIS_MODULE, | |
617 | }; | |
618 | ||
619 | CONFIGFS_ATTR_STRUCT(gadget_config_name); | |
620 | GS_STRINGS_RW(gadget_config_name, configuration); | |
621 | ||
622 | static struct configfs_attribute *gadget_config_name_langid_attrs[] = { | |
623 | &gadget_config_name_configuration.attr, | |
624 | NULL, | |
625 | }; | |
626 | ||
627 | static void gadget_config_name_attr_release(struct config_item *item) | |
628 | { | |
629 | struct gadget_config_name *cn = to_gadget_config_name(item); | |
630 | ||
631 | kfree(cn->configuration); | |
632 | ||
633 | list_del(&cn->list); | |
634 | kfree(cn); | |
635 | } | |
636 | ||
637 | USB_CONFIG_STRING_RW_OPS(gadget_config_name); | |
638 | USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); | |
639 | ||
640 | static struct config_group *config_desc_make( | |
641 | struct config_group *group, | |
642 | const char *name) | |
643 | { | |
644 | struct gadget_info *gi; | |
645 | struct config_usb_cfg *cfg; | |
646 | char buf[MAX_NAME_LEN]; | |
647 | char *num_str; | |
648 | u8 num; | |
649 | int ret; | |
650 | ||
651 | gi = container_of(group, struct gadget_info, configs_group); | |
652 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); | |
653 | if (ret >= MAX_NAME_LEN) | |
654 | return ERR_PTR(-ENAMETOOLONG); | |
655 | ||
656 | num_str = strchr(buf, '.'); | |
657 | if (!num_str) { | |
658 | pr_err("Unable to locate . in name.bConfigurationValue\n"); | |
659 | return ERR_PTR(-EINVAL); | |
660 | } | |
661 | ||
662 | *num_str = '\0'; | |
663 | num_str++; | |
664 | ||
665 | if (!strlen(buf)) | |
666 | return ERR_PTR(-EINVAL); | |
667 | ||
668 | ret = kstrtou8(num_str, 0, &num); | |
669 | if (ret) | |
670 | return ERR_PTR(ret); | |
671 | ||
672 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); | |
673 | if (!cfg) | |
674 | return ERR_PTR(-ENOMEM); | |
675 | cfg->c.label = kstrdup(buf, GFP_KERNEL); | |
676 | if (!cfg->c.label) { | |
677 | ret = -ENOMEM; | |
678 | goto err; | |
679 | } | |
680 | cfg->c.bConfigurationValue = num; | |
681 | cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; | |
682 | cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; | |
683 | INIT_LIST_HEAD(&cfg->string_list); | |
684 | INIT_LIST_HEAD(&cfg->func_list); | |
685 | ||
686 | cfg->group.default_groups = cfg->default_groups; | |
687 | cfg->default_groups[0] = &cfg->strings_group; | |
688 | ||
689 | config_group_init_type_name(&cfg->group, name, | |
690 | &gadget_config_type); | |
691 | config_group_init_type_name(&cfg->strings_group, "strings", | |
692 | &gadget_config_name_strings_type); | |
693 | ||
694 | ret = usb_add_config_only(&gi->cdev, &cfg->c); | |
695 | if (ret) | |
696 | goto err; | |
697 | ||
698 | return &cfg->group; | |
699 | err: | |
700 | kfree(cfg->c.label); | |
701 | kfree(cfg); | |
702 | return ERR_PTR(ret); | |
703 | } | |
704 | ||
705 | static void config_desc_drop( | |
706 | struct config_group *group, | |
707 | struct config_item *item) | |
708 | { | |
709 | config_item_put(item); | |
710 | } | |
711 | ||
712 | static struct configfs_group_operations config_desc_ops = { | |
713 | .make_group = &config_desc_make, | |
714 | .drop_item = &config_desc_drop, | |
715 | }; | |
716 | ||
717 | static struct config_item_type config_desc_type = { | |
718 | .ct_group_ops = &config_desc_ops, | |
719 | .ct_owner = THIS_MODULE, | |
720 | }; | |
721 | ||
722 | CONFIGFS_ATTR_STRUCT(gadget_strings); | |
723 | GS_STRINGS_RW(gadget_strings, manufacturer); | |
724 | GS_STRINGS_RW(gadget_strings, product); | |
725 | GS_STRINGS_RW(gadget_strings, serialnumber); | |
726 | ||
727 | static struct configfs_attribute *gadget_strings_langid_attrs[] = { | |
728 | &gadget_strings_manufacturer.attr, | |
729 | &gadget_strings_product.attr, | |
730 | &gadget_strings_serialnumber.attr, | |
731 | NULL, | |
732 | }; | |
733 | ||
734 | static void gadget_strings_attr_release(struct config_item *item) | |
735 | { | |
736 | struct gadget_strings *gs = to_gadget_strings(item); | |
737 | ||
738 | kfree(gs->manufacturer); | |
739 | kfree(gs->product); | |
740 | kfree(gs->serialnumber); | |
741 | ||
742 | list_del(&gs->list); | |
743 | kfree(gs); | |
744 | } | |
745 | ||
746 | USB_CONFIG_STRING_RW_OPS(gadget_strings); | |
747 | USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); | |
748 | ||
87213d38 AP |
749 | static inline struct os_desc *to_os_desc(struct config_item *item) |
750 | { | |
751 | return container_of(to_config_group(item), struct os_desc, group); | |
752 | } | |
753 | ||
754 | CONFIGFS_ATTR_STRUCT(os_desc); | |
755 | CONFIGFS_ATTR_OPS(os_desc); | |
756 | ||
757 | static ssize_t os_desc_use_show(struct os_desc *os_desc, char *page) | |
758 | { | |
759 | struct gadget_info *gi; | |
760 | ||
761 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
762 | ||
763 | return sprintf(page, "%d", gi->use_os_desc); | |
764 | } | |
765 | ||
766 | static ssize_t os_desc_use_store(struct os_desc *os_desc, const char *page, | |
767 | size_t len) | |
768 | { | |
769 | struct gadget_info *gi; | |
770 | int ret; | |
771 | bool use; | |
772 | ||
773 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
774 | ||
775 | mutex_lock(&gi->lock); | |
776 | ret = strtobool(page, &use); | |
777 | if (!ret) { | |
778 | gi->use_os_desc = use; | |
779 | ret = len; | |
780 | } | |
781 | mutex_unlock(&gi->lock); | |
782 | ||
783 | return ret; | |
784 | } | |
785 | ||
786 | static struct os_desc_attribute os_desc_use = | |
787 | __CONFIGFS_ATTR(use, S_IRUGO | S_IWUSR, | |
788 | os_desc_use_show, | |
789 | os_desc_use_store); | |
790 | ||
791 | static ssize_t os_desc_b_vendor_code_show(struct os_desc *os_desc, char *page) | |
792 | { | |
793 | struct gadget_info *gi; | |
794 | ||
795 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
796 | ||
797 | return sprintf(page, "%d", gi->b_vendor_code); | |
798 | } | |
799 | ||
800 | static ssize_t os_desc_b_vendor_code_store(struct os_desc *os_desc, | |
801 | const char *page, size_t len) | |
802 | { | |
803 | struct gadget_info *gi; | |
804 | int ret; | |
805 | u8 b_vendor_code; | |
806 | ||
807 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
808 | ||
809 | mutex_lock(&gi->lock); | |
810 | ret = kstrtou8(page, 0, &b_vendor_code); | |
811 | if (!ret) { | |
812 | gi->b_vendor_code = b_vendor_code; | |
813 | ret = len; | |
814 | } | |
815 | mutex_unlock(&gi->lock); | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
820 | static struct os_desc_attribute os_desc_b_vendor_code = | |
821 | __CONFIGFS_ATTR(b_vendor_code, S_IRUGO | S_IWUSR, | |
822 | os_desc_b_vendor_code_show, | |
823 | os_desc_b_vendor_code_store); | |
824 | ||
825 | static ssize_t os_desc_qw_sign_show(struct os_desc *os_desc, char *page) | |
826 | { | |
827 | struct gadget_info *gi; | |
828 | ||
829 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
830 | ||
831 | memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN); | |
832 | ||
833 | return OS_STRING_QW_SIGN_LEN; | |
834 | } | |
835 | ||
836 | static ssize_t os_desc_qw_sign_store(struct os_desc *os_desc, const char *page, | |
837 | size_t len) | |
838 | { | |
839 | struct gadget_info *gi; | |
840 | int res, l; | |
841 | ||
842 | gi = to_gadget_info(os_desc->group.cg_item.ci_parent); | |
843 | l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1); | |
844 | if (page[l - 1] == '\n') | |
845 | --l; | |
846 | ||
847 | mutex_lock(&gi->lock); | |
848 | res = utf8s_to_utf16s(page, l, | |
849 | UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign, | |
850 | OS_STRING_QW_SIGN_LEN); | |
851 | if (res > 0) | |
852 | res = len; | |
853 | mutex_unlock(&gi->lock); | |
854 | ||
855 | return res; | |
856 | } | |
857 | ||
858 | static struct os_desc_attribute os_desc_qw_sign = | |
859 | __CONFIGFS_ATTR(qw_sign, S_IRUGO | S_IWUSR, | |
860 | os_desc_qw_sign_show, | |
861 | os_desc_qw_sign_store); | |
862 | ||
863 | static struct configfs_attribute *os_desc_attrs[] = { | |
864 | &os_desc_use.attr, | |
865 | &os_desc_b_vendor_code.attr, | |
866 | &os_desc_qw_sign.attr, | |
867 | NULL, | |
868 | }; | |
869 | ||
870 | static void os_desc_attr_release(struct config_item *item) | |
871 | { | |
872 | struct os_desc *os_desc = to_os_desc(item); | |
873 | kfree(os_desc); | |
874 | } | |
875 | ||
da424314 AP |
876 | static int os_desc_link(struct config_item *os_desc_ci, |
877 | struct config_item *usb_cfg_ci) | |
878 | { | |
879 | struct gadget_info *gi = container_of(to_config_group(os_desc_ci), | |
880 | struct gadget_info, os_desc_group); | |
881 | struct usb_composite_dev *cdev = &gi->cdev; | |
882 | struct config_usb_cfg *c_target = | |
883 | container_of(to_config_group(usb_cfg_ci), | |
884 | struct config_usb_cfg, group); | |
885 | struct usb_configuration *c; | |
886 | int ret; | |
887 | ||
888 | mutex_lock(&gi->lock); | |
889 | list_for_each_entry(c, &cdev->configs, list) { | |
890 | if (c == &c_target->c) | |
891 | break; | |
892 | } | |
893 | if (c != &c_target->c) { | |
894 | ret = -EINVAL; | |
895 | goto out; | |
896 | } | |
897 | ||
898 | if (cdev->os_desc_config) { | |
899 | ret = -EBUSY; | |
900 | goto out; | |
901 | } | |
902 | ||
903 | cdev->os_desc_config = &c_target->c; | |
904 | ret = 0; | |
905 | ||
906 | out: | |
907 | mutex_unlock(&gi->lock); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | static int os_desc_unlink(struct config_item *os_desc_ci, | |
912 | struct config_item *usb_cfg_ci) | |
913 | { | |
914 | struct gadget_info *gi = container_of(to_config_group(os_desc_ci), | |
915 | struct gadget_info, os_desc_group); | |
916 | struct usb_composite_dev *cdev = &gi->cdev; | |
917 | ||
918 | mutex_lock(&gi->lock); | |
919 | if (gi->udc_name) | |
920 | unregister_gadget(gi); | |
921 | cdev->os_desc_config = NULL; | |
922 | WARN_ON(gi->udc_name); | |
923 | mutex_unlock(&gi->lock); | |
924 | return 0; | |
925 | } | |
926 | ||
87213d38 AP |
927 | static struct configfs_item_operations os_desc_ops = { |
928 | .release = os_desc_attr_release, | |
929 | .show_attribute = os_desc_attr_show, | |
930 | .store_attribute = os_desc_attr_store, | |
da424314 AP |
931 | .allow_link = os_desc_link, |
932 | .drop_link = os_desc_unlink, | |
87213d38 AP |
933 | }; |
934 | ||
935 | static struct config_item_type os_desc_type = { | |
936 | .ct_item_ops = &os_desc_ops, | |
937 | .ct_attrs = os_desc_attrs, | |
938 | .ct_owner = THIS_MODULE, | |
939 | }; | |
940 | ||
da424314 AP |
941 | CONFIGFS_ATTR_STRUCT(usb_os_desc); |
942 | CONFIGFS_ATTR_OPS(usb_os_desc); | |
943 | ||
7419485f AP |
944 | |
945 | static 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 | ||
951 | CONFIGFS_ATTR_STRUCT(usb_os_desc_ext_prop); | |
952 | CONFIGFS_ATTR_OPS(usb_os_desc_ext_prop); | |
953 | ||
954 | static ssize_t ext_prop_type_show(struct usb_os_desc_ext_prop *ext_prop, | |
955 | char *page) | |
956 | { | |
957 | return sprintf(page, "%d", ext_prop->type); | |
958 | } | |
959 | ||
960 | static ssize_t ext_prop_type_store(struct usb_os_desc_ext_prop *ext_prop, | |
961 | const char *page, size_t len) | |
962 | { | |
963 | struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); | |
964 | u8 type; | |
965 | int ret; | |
966 | ||
967 | if (desc->opts_mutex) | |
968 | mutex_lock(desc->opts_mutex); | |
969 | ret = kstrtou8(page, 0, &type); | |
970 | if (ret) | |
971 | goto end; | |
972 | if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) { | |
973 | ret = -EINVAL; | |
974 | goto end; | |
975 | } | |
976 | ||
977 | if ((ext_prop->type == USB_EXT_PROP_BINARY || | |
978 | ext_prop->type == USB_EXT_PROP_LE32 || | |
979 | ext_prop->type == USB_EXT_PROP_BE32) && | |
980 | (type == USB_EXT_PROP_UNICODE || | |
981 | type == USB_EXT_PROP_UNICODE_ENV || | |
982 | type == USB_EXT_PROP_UNICODE_LINK)) | |
983 | ext_prop->data_len <<= 1; | |
984 | else if ((ext_prop->type == USB_EXT_PROP_UNICODE || | |
985 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || | |
986 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) && | |
987 | (type == USB_EXT_PROP_BINARY || | |
988 | type == USB_EXT_PROP_LE32 || | |
989 | type == USB_EXT_PROP_BE32)) | |
990 | ext_prop->data_len >>= 1; | |
991 | ext_prop->type = type; | |
992 | ret = len; | |
993 | ||
994 | end: | |
995 | if (desc->opts_mutex) | |
996 | mutex_unlock(desc->opts_mutex); | |
997 | return ret; | |
998 | } | |
999 | ||
1000 | static ssize_t ext_prop_data_show(struct usb_os_desc_ext_prop *ext_prop, | |
1001 | char *page) | |
1002 | { | |
1003 | int len = ext_prop->data_len; | |
1004 | ||
1005 | if (ext_prop->type == USB_EXT_PROP_UNICODE || | |
1006 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || | |
1007 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) | |
1008 | len >>= 1; | |
1009 | memcpy(page, ext_prop->data, len); | |
1010 | ||
1011 | return len; | |
1012 | } | |
1013 | ||
1014 | static ssize_t ext_prop_data_store(struct usb_os_desc_ext_prop *ext_prop, | |
1015 | const char *page, size_t len) | |
1016 | { | |
1017 | struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); | |
1018 | char *new_data; | |
1019 | size_t ret_len = len; | |
1020 | ||
1021 | if (page[len - 1] == '\n' || page[len - 1] == '\0') | |
1022 | --len; | |
58b949e0 | 1023 | new_data = kmemdup(page, len, GFP_KERNEL); |
7419485f AP |
1024 | if (!new_data) |
1025 | return -ENOMEM; | |
1026 | ||
7419485f AP |
1027 | if (desc->opts_mutex) |
1028 | mutex_lock(desc->opts_mutex); | |
1029 | kfree(ext_prop->data); | |
1030 | ext_prop->data = new_data; | |
1031 | desc->ext_prop_len -= ext_prop->data_len; | |
1032 | ext_prop->data_len = len; | |
1033 | desc->ext_prop_len += ext_prop->data_len; | |
1034 | if (ext_prop->type == USB_EXT_PROP_UNICODE || | |
1035 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || | |
1036 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) { | |
1037 | desc->ext_prop_len -= ext_prop->data_len; | |
1038 | ext_prop->data_len <<= 1; | |
1039 | ext_prop->data_len += 2; | |
1040 | desc->ext_prop_len += ext_prop->data_len; | |
1041 | } | |
1042 | if (desc->opts_mutex) | |
1043 | mutex_unlock(desc->opts_mutex); | |
1044 | return ret_len; | |
1045 | } | |
1046 | ||
1047 | static struct usb_os_desc_ext_prop_attribute ext_prop_type = | |
1048 | __CONFIGFS_ATTR(type, S_IRUGO | S_IWUSR, | |
1049 | ext_prop_type_show, ext_prop_type_store); | |
1050 | ||
1051 | static struct usb_os_desc_ext_prop_attribute ext_prop_data = | |
1052 | __CONFIGFS_ATTR(data, S_IRUGO | S_IWUSR, | |
1053 | ext_prop_data_show, ext_prop_data_store); | |
1054 | ||
1055 | static struct configfs_attribute *ext_prop_attrs[] = { | |
1056 | &ext_prop_type.attr, | |
1057 | &ext_prop_data.attr, | |
1058 | NULL, | |
1059 | }; | |
1060 | ||
1061 | static void usb_os_desc_ext_prop_release(struct config_item *item) | |
1062 | { | |
1063 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); | |
1064 | ||
1065 | kfree(ext_prop); /* frees a whole chunk */ | |
1066 | } | |
1067 | ||
1068 | static struct configfs_item_operations ext_prop_ops = { | |
1069 | .release = usb_os_desc_ext_prop_release, | |
1070 | .show_attribute = usb_os_desc_ext_prop_attr_show, | |
1071 | .store_attribute = usb_os_desc_ext_prop_attr_store, | |
1072 | }; | |
1073 | ||
1074 | static struct config_item *ext_prop_make( | |
1075 | struct config_group *group, | |
1076 | const char *name) | |
1077 | { | |
1078 | struct usb_os_desc_ext_prop *ext_prop; | |
1079 | struct config_item_type *ext_prop_type; | |
1080 | struct usb_os_desc *desc; | |
1081 | char *vlabuf; | |
1082 | ||
1083 | vla_group(data_chunk); | |
1084 | vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1); | |
1085 | vla_item(data_chunk, struct config_item_type, ext_prop_type, 1); | |
1086 | ||
1087 | vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); | |
1088 | if (!vlabuf) | |
1089 | return ERR_PTR(-ENOMEM); | |
1090 | ||
1091 | ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop); | |
1092 | ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type); | |
1093 | ||
1094 | desc = container_of(group, struct usb_os_desc, group); | |
1095 | ext_prop_type->ct_item_ops = &ext_prop_ops; | |
1096 | ext_prop_type->ct_attrs = ext_prop_attrs; | |
1097 | ext_prop_type->ct_owner = desc->owner; | |
1098 | ||
1099 | config_item_init_type_name(&ext_prop->item, name, ext_prop_type); | |
1100 | ||
1101 | ext_prop->name = kstrdup(name, GFP_KERNEL); | |
1102 | if (!ext_prop->name) { | |
1103 | kfree(vlabuf); | |
1104 | return ERR_PTR(-ENOMEM); | |
1105 | } | |
1106 | desc->ext_prop_len += 14; | |
1107 | ext_prop->name_len = 2 * strlen(ext_prop->name) + 2; | |
1108 | if (desc->opts_mutex) | |
1109 | mutex_lock(desc->opts_mutex); | |
1110 | desc->ext_prop_len += ext_prop->name_len; | |
1111 | list_add_tail(&ext_prop->entry, &desc->ext_prop); | |
1112 | ++desc->ext_prop_count; | |
1113 | if (desc->opts_mutex) | |
1114 | mutex_unlock(desc->opts_mutex); | |
1115 | ||
1116 | return &ext_prop->item; | |
1117 | } | |
1118 | ||
1119 | static void ext_prop_drop(struct config_group *group, struct config_item *item) | |
1120 | { | |
1121 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); | |
1122 | struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item); | |
1123 | ||
1124 | if (desc->opts_mutex) | |
1125 | mutex_lock(desc->opts_mutex); | |
1126 | list_del(&ext_prop->entry); | |
1127 | --desc->ext_prop_count; | |
1128 | kfree(ext_prop->name); | |
1129 | desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14); | |
1130 | if (desc->opts_mutex) | |
1131 | mutex_unlock(desc->opts_mutex); | |
1132 | config_item_put(item); | |
1133 | } | |
1134 | ||
1135 | static struct configfs_group_operations interf_grp_ops = { | |
1136 | .make_item = &ext_prop_make, | |
1137 | .drop_item = &ext_prop_drop, | |
1138 | }; | |
1139 | ||
da424314 AP |
1140 | static struct configfs_item_operations interf_item_ops = { |
1141 | .show_attribute = usb_os_desc_attr_show, | |
1142 | .store_attribute = usb_os_desc_attr_store, | |
1143 | }; | |
1144 | ||
fe00b138 AP |
1145 | static ssize_t interf_grp_compatible_id_show(struct usb_os_desc *desc, |
1146 | char *page) | |
da424314 AP |
1147 | { |
1148 | memcpy(page, desc->ext_compat_id, 8); | |
1149 | return 8; | |
1150 | } | |
1151 | ||
fe00b138 AP |
1152 | static ssize_t interf_grp_compatible_id_store(struct usb_os_desc *desc, |
1153 | const char *page, size_t len) | |
da424314 AP |
1154 | { |
1155 | int l; | |
1156 | ||
1157 | l = min_t(int, 8, len); | |
1158 | if (page[l - 1] == '\n') | |
1159 | --l; | |
1160 | if (desc->opts_mutex) | |
1161 | mutex_lock(desc->opts_mutex); | |
1162 | memcpy(desc->ext_compat_id, page, l); | |
da424314 AP |
1163 | |
1164 | if (desc->opts_mutex) | |
1165 | mutex_unlock(desc->opts_mutex); | |
1166 | ||
1167 | return len; | |
1168 | } | |
1169 | ||
fe00b138 | 1170 | static struct usb_os_desc_attribute interf_grp_attr_compatible_id = |
da424314 | 1171 | __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR, |
fe00b138 AP |
1172 | interf_grp_compatible_id_show, |
1173 | interf_grp_compatible_id_store); | |
da424314 | 1174 | |
fe00b138 AP |
1175 | static ssize_t interf_grp_sub_compatible_id_show(struct usb_os_desc *desc, |
1176 | char *page) | |
da424314 AP |
1177 | { |
1178 | memcpy(page, desc->ext_compat_id + 8, 8); | |
1179 | return 8; | |
1180 | } | |
1181 | ||
fe00b138 AP |
1182 | static ssize_t interf_grp_sub_compatible_id_store(struct usb_os_desc *desc, |
1183 | const char *page, size_t len) | |
da424314 AP |
1184 | { |
1185 | int l; | |
1186 | ||
1187 | l = min_t(int, 8, len); | |
1188 | if (page[l - 1] == '\n') | |
1189 | --l; | |
1190 | if (desc->opts_mutex) | |
1191 | mutex_lock(desc->opts_mutex); | |
1192 | memcpy(desc->ext_compat_id + 8, page, l); | |
da424314 AP |
1193 | |
1194 | if (desc->opts_mutex) | |
1195 | mutex_unlock(desc->opts_mutex); | |
1196 | ||
1197 | return len; | |
1198 | } | |
1199 | ||
fe00b138 | 1200 | static struct usb_os_desc_attribute interf_grp_attr_sub_compatible_id = |
da424314 | 1201 | __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR, |
fe00b138 AP |
1202 | interf_grp_sub_compatible_id_show, |
1203 | interf_grp_sub_compatible_id_store); | |
da424314 AP |
1204 | |
1205 | static struct configfs_attribute *interf_grp_attrs[] = { | |
fe00b138 AP |
1206 | &interf_grp_attr_compatible_id.attr, |
1207 | &interf_grp_attr_sub_compatible_id.attr, | |
da424314 AP |
1208 | NULL |
1209 | }; | |
1210 | ||
1211 | int usb_os_desc_prepare_interf_dir(struct config_group *parent, | |
1212 | int n_interf, | |
1213 | struct usb_os_desc **desc, | |
14574b54 | 1214 | char **names, |
da424314 AP |
1215 | struct module *owner) |
1216 | { | |
1217 | struct config_group **f_default_groups, *os_desc_group, | |
1218 | **interface_groups; | |
1219 | struct config_item_type *os_desc_type, *interface_type; | |
1220 | ||
1221 | vla_group(data_chunk); | |
1222 | vla_item(data_chunk, struct config_group *, f_default_groups, 2); | |
1223 | vla_item(data_chunk, struct config_group, os_desc_group, 1); | |
1224 | vla_item(data_chunk, struct config_group *, interface_groups, | |
1225 | n_interf + 1); | |
1226 | vla_item(data_chunk, struct config_item_type, os_desc_type, 1); | |
1227 | vla_item(data_chunk, struct config_item_type, interface_type, 1); | |
1228 | ||
1229 | char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); | |
1230 | if (!vlabuf) | |
1231 | return -ENOMEM; | |
1232 | ||
1233 | f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups); | |
1234 | os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group); | |
1235 | os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type); | |
1236 | interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups); | |
1237 | interface_type = vla_ptr(vlabuf, data_chunk, interface_type); | |
1238 | ||
1239 | parent->default_groups = f_default_groups; | |
1240 | os_desc_type->ct_owner = owner; | |
1241 | config_group_init_type_name(os_desc_group, "os_desc", os_desc_type); | |
1242 | f_default_groups[0] = os_desc_group; | |
1243 | ||
1244 | os_desc_group->default_groups = interface_groups; | |
1245 | interface_type->ct_item_ops = &interf_item_ops; | |
7419485f | 1246 | interface_type->ct_group_ops = &interf_grp_ops; |
da424314 AP |
1247 | interface_type->ct_attrs = interf_grp_attrs; |
1248 | interface_type->ct_owner = owner; | |
1249 | ||
1250 | while (n_interf--) { | |
1251 | struct usb_os_desc *d; | |
1252 | ||
1253 | d = desc[n_interf]; | |
7419485f | 1254 | d->owner = owner; |
da424314 | 1255 | config_group_init_type_name(&d->group, "", interface_type); |
14574b54 AP |
1256 | config_item_set_name(&d->group.cg_item, "interface.%s", |
1257 | names[n_interf]); | |
da424314 AP |
1258 | interface_groups[n_interf] = &d->group; |
1259 | } | |
1260 | ||
1261 | return 0; | |
1262 | } | |
1263 | EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir); | |
1264 | ||
88af8bbe SAS |
1265 | static int configfs_do_nothing(struct usb_composite_dev *cdev) |
1266 | { | |
75bfe23a | 1267 | WARN_ON(1); |
88af8bbe SAS |
1268 | return -EINVAL; |
1269 | } | |
1270 | ||
1271 | int composite_dev_prepare(struct usb_composite_driver *composite, | |
1272 | struct usb_composite_dev *dev); | |
1273 | ||
da424314 AP |
1274 | int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, |
1275 | struct usb_ep *ep0); | |
1276 | ||
88af8bbe SAS |
1277 | static void purge_configs_funcs(struct gadget_info *gi) |
1278 | { | |
1279 | struct usb_configuration *c; | |
1280 | ||
1281 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
1282 | struct usb_function *f, *tmp; | |
1283 | struct config_usb_cfg *cfg; | |
1284 | ||
1285 | cfg = container_of(c, struct config_usb_cfg, c); | |
1286 | ||
1287 | list_for_each_entry_safe(f, tmp, &c->functions, list) { | |
1288 | ||
1289 | list_move_tail(&f->list, &cfg->func_list); | |
1290 | if (f->unbind) { | |
1291 | dev_err(&gi->cdev.gadget->dev, "unbind function" | |
1292 | " '%s'/%p\n", f->name, f); | |
1293 | f->unbind(c, f); | |
1294 | } | |
1295 | } | |
1296 | c->next_interface_id = 0; | |
903124fe | 1297 | memset(c->interface, 0, sizeof(c->interface)); |
88af8bbe SAS |
1298 | c->superspeed = 0; |
1299 | c->highspeed = 0; | |
1300 | c->fullspeed = 0; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | static int configfs_composite_bind(struct usb_gadget *gadget, | |
1305 | struct usb_gadget_driver *gdriver) | |
1306 | { | |
1307 | struct usb_composite_driver *composite = to_cdriver(gdriver); | |
1308 | struct gadget_info *gi = container_of(composite, | |
1309 | struct gadget_info, composite); | |
1310 | struct usb_composite_dev *cdev = &gi->cdev; | |
1311 | struct usb_configuration *c; | |
1312 | struct usb_string *s; | |
1313 | unsigned i; | |
1314 | int ret; | |
1315 | ||
1316 | /* the gi->lock is hold by the caller */ | |
1317 | cdev->gadget = gadget; | |
1318 | set_gadget_data(gadget, cdev); | |
1319 | ret = composite_dev_prepare(composite, cdev); | |
1320 | if (ret) | |
1321 | return ret; | |
1322 | /* and now the gadget bind */ | |
1323 | ret = -EINVAL; | |
1324 | ||
1325 | if (list_empty(&gi->cdev.configs)) { | |
4d9f872c | 1326 | pr_err("Need at least one configuration in %s.\n", |
88af8bbe SAS |
1327 | gi->composite.name); |
1328 | goto err_comp_cleanup; | |
1329 | } | |
1330 | ||
1331 | ||
1332 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
1333 | struct config_usb_cfg *cfg; | |
1334 | ||
1335 | cfg = container_of(c, struct config_usb_cfg, c); | |
1336 | if (list_empty(&cfg->func_list)) { | |
4d9f872c | 1337 | pr_err("Config %s/%d of %s needs at least one function.\n", |
88af8bbe SAS |
1338 | c->label, c->bConfigurationValue, |
1339 | gi->composite.name); | |
1340 | goto err_comp_cleanup; | |
1341 | } | |
1342 | } | |
1343 | ||
1344 | /* init all strings */ | |
1345 | if (!list_empty(&gi->string_list)) { | |
1346 | struct gadget_strings *gs; | |
1347 | ||
1348 | i = 0; | |
1349 | list_for_each_entry(gs, &gi->string_list, list) { | |
1350 | ||
1351 | gi->gstrings[i] = &gs->stringtab_dev; | |
1352 | gs->stringtab_dev.strings = gs->strings; | |
1353 | gs->strings[USB_GADGET_MANUFACTURER_IDX].s = | |
1354 | gs->manufacturer; | |
1355 | gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; | |
1356 | gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; | |
1357 | i++; | |
1358 | } | |
1359 | gi->gstrings[i] = NULL; | |
1360 | s = usb_gstrings_attach(&gi->cdev, gi->gstrings, | |
1361 | USB_GADGET_FIRST_AVAIL_IDX); | |
fea77077 WY |
1362 | if (IS_ERR(s)) { |
1363 | ret = PTR_ERR(s); | |
88af8bbe | 1364 | goto err_comp_cleanup; |
fea77077 | 1365 | } |
88af8bbe SAS |
1366 | |
1367 | gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; | |
1368 | gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; | |
1369 | gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; | |
1370 | } | |
1371 | ||
87213d38 AP |
1372 | if (gi->use_os_desc) { |
1373 | cdev->use_os_string = true; | |
1374 | cdev->b_vendor_code = gi->b_vendor_code; | |
1375 | memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN); | |
1376 | } | |
1377 | ||
41ce84c8 LJ |
1378 | if (gadget_is_otg(gadget) && !otg_desc[0]) { |
1379 | struct usb_descriptor_header *usb_desc; | |
1380 | ||
1381 | usb_desc = usb_otg_descriptor_alloc(gadget); | |
1382 | if (!usb_desc) { | |
1383 | ret = -ENOMEM; | |
1384 | goto err_comp_cleanup; | |
1385 | } | |
1386 | usb_otg_descriptor_init(gadget, usb_desc); | |
1387 | otg_desc[0] = usb_desc; | |
1388 | otg_desc[1] = NULL; | |
1389 | } | |
1390 | ||
88af8bbe SAS |
1391 | /* Go through all configs, attach all functions */ |
1392 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
1393 | struct config_usb_cfg *cfg; | |
1394 | struct usb_function *f; | |
1395 | struct usb_function *tmp; | |
1396 | struct gadget_config_name *cn; | |
1397 | ||
41ce84c8 LJ |
1398 | if (gadget_is_otg(gadget)) |
1399 | c->descriptors = otg_desc; | |
1400 | ||
88af8bbe SAS |
1401 | cfg = container_of(c, struct config_usb_cfg, c); |
1402 | if (!list_empty(&cfg->string_list)) { | |
1403 | i = 0; | |
1404 | list_for_each_entry(cn, &cfg->string_list, list) { | |
1405 | cfg->gstrings[i] = &cn->stringtab_dev; | |
1406 | cn->stringtab_dev.strings = &cn->strings; | |
1407 | cn->strings.s = cn->configuration; | |
1408 | i++; | |
1409 | } | |
1410 | cfg->gstrings[i] = NULL; | |
1411 | s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); | |
fea77077 WY |
1412 | if (IS_ERR(s)) { |
1413 | ret = PTR_ERR(s); | |
88af8bbe | 1414 | goto err_comp_cleanup; |
fea77077 | 1415 | } |
88af8bbe SAS |
1416 | c->iConfiguration = s[0].id; |
1417 | } | |
1418 | ||
1419 | list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { | |
1420 | list_del(&f->list); | |
1421 | ret = usb_add_function(c, f); | |
5a68e9b5 AP |
1422 | if (ret) { |
1423 | list_add(&f->list, &cfg->func_list); | |
88af8bbe | 1424 | goto err_purge_funcs; |
5a68e9b5 | 1425 | } |
88af8bbe SAS |
1426 | } |
1427 | usb_ep_autoconfig_reset(cdev->gadget); | |
1428 | } | |
da424314 AP |
1429 | if (cdev->use_os_string) { |
1430 | ret = composite_os_desc_req_prepare(cdev, gadget->ep0); | |
1431 | if (ret) | |
1432 | goto err_purge_funcs; | |
1433 | } | |
1434 | ||
88af8bbe SAS |
1435 | usb_ep_autoconfig_reset(cdev->gadget); |
1436 | return 0; | |
1437 | ||
1438 | err_purge_funcs: | |
1439 | purge_configs_funcs(gi); | |
1440 | err_comp_cleanup: | |
1441 | composite_dev_cleanup(cdev); | |
1442 | return ret; | |
1443 | } | |
1444 | ||
1445 | static void configfs_composite_unbind(struct usb_gadget *gadget) | |
1446 | { | |
1447 | struct usb_composite_dev *cdev; | |
1448 | struct gadget_info *gi; | |
1449 | ||
1450 | /* the gi->lock is hold by the caller */ | |
1451 | ||
1452 | cdev = get_gadget_data(gadget); | |
1453 | gi = container_of(cdev, struct gadget_info, cdev); | |
1454 | ||
41ce84c8 LJ |
1455 | kfree(otg_desc[0]); |
1456 | otg_desc[0] = NULL; | |
88af8bbe SAS |
1457 | purge_configs_funcs(gi); |
1458 | composite_dev_cleanup(cdev); | |
1459 | usb_ep_autoconfig_reset(cdev->gadget); | |
1460 | cdev->gadget = NULL; | |
1461 | set_gadget_data(gadget, NULL); | |
1462 | } | |
1463 | ||
1464 | static const struct usb_gadget_driver configfs_driver_template = { | |
1465 | .bind = configfs_composite_bind, | |
1466 | .unbind = configfs_composite_unbind, | |
1467 | ||
1468 | .setup = composite_setup, | |
02f751b4 | 1469 | .reset = composite_disconnect, |
88af8bbe SAS |
1470 | .disconnect = composite_disconnect, |
1471 | ||
3a571870 AP |
1472 | .suspend = composite_suspend, |
1473 | .resume = composite_resume, | |
1474 | ||
88af8bbe SAS |
1475 | .max_speed = USB_SPEED_SUPER, |
1476 | .driver = { | |
1477 | .owner = THIS_MODULE, | |
1478 | .name = "configfs-gadget", | |
1479 | }, | |
1480 | }; | |
1481 | ||
1482 | static struct config_group *gadgets_make( | |
1483 | struct config_group *group, | |
1484 | const char *name) | |
1485 | { | |
1486 | struct gadget_info *gi; | |
1487 | ||
1488 | gi = kzalloc(sizeof(*gi), GFP_KERNEL); | |
1489 | if (!gi) | |
1490 | return ERR_PTR(-ENOMEM); | |
1491 | ||
1492 | gi->group.default_groups = gi->default_groups; | |
1493 | gi->group.default_groups[0] = &gi->functions_group; | |
1494 | gi->group.default_groups[1] = &gi->configs_group; | |
1495 | gi->group.default_groups[2] = &gi->strings_group; | |
87213d38 | 1496 | gi->group.default_groups[3] = &gi->os_desc_group; |
88af8bbe SAS |
1497 | |
1498 | config_group_init_type_name(&gi->functions_group, "functions", | |
1499 | &functions_type); | |
1500 | config_group_init_type_name(&gi->configs_group, "configs", | |
1501 | &config_desc_type); | |
1502 | config_group_init_type_name(&gi->strings_group, "strings", | |
1503 | &gadget_strings_strings_type); | |
87213d38 AP |
1504 | config_group_init_type_name(&gi->os_desc_group, "os_desc", |
1505 | &os_desc_type); | |
88af8bbe SAS |
1506 | |
1507 | gi->composite.bind = configfs_do_nothing; | |
1508 | gi->composite.unbind = configfs_do_nothing; | |
1509 | gi->composite.suspend = NULL; | |
1510 | gi->composite.resume = NULL; | |
1511 | gi->composite.max_speed = USB_SPEED_SUPER; | |
1512 | ||
1513 | mutex_init(&gi->lock); | |
1514 | INIT_LIST_HEAD(&gi->string_list); | |
1515 | INIT_LIST_HEAD(&gi->available_func); | |
1516 | ||
1517 | composite_init_dev(&gi->cdev); | |
1518 | gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; | |
1519 | gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; | |
1520 | gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); | |
1521 | ||
1522 | gi->composite.gadget_driver = configfs_driver_template; | |
1523 | ||
1524 | gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); | |
1525 | gi->composite.name = gi->composite.gadget_driver.function; | |
1526 | ||
1527 | if (!gi->composite.gadget_driver.function) | |
1528 | goto err; | |
1529 | ||
88af8bbe SAS |
1530 | config_group_init_type_name(&gi->group, name, |
1531 | &gadget_root_type); | |
1532 | return &gi->group; | |
1533 | err: | |
1534 | kfree(gi); | |
1535 | return ERR_PTR(-ENOMEM); | |
1536 | } | |
1537 | ||
1538 | static void gadgets_drop(struct config_group *group, struct config_item *item) | |
1539 | { | |
1540 | config_item_put(item); | |
1541 | } | |
1542 | ||
1543 | static struct configfs_group_operations gadgets_ops = { | |
1544 | .make_group = &gadgets_make, | |
1545 | .drop_item = &gadgets_drop, | |
1546 | }; | |
1547 | ||
1548 | static struct config_item_type gadgets_type = { | |
1549 | .ct_group_ops = &gadgets_ops, | |
1550 | .ct_owner = THIS_MODULE, | |
1551 | }; | |
1552 | ||
1553 | static struct configfs_subsystem gadget_subsys = { | |
1554 | .su_group = { | |
1555 | .cg_item = { | |
1556 | .ci_namebuf = "usb_gadget", | |
1557 | .ci_type = &gadgets_type, | |
1558 | }, | |
1559 | }, | |
1560 | .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), | |
1561 | }; | |
1562 | ||
092a4bd0 AP |
1563 | void unregister_gadget_item(struct config_item *item) |
1564 | { | |
1565 | struct gadget_info *gi = to_gadget_info(item); | |
1566 | ||
1567 | unregister_gadget(gi); | |
1568 | } | |
0700faaf | 1569 | EXPORT_SYMBOL_GPL(unregister_gadget_item); |
092a4bd0 | 1570 | |
88af8bbe SAS |
1571 | static int __init gadget_cfs_init(void) |
1572 | { | |
1573 | int ret; | |
1574 | ||
1575 | config_group_init(&gadget_subsys.su_group); | |
1576 | ||
1577 | ret = configfs_register_subsystem(&gadget_subsys); | |
1578 | return ret; | |
1579 | } | |
1580 | module_init(gadget_cfs_init); | |
1581 | ||
1582 | static void __exit gadget_cfs_exit(void) | |
1583 | { | |
1584 | configfs_unregister_subsystem(&gadget_subsys); | |
1585 | } | |
1586 | module_exit(gadget_cfs_exit); |