blob: 0dc3552d13603282d3d631aa1a7d06078bec0c3b [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
Robert Baldyga2e4c7552014-02-10 10:42:44 +010031#include <linux/aio.h>
32#include <linux/mmu_context.h>
Robert Baldyga23de91e2014-02-10 10:42:43 +010033#include <linux/poll.h>
34
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010035#include "u_fs.h"
Andrzej Pietrasiewicz74d48462014-05-08 14:06:21 +020036#include "u_f.h"
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +020037#include "u_os_desc.h"
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010038#include "configfs.h"
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020039
40#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
41
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020042/* Reference counter handling */
43static void ffs_data_get(struct ffs_data *ffs);
44static void ffs_data_put(struct ffs_data *ffs);
45/* Creates new ffs_data object. */
46static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
47
48/* Opened counter handling. */
49static void ffs_data_opened(struct ffs_data *ffs);
50static void ffs_data_closed(struct ffs_data *ffs);
51
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010052/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020053static int __must_check
54__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
55static int __must_check
56__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
57
58
59/* The function structure ***************************************************/
60
61struct ffs_ep;
62
63struct ffs_function {
64 struct usb_configuration *conf;
65 struct usb_gadget *gadget;
66 struct ffs_data *ffs;
67
68 struct ffs_ep *eps;
69 u8 eps_revmap[16];
70 short *interfaces_nums;
71
72 struct usb_function function;
73};
74
75
76static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
77{
78 return container_of(f, struct ffs_function, function);
79}
80
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020081
Michal Nazarewicza7ecf052014-02-10 10:42:41 +010082static inline enum ffs_setup_state
83ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
84{
85 return (enum ffs_setup_state)
86 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
87}
88
89
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020090static void ffs_func_eps_disable(struct ffs_function *func);
91static int __must_check ffs_func_eps_enable(struct ffs_function *func);
92
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020093static int ffs_func_bind(struct usb_configuration *,
94 struct usb_function *);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020095static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
96static void ffs_func_disable(struct usb_function *);
97static int ffs_func_setup(struct usb_function *,
98 const struct usb_ctrlrequest *);
99static void ffs_func_suspend(struct usb_function *);
100static void ffs_func_resume(struct usb_function *);
101
102
103static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
104static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
105
106
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107/* The endpoints structures *************************************************/
108
109struct ffs_ep {
110 struct usb_ep *ep; /* P: ffs->eps_lock */
111 struct usb_request *req; /* P: epfile->mutex */
112
Manu Gautam8d4e8972014-02-28 16:50:22 +0530113 /* [0]: full speed, [1]: high speed, [2]: super speed */
114 struct usb_endpoint_descriptor *descs[3];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200115
116 u8 num;
117
118 int status; /* P: epfile->mutex */
119};
120
121struct ffs_epfile {
122 /* Protects ep->ep and ep->req. */
123 struct mutex mutex;
124 wait_queue_head_t wait;
125
126 struct ffs_data *ffs;
127 struct ffs_ep *ep; /* P: ffs->eps_lock */
128
129 struct dentry *dentry;
130
131 char name[5];
132
133 unsigned char in; /* P: ffs->eps_lock */
134 unsigned char isoc; /* P: ffs->eps_lock */
135
136 unsigned char _pad;
137};
138
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100139/* ffs_io_data structure ***************************************************/
140
141struct ffs_io_data {
142 bool aio;
143 bool read;
144
145 struct kiocb *kiocb;
146 const struct iovec *iovec;
147 unsigned long nr_segs;
148 char __user *buf;
149 size_t len;
150
151 struct mm_struct *mm;
152 struct work_struct work;
153
154 struct usb_ep *ep;
155 struct usb_request *req;
156};
157
Robert Baldyga6d5c1c72014-08-25 11:16:27 +0200158struct ffs_desc_helper {
159 struct ffs_data *ffs;
160 unsigned interfaces_count;
161 unsigned eps_count;
162};
163
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200164static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
165static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
166
167static struct inode *__must_check
168ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
169 const struct file_operations *fops,
170 struct dentry **dentry_p);
171
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100172/* Devices management *******************************************************/
173
174DEFINE_MUTEX(ffs_lock);
Felipe Balbi0700faa2014-04-01 13:19:32 -0500175EXPORT_SYMBOL_GPL(ffs_lock);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100176
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100177static struct ffs_dev *_ffs_find_dev(const char *name);
178static struct ffs_dev *_ffs_alloc_dev(void);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +0100179static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100180static void _ffs_free_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100181static void *ffs_acquire_dev(const char *dev_name);
182static void ffs_release_dev(struct ffs_data *ffs_data);
183static int ffs_ready(struct ffs_data *ffs);
184static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200185
186/* Misc helper functions ****************************************************/
187
188static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
189 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400190static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200191 __attribute__((warn_unused_result, nonnull));
192
193
194/* Control file aka ep0 *****************************************************/
195
196static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
197{
198 struct ffs_data *ffs = req->context;
199
200 complete_all(&ffs->ep0req_completion);
201}
202
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200203static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
204{
205 struct usb_request *req = ffs->ep0req;
206 int ret;
207
208 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
209
210 spin_unlock_irq(&ffs->ev.waitq.lock);
211
212 req->buf = data;
213 req->length = len;
214
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100215 /*
216 * UDC layer requires to provide a buffer even for ZLP, but should
217 * not use it at all. Let's provide some poisoned pointer to catch
218 * possible bug in the driver.
219 */
220 if (req->buf == NULL)
221 req->buf = (void *)0xDEADBABE;
222
Wolfram Sang16735d02013-11-14 14:32:02 -0800223 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200224
225 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
226 if (unlikely(ret < 0))
227 return ret;
228
229 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
230 if (unlikely(ret)) {
231 usb_ep_dequeue(ffs->gadget->ep0, req);
232 return -EINTR;
233 }
234
235 ffs->setup_state = FFS_NO_SETUP;
Robert Baldyga0a7b1f82014-02-10 10:42:42 +0100236 return req->status ? req->status : req->actual;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200237}
238
239static int __ffs_ep0_stall(struct ffs_data *ffs)
240{
241 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100242 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200243 usb_ep_set_halt(ffs->gadget->ep0);
244 ffs->setup_state = FFS_NO_SETUP;
245 return -EL2HLT;
246 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100247 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200248 return -ESRCH;
249 }
250}
251
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200252static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
253 size_t len, loff_t *ptr)
254{
255 struct ffs_data *ffs = file->private_data;
256 ssize_t ret;
257 char *data;
258
259 ENTER();
260
261 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100262 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200263 return -EIDRM;
264
265 /* Acquire mutex */
266 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
267 if (unlikely(ret < 0))
268 return ret;
269
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200270 /* Check state */
271 switch (ffs->state) {
272 case FFS_READ_DESCRIPTORS:
273 case FFS_READ_STRINGS:
274 /* Copy data */
275 if (unlikely(len < 16)) {
276 ret = -EINVAL;
277 break;
278 }
279
280 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100281 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200282 ret = PTR_ERR(data);
283 break;
284 }
285
286 /* Handle data */
287 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100288 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200289 ret = __ffs_data_got_descs(ffs, data, len);
290 if (unlikely(ret < 0))
291 break;
292
293 ffs->state = FFS_READ_STRINGS;
294 ret = len;
295 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100296 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200297 ret = __ffs_data_got_strings(ffs, data, len);
298 if (unlikely(ret < 0))
299 break;
300
301 ret = ffs_epfiles_create(ffs);
302 if (unlikely(ret)) {
303 ffs->state = FFS_CLOSING;
304 break;
305 }
306
307 ffs->state = FFS_ACTIVE;
308 mutex_unlock(&ffs->mutex);
309
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100310 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200311 if (unlikely(ret < 0)) {
312 ffs->state = FFS_CLOSING;
313 return ret;
314 }
315
316 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
317 return len;
318 }
319 break;
320
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200321 case FFS_ACTIVE:
322 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100323 /*
324 * We're called from user space, we can use _irq
325 * rather then _irqsave
326 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200327 spin_lock_irq(&ffs->ev.waitq.lock);
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100328 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100329 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200330 ret = -EIDRM;
331 goto done_spin;
332
333 case FFS_NO_SETUP:
334 ret = -ESRCH;
335 goto done_spin;
336
337 case FFS_SETUP_PENDING:
338 break;
339 }
340
341 /* FFS_SETUP_PENDING */
342 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
343 spin_unlock_irq(&ffs->ev.waitq.lock);
344 ret = __ffs_ep0_stall(ffs);
345 break;
346 }
347
348 /* FFS_SETUP_PENDING and not stall */
349 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
350
351 spin_unlock_irq(&ffs->ev.waitq.lock);
352
353 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100354 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200355 ret = PTR_ERR(data);
356 break;
357 }
358
359 spin_lock_irq(&ffs->ev.waitq.lock);
360
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100361 /*
362 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200363 * but the state of setup could have changed from
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100364 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200365 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100366 * from user space in vain but it's unlikely.
367 *
368 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200369 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
370 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100371 * mutex.
372 */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100373 if (ffs_setup_state_clear_cancelled(ffs) ==
374 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200375 ret = -EIDRM;
376done_spin:
377 spin_unlock_irq(&ffs->ev.waitq.lock);
378 } else {
379 /* unlocks spinlock */
380 ret = __ffs_ep0_queue_wait(ffs, data, len);
381 }
382 kfree(data);
383 break;
384
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200385 default:
386 ret = -EBADFD;
387 break;
388 }
389
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200390 mutex_unlock(&ffs->mutex);
391 return ret;
392}
393
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200394static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
395 size_t n)
396{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100397 /*
398 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
399 * to release them.
400 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200401 struct usb_functionfs_event events[n];
402 unsigned i = 0;
403
404 memset(events, 0, sizeof events);
405
406 do {
407 events[i].type = ffs->ev.types[i];
408 if (events[i].type == FUNCTIONFS_SETUP) {
409 events[i].u.setup = ffs->ev.setup;
410 ffs->setup_state = FFS_SETUP_PENDING;
411 }
412 } while (++i < n);
413
414 if (n < ffs->ev.count) {
415 ffs->ev.count -= n;
416 memmove(ffs->ev.types, ffs->ev.types + n,
417 ffs->ev.count * sizeof *ffs->ev.types);
418 } else {
419 ffs->ev.count = 0;
420 }
421
422 spin_unlock_irq(&ffs->ev.waitq.lock);
423 mutex_unlock(&ffs->mutex);
424
425 return unlikely(__copy_to_user(buf, events, sizeof events))
426 ? -EFAULT : sizeof events;
427}
428
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200429static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
430 size_t len, loff_t *ptr)
431{
432 struct ffs_data *ffs = file->private_data;
433 char *data = NULL;
434 size_t n;
435 int ret;
436
437 ENTER();
438
439 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100440 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200441 return -EIDRM;
442
443 /* Acquire mutex */
444 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
445 if (unlikely(ret < 0))
446 return ret;
447
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200448 /* Check state */
449 if (ffs->state != FFS_ACTIVE) {
450 ret = -EBADFD;
451 goto done_mutex;
452 }
453
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100454 /*
455 * We're called from user space, we can use _irq rather then
456 * _irqsave
457 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200458 spin_lock_irq(&ffs->ev.waitq.lock);
459
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100460 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100461 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200462 ret = -EIDRM;
463 break;
464
465 case FFS_NO_SETUP:
466 n = len / sizeof(struct usb_functionfs_event);
467 if (unlikely(!n)) {
468 ret = -EINVAL;
469 break;
470 }
471
472 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
473 ret = -EAGAIN;
474 break;
475 }
476
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100477 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
478 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200479 ret = -EINTR;
480 break;
481 }
482
483 return __ffs_ep0_read_events(ffs, buf,
484 min(n, (size_t)ffs->ev.count));
485
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200486 case FFS_SETUP_PENDING:
487 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
488 spin_unlock_irq(&ffs->ev.waitq.lock);
489 ret = __ffs_ep0_stall(ffs);
490 goto done_mutex;
491 }
492
493 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
494
495 spin_unlock_irq(&ffs->ev.waitq.lock);
496
497 if (likely(len)) {
498 data = kmalloc(len, GFP_KERNEL);
499 if (unlikely(!data)) {
500 ret = -ENOMEM;
501 goto done_mutex;
502 }
503 }
504
505 spin_lock_irq(&ffs->ev.waitq.lock);
506
507 /* See ffs_ep0_write() */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100508 if (ffs_setup_state_clear_cancelled(ffs) ==
509 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200510 ret = -EIDRM;
511 break;
512 }
513
514 /* unlocks spinlock */
515 ret = __ffs_ep0_queue_wait(ffs, data, len);
516 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
517 ret = -EFAULT;
518 goto done_mutex;
519
520 default:
521 ret = -EBADFD;
522 break;
523 }
524
525 spin_unlock_irq(&ffs->ev.waitq.lock);
526done_mutex:
527 mutex_unlock(&ffs->mutex);
528 kfree(data);
529 return ret;
530}
531
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200532static int ffs_ep0_open(struct inode *inode, struct file *file)
533{
534 struct ffs_data *ffs = inode->i_private;
535
536 ENTER();
537
538 if (unlikely(ffs->state == FFS_CLOSING))
539 return -EBUSY;
540
541 file->private_data = ffs;
542 ffs_data_opened(ffs);
543
544 return 0;
545}
546
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200547static int ffs_ep0_release(struct inode *inode, struct file *file)
548{
549 struct ffs_data *ffs = file->private_data;
550
551 ENTER();
552
553 ffs_data_closed(ffs);
554
555 return 0;
556}
557
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200558static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
559{
560 struct ffs_data *ffs = file->private_data;
561 struct usb_gadget *gadget = ffs->gadget;
562 long ret;
563
564 ENTER();
565
566 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
567 struct ffs_function *func = ffs->func;
568 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200569 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200570 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200571 } else {
572 ret = -ENOTTY;
573 }
574
575 return ret;
576}
577
Robert Baldyga23de91e2014-02-10 10:42:43 +0100578static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
579{
580 struct ffs_data *ffs = file->private_data;
581 unsigned int mask = POLLWRNORM;
582 int ret;
583
584 poll_wait(file, &ffs->ev.waitq, wait);
585
586 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
587 if (unlikely(ret < 0))
588 return mask;
589
590 switch (ffs->state) {
591 case FFS_READ_DESCRIPTORS:
592 case FFS_READ_STRINGS:
593 mask |= POLLOUT;
594 break;
595
596 case FFS_ACTIVE:
597 switch (ffs->setup_state) {
598 case FFS_NO_SETUP:
599 if (ffs->ev.count)
600 mask |= POLLIN;
601 break;
602
603 case FFS_SETUP_PENDING:
604 case FFS_SETUP_CANCELLED:
605 mask |= (POLLIN | POLLOUT);
606 break;
607 }
608 case FFS_CLOSING:
609 break;
610 }
611
612 mutex_unlock(&ffs->mutex);
613
614 return mask;
615}
616
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200617static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200618 .llseek = no_llseek,
619
620 .open = ffs_ep0_open,
621 .write = ffs_ep0_write,
622 .read = ffs_ep0_read,
623 .release = ffs_ep0_release,
624 .unlocked_ioctl = ffs_ep0_ioctl,
Robert Baldyga23de91e2014-02-10 10:42:43 +0100625 .poll = ffs_ep0_poll,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200626};
627
628
629/* "Normal" endpoints operations ********************************************/
630
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200631static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
632{
633 ENTER();
634 if (likely(req->context)) {
635 struct ffs_ep *ep = _ep->driver_data;
636 ep->status = req->status ? req->status : req->actual;
637 complete(req->context);
638 }
639}
640
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100641static void ffs_user_copy_worker(struct work_struct *work)
642{
643 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
644 work);
645 int ret = io_data->req->status ? io_data->req->status :
646 io_data->req->actual;
647
648 if (io_data->read && ret > 0) {
649 int i;
650 size_t pos = 0;
651 use_mm(io_data->mm);
652 for (i = 0; i < io_data->nr_segs; i++) {
653 if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
654 &io_data->buf[pos],
655 io_data->iovec[i].iov_len))) {
656 ret = -EFAULT;
657 break;
658 }
659 pos += io_data->iovec[i].iov_len;
660 }
661 unuse_mm(io_data->mm);
662 }
663
664 aio_complete(io_data->kiocb, ret, ret);
665
666 usb_ep_free_request(io_data->ep, io_data->req);
667
668 io_data->kiocb->private = NULL;
669 if (io_data->read)
670 kfree(io_data->iovec);
671 kfree(io_data->buf);
672 kfree(io_data);
673}
674
675static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
676 struct usb_request *req)
677{
678 struct ffs_io_data *io_data = req->context;
679
680 ENTER();
681
682 INIT_WORK(&io_data->work, ffs_user_copy_worker);
683 schedule_work(&io_data->work);
684}
685
686static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200687{
688 struct ffs_epfile *epfile = file->private_data;
689 struct ffs_ep *ep;
690 char *data = NULL;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800691 ssize_t ret, data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200692 int halt;
693
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800694 /* Are we still active? */
695 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
696 ret = -ENODEV;
697 goto error;
698 }
699
700 /* Wait for endpoint to be enabled */
701 ep = epfile->ep;
702 if (!ep) {
703 if (file->f_flags & O_NONBLOCK) {
704 ret = -EAGAIN;
705 goto error;
706 }
707
708 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
709 if (ret) {
710 ret = -EINTR;
711 goto error;
712 }
713 }
714
715 /* Do we halt? */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100716 halt = (!io_data->read == !epfile->in);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800717 if (halt && epfile->isoc) {
718 ret = -EINVAL;
719 goto error;
720 }
721
722 /* Allocate & copy */
723 if (!halt) {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800724 /*
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100725 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
726 * before the waiting completes, so do not assign to 'gadget' earlier
727 */
728 struct usb_gadget *gadget = epfile->ffs->gadget;
729
Chao Bi97839ca2014-04-14 11:19:53 +0800730 spin_lock_irq(&epfile->ffs->eps_lock);
731 /* In the meantime, endpoint got disabled or changed. */
732 if (epfile->ep != ep) {
733 spin_unlock_irq(&epfile->ffs->eps_lock);
734 return -ESHUTDOWN;
735 }
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100736 /*
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800737 * Controller may require buffer size to be aligned to
738 * maxpacketsize of an out endpoint.
739 */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100740 data_len = io_data->read ?
741 usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
742 io_data->len;
Chao Bi97839ca2014-04-14 11:19:53 +0800743 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800744
745 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800746 if (unlikely(!data))
747 return -ENOMEM;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100748 if (io_data->aio && !io_data->read) {
749 int i;
750 size_t pos = 0;
751 for (i = 0; i < io_data->nr_segs; i++) {
752 if (unlikely(copy_from_user(&data[pos],
753 io_data->iovec[i].iov_base,
754 io_data->iovec[i].iov_len))) {
755 ret = -EFAULT;
756 goto error;
757 }
758 pos += io_data->iovec[i].iov_len;
759 }
760 } else {
761 if (!io_data->read &&
762 unlikely(__copy_from_user(data, io_data->buf,
763 io_data->len))) {
764 ret = -EFAULT;
765 goto error;
766 }
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800767 }
768 }
769
770 /* We will be using request */
771 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
772 if (unlikely(ret))
773 goto error;
774
775 spin_lock_irq(&epfile->ffs->eps_lock);
776
777 if (epfile->ep != ep) {
778 /* In the meantime, endpoint got disabled or changed. */
779 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200780 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800781 } else if (halt) {
782 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200783 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
784 usb_ep_set_halt(ep->ep);
785 spin_unlock_irq(&epfile->ffs->eps_lock);
786 ret = -EBADMSG;
787 } else {
788 /* Fire the request */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100789 struct usb_request *req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200790
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100791 if (io_data->aio) {
792 req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
793 if (unlikely(!req))
Robert Baldyga48968f82014-03-10 09:33:37 +0100794 goto error_lock;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200795
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100796 req->buf = data;
797 req->length = io_data->len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200798
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100799 io_data->buf = data;
800 io_data->ep = ep->ep;
801 io_data->req = req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200802
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100803 req->context = io_data;
804 req->complete = ffs_epfile_async_io_complete;
805
806 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
807 if (unlikely(ret)) {
808 usb_ep_free_request(ep->ep, req);
Robert Baldyga48968f82014-03-10 09:33:37 +0100809 goto error_lock;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100810 }
811 ret = -EIOCBQUEUED;
812
813 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200814 } else {
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100815 DECLARE_COMPLETION_ONSTACK(done);
816
817 req = ep->req;
818 req->buf = data;
819 req->length = io_data->len;
820
821 req->context = &done;
822 req->complete = ffs_epfile_io_complete;
823
824 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
825
826 spin_unlock_irq(&epfile->ffs->eps_lock);
827
828 if (unlikely(ret < 0)) {
829 /* nop */
830 } else if (unlikely(
831 wait_for_completion_interruptible(&done))) {
832 ret = -EINTR;
833 usb_ep_dequeue(ep->ep, req);
834 } else {
Chuansheng Liucfe919b2014-03-04 15:34:57 +0800835 /*
836 * XXX We may end up silently droping data
837 * here. Since data_len (i.e. req->length) may
838 * be bigger than len (after being rounded up
839 * to maxpacketsize), we may end up with more
840 * data then user space has space for.
841 */
842 ret = ep->status;
843 if (io_data->read && ret > 0) {
844 ret = min_t(size_t, ret, io_data->len);
845
846 if (unlikely(copy_to_user(io_data->buf,
847 data, ret)))
848 ret = -EFAULT;
849 }
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100850 }
851 kfree(data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200852 }
853 }
854
855 mutex_unlock(&epfile->mutex);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100856 return ret;
Robert Baldyga48968f82014-03-10 09:33:37 +0100857
858error_lock:
859 spin_unlock_irq(&epfile->ffs->eps_lock);
860 mutex_unlock(&epfile->mutex);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200861error:
862 kfree(data);
863 return ret;
864}
865
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200866static ssize_t
867ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
868 loff_t *ptr)
869{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100870 struct ffs_io_data io_data;
871
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200872 ENTER();
873
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100874 io_data.aio = false;
875 io_data.read = false;
876 io_data.buf = (char * __user)buf;
877 io_data.len = len;
878
879 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200880}
881
882static ssize_t
883ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
884{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100885 struct ffs_io_data io_data;
886
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200887 ENTER();
888
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100889 io_data.aio = false;
890 io_data.read = true;
891 io_data.buf = buf;
892 io_data.len = len;
893
894 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200895}
896
897static int
898ffs_epfile_open(struct inode *inode, struct file *file)
899{
900 struct ffs_epfile *epfile = inode->i_private;
901
902 ENTER();
903
904 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
905 return -ENODEV;
906
907 file->private_data = epfile;
908 ffs_data_opened(epfile->ffs);
909
910 return 0;
911}
912
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100913static int ffs_aio_cancel(struct kiocb *kiocb)
914{
915 struct ffs_io_data *io_data = kiocb->private;
916 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
917 int value;
918
919 ENTER();
920
921 spin_lock_irq(&epfile->ffs->eps_lock);
922
923 if (likely(io_data && io_data->ep && io_data->req))
924 value = usb_ep_dequeue(io_data->ep, io_data->req);
925 else
926 value = -EINVAL;
927
928 spin_unlock_irq(&epfile->ffs->eps_lock);
929
930 return value;
931}
932
933static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
934 const struct iovec *iovec,
935 unsigned long nr_segs, loff_t loff)
936{
937 struct ffs_io_data *io_data;
938
939 ENTER();
940
941 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
942 if (unlikely(!io_data))
943 return -ENOMEM;
944
945 io_data->aio = true;
946 io_data->read = false;
947 io_data->kiocb = kiocb;
948 io_data->iovec = iovec;
949 io_data->nr_segs = nr_segs;
950 io_data->len = kiocb->ki_nbytes;
951 io_data->mm = current->mm;
952
953 kiocb->private = io_data;
954
955 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
956
957 return ffs_epfile_io(kiocb->ki_filp, io_data);
958}
959
960static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
961 const struct iovec *iovec,
962 unsigned long nr_segs, loff_t loff)
963{
964 struct ffs_io_data *io_data;
965 struct iovec *iovec_copy;
966
967 ENTER();
968
969 iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
970 if (unlikely(!iovec_copy))
971 return -ENOMEM;
972
973 memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
974
975 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
976 if (unlikely(!io_data)) {
977 kfree(iovec_copy);
978 return -ENOMEM;
979 }
980
981 io_data->aio = true;
982 io_data->read = true;
983 io_data->kiocb = kiocb;
984 io_data->iovec = iovec_copy;
985 io_data->nr_segs = nr_segs;
986 io_data->len = kiocb->ki_nbytes;
987 io_data->mm = current->mm;
988
989 kiocb->private = io_data;
990
991 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
992
993 return ffs_epfile_io(kiocb->ki_filp, io_data);
994}
995
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200996static int
997ffs_epfile_release(struct inode *inode, struct file *file)
998{
999 struct ffs_epfile *epfile = inode->i_private;
1000
1001 ENTER();
1002
1003 ffs_data_closed(epfile->ffs);
1004
1005 return 0;
1006}
1007
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001008static long ffs_epfile_ioctl(struct file *file, unsigned code,
1009 unsigned long value)
1010{
1011 struct ffs_epfile *epfile = file->private_data;
1012 int ret;
1013
1014 ENTER();
1015
1016 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1017 return -ENODEV;
1018
1019 spin_lock_irq(&epfile->ffs->eps_lock);
1020 if (likely(epfile->ep)) {
1021 switch (code) {
1022 case FUNCTIONFS_FIFO_STATUS:
1023 ret = usb_ep_fifo_status(epfile->ep->ep);
1024 break;
1025 case FUNCTIONFS_FIFO_FLUSH:
1026 usb_ep_fifo_flush(epfile->ep->ep);
1027 ret = 0;
1028 break;
1029 case FUNCTIONFS_CLEAR_HALT:
1030 ret = usb_ep_clear_halt(epfile->ep->ep);
1031 break;
1032 case FUNCTIONFS_ENDPOINT_REVMAP:
1033 ret = epfile->ep->num;
1034 break;
1035 default:
1036 ret = -ENOTTY;
1037 }
1038 } else {
1039 ret = -ENODEV;
1040 }
1041 spin_unlock_irq(&epfile->ffs->eps_lock);
1042
1043 return ret;
1044}
1045
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001046static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001047 .llseek = no_llseek,
1048
1049 .open = ffs_epfile_open,
1050 .write = ffs_epfile_write,
1051 .read = ffs_epfile_read,
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001052 .aio_write = ffs_epfile_aio_write,
1053 .aio_read = ffs_epfile_aio_read,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001054 .release = ffs_epfile_release,
1055 .unlocked_ioctl = ffs_epfile_ioctl,
1056};
1057
1058
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001059/* File system and super block operations ***********************************/
1060
1061/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001062 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001063 * function configuration then later for event monitoring.
1064 */
1065
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001066static struct inode *__must_check
1067ffs_sb_make_inode(struct super_block *sb, void *data,
1068 const struct file_operations *fops,
1069 const struct inode_operations *iops,
1070 struct ffs_file_perms *perms)
1071{
1072 struct inode *inode;
1073
1074 ENTER();
1075
1076 inode = new_inode(sb);
1077
1078 if (likely(inode)) {
1079 struct timespec current_time = CURRENT_TIME;
1080
Al Viro12ba8d12010-10-27 04:19:36 +01001081 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001082 inode->i_mode = perms->mode;
1083 inode->i_uid = perms->uid;
1084 inode->i_gid = perms->gid;
1085 inode->i_atime = current_time;
1086 inode->i_mtime = current_time;
1087 inode->i_ctime = current_time;
1088 inode->i_private = data;
1089 if (fops)
1090 inode->i_fop = fops;
1091 if (iops)
1092 inode->i_op = iops;
1093 }
1094
1095 return inode;
1096}
1097
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001098/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001099static struct inode *ffs_sb_create_file(struct super_block *sb,
1100 const char *name, void *data,
1101 const struct file_operations *fops,
1102 struct dentry **dentry_p)
1103{
1104 struct ffs_data *ffs = sb->s_fs_info;
1105 struct dentry *dentry;
1106 struct inode *inode;
1107
1108 ENTER();
1109
1110 dentry = d_alloc_name(sb->s_root, name);
1111 if (unlikely(!dentry))
1112 return NULL;
1113
1114 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1115 if (unlikely(!inode)) {
1116 dput(dentry);
1117 return NULL;
1118 }
1119
1120 d_add(dentry, inode);
1121 if (dentry_p)
1122 *dentry_p = dentry;
1123
1124 return inode;
1125}
1126
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001127/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001128static const struct super_operations ffs_sb_operations = {
1129 .statfs = simple_statfs,
1130 .drop_inode = generic_delete_inode,
1131};
1132
1133struct ffs_sb_fill_data {
1134 struct ffs_file_perms perms;
1135 umode_t root_mode;
1136 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +01001137 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001138};
1139
1140static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1141{
1142 struct ffs_sb_fill_data *data = _data;
1143 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +01001144 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001145
1146 ENTER();
1147
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001148 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +01001149 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001150 sb->s_fs_info = ffs;
1151 sb->s_blocksize = PAGE_CACHE_SIZE;
1152 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1153 sb->s_magic = FUNCTIONFS_MAGIC;
1154 sb->s_op = &ffs_sb_operations;
1155 sb->s_time_gran = 1;
1156
1157 /* Root inode */
1158 data->perms.mode = data->root_mode;
1159 inode = ffs_sb_make_inode(sb, NULL,
1160 &simple_dir_operations,
1161 &simple_dir_inode_operations,
1162 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001163 sb->s_root = d_make_root(inode);
1164 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +01001165 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166
1167 /* EP0 file */
1168 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1169 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +01001170 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001171
1172 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001173}
1174
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001175static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1176{
1177 ENTER();
1178
1179 if (!opts || !*opts)
1180 return 0;
1181
1182 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001183 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001184 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001185
1186 /* Option limit */
1187 comma = strchr(opts, ',');
1188 if (comma)
1189 *comma = 0;
1190
1191 /* Value limit */
1192 eq = strchr(opts, '=');
1193 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001194 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001195 return -EINVAL;
1196 }
1197 *eq = 0;
1198
1199 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001200 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001201 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001202 return -EINVAL;
1203 }
1204
1205 /* Interpret option */
1206 switch (eq - opts) {
1207 case 5:
1208 if (!memcmp(opts, "rmode", 5))
1209 data->root_mode = (value & 0555) | S_IFDIR;
1210 else if (!memcmp(opts, "fmode", 5))
1211 data->perms.mode = (value & 0666) | S_IFREG;
1212 else
1213 goto invalid;
1214 break;
1215
1216 case 4:
1217 if (!memcmp(opts, "mode", 4)) {
1218 data->root_mode = (value & 0555) | S_IFDIR;
1219 data->perms.mode = (value & 0666) | S_IFREG;
1220 } else {
1221 goto invalid;
1222 }
1223 break;
1224
1225 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001226 if (!memcmp(opts, "uid", 3)) {
1227 data->perms.uid = make_kuid(current_user_ns(), value);
1228 if (!uid_valid(data->perms.uid)) {
1229 pr_err("%s: unmapped value: %lu\n", opts, value);
1230 return -EINVAL;
1231 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001232 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001233 data->perms.gid = make_kgid(current_user_ns(), value);
1234 if (!gid_valid(data->perms.gid)) {
1235 pr_err("%s: unmapped value: %lu\n", opts, value);
1236 return -EINVAL;
1237 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001238 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001239 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -08001240 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001241 break;
1242
1243 default:
1244invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001245 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001246 return -EINVAL;
1247 }
1248
1249 /* Next iteration */
1250 if (!comma)
1251 break;
1252 opts = comma + 1;
1253 }
1254
1255 return 0;
1256}
1257
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001258/* "mount -t functionfs dev_name /dev/function" ends up here */
1259
Al Virofc14f2f2010-07-25 01:48:30 +04001260static struct dentry *
1261ffs_fs_mount(struct file_system_type *t, int flags,
1262 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001263{
1264 struct ffs_sb_fill_data data = {
1265 .perms = {
1266 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001267 .uid = GLOBAL_ROOT_UID,
1268 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001269 },
1270 .root_mode = S_IFDIR | 0500,
1271 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001272 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001273 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001274 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001275 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001276
1277 ENTER();
1278
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001279 ret = ffs_fs_parse_opts(&data, opts);
1280 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001281 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001282
Al Viro2606b282013-09-20 17:14:21 +01001283 ffs = ffs_data_new();
1284 if (unlikely(!ffs))
1285 return ERR_PTR(-ENOMEM);
1286 ffs->file_perms = data.perms;
1287
1288 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1289 if (unlikely(!ffs->dev_name)) {
1290 ffs_data_put(ffs);
1291 return ERR_PTR(-ENOMEM);
1292 }
1293
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001294 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001295 if (IS_ERR(ffs_dev)) {
1296 ffs_data_put(ffs);
1297 return ERR_CAST(ffs_dev);
1298 }
1299 ffs->private_data = ffs_dev;
1300 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001301
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001302 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001303 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001304 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001305 ffs_data_put(data.ffs_data);
1306 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001307 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001308}
1309
1310static void
1311ffs_fs_kill_sb(struct super_block *sb)
1312{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001313 ENTER();
1314
1315 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001316 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001317 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001318 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001319 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001320}
1321
1322static struct file_system_type ffs_fs_type = {
1323 .owner = THIS_MODULE,
1324 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001325 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001326 .kill_sb = ffs_fs_kill_sb,
1327};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001328MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001329
1330
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001331/* Driver's main init/cleanup functions *************************************/
1332
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001333static int functionfs_init(void)
1334{
1335 int ret;
1336
1337 ENTER();
1338
1339 ret = register_filesystem(&ffs_fs_type);
1340 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001341 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001342 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001343 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001344
1345 return ret;
1346}
1347
1348static void functionfs_cleanup(void)
1349{
1350 ENTER();
1351
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001352 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001353 unregister_filesystem(&ffs_fs_type);
1354}
1355
1356
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001357/* ffs_data and ffs_function construction and destruction code **************/
1358
1359static void ffs_data_clear(struct ffs_data *ffs);
1360static void ffs_data_reset(struct ffs_data *ffs);
1361
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001362static void ffs_data_get(struct ffs_data *ffs)
1363{
1364 ENTER();
1365
1366 atomic_inc(&ffs->ref);
1367}
1368
1369static void ffs_data_opened(struct ffs_data *ffs)
1370{
1371 ENTER();
1372
1373 atomic_inc(&ffs->ref);
1374 atomic_inc(&ffs->opened);
1375}
1376
1377static void ffs_data_put(struct ffs_data *ffs)
1378{
1379 ENTER();
1380
1381 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001382 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001383 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001384 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001385 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001386 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001387 kfree(ffs);
1388 }
1389}
1390
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001391static void ffs_data_closed(struct ffs_data *ffs)
1392{
1393 ENTER();
1394
1395 if (atomic_dec_and_test(&ffs->opened)) {
1396 ffs->state = FFS_CLOSING;
1397 ffs_data_reset(ffs);
1398 }
1399
1400 ffs_data_put(ffs);
1401}
1402
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403static struct ffs_data *ffs_data_new(void)
1404{
1405 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1406 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001407 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001408
1409 ENTER();
1410
1411 atomic_set(&ffs->ref, 1);
1412 atomic_set(&ffs->opened, 0);
1413 ffs->state = FFS_READ_DESCRIPTORS;
1414 mutex_init(&ffs->mutex);
1415 spin_lock_init(&ffs->eps_lock);
1416 init_waitqueue_head(&ffs->ev.waitq);
1417 init_completion(&ffs->ep0req_completion);
1418
1419 /* XXX REVISIT need to update it in some places, or do we? */
1420 ffs->ev.can_stall = 1;
1421
1422 return ffs;
1423}
1424
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001425static void ffs_data_clear(struct ffs_data *ffs)
1426{
1427 ENTER();
1428
1429 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001430 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001431
1432 BUG_ON(ffs->gadget);
1433
1434 if (ffs->epfiles)
1435 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1436
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301437 kfree(ffs->raw_descs_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001438 kfree(ffs->raw_strings);
1439 kfree(ffs->stringtabs);
1440}
1441
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001442static void ffs_data_reset(struct ffs_data *ffs)
1443{
1444 ENTER();
1445
1446 ffs_data_clear(ffs);
1447
1448 ffs->epfiles = NULL;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301449 ffs->raw_descs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001450 ffs->raw_descs = NULL;
1451 ffs->raw_strings = NULL;
1452 ffs->stringtabs = NULL;
1453
1454 ffs->raw_descs_length = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001455 ffs->fs_descs_count = 0;
1456 ffs->hs_descs_count = 0;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301457 ffs->ss_descs_count = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001458
1459 ffs->strings_count = 0;
1460 ffs->interfaces_count = 0;
1461 ffs->eps_count = 0;
1462
1463 ffs->ev.count = 0;
1464
1465 ffs->state = FFS_READ_DESCRIPTORS;
1466 ffs->setup_state = FFS_NO_SETUP;
1467 ffs->flags = 0;
1468}
1469
1470
1471static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1472{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001473 struct usb_gadget_strings **lang;
1474 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001475
1476 ENTER();
1477
1478 if (WARN_ON(ffs->state != FFS_ACTIVE
1479 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1480 return -EBADFD;
1481
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001482 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1483 if (unlikely(first_id < 0))
1484 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001485
1486 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1487 if (unlikely(!ffs->ep0req))
1488 return -ENOMEM;
1489 ffs->ep0req->complete = ffs_ep0_complete;
1490 ffs->ep0req->context = ffs;
1491
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001492 lang = ffs->stringtabs;
Michal Nazarewiczf0688c82014-06-17 17:47:41 +02001493 if (lang) {
1494 for (; *lang; ++lang) {
1495 struct usb_string *str = (*lang)->strings;
1496 int id = first_id;
1497 for (; str->s; ++id, ++str)
1498 str->id = id;
1499 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001500 }
1501
1502 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001503 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001504 return 0;
1505}
1506
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001507static void functionfs_unbind(struct ffs_data *ffs)
1508{
1509 ENTER();
1510
1511 if (!WARN_ON(!ffs->gadget)) {
1512 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1513 ffs->ep0req = NULL;
1514 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001515 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001516 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001517 }
1518}
1519
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001520static int ffs_epfiles_create(struct ffs_data *ffs)
1521{
1522 struct ffs_epfile *epfile, *epfiles;
1523 unsigned i, count;
1524
1525 ENTER();
1526
1527 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001528 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001529 if (!epfiles)
1530 return -ENOMEM;
1531
1532 epfile = epfiles;
1533 for (i = 1; i <= count; ++i, ++epfile) {
1534 epfile->ffs = ffs;
1535 mutex_init(&epfile->mutex);
1536 init_waitqueue_head(&epfile->wait);
1537 sprintf(epfiles->name, "ep%u", i);
1538 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1539 &ffs_epfile_operations,
1540 &epfile->dentry))) {
1541 ffs_epfiles_destroy(epfiles, i - 1);
1542 return -ENOMEM;
1543 }
1544 }
1545
1546 ffs->epfiles = epfiles;
1547 return 0;
1548}
1549
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001550static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1551{
1552 struct ffs_epfile *epfile = epfiles;
1553
1554 ENTER();
1555
1556 for (; count; --count, ++epfile) {
1557 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1558 waitqueue_active(&epfile->wait));
1559 if (epfile->dentry) {
1560 d_delete(epfile->dentry);
1561 dput(epfile->dentry);
1562 epfile->dentry = NULL;
1563 }
1564 }
1565
1566 kfree(epfiles);
1567}
1568
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001569
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001570static void ffs_func_eps_disable(struct ffs_function *func)
1571{
1572 struct ffs_ep *ep = func->eps;
1573 struct ffs_epfile *epfile = func->ffs->epfiles;
1574 unsigned count = func->ffs->eps_count;
1575 unsigned long flags;
1576
1577 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1578 do {
1579 /* pending requests get nuked */
1580 if (likely(ep->ep))
1581 usb_ep_disable(ep->ep);
1582 epfile->ep = NULL;
1583
1584 ++ep;
1585 ++epfile;
1586 } while (--count);
1587 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1588}
1589
1590static int ffs_func_eps_enable(struct ffs_function *func)
1591{
1592 struct ffs_data *ffs = func->ffs;
1593 struct ffs_ep *ep = func->eps;
1594 struct ffs_epfile *epfile = ffs->epfiles;
1595 unsigned count = ffs->eps_count;
1596 unsigned long flags;
1597 int ret = 0;
1598
1599 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1600 do {
1601 struct usb_endpoint_descriptor *ds;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301602 int desc_idx;
1603
1604 if (ffs->gadget->speed == USB_SPEED_SUPER)
1605 desc_idx = 2;
1606 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1607 desc_idx = 1;
1608 else
1609 desc_idx = 0;
1610
1611 /* fall-back to lower speed if desc missing for current speed */
1612 do {
1613 ds = ep->descs[desc_idx];
1614 } while (!ds && --desc_idx >= 0);
1615
1616 if (!ds) {
1617 ret = -EINVAL;
1618 break;
1619 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001620
1621 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001622 ep->ep->desc = ds;
1623 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001624 if (likely(!ret)) {
1625 epfile->ep = ep;
1626 epfile->in = usb_endpoint_dir_in(ds);
1627 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1628 } else {
1629 break;
1630 }
1631
1632 wake_up(&epfile->wait);
1633
1634 ++ep;
1635 ++epfile;
1636 } while (--count);
1637 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1638
1639 return ret;
1640}
1641
1642
1643/* Parsing and building descriptors and strings *****************************/
1644
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001645/*
1646 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001647 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001648 * required by given configuration. Returns address after the
1649 * descriptor or NULL if data is invalid.
1650 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001651
1652enum ffs_entity_type {
1653 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1654};
1655
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02001656enum ffs_os_desc_type {
1657 FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
1658};
1659
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001660typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1661 u8 *valuep,
1662 struct usb_descriptor_header *desc,
1663 void *priv);
1664
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02001665typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
1666 struct usb_os_desc_header *h, void *data,
1667 unsigned len, void *priv);
1668
Andrzej Pietrasiewiczf96cbd12014-07-09 12:20:06 +02001669static int __must_check ffs_do_single_desc(char *data, unsigned len,
1670 ffs_entity_callback entity,
1671 void *priv)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001672{
1673 struct usb_descriptor_header *_ds = (void *)data;
1674 u8 length;
1675 int ret;
1676
1677 ENTER();
1678
1679 /* At least two bytes are required: length and type */
1680 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001681 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001682 return -EINVAL;
1683 }
1684
1685 /* If we have at least as many bytes as the descriptor takes? */
1686 length = _ds->bLength;
1687 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001688 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001689 return -EINVAL;
1690 }
1691
1692#define __entity_check_INTERFACE(val) 1
1693#define __entity_check_STRING(val) (val)
1694#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1695#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001696 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001697 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001698 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001699 return -EINVAL; \
1700 } \
1701 ret = entity(FFS_ ##type, &val, _ds, priv); \
1702 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001703 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001704 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001705 return ret; \
1706 } \
1707 } while (0)
1708
1709 /* Parse descriptor depending on type. */
1710 switch (_ds->bDescriptorType) {
1711 case USB_DT_DEVICE:
1712 case USB_DT_CONFIG:
1713 case USB_DT_STRING:
1714 case USB_DT_DEVICE_QUALIFIER:
1715 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001716 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001717 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001718 return -EINVAL;
1719
1720 case USB_DT_INTERFACE: {
1721 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001722 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001723 if (length != sizeof *ds)
1724 goto inv_length;
1725
1726 __entity(INTERFACE, ds->bInterfaceNumber);
1727 if (ds->iInterface)
1728 __entity(STRING, ds->iInterface);
1729 }
1730 break;
1731
1732 case USB_DT_ENDPOINT: {
1733 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001734 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001735 if (length != USB_DT_ENDPOINT_SIZE &&
1736 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1737 goto inv_length;
1738 __entity(ENDPOINT, ds->bEndpointAddress);
1739 }
1740 break;
1741
Koen Beel560f1182012-05-30 20:43:37 +02001742 case HID_DT_HID:
1743 pr_vdebug("hid descriptor\n");
1744 if (length != sizeof(struct hid_descriptor))
1745 goto inv_length;
1746 break;
1747
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001748 case USB_DT_OTG:
1749 if (length != sizeof(struct usb_otg_descriptor))
1750 goto inv_length;
1751 break;
1752
1753 case USB_DT_INTERFACE_ASSOCIATION: {
1754 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001755 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001756 if (length != sizeof *ds)
1757 goto inv_length;
1758 if (ds->iFunction)
1759 __entity(STRING, ds->iFunction);
1760 }
1761 break;
1762
Manu Gautam8d4e8972014-02-28 16:50:22 +05301763 case USB_DT_SS_ENDPOINT_COMP:
1764 pr_vdebug("EP SS companion descriptor\n");
1765 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1766 goto inv_length;
1767 break;
1768
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001769 case USB_DT_OTHER_SPEED_CONFIG:
1770 case USB_DT_INTERFACE_POWER:
1771 case USB_DT_DEBUG:
1772 case USB_DT_SECURITY:
1773 case USB_DT_CS_RADIO_CONTROL:
1774 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001775 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001776 return -EINVAL;
1777
1778 default:
1779 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001780 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001781 return -EINVAL;
1782
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001783inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001784 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001785 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001786 return -EINVAL;
1787 }
1788
1789#undef __entity
1790#undef __entity_check_DESCRIPTOR
1791#undef __entity_check_INTERFACE
1792#undef __entity_check_STRING
1793#undef __entity_check_ENDPOINT
1794
1795 return length;
1796}
1797
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001798static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1799 ffs_entity_callback entity, void *priv)
1800{
1801 const unsigned _len = len;
1802 unsigned long num = 0;
1803
1804 ENTER();
1805
1806 for (;;) {
1807 int ret;
1808
1809 if (num == count)
1810 data = NULL;
1811
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001812 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001813 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1814 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001815 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001816 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001817 return ret;
1818 }
1819
1820 if (!data)
1821 return _len - len;
1822
Andrzej Pietrasiewiczf96cbd12014-07-09 12:20:06 +02001823 ret = ffs_do_single_desc(data, len, entity, priv);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001824 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001825 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001826 return ret;
1827 }
1828
1829 len -= ret;
1830 data += ret;
1831 ++num;
1832 }
1833}
1834
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001835static int __ffs_data_do_entity(enum ffs_entity_type type,
1836 u8 *valuep, struct usb_descriptor_header *desc,
1837 void *priv)
1838{
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02001839 struct ffs_desc_helper *helper = priv;
1840 struct usb_endpoint_descriptor *d;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001841
1842 ENTER();
1843
1844 switch (type) {
1845 case FFS_DESCRIPTOR:
1846 break;
1847
1848 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001849 /*
1850 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001851 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001852 * "n+1" interfaces.
1853 */
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02001854 if (*valuep >= helper->interfaces_count)
1855 helper->interfaces_count = *valuep + 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001856 break;
1857
1858 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001859 /*
1860 * Strings are indexed from 1 (0 is magic ;) reserved
1861 * for languages list or some such)
1862 */
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02001863 if (*valuep > helper->ffs->strings_count)
1864 helper->ffs->strings_count = *valuep;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001865 break;
1866
1867 case FFS_ENDPOINT:
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02001868 d = (void *)desc;
1869 helper->eps_count++;
1870 if (helper->eps_count >= 15)
1871 return -EINVAL;
1872 /* Check if descriptors for any speed were already parsed */
1873 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
1874 helper->ffs->eps_addrmap[helper->eps_count] =
1875 d->bEndpointAddress;
1876 else if (helper->ffs->eps_addrmap[helper->eps_count] !=
1877 d->bEndpointAddress)
1878 return -EINVAL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001879 break;
1880 }
1881
1882 return 0;
1883}
1884
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02001885static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
1886 struct usb_os_desc_header *desc)
1887{
1888 u16 bcd_version = le16_to_cpu(desc->bcdVersion);
1889 u16 w_index = le16_to_cpu(desc->wIndex);
1890
1891 if (bcd_version != 1) {
1892 pr_vdebug("unsupported os descriptors version: %d",
1893 bcd_version);
1894 return -EINVAL;
1895 }
1896 switch (w_index) {
1897 case 0x4:
1898 *next_type = FFS_OS_DESC_EXT_COMPAT;
1899 break;
1900 case 0x5:
1901 *next_type = FFS_OS_DESC_EXT_PROP;
1902 break;
1903 default:
1904 pr_vdebug("unsupported os descriptor type: %d", w_index);
1905 return -EINVAL;
1906 }
1907
1908 return sizeof(*desc);
1909}
1910
1911/*
1912 * Process all extended compatibility/extended property descriptors
1913 * of a feature descriptor
1914 */
1915static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
1916 enum ffs_os_desc_type type,
1917 u16 feature_count,
1918 ffs_os_desc_callback entity,
1919 void *priv,
1920 struct usb_os_desc_header *h)
1921{
1922 int ret;
1923 const unsigned _len = len;
1924
1925 ENTER();
1926
1927 /* loop over all ext compat/ext prop descriptors */
1928 while (feature_count--) {
1929 ret = entity(type, h, data, len, priv);
1930 if (unlikely(ret < 0)) {
1931 pr_debug("bad OS descriptor, type: %d\n", type);
1932 return ret;
1933 }
1934 data += ret;
1935 len -= ret;
1936 }
1937 return _len - len;
1938}
1939
1940/* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
1941static int __must_check ffs_do_os_descs(unsigned count,
1942 char *data, unsigned len,
1943 ffs_os_desc_callback entity, void *priv)
1944{
1945 const unsigned _len = len;
1946 unsigned long num = 0;
1947
1948 ENTER();
1949
1950 for (num = 0; num < count; ++num) {
1951 int ret;
1952 enum ffs_os_desc_type type;
1953 u16 feature_count;
1954 struct usb_os_desc_header *desc = (void *)data;
1955
1956 if (len < sizeof(*desc))
1957 return -EINVAL;
1958
1959 /*
1960 * Record "descriptor" entity.
1961 * Process dwLength, bcdVersion, wIndex, get b/wCount.
1962 * Move the data pointer to the beginning of extended
1963 * compatibilities proper or extended properties proper
1964 * portions of the data
1965 */
1966 if (le32_to_cpu(desc->dwLength) > len)
1967 return -EINVAL;
1968
1969 ret = __ffs_do_os_desc_header(&type, desc);
1970 if (unlikely(ret < 0)) {
1971 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
1972 num, ret);
1973 return ret;
1974 }
1975 /*
1976 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
1977 */
1978 feature_count = le16_to_cpu(desc->wCount);
1979 if (type == FFS_OS_DESC_EXT_COMPAT &&
1980 (feature_count > 255 || desc->Reserved))
1981 return -EINVAL;
1982 len -= ret;
1983 data += ret;
1984
1985 /*
1986 * Process all function/property descriptors
1987 * of this Feature Descriptor
1988 */
1989 ret = ffs_do_single_os_desc(data, len, type,
1990 feature_count, entity, priv, desc);
1991 if (unlikely(ret < 0)) {
1992 pr_debug("%s returns %d\n", __func__, ret);
1993 return ret;
1994 }
1995
1996 len -= ret;
1997 data += ret;
1998 }
1999 return _len - len;
2000}
2001
2002/**
2003 * Validate contents of the buffer from userspace related to OS descriptors.
2004 */
2005static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2006 struct usb_os_desc_header *h, void *data,
2007 unsigned len, void *priv)
2008{
2009 struct ffs_data *ffs = priv;
2010 u8 length;
2011
2012 ENTER();
2013
2014 switch (type) {
2015 case FFS_OS_DESC_EXT_COMPAT: {
2016 struct usb_ext_compat_desc *d = data;
2017 int i;
2018
2019 if (len < sizeof(*d) ||
2020 d->bFirstInterfaceNumber >= ffs->interfaces_count ||
2021 d->Reserved1)
2022 return -EINVAL;
2023 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2024 if (d->Reserved2[i])
2025 return -EINVAL;
2026
2027 length = sizeof(struct usb_ext_compat_desc);
2028 }
2029 break;
2030 case FFS_OS_DESC_EXT_PROP: {
2031 struct usb_ext_prop_desc *d = data;
2032 u32 type, pdl;
2033 u16 pnl;
2034
2035 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2036 return -EINVAL;
2037 length = le32_to_cpu(d->dwSize);
2038 type = le32_to_cpu(d->dwPropertyDataType);
2039 if (type < USB_EXT_PROP_UNICODE ||
2040 type > USB_EXT_PROP_UNICODE_MULTI) {
2041 pr_vdebug("unsupported os descriptor property type: %d",
2042 type);
2043 return -EINVAL;
2044 }
2045 pnl = le16_to_cpu(d->wPropertyNameLength);
2046 pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
2047 if (length != 14 + pnl + pdl) {
2048 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2049 length, pnl, pdl, type);
2050 return -EINVAL;
2051 }
2052 ++ffs->ms_os_descs_ext_prop_count;
2053 /* property name reported to the host as "WCHAR"s */
2054 ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2055 ffs->ms_os_descs_ext_prop_data_len += pdl;
2056 }
2057 break;
2058 default:
2059 pr_vdebug("unknown descriptor: %d\n", type);
2060 return -EINVAL;
2061 }
2062 return length;
2063}
2064
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002065static int __ffs_data_got_descs(struct ffs_data *ffs,
2066 char *const _data, size_t len)
2067{
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302068 char *data = _data, *raw_descs;
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002069 unsigned os_descs_count = 0, counts[3], flags;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302070 int ret = -EINVAL, i;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002071 struct ffs_desc_helper helper;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002072
2073 ENTER();
2074
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302075 if (get_unaligned_le32(data + 4) != len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002076 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002077
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302078 switch (get_unaligned_le32(data)) {
2079 case FUNCTIONFS_DESCRIPTORS_MAGIC:
2080 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302081 data += 8;
2082 len -= 8;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302083 break;
2084 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2085 flags = get_unaligned_le32(data + 8);
2086 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2087 FUNCTIONFS_HAS_HS_DESC |
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002088 FUNCTIONFS_HAS_SS_DESC |
2089 FUNCTIONFS_HAS_MS_OS_DESC)) {
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302090 ret = -ENOSYS;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002091 goto error;
2092 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302093 data += 12;
2094 len -= 12;
2095 break;
2096 default:
2097 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002098 }
2099
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302100 /* Read fs_count, hs_count and ss_count (if present) */
2101 for (i = 0; i < 3; ++i) {
2102 if (!(flags & (1 << i))) {
2103 counts[i] = 0;
2104 } else if (len < 4) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002105 goto error;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302106 } else {
2107 counts[i] = get_unaligned_le32(data);
2108 data += 4;
2109 len -= 4;
2110 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002111 }
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002112 if (flags & (1 << i)) {
2113 os_descs_count = get_unaligned_le32(data);
2114 data += 4;
2115 len -= 4;
2116 };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002117
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302118 /* Read descriptors */
2119 raw_descs = data;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002120 helper.ffs = ffs;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302121 for (i = 0; i < 3; ++i) {
2122 if (!counts[i])
2123 continue;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002124 helper.interfaces_count = 0;
2125 helper.eps_count = 0;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302126 ret = ffs_do_descs(counts[i], data, len,
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002127 __ffs_data_do_entity, &helper);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302128 if (ret < 0)
2129 goto error;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002130 if (!ffs->eps_count && !ffs->interfaces_count) {
2131 ffs->eps_count = helper.eps_count;
2132 ffs->interfaces_count = helper.interfaces_count;
2133 } else {
2134 if (ffs->eps_count != helper.eps_count) {
2135 ret = -EINVAL;
2136 goto error;
2137 }
2138 if (ffs->interfaces_count != helper.interfaces_count) {
2139 ret = -EINVAL;
2140 goto error;
2141 }
2142 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302143 data += ret;
2144 len -= ret;
2145 }
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002146 if (os_descs_count) {
2147 ret = ffs_do_os_descs(os_descs_count, data, len,
2148 __ffs_data_do_os_desc, ffs);
2149 if (ret < 0)
2150 goto error;
2151 data += ret;
2152 len -= ret;
2153 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002154
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302155 if (raw_descs == data || len) {
2156 ret = -EINVAL;
2157 goto error;
2158 }
2159
2160 ffs->raw_descs_data = _data;
2161 ffs->raw_descs = raw_descs;
2162 ffs->raw_descs_length = data - raw_descs;
2163 ffs->fs_descs_count = counts[0];
2164 ffs->hs_descs_count = counts[1];
2165 ffs->ss_descs_count = counts[2];
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002166 ffs->ms_os_descs_count = os_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002167
2168 return 0;
2169
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002170error:
2171 kfree(_data);
2172 return ret;
2173}
2174
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002175static int __ffs_data_got_strings(struct ffs_data *ffs,
2176 char *const _data, size_t len)
2177{
2178 u32 str_count, needed_count, lang_count;
2179 struct usb_gadget_strings **stringtabs, *t;
2180 struct usb_string *strings, *s;
2181 const char *data = _data;
2182
2183 ENTER();
2184
2185 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2186 get_unaligned_le32(data + 4) != len))
2187 goto error;
2188 str_count = get_unaligned_le32(data + 8);
2189 lang_count = get_unaligned_le32(data + 12);
2190
2191 /* if one is zero the other must be zero */
2192 if (unlikely(!str_count != !lang_count))
2193 goto error;
2194
2195 /* Do we have at least as many strings as descriptors need? */
2196 needed_count = ffs->strings_count;
2197 if (unlikely(str_count < needed_count))
2198 goto error;
2199
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002200 /*
2201 * If we don't need any strings just return and free all
2202 * memory.
2203 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002204 if (!needed_count) {
2205 kfree(_data);
2206 return 0;
2207 }
2208
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002209 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002210 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002211 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002212 vla_group(d);
2213 vla_item(d, struct usb_gadget_strings *, stringtabs,
2214 lang_count + 1);
2215 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2216 vla_item(d, struct usb_string, strings,
2217 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002218
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002219 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2220
2221 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002222 kfree(_data);
2223 return -ENOMEM;
2224 }
2225
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002226 /* Initialize the VLA pointers */
2227 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2228 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002229 i = lang_count;
2230 do {
2231 *stringtabs++ = t++;
2232 } while (--i);
2233 *stringtabs = NULL;
2234
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002235 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2236 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2237 t = vla_ptr(vlabuf, d, stringtab);
2238 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002239 strings = s;
2240 }
2241
2242 /* For each language */
2243 data += 16;
2244 len -= 16;
2245
2246 do { /* lang_count > 0 so we can use do-while */
2247 unsigned needed = needed_count;
2248
2249 if (unlikely(len < 3))
2250 goto error_free;
2251 t->language = get_unaligned_le16(data);
2252 t->strings = s;
2253 ++t;
2254
2255 data += 2;
2256 len -= 2;
2257
2258 /* For each string */
2259 do { /* str_count > 0 so we can use do-while */
2260 size_t length = strnlen(data, len);
2261
2262 if (unlikely(length == len))
2263 goto error_free;
2264
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002265 /*
2266 * User may provide more strings then we need,
2267 * if that's the case we simply ignore the
2268 * rest
2269 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002270 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002271 /*
2272 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002273 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002274 * now just leave garbage here.
2275 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002276 s->s = data;
2277 --needed;
2278 ++s;
2279 }
2280
2281 data += length + 1;
2282 len -= length + 1;
2283 } while (--str_count);
2284
2285 s->id = 0; /* terminator */
2286 s->s = NULL;
2287 ++s;
2288
2289 } while (--lang_count);
2290
2291 /* Some garbage left? */
2292 if (unlikely(len))
2293 goto error_free;
2294
2295 /* Done! */
2296 ffs->stringtabs = stringtabs;
2297 ffs->raw_strings = _data;
2298
2299 return 0;
2300
2301error_free:
2302 kfree(stringtabs);
2303error:
2304 kfree(_data);
2305 return -EINVAL;
2306}
2307
2308
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002309/* Events handling and management *******************************************/
2310
2311static void __ffs_event_add(struct ffs_data *ffs,
2312 enum usb_functionfs_event_type type)
2313{
2314 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2315 int neg = 0;
2316
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002317 /*
2318 * Abort any unhandled setup
2319 *
2320 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002321 * of ffs->setup_state without holding the lock because when
2322 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002323 * the source does nothing.
2324 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002325 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01002326 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002327
2328 switch (type) {
2329 case FUNCTIONFS_RESUME:
2330 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002331 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002332 case FUNCTIONFS_SUSPEND:
2333 case FUNCTIONFS_SETUP:
2334 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002335 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002336 break;
2337
2338 case FUNCTIONFS_BIND:
2339 case FUNCTIONFS_UNBIND:
2340 case FUNCTIONFS_DISABLE:
2341 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002342 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002343 rem_type1 = FUNCTIONFS_SUSPEND;
2344 rem_type2 = FUNCTIONFS_RESUME;
2345 neg = 1;
2346 break;
2347
2348 default:
2349 BUG();
2350 }
2351
2352 {
2353 u8 *ev = ffs->ev.types, *out = ev;
2354 unsigned n = ffs->ev.count;
2355 for (; n; --n, ++ev)
2356 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2357 *out++ = *ev;
2358 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002359 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002360 ffs->ev.count = out - ffs->ev.types;
2361 }
2362
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002363 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002364 ffs->ev.types[ffs->ev.count++] = type;
2365 wake_up_locked(&ffs->ev.waitq);
2366}
2367
2368static void ffs_event_add(struct ffs_data *ffs,
2369 enum usb_functionfs_event_type type)
2370{
2371 unsigned long flags;
2372 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2373 __ffs_event_add(ffs, type);
2374 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2375}
2376
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002377/* Bind/unbind USB function hooks *******************************************/
2378
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002379static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2380{
2381 int i;
2382
2383 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2384 if (ffs->eps_addrmap[i] == endpoint_address)
2385 return i;
2386 return -ENOENT;
2387}
2388
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002389static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2390 struct usb_descriptor_header *desc,
2391 void *priv)
2392{
2393 struct usb_endpoint_descriptor *ds = (void *)desc;
2394 struct ffs_function *func = priv;
2395 struct ffs_ep *ffs_ep;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302396 unsigned ep_desc_id, idx;
2397 static const char *speed_names[] = { "full", "high", "super" };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002398
2399 if (type != FFS_DESCRIPTOR)
2400 return 0;
2401
Manu Gautam8d4e8972014-02-28 16:50:22 +05302402 /*
2403 * If ss_descriptors is not NULL, we are reading super speed
2404 * descriptors; if hs_descriptors is not NULL, we are reading high
2405 * speed descriptors; otherwise, we are reading full speed
2406 * descriptors.
2407 */
2408 if (func->function.ss_descriptors) {
2409 ep_desc_id = 2;
2410 func->function.ss_descriptors[(long)valuep] = desc;
2411 } else if (func->function.hs_descriptors) {
2412 ep_desc_id = 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002413 func->function.hs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302414 } else {
2415 ep_desc_id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002416 func->function.fs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302417 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002418
2419 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2420 return 0;
2421
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002422 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2423 if (idx < 0)
2424 return idx;
2425
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002426 ffs_ep = func->eps + idx;
2427
Manu Gautam8d4e8972014-02-28 16:50:22 +05302428 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2429 pr_err("two %sspeed descriptors for EP %d\n",
2430 speed_names[ep_desc_id],
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002431 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002432 return -EINVAL;
2433 }
Manu Gautam8d4e8972014-02-28 16:50:22 +05302434 ffs_ep->descs[ep_desc_id] = ds;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002435
2436 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2437 if (ffs_ep->ep) {
2438 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2439 if (!ds->wMaxPacketSize)
2440 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2441 } else {
2442 struct usb_request *req;
2443 struct usb_ep *ep;
2444
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002445 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002446 ep = usb_ep_autoconfig(func->gadget, ds);
2447 if (unlikely(!ep))
2448 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08002449 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002450
2451 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2452 if (unlikely(!req))
2453 return -ENOMEM;
2454
2455 ffs_ep->ep = ep;
2456 ffs_ep->req = req;
2457 func->eps_revmap[ds->bEndpointAddress &
2458 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2459 }
2460 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2461
2462 return 0;
2463}
2464
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002465static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2466 struct usb_descriptor_header *desc,
2467 void *priv)
2468{
2469 struct ffs_function *func = priv;
2470 unsigned idx;
2471 u8 newValue;
2472
2473 switch (type) {
2474 default:
2475 case FFS_DESCRIPTOR:
2476 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2477 return 0;
2478
2479 case FFS_INTERFACE:
2480 idx = *valuep;
2481 if (func->interfaces_nums[idx] < 0) {
2482 int id = usb_interface_id(func->conf, &func->function);
2483 if (unlikely(id < 0))
2484 return id;
2485 func->interfaces_nums[idx] = id;
2486 }
2487 newValue = func->interfaces_nums[idx];
2488 break;
2489
2490 case FFS_STRING:
2491 /* String' IDs are allocated when fsf_data is bound to cdev */
2492 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2493 break;
2494
2495 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002496 /*
2497 * USB_DT_ENDPOINT are handled in
2498 * __ffs_func_bind_do_descs().
2499 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002500 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2501 return 0;
2502
2503 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2504 if (unlikely(!func->eps[idx].ep))
2505 return -EINVAL;
2506
2507 {
2508 struct usb_endpoint_descriptor **descs;
2509 descs = func->eps[idx].descs;
2510 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2511 }
2512 break;
2513 }
2514
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002515 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002516 *valuep = newValue;
2517 return 0;
2518}
2519
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002520static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2521 struct usb_os_desc_header *h, void *data,
2522 unsigned len, void *priv)
2523{
2524 struct ffs_function *func = priv;
2525 u8 length = 0;
2526
2527 switch (type) {
2528 case FFS_OS_DESC_EXT_COMPAT: {
2529 struct usb_ext_compat_desc *desc = data;
2530 struct usb_os_desc_table *t;
2531
2532 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
2533 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
2534 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
2535 ARRAY_SIZE(desc->CompatibleID) +
2536 ARRAY_SIZE(desc->SubCompatibleID));
2537 length = sizeof(*desc);
2538 }
2539 break;
2540 case FFS_OS_DESC_EXT_PROP: {
2541 struct usb_ext_prop_desc *desc = data;
2542 struct usb_os_desc_table *t;
2543 struct usb_os_desc_ext_prop *ext_prop;
2544 char *ext_prop_name;
2545 char *ext_prop_data;
2546
2547 t = &func->function.os_desc_table[h->interface];
2548 t->if_id = func->interfaces_nums[h->interface];
2549
2550 ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
2551 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
2552
2553 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
2554 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2555 ext_prop->data_len = le32_to_cpu(*(u32 *)
2556 usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
2557 length = ext_prop->name_len + ext_prop->data_len + 14;
2558
2559 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
2560 func->ffs->ms_os_descs_ext_prop_name_avail +=
2561 ext_prop->name_len;
2562
2563 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
2564 func->ffs->ms_os_descs_ext_prop_data_avail +=
2565 ext_prop->data_len;
2566 memcpy(ext_prop_data,
2567 usb_ext_prop_data_ptr(data, ext_prop->name_len),
2568 ext_prop->data_len);
2569 /* unicode data reported to the host as "WCHAR"s */
2570 switch (ext_prop->type) {
2571 case USB_EXT_PROP_UNICODE:
2572 case USB_EXT_PROP_UNICODE_ENV:
2573 case USB_EXT_PROP_UNICODE_LINK:
2574 case USB_EXT_PROP_UNICODE_MULTI:
2575 ext_prop->data_len *= 2;
2576 break;
2577 }
2578 ext_prop->data = ext_prop_data;
2579
2580 memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
2581 ext_prop->name_len);
2582 /* property name reported to the host as "WCHAR"s */
2583 ext_prop->name_len *= 2;
2584 ext_prop->name = ext_prop_name;
2585
2586 t->os_desc->ext_prop_len +=
2587 ext_prop->name_len + ext_prop->data_len + 14;
2588 ++t->os_desc->ext_prop_count;
2589 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
2590 }
2591 break;
2592 default:
2593 pr_vdebug("unknown descriptor: %d\n", type);
2594 }
2595
2596 return length;
2597}
2598
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002599static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2600 struct usb_configuration *c)
2601{
2602 struct ffs_function *func = ffs_func_from_usb(f);
2603 struct f_fs_opts *ffs_opts =
2604 container_of(f->fi, struct f_fs_opts, func_inst);
2605 int ret;
2606
2607 ENTER();
2608
2609 /*
2610 * Legacy gadget triggers binding in functionfs_ready_callback,
2611 * which already uses locking; taking the same lock here would
2612 * cause a deadlock.
2613 *
2614 * Configfs-enabled gadgets however do need ffs_dev_lock.
2615 */
2616 if (!ffs_opts->no_configfs)
2617 ffs_dev_lock();
2618 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2619 func->ffs = ffs_opts->dev->ffs_data;
2620 if (!ffs_opts->no_configfs)
2621 ffs_dev_unlock();
2622 if (ret)
2623 return ERR_PTR(ret);
2624
2625 func->conf = c;
2626 func->gadget = c->cdev->gadget;
2627
2628 ffs_data_get(func->ffs);
2629
2630 /*
2631 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2632 * configurations are bound in sequence with list_for_each_entry,
2633 * in each configuration its functions are bound in sequence
2634 * with list_for_each_entry, so we assume no race condition
2635 * with regard to ffs_opts->bound access
2636 */
2637 if (!ffs_opts->refcnt) {
2638 ret = functionfs_bind(func->ffs, c->cdev);
2639 if (ret)
2640 return ERR_PTR(ret);
2641 }
2642 ffs_opts->refcnt++;
2643 func->function.strings = func->ffs->stringtabs;
2644
2645 return ffs_opts;
2646}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002647
2648static int _ffs_func_bind(struct usb_configuration *c,
2649 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002650{
2651 struct ffs_function *func = ffs_func_from_usb(f);
2652 struct ffs_data *ffs = func->ffs;
2653
2654 const int full = !!func->ffs->fs_descs_count;
2655 const int high = gadget_is_dualspeed(func->gadget) &&
2656 func->ffs->hs_descs_count;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302657 const int super = gadget_is_superspeed(func->gadget) &&
2658 func->ffs->ss_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002659
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002660 int fs_len, hs_len, ss_len, ret, i;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002661
2662 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002663 vla_group(d);
2664 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2665 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2666 full ? ffs->fs_descs_count + 1 : 0);
2667 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2668 high ? ffs->hs_descs_count + 1 : 0);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302669 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2670 super ? ffs->ss_descs_count + 1 : 0);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002671 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002672 vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
2673 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2674 vla_item_with_sz(d, char[16], ext_compat,
2675 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2676 vla_item_with_sz(d, struct usb_os_desc, os_desc,
2677 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2678 vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
2679 ffs->ms_os_descs_ext_prop_count);
2680 vla_item_with_sz(d, char, ext_prop_name,
2681 ffs->ms_os_descs_ext_prop_name_len);
2682 vla_item_with_sz(d, char, ext_prop_data,
2683 ffs->ms_os_descs_ext_prop_data_len);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302684 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002685 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002686
2687 ENTER();
2688
Manu Gautam8d4e8972014-02-28 16:50:22 +05302689 /* Has descriptors only for speeds gadget does not support */
2690 if (unlikely(!(full | high | super)))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002691 return -ENOTSUPP;
2692
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002693 /* Allocate a single chunk, less management later on */
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002694 vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002695 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002696 return -ENOMEM;
2697
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002698 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
2699 ffs->ms_os_descs_ext_prop_name_avail =
2700 vla_ptr(vlabuf, d, ext_prop_name);
2701 ffs->ms_os_descs_ext_prop_data_avail =
2702 vla_ptr(vlabuf, d, ext_prop_data);
2703
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302704 /* Copy descriptors */
2705 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2706 ffs->raw_descs_length);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302707
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002708 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2709 for (ret = ffs->eps_count; ret; --ret) {
2710 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002711
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002712 ptr = vla_ptr(vlabuf, d, eps);
2713 ptr[ret].num = -1;
2714 }
2715
2716 /* Save pointers
2717 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2718 */
2719 func->eps = vla_ptr(vlabuf, d, eps);
2720 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002721
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002722 /*
2723 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002724 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002725 * numbers without worrying that it may be described later on.
2726 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002727 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002728 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302729 fs_len = ffs_do_descs(ffs->fs_descs_count,
2730 vla_ptr(vlabuf, d, raw_descs),
2731 d_raw_descs__sz,
2732 __ffs_func_bind_do_descs, func);
2733 if (unlikely(fs_len < 0)) {
2734 ret = fs_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002735 goto error;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302736 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002737 } else {
Manu Gautam8d4e8972014-02-28 16:50:22 +05302738 fs_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002739 }
2740
2741 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002742 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302743 hs_len = ffs_do_descs(ffs->hs_descs_count,
2744 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2745 d_raw_descs__sz - fs_len,
2746 __ffs_func_bind_do_descs, func);
2747 if (unlikely(hs_len < 0)) {
2748 ret = hs_len;
2749 goto error;
2750 }
2751 } else {
2752 hs_len = 0;
2753 }
2754
2755 if (likely(super)) {
2756 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002757 ss_len = ffs_do_descs(ffs->ss_descs_count,
Manu Gautam8d4e8972014-02-28 16:50:22 +05302758 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2759 d_raw_descs__sz - fs_len - hs_len,
2760 __ffs_func_bind_do_descs, func);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002761 if (unlikely(ss_len < 0)) {
2762 ret = ss_len;
Robert Baldyga88548942013-09-27 12:28:54 +02002763 goto error;
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002764 }
2765 } else {
2766 ss_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002767 }
2768
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002769 /*
2770 * Now handle interface numbers allocation and interface and
2771 * endpoint numbers rewriting. We can do that in one go
2772 * now.
2773 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002774 ret = ffs_do_descs(ffs->fs_descs_count +
Manu Gautam8d4e8972014-02-28 16:50:22 +05302775 (high ? ffs->hs_descs_count : 0) +
2776 (super ? ffs->ss_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002777 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002778 __ffs_func_bind_do_nums, func);
2779 if (unlikely(ret < 0))
2780 goto error;
2781
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002782 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
2783 if (c->cdev->use_os_string)
2784 for (i = 0; i < ffs->interfaces_count; ++i) {
2785 struct usb_os_desc *desc;
2786
2787 desc = func->function.os_desc_table[i].os_desc =
2788 vla_ptr(vlabuf, d, os_desc) +
2789 i * sizeof(struct usb_os_desc);
2790 desc->ext_compat_id =
2791 vla_ptr(vlabuf, d, ext_compat) + i * 16;
2792 INIT_LIST_HEAD(&desc->ext_prop);
2793 }
2794 ret = ffs_do_os_descs(ffs->ms_os_descs_count,
2795 vla_ptr(vlabuf, d, raw_descs) +
2796 fs_len + hs_len + ss_len,
2797 d_raw_descs__sz - fs_len - hs_len - ss_len,
2798 __ffs_func_bind_do_os_desc, func);
2799 if (unlikely(ret < 0))
2800 goto error;
2801 func->function.os_desc_n =
2802 c->cdev->use_os_string ? ffs->interfaces_count : 0;
2803
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002804 /* And we're done */
2805 ffs_event_add(ffs, FUNCTIONFS_BIND);
2806 return 0;
2807
2808error:
2809 /* XXX Do we need to release all claimed endpoints here? */
2810 return ret;
2811}
2812
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002813static int ffs_func_bind(struct usb_configuration *c,
2814 struct usb_function *f)
2815{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002816 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2817
2818 if (IS_ERR(ffs_opts))
2819 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002820
2821 return _ffs_func_bind(c, f);
2822}
2823
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002824
2825/* Other USB function hooks *************************************************/
2826
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002827static int ffs_func_set_alt(struct usb_function *f,
2828 unsigned interface, unsigned alt)
2829{
2830 struct ffs_function *func = ffs_func_from_usb(f);
2831 struct ffs_data *ffs = func->ffs;
2832 int ret = 0, intf;
2833
2834 if (alt != (unsigned)-1) {
2835 intf = ffs_func_revmap_intf(func, interface);
2836 if (unlikely(intf < 0))
2837 return intf;
2838 }
2839
2840 if (ffs->func)
2841 ffs_func_eps_disable(ffs->func);
2842
2843 if (ffs->state != FFS_ACTIVE)
2844 return -ENODEV;
2845
2846 if (alt == (unsigned)-1) {
2847 ffs->func = NULL;
2848 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2849 return 0;
2850 }
2851
2852 ffs->func = func;
2853 ret = ffs_func_eps_enable(func);
2854 if (likely(ret >= 0))
2855 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2856 return ret;
2857}
2858
2859static void ffs_func_disable(struct usb_function *f)
2860{
2861 ffs_func_set_alt(f, 0, (unsigned)-1);
2862}
2863
2864static int ffs_func_setup(struct usb_function *f,
2865 const struct usb_ctrlrequest *creq)
2866{
2867 struct ffs_function *func = ffs_func_from_usb(f);
2868 struct ffs_data *ffs = func->ffs;
2869 unsigned long flags;
2870 int ret;
2871
2872 ENTER();
2873
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002874 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2875 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2876 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2877 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2878 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002879
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002880 /*
2881 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002882 * (notable exceptions are set/get interface) so we need to
2883 * handle them. All other either handled by composite or
2884 * passed to usb_configuration->setup() (if one is set). No
2885 * matter, we will handle requests directed to endpoint here
2886 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002887 * other request?
2888 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002889 if (ffs->state != FFS_ACTIVE)
2890 return -ENODEV;
2891
2892 switch (creq->bRequestType & USB_RECIP_MASK) {
2893 case USB_RECIP_INTERFACE:
2894 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2895 if (unlikely(ret < 0))
2896 return ret;
2897 break;
2898
2899 case USB_RECIP_ENDPOINT:
2900 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2901 if (unlikely(ret < 0))
2902 return ret;
2903 break;
2904
2905 default:
2906 return -EOPNOTSUPP;
2907 }
2908
2909 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2910 ffs->ev.setup = *creq;
2911 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2912 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2913 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2914
2915 return 0;
2916}
2917
2918static void ffs_func_suspend(struct usb_function *f)
2919{
2920 ENTER();
2921 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2922}
2923
2924static void ffs_func_resume(struct usb_function *f)
2925{
2926 ENTER();
2927 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2928}
2929
2930
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002931/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002932
2933static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2934{
2935 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2936 return num ? num : -EDOM;
2937}
2938
2939static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2940{
2941 short *nums = func->interfaces_nums;
2942 unsigned count = func->ffs->interfaces_count;
2943
2944 for (; count; --count, ++nums) {
2945 if (*nums >= 0 && *nums == intf)
2946 return nums - func->interfaces_nums;
2947 }
2948
2949 return -EDOM;
2950}
2951
2952
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002953/* Devices management *******************************************************/
2954
2955static LIST_HEAD(ffs_devices);
2956
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002957static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002958{
2959 struct ffs_dev *dev;
2960
2961 list_for_each_entry(dev, &ffs_devices, entry) {
2962 if (!dev->name || !name)
2963 continue;
2964 if (strcmp(dev->name, name) == 0)
2965 return dev;
2966 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002967
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002968 return NULL;
2969}
2970
2971/*
2972 * ffs_lock must be taken by the caller of this function
2973 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002974static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002975{
2976 struct ffs_dev *dev;
2977
2978 if (list_is_singular(&ffs_devices)) {
2979 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2980 if (dev->single)
2981 return dev;
2982 }
2983
2984 return NULL;
2985}
2986
2987/*
2988 * ffs_lock must be taken by the caller of this function
2989 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002990static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002991{
2992 struct ffs_dev *dev;
2993
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002994 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002995 if (dev)
2996 return dev;
2997
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002998 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002999}
3000
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003001/* Configfs support *********************************************************/
3002
3003static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3004{
3005 return container_of(to_config_group(item), struct f_fs_opts,
3006 func_inst.group);
3007}
3008
3009static void ffs_attr_release(struct config_item *item)
3010{
3011 struct f_fs_opts *opts = to_ffs_opts(item);
3012
3013 usb_put_function_instance(&opts->func_inst);
3014}
3015
3016static struct configfs_item_operations ffs_item_ops = {
3017 .release = ffs_attr_release,
3018};
3019
3020static struct config_item_type ffs_func_type = {
3021 .ct_item_ops = &ffs_item_ops,
3022 .ct_owner = THIS_MODULE,
3023};
3024
3025
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003026/* Function registration interface ******************************************/
3027
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003028static void ffs_free_inst(struct usb_function_instance *f)
3029{
3030 struct f_fs_opts *opts;
3031
3032 opts = to_f_fs_opts(f);
3033 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003034 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003035 ffs_dev_unlock();
3036 kfree(opts);
3037}
3038
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003039#define MAX_INST_NAME_LEN 40
3040
3041static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3042{
3043 struct f_fs_opts *opts;
3044 char *ptr;
3045 const char *tmp;
3046 int name_len, ret;
3047
3048 name_len = strlen(name) + 1;
3049 if (name_len > MAX_INST_NAME_LEN)
3050 return -ENAMETOOLONG;
3051
3052 ptr = kstrndup(name, name_len, GFP_KERNEL);
3053 if (!ptr)
3054 return -ENOMEM;
3055
3056 opts = to_f_fs_opts(fi);
3057 tmp = NULL;
3058
3059 ffs_dev_lock();
3060
3061 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
3062 ret = _ffs_name_dev(opts->dev, ptr);
3063 if (ret) {
3064 kfree(ptr);
3065 ffs_dev_unlock();
3066 return ret;
3067 }
3068 opts->dev->name_allocated = true;
3069
3070 ffs_dev_unlock();
3071
3072 kfree(tmp);
3073
3074 return 0;
3075}
3076
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003077static struct usb_function_instance *ffs_alloc_inst(void)
3078{
3079 struct f_fs_opts *opts;
3080 struct ffs_dev *dev;
3081
3082 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3083 if (!opts)
3084 return ERR_PTR(-ENOMEM);
3085
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003086 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003087 opts->func_inst.free_func_inst = ffs_free_inst;
3088 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003089 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003090 ffs_dev_unlock();
3091 if (IS_ERR(dev)) {
3092 kfree(opts);
3093 return ERR_CAST(dev);
3094 }
3095 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003096 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003097
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003098 config_group_init_type_name(&opts->func_inst.group, "",
3099 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003100 return &opts->func_inst;
3101}
3102
3103static void ffs_free(struct usb_function *f)
3104{
3105 kfree(ffs_func_from_usb(f));
3106}
3107
3108static void ffs_func_unbind(struct usb_configuration *c,
3109 struct usb_function *f)
3110{
3111 struct ffs_function *func = ffs_func_from_usb(f);
3112 struct ffs_data *ffs = func->ffs;
3113 struct f_fs_opts *opts =
3114 container_of(f->fi, struct f_fs_opts, func_inst);
3115 struct ffs_ep *ep = func->eps;
3116 unsigned count = ffs->eps_count;
3117 unsigned long flags;
3118
3119 ENTER();
3120 if (ffs->func == func) {
3121 ffs_func_eps_disable(func);
3122 ffs->func = NULL;
3123 }
3124
3125 if (!--opts->refcnt)
3126 functionfs_unbind(ffs);
3127
3128 /* cleanup after autoconfig */
3129 spin_lock_irqsave(&func->ffs->eps_lock, flags);
3130 do {
3131 if (ep->ep && ep->req)
3132 usb_ep_free_request(ep->ep, ep->req);
3133 ep->req = NULL;
3134 ++ep;
3135 } while (--count);
3136 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3137 kfree(func->eps);
3138 func->eps = NULL;
3139 /*
3140 * eps, descriptors and interfaces_nums are allocated in the
3141 * same chunk so only one free is required.
3142 */
3143 func->function.fs_descriptors = NULL;
3144 func->function.hs_descriptors = NULL;
Manu Gautam8d4e8972014-02-28 16:50:22 +05303145 func->function.ss_descriptors = NULL;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003146 func->interfaces_nums = NULL;
3147
3148 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3149}
3150
3151static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3152{
3153 struct ffs_function *func;
3154
3155 ENTER();
3156
3157 func = kzalloc(sizeof(*func), GFP_KERNEL);
3158 if (unlikely(!func))
3159 return ERR_PTR(-ENOMEM);
3160
3161 func->function.name = "Function FS Gadget";
3162
3163 func->function.bind = ffs_func_bind;
3164 func->function.unbind = ffs_func_unbind;
3165 func->function.set_alt = ffs_func_set_alt;
3166 func->function.disable = ffs_func_disable;
3167 func->function.setup = ffs_func_setup;
3168 func->function.suspend = ffs_func_suspend;
3169 func->function.resume = ffs_func_resume;
3170 func->function.free_func = ffs_free;
3171
3172 return &func->function;
3173}
3174
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003175/*
3176 * ffs_lock must be taken by the caller of this function
3177 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003178static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003179{
3180 struct ffs_dev *dev;
3181 int ret;
3182
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003183 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003184 return ERR_PTR(-EBUSY);
3185
3186 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3187 if (!dev)
3188 return ERR_PTR(-ENOMEM);
3189
3190 if (list_empty(&ffs_devices)) {
3191 ret = functionfs_init();
3192 if (ret) {
3193 kfree(dev);
3194 return ERR_PTR(ret);
3195 }
3196 }
3197
3198 list_add(&dev->entry, &ffs_devices);
3199
3200 return dev;
3201}
3202
3203/*
3204 * ffs_lock must be taken by the caller of this function
3205 * The caller is responsible for "name" being available whenever f_fs needs it
3206 */
3207static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
3208{
3209 struct ffs_dev *existing;
3210
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003211 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003212 if (existing)
3213 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01003214
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003215 dev->name = name;
3216
3217 return 0;
3218}
3219
3220/*
3221 * The caller is responsible for "name" being available whenever f_fs needs it
3222 */
3223int ffs_name_dev(struct ffs_dev *dev, const char *name)
3224{
3225 int ret;
3226
3227 ffs_dev_lock();
3228 ret = _ffs_name_dev(dev, name);
3229 ffs_dev_unlock();
3230
3231 return ret;
3232}
Felipe Balbi0700faa2014-04-01 13:19:32 -05003233EXPORT_SYMBOL_GPL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003234
3235int ffs_single_dev(struct ffs_dev *dev)
3236{
3237 int ret;
3238
3239 ret = 0;
3240 ffs_dev_lock();
3241
3242 if (!list_is_singular(&ffs_devices))
3243 ret = -EBUSY;
3244 else
3245 dev->single = true;
3246
3247 ffs_dev_unlock();
3248 return ret;
3249}
Felipe Balbi0700faa2014-04-01 13:19:32 -05003250EXPORT_SYMBOL_GPL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003251
3252/*
3253 * ffs_lock must be taken by the caller of this function
3254 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003255static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003256{
3257 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003258 if (dev->name_allocated)
3259 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003260 kfree(dev);
3261 if (list_empty(&ffs_devices))
3262 functionfs_cleanup();
3263}
3264
3265static void *ffs_acquire_dev(const char *dev_name)
3266{
3267 struct ffs_dev *ffs_dev;
3268
3269 ENTER();
3270 ffs_dev_lock();
3271
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003272 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003273 if (!ffs_dev)
Krzysztof Opasiakd668b4f2014-05-21 14:05:35 +02003274 ffs_dev = ERR_PTR(-ENOENT);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003275 else if (ffs_dev->mounted)
3276 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003277 else if (ffs_dev->ffs_acquire_dev_callback &&
3278 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
Krzysztof Opasiakd668b4f2014-05-21 14:05:35 +02003279 ffs_dev = ERR_PTR(-ENOENT);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003280 else
3281 ffs_dev->mounted = true;
3282
3283 ffs_dev_unlock();
3284 return ffs_dev;
3285}
3286
3287static void ffs_release_dev(struct ffs_data *ffs_data)
3288{
3289 struct ffs_dev *ffs_dev;
3290
3291 ENTER();
3292 ffs_dev_lock();
3293
3294 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01003295 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003296 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01003297
3298 if (ffs_dev->ffs_release_dev_callback)
3299 ffs_dev->ffs_release_dev_callback(ffs_dev);
3300 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003301
3302 ffs_dev_unlock();
3303}
3304
3305static int ffs_ready(struct ffs_data *ffs)
3306{
3307 struct ffs_dev *ffs_obj;
3308 int ret = 0;
3309
3310 ENTER();
3311 ffs_dev_lock();
3312
3313 ffs_obj = ffs->private_data;
3314 if (!ffs_obj) {
3315 ret = -EINVAL;
3316 goto done;
3317 }
3318 if (WARN_ON(ffs_obj->desc_ready)) {
3319 ret = -EBUSY;
3320 goto done;
3321 }
3322
3323 ffs_obj->desc_ready = true;
3324 ffs_obj->ffs_data = ffs;
3325
3326 if (ffs_obj->ffs_ready_callback)
3327 ret = ffs_obj->ffs_ready_callback(ffs);
3328
3329done:
3330 ffs_dev_unlock();
3331 return ret;
3332}
3333
3334static void ffs_closed(struct ffs_data *ffs)
3335{
3336 struct ffs_dev *ffs_obj;
3337
3338 ENTER();
3339 ffs_dev_lock();
3340
3341 ffs_obj = ffs->private_data;
3342 if (!ffs_obj)
3343 goto done;
3344
3345 ffs_obj->desc_ready = false;
3346
3347 if (ffs_obj->ffs_closed_callback)
3348 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003349
3350 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
3351 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
3352 goto done;
3353
3354 unregister_gadget_item(ffs_obj->opts->
3355 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003356done:
3357 ffs_dev_unlock();
3358}
3359
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003360/* Misc helper functions ****************************************************/
3361
3362static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3363{
3364 return nonblock
3365 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3366 : mutex_lock_interruptible(mutex);
3367}
3368
Al Viro260ef312012-09-26 21:43:45 -04003369static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003370{
3371 char *data;
3372
3373 if (unlikely(!len))
3374 return NULL;
3375
3376 data = kmalloc(len, GFP_KERNEL);
3377 if (unlikely(!data))
3378 return ERR_PTR(-ENOMEM);
3379
3380 if (unlikely(__copy_from_user(data, buf, len))) {
3381 kfree(data);
3382 return ERR_PTR(-EFAULT);
3383 }
3384
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01003385 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003386 ffs_dump_mem("", data, len);
3387
3388 return data;
3389}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003390
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003391DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3392MODULE_LICENSE("GPL");
3393MODULE_AUTHOR("Michal Nazarewicz");