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