1 // SPDX-License-Identifier: GPL-2.0+
3 * inode.c -- user mode filesystem api for usb gadget controllers
5 * Copyright (C) 2003-2004 David Brownell
6 * Copyright (C) 2003 Agilent Technologies
10 /* #define VERBOSE_DEBUG */
12 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/fs_context.h>
16 #include <linux/pagemap.h>
17 #include <linux/uts.h>
18 #include <linux/wait.h>
19 #include <linux/compiler.h>
20 #include <linux/uaccess.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/poll.h>
24 #include <linux/kthread.h>
25 #include <linux/aio.h>
26 #include <linux/uio.h>
27 #include <linux/refcount.h>
28 #include <linux/delay.h>
29 #include <linux/device.h>
30 #include <linux/moduleparam.h>
32 #include <linux/usb/gadgetfs.h>
33 #include <linux/usb/gadget.h>
37 * The gadgetfs API maps each endpoint to a file descriptor so that you
38 * can use standard synchronous read/write calls for I/O. There's some
39 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
40 * drivers show how this works in practice. You can also use AIO to
41 * eliminate I/O gaps between requests, to help when streaming data.
43 * Key parts that must be USB-specific are protocols defining how the
44 * read/write operations relate to the hardware state machines. There
45 * are two types of files. One type is for the device, implementing ep0.
46 * The other type is for each IN or OUT endpoint. In both cases, the
47 * user mode driver must configure the hardware before using it.
49 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
50 * (by writing configuration and device descriptors). Afterwards it
51 * may serve as a source of device events, used to handle all control
52 * requests other than basic enumeration.
54 * - Then, after a SET_CONFIGURATION control request, ep_config() is
55 * called when each /dev/gadget/ep* file is configured (by writing
56 * endpoint descriptors). Afterwards these files are used to write()
57 * IN data or to read() OUT data. To halt the endpoint, a "wrong
58 * direction" request is issued (like reading an IN endpoint).
60 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
61 * not possible on all hardware. For example, precise fault handling with
62 * respect to data left in endpoint fifos after aborted operations; or
63 * selective clearing of endpoint halts, to implement SET_INTERFACE.
66 #define DRIVER_DESC "USB Gadget filesystem"
67 #define DRIVER_VERSION "24 Aug 2004"
69 static const char driver_desc [] = DRIVER_DESC;
70 static const char shortname [] = "gadgetfs";
72 MODULE_DESCRIPTION (DRIVER_DESC);
73 MODULE_AUTHOR ("David Brownell");
74 MODULE_LICENSE ("GPL");
76 static int ep_open(struct inode *, struct file *);
79 /*----------------------------------------------------------------------*/
81 #define GADGETFS_MAGIC 0xaee71ee7
83 /* /dev/gadget/$CHIP represents ep0 and the whole device */
85 /* DISABLED is the initial state. */
86 STATE_DEV_DISABLED = 0,
88 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
89 * ep0/device i/o modes and binding to the controller. Driver
90 * must always write descriptors to initialize the device, then
91 * the device becomes UNCONNECTED until enumeration.
95 /* From then on, ep0 fd is in either of two basic modes:
96 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
97 * - SETUP: read/write will transfer control data and succeed;
98 * or if "wrong direction", performs protocol stall
100 STATE_DEV_UNCONNECTED,
104 /* UNBOUND means the driver closed ep0, so the device won't be
105 * accessible again (DEV_DISABLED) until all fds are closed.
110 /* enough for the whole queue: most events invalidate others */
113 #define RBUF_SIZE 256
119 enum ep0_state state; /* P: lock */
120 struct usb_gadgetfs_event event [N_EVENT];
122 struct fasync_struct *fasync;
125 /* drivers reading ep0 MUST handle control requests (SETUP)
126 * reported that way; else the host will time out.
128 unsigned usermode_setup : 1,
134 gadget_registered : 1;
135 unsigned setup_wLength;
137 /* the rest is basically write-once */
138 struct usb_config_descriptor *config, *hs_config;
139 struct usb_device_descriptor *dev;
140 struct usb_request *req;
141 struct usb_gadget *gadget;
142 struct list_head epfiles;
144 wait_queue_head_t wait;
145 struct super_block *sb;
146 struct dentry *dentry;
148 /* except this scratch i/o buffer for ep0 */
152 static inline void get_dev (struct dev_data *data)
154 refcount_inc (&data->count);
157 static void put_dev (struct dev_data *data)
159 if (likely (!refcount_dec_and_test (&data->count)))
161 /* needs no more cleanup */
162 BUG_ON (waitqueue_active (&data->wait));
166 static struct dev_data *dev_new (void)
168 struct dev_data *dev;
170 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
173 dev->state = STATE_DEV_DISABLED;
174 refcount_set (&dev->count, 1);
175 spin_lock_init (&dev->lock);
176 INIT_LIST_HEAD (&dev->epfiles);
177 init_waitqueue_head (&dev->wait);
181 /*----------------------------------------------------------------------*/
183 /* other /dev/gadget/$ENDPOINT files represent endpoints */
185 STATE_EP_DISABLED = 0,
195 struct dev_data *dev;
196 /* must hold dev->lock before accessing ep or req */
198 struct usb_request *req;
201 struct usb_endpoint_descriptor desc, hs_desc;
202 struct list_head epfiles;
203 wait_queue_head_t wait;
204 struct dentry *dentry;
207 static inline void get_ep (struct ep_data *data)
209 refcount_inc (&data->count);
212 static void put_ep (struct ep_data *data)
214 if (likely (!refcount_dec_and_test (&data->count)))
217 /* needs no more cleanup */
218 BUG_ON (!list_empty (&data->epfiles));
219 BUG_ON (waitqueue_active (&data->wait));
223 /*----------------------------------------------------------------------*/
225 /* most "how to use the hardware" policy choices are in userspace:
226 * mapping endpoint roles (which the driver needs) to the capabilities
227 * which the usb controller has. most of those capabilities are exposed
228 * implicitly, starting with the driver name and then endpoint names.
231 static const char *CHIP;
232 static DEFINE_MUTEX(sb_mutex); /* Serialize superblock operations */
234 /*----------------------------------------------------------------------*/
236 /* NOTE: don't use dev_printk calls before binding to the gadget
237 * at the end of ep0 configuration, or after unbind.
240 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
241 #define xprintk(d,level,fmt,args...) \
242 printk(level "%s: " fmt , shortname , ## args)
245 #define DBG(dev,fmt,args...) \
246 xprintk(dev , KERN_DEBUG , fmt , ## args)
248 #define DBG(dev,fmt,args...) \
255 #define VDEBUG(dev,fmt,args...) \
259 #define ERROR(dev,fmt,args...) \
260 xprintk(dev , KERN_ERR , fmt , ## args)
261 #define INFO(dev,fmt,args...) \
262 xprintk(dev , KERN_INFO , fmt , ## args)
265 /*----------------------------------------------------------------------*/
267 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
269 * After opening, configure non-control endpoints. Then use normal
270 * stream read() and write() requests; and maybe ioctl() to get more
271 * precise FIFO status when recovering from cancellation.
274 static void epio_complete (struct usb_ep *ep, struct usb_request *req)
276 struct ep_data *epdata = ep->driver_data;
281 epdata->status = req->status;
283 epdata->status = req->actual;
284 complete ((struct completion *)req->context);
287 /* tasklock endpoint, returning when it's connected.
288 * still need dev->lock to use epdata->ep.
291 get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
295 if (f_flags & O_NONBLOCK) {
296 if (!mutex_trylock(&epdata->lock))
298 if (epdata->state != STATE_EP_ENABLED &&
299 (!is_write || epdata->state != STATE_EP_READY)) {
300 mutex_unlock(&epdata->lock);
308 val = mutex_lock_interruptible(&epdata->lock);
312 switch (epdata->state) {
313 case STATE_EP_ENABLED:
315 case STATE_EP_READY: /* not configured yet */
319 case STATE_EP_UNBOUND: /* clean disconnect */
321 // case STATE_EP_DISABLED: /* "can't happen" */
322 default: /* error! */
323 pr_debug ("%s: ep %p not available, state %d\n",
324 shortname, epdata, epdata->state);
326 mutex_unlock(&epdata->lock);
331 ep_io (struct ep_data *epdata, void *buf, unsigned len)
333 DECLARE_COMPLETION_ONSTACK (done);
336 spin_lock_irq (&epdata->dev->lock);
337 if (likely (epdata->ep != NULL)) {
338 struct usb_request *req = epdata->req;
340 req->context = &done;
341 req->complete = epio_complete;
344 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
347 spin_unlock_irq (&epdata->dev->lock);
349 if (likely (value == 0)) {
350 value = wait_for_completion_interruptible(&done);
352 spin_lock_irq (&epdata->dev->lock);
353 if (likely (epdata->ep != NULL)) {
354 DBG (epdata->dev, "%s i/o interrupted\n",
356 usb_ep_dequeue (epdata->ep, epdata->req);
357 spin_unlock_irq (&epdata->dev->lock);
359 wait_for_completion(&done);
360 if (epdata->status == -ECONNRESET)
361 epdata->status = -EINTR;
363 spin_unlock_irq (&epdata->dev->lock);
365 DBG (epdata->dev, "endpoint gone\n");
366 wait_for_completion(&done);
367 epdata->status = -ENODEV;
370 return epdata->status;
376 ep_release (struct inode *inode, struct file *fd)
378 struct ep_data *data = fd->private_data;
381 value = mutex_lock_interruptible(&data->lock);
385 /* clean up if this can be reopened */
386 if (data->state != STATE_EP_UNBOUND) {
387 data->state = STATE_EP_DISABLED;
388 data->desc.bDescriptorType = 0;
389 data->hs_desc.bDescriptorType = 0;
390 usb_ep_disable(data->ep);
392 mutex_unlock(&data->lock);
397 static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
399 struct ep_data *data = fd->private_data;
402 if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
405 spin_lock_irq (&data->dev->lock);
406 if (likely (data->ep != NULL)) {
408 case GADGETFS_FIFO_STATUS:
409 status = usb_ep_fifo_status (data->ep);
411 case GADGETFS_FIFO_FLUSH:
412 usb_ep_fifo_flush (data->ep);
414 case GADGETFS_CLEAR_HALT:
415 status = usb_ep_clear_halt (data->ep);
422 spin_unlock_irq (&data->dev->lock);
423 mutex_unlock(&data->lock);
427 /*----------------------------------------------------------------------*/
429 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
432 struct usb_request *req;
433 struct ep_data *epdata;
435 struct mm_struct *mm;
436 struct work_struct work;
443 static int ep_aio_cancel(struct kiocb *iocb)
445 struct kiocb_priv *priv = iocb->private;
446 struct ep_data *epdata;
450 epdata = priv->epdata;
451 // spin_lock(&epdata->dev->lock);
452 if (likely(epdata && epdata->ep && priv->req))
453 value = usb_ep_dequeue (epdata->ep, priv->req);
456 // spin_unlock(&epdata->dev->lock);
462 static void ep_user_copy_worker(struct work_struct *work)
464 struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
465 struct mm_struct *mm = priv->mm;
466 struct kiocb *iocb = priv->iocb;
470 ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
471 kthread_unuse_mm(mm);
475 /* completing the iocb can drop the ctx and mm, don't touch mm after */
476 iocb->ki_complete(iocb, ret);
479 kfree(priv->to_free);
483 static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
485 struct kiocb *iocb = req->context;
486 struct kiocb_priv *priv = iocb->private;
487 struct ep_data *epdata = priv->epdata;
489 /* lock against disconnect (and ideally, cancel) */
490 spin_lock(&epdata->dev->lock);
494 /* if this was a write or a read returning no data then we
495 * don't need to copy anything to userspace, so we can
496 * complete the aio request immediately.
498 if (priv->to_free == NULL || unlikely(req->actual == 0)) {
500 kfree(priv->to_free);
502 iocb->private = NULL;
503 iocb->ki_complete(iocb,
504 req->actual ? req->actual : (long)req->status);
506 /* ep_copy_to_user() won't report both; we hide some faults */
507 if (unlikely(0 != req->status))
508 DBG(epdata->dev, "%s fault %d len %d\n",
509 ep->name, req->status, req->actual);
511 priv->buf = req->buf;
512 priv->actual = req->actual;
513 INIT_WORK(&priv->work, ep_user_copy_worker);
514 schedule_work(&priv->work);
517 usb_ep_free_request(ep, req);
518 spin_unlock(&epdata->dev->lock);
522 static ssize_t ep_aio(struct kiocb *iocb,
523 struct kiocb_priv *priv,
524 struct ep_data *epdata,
528 struct usb_request *req;
531 iocb->private = priv;
534 kiocb_set_cancel_fn(iocb, ep_aio_cancel);
536 priv->epdata = epdata;
538 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
540 /* each kiocb is coupled to one usb_request, but we can't
541 * allocate or submit those if the host disconnected.
543 spin_lock_irq(&epdata->dev->lock);
545 if (unlikely(epdata->ep == NULL))
548 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
556 req->complete = ep_aio_complete;
558 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
559 if (unlikely(0 != value)) {
560 usb_ep_free_request(epdata->ep, req);
563 spin_unlock_irq(&epdata->dev->lock);
567 spin_unlock_irq(&epdata->dev->lock);
568 kfree(priv->to_free);
575 ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
577 struct file *file = iocb->ki_filp;
578 struct ep_data *epdata = file->private_data;
579 size_t len = iov_iter_count(to);
583 if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
586 /* halt any endpoint by doing a "wrong direction" i/o call */
587 if (usb_endpoint_dir_in(&epdata->desc)) {
588 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
589 !is_sync_kiocb(iocb)) {
590 mutex_unlock(&epdata->lock);
593 DBG (epdata->dev, "%s halt\n", epdata->name);
594 spin_lock_irq(&epdata->dev->lock);
595 if (likely(epdata->ep != NULL))
596 usb_ep_set_halt(epdata->ep);
597 spin_unlock_irq(&epdata->dev->lock);
598 mutex_unlock(&epdata->lock);
602 buf = kmalloc(len, GFP_KERNEL);
603 if (unlikely(!buf)) {
604 mutex_unlock(&epdata->lock);
607 if (is_sync_kiocb(iocb)) {
608 value = ep_io(epdata, buf, len);
609 if (value >= 0 && (copy_to_iter(buf, value, to) != value))
612 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
616 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
617 if (!iter_is_ubuf(&priv->to) && !priv->to_free) {
621 value = ep_aio(iocb, priv, epdata, buf, len);
622 if (value == -EIOCBQUEUED)
627 mutex_unlock(&epdata->lock);
631 static ssize_t ep_config(struct ep_data *, const char *, size_t);
634 ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
636 struct file *file = iocb->ki_filp;
637 struct ep_data *epdata = file->private_data;
638 size_t len = iov_iter_count(from);
643 if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
646 configured = epdata->state == STATE_EP_ENABLED;
648 /* halt any endpoint by doing a "wrong direction" i/o call */
649 if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
650 if (usb_endpoint_xfer_isoc(&epdata->desc) ||
651 !is_sync_kiocb(iocb)) {
652 mutex_unlock(&epdata->lock);
655 DBG (epdata->dev, "%s halt\n", epdata->name);
656 spin_lock_irq(&epdata->dev->lock);
657 if (likely(epdata->ep != NULL))
658 usb_ep_set_halt(epdata->ep);
659 spin_unlock_irq(&epdata->dev->lock);
660 mutex_unlock(&epdata->lock);
664 buf = kmalloc(len, GFP_KERNEL);
665 if (unlikely(!buf)) {
666 mutex_unlock(&epdata->lock);
670 if (unlikely(!copy_from_iter_full(buf, len, from))) {
675 if (unlikely(!configured)) {
676 value = ep_config(epdata, buf, len);
677 } else if (is_sync_kiocb(iocb)) {
678 value = ep_io(epdata, buf, len);
680 struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
683 value = ep_aio(iocb, priv, epdata, buf, len);
684 if (value == -EIOCBQUEUED)
690 mutex_unlock(&epdata->lock);
694 /*----------------------------------------------------------------------*/
696 /* used after endpoint configuration */
697 static const struct file_operations ep_io_operations = {
698 .owner = THIS_MODULE,
701 .release = ep_release,
703 .unlocked_ioctl = ep_ioctl,
704 .read_iter = ep_read_iter,
705 .write_iter = ep_write_iter,
708 /* ENDPOINT INITIALIZATION
710 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
711 * status = write (fd, descriptors, sizeof descriptors)
713 * That write establishes the endpoint configuration, configuring
714 * the controller to process bulk, interrupt, or isochronous transfers
715 * at the right maxpacket size, and so on.
717 * The descriptors are message type 1, identified by a host order u32
718 * at the beginning of what's written. Descriptor order is: full/low
719 * speed descriptor, then optional high speed descriptor.
722 ep_config (struct ep_data *data, const char *buf, size_t len)
726 int value, length = len;
728 if (data->state != STATE_EP_READY) {
734 if (len < USB_DT_ENDPOINT_SIZE + 4)
737 /* we might need to change message format someday */
738 memcpy(&tag, buf, 4);
740 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
746 /* NOTE: audio endpoint extensions not accepted here;
747 * just don't include the extra bytes.
750 /* full/low speed descriptor, then high speed */
751 memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
752 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
753 || data->desc.bDescriptorType != USB_DT_ENDPOINT)
755 if (len != USB_DT_ENDPOINT_SIZE) {
756 if (len != 2 * USB_DT_ENDPOINT_SIZE)
758 memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
759 USB_DT_ENDPOINT_SIZE);
760 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
761 || data->hs_desc.bDescriptorType
762 != USB_DT_ENDPOINT) {
763 DBG(data->dev, "config %s, bad hs length or type\n",
769 spin_lock_irq (&data->dev->lock);
770 if (data->dev->state == STATE_DEV_UNBOUND) {
780 switch (data->dev->gadget->speed) {
783 ep->desc = &data->desc;
786 /* fails if caller didn't provide that descriptor... */
787 ep->desc = &data->hs_desc;
790 DBG(data->dev, "unconnected, %s init abandoned\n",
795 value = usb_ep_enable(ep);
797 data->state = STATE_EP_ENABLED;
801 spin_unlock_irq (&data->dev->lock);
804 data->desc.bDescriptorType = 0;
805 data->hs_desc.bDescriptorType = 0;
814 ep_open (struct inode *inode, struct file *fd)
816 struct ep_data *data = inode->i_private;
819 if (mutex_lock_interruptible(&data->lock) != 0)
821 spin_lock_irq (&data->dev->lock);
822 if (data->dev->state == STATE_DEV_UNBOUND)
824 else if (data->state == STATE_EP_DISABLED) {
826 data->state = STATE_EP_READY;
828 fd->private_data = data;
829 VDEBUG (data->dev, "%s ready\n", data->name);
831 DBG (data->dev, "%s state %d\n",
832 data->name, data->state);
833 spin_unlock_irq (&data->dev->lock);
834 mutex_unlock(&data->lock);
838 /*----------------------------------------------------------------------*/
840 /* EP0 IMPLEMENTATION can be partly in userspace.
842 * Drivers that use this facility receive various events, including
843 * control requests the kernel doesn't handle. Drivers that don't
844 * use this facility may be too simple-minded for real applications.
847 static inline void ep0_readable (struct dev_data *dev)
849 wake_up (&dev->wait);
850 kill_fasync (&dev->fasync, SIGIO, POLL_IN);
853 static void clean_req (struct usb_ep *ep, struct usb_request *req)
855 struct dev_data *dev = ep->driver_data;
857 if (req->buf != dev->rbuf) {
859 req->buf = dev->rbuf;
861 req->complete = epio_complete;
862 dev->setup_out_ready = 0;
865 static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
867 struct dev_data *dev = ep->driver_data;
871 /* for control OUT, data must still get to userspace */
872 spin_lock_irqsave(&dev->lock, flags);
873 if (!dev->setup_in) {
874 dev->setup_out_error = (req->status != 0);
875 if (!dev->setup_out_error)
877 dev->setup_out_ready = 1;
881 /* clean up as appropriate */
882 if (free && req->buf != &dev->rbuf)
884 req->complete = epio_complete;
885 spin_unlock_irqrestore(&dev->lock, flags);
888 static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
890 struct dev_data *dev = ep->driver_data;
892 if (dev->setup_out_ready) {
893 DBG (dev, "ep0 request busy!\n");
896 if (len > sizeof (dev->rbuf))
897 req->buf = kmalloc(len, GFP_ATOMIC);
898 if (req->buf == NULL) {
899 req->buf = dev->rbuf;
902 req->complete = ep0_complete;
909 ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
911 struct dev_data *dev = fd->private_data;
913 enum ep0_state state;
915 spin_lock_irq (&dev->lock);
916 if (dev->state <= STATE_DEV_OPENED) {
921 /* report fd mode change before acting on it */
922 if (dev->setup_abort) {
923 dev->setup_abort = 0;
928 /* control DATA stage */
929 if ((state = dev->state) == STATE_DEV_SETUP) {
931 if (dev->setup_in) { /* stall IN */
932 VDEBUG(dev, "ep0in stall\n");
933 (void) usb_ep_set_halt (dev->gadget->ep0);
935 dev->state = STATE_DEV_CONNECTED;
937 } else if (len == 0) { /* ack SET_CONFIGURATION etc */
938 struct usb_ep *ep = dev->gadget->ep0;
939 struct usb_request *req = dev->req;
941 if ((retval = setup_req (ep, req, 0)) == 0) {
943 spin_unlock_irq (&dev->lock);
944 retval = usb_ep_queue (ep, req, GFP_KERNEL);
945 spin_lock_irq (&dev->lock);
948 dev->state = STATE_DEV_CONNECTED;
950 /* assume that was SET_CONFIGURATION */
951 if (dev->current_config) {
954 if (gadget_is_dualspeed(dev->gadget)
955 && (dev->gadget->speed
957 power = dev->hs_config->bMaxPower;
959 power = dev->config->bMaxPower;
960 usb_gadget_vbus_draw(dev->gadget, 2 * power);
963 } else { /* collect OUT data */
964 if ((fd->f_flags & O_NONBLOCK) != 0
965 && !dev->setup_out_ready) {
969 spin_unlock_irq (&dev->lock);
970 retval = wait_event_interruptible (dev->wait,
971 dev->setup_out_ready != 0);
973 /* FIXME state could change from under us */
974 spin_lock_irq (&dev->lock);
978 if (dev->state != STATE_DEV_SETUP) {
982 dev->state = STATE_DEV_CONNECTED;
984 if (dev->setup_out_error)
987 len = min (len, (size_t)dev->req->actual);
989 spin_unlock_irq(&dev->lock);
990 if (copy_to_user (buf, dev->req->buf, len))
994 spin_lock_irq(&dev->lock);
996 clean_req (dev->gadget->ep0, dev->req);
997 /* NOTE userspace can't yet choose to stall */
1003 /* else normal: return event data */
1004 if (len < sizeof dev->event [0]) {
1008 len -= len % sizeof (struct usb_gadgetfs_event);
1009 dev->usermode_setup = 1;
1012 /* return queued events right away */
1013 if (dev->ev_next != 0) {
1016 n = len / sizeof (struct usb_gadgetfs_event);
1017 if (dev->ev_next < n)
1020 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1021 for (i = 0; i < n; i++) {
1022 if (dev->event [i].type == GADGETFS_SETUP) {
1023 dev->state = STATE_DEV_SETUP;
1028 spin_unlock_irq (&dev->lock);
1029 len = n * sizeof (struct usb_gadgetfs_event);
1030 if (copy_to_user (buf, &dev->event, len))
1035 /* NOTE this doesn't guard against broken drivers;
1036 * concurrent ep0 readers may lose events.
1038 spin_lock_irq (&dev->lock);
1039 if (dev->ev_next > n) {
1040 memmove(&dev->event[0], &dev->event[n],
1041 sizeof (struct usb_gadgetfs_event)
1042 * (dev->ev_next - n));
1045 spin_unlock_irq (&dev->lock);
1049 if (fd->f_flags & O_NONBLOCK) {
1056 DBG (dev, "fail %s, state %d\n", __func__, state);
1059 case STATE_DEV_UNCONNECTED:
1060 case STATE_DEV_CONNECTED:
1061 spin_unlock_irq (&dev->lock);
1062 DBG (dev, "%s wait\n", __func__);
1064 /* wait for events */
1065 retval = wait_event_interruptible (dev->wait,
1069 spin_lock_irq (&dev->lock);
1074 spin_unlock_irq (&dev->lock);
1078 static struct usb_gadgetfs_event *
1079 next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
1081 struct usb_gadgetfs_event *event;
1085 /* these events purge the queue */
1086 case GADGETFS_DISCONNECT:
1087 if (dev->state == STATE_DEV_SETUP)
1088 dev->setup_abort = 1;
1090 case GADGETFS_CONNECT:
1093 case GADGETFS_SETUP: /* previous request timed out */
1094 case GADGETFS_SUSPEND: /* same effect */
1095 /* these events can't be repeated */
1096 for (i = 0; i != dev->ev_next; i++) {
1097 if (dev->event [i].type != type)
1099 DBG(dev, "discard old event[%d] %d\n", i, type);
1101 if (i == dev->ev_next)
1103 /* indices start at zero, for simplicity */
1104 memmove (&dev->event [i], &dev->event [i + 1],
1105 sizeof (struct usb_gadgetfs_event)
1106 * (dev->ev_next - i));
1112 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
1113 event = &dev->event [dev->ev_next++];
1114 BUG_ON (dev->ev_next > N_EVENT);
1115 memset (event, 0, sizeof *event);
1121 ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1123 struct dev_data *dev = fd->private_data;
1124 ssize_t retval = -ESRCH;
1126 /* report fd mode change before acting on it */
1127 if (dev->setup_abort) {
1128 dev->setup_abort = 0;
1131 /* data and/or status stage for control request */
1132 } else if (dev->state == STATE_DEV_SETUP) {
1134 len = min_t(size_t, len, dev->setup_wLength);
1135 if (dev->setup_in) {
1136 retval = setup_req (dev->gadget->ep0, dev->req, len);
1138 dev->state = STATE_DEV_CONNECTED;
1140 spin_unlock_irq (&dev->lock);
1141 if (copy_from_user (dev->req->buf, buf, len))
1144 if (len < dev->setup_wLength)
1146 retval = usb_ep_queue (
1147 dev->gadget->ep0, dev->req,
1150 spin_lock_irq(&dev->lock);
1153 clean_req (dev->gadget->ep0, dev->req);
1160 /* can stall some OUT transfers */
1161 } else if (dev->setup_can_stall) {
1162 VDEBUG(dev, "ep0out stall\n");
1163 (void) usb_ep_set_halt (dev->gadget->ep0);
1165 dev->state = STATE_DEV_CONNECTED;
1167 DBG(dev, "bogus ep0out stall!\n");
1170 DBG (dev, "fail %s, state %d\n", __func__, dev->state);
1176 ep0_fasync (int f, struct file *fd, int on)
1178 struct dev_data *dev = fd->private_data;
1179 // caller must F_SETOWN before signal delivery happens
1180 VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
1181 return fasync_helper (f, fd, on, &dev->fasync);
1184 static struct usb_gadget_driver gadgetfs_driver;
1187 dev_release (struct inode *inode, struct file *fd)
1189 struct dev_data *dev = fd->private_data;
1191 /* closing ep0 === shutdown all */
1193 if (dev->gadget_registered) {
1194 usb_gadget_unregister_driver (&gadgetfs_driver);
1195 dev->gadget_registered = false;
1198 /* at this point "good" hardware has disconnected the
1199 * device from USB; the host won't see it any more.
1200 * alternatively, all host requests will time out.
1206 /* other endpoints were all decoupled from this device */
1207 spin_lock_irq(&dev->lock);
1208 dev->state = STATE_DEV_DISABLED;
1209 spin_unlock_irq(&dev->lock);
1216 ep0_poll (struct file *fd, poll_table *wait)
1218 struct dev_data *dev = fd->private_data;
1221 if (dev->state <= STATE_DEV_OPENED)
1222 return DEFAULT_POLLMASK;
1224 poll_wait(fd, &dev->wait, wait);
1226 spin_lock_irq(&dev->lock);
1228 /* report fd mode change before acting on it */
1229 if (dev->setup_abort) {
1230 dev->setup_abort = 0;
1235 if (dev->state == STATE_DEV_SETUP) {
1236 if (dev->setup_in || dev->setup_can_stall)
1239 if (dev->ev_next != 0)
1243 spin_unlock_irq(&dev->lock);
1247 static long gadget_dev_ioctl (struct file *fd, unsigned code, unsigned long value)
1249 struct dev_data *dev = fd->private_data;
1250 struct usb_gadget *gadget = dev->gadget;
1253 spin_lock_irq(&dev->lock);
1254 if (dev->state == STATE_DEV_OPENED ||
1255 dev->state == STATE_DEV_UNBOUND) {
1256 /* Not bound to a UDC */
1257 } else if (gadget->ops->ioctl) {
1259 spin_unlock_irq(&dev->lock);
1261 ret = gadget->ops->ioctl (gadget, code, value);
1263 spin_lock_irq(&dev->lock);
1266 spin_unlock_irq(&dev->lock);
1271 /*----------------------------------------------------------------------*/
1273 /* The in-kernel gadget driver handles most ep0 issues, in particular
1274 * enumerating the single configuration (as provided from user space).
1276 * Unrecognized ep0 requests may be handled in user space.
1279 static void make_qualifier (struct dev_data *dev)
1281 struct usb_qualifier_descriptor qual;
1282 struct usb_device_descriptor *desc;
1284 qual.bLength = sizeof qual;
1285 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1286 qual.bcdUSB = cpu_to_le16 (0x0200);
1289 qual.bDeviceClass = desc->bDeviceClass;
1290 qual.bDeviceSubClass = desc->bDeviceSubClass;
1291 qual.bDeviceProtocol = desc->bDeviceProtocol;
1293 /* assumes ep0 uses the same value for both speeds ... */
1294 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1296 qual.bNumConfigurations = 1;
1299 memcpy (dev->rbuf, &qual, sizeof qual);
1303 config_buf (struct dev_data *dev, u8 type, unsigned index)
1308 /* only one configuration */
1312 if (gadget_is_dualspeed(dev->gadget)) {
1313 hs = (dev->gadget->speed == USB_SPEED_HIGH);
1314 if (type == USB_DT_OTHER_SPEED_CONFIG)
1318 dev->req->buf = dev->hs_config;
1319 len = le16_to_cpu(dev->hs_config->wTotalLength);
1321 dev->req->buf = dev->config;
1322 len = le16_to_cpu(dev->config->wTotalLength);
1324 ((u8 *)dev->req->buf) [1] = type;
1329 gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1331 struct dev_data *dev = get_gadget_data (gadget);
1332 struct usb_request *req = dev->req;
1333 int value = -EOPNOTSUPP;
1334 struct usb_gadgetfs_event *event;
1335 u16 w_value = le16_to_cpu(ctrl->wValue);
1336 u16 w_length = le16_to_cpu(ctrl->wLength);
1338 if (w_length > RBUF_SIZE) {
1339 if (ctrl->bRequestType & USB_DIR_IN) {
1340 /* Cast away the const, we are going to overwrite on purpose. */
1341 __le16 *temp = (__le16 *)&ctrl->wLength;
1343 *temp = cpu_to_le16(RBUF_SIZE);
1344 w_length = RBUF_SIZE;
1350 spin_lock (&dev->lock);
1351 dev->setup_abort = 0;
1352 if (dev->state == STATE_DEV_UNCONNECTED) {
1353 if (gadget_is_dualspeed(gadget)
1354 && gadget->speed == USB_SPEED_HIGH
1355 && dev->hs_config == NULL) {
1356 spin_unlock(&dev->lock);
1357 ERROR (dev, "no high speed config??\n");
1361 dev->state = STATE_DEV_CONNECTED;
1363 INFO (dev, "connected\n");
1364 event = next_event (dev, GADGETFS_CONNECT);
1365 event->u.speed = gadget->speed;
1368 /* host may have given up waiting for response. we can miss control
1369 * requests handled lower down (device/endpoint status and features);
1370 * then ep0_{read,write} will report the wrong status. controller
1371 * driver will have aborted pending i/o.
1373 } else if (dev->state == STATE_DEV_SETUP)
1374 dev->setup_abort = 1;
1376 req->buf = dev->rbuf;
1377 req->context = NULL;
1378 switch (ctrl->bRequest) {
1380 case USB_REQ_GET_DESCRIPTOR:
1381 if (ctrl->bRequestType != USB_DIR_IN)
1383 switch (w_value >> 8) {
1386 value = min (w_length, (u16) sizeof *dev->dev);
1387 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
1388 req->buf = dev->dev;
1390 case USB_DT_DEVICE_QUALIFIER:
1391 if (!dev->hs_config)
1393 value = min (w_length, (u16)
1394 sizeof (struct usb_qualifier_descriptor));
1395 make_qualifier (dev);
1397 case USB_DT_OTHER_SPEED_CONFIG:
1399 value = config_buf (dev,
1403 value = min (w_length, (u16) value);
1408 default: // all others are errors
1413 /* currently one config, two speeds */
1414 case USB_REQ_SET_CONFIGURATION:
1415 if (ctrl->bRequestType != 0)
1417 if (0 == (u8) w_value) {
1419 dev->current_config = 0;
1420 usb_gadget_vbus_draw(gadget, 8 /* mA */ );
1421 // user mode expected to disable endpoints
1425 if (gadget_is_dualspeed(gadget)
1426 && gadget->speed == USB_SPEED_HIGH) {
1427 config = dev->hs_config->bConfigurationValue;
1428 power = dev->hs_config->bMaxPower;
1430 config = dev->config->bConfigurationValue;
1431 power = dev->config->bMaxPower;
1434 if (config == (u8) w_value) {
1436 dev->current_config = config;
1437 usb_gadget_vbus_draw(gadget, 2 * power);
1441 /* report SET_CONFIGURATION like any other control request,
1442 * except that usermode may not stall this. the next
1443 * request mustn't be allowed start until this finishes:
1444 * endpoints and threads set up, etc.
1446 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1447 * has bad/racey automagic that prevents synchronizing here.
1448 * even kernel mode drivers often miss them.
1451 INFO (dev, "configuration #%d\n", dev->current_config);
1452 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
1453 if (dev->usermode_setup) {
1454 dev->setup_can_stall = 0;
1460 #ifndef CONFIG_USB_PXA25X
1461 /* PXA automagically handles this request too */
1462 case USB_REQ_GET_CONFIGURATION:
1463 if (ctrl->bRequestType != 0x80)
1465 *(u8 *)req->buf = dev->current_config;
1466 value = min (w_length, (u16) 1);
1472 VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
1473 dev->usermode_setup ? "delegate" : "fail",
1474 ctrl->bRequestType, ctrl->bRequest,
1475 w_value, le16_to_cpu(ctrl->wIndex), w_length);
1477 /* if there's an ep0 reader, don't stall */
1478 if (dev->usermode_setup) {
1479 dev->setup_can_stall = 1;
1481 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1483 dev->setup_wLength = w_length;
1484 dev->setup_out_ready = 0;
1485 dev->setup_out_error = 0;
1487 /* read DATA stage for OUT right away */
1488 if (unlikely (!dev->setup_in && w_length)) {
1489 value = setup_req (gadget->ep0, dev->req,
1495 spin_unlock (&dev->lock);
1496 value = usb_ep_queue (gadget->ep0, dev->req,
1498 spin_lock (&dev->lock);
1501 clean_req (gadget->ep0, dev->req);
1505 /* we can't currently stall these */
1506 dev->setup_can_stall = 0;
1509 /* state changes when reader collects event */
1510 event = next_event (dev, GADGETFS_SETUP);
1511 event->u.setup = *ctrl;
1513 spin_unlock (&dev->lock);
1518 /* proceed with data transfer and status phases? */
1519 if (value >= 0 && dev->state != STATE_DEV_SETUP) {
1520 req->length = value;
1521 req->zero = value < w_length;
1524 spin_unlock (&dev->lock);
1525 value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
1526 spin_lock(&dev->lock);
1528 spin_unlock(&dev->lock);
1530 DBG (dev, "ep_queue --> %d\n", value);
1536 /* device stalls when value < 0 */
1537 spin_unlock (&dev->lock);
1541 static void destroy_ep_files (struct dev_data *dev)
1543 DBG (dev, "%s %d\n", __func__, dev->state);
1545 /* dev->state must prevent interference */
1546 spin_lock_irq (&dev->lock);
1547 while (!list_empty(&dev->epfiles)) {
1549 struct inode *parent;
1550 struct dentry *dentry;
1552 /* break link to FS */
1553 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
1554 list_del_init (&ep->epfiles);
1555 spin_unlock_irq (&dev->lock);
1557 dentry = ep->dentry;
1559 parent = d_inode(dentry->d_parent);
1561 /* break link to controller */
1562 mutex_lock(&ep->lock);
1563 if (ep->state == STATE_EP_ENABLED)
1564 (void) usb_ep_disable (ep->ep);
1565 ep->state = STATE_EP_UNBOUND;
1566 usb_ep_free_request (ep->ep, ep->req);
1568 mutex_unlock(&ep->lock);
1570 wake_up (&ep->wait);
1573 /* break link to dcache */
1577 inode_unlock(parent);
1579 spin_lock_irq (&dev->lock);
1581 spin_unlock_irq (&dev->lock);
1585 static struct dentry *
1586 gadgetfs_create_file (struct super_block *sb, char const *name,
1587 void *data, const struct file_operations *fops);
1589 static int activate_ep_files (struct dev_data *dev)
1592 struct ep_data *data;
1594 gadget_for_each_ep (ep, dev->gadget) {
1596 data = kzalloc(sizeof(*data), GFP_KERNEL);
1599 data->state = STATE_EP_DISABLED;
1600 mutex_init(&data->lock);
1601 init_waitqueue_head (&data->wait);
1603 strncpy (data->name, ep->name, sizeof (data->name) - 1);
1604 refcount_set (&data->count, 1);
1609 ep->driver_data = data;
1611 data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
1615 data->dentry = gadgetfs_create_file (dev->sb, data->name,
1616 data, &ep_io_operations);
1619 list_add_tail (&data->epfiles, &dev->epfiles);
1624 usb_ep_free_request (ep, data->req);
1629 DBG (dev, "%s enomem\n", __func__);
1630 destroy_ep_files (dev);
1635 gadgetfs_unbind (struct usb_gadget *gadget)
1637 struct dev_data *dev = get_gadget_data (gadget);
1639 DBG (dev, "%s\n", __func__);
1641 spin_lock_irq (&dev->lock);
1642 dev->state = STATE_DEV_UNBOUND;
1643 while (dev->udc_usage > 0) {
1644 spin_unlock_irq(&dev->lock);
1645 usleep_range(1000, 2000);
1646 spin_lock_irq(&dev->lock);
1648 spin_unlock_irq (&dev->lock);
1650 destroy_ep_files (dev);
1651 gadget->ep0->driver_data = NULL;
1652 set_gadget_data (gadget, NULL);
1654 /* we've already been disconnected ... no i/o is active */
1656 usb_ep_free_request (gadget->ep0, dev->req);
1657 DBG (dev, "%s done\n", __func__);
1661 static struct dev_data *the_device;
1663 static int gadgetfs_bind(struct usb_gadget *gadget,
1664 struct usb_gadget_driver *driver)
1666 struct dev_data *dev = the_device;
1670 if (0 != strcmp (CHIP, gadget->name)) {
1671 pr_err("%s expected %s controller not %s\n",
1672 shortname, CHIP, gadget->name);
1676 set_gadget_data (gadget, dev);
1677 dev->gadget = gadget;
1678 gadget->ep0->driver_data = dev;
1680 /* preallocate control response and buffer */
1681 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1684 dev->req->context = NULL;
1685 dev->req->complete = epio_complete;
1687 if (activate_ep_files (dev) < 0)
1690 INFO (dev, "bound to %s driver\n", gadget->name);
1691 spin_lock_irq(&dev->lock);
1692 dev->state = STATE_DEV_UNCONNECTED;
1693 spin_unlock_irq(&dev->lock);
1698 gadgetfs_unbind (gadget);
1703 gadgetfs_disconnect (struct usb_gadget *gadget)
1705 struct dev_data *dev = get_gadget_data (gadget);
1706 unsigned long flags;
1708 spin_lock_irqsave (&dev->lock, flags);
1709 if (dev->state == STATE_DEV_UNCONNECTED)
1711 dev->state = STATE_DEV_UNCONNECTED;
1713 INFO (dev, "disconnected\n");
1714 next_event (dev, GADGETFS_DISCONNECT);
1717 spin_unlock_irqrestore (&dev->lock, flags);
1721 gadgetfs_suspend (struct usb_gadget *gadget)
1723 struct dev_data *dev = get_gadget_data (gadget);
1724 unsigned long flags;
1726 INFO (dev, "suspended from state %d\n", dev->state);
1727 spin_lock_irqsave(&dev->lock, flags);
1728 switch (dev->state) {
1729 case STATE_DEV_SETUP: // VERY odd... host died??
1730 case STATE_DEV_CONNECTED:
1731 case STATE_DEV_UNCONNECTED:
1732 next_event (dev, GADGETFS_SUSPEND);
1738 spin_unlock_irqrestore(&dev->lock, flags);
1741 static struct usb_gadget_driver gadgetfs_driver = {
1742 .function = (char *) driver_desc,
1743 .bind = gadgetfs_bind,
1744 .unbind = gadgetfs_unbind,
1745 .setup = gadgetfs_setup,
1746 .reset = gadgetfs_disconnect,
1747 .disconnect = gadgetfs_disconnect,
1748 .suspend = gadgetfs_suspend,
1755 /*----------------------------------------------------------------------*/
1756 /* DEVICE INITIALIZATION
1758 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1759 * status = write (fd, descriptors, sizeof descriptors)
1761 * That write establishes the device configuration, so the kernel can
1762 * bind to the controller ... guaranteeing it can handle enumeration
1763 * at all necessary speeds. Descriptor order is:
1765 * . message tag (u32, host order) ... for now, must be zero; it
1766 * would change to support features like multi-config devices
1767 * . full/low speed config ... all wTotalLength bytes (with interface,
1768 * class, altsetting, endpoint, and other descriptors)
1769 * . high speed config ... all descriptors, for high speed operation;
1770 * this one's optional except for high-speed hardware
1771 * . device descriptor
1773 * Endpoints are not yet enabled. Drivers must wait until device
1774 * configuration and interface altsetting changes create
1775 * the need to configure (or unconfigure) them.
1777 * After initialization, the device stays active for as long as that
1778 * $CHIP file is open. Events must then be read from that descriptor,
1779 * such as configuration notifications.
1782 static int is_valid_config(struct usb_config_descriptor *config,
1785 return config->bDescriptorType == USB_DT_CONFIG
1786 && config->bLength == USB_DT_CONFIG_SIZE
1787 && total >= USB_DT_CONFIG_SIZE
1788 && config->bConfigurationValue != 0
1789 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
1790 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
1791 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1792 /* FIXME check lengths: walk to end */
1796 dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1798 struct dev_data *dev = fd->private_data;
1799 ssize_t value, length = len;
1804 spin_lock_irq(&dev->lock);
1805 if (dev->state > STATE_DEV_OPENED) {
1806 value = ep0_write(fd, buf, len, ptr);
1807 spin_unlock_irq(&dev->lock);
1810 spin_unlock_irq(&dev->lock);
1812 if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
1813 (len > PAGE_SIZE * 4))
1816 /* we might need to change message format someday */
1817 if (copy_from_user (&tag, buf, 4))
1824 kbuf = memdup_user(buf, length);
1826 return PTR_ERR(kbuf);
1828 spin_lock_irq (&dev->lock);
1831 spin_unlock_irq(&dev->lock);
1837 /* full or low speed config */
1838 dev->config = (void *) kbuf;
1839 total = le16_to_cpu(dev->config->wTotalLength);
1840 if (!is_valid_config(dev->config, total) ||
1841 total > length - USB_DT_DEVICE_SIZE)
1846 /* optional high speed config */
1847 if (kbuf [1] == USB_DT_CONFIG) {
1848 dev->hs_config = (void *) kbuf;
1849 total = le16_to_cpu(dev->hs_config->wTotalLength);
1850 if (!is_valid_config(dev->hs_config, total) ||
1851 total > length - USB_DT_DEVICE_SIZE)
1856 dev->hs_config = NULL;
1859 /* could support multiple configs, using another encoding! */
1861 /* device descriptor (tweaked for paranoia) */
1862 if (length != USB_DT_DEVICE_SIZE)
1864 dev->dev = (void *)kbuf;
1865 if (dev->dev->bLength != USB_DT_DEVICE_SIZE
1866 || dev->dev->bDescriptorType != USB_DT_DEVICE
1867 || dev->dev->bNumConfigurations != 1)
1869 dev->dev->bcdUSB = cpu_to_le16 (0x0200);
1871 /* triggers gadgetfs_bind(); then we can enumerate. */
1872 spin_unlock_irq (&dev->lock);
1874 gadgetfs_driver.max_speed = USB_SPEED_HIGH;
1876 gadgetfs_driver.max_speed = USB_SPEED_FULL;
1878 value = usb_gadget_register_driver(&gadgetfs_driver);
1880 spin_lock_irq(&dev->lock);
1883 /* at this point "good" hardware has for the first time
1884 * let the USB the host see us. alternatively, if users
1885 * unplug/replug that will clear all the error state.
1887 * note: everything running before here was guaranteed
1888 * to choke driver model style diagnostics. from here
1889 * on, they can work ... except in cleanup paths that
1890 * kick in after the ep0 descriptor is closed.
1893 dev->gadget_registered = true;
1899 dev->hs_config = NULL;
1901 spin_unlock_irq (&dev->lock);
1902 pr_debug ("%s: %s fail %zd, %p\n", shortname, __func__, value, dev);
1909 gadget_dev_open (struct inode *inode, struct file *fd)
1911 struct dev_data *dev = inode->i_private;
1914 spin_lock_irq(&dev->lock);
1915 if (dev->state == STATE_DEV_DISABLED) {
1917 dev->state = STATE_DEV_OPENED;
1918 fd->private_data = dev;
1922 spin_unlock_irq(&dev->lock);
1926 static const struct file_operations ep0_operations = {
1927 .llseek = no_llseek,
1929 .open = gadget_dev_open,
1931 .write = dev_config,
1932 .fasync = ep0_fasync,
1934 .unlocked_ioctl = gadget_dev_ioctl,
1935 .release = dev_release,
1938 /*----------------------------------------------------------------------*/
1940 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1942 * Mounting the filesystem creates a controller file, used first for
1943 * device configuration then later for event monitoring.
1947 /* FIXME PAM etc could set this security policy without mount options
1948 * if epfiles inherited ownership and permissons from ep0 ...
1951 static unsigned default_uid;
1952 static unsigned default_gid;
1953 static unsigned default_perm = S_IRUSR | S_IWUSR;
1955 module_param (default_uid, uint, 0644);
1956 module_param (default_gid, uint, 0644);
1957 module_param (default_perm, uint, 0644);
1960 static struct inode *
1961 gadgetfs_make_inode (struct super_block *sb,
1962 void *data, const struct file_operations *fops,
1965 struct inode *inode = new_inode (sb);
1968 inode->i_ino = get_next_ino();
1969 inode->i_mode = mode;
1970 inode->i_uid = make_kuid(&init_user_ns, default_uid);
1971 inode->i_gid = make_kgid(&init_user_ns, default_gid);
1972 inode->i_atime = inode->i_mtime = inode->i_ctime
1973 = current_time(inode);
1974 inode->i_private = data;
1975 inode->i_fop = fops;
1980 /* creates in fs root directory, so non-renamable and non-linkable.
1981 * so inode and dentry are paired, until device reconfig.
1983 static struct dentry *
1984 gadgetfs_create_file (struct super_block *sb, char const *name,
1985 void *data, const struct file_operations *fops)
1987 struct dentry *dentry;
1988 struct inode *inode;
1990 dentry = d_alloc_name(sb->s_root, name);
1994 inode = gadgetfs_make_inode (sb, data, fops,
1995 S_IFREG | (default_perm & S_IRWXUGO));
2000 d_add (dentry, inode);
2004 static const struct super_operations gadget_fs_operations = {
2005 .statfs = simple_statfs,
2006 .drop_inode = generic_delete_inode,
2010 gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
2012 struct inode *inode;
2013 struct dev_data *dev;
2016 mutex_lock(&sb_mutex);
2023 CHIP = usb_get_gadget_udc_name();
2030 sb->s_blocksize = PAGE_SIZE;
2031 sb->s_blocksize_bits = PAGE_SHIFT;
2032 sb->s_magic = GADGETFS_MAGIC;
2033 sb->s_op = &gadget_fs_operations;
2034 sb->s_time_gran = 1;
2037 inode = gadgetfs_make_inode (sb,
2038 NULL, &simple_dir_operations,
2039 S_IFDIR | S_IRUGO | S_IXUGO);
2042 inode->i_op = &simple_dir_inode_operations;
2043 if (!(sb->s_root = d_make_root (inode)))
2046 /* the ep0 file is named after the controller we expect;
2047 * user mode code can use it for sanity checks, like we do.
2054 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
2060 /* other endpoint files are available after hardware setup,
2061 * from binding to a controller.
2073 mutex_unlock(&sb_mutex);
2077 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2078 static int gadgetfs_get_tree(struct fs_context *fc)
2080 return get_tree_single(fc, gadgetfs_fill_super);
2083 static const struct fs_context_operations gadgetfs_context_ops = {
2084 .get_tree = gadgetfs_get_tree,
2087 static int gadgetfs_init_fs_context(struct fs_context *fc)
2089 fc->ops = &gadgetfs_context_ops;
2094 gadgetfs_kill_sb (struct super_block *sb)
2096 mutex_lock(&sb_mutex);
2097 kill_litter_super (sb);
2099 put_dev (the_device);
2104 mutex_unlock(&sb_mutex);
2107 /*----------------------------------------------------------------------*/
2109 static struct file_system_type gadgetfs_type = {
2110 .owner = THIS_MODULE,
2112 .init_fs_context = gadgetfs_init_fs_context,
2113 .kill_sb = gadgetfs_kill_sb,
2115 MODULE_ALIAS_FS("gadgetfs");
2117 /*----------------------------------------------------------------------*/
2119 static int __init gadgetfs_init (void)
2123 status = register_filesystem (&gadgetfs_type);
2125 pr_info ("%s: %s, version " DRIVER_VERSION "\n",
2126 shortname, driver_desc);
2129 module_init (gadgetfs_init);
2131 static void __exit gadgetfs_cleanup (void)
2133 pr_debug ("unregister %s\n", shortname);
2134 unregister_filesystem (&gadgetfs_type);
2136 module_exit (gadgetfs_cleanup);