blob: 1e12b3ee56fd837117a03266cf66205bb418b366 [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
Chao Bi97839ca2014-04-14 11:19:53 +0800748 spin_lock_irq(&epfile->ffs->eps_lock);
749 /* In the meantime, endpoint got disabled or changed. */
750 if (epfile->ep != ep) {
751 spin_unlock_irq(&epfile->ffs->eps_lock);
752 return -ESHUTDOWN;
753 }
Andrzej Pietrasiewiczf0f42202014-01-20 08:33:50 +0100754 /*
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800755 * Controller may require buffer size to be aligned to
756 * maxpacketsize of an out endpoint.
757 */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100758 data_len = io_data->read ?
759 usb_ep_align_maybe(gadget, ep->ep, io_data->len) :
760 io_data->len;
Chao Bi97839ca2014-04-14 11:19:53 +0800761 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz219580e2013-12-09 15:55:37 -0800762
763 data = kmalloc(data_len, GFP_KERNEL);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800764 if (unlikely(!data))
765 return -ENOMEM;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100766 if (io_data->aio && !io_data->read) {
767 int i;
768 size_t pos = 0;
769 for (i = 0; i < io_data->nr_segs; i++) {
770 if (unlikely(copy_from_user(&data[pos],
771 io_data->iovec[i].iov_base,
772 io_data->iovec[i].iov_len))) {
773 ret = -EFAULT;
774 goto error;
775 }
776 pos += io_data->iovec[i].iov_len;
777 }
778 } else {
779 if (!io_data->read &&
780 unlikely(__copy_from_user(data, io_data->buf,
781 io_data->len))) {
782 ret = -EFAULT;
783 goto error;
784 }
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800785 }
786 }
787
788 /* We will be using request */
789 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
790 if (unlikely(ret))
791 goto error;
792
793 spin_lock_irq(&epfile->ffs->eps_lock);
794
795 if (epfile->ep != ep) {
796 /* In the meantime, endpoint got disabled or changed. */
797 ret = -ESHUTDOWN;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200798 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewicz7fa68032013-12-09 15:55:36 -0800799 } else if (halt) {
800 /* Halt */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200801 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
802 usb_ep_set_halt(ep->ep);
803 spin_unlock_irq(&epfile->ffs->eps_lock);
804 ret = -EBADMSG;
805 } else {
806 /* Fire the request */
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100807 struct usb_request *req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200808
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100809 if (io_data->aio) {
810 req = usb_ep_alloc_request(ep->ep, GFP_KERNEL);
811 if (unlikely(!req))
Robert Baldyga48968f82014-03-10 09:33:37 +0100812 goto error_lock;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200813
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100814 req->buf = data;
815 req->length = io_data->len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200816
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100817 io_data->buf = data;
818 io_data->ep = ep->ep;
819 io_data->req = req;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200820
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100821 req->context = io_data;
822 req->complete = ffs_epfile_async_io_complete;
823
824 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
825 if (unlikely(ret)) {
826 usb_ep_free_request(ep->ep, req);
Robert Baldyga48968f82014-03-10 09:33:37 +0100827 goto error_lock;
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100828 }
829 ret = -EIOCBQUEUED;
830
831 spin_unlock_irq(&epfile->ffs->eps_lock);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200832 } else {
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100833 DECLARE_COMPLETION_ONSTACK(done);
834
835 req = ep->req;
836 req->buf = data;
837 req->length = io_data->len;
838
839 req->context = &done;
840 req->complete = ffs_epfile_io_complete;
841
842 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
843
844 spin_unlock_irq(&epfile->ffs->eps_lock);
845
846 if (unlikely(ret < 0)) {
847 /* nop */
848 } else if (unlikely(
849 wait_for_completion_interruptible(&done))) {
850 ret = -EINTR;
851 usb_ep_dequeue(ep->ep, req);
852 } else {
Chuansheng Liucfe919b2014-03-04 15:34:57 +0800853 /*
854 * XXX We may end up silently droping data
855 * here. Since data_len (i.e. req->length) may
856 * be bigger than len (after being rounded up
857 * to maxpacketsize), we may end up with more
858 * data then user space has space for.
859 */
860 ret = ep->status;
861 if (io_data->read && ret > 0) {
862 ret = min_t(size_t, ret, io_data->len);
863
864 if (unlikely(copy_to_user(io_data->buf,
865 data, ret)))
866 ret = -EFAULT;
867 }
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100868 }
869 kfree(data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200870 }
871 }
872
873 mutex_unlock(&epfile->mutex);
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100874 return ret;
Robert Baldyga48968f82014-03-10 09:33:37 +0100875
876error_lock:
877 spin_unlock_irq(&epfile->ffs->eps_lock);
878 mutex_unlock(&epfile->mutex);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200879error:
880 kfree(data);
881 return ret;
882}
883
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200884static ssize_t
885ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
886 loff_t *ptr)
887{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100888 struct ffs_io_data io_data;
889
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200890 ENTER();
891
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100892 io_data.aio = false;
893 io_data.read = false;
894 io_data.buf = (char * __user)buf;
895 io_data.len = len;
896
897 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200898}
899
900static ssize_t
901ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
902{
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100903 struct ffs_io_data io_data;
904
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200905 ENTER();
906
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100907 io_data.aio = false;
908 io_data.read = true;
909 io_data.buf = buf;
910 io_data.len = len;
911
912 return ffs_epfile_io(file, &io_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200913}
914
915static int
916ffs_epfile_open(struct inode *inode, struct file *file)
917{
918 struct ffs_epfile *epfile = inode->i_private;
919
920 ENTER();
921
922 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
923 return -ENODEV;
924
925 file->private_data = epfile;
926 ffs_data_opened(epfile->ffs);
927
928 return 0;
929}
930
Robert Baldyga2e4c7552014-02-10 10:42:44 +0100931static int ffs_aio_cancel(struct kiocb *kiocb)
932{
933 struct ffs_io_data *io_data = kiocb->private;
934 struct ffs_epfile *epfile = kiocb->ki_filp->private_data;
935 int value;
936
937 ENTER();
938
939 spin_lock_irq(&epfile->ffs->eps_lock);
940
941 if (likely(io_data && io_data->ep && io_data->req))
942 value = usb_ep_dequeue(io_data->ep, io_data->req);
943 else
944 value = -EINVAL;
945
946 spin_unlock_irq(&epfile->ffs->eps_lock);
947
948 return value;
949}
950
951static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb,
952 const struct iovec *iovec,
953 unsigned long nr_segs, loff_t loff)
954{
955 struct ffs_io_data *io_data;
956
957 ENTER();
958
959 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
960 if (unlikely(!io_data))
961 return -ENOMEM;
962
963 io_data->aio = true;
964 io_data->read = false;
965 io_data->kiocb = kiocb;
966 io_data->iovec = iovec;
967 io_data->nr_segs = nr_segs;
968 io_data->len = kiocb->ki_nbytes;
969 io_data->mm = current->mm;
970
971 kiocb->private = io_data;
972
973 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
974
975 return ffs_epfile_io(kiocb->ki_filp, io_data);
976}
977
978static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb,
979 const struct iovec *iovec,
980 unsigned long nr_segs, loff_t loff)
981{
982 struct ffs_io_data *io_data;
983 struct iovec *iovec_copy;
984
985 ENTER();
986
987 iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL);
988 if (unlikely(!iovec_copy))
989 return -ENOMEM;
990
991 memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs);
992
993 io_data = kmalloc(sizeof(*io_data), GFP_KERNEL);
994 if (unlikely(!io_data)) {
995 kfree(iovec_copy);
996 return -ENOMEM;
997 }
998
999 io_data->aio = true;
1000 io_data->read = true;
1001 io_data->kiocb = kiocb;
1002 io_data->iovec = iovec_copy;
1003 io_data->nr_segs = nr_segs;
1004 io_data->len = kiocb->ki_nbytes;
1005 io_data->mm = current->mm;
1006
1007 kiocb->private = io_data;
1008
1009 kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
1010
1011 return ffs_epfile_io(kiocb->ki_filp, io_data);
1012}
1013
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001014static int
1015ffs_epfile_release(struct inode *inode, struct file *file)
1016{
1017 struct ffs_epfile *epfile = inode->i_private;
1018
1019 ENTER();
1020
1021 ffs_data_closed(epfile->ffs);
1022
1023 return 0;
1024}
1025
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001026static long ffs_epfile_ioctl(struct file *file, unsigned code,
1027 unsigned long value)
1028{
1029 struct ffs_epfile *epfile = file->private_data;
1030 int ret;
1031
1032 ENTER();
1033
1034 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
1035 return -ENODEV;
1036
1037 spin_lock_irq(&epfile->ffs->eps_lock);
1038 if (likely(epfile->ep)) {
1039 switch (code) {
1040 case FUNCTIONFS_FIFO_STATUS:
1041 ret = usb_ep_fifo_status(epfile->ep->ep);
1042 break;
1043 case FUNCTIONFS_FIFO_FLUSH:
1044 usb_ep_fifo_flush(epfile->ep->ep);
1045 ret = 0;
1046 break;
1047 case FUNCTIONFS_CLEAR_HALT:
1048 ret = usb_ep_clear_halt(epfile->ep->ep);
1049 break;
1050 case FUNCTIONFS_ENDPOINT_REVMAP:
1051 ret = epfile->ep->num;
1052 break;
1053 default:
1054 ret = -ENOTTY;
1055 }
1056 } else {
1057 ret = -ENODEV;
1058 }
1059 spin_unlock_irq(&epfile->ffs->eps_lock);
1060
1061 return ret;
1062}
1063
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001064static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001065 .llseek = no_llseek,
1066
1067 .open = ffs_epfile_open,
1068 .write = ffs_epfile_write,
1069 .read = ffs_epfile_read,
Robert Baldyga2e4c7552014-02-10 10:42:44 +01001070 .aio_write = ffs_epfile_aio_write,
1071 .aio_read = ffs_epfile_aio_read,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001072 .release = ffs_epfile_release,
1073 .unlocked_ioctl = ffs_epfile_ioctl,
1074};
1075
1076
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001077/* File system and super block operations ***********************************/
1078
1079/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001080 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001081 * function configuration then later for event monitoring.
1082 */
1083
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001084static struct inode *__must_check
1085ffs_sb_make_inode(struct super_block *sb, void *data,
1086 const struct file_operations *fops,
1087 const struct inode_operations *iops,
1088 struct ffs_file_perms *perms)
1089{
1090 struct inode *inode;
1091
1092 ENTER();
1093
1094 inode = new_inode(sb);
1095
1096 if (likely(inode)) {
1097 struct timespec current_time = CURRENT_TIME;
1098
Al Viro12ba8d12010-10-27 04:19:36 +01001099 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001100 inode->i_mode = perms->mode;
1101 inode->i_uid = perms->uid;
1102 inode->i_gid = perms->gid;
1103 inode->i_atime = current_time;
1104 inode->i_mtime = current_time;
1105 inode->i_ctime = current_time;
1106 inode->i_private = data;
1107 if (fops)
1108 inode->i_fop = fops;
1109 if (iops)
1110 inode->i_op = iops;
1111 }
1112
1113 return inode;
1114}
1115
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001116/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001117static struct inode *ffs_sb_create_file(struct super_block *sb,
1118 const char *name, void *data,
1119 const struct file_operations *fops,
1120 struct dentry **dentry_p)
1121{
1122 struct ffs_data *ffs = sb->s_fs_info;
1123 struct dentry *dentry;
1124 struct inode *inode;
1125
1126 ENTER();
1127
1128 dentry = d_alloc_name(sb->s_root, name);
1129 if (unlikely(!dentry))
1130 return NULL;
1131
1132 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1133 if (unlikely(!inode)) {
1134 dput(dentry);
1135 return NULL;
1136 }
1137
1138 d_add(dentry, inode);
1139 if (dentry_p)
1140 *dentry_p = dentry;
1141
1142 return inode;
1143}
1144
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001145/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001146static const struct super_operations ffs_sb_operations = {
1147 .statfs = simple_statfs,
1148 .drop_inode = generic_delete_inode,
1149};
1150
1151struct ffs_sb_fill_data {
1152 struct ffs_file_perms perms;
1153 umode_t root_mode;
1154 const char *dev_name;
Al Viro2606b282013-09-20 17:14:21 +01001155 struct ffs_data *ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001156};
1157
1158static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1159{
1160 struct ffs_sb_fill_data *data = _data;
1161 struct inode *inode;
Al Viro2606b282013-09-20 17:14:21 +01001162 struct ffs_data *ffs = data->ffs_data;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001163
1164 ENTER();
1165
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166 ffs->sb = sb;
Al Viro2606b282013-09-20 17:14:21 +01001167 data->ffs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001168 sb->s_fs_info = ffs;
1169 sb->s_blocksize = PAGE_CACHE_SIZE;
1170 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1171 sb->s_magic = FUNCTIONFS_MAGIC;
1172 sb->s_op = &ffs_sb_operations;
1173 sb->s_time_gran = 1;
1174
1175 /* Root inode */
1176 data->perms.mode = data->root_mode;
1177 inode = ffs_sb_make_inode(sb, NULL,
1178 &simple_dir_operations,
1179 &simple_dir_inode_operations,
1180 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001181 sb->s_root = d_make_root(inode);
1182 if (unlikely(!sb->s_root))
Al Viro2606b282013-09-20 17:14:21 +01001183 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001184
1185 /* EP0 file */
1186 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1187 &ffs_ep0_operations, NULL)))
Al Viro2606b282013-09-20 17:14:21 +01001188 return -ENOMEM;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001189
1190 return 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001191}
1192
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001193static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1194{
1195 ENTER();
1196
1197 if (!opts || !*opts)
1198 return 0;
1199
1200 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001201 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001202 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001203
1204 /* Option limit */
1205 comma = strchr(opts, ',');
1206 if (comma)
1207 *comma = 0;
1208
1209 /* Value limit */
1210 eq = strchr(opts, '=');
1211 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001212 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001213 return -EINVAL;
1214 }
1215 *eq = 0;
1216
1217 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001218 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001219 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001220 return -EINVAL;
1221 }
1222
1223 /* Interpret option */
1224 switch (eq - opts) {
1225 case 5:
1226 if (!memcmp(opts, "rmode", 5))
1227 data->root_mode = (value & 0555) | S_IFDIR;
1228 else if (!memcmp(opts, "fmode", 5))
1229 data->perms.mode = (value & 0666) | S_IFREG;
1230 else
1231 goto invalid;
1232 break;
1233
1234 case 4:
1235 if (!memcmp(opts, "mode", 4)) {
1236 data->root_mode = (value & 0555) | S_IFDIR;
1237 data->perms.mode = (value & 0666) | S_IFREG;
1238 } else {
1239 goto invalid;
1240 }
1241 break;
1242
1243 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001244 if (!memcmp(opts, "uid", 3)) {
1245 data->perms.uid = make_kuid(current_user_ns(), value);
1246 if (!uid_valid(data->perms.uid)) {
1247 pr_err("%s: unmapped value: %lu\n", opts, value);
1248 return -EINVAL;
1249 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001250 } else if (!memcmp(opts, "gid", 3)) {
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001251 data->perms.gid = make_kgid(current_user_ns(), value);
1252 if (!gid_valid(data->perms.gid)) {
1253 pr_err("%s: unmapped value: %lu\n", opts, value);
1254 return -EINVAL;
1255 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001256 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001257 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -08001258 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001259 break;
1260
1261 default:
1262invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001263 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001264 return -EINVAL;
1265 }
1266
1267 /* Next iteration */
1268 if (!comma)
1269 break;
1270 opts = comma + 1;
1271 }
1272
1273 return 0;
1274}
1275
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001276/* "mount -t functionfs dev_name /dev/function" ends up here */
1277
Al Virofc14f2f2010-07-25 01:48:30 +04001278static struct dentry *
1279ffs_fs_mount(struct file_system_type *t, int flags,
1280 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001281{
1282 struct ffs_sb_fill_data data = {
1283 .perms = {
1284 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001285 .uid = GLOBAL_ROOT_UID,
1286 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001287 },
1288 .root_mode = S_IFDIR | 0500,
1289 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001290 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001291 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001292 void *ffs_dev;
Al Viro2606b282013-09-20 17:14:21 +01001293 struct ffs_data *ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001294
1295 ENTER();
1296
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001297 ret = ffs_fs_parse_opts(&data, opts);
1298 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001299 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001300
Al Viro2606b282013-09-20 17:14:21 +01001301 ffs = ffs_data_new();
1302 if (unlikely(!ffs))
1303 return ERR_PTR(-ENOMEM);
1304 ffs->file_perms = data.perms;
1305
1306 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1307 if (unlikely(!ffs->dev_name)) {
1308 ffs_data_put(ffs);
1309 return ERR_PTR(-ENOMEM);
1310 }
1311
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001312 ffs_dev = ffs_acquire_dev(dev_name);
Al Viro2606b282013-09-20 17:14:21 +01001313 if (IS_ERR(ffs_dev)) {
1314 ffs_data_put(ffs);
1315 return ERR_CAST(ffs_dev);
1316 }
1317 ffs->private_data = ffs_dev;
1318 data.ffs_data = ffs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001319
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001320 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
Al Viro2606b282013-09-20 17:14:21 +01001321 if (IS_ERR(rv) && data.ffs_data) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001322 ffs_release_dev(data.ffs_data);
Al Viro2606b282013-09-20 17:14:21 +01001323 ffs_data_put(data.ffs_data);
1324 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001325 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001326}
1327
1328static void
1329ffs_fs_kill_sb(struct super_block *sb)
1330{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001331 ENTER();
1332
1333 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001334 if (sb->s_fs_info) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001335 ffs_release_dev(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001336 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001337 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001338}
1339
1340static struct file_system_type ffs_fs_type = {
1341 .owner = THIS_MODULE,
1342 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001343 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001344 .kill_sb = ffs_fs_kill_sb,
1345};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001346MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001347
1348
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001349/* Driver's main init/cleanup functions *************************************/
1350
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001351static int functionfs_init(void)
1352{
1353 int ret;
1354
1355 ENTER();
1356
1357 ret = register_filesystem(&ffs_fs_type);
1358 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001359 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001360 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001361 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001362
1363 return ret;
1364}
1365
1366static void functionfs_cleanup(void)
1367{
1368 ENTER();
1369
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001370 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001371 unregister_filesystem(&ffs_fs_type);
1372}
1373
1374
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001375/* ffs_data and ffs_function construction and destruction code **************/
1376
1377static void ffs_data_clear(struct ffs_data *ffs);
1378static void ffs_data_reset(struct ffs_data *ffs);
1379
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001380static void ffs_data_get(struct ffs_data *ffs)
1381{
1382 ENTER();
1383
1384 atomic_inc(&ffs->ref);
1385}
1386
1387static void ffs_data_opened(struct ffs_data *ffs)
1388{
1389 ENTER();
1390
1391 atomic_inc(&ffs->ref);
1392 atomic_inc(&ffs->opened);
1393}
1394
1395static void ffs_data_put(struct ffs_data *ffs)
1396{
1397 ENTER();
1398
1399 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001400 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001401 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001402 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001404 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001405 kfree(ffs);
1406 }
1407}
1408
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001409static void ffs_data_closed(struct ffs_data *ffs)
1410{
1411 ENTER();
1412
1413 if (atomic_dec_and_test(&ffs->opened)) {
1414 ffs->state = FFS_CLOSING;
1415 ffs_data_reset(ffs);
1416 }
1417
1418 ffs_data_put(ffs);
1419}
1420
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001421static struct ffs_data *ffs_data_new(void)
1422{
1423 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1424 if (unlikely(!ffs))
Felipe Balbif8800d42013-12-12 12:15:43 -06001425 return NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001426
1427 ENTER();
1428
1429 atomic_set(&ffs->ref, 1);
1430 atomic_set(&ffs->opened, 0);
1431 ffs->state = FFS_READ_DESCRIPTORS;
1432 mutex_init(&ffs->mutex);
1433 spin_lock_init(&ffs->eps_lock);
1434 init_waitqueue_head(&ffs->ev.waitq);
1435 init_completion(&ffs->ep0req_completion);
1436
1437 /* XXX REVISIT need to update it in some places, or do we? */
1438 ffs->ev.can_stall = 1;
1439
1440 return ffs;
1441}
1442
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001443static void ffs_data_clear(struct ffs_data *ffs)
1444{
1445 ENTER();
1446
1447 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01001448 ffs_closed(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001449
1450 BUG_ON(ffs->gadget);
1451
1452 if (ffs->epfiles)
1453 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1454
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301455 kfree(ffs->raw_descs_data);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001456 kfree(ffs->raw_strings);
1457 kfree(ffs->stringtabs);
1458}
1459
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001460static void ffs_data_reset(struct ffs_data *ffs)
1461{
1462 ENTER();
1463
1464 ffs_data_clear(ffs);
1465
1466 ffs->epfiles = NULL;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301467 ffs->raw_descs_data = NULL;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001468 ffs->raw_descs = NULL;
1469 ffs->raw_strings = NULL;
1470 ffs->stringtabs = NULL;
1471
1472 ffs->raw_descs_length = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001473 ffs->fs_descs_count = 0;
1474 ffs->hs_descs_count = 0;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301475 ffs->ss_descs_count = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001476
1477 ffs->strings_count = 0;
1478 ffs->interfaces_count = 0;
1479 ffs->eps_count = 0;
1480
1481 ffs->ev.count = 0;
1482
1483 ffs->state = FFS_READ_DESCRIPTORS;
1484 ffs->setup_state = FFS_NO_SETUP;
1485 ffs->flags = 0;
1486}
1487
1488
1489static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1490{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001491 struct usb_gadget_strings **lang;
1492 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001493
1494 ENTER();
1495
1496 if (WARN_ON(ffs->state != FFS_ACTIVE
1497 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1498 return -EBADFD;
1499
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001500 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1501 if (unlikely(first_id < 0))
1502 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001503
1504 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1505 if (unlikely(!ffs->ep0req))
1506 return -ENOMEM;
1507 ffs->ep0req->complete = ffs_ep0_complete;
1508 ffs->ep0req->context = ffs;
1509
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001510 lang = ffs->stringtabs;
1511 for (lang = ffs->stringtabs; *lang; ++lang) {
1512 struct usb_string *str = (*lang)->strings;
1513 int id = first_id;
1514 for (; str->s; ++id, ++str)
1515 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001516 }
1517
1518 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001519 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001520 return 0;
1521}
1522
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001523static void functionfs_unbind(struct ffs_data *ffs)
1524{
1525 ENTER();
1526
1527 if (!WARN_ON(!ffs->gadget)) {
1528 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1529 ffs->ep0req = NULL;
1530 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001531 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001532 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001533 }
1534}
1535
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001536static int ffs_epfiles_create(struct ffs_data *ffs)
1537{
1538 struct ffs_epfile *epfile, *epfiles;
1539 unsigned i, count;
1540
1541 ENTER();
1542
1543 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001544 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001545 if (!epfiles)
1546 return -ENOMEM;
1547
1548 epfile = epfiles;
1549 for (i = 1; i <= count; ++i, ++epfile) {
1550 epfile->ffs = ffs;
1551 mutex_init(&epfile->mutex);
1552 init_waitqueue_head(&epfile->wait);
1553 sprintf(epfiles->name, "ep%u", i);
1554 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1555 &ffs_epfile_operations,
1556 &epfile->dentry))) {
1557 ffs_epfiles_destroy(epfiles, i - 1);
1558 return -ENOMEM;
1559 }
1560 }
1561
1562 ffs->epfiles = epfiles;
1563 return 0;
1564}
1565
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001566static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1567{
1568 struct ffs_epfile *epfile = epfiles;
1569
1570 ENTER();
1571
1572 for (; count; --count, ++epfile) {
1573 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1574 waitqueue_active(&epfile->wait));
1575 if (epfile->dentry) {
1576 d_delete(epfile->dentry);
1577 dput(epfile->dentry);
1578 epfile->dentry = NULL;
1579 }
1580 }
1581
1582 kfree(epfiles);
1583}
1584
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01001585
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001586static void ffs_func_eps_disable(struct ffs_function *func)
1587{
1588 struct ffs_ep *ep = func->eps;
1589 struct ffs_epfile *epfile = func->ffs->epfiles;
1590 unsigned count = func->ffs->eps_count;
1591 unsigned long flags;
1592
1593 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1594 do {
1595 /* pending requests get nuked */
1596 if (likely(ep->ep))
1597 usb_ep_disable(ep->ep);
1598 epfile->ep = NULL;
1599
1600 ++ep;
1601 ++epfile;
1602 } while (--count);
1603 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1604}
1605
1606static int ffs_func_eps_enable(struct ffs_function *func)
1607{
1608 struct ffs_data *ffs = func->ffs;
1609 struct ffs_ep *ep = func->eps;
1610 struct ffs_epfile *epfile = ffs->epfiles;
1611 unsigned count = ffs->eps_count;
1612 unsigned long flags;
1613 int ret = 0;
1614
1615 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1616 do {
1617 struct usb_endpoint_descriptor *ds;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301618 int desc_idx;
1619
1620 if (ffs->gadget->speed == USB_SPEED_SUPER)
1621 desc_idx = 2;
1622 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1623 desc_idx = 1;
1624 else
1625 desc_idx = 0;
1626
1627 /* fall-back to lower speed if desc missing for current speed */
1628 do {
1629 ds = ep->descs[desc_idx];
1630 } while (!ds && --desc_idx >= 0);
1631
1632 if (!ds) {
1633 ret = -EINVAL;
1634 break;
1635 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001636
1637 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001638 ep->ep->desc = ds;
1639 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001640 if (likely(!ret)) {
1641 epfile->ep = ep;
1642 epfile->in = usb_endpoint_dir_in(ds);
1643 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1644 } else {
1645 break;
1646 }
1647
1648 wake_up(&epfile->wait);
1649
1650 ++ep;
1651 ++epfile;
1652 } while (--count);
1653 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1654
1655 return ret;
1656}
1657
1658
1659/* Parsing and building descriptors and strings *****************************/
1660
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001661/*
1662 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001663 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001664 * required by given configuration. Returns address after the
1665 * descriptor or NULL if data is invalid.
1666 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001667
1668enum ffs_entity_type {
1669 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1670};
1671
1672typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1673 u8 *valuep,
1674 struct usb_descriptor_header *desc,
1675 void *priv);
1676
1677static int __must_check ffs_do_desc(char *data, unsigned len,
1678 ffs_entity_callback entity, void *priv)
1679{
1680 struct usb_descriptor_header *_ds = (void *)data;
1681 u8 length;
1682 int ret;
1683
1684 ENTER();
1685
1686 /* At least two bytes are required: length and type */
1687 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001688 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001689 return -EINVAL;
1690 }
1691
1692 /* If we have at least as many bytes as the descriptor takes? */
1693 length = _ds->bLength;
1694 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001695 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001696 return -EINVAL;
1697 }
1698
1699#define __entity_check_INTERFACE(val) 1
1700#define __entity_check_STRING(val) (val)
1701#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1702#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001703 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001704 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001705 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001706 return -EINVAL; \
1707 } \
1708 ret = entity(FFS_ ##type, &val, _ds, priv); \
1709 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001710 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001711 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001712 return ret; \
1713 } \
1714 } while (0)
1715
1716 /* Parse descriptor depending on type. */
1717 switch (_ds->bDescriptorType) {
1718 case USB_DT_DEVICE:
1719 case USB_DT_CONFIG:
1720 case USB_DT_STRING:
1721 case USB_DT_DEVICE_QUALIFIER:
1722 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001723 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001724 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001725 return -EINVAL;
1726
1727 case USB_DT_INTERFACE: {
1728 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001729 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001730 if (length != sizeof *ds)
1731 goto inv_length;
1732
1733 __entity(INTERFACE, ds->bInterfaceNumber);
1734 if (ds->iInterface)
1735 __entity(STRING, ds->iInterface);
1736 }
1737 break;
1738
1739 case USB_DT_ENDPOINT: {
1740 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001741 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001742 if (length != USB_DT_ENDPOINT_SIZE &&
1743 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1744 goto inv_length;
1745 __entity(ENDPOINT, ds->bEndpointAddress);
1746 }
1747 break;
1748
Koen Beel560f1182012-05-30 20:43:37 +02001749 case HID_DT_HID:
1750 pr_vdebug("hid descriptor\n");
1751 if (length != sizeof(struct hid_descriptor))
1752 goto inv_length;
1753 break;
1754
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001755 case USB_DT_OTG:
1756 if (length != sizeof(struct usb_otg_descriptor))
1757 goto inv_length;
1758 break;
1759
1760 case USB_DT_INTERFACE_ASSOCIATION: {
1761 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001762 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001763 if (length != sizeof *ds)
1764 goto inv_length;
1765 if (ds->iFunction)
1766 __entity(STRING, ds->iFunction);
1767 }
1768 break;
1769
Manu Gautam8d4e8972014-02-28 16:50:22 +05301770 case USB_DT_SS_ENDPOINT_COMP:
1771 pr_vdebug("EP SS companion descriptor\n");
1772 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1773 goto inv_length;
1774 break;
1775
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001776 case USB_DT_OTHER_SPEED_CONFIG:
1777 case USB_DT_INTERFACE_POWER:
1778 case USB_DT_DEBUG:
1779 case USB_DT_SECURITY:
1780 case USB_DT_CS_RADIO_CONTROL:
1781 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001782 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001783 return -EINVAL;
1784
1785 default:
1786 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001787 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001788 return -EINVAL;
1789
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001790inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001791 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001792 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001793 return -EINVAL;
1794 }
1795
1796#undef __entity
1797#undef __entity_check_DESCRIPTOR
1798#undef __entity_check_INTERFACE
1799#undef __entity_check_STRING
1800#undef __entity_check_ENDPOINT
1801
1802 return length;
1803}
1804
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001805static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1806 ffs_entity_callback entity, void *priv)
1807{
1808 const unsigned _len = len;
1809 unsigned long num = 0;
1810
1811 ENTER();
1812
1813 for (;;) {
1814 int ret;
1815
1816 if (num == count)
1817 data = NULL;
1818
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001819 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001820 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1821 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001822 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001823 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001824 return ret;
1825 }
1826
1827 if (!data)
1828 return _len - len;
1829
1830 ret = ffs_do_desc(data, len, entity, priv);
1831 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001832 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001833 return ret;
1834 }
1835
1836 len -= ret;
1837 data += ret;
1838 ++num;
1839 }
1840}
1841
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001842static int __ffs_data_do_entity(enum ffs_entity_type type,
1843 u8 *valuep, struct usb_descriptor_header *desc,
1844 void *priv)
1845{
1846 struct ffs_data *ffs = priv;
1847
1848 ENTER();
1849
1850 switch (type) {
1851 case FFS_DESCRIPTOR:
1852 break;
1853
1854 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001855 /*
1856 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001857 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001858 * "n+1" interfaces.
1859 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001860 if (*valuep >= ffs->interfaces_count)
1861 ffs->interfaces_count = *valuep + 1;
1862 break;
1863
1864 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001865 /*
1866 * Strings are indexed from 1 (0 is magic ;) reserved
1867 * for languages list or some such)
1868 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001869 if (*valuep > ffs->strings_count)
1870 ffs->strings_count = *valuep;
1871 break;
1872
1873 case FFS_ENDPOINT:
1874 /* Endpoints are indexed from 1 as well. */
1875 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1876 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1877 break;
1878 }
1879
1880 return 0;
1881}
1882
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001883static int __ffs_data_got_descs(struct ffs_data *ffs,
1884 char *const _data, size_t len)
1885{
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301886 char *data = _data, *raw_descs;
1887 unsigned counts[3], flags;
1888 int ret = -EINVAL, i;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001889
1890 ENTER();
1891
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301892 if (get_unaligned_le32(data + 4) != len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001893 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001894
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301895 switch (get_unaligned_le32(data)) {
1896 case FUNCTIONFS_DESCRIPTORS_MAGIC:
1897 flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
Manu Gautam8d4e8972014-02-28 16:50:22 +05301898 data += 8;
1899 len -= 8;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301900 break;
1901 case FUNCTIONFS_DESCRIPTORS_MAGIC_V2:
1902 flags = get_unaligned_le32(data + 8);
1903 if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
1904 FUNCTIONFS_HAS_HS_DESC |
1905 FUNCTIONFS_HAS_SS_DESC)) {
1906 ret = -ENOSYS;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001907 goto error;
1908 }
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301909 data += 12;
1910 len -= 12;
1911 break;
1912 default:
1913 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001914 }
1915
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301916 /* Read fs_count, hs_count and ss_count (if present) */
1917 for (i = 0; i < 3; ++i) {
1918 if (!(flags & (1 << i))) {
1919 counts[i] = 0;
1920 } else if (len < 4) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001921 goto error;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301922 } else {
1923 counts[i] = get_unaligned_le32(data);
1924 data += 4;
1925 len -= 4;
1926 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001927 }
1928
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301929 /* Read descriptors */
1930 raw_descs = data;
1931 for (i = 0; i < 3; ++i) {
1932 if (!counts[i])
1933 continue;
1934 ret = ffs_do_descs(counts[i], data, len,
1935 __ffs_data_do_entity, ffs);
1936 if (ret < 0)
1937 goto error;
1938 data += ret;
1939 len -= ret;
1940 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001941
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05301942 if (raw_descs == data || len) {
1943 ret = -EINVAL;
1944 goto error;
1945 }
1946
1947 ffs->raw_descs_data = _data;
1948 ffs->raw_descs = raw_descs;
1949 ffs->raw_descs_length = data - raw_descs;
1950 ffs->fs_descs_count = counts[0];
1951 ffs->hs_descs_count = counts[1];
1952 ffs->ss_descs_count = counts[2];
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001953
1954 return 0;
1955
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001956error:
1957 kfree(_data);
1958 return ret;
1959}
1960
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001961static int __ffs_data_got_strings(struct ffs_data *ffs,
1962 char *const _data, size_t len)
1963{
1964 u32 str_count, needed_count, lang_count;
1965 struct usb_gadget_strings **stringtabs, *t;
1966 struct usb_string *strings, *s;
1967 const char *data = _data;
1968
1969 ENTER();
1970
1971 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1972 get_unaligned_le32(data + 4) != len))
1973 goto error;
1974 str_count = get_unaligned_le32(data + 8);
1975 lang_count = get_unaligned_le32(data + 12);
1976
1977 /* if one is zero the other must be zero */
1978 if (unlikely(!str_count != !lang_count))
1979 goto error;
1980
1981 /* Do we have at least as many strings as descriptors need? */
1982 needed_count = ffs->strings_count;
1983 if (unlikely(str_count < needed_count))
1984 goto error;
1985
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001986 /*
1987 * If we don't need any strings just return and free all
1988 * memory.
1989 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001990 if (!needed_count) {
1991 kfree(_data);
1992 return 0;
1993 }
1994
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001995 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001996 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001997 unsigned i = 0;
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01001998 vla_group(d);
1999 vla_item(d, struct usb_gadget_strings *, stringtabs,
2000 lang_count + 1);
2001 vla_item(d, struct usb_gadget_strings, stringtab, lang_count);
2002 vla_item(d, struct usb_string, strings,
2003 lang_count*(needed_count+1));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002004
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002005 char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2006
2007 if (unlikely(!vlabuf)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002008 kfree(_data);
2009 return -ENOMEM;
2010 }
2011
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002012 /* Initialize the VLA pointers */
2013 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2014 t = vla_ptr(vlabuf, d, stringtab);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002015 i = lang_count;
2016 do {
2017 *stringtabs++ = t++;
2018 } while (--i);
2019 *stringtabs = NULL;
2020
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002021 /* stringtabs = vlabuf = d_stringtabs for later kfree */
2022 stringtabs = vla_ptr(vlabuf, d, stringtabs);
2023 t = vla_ptr(vlabuf, d, stringtab);
2024 s = vla_ptr(vlabuf, d, strings);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002025 strings = s;
2026 }
2027
2028 /* For each language */
2029 data += 16;
2030 len -= 16;
2031
2032 do { /* lang_count > 0 so we can use do-while */
2033 unsigned needed = needed_count;
2034
2035 if (unlikely(len < 3))
2036 goto error_free;
2037 t->language = get_unaligned_le16(data);
2038 t->strings = s;
2039 ++t;
2040
2041 data += 2;
2042 len -= 2;
2043
2044 /* For each string */
2045 do { /* str_count > 0 so we can use do-while */
2046 size_t length = strnlen(data, len);
2047
2048 if (unlikely(length == len))
2049 goto error_free;
2050
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002051 /*
2052 * User may provide more strings then we need,
2053 * if that's the case we simply ignore the
2054 * rest
2055 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002056 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002057 /*
2058 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002059 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002060 * now just leave garbage here.
2061 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002062 s->s = data;
2063 --needed;
2064 ++s;
2065 }
2066
2067 data += length + 1;
2068 len -= length + 1;
2069 } while (--str_count);
2070
2071 s->id = 0; /* terminator */
2072 s->s = NULL;
2073 ++s;
2074
2075 } while (--lang_count);
2076
2077 /* Some garbage left? */
2078 if (unlikely(len))
2079 goto error_free;
2080
2081 /* Done! */
2082 ffs->stringtabs = stringtabs;
2083 ffs->raw_strings = _data;
2084
2085 return 0;
2086
2087error_free:
2088 kfree(stringtabs);
2089error:
2090 kfree(_data);
2091 return -EINVAL;
2092}
2093
2094
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002095/* Events handling and management *******************************************/
2096
2097static void __ffs_event_add(struct ffs_data *ffs,
2098 enum usb_functionfs_event_type type)
2099{
2100 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2101 int neg = 0;
2102
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002103 /*
2104 * Abort any unhandled setup
2105 *
2106 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002107 * of ffs->setup_state without holding the lock because when
2108 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002109 * the source does nothing.
2110 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002111 if (ffs->setup_state == FFS_SETUP_PENDING)
Michal Nazarewicze46318a2014-02-10 10:42:40 +01002112 ffs->setup_state = FFS_SETUP_CANCELLED;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002113
2114 switch (type) {
2115 case FUNCTIONFS_RESUME:
2116 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002117 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002118 case FUNCTIONFS_SUSPEND:
2119 case FUNCTIONFS_SETUP:
2120 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002121 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002122 break;
2123
2124 case FUNCTIONFS_BIND:
2125 case FUNCTIONFS_UNBIND:
2126 case FUNCTIONFS_DISABLE:
2127 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002128 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002129 rem_type1 = FUNCTIONFS_SUSPEND;
2130 rem_type2 = FUNCTIONFS_RESUME;
2131 neg = 1;
2132 break;
2133
2134 default:
2135 BUG();
2136 }
2137
2138 {
2139 u8 *ev = ffs->ev.types, *out = ev;
2140 unsigned n = ffs->ev.count;
2141 for (; n; --n, ++ev)
2142 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2143 *out++ = *ev;
2144 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002145 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002146 ffs->ev.count = out - ffs->ev.types;
2147 }
2148
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002149 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002150 ffs->ev.types[ffs->ev.count++] = type;
2151 wake_up_locked(&ffs->ev.waitq);
2152}
2153
2154static void ffs_event_add(struct ffs_data *ffs,
2155 enum usb_functionfs_event_type type)
2156{
2157 unsigned long flags;
2158 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2159 __ffs_event_add(ffs, type);
2160 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2161}
2162
2163
2164/* Bind/unbind USB function hooks *******************************************/
2165
2166static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2167 struct usb_descriptor_header *desc,
2168 void *priv)
2169{
2170 struct usb_endpoint_descriptor *ds = (void *)desc;
2171 struct ffs_function *func = priv;
2172 struct ffs_ep *ffs_ep;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302173 unsigned ep_desc_id, idx;
2174 static const char *speed_names[] = { "full", "high", "super" };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002175
2176 if (type != FFS_DESCRIPTOR)
2177 return 0;
2178
Manu Gautam8d4e8972014-02-28 16:50:22 +05302179 /*
2180 * If ss_descriptors is not NULL, we are reading super speed
2181 * descriptors; if hs_descriptors is not NULL, we are reading high
2182 * speed descriptors; otherwise, we are reading full speed
2183 * descriptors.
2184 */
2185 if (func->function.ss_descriptors) {
2186 ep_desc_id = 2;
2187 func->function.ss_descriptors[(long)valuep] = desc;
2188 } else if (func->function.hs_descriptors) {
2189 ep_desc_id = 1;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002190 func->function.hs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302191 } else {
2192 ep_desc_id = 0;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002193 func->function.fs_descriptors[(long)valuep] = desc;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302194 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002195
2196 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2197 return 0;
2198
2199 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2200 ffs_ep = func->eps + idx;
2201
Manu Gautam8d4e8972014-02-28 16:50:22 +05302202 if (unlikely(ffs_ep->descs[ep_desc_id])) {
2203 pr_err("two %sspeed descriptors for EP %d\n",
2204 speed_names[ep_desc_id],
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002205 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002206 return -EINVAL;
2207 }
Manu Gautam8d4e8972014-02-28 16:50:22 +05302208 ffs_ep->descs[ep_desc_id] = ds;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002209
2210 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2211 if (ffs_ep->ep) {
2212 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2213 if (!ds->wMaxPacketSize)
2214 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2215 } else {
2216 struct usb_request *req;
2217 struct usb_ep *ep;
2218
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002219 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002220 ep = usb_ep_autoconfig(func->gadget, ds);
2221 if (unlikely(!ep))
2222 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08002223 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002224
2225 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2226 if (unlikely(!req))
2227 return -ENOMEM;
2228
2229 ffs_ep->ep = ep;
2230 ffs_ep->req = req;
2231 func->eps_revmap[ds->bEndpointAddress &
2232 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2233 }
2234 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2235
2236 return 0;
2237}
2238
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002239static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2240 struct usb_descriptor_header *desc,
2241 void *priv)
2242{
2243 struct ffs_function *func = priv;
2244 unsigned idx;
2245 u8 newValue;
2246
2247 switch (type) {
2248 default:
2249 case FFS_DESCRIPTOR:
2250 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2251 return 0;
2252
2253 case FFS_INTERFACE:
2254 idx = *valuep;
2255 if (func->interfaces_nums[idx] < 0) {
2256 int id = usb_interface_id(func->conf, &func->function);
2257 if (unlikely(id < 0))
2258 return id;
2259 func->interfaces_nums[idx] = id;
2260 }
2261 newValue = func->interfaces_nums[idx];
2262 break;
2263
2264 case FFS_STRING:
2265 /* String' IDs are allocated when fsf_data is bound to cdev */
2266 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2267 break;
2268
2269 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002270 /*
2271 * USB_DT_ENDPOINT are handled in
2272 * __ffs_func_bind_do_descs().
2273 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002274 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2275 return 0;
2276
2277 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2278 if (unlikely(!func->eps[idx].ep))
2279 return -EINVAL;
2280
2281 {
2282 struct usb_endpoint_descriptor **descs;
2283 descs = func->eps[idx].descs;
2284 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2285 }
2286 break;
2287 }
2288
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002289 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002290 *valuep = newValue;
2291 return 0;
2292}
2293
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002294static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2295 struct usb_configuration *c)
2296{
2297 struct ffs_function *func = ffs_func_from_usb(f);
2298 struct f_fs_opts *ffs_opts =
2299 container_of(f->fi, struct f_fs_opts, func_inst);
2300 int ret;
2301
2302 ENTER();
2303
2304 /*
2305 * Legacy gadget triggers binding in functionfs_ready_callback,
2306 * which already uses locking; taking the same lock here would
2307 * cause a deadlock.
2308 *
2309 * Configfs-enabled gadgets however do need ffs_dev_lock.
2310 */
2311 if (!ffs_opts->no_configfs)
2312 ffs_dev_lock();
2313 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2314 func->ffs = ffs_opts->dev->ffs_data;
2315 if (!ffs_opts->no_configfs)
2316 ffs_dev_unlock();
2317 if (ret)
2318 return ERR_PTR(ret);
2319
2320 func->conf = c;
2321 func->gadget = c->cdev->gadget;
2322
2323 ffs_data_get(func->ffs);
2324
2325 /*
2326 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2327 * configurations are bound in sequence with list_for_each_entry,
2328 * in each configuration its functions are bound in sequence
2329 * with list_for_each_entry, so we assume no race condition
2330 * with regard to ffs_opts->bound access
2331 */
2332 if (!ffs_opts->refcnt) {
2333 ret = functionfs_bind(func->ffs, c->cdev);
2334 if (ret)
2335 return ERR_PTR(ret);
2336 }
2337 ffs_opts->refcnt++;
2338 func->function.strings = func->ffs->stringtabs;
2339
2340 return ffs_opts;
2341}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002342
2343static int _ffs_func_bind(struct usb_configuration *c,
2344 struct usb_function *f)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002345{
2346 struct ffs_function *func = ffs_func_from_usb(f);
2347 struct ffs_data *ffs = func->ffs;
2348
2349 const int full = !!func->ffs->fs_descs_count;
2350 const int high = gadget_is_dualspeed(func->gadget) &&
2351 func->ffs->hs_descs_count;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302352 const int super = gadget_is_superspeed(func->gadget) &&
2353 func->ffs->ss_descs_count;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002354
Manu Gautam8d4e8972014-02-28 16:50:22 +05302355 int fs_len, hs_len, ret;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002356
2357 /* Make it a single chunk, less management later on */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002358 vla_group(d);
2359 vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count);
2360 vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs,
2361 full ? ffs->fs_descs_count + 1 : 0);
2362 vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs,
2363 high ? ffs->hs_descs_count + 1 : 0);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302364 vla_item_with_sz(d, struct usb_descriptor_header *, ss_descs,
2365 super ? ffs->ss_descs_count + 1 : 0);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002366 vla_item_with_sz(d, short, inums, ffs->interfaces_count);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302367 vla_item_with_sz(d, char, raw_descs, ffs->raw_descs_length);
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002368 char *vlabuf;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002369
2370 ENTER();
2371
Manu Gautam8d4e8972014-02-28 16:50:22 +05302372 /* Has descriptors only for speeds gadget does not support */
2373 if (unlikely(!(full | high | super)))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002374 return -ENOTSUPP;
2375
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002376 /* Allocate a single chunk, less management later on */
2377 vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL);
2378 if (unlikely(!vlabuf))
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002379 return -ENOMEM;
2380
2381 /* Zero */
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002382 memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz);
Michal Nazarewiczac8dde12014-02-28 16:50:23 +05302383 /* Copy descriptors */
2384 memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs,
2385 ffs->raw_descs_length);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302386
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002387 memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz);
2388 for (ret = ffs->eps_count; ret; --ret) {
2389 struct ffs_ep *ptr;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002390
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002391 ptr = vla_ptr(vlabuf, d, eps);
2392 ptr[ret].num = -1;
2393 }
2394
2395 /* Save pointers
2396 * d_eps == vlabuf, func->eps used to kfree vlabuf later
2397 */
2398 func->eps = vla_ptr(vlabuf, d, eps);
2399 func->interfaces_nums = vla_ptr(vlabuf, d, inums);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002400
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002401 /*
2402 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002403 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002404 * numbers without worrying that it may be described later on.
2405 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002406 if (likely(full)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002407 func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302408 fs_len = ffs_do_descs(ffs->fs_descs_count,
2409 vla_ptr(vlabuf, d, raw_descs),
2410 d_raw_descs__sz,
2411 __ffs_func_bind_do_descs, func);
2412 if (unlikely(fs_len < 0)) {
2413 ret = fs_len;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002414 goto error;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302415 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002416 } else {
Manu Gautam8d4e8972014-02-28 16:50:22 +05302417 fs_len = 0;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002418 }
2419
2420 if (likely(high)) {
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002421 func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs);
Manu Gautam8d4e8972014-02-28 16:50:22 +05302422 hs_len = ffs_do_descs(ffs->hs_descs_count,
2423 vla_ptr(vlabuf, d, raw_descs) + fs_len,
2424 d_raw_descs__sz - fs_len,
2425 __ffs_func_bind_do_descs, func);
2426 if (unlikely(hs_len < 0)) {
2427 ret = hs_len;
2428 goto error;
2429 }
2430 } else {
2431 hs_len = 0;
2432 }
2433
2434 if (likely(super)) {
2435 func->function.ss_descriptors = vla_ptr(vlabuf, d, ss_descs);
2436 ret = ffs_do_descs(ffs->ss_descs_count,
2437 vla_ptr(vlabuf, d, raw_descs) + fs_len + hs_len,
2438 d_raw_descs__sz - fs_len - hs_len,
2439 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002440 if (unlikely(ret < 0))
2441 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002442 }
2443
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002444 /*
2445 * Now handle interface numbers allocation and interface and
2446 * endpoint numbers rewriting. We can do that in one go
2447 * now.
2448 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002449 ret = ffs_do_descs(ffs->fs_descs_count +
Manu Gautam8d4e8972014-02-28 16:50:22 +05302450 (high ? ffs->hs_descs_count : 0) +
2451 (super ? ffs->ss_descs_count : 0),
Andrzej Pietrasiewicze6f38622013-12-03 15:15:30 +01002452 vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002453 __ffs_func_bind_do_nums, func);
2454 if (unlikely(ret < 0))
2455 goto error;
2456
2457 /* And we're done */
2458 ffs_event_add(ffs, FUNCTIONFS_BIND);
2459 return 0;
2460
2461error:
2462 /* XXX Do we need to release all claimed endpoints here? */
2463 return ret;
2464}
2465
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002466static int ffs_func_bind(struct usb_configuration *c,
2467 struct usb_function *f)
2468{
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002469 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2470
2471 if (IS_ERR(ffs_opts))
2472 return PTR_ERR(ffs_opts);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002473
2474 return _ffs_func_bind(c, f);
2475}
2476
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002477
2478/* Other USB function hooks *************************************************/
2479
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002480static int ffs_func_set_alt(struct usb_function *f,
2481 unsigned interface, unsigned alt)
2482{
2483 struct ffs_function *func = ffs_func_from_usb(f);
2484 struct ffs_data *ffs = func->ffs;
2485 int ret = 0, intf;
2486
2487 if (alt != (unsigned)-1) {
2488 intf = ffs_func_revmap_intf(func, interface);
2489 if (unlikely(intf < 0))
2490 return intf;
2491 }
2492
2493 if (ffs->func)
2494 ffs_func_eps_disable(ffs->func);
2495
2496 if (ffs->state != FFS_ACTIVE)
2497 return -ENODEV;
2498
2499 if (alt == (unsigned)-1) {
2500 ffs->func = NULL;
2501 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2502 return 0;
2503 }
2504
2505 ffs->func = func;
2506 ret = ffs_func_eps_enable(func);
2507 if (likely(ret >= 0))
2508 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2509 return ret;
2510}
2511
2512static void ffs_func_disable(struct usb_function *f)
2513{
2514 ffs_func_set_alt(f, 0, (unsigned)-1);
2515}
2516
2517static int ffs_func_setup(struct usb_function *f,
2518 const struct usb_ctrlrequest *creq)
2519{
2520 struct ffs_function *func = ffs_func_from_usb(f);
2521 struct ffs_data *ffs = func->ffs;
2522 unsigned long flags;
2523 int ret;
2524
2525 ENTER();
2526
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002527 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2528 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2529 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2530 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2531 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002532
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002533 /*
2534 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002535 * (notable exceptions are set/get interface) so we need to
2536 * handle them. All other either handled by composite or
2537 * passed to usb_configuration->setup() (if one is set). No
2538 * matter, we will handle requests directed to endpoint here
2539 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002540 * other request?
2541 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002542 if (ffs->state != FFS_ACTIVE)
2543 return -ENODEV;
2544
2545 switch (creq->bRequestType & USB_RECIP_MASK) {
2546 case USB_RECIP_INTERFACE:
2547 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2548 if (unlikely(ret < 0))
2549 return ret;
2550 break;
2551
2552 case USB_RECIP_ENDPOINT:
2553 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2554 if (unlikely(ret < 0))
2555 return ret;
2556 break;
2557
2558 default:
2559 return -EOPNOTSUPP;
2560 }
2561
2562 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2563 ffs->ev.setup = *creq;
2564 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2565 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2566 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2567
2568 return 0;
2569}
2570
2571static void ffs_func_suspend(struct usb_function *f)
2572{
2573 ENTER();
2574 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2575}
2576
2577static void ffs_func_resume(struct usb_function *f)
2578{
2579 ENTER();
2580 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2581}
2582
2583
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002584/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002585
2586static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2587{
2588 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2589 return num ? num : -EDOM;
2590}
2591
2592static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2593{
2594 short *nums = func->interfaces_nums;
2595 unsigned count = func->ffs->interfaces_count;
2596
2597 for (; count; --count, ++nums) {
2598 if (*nums >= 0 && *nums == intf)
2599 return nums - func->interfaces_nums;
2600 }
2601
2602 return -EDOM;
2603}
2604
2605
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002606/* Devices management *******************************************************/
2607
2608static LIST_HEAD(ffs_devices);
2609
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002610static struct ffs_dev *_ffs_do_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002611{
2612 struct ffs_dev *dev;
2613
2614 list_for_each_entry(dev, &ffs_devices, entry) {
2615 if (!dev->name || !name)
2616 continue;
2617 if (strcmp(dev->name, name) == 0)
2618 return dev;
2619 }
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002620
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002621 return NULL;
2622}
2623
2624/*
2625 * ffs_lock must be taken by the caller of this function
2626 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002627static struct ffs_dev *_ffs_get_single_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002628{
2629 struct ffs_dev *dev;
2630
2631 if (list_is_singular(&ffs_devices)) {
2632 dev = list_first_entry(&ffs_devices, struct ffs_dev, entry);
2633 if (dev->single)
2634 return dev;
2635 }
2636
2637 return NULL;
2638}
2639
2640/*
2641 * ffs_lock must be taken by the caller of this function
2642 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002643static struct ffs_dev *_ffs_find_dev(const char *name)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002644{
2645 struct ffs_dev *dev;
2646
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002647 dev = _ffs_get_single_dev();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002648 if (dev)
2649 return dev;
2650
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002651 return _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002652}
2653
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002654/* Configfs support *********************************************************/
2655
2656static inline struct f_fs_opts *to_ffs_opts(struct config_item *item)
2657{
2658 return container_of(to_config_group(item), struct f_fs_opts,
2659 func_inst.group);
2660}
2661
2662static void ffs_attr_release(struct config_item *item)
2663{
2664 struct f_fs_opts *opts = to_ffs_opts(item);
2665
2666 usb_put_function_instance(&opts->func_inst);
2667}
2668
2669static struct configfs_item_operations ffs_item_ops = {
2670 .release = ffs_attr_release,
2671};
2672
2673static struct config_item_type ffs_func_type = {
2674 .ct_item_ops = &ffs_item_ops,
2675 .ct_owner = THIS_MODULE,
2676};
2677
2678
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002679/* Function registration interface ******************************************/
2680
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002681static void ffs_free_inst(struct usb_function_instance *f)
2682{
2683 struct f_fs_opts *opts;
2684
2685 opts = to_f_fs_opts(f);
2686 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002687 _ffs_free_dev(opts->dev);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002688 ffs_dev_unlock();
2689 kfree(opts);
2690}
2691
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002692#define MAX_INST_NAME_LEN 40
2693
2694static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
2695{
2696 struct f_fs_opts *opts;
2697 char *ptr;
2698 const char *tmp;
2699 int name_len, ret;
2700
2701 name_len = strlen(name) + 1;
2702 if (name_len > MAX_INST_NAME_LEN)
2703 return -ENAMETOOLONG;
2704
2705 ptr = kstrndup(name, name_len, GFP_KERNEL);
2706 if (!ptr)
2707 return -ENOMEM;
2708
2709 opts = to_f_fs_opts(fi);
2710 tmp = NULL;
2711
2712 ffs_dev_lock();
2713
2714 tmp = opts->dev->name_allocated ? opts->dev->name : NULL;
2715 ret = _ffs_name_dev(opts->dev, ptr);
2716 if (ret) {
2717 kfree(ptr);
2718 ffs_dev_unlock();
2719 return ret;
2720 }
2721 opts->dev->name_allocated = true;
2722
2723 ffs_dev_unlock();
2724
2725 kfree(tmp);
2726
2727 return 0;
2728}
2729
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002730static struct usb_function_instance *ffs_alloc_inst(void)
2731{
2732 struct f_fs_opts *opts;
2733 struct ffs_dev *dev;
2734
2735 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2736 if (!opts)
2737 return ERR_PTR(-ENOMEM);
2738
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002739 opts->func_inst.set_inst_name = ffs_set_inst_name;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002740 opts->func_inst.free_func_inst = ffs_free_inst;
2741 ffs_dev_lock();
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002742 dev = _ffs_alloc_dev();
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002743 ffs_dev_unlock();
2744 if (IS_ERR(dev)) {
2745 kfree(opts);
2746 return ERR_CAST(dev);
2747 }
2748 opts->dev = dev;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002749 dev->opts = opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002750
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002751 config_group_init_type_name(&opts->func_inst.group, "",
2752 &ffs_func_type);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002753 return &opts->func_inst;
2754}
2755
2756static void ffs_free(struct usb_function *f)
2757{
2758 kfree(ffs_func_from_usb(f));
2759}
2760
2761static void ffs_func_unbind(struct usb_configuration *c,
2762 struct usb_function *f)
2763{
2764 struct ffs_function *func = ffs_func_from_usb(f);
2765 struct ffs_data *ffs = func->ffs;
2766 struct f_fs_opts *opts =
2767 container_of(f->fi, struct f_fs_opts, func_inst);
2768 struct ffs_ep *ep = func->eps;
2769 unsigned count = ffs->eps_count;
2770 unsigned long flags;
2771
2772 ENTER();
2773 if (ffs->func == func) {
2774 ffs_func_eps_disable(func);
2775 ffs->func = NULL;
2776 }
2777
2778 if (!--opts->refcnt)
2779 functionfs_unbind(ffs);
2780
2781 /* cleanup after autoconfig */
2782 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2783 do {
2784 if (ep->ep && ep->req)
2785 usb_ep_free_request(ep->ep, ep->req);
2786 ep->req = NULL;
2787 ++ep;
2788 } while (--count);
2789 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2790 kfree(func->eps);
2791 func->eps = NULL;
2792 /*
2793 * eps, descriptors and interfaces_nums are allocated in the
2794 * same chunk so only one free is required.
2795 */
2796 func->function.fs_descriptors = NULL;
2797 func->function.hs_descriptors = NULL;
Manu Gautam8d4e8972014-02-28 16:50:22 +05302798 func->function.ss_descriptors = NULL;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002799 func->interfaces_nums = NULL;
2800
2801 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2802}
2803
2804static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2805{
2806 struct ffs_function *func;
2807
2808 ENTER();
2809
2810 func = kzalloc(sizeof(*func), GFP_KERNEL);
2811 if (unlikely(!func))
2812 return ERR_PTR(-ENOMEM);
2813
2814 func->function.name = "Function FS Gadget";
2815
2816 func->function.bind = ffs_func_bind;
2817 func->function.unbind = ffs_func_unbind;
2818 func->function.set_alt = ffs_func_set_alt;
2819 func->function.disable = ffs_func_disable;
2820 func->function.setup = ffs_func_setup;
2821 func->function.suspend = ffs_func_suspend;
2822 func->function.resume = ffs_func_resume;
2823 func->function.free_func = ffs_free;
2824
2825 return &func->function;
2826}
2827
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002828/*
2829 * ffs_lock must be taken by the caller of this function
2830 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002831static struct ffs_dev *_ffs_alloc_dev(void)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002832{
2833 struct ffs_dev *dev;
2834 int ret;
2835
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002836 if (_ffs_get_single_dev())
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002837 return ERR_PTR(-EBUSY);
2838
2839 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2840 if (!dev)
2841 return ERR_PTR(-ENOMEM);
2842
2843 if (list_empty(&ffs_devices)) {
2844 ret = functionfs_init();
2845 if (ret) {
2846 kfree(dev);
2847 return ERR_PTR(ret);
2848 }
2849 }
2850
2851 list_add(&dev->entry, &ffs_devices);
2852
2853 return dev;
2854}
2855
2856/*
2857 * ffs_lock must be taken by the caller of this function
2858 * The caller is responsible for "name" being available whenever f_fs needs it
2859 */
2860static int _ffs_name_dev(struct ffs_dev *dev, const char *name)
2861{
2862 struct ffs_dev *existing;
2863
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002864 existing = _ffs_do_find_dev(name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002865 if (existing)
2866 return -EBUSY;
Andrzej Pietrasiewiczab13cb02014-01-13 16:49:36 +01002867
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002868 dev->name = name;
2869
2870 return 0;
2871}
2872
2873/*
2874 * The caller is responsible for "name" being available whenever f_fs needs it
2875 */
2876int ffs_name_dev(struct ffs_dev *dev, const char *name)
2877{
2878 int ret;
2879
2880 ffs_dev_lock();
2881 ret = _ffs_name_dev(dev, name);
2882 ffs_dev_unlock();
2883
2884 return ret;
2885}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002886EXPORT_SYMBOL(ffs_name_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002887
2888int ffs_single_dev(struct ffs_dev *dev)
2889{
2890 int ret;
2891
2892 ret = 0;
2893 ffs_dev_lock();
2894
2895 if (!list_is_singular(&ffs_devices))
2896 ret = -EBUSY;
2897 else
2898 dev->single = true;
2899
2900 ffs_dev_unlock();
2901 return ret;
2902}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002903EXPORT_SYMBOL(ffs_single_dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002904
2905/*
2906 * ffs_lock must be taken by the caller of this function
2907 */
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002908static void _ffs_free_dev(struct ffs_dev *dev)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002909{
2910 list_del(&dev->entry);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01002911 if (dev->name_allocated)
2912 kfree(dev->name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002913 kfree(dev);
2914 if (list_empty(&ffs_devices))
2915 functionfs_cleanup();
2916}
2917
2918static void *ffs_acquire_dev(const char *dev_name)
2919{
2920 struct ffs_dev *ffs_dev;
2921
2922 ENTER();
2923 ffs_dev_lock();
2924
Andrzej Pietrasiewiczda13a772014-01-13 16:49:38 +01002925 ffs_dev = _ffs_find_dev(dev_name);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002926 if (!ffs_dev)
2927 ffs_dev = ERR_PTR(-ENODEV);
2928 else if (ffs_dev->mounted)
2929 ffs_dev = ERR_PTR(-EBUSY);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01002930 else if (ffs_dev->ffs_acquire_dev_callback &&
2931 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2932 ffs_dev = ERR_PTR(-ENODEV);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002933 else
2934 ffs_dev->mounted = true;
2935
2936 ffs_dev_unlock();
2937 return ffs_dev;
2938}
2939
2940static void ffs_release_dev(struct ffs_data *ffs_data)
2941{
2942 struct ffs_dev *ffs_dev;
2943
2944 ENTER();
2945 ffs_dev_lock();
2946
2947 ffs_dev = ffs_data->private_data;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002948 if (ffs_dev) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002949 ffs_dev->mounted = false;
Andrzej Pietrasiewiczea365922014-01-13 16:49:35 +01002950
2951 if (ffs_dev->ffs_release_dev_callback)
2952 ffs_dev->ffs_release_dev_callback(ffs_dev);
2953 }
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01002954
2955 ffs_dev_unlock();
2956}
2957
2958static int ffs_ready(struct ffs_data *ffs)
2959{
2960 struct ffs_dev *ffs_obj;
2961 int ret = 0;
2962
2963 ENTER();
2964 ffs_dev_lock();
2965
2966 ffs_obj = ffs->private_data;
2967 if (!ffs_obj) {
2968 ret = -EINVAL;
2969 goto done;
2970 }
2971 if (WARN_ON(ffs_obj->desc_ready)) {
2972 ret = -EBUSY;
2973 goto done;
2974 }
2975
2976 ffs_obj->desc_ready = true;
2977 ffs_obj->ffs_data = ffs;
2978
2979 if (ffs_obj->ffs_ready_callback)
2980 ret = ffs_obj->ffs_ready_callback(ffs);
2981
2982done:
2983 ffs_dev_unlock();
2984 return ret;
2985}
2986
2987static void ffs_closed(struct ffs_data *ffs)
2988{
2989 struct ffs_dev *ffs_obj;
2990
2991 ENTER();
2992 ffs_dev_lock();
2993
2994 ffs_obj = ffs->private_data;
2995 if (!ffs_obj)
2996 goto done;
2997
2998 ffs_obj->desc_ready = false;
2999
3000 if (ffs_obj->ffs_closed_callback)
3001 ffs_obj->ffs_closed_callback(ffs);
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +01003002
3003 if (!ffs_obj->opts || ffs_obj->opts->no_configfs
3004 || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
3005 goto done;
3006
3007 unregister_gadget_item(ffs_obj->opts->
3008 func_inst.group.cg_item.ci_parent->ci_parent);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +01003009done:
3010 ffs_dev_unlock();
3011}
3012
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003013/* Misc helper functions ****************************************************/
3014
3015static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
3016{
3017 return nonblock
3018 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
3019 : mutex_lock_interruptible(mutex);
3020}
3021
Al Viro260ef312012-09-26 21:43:45 -04003022static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003023{
3024 char *data;
3025
3026 if (unlikely(!len))
3027 return NULL;
3028
3029 data = kmalloc(len, GFP_KERNEL);
3030 if (unlikely(!data))
3031 return ERR_PTR(-ENOMEM);
3032
3033 if (unlikely(__copy_from_user(data, buf, len))) {
3034 kfree(data);
3035 return ERR_PTR(-EFAULT);
3036 }
3037
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01003038 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003039 ffs_dump_mem("", data, len);
3040
3041 return data;
3042}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003043
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +01003044DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
3045MODULE_LICENSE("GPL");
3046MODULE_AUTHOR("Michal Nazarewicz");