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> | |
5 | #include <linux/usb/composite.h> | |
6 | #include <linux/usb/gadget_configfs.h> | |
7 | ||
8 | int check_user_usb_string(const char *name, | |
9 | struct usb_gadget_strings *stringtab_dev) | |
10 | { | |
11 | unsigned primary_lang; | |
12 | unsigned sub_lang; | |
13 | u16 num; | |
14 | int ret; | |
15 | ||
16 | ret = kstrtou16(name, 0, &num); | |
17 | if (ret) | |
18 | return ret; | |
19 | ||
20 | primary_lang = num & 0x3ff; | |
21 | sub_lang = num >> 10; | |
22 | ||
23 | /* simple sanity check for valid langid */ | |
24 | switch (primary_lang) { | |
25 | case 0: | |
26 | case 0x62 ... 0xfe: | |
27 | case 0x100 ... 0x3ff: | |
28 | return -EINVAL; | |
29 | } | |
30 | if (!sub_lang) | |
31 | return -EINVAL; | |
32 | ||
33 | stringtab_dev->language = num; | |
34 | return 0; | |
35 | } | |
36 | ||
37 | #define MAX_NAME_LEN 40 | |
38 | #define MAX_USB_STRING_LANGS 2 | |
39 | ||
40 | struct gadget_info { | |
41 | struct config_group group; | |
42 | struct config_group functions_group; | |
43 | struct config_group configs_group; | |
44 | struct config_group strings_group; | |
45 | struct config_group *default_groups[4]; | |
46 | ||
47 | struct mutex lock; | |
48 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; | |
49 | struct list_head string_list; | |
50 | struct list_head available_func; | |
51 | ||
52 | const char *udc_name; | |
53 | #ifdef CONFIG_USB_OTG | |
54 | struct usb_otg_descriptor otg; | |
55 | #endif | |
56 | struct usb_composite_driver composite; | |
57 | struct usb_composite_dev cdev; | |
58 | }; | |
59 | ||
60 | struct config_usb_cfg { | |
61 | struct config_group group; | |
62 | struct config_group strings_group; | |
63 | struct config_group *default_groups[2]; | |
64 | struct list_head string_list; | |
65 | struct usb_configuration c; | |
66 | struct list_head func_list; | |
67 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; | |
68 | }; | |
69 | ||
70 | struct gadget_strings { | |
71 | struct usb_gadget_strings stringtab_dev; | |
72 | struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; | |
73 | char *manufacturer; | |
74 | char *product; | |
75 | char *serialnumber; | |
76 | ||
77 | struct config_group group; | |
78 | struct list_head list; | |
79 | }; | |
80 | ||
81 | struct gadget_config_name { | |
82 | struct usb_gadget_strings stringtab_dev; | |
83 | struct usb_string strings; | |
84 | char *configuration; | |
85 | ||
86 | struct config_group group; | |
87 | struct list_head list; | |
88 | }; | |
89 | ||
90 | static int usb_string_copy(const char *s, char **s_copy) | |
91 | { | |
92 | int ret; | |
93 | char *str; | |
94 | char *copy = *s_copy; | |
95 | ret = strlen(s); | |
96 | if (ret > 126) | |
97 | return -EOVERFLOW; | |
98 | ||
99 | str = kstrdup(s, GFP_KERNEL); | |
100 | if (!str) | |
101 | return -ENOMEM; | |
102 | if (str[ret - 1] == '\n') | |
103 | str[ret - 1] = '\0'; | |
104 | kfree(copy); | |
105 | *s_copy = str; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | CONFIGFS_ATTR_STRUCT(gadget_info); | |
110 | CONFIGFS_ATTR_STRUCT(config_usb_cfg); | |
111 | ||
112 | #define GI_DEVICE_DESC_ITEM_ATTR(name) \ | |
113 | static struct gadget_info_attribute gadget_cdev_desc_##name = \ | |
114 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
115 | gadget_dev_desc_##name##_show, \ | |
116 | gadget_dev_desc_##name##_store) | |
117 | ||
118 | #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ | |
119 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ | |
120 | char *page) \ | |
121 | { \ | |
122 | return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \ | |
123 | } | |
124 | ||
125 | #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ | |
126 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ | |
127 | char *page) \ | |
128 | { \ | |
129 | return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \ | |
130 | } | |
131 | ||
132 | ||
133 | #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ | |
134 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ | |
135 | const char *page, size_t len) \ | |
136 | { \ | |
137 | u8 val; \ | |
138 | int ret; \ | |
139 | ret = kstrtou8(page, 0, &val); \ | |
140 | if (ret) \ | |
141 | return ret; \ | |
142 | gi->cdev.desc._name = val; \ | |
143 | return len; \ | |
144 | } | |
145 | ||
146 | #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ | |
147 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ | |
148 | const char *page, size_t len) \ | |
149 | { \ | |
150 | u16 val; \ | |
151 | int ret; \ | |
152 | ret = kstrtou16(page, 0, &val); \ | |
153 | if (ret) \ | |
154 | return ret; \ | |
155 | gi->cdev.desc._name = cpu_to_le16p(&val); \ | |
156 | return len; \ | |
157 | } | |
158 | ||
159 | #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ | |
160 | GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ | |
161 | GI_DEVICE_DESC_SIMPLE_W_##_type(_name) | |
162 | ||
163 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); | |
164 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); | |
165 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); | |
166 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); | |
167 | GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); | |
168 | GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); | |
169 | GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); | |
170 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); | |
171 | ||
172 | static ssize_t is_valid_bcd(u16 bcd_val) | |
173 | { | |
174 | if ((bcd_val & 0xf) > 9) | |
175 | return -EINVAL; | |
176 | if (((bcd_val >> 4) & 0xf) > 9) | |
177 | return -EINVAL; | |
178 | if (((bcd_val >> 8) & 0xf) > 9) | |
179 | return -EINVAL; | |
180 | if (((bcd_val >> 12) & 0xf) > 9) | |
181 | return -EINVAL; | |
182 | return 0; | |
183 | } | |
184 | ||
185 | static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi, | |
186 | const char *page, size_t len) | |
187 | { | |
188 | u16 bcdDevice; | |
189 | int ret; | |
190 | ||
191 | ret = kstrtou16(page, 0, &bcdDevice); | |
192 | if (ret) | |
193 | return ret; | |
194 | ret = is_valid_bcd(bcdDevice); | |
195 | if (ret) | |
196 | return ret; | |
197 | ||
198 | gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); | |
199 | return len; | |
200 | } | |
201 | ||
202 | static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi, | |
203 | const char *page, size_t len) | |
204 | { | |
205 | u16 bcdUSB; | |
206 | int ret; | |
207 | ||
208 | ret = kstrtou16(page, 0, &bcdUSB); | |
209 | if (ret) | |
210 | return ret; | |
211 | ret = is_valid_bcd(bcdUSB); | |
212 | if (ret) | |
213 | return ret; | |
214 | ||
215 | gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); | |
216 | return len; | |
217 | } | |
218 | ||
219 | static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page) | |
220 | { | |
221 | return sprintf(page, "%s\n", gi->udc_name ?: ""); | |
222 | } | |
223 | ||
224 | static int unregister_gadget(struct gadget_info *gi) | |
225 | { | |
226 | int ret; | |
227 | ||
228 | if (!gi->udc_name) | |
229 | return -ENODEV; | |
230 | ||
231 | ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); | |
232 | if (ret) | |
233 | return ret; | |
234 | kfree(gi->udc_name); | |
235 | gi->udc_name = NULL; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi, | |
240 | const char *page, size_t len) | |
241 | { | |
242 | char *name; | |
243 | int ret; | |
244 | ||
245 | name = kstrdup(page, GFP_KERNEL); | |
246 | if (!name) | |
247 | return -ENOMEM; | |
248 | if (name[len - 1] == '\n') | |
249 | name[len - 1] = '\0'; | |
250 | ||
251 | mutex_lock(&gi->lock); | |
252 | ||
253 | if (!strlen(name)) { | |
254 | ret = unregister_gadget(gi); | |
255 | if (ret) | |
256 | goto err; | |
257 | } else { | |
258 | if (gi->udc_name) { | |
259 | ret = -EBUSY; | |
260 | goto err; | |
261 | } | |
262 | ret = udc_attach_driver(name, &gi->composite.gadget_driver); | |
263 | if (ret) | |
264 | goto err; | |
265 | gi->udc_name = name; | |
266 | } | |
267 | mutex_unlock(&gi->lock); | |
268 | return len; | |
269 | err: | |
270 | kfree(name); | |
271 | mutex_unlock(&gi->lock); | |
272 | return ret; | |
273 | } | |
274 | ||
275 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass); | |
276 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass); | |
277 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol); | |
278 | GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0); | |
279 | GI_DEVICE_DESC_ITEM_ATTR(idVendor); | |
280 | GI_DEVICE_DESC_ITEM_ATTR(idProduct); | |
281 | GI_DEVICE_DESC_ITEM_ATTR(bcdDevice); | |
282 | GI_DEVICE_DESC_ITEM_ATTR(bcdUSB); | |
283 | GI_DEVICE_DESC_ITEM_ATTR(UDC); | |
284 | ||
285 | static struct configfs_attribute *gadget_root_attrs[] = { | |
286 | &gadget_cdev_desc_bDeviceClass.attr, | |
287 | &gadget_cdev_desc_bDeviceSubClass.attr, | |
288 | &gadget_cdev_desc_bDeviceProtocol.attr, | |
289 | &gadget_cdev_desc_bMaxPacketSize0.attr, | |
290 | &gadget_cdev_desc_idVendor.attr, | |
291 | &gadget_cdev_desc_idProduct.attr, | |
292 | &gadget_cdev_desc_bcdDevice.attr, | |
293 | &gadget_cdev_desc_bcdUSB.attr, | |
294 | &gadget_cdev_desc_UDC.attr, | |
295 | NULL, | |
296 | }; | |
297 | ||
298 | static inline struct gadget_info *to_gadget_info(struct config_item *item) | |
299 | { | |
300 | return container_of(to_config_group(item), struct gadget_info, group); | |
301 | } | |
302 | ||
303 | static inline struct gadget_strings *to_gadget_strings(struct config_item *item) | |
304 | { | |
305 | return container_of(to_config_group(item), struct gadget_strings, | |
306 | group); | |
307 | } | |
308 | ||
309 | static inline struct gadget_config_name *to_gadget_config_name( | |
310 | struct config_item *item) | |
311 | { | |
312 | return container_of(to_config_group(item), struct gadget_config_name, | |
313 | group); | |
314 | } | |
315 | ||
316 | static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) | |
317 | { | |
318 | return container_of(to_config_group(item), struct config_usb_cfg, | |
319 | group); | |
320 | } | |
321 | ||
322 | static inline struct usb_function_instance *to_usb_function_instance( | |
323 | struct config_item *item) | |
324 | { | |
325 | return container_of(to_config_group(item), | |
326 | struct usb_function_instance, group); | |
327 | } | |
328 | ||
329 | static void gadget_info_attr_release(struct config_item *item) | |
330 | { | |
331 | struct gadget_info *gi = to_gadget_info(item); | |
332 | ||
333 | WARN_ON(!list_empty(&gi->cdev.configs)); | |
334 | WARN_ON(!list_empty(&gi->string_list)); | |
335 | WARN_ON(!list_empty(&gi->available_func)); | |
336 | kfree(gi->composite.gadget_driver.function); | |
337 | kfree(gi); | |
338 | } | |
339 | ||
340 | CONFIGFS_ATTR_OPS(gadget_info); | |
341 | ||
342 | static struct configfs_item_operations gadget_root_item_ops = { | |
343 | .release = gadget_info_attr_release, | |
344 | .show_attribute = gadget_info_attr_show, | |
345 | .store_attribute = gadget_info_attr_store, | |
346 | }; | |
347 | ||
348 | static void gadget_config_attr_release(struct config_item *item) | |
349 | { | |
350 | struct config_usb_cfg *cfg = to_config_usb_cfg(item); | |
351 | ||
352 | WARN_ON(!list_empty(&cfg->c.functions)); | |
353 | list_del(&cfg->c.list); | |
354 | kfree(cfg->c.label); | |
355 | kfree(cfg); | |
356 | } | |
357 | ||
358 | static int config_usb_cfg_link( | |
359 | struct config_item *usb_cfg_ci, | |
360 | struct config_item *usb_func_ci) | |
361 | { | |
362 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); | |
363 | struct usb_composite_dev *cdev = cfg->c.cdev; | |
364 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); | |
365 | ||
366 | struct config_group *group = to_config_group(usb_func_ci); | |
367 | struct usb_function_instance *fi = container_of(group, | |
368 | struct usb_function_instance, group); | |
369 | struct usb_function_instance *a_fi; | |
370 | struct usb_function *f; | |
371 | int ret; | |
372 | ||
373 | mutex_lock(&gi->lock); | |
374 | /* | |
375 | * Make sure this function is from within our _this_ gadget and not | |
376 | * from another gadget or a random directory. | |
377 | * Also a function instance can only be linked once. | |
378 | */ | |
379 | list_for_each_entry(a_fi, &gi->available_func, cfs_list) { | |
380 | if (a_fi == fi) | |
381 | break; | |
382 | } | |
383 | if (a_fi != fi) { | |
384 | ret = -EINVAL; | |
385 | goto out; | |
386 | } | |
387 | ||
388 | list_for_each_entry(f, &cfg->func_list, list) { | |
389 | if (f->fi == fi) { | |
390 | ret = -EEXIST; | |
391 | goto out; | |
392 | } | |
393 | } | |
394 | ||
395 | f = usb_get_function(fi); | |
396 | if (IS_ERR(f)) { | |
397 | ret = PTR_ERR(f); | |
398 | goto out; | |
399 | } | |
400 | ||
401 | /* stash the function until we bind it to the gadget */ | |
402 | list_add_tail(&f->list, &cfg->func_list); | |
403 | ret = 0; | |
404 | out: | |
405 | mutex_unlock(&gi->lock); | |
406 | return ret; | |
407 | } | |
408 | ||
409 | static int config_usb_cfg_unlink( | |
410 | struct config_item *usb_cfg_ci, | |
411 | struct config_item *usb_func_ci) | |
412 | { | |
413 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); | |
414 | struct usb_composite_dev *cdev = cfg->c.cdev; | |
415 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); | |
416 | ||
417 | struct config_group *group = to_config_group(usb_func_ci); | |
418 | struct usb_function_instance *fi = container_of(group, | |
419 | struct usb_function_instance, group); | |
420 | struct usb_function *f; | |
421 | ||
422 | /* | |
423 | * ideally I would like to forbid to unlink functions while a gadget is | |
424 | * bound to an UDC. Since this isn't possible at the moment, we simply | |
425 | * force an unbind, the function is available here and then we can | |
426 | * remove the function. | |
427 | */ | |
428 | mutex_lock(&gi->lock); | |
429 | if (gi->udc_name) | |
430 | unregister_gadget(gi); | |
431 | WARN_ON(gi->udc_name); | |
432 | ||
433 | list_for_each_entry(f, &cfg->func_list, list) { | |
434 | if (f->fi == fi) { | |
435 | list_del(&f->list); | |
436 | usb_put_function(f); | |
437 | mutex_unlock(&gi->lock); | |
438 | return 0; | |
439 | } | |
440 | } | |
441 | mutex_unlock(&gi->lock); | |
75bfe23a | 442 | WARN(1, "Unable to locate function to unbind\n"); |
88af8bbe SAS |
443 | return 0; |
444 | } | |
445 | ||
446 | CONFIGFS_ATTR_OPS(config_usb_cfg); | |
447 | ||
448 | static struct configfs_item_operations gadget_config_item_ops = { | |
449 | .release = gadget_config_attr_release, | |
450 | .show_attribute = config_usb_cfg_attr_show, | |
451 | .store_attribute = config_usb_cfg_attr_store, | |
452 | .allow_link = config_usb_cfg_link, | |
453 | .drop_link = config_usb_cfg_unlink, | |
454 | }; | |
455 | ||
456 | ||
457 | static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg, | |
458 | char *page) | |
459 | { | |
460 | return sprintf(page, "%u\n", cfg->c.MaxPower); | |
461 | } | |
462 | ||
463 | static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg, | |
464 | const char *page, size_t len) | |
465 | { | |
466 | u16 val; | |
467 | int ret; | |
468 | ret = kstrtou16(page, 0, &val); | |
469 | if (ret) | |
470 | return ret; | |
471 | if (DIV_ROUND_UP(val, 8) > 0xff) | |
472 | return -ERANGE; | |
473 | cfg->c.MaxPower = val; | |
474 | return len; | |
475 | } | |
476 | ||
477 | static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg, | |
478 | char *page) | |
479 | { | |
480 | return sprintf(page, "0x%02x\n", cfg->c.bmAttributes); | |
481 | } | |
482 | ||
483 | static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg, | |
484 | const char *page, size_t len) | |
485 | { | |
486 | u8 val; | |
487 | int ret; | |
488 | ret = kstrtou8(page, 0, &val); | |
489 | if (ret) | |
490 | return ret; | |
491 | if (!(val & USB_CONFIG_ATT_ONE)) | |
492 | return -EINVAL; | |
493 | if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | | |
494 | USB_CONFIG_ATT_WAKEUP)) | |
495 | return -EINVAL; | |
496 | cfg->c.bmAttributes = val; | |
497 | return len; | |
498 | } | |
499 | ||
500 | #define CFG_CONFIG_DESC_ITEM_ATTR(name) \ | |
501 | static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \ | |
502 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ | |
503 | gadget_config_desc_##name##_show, \ | |
504 | gadget_config_desc_##name##_store) | |
505 | ||
506 | CFG_CONFIG_DESC_ITEM_ATTR(MaxPower); | |
507 | CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes); | |
508 | ||
509 | static struct configfs_attribute *gadget_config_attrs[] = { | |
510 | &gadget_usb_cfg_MaxPower.attr, | |
511 | &gadget_usb_cfg_bmAttributes.attr, | |
512 | NULL, | |
513 | }; | |
514 | ||
515 | static struct config_item_type gadget_config_type = { | |
516 | .ct_item_ops = &gadget_config_item_ops, | |
517 | .ct_attrs = gadget_config_attrs, | |
518 | .ct_owner = THIS_MODULE, | |
519 | }; | |
520 | ||
521 | static struct config_item_type gadget_root_type = { | |
522 | .ct_item_ops = &gadget_root_item_ops, | |
523 | .ct_attrs = gadget_root_attrs, | |
524 | .ct_owner = THIS_MODULE, | |
525 | }; | |
526 | ||
527 | static void composite_init_dev(struct usb_composite_dev *cdev) | |
528 | { | |
529 | spin_lock_init(&cdev->lock); | |
530 | INIT_LIST_HEAD(&cdev->configs); | |
531 | INIT_LIST_HEAD(&cdev->gstrings); | |
532 | } | |
533 | ||
534 | static struct config_group *function_make( | |
535 | struct config_group *group, | |
536 | const char *name) | |
537 | { | |
538 | struct gadget_info *gi; | |
539 | struct usb_function_instance *fi; | |
540 | char buf[MAX_NAME_LEN]; | |
541 | char *func_name; | |
542 | char *instance_name; | |
543 | int ret; | |
544 | ||
545 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); | |
546 | if (ret >= MAX_NAME_LEN) | |
547 | return ERR_PTR(-ENAMETOOLONG); | |
548 | ||
549 | func_name = buf; | |
550 | instance_name = strchr(func_name, '.'); | |
551 | if (!instance_name) { | |
552 | pr_err("Unable to locate . in FUNC.INSTANCE\n"); | |
553 | return ERR_PTR(-EINVAL); | |
554 | } | |
555 | *instance_name = '\0'; | |
556 | instance_name++; | |
557 | ||
558 | fi = usb_get_function_instance(func_name); | |
559 | if (IS_ERR(fi)) | |
a3469411 | 560 | return ERR_CAST(fi); |
88af8bbe SAS |
561 | |
562 | ret = config_item_set_name(&fi->group.cg_item, name); | |
563 | if (ret) { | |
564 | usb_put_function_instance(fi); | |
565 | return ERR_PTR(ret); | |
566 | } | |
1933861d AP |
567 | if (fi->set_inst_name) { |
568 | ret = fi->set_inst_name(fi, instance_name); | |
569 | if (ret) { | |
570 | usb_put_function_instance(fi); | |
571 | return ERR_PTR(ret); | |
572 | } | |
573 | } | |
88af8bbe SAS |
574 | |
575 | gi = container_of(group, struct gadget_info, functions_group); | |
576 | ||
577 | mutex_lock(&gi->lock); | |
578 | list_add_tail(&fi->cfs_list, &gi->available_func); | |
579 | mutex_unlock(&gi->lock); | |
580 | return &fi->group; | |
581 | } | |
582 | ||
583 | static void function_drop( | |
584 | struct config_group *group, | |
585 | struct config_item *item) | |
586 | { | |
587 | struct usb_function_instance *fi = to_usb_function_instance(item); | |
588 | struct gadget_info *gi; | |
589 | ||
590 | gi = container_of(group, struct gadget_info, functions_group); | |
591 | ||
592 | mutex_lock(&gi->lock); | |
593 | list_del(&fi->cfs_list); | |
594 | mutex_unlock(&gi->lock); | |
595 | config_item_put(item); | |
596 | } | |
597 | ||
598 | static struct configfs_group_operations functions_ops = { | |
599 | .make_group = &function_make, | |
600 | .drop_item = &function_drop, | |
601 | }; | |
602 | ||
603 | static struct config_item_type functions_type = { | |
604 | .ct_group_ops = &functions_ops, | |
605 | .ct_owner = THIS_MODULE, | |
606 | }; | |
607 | ||
608 | CONFIGFS_ATTR_STRUCT(gadget_config_name); | |
609 | GS_STRINGS_RW(gadget_config_name, configuration); | |
610 | ||
611 | static struct configfs_attribute *gadget_config_name_langid_attrs[] = { | |
612 | &gadget_config_name_configuration.attr, | |
613 | NULL, | |
614 | }; | |
615 | ||
616 | static void gadget_config_name_attr_release(struct config_item *item) | |
617 | { | |
618 | struct gadget_config_name *cn = to_gadget_config_name(item); | |
619 | ||
620 | kfree(cn->configuration); | |
621 | ||
622 | list_del(&cn->list); | |
623 | kfree(cn); | |
624 | } | |
625 | ||
626 | USB_CONFIG_STRING_RW_OPS(gadget_config_name); | |
627 | USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); | |
628 | ||
629 | static struct config_group *config_desc_make( | |
630 | struct config_group *group, | |
631 | const char *name) | |
632 | { | |
633 | struct gadget_info *gi; | |
634 | struct config_usb_cfg *cfg; | |
635 | char buf[MAX_NAME_LEN]; | |
636 | char *num_str; | |
637 | u8 num; | |
638 | int ret; | |
639 | ||
640 | gi = container_of(group, struct gadget_info, configs_group); | |
641 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); | |
642 | if (ret >= MAX_NAME_LEN) | |
643 | return ERR_PTR(-ENAMETOOLONG); | |
644 | ||
645 | num_str = strchr(buf, '.'); | |
646 | if (!num_str) { | |
647 | pr_err("Unable to locate . in name.bConfigurationValue\n"); | |
648 | return ERR_PTR(-EINVAL); | |
649 | } | |
650 | ||
651 | *num_str = '\0'; | |
652 | num_str++; | |
653 | ||
654 | if (!strlen(buf)) | |
655 | return ERR_PTR(-EINVAL); | |
656 | ||
657 | ret = kstrtou8(num_str, 0, &num); | |
658 | if (ret) | |
659 | return ERR_PTR(ret); | |
660 | ||
661 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); | |
662 | if (!cfg) | |
663 | return ERR_PTR(-ENOMEM); | |
664 | cfg->c.label = kstrdup(buf, GFP_KERNEL); | |
665 | if (!cfg->c.label) { | |
666 | ret = -ENOMEM; | |
667 | goto err; | |
668 | } | |
669 | cfg->c.bConfigurationValue = num; | |
670 | cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; | |
671 | cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; | |
672 | INIT_LIST_HEAD(&cfg->string_list); | |
673 | INIT_LIST_HEAD(&cfg->func_list); | |
674 | ||
675 | cfg->group.default_groups = cfg->default_groups; | |
676 | cfg->default_groups[0] = &cfg->strings_group; | |
677 | ||
678 | config_group_init_type_name(&cfg->group, name, | |
679 | &gadget_config_type); | |
680 | config_group_init_type_name(&cfg->strings_group, "strings", | |
681 | &gadget_config_name_strings_type); | |
682 | ||
683 | ret = usb_add_config_only(&gi->cdev, &cfg->c); | |
684 | if (ret) | |
685 | goto err; | |
686 | ||
687 | return &cfg->group; | |
688 | err: | |
689 | kfree(cfg->c.label); | |
690 | kfree(cfg); | |
691 | return ERR_PTR(ret); | |
692 | } | |
693 | ||
694 | static void config_desc_drop( | |
695 | struct config_group *group, | |
696 | struct config_item *item) | |
697 | { | |
698 | config_item_put(item); | |
699 | } | |
700 | ||
701 | static struct configfs_group_operations config_desc_ops = { | |
702 | .make_group = &config_desc_make, | |
703 | .drop_item = &config_desc_drop, | |
704 | }; | |
705 | ||
706 | static struct config_item_type config_desc_type = { | |
707 | .ct_group_ops = &config_desc_ops, | |
708 | .ct_owner = THIS_MODULE, | |
709 | }; | |
710 | ||
711 | CONFIGFS_ATTR_STRUCT(gadget_strings); | |
712 | GS_STRINGS_RW(gadget_strings, manufacturer); | |
713 | GS_STRINGS_RW(gadget_strings, product); | |
714 | GS_STRINGS_RW(gadget_strings, serialnumber); | |
715 | ||
716 | static struct configfs_attribute *gadget_strings_langid_attrs[] = { | |
717 | &gadget_strings_manufacturer.attr, | |
718 | &gadget_strings_product.attr, | |
719 | &gadget_strings_serialnumber.attr, | |
720 | NULL, | |
721 | }; | |
722 | ||
723 | static void gadget_strings_attr_release(struct config_item *item) | |
724 | { | |
725 | struct gadget_strings *gs = to_gadget_strings(item); | |
726 | ||
727 | kfree(gs->manufacturer); | |
728 | kfree(gs->product); | |
729 | kfree(gs->serialnumber); | |
730 | ||
731 | list_del(&gs->list); | |
732 | kfree(gs); | |
733 | } | |
734 | ||
735 | USB_CONFIG_STRING_RW_OPS(gadget_strings); | |
736 | USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); | |
737 | ||
738 | static int configfs_do_nothing(struct usb_composite_dev *cdev) | |
739 | { | |
75bfe23a | 740 | WARN_ON(1); |
88af8bbe SAS |
741 | return -EINVAL; |
742 | } | |
743 | ||
744 | int composite_dev_prepare(struct usb_composite_driver *composite, | |
745 | struct usb_composite_dev *dev); | |
746 | ||
747 | static void purge_configs_funcs(struct gadget_info *gi) | |
748 | { | |
749 | struct usb_configuration *c; | |
750 | ||
751 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
752 | struct usb_function *f, *tmp; | |
753 | struct config_usb_cfg *cfg; | |
754 | ||
755 | cfg = container_of(c, struct config_usb_cfg, c); | |
756 | ||
757 | list_for_each_entry_safe(f, tmp, &c->functions, list) { | |
758 | ||
759 | list_move_tail(&f->list, &cfg->func_list); | |
760 | if (f->unbind) { | |
761 | dev_err(&gi->cdev.gadget->dev, "unbind function" | |
762 | " '%s'/%p\n", f->name, f); | |
763 | f->unbind(c, f); | |
764 | } | |
765 | } | |
766 | c->next_interface_id = 0; | |
767 | c->superspeed = 0; | |
768 | c->highspeed = 0; | |
769 | c->fullspeed = 0; | |
770 | } | |
771 | } | |
772 | ||
773 | static int configfs_composite_bind(struct usb_gadget *gadget, | |
774 | struct usb_gadget_driver *gdriver) | |
775 | { | |
776 | struct usb_composite_driver *composite = to_cdriver(gdriver); | |
777 | struct gadget_info *gi = container_of(composite, | |
778 | struct gadget_info, composite); | |
779 | struct usb_composite_dev *cdev = &gi->cdev; | |
780 | struct usb_configuration *c; | |
781 | struct usb_string *s; | |
782 | unsigned i; | |
783 | int ret; | |
784 | ||
785 | /* the gi->lock is hold by the caller */ | |
786 | cdev->gadget = gadget; | |
787 | set_gadget_data(gadget, cdev); | |
788 | ret = composite_dev_prepare(composite, cdev); | |
789 | if (ret) | |
790 | return ret; | |
791 | /* and now the gadget bind */ | |
792 | ret = -EINVAL; | |
793 | ||
794 | if (list_empty(&gi->cdev.configs)) { | |
795 | pr_err("Need atleast one configuration in %s.\n", | |
796 | gi->composite.name); | |
797 | goto err_comp_cleanup; | |
798 | } | |
799 | ||
800 | ||
801 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
802 | struct config_usb_cfg *cfg; | |
803 | ||
804 | cfg = container_of(c, struct config_usb_cfg, c); | |
805 | if (list_empty(&cfg->func_list)) { | |
806 | pr_err("Config %s/%d of %s needs atleast one function.\n", | |
807 | c->label, c->bConfigurationValue, | |
808 | gi->composite.name); | |
809 | goto err_comp_cleanup; | |
810 | } | |
811 | } | |
812 | ||
813 | /* init all strings */ | |
814 | if (!list_empty(&gi->string_list)) { | |
815 | struct gadget_strings *gs; | |
816 | ||
817 | i = 0; | |
818 | list_for_each_entry(gs, &gi->string_list, list) { | |
819 | ||
820 | gi->gstrings[i] = &gs->stringtab_dev; | |
821 | gs->stringtab_dev.strings = gs->strings; | |
822 | gs->strings[USB_GADGET_MANUFACTURER_IDX].s = | |
823 | gs->manufacturer; | |
824 | gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; | |
825 | gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; | |
826 | i++; | |
827 | } | |
828 | gi->gstrings[i] = NULL; | |
829 | s = usb_gstrings_attach(&gi->cdev, gi->gstrings, | |
830 | USB_GADGET_FIRST_AVAIL_IDX); | |
fea77077 WY |
831 | if (IS_ERR(s)) { |
832 | ret = PTR_ERR(s); | |
88af8bbe | 833 | goto err_comp_cleanup; |
fea77077 | 834 | } |
88af8bbe SAS |
835 | |
836 | gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; | |
837 | gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; | |
838 | gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; | |
839 | } | |
840 | ||
841 | /* Go through all configs, attach all functions */ | |
842 | list_for_each_entry(c, &gi->cdev.configs, list) { | |
843 | struct config_usb_cfg *cfg; | |
844 | struct usb_function *f; | |
845 | struct usb_function *tmp; | |
846 | struct gadget_config_name *cn; | |
847 | ||
848 | cfg = container_of(c, struct config_usb_cfg, c); | |
849 | if (!list_empty(&cfg->string_list)) { | |
850 | i = 0; | |
851 | list_for_each_entry(cn, &cfg->string_list, list) { | |
852 | cfg->gstrings[i] = &cn->stringtab_dev; | |
853 | cn->stringtab_dev.strings = &cn->strings; | |
854 | cn->strings.s = cn->configuration; | |
855 | i++; | |
856 | } | |
857 | cfg->gstrings[i] = NULL; | |
858 | s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); | |
fea77077 WY |
859 | if (IS_ERR(s)) { |
860 | ret = PTR_ERR(s); | |
88af8bbe | 861 | goto err_comp_cleanup; |
fea77077 | 862 | } |
88af8bbe SAS |
863 | c->iConfiguration = s[0].id; |
864 | } | |
865 | ||
866 | list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { | |
867 | list_del(&f->list); | |
868 | ret = usb_add_function(c, f); | |
5a68e9b5 AP |
869 | if (ret) { |
870 | list_add(&f->list, &cfg->func_list); | |
88af8bbe | 871 | goto err_purge_funcs; |
5a68e9b5 | 872 | } |
88af8bbe SAS |
873 | } |
874 | usb_ep_autoconfig_reset(cdev->gadget); | |
875 | } | |
876 | usb_ep_autoconfig_reset(cdev->gadget); | |
877 | return 0; | |
878 | ||
879 | err_purge_funcs: | |
880 | purge_configs_funcs(gi); | |
881 | err_comp_cleanup: | |
882 | composite_dev_cleanup(cdev); | |
883 | return ret; | |
884 | } | |
885 | ||
886 | static void configfs_composite_unbind(struct usb_gadget *gadget) | |
887 | { | |
888 | struct usb_composite_dev *cdev; | |
889 | struct gadget_info *gi; | |
890 | ||
891 | /* the gi->lock is hold by the caller */ | |
892 | ||
893 | cdev = get_gadget_data(gadget); | |
894 | gi = container_of(cdev, struct gadget_info, cdev); | |
895 | ||
896 | purge_configs_funcs(gi); | |
897 | composite_dev_cleanup(cdev); | |
898 | usb_ep_autoconfig_reset(cdev->gadget); | |
899 | cdev->gadget = NULL; | |
900 | set_gadget_data(gadget, NULL); | |
901 | } | |
902 | ||
903 | static const struct usb_gadget_driver configfs_driver_template = { | |
904 | .bind = configfs_composite_bind, | |
905 | .unbind = configfs_composite_unbind, | |
906 | ||
907 | .setup = composite_setup, | |
908 | .disconnect = composite_disconnect, | |
909 | ||
910 | .max_speed = USB_SPEED_SUPER, | |
911 | .driver = { | |
912 | .owner = THIS_MODULE, | |
913 | .name = "configfs-gadget", | |
914 | }, | |
915 | }; | |
916 | ||
917 | static struct config_group *gadgets_make( | |
918 | struct config_group *group, | |
919 | const char *name) | |
920 | { | |
921 | struct gadget_info *gi; | |
922 | ||
923 | gi = kzalloc(sizeof(*gi), GFP_KERNEL); | |
924 | if (!gi) | |
925 | return ERR_PTR(-ENOMEM); | |
926 | ||
927 | gi->group.default_groups = gi->default_groups; | |
928 | gi->group.default_groups[0] = &gi->functions_group; | |
929 | gi->group.default_groups[1] = &gi->configs_group; | |
930 | gi->group.default_groups[2] = &gi->strings_group; | |
931 | ||
932 | config_group_init_type_name(&gi->functions_group, "functions", | |
933 | &functions_type); | |
934 | config_group_init_type_name(&gi->configs_group, "configs", | |
935 | &config_desc_type); | |
936 | config_group_init_type_name(&gi->strings_group, "strings", | |
937 | &gadget_strings_strings_type); | |
938 | ||
939 | gi->composite.bind = configfs_do_nothing; | |
940 | gi->composite.unbind = configfs_do_nothing; | |
941 | gi->composite.suspend = NULL; | |
942 | gi->composite.resume = NULL; | |
943 | gi->composite.max_speed = USB_SPEED_SUPER; | |
944 | ||
945 | mutex_init(&gi->lock); | |
946 | INIT_LIST_HEAD(&gi->string_list); | |
947 | INIT_LIST_HEAD(&gi->available_func); | |
948 | ||
949 | composite_init_dev(&gi->cdev); | |
950 | gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; | |
951 | gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; | |
952 | gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); | |
953 | ||
954 | gi->composite.gadget_driver = configfs_driver_template; | |
955 | ||
956 | gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); | |
957 | gi->composite.name = gi->composite.gadget_driver.function; | |
958 | ||
959 | if (!gi->composite.gadget_driver.function) | |
960 | goto err; | |
961 | ||
962 | #ifdef CONFIG_USB_OTG | |
963 | gi->otg.bLength = sizeof(struct usb_otg_descriptor); | |
964 | gi->otg.bDescriptorType = USB_DT_OTG; | |
965 | gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP; | |
966 | #endif | |
967 | ||
968 | config_group_init_type_name(&gi->group, name, | |
969 | &gadget_root_type); | |
970 | return &gi->group; | |
971 | err: | |
972 | kfree(gi); | |
973 | return ERR_PTR(-ENOMEM); | |
974 | } | |
975 | ||
976 | static void gadgets_drop(struct config_group *group, struct config_item *item) | |
977 | { | |
978 | config_item_put(item); | |
979 | } | |
980 | ||
981 | static struct configfs_group_operations gadgets_ops = { | |
982 | .make_group = &gadgets_make, | |
983 | .drop_item = &gadgets_drop, | |
984 | }; | |
985 | ||
986 | static struct config_item_type gadgets_type = { | |
987 | .ct_group_ops = &gadgets_ops, | |
988 | .ct_owner = THIS_MODULE, | |
989 | }; | |
990 | ||
991 | static struct configfs_subsystem gadget_subsys = { | |
992 | .su_group = { | |
993 | .cg_item = { | |
994 | .ci_namebuf = "usb_gadget", | |
995 | .ci_type = &gadgets_type, | |
996 | }, | |
997 | }, | |
998 | .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), | |
999 | }; | |
1000 | ||
092a4bd0 AP |
1001 | void unregister_gadget_item(struct config_item *item) |
1002 | { | |
1003 | struct gadget_info *gi = to_gadget_info(item); | |
1004 | ||
1005 | unregister_gadget(gi); | |
1006 | } | |
1007 | EXPORT_SYMBOL(unregister_gadget_item); | |
1008 | ||
88af8bbe SAS |
1009 | static int __init gadget_cfs_init(void) |
1010 | { | |
1011 | int ret; | |
1012 | ||
1013 | config_group_init(&gadget_subsys.su_group); | |
1014 | ||
1015 | ret = configfs_register_subsystem(&gadget_subsys); | |
1016 | return ret; | |
1017 | } | |
1018 | module_init(gadget_cfs_init); | |
1019 | ||
1020 | static void __exit gadget_cfs_exit(void) | |
1021 | { | |
1022 | configfs_unregister_subsystem(&gadget_subsys); | |
1023 | } | |
1024 | module_exit(gadget_cfs_exit); |