2 * inode.c -- user mode filesystem api for usb gadget controllers
4 * Copyright (C) 2003-2004 David Brownell
5 * Copyright (C) 2003 Agilent Technologies
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 /* #define VERBOSE_DEBUG */
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/uts.h>
21 #include <linux/wait.h>
22 #include <linux/compiler.h>
23 #include <asm/uaccess.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/poll.h>
27 #include <linux/mmu_context.h>
28 #include <linux/aio.h>
30 #include <linux/device.h>
31 #include <linux/moduleparam.h>
33 #include <linux/usb/gadgetfs.h>
34 #include <linux/usb/gadget.h>
38 * The gadgetfs API maps each endpoint to a file descriptor so that you
39 * can use standard synchronous read/write calls for I/O. There's some
40 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
41 * drivers show how this works in practice. You can also use AIO to
42 * eliminate I/O gaps between requests, to help when streaming data.
44 * Key parts that must be USB-specific are protocols defining how the
45 * read/write operations relate to the hardware state machines. There
46 * are two types of files. One type is for the device, implementing ep0.
47 * The other type is for each IN or OUT endpoint. In both cases, the
48 * user mode driver must configure the hardware before using it.
50 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
51 * (by writing configuration and device descriptors). Afterwards it
52 * may serve as a source of device events, used to handle all control
53 * requests other than basic enumeration.
55 * - Then, after a SET_CONFIGURATION control request, ep_config() is
56 * called when each /dev/gadget/ep* file is configured (by writing
57 * endpoint descriptors). Afterwards these files are used to write()
58 * IN data or to read() OUT data. To halt the endpoint, a "wrong
59 * direction" request is issued (like reading an IN endpoint).
61 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
62 * not possible on all hardware. For example, precise fault handling with
63 * respect to data left in endpoint fifos after aborted operations; or
64 * selective clearing of endpoint halts, to implement SET_INTERFACE.
67 #define DRIVER_DESC "USB Gadget filesystem"
68 #define DRIVER_VERSION "24 Aug 2004"
70 static const char driver_desc
[] = DRIVER_DESC
;
71 static const char shortname
[] = "gadgetfs";
73 MODULE_DESCRIPTION (DRIVER_DESC
);
74 MODULE_AUTHOR ("David Brownell");
75 MODULE_LICENSE ("GPL");
77 static int ep_open(struct inode
*, struct file
*);
80 /*----------------------------------------------------------------------*/
82 #define GADGETFS_MAGIC 0xaee71ee7
84 /* /dev/gadget/$CHIP represents ep0 and the whole device */
86 /* DISBLED is the initial state.
88 STATE_DEV_DISABLED
= 0,
90 /* Only one open() of /dev/gadget/$CHIP; only one file tracks
91 * ep0/device i/o modes and binding to the controller. Driver
92 * must always write descriptors to initialize the device, then
93 * the device becomes UNCONNECTED until enumeration.
97 /* From then on, ep0 fd is in either of two basic modes:
98 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
99 * - SETUP: read/write will transfer control data and succeed;
100 * or if "wrong direction", performs protocol stall
102 STATE_DEV_UNCONNECTED
,
106 /* UNBOUND means the driver closed ep0, so the device won't be
107 * accessible again (DEV_DISABLED) until all fds are closed.
112 /* enough for the whole queue: most events invalidate others */
118 enum ep0_state state
; /* P: lock */
119 struct usb_gadgetfs_event event
[N_EVENT
];
121 struct fasync_struct
*fasync
;
124 /* drivers reading ep0 MUST handle control requests (SETUP)
125 * reported that way; else the host will time out.
127 unsigned usermode_setup
: 1,
133 unsigned setup_wLength
;
135 /* the rest is basically write-once */
136 struct usb_config_descriptor
*config
, *hs_config
;
137 struct usb_device_descriptor
*dev
;
138 struct usb_request
*req
;
139 struct usb_gadget
*gadget
;
140 struct list_head epfiles
;
142 wait_queue_head_t wait
;
143 struct super_block
*sb
;
144 struct dentry
*dentry
;
146 /* except this scratch i/o buffer for ep0 */
150 static inline void get_dev (struct dev_data
*data
)
152 atomic_inc (&data
->count
);
155 static void put_dev (struct dev_data
*data
)
157 if (likely (!atomic_dec_and_test (&data
->count
)))
159 /* needs no more cleanup */
160 BUG_ON (waitqueue_active (&data
->wait
));
164 static struct dev_data
*dev_new (void)
166 struct dev_data
*dev
;
168 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
171 dev
->state
= STATE_DEV_DISABLED
;
172 atomic_set (&dev
->count
, 1);
173 spin_lock_init (&dev
->lock
);
174 INIT_LIST_HEAD (&dev
->epfiles
);
175 init_waitqueue_head (&dev
->wait
);
179 /*----------------------------------------------------------------------*/
181 /* other /dev/gadget/$ENDPOINT files represent endpoints */
183 STATE_EP_DISABLED
= 0,
193 struct dev_data
*dev
;
194 /* must hold dev->lock before accessing ep or req */
196 struct usb_request
*req
;
199 struct usb_endpoint_descriptor desc
, hs_desc
;
200 struct list_head epfiles
;
201 wait_queue_head_t wait
;
202 struct dentry
*dentry
;
205 static inline void get_ep (struct ep_data
*data
)
207 atomic_inc (&data
->count
);
210 static void put_ep (struct ep_data
*data
)
212 if (likely (!atomic_dec_and_test (&data
->count
)))
215 /* needs no more cleanup */
216 BUG_ON (!list_empty (&data
->epfiles
));
217 BUG_ON (waitqueue_active (&data
->wait
));
221 /*----------------------------------------------------------------------*/
223 /* most "how to use the hardware" policy choices are in userspace:
224 * mapping endpoint roles (which the driver needs) to the capabilities
225 * which the usb controller has. most of those capabilities are exposed
226 * implicitly, starting with the driver name and then endpoint names.
229 static const char *CHIP
;
231 /*----------------------------------------------------------------------*/
233 /* NOTE: don't use dev_printk calls before binding to the gadget
234 * at the end of ep0 configuration, or after unbind.
237 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
238 #define xprintk(d,level,fmt,args...) \
239 printk(level "%s: " fmt , shortname , ## args)
242 #define DBG(dev,fmt,args...) \
243 xprintk(dev , KERN_DEBUG , fmt , ## args)
245 #define DBG(dev,fmt,args...) \
252 #define VDEBUG(dev,fmt,args...) \
256 #define ERROR(dev,fmt,args...) \
257 xprintk(dev , KERN_ERR , fmt , ## args)
258 #define INFO(dev,fmt,args...) \
259 xprintk(dev , KERN_INFO , fmt , ## args)
262 /*----------------------------------------------------------------------*/
264 /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
266 * After opening, configure non-control endpoints. Then use normal
267 * stream read() and write() requests; and maybe ioctl() to get more
268 * precise FIFO status when recovering from cancellation.
271 static void epio_complete (struct usb_ep
*ep
, struct usb_request
*req
)
273 struct ep_data
*epdata
= ep
->driver_data
;
278 epdata
->status
= req
->status
;
280 epdata
->status
= req
->actual
;
281 complete ((struct completion
*)req
->context
);
284 /* tasklock endpoint, returning when it's connected.
285 * still need dev->lock to use epdata->ep.
288 get_ready_ep (unsigned f_flags
, struct ep_data
*epdata
, bool is_write
)
292 if (f_flags
& O_NONBLOCK
) {
293 if (!mutex_trylock(&epdata
->lock
))
295 if (epdata
->state
!= STATE_EP_ENABLED
&&
296 (!is_write
|| epdata
->state
!= STATE_EP_READY
)) {
297 mutex_unlock(&epdata
->lock
);
305 val
= mutex_lock_interruptible(&epdata
->lock
);
309 switch (epdata
->state
) {
310 case STATE_EP_ENABLED
:
312 case STATE_EP_READY
: /* not configured yet */
316 case STATE_EP_UNBOUND
: /* clean disconnect */
318 // case STATE_EP_DISABLED: /* "can't happen" */
319 default: /* error! */
320 pr_debug ("%s: ep %p not available, state %d\n",
321 shortname
, epdata
, epdata
->state
);
323 mutex_unlock(&epdata
->lock
);
328 ep_io (struct ep_data
*epdata
, void *buf
, unsigned len
)
330 DECLARE_COMPLETION_ONSTACK (done
);
333 spin_lock_irq (&epdata
->dev
->lock
);
334 if (likely (epdata
->ep
!= NULL
)) {
335 struct usb_request
*req
= epdata
->req
;
337 req
->context
= &done
;
338 req
->complete
= epio_complete
;
341 value
= usb_ep_queue (epdata
->ep
, req
, GFP_ATOMIC
);
344 spin_unlock_irq (&epdata
->dev
->lock
);
346 if (likely (value
== 0)) {
347 value
= wait_event_interruptible (done
.wait
, done
.done
);
349 spin_lock_irq (&epdata
->dev
->lock
);
350 if (likely (epdata
->ep
!= NULL
)) {
351 DBG (epdata
->dev
, "%s i/o interrupted\n",
353 usb_ep_dequeue (epdata
->ep
, epdata
->req
);
354 spin_unlock_irq (&epdata
->dev
->lock
);
356 wait_event (done
.wait
, done
.done
);
357 if (epdata
->status
== -ECONNRESET
)
358 epdata
->status
= -EINTR
;
360 spin_unlock_irq (&epdata
->dev
->lock
);
362 DBG (epdata
->dev
, "endpoint gone\n");
363 epdata
->status
= -ENODEV
;
366 return epdata
->status
;
372 ep_release (struct inode
*inode
, struct file
*fd
)
374 struct ep_data
*data
= fd
->private_data
;
377 value
= mutex_lock_interruptible(&data
->lock
);
381 /* clean up if this can be reopened */
382 if (data
->state
!= STATE_EP_UNBOUND
) {
383 data
->state
= STATE_EP_DISABLED
;
384 data
->desc
.bDescriptorType
= 0;
385 data
->hs_desc
.bDescriptorType
= 0;
386 usb_ep_disable(data
->ep
);
388 mutex_unlock(&data
->lock
);
393 static long ep_ioctl(struct file
*fd
, unsigned code
, unsigned long value
)
395 struct ep_data
*data
= fd
->private_data
;
398 if ((status
= get_ready_ep (fd
->f_flags
, data
, false)) < 0)
401 spin_lock_irq (&data
->dev
->lock
);
402 if (likely (data
->ep
!= NULL
)) {
404 case GADGETFS_FIFO_STATUS
:
405 status
= usb_ep_fifo_status (data
->ep
);
407 case GADGETFS_FIFO_FLUSH
:
408 usb_ep_fifo_flush (data
->ep
);
410 case GADGETFS_CLEAR_HALT
:
411 status
= usb_ep_clear_halt (data
->ep
);
418 spin_unlock_irq (&data
->dev
->lock
);
419 mutex_unlock(&data
->lock
);
423 /*----------------------------------------------------------------------*/
425 /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
428 struct usb_request
*req
;
429 struct ep_data
*epdata
;
431 struct mm_struct
*mm
;
432 struct work_struct work
;
439 static int ep_aio_cancel(struct kiocb
*iocb
)
441 struct kiocb_priv
*priv
= iocb
->private;
442 struct ep_data
*epdata
;
446 epdata
= priv
->epdata
;
447 // spin_lock(&epdata->dev->lock);
448 if (likely(epdata
&& epdata
->ep
&& priv
->req
))
449 value
= usb_ep_dequeue (epdata
->ep
, priv
->req
);
452 // spin_unlock(&epdata->dev->lock);
458 static void ep_user_copy_worker(struct work_struct
*work
)
460 struct kiocb_priv
*priv
= container_of(work
, struct kiocb_priv
, work
);
461 struct mm_struct
*mm
= priv
->mm
;
462 struct kiocb
*iocb
= priv
->iocb
;
466 ret
= copy_to_iter(priv
->buf
, priv
->actual
, &priv
->to
);
471 /* completing the iocb can drop the ctx and mm, don't touch mm after */
472 aio_complete(iocb
, ret
, ret
);
475 kfree(priv
->to_free
);
479 static void ep_aio_complete(struct usb_ep
*ep
, struct usb_request
*req
)
481 struct kiocb
*iocb
= req
->context
;
482 struct kiocb_priv
*priv
= iocb
->private;
483 struct ep_data
*epdata
= priv
->epdata
;
485 /* lock against disconnect (and ideally, cancel) */
486 spin_lock(&epdata
->dev
->lock
);
490 /* if this was a write or a read returning no data then we
491 * don't need to copy anything to userspace, so we can
492 * complete the aio request immediately.
494 if (priv
->to_free
== NULL
|| unlikely(req
->actual
== 0)) {
496 kfree(priv
->to_free
);
498 iocb
->private = NULL
;
499 /* aio_complete() reports bytes-transferred _and_ faults */
500 aio_complete(iocb
, req
->actual
? req
->actual
: req
->status
,
503 /* ep_copy_to_user() won't report both; we hide some faults */
504 if (unlikely(0 != req
->status
))
505 DBG(epdata
->dev
, "%s fault %d len %d\n",
506 ep
->name
, req
->status
, req
->actual
);
508 priv
->buf
= req
->buf
;
509 priv
->actual
= req
->actual
;
510 INIT_WORK(&priv
->work
, ep_user_copy_worker
);
511 schedule_work(&priv
->work
);
513 spin_unlock(&epdata
->dev
->lock
);
515 usb_ep_free_request(ep
, req
);
519 static ssize_t
ep_aio(struct kiocb
*iocb
,
520 struct kiocb_priv
*priv
,
521 struct ep_data
*epdata
,
525 struct usb_request
*req
;
528 iocb
->private = priv
;
531 kiocb_set_cancel_fn(iocb
, ep_aio_cancel
);
533 priv
->epdata
= epdata
;
535 priv
->mm
= current
->mm
; /* mm teardown waits for iocbs in exit_aio() */
537 /* each kiocb is coupled to one usb_request, but we can't
538 * allocate or submit those if the host disconnected.
540 spin_lock_irq(&epdata
->dev
->lock
);
542 if (unlikely(epdata
->ep
))
545 req
= usb_ep_alloc_request(epdata
->ep
, GFP_ATOMIC
);
553 req
->complete
= ep_aio_complete
;
555 value
= usb_ep_queue(epdata
->ep
, req
, GFP_ATOMIC
);
556 if (unlikely(0 != value
)) {
557 usb_ep_free_request(epdata
->ep
, req
);
560 spin_unlock_irq(&epdata
->dev
->lock
);
564 spin_unlock_irq(&epdata
->dev
->lock
);
565 kfree(priv
->to_free
);
572 ep_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
574 struct file
*file
= iocb
->ki_filp
;
575 struct ep_data
*epdata
= file
->private_data
;
576 size_t len
= iov_iter_count(to
);
580 if ((value
= get_ready_ep(file
->f_flags
, epdata
, false)) < 0)
583 /* halt any endpoint by doing a "wrong direction" i/o call */
584 if (usb_endpoint_dir_in(&epdata
->desc
)) {
585 if (usb_endpoint_xfer_isoc(&epdata
->desc
) ||
586 !is_sync_kiocb(iocb
)) {
587 mutex_unlock(&epdata
->lock
);
590 DBG (epdata
->dev
, "%s halt\n", epdata
->name
);
591 spin_lock_irq(&epdata
->dev
->lock
);
592 if (likely(epdata
->ep
!= NULL
))
593 usb_ep_set_halt(epdata
->ep
);
594 spin_unlock_irq(&epdata
->dev
->lock
);
595 mutex_unlock(&epdata
->lock
);
599 buf
= kmalloc(len
, GFP_KERNEL
);
600 if (unlikely(!buf
)) {
601 mutex_unlock(&epdata
->lock
);
604 if (is_sync_kiocb(iocb
)) {
605 value
= ep_io(epdata
, buf
, len
);
606 if (value
>= 0 && copy_to_iter(buf
, value
, to
))
609 struct kiocb_priv
*priv
= kzalloc(sizeof *priv
, GFP_KERNEL
);
613 priv
->to_free
= dup_iter(&priv
->to
, to
, GFP_KERNEL
);
614 if (!priv
->to_free
) {
618 value
= ep_aio(iocb
, priv
, epdata
, buf
, len
);
619 if (value
== -EIOCBQUEUED
)
624 mutex_unlock(&epdata
->lock
);
628 static ssize_t
ep_config(struct ep_data
*, const char *, size_t);
631 ep_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
633 struct file
*file
= iocb
->ki_filp
;
634 struct ep_data
*epdata
= file
->private_data
;
635 size_t len
= iov_iter_count(from
);
640 if ((value
= get_ready_ep(file
->f_flags
, epdata
, true)) < 0)
643 configured
= epdata
->state
== STATE_EP_ENABLED
;
645 /* halt any endpoint by doing a "wrong direction" i/o call */
646 if (configured
&& !usb_endpoint_dir_in(&epdata
->desc
)) {
647 if (usb_endpoint_xfer_isoc(&epdata
->desc
) ||
648 !is_sync_kiocb(iocb
)) {
649 mutex_unlock(&epdata
->lock
);
652 DBG (epdata
->dev
, "%s halt\n", epdata
->name
);
653 spin_lock_irq(&epdata
->dev
->lock
);
654 if (likely(epdata
->ep
!= NULL
))
655 usb_ep_set_halt(epdata
->ep
);
656 spin_unlock_irq(&epdata
->dev
->lock
);
657 mutex_unlock(&epdata
->lock
);
661 buf
= kmalloc(len
, GFP_KERNEL
);
662 if (unlikely(!buf
)) {
663 mutex_unlock(&epdata
->lock
);
667 if (unlikely(copy_from_iter(buf
, len
, from
) != len
)) {
672 if (unlikely(!configured
)) {
673 value
= ep_config(epdata
, buf
, len
);
674 } else if (is_sync_kiocb(iocb
)) {
675 value
= ep_io(epdata
, buf
, len
);
677 struct kiocb_priv
*priv
= kzalloc(sizeof *priv
, GFP_KERNEL
);
680 value
= ep_aio(iocb
, priv
, epdata
, buf
, len
);
681 if (value
== -EIOCBQUEUED
)
687 mutex_unlock(&epdata
->lock
);
691 /*----------------------------------------------------------------------*/
693 /* used after endpoint configuration */
694 static const struct file_operations ep_io_operations
= {
695 .owner
= THIS_MODULE
,
698 .release
= ep_release
,
700 .read
= new_sync_read
,
701 .write
= new_sync_write
,
702 .unlocked_ioctl
= ep_ioctl
,
703 .read_iter
= ep_read_iter
,
704 .write_iter
= ep_write_iter
,
707 /* ENDPOINT INITIALIZATION
709 * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
710 * status = write (fd, descriptors, sizeof descriptors)
712 * That write establishes the endpoint configuration, configuring
713 * the controller to process bulk, interrupt, or isochronous transfers
714 * at the right maxpacket size, and so on.
716 * The descriptors are message type 1, identified by a host order u32
717 * at the beginning of what's written. Descriptor order is: full/low
718 * speed descriptor, then optional high speed descriptor.
721 ep_config (struct ep_data
*data
, const char *buf
, size_t len
)
725 int value
, length
= len
;
727 if (data
->state
!= STATE_EP_READY
) {
733 if (len
< USB_DT_ENDPOINT_SIZE
+ 4)
736 /* we might need to change message format someday */
737 memcpy(&tag
, buf
, 4);
739 DBG(data
->dev
, "config %s, bad tag %d\n", data
->name
, tag
);
745 /* NOTE: audio endpoint extensions not accepted here;
746 * just don't include the extra bytes.
749 /* full/low speed descriptor, then high speed */
750 memcpy(&data
->desc
, buf
, USB_DT_ENDPOINT_SIZE
);
751 if (data
->desc
.bLength
!= USB_DT_ENDPOINT_SIZE
752 || data
->desc
.bDescriptorType
!= USB_DT_ENDPOINT
)
754 if (len
!= USB_DT_ENDPOINT_SIZE
) {
755 if (len
!= 2 * USB_DT_ENDPOINT_SIZE
)
757 memcpy(&data
->hs_desc
, buf
+ USB_DT_ENDPOINT_SIZE
,
758 USB_DT_ENDPOINT_SIZE
);
759 if (data
->hs_desc
.bLength
!= USB_DT_ENDPOINT_SIZE
760 || data
->hs_desc
.bDescriptorType
761 != USB_DT_ENDPOINT
) {
762 DBG(data
->dev
, "config %s, bad hs length or type\n",
768 spin_lock_irq (&data
->dev
->lock
);
769 if (data
->dev
->state
== STATE_DEV_UNBOUND
) {
772 } else if ((ep
= data
->ep
) == NULL
) {
776 switch (data
->dev
->gadget
->speed
) {
779 ep
->desc
= &data
->desc
;
782 /* fails if caller didn't provide that descriptor... */
783 ep
->desc
= &data
->hs_desc
;
786 DBG(data
->dev
, "unconnected, %s init abandoned\n",
791 value
= usb_ep_enable(ep
);
793 data
->state
= STATE_EP_ENABLED
;
797 spin_unlock_irq (&data
->dev
->lock
);
800 data
->desc
.bDescriptorType
= 0;
801 data
->hs_desc
.bDescriptorType
= 0;
810 ep_open (struct inode
*inode
, struct file
*fd
)
812 struct ep_data
*data
= inode
->i_private
;
815 if (mutex_lock_interruptible(&data
->lock
) != 0)
817 spin_lock_irq (&data
->dev
->lock
);
818 if (data
->dev
->state
== STATE_DEV_UNBOUND
)
820 else if (data
->state
== STATE_EP_DISABLED
) {
822 data
->state
= STATE_EP_READY
;
824 fd
->private_data
= data
;
825 VDEBUG (data
->dev
, "%s ready\n", data
->name
);
827 DBG (data
->dev
, "%s state %d\n",
828 data
->name
, data
->state
);
829 spin_unlock_irq (&data
->dev
->lock
);
830 mutex_unlock(&data
->lock
);
834 /*----------------------------------------------------------------------*/
836 /* EP0 IMPLEMENTATION can be partly in userspace.
838 * Drivers that use this facility receive various events, including
839 * control requests the kernel doesn't handle. Drivers that don't
840 * use this facility may be too simple-minded for real applications.
843 static inline void ep0_readable (struct dev_data
*dev
)
845 wake_up (&dev
->wait
);
846 kill_fasync (&dev
->fasync
, SIGIO
, POLL_IN
);
849 static void clean_req (struct usb_ep
*ep
, struct usb_request
*req
)
851 struct dev_data
*dev
= ep
->driver_data
;
853 if (req
->buf
!= dev
->rbuf
) {
855 req
->buf
= dev
->rbuf
;
857 req
->complete
= epio_complete
;
858 dev
->setup_out_ready
= 0;
861 static void ep0_complete (struct usb_ep
*ep
, struct usb_request
*req
)
863 struct dev_data
*dev
= ep
->driver_data
;
867 /* for control OUT, data must still get to userspace */
868 spin_lock_irqsave(&dev
->lock
, flags
);
869 if (!dev
->setup_in
) {
870 dev
->setup_out_error
= (req
->status
!= 0);
871 if (!dev
->setup_out_error
)
873 dev
->setup_out_ready
= 1;
877 /* clean up as appropriate */
878 if (free
&& req
->buf
!= &dev
->rbuf
)
880 req
->complete
= epio_complete
;
881 spin_unlock_irqrestore(&dev
->lock
, flags
);
884 static int setup_req (struct usb_ep
*ep
, struct usb_request
*req
, u16 len
)
886 struct dev_data
*dev
= ep
->driver_data
;
888 if (dev
->setup_out_ready
) {
889 DBG (dev
, "ep0 request busy!\n");
892 if (len
> sizeof (dev
->rbuf
))
893 req
->buf
= kmalloc(len
, GFP_ATOMIC
);
894 if (req
->buf
== NULL
) {
895 req
->buf
= dev
->rbuf
;
898 req
->complete
= ep0_complete
;
905 ep0_read (struct file
*fd
, char __user
*buf
, size_t len
, loff_t
*ptr
)
907 struct dev_data
*dev
= fd
->private_data
;
909 enum ep0_state state
;
911 spin_lock_irq (&dev
->lock
);
912 if (dev
->state
<= STATE_DEV_OPENED
) {
917 /* report fd mode change before acting on it */
918 if (dev
->setup_abort
) {
919 dev
->setup_abort
= 0;
924 /* control DATA stage */
925 if ((state
= dev
->state
) == STATE_DEV_SETUP
) {
927 if (dev
->setup_in
) { /* stall IN */
928 VDEBUG(dev
, "ep0in stall\n");
929 (void) usb_ep_set_halt (dev
->gadget
->ep0
);
931 dev
->state
= STATE_DEV_CONNECTED
;
933 } else if (len
== 0) { /* ack SET_CONFIGURATION etc */
934 struct usb_ep
*ep
= dev
->gadget
->ep0
;
935 struct usb_request
*req
= dev
->req
;
937 if ((retval
= setup_req (ep
, req
, 0)) == 0)
938 retval
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
939 dev
->state
= STATE_DEV_CONNECTED
;
941 /* assume that was SET_CONFIGURATION */
942 if (dev
->current_config
) {
945 if (gadget_is_dualspeed(dev
->gadget
)
946 && (dev
->gadget
->speed
948 power
= dev
->hs_config
->bMaxPower
;
950 power
= dev
->config
->bMaxPower
;
951 usb_gadget_vbus_draw(dev
->gadget
, 2 * power
);
954 } else { /* collect OUT data */
955 if ((fd
->f_flags
& O_NONBLOCK
) != 0
956 && !dev
->setup_out_ready
) {
960 spin_unlock_irq (&dev
->lock
);
961 retval
= wait_event_interruptible (dev
->wait
,
962 dev
->setup_out_ready
!= 0);
964 /* FIXME state could change from under us */
965 spin_lock_irq (&dev
->lock
);
969 if (dev
->state
!= STATE_DEV_SETUP
) {
973 dev
->state
= STATE_DEV_CONNECTED
;
975 if (dev
->setup_out_error
)
978 len
= min (len
, (size_t)dev
->req
->actual
);
979 // FIXME don't call this with the spinlock held ...
980 if (copy_to_user (buf
, dev
->req
->buf
, len
))
984 clean_req (dev
->gadget
->ep0
, dev
->req
);
985 /* NOTE userspace can't yet choose to stall */
991 /* else normal: return event data */
992 if (len
< sizeof dev
->event
[0]) {
996 len
-= len
% sizeof (struct usb_gadgetfs_event
);
997 dev
->usermode_setup
= 1;
1000 /* return queued events right away */
1001 if (dev
->ev_next
!= 0) {
1004 n
= len
/ sizeof (struct usb_gadgetfs_event
);
1005 if (dev
->ev_next
< n
)
1008 /* ep0 i/o has special semantics during STATE_DEV_SETUP */
1009 for (i
= 0; i
< n
; i
++) {
1010 if (dev
->event
[i
].type
== GADGETFS_SETUP
) {
1011 dev
->state
= STATE_DEV_SETUP
;
1016 spin_unlock_irq (&dev
->lock
);
1017 len
= n
* sizeof (struct usb_gadgetfs_event
);
1018 if (copy_to_user (buf
, &dev
->event
, len
))
1023 /* NOTE this doesn't guard against broken drivers;
1024 * concurrent ep0 readers may lose events.
1026 spin_lock_irq (&dev
->lock
);
1027 if (dev
->ev_next
> n
) {
1028 memmove(&dev
->event
[0], &dev
->event
[n
],
1029 sizeof (struct usb_gadgetfs_event
)
1030 * (dev
->ev_next
- n
));
1033 spin_unlock_irq (&dev
->lock
);
1037 if (fd
->f_flags
& O_NONBLOCK
) {
1044 DBG (dev
, "fail %s, state %d\n", __func__
, state
);
1047 case STATE_DEV_UNCONNECTED
:
1048 case STATE_DEV_CONNECTED
:
1049 spin_unlock_irq (&dev
->lock
);
1050 DBG (dev
, "%s wait\n", __func__
);
1052 /* wait for events */
1053 retval
= wait_event_interruptible (dev
->wait
,
1057 spin_lock_irq (&dev
->lock
);
1062 spin_unlock_irq (&dev
->lock
);
1066 static struct usb_gadgetfs_event
*
1067 next_event (struct dev_data
*dev
, enum usb_gadgetfs_event_type type
)
1069 struct usb_gadgetfs_event
*event
;
1073 /* these events purge the queue */
1074 case GADGETFS_DISCONNECT
:
1075 if (dev
->state
== STATE_DEV_SETUP
)
1076 dev
->setup_abort
= 1;
1078 case GADGETFS_CONNECT
:
1081 case GADGETFS_SETUP
: /* previous request timed out */
1082 case GADGETFS_SUSPEND
: /* same effect */
1083 /* these events can't be repeated */
1084 for (i
= 0; i
!= dev
->ev_next
; i
++) {
1085 if (dev
->event
[i
].type
!= type
)
1087 DBG(dev
, "discard old event[%d] %d\n", i
, type
);
1089 if (i
== dev
->ev_next
)
1091 /* indices start at zero, for simplicity */
1092 memmove (&dev
->event
[i
], &dev
->event
[i
+ 1],
1093 sizeof (struct usb_gadgetfs_event
)
1094 * (dev
->ev_next
- i
));
1100 VDEBUG(dev
, "event[%d] = %d\n", dev
->ev_next
, type
);
1101 event
= &dev
->event
[dev
->ev_next
++];
1102 BUG_ON (dev
->ev_next
> N_EVENT
);
1103 memset (event
, 0, sizeof *event
);
1109 ep0_write (struct file
*fd
, const char __user
*buf
, size_t len
, loff_t
*ptr
)
1111 struct dev_data
*dev
= fd
->private_data
;
1112 ssize_t retval
= -ESRCH
;
1114 /* report fd mode change before acting on it */
1115 if (dev
->setup_abort
) {
1116 dev
->setup_abort
= 0;
1119 /* data and/or status stage for control request */
1120 } else if (dev
->state
== STATE_DEV_SETUP
) {
1122 /* IN DATA+STATUS caller makes len <= wLength */
1123 if (dev
->setup_in
) {
1124 retval
= setup_req (dev
->gadget
->ep0
, dev
->req
, len
);
1126 dev
->state
= STATE_DEV_CONNECTED
;
1127 spin_unlock_irq (&dev
->lock
);
1128 if (copy_from_user (dev
->req
->buf
, buf
, len
))
1131 if (len
< dev
->setup_wLength
)
1133 retval
= usb_ep_queue (
1134 dev
->gadget
->ep0
, dev
->req
,
1138 spin_lock_irq (&dev
->lock
);
1139 clean_req (dev
->gadget
->ep0
, dev
->req
);
1140 spin_unlock_irq (&dev
->lock
);
1147 /* can stall some OUT transfers */
1148 } else if (dev
->setup_can_stall
) {
1149 VDEBUG(dev
, "ep0out stall\n");
1150 (void) usb_ep_set_halt (dev
->gadget
->ep0
);
1152 dev
->state
= STATE_DEV_CONNECTED
;
1154 DBG(dev
, "bogus ep0out stall!\n");
1157 DBG (dev
, "fail %s, state %d\n", __func__
, dev
->state
);
1163 ep0_fasync (int f
, struct file
*fd
, int on
)
1165 struct dev_data
*dev
= fd
->private_data
;
1166 // caller must F_SETOWN before signal delivery happens
1167 VDEBUG (dev
, "%s %s\n", __func__
, on
? "on" : "off");
1168 return fasync_helper (f
, fd
, on
, &dev
->fasync
);
1171 static struct usb_gadget_driver gadgetfs_driver
;
1174 dev_release (struct inode
*inode
, struct file
*fd
)
1176 struct dev_data
*dev
= fd
->private_data
;
1178 /* closing ep0 === shutdown all */
1180 usb_gadget_unregister_driver (&gadgetfs_driver
);
1182 /* at this point "good" hardware has disconnected the
1183 * device from USB; the host won't see it any more.
1184 * alternatively, all host requests will time out.
1190 /* other endpoints were all decoupled from this device */
1191 spin_lock_irq(&dev
->lock
);
1192 dev
->state
= STATE_DEV_DISABLED
;
1193 spin_unlock_irq(&dev
->lock
);
1200 ep0_poll (struct file
*fd
, poll_table
*wait
)
1202 struct dev_data
*dev
= fd
->private_data
;
1205 if (dev
->state
<= STATE_DEV_OPENED
)
1206 return DEFAULT_POLLMASK
;
1208 poll_wait(fd
, &dev
->wait
, wait
);
1210 spin_lock_irq (&dev
->lock
);
1212 /* report fd mode change before acting on it */
1213 if (dev
->setup_abort
) {
1214 dev
->setup_abort
= 0;
1219 if (dev
->state
== STATE_DEV_SETUP
) {
1220 if (dev
->setup_in
|| dev
->setup_can_stall
)
1223 if (dev
->ev_next
!= 0)
1227 spin_unlock_irq(&dev
->lock
);
1231 static long dev_ioctl (struct file
*fd
, unsigned code
, unsigned long value
)
1233 struct dev_data
*dev
= fd
->private_data
;
1234 struct usb_gadget
*gadget
= dev
->gadget
;
1237 if (gadget
->ops
->ioctl
)
1238 ret
= gadget
->ops
->ioctl (gadget
, code
, value
);
1243 /*----------------------------------------------------------------------*/
1245 /* The in-kernel gadget driver handles most ep0 issues, in particular
1246 * enumerating the single configuration (as provided from user space).
1248 * Unrecognized ep0 requests may be handled in user space.
1251 static void make_qualifier (struct dev_data
*dev
)
1253 struct usb_qualifier_descriptor qual
;
1254 struct usb_device_descriptor
*desc
;
1256 qual
.bLength
= sizeof qual
;
1257 qual
.bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
1258 qual
.bcdUSB
= cpu_to_le16 (0x0200);
1261 qual
.bDeviceClass
= desc
->bDeviceClass
;
1262 qual
.bDeviceSubClass
= desc
->bDeviceSubClass
;
1263 qual
.bDeviceProtocol
= desc
->bDeviceProtocol
;
1265 /* assumes ep0 uses the same value for both speeds ... */
1266 qual
.bMaxPacketSize0
= dev
->gadget
->ep0
->maxpacket
;
1268 qual
.bNumConfigurations
= 1;
1271 memcpy (dev
->rbuf
, &qual
, sizeof qual
);
1275 config_buf (struct dev_data
*dev
, u8 type
, unsigned index
)
1280 /* only one configuration */
1284 if (gadget_is_dualspeed(dev
->gadget
)) {
1285 hs
= (dev
->gadget
->speed
== USB_SPEED_HIGH
);
1286 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
1290 dev
->req
->buf
= dev
->hs_config
;
1291 len
= le16_to_cpu(dev
->hs_config
->wTotalLength
);
1293 dev
->req
->buf
= dev
->config
;
1294 len
= le16_to_cpu(dev
->config
->wTotalLength
);
1296 ((u8
*)dev
->req
->buf
) [1] = type
;
1301 gadgetfs_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1303 struct dev_data
*dev
= get_gadget_data (gadget
);
1304 struct usb_request
*req
= dev
->req
;
1305 int value
= -EOPNOTSUPP
;
1306 struct usb_gadgetfs_event
*event
;
1307 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1308 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1310 spin_lock (&dev
->lock
);
1311 dev
->setup_abort
= 0;
1312 if (dev
->state
== STATE_DEV_UNCONNECTED
) {
1313 if (gadget_is_dualspeed(gadget
)
1314 && gadget
->speed
== USB_SPEED_HIGH
1315 && dev
->hs_config
== NULL
) {
1316 spin_unlock(&dev
->lock
);
1317 ERROR (dev
, "no high speed config??\n");
1321 dev
->state
= STATE_DEV_CONNECTED
;
1323 INFO (dev
, "connected\n");
1324 event
= next_event (dev
, GADGETFS_CONNECT
);
1325 event
->u
.speed
= gadget
->speed
;
1328 /* host may have given up waiting for response. we can miss control
1329 * requests handled lower down (device/endpoint status and features);
1330 * then ep0_{read,write} will report the wrong status. controller
1331 * driver will have aborted pending i/o.
1333 } else if (dev
->state
== STATE_DEV_SETUP
)
1334 dev
->setup_abort
= 1;
1336 req
->buf
= dev
->rbuf
;
1337 req
->context
= NULL
;
1338 value
= -EOPNOTSUPP
;
1339 switch (ctrl
->bRequest
) {
1341 case USB_REQ_GET_DESCRIPTOR
:
1342 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1344 switch (w_value
>> 8) {
1347 value
= min (w_length
, (u16
) sizeof *dev
->dev
);
1348 dev
->dev
->bMaxPacketSize0
= dev
->gadget
->ep0
->maxpacket
;
1349 req
->buf
= dev
->dev
;
1351 case USB_DT_DEVICE_QUALIFIER
:
1352 if (!dev
->hs_config
)
1354 value
= min (w_length
, (u16
)
1355 sizeof (struct usb_qualifier_descriptor
));
1356 make_qualifier (dev
);
1358 case USB_DT_OTHER_SPEED_CONFIG
:
1361 value
= config_buf (dev
,
1365 value
= min (w_length
, (u16
) value
);
1370 default: // all others are errors
1375 /* currently one config, two speeds */
1376 case USB_REQ_SET_CONFIGURATION
:
1377 if (ctrl
->bRequestType
!= 0)
1379 if (0 == (u8
) w_value
) {
1381 dev
->current_config
= 0;
1382 usb_gadget_vbus_draw(gadget
, 8 /* mA */ );
1383 // user mode expected to disable endpoints
1387 if (gadget_is_dualspeed(gadget
)
1388 && gadget
->speed
== USB_SPEED_HIGH
) {
1389 config
= dev
->hs_config
->bConfigurationValue
;
1390 power
= dev
->hs_config
->bMaxPower
;
1392 config
= dev
->config
->bConfigurationValue
;
1393 power
= dev
->config
->bMaxPower
;
1396 if (config
== (u8
) w_value
) {
1398 dev
->current_config
= config
;
1399 usb_gadget_vbus_draw(gadget
, 2 * power
);
1403 /* report SET_CONFIGURATION like any other control request,
1404 * except that usermode may not stall this. the next
1405 * request mustn't be allowed start until this finishes:
1406 * endpoints and threads set up, etc.
1408 * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
1409 * has bad/racey automagic that prevents synchronizing here.
1410 * even kernel mode drivers often miss them.
1413 INFO (dev
, "configuration #%d\n", dev
->current_config
);
1414 usb_gadget_set_state(gadget
, USB_STATE_CONFIGURED
);
1415 if (dev
->usermode_setup
) {
1416 dev
->setup_can_stall
= 0;
1422 #ifndef CONFIG_USB_PXA25X
1423 /* PXA automagically handles this request too */
1424 case USB_REQ_GET_CONFIGURATION
:
1425 if (ctrl
->bRequestType
!= 0x80)
1427 *(u8
*)req
->buf
= dev
->current_config
;
1428 value
= min (w_length
, (u16
) 1);
1434 VDEBUG (dev
, "%s req%02x.%02x v%04x i%04x l%d\n",
1435 dev
->usermode_setup
? "delegate" : "fail",
1436 ctrl
->bRequestType
, ctrl
->bRequest
,
1437 w_value
, le16_to_cpu(ctrl
->wIndex
), w_length
);
1439 /* if there's an ep0 reader, don't stall */
1440 if (dev
->usermode_setup
) {
1441 dev
->setup_can_stall
= 1;
1443 dev
->setup_in
= (ctrl
->bRequestType
& USB_DIR_IN
)
1445 dev
->setup_wLength
= w_length
;
1446 dev
->setup_out_ready
= 0;
1447 dev
->setup_out_error
= 0;
1450 /* read DATA stage for OUT right away */
1451 if (unlikely (!dev
->setup_in
&& w_length
)) {
1452 value
= setup_req (gadget
->ep0
, dev
->req
,
1456 value
= usb_ep_queue (gadget
->ep0
, dev
->req
,
1459 clean_req (gadget
->ep0
, dev
->req
);
1463 /* we can't currently stall these */
1464 dev
->setup_can_stall
= 0;
1467 /* state changes when reader collects event */
1468 event
= next_event (dev
, GADGETFS_SETUP
);
1469 event
->u
.setup
= *ctrl
;
1471 spin_unlock (&dev
->lock
);
1476 /* proceed with data transfer and status phases? */
1477 if (value
>= 0 && dev
->state
!= STATE_DEV_SETUP
) {
1478 req
->length
= value
;
1479 req
->zero
= value
< w_length
;
1480 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1482 DBG (dev
, "ep_queue --> %d\n", value
);
1487 /* device stalls when value < 0 */
1488 spin_unlock (&dev
->lock
);
1492 static void destroy_ep_files (struct dev_data
*dev
)
1494 DBG (dev
, "%s %d\n", __func__
, dev
->state
);
1496 /* dev->state must prevent interference */
1497 spin_lock_irq (&dev
->lock
);
1498 while (!list_empty(&dev
->epfiles
)) {
1500 struct inode
*parent
;
1501 struct dentry
*dentry
;
1503 /* break link to FS */
1504 ep
= list_first_entry (&dev
->epfiles
, struct ep_data
, epfiles
);
1505 list_del_init (&ep
->epfiles
);
1506 dentry
= ep
->dentry
;
1508 parent
= dentry
->d_parent
->d_inode
;
1510 /* break link to controller */
1511 if (ep
->state
== STATE_EP_ENABLED
)
1512 (void) usb_ep_disable (ep
->ep
);
1513 ep
->state
= STATE_EP_UNBOUND
;
1514 usb_ep_free_request (ep
->ep
, ep
->req
);
1516 wake_up (&ep
->wait
);
1519 spin_unlock_irq (&dev
->lock
);
1521 /* break link to dcache */
1522 mutex_lock (&parent
->i_mutex
);
1525 mutex_unlock (&parent
->i_mutex
);
1527 spin_lock_irq (&dev
->lock
);
1529 spin_unlock_irq (&dev
->lock
);
1533 static struct dentry
*
1534 gadgetfs_create_file (struct super_block
*sb
, char const *name
,
1535 void *data
, const struct file_operations
*fops
);
1537 static int activate_ep_files (struct dev_data
*dev
)
1540 struct ep_data
*data
;
1542 gadget_for_each_ep (ep
, dev
->gadget
) {
1544 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1547 data
->state
= STATE_EP_DISABLED
;
1548 mutex_init(&data
->lock
);
1549 init_waitqueue_head (&data
->wait
);
1551 strncpy (data
->name
, ep
->name
, sizeof (data
->name
) - 1);
1552 atomic_set (&data
->count
, 1);
1557 ep
->driver_data
= data
;
1559 data
->req
= usb_ep_alloc_request (ep
, GFP_KERNEL
);
1563 data
->dentry
= gadgetfs_create_file (dev
->sb
, data
->name
,
1564 data
, &ep_io_operations
);
1567 list_add_tail (&data
->epfiles
, &dev
->epfiles
);
1572 usb_ep_free_request (ep
, data
->req
);
1577 DBG (dev
, "%s enomem\n", __func__
);
1578 destroy_ep_files (dev
);
1583 gadgetfs_unbind (struct usb_gadget
*gadget
)
1585 struct dev_data
*dev
= get_gadget_data (gadget
);
1587 DBG (dev
, "%s\n", __func__
);
1589 spin_lock_irq (&dev
->lock
);
1590 dev
->state
= STATE_DEV_UNBOUND
;
1591 spin_unlock_irq (&dev
->lock
);
1593 destroy_ep_files (dev
);
1594 gadget
->ep0
->driver_data
= NULL
;
1595 set_gadget_data (gadget
, NULL
);
1597 /* we've already been disconnected ... no i/o is active */
1599 usb_ep_free_request (gadget
->ep0
, dev
->req
);
1600 DBG (dev
, "%s done\n", __func__
);
1604 static struct dev_data
*the_device
;
1606 static int gadgetfs_bind(struct usb_gadget
*gadget
,
1607 struct usb_gadget_driver
*driver
)
1609 struct dev_data
*dev
= the_device
;
1613 if (0 != strcmp (CHIP
, gadget
->name
)) {
1614 pr_err("%s expected %s controller not %s\n",
1615 shortname
, CHIP
, gadget
->name
);
1619 set_gadget_data (gadget
, dev
);
1620 dev
->gadget
= gadget
;
1621 gadget
->ep0
->driver_data
= dev
;
1623 /* preallocate control response and buffer */
1624 dev
->req
= usb_ep_alloc_request (gadget
->ep0
, GFP_KERNEL
);
1627 dev
->req
->context
= NULL
;
1628 dev
->req
->complete
= epio_complete
;
1630 if (activate_ep_files (dev
) < 0)
1633 INFO (dev
, "bound to %s driver\n", gadget
->name
);
1634 spin_lock_irq(&dev
->lock
);
1635 dev
->state
= STATE_DEV_UNCONNECTED
;
1636 spin_unlock_irq(&dev
->lock
);
1641 gadgetfs_unbind (gadget
);
1646 gadgetfs_disconnect (struct usb_gadget
*gadget
)
1648 struct dev_data
*dev
= get_gadget_data (gadget
);
1649 unsigned long flags
;
1651 spin_lock_irqsave (&dev
->lock
, flags
);
1652 if (dev
->state
== STATE_DEV_UNCONNECTED
)
1654 dev
->state
= STATE_DEV_UNCONNECTED
;
1656 INFO (dev
, "disconnected\n");
1657 next_event (dev
, GADGETFS_DISCONNECT
);
1660 spin_unlock_irqrestore (&dev
->lock
, flags
);
1664 gadgetfs_suspend (struct usb_gadget
*gadget
)
1666 struct dev_data
*dev
= get_gadget_data (gadget
);
1668 INFO (dev
, "suspended from state %d\n", dev
->state
);
1669 spin_lock (&dev
->lock
);
1670 switch (dev
->state
) {
1671 case STATE_DEV_SETUP
: // VERY odd... host died??
1672 case STATE_DEV_CONNECTED
:
1673 case STATE_DEV_UNCONNECTED
:
1674 next_event (dev
, GADGETFS_SUSPEND
);
1680 spin_unlock (&dev
->lock
);
1683 static struct usb_gadget_driver gadgetfs_driver
= {
1684 .function
= (char *) driver_desc
,
1685 .bind
= gadgetfs_bind
,
1686 .unbind
= gadgetfs_unbind
,
1687 .setup
= gadgetfs_setup
,
1688 .reset
= gadgetfs_disconnect
,
1689 .disconnect
= gadgetfs_disconnect
,
1690 .suspend
= gadgetfs_suspend
,
1693 .name
= (char *) shortname
,
1697 /*----------------------------------------------------------------------*/
1699 static void gadgetfs_nop(struct usb_gadget
*arg
) { }
1701 static int gadgetfs_probe(struct usb_gadget
*gadget
,
1702 struct usb_gadget_driver
*driver
)
1704 CHIP
= gadget
->name
;
1708 static struct usb_gadget_driver probe_driver
= {
1709 .max_speed
= USB_SPEED_HIGH
,
1710 .bind
= gadgetfs_probe
,
1711 .unbind
= gadgetfs_nop
,
1712 .setup
= (void *)gadgetfs_nop
,
1713 .disconnect
= gadgetfs_nop
,
1720 /* DEVICE INITIALIZATION
1722 * fd = open ("/dev/gadget/$CHIP", O_RDWR)
1723 * status = write (fd, descriptors, sizeof descriptors)
1725 * That write establishes the device configuration, so the kernel can
1726 * bind to the controller ... guaranteeing it can handle enumeration
1727 * at all necessary speeds. Descriptor order is:
1729 * . message tag (u32, host order) ... for now, must be zero; it
1730 * would change to support features like multi-config devices
1731 * . full/low speed config ... all wTotalLength bytes (with interface,
1732 * class, altsetting, endpoint, and other descriptors)
1733 * . high speed config ... all descriptors, for high speed operation;
1734 * this one's optional except for high-speed hardware
1735 * . device descriptor
1737 * Endpoints are not yet enabled. Drivers must wait until device
1738 * configuration and interface altsetting changes create
1739 * the need to configure (or unconfigure) them.
1741 * After initialization, the device stays active for as long as that
1742 * $CHIP file is open. Events must then be read from that descriptor,
1743 * such as configuration notifications.
1746 static int is_valid_config (struct usb_config_descriptor
*config
)
1748 return config
->bDescriptorType
== USB_DT_CONFIG
1749 && config
->bLength
== USB_DT_CONFIG_SIZE
1750 && config
->bConfigurationValue
!= 0
1751 && (config
->bmAttributes
& USB_CONFIG_ATT_ONE
) != 0
1752 && (config
->bmAttributes
& USB_CONFIG_ATT_WAKEUP
) == 0;
1753 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
1754 /* FIXME check lengths: walk to end */
1758 dev_config (struct file
*fd
, const char __user
*buf
, size_t len
, loff_t
*ptr
)
1760 struct dev_data
*dev
= fd
->private_data
;
1761 ssize_t value
= len
, length
= len
;
1766 spin_lock_irq(&dev
->lock
);
1767 if (dev
->state
> STATE_DEV_OPENED
) {
1768 value
= ep0_write(fd
, buf
, len
, ptr
);
1769 spin_unlock_irq(&dev
->lock
);
1772 spin_unlock_irq(&dev
->lock
);
1774 if (len
< (USB_DT_CONFIG_SIZE
+ USB_DT_DEVICE_SIZE
+ 4))
1777 /* we might need to change message format someday */
1778 if (copy_from_user (&tag
, buf
, 4))
1785 kbuf
= memdup_user(buf
, length
);
1787 return PTR_ERR(kbuf
);
1789 spin_lock_irq (&dev
->lock
);
1795 /* full or low speed config */
1796 dev
->config
= (void *) kbuf
;
1797 total
= le16_to_cpu(dev
->config
->wTotalLength
);
1798 if (!is_valid_config (dev
->config
) || total
>= length
)
1803 /* optional high speed config */
1804 if (kbuf
[1] == USB_DT_CONFIG
) {
1805 dev
->hs_config
= (void *) kbuf
;
1806 total
= le16_to_cpu(dev
->hs_config
->wTotalLength
);
1807 if (!is_valid_config (dev
->hs_config
) || total
>= length
)
1813 /* could support multiple configs, using another encoding! */
1815 /* device descriptor (tweaked for paranoia) */
1816 if (length
!= USB_DT_DEVICE_SIZE
)
1818 dev
->dev
= (void *)kbuf
;
1819 if (dev
->dev
->bLength
!= USB_DT_DEVICE_SIZE
1820 || dev
->dev
->bDescriptorType
!= USB_DT_DEVICE
1821 || dev
->dev
->bNumConfigurations
!= 1)
1823 dev
->dev
->bNumConfigurations
= 1;
1824 dev
->dev
->bcdUSB
= cpu_to_le16 (0x0200);
1826 /* triggers gadgetfs_bind(); then we can enumerate. */
1827 spin_unlock_irq (&dev
->lock
);
1829 gadgetfs_driver
.max_speed
= USB_SPEED_HIGH
;
1831 gadgetfs_driver
.max_speed
= USB_SPEED_FULL
;
1833 value
= usb_gadget_probe_driver(&gadgetfs_driver
);
1838 /* at this point "good" hardware has for the first time
1839 * let the USB the host see us. alternatively, if users
1840 * unplug/replug that will clear all the error state.
1842 * note: everything running before here was guaranteed
1843 * to choke driver model style diagnostics. from here
1844 * on, they can work ... except in cleanup paths that
1845 * kick in after the ep0 descriptor is closed.
1852 spin_unlock_irq (&dev
->lock
);
1853 pr_debug ("%s: %s fail %Zd, %p\n", shortname
, __func__
, value
, dev
);
1860 dev_open (struct inode
*inode
, struct file
*fd
)
1862 struct dev_data
*dev
= inode
->i_private
;
1865 spin_lock_irq(&dev
->lock
);
1866 if (dev
->state
== STATE_DEV_DISABLED
) {
1868 dev
->state
= STATE_DEV_OPENED
;
1869 fd
->private_data
= dev
;
1873 spin_unlock_irq(&dev
->lock
);
1877 static const struct file_operations ep0_operations
= {
1878 .llseek
= no_llseek
,
1882 .write
= dev_config
,
1883 .fasync
= ep0_fasync
,
1885 .unlocked_ioctl
= dev_ioctl
,
1886 .release
= dev_release
,
1889 /*----------------------------------------------------------------------*/
1891 /* FILESYSTEM AND SUPERBLOCK OPERATIONS
1893 * Mounting the filesystem creates a controller file, used first for
1894 * device configuration then later for event monitoring.
1898 /* FIXME PAM etc could set this security policy without mount options
1899 * if epfiles inherited ownership and permissons from ep0 ...
1902 static unsigned default_uid
;
1903 static unsigned default_gid
;
1904 static unsigned default_perm
= S_IRUSR
| S_IWUSR
;
1906 module_param (default_uid
, uint
, 0644);
1907 module_param (default_gid
, uint
, 0644);
1908 module_param (default_perm
, uint
, 0644);
1911 static struct inode
*
1912 gadgetfs_make_inode (struct super_block
*sb
,
1913 void *data
, const struct file_operations
*fops
,
1916 struct inode
*inode
= new_inode (sb
);
1919 inode
->i_ino
= get_next_ino();
1920 inode
->i_mode
= mode
;
1921 inode
->i_uid
= make_kuid(&init_user_ns
, default_uid
);
1922 inode
->i_gid
= make_kgid(&init_user_ns
, default_gid
);
1923 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
1925 inode
->i_private
= data
;
1926 inode
->i_fop
= fops
;
1931 /* creates in fs root directory, so non-renamable and non-linkable.
1932 * so inode and dentry are paired, until device reconfig.
1934 static struct dentry
*
1935 gadgetfs_create_file (struct super_block
*sb
, char const *name
,
1936 void *data
, const struct file_operations
*fops
)
1938 struct dentry
*dentry
;
1939 struct inode
*inode
;
1941 dentry
= d_alloc_name(sb
->s_root
, name
);
1945 inode
= gadgetfs_make_inode (sb
, data
, fops
,
1946 S_IFREG
| (default_perm
& S_IRWXUGO
));
1951 d_add (dentry
, inode
);
1955 static const struct super_operations gadget_fs_operations
= {
1956 .statfs
= simple_statfs
,
1957 .drop_inode
= generic_delete_inode
,
1961 gadgetfs_fill_super (struct super_block
*sb
, void *opts
, int silent
)
1963 struct inode
*inode
;
1964 struct dev_data
*dev
;
1969 /* fake probe to determine $CHIP */
1971 usb_gadget_probe_driver(&probe_driver
);
1976 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1977 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1978 sb
->s_magic
= GADGETFS_MAGIC
;
1979 sb
->s_op
= &gadget_fs_operations
;
1980 sb
->s_time_gran
= 1;
1983 inode
= gadgetfs_make_inode (sb
,
1984 NULL
, &simple_dir_operations
,
1985 S_IFDIR
| S_IRUGO
| S_IXUGO
);
1988 inode
->i_op
= &simple_dir_inode_operations
;
1989 if (!(sb
->s_root
= d_make_root (inode
)))
1992 /* the ep0 file is named after the controller we expect;
1993 * user mode code can use it for sanity checks, like we do.
2000 dev
->dentry
= gadgetfs_create_file(sb
, CHIP
, dev
, &ep0_operations
);
2006 /* other endpoint files are available after hardware setup,
2007 * from binding to a controller.
2016 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2017 static struct dentry
*
2018 gadgetfs_mount (struct file_system_type
*t
, int flags
,
2019 const char *path
, void *opts
)
2021 return mount_single (t
, flags
, opts
, gadgetfs_fill_super
);
2025 gadgetfs_kill_sb (struct super_block
*sb
)
2027 kill_litter_super (sb
);
2029 put_dev (the_device
);
2034 /*----------------------------------------------------------------------*/
2036 static struct file_system_type gadgetfs_type
= {
2037 .owner
= THIS_MODULE
,
2039 .mount
= gadgetfs_mount
,
2040 .kill_sb
= gadgetfs_kill_sb
,
2042 MODULE_ALIAS_FS("gadgetfs");
2044 /*----------------------------------------------------------------------*/
2046 static int __init
init (void)
2050 status
= register_filesystem (&gadgetfs_type
);
2052 pr_info ("%s: %s, version " DRIVER_VERSION
"\n",
2053 shortname
, driver_desc
);
2058 static void __exit
cleanup (void)
2060 pr_debug ("unregister %s\n", shortname
);
2061 unregister_filesystem (&gadgetfs_type
);
2063 module_exit (cleanup
);