2 * inode.c -- user mode filesystem api for usb gadget controllers
4 * Copyright (C) 2003 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 DEBUG /* data to help fault diagnosis */
24 // #define VERBOSE /* extra debug messages (success too) */
26 #include <linux/init.h>
27 #include <linux/module.h>
29 #include <linux/pagemap.h>
30 #include <linux/uts.h>
31 #include <linux/wait.h>
32 #include <linux/compiler.h>
33 #include <asm/uaccess.h>
34 #include <linux/slab.h>
36 #include <linux/device.h>
37 #include <linux/moduleparam.h>
39 #include <linux/usb_gadgetfs.h>
40 #include <linux/usb_gadget.h>
44 * The gadgetfs API maps each endpoint to a file descriptor so that you
45 * can use standard synchronous read/write calls for I/O. There's some
46 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
47 * drivers show how this works in practice. You can also use AIO to
48 * eliminate I/O gaps between requests, to help when streaming data.
50 * Key parts that must be USB-specific are protocols defining how the
51 * read/write operations relate to the hardware state machines. There
52 * are two types of files. One type is for the device, implementing ep0.
53 * The other type is for each IN or OUT endpoint. In both cases, the
54 * user mode driver must configure the hardware before using it.
56 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
57 * (by writing configuration and device descriptors). Afterwards it
58 * may serve as a source of device events, used to handle all control
59 * requests other than basic enumeration.
61 * - Then either immediately, or after a SET_CONFIGURATION control request,
62 * ep_config() is called when each /dev/gadget/ep* file is configured
63 * (by writing endpoint descriptors). Afterwards these files are used
64 * to write() IN data or to read() OUT data. To halt the endpoint, a
65 * "wrong direction" request is issued (like reading an IN endpoint).
67 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
68 * not possible on all hardware. For example, precise fault handling with
69 * respect to data left in endpoint fifos after aborted operations; or
70 * selective clearing of endpoint halts, to implement SET_INTERFACE.
73 #define DRIVER_DESC "USB Gadget filesystem"
74 #define DRIVER_VERSION "18 Nov 2003"
76 static const char driver_desc [] = DRIVER_DESC;
77 static const char shortname [] = "gadgetfs";
79 MODULE_DESCRIPTION (DRIVER_DESC);
80 MODULE_AUTHOR ("David Brownell");
81 MODULE_LICENSE ("GPL");
84 /*----------------------------------------------------------------------*/
86 #define GADGETFS_MAGIC 0xaee71ee7
87 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
89 /* /dev/gadget/$CHIP represents ep0 and the whole device */
91 /* DISBLED is the initial state.
93 STATE_DEV_DISABLED = 0,
95 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
96 * ep0/device i/o modes and binding to the controller. Driver
97 * must always write descriptors to initialize the device, then
98 * the device becomes UNCONNECTED until enumeration.
102 /* From then on, ep0 fd is in either of two basic modes:
103 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
104 * - SETUP: read/write will transfer control data and succeed;
105 * or if "wrong direction", performs protocol stall
111 /* UNBOUND means the driver closed ep0, so the device won't be
112 * accessible again (DEV_DISABLED) until all fds are closed.
117 /* enough for the whole queue: most events invalidate others */
123 enum ep0_state state;
124 struct usb_gadgetfs_event event [N_EVENT];
126 struct fasync_struct *fasync;
129 /* drivers reading ep0 MUST handle control requests (SETUP)
130 * reported that way; else the host will time out.
132 unsigned usermode_setup : 1,
139 /* the rest is basically write-once */
140 struct usb_config_descriptor *config, *hs_config;
141 struct usb_device_descriptor *dev;
142 struct usb_request *req;
143 struct usb_gadget *gadget;
144 struct list_head epfiles;
146 wait_queue_head_t wait;
147 struct super_block *sb;
148 struct dentry *dentry;
150 /* except this scratch i/o buffer for ep0 */
154 static inline void get_dev (struct dev_data *data)
156 atomic_inc (&data->count);
159 static void put_dev (struct dev_data *data)
161 if (likely (!atomic_dec_and_test (&data->count)))
163 /* needs no more cleanup */
164 BUG_ON (waitqueue_active (&data->wait));
168 static struct dev_data *dev_new (void)
170 struct dev_data *dev;
172 dev = kmalloc (sizeof *dev, GFP_KERNEL);
175 memset (dev, 0, sizeof *dev);
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,
190 STATE_EP_DEFER_ENABLE,
196 struct semaphore lock;
199 struct dev_data *dev;
200 /* must hold dev->lock before accessing ep or req */
202 struct usb_request *req;
205 struct usb_endpoint_descriptor desc, hs_desc;
206 struct list_head epfiles;
207 wait_queue_head_t wait;
208 struct dentry *dentry;
212 static inline void get_ep (struct ep_data *data)
214 atomic_inc (&data->count);
217 static void put_ep (struct ep_data *data)
219 if (likely (!atomic_dec_and_test (&data->count)))
222 /* needs no more cleanup */
223 BUG_ON (!list_empty (&data->epfiles));
224 BUG_ON (waitqueue_active (&data->wait));
225 BUG_ON (down_trylock (&data->lock) != 0);
229 /*----------------------------------------------------------------------*/
231 /* most "how to use the hardware" policy choices are in userspace:
232 * mapping endpoint roles the driver needs to the capabilities that
233 * the usb controller exposes.
236 #ifdef CONFIG_USB_GADGET_DUMMY_HCD
237 /* act (mostly) like a net2280 */
238 #define CONFIG_USB_GADGET_NET2280
241 #ifdef CONFIG_USB_GADGET_NET2280
242 #define CHIP "net2280"
246 #ifdef CONFIG_USB_GADGET_PXA2XX
247 #define CHIP "pxa2xx_udc"
248 /* earlier hardware doesn't have UDCCFR, races set_{config,interface} */
249 #warning works best with pxa255 or newer
252 #ifdef CONFIG_USB_GADGET_GOKU
253 #define CHIP "goku_udc"
256 #ifdef CONFIG_USB_GADGET_OMAP
257 #define CHIP "omap_udc"
260 #ifdef CONFIG_USB_GADGET_SA1100
261 #define CHIP "sa1100"
264 /*----------------------------------------------------------------------*/
266 /* NOTE: don't use dev_printk calls before binding to the gadget
267 * at the end of ep0 configuration, or after unbind.
270 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
271 #define xprintk(d,level,fmt,args...) \
272 printk(level "%s: " fmt , shortname , ## args)
275 #define DBG(dev,fmt,args...) \
276 xprintk(dev , KERN_DEBUG , fmt , ## args)
278 #define DBG(dev,fmt,args...) \
285 #define VDEBUG(dev,fmt,args...) \
289 #define ERROR(dev,fmt,args...) \
290 xprintk(dev , KERN_ERR , fmt , ## args)
291 #define WARN(dev,fmt,args...) \
292 xprintk(dev , KERN_WARNING , fmt , ## args)
293 #define INFO(dev,fmt,args...) \
294 xprintk(dev , KERN_INFO , fmt , ## args)
297 /*----------------------------------------------------------------------*/
299 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
301 * After opening, configure non-control endpoints. Then use normal
302 * stream read() and write() requests; and maybe ioctl() to get more
303 * precise FIFO status when recovering from cancelation.
306 static void epio_complete (struct usb_ep *ep, struct usb_request *req)
308 struct ep_data *epdata = ep->driver_data;
313 epdata->status = req->status;
315 epdata->status = req->actual;
316 complete ((struct completion *)req->context);
319 /* tasklock endpoint, returning when it's connected.
320 * still need dev->lock to use epdata->ep.
323 get_ready_ep (unsigned f_flags, struct ep_data *epdata)
327 if (f_flags & O_NONBLOCK) {
328 if (down_trylock (&epdata->lock) != 0)
330 if (epdata->state != STATE_EP_ENABLED) {
339 if ((val = down_interruptible (&epdata->lock)) < 0)
342 switch (epdata->state) {
343 case STATE_EP_ENABLED:
345 case STATE_EP_DEFER_ENABLE:
346 DBG (epdata->dev, "%s wait for host\n", epdata->name);
347 if ((val = wait_event_interruptible (epdata->wait,
348 epdata->state != STATE_EP_DEFER_ENABLE
349 || epdata->dev->state == STATE_DEV_UNBOUND
353 // case STATE_EP_DISABLED: /* "can't happen" */
354 // case STATE_EP_READY: /* "can't happen" */
355 default: /* error! */
356 pr_debug ("%s: ep %p not available, state %d\n",
357 shortname, epdata, epdata->state);
359 case STATE_EP_UNBOUND: /* clean disconnect */
368 ep_io (struct ep_data *epdata, void *buf, unsigned len)
370 DECLARE_COMPLETION (done);
373 spin_lock_irq (&epdata->dev->lock);
374 if (likely (epdata->ep != NULL)) {
375 struct usb_request *req = epdata->req;
377 req->context = &done;
378 req->complete = epio_complete;
381 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
384 spin_unlock_irq (&epdata->dev->lock);
386 if (likely (value == 0)) {
387 value = wait_event_interruptible (done.wait, done.done);
389 spin_lock_irq (&epdata->dev->lock);
390 if (likely (epdata->ep != NULL)) {
391 DBG (epdata->dev, "%s i/o interrupted\n",
393 usb_ep_dequeue (epdata->ep, epdata->req);
394 spin_unlock_irq (&epdata->dev->lock);
396 wait_event (done.wait, done.done);
397 if (epdata->status == -ECONNRESET)
398 epdata->status = -EINTR;
400 spin_unlock_irq (&epdata->dev->lock);
402 DBG (epdata->dev, "endpoint gone\n");
403 epdata->status = -ENODEV;
406 return epdata->status;
412 /* handle a synchronous OUT bulk/intr/iso transfer */
414 ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
416 struct ep_data *data = fd->private_data;
420 if ((value = get_ready_ep (fd->f_flags, data)) < 0)
423 /* halt any endpoint by doing a "wrong direction" i/o call */
424 if (data->desc.bEndpointAddress & USB_DIR_IN) {
425 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
426 == USB_ENDPOINT_XFER_ISOC)
428 DBG (data->dev, "%s halt\n", data->name);
429 spin_lock_irq (&data->dev->lock);
430 if (likely (data->ep != NULL))
431 usb_ep_set_halt (data->ep);
432 spin_unlock_irq (&data->dev->lock);
437 /* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */
440 kbuf = kmalloc (len, SLAB_KERNEL);
441 if (unlikely (!kbuf))
444 value = ep_io (data, kbuf, len);
445 VDEBUG (data->dev, "%s read %d OUT, status %d\n",
446 data->name, len, value);
447 if (value >= 0 && copy_to_user (buf, kbuf, value))
456 /* handle a synchronous IN bulk/intr/iso transfer */
458 ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
460 struct ep_data *data = fd->private_data;
464 if ((value = get_ready_ep (fd->f_flags, data)) < 0)
467 /* halt any endpoint by doing a "wrong direction" i/o call */
468 if (!(data->desc.bEndpointAddress & USB_DIR_IN)) {
469 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
470 == USB_ENDPOINT_XFER_ISOC)
472 DBG (data->dev, "%s halt\n", data->name);
473 spin_lock_irq (&data->dev->lock);
474 if (likely (data->ep != NULL))
475 usb_ep_set_halt (data->ep);
476 spin_unlock_irq (&data->dev->lock);
481 /* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */
484 kbuf = kmalloc (len, SLAB_KERNEL);
487 if (copy_from_user (kbuf, buf, len)) {
492 value = ep_io (data, kbuf, len);
493 VDEBUG (data->dev, "%s write %d IN, status %d\n",
494 data->name, len, value);
502 ep_release (struct inode *inode, struct file *fd)
504 struct ep_data *data = fd->private_data;
506 /* clean up if this can be reopened */
507 if (data->state != STATE_EP_UNBOUND) {
508 data->state = STATE_EP_DISABLED;
509 data->desc.bDescriptorType = 0;
510 data->hs_desc.bDescriptorType = 0;
516 static int ep_ioctl (struct inode *inode, struct file *fd,
517 unsigned code, unsigned long value)
519 struct ep_data *data = fd->private_data;
522 if ((status = get_ready_ep (fd->f_flags, data)) < 0)
525 spin_lock_irq (&data->dev->lock);
526 if (likely (data->ep != NULL)) {
528 case GADGETFS_FIFO_STATUS:
529 status = usb_ep_fifo_status (data->ep);
531 case GADGETFS_FIFO_FLUSH:
532 usb_ep_fifo_flush (data->ep);
534 case GADGETFS_CLEAR_HALT:
535 status = usb_ep_clear_halt (data->ep);
542 spin_unlock_irq (&data->dev->lock);
547 /*----------------------------------------------------------------------*/
549 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
552 struct usb_request *req;
553 struct ep_data *epdata;
559 static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
561 struct kiocb_priv *priv = (void *) &iocb->private;
562 struct ep_data *epdata;
566 epdata = priv->epdata;
567 // spin_lock(&epdata->dev->lock);
568 kiocbSetCancelled(iocb);
569 if (likely(epdata && epdata->ep && priv->req))
570 value = usb_ep_dequeue (epdata->ep, priv->req);
573 // spin_unlock(&epdata->dev->lock);
580 static long ep_aio_read_retry(struct kiocb *iocb)
582 struct kiocb_priv *priv = (void *) &iocb->private;
583 int status = priv->actual;
585 /* we "retry" to get the right mm context for this: */
586 status = copy_to_user(priv->ubuf, priv->buf, priv->actual);
587 if (unlikely(0 != status))
590 status = priv->actual;
596 static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
598 struct kiocb *iocb = req->context;
599 struct kiocb_priv *priv = (void *) &iocb->private;
600 struct ep_data *epdata = priv->epdata;
602 /* lock against disconnect (and ideally, cancel) */
603 spin_lock(&epdata->dev->lock);
606 if (NULL == iocb->ki_retry
607 || unlikely(0 == req->actual)
608 || unlikely(kiocbIsCancelled(iocb))) {
610 /* aio_complete() reports bytes-transferred _and_ faults */
611 if (unlikely(kiocbIsCancelled(iocb)))
615 req->actual ? req->actual : req->status,
618 /* retry() won't report both; so we hide some faults */
619 if (unlikely(0 != req->status))
620 DBG(epdata->dev, "%s fault %d len %d\n",
621 ep->name, req->status, req->actual);
623 priv->buf = req->buf;
624 priv->actual = req->actual;
627 spin_unlock(&epdata->dev->lock);
629 usb_ep_free_request(ep, req);
634 ep_aio_rwtail(struct kiocb *iocb, char *buf, size_t len, struct ep_data *epdata)
636 struct kiocb_priv *priv = (void *) &iocb->private;
637 struct usb_request *req;
640 value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
641 if (unlikely(value < 0)) {
646 iocb->ki_cancel = ep_aio_cancel;
648 priv->epdata = epdata;
651 /* each kiocb is coupled to one usb_request, but we can't
652 * allocate or submit those if the host disconnected.
654 spin_lock_irq(&epdata->dev->lock);
655 if (likely(epdata->ep)) {
656 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
661 req->complete = ep_aio_complete;
663 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
664 if (unlikely(0 != value))
665 usb_ep_free_request(epdata->ep, req);
670 spin_unlock_irq(&epdata->dev->lock);
677 value = -EIOCBQUEUED;
682 ep_aio_read(struct kiocb *iocb, char __user *ubuf, size_t len, loff_t o)
684 struct kiocb_priv *priv = (void *) &iocb->private;
685 struct ep_data *epdata = iocb->ki_filp->private_data;
688 if (unlikely(epdata->desc.bEndpointAddress & USB_DIR_IN))
690 buf = kmalloc(len, GFP_KERNEL);
693 iocb->ki_retry = ep_aio_read_retry;
695 return ep_aio_rwtail(iocb, buf, len, epdata);
699 ep_aio_write(struct kiocb *iocb, const char __user *ubuf, size_t len, loff_t o)
701 struct ep_data *epdata = iocb->ki_filp->private_data;
704 if (unlikely(!(epdata->desc.bEndpointAddress & USB_DIR_IN)))
706 buf = kmalloc(len, GFP_KERNEL);
709 if (unlikely(copy_from_user(buf, ubuf, len) != 0)) {
713 return ep_aio_rwtail(iocb, buf, len, epdata);
716 /*----------------------------------------------------------------------*/
718 /* used after endpoint configuration */
719 static struct file_operations ep_io_operations = {
720 .owner = THIS_MODULE,
724 .release = ep_release,
726 .aio_read = ep_aio_read,
727 .aio_write = ep_aio_write,
730 /* ENDPOINT INITIALIZATION
732 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
733 * status = write (fd, descriptors, sizeof descriptors)
735 * That write establishes the endpoint configuration, configuring
736 * the controller to process bulk, interrupt, or isochronous transfers
737 * at the right maxpacket size, and so on.
739 * The descriptors are message type 1, identified by a host order u32
740 * at the beginning of what's written. Descriptor order is: full/low
741 * speed descriptor, then optional high speed descriptor.
744 ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
746 struct ep_data *data = fd->private_data;
751 if ((value = down_interruptible (&data->lock)) < 0)
754 if (data->state != STATE_EP_READY) {
760 if (len < USB_DT_ENDPOINT_SIZE + 4)
763 /* we might need to change message format someday */
764 if (copy_from_user (&tag, buf, 4)) {
768 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
774 /* NOTE: audio endpoint extensions not accepted here;
775 * just don't include the extra bytes.
778 /* full/low speed descriptor, then high speed */
779 if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
782 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
783 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
785 if (len != USB_DT_ENDPOINT_SIZE) {
786 if (len != 2 * USB_DT_ENDPOINT_SIZE)
788 if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
789 USB_DT_ENDPOINT_SIZE)) {
792 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
793 || data->hs_desc.bDescriptorType
794 != USB_DT_ENDPOINT) {
795 DBG(data->dev, "config %s, bad hs length or type\n",
802 spin_lock_irq (&data->dev->lock);
803 if (data->dev->state == STATE_DEV_UNBOUND) {
806 } else if ((ep = data->ep) == NULL) {
810 switch (data->dev->gadget->speed) {
813 value = usb_ep_enable (ep, &data->desc);
815 data->state = STATE_EP_ENABLED;
819 /* fails if caller didn't provide that descriptor... */
820 value = usb_ep_enable (ep, &data->hs_desc);
822 data->state = STATE_EP_ENABLED;
826 DBG (data->dev, "unconnected, %s init deferred\n",
828 data->state = STATE_EP_DEFER_ENABLE;
831 fd->f_op = &ep_io_operations;
833 spin_unlock_irq (&data->dev->lock);
836 data->desc.bDescriptorType = 0;
837 data->hs_desc.bDescriptorType = 0;
850 ep_open (struct inode *inode, struct file *fd)
852 struct ep_data *data = inode->u.generic_ip;
855 if (down_interruptible (&data->lock) != 0)
857 spin_lock_irq (&data->dev->lock);
858 if (data->dev->state == STATE_DEV_UNBOUND)
860 else if (data->state == STATE_EP_DISABLED) {
862 data->state = STATE_EP_READY;
864 fd->private_data = data;
865 VDEBUG (data->dev, "%s ready\n", data->name);
867 DBG (data->dev, "%s state %d\n",
868 data->name, data->state);
869 spin_unlock_irq (&data->dev->lock);
874 /* used before endpoint configuration */
875 static struct file_operations ep_config_operations = {
876 .owner = THIS_MODULE,
879 .release = ep_release,
882 /*----------------------------------------------------------------------*/
884 /* EP0 IMPLEMENTATION can be partly in userspace.
886 * Drivers that use this facility receive various events, including
887 * control requests the kernel doesn't handle. Drivers that don't
888 * use this facility may be too simple-minded for real applications.
891 static inline void ep0_readable (struct dev_data *dev)
893 wake_up (&dev->wait);
894 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
897 static void clean_req (struct usb_ep *ep, struct usb_request *req)
899 struct dev_data *dev = ep->driver_data;
901 if (req->buf != dev->rbuf) {
902 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
903 req->buf = dev->rbuf;
904 req->dma = DMA_ADDR_INVALID;
906 req->complete = epio_complete;
907 dev->setup_out_ready = 0;
910 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
912 struct dev_data *dev = ep->driver_data;
915 /* for control OUT, data must still get to userspace */
916 if (!dev->setup_in) {
917 dev->setup_out_error = (req->status != 0);
918 if (!dev->setup_out_error)
920 dev->setup_out_ready = 1;
922 } else if (dev->state == STATE_SETUP)
923 dev->state = STATE_CONNECTED;
925 /* clean up as appropriate */
926 if (free && req->buf != &dev->rbuf)
928 req->complete = epio_complete;
931 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
933 struct dev_data *dev = ep->driver_data;
935 if (dev->setup_out_ready) {
936 DBG (dev, "ep0 request busy!\n");
939 if (len > sizeof (dev->rbuf))
940 req->buf = usb_ep_alloc_buffer (ep, len, &req->dma, GFP_ATOMIC);
942 req->buf = dev->rbuf;
945 req->complete = ep0_complete;
951 ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
953 struct dev_data *dev = fd->private_data;
955 enum ep0_state state;
957 spin_lock_irq (&dev->lock);
959 /* report fd mode change before acting on it */
960 if (dev->setup_abort) {
961 dev->setup_abort = 0;
966 /* control DATA stage */
967 if ((state = dev->state) == STATE_SETUP) {
969 if (dev->setup_in) { /* stall IN */
970 VDEBUG(dev, "ep0in stall\n");
971 (void) usb_ep_set_halt (dev->gadget->ep0);
973 dev->state = STATE_CONNECTED;
975 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
976 struct usb_ep *ep = dev->gadget->ep0;
977 struct usb_request *req = dev->req;
979 if ((retval = setup_req (ep, req, 0)) == 0)
980 retval = usb_ep_queue (ep, req, GFP_ATOMIC);
981 dev->state = STATE_CONNECTED;
983 } else { /* collect OUT data */
984 if ((fd->f_flags & O_NONBLOCK) != 0
985 && !dev->setup_out_ready) {
989 spin_unlock_irq (&dev->lock);
990 retval = wait_event_interruptible (dev->wait,
991 dev->setup_out_ready != 0);
993 /* FIXME state could change from under us */
994 spin_lock_irq (&dev->lock);
997 if (dev->setup_out_error)
1000 len = min (len, (size_t)dev->req->actual);
1001 // FIXME don't call this with the spinlock held ...
1002 if (copy_to_user (buf, &dev->req->buf, len))
1004 clean_req (dev->gadget->ep0, dev->req);
1005 /* NOTE userspace can't yet choose to stall */
1011 /* else normal: return event data */
1012 if (len < sizeof dev->event [0]) {
1016 len -= len % sizeof (struct usb_gadgetfs_event);
1017 dev->usermode_setup = 1;
1020 /* return queued events right away */
1021 if (dev->ev_next != 0) {
1023 int tmp = dev->ev_next;
1025 len = min (len, tmp * sizeof (struct usb_gadgetfs_event));
1026 n = len / sizeof (struct usb_gadgetfs_event);
1028 /* ep0 can't deliver events when STATE_SETUP */
1029 for (i = 0; i < n; i++) {
1030 if (dev->event [i].type == GADGETFS_SETUP) {
1032 len *= sizeof (struct usb_gadgetfs_event);
1037 spin_unlock_irq (&dev->lock);
1038 if (copy_to_user (buf, &dev->event, len))
1043 len /= sizeof (struct usb_gadgetfs_event);
1045 /* NOTE this doesn't guard against broken drivers;
1046 * concurrent ep0 readers may lose events.
1048 spin_lock_irq (&dev->lock);
1049 dev->ev_next -= len;
1050 if (dev->ev_next != 0)
1051 memmove (&dev->event, &dev->event [len],
1052 sizeof (struct usb_gadgetfs_event)
1055 dev->state = STATE_SETUP;
1056 spin_unlock_irq (&dev->lock);
1060 if (fd->f_flags & O_NONBLOCK) {
1067 DBG (dev, "fail %s, state %d\n", __FUNCTION__, state);
1070 case STATE_UNCONNECTED:
1071 case STATE_CONNECTED:
1072 spin_unlock_irq (&dev->lock);
1073 DBG (dev, "%s wait\n", __FUNCTION__);
1075 /* wait for events */
1076 retval = wait_event_interruptible (dev->wait,
1080 spin_lock_irq (&dev->lock);
1085 spin_unlock_irq (&dev->lock);
1089 static struct usb_gadgetfs_event *
1090 next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1092 struct usb_gadgetfs_event *event;
1096 /* these events purge the queue */
1097 case GADGETFS_DISCONNECT:
1098 if (dev->state == STATE_SETUP)
1099 dev->setup_abort = 1;
1101 case GADGETFS_CONNECT:
1104 case GADGETFS_SETUP: /* previous request timed out */
1105 case GADGETFS_SUSPEND: /* same effect */
1106 /* these events can't be repeated */
1107 for (i = 0; i != dev->ev_next; i++) {
1108 if (dev->event [i].type != type)
1110 DBG (dev, "discard old event %d\n", type);
1112 if (i == dev->ev_next)
1114 /* indices start at zero, for simplicity */
1115 memmove (&dev->event [i], &dev->event [i + 1],
1116 sizeof (struct usb_gadgetfs_event)
1117 * (dev->ev_next - i));
1123 event = &dev->event [dev->ev_next++];
1124 BUG_ON (dev->ev_next > N_EVENT);
1125 VDEBUG (dev, "ev %d, next %d\n", type, dev->ev_next);
1126 memset (event, 0, sizeof *event);
1132 ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1134 struct dev_data *dev = fd->private_data;
1135 ssize_t retval = -ESRCH;
1137 spin_lock_irq (&dev->lock);
1139 /* report fd mode change before acting on it */
1140 if (dev->setup_abort) {
1141 dev->setup_abort = 0;
1144 /* data and/or status stage for control request */
1145 } else if (dev->state == STATE_SETUP) {
1147 /* IN DATA+STATUS caller makes len <= wLength */
1148 if (dev->setup_in) {
1149 retval = setup_req (dev->gadget->ep0, dev->req, len);
1151 spin_unlock_irq (&dev->lock);
1152 if (copy_from_user (dev->req->buf, buf, len))
1155 retval = usb_ep_queue (
1156 dev->gadget->ep0, dev->req,
1159 spin_lock_irq (&dev->lock);
1160 clean_req (dev->gadget->ep0, dev->req);
1161 spin_unlock_irq (&dev->lock);
1168 /* can stall some OUT transfers */
1169 } else if (dev->setup_can_stall) {
1170 VDEBUG(dev, "ep0out stall\n");
1171 (void) usb_ep_set_halt (dev->gadget->ep0);
1173 dev->state = STATE_CONNECTED;
1175 DBG(dev, "bogus ep0out stall!\n");
1178 DBG (dev, "fail %s, state %d\n", __FUNCTION__, dev->state);
1180 spin_unlock_irq (&dev->lock);
1185 ep0_fasync (int f, struct file *fd, int on)
1187 struct dev_data *dev = fd->private_data;
1188 // caller must F_SETOWN before signal delivery happens
1189 VDEBUG (dev, "%s %s\n", __FUNCTION__, on ? "on" : "off");
1190 return fasync_helper (f, fd, on, &dev->fasync);
1193 static struct usb_gadget_driver gadgetfs_driver;
1196 dev_release (struct inode *inode, struct file *fd)
1198 struct dev_data *dev = fd->private_data;
1200 /* closing ep0 === shutdown all */
1202 usb_gadget_unregister_driver (&gadgetfs_driver);
1204 /* at this point "good" hardware has disconnected the
1205 * device from USB; the host won't see it any more.
1206 * alternatively, all host requests will time out.
1209 fasync_helper (-1, fd, 0, &dev->fasync);
1214 /* other endpoints were all decoupled from this device */
1215 dev->state = STATE_DEV_DISABLED;
1219 static int dev_ioctl (struct inode *inode, struct file *fd,
1220 unsigned code, unsigned long value)
1222 struct dev_data *dev = fd->private_data;
1223 struct usb_gadget *gadget = dev->gadget;
1225 if (gadget->ops->ioctl)
1226 return gadget->ops->ioctl (gadget, code, value);
1230 /* used after device configuration */
1231 static struct file_operations ep0_io_operations = {
1232 .owner = THIS_MODULE,
1235 .fasync = ep0_fasync,
1236 // .poll = ep0_poll,
1238 .release = dev_release,
1241 /*----------------------------------------------------------------------*/
1243 /* The in-kernel gadget driver handles most ep0 issues, in particular
1244 * enumerating the single configuration (as provided from user space).
1246 * Unrecognized ep0 requests may be handled in user space.
1250 static void make_qualifier (struct dev_data *dev)
1252 struct usb_qualifier_descriptor qual;
1253 struct usb_device_descriptor *desc;
1255 qual.bLength = sizeof qual;
1256 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1257 qual.bcdUSB = __constant_cpu_to_le16 (0x0200);
1260 qual.bDeviceClass = desc->bDeviceClass;
1261 qual.bDeviceSubClass = desc->bDeviceSubClass;
1262 qual.bDeviceProtocol = desc->bDeviceProtocol;
1264 /* assumes ep0 uses the same value for both speeds ... */
1265 qual.bMaxPacketSize0 = desc->bMaxPacketSize0;
1267 qual.bNumConfigurations = 1;
1270 memcpy (dev->rbuf, &qual, sizeof qual);
1275 config_buf (struct dev_data *dev, u8 type, unsigned index)
1282 /* only one configuration */
1287 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1288 if (type == USB_DT_OTHER_SPEED_CONFIG)
1291 dev->req->buf = dev->hs_config;
1292 len = le16_to_cpup (&dev->hs_config->wTotalLength);
1296 dev->req->buf = dev->config;
1297 len = le16_to_cpup (&dev->config->wTotalLength);
1299 ((u8 *)dev->req->buf) [1] = type;
1304 gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1306 struct dev_data *dev = get_gadget_data (gadget);
1307 struct usb_request *req = dev->req;
1308 int value = -EOPNOTSUPP;
1309 struct usb_gadgetfs_event *event;
1311 spin_lock (&dev->lock);
1312 dev->setup_abort = 0;
1313 if (dev->state == STATE_UNCONNECTED) {
1315 struct ep_data *data;
1317 dev->state = STATE_CONNECTED;
1318 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1321 if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) {
1322 ERROR (dev, "no high speed config??\n");
1325 #endif /* HIGHSPEED */
1327 INFO (dev, "connected\n");
1328 event = next_event (dev, GADGETFS_CONNECT);
1329 event->u.speed = gadget->speed;
1332 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
1333 data = ep->driver_data;
1334 /* ... down_trylock (&data->lock) ... */
1335 if (data->state != STATE_EP_DEFER_ENABLE)
1338 if (gadget->speed == USB_SPEED_HIGH)
1339 value = usb_ep_enable (ep, &data->hs_desc);
1341 #endif /* HIGHSPEED */
1342 value = usb_ep_enable (ep, &data->desc);
1344 ERROR (dev, "deferred %s enable --> %d\n",
1348 data->state = STATE_EP_ENABLED;
1349 wake_up (&data->wait);
1350 DBG (dev, "woke up %s waiters\n", data->name);
1353 /* host may have given up waiting for response. we can miss control
1354 * requests handled lower down (device/endpoint status and features);
1355 * then ep0_{read,write} will report the wrong status. controller
1356 * driver will have aborted pending i/o.
1358 } else if (dev->state == STATE_SETUP)
1359 dev->setup_abort = 1;
1361 req->buf = dev->rbuf;
1362 req->dma = DMA_ADDR_INVALID;
1363 req->context = NULL;
1364 value = -EOPNOTSUPP;
1365 switch (ctrl->bRequest) {
1367 case USB_REQ_GET_DESCRIPTOR:
1368 if (ctrl->bRequestType != USB_DIR_IN)
1370 switch (ctrl->wValue >> 8) {
1373 value = min (ctrl->wLength, (u16) sizeof *dev->dev);
1374 req->buf = dev->dev;
1377 case USB_DT_DEVICE_QUALIFIER:
1378 if (!dev->hs_config)
1380 value = min (ctrl->wLength, (u16)
1381 sizeof (struct usb_qualifier_descriptor));
1382 make_qualifier (dev);
1384 case USB_DT_OTHER_SPEED_CONFIG:
1388 value = config_buf (dev,
1390 ctrl->wValue & 0xff);
1392 value = min (ctrl->wLength, (u16) value);
1397 default: // all others are errors
1402 /* currently one config, two speeds */
1403 case USB_REQ_SET_CONFIGURATION:
1404 if (ctrl->bRequestType != 0)
1406 if (0 == (u8) ctrl->wValue) {
1408 dev->current_config = 0;
1409 // user mode expected to disable endpoints
1413 if (gadget->speed == USB_SPEED_HIGH)
1414 config = dev->hs_config->bConfigurationValue;
1417 config = dev->config->bConfigurationValue;
1419 if (config == (u8) ctrl->wValue) {
1421 dev->current_config = config;
1425 /* report SET_CONFIGURATION like any other control request,
1426 * except that usermode may not stall this. the next
1427 * request mustn't be allowed start until this finishes:
1428 * endpoints and threads set up, etc.
1430 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1431 * has bad/racey automagic that prevents synchronizing here.
1432 * even kernel mode drivers often miss them.
1435 INFO (dev, "configuration #%d\n", dev->current_config);
1436 if (dev->usermode_setup) {
1437 dev->setup_can_stall = 0;
1443 #ifndef CONFIG_USB_GADGETFS_PXA2XX
1444 /* PXA automagically handles this request too */
1445 case USB_REQ_GET_CONFIGURATION:
1446 if (ctrl->bRequestType != 0x80)
1448 *(u8 *)req->buf = dev->current_config;
1449 value = min (ctrl->wLength, (u16) 1);
1455 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1456 dev->usermode_setup ? "delegate" : "fail",
1457 ctrl->bRequestType, ctrl->bRequest,
1458 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1460 /* if there's an ep0 reader, don't stall */
1461 if (dev->usermode_setup) {
1462 dev->setup_can_stall = 1;
1464 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1466 dev->setup_out_ready = 0;
1467 dev->setup_out_error = 0;
1470 /* read DATA stage for OUT right away */
1471 if (unlikely (!dev->setup_in && ctrl->wLength)) {
1472 value = setup_req (gadget->ep0, dev->req,
1476 value = usb_ep_queue (gadget->ep0, dev->req,
1479 clean_req (gadget->ep0, dev->req);
1483 /* we can't currently stall these */
1484 dev->setup_can_stall = 0;
1487 /* state changes when reader collects event */
1488 event = next_event (dev, GADGETFS_SETUP);
1489 event->u.setup = *ctrl;
1491 spin_unlock (&dev->lock);
1496 /* proceed with data transfer and status phases? */
1497 if (value >= 0 && dev->state != STATE_SETUP) {
1498 req->length = value;
1499 req->zero = value < ctrl->wLength
1500 && (value % gadget->ep0->maxpacket) == 0;
1501 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1503 DBG (dev, "ep_queue --> %d\n", value);
1508 /* device stalls when value < 0 */
1509 spin_unlock (&dev->lock);
1513 static void destroy_ep_files (struct dev_data *dev)
1515 struct list_head *entry, *tmp;
1517 DBG (dev, "%s %d\n", __FUNCTION__, dev->state);
1519 /* dev->state must prevent interference */
1521 spin_lock_irq (&dev->lock);
1522 list_for_each_safe (entry, tmp, &dev->epfiles) {
1524 struct inode *parent;
1525 struct dentry *dentry;
1527 /* break link to FS */
1528 ep = list_entry (entry, struct ep_data, epfiles);
1529 list_del_init (&ep->epfiles);
1530 dentry = ep->dentry;
1532 parent = dentry->d_parent->d_inode;
1534 /* break link to controller */
1535 if (ep->state == STATE_EP_ENABLED)
1536 (void) usb_ep_disable (ep->ep);
1537 ep->state = STATE_EP_UNBOUND;
1538 usb_ep_free_request (ep->ep, ep->req);
1540 wake_up (&ep->wait);
1543 spin_unlock_irq (&dev->lock);
1545 /* break link to dcache */
1546 down (&parent->i_sem);
1549 up (&parent->i_sem);
1551 /* fds may still be open */
1554 spin_unlock_irq (&dev->lock);
1558 static struct inode *
1559 gadgetfs_create_file (struct super_block *sb, char const *name,
1560 void *data, struct file_operations *fops,
1561 struct dentry **dentry_p);
1563 static int activate_ep_files (struct dev_data *dev)
1567 gadget_for_each_ep (ep, dev->gadget) {
1568 struct ep_data *data;
1570 data = kmalloc (sizeof *data, GFP_KERNEL);
1573 memset (data, 0, sizeof data);
1574 data->state = STATE_EP_DISABLED;
1575 init_MUTEX (&data->lock);
1576 init_waitqueue_head (&data->wait);
1578 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1579 atomic_set (&data->count, 1);
1584 ep->driver_data = data;
1586 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1590 data->inode = gadgetfs_create_file (dev->sb, data->name,
1591 data, &ep_config_operations,
1597 list_add_tail (&data->epfiles, &dev->epfiles);
1602 DBG (dev, "%s enomem\n", __FUNCTION__);
1603 destroy_ep_files (dev);
1608 gadgetfs_unbind (struct usb_gadget *gadget)
1610 struct dev_data *dev = get_gadget_data (gadget);
1612 DBG (dev, "%s\n", __FUNCTION__);
1614 spin_lock_irq (&dev->lock);
1615 dev->state = STATE_DEV_UNBOUND;
1616 spin_unlock_irq (&dev->lock);
1618 destroy_ep_files (dev);
1619 gadget->ep0->driver_data = NULL;
1620 set_gadget_data (gadget, NULL);
1622 /* we've already been disconnected ... no i/o is active */
1624 usb_ep_free_request (gadget->ep0, dev->req);
1625 DBG (dev, "%s done\n", __FUNCTION__);
1629 static struct dev_data *the_device;
1632 gadgetfs_bind (struct usb_gadget *gadget)
1634 struct dev_data *dev = the_device;
1638 if (0 != strcmp (CHIP, gadget->name)) {
1639 printk (KERN_ERR "%s expected " CHIP " controller not %s\n",
1640 shortname, gadget->name);
1644 set_gadget_data (gadget, dev);
1645 dev->gadget = gadget;
1646 gadget->ep0->driver_data = dev;
1647 dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
1649 /* preallocate control response and buffer */
1650 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1653 dev->req->context = NULL;
1654 dev->req->complete = epio_complete;
1656 if (activate_ep_files (dev) < 0)
1659 INFO (dev, "bound to %s driver\n", gadget->name);
1660 dev->state = STATE_UNCONNECTED;
1665 gadgetfs_unbind (gadget);
1670 gadgetfs_disconnect (struct usb_gadget *gadget)
1672 struct dev_data *dev = get_gadget_data (gadget);
1674 if (dev->state == STATE_UNCONNECTED) {
1675 DBG (dev, "already unconnected\n");
1678 dev->state = STATE_UNCONNECTED;
1680 INFO (dev, "disconnected\n");
1681 spin_lock (&dev->lock);
1682 next_event (dev, GADGETFS_DISCONNECT);
1684 spin_unlock (&dev->lock);
1688 gadgetfs_suspend (struct usb_gadget *gadget)
1690 struct dev_data *dev = get_gadget_data (gadget);
1692 INFO (dev, "suspended from state %d\n", dev->state);
1693 spin_lock (&dev->lock);
1694 switch (dev->state) {
1695 case STATE_SETUP: // VERY odd... host died??
1696 case STATE_CONNECTED:
1697 case STATE_UNCONNECTED:
1698 next_event (dev, GADGETFS_SUSPEND);
1704 spin_unlock (&dev->lock);
1707 static struct usb_gadget_driver gadgetfs_driver = {
1709 .speed = USB_SPEED_HIGH,
1711 .speed = USB_SPEED_FULL,
1713 .function = (char *) driver_desc,
1714 .bind = gadgetfs_bind,
1715 .unbind = gadgetfs_unbind,
1716 .setup = gadgetfs_setup,
1717 .disconnect = gadgetfs_disconnect,
1718 .suspend = gadgetfs_suspend,
1721 .name = (char *) shortname,
1728 /*----------------------------------------------------------------------*/
1730 /* DEVICE INITIALIZATION
1732 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1733 * status = write (fd, descriptors, sizeof descriptors)
1735 * That write establishes the device configuration, so the kernel can
1736 * bind to the controller ... guaranteeing it can handle enumeration
1737 * at all necessary speeds. Descriptor order is:
1739 * . message tag (u32, host order) ... for now, must be zero; it
1740 * would change to support features like multi-config devices
1741 * . full/low speed config ... all wTotalLength bytes (with interface,
1742 * class, altsetting, endpoint, and other descriptors)
1743 * . high speed config ... all descriptors, for high speed operation;
1744 * this one's optional except for high-speed hardware
1745 * . device descriptor
1747 * Endpoints are not yet enabled. Drivers may want to immediately
1748 * initialize them, using the /dev/gadget/ep* files that are available
1749 * as soon as the kernel sees the configuration, or they can wait
1750 * until device configuration and interface altsetting changes create
1751 * the need to configure (or unconfigure) them.
1753 * After initialization, the device stays active for as long as that
1754 * $CHIP file is open. Events may then be read from that descriptor,
1755 * such configuration notifications. More complex drivers will handle
1756 * some control requests in user space.
1759 static int is_valid_config (struct usb_config_descriptor *config)
1761 return config->bDescriptorType == USB_DT_CONFIG
1762 && config->bLength == USB_DT_CONFIG_SIZE
1763 && config->bConfigurationValue != 0
1764 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1765 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1766 /* FIXME check lengths: walk to end */
1770 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1772 struct dev_data *dev = fd->private_data;
1773 ssize_t value = len, length = len;
1778 if (dev->state != STATE_OPENED)
1781 if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
1784 /* we might need to change message format someday */
1785 if (copy_from_user (&tag, buf, 4))
1792 kbuf = kmalloc (length, SLAB_KERNEL);
1795 if (copy_from_user (kbuf, buf, length)) {
1800 spin_lock_irq (&dev->lock);
1806 /* full or low speed config */
1807 dev->config = (void *) kbuf;
1808 total = le16_to_cpup (&dev->config->wTotalLength);
1809 if (!is_valid_config (dev->config) || total >= length)
1814 /* optional high speed config */
1815 if (kbuf [1] == USB_DT_CONFIG) {
1816 dev->hs_config = (void *) kbuf;
1817 total = le16_to_cpup (&dev->hs_config->wTotalLength);
1818 if (!is_valid_config (dev->hs_config) || total >= length)
1824 /* could support multiple configs, using another encoding! */
1826 /* device descriptor (tweaked for paranoia) */
1827 if (length != USB_DT_DEVICE_SIZE)
1829 dev->dev = (void *)kbuf;
1830 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1831 || dev->dev->bDescriptorType != USB_DT_DEVICE
1832 || dev->dev->bNumConfigurations != 1)
1834 dev->dev->bNumConfigurations = 1;
1835 dev->dev->bcdUSB = __constant_cpu_to_le16 (0x0200);
1837 /* triggers gadgetfs_bind(); then we can enumerate. */
1838 spin_unlock_irq (&dev->lock);
1839 value = usb_gadget_register_driver (&gadgetfs_driver);
1844 /* at this point "good" hardware has for the first time
1845 * let the USB the host see us. alternatively, if users
1846 * unplug/replug that will clear all the error state.
1848 * note: everything running before here was guaranteed
1849 * to choke driver model style diagnostics. from here
1850 * on, they can work ... except in cleanup paths that
1851 * kick in after the ep0 descriptor is closed.
1853 fd->f_op = &ep0_io_operations;
1859 spin_unlock_irq (&dev->lock);
1860 pr_debug ("%s: %s fail %Zd, %p\n", shortname, __FUNCTION__, value, dev);
1867 dev_open (struct inode *inode, struct file *fd)
1869 struct dev_data *dev = inode->u.generic_ip;
1872 if (dev->state == STATE_DEV_DISABLED) {
1874 dev->state = STATE_OPENED;
1875 fd->private_data = dev;
1882 static struct file_operations dev_init_operations = {
1883 .owner = THIS_MODULE,
1885 .write = dev_config,
1886 .fasync = ep0_fasync,
1888 .release = dev_release,
1891 /*----------------------------------------------------------------------*/
1893 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1895 * Mounting the filesystem creates a controller file, used first for
1896 * device configuration then later for event monitoring.
1900 /* FIXME PAM etc could set this security policy without mount options
1901 * if epfiles inherited ownership and permissons from ep0 ...
1904 static unsigned default_uid;
1905 static unsigned default_gid;
1906 static unsigned default_perm = S_IRUSR | S_IWUSR;
1908 module_param (default_uid, uint, 0644);
1909 module_param (default_gid, uint, 0644);
1910 module_param (default_perm, uint, 0644);
1913 static struct inode *
1914 gadgetfs_make_inode (struct super_block *sb,
1915 void *data, struct file_operations *fops,
1918 struct inode *inode = new_inode (sb);
1921 inode->i_mode = mode;
1922 inode->i_uid = default_uid;
1923 inode->i_gid = default_gid;
1924 inode->i_blksize = PAGE_CACHE_SIZE;
1925 inode->i_blocks = 0;
1926 inode->i_atime = inode->i_mtime = inode->i_ctime
1928 inode->u.generic_ip = data;
1929 inode->i_fop = fops;
1934 /* creates in fs root directory, so non-renamable and non-linkable.
1935 * so inode and dentry are paired, until device reconfig.
1937 static struct inode *
1938 gadgetfs_create_file (struct super_block *sb, char const *name,
1939 void *data, struct file_operations *fops,
1940 struct dentry **dentry_p)
1942 struct dentry *dentry;
1943 struct inode *inode;
1947 qname.len = strlen (name);
1948 qname.hash = full_name_hash (qname.name, qname.len);
1949 dentry = d_alloc (sb->s_root, &qname);
1953 inode = gadgetfs_make_inode (sb, data, fops,
1954 S_IFREG | (default_perm & S_IRWXUGO));
1959 d_add (dentry, inode);
1964 static struct super_operations gadget_fs_operations = {
1965 .statfs = simple_statfs,
1966 .drop_inode = generic_delete_inode,
1970 gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
1972 struct inode *inode;
1974 struct dev_data *dev;
1980 sb->s_blocksize = PAGE_CACHE_SIZE;
1981 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1982 sb->s_magic = GADGETFS_MAGIC;
1983 sb->s_op = &gadget_fs_operations;
1986 inode = gadgetfs_make_inode (sb,
1987 NULL, &simple_dir_operations,
1988 S_IFDIR | S_IRUGO | S_IXUGO);
1991 inode->i_op = &simple_dir_inode_operations;
1992 if (!(d = d_alloc_root (inode))) {
1998 /* the ep0 file is named after the controller we expect;
1999 * user mode code can use it for sanity checks, like we do.
2006 if (!(inode = gadgetfs_create_file (sb, CHIP,
2007 dev, &dev_init_operations,
2013 /* other endpoint files are available after hardware setup,
2014 * from binding to a controller.
2020 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2021 static struct super_block *
2022 gadgetfs_get_sb (struct file_system_type *t, int flags,
2023 const char *path, void *opts)
2025 return get_sb_single (t, flags, opts, gadgetfs_fill_super);
2029 gadgetfs_kill_sb (struct super_block *sb)
2031 kill_litter_super (sb);
2033 put_dev (the_device);
2038 /*----------------------------------------------------------------------*/
2040 static struct file_system_type gadgetfs_type = {
2041 .owner = THIS_MODULE,
2043 .get_sb = gadgetfs_get_sb,
2044 .kill_sb = gadgetfs_kill_sb,
2047 /*----------------------------------------------------------------------*/
2049 static int __init init (void)
2053 status = register_filesystem (&gadgetfs_type);
2055 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2056 shortname, driver_desc);
2061 static void __exit cleanup (void)
2063 pr_debug ("unregister %s\n", shortname);
2064 unregister_filesystem (&gadgetfs_type);
2066 module_exit (cleanup);