blob: 2e164dca08e89fc29ea1887f0afe7b2a09e1a5af [file] [log] [blame]
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002 * f_fs.c -- user mode file system API for USB composite function controllers
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003 *
4 * Copyright (C) 2010 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01005 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02006 *
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01007 * Based on inode.c (GadgetFS) which was:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02008 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020015 */
16
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/blkdev.h>
Randy Dunlapb0608692010-05-10 10:51:36 -070022#include <linux/pagemap.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040023#include <linux/export.h>
Koen Beel560f1182012-05-30 20:43:37 +020024#include <linux/hid.h>
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010025#include <linux/module.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020026#include <asm/unaligned.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020027
28#include <linux/usb/composite.h>
29#include <linux/usb/functionfs.h>
30
Robert Baldyga2e4c7552014-02-10 10:42:44 +010031#include <linux/aio.h>
32#include <linux/mmu_context.h>
Robert Baldyga23de91e2014-02-10 10:42:43 +010033#include <linux/poll.h>
34
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010035#include "u_fs.h"
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010036#include "configfs.h"
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020037
38#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
39
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +010040/* Variable Length Array Macros **********************************************/
41#define vla_group(groupname) size_t groupname##__next = 0
42#define vla_group_size(groupname) groupname##__next
43
44#define vla_item(groupname, type, name, n) \
45 size_t groupname##_##name##__offset = ({ \
46 size_t align_mask = __alignof__(type) - 1; \
47 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
48 size_t size = (n) * sizeof(type); \
49 groupname##__next = offset + size; \
50 offset; \
51 })
52
53#define vla_item_with_sz(groupname, type, name, n) \
54 size_t groupname##_##name##__sz = (n) * sizeof(type); \
55 size_t groupname##_##name##__offset = ({ \
56 size_t align_mask = __alignof__(type) - 1; \
57 size_t offset = (groupname##__next + align_mask) & ~align_mask;\
58 size_t size = groupname##_##name##__sz; \
59 groupname##__next = offset + size; \
60 offset; \
61 })
62
63#define vla_ptr(ptr, groupname, name) \
64 ((void *) ((char *)ptr + groupname##_##name##__offset))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020065
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020066/* Reference counter handling */
67static void ffs_data_get(struct ffs_data *ffs);
68static void ffs_data_put(struct ffs_data *ffs);
69/* Creates new ffs_data object. */
70static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
71
72/* Opened counter handling. */
73static void ffs_data_opened(struct ffs_data *ffs);
74static void ffs_data_closed(struct ffs_data *ffs);
75
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010076/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020077static int __must_check
78__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
79static int __must_check
80__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
81
82
83/* The function structure ***************************************************/
84
85struct ffs_ep;
86
87struct ffs_function {
88 struct usb_configuration *conf;
89 struct usb_gadget *gadget;
90 struct ffs_data *ffs;
91
92 struct ffs_ep *eps;
93 u8 eps_revmap[16];
94 short *interfaces_nums;
95
96 struct usb_function function;
97};
98
99
100static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
101{
102 return container_of(f, struct ffs_function, function);
103}
104
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200105
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100106static inline enum ffs_setup_state
107ffs_setup_state_clear_cancelled(struct ffs_data *ffs)
108{
109 return (enum ffs_setup_state)
110 cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP);
111}
112
113
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200114static void ffs_func_eps_disable(struct ffs_function *func);
115static int __must_check ffs_func_eps_enable(struct ffs_function *func);
116
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200117static int ffs_func_bind(struct usb_configuration *,
118 struct usb_function *);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200119static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
120static void ffs_func_disable(struct usb_function *);
121static int ffs_func_setup(struct usb_function *,
122 const struct usb_ctrlrequest *);
123static void ffs_func_suspend(struct usb_function *);
124static void ffs_func_resume(struct usb_function *);
125
126
127static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
128static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
129
130
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200131/* The endpoints structures *************************************************/
132
133struct ffs_ep {
134 struct usb_ep *ep; /* P: ffs->eps_lock */
135 struct usb_request *req; /* P: epfile->mutex */
136
Manu Gautam8d4e8972014-02-28 16:50:22 +0530137 /* [0]: full speed, [1]: high speed, [2]: super speed */
138 struct usb_endpoint_descriptor *descs[3];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200139
140 u8 num;
141
142 int status; /* P: epfile->mutex */
143};
144
145struct ffs_epfile {
146 /* Protects ep->ep and ep->req. */
147 struct mutex mutex;
148 wait_queue_head_t wait;
149
150 struct ffs_data *ffs;
151 struct ffs_ep *ep; /* P: ffs->eps_lock */
152
153 struct dentry *dentry;
154
155 char name[5];
156
157 unsigned char in; /* P: ffs->eps_lock */
158 unsigned char isoc; /* P: ffs->eps_lock */
159
160 unsigned char _pad;
161};
162
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100163/* ffs_io_data structure ***************************************************/
164
165struct ffs_io_data {
166 bool aio;
167 bool read;
168
169 struct kiocb *kiocb;
170 const struct iovec *iovec;
171 unsigned long nr_segs;
172 char __user *buf;
173 size_t len;
174
175 struct mm_struct *mm;
176 struct work_struct work;
177
178 struct usb_ep *ep;
179 struct usb_request *req;
180};
181
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200182static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
183static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
184
185static struct inode *__must_check
186ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
187 const struct file_operations *fops,
188 struct dentry **dentry_p);
189
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100190/* Devices management *******************************************************/
191
192DEFINE_MUTEX(ffs_lock);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100193EXPORT_SYMBOL(ffs_lock);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100194
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100195static struct ffs_dev *_ffs_find_dev(const char *name);
196static struct ffs_dev *_ffs_alloc_dev(void);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +0100197static int _ffs_name_dev(struct ffs_dev *dev, const char *name);
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +0100198static void _ffs_free_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100199static void *ffs_acquire_dev(const char *dev_name);
200static void ffs_release_dev(struct ffs_data *ffs_data);
201static int ffs_ready(struct ffs_data *ffs);
202static void ffs_closed(struct ffs_data *ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200203
204/* Misc helper functions ****************************************************/
205
206static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
207 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400208static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200209 __attribute__((warn_unused_result, nonnull));
210
211
212/* Control file aka ep0 *****************************************************/
213
214static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
215{
216 struct ffs_data *ffs = req->context;
217
218 complete_all(&ffs->ep0req_completion);
219}
220
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200221static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
222{
223 struct usb_request *req = ffs->ep0req;
224 int ret;
225
226 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
227
228 spin_unlock_irq(&ffs->ev.waitq.lock);
229
230 req->buf = data;
231 req->length = len;
232
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100233 /*
234 * UDC layer requires to provide a buffer even for ZLP, but should
235 * not use it at all. Let's provide some poisoned pointer to catch
236 * possible bug in the driver.
237 */
238 if (req->buf == NULL)
239 req->buf = (void *)0xDEADBABE;
240
Wolfram Sang16735d02013-11-14 14:32:02 -0800241 reinit_completion(&ffs->ep0req_completion);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200242
243 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
244 if (unlikely(ret < 0))
245 return ret;
246
247 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
248 if (unlikely(ret)) {
249 usb_ep_dequeue(ffs->gadget->ep0, req);
250 return -EINTR;
251 }
252
253 ffs->setup_state = FFS_NO_SETUP;
Robert Baldyga0a7b1f82014-02-10 10:42:42 +0100254 return req->status ? req->status : req->actual;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200255}
256
257static int __ffs_ep0_stall(struct ffs_data *ffs)
258{
259 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100260 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200261 usb_ep_set_halt(ffs->gadget->ep0);
262 ffs->setup_state = FFS_NO_SETUP;
263 return -EL2HLT;
264 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100265 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200266 return -ESRCH;
267 }
268}
269
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200270static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
271 size_t len, loff_t *ptr)
272{
273 struct ffs_data *ffs = file->private_data;
274 ssize_t ret;
275 char *data;
276
277 ENTER();
278
279 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100280 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200281 return -EIDRM;
282
283 /* Acquire mutex */
284 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
285 if (unlikely(ret < 0))
286 return ret;
287
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200288 /* Check state */
289 switch (ffs->state) {
290 case FFS_READ_DESCRIPTORS:
291 case FFS_READ_STRINGS:
292 /* Copy data */
293 if (unlikely(len < 16)) {
294 ret = -EINVAL;
295 break;
296 }
297
298 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100299 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200300 ret = PTR_ERR(data);
301 break;
302 }
303
304 /* Handle data */
305 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100306 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200307 ret = __ffs_data_got_descs(ffs, data, len);
308 if (unlikely(ret < 0))
309 break;
310
311 ffs->state = FFS_READ_STRINGS;
312 ret = len;
313 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100314 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200315 ret = __ffs_data_got_strings(ffs, data, len);
316 if (unlikely(ret < 0))
317 break;
318
319 ret = ffs_epfiles_create(ffs);
320 if (unlikely(ret)) {
321 ffs->state = FFS_CLOSING;
322 break;
323 }
324
325 ffs->state = FFS_ACTIVE;
326 mutex_unlock(&ffs->mutex);
327
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100328 ret = ffs_ready(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200329 if (unlikely(ret < 0)) {
330 ffs->state = FFS_CLOSING;
331 return ret;
332 }
333
334 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
335 return len;
336 }
337 break;
338
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200339 case FFS_ACTIVE:
340 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100341 /*
342 * We're called from user space, we can use _irq
343 * rather then _irqsave
344 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200345 spin_lock_irq(&ffs->ev.waitq.lock);
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100346 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100347 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200348 ret = -EIDRM;
349 goto done_spin;
350
351 case FFS_NO_SETUP:
352 ret = -ESRCH;
353 goto done_spin;
354
355 case FFS_SETUP_PENDING:
356 break;
357 }
358
359 /* FFS_SETUP_PENDING */
360 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
361 spin_unlock_irq(&ffs->ev.waitq.lock);
362 ret = __ffs_ep0_stall(ffs);
363 break;
364 }
365
366 /* FFS_SETUP_PENDING and not stall */
367 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
368
369 spin_unlock_irq(&ffs->ev.waitq.lock);
370
371 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100372 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200373 ret = PTR_ERR(data);
374 break;
375 }
376
377 spin_lock_irq(&ffs->ev.waitq.lock);
378
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100379 /*
380 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200381 * but the state of setup could have changed from
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100382 * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200383 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100384 * from user space in vain but it's unlikely.
385 *
386 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200387 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
388 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100389 * mutex.
390 */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100391 if (ffs_setup_state_clear_cancelled(ffs) ==
392 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200393 ret = -EIDRM;
394done_spin:
395 spin_unlock_irq(&ffs->ev.waitq.lock);
396 } else {
397 /* unlocks spinlock */
398 ret = __ffs_ep0_queue_wait(ffs, data, len);
399 }
400 kfree(data);
401 break;
402
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200403 default:
404 ret = -EBADFD;
405 break;
406 }
407
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200408 mutex_unlock(&ffs->mutex);
409 return ret;
410}
411
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200412static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
413 size_t n)
414{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100415 /*
416 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
417 * to release them.
418 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200419 struct usb_functionfs_event events[n];
420 unsigned i = 0;
421
422 memset(events, 0, sizeof events);
423
424 do {
425 events[i].type = ffs->ev.types[i];
426 if (events[i].type == FUNCTIONFS_SETUP) {
427 events[i].u.setup = ffs->ev.setup;
428 ffs->setup_state = FFS_SETUP_PENDING;
429 }
430 } while (++i < n);
431
432 if (n < ffs->ev.count) {
433 ffs->ev.count -= n;
434 memmove(ffs->ev.types, ffs->ev.types + n,
435 ffs->ev.count * sizeof *ffs->ev.types);
436 } else {
437 ffs->ev.count = 0;
438 }
439
440 spin_unlock_irq(&ffs->ev.waitq.lock);
441 mutex_unlock(&ffs->mutex);
442
443 return unlikely(__copy_to_user(buf, events, sizeof events))
444 ? -EFAULT : sizeof events;
445}
446
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200447static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
448 size_t len, loff_t *ptr)
449{
450 struct ffs_data *ffs = file->private_data;
451 char *data = NULL;
452 size_t n;
453 int ret;
454
455 ENTER();
456
457 /* Fast check if setup was canceled */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100458 if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200459 return -EIDRM;
460
461 /* Acquire mutex */
462 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
463 if (unlikely(ret < 0))
464 return ret;
465
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200466 /* Check state */
467 if (ffs->state != FFS_ACTIVE) {
468 ret = -EBADFD;
469 goto done_mutex;
470 }
471
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100472 /*
473 * We're called from user space, we can use _irq rather then
474 * _irqsave
475 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200476 spin_lock_irq(&ffs->ev.waitq.lock);
477
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100478 switch (ffs_setup_state_clear_cancelled(ffs)) {
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100479 case FFS_SETUP_CANCELLED:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200480 ret = -EIDRM;
481 break;
482
483 case FFS_NO_SETUP:
484 n = len / sizeof(struct usb_functionfs_event);
485 if (unlikely(!n)) {
486 ret = -EINVAL;
487 break;
488 }
489
490 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
491 ret = -EAGAIN;
492 break;
493 }
494
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100495 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
496 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200497 ret = -EINTR;
498 break;
499 }
500
501 return __ffs_ep0_read_events(ffs, buf,
502 min(n, (size_t)ffs->ev.count));
503
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200504 case FFS_SETUP_PENDING:
505 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
506 spin_unlock_irq(&ffs->ev.waitq.lock);
507 ret = __ffs_ep0_stall(ffs);
508 goto done_mutex;
509 }
510
511 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
512
513 spin_unlock_irq(&ffs->ev.waitq.lock);
514
515 if (likely(len)) {
516 data = kmalloc(len, GFP_KERNEL);
517 if (unlikely(!data)) {
518 ret = -ENOMEM;
519 goto done_mutex;
520 }
521 }
522
523 spin_lock_irq(&ffs->ev.waitq.lock);
524
525 /* See ffs_ep0_write() */
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100526 if (ffs_setup_state_clear_cancelled(ffs) ==
527 FFS_SETUP_CANCELLED) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200528 ret = -EIDRM;
529 break;
530 }
531
532 /* unlocks spinlock */
533 ret = __ffs_ep0_queue_wait(ffs, data, len);
534 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
535 ret = -EFAULT;
536 goto done_mutex;
537
538 default:
539 ret = -EBADFD;
540 break;
541 }
542
543 spin_unlock_irq(&ffs->ev.waitq.lock);
544done_mutex:
545 mutex_unlock(&ffs->mutex);
546 kfree(data);
547 return ret;
548}
549
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200550static int ffs_ep0_open(struct inode *inode, struct file *file)
551{
552 struct ffs_data *ffs = inode->i_private;
553
554 ENTER();
555
556 if (unlikely(ffs->state == FFS_CLOSING))
557 return -EBUSY;
558
559 file->private_data = ffs;
560 ffs_data_opened(ffs);
561
562 return 0;
563}
564
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200565static int ffs_ep0_release(struct inode *inode, struct file *file)
566{
567 struct ffs_data *ffs = file->private_data;
568
569 ENTER();
570
571 ffs_data_closed(ffs);
572
573 return 0;
574}
575
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200576static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
577{
578 struct ffs_data *ffs = file->private_data;
579 struct usb_gadget *gadget = ffs->gadget;
580 long ret;
581
582 ENTER();
583
584 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
585 struct ffs_function *func = ffs->func;
586 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200587 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200588 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200589 } else {
590 ret = -ENOTTY;
591 }
592
593 return ret;
594}
595
Robert Baldyga23de91e2014-02-10 10:42:43 +0100596static unsigned int ffs_ep0_poll(struct file *file, poll_table *wait)
597{
598 struct ffs_data *ffs = file->private_data;
599 unsigned int mask = POLLWRNORM;
600 int ret;
601
602 poll_wait(file, &ffs->ev.waitq, wait);
603
604 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
605 if (unlikely(ret < 0))
606 return mask;
607
608 switch (ffs->state) {
609 case FFS_READ_DESCRIPTORS:
610 case FFS_READ_STRINGS:
611 mask |= POLLOUT;
612 break;
613
614 case FFS_ACTIVE:
615 switch (ffs->setup_state) {
616 case FFS_NO_SETUP:
617 if (ffs->ev.count)
618 mask |= POLLIN;
619 break;
620
621 case FFS_SETUP_PENDING:
622 case FFS_SETUP_CANCELLED:
623 mask |= (POLLIN | POLLOUT);
624 break;
625 }
626 case FFS_CLOSING:
627 break;
628 }
629
630 mutex_unlock(&ffs->mutex);
631
632 return mask;
633}
634
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200635static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200636 .llseek = no_llseek,
637
638 .open = ffs_ep0_open,
639 .write = ffs_ep0_write,
640 .read = ffs_ep0_read,
641 .release = ffs_ep0_release,
642 .unlocked_ioctl = ffs_ep0_ioctl,
Robert Baldyga23de91e2014-02-10 10:42:43 +0100643 .poll = ffs_ep0_poll,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200644};
645
646
647/* "Normal" endpoints operations ********************************************/
648
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200649static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
650{
651 ENTER();
652 if (likely(req->context)) {
653 struct ffs_ep *ep = _ep->driver_data;
654 ep->status = req->status ? req->status : req->actual;
655 complete(req->context);
656 }
657}
658
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100659static void ffs_user_copy_worker(struct work_struct *work)
660{
661 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
662 work);
663 int ret = io_data->req->status ? io_data->req->status :
664 io_data->req->actual;
665
666 if (io_data->read && ret > 0) {
667 int i;
668 size_t pos = 0;
669 use_mm(io_data->mm);
670 for (i = 0; i < io_data->nr_segs; i++) {
671 if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
672 &io_data->buf[pos],
673 io_data->iovec[i].iov_len))) {
674 ret = -EFAULT;
675 break;
676 }
677 pos += io_data->iovec[i].iov_len;
678 }
679 unuse_mm(io_data->mm);
680 }
681
682 aio_complete(io_data->kiocb, ret, ret);
683
684 usb_ep_free_request(io_data->ep, io_data->req);
685
686 io_data->kiocb->private = NULL;
687 if (io_data->read)
688 kfree(io_data->iovec);
689 kfree(io_data->buf);
690 kfree(io_data);
691}
692
693static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
694 struct usb_request *req)
695{
696 struct ffs_io_data *io_data = req->context;
697
698 ENTER();
699
700 INIT_WORK(&io_data->work, ffs_user_copy_worker);
701 schedule_work(&io_data->work);
702}
703
704static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200705{
706 struct ffs_epfile *epfile = file->private_data;
707 struct ffs_ep *ep;
708 char *data = NULL;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800709 ssize_t ret, data_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200710 int halt;
711
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800712 /* Are we still active? */
713 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
714 ret = -ENODEV;
715 goto error;
716 }
717
718 /* Wait for endpoint to be enabled */
719 ep = epfile->ep;
720 if (!ep) {
721 if (file->f_flags & O_NONBLOCK) {
722 ret = -EAGAIN;
723 goto error;
724 }
725
726 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
727 if (ret) {
728 ret = -EINTR;
729 goto error;
730 }
731 }
732
733 /* Do we halt? */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100734 halt = (!io_data->read == !epfile->in);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800735 if (halt && epfile->isoc) {
736 ret = -EINVAL;
737 goto error;
738 }
739
740 /* Allocate & copy */
741 if (!halt) {
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800742 /*
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100743 * if we _do_ wait above, the epfile->ffs->gadget might be NULL
744 * before the waiting completes, so do not assign to 'gadget' earlier
745 */
746 struct usb_gadget *gadget = epfile->ffs->gadget;
747
748 /*
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800749 * Controller may require buffer size to be aligned to
750 * maxpacketsize of an out endpoint.
751 */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100752 data_len = io_data->read ?
753 usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
754 io_data->len;
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800755
756 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800757 if (unlikely(!data))
758 return -ENOMEM;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100759 if (io_data->aio && !io_data->read) {
760 int i;
761 size_t pos = 0;
762 for (i = 0; i < io_data->nr_segs; i++) {
763 if (unlikely(copy_from_user(&data[pos],
764 io_data->iovec[i].iov_base,
765 io_data->iovec[i].iov_len))) {
766 ret = -EFAULT;
767 goto error;
768 }
769 pos += io_data->iovec[i].iov_len;
770 }
771 } else {
772 if (!io_data->read &&
773 unlikely(__copy_from_user(data, io_data->buf,
774 io_data->len))) {
775 ret = -EFAULT;
776 goto error;
777 }
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800778 }
779 }
780
781 /* We will be using request */
782 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
783 if (unlikely(ret))
784 goto error;
785
786 spin_lock_irq(&epfile->ffs->eps_lock);
787
788 if (epfile->ep != ep) {
789 /* In the meantime, endpoint got disabled or changed. */
790 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200791 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800792 } else if (halt) {
793 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200794 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
795 usb_ep_set_halt(ep->ep);
796 spin_unlock_irq(&epfile->ffs->eps_lock);
797 ret = -EBADMSG;
798 } else {
799 /* Fire the request */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100800 struct usb_request *req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200801
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100802 if (io_data->aio) {
803 req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
804 if (unlikely(!req))
Robert Baldyga48968f82014-03-10 09:33:37 +0100805 goto error_lock;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200806
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100807 req->buf = data;
808 req->length = io_data->len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200809
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100810 io_data->buf = data;
811 io_data->ep = ep->ep;
812 io_data->req = req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200813
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100814 req->context = io_data;
815 req->complete = ffs_epfile_async_io_complete;
816
817 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
818 if (unlikely(ret)) {
819 usb_ep_free_request(ep->ep, req);
Robert Baldyga48968f82014-03-10 09:33:37 +0100820 goto error_lock;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100821 }
822 ret = -EIOCBQUEUED;
823
824 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200825 } else {
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100826 DECLARE_COMPLETION_ONSTACK(done);
827
828 req = ep->req;
829 req->buf = data;
830 req->length = io_data->len;
831
832 req->context = &done;
833 req->complete = ffs_epfile_io_complete;
834
835 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
836
837 spin_unlock_irq(&epfile->ffs->eps_lock);
838
839 if (unlikely(ret < 0)) {
840 /* nop */
841 } else if (unlikely(
842 wait_for_completion_interruptible(&done))) {
843 ret = -EINTR;
844 usb_ep_dequeue(ep->ep, req);
845 } else {
Chuansheng Liucfe919b2014-03-04 15:34:57 +0800846 /*
847 * XXX We may end up silently droping data
848 * here. Since data_len (i.e. req->length) may
849 * be bigger than len (after being rounded up
850 * to maxpacketsize), we may end up with more
851 * data then user space has space for.
852 */
853 ret = ep->status;
854 if (io_data->read && ret > 0) {
855 ret = min_t(size_t, ret, io_data->len);
856
857 if (unlikely(copy_to_user(io_data->buf,
858 data, ret)))
859 ret = -EFAULT;
860 }
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100861 }
862 kfree(data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200863 }
864 }
865
866 mutex_unlock(&epfile->mutex);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100867 return ret;
Robert Baldyga48968f82014-03-10 09:33:37 +0100868
869error_lock:
870 spin_unlock_irq(&epfile->ffs->eps_lock);
871 mutex_unlock(&epfile->mutex);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200872error:
873 kfree(data);
874 return ret;
875}
876
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200877static ssize_t
878ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
879 loff_t *ptr)
880{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100881 struct ffs_io_data io_data;
882
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200883 ENTER();
884
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100885 io_data.aio = false;
886 io_data.read = false;
887 io_data.buf = (char * __user)buf;
888 io_data.len = len;
889
890 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200891}
892
893static ssize_t
894ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
895{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100896 struct ffs_io_data io_data;
897
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200898 ENTER();
899
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100900 io_data.aio = false;
901 io_data.read = true;
902 io_data.buf = buf;
903 io_data.len = len;
904
905 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200906}
907
908static int
909ffs_epfile_open(struct inode *inode, struct file *file)
910{
911 struct ffs_epfile *epfile = inode->i_private;
912
913 ENTER();
914
915 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
916 return -ENODEV;
917
918 file->private_data = epfile;
919 ffs_data_opened(epfile->ffs);
920
921 return 0;
922}
923
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100924static int ffs_aio_cancel(struct kiocb *kiocb)
925{
926 struct ffs_io_data *io_data = kiocb->private;
927 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
928 int value;
929
930 ENTER();
931
932 spin_lock_irq(&epfile->ffs->eps_lock);
933
934 if (likely(io_data && io_data->ep && io_data->req))
935 value = usb_ep_dequeue(io_data->ep, io_data->req);
936 else
937 value = -EINVAL;
938
939 spin_unlock_irq(&epfile->ffs->eps_lock);
940
941 return value;
942}
943
944static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
945 const struct iovec *iovec,
946 unsigned long nr_segs, loff_t loff)
947{
948 struct ffs_io_data *io_data;
949
950 ENTER();
951
952 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
953 if (unlikely(!io_data))
954 return -ENOMEM;
955
956 io_data->aio = true;
957 io_data->read = false;
958 io_data->kiocb = kiocb;
959 io_data->iovec = iovec;
960 io_data->nr_segs = nr_segs;
961 io_data->len = kiocb->ki_nbytes;
962 io_data->mm = current->mm;
963
964 kiocb->private = io_data;
965
966 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
967
968 return ffs_epfile_io(kiocb->ki_filp, io_data);
969}
970
971static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
972 const struct iovec *iovec,
973 unsigned long nr_segs, loff_t loff)
974{
975 struct ffs_io_data *io_data;
976 struct iovec *iovec_copy;
977
978 ENTER();
979
980 iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
981 if (unlikely(!iovec_copy))
982 return -ENOMEM;
983
984 memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
985
986 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
987 if (unlikely(!io_data)) {
988 kfree(iovec_copy);
989 return -ENOMEM;
990 }
991
992 io_data->aio = true;
993 io_data->read = true;
994 io_data->kiocb = kiocb;
995 io_data->iovec = iovec_copy;
996 io_data->nr_segs = nr_segs;
997 io_data->len = kiocb->ki_nbytes;
998 io_data->mm = current->mm;
999
1000 kiocb->private = io_data;
1001
1002 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1003
1004 return ffs_epfile_io(kiocb->ki_filp, io_data);
1005}
1006
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001007static int
1008ffs_epfile_release(struct inode *inode, struct file *file)
1009{
1010 struct ffs_epfile *epfile = inode->i_private;
1011
1012 ENTER();
1013
1014 ffs_data_closed(epfile->ffs);
1015
1016 return 0;
1017}
1018
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001019static long ffs_epfile_ioctl(struct file *file, unsigned code,
1020 unsigned long value)
1021{
1022 struct ffs_epfile *epfile = file->private_data;
1023 int ret;
1024
1025 ENTER();
1026
1027 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1028 return -ENODEV;
1029
1030 spin_lock_irq(&epfile->ffs->eps_lock);
1031 if (likely(epfile->ep)) {
1032 switch (code) {
1033 case FUNCTIONFS_FIFO_STATUS:
1034 ret = usb_ep_fifo_status(epfile->ep->ep);
1035 break;
1036 case FUNCTIONFS_FIFO_FLUSH:
1037 usb_ep_fifo_flush(epfile->ep->ep);
1038 ret = 0;
1039 break;
1040 case FUNCTIONFS_CLEAR_HALT:
1041 ret = usb_ep_clear_halt(epfile->ep->ep);
1042 break;
1043 case FUNCTIONFS_ENDPOINT_REVMAP:
1044 ret = epfile->ep->num;
1045 break;
1046 default:
1047 ret = -ENOTTY;
1048 }
1049 } else {
1050 ret = -ENODEV;
1051 }
1052 spin_unlock_irq(&epfile->ffs->eps_lock);
1053
1054 return ret;
1055}
1056
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001057static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001058 .llseek = no_llseek,
1059
1060 .open = ffs_epfile_open,
1061 .write = ffs_epfile_write,
1062 .read = ffs_epfile_read,
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001063 .aio_write = ffs_epfile_aio_write,
1064 .aio_read = ffs_epfile_aio_read,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001065 .release = ffs_epfile_release,
1066 .unlocked_ioctl = ffs_epfile_ioctl,
1067};
1068
1069
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001070/* File system and super block operations ***********************************/
1071
1072/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001073 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001074 * function configuration then later for event monitoring.
1075 */
1076
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001077static struct inode *__must_check
1078ffs_sb_make_inode(struct super_block *sb, void *data,
1079 const struct file_operations *fops,
1080 const struct inode_operations *iops,
1081 struct ffs_file_perms *perms)
1082{
1083 struct inode *inode;
1084
1085 ENTER();
1086
1087 inode = new_inode(sb);
1088
1089 if (likely(inode)) {
1090 struct timespec current_time = CURRENT_TIME;
1091
Al Viro12ba8d12010-10-27 04:19:36 +01001092 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001093 inode->i_mode = perms->mode;
1094 inode->i_uid = perms->uid;
1095 inode->i_gid = perms->gid;
1096 inode->i_atime = current_time;
1097 inode->i_mtime = current_time;
1098 inode->i_ctime = current_time;
1099 inode->i_private = data;
1100 if (fops)
1101 inode->i_fop = fops;
1102 if (iops)
1103 inode->i_op = iops;
1104 }
1105
1106 return inode;
1107}
1108
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001109/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001110static struct inode *ffs_sb_create_file(struct super_block *sb,
1111 const char *name, void *data,
1112 const struct file_operations *fops,
1113 struct dentry **dentry_p)
1114{
1115 struct ffs_data *ffs = sb->s_fs_info;
1116 struct dentry *dentry;
1117 struct inode *inode;
1118
1119 ENTER();
1120
1121 dentry = d_alloc_name(sb->s_root, name);
1122 if (unlikely(!dentry))
1123 return NULL;
1124
1125 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1126 if (unlikely(!inode)) {
1127 dput(dentry);
1128 return NULL;
1129 }
1130
1131 d_add(dentry, inode);
1132 if (dentry_p)
1133 *dentry_p = dentry;
1134
1135 return inode;
1136}
1137
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001138/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001139static const struct super_operations ffs_sb_operations = {
1140 .statfs = simple_statfs,
1141 .drop_inode = generic_delete_inode,
1142};
1143
1144struct ffs_sb_fill_data {
1145 struct ffs_file_perms perms;
1146 umode_t root_mode;
1147 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +01001148 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001149};
1150
1151static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1152{
1153 struct ffs_sb_fill_data *data = _data;
1154 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +01001155 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001156
1157 ENTER();
1158
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001159 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +01001160 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001161 sb->s_fs_info = ffs;
1162 sb->s_blocksize = PAGE_CACHE_SIZE;
1163 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1164 sb->s_magic = FUNCTIONFS_MAGIC;
1165 sb->s_op = &ffs_sb_operations;
1166 sb->s_time_gran = 1;
1167
1168 /* Root inode */
1169 data->perms.mode = data->root_mode;
1170 inode = ffs_sb_make_inode(sb, NULL,
1171 &simple_dir_operations,
1172 &simple_dir_inode_operations,
1173 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001174 sb->s_root = d_make_root(inode);
1175 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +01001176 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001177
1178 /* EP0 file */
1179 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1180 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +01001181 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001182
1183 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001184}
1185
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001186static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1187{
1188 ENTER();
1189
1190 if (!opts || !*opts)
1191 return 0;
1192
1193 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001194 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001195 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001196
1197 /* Option limit */
1198 comma = strchr(opts, ',');
1199 if (comma)
1200 *comma = 0;
1201
1202 /* Value limit */
1203 eq = strchr(opts, '=');
1204 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001205 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001206 return -EINVAL;
1207 }
1208 *eq = 0;
1209
1210 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001211 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001212 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001213 return -EINVAL;
1214 }
1215
1216 /* Interpret option */
1217 switch (eq - opts) {
1218 case 5:
1219 if (!memcmp(opts, "rmode", 5))
1220 data->root_mode = (value & 0555) | S_IFDIR;
1221 else if (!memcmp(opts, "fmode", 5))
1222 data->perms.mode = (value & 0666) | S_IFREG;
1223 else
1224 goto invalid;
1225 break;
1226
1227 case 4:
1228 if (!memcmp(opts, "mode", 4)) {
1229 data->root_mode = (value & 0555) | S_IFDIR;
1230 data->perms.mode = (value & 0666) | S_IFREG;
1231 } else {
1232 goto invalid;
1233 }
1234 break;
1235
1236 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001237 if (!memcmp(opts, "uid", 3)) {
1238 data->perms.uid = make_kuid(current_user_ns(), value);
1239 if (!uid_valid(data->perms.uid)) {
1240 pr_err("%s: unmapped value: %lu\n", opts, value);
1241 return -EINVAL;
1242 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001243 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001244 data->perms.gid = make_kgid(current_user_ns(), value);
1245 if (!gid_valid(data->perms.gid)) {
1246 pr_err("%s: unmapped value: %lu\n", opts, value);
1247 return -EINVAL;
1248 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001249 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001250 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -08001251 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001252 break;
1253
1254 default:
1255invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001256 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001257 return -EINVAL;
1258 }
1259
1260 /* Next iteration */
1261 if (!comma)
1262 break;
1263 opts = comma + 1;
1264 }
1265
1266 return 0;
1267}
1268
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001269/* "mount -t functionfs dev_name /dev/function" ends up here */
1270
Al Virofc14f2f2010-07-25 01:48:30 +04001271static struct dentry *
1272ffs_fs_mount(struct file_system_type *t, int flags,
1273 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001274{
1275 struct ffs_sb_fill_data data = {
1276 .perms = {
1277 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001278 .uid = GLOBAL_ROOT_UID,
1279 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001280 },
1281 .root_mode = S_IFDIR | 0500,
1282 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001283 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001284 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001285 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001286 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001287
1288 ENTER();
1289
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001290 ret = ffs_fs_parse_opts(&data, opts);
1291 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001292 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001293
Al Viro2606b282013-09-20 17:14:21 +01001294 ffs = ffs_data_new();
1295 if (unlikely(!ffs))
1296 return ERR_PTR(-ENOMEM);
1297 ffs->file_perms = data.perms;
1298
1299 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1300 if (unlikely(!ffs->dev_name)) {
1301 ffs_data_put(ffs);
1302 return ERR_PTR(-ENOMEM);
1303 }
1304
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001305 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001306 if (IS_ERR(ffs_dev)) {
1307 ffs_data_put(ffs);
1308 return ERR_CAST(ffs_dev);
1309 }
1310 ffs->private_data = ffs_dev;
1311 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001312
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001313 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001314 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001315 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001316 ffs_data_put(data.ffs_data);
1317 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001318 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001319}
1320
1321static void
1322ffs_fs_kill_sb(struct super_block *sb)
1323{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001324 ENTER();
1325
1326 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001327 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001328 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001329 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001330 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001331}
1332
1333static struct file_system_type ffs_fs_type = {
1334 .owner = THIS_MODULE,
1335 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001336 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001337 .kill_sb = ffs_fs_kill_sb,
1338};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001339MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001340
1341
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001342/* Driver's main init/cleanup functions *************************************/
1343
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001344static int functionfs_init(void)
1345{
1346 int ret;
1347
1348 ENTER();
1349
1350 ret = register_filesystem(&ffs_fs_type);
1351 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001352 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001353 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001354 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001355
1356 return ret;
1357}
1358
1359static void functionfs_cleanup(void)
1360{
1361 ENTER();
1362
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001363 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001364 unregister_filesystem(&ffs_fs_type);
1365}
1366
1367
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001368/* ffs_data and ffs_function construction and destruction code **************/
1369
1370static void ffs_data_clear(struct ffs_data *ffs);
1371static void ffs_data_reset(struct ffs_data *ffs);
1372
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001373static void ffs_data_get(struct ffs_data *ffs)
1374{
1375 ENTER();
1376
1377 atomic_inc(&ffs->ref);
1378}
1379
1380static void ffs_data_opened(struct ffs_data *ffs)
1381{
1382 ENTER();
1383
1384 atomic_inc(&ffs->ref);
1385 atomic_inc(&ffs->opened);
1386}
1387
1388static void ffs_data_put(struct ffs_data *ffs)
1389{
1390 ENTER();
1391
1392 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001393 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001394 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001395 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001396 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001397 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001398 kfree(ffs);
1399 }
1400}
1401
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001402static void ffs_data_closed(struct ffs_data *ffs)
1403{
1404 ENTER();
1405
1406 if (atomic_dec_and_test(&ffs->opened)) {
1407 ffs->state = FFS_CLOSING;
1408 ffs_data_reset(ffs);
1409 }
1410
1411 ffs_data_put(ffs);
1412}
1413
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001414static struct ffs_data *ffs_data_new(void)
1415{
1416 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1417 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001418 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001419
1420 ENTER();
1421
1422 atomic_set(&ffs->ref, 1);
1423 atomic_set(&ffs->opened, 0);
1424 ffs->state = FFS_READ_DESCRIPTORS;
1425 mutex_init(&ffs->mutex);
1426 spin_lock_init(&ffs->eps_lock);
1427 init_waitqueue_head(&ffs->ev.waitq);
1428 init_completion(&ffs->ep0req_completion);
1429
1430 /* XXX REVISIT need to update it in some places, or do we? */
1431 ffs->ev.can_stall = 1;
1432
1433 return ffs;
1434}
1435
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001436static void ffs_data_clear(struct ffs_data *ffs)
1437{
1438 ENTER();
1439
1440 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001441 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001442
1443 BUG_ON(ffs->gadget);
1444
1445 if (ffs->epfiles)
1446 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1447
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301448 kfree(ffs->raw_descs_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001449 kfree(ffs->raw_strings);
1450 kfree(ffs->stringtabs);
1451}
1452
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001453static void ffs_data_reset(struct ffs_data *ffs)
1454{
1455 ENTER();
1456
1457 ffs_data_clear(ffs);
1458
1459 ffs->epfiles = NULL;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301460 ffs->raw_descs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001461 ffs->raw_descs = NULL;
1462 ffs->raw_strings = NULL;
1463 ffs->stringtabs = NULL;
1464
1465 ffs->raw_descs_length = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001466 ffs->fs_descs_count = 0;
1467 ffs->hs_descs_count = 0;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301468 ffs->ss_descs_count = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001469
1470 ffs->strings_count = 0;
1471 ffs->interfaces_count = 0;
1472 ffs->eps_count = 0;
1473
1474 ffs->ev.count = 0;
1475
1476 ffs->state = FFS_READ_DESCRIPTORS;
1477 ffs->setup_state = FFS_NO_SETUP;
1478 ffs->flags = 0;
1479}
1480
1481
1482static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1483{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001484 struct usb_gadget_strings **lang;
1485 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001486
1487 ENTER();
1488
1489 if (WARN_ON(ffs->state != FFS_ACTIVE
1490 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1491 return -EBADFD;
1492
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001493 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1494 if (unlikely(first_id < 0))
1495 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001496
1497 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1498 if (unlikely(!ffs->ep0req))
1499 return -ENOMEM;
1500 ffs->ep0req->complete = ffs_ep0_complete;
1501 ffs->ep0req->context = ffs;
1502
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001503 lang = ffs->stringtabs;
1504 for (lang = ffs->stringtabs; *lang; ++lang) {
1505 struct usb_string *str = (*lang)->strings;
1506 int id = first_id;
1507 for (; str->s; ++id, ++str)
1508 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001509 }
1510
1511 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001512 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001513 return 0;
1514}
1515
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001516static void functionfs_unbind(struct ffs_data *ffs)
1517{
1518 ENTER();
1519
1520 if (!WARN_ON(!ffs->gadget)) {
1521 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1522 ffs->ep0req = NULL;
1523 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001524 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001525 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001526 }
1527}
1528
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001529static int ffs_epfiles_create(struct ffs_data *ffs)
1530{
1531 struct ffs_epfile *epfile, *epfiles;
1532 unsigned i, count;
1533
1534 ENTER();
1535
1536 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001537 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001538 if (!epfiles)
1539 return -ENOMEM;
1540
1541 epfile = epfiles;
1542 for (i = 1; i <= count; ++i, ++epfile) {
1543 epfile->ffs = ffs;
1544 mutex_init(&epfile->mutex);
1545 init_waitqueue_head(&epfile->wait);
1546 sprintf(epfiles->name, "ep%u", i);
1547 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1548 &ffs_epfile_operations,
1549 &epfile->dentry))) {
1550 ffs_epfiles_destroy(epfiles, i - 1);
1551 return -ENOMEM;
1552 }
1553 }
1554
1555 ffs->epfiles = epfiles;
1556 return 0;
1557}
1558
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001559static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1560{
1561 struct ffs_epfile *epfile = epfiles;
1562
1563 ENTER();
1564
1565 for (; count; --count, ++epfile) {
1566 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1567 waitqueue_active(&epfile->wait));
1568 if (epfile->dentry) {
1569 d_delete(epfile->dentry);
1570 dput(epfile->dentry);
1571 epfile->dentry = NULL;
1572 }
1573 }
1574
1575 kfree(epfiles);
1576}
1577
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001578
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001579static void ffs_func_eps_disable(struct ffs_function *func)
1580{
1581 struct ffs_ep *ep = func->eps;
1582 struct ffs_epfile *epfile = func->ffs->epfiles;
1583 unsigned count = func->ffs->eps_count;
1584 unsigned long flags;
1585
1586 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1587 do {
1588 /* pending requests get nuked */
1589 if (likely(ep->ep))
1590 usb_ep_disable(ep->ep);
1591 epfile->ep = NULL;
1592
1593 ++ep;
1594 ++epfile;
1595 } while (--count);
1596 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1597}
1598
1599static int ffs_func_eps_enable(struct ffs_function *func)
1600{
1601 struct ffs_data *ffs = func->ffs;
1602 struct ffs_ep *ep = func->eps;
1603 struct ffs_epfile *epfile = ffs->epfiles;
1604 unsigned count = ffs->eps_count;
1605 unsigned long flags;
1606 int ret = 0;
1607
1608 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1609 do {
1610 struct usb_endpoint_descriptor *ds;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301611 int desc_idx;
1612
1613 if (ffs->gadget->speed == USB_SPEED_SUPER)
1614 desc_idx = 2;
1615 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1616 desc_idx = 1;
1617 else
1618 desc_idx = 0;
1619
1620 /* fall-back to lower speed if desc missing for current speed */
1621 do {
1622 ds = ep->descs[desc_idx];
1623 } while (!ds && --desc_idx >= 0);
1624
1625 if (!ds) {
1626 ret = -EINVAL;
1627 break;
1628 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001629
1630 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001631 ep->ep->desc = ds;
1632 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001633 if (likely(!ret)) {
1634 epfile->ep = ep;
1635 epfile->in = usb_endpoint_dir_in(ds);
1636 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1637 } else {
1638 break;
1639 }
1640
1641 wake_up(&epfile->wait);
1642
1643 ++ep;
1644 ++epfile;
1645 } while (--count);
1646 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1647
1648 return ret;
1649}
1650
1651
1652/* Parsing and building descriptors and strings *****************************/
1653
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001654/*
1655 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001656 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001657 * required by given configuration. Returns address after the
1658 * descriptor or NULL if data is invalid.
1659 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001660
1661enum ffs_entity_type {
1662 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1663};
1664
1665typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1666 u8 *valuep,
1667 struct usb_descriptor_header *desc,
1668 void *priv);
1669
1670static int __must_check ffs_do_desc(char *data, unsigned len,
1671 ffs_entity_callback entity, void *priv)
1672{
1673 struct usb_descriptor_header *_ds = (void *)data;
1674 u8 length;
1675 int ret;
1676
1677 ENTER();
1678
1679 /* At least two bytes are required: length and type */
1680 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001681 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001682 return -EINVAL;
1683 }
1684
1685 /* If we have at least as many bytes as the descriptor takes? */
1686 length = _ds->bLength;
1687 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001688 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001689 return -EINVAL;
1690 }
1691
1692#define __entity_check_INTERFACE(val) 1
1693#define __entity_check_STRING(val) (val)
1694#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1695#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001696 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001697 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001698 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001699 return -EINVAL; \
1700 } \
1701 ret = entity(FFS_ ##type, &val, _ds, priv); \
1702 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001703 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001704 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001705 return ret; \
1706 } \
1707 } while (0)
1708
1709 /* Parse descriptor depending on type. */
1710 switch (_ds->bDescriptorType) {
1711 case USB_DT_DEVICE:
1712 case USB_DT_CONFIG:
1713 case USB_DT_STRING:
1714 case USB_DT_DEVICE_QUALIFIER:
1715 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001716 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001717 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001718 return -EINVAL;
1719
1720 case USB_DT_INTERFACE: {
1721 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001722 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001723 if (length != sizeof *ds)
1724 goto inv_length;
1725
1726 __entity(INTERFACE, ds->bInterfaceNumber);
1727 if (ds->iInterface)
1728 __entity(STRING, ds->iInterface);
1729 }
1730 break;
1731
1732 case USB_DT_ENDPOINT: {
1733 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001734 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001735 if (length != USB_DT_ENDPOINT_SIZE &&
1736 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1737 goto inv_length;
1738 __entity(ENDPOINT, ds->bEndpointAddress);
1739 }
1740 break;
1741
Koen Beel560f1182012-05-30 20:43:37 +02001742 case HID_DT_HID:
1743 pr_vdebug("hid descriptor\n");
1744 if (length != sizeof(struct hid_descriptor))
1745 goto inv_length;
1746 break;
1747
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001748 case USB_DT_OTG:
1749 if (length != sizeof(struct usb_otg_descriptor))
1750 goto inv_length;
1751 break;
1752
1753 case USB_DT_INTERFACE_ASSOCIATION: {
1754 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001755 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001756 if (length != sizeof *ds)
1757 goto inv_length;
1758 if (ds->iFunction)
1759 __entity(STRING, ds->iFunction);
1760 }
1761 break;
1762
Manu Gautam8d4e8972014-02-28 16:50:22 +05301763 case USB_DT_SS_ENDPOINT_COMP:
1764 pr_vdebug("EP SS companion descriptor\n");
1765 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1766 goto inv_length;
1767 break;
1768
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001769 case USB_DT_OTHER_SPEED_CONFIG:
1770 case USB_DT_INTERFACE_POWER:
1771 case USB_DT_DEBUG:
1772 case USB_DT_SECURITY:
1773 case USB_DT_CS_RADIO_CONTROL:
1774 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001775 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001776 return -EINVAL;
1777
1778 default:
1779 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001780 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001781 return -EINVAL;
1782
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001783inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001784 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001785 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001786 return -EINVAL;
1787 }
1788
1789#undef __entity
1790#undef __entity_check_DESCRIPTOR
1791#undef __entity_check_INTERFACE
1792#undef __entity_check_STRING
1793#undef __entity_check_ENDPOINT
1794
1795 return length;
1796}
1797
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001798static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1799 ffs_entity_callback entity, void *priv)
1800{
1801 const unsigned _len = len;
1802 unsigned long num = 0;
1803
1804 ENTER();
1805
1806 for (;;) {
1807 int ret;
1808
1809 if (num == count)
1810 data = NULL;
1811
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001812 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001813 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1814 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001815 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001816 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001817 return ret;
1818 }
1819
1820 if (!data)
1821 return _len - len;
1822
1823 ret = ffs_do_desc(data, len, entity, priv);
1824 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001825 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001826 return ret;
1827 }
1828
1829 len -= ret;
1830 data += ret;
1831 ++num;
1832 }
1833}
1834
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001835static int __ffs_data_do_entity(enum ffs_entity_type type,
1836 u8 *valuep, struct usb_descriptor_header *desc,
1837 void *priv)
1838{
1839 struct ffs_data *ffs = priv;
1840
1841 ENTER();
1842
1843 switch (type) {
1844 case FFS_DESCRIPTOR:
1845 break;
1846
1847 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001848 /*
1849 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001850 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001851 * "n+1" interfaces.
1852 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001853 if (*valuep >= ffs->interfaces_count)
1854 ffs->interfaces_count = *valuep + 1;
1855 break;
1856
1857 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001858 /*
1859 * Strings are indexed from 1 (0 is magic ;) reserved
1860 * for languages list or some such)
1861 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001862 if (*valuep > ffs->strings_count)
1863 ffs->strings_count = *valuep;
1864 break;
1865
1866 case FFS_ENDPOINT:
1867 /* Endpoints are indexed from 1 as well. */
1868 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1869 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1870 break;
1871 }
1872
1873 return 0;
1874}
1875
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001876static int __ffs_data_got_descs(struct ffs_data *ffs,
1877 char *const _data, size_t len)
1878{
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301879 char *data = _data, *raw_descs;
1880 unsigned counts[3], flags;
1881 int ret = -EINVAL, i;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001882
1883 ENTER();
1884
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301885 if (get_unaligned_le32(data + 4) != len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001886 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001887
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301888 switch (get_unaligned_le32(data)) {
1889 case FUNCTIONFS_DESCRIPTORS_MAGIC:
1890 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301891 data += 8;
1892 len -= 8;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301893 break;
1894 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
1895 flags = get_unaligned_le32(data + 8);
1896 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
1897 FUNCTIONFS_HAS_HS_DESC |
1898 FUNCTIONFS_HAS_SS_DESC)) {
1899 ret = -ENOSYS;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001900 goto error;
1901 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301902 data += 12;
1903 len -= 12;
1904 break;
1905 default:
1906 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001907 }
1908
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301909 /* Read fs_count, hs_count and ss_count (if present) */
1910 for (i = 0; i < 3; ++i) {
1911 if (!(flags & (1 << i))) {
1912 counts[i] = 0;
1913 } else if (len < 4) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001914 goto error;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301915 } else {
1916 counts[i] = get_unaligned_le32(data);
1917 data += 4;
1918 len -= 4;
1919 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001920 }
1921
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301922 /* Read descriptors */
1923 raw_descs = data;
1924 for (i = 0; i < 3; ++i) {
1925 if (!counts[i])
1926 continue;
1927 ret = ffs_do_descs(counts[i], data, len,
1928 __ffs_data_do_entity, ffs);
1929 if (ret < 0)
1930 goto error;
1931 data += ret;
1932 len -= ret;
1933 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001934
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301935 if (raw_descs == data || len) {
1936 ret = -EINVAL;
1937 goto error;
1938 }
1939
1940 ffs->raw_descs_data = _data;
1941 ffs->raw_descs = raw_descs;
1942 ffs->raw_descs_length = data - raw_descs;
1943 ffs->fs_descs_count = counts[0];
1944 ffs->hs_descs_count = counts[1];
1945 ffs->ss_descs_count = counts[2];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001946
1947 return 0;
1948
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001949error:
1950 kfree(_data);
1951 return ret;
1952}
1953
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001954static int __ffs_data_got_strings(struct ffs_data *ffs,
1955 char *const _data, size_t len)
1956{
1957 u32 str_count, needed_count, lang_count;
1958 struct usb_gadget_strings **stringtabs, *t;
1959 struct usb_string *strings, *s;
1960 const char *data = _data;
1961
1962 ENTER();
1963
1964 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1965 get_unaligned_le32(data + 4) != len))
1966 goto error;
1967 str_count = get_unaligned_le32(data + 8);
1968 lang_count = get_unaligned_le32(data + 12);
1969
1970 /* if one is zero the other must be zero */
1971 if (unlikely(!str_count != !lang_count))
1972 goto error;
1973
1974 /* Do we have at least as many strings as descriptors need? */
1975 needed_count = ffs->strings_count;
1976 if (unlikely(str_count < needed_count))
1977 goto error;
1978
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001979 /*
1980 * If we don't need any strings just return and free all
1981 * memory.
1982 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001983 if (!needed_count) {
1984 kfree(_data);
1985 return 0;
1986 }
1987
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001988 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001989 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001990 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001991 vla_group(d);
1992 vla_item(d, struct usb_gadget_strings *, stringtabs,
1993 lang_count + 1);
1994 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
1995 vla_item(d, struct usb_string, strings,
1996 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001997
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001998 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
1999
2000 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002001 kfree(_data);
2002 return -ENOMEM;
2003 }
2004
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002005 /* Initialize the VLA pointers */
2006 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2007 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002008 i = lang_count;
2009 do {
2010 *stringtabs++ = t++;
2011 } while (--i);
2012 *stringtabs = NULL;
2013
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002014 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2015 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2016 t = vla_ptr(vlabuf, d, stringtab);
2017 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002018 strings = s;
2019 }
2020
2021 /* For each language */
2022 data += 16;
2023 len -= 16;
2024
2025 do { /* lang_count > 0 so we can use do-while */
2026 unsigned needed = needed_count;
2027
2028 if (unlikely(len < 3))
2029 goto error_free;
2030 t->language = get_unaligned_le16(data);
2031 t->strings = s;
2032 ++t;
2033
2034 data += 2;
2035 len -= 2;
2036
2037 /* For each string */
2038 do { /* str_count > 0 so we can use do-while */
2039 size_t length = strnlen(data, len);
2040
2041 if (unlikely(length == len))
2042 goto error_free;
2043
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002044 /*
2045 * User may provide more strings then we need,
2046 * if that's the case we simply ignore the
2047 * rest
2048 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002049 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002050 /*
2051 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002052 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002053 * now just leave garbage here.
2054 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002055 s->s = data;
2056 --needed;
2057 ++s;
2058 }
2059
2060 data += length + 1;
2061 len -= length + 1;
2062 } while (--str_count);
2063
2064 s->id = 0; /* terminator */
2065 s->s = NULL;
2066 ++s;
2067
2068 } while (--lang_count);
2069
2070 /* Some garbage left? */
2071 if (unlikely(len))
2072 goto error_free;
2073
2074 /* Done! */
2075 ffs->stringtabs = stringtabs;
2076 ffs->raw_strings = _data;
2077
2078 return 0;
2079
2080error_free:
2081 kfree(stringtabs);
2082error:
2083 kfree(_data);
2084 return -EINVAL;
2085}
2086
2087
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002088/* Events handling and management *******************************************/
2089
2090static void __ffs_event_add(struct ffs_data *ffs,
2091 enum usb_functionfs_event_type type)
2092{
2093 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2094 int neg = 0;
2095
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002096 /*
2097 * Abort any unhandled setup
2098 *
2099 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002100 * of ffs->setup_state without holding the lock because when
2101 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002102 * the source does nothing.
2103 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002104 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01002105 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002106
2107 switch (type) {
2108 case FUNCTIONFS_RESUME:
2109 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002110 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002111 case FUNCTIONFS_SUSPEND:
2112 case FUNCTIONFS_SETUP:
2113 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002114 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002115 break;
2116
2117 case FUNCTIONFS_BIND:
2118 case FUNCTIONFS_UNBIND:
2119 case FUNCTIONFS_DISABLE:
2120 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002121 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002122 rem_type1 = FUNCTIONFS_SUSPEND;
2123 rem_type2 = FUNCTIONFS_RESUME;
2124 neg = 1;
2125 break;
2126
2127 default:
2128 BUG();
2129 }
2130
2131 {
2132 u8 *ev = ffs->ev.types, *out = ev;
2133 unsigned n = ffs->ev.count;
2134 for (; n; --n, ++ev)
2135 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2136 *out++ = *ev;
2137 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002138 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002139 ffs->ev.count = out - ffs->ev.types;
2140 }
2141
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002142 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002143 ffs->ev.types[ffs->ev.count++] = type;
2144 wake_up_locked(&ffs->ev.waitq);
2145}
2146
2147static void ffs_event_add(struct ffs_data *ffs,
2148 enum usb_functionfs_event_type type)
2149{
2150 unsigned long flags;
2151 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2152 __ffs_event_add(ffs, type);
2153 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2154}
2155
2156
2157/* Bind/unbind USB function hooks *******************************************/
2158
2159static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2160 struct usb_descriptor_header *desc,
2161 void *priv)
2162{
2163 struct usb_endpoint_descriptor *ds = (void *)desc;
2164 struct ffs_function *func = priv;
2165 struct ffs_ep *ffs_ep;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302166 unsigned ep_desc_id, idx;
2167 static const char *speed_names[] = { "full", "high", "super" };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002168
2169 if (type != FFS_DESCRIPTOR)
2170 return 0;
2171
Manu Gautam8d4e8972014-02-28 16:50:22 +05302172 /*
2173 * If ss_descriptors is not NULL, we are reading super speed
2174 * descriptors; if hs_descriptors is not NULL, we are reading high
2175 * speed descriptors; otherwise, we are reading full speed
2176 * descriptors.
2177 */
2178 if (func->function.ss_descriptors) {
2179 ep_desc_id = 2;
2180 func->function.ss_descriptors[(long)valuep] = desc;
2181 } else if (func->function.hs_descriptors) {
2182 ep_desc_id = 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002183 func->function.hs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302184 } else {
2185 ep_desc_id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002186 func->function.fs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302187 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002188
2189 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2190 return 0;
2191
2192 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2193 ffs_ep = func->eps + idx;
2194
Manu Gautam8d4e8972014-02-28 16:50:22 +05302195 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2196 pr_err("two %sspeed descriptors for EP %d\n",
2197 speed_names[ep_desc_id],
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002198 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002199 return -EINVAL;
2200 }
Manu Gautam8d4e8972014-02-28 16:50:22 +05302201 ffs_ep->descs[ep_desc_id] = ds;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002202
2203 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2204 if (ffs_ep->ep) {
2205 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2206 if (!ds->wMaxPacketSize)
2207 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2208 } else {
2209 struct usb_request *req;
2210 struct usb_ep *ep;
2211
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002212 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002213 ep = usb_ep_autoconfig(func->gadget, ds);
2214 if (unlikely(!ep))
2215 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08002216 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002217
2218 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2219 if (unlikely(!req))
2220 return -ENOMEM;
2221
2222 ffs_ep->ep = ep;
2223 ffs_ep->req = req;
2224 func->eps_revmap[ds->bEndpointAddress &
2225 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2226 }
2227 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2228
2229 return 0;
2230}
2231
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002232static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2233 struct usb_descriptor_header *desc,
2234 void *priv)
2235{
2236 struct ffs_function *func = priv;
2237 unsigned idx;
2238 u8 newValue;
2239
2240 switch (type) {
2241 default:
2242 case FFS_DESCRIPTOR:
2243 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2244 return 0;
2245
2246 case FFS_INTERFACE:
2247 idx = *valuep;
2248 if (func->interfaces_nums[idx] < 0) {
2249 int id = usb_interface_id(func->conf, &func->function);
2250 if (unlikely(id < 0))
2251 return id;
2252 func->interfaces_nums[idx] = id;
2253 }
2254 newValue = func->interfaces_nums[idx];
2255 break;
2256
2257 case FFS_STRING:
2258 /* String' IDs are allocated when fsf_data is bound to cdev */
2259 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2260 break;
2261
2262 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002263 /*
2264 * USB_DT_ENDPOINT are handled in
2265 * __ffs_func_bind_do_descs().
2266 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002267 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2268 return 0;
2269
2270 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2271 if (unlikely(!func->eps[idx].ep))
2272 return -EINVAL;
2273
2274 {
2275 struct usb_endpoint_descriptor **descs;
2276 descs = func->eps[idx].descs;
2277 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2278 }
2279 break;
2280 }
2281
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002282 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002283 *valuep = newValue;
2284 return 0;
2285}
2286
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002287static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2288 struct usb_configuration *c)
2289{
2290 struct ffs_function *func = ffs_func_from_usb(f);
2291 struct f_fs_opts *ffs_opts =
2292 container_of(f->fi, struct f_fs_opts, func_inst);
2293 int ret;
2294
2295 ENTER();
2296
2297 /*
2298 * Legacy gadget triggers binding in functionfs_ready_callback,
2299 * which already uses locking; taking the same lock here would
2300 * cause a deadlock.
2301 *
2302 * Configfs-enabled gadgets however do need ffs_dev_lock.
2303 */
2304 if (!ffs_opts->no_configfs)
2305 ffs_dev_lock();
2306 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2307 func->ffs = ffs_opts->dev->ffs_data;
2308 if (!ffs_opts->no_configfs)
2309 ffs_dev_unlock();
2310 if (ret)
2311 return ERR_PTR(ret);
2312
2313 func->conf = c;
2314 func->gadget = c->cdev->gadget;
2315
2316 ffs_data_get(func->ffs);
2317
2318 /*
2319 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2320 * configurations are bound in sequence with list_for_each_entry,
2321 * in each configuration its functions are bound in sequence
2322 * with list_for_each_entry, so we assume no race condition
2323 * with regard to ffs_opts->bound access
2324 */
2325 if (!ffs_opts->refcnt) {
2326 ret = functionfs_bind(func->ffs, c->cdev);
2327 if (ret)
2328 return ERR_PTR(ret);
2329 }
2330 ffs_opts->refcnt++;
2331 func->function.strings = func->ffs->stringtabs;
2332
2333 return ffs_opts;
2334}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002335
2336static int _ffs_func_bind(struct usb_configuration *c,
2337 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002338{
2339 struct ffs_function *func = ffs_func_from_usb(f);
2340 struct ffs_data *ffs = func->ffs;
2341
2342 const int full = !!func->ffs->fs_descs_count;
2343 const int high = gadget_is_dualspeed(func->gadget) &&
2344 func->ffs->hs_descs_count;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302345 const int super = gadget_is_superspeed(func->gadget) &&
2346 func->ffs->ss_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002347
Manu Gautam8d4e8972014-02-28 16:50:22 +05302348 int fs_len, hs_len, ret;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002349
2350 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002351 vla_group(d);
2352 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2353 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2354 full ? ffs->fs_descs_count + 1 : 0);
2355 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2356 high ? ffs->hs_descs_count + 1 : 0);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302357 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2358 super ? ffs->ss_descs_count + 1 : 0);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002359 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302360 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002361 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002362
2363 ENTER();
2364
Manu Gautam8d4e8972014-02-28 16:50:22 +05302365 /* Has descriptors only for speeds gadget does not support */
2366 if (unlikely(!(full | high | super)))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002367 return -ENOTSUPP;
2368
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002369 /* Allocate a single chunk, less management later on */
2370 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2371 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002372 return -ENOMEM;
2373
2374 /* Zero */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002375 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302376 /* Copy descriptors */
2377 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2378 ffs->raw_descs_length);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302379
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002380 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2381 for (ret = ffs->eps_count; ret; --ret) {
2382 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002383
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002384 ptr = vla_ptr(vlabuf, d, eps);
2385 ptr[ret].num = -1;
2386 }
2387
2388 /* Save pointers
2389 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2390 */
2391 func->eps = vla_ptr(vlabuf, d, eps);
2392 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002393
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002394 /*
2395 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002396 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002397 * numbers without worrying that it may be described later on.
2398 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002399 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002400 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302401 fs_len = ffs_do_descs(ffs->fs_descs_count,
2402 vla_ptr(vlabuf, d, raw_descs),
2403 d_raw_descs__sz,
2404 __ffs_func_bind_do_descs, func);
2405 if (unlikely(fs_len < 0)) {
2406 ret = fs_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002407 goto error;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302408 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002409 } else {
Manu Gautam8d4e8972014-02-28 16:50:22 +05302410 fs_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002411 }
2412
2413 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002414 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302415 hs_len = ffs_do_descs(ffs->hs_descs_count,
2416 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2417 d_raw_descs__sz - fs_len,
2418 __ffs_func_bind_do_descs, func);
2419 if (unlikely(hs_len < 0)) {
2420 ret = hs_len;
2421 goto error;
2422 }
2423 } else {
2424 hs_len = 0;
2425 }
2426
2427 if (likely(super)) {
2428 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2429 ret = ffs_do_descs(ffs->ss_descs_count,
2430 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2431 d_raw_descs__sz - fs_len - hs_len,
2432 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002433 if (unlikely(ret < 0))
2434 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002435 }
2436
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002437 /*
2438 * Now handle interface numbers allocation and interface and
2439 * endpoint numbers rewriting. We can do that in one go
2440 * now.
2441 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002442 ret = ffs_do_descs(ffs->fs_descs_count +
Manu Gautam8d4e8972014-02-28 16:50:22 +05302443 (high ? ffs->hs_descs_count : 0) +
2444 (super ? ffs->ss_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002445 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002446 __ffs_func_bind_do_nums, func);
2447 if (unlikely(ret < 0))
2448 goto error;
2449
2450 /* And we're done */
2451 ffs_event_add(ffs, FUNCTIONFS_BIND);
2452 return 0;
2453
2454error:
2455 /* XXX Do we need to release all claimed endpoints here? */
2456 return ret;
2457}
2458
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002459static int ffs_func_bind(struct usb_configuration *c,
2460 struct usb_function *f)
2461{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002462 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2463
2464 if (IS_ERR(ffs_opts))
2465 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002466
2467 return _ffs_func_bind(c, f);
2468}
2469
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002470
2471/* Other USB function hooks *************************************************/
2472
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002473static int ffs_func_set_alt(struct usb_function *f,
2474 unsigned interface, unsigned alt)
2475{
2476 struct ffs_function *func = ffs_func_from_usb(f);
2477 struct ffs_data *ffs = func->ffs;
2478 int ret = 0, intf;
2479
2480 if (alt != (unsigned)-1) {
2481 intf = ffs_func_revmap_intf(func, interface);
2482 if (unlikely(intf < 0))
2483 return intf;
2484 }
2485
2486 if (ffs->func)
2487 ffs_func_eps_disable(ffs->func);
2488
2489 if (ffs->state != FFS_ACTIVE)
2490 return -ENODEV;
2491
2492 if (alt == (unsigned)-1) {
2493 ffs->func = NULL;
2494 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2495 return 0;
2496 }
2497
2498 ffs->func = func;
2499 ret = ffs_func_eps_enable(func);
2500 if (likely(ret >= 0))
2501 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2502 return ret;
2503}
2504
2505static void ffs_func_disable(struct usb_function *f)
2506{
2507 ffs_func_set_alt(f, 0, (unsigned)-1);
2508}
2509
2510static int ffs_func_setup(struct usb_function *f,
2511 const struct usb_ctrlrequest *creq)
2512{
2513 struct ffs_function *func = ffs_func_from_usb(f);
2514 struct ffs_data *ffs = func->ffs;
2515 unsigned long flags;
2516 int ret;
2517
2518 ENTER();
2519
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002520 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2521 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2522 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2523 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2524 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002525
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002526 /*
2527 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002528 * (notable exceptions are set/get interface) so we need to
2529 * handle them. All other either handled by composite or
2530 * passed to usb_configuration->setup() (if one is set). No
2531 * matter, we will handle requests directed to endpoint here
2532 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002533 * other request?
2534 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002535 if (ffs->state != FFS_ACTIVE)
2536 return -ENODEV;
2537
2538 switch (creq->bRequestType & USB_RECIP_MASK) {
2539 case USB_RECIP_INTERFACE:
2540 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2541 if (unlikely(ret < 0))
2542 return ret;
2543 break;
2544
2545 case USB_RECIP_ENDPOINT:
2546 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2547 if (unlikely(ret < 0))
2548 return ret;
2549 break;
2550
2551 default:
2552 return -EOPNOTSUPP;
2553 }
2554
2555 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2556 ffs->ev.setup = *creq;
2557 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2558 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2559 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2560
2561 return 0;
2562}
2563
2564static void ffs_func_suspend(struct usb_function *f)
2565{
2566 ENTER();
2567 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2568}
2569
2570static void ffs_func_resume(struct usb_function *f)
2571{
2572 ENTER();
2573 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2574}
2575
2576
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002577/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002578
2579static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2580{
2581 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2582 return num ? num : -EDOM;
2583}
2584
2585static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2586{
2587 short *nums = func->interfaces_nums;
2588 unsigned count = func->ffs->interfaces_count;
2589
2590 for (; count; --count, ++nums) {
2591 if (*nums >= 0 && *nums == intf)
2592 return nums - func->interfaces_nums;
2593 }
2594
2595 return -EDOM;
2596}
2597
2598
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002599/* Devices management *******************************************************/
2600
2601static LIST_HEAD(ffs_devices);
2602
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002603static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002604{
2605 struct ffs_dev *dev;
2606
2607 list_for_each_entry(dev, &ffs_devices, entry) {
2608 if (!dev->name || !name)
2609 continue;
2610 if (strcmp(dev->name, name) == 0)
2611 return dev;
2612 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002613
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002614 return NULL;
2615}
2616
2617/*
2618 * ffs_lock must be taken by the caller of this function
2619 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002620static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002621{
2622 struct ffs_dev *dev;
2623
2624 if (list_is_singular(&ffs_devices)) {
2625 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2626 if (dev->single)
2627 return dev;
2628 }
2629
2630 return NULL;
2631}
2632
2633/*
2634 * ffs_lock must be taken by the caller of this function
2635 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002636static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002637{
2638 struct ffs_dev *dev;
2639
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002640 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002641 if (dev)
2642 return dev;
2643
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002644 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002645}
2646
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002647/* Configfs support *********************************************************/
2648
2649static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2650{
2651 return container_of(to_config_group(item), struct f_fs_opts,
2652 func_inst.group);
2653}
2654
2655static void ffs_attr_release(struct config_item *item)
2656{
2657 struct f_fs_opts *opts = to_ffs_opts(item);
2658
2659 usb_put_function_instance(&opts->func_inst);
2660}
2661
2662static struct configfs_item_operations ffs_item_ops = {
2663 .release = ffs_attr_release,
2664};
2665
2666static struct config_item_type ffs_func_type = {
2667 .ct_item_ops = &ffs_item_ops,
2668 .ct_owner = THIS_MODULE,
2669};
2670
2671
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002672/* Function registration interface ******************************************/
2673
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002674static void ffs_free_inst(struct usb_function_instance *f)
2675{
2676 struct f_fs_opts *opts;
2677
2678 opts = to_f_fs_opts(f);
2679 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002680 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002681 ffs_dev_unlock();
2682 kfree(opts);
2683}
2684
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002685#define MAX_INST_NAME_LEN 40
2686
2687static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2688{
2689 struct f_fs_opts *opts;
2690 char *ptr;
2691 const char *tmp;
2692 int name_len, ret;
2693
2694 name_len = strlen(name) + 1;
2695 if (name_len > MAX_INST_NAME_LEN)
2696 return -ENAMETOOLONG;
2697
2698 ptr = kstrndup(name, name_len, GFP_KERNEL);
2699 if (!ptr)
2700 return -ENOMEM;
2701
2702 opts = to_f_fs_opts(fi);
2703 tmp = NULL;
2704
2705 ffs_dev_lock();
2706
2707 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2708 ret = _ffs_name_dev(opts->dev, ptr);
2709 if (ret) {
2710 kfree(ptr);
2711 ffs_dev_unlock();
2712 return ret;
2713 }
2714 opts->dev->name_allocated = true;
2715
2716 ffs_dev_unlock();
2717
2718 kfree(tmp);
2719
2720 return 0;
2721}
2722
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002723static struct usb_function_instance *ffs_alloc_inst(void)
2724{
2725 struct f_fs_opts *opts;
2726 struct ffs_dev *dev;
2727
2728 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2729 if (!opts)
2730 return ERR_PTR(-ENOMEM);
2731
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002732 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002733 opts->func_inst.free_func_inst = ffs_free_inst;
2734 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002735 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002736 ffs_dev_unlock();
2737 if (IS_ERR(dev)) {
2738 kfree(opts);
2739 return ERR_CAST(dev);
2740 }
2741 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002742 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002743
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002744 config_group_init_type_name(&opts->func_inst.group, "",
2745 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002746 return &opts->func_inst;
2747}
2748
2749static void ffs_free(struct usb_function *f)
2750{
2751 kfree(ffs_func_from_usb(f));
2752}
2753
2754static void ffs_func_unbind(struct usb_configuration *c,
2755 struct usb_function *f)
2756{
2757 struct ffs_function *func = ffs_func_from_usb(f);
2758 struct ffs_data *ffs = func->ffs;
2759 struct f_fs_opts *opts =
2760 container_of(f->fi, struct f_fs_opts, func_inst);
2761 struct ffs_ep *ep = func->eps;
2762 unsigned count = ffs->eps_count;
2763 unsigned long flags;
2764
2765 ENTER();
2766 if (ffs->func == func) {
2767 ffs_func_eps_disable(func);
2768 ffs->func = NULL;
2769 }
2770
2771 if (!--opts->refcnt)
2772 functionfs_unbind(ffs);
2773
2774 /* cleanup after autoconfig */
2775 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2776 do {
2777 if (ep->ep && ep->req)
2778 usb_ep_free_request(ep->ep, ep->req);
2779 ep->req = NULL;
2780 ++ep;
2781 } while (--count);
2782 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2783 kfree(func->eps);
2784 func->eps = NULL;
2785 /*
2786 * eps, descriptors and interfaces_nums are allocated in the
2787 * same chunk so only one free is required.
2788 */
2789 func->function.fs_descriptors = NULL;
2790 func->function.hs_descriptors = NULL;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302791 func->function.ss_descriptors = NULL;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002792 func->interfaces_nums = NULL;
2793
2794 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2795}
2796
2797static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2798{
2799 struct ffs_function *func;
2800
2801 ENTER();
2802
2803 func = kzalloc(sizeof(*func), GFP_KERNEL);
2804 if (unlikely(!func))
2805 return ERR_PTR(-ENOMEM);
2806
2807 func->function.name = "Function FS Gadget";
2808
2809 func->function.bind = ffs_func_bind;
2810 func->function.unbind = ffs_func_unbind;
2811 func->function.set_alt = ffs_func_set_alt;
2812 func->function.disable = ffs_func_disable;
2813 func->function.setup = ffs_func_setup;
2814 func->function.suspend = ffs_func_suspend;
2815 func->function.resume = ffs_func_resume;
2816 func->function.free_func = ffs_free;
2817
2818 return &func->function;
2819}
2820
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002821/*
2822 * ffs_lock must be taken by the caller of this function
2823 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002824static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002825{
2826 struct ffs_dev *dev;
2827 int ret;
2828
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002829 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002830 return ERR_PTR(-EBUSY);
2831
2832 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2833 if (!dev)
2834 return ERR_PTR(-ENOMEM);
2835
2836 if (list_empty(&ffs_devices)) {
2837 ret = functionfs_init();
2838 if (ret) {
2839 kfree(dev);
2840 return ERR_PTR(ret);
2841 }
2842 }
2843
2844 list_add(&dev->entry, &ffs_devices);
2845
2846 return dev;
2847}
2848
2849/*
2850 * ffs_lock must be taken by the caller of this function
2851 * The caller is responsible for "name" being available whenever f_fs needs it
2852 */
2853static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2854{
2855 struct ffs_dev *existing;
2856
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002857 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002858 if (existing)
2859 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01002860
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002861 dev->name = name;
2862
2863 return 0;
2864}
2865
2866/*
2867 * The caller is responsible for "name" being available whenever f_fs needs it
2868 */
2869int ffs_name_dev(struct ffs_dev *dev, const char *name)
2870{
2871 int ret;
2872
2873 ffs_dev_lock();
2874 ret = _ffs_name_dev(dev, name);
2875 ffs_dev_unlock();
2876
2877 return ret;
2878}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002879EXPORT_SYMBOL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002880
2881int ffs_single_dev(struct ffs_dev *dev)
2882{
2883 int ret;
2884
2885 ret = 0;
2886 ffs_dev_lock();
2887
2888 if (!list_is_singular(&ffs_devices))
2889 ret = -EBUSY;
2890 else
2891 dev->single = true;
2892
2893 ffs_dev_unlock();
2894 return ret;
2895}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002896EXPORT_SYMBOL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002897
2898/*
2899 * ffs_lock must be taken by the caller of this function
2900 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002901static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002902{
2903 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002904 if (dev->name_allocated)
2905 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002906 kfree(dev);
2907 if (list_empty(&ffs_devices))
2908 functionfs_cleanup();
2909}
2910
2911static void *ffs_acquire_dev(const char *dev_name)
2912{
2913 struct ffs_dev *ffs_dev;
2914
2915 ENTER();
2916 ffs_dev_lock();
2917
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002918 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002919 if (!ffs_dev)
2920 ffs_dev = ERR_PTR(-ENODEV);
2921 else if (ffs_dev->mounted)
2922 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002923 else if (ffs_dev->ffs_acquire_dev_callback &&
2924 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2925 ffs_dev = ERR_PTR(-ENODEV);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002926 else
2927 ffs_dev->mounted = true;
2928
2929 ffs_dev_unlock();
2930 return ffs_dev;
2931}
2932
2933static void ffs_release_dev(struct ffs_data *ffs_data)
2934{
2935 struct ffs_dev *ffs_dev;
2936
2937 ENTER();
2938 ffs_dev_lock();
2939
2940 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002941 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002942 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002943
2944 if (ffs_dev->ffs_release_dev_callback)
2945 ffs_dev->ffs_release_dev_callback(ffs_dev);
2946 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002947
2948 ffs_dev_unlock();
2949}
2950
2951static int ffs_ready(struct ffs_data *ffs)
2952{
2953 struct ffs_dev *ffs_obj;
2954 int ret = 0;
2955
2956 ENTER();
2957 ffs_dev_lock();
2958
2959 ffs_obj = ffs->private_data;
2960 if (!ffs_obj) {
2961 ret = -EINVAL;
2962 goto done;
2963 }
2964 if (WARN_ON(ffs_obj->desc_ready)) {
2965 ret = -EBUSY;
2966 goto done;
2967 }
2968
2969 ffs_obj->desc_ready = true;
2970 ffs_obj->ffs_data = ffs;
2971
2972 if (ffs_obj->ffs_ready_callback)
2973 ret = ffs_obj->ffs_ready_callback(ffs);
2974
2975done:
2976 ffs_dev_unlock();
2977 return ret;
2978}
2979
2980static void ffs_closed(struct ffs_data *ffs)
2981{
2982 struct ffs_dev *ffs_obj;
2983
2984 ENTER();
2985 ffs_dev_lock();
2986
2987 ffs_obj = ffs->private_data;
2988 if (!ffs_obj)
2989 goto done;
2990
2991 ffs_obj->desc_ready = false;
2992
2993 if (ffs_obj->ffs_closed_callback)
2994 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002995
2996 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
2997 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
2998 goto done;
2999
3000 unregister_gadget_item(ffs_obj->opts->
3001 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003002done:
3003 ffs_dev_unlock();
3004}
3005
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003006/* Misc helper functions ****************************************************/
3007
3008static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3009{
3010 return nonblock
3011 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3012 : mutex_lock_interruptible(mutex);
3013}
3014
Al Viro260ef312012-09-26 21:43:45 -04003015static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003016{
3017 char *data;
3018
3019 if (unlikely(!len))
3020 return NULL;
3021
3022 data = kmalloc(len, GFP_KERNEL);
3023 if (unlikely(!data))
3024 return ERR_PTR(-ENOMEM);
3025
3026 if (unlikely(__copy_from_user(data, buf, len))) {
3027 kfree(data);
3028 return ERR_PTR(-EFAULT);
3029 }
3030
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01003031 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003032 ffs_dump_mem("", data, len);
3033
3034 return data;
3035}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003036
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003037DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3038MODULE_LICENSE("GPL");
3039MODULE_AUTHOR("Michal Nazarewicz");