blob: 8d318eaaaf2042893216aa50b192b43b7bf1443d [file] [log] [blame]
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002 * f_fs.c -- user mode file system API for USB composite function controllers
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003 *
4 * Copyright (C) 2010 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01005 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02006 *
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01007 * Based on inode.c (GadgetFS) which was:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02008 * 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.
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020015 */
16
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/blkdev.h>
Randy Dunlapb0608692010-05-10 10:51:36 -070022#include <linux/pagemap.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040023#include <linux/export.h>
Koen Beel560f1182012-05-30 20:43:37 +020024#include <linux/hid.h>
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010025#include <linux/module.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020026#include <asm/unaligned.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020027
28#include <linux/usb/composite.h>
29#include <linux/usb/functionfs.h>
30
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010031#include "u_fs.h"
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020032
33#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
34
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +010035/* Variable Length Array Macros **********************************************/
36#define vla_group(groupname) size_t groupname##__next = 0
37#define vla_group_size(groupname) groupname##__next
38
39#define vla_item(groupname, type, name, n) \
40 size_t groupname##_##name##__offset = ({ \
41 size_t align_mask = __alignof__(type) - 1; \
42 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
43 size_t size = (n) * sizeof(type); \
44 groupname##__next = offset + size; \
45 offset; \
46 })
47
48#define vla_item_with_sz(groupname, type, name, n) \
49 size_t groupname##_##name##__sz = (n) * sizeof(type); \
50 size_t groupname##_##name##__offset = ({ \
51 size_t align_mask = __alignof__(type) - 1; \
52 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
53 size_t size = groupname##_##name##__sz; \
54 groupname##__next = offset + size; \
55 offset; \
56 })
57
58#define vla_ptr(ptr, groupname, name) \
59 ((void *) ((char *)ptr + groupname##_##name##__offset))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020060
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020061/* Reference counter handling */
62static void ffs_data_get(struct ffs_data *ffs);
63static void ffs_data_put(struct ffs_data *ffs);
64/* Creates new ffs_data object. */
65static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
66
67/* Opened counter handling. */
68static void ffs_data_opened(struct ffs_data *ffs);
69static void ffs_data_closed(struct ffs_data *ffs);
70
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010071/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020072static int __must_check
73__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
74static int __must_check
75__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
76
77
78/* The function structure ***************************************************/
79
80struct ffs_ep;
81
82struct ffs_function {
83 struct usb_configuration *conf;
84 struct usb_gadget *gadget;
85 struct ffs_data *ffs;
86
87 struct ffs_ep *eps;
88 u8 eps_revmap[16];
89 short *interfaces_nums;
90
91 struct usb_function function;
92};
93
94
95static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
96{
97 return container_of(f, struct ffs_function, function);
98}
99
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100100#ifdef USB_FFS_INCLUDED
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200101static void ffs_func_free(struct ffs_function *func);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100102#endif
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200103
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200104static void ffs_func_eps_disable(struct ffs_function *func);
105static int __must_check ffs_func_eps_enable(struct ffs_function *func);
106
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107static int ffs_func_bind(struct usb_configuration *,
108 struct usb_function *);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100109#ifdef USB_FFS_INCLUDED
110static void old_ffs_func_unbind(struct usb_configuration *,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200111 struct usb_function *);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100112#endif
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200113static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
114static void ffs_func_disable(struct usb_function *);
115static int ffs_func_setup(struct usb_function *,
116 const struct usb_ctrlrequest *);
117static void ffs_func_suspend(struct usb_function *);
118static void ffs_func_resume(struct usb_function *);
119
120
121static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
122static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
123
124
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200125/* The endpoints structures *************************************************/
126
127struct ffs_ep {
128 struct usb_ep *ep; /* P: ffs->eps_lock */
129 struct usb_request *req; /* P: epfile->mutex */
130
131 /* [0]: full speed, [1]: high speed */
132 struct usb_endpoint_descriptor *descs[2];
133
134 u8 num;
135
136 int status; /* P: epfile->mutex */
137};
138
139struct ffs_epfile {
140 /* Protects ep->ep and ep->req. */
141 struct mutex mutex;
142 wait_queue_head_t wait;
143
144 struct ffs_data *ffs;
145 struct ffs_ep *ep; /* P: ffs->eps_lock */
146
147 struct dentry *dentry;
148
149 char name[5];
150
151 unsigned char in; /* P: ffs->eps_lock */
152 unsigned char isoc; /* P: ffs->eps_lock */
153
154 unsigned char _pad;
155};
156
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200157static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
158static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
159
160static struct inode *__must_check
161ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
162 const struct file_operations *fops,
163 struct dentry **dentry_p);
164
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100165/* Devices management *******************************************************/
166
167DEFINE_MUTEX(ffs_lock);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100168#ifndef USB_FFS_INCLUDED
169EXPORT_SYMBOL(ffs_lock);
170#endif
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100171
172static struct ffs_dev *ffs_find_dev(const char *name);
173static void *ffs_acquire_dev(const char *dev_name);
174static void ffs_release_dev(struct ffs_data *ffs_data);
175static int ffs_ready(struct ffs_data *ffs);
176static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200177
178/* Misc helper functions ****************************************************/
179
180static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
181 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400182static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200183 __attribute__((warn_unused_result, nonnull));
184
185
186/* Control file aka ep0 *****************************************************/
187
188static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
189{
190 struct ffs_data *ffs = req->context;
191
192 complete_all(&ffs->ep0req_completion);
193}
194
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200195static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
196{
197 struct usb_request *req = ffs->ep0req;
198 int ret;
199
200 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
201
202 spin_unlock_irq(&ffs->ev.waitq.lock);
203
204 req->buf = data;
205 req->length = len;
206
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100207 /*
208 * UDC layer requires to provide a buffer even for ZLP, but should
209 * not use it at all. Let's provide some poisoned pointer to catch
210 * possible bug in the driver.
211 */
212 if (req->buf == NULL)
213 req->buf = (void *)0xDEADBABE;
214
Wolfram Sang16735d02013-11-14 14:32:02 -0800215 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200216
217 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
218 if (unlikely(ret < 0))
219 return ret;
220
221 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
222 if (unlikely(ret)) {
223 usb_ep_dequeue(ffs->gadget->ep0, req);
224 return -EINTR;
225 }
226
227 ffs->setup_state = FFS_NO_SETUP;
228 return ffs->ep0req_status;
229}
230
231static int __ffs_ep0_stall(struct ffs_data *ffs)
232{
233 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100234 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200235 usb_ep_set_halt(ffs->gadget->ep0);
236 ffs->setup_state = FFS_NO_SETUP;
237 return -EL2HLT;
238 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100239 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200240 return -ESRCH;
241 }
242}
243
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200244static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
245 size_t len, loff_t *ptr)
246{
247 struct ffs_data *ffs = file->private_data;
248 ssize_t ret;
249 char *data;
250
251 ENTER();
252
253 /* Fast check if setup was canceled */
254 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
255 return -EIDRM;
256
257 /* Acquire mutex */
258 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
259 if (unlikely(ret < 0))
260 return ret;
261
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200262 /* Check state */
263 switch (ffs->state) {
264 case FFS_READ_DESCRIPTORS:
265 case FFS_READ_STRINGS:
266 /* Copy data */
267 if (unlikely(len < 16)) {
268 ret = -EINVAL;
269 break;
270 }
271
272 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100273 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200274 ret = PTR_ERR(data);
275 break;
276 }
277
278 /* Handle data */
279 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100280 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200281 ret = __ffs_data_got_descs(ffs, data, len);
282 if (unlikely(ret < 0))
283 break;
284
285 ffs->state = FFS_READ_STRINGS;
286 ret = len;
287 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100288 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200289 ret = __ffs_data_got_strings(ffs, data, len);
290 if (unlikely(ret < 0))
291 break;
292
293 ret = ffs_epfiles_create(ffs);
294 if (unlikely(ret)) {
295 ffs->state = FFS_CLOSING;
296 break;
297 }
298
299 ffs->state = FFS_ACTIVE;
300 mutex_unlock(&ffs->mutex);
301
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100302 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200303 if (unlikely(ret < 0)) {
304 ffs->state = FFS_CLOSING;
305 return ret;
306 }
307
308 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
309 return len;
310 }
311 break;
312
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200313 case FFS_ACTIVE:
314 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100315 /*
316 * We're called from user space, we can use _irq
317 * rather then _irqsave
318 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200319 spin_lock_irq(&ffs->ev.waitq.lock);
320 switch (FFS_SETUP_STATE(ffs)) {
321 case FFS_SETUP_CANCELED:
322 ret = -EIDRM;
323 goto done_spin;
324
325 case FFS_NO_SETUP:
326 ret = -ESRCH;
327 goto done_spin;
328
329 case FFS_SETUP_PENDING:
330 break;
331 }
332
333 /* FFS_SETUP_PENDING */
334 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
335 spin_unlock_irq(&ffs->ev.waitq.lock);
336 ret = __ffs_ep0_stall(ffs);
337 break;
338 }
339
340 /* FFS_SETUP_PENDING and not stall */
341 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
342
343 spin_unlock_irq(&ffs->ev.waitq.lock);
344
345 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100346 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200347 ret = PTR_ERR(data);
348 break;
349 }
350
351 spin_lock_irq(&ffs->ev.waitq.lock);
352
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100353 /*
354 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200355 * but the state of setup could have changed from
356 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
357 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100358 * from user space in vain but it's unlikely.
359 *
360 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200361 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
362 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100363 * mutex.
364 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200365 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
366 ret = -EIDRM;
367done_spin:
368 spin_unlock_irq(&ffs->ev.waitq.lock);
369 } else {
370 /* unlocks spinlock */
371 ret = __ffs_ep0_queue_wait(ffs, data, len);
372 }
373 kfree(data);
374 break;
375
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200376 default:
377 ret = -EBADFD;
378 break;
379 }
380
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200381 mutex_unlock(&ffs->mutex);
382 return ret;
383}
384
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200385static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
386 size_t n)
387{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100388 /*
389 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
390 * to release them.
391 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200392 struct usb_functionfs_event events[n];
393 unsigned i = 0;
394
395 memset(events, 0, sizeof events);
396
397 do {
398 events[i].type = ffs->ev.types[i];
399 if (events[i].type == FUNCTIONFS_SETUP) {
400 events[i].u.setup = ffs->ev.setup;
401 ffs->setup_state = FFS_SETUP_PENDING;
402 }
403 } while (++i < n);
404
405 if (n < ffs->ev.count) {
406 ffs->ev.count -= n;
407 memmove(ffs->ev.types, ffs->ev.types + n,
408 ffs->ev.count * sizeof *ffs->ev.types);
409 } else {
410 ffs->ev.count = 0;
411 }
412
413 spin_unlock_irq(&ffs->ev.waitq.lock);
414 mutex_unlock(&ffs->mutex);
415
416 return unlikely(__copy_to_user(buf, events, sizeof events))
417 ? -EFAULT : sizeof events;
418}
419
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200420static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
421 size_t len, loff_t *ptr)
422{
423 struct ffs_data *ffs = file->private_data;
424 char *data = NULL;
425 size_t n;
426 int ret;
427
428 ENTER();
429
430 /* Fast check if setup was canceled */
431 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
432 return -EIDRM;
433
434 /* Acquire mutex */
435 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
436 if (unlikely(ret < 0))
437 return ret;
438
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200439 /* Check state */
440 if (ffs->state != FFS_ACTIVE) {
441 ret = -EBADFD;
442 goto done_mutex;
443 }
444
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100445 /*
446 * We're called from user space, we can use _irq rather then
447 * _irqsave
448 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200449 spin_lock_irq(&ffs->ev.waitq.lock);
450
451 switch (FFS_SETUP_STATE(ffs)) {
452 case FFS_SETUP_CANCELED:
453 ret = -EIDRM;
454 break;
455
456 case FFS_NO_SETUP:
457 n = len / sizeof(struct usb_functionfs_event);
458 if (unlikely(!n)) {
459 ret = -EINVAL;
460 break;
461 }
462
463 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
464 ret = -EAGAIN;
465 break;
466 }
467
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100468 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
469 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200470 ret = -EINTR;
471 break;
472 }
473
474 return __ffs_ep0_read_events(ffs, buf,
475 min(n, (size_t)ffs->ev.count));
476
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200477 case FFS_SETUP_PENDING:
478 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
479 spin_unlock_irq(&ffs->ev.waitq.lock);
480 ret = __ffs_ep0_stall(ffs);
481 goto done_mutex;
482 }
483
484 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
485
486 spin_unlock_irq(&ffs->ev.waitq.lock);
487
488 if (likely(len)) {
489 data = kmalloc(len, GFP_KERNEL);
490 if (unlikely(!data)) {
491 ret = -ENOMEM;
492 goto done_mutex;
493 }
494 }
495
496 spin_lock_irq(&ffs->ev.waitq.lock);
497
498 /* See ffs_ep0_write() */
499 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
500 ret = -EIDRM;
501 break;
502 }
503
504 /* unlocks spinlock */
505 ret = __ffs_ep0_queue_wait(ffs, data, len);
506 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
507 ret = -EFAULT;
508 goto done_mutex;
509
510 default:
511 ret = -EBADFD;
512 break;
513 }
514
515 spin_unlock_irq(&ffs->ev.waitq.lock);
516done_mutex:
517 mutex_unlock(&ffs->mutex);
518 kfree(data);
519 return ret;
520}
521
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200522static int ffs_ep0_open(struct inode *inode, struct file *file)
523{
524 struct ffs_data *ffs = inode->i_private;
525
526 ENTER();
527
528 if (unlikely(ffs->state == FFS_CLOSING))
529 return -EBUSY;
530
531 file->private_data = ffs;
532 ffs_data_opened(ffs);
533
534 return 0;
535}
536
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200537static int ffs_ep0_release(struct inode *inode, struct file *file)
538{
539 struct ffs_data *ffs = file->private_data;
540
541 ENTER();
542
543 ffs_data_closed(ffs);
544
545 return 0;
546}
547
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200548static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
549{
550 struct ffs_data *ffs = file->private_data;
551 struct usb_gadget *gadget = ffs->gadget;
552 long ret;
553
554 ENTER();
555
556 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
557 struct ffs_function *func = ffs->func;
558 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200559 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200560 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200561 } else {
562 ret = -ENOTTY;
563 }
564
565 return ret;
566}
567
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200568static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200569 .llseek = no_llseek,
570
571 .open = ffs_ep0_open,
572 .write = ffs_ep0_write,
573 .read = ffs_ep0_read,
574 .release = ffs_ep0_release,
575 .unlocked_ioctl = ffs_ep0_ioctl,
576};
577
578
579/* "Normal" endpoints operations ********************************************/
580
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200581static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
582{
583 ENTER();
584 if (likely(req->context)) {
585 struct ffs_ep *ep = _ep->driver_data;
586 ep->status = req->status ? req->status : req->actual;
587 complete(req->context);
588 }
589}
590
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200591static ssize_t ffs_epfile_io(struct file *file,
592 char __user *buf, size_t len, int read)
593{
594 struct ffs_epfile *epfile = file->private_data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800595 struct usb_gadget *gadget = epfile->ffs->gadget;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200596 struct ffs_ep *ep;
597 char *data = NULL;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800598 ssize_t ret, data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200599 int halt;
600
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800601 /* Are we still active? */
602 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
603 ret = -ENODEV;
604 goto error;
605 }
606
607 /* Wait for endpoint to be enabled */
608 ep = epfile->ep;
609 if (!ep) {
610 if (file->f_flags & O_NONBLOCK) {
611 ret = -EAGAIN;
612 goto error;
613 }
614
615 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
616 if (ret) {
617 ret = -EINTR;
618 goto error;
619 }
620 }
621
622 /* Do we halt? */
623 halt = !read == !epfile->in;
624 if (halt && epfile->isoc) {
625 ret = -EINVAL;
626 goto error;
627 }
628
629 /* Allocate & copy */
630 if (!halt) {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800631 /*
632 * Controller may require buffer size to be aligned to
633 * maxpacketsize of an out endpoint.
634 */
635 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
636
637 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800638 if (unlikely(!data))
639 return -ENOMEM;
640
641 if (!read && unlikely(copy_from_user(data, buf, len))) {
642 ret = -EFAULT;
643 goto error;
644 }
645 }
646
647 /* We will be using request */
648 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
649 if (unlikely(ret))
650 goto error;
651
652 spin_lock_irq(&epfile->ffs->eps_lock);
653
654 if (epfile->ep != ep) {
655 /* In the meantime, endpoint got disabled or changed. */
656 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200657 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800658 } else if (halt) {
659 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200660 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
661 usb_ep_set_halt(ep->ep);
662 spin_unlock_irq(&epfile->ffs->eps_lock);
663 ret = -EBADMSG;
664 } else {
665 /* Fire the request */
666 DECLARE_COMPLETION_ONSTACK(done);
667
668 struct usb_request *req = ep->req;
669 req->context = &done;
670 req->complete = ffs_epfile_io_complete;
671 req->buf = data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800672 req->length = data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200673
674 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
675
676 spin_unlock_irq(&epfile->ffs->eps_lock);
677
678 if (unlikely(ret < 0)) {
679 /* nop */
680 } else if (unlikely(wait_for_completion_interruptible(&done))) {
681 ret = -EINTR;
682 usb_ep_dequeue(ep->ep, req);
683 } else {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800684 /*
685 * XXX We may end up silently droping data here.
686 * Since data_len (i.e. req->length) may be bigger
687 * than len (after being rounded up to maxpacketsize),
688 * we may end up with more data then user space has
689 * space for.
690 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200691 ret = ep->status;
692 if (read && ret > 0 &&
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800693 unlikely(copy_to_user(buf, data,
694 min_t(size_t, ret, len))))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200695 ret = -EFAULT;
696 }
697 }
698
699 mutex_unlock(&epfile->mutex);
700error:
701 kfree(data);
702 return ret;
703}
704
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200705static ssize_t
706ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
707 loff_t *ptr)
708{
709 ENTER();
710
711 return ffs_epfile_io(file, (char __user *)buf, len, 0);
712}
713
714static ssize_t
715ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
716{
717 ENTER();
718
719 return ffs_epfile_io(file, buf, len, 1);
720}
721
722static int
723ffs_epfile_open(struct inode *inode, struct file *file)
724{
725 struct ffs_epfile *epfile = inode->i_private;
726
727 ENTER();
728
729 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
730 return -ENODEV;
731
732 file->private_data = epfile;
733 ffs_data_opened(epfile->ffs);
734
735 return 0;
736}
737
738static int
739ffs_epfile_release(struct inode *inode, struct file *file)
740{
741 struct ffs_epfile *epfile = inode->i_private;
742
743 ENTER();
744
745 ffs_data_closed(epfile->ffs);
746
747 return 0;
748}
749
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200750static long ffs_epfile_ioctl(struct file *file, unsigned code,
751 unsigned long value)
752{
753 struct ffs_epfile *epfile = file->private_data;
754 int ret;
755
756 ENTER();
757
758 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
759 return -ENODEV;
760
761 spin_lock_irq(&epfile->ffs->eps_lock);
762 if (likely(epfile->ep)) {
763 switch (code) {
764 case FUNCTIONFS_FIFO_STATUS:
765 ret = usb_ep_fifo_status(epfile->ep->ep);
766 break;
767 case FUNCTIONFS_FIFO_FLUSH:
768 usb_ep_fifo_flush(epfile->ep->ep);
769 ret = 0;
770 break;
771 case FUNCTIONFS_CLEAR_HALT:
772 ret = usb_ep_clear_halt(epfile->ep->ep);
773 break;
774 case FUNCTIONFS_ENDPOINT_REVMAP:
775 ret = epfile->ep->num;
776 break;
777 default:
778 ret = -ENOTTY;
779 }
780 } else {
781 ret = -ENODEV;
782 }
783 spin_unlock_irq(&epfile->ffs->eps_lock);
784
785 return ret;
786}
787
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200788static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200789 .llseek = no_llseek,
790
791 .open = ffs_epfile_open,
792 .write = ffs_epfile_write,
793 .read = ffs_epfile_read,
794 .release = ffs_epfile_release,
795 .unlocked_ioctl = ffs_epfile_ioctl,
796};
797
798
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200799/* File system and super block operations ***********************************/
800
801/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100802 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200803 * function configuration then later for event monitoring.
804 */
805
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200806static struct inode *__must_check
807ffs_sb_make_inode(struct super_block *sb, void *data,
808 const struct file_operations *fops,
809 const struct inode_operations *iops,
810 struct ffs_file_perms *perms)
811{
812 struct inode *inode;
813
814 ENTER();
815
816 inode = new_inode(sb);
817
818 if (likely(inode)) {
819 struct timespec current_time = CURRENT_TIME;
820
Al Viro12ba8d12010-10-27 04:19:36 +0100821 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200822 inode->i_mode = perms->mode;
823 inode->i_uid = perms->uid;
824 inode->i_gid = perms->gid;
825 inode->i_atime = current_time;
826 inode->i_mtime = current_time;
827 inode->i_ctime = current_time;
828 inode->i_private = data;
829 if (fops)
830 inode->i_fop = fops;
831 if (iops)
832 inode->i_op = iops;
833 }
834
835 return inode;
836}
837
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200838/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200839static struct inode *ffs_sb_create_file(struct super_block *sb,
840 const char *name, void *data,
841 const struct file_operations *fops,
842 struct dentry **dentry_p)
843{
844 struct ffs_data *ffs = sb->s_fs_info;
845 struct dentry *dentry;
846 struct inode *inode;
847
848 ENTER();
849
850 dentry = d_alloc_name(sb->s_root, name);
851 if (unlikely(!dentry))
852 return NULL;
853
854 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
855 if (unlikely(!inode)) {
856 dput(dentry);
857 return NULL;
858 }
859
860 d_add(dentry, inode);
861 if (dentry_p)
862 *dentry_p = dentry;
863
864 return inode;
865}
866
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200867/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200868static const struct super_operations ffs_sb_operations = {
869 .statfs = simple_statfs,
870 .drop_inode = generic_delete_inode,
871};
872
873struct ffs_sb_fill_data {
874 struct ffs_file_perms perms;
875 umode_t root_mode;
876 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +0100877 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200878};
879
880static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
881{
882 struct ffs_sb_fill_data *data = _data;
883 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +0100884 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200885
886 ENTER();
887
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200888 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +0100889 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200890 sb->s_fs_info = ffs;
891 sb->s_blocksize = PAGE_CACHE_SIZE;
892 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
893 sb->s_magic = FUNCTIONFS_MAGIC;
894 sb->s_op = &ffs_sb_operations;
895 sb->s_time_gran = 1;
896
897 /* Root inode */
898 data->perms.mode = data->root_mode;
899 inode = ffs_sb_make_inode(sb, NULL,
900 &simple_dir_operations,
901 &simple_dir_inode_operations,
902 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -0500903 sb->s_root = d_make_root(inode);
904 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +0100905 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200906
907 /* EP0 file */
908 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
909 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +0100910 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200911
912 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200913}
914
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200915static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
916{
917 ENTER();
918
919 if (!opts || !*opts)
920 return 0;
921
922 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200923 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100924 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200925
926 /* Option limit */
927 comma = strchr(opts, ',');
928 if (comma)
929 *comma = 0;
930
931 /* Value limit */
932 eq = strchr(opts, '=');
933 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100934 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200935 return -EINVAL;
936 }
937 *eq = 0;
938
939 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100940 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100941 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200942 return -EINVAL;
943 }
944
945 /* Interpret option */
946 switch (eq - opts) {
947 case 5:
948 if (!memcmp(opts, "rmode", 5))
949 data->root_mode = (value & 0555) | S_IFDIR;
950 else if (!memcmp(opts, "fmode", 5))
951 data->perms.mode = (value & 0666) | S_IFREG;
952 else
953 goto invalid;
954 break;
955
956 case 4:
957 if (!memcmp(opts, "mode", 4)) {
958 data->root_mode = (value & 0555) | S_IFDIR;
959 data->perms.mode = (value & 0666) | S_IFREG;
960 } else {
961 goto invalid;
962 }
963 break;
964
965 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700966 if (!memcmp(opts, "uid", 3)) {
967 data->perms.uid = make_kuid(current_user_ns(), value);
968 if (!uid_valid(data->perms.uid)) {
969 pr_err("%s: unmapped value: %lu\n", opts, value);
970 return -EINVAL;
971 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800972 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700973 data->perms.gid = make_kgid(current_user_ns(), value);
974 if (!gid_valid(data->perms.gid)) {
975 pr_err("%s: unmapped value: %lu\n", opts, value);
976 return -EINVAL;
977 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800978 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200979 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -0800980 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200981 break;
982
983 default:
984invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100985 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200986 return -EINVAL;
987 }
988
989 /* Next iteration */
990 if (!comma)
991 break;
992 opts = comma + 1;
993 }
994
995 return 0;
996}
997
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200998/* "mount -t functionfs dev_name /dev/function" ends up here */
999
Al Virofc14f2f2010-07-25 01:48:30 +04001000static struct dentry *
1001ffs_fs_mount(struct file_system_type *t, int flags,
1002 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001003{
1004 struct ffs_sb_fill_data data = {
1005 .perms = {
1006 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001007 .uid = GLOBAL_ROOT_UID,
1008 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001009 },
1010 .root_mode = S_IFDIR | 0500,
1011 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001012 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001013 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001014 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001015 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001016
1017 ENTER();
1018
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001019 ret = ffs_fs_parse_opts(&data, opts);
1020 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001021 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001022
Al Viro2606b282013-09-20 17:14:21 +01001023 ffs = ffs_data_new();
1024 if (unlikely(!ffs))
1025 return ERR_PTR(-ENOMEM);
1026 ffs->file_perms = data.perms;
1027
1028 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1029 if (unlikely(!ffs->dev_name)) {
1030 ffs_data_put(ffs);
1031 return ERR_PTR(-ENOMEM);
1032 }
1033
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001034 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001035 if (IS_ERR(ffs_dev)) {
1036 ffs_data_put(ffs);
1037 return ERR_CAST(ffs_dev);
1038 }
1039 ffs->private_data = ffs_dev;
1040 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001041
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001042 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001043 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001044 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001045 ffs_data_put(data.ffs_data);
1046 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001047 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001048}
1049
1050static void
1051ffs_fs_kill_sb(struct super_block *sb)
1052{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001053 ENTER();
1054
1055 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001056 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001057 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001058 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001059 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001060}
1061
1062static struct file_system_type ffs_fs_type = {
1063 .owner = THIS_MODULE,
1064 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001065 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001066 .kill_sb = ffs_fs_kill_sb,
1067};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001068MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001069
1070
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001071/* Driver's main init/cleanup functions *************************************/
1072
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001073static int functionfs_init(void)
1074{
1075 int ret;
1076
1077 ENTER();
1078
1079 ret = register_filesystem(&ffs_fs_type);
1080 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001081 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001082 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001083 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001084
1085 return ret;
1086}
1087
1088static void functionfs_cleanup(void)
1089{
1090 ENTER();
1091
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001092 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001093 unregister_filesystem(&ffs_fs_type);
1094}
1095
1096
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001097/* ffs_data and ffs_function construction and destruction code **************/
1098
1099static void ffs_data_clear(struct ffs_data *ffs);
1100static void ffs_data_reset(struct ffs_data *ffs);
1101
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001102static void ffs_data_get(struct ffs_data *ffs)
1103{
1104 ENTER();
1105
1106 atomic_inc(&ffs->ref);
1107}
1108
1109static void ffs_data_opened(struct ffs_data *ffs)
1110{
1111 ENTER();
1112
1113 atomic_inc(&ffs->ref);
1114 atomic_inc(&ffs->opened);
1115}
1116
1117static void ffs_data_put(struct ffs_data *ffs)
1118{
1119 ENTER();
1120
1121 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001122 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001123 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001124 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001125 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001126 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001127 kfree(ffs);
1128 }
1129}
1130
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001131static void ffs_data_closed(struct ffs_data *ffs)
1132{
1133 ENTER();
1134
1135 if (atomic_dec_and_test(&ffs->opened)) {
1136 ffs->state = FFS_CLOSING;
1137 ffs_data_reset(ffs);
1138 }
1139
1140 ffs_data_put(ffs);
1141}
1142
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001143static struct ffs_data *ffs_data_new(void)
1144{
1145 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1146 if (unlikely(!ffs))
1147 return 0;
1148
1149 ENTER();
1150
1151 atomic_set(&ffs->ref, 1);
1152 atomic_set(&ffs->opened, 0);
1153 ffs->state = FFS_READ_DESCRIPTORS;
1154 mutex_init(&ffs->mutex);
1155 spin_lock_init(&ffs->eps_lock);
1156 init_waitqueue_head(&ffs->ev.waitq);
1157 init_completion(&ffs->ep0req_completion);
1158
1159 /* XXX REVISIT need to update it in some places, or do we? */
1160 ffs->ev.can_stall = 1;
1161
1162 return ffs;
1163}
1164
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001165static void ffs_data_clear(struct ffs_data *ffs)
1166{
1167 ENTER();
1168
1169 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001170 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001171
1172 BUG_ON(ffs->gadget);
1173
1174 if (ffs->epfiles)
1175 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1176
1177 kfree(ffs->raw_descs);
1178 kfree(ffs->raw_strings);
1179 kfree(ffs->stringtabs);
1180}
1181
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001182static void ffs_data_reset(struct ffs_data *ffs)
1183{
1184 ENTER();
1185
1186 ffs_data_clear(ffs);
1187
1188 ffs->epfiles = NULL;
1189 ffs->raw_descs = NULL;
1190 ffs->raw_strings = NULL;
1191 ffs->stringtabs = NULL;
1192
1193 ffs->raw_descs_length = 0;
1194 ffs->raw_fs_descs_length = 0;
1195 ffs->fs_descs_count = 0;
1196 ffs->hs_descs_count = 0;
1197
1198 ffs->strings_count = 0;
1199 ffs->interfaces_count = 0;
1200 ffs->eps_count = 0;
1201
1202 ffs->ev.count = 0;
1203
1204 ffs->state = FFS_READ_DESCRIPTORS;
1205 ffs->setup_state = FFS_NO_SETUP;
1206 ffs->flags = 0;
1207}
1208
1209
1210static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1211{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001212 struct usb_gadget_strings **lang;
1213 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001214
1215 ENTER();
1216
1217 if (WARN_ON(ffs->state != FFS_ACTIVE
1218 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1219 return -EBADFD;
1220
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001221 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1222 if (unlikely(first_id < 0))
1223 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001224
1225 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1226 if (unlikely(!ffs->ep0req))
1227 return -ENOMEM;
1228 ffs->ep0req->complete = ffs_ep0_complete;
1229 ffs->ep0req->context = ffs;
1230
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001231 lang = ffs->stringtabs;
1232 for (lang = ffs->stringtabs; *lang; ++lang) {
1233 struct usb_string *str = (*lang)->strings;
1234 int id = first_id;
1235 for (; str->s; ++id, ++str)
1236 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001237 }
1238
1239 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001240 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001241 return 0;
1242}
1243
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001244static void functionfs_unbind(struct ffs_data *ffs)
1245{
1246 ENTER();
1247
1248 if (!WARN_ON(!ffs->gadget)) {
1249 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1250 ffs->ep0req = NULL;
1251 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001252 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001253 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001254 }
1255}
1256
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001257static int ffs_epfiles_create(struct ffs_data *ffs)
1258{
1259 struct ffs_epfile *epfile, *epfiles;
1260 unsigned i, count;
1261
1262 ENTER();
1263
1264 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001265 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001266 if (!epfiles)
1267 return -ENOMEM;
1268
1269 epfile = epfiles;
1270 for (i = 1; i <= count; ++i, ++epfile) {
1271 epfile->ffs = ffs;
1272 mutex_init(&epfile->mutex);
1273 init_waitqueue_head(&epfile->wait);
1274 sprintf(epfiles->name, "ep%u", i);
1275 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1276 &ffs_epfile_operations,
1277 &epfile->dentry))) {
1278 ffs_epfiles_destroy(epfiles, i - 1);
1279 return -ENOMEM;
1280 }
1281 }
1282
1283 ffs->epfiles = epfiles;
1284 return 0;
1285}
1286
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001287static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1288{
1289 struct ffs_epfile *epfile = epfiles;
1290
1291 ENTER();
1292
1293 for (; count; --count, ++epfile) {
1294 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1295 waitqueue_active(&epfile->wait));
1296 if (epfile->dentry) {
1297 d_delete(epfile->dentry);
1298 dput(epfile->dentry);
1299 epfile->dentry = NULL;
1300 }
1301 }
1302
1303 kfree(epfiles);
1304}
1305
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001306#ifdef USB_FFS_INCLUDED
1307
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001308static int functionfs_bind_config(struct usb_composite_dev *cdev,
1309 struct usb_configuration *c,
1310 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001311{
1312 struct ffs_function *func;
1313 int ret;
1314
1315 ENTER();
1316
1317 func = kzalloc(sizeof *func, GFP_KERNEL);
1318 if (unlikely(!func))
1319 return -ENOMEM;
1320
1321 func->function.name = "Function FS Gadget";
1322 func->function.strings = ffs->stringtabs;
1323
1324 func->function.bind = ffs_func_bind;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001325 func->function.unbind = old_ffs_func_unbind;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001326 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001327 func->function.disable = ffs_func_disable;
1328 func->function.setup = ffs_func_setup;
1329 func->function.suspend = ffs_func_suspend;
1330 func->function.resume = ffs_func_resume;
1331
1332 func->conf = c;
1333 func->gadget = cdev->gadget;
1334 func->ffs = ffs;
1335 ffs_data_get(ffs);
1336
1337 ret = usb_add_function(c, &func->function);
1338 if (unlikely(ret))
1339 ffs_func_free(func);
1340
1341 return ret;
1342}
1343
1344static void ffs_func_free(struct ffs_function *func)
1345{
Peter Korsgaard4f065392012-05-03 12:58:49 +02001346 struct ffs_ep *ep = func->eps;
1347 unsigned count = func->ffs->eps_count;
1348 unsigned long flags;
1349
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001350 ENTER();
1351
Peter Korsgaard4f065392012-05-03 12:58:49 +02001352 /* cleanup after autoconfig */
1353 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1354 do {
1355 if (ep->ep && ep->req)
1356 usb_ep_free_request(ep->ep, ep->req);
1357 ep->req = NULL;
1358 ++ep;
1359 } while (--count);
1360 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1361
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001362 ffs_data_put(func->ffs);
1363
1364 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001365 /*
1366 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001367 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001368 * in the same chunk.
1369 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001370
1371 kfree(func);
1372}
1373
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001374#endif
1375
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001376static void ffs_func_eps_disable(struct ffs_function *func)
1377{
1378 struct ffs_ep *ep = func->eps;
1379 struct ffs_epfile *epfile = func->ffs->epfiles;
1380 unsigned count = func->ffs->eps_count;
1381 unsigned long flags;
1382
1383 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1384 do {
1385 /* pending requests get nuked */
1386 if (likely(ep->ep))
1387 usb_ep_disable(ep->ep);
1388 epfile->ep = NULL;
1389
1390 ++ep;
1391 ++epfile;
1392 } while (--count);
1393 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1394}
1395
1396static int ffs_func_eps_enable(struct ffs_function *func)
1397{
1398 struct ffs_data *ffs = func->ffs;
1399 struct ffs_ep *ep = func->eps;
1400 struct ffs_epfile *epfile = ffs->epfiles;
1401 unsigned count = ffs->eps_count;
1402 unsigned long flags;
1403 int ret = 0;
1404
1405 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1406 do {
1407 struct usb_endpoint_descriptor *ds;
1408 ds = ep->descs[ep->descs[1] ? 1 : 0];
1409
1410 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001411 ep->ep->desc = ds;
1412 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001413 if (likely(!ret)) {
1414 epfile->ep = ep;
1415 epfile->in = usb_endpoint_dir_in(ds);
1416 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1417 } else {
1418 break;
1419 }
1420
1421 wake_up(&epfile->wait);
1422
1423 ++ep;
1424 ++epfile;
1425 } while (--count);
1426 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1427
1428 return ret;
1429}
1430
1431
1432/* Parsing and building descriptors and strings *****************************/
1433
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001434/*
1435 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001436 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001437 * required by given configuration. Returns address after the
1438 * descriptor or NULL if data is invalid.
1439 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001440
1441enum ffs_entity_type {
1442 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1443};
1444
1445typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1446 u8 *valuep,
1447 struct usb_descriptor_header *desc,
1448 void *priv);
1449
1450static int __must_check ffs_do_desc(char *data, unsigned len,
1451 ffs_entity_callback entity, void *priv)
1452{
1453 struct usb_descriptor_header *_ds = (void *)data;
1454 u8 length;
1455 int ret;
1456
1457 ENTER();
1458
1459 /* At least two bytes are required: length and type */
1460 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001461 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001462 return -EINVAL;
1463 }
1464
1465 /* If we have at least as many bytes as the descriptor takes? */
1466 length = _ds->bLength;
1467 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001468 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001469 return -EINVAL;
1470 }
1471
1472#define __entity_check_INTERFACE(val) 1
1473#define __entity_check_STRING(val) (val)
1474#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1475#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001476 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001477 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001478 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001479 return -EINVAL; \
1480 } \
1481 ret = entity(FFS_ ##type, &val, _ds, priv); \
1482 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001483 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001484 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001485 return ret; \
1486 } \
1487 } while (0)
1488
1489 /* Parse descriptor depending on type. */
1490 switch (_ds->bDescriptorType) {
1491 case USB_DT_DEVICE:
1492 case USB_DT_CONFIG:
1493 case USB_DT_STRING:
1494 case USB_DT_DEVICE_QUALIFIER:
1495 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001496 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001497 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001498 return -EINVAL;
1499
1500 case USB_DT_INTERFACE: {
1501 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001502 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001503 if (length != sizeof *ds)
1504 goto inv_length;
1505
1506 __entity(INTERFACE, ds->bInterfaceNumber);
1507 if (ds->iInterface)
1508 __entity(STRING, ds->iInterface);
1509 }
1510 break;
1511
1512 case USB_DT_ENDPOINT: {
1513 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001514 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001515 if (length != USB_DT_ENDPOINT_SIZE &&
1516 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1517 goto inv_length;
1518 __entity(ENDPOINT, ds->bEndpointAddress);
1519 }
1520 break;
1521
Koen Beel560f1182012-05-30 20:43:37 +02001522 case HID_DT_HID:
1523 pr_vdebug("hid descriptor\n");
1524 if (length != sizeof(struct hid_descriptor))
1525 goto inv_length;
1526 break;
1527
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001528 case USB_DT_OTG:
1529 if (length != sizeof(struct usb_otg_descriptor))
1530 goto inv_length;
1531 break;
1532
1533 case USB_DT_INTERFACE_ASSOCIATION: {
1534 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001535 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001536 if (length != sizeof *ds)
1537 goto inv_length;
1538 if (ds->iFunction)
1539 __entity(STRING, ds->iFunction);
1540 }
1541 break;
1542
1543 case USB_DT_OTHER_SPEED_CONFIG:
1544 case USB_DT_INTERFACE_POWER:
1545 case USB_DT_DEBUG:
1546 case USB_DT_SECURITY:
1547 case USB_DT_CS_RADIO_CONTROL:
1548 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001549 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001550 return -EINVAL;
1551
1552 default:
1553 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001554 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001555 return -EINVAL;
1556
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001557inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001558 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001559 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001560 return -EINVAL;
1561 }
1562
1563#undef __entity
1564#undef __entity_check_DESCRIPTOR
1565#undef __entity_check_INTERFACE
1566#undef __entity_check_STRING
1567#undef __entity_check_ENDPOINT
1568
1569 return length;
1570}
1571
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001572static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1573 ffs_entity_callback entity, void *priv)
1574{
1575 const unsigned _len = len;
1576 unsigned long num = 0;
1577
1578 ENTER();
1579
1580 for (;;) {
1581 int ret;
1582
1583 if (num == count)
1584 data = NULL;
1585
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001586 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001587 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1588 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001589 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001590 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001591 return ret;
1592 }
1593
1594 if (!data)
1595 return _len - len;
1596
1597 ret = ffs_do_desc(data, len, entity, priv);
1598 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001599 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001600 return ret;
1601 }
1602
1603 len -= ret;
1604 data += ret;
1605 ++num;
1606 }
1607}
1608
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001609static int __ffs_data_do_entity(enum ffs_entity_type type,
1610 u8 *valuep, struct usb_descriptor_header *desc,
1611 void *priv)
1612{
1613 struct ffs_data *ffs = priv;
1614
1615 ENTER();
1616
1617 switch (type) {
1618 case FFS_DESCRIPTOR:
1619 break;
1620
1621 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001622 /*
1623 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001624 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001625 * "n+1" interfaces.
1626 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001627 if (*valuep >= ffs->interfaces_count)
1628 ffs->interfaces_count = *valuep + 1;
1629 break;
1630
1631 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001632 /*
1633 * Strings are indexed from 1 (0 is magic ;) reserved
1634 * for languages list or some such)
1635 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001636 if (*valuep > ffs->strings_count)
1637 ffs->strings_count = *valuep;
1638 break;
1639
1640 case FFS_ENDPOINT:
1641 /* Endpoints are indexed from 1 as well. */
1642 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1643 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1644 break;
1645 }
1646
1647 return 0;
1648}
1649
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001650static int __ffs_data_got_descs(struct ffs_data *ffs,
1651 char *const _data, size_t len)
1652{
1653 unsigned fs_count, hs_count;
1654 int fs_len, ret = -EINVAL;
1655 char *data = _data;
1656
1657 ENTER();
1658
1659 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1660 get_unaligned_le32(data + 4) != len))
1661 goto error;
1662 fs_count = get_unaligned_le32(data + 8);
1663 hs_count = get_unaligned_le32(data + 12);
1664
1665 if (!fs_count && !hs_count)
1666 goto einval;
1667
1668 data += 16;
1669 len -= 16;
1670
1671 if (likely(fs_count)) {
1672 fs_len = ffs_do_descs(fs_count, data, len,
1673 __ffs_data_do_entity, ffs);
1674 if (unlikely(fs_len < 0)) {
1675 ret = fs_len;
1676 goto error;
1677 }
1678
1679 data += fs_len;
1680 len -= fs_len;
1681 } else {
1682 fs_len = 0;
1683 }
1684
1685 if (likely(hs_count)) {
1686 ret = ffs_do_descs(hs_count, data, len,
1687 __ffs_data_do_entity, ffs);
1688 if (unlikely(ret < 0))
1689 goto error;
1690 } else {
1691 ret = 0;
1692 }
1693
1694 if (unlikely(len != ret))
1695 goto einval;
1696
1697 ffs->raw_fs_descs_length = fs_len;
1698 ffs->raw_descs_length = fs_len + ret;
1699 ffs->raw_descs = _data;
1700 ffs->fs_descs_count = fs_count;
1701 ffs->hs_descs_count = hs_count;
1702
1703 return 0;
1704
1705einval:
1706 ret = -EINVAL;
1707error:
1708 kfree(_data);
1709 return ret;
1710}
1711
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001712static int __ffs_data_got_strings(struct ffs_data *ffs,
1713 char *const _data, size_t len)
1714{
1715 u32 str_count, needed_count, lang_count;
1716 struct usb_gadget_strings **stringtabs, *t;
1717 struct usb_string *strings, *s;
1718 const char *data = _data;
1719
1720 ENTER();
1721
1722 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1723 get_unaligned_le32(data + 4) != len))
1724 goto error;
1725 str_count = get_unaligned_le32(data + 8);
1726 lang_count = get_unaligned_le32(data + 12);
1727
1728 /* if one is zero the other must be zero */
1729 if (unlikely(!str_count != !lang_count))
1730 goto error;
1731
1732 /* Do we have at least as many strings as descriptors need? */
1733 needed_count = ffs->strings_count;
1734 if (unlikely(str_count < needed_count))
1735 goto error;
1736
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001737 /*
1738 * If we don't need any strings just return and free all
1739 * memory.
1740 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001741 if (!needed_count) {
1742 kfree(_data);
1743 return 0;
1744 }
1745
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001746 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001747 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001748 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001749 vla_group(d);
1750 vla_item(d, struct usb_gadget_strings *, stringtabs,
1751 lang_count + 1);
1752 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
1753 vla_item(d, struct usb_string, strings,
1754 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001755
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001756 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
1757
1758 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001759 kfree(_data);
1760 return -ENOMEM;
1761 }
1762
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001763 /* Initialize the VLA pointers */
1764 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1765 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001766 i = lang_count;
1767 do {
1768 *stringtabs++ = t++;
1769 } while (--i);
1770 *stringtabs = NULL;
1771
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001772 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1773 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1774 t = vla_ptr(vlabuf, d, stringtab);
1775 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001776 strings = s;
1777 }
1778
1779 /* For each language */
1780 data += 16;
1781 len -= 16;
1782
1783 do { /* lang_count > 0 so we can use do-while */
1784 unsigned needed = needed_count;
1785
1786 if (unlikely(len < 3))
1787 goto error_free;
1788 t->language = get_unaligned_le16(data);
1789 t->strings = s;
1790 ++t;
1791
1792 data += 2;
1793 len -= 2;
1794
1795 /* For each string */
1796 do { /* str_count > 0 so we can use do-while */
1797 size_t length = strnlen(data, len);
1798
1799 if (unlikely(length == len))
1800 goto error_free;
1801
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001802 /*
1803 * User may provide more strings then we need,
1804 * if that's the case we simply ignore the
1805 * rest
1806 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001807 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001808 /*
1809 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001810 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001811 * now just leave garbage here.
1812 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001813 s->s = data;
1814 --needed;
1815 ++s;
1816 }
1817
1818 data += length + 1;
1819 len -= length + 1;
1820 } while (--str_count);
1821
1822 s->id = 0; /* terminator */
1823 s->s = NULL;
1824 ++s;
1825
1826 } while (--lang_count);
1827
1828 /* Some garbage left? */
1829 if (unlikely(len))
1830 goto error_free;
1831
1832 /* Done! */
1833 ffs->stringtabs = stringtabs;
1834 ffs->raw_strings = _data;
1835
1836 return 0;
1837
1838error_free:
1839 kfree(stringtabs);
1840error:
1841 kfree(_data);
1842 return -EINVAL;
1843}
1844
1845
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001846/* Events handling and management *******************************************/
1847
1848static void __ffs_event_add(struct ffs_data *ffs,
1849 enum usb_functionfs_event_type type)
1850{
1851 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1852 int neg = 0;
1853
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001854 /*
1855 * Abort any unhandled setup
1856 *
1857 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001858 * of ffs->setup_state without holding the lock because when
1859 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001860 * the source does nothing.
1861 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001862 if (ffs->setup_state == FFS_SETUP_PENDING)
1863 ffs->setup_state = FFS_SETUP_CANCELED;
1864
1865 switch (type) {
1866 case FUNCTIONFS_RESUME:
1867 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001868 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001869 case FUNCTIONFS_SUSPEND:
1870 case FUNCTIONFS_SETUP:
1871 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001872 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001873 break;
1874
1875 case FUNCTIONFS_BIND:
1876 case FUNCTIONFS_UNBIND:
1877 case FUNCTIONFS_DISABLE:
1878 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001879 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001880 rem_type1 = FUNCTIONFS_SUSPEND;
1881 rem_type2 = FUNCTIONFS_RESUME;
1882 neg = 1;
1883 break;
1884
1885 default:
1886 BUG();
1887 }
1888
1889 {
1890 u8 *ev = ffs->ev.types, *out = ev;
1891 unsigned n = ffs->ev.count;
1892 for (; n; --n, ++ev)
1893 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
1894 *out++ = *ev;
1895 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001896 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001897 ffs->ev.count = out - ffs->ev.types;
1898 }
1899
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001900 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001901 ffs->ev.types[ffs->ev.count++] = type;
1902 wake_up_locked(&ffs->ev.waitq);
1903}
1904
1905static void ffs_event_add(struct ffs_data *ffs,
1906 enum usb_functionfs_event_type type)
1907{
1908 unsigned long flags;
1909 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
1910 __ffs_event_add(ffs, type);
1911 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
1912}
1913
1914
1915/* Bind/unbind USB function hooks *******************************************/
1916
1917static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1918 struct usb_descriptor_header *desc,
1919 void *priv)
1920{
1921 struct usb_endpoint_descriptor *ds = (void *)desc;
1922 struct ffs_function *func = priv;
1923 struct ffs_ep *ffs_ep;
1924
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001925 /*
1926 * If hs_descriptors is not NULL then we are reading hs
1927 * descriptors now
1928 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001929 const int isHS = func->function.hs_descriptors != NULL;
1930 unsigned idx;
1931
1932 if (type != FFS_DESCRIPTOR)
1933 return 0;
1934
1935 if (isHS)
1936 func->function.hs_descriptors[(long)valuep] = desc;
1937 else
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001938 func->function.fs_descriptors[(long)valuep] = desc;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001939
1940 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1941 return 0;
1942
1943 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
1944 ffs_ep = func->eps + idx;
1945
1946 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001947 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001948 isHS ? "high" : "full",
1949 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001950 return -EINVAL;
1951 }
1952 ffs_ep->descs[isHS] = ds;
1953
1954 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
1955 if (ffs_ep->ep) {
1956 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
1957 if (!ds->wMaxPacketSize)
1958 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
1959 } else {
1960 struct usb_request *req;
1961 struct usb_ep *ep;
1962
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001963 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001964 ep = usb_ep_autoconfig(func->gadget, ds);
1965 if (unlikely(!ep))
1966 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08001967 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001968
1969 req = usb_ep_alloc_request(ep, GFP_KERNEL);
1970 if (unlikely(!req))
1971 return -ENOMEM;
1972
1973 ffs_ep->ep = ep;
1974 ffs_ep->req = req;
1975 func->eps_revmap[ds->bEndpointAddress &
1976 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
1977 }
1978 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
1979
1980 return 0;
1981}
1982
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001983static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
1984 struct usb_descriptor_header *desc,
1985 void *priv)
1986{
1987 struct ffs_function *func = priv;
1988 unsigned idx;
1989 u8 newValue;
1990
1991 switch (type) {
1992 default:
1993 case FFS_DESCRIPTOR:
1994 /* Handled in previous pass by __ffs_func_bind_do_descs() */
1995 return 0;
1996
1997 case FFS_INTERFACE:
1998 idx = *valuep;
1999 if (func->interfaces_nums[idx] < 0) {
2000 int id = usb_interface_id(func->conf, &func->function);
2001 if (unlikely(id < 0))
2002 return id;
2003 func->interfaces_nums[idx] = id;
2004 }
2005 newValue = func->interfaces_nums[idx];
2006 break;
2007
2008 case FFS_STRING:
2009 /* String' IDs are allocated when fsf_data is bound to cdev */
2010 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2011 break;
2012
2013 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002014 /*
2015 * USB_DT_ENDPOINT are handled in
2016 * __ffs_func_bind_do_descs().
2017 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002018 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2019 return 0;
2020
2021 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2022 if (unlikely(!func->eps[idx].ep))
2023 return -EINVAL;
2024
2025 {
2026 struct usb_endpoint_descriptor **descs;
2027 descs = func->eps[idx].descs;
2028 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2029 }
2030 break;
2031 }
2032
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002033 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002034 *valuep = newValue;
2035 return 0;
2036}
2037
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002038#ifndef USB_FFS_INCLUDED
2039static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2040 struct usb_configuration *c)
2041{
2042 struct ffs_function *func = ffs_func_from_usb(f);
2043 struct f_fs_opts *ffs_opts =
2044 container_of(f->fi, struct f_fs_opts, func_inst);
2045 int ret;
2046
2047 ENTER();
2048
2049 /*
2050 * Legacy gadget triggers binding in functionfs_ready_callback,
2051 * which already uses locking; taking the same lock here would
2052 * cause a deadlock.
2053 *
2054 * Configfs-enabled gadgets however do need ffs_dev_lock.
2055 */
2056 if (!ffs_opts->no_configfs)
2057 ffs_dev_lock();
2058 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2059 func->ffs = ffs_opts->dev->ffs_data;
2060 if (!ffs_opts->no_configfs)
2061 ffs_dev_unlock();
2062 if (ret)
2063 return ERR_PTR(ret);
2064
2065 func->conf = c;
2066 func->gadget = c->cdev->gadget;
2067
2068 ffs_data_get(func->ffs);
2069
2070 /*
2071 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2072 * configurations are bound in sequence with list_for_each_entry,
2073 * in each configuration its functions are bound in sequence
2074 * with list_for_each_entry, so we assume no race condition
2075 * with regard to ffs_opts->bound access
2076 */
2077 if (!ffs_opts->refcnt) {
2078 ret = functionfs_bind(func->ffs, c->cdev);
2079 if (ret)
2080 return ERR_PTR(ret);
2081 }
2082 ffs_opts->refcnt++;
2083 func->function.strings = func->ffs->stringtabs;
2084
2085 return ffs_opts;
2086}
2087#endif
2088
2089static int _ffs_func_bind(struct usb_configuration *c,
2090 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002091{
2092 struct ffs_function *func = ffs_func_from_usb(f);
2093 struct ffs_data *ffs = func->ffs;
2094
2095 const int full = !!func->ffs->fs_descs_count;
2096 const int high = gadget_is_dualspeed(func->gadget) &&
2097 func->ffs->hs_descs_count;
2098
2099 int ret;
2100
2101 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002102 vla_group(d);
2103 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2104 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2105 full ? ffs->fs_descs_count + 1 : 0);
2106 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2107 high ? ffs->hs_descs_count + 1 : 0);
2108 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2109 vla_item_with_sz(d, char, raw_descs,
2110 high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2111 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002112
2113 ENTER();
2114
2115 /* Only high speed but not supported by gadget? */
2116 if (unlikely(!(full | high)))
2117 return -ENOTSUPP;
2118
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002119 /* Allocate a single chunk, less management later on */
2120 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2121 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002122 return -ENOMEM;
2123
2124 /* Zero */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002125 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2126 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
2127 d_raw_descs__sz);
2128 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2129 for (ret = ffs->eps_count; ret; --ret) {
2130 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002131
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002132 ptr = vla_ptr(vlabuf, d, eps);
2133 ptr[ret].num = -1;
2134 }
2135
2136 /* Save pointers
2137 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2138 */
2139 func->eps = vla_ptr(vlabuf, d, eps);
2140 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002141
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002142 /*
2143 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002144 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002145 * numbers without worrying that it may be described later on.
2146 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002147 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002148 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002149 ret = ffs_do_descs(ffs->fs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002150 vla_ptr(vlabuf, d, raw_descs),
2151 d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002152 __ffs_func_bind_do_descs, func);
2153 if (unlikely(ret < 0))
2154 goto error;
2155 } else {
2156 ret = 0;
2157 }
2158
2159 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002160 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002161 ret = ffs_do_descs(ffs->hs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002162 vla_ptr(vlabuf, d, raw_descs) + ret,
2163 d_raw_descs__sz - ret,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002164 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002165 if (unlikely(ret < 0))
2166 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002167 }
2168
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002169 /*
2170 * Now handle interface numbers allocation and interface and
2171 * endpoint numbers rewriting. We can do that in one go
2172 * now.
2173 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002174 ret = ffs_do_descs(ffs->fs_descs_count +
2175 (high ? ffs->hs_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002176 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002177 __ffs_func_bind_do_nums, func);
2178 if (unlikely(ret < 0))
2179 goto error;
2180
2181 /* And we're done */
2182 ffs_event_add(ffs, FUNCTIONFS_BIND);
2183 return 0;
2184
2185error:
2186 /* XXX Do we need to release all claimed endpoints here? */
2187 return ret;
2188}
2189
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002190static int ffs_func_bind(struct usb_configuration *c,
2191 struct usb_function *f)
2192{
2193#ifndef USB_FFS_INCLUDED
2194 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2195
2196 if (IS_ERR(ffs_opts))
2197 return PTR_ERR(ffs_opts);
2198#endif
2199
2200 return _ffs_func_bind(c, f);
2201}
2202
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002203
2204/* Other USB function hooks *************************************************/
2205
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002206#ifdef USB_FFS_INCLUDED
2207
2208static void old_ffs_func_unbind(struct usb_configuration *c,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002209 struct usb_function *f)
2210{
2211 struct ffs_function *func = ffs_func_from_usb(f);
2212 struct ffs_data *ffs = func->ffs;
2213
2214 ENTER();
2215
2216 if (ffs->func == func) {
2217 ffs_func_eps_disable(func);
2218 ffs->func = NULL;
2219 }
2220
2221 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2222
2223 ffs_func_free(func);
2224}
2225
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002226#endif
2227
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002228static int ffs_func_set_alt(struct usb_function *f,
2229 unsigned interface, unsigned alt)
2230{
2231 struct ffs_function *func = ffs_func_from_usb(f);
2232 struct ffs_data *ffs = func->ffs;
2233 int ret = 0, intf;
2234
2235 if (alt != (unsigned)-1) {
2236 intf = ffs_func_revmap_intf(func, interface);
2237 if (unlikely(intf < 0))
2238 return intf;
2239 }
2240
2241 if (ffs->func)
2242 ffs_func_eps_disable(ffs->func);
2243
2244 if (ffs->state != FFS_ACTIVE)
2245 return -ENODEV;
2246
2247 if (alt == (unsigned)-1) {
2248 ffs->func = NULL;
2249 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2250 return 0;
2251 }
2252
2253 ffs->func = func;
2254 ret = ffs_func_eps_enable(func);
2255 if (likely(ret >= 0))
2256 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2257 return ret;
2258}
2259
2260static void ffs_func_disable(struct usb_function *f)
2261{
2262 ffs_func_set_alt(f, 0, (unsigned)-1);
2263}
2264
2265static int ffs_func_setup(struct usb_function *f,
2266 const struct usb_ctrlrequest *creq)
2267{
2268 struct ffs_function *func = ffs_func_from_usb(f);
2269 struct ffs_data *ffs = func->ffs;
2270 unsigned long flags;
2271 int ret;
2272
2273 ENTER();
2274
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002275 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2276 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2277 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2278 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2279 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002280
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002281 /*
2282 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002283 * (notable exceptions are set/get interface) so we need to
2284 * handle them. All other either handled by composite or
2285 * passed to usb_configuration->setup() (if one is set). No
2286 * matter, we will handle requests directed to endpoint here
2287 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002288 * other request?
2289 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002290 if (ffs->state != FFS_ACTIVE)
2291 return -ENODEV;
2292
2293 switch (creq->bRequestType & USB_RECIP_MASK) {
2294 case USB_RECIP_INTERFACE:
2295 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2296 if (unlikely(ret < 0))
2297 return ret;
2298 break;
2299
2300 case USB_RECIP_ENDPOINT:
2301 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2302 if (unlikely(ret < 0))
2303 return ret;
2304 break;
2305
2306 default:
2307 return -EOPNOTSUPP;
2308 }
2309
2310 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2311 ffs->ev.setup = *creq;
2312 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2313 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2314 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2315
2316 return 0;
2317}
2318
2319static void ffs_func_suspend(struct usb_function *f)
2320{
2321 ENTER();
2322 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2323}
2324
2325static void ffs_func_resume(struct usb_function *f)
2326{
2327 ENTER();
2328 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2329}
2330
2331
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002332/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002333
2334static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2335{
2336 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2337 return num ? num : -EDOM;
2338}
2339
2340static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2341{
2342 short *nums = func->interfaces_nums;
2343 unsigned count = func->ffs->interfaces_count;
2344
2345 for (; count; --count, ++nums) {
2346 if (*nums >= 0 && *nums == intf)
2347 return nums - func->interfaces_nums;
2348 }
2349
2350 return -EDOM;
2351}
2352
2353
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002354/* Devices management *******************************************************/
2355
2356static LIST_HEAD(ffs_devices);
2357
2358static struct ffs_dev *_ffs_find_dev(const char *name)
2359{
2360 struct ffs_dev *dev;
2361
2362 list_for_each_entry(dev, &ffs_devices, entry) {
2363 if (!dev->name || !name)
2364 continue;
2365 if (strcmp(dev->name, name) == 0)
2366 return dev;
2367 }
2368
2369 return NULL;
2370}
2371
2372/*
2373 * ffs_lock must be taken by the caller of this function
2374 */
2375static struct ffs_dev *ffs_get_single_dev(void)
2376{
2377 struct ffs_dev *dev;
2378
2379 if (list_is_singular(&ffs_devices)) {
2380 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2381 if (dev->single)
2382 return dev;
2383 }
2384
2385 return NULL;
2386}
2387
2388/*
2389 * ffs_lock must be taken by the caller of this function
2390 */
2391static struct ffs_dev *ffs_find_dev(const char *name)
2392{
2393 struct ffs_dev *dev;
2394
2395 dev = ffs_get_single_dev();
2396 if (dev)
2397 return dev;
2398
2399 return _ffs_find_dev(name);
2400}
2401
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002402/* Function registration interface ******************************************/
2403
2404#ifndef USB_FFS_INCLUDED
2405
2406static void ffs_free_inst(struct usb_function_instance *f)
2407{
2408 struct f_fs_opts *opts;
2409
2410 opts = to_f_fs_opts(f);
2411 ffs_dev_lock();
2412 ffs_free_dev(opts->dev);
2413 ffs_dev_unlock();
2414 kfree(opts);
2415}
2416
2417static struct usb_function_instance *ffs_alloc_inst(void)
2418{
2419 struct f_fs_opts *opts;
2420 struct ffs_dev *dev;
2421
2422 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2423 if (!opts)
2424 return ERR_PTR(-ENOMEM);
2425
2426 opts->func_inst.free_func_inst = ffs_free_inst;
2427 ffs_dev_lock();
2428 dev = ffs_alloc_dev();
2429 ffs_dev_unlock();
2430 if (IS_ERR(dev)) {
2431 kfree(opts);
2432 return ERR_CAST(dev);
2433 }
2434 opts->dev = dev;
2435
2436 return &opts->func_inst;
2437}
2438
2439static void ffs_free(struct usb_function *f)
2440{
2441 kfree(ffs_func_from_usb(f));
2442}
2443
2444static void ffs_func_unbind(struct usb_configuration *c,
2445 struct usb_function *f)
2446{
2447 struct ffs_function *func = ffs_func_from_usb(f);
2448 struct ffs_data *ffs = func->ffs;
2449 struct f_fs_opts *opts =
2450 container_of(f->fi, struct f_fs_opts, func_inst);
2451 struct ffs_ep *ep = func->eps;
2452 unsigned count = ffs->eps_count;
2453 unsigned long flags;
2454
2455 ENTER();
2456 if (ffs->func == func) {
2457 ffs_func_eps_disable(func);
2458 ffs->func = NULL;
2459 }
2460
2461 if (!--opts->refcnt)
2462 functionfs_unbind(ffs);
2463
2464 /* cleanup after autoconfig */
2465 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2466 do {
2467 if (ep->ep && ep->req)
2468 usb_ep_free_request(ep->ep, ep->req);
2469 ep->req = NULL;
2470 ++ep;
2471 } while (--count);
2472 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2473 kfree(func->eps);
2474 func->eps = NULL;
2475 /*
2476 * eps, descriptors and interfaces_nums are allocated in the
2477 * same chunk so only one free is required.
2478 */
2479 func->function.fs_descriptors = NULL;
2480 func->function.hs_descriptors = NULL;
2481 func->interfaces_nums = NULL;
2482
2483 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2484}
2485
2486static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2487{
2488 struct ffs_function *func;
2489
2490 ENTER();
2491
2492 func = kzalloc(sizeof(*func), GFP_KERNEL);
2493 if (unlikely(!func))
2494 return ERR_PTR(-ENOMEM);
2495
2496 func->function.name = "Function FS Gadget";
2497
2498 func->function.bind = ffs_func_bind;
2499 func->function.unbind = ffs_func_unbind;
2500 func->function.set_alt = ffs_func_set_alt;
2501 func->function.disable = ffs_func_disable;
2502 func->function.setup = ffs_func_setup;
2503 func->function.suspend = ffs_func_suspend;
2504 func->function.resume = ffs_func_resume;
2505 func->function.free_func = ffs_free;
2506
2507 return &func->function;
2508}
2509
2510#endif
2511
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002512/*
2513 * ffs_lock must be taken by the caller of this function
2514 */
2515struct ffs_dev *ffs_alloc_dev(void)
2516{
2517 struct ffs_dev *dev;
2518 int ret;
2519
2520 if (ffs_get_single_dev())
2521 return ERR_PTR(-EBUSY);
2522
2523 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2524 if (!dev)
2525 return ERR_PTR(-ENOMEM);
2526
2527 if (list_empty(&ffs_devices)) {
2528 ret = functionfs_init();
2529 if (ret) {
2530 kfree(dev);
2531 return ERR_PTR(ret);
2532 }
2533 }
2534
2535 list_add(&dev->entry, &ffs_devices);
2536
2537 return dev;
2538}
2539
2540/*
2541 * ffs_lock must be taken by the caller of this function
2542 * The caller is responsible for "name" being available whenever f_fs needs it
2543 */
2544static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2545{
2546 struct ffs_dev *existing;
2547
2548 existing = _ffs_find_dev(name);
2549 if (existing)
2550 return -EBUSY;
2551
2552 dev->name = name;
2553
2554 return 0;
2555}
2556
2557/*
2558 * The caller is responsible for "name" being available whenever f_fs needs it
2559 */
2560int ffs_name_dev(struct ffs_dev *dev, const char *name)
2561{
2562 int ret;
2563
2564 ffs_dev_lock();
2565 ret = _ffs_name_dev(dev, name);
2566 ffs_dev_unlock();
2567
2568 return ret;
2569}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002570#ifndef USB_FFS_INCLUDED
2571EXPORT_SYMBOL(ffs_name_dev);
2572#endif
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002573
2574int ffs_single_dev(struct ffs_dev *dev)
2575{
2576 int ret;
2577
2578 ret = 0;
2579 ffs_dev_lock();
2580
2581 if (!list_is_singular(&ffs_devices))
2582 ret = -EBUSY;
2583 else
2584 dev->single = true;
2585
2586 ffs_dev_unlock();
2587 return ret;
2588}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002589#ifndef USB_FFS_INCLUDED
2590EXPORT_SYMBOL(ffs_single_dev);
2591#endif
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002592
2593/*
2594 * ffs_lock must be taken by the caller of this function
2595 */
2596void ffs_free_dev(struct ffs_dev *dev)
2597{
2598 list_del(&dev->entry);
2599 kfree(dev);
2600 if (list_empty(&ffs_devices))
2601 functionfs_cleanup();
2602}
2603
2604static void *ffs_acquire_dev(const char *dev_name)
2605{
2606 struct ffs_dev *ffs_dev;
2607
2608 ENTER();
2609 ffs_dev_lock();
2610
2611 ffs_dev = ffs_find_dev(dev_name);
2612 if (!ffs_dev)
2613 ffs_dev = ERR_PTR(-ENODEV);
2614 else if (ffs_dev->mounted)
2615 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002616 else if (ffs_dev->ffs_acquire_dev_callback &&
2617 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2618 ffs_dev = ERR_PTR(-ENODEV);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002619 else
2620 ffs_dev->mounted = true;
2621
2622 ffs_dev_unlock();
2623 return ffs_dev;
2624}
2625
2626static void ffs_release_dev(struct ffs_data *ffs_data)
2627{
2628 struct ffs_dev *ffs_dev;
2629
2630 ENTER();
2631 ffs_dev_lock();
2632
2633 ffs_dev = ffs_data->private_data;
2634 if (ffs_dev)
2635 ffs_dev->mounted = false;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002636
2637 if (ffs_dev->ffs_release_dev_callback)
2638 ffs_dev->ffs_release_dev_callback(ffs_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002639
2640 ffs_dev_unlock();
2641}
2642
2643static int ffs_ready(struct ffs_data *ffs)
2644{
2645 struct ffs_dev *ffs_obj;
2646 int ret = 0;
2647
2648 ENTER();
2649 ffs_dev_lock();
2650
2651 ffs_obj = ffs->private_data;
2652 if (!ffs_obj) {
2653 ret = -EINVAL;
2654 goto done;
2655 }
2656 if (WARN_ON(ffs_obj->desc_ready)) {
2657 ret = -EBUSY;
2658 goto done;
2659 }
2660
2661 ffs_obj->desc_ready = true;
2662 ffs_obj->ffs_data = ffs;
2663
2664 if (ffs_obj->ffs_ready_callback)
2665 ret = ffs_obj->ffs_ready_callback(ffs);
2666
2667done:
2668 ffs_dev_unlock();
2669 return ret;
2670}
2671
2672static void ffs_closed(struct ffs_data *ffs)
2673{
2674 struct ffs_dev *ffs_obj;
2675
2676 ENTER();
2677 ffs_dev_lock();
2678
2679 ffs_obj = ffs->private_data;
2680 if (!ffs_obj)
2681 goto done;
2682
2683 ffs_obj->desc_ready = false;
2684
2685 if (ffs_obj->ffs_closed_callback)
2686 ffs_obj->ffs_closed_callback(ffs);
2687done:
2688 ffs_dev_unlock();
2689}
2690
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002691/* Misc helper functions ****************************************************/
2692
2693static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2694{
2695 return nonblock
2696 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2697 : mutex_lock_interruptible(mutex);
2698}
2699
Al Viro260ef312012-09-26 21:43:45 -04002700static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002701{
2702 char *data;
2703
2704 if (unlikely(!len))
2705 return NULL;
2706
2707 data = kmalloc(len, GFP_KERNEL);
2708 if (unlikely(!data))
2709 return ERR_PTR(-ENOMEM);
2710
2711 if (unlikely(__copy_from_user(data, buf, len))) {
2712 kfree(data);
2713 return ERR_PTR(-EFAULT);
2714 }
2715
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002716 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002717 ffs_dump_mem("", data, len);
2718
2719 return data;
2720}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002721
2722#ifndef USB_FFS_INCLUDED
2723DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
2724MODULE_LICENSE("GPL");
2725MODULE_AUTHOR("Michal Nazarewicz");
2726#endif