17 #include <linux/module.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>
28 #include <linux/device.h>
65 #define DRIVER_DESC "USB Gadget filesystem"
66 #define DRIVER_VERSION "24 Aug 2004"
69 static const char shortname [] =
"gadgetfs";
78 #define GADGETFS_MAGIC 0xaee71ee7
79 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
161 static struct dev_data *dev_new (
void)
171 INIT_LIST_HEAD (&dev->
epfiles);
227 static const char *CHIP;
236 #define xprintk(d,level,fmt,args...) \
237 printk(level "%s: " fmt , shortname , ## args)
240 #define DBG(dev,fmt,args...) \
241 xprintk(dev , KERN_DEBUG , fmt , ## args)
243 #define DBG(dev,fmt,args...) \
250 #define VDEBUG(dev,fmt,args...) \
254 #define ERROR(dev,fmt,args...) \
255 xprintk(dev , KERN_ERR , fmt , ## args)
256 #define INFO(dev,fmt,args...) \
257 xprintk(dev , KERN_INFO , fmt , ## args)
286 get_ready_ep (
unsigned f_flags,
struct ep_data *epdata)
306 switch (epdata->
state) {
312 pr_debug (
"%s: ep %p not available, state %d\n",
313 shortname, epdata, epdata->
state);
323 ep_io (
struct ep_data *epdata,
void *
buf,
unsigned len)
328 spin_lock_irq (&epdata->
dev->lock);
339 spin_unlock_irq (&epdata->
dev->lock);
341 if (
likely (value == 0)) {
344 spin_lock_irq (&epdata->
dev->lock);
346 DBG (epdata->
dev,
"%s i/o interrupted\n",
348 usb_ep_dequeue (epdata->
ep, epdata->
req);
349 spin_unlock_irq (&epdata->
dev->lock);
355 spin_unlock_irq (&epdata->
dev->lock);
357 DBG (epdata->
dev,
"endpoint gone\n");
369 ep_read (
struct file *
fd,
char __user *buf,
size_t len, loff_t *
ptr)
375 if ((value = get_ready_ep (fd->
f_flags, data)) < 0)
379 if (usb_endpoint_dir_in(&data->desc)) {
380 if (usb_endpoint_xfer_isoc(&data->desc)) {
385 spin_lock_irq (&data->
dev->lock);
387 usb_ep_set_halt (data->
ep);
388 spin_unlock_irq (&data->
dev->lock);
400 value = ep_io (data, kbuf, len);
401 VDEBUG (data->
dev,
"%s read %zu OUT, status %d\n",
402 data->
name, len, (
int) value);
414 ep_write (
struct file *fd,
const char __user *buf,
size_t len, loff_t *ptr)
420 if ((value = get_ready_ep (fd->
f_flags, data)) < 0)
424 if (!usb_endpoint_dir_in(&data->desc)) {
425 if (usb_endpoint_xfer_isoc(&data->desc)) {
430 spin_lock_irq (&data->
dev->lock);
432 usb_ep_set_halt (data->
ep);
433 spin_unlock_irq (&data->
dev->lock);
449 value = ep_io (data, kbuf, len);
450 VDEBUG (data->
dev,
"%s write %zu IN, status %d\n",
451 data->
name, len, (
int) value);
471 data->desc.bDescriptorType = 0;
472 data->
hs_desc.bDescriptorType = 0;
473 usb_ep_disable(data->
ep);
480 static long ep_ioctl(
struct file *fd,
unsigned code,
unsigned long value)
485 if ((status = get_ready_ep (fd->
f_flags, data)) < 0)
488 spin_lock_irq (&data->
dev->lock);
492 status = usb_ep_fifo_status (data->
ep);
495 usb_ep_fifo_flush (data->
ep);
498 status = usb_ep_clear_halt (data->
ep);
505 spin_unlock_irq (&data->
dev->lock);
534 value = usb_ep_dequeue (epdata->
ep, priv->
req);
557 for (i=0; i < priv->
nr_segs; i++) {
584 spin_lock(&epdata->
dev->lock);
602 DBG(epdata->
dev,
"%s fault %d len %d\n",
609 spin_unlock(&epdata->
dev->lock);
611 usb_ep_free_request(ep, req);
622 unsigned long nr_segs
640 value = get_ready_ep(iocb->
ki_filp->f_flags, epdata);
654 spin_lock_irq(&epdata->
dev->lock);
665 usb_ep_free_request(epdata->
ep, req);
670 spin_unlock_irq(&epdata->
dev->lock);
683 ep_aio_read(
struct kiocb *iocb,
const struct iovec *iov,
684 unsigned long nr_segs, loff_t o)
689 if (
unlikely(usb_endpoint_dir_in(&epdata->desc)))
697 return ep_aio_rwtail(iocb, buf, iocb->
ki_left, epdata, iov, nr_segs);
701 ep_aio_write(
struct kiocb *iocb,
const struct iovec *iov,
702 unsigned long nr_segs, loff_t o)
709 if (
unlikely(!usb_endpoint_dir_in(&epdata->desc)))
716 for (i=0; i < nr_segs; i++) {
718 iov[i].iov_len) != 0)) {
724 return ep_aio_rwtail(iocb, buf, len, epdata,
NULL, 0);
736 .unlocked_ioctl = ep_ioctl,
737 .release = ep_release,
739 .aio_read = ep_aio_read,
740 .aio_write = ep_aio_write,
757 ep_config (
struct file *fd,
const char __user *buf,
size_t len, loff_t *ptr)
782 DBG(data->
dev,
"config %s, bad tag %d\n", data->
name, tag);
807 || data->
hs_desc.bDescriptorType
809 DBG(data->
dev,
"config %s, bad hs length or type\n",
815 spin_lock_irq (&data->
dev->lock);
819 }
else if ((ep = data->
ep) ==
NULL) {
823 switch (data->
dev->gadget->speed) {
826 ep->
desc = &data->desc;
827 value = usb_ep_enable(ep);
834 value = usb_ep_enable(ep);
839 DBG(data->
dev,
"unconnected, %s init abandoned\n",
844 fd->
f_op = &ep_io_operations;
848 spin_unlock_irq (&data->
dev->lock);
851 data->desc.bDescriptorType = 0;
852 data->
hs_desc.bDescriptorType = 0;
865 ep_open (
struct inode *inode,
struct file *fd)
872 spin_lock_irq (&data->
dev->lock);
882 DBG (data->
dev,
"%s state %d\n",
884 spin_unlock_irq (&data->
dev->lock);
896 .release = ep_release,
908 static inline void ep0_readable (
struct dev_data *dev)
944 if (free && req->
buf != &dev->
rbuf)
947 spin_unlock_irqrestore(&dev->
lock, flags);
955 DBG (dev,
"ep0 request busy!\n");
958 if (len >
sizeof (dev->
rbuf))
971 ep0_read (
struct file *fd,
char __user *buf,
size_t len, loff_t *ptr)
977 spin_lock_irq (&dev->
lock);
990 VDEBUG(dev,
"ep0in stall\n");
995 }
else if (len == 0) {
999 if ((retval = setup_req (ep, req, 0)) == 0)
1007 if (gadget_is_dualspeed(dev->
gadget)
1012 power = dev->
config->bMaxPower;
1013 usb_gadget_vbus_draw(dev->
gadget, 2 * power);
1017 if ((fd->
f_flags & O_NONBLOCK) != 0
1022 spin_unlock_irq (&dev->
lock);
1027 spin_lock_irq (&dev->
lock);
1040 len =
min (len, (
size_t)dev->
req->actual);
1054 if (len < sizeof dev->
event [0]) {
1071 for (i = 0; i <
n; i++) {
1078 spin_unlock_irq (&dev->
lock);
1088 spin_lock_irq (&dev->
lock);
1095 spin_unlock_irq (&dev->
lock);
1099 if (fd->
f_flags & O_NONBLOCK) {
1106 DBG (dev,
"fail %s, state %d\n", __func__, state);
1111 spin_unlock_irq (&dev->
lock);
1112 DBG (dev,
"%s wait\n", __func__);
1119 spin_lock_irq (&dev->
lock);
1124 spin_unlock_irq (&dev->
lock);
1146 for (i = 0; i != dev->
ev_next; i++) {
1147 if (dev->
event [i].type != type)
1149 DBG(dev,
"discard old event[%d] %d\n", i, type);
1165 memset (event, 0,
sizeof *event);
1171 ep0_write (
struct file *fd,
const char __user *buf,
size_t len, loff_t *ptr)
1176 spin_lock_irq (&dev->
lock);
1188 retval = setup_req (dev->
gadget->ep0, dev->
req, len);
1191 spin_unlock_irq (&dev->
lock);
1197 retval = usb_ep_queue (
1202 spin_lock_irq (&dev->
lock);
1204 spin_unlock_irq (&dev->
lock);
1213 VDEBUG(dev,
"ep0out stall\n");
1218 DBG(dev,
"bogus ep0out stall!\n");
1221 DBG (dev,
"fail %s, state %d\n", __func__, dev->
state);
1223 spin_unlock_irq (&dev->
lock);
1228 ep0_fasync (
int f,
struct file *fd,
int on)
1232 VDEBUG (dev,
"%s %s\n", __func__, on ?
"on" :
"off");
1239 dev_release (
struct inode *inode,
struct file *fd)
1257 spin_lock_irq(&dev->
lock);
1259 spin_unlock_irq(&dev->
lock);
1269 poll_wait(fd, &dev->
wait, wait);
1271 spin_lock_irq (&dev->
lock);
1288 spin_unlock_irq(&dev->
lock);
1292 static long dev_ioctl (
struct file *fd,
unsigned code,
unsigned long value)
1298 if (gadget->
ops->ioctl)
1299 ret = gadget->
ops->ioctl (gadget, code, value);
1311 .fasync = ep0_fasync,
1313 .unlocked_ioctl = dev_ioctl,
1314 .release = dev_release,
1325 static void make_qualifier (
struct dev_data *dev)
1330 qual.bLength =
sizeof qual;
1340 qual.bMaxPacketSize0 = dev->
gadget->ep0->maxpacket;
1342 qual.bNumConfigurations = 1;
1358 if (gadget_is_dualspeed(dev->
gadget)) {
1377 struct dev_data *dev = get_gadget_data (gadget);
1384 spin_lock (&dev->
lock);
1387 if (gadget_is_dualspeed(gadget)
1390 spin_unlock(&dev->
lock);
1391 ERROR (dev,
"no high speed config??\n");
1397 INFO (dev,
"connected\n");
1399 event->u.speed = gadget->
speed;
1419 switch (w_value >> 8) {
1422 value =
min (w_length, (
u16)
sizeof *dev->
dev);
1423 dev->
dev->bMaxPacketSize0 = dev->
gadget->ep0->maxpacket;
1429 value =
min (w_length, (
u16)
1431 make_qualifier (dev);
1436 value = config_buf (dev,
1440 value =
min (w_length, (
u16) value);
1454 if (0 == (
u8) w_value) {
1457 usb_gadget_vbus_draw(gadget, 8 );
1462 if (gadget_is_dualspeed(gadget)
1464 config = dev->
hs_config->bConfigurationValue;
1467 config = dev->
config->bConfigurationValue;
1468 power = dev->
config->bMaxPower;
1471 if (config == (
u8) w_value) {
1474 usb_gadget_vbus_draw(gadget, 2 * power);
1496 #ifndef CONFIG_USB_GADGET_PXA25X
1502 value =
min (w_length, (
u16) 1);
1508 VDEBUG (dev,
"%s req%02x.%02x v%04x i%04x l%d\n",
1526 value = setup_req (gadget->
ep0, dev->
req,
1530 value = usb_ep_queue (gadget->
ep0, dev->
req,
1533 clean_req (gadget->
ep0, dev->
req);
1543 event->u.setup = *
ctrl;
1545 spin_unlock (&dev->
lock);
1556 DBG (dev,
"ep_queue --> %d\n", value);
1562 spin_unlock (&dev->
lock);
1566 static void destroy_ep_files (
struct dev_data *dev)
1568 DBG (dev,
"%s %d\n", __func__, dev->
state);
1571 spin_lock_irq (&dev->
lock);
1572 while (!list_empty(&dev->
epfiles)) {
1582 parent = dentry->
d_parent->d_inode;
1586 (
void) usb_ep_disable (ep->
ep);
1588 usb_ep_free_request (ep->
ep, ep->
req);
1593 spin_unlock_irq (&dev->
lock);
1601 spin_lock_irq (&dev->
lock);
1603 spin_unlock_irq (&dev->
lock);
1607 static struct inode *
1610 struct dentry **dentry_p);
1612 static int activate_ep_files (
struct dev_data *dev)
1638 data->
inode = gadgetfs_create_file (dev->
sb, data->
name,
1639 data, &ep_config_operations,
1648 usb_ep_free_request (ep, data->
req);
1653 DBG (dev,
"%s enomem\n", __func__);
1654 destroy_ep_files (dev);
1661 struct dev_data *dev = get_gadget_data (gadget);
1663 DBG (dev,
"%s\n", __func__);
1665 spin_lock_irq (&dev->
lock);
1667 spin_unlock_irq (&dev->
lock);
1669 destroy_ep_files (dev);
1670 gadget->
ep0->driver_data =
NULL;
1671 set_gadget_data (gadget,
NULL);
1675 usb_ep_free_request (gadget->
ep0, dev->
req);
1676 DBG (dev,
"%s done\n", __func__);
1680 static struct dev_data *the_device;
1682 static int gadgetfs_bind(
struct usb_gadget *gadget,
1690 pr_err(
"%s expected %s controller not %s\n",
1691 shortname, CHIP, gadget->
name);
1695 set_gadget_data (gadget, dev);
1697 gadget->
ep0->driver_data =
dev;
1704 dev->
req->complete = epio_complete;
1706 if (activate_ep_files (dev) < 0)
1709 INFO (dev,
"bound to %s driver\n", gadget->
name);
1710 spin_lock_irq(&dev->
lock);
1712 spin_unlock_irq(&dev->
lock);
1717 gadgetfs_unbind (gadget);
1722 gadgetfs_disconnect (
struct usb_gadget *gadget)
1724 struct dev_data *dev = get_gadget_data (gadget);
1725 unsigned long flags;
1732 INFO (dev,
"disconnected\n");
1736 spin_unlock_irqrestore (&dev->
lock, flags);
1742 struct dev_data *dev = get_gadget_data (gadget);
1744 INFO (dev,
"suspended from state %d\n", dev->
state);
1745 spin_lock (&dev->
lock);
1746 switch (dev->
state) {
1756 spin_unlock (&dev->
lock);
1760 .function = (
char *) driver_desc,
1761 .
bind = gadgetfs_bind,
1762 .
unbind = gadgetfs_unbind,
1763 .
setup = gadgetfs_setup,
1768 .name = (
char *) shortname,
1776 static int gadgetfs_probe(
struct usb_gadget *gadget,
1779 CHIP = gadget->
name;
1785 .bind = gadgetfs_probe,
1786 .unbind = gadgetfs_nop,
1787 .setup = (
void *)gadgetfs_nop,
1833 dev_config (
struct file *fd,
const char __user *buf,
size_t len, loff_t *ptr)
1836 ssize_t value = len, length = len;
1854 return PTR_ERR(kbuf);
1856 spin_lock_irq (&dev->
lock);
1863 dev->
config = (
void *) kbuf;
1865 if (!is_valid_config (dev->
config) || total >= length)
1874 if (!is_valid_config (dev->
hs_config) || total >= length)
1885 dev->
dev = (
void *)kbuf;
1888 || dev->
dev->bNumConfigurations != 1)
1890 dev->
dev->bNumConfigurations = 1;
1894 spin_unlock_irq (&dev->
lock);
1914 fd->
f_op = &ep0_io_operations;
1920 spin_unlock_irq (&dev->
lock);
1921 pr_debug (
"%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
1928 dev_open (
struct inode *inode,
struct file *fd)
1933 spin_lock_irq(&dev->
lock);
1941 spin_unlock_irq(&dev->
lock);
1950 .write = dev_config,
1951 .fasync = ep0_fasync,
1952 .unlocked_ioctl = dev_ioctl,
1953 .release = dev_release,
1969 static unsigned default_uid;
1970 static unsigned default_gid;
1978 static struct inode *
2001 static struct inode *
2004 struct dentry **dentry_p)
2006 struct dentry *dentry;
2007 struct inode *
inode;
2013 inode = gadgetfs_make_inode (sb, data, fops,
2019 d_add (dentry, inode);
2032 struct inode *
inode;
2047 sb->
s_op = &gadget_fs_operations;
2051 inode = gadgetfs_make_inode (sb,
2068 if (!gadgetfs_create_file (sb, CHIP,
2069 dev, &dev_init_operations,
2086 static struct dentry *
2088 const char *
path,
void *opts)
2090 return mount_single (t, flags, opts, gadgetfs_fill_super);
2098 put_dev (the_device);
2108 .mount = gadgetfs_mount,
2109 .kill_sb = gadgetfs_kill_sb,
2121 shortname, driver_desc);
2126 static void __exit cleanup (
void)
2128 pr_debug (
"unregister %s\n", shortname);