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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* #define VERBOSE_DEBUG */
25 #include <linux/init.h>
26 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/uts.h>
30 #include <linux/wait.h>
31 #include <linux/compiler.h>
32 #include <asm/uaccess.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/smp_lock.h>
38 #include <linux/device.h>
39 #include <linux/moduleparam.h>
41 #include <linux/usb/gadgetfs.h>
42 #include <linux/usb/gadget.h>
46 * The gadgetfs API maps each endpoint to a file descriptor so that you
47 * can use standard synchronous read/write calls for I/O. There's some
48 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
49 * drivers show how this works in practice. You can also use AIO to
50 * eliminate I/O gaps between requests, to help when streaming data.
52 * Key parts that must be USB-specific are protocols defining how the
53 * read/write operations relate to the hardware state machines. There
54 * are two types of files. One type is for the device, implementing ep0.
55 * The other type is for each IN or OUT endpoint. In both cases, the
56 * user mode driver must configure the hardware before using it.
58 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
59 * (by writing configuration and device descriptors). Afterwards it
60 * may serve as a source of device events, used to handle all control
61 * requests other than basic enumeration.
63 * - Then, after a SET_CONFIGURATION control request, ep_config() is
64 * called when each /dev/gadget/ep* file is configured (by writing
65 * endpoint descriptors). Afterwards these files are used to write()
66 * IN data or to read() OUT data. To halt the endpoint, a "wrong
67 * direction" request is issued (like reading an IN endpoint).
69 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
70 * not possible on all hardware. For example, precise fault handling with
71 * respect to data left in endpoint fifos after aborted operations; or
72 * selective clearing of endpoint halts, to implement SET_INTERFACE.
75 #define DRIVER_DESC "USB Gadget filesystem"
76 #define DRIVER_VERSION "24 Aug 2004"
78 static const char driver_desc [] = DRIVER_DESC;
79 static const char shortname [] = "gadgetfs";
81 MODULE_DESCRIPTION (DRIVER_DESC);
82 MODULE_AUTHOR ("David Brownell");
83 MODULE_LICENSE ("GPL");
86 /*----------------------------------------------------------------------*/
88 #define GADGETFS_MAGIC 0xaee71ee7
89 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
91 /* /dev/gadget/$CHIP represents ep0 and the whole device */
93 /* DISBLED is the initial state.
95 STATE_DEV_DISABLED = 0,
97 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
98 * ep0/device i/o modes and binding to the controller. Driver
99 * must always write descriptors to initialize the device, then
100 * the device becomes UNCONNECTED until enumeration.
104 /* From then on, ep0 fd is in either of two basic modes:
105 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
106 * - SETUP: read/write will transfer control data and succeed;
107 * or if "wrong direction", performs protocol stall
109 STATE_DEV_UNCONNECTED,
113 /* UNBOUND means the driver closed ep0, so the device won't be
114 * accessible again (DEV_DISABLED) until all fds are closed.
119 /* enough for the whole queue: most events invalidate others */
125 enum ep0_state state; /* P: lock */
126 struct usb_gadgetfs_event event [N_EVENT];
128 struct fasync_struct *fasync;
131 /* drivers reading ep0 MUST handle control requests (SETUP)
132 * reported that way; else the host will time out.
134 unsigned usermode_setup : 1,
140 unsigned setup_wLength;
142 /* the rest is basically write-once */
143 struct usb_config_descriptor *config, *hs_config;
144 struct usb_device_descriptor *dev;
145 struct usb_request *req;
146 struct usb_gadget *gadget;
147 struct list_head epfiles;
149 wait_queue_head_t wait;
150 struct super_block *sb;
151 struct dentry *dentry;
153 /* except this scratch i/o buffer for ep0 */
157 static inline void get_dev (struct dev_data *data)
159 atomic_inc (&data->count);
162 static void put_dev (struct dev_data *data)
164 if (likely (!atomic_dec_and_test (&data->count)))
166 /* needs no more cleanup */
167 BUG_ON (waitqueue_active (&data->wait));
171 static struct dev_data *dev_new (void)
173 struct dev_data *dev;
175 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
178 dev->state = STATE_DEV_DISABLED;
179 atomic_set (&dev->count, 1);
180 spin_lock_init (&dev->lock);
181 INIT_LIST_HEAD (&dev->epfiles);
182 init_waitqueue_head (&dev->wait);
186 /*----------------------------------------------------------------------*/
188 /* other /dev/gadget/$ENDPOINT files represent endpoints */
190 STATE_EP_DISABLED = 0,
200 struct dev_data *dev;
201 /* must hold dev->lock before accessing ep or req */
203 struct usb_request *req;
206 struct usb_endpoint_descriptor desc, hs_desc;
207 struct list_head epfiles;
208 wait_queue_head_t wait;
209 struct dentry *dentry;
213 static inline void get_ep (struct ep_data *data)
215 atomic_inc (&data->count);
218 static void put_ep (struct ep_data *data)
220 if (likely (!atomic_dec_and_test (&data->count)))
223 /* needs no more cleanup */
224 BUG_ON (!list_empty (&data->epfiles));
225 BUG_ON (waitqueue_active (&data->wait));
229 /*----------------------------------------------------------------------*/
231 /* most "how to use the hardware" policy choices are in userspace:
232 * mapping endpoint roles (which the driver needs) to the capabilities
233 * which the usb controller has. most of those capabilities are exposed
234 * implicitly, starting with the driver name and then endpoint names.
237 static const char *CHIP;
239 /*----------------------------------------------------------------------*/
241 /* NOTE: don't use dev_printk calls before binding to the gadget
242 * at the end of ep0 configuration, or after unbind.
245 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
246 #define xprintk(d,level,fmt,args...) \
247 printk(level "%s: " fmt , shortname , ## args)
250 #define DBG(dev,fmt,args...) \
251 xprintk(dev , KERN_DEBUG , fmt , ## args)
253 #define DBG(dev,fmt,args...) \
260 #define VDEBUG(dev,fmt,args...) \
264 #define ERROR(dev,fmt,args...) \
265 xprintk(dev , KERN_ERR , fmt , ## args)
266 #define INFO(dev,fmt,args...) \
267 xprintk(dev , KERN_INFO , fmt , ## args)
270 /*----------------------------------------------------------------------*/
272 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
274 * After opening, configure non-control endpoints. Then use normal
275 * stream read() and write() requests; and maybe ioctl() to get more
276 * precise FIFO status when recovering from cancellation.
279 static void epio_complete (struct usb_ep *ep, struct usb_request *req)
281 struct ep_data *epdata = ep->driver_data;
286 epdata->status = req->status;
288 epdata->status = req->actual;
289 complete ((struct completion *)req->context);
292 /* tasklock endpoint, returning when it's connected.
293 * still need dev->lock to use epdata->ep.
296 get_ready_ep (unsigned f_flags, struct ep_data *epdata)
300 if (f_flags & O_NONBLOCK) {
301 if (!mutex_trylock(&epdata->lock))
303 if (epdata->state != STATE_EP_ENABLED) {
304 mutex_unlock(&epdata->lock);
312 val = mutex_lock_interruptible(&epdata->lock);
316 switch (epdata->state) {
317 case STATE_EP_ENABLED:
319 // case STATE_EP_DISABLED: /* "can't happen" */
320 // case STATE_EP_READY: /* "can't happen" */
321 default: /* error! */
322 pr_debug ("%s: ep %p not available, state %d\n",
323 shortname, epdata, epdata->state);
325 case STATE_EP_UNBOUND: /* clean disconnect */
327 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 /* handle a synchronous OUT bulk/intr/iso transfer */
379 ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
381 struct ep_data *data = fd->private_data;
385 if ((value = get_ready_ep (fd->f_flags, data)) < 0)
388 /* halt any endpoint by doing a "wrong direction" i/o call */
389 if (usb_endpoint_dir_in(&data->desc)) {
390 if (usb_endpoint_xfer_isoc(&data->desc)) {
391 mutex_unlock(&data->lock);
394 DBG (data->dev, "%s halt\n", data->name);
395 spin_lock_irq (&data->dev->lock);
396 if (likely (data->ep != NULL))
397 usb_ep_set_halt (data->ep);
398 spin_unlock_irq (&data->dev->lock);
399 mutex_unlock(&data->lock);
403 /* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */
406 kbuf = kmalloc (len, GFP_KERNEL);
407 if (unlikely (!kbuf))
410 value = ep_io (data, kbuf, len);
411 VDEBUG (data->dev, "%s read %zu OUT, status %d\n",
412 data->name, len, (int) value);
413 if (value >= 0 && copy_to_user (buf, kbuf, value))
417 mutex_unlock(&data->lock);
422 /* handle a synchronous IN bulk/intr/iso transfer */
424 ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
426 struct ep_data *data = fd->private_data;
430 if ((value = get_ready_ep (fd->f_flags, data)) < 0)
433 /* halt any endpoint by doing a "wrong direction" i/o call */
434 if (!usb_endpoint_dir_in(&data->desc)) {
435 if (usb_endpoint_xfer_isoc(&data->desc))
437 DBG (data->dev, "%s halt\n", data->name);
438 spin_lock_irq (&data->dev->lock);
439 if (likely (data->ep != NULL))
440 usb_ep_set_halt (data->ep);
441 spin_unlock_irq (&data->dev->lock);
442 mutex_unlock(&data->lock);
446 /* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */
449 kbuf = kmalloc (len, GFP_KERNEL);
452 if (copy_from_user (kbuf, buf, len)) {
457 value = ep_io (data, kbuf, len);
458 VDEBUG (data->dev, "%s write %zu IN, status %d\n",
459 data->name, len, (int) value);
461 mutex_unlock(&data->lock);
467 ep_release (struct inode *inode, struct file *fd)
469 struct ep_data *data = fd->private_data;
472 value = mutex_lock_interruptible(&data->lock);
476 /* clean up if this can be reopened */
477 if (data->state != STATE_EP_UNBOUND) {
478 data->state = STATE_EP_DISABLED;
479 data->desc.bDescriptorType = 0;
480 data->hs_desc.bDescriptorType = 0;
481 usb_ep_disable(data->ep);
483 mutex_unlock(&data->lock);
488 static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
490 struct ep_data *data = fd->private_data;
493 if ((status = get_ready_ep (fd->f_flags, data)) < 0)
496 spin_lock_irq (&data->dev->lock);
497 if (likely (data->ep != NULL)) {
499 case GADGETFS_FIFO_STATUS:
500 status = usb_ep_fifo_status (data->ep);
502 case GADGETFS_FIFO_FLUSH:
503 usb_ep_fifo_flush (data->ep);
505 case GADGETFS_CLEAR_HALT:
506 status = usb_ep_clear_halt (data->ep);
513 spin_unlock_irq (&data->dev->lock);
514 mutex_unlock(&data->lock);
518 /*----------------------------------------------------------------------*/
520 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
523 struct usb_request *req;
524 struct ep_data *epdata;
526 const struct iovec *iv;
527 unsigned long nr_segs;
531 static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
533 struct kiocb_priv *priv = iocb->private;
534 struct ep_data *epdata;
538 epdata = priv->epdata;
539 // spin_lock(&epdata->dev->lock);
540 kiocbSetCancelled(iocb);
541 if (likely(epdata && epdata->ep && priv->req))
542 value = usb_ep_dequeue (epdata->ep, priv->req);
545 // spin_unlock(&epdata->dev->lock);
552 static ssize_t ep_aio_read_retry(struct kiocb *iocb)
554 struct kiocb_priv *priv = iocb->private;
559 /* we "retry" to get the right mm context for this: */
561 /* copy stuff into user buffers */
562 total = priv->actual;
565 for (i=0; i < priv->nr_segs; i++) {
566 ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
568 if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
585 static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
587 struct kiocb *iocb = req->context;
588 struct kiocb_priv *priv = iocb->private;
589 struct ep_data *epdata = priv->epdata;
591 /* lock against disconnect (and ideally, cancel) */
592 spin_lock(&epdata->dev->lock);
596 /* if this was a write or a read returning no data then we
597 * don't need to copy anything to userspace, so we can
598 * complete the aio request immediately.
600 if (priv->iv == NULL || unlikely(req->actual == 0)) {
603 iocb->private = NULL;
604 /* aio_complete() reports bytes-transferred _and_ faults */
605 aio_complete(iocb, req->actual ? req->actual : req->status,
608 /* retry() won't report both; so we hide some faults */
609 if (unlikely(0 != req->status))
610 DBG(epdata->dev, "%s fault %d len %d\n",
611 ep->name, req->status, req->actual);
613 priv->buf = req->buf;
614 priv->actual = req->actual;
617 spin_unlock(&epdata->dev->lock);
619 usb_ep_free_request(ep, req);
628 struct ep_data *epdata,
629 const struct iovec *iv,
630 unsigned long nr_segs
633 struct kiocb_priv *priv;
634 struct usb_request *req;
637 priv = kmalloc(sizeof *priv, GFP_KERNEL);
644 iocb->private = priv;
646 priv->nr_segs = nr_segs;
648 value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
649 if (unlikely(value < 0)) {
654 iocb->ki_cancel = ep_aio_cancel;
656 priv->epdata = epdata;
659 /* each kiocb is coupled to one usb_request, but we can't
660 * allocate or submit those if the host disconnected.
662 spin_lock_irq(&epdata->dev->lock);
663 if (likely(epdata->ep)) {
664 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
669 req->complete = ep_aio_complete;
671 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
672 if (unlikely(0 != value))
673 usb_ep_free_request(epdata->ep, req);
678 spin_unlock_irq(&epdata->dev->lock);
680 mutex_unlock(&epdata->lock);
682 if (unlikely(value)) {
686 value = (iv ? -EIOCBRETRY : -EIOCBQUEUED);
691 ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
692 unsigned long nr_segs, loff_t o)
694 struct ep_data *epdata = iocb->ki_filp->private_data;
697 if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
700 buf = kmalloc(iocb->ki_left, GFP_KERNEL);
704 iocb->ki_retry = ep_aio_read_retry;
705 return ep_aio_rwtail(iocb, buf, iocb->ki_left, epdata, iov, nr_segs);
709 ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
710 unsigned long nr_segs, loff_t o)
712 struct ep_data *epdata = iocb->ki_filp->private_data;
717 if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
720 buf = kmalloc(iocb->ki_left, GFP_KERNEL);
724 for (i=0; i < nr_segs; i++) {
725 if (unlikely(copy_from_user(&buf[len], iov[i].iov_base,
726 iov[i].iov_len) != 0)) {
730 len += iov[i].iov_len;
732 return ep_aio_rwtail(iocb, buf, len, epdata, NULL, 0);
735 /*----------------------------------------------------------------------*/
737 /* used after endpoint configuration */
738 static const struct file_operations ep_io_operations = {
739 .owner = THIS_MODULE,
744 .unlocked_ioctl = ep_ioctl,
745 .release = ep_release,
747 .aio_read = ep_aio_read,
748 .aio_write = ep_aio_write,
751 /* ENDPOINT INITIALIZATION
753 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
754 * status = write (fd, descriptors, sizeof descriptors)
756 * That write establishes the endpoint configuration, configuring
757 * the controller to process bulk, interrupt, or isochronous transfers
758 * at the right maxpacket size, and so on.
760 * The descriptors are message type 1, identified by a host order u32
761 * at the beginning of what's written. Descriptor order is: full/low
762 * speed descriptor, then optional high speed descriptor.
765 ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
767 struct ep_data *data = fd->private_data;
770 int value, length = len;
772 value = mutex_lock_interruptible(&data->lock);
776 if (data->state != STATE_EP_READY) {
782 if (len < USB_DT_ENDPOINT_SIZE + 4)
785 /* we might need to change message format someday */
786 if (copy_from_user (&tag, buf, 4)) {
790 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
796 /* NOTE: audio endpoint extensions not accepted here;
797 * just don't include the extra bytes.
800 /* full/low speed descriptor, then high speed */
801 if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
804 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
805 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
807 if (len != USB_DT_ENDPOINT_SIZE) {
808 if (len != 2 * USB_DT_ENDPOINT_SIZE)
810 if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
811 USB_DT_ENDPOINT_SIZE)) {
814 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
815 || data->hs_desc.bDescriptorType
816 != USB_DT_ENDPOINT) {
817 DBG(data->dev, "config %s, bad hs length or type\n",
823 spin_lock_irq (&data->dev->lock);
824 if (data->dev->state == STATE_DEV_UNBOUND) {
827 } else if ((ep = data->ep) == NULL) {
831 switch (data->dev->gadget->speed) {
834 value = usb_ep_enable (ep, &data->desc);
836 data->state = STATE_EP_ENABLED;
838 #ifdef CONFIG_USB_GADGET_DUALSPEED
840 /* fails if caller didn't provide that descriptor... */
841 value = usb_ep_enable (ep, &data->hs_desc);
843 data->state = STATE_EP_ENABLED;
847 DBG(data->dev, "unconnected, %s init abandoned\n",
852 fd->f_op = &ep_io_operations;
856 spin_unlock_irq (&data->dev->lock);
859 data->desc.bDescriptorType = 0;
860 data->hs_desc.bDescriptorType = 0;
862 mutex_unlock(&data->lock);
873 ep_open (struct inode *inode, struct file *fd)
875 struct ep_data *data = inode->i_private;
878 if (mutex_lock_interruptible(&data->lock) != 0)
880 spin_lock_irq (&data->dev->lock);
881 if (data->dev->state == STATE_DEV_UNBOUND)
883 else if (data->state == STATE_EP_DISABLED) {
885 data->state = STATE_EP_READY;
887 fd->private_data = data;
888 VDEBUG (data->dev, "%s ready\n", data->name);
890 DBG (data->dev, "%s state %d\n",
891 data->name, data->state);
892 spin_unlock_irq (&data->dev->lock);
893 mutex_unlock(&data->lock);
897 /* used before endpoint configuration */
898 static const struct file_operations ep_config_operations = {
899 .owner = THIS_MODULE,
904 .release = ep_release,
907 /*----------------------------------------------------------------------*/
909 /* EP0 IMPLEMENTATION can be partly in userspace.
911 * Drivers that use this facility receive various events, including
912 * control requests the kernel doesn't handle. Drivers that don't
913 * use this facility may be too simple-minded for real applications.
916 static inline void ep0_readable (struct dev_data *dev)
918 wake_up (&dev->wait);
919 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
922 static void clean_req (struct usb_ep *ep, struct usb_request *req)
924 struct dev_data *dev = ep->driver_data;
926 if (req->buf != dev->rbuf) {
928 req->buf = dev->rbuf;
929 req->dma = DMA_ADDR_INVALID;
931 req->complete = epio_complete;
932 dev->setup_out_ready = 0;
935 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
937 struct dev_data *dev = ep->driver_data;
941 /* for control OUT, data must still get to userspace */
942 spin_lock_irqsave(&dev->lock, flags);
943 if (!dev->setup_in) {
944 dev->setup_out_error = (req->status != 0);
945 if (!dev->setup_out_error)
947 dev->setup_out_ready = 1;
951 /* clean up as appropriate */
952 if (free && req->buf != &dev->rbuf)
954 req->complete = epio_complete;
955 spin_unlock_irqrestore(&dev->lock, flags);
958 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
960 struct dev_data *dev = ep->driver_data;
962 if (dev->setup_out_ready) {
963 DBG (dev, "ep0 request busy!\n");
966 if (len > sizeof (dev->rbuf))
967 req->buf = kmalloc(len, GFP_ATOMIC);
968 if (req->buf == NULL) {
969 req->buf = dev->rbuf;
972 req->complete = ep0_complete;
979 ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
981 struct dev_data *dev = fd->private_data;
983 enum ep0_state state;
985 spin_lock_irq (&dev->lock);
987 /* report fd mode change before acting on it */
988 if (dev->setup_abort) {
989 dev->setup_abort = 0;
994 /* control DATA stage */
995 if ((state = dev->state) == STATE_DEV_SETUP) {
997 if (dev->setup_in) { /* stall IN */
998 VDEBUG(dev, "ep0in stall\n");
999 (void) usb_ep_set_halt (dev->gadget->ep0);
1001 dev->state = STATE_DEV_CONNECTED;
1003 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
1004 struct usb_ep *ep = dev->gadget->ep0;
1005 struct usb_request *req = dev->req;
1007 if ((retval = setup_req (ep, req, 0)) == 0)
1008 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
1009 dev->state = STATE_DEV_CONNECTED;
1011 /* assume that was SET_CONFIGURATION */
1012 if (dev->current_config) {
1015 if (gadget_is_dualspeed(dev->gadget)
1016 && (dev->gadget->speed
1018 power = dev->hs_config->bMaxPower;
1020 power = dev->config->bMaxPower;
1021 usb_gadget_vbus_draw(dev->gadget, 2 * power);
1024 } else { /* collect OUT data */
1025 if ((fd->f_flags & O_NONBLOCK) != 0
1026 && !dev->setup_out_ready) {
1030 spin_unlock_irq (&dev->lock);
1031 retval = wait_event_interruptible (dev->wait,
1032 dev->setup_out_ready != 0);
1034 /* FIXME state could change from under us */
1035 spin_lock_irq (&dev->lock);
1039 if (dev->state != STATE_DEV_SETUP) {
1040 retval = -ECANCELED;
1043 dev->state = STATE_DEV_CONNECTED;
1045 if (dev->setup_out_error)
1048 len = min (len, (size_t)dev->req->actual);
1049 // FIXME don't call this with the spinlock held ...
1050 if (copy_to_user (buf, dev->req->buf, len))
1052 clean_req (dev->gadget->ep0, dev->req);
1053 /* NOTE userspace can't yet choose to stall */
1059 /* else normal: return event data */
1060 if (len < sizeof dev->event [0]) {
1064 len -= len % sizeof (struct usb_gadgetfs_event);
1065 dev->usermode_setup = 1;
1068 /* return queued events right away */
1069 if (dev->ev_next != 0) {
1072 n = len / sizeof (struct usb_gadgetfs_event);
1073 if (dev->ev_next < n)
1076 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1077 for (i = 0; i < n; i++) {
1078 if (dev->event [i].type == GADGETFS_SETUP) {
1079 dev->state = STATE_DEV_SETUP;
1084 spin_unlock_irq (&dev->lock);
1085 len = n * sizeof (struct usb_gadgetfs_event);
1086 if (copy_to_user (buf, &dev->event, len))
1091 /* NOTE this doesn't guard against broken drivers;
1092 * concurrent ep0 readers may lose events.
1094 spin_lock_irq (&dev->lock);
1095 if (dev->ev_next > n) {
1096 memmove(&dev->event[0], &dev->event[n],
1097 sizeof (struct usb_gadgetfs_event)
1098 * (dev->ev_next - n));
1101 spin_unlock_irq (&dev->lock);
1105 if (fd->f_flags & O_NONBLOCK) {
1112 DBG (dev, "fail %s, state %d\n", __func__, state);
1115 case STATE_DEV_UNCONNECTED:
1116 case STATE_DEV_CONNECTED:
1117 spin_unlock_irq (&dev->lock);
1118 DBG (dev, "%s wait\n", __func__);
1120 /* wait for events */
1121 retval = wait_event_interruptible (dev->wait,
1125 spin_lock_irq (&dev->lock);
1130 spin_unlock_irq (&dev->lock);
1134 static struct usb_gadgetfs_event *
1135 next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1137 struct usb_gadgetfs_event *event;
1141 /* these events purge the queue */
1142 case GADGETFS_DISCONNECT:
1143 if (dev->state == STATE_DEV_SETUP)
1144 dev->setup_abort = 1;
1146 case GADGETFS_CONNECT:
1149 case GADGETFS_SETUP: /* previous request timed out */
1150 case GADGETFS_SUSPEND: /* same effect */
1151 /* these events can't be repeated */
1152 for (i = 0; i != dev->ev_next; i++) {
1153 if (dev->event [i].type != type)
1155 DBG(dev, "discard old event[%d] %d\n", i, type);
1157 if (i == dev->ev_next)
1159 /* indices start at zero, for simplicity */
1160 memmove (&dev->event [i], &dev->event [i + 1],
1161 sizeof (struct usb_gadgetfs_event)
1162 * (dev->ev_next - i));
1168 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
1169 event = &dev->event [dev->ev_next++];
1170 BUG_ON (dev->ev_next > N_EVENT);
1171 memset (event, 0, sizeof *event);
1177 ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1179 struct dev_data *dev = fd->private_data;
1180 ssize_t retval = -ESRCH;
1182 spin_lock_irq (&dev->lock);
1184 /* report fd mode change before acting on it */
1185 if (dev->setup_abort) {
1186 dev->setup_abort = 0;
1189 /* data and/or status stage for control request */
1190 } else if (dev->state == STATE_DEV_SETUP) {
1192 /* IN DATA+STATUS caller makes len <= wLength */
1193 if (dev->setup_in) {
1194 retval = setup_req (dev->gadget->ep0, dev->req, len);
1196 dev->state = STATE_DEV_CONNECTED;
1197 spin_unlock_irq (&dev->lock);
1198 if (copy_from_user (dev->req->buf, buf, len))
1201 if (len < dev->setup_wLength)
1203 retval = usb_ep_queue (
1204 dev->gadget->ep0, dev->req,
1208 spin_lock_irq (&dev->lock);
1209 clean_req (dev->gadget->ep0, dev->req);
1210 spin_unlock_irq (&dev->lock);
1217 /* can stall some OUT transfers */
1218 } else if (dev->setup_can_stall) {
1219 VDEBUG(dev, "ep0out stall\n");
1220 (void) usb_ep_set_halt (dev->gadget->ep0);
1222 dev->state = STATE_DEV_CONNECTED;
1224 DBG(dev, "bogus ep0out stall!\n");
1227 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
1229 spin_unlock_irq (&dev->lock);
1234 ep0_fasync (int f, struct file *fd, int on)
1236 struct dev_data *dev = fd->private_data;
1237 // caller must F_SETOWN before signal delivery happens
1238 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
1239 return fasync_helper (f, fd, on, &dev->fasync);
1242 static struct usb_gadget_driver gadgetfs_driver;
1245 dev_release (struct inode *inode, struct file *fd)
1247 struct dev_data *dev = fd->private_data;
1249 /* closing ep0 === shutdown all */
1251 usb_gadget_unregister_driver (&gadgetfs_driver);
1253 /* at this point "good" hardware has disconnected the
1254 * device from USB; the host won't see it any more.
1255 * alternatively, all host requests will time out.
1262 /* other endpoints were all decoupled from this device */
1263 spin_lock_irq(&dev->lock);
1264 dev->state = STATE_DEV_DISABLED;
1265 spin_unlock_irq(&dev->lock);
1270 ep0_poll (struct file *fd, poll_table *wait)
1272 struct dev_data *dev = fd->private_data;
1275 poll_wait(fd, &dev->wait, wait);
1277 spin_lock_irq (&dev->lock);
1279 /* report fd mode change before acting on it */
1280 if (dev->setup_abort) {
1281 dev->setup_abort = 0;
1286 if (dev->state == STATE_DEV_SETUP) {
1287 if (dev->setup_in || dev->setup_can_stall)
1290 if (dev->ev_next != 0)
1294 spin_unlock_irq(&dev->lock);
1298 static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1300 struct dev_data *dev = fd->private_data;
1301 struct usb_gadget *gadget = dev->gadget;
1304 if (gadget->ops->ioctl)
1305 ret = gadget->ops->ioctl (gadget, code, value);
1310 /* used after device configuration */
1311 static const struct file_operations ep0_io_operations = {
1312 .owner = THIS_MODULE,
1313 .llseek = no_llseek,
1317 .fasync = ep0_fasync,
1319 .unlocked_ioctl = dev_ioctl,
1320 .release = dev_release,
1323 /*----------------------------------------------------------------------*/
1325 /* The in-kernel gadget driver handles most ep0 issues, in particular
1326 * enumerating the single configuration (as provided from user space).
1328 * Unrecognized ep0 requests may be handled in user space.
1331 #ifdef CONFIG_USB_GADGET_DUALSPEED
1332 static void make_qualifier (struct dev_data *dev)
1334 struct usb_qualifier_descriptor qual;
1335 struct usb_device_descriptor *desc;
1337 qual.bLength = sizeof qual;
1338 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1339 qual.bcdUSB = cpu_to_le16 (0x0200);
1342 qual.bDeviceClass = desc->bDeviceClass;
1343 qual.bDeviceSubClass = desc->bDeviceSubClass;
1344 qual.bDeviceProtocol = desc->bDeviceProtocol;
1346 /* assumes ep0 uses the same value for both speeds ... */
1347 qual.bMaxPacketSize0 = desc->bMaxPacketSize0;
1349 qual.bNumConfigurations = 1;
1352 memcpy (dev->rbuf, &qual, sizeof qual);
1357 config_buf (struct dev_data *dev, u8 type, unsigned index)
1362 /* only one configuration */
1366 if (gadget_is_dualspeed(dev->gadget)) {
1367 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1368 if (type == USB_DT_OTHER_SPEED_CONFIG)
1372 dev->req->buf = dev->hs_config;
1373 len = le16_to_cpu(dev->hs_config->wTotalLength);
1375 dev->req->buf = dev->config;
1376 len = le16_to_cpu(dev->config->wTotalLength);
1378 ((u8 *)dev->req->buf) [1] = type;
1383 gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1385 struct dev_data *dev = get_gadget_data (gadget);
1386 struct usb_request *req = dev->req;
1387 int value = -EOPNOTSUPP;
1388 struct usb_gadgetfs_event *event;
1389 u16 w_value = le16_to_cpu(ctrl->wValue);
1390 u16 w_length = le16_to_cpu(ctrl->wLength);
1392 spin_lock (&dev->lock);
1393 dev->setup_abort = 0;
1394 if (dev->state == STATE_DEV_UNCONNECTED) {
1395 if (gadget_is_dualspeed(gadget)
1396 && gadget->speed == USB_SPEED_HIGH
1397 && dev->hs_config == NULL) {
1398 spin_unlock(&dev->lock);
1399 ERROR (dev, "no high speed config??\n");
1403 dev->state = STATE_DEV_CONNECTED;
1404 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1406 INFO (dev, "connected\n");
1407 event = next_event (dev, GADGETFS_CONNECT);
1408 event->u.speed = gadget->speed;
1411 /* host may have given up waiting for response. we can miss control
1412 * requests handled lower down (device/endpoint status and features);
1413 * then ep0_{read,write} will report the wrong status. controller
1414 * driver will have aborted pending i/o.
1416 } else if (dev->state == STATE_DEV_SETUP)
1417 dev->setup_abort = 1;
1419 req->buf = dev->rbuf;
1420 req->dma = DMA_ADDR_INVALID;
1421 req->context = NULL;
1422 value = -EOPNOTSUPP;
1423 switch (ctrl->bRequest) {
1425 case USB_REQ_GET_DESCRIPTOR:
1426 if (ctrl->bRequestType != USB_DIR_IN)
1428 switch (w_value >> 8) {
1431 value = min (w_length, (u16) sizeof *dev->dev);
1432 req->buf = dev->dev;
1434 #ifdef CONFIG_USB_GADGET_DUALSPEED
1435 case USB_DT_DEVICE_QUALIFIER:
1436 if (!dev->hs_config)
1438 value = min (w_length, (u16)
1439 sizeof (struct usb_qualifier_descriptor));
1440 make_qualifier (dev);
1442 case USB_DT_OTHER_SPEED_CONFIG:
1446 value = config_buf (dev,
1450 value = min (w_length, (u16) value);
1455 default: // all others are errors
1460 /* currently one config, two speeds */
1461 case USB_REQ_SET_CONFIGURATION:
1462 if (ctrl->bRequestType != 0)
1464 if (0 == (u8) w_value) {
1466 dev->current_config = 0;
1467 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1468 // user mode expected to disable endpoints
1472 if (gadget_is_dualspeed(gadget)
1473 && gadget->speed == USB_SPEED_HIGH) {
1474 config = dev->hs_config->bConfigurationValue;
1475 power = dev->hs_config->bMaxPower;
1477 config = dev->config->bConfigurationValue;
1478 power = dev->config->bMaxPower;
1481 if (config == (u8) w_value) {
1483 dev->current_config = config;
1484 usb_gadget_vbus_draw(gadget, 2 * power);
1488 /* report SET_CONFIGURATION like any other control request,
1489 * except that usermode may not stall this. the next
1490 * request mustn't be allowed start until this finishes:
1491 * endpoints and threads set up, etc.
1493 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1494 * has bad/racey automagic that prevents synchronizing here.
1495 * even kernel mode drivers often miss them.
1498 INFO (dev, "configuration #%d\n", dev->current_config);
1499 if (dev->usermode_setup) {
1500 dev->setup_can_stall = 0;
1506 #ifndef CONFIG_USB_GADGET_PXA25X
1507 /* PXA automagically handles this request too */
1508 case USB_REQ_GET_CONFIGURATION:
1509 if (ctrl->bRequestType != 0x80)
1511 *(u8 *)req->buf = dev->current_config;
1512 value = min (w_length, (u16) 1);
1518 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1519 dev->usermode_setup ? "delegate" : "fail",
1520 ctrl->bRequestType, ctrl->bRequest,
1521 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1523 /* if there's an ep0 reader, don't stall */
1524 if (dev->usermode_setup) {
1525 dev->setup_can_stall = 1;
1527 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1529 dev->setup_wLength = w_length;
1530 dev->setup_out_ready = 0;
1531 dev->setup_out_error = 0;
1534 /* read DATA stage for OUT right away */
1535 if (unlikely (!dev->setup_in && w_length)) {
1536 value = setup_req (gadget->ep0, dev->req,
1540 value = usb_ep_queue (gadget->ep0, dev->req,
1543 clean_req (gadget->ep0, dev->req);
1547 /* we can't currently stall these */
1548 dev->setup_can_stall = 0;
1551 /* state changes when reader collects event */
1552 event = next_event (dev, GADGETFS_SETUP);
1553 event->u.setup = *ctrl;
1555 spin_unlock (&dev->lock);
1560 /* proceed with data transfer and status phases? */
1561 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
1562 req->length = value;
1563 req->zero = value < w_length;
1564 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1566 DBG (dev, "ep_queue --> %d\n", value);
1571 /* device stalls when value < 0 */
1572 spin_unlock (&dev->lock);
1576 static void destroy_ep_files (struct dev_data *dev)
1578 struct list_head *entry, *tmp;
1580 DBG (dev, "%s %d\n", __func__, dev->state);
1582 /* dev->state must prevent interference */
1584 spin_lock_irq (&dev->lock);
1585 list_for_each_safe (entry, tmp, &dev->epfiles) {
1587 struct inode *parent;
1588 struct dentry *dentry;
1590 /* break link to FS */
1591 ep = list_entry (entry, struct ep_data, epfiles);
1592 list_del_init (&ep->epfiles);
1593 dentry = ep->dentry;
1595 parent = dentry->d_parent->d_inode;
1597 /* break link to controller */
1598 if (ep->state == STATE_EP_ENABLED)
1599 (void) usb_ep_disable (ep->ep);
1600 ep->state = STATE_EP_UNBOUND;
1601 usb_ep_free_request (ep->ep, ep->req);
1603 wake_up (&ep->wait);
1606 spin_unlock_irq (&dev->lock);
1608 /* break link to dcache */
1609 mutex_lock (&parent->i_mutex);
1612 mutex_unlock (&parent->i_mutex);
1614 /* fds may still be open */
1617 spin_unlock_irq (&dev->lock);
1621 static struct inode *
1622 gadgetfs_create_file (struct super_block *sb, char const *name,
1623 void *data, const struct file_operations *fops,
1624 struct dentry **dentry_p);
1626 static int activate_ep_files (struct dev_data *dev)
1629 struct ep_data *data;
1631 gadget_for_each_ep (ep, dev->gadget) {
1633 data = kzalloc(sizeof(*data), GFP_KERNEL);
1636 data->state = STATE_EP_DISABLED;
1637 mutex_init(&data->lock);
1638 init_waitqueue_head (&data->wait);
1640 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1641 atomic_set (&data->count, 1);
1646 ep->driver_data = data;
1648 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1652 data->inode = gadgetfs_create_file (dev->sb, data->name,
1653 data, &ep_config_operations,
1657 list_add_tail (&data->epfiles, &dev->epfiles);
1662 usb_ep_free_request (ep, data->req);
1667 DBG (dev, "%s enomem\n", __func__);
1668 destroy_ep_files (dev);
1673 gadgetfs_unbind (struct usb_gadget *gadget)
1675 struct dev_data *dev = get_gadget_data (gadget);
1677 DBG (dev, "%s\n", __func__);
1679 spin_lock_irq (&dev->lock);
1680 dev->state = STATE_DEV_UNBOUND;
1681 spin_unlock_irq (&dev->lock);
1683 destroy_ep_files (dev);
1684 gadget->ep0->driver_data = NULL;
1685 set_gadget_data (gadget, NULL);
1687 /* we've already been disconnected ... no i/o is active */
1689 usb_ep_free_request (gadget->ep0, dev->req);
1690 DBG (dev, "%s done\n", __func__);
1694 static struct dev_data *the_device;
1697 gadgetfs_bind (struct usb_gadget *gadget)
1699 struct dev_data *dev = the_device;
1703 if (0 != strcmp (CHIP, gadget->name)) {
1704 pr_err("%s expected %s controller not %s\n",
1705 shortname, CHIP, gadget->name);
1709 set_gadget_data (gadget, dev);
1710 dev->gadget = gadget;
1711 gadget->ep0->driver_data = dev;
1712 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1714 /* preallocate control response and buffer */
1715 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1718 dev->req->context = NULL;
1719 dev->req->complete = epio_complete;
1721 if (activate_ep_files (dev) < 0)
1724 INFO (dev, "bound to %s driver\n", gadget->name);
1725 spin_lock_irq(&dev->lock);
1726 dev->state = STATE_DEV_UNCONNECTED;
1727 spin_unlock_irq(&dev->lock);
1732 gadgetfs_unbind (gadget);
1737 gadgetfs_disconnect (struct usb_gadget *gadget)
1739 struct dev_data *dev = get_gadget_data (gadget);
1741 spin_lock (&dev->lock);
1742 if (dev->state == STATE_DEV_UNCONNECTED)
1744 dev->state = STATE_DEV_UNCONNECTED;
1746 INFO (dev, "disconnected\n");
1747 next_event (dev, GADGETFS_DISCONNECT);
1750 spin_unlock (&dev->lock);
1754 gadgetfs_suspend (struct usb_gadget *gadget)
1756 struct dev_data *dev = get_gadget_data (gadget);
1758 INFO (dev, "suspended from state %d\n", dev->state);
1759 spin_lock (&dev->lock);
1760 switch (dev->state) {
1761 case STATE_DEV_SETUP: // VERY odd... host died??
1762 case STATE_DEV_CONNECTED:
1763 case STATE_DEV_UNCONNECTED:
1764 next_event (dev, GADGETFS_SUSPEND);
1770 spin_unlock (&dev->lock);
1773 static struct usb_gadget_driver gadgetfs_driver = {
1774 #ifdef CONFIG_USB_GADGET_DUALSPEED
1775 .speed = USB_SPEED_HIGH,
1777 .speed = USB_SPEED_FULL,
1779 .function = (char *) driver_desc,
1780 .bind = gadgetfs_bind,
1781 .unbind = gadgetfs_unbind,
1782 .setup = gadgetfs_setup,
1783 .disconnect = gadgetfs_disconnect,
1784 .suspend = gadgetfs_suspend,
1787 .name = (char *) shortname,
1791 /*----------------------------------------------------------------------*/
1793 static void gadgetfs_nop(struct usb_gadget *arg) { }
1795 static int gadgetfs_probe (struct usb_gadget *gadget)
1797 CHIP = gadget->name;
1801 static struct usb_gadget_driver probe_driver = {
1802 .speed = USB_SPEED_HIGH,
1803 .bind = gadgetfs_probe,
1804 .unbind = gadgetfs_nop,
1805 .setup = (void *)gadgetfs_nop,
1806 .disconnect = gadgetfs_nop,
1813 /* DEVICE INITIALIZATION
1815 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1816 * status = write (fd, descriptors, sizeof descriptors)
1818 * That write establishes the device configuration, so the kernel can
1819 * bind to the controller ... guaranteeing it can handle enumeration
1820 * at all necessary speeds. Descriptor order is:
1822 * . message tag (u32, host order) ... for now, must be zero; it
1823 * would change to support features like multi-config devices
1824 * . full/low speed config ... all wTotalLength bytes (with interface,
1825 * class, altsetting, endpoint, and other descriptors)
1826 * . high speed config ... all descriptors, for high speed operation;
1827 * this one's optional except for high-speed hardware
1828 * . device descriptor
1830 * Endpoints are not yet enabled. Drivers must wait until device
1831 * configuration and interface altsetting changes create
1832 * the need to configure (or unconfigure) them.
1834 * After initialization, the device stays active for as long as that
1835 * $CHIP file is open. Events must then be read from that descriptor,
1836 * such as configuration notifications.
1839 static int is_valid_config (struct usb_config_descriptor *config)
1841 return config->bDescriptorType == USB_DT_CONFIG
1842 && config->bLength == USB_DT_CONFIG_SIZE
1843 && config->bConfigurationValue != 0
1844 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1845 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1846 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1847 /* FIXME check lengths: walk to end */
1851 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1853 struct dev_data *dev = fd->private_data;
1854 ssize_t value = len, length = len;
1859 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1862 /* we might need to change message format someday */
1863 if (copy_from_user (&tag, buf, 4))
1870 kbuf = memdup_user(buf, length);
1872 return PTR_ERR(kbuf);
1874 spin_lock_irq (&dev->lock);
1880 /* full or low speed config */
1881 dev->config = (void *) kbuf;
1882 total = le16_to_cpu(dev->config->wTotalLength);
1883 if (!is_valid_config (dev->config) || total >= length)
1888 /* optional high speed config */
1889 if (kbuf [1] == USB_DT_CONFIG) {
1890 dev->hs_config = (void *) kbuf;
1891 total = le16_to_cpu(dev->hs_config->wTotalLength);
1892 if (!is_valid_config (dev->hs_config) || total >= length)
1898 /* could support multiple configs, using another encoding! */
1900 /* device descriptor (tweaked for paranoia) */
1901 if (length != USB_DT_DEVICE_SIZE)
1903 dev->dev = (void *)kbuf;
1904 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1905 || dev->dev->bDescriptorType != USB_DT_DEVICE
1906 || dev->dev->bNumConfigurations != 1)
1908 dev->dev->bNumConfigurations = 1;
1909 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
1911 /* triggers gadgetfs_bind(); then we can enumerate. */
1912 spin_unlock_irq (&dev->lock);
1913 value = usb_gadget_register_driver (&gadgetfs_driver);
1918 /* at this point "good" hardware has for the first time
1919 * let the USB the host see us. alternatively, if users
1920 * unplug/replug that will clear all the error state.
1922 * note: everything running before here was guaranteed
1923 * to choke driver model style diagnostics. from here
1924 * on, they can work ... except in cleanup paths that
1925 * kick in after the ep0 descriptor is closed.
1927 fd->f_op = &ep0_io_operations;
1933 spin_unlock_irq (&dev->lock);
1934 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
1941 dev_open (struct inode *inode, struct file *fd)
1943 struct dev_data *dev = inode->i_private;
1946 spin_lock_irq(&dev->lock);
1947 if (dev->state == STATE_DEV_DISABLED) {
1949 dev->state = STATE_DEV_OPENED;
1950 fd->private_data = dev;
1954 spin_unlock_irq(&dev->lock);
1958 static const struct file_operations dev_init_operations = {
1959 .owner = THIS_MODULE,
1960 .llseek = no_llseek,
1963 .write = dev_config,
1964 .fasync = ep0_fasync,
1965 .unlocked_ioctl = dev_ioctl,
1966 .release = dev_release,
1969 /*----------------------------------------------------------------------*/
1971 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1973 * Mounting the filesystem creates a controller file, used first for
1974 * device configuration then later for event monitoring.
1978 /* FIXME PAM etc could set this security policy without mount options
1979 * if epfiles inherited ownership and permissons from ep0 ...
1982 static unsigned default_uid;
1983 static unsigned default_gid;
1984 static unsigned default_perm = S_IRUSR | S_IWUSR;
1986 module_param (default_uid, uint, 0644);
1987 module_param (default_gid, uint, 0644);
1988 module_param (default_perm, uint, 0644);
1991 static struct inode *
1992 gadgetfs_make_inode (struct super_block *sb,
1993 void *data, const struct file_operations *fops,
1996 struct inode *inode = new_inode (sb);
1999 inode->i_mode = mode;
2000 inode->i_uid = default_uid;
2001 inode->i_gid = default_gid;
2002 inode->i_atime = inode->i_mtime = inode->i_ctime
2004 inode->i_private = data;
2005 inode->i_fop = fops;
2010 /* creates in fs root directory, so non-renamable and non-linkable.
2011 * so inode and dentry are paired, until device reconfig.
2013 static struct inode *
2014 gadgetfs_create_file (struct super_block *sb, char const *name,
2015 void *data, const struct file_operations *fops,
2016 struct dentry **dentry_p)
2018 struct dentry *dentry;
2019 struct inode *inode;
2021 dentry = d_alloc_name(sb->s_root, name);
2025 inode = gadgetfs_make_inode (sb, data, fops,
2026 S_IFREG | (default_perm & S_IRWXUGO));
2031 d_add (dentry, inode);
2036 static const struct super_operations gadget_fs_operations = {
2037 .statfs = simple_statfs,
2038 .drop_inode = generic_delete_inode,
2042 gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
2044 struct inode *inode;
2046 struct dev_data *dev;
2051 /* fake probe to determine $CHIP */
2052 (void) usb_gadget_register_driver (&probe_driver);
2057 sb->s_blocksize = PAGE_CACHE_SIZE;
2058 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2059 sb->s_magic = GADGETFS_MAGIC;
2060 sb->s_op = &gadget_fs_operations;
2061 sb->s_time_gran = 1;
2064 inode = gadgetfs_make_inode (sb,
2065 NULL, &simple_dir_operations,
2066 S_IFDIR | S_IRUGO | S_IXUGO);
2069 inode->i_op = &simple_dir_inode_operations;
2070 if (!(d = d_alloc_root (inode)))
2074 /* the ep0 file is named after the controller we expect;
2075 * user mode code can use it for sanity checks, like we do.
2082 if (!gadgetfs_create_file (sb, CHIP,
2083 dev, &dev_init_operations,
2087 /* other endpoint files are available after hardware setup,
2088 * from binding to a controller.
2103 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2105 gadgetfs_get_sb (struct file_system_type *t, int flags,
2106 const char *path, void *opts, struct vfsmount *mnt)
2108 return get_sb_single (t, flags, opts, gadgetfs_fill_super, mnt);
2112 gadgetfs_kill_sb (struct super_block *sb)
2114 kill_litter_super (sb);
2116 put_dev (the_device);
2121 /*----------------------------------------------------------------------*/
2123 static struct file_system_type gadgetfs_type = {
2124 .owner = THIS_MODULE,
2126 .get_sb = gadgetfs_get_sb,
2127 .kill_sb = gadgetfs_kill_sb,
2130 /*----------------------------------------------------------------------*/
2132 static int __init init (void)
2136 status = register_filesystem (&gadgetfs_type);
2138 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2139 shortname, driver_desc);
2144 static void __exit cleanup (void)
2146 pr_debug ("unregister %s\n", shortname);
2147 unregister_filesystem (&gadgetfs_type);
2149 module_exit (cleanup);