diff options
-rw-r--r-- | drivers/usb/gadget/Kconfig | 3 | ||||
-rw-r--r-- | drivers/usb/gadget/Makefile | 2 | ||||
-rw-r--r-- | drivers/usb/gadget/f_fs.c | 422 | ||||
-rw-r--r-- | drivers/usb/gadget/g_ffs.c | 1 | ||||
-rw-r--r-- | drivers/usb/gadget/u_fs.h | 214 | ||||
-rw-r--r-- | include/linux/usb/functionfs.h | 2 |
6 files changed, 435 insertions, 209 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index aa3610ea9d9f..1761a3b8b31a 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig | |||
@@ -539,6 +539,9 @@ config USB_F_RNDIS | |||
539 | config USB_F_MASS_STORAGE | 539 | config USB_F_MASS_STORAGE |
540 | tristate | 540 | tristate |
541 | 541 | ||
542 | config USB_F_FS | ||
543 | tristate | ||
544 | |||
542 | choice | 545 | choice |
543 | tristate "USB Gadget Drivers" | 546 | tristate "USB Gadget Drivers" |
544 | default USB_ETH | 547 | default USB_ETH |
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 0c40277931f4..6cccdfed140c 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile | |||
@@ -61,6 +61,8 @@ usb_f_rndis-y := f_rndis.o rndis.o | |||
61 | obj-$(CONFIG_USB_F_RNDIS) += usb_f_rndis.o | 61 | obj-$(CONFIG_USB_F_RNDIS) += usb_f_rndis.o |
62 | usb_f_mass_storage-y := f_mass_storage.o storage_common.o | 62 | usb_f_mass_storage-y := f_mass_storage.o storage_common.o |
63 | obj-$(CONFIG_USB_F_MASS_STORAGE)+= usb_f_mass_storage.o | 63 | obj-$(CONFIG_USB_F_MASS_STORAGE)+= usb_f_mass_storage.o |
64 | usb_f_fs-y := f_fs.o | ||
65 | obj-$(CONFIG_USB_F_FS) += usb_f_fs.o | ||
64 | 66 | ||
65 | # | 67 | # |
66 | # USB gadget drivers | 68 | # USB gadget drivers |
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c index d17930d9a843..8d318eaaaf20 100644 --- a/drivers/usb/gadget/f_fs.c +++ b/drivers/usb/gadget/f_fs.c | |||
@@ -22,6 +22,7 @@ | |||
22 | #include <linux/pagemap.h> | 22 | #include <linux/pagemap.h> |
23 | #include <linux/export.h> | 23 | #include <linux/export.h> |
24 | #include <linux/hid.h> | 24 | #include <linux/hid.h> |
25 | #include <linux/module.h> | ||
25 | #include <asm/unaligned.h> | 26 | #include <asm/unaligned.h> |
26 | 27 | ||
27 | #include <linux/usb/composite.h> | 28 | #include <linux/usb/composite.h> |
@@ -57,210 +58,6 @@ | |||
57 | #define vla_ptr(ptr, groupname, name) \ | 58 | #define vla_ptr(ptr, groupname, name) \ |
58 | ((void *) ((char *)ptr + groupname##_##name##__offset)) | 59 | ((void *) ((char *)ptr + groupname##_##name##__offset)) |
59 | 60 | ||
60 | /* Debugging ****************************************************************/ | ||
61 | |||
62 | #ifdef VERBOSE_DEBUG | ||
63 | #ifndef pr_vdebug | ||
64 | # define pr_vdebug pr_debug | ||
65 | #endif /* pr_vdebug */ | ||
66 | # define ffs_dump_mem(prefix, ptr, len) \ | ||
67 | print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len) | ||
68 | #else | ||
69 | #ifndef pr_vdebug | ||
70 | # define pr_vdebug(...) do { } while (0) | ||
71 | #endif /* pr_vdebug */ | ||
72 | # define ffs_dump_mem(prefix, ptr, len) do { } while (0) | ||
73 | #endif /* VERBOSE_DEBUG */ | ||
74 | |||
75 | #define ENTER() pr_vdebug("%s()\n", __func__) | ||
76 | |||
77 | |||
78 | /* The data structure and setup file ****************************************/ | ||
79 | |||
80 | enum ffs_state { | ||
81 | /* | ||
82 | * Waiting for descriptors and strings. | ||
83 | * | ||
84 | * In this state no open(2), read(2) or write(2) on epfiles | ||
85 | * may succeed (which should not be the problem as there | ||
86 | * should be no such files opened in the first place). | ||
87 | */ | ||
88 | FFS_READ_DESCRIPTORS, | ||
89 | FFS_READ_STRINGS, | ||
90 | |||
91 | /* | ||
92 | * We've got descriptors and strings. We are or have called | ||
93 | * ffs_ready(). functionfs_bind() may have | ||
94 | * been called but we don't know. | ||
95 | * | ||
96 | * This is the only state in which operations on epfiles may | ||
97 | * succeed. | ||
98 | */ | ||
99 | FFS_ACTIVE, | ||
100 | |||
101 | /* | ||
102 | * All endpoints have been closed. This state is also set if | ||
103 | * we encounter an unrecoverable error. The only | ||
104 | * unrecoverable error is situation when after reading strings | ||
105 | * from user space we fail to initialise epfiles or | ||
106 | * ffs_ready() returns with error (<0). | ||
107 | * | ||
108 | * In this state no open(2), read(2) or write(2) (both on ep0 | ||
109 | * as well as epfile) may succeed (at this point epfiles are | ||
110 | * unlinked and all closed so this is not a problem; ep0 is | ||
111 | * also closed but ep0 file exists and so open(2) on ep0 must | ||
112 | * fail). | ||
113 | */ | ||
114 | FFS_CLOSING | ||
115 | }; | ||
116 | |||
117 | |||
118 | enum ffs_setup_state { | ||
119 | /* There is no setup request pending. */ | ||
120 | FFS_NO_SETUP, | ||
121 | /* | ||
122 | * User has read events and there was a setup request event | ||
123 | * there. The next read/write on ep0 will handle the | ||
124 | * request. | ||
125 | */ | ||
126 | FFS_SETUP_PENDING, | ||
127 | /* | ||
128 | * There was event pending but before user space handled it | ||
129 | * some other event was introduced which canceled existing | ||
130 | * setup. If this state is set read/write on ep0 return | ||
131 | * -EIDRM. This state is only set when adding event. | ||
132 | */ | ||
133 | FFS_SETUP_CANCELED | ||
134 | }; | ||
135 | |||
136 | |||
137 | |||
138 | struct ffs_epfile; | ||
139 | struct ffs_function; | ||
140 | |||
141 | struct ffs_data { | ||
142 | struct usb_gadget *gadget; | ||
143 | |||
144 | /* | ||
145 | * Protect access read/write operations, only one read/write | ||
146 | * at a time. As a consequence protects ep0req and company. | ||
147 | * While setup request is being processed (queued) this is | ||
148 | * held. | ||
149 | */ | ||
150 | struct mutex mutex; | ||
151 | |||
152 | /* | ||
153 | * Protect access to endpoint related structures (basically | ||
154 | * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for | ||
155 | * endpoint zero. | ||
156 | */ | ||
157 | spinlock_t eps_lock; | ||
158 | |||
159 | /* | ||
160 | * XXX REVISIT do we need our own request? Since we are not | ||
161 | * handling setup requests immediately user space may be so | ||
162 | * slow that another setup will be sent to the gadget but this | ||
163 | * time not to us but another function and then there could be | ||
164 | * a race. Is that the case? Or maybe we can use cdev->req | ||
165 | * after all, maybe we just need some spinlock for that? | ||
166 | */ | ||
167 | struct usb_request *ep0req; /* P: mutex */ | ||
168 | struct completion ep0req_completion; /* P: mutex */ | ||
169 | int ep0req_status; /* P: mutex */ | ||
170 | |||
171 | /* reference counter */ | ||
172 | atomic_t ref; | ||
173 | /* how many files are opened (EP0 and others) */ | ||
174 | atomic_t opened; | ||
175 | |||
176 | /* EP0 state */ | ||
177 | enum ffs_state state; | ||
178 | |||
179 | /* | ||
180 | * Possible transitions: | ||
181 | * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock | ||
182 | * happens only in ep0 read which is P: mutex | ||
183 | * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock | ||
184 | * happens only in ep0 i/o which is P: mutex | ||
185 | * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock | ||
186 | * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg | ||
187 | */ | ||
188 | enum ffs_setup_state setup_state; | ||
189 | |||
190 | #define FFS_SETUP_STATE(ffs) \ | ||
191 | ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \ | ||
192 | FFS_SETUP_CANCELED, FFS_NO_SETUP)) | ||
193 | |||
194 | /* Events & such. */ | ||
195 | struct { | ||
196 | u8 types[4]; | ||
197 | unsigned short count; | ||
198 | /* XXX REVISIT need to update it in some places, or do we? */ | ||
199 | unsigned short can_stall; | ||
200 | struct usb_ctrlrequest setup; | ||
201 | |||
202 | wait_queue_head_t waitq; | ||
203 | } ev; /* the whole structure, P: ev.waitq.lock */ | ||
204 | |||
205 | /* Flags */ | ||
206 | unsigned long flags; | ||
207 | #define FFS_FL_CALL_CLOSED_CALLBACK 0 | ||
208 | #define FFS_FL_BOUND 1 | ||
209 | |||
210 | /* Active function */ | ||
211 | struct ffs_function *func; | ||
212 | |||
213 | /* | ||
214 | * Device name, write once when file system is mounted. | ||
215 | * Intended for user to read if she wants. | ||
216 | */ | ||
217 | const char *dev_name; | ||
218 | /* Private data for our user (ie. gadget). Managed by user. */ | ||
219 | void *private_data; | ||
220 | |||
221 | /* filled by __ffs_data_got_descs() */ | ||
222 | /* | ||
223 | * Real descriptors are 16 bytes after raw_descs (so you need | ||
224 | * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the | ||
225 | * first full speed descriptor). raw_descs_length and | ||
226 | * raw_fs_descs_length do not have those 16 bytes added. | ||
227 | */ | ||
228 | const void *raw_descs; | ||
229 | unsigned raw_descs_length; | ||
230 | unsigned raw_fs_descs_length; | ||
231 | unsigned fs_descs_count; | ||
232 | unsigned hs_descs_count; | ||
233 | |||
234 | unsigned short strings_count; | ||
235 | unsigned short interfaces_count; | ||
236 | unsigned short eps_count; | ||
237 | unsigned short _pad1; | ||
238 | |||
239 | /* filled by __ffs_data_got_strings() */ | ||
240 | /* ids in stringtabs are set in functionfs_bind() */ | ||
241 | const void *raw_strings; | ||
242 | struct usb_gadget_strings **stringtabs; | ||
243 | |||
244 | /* | ||
245 | * File system's super block, write once when file system is | ||
246 | * mounted. | ||
247 | */ | ||
248 | struct super_block *sb; | ||
249 | |||
250 | /* File permissions, written once when fs is mounted */ | ||
251 | struct ffs_file_perms { | ||
252 | umode_t mode; | ||
253 | kuid_t uid; | ||
254 | kgid_t gid; | ||
255 | } file_perms; | ||
256 | |||
257 | /* | ||
258 | * The endpoint files, filled by ffs_epfiles_create(), | ||
259 | * destroyed by ffs_epfiles_destroy(). | ||
260 | */ | ||
261 | struct ffs_epfile *epfiles; | ||
262 | }; | ||
263 | |||
264 | /* Reference counter handling */ | 61 | /* Reference counter handling */ |
265 | static void ffs_data_get(struct ffs_data *ffs); | 62 | static void ffs_data_get(struct ffs_data *ffs); |
266 | static void ffs_data_put(struct ffs_data *ffs); | 63 | static void ffs_data_put(struct ffs_data *ffs); |
@@ -300,15 +97,19 @@ static struct ffs_function *ffs_func_from_usb(struct usb_function *f) | |||
300 | return container_of(f, struct ffs_function, function); | 97 | return container_of(f, struct ffs_function, function); |
301 | } | 98 | } |
302 | 99 | ||
100 | #ifdef USB_FFS_INCLUDED | ||
303 | static void ffs_func_free(struct ffs_function *func); | 101 | static void ffs_func_free(struct ffs_function *func); |
102 | #endif | ||
304 | 103 | ||
305 | static void ffs_func_eps_disable(struct ffs_function *func); | 104 | static void ffs_func_eps_disable(struct ffs_function *func); |
306 | static int __must_check ffs_func_eps_enable(struct ffs_function *func); | 105 | static int __must_check ffs_func_eps_enable(struct ffs_function *func); |
307 | 106 | ||
308 | static int ffs_func_bind(struct usb_configuration *, | 107 | static int ffs_func_bind(struct usb_configuration *, |
309 | struct usb_function *); | 108 | struct usb_function *); |
310 | static void ffs_func_unbind(struct usb_configuration *, | 109 | #ifdef USB_FFS_INCLUDED |
110 | static void old_ffs_func_unbind(struct usb_configuration *, | ||
311 | struct usb_function *); | 111 | struct usb_function *); |
112 | #endif | ||
312 | static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); | 113 | static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); |
313 | static void ffs_func_disable(struct usb_function *); | 114 | static void ffs_func_disable(struct usb_function *); |
314 | static int ffs_func_setup(struct usb_function *, | 115 | static int ffs_func_setup(struct usb_function *, |
@@ -364,6 +165,9 @@ ffs_sb_create_file(struct super_block *sb, const char *name, void *data, | |||
364 | /* Devices management *******************************************************/ | 165 | /* Devices management *******************************************************/ |
365 | 166 | ||
366 | DEFINE_MUTEX(ffs_lock); | 167 | DEFINE_MUTEX(ffs_lock); |
168 | #ifndef USB_FFS_INCLUDED | ||
169 | EXPORT_SYMBOL(ffs_lock); | ||
170 | #endif | ||
367 | 171 | ||
368 | static struct ffs_dev *ffs_find_dev(const char *name); | 172 | static struct ffs_dev *ffs_find_dev(const char *name); |
369 | static void *ffs_acquire_dev(const char *dev_name); | 173 | static void *ffs_acquire_dev(const char *dev_name); |
@@ -1499,6 +1303,8 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) | |||
1499 | kfree(epfiles); | 1303 | kfree(epfiles); |
1500 | } | 1304 | } |
1501 | 1305 | ||
1306 | #ifdef USB_FFS_INCLUDED | ||
1307 | |||
1502 | static int functionfs_bind_config(struct usb_composite_dev *cdev, | 1308 | static int functionfs_bind_config(struct usb_composite_dev *cdev, |
1503 | struct usb_configuration *c, | 1309 | struct usb_configuration *c, |
1504 | struct ffs_data *ffs) | 1310 | struct ffs_data *ffs) |
@@ -1516,7 +1322,7 @@ static int functionfs_bind_config(struct usb_composite_dev *cdev, | |||
1516 | func->function.strings = ffs->stringtabs; | 1322 | func->function.strings = ffs->stringtabs; |
1517 | 1323 | ||
1518 | func->function.bind = ffs_func_bind; | 1324 | func->function.bind = ffs_func_bind; |
1519 | func->function.unbind = ffs_func_unbind; | 1325 | func->function.unbind = old_ffs_func_unbind; |
1520 | func->function.set_alt = ffs_func_set_alt; | 1326 | func->function.set_alt = ffs_func_set_alt; |
1521 | func->function.disable = ffs_func_disable; | 1327 | func->function.disable = ffs_func_disable; |
1522 | func->function.setup = ffs_func_setup; | 1328 | func->function.setup = ffs_func_setup; |
@@ -1565,6 +1371,8 @@ static void ffs_func_free(struct ffs_function *func) | |||
1565 | kfree(func); | 1371 | kfree(func); |
1566 | } | 1372 | } |
1567 | 1373 | ||
1374 | #endif | ||
1375 | |||
1568 | static void ffs_func_eps_disable(struct ffs_function *func) | 1376 | static void ffs_func_eps_disable(struct ffs_function *func) |
1569 | { | 1377 | { |
1570 | struct ffs_ep *ep = func->eps; | 1378 | struct ffs_ep *ep = func->eps; |
@@ -2227,8 +2035,59 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, | |||
2227 | return 0; | 2035 | return 0; |
2228 | } | 2036 | } |
2229 | 2037 | ||
2230 | static int ffs_func_bind(struct usb_configuration *c, | 2038 | #ifndef USB_FFS_INCLUDED |
2231 | struct usb_function *f) | 2039 | static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f, |
2040 | struct usb_configuration *c) | ||
2041 | { | ||
2042 | struct ffs_function *func = ffs_func_from_usb(f); | ||
2043 | struct f_fs_opts *ffs_opts = | ||
2044 | container_of(f->fi, struct f_fs_opts, func_inst); | ||
2045 | int ret; | ||
2046 | |||
2047 | ENTER(); | ||
2048 | |||
2049 | /* | ||
2050 | * Legacy gadget triggers binding in functionfs_ready_callback, | ||
2051 | * which already uses locking; taking the same lock here would | ||
2052 | * cause a deadlock. | ||
2053 | * | ||
2054 | * Configfs-enabled gadgets however do need ffs_dev_lock. | ||
2055 | */ | ||
2056 | if (!ffs_opts->no_configfs) | ||
2057 | ffs_dev_lock(); | ||
2058 | ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; | ||
2059 | func->ffs = ffs_opts->dev->ffs_data; | ||
2060 | if (!ffs_opts->no_configfs) | ||
2061 | ffs_dev_unlock(); | ||
2062 | if (ret) | ||
2063 | return ERR_PTR(ret); | ||
2064 | |||
2065 | func->conf = c; | ||
2066 | func->gadget = c->cdev->gadget; | ||
2067 | |||
2068 | ffs_data_get(func->ffs); | ||
2069 | |||
2070 | /* | ||
2071 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() | ||
2072 | * configurations are bound in sequence with list_for_each_entry, | ||
2073 | * in each configuration its functions are bound in sequence | ||
2074 | * with list_for_each_entry, so we assume no race condition | ||
2075 | * with regard to ffs_opts->bound access | ||
2076 | */ | ||
2077 | if (!ffs_opts->refcnt) { | ||
2078 | ret = functionfs_bind(func->ffs, c->cdev); | ||
2079 | if (ret) | ||
2080 | return ERR_PTR(ret); | ||
2081 | } | ||
2082 | ffs_opts->refcnt++; | ||
2083 | func->function.strings = func->ffs->stringtabs; | ||
2084 | |||
2085 | return ffs_opts; | ||
2086 | } | ||
2087 | #endif | ||
2088 | |||
2089 | static int _ffs_func_bind(struct usb_configuration *c, | ||
2090 | struct usb_function *f) | ||
2232 | { | 2091 | { |
2233 | struct ffs_function *func = ffs_func_from_usb(f); | 2092 | struct ffs_function *func = ffs_func_from_usb(f); |
2234 | struct ffs_data *ffs = func->ffs; | 2093 | struct ffs_data *ffs = func->ffs; |
@@ -2328,10 +2187,25 @@ error: | |||
2328 | return ret; | 2187 | return ret; |
2329 | } | 2188 | } |
2330 | 2189 | ||
2190 | static int ffs_func_bind(struct usb_configuration *c, | ||
2191 | struct usb_function *f) | ||
2192 | { | ||
2193 | #ifndef USB_FFS_INCLUDED | ||
2194 | struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c); | ||
2195 | |||
2196 | if (IS_ERR(ffs_opts)) | ||
2197 | return PTR_ERR(ffs_opts); | ||
2198 | #endif | ||
2199 | |||
2200 | return _ffs_func_bind(c, f); | ||
2201 | } | ||
2202 | |||
2331 | 2203 | ||
2332 | /* Other USB function hooks *************************************************/ | 2204 | /* Other USB function hooks *************************************************/ |
2333 | 2205 | ||
2334 | static void ffs_func_unbind(struct usb_configuration *c, | 2206 | #ifdef USB_FFS_INCLUDED |
2207 | |||
2208 | static void old_ffs_func_unbind(struct usb_configuration *c, | ||
2335 | struct usb_function *f) | 2209 | struct usb_function *f) |
2336 | { | 2210 | { |
2337 | struct ffs_function *func = ffs_func_from_usb(f); | 2211 | struct ffs_function *func = ffs_func_from_usb(f); |
@@ -2349,6 +2223,8 @@ static void ffs_func_unbind(struct usb_configuration *c, | |||
2349 | ffs_func_free(func); | 2223 | ffs_func_free(func); |
2350 | } | 2224 | } |
2351 | 2225 | ||
2226 | #endif | ||
2227 | |||
2352 | static int ffs_func_set_alt(struct usb_function *f, | 2228 | static int ffs_func_set_alt(struct usb_function *f, |
2353 | unsigned interface, unsigned alt) | 2229 | unsigned interface, unsigned alt) |
2354 | { | 2230 | { |
@@ -2523,6 +2399,116 @@ static struct ffs_dev *ffs_find_dev(const char *name) | |||
2523 | return _ffs_find_dev(name); | 2399 | return _ffs_find_dev(name); |
2524 | } | 2400 | } |
2525 | 2401 | ||
2402 | /* Function registration interface ******************************************/ | ||
2403 | |||
2404 | #ifndef USB_FFS_INCLUDED | ||
2405 | |||
2406 | static void ffs_free_inst(struct usb_function_instance *f) | ||
2407 | { | ||
2408 | struct f_fs_opts *opts; | ||
2409 | |||
2410 | opts = to_f_fs_opts(f); | ||
2411 | ffs_dev_lock(); | ||
2412 | ffs_free_dev(opts->dev); | ||
2413 | ffs_dev_unlock(); | ||
2414 | kfree(opts); | ||
2415 | } | ||
2416 | |||
2417 | static struct usb_function_instance *ffs_alloc_inst(void) | ||
2418 | { | ||
2419 | struct f_fs_opts *opts; | ||
2420 | struct ffs_dev *dev; | ||
2421 | |||
2422 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | ||
2423 | if (!opts) | ||
2424 | return ERR_PTR(-ENOMEM); | ||
2425 | |||
2426 | opts->func_inst.free_func_inst = ffs_free_inst; | ||
2427 | ffs_dev_lock(); | ||
2428 | dev = ffs_alloc_dev(); | ||
2429 | ffs_dev_unlock(); | ||
2430 | if (IS_ERR(dev)) { | ||
2431 | kfree(opts); | ||
2432 | return ERR_CAST(dev); | ||
2433 | } | ||
2434 | opts->dev = dev; | ||
2435 | |||
2436 | return &opts->func_inst; | ||
2437 | } | ||
2438 | |||
2439 | static void ffs_free(struct usb_function *f) | ||
2440 | { | ||
2441 | kfree(ffs_func_from_usb(f)); | ||
2442 | } | ||
2443 | |||
2444 | static void ffs_func_unbind(struct usb_configuration *c, | ||
2445 | struct usb_function *f) | ||
2446 | { | ||
2447 | struct ffs_function *func = ffs_func_from_usb(f); | ||
2448 | struct ffs_data *ffs = func->ffs; | ||
2449 | struct f_fs_opts *opts = | ||
2450 | container_of(f->fi, struct f_fs_opts, func_inst); | ||
2451 | struct ffs_ep *ep = func->eps; | ||
2452 | unsigned count = ffs->eps_count; | ||
2453 | unsigned long flags; | ||
2454 | |||
2455 | ENTER(); | ||
2456 | if (ffs->func == func) { | ||
2457 | ffs_func_eps_disable(func); | ||
2458 | ffs->func = NULL; | ||
2459 | } | ||
2460 | |||
2461 | if (!--opts->refcnt) | ||
2462 | functionfs_unbind(ffs); | ||
2463 | |||
2464 | /* cleanup after autoconfig */ | ||
2465 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | ||
2466 | do { | ||
2467 | if (ep->ep && ep->req) | ||
2468 | usb_ep_free_request(ep->ep, ep->req); | ||
2469 | ep->req = NULL; | ||
2470 | ++ep; | ||
2471 | } while (--count); | ||
2472 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | ||
2473 | kfree(func->eps); | ||
2474 | func->eps = NULL; | ||
2475 | /* | ||
2476 | * eps, descriptors and interfaces_nums are allocated in the | ||
2477 | * same chunk so only one free is required. | ||
2478 | */ | ||
2479 | func->function.fs_descriptors = NULL; | ||
2480 | func->function.hs_descriptors = NULL; | ||
2481 | func->interfaces_nums = NULL; | ||
2482 | |||
2483 | ffs_event_add(ffs, FUNCTIONFS_UNBIND); | ||
2484 | } | ||
2485 | |||
2486 | static struct usb_function *ffs_alloc(struct usb_function_instance *fi) | ||
2487 | { | ||
2488 | struct ffs_function *func; | ||
2489 | |||
2490 | ENTER(); | ||
2491 | |||
2492 | func = kzalloc(sizeof(*func), GFP_KERNEL); | ||
2493 | if (unlikely(!func)) | ||
2494 | return ERR_PTR(-ENOMEM); | ||
2495 | |||
2496 | func->function.name = "Function FS Gadget"; | ||
2497 | |||
2498 | func->function.bind = ffs_func_bind; | ||
2499 | func->function.unbind = ffs_func_unbind; | ||
2500 | func->function.set_alt = ffs_func_set_alt; | ||
2501 | func->function.disable = ffs_func_disable; | ||
2502 | func->function.setup = ffs_func_setup; | ||
2503 | func->function.suspend = ffs_func_suspend; | ||
2504 | func->function.resume = ffs_func_resume; | ||
2505 | func->function.free_func = ffs_free; | ||
2506 | |||
2507 | return &func->function; | ||
2508 | } | ||
2509 | |||
2510 | #endif | ||
2511 | |||
2526 | /* | 2512 | /* |
2527 | * ffs_lock must be taken by the caller of this function | 2513 | * ffs_lock must be taken by the caller of this function |
2528 | */ | 2514 | */ |
@@ -2581,6 +2567,9 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name) | |||
2581 | 2567 | ||
2582 | return ret; | 2568 | return ret; |
2583 | } | 2569 | } |
2570 | #ifndef USB_FFS_INCLUDED | ||
2571 | EXPORT_SYMBOL(ffs_name_dev); | ||
2572 | #endif | ||
2584 | 2573 | ||
2585 | int ffs_single_dev(struct ffs_dev *dev) | 2574 | int ffs_single_dev(struct ffs_dev *dev) |
2586 | { | 2575 | { |
@@ -2597,6 +2586,9 @@ int ffs_single_dev(struct ffs_dev *dev) | |||
2597 | ffs_dev_unlock(); | 2586 | ffs_dev_unlock(); |
2598 | return ret; | 2587 | return ret; |
2599 | } | 2588 | } |
2589 | #ifndef USB_FFS_INCLUDED | ||
2590 | EXPORT_SYMBOL(ffs_single_dev); | ||
2591 | #endif | ||
2600 | 2592 | ||
2601 | /* | 2593 | /* |
2602 | * ffs_lock must be taken by the caller of this function | 2594 | * ffs_lock must be taken by the caller of this function |
@@ -2621,6 +2613,9 @@ static void *ffs_acquire_dev(const char *dev_name) | |||
2621 | ffs_dev = ERR_PTR(-ENODEV); | 2613 | ffs_dev = ERR_PTR(-ENODEV); |
2622 | else if (ffs_dev->mounted) | 2614 | else if (ffs_dev->mounted) |
2623 | ffs_dev = ERR_PTR(-EBUSY); | 2615 | ffs_dev = ERR_PTR(-EBUSY); |
2616 | else if (ffs_dev->ffs_acquire_dev_callback && | ||
2617 | ffs_dev->ffs_acquire_dev_callback(ffs_dev)) | ||
2618 | ffs_dev = ERR_PTR(-ENODEV); | ||
2624 | else | 2619 | else |
2625 | ffs_dev->mounted = true; | 2620 | ffs_dev->mounted = true; |
2626 | 2621 | ||
@@ -2638,6 +2633,9 @@ static void ffs_release_dev(struct ffs_data *ffs_data) | |||
2638 | ffs_dev = ffs_data->private_data; | 2633 | ffs_dev = ffs_data->private_data; |
2639 | if (ffs_dev) | 2634 | if (ffs_dev) |
2640 | ffs_dev->mounted = false; | 2635 | ffs_dev->mounted = false; |
2636 | |||
2637 | if (ffs_dev->ffs_release_dev_callback) | ||
2638 | ffs_dev->ffs_release_dev_callback(ffs_dev); | ||
2641 | 2639 | ||
2642 | ffs_dev_unlock(); | 2640 | ffs_dev_unlock(); |
2643 | } | 2641 | } |
@@ -2720,3 +2718,9 @@ static char *ffs_prepare_buffer(const char __user *buf, size_t len) | |||
2720 | 2718 | ||
2721 | return data; | 2719 | return data; |
2722 | } | 2720 | } |
2721 | |||
2722 | #ifndef USB_FFS_INCLUDED | ||
2723 | DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc); | ||
2724 | MODULE_LICENSE("GPL"); | ||
2725 | MODULE_AUTHOR("Michal Nazarewicz"); | ||
2726 | #endif | ||
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c index 074df0d56255..ffde36f2c672 100644 --- a/drivers/usb/gadget/g_ffs.c +++ b/drivers/usb/gadget/g_ffs.c | |||
@@ -54,6 +54,7 @@ static struct usb_function *f_rndis; | |||
54 | # endif | 54 | # endif |
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | #define USB_FFS_INCLUDED | ||
57 | #include "f_fs.c" | 58 | #include "f_fs.c" |
58 | 59 | ||
59 | #define DRIVER_NAME "g_ffs" | 60 | #define DRIVER_NAME "g_ffs" |
diff --git a/drivers/usb/gadget/u_fs.h b/drivers/usb/gadget/u_fs.h index 2d00f9d296a3..d60a9ddc0332 100644 --- a/drivers/usb/gadget/u_fs.h +++ b/drivers/usb/gadget/u_fs.h | |||
@@ -20,6 +20,22 @@ | |||
20 | #include <linux/list.h> | 20 | #include <linux/list.h> |
21 | #include <linux/mutex.h> | 21 | #include <linux/mutex.h> |
22 | 22 | ||
23 | #ifdef VERBOSE_DEBUG | ||
24 | #ifndef pr_vdebug | ||
25 | # define pr_vdebug pr_debug | ||
26 | #endif /* pr_vdebug */ | ||
27 | # define ffs_dump_mem(prefix, ptr, len) \ | ||
28 | print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len) | ||
29 | #else | ||
30 | #ifndef pr_vdebug | ||
31 | # define pr_vdebug(...) do { } while (0) | ||
32 | #endif /* pr_vdebug */ | ||
33 | # define ffs_dump_mem(prefix, ptr, len) do { } while (0) | ||
34 | #endif /* VERBOSE_DEBUG */ | ||
35 | |||
36 | #define ENTER() pr_vdebug("%s()\n", __func__) | ||
37 | |||
38 | |||
23 | struct ffs_dev { | 39 | struct ffs_dev { |
24 | const char *name; | 40 | const char *name; |
25 | bool mounted; | 41 | bool mounted; |
@@ -30,6 +46,8 @@ struct ffs_dev { | |||
30 | 46 | ||
31 | int (*ffs_ready_callback)(struct ffs_data *ffs); | 47 | int (*ffs_ready_callback)(struct ffs_data *ffs); |
32 | void (*ffs_closed_callback)(struct ffs_data *ffs); | 48 | void (*ffs_closed_callback)(struct ffs_data *ffs); |
49 | void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev); | ||
50 | void (*ffs_release_dev_callback)(struct ffs_dev *dev); | ||
33 | }; | 51 | }; |
34 | 52 | ||
35 | extern struct mutex ffs_lock; | 53 | extern struct mutex ffs_lock; |
@@ -49,4 +67,200 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name); | |||
49 | int ffs_single_dev(struct ffs_dev *dev); | 67 | int ffs_single_dev(struct ffs_dev *dev); |
50 | void ffs_free_dev(struct ffs_dev *dev); | 68 | void ffs_free_dev(struct ffs_dev *dev); |
51 | 69 | ||
70 | struct ffs_epfile; | ||
71 | struct ffs_function; | ||
72 | |||
73 | enum ffs_state { | ||
74 | /* | ||
75 | * Waiting for descriptors and strings. | ||
76 | * | ||
77 | * In this state no open(2), read(2) or write(2) on epfiles | ||
78 | * may succeed (which should not be the problem as there | ||
79 | * should be no such files opened in the first place). | ||
80 | */ | ||
81 | FFS_READ_DESCRIPTORS, | ||
82 | FFS_READ_STRINGS, | ||
83 | |||
84 | /* | ||
85 | * We've got descriptors and strings. We are or have called | ||
86 | * functionfs_ready_callback(). functionfs_bind() may have | ||
87 | * been called but we don't know. | ||
88 | * | ||
89 | * This is the only state in which operations on epfiles may | ||
90 | * succeed. | ||
91 | */ | ||
92 | FFS_ACTIVE, | ||
93 | |||
94 | /* | ||
95 | * All endpoints have been closed. This state is also set if | ||
96 | * we encounter an unrecoverable error. The only | ||
97 | * unrecoverable error is situation when after reading strings | ||
98 | * from user space we fail to initialise epfiles or | ||
99 | * functionfs_ready_callback() returns with error (<0). | ||
100 | * | ||
101 | * In this state no open(2), read(2) or write(2) (both on ep0 | ||
102 | * as well as epfile) may succeed (at this point epfiles are | ||
103 | * unlinked and all closed so this is not a problem; ep0 is | ||
104 | * also closed but ep0 file exists and so open(2) on ep0 must | ||
105 | * fail). | ||
106 | */ | ||
107 | FFS_CLOSING | ||
108 | }; | ||
109 | |||
110 | enum ffs_setup_state { | ||
111 | /* There is no setup request pending. */ | ||
112 | FFS_NO_SETUP, | ||
113 | /* | ||
114 | * User has read events and there was a setup request event | ||
115 | * there. The next read/write on ep0 will handle the | ||
116 | * request. | ||
117 | */ | ||
118 | FFS_SETUP_PENDING, | ||
119 | /* | ||
120 | * There was event pending but before user space handled it | ||
121 | * some other event was introduced which canceled existing | ||
122 | * setup. If this state is set read/write on ep0 return | ||
123 | * -EIDRM. This state is only set when adding event. | ||
124 | */ | ||
125 | FFS_SETUP_CANCELED | ||
126 | }; | ||
127 | |||
128 | struct ffs_data { | ||
129 | struct usb_gadget *gadget; | ||
130 | |||
131 | /* | ||
132 | * Protect access read/write operations, only one read/write | ||
133 | * at a time. As a consequence protects ep0req and company. | ||
134 | * While setup request is being processed (queued) this is | ||
135 | * held. | ||
136 | */ | ||
137 | struct mutex mutex; | ||
138 | |||
139 | /* | ||
140 | * Protect access to endpoint related structures (basically | ||
141 | * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for | ||
142 | * endpoint zero. | ||
143 | */ | ||
144 | spinlock_t eps_lock; | ||
145 | |||
146 | /* | ||
147 | * XXX REVISIT do we need our own request? Since we are not | ||
148 | * handling setup requests immediately user space may be so | ||
149 | * slow that another setup will be sent to the gadget but this | ||
150 | * time not to us but another function and then there could be | ||
151 | * a race. Is that the case? Or maybe we can use cdev->req | ||
152 | * after all, maybe we just need some spinlock for that? | ||
153 | */ | ||
154 | struct usb_request *ep0req; /* P: mutex */ | ||
155 | struct completion ep0req_completion; /* P: mutex */ | ||
156 | int ep0req_status; /* P: mutex */ | ||
157 | |||
158 | /* reference counter */ | ||
159 | atomic_t ref; | ||
160 | /* how many files are opened (EP0 and others) */ | ||
161 | atomic_t opened; | ||
162 | |||
163 | /* EP0 state */ | ||
164 | enum ffs_state state; | ||
165 | |||
166 | /* | ||
167 | * Possible transitions: | ||
168 | * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock | ||
169 | * happens only in ep0 read which is P: mutex | ||
170 | * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock | ||
171 | * happens only in ep0 i/o which is P: mutex | ||
172 | * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock | ||
173 | * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg | ||
174 | */ | ||
175 | enum ffs_setup_state setup_state; | ||
176 | |||
177 | #define FFS_SETUP_STATE(ffs) \ | ||
178 | ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \ | ||
179 | FFS_SETUP_CANCELED, FFS_NO_SETUP)) | ||
180 | |||
181 | /* Events & such. */ | ||
182 | struct { | ||
183 | u8 types[4]; | ||
184 | unsigned short count; | ||
185 | /* XXX REVISIT need to update it in some places, or do we? */ | ||
186 | unsigned short can_stall; | ||
187 | struct usb_ctrlrequest setup; | ||
188 | |||
189 | wait_queue_head_t waitq; | ||
190 | } ev; /* the whole structure, P: ev.waitq.lock */ | ||
191 | |||
192 | /* Flags */ | ||
193 | unsigned long flags; | ||
194 | #define FFS_FL_CALL_CLOSED_CALLBACK 0 | ||
195 | #define FFS_FL_BOUND 1 | ||
196 | |||
197 | /* Active function */ | ||
198 | struct ffs_function *func; | ||
199 | |||
200 | /* | ||
201 | * Device name, write once when file system is mounted. | ||
202 | * Intended for user to read if she wants. | ||
203 | */ | ||
204 | const char *dev_name; | ||
205 | /* Private data for our user (ie. gadget). Managed by user. */ | ||
206 | void *private_data; | ||
207 | |||
208 | /* filled by __ffs_data_got_descs() */ | ||
209 | /* | ||
210 | * Real descriptors are 16 bytes after raw_descs (so you need | ||
211 | * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the | ||
212 | * first full speed descriptor). raw_descs_length and | ||
213 | * raw_fs_descs_length do not have those 16 bytes added. | ||
214 | */ | ||
215 | const void *raw_descs; | ||
216 | unsigned raw_descs_length; | ||
217 | unsigned raw_fs_descs_length; | ||
218 | unsigned fs_descs_count; | ||
219 | unsigned hs_descs_count; | ||
220 | |||
221 | unsigned short strings_count; | ||
222 | unsigned short interfaces_count; | ||
223 | unsigned short eps_count; | ||
224 | unsigned short _pad1; | ||
225 | |||
226 | /* filled by __ffs_data_got_strings() */ | ||
227 | /* ids in stringtabs are set in functionfs_bind() */ | ||
228 | const void *raw_strings; | ||
229 | struct usb_gadget_strings **stringtabs; | ||
230 | |||
231 | /* | ||
232 | * File system's super block, write once when file system is | ||
233 | * mounted. | ||
234 | */ | ||
235 | struct super_block *sb; | ||
236 | |||
237 | /* File permissions, written once when fs is mounted */ | ||
238 | struct ffs_file_perms { | ||
239 | umode_t mode; | ||
240 | kuid_t uid; | ||
241 | kgid_t gid; | ||
242 | } file_perms; | ||
243 | |||
244 | /* | ||
245 | * The endpoint files, filled by ffs_epfiles_create(), | ||
246 | * destroyed by ffs_epfiles_destroy(). | ||
247 | */ | ||
248 | struct ffs_epfile *epfiles; | ||
249 | }; | ||
250 | |||
251 | |||
252 | #ifndef USB_FFS_INCLUDED | ||
253 | struct f_fs_opts { | ||
254 | struct usb_function_instance func_inst; | ||
255 | struct ffs_dev *dev; | ||
256 | unsigned refcnt; | ||
257 | bool no_configfs; | ||
258 | }; | ||
259 | |||
260 | static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi) | ||
261 | { | ||
262 | return container_of(fi, struct f_fs_opts, func_inst); | ||
263 | } | ||
264 | #endif | ||
265 | |||
52 | #endif /* U_FFS_H */ | 266 | #endif /* U_FFS_H */ |
diff --git a/include/linux/usb/functionfs.h b/include/linux/usb/functionfs.h index 9c1e92620dfb..3448efbe56aa 100644 --- a/include/linux/usb/functionfs.h +++ b/include/linux/usb/functionfs.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <uapi/linux/usb/functionfs.h> | 4 | #include <uapi/linux/usb/functionfs.h> |
5 | 5 | ||
6 | #ifdef USB_FFS_INCLUDED | ||
6 | 7 | ||
7 | struct ffs_data; | 8 | struct ffs_data; |
8 | struct usb_composite_dev; | 9 | struct usb_composite_dev; |
@@ -20,3 +21,4 @@ static int functionfs_bind_config(struct usb_composite_dev *cdev, | |||
20 | 21 | ||
21 | 22 | ||
22 | #endif | 23 | #endif |
24 | #endif | ||