]>
Commit | Line | Data |
---|---|---|
ddf8abd2 | 1 | /* |
5ab54cf7 | 2 | * f_fs.c -- user mode file system API for USB composite function controllers |
ddf8abd2 MN |
3 | * |
4 | * Copyright (C) 2010 Samsung Electronics | |
5 | * Author: Michal Nazarewicz <m.nazarewicz@samsung.com> | |
6 | * | |
5ab54cf7 | 7 | * Based on inode.c (GadgetFS) which was: |
ddf8abd2 MN |
8 | * Copyright (C) 2003-2004 David Brownell |
9 | * Copyright (C) 2003 Agilent Technologies | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | */ | |
25 | ||
26 | ||
27 | /* #define DEBUG */ | |
28 | /* #define VERBOSE_DEBUG */ | |
29 | ||
d8df0b61 MN |
30 | #define pr_fmt(fmt) "f_fs: " fmt "\n" |
31 | ||
ddf8abd2 | 32 | #include <linux/blkdev.h> |
b0608690 | 33 | #include <linux/pagemap.h> |
ddf8abd2 MN |
34 | #include <asm/unaligned.h> |
35 | #include <linux/smp_lock.h> | |
36 | ||
37 | #include <linux/usb/composite.h> | |
38 | #include <linux/usb/functionfs.h> | |
39 | ||
40 | ||
41 | #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */ | |
42 | ||
43 | ||
5ab54cf7 | 44 | /* Debugging ****************************************************************/ |
ddf8abd2 | 45 | |
ddf8abd2 | 46 | #ifdef VERBOSE_DEBUG |
d8df0b61 | 47 | # define pr_vdebug pr_debug |
ddf8abd2 MN |
48 | # define ffs_dump_mem(prefix, ptr, len) \ |
49 | print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len) | |
50 | #else | |
d8df0b61 | 51 | # define pr_vdebug(...) do { } while (0) |
ddf8abd2 | 52 | # define ffs_dump_mem(prefix, ptr, len) do { } while (0) |
d8df0b61 MN |
53 | #endif /* VERBOSE_DEBUG */ |
54 | ||
55 | #define ENTER() pr_vdebug("%s()", __func__) | |
ddf8abd2 MN |
56 | |
57 | ||
58 | /* The data structure and setup file ****************************************/ | |
59 | ||
60 | enum ffs_state { | |
5ab54cf7 MN |
61 | /* |
62 | * Waiting for descriptors and strings. | |
63 | * | |
64 | * In this state no open(2), read(2) or write(2) on epfiles | |
ddf8abd2 | 65 | * may succeed (which should not be the problem as there |
5ab54cf7 MN |
66 | * should be no such files opened in the first place). |
67 | */ | |
ddf8abd2 MN |
68 | FFS_READ_DESCRIPTORS, |
69 | FFS_READ_STRINGS, | |
70 | ||
5ab54cf7 MN |
71 | /* |
72 | * We've got descriptors and strings. We are or have called | |
ddf8abd2 | 73 | * functionfs_ready_callback(). functionfs_bind() may have |
5ab54cf7 MN |
74 | * been called but we don't know. |
75 | * | |
76 | * This is the only state in which operations on epfiles may | |
77 | * succeed. | |
78 | */ | |
ddf8abd2 MN |
79 | FFS_ACTIVE, |
80 | ||
5ab54cf7 MN |
81 | /* |
82 | * All endpoints have been closed. This state is also set if | |
ddf8abd2 MN |
83 | * we encounter an unrecoverable error. The only |
84 | * unrecoverable error is situation when after reading strings | |
5ab54cf7 MN |
85 | * from user space we fail to initialise epfiles or |
86 | * functionfs_ready_callback() returns with error (<0). | |
87 | * | |
88 | * In this state no open(2), read(2) or write(2) (both on ep0 | |
ddf8abd2 MN |
89 | * as well as epfile) may succeed (at this point epfiles are |
90 | * unlinked and all closed so this is not a problem; ep0 is | |
91 | * also closed but ep0 file exists and so open(2) on ep0 must | |
5ab54cf7 MN |
92 | * fail). |
93 | */ | |
ddf8abd2 MN |
94 | FFS_CLOSING |
95 | }; | |
96 | ||
97 | ||
98 | enum ffs_setup_state { | |
99 | /* There is no setup request pending. */ | |
100 | FFS_NO_SETUP, | |
5ab54cf7 MN |
101 | /* |
102 | * User has read events and there was a setup request event | |
ddf8abd2 | 103 | * there. The next read/write on ep0 will handle the |
5ab54cf7 MN |
104 | * request. |
105 | */ | |
ddf8abd2 | 106 | FFS_SETUP_PENDING, |
5ab54cf7 MN |
107 | /* |
108 | * There was event pending but before user space handled it | |
ddf8abd2 MN |
109 | * some other event was introduced which canceled existing |
110 | * setup. If this state is set read/write on ep0 return | |
5ab54cf7 MN |
111 | * -EIDRM. This state is only set when adding event. |
112 | */ | |
ddf8abd2 MN |
113 | FFS_SETUP_CANCELED |
114 | }; | |
115 | ||
116 | ||
117 | ||
118 | struct ffs_epfile; | |
119 | struct ffs_function; | |
120 | ||
121 | struct ffs_data { | |
122 | struct usb_gadget *gadget; | |
123 | ||
5ab54cf7 MN |
124 | /* |
125 | * Protect access read/write operations, only one read/write | |
ddf8abd2 MN |
126 | * at a time. As a consequence protects ep0req and company. |
127 | * While setup request is being processed (queued) this is | |
5ab54cf7 MN |
128 | * held. |
129 | */ | |
ddf8abd2 MN |
130 | struct mutex mutex; |
131 | ||
5ab54cf7 MN |
132 | /* |
133 | * Protect access to endpoint related structures (basically | |
ddf8abd2 | 134 | * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for |
5ab54cf7 MN |
135 | * endpoint zero. |
136 | */ | |
ddf8abd2 MN |
137 | spinlock_t eps_lock; |
138 | ||
5ab54cf7 MN |
139 | /* |
140 | * XXX REVISIT do we need our own request? Since we are not | |
141 | * handling setup requests immediately user space may be so | |
ddf8abd2 MN |
142 | * slow that another setup will be sent to the gadget but this |
143 | * time not to us but another function and then there could be | |
a4ce96ac | 144 | * a race. Is that the case? Or maybe we can use cdev->req |
5ab54cf7 MN |
145 | * after all, maybe we just need some spinlock for that? |
146 | */ | |
ddf8abd2 MN |
147 | struct usb_request *ep0req; /* P: mutex */ |
148 | struct completion ep0req_completion; /* P: mutex */ | |
149 | int ep0req_status; /* P: mutex */ | |
150 | ||
151 | /* reference counter */ | |
152 | atomic_t ref; | |
153 | /* how many files are opened (EP0 and others) */ | |
154 | atomic_t opened; | |
155 | ||
156 | /* EP0 state */ | |
157 | enum ffs_state state; | |
158 | ||
159 | /* | |
5ab54cf7 | 160 | * Possible transitions: |
ddf8abd2 MN |
161 | * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock |
162 | * happens only in ep0 read which is P: mutex | |
163 | * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock | |
164 | * happens only in ep0 i/o which is P: mutex | |
165 | * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock | |
166 | * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg | |
167 | */ | |
168 | enum ffs_setup_state setup_state; | |
169 | ||
170 | #define FFS_SETUP_STATE(ffs) \ | |
171 | ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \ | |
172 | FFS_SETUP_CANCELED, FFS_NO_SETUP)) | |
173 | ||
174 | /* Events & such. */ | |
175 | struct { | |
176 | u8 types[4]; | |
177 | unsigned short count; | |
178 | /* XXX REVISIT need to update it in some places, or do we? */ | |
179 | unsigned short can_stall; | |
180 | struct usb_ctrlrequest setup; | |
181 | ||
182 | wait_queue_head_t waitq; | |
183 | } ev; /* the whole structure, P: ev.waitq.lock */ | |
184 | ||
185 | /* Flags */ | |
186 | unsigned long flags; | |
187 | #define FFS_FL_CALL_CLOSED_CALLBACK 0 | |
188 | #define FFS_FL_BOUND 1 | |
189 | ||
190 | /* Active function */ | |
191 | struct ffs_function *func; | |
192 | ||
5ab54cf7 MN |
193 | /* |
194 | * Device name, write once when file system is mounted. | |
195 | * Intended for user to read if she wants. | |
196 | */ | |
ddf8abd2 | 197 | const char *dev_name; |
5ab54cf7 | 198 | /* Private data for our user (ie. gadget). Managed by user. */ |
ddf8abd2 MN |
199 | void *private_data; |
200 | ||
201 | /* filled by __ffs_data_got_descs() */ | |
5ab54cf7 MN |
202 | /* |
203 | * Real descriptors are 16 bytes after raw_descs (so you need | |
ddf8abd2 MN |
204 | * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the |
205 | * first full speed descriptor). raw_descs_length and | |
5ab54cf7 MN |
206 | * raw_fs_descs_length do not have those 16 bytes added. |
207 | */ | |
ddf8abd2 MN |
208 | const void *raw_descs; |
209 | unsigned raw_descs_length; | |
210 | unsigned raw_fs_descs_length; | |
211 | unsigned fs_descs_count; | |
212 | unsigned hs_descs_count; | |
213 | ||
214 | unsigned short strings_count; | |
215 | unsigned short interfaces_count; | |
216 | unsigned short eps_count; | |
217 | unsigned short _pad1; | |
218 | ||
219 | /* filled by __ffs_data_got_strings() */ | |
220 | /* ids in stringtabs are set in functionfs_bind() */ | |
221 | const void *raw_strings; | |
222 | struct usb_gadget_strings **stringtabs; | |
223 | ||
5ab54cf7 MN |
224 | /* |
225 | * File system's super block, write once when file system is | |
226 | * mounted. | |
227 | */ | |
ddf8abd2 MN |
228 | struct super_block *sb; |
229 | ||
5ab54cf7 | 230 | /* File permissions, written once when fs is mounted */ |
ddf8abd2 MN |
231 | struct ffs_file_perms { |
232 | umode_t mode; | |
233 | uid_t uid; | |
234 | gid_t gid; | |
235 | } file_perms; | |
236 | ||
5ab54cf7 MN |
237 | /* |
238 | * The endpoint files, filled by ffs_epfiles_create(), | |
239 | * destroyed by ffs_epfiles_destroy(). | |
240 | */ | |
ddf8abd2 MN |
241 | struct ffs_epfile *epfiles; |
242 | }; | |
243 | ||
244 | /* Reference counter handling */ | |
245 | static void ffs_data_get(struct ffs_data *ffs); | |
246 | static void ffs_data_put(struct ffs_data *ffs); | |
247 | /* Creates new ffs_data object. */ | |
248 | static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc)); | |
249 | ||
250 | /* Opened counter handling. */ | |
251 | static void ffs_data_opened(struct ffs_data *ffs); | |
252 | static void ffs_data_closed(struct ffs_data *ffs); | |
253 | ||
5ab54cf7 | 254 | /* Called with ffs->mutex held; take over ownership of data. */ |
ddf8abd2 MN |
255 | static int __must_check |
256 | __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len); | |
257 | static int __must_check | |
258 | __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len); | |
259 | ||
260 | ||
261 | /* The function structure ***************************************************/ | |
262 | ||
263 | struct ffs_ep; | |
264 | ||
265 | struct ffs_function { | |
266 | struct usb_configuration *conf; | |
267 | struct usb_gadget *gadget; | |
268 | struct ffs_data *ffs; | |
269 | ||
270 | struct ffs_ep *eps; | |
271 | u8 eps_revmap[16]; | |
272 | short *interfaces_nums; | |
273 | ||
274 | struct usb_function function; | |
275 | }; | |
276 | ||
277 | ||
278 | static struct ffs_function *ffs_func_from_usb(struct usb_function *f) | |
279 | { | |
280 | return container_of(f, struct ffs_function, function); | |
281 | } | |
282 | ||
283 | static void ffs_func_free(struct ffs_function *func); | |
284 | ||
ddf8abd2 MN |
285 | static void ffs_func_eps_disable(struct ffs_function *func); |
286 | static int __must_check ffs_func_eps_enable(struct ffs_function *func); | |
287 | ||
ddf8abd2 MN |
288 | static int ffs_func_bind(struct usb_configuration *, |
289 | struct usb_function *); | |
290 | static void ffs_func_unbind(struct usb_configuration *, | |
291 | struct usb_function *); | |
292 | static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); | |
293 | static void ffs_func_disable(struct usb_function *); | |
294 | static int ffs_func_setup(struct usb_function *, | |
295 | const struct usb_ctrlrequest *); | |
296 | static void ffs_func_suspend(struct usb_function *); | |
297 | static void ffs_func_resume(struct usb_function *); | |
298 | ||
299 | ||
300 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num); | |
301 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf); | |
302 | ||
303 | ||
ddf8abd2 MN |
304 | /* The endpoints structures *************************************************/ |
305 | ||
306 | struct ffs_ep { | |
307 | struct usb_ep *ep; /* P: ffs->eps_lock */ | |
308 | struct usb_request *req; /* P: epfile->mutex */ | |
309 | ||
310 | /* [0]: full speed, [1]: high speed */ | |
311 | struct usb_endpoint_descriptor *descs[2]; | |
312 | ||
313 | u8 num; | |
314 | ||
315 | int status; /* P: epfile->mutex */ | |
316 | }; | |
317 | ||
318 | struct ffs_epfile { | |
319 | /* Protects ep->ep and ep->req. */ | |
320 | struct mutex mutex; | |
321 | wait_queue_head_t wait; | |
322 | ||
323 | struct ffs_data *ffs; | |
324 | struct ffs_ep *ep; /* P: ffs->eps_lock */ | |
325 | ||
326 | struct dentry *dentry; | |
327 | ||
328 | char name[5]; | |
329 | ||
330 | unsigned char in; /* P: ffs->eps_lock */ | |
331 | unsigned char isoc; /* P: ffs->eps_lock */ | |
332 | ||
333 | unsigned char _pad; | |
334 | }; | |
335 | ||
ddf8abd2 MN |
336 | static int __must_check ffs_epfiles_create(struct ffs_data *ffs); |
337 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); | |
338 | ||
339 | static struct inode *__must_check | |
340 | ffs_sb_create_file(struct super_block *sb, const char *name, void *data, | |
341 | const struct file_operations *fops, | |
342 | struct dentry **dentry_p); | |
343 | ||
344 | ||
345 | /* Misc helper functions ****************************************************/ | |
346 | ||
347 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) | |
348 | __attribute__((warn_unused_result, nonnull)); | |
349 | static char *ffs_prepare_buffer(const char * __user buf, size_t len) | |
350 | __attribute__((warn_unused_result, nonnull)); | |
351 | ||
352 | ||
353 | /* Control file aka ep0 *****************************************************/ | |
354 | ||
355 | static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req) | |
356 | { | |
357 | struct ffs_data *ffs = req->context; | |
358 | ||
359 | complete_all(&ffs->ep0req_completion); | |
360 | } | |
361 | ||
ddf8abd2 MN |
362 | static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len) |
363 | { | |
364 | struct usb_request *req = ffs->ep0req; | |
365 | int ret; | |
366 | ||
367 | req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); | |
368 | ||
369 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
370 | ||
371 | req->buf = data; | |
372 | req->length = len; | |
373 | ||
374 | INIT_COMPLETION(ffs->ep0req_completion); | |
375 | ||
376 | ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); | |
377 | if (unlikely(ret < 0)) | |
378 | return ret; | |
379 | ||
380 | ret = wait_for_completion_interruptible(&ffs->ep0req_completion); | |
381 | if (unlikely(ret)) { | |
382 | usb_ep_dequeue(ffs->gadget->ep0, req); | |
383 | return -EINTR; | |
384 | } | |
385 | ||
386 | ffs->setup_state = FFS_NO_SETUP; | |
387 | return ffs->ep0req_status; | |
388 | } | |
389 | ||
390 | static int __ffs_ep0_stall(struct ffs_data *ffs) | |
391 | { | |
392 | if (ffs->ev.can_stall) { | |
d8df0b61 | 393 | pr_vdebug("ep0 stall"); |
ddf8abd2 MN |
394 | usb_ep_set_halt(ffs->gadget->ep0); |
395 | ffs->setup_state = FFS_NO_SETUP; | |
396 | return -EL2HLT; | |
397 | } else { | |
d8df0b61 | 398 | pr_debug("bogus ep0 stall!"); |
ddf8abd2 MN |
399 | return -ESRCH; |
400 | } | |
401 | } | |
402 | ||
ddf8abd2 MN |
403 | static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, |
404 | size_t len, loff_t *ptr) | |
405 | { | |
406 | struct ffs_data *ffs = file->private_data; | |
407 | ssize_t ret; | |
408 | char *data; | |
409 | ||
410 | ENTER(); | |
411 | ||
412 | /* Fast check if setup was canceled */ | |
413 | if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) | |
414 | return -EIDRM; | |
415 | ||
416 | /* Acquire mutex */ | |
417 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); | |
418 | if (unlikely(ret < 0)) | |
419 | return ret; | |
420 | ||
ddf8abd2 MN |
421 | /* Check state */ |
422 | switch (ffs->state) { | |
423 | case FFS_READ_DESCRIPTORS: | |
424 | case FFS_READ_STRINGS: | |
425 | /* Copy data */ | |
426 | if (unlikely(len < 16)) { | |
427 | ret = -EINVAL; | |
428 | break; | |
429 | } | |
430 | ||
431 | data = ffs_prepare_buffer(buf, len); | |
432 | if (unlikely(IS_ERR(data))) { | |
433 | ret = PTR_ERR(data); | |
434 | break; | |
435 | } | |
436 | ||
437 | /* Handle data */ | |
438 | if (ffs->state == FFS_READ_DESCRIPTORS) { | |
d8df0b61 | 439 | pr_info("read descriptors"); |
ddf8abd2 MN |
440 | ret = __ffs_data_got_descs(ffs, data, len); |
441 | if (unlikely(ret < 0)) | |
442 | break; | |
443 | ||
444 | ffs->state = FFS_READ_STRINGS; | |
445 | ret = len; | |
446 | } else { | |
d8df0b61 | 447 | pr_info("read strings"); |
ddf8abd2 MN |
448 | ret = __ffs_data_got_strings(ffs, data, len); |
449 | if (unlikely(ret < 0)) | |
450 | break; | |
451 | ||
452 | ret = ffs_epfiles_create(ffs); | |
453 | if (unlikely(ret)) { | |
454 | ffs->state = FFS_CLOSING; | |
455 | break; | |
456 | } | |
457 | ||
458 | ffs->state = FFS_ACTIVE; | |
459 | mutex_unlock(&ffs->mutex); | |
460 | ||
461 | ret = functionfs_ready_callback(ffs); | |
462 | if (unlikely(ret < 0)) { | |
463 | ffs->state = FFS_CLOSING; | |
464 | return ret; | |
465 | } | |
466 | ||
467 | set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); | |
468 | return len; | |
469 | } | |
470 | break; | |
471 | ||
ddf8abd2 MN |
472 | case FFS_ACTIVE: |
473 | data = NULL; | |
5ab54cf7 MN |
474 | /* |
475 | * We're called from user space, we can use _irq | |
476 | * rather then _irqsave | |
477 | */ | |
ddf8abd2 MN |
478 | spin_lock_irq(&ffs->ev.waitq.lock); |
479 | switch (FFS_SETUP_STATE(ffs)) { | |
480 | case FFS_SETUP_CANCELED: | |
481 | ret = -EIDRM; | |
482 | goto done_spin; | |
483 | ||
484 | case FFS_NO_SETUP: | |
485 | ret = -ESRCH; | |
486 | goto done_spin; | |
487 | ||
488 | case FFS_SETUP_PENDING: | |
489 | break; | |
490 | } | |
491 | ||
492 | /* FFS_SETUP_PENDING */ | |
493 | if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { | |
494 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
495 | ret = __ffs_ep0_stall(ffs); | |
496 | break; | |
497 | } | |
498 | ||
499 | /* FFS_SETUP_PENDING and not stall */ | |
500 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); | |
501 | ||
502 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
503 | ||
504 | data = ffs_prepare_buffer(buf, len); | |
505 | if (unlikely(IS_ERR(data))) { | |
506 | ret = PTR_ERR(data); | |
507 | break; | |
508 | } | |
509 | ||
510 | spin_lock_irq(&ffs->ev.waitq.lock); | |
511 | ||
5ab54cf7 MN |
512 | /* |
513 | * We are guaranteed to be still in FFS_ACTIVE state | |
ddf8abd2 MN |
514 | * but the state of setup could have changed from |
515 | * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need | |
516 | * to check for that. If that happened we copied data | |
5ab54cf7 MN |
517 | * from user space in vain but it's unlikely. |
518 | * | |
519 | * For sure we are not in FFS_NO_SETUP since this is | |
ddf8abd2 MN |
520 | * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP |
521 | * transition can be performed and it's protected by | |
5ab54cf7 MN |
522 | * mutex. |
523 | */ | |
ddf8abd2 MN |
524 | if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { |
525 | ret = -EIDRM; | |
526 | done_spin: | |
527 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
528 | } else { | |
529 | /* unlocks spinlock */ | |
530 | ret = __ffs_ep0_queue_wait(ffs, data, len); | |
531 | } | |
532 | kfree(data); | |
533 | break; | |
534 | ||
ddf8abd2 MN |
535 | default: |
536 | ret = -EBADFD; | |
537 | break; | |
538 | } | |
539 | ||
ddf8abd2 MN |
540 | mutex_unlock(&ffs->mutex); |
541 | return ret; | |
542 | } | |
543 | ||
ddf8abd2 MN |
544 | static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, |
545 | size_t n) | |
546 | { | |
5ab54cf7 MN |
547 | /* |
548 | * We are holding ffs->ev.waitq.lock and ffs->mutex and we need | |
549 | * to release them. | |
550 | */ | |
ddf8abd2 MN |
551 | struct usb_functionfs_event events[n]; |
552 | unsigned i = 0; | |
553 | ||
554 | memset(events, 0, sizeof events); | |
555 | ||
556 | do { | |
557 | events[i].type = ffs->ev.types[i]; | |
558 | if (events[i].type == FUNCTIONFS_SETUP) { | |
559 | events[i].u.setup = ffs->ev.setup; | |
560 | ffs->setup_state = FFS_SETUP_PENDING; | |
561 | } | |
562 | } while (++i < n); | |
563 | ||
564 | if (n < ffs->ev.count) { | |
565 | ffs->ev.count -= n; | |
566 | memmove(ffs->ev.types, ffs->ev.types + n, | |
567 | ffs->ev.count * sizeof *ffs->ev.types); | |
568 | } else { | |
569 | ffs->ev.count = 0; | |
570 | } | |
571 | ||
572 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
573 | mutex_unlock(&ffs->mutex); | |
574 | ||
575 | return unlikely(__copy_to_user(buf, events, sizeof events)) | |
576 | ? -EFAULT : sizeof events; | |
577 | } | |
578 | ||
ddf8abd2 MN |
579 | static ssize_t ffs_ep0_read(struct file *file, char __user *buf, |
580 | size_t len, loff_t *ptr) | |
581 | { | |
582 | struct ffs_data *ffs = file->private_data; | |
583 | char *data = NULL; | |
584 | size_t n; | |
585 | int ret; | |
586 | ||
587 | ENTER(); | |
588 | ||
589 | /* Fast check if setup was canceled */ | |
590 | if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) | |
591 | return -EIDRM; | |
592 | ||
593 | /* Acquire mutex */ | |
594 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); | |
595 | if (unlikely(ret < 0)) | |
596 | return ret; | |
597 | ||
ddf8abd2 MN |
598 | /* Check state */ |
599 | if (ffs->state != FFS_ACTIVE) { | |
600 | ret = -EBADFD; | |
601 | goto done_mutex; | |
602 | } | |
603 | ||
5ab54cf7 MN |
604 | /* |
605 | * We're called from user space, we can use _irq rather then | |
606 | * _irqsave | |
607 | */ | |
ddf8abd2 MN |
608 | spin_lock_irq(&ffs->ev.waitq.lock); |
609 | ||
610 | switch (FFS_SETUP_STATE(ffs)) { | |
611 | case FFS_SETUP_CANCELED: | |
612 | ret = -EIDRM; | |
613 | break; | |
614 | ||
615 | case FFS_NO_SETUP: | |
616 | n = len / sizeof(struct usb_functionfs_event); | |
617 | if (unlikely(!n)) { | |
618 | ret = -EINVAL; | |
619 | break; | |
620 | } | |
621 | ||
622 | if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { | |
623 | ret = -EAGAIN; | |
624 | break; | |
625 | } | |
626 | ||
5ab54cf7 MN |
627 | if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, |
628 | ffs->ev.count)) { | |
ddf8abd2 MN |
629 | ret = -EINTR; |
630 | break; | |
631 | } | |
632 | ||
633 | return __ffs_ep0_read_events(ffs, buf, | |
634 | min(n, (size_t)ffs->ev.count)); | |
635 | ||
ddf8abd2 MN |
636 | case FFS_SETUP_PENDING: |
637 | if (ffs->ev.setup.bRequestType & USB_DIR_IN) { | |
638 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
639 | ret = __ffs_ep0_stall(ffs); | |
640 | goto done_mutex; | |
641 | } | |
642 | ||
643 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); | |
644 | ||
645 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
646 | ||
647 | if (likely(len)) { | |
648 | data = kmalloc(len, GFP_KERNEL); | |
649 | if (unlikely(!data)) { | |
650 | ret = -ENOMEM; | |
651 | goto done_mutex; | |
652 | } | |
653 | } | |
654 | ||
655 | spin_lock_irq(&ffs->ev.waitq.lock); | |
656 | ||
657 | /* See ffs_ep0_write() */ | |
658 | if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) { | |
659 | ret = -EIDRM; | |
660 | break; | |
661 | } | |
662 | ||
663 | /* unlocks spinlock */ | |
664 | ret = __ffs_ep0_queue_wait(ffs, data, len); | |
665 | if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len))) | |
666 | ret = -EFAULT; | |
667 | goto done_mutex; | |
668 | ||
669 | default: | |
670 | ret = -EBADFD; | |
671 | break; | |
672 | } | |
673 | ||
674 | spin_unlock_irq(&ffs->ev.waitq.lock); | |
675 | done_mutex: | |
676 | mutex_unlock(&ffs->mutex); | |
677 | kfree(data); | |
678 | return ret; | |
679 | } | |
680 | ||
ddf8abd2 MN |
681 | static int ffs_ep0_open(struct inode *inode, struct file *file) |
682 | { | |
683 | struct ffs_data *ffs = inode->i_private; | |
684 | ||
685 | ENTER(); | |
686 | ||
687 | if (unlikely(ffs->state == FFS_CLOSING)) | |
688 | return -EBUSY; | |
689 | ||
690 | file->private_data = ffs; | |
691 | ffs_data_opened(ffs); | |
692 | ||
693 | return 0; | |
694 | } | |
695 | ||
ddf8abd2 MN |
696 | static int ffs_ep0_release(struct inode *inode, struct file *file) |
697 | { | |
698 | struct ffs_data *ffs = file->private_data; | |
699 | ||
700 | ENTER(); | |
701 | ||
702 | ffs_data_closed(ffs); | |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
ddf8abd2 MN |
707 | static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) |
708 | { | |
709 | struct ffs_data *ffs = file->private_data; | |
710 | struct usb_gadget *gadget = ffs->gadget; | |
711 | long ret; | |
712 | ||
713 | ENTER(); | |
714 | ||
715 | if (code == FUNCTIONFS_INTERFACE_REVMAP) { | |
716 | struct ffs_function *func = ffs->func; | |
717 | ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; | |
718 | } else if (gadget->ops->ioctl) { | |
ddf8abd2 | 719 | ret = gadget->ops->ioctl(gadget, code, value); |
ddf8abd2 MN |
720 | } else { |
721 | ret = -ENOTTY; | |
722 | } | |
723 | ||
724 | return ret; | |
725 | } | |
726 | ||
ddf8abd2 MN |
727 | static const struct file_operations ffs_ep0_operations = { |
728 | .owner = THIS_MODULE, | |
729 | .llseek = no_llseek, | |
730 | ||
731 | .open = ffs_ep0_open, | |
732 | .write = ffs_ep0_write, | |
733 | .read = ffs_ep0_read, | |
734 | .release = ffs_ep0_release, | |
735 | .unlocked_ioctl = ffs_ep0_ioctl, | |
736 | }; | |
737 | ||
738 | ||
739 | /* "Normal" endpoints operations ********************************************/ | |
740 | ||
ddf8abd2 MN |
741 | static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req) |
742 | { | |
743 | ENTER(); | |
744 | if (likely(req->context)) { | |
745 | struct ffs_ep *ep = _ep->driver_data; | |
746 | ep->status = req->status ? req->status : req->actual; | |
747 | complete(req->context); | |
748 | } | |
749 | } | |
750 | ||
ddf8abd2 MN |
751 | static ssize_t ffs_epfile_io(struct file *file, |
752 | char __user *buf, size_t len, int read) | |
753 | { | |
754 | struct ffs_epfile *epfile = file->private_data; | |
755 | struct ffs_ep *ep; | |
756 | char *data = NULL; | |
757 | ssize_t ret; | |
758 | int halt; | |
759 | ||
760 | goto first_try; | |
761 | do { | |
762 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
763 | mutex_unlock(&epfile->mutex); | |
764 | ||
765 | first_try: | |
766 | /* Are we still active? */ | |
767 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) { | |
768 | ret = -ENODEV; | |
769 | goto error; | |
770 | } | |
771 | ||
772 | /* Wait for endpoint to be enabled */ | |
773 | ep = epfile->ep; | |
774 | if (!ep) { | |
775 | if (file->f_flags & O_NONBLOCK) { | |
776 | ret = -EAGAIN; | |
777 | goto error; | |
778 | } | |
779 | ||
5ab54cf7 MN |
780 | if (wait_event_interruptible(epfile->wait, |
781 | (ep = epfile->ep))) { | |
ddf8abd2 MN |
782 | ret = -EINTR; |
783 | goto error; | |
784 | } | |
785 | } | |
786 | ||
787 | /* Do we halt? */ | |
788 | halt = !read == !epfile->in; | |
789 | if (halt && epfile->isoc) { | |
790 | ret = -EINVAL; | |
791 | goto error; | |
792 | } | |
793 | ||
794 | /* Allocate & copy */ | |
795 | if (!halt && !data) { | |
796 | data = kzalloc(len, GFP_KERNEL); | |
797 | if (unlikely(!data)) | |
798 | return -ENOMEM; | |
799 | ||
800 | if (!read && | |
801 | unlikely(__copy_from_user(data, buf, len))) { | |
802 | ret = -EFAULT; | |
803 | goto error; | |
804 | } | |
805 | } | |
806 | ||
807 | /* We will be using request */ | |
808 | ret = ffs_mutex_lock(&epfile->mutex, | |
809 | file->f_flags & O_NONBLOCK); | |
810 | if (unlikely(ret)) | |
811 | goto error; | |
812 | ||
5ab54cf7 MN |
813 | /* |
814 | * We're called from user space, we can use _irq rather then | |
815 | * _irqsave | |
816 | */ | |
ddf8abd2 MN |
817 | spin_lock_irq(&epfile->ffs->eps_lock); |
818 | ||
5ab54cf7 MN |
819 | /* |
820 | * While we were acquiring mutex endpoint got disabled | |
821 | * or changed? | |
822 | */ | |
ddf8abd2 MN |
823 | } while (unlikely(epfile->ep != ep)); |
824 | ||
825 | /* Halt */ | |
826 | if (unlikely(halt)) { | |
827 | if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep)) | |
828 | usb_ep_set_halt(ep->ep); | |
829 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
830 | ret = -EBADMSG; | |
831 | } else { | |
832 | /* Fire the request */ | |
833 | DECLARE_COMPLETION_ONSTACK(done); | |
834 | ||
835 | struct usb_request *req = ep->req; | |
836 | req->context = &done; | |
837 | req->complete = ffs_epfile_io_complete; | |
838 | req->buf = data; | |
839 | req->length = len; | |
840 | ||
841 | ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); | |
842 | ||
843 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
844 | ||
845 | if (unlikely(ret < 0)) { | |
846 | /* nop */ | |
847 | } else if (unlikely(wait_for_completion_interruptible(&done))) { | |
848 | ret = -EINTR; | |
849 | usb_ep_dequeue(ep->ep, req); | |
850 | } else { | |
851 | ret = ep->status; | |
852 | if (read && ret > 0 && | |
853 | unlikely(copy_to_user(buf, data, ret))) | |
854 | ret = -EFAULT; | |
855 | } | |
856 | } | |
857 | ||
858 | mutex_unlock(&epfile->mutex); | |
859 | error: | |
860 | kfree(data); | |
861 | return ret; | |
862 | } | |
863 | ||
ddf8abd2 MN |
864 | static ssize_t |
865 | ffs_epfile_write(struct file *file, const char __user *buf, size_t len, | |
866 | loff_t *ptr) | |
867 | { | |
868 | ENTER(); | |
869 | ||
870 | return ffs_epfile_io(file, (char __user *)buf, len, 0); | |
871 | } | |
872 | ||
873 | static ssize_t | |
874 | ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr) | |
875 | { | |
876 | ENTER(); | |
877 | ||
878 | return ffs_epfile_io(file, buf, len, 1); | |
879 | } | |
880 | ||
881 | static int | |
882 | ffs_epfile_open(struct inode *inode, struct file *file) | |
883 | { | |
884 | struct ffs_epfile *epfile = inode->i_private; | |
885 | ||
886 | ENTER(); | |
887 | ||
888 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) | |
889 | return -ENODEV; | |
890 | ||
891 | file->private_data = epfile; | |
892 | ffs_data_opened(epfile->ffs); | |
893 | ||
894 | return 0; | |
895 | } | |
896 | ||
897 | static int | |
898 | ffs_epfile_release(struct inode *inode, struct file *file) | |
899 | { | |
900 | struct ffs_epfile *epfile = inode->i_private; | |
901 | ||
902 | ENTER(); | |
903 | ||
904 | ffs_data_closed(epfile->ffs); | |
905 | ||
906 | return 0; | |
907 | } | |
908 | ||
ddf8abd2 MN |
909 | static long ffs_epfile_ioctl(struct file *file, unsigned code, |
910 | unsigned long value) | |
911 | { | |
912 | struct ffs_epfile *epfile = file->private_data; | |
913 | int ret; | |
914 | ||
915 | ENTER(); | |
916 | ||
917 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) | |
918 | return -ENODEV; | |
919 | ||
920 | spin_lock_irq(&epfile->ffs->eps_lock); | |
921 | if (likely(epfile->ep)) { | |
922 | switch (code) { | |
923 | case FUNCTIONFS_FIFO_STATUS: | |
924 | ret = usb_ep_fifo_status(epfile->ep->ep); | |
925 | break; | |
926 | case FUNCTIONFS_FIFO_FLUSH: | |
927 | usb_ep_fifo_flush(epfile->ep->ep); | |
928 | ret = 0; | |
929 | break; | |
930 | case FUNCTIONFS_CLEAR_HALT: | |
931 | ret = usb_ep_clear_halt(epfile->ep->ep); | |
932 | break; | |
933 | case FUNCTIONFS_ENDPOINT_REVMAP: | |
934 | ret = epfile->ep->num; | |
935 | break; | |
936 | default: | |
937 | ret = -ENOTTY; | |
938 | } | |
939 | } else { | |
940 | ret = -ENODEV; | |
941 | } | |
942 | spin_unlock_irq(&epfile->ffs->eps_lock); | |
943 | ||
944 | return ret; | |
945 | } | |
946 | ||
ddf8abd2 MN |
947 | static const struct file_operations ffs_epfile_operations = { |
948 | .owner = THIS_MODULE, | |
949 | .llseek = no_llseek, | |
950 | ||
951 | .open = ffs_epfile_open, | |
952 | .write = ffs_epfile_write, | |
953 | .read = ffs_epfile_read, | |
954 | .release = ffs_epfile_release, | |
955 | .unlocked_ioctl = ffs_epfile_ioctl, | |
956 | }; | |
957 | ||
958 | ||
ddf8abd2 MN |
959 | /* File system and super block operations ***********************************/ |
960 | ||
961 | /* | |
5ab54cf7 | 962 | * Mounting the file system creates a controller file, used first for |
ddf8abd2 MN |
963 | * function configuration then later for event monitoring. |
964 | */ | |
965 | ||
ddf8abd2 MN |
966 | static struct inode *__must_check |
967 | ffs_sb_make_inode(struct super_block *sb, void *data, | |
968 | const struct file_operations *fops, | |
969 | const struct inode_operations *iops, | |
970 | struct ffs_file_perms *perms) | |
971 | { | |
972 | struct inode *inode; | |
973 | ||
974 | ENTER(); | |
975 | ||
976 | inode = new_inode(sb); | |
977 | ||
978 | if (likely(inode)) { | |
979 | struct timespec current_time = CURRENT_TIME; | |
980 | ||
12ba8d1e | 981 | inode->i_ino = get_next_ino(); |
ddf8abd2 MN |
982 | inode->i_mode = perms->mode; |
983 | inode->i_uid = perms->uid; | |
984 | inode->i_gid = perms->gid; | |
985 | inode->i_atime = current_time; | |
986 | inode->i_mtime = current_time; | |
987 | inode->i_ctime = current_time; | |
988 | inode->i_private = data; | |
989 | if (fops) | |
990 | inode->i_fop = fops; | |
991 | if (iops) | |
992 | inode->i_op = iops; | |
993 | } | |
994 | ||
995 | return inode; | |
996 | } | |
997 | ||
ddf8abd2 | 998 | /* Create "regular" file */ |
ddf8abd2 MN |
999 | static struct inode *ffs_sb_create_file(struct super_block *sb, |
1000 | const char *name, void *data, | |
1001 | const struct file_operations *fops, | |
1002 | struct dentry **dentry_p) | |
1003 | { | |
1004 | struct ffs_data *ffs = sb->s_fs_info; | |
1005 | struct dentry *dentry; | |
1006 | struct inode *inode; | |
1007 | ||
1008 | ENTER(); | |
1009 | ||
1010 | dentry = d_alloc_name(sb->s_root, name); | |
1011 | if (unlikely(!dentry)) | |
1012 | return NULL; | |
1013 | ||
1014 | inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); | |
1015 | if (unlikely(!inode)) { | |
1016 | dput(dentry); | |
1017 | return NULL; | |
1018 | } | |
1019 | ||
1020 | d_add(dentry, inode); | |
1021 | if (dentry_p) | |
1022 | *dentry_p = dentry; | |
1023 | ||
1024 | return inode; | |
1025 | } | |
1026 | ||
ddf8abd2 | 1027 | /* Super block */ |
ddf8abd2 MN |
1028 | static const struct super_operations ffs_sb_operations = { |
1029 | .statfs = simple_statfs, | |
1030 | .drop_inode = generic_delete_inode, | |
1031 | }; | |
1032 | ||
1033 | struct ffs_sb_fill_data { | |
1034 | struct ffs_file_perms perms; | |
1035 | umode_t root_mode; | |
1036 | const char *dev_name; | |
1037 | }; | |
1038 | ||
1039 | static int ffs_sb_fill(struct super_block *sb, void *_data, int silent) | |
1040 | { | |
1041 | struct ffs_sb_fill_data *data = _data; | |
1042 | struct inode *inode; | |
1043 | struct dentry *d; | |
1044 | struct ffs_data *ffs; | |
1045 | ||
1046 | ENTER(); | |
1047 | ||
5ab54cf7 | 1048 | /* Initialise data */ |
ddf8abd2 MN |
1049 | ffs = ffs_data_new(); |
1050 | if (unlikely(!ffs)) | |
1051 | goto enomem0; | |
1052 | ||
1053 | ffs->sb = sb; | |
1054 | ffs->dev_name = data->dev_name; | |
1055 | ffs->file_perms = data->perms; | |
1056 | ||
1057 | sb->s_fs_info = ffs; | |
1058 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
1059 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
1060 | sb->s_magic = FUNCTIONFS_MAGIC; | |
1061 | sb->s_op = &ffs_sb_operations; | |
1062 | sb->s_time_gran = 1; | |
1063 | ||
1064 | /* Root inode */ | |
1065 | data->perms.mode = data->root_mode; | |
1066 | inode = ffs_sb_make_inode(sb, NULL, | |
1067 | &simple_dir_operations, | |
1068 | &simple_dir_inode_operations, | |
1069 | &data->perms); | |
1070 | if (unlikely(!inode)) | |
1071 | goto enomem1; | |
1072 | d = d_alloc_root(inode); | |
1073 | if (unlikely(!d)) | |
1074 | goto enomem2; | |
1075 | sb->s_root = d; | |
1076 | ||
1077 | /* EP0 file */ | |
1078 | if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs, | |
1079 | &ffs_ep0_operations, NULL))) | |
1080 | goto enomem3; | |
1081 | ||
1082 | return 0; | |
1083 | ||
1084 | enomem3: | |
1085 | dput(d); | |
1086 | enomem2: | |
1087 | iput(inode); | |
1088 | enomem1: | |
1089 | ffs_data_put(ffs); | |
1090 | enomem0: | |
1091 | return -ENOMEM; | |
1092 | } | |
1093 | ||
ddf8abd2 MN |
1094 | static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts) |
1095 | { | |
1096 | ENTER(); | |
1097 | ||
1098 | if (!opts || !*opts) | |
1099 | return 0; | |
1100 | ||
1101 | for (;;) { | |
1102 | char *end, *eq, *comma; | |
1103 | unsigned long value; | |
1104 | ||
1105 | /* Option limit */ | |
1106 | comma = strchr(opts, ','); | |
1107 | if (comma) | |
1108 | *comma = 0; | |
1109 | ||
1110 | /* Value limit */ | |
1111 | eq = strchr(opts, '='); | |
1112 | if (unlikely(!eq)) { | |
d8df0b61 | 1113 | pr_err("'=' missing in %s", opts); |
ddf8abd2 MN |
1114 | return -EINVAL; |
1115 | } | |
1116 | *eq = 0; | |
1117 | ||
1118 | /* Parse value */ | |
1119 | value = simple_strtoul(eq + 1, &end, 0); | |
1120 | if (unlikely(*end != ',' && *end != 0)) { | |
d8df0b61 | 1121 | pr_err("%s: invalid value: %s", opts, eq + 1); |
ddf8abd2 MN |
1122 | return -EINVAL; |
1123 | } | |
1124 | ||
1125 | /* Interpret option */ | |
1126 | switch (eq - opts) { | |
1127 | case 5: | |
1128 | if (!memcmp(opts, "rmode", 5)) | |
1129 | data->root_mode = (value & 0555) | S_IFDIR; | |
1130 | else if (!memcmp(opts, "fmode", 5)) | |
1131 | data->perms.mode = (value & 0666) | S_IFREG; | |
1132 | else | |
1133 | goto invalid; | |
1134 | break; | |
1135 | ||
1136 | case 4: | |
1137 | if (!memcmp(opts, "mode", 4)) { | |
1138 | data->root_mode = (value & 0555) | S_IFDIR; | |
1139 | data->perms.mode = (value & 0666) | S_IFREG; | |
1140 | } else { | |
1141 | goto invalid; | |
1142 | } | |
1143 | break; | |
1144 | ||
1145 | case 3: | |
1146 | if (!memcmp(opts, "uid", 3)) | |
1147 | data->perms.uid = value; | |
1148 | else if (!memcmp(opts, "gid", 3)) | |
1149 | data->perms.gid = value; | |
1150 | else | |
1151 | goto invalid; | |
1152 | break; | |
1153 | ||
1154 | default: | |
1155 | invalid: | |
d8df0b61 | 1156 | pr_err("%s: invalid option", opts); |
ddf8abd2 MN |
1157 | return -EINVAL; |
1158 | } | |
1159 | ||
1160 | /* Next iteration */ | |
1161 | if (!comma) | |
1162 | break; | |
1163 | opts = comma + 1; | |
1164 | } | |
1165 | ||
1166 | return 0; | |
1167 | } | |
1168 | ||
ddf8abd2 MN |
1169 | /* "mount -t functionfs dev_name /dev/function" ends up here */ |
1170 | ||
fc14f2fe AV |
1171 | static struct dentry * |
1172 | ffs_fs_mount(struct file_system_type *t, int flags, | |
1173 | const char *dev_name, void *opts) | |
ddf8abd2 MN |
1174 | { |
1175 | struct ffs_sb_fill_data data = { | |
1176 | .perms = { | |
1177 | .mode = S_IFREG | 0600, | |
1178 | .uid = 0, | |
1179 | .gid = 0 | |
1180 | }, | |
1181 | .root_mode = S_IFDIR | 0500, | |
1182 | }; | |
1183 | int ret; | |
1184 | ||
1185 | ENTER(); | |
1186 | ||
1187 | ret = functionfs_check_dev_callback(dev_name); | |
1188 | if (unlikely(ret < 0)) | |
fc14f2fe | 1189 | return ERR_PTR(ret); |
ddf8abd2 MN |
1190 | |
1191 | ret = ffs_fs_parse_opts(&data, opts); | |
1192 | if (unlikely(ret < 0)) | |
fc14f2fe | 1193 | return ERR_PTR(ret); |
ddf8abd2 MN |
1194 | |
1195 | data.dev_name = dev_name; | |
fc14f2fe | 1196 | return mount_single(t, flags, &data, ffs_sb_fill); |
ddf8abd2 MN |
1197 | } |
1198 | ||
1199 | static void | |
1200 | ffs_fs_kill_sb(struct super_block *sb) | |
1201 | { | |
1202 | void *ptr; | |
1203 | ||
1204 | ENTER(); | |
1205 | ||
1206 | kill_litter_super(sb); | |
1207 | ptr = xchg(&sb->s_fs_info, NULL); | |
1208 | if (ptr) | |
1209 | ffs_data_put(ptr); | |
1210 | } | |
1211 | ||
1212 | static struct file_system_type ffs_fs_type = { | |
1213 | .owner = THIS_MODULE, | |
1214 | .name = "functionfs", | |
fc14f2fe | 1215 | .mount = ffs_fs_mount, |
ddf8abd2 MN |
1216 | .kill_sb = ffs_fs_kill_sb, |
1217 | }; | |
1218 | ||
1219 | ||
ddf8abd2 MN |
1220 | /* Driver's main init/cleanup functions *************************************/ |
1221 | ||
ddf8abd2 MN |
1222 | static int functionfs_init(void) |
1223 | { | |
1224 | int ret; | |
1225 | ||
1226 | ENTER(); | |
1227 | ||
1228 | ret = register_filesystem(&ffs_fs_type); | |
1229 | if (likely(!ret)) | |
d8df0b61 | 1230 | pr_info("file system registered"); |
ddf8abd2 | 1231 | else |
d8df0b61 | 1232 | pr_err("failed registering file system (%d)", ret); |
ddf8abd2 MN |
1233 | |
1234 | return ret; | |
1235 | } | |
1236 | ||
1237 | static void functionfs_cleanup(void) | |
1238 | { | |
1239 | ENTER(); | |
1240 | ||
d8df0b61 | 1241 | pr_info("unloading"); |
ddf8abd2 MN |
1242 | unregister_filesystem(&ffs_fs_type); |
1243 | } | |
1244 | ||
1245 | ||
ddf8abd2 MN |
1246 | /* ffs_data and ffs_function construction and destruction code **************/ |
1247 | ||
1248 | static void ffs_data_clear(struct ffs_data *ffs); | |
1249 | static void ffs_data_reset(struct ffs_data *ffs); | |
1250 | ||
ddf8abd2 MN |
1251 | static void ffs_data_get(struct ffs_data *ffs) |
1252 | { | |
1253 | ENTER(); | |
1254 | ||
1255 | atomic_inc(&ffs->ref); | |
1256 | } | |
1257 | ||
1258 | static void ffs_data_opened(struct ffs_data *ffs) | |
1259 | { | |
1260 | ENTER(); | |
1261 | ||
1262 | atomic_inc(&ffs->ref); | |
1263 | atomic_inc(&ffs->opened); | |
1264 | } | |
1265 | ||
1266 | static void ffs_data_put(struct ffs_data *ffs) | |
1267 | { | |
1268 | ENTER(); | |
1269 | ||
1270 | if (unlikely(atomic_dec_and_test(&ffs->ref))) { | |
d8df0b61 | 1271 | pr_info("%s(): freeing", __func__); |
ddf8abd2 MN |
1272 | ffs_data_clear(ffs); |
1273 | BUG_ON(mutex_is_locked(&ffs->mutex) || | |
1274 | spin_is_locked(&ffs->ev.waitq.lock) || | |
1275 | waitqueue_active(&ffs->ev.waitq) || | |
1276 | waitqueue_active(&ffs->ep0req_completion.wait)); | |
1277 | kfree(ffs); | |
1278 | } | |
1279 | } | |
1280 | ||
ddf8abd2 MN |
1281 | static void ffs_data_closed(struct ffs_data *ffs) |
1282 | { | |
1283 | ENTER(); | |
1284 | ||
1285 | if (atomic_dec_and_test(&ffs->opened)) { | |
1286 | ffs->state = FFS_CLOSING; | |
1287 | ffs_data_reset(ffs); | |
1288 | } | |
1289 | ||
1290 | ffs_data_put(ffs); | |
1291 | } | |
1292 | ||
ddf8abd2 MN |
1293 | static struct ffs_data *ffs_data_new(void) |
1294 | { | |
1295 | struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); | |
1296 | if (unlikely(!ffs)) | |
1297 | return 0; | |
1298 | ||
1299 | ENTER(); | |
1300 | ||
1301 | atomic_set(&ffs->ref, 1); | |
1302 | atomic_set(&ffs->opened, 0); | |
1303 | ffs->state = FFS_READ_DESCRIPTORS; | |
1304 | mutex_init(&ffs->mutex); | |
1305 | spin_lock_init(&ffs->eps_lock); | |
1306 | init_waitqueue_head(&ffs->ev.waitq); | |
1307 | init_completion(&ffs->ep0req_completion); | |
1308 | ||
1309 | /* XXX REVISIT need to update it in some places, or do we? */ | |
1310 | ffs->ev.can_stall = 1; | |
1311 | ||
1312 | return ffs; | |
1313 | } | |
1314 | ||
ddf8abd2 MN |
1315 | static void ffs_data_clear(struct ffs_data *ffs) |
1316 | { | |
1317 | ENTER(); | |
1318 | ||
1319 | if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags)) | |
1320 | functionfs_closed_callback(ffs); | |
1321 | ||
1322 | BUG_ON(ffs->gadget); | |
1323 | ||
1324 | if (ffs->epfiles) | |
1325 | ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count); | |
1326 | ||
1327 | kfree(ffs->raw_descs); | |
1328 | kfree(ffs->raw_strings); | |
1329 | kfree(ffs->stringtabs); | |
1330 | } | |
1331 | ||
ddf8abd2 MN |
1332 | static void ffs_data_reset(struct ffs_data *ffs) |
1333 | { | |
1334 | ENTER(); | |
1335 | ||
1336 | ffs_data_clear(ffs); | |
1337 | ||
1338 | ffs->epfiles = NULL; | |
1339 | ffs->raw_descs = NULL; | |
1340 | ffs->raw_strings = NULL; | |
1341 | ffs->stringtabs = NULL; | |
1342 | ||
1343 | ffs->raw_descs_length = 0; | |
1344 | ffs->raw_fs_descs_length = 0; | |
1345 | ffs->fs_descs_count = 0; | |
1346 | ffs->hs_descs_count = 0; | |
1347 | ||
1348 | ffs->strings_count = 0; | |
1349 | ffs->interfaces_count = 0; | |
1350 | ffs->eps_count = 0; | |
1351 | ||
1352 | ffs->ev.count = 0; | |
1353 | ||
1354 | ffs->state = FFS_READ_DESCRIPTORS; | |
1355 | ffs->setup_state = FFS_NO_SETUP; | |
1356 | ffs->flags = 0; | |
1357 | } | |
1358 | ||
1359 | ||
1360 | static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev) | |
1361 | { | |
fd7c9a00 MN |
1362 | struct usb_gadget_strings **lang; |
1363 | int first_id; | |
ddf8abd2 MN |
1364 | |
1365 | ENTER(); | |
1366 | ||
1367 | if (WARN_ON(ffs->state != FFS_ACTIVE | |
1368 | || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) | |
1369 | return -EBADFD; | |
1370 | ||
fd7c9a00 MN |
1371 | first_id = usb_string_ids_n(cdev, ffs->strings_count); |
1372 | if (unlikely(first_id < 0)) | |
1373 | return first_id; | |
ddf8abd2 MN |
1374 | |
1375 | ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); | |
1376 | if (unlikely(!ffs->ep0req)) | |
1377 | return -ENOMEM; | |
1378 | ffs->ep0req->complete = ffs_ep0_complete; | |
1379 | ffs->ep0req->context = ffs; | |
1380 | ||
fd7c9a00 MN |
1381 | lang = ffs->stringtabs; |
1382 | for (lang = ffs->stringtabs; *lang; ++lang) { | |
1383 | struct usb_string *str = (*lang)->strings; | |
1384 | int id = first_id; | |
1385 | for (; str->s; ++id, ++str) | |
1386 | str->id = id; | |
ddf8abd2 MN |
1387 | } |
1388 | ||
1389 | ffs->gadget = cdev->gadget; | |
fd7c9a00 | 1390 | ffs_data_get(ffs); |
ddf8abd2 MN |
1391 | return 0; |
1392 | } | |
1393 | ||
ddf8abd2 MN |
1394 | static void functionfs_unbind(struct ffs_data *ffs) |
1395 | { | |
1396 | ENTER(); | |
1397 | ||
1398 | if (!WARN_ON(!ffs->gadget)) { | |
1399 | usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); | |
1400 | ffs->ep0req = NULL; | |
1401 | ffs->gadget = NULL; | |
1402 | ffs_data_put(ffs); | |
1403 | } | |
1404 | } | |
1405 | ||
ddf8abd2 MN |
1406 | static int ffs_epfiles_create(struct ffs_data *ffs) |
1407 | { | |
1408 | struct ffs_epfile *epfile, *epfiles; | |
1409 | unsigned i, count; | |
1410 | ||
1411 | ENTER(); | |
1412 | ||
1413 | count = ffs->eps_count; | |
1414 | epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL); | |
1415 | if (!epfiles) | |
1416 | return -ENOMEM; | |
1417 | ||
1418 | epfile = epfiles; | |
1419 | for (i = 1; i <= count; ++i, ++epfile) { | |
1420 | epfile->ffs = ffs; | |
1421 | mutex_init(&epfile->mutex); | |
1422 | init_waitqueue_head(&epfile->wait); | |
1423 | sprintf(epfiles->name, "ep%u", i); | |
1424 | if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile, | |
1425 | &ffs_epfile_operations, | |
1426 | &epfile->dentry))) { | |
1427 | ffs_epfiles_destroy(epfiles, i - 1); | |
1428 | return -ENOMEM; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | ffs->epfiles = epfiles; | |
1433 | return 0; | |
1434 | } | |
1435 | ||
ddf8abd2 MN |
1436 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) |
1437 | { | |
1438 | struct ffs_epfile *epfile = epfiles; | |
1439 | ||
1440 | ENTER(); | |
1441 | ||
1442 | for (; count; --count, ++epfile) { | |
1443 | BUG_ON(mutex_is_locked(&epfile->mutex) || | |
1444 | waitqueue_active(&epfile->wait)); | |
1445 | if (epfile->dentry) { | |
1446 | d_delete(epfile->dentry); | |
1447 | dput(epfile->dentry); | |
1448 | epfile->dentry = NULL; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | kfree(epfiles); | |
1453 | } | |
1454 | ||
7898aee1 MN |
1455 | static int functionfs_bind_config(struct usb_composite_dev *cdev, |
1456 | struct usb_configuration *c, | |
1457 | struct ffs_data *ffs) | |
ddf8abd2 MN |
1458 | { |
1459 | struct ffs_function *func; | |
1460 | int ret; | |
1461 | ||
1462 | ENTER(); | |
1463 | ||
1464 | func = kzalloc(sizeof *func, GFP_KERNEL); | |
1465 | if (unlikely(!func)) | |
1466 | return -ENOMEM; | |
1467 | ||
1468 | func->function.name = "Function FS Gadget"; | |
1469 | func->function.strings = ffs->stringtabs; | |
1470 | ||
1471 | func->function.bind = ffs_func_bind; | |
1472 | func->function.unbind = ffs_func_unbind; | |
1473 | func->function.set_alt = ffs_func_set_alt; | |
ddf8abd2 MN |
1474 | func->function.disable = ffs_func_disable; |
1475 | func->function.setup = ffs_func_setup; | |
1476 | func->function.suspend = ffs_func_suspend; | |
1477 | func->function.resume = ffs_func_resume; | |
1478 | ||
1479 | func->conf = c; | |
1480 | func->gadget = cdev->gadget; | |
1481 | func->ffs = ffs; | |
1482 | ffs_data_get(ffs); | |
1483 | ||
1484 | ret = usb_add_function(c, &func->function); | |
1485 | if (unlikely(ret)) | |
1486 | ffs_func_free(func); | |
1487 | ||
1488 | return ret; | |
1489 | } | |
1490 | ||
1491 | static void ffs_func_free(struct ffs_function *func) | |
1492 | { | |
1493 | ENTER(); | |
1494 | ||
1495 | ffs_data_put(func->ffs); | |
1496 | ||
1497 | kfree(func->eps); | |
5ab54cf7 MN |
1498 | /* |
1499 | * eps and interfaces_nums are allocated in the same chunk so | |
ddf8abd2 | 1500 | * only one free is required. Descriptors are also allocated |
5ab54cf7 MN |
1501 | * in the same chunk. |
1502 | */ | |
ddf8abd2 MN |
1503 | |
1504 | kfree(func); | |
1505 | } | |
1506 | ||
ddf8abd2 MN |
1507 | static void ffs_func_eps_disable(struct ffs_function *func) |
1508 | { | |
1509 | struct ffs_ep *ep = func->eps; | |
1510 | struct ffs_epfile *epfile = func->ffs->epfiles; | |
1511 | unsigned count = func->ffs->eps_count; | |
1512 | unsigned long flags; | |
1513 | ||
1514 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | |
1515 | do { | |
1516 | /* pending requests get nuked */ | |
1517 | if (likely(ep->ep)) | |
1518 | usb_ep_disable(ep->ep); | |
1519 | epfile->ep = NULL; | |
1520 | ||
1521 | ++ep; | |
1522 | ++epfile; | |
1523 | } while (--count); | |
1524 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | |
1525 | } | |
1526 | ||
1527 | static int ffs_func_eps_enable(struct ffs_function *func) | |
1528 | { | |
1529 | struct ffs_data *ffs = func->ffs; | |
1530 | struct ffs_ep *ep = func->eps; | |
1531 | struct ffs_epfile *epfile = ffs->epfiles; | |
1532 | unsigned count = ffs->eps_count; | |
1533 | unsigned long flags; | |
1534 | int ret = 0; | |
1535 | ||
1536 | spin_lock_irqsave(&func->ffs->eps_lock, flags); | |
1537 | do { | |
1538 | struct usb_endpoint_descriptor *ds; | |
1539 | ds = ep->descs[ep->descs[1] ? 1 : 0]; | |
1540 | ||
1541 | ep->ep->driver_data = ep; | |
1542 | ret = usb_ep_enable(ep->ep, ds); | |
1543 | if (likely(!ret)) { | |
1544 | epfile->ep = ep; | |
1545 | epfile->in = usb_endpoint_dir_in(ds); | |
1546 | epfile->isoc = usb_endpoint_xfer_isoc(ds); | |
1547 | } else { | |
1548 | break; | |
1549 | } | |
1550 | ||
1551 | wake_up(&epfile->wait); | |
1552 | ||
1553 | ++ep; | |
1554 | ++epfile; | |
1555 | } while (--count); | |
1556 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); | |
1557 | ||
1558 | return ret; | |
1559 | } | |
1560 | ||
1561 | ||
1562 | /* Parsing and building descriptors and strings *****************************/ | |
1563 | ||
5ab54cf7 MN |
1564 | /* |
1565 | * This validates if data pointed by data is a valid USB descriptor as | |
ddf8abd2 | 1566 | * well as record how many interfaces, endpoints and strings are |
5ab54cf7 MN |
1567 | * required by given configuration. Returns address after the |
1568 | * descriptor or NULL if data is invalid. | |
1569 | */ | |
ddf8abd2 MN |
1570 | |
1571 | enum ffs_entity_type { | |
1572 | FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT | |
1573 | }; | |
1574 | ||
1575 | typedef int (*ffs_entity_callback)(enum ffs_entity_type entity, | |
1576 | u8 *valuep, | |
1577 | struct usb_descriptor_header *desc, | |
1578 | void *priv); | |
1579 | ||
1580 | static int __must_check ffs_do_desc(char *data, unsigned len, | |
1581 | ffs_entity_callback entity, void *priv) | |
1582 | { | |
1583 | struct usb_descriptor_header *_ds = (void *)data; | |
1584 | u8 length; | |
1585 | int ret; | |
1586 | ||
1587 | ENTER(); | |
1588 | ||
1589 | /* At least two bytes are required: length and type */ | |
1590 | if (len < 2) { | |
d8df0b61 | 1591 | pr_vdebug("descriptor too short"); |
ddf8abd2 MN |
1592 | return -EINVAL; |
1593 | } | |
1594 | ||
1595 | /* If we have at least as many bytes as the descriptor takes? */ | |
1596 | length = _ds->bLength; | |
1597 | if (len < length) { | |
d8df0b61 | 1598 | pr_vdebug("descriptor longer then available data"); |
ddf8abd2 MN |
1599 | return -EINVAL; |
1600 | } | |
1601 | ||
1602 | #define __entity_check_INTERFACE(val) 1 | |
1603 | #define __entity_check_STRING(val) (val) | |
1604 | #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK) | |
1605 | #define __entity(type, val) do { \ | |
d8df0b61 | 1606 | pr_vdebug("entity " #type "(%02x)", (val)); \ |
ddf8abd2 | 1607 | if (unlikely(!__entity_check_ ##type(val))) { \ |
d8df0b61 | 1608 | pr_vdebug("invalid entity's value"); \ |
ddf8abd2 MN |
1609 | return -EINVAL; \ |
1610 | } \ | |
1611 | ret = entity(FFS_ ##type, &val, _ds, priv); \ | |
1612 | if (unlikely(ret < 0)) { \ | |
d8df0b61 MN |
1613 | pr_debug("entity " #type "(%02x); ret = %d", \ |
1614 | (val), ret); \ | |
ddf8abd2 MN |
1615 | return ret; \ |
1616 | } \ | |
1617 | } while (0) | |
1618 | ||
1619 | /* Parse descriptor depending on type. */ | |
1620 | switch (_ds->bDescriptorType) { | |
1621 | case USB_DT_DEVICE: | |
1622 | case USB_DT_CONFIG: | |
1623 | case USB_DT_STRING: | |
1624 | case USB_DT_DEVICE_QUALIFIER: | |
1625 | /* function can't have any of those */ | |
d8df0b61 | 1626 | pr_vdebug("descriptor reserved for gadget: %d", |
5ab54cf7 | 1627 | _ds->bDescriptorType); |
ddf8abd2 MN |
1628 | return -EINVAL; |
1629 | ||
1630 | case USB_DT_INTERFACE: { | |
1631 | struct usb_interface_descriptor *ds = (void *)_ds; | |
d8df0b61 | 1632 | pr_vdebug("interface descriptor"); |
ddf8abd2 MN |
1633 | if (length != sizeof *ds) |
1634 | goto inv_length; | |
1635 | ||
1636 | __entity(INTERFACE, ds->bInterfaceNumber); | |
1637 | if (ds->iInterface) | |
1638 | __entity(STRING, ds->iInterface); | |
1639 | } | |
1640 | break; | |
1641 | ||
1642 | case USB_DT_ENDPOINT: { | |
1643 | struct usb_endpoint_descriptor *ds = (void *)_ds; | |
d8df0b61 | 1644 | pr_vdebug("endpoint descriptor"); |
ddf8abd2 MN |
1645 | if (length != USB_DT_ENDPOINT_SIZE && |
1646 | length != USB_DT_ENDPOINT_AUDIO_SIZE) | |
1647 | goto inv_length; | |
1648 | __entity(ENDPOINT, ds->bEndpointAddress); | |
1649 | } | |
1650 | break; | |
1651 | ||
1652 | case USB_DT_OTG: | |
1653 | if (length != sizeof(struct usb_otg_descriptor)) | |
1654 | goto inv_length; | |
1655 | break; | |
1656 | ||
1657 | case USB_DT_INTERFACE_ASSOCIATION: { | |
1658 | struct usb_interface_assoc_descriptor *ds = (void *)_ds; | |
d8df0b61 | 1659 | pr_vdebug("interface association descriptor"); |
ddf8abd2 MN |
1660 | if (length != sizeof *ds) |
1661 | goto inv_length; | |
1662 | if (ds->iFunction) | |
1663 | __entity(STRING, ds->iFunction); | |
1664 | } | |
1665 | break; | |
1666 | ||
1667 | case USB_DT_OTHER_SPEED_CONFIG: | |
1668 | case USB_DT_INTERFACE_POWER: | |
1669 | case USB_DT_DEBUG: | |
1670 | case USB_DT_SECURITY: | |
1671 | case USB_DT_CS_RADIO_CONTROL: | |
1672 | /* TODO */ | |
d8df0b61 | 1673 | pr_vdebug("unimplemented descriptor: %d", _ds->bDescriptorType); |
ddf8abd2 MN |
1674 | return -EINVAL; |
1675 | ||
1676 | default: | |
1677 | /* We should never be here */ | |
d8df0b61 | 1678 | pr_vdebug("unknown descriptor: %d", _ds->bDescriptorType); |
ddf8abd2 MN |
1679 | return -EINVAL; |
1680 | ||
5ab54cf7 | 1681 | inv_length: |
d8df0b61 MN |
1682 | pr_vdebug("invalid length: %d (descriptor %d)", |
1683 | _ds->bLength, _ds->bDescriptorType); | |
ddf8abd2 MN |
1684 | return -EINVAL; |
1685 | } | |
1686 | ||
1687 | #undef __entity | |
1688 | #undef __entity_check_DESCRIPTOR | |
1689 | #undef __entity_check_INTERFACE | |
1690 | #undef __entity_check_STRING | |
1691 | #undef __entity_check_ENDPOINT | |
1692 | ||
1693 | return length; | |
1694 | } | |
1695 | ||
ddf8abd2 MN |
1696 | static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len, |
1697 | ffs_entity_callback entity, void *priv) | |
1698 | { | |
1699 | const unsigned _len = len; | |
1700 | unsigned long num = 0; | |
1701 | ||
1702 | ENTER(); | |
1703 | ||
1704 | for (;;) { | |
1705 | int ret; | |
1706 | ||
1707 | if (num == count) | |
1708 | data = NULL; | |
1709 | ||
5ab54cf7 | 1710 | /* Record "descriptor" entity */ |
ddf8abd2 MN |
1711 | ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); |
1712 | if (unlikely(ret < 0)) { | |
d8df0b61 MN |
1713 | pr_debug("entity DESCRIPTOR(%02lx); ret = %d", |
1714 | num, ret); | |
ddf8abd2 MN |
1715 | return ret; |
1716 | } | |
1717 | ||
1718 | if (!data) | |
1719 | return _len - len; | |
1720 | ||
1721 | ret = ffs_do_desc(data, len, entity, priv); | |
1722 | if (unlikely(ret < 0)) { | |
d8df0b61 | 1723 | pr_debug("%s returns %d", __func__, ret); |
ddf8abd2 MN |
1724 | return ret; |
1725 | } | |
1726 | ||
1727 | len -= ret; | |
1728 | data += ret; | |
1729 | ++num; | |
1730 | } | |
1731 | } | |
1732 | ||
ddf8abd2 MN |
1733 | static int __ffs_data_do_entity(enum ffs_entity_type type, |
1734 | u8 *valuep, struct usb_descriptor_header *desc, | |
1735 | void *priv) | |
1736 | { | |
1737 | struct ffs_data *ffs = priv; | |
1738 | ||
1739 | ENTER(); | |
1740 | ||
1741 | switch (type) { | |
1742 | case FFS_DESCRIPTOR: | |
1743 | break; | |
1744 | ||
1745 | case FFS_INTERFACE: | |
5ab54cf7 MN |
1746 | /* |
1747 | * Interfaces are indexed from zero so if we | |
ddf8abd2 | 1748 | * encountered interface "n" then there are at least |
5ab54cf7 MN |
1749 | * "n+1" interfaces. |
1750 | */ | |
ddf8abd2 MN |
1751 | if (*valuep >= ffs->interfaces_count) |
1752 | ffs->interfaces_count = *valuep + 1; | |
1753 | break; | |
1754 | ||
1755 | case FFS_STRING: | |
5ab54cf7 MN |
1756 | /* |
1757 | * Strings are indexed from 1 (0 is magic ;) reserved | |
1758 | * for languages list or some such) | |
1759 | */ | |
ddf8abd2 MN |
1760 | if (*valuep > ffs->strings_count) |
1761 | ffs->strings_count = *valuep; | |
1762 | break; | |
1763 | ||
1764 | case FFS_ENDPOINT: | |
1765 | /* Endpoints are indexed from 1 as well. */ | |
1766 | if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count) | |
1767 | ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK); | |
1768 | break; | |
1769 | } | |
1770 | ||
1771 | return 0; | |
1772 | } | |
1773 | ||
ddf8abd2 MN |
1774 | static int __ffs_data_got_descs(struct ffs_data *ffs, |
1775 | char *const _data, size_t len) | |
1776 | { | |
1777 | unsigned fs_count, hs_count; | |
1778 | int fs_len, ret = -EINVAL; | |
1779 | char *data = _data; | |
1780 | ||
1781 | ENTER(); | |
1782 | ||
1783 | if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC || | |
1784 | get_unaligned_le32(data + 4) != len)) | |
1785 | goto error; | |
1786 | fs_count = get_unaligned_le32(data + 8); | |
1787 | hs_count = get_unaligned_le32(data + 12); | |
1788 | ||
1789 | if (!fs_count && !hs_count) | |
1790 | goto einval; | |
1791 | ||
1792 | data += 16; | |
1793 | len -= 16; | |
1794 | ||
1795 | if (likely(fs_count)) { | |
1796 | fs_len = ffs_do_descs(fs_count, data, len, | |
1797 | __ffs_data_do_entity, ffs); | |
1798 | if (unlikely(fs_len < 0)) { | |
1799 | ret = fs_len; | |
1800 | goto error; | |
1801 | } | |
1802 | ||
1803 | data += fs_len; | |
1804 | len -= fs_len; | |
1805 | } else { | |
1806 | fs_len = 0; | |
1807 | } | |
1808 | ||
1809 | if (likely(hs_count)) { | |
1810 | ret = ffs_do_descs(hs_count, data, len, | |
1811 | __ffs_data_do_entity, ffs); | |
1812 | if (unlikely(ret < 0)) | |
1813 | goto error; | |
1814 | } else { | |
1815 | ret = 0; | |
1816 | } | |
1817 | ||
1818 | if (unlikely(len != ret)) | |
1819 | goto einval; | |
1820 | ||
1821 | ffs->raw_fs_descs_length = fs_len; | |
1822 | ffs->raw_descs_length = fs_len + ret; | |
1823 | ffs->raw_descs = _data; | |
1824 | ffs->fs_descs_count = fs_count; | |
1825 | ffs->hs_descs_count = hs_count; | |
1826 | ||
1827 | return 0; | |
1828 | ||
1829 | einval: | |
1830 | ret = -EINVAL; | |
1831 | error: | |
1832 | kfree(_data); | |
1833 | return ret; | |
1834 | } | |
1835 | ||
ddf8abd2 MN |
1836 | static int __ffs_data_got_strings(struct ffs_data *ffs, |
1837 | char *const _data, size_t len) | |
1838 | { | |
1839 | u32 str_count, needed_count, lang_count; | |
1840 | struct usb_gadget_strings **stringtabs, *t; | |
1841 | struct usb_string *strings, *s; | |
1842 | const char *data = _data; | |
1843 | ||
1844 | ENTER(); | |
1845 | ||
1846 | if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC || | |
1847 | get_unaligned_le32(data + 4) != len)) | |
1848 | goto error; | |
1849 | str_count = get_unaligned_le32(data + 8); | |
1850 | lang_count = get_unaligned_le32(data + 12); | |
1851 | ||
1852 | /* if one is zero the other must be zero */ | |
1853 | if (unlikely(!str_count != !lang_count)) | |
1854 | goto error; | |
1855 | ||
1856 | /* Do we have at least as many strings as descriptors need? */ | |
1857 | needed_count = ffs->strings_count; | |
1858 | if (unlikely(str_count < needed_count)) | |
1859 | goto error; | |
1860 | ||
5ab54cf7 MN |
1861 | /* |
1862 | * If we don't need any strings just return and free all | |
1863 | * memory. | |
1864 | */ | |
ddf8abd2 MN |
1865 | if (!needed_count) { |
1866 | kfree(_data); | |
1867 | return 0; | |
1868 | } | |
1869 | ||
5ab54cf7 | 1870 | /* Allocate everything in one chunk so there's less maintenance. */ |
ddf8abd2 | 1871 | { |
ddf8abd2 MN |
1872 | struct { |
1873 | struct usb_gadget_strings *stringtabs[lang_count + 1]; | |
1874 | struct usb_gadget_strings stringtab[lang_count]; | |
1875 | struct usb_string strings[lang_count*(needed_count+1)]; | |
1876 | } *d; | |
1877 | unsigned i = 0; | |
1878 | ||
1879 | d = kmalloc(sizeof *d, GFP_KERNEL); | |
1880 | if (unlikely(!d)) { | |
1881 | kfree(_data); | |
1882 | return -ENOMEM; | |
1883 | } | |
1884 | ||
1885 | stringtabs = d->stringtabs; | |
1886 | t = d->stringtab; | |
1887 | i = lang_count; | |
1888 | do { | |
1889 | *stringtabs++ = t++; | |
1890 | } while (--i); | |
1891 | *stringtabs = NULL; | |
1892 | ||
1893 | stringtabs = d->stringtabs; | |
1894 | t = d->stringtab; | |
1895 | s = d->strings; | |
1896 | strings = s; | |
1897 | } | |
1898 | ||
1899 | /* For each language */ | |
1900 | data += 16; | |
1901 | len -= 16; | |
1902 | ||
1903 | do { /* lang_count > 0 so we can use do-while */ | |
1904 | unsigned needed = needed_count; | |
1905 | ||
1906 | if (unlikely(len < 3)) | |
1907 | goto error_free; | |
1908 | t->language = get_unaligned_le16(data); | |
1909 | t->strings = s; | |
1910 | ++t; | |
1911 | ||
1912 | data += 2; | |
1913 | len -= 2; | |
1914 | ||
1915 | /* For each string */ | |
1916 | do { /* str_count > 0 so we can use do-while */ | |
1917 | size_t length = strnlen(data, len); | |
1918 | ||
1919 | if (unlikely(length == len)) | |
1920 | goto error_free; | |
1921 | ||
5ab54cf7 MN |
1922 | /* |
1923 | * User may provide more strings then we need, | |
1924 | * if that's the case we simply ignore the | |
1925 | * rest | |
1926 | */ | |
ddf8abd2 | 1927 | if (likely(needed)) { |
5ab54cf7 MN |
1928 | /* |
1929 | * s->id will be set while adding | |
ddf8abd2 | 1930 | * function to configuration so for |
5ab54cf7 MN |
1931 | * now just leave garbage here. |
1932 | */ | |
ddf8abd2 MN |
1933 | s->s = data; |
1934 | --needed; | |
1935 | ++s; | |
1936 | } | |
1937 | ||
1938 | data += length + 1; | |
1939 | len -= length + 1; | |
1940 | } while (--str_count); | |
1941 | ||
1942 | s->id = 0; /* terminator */ | |
1943 | s->s = NULL; | |
1944 | ++s; | |
1945 | ||
1946 | } while (--lang_count); | |
1947 | ||
1948 | /* Some garbage left? */ | |
1949 | if (unlikely(len)) | |
1950 | goto error_free; | |
1951 | ||
1952 | /* Done! */ | |
1953 | ffs->stringtabs = stringtabs; | |
1954 | ffs->raw_strings = _data; | |
1955 | ||
1956 | return 0; | |
1957 | ||
1958 | error_free: | |
1959 | kfree(stringtabs); | |
1960 | error: | |
1961 | kfree(_data); | |
1962 | return -EINVAL; | |
1963 | } | |
1964 | ||
1965 | ||
ddf8abd2 MN |
1966 | /* Events handling and management *******************************************/ |
1967 | ||
1968 | static void __ffs_event_add(struct ffs_data *ffs, | |
1969 | enum usb_functionfs_event_type type) | |
1970 | { | |
1971 | enum usb_functionfs_event_type rem_type1, rem_type2 = type; | |
1972 | int neg = 0; | |
1973 | ||
5ab54cf7 MN |
1974 | /* |
1975 | * Abort any unhandled setup | |
1976 | * | |
1977 | * We do not need to worry about some cmpxchg() changing value | |
ddf8abd2 MN |
1978 | * of ffs->setup_state without holding the lock because when |
1979 | * state is FFS_SETUP_PENDING cmpxchg() in several places in | |
5ab54cf7 MN |
1980 | * the source does nothing. |
1981 | */ | |
ddf8abd2 MN |
1982 | if (ffs->setup_state == FFS_SETUP_PENDING) |
1983 | ffs->setup_state = FFS_SETUP_CANCELED; | |
1984 | ||
1985 | switch (type) { | |
1986 | case FUNCTIONFS_RESUME: | |
1987 | rem_type2 = FUNCTIONFS_SUSPEND; | |
5ab54cf7 | 1988 | /* FALL THROUGH */ |
ddf8abd2 MN |
1989 | case FUNCTIONFS_SUSPEND: |
1990 | case FUNCTIONFS_SETUP: | |
1991 | rem_type1 = type; | |
5ab54cf7 | 1992 | /* Discard all similar events */ |
ddf8abd2 MN |
1993 | break; |
1994 | ||
1995 | case FUNCTIONFS_BIND: | |
1996 | case FUNCTIONFS_UNBIND: | |
1997 | case FUNCTIONFS_DISABLE: | |
1998 | case FUNCTIONFS_ENABLE: | |
5ab54cf7 | 1999 | /* Discard everything other then power management. */ |
ddf8abd2 MN |
2000 | rem_type1 = FUNCTIONFS_SUSPEND; |
2001 | rem_type2 = FUNCTIONFS_RESUME; | |
2002 | neg = 1; | |
2003 | break; | |
2004 | ||
2005 | default: | |
2006 | BUG(); | |
2007 | } | |
2008 | ||
2009 | { | |
2010 | u8 *ev = ffs->ev.types, *out = ev; | |
2011 | unsigned n = ffs->ev.count; | |
2012 | for (; n; --n, ++ev) | |
2013 | if ((*ev == rem_type1 || *ev == rem_type2) == neg) | |
2014 | *out++ = *ev; | |
2015 | else | |
d8df0b61 | 2016 | pr_vdebug("purging event %d", *ev); |
ddf8abd2 MN |
2017 | ffs->ev.count = out - ffs->ev.types; |
2018 | } | |
2019 | ||
d8df0b61 | 2020 | pr_vdebug("adding event %d", type); |
ddf8abd2 MN |
2021 | ffs->ev.types[ffs->ev.count++] = type; |
2022 | wake_up_locked(&ffs->ev.waitq); | |
2023 | } | |
2024 | ||
2025 | static void ffs_event_add(struct ffs_data *ffs, | |
2026 | enum usb_functionfs_event_type type) | |
2027 | { | |
2028 | unsigned long flags; | |
2029 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); | |
2030 | __ffs_event_add(ffs, type); | |
2031 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); | |
2032 | } | |
2033 | ||
2034 | ||
2035 | /* Bind/unbind USB function hooks *******************************************/ | |
2036 | ||
2037 | static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep, | |
2038 | struct usb_descriptor_header *desc, | |
2039 | void *priv) | |
2040 | { | |
2041 | struct usb_endpoint_descriptor *ds = (void *)desc; | |
2042 | struct ffs_function *func = priv; | |
2043 | struct ffs_ep *ffs_ep; | |
2044 | ||
5ab54cf7 MN |
2045 | /* |
2046 | * If hs_descriptors is not NULL then we are reading hs | |
2047 | * descriptors now | |
2048 | */ | |
ddf8abd2 MN |
2049 | const int isHS = func->function.hs_descriptors != NULL; |
2050 | unsigned idx; | |
2051 | ||
2052 | if (type != FFS_DESCRIPTOR) | |
2053 | return 0; | |
2054 | ||
2055 | if (isHS) | |
2056 | func->function.hs_descriptors[(long)valuep] = desc; | |
2057 | else | |
2058 | func->function.descriptors[(long)valuep] = desc; | |
2059 | ||
2060 | if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) | |
2061 | return 0; | |
2062 | ||
2063 | idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1; | |
2064 | ffs_ep = func->eps + idx; | |
2065 | ||
2066 | if (unlikely(ffs_ep->descs[isHS])) { | |
d8df0b61 MN |
2067 | pr_vdebug("two %sspeed descriptors for EP %d", |
2068 | isHS ? "high" : "full", | |
2069 | ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); | |
ddf8abd2 MN |
2070 | return -EINVAL; |
2071 | } | |
2072 | ffs_ep->descs[isHS] = ds; | |
2073 | ||
2074 | ffs_dump_mem(": Original ep desc", ds, ds->bLength); | |
2075 | if (ffs_ep->ep) { | |
2076 | ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; | |
2077 | if (!ds->wMaxPacketSize) | |
2078 | ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; | |
2079 | } else { | |
2080 | struct usb_request *req; | |
2081 | struct usb_ep *ep; | |
2082 | ||
d8df0b61 | 2083 | pr_vdebug("autoconfig"); |
ddf8abd2 MN |
2084 | ep = usb_ep_autoconfig(func->gadget, ds); |
2085 | if (unlikely(!ep)) | |
2086 | return -ENOTSUPP; | |
cc7e6056 | 2087 | ep->driver_data = func->eps + idx; |
ddf8abd2 MN |
2088 | |
2089 | req = usb_ep_alloc_request(ep, GFP_KERNEL); | |
2090 | if (unlikely(!req)) | |
2091 | return -ENOMEM; | |
2092 | ||
2093 | ffs_ep->ep = ep; | |
2094 | ffs_ep->req = req; | |
2095 | func->eps_revmap[ds->bEndpointAddress & | |
2096 | USB_ENDPOINT_NUMBER_MASK] = idx + 1; | |
2097 | } | |
2098 | ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); | |
2099 | ||
2100 | return 0; | |
2101 | } | |
2102 | ||
ddf8abd2 MN |
2103 | static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, |
2104 | struct usb_descriptor_header *desc, | |
2105 | void *priv) | |
2106 | { | |
2107 | struct ffs_function *func = priv; | |
2108 | unsigned idx; | |
2109 | u8 newValue; | |
2110 | ||
2111 | switch (type) { | |
2112 | default: | |
2113 | case FFS_DESCRIPTOR: | |
2114 | /* Handled in previous pass by __ffs_func_bind_do_descs() */ | |
2115 | return 0; | |
2116 | ||
2117 | case FFS_INTERFACE: | |
2118 | idx = *valuep; | |
2119 | if (func->interfaces_nums[idx] < 0) { | |
2120 | int id = usb_interface_id(func->conf, &func->function); | |
2121 | if (unlikely(id < 0)) | |
2122 | return id; | |
2123 | func->interfaces_nums[idx] = id; | |
2124 | } | |
2125 | newValue = func->interfaces_nums[idx]; | |
2126 | break; | |
2127 | ||
2128 | case FFS_STRING: | |
2129 | /* String' IDs are allocated when fsf_data is bound to cdev */ | |
2130 | newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; | |
2131 | break; | |
2132 | ||
2133 | case FFS_ENDPOINT: | |
5ab54cf7 MN |
2134 | /* |
2135 | * USB_DT_ENDPOINT are handled in | |
2136 | * __ffs_func_bind_do_descs(). | |
2137 | */ | |
ddf8abd2 MN |
2138 | if (desc->bDescriptorType == USB_DT_ENDPOINT) |
2139 | return 0; | |
2140 | ||
2141 | idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; | |
2142 | if (unlikely(!func->eps[idx].ep)) | |
2143 | return -EINVAL; | |
2144 | ||
2145 | { | |
2146 | struct usb_endpoint_descriptor **descs; | |
2147 | descs = func->eps[idx].descs; | |
2148 | newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; | |
2149 | } | |
2150 | break; | |
2151 | } | |
2152 | ||
d8df0b61 | 2153 | pr_vdebug("%02x -> %02x", *valuep, newValue); |
ddf8abd2 MN |
2154 | *valuep = newValue; |
2155 | return 0; | |
2156 | } | |
2157 | ||
2158 | static int ffs_func_bind(struct usb_configuration *c, | |
2159 | struct usb_function *f) | |
2160 | { | |
2161 | struct ffs_function *func = ffs_func_from_usb(f); | |
2162 | struct ffs_data *ffs = func->ffs; | |
2163 | ||
2164 | const int full = !!func->ffs->fs_descs_count; | |
2165 | const int high = gadget_is_dualspeed(func->gadget) && | |
2166 | func->ffs->hs_descs_count; | |
2167 | ||
2168 | int ret; | |
2169 | ||
2170 | /* Make it a single chunk, less management later on */ | |
2171 | struct { | |
2172 | struct ffs_ep eps[ffs->eps_count]; | |
2173 | struct usb_descriptor_header | |
2174 | *fs_descs[full ? ffs->fs_descs_count + 1 : 0]; | |
2175 | struct usb_descriptor_header | |
2176 | *hs_descs[high ? ffs->hs_descs_count + 1 : 0]; | |
2177 | short inums[ffs->interfaces_count]; | |
2178 | char raw_descs[high ? ffs->raw_descs_length | |
2179 | : ffs->raw_fs_descs_length]; | |
2180 | } *data; | |
2181 | ||
2182 | ENTER(); | |
2183 | ||
2184 | /* Only high speed but not supported by gadget? */ | |
2185 | if (unlikely(!(full | high))) | |
2186 | return -ENOTSUPP; | |
2187 | ||
2188 | /* Allocate */ | |
2189 | data = kmalloc(sizeof *data, GFP_KERNEL); | |
2190 | if (unlikely(!data)) | |
2191 | return -ENOMEM; | |
2192 | ||
2193 | /* Zero */ | |
2194 | memset(data->eps, 0, sizeof data->eps); | |
2195 | memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs); | |
2196 | memset(data->inums, 0xff, sizeof data->inums); | |
2197 | for (ret = ffs->eps_count; ret; --ret) | |
2198 | data->eps[ret].num = -1; | |
2199 | ||
2200 | /* Save pointers */ | |
2201 | func->eps = data->eps; | |
2202 | func->interfaces_nums = data->inums; | |
2203 | ||
5ab54cf7 MN |
2204 | /* |
2205 | * Go through all the endpoint descriptors and allocate | |
ddf8abd2 | 2206 | * endpoints first, so that later we can rewrite the endpoint |
5ab54cf7 MN |
2207 | * numbers without worrying that it may be described later on. |
2208 | */ | |
ddf8abd2 MN |
2209 | if (likely(full)) { |
2210 | func->function.descriptors = data->fs_descs; | |
2211 | ret = ffs_do_descs(ffs->fs_descs_count, | |
2212 | data->raw_descs, | |
2213 | sizeof data->raw_descs, | |
2214 | __ffs_func_bind_do_descs, func); | |
2215 | if (unlikely(ret < 0)) | |
2216 | goto error; | |
2217 | } else { | |
2218 | ret = 0; | |
2219 | } | |
2220 | ||
2221 | if (likely(high)) { | |
2222 | func->function.hs_descriptors = data->hs_descs; | |
2223 | ret = ffs_do_descs(ffs->hs_descs_count, | |
2224 | data->raw_descs + ret, | |
2225 | (sizeof data->raw_descs) - ret, | |
2226 | __ffs_func_bind_do_descs, func); | |
2227 | } | |
2228 | ||
5ab54cf7 MN |
2229 | /* |
2230 | * Now handle interface numbers allocation and interface and | |
2231 | * endpoint numbers rewriting. We can do that in one go | |
2232 | * now. | |
2233 | */ | |
ddf8abd2 MN |
2234 | ret = ffs_do_descs(ffs->fs_descs_count + |
2235 | (high ? ffs->hs_descs_count : 0), | |
2236 | data->raw_descs, sizeof data->raw_descs, | |
2237 | __ffs_func_bind_do_nums, func); | |
2238 | if (unlikely(ret < 0)) | |
2239 | goto error; | |
2240 | ||
2241 | /* And we're done */ | |
2242 | ffs_event_add(ffs, FUNCTIONFS_BIND); | |
2243 | return 0; | |
2244 | ||
2245 | error: | |
2246 | /* XXX Do we need to release all claimed endpoints here? */ | |
2247 | return ret; | |
2248 | } | |
2249 | ||
2250 | ||
2251 | /* Other USB function hooks *************************************************/ | |
2252 | ||
2253 | static void ffs_func_unbind(struct usb_configuration *c, | |
2254 | struct usb_function *f) | |
2255 | { | |
2256 | struct ffs_function *func = ffs_func_from_usb(f); | |
2257 | struct ffs_data *ffs = func->ffs; | |
2258 | ||
2259 | ENTER(); | |
2260 | ||
2261 | if (ffs->func == func) { | |
2262 | ffs_func_eps_disable(func); | |
2263 | ffs->func = NULL; | |
2264 | } | |
2265 | ||
2266 | ffs_event_add(ffs, FUNCTIONFS_UNBIND); | |
2267 | ||
2268 | ffs_func_free(func); | |
2269 | } | |
2270 | ||
ddf8abd2 MN |
2271 | static int ffs_func_set_alt(struct usb_function *f, |
2272 | unsigned interface, unsigned alt) | |
2273 | { | |
2274 | struct ffs_function *func = ffs_func_from_usb(f); | |
2275 | struct ffs_data *ffs = func->ffs; | |
2276 | int ret = 0, intf; | |
2277 | ||
2278 | if (alt != (unsigned)-1) { | |
2279 | intf = ffs_func_revmap_intf(func, interface); | |
2280 | if (unlikely(intf < 0)) | |
2281 | return intf; | |
2282 | } | |
2283 | ||
2284 | if (ffs->func) | |
2285 | ffs_func_eps_disable(ffs->func); | |
2286 | ||
2287 | if (ffs->state != FFS_ACTIVE) | |
2288 | return -ENODEV; | |
2289 | ||
2290 | if (alt == (unsigned)-1) { | |
2291 | ffs->func = NULL; | |
2292 | ffs_event_add(ffs, FUNCTIONFS_DISABLE); | |
2293 | return 0; | |
2294 | } | |
2295 | ||
2296 | ffs->func = func; | |
2297 | ret = ffs_func_eps_enable(func); | |
2298 | if (likely(ret >= 0)) | |
2299 | ffs_event_add(ffs, FUNCTIONFS_ENABLE); | |
2300 | return ret; | |
2301 | } | |
2302 | ||
2303 | static void ffs_func_disable(struct usb_function *f) | |
2304 | { | |
2305 | ffs_func_set_alt(f, 0, (unsigned)-1); | |
2306 | } | |
2307 | ||
2308 | static int ffs_func_setup(struct usb_function *f, | |
2309 | const struct usb_ctrlrequest *creq) | |
2310 | { | |
2311 | struct ffs_function *func = ffs_func_from_usb(f); | |
2312 | struct ffs_data *ffs = func->ffs; | |
2313 | unsigned long flags; | |
2314 | int ret; | |
2315 | ||
2316 | ENTER(); | |
2317 | ||
d8df0b61 MN |
2318 | pr_vdebug("creq->bRequestType = %02x", creq->bRequestType); |
2319 | pr_vdebug("creq->bRequest = %02x", creq->bRequest); | |
2320 | pr_vdebug("creq->wValue = %04x", le16_to_cpu(creq->wValue)); | |
2321 | pr_vdebug("creq->wIndex = %04x", le16_to_cpu(creq->wIndex)); | |
2322 | pr_vdebug("creq->wLength = %04x", le16_to_cpu(creq->wLength)); | |
ddf8abd2 | 2323 | |
5ab54cf7 MN |
2324 | /* |
2325 | * Most requests directed to interface go through here | |
ddf8abd2 MN |
2326 | * (notable exceptions are set/get interface) so we need to |
2327 | * handle them. All other either handled by composite or | |
2328 | * passed to usb_configuration->setup() (if one is set). No | |
2329 | * matter, we will handle requests directed to endpoint here | |
2330 | * as well (as it's straightforward) but what to do with any | |
5ab54cf7 MN |
2331 | * other request? |
2332 | */ | |
ddf8abd2 MN |
2333 | if (ffs->state != FFS_ACTIVE) |
2334 | return -ENODEV; | |
2335 | ||
2336 | switch (creq->bRequestType & USB_RECIP_MASK) { | |
2337 | case USB_RECIP_INTERFACE: | |
2338 | ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); | |
2339 | if (unlikely(ret < 0)) | |
2340 | return ret; | |
2341 | break; | |
2342 | ||
2343 | case USB_RECIP_ENDPOINT: | |
2344 | ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); | |
2345 | if (unlikely(ret < 0)) | |
2346 | return ret; | |
2347 | break; | |
2348 | ||
2349 | default: | |
2350 | return -EOPNOTSUPP; | |
2351 | } | |
2352 | ||
2353 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); | |
2354 | ffs->ev.setup = *creq; | |
2355 | ffs->ev.setup.wIndex = cpu_to_le16(ret); | |
2356 | __ffs_event_add(ffs, FUNCTIONFS_SETUP); | |
2357 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); | |
2358 | ||
2359 | return 0; | |
2360 | } | |
2361 | ||
2362 | static void ffs_func_suspend(struct usb_function *f) | |
2363 | { | |
2364 | ENTER(); | |
2365 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); | |
2366 | } | |
2367 | ||
2368 | static void ffs_func_resume(struct usb_function *f) | |
2369 | { | |
2370 | ENTER(); | |
2371 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); | |
2372 | } | |
2373 | ||
2374 | ||
5ab54cf7 | 2375 | /* Endpoint and interface numbers reverse mapping ***************************/ |
ddf8abd2 MN |
2376 | |
2377 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) | |
2378 | { | |
2379 | num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; | |
2380 | return num ? num : -EDOM; | |
2381 | } | |
2382 | ||
2383 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf) | |
2384 | { | |
2385 | short *nums = func->interfaces_nums; | |
2386 | unsigned count = func->ffs->interfaces_count; | |
2387 | ||
2388 | for (; count; --count, ++nums) { | |
2389 | if (*nums >= 0 && *nums == intf) | |
2390 | return nums - func->interfaces_nums; | |
2391 | } | |
2392 | ||
2393 | return -EDOM; | |
2394 | } | |
2395 | ||
2396 | ||
2397 | /* Misc helper functions ****************************************************/ | |
2398 | ||
2399 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) | |
2400 | { | |
2401 | return nonblock | |
2402 | ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN | |
2403 | : mutex_lock_interruptible(mutex); | |
2404 | } | |
2405 | ||
ddf8abd2 MN |
2406 | static char *ffs_prepare_buffer(const char * __user buf, size_t len) |
2407 | { | |
2408 | char *data; | |
2409 | ||
2410 | if (unlikely(!len)) | |
2411 | return NULL; | |
2412 | ||
2413 | data = kmalloc(len, GFP_KERNEL); | |
2414 | if (unlikely(!data)) | |
2415 | return ERR_PTR(-ENOMEM); | |
2416 | ||
2417 | if (unlikely(__copy_from_user(data, buf, len))) { | |
2418 | kfree(data); | |
2419 | return ERR_PTR(-EFAULT); | |
2420 | } | |
2421 | ||
d8df0b61 | 2422 | pr_vdebug("Buffer from user space:"); |
ddf8abd2 MN |
2423 | ffs_dump_mem("", data, len); |
2424 | ||
2425 | return data; | |
2426 | } |