blob: 5bfacf8b9e6e3144f171ac6216d4787404656066 [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 Nazarewicza7ecf052014-02-10 10:42:41 +0100102static inline enum ffs_setup_state
103ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
104{
105 return (enum ffs_setup_state)
106 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
107}
108
109
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200110static void ffs_func_eps_disable(struct ffs_function *func);
111static int __must_check ffs_func_eps_enable(struct ffs_function *func);
112
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200113static int ffs_func_bind(struct usb_configuration *,
114 struct usb_function *);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200115static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
116static void ffs_func_disable(struct usb_function *);
117static int ffs_func_setup(struct usb_function *,
118 const struct usb_ctrlrequest *);
119static void ffs_func_suspend(struct usb_function *);
120static void ffs_func_resume(struct usb_function *);
121
122
123static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
124static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
125
126
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200127/* The endpoints structures *************************************************/
128
129struct ffs_ep {
130 struct usb_ep *ep; /* P: ffs->eps_lock */
131 struct usb_request *req; /* P: epfile->mutex */
132
133 /* [0]: full speed, [1]: high speed */
134 struct usb_endpoint_descriptor *descs[2];
135
136 u8 num;
137
138 int status; /* P: epfile->mutex */
139};
140
141struct ffs_epfile {
142 /* Protects ep->ep and ep->req. */
143 struct mutex mutex;
144 wait_queue_head_t wait;
145
146 struct ffs_data *ffs;
147 struct ffs_ep *ep; /* P: ffs->eps_lock */
148
149 struct dentry *dentry;
150
151 char name[5];
152
153 unsigned char in; /* P: ffs->eps_lock */
154 unsigned char isoc; /* P: ffs->eps_lock */
155
156 unsigned char _pad;
157};
158
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200159static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
160static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
161
162static struct inode *__must_check
163ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
164 const struct file_operations *fops,
165 struct dentry **dentry_p);
166
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100167/* Devices management *******************************************************/
168
169DEFINE_MUTEX(ffs_lock);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100170EXPORT_SYMBOL(ffs_lock);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100171
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100172static struct ffs_dev *_ffs_find_dev(const char *name);
173static struct ffs_dev *_ffs_alloc_dev(void);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +0100174static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100175static void _ffs_free_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100176static void *ffs_acquire_dev(const char *dev_name);
177static void ffs_release_dev(struct ffs_data *ffs_data);
178static int ffs_ready(struct ffs_data *ffs);
179static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200180
181/* Misc helper functions ****************************************************/
182
183static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
184 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400185static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200186 __attribute__((warn_unused_result, nonnull));
187
188
189/* Control file aka ep0 *****************************************************/
190
191static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
192{
193 struct ffs_data *ffs = req->context;
194
195 complete_all(&ffs->ep0req_completion);
196}
197
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200198static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
199{
200 struct usb_request *req = ffs->ep0req;
201 int ret;
202
203 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
204
205 spin_unlock_irq(&ffs->ev.waitq.lock);
206
207 req->buf = data;
208 req->length = len;
209
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100210 /*
211 * UDC layer requires to provide a buffer even for ZLP, but should
212 * not use it at all. Let's provide some poisoned pointer to catch
213 * possible bug in the driver.
214 */
215 if (req->buf == NULL)
216 req->buf = (void *)0xDEADBABE;
217
Wolfram Sang16735d02013-11-14 14:32:02 -0800218 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200219
220 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
221 if (unlikely(ret < 0))
222 return ret;
223
224 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
225 if (unlikely(ret)) {
226 usb_ep_dequeue(ffs->gadget->ep0, req);
227 return -EINTR;
228 }
229
230 ffs->setup_state = FFS_NO_SETUP;
231 return ffs->ep0req_status;
232}
233
234static int __ffs_ep0_stall(struct ffs_data *ffs)
235{
236 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100237 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200238 usb_ep_set_halt(ffs->gadget->ep0);
239 ffs->setup_state = FFS_NO_SETUP;
240 return -EL2HLT;
241 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100242 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200243 return -ESRCH;
244 }
245}
246
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200247static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
248 size_t len, loff_t *ptr)
249{
250 struct ffs_data *ffs = file->private_data;
251 ssize_t ret;
252 char *data;
253
254 ENTER();
255
256 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100257 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200258 return -EIDRM;
259
260 /* Acquire mutex */
261 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
262 if (unlikely(ret < 0))
263 return ret;
264
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200265 /* Check state */
266 switch (ffs->state) {
267 case FFS_READ_DESCRIPTORS:
268 case FFS_READ_STRINGS:
269 /* Copy data */
270 if (unlikely(len < 16)) {
271 ret = -EINVAL;
272 break;
273 }
274
275 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100276 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200277 ret = PTR_ERR(data);
278 break;
279 }
280
281 /* Handle data */
282 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100283 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200284 ret = __ffs_data_got_descs(ffs, data, len);
285 if (unlikely(ret < 0))
286 break;
287
288 ffs->state = FFS_READ_STRINGS;
289 ret = len;
290 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100291 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200292 ret = __ffs_data_got_strings(ffs, data, len);
293 if (unlikely(ret < 0))
294 break;
295
296 ret = ffs_epfiles_create(ffs);
297 if (unlikely(ret)) {
298 ffs->state = FFS_CLOSING;
299 break;
300 }
301
302 ffs->state = FFS_ACTIVE;
303 mutex_unlock(&ffs->mutex);
304
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100305 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200306 if (unlikely(ret < 0)) {
307 ffs->state = FFS_CLOSING;
308 return ret;
309 }
310
311 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
312 return len;
313 }
314 break;
315
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200316 case FFS_ACTIVE:
317 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100318 /*
319 * We're called from user space, we can use _irq
320 * rather then _irqsave
321 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200322 spin_lock_irq(&ffs->ev.waitq.lock);
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100323 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100324 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200325 ret = -EIDRM;
326 goto done_spin;
327
328 case FFS_NO_SETUP:
329 ret = -ESRCH;
330 goto done_spin;
331
332 case FFS_SETUP_PENDING:
333 break;
334 }
335
336 /* FFS_SETUP_PENDING */
337 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
338 spin_unlock_irq(&ffs->ev.waitq.lock);
339 ret = __ffs_ep0_stall(ffs);
340 break;
341 }
342
343 /* FFS_SETUP_PENDING and not stall */
344 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
345
346 spin_unlock_irq(&ffs->ev.waitq.lock);
347
348 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100349 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200350 ret = PTR_ERR(data);
351 break;
352 }
353
354 spin_lock_irq(&ffs->ev.waitq.lock);
355
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100356 /*
357 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200358 * but the state of setup could have changed from
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100359 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200360 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100361 * from user space in vain but it's unlikely.
362 *
363 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200364 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
365 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100366 * mutex.
367 */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100368 if (ffs_setup_state_clear_cancelled(ffs) ==
369 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200370 ret = -EIDRM;
371done_spin:
372 spin_unlock_irq(&ffs->ev.waitq.lock);
373 } else {
374 /* unlocks spinlock */
375 ret = __ffs_ep0_queue_wait(ffs, data, len);
376 }
377 kfree(data);
378 break;
379
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200380 default:
381 ret = -EBADFD;
382 break;
383 }
384
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200385 mutex_unlock(&ffs->mutex);
386 return ret;
387}
388
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200389static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
390 size_t n)
391{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100392 /*
393 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
394 * to release them.
395 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200396 struct usb_functionfs_event events[n];
397 unsigned i = 0;
398
399 memset(events, 0, sizeof events);
400
401 do {
402 events[i].type = ffs->ev.types[i];
403 if (events[i].type == FUNCTIONFS_SETUP) {
404 events[i].u.setup = ffs->ev.setup;
405 ffs->setup_state = FFS_SETUP_PENDING;
406 }
407 } while (++i < n);
408
409 if (n < ffs->ev.count) {
410 ffs->ev.count -= n;
411 memmove(ffs->ev.types, ffs->ev.types + n,
412 ffs->ev.count * sizeof *ffs->ev.types);
413 } else {
414 ffs->ev.count = 0;
415 }
416
417 spin_unlock_irq(&ffs->ev.waitq.lock);
418 mutex_unlock(&ffs->mutex);
419
420 return unlikely(__copy_to_user(buf, events, sizeof events))
421 ? -EFAULT : sizeof events;
422}
423
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200424static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
425 size_t len, loff_t *ptr)
426{
427 struct ffs_data *ffs = file->private_data;
428 char *data = NULL;
429 size_t n;
430 int ret;
431
432 ENTER();
433
434 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100435 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200436 return -EIDRM;
437
438 /* Acquire mutex */
439 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
440 if (unlikely(ret < 0))
441 return ret;
442
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200443 /* Check state */
444 if (ffs->state != FFS_ACTIVE) {
445 ret = -EBADFD;
446 goto done_mutex;
447 }
448
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100449 /*
450 * We're called from user space, we can use _irq rather then
451 * _irqsave
452 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200453 spin_lock_irq(&ffs->ev.waitq.lock);
454
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100455 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100456 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200457 ret = -EIDRM;
458 break;
459
460 case FFS_NO_SETUP:
461 n = len / sizeof(struct usb_functionfs_event);
462 if (unlikely(!n)) {
463 ret = -EINVAL;
464 break;
465 }
466
467 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
468 ret = -EAGAIN;
469 break;
470 }
471
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100472 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
473 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200474 ret = -EINTR;
475 break;
476 }
477
478 return __ffs_ep0_read_events(ffs, buf,
479 min(n, (size_t)ffs->ev.count));
480
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200481 case FFS_SETUP_PENDING:
482 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
483 spin_unlock_irq(&ffs->ev.waitq.lock);
484 ret = __ffs_ep0_stall(ffs);
485 goto done_mutex;
486 }
487
488 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
489
490 spin_unlock_irq(&ffs->ev.waitq.lock);
491
492 if (likely(len)) {
493 data = kmalloc(len, GFP_KERNEL);
494 if (unlikely(!data)) {
495 ret = -ENOMEM;
496 goto done_mutex;
497 }
498 }
499
500 spin_lock_irq(&ffs->ev.waitq.lock);
501
502 /* See ffs_ep0_write() */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100503 if (ffs_setup_state_clear_cancelled(ffs) ==
504 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200505 ret = -EIDRM;
506 break;
507 }
508
509 /* unlocks spinlock */
510 ret = __ffs_ep0_queue_wait(ffs, data, len);
511 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
512 ret = -EFAULT;
513 goto done_mutex;
514
515 default:
516 ret = -EBADFD;
517 break;
518 }
519
520 spin_unlock_irq(&ffs->ev.waitq.lock);
521done_mutex:
522 mutex_unlock(&ffs->mutex);
523 kfree(data);
524 return ret;
525}
526
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200527static int ffs_ep0_open(struct inode *inode, struct file *file)
528{
529 struct ffs_data *ffs = inode->i_private;
530
531 ENTER();
532
533 if (unlikely(ffs->state == FFS_CLOSING))
534 return -EBUSY;
535
536 file->private_data = ffs;
537 ffs_data_opened(ffs);
538
539 return 0;
540}
541
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200542static int ffs_ep0_release(struct inode *inode, struct file *file)
543{
544 struct ffs_data *ffs = file->private_data;
545
546 ENTER();
547
548 ffs_data_closed(ffs);
549
550 return 0;
551}
552
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200553static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
554{
555 struct ffs_data *ffs = file->private_data;
556 struct usb_gadget *gadget = ffs->gadget;
557 long ret;
558
559 ENTER();
560
561 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
562 struct ffs_function *func = ffs->func;
563 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200564 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200565 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200566 } else {
567 ret = -ENOTTY;
568 }
569
570 return ret;
571}
572
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200573static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200574 .llseek = no_llseek,
575
576 .open = ffs_ep0_open,
577 .write = ffs_ep0_write,
578 .read = ffs_ep0_read,
579 .release = ffs_ep0_release,
580 .unlocked_ioctl = ffs_ep0_ioctl,
581};
582
583
584/* "Normal" endpoints operations ********************************************/
585
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200586static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
587{
588 ENTER();
589 if (likely(req->context)) {
590 struct ffs_ep *ep = _ep->driver_data;
591 ep->status = req->status ? req->status : req->actual;
592 complete(req->context);
593 }
594}
595
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200596static ssize_t ffs_epfile_io(struct file *file,
597 char __user *buf, size_t len, int read)
598{
599 struct ffs_epfile *epfile = file->private_data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800600 struct usb_gadget *gadget = epfile->ffs->gadget;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200601 struct ffs_ep *ep;
602 char *data = NULL;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800603 ssize_t ret, data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200604 int halt;
605
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800606 /* Are we still active? */
607 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
608 ret = -ENODEV;
609 goto error;
610 }
611
612 /* Wait for endpoint to be enabled */
613 ep = epfile->ep;
614 if (!ep) {
615 if (file->f_flags & O_NONBLOCK) {
616 ret = -EAGAIN;
617 goto error;
618 }
619
620 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
621 if (ret) {
622 ret = -EINTR;
623 goto error;
624 }
625 }
626
627 /* Do we halt? */
628 halt = !read == !epfile->in;
629 if (halt && epfile->isoc) {
630 ret = -EINVAL;
631 goto error;
632 }
633
634 /* Allocate & copy */
635 if (!halt) {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800636 /*
637 * Controller may require buffer size to be aligned to
638 * maxpacketsize of an out endpoint.
639 */
640 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
641
642 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800643 if (unlikely(!data))
644 return -ENOMEM;
645
646 if (!read && unlikely(copy_from_user(data, buf, len))) {
647 ret = -EFAULT;
648 goto error;
649 }
650 }
651
652 /* We will be using request */
653 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
654 if (unlikely(ret))
655 goto error;
656
657 spin_lock_irq(&epfile->ffs->eps_lock);
658
659 if (epfile->ep != ep) {
660 /* In the meantime, endpoint got disabled or changed. */
661 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200662 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800663 } else if (halt) {
664 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200665 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
666 usb_ep_set_halt(ep->ep);
667 spin_unlock_irq(&epfile->ffs->eps_lock);
668 ret = -EBADMSG;
669 } else {
670 /* Fire the request */
671 DECLARE_COMPLETION_ONSTACK(done);
672
673 struct usb_request *req = ep->req;
674 req->context = &done;
675 req->complete = ffs_epfile_io_complete;
676 req->buf = data;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800677 req->length = data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200678
679 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
680
681 spin_unlock_irq(&epfile->ffs->eps_lock);
682
683 if (unlikely(ret < 0)) {
684 /* nop */
685 } else if (unlikely(wait_for_completion_interruptible(&done))) {
686 ret = -EINTR;
687 usb_ep_dequeue(ep->ep, req);
688 } else {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800689 /*
690 * XXX We may end up silently droping data here.
691 * Since data_len (i.e. req->length) may be bigger
692 * than len (after being rounded up to maxpacketsize),
693 * we may end up with more data then user space has
694 * space for.
695 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200696 ret = ep->status;
697 if (read && ret > 0 &&
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800698 unlikely(copy_to_user(buf, data,
699 min_t(size_t, ret, len))))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200700 ret = -EFAULT;
701 }
702 }
703
704 mutex_unlock(&epfile->mutex);
705error:
706 kfree(data);
707 return ret;
708}
709
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200710static ssize_t
711ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
712 loff_t *ptr)
713{
714 ENTER();
715
716 return ffs_epfile_io(file, (char __user *)buf, len, 0);
717}
718
719static ssize_t
720ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
721{
722 ENTER();
723
724 return ffs_epfile_io(file, buf, len, 1);
725}
726
727static int
728ffs_epfile_open(struct inode *inode, struct file *file)
729{
730 struct ffs_epfile *epfile = inode->i_private;
731
732 ENTER();
733
734 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
735 return -ENODEV;
736
737 file->private_data = epfile;
738 ffs_data_opened(epfile->ffs);
739
740 return 0;
741}
742
743static int
744ffs_epfile_release(struct inode *inode, struct file *file)
745{
746 struct ffs_epfile *epfile = inode->i_private;
747
748 ENTER();
749
750 ffs_data_closed(epfile->ffs);
751
752 return 0;
753}
754
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200755static long ffs_epfile_ioctl(struct file *file, unsigned code,
756 unsigned long value)
757{
758 struct ffs_epfile *epfile = file->private_data;
759 int ret;
760
761 ENTER();
762
763 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
764 return -ENODEV;
765
766 spin_lock_irq(&epfile->ffs->eps_lock);
767 if (likely(epfile->ep)) {
768 switch (code) {
769 case FUNCTIONFS_FIFO_STATUS:
770 ret = usb_ep_fifo_status(epfile->ep->ep);
771 break;
772 case FUNCTIONFS_FIFO_FLUSH:
773 usb_ep_fifo_flush(epfile->ep->ep);
774 ret = 0;
775 break;
776 case FUNCTIONFS_CLEAR_HALT:
777 ret = usb_ep_clear_halt(epfile->ep->ep);
778 break;
779 case FUNCTIONFS_ENDPOINT_REVMAP:
780 ret = epfile->ep->num;
781 break;
782 default:
783 ret = -ENOTTY;
784 }
785 } else {
786 ret = -ENODEV;
787 }
788 spin_unlock_irq(&epfile->ffs->eps_lock);
789
790 return ret;
791}
792
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200793static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200794 .llseek = no_llseek,
795
796 .open = ffs_epfile_open,
797 .write = ffs_epfile_write,
798 .read = ffs_epfile_read,
799 .release = ffs_epfile_release,
800 .unlocked_ioctl = ffs_epfile_ioctl,
801};
802
803
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200804/* File system and super block operations ***********************************/
805
806/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100807 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200808 * function configuration then later for event monitoring.
809 */
810
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200811static struct inode *__must_check
812ffs_sb_make_inode(struct super_block *sb, void *data,
813 const struct file_operations *fops,
814 const struct inode_operations *iops,
815 struct ffs_file_perms *perms)
816{
817 struct inode *inode;
818
819 ENTER();
820
821 inode = new_inode(sb);
822
823 if (likely(inode)) {
824 struct timespec current_time = CURRENT_TIME;
825
Al Viro12ba8d12010-10-27 04:19:36 +0100826 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200827 inode->i_mode = perms->mode;
828 inode->i_uid = perms->uid;
829 inode->i_gid = perms->gid;
830 inode->i_atime = current_time;
831 inode->i_mtime = current_time;
832 inode->i_ctime = current_time;
833 inode->i_private = data;
834 if (fops)
835 inode->i_fop = fops;
836 if (iops)
837 inode->i_op = iops;
838 }
839
840 return inode;
841}
842
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200843/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200844static struct inode *ffs_sb_create_file(struct super_block *sb,
845 const char *name, void *data,
846 const struct file_operations *fops,
847 struct dentry **dentry_p)
848{
849 struct ffs_data *ffs = sb->s_fs_info;
850 struct dentry *dentry;
851 struct inode *inode;
852
853 ENTER();
854
855 dentry = d_alloc_name(sb->s_root, name);
856 if (unlikely(!dentry))
857 return NULL;
858
859 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
860 if (unlikely(!inode)) {
861 dput(dentry);
862 return NULL;
863 }
864
865 d_add(dentry, inode);
866 if (dentry_p)
867 *dentry_p = dentry;
868
869 return inode;
870}
871
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200872/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200873static const struct super_operations ffs_sb_operations = {
874 .statfs = simple_statfs,
875 .drop_inode = generic_delete_inode,
876};
877
878struct ffs_sb_fill_data {
879 struct ffs_file_perms perms;
880 umode_t root_mode;
881 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +0100882 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200883};
884
885static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
886{
887 struct ffs_sb_fill_data *data = _data;
888 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +0100889 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200890
891 ENTER();
892
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200893 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +0100894 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200895 sb->s_fs_info = ffs;
896 sb->s_blocksize = PAGE_CACHE_SIZE;
897 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
898 sb->s_magic = FUNCTIONFS_MAGIC;
899 sb->s_op = &ffs_sb_operations;
900 sb->s_time_gran = 1;
901
902 /* Root inode */
903 data->perms.mode = data->root_mode;
904 inode = ffs_sb_make_inode(sb, NULL,
905 &simple_dir_operations,
906 &simple_dir_inode_operations,
907 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -0500908 sb->s_root = d_make_root(inode);
909 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +0100910 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200911
912 /* EP0 file */
913 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
914 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +0100915 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200916
917 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200918}
919
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200920static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
921{
922 ENTER();
923
924 if (!opts || !*opts)
925 return 0;
926
927 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200928 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100929 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200930
931 /* Option limit */
932 comma = strchr(opts, ',');
933 if (comma)
934 *comma = 0;
935
936 /* Value limit */
937 eq = strchr(opts, '=');
938 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100939 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200940 return -EINVAL;
941 }
942 *eq = 0;
943
944 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +0100945 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100946 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200947 return -EINVAL;
948 }
949
950 /* Interpret option */
951 switch (eq - opts) {
952 case 5:
953 if (!memcmp(opts, "rmode", 5))
954 data->root_mode = (value & 0555) | S_IFDIR;
955 else if (!memcmp(opts, "fmode", 5))
956 data->perms.mode = (value & 0666) | S_IFREG;
957 else
958 goto invalid;
959 break;
960
961 case 4:
962 if (!memcmp(opts, "mode", 4)) {
963 data->root_mode = (value & 0555) | S_IFDIR;
964 data->perms.mode = (value & 0666) | S_IFREG;
965 } else {
966 goto invalid;
967 }
968 break;
969
970 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700971 if (!memcmp(opts, "uid", 3)) {
972 data->perms.uid = make_kuid(current_user_ns(), value);
973 if (!uid_valid(data->perms.uid)) {
974 pr_err("%s: unmapped value: %lu\n", opts, value);
975 return -EINVAL;
976 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800977 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700978 data->perms.gid = make_kgid(current_user_ns(), value);
979 if (!gid_valid(data->perms.gid)) {
980 pr_err("%s: unmapped value: %lu\n", opts, value);
981 return -EINVAL;
982 }
Benoit Gobyb8100752013-01-08 19:57:09 -0800983 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200984 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -0800985 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200986 break;
987
988 default:
989invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100990 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200991 return -EINVAL;
992 }
993
994 /* Next iteration */
995 if (!comma)
996 break;
997 opts = comma + 1;
998 }
999
1000 return 0;
1001}
1002
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001003/* "mount -t functionfs dev_name /dev/function" ends up here */
1004
Al Virofc14f2f2010-07-25 01:48:30 +04001005static struct dentry *
1006ffs_fs_mount(struct file_system_type *t, int flags,
1007 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001008{
1009 struct ffs_sb_fill_data data = {
1010 .perms = {
1011 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001012 .uid = GLOBAL_ROOT_UID,
1013 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001014 },
1015 .root_mode = S_IFDIR | 0500,
1016 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001017 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001018 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001019 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001020 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001021
1022 ENTER();
1023
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001024 ret = ffs_fs_parse_opts(&data, opts);
1025 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001026 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001027
Al Viro2606b282013-09-20 17:14:21 +01001028 ffs = ffs_data_new();
1029 if (unlikely(!ffs))
1030 return ERR_PTR(-ENOMEM);
1031 ffs->file_perms = data.perms;
1032
1033 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1034 if (unlikely(!ffs->dev_name)) {
1035 ffs_data_put(ffs);
1036 return ERR_PTR(-ENOMEM);
1037 }
1038
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001039 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001040 if (IS_ERR(ffs_dev)) {
1041 ffs_data_put(ffs);
1042 return ERR_CAST(ffs_dev);
1043 }
1044 ffs->private_data = ffs_dev;
1045 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001046
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001047 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001048 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001049 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001050 ffs_data_put(data.ffs_data);
1051 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001052 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001053}
1054
1055static void
1056ffs_fs_kill_sb(struct super_block *sb)
1057{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001058 ENTER();
1059
1060 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001061 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001062 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001063 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001064 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001065}
1066
1067static struct file_system_type ffs_fs_type = {
1068 .owner = THIS_MODULE,
1069 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001070 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001071 .kill_sb = ffs_fs_kill_sb,
1072};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001073MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001074
1075
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001076/* Driver's main init/cleanup functions *************************************/
1077
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001078static int functionfs_init(void)
1079{
1080 int ret;
1081
1082 ENTER();
1083
1084 ret = register_filesystem(&ffs_fs_type);
1085 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001086 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001087 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001088 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001089
1090 return ret;
1091}
1092
1093static void functionfs_cleanup(void)
1094{
1095 ENTER();
1096
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001097 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001098 unregister_filesystem(&ffs_fs_type);
1099}
1100
1101
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001102/* ffs_data and ffs_function construction and destruction code **************/
1103
1104static void ffs_data_clear(struct ffs_data *ffs);
1105static void ffs_data_reset(struct ffs_data *ffs);
1106
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001107static void ffs_data_get(struct ffs_data *ffs)
1108{
1109 ENTER();
1110
1111 atomic_inc(&ffs->ref);
1112}
1113
1114static void ffs_data_opened(struct ffs_data *ffs)
1115{
1116 ENTER();
1117
1118 atomic_inc(&ffs->ref);
1119 atomic_inc(&ffs->opened);
1120}
1121
1122static void ffs_data_put(struct ffs_data *ffs)
1123{
1124 ENTER();
1125
1126 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001127 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001128 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001129 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001130 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001131 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001132 kfree(ffs);
1133 }
1134}
1135
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001136static void ffs_data_closed(struct ffs_data *ffs)
1137{
1138 ENTER();
1139
1140 if (atomic_dec_and_test(&ffs->opened)) {
1141 ffs->state = FFS_CLOSING;
1142 ffs_data_reset(ffs);
1143 }
1144
1145 ffs_data_put(ffs);
1146}
1147
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001148static struct ffs_data *ffs_data_new(void)
1149{
1150 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1151 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001152 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001153
1154 ENTER();
1155
1156 atomic_set(&ffs->ref, 1);
1157 atomic_set(&ffs->opened, 0);
1158 ffs->state = FFS_READ_DESCRIPTORS;
1159 mutex_init(&ffs->mutex);
1160 spin_lock_init(&ffs->eps_lock);
1161 init_waitqueue_head(&ffs->ev.waitq);
1162 init_completion(&ffs->ep0req_completion);
1163
1164 /* XXX REVISIT need to update it in some places, or do we? */
1165 ffs->ev.can_stall = 1;
1166
1167 return ffs;
1168}
1169
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001170static void ffs_data_clear(struct ffs_data *ffs)
1171{
1172 ENTER();
1173
1174 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001175 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001176
1177 BUG_ON(ffs->gadget);
1178
1179 if (ffs->epfiles)
1180 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1181
1182 kfree(ffs->raw_descs);
1183 kfree(ffs->raw_strings);
1184 kfree(ffs->stringtabs);
1185}
1186
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001187static void ffs_data_reset(struct ffs_data *ffs)
1188{
1189 ENTER();
1190
1191 ffs_data_clear(ffs);
1192
1193 ffs->epfiles = NULL;
1194 ffs->raw_descs = NULL;
1195 ffs->raw_strings = NULL;
1196 ffs->stringtabs = NULL;
1197
1198 ffs->raw_descs_length = 0;
1199 ffs->raw_fs_descs_length = 0;
1200 ffs->fs_descs_count = 0;
1201 ffs->hs_descs_count = 0;
1202
1203 ffs->strings_count = 0;
1204 ffs->interfaces_count = 0;
1205 ffs->eps_count = 0;
1206
1207 ffs->ev.count = 0;
1208
1209 ffs->state = FFS_READ_DESCRIPTORS;
1210 ffs->setup_state = FFS_NO_SETUP;
1211 ffs->flags = 0;
1212}
1213
1214
1215static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1216{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001217 struct usb_gadget_strings **lang;
1218 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001219
1220 ENTER();
1221
1222 if (WARN_ON(ffs->state != FFS_ACTIVE
1223 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1224 return -EBADFD;
1225
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001226 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1227 if (unlikely(first_id < 0))
1228 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001229
1230 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1231 if (unlikely(!ffs->ep0req))
1232 return -ENOMEM;
1233 ffs->ep0req->complete = ffs_ep0_complete;
1234 ffs->ep0req->context = ffs;
1235
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001236 lang = ffs->stringtabs;
1237 for (lang = ffs->stringtabs; *lang; ++lang) {
1238 struct usb_string *str = (*lang)->strings;
1239 int id = first_id;
1240 for (; str->s; ++id, ++str)
1241 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001242 }
1243
1244 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001245 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001246 return 0;
1247}
1248
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001249static void functionfs_unbind(struct ffs_data *ffs)
1250{
1251 ENTER();
1252
1253 if (!WARN_ON(!ffs->gadget)) {
1254 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1255 ffs->ep0req = NULL;
1256 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001257 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001258 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001259 }
1260}
1261
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001262static int ffs_epfiles_create(struct ffs_data *ffs)
1263{
1264 struct ffs_epfile *epfile, *epfiles;
1265 unsigned i, count;
1266
1267 ENTER();
1268
1269 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001270 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001271 if (!epfiles)
1272 return -ENOMEM;
1273
1274 epfile = epfiles;
1275 for (i = 1; i <= count; ++i, ++epfile) {
1276 epfile->ffs = ffs;
1277 mutex_init(&epfile->mutex);
1278 init_waitqueue_head(&epfile->wait);
1279 sprintf(epfiles->name, "ep%u", i);
1280 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1281 &ffs_epfile_operations,
1282 &epfile->dentry))) {
1283 ffs_epfiles_destroy(epfiles, i - 1);
1284 return -ENOMEM;
1285 }
1286 }
1287
1288 ffs->epfiles = epfiles;
1289 return 0;
1290}
1291
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001292static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1293{
1294 struct ffs_epfile *epfile = epfiles;
1295
1296 ENTER();
1297
1298 for (; count; --count, ++epfile) {
1299 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1300 waitqueue_active(&epfile->wait));
1301 if (epfile->dentry) {
1302 d_delete(epfile->dentry);
1303 dput(epfile->dentry);
1304 epfile->dentry = NULL;
1305 }
1306 }
1307
1308 kfree(epfiles);
1309}
1310
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001311
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001312static void ffs_func_eps_disable(struct ffs_function *func)
1313{
1314 struct ffs_ep *ep = func->eps;
1315 struct ffs_epfile *epfile = func->ffs->epfiles;
1316 unsigned count = func->ffs->eps_count;
1317 unsigned long flags;
1318
1319 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1320 do {
1321 /* pending requests get nuked */
1322 if (likely(ep->ep))
1323 usb_ep_disable(ep->ep);
1324 epfile->ep = NULL;
1325
1326 ++ep;
1327 ++epfile;
1328 } while (--count);
1329 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1330}
1331
1332static int ffs_func_eps_enable(struct ffs_function *func)
1333{
1334 struct ffs_data *ffs = func->ffs;
1335 struct ffs_ep *ep = func->eps;
1336 struct ffs_epfile *epfile = ffs->epfiles;
1337 unsigned count = ffs->eps_count;
1338 unsigned long flags;
1339 int ret = 0;
1340
1341 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1342 do {
1343 struct usb_endpoint_descriptor *ds;
1344 ds = ep->descs[ep->descs[1] ? 1 : 0];
1345
1346 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001347 ep->ep->desc = ds;
1348 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001349 if (likely(!ret)) {
1350 epfile->ep = ep;
1351 epfile->in = usb_endpoint_dir_in(ds);
1352 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1353 } else {
1354 break;
1355 }
1356
1357 wake_up(&epfile->wait);
1358
1359 ++ep;
1360 ++epfile;
1361 } while (--count);
1362 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1363
1364 return ret;
1365}
1366
1367
1368/* Parsing and building descriptors and strings *****************************/
1369
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001370/*
1371 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001372 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001373 * required by given configuration. Returns address after the
1374 * descriptor or NULL if data is invalid.
1375 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001376
1377enum ffs_entity_type {
1378 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1379};
1380
1381typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1382 u8 *valuep,
1383 struct usb_descriptor_header *desc,
1384 void *priv);
1385
1386static int __must_check ffs_do_desc(char *data, unsigned len,
1387 ffs_entity_callback entity, void *priv)
1388{
1389 struct usb_descriptor_header *_ds = (void *)data;
1390 u8 length;
1391 int ret;
1392
1393 ENTER();
1394
1395 /* At least two bytes are required: length and type */
1396 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001397 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001398 return -EINVAL;
1399 }
1400
1401 /* If we have at least as many bytes as the descriptor takes? */
1402 length = _ds->bLength;
1403 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001404 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001405 return -EINVAL;
1406 }
1407
1408#define __entity_check_INTERFACE(val) 1
1409#define __entity_check_STRING(val) (val)
1410#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1411#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001412 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001413 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001414 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001415 return -EINVAL; \
1416 } \
1417 ret = entity(FFS_ ##type, &val, _ds, priv); \
1418 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001419 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001420 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001421 return ret; \
1422 } \
1423 } while (0)
1424
1425 /* Parse descriptor depending on type. */
1426 switch (_ds->bDescriptorType) {
1427 case USB_DT_DEVICE:
1428 case USB_DT_CONFIG:
1429 case USB_DT_STRING:
1430 case USB_DT_DEVICE_QUALIFIER:
1431 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001432 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001433 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001434 return -EINVAL;
1435
1436 case USB_DT_INTERFACE: {
1437 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001438 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001439 if (length != sizeof *ds)
1440 goto inv_length;
1441
1442 __entity(INTERFACE, ds->bInterfaceNumber);
1443 if (ds->iInterface)
1444 __entity(STRING, ds->iInterface);
1445 }
1446 break;
1447
1448 case USB_DT_ENDPOINT: {
1449 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001450 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001451 if (length != USB_DT_ENDPOINT_SIZE &&
1452 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1453 goto inv_length;
1454 __entity(ENDPOINT, ds->bEndpointAddress);
1455 }
1456 break;
1457
Koen Beel560f1182012-05-30 20:43:37 +02001458 case HID_DT_HID:
1459 pr_vdebug("hid descriptor\n");
1460 if (length != sizeof(struct hid_descriptor))
1461 goto inv_length;
1462 break;
1463
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001464 case USB_DT_OTG:
1465 if (length != sizeof(struct usb_otg_descriptor))
1466 goto inv_length;
1467 break;
1468
1469 case USB_DT_INTERFACE_ASSOCIATION: {
1470 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001471 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001472 if (length != sizeof *ds)
1473 goto inv_length;
1474 if (ds->iFunction)
1475 __entity(STRING, ds->iFunction);
1476 }
1477 break;
1478
1479 case USB_DT_OTHER_SPEED_CONFIG:
1480 case USB_DT_INTERFACE_POWER:
1481 case USB_DT_DEBUG:
1482 case USB_DT_SECURITY:
1483 case USB_DT_CS_RADIO_CONTROL:
1484 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001485 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001486 return -EINVAL;
1487
1488 default:
1489 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001490 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001491 return -EINVAL;
1492
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001493inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001494 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001495 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001496 return -EINVAL;
1497 }
1498
1499#undef __entity
1500#undef __entity_check_DESCRIPTOR
1501#undef __entity_check_INTERFACE
1502#undef __entity_check_STRING
1503#undef __entity_check_ENDPOINT
1504
1505 return length;
1506}
1507
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001508static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1509 ffs_entity_callback entity, void *priv)
1510{
1511 const unsigned _len = len;
1512 unsigned long num = 0;
1513
1514 ENTER();
1515
1516 for (;;) {
1517 int ret;
1518
1519 if (num == count)
1520 data = NULL;
1521
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001522 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001523 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1524 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001525 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001526 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001527 return ret;
1528 }
1529
1530 if (!data)
1531 return _len - len;
1532
1533 ret = ffs_do_desc(data, len, entity, priv);
1534 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001535 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001536 return ret;
1537 }
1538
1539 len -= ret;
1540 data += ret;
1541 ++num;
1542 }
1543}
1544
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001545static int __ffs_data_do_entity(enum ffs_entity_type type,
1546 u8 *valuep, struct usb_descriptor_header *desc,
1547 void *priv)
1548{
1549 struct ffs_data *ffs = priv;
1550
1551 ENTER();
1552
1553 switch (type) {
1554 case FFS_DESCRIPTOR:
1555 break;
1556
1557 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001558 /*
1559 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001560 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001561 * "n+1" interfaces.
1562 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001563 if (*valuep >= ffs->interfaces_count)
1564 ffs->interfaces_count = *valuep + 1;
1565 break;
1566
1567 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001568 /*
1569 * Strings are indexed from 1 (0 is magic ;) reserved
1570 * for languages list or some such)
1571 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001572 if (*valuep > ffs->strings_count)
1573 ffs->strings_count = *valuep;
1574 break;
1575
1576 case FFS_ENDPOINT:
1577 /* Endpoints are indexed from 1 as well. */
1578 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1579 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1580 break;
1581 }
1582
1583 return 0;
1584}
1585
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001586static int __ffs_data_got_descs(struct ffs_data *ffs,
1587 char *const _data, size_t len)
1588{
1589 unsigned fs_count, hs_count;
1590 int fs_len, ret = -EINVAL;
1591 char *data = _data;
1592
1593 ENTER();
1594
1595 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1596 get_unaligned_le32(data + 4) != len))
1597 goto error;
1598 fs_count = get_unaligned_le32(data + 8);
1599 hs_count = get_unaligned_le32(data + 12);
1600
1601 if (!fs_count && !hs_count)
1602 goto einval;
1603
1604 data += 16;
1605 len -= 16;
1606
1607 if (likely(fs_count)) {
1608 fs_len = ffs_do_descs(fs_count, data, len,
1609 __ffs_data_do_entity, ffs);
1610 if (unlikely(fs_len < 0)) {
1611 ret = fs_len;
1612 goto error;
1613 }
1614
1615 data += fs_len;
1616 len -= fs_len;
1617 } else {
1618 fs_len = 0;
1619 }
1620
1621 if (likely(hs_count)) {
1622 ret = ffs_do_descs(hs_count, data, len,
1623 __ffs_data_do_entity, ffs);
1624 if (unlikely(ret < 0))
1625 goto error;
1626 } else {
1627 ret = 0;
1628 }
1629
1630 if (unlikely(len != ret))
1631 goto einval;
1632
1633 ffs->raw_fs_descs_length = fs_len;
1634 ffs->raw_descs_length = fs_len + ret;
1635 ffs->raw_descs = _data;
1636 ffs->fs_descs_count = fs_count;
1637 ffs->hs_descs_count = hs_count;
1638
1639 return 0;
1640
1641einval:
1642 ret = -EINVAL;
1643error:
1644 kfree(_data);
1645 return ret;
1646}
1647
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001648static int __ffs_data_got_strings(struct ffs_data *ffs,
1649 char *const _data, size_t len)
1650{
1651 u32 str_count, needed_count, lang_count;
1652 struct usb_gadget_strings **stringtabs, *t;
1653 struct usb_string *strings, *s;
1654 const char *data = _data;
1655
1656 ENTER();
1657
1658 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1659 get_unaligned_le32(data + 4) != len))
1660 goto error;
1661 str_count = get_unaligned_le32(data + 8);
1662 lang_count = get_unaligned_le32(data + 12);
1663
1664 /* if one is zero the other must be zero */
1665 if (unlikely(!str_count != !lang_count))
1666 goto error;
1667
1668 /* Do we have at least as many strings as descriptors need? */
1669 needed_count = ffs->strings_count;
1670 if (unlikely(str_count < needed_count))
1671 goto error;
1672
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001673 /*
1674 * If we don't need any strings just return and free all
1675 * memory.
1676 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001677 if (!needed_count) {
1678 kfree(_data);
1679 return 0;
1680 }
1681
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001682 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001683 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001684 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001685 vla_group(d);
1686 vla_item(d, struct usb_gadget_strings *, stringtabs,
1687 lang_count + 1);
1688 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
1689 vla_item(d, struct usb_string, strings,
1690 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001691
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001692 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
1693
1694 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001695 kfree(_data);
1696 return -ENOMEM;
1697 }
1698
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001699 /* Initialize the VLA pointers */
1700 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1701 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001702 i = lang_count;
1703 do {
1704 *stringtabs++ = t++;
1705 } while (--i);
1706 *stringtabs = NULL;
1707
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001708 /* stringtabs = vlabuf = d_stringtabs for later kfree */
1709 stringtabs = vla_ptr(vlabuf, d, stringtabs);
1710 t = vla_ptr(vlabuf, d, stringtab);
1711 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001712 strings = s;
1713 }
1714
1715 /* For each language */
1716 data += 16;
1717 len -= 16;
1718
1719 do { /* lang_count > 0 so we can use do-while */
1720 unsigned needed = needed_count;
1721
1722 if (unlikely(len < 3))
1723 goto error_free;
1724 t->language = get_unaligned_le16(data);
1725 t->strings = s;
1726 ++t;
1727
1728 data += 2;
1729 len -= 2;
1730
1731 /* For each string */
1732 do { /* str_count > 0 so we can use do-while */
1733 size_t length = strnlen(data, len);
1734
1735 if (unlikely(length == len))
1736 goto error_free;
1737
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001738 /*
1739 * User may provide more strings then we need,
1740 * if that's the case we simply ignore the
1741 * rest
1742 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001743 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001744 /*
1745 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001746 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001747 * now just leave garbage here.
1748 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001749 s->s = data;
1750 --needed;
1751 ++s;
1752 }
1753
1754 data += length + 1;
1755 len -= length + 1;
1756 } while (--str_count);
1757
1758 s->id = 0; /* terminator */
1759 s->s = NULL;
1760 ++s;
1761
1762 } while (--lang_count);
1763
1764 /* Some garbage left? */
1765 if (unlikely(len))
1766 goto error_free;
1767
1768 /* Done! */
1769 ffs->stringtabs = stringtabs;
1770 ffs->raw_strings = _data;
1771
1772 return 0;
1773
1774error_free:
1775 kfree(stringtabs);
1776error:
1777 kfree(_data);
1778 return -EINVAL;
1779}
1780
1781
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001782/* Events handling and management *******************************************/
1783
1784static void __ffs_event_add(struct ffs_data *ffs,
1785 enum usb_functionfs_event_type type)
1786{
1787 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1788 int neg = 0;
1789
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001790 /*
1791 * Abort any unhandled setup
1792 *
1793 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001794 * of ffs->setup_state without holding the lock because when
1795 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001796 * the source does nothing.
1797 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001798 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01001799 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001800
1801 switch (type) {
1802 case FUNCTIONFS_RESUME:
1803 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001804 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001805 case FUNCTIONFS_SUSPEND:
1806 case FUNCTIONFS_SETUP:
1807 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001808 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001809 break;
1810
1811 case FUNCTIONFS_BIND:
1812 case FUNCTIONFS_UNBIND:
1813 case FUNCTIONFS_DISABLE:
1814 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001815 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001816 rem_type1 = FUNCTIONFS_SUSPEND;
1817 rem_type2 = FUNCTIONFS_RESUME;
1818 neg = 1;
1819 break;
1820
1821 default:
1822 BUG();
1823 }
1824
1825 {
1826 u8 *ev = ffs->ev.types, *out = ev;
1827 unsigned n = ffs->ev.count;
1828 for (; n; --n, ++ev)
1829 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
1830 *out++ = *ev;
1831 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001832 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001833 ffs->ev.count = out - ffs->ev.types;
1834 }
1835
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001836 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001837 ffs->ev.types[ffs->ev.count++] = type;
1838 wake_up_locked(&ffs->ev.waitq);
1839}
1840
1841static void ffs_event_add(struct ffs_data *ffs,
1842 enum usb_functionfs_event_type type)
1843{
1844 unsigned long flags;
1845 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
1846 __ffs_event_add(ffs, type);
1847 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
1848}
1849
1850
1851/* Bind/unbind USB function hooks *******************************************/
1852
1853static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
1854 struct usb_descriptor_header *desc,
1855 void *priv)
1856{
1857 struct usb_endpoint_descriptor *ds = (void *)desc;
1858 struct ffs_function *func = priv;
1859 struct ffs_ep *ffs_ep;
1860
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001861 /*
1862 * If hs_descriptors is not NULL then we are reading hs
1863 * descriptors now
1864 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001865 const int isHS = func->function.hs_descriptors != NULL;
1866 unsigned idx;
1867
1868 if (type != FFS_DESCRIPTOR)
1869 return 0;
1870
1871 if (isHS)
1872 func->function.hs_descriptors[(long)valuep] = desc;
1873 else
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001874 func->function.fs_descriptors[(long)valuep] = desc;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001875
1876 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1877 return 0;
1878
1879 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
1880 ffs_ep = func->eps + idx;
1881
1882 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001883 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001884 isHS ? "high" : "full",
1885 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001886 return -EINVAL;
1887 }
1888 ffs_ep->descs[isHS] = ds;
1889
1890 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
1891 if (ffs_ep->ep) {
1892 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
1893 if (!ds->wMaxPacketSize)
1894 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
1895 } else {
1896 struct usb_request *req;
1897 struct usb_ep *ep;
1898
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001899 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001900 ep = usb_ep_autoconfig(func->gadget, ds);
1901 if (unlikely(!ep))
1902 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08001903 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001904
1905 req = usb_ep_alloc_request(ep, GFP_KERNEL);
1906 if (unlikely(!req))
1907 return -ENOMEM;
1908
1909 ffs_ep->ep = ep;
1910 ffs_ep->req = req;
1911 func->eps_revmap[ds->bEndpointAddress &
1912 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
1913 }
1914 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
1915
1916 return 0;
1917}
1918
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001919static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
1920 struct usb_descriptor_header *desc,
1921 void *priv)
1922{
1923 struct ffs_function *func = priv;
1924 unsigned idx;
1925 u8 newValue;
1926
1927 switch (type) {
1928 default:
1929 case FFS_DESCRIPTOR:
1930 /* Handled in previous pass by __ffs_func_bind_do_descs() */
1931 return 0;
1932
1933 case FFS_INTERFACE:
1934 idx = *valuep;
1935 if (func->interfaces_nums[idx] < 0) {
1936 int id = usb_interface_id(func->conf, &func->function);
1937 if (unlikely(id < 0))
1938 return id;
1939 func->interfaces_nums[idx] = id;
1940 }
1941 newValue = func->interfaces_nums[idx];
1942 break;
1943
1944 case FFS_STRING:
1945 /* String' IDs are allocated when fsf_data is bound to cdev */
1946 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
1947 break;
1948
1949 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001950 /*
1951 * USB_DT_ENDPOINT are handled in
1952 * __ffs_func_bind_do_descs().
1953 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001954 if (desc->bDescriptorType == USB_DT_ENDPOINT)
1955 return 0;
1956
1957 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
1958 if (unlikely(!func->eps[idx].ep))
1959 return -EINVAL;
1960
1961 {
1962 struct usb_endpoint_descriptor **descs;
1963 descs = func->eps[idx].descs;
1964 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
1965 }
1966 break;
1967 }
1968
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001969 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001970 *valuep = newValue;
1971 return 0;
1972}
1973
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001974static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
1975 struct usb_configuration *c)
1976{
1977 struct ffs_function *func = ffs_func_from_usb(f);
1978 struct f_fs_opts *ffs_opts =
1979 container_of(f->fi, struct f_fs_opts, func_inst);
1980 int ret;
1981
1982 ENTER();
1983
1984 /*
1985 * Legacy gadget triggers binding in functionfs_ready_callback,
1986 * which already uses locking; taking the same lock here would
1987 * cause a deadlock.
1988 *
1989 * Configfs-enabled gadgets however do need ffs_dev_lock.
1990 */
1991 if (!ffs_opts->no_configfs)
1992 ffs_dev_lock();
1993 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
1994 func->ffs = ffs_opts->dev->ffs_data;
1995 if (!ffs_opts->no_configfs)
1996 ffs_dev_unlock();
1997 if (ret)
1998 return ERR_PTR(ret);
1999
2000 func->conf = c;
2001 func->gadget = c->cdev->gadget;
2002
2003 ffs_data_get(func->ffs);
2004
2005 /*
2006 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2007 * configurations are bound in sequence with list_for_each_entry,
2008 * in each configuration its functions are bound in sequence
2009 * with list_for_each_entry, so we assume no race condition
2010 * with regard to ffs_opts->bound access
2011 */
2012 if (!ffs_opts->refcnt) {
2013 ret = functionfs_bind(func->ffs, c->cdev);
2014 if (ret)
2015 return ERR_PTR(ret);
2016 }
2017 ffs_opts->refcnt++;
2018 func->function.strings = func->ffs->stringtabs;
2019
2020 return ffs_opts;
2021}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002022
2023static int _ffs_func_bind(struct usb_configuration *c,
2024 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002025{
2026 struct ffs_function *func = ffs_func_from_usb(f);
2027 struct ffs_data *ffs = func->ffs;
2028
2029 const int full = !!func->ffs->fs_descs_count;
2030 const int high = gadget_is_dualspeed(func->gadget) &&
2031 func->ffs->hs_descs_count;
2032
2033 int ret;
2034
2035 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002036 vla_group(d);
2037 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2038 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2039 full ? ffs->fs_descs_count + 1 : 0);
2040 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2041 high ? ffs->hs_descs_count + 1 : 0);
2042 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
2043 vla_item_with_sz(d, char, raw_descs,
2044 high ? ffs->raw_descs_length : ffs->raw_fs_descs_length);
2045 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002046
2047 ENTER();
2048
2049 /* Only high speed but not supported by gadget? */
2050 if (unlikely(!(full | high)))
2051 return -ENOTSUPP;
2052
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002053 /* Allocate a single chunk, less management later on */
2054 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2055 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002056 return -ENOMEM;
2057
2058 /* Zero */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002059 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
2060 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16,
2061 d_raw_descs__sz);
2062 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2063 for (ret = ffs->eps_count; ret; --ret) {
2064 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002065
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002066 ptr = vla_ptr(vlabuf, d, eps);
2067 ptr[ret].num = -1;
2068 }
2069
2070 /* Save pointers
2071 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2072 */
2073 func->eps = vla_ptr(vlabuf, d, eps);
2074 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002075
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002076 /*
2077 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002078 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002079 * numbers without worrying that it may be described later on.
2080 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002081 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002082 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002083 ret = ffs_do_descs(ffs->fs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002084 vla_ptr(vlabuf, d, raw_descs),
2085 d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002086 __ffs_func_bind_do_descs, func);
2087 if (unlikely(ret < 0))
2088 goto error;
2089 } else {
2090 ret = 0;
2091 }
2092
2093 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002094 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002095 ret = ffs_do_descs(ffs->hs_descs_count,
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002096 vla_ptr(vlabuf, d, raw_descs) + ret,
2097 d_raw_descs__sz - ret,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002098 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002099 if (unlikely(ret < 0))
2100 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002101 }
2102
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002103 /*
2104 * Now handle interface numbers allocation and interface and
2105 * endpoint numbers rewriting. We can do that in one go
2106 * now.
2107 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002108 ret = ffs_do_descs(ffs->fs_descs_count +
2109 (high ? ffs->hs_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002110 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002111 __ffs_func_bind_do_nums, func);
2112 if (unlikely(ret < 0))
2113 goto error;
2114
2115 /* And we're done */
2116 ffs_event_add(ffs, FUNCTIONFS_BIND);
2117 return 0;
2118
2119error:
2120 /* XXX Do we need to release all claimed endpoints here? */
2121 return ret;
2122}
2123
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002124static int ffs_func_bind(struct usb_configuration *c,
2125 struct usb_function *f)
2126{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002127 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2128
2129 if (IS_ERR(ffs_opts))
2130 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002131
2132 return _ffs_func_bind(c, f);
2133}
2134
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002135
2136/* Other USB function hooks *************************************************/
2137
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002138static int ffs_func_set_alt(struct usb_function *f,
2139 unsigned interface, unsigned alt)
2140{
2141 struct ffs_function *func = ffs_func_from_usb(f);
2142 struct ffs_data *ffs = func->ffs;
2143 int ret = 0, intf;
2144
2145 if (alt != (unsigned)-1) {
2146 intf = ffs_func_revmap_intf(func, interface);
2147 if (unlikely(intf < 0))
2148 return intf;
2149 }
2150
2151 if (ffs->func)
2152 ffs_func_eps_disable(ffs->func);
2153
2154 if (ffs->state != FFS_ACTIVE)
2155 return -ENODEV;
2156
2157 if (alt == (unsigned)-1) {
2158 ffs->func = NULL;
2159 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2160 return 0;
2161 }
2162
2163 ffs->func = func;
2164 ret = ffs_func_eps_enable(func);
2165 if (likely(ret >= 0))
2166 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2167 return ret;
2168}
2169
2170static void ffs_func_disable(struct usb_function *f)
2171{
2172 ffs_func_set_alt(f, 0, (unsigned)-1);
2173}
2174
2175static int ffs_func_setup(struct usb_function *f,
2176 const struct usb_ctrlrequest *creq)
2177{
2178 struct ffs_function *func = ffs_func_from_usb(f);
2179 struct ffs_data *ffs = func->ffs;
2180 unsigned long flags;
2181 int ret;
2182
2183 ENTER();
2184
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002185 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2186 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2187 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2188 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2189 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002190
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002191 /*
2192 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002193 * (notable exceptions are set/get interface) so we need to
2194 * handle them. All other either handled by composite or
2195 * passed to usb_configuration->setup() (if one is set). No
2196 * matter, we will handle requests directed to endpoint here
2197 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002198 * other request?
2199 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002200 if (ffs->state != FFS_ACTIVE)
2201 return -ENODEV;
2202
2203 switch (creq->bRequestType & USB_RECIP_MASK) {
2204 case USB_RECIP_INTERFACE:
2205 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2206 if (unlikely(ret < 0))
2207 return ret;
2208 break;
2209
2210 case USB_RECIP_ENDPOINT:
2211 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2212 if (unlikely(ret < 0))
2213 return ret;
2214 break;
2215
2216 default:
2217 return -EOPNOTSUPP;
2218 }
2219
2220 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2221 ffs->ev.setup = *creq;
2222 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2223 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2224 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2225
2226 return 0;
2227}
2228
2229static void ffs_func_suspend(struct usb_function *f)
2230{
2231 ENTER();
2232 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2233}
2234
2235static void ffs_func_resume(struct usb_function *f)
2236{
2237 ENTER();
2238 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2239}
2240
2241
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002242/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002243
2244static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2245{
2246 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2247 return num ? num : -EDOM;
2248}
2249
2250static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2251{
2252 short *nums = func->interfaces_nums;
2253 unsigned count = func->ffs->interfaces_count;
2254
2255 for (; count; --count, ++nums) {
2256 if (*nums >= 0 && *nums == intf)
2257 return nums - func->interfaces_nums;
2258 }
2259
2260 return -EDOM;
2261}
2262
2263
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002264/* Devices management *******************************************************/
2265
2266static LIST_HEAD(ffs_devices);
2267
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002268static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002269{
2270 struct ffs_dev *dev;
2271
2272 list_for_each_entry(dev, &ffs_devices, entry) {
2273 if (!dev->name || !name)
2274 continue;
2275 if (strcmp(dev->name, name) == 0)
2276 return dev;
2277 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002278
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002279 return NULL;
2280}
2281
2282/*
2283 * ffs_lock must be taken by the caller of this function
2284 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002285static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002286{
2287 struct ffs_dev *dev;
2288
2289 if (list_is_singular(&ffs_devices)) {
2290 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2291 if (dev->single)
2292 return dev;
2293 }
2294
2295 return NULL;
2296}
2297
2298/*
2299 * ffs_lock must be taken by the caller of this function
2300 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002301static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002302{
2303 struct ffs_dev *dev;
2304
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002305 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002306 if (dev)
2307 return dev;
2308
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002309 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002310}
2311
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002312/* Configfs support *********************************************************/
2313
2314static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2315{
2316 return container_of(to_config_group(item), struct f_fs_opts,
2317 func_inst.group);
2318}
2319
2320static void ffs_attr_release(struct config_item *item)
2321{
2322 struct f_fs_opts *opts = to_ffs_opts(item);
2323
2324 usb_put_function_instance(&opts->func_inst);
2325}
2326
2327static struct configfs_item_operations ffs_item_ops = {
2328 .release = ffs_attr_release,
2329};
2330
2331static struct config_item_type ffs_func_type = {
2332 .ct_item_ops = &ffs_item_ops,
2333 .ct_owner = THIS_MODULE,
2334};
2335
2336
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002337/* Function registration interface ******************************************/
2338
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002339static void ffs_free_inst(struct usb_function_instance *f)
2340{
2341 struct f_fs_opts *opts;
2342
2343 opts = to_f_fs_opts(f);
2344 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002345 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002346 ffs_dev_unlock();
2347 kfree(opts);
2348}
2349
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002350#define MAX_INST_NAME_LEN 40
2351
2352static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2353{
2354 struct f_fs_opts *opts;
2355 char *ptr;
2356 const char *tmp;
2357 int name_len, ret;
2358
2359 name_len = strlen(name) + 1;
2360 if (name_len > MAX_INST_NAME_LEN)
2361 return -ENAMETOOLONG;
2362
2363 ptr = kstrndup(name, name_len, GFP_KERNEL);
2364 if (!ptr)
2365 return -ENOMEM;
2366
2367 opts = to_f_fs_opts(fi);
2368 tmp = NULL;
2369
2370 ffs_dev_lock();
2371
2372 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2373 ret = _ffs_name_dev(opts->dev, ptr);
2374 if (ret) {
2375 kfree(ptr);
2376 ffs_dev_unlock();
2377 return ret;
2378 }
2379 opts->dev->name_allocated = true;
2380
2381 ffs_dev_unlock();
2382
2383 kfree(tmp);
2384
2385 return 0;
2386}
2387
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002388static struct usb_function_instance *ffs_alloc_inst(void)
2389{
2390 struct f_fs_opts *opts;
2391 struct ffs_dev *dev;
2392
2393 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2394 if (!opts)
2395 return ERR_PTR(-ENOMEM);
2396
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002397 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002398 opts->func_inst.free_func_inst = ffs_free_inst;
2399 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002400 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002401 ffs_dev_unlock();
2402 if (IS_ERR(dev)) {
2403 kfree(opts);
2404 return ERR_CAST(dev);
2405 }
2406 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002407 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002408
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002409 config_group_init_type_name(&opts->func_inst.group, "",
2410 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002411 return &opts->func_inst;
2412}
2413
2414static void ffs_free(struct usb_function *f)
2415{
2416 kfree(ffs_func_from_usb(f));
2417}
2418
2419static void ffs_func_unbind(struct usb_configuration *c,
2420 struct usb_function *f)
2421{
2422 struct ffs_function *func = ffs_func_from_usb(f);
2423 struct ffs_data *ffs = func->ffs;
2424 struct f_fs_opts *opts =
2425 container_of(f->fi, struct f_fs_opts, func_inst);
2426 struct ffs_ep *ep = func->eps;
2427 unsigned count = ffs->eps_count;
2428 unsigned long flags;
2429
2430 ENTER();
2431 if (ffs->func == func) {
2432 ffs_func_eps_disable(func);
2433 ffs->func = NULL;
2434 }
2435
2436 if (!--opts->refcnt)
2437 functionfs_unbind(ffs);
2438
2439 /* cleanup after autoconfig */
2440 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2441 do {
2442 if (ep->ep && ep->req)
2443 usb_ep_free_request(ep->ep, ep->req);
2444 ep->req = NULL;
2445 ++ep;
2446 } while (--count);
2447 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2448 kfree(func->eps);
2449 func->eps = NULL;
2450 /*
2451 * eps, descriptors and interfaces_nums are allocated in the
2452 * same chunk so only one free is required.
2453 */
2454 func->function.fs_descriptors = NULL;
2455 func->function.hs_descriptors = NULL;
2456 func->interfaces_nums = NULL;
2457
2458 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2459}
2460
2461static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2462{
2463 struct ffs_function *func;
2464
2465 ENTER();
2466
2467 func = kzalloc(sizeof(*func), GFP_KERNEL);
2468 if (unlikely(!func))
2469 return ERR_PTR(-ENOMEM);
2470
2471 func->function.name = "Function FS Gadget";
2472
2473 func->function.bind = ffs_func_bind;
2474 func->function.unbind = ffs_func_unbind;
2475 func->function.set_alt = ffs_func_set_alt;
2476 func->function.disable = ffs_func_disable;
2477 func->function.setup = ffs_func_setup;
2478 func->function.suspend = ffs_func_suspend;
2479 func->function.resume = ffs_func_resume;
2480 func->function.free_func = ffs_free;
2481
2482 return &func->function;
2483}
2484
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002485/*
2486 * ffs_lock must be taken by the caller of this function
2487 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002488static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002489{
2490 struct ffs_dev *dev;
2491 int ret;
2492
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002493 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002494 return ERR_PTR(-EBUSY);
2495
2496 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2497 if (!dev)
2498 return ERR_PTR(-ENOMEM);
2499
2500 if (list_empty(&ffs_devices)) {
2501 ret = functionfs_init();
2502 if (ret) {
2503 kfree(dev);
2504 return ERR_PTR(ret);
2505 }
2506 }
2507
2508 list_add(&dev->entry, &ffs_devices);
2509
2510 return dev;
2511}
2512
2513/*
2514 * ffs_lock must be taken by the caller of this function
2515 * The caller is responsible for "name" being available whenever f_fs needs it
2516 */
2517static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2518{
2519 struct ffs_dev *existing;
2520
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002521 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002522 if (existing)
2523 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01002524
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002525 dev->name = name;
2526
2527 return 0;
2528}
2529
2530/*
2531 * The caller is responsible for "name" being available whenever f_fs needs it
2532 */
2533int ffs_name_dev(struct ffs_dev *dev, const char *name)
2534{
2535 int ret;
2536
2537 ffs_dev_lock();
2538 ret = _ffs_name_dev(dev, name);
2539 ffs_dev_unlock();
2540
2541 return ret;
2542}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002543EXPORT_SYMBOL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002544
2545int ffs_single_dev(struct ffs_dev *dev)
2546{
2547 int ret;
2548
2549 ret = 0;
2550 ffs_dev_lock();
2551
2552 if (!list_is_singular(&ffs_devices))
2553 ret = -EBUSY;
2554 else
2555 dev->single = true;
2556
2557 ffs_dev_unlock();
2558 return ret;
2559}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002560EXPORT_SYMBOL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002561
2562/*
2563 * ffs_lock must be taken by the caller of this function
2564 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002565static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002566{
2567 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002568 if (dev->name_allocated)
2569 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002570 kfree(dev);
2571 if (list_empty(&ffs_devices))
2572 functionfs_cleanup();
2573}
2574
2575static void *ffs_acquire_dev(const char *dev_name)
2576{
2577 struct ffs_dev *ffs_dev;
2578
2579 ENTER();
2580 ffs_dev_lock();
2581
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002582 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002583 if (!ffs_dev)
2584 ffs_dev = ERR_PTR(-ENODEV);
2585 else if (ffs_dev->mounted)
2586 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002587 else if (ffs_dev->ffs_acquire_dev_callback &&
2588 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2589 ffs_dev = ERR_PTR(-ENODEV);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002590 else
2591 ffs_dev->mounted = true;
2592
2593 ffs_dev_unlock();
2594 return ffs_dev;
2595}
2596
2597static void ffs_release_dev(struct ffs_data *ffs_data)
2598{
2599 struct ffs_dev *ffs_dev;
2600
2601 ENTER();
2602 ffs_dev_lock();
2603
2604 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002605 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002606 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002607
2608 if (ffs_dev->ffs_release_dev_callback)
2609 ffs_dev->ffs_release_dev_callback(ffs_dev);
2610 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002611
2612 ffs_dev_unlock();
2613}
2614
2615static int ffs_ready(struct ffs_data *ffs)
2616{
2617 struct ffs_dev *ffs_obj;
2618 int ret = 0;
2619
2620 ENTER();
2621 ffs_dev_lock();
2622
2623 ffs_obj = ffs->private_data;
2624 if (!ffs_obj) {
2625 ret = -EINVAL;
2626 goto done;
2627 }
2628 if (WARN_ON(ffs_obj->desc_ready)) {
2629 ret = -EBUSY;
2630 goto done;
2631 }
2632
2633 ffs_obj->desc_ready = true;
2634 ffs_obj->ffs_data = ffs;
2635
2636 if (ffs_obj->ffs_ready_callback)
2637 ret = ffs_obj->ffs_ready_callback(ffs);
2638
2639done:
2640 ffs_dev_unlock();
2641 return ret;
2642}
2643
2644static void ffs_closed(struct ffs_data *ffs)
2645{
2646 struct ffs_dev *ffs_obj;
2647
2648 ENTER();
2649 ffs_dev_lock();
2650
2651 ffs_obj = ffs->private_data;
2652 if (!ffs_obj)
2653 goto done;
2654
2655 ffs_obj->desc_ready = false;
2656
2657 if (ffs_obj->ffs_closed_callback)
2658 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002659
2660 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
2661 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
2662 goto done;
2663
2664 unregister_gadget_item(ffs_obj->opts->
2665 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002666done:
2667 ffs_dev_unlock();
2668}
2669
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002670/* Misc helper functions ****************************************************/
2671
2672static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2673{
2674 return nonblock
2675 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2676 : mutex_lock_interruptible(mutex);
2677}
2678
Al Viro260ef312012-09-26 21:43:45 -04002679static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002680{
2681 char *data;
2682
2683 if (unlikely(!len))
2684 return NULL;
2685
2686 data = kmalloc(len, GFP_KERNEL);
2687 if (unlikely(!data))
2688 return ERR_PTR(-ENOMEM);
2689
2690 if (unlikely(__copy_from_user(data, buf, len))) {
2691 kfree(data);
2692 return ERR_PTR(-EFAULT);
2693 }
2694
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002695 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002696 ffs_dump_mem("", data, len);
2697
2698 return data;
2699}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002700
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002701DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
2702MODULE_LICENSE("GPL");
2703MODULE_AUTHOR("Michal Nazarewicz");