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