blob: b825edcbf387785081da01f870780e8380984a31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c -- user mode filesystem api for usb gadget controllers
3 *
4 * Copyright (C) 2003-2004 David Brownell
5 * Copyright (C) 2003 Agilent Technologies
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13
David Brownella4e3ef52007-08-01 23:58:22 -070014/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/pagemap.h>
20#include <linux/uts.h>
21#include <linux/wait.h>
22#include <linux/compiler.h>
23#include <asm/uaccess.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040024#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
Milan Svobodae22fc272006-08-08 22:23:12 -070026#include <linux/poll.h>
Zach Browna80bf612013-05-07 16:18:23 -070027#include <linux/mmu_context.h>
Kent Overstreet4e179bc2013-05-07 16:18:33 -070028#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/device.h>
31#include <linux/moduleparam.h>
32
David Brownella5262dc2007-05-14 19:36:41 -070033#include <linux/usb/gadgetfs.h>
David Brownell9454a572007-10-04 18:05:17 -070034#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
37/*
38 * The gadgetfs API maps each endpoint to a file descriptor so that you
39 * can use standard synchronous read/write calls for I/O. There's some
40 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
41 * drivers show how this works in practice. You can also use AIO to
42 * eliminate I/O gaps between requests, to help when streaming data.
43 *
44 * Key parts that must be USB-specific are protocols defining how the
45 * read/write operations relate to the hardware state machines. There
46 * are two types of files. One type is for the device, implementing ep0.
47 * The other type is for each IN or OUT endpoint. In both cases, the
48 * user mode driver must configure the hardware before using it.
49 *
50 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
51 * (by writing configuration and device descriptors). Afterwards it
52 * may serve as a source of device events, used to handle all control
53 * requests other than basic enumeration.
54 *
Phil Endecott511779f2007-01-15 11:35:01 -080055 * - Then, after a SET_CONFIGURATION control request, ep_config() is
56 * called when each /dev/gadget/ep* file is configured (by writing
57 * endpoint descriptors). Afterwards these files are used to write()
58 * IN data or to read() OUT data. To halt the endpoint, a "wrong
59 * direction" request is issued (like reading an IN endpoint).
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
62 * not possible on all hardware. For example, precise fault handling with
63 * respect to data left in endpoint fifos after aborted operations; or
64 * selective clearing of endpoint halts, to implement SET_INTERFACE.
65 */
66
67#define DRIVER_DESC "USB Gadget filesystem"
68#define DRIVER_VERSION "24 Aug 2004"
69
70static const char driver_desc [] = DRIVER_DESC;
71static const char shortname [] = "gadgetfs";
72
73MODULE_DESCRIPTION (DRIVER_DESC);
74MODULE_AUTHOR ("David Brownell");
75MODULE_LICENSE ("GPL");
76
77
78/*----------------------------------------------------------------------*/
79
80#define GADGETFS_MAGIC 0xaee71ee7
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* /dev/gadget/$CHIP represents ep0 and the whole device */
83enum ep0_state {
84 /* DISBLED is the initial state.
85 */
86 STATE_DEV_DISABLED = 0,
87
88 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
89 * ep0/device i/o modes and binding to the controller. Driver
90 * must always write descriptors to initialize the device, then
91 * the device becomes UNCONNECTED until enumeration.
92 */
David Brownell7489d142007-01-16 22:51:04 -080093 STATE_DEV_OPENED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* From then on, ep0 fd is in either of two basic modes:
96 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
97 * - SETUP: read/write will transfer control data and succeed;
98 * or if "wrong direction", performs protocol stall
99 */
David Brownell7489d142007-01-16 22:51:04 -0800100 STATE_DEV_UNCONNECTED,
101 STATE_DEV_CONNECTED,
102 STATE_DEV_SETUP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* UNBOUND means the driver closed ep0, so the device won't be
105 * accessible again (DEV_DISABLED) until all fds are closed.
106 */
107 STATE_DEV_UNBOUND,
108};
109
110/* enough for the whole queue: most events invalidate others */
111#define N_EVENT 5
112
113struct dev_data {
114 spinlock_t lock;
115 atomic_t count;
David Brownell7489d142007-01-16 22:51:04 -0800116 enum ep0_state state; /* P: lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct usb_gadgetfs_event event [N_EVENT];
118 unsigned ev_next;
119 struct fasync_struct *fasync;
120 u8 current_config;
121
122 /* drivers reading ep0 MUST handle control requests (SETUP)
123 * reported that way; else the host will time out.
124 */
125 unsigned usermode_setup : 1,
126 setup_in : 1,
127 setup_can_stall : 1,
128 setup_out_ready : 1,
129 setup_out_error : 1,
130 setup_abort : 1;
Alan Stern97906362006-01-03 10:30:31 -0500131 unsigned setup_wLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* the rest is basically write-once */
134 struct usb_config_descriptor *config, *hs_config;
135 struct usb_device_descriptor *dev;
136 struct usb_request *req;
137 struct usb_gadget *gadget;
138 struct list_head epfiles;
139 void *buf;
140 wait_queue_head_t wait;
141 struct super_block *sb;
142 struct dentry *dentry;
143
144 /* except this scratch i/o buffer for ep0 */
145 u8 rbuf [256];
146};
147
148static inline void get_dev (struct dev_data *data)
149{
150 atomic_inc (&data->count);
151}
152
153static void put_dev (struct dev_data *data)
154{
155 if (likely (!atomic_dec_and_test (&data->count)))
156 return;
157 /* needs no more cleanup */
158 BUG_ON (waitqueue_active (&data->wait));
159 kfree (data);
160}
161
162static struct dev_data *dev_new (void)
163{
164 struct dev_data *dev;
165
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800166 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!dev)
168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dev->state = STATE_DEV_DISABLED;
170 atomic_set (&dev->count, 1);
171 spin_lock_init (&dev->lock);
172 INIT_LIST_HEAD (&dev->epfiles);
173 init_waitqueue_head (&dev->wait);
174 return dev;
175}
176
177/*----------------------------------------------------------------------*/
178
179/* other /dev/gadget/$ENDPOINT files represent endpoints */
180enum ep_state {
181 STATE_EP_DISABLED = 0,
182 STATE_EP_READY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 STATE_EP_ENABLED,
184 STATE_EP_UNBOUND,
185};
186
187struct ep_data {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000188 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 enum ep_state state;
190 atomic_t count;
191 struct dev_data *dev;
192 /* must hold dev->lock before accessing ep or req */
193 struct usb_ep *ep;
194 struct usb_request *req;
195 ssize_t status;
196 char name [16];
197 struct usb_endpoint_descriptor desc, hs_desc;
198 struct list_head epfiles;
199 wait_queue_head_t wait;
200 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203static inline void get_ep (struct ep_data *data)
204{
205 atomic_inc (&data->count);
206}
207
208static void put_ep (struct ep_data *data)
209{
210 if (likely (!atomic_dec_and_test (&data->count)))
211 return;
212 put_dev (data->dev);
213 /* needs no more cleanup */
214 BUG_ON (!list_empty (&data->epfiles));
215 BUG_ON (waitqueue_active (&data->wait));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 kfree (data);
217}
218
219/*----------------------------------------------------------------------*/
220
221/* most "how to use the hardware" policy choices are in userspace:
222 * mapping endpoint roles (which the driver needs) to the capabilities
223 * which the usb controller has. most of those capabilities are exposed
224 * implicitly, starting with the driver name and then endpoint names.
225 */
226
227static const char *CHIP;
228
229/*----------------------------------------------------------------------*/
230
231/* NOTE: don't use dev_printk calls before binding to the gadget
232 * at the end of ep0 configuration, or after unbind.
233 */
234
235/* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
236#define xprintk(d,level,fmt,args...) \
237 printk(level "%s: " fmt , shortname , ## args)
238
239#ifdef DEBUG
240#define DBG(dev,fmt,args...) \
241 xprintk(dev , KERN_DEBUG , fmt , ## args)
242#else
243#define DBG(dev,fmt,args...) \
244 do { } while (0)
245#endif /* DEBUG */
246
David Brownella4e3ef52007-08-01 23:58:22 -0700247#ifdef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#define VDEBUG DBG
249#else
250#define VDEBUG(dev,fmt,args...) \
251 do { } while (0)
252#endif /* DEBUG */
253
254#define ERROR(dev,fmt,args...) \
255 xprintk(dev , KERN_ERR , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#define INFO(dev,fmt,args...) \
257 xprintk(dev , KERN_INFO , fmt , ## args)
258
259
260/*----------------------------------------------------------------------*/
261
262/* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
263 *
264 * After opening, configure non-control endpoints. Then use normal
265 * stream read() and write() requests; and maybe ioctl() to get more
Steven Cole093cf722005-05-03 19:07:24 -0600266 * precise FIFO status when recovering from cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268
269static void epio_complete (struct usb_ep *ep, struct usb_request *req)
270{
271 struct ep_data *epdata = ep->driver_data;
272
273 if (!req->context)
274 return;
275 if (req->status)
276 epdata->status = req->status;
277 else
278 epdata->status = req->actual;
279 complete ((struct completion *)req->context);
280}
281
282/* tasklock endpoint, returning when it's connected.
283 * still need dev->lock to use epdata->ep.
284 */
285static int
286get_ready_ep (unsigned f_flags, struct ep_data *epdata)
287{
288 int val;
289
290 if (f_flags & O_NONBLOCK) {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000291 if (!mutex_trylock(&epdata->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto nonblock;
293 if (epdata->state != STATE_EP_ENABLED) {
Thomas Gleixnera79df502010-01-29 20:38:59 +0000294 mutex_unlock(&epdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295nonblock:
296 val = -EAGAIN;
297 } else
298 val = 0;
299 return val;
300 }
301
Thomas Gleixnera79df502010-01-29 20:38:59 +0000302 val = mutex_lock_interruptible(&epdata->lock);
303 if (val < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return val;
Phil Endecott511779f2007-01-15 11:35:01 -0800305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 switch (epdata->state) {
307 case STATE_EP_ENABLED:
308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 // case STATE_EP_DISABLED: /* "can't happen" */
310 // case STATE_EP_READY: /* "can't happen" */
311 default: /* error! */
312 pr_debug ("%s: ep %p not available, state %d\n",
313 shortname, epdata, epdata->state);
314 // FALLTHROUGH
315 case STATE_EP_UNBOUND: /* clean disconnect */
316 val = -ENODEV;
Thomas Gleixnera79df502010-01-29 20:38:59 +0000317 mutex_unlock(&epdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 return val;
320}
321
322static ssize_t
323ep_io (struct ep_data *epdata, void *buf, unsigned len)
324{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700325 DECLARE_COMPLETION_ONSTACK (done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int value;
327
328 spin_lock_irq (&epdata->dev->lock);
329 if (likely (epdata->ep != NULL)) {
330 struct usb_request *req = epdata->req;
331
332 req->context = &done;
333 req->complete = epio_complete;
334 req->buf = buf;
335 req->length = len;
336 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
337 } else
338 value = -ENODEV;
339 spin_unlock_irq (&epdata->dev->lock);
340
341 if (likely (value == 0)) {
342 value = wait_event_interruptible (done.wait, done.done);
343 if (value != 0) {
344 spin_lock_irq (&epdata->dev->lock);
345 if (likely (epdata->ep != NULL)) {
346 DBG (epdata->dev, "%s i/o interrupted\n",
347 epdata->name);
348 usb_ep_dequeue (epdata->ep, epdata->req);
349 spin_unlock_irq (&epdata->dev->lock);
350
351 wait_event (done.wait, done.done);
352 if (epdata->status == -ECONNRESET)
353 epdata->status = -EINTR;
354 } else {
355 spin_unlock_irq (&epdata->dev->lock);
356
357 DBG (epdata->dev, "endpoint gone\n");
358 epdata->status = -ENODEV;
359 }
360 }
361 return epdata->status;
362 }
363 return value;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static int
367ep_release (struct inode *inode, struct file *fd)
368{
369 struct ep_data *data = fd->private_data;
Milan Svobodaba307f52006-06-26 07:48:00 -0700370 int value;
371
Thomas Gleixnera79df502010-01-29 20:38:59 +0000372 value = mutex_lock_interruptible(&data->lock);
373 if (value < 0)
Milan Svobodaba307f52006-06-26 07:48:00 -0700374 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* clean up if this can be reopened */
377 if (data->state != STATE_EP_UNBOUND) {
378 data->state = STATE_EP_DISABLED;
379 data->desc.bDescriptorType = 0;
380 data->hs_desc.bDescriptorType = 0;
Pavol Kurina4809ecc2005-09-07 09:49:34 -0700381 usb_ep_disable(data->ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Thomas Gleixnera79df502010-01-29 20:38:59 +0000383 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 put_ep (data);
385 return 0;
386}
387
Alan Cox44c389a2008-05-22 22:03:27 +0100388static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct ep_data *data = fd->private_data;
391 int status;
392
393 if ((status = get_ready_ep (fd->f_flags, data)) < 0)
394 return status;
395
396 spin_lock_irq (&data->dev->lock);
397 if (likely (data->ep != NULL)) {
398 switch (code) {
399 case GADGETFS_FIFO_STATUS:
400 status = usb_ep_fifo_status (data->ep);
401 break;
402 case GADGETFS_FIFO_FLUSH:
403 usb_ep_fifo_flush (data->ep);
404 break;
405 case GADGETFS_CLEAR_HALT:
406 status = usb_ep_clear_halt (data->ep);
407 break;
408 default:
409 status = -ENOTTY;
410 }
411 } else
412 status = -ENODEV;
413 spin_unlock_irq (&data->dev->lock);
Thomas Gleixnera79df502010-01-29 20:38:59 +0000414 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return status;
416}
417
418/*----------------------------------------------------------------------*/
419
420/* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
421
422struct kiocb_priv {
423 struct usb_request *req;
424 struct ep_data *epdata;
Zach Browna80bf612013-05-07 16:18:23 -0700425 struct kiocb *iocb;
426 struct mm_struct *mm;
427 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 void *buf;
Al Viro7fe39762015-02-07 00:30:23 -0500429 struct iov_iter to;
430 const void *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned actual;
432};
433
Kent Overstreetbec68faa2013-05-13 14:45:08 -0700434static int ep_aio_cancel(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct kiocb_priv *priv = iocb->private;
437 struct ep_data *epdata;
438 int value;
439
440 local_irq_disable();
441 epdata = priv->epdata;
442 // spin_lock(&epdata->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (likely(epdata && epdata->ep && priv->req))
444 value = usb_ep_dequeue (epdata->ep, priv->req);
445 else
446 value = -EINVAL;
447 // spin_unlock(&epdata->dev->lock);
448 local_irq_enable();
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return value;
451}
452
Zach Browna80bf612013-05-07 16:18:23 -0700453static void ep_user_copy_worker(struct work_struct *work)
454{
455 struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
456 struct mm_struct *mm = priv->mm;
457 struct kiocb *iocb = priv->iocb;
458 size_t ret;
459
460 use_mm(mm);
Al Viro7fe39762015-02-07 00:30:23 -0500461 ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
Zach Browna80bf612013-05-07 16:18:23 -0700462 unuse_mm(mm);
Al Viro7fe39762015-02-07 00:30:23 -0500463 if (!ret)
464 ret = -EFAULT;
Zach Browna80bf612013-05-07 16:18:23 -0700465
466 /* completing the iocb can drop the ctx and mm, don't touch mm after */
467 aio_complete(iocb, ret, ret);
468
David Brownell25051072007-01-15 11:30:28 -0800469 kfree(priv->buf);
Al Viro7fe39762015-02-07 00:30:23 -0500470 kfree(priv->to_free);
David Brownell25051072007-01-15 11:30:28 -0800471 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
475{
476 struct kiocb *iocb = req->context;
477 struct kiocb_priv *priv = iocb->private;
478 struct ep_data *epdata = priv->epdata;
479
480 /* lock against disconnect (and ideally, cancel) */
481 spin_lock(&epdata->dev->lock);
482 priv->req = NULL;
483 priv->epdata = NULL;
Alan Stern49631ca2007-01-16 23:28:48 -0800484
485 /* if this was a write or a read returning no data then we
486 * don't need to copy anything to userspace, so we can
487 * complete the aio request immediately.
488 */
Al Viro7fe39762015-02-07 00:30:23 -0500489 if (priv->to_free == NULL || unlikely(req->actual == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kfree(req->buf);
Al Viro7fe39762015-02-07 00:30:23 -0500491 kfree(priv->to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 kfree(priv);
493 iocb->private = NULL;
494 /* aio_complete() reports bytes-transferred _and_ faults */
Alan Stern49631ca2007-01-16 23:28:48 -0800495 aio_complete(iocb, req->actual ? req->actual : req->status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 req->status);
497 } else {
Zach Browna80bf612013-05-07 16:18:23 -0700498 /* ep_copy_to_user() won't report both; we hide some faults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (unlikely(0 != req->status))
500 DBG(epdata->dev, "%s fault %d len %d\n",
501 ep->name, req->status, req->actual);
502
503 priv->buf = req->buf;
504 priv->actual = req->actual;
Al Viro7fe39762015-02-07 00:30:23 -0500505 INIT_WORK(&priv->work, ep_user_copy_worker);
Zach Browna80bf612013-05-07 16:18:23 -0700506 schedule_work(&priv->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 spin_unlock(&epdata->dev->lock);
509
510 usb_ep_free_request(ep, req);
511 put_ep(epdata);
512}
513
Al Viro7fe39762015-02-07 00:30:23 -0500514static ssize_t ep_aio(struct kiocb *iocb,
515 struct kiocb_priv *priv,
516 struct ep_data *epdata,
517 char *buf,
518 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Al Viro7fe39762015-02-07 00:30:23 -0500520 struct usb_request *req;
521 ssize_t value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 iocb->private = priv;
Zach Browna80bf612013-05-07 16:18:23 -0700524 priv->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Kent Overstreet0460fef2013-05-07 16:18:49 -0700526 kiocb_set_cancel_fn(iocb, ep_aio_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 get_ep(epdata);
528 priv->epdata = epdata;
529 priv->actual = 0;
Zach Browna80bf612013-05-07 16:18:23 -0700530 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* each kiocb is coupled to one usb_request, but we can't
533 * allocate or submit those if the host disconnected.
534 */
535 spin_lock_irq(&epdata->dev->lock);
Al Viro7fe39762015-02-07 00:30:23 -0500536 value = -ENODEV;
537 if (unlikely(epdata->ep))
538 goto fail;
539
540 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
541 value = -ENOMEM;
542 if (unlikely(!req))
543 goto fail;
544
545 priv->req = req;
546 req->buf = buf;
547 req->length = len;
548 req->complete = ep_aio_complete;
549 req->context = iocb;
550 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
551 if (unlikely(0 != value)) {
552 usb_ep_free_request(epdata->ep, req);
553 goto fail;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_unlock_irq(&epdata->dev->lock);
Al Viro7fe39762015-02-07 00:30:23 -0500556 return -EIOCBQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Al Viro7fe39762015-02-07 00:30:23 -0500558fail:
559 spin_unlock_irq(&epdata->dev->lock);
560 kfree(priv->to_free);
561 kfree(priv);
562 put_ep(epdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return value;
564}
565
566static ssize_t
Al Viro7fe39762015-02-07 00:30:23 -0500567ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Al Viro7fe39762015-02-07 00:30:23 -0500569 struct file *file = iocb->ki_filp;
570 struct ep_data *epdata = file->private_data;
571 size_t len = iov_iter_count(to);
572 ssize_t value;
573 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Al Viro7fe39762015-02-07 00:30:23 -0500575 if ((value = get_ready_ep(file->f_flags, epdata)) < 0)
576 return value;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700577
Al Viro7fe39762015-02-07 00:30:23 -0500578 /* halt any endpoint by doing a "wrong direction" i/o call */
579 if (usb_endpoint_dir_in(&epdata->desc)) {
580 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
581 !is_sync_kiocb(iocb)) {
582 mutex_unlock(&epdata->lock);
583 return -EINVAL;
584 }
585 DBG (epdata->dev, "%s halt\n", epdata->name);
586 spin_lock_irq(&epdata->dev->lock);
587 if (likely(epdata->ep != NULL))
588 usb_ep_set_halt(epdata->ep);
589 spin_unlock_irq(&epdata->dev->lock);
590 mutex_unlock(&epdata->lock);
591 return -EBADMSG;
592 }
593
594 buf = kmalloc(len, GFP_KERNEL);
595 if (unlikely(!buf)) {
596 mutex_unlock(&epdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -ENOMEM;
Al Viro7fe39762015-02-07 00:30:23 -0500598 }
599 if (is_sync_kiocb(iocb)) {
600 value = ep_io(epdata, buf, len);
601 if (value >= 0 && copy_to_iter(buf, value, to))
602 value = -EFAULT;
603 } else {
604 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
605 value = -ENOMEM;
606 if (!priv)
607 goto fail;
608 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
609 if (!priv->to_free) {
610 kfree(priv);
611 goto fail;
612 }
613 value = ep_aio(iocb, priv, epdata, buf, len);
614 if (value == -EIOCBQUEUED)
615 buf = NULL;
616 }
617fail:
618 kfree(buf);
619 mutex_unlock(&epdata->lock);
620 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static ssize_t
Al Viro7fe39762015-02-07 00:30:23 -0500624ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Al Viro7fe39762015-02-07 00:30:23 -0500626 struct file *file = iocb->ki_filp;
627 struct ep_data *epdata = file->private_data;
628 size_t len = iov_iter_count(from);
629 ssize_t value;
630 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Al Viro7fe39762015-02-07 00:30:23 -0500632 if ((value = get_ready_ep(file->f_flags, epdata)) < 0)
633 return value;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700634
Al Viro7fe39762015-02-07 00:30:23 -0500635 /* halt any endpoint by doing a "wrong direction" i/o call */
636 if (!usb_endpoint_dir_in(&epdata->desc)) {
637 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
638 !is_sync_kiocb(iocb)) {
639 mutex_unlock(&epdata->lock);
640 return -EINVAL;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700641 }
Al Viro7fe39762015-02-07 00:30:23 -0500642 DBG (epdata->dev, "%s halt\n", epdata->name);
643 spin_lock_irq(&epdata->dev->lock);
644 if (likely(epdata->ep != NULL))
645 usb_ep_set_halt(epdata->ep);
646 spin_unlock_irq(&epdata->dev->lock);
647 mutex_unlock(&epdata->lock);
648 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Al Viro7fe39762015-02-07 00:30:23 -0500650
651 buf = kmalloc(len, GFP_KERNEL);
652 if (unlikely(!buf)) {
653 mutex_unlock(&epdata->lock);
654 return -ENOMEM;
655 }
656
657 if (unlikely(copy_from_iter(buf, len, from) != len)) {
658 value = -EFAULT;
659 goto out;
660 }
661
662 if (is_sync_kiocb(iocb)) {
663 value = ep_io(epdata, buf, len);
664 } else {
665 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
666 value = -ENOMEM;
667 if (priv) {
668 value = ep_aio(iocb, priv, epdata, buf, len);
669 if (value == -EIOCBQUEUED)
670 buf = NULL;
671 }
672 }
673out:
674 kfree(buf);
675 mutex_unlock(&epdata->lock);
676 return value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/*----------------------------------------------------------------------*/
680
681/* used after endpoint configuration */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300682static const struct file_operations ep_io_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .owner = THIS_MODULE,
684 .llseek = no_llseek,
685
Al Viro7fe39762015-02-07 00:30:23 -0500686 .read = new_sync_read,
687 .write = new_sync_write,
Alan Cox44c389a2008-05-22 22:03:27 +0100688 .unlocked_ioctl = ep_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 .release = ep_release,
690
Al Viro7fe39762015-02-07 00:30:23 -0500691 .read_iter = ep_read_iter,
692 .write_iter = ep_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
694
695/* ENDPOINT INITIALIZATION
696 *
697 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
698 * status = write (fd, descriptors, sizeof descriptors)
699 *
700 * That write establishes the endpoint configuration, configuring
701 * the controller to process bulk, interrupt, or isochronous transfers
702 * at the right maxpacket size, and so on.
703 *
704 * The descriptors are message type 1, identified by a host order u32
705 * at the beginning of what's written. Descriptor order is: full/low
706 * speed descriptor, then optional high speed descriptor.
707 */
708static ssize_t
709ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
710{
711 struct ep_data *data = fd->private_data;
712 struct usb_ep *ep;
713 u32 tag;
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700714 int value, length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Thomas Gleixnera79df502010-01-29 20:38:59 +0000716 value = mutex_lock_interruptible(&data->lock);
717 if (value < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return value;
719
720 if (data->state != STATE_EP_READY) {
721 value = -EL2HLT;
722 goto fail;
723 }
724
725 value = len;
726 if (len < USB_DT_ENDPOINT_SIZE + 4)
727 goto fail0;
728
729 /* we might need to change message format someday */
730 if (copy_from_user (&tag, buf, 4)) {
731 goto fail1;
732 }
733 if (tag != 1) {
734 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
735 goto fail0;
736 }
737 buf += 4;
738 len -= 4;
739
740 /* NOTE: audio endpoint extensions not accepted here;
741 * just don't include the extra bytes.
742 */
743
744 /* full/low speed descriptor, then high speed */
745 if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
746 goto fail1;
747 }
748 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
749 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
750 goto fail0;
751 if (len != USB_DT_ENDPOINT_SIZE) {
752 if (len != 2 * USB_DT_ENDPOINT_SIZE)
753 goto fail0;
754 if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
755 USB_DT_ENDPOINT_SIZE)) {
756 goto fail1;
757 }
758 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
759 || data->hs_desc.bDescriptorType
760 != USB_DT_ENDPOINT) {
761 DBG(data->dev, "config %s, bad hs length or type\n",
762 data->name);
763 goto fail0;
764 }
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 spin_lock_irq (&data->dev->lock);
768 if (data->dev->state == STATE_DEV_UNBOUND) {
769 value = -ENOENT;
770 goto gone;
771 } else if ((ep = data->ep) == NULL) {
772 value = -ENODEV;
773 goto gone;
774 }
775 switch (data->dev->gadget->speed) {
776 case USB_SPEED_LOW:
777 case USB_SPEED_FULL:
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300778 ep->desc = &data->desc;
779 value = usb_ep_enable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (value == 0)
781 data->state = STATE_EP_ENABLED;
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case USB_SPEED_HIGH:
784 /* fails if caller didn't provide that descriptor... */
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300785 ep->desc = &data->hs_desc;
786 value = usb_ep_enable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (value == 0)
788 data->state = STATE_EP_ENABLED;
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 default:
Phil Endecott511779f2007-01-15 11:35:01 -0800791 DBG(data->dev, "unconnected, %s init abandoned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 data->name);
Phil Endecott511779f2007-01-15 11:35:01 -0800793 value = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700795 if (value == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 fd->f_op = &ep_io_operations;
Milan Svoboda8a7471a2006-06-26 07:19:00 -0700797 value = length;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799gone:
800 spin_unlock_irq (&data->dev->lock);
801 if (value < 0) {
802fail:
803 data->desc.bDescriptorType = 0;
804 data->hs_desc.bDescriptorType = 0;
805 }
Thomas Gleixnera79df502010-01-29 20:38:59 +0000806 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return value;
808fail0:
809 value = -EINVAL;
810 goto fail;
811fail1:
812 value = -EFAULT;
813 goto fail;
814}
815
816static int
817ep_open (struct inode *inode, struct file *fd)
818{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700819 struct ep_data *data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int value = -EBUSY;
821
Thomas Gleixnera79df502010-01-29 20:38:59 +0000822 if (mutex_lock_interruptible(&data->lock) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EINTR;
824 spin_lock_irq (&data->dev->lock);
825 if (data->dev->state == STATE_DEV_UNBOUND)
826 value = -ENOENT;
827 else if (data->state == STATE_EP_DISABLED) {
828 value = 0;
829 data->state = STATE_EP_READY;
830 get_ep (data);
831 fd->private_data = data;
832 VDEBUG (data->dev, "%s ready\n", data->name);
833 } else
834 DBG (data->dev, "%s state %d\n",
835 data->name, data->state);
836 spin_unlock_irq (&data->dev->lock);
Thomas Gleixnera79df502010-01-29 20:38:59 +0000837 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return value;
839}
840
841/* used before endpoint configuration */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300842static const struct file_operations ep_config_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .llseek = no_llseek,
844
845 .open = ep_open,
846 .write = ep_config,
847 .release = ep_release,
848};
849
850/*----------------------------------------------------------------------*/
851
852/* EP0 IMPLEMENTATION can be partly in userspace.
853 *
854 * Drivers that use this facility receive various events, including
855 * control requests the kernel doesn't handle. Drivers that don't
856 * use this facility may be too simple-minded for real applications.
857 */
858
859static inline void ep0_readable (struct dev_data *dev)
860{
861 wake_up (&dev->wait);
862 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
863}
864
865static void clean_req (struct usb_ep *ep, struct usb_request *req)
866{
867 struct dev_data *dev = ep->driver_data;
868
869 if (req->buf != dev->rbuf) {
David Brownell9d8bab52007-07-01 11:04:54 -0700870 kfree(req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 req->buf = dev->rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 req->complete = epio_complete;
874 dev->setup_out_ready = 0;
875}
876
877static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
878{
879 struct dev_data *dev = ep->driver_data;
David Brownell5b89db022007-01-16 22:56:26 -0800880 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int free = 1;
882
883 /* for control OUT, data must still get to userspace */
David Brownell5b89db022007-01-16 22:56:26 -0800884 spin_lock_irqsave(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!dev->setup_in) {
886 dev->setup_out_error = (req->status != 0);
887 if (!dev->setup_out_error)
888 free = 0;
889 dev->setup_out_ready = 1;
890 ep0_readable (dev);
David Brownell7489d142007-01-16 22:51:04 -0800891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* clean up as appropriate */
894 if (free && req->buf != &dev->rbuf)
895 clean_req (ep, req);
896 req->complete = epio_complete;
David Brownell5b89db022007-01-16 22:56:26 -0800897 spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
901{
902 struct dev_data *dev = ep->driver_data;
903
904 if (dev->setup_out_ready) {
905 DBG (dev, "ep0 request busy!\n");
906 return -EBUSY;
907 }
908 if (len > sizeof (dev->rbuf))
David Brownell9d8bab52007-07-01 11:04:54 -0700909 req->buf = kmalloc(len, GFP_ATOMIC);
David Brownella9475222007-07-30 12:31:07 -0700910 if (req->buf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 req->buf = dev->rbuf;
912 return -ENOMEM;
913 }
914 req->complete = ep0_complete;
915 req->length = len;
Alan Stern97906362006-01-03 10:30:31 -0500916 req->zero = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918}
919
920static ssize_t
921ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
922{
923 struct dev_data *dev = fd->private_data;
924 ssize_t retval;
925 enum ep0_state state;
926
927 spin_lock_irq (&dev->lock);
928
929 /* report fd mode change before acting on it */
930 if (dev->setup_abort) {
931 dev->setup_abort = 0;
932 retval = -EIDRM;
933 goto done;
934 }
935
936 /* control DATA stage */
David Brownell7489d142007-01-16 22:51:04 -0800937 if ((state = dev->state) == STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (dev->setup_in) { /* stall IN */
940 VDEBUG(dev, "ep0in stall\n");
941 (void) usb_ep_set_halt (dev->gadget->ep0);
942 retval = -EL2HLT;
David Brownell7489d142007-01-16 22:51:04 -0800943 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
946 struct usb_ep *ep = dev->gadget->ep0;
947 struct usb_request *req = dev->req;
948
949 if ((retval = setup_req (ep, req, 0)) == 0)
950 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
David Brownell7489d142007-01-16 22:51:04 -0800951 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* assume that was SET_CONFIGURATION */
954 if (dev->current_config) {
955 unsigned power;
David Brownella4e3ef52007-08-01 23:58:22 -0700956
957 if (gadget_is_dualspeed(dev->gadget)
958 && (dev->gadget->speed
959 == USB_SPEED_HIGH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 power = dev->hs_config->bMaxPower;
961 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 power = dev->config->bMaxPower;
963 usb_gadget_vbus_draw(dev->gadget, 2 * power);
964 }
965
966 } else { /* collect OUT data */
967 if ((fd->f_flags & O_NONBLOCK) != 0
968 && !dev->setup_out_ready) {
969 retval = -EAGAIN;
970 goto done;
971 }
972 spin_unlock_irq (&dev->lock);
973 retval = wait_event_interruptible (dev->wait,
974 dev->setup_out_ready != 0);
975
976 /* FIXME state could change from under us */
977 spin_lock_irq (&dev->lock);
978 if (retval)
979 goto done;
David Brownell5b89db022007-01-16 22:56:26 -0800980
981 if (dev->state != STATE_DEV_SETUP) {
982 retval = -ECANCELED;
983 goto done;
984 }
985 dev->state = STATE_DEV_CONNECTED;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (dev->setup_out_error)
988 retval = -EIO;
989 else {
990 len = min (len, (size_t)dev->req->actual);
991// FIXME don't call this with the spinlock held ...
Skip Hansen997694d2006-09-01 15:26:27 -0700992 if (copy_to_user (buf, dev->req->buf, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 retval = -EFAULT;
Thomas Faber85b4b3c2012-03-02 09:41:50 +0100994 else
995 retval = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 clean_req (dev->gadget->ep0, dev->req);
997 /* NOTE userspace can't yet choose to stall */
998 }
999 }
1000 goto done;
1001 }
1002
1003 /* else normal: return event data */
1004 if (len < sizeof dev->event [0]) {
1005 retval = -EINVAL;
1006 goto done;
1007 }
1008 len -= len % sizeof (struct usb_gadgetfs_event);
1009 dev->usermode_setup = 1;
1010
1011scan:
1012 /* return queued events right away */
1013 if (dev->ev_next != 0) {
1014 unsigned i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 n = len / sizeof (struct usb_gadgetfs_event);
David Brownell0864c7a2007-01-16 22:53:58 -08001017 if (dev->ev_next < n)
1018 n = dev->ev_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David Brownell0864c7a2007-01-16 22:53:58 -08001020 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 for (i = 0; i < n; i++) {
1022 if (dev->event [i].type == GADGETFS_SETUP) {
David Brownell0864c7a2007-01-16 22:53:58 -08001023 dev->state = STATE_DEV_SETUP;
1024 n = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026 }
1027 }
1028 spin_unlock_irq (&dev->lock);
David Brownell0864c7a2007-01-16 22:53:58 -08001029 len = n * sizeof (struct usb_gadgetfs_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (copy_to_user (buf, &dev->event, len))
1031 retval = -EFAULT;
1032 else
1033 retval = len;
1034 if (len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* NOTE this doesn't guard against broken drivers;
1036 * concurrent ep0 readers may lose events.
1037 */
1038 spin_lock_irq (&dev->lock);
David Brownell0864c7a2007-01-16 22:53:58 -08001039 if (dev->ev_next > n) {
1040 memmove(&dev->event[0], &dev->event[n],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 sizeof (struct usb_gadgetfs_event)
David Brownell0864c7a2007-01-16 22:53:58 -08001042 * (dev->ev_next - n));
1043 }
1044 dev->ev_next -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_unlock_irq (&dev->lock);
1046 }
1047 return retval;
1048 }
1049 if (fd->f_flags & O_NONBLOCK) {
1050 retval = -EAGAIN;
1051 goto done;
1052 }
1053
1054 switch (state) {
1055 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001056 DBG (dev, "fail %s, state %d\n", __func__, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 retval = -ESRCH;
1058 break;
David Brownell7489d142007-01-16 22:51:04 -08001059 case STATE_DEV_UNCONNECTED:
1060 case STATE_DEV_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_unlock_irq (&dev->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001062 DBG (dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* wait for events */
1065 retval = wait_event_interruptible (dev->wait,
1066 dev->ev_next != 0);
1067 if (retval < 0)
1068 return retval;
1069 spin_lock_irq (&dev->lock);
1070 goto scan;
1071 }
1072
1073done:
1074 spin_unlock_irq (&dev->lock);
1075 return retval;
1076}
1077
1078static struct usb_gadgetfs_event *
1079next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1080{
1081 struct usb_gadgetfs_event *event;
1082 unsigned i;
1083
1084 switch (type) {
1085 /* these events purge the queue */
1086 case GADGETFS_DISCONNECT:
David Brownell7489d142007-01-16 22:51:04 -08001087 if (dev->state == STATE_DEV_SETUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dev->setup_abort = 1;
1089 // FALL THROUGH
1090 case GADGETFS_CONNECT:
1091 dev->ev_next = 0;
1092 break;
1093 case GADGETFS_SETUP: /* previous request timed out */
1094 case GADGETFS_SUSPEND: /* same effect */
1095 /* these events can't be repeated */
1096 for (i = 0; i != dev->ev_next; i++) {
1097 if (dev->event [i].type != type)
1098 continue;
David Brownell0864c7a2007-01-16 22:53:58 -08001099 DBG(dev, "discard old event[%d] %d\n", i, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dev->ev_next--;
1101 if (i == dev->ev_next)
1102 break;
1103 /* indices start at zero, for simplicity */
1104 memmove (&dev->event [i], &dev->event [i + 1],
1105 sizeof (struct usb_gadgetfs_event)
1106 * (dev->ev_next - i));
1107 }
1108 break;
1109 default:
1110 BUG ();
1111 }
David Brownell0864c7a2007-01-16 22:53:58 -08001112 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 event = &dev->event [dev->ev_next++];
1114 BUG_ON (dev->ev_next > N_EVENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 memset (event, 0, sizeof *event);
1116 event->type = type;
1117 return event;
1118}
1119
1120static ssize_t
1121ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1122{
1123 struct dev_data *dev = fd->private_data;
1124 ssize_t retval = -ESRCH;
1125
1126 spin_lock_irq (&dev->lock);
1127
1128 /* report fd mode change before acting on it */
1129 if (dev->setup_abort) {
1130 dev->setup_abort = 0;
1131 retval = -EIDRM;
1132
1133 /* data and/or status stage for control request */
David Brownell7489d142007-01-16 22:51:04 -08001134 } else if (dev->state == STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /* IN DATA+STATUS caller makes len <= wLength */
1137 if (dev->setup_in) {
1138 retval = setup_req (dev->gadget->ep0, dev->req, len);
1139 if (retval == 0) {
David Brownell5b89db022007-01-16 22:56:26 -08001140 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 spin_unlock_irq (&dev->lock);
1142 if (copy_from_user (dev->req->buf, buf, len))
1143 retval = -EFAULT;
Alan Stern97906362006-01-03 10:30:31 -05001144 else {
1145 if (len < dev->setup_wLength)
1146 dev->req->zero = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 retval = usb_ep_queue (
1148 dev->gadget->ep0, dev->req,
1149 GFP_KERNEL);
Alan Stern97906362006-01-03 10:30:31 -05001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (retval < 0) {
1152 spin_lock_irq (&dev->lock);
1153 clean_req (dev->gadget->ep0, dev->req);
1154 spin_unlock_irq (&dev->lock);
1155 } else
1156 retval = len;
1157
1158 return retval;
1159 }
1160
1161 /* can stall some OUT transfers */
1162 } else if (dev->setup_can_stall) {
1163 VDEBUG(dev, "ep0out stall\n");
1164 (void) usb_ep_set_halt (dev->gadget->ep0);
1165 retval = -EL2HLT;
David Brownell7489d142007-01-16 22:51:04 -08001166 dev->state = STATE_DEV_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
1168 DBG(dev, "bogus ep0out stall!\n");
1169 }
1170 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -08001171 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 spin_unlock_irq (&dev->lock);
1174 return retval;
1175}
1176
1177static int
1178ep0_fasync (int f, struct file *fd, int on)
1179{
1180 struct dev_data *dev = fd->private_data;
1181 // caller must F_SETOWN before signal delivery happens
Harvey Harrison441b62c2008-03-03 16:08:34 -08001182 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return fasync_helper (f, fd, on, &dev->fasync);
1184}
1185
1186static struct usb_gadget_driver gadgetfs_driver;
1187
1188static int
1189dev_release (struct inode *inode, struct file *fd)
1190{
1191 struct dev_data *dev = fd->private_data;
1192
1193 /* closing ep0 === shutdown all */
1194
1195 usb_gadget_unregister_driver (&gadgetfs_driver);
1196
1197 /* at this point "good" hardware has disconnected the
1198 * device from USB; the host won't see it any more.
1199 * alternatively, all host requests will time out.
1200 */
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 kfree (dev->buf);
1203 dev->buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Marcus Nutzingerf0cae932014-06-05 17:17:06 +02001205 /* other endpoints were all decoupled from this device */
1206 spin_lock_irq(&dev->lock);
1207 dev->state = STATE_DEV_DISABLED;
1208 spin_unlock_irq(&dev->lock);
1209
1210 put_dev (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return 0;
1212}
1213
Milan Svobodae22fc272006-08-08 22:23:12 -07001214static unsigned int
1215ep0_poll (struct file *fd, poll_table *wait)
1216{
1217 struct dev_data *dev = fd->private_data;
1218 int mask = 0;
1219
1220 poll_wait(fd, &dev->wait, wait);
1221
1222 spin_lock_irq (&dev->lock);
1223
1224 /* report fd mode change before acting on it */
1225 if (dev->setup_abort) {
1226 dev->setup_abort = 0;
1227 mask = POLLHUP;
1228 goto out;
1229 }
1230
David Brownell7489d142007-01-16 22:51:04 -08001231 if (dev->state == STATE_DEV_SETUP) {
Milan Svobodae22fc272006-08-08 22:23:12 -07001232 if (dev->setup_in || dev->setup_can_stall)
1233 mask = POLLOUT;
1234 } else {
1235 if (dev->ev_next != 0)
1236 mask = POLLIN;
1237 }
1238out:
1239 spin_unlock_irq(&dev->lock);
1240 return mask;
1241}
1242
Alan Cox44c389a2008-05-22 22:03:27 +01001243static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct dev_data *dev = fd->private_data;
1246 struct usb_gadget *gadget = dev->gadget;
Alan Cox44c389a2008-05-22 22:03:27 +01001247 long ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Arnd Bergmann1548b132010-06-01 23:04:44 +02001249 if (gadget->ops->ioctl)
Alan Cox44c389a2008-05-22 22:03:27 +01001250 ret = gadget->ops->ioctl (gadget, code, value);
Arnd Bergmann1548b132010-06-01 23:04:44 +02001251
Alan Cox44c389a2008-05-22 22:03:27 +01001252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/* used after device configuration */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03001256static const struct file_operations ep0_io_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 .owner = THIS_MODULE,
1258 .llseek = no_llseek,
1259
1260 .read = ep0_read,
1261 .write = ep0_write,
1262 .fasync = ep0_fasync,
Milan Svobodae22fc272006-08-08 22:23:12 -07001263 .poll = ep0_poll,
Alan Cox44c389a2008-05-22 22:03:27 +01001264 .unlocked_ioctl = dev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 .release = dev_release,
1266};
1267
1268/*----------------------------------------------------------------------*/
1269
1270/* The in-kernel gadget driver handles most ep0 issues, in particular
1271 * enumerating the single configuration (as provided from user space).
1272 *
1273 * Unrecognized ep0 requests may be handled in user space.
1274 */
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static void make_qualifier (struct dev_data *dev)
1277{
1278 struct usb_qualifier_descriptor qual;
1279 struct usb_device_descriptor *desc;
1280
1281 qual.bLength = sizeof qual;
1282 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
Harvey Harrison551509d2009-02-11 14:11:36 -08001283 qual.bcdUSB = cpu_to_le16 (0x0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 desc = dev->dev;
1286 qual.bDeviceClass = desc->bDeviceClass;
1287 qual.bDeviceSubClass = desc->bDeviceSubClass;
1288 qual.bDeviceProtocol = desc->bDeviceProtocol;
1289
1290 /* assumes ep0 uses the same value for both speeds ... */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +02001291 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 qual.bNumConfigurations = 1;
1294 qual.bRESERVED = 0;
1295
1296 memcpy (dev->rbuf, &qual, sizeof qual);
1297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299static int
1300config_buf (struct dev_data *dev, u8 type, unsigned index)
1301{
1302 int len;
David Brownella4e3ef52007-08-01 23:58:22 -07001303 int hs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* only one configuration */
1306 if (index > 0)
1307 return -EINVAL;
1308
David Brownella4e3ef52007-08-01 23:58:22 -07001309 if (gadget_is_dualspeed(dev->gadget)) {
1310 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1311 if (type == USB_DT_OTHER_SPEED_CONFIG)
1312 hs = !hs;
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (hs) {
1315 dev->req->buf = dev->hs_config;
David Brownell01ee7d72007-05-25 20:40:14 -07001316 len = le16_to_cpu(dev->hs_config->wTotalLength);
David Brownella4e3ef52007-08-01 23:58:22 -07001317 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 dev->req->buf = dev->config;
David Brownell01ee7d72007-05-25 20:40:14 -07001319 len = le16_to_cpu(dev->config->wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 ((u8 *)dev->req->buf) [1] = type;
1322 return len;
1323}
1324
1325static int
1326gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1327{
1328 struct dev_data *dev = get_gadget_data (gadget);
1329 struct usb_request *req = dev->req;
1330 int value = -EOPNOTSUPP;
1331 struct usb_gadgetfs_event *event;
David Brownell1bbc1692005-05-07 13:05:13 -07001332 u16 w_value = le16_to_cpu(ctrl->wValue);
1333 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 spin_lock (&dev->lock);
1336 dev->setup_abort = 0;
David Brownell7489d142007-01-16 22:51:04 -08001337 if (dev->state == STATE_DEV_UNCONNECTED) {
David Brownella4e3ef52007-08-01 23:58:22 -07001338 if (gadget_is_dualspeed(gadget)
1339 && gadget->speed == USB_SPEED_HIGH
1340 && dev->hs_config == NULL) {
David Brownellce467942007-01-16 23:06:07 -08001341 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 ERROR (dev, "no high speed config??\n");
1343 return -EINVAL;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
David Brownellce467942007-01-16 23:06:07 -08001346 dev->state = STATE_DEV_CONNECTED;
David Brownellce467942007-01-16 23:06:07 -08001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 INFO (dev, "connected\n");
1349 event = next_event (dev, GADGETFS_CONNECT);
1350 event->u.speed = gadget->speed;
1351 ep0_readable (dev);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* host may have given up waiting for response. we can miss control
1354 * requests handled lower down (device/endpoint status and features);
1355 * then ep0_{read,write} will report the wrong status. controller
1356 * driver will have aborted pending i/o.
1357 */
David Brownell7489d142007-01-16 22:51:04 -08001358 } else if (dev->state == STATE_DEV_SETUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 dev->setup_abort = 1;
1360
1361 req->buf = dev->rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 req->context = NULL;
1363 value = -EOPNOTSUPP;
1364 switch (ctrl->bRequest) {
1365
1366 case USB_REQ_GET_DESCRIPTOR:
1367 if (ctrl->bRequestType != USB_DIR_IN)
1368 goto unrecognized;
1369 switch (w_value >> 8) {
1370
1371 case USB_DT_DEVICE:
1372 value = min (w_length, (u16) sizeof *dev->dev);
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +02001373 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 req->buf = dev->dev;
1375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 case USB_DT_DEVICE_QUALIFIER:
1377 if (!dev->hs_config)
1378 break;
1379 value = min (w_length, (u16)
1380 sizeof (struct usb_qualifier_descriptor));
1381 make_qualifier (dev);
1382 break;
1383 case USB_DT_OTHER_SPEED_CONFIG:
1384 // FALLTHROUGH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 case USB_DT_CONFIG:
1386 value = config_buf (dev,
1387 w_value >> 8,
1388 w_value & 0xff);
1389 if (value >= 0)
1390 value = min (w_length, (u16) value);
1391 break;
1392 case USB_DT_STRING:
1393 goto unrecognized;
1394
1395 default: // all others are errors
1396 break;
1397 }
1398 break;
1399
1400 /* currently one config, two speeds */
1401 case USB_REQ_SET_CONFIGURATION:
1402 if (ctrl->bRequestType != 0)
Roy Hashimoto12cd5b92008-03-12 13:55:31 -08001403 goto unrecognized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (0 == (u8) w_value) {
1405 value = 0;
1406 dev->current_config = 0;
1407 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1408 // user mode expected to disable endpoints
1409 } else {
1410 u8 config, power;
David Brownella4e3ef52007-08-01 23:58:22 -07001411
1412 if (gadget_is_dualspeed(gadget)
1413 && gadget->speed == USB_SPEED_HIGH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 config = dev->hs_config->bConfigurationValue;
1415 power = dev->hs_config->bMaxPower;
David Brownella4e3ef52007-08-01 23:58:22 -07001416 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 config = dev->config->bConfigurationValue;
1418 power = dev->config->bMaxPower;
1419 }
1420
1421 if (config == (u8) w_value) {
1422 value = 0;
1423 dev->current_config = config;
1424 usb_gadget_vbus_draw(gadget, 2 * power);
1425 }
1426 }
1427
1428 /* report SET_CONFIGURATION like any other control request,
1429 * except that usermode may not stall this. the next
1430 * request mustn't be allowed start until this finishes:
1431 * endpoints and threads set up, etc.
1432 *
1433 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1434 * has bad/racey automagic that prevents synchronizing here.
1435 * even kernel mode drivers often miss them.
1436 */
1437 if (value == 0) {
1438 INFO (dev, "configuration #%d\n", dev->current_config);
Peter Chen6027f312014-04-29 13:26:28 +08001439 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (dev->usermode_setup) {
1441 dev->setup_can_stall = 0;
1442 goto delegate;
1443 }
1444 }
1445 break;
1446
Paul Bolled30f2062014-05-26 23:37:09 +02001447#ifndef CONFIG_USB_PXA25X
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* PXA automagically handles this request too */
1449 case USB_REQ_GET_CONFIGURATION:
1450 if (ctrl->bRequestType != 0x80)
Roy Hashimoto12cd5b92008-03-12 13:55:31 -08001451 goto unrecognized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *(u8 *)req->buf = dev->current_config;
1453 value = min (w_length, (u16) 1);
1454 break;
1455#endif
1456
1457 default:
1458unrecognized:
1459 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1460 dev->usermode_setup ? "delegate" : "fail",
1461 ctrl->bRequestType, ctrl->bRequest,
1462 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1463
1464 /* if there's an ep0 reader, don't stall */
1465 if (dev->usermode_setup) {
1466 dev->setup_can_stall = 1;
1467delegate:
1468 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1469 ? 1 : 0;
Alan Stern97906362006-01-03 10:30:31 -05001470 dev->setup_wLength = w_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 dev->setup_out_ready = 0;
1472 dev->setup_out_error = 0;
1473 value = 0;
1474
1475 /* read DATA stage for OUT right away */
1476 if (unlikely (!dev->setup_in && w_length)) {
1477 value = setup_req (gadget->ep0, dev->req,
1478 w_length);
1479 if (value < 0)
1480 break;
1481 value = usb_ep_queue (gadget->ep0, dev->req,
1482 GFP_ATOMIC);
1483 if (value < 0) {
1484 clean_req (gadget->ep0, dev->req);
1485 break;
1486 }
1487
1488 /* we can't currently stall these */
1489 dev->setup_can_stall = 0;
1490 }
1491
1492 /* state changes when reader collects event */
1493 event = next_event (dev, GADGETFS_SETUP);
1494 event->u.setup = *ctrl;
1495 ep0_readable (dev);
1496 spin_unlock (&dev->lock);
1497 return 0;
1498 }
1499 }
1500
1501 /* proceed with data transfer and status phases? */
David Brownell7489d142007-01-16 22:51:04 -08001502 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 req->length = value;
1504 req->zero = value < w_length;
1505 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1506 if (value < 0) {
1507 DBG (dev, "ep_queue --> %d\n", value);
1508 req->status = 0;
1509 }
1510 }
1511
1512 /* device stalls when value < 0 */
1513 spin_unlock (&dev->lock);
1514 return value;
1515}
1516
1517static void destroy_ep_files (struct dev_data *dev)
1518{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001519 DBG (dev, "%s %d\n", __func__, dev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /* dev->state must prevent interference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 spin_lock_irq (&dev->lock);
Al Viro104bb372012-01-08 16:13:28 -05001523 while (!list_empty(&dev->epfiles)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct ep_data *ep;
1525 struct inode *parent;
1526 struct dentry *dentry;
1527
1528 /* break link to FS */
Al Viro104bb372012-01-08 16:13:28 -05001529 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 list_del_init (&ep->epfiles);
1531 dentry = ep->dentry;
1532 ep->dentry = NULL;
1533 parent = dentry->d_parent->d_inode;
1534
1535 /* break link to controller */
1536 if (ep->state == STATE_EP_ENABLED)
1537 (void) usb_ep_disable (ep->ep);
1538 ep->state = STATE_EP_UNBOUND;
1539 usb_ep_free_request (ep->ep, ep->req);
1540 ep->ep = NULL;
1541 wake_up (&ep->wait);
1542 put_ep (ep);
1543
1544 spin_unlock_irq (&dev->lock);
1545
1546 /* break link to dcache */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001547 mutex_lock (&parent->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 d_delete (dentry);
1549 dput (dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001550 mutex_unlock (&parent->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Al Viro104bb372012-01-08 16:13:28 -05001552 spin_lock_irq (&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 spin_unlock_irq (&dev->lock);
1555}
1556
1557
Al Virofb6c3222014-09-03 13:37:56 -04001558static struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559gadgetfs_create_file (struct super_block *sb, char const *name,
Al Virofb6c3222014-09-03 13:37:56 -04001560 void *data, const struct file_operations *fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562static int activate_ep_files (struct dev_data *dev)
1563{
1564 struct usb_ep *ep;
Alan Stern0ae4ea82006-05-22 12:27:38 -04001565 struct ep_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 gadget_for_each_ep (ep, dev->gadget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Eric Sesterhenn7039f422006-02-27 13:34:10 -08001569 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!data)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001571 goto enomem0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 data->state = STATE_EP_DISABLED;
Thomas Gleixnera79df502010-01-29 20:38:59 +00001573 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 init_waitqueue_head (&data->wait);
1575
1576 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1577 atomic_set (&data->count, 1);
1578 data->dev = dev;
1579 get_dev (dev);
1580
1581 data->ep = ep;
1582 ep->driver_data = data;
1583
1584 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1585 if (!data->req)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001586 goto enomem1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Al Virofb6c3222014-09-03 13:37:56 -04001588 data->dentry = gadgetfs_create_file (dev->sb, data->name,
1589 data, &ep_config_operations);
1590 if (!data->dentry)
Alan Stern0ae4ea82006-05-22 12:27:38 -04001591 goto enomem2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 list_add_tail (&data->epfiles, &dev->epfiles);
1593 }
1594 return 0;
1595
Alan Stern0ae4ea82006-05-22 12:27:38 -04001596enomem2:
1597 usb_ep_free_request (ep, data->req);
1598enomem1:
1599 put_dev (dev);
1600 kfree (data);
1601enomem0:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001602 DBG (dev, "%s enomem\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 destroy_ep_files (dev);
1604 return -ENOMEM;
1605}
1606
1607static void
1608gadgetfs_unbind (struct usb_gadget *gadget)
1609{
1610 struct dev_data *dev = get_gadget_data (gadget);
1611
Harvey Harrison441b62c2008-03-03 16:08:34 -08001612 DBG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 spin_lock_irq (&dev->lock);
1615 dev->state = STATE_DEV_UNBOUND;
1616 spin_unlock_irq (&dev->lock);
1617
1618 destroy_ep_files (dev);
1619 gadget->ep0->driver_data = NULL;
1620 set_gadget_data (gadget, NULL);
1621
1622 /* we've already been disconnected ... no i/o is active */
1623 if (dev->req)
1624 usb_ep_free_request (gadget->ep0, dev->req);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 DBG (dev, "%s done\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 put_dev (dev);
1627}
1628
1629static struct dev_data *the_device;
1630
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001631static int gadgetfs_bind(struct usb_gadget *gadget,
1632 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct dev_data *dev = the_device;
1635
1636 if (!dev)
1637 return -ESRCH;
1638 if (0 != strcmp (CHIP, gadget->name)) {
David Brownell00274922007-11-19 12:58:36 -08001639 pr_err("%s expected %s controller not %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 shortname, CHIP, gadget->name);
1641 return -ENODEV;
1642 }
1643
1644 set_gadget_data (gadget, dev);
1645 dev->gadget = gadget;
1646 gadget->ep0->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* preallocate control response and buffer */
1649 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1650 if (!dev->req)
1651 goto enomem;
1652 dev->req->context = NULL;
1653 dev->req->complete = epio_complete;
1654
1655 if (activate_ep_files (dev) < 0)
1656 goto enomem;
1657
1658 INFO (dev, "bound to %s driver\n", gadget->name);
David Brownell7489d142007-01-16 22:51:04 -08001659 spin_lock_irq(&dev->lock);
1660 dev->state = STATE_DEV_UNCONNECTED;
1661 spin_unlock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 get_dev (dev);
1663 return 0;
1664
1665enomem:
1666 gadgetfs_unbind (gadget);
1667 return -ENOMEM;
1668}
1669
1670static void
1671gadgetfs_disconnect (struct usb_gadget *gadget)
1672{
1673 struct dev_data *dev = get_gadget_data (gadget);
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001674 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001676 spin_lock_irqsave (&dev->lock, flags);
David Brownell7489d142007-01-16 22:51:04 -08001677 if (dev->state == STATE_DEV_UNCONNECTED)
Milan Svoboda07cb7f22006-06-26 07:46:00 -07001678 goto exit;
David Brownell7489d142007-01-16 22:51:04 -08001679 dev->state = STATE_DEV_UNCONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 INFO (dev, "disconnected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 next_event (dev, GADGETFS_DISCONNECT);
1683 ep0_readable (dev);
Milan Svoboda07cb7f22006-06-26 07:46:00 -07001684exit:
Marc Kleine-Budde001428e2011-10-10 18:38:05 +02001685 spin_unlock_irqrestore (&dev->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688static void
1689gadgetfs_suspend (struct usb_gadget *gadget)
1690{
1691 struct dev_data *dev = get_gadget_data (gadget);
1692
1693 INFO (dev, "suspended from state %d\n", dev->state);
1694 spin_lock (&dev->lock);
1695 switch (dev->state) {
David Brownell7489d142007-01-16 22:51:04 -08001696 case STATE_DEV_SETUP: // VERY odd... host died??
1697 case STATE_DEV_CONNECTED:
1698 case STATE_DEV_UNCONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 next_event (dev, GADGETFS_SUSPEND);
1700 ep0_readable (dev);
1701 /* FALLTHROUGH */
1702 default:
1703 break;
1704 }
1705 spin_unlock (&dev->lock);
1706}
1707
1708static struct usb_gadget_driver gadgetfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .function = (char *) driver_desc,
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001710 .bind = gadgetfs_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 .unbind = gadgetfs_unbind,
1712 .setup = gadgetfs_setup,
Peter Chen0eba4552014-09-09 08:56:51 +08001713 .reset = gadgetfs_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .disconnect = gadgetfs_disconnect,
1715 .suspend = gadgetfs_suspend,
1716
David Brownell25051072007-01-15 11:30:28 -08001717 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 .name = (char *) shortname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 },
1720};
1721
1722/*----------------------------------------------------------------------*/
1723
1724static void gadgetfs_nop(struct usb_gadget *arg) { }
1725
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001726static int gadgetfs_probe(struct usb_gadget *gadget,
1727 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 CHIP = gadget->name;
1730 return -EISNAM;
1731}
1732
1733static struct usb_gadget_driver probe_driver = {
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01001734 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001735 .bind = gadgetfs_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 .unbind = gadgetfs_nop,
1737 .setup = (void *)gadgetfs_nop,
1738 .disconnect = gadgetfs_nop,
David Brownell25051072007-01-15 11:30:28 -08001739 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .name = "nop",
1741 },
1742};
1743
1744
1745/* DEVICE INITIALIZATION
1746 *
1747 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1748 * status = write (fd, descriptors, sizeof descriptors)
1749 *
1750 * That write establishes the device configuration, so the kernel can
1751 * bind to the controller ... guaranteeing it can handle enumeration
1752 * at all necessary speeds. Descriptor order is:
1753 *
1754 * . message tag (u32, host order) ... for now, must be zero; it
1755 * would change to support features like multi-config devices
1756 * . full/low speed config ... all wTotalLength bytes (with interface,
1757 * class, altsetting, endpoint, and other descriptors)
1758 * . high speed config ... all descriptors, for high speed operation;
David Brownell25051072007-01-15 11:30:28 -08001759 * this one's optional except for high-speed hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * . device descriptor
1761 *
Phil Endecott511779f2007-01-15 11:35:01 -08001762 * Endpoints are not yet enabled. Drivers must wait until device
1763 * configuration and interface altsetting changes create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * the need to configure (or unconfigure) them.
1765 *
1766 * After initialization, the device stays active for as long as that
Phil Endecott511779f2007-01-15 11:35:01 -08001767 * $CHIP file is open. Events must then be read from that descriptor,
1768 * such as configuration notifications.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
1770
1771static int is_valid_config (struct usb_config_descriptor *config)
1772{
1773 return config->bDescriptorType == USB_DT_CONFIG
1774 && config->bLength == USB_DT_CONFIG_SIZE
1775 && config->bConfigurationValue != 0
1776 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1777 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1778 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1779 /* FIXME check lengths: walk to end */
1780}
1781
1782static ssize_t
1783dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1784{
1785 struct dev_data *dev = fd->private_data;
1786 ssize_t value = len, length = len;
1787 unsigned total;
1788 u32 tag;
1789 char *kbuf;
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1792 return -EINVAL;
1793
1794 /* we might need to change message format someday */
1795 if (copy_from_user (&tag, buf, 4))
1796 return -EFAULT;
1797 if (tag != 0)
1798 return -EINVAL;
1799 buf += 4;
1800 length -= 4;
1801
Julia Lawallbe8a0582010-05-22 10:26:22 +02001802 kbuf = memdup_user(buf, length);
1803 if (IS_ERR(kbuf))
1804 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 spin_lock_irq (&dev->lock);
1807 value = -EINVAL;
1808 if (dev->buf)
1809 goto fail;
1810 dev->buf = kbuf;
1811
1812 /* full or low speed config */
1813 dev->config = (void *) kbuf;
David Brownell01ee7d72007-05-25 20:40:14 -07001814 total = le16_to_cpu(dev->config->wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!is_valid_config (dev->config) || total >= length)
1816 goto fail;
1817 kbuf += total;
1818 length -= total;
1819
1820 /* optional high speed config */
1821 if (kbuf [1] == USB_DT_CONFIG) {
1822 dev->hs_config = (void *) kbuf;
David Brownell01ee7d72007-05-25 20:40:14 -07001823 total = le16_to_cpu(dev->hs_config->wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (!is_valid_config (dev->hs_config) || total >= length)
1825 goto fail;
1826 kbuf += total;
1827 length -= total;
1828 }
1829
1830 /* could support multiple configs, using another encoding! */
1831
1832 /* device descriptor (tweaked for paranoia) */
1833 if (length != USB_DT_DEVICE_SIZE)
1834 goto fail;
1835 dev->dev = (void *)kbuf;
1836 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1837 || dev->dev->bDescriptorType != USB_DT_DEVICE
1838 || dev->dev->bNumConfigurations != 1)
1839 goto fail;
1840 dev->dev->bNumConfigurations = 1;
Harvey Harrison551509d2009-02-11 14:11:36 -08001841 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /* triggers gadgetfs_bind(); then we can enumerate. */
1844 spin_unlock_irq (&dev->lock);
Michal Nazarewicz85b86142012-08-24 20:46:18 +02001845 if (dev->hs_config)
1846 gadgetfs_driver.max_speed = USB_SPEED_HIGH;
1847 else
1848 gadgetfs_driver.max_speed = USB_SPEED_FULL;
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001849
1850 value = usb_gadget_probe_driver(&gadgetfs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (value != 0) {
1852 kfree (dev->buf);
1853 dev->buf = NULL;
1854 } else {
1855 /* at this point "good" hardware has for the first time
1856 * let the USB the host see us. alternatively, if users
1857 * unplug/replug that will clear all the error state.
1858 *
1859 * note: everything running before here was guaranteed
1860 * to choke driver model style diagnostics. from here
1861 * on, they can work ... except in cleanup paths that
1862 * kick in after the ep0 descriptor is closed.
1863 */
1864 fd->f_op = &ep0_io_operations;
1865 value = len;
1866 }
1867 return value;
1868
1869fail:
1870 spin_unlock_irq (&dev->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001871 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 kfree (dev->buf);
1873 dev->buf = NULL;
1874 return value;
1875}
1876
1877static int
1878dev_open (struct inode *inode, struct file *fd)
1879{
Theodore Ts'o8e18e292006-09-27 01:50:46 -07001880 struct dev_data *dev = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int value = -EBUSY;
1882
David Brownell7489d142007-01-16 22:51:04 -08001883 spin_lock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (dev->state == STATE_DEV_DISABLED) {
1885 dev->ev_next = 0;
David Brownell7489d142007-01-16 22:51:04 -08001886 dev->state = STATE_DEV_OPENED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 fd->private_data = dev;
1888 get_dev (dev);
1889 value = 0;
1890 }
David Brownell7489d142007-01-16 22:51:04 -08001891 spin_unlock_irq(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return value;
1893}
1894
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03001895static const struct file_operations dev_init_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .llseek = no_llseek,
1897
1898 .open = dev_open,
1899 .write = dev_config,
1900 .fasync = ep0_fasync,
Alan Cox44c389a2008-05-22 22:03:27 +01001901 .unlocked_ioctl = dev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .release = dev_release,
1903};
1904
1905/*----------------------------------------------------------------------*/
1906
1907/* FILESYSTEM AND SUPERBLOCK OPERATIONS
1908 *
1909 * Mounting the filesystem creates a controller file, used first for
1910 * device configuration then later for event monitoring.
1911 */
1912
1913
1914/* FIXME PAM etc could set this security policy without mount options
1915 * if epfiles inherited ownership and permissons from ep0 ...
1916 */
1917
1918static unsigned default_uid;
1919static unsigned default_gid;
1920static unsigned default_perm = S_IRUSR | S_IWUSR;
1921
1922module_param (default_uid, uint, 0644);
1923module_param (default_gid, uint, 0644);
1924module_param (default_perm, uint, 0644);
1925
1926
1927static struct inode *
1928gadgetfs_make_inode (struct super_block *sb,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001929 void *data, const struct file_operations *fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int mode)
1931{
1932 struct inode *inode = new_inode (sb);
1933
1934 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001935 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 inode->i_mode = mode;
Eric W. Biederman32d639c2012-02-07 16:32:04 -08001937 inode->i_uid = make_kuid(&init_user_ns, default_uid);
1938 inode->i_gid = make_kgid(&init_user_ns, default_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 inode->i_atime = inode->i_mtime = inode->i_ctime
1940 = CURRENT_TIME;
Theodore Ts'o8e18e292006-09-27 01:50:46 -07001941 inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 inode->i_fop = fops;
1943 }
1944 return inode;
1945}
1946
1947/* creates in fs root directory, so non-renamable and non-linkable.
1948 * so inode and dentry are paired, until device reconfig.
1949 */
Al Virofb6c3222014-09-03 13:37:56 -04001950static struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951gadgetfs_create_file (struct super_block *sb, char const *name,
Al Virofb6c3222014-09-03 13:37:56 -04001952 void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 struct dentry *dentry;
1955 struct inode *inode;
1956
1957 dentry = d_alloc_name(sb->s_root, name);
1958 if (!dentry)
1959 return NULL;
1960
1961 inode = gadgetfs_make_inode (sb, data, fops,
1962 S_IFREG | (default_perm & S_IRWXUGO));
1963 if (!inode) {
1964 dput(dentry);
1965 return NULL;
1966 }
1967 d_add (dentry, inode);
Al Virofb6c3222014-09-03 13:37:56 -04001968 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001971static const struct super_operations gadget_fs_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 .statfs = simple_statfs,
1973 .drop_inode = generic_delete_inode,
1974};
1975
1976static int
1977gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
1978{
1979 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct dev_data *dev;
1981
1982 if (the_device)
1983 return -ESRCH;
1984
1985 /* fake probe to determine $CHIP */
Lubomir Rintel5cdf7d52014-03-27 08:09:21 +01001986 CHIP = NULL;
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001987 usb_gadget_probe_driver(&probe_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (!CHIP)
1989 return -ENODEV;
1990
1991 /* superblock */
1992 sb->s_blocksize = PAGE_CACHE_SIZE;
1993 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1994 sb->s_magic = GADGETFS_MAGIC;
1995 sb->s_op = &gadget_fs_operations;
1996 sb->s_time_gran = 1;
1997
1998 /* root inode */
1999 inode = gadgetfs_make_inode (sb,
2000 NULL, &simple_dir_operations,
2001 S_IFDIR | S_IRUGO | S_IXUGO);
2002 if (!inode)
Al Viro87da5b32012-01-08 15:59:45 -05002003 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 inode->i_op = &simple_dir_inode_operations;
Al Viro48fde702012-01-08 22:15:13 -05002005 if (!(sb->s_root = d_make_root (inode)))
Al Viro87da5b32012-01-08 15:59:45 -05002006 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* the ep0 file is named after the controller we expect;
2009 * user mode code can use it for sanity checks, like we do.
2010 */
2011 dev = dev_new ();
2012 if (!dev)
Al Viro87da5b32012-01-08 15:59:45 -05002013 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 dev->sb = sb;
Al Virofb6c3222014-09-03 13:37:56 -04002016 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &dev_init_operations);
2017 if (!dev->dentry) {
Al Viro87da5b32012-01-08 15:59:45 -05002018 put_dev(dev);
2019 goto Enomem;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 /* other endpoint files are available after hardware setup,
2023 * from binding to a controller.
2024 */
2025 the_device = dev;
2026 return 0;
Alan Stern0ae4ea82006-05-22 12:27:38 -04002027
Al Viro87da5b32012-01-08 15:59:45 -05002028Enomem:
Alan Stern0ae4ea82006-05-22 12:27:38 -04002029 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032/* "mount -t gadgetfs path /dev/gadget" ends up here */
Al Virofc14f2f2010-07-25 01:48:30 +04002033static struct dentry *
2034gadgetfs_mount (struct file_system_type *t, int flags,
2035 const char *path, void *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Al Virofc14f2f2010-07-25 01:48:30 +04002037 return mount_single (t, flags, opts, gadgetfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040static void
2041gadgetfs_kill_sb (struct super_block *sb)
2042{
2043 kill_litter_super (sb);
2044 if (the_device) {
2045 put_dev (the_device);
2046 the_device = NULL;
2047 }
2048}
2049
2050/*----------------------------------------------------------------------*/
2051
2052static struct file_system_type gadgetfs_type = {
2053 .owner = THIS_MODULE,
2054 .name = shortname,
Al Virofc14f2f2010-07-25 01:48:30 +04002055 .mount = gadgetfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 .kill_sb = gadgetfs_kill_sb,
2057};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002058MODULE_ALIAS_FS("gadgetfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060/*----------------------------------------------------------------------*/
2061
2062static int __init init (void)
2063{
2064 int status;
2065
2066 status = register_filesystem (&gadgetfs_type);
2067 if (status == 0)
2068 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2069 shortname, driver_desc);
2070 return status;
2071}
2072module_init (init);
2073
2074static void __exit cleanup (void)
2075{
2076 pr_debug ("unregister %s\n", shortname);
2077 unregister_filesystem (&gadgetfs_type);
2078}
2079module_exit (cleanup);
2080