2 * inode.c -- user mode filesystem api for usb gadget controllers
4 * Copyright (C) 2003-2004 David Brownell
5 * Copyright (C) 2003 Agilent Technologies
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.
14 /* #define VERBOSE_DEBUG */
16 #include <linux/init.h>
17 #include <linux/module.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>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/poll.h>
27 #include <linux/mmu_context.h>
28 #include <linux/aio.h>
29 #include <linux/uio.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/moduleparam.h>
34 #include <linux/usb/gadgetfs.h>
35 #include <linux/usb/gadget.h>
39 * The gadgetfs API maps each endpoint to a file descriptor so that you
40 * can use standard synchronous read/write calls for I/O. There's some
41 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
42 * drivers show how this works in practice. You can also use AIO to
43 * eliminate I/O gaps between requests, to help when streaming data.
45 * Key parts that must be USB-specific are protocols defining how the
46 * read/write operations relate to the hardware state machines. There
47 * are two types of files. One type is for the device, implementing ep0.
48 * The other type is for each IN or OUT endpoint. In both cases, the
49 * user mode driver must configure the hardware before using it.
51 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
52 * (by writing configuration and device descriptors). Afterwards it
53 * may serve as a source of device events, used to handle all control
54 * requests other than basic enumeration.
56 * - Then, after a SET_CONFIGURATION control request, ep_config() is
57 * called when each /dev/gadget/ep* file is configured (by writing
58 * endpoint descriptors). Afterwards these files are used to write()
59 * IN data or to read() OUT data. To halt the endpoint, a "wrong
60 * direction" request is issued (like reading an IN endpoint).
62 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
63 * not possible on all hardware. For example, precise fault handling with
64 * respect to data left in endpoint fifos after aborted operations; or
65 * selective clearing of endpoint halts, to implement SET_INTERFACE.
68 #define DRIVER_DESC "USB Gadget filesystem"
69 #define DRIVER_VERSION "24 Aug 2004"
71 static const char driver_desc [] = DRIVER_DESC;
72 static const char shortname [] = "gadgetfs";
74 MODULE_DESCRIPTION (DRIVER_DESC);
75 MODULE_AUTHOR ("David Brownell");
76 MODULE_LICENSE ("GPL");
78 static int ep_open(struct inode *, struct file *);
81 /*----------------------------------------------------------------------*/
83 #define GADGETFS_MAGIC 0xaee71ee7
85 /* /dev/gadget/$CHIP represents ep0 and the whole device */
87 /* DISBLED is the initial state.
89 STATE_DEV_DISABLED = 0,
91 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
92 * ep0/device i/o modes and binding to the controller. Driver
93 * must always write descriptors to initialize the device, then
94 * the device becomes UNCONNECTED until enumeration.
98 /* From then on, ep0 fd is in either of two basic modes:
99 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
100 * - SETUP: read/write will transfer control data and succeed;
101 * or if "wrong direction", performs protocol stall
103 STATE_DEV_UNCONNECTED,
107 /* UNBOUND means the driver closed ep0, so the device won't be
108 * accessible again (DEV_DISABLED) until all fds are closed.
113 /* enough for the whole queue: most events invalidate others */
116 #define RBUF_SIZE 256
122 enum ep0_state state; /* P: lock */
123 struct usb_gadgetfs_event event [N_EVENT];
125 struct fasync_struct *fasync;
128 /* drivers reading ep0 MUST handle control requests (SETUP)
129 * reported that way; else the host will time out.
131 unsigned usermode_setup : 1,
137 gadget_registered : 1;
138 unsigned setup_wLength;
140 /* the rest is basically write-once */
141 struct usb_config_descriptor *config, *hs_config;
142 struct usb_device_descriptor *dev;
143 struct usb_request *req;
144 struct usb_gadget *gadget;
145 struct list_head epfiles;
147 wait_queue_head_t wait;
148 struct super_block *sb;
149 struct dentry *dentry;
151 /* except this scratch i/o buffer for ep0 */
155 static inline void get_dev (struct dev_data *data)
157 atomic_inc (&data->count);
160 static void put_dev (struct dev_data *data)
162 if (likely (!atomic_dec_and_test (&data->count)))
164 /* needs no more cleanup */
165 BUG_ON (waitqueue_active (&data->wait));
169 static struct dev_data *dev_new (void)
171 struct dev_data *dev;
173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
176 dev->state = STATE_DEV_DISABLED;
177 atomic_set (&dev->count, 1);
178 spin_lock_init (&dev->lock);
179 INIT_LIST_HEAD (&dev->epfiles);
180 init_waitqueue_head (&dev->wait);
184 /*----------------------------------------------------------------------*/
186 /* other /dev/gadget/$ENDPOINT files represent endpoints */
188 STATE_EP_DISABLED = 0,
198 struct dev_data *dev;
199 /* must hold dev->lock before accessing ep or req */
201 struct usb_request *req;
204 struct usb_endpoint_descriptor desc, hs_desc;
205 struct list_head epfiles;
206 wait_queue_head_t wait;
207 struct dentry *dentry;
210 static inline void get_ep (struct ep_data *data)
212 atomic_inc (&data->count);
215 static void put_ep (struct ep_data *data)
217 if (likely (!atomic_dec_and_test (&data->count)))
220 /* needs no more cleanup */
221 BUG_ON (!list_empty (&data->epfiles));
222 BUG_ON (waitqueue_active (&data->wait));
226 /*----------------------------------------------------------------------*/
228 /* most "how to use the hardware" policy choices are in userspace:
229 * mapping endpoint roles (which the driver needs) to the capabilities
230 * which the usb controller has. most of those capabilities are exposed
231 * implicitly, starting with the driver name and then endpoint names.
234 static const char *CHIP;
236 /*----------------------------------------------------------------------*/
238 /* NOTE: don't use dev_printk calls before binding to the gadget
239 * at the end of ep0 configuration, or after unbind.
242 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
243 #define xprintk(d,level,fmt,args...) \
244 printk(level "%s: " fmt , shortname , ## args)
247 #define DBG(dev,fmt,args...) \
248 xprintk(dev , KERN_DEBUG , fmt , ## args)
250 #define DBG(dev,fmt,args...) \
257 #define VDEBUG(dev,fmt,args...) \
261 #define ERROR(dev,fmt,args...) \
262 xprintk(dev , KERN_ERR , fmt , ## args)
263 #define INFO(dev,fmt,args...) \
264 xprintk(dev , KERN_INFO , fmt , ## args)
267 /*----------------------------------------------------------------------*/
269 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
271 * After opening, configure non-control endpoints. Then use normal
272 * stream read() and write() requests; and maybe ioctl() to get more
273 * precise FIFO status when recovering from cancellation.
276 static void epio_complete (struct usb_ep *ep, struct usb_request *req)
278 struct ep_data *epdata = ep->driver_data;
283 epdata->status = req->status;
285 epdata->status = req->actual;
286 complete ((struct completion *)req->context);
289 /* tasklock endpoint, returning when it's connected.
290 * still need dev->lock to use epdata->ep.
293 get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
297 if (f_flags & O_NONBLOCK) {
298 if (!mutex_trylock(&epdata->lock))
300 if (epdata->state != STATE_EP_ENABLED &&
301 (!is_write || epdata->state != STATE_EP_READY)) {
302 mutex_unlock(&epdata->lock);
310 val = mutex_lock_interruptible(&epdata->lock);
314 switch (epdata->state) {
315 case STATE_EP_ENABLED:
317 case STATE_EP_READY: /* not configured yet */
321 case STATE_EP_UNBOUND: /* clean disconnect */
323 // case STATE_EP_DISABLED: /* "can't happen" */
324 default: /* error! */
325 pr_debug ("%s: ep %p not available, state %d\n",
326 shortname, epdata, epdata->state);
328 mutex_unlock(&epdata->lock);
333 ep_io (struct ep_data *epdata, void *buf, unsigned len)
335 DECLARE_COMPLETION_ONSTACK (done);
338 spin_lock_irq (&epdata->dev->lock);
339 if (likely (epdata->ep != NULL)) {
340 struct usb_request *req = epdata->req;
342 req->context = &done;
343 req->complete = epio_complete;
346 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
349 spin_unlock_irq (&epdata->dev->lock);
351 if (likely (value == 0)) {
352 value = wait_event_interruptible (done.wait, done.done);
354 spin_lock_irq (&epdata->dev->lock);
355 if (likely (epdata->ep != NULL)) {
356 DBG (epdata->dev, "%s i/o interrupted\n",
358 usb_ep_dequeue (epdata->ep, epdata->req);
359 spin_unlock_irq (&epdata->dev->lock);
361 wait_event (done.wait, done.done);
362 if (epdata->status == -ECONNRESET)
363 epdata->status = -EINTR;
365 spin_unlock_irq (&epdata->dev->lock);
367 DBG (epdata->dev, "endpoint gone\n");
368 epdata->status = -ENODEV;
371 return epdata->status;
377 ep_release (struct inode *inode, struct file *fd)
379 struct ep_data *data = fd->private_data;
382 value = mutex_lock_interruptible(&data->lock);
386 /* clean up if this can be reopened */
387 if (data->state != STATE_EP_UNBOUND) {
388 data->state = STATE_EP_DISABLED;
389 data->desc.bDescriptorType = 0;
390 data->hs_desc.bDescriptorType = 0;
391 usb_ep_disable(data->ep);
393 mutex_unlock(&data->lock);
398 static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
400 struct ep_data *data = fd->private_data;
403 if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
406 spin_lock_irq (&data->dev->lock);
407 if (likely (data->ep != NULL)) {
409 case GADGETFS_FIFO_STATUS:
410 status = usb_ep_fifo_status (data->ep);
412 case GADGETFS_FIFO_FLUSH:
413 usb_ep_fifo_flush (data->ep);
415 case GADGETFS_CLEAR_HALT:
416 status = usb_ep_clear_halt (data->ep);
423 spin_unlock_irq (&data->dev->lock);
424 mutex_unlock(&data->lock);
428 /*----------------------------------------------------------------------*/
430 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
433 struct usb_request *req;
434 struct ep_data *epdata;
436 struct mm_struct *mm;
437 struct work_struct work;
444 static int ep_aio_cancel(struct kiocb *iocb)
446 struct kiocb_priv *priv = iocb->private;
447 struct ep_data *epdata;
451 epdata = priv->epdata;
452 // spin_lock(&epdata->dev->lock);
453 if (likely(epdata && epdata->ep && priv->req))
454 value = usb_ep_dequeue (epdata->ep, priv->req);
457 // spin_unlock(&epdata->dev->lock);
463 static void ep_user_copy_worker(struct work_struct *work)
465 struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
466 struct mm_struct *mm = priv->mm;
467 struct kiocb *iocb = priv->iocb;
471 ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
476 /* completing the iocb can drop the ctx and mm, don't touch mm after */
477 iocb->ki_complete(iocb, ret, ret);
480 kfree(priv->to_free);
484 static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
486 struct kiocb *iocb = req->context;
487 struct kiocb_priv *priv = iocb->private;
488 struct ep_data *epdata = priv->epdata;
490 /* lock against disconnect (and ideally, cancel) */
491 spin_lock(&epdata->dev->lock);
495 /* if this was a write or a read returning no data then we
496 * don't need to copy anything to userspace, so we can
497 * complete the aio request immediately.
499 if (priv->to_free == NULL || unlikely(req->actual == 0)) {
501 kfree(priv->to_free);
503 iocb->private = NULL;
504 /* aio_complete() reports bytes-transferred _and_ faults */
506 iocb->ki_complete(iocb, req->actual ? req->actual : req->status,
509 /* ep_copy_to_user() won't report both; we hide some faults */
510 if (unlikely(0 != req->status))
511 DBG(epdata->dev, "%s fault %d len %d\n",
512 ep->name, req->status, req->actual);
514 priv->buf = req->buf;
515 priv->actual = req->actual;
516 INIT_WORK(&priv->work, ep_user_copy_worker);
517 schedule_work(&priv->work);
520 usb_ep_free_request(ep, req);
521 spin_unlock(&epdata->dev->lock);
525 static ssize_t ep_aio(struct kiocb *iocb,
526 struct kiocb_priv *priv,
527 struct ep_data *epdata,
531 struct usb_request *req;
534 iocb->private = priv;
537 kiocb_set_cancel_fn(iocb, ep_aio_cancel);
539 priv->epdata = epdata;
541 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
543 /* each kiocb is coupled to one usb_request, but we can't
544 * allocate or submit those if the host disconnected.
546 spin_lock_irq(&epdata->dev->lock);
548 if (unlikely(epdata->ep == NULL))
551 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
559 req->complete = ep_aio_complete;
561 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
562 if (unlikely(0 != value)) {
563 usb_ep_free_request(epdata->ep, req);
566 spin_unlock_irq(&epdata->dev->lock);
570 spin_unlock_irq(&epdata->dev->lock);
571 kfree(priv->to_free);
578 ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
580 struct file *file = iocb->ki_filp;
581 struct ep_data *epdata = file->private_data;
582 size_t len = iov_iter_count(to);
586 if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
589 /* halt any endpoint by doing a "wrong direction" i/o call */
590 if (usb_endpoint_dir_in(&epdata->desc)) {
591 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
592 !is_sync_kiocb(iocb)) {
593 mutex_unlock(&epdata->lock);
596 DBG (epdata->dev, "%s halt\n", epdata->name);
597 spin_lock_irq(&epdata->dev->lock);
598 if (likely(epdata->ep != NULL))
599 usb_ep_set_halt(epdata->ep);
600 spin_unlock_irq(&epdata->dev->lock);
601 mutex_unlock(&epdata->lock);
605 buf = kmalloc(len, GFP_KERNEL);
606 if (unlikely(!buf)) {
607 mutex_unlock(&epdata->lock);
610 if (is_sync_kiocb(iocb)) {
611 value = ep_io(epdata, buf, len);
612 if (value >= 0 && (copy_to_iter(buf, value, to) != value))
615 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
619 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
620 if (!priv->to_free) {
624 value = ep_aio(iocb, priv, epdata, buf, len);
625 if (value == -EIOCBQUEUED)
630 mutex_unlock(&epdata->lock);
634 static ssize_t ep_config(struct ep_data *, const char *, size_t);
637 ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
639 struct file *file = iocb->ki_filp;
640 struct ep_data *epdata = file->private_data;
641 size_t len = iov_iter_count(from);
646 if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
649 configured = epdata->state == STATE_EP_ENABLED;
651 /* halt any endpoint by doing a "wrong direction" i/o call */
652 if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
653 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
654 !is_sync_kiocb(iocb)) {
655 mutex_unlock(&epdata->lock);
658 DBG (epdata->dev, "%s halt\n", epdata->name);
659 spin_lock_irq(&epdata->dev->lock);
660 if (likely(epdata->ep != NULL))
661 usb_ep_set_halt(epdata->ep);
662 spin_unlock_irq(&epdata->dev->lock);
663 mutex_unlock(&epdata->lock);
667 buf = kmalloc(len, GFP_KERNEL);
668 if (unlikely(!buf)) {
669 mutex_unlock(&epdata->lock);
673 if (unlikely(copy_from_iter(buf, len, from) != len)) {
678 if (unlikely(!configured)) {
679 value = ep_config(epdata, buf, len);
680 } else if (is_sync_kiocb(iocb)) {
681 value = ep_io(epdata, buf, len);
683 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
686 value = ep_aio(iocb, priv, epdata, buf, len);
687 if (value == -EIOCBQUEUED)
693 mutex_unlock(&epdata->lock);
697 /*----------------------------------------------------------------------*/
699 /* used after endpoint configuration */
700 static const struct file_operations ep_io_operations = {
701 .owner = THIS_MODULE,
704 .release = ep_release,
706 .unlocked_ioctl = ep_ioctl,
707 .read_iter = ep_read_iter,
708 .write_iter = ep_write_iter,
711 /* ENDPOINT INITIALIZATION
713 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
714 * status = write (fd, descriptors, sizeof descriptors)
716 * That write establishes the endpoint configuration, configuring
717 * the controller to process bulk, interrupt, or isochronous transfers
718 * at the right maxpacket size, and so on.
720 * The descriptors are message type 1, identified by a host order u32
721 * at the beginning of what's written. Descriptor order is: full/low
722 * speed descriptor, then optional high speed descriptor.
725 ep_config (struct ep_data *data, const char *buf, size_t len)
729 int value, length = len;
731 if (data->state != STATE_EP_READY) {
737 if (len < USB_DT_ENDPOINT_SIZE + 4)
740 /* we might need to change message format someday */
741 memcpy(&tag, buf, 4);
743 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
749 /* NOTE: audio endpoint extensions not accepted here;
750 * just don't include the extra bytes.
753 /* full/low speed descriptor, then high speed */
754 memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
755 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
756 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
758 if (len != USB_DT_ENDPOINT_SIZE) {
759 if (len != 2 * USB_DT_ENDPOINT_SIZE)
761 memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
762 USB_DT_ENDPOINT_SIZE);
763 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
764 || data->hs_desc.bDescriptorType
765 != USB_DT_ENDPOINT) {
766 DBG(data->dev, "config %s, bad hs length or type\n",
772 spin_lock_irq (&data->dev->lock);
773 if (data->dev->state == STATE_DEV_UNBOUND) {
783 switch (data->dev->gadget->speed) {
786 ep->desc = &data->desc;
789 /* fails if caller didn't provide that descriptor... */
790 ep->desc = &data->hs_desc;
793 DBG(data->dev, "unconnected, %s init abandoned\n",
798 value = usb_ep_enable(ep);
800 data->state = STATE_EP_ENABLED;
804 spin_unlock_irq (&data->dev->lock);
807 data->desc.bDescriptorType = 0;
808 data->hs_desc.bDescriptorType = 0;
817 ep_open (struct inode *inode, struct file *fd)
819 struct ep_data *data = inode->i_private;
822 if (mutex_lock_interruptible(&data->lock) != 0)
824 spin_lock_irq (&data->dev->lock);
825 if (data->dev->state == STATE_DEV_UNBOUND)
827 else if (data->state == STATE_EP_DISABLED) {
829 data->state = STATE_EP_READY;
831 fd->private_data = data;
832 VDEBUG (data->dev, "%s ready\n", data->name);
834 DBG (data->dev, "%s state %d\n",
835 data->name, data->state);
836 spin_unlock_irq (&data->dev->lock);
837 mutex_unlock(&data->lock);
841 /*----------------------------------------------------------------------*/
843 /* EP0 IMPLEMENTATION can be partly in userspace.
845 * Drivers that use this facility receive various events, including
846 * control requests the kernel doesn't handle. Drivers that don't
847 * use this facility may be too simple-minded for real applications.
850 static inline void ep0_readable (struct dev_data *dev)
852 wake_up (&dev->wait);
853 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
856 static void clean_req (struct usb_ep *ep, struct usb_request *req)
858 struct dev_data *dev = ep->driver_data;
860 if (req->buf != dev->rbuf) {
862 req->buf = dev->rbuf;
864 req->complete = epio_complete;
865 dev->setup_out_ready = 0;
868 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
870 struct dev_data *dev = ep->driver_data;
874 /* for control OUT, data must still get to userspace */
875 spin_lock_irqsave(&dev->lock, flags);
876 if (!dev->setup_in) {
877 dev->setup_out_error = (req->status != 0);
878 if (!dev->setup_out_error)
880 dev->setup_out_ready = 1;
884 /* clean up as appropriate */
885 if (free && req->buf != &dev->rbuf)
887 req->complete = epio_complete;
888 spin_unlock_irqrestore(&dev->lock, flags);
891 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
893 struct dev_data *dev = ep->driver_data;
895 if (dev->setup_out_ready) {
896 DBG (dev, "ep0 request busy!\n");
899 if (len > sizeof (dev->rbuf))
900 req->buf = kmalloc(len, GFP_ATOMIC);
901 if (req->buf == NULL) {
902 req->buf = dev->rbuf;
905 req->complete = ep0_complete;
912 ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
914 struct dev_data *dev = fd->private_data;
916 enum ep0_state state;
918 spin_lock_irq (&dev->lock);
919 if (dev->state <= STATE_DEV_OPENED) {
924 /* report fd mode change before acting on it */
925 if (dev->setup_abort) {
926 dev->setup_abort = 0;
931 /* control DATA stage */
932 if ((state = dev->state) == STATE_DEV_SETUP) {
934 if (dev->setup_in) { /* stall IN */
935 VDEBUG(dev, "ep0in stall\n");
936 (void) usb_ep_set_halt (dev->gadget->ep0);
938 dev->state = STATE_DEV_CONNECTED;
940 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
941 struct usb_ep *ep = dev->gadget->ep0;
942 struct usb_request *req = dev->req;
944 if ((retval = setup_req (ep, req, 0)) == 0) {
946 spin_unlock_irq (&dev->lock);
947 retval = usb_ep_queue (ep, req, GFP_KERNEL);
948 spin_lock_irq (&dev->lock);
951 dev->state = STATE_DEV_CONNECTED;
953 /* assume that was SET_CONFIGURATION */
954 if (dev->current_config) {
957 if (gadget_is_dualspeed(dev->gadget)
958 && (dev->gadget->speed
960 power = dev->hs_config->bMaxPower;
962 power = dev->config->bMaxPower;
963 usb_gadget_vbus_draw(dev->gadget, 2 * power);
966 } else { /* collect OUT data */
967 if ((fd->f_flags & O_NONBLOCK) != 0
968 && !dev->setup_out_ready) {
972 spin_unlock_irq (&dev->lock);
973 retval = wait_event_interruptible (dev->wait,
974 dev->setup_out_ready != 0);
976 /* FIXME state could change from under us */
977 spin_lock_irq (&dev->lock);
981 if (dev->state != STATE_DEV_SETUP) {
985 dev->state = STATE_DEV_CONNECTED;
987 if (dev->setup_out_error)
990 len = min (len, (size_t)dev->req->actual);
992 spin_unlock_irq(&dev->lock);
993 if (copy_to_user (buf, dev->req->buf, len))
997 spin_lock_irq(&dev->lock);
999 clean_req (dev->gadget->ep0, dev->req);
1000 /* NOTE userspace can't yet choose to stall */
1006 /* else normal: return event data */
1007 if (len < sizeof dev->event [0]) {
1011 len -= len % sizeof (struct usb_gadgetfs_event);
1012 dev->usermode_setup = 1;
1015 /* return queued events right away */
1016 if (dev->ev_next != 0) {
1019 n = len / sizeof (struct usb_gadgetfs_event);
1020 if (dev->ev_next < n)
1023 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1024 for (i = 0; i < n; i++) {
1025 if (dev->event [i].type == GADGETFS_SETUP) {
1026 dev->state = STATE_DEV_SETUP;
1031 spin_unlock_irq (&dev->lock);
1032 len = n * sizeof (struct usb_gadgetfs_event);
1033 if (copy_to_user (buf, &dev->event, len))
1038 /* NOTE this doesn't guard against broken drivers;
1039 * concurrent ep0 readers may lose events.
1041 spin_lock_irq (&dev->lock);
1042 if (dev->ev_next > n) {
1043 memmove(&dev->event[0], &dev->event[n],
1044 sizeof (struct usb_gadgetfs_event)
1045 * (dev->ev_next - n));
1048 spin_unlock_irq (&dev->lock);
1052 if (fd->f_flags & O_NONBLOCK) {
1059 DBG (dev, "fail %s, state %d\n", __func__, state);
1062 case STATE_DEV_UNCONNECTED:
1063 case STATE_DEV_CONNECTED:
1064 spin_unlock_irq (&dev->lock);
1065 DBG (dev, "%s wait\n", __func__);
1067 /* wait for events */
1068 retval = wait_event_interruptible (dev->wait,
1072 spin_lock_irq (&dev->lock);
1077 spin_unlock_irq (&dev->lock);
1081 static struct usb_gadgetfs_event *
1082 next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1084 struct usb_gadgetfs_event *event;
1088 /* these events purge the queue */
1089 case GADGETFS_DISCONNECT:
1090 if (dev->state == STATE_DEV_SETUP)
1091 dev->setup_abort = 1;
1093 case GADGETFS_CONNECT:
1096 case GADGETFS_SETUP: /* previous request timed out */
1097 case GADGETFS_SUSPEND: /* same effect */
1098 /* these events can't be repeated */
1099 for (i = 0; i != dev->ev_next; i++) {
1100 if (dev->event [i].type != type)
1102 DBG(dev, "discard old event[%d] %d\n", i, type);
1104 if (i == dev->ev_next)
1106 /* indices start at zero, for simplicity */
1107 memmove (&dev->event [i], &dev->event [i + 1],
1108 sizeof (struct usb_gadgetfs_event)
1109 * (dev->ev_next - i));
1115 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
1116 event = &dev->event [dev->ev_next++];
1117 BUG_ON (dev->ev_next > N_EVENT);
1118 memset (event, 0, sizeof *event);
1124 ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1126 struct dev_data *dev = fd->private_data;
1127 ssize_t retval = -ESRCH;
1129 /* report fd mode change before acting on it */
1130 if (dev->setup_abort) {
1131 dev->setup_abort = 0;
1134 /* data and/or status stage for control request */
1135 } else if (dev->state == STATE_DEV_SETUP) {
1137 len = min_t(size_t, len, dev->setup_wLength);
1138 if (dev->setup_in) {
1139 retval = setup_req (dev->gadget->ep0, dev->req, len);
1141 dev->state = STATE_DEV_CONNECTED;
1143 spin_unlock_irq (&dev->lock);
1144 if (copy_from_user (dev->req->buf, buf, len))
1147 if (len < dev->setup_wLength)
1149 retval = usb_ep_queue (
1150 dev->gadget->ep0, dev->req,
1153 spin_lock_irq(&dev->lock);
1156 clean_req (dev->gadget->ep0, dev->req);
1163 /* can stall some OUT transfers */
1164 } else if (dev->setup_can_stall) {
1165 VDEBUG(dev, "ep0out stall\n");
1166 (void) usb_ep_set_halt (dev->gadget->ep0);
1168 dev->state = STATE_DEV_CONNECTED;
1170 DBG(dev, "bogus ep0out stall!\n");
1173 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
1179 ep0_fasync (int f, struct file *fd, int on)
1181 struct dev_data *dev = fd->private_data;
1182 // caller must F_SETOWN before signal delivery happens
1183 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
1184 return fasync_helper (f, fd, on, &dev->fasync);
1187 static struct usb_gadget_driver gadgetfs_driver;
1190 dev_release (struct inode *inode, struct file *fd)
1192 struct dev_data *dev = fd->private_data;
1194 /* closing ep0 === shutdown all */
1196 if (dev->gadget_registered) {
1197 usb_gadget_unregister_driver (&gadgetfs_driver);
1198 dev->gadget_registered = false;
1201 /* at this point "good" hardware has disconnected the
1202 * device from USB; the host won't see it any more.
1203 * alternatively, all host requests will time out.
1209 /* other endpoints were all decoupled from this device */
1210 spin_lock_irq(&dev->lock);
1211 dev->state = STATE_DEV_DISABLED;
1212 spin_unlock_irq(&dev->lock);
1219 ep0_poll (struct file *fd, poll_table *wait)
1221 struct dev_data *dev = fd->private_data;
1224 if (dev->state <= STATE_DEV_OPENED)
1225 return DEFAULT_POLLMASK;
1227 poll_wait(fd, &dev->wait, wait);
1229 spin_lock_irq (&dev->lock);
1231 /* report fd mode change before acting on it */
1232 if (dev->setup_abort) {
1233 dev->setup_abort = 0;
1238 if (dev->state == STATE_DEV_SETUP) {
1239 if (dev->setup_in || dev->setup_can_stall)
1242 if (dev->ev_next != 0)
1246 spin_unlock_irq(&dev->lock);
1250 static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1252 struct dev_data *dev = fd->private_data;
1253 struct usb_gadget *gadget = dev->gadget;
1256 spin_lock_irq(&dev->lock);
1257 if (dev->state == STATE_DEV_OPENED ||
1258 dev->state == STATE_DEV_UNBOUND) {
1259 /* Not bound to a UDC */
1260 } else if (gadget->ops->ioctl) {
1262 spin_unlock_irq(&dev->lock);
1264 ret = gadget->ops->ioctl (gadget, code, value);
1266 spin_lock_irq(&dev->lock);
1269 spin_unlock_irq(&dev->lock);
1274 /*----------------------------------------------------------------------*/
1276 /* The in-kernel gadget driver handles most ep0 issues, in particular
1277 * enumerating the single configuration (as provided from user space).
1279 * Unrecognized ep0 requests may be handled in user space.
1282 static void make_qualifier (struct dev_data *dev)
1284 struct usb_qualifier_descriptor qual;
1285 struct usb_device_descriptor *desc;
1287 qual.bLength = sizeof qual;
1288 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1289 qual.bcdUSB = cpu_to_le16 (0x0200);
1292 qual.bDeviceClass = desc->bDeviceClass;
1293 qual.bDeviceSubClass = desc->bDeviceSubClass;
1294 qual.bDeviceProtocol = desc->bDeviceProtocol;
1296 /* assumes ep0 uses the same value for both speeds ... */
1297 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1299 qual.bNumConfigurations = 1;
1302 memcpy (dev->rbuf, &qual, sizeof qual);
1306 config_buf (struct dev_data *dev, u8 type, unsigned index)
1311 /* only one configuration */
1315 if (gadget_is_dualspeed(dev->gadget)) {
1316 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1317 if (type == USB_DT_OTHER_SPEED_CONFIG)
1321 dev->req->buf = dev->hs_config;
1322 len = le16_to_cpu(dev->hs_config->wTotalLength);
1324 dev->req->buf = dev->config;
1325 len = le16_to_cpu(dev->config->wTotalLength);
1327 ((u8 *)dev->req->buf) [1] = type;
1332 gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1334 struct dev_data *dev = get_gadget_data (gadget);
1335 struct usb_request *req = dev->req;
1336 int value = -EOPNOTSUPP;
1337 struct usb_gadgetfs_event *event;
1338 u16 w_value = le16_to_cpu(ctrl->wValue);
1339 u16 w_length = le16_to_cpu(ctrl->wLength);
1341 if (w_length > RBUF_SIZE) {
1342 if (ctrl->bRequestType & USB_DIR_IN) {
1343 /* Cast away the const, we are going to overwrite on purpose. */
1344 __le16 *temp = (__le16 *)&ctrl->wLength;
1346 *temp = cpu_to_le16(RBUF_SIZE);
1347 w_length = RBUF_SIZE;
1353 spin_lock (&dev->lock);
1354 dev->setup_abort = 0;
1355 if (dev->state == STATE_DEV_UNCONNECTED) {
1356 if (gadget_is_dualspeed(gadget)
1357 && gadget->speed == USB_SPEED_HIGH
1358 && dev->hs_config == NULL) {
1359 spin_unlock(&dev->lock);
1360 ERROR (dev, "no high speed config??\n");
1364 dev->state = STATE_DEV_CONNECTED;
1366 INFO (dev, "connected\n");
1367 event = next_event (dev, GADGETFS_CONNECT);
1368 event->u.speed = gadget->speed;
1371 /* host may have given up waiting for response. we can miss control
1372 * requests handled lower down (device/endpoint status and features);
1373 * then ep0_{read,write} will report the wrong status. controller
1374 * driver will have aborted pending i/o.
1376 } else if (dev->state == STATE_DEV_SETUP)
1377 dev->setup_abort = 1;
1379 req->buf = dev->rbuf;
1380 req->context = NULL;
1381 switch (ctrl->bRequest) {
1383 case USB_REQ_GET_DESCRIPTOR:
1384 if (ctrl->bRequestType != USB_DIR_IN)
1386 switch (w_value >> 8) {
1389 value = min (w_length, (u16) sizeof *dev->dev);
1390 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1391 req->buf = dev->dev;
1393 case USB_DT_DEVICE_QUALIFIER:
1394 if (!dev->hs_config)
1396 value = min (w_length, (u16)
1397 sizeof (struct usb_qualifier_descriptor));
1398 make_qualifier (dev);
1400 case USB_DT_OTHER_SPEED_CONFIG:
1403 value = config_buf (dev,
1407 value = min (w_length, (u16) value);
1412 default: // all others are errors
1417 /* currently one config, two speeds */
1418 case USB_REQ_SET_CONFIGURATION:
1419 if (ctrl->bRequestType != 0)
1421 if (0 == (u8) w_value) {
1423 dev->current_config = 0;
1424 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1425 // user mode expected to disable endpoints
1429 if (gadget_is_dualspeed(gadget)
1430 && gadget->speed == USB_SPEED_HIGH) {
1431 config = dev->hs_config->bConfigurationValue;
1432 power = dev->hs_config->bMaxPower;
1434 config = dev->config->bConfigurationValue;
1435 power = dev->config->bMaxPower;
1438 if (config == (u8) w_value) {
1440 dev->current_config = config;
1441 usb_gadget_vbus_draw(gadget, 2 * power);
1445 /* report SET_CONFIGURATION like any other control request,
1446 * except that usermode may not stall this. the next
1447 * request mustn't be allowed start until this finishes:
1448 * endpoints and threads set up, etc.
1450 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1451 * has bad/racey automagic that prevents synchronizing here.
1452 * even kernel mode drivers often miss them.
1455 INFO (dev, "configuration #%d\n", dev->current_config);
1456 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
1457 if (dev->usermode_setup) {
1458 dev->setup_can_stall = 0;
1464 #ifndef CONFIG_USB_PXA25X
1465 /* PXA automagically handles this request too */
1466 case USB_REQ_GET_CONFIGURATION:
1467 if (ctrl->bRequestType != 0x80)
1469 *(u8 *)req->buf = dev->current_config;
1470 value = min (w_length, (u16) 1);
1476 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1477 dev->usermode_setup ? "delegate" : "fail",
1478 ctrl->bRequestType, ctrl->bRequest,
1479 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1481 /* if there's an ep0 reader, don't stall */
1482 if (dev->usermode_setup) {
1483 dev->setup_can_stall = 1;
1485 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1487 dev->setup_wLength = w_length;
1488 dev->setup_out_ready = 0;
1489 dev->setup_out_error = 0;
1492 /* read DATA stage for OUT right away */
1493 if (unlikely (!dev->setup_in && w_length)) {
1494 value = setup_req (gadget->ep0, dev->req,
1500 spin_unlock (&dev->lock);
1501 value = usb_ep_queue (gadget->ep0, dev->req,
1503 spin_lock (&dev->lock);
1506 clean_req (gadget->ep0, dev->req);
1510 /* we can't currently stall these */
1511 dev->setup_can_stall = 0;
1514 /* state changes when reader collects event */
1515 event = next_event (dev, GADGETFS_SETUP);
1516 event->u.setup = *ctrl;
1518 spin_unlock (&dev->lock);
1523 /* proceed with data transfer and status phases? */
1524 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
1525 req->length = value;
1526 req->zero = value < w_length;
1529 spin_unlock (&dev->lock);
1530 value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
1531 spin_lock(&dev->lock);
1533 spin_unlock(&dev->lock);
1535 DBG (dev, "ep_queue --> %d\n", value);
1541 /* device stalls when value < 0 */
1542 spin_unlock (&dev->lock);
1546 static void destroy_ep_files (struct dev_data *dev)
1548 DBG (dev, "%s %d\n", __func__, dev->state);
1550 /* dev->state must prevent interference */
1551 spin_lock_irq (&dev->lock);
1552 while (!list_empty(&dev->epfiles)) {
1554 struct inode *parent;
1555 struct dentry *dentry;
1557 /* break link to FS */
1558 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
1559 list_del_init (&ep->epfiles);
1560 spin_unlock_irq (&dev->lock);
1562 dentry = ep->dentry;
1564 parent = d_inode(dentry->d_parent);
1566 /* break link to controller */
1567 mutex_lock(&ep->lock);
1568 if (ep->state == STATE_EP_ENABLED)
1569 (void) usb_ep_disable (ep->ep);
1570 ep->state = STATE_EP_UNBOUND;
1571 usb_ep_free_request (ep->ep, ep->req);
1573 mutex_unlock(&ep->lock);
1575 wake_up (&ep->wait);
1578 /* break link to dcache */
1582 inode_unlock(parent);
1584 spin_lock_irq (&dev->lock);
1586 spin_unlock_irq (&dev->lock);
1590 static struct dentry *
1591 gadgetfs_create_file (struct super_block *sb, char const *name,
1592 void *data, const struct file_operations *fops);
1594 static int activate_ep_files (struct dev_data *dev)
1597 struct ep_data *data;
1599 gadget_for_each_ep (ep, dev->gadget) {
1601 data = kzalloc(sizeof(*data), GFP_KERNEL);
1604 data->state = STATE_EP_DISABLED;
1605 mutex_init(&data->lock);
1606 init_waitqueue_head (&data->wait);
1608 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1609 atomic_set (&data->count, 1);
1614 ep->driver_data = data;
1616 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1620 data->dentry = gadgetfs_create_file (dev->sb, data->name,
1621 data, &ep_io_operations);
1624 list_add_tail (&data->epfiles, &dev->epfiles);
1629 usb_ep_free_request (ep, data->req);
1634 DBG (dev, "%s enomem\n", __func__);
1635 destroy_ep_files (dev);
1640 gadgetfs_unbind (struct usb_gadget *gadget)
1642 struct dev_data *dev = get_gadget_data (gadget);
1644 DBG (dev, "%s\n", __func__);
1646 spin_lock_irq (&dev->lock);
1647 dev->state = STATE_DEV_UNBOUND;
1648 while (dev->udc_usage > 0) {
1649 spin_unlock_irq(&dev->lock);
1650 usleep_range(1000, 2000);
1651 spin_lock_irq(&dev->lock);
1653 spin_unlock_irq (&dev->lock);
1655 destroy_ep_files (dev);
1656 gadget->ep0->driver_data = NULL;
1657 set_gadget_data (gadget, NULL);
1659 /* we've already been disconnected ... no i/o is active */
1661 usb_ep_free_request (gadget->ep0, dev->req);
1662 DBG (dev, "%s done\n", __func__);
1666 static struct dev_data *the_device;
1668 static int gadgetfs_bind(struct usb_gadget *gadget,
1669 struct usb_gadget_driver *driver)
1671 struct dev_data *dev = the_device;
1675 if (0 != strcmp (CHIP, gadget->name)) {
1676 pr_err("%s expected %s controller not %s\n",
1677 shortname, CHIP, gadget->name);
1681 set_gadget_data (gadget, dev);
1682 dev->gadget = gadget;
1683 gadget->ep0->driver_data = dev;
1685 /* preallocate control response and buffer */
1686 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1689 dev->req->context = NULL;
1690 dev->req->complete = epio_complete;
1692 if (activate_ep_files (dev) < 0)
1695 INFO (dev, "bound to %s driver\n", gadget->name);
1696 spin_lock_irq(&dev->lock);
1697 dev->state = STATE_DEV_UNCONNECTED;
1698 spin_unlock_irq(&dev->lock);
1703 gadgetfs_unbind (gadget);
1708 gadgetfs_disconnect (struct usb_gadget *gadget)
1710 struct dev_data *dev = get_gadget_data (gadget);
1711 unsigned long flags;
1713 spin_lock_irqsave (&dev->lock, flags);
1714 if (dev->state == STATE_DEV_UNCONNECTED)
1716 dev->state = STATE_DEV_UNCONNECTED;
1718 INFO (dev, "disconnected\n");
1719 next_event (dev, GADGETFS_DISCONNECT);
1722 spin_unlock_irqrestore (&dev->lock, flags);
1726 gadgetfs_suspend (struct usb_gadget *gadget)
1728 struct dev_data *dev = get_gadget_data (gadget);
1729 unsigned long flags;
1731 INFO (dev, "suspended from state %d\n", dev->state);
1732 spin_lock_irqsave(&dev->lock, flags);
1733 switch (dev->state) {
1734 case STATE_DEV_SETUP: // VERY odd... host died??
1735 case STATE_DEV_CONNECTED:
1736 case STATE_DEV_UNCONNECTED:
1737 next_event (dev, GADGETFS_SUSPEND);
1743 spin_unlock_irqrestore(&dev->lock, flags);
1746 static struct usb_gadget_driver gadgetfs_driver = {
1747 .function = (char *) driver_desc,
1748 .bind = gadgetfs_bind,
1749 .unbind = gadgetfs_unbind,
1750 .setup = gadgetfs_setup,
1751 .reset = gadgetfs_disconnect,
1752 .disconnect = gadgetfs_disconnect,
1753 .suspend = gadgetfs_suspend,
1756 .name = (char *) shortname,
1760 /*----------------------------------------------------------------------*/
1761 /* DEVICE INITIALIZATION
1763 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1764 * status = write (fd, descriptors, sizeof descriptors)
1766 * That write establishes the device configuration, so the kernel can
1767 * bind to the controller ... guaranteeing it can handle enumeration
1768 * at all necessary speeds. Descriptor order is:
1770 * . message tag (u32, host order) ... for now, must be zero; it
1771 * would change to support features like multi-config devices
1772 * . full/low speed config ... all wTotalLength bytes (with interface,
1773 * class, altsetting, endpoint, and other descriptors)
1774 * . high speed config ... all descriptors, for high speed operation;
1775 * this one's optional except for high-speed hardware
1776 * . device descriptor
1778 * Endpoints are not yet enabled. Drivers must wait until device
1779 * configuration and interface altsetting changes create
1780 * the need to configure (or unconfigure) them.
1782 * After initialization, the device stays active for as long as that
1783 * $CHIP file is open. Events must then be read from that descriptor,
1784 * such as configuration notifications.
1787 static int is_valid_config(struct usb_config_descriptor *config,
1790 return config->bDescriptorType == USB_DT_CONFIG
1791 && config->bLength == USB_DT_CONFIG_SIZE
1792 && total >= USB_DT_CONFIG_SIZE
1793 && config->bConfigurationValue != 0
1794 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1795 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1796 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1797 /* FIXME check lengths: walk to end */
1801 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1803 struct dev_data *dev = fd->private_data;
1804 ssize_t value, length = len;
1809 spin_lock_irq(&dev->lock);
1810 if (dev->state > STATE_DEV_OPENED) {
1811 value = ep0_write(fd, buf, len, ptr);
1812 spin_unlock_irq(&dev->lock);
1815 spin_unlock_irq(&dev->lock);
1817 if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
1818 (len > PAGE_SIZE * 4))
1821 /* we might need to change message format someday */
1822 if (copy_from_user (&tag, buf, 4))
1829 kbuf = memdup_user(buf, length);
1831 return PTR_ERR(kbuf);
1833 spin_lock_irq (&dev->lock);
1841 /* full or low speed config */
1842 dev->config = (void *) kbuf;
1843 total = le16_to_cpu(dev->config->wTotalLength);
1844 if (!is_valid_config(dev->config, total) ||
1845 total > length - USB_DT_DEVICE_SIZE)
1850 /* optional high speed config */
1851 if (kbuf [1] == USB_DT_CONFIG) {
1852 dev->hs_config = (void *) kbuf;
1853 total = le16_to_cpu(dev->hs_config->wTotalLength);
1854 if (!is_valid_config(dev->hs_config, total) ||
1855 total > length - USB_DT_DEVICE_SIZE)
1860 dev->hs_config = NULL;
1863 /* could support multiple configs, using another encoding! */
1865 /* device descriptor (tweaked for paranoia) */
1866 if (length != USB_DT_DEVICE_SIZE)
1868 dev->dev = (void *)kbuf;
1869 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1870 || dev->dev->bDescriptorType != USB_DT_DEVICE
1871 || dev->dev->bNumConfigurations != 1)
1873 dev->dev->bNumConfigurations = 1;
1874 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
1876 /* triggers gadgetfs_bind(); then we can enumerate. */
1877 spin_unlock_irq (&dev->lock);
1879 gadgetfs_driver.max_speed = USB_SPEED_HIGH;
1881 gadgetfs_driver.max_speed = USB_SPEED_FULL;
1883 value = usb_gadget_probe_driver(&gadgetfs_driver);
1888 /* at this point "good" hardware has for the first time
1889 * let the USB the host see us. alternatively, if users
1890 * unplug/replug that will clear all the error state.
1892 * note: everything running before here was guaranteed
1893 * to choke driver model style diagnostics. from here
1894 * on, they can work ... except in cleanup paths that
1895 * kick in after the ep0 descriptor is closed.
1898 dev->gadget_registered = true;
1903 spin_unlock_irq (&dev->lock);
1904 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
1911 dev_open (struct inode *inode, struct file *fd)
1913 struct dev_data *dev = inode->i_private;
1916 spin_lock_irq(&dev->lock);
1917 if (dev->state == STATE_DEV_DISABLED) {
1919 dev->state = STATE_DEV_OPENED;
1920 fd->private_data = dev;
1924 spin_unlock_irq(&dev->lock);
1928 static const struct file_operations ep0_operations = {
1929 .llseek = no_llseek,
1933 .write = dev_config,
1934 .fasync = ep0_fasync,
1936 .unlocked_ioctl = dev_ioctl,
1937 .release = dev_release,
1940 /*----------------------------------------------------------------------*/
1942 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1944 * Mounting the filesystem creates a controller file, used first for
1945 * device configuration then later for event monitoring.
1949 /* FIXME PAM etc could set this security policy without mount options
1950 * if epfiles inherited ownership and permissons from ep0 ...
1953 static unsigned default_uid;
1954 static unsigned default_gid;
1955 static unsigned default_perm = S_IRUSR | S_IWUSR;
1957 module_param (default_uid, uint, 0644);
1958 module_param (default_gid, uint, 0644);
1959 module_param (default_perm, uint, 0644);
1962 static struct inode *
1963 gadgetfs_make_inode (struct super_block *sb,
1964 void *data, const struct file_operations *fops,
1967 struct inode *inode = new_inode (sb);
1970 inode->i_ino = get_next_ino();
1971 inode->i_mode = mode;
1972 inode->i_uid = make_kuid(&init_user_ns, default_uid);
1973 inode->i_gid = make_kgid(&init_user_ns, default_gid);
1974 inode->i_atime = inode->i_mtime = inode->i_ctime
1975 = current_time(inode);
1976 inode->i_private = data;
1977 inode->i_fop = fops;
1982 /* creates in fs root directory, so non-renamable and non-linkable.
1983 * so inode and dentry are paired, until device reconfig.
1985 static struct dentry *
1986 gadgetfs_create_file (struct super_block *sb, char const *name,
1987 void *data, const struct file_operations *fops)
1989 struct dentry *dentry;
1990 struct inode *inode;
1992 dentry = d_alloc_name(sb->s_root, name);
1996 inode = gadgetfs_make_inode (sb, data, fops,
1997 S_IFREG | (default_perm & S_IRWXUGO));
2002 d_add (dentry, inode);
2006 static const struct super_operations gadget_fs_operations = {
2007 .statfs = simple_statfs,
2008 .drop_inode = generic_delete_inode,
2012 gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
2014 struct inode *inode;
2015 struct dev_data *dev;
2020 CHIP = usb_get_gadget_udc_name();
2025 sb->s_blocksize = PAGE_SIZE;
2026 sb->s_blocksize_bits = PAGE_SHIFT;
2027 sb->s_magic = GADGETFS_MAGIC;
2028 sb->s_op = &gadget_fs_operations;
2029 sb->s_time_gran = 1;
2032 inode = gadgetfs_make_inode (sb,
2033 NULL, &simple_dir_operations,
2034 S_IFDIR | S_IRUGO | S_IXUGO);
2037 inode->i_op = &simple_dir_inode_operations;
2038 if (!(sb->s_root = d_make_root (inode)))
2041 /* the ep0 file is named after the controller we expect;
2042 * user mode code can use it for sanity checks, like we do.
2049 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
2055 /* other endpoint files are available after hardware setup,
2056 * from binding to a controller.
2068 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2069 static struct dentry *
2070 gadgetfs_mount (struct file_system_type *t, int flags,
2071 const char *path, void *opts)
2073 return mount_single (t, flags, opts, gadgetfs_fill_super);
2077 gadgetfs_kill_sb (struct super_block *sb)
2079 kill_litter_super (sb);
2081 put_dev (the_device);
2088 /*----------------------------------------------------------------------*/
2090 static struct file_system_type gadgetfs_type = {
2091 .owner = THIS_MODULE,
2093 .mount = gadgetfs_mount,
2094 .kill_sb = gadgetfs_kill_sb,
2096 MODULE_ALIAS_FS("gadgetfs");
2098 /*----------------------------------------------------------------------*/
2100 static int __init init (void)
2104 status = register_filesystem (&gadgetfs_type);
2106 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2107 shortname, driver_desc);
2112 static void __exit cleanup (void)
2114 pr_debug ("unregister %s\n", shortname);
2115 unregister_filesystem (&gadgetfs_type);
2117 module_exit (cleanup);