gadgetfs: get rid of flipping ->f_op in ep_config()
[deliverable/linux.git] / drivers / usb / gadget / legacy / inode.c
CommitLineData
1da177e4
LT
1/*
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.
1da177e4
LT
11 */
12
13
a4e3ef55 14/* #define VERBOSE_DEBUG */
1da177e4
LT
15
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>
a99bbaf5 24#include <linux/sched.h>
1da177e4 25#include <linux/slab.h>
e22fc27c 26#include <linux/poll.h>
a80bf61e 27#include <linux/mmu_context.h>
4e179bca 28#include <linux/aio.h>
1da177e4
LT
29
30#include <linux/device.h>
31#include <linux/moduleparam.h>
32
a5262dcf 33#include <linux/usb/gadgetfs.h>
9454a57a 34#include <linux/usb/gadget.h>
1da177e4
LT
35
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 *
511779fd
PE
55 * - 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).
1da177e4
LT
60 *
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
d4461a60
AV
77static int ep_open(struct inode *, struct file *);
78
1da177e4
LT
79
80/*----------------------------------------------------------------------*/
81
82#define GADGETFS_MAGIC 0xaee71ee7
1da177e4
LT
83
84/* /dev/gadget/$CHIP represents ep0 and the whole device */
85enum ep0_state {
86 /* DISBLED is the initial state.
87 */
88 STATE_DEV_DISABLED = 0,
89
90 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
91 * ep0/device i/o modes and binding to the controller. Driver
92 * must always write descriptors to initialize the device, then
93 * the device becomes UNCONNECTED until enumeration.
94 */
7489d149 95 STATE_DEV_OPENED,
1da177e4
LT
96
97 /* From then on, ep0 fd is in either of two basic modes:
98 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
99 * - SETUP: read/write will transfer control data and succeed;
100 * or if "wrong direction", performs protocol stall
101 */
7489d149
DB
102 STATE_DEV_UNCONNECTED,
103 STATE_DEV_CONNECTED,
104 STATE_DEV_SETUP,
1da177e4
LT
105
106 /* UNBOUND means the driver closed ep0, so the device won't be
107 * accessible again (DEV_DISABLED) until all fds are closed.
108 */
109 STATE_DEV_UNBOUND,
110};
111
112/* enough for the whole queue: most events invalidate others */
113#define N_EVENT 5
114
115struct dev_data {
116 spinlock_t lock;
117 atomic_t count;
7489d149 118 enum ep0_state state; /* P: lock */
1da177e4
LT
119 struct usb_gadgetfs_event event [N_EVENT];
120 unsigned ev_next;
121 struct fasync_struct *fasync;
122 u8 current_config;
123
124 /* drivers reading ep0 MUST handle control requests (SETUP)
125 * reported that way; else the host will time out.
126 */
127 unsigned usermode_setup : 1,
128 setup_in : 1,
129 setup_can_stall : 1,
130 setup_out_ready : 1,
131 setup_out_error : 1,
132 setup_abort : 1;
97906369 133 unsigned setup_wLength;
1da177e4
LT
134
135 /* the rest is basically write-once */
136 struct usb_config_descriptor *config, *hs_config;
137 struct usb_device_descriptor *dev;
138 struct usb_request *req;
139 struct usb_gadget *gadget;
140 struct list_head epfiles;
141 void *buf;
142 wait_queue_head_t wait;
143 struct super_block *sb;
144 struct dentry *dentry;
145
146 /* except this scratch i/o buffer for ep0 */
147 u8 rbuf [256];
148};
149
150static inline void get_dev (struct dev_data *data)
151{
152 atomic_inc (&data->count);
153}
154
155static void put_dev (struct dev_data *data)
156{
157 if (likely (!atomic_dec_and_test (&data->count)))
158 return;
159 /* needs no more cleanup */
160 BUG_ON (waitqueue_active (&data->wait));
161 kfree (data);
162}
163
164static struct dev_data *dev_new (void)
165{
166 struct dev_data *dev;
167
7039f422 168 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1da177e4
LT
169 if (!dev)
170 return NULL;
1da177e4
LT
171 dev->state = STATE_DEV_DISABLED;
172 atomic_set (&dev->count, 1);
173 spin_lock_init (&dev->lock);
174 INIT_LIST_HEAD (&dev->epfiles);
175 init_waitqueue_head (&dev->wait);
176 return dev;
177}
178
179/*----------------------------------------------------------------------*/
180
181/* other /dev/gadget/$ENDPOINT files represent endpoints */
182enum ep_state {
183 STATE_EP_DISABLED = 0,
184 STATE_EP_READY,
1da177e4
LT
185 STATE_EP_ENABLED,
186 STATE_EP_UNBOUND,
187};
188
189struct ep_data {
a79df50b 190 struct mutex lock;
1da177e4
LT
191 enum ep_state state;
192 atomic_t count;
193 struct dev_data *dev;
194 /* must hold dev->lock before accessing ep or req */
195 struct usb_ep *ep;
196 struct usb_request *req;
197 ssize_t status;
198 char name [16];
199 struct usb_endpoint_descriptor desc, hs_desc;
200 struct list_head epfiles;
201 wait_queue_head_t wait;
202 struct dentry *dentry;
1da177e4
LT
203};
204
205static inline void get_ep (struct ep_data *data)
206{
207 atomic_inc (&data->count);
208}
209
210static void put_ep (struct ep_data *data)
211{
212 if (likely (!atomic_dec_and_test (&data->count)))
213 return;
214 put_dev (data->dev);
215 /* needs no more cleanup */
216 BUG_ON (!list_empty (&data->epfiles));
217 BUG_ON (waitqueue_active (&data->wait));
1da177e4
LT
218 kfree (data);
219}
220
221/*----------------------------------------------------------------------*/
222
223/* most "how to use the hardware" policy choices are in userspace:
224 * mapping endpoint roles (which the driver needs) to the capabilities
225 * which the usb controller has. most of those capabilities are exposed
226 * implicitly, starting with the driver name and then endpoint names.
227 */
228
229static const char *CHIP;
230
231/*----------------------------------------------------------------------*/
232
233/* NOTE: don't use dev_printk calls before binding to the gadget
234 * at the end of ep0 configuration, or after unbind.
235 */
236
237/* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
238#define xprintk(d,level,fmt,args...) \
239 printk(level "%s: " fmt , shortname , ## args)
240
241#ifdef DEBUG
242#define DBG(dev,fmt,args...) \
243 xprintk(dev , KERN_DEBUG , fmt , ## args)
244#else
245#define DBG(dev,fmt,args...) \
246 do { } while (0)
247#endif /* DEBUG */
248
a4e3ef55 249#ifdef VERBOSE_DEBUG
1da177e4
LT
250#define VDEBUG DBG
251#else
252#define VDEBUG(dev,fmt,args...) \
253 do { } while (0)
254#endif /* DEBUG */
255
256#define ERROR(dev,fmt,args...) \
257 xprintk(dev , KERN_ERR , fmt , ## args)
1da177e4
LT
258#define INFO(dev,fmt,args...) \
259 xprintk(dev , KERN_INFO , fmt , ## args)
260
261
262/*----------------------------------------------------------------------*/
263
264/* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
265 *
266 * After opening, configure non-control endpoints. Then use normal
267 * stream read() and write() requests; and maybe ioctl() to get more
093cf723 268 * precise FIFO status when recovering from cancellation.
1da177e4
LT
269 */
270
271static void epio_complete (struct usb_ep *ep, struct usb_request *req)
272{
273 struct ep_data *epdata = ep->driver_data;
274
275 if (!req->context)
276 return;
277 if (req->status)
278 epdata->status = req->status;
279 else
280 epdata->status = req->actual;
281 complete ((struct completion *)req->context);
282}
283
284/* tasklock endpoint, returning when it's connected.
285 * still need dev->lock to use epdata->ep.
286 */
287static int
d4461a60 288get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
1da177e4
LT
289{
290 int val;
291
292 if (f_flags & O_NONBLOCK) {
a79df50b 293 if (!mutex_trylock(&epdata->lock))
1da177e4 294 goto nonblock;
d4461a60
AV
295 if (epdata->state != STATE_EP_ENABLED &&
296 (!is_write || epdata->state != STATE_EP_READY)) {
a79df50b 297 mutex_unlock(&epdata->lock);
1da177e4
LT
298nonblock:
299 val = -EAGAIN;
300 } else
301 val = 0;
302 return val;
303 }
304
a79df50b
TG
305 val = mutex_lock_interruptible(&epdata->lock);
306 if (val < 0)
1da177e4 307 return val;
511779fd 308
1da177e4
LT
309 switch (epdata->state) {
310 case STATE_EP_ENABLED:
d4461a60
AV
311 return 0;
312 case STATE_EP_READY: /* not configured yet */
313 if (is_write)
314 return 0;
315 // FALLTHRU
316 case STATE_EP_UNBOUND: /* clean disconnect */
1da177e4 317 break;
1da177e4 318 // case STATE_EP_DISABLED: /* "can't happen" */
1da177e4
LT
319 default: /* error! */
320 pr_debug ("%s: ep %p not available, state %d\n",
321 shortname, epdata, epdata->state);
1da177e4 322 }
d4461a60
AV
323 mutex_unlock(&epdata->lock);
324 return -ENODEV;
1da177e4
LT
325}
326
327static ssize_t
328ep_io (struct ep_data *epdata, void *buf, unsigned len)
329{
6e9a4738 330 DECLARE_COMPLETION_ONSTACK (done);
1da177e4
LT
331 int value;
332
333 spin_lock_irq (&epdata->dev->lock);
334 if (likely (epdata->ep != NULL)) {
335 struct usb_request *req = epdata->req;
336
337 req->context = &done;
338 req->complete = epio_complete;
339 req->buf = buf;
340 req->length = len;
341 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
342 } else
343 value = -ENODEV;
344 spin_unlock_irq (&epdata->dev->lock);
345
346 if (likely (value == 0)) {
347 value = wait_event_interruptible (done.wait, done.done);
348 if (value != 0) {
349 spin_lock_irq (&epdata->dev->lock);
350 if (likely (epdata->ep != NULL)) {
351 DBG (epdata->dev, "%s i/o interrupted\n",
352 epdata->name);
353 usb_ep_dequeue (epdata->ep, epdata->req);
354 spin_unlock_irq (&epdata->dev->lock);
355
356 wait_event (done.wait, done.done);
357 if (epdata->status == -ECONNRESET)
358 epdata->status = -EINTR;
359 } else {
360 spin_unlock_irq (&epdata->dev->lock);
361
362 DBG (epdata->dev, "endpoint gone\n");
363 epdata->status = -ENODEV;
364 }
365 }
366 return epdata->status;
367 }
368 return value;
369}
370
1da177e4
LT
371static int
372ep_release (struct inode *inode, struct file *fd)
373{
374 struct ep_data *data = fd->private_data;
ba307f58
MS
375 int value;
376
a79df50b
TG
377 value = mutex_lock_interruptible(&data->lock);
378 if (value < 0)
ba307f58 379 return value;
1da177e4
LT
380
381 /* clean up if this can be reopened */
382 if (data->state != STATE_EP_UNBOUND) {
383 data->state = STATE_EP_DISABLED;
384 data->desc.bDescriptorType = 0;
385 data->hs_desc.bDescriptorType = 0;
4809ecc2 386 usb_ep_disable(data->ep);
1da177e4 387 }
a79df50b 388 mutex_unlock(&data->lock);
1da177e4
LT
389 put_ep (data);
390 return 0;
391}
392
44c389a0 393static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
1da177e4
LT
394{
395 struct ep_data *data = fd->private_data;
396 int status;
397
d4461a60 398 if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
1da177e4
LT
399 return status;
400
401 spin_lock_irq (&data->dev->lock);
402 if (likely (data->ep != NULL)) {
403 switch (code) {
404 case GADGETFS_FIFO_STATUS:
405 status = usb_ep_fifo_status (data->ep);
406 break;
407 case GADGETFS_FIFO_FLUSH:
408 usb_ep_fifo_flush (data->ep);
409 break;
410 case GADGETFS_CLEAR_HALT:
411 status = usb_ep_clear_halt (data->ep);
412 break;
413 default:
414 status = -ENOTTY;
415 }
416 } else
417 status = -ENODEV;
418 spin_unlock_irq (&data->dev->lock);
a79df50b 419 mutex_unlock(&data->lock);
1da177e4
LT
420 return status;
421}
422
423/*----------------------------------------------------------------------*/
424
425/* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
426
427struct kiocb_priv {
428 struct usb_request *req;
429 struct ep_data *epdata;
a80bf61e
ZB
430 struct kiocb *iocb;
431 struct mm_struct *mm;
432 struct work_struct work;
1da177e4 433 void *buf;
7fe3976e
AV
434 struct iov_iter to;
435 const void *to_free;
1da177e4
LT
436 unsigned actual;
437};
438
bec68faa 439static int ep_aio_cancel(struct kiocb *iocb)
1da177e4
LT
440{
441 struct kiocb_priv *priv = iocb->private;
442 struct ep_data *epdata;
443 int value;
444
445 local_irq_disable();
446 epdata = priv->epdata;
447 // spin_lock(&epdata->dev->lock);
1da177e4
LT
448 if (likely(epdata && epdata->ep && priv->req))
449 value = usb_ep_dequeue (epdata->ep, priv->req);
450 else
451 value = -EINVAL;
452 // spin_unlock(&epdata->dev->lock);
453 local_irq_enable();
454
1da177e4
LT
455 return value;
456}
457
a80bf61e
ZB
458static void ep_user_copy_worker(struct work_struct *work)
459{
460 struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
461 struct mm_struct *mm = priv->mm;
462 struct kiocb *iocb = priv->iocb;
463 size_t ret;
464
465 use_mm(mm);
7fe3976e 466 ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
a80bf61e 467 unuse_mm(mm);
7fe3976e
AV
468 if (!ret)
469 ret = -EFAULT;
a80bf61e
ZB
470
471 /* completing the iocb can drop the ctx and mm, don't touch mm after */
472 aio_complete(iocb, ret, ret);
473
2505107d 474 kfree(priv->buf);
7fe3976e 475 kfree(priv->to_free);
2505107d 476 kfree(priv);
1da177e4
LT
477}
478
479static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
480{
481 struct kiocb *iocb = req->context;
482 struct kiocb_priv *priv = iocb->private;
483 struct ep_data *epdata = priv->epdata;
484
485 /* lock against disconnect (and ideally, cancel) */
486 spin_lock(&epdata->dev->lock);
487 priv->req = NULL;
488 priv->epdata = NULL;
49631ca7
AS
489
490 /* if this was a write or a read returning no data then we
491 * don't need to copy anything to userspace, so we can
492 * complete the aio request immediately.
493 */
7fe3976e 494 if (priv->to_free == NULL || unlikely(req->actual == 0)) {
1da177e4 495 kfree(req->buf);
7fe3976e 496 kfree(priv->to_free);
1da177e4
LT
497 kfree(priv);
498 iocb->private = NULL;
499 /* aio_complete() reports bytes-transferred _and_ faults */
49631ca7 500 aio_complete(iocb, req->actual ? req->actual : req->status,
1da177e4
LT
501 req->status);
502 } else {
a80bf61e 503 /* ep_copy_to_user() won't report both; we hide some faults */
1da177e4
LT
504 if (unlikely(0 != req->status))
505 DBG(epdata->dev, "%s fault %d len %d\n",
506 ep->name, req->status, req->actual);
507
508 priv->buf = req->buf;
509 priv->actual = req->actual;
7fe3976e 510 INIT_WORK(&priv->work, ep_user_copy_worker);
a80bf61e 511 schedule_work(&priv->work);
1da177e4
LT
512 }
513 spin_unlock(&epdata->dev->lock);
514
515 usb_ep_free_request(ep, req);
516 put_ep(epdata);
517}
518
7fe3976e
AV
519static ssize_t ep_aio(struct kiocb *iocb,
520 struct kiocb_priv *priv,
521 struct ep_data *epdata,
522 char *buf,
523 size_t len)
1da177e4 524{
7fe3976e
AV
525 struct usb_request *req;
526 ssize_t value;
1da177e4 527
1da177e4 528 iocb->private = priv;
a80bf61e 529 priv->iocb = iocb;
1da177e4 530
0460fef2 531 kiocb_set_cancel_fn(iocb, ep_aio_cancel);
1da177e4
LT
532 get_ep(epdata);
533 priv->epdata = epdata;
534 priv->actual = 0;
a80bf61e 535 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
1da177e4
LT
536
537 /* each kiocb is coupled to one usb_request, but we can't
538 * allocate or submit those if the host disconnected.
539 */
540 spin_lock_irq(&epdata->dev->lock);
7fe3976e
AV
541 value = -ENODEV;
542 if (unlikely(epdata->ep))
543 goto fail;
1da177e4 544
7fe3976e
AV
545 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
546 value = -ENOMEM;
547 if (unlikely(!req))
548 goto fail;
1da177e4 549
7fe3976e
AV
550 priv->req = req;
551 req->buf = buf;
552 req->length = len;
553 req->complete = ep_aio_complete;
554 req->context = iocb;
555 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
556 if (unlikely(0 != value)) {
557 usb_ep_free_request(epdata->ep, req);
558 goto fail;
559 }
560 spin_unlock_irq(&epdata->dev->lock);
561 return -EIOCBQUEUED;
562
563fail:
564 spin_unlock_irq(&epdata->dev->lock);
565 kfree(priv->to_free);
566 kfree(priv);
567 put_ep(epdata);
1da177e4
LT
568 return value;
569}
570
571static ssize_t
7fe3976e 572ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
1da177e4 573{
7fe3976e
AV
574 struct file *file = iocb->ki_filp;
575 struct ep_data *epdata = file->private_data;
576 size_t len = iov_iter_count(to);
577 ssize_t value;
578 char *buf;
1da177e4 579
d4461a60 580 if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
7fe3976e 581 return value;
027445c3 582
7fe3976e
AV
583 /* halt any endpoint by doing a "wrong direction" i/o call */
584 if (usb_endpoint_dir_in(&epdata->desc)) {
585 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
586 !is_sync_kiocb(iocb)) {
587 mutex_unlock(&epdata->lock);
588 return -EINVAL;
589 }
590 DBG (epdata->dev, "%s halt\n", epdata->name);
591 spin_lock_irq(&epdata->dev->lock);
592 if (likely(epdata->ep != NULL))
593 usb_ep_set_halt(epdata->ep);
594 spin_unlock_irq(&epdata->dev->lock);
595 mutex_unlock(&epdata->lock);
596 return -EBADMSG;
597 }
027445c3 598
7fe3976e
AV
599 buf = kmalloc(len, GFP_KERNEL);
600 if (unlikely(!buf)) {
601 mutex_unlock(&epdata->lock);
602 return -ENOMEM;
603 }
604 if (is_sync_kiocb(iocb)) {
605 value = ep_io(epdata, buf, len);
606 if (value >= 0 && copy_to_iter(buf, value, to))
607 value = -EFAULT;
608 } else {
609 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
610 value = -ENOMEM;
611 if (!priv)
612 goto fail;
613 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
614 if (!priv->to_free) {
615 kfree(priv);
616 goto fail;
617 }
618 value = ep_aio(iocb, priv, epdata, buf, len);
619 if (value == -EIOCBQUEUED)
620 buf = NULL;
621 }
622fail:
623 kfree(buf);
624 mutex_unlock(&epdata->lock);
625 return value;
1da177e4
LT
626}
627
d4461a60
AV
628static ssize_t ep_config(struct ep_data *, const char *, size_t);
629
1da177e4 630static ssize_t
7fe3976e 631ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4 632{
7fe3976e
AV
633 struct file *file = iocb->ki_filp;
634 struct ep_data *epdata = file->private_data;
635 size_t len = iov_iter_count(from);
d4461a60 636 bool configured;
7fe3976e
AV
637 ssize_t value;
638 char *buf;
1da177e4 639
d4461a60 640 if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
7fe3976e
AV
641 return value;
642
d4461a60
AV
643 configured = epdata->state == STATE_EP_ENABLED;
644
7fe3976e 645 /* halt any endpoint by doing a "wrong direction" i/o call */
d4461a60 646 if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
7fe3976e
AV
647 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
648 !is_sync_kiocb(iocb)) {
649 mutex_unlock(&epdata->lock);
650 return -EINVAL;
651 }
652 DBG (epdata->dev, "%s halt\n", epdata->name);
653 spin_lock_irq(&epdata->dev->lock);
654 if (likely(epdata->ep != NULL))
655 usb_ep_set_halt(epdata->ep);
656 spin_unlock_irq(&epdata->dev->lock);
657 mutex_unlock(&epdata->lock);
658 return -EBADMSG;
659 }
027445c3 660
7fe3976e
AV
661 buf = kmalloc(len, GFP_KERNEL);
662 if (unlikely(!buf)) {
663 mutex_unlock(&epdata->lock);
1da177e4 664 return -ENOMEM;
7fe3976e 665 }
027445c3 666
7fe3976e
AV
667 if (unlikely(copy_from_iter(buf, len, from) != len)) {
668 value = -EFAULT;
669 goto out;
670 }
671
d4461a60
AV
672 if (unlikely(!configured)) {
673 value = ep_config(epdata, buf, len);
674 } else if (is_sync_kiocb(iocb)) {
7fe3976e
AV
675 value = ep_io(epdata, buf, len);
676 } else {
677 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
678 value = -ENOMEM;
679 if (priv) {
680 value = ep_aio(iocb, priv, epdata, buf, len);
681 if (value == -EIOCBQUEUED)
682 buf = NULL;
027445c3 683 }
1da177e4 684 }
7fe3976e
AV
685out:
686 kfree(buf);
687 mutex_unlock(&epdata->lock);
688 return value;
1da177e4
LT
689}
690
691/*----------------------------------------------------------------------*/
692
693/* used after endpoint configuration */
066202dd 694static const struct file_operations ep_io_operations = {
1da177e4 695 .owner = THIS_MODULE,
1da177e4 696
d4461a60
AV
697 .open = ep_open,
698 .release = ep_release,
699 .llseek = no_llseek,
7fe3976e
AV
700 .read = new_sync_read,
701 .write = new_sync_write,
44c389a0 702 .unlocked_ioctl = ep_ioctl,
7fe3976e
AV
703 .read_iter = ep_read_iter,
704 .write_iter = ep_write_iter,
1da177e4
LT
705};
706
707/* ENDPOINT INITIALIZATION
708 *
709 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
710 * status = write (fd, descriptors, sizeof descriptors)
711 *
712 * That write establishes the endpoint configuration, configuring
713 * the controller to process bulk, interrupt, or isochronous transfers
714 * at the right maxpacket size, and so on.
715 *
716 * The descriptors are message type 1, identified by a host order u32
717 * at the beginning of what's written. Descriptor order is: full/low
718 * speed descriptor, then optional high speed descriptor.
719 */
720static ssize_t
d4461a60 721ep_config (struct ep_data *data, const char *buf, size_t len)
1da177e4 722{
1da177e4
LT
723 struct usb_ep *ep;
724 u32 tag;
8a7471ab 725 int value, length = len;
1da177e4 726
1da177e4
LT
727 if (data->state != STATE_EP_READY) {
728 value = -EL2HLT;
729 goto fail;
730 }
731
732 value = len;
733 if (len < USB_DT_ENDPOINT_SIZE + 4)
734 goto fail0;
735
736 /* we might need to change message format someday */
d4461a60 737 memcpy(&tag, buf, 4);
1da177e4
LT
738 if (tag != 1) {
739 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
740 goto fail0;
741 }
742 buf += 4;
743 len -= 4;
744
745 /* NOTE: audio endpoint extensions not accepted here;
746 * just don't include the extra bytes.
747 */
748
749 /* full/low speed descriptor, then high speed */
d4461a60 750 memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
1da177e4
LT
751 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
752 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
753 goto fail0;
754 if (len != USB_DT_ENDPOINT_SIZE) {
755 if (len != 2 * USB_DT_ENDPOINT_SIZE)
756 goto fail0;
d4461a60
AV
757 memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
758 USB_DT_ENDPOINT_SIZE);
1da177e4
LT
759 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
760 || data->hs_desc.bDescriptorType
761 != USB_DT_ENDPOINT) {
762 DBG(data->dev, "config %s, bad hs length or type\n",
763 data->name);
764 goto fail0;
765 }
766 }
1da177e4
LT
767
768 spin_lock_irq (&data->dev->lock);
769 if (data->dev->state == STATE_DEV_UNBOUND) {
770 value = -ENOENT;
771 goto gone;
772 } else if ((ep = data->ep) == NULL) {
773 value = -ENODEV;
774 goto gone;
775 }
776 switch (data->dev->gadget->speed) {
777 case USB_SPEED_LOW:
778 case USB_SPEED_FULL:
72c973dd 779 ep->desc = &data->desc;
1da177e4 780 break;
1da177e4
LT
781 case USB_SPEED_HIGH:
782 /* fails if caller didn't provide that descriptor... */
72c973dd 783 ep->desc = &data->hs_desc;
1da177e4 784 break;
1da177e4 785 default:
511779fd 786 DBG(data->dev, "unconnected, %s init abandoned\n",
1da177e4 787 data->name);
511779fd 788 value = -EINVAL;
d4461a60 789 goto gone;
1da177e4 790 }
d4461a60 791 value = usb_ep_enable(ep);
8a7471ab 792 if (value == 0) {
d4461a60 793 data->state = STATE_EP_ENABLED;
8a7471ab
MS
794 value = length;
795 }
1da177e4
LT
796gone:
797 spin_unlock_irq (&data->dev->lock);
798 if (value < 0) {
799fail:
800 data->desc.bDescriptorType = 0;
801 data->hs_desc.bDescriptorType = 0;
802 }
1da177e4
LT
803 return value;
804fail0:
805 value = -EINVAL;
806 goto fail;
1da177e4
LT
807}
808
809static int
810ep_open (struct inode *inode, struct file *fd)
811{
8e18e294 812 struct ep_data *data = inode->i_private;
1da177e4
LT
813 int value = -EBUSY;
814
a79df50b 815 if (mutex_lock_interruptible(&data->lock) != 0)
1da177e4
LT
816 return -EINTR;
817 spin_lock_irq (&data->dev->lock);
818 if (data->dev->state == STATE_DEV_UNBOUND)
819 value = -ENOENT;
820 else if (data->state == STATE_EP_DISABLED) {
821 value = 0;
822 data->state = STATE_EP_READY;
823 get_ep (data);
824 fd->private_data = data;
825 VDEBUG (data->dev, "%s ready\n", data->name);
826 } else
827 DBG (data->dev, "%s state %d\n",
828 data->name, data->state);
829 spin_unlock_irq (&data->dev->lock);
a79df50b 830 mutex_unlock(&data->lock);
1da177e4
LT
831 return value;
832}
833
1da177e4
LT
834/*----------------------------------------------------------------------*/
835
836/* EP0 IMPLEMENTATION can be partly in userspace.
837 *
838 * Drivers that use this facility receive various events, including
839 * control requests the kernel doesn't handle. Drivers that don't
840 * use this facility may be too simple-minded for real applications.
841 */
842
843static inline void ep0_readable (struct dev_data *dev)
844{
845 wake_up (&dev->wait);
846 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
847}
848
849static void clean_req (struct usb_ep *ep, struct usb_request *req)
850{
851 struct dev_data *dev = ep->driver_data;
852
853 if (req->buf != dev->rbuf) {
9d8bab58 854 kfree(req->buf);
1da177e4 855 req->buf = dev->rbuf;
1da177e4
LT
856 }
857 req->complete = epio_complete;
858 dev->setup_out_ready = 0;
859}
860
861static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
862{
863 struct dev_data *dev = ep->driver_data;
5b89db02 864 unsigned long flags;
1da177e4
LT
865 int free = 1;
866
867 /* for control OUT, data must still get to userspace */
5b89db02 868 spin_lock_irqsave(&dev->lock, flags);
1da177e4
LT
869 if (!dev->setup_in) {
870 dev->setup_out_error = (req->status != 0);
871 if (!dev->setup_out_error)
872 free = 0;
873 dev->setup_out_ready = 1;
874 ep0_readable (dev);
7489d149 875 }
1da177e4
LT
876
877 /* clean up as appropriate */
878 if (free && req->buf != &dev->rbuf)
879 clean_req (ep, req);
880 req->complete = epio_complete;
5b89db02 881 spin_unlock_irqrestore(&dev->lock, flags);
1da177e4
LT
882}
883
884static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
885{
886 struct dev_data *dev = ep->driver_data;
887
888 if (dev->setup_out_ready) {
889 DBG (dev, "ep0 request busy!\n");
890 return -EBUSY;
891 }
892 if (len > sizeof (dev->rbuf))
9d8bab58 893 req->buf = kmalloc(len, GFP_ATOMIC);
a9475226 894 if (req->buf == NULL) {
1da177e4
LT
895 req->buf = dev->rbuf;
896 return -ENOMEM;
897 }
898 req->complete = ep0_complete;
899 req->length = len;
97906369 900 req->zero = 0;
1da177e4
LT
901 return 0;
902}
903
904static ssize_t
905ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
906{
907 struct dev_data *dev = fd->private_data;
908 ssize_t retval;
909 enum ep0_state state;
910
911 spin_lock_irq (&dev->lock);
912
913 /* report fd mode change before acting on it */
914 if (dev->setup_abort) {
915 dev->setup_abort = 0;
916 retval = -EIDRM;
917 goto done;
918 }
919
920 /* control DATA stage */
7489d149 921 if ((state = dev->state) == STATE_DEV_SETUP) {
1da177e4
LT
922
923 if (dev->setup_in) { /* stall IN */
924 VDEBUG(dev, "ep0in stall\n");
925 (void) usb_ep_set_halt (dev->gadget->ep0);
926 retval = -EL2HLT;
7489d149 927 dev->state = STATE_DEV_CONNECTED;
1da177e4
LT
928
929 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
930 struct usb_ep *ep = dev->gadget->ep0;
931 struct usb_request *req = dev->req;
932
933 if ((retval = setup_req (ep, req, 0)) == 0)
934 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
7489d149 935 dev->state = STATE_DEV_CONNECTED;
1da177e4
LT
936
937 /* assume that was SET_CONFIGURATION */
938 if (dev->current_config) {
939 unsigned power;
a4e3ef55
DB
940
941 if (gadget_is_dualspeed(dev->gadget)
942 && (dev->gadget->speed
943 == USB_SPEED_HIGH))
1da177e4
LT
944 power = dev->hs_config->bMaxPower;
945 else
1da177e4
LT
946 power = dev->config->bMaxPower;
947 usb_gadget_vbus_draw(dev->gadget, 2 * power);
948 }
949
950 } else { /* collect OUT data */
951 if ((fd->f_flags & O_NONBLOCK) != 0
952 && !dev->setup_out_ready) {
953 retval = -EAGAIN;
954 goto done;
955 }
956 spin_unlock_irq (&dev->lock);
957 retval = wait_event_interruptible (dev->wait,
958 dev->setup_out_ready != 0);
959
960 /* FIXME state could change from under us */
961 spin_lock_irq (&dev->lock);
962 if (retval)
963 goto done;
5b89db02
DB
964
965 if (dev->state != STATE_DEV_SETUP) {
966 retval = -ECANCELED;
967 goto done;
968 }
969 dev->state = STATE_DEV_CONNECTED;
970
1da177e4
LT
971 if (dev->setup_out_error)
972 retval = -EIO;
973 else {
974 len = min (len, (size_t)dev->req->actual);
975// FIXME don't call this with the spinlock held ...
997694de 976 if (copy_to_user (buf, dev->req->buf, len))
1da177e4 977 retval = -EFAULT;
85b4b3c8
TF
978 else
979 retval = len;
1da177e4
LT
980 clean_req (dev->gadget->ep0, dev->req);
981 /* NOTE userspace can't yet choose to stall */
982 }
983 }
984 goto done;
985 }
986
987 /* else normal: return event data */
988 if (len < sizeof dev->event [0]) {
989 retval = -EINVAL;
990 goto done;
991 }
992 len -= len % sizeof (struct usb_gadgetfs_event);
993 dev->usermode_setup = 1;
994
995scan:
996 /* return queued events right away */
997 if (dev->ev_next != 0) {
998 unsigned i, n;
1da177e4 999
1da177e4 1000 n = len / sizeof (struct usb_gadgetfs_event);
0864c7a9
DB
1001 if (dev->ev_next < n)
1002 n = dev->ev_next;
1da177e4 1003
0864c7a9 1004 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1da177e4
LT
1005 for (i = 0; i < n; i++) {
1006 if (dev->event [i].type == GADGETFS_SETUP) {
0864c7a9
DB
1007 dev->state = STATE_DEV_SETUP;
1008 n = i + 1;
1da177e4
LT
1009 break;
1010 }
1011 }
1012 spin_unlock_irq (&dev->lock);
0864c7a9 1013 len = n * sizeof (struct usb_gadgetfs_event);
1da177e4
LT
1014 if (copy_to_user (buf, &dev->event, len))
1015 retval = -EFAULT;
1016 else
1017 retval = len;
1018 if (len > 0) {
1da177e4
LT
1019 /* NOTE this doesn't guard against broken drivers;
1020 * concurrent ep0 readers may lose events.
1021 */
1022 spin_lock_irq (&dev->lock);
0864c7a9
DB
1023 if (dev->ev_next > n) {
1024 memmove(&dev->event[0], &dev->event[n],
1da177e4 1025 sizeof (struct usb_gadgetfs_event)
0864c7a9
DB
1026 * (dev->ev_next - n));
1027 }
1028 dev->ev_next -= n;
1da177e4
LT
1029 spin_unlock_irq (&dev->lock);
1030 }
1031 return retval;
1032 }
1033 if (fd->f_flags & O_NONBLOCK) {
1034 retval = -EAGAIN;
1035 goto done;
1036 }
1037
1038 switch (state) {
1039 default:
441b62c1 1040 DBG (dev, "fail %s, state %d\n", __func__, state);
1da177e4
LT
1041 retval = -ESRCH;
1042 break;
7489d149
DB
1043 case STATE_DEV_UNCONNECTED:
1044 case STATE_DEV_CONNECTED:
1da177e4 1045 spin_unlock_irq (&dev->lock);
441b62c1 1046 DBG (dev, "%s wait\n", __func__);
1da177e4
LT
1047
1048 /* wait for events */
1049 retval = wait_event_interruptible (dev->wait,
1050 dev->ev_next != 0);
1051 if (retval < 0)
1052 return retval;
1053 spin_lock_irq (&dev->lock);
1054 goto scan;
1055 }
1056
1057done:
1058 spin_unlock_irq (&dev->lock);
1059 return retval;
1060}
1061
1062static struct usb_gadgetfs_event *
1063next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1064{
1065 struct usb_gadgetfs_event *event;
1066 unsigned i;
1067
1068 switch (type) {
1069 /* these events purge the queue */
1070 case GADGETFS_DISCONNECT:
7489d149 1071 if (dev->state == STATE_DEV_SETUP)
1da177e4
LT
1072 dev->setup_abort = 1;
1073 // FALL THROUGH
1074 case GADGETFS_CONNECT:
1075 dev->ev_next = 0;
1076 break;
1077 case GADGETFS_SETUP: /* previous request timed out */
1078 case GADGETFS_SUSPEND: /* same effect */
1079 /* these events can't be repeated */
1080 for (i = 0; i != dev->ev_next; i++) {
1081 if (dev->event [i].type != type)
1082 continue;
0864c7a9 1083 DBG(dev, "discard old event[%d] %d\n", i, type);
1da177e4
LT
1084 dev->ev_next--;
1085 if (i == dev->ev_next)
1086 break;
1087 /* indices start at zero, for simplicity */
1088 memmove (&dev->event [i], &dev->event [i + 1],
1089 sizeof (struct usb_gadgetfs_event)
1090 * (dev->ev_next - i));
1091 }
1092 break;
1093 default:
1094 BUG ();
1095 }
0864c7a9 1096 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
1da177e4
LT
1097 event = &dev->event [dev->ev_next++];
1098 BUG_ON (dev->ev_next > N_EVENT);
1da177e4
LT
1099 memset (event, 0, sizeof *event);
1100 event->type = type;
1101 return event;
1102}
1103
1104static ssize_t
1105ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1106{
1107 struct dev_data *dev = fd->private_data;
1108 ssize_t retval = -ESRCH;
1109
1110 spin_lock_irq (&dev->lock);
1111
1112 /* report fd mode change before acting on it */
1113 if (dev->setup_abort) {
1114 dev->setup_abort = 0;
1115 retval = -EIDRM;
1116
1117 /* data and/or status stage for control request */
7489d149 1118 } else if (dev->state == STATE_DEV_SETUP) {
1da177e4
LT
1119
1120 /* IN DATA+STATUS caller makes len <= wLength */
1121 if (dev->setup_in) {
1122 retval = setup_req (dev->gadget->ep0, dev->req, len);
1123 if (retval == 0) {
5b89db02 1124 dev->state = STATE_DEV_CONNECTED;
1da177e4
LT
1125 spin_unlock_irq (&dev->lock);
1126 if (copy_from_user (dev->req->buf, buf, len))
1127 retval = -EFAULT;
97906369
AS
1128 else {
1129 if (len < dev->setup_wLength)
1130 dev->req->zero = 1;
1da177e4
LT
1131 retval = usb_ep_queue (
1132 dev->gadget->ep0, dev->req,
1133 GFP_KERNEL);
97906369 1134 }
1da177e4
LT
1135 if (retval < 0) {
1136 spin_lock_irq (&dev->lock);
1137 clean_req (dev->gadget->ep0, dev->req);
1138 spin_unlock_irq (&dev->lock);
1139 } else
1140 retval = len;
1141
1142 return retval;
1143 }
1144
1145 /* can stall some OUT transfers */
1146 } else if (dev->setup_can_stall) {
1147 VDEBUG(dev, "ep0out stall\n");
1148 (void) usb_ep_set_halt (dev->gadget->ep0);
1149 retval = -EL2HLT;
7489d149 1150 dev->state = STATE_DEV_CONNECTED;
1da177e4
LT
1151 } else {
1152 DBG(dev, "bogus ep0out stall!\n");
1153 }
1154 } else
441b62c1 1155 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
1da177e4
LT
1156
1157 spin_unlock_irq (&dev->lock);
1158 return retval;
1159}
1160
1161static int
1162ep0_fasync (int f, struct file *fd, int on)
1163{
1164 struct dev_data *dev = fd->private_data;
1165 // caller must F_SETOWN before signal delivery happens
441b62c1 1166 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
1da177e4
LT
1167 return fasync_helper (f, fd, on, &dev->fasync);
1168}
1169
1170static struct usb_gadget_driver gadgetfs_driver;
1171
1172static int
1173dev_release (struct inode *inode, struct file *fd)
1174{
1175 struct dev_data *dev = fd->private_data;
1176
1177 /* closing ep0 === shutdown all */
1178
1179 usb_gadget_unregister_driver (&gadgetfs_driver);
1180
1181 /* at this point "good" hardware has disconnected the
1182 * device from USB; the host won't see it any more.
1183 * alternatively, all host requests will time out.
1184 */
1185
1da177e4
LT
1186 kfree (dev->buf);
1187 dev->buf = NULL;
1da177e4 1188
f0cae93f
MN
1189 /* other endpoints were all decoupled from this device */
1190 spin_lock_irq(&dev->lock);
1191 dev->state = STATE_DEV_DISABLED;
1192 spin_unlock_irq(&dev->lock);
1193
1194 put_dev (dev);
1da177e4
LT
1195 return 0;
1196}
1197
e22fc27c
MS
1198static unsigned int
1199ep0_poll (struct file *fd, poll_table *wait)
1200{
1201 struct dev_data *dev = fd->private_data;
1202 int mask = 0;
1203
1204 poll_wait(fd, &dev->wait, wait);
1205
1206 spin_lock_irq (&dev->lock);
1207
1208 /* report fd mode change before acting on it */
1209 if (dev->setup_abort) {
1210 dev->setup_abort = 0;
1211 mask = POLLHUP;
1212 goto out;
1213 }
1214
7489d149 1215 if (dev->state == STATE_DEV_SETUP) {
e22fc27c
MS
1216 if (dev->setup_in || dev->setup_can_stall)
1217 mask = POLLOUT;
1218 } else {
1219 if (dev->ev_next != 0)
1220 mask = POLLIN;
1221 }
1222out:
1223 spin_unlock_irq(&dev->lock);
1224 return mask;
1225}
1226
44c389a0 1227static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1da177e4
LT
1228{
1229 struct dev_data *dev = fd->private_data;
1230 struct usb_gadget *gadget = dev->gadget;
44c389a0 1231 long ret = -ENOTTY;
1da177e4 1232
1548b13b 1233 if (gadget->ops->ioctl)
44c389a0 1234 ret = gadget->ops->ioctl (gadget, code, value);
1548b13b 1235
44c389a0 1236 return ret;
1da177e4
LT
1237}
1238
1239/* used after device configuration */
066202dd 1240static const struct file_operations ep0_io_operations = {
1da177e4
LT
1241 .owner = THIS_MODULE,
1242 .llseek = no_llseek,
1243
1244 .read = ep0_read,
1245 .write = ep0_write,
1246 .fasync = ep0_fasync,
e22fc27c 1247 .poll = ep0_poll,
44c389a0 1248 .unlocked_ioctl = dev_ioctl,
1da177e4
LT
1249 .release = dev_release,
1250};
1251
1252/*----------------------------------------------------------------------*/
1253
1254/* The in-kernel gadget driver handles most ep0 issues, in particular
1255 * enumerating the single configuration (as provided from user space).
1256 *
1257 * Unrecognized ep0 requests may be handled in user space.
1258 */
1259
1da177e4
LT
1260static void make_qualifier (struct dev_data *dev)
1261{
1262 struct usb_qualifier_descriptor qual;
1263 struct usb_device_descriptor *desc;
1264
1265 qual.bLength = sizeof qual;
1266 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
551509d2 1267 qual.bcdUSB = cpu_to_le16 (0x0200);
1da177e4
LT
1268
1269 desc = dev->dev;
1270 qual.bDeviceClass = desc->bDeviceClass;
1271 qual.bDeviceSubClass = desc->bDeviceSubClass;
1272 qual.bDeviceProtocol = desc->bDeviceProtocol;
1273
1274 /* assumes ep0 uses the same value for both speeds ... */
765f5b83 1275 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1da177e4
LT
1276
1277 qual.bNumConfigurations = 1;
1278 qual.bRESERVED = 0;
1279
1280 memcpy (dev->rbuf, &qual, sizeof qual);
1281}
1da177e4
LT
1282
1283static int
1284config_buf (struct dev_data *dev, u8 type, unsigned index)
1285{
1286 int len;
a4e3ef55 1287 int hs = 0;
1da177e4
LT
1288
1289 /* only one configuration */
1290 if (index > 0)
1291 return -EINVAL;
1292
a4e3ef55
DB
1293 if (gadget_is_dualspeed(dev->gadget)) {
1294 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1295 if (type == USB_DT_OTHER_SPEED_CONFIG)
1296 hs = !hs;
1297 }
1da177e4
LT
1298 if (hs) {
1299 dev->req->buf = dev->hs_config;
01ee7d70 1300 len = le16_to_cpu(dev->hs_config->wTotalLength);
a4e3ef55 1301 } else {
1da177e4 1302 dev->req->buf = dev->config;
01ee7d70 1303 len = le16_to_cpu(dev->config->wTotalLength);
1da177e4
LT
1304 }
1305 ((u8 *)dev->req->buf) [1] = type;
1306 return len;
1307}
1308
1309static int
1310gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1311{
1312 struct dev_data *dev = get_gadget_data (gadget);
1313 struct usb_request *req = dev->req;
1314 int value = -EOPNOTSUPP;
1315 struct usb_gadgetfs_event *event;
1bbc1696
DB
1316 u16 w_value = le16_to_cpu(ctrl->wValue);
1317 u16 w_length = le16_to_cpu(ctrl->wLength);
1da177e4
LT
1318
1319 spin_lock (&dev->lock);
1320 dev->setup_abort = 0;
7489d149 1321 if (dev->state == STATE_DEV_UNCONNECTED) {
a4e3ef55
DB
1322 if (gadget_is_dualspeed(gadget)
1323 && gadget->speed == USB_SPEED_HIGH
1324 && dev->hs_config == NULL) {
ce46794f 1325 spin_unlock(&dev->lock);
1da177e4
LT
1326 ERROR (dev, "no high speed config??\n");
1327 return -EINVAL;
1328 }
1da177e4 1329
ce46794f 1330 dev->state = STATE_DEV_CONNECTED;
ce46794f 1331
1da177e4
LT
1332 INFO (dev, "connected\n");
1333 event = next_event (dev, GADGETFS_CONNECT);
1334 event->u.speed = gadget->speed;
1335 ep0_readable (dev);
1336
1da177e4
LT
1337 /* host may have given up waiting for response. we can miss control
1338 * requests handled lower down (device/endpoint status and features);
1339 * then ep0_{read,write} will report the wrong status. controller
1340 * driver will have aborted pending i/o.
1341 */
7489d149 1342 } else if (dev->state == STATE_DEV_SETUP)
1da177e4
LT
1343 dev->setup_abort = 1;
1344
1345 req->buf = dev->rbuf;
1da177e4
LT
1346 req->context = NULL;
1347 value = -EOPNOTSUPP;
1348 switch (ctrl->bRequest) {
1349
1350 case USB_REQ_GET_DESCRIPTOR:
1351 if (ctrl->bRequestType != USB_DIR_IN)
1352 goto unrecognized;
1353 switch (w_value >> 8) {
1354
1355 case USB_DT_DEVICE:
1356 value = min (w_length, (u16) sizeof *dev->dev);
765f5b83 1357 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1da177e4
LT
1358 req->buf = dev->dev;
1359 break;
1da177e4
LT
1360 case USB_DT_DEVICE_QUALIFIER:
1361 if (!dev->hs_config)
1362 break;
1363 value = min (w_length, (u16)
1364 sizeof (struct usb_qualifier_descriptor));
1365 make_qualifier (dev);
1366 break;
1367 case USB_DT_OTHER_SPEED_CONFIG:
1368 // FALLTHROUGH
1da177e4
LT
1369 case USB_DT_CONFIG:
1370 value = config_buf (dev,
1371 w_value >> 8,
1372 w_value & 0xff);
1373 if (value >= 0)
1374 value = min (w_length, (u16) value);
1375 break;
1376 case USB_DT_STRING:
1377 goto unrecognized;
1378
1379 default: // all others are errors
1380 break;
1381 }
1382 break;
1383
1384 /* currently one config, two speeds */
1385 case USB_REQ_SET_CONFIGURATION:
1386 if (ctrl->bRequestType != 0)
12cd5b98 1387 goto unrecognized;
1da177e4
LT
1388 if (0 == (u8) w_value) {
1389 value = 0;
1390 dev->current_config = 0;
1391 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1392 // user mode expected to disable endpoints
1393 } else {
1394 u8 config, power;
a4e3ef55
DB
1395
1396 if (gadget_is_dualspeed(gadget)
1397 && gadget->speed == USB_SPEED_HIGH) {
1da177e4
LT
1398 config = dev->hs_config->bConfigurationValue;
1399 power = dev->hs_config->bMaxPower;
a4e3ef55 1400 } else {
1da177e4
LT
1401 config = dev->config->bConfigurationValue;
1402 power = dev->config->bMaxPower;
1403 }
1404
1405 if (config == (u8) w_value) {
1406 value = 0;
1407 dev->current_config = config;
1408 usb_gadget_vbus_draw(gadget, 2 * power);
1409 }
1410 }
1411
1412 /* report SET_CONFIGURATION like any other control request,
1413 * except that usermode may not stall this. the next
1414 * request mustn't be allowed start until this finishes:
1415 * endpoints and threads set up, etc.
1416 *
1417 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1418 * has bad/racey automagic that prevents synchronizing here.
1419 * even kernel mode drivers often miss them.
1420 */
1421 if (value == 0) {
1422 INFO (dev, "configuration #%d\n", dev->current_config);
6027f317 1423 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
1da177e4
LT
1424 if (dev->usermode_setup) {
1425 dev->setup_can_stall = 0;
1426 goto delegate;
1427 }
1428 }
1429 break;
1430
d30f2065 1431#ifndef CONFIG_USB_PXA25X
1da177e4
LT
1432 /* PXA automagically handles this request too */
1433 case USB_REQ_GET_CONFIGURATION:
1434 if (ctrl->bRequestType != 0x80)
12cd5b98 1435 goto unrecognized;
1da177e4
LT
1436 *(u8 *)req->buf = dev->current_config;
1437 value = min (w_length, (u16) 1);
1438 break;
1439#endif
1440
1441 default:
1442unrecognized:
1443 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1444 dev->usermode_setup ? "delegate" : "fail",
1445 ctrl->bRequestType, ctrl->bRequest,
1446 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1447
1448 /* if there's an ep0 reader, don't stall */
1449 if (dev->usermode_setup) {
1450 dev->setup_can_stall = 1;
1451delegate:
1452 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1453 ? 1 : 0;
97906369 1454 dev->setup_wLength = w_length;
1da177e4
LT
1455 dev->setup_out_ready = 0;
1456 dev->setup_out_error = 0;
1457 value = 0;
1458
1459 /* read DATA stage for OUT right away */
1460 if (unlikely (!dev->setup_in && w_length)) {
1461 value = setup_req (gadget->ep0, dev->req,
1462 w_length);
1463 if (value < 0)
1464 break;
1465 value = usb_ep_queue (gadget->ep0, dev->req,
1466 GFP_ATOMIC);
1467 if (value < 0) {
1468 clean_req (gadget->ep0, dev->req);
1469 break;
1470 }
1471
1472 /* we can't currently stall these */
1473 dev->setup_can_stall = 0;
1474 }
1475
1476 /* state changes when reader collects event */
1477 event = next_event (dev, GADGETFS_SETUP);
1478 event->u.setup = *ctrl;
1479 ep0_readable (dev);
1480 spin_unlock (&dev->lock);
1481 return 0;
1482 }
1483 }
1484
1485 /* proceed with data transfer and status phases? */
7489d149 1486 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
1da177e4
LT
1487 req->length = value;
1488 req->zero = value < w_length;
1489 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1490 if (value < 0) {
1491 DBG (dev, "ep_queue --> %d\n", value);
1492 req->status = 0;
1493 }
1494 }
1495
1496 /* device stalls when value < 0 */
1497 spin_unlock (&dev->lock);
1498 return value;
1499}
1500
1501static void destroy_ep_files (struct dev_data *dev)
1502{
441b62c1 1503 DBG (dev, "%s %d\n", __func__, dev->state);
1da177e4
LT
1504
1505 /* dev->state must prevent interference */
1da177e4 1506 spin_lock_irq (&dev->lock);
104bb37d 1507 while (!list_empty(&dev->epfiles)) {
1da177e4
LT
1508 struct ep_data *ep;
1509 struct inode *parent;
1510 struct dentry *dentry;
1511
1512 /* break link to FS */
104bb37d 1513 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
1da177e4
LT
1514 list_del_init (&ep->epfiles);
1515 dentry = ep->dentry;
1516 ep->dentry = NULL;
1517 parent = dentry->d_parent->d_inode;
1518
1519 /* break link to controller */
1520 if (ep->state == STATE_EP_ENABLED)
1521 (void) usb_ep_disable (ep->ep);
1522 ep->state = STATE_EP_UNBOUND;
1523 usb_ep_free_request (ep->ep, ep->req);
1524 ep->ep = NULL;
1525 wake_up (&ep->wait);
1526 put_ep (ep);
1527
1528 spin_unlock_irq (&dev->lock);
1529
1530 /* break link to dcache */
1b1dcc1b 1531 mutex_lock (&parent->i_mutex);
1da177e4
LT
1532 d_delete (dentry);
1533 dput (dentry);
1b1dcc1b 1534 mutex_unlock (&parent->i_mutex);
1da177e4 1535
104bb37d 1536 spin_lock_irq (&dev->lock);
1da177e4
LT
1537 }
1538 spin_unlock_irq (&dev->lock);
1539}
1540
1541
fb6c3225 1542static struct dentry *
1da177e4 1543gadgetfs_create_file (struct super_block *sb, char const *name,
fb6c3225 1544 void *data, const struct file_operations *fops);
1da177e4
LT
1545
1546static int activate_ep_files (struct dev_data *dev)
1547{
1548 struct usb_ep *ep;
0ae4ea80 1549 struct ep_data *data;
1da177e4
LT
1550
1551 gadget_for_each_ep (ep, dev->gadget) {
1da177e4 1552
7039f422 1553 data = kzalloc(sizeof(*data), GFP_KERNEL);
1da177e4 1554 if (!data)
0ae4ea80 1555 goto enomem0;
1da177e4 1556 data->state = STATE_EP_DISABLED;
a79df50b 1557 mutex_init(&data->lock);
1da177e4
LT
1558 init_waitqueue_head (&data->wait);
1559
1560 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1561 atomic_set (&data->count, 1);
1562 data->dev = dev;
1563 get_dev (dev);
1564
1565 data->ep = ep;
1566 ep->driver_data = data;
1567
1568 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1569 if (!data->req)
0ae4ea80 1570 goto enomem1;
1da177e4 1571
fb6c3225 1572 data->dentry = gadgetfs_create_file (dev->sb, data->name,
d4461a60 1573 data, &ep_io_operations);
fb6c3225 1574 if (!data->dentry)
0ae4ea80 1575 goto enomem2;
1da177e4
LT
1576 list_add_tail (&data->epfiles, &dev->epfiles);
1577 }
1578 return 0;
1579
0ae4ea80
AS
1580enomem2:
1581 usb_ep_free_request (ep, data->req);
1582enomem1:
1583 put_dev (dev);
1584 kfree (data);
1585enomem0:
441b62c1 1586 DBG (dev, "%s enomem\n", __func__);
1da177e4
LT
1587 destroy_ep_files (dev);
1588 return -ENOMEM;
1589}
1590
1591static void
1592gadgetfs_unbind (struct usb_gadget *gadget)
1593{
1594 struct dev_data *dev = get_gadget_data (gadget);
1595
441b62c1 1596 DBG (dev, "%s\n", __func__);
1da177e4
LT
1597
1598 spin_lock_irq (&dev->lock);
1599 dev->state = STATE_DEV_UNBOUND;
1600 spin_unlock_irq (&dev->lock);
1601
1602 destroy_ep_files (dev);
1603 gadget->ep0->driver_data = NULL;
1604 set_gadget_data (gadget, NULL);
1605
1606 /* we've already been disconnected ... no i/o is active */
1607 if (dev->req)
1608 usb_ep_free_request (gadget->ep0, dev->req);
441b62c1 1609 DBG (dev, "%s done\n", __func__);
1da177e4
LT
1610 put_dev (dev);
1611}
1612
1613static struct dev_data *the_device;
1614
ffe0b335
SAS
1615static int gadgetfs_bind(struct usb_gadget *gadget,
1616 struct usb_gadget_driver *driver)
1da177e4
LT
1617{
1618 struct dev_data *dev = the_device;
1619
1620 if (!dev)
1621 return -ESRCH;
1622 if (0 != strcmp (CHIP, gadget->name)) {
00274921 1623 pr_err("%s expected %s controller not %s\n",
1da177e4
LT
1624 shortname, CHIP, gadget->name);
1625 return -ENODEV;
1626 }
1627
1628 set_gadget_data (gadget, dev);
1629 dev->gadget = gadget;
1630 gadget->ep0->driver_data = dev;
1da177e4
LT
1631
1632 /* preallocate control response and buffer */
1633 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1634 if (!dev->req)
1635 goto enomem;
1636 dev->req->context = NULL;
1637 dev->req->complete = epio_complete;
1638
1639 if (activate_ep_files (dev) < 0)
1640 goto enomem;
1641
1642 INFO (dev, "bound to %s driver\n", gadget->name);
7489d149
DB
1643 spin_lock_irq(&dev->lock);
1644 dev->state = STATE_DEV_UNCONNECTED;
1645 spin_unlock_irq(&dev->lock);
1da177e4
LT
1646 get_dev (dev);
1647 return 0;
1648
1649enomem:
1650 gadgetfs_unbind (gadget);
1651 return -ENOMEM;
1652}
1653
1654static void
1655gadgetfs_disconnect (struct usb_gadget *gadget)
1656{
1657 struct dev_data *dev = get_gadget_data (gadget);
001428e4 1658 unsigned long flags;
1da177e4 1659
001428e4 1660 spin_lock_irqsave (&dev->lock, flags);
7489d149 1661 if (dev->state == STATE_DEV_UNCONNECTED)
07cb7f23 1662 goto exit;
7489d149 1663 dev->state = STATE_DEV_UNCONNECTED;
1da177e4
LT
1664
1665 INFO (dev, "disconnected\n");
1da177e4
LT
1666 next_event (dev, GADGETFS_DISCONNECT);
1667 ep0_readable (dev);
07cb7f23 1668exit:
001428e4 1669 spin_unlock_irqrestore (&dev->lock, flags);
1da177e4
LT
1670}
1671
1672static void
1673gadgetfs_suspend (struct usb_gadget *gadget)
1674{
1675 struct dev_data *dev = get_gadget_data (gadget);
1676
1677 INFO (dev, "suspended from state %d\n", dev->state);
1678 spin_lock (&dev->lock);
1679 switch (dev->state) {
7489d149
DB
1680 case STATE_DEV_SETUP: // VERY odd... host died??
1681 case STATE_DEV_CONNECTED:
1682 case STATE_DEV_UNCONNECTED:
1da177e4
LT
1683 next_event (dev, GADGETFS_SUSPEND);
1684 ep0_readable (dev);
1685 /* FALLTHROUGH */
1686 default:
1687 break;
1688 }
1689 spin_unlock (&dev->lock);
1690}
1691
1692static struct usb_gadget_driver gadgetfs_driver = {
1da177e4 1693 .function = (char *) driver_desc,
93952956 1694 .bind = gadgetfs_bind,
1da177e4
LT
1695 .unbind = gadgetfs_unbind,
1696 .setup = gadgetfs_setup,
0eba4550 1697 .reset = gadgetfs_disconnect,
1da177e4
LT
1698 .disconnect = gadgetfs_disconnect,
1699 .suspend = gadgetfs_suspend,
1700
2505107d 1701 .driver = {
1da177e4 1702 .name = (char *) shortname,
1da177e4
LT
1703 },
1704};
1705
1706/*----------------------------------------------------------------------*/
1707
1708static void gadgetfs_nop(struct usb_gadget *arg) { }
1709
ffe0b335
SAS
1710static int gadgetfs_probe(struct usb_gadget *gadget,
1711 struct usb_gadget_driver *driver)
1da177e4
LT
1712{
1713 CHIP = gadget->name;
1714 return -EISNAM;
1715}
1716
1717static struct usb_gadget_driver probe_driver = {
7177aed4 1718 .max_speed = USB_SPEED_HIGH,
93952956 1719 .bind = gadgetfs_probe,
1da177e4
LT
1720 .unbind = gadgetfs_nop,
1721 .setup = (void *)gadgetfs_nop,
1722 .disconnect = gadgetfs_nop,
2505107d 1723 .driver = {
1da177e4
LT
1724 .name = "nop",
1725 },
1726};
1727
1728
1729/* DEVICE INITIALIZATION
1730 *
1731 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1732 * status = write (fd, descriptors, sizeof descriptors)
1733 *
1734 * That write establishes the device configuration, so the kernel can
1735 * bind to the controller ... guaranteeing it can handle enumeration
1736 * at all necessary speeds. Descriptor order is:
1737 *
1738 * . message tag (u32, host order) ... for now, must be zero; it
1739 * would change to support features like multi-config devices
1740 * . full/low speed config ... all wTotalLength bytes (with interface,
1741 * class, altsetting, endpoint, and other descriptors)
1742 * . high speed config ... all descriptors, for high speed operation;
2505107d 1743 * this one's optional except for high-speed hardware
1da177e4
LT
1744 * . device descriptor
1745 *
511779fd
PE
1746 * Endpoints are not yet enabled. Drivers must wait until device
1747 * configuration and interface altsetting changes create
1da177e4
LT
1748 * the need to configure (or unconfigure) them.
1749 *
1750 * After initialization, the device stays active for as long as that
511779fd
PE
1751 * $CHIP file is open. Events must then be read from that descriptor,
1752 * such as configuration notifications.
1da177e4
LT
1753 */
1754
1755static int is_valid_config (struct usb_config_descriptor *config)
1756{
1757 return config->bDescriptorType == USB_DT_CONFIG
1758 && config->bLength == USB_DT_CONFIG_SIZE
1759 && config->bConfigurationValue != 0
1760 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1761 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1762 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1763 /* FIXME check lengths: walk to end */
1764}
1765
1766static ssize_t
1767dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1768{
1769 struct dev_data *dev = fd->private_data;
1770 ssize_t value = len, length = len;
1771 unsigned total;
1772 u32 tag;
1773 char *kbuf;
1774
1da177e4
LT
1775 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1776 return -EINVAL;
1777
1778 /* we might need to change message format someday */
1779 if (copy_from_user (&tag, buf, 4))
1780 return -EFAULT;
1781 if (tag != 0)
1782 return -EINVAL;
1783 buf += 4;
1784 length -= 4;
1785
be8a058b
JL
1786 kbuf = memdup_user(buf, length);
1787 if (IS_ERR(kbuf))
1788 return PTR_ERR(kbuf);
1da177e4
LT
1789
1790 spin_lock_irq (&dev->lock);
1791 value = -EINVAL;
1792 if (dev->buf)
1793 goto fail;
1794 dev->buf = kbuf;
1795
1796 /* full or low speed config */
1797 dev->config = (void *) kbuf;
01ee7d70 1798 total = le16_to_cpu(dev->config->wTotalLength);
1da177e4
LT
1799 if (!is_valid_config (dev->config) || total >= length)
1800 goto fail;
1801 kbuf += total;
1802 length -= total;
1803
1804 /* optional high speed config */
1805 if (kbuf [1] == USB_DT_CONFIG) {
1806 dev->hs_config = (void *) kbuf;
01ee7d70 1807 total = le16_to_cpu(dev->hs_config->wTotalLength);
1da177e4
LT
1808 if (!is_valid_config (dev->hs_config) || total >= length)
1809 goto fail;
1810 kbuf += total;
1811 length -= total;
1812 }
1813
1814 /* could support multiple configs, using another encoding! */
1815
1816 /* device descriptor (tweaked for paranoia) */
1817 if (length != USB_DT_DEVICE_SIZE)
1818 goto fail;
1819 dev->dev = (void *)kbuf;
1820 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1821 || dev->dev->bDescriptorType != USB_DT_DEVICE
1822 || dev->dev->bNumConfigurations != 1)
1823 goto fail;
1824 dev->dev->bNumConfigurations = 1;
551509d2 1825 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
1da177e4
LT
1826
1827 /* triggers gadgetfs_bind(); then we can enumerate. */
1828 spin_unlock_irq (&dev->lock);
85b8614d
MN
1829 if (dev->hs_config)
1830 gadgetfs_driver.max_speed = USB_SPEED_HIGH;
1831 else
1832 gadgetfs_driver.max_speed = USB_SPEED_FULL;
93952956
SAS
1833
1834 value = usb_gadget_probe_driver(&gadgetfs_driver);
1da177e4
LT
1835 if (value != 0) {
1836 kfree (dev->buf);
1837 dev->buf = NULL;
1838 } else {
1839 /* at this point "good" hardware has for the first time
1840 * let the USB the host see us. alternatively, if users
1841 * unplug/replug that will clear all the error state.
1842 *
1843 * note: everything running before here was guaranteed
1844 * to choke driver model style diagnostics. from here
1845 * on, they can work ... except in cleanup paths that
1846 * kick in after the ep0 descriptor is closed.
1847 */
1848 fd->f_op = &ep0_io_operations;
1849 value = len;
1850 }
1851 return value;
1852
1853fail:
1854 spin_unlock_irq (&dev->lock);
441b62c1 1855 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
1da177e4
LT
1856 kfree (dev->buf);
1857 dev->buf = NULL;
1858 return value;
1859}
1860
1861static int
1862dev_open (struct inode *inode, struct file *fd)
1863{
8e18e294 1864 struct dev_data *dev = inode->i_private;
1da177e4
LT
1865 int value = -EBUSY;
1866
7489d149 1867 spin_lock_irq(&dev->lock);
1da177e4
LT
1868 if (dev->state == STATE_DEV_DISABLED) {
1869 dev->ev_next = 0;
7489d149 1870 dev->state = STATE_DEV_OPENED;
1da177e4
LT
1871 fd->private_data = dev;
1872 get_dev (dev);
1873 value = 0;
1874 }
7489d149 1875 spin_unlock_irq(&dev->lock);
1da177e4
LT
1876 return value;
1877}
1878
066202dd 1879static const struct file_operations dev_init_operations = {
1da177e4
LT
1880 .llseek = no_llseek,
1881
1882 .open = dev_open,
1883 .write = dev_config,
1884 .fasync = ep0_fasync,
44c389a0 1885 .unlocked_ioctl = dev_ioctl,
1da177e4
LT
1886 .release = dev_release,
1887};
1888
1889/*----------------------------------------------------------------------*/
1890
1891/* FILESYSTEM AND SUPERBLOCK OPERATIONS
1892 *
1893 * Mounting the filesystem creates a controller file, used first for
1894 * device configuration then later for event monitoring.
1895 */
1896
1897
1898/* FIXME PAM etc could set this security policy without mount options
1899 * if epfiles inherited ownership and permissons from ep0 ...
1900 */
1901
1902static unsigned default_uid;
1903static unsigned default_gid;
1904static unsigned default_perm = S_IRUSR | S_IWUSR;
1905
1906module_param (default_uid, uint, 0644);
1907module_param (default_gid, uint, 0644);
1908module_param (default_perm, uint, 0644);
1909
1910
1911static struct inode *
1912gadgetfs_make_inode (struct super_block *sb,
99ac48f5 1913 void *data, const struct file_operations *fops,
1da177e4
LT
1914 int mode)
1915{
1916 struct inode *inode = new_inode (sb);
1917
1918 if (inode) {
85fe4025 1919 inode->i_ino = get_next_ino();
1da177e4 1920 inode->i_mode = mode;
32d639c6
EB
1921 inode->i_uid = make_kuid(&init_user_ns, default_uid);
1922 inode->i_gid = make_kgid(&init_user_ns, default_gid);
1da177e4
LT
1923 inode->i_atime = inode->i_mtime = inode->i_ctime
1924 = CURRENT_TIME;
8e18e294 1925 inode->i_private = data;
1da177e4
LT
1926 inode->i_fop = fops;
1927 }
1928 return inode;
1929}
1930
1931/* creates in fs root directory, so non-renamable and non-linkable.
1932 * so inode and dentry are paired, until device reconfig.
1933 */
fb6c3225 1934static struct dentry *
1da177e4 1935gadgetfs_create_file (struct super_block *sb, char const *name,
fb6c3225 1936 void *data, const struct file_operations *fops)
1da177e4
LT
1937{
1938 struct dentry *dentry;
1939 struct inode *inode;
1940
1941 dentry = d_alloc_name(sb->s_root, name);
1942 if (!dentry)
1943 return NULL;
1944
1945 inode = gadgetfs_make_inode (sb, data, fops,
1946 S_IFREG | (default_perm & S_IRWXUGO));
1947 if (!inode) {
1948 dput(dentry);
1949 return NULL;
1950 }
1951 d_add (dentry, inode);
fb6c3225 1952 return dentry;
1da177e4
LT
1953}
1954
b87221de 1955static const struct super_operations gadget_fs_operations = {
1da177e4
LT
1956 .statfs = simple_statfs,
1957 .drop_inode = generic_delete_inode,
1958};
1959
1960static int
1961gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
1962{
1963 struct inode *inode;
1da177e4
LT
1964 struct dev_data *dev;
1965
1966 if (the_device)
1967 return -ESRCH;
1968
1969 /* fake probe to determine $CHIP */
5cdf7d5b 1970 CHIP = NULL;
93952956 1971 usb_gadget_probe_driver(&probe_driver);
1da177e4
LT
1972 if (!CHIP)
1973 return -ENODEV;
1974
1975 /* superblock */
1976 sb->s_blocksize = PAGE_CACHE_SIZE;
1977 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1978 sb->s_magic = GADGETFS_MAGIC;
1979 sb->s_op = &gadget_fs_operations;
1980 sb->s_time_gran = 1;
1981
1982 /* root inode */
1983 inode = gadgetfs_make_inode (sb,
1984 NULL, &simple_dir_operations,
1985 S_IFDIR | S_IRUGO | S_IXUGO);
1986 if (!inode)
87da5b32 1987 goto Enomem;
1da177e4 1988 inode->i_op = &simple_dir_inode_operations;
48fde701 1989 if (!(sb->s_root = d_make_root (inode)))
87da5b32 1990 goto Enomem;
1da177e4
LT
1991
1992 /* the ep0 file is named after the controller we expect;
1993 * user mode code can use it for sanity checks, like we do.
1994 */
1995 dev = dev_new ();
1996 if (!dev)
87da5b32 1997 goto Enomem;
1da177e4
LT
1998
1999 dev->sb = sb;
fb6c3225
AV
2000 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &dev_init_operations);
2001 if (!dev->dentry) {
87da5b32
AV
2002 put_dev(dev);
2003 goto Enomem;
2004 }
1da177e4
LT
2005
2006 /* other endpoint files are available after hardware setup,
2007 * from binding to a controller.
2008 */
2009 the_device = dev;
2010 return 0;
0ae4ea80 2011
87da5b32 2012Enomem:
0ae4ea80 2013 return -ENOMEM;
1da177e4
LT
2014}
2015
2016/* "mount -t gadgetfs path /dev/gadget" ends up here */
fc14f2fe
AV
2017static struct dentry *
2018gadgetfs_mount (struct file_system_type *t, int flags,
2019 const char *path, void *opts)
1da177e4 2020{
fc14f2fe 2021 return mount_single (t, flags, opts, gadgetfs_fill_super);
1da177e4
LT
2022}
2023
2024static void
2025gadgetfs_kill_sb (struct super_block *sb)
2026{
2027 kill_litter_super (sb);
2028 if (the_device) {
2029 put_dev (the_device);
2030 the_device = NULL;
2031 }
2032}
2033
2034/*----------------------------------------------------------------------*/
2035
2036static struct file_system_type gadgetfs_type = {
2037 .owner = THIS_MODULE,
2038 .name = shortname,
fc14f2fe 2039 .mount = gadgetfs_mount,
1da177e4
LT
2040 .kill_sb = gadgetfs_kill_sb,
2041};
7f78e035 2042MODULE_ALIAS_FS("gadgetfs");
1da177e4
LT
2043
2044/*----------------------------------------------------------------------*/
2045
2046static int __init init (void)
2047{
2048 int status;
2049
2050 status = register_filesystem (&gadgetfs_type);
2051 if (status == 0)
2052 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2053 shortname, driver_desc);
2054 return status;
2055}
2056module_init (init);
2057
2058static void __exit cleanup (void)
2059{
2060 pr_debug ("unregister %s\n", shortname);
2061 unregister_filesystem (&gadgetfs_type);
2062}
2063module_exit (cleanup);
2064
This page took 0.971274 seconds and 5 git commands to generate.