blob: eb5cb2b4b90d3f8cf05e8035dc0fb6af4a0ce577 [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"
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010032#include "configfs.h"
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020033
34#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
35
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +010036/* Variable Length Array Macros **********************************************/
37#define vla_group(groupname) size_t groupname##__next = 0
38#define vla_group_size(groupname) groupname##__next
39
40#define vla_item(groupname, type, name, n) \
41 size_t groupname##_##name##__offset = ({ \
42 size_t align_mask = __alignof__(type) - 1; \
43 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
44 size_t size = (n) * sizeof(type); \
45 groupname##__next = offset + size; \
46 offset; \
47 })
48
49#define vla_item_with_sz(groupname, type, name, n) \
50 size_t groupname##_##name##__sz = (n) * sizeof(type); \
51 size_t groupname##_##name##__offset = ({ \
52 size_t align_mask = __alignof__(type) - 1; \
53 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
54 size_t size = groupname##_##name##__sz; \
55 groupname##__next = offset + size; \
56 offset; \
57 })
58
59#define vla_ptr(ptr, groupname, name) \
60 ((void *) ((char *)ptr + groupname##_##name##__offset))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020061
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020062/* Reference counter handling */
63static void ffs_data_get(struct ffs_data *ffs);
64static void ffs_data_put(struct ffs_data *ffs);
65/* Creates new ffs_data object. */
66static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
67
68/* Opened counter handling. */
69static void ffs_data_opened(struct ffs_data *ffs);
70static void ffs_data_closed(struct ffs_data *ffs);
71
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010072/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020073static int __must_check
74__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
75static int __must_check
76__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
77
78
79/* The function structure ***************************************************/
80
81struct ffs_ep;
82
83struct ffs_function {
84 struct usb_configuration *conf;
85 struct usb_gadget *gadget;
86 struct ffs_data *ffs;
87
88 struct ffs_ep *eps;
89 u8 eps_revmap[16];
90 short *interfaces_nums;
91
92 struct usb_function function;
93};
94
95
96static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
97{
98 return container_of(f, struct ffs_function, function);
99}
100
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200101
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200102static void ffs_func_eps_disable(struct ffs_function *func);
103static int __must_check ffs_func_eps_enable(struct ffs_function *func);
104
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200105static int ffs_func_bind(struct usb_configuration *,
106 struct usb_function *);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
108static void ffs_func_disable(struct usb_function *);
109static int ffs_func_setup(struct usb_function *,
110 const struct usb_ctrlrequest *);
111static void ffs_func_suspend(struct usb_function *);
112static void ffs_func_resume(struct usb_function *);
113
114
115static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
116static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
117
118
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200119/* The endpoints structures *************************************************/
120
121struct ffs_ep {
122 struct usb_ep *ep; /* P: ffs->eps_lock */
123 struct usb_request *req; /* P: epfile->mutex */
124
125 /* [0]: full speed, [1]: high speed */
126 struct usb_endpoint_descriptor *descs[2];
127
128 u8 num;
129
130 int status; /* P: epfile->mutex */
131};
132
133struct ffs_epfile {
134 /* Protects ep->ep and ep->req. */
135 struct mutex mutex;
136 wait_queue_head_t wait;
137
138 struct ffs_data *ffs;
139 struct ffs_ep *ep; /* P: ffs->eps_lock */
140
141 struct dentry *dentry;
142
143 char name[5];
144
145 unsigned char in; /* P: ffs->eps_lock */
146 unsigned char isoc; /* P: ffs->eps_lock */
147
148 unsigned char _pad;
149};
150
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200151static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
152static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
153
154static struct inode *__must_check
155ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
156 const struct file_operations *fops,
157 struct dentry **dentry_p);
158
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100159/* Devices management *******************************************************/
160
161DEFINE_MUTEX(ffs_lock);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100162EXPORT_SYMBOL(ffs_lock);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100163
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100164static struct ffs_dev *_ffs_find_dev(const char *name);
165static struct ffs_dev *_ffs_alloc_dev(void);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +0100166static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100167static void _ffs_free_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100168static void *ffs_acquire_dev(const char *dev_name);
169static void ffs_release_dev(struct ffs_data *ffs_data);
170static int ffs_ready(struct ffs_data *ffs);
171static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200172
173/* Misc helper functions ****************************************************/
174
175static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
176 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400177static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200178 __attribute__((warn_unused_result, nonnull));
179
180
181/* Control file aka ep0 *****************************************************/
182
183static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
184{
185 struct ffs_data *ffs = req->context;
186
187 complete_all(&ffs->ep0req_completion);
188}
189
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200190static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
191{
192 struct usb_request *req = ffs->ep0req;
193 int ret;
194
195 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
196
197 spin_unlock_irq(&ffs->ev.waitq.lock);
198
199 req->buf = data;
200 req->length = len;
201
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100202 /*
203 * UDC layer requires to provide a buffer even for ZLP, but should
204 * not use it at all. Let's provide some poisoned pointer to catch
205 * possible bug in the driver.
206 */
207 if (req->buf == NULL)
208 req->buf = (void *)0xDEADBABE;
209
Wolfram Sang16735d02013-11-14 14:32:02 -0800210 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200211
212 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
213 if (unlikely(ret < 0))
214 return ret;
215
216 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
217 if (unlikely(ret)) {
218 usb_ep_dequeue(ffs->gadget->ep0, req);
219 return -EINTR;
220 }
221
222 ffs->setup_state = FFS_NO_SETUP;
223 return ffs->ep0req_status;
224}
225
226static int __ffs_ep0_stall(struct ffs_data *ffs)
227{
228 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100229 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200230 usb_ep_set_halt(ffs->gadget->ep0);
231 ffs->setup_state = FFS_NO_SETUP;
232 return -EL2HLT;
233 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100234 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200235 return -ESRCH;
236 }
237}
238
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200239static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
240 size_t len, loff_t *ptr)
241{
242 struct ffs_data *ffs = file->private_data;
243 ssize_t ret;
244 char *data;
245
246 ENTER();
247
248 /* Fast check if setup was canceled */
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100249 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200250 return -EIDRM;
251
252 /* Acquire mutex */
253 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
254 if (unlikely(ret < 0))
255 return ret;
256
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200257 /* Check state */
258 switch (ffs->state) {
259 case FFS_READ_DESCRIPTORS:
260 case FFS_READ_STRINGS:
261 /* Copy data */
262 if (unlikely(len < 16)) {
263 ret = -EINVAL;
264 break;
265 }
266
267 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100268 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200269 ret = PTR_ERR(data);
270 break;
271 }
272
273 /* Handle data */
274 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100275 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200276 ret = __ffs_data_got_descs(ffs, data, len);
277 if (unlikely(ret < 0))
278 break;
279
280 ffs->state = FFS_READ_STRINGS;
281 ret = len;
282 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100283 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200284 ret = __ffs_data_got_strings(ffs, data, len);
285 if (unlikely(ret < 0))
286 break;
287
288 ret = ffs_epfiles_create(ffs);
289 if (unlikely(ret)) {
290 ffs->state = FFS_CLOSING;
291 break;
292 }
293
294 ffs->state = FFS_ACTIVE;
295 mutex_unlock(&ffs->mutex);
296
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100297 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200298 if (unlikely(ret < 0)) {
299 ffs->state = FFS_CLOSING;
300 return ret;
301 }
302
303 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
304 return len;
305 }
306 break;
307
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200308 case FFS_ACTIVE:
309 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100310 /*
311 * We're called from user space, we can use _irq
312 * rather then _irqsave
313 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200314 spin_lock_irq(&ffs->ev.waitq.lock);
315 switch (FFS_SETUP_STATE(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100316 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200317 ret = -EIDRM;
318 goto done_spin;
319
320 case FFS_NO_SETUP:
321 ret = -ESRCH;
322 goto done_spin;
323
324 case FFS_SETUP_PENDING:
325 break;
326 }
327
328 /* FFS_SETUP_PENDING */
329 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
330 spin_unlock_irq(&ffs->ev.waitq.lock);
331 ret = __ffs_ep0_stall(ffs);
332 break;
333 }
334
335 /* FFS_SETUP_PENDING and not stall */
336 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
337
338 spin_unlock_irq(&ffs->ev.waitq.lock);
339
340 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100341 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200342 ret = PTR_ERR(data);
343 break;
344 }
345
346 spin_lock_irq(&ffs->ev.waitq.lock);
347
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100348 /*
349 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200350 * but the state of setup could have changed from
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100351 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200352 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100353 * from user space in vain but it's unlikely.
354 *
355 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200356 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
357 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100358 * mutex.
359 */
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100360 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200361 ret = -EIDRM;
362done_spin:
363 spin_unlock_irq(&ffs->ev.waitq.lock);
364 } else {
365 /* unlocks spinlock */
366 ret = __ffs_ep0_queue_wait(ffs, data, len);
367 }
368 kfree(data);
369 break;
370
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200371 default:
372 ret = -EBADFD;
373 break;
374 }
375
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200376 mutex_unlock(&ffs->mutex);
377 return ret;
378}
379
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200380static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
381 size_t n)
382{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100383 /*
384 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
385 * to release them.
386 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200387 struct usb_functionfs_event events[n];
388 unsigned i = 0;
389
390 memset(events, 0, sizeof events);
391
392 do {
393 events[i].type = ffs->ev.types[i];
394 if (events[i].type == FUNCTIONFS_SETUP) {
395 events[i].u.setup = ffs->ev.setup;
396 ffs->setup_state = FFS_SETUP_PENDING;
397 }
398 } while (++i < n);
399
400 if (n < ffs->ev.count) {
401 ffs->ev.count -= n;
402 memmove(ffs->ev.types, ffs->ev.types + n,
403 ffs->ev.count * sizeof *ffs->ev.types);
404 } else {
405 ffs->ev.count = 0;
406 }
407
408 spin_unlock_irq(&ffs->ev.waitq.lock);
409 mutex_unlock(&ffs->mutex);
410
411 return unlikely(__copy_to_user(buf, events, sizeof events))
412 ? -EFAULT : sizeof events;
413}
414
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200415static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
416 size_t len, loff_t *ptr)
417{
418 struct ffs_data *ffs = file->private_data;
419 char *data = NULL;
420 size_t n;
421 int ret;
422
423 ENTER();
424
425 /* Fast check if setup was canceled */
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100426 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200427 return -EIDRM;
428
429 /* Acquire mutex */
430 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
431 if (unlikely(ret < 0))
432 return ret;
433
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200434 /* Check state */
435 if (ffs->state != FFS_ACTIVE) {
436 ret = -EBADFD;
437 goto done_mutex;
438 }
439
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100440 /*
441 * We're called from user space, we can use _irq rather then
442 * _irqsave
443 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200444 spin_lock_irq(&ffs->ev.waitq.lock);
445
446 switch (FFS_SETUP_STATE(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100447 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200448 ret = -EIDRM;
449 break;
450
451 case FFS_NO_SETUP:
452 n = len / sizeof(struct usb_functionfs_event);
453 if (unlikely(!n)) {
454 ret = -EINVAL;
455 break;
456 }
457
458 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
459 ret = -EAGAIN;
460 break;
461 }
462
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100463 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
464 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200465 ret = -EINTR;
466 break;
467 }
468
469 return __ffs_ep0_read_events(ffs, buf,
470 min(n, (size_t)ffs->ev.count));
471
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200472 case FFS_SETUP_PENDING:
473 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
474 spin_unlock_irq(&ffs->ev.waitq.lock);
475 ret = __ffs_ep0_stall(ffs);
476 goto done_mutex;
477 }
478
479 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
480
481 spin_unlock_irq(&ffs->ev.waitq.lock);
482
483 if (likely(len)) {
484 data = kmalloc(len, GFP_KERNEL);
485 if (unlikely(!data)) {
486 ret = -ENOMEM;
487 goto done_mutex;
488 }
489 }
490
491 spin_lock_irq(&ffs->ev.waitq.lock);
492
493 /* See ffs_ep0_write() */
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100494 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200495 ret = -EIDRM;
496 break;
497 }
498
499 /* unlocks spinlock */
500 ret = __ffs_ep0_queue_wait(ffs, data, len);
501 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
502 ret = -EFAULT;
503 goto done_mutex;
504
505 default:
506 ret = -EBADFD;
507 break;
508 }
509
510 spin_unlock_irq(&ffs->ev.waitq.lock);
511done_mutex:
512 mutex_unlock(&ffs->mutex);
513 kfree(data);
514 return ret;
515}
516
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200517static int ffs_ep0_open(struct inode *inode, struct file *file)
518{
519 struct ffs_data *ffs = inode->i_private;
520
521 ENTER();
522
523 if (unlikely(ffs->state == FFS_CLOSING))
524 return -EBUSY;
525
526 file->private_data = ffs;
527 ffs_data_opened(ffs);
528
529 return 0;
530}
531
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200532static int ffs_ep0_release(struct inode *inode, struct file *file)
533{
534 struct ffs_data *ffs = file->private_data;
535
536 ENTER();
537
538 ffs_data_closed(ffs);
539
540 return 0;
541}
542
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200543static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
544{
545 struct ffs_data *ffs = file->private_data;
546 struct usb_gadget *gadget = ffs->gadget;
547 long ret;
548
549 ENTER();
550
551 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
552 struct ffs_function *func = ffs->func;
553 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200554 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200555 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200556 } else {
557 ret = -ENOTTY;
558 }
559
560 return ret;
561}
562
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200563static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200564 .llseek = no_llseek,
565
566 .open = ffs_ep0_open,
567 .write = ffs_ep0_write,
568 .read = ffs_ep0_read,
569 .release = ffs_ep0_release,
570 .unlocked_ioctl = ffs_ep0_ioctl,
571};
572
573
574/* "Normal" endpoints operations ********************************************/
575
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200576static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
577{
578 ENTER();
579 if (likely(req->context)) {
580 struct ffs_ep *ep = _ep->driver_data;
581 ep->status = req->status ? req->status : req->actual;
582 complete(req->context);
583 }
584}
585
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200586static ssize_t ffs_epfile_io(struct file *file,
587 char __user *buf, size_t len, int read)
588{
589 struct ffs_epfile *epfile = file->private_data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800590 struct usb_gadget *gadget = epfile->ffs->gadget;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200591 struct ffs_ep *ep;
592 char *data = NULL;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800593 ssize_t ret, data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200594 int halt;
595
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800596 /* Are we still active? */
597 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
598 ret = -ENODEV;
599 goto error;
600 }
601
602 /* Wait for endpoint to be enabled */
603 ep = epfile->ep;
604 if (!ep) {
605 if (file->f_flags & O_NONBLOCK) {
606 ret = -EAGAIN;
607 goto error;
608 }
609
610 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
611 if (ret) {
612 ret = -EINTR;
613 goto error;
614 }
615 }
616
617 /* Do we halt? */
618 halt = !read == !epfile->in;
619 if (halt && epfile->isoc) {
620 ret = -EINVAL;
621 goto error;
622 }
623
624 /* Allocate & copy */
625 if (!halt) {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800626 /*
627 * Controller may require buffer size to be aligned to
628 * maxpacketsize of an out endpoint.
629 */
630 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
631
632 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800633 if (unlikely(!data))
634 return -ENOMEM;
635
636 if (!read && unlikely(copy_from_user(data, buf, len))) {
637 ret = -EFAULT;
638 goto error;
639 }
640 }
641
642 /* We will be using request */
643 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
644 if (unlikely(ret))
645 goto error;
646
647 spin_lock_irq(&epfile->ffs->eps_lock);
648
649 if (epfile->ep != ep) {
650 /* In the meantime, endpoint got disabled or changed. */
651 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200652 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800653 } else if (halt) {
654 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200655 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
656 usb_ep_set_halt(ep->ep);
657 spin_unlock_irq(&epfile->ffs->eps_lock);
658 ret = -EBADMSG;
659 } else {
660 /* Fire the request */
661 DECLARE_COMPLETION_ONSTACK(done);
662
663 struct usb_request *req = ep->req;
664 req->context = &done;
665 req->complete = ffs_epfile_io_complete;
666 req->buf = data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800667 req->length = data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200668
669 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
670
671 spin_unlock_irq(&epfile->ffs->eps_lock);
672
673 if (unlikely(ret < 0)) {
674 /* nop */
675 } else if (unlikely(wait_for_completion_interruptible(&done))) {
676 ret = -EINTR;
677 usb_ep_dequeue(ep->ep, req);
678 } else {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800679 /*
680 * XXX We may end up silently droping data here.
681 * Since data_len (i.e. req->length) may be bigger
682 * than len (after being rounded up to maxpacketsize),
683 * we may end up with more data then user space has
684 * space for.
685 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200686 ret = ep->status;
687 if (read && ret > 0 &&
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800688 unlikely(copy_to_user(buf, data,
689 min_t(size_t, ret, len))))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200690 ret = -EFAULT;
691 }
692 }
693
694 mutex_unlock(&epfile->mutex);
695error:
696 kfree(data);
697 return ret;
698}
699
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200700static ssize_t
701ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
702 loff_t *ptr)
703{
704 ENTER();
705
706 return ffs_epfile_io(file, (char __user *)buf, len, 0);
707}
708
709static ssize_t
710ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
711{
712 ENTER();
713
714 return ffs_epfile_io(file, buf, len, 1);
715}
716
717static int
718ffs_epfile_open(struct inode *inode, struct file *file)
719{
720 struct ffs_epfile *epfile = inode->i_private;
721
722 ENTER();
723
724 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
725 return -ENODEV;
726
727 file->private_data = epfile;
728 ffs_data_opened(epfile->ffs);
729
730 return 0;
731}
732
733static int
734ffs_epfile_release(struct inode *inode, struct file *file)
735{
736 struct ffs_epfile *epfile = inode->i_private;
737
738 ENTER();
739
740 ffs_data_closed(epfile->ffs);
741
742 return 0;
743}
744
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200745static long ffs_epfile_ioctl(struct file *file, unsigned code,
746 unsigned long value)
747{
748 struct ffs_epfile *epfile = file->private_data;
749 int ret;
750
751 ENTER();
752
753 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
754 return -ENODEV;
755
756 spin_lock_irq(&epfile->ffs->eps_lock);
757 if (likely(epfile->ep)) {
758 switch (code) {
759 case FUNCTIONFS_FIFO_STATUS:
760 ret = usb_ep_fifo_status(epfile->ep->ep);
761 break;
762 case FUNCTIONFS_FIFO_FLUSH:
763 usb_ep_fifo_flush(epfile->ep->ep);
764 ret = 0;
765 break;
766 case FUNCTIONFS_CLEAR_HALT:
767 ret = usb_ep_clear_halt(epfile->ep->ep);
768 break;
769 case FUNCTIONFS_ENDPOINT_REVMAP:
770 ret = epfile->ep->num;
771 break;
772 default:
773 ret = -ENOTTY;
774 }
775 } else {
776 ret = -ENODEV;
777 }
778 spin_unlock_irq(&epfile->ffs->eps_lock);
779
780 return ret;
781}
782
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200783static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200784 .llseek = no_llseek,
785
786 .open = ffs_epfile_open,
787 .write = ffs_epfile_write,
788 .read = ffs_epfile_read,
789 .release = ffs_epfile_release,
790 .unlocked_ioctl = ffs_epfile_ioctl,
791};
792
793
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200794/* File system and super block operations ***********************************/
795
796/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100797 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200798 * function configuration then later for event monitoring.
799 */
800
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200801static struct inode *__must_check
802ffs_sb_make_inode(struct super_block *sb, void *data,
803 const struct file_operations *fops,
804 const struct inode_operations *iops,
805 struct ffs_file_perms *perms)
806{
807 struct inode *inode;
808
809 ENTER();
810
811 inode = new_inode(sb);
812
813 if (likely(inode)) {
814 struct timespec current_time = CURRENT_TIME;
815
Al Viro12ba8d12010-10-27 04:19:36 +0100816 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200817 inode->i_mode = perms->mode;
818 inode->i_uid = perms->uid;
819 inode->i_gid = perms->gid;
820 inode->i_atime = current_time;
821 inode->i_mtime = current_time;
822 inode->i_ctime = current_time;
823 inode->i_private = data;
824 if (fops)
825 inode->i_fop = fops;
826 if (iops)
827 inode->i_op = iops;
828 }
829
830 return inode;
831}
832
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200833/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200834static struct inode *ffs_sb_create_file(struct super_block *sb,
835 const char *name, void *data,
836 const struct file_operations *fops,
837 struct dentry **dentry_p)
838{
839 struct ffs_data *ffs = sb->s_fs_info;
840 struct dentry *dentry;
841 struct inode *inode;
842
843 ENTER();
844
845 dentry = d_alloc_name(sb->s_root, name);
846 if (unlikely(!dentry))
847 return NULL;
848
849 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
850 if (unlikely(!inode)) {
851 dput(dentry);
852 return NULL;
853 }
854
855 d_add(dentry, inode);
856 if (dentry_p)
857 *dentry_p = dentry;
858
859 return inode;
860}
861
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200862/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200863static const struct super_operations ffs_sb_operations = {
864 .statfs = simple_statfs,
865 .drop_inode = generic_delete_inode,
866};
867
868struct ffs_sb_fill_data {
869 struct ffs_file_perms perms;
870 umode_t root_mode;
871 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +0100872 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200873};
874
875static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
876{
877 struct ffs_sb_fill_data *data = _data;
878 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +0100879 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200880
881 ENTER();
882
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200883 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +0100884 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200885 sb->s_fs_info = ffs;
886 sb->s_blocksize = PAGE_CACHE_SIZE;
887 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
888 sb->s_magic = FUNCTIONFS_MAGIC;
889 sb->s_op = &ffs_sb_operations;
890 sb->s_time_gran = 1;
891
892 /* Root inode */
893 data->perms.mode = data->root_mode;
894 inode = ffs_sb_make_inode(sb, NULL,
895 &simple_dir_operations,
896 &simple_dir_inode_operations,
897 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -0500898 sb->s_root = d_make_root(inode);
899 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +0100900 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200901
902 /* EP0 file */
903 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
904 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +0100905 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200906
907 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200908}
909
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200910static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
911{
912 ENTER();
913
914 if (!opts || !*opts)
915 return 0;
916
917 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200918 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100919 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200920
921 /* Option limit */
922 comma = strchr(opts, ',');
923 if (comma)
924 *comma = 0;
925
926 /* Value limit */
927 eq = strchr(opts, '=');
928 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100929 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200930 return -EINVAL;
931 }
932 *eq = 0;
933
934 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100935 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100936 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200937 return -EINVAL;
938 }
939
940 /* Interpret option */
941 switch (eq - opts) {
942 case 5:
943 if (!memcmp(opts, "rmode", 5))
944 data->root_mode = (value & 0555) | S_IFDIR;
945 else if (!memcmp(opts, "fmode", 5))
946 data->perms.mode = (value & 0666) | S_IFREG;
947 else
948 goto invalid;
949 break;
950
951 case 4:
952 if (!memcmp(opts, "mode", 4)) {
953 data->root_mode = (value & 0555) | S_IFDIR;
954 data->perms.mode = (value & 0666) | S_IFREG;
955 } else {
956 goto invalid;
957 }
958 break;
959
960 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700961 if (!memcmp(opts, "uid", 3)) {
962 data->perms.uid = make_kuid(current_user_ns(), value);
963 if (!uid_valid(data->perms.uid)) {
964 pr_err("%s: unmapped value: %lu\n", opts, value);
965 return -EINVAL;
966 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800967 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700968 data->perms.gid = make_kgid(current_user_ns(), value);
969 if (!gid_valid(data->perms.gid)) {
970 pr_err("%s: unmapped value: %lu\n", opts, value);
971 return -EINVAL;
972 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800973 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200974 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -0800975 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200976 break;
977
978 default:
979invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100980 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200981 return -EINVAL;
982 }
983
984 /* Next iteration */
985 if (!comma)
986 break;
987 opts = comma + 1;
988 }
989
990 return 0;
991}
992
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200993/* "mount -t functionfs dev_name /dev/function" ends up here */
994
Al Virofc14f2f2010-07-25 01:48:30 +0400995static struct dentry *
996ffs_fs_mount(struct file_system_type *t, int flags,
997 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200998{
999 struct ffs_sb_fill_data data = {
1000 .perms = {
1001 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001002 .uid = GLOBAL_ROOT_UID,
1003 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001004 },
1005 .root_mode = S_IFDIR | 0500,
1006 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001007 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001008 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001009 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001010 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001011
1012 ENTER();
1013
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001014 ret = ffs_fs_parse_opts(&data, opts);
1015 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001016 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001017
Al Viro2606b282013-09-20 17:14:21 +01001018 ffs = ffs_data_new();
1019 if (unlikely(!ffs))
1020 return ERR_PTR(-ENOMEM);
1021 ffs->file_perms = data.perms;
1022
1023 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1024 if (unlikely(!ffs->dev_name)) {
1025 ffs_data_put(ffs);
1026 return ERR_PTR(-ENOMEM);
1027 }
1028
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001029 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001030 if (IS_ERR(ffs_dev)) {
1031 ffs_data_put(ffs);
1032 return ERR_CAST(ffs_dev);
1033 }
1034 ffs->private_data = ffs_dev;
1035 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001036
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001037 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001038 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001039 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001040 ffs_data_put(data.ffs_data);
1041 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001042 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001043}
1044
1045static void
1046ffs_fs_kill_sb(struct super_block *sb)
1047{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001048 ENTER();
1049
1050 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001051 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001052 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001053 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001054 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001055}
1056
1057static struct file_system_type ffs_fs_type = {
1058 .owner = THIS_MODULE,
1059 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001060 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001061 .kill_sb = ffs_fs_kill_sb,
1062};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001063MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001064
1065
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001066/* Driver's main init/cleanup functions *************************************/
1067
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001068static int functionfs_init(void)
1069{
1070 int ret;
1071
1072 ENTER();
1073
1074 ret = register_filesystem(&ffs_fs_type);
1075 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001076 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001077 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001078 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001079
1080 return ret;
1081}
1082
1083static void functionfs_cleanup(void)
1084{
1085 ENTER();
1086
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001087 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001088 unregister_filesystem(&ffs_fs_type);
1089}
1090
1091
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001092/* ffs_data and ffs_function construction and destruction code **************/
1093
1094static void ffs_data_clear(struct ffs_data *ffs);
1095static void ffs_data_reset(struct ffs_data *ffs);
1096
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001097static void ffs_data_get(struct ffs_data *ffs)
1098{
1099 ENTER();
1100
1101 atomic_inc(&ffs->ref);
1102}
1103
1104static void ffs_data_opened(struct ffs_data *ffs)
1105{
1106 ENTER();
1107
1108 atomic_inc(&ffs->ref);
1109 atomic_inc(&ffs->opened);
1110}
1111
1112static void ffs_data_put(struct ffs_data *ffs)
1113{
1114 ENTER();
1115
1116 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001117 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001118 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001119 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001120 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001121 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001122 kfree(ffs);
1123 }
1124}
1125
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001126static void ffs_data_closed(struct ffs_data *ffs)
1127{
1128 ENTER();
1129
1130 if (atomic_dec_and_test(&ffs->opened)) {
1131 ffs->state = FFS_CLOSING;
1132 ffs_data_reset(ffs);
1133 }
1134
1135 ffs_data_put(ffs);
1136}
1137
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001138static struct ffs_data *ffs_data_new(void)
1139{
1140 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1141 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001142 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001143
1144 ENTER();
1145
1146 atomic_set(&ffs->ref, 1);
1147 atomic_set(&ffs->opened, 0);
1148 ffs->state = FFS_READ_DESCRIPTORS;
1149 mutex_init(&ffs->mutex);
1150 spin_lock_init(&ffs->eps_lock);
1151 init_waitqueue_head(&ffs->ev.waitq);
1152 init_completion(&ffs->ep0req_completion);
1153
1154 /* XXX REVISIT need to update it in some places, or do we? */
1155 ffs->ev.can_stall = 1;
1156
1157 return ffs;
1158}
1159
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001160static void ffs_data_clear(struct ffs_data *ffs)
1161{
1162 ENTER();
1163
1164 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001165 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166
1167 BUG_ON(ffs->gadget);
1168
1169 if (ffs->epfiles)
1170 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1171
1172 kfree(ffs->raw_descs);
1173 kfree(ffs->raw_strings);
1174 kfree(ffs->stringtabs);
1175}
1176
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001177static void ffs_data_reset(struct ffs_data *ffs)
1178{
1179 ENTER();
1180
1181 ffs_data_clear(ffs);
1182
1183 ffs->epfiles = NULL;
1184 ffs->raw_descs = NULL;
1185 ffs->raw_strings = NULL;
1186 ffs->stringtabs = NULL;
1187
1188 ffs->raw_descs_length = 0;
1189 ffs->raw_fs_descs_length = 0;
1190 ffs->fs_descs_count = 0;
1191 ffs->hs_descs_count = 0;
1192
1193 ffs->strings_count = 0;
1194 ffs->interfaces_count = 0;
1195 ffs->eps_count = 0;
1196
1197 ffs->ev.count = 0;
1198
1199 ffs->state = FFS_READ_DESCRIPTORS;
1200 ffs->setup_state = FFS_NO_SETUP;
1201 ffs->flags = 0;
1202}
1203
1204
1205static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1206{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001207 struct usb_gadget_strings **lang;
1208 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001209
1210 ENTER();
1211
1212 if (WARN_ON(ffs->state != FFS_ACTIVE
1213 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1214 return -EBADFD;
1215
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001216 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1217 if (unlikely(first_id < 0))
1218 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001219
1220 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1221 if (unlikely(!ffs->ep0req))
1222 return -ENOMEM;
1223 ffs->ep0req->complete = ffs_ep0_complete;
1224 ffs->ep0req->context = ffs;
1225
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001226 lang = ffs->stringtabs;
1227 for (lang = ffs->stringtabs; *lang; ++lang) {
1228 struct usb_string *str = (*lang)->strings;
1229 int id = first_id;
1230 for (; str->s; ++id, ++str)
1231 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001232 }
1233
1234 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001235 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001236 return 0;
1237}
1238
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001239static void functionfs_unbind(struct ffs_data *ffs)
1240{
1241 ENTER();
1242
1243 if (!WARN_ON(!ffs->gadget)) {
1244 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1245 ffs->ep0req = NULL;
1246 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001247 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001248 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001249 }
1250}
1251
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001252static int ffs_epfiles_create(struct ffs_data *ffs)
1253{
1254 struct ffs_epfile *epfile, *epfiles;
1255 unsigned i, count;
1256
1257 ENTER();
1258
1259 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001260 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001261 if (!epfiles)
1262 return -ENOMEM;
1263
1264 epfile = epfiles;
1265 for (i = 1; i <= count; ++i, ++epfile) {
1266 epfile->ffs = ffs;
1267 mutex_init(&epfile->mutex);
1268 init_waitqueue_head(&epfile->wait);
1269 sprintf(epfiles->name, "ep%u", i);
1270 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1271 &ffs_epfile_operations,
1272 &epfile->dentry))) {
1273 ffs_epfiles_destroy(epfiles, i - 1);
1274 return -ENOMEM;
1275 }
1276 }
1277
1278 ffs->epfiles = epfiles;
1279 return 0;
1280}
1281
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001282static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1283{
1284 struct ffs_epfile *epfile = epfiles;
1285
1286 ENTER();
1287
1288 for (; count; --count, ++epfile) {
1289 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1290 waitqueue_active(&epfile->wait));
1291 if (epfile->dentry) {
1292 d_delete(epfile->dentry);
1293 dput(epfile->dentry);
1294 epfile->dentry = NULL;
1295 }
1296 }
1297
1298 kfree(epfiles);
1299}
1300
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001301
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001302static void ffs_func_eps_disable(struct ffs_function *func)
1303{
1304 struct ffs_ep *ep = func->eps;
1305 struct ffs_epfile *epfile = func->ffs->epfiles;
1306 unsigned count = func->ffs->eps_count;
1307 unsigned long flags;
1308
1309 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1310 do {
1311 /* pending requests get nuked */
1312 if (likely(ep->ep))
1313 usb_ep_disable(ep->ep);
1314 epfile->ep = NULL;
1315
1316 ++ep;
1317 ++epfile;
1318 } while (--count);
1319 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1320}
1321
1322static int ffs_func_eps_enable(struct ffs_function *func)
1323{
1324 struct ffs_data *ffs = func->ffs;
1325 struct ffs_ep *ep = func->eps;
1326 struct ffs_epfile *epfile = ffs->epfiles;
1327 unsigned count = ffs->eps_count;
1328 unsigned long flags;
1329 int ret = 0;
1330
1331 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1332 do {
1333 struct usb_endpoint_descriptor *ds;
1334 ds = ep->descs[ep->descs[1] ? 1 : 0];
1335
1336 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001337 ep->ep->desc = ds;
1338 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001339 if (likely(!ret)) {
1340 epfile->ep = ep;
1341 epfile->in = usb_endpoint_dir_in(ds);
1342 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1343 } else {
1344 break;
1345 }
1346
1347 wake_up(&epfile->wait);
1348
1349 ++ep;
1350 ++epfile;
1351 } while (--count);
1352 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1353
1354 return ret;
1355}
1356
1357
1358/* Parsing and building descriptors and strings *****************************/
1359
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001360/*
1361 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001362 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001363 * required by given configuration. Returns address after the
1364 * descriptor or NULL if data is invalid.
1365 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001366
1367enum ffs_entity_type {
1368 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1369};
1370
1371typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1372 u8 *valuep,
1373 struct usb_descriptor_header *desc,
1374 void *priv);
1375
1376static int __must_check ffs_do_desc(char *data, unsigned len,
1377 ffs_entity_callback entity, void *priv)
1378{
1379 struct usb_descriptor_header *_ds = (void *)data;
1380 u8 length;
1381 int ret;
1382
1383 ENTER();
1384
1385 /* At least two bytes are required: length and type */
1386 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001387 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001388 return -EINVAL;
1389 }
1390
1391 /* If we have at least as many bytes as the descriptor takes? */
1392 length = _ds->bLength;
1393 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001394 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001395 return -EINVAL;
1396 }
1397
1398#define __entity_check_INTERFACE(val) 1
1399#define __entity_check_STRING(val) (val)
1400#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1401#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001402 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001404 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001405 return -EINVAL; \
1406 } \
1407 ret = entity(FFS_ ##type, &val, _ds, priv); \
1408 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001409 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001410 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001411 return ret; \
1412 } \
1413 } while (0)
1414
1415 /* Parse descriptor depending on type. */
1416 switch (_ds->bDescriptorType) {
1417 case USB_DT_DEVICE:
1418 case USB_DT_CONFIG:
1419 case USB_DT_STRING:
1420 case USB_DT_DEVICE_QUALIFIER:
1421 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001422 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001423 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001424 return -EINVAL;
1425
1426 case USB_DT_INTERFACE: {
1427 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001428 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001429 if (length != sizeof *ds)
1430 goto inv_length;
1431
1432 __entity(INTERFACE, ds->bInterfaceNumber);
1433 if (ds->iInterface)
1434 __entity(STRING, ds->iInterface);
1435 }
1436 break;
1437
1438 case USB_DT_ENDPOINT: {
1439 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001440 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001441 if (length != USB_DT_ENDPOINT_SIZE &&
1442 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1443 goto inv_length;
1444 __entity(ENDPOINT, ds->bEndpointAddress);
1445 }
1446 break;
1447
Koen Beel560f1182012-05-30 20:43:37 +02001448 case HID_DT_HID:
1449 pr_vdebug("hid descriptor\n");
1450 if (length != sizeof(struct hid_descriptor))
1451 goto inv_length;
1452 break;
1453
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001454 case USB_DT_OTG:
1455 if (length != sizeof(struct usb_otg_descriptor))
1456 goto inv_length;
1457 break;
1458
1459 case USB_DT_INTERFACE_ASSOCIATION: {
1460 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001461 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001462 if (length != sizeof *ds)
1463 goto inv_length;
1464 if (ds->iFunction)
1465 __entity(STRING, ds->iFunction);
1466 }
1467 break;
1468
1469 case USB_DT_OTHER_SPEED_CONFIG:
1470 case USB_DT_INTERFACE_POWER:
1471 case USB_DT_DEBUG:
1472 case USB_DT_SECURITY:
1473 case USB_DT_CS_RADIO_CONTROL:
1474 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001475 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001476 return -EINVAL;
1477
1478 default:
1479 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001480 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001481 return -EINVAL;
1482
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001483inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001484 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001485 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001486 return -EINVAL;
1487 }
1488
1489#undef __entity
1490#undef __entity_check_DESCRIPTOR
1491#undef __entity_check_INTERFACE
1492#undef __entity_check_STRING
1493#undef __entity_check_ENDPOINT
1494
1495 return length;
1496}
1497
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001498static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1499 ffs_entity_callback entity, void *priv)
1500{
1501 const unsigned _len = len;
1502 unsigned long num = 0;
1503
1504 ENTER();
1505
1506 for (;;) {
1507 int ret;
1508
1509 if (num == count)
1510 data = NULL;
1511
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001512 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001513 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1514 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001515 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001516 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001517 return ret;
1518 }
1519
1520 if (!data)
1521 return _len - len;
1522
1523 ret = ffs_do_desc(data, len, entity, priv);
1524 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001525 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001526 return ret;
1527 }
1528
1529 len -= ret;
1530 data += ret;
1531 ++num;
1532 }
1533}
1534
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001535static int __ffs_data_do_entity(enum ffs_entity_type type,
1536 u8 *valuep, struct usb_descriptor_header *desc,
1537 void *priv)
1538{
1539 struct ffs_data *ffs = priv;
1540
1541 ENTER();
1542
1543 switch (type) {
1544 case FFS_DESCRIPTOR:
1545 break;
1546
1547 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001548 /*
1549 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001550 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001551 * "n+1" interfaces.
1552 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001553 if (*valuep >= ffs->interfaces_count)
1554 ffs->interfaces_count = *valuep + 1;
1555 break;
1556
1557 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001558 /*
1559 * Strings are indexed from 1 (0 is magic ;) reserved
1560 * for languages list or some such)
1561 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001562 if (*valuep > ffs->strings_count)
1563 ffs->strings_count = *valuep;
1564 break;
1565
1566 case FFS_ENDPOINT:
1567 /* Endpoints are indexed from 1 as well. */
1568 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1569 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1570 break;
1571 }
1572
1573 return 0;
1574}
1575
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001576static int __ffs_data_got_descs(struct ffs_data *ffs,
1577 char *const _data, size_t len)
1578{
1579 unsigned fs_count, hs_count;
1580 int fs_len, ret = -EINVAL;
1581 char *data = _data;
1582
1583 ENTER();
1584
1585 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1586 get_unaligned_le32(data + 4) != len))
1587 goto error;
1588 fs_count = get_unaligned_le32(data + 8);
1589 hs_count = get_unaligned_le32(data + 12);
1590
1591 if (!fs_count && !hs_count)
1592 goto einval;
1593
1594 data += 16;
1595 len -= 16;
1596
1597 if (likely(fs_count)) {
1598 fs_len = ffs_do_descs(fs_count, data, len,
1599 __ffs_data_do_entity, ffs);
1600 if (unlikely(fs_len < 0)) {
1601 ret = fs_len;
1602 goto error;
1603 }
1604
1605 data += fs_len;
1606 len -= fs_len;
1607 } else {
1608 fs_len = 0;
1609 }
1610
1611 if (likely(hs_count)) {
1612 ret = ffs_do_descs(hs_count, data, len,
1613 __ffs_data_do_entity, ffs);
1614 if (unlikely(ret < 0))
1615 goto error;
1616 } else {
1617 ret = 0;
1618 }
1619
1620 if (unlikely(len != ret))
1621 goto einval;
1622
1623 ffs->raw_fs_descs_length = fs_len;
1624 ffs->raw_descs_length = fs_len + ret;
1625 ffs->raw_descs = _data;
1626 ffs->fs_descs_count = fs_count;
1627 ffs->hs_descs_count = hs_count;
1628
1629 return 0;
1630
1631einval:
1632 ret = -EINVAL;
1633error:
1634 kfree(_data);
1635 return ret;
1636}
1637
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001638static int __ffs_data_got_strings(struct ffs_data *ffs,
1639 char *const _data, size_t len)
1640{
1641 u32 str_count, needed_count, lang_count;
1642 struct usb_gadget_strings **stringtabs, *t;
1643 struct usb_string *strings, *s;
1644 const char *data = _data;
1645
1646 ENTER();
1647
1648 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1649 get_unaligned_le32(data + 4) != len))
1650 goto error;
1651 str_count = get_unaligned_le32(data + 8);
1652 lang_count = get_unaligned_le32(data + 12);
1653
1654 /* if one is zero the other must be zero */
1655 if (unlikely(!str_count != !lang_count))
1656 goto error;
1657
1658 /* Do we have at least as many strings as descriptors need? */
1659 needed_count = ffs->strings_count;
1660 if (unlikely(str_count < needed_count))
1661 goto error;
1662
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001663 /*
1664 * If we don't need any strings just return and free all
1665 * memory.
1666 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001667 if (!needed_count) {
1668 kfree(_data);
1669 return 0;
1670 }
1671
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001672 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001673 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001674 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001675 vla_group(d);
1676 vla_item(d, struct usb_gadget_strings *, stringtabs,
1677 lang_count + 1);
1678 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
1679 vla_item(d, struct usb_string, strings,
1680 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001681
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001682 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
1683
1684 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001685 kfree(_data);
1686 return -ENOMEM;
1687 }
1688
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001689 /* Initialize the VLA pointers */
1690 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1691 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001692 i = lang_count;
1693 do {
1694 *stringtabs++ = t++;
1695 } while (--i);
1696 *stringtabs = NULL;
1697
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001698 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1699 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1700 t = vla_ptr(vlabuf, d, stringtab);
1701 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001702 strings = s;
1703 }
1704
1705 /* For each language */
1706 data += 16;
1707 len -= 16;
1708
1709 do { /* lang_count > 0 so we can use do-while */
1710 unsigned needed = needed_count;
1711
1712 if (unlikely(len < 3))
1713 goto error_free;
1714 t->language = get_unaligned_le16(data);
1715 t->strings = s;
1716 ++t;
1717
1718 data += 2;
1719 len -= 2;
1720
1721 /* For each string */
1722 do { /* str_count > 0 so we can use do-while */
1723 size_t length = strnlen(data, len);
1724
1725 if (unlikely(length == len))
1726 goto error_free;
1727
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001728 /*
1729 * User may provide more strings then we need,
1730 * if that's the case we simply ignore the
1731 * rest
1732 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001733 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001734 /*
1735 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001736 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001737 * now just leave garbage here.
1738 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001739 s->s = data;
1740 --needed;
1741 ++s;
1742 }
1743
1744 data += length + 1;
1745 len -= length + 1;
1746 } while (--str_count);
1747
1748 s->id = 0; /* terminator */
1749 s->s = NULL;
1750 ++s;
1751
1752 } while (--lang_count);
1753
1754 /* Some garbage left? */
1755 if (unlikely(len))
1756 goto error_free;
1757
1758 /* Done! */
1759 ffs->stringtabs = stringtabs;
1760 ffs->raw_strings = _data;
1761
1762 return 0;
1763
1764error_free:
1765 kfree(stringtabs);
1766error:
1767 kfree(_data);
1768 return -EINVAL;
1769}
1770
1771
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001772/* Events handling and management *******************************************/
1773
1774static void __ffs_event_add(struct ffs_data *ffs,
1775 enum usb_functionfs_event_type type)
1776{
1777 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1778 int neg = 0;
1779
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001780 /*
1781 * Abort any unhandled setup
1782 *
1783 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001784 * of ffs->setup_state without holding the lock because when
1785 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001786 * the source does nothing.
1787 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001788 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01001789 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001790
1791 switch (type) {
1792 case FUNCTIONFS_RESUME:
1793 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001794 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001795 case FUNCTIONFS_SUSPEND:
1796 case FUNCTIONFS_SETUP:
1797 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001798 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001799 break;
1800
1801 case FUNCTIONFS_BIND:
1802 case FUNCTIONFS_UNBIND:
1803 case FUNCTIONFS_DISABLE:
1804 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001805 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001806 rem_type1 = FUNCTIONFS_SUSPEND;
1807 rem_type2 = FUNCTIONFS_RESUME;
1808 neg = 1;
1809 break;
1810
1811 default:
1812 BUG();
1813 }
1814
1815 {
1816 u8 *ev = ffs->ev.types, *out = ev;
1817 unsigned n = ffs->ev.count;
1818 for (; n; --n, ++ev)
1819 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
1820 *out++ = *ev;
1821 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001822 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001823 ffs->ev.count = out - ffs->ev.types;
1824 }
1825
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001826 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001827 ffs->ev.types[ffs->ev.count++] = type;
1828 wake_up_locked(&ffs->ev.waitq);
1829}
1830
1831static void ffs_event_add(struct ffs_data *ffs,
1832 enum usb_functionfs_event_type type)
1833{
1834 unsigned long flags;
1835 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
1836 __ffs_event_add(ffs, type);
1837 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
1838}
1839
1840
1841/* Bind/unbind USB function hooks *******************************************/
1842
1843static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1844 struct usb_descriptor_header *desc,
1845 void *priv)
1846{
1847 struct usb_endpoint_descriptor *ds = (void *)desc;
1848 struct ffs_function *func = priv;
1849 struct ffs_ep *ffs_ep;
1850
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001851 /*
1852 * If hs_descriptors is not NULL then we are reading hs
1853 * descriptors now
1854 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001855 const int isHS = func->function.hs_descriptors != NULL;
1856 unsigned idx;
1857
1858 if (type != FFS_DESCRIPTOR)
1859 return 0;
1860
1861 if (isHS)
1862 func->function.hs_descriptors[(long)valuep] = desc;
1863 else
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001864 func->function.fs_descriptors[(long)valuep] = desc;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001865
1866 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1867 return 0;
1868
1869 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
1870 ffs_ep = func->eps + idx;
1871
1872 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001873 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001874 isHS ? "high" : "full",
1875 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001876 return -EINVAL;
1877 }
1878 ffs_ep->descs[isHS] = ds;
1879
1880 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
1881 if (ffs_ep->ep) {
1882 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
1883 if (!ds->wMaxPacketSize)
1884 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
1885 } else {
1886 struct usb_request *req;
1887 struct usb_ep *ep;
1888
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001889 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001890 ep = usb_ep_autoconfig(func->gadget, ds);
1891 if (unlikely(!ep))
1892 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08001893 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001894
1895 req = usb_ep_alloc_request(ep, GFP_KERNEL);
1896 if (unlikely(!req))
1897 return -ENOMEM;
1898
1899 ffs_ep->ep = ep;
1900 ffs_ep->req = req;
1901 func->eps_revmap[ds->bEndpointAddress &
1902 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
1903 }
1904 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
1905
1906 return 0;
1907}
1908
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001909static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
1910 struct usb_descriptor_header *desc,
1911 void *priv)
1912{
1913 struct ffs_function *func = priv;
1914 unsigned idx;
1915 u8 newValue;
1916
1917 switch (type) {
1918 default:
1919 case FFS_DESCRIPTOR:
1920 /* Handled in previous pass by __ffs_func_bind_do_descs() */
1921 return 0;
1922
1923 case FFS_INTERFACE:
1924 idx = *valuep;
1925 if (func->interfaces_nums[idx] < 0) {
1926 int id = usb_interface_id(func->conf, &func->function);
1927 if (unlikely(id < 0))
1928 return id;
1929 func->interfaces_nums[idx] = id;
1930 }
1931 newValue = func->interfaces_nums[idx];
1932 break;
1933
1934 case FFS_STRING:
1935 /* String' IDs are allocated when fsf_data is bound to cdev */
1936 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
1937 break;
1938
1939 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001940 /*
1941 * USB_DT_ENDPOINT are handled in
1942 * __ffs_func_bind_do_descs().
1943 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001944 if (desc->bDescriptorType == USB_DT_ENDPOINT)
1945 return 0;
1946
1947 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
1948 if (unlikely(!func->eps[idx].ep))
1949 return -EINVAL;
1950
1951 {
1952 struct usb_endpoint_descriptor **descs;
1953 descs = func->eps[idx].descs;
1954 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
1955 }
1956 break;
1957 }
1958
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001959 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001960 *valuep = newValue;
1961 return 0;
1962}
1963
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001964static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
1965 struct usb_configuration *c)
1966{
1967 struct ffs_function *func = ffs_func_from_usb(f);
1968 struct f_fs_opts *ffs_opts =
1969 container_of(f->fi, struct f_fs_opts, func_inst);
1970 int ret;
1971
1972 ENTER();
1973
1974 /*
1975 * Legacy gadget triggers binding in functionfs_ready_callback,
1976 * which already uses locking; taking the same lock here would
1977 * cause a deadlock.
1978 *
1979 * Configfs-enabled gadgets however do need ffs_dev_lock.
1980 */
1981 if (!ffs_opts->no_configfs)
1982 ffs_dev_lock();
1983 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
1984 func->ffs = ffs_opts->dev->ffs_data;
1985 if (!ffs_opts->no_configfs)
1986 ffs_dev_unlock();
1987 if (ret)
1988 return ERR_PTR(ret);
1989
1990 func->conf = c;
1991 func->gadget = c->cdev->gadget;
1992
1993 ffs_data_get(func->ffs);
1994
1995 /*
1996 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1997 * configurations are bound in sequence with list_for_each_entry,
1998 * in each configuration its functions are bound in sequence
1999 * with list_for_each_entry, so we assume no race condition
2000 * with regard to ffs_opts->bound access
2001 */
2002 if (!ffs_opts->refcnt) {
2003 ret = functionfs_bind(func->ffs, c->cdev);
2004 if (ret)
2005 return ERR_PTR(ret);
2006 }
2007 ffs_opts->refcnt++;
2008 func->function.strings = func->ffs->stringtabs;
2009
2010 return ffs_opts;
2011}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002012
2013static int _ffs_func_bind(struct usb_configuration *c,
2014 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002015{
2016 struct ffs_function *func = ffs_func_from_usb(f);
2017 struct ffs_data *ffs = func->ffs;
2018
2019 const int full = !!func->ffs->fs_descs_count;
2020 const int high = gadget_is_dualspeed(func->gadget) &&
2021 func->ffs->hs_descs_count;
2022
2023 int ret;
2024
2025 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002026 vla_group(d);
2027 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2028 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2029 full ? ffs->fs_descs_count + 1 : 0);
2030 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2031 high ? ffs->hs_descs_count + 1 : 0);
2032 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2033 vla_item_with_sz(d, char, raw_descs,
2034 high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2035 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002036
2037 ENTER();
2038
2039 /* Only high speed but not supported by gadget? */
2040 if (unlikely(!(full | high)))
2041 return -ENOTSUPP;
2042
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002043 /* Allocate a single chunk, less management later on */
2044 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2045 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002046 return -ENOMEM;
2047
2048 /* Zero */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002049 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2050 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
2051 d_raw_descs__sz);
2052 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2053 for (ret = ffs->eps_count; ret; --ret) {
2054 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002055
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002056 ptr = vla_ptr(vlabuf, d, eps);
2057 ptr[ret].num = -1;
2058 }
2059
2060 /* Save pointers
2061 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2062 */
2063 func->eps = vla_ptr(vlabuf, d, eps);
2064 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002065
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002066 /*
2067 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002068 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002069 * numbers without worrying that it may be described later on.
2070 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002071 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002072 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002073 ret = ffs_do_descs(ffs->fs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002074 vla_ptr(vlabuf, d, raw_descs),
2075 d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002076 __ffs_func_bind_do_descs, func);
2077 if (unlikely(ret < 0))
2078 goto error;
2079 } else {
2080 ret = 0;
2081 }
2082
2083 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002084 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002085 ret = ffs_do_descs(ffs->hs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002086 vla_ptr(vlabuf, d, raw_descs) + ret,
2087 d_raw_descs__sz - ret,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002088 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002089 if (unlikely(ret < 0))
2090 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002091 }
2092
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002093 /*
2094 * Now handle interface numbers allocation and interface and
2095 * endpoint numbers rewriting. We can do that in one go
2096 * now.
2097 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002098 ret = ffs_do_descs(ffs->fs_descs_count +
2099 (high ? ffs->hs_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002100 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002101 __ffs_func_bind_do_nums, func);
2102 if (unlikely(ret < 0))
2103 goto error;
2104
2105 /* And we're done */
2106 ffs_event_add(ffs, FUNCTIONFS_BIND);
2107 return 0;
2108
2109error:
2110 /* XXX Do we need to release all claimed endpoints here? */
2111 return ret;
2112}
2113
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002114static int ffs_func_bind(struct usb_configuration *c,
2115 struct usb_function *f)
2116{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002117 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2118
2119 if (IS_ERR(ffs_opts))
2120 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002121
2122 return _ffs_func_bind(c, f);
2123}
2124
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002125
2126/* Other USB function hooks *************************************************/
2127
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002128static int ffs_func_set_alt(struct usb_function *f,
2129 unsigned interface, unsigned alt)
2130{
2131 struct ffs_function *func = ffs_func_from_usb(f);
2132 struct ffs_data *ffs = func->ffs;
2133 int ret = 0, intf;
2134
2135 if (alt != (unsigned)-1) {
2136 intf = ffs_func_revmap_intf(func, interface);
2137 if (unlikely(intf < 0))
2138 return intf;
2139 }
2140
2141 if (ffs->func)
2142 ffs_func_eps_disable(ffs->func);
2143
2144 if (ffs->state != FFS_ACTIVE)
2145 return -ENODEV;
2146
2147 if (alt == (unsigned)-1) {
2148 ffs->func = NULL;
2149 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2150 return 0;
2151 }
2152
2153 ffs->func = func;
2154 ret = ffs_func_eps_enable(func);
2155 if (likely(ret >= 0))
2156 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2157 return ret;
2158}
2159
2160static void ffs_func_disable(struct usb_function *f)
2161{
2162 ffs_func_set_alt(f, 0, (unsigned)-1);
2163}
2164
2165static int ffs_func_setup(struct usb_function *f,
2166 const struct usb_ctrlrequest *creq)
2167{
2168 struct ffs_function *func = ffs_func_from_usb(f);
2169 struct ffs_data *ffs = func->ffs;
2170 unsigned long flags;
2171 int ret;
2172
2173 ENTER();
2174
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002175 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2176 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2177 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2178 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2179 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002180
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002181 /*
2182 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002183 * (notable exceptions are set/get interface) so we need to
2184 * handle them. All other either handled by composite or
2185 * passed to usb_configuration->setup() (if one is set). No
2186 * matter, we will handle requests directed to endpoint here
2187 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002188 * other request?
2189 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002190 if (ffs->state != FFS_ACTIVE)
2191 return -ENODEV;
2192
2193 switch (creq->bRequestType & USB_RECIP_MASK) {
2194 case USB_RECIP_INTERFACE:
2195 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2196 if (unlikely(ret < 0))
2197 return ret;
2198 break;
2199
2200 case USB_RECIP_ENDPOINT:
2201 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2202 if (unlikely(ret < 0))
2203 return ret;
2204 break;
2205
2206 default:
2207 return -EOPNOTSUPP;
2208 }
2209
2210 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2211 ffs->ev.setup = *creq;
2212 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2213 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2214 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2215
2216 return 0;
2217}
2218
2219static void ffs_func_suspend(struct usb_function *f)
2220{
2221 ENTER();
2222 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2223}
2224
2225static void ffs_func_resume(struct usb_function *f)
2226{
2227 ENTER();
2228 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2229}
2230
2231
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002232/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002233
2234static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2235{
2236 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2237 return num ? num : -EDOM;
2238}
2239
2240static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2241{
2242 short *nums = func->interfaces_nums;
2243 unsigned count = func->ffs->interfaces_count;
2244
2245 for (; count; --count, ++nums) {
2246 if (*nums >= 0 && *nums == intf)
2247 return nums - func->interfaces_nums;
2248 }
2249
2250 return -EDOM;
2251}
2252
2253
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002254/* Devices management *******************************************************/
2255
2256static LIST_HEAD(ffs_devices);
2257
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002258static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002259{
2260 struct ffs_dev *dev;
2261
2262 list_for_each_entry(dev, &ffs_devices, entry) {
2263 if (!dev->name || !name)
2264 continue;
2265 if (strcmp(dev->name, name) == 0)
2266 return dev;
2267 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002268
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002269 return NULL;
2270}
2271
2272/*
2273 * ffs_lock must be taken by the caller of this function
2274 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002275static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002276{
2277 struct ffs_dev *dev;
2278
2279 if (list_is_singular(&ffs_devices)) {
2280 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2281 if (dev->single)
2282 return dev;
2283 }
2284
2285 return NULL;
2286}
2287
2288/*
2289 * ffs_lock must be taken by the caller of this function
2290 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002291static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002292{
2293 struct ffs_dev *dev;
2294
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002295 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002296 if (dev)
2297 return dev;
2298
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002299 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002300}
2301
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002302/* Configfs support *********************************************************/
2303
2304static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2305{
2306 return container_of(to_config_group(item), struct f_fs_opts,
2307 func_inst.group);
2308}
2309
2310static void ffs_attr_release(struct config_item *item)
2311{
2312 struct f_fs_opts *opts = to_ffs_opts(item);
2313
2314 usb_put_function_instance(&opts->func_inst);
2315}
2316
2317static struct configfs_item_operations ffs_item_ops = {
2318 .release = ffs_attr_release,
2319};
2320
2321static struct config_item_type ffs_func_type = {
2322 .ct_item_ops = &ffs_item_ops,
2323 .ct_owner = THIS_MODULE,
2324};
2325
2326
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002327/* Function registration interface ******************************************/
2328
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002329static void ffs_free_inst(struct usb_function_instance *f)
2330{
2331 struct f_fs_opts *opts;
2332
2333 opts = to_f_fs_opts(f);
2334 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002335 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002336 ffs_dev_unlock();
2337 kfree(opts);
2338}
2339
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002340#define MAX_INST_NAME_LEN 40
2341
2342static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2343{
2344 struct f_fs_opts *opts;
2345 char *ptr;
2346 const char *tmp;
2347 int name_len, ret;
2348
2349 name_len = strlen(name) + 1;
2350 if (name_len > MAX_INST_NAME_LEN)
2351 return -ENAMETOOLONG;
2352
2353 ptr = kstrndup(name, name_len, GFP_KERNEL);
2354 if (!ptr)
2355 return -ENOMEM;
2356
2357 opts = to_f_fs_opts(fi);
2358 tmp = NULL;
2359
2360 ffs_dev_lock();
2361
2362 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2363 ret = _ffs_name_dev(opts->dev, ptr);
2364 if (ret) {
2365 kfree(ptr);
2366 ffs_dev_unlock();
2367 return ret;
2368 }
2369 opts->dev->name_allocated = true;
2370
2371 ffs_dev_unlock();
2372
2373 kfree(tmp);
2374
2375 return 0;
2376}
2377
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002378static struct usb_function_instance *ffs_alloc_inst(void)
2379{
2380 struct f_fs_opts *opts;
2381 struct ffs_dev *dev;
2382
2383 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2384 if (!opts)
2385 return ERR_PTR(-ENOMEM);
2386
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002387 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002388 opts->func_inst.free_func_inst = ffs_free_inst;
2389 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002390 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002391 ffs_dev_unlock();
2392 if (IS_ERR(dev)) {
2393 kfree(opts);
2394 return ERR_CAST(dev);
2395 }
2396 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002397 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002398
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002399 config_group_init_type_name(&opts->func_inst.group, "",
2400 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002401 return &opts->func_inst;
2402}
2403
2404static void ffs_free(struct usb_function *f)
2405{
2406 kfree(ffs_func_from_usb(f));
2407}
2408
2409static void ffs_func_unbind(struct usb_configuration *c,
2410 struct usb_function *f)
2411{
2412 struct ffs_function *func = ffs_func_from_usb(f);
2413 struct ffs_data *ffs = func->ffs;
2414 struct f_fs_opts *opts =
2415 container_of(f->fi, struct f_fs_opts, func_inst);
2416 struct ffs_ep *ep = func->eps;
2417 unsigned count = ffs->eps_count;
2418 unsigned long flags;
2419
2420 ENTER();
2421 if (ffs->func == func) {
2422 ffs_func_eps_disable(func);
2423 ffs->func = NULL;
2424 }
2425
2426 if (!--opts->refcnt)
2427 functionfs_unbind(ffs);
2428
2429 /* cleanup after autoconfig */
2430 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2431 do {
2432 if (ep->ep && ep->req)
2433 usb_ep_free_request(ep->ep, ep->req);
2434 ep->req = NULL;
2435 ++ep;
2436 } while (--count);
2437 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2438 kfree(func->eps);
2439 func->eps = NULL;
2440 /*
2441 * eps, descriptors and interfaces_nums are allocated in the
2442 * same chunk so only one free is required.
2443 */
2444 func->function.fs_descriptors = NULL;
2445 func->function.hs_descriptors = NULL;
2446 func->interfaces_nums = NULL;
2447
2448 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2449}
2450
2451static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2452{
2453 struct ffs_function *func;
2454
2455 ENTER();
2456
2457 func = kzalloc(sizeof(*func), GFP_KERNEL);
2458 if (unlikely(!func))
2459 return ERR_PTR(-ENOMEM);
2460
2461 func->function.name = "Function FS Gadget";
2462
2463 func->function.bind = ffs_func_bind;
2464 func->function.unbind = ffs_func_unbind;
2465 func->function.set_alt = ffs_func_set_alt;
2466 func->function.disable = ffs_func_disable;
2467 func->function.setup = ffs_func_setup;
2468 func->function.suspend = ffs_func_suspend;
2469 func->function.resume = ffs_func_resume;
2470 func->function.free_func = ffs_free;
2471
2472 return &func->function;
2473}
2474
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002475/*
2476 * ffs_lock must be taken by the caller of this function
2477 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002478static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002479{
2480 struct ffs_dev *dev;
2481 int ret;
2482
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002483 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002484 return ERR_PTR(-EBUSY);
2485
2486 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2487 if (!dev)
2488 return ERR_PTR(-ENOMEM);
2489
2490 if (list_empty(&ffs_devices)) {
2491 ret = functionfs_init();
2492 if (ret) {
2493 kfree(dev);
2494 return ERR_PTR(ret);
2495 }
2496 }
2497
2498 list_add(&dev->entry, &ffs_devices);
2499
2500 return dev;
2501}
2502
2503/*
2504 * ffs_lock must be taken by the caller of this function
2505 * The caller is responsible for "name" being available whenever f_fs needs it
2506 */
2507static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2508{
2509 struct ffs_dev *existing;
2510
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002511 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002512 if (existing)
2513 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01002514
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002515 dev->name = name;
2516
2517 return 0;
2518}
2519
2520/*
2521 * The caller is responsible for "name" being available whenever f_fs needs it
2522 */
2523int ffs_name_dev(struct ffs_dev *dev, const char *name)
2524{
2525 int ret;
2526
2527 ffs_dev_lock();
2528 ret = _ffs_name_dev(dev, name);
2529 ffs_dev_unlock();
2530
2531 return ret;
2532}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002533EXPORT_SYMBOL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002534
2535int ffs_single_dev(struct ffs_dev *dev)
2536{
2537 int ret;
2538
2539 ret = 0;
2540 ffs_dev_lock();
2541
2542 if (!list_is_singular(&ffs_devices))
2543 ret = -EBUSY;
2544 else
2545 dev->single = true;
2546
2547 ffs_dev_unlock();
2548 return ret;
2549}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002550EXPORT_SYMBOL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002551
2552/*
2553 * ffs_lock must be taken by the caller of this function
2554 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002555static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002556{
2557 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002558 if (dev->name_allocated)
2559 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002560 kfree(dev);
2561 if (list_empty(&ffs_devices))
2562 functionfs_cleanup();
2563}
2564
2565static void *ffs_acquire_dev(const char *dev_name)
2566{
2567 struct ffs_dev *ffs_dev;
2568
2569 ENTER();
2570 ffs_dev_lock();
2571
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002572 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002573 if (!ffs_dev)
2574 ffs_dev = ERR_PTR(-ENODEV);
2575 else if (ffs_dev->mounted)
2576 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002577 else if (ffs_dev->ffs_acquire_dev_callback &&
2578 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2579 ffs_dev = ERR_PTR(-ENODEV);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002580 else
2581 ffs_dev->mounted = true;
2582
2583 ffs_dev_unlock();
2584 return ffs_dev;
2585}
2586
2587static void ffs_release_dev(struct ffs_data *ffs_data)
2588{
2589 struct ffs_dev *ffs_dev;
2590
2591 ENTER();
2592 ffs_dev_lock();
2593
2594 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002595 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002596 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002597
2598 if (ffs_dev->ffs_release_dev_callback)
2599 ffs_dev->ffs_release_dev_callback(ffs_dev);
2600 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002601
2602 ffs_dev_unlock();
2603}
2604
2605static int ffs_ready(struct ffs_data *ffs)
2606{
2607 struct ffs_dev *ffs_obj;
2608 int ret = 0;
2609
2610 ENTER();
2611 ffs_dev_lock();
2612
2613 ffs_obj = ffs->private_data;
2614 if (!ffs_obj) {
2615 ret = -EINVAL;
2616 goto done;
2617 }
2618 if (WARN_ON(ffs_obj->desc_ready)) {
2619 ret = -EBUSY;
2620 goto done;
2621 }
2622
2623 ffs_obj->desc_ready = true;
2624 ffs_obj->ffs_data = ffs;
2625
2626 if (ffs_obj->ffs_ready_callback)
2627 ret = ffs_obj->ffs_ready_callback(ffs);
2628
2629done:
2630 ffs_dev_unlock();
2631 return ret;
2632}
2633
2634static void ffs_closed(struct ffs_data *ffs)
2635{
2636 struct ffs_dev *ffs_obj;
2637
2638 ENTER();
2639 ffs_dev_lock();
2640
2641 ffs_obj = ffs->private_data;
2642 if (!ffs_obj)
2643 goto done;
2644
2645 ffs_obj->desc_ready = false;
2646
2647 if (ffs_obj->ffs_closed_callback)
2648 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002649
2650 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
2651 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
2652 goto done;
2653
2654 unregister_gadget_item(ffs_obj->opts->
2655 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002656done:
2657 ffs_dev_unlock();
2658}
2659
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002660/* Misc helper functions ****************************************************/
2661
2662static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2663{
2664 return nonblock
2665 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2666 : mutex_lock_interruptible(mutex);
2667}
2668
Al Viro260ef312012-09-26 21:43:45 -04002669static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002670{
2671 char *data;
2672
2673 if (unlikely(!len))
2674 return NULL;
2675
2676 data = kmalloc(len, GFP_KERNEL);
2677 if (unlikely(!data))
2678 return ERR_PTR(-ENOMEM);
2679
2680 if (unlikely(__copy_from_user(data, buf, len))) {
2681 kfree(data);
2682 return ERR_PTR(-EFAULT);
2683 }
2684
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002685 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002686 ffs_dump_mem("", data, len);
2687
2688 return data;
2689}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002690
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002691DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
2692MODULE_LICENSE("GPL");
2693MODULE_AUTHOR("Michal Nazarewicz");