blob: 5c8429f23a892782febdb3c650efa8c168705227 [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>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080026#include <linux/uio.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020027#include <asm/unaligned.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020028
29#include <linux/usb/composite.h>
30#include <linux/usb/functionfs.h>
31
Robert Baldyga2e4c7552014-02-10 10:42:44 +010032#include <linux/aio.h>
33#include <linux/mmu_context.h>
Robert Baldyga23de91e2014-02-10 10:42:43 +010034#include <linux/poll.h>
Robert Baldyga5e33f6f2015-01-23 13:41:01 +010035#include <linux/eventfd.h>
Robert Baldyga23de91e2014-02-10 10:42:43 +010036
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010037#include "u_fs.h"
Andrzej Pietrasiewicz74d48462014-05-08 14:06:21 +020038#include "u_f.h"
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +020039#include "u_os_desc.h"
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010040#include "configfs.h"
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020041
42#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
43
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020044/* Reference counter handling */
45static void ffs_data_get(struct ffs_data *ffs);
46static void ffs_data_put(struct ffs_data *ffs);
47/* Creates new ffs_data object. */
48static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
49
50/* Opened counter handling. */
51static void ffs_data_opened(struct ffs_data *ffs);
52static void ffs_data_closed(struct ffs_data *ffs);
53
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010054/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020055static int __must_check
56__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
57static int __must_check
58__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
59
60
61/* The function structure ***************************************************/
62
63struct ffs_ep;
64
65struct ffs_function {
66 struct usb_configuration *conf;
67 struct usb_gadget *gadget;
68 struct ffs_data *ffs;
69
70 struct ffs_ep *eps;
71 u8 eps_revmap[16];
72 short *interfaces_nums;
73
74 struct usb_function function;
75};
76
77
78static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
79{
80 return container_of(f, struct ffs_function, function);
81}
82
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020083
Michal Nazarewicza7ecf052014-02-10 10:42:41 +010084static inline enum ffs_setup_state
85ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
86{
87 return (enum ffs_setup_state)
88 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
89}
90
91
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020092static void ffs_func_eps_disable(struct ffs_function *func);
93static int __must_check ffs_func_eps_enable(struct ffs_function *func);
94
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020095static int ffs_func_bind(struct usb_configuration *,
96 struct usb_function *);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020097static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
98static void ffs_func_disable(struct usb_function *);
99static int ffs_func_setup(struct usb_function *,
100 const struct usb_ctrlrequest *);
101static void ffs_func_suspend(struct usb_function *);
102static void ffs_func_resume(struct usb_function *);
103
104
105static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
106static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
107
108
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200109/* The endpoints structures *************************************************/
110
111struct ffs_ep {
112 struct usb_ep *ep; /* P: ffs->eps_lock */
113 struct usb_request *req; /* P: epfile->mutex */
114
Manu Gautam8d4e8972014-02-28 16:50:22 +0530115 /* [0]: full speed, [1]: high speed, [2]: super speed */
116 struct usb_endpoint_descriptor *descs[3];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200117
118 u8 num;
119
120 int status; /* P: epfile->mutex */
121};
122
123struct ffs_epfile {
124 /* Protects ep->ep and ep->req. */
125 struct mutex mutex;
126 wait_queue_head_t wait;
127
128 struct ffs_data *ffs;
129 struct ffs_ep *ep; /* P: ffs->eps_lock */
130
131 struct dentry *dentry;
132
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200133 /*
134 * Buffer for holding data from partial reads which may happen since
135 * we’re rounding user read requests to a multiple of a max packet size.
136 */
137 struct ffs_buffer *read_buffer; /* P: epfile->mutex */
138
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200139 char name[5];
140
141 unsigned char in; /* P: ffs->eps_lock */
142 unsigned char isoc; /* P: ffs->eps_lock */
143
144 unsigned char _pad;
145};
146
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200147struct ffs_buffer {
148 size_t length;
149 char *data;
150 char storage[];
151};
152
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100153/* ffs_io_data structure ***************************************************/
154
155struct ffs_io_data {
156 bool aio;
157 bool read;
158
159 struct kiocb *kiocb;
Al Viroc993c392015-01-31 23:23:35 -0500160 struct iov_iter data;
161 const void *to_free;
162 char *buf;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100163
164 struct mm_struct *mm;
165 struct work_struct work;
166
167 struct usb_ep *ep;
168 struct usb_request *req;
Robert Baldyga5e33f6f2015-01-23 13:41:01 +0100169
170 struct ffs_data *ffs;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100171};
172
Robert Baldyga6d5c1c72014-08-25 11:16:27 +0200173struct ffs_desc_helper {
174 struct ffs_data *ffs;
175 unsigned interfaces_count;
176 unsigned eps_count;
177};
178
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200179static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
180static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
181
Al Viro1bb27ca2014-09-03 13:32:19 -0400182static struct dentry *
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200183ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
Al Viro1bb27ca2014-09-03 13:32:19 -0400184 const struct file_operations *fops);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200185
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100186/* Devices management *******************************************************/
187
188DEFINE_MUTEX(ffs_lock);
Felipe Balbi0700faa2014-04-01 13:19:32 -0500189EXPORT_SYMBOL_GPL(ffs_lock);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100190
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100191static struct ffs_dev *_ffs_find_dev(const char *name);
192static struct ffs_dev *_ffs_alloc_dev(void);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +0100193static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100194static void _ffs_free_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100195static void *ffs_acquire_dev(const char *dev_name);
196static void ffs_release_dev(struct ffs_data *ffs_data);
197static int ffs_ready(struct ffs_data *ffs);
198static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200199
200/* Misc helper functions ****************************************************/
201
202static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
203 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400204static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200205 __attribute__((warn_unused_result, nonnull));
206
207
208/* Control file aka ep0 *****************************************************/
209
210static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
211{
212 struct ffs_data *ffs = req->context;
213
214 complete_all(&ffs->ep0req_completion);
215}
216
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200217static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
218{
219 struct usb_request *req = ffs->ep0req;
220 int ret;
221
222 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
223
224 spin_unlock_irq(&ffs->ev.waitq.lock);
225
226 req->buf = data;
227 req->length = len;
228
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100229 /*
230 * UDC layer requires to provide a buffer even for ZLP, but should
231 * not use it at all. Let's provide some poisoned pointer to catch
232 * possible bug in the driver.
233 */
234 if (req->buf == NULL)
235 req->buf = (void *)0xDEADBABE;
236
Wolfram Sang16735d02013-11-14 14:32:02 -0800237 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200238
239 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
240 if (unlikely(ret < 0))
241 return ret;
242
243 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
244 if (unlikely(ret)) {
245 usb_ep_dequeue(ffs->gadget->ep0, req);
246 return -EINTR;
247 }
248
249 ffs->setup_state = FFS_NO_SETUP;
Robert Baldyga0a7b1f82014-02-10 10:42:42 +0100250 return req->status ? req->status : req->actual;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200251}
252
253static int __ffs_ep0_stall(struct ffs_data *ffs)
254{
255 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100256 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200257 usb_ep_set_halt(ffs->gadget->ep0);
258 ffs->setup_state = FFS_NO_SETUP;
259 return -EL2HLT;
260 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100261 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200262 return -ESRCH;
263 }
264}
265
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200266static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
267 size_t len, loff_t *ptr)
268{
269 struct ffs_data *ffs = file->private_data;
270 ssize_t ret;
271 char *data;
272
273 ENTER();
274
275 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100276 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200277 return -EIDRM;
278
279 /* Acquire mutex */
280 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
281 if (unlikely(ret < 0))
282 return ret;
283
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200284 /* Check state */
285 switch (ffs->state) {
286 case FFS_READ_DESCRIPTORS:
287 case FFS_READ_STRINGS:
288 /* Copy data */
289 if (unlikely(len < 16)) {
290 ret = -EINVAL;
291 break;
292 }
293
294 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100295 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200296 ret = PTR_ERR(data);
297 break;
298 }
299
300 /* Handle data */
301 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100302 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200303 ret = __ffs_data_got_descs(ffs, data, len);
304 if (unlikely(ret < 0))
305 break;
306
307 ffs->state = FFS_READ_STRINGS;
308 ret = len;
309 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100310 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200311 ret = __ffs_data_got_strings(ffs, data, len);
312 if (unlikely(ret < 0))
313 break;
314
315 ret = ffs_epfiles_create(ffs);
316 if (unlikely(ret)) {
317 ffs->state = FFS_CLOSING;
318 break;
319 }
320
321 ffs->state = FFS_ACTIVE;
322 mutex_unlock(&ffs->mutex);
323
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100324 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200325 if (unlikely(ret < 0)) {
326 ffs->state = FFS_CLOSING;
327 return ret;
328 }
329
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200330 return len;
331 }
332 break;
333
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200334 case FFS_ACTIVE:
335 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100336 /*
337 * We're called from user space, we can use _irq
338 * rather then _irqsave
339 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200340 spin_lock_irq(&ffs->ev.waitq.lock);
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100341 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100342 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200343 ret = -EIDRM;
344 goto done_spin;
345
346 case FFS_NO_SETUP:
347 ret = -ESRCH;
348 goto done_spin;
349
350 case FFS_SETUP_PENDING:
351 break;
352 }
353
354 /* FFS_SETUP_PENDING */
355 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
356 spin_unlock_irq(&ffs->ev.waitq.lock);
357 ret = __ffs_ep0_stall(ffs);
358 break;
359 }
360
361 /* FFS_SETUP_PENDING and not stall */
362 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
363
364 spin_unlock_irq(&ffs->ev.waitq.lock);
365
366 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100367 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200368 ret = PTR_ERR(data);
369 break;
370 }
371
372 spin_lock_irq(&ffs->ev.waitq.lock);
373
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100374 /*
375 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200376 * but the state of setup could have changed from
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100377 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200378 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100379 * from user space in vain but it's unlikely.
380 *
381 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200382 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
383 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100384 * mutex.
385 */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100386 if (ffs_setup_state_clear_cancelled(ffs) ==
387 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200388 ret = -EIDRM;
389done_spin:
390 spin_unlock_irq(&ffs->ev.waitq.lock);
391 } else {
392 /* unlocks spinlock */
393 ret = __ffs_ep0_queue_wait(ffs, data, len);
394 }
395 kfree(data);
396 break;
397
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200398 default:
399 ret = -EBADFD;
400 break;
401 }
402
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200403 mutex_unlock(&ffs->mutex);
404 return ret;
405}
406
Michal Nazarewicz67913bb2014-09-10 17:50:24 +0200407/* Called with ffs->ev.waitq.lock and ffs->mutex held, both released on exit. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200408static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
409 size_t n)
410{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100411 /*
Michal Nazarewicz67913bb2014-09-10 17:50:24 +0200412 * n cannot be bigger than ffs->ev.count, which cannot be bigger than
413 * size of ffs->ev.types array (which is four) so that's how much space
414 * we reserve.
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100415 */
Michal Nazarewicz67913bb2014-09-10 17:50:24 +0200416 struct usb_functionfs_event events[ARRAY_SIZE(ffs->ev.types)];
417 const size_t size = n * sizeof *events;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200418 unsigned i = 0;
419
Michal Nazarewicz67913bb2014-09-10 17:50:24 +0200420 memset(events, 0, size);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200421
422 do {
423 events[i].type = ffs->ev.types[i];
424 if (events[i].type == FUNCTIONFS_SETUP) {
425 events[i].u.setup = ffs->ev.setup;
426 ffs->setup_state = FFS_SETUP_PENDING;
427 }
428 } while (++i < n);
429
Michal Nazarewicz67913bb2014-09-10 17:50:24 +0200430 ffs->ev.count -= n;
431 if (ffs->ev.count)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200432 memmove(ffs->ev.types, ffs->ev.types + n,
433 ffs->ev.count * sizeof *ffs->ev.types);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200434
435 spin_unlock_irq(&ffs->ev.waitq.lock);
436 mutex_unlock(&ffs->mutex);
437
Daniel Walter7fe9a932015-11-18 17:15:49 +0100438 return unlikely(copy_to_user(buf, events, size)) ? -EFAULT : size;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200439}
440
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200441static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
442 size_t len, loff_t *ptr)
443{
444 struct ffs_data *ffs = file->private_data;
445 char *data = NULL;
446 size_t n;
447 int ret;
448
449 ENTER();
450
451 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100452 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200453 return -EIDRM;
454
455 /* Acquire mutex */
456 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
457 if (unlikely(ret < 0))
458 return ret;
459
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200460 /* Check state */
461 if (ffs->state != FFS_ACTIVE) {
462 ret = -EBADFD;
463 goto done_mutex;
464 }
465
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100466 /*
467 * We're called from user space, we can use _irq rather then
468 * _irqsave
469 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200470 spin_lock_irq(&ffs->ev.waitq.lock);
471
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100472 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100473 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200474 ret = -EIDRM;
475 break;
476
477 case FFS_NO_SETUP:
478 n = len / sizeof(struct usb_functionfs_event);
479 if (unlikely(!n)) {
480 ret = -EINVAL;
481 break;
482 }
483
484 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
485 ret = -EAGAIN;
486 break;
487 }
488
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100489 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
490 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200491 ret = -EINTR;
492 break;
493 }
494
495 return __ffs_ep0_read_events(ffs, buf,
496 min(n, (size_t)ffs->ev.count));
497
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200498 case FFS_SETUP_PENDING:
499 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
500 spin_unlock_irq(&ffs->ev.waitq.lock);
501 ret = __ffs_ep0_stall(ffs);
502 goto done_mutex;
503 }
504
505 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
506
507 spin_unlock_irq(&ffs->ev.waitq.lock);
508
509 if (likely(len)) {
510 data = kmalloc(len, GFP_KERNEL);
511 if (unlikely(!data)) {
512 ret = -ENOMEM;
513 goto done_mutex;
514 }
515 }
516
517 spin_lock_irq(&ffs->ev.waitq.lock);
518
519 /* See ffs_ep0_write() */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100520 if (ffs_setup_state_clear_cancelled(ffs) ==
521 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200522 ret = -EIDRM;
523 break;
524 }
525
526 /* unlocks spinlock */
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
Daniel Walter7fe9a932015-11-18 17:15:49 +0100528 if (likely(ret > 0) && unlikely(copy_to_user(buf, data, len)))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200529 ret = -EFAULT;
530 goto done_mutex;
531
532 default:
533 ret = -EBADFD;
534 break;
535 }
536
537 spin_unlock_irq(&ffs->ev.waitq.lock);
538done_mutex:
539 mutex_unlock(&ffs->mutex);
540 kfree(data);
541 return ret;
542}
543
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200544static int ffs_ep0_open(struct inode *inode, struct file *file)
545{
546 struct ffs_data *ffs = inode->i_private;
547
548 ENTER();
549
550 if (unlikely(ffs->state == FFS_CLOSING))
551 return -EBUSY;
552
553 file->private_data = ffs;
554 ffs_data_opened(ffs);
555
556 return 0;
557}
558
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200559static int ffs_ep0_release(struct inode *inode, struct file *file)
560{
561 struct ffs_data *ffs = file->private_data;
562
563 ENTER();
564
565 ffs_data_closed(ffs);
566
567 return 0;
568}
569
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200570static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
571{
572 struct ffs_data *ffs = file->private_data;
573 struct usb_gadget *gadget = ffs->gadget;
574 long ret;
575
576 ENTER();
577
578 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
579 struct ffs_function *func = ffs->func;
580 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200581 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200582 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200583 } else {
584 ret = -ENOTTY;
585 }
586
587 return ret;
588}
589
Robert Baldyga23de91e2014-02-10 10:42:43 +0100590static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
591{
592 struct ffs_data *ffs = file->private_data;
593 unsigned int mask = POLLWRNORM;
594 int ret;
595
596 poll_wait(file, &ffs->ev.waitq, wait);
597
598 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
599 if (unlikely(ret < 0))
600 return mask;
601
602 switch (ffs->state) {
603 case FFS_READ_DESCRIPTORS:
604 case FFS_READ_STRINGS:
605 mask |= POLLOUT;
606 break;
607
608 case FFS_ACTIVE:
609 switch (ffs->setup_state) {
610 case FFS_NO_SETUP:
611 if (ffs->ev.count)
612 mask |= POLLIN;
613 break;
614
615 case FFS_SETUP_PENDING:
616 case FFS_SETUP_CANCELLED:
617 mask |= (POLLIN | POLLOUT);
618 break;
619 }
620 case FFS_CLOSING:
621 break;
Robert Baldyga18d6b32f2014-12-18 09:55:10 +0100622 case FFS_DEACTIVATED:
623 break;
Robert Baldyga23de91e2014-02-10 10:42:43 +0100624 }
625
626 mutex_unlock(&ffs->mutex);
627
628 return mask;
629}
630
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200631static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200632 .llseek = no_llseek,
633
634 .open = ffs_ep0_open,
635 .write = ffs_ep0_write,
636 .read = ffs_ep0_read,
637 .release = ffs_ep0_release,
638 .unlocked_ioctl = ffs_ep0_ioctl,
Robert Baldyga23de91e2014-02-10 10:42:43 +0100639 .poll = ffs_ep0_poll,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200640};
641
642
643/* "Normal" endpoints operations ********************************************/
644
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200645static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
646{
647 ENTER();
648 if (likely(req->context)) {
649 struct ffs_ep *ep = _ep->driver_data;
650 ep->status = req->status ? req->status : req->actual;
651 complete(req->context);
652 }
653}
654
Michal Nazarewiczc662a312016-05-21 20:47:34 +0200655static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
656{
657 ssize_t ret = copy_to_iter(data, data_len, iter);
658 if (likely(ret == data_len))
659 return ret;
660
661 if (unlikely(iov_iter_count(iter)))
662 return -EFAULT;
663
664 /*
665 * Dear user space developer!
666 *
667 * TL;DR: To stop getting below error message in your kernel log, change
668 * user space code using functionfs to align read buffers to a max
669 * packet size.
670 *
671 * Some UDCs (e.g. dwc3) require request sizes to be a multiple of a max
672 * packet size. When unaligned buffer is passed to functionfs, it
673 * internally uses a larger, aligned buffer so that such UDCs are happy.
674 *
675 * Unfortunately, this means that host may send more data than was
676 * requested in read(2) system call. f_fs doesn’t know what to do with
677 * that excess data so it simply drops it.
678 *
679 * Was the buffer aligned in the first place, no such problem would
680 * happen.
681 *
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200682 * Data may be dropped only in AIO reads. Synchronous reads are handled
683 * by splitting a request into multiple parts. This splitting may still
684 * be a problem though so it’s likely best to align the buffer
685 * regardless of it being AIO or not..
686 *
Michal Nazarewiczc662a312016-05-21 20:47:34 +0200687 * This only affects OUT endpoints, i.e. reading data with a read(2),
688 * aio_read(2) etc. system calls. Writing data to an IN endpoint is not
689 * affected.
690 */
691 pr_err("functionfs read size %d > requested size %zd, dropping excess data. "
692 "Align read buffer size to max packet size to avoid the problem.\n",
693 data_len, ret);
694
695 return ret;
696}
697
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100698static void ffs_user_copy_worker(struct work_struct *work)
699{
700 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
701 work);
702 int ret = io_data->req->status ? io_data->req->status :
703 io_data->req->actual;
Lars-Peter Clausen38740a52016-04-14 17:01:17 +0200704 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100705
706 if (io_data->read && ret > 0) {
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100707 use_mm(io_data->mm);
Michal Nazarewiczc662a312016-05-21 20:47:34 +0200708 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100709 unuse_mm(io_data->mm);
710 }
711
Christoph Hellwig04b2fa92015-02-02 14:49:06 +0100712 io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100713
Lars-Peter Clausen38740a52016-04-14 17:01:17 +0200714 if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
Robert Baldyga5e33f6f2015-01-23 13:41:01 +0100715 eventfd_signal(io_data->ffs->ffs_eventfd, 1);
716
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100717 usb_ep_free_request(io_data->ep, io_data->req);
718
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100719 if (io_data->read)
Al Viroc993c392015-01-31 23:23:35 -0500720 kfree(io_data->to_free);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100721 kfree(io_data->buf);
722 kfree(io_data);
723}
724
725static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
726 struct usb_request *req)
727{
728 struct ffs_io_data *io_data = req->context;
729
730 ENTER();
731
732 INIT_WORK(&io_data->work, ffs_user_copy_worker);
733 schedule_work(&io_data->work);
734}
735
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200736/* Assumes epfile->mutex is held. */
737static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
738 struct iov_iter *iter)
739{
740 struct ffs_buffer *buf = epfile->read_buffer;
741 ssize_t ret;
742 if (!buf)
743 return 0;
744
745 ret = copy_to_iter(buf->data, buf->length, iter);
746 if (buf->length == ret) {
747 kfree(buf);
748 epfile->read_buffer = NULL;
749 } else if (unlikely(iov_iter_count(iter))) {
750 ret = -EFAULT;
751 } else {
752 buf->length -= ret;
753 buf->data += ret;
754 }
755 return ret;
756}
757
758/* Assumes epfile->mutex is held. */
759static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
760 void *data, int data_len,
761 struct iov_iter *iter)
762{
763 struct ffs_buffer *buf;
764
765 ssize_t ret = copy_to_iter(data, data_len, iter);
766 if (likely(data_len == ret))
767 return ret;
768
769 if (unlikely(iov_iter_count(iter)))
770 return -EFAULT;
771
772 /* See ffs_copy_to_iter for more context. */
773 pr_warn("functionfs read size %d > requested size %zd, splitting request into multiple reads.",
774 data_len, ret);
775
776 data_len -= ret;
777 buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
Dan Carpenter44963d62016-06-24 15:23:16 +0300778 if (!buf)
779 return -ENOMEM;
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200780 buf->length = data_len;
781 buf->data = buf->storage;
782 memcpy(buf->storage, data + ret, data_len);
783 epfile->read_buffer = buf;
784
785 return ret;
786}
787
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100788static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200789{
790 struct ffs_epfile *epfile = file->private_data;
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100791 struct usb_request *req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200792 struct ffs_ep *ep;
793 char *data = NULL;
David Cohenc0d31b32014-10-13 11:15:54 -0700794 ssize_t ret, data_len = -EINVAL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200795 int halt;
796
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800797 /* Are we still active? */
Michal Nazarewiczb3591f62016-01-04 20:58:12 +0100798 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
799 return -ENODEV;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800800
801 /* Wait for endpoint to be enabled */
802 ep = epfile->ep;
803 if (!ep) {
Michal Nazarewiczb3591f62016-01-04 20:58:12 +0100804 if (file->f_flags & O_NONBLOCK)
805 return -EAGAIN;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800806
807 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
Michal Nazarewiczb3591f62016-01-04 20:58:12 +0100808 if (ret)
809 return -EINTR;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800810 }
811
812 /* Do we halt? */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100813 halt = (!io_data->read == !epfile->in);
Michal Nazarewiczb3591f62016-01-04 20:58:12 +0100814 if (halt && epfile->isoc)
815 return -EINVAL;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800816
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200817 /* We will be using request and read_buffer */
818 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
819 if (unlikely(ret))
820 goto error;
821
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800822 /* Allocate & copy */
823 if (!halt) {
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200824 struct usb_gadget *gadget;
825
826 /*
827 * Do we have buffered data from previous partial read? Check
828 * that for synchronous case only because we do not have
829 * facility to ‘wake up’ a pending asynchronous read and push
830 * buffered data to it which we would need to make things behave
831 * consistently.
832 */
833 if (!io_data->aio && io_data->read) {
834 ret = __ffs_epfile_read_buffered(epfile, &io_data->data);
835 if (ret)
836 goto error_mutex;
837 }
838
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800839 /*
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100840 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100841 * before the waiting completes, so do not assign to 'gadget'
842 * earlier
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100843 */
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200844 gadget = epfile->ffs->gadget;
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100845
Chao Bi97839ca2014-04-14 11:19:53 +0800846 spin_lock_irq(&epfile->ffs->eps_lock);
847 /* In the meantime, endpoint got disabled or changed. */
848 if (epfile->ep != ep) {
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200849 ret = -ESHUTDOWN;
850 goto error_lock;
Chao Bi97839ca2014-04-14 11:19:53 +0800851 }
Al Viroc993c392015-01-31 23:23:35 -0500852 data_len = iov_iter_count(&io_data->data);
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100853 /*
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800854 * Controller may require buffer size to be aligned to
855 * maxpacketsize of an out endpoint.
856 */
Al Viroc993c392015-01-31 23:23:35 -0500857 if (io_data->read)
858 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
Chao Bi97839ca2014-04-14 11:19:53 +0800859 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800860
861 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200862 if (unlikely(!data)) {
863 ret = -ENOMEM;
864 goto error_mutex;
865 }
866 if (!io_data->read &&
867 copy_from_iter(data, data_len, &io_data->data) != data_len) {
868 ret = -EFAULT;
869 goto error_mutex;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800870 }
871 }
872
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800873 spin_lock_irq(&epfile->ffs->eps_lock);
874
875 if (epfile->ep != ep) {
876 /* In the meantime, endpoint got disabled or changed. */
877 ret = -ESHUTDOWN;
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800878 } else if (halt) {
879 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200880 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
881 usb_ep_set_halt(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200882 ret = -EBADMSG;
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100883 } else if (unlikely(data_len == -EINVAL)) {
David Cohenc0d31b32014-10-13 11:15:54 -0700884 /*
885 * Sanity Check: even though data_len can't be used
886 * uninitialized at the time I write this comment, some
887 * compilers complain about this situation.
888 * In order to keep the code clean from warnings, data_len is
889 * being initialized to -EINVAL during its declaration, which
890 * means we can't rely on compiler anymore to warn no future
891 * changes won't result in data_len being used uninitialized.
892 * For such reason, we're adding this redundant sanity check
893 * here.
894 */
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100895 WARN(1, "%s: data_len == -EINVAL\n", __func__);
896 ret = -EINVAL;
897 } else if (!io_data->aio) {
898 DECLARE_COMPLETION_ONSTACK(done);
Du, Changbinef150882015-12-29 14:36:58 +0800899 bool interrupted = false;
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100900
901 req = ep->req;
902 req->buf = data;
903 req->length = data_len;
904
905 req->context = &done;
906 req->complete = ffs_epfile_io_complete;
907
908 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
909 if (unlikely(ret < 0))
910 goto error_lock;
911
912 spin_unlock_irq(&epfile->ffs->eps_lock);
913
914 if (unlikely(wait_for_completion_interruptible(&done))) {
Du, Changbinef150882015-12-29 14:36:58 +0800915 /*
916 * To avoid race condition with ffs_epfile_io_complete,
917 * dequeue the request first then check
918 * status. usb_ep_dequeue API should guarantee no race
919 * condition with req->complete callback.
920 */
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100921 usb_ep_dequeue(ep->ep, req);
Du, Changbinef150882015-12-29 14:36:58 +0800922 interrupted = ep->status < 0;
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100923 }
924
Michal Nazarewiczc662a312016-05-21 20:47:34 +0200925 if (interrupted)
926 ret = -EINTR;
927 else if (io_data->read && ep->status > 0)
Michal Nazarewicz9353afb2016-05-21 20:47:35 +0200928 ret = __ffs_epfile_read_data(epfile, data, ep->status,
929 &io_data->data);
Michal Nazarewiczc662a312016-05-21 20:47:34 +0200930 else
931 ret = ep->status;
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100932 goto error_mutex;
933 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
934 ret = -ENOMEM;
935 } else {
936 req->buf = data;
937 req->length = data_len;
938
939 io_data->buf = data;
940 io_data->ep = ep->ep;
941 io_data->req = req;
942 io_data->ffs = epfile->ffs;
943
944 req->context = io_data;
945 req->complete = ffs_epfile_async_io_complete;
946
947 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
948 if (unlikely(ret)) {
949 usb_ep_free_request(ep->ep, req);
David Cohenc0d31b32014-10-13 11:15:54 -0700950 goto error_lock;
951 }
952
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100953 ret = -EIOCBQUEUED;
954 /*
955 * Do not kfree the buffer in this function. It will be freed
956 * by ffs_user_copy_worker.
957 */
958 data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200959 }
960
Robert Baldyga48968f82014-03-10 09:33:37 +0100961error_lock:
962 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewiczae76e132016-01-04 21:05:59 +0100963error_mutex:
Robert Baldyga48968f82014-03-10 09:33:37 +0100964 mutex_unlock(&epfile->mutex);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200965error:
966 kfree(data);
967 return ret;
968}
969
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200970static int
971ffs_epfile_open(struct inode *inode, struct file *file)
972{
973 struct ffs_epfile *epfile = inode->i_private;
974
975 ENTER();
976
977 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
978 return -ENODEV;
979
980 file->private_data = epfile;
981 ffs_data_opened(epfile->ffs);
982
983 return 0;
984}
985
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100986static int ffs_aio_cancel(struct kiocb *kiocb)
987{
988 struct ffs_io_data *io_data = kiocb->private;
989 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
990 int value;
991
992 ENTER();
993
994 spin_lock_irq(&epfile->ffs->eps_lock);
995
996 if (likely(io_data && io_data->ep && io_data->req))
997 value = usb_ep_dequeue(io_data->ep, io_data->req);
998 else
999 value = -EINVAL;
1000
1001 spin_unlock_irq(&epfile->ffs->eps_lock);
1002
1003 return value;
1004}
1005
Al Viro70e60d92015-01-31 23:55:39 -05001006static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001007{
Al Viro70e60d92015-01-31 23:55:39 -05001008 struct ffs_io_data io_data, *p = &io_data;
Al Virode2080d2015-01-31 23:42:34 -05001009 ssize_t res;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001010
1011 ENTER();
1012
Al Viro70e60d92015-01-31 23:55:39 -05001013 if (!is_sync_kiocb(kiocb)) {
1014 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1015 if (unlikely(!p))
1016 return -ENOMEM;
1017 p->aio = true;
1018 } else {
1019 p->aio = false;
1020 }
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001021
Al Viro70e60d92015-01-31 23:55:39 -05001022 p->read = false;
1023 p->kiocb = kiocb;
1024 p->data = *from;
1025 p->mm = current->mm;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001026
Al Viro70e60d92015-01-31 23:55:39 -05001027 kiocb->private = p;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001028
Rui Miguel Silva4088acf2015-05-18 16:02:07 +01001029 if (p->aio)
1030 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001031
Al Viro70e60d92015-01-31 23:55:39 -05001032 res = ffs_epfile_io(kiocb->ki_filp, p);
1033 if (res == -EIOCBQUEUED)
1034 return res;
1035 if (p->aio)
1036 kfree(p);
1037 else
1038 *from = p->data;
Al Virode2080d2015-01-31 23:42:34 -05001039 return res;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001040}
1041
Al Viro70e60d92015-01-31 23:55:39 -05001042static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001043{
Al Viro70e60d92015-01-31 23:55:39 -05001044 struct ffs_io_data io_data, *p = &io_data;
Al Virode2080d2015-01-31 23:42:34 -05001045 ssize_t res;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001046
1047 ENTER();
1048
Al Viro70e60d92015-01-31 23:55:39 -05001049 if (!is_sync_kiocb(kiocb)) {
1050 p = kmalloc(sizeof(io_data), GFP_KERNEL);
1051 if (unlikely(!p))
1052 return -ENOMEM;
1053 p->aio = true;
1054 } else {
1055 p->aio = false;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001056 }
1057
Al Viro70e60d92015-01-31 23:55:39 -05001058 p->read = true;
1059 p->kiocb = kiocb;
1060 if (p->aio) {
1061 p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
1062 if (!p->to_free) {
1063 kfree(p);
1064 return -ENOMEM;
1065 }
1066 } else {
1067 p->data = *to;
1068 p->to_free = NULL;
1069 }
1070 p->mm = current->mm;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001071
Al Viro70e60d92015-01-31 23:55:39 -05001072 kiocb->private = p;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001073
Rui Miguel Silva4088acf2015-05-18 16:02:07 +01001074 if (p->aio)
1075 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001076
Al Viro70e60d92015-01-31 23:55:39 -05001077 res = ffs_epfile_io(kiocb->ki_filp, p);
1078 if (res == -EIOCBQUEUED)
1079 return res;
1080
1081 if (p->aio) {
1082 kfree(p->to_free);
1083 kfree(p);
1084 } else {
1085 *to = p->data;
Al Virode2080d2015-01-31 23:42:34 -05001086 }
1087 return res;
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001088}
1089
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001090static int
1091ffs_epfile_release(struct inode *inode, struct file *file)
1092{
1093 struct ffs_epfile *epfile = inode->i_private;
1094
1095 ENTER();
1096
Michal Nazarewicz9353afb2016-05-21 20:47:35 +02001097 kfree(epfile->read_buffer);
1098 epfile->read_buffer = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001099 ffs_data_closed(epfile->ffs);
1100
1101 return 0;
1102}
1103
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001104static long ffs_epfile_ioctl(struct file *file, unsigned code,
1105 unsigned long value)
1106{
1107 struct ffs_epfile *epfile = file->private_data;
1108 int ret;
1109
1110 ENTER();
1111
1112 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1113 return -ENODEV;
1114
1115 spin_lock_irq(&epfile->ffs->eps_lock);
1116 if (likely(epfile->ep)) {
1117 switch (code) {
1118 case FUNCTIONFS_FIFO_STATUS:
1119 ret = usb_ep_fifo_status(epfile->ep->ep);
1120 break;
1121 case FUNCTIONFS_FIFO_FLUSH:
1122 usb_ep_fifo_flush(epfile->ep->ep);
1123 ret = 0;
1124 break;
1125 case FUNCTIONFS_CLEAR_HALT:
1126 ret = usb_ep_clear_halt(epfile->ep->ep);
1127 break;
1128 case FUNCTIONFS_ENDPOINT_REVMAP:
1129 ret = epfile->ep->num;
1130 break;
Robert Baldygac559a352014-09-09 08:23:16 +02001131 case FUNCTIONFS_ENDPOINT_DESC:
1132 {
1133 int desc_idx;
1134 struct usb_endpoint_descriptor *desc;
1135
1136 switch (epfile->ffs->gadget->speed) {
1137 case USB_SPEED_SUPER:
1138 desc_idx = 2;
1139 break;
1140 case USB_SPEED_HIGH:
1141 desc_idx = 1;
1142 break;
1143 default:
1144 desc_idx = 0;
1145 }
1146 desc = epfile->ep->descs[desc_idx];
1147
1148 spin_unlock_irq(&epfile->ffs->eps_lock);
1149 ret = copy_to_user((void *)value, desc, sizeof(*desc));
1150 if (ret)
1151 ret = -EFAULT;
1152 return ret;
1153 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001154 default:
1155 ret = -ENOTTY;
1156 }
1157 } else {
1158 ret = -ENODEV;
1159 }
1160 spin_unlock_irq(&epfile->ffs->eps_lock);
1161
1162 return ret;
1163}
1164
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001165static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166 .llseek = no_llseek,
1167
1168 .open = ffs_epfile_open,
Al Viro70e60d92015-01-31 23:55:39 -05001169 .write_iter = ffs_epfile_write_iter,
1170 .read_iter = ffs_epfile_read_iter,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001171 .release = ffs_epfile_release,
1172 .unlocked_ioctl = ffs_epfile_ioctl,
1173};
1174
1175
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001176/* File system and super block operations ***********************************/
1177
1178/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001179 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001180 * function configuration then later for event monitoring.
1181 */
1182
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001183static struct inode *__must_check
1184ffs_sb_make_inode(struct super_block *sb, void *data,
1185 const struct file_operations *fops,
1186 const struct inode_operations *iops,
1187 struct ffs_file_perms *perms)
1188{
1189 struct inode *inode;
1190
1191 ENTER();
1192
1193 inode = new_inode(sb);
1194
1195 if (likely(inode)) {
1196 struct timespec current_time = CURRENT_TIME;
1197
Al Viro12ba8d12010-10-27 04:19:36 +01001198 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001199 inode->i_mode = perms->mode;
1200 inode->i_uid = perms->uid;
1201 inode->i_gid = perms->gid;
1202 inode->i_atime = current_time;
1203 inode->i_mtime = current_time;
1204 inode->i_ctime = current_time;
1205 inode->i_private = data;
1206 if (fops)
1207 inode->i_fop = fops;
1208 if (iops)
1209 inode->i_op = iops;
1210 }
1211
1212 return inode;
1213}
1214
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001215/* Create "regular" file */
Al Viro1bb27ca2014-09-03 13:32:19 -04001216static struct dentry *ffs_sb_create_file(struct super_block *sb,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001217 const char *name, void *data,
Al Viro1bb27ca2014-09-03 13:32:19 -04001218 const struct file_operations *fops)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001219{
1220 struct ffs_data *ffs = sb->s_fs_info;
1221 struct dentry *dentry;
1222 struct inode *inode;
1223
1224 ENTER();
1225
1226 dentry = d_alloc_name(sb->s_root, name);
1227 if (unlikely(!dentry))
1228 return NULL;
1229
1230 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1231 if (unlikely(!inode)) {
1232 dput(dentry);
1233 return NULL;
1234 }
1235
1236 d_add(dentry, inode);
Al Viro1bb27ca2014-09-03 13:32:19 -04001237 return dentry;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001238}
1239
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001240/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001241static const struct super_operations ffs_sb_operations = {
1242 .statfs = simple_statfs,
1243 .drop_inode = generic_delete_inode,
1244};
1245
1246struct ffs_sb_fill_data {
1247 struct ffs_file_perms perms;
1248 umode_t root_mode;
1249 const char *dev_name;
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001250 bool no_disconnect;
Al Viro2606b282013-09-20 17:14:21 +01001251 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001252};
1253
1254static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1255{
1256 struct ffs_sb_fill_data *data = _data;
1257 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +01001258 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001259
1260 ENTER();
1261
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001262 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +01001263 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001264 sb->s_fs_info = ffs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001265 sb->s_blocksize = PAGE_SIZE;
1266 sb->s_blocksize_bits = PAGE_SHIFT;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001267 sb->s_magic = FUNCTIONFS_MAGIC;
1268 sb->s_op = &ffs_sb_operations;
1269 sb->s_time_gran = 1;
1270
1271 /* Root inode */
1272 data->perms.mode = data->root_mode;
1273 inode = ffs_sb_make_inode(sb, NULL,
1274 &simple_dir_operations,
1275 &simple_dir_inode_operations,
1276 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001277 sb->s_root = d_make_root(inode);
1278 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +01001279 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001280
1281 /* EP0 file */
1282 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
Al Viro1bb27ca2014-09-03 13:32:19 -04001283 &ffs_ep0_operations)))
Al Viro2606b282013-09-20 17:14:21 +01001284 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001285
1286 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001287}
1288
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001289static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1290{
1291 ENTER();
1292
1293 if (!opts || !*opts)
1294 return 0;
1295
1296 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001297 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001298 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001299
1300 /* Option limit */
1301 comma = strchr(opts, ',');
1302 if (comma)
1303 *comma = 0;
1304
1305 /* Value limit */
1306 eq = strchr(opts, '=');
1307 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001308 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001309 return -EINVAL;
1310 }
1311 *eq = 0;
1312
1313 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001314 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001315 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001316 return -EINVAL;
1317 }
1318
1319 /* Interpret option */
1320 switch (eq - opts) {
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001321 case 13:
1322 if (!memcmp(opts, "no_disconnect", 13))
1323 data->no_disconnect = !!value;
1324 else
1325 goto invalid;
1326 break;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001327 case 5:
1328 if (!memcmp(opts, "rmode", 5))
1329 data->root_mode = (value & 0555) | S_IFDIR;
1330 else if (!memcmp(opts, "fmode", 5))
1331 data->perms.mode = (value & 0666) | S_IFREG;
1332 else
1333 goto invalid;
1334 break;
1335
1336 case 4:
1337 if (!memcmp(opts, "mode", 4)) {
1338 data->root_mode = (value & 0555) | S_IFDIR;
1339 data->perms.mode = (value & 0666) | S_IFREG;
1340 } else {
1341 goto invalid;
1342 }
1343 break;
1344
1345 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001346 if (!memcmp(opts, "uid", 3)) {
1347 data->perms.uid = make_kuid(current_user_ns(), value);
1348 if (!uid_valid(data->perms.uid)) {
1349 pr_err("%s: unmapped value: %lu\n", opts, value);
1350 return -EINVAL;
1351 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001352 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001353 data->perms.gid = make_kgid(current_user_ns(), value);
1354 if (!gid_valid(data->perms.gid)) {
1355 pr_err("%s: unmapped value: %lu\n", opts, value);
1356 return -EINVAL;
1357 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001358 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001359 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -08001360 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001361 break;
1362
1363 default:
1364invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001365 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001366 return -EINVAL;
1367 }
1368
1369 /* Next iteration */
1370 if (!comma)
1371 break;
1372 opts = comma + 1;
1373 }
1374
1375 return 0;
1376}
1377
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001378/* "mount -t functionfs dev_name /dev/function" ends up here */
1379
Al Virofc14f2f2010-07-25 01:48:30 +04001380static struct dentry *
1381ffs_fs_mount(struct file_system_type *t, int flags,
1382 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001383{
1384 struct ffs_sb_fill_data data = {
1385 .perms = {
1386 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001387 .uid = GLOBAL_ROOT_UID,
1388 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001389 },
1390 .root_mode = S_IFDIR | 0500,
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001391 .no_disconnect = false,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001392 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001393 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001394 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001395 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001396 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001397
1398 ENTER();
1399
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001400 ret = ffs_fs_parse_opts(&data, opts);
1401 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001402 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403
Al Viro2606b282013-09-20 17:14:21 +01001404 ffs = ffs_data_new();
1405 if (unlikely(!ffs))
1406 return ERR_PTR(-ENOMEM);
1407 ffs->file_perms = data.perms;
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001408 ffs->no_disconnect = data.no_disconnect;
Al Viro2606b282013-09-20 17:14:21 +01001409
1410 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1411 if (unlikely(!ffs->dev_name)) {
1412 ffs_data_put(ffs);
1413 return ERR_PTR(-ENOMEM);
1414 }
1415
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001416 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001417 if (IS_ERR(ffs_dev)) {
1418 ffs_data_put(ffs);
1419 return ERR_CAST(ffs_dev);
1420 }
1421 ffs->private_data = ffs_dev;
1422 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001423
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001424 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001425 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001426 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001427 ffs_data_put(data.ffs_data);
1428 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001429 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001430}
1431
1432static void
1433ffs_fs_kill_sb(struct super_block *sb)
1434{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001435 ENTER();
1436
1437 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001438 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001439 ffs_release_dev(sb->s_fs_info);
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001440 ffs_data_closed(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001441 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001442 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001443}
1444
1445static struct file_system_type ffs_fs_type = {
1446 .owner = THIS_MODULE,
1447 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001448 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001449 .kill_sb = ffs_fs_kill_sb,
1450};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001451MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001452
1453
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001454/* Driver's main init/cleanup functions *************************************/
1455
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001456static int functionfs_init(void)
1457{
1458 int ret;
1459
1460 ENTER();
1461
1462 ret = register_filesystem(&ffs_fs_type);
1463 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001464 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001465 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001466 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001467
1468 return ret;
1469}
1470
1471static void functionfs_cleanup(void)
1472{
1473 ENTER();
1474
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001475 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001476 unregister_filesystem(&ffs_fs_type);
1477}
1478
1479
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001480/* ffs_data and ffs_function construction and destruction code **************/
1481
1482static void ffs_data_clear(struct ffs_data *ffs);
1483static void ffs_data_reset(struct ffs_data *ffs);
1484
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001485static void ffs_data_get(struct ffs_data *ffs)
1486{
1487 ENTER();
1488
1489 atomic_inc(&ffs->ref);
1490}
1491
1492static void ffs_data_opened(struct ffs_data *ffs)
1493{
1494 ENTER();
1495
1496 atomic_inc(&ffs->ref);
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001497 if (atomic_add_return(1, &ffs->opened) == 1 &&
1498 ffs->state == FFS_DEACTIVATED) {
1499 ffs->state = FFS_CLOSING;
1500 ffs_data_reset(ffs);
1501 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001502}
1503
1504static void ffs_data_put(struct ffs_data *ffs)
1505{
1506 ENTER();
1507
1508 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001509 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001510 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001511 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001512 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001513 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001514 kfree(ffs);
1515 }
1516}
1517
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001518static void ffs_data_closed(struct ffs_data *ffs)
1519{
1520 ENTER();
1521
1522 if (atomic_dec_and_test(&ffs->opened)) {
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001523 if (ffs->no_disconnect) {
1524 ffs->state = FFS_DEACTIVATED;
1525 if (ffs->epfiles) {
1526 ffs_epfiles_destroy(ffs->epfiles,
1527 ffs->eps_count);
1528 ffs->epfiles = NULL;
1529 }
1530 if (ffs->setup_state == FFS_SETUP_PENDING)
1531 __ffs_ep0_stall(ffs);
1532 } else {
1533 ffs->state = FFS_CLOSING;
1534 ffs_data_reset(ffs);
1535 }
1536 }
1537 if (atomic_read(&ffs->opened) < 0) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001538 ffs->state = FFS_CLOSING;
1539 ffs_data_reset(ffs);
1540 }
1541
1542 ffs_data_put(ffs);
1543}
1544
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001545static struct ffs_data *ffs_data_new(void)
1546{
1547 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1548 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001549 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001550
1551 ENTER();
1552
1553 atomic_set(&ffs->ref, 1);
1554 atomic_set(&ffs->opened, 0);
1555 ffs->state = FFS_READ_DESCRIPTORS;
1556 mutex_init(&ffs->mutex);
1557 spin_lock_init(&ffs->eps_lock);
1558 init_waitqueue_head(&ffs->ev.waitq);
1559 init_completion(&ffs->ep0req_completion);
1560
1561 /* XXX REVISIT need to update it in some places, or do we? */
1562 ffs->ev.can_stall = 1;
1563
1564 return ffs;
1565}
1566
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001567static void ffs_data_clear(struct ffs_data *ffs)
1568{
1569 ENTER();
1570
Krzysztof Opasiak49a79d82015-05-22 17:25:18 +02001571 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001572
1573 BUG_ON(ffs->gadget);
1574
1575 if (ffs->epfiles)
1576 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1577
Robert Baldyga5e33f6f2015-01-23 13:41:01 +01001578 if (ffs->ffs_eventfd)
1579 eventfd_ctx_put(ffs->ffs_eventfd);
1580
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301581 kfree(ffs->raw_descs_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001582 kfree(ffs->raw_strings);
1583 kfree(ffs->stringtabs);
1584}
1585
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001586static void ffs_data_reset(struct ffs_data *ffs)
1587{
1588 ENTER();
1589
1590 ffs_data_clear(ffs);
1591
1592 ffs->epfiles = NULL;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301593 ffs->raw_descs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001594 ffs->raw_descs = NULL;
1595 ffs->raw_strings = NULL;
1596 ffs->stringtabs = NULL;
1597
1598 ffs->raw_descs_length = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001599 ffs->fs_descs_count = 0;
1600 ffs->hs_descs_count = 0;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301601 ffs->ss_descs_count = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001602
1603 ffs->strings_count = 0;
1604 ffs->interfaces_count = 0;
1605 ffs->eps_count = 0;
1606
1607 ffs->ev.count = 0;
1608
1609 ffs->state = FFS_READ_DESCRIPTORS;
1610 ffs->setup_state = FFS_NO_SETUP;
1611 ffs->flags = 0;
1612}
1613
1614
1615static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1616{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001617 struct usb_gadget_strings **lang;
1618 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001619
1620 ENTER();
1621
1622 if (WARN_ON(ffs->state != FFS_ACTIVE
1623 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1624 return -EBADFD;
1625
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001626 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1627 if (unlikely(first_id < 0))
1628 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001629
1630 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1631 if (unlikely(!ffs->ep0req))
1632 return -ENOMEM;
1633 ffs->ep0req->complete = ffs_ep0_complete;
1634 ffs->ep0req->context = ffs;
1635
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001636 lang = ffs->stringtabs;
Michal Nazarewiczf0688c82014-06-17 17:47:41 +02001637 if (lang) {
1638 for (; *lang; ++lang) {
1639 struct usb_string *str = (*lang)->strings;
1640 int id = first_id;
1641 for (; str->s; ++id, ++str)
1642 str->id = id;
1643 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001644 }
1645
1646 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001647 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001648 return 0;
1649}
1650
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001651static void functionfs_unbind(struct ffs_data *ffs)
1652{
1653 ENTER();
1654
1655 if (!WARN_ON(!ffs->gadget)) {
1656 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1657 ffs->ep0req = NULL;
1658 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001659 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001660 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001661 }
1662}
1663
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001664static int ffs_epfiles_create(struct ffs_data *ffs)
1665{
1666 struct ffs_epfile *epfile, *epfiles;
1667 unsigned i, count;
1668
1669 ENTER();
1670
1671 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001672 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001673 if (!epfiles)
1674 return -ENOMEM;
1675
1676 epfile = epfiles;
1677 for (i = 1; i <= count; ++i, ++epfile) {
1678 epfile->ffs = ffs;
1679 mutex_init(&epfile->mutex);
1680 init_waitqueue_head(&epfile->wait);
Robert Baldyga1b0bf882014-09-09 08:23:17 +02001681 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
Mario Schuknechtacba23f2015-01-26 20:40:21 +01001682 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]);
Robert Baldyga1b0bf882014-09-09 08:23:17 +02001683 else
Mario Schuknechtacba23f2015-01-26 20:40:21 +01001684 sprintf(epfile->name, "ep%u", i);
1685 epfile->dentry = ffs_sb_create_file(ffs->sb, epfile->name,
Al Viro1bb27ca2014-09-03 13:32:19 -04001686 epfile,
1687 &ffs_epfile_operations);
1688 if (unlikely(!epfile->dentry)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001689 ffs_epfiles_destroy(epfiles, i - 1);
1690 return -ENOMEM;
1691 }
1692 }
1693
1694 ffs->epfiles = epfiles;
1695 return 0;
1696}
1697
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001698static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1699{
1700 struct ffs_epfile *epfile = epfiles;
1701
1702 ENTER();
1703
1704 for (; count; --count, ++epfile) {
1705 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1706 waitqueue_active(&epfile->wait));
1707 if (epfile->dentry) {
1708 d_delete(epfile->dentry);
1709 dput(epfile->dentry);
1710 epfile->dentry = NULL;
1711 }
1712 }
1713
1714 kfree(epfiles);
1715}
1716
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001717static void ffs_func_eps_disable(struct ffs_function *func)
1718{
1719 struct ffs_ep *ep = func->eps;
1720 struct ffs_epfile *epfile = func->ffs->epfiles;
1721 unsigned count = func->ffs->eps_count;
1722 unsigned long flags;
1723
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001724 do {
Michal Nazarewicz9353afb2016-05-21 20:47:35 +02001725 if (epfile)
1726 mutex_lock(&epfile->mutex);
1727 spin_lock_irqsave(&func->ffs->eps_lock, flags);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001728 /* pending requests get nuked */
1729 if (likely(ep->ep))
1730 usb_ep_disable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001731 ++ep;
Michal Nazarewicz9353afb2016-05-21 20:47:35 +02001732 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001733
1734 if (epfile) {
1735 epfile->ep = NULL;
Michal Nazarewicz9353afb2016-05-21 20:47:35 +02001736 kfree(epfile->read_buffer);
1737 epfile->read_buffer = NULL;
1738 mutex_unlock(&epfile->mutex);
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01001739 ++epfile;
1740 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001741 } while (--count);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001742}
1743
1744static int ffs_func_eps_enable(struct ffs_function *func)
1745{
1746 struct ffs_data *ffs = func->ffs;
1747 struct ffs_ep *ep = func->eps;
1748 struct ffs_epfile *epfile = ffs->epfiles;
1749 unsigned count = ffs->eps_count;
1750 unsigned long flags;
1751 int ret = 0;
1752
1753 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1754 do {
1755 struct usb_endpoint_descriptor *ds;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301756 int desc_idx;
1757
1758 if (ffs->gadget->speed == USB_SPEED_SUPER)
1759 desc_idx = 2;
1760 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1761 desc_idx = 1;
1762 else
1763 desc_idx = 0;
1764
1765 /* fall-back to lower speed if desc missing for current speed */
1766 do {
1767 ds = ep->descs[desc_idx];
1768 } while (!ds && --desc_idx >= 0);
1769
1770 if (!ds) {
1771 ret = -EINVAL;
1772 break;
1773 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001774
1775 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001776 ep->ep->desc = ds;
1777 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001778 if (likely(!ret)) {
1779 epfile->ep = ep;
1780 epfile->in = usb_endpoint_dir_in(ds);
1781 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1782 } else {
1783 break;
1784 }
1785
1786 wake_up(&epfile->wait);
1787
1788 ++ep;
1789 ++epfile;
1790 } while (--count);
1791 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1792
1793 return ret;
1794}
1795
1796
1797/* Parsing and building descriptors and strings *****************************/
1798
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001799/*
1800 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001801 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001802 * required by given configuration. Returns address after the
1803 * descriptor or NULL if data is invalid.
1804 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001805
1806enum ffs_entity_type {
1807 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1808};
1809
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02001810enum ffs_os_desc_type {
1811 FFS_OS_DESC, FFS_OS_DESC_EXT_COMPAT, FFS_OS_DESC_EXT_PROP
1812};
1813
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001814typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1815 u8 *valuep,
1816 struct usb_descriptor_header *desc,
1817 void *priv);
1818
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02001819typedef int (*ffs_os_desc_callback)(enum ffs_os_desc_type entity,
1820 struct usb_os_desc_header *h, void *data,
1821 unsigned len, void *priv);
1822
Andrzej Pietrasiewiczf96cbd12014-07-09 12:20:06 +02001823static int __must_check ffs_do_single_desc(char *data, unsigned len,
1824 ffs_entity_callback entity,
1825 void *priv)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001826{
1827 struct usb_descriptor_header *_ds = (void *)data;
1828 u8 length;
1829 int ret;
1830
1831 ENTER();
1832
1833 /* At least two bytes are required: length and type */
1834 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001835 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001836 return -EINVAL;
1837 }
1838
1839 /* If we have at least as many bytes as the descriptor takes? */
1840 length = _ds->bLength;
1841 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001842 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001843 return -EINVAL;
1844 }
1845
1846#define __entity_check_INTERFACE(val) 1
1847#define __entity_check_STRING(val) (val)
1848#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1849#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001850 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001851 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001852 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001853 return -EINVAL; \
1854 } \
1855 ret = entity(FFS_ ##type, &val, _ds, priv); \
1856 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001857 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001858 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001859 return ret; \
1860 } \
1861 } while (0)
1862
1863 /* Parse descriptor depending on type. */
1864 switch (_ds->bDescriptorType) {
1865 case USB_DT_DEVICE:
1866 case USB_DT_CONFIG:
1867 case USB_DT_STRING:
1868 case USB_DT_DEVICE_QUALIFIER:
1869 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001870 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001871 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001872 return -EINVAL;
1873
1874 case USB_DT_INTERFACE: {
1875 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001876 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001877 if (length != sizeof *ds)
1878 goto inv_length;
1879
1880 __entity(INTERFACE, ds->bInterfaceNumber);
1881 if (ds->iInterface)
1882 __entity(STRING, ds->iInterface);
1883 }
1884 break;
1885
1886 case USB_DT_ENDPOINT: {
1887 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001888 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001889 if (length != USB_DT_ENDPOINT_SIZE &&
1890 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1891 goto inv_length;
1892 __entity(ENDPOINT, ds->bEndpointAddress);
1893 }
1894 break;
1895
Koen Beel560f1182012-05-30 20:43:37 +02001896 case HID_DT_HID:
1897 pr_vdebug("hid descriptor\n");
1898 if (length != sizeof(struct hid_descriptor))
1899 goto inv_length;
1900 break;
1901
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001902 case USB_DT_OTG:
1903 if (length != sizeof(struct usb_otg_descriptor))
1904 goto inv_length;
1905 break;
1906
1907 case USB_DT_INTERFACE_ASSOCIATION: {
1908 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001909 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001910 if (length != sizeof *ds)
1911 goto inv_length;
1912 if (ds->iFunction)
1913 __entity(STRING, ds->iFunction);
1914 }
1915 break;
1916
Manu Gautam8d4e8972014-02-28 16:50:22 +05301917 case USB_DT_SS_ENDPOINT_COMP:
1918 pr_vdebug("EP SS companion descriptor\n");
1919 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1920 goto inv_length;
1921 break;
1922
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001923 case USB_DT_OTHER_SPEED_CONFIG:
1924 case USB_DT_INTERFACE_POWER:
1925 case USB_DT_DEBUG:
1926 case USB_DT_SECURITY:
1927 case USB_DT_CS_RADIO_CONTROL:
1928 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001929 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001930 return -EINVAL;
1931
1932 default:
1933 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001934 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001935 return -EINVAL;
1936
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001937inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001938 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001939 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001940 return -EINVAL;
1941 }
1942
1943#undef __entity
1944#undef __entity_check_DESCRIPTOR
1945#undef __entity_check_INTERFACE
1946#undef __entity_check_STRING
1947#undef __entity_check_ENDPOINT
1948
1949 return length;
1950}
1951
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001952static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1953 ffs_entity_callback entity, void *priv)
1954{
1955 const unsigned _len = len;
1956 unsigned long num = 0;
1957
1958 ENTER();
1959
1960 for (;;) {
1961 int ret;
1962
1963 if (num == count)
1964 data = NULL;
1965
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001966 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001967 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1968 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001969 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001970 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001971 return ret;
1972 }
1973
1974 if (!data)
1975 return _len - len;
1976
Andrzej Pietrasiewiczf96cbd12014-07-09 12:20:06 +02001977 ret = ffs_do_single_desc(data, len, entity, priv);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001978 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001979 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001980 return ret;
1981 }
1982
1983 len -= ret;
1984 data += ret;
1985 ++num;
1986 }
1987}
1988
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001989static int __ffs_data_do_entity(enum ffs_entity_type type,
1990 u8 *valuep, struct usb_descriptor_header *desc,
1991 void *priv)
1992{
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02001993 struct ffs_desc_helper *helper = priv;
1994 struct usb_endpoint_descriptor *d;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001995
1996 ENTER();
1997
1998 switch (type) {
1999 case FFS_DESCRIPTOR:
2000 break;
2001
2002 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002003 /*
2004 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002005 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002006 * "n+1" interfaces.
2007 */
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002008 if (*valuep >= helper->interfaces_count)
2009 helper->interfaces_count = *valuep + 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002010 break;
2011
2012 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002013 /*
2014 * Strings are indexed from 1 (0 is magic ;) reserved
2015 * for languages list or some such)
2016 */
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002017 if (*valuep > helper->ffs->strings_count)
2018 helper->ffs->strings_count = *valuep;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002019 break;
2020
2021 case FFS_ENDPOINT:
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002022 d = (void *)desc;
2023 helper->eps_count++;
2024 if (helper->eps_count >= 15)
2025 return -EINVAL;
2026 /* Check if descriptors for any speed were already parsed */
2027 if (!helper->ffs->eps_count && !helper->ffs->interfaces_count)
2028 helper->ffs->eps_addrmap[helper->eps_count] =
2029 d->bEndpointAddress;
2030 else if (helper->ffs->eps_addrmap[helper->eps_count] !=
2031 d->bEndpointAddress)
2032 return -EINVAL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002033 break;
2034 }
2035
2036 return 0;
2037}
2038
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002039static int __ffs_do_os_desc_header(enum ffs_os_desc_type *next_type,
2040 struct usb_os_desc_header *desc)
2041{
2042 u16 bcd_version = le16_to_cpu(desc->bcdVersion);
2043 u16 w_index = le16_to_cpu(desc->wIndex);
2044
2045 if (bcd_version != 1) {
2046 pr_vdebug("unsupported os descriptors version: %d",
2047 bcd_version);
2048 return -EINVAL;
2049 }
2050 switch (w_index) {
2051 case 0x4:
2052 *next_type = FFS_OS_DESC_EXT_COMPAT;
2053 break;
2054 case 0x5:
2055 *next_type = FFS_OS_DESC_EXT_PROP;
2056 break;
2057 default:
2058 pr_vdebug("unsupported os descriptor type: %d", w_index);
2059 return -EINVAL;
2060 }
2061
2062 return sizeof(*desc);
2063}
2064
2065/*
2066 * Process all extended compatibility/extended property descriptors
2067 * of a feature descriptor
2068 */
2069static int __must_check ffs_do_single_os_desc(char *data, unsigned len,
2070 enum ffs_os_desc_type type,
2071 u16 feature_count,
2072 ffs_os_desc_callback entity,
2073 void *priv,
2074 struct usb_os_desc_header *h)
2075{
2076 int ret;
2077 const unsigned _len = len;
2078
2079 ENTER();
2080
2081 /* loop over all ext compat/ext prop descriptors */
2082 while (feature_count--) {
2083 ret = entity(type, h, data, len, priv);
2084 if (unlikely(ret < 0)) {
2085 pr_debug("bad OS descriptor, type: %d\n", type);
2086 return ret;
2087 }
2088 data += ret;
2089 len -= ret;
2090 }
2091 return _len - len;
2092}
2093
2094/* Process a number of complete Feature Descriptors (Ext Compat or Ext Prop) */
2095static int __must_check ffs_do_os_descs(unsigned count,
2096 char *data, unsigned len,
2097 ffs_os_desc_callback entity, void *priv)
2098{
2099 const unsigned _len = len;
2100 unsigned long num = 0;
2101
2102 ENTER();
2103
2104 for (num = 0; num < count; ++num) {
2105 int ret;
2106 enum ffs_os_desc_type type;
2107 u16 feature_count;
2108 struct usb_os_desc_header *desc = (void *)data;
2109
2110 if (len < sizeof(*desc))
2111 return -EINVAL;
2112
2113 /*
2114 * Record "descriptor" entity.
2115 * Process dwLength, bcdVersion, wIndex, get b/wCount.
2116 * Move the data pointer to the beginning of extended
2117 * compatibilities proper or extended properties proper
2118 * portions of the data
2119 */
2120 if (le32_to_cpu(desc->dwLength) > len)
2121 return -EINVAL;
2122
2123 ret = __ffs_do_os_desc_header(&type, desc);
2124 if (unlikely(ret < 0)) {
2125 pr_debug("entity OS_DESCRIPTOR(%02lx); ret = %d\n",
2126 num, ret);
2127 return ret;
2128 }
2129 /*
2130 * 16-bit hex "?? 00" Little Endian looks like 8-bit hex "??"
2131 */
2132 feature_count = le16_to_cpu(desc->wCount);
2133 if (type == FFS_OS_DESC_EXT_COMPAT &&
2134 (feature_count > 255 || desc->Reserved))
2135 return -EINVAL;
2136 len -= ret;
2137 data += ret;
2138
2139 /*
2140 * Process all function/property descriptors
2141 * of this Feature Descriptor
2142 */
2143 ret = ffs_do_single_os_desc(data, len, type,
2144 feature_count, entity, priv, desc);
2145 if (unlikely(ret < 0)) {
2146 pr_debug("%s returns %d\n", __func__, ret);
2147 return ret;
2148 }
2149
2150 len -= ret;
2151 data += ret;
2152 }
2153 return _len - len;
2154}
2155
2156/**
2157 * Validate contents of the buffer from userspace related to OS descriptors.
2158 */
2159static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
2160 struct usb_os_desc_header *h, void *data,
2161 unsigned len, void *priv)
2162{
2163 struct ffs_data *ffs = priv;
2164 u8 length;
2165
2166 ENTER();
2167
2168 switch (type) {
2169 case FFS_OS_DESC_EXT_COMPAT: {
2170 struct usb_ext_compat_desc *d = data;
2171 int i;
2172
2173 if (len < sizeof(*d) ||
2174 d->bFirstInterfaceNumber >= ffs->interfaces_count ||
Jim Lin53642392016-05-20 18:13:19 +08002175 !d->Reserved1)
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002176 return -EINVAL;
2177 for (i = 0; i < ARRAY_SIZE(d->Reserved2); ++i)
2178 if (d->Reserved2[i])
2179 return -EINVAL;
2180
2181 length = sizeof(struct usb_ext_compat_desc);
2182 }
2183 break;
2184 case FFS_OS_DESC_EXT_PROP: {
2185 struct usb_ext_prop_desc *d = data;
2186 u32 type, pdl;
2187 u16 pnl;
2188
2189 if (len < sizeof(*d) || h->interface >= ffs->interfaces_count)
2190 return -EINVAL;
2191 length = le32_to_cpu(d->dwSize);
2192 type = le32_to_cpu(d->dwPropertyDataType);
2193 if (type < USB_EXT_PROP_UNICODE ||
2194 type > USB_EXT_PROP_UNICODE_MULTI) {
2195 pr_vdebug("unsupported os descriptor property type: %d",
2196 type);
2197 return -EINVAL;
2198 }
2199 pnl = le16_to_cpu(d->wPropertyNameLength);
2200 pdl = le32_to_cpu(*(u32 *)((u8 *)data + 10 + pnl));
2201 if (length != 14 + pnl + pdl) {
2202 pr_vdebug("invalid os descriptor length: %d pnl:%d pdl:%d (descriptor %d)\n",
2203 length, pnl, pdl, type);
2204 return -EINVAL;
2205 }
2206 ++ffs->ms_os_descs_ext_prop_count;
2207 /* property name reported to the host as "WCHAR"s */
2208 ffs->ms_os_descs_ext_prop_name_len += pnl * 2;
2209 ffs->ms_os_descs_ext_prop_data_len += pdl;
2210 }
2211 break;
2212 default:
2213 pr_vdebug("unknown descriptor: %d\n", type);
2214 return -EINVAL;
2215 }
2216 return length;
2217}
2218
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002219static int __ffs_data_got_descs(struct ffs_data *ffs,
2220 char *const _data, size_t len)
2221{
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302222 char *data = _data, *raw_descs;
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002223 unsigned os_descs_count = 0, counts[3], flags;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302224 int ret = -EINVAL, i;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002225 struct ffs_desc_helper helper;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002226
2227 ENTER();
2228
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302229 if (get_unaligned_le32(data + 4) != len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002230 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002231
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302232 switch (get_unaligned_le32(data)) {
2233 case FUNCTIONFS_DESCRIPTORS_MAGIC:
2234 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302235 data += 8;
2236 len -= 8;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302237 break;
2238 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
2239 flags = get_unaligned_le32(data + 8);
Robert Baldyga1b0bf882014-09-09 08:23:17 +02002240 ffs->user_flags = flags;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302241 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
2242 FUNCTIONFS_HAS_HS_DESC |
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002243 FUNCTIONFS_HAS_SS_DESC |
Robert Baldyga1b0bf882014-09-09 08:23:17 +02002244 FUNCTIONFS_HAS_MS_OS_DESC |
Robert Baldyga5e33f6f2015-01-23 13:41:01 +01002245 FUNCTIONFS_VIRTUAL_ADDR |
2246 FUNCTIONFS_EVENTFD)) {
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302247 ret = -ENOSYS;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002248 goto error;
2249 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302250 data += 12;
2251 len -= 12;
2252 break;
2253 default:
2254 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002255 }
2256
Robert Baldyga5e33f6f2015-01-23 13:41:01 +01002257 if (flags & FUNCTIONFS_EVENTFD) {
2258 if (len < 4)
2259 goto error;
2260 ffs->ffs_eventfd =
2261 eventfd_ctx_fdget((int)get_unaligned_le32(data));
2262 if (IS_ERR(ffs->ffs_eventfd)) {
2263 ret = PTR_ERR(ffs->ffs_eventfd);
2264 ffs->ffs_eventfd = NULL;
2265 goto error;
2266 }
2267 data += 4;
2268 len -= 4;
2269 }
2270
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302271 /* Read fs_count, hs_count and ss_count (if present) */
2272 for (i = 0; i < 3; ++i) {
2273 if (!(flags & (1 << i))) {
2274 counts[i] = 0;
2275 } else if (len < 4) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002276 goto error;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302277 } else {
2278 counts[i] = get_unaligned_le32(data);
2279 data += 4;
2280 len -= 4;
2281 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002282 }
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002283 if (flags & (1 << i)) {
2284 os_descs_count = get_unaligned_le32(data);
2285 data += 4;
2286 len -= 4;
2287 };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002288
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302289 /* Read descriptors */
2290 raw_descs = data;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002291 helper.ffs = ffs;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302292 for (i = 0; i < 3; ++i) {
2293 if (!counts[i])
2294 continue;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002295 helper.interfaces_count = 0;
2296 helper.eps_count = 0;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302297 ret = ffs_do_descs(counts[i], data, len,
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002298 __ffs_data_do_entity, &helper);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302299 if (ret < 0)
2300 goto error;
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002301 if (!ffs->eps_count && !ffs->interfaces_count) {
2302 ffs->eps_count = helper.eps_count;
2303 ffs->interfaces_count = helper.interfaces_count;
2304 } else {
2305 if (ffs->eps_count != helper.eps_count) {
2306 ret = -EINVAL;
2307 goto error;
2308 }
2309 if (ffs->interfaces_count != helper.interfaces_count) {
2310 ret = -EINVAL;
2311 goto error;
2312 }
2313 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302314 data += ret;
2315 len -= ret;
2316 }
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002317 if (os_descs_count) {
2318 ret = ffs_do_os_descs(os_descs_count, data, len,
2319 __ffs_data_do_os_desc, ffs);
2320 if (ret < 0)
2321 goto error;
2322 data += ret;
2323 len -= ret;
2324 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002325
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302326 if (raw_descs == data || len) {
2327 ret = -EINVAL;
2328 goto error;
2329 }
2330
2331 ffs->raw_descs_data = _data;
2332 ffs->raw_descs = raw_descs;
2333 ffs->raw_descs_length = data - raw_descs;
2334 ffs->fs_descs_count = counts[0];
2335 ffs->hs_descs_count = counts[1];
2336 ffs->ss_descs_count = counts[2];
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002337 ffs->ms_os_descs_count = os_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002338
2339 return 0;
2340
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002341error:
2342 kfree(_data);
2343 return ret;
2344}
2345
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002346static int __ffs_data_got_strings(struct ffs_data *ffs,
2347 char *const _data, size_t len)
2348{
2349 u32 str_count, needed_count, lang_count;
2350 struct usb_gadget_strings **stringtabs, *t;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002351 const char *data = _data;
Michal Nazarewicz872ce512016-05-31 14:17:21 +02002352 struct usb_string *s;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002353
2354 ENTER();
2355
2356 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
2357 get_unaligned_le32(data + 4) != len))
2358 goto error;
2359 str_count = get_unaligned_le32(data + 8);
2360 lang_count = get_unaligned_le32(data + 12);
2361
2362 /* if one is zero the other must be zero */
2363 if (unlikely(!str_count != !lang_count))
2364 goto error;
2365
2366 /* Do we have at least as many strings as descriptors need? */
2367 needed_count = ffs->strings_count;
2368 if (unlikely(str_count < needed_count))
2369 goto error;
2370
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002371 /*
2372 * If we don't need any strings just return and free all
2373 * memory.
2374 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002375 if (!needed_count) {
2376 kfree(_data);
2377 return 0;
2378 }
2379
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002380 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002381 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002382 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002383 vla_group(d);
2384 vla_item(d, struct usb_gadget_strings *, stringtabs,
2385 lang_count + 1);
2386 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2387 vla_item(d, struct usb_string, strings,
2388 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002389
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002390 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2391
2392 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002393 kfree(_data);
2394 return -ENOMEM;
2395 }
2396
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002397 /* Initialize the VLA pointers */
2398 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2399 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002400 i = lang_count;
2401 do {
2402 *stringtabs++ = t++;
2403 } while (--i);
2404 *stringtabs = NULL;
2405
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002406 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2407 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2408 t = vla_ptr(vlabuf, d, stringtab);
2409 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002410 }
2411
2412 /* For each language */
2413 data += 16;
2414 len -= 16;
2415
2416 do { /* lang_count > 0 so we can use do-while */
2417 unsigned needed = needed_count;
2418
2419 if (unlikely(len < 3))
2420 goto error_free;
2421 t->language = get_unaligned_le16(data);
2422 t->strings = s;
2423 ++t;
2424
2425 data += 2;
2426 len -= 2;
2427
2428 /* For each string */
2429 do { /* str_count > 0 so we can use do-while */
2430 size_t length = strnlen(data, len);
2431
2432 if (unlikely(length == len))
2433 goto error_free;
2434
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002435 /*
2436 * User may provide more strings then we need,
2437 * if that's the case we simply ignore the
2438 * rest
2439 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002440 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002441 /*
2442 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002443 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002444 * now just leave garbage here.
2445 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002446 s->s = data;
2447 --needed;
2448 ++s;
2449 }
2450
2451 data += length + 1;
2452 len -= length + 1;
2453 } while (--str_count);
2454
2455 s->id = 0; /* terminator */
2456 s->s = NULL;
2457 ++s;
2458
2459 } while (--lang_count);
2460
2461 /* Some garbage left? */
2462 if (unlikely(len))
2463 goto error_free;
2464
2465 /* Done! */
2466 ffs->stringtabs = stringtabs;
2467 ffs->raw_strings = _data;
2468
2469 return 0;
2470
2471error_free:
2472 kfree(stringtabs);
2473error:
2474 kfree(_data);
2475 return -EINVAL;
2476}
2477
2478
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002479/* Events handling and management *******************************************/
2480
2481static void __ffs_event_add(struct ffs_data *ffs,
2482 enum usb_functionfs_event_type type)
2483{
2484 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2485 int neg = 0;
2486
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002487 /*
2488 * Abort any unhandled setup
2489 *
2490 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002491 * of ffs->setup_state without holding the lock because when
2492 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002493 * the source does nothing.
2494 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002495 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01002496 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002497
Michal Nazarewicz67913bb2014-09-10 17:50:24 +02002498 /*
2499 * Logic of this function guarantees that there are at most four pending
2500 * evens on ffs->ev.types queue. This is important because the queue
2501 * has space for four elements only and __ffs_ep0_read_events function
2502 * depends on that limit as well. If more event types are added, those
2503 * limits have to be revisited or guaranteed to still hold.
2504 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002505 switch (type) {
2506 case FUNCTIONFS_RESUME:
2507 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002508 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002509 case FUNCTIONFS_SUSPEND:
2510 case FUNCTIONFS_SETUP:
2511 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002512 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002513 break;
2514
2515 case FUNCTIONFS_BIND:
2516 case FUNCTIONFS_UNBIND:
2517 case FUNCTIONFS_DISABLE:
2518 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002519 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002520 rem_type1 = FUNCTIONFS_SUSPEND;
2521 rem_type2 = FUNCTIONFS_RESUME;
2522 neg = 1;
2523 break;
2524
2525 default:
Michal Nazarewiczfe00bcb2014-09-11 18:52:49 +02002526 WARN(1, "%d: unknown event, this should not happen\n", type);
2527 return;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002528 }
2529
2530 {
2531 u8 *ev = ffs->ev.types, *out = ev;
2532 unsigned n = ffs->ev.count;
2533 for (; n; --n, ++ev)
2534 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2535 *out++ = *ev;
2536 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002537 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002538 ffs->ev.count = out - ffs->ev.types;
2539 }
2540
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002541 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002542 ffs->ev.types[ffs->ev.count++] = type;
2543 wake_up_locked(&ffs->ev.waitq);
Robert Baldyga5e33f6f2015-01-23 13:41:01 +01002544 if (ffs->ffs_eventfd)
2545 eventfd_signal(ffs->ffs_eventfd, 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002546}
2547
2548static void ffs_event_add(struct ffs_data *ffs,
2549 enum usb_functionfs_event_type type)
2550{
2551 unsigned long flags;
2552 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2553 __ffs_event_add(ffs, type);
2554 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2555}
2556
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002557/* Bind/unbind USB function hooks *******************************************/
2558
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002559static int ffs_ep_addr2idx(struct ffs_data *ffs, u8 endpoint_address)
2560{
2561 int i;
2562
2563 for (i = 1; i < ARRAY_SIZE(ffs->eps_addrmap); ++i)
2564 if (ffs->eps_addrmap[i] == endpoint_address)
2565 return i;
2566 return -ENOENT;
2567}
2568
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002569static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2570 struct usb_descriptor_header *desc,
2571 void *priv)
2572{
2573 struct usb_endpoint_descriptor *ds = (void *)desc;
2574 struct ffs_function *func = priv;
2575 struct ffs_ep *ffs_ep;
Dan Carpenter85b06f52014-09-09 15:06:09 +03002576 unsigned ep_desc_id;
2577 int idx;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302578 static const char *speed_names[] = { "full", "high", "super" };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002579
2580 if (type != FFS_DESCRIPTOR)
2581 return 0;
2582
Manu Gautam8d4e8972014-02-28 16:50:22 +05302583 /*
2584 * If ss_descriptors is not NULL, we are reading super speed
2585 * descriptors; if hs_descriptors is not NULL, we are reading high
2586 * speed descriptors; otherwise, we are reading full speed
2587 * descriptors.
2588 */
2589 if (func->function.ss_descriptors) {
2590 ep_desc_id = 2;
2591 func->function.ss_descriptors[(long)valuep] = desc;
2592 } else if (func->function.hs_descriptors) {
2593 ep_desc_id = 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002594 func->function.hs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302595 } else {
2596 ep_desc_id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002597 func->function.fs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302598 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002599
2600 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2601 return 0;
2602
Robert Baldyga6d5c1c72014-08-25 11:16:27 +02002603 idx = ffs_ep_addr2idx(func->ffs, ds->bEndpointAddress) - 1;
2604 if (idx < 0)
2605 return idx;
2606
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002607 ffs_ep = func->eps + idx;
2608
Manu Gautam8d4e8972014-02-28 16:50:22 +05302609 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2610 pr_err("two %sspeed descriptors for EP %d\n",
2611 speed_names[ep_desc_id],
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002612 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002613 return -EINVAL;
2614 }
Manu Gautam8d4e8972014-02-28 16:50:22 +05302615 ffs_ep->descs[ep_desc_id] = ds;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002616
2617 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2618 if (ffs_ep->ep) {
2619 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2620 if (!ds->wMaxPacketSize)
2621 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2622 } else {
2623 struct usb_request *req;
2624 struct usb_ep *ep;
Robert Baldyga1b0bf882014-09-09 08:23:17 +02002625 u8 bEndpointAddress;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002626
Robert Baldyga1b0bf882014-09-09 08:23:17 +02002627 /*
2628 * We back up bEndpointAddress because autoconfig overwrites
2629 * it with physical endpoint address.
2630 */
2631 bEndpointAddress = ds->bEndpointAddress;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002632 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002633 ep = usb_ep_autoconfig(func->gadget, ds);
2634 if (unlikely(!ep))
2635 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08002636 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002637
2638 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2639 if (unlikely(!req))
2640 return -ENOMEM;
2641
2642 ffs_ep->ep = ep;
2643 ffs_ep->req = req;
2644 func->eps_revmap[ds->bEndpointAddress &
2645 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
Robert Baldyga1b0bf882014-09-09 08:23:17 +02002646 /*
2647 * If we use virtual address mapping, we restore
2648 * original bEndpointAddress value.
2649 */
2650 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
2651 ds->bEndpointAddress = bEndpointAddress;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002652 }
2653 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2654
2655 return 0;
2656}
2657
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002658static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2659 struct usb_descriptor_header *desc,
2660 void *priv)
2661{
2662 struct ffs_function *func = priv;
2663 unsigned idx;
2664 u8 newValue;
2665
2666 switch (type) {
2667 default:
2668 case FFS_DESCRIPTOR:
2669 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2670 return 0;
2671
2672 case FFS_INTERFACE:
2673 idx = *valuep;
2674 if (func->interfaces_nums[idx] < 0) {
2675 int id = usb_interface_id(func->conf, &func->function);
2676 if (unlikely(id < 0))
2677 return id;
2678 func->interfaces_nums[idx] = id;
2679 }
2680 newValue = func->interfaces_nums[idx];
2681 break;
2682
2683 case FFS_STRING:
2684 /* String' IDs are allocated when fsf_data is bound to cdev */
2685 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2686 break;
2687
2688 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002689 /*
2690 * USB_DT_ENDPOINT are handled in
2691 * __ffs_func_bind_do_descs().
2692 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002693 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2694 return 0;
2695
2696 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2697 if (unlikely(!func->eps[idx].ep))
2698 return -EINVAL;
2699
2700 {
2701 struct usb_endpoint_descriptor **descs;
2702 descs = func->eps[idx].descs;
2703 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2704 }
2705 break;
2706 }
2707
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002708 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002709 *valuep = newValue;
2710 return 0;
2711}
2712
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002713static int __ffs_func_bind_do_os_desc(enum ffs_os_desc_type type,
2714 struct usb_os_desc_header *h, void *data,
2715 unsigned len, void *priv)
2716{
2717 struct ffs_function *func = priv;
2718 u8 length = 0;
2719
2720 switch (type) {
2721 case FFS_OS_DESC_EXT_COMPAT: {
2722 struct usb_ext_compat_desc *desc = data;
2723 struct usb_os_desc_table *t;
2724
2725 t = &func->function.os_desc_table[desc->bFirstInterfaceNumber];
2726 t->if_id = func->interfaces_nums[desc->bFirstInterfaceNumber];
2727 memcpy(t->os_desc->ext_compat_id, &desc->CompatibleID,
2728 ARRAY_SIZE(desc->CompatibleID) +
2729 ARRAY_SIZE(desc->SubCompatibleID));
2730 length = sizeof(*desc);
2731 }
2732 break;
2733 case FFS_OS_DESC_EXT_PROP: {
2734 struct usb_ext_prop_desc *desc = data;
2735 struct usb_os_desc_table *t;
2736 struct usb_os_desc_ext_prop *ext_prop;
2737 char *ext_prop_name;
2738 char *ext_prop_data;
2739
2740 t = &func->function.os_desc_table[h->interface];
2741 t->if_id = func->interfaces_nums[h->interface];
2742
2743 ext_prop = func->ffs->ms_os_descs_ext_prop_avail;
2744 func->ffs->ms_os_descs_ext_prop_avail += sizeof(*ext_prop);
2745
2746 ext_prop->type = le32_to_cpu(desc->dwPropertyDataType);
2747 ext_prop->name_len = le16_to_cpu(desc->wPropertyNameLength);
2748 ext_prop->data_len = le32_to_cpu(*(u32 *)
2749 usb_ext_prop_data_len_ptr(data, ext_prop->name_len));
2750 length = ext_prop->name_len + ext_prop->data_len + 14;
2751
2752 ext_prop_name = func->ffs->ms_os_descs_ext_prop_name_avail;
2753 func->ffs->ms_os_descs_ext_prop_name_avail +=
2754 ext_prop->name_len;
2755
2756 ext_prop_data = func->ffs->ms_os_descs_ext_prop_data_avail;
2757 func->ffs->ms_os_descs_ext_prop_data_avail +=
2758 ext_prop->data_len;
2759 memcpy(ext_prop_data,
2760 usb_ext_prop_data_ptr(data, ext_prop->name_len),
2761 ext_prop->data_len);
2762 /* unicode data reported to the host as "WCHAR"s */
2763 switch (ext_prop->type) {
2764 case USB_EXT_PROP_UNICODE:
2765 case USB_EXT_PROP_UNICODE_ENV:
2766 case USB_EXT_PROP_UNICODE_LINK:
2767 case USB_EXT_PROP_UNICODE_MULTI:
2768 ext_prop->data_len *= 2;
2769 break;
2770 }
2771 ext_prop->data = ext_prop_data;
2772
2773 memcpy(ext_prop_name, usb_ext_prop_name_ptr(data),
2774 ext_prop->name_len);
2775 /* property name reported to the host as "WCHAR"s */
2776 ext_prop->name_len *= 2;
2777 ext_prop->name = ext_prop_name;
2778
2779 t->os_desc->ext_prop_len +=
2780 ext_prop->name_len + ext_prop->data_len + 14;
2781 ++t->os_desc->ext_prop_count;
2782 list_add_tail(&ext_prop->entry, &t->os_desc->ext_prop);
2783 }
2784 break;
2785 default:
2786 pr_vdebug("unknown descriptor: %d\n", type);
2787 }
2788
2789 return length;
2790}
2791
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002792static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2793 struct usb_configuration *c)
2794{
2795 struct ffs_function *func = ffs_func_from_usb(f);
2796 struct f_fs_opts *ffs_opts =
2797 container_of(f->fi, struct f_fs_opts, func_inst);
2798 int ret;
2799
2800 ENTER();
2801
2802 /*
2803 * Legacy gadget triggers binding in functionfs_ready_callback,
2804 * which already uses locking; taking the same lock here would
2805 * cause a deadlock.
2806 *
2807 * Configfs-enabled gadgets however do need ffs_dev_lock.
2808 */
2809 if (!ffs_opts->no_configfs)
2810 ffs_dev_lock();
2811 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2812 func->ffs = ffs_opts->dev->ffs_data;
2813 if (!ffs_opts->no_configfs)
2814 ffs_dev_unlock();
2815 if (ret)
2816 return ERR_PTR(ret);
2817
2818 func->conf = c;
2819 func->gadget = c->cdev->gadget;
2820
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002821 /*
2822 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2823 * configurations are bound in sequence with list_for_each_entry,
2824 * in each configuration its functions are bound in sequence
2825 * with list_for_each_entry, so we assume no race condition
2826 * with regard to ffs_opts->bound access
2827 */
2828 if (!ffs_opts->refcnt) {
2829 ret = functionfs_bind(func->ffs, c->cdev);
2830 if (ret)
2831 return ERR_PTR(ret);
2832 }
2833 ffs_opts->refcnt++;
2834 func->function.strings = func->ffs->stringtabs;
2835
2836 return ffs_opts;
2837}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002838
2839static int _ffs_func_bind(struct usb_configuration *c,
2840 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002841{
2842 struct ffs_function *func = ffs_func_from_usb(f);
2843 struct ffs_data *ffs = func->ffs;
2844
2845 const int full = !!func->ffs->fs_descs_count;
2846 const int high = gadget_is_dualspeed(func->gadget) &&
2847 func->ffs->hs_descs_count;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302848 const int super = gadget_is_superspeed(func->gadget) &&
2849 func->ffs->ss_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002850
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002851 int fs_len, hs_len, ss_len, ret, i;
Dan Carpenter0015f912016-05-28 07:48:10 +03002852 struct ffs_ep *eps_ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002853
2854 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002855 vla_group(d);
2856 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2857 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2858 full ? ffs->fs_descs_count + 1 : 0);
2859 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2860 high ? ffs->hs_descs_count + 1 : 0);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302861 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2862 super ? ffs->ss_descs_count + 1 : 0);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002863 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002864 vla_item_with_sz(d, struct usb_os_desc_table, os_desc_table,
2865 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2866 vla_item_with_sz(d, char[16], ext_compat,
2867 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2868 vla_item_with_sz(d, struct usb_os_desc, os_desc,
2869 c->cdev->use_os_string ? ffs->interfaces_count : 0);
2870 vla_item_with_sz(d, struct usb_os_desc_ext_prop, ext_prop,
2871 ffs->ms_os_descs_ext_prop_count);
2872 vla_item_with_sz(d, char, ext_prop_name,
2873 ffs->ms_os_descs_ext_prop_name_len);
2874 vla_item_with_sz(d, char, ext_prop_data,
2875 ffs->ms_os_descs_ext_prop_data_len);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302876 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002877 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002878
2879 ENTER();
2880
Manu Gautam8d4e8972014-02-28 16:50:22 +05302881 /* Has descriptors only for speeds gadget does not support */
2882 if (unlikely(!(full | high | super)))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002883 return -ENOTSUPP;
2884
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002885 /* Allocate a single chunk, less management later on */
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002886 vlabuf = kzalloc(vla_group_size(d), GFP_KERNEL);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002887 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002888 return -ENOMEM;
2889
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002890 ffs->ms_os_descs_ext_prop_avail = vla_ptr(vlabuf, d, ext_prop);
2891 ffs->ms_os_descs_ext_prop_name_avail =
2892 vla_ptr(vlabuf, d, ext_prop_name);
2893 ffs->ms_os_descs_ext_prop_data_avail =
2894 vla_ptr(vlabuf, d, ext_prop_data);
2895
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302896 /* Copy descriptors */
2897 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2898 ffs->raw_descs_length);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302899
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002900 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
Dan Carpenter0015f912016-05-28 07:48:10 +03002901 eps_ptr = vla_ptr(vlabuf, d, eps);
2902 for (i = 0; i < ffs->eps_count; i++)
2903 eps_ptr[i].num = -1;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002904
2905 /* Save pointers
2906 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2907 */
2908 func->eps = vla_ptr(vlabuf, d, eps);
2909 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002910
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002911 /*
2912 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002913 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002914 * numbers without worrying that it may be described later on.
2915 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002916 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002917 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302918 fs_len = ffs_do_descs(ffs->fs_descs_count,
2919 vla_ptr(vlabuf, d, raw_descs),
2920 d_raw_descs__sz,
2921 __ffs_func_bind_do_descs, func);
2922 if (unlikely(fs_len < 0)) {
2923 ret = fs_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002924 goto error;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302925 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002926 } else {
Manu Gautam8d4e8972014-02-28 16:50:22 +05302927 fs_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002928 }
2929
2930 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002931 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302932 hs_len = ffs_do_descs(ffs->hs_descs_count,
2933 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2934 d_raw_descs__sz - fs_len,
2935 __ffs_func_bind_do_descs, func);
2936 if (unlikely(hs_len < 0)) {
2937 ret = hs_len;
2938 goto error;
2939 }
2940 } else {
2941 hs_len = 0;
2942 }
2943
2944 if (likely(super)) {
2945 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002946 ss_len = ffs_do_descs(ffs->ss_descs_count,
Manu Gautam8d4e8972014-02-28 16:50:22 +05302947 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2948 d_raw_descs__sz - fs_len - hs_len,
2949 __ffs_func_bind_do_descs, func);
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002950 if (unlikely(ss_len < 0)) {
2951 ret = ss_len;
Robert Baldyga88548942013-09-27 12:28:54 +02002952 goto error;
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002953 }
2954 } else {
2955 ss_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002956 }
2957
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002958 /*
2959 * Now handle interface numbers allocation and interface and
2960 * endpoint numbers rewriting. We can do that in one go
2961 * now.
2962 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002963 ret = ffs_do_descs(ffs->fs_descs_count +
Manu Gautam8d4e8972014-02-28 16:50:22 +05302964 (high ? ffs->hs_descs_count : 0) +
2965 (super ? ffs->ss_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002966 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002967 __ffs_func_bind_do_nums, func);
2968 if (unlikely(ret < 0))
2969 goto error;
2970
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002971 func->function.os_desc_table = vla_ptr(vlabuf, d, os_desc_table);
Jim Linc6010c82016-05-13 20:32:16 +08002972 if (c->cdev->use_os_string) {
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002973 for (i = 0; i < ffs->interfaces_count; ++i) {
2974 struct usb_os_desc *desc;
2975
2976 desc = func->function.os_desc_table[i].os_desc =
2977 vla_ptr(vlabuf, d, os_desc) +
2978 i * sizeof(struct usb_os_desc);
2979 desc->ext_compat_id =
2980 vla_ptr(vlabuf, d, ext_compat) + i * 16;
2981 INIT_LIST_HEAD(&desc->ext_prop);
2982 }
Jim Linc6010c82016-05-13 20:32:16 +08002983 ret = ffs_do_os_descs(ffs->ms_os_descs_count,
2984 vla_ptr(vlabuf, d, raw_descs) +
2985 fs_len + hs_len + ss_len,
2986 d_raw_descs__sz - fs_len - hs_len -
2987 ss_len,
2988 __ffs_func_bind_do_os_desc, func);
2989 if (unlikely(ret < 0))
2990 goto error;
2991 }
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +02002992 func->function.os_desc_n =
2993 c->cdev->use_os_string ? ffs->interfaces_count : 0;
2994
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002995 /* And we're done */
2996 ffs_event_add(ffs, FUNCTIONFS_BIND);
2997 return 0;
2998
2999error:
3000 /* XXX Do we need to release all claimed endpoints here? */
3001 return ret;
3002}
3003
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003004static int ffs_func_bind(struct usb_configuration *c,
3005 struct usb_function *f)
3006{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003007 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
Robert Baldyga55d81122015-07-13 11:03:50 +02003008 struct ffs_function *func = ffs_func_from_usb(f);
3009 int ret;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003010
3011 if (IS_ERR(ffs_opts))
3012 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003013
Robert Baldyga55d81122015-07-13 11:03:50 +02003014 ret = _ffs_func_bind(c, f);
3015 if (ret && !--ffs_opts->refcnt)
3016 functionfs_unbind(func->ffs);
3017
3018 return ret;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003019}
3020
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003021
3022/* Other USB function hooks *************************************************/
3023
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01003024static void ffs_reset_work(struct work_struct *work)
3025{
3026 struct ffs_data *ffs = container_of(work,
3027 struct ffs_data, reset_work);
3028 ffs_data_reset(ffs);
3029}
3030
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003031static int ffs_func_set_alt(struct usb_function *f,
3032 unsigned interface, unsigned alt)
3033{
3034 struct ffs_function *func = ffs_func_from_usb(f);
3035 struct ffs_data *ffs = func->ffs;
3036 int ret = 0, intf;
3037
3038 if (alt != (unsigned)-1) {
3039 intf = ffs_func_revmap_intf(func, interface);
3040 if (unlikely(intf < 0))
3041 return intf;
3042 }
3043
3044 if (ffs->func)
3045 ffs_func_eps_disable(ffs->func);
3046
Robert Baldyga18d6b32f2014-12-18 09:55:10 +01003047 if (ffs->state == FFS_DEACTIVATED) {
3048 ffs->state = FFS_CLOSING;
3049 INIT_WORK(&ffs->reset_work, ffs_reset_work);
3050 schedule_work(&ffs->reset_work);
3051 return -ENODEV;
3052 }
3053
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003054 if (ffs->state != FFS_ACTIVE)
3055 return -ENODEV;
3056
3057 if (alt == (unsigned)-1) {
3058 ffs->func = NULL;
3059 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
3060 return 0;
3061 }
3062
3063 ffs->func = func;
3064 ret = ffs_func_eps_enable(func);
3065 if (likely(ret >= 0))
3066 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
3067 return ret;
3068}
3069
3070static void ffs_func_disable(struct usb_function *f)
3071{
3072 ffs_func_set_alt(f, 0, (unsigned)-1);
3073}
3074
3075static int ffs_func_setup(struct usb_function *f,
3076 const struct usb_ctrlrequest *creq)
3077{
3078 struct ffs_function *func = ffs_func_from_usb(f);
3079 struct ffs_data *ffs = func->ffs;
3080 unsigned long flags;
3081 int ret;
3082
3083 ENTER();
3084
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01003085 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
3086 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
3087 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
3088 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
3089 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003090
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01003091 /*
3092 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003093 * (notable exceptions are set/get interface) so we need to
3094 * handle them. All other either handled by composite or
3095 * passed to usb_configuration->setup() (if one is set). No
3096 * matter, we will handle requests directed to endpoint here
3097 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01003098 * other request?
3099 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003100 if (ffs->state != FFS_ACTIVE)
3101 return -ENODEV;
3102
3103 switch (creq->bRequestType & USB_RECIP_MASK) {
3104 case USB_RECIP_INTERFACE:
3105 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
3106 if (unlikely(ret < 0))
3107 return ret;
3108 break;
3109
3110 case USB_RECIP_ENDPOINT:
3111 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
3112 if (unlikely(ret < 0))
3113 return ret;
Robert Baldyga1b0bf882014-09-09 08:23:17 +02003114 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
3115 ret = func->ffs->eps_addrmap[ret];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003116 break;
3117
3118 default:
3119 return -EOPNOTSUPP;
3120 }
3121
3122 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
3123 ffs->ev.setup = *creq;
3124 ffs->ev.setup.wIndex = cpu_to_le16(ret);
3125 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
3126 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
3127
3128 return 0;
3129}
3130
3131static void ffs_func_suspend(struct usb_function *f)
3132{
3133 ENTER();
3134 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
3135}
3136
3137static void ffs_func_resume(struct usb_function *f)
3138{
3139 ENTER();
3140 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
3141}
3142
3143
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01003144/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003145
3146static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
3147{
3148 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
3149 return num ? num : -EDOM;
3150}
3151
3152static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
3153{
3154 short *nums = func->interfaces_nums;
3155 unsigned count = func->ffs->interfaces_count;
3156
3157 for (; count; --count, ++nums) {
3158 if (*nums >= 0 && *nums == intf)
3159 return nums - func->interfaces_nums;
3160 }
3161
3162 return -EDOM;
3163}
3164
3165
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003166/* Devices management *******************************************************/
3167
3168static LIST_HEAD(ffs_devices);
3169
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003170static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003171{
3172 struct ffs_dev *dev;
3173
3174 list_for_each_entry(dev, &ffs_devices, entry) {
3175 if (!dev->name || !name)
3176 continue;
3177 if (strcmp(dev->name, name) == 0)
3178 return dev;
3179 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003180
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003181 return NULL;
3182}
3183
3184/*
3185 * ffs_lock must be taken by the caller of this function
3186 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003187static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003188{
3189 struct ffs_dev *dev;
3190
3191 if (list_is_singular(&ffs_devices)) {
3192 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
3193 if (dev->single)
3194 return dev;
3195 }
3196
3197 return NULL;
3198}
3199
3200/*
3201 * ffs_lock must be taken by the caller of this function
3202 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003203static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003204{
3205 struct ffs_dev *dev;
3206
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003207 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003208 if (dev)
3209 return dev;
3210
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003211 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003212}
3213
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003214/* Configfs support *********************************************************/
3215
3216static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
3217{
3218 return container_of(to_config_group(item), struct f_fs_opts,
3219 func_inst.group);
3220}
3221
3222static void ffs_attr_release(struct config_item *item)
3223{
3224 struct f_fs_opts *opts = to_ffs_opts(item);
3225
3226 usb_put_function_instance(&opts->func_inst);
3227}
3228
3229static struct configfs_item_operations ffs_item_ops = {
3230 .release = ffs_attr_release,
3231};
3232
3233static struct config_item_type ffs_func_type = {
3234 .ct_item_ops = &ffs_item_ops,
3235 .ct_owner = THIS_MODULE,
3236};
3237
3238
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003239/* Function registration interface ******************************************/
3240
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003241static void ffs_free_inst(struct usb_function_instance *f)
3242{
3243 struct f_fs_opts *opts;
3244
3245 opts = to_f_fs_opts(f);
3246 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003247 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003248 ffs_dev_unlock();
3249 kfree(opts);
3250}
3251
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003252#define MAX_INST_NAME_LEN 40
3253
3254static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
3255{
3256 struct f_fs_opts *opts;
3257 char *ptr;
3258 const char *tmp;
3259 int name_len, ret;
3260
3261 name_len = strlen(name) + 1;
3262 if (name_len > MAX_INST_NAME_LEN)
3263 return -ENAMETOOLONG;
3264
3265 ptr = kstrndup(name, name_len, GFP_KERNEL);
3266 if (!ptr)
3267 return -ENOMEM;
3268
3269 opts = to_f_fs_opts(fi);
3270 tmp = NULL;
3271
3272 ffs_dev_lock();
3273
3274 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
3275 ret = _ffs_name_dev(opts->dev, ptr);
3276 if (ret) {
3277 kfree(ptr);
3278 ffs_dev_unlock();
3279 return ret;
3280 }
3281 opts->dev->name_allocated = true;
3282
3283 ffs_dev_unlock();
3284
3285 kfree(tmp);
3286
3287 return 0;
3288}
3289
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003290static struct usb_function_instance *ffs_alloc_inst(void)
3291{
3292 struct f_fs_opts *opts;
3293 struct ffs_dev *dev;
3294
3295 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3296 if (!opts)
3297 return ERR_PTR(-ENOMEM);
3298
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003299 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003300 opts->func_inst.free_func_inst = ffs_free_inst;
3301 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003302 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003303 ffs_dev_unlock();
3304 if (IS_ERR(dev)) {
3305 kfree(opts);
3306 return ERR_CAST(dev);
3307 }
3308 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003309 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003310
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003311 config_group_init_type_name(&opts->func_inst.group, "",
3312 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003313 return &opts->func_inst;
3314}
3315
3316static void ffs_free(struct usb_function *f)
3317{
3318 kfree(ffs_func_from_usb(f));
3319}
3320
3321static void ffs_func_unbind(struct usb_configuration *c,
3322 struct usb_function *f)
3323{
3324 struct ffs_function *func = ffs_func_from_usb(f);
3325 struct ffs_data *ffs = func->ffs;
3326 struct f_fs_opts *opts =
3327 container_of(f->fi, struct f_fs_opts, func_inst);
3328 struct ffs_ep *ep = func->eps;
3329 unsigned count = ffs->eps_count;
3330 unsigned long flags;
3331
3332 ENTER();
3333 if (ffs->func == func) {
3334 ffs_func_eps_disable(func);
3335 ffs->func = NULL;
3336 }
3337
3338 if (!--opts->refcnt)
3339 functionfs_unbind(ffs);
3340
3341 /* cleanup after autoconfig */
3342 spin_lock_irqsave(&func->ffs->eps_lock, flags);
3343 do {
3344 if (ep->ep && ep->req)
3345 usb_ep_free_request(ep->ep, ep->req);
3346 ep->req = NULL;
3347 ++ep;
3348 } while (--count);
3349 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
3350 kfree(func->eps);
3351 func->eps = NULL;
3352 /*
3353 * eps, descriptors and interfaces_nums are allocated in the
3354 * same chunk so only one free is required.
3355 */
3356 func->function.fs_descriptors = NULL;
3357 func->function.hs_descriptors = NULL;
Manu Gautam8d4e8972014-02-28 16:50:22 +05303358 func->function.ss_descriptors = NULL;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003359 func->interfaces_nums = NULL;
3360
3361 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
3362}
3363
3364static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
3365{
3366 struct ffs_function *func;
3367
3368 ENTER();
3369
3370 func = kzalloc(sizeof(*func), GFP_KERNEL);
3371 if (unlikely(!func))
3372 return ERR_PTR(-ENOMEM);
3373
3374 func->function.name = "Function FS Gadget";
3375
3376 func->function.bind = ffs_func_bind;
3377 func->function.unbind = ffs_func_unbind;
3378 func->function.set_alt = ffs_func_set_alt;
3379 func->function.disable = ffs_func_disable;
3380 func->function.setup = ffs_func_setup;
3381 func->function.suspend = ffs_func_suspend;
3382 func->function.resume = ffs_func_resume;
3383 func->function.free_func = ffs_free;
3384
3385 return &func->function;
3386}
3387
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003388/*
3389 * ffs_lock must be taken by the caller of this function
3390 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003391static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003392{
3393 struct ffs_dev *dev;
3394 int ret;
3395
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003396 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003397 return ERR_PTR(-EBUSY);
3398
3399 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3400 if (!dev)
3401 return ERR_PTR(-ENOMEM);
3402
3403 if (list_empty(&ffs_devices)) {
3404 ret = functionfs_init();
3405 if (ret) {
3406 kfree(dev);
3407 return ERR_PTR(ret);
3408 }
3409 }
3410
3411 list_add(&dev->entry, &ffs_devices);
3412
3413 return dev;
3414}
3415
3416/*
3417 * ffs_lock must be taken by the caller of this function
3418 * The caller is responsible for "name" being available whenever f_fs needs it
3419 */
3420static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
3421{
3422 struct ffs_dev *existing;
3423
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003424 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003425 if (existing)
3426 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01003427
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003428 dev->name = name;
3429
3430 return 0;
3431}
3432
3433/*
3434 * The caller is responsible for "name" being available whenever f_fs needs it
3435 */
3436int ffs_name_dev(struct ffs_dev *dev, const char *name)
3437{
3438 int ret;
3439
3440 ffs_dev_lock();
3441 ret = _ffs_name_dev(dev, name);
3442 ffs_dev_unlock();
3443
3444 return ret;
3445}
Felipe Balbi0700faa2014-04-01 13:19:32 -05003446EXPORT_SYMBOL_GPL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003447
3448int ffs_single_dev(struct ffs_dev *dev)
3449{
3450 int ret;
3451
3452 ret = 0;
3453 ffs_dev_lock();
3454
3455 if (!list_is_singular(&ffs_devices))
3456 ret = -EBUSY;
3457 else
3458 dev->single = true;
3459
3460 ffs_dev_unlock();
3461 return ret;
3462}
Felipe Balbi0700faa2014-04-01 13:19:32 -05003463EXPORT_SYMBOL_GPL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003464
3465/*
3466 * ffs_lock must be taken by the caller of this function
3467 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003468static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003469{
3470 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003471 if (dev->name_allocated)
3472 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003473 kfree(dev);
3474 if (list_empty(&ffs_devices))
3475 functionfs_cleanup();
3476}
3477
3478static void *ffs_acquire_dev(const char *dev_name)
3479{
3480 struct ffs_dev *ffs_dev;
3481
3482 ENTER();
3483 ffs_dev_lock();
3484
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01003485 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003486 if (!ffs_dev)
Krzysztof Opasiakd668b4f2014-05-21 14:05:35 +02003487 ffs_dev = ERR_PTR(-ENOENT);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003488 else if (ffs_dev->mounted)
3489 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003490 else if (ffs_dev->ffs_acquire_dev_callback &&
3491 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
Krzysztof Opasiakd668b4f2014-05-21 14:05:35 +02003492 ffs_dev = ERR_PTR(-ENOENT);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003493 else
3494 ffs_dev->mounted = true;
3495
3496 ffs_dev_unlock();
3497 return ffs_dev;
3498}
3499
3500static void ffs_release_dev(struct ffs_data *ffs_data)
3501{
3502 struct ffs_dev *ffs_dev;
3503
3504 ENTER();
3505 ffs_dev_lock();
3506
3507 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01003508 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003509 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01003510
3511 if (ffs_dev->ffs_release_dev_callback)
3512 ffs_dev->ffs_release_dev_callback(ffs_dev);
3513 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003514
3515 ffs_dev_unlock();
3516}
3517
3518static int ffs_ready(struct ffs_data *ffs)
3519{
3520 struct ffs_dev *ffs_obj;
3521 int ret = 0;
3522
3523 ENTER();
3524 ffs_dev_lock();
3525
3526 ffs_obj = ffs->private_data;
3527 if (!ffs_obj) {
3528 ret = -EINVAL;
3529 goto done;
3530 }
3531 if (WARN_ON(ffs_obj->desc_ready)) {
3532 ret = -EBUSY;
3533 goto done;
3534 }
3535
3536 ffs_obj->desc_ready = true;
3537 ffs_obj->ffs_data = ffs;
3538
Krzysztof Opasiak49a79d82015-05-22 17:25:18 +02003539 if (ffs_obj->ffs_ready_callback) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003540 ret = ffs_obj->ffs_ready_callback(ffs);
Krzysztof Opasiak49a79d82015-05-22 17:25:18 +02003541 if (ret)
3542 goto done;
3543 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003544
Krzysztof Opasiak49a79d82015-05-22 17:25:18 +02003545 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003546done:
3547 ffs_dev_unlock();
3548 return ret;
3549}
3550
3551static void ffs_closed(struct ffs_data *ffs)
3552{
3553 struct ffs_dev *ffs_obj;
Rui Miguel Silvaf14e9ad2015-05-20 14:52:40 +01003554 struct f_fs_opts *opts;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003555
3556 ENTER();
3557 ffs_dev_lock();
3558
3559 ffs_obj = ffs->private_data;
3560 if (!ffs_obj)
3561 goto done;
3562
3563 ffs_obj->desc_ready = false;
3564
Krzysztof Opasiak49a79d82015-05-22 17:25:18 +02003565 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags) &&
3566 ffs_obj->ffs_closed_callback)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003567 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003568
Rui Miguel Silvaf14e9ad2015-05-20 14:52:40 +01003569 if (ffs_obj->opts)
3570 opts = ffs_obj->opts;
3571 else
3572 goto done;
3573
3574 if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
3575 || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003576 goto done;
3577
3578 unregister_gadget_item(ffs_obj->opts->
3579 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003580done:
3581 ffs_dev_unlock();
3582}
3583
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003584/* Misc helper functions ****************************************************/
3585
3586static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3587{
3588 return nonblock
3589 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3590 : mutex_lock_interruptible(mutex);
3591}
3592
Al Viro260ef312012-09-26 21:43:45 -04003593static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003594{
3595 char *data;
3596
3597 if (unlikely(!len))
3598 return NULL;
3599
3600 data = kmalloc(len, GFP_KERNEL);
3601 if (unlikely(!data))
3602 return ERR_PTR(-ENOMEM);
3603
Daniel Walter7fe9a932015-11-18 17:15:49 +01003604 if (unlikely(copy_from_user(data, buf, len))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003605 kfree(data);
3606 return ERR_PTR(-EFAULT);
3607 }
3608
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01003609 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003610 ffs_dump_mem("", data, len);
3611
3612 return data;
3613}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003614
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003615DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3616MODULE_LICENSE("GPL");
3617MODULE_AUTHOR("Michal Nazarewicz");