Commit | Line | Data |
---|---|---|
5ab54cf7 MN |
1 | /* |
2 | * g_ffs.c -- user mode file system API for USB composite function controllers | |
3 | * | |
4 | * Copyright (C) 2010 Samsung Electronics | |
54b8360f | 5 | * Author: Michal Nazarewicz <mina86@mina86.com> |
5ab54cf7 MN |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
5ab54cf7 MN |
11 | */ |
12 | ||
aa02f172 MN |
13 | #define pr_fmt(fmt) "g_ffs: " fmt |
14 | ||
c6c56008 MN |
15 | #include <linux/module.h> |
16 | #include <linux/utsname.h> | |
17 | ||
c6c56008 MN |
18 | /* |
19 | * kbuild is not very cooperative with respect to linking separately | |
20 | * compiled library objects into one module. So for now we won't use | |
21 | * separate compilation ... ensuring init/exit sections work to shrink | |
22 | * the runtime footprint, and giving us at least some parts of what | |
23 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
24 | */ | |
25 | ||
26 | #include "composite.c" | |
c6c56008 MN |
27 | |
28 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS | |
29 | # if defined USB_ETH_RNDIS | |
30 | # undef USB_ETH_RNDIS | |
31 | # endif | |
32 | # ifdef CONFIG_USB_FUNCTIONFS_RNDIS | |
33 | # define USB_ETH_RNDIS y | |
34 | # endif | |
35 | ||
36 | # include "f_ecm.c" | |
37 | # include "f_subset.c" | |
38 | # ifdef USB_ETH_RNDIS | |
39 | # include "f_rndis.c" | |
40 | # include "rndis.c" | |
41 | # endif | |
42 | # include "u_ether.c" | |
43 | ||
44 | static u8 gfs_hostaddr[ETH_ALEN]; | |
f8dae531 MN |
45 | # ifdef CONFIG_USB_FUNCTIONFS_ETH |
46 | static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); | |
c6c56008 | 47 | # endif |
f8dae531 | 48 | #else |
c6c56008 MN |
49 | # define gether_cleanup() do { } while (0) |
50 | # define gether_setup(gadget, hostaddr) ((int)0) | |
f8dae531 | 51 | # define gfs_hostaddr NULL |
c6c56008 MN |
52 | #endif |
53 | ||
54 | #include "f_fs.c" | |
55 | ||
c6c56008 MN |
56 | #define DRIVER_NAME "g_ffs" |
57 | #define DRIVER_DESC "USB Function Filesystem" | |
58 | #define DRIVER_VERSION "24 Aug 2004" | |
59 | ||
60 | MODULE_DESCRIPTION(DRIVER_DESC); | |
61 | MODULE_AUTHOR("Michal Nazarewicz"); | |
62 | MODULE_LICENSE("GPL"); | |
63 | ||
fc19de61 MN |
64 | #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */ |
65 | #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */ | |
c6c56008 | 66 | |
581791f5 AP |
67 | #define GFS_MAX_DEVS 10 |
68 | ||
69 | struct gfs_ffs_obj { | |
70 | const char *name; | |
71 | bool mounted; | |
72 | bool desc_ready; | |
73 | struct ffs_data *ffs_data; | |
74 | }; | |
75 | ||
7d16e8d3 SAS |
76 | USB_GADGET_COMPOSITE_OPTIONS(); |
77 | ||
c6c56008 MN |
78 | static struct usb_device_descriptor gfs_dev_desc = { |
79 | .bLength = sizeof gfs_dev_desc, | |
80 | .bDescriptorType = USB_DT_DEVICE, | |
81 | ||
82 | .bcdUSB = cpu_to_le16(0x0200), | |
83 | .bDeviceClass = USB_CLASS_PER_INTERFACE, | |
84 | ||
fc19de61 MN |
85 | .idVendor = cpu_to_le16(GFS_VENDOR_ID), |
86 | .idProduct = cpu_to_le16(GFS_PRODUCT_ID), | |
c6c56008 MN |
87 | }; |
88 | ||
581791f5 AP |
89 | static char *func_names[GFS_MAX_DEVS]; |
90 | static unsigned int func_num; | |
91 | ||
fc19de61 MN |
92 | module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644); |
93 | MODULE_PARM_DESC(bDeviceClass, "USB Device class"); | |
94 | module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644); | |
95 | MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass"); | |
96 | module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644); | |
97 | MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol"); | |
581791f5 AP |
98 | module_param_array_named(functions, func_names, charp, &func_num, 0); |
99 | MODULE_PARM_DESC(functions, "USB Functions list"); | |
c6c56008 | 100 | |
c6c56008 MN |
101 | static const struct usb_descriptor_header *gfs_otg_desc[] = { |
102 | (const struct usb_descriptor_header *) | |
103 | &(const struct usb_otg_descriptor) { | |
104 | .bLength = sizeof(struct usb_otg_descriptor), | |
105 | .bDescriptorType = USB_DT_OTG, | |
106 | ||
fc19de61 MN |
107 | /* |
108 | * REVISIT SRP-only hardware is possible, although | |
109 | * it would not be called "OTG" ... | |
110 | */ | |
c6c56008 MN |
111 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
112 | }, | |
113 | ||
114 | NULL | |
115 | }; | |
116 | ||
5ab54cf7 | 117 | /* String IDs are assigned dynamically */ |
c6c56008 | 118 | static struct usb_string gfs_strings[] = { |
276e2e4f SAS |
119 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
120 | [USB_GADGET_PRODUCT_IDX].s = "", | |
121 | [USB_GADGET_SERIAL_IDX].s = "", | |
c6c56008 | 122 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
f8dae531 | 123 | { .s = "FunctionFS + RNDIS" }, |
c6c56008 MN |
124 | #endif |
125 | #ifdef CONFIG_USB_FUNCTIONFS_ETH | |
f8dae531 | 126 | { .s = "FunctionFS + ECM" }, |
c6c56008 MN |
127 | #endif |
128 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC | |
f8dae531 | 129 | { .s = "FunctionFS" }, |
c6c56008 MN |
130 | #endif |
131 | { } /* end of list */ | |
132 | }; | |
133 | ||
134 | static struct usb_gadget_strings *gfs_dev_strings[] = { | |
135 | &(struct usb_gadget_strings) { | |
136 | .language = 0x0409, /* en-us */ | |
137 | .strings = gfs_strings, | |
138 | }, | |
139 | NULL, | |
140 | }; | |
141 | ||
f8dae531 MN |
142 | struct gfs_configuration { |
143 | struct usb_configuration c; | |
144 | int (*eth)(struct usb_configuration *c, u8 *ethaddr); | |
145 | } gfs_configurations[] = { | |
c6c56008 | 146 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
f8dae531 MN |
147 | { |
148 | .eth = rndis_bind_config, | |
149 | }, | |
c6c56008 MN |
150 | #endif |
151 | ||
c6c56008 | 152 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
f8dae531 MN |
153 | { |
154 | .eth = eth_bind_config, | |
155 | }, | |
c6c56008 MN |
156 | #endif |
157 | ||
c6c56008 | 158 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
f8dae531 MN |
159 | { |
160 | }, | |
c6c56008 | 161 | #endif |
f8dae531 | 162 | }; |
c6c56008 | 163 | |
c6c56008 MN |
164 | static int gfs_bind(struct usb_composite_dev *cdev); |
165 | static int gfs_unbind(struct usb_composite_dev *cdev); | |
f8dae531 | 166 | static int gfs_do_config(struct usb_configuration *c); |
c6c56008 | 167 | |
c2ec75c2 | 168 | static __refdata struct usb_composite_driver gfs_driver = { |
fc19de61 | 169 | .name = DRIVER_NAME, |
c6c56008 MN |
170 | .dev = &gfs_dev_desc, |
171 | .strings = gfs_dev_strings, | |
35a0e0bf | 172 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 173 | .bind = gfs_bind, |
c6c56008 | 174 | .unbind = gfs_unbind, |
fc19de61 | 175 | .iProduct = DRIVER_DESC, |
c6c56008 MN |
176 | }; |
177 | ||
581791f5 AP |
178 | static DEFINE_MUTEX(gfs_lock); |
179 | static unsigned int missing_funcs; | |
180 | static bool gfs_ether_setup; | |
181 | static bool gfs_registered; | |
182 | static bool gfs_single_func; | |
183 | static struct gfs_ffs_obj *ffs_tab; | |
c6c56008 | 184 | |
8545e603 | 185 | static int __init gfs_init(void) |
c6c56008 | 186 | { |
581791f5 AP |
187 | int i; |
188 | ||
c6c56008 MN |
189 | ENTER(); |
190 | ||
581791f5 AP |
191 | if (!func_num) { |
192 | gfs_single_func = true; | |
193 | func_num = 1; | |
194 | } | |
195 | ||
196 | ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL); | |
197 | if (!ffs_tab) | |
198 | return -ENOMEM; | |
199 | ||
200 | if (!gfs_single_func) | |
201 | for (i = 0; i < func_num; i++) | |
202 | ffs_tab[i].name = func_names[i]; | |
203 | ||
204 | missing_funcs = func_num; | |
205 | ||
c6c56008 MN |
206 | return functionfs_init(); |
207 | } | |
208 | module_init(gfs_init); | |
209 | ||
8545e603 | 210 | static void __exit gfs_exit(void) |
c6c56008 MN |
211 | { |
212 | ENTER(); | |
581791f5 | 213 | mutex_lock(&gfs_lock); |
c6c56008 | 214 | |
581791f5 | 215 | if (gfs_registered) |
c6c56008 | 216 | usb_composite_unregister(&gfs_driver); |
581791f5 | 217 | gfs_registered = false; |
c6c56008 MN |
218 | |
219 | functionfs_cleanup(); | |
581791f5 AP |
220 | |
221 | mutex_unlock(&gfs_lock); | |
222 | kfree(ffs_tab); | |
c6c56008 MN |
223 | } |
224 | module_exit(gfs_exit); | |
225 | ||
581791f5 AP |
226 | static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name) |
227 | { | |
228 | int i; | |
229 | ||
230 | ENTER(); | |
231 | ||
232 | if (gfs_single_func) | |
233 | return &ffs_tab[0]; | |
234 | ||
235 | for (i = 0; i < func_num; i++) | |
236 | if (strcmp(ffs_tab[i].name, dev_name) == 0) | |
237 | return &ffs_tab[i]; | |
238 | ||
239 | return NULL; | |
240 | } | |
241 | ||
c6c56008 MN |
242 | static int functionfs_ready_callback(struct ffs_data *ffs) |
243 | { | |
581791f5 | 244 | struct gfs_ffs_obj *ffs_obj; |
c6c56008 MN |
245 | int ret; |
246 | ||
247 | ENTER(); | |
581791f5 AP |
248 | mutex_lock(&gfs_lock); |
249 | ||
250 | ffs_obj = ffs->private_data; | |
251 | if (!ffs_obj) { | |
252 | ret = -EINVAL; | |
253 | goto done; | |
254 | } | |
255 | ||
256 | if (WARN_ON(ffs_obj->desc_ready)) { | |
257 | ret = -EBUSY; | |
258 | goto done; | |
259 | } | |
260 | ffs_obj->desc_ready = true; | |
261 | ffs_obj->ffs_data = ffs; | |
c6c56008 | 262 | |
581791f5 AP |
263 | if (--missing_funcs) { |
264 | ret = 0; | |
265 | goto done; | |
266 | } | |
267 | ||
268 | if (gfs_registered) { | |
269 | ret = -EBUSY; | |
270 | goto done; | |
271 | } | |
272 | gfs_registered = true; | |
c6c56008 | 273 | |
03e42bd5 | 274 | ret = usb_composite_probe(&gfs_driver); |
c6c56008 | 275 | if (unlikely(ret < 0)) |
581791f5 AP |
276 | gfs_registered = false; |
277 | ||
278 | done: | |
279 | mutex_unlock(&gfs_lock); | |
c6c56008 MN |
280 | return ret; |
281 | } | |
282 | ||
283 | static void functionfs_closed_callback(struct ffs_data *ffs) | |
284 | { | |
581791f5 AP |
285 | struct gfs_ffs_obj *ffs_obj; |
286 | ||
c6c56008 | 287 | ENTER(); |
581791f5 | 288 | mutex_lock(&gfs_lock); |
c6c56008 | 289 | |
581791f5 AP |
290 | ffs_obj = ffs->private_data; |
291 | if (!ffs_obj) | |
292 | goto done; | |
293 | ||
294 | ffs_obj->desc_ready = false; | |
295 | missing_funcs++; | |
296 | ||
297 | if (gfs_registered) | |
c6c56008 | 298 | usb_composite_unregister(&gfs_driver); |
581791f5 AP |
299 | gfs_registered = false; |
300 | ||
301 | done: | |
302 | mutex_unlock(&gfs_lock); | |
c6c56008 MN |
303 | } |
304 | ||
581791f5 | 305 | static void *functionfs_acquire_dev_callback(const char *dev_name) |
c6c56008 | 306 | { |
581791f5 AP |
307 | struct gfs_ffs_obj *ffs_dev; |
308 | ||
309 | ENTER(); | |
310 | mutex_lock(&gfs_lock); | |
311 | ||
312 | ffs_dev = gfs_find_dev(dev_name); | |
313 | if (!ffs_dev) { | |
314 | ffs_dev = ERR_PTR(-ENODEV); | |
315 | goto done; | |
316 | } | |
317 | ||
318 | if (ffs_dev->mounted) { | |
319 | ffs_dev = ERR_PTR(-EBUSY); | |
320 | goto done; | |
321 | } | |
322 | ffs_dev->mounted = true; | |
323 | ||
324 | done: | |
325 | mutex_unlock(&gfs_lock); | |
326 | return ffs_dev; | |
c6c56008 MN |
327 | } |
328 | ||
581791f5 AP |
329 | static void functionfs_release_dev_callback(struct ffs_data *ffs_data) |
330 | { | |
331 | struct gfs_ffs_obj *ffs_dev; | |
332 | ||
333 | ENTER(); | |
334 | mutex_lock(&gfs_lock); | |
335 | ||
336 | ffs_dev = ffs_data->private_data; | |
337 | if (ffs_dev) | |
338 | ffs_dev->mounted = false; | |
339 | ||
340 | mutex_unlock(&gfs_lock); | |
341 | } | |
342 | ||
343 | /* | |
344 | * It is assumed that gfs_bind is called from a context where gfs_lock is held | |
345 | */ | |
c6c56008 MN |
346 | static int gfs_bind(struct usb_composite_dev *cdev) |
347 | { | |
f8dae531 | 348 | int ret, i; |
c6c56008 MN |
349 | |
350 | ENTER(); | |
351 | ||
581791f5 | 352 | if (missing_funcs) |
c6c56008 MN |
353 | return -ENODEV; |
354 | ||
355 | ret = gether_setup(cdev->gadget, gfs_hostaddr); | |
356 | if (unlikely(ret < 0)) | |
357 | goto error_quick; | |
581791f5 | 358 | gfs_ether_setup = true; |
c6c56008 | 359 | |
f8dae531 | 360 | ret = usb_string_ids_tab(cdev, gfs_strings); |
c6c56008 MN |
361 | if (unlikely(ret < 0)) |
362 | goto error; | |
c6c56008 | 363 | |
581791f5 AP |
364 | for (i = func_num; --i; ) { |
365 | ret = functionfs_bind(ffs_tab[i].ffs_data, cdev); | |
366 | if (unlikely(ret < 0)) { | |
367 | while (++i < func_num) | |
368 | functionfs_unbind(ffs_tab[i].ffs_data); | |
369 | goto error; | |
370 | } | |
371 | } | |
c6c56008 | 372 | |
f8dae531 MN |
373 | for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) { |
374 | struct gfs_configuration *c = gfs_configurations + i; | |
276e2e4f | 375 | int sid = USB_GADGET_FIRST_AVAIL_IDX + i; |
c6c56008 | 376 | |
276e2e4f SAS |
377 | c->c.label = gfs_strings[sid].s; |
378 | c->c.iConfiguration = gfs_strings[sid].id; | |
f8dae531 MN |
379 | c->c.bConfigurationValue = 1 + i; |
380 | c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER; | |
c6c56008 | 381 | |
c9bfff9c | 382 | ret = usb_add_config(cdev, &c->c, gfs_do_config); |
f8dae531 MN |
383 | if (unlikely(ret < 0)) |
384 | goto error_unbind; | |
385 | } | |
7d16e8d3 | 386 | usb_composite_overwrite_options(cdev, &coverwrite); |
c6c56008 MN |
387 | return 0; |
388 | ||
389 | error_unbind: | |
581791f5 AP |
390 | for (i = 0; i < func_num; i++) |
391 | functionfs_unbind(ffs_tab[i].ffs_data); | |
c6c56008 MN |
392 | error: |
393 | gether_cleanup(); | |
394 | error_quick: | |
581791f5 | 395 | gfs_ether_setup = false; |
c6c56008 MN |
396 | return ret; |
397 | } | |
398 | ||
581791f5 AP |
399 | /* |
400 | * It is assumed that gfs_unbind is called from a context where gfs_lock is held | |
401 | */ | |
c6c56008 MN |
402 | static int gfs_unbind(struct usb_composite_dev *cdev) |
403 | { | |
581791f5 AP |
404 | int i; |
405 | ||
c6c56008 MN |
406 | ENTER(); |
407 | ||
fc19de61 MN |
408 | /* |
409 | * We may have been called in an error recovery from | |
c6c56008 MN |
410 | * composite_bind() after gfs_unbind() failure so we need to |
411 | * check if gfs_ffs_data is not NULL since gfs_bind() handles | |
412 | * all error recovery itself. I'd rather we werent called | |
413 | * from composite on orror recovery, but what you're gonna | |
fc19de61 MN |
414 | * do...? |
415 | */ | |
581791f5 | 416 | if (gfs_ether_setup) |
c6c56008 | 417 | gether_cleanup(); |
581791f5 AP |
418 | gfs_ether_setup = false; |
419 | ||
420 | for (i = func_num; --i; ) | |
421 | if (ffs_tab[i].ffs_data) | |
422 | functionfs_unbind(ffs_tab[i].ffs_data); | |
c6c56008 MN |
423 | |
424 | return 0; | |
425 | } | |
426 | ||
581791f5 AP |
427 | /* |
428 | * It is assumed that gfs_do_config is called from a context where | |
429 | * gfs_lock is held | |
430 | */ | |
f8dae531 | 431 | static int gfs_do_config(struct usb_configuration *c) |
c6c56008 | 432 | { |
f8dae531 MN |
433 | struct gfs_configuration *gc = |
434 | container_of(c, struct gfs_configuration, c); | |
581791f5 | 435 | int i; |
c6c56008 MN |
436 | int ret; |
437 | ||
581791f5 | 438 | if (missing_funcs) |
c6c56008 MN |
439 | return -ENODEV; |
440 | ||
441 | if (gadget_is_otg(c->cdev->gadget)) { | |
442 | c->descriptors = gfs_otg_desc; | |
443 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
444 | } | |
445 | ||
f8dae531 MN |
446 | if (gc->eth) { |
447 | ret = gc->eth(c, gfs_hostaddr); | |
c6c56008 MN |
448 | if (unlikely(ret < 0)) |
449 | return ret; | |
450 | } | |
451 | ||
581791f5 AP |
452 | for (i = 0; i < func_num; i++) { |
453 | ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data); | |
454 | if (unlikely(ret < 0)) | |
455 | return ret; | |
456 | } | |
c6c56008 | 457 | |
fc19de61 MN |
458 | /* |
459 | * After previous do_configs there may be some invalid | |
f588c0db MN |
460 | * pointers in c->interface array. This happens every time |
461 | * a user space function with fewer interfaces than a user | |
462 | * space function that was run before the new one is run. The | |
463 | * compasit's set_config() assumes that if there is no more | |
464 | * then MAX_CONFIG_INTERFACES interfaces in a configuration | |
465 | * then there is a NULL pointer after the last interface in | |
fc19de61 MN |
466 | * c->interface array. We need to make sure this is true. |
467 | */ | |
f588c0db MN |
468 | if (c->next_interface_id < ARRAY_SIZE(c->interface)) |
469 | c->interface[c->next_interface_id] = NULL; | |
470 | ||
c6c56008 MN |
471 | return 0; |
472 | } | |
473 | ||
c6c56008 | 474 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
fc19de61 | 475 | |
f8dae531 | 476 | static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) |
c6c56008 | 477 | { |
f8dae531 MN |
478 | return can_support_ecm(c->cdev->gadget) |
479 | ? ecm_bind_config(c, ethaddr) | |
480 | : geth_bind_config(c, ethaddr); | |
c6c56008 | 481 | } |
fc19de61 | 482 | |
c6c56008 | 483 | #endif |