aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Kconfig2
-rw-r--r--drivers/usb/Makefile2
-rw-r--r--drivers/usb/class/cdc-acm.c58
-rw-r--r--drivers/usb/class/usbtmc.c54
-rw-r--r--drivers/usb/core/driver.c135
-rw-r--r--drivers/usb/core/file.c1
-rw-r--r--drivers/usb/core/generic.c2
-rw-r--r--drivers/usb/core/hcd.c82
-rw-r--r--drivers/usb/core/hcd.h19
-rw-r--r--drivers/usb/core/hub.c142
-rw-r--r--drivers/usb/core/message.c82
-rw-r--r--drivers/usb/core/sysfs.c61
-rw-r--r--drivers/usb/core/urb.c22
-rw-r--r--drivers/usb/core/usb.c67
-rw-r--r--drivers/usb/core/usb.h1
-rw-r--r--drivers/usb/gadget/Kconfig60
-rw-r--r--drivers/usb/gadget/Makefile4
-rw-r--r--drivers/usb/gadget/at91_udc.c6
-rw-r--r--drivers/usb/gadget/audio.c115
-rw-r--r--drivers/usb/gadget/composite.c59
-rw-r--r--drivers/usb/gadget/ether.c16
-rw-r--r--drivers/usb/gadget/f_acm.c28
-rw-r--r--drivers/usb/gadget/f_audio.c76
-rw-r--r--drivers/usb/gadget/f_mass_storage.c3091
-rw-r--r--drivers/usb/gadget/f_rndis.c35
-rw-r--r--drivers/usb/gadget/file_storage.c879
-rw-r--r--drivers/usb/gadget/mass_storage.c240
-rw-r--r--drivers/usb/gadget/multi.c358
-rw-r--r--drivers/usb/gadget/storage_common.c778
-rw-r--r--drivers/usb/gadget/u_ether.h2
-rw-r--r--drivers/usb/host/Kconfig22
-rw-r--r--drivers/usb/host/ehci-hcd.c19
-rw-r--r--drivers/usb/host/ehci-hub.c2
-rw-r--r--drivers/usb/host/ehci-mxc.c296
-rw-r--r--drivers/usb/host/ehci-omap.c756
-rw-r--r--drivers/usb/host/ehci-q.c32
-rw-r--r--drivers/usb/host/ehci-sched.c36
-rw-r--r--drivers/usb/host/ehci-xilinx-of.c300
-rw-r--r--drivers/usb/host/isp1362.h4
-rw-r--r--drivers/usb/host/ohci-at91.c10
-rw-r--r--drivers/usb/host/ohci-pnx4008.c8
-rw-r--r--drivers/usb/host/r8a66597-hcd.c4
-rw-r--r--drivers/usb/host/whci/debug.c20
-rw-r--r--drivers/usb/host/whci/hcd.c1
-rw-r--r--drivers/usb/host/whci/qset.c350
-rw-r--r--drivers/usb/host/whci/whcd.h9
-rw-r--r--drivers/usb/host/whci/whci-hc.h14
-rw-r--r--drivers/usb/host/xhci-hcd.c139
-rw-r--r--drivers/usb/host/xhci-mem.c255
-rw-r--r--drivers/usb/host/xhci-pci.c2
-rw-r--r--drivers/usb/host/xhci-ring.c343
-rw-r--r--drivers/usb/host/xhci.h37
-rw-r--r--drivers/usb/misc/usbtest.c7
-rw-r--r--drivers/usb/mon/mon_bin.c51
-rw-r--r--drivers/usb/mon/mon_text.c23
-rw-r--r--drivers/usb/musb/Kconfig5
-rw-r--r--drivers/usb/musb/blackfin.c16
-rw-r--r--drivers/usb/musb/blackfin.h37
-rw-r--r--drivers/usb/musb/musb_core.c10
-rw-r--r--drivers/usb/musb/musb_core.h8
-rw-r--r--drivers/usb/musb/musb_dma.h11
-rw-r--r--drivers/usb/musb/musb_gadget.c196
-rw-r--r--drivers/usb/musb/musb_gadget.h4
-rw-r--r--drivers/usb/musb/musb_gadget_ep0.c70
-rw-r--r--drivers/usb/musb/musb_host.c10
-rw-r--r--drivers/usb/musb/musb_regs.h4
-rw-r--r--drivers/usb/musb/musbhsdma.c12
-rw-r--r--drivers/usb/musb/omap2430.c2
-rw-r--r--drivers/usb/otg/Kconfig9
-rw-r--r--drivers/usb/otg/Makefile1
-rw-r--r--drivers/usb/otg/twl4030-usb.c4
-rw-r--r--drivers/usb/otg/ulpi.c136
-rw-r--r--drivers/usb/serial/ark3116.c975
-rw-r--r--drivers/usb/serial/ftdi_sio.c32
-rw-r--r--drivers/usb/serial/ftdi_sio.h14
-rw-r--r--drivers/usb/serial/mos7840.c24
-rw-r--r--drivers/usb/serial/option.c39
-rw-r--r--drivers/usb/serial/sierra.c91
-rw-r--r--drivers/usb/storage/scsiglue.c3
-rw-r--r--drivers/usb/storage/transport.c17
-rw-r--r--drivers/usb/storage/unusual_devs.h7
-rw-r--r--drivers/usb/storage/usb.c43
-rw-r--r--drivers/usb/storage/usb.h1
-rw-r--r--drivers/usb/usb-skeleton.c4
-rw-r--r--drivers/usb/wusbcore/devconnect.c7
-rw-r--r--drivers/usb/wusbcore/security.c6
-rw-r--r--drivers/usb/wusbcore/wusbhc.c32
-rw-r--r--drivers/usb/wusbcore/wusbhc.h1
88 files changed, 9247 insertions, 1801 deletions
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index 240750881d28..81aac7f4ca59 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -60,6 +60,8 @@ config USB_ARCH_HAS_EHCI
60 default y if ARCH_IXP4XX 60 default y if ARCH_IXP4XX
61 default y if ARCH_W90X900 61 default y if ARCH_W90X900
62 default y if ARCH_AT91SAM9G45 62 default y if ARCH_AT91SAM9G45
63 default y if ARCH_MXC
64 default y if ARCH_OMAP34XX
63 default PCI 65 default PCI
64 66
65# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface. 67# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface.
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index be3c9b80bc9f..473aa1a20de9 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -44,3 +44,5 @@ obj-y += early/
44 44
45obj-$(CONFIG_USB_ATM) += atm/ 45obj-$(CONFIG_USB_ATM) += atm/
46obj-$(CONFIG_USB_SPEEDTOUCH) += atm/ 46obj-$(CONFIG_USB_SPEEDTOUCH) += atm/
47
48obj-$(CONFIG_USB_ULPI) += otg/
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index e4eca7810bcf..34d4eb98829e 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -1461,6 +1461,12 @@ err_out:
1461} 1461}
1462 1462
1463#endif /* CONFIG_PM */ 1463#endif /* CONFIG_PM */
1464
1465#define NOKIA_PCSUITE_ACM_INFO(x) \
1466 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1467 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1468 USB_CDC_ACM_PROTO_VENDOR)
1469
1464/* 1470/*
1465 * USB driver structure. 1471 * USB driver structure.
1466 */ 1472 */
@@ -1519,6 +1525,57 @@ static struct usb_device_id acm_ids[] = {
1519 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */ 1525 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1520 }, 1526 },
1521 1527
1528 /* Nokia S60 phones expose two ACM channels. The first is
1529 * a modem and is picked up by the standard AT-command
1530 * information below. The second is 'vendor-specific' but
1531 * is treated as a serial device at the S60 end, so we want
1532 * to expose it on Linux too. */
1533 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1534 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1535 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1536 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1537 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1538 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1539 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1540 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1541 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1542 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1543 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1544 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1545 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1546 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1547 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1548 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1549 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1550 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1551 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1552 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1553 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1554 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1555 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1556 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1557 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1558 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1559 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1560 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1561 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1562 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1563 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1564 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1565 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1566 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1567 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1568 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1569 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1570 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1571 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1572 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1573 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1574 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1575 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1576
1577 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1578
1522 /* control interfaces with various AT-command sets */ 1579 /* control interfaces with various AT-command sets */
1523 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1580 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1524 USB_CDC_ACM_PROTO_AT_V25TER) }, 1581 USB_CDC_ACM_PROTO_AT_V25TER) },
@@ -1533,7 +1590,6 @@ static struct usb_device_id acm_ids[] = {
1533 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1590 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1534 USB_CDC_ACM_PROTO_AT_CDMA) }, 1591 USB_CDC_ACM_PROTO_AT_CDMA) },
1535 1592
1536 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1537 { } 1593 { }
1538}; 1594};
1539 1595
diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index b4bd2411c666..7c5f4e32c920 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -347,13 +347,8 @@ usbtmc_abort_bulk_out_check_status:
347 goto exit; 347 goto exit;
348 348
349usbtmc_abort_bulk_out_clear_halt: 349usbtmc_abort_bulk_out_clear_halt:
350 rv = usb_control_msg(data->usb_dev, 350 rv = usb_clear_halt(data->usb_dev,
351 usb_sndctrlpipe(data->usb_dev, 0), 351 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
352 USB_REQ_CLEAR_FEATURE,
353 USB_DIR_OUT | USB_TYPE_STANDARD |
354 USB_RECIP_ENDPOINT,
355 USB_ENDPOINT_HALT, data->bulk_out, buffer,
356 0, USBTMC_TIMEOUT);
357 352
358 if (rv < 0) { 353 if (rv < 0) {
359 dev_err(dev, "usb_control_msg returned %d\n", rv); 354 dev_err(dev, "usb_control_msg returned %d\n", rv);
@@ -562,10 +557,16 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
562 n_bytes = roundup(12 + this_part, 4); 557 n_bytes = roundup(12 + this_part, 4);
563 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part)); 558 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
564 559
565 retval = usb_bulk_msg(data->usb_dev, 560 do {
566 usb_sndbulkpipe(data->usb_dev, 561 retval = usb_bulk_msg(data->usb_dev,
567 data->bulk_out), 562 usb_sndbulkpipe(data->usb_dev,
568 buffer, n_bytes, &actual, USBTMC_TIMEOUT); 563 data->bulk_out),
564 buffer, n_bytes,
565 &actual, USBTMC_TIMEOUT);
566 if (retval != 0)
567 break;
568 n_bytes -= actual;
569 } while (n_bytes);
569 570
570 data->bTag_last_write = data->bTag; 571 data->bTag_last_write = data->bTag;
571 data->bTag++; 572 data->bTag++;
@@ -702,14 +703,8 @@ usbtmc_clear_check_status:
702 703
703usbtmc_clear_bulk_out_halt: 704usbtmc_clear_bulk_out_halt:
704 705
705 rv = usb_control_msg(data->usb_dev, 706 rv = usb_clear_halt(data->usb_dev,
706 usb_sndctrlpipe(data->usb_dev, 0), 707 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
707 USB_REQ_CLEAR_FEATURE,
708 USB_DIR_OUT | USB_TYPE_STANDARD |
709 USB_RECIP_ENDPOINT,
710 USB_ENDPOINT_HALT,
711 data->bulk_out, buffer, 0,
712 USBTMC_TIMEOUT);
713 if (rv < 0) { 708 if (rv < 0) {
714 dev_err(dev, "usb_control_msg returned %d\n", rv); 709 dev_err(dev, "usb_control_msg returned %d\n", rv);
715 goto exit; 710 goto exit;
@@ -730,13 +725,8 @@ static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
730 if (!buffer) 725 if (!buffer)
731 return -ENOMEM; 726 return -ENOMEM;
732 727
733 rv = usb_control_msg(data->usb_dev, 728 rv = usb_clear_halt(data->usb_dev,
734 usb_sndctrlpipe(data->usb_dev, 0), 729 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
735 USB_REQ_CLEAR_FEATURE,
736 USB_DIR_OUT | USB_TYPE_STANDARD |
737 USB_RECIP_ENDPOINT,
738 USB_ENDPOINT_HALT, data->bulk_out,
739 buffer, 0, USBTMC_TIMEOUT);
740 730
741 if (rv < 0) { 731 if (rv < 0) {
742 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", 732 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
@@ -759,12 +749,8 @@ static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
759 if (!buffer) 749 if (!buffer)
760 return -ENOMEM; 750 return -ENOMEM;
761 751
762 rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0), 752 rv = usb_clear_halt(data->usb_dev,
763 USB_REQ_CLEAR_FEATURE, 753 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
764 USB_DIR_OUT | USB_TYPE_STANDARD |
765 USB_RECIP_ENDPOINT,
766 USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
767 USBTMC_TIMEOUT);
768 754
769 if (rv < 0) { 755 if (rv < 0) {
770 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n", 756 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
@@ -1109,13 +1095,13 @@ static void usbtmc_disconnect(struct usb_interface *intf)
1109 kref_put(&data->kref, usbtmc_delete); 1095 kref_put(&data->kref, usbtmc_delete);
1110} 1096}
1111 1097
1112static int usbtmc_suspend (struct usb_interface *intf, pm_message_t message) 1098static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1113{ 1099{
1114 /* this driver does not have pending URBs */ 1100 /* this driver does not have pending URBs */
1115 return 0; 1101 return 0;
1116} 1102}
1117 1103
1118static int usbtmc_resume (struct usb_interface *intf) 1104static int usbtmc_resume(struct usb_interface *intf)
1119{ 1105{
1120 return 0; 1106 return 0;
1121} 1107}
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 4f864472c5c4..60a45f1e3a67 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -83,6 +83,47 @@ static ssize_t store_new_id(struct device_driver *driver,
83} 83}
84static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); 84static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
85 85
86/**
87 * store_remove_id - remove a USB device ID from this driver
88 * @driver: target device driver
89 * @buf: buffer for scanning device ID data
90 * @count: input size
91 *
92 * Removes a dynamic usb device ID from this driver.
93 */
94static ssize_t
95store_remove_id(struct device_driver *driver, const char *buf, size_t count)
96{
97 struct usb_dynid *dynid, *n;
98 struct usb_driver *usb_driver = to_usb_driver(driver);
99 u32 idVendor = 0;
100 u32 idProduct = 0;
101 int fields = 0;
102 int retval = 0;
103
104 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
105 if (fields < 2)
106 return -EINVAL;
107
108 spin_lock(&usb_driver->dynids.lock);
109 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
110 struct usb_device_id *id = &dynid->id;
111 if ((id->idVendor == idVendor) &&
112 (id->idProduct == idProduct)) {
113 list_del(&dynid->node);
114 kfree(dynid);
115 retval = 0;
116 break;
117 }
118 }
119 spin_unlock(&usb_driver->dynids.lock);
120
121 if (retval)
122 return retval;
123 return count;
124}
125static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
126
86static int usb_create_newid_file(struct usb_driver *usb_drv) 127static int usb_create_newid_file(struct usb_driver *usb_drv)
87{ 128{
88 int error = 0; 129 int error = 0;
@@ -107,6 +148,21 @@ static void usb_remove_newid_file(struct usb_driver *usb_drv)
107 &driver_attr_new_id); 148 &driver_attr_new_id);
108} 149}
109 150
151static int
152usb_create_removeid_file(struct usb_driver *drv)
153{
154 int error = 0;
155 if (drv->probe != NULL)
156 error = driver_create_file(&drv->drvwrap.driver,
157 &driver_attr_remove_id);
158 return error;
159}
160
161static void usb_remove_removeid_file(struct usb_driver *drv)
162{
163 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
164}
165
110static void usb_free_dynids(struct usb_driver *usb_drv) 166static void usb_free_dynids(struct usb_driver *usb_drv)
111{ 167{
112 struct usb_dynid *dynid, *n; 168 struct usb_dynid *dynid, *n;
@@ -128,6 +184,16 @@ static void usb_remove_newid_file(struct usb_driver *usb_drv)
128{ 184{
129} 185}
130 186
187static int
188usb_create_removeid_file(struct usb_driver *drv)
189{
190 return 0;
191}
192
193static void usb_remove_removeid_file(struct usb_driver *drv)
194{
195}
196
131static inline void usb_free_dynids(struct usb_driver *usb_drv) 197static inline void usb_free_dynids(struct usb_driver *usb_drv)
132{ 198{
133} 199}
@@ -774,19 +840,34 @@ int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
774 INIT_LIST_HEAD(&new_driver->dynids.list); 840 INIT_LIST_HEAD(&new_driver->dynids.list);
775 841
776 retval = driver_register(&new_driver->drvwrap.driver); 842 retval = driver_register(&new_driver->drvwrap.driver);
843 if (retval)
844 goto out;
777 845
778 if (!retval) { 846 usbfs_update_special();
779 pr_info("%s: registered new interface driver %s\n", 847
848 retval = usb_create_newid_file(new_driver);
849 if (retval)
850 goto out_newid;
851
852 retval = usb_create_removeid_file(new_driver);
853 if (retval)
854 goto out_removeid;
855
856 pr_info("%s: registered new interface driver %s\n",
780 usbcore_name, new_driver->name); 857 usbcore_name, new_driver->name);
781 usbfs_update_special();
782 usb_create_newid_file(new_driver);
783 } else {
784 printk(KERN_ERR "%s: error %d registering interface "
785 " driver %s\n",
786 usbcore_name, retval, new_driver->name);
787 }
788 858
859out:
789 return retval; 860 return retval;
861
862out_removeid:
863 usb_remove_newid_file(new_driver);
864out_newid:
865 driver_unregister(&new_driver->drvwrap.driver);
866
867 printk(KERN_ERR "%s: error %d registering interface "
868 " driver %s\n",
869 usbcore_name, retval, new_driver->name);
870 goto out;
790} 871}
791EXPORT_SYMBOL_GPL(usb_register_driver); 872EXPORT_SYMBOL_GPL(usb_register_driver);
792 873
@@ -806,6 +887,7 @@ void usb_deregister(struct usb_driver *driver)
806 pr_info("%s: deregistering interface driver %s\n", 887 pr_info("%s: deregistering interface driver %s\n",
807 usbcore_name, driver->name); 888 usbcore_name, driver->name);
808 889
890 usb_remove_removeid_file(driver);
809 usb_remove_newid_file(driver); 891 usb_remove_newid_file(driver);
810 usb_free_dynids(driver); 892 usb_free_dynids(driver);
811 driver_unregister(&driver->drvwrap.driver); 893 driver_unregister(&driver->drvwrap.driver);
@@ -948,8 +1030,6 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
948 1030
949 done: 1031 done:
950 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1032 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
951 if (status == 0)
952 udev->autoresume_disabled = 0;
953 return status; 1033 return status;
954} 1034}
955 1035
@@ -1280,11 +1360,6 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1280 1360
1281 /* Propagate the resume up the tree, if necessary */ 1361 /* Propagate the resume up the tree, if necessary */
1282 if (udev->state == USB_STATE_SUSPENDED) { 1362 if (udev->state == USB_STATE_SUSPENDED) {
1283 if ((msg.event & PM_EVENT_AUTO) &&
1284 udev->autoresume_disabled) {
1285 status = -EPERM;
1286 goto done;
1287 }
1288 if (parent) { 1363 if (parent) {
1289 status = usb_autoresume_device(parent); 1364 status = usb_autoresume_device(parent);
1290 if (status == 0) { 1365 if (status == 0) {
@@ -1341,7 +1416,6 @@ static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1341 int status = 0; 1416 int status = 0;
1342 1417
1343 usb_pm_lock(udev); 1418 usb_pm_lock(udev);
1344 udev->auto_pm = 1;
1345 udev->pm_usage_cnt += inc_usage_cnt; 1419 udev->pm_usage_cnt += inc_usage_cnt;
1346 WARN_ON(udev->pm_usage_cnt < 0); 1420 WARN_ON(udev->pm_usage_cnt < 0);
1347 if (inc_usage_cnt) 1421 if (inc_usage_cnt)
@@ -1473,7 +1547,6 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1473 if (intf->condition == USB_INTERFACE_UNBOUND) 1547 if (intf->condition == USB_INTERFACE_UNBOUND)
1474 status = -ENODEV; 1548 status = -ENODEV;
1475 else { 1549 else {
1476 udev->auto_pm = 1;
1477 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt); 1550 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
1478 udev->last_busy = jiffies; 1551 udev->last_busy = jiffies;
1479 if (inc_usage_cnt >= 0 && 1552 if (inc_usage_cnt >= 0 &&
@@ -1640,8 +1713,6 @@ int usb_autopm_get_interface_async(struct usb_interface *intf)
1640 1713
1641 if (intf->condition == USB_INTERFACE_UNBOUND) 1714 if (intf->condition == USB_INTERFACE_UNBOUND)
1642 status = -ENODEV; 1715 status = -ENODEV;
1643 else if (udev->autoresume_disabled)
1644 status = -EPERM;
1645 else { 1716 else {
1646 atomic_inc(&intf->pm_usage_cnt); 1717 atomic_inc(&intf->pm_usage_cnt);
1647 if (atomic_read(&intf->pm_usage_cnt) > 0 && 1718 if (atomic_read(&intf->pm_usage_cnt) > 0 &&
@@ -1654,28 +1725,6 @@ int usb_autopm_get_interface_async(struct usb_interface *intf)
1654} 1725}
1655EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1726EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1656 1727
1657/**
1658 * usb_autopm_set_interface - set a USB interface's autosuspend state
1659 * @intf: the usb_interface whose state should be set
1660 *
1661 * This routine sets the autosuspend state of @intf's device according
1662 * to @intf's usage counter, which the caller must have set previously.
1663 * If the counter is <= 0, the device is autosuspended (if it isn't
1664 * already suspended and if nothing else prevents the autosuspend). If
1665 * the counter is > 0, the device is autoresumed (if it isn't already
1666 * awake).
1667 */
1668int usb_autopm_set_interface(struct usb_interface *intf)
1669{
1670 int status;
1671
1672 status = usb_autopm_do_interface(intf, 0);
1673 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1674 __func__, status, atomic_read(&intf->pm_usage_cnt));
1675 return status;
1676}
1677EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1678
1679#else 1728#else
1680 1729
1681void usb_autosuspend_work(struct work_struct *work) 1730void usb_autosuspend_work(struct work_struct *work)
@@ -1707,7 +1756,6 @@ int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1707 1756
1708 do_unbind_rebind(udev, DO_UNBIND); 1757 do_unbind_rebind(udev, DO_UNBIND);
1709 usb_pm_lock(udev); 1758 usb_pm_lock(udev);
1710 udev->auto_pm = 0;
1711 status = usb_suspend_both(udev, msg); 1759 status = usb_suspend_both(udev, msg);
1712 usb_pm_unlock(udev); 1760 usb_pm_unlock(udev);
1713 return status; 1761 return status;
@@ -1730,7 +1778,6 @@ int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
1730 int status; 1778 int status;
1731 1779
1732 usb_pm_lock(udev); 1780 usb_pm_lock(udev);
1733 udev->auto_pm = 0;
1734 status = usb_resume_both(udev, msg); 1781 status = usb_resume_both(udev, msg);
1735 udev->last_busy = jiffies; 1782 udev->last_busy = jiffies;
1736 usb_pm_unlock(udev); 1783 usb_pm_unlock(udev);
diff --git a/drivers/usb/core/file.c b/drivers/usb/core/file.c
index 222ee07ea680..bfc6c2eea647 100644
--- a/drivers/usb/core/file.c
+++ b/drivers/usb/core/file.c
@@ -99,6 +99,7 @@ static int init_usb_class(void)
99 printk(KERN_ERR "class_create failed for usb devices\n"); 99 printk(KERN_ERR "class_create failed for usb devices\n");
100 kfree(usb_class); 100 kfree(usb_class);
101 usb_class = NULL; 101 usb_class = NULL;
102 goto exit;
102 } 103 }
103 usb_class->class->devnode = usb_devnode; 104 usb_class->class->devnode = usb_devnode;
104 105
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index 05e6d313961e..bdf87a8414a1 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -139,7 +139,7 @@ int usb_choose_configuration(struct usb_device *udev)
139 139
140 if (best) { 140 if (best) {
141 i = best->desc.bConfigurationValue; 141 i = best->desc.bConfigurationValue;
142 dev_info(&udev->dev, 142 dev_dbg(&udev->dev,
143 "configuration #%d chosen from %d choice%s\n", 143 "configuration #%d chosen from %d choice%s\n",
144 i, num_configs, plural(num_configs)); 144 i, num_configs, plural(num_configs));
145 } else { 145 } else {
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 34de475f016e..6dac3b802d41 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -38,6 +38,7 @@
38#include <asm/unaligned.h> 38#include <asm/unaligned.h>
39#include <linux/platform_device.h> 39#include <linux/platform_device.h>
40#include <linux/workqueue.h> 40#include <linux/workqueue.h>
41#include <linux/mutex.h>
41 42
42#include <linux/usb.h> 43#include <linux/usb.h>
43 44
@@ -1275,13 +1276,16 @@ static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1275 1276
1276 if (usb_endpoint_xfer_control(&urb->ep->desc) 1277 if (usb_endpoint_xfer_control(&urb->ep->desc)
1277 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) { 1278 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1278 if (hcd->self.uses_dma) 1279 if (hcd->self.uses_dma) {
1279 urb->setup_dma = dma_map_single( 1280 urb->setup_dma = dma_map_single(
1280 hcd->self.controller, 1281 hcd->self.controller,
1281 urb->setup_packet, 1282 urb->setup_packet,
1282 sizeof(struct usb_ctrlrequest), 1283 sizeof(struct usb_ctrlrequest),
1283 DMA_TO_DEVICE); 1284 DMA_TO_DEVICE);
1284 else if (hcd->driver->flags & HCD_LOCAL_MEM) 1285 if (dma_mapping_error(hcd->self.controller,
1286 urb->setup_dma))
1287 return -EAGAIN;
1288 } else if (hcd->driver->flags & HCD_LOCAL_MEM)
1285 ret = hcd_alloc_coherent( 1289 ret = hcd_alloc_coherent(
1286 urb->dev->bus, mem_flags, 1290 urb->dev->bus, mem_flags,
1287 &urb->setup_dma, 1291 &urb->setup_dma,
@@ -1293,13 +1297,16 @@ static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1293 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 1297 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1294 if (ret == 0 && urb->transfer_buffer_length != 0 1298 if (ret == 0 && urb->transfer_buffer_length != 0
1295 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { 1299 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1296 if (hcd->self.uses_dma) 1300 if (hcd->self.uses_dma) {
1297 urb->transfer_dma = dma_map_single ( 1301 urb->transfer_dma = dma_map_single (
1298 hcd->self.controller, 1302 hcd->self.controller,
1299 urb->transfer_buffer, 1303 urb->transfer_buffer,
1300 urb->transfer_buffer_length, 1304 urb->transfer_buffer_length,
1301 dir); 1305 dir);
1302 else if (hcd->driver->flags & HCD_LOCAL_MEM) { 1306 if (dma_mapping_error(hcd->self.controller,
1307 urb->transfer_dma))
1308 return -EAGAIN;
1309 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1303 ret = hcd_alloc_coherent( 1310 ret = hcd_alloc_coherent(
1304 urb->dev->bus, mem_flags, 1311 urb->dev->bus, mem_flags,
1305 &urb->transfer_dma, 1312 &urb->transfer_dma,
@@ -1589,19 +1596,32 @@ rescan:
1589 } 1596 }
1590} 1597}
1591 1598
1592/* Check whether a new configuration or alt setting for an interface 1599/**
1593 * will exceed the bandwidth for the bus (or the host controller resources). 1600 * Check whether a new bandwidth setting exceeds the bus bandwidth.
1594 * Only pass in a non-NULL config or interface, not both! 1601 * @new_config: new configuration to install
1595 * Passing NULL for both new_config and new_intf means the device will be 1602 * @cur_alt: the current alternate interface setting
1596 * de-configured by issuing a set configuration 0 command. 1603 * @new_alt: alternate interface setting that is being installed
1604 *
1605 * To change configurations, pass in the new configuration in new_config,
1606 * and pass NULL for cur_alt and new_alt.
1607 *
1608 * To reset a device's configuration (put the device in the ADDRESSED state),
1609 * pass in NULL for new_config, cur_alt, and new_alt.
1610 *
1611 * To change alternate interface settings, pass in NULL for new_config,
1612 * pass in the current alternate interface setting in cur_alt,
1613 * and pass in the new alternate interface setting in new_alt.
1614 *
1615 * Returns an error if the requested bandwidth change exceeds the
1616 * bus bandwidth or host controller internal resources.
1597 */ 1617 */
1598int usb_hcd_check_bandwidth(struct usb_device *udev, 1618int usb_hcd_alloc_bandwidth(struct usb_device *udev,
1599 struct usb_host_config *new_config, 1619 struct usb_host_config *new_config,
1600 struct usb_interface *new_intf) 1620 struct usb_host_interface *cur_alt,
1621 struct usb_host_interface *new_alt)
1601{ 1622{
1602 int num_intfs, i, j; 1623 int num_intfs, i, j;
1603 struct usb_interface_cache *intf_cache; 1624 struct usb_host_interface *alt = NULL;
1604 struct usb_host_interface *alt = 0;
1605 int ret = 0; 1625 int ret = 0;
1606 struct usb_hcd *hcd; 1626 struct usb_hcd *hcd;
1607 struct usb_host_endpoint *ep; 1627 struct usb_host_endpoint *ep;
@@ -1611,7 +1631,7 @@ int usb_hcd_check_bandwidth(struct usb_device *udev,
1611 return 0; 1631 return 0;
1612 1632
1613 /* Configuration is being removed - set configuration 0 */ 1633 /* Configuration is being removed - set configuration 0 */
1614 if (!new_config && !new_intf) { 1634 if (!new_config && !cur_alt) {
1615 for (i = 1; i < 16; ++i) { 1635 for (i = 1; i < 16; ++i) {
1616 ep = udev->ep_out[i]; 1636 ep = udev->ep_out[i];
1617 if (ep) 1637 if (ep)
@@ -1648,19 +1668,12 @@ int usb_hcd_check_bandwidth(struct usb_device *udev,
1648 } 1668 }
1649 } 1669 }
1650 for (i = 0; i < num_intfs; ++i) { 1670 for (i = 0; i < num_intfs; ++i) {
1671 /* Set up endpoints for alternate interface setting 0 */
1672 alt = usb_find_alt_setting(new_config, i, 0);
1673 if (!alt)
1674 /* No alt setting 0? Pick the first setting. */
1675 alt = &new_config->intf_cache[i]->altsetting[0];
1651 1676
1652 /* Dig the endpoints for alt setting 0 out of the
1653 * interface cache for this interface
1654 */
1655 intf_cache = new_config->intf_cache[i];
1656 for (j = 0; j < intf_cache->num_altsetting; j++) {
1657 if (intf_cache->altsetting[j].desc.bAlternateSetting == 0)
1658 alt = &intf_cache->altsetting[j];
1659 }
1660 if (!alt) {
1661 printk(KERN_DEBUG "Did not find alt setting 0 for intf %d\n", i);
1662 continue;
1663 }
1664 for (j = 0; j < alt->desc.bNumEndpoints; j++) { 1677 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1665 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); 1678 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1666 if (ret < 0) 1679 if (ret < 0)
@@ -1668,6 +1681,22 @@ int usb_hcd_check_bandwidth(struct usb_device *udev,
1668 } 1681 }
1669 } 1682 }
1670 } 1683 }
1684 if (cur_alt && new_alt) {
1685 /* Drop all the endpoints in the current alt setting */
1686 for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
1687 ret = hcd->driver->drop_endpoint(hcd, udev,
1688 &cur_alt->endpoint[i]);
1689 if (ret < 0)
1690 goto reset;
1691 }
1692 /* Add all the endpoints in the new alt setting */
1693 for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
1694 ret = hcd->driver->add_endpoint(hcd, udev,
1695 &new_alt->endpoint[i]);
1696 if (ret < 0)
1697 goto reset;
1698 }
1699 }
1671 ret = hcd->driver->check_bandwidth(hcd, udev); 1700 ret = hcd->driver->check_bandwidth(hcd, udev);
1672reset: 1701reset:
1673 if (ret < 0) 1702 if (ret < 0)
@@ -1984,6 +2013,7 @@ struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1984#ifdef CONFIG_PM 2013#ifdef CONFIG_PM
1985 INIT_WORK(&hcd->wakeup_work, hcd_resume_work); 2014 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
1986#endif 2015#endif
2016 mutex_init(&hcd->bandwidth_mutex);
1987 2017
1988 hcd->driver = driver; 2018 hcd->driver = driver;
1989 hcd->product_desc = (driver->product_desc) ? driver->product_desc : 2019 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 79782a1c43f6..d8b43aee581e 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -111,6 +111,20 @@ struct usb_hcd {
111 u64 rsrc_len; /* memory/io resource length */ 111 u64 rsrc_len; /* memory/io resource length */
112 unsigned power_budget; /* in mA, 0 = no limit */ 112 unsigned power_budget; /* in mA, 0 = no limit */
113 113
114 /* bandwidth_mutex should be taken before adding or removing
115 * any new bus bandwidth constraints:
116 * 1. Before adding a configuration for a new device.
117 * 2. Before removing the configuration to put the device into
118 * the addressed state.
119 * 3. Before selecting a different configuration.
120 * 4. Before selecting an alternate interface setting.
121 *
122 * bandwidth_mutex should be dropped after a successful control message
123 * to the device, or resetting the bandwidth after a failed attempt.
124 */
125 struct mutex bandwidth_mutex;
126
127
114#define HCD_BUFFER_POOLS 4 128#define HCD_BUFFER_POOLS 4
115 struct dma_pool *pool [HCD_BUFFER_POOLS]; 129 struct dma_pool *pool [HCD_BUFFER_POOLS];
116 130
@@ -290,9 +304,10 @@ extern void usb_hcd_disable_endpoint(struct usb_device *udev,
290extern void usb_hcd_reset_endpoint(struct usb_device *udev, 304extern void usb_hcd_reset_endpoint(struct usb_device *udev,
291 struct usb_host_endpoint *ep); 305 struct usb_host_endpoint *ep);
292extern void usb_hcd_synchronize_unlinks(struct usb_device *udev); 306extern void usb_hcd_synchronize_unlinks(struct usb_device *udev);
293extern int usb_hcd_check_bandwidth(struct usb_device *udev, 307extern int usb_hcd_alloc_bandwidth(struct usb_device *udev,
294 struct usb_host_config *new_config, 308 struct usb_host_config *new_config,
295 struct usb_interface *new_intf); 309 struct usb_host_interface *old_alt,
310 struct usb_host_interface *new_alt);
296extern int usb_hcd_get_frame_number(struct usb_device *udev); 311extern int usb_hcd_get_frame_number(struct usb_device *udev);
297 312
298extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver, 313extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 0f857e645058..06af970e1064 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -45,7 +45,6 @@ struct usb_hub {
45 45
46 /* buffer for urb ... with extra space in case of babble */ 46 /* buffer for urb ... with extra space in case of babble */
47 char (*buffer)[8]; 47 char (*buffer)[8];
48 dma_addr_t buffer_dma; /* DMA address for buffer */
49 union { 48 union {
50 struct usb_hub_status hub; 49 struct usb_hub_status hub;
51 struct usb_port_status port; 50 struct usb_port_status port;
@@ -61,6 +60,8 @@ struct usb_hub {
61 status change */ 60 status change */
62 unsigned long busy_bits[1]; /* ports being reset or 61 unsigned long busy_bits[1]; /* ports being reset or
63 resumed */ 62 resumed */
63 unsigned long removed_bits[1]; /* ports with a "removed"
64 device present */
64#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ 65#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
65#error event_bits[] is too short! 66#error event_bits[] is too short!
66#endif 67#endif
@@ -70,6 +71,7 @@ struct usb_hub {
70 71
71 unsigned mA_per_port; /* current for each child */ 72 unsigned mA_per_port; /* current for each child */
72 73
74 unsigned init_done:1;
73 unsigned limited_power:1; 75 unsigned limited_power:1;
74 unsigned quiescing:1; 76 unsigned quiescing:1;
75 unsigned disconnected:1; 77 unsigned disconnected:1;
@@ -374,12 +376,13 @@ static void kick_khubd(struct usb_hub *hub)
374{ 376{
375 unsigned long flags; 377 unsigned long flags;
376 378
377 /* Suppress autosuspend until khubd runs */
378 atomic_set(&to_usb_interface(hub->intfdev)->pm_usage_cnt, 1);
379
380 spin_lock_irqsave(&hub_event_lock, flags); 379 spin_lock_irqsave(&hub_event_lock, flags);
381 if (!hub->disconnected && list_empty(&hub->event_list)) { 380 if (!hub->disconnected && list_empty(&hub->event_list)) {
382 list_add_tail(&hub->event_list, &hub_event_list); 381 list_add_tail(&hub->event_list, &hub_event_list);
382
383 /* Suppress autosuspend until khubd runs */
384 usb_autopm_get_interface_no_resume(
385 to_usb_interface(hub->intfdev));
383 wake_up(&khubd_wait); 386 wake_up(&khubd_wait);
384 } 387 }
385 spin_unlock_irqrestore(&hub_event_lock, flags); 388 spin_unlock_irqrestore(&hub_event_lock, flags);
@@ -636,8 +639,35 @@ static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
636 kick_khubd(hub); 639 kick_khubd(hub);
637} 640}
638 641
642/**
643 * usb_remove_device - disable a device's port on its parent hub
644 * @udev: device to be disabled and removed
645 * Context: @udev locked, must be able to sleep.
646 *
647 * After @udev's port has been disabled, khubd is notified and it will
648 * see that the device has been disconnected. When the device is
649 * physically unplugged and something is plugged in, the events will
650 * be received and processed normally.
651 */
652int usb_remove_device(struct usb_device *udev)
653{
654 struct usb_hub *hub;
655 struct usb_interface *intf;
656
657 if (!udev->parent) /* Can't remove a root hub */
658 return -EINVAL;
659 hub = hdev_to_hub(udev->parent);
660 intf = to_usb_interface(hub->intfdev);
661
662 usb_autopm_get_interface(intf);
663 set_bit(udev->portnum, hub->removed_bits);
664 hub_port_logical_disconnect(hub, udev->portnum);
665 usb_autopm_put_interface(intf);
666 return 0;
667}
668
639enum hub_activation_type { 669enum hub_activation_type {
640 HUB_INIT, HUB_INIT2, HUB_INIT3, 670 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
641 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 671 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
642}; 672};
643 673
@@ -682,8 +712,8 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
682 msecs_to_jiffies(delay)); 712 msecs_to_jiffies(delay));
683 713
684 /* Suppress autosuspend until init is done */ 714 /* Suppress autosuspend until init is done */
685 atomic_set(&to_usb_interface(hub->intfdev)-> 715 usb_autopm_get_interface_no_resume(
686 pm_usage_cnt, 1); 716 to_usb_interface(hub->intfdev));
687 return; /* Continues at init2: below */ 717 return; /* Continues at init2: below */
688 } else { 718 } else {
689 hub_power_on(hub, true); 719 hub_power_on(hub, true);
@@ -731,6 +761,13 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
731 USB_PORT_FEAT_C_ENABLE); 761 USB_PORT_FEAT_C_ENABLE);
732 } 762 }
733 763
764 /* We can forget about a "removed" device when there's a
765 * physical disconnect or the connect status changes.
766 */
767 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
768 (portchange & USB_PORT_STAT_C_CONNECTION))
769 clear_bit(port1, hub->removed_bits);
770
734 if (!udev || udev->state == USB_STATE_NOTATTACHED) { 771 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
735 /* Tell khubd to disconnect the device or 772 /* Tell khubd to disconnect the device or
736 * check for a new connection 773 * check for a new connection
@@ -783,6 +820,7 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
783 } 820 }
784 init3: 821 init3:
785 hub->quiescing = 0; 822 hub->quiescing = 0;
823 hub->init_done = 1;
786 824
787 status = usb_submit_urb(hub->urb, GFP_NOIO); 825 status = usb_submit_urb(hub->urb, GFP_NOIO);
788 if (status < 0) 826 if (status < 0)
@@ -792,6 +830,10 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
792 830
793 /* Scan all ports that need attention */ 831 /* Scan all ports that need attention */
794 kick_khubd(hub); 832 kick_khubd(hub);
833
834 /* Allow autosuspend if it was suppressed */
835 if (type <= HUB_INIT3)
836 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
795} 837}
796 838
797/* Implement the continuations for the delays above */ 839/* Implement the continuations for the delays above */
@@ -819,6 +861,11 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
819 int i; 861 int i;
820 862
821 cancel_delayed_work_sync(&hub->init_work); 863 cancel_delayed_work_sync(&hub->init_work);
864 if (!hub->init_done) {
865 hub->init_done = 1;
866 usb_autopm_put_interface_no_suspend(
867 to_usb_interface(hub->intfdev));
868 }
822 869
823 /* khubd and related activity won't re-trigger */ 870 /* khubd and related activity won't re-trigger */
824 hub->quiescing = 1; 871 hub->quiescing = 1;
@@ -869,8 +916,7 @@ static int hub_configure(struct usb_hub *hub,
869 int maxp, ret; 916 int maxp, ret;
870 char *message = "out of memory"; 917 char *message = "out of memory";
871 918
872 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL, 919 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
873 &hub->buffer_dma);
874 if (!hub->buffer) { 920 if (!hub->buffer) {
875 ret = -ENOMEM; 921 ret = -ENOMEM;
876 goto fail; 922 goto fail;
@@ -1111,8 +1157,6 @@ static int hub_configure(struct usb_hub *hub,
1111 1157
1112 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 1158 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1113 hub, endpoint->bInterval); 1159 hub, endpoint->bInterval);
1114 hub->urb->transfer_dma = hub->buffer_dma;
1115 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1116 1160
1117 /* maybe cycle the hub leds */ 1161 /* maybe cycle the hub leds */
1118 if (hub->has_indicators && blinkenlights) 1162 if (hub->has_indicators && blinkenlights)
@@ -1144,7 +1188,10 @@ static void hub_disconnect(struct usb_interface *intf)
1144 1188
1145 /* Take the hub off the event list and don't let it be added again */ 1189 /* Take the hub off the event list and don't let it be added again */
1146 spin_lock_irq(&hub_event_lock); 1190 spin_lock_irq(&hub_event_lock);
1147 list_del_init(&hub->event_list); 1191 if (!list_empty(&hub->event_list)) {
1192 list_del_init(&hub->event_list);
1193 usb_autopm_put_interface_no_suspend(intf);
1194 }
1148 hub->disconnected = 1; 1195 hub->disconnected = 1;
1149 spin_unlock_irq(&hub_event_lock); 1196 spin_unlock_irq(&hub_event_lock);
1150 1197
@@ -1162,8 +1209,7 @@ static void hub_disconnect(struct usb_interface *intf)
1162 kfree(hub->port_owners); 1209 kfree(hub->port_owners);
1163 kfree(hub->descriptor); 1210 kfree(hub->descriptor);
1164 kfree(hub->status); 1211 kfree(hub->status);
1165 usb_buffer_free(hub->hdev, sizeof(*hub->buffer), hub->buffer, 1212 kfree(hub->buffer);
1166 hub->buffer_dma);
1167 1213
1168 kref_put(&hub->kref, hub_release); 1214 kref_put(&hub->kref, hub_release);
1169} 1215}
@@ -1630,7 +1676,7 @@ static int usb_configure_device_otg(struct usb_device *udev)
1630 if (!udev->bus->is_b_host 1676 if (!udev->bus->is_b_host
1631 && udev->config 1677 && udev->config
1632 && udev->parent == udev->bus->root_hub) { 1678 && udev->parent == udev->bus->root_hub) {
1633 struct usb_otg_descriptor *desc = 0; 1679 struct usb_otg_descriptor *desc = NULL;
1634 struct usb_bus *bus = udev->bus; 1680 struct usb_bus *bus = udev->bus;
1635 1681
1636 /* descriptor may appear anywhere in config */ 1682 /* descriptor may appear anywhere in config */
@@ -2123,9 +2169,13 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2123 USB_DEVICE_REMOTE_WAKEUP, 0, 2169 USB_DEVICE_REMOTE_WAKEUP, 0,
2124 NULL, 0, 2170 NULL, 0,
2125 USB_CTRL_SET_TIMEOUT); 2171 USB_CTRL_SET_TIMEOUT);
2126 if (status) 2172 if (status) {
2127 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 2173 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2128 status); 2174 status);
2175 /* bail if autosuspend is requested */
2176 if (msg.event & PM_EVENT_AUTO)
2177 return status;
2178 }
2129 } 2179 }
2130 2180
2131 /* see 7.1.7.6 */ 2181 /* see 7.1.7.6 */
@@ -2134,7 +2184,8 @@ int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2134 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", 2184 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2135 port1, status); 2185 port1, status);
2136 /* paranoia: "should not happen" */ 2186 /* paranoia: "should not happen" */
2137 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 2187 if (udev->do_remote_wakeup)
2188 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2138 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 2189 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2139 USB_DEVICE_REMOTE_WAKEUP, 0, 2190 USB_DEVICE_REMOTE_WAKEUP, 0,
2140 NULL, 0, 2191 NULL, 0,
@@ -2965,6 +3016,13 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2965 usb_disconnect(&hdev->children[port1-1]); 3016 usb_disconnect(&hdev->children[port1-1]);
2966 clear_bit(port1, hub->change_bits); 3017 clear_bit(port1, hub->change_bits);
2967 3018
3019 /* We can forget about a "removed" device when there's a physical
3020 * disconnect or the connect status changes.
3021 */
3022 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3023 (portchange & USB_PORT_STAT_C_CONNECTION))
3024 clear_bit(port1, hub->removed_bits);
3025
2968 if (portchange & (USB_PORT_STAT_C_CONNECTION | 3026 if (portchange & (USB_PORT_STAT_C_CONNECTION |
2969 USB_PORT_STAT_C_ENABLE)) { 3027 USB_PORT_STAT_C_ENABLE)) {
2970 status = hub_port_debounce(hub, port1); 3028 status = hub_port_debounce(hub, port1);
@@ -2978,8 +3036,11 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2978 } 3036 }
2979 } 3037 }
2980 3038
2981 /* Return now if debouncing failed or nothing is connected */ 3039 /* Return now if debouncing failed or nothing is connected or
2982 if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 3040 * the device was "removed".
3041 */
3042 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3043 test_bit(port1, hub->removed_bits)) {
2983 3044
2984 /* maybe switch power back on (e.g. root hub was reset) */ 3045 /* maybe switch power back on (e.g. root hub was reset) */
2985 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 3046 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
@@ -3189,7 +3250,7 @@ static void hub_events(void)
3189 * disconnected while waiting for the lock to succeed. */ 3250 * disconnected while waiting for the lock to succeed. */
3190 usb_lock_device(hdev); 3251 usb_lock_device(hdev);
3191 if (unlikely(hub->disconnected)) 3252 if (unlikely(hub->disconnected))
3192 goto loop; 3253 goto loop2;
3193 3254
3194 /* If the hub has died, clean up after it */ 3255 /* If the hub has died, clean up after it */
3195 if (hdev->state == USB_STATE_NOTATTACHED) { 3256 if (hdev->state == USB_STATE_NOTATTACHED) {
@@ -3338,11 +3399,15 @@ static void hub_events(void)
3338 } 3399 }
3339 } 3400 }
3340 3401
3341loop_autopm: 3402 loop_autopm:
3342 /* Allow autosuspend if we're not going to run again */ 3403 /* Balance the usb_autopm_get_interface() above */
3343 if (list_empty(&hub->event_list)) 3404 usb_autopm_put_interface_no_suspend(intf);
3344 usb_autopm_enable(intf); 3405 loop:
3345loop: 3406 /* Balance the usb_autopm_get_interface_no_resume() in
3407 * kick_khubd() and allow autosuspend.
3408 */
3409 usb_autopm_put_interface(intf);
3410 loop2:
3346 usb_unlock_device(hdev); 3411 usb_unlock_device(hdev);
3347 kref_put(&hub->kref, hub_release); 3412 kref_put(&hub->kref, hub_release);
3348 3413
@@ -3534,6 +3599,7 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
3534{ 3599{
3535 struct usb_device *parent_hdev = udev->parent; 3600 struct usb_device *parent_hdev = udev->parent;
3536 struct usb_hub *parent_hub; 3601 struct usb_hub *parent_hub;
3602 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3537 struct usb_device_descriptor descriptor = udev->descriptor; 3603 struct usb_device_descriptor descriptor = udev->descriptor;
3538 int i, ret = 0; 3604 int i, ret = 0;
3539 int port1 = udev->portnum; 3605 int port1 = udev->portnum;
@@ -3577,6 +3643,16 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
3577 /* Restore the device's previous configuration */ 3643 /* Restore the device's previous configuration */
3578 if (!udev->actconfig) 3644 if (!udev->actconfig)
3579 goto done; 3645 goto done;
3646
3647 mutex_lock(&hcd->bandwidth_mutex);
3648 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
3649 if (ret < 0) {
3650 dev_warn(&udev->dev,
3651 "Busted HC? Not enough HCD resources for "
3652 "old configuration.\n");
3653 mutex_unlock(&hcd->bandwidth_mutex);
3654 goto re_enumerate;
3655 }
3580 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3656 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3581 USB_REQ_SET_CONFIGURATION, 0, 3657 USB_REQ_SET_CONFIGURATION, 0,
3582 udev->actconfig->desc.bConfigurationValue, 0, 3658 udev->actconfig->desc.bConfigurationValue, 0,
@@ -3585,8 +3661,10 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
3585 dev_err(&udev->dev, 3661 dev_err(&udev->dev,
3586 "can't restore configuration #%d (error=%d)\n", 3662 "can't restore configuration #%d (error=%d)\n",
3587 udev->actconfig->desc.bConfigurationValue, ret); 3663 udev->actconfig->desc.bConfigurationValue, ret);
3664 mutex_unlock(&hcd->bandwidth_mutex);
3588 goto re_enumerate; 3665 goto re_enumerate;
3589 } 3666 }
3667 mutex_unlock(&hcd->bandwidth_mutex);
3590 usb_set_device_state(udev, USB_STATE_CONFIGURED); 3668 usb_set_device_state(udev, USB_STATE_CONFIGURED);
3591 3669
3592 /* Put interfaces back into the same altsettings as before. 3670 /* Put interfaces back into the same altsettings as before.
@@ -3596,7 +3674,8 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
3596 * endpoint state. 3674 * endpoint state.
3597 */ 3675 */
3598 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 3676 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3599 struct usb_interface *intf = udev->actconfig->interface[i]; 3677 struct usb_host_config *config = udev->actconfig;
3678 struct usb_interface *intf = config->interface[i];
3600 struct usb_interface_descriptor *desc; 3679 struct usb_interface_descriptor *desc;
3601 3680
3602 desc = &intf->cur_altsetting->desc; 3681 desc = &intf->cur_altsetting->desc;
@@ -3605,6 +3684,17 @@ static int usb_reset_and_verify_device(struct usb_device *udev)
3605 usb_enable_interface(udev, intf, true); 3684 usb_enable_interface(udev, intf, true);
3606 ret = 0; 3685 ret = 0;
3607 } else { 3686 } else {
3687 /* We've just reset the device, so it will think alt
3688 * setting 0 is installed. For usb_set_interface() to
3689 * work properly, we need to set the current alternate
3690 * interface setting to 0 (or the first alt setting, if
3691 * the device doesn't have alt setting 0).
3692 */
3693 intf->cur_altsetting =
3694 usb_find_alt_setting(config, i, 0);
3695 if (!intf->cur_altsetting)
3696 intf->cur_altsetting =
3697 &config->intf_cache[i]->altsetting[0];
3608 ret = usb_set_interface(udev, desc->bInterfaceNumber, 3698 ret = usb_set_interface(udev, desc->bInterfaceNumber,
3609 desc->bAlternateSetting); 3699 desc->bAlternateSetting);
3610 } 3700 }
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index e80f1af438c8..1b994846e8e0 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -393,13 +393,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
393 if (io->entries <= 0) 393 if (io->entries <= 0)
394 return io->entries; 394 return io->entries;
395 395
396 /* If we're running on an xHCI host controller, queue the whole scatter 396 if (dev->bus->sg_tablesize > 0) {
397 * gather list with one call to urb_enqueue(). This is only for bulk,
398 * as that endpoint type does not care how the data gets broken up
399 * across frames.
400 */
401 if (usb_pipebulk(pipe) &&
402 bus_to_hcd(dev->bus)->driver->flags & HCD_USB3) {
403 io->urbs = kmalloc(sizeof *io->urbs, mem_flags); 397 io->urbs = kmalloc(sizeof *io->urbs, mem_flags);
404 use_sg = true; 398 use_sg = true;
405 } else { 399 } else {
@@ -409,7 +403,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
409 if (!io->urbs) 403 if (!io->urbs)
410 goto nomem; 404 goto nomem;
411 405
412 urb_flags = URB_NO_INTERRUPT; 406 urb_flags = 0;
413 if (dma) 407 if (dma)
414 urb_flags |= URB_NO_TRANSFER_DMA_MAP; 408 urb_flags |= URB_NO_TRANSFER_DMA_MAP;
415 if (usb_pipein(pipe)) 409 if (usb_pipein(pipe))
@@ -441,6 +435,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
441 io->urbs[0]->num_sgs = io->entries; 435 io->urbs[0]->num_sgs = io->entries;
442 io->entries = 1; 436 io->entries = 1;
443 } else { 437 } else {
438 urb_flags |= URB_NO_INTERRUPT;
444 for_each_sg(sg, sg, io->entries, i) { 439 for_each_sg(sg, sg, io->entries, i) {
445 unsigned len; 440 unsigned len;
446 441
@@ -1303,6 +1298,7 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1303{ 1298{
1304 struct usb_interface *iface; 1299 struct usb_interface *iface;
1305 struct usb_host_interface *alt; 1300 struct usb_host_interface *alt;
1301 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1306 int ret; 1302 int ret;
1307 int manual = 0; 1303 int manual = 0;
1308 unsigned int epaddr; 1304 unsigned int epaddr;
@@ -1325,6 +1321,18 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1325 return -EINVAL; 1321 return -EINVAL;
1326 } 1322 }
1327 1323
1324 /* Make sure we have enough bandwidth for this alternate interface.
1325 * Remove the current alt setting and add the new alt setting.
1326 */
1327 mutex_lock(&hcd->bandwidth_mutex);
1328 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1329 if (ret < 0) {
1330 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1331 alternate);
1332 mutex_unlock(&hcd->bandwidth_mutex);
1333 return ret;
1334 }
1335
1328 if (dev->quirks & USB_QUIRK_NO_SET_INTF) 1336 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1329 ret = -EPIPE; 1337 ret = -EPIPE;
1330 else 1338 else
@@ -1340,8 +1348,13 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1340 "manual set_interface for iface %d, alt %d\n", 1348 "manual set_interface for iface %d, alt %d\n",
1341 interface, alternate); 1349 interface, alternate);
1342 manual = 1; 1350 manual = 1;
1343 } else if (ret < 0) 1351 } else if (ret < 0) {
1352 /* Re-instate the old alt setting */
1353 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
1354 mutex_unlock(&hcd->bandwidth_mutex);
1344 return ret; 1355 return ret;
1356 }
1357 mutex_unlock(&hcd->bandwidth_mutex);
1345 1358
1346 /* FIXME drivers shouldn't need to replicate/bugfix the logic here 1359 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1347 * when they implement async or easily-killable versions of this or 1360 * when they implement async or easily-killable versions of this or
@@ -1423,6 +1436,7 @@ int usb_reset_configuration(struct usb_device *dev)
1423{ 1436{
1424 int i, retval; 1437 int i, retval;
1425 struct usb_host_config *config; 1438 struct usb_host_config *config;
1439 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1426 1440
1427 if (dev->state == USB_STATE_SUSPENDED) 1441 if (dev->state == USB_STATE_SUSPENDED)
1428 return -EHOSTUNREACH; 1442 return -EHOSTUNREACH;
@@ -1438,12 +1452,46 @@ int usb_reset_configuration(struct usb_device *dev)
1438 } 1452 }
1439 1453
1440 config = dev->actconfig; 1454 config = dev->actconfig;
1455 retval = 0;
1456 mutex_lock(&hcd->bandwidth_mutex);
1457 /* Make sure we have enough bandwidth for each alternate setting 0 */
1458 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1459 struct usb_interface *intf = config->interface[i];
1460 struct usb_host_interface *alt;
1461
1462 alt = usb_altnum_to_altsetting(intf, 0);
1463 if (!alt)
1464 alt = &intf->altsetting[0];
1465 if (alt != intf->cur_altsetting)
1466 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1467 intf->cur_altsetting, alt);
1468 if (retval < 0)
1469 break;
1470 }
1471 /* If not, reinstate the old alternate settings */
1472 if (retval < 0) {
1473reset_old_alts:
1474 for (; i >= 0; i--) {
1475 struct usb_interface *intf = config->interface[i];
1476 struct usb_host_interface *alt;
1477
1478 alt = usb_altnum_to_altsetting(intf, 0);
1479 if (!alt)
1480 alt = &intf->altsetting[0];
1481 if (alt != intf->cur_altsetting)
1482 usb_hcd_alloc_bandwidth(dev, NULL,
1483 alt, intf->cur_altsetting);
1484 }
1485 mutex_unlock(&hcd->bandwidth_mutex);
1486 return retval;
1487 }
1441 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1488 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1442 USB_REQ_SET_CONFIGURATION, 0, 1489 USB_REQ_SET_CONFIGURATION, 0,
1443 config->desc.bConfigurationValue, 0, 1490 config->desc.bConfigurationValue, 0,
1444 NULL, 0, USB_CTRL_SET_TIMEOUT); 1491 NULL, 0, USB_CTRL_SET_TIMEOUT);
1445 if (retval < 0) 1492 if (retval < 0)
1446 return retval; 1493 goto reset_old_alts;
1494 mutex_unlock(&hcd->bandwidth_mutex);
1447 1495
1448 /* re-init hc/hcd interface/endpoint state */ 1496 /* re-init hc/hcd interface/endpoint state */
1449 for (i = 0; i < config->desc.bNumInterfaces; i++) { 1497 for (i = 0; i < config->desc.bNumInterfaces; i++) {
@@ -1585,7 +1633,7 @@ static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1585 * 1633 *
1586 * See usb_queue_reset_device() for more details 1634 * See usb_queue_reset_device() for more details
1587 */ 1635 */
1588void __usb_queue_reset_device(struct work_struct *ws) 1636static void __usb_queue_reset_device(struct work_struct *ws)
1589{ 1637{
1590 int rc; 1638 int rc;
1591 struct usb_interface *iface = 1639 struct usb_interface *iface =
@@ -1652,6 +1700,7 @@ int usb_set_configuration(struct usb_device *dev, int configuration)
1652 int i, ret; 1700 int i, ret;
1653 struct usb_host_config *cp = NULL; 1701 struct usb_host_config *cp = NULL;
1654 struct usb_interface **new_interfaces = NULL; 1702 struct usb_interface **new_interfaces = NULL;
1703 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1655 int n, nintf; 1704 int n, nintf;
1656 1705
1657 if (dev->authorized == 0 || configuration == -1) 1706 if (dev->authorized == 0 || configuration == -1)
@@ -1721,12 +1770,11 @@ free_interfaces:
1721 * host controller will not allow submissions to dropped endpoints. If 1770 * host controller will not allow submissions to dropped endpoints. If
1722 * this call fails, the device state is unchanged. 1771 * this call fails, the device state is unchanged.
1723 */ 1772 */
1724 if (cp) 1773 mutex_lock(&hcd->bandwidth_mutex);
1725 ret = usb_hcd_check_bandwidth(dev, cp, NULL); 1774 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
1726 else
1727 ret = usb_hcd_check_bandwidth(dev, NULL, NULL);
1728 if (ret < 0) { 1775 if (ret < 0) {
1729 usb_autosuspend_device(dev); 1776 usb_autosuspend_device(dev);
1777 mutex_unlock(&hcd->bandwidth_mutex);
1730 goto free_interfaces; 1778 goto free_interfaces;
1731 } 1779 }
1732 1780
@@ -1752,10 +1800,12 @@ free_interfaces:
1752 dev->actconfig = cp; 1800 dev->actconfig = cp;
1753 if (!cp) { 1801 if (!cp) {
1754 usb_set_device_state(dev, USB_STATE_ADDRESS); 1802 usb_set_device_state(dev, USB_STATE_ADDRESS);
1755 usb_hcd_check_bandwidth(dev, NULL, NULL); 1803 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1756 usb_autosuspend_device(dev); 1804 usb_autosuspend_device(dev);
1805 mutex_unlock(&hcd->bandwidth_mutex);
1757 goto free_interfaces; 1806 goto free_interfaces;
1758 } 1807 }
1808 mutex_unlock(&hcd->bandwidth_mutex);
1759 usb_set_device_state(dev, USB_STATE_CONFIGURED); 1809 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1760 1810
1761 /* Initialize the new interface structures and the 1811 /* Initialize the new interface structures and the
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 7ec3041ae79e..15477008b631 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -139,6 +139,16 @@ show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
139static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL); 139static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
140 140
141static ssize_t 141static ssize_t
142show_devpath(struct device *dev, struct device_attribute *attr, char *buf)
143{
144 struct usb_device *udev;
145
146 udev = to_usb_device(dev);
147 return sprintf(buf, "%s\n", udev->devpath);
148}
149static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL);
150
151static ssize_t
142show_version(struct device *dev, struct device_attribute *attr, char *buf) 152show_version(struct device *dev, struct device_attribute *attr, char *buf)
143{ 153{
144 struct usb_device *udev; 154 struct usb_device *udev;
@@ -317,7 +327,6 @@ static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
317 327
318static const char on_string[] = "on"; 328static const char on_string[] = "on";
319static const char auto_string[] = "auto"; 329static const char auto_string[] = "auto";
320static const char suspend_string[] = "suspend";
321 330
322static ssize_t 331static ssize_t
323show_level(struct device *dev, struct device_attribute *attr, char *buf) 332show_level(struct device *dev, struct device_attribute *attr, char *buf)
@@ -325,13 +334,8 @@ show_level(struct device *dev, struct device_attribute *attr, char *buf)
325 struct usb_device *udev = to_usb_device(dev); 334 struct usb_device *udev = to_usb_device(dev);
326 const char *p = auto_string; 335 const char *p = auto_string;
327 336
328 if (udev->state == USB_STATE_SUSPENDED) { 337 if (udev->state != USB_STATE_SUSPENDED && udev->autosuspend_disabled)
329 if (udev->autoresume_disabled) 338 p = on_string;
330 p = suspend_string;
331 } else {
332 if (udev->autosuspend_disabled)
333 p = on_string;
334 }
335 return sprintf(buf, "%s\n", p); 339 return sprintf(buf, "%s\n", p);
336} 340}
337 341
@@ -343,7 +347,7 @@ set_level(struct device *dev, struct device_attribute *attr,
343 int len = count; 347 int len = count;
344 char *cp; 348 char *cp;
345 int rc = 0; 349 int rc = 0;
346 int old_autosuspend_disabled, old_autoresume_disabled; 350 int old_autosuspend_disabled;
347 351
348 cp = memchr(buf, '\n', count); 352 cp = memchr(buf, '\n', count);
349 if (cp) 353 if (cp)
@@ -351,7 +355,6 @@ set_level(struct device *dev, struct device_attribute *attr,
351 355
352 usb_lock_device(udev); 356 usb_lock_device(udev);
353 old_autosuspend_disabled = udev->autosuspend_disabled; 357 old_autosuspend_disabled = udev->autosuspend_disabled;
354 old_autoresume_disabled = udev->autoresume_disabled;
355 358
356 /* Setting the flags without calling usb_pm_lock is a subject to 359 /* Setting the flags without calling usb_pm_lock is a subject to
357 * races, but who cares... 360 * races, but who cares...
@@ -359,28 +362,18 @@ set_level(struct device *dev, struct device_attribute *attr,
359 if (len == sizeof on_string - 1 && 362 if (len == sizeof on_string - 1 &&
360 strncmp(buf, on_string, len) == 0) { 363 strncmp(buf, on_string, len) == 0) {
361 udev->autosuspend_disabled = 1; 364 udev->autosuspend_disabled = 1;
362 udev->autoresume_disabled = 0;
363 rc = usb_external_resume_device(udev, PMSG_USER_RESUME); 365 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
364 366
365 } else if (len == sizeof auto_string - 1 && 367 } else if (len == sizeof auto_string - 1 &&
366 strncmp(buf, auto_string, len) == 0) { 368 strncmp(buf, auto_string, len) == 0) {
367 udev->autosuspend_disabled = 0; 369 udev->autosuspend_disabled = 0;
368 udev->autoresume_disabled = 0;
369 rc = usb_external_resume_device(udev, PMSG_USER_RESUME); 370 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
370 371
371 } else if (len == sizeof suspend_string - 1 &&
372 strncmp(buf, suspend_string, len) == 0) {
373 udev->autosuspend_disabled = 0;
374 udev->autoresume_disabled = 1;
375 rc = usb_external_suspend_device(udev, PMSG_USER_SUSPEND);
376
377 } else 372 } else
378 rc = -EINVAL; 373 rc = -EINVAL;
379 374
380 if (rc) { 375 if (rc)
381 udev->autosuspend_disabled = old_autosuspend_disabled; 376 udev->autosuspend_disabled = old_autosuspend_disabled;
382 udev->autoresume_disabled = old_autoresume_disabled;
383 }
384 usb_unlock_device(udev); 377 usb_unlock_device(udev);
385 return (rc < 0 ? rc : count); 378 return (rc < 0 ? rc : count);
386} 379}
@@ -508,6 +501,28 @@ static ssize_t usb_dev_authorized_store(struct device *dev,
508static DEVICE_ATTR(authorized, 0644, 501static DEVICE_ATTR(authorized, 0644,
509 usb_dev_authorized_show, usb_dev_authorized_store); 502 usb_dev_authorized_show, usb_dev_authorized_store);
510 503
504/* "Safely remove a device" */
505static ssize_t usb_remove_store(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
508{
509 struct usb_device *udev = to_usb_device(dev);
510 int rc = 0;
511
512 usb_lock_device(udev);
513 if (udev->state != USB_STATE_NOTATTACHED) {
514
515 /* To avoid races, first unconfigure and then remove */
516 usb_set_configuration(udev, -1);
517 rc = usb_remove_device(udev);
518 }
519 if (rc == 0)
520 rc = count;
521 usb_unlock_device(udev);
522 return rc;
523}
524static DEVICE_ATTR(remove, 0200, NULL, usb_remove_store);
525
511 526
512static struct attribute *dev_attrs[] = { 527static struct attribute *dev_attrs[] = {
513 /* current configuration's attributes */ 528 /* current configuration's attributes */
@@ -516,8 +531,8 @@ static struct attribute *dev_attrs[] = {
516 &dev_attr_bConfigurationValue.attr, 531 &dev_attr_bConfigurationValue.attr,
517 &dev_attr_bmAttributes.attr, 532 &dev_attr_bmAttributes.attr,
518 &dev_attr_bMaxPower.attr, 533 &dev_attr_bMaxPower.attr,
519 &dev_attr_urbnum.attr,
520 /* device attributes */ 534 /* device attributes */
535 &dev_attr_urbnum.attr,
521 &dev_attr_idVendor.attr, 536 &dev_attr_idVendor.attr,
522 &dev_attr_idProduct.attr, 537 &dev_attr_idProduct.attr,
523 &dev_attr_bcdDevice.attr, 538 &dev_attr_bcdDevice.attr,
@@ -529,10 +544,12 @@ static struct attribute *dev_attrs[] = {
529 &dev_attr_speed.attr, 544 &dev_attr_speed.attr,
530 &dev_attr_busnum.attr, 545 &dev_attr_busnum.attr,
531 &dev_attr_devnum.attr, 546 &dev_attr_devnum.attr,
547 &dev_attr_devpath.attr,
532 &dev_attr_version.attr, 548 &dev_attr_version.attr,
533 &dev_attr_maxchild.attr, 549 &dev_attr_maxchild.attr,
534 &dev_attr_quirks.attr, 550 &dev_attr_quirks.attr,
535 &dev_attr_authorized.attr, 551 &dev_attr_authorized.attr,
552 &dev_attr_remove.attr,
536 NULL, 553 NULL,
537}; 554};
538static struct attribute_group dev_attr_grp = { 555static struct attribute_group dev_attr_grp = {
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 0885d4abdc62..e7cae1334693 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -429,8 +429,16 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
429 case USB_ENDPOINT_XFER_ISOC: 429 case USB_ENDPOINT_XFER_ISOC:
430 case USB_ENDPOINT_XFER_INT: 430 case USB_ENDPOINT_XFER_INT:
431 /* too small? */ 431 /* too small? */
432 if (urb->interval <= 0) 432 switch (dev->speed) {
433 return -EINVAL; 433 case USB_SPEED_VARIABLE:
434 if (urb->interval < 6)
435 return -EINVAL;
436 break;
437 default:
438 if (urb->interval <= 0)
439 return -EINVAL;
440 break;
441 }
434 /* too big? */ 442 /* too big? */
435 switch (dev->speed) { 443 switch (dev->speed) {
436 case USB_SPEED_SUPER: /* units are 125us */ 444 case USB_SPEED_SUPER: /* units are 125us */
@@ -438,6 +446,10 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
438 if (urb->interval > (1 << 15)) 446 if (urb->interval > (1 << 15))
439 return -EINVAL; 447 return -EINVAL;
440 max = 1 << 15; 448 max = 1 << 15;
449 case USB_SPEED_VARIABLE:
450 if (urb->interval > 16)
451 return -EINVAL;
452 break;
441 case USB_SPEED_HIGH: /* units are microframes */ 453 case USB_SPEED_HIGH: /* units are microframes */
442 /* NOTE usb handles 2^15 */ 454 /* NOTE usb handles 2^15 */
443 if (urb->interval > (1024 * 8)) 455 if (urb->interval > (1024 * 8))
@@ -461,8 +473,10 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
461 default: 473 default:
462 return -EINVAL; 474 return -EINVAL;
463 } 475 }
464 /* Round down to a power of 2, no more than max */ 476 if (dev->speed != USB_SPEED_VARIABLE) {
465 urb->interval = min(max, 1 << ilog2(urb->interval)); 477 /* Round down to a power of 2, no more than max */
478 urb->interval = min(max, 1 << ilog2(urb->interval));
479 }
466 } 480 }
467 481
468 return usb_hcd_submit_urb(urb, mem_flags); 482 return usb_hcd_submit_urb(urb, mem_flags);
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index b1b85abb9a2d..4e2c6df8d3cc 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -64,6 +64,43 @@ MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
64 64
65 65
66/** 66/**
67 * usb_find_alt_setting() - Given a configuration, find the alternate setting
68 * for the given interface.
69 * @config - the configuration to search (not necessarily the current config).
70 * @iface_num - interface number to search in
71 * @alt_num - alternate interface setting number to search for.
72 *
73 * Search the configuration's interface cache for the given alt setting.
74 */
75struct usb_host_interface *usb_find_alt_setting(
76 struct usb_host_config *config,
77 unsigned int iface_num,
78 unsigned int alt_num)
79{
80 struct usb_interface_cache *intf_cache = NULL;
81 int i;
82
83 for (i = 0; i < config->desc.bNumInterfaces; i++) {
84 if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber
85 == iface_num) {
86 intf_cache = config->intf_cache[i];
87 break;
88 }
89 }
90 if (!intf_cache)
91 return NULL;
92 for (i = 0; i < intf_cache->num_altsetting; i++)
93 if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)
94 return &intf_cache->altsetting[i];
95
96 printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "
97 "config %u\n", alt_num, iface_num,
98 config->desc.bConfigurationValue);
99 return NULL;
100}
101EXPORT_SYMBOL_GPL(usb_find_alt_setting);
102
103/**
67 * usb_ifnum_to_if - get the interface object with a given interface number 104 * usb_ifnum_to_if - get the interface object with a given interface number
68 * @dev: the device whose current configuration is considered 105 * @dev: the device whose current configuration is considered
69 * @ifnum: the desired interface 106 * @ifnum: the desired interface
@@ -130,24 +167,17 @@ struct usb_host_interface *usb_altnum_to_altsetting(
130} 167}
131EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting); 168EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
132 169
133struct find_interface_arg {
134 int minor;
135 struct usb_interface *interface;
136};
137
138static int __find_interface(struct device *dev, void *data) 170static int __find_interface(struct device *dev, void *data)
139{ 171{
140 struct find_interface_arg *arg = data; 172 int *minor = data;
141 struct usb_interface *intf; 173 struct usb_interface *intf;
142 174
143 if (!is_usb_interface(dev)) 175 if (!is_usb_interface(dev))
144 return 0; 176 return 0;
145 177
146 intf = to_usb_interface(dev); 178 intf = to_usb_interface(dev);
147 if (intf->minor != -1 && intf->minor == arg->minor) { 179 if (intf->minor != -1 && intf->minor == *minor)
148 arg->interface = intf;
149 return 1; 180 return 1;
150 }
151 return 0; 181 return 0;
152} 182}
153 183
@@ -156,21 +186,20 @@ static int __find_interface(struct device *dev, void *data)
156 * @drv: the driver whose current configuration is considered 186 * @drv: the driver whose current configuration is considered
157 * @minor: the minor number of the desired device 187 * @minor: the minor number of the desired device
158 * 188 *
159 * This walks the driver device list and returns a pointer to the interface 189 * This walks the bus device list and returns a pointer to the interface
160 * with the matching minor. Note, this only works for devices that share the 190 * with the matching minor. Note, this only works for devices that share the
161 * USB major number. 191 * USB major number.
162 */ 192 */
163struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) 193struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
164{ 194{
165 struct find_interface_arg argb; 195 struct device *dev;
166 int retval; 196
197 dev = bus_find_device(&usb_bus_type, NULL, &minor, __find_interface);
198
199 /* Drop reference count from bus_find_device */
200 put_device(dev);
167 201
168 argb.minor = minor; 202 return dev ? to_usb_interface(dev) : NULL;
169 argb.interface = NULL;
170 /* eat the error, it will be in argb.interface */
171 retval = driver_for_each_device(&drv->drvwrap.driver, NULL, &argb,
172 __find_interface);
173 return argb.interface;
174} 203}
175EXPORT_SYMBOL_GPL(usb_find_interface); 204EXPORT_SYMBOL_GPL(usb_find_interface);
176 205
@@ -1038,7 +1067,7 @@ static struct notifier_block usb_bus_nb = {
1038struct dentry *usb_debug_root; 1067struct dentry *usb_debug_root;
1039EXPORT_SYMBOL_GPL(usb_debug_root); 1068EXPORT_SYMBOL_GPL(usb_debug_root);
1040 1069
1041struct dentry *usb_debug_devices; 1070static struct dentry *usb_debug_devices;
1042 1071
1043static int usb_debugfs_init(void) 1072static int usb_debugfs_init(void)
1044{ 1073{
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 9a8b15e6377a..4c36c7f512a0 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -24,6 +24,7 @@ extern void usb_disable_device(struct usb_device *dev, int skip_ep0);
24extern int usb_deauthorize_device(struct usb_device *); 24extern int usb_deauthorize_device(struct usb_device *);
25extern int usb_authorize_device(struct usb_device *); 25extern int usb_authorize_device(struct usb_device *);
26extern void usb_detect_quirks(struct usb_device *udev); 26extern void usb_detect_quirks(struct usb_device *udev);
27extern int usb_remove_device(struct usb_device *udev);
27 28
28extern int usb_get_device_descriptor(struct usb_device *dev, 29extern int usb_get_device_descriptor(struct usb_device *dev,
29 unsigned int size); 30 unsigned int size);
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index a18e3c5dd82e..ee411206c699 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -732,6 +732,24 @@ config USB_FILE_STORAGE_TEST
732 behavior of USB Mass Storage hosts. Not needed for 732 behavior of USB Mass Storage hosts. Not needed for
733 normal operation. 733 normal operation.
734 734
735config USB_MASS_STORAGE
736 tristate "Mass Storage Gadget"
737 depends on BLOCK
738 help
739 The Mass Storage Gadget acts as a USB Mass Storage disk drive.
740 As its storage repository it can use a regular file or a block
741 device (in much the same way as the "loop" device driver),
742 specified as a module parameter or sysfs option.
743
744 This is heavily based on File-backed Storage Gadget and in most
745 cases you will want to use FSG instead. This gadget is mostly
746 here to test the functionality of the Mass Storage Function
747 which may be used with composite framework.
748
749 Say "y" to link the driver statically, or "m" to build
750 a dynamically linked module called "g_file_storage". If unsure,
751 consider File-backed Storage Gadget.
752
735config USB_G_SERIAL 753config USB_G_SERIAL
736 tristate "Serial Gadget (with CDC ACM and CDC OBEX support)" 754 tristate "Serial Gadget (with CDC ACM and CDC OBEX support)"
737 help 755 help
@@ -794,6 +812,48 @@ config USB_CDC_COMPOSITE
794 Say "y" to link the driver statically, or "m" to build a 812 Say "y" to link the driver statically, or "m" to build a
795 dynamically linked module. 813 dynamically linked module.
796 814
815config USB_G_MULTI
816 tristate "Multifunction Composite Gadget (EXPERIMENTAL)"
817 depends on BLOCK && NET
818 help
819 The Multifunction Composite Gadget provides Ethernet (RNDIS
820 and/or CDC Ethernet), mass storage and ACM serial link
821 interfaces.
822
823 You will be asked to choose which of the two configurations is
824 to be available in the gadget. At least one configuration must
825 be chosen to make the gadget usable. Selecting more than one
826 configuration will prevent Windows from automatically detecting
827 the gadget as a composite gadget, so an INF file will be needed to
828 use the gadget.
829
830 Say "y" to link the driver statically, or "m" to build a
831 dynamically linked module called "g_multi".
832
833config USB_G_MULTI_RNDIS
834 bool "RNDIS + CDC Serial + Storage configuration"
835 depends on USB_G_MULTI
836 default y
837 help
838 This option enables a configuration with RNDIS, CDC Serial and
839 Mass Storage functions available in the Multifunction Composite
840 Gadget. This is the configuration dedicated for Windows since RNDIS
841 is Microsoft's protocol.
842
843 If unsure, say "y".
844
845config USB_G_MULTI_CDC
846 bool "CDC Ethernet + CDC Serial + Storage configuration"
847 depends on USB_G_MULTI
848 default n
849 help
850 This option enables a configuration with CDC Ethernet (ECM), CDC
851 Serial and Mass Storage functions available in the Multifunction
852 Composite Gadget.
853
854 If unsure, say "y".
855
856
797# put drivers that need isochronous transfer support (for audio 857# put drivers that need isochronous transfer support (for audio
798# or video class gadget drivers), or specific hardware, here. 858# or video class gadget drivers), or specific hardware, here.
799 859
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 9d7b87c52e9f..2e2c047262b7 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -39,16 +39,20 @@ g_serial-objs := serial.o
39g_midi-objs := gmidi.o 39g_midi-objs := gmidi.o
40gadgetfs-objs := inode.o 40gadgetfs-objs := inode.o
41g_file_storage-objs := file_storage.o 41g_file_storage-objs := file_storage.o
42g_mass_storage-objs := mass_storage.o
42g_printer-objs := printer.o 43g_printer-objs := printer.o
43g_cdc-objs := cdc2.o 44g_cdc-objs := cdc2.o
45g_multi-objs := multi.o
44 46
45obj-$(CONFIG_USB_ZERO) += g_zero.o 47obj-$(CONFIG_USB_ZERO) += g_zero.o
46obj-$(CONFIG_USB_AUDIO) += g_audio.o 48obj-$(CONFIG_USB_AUDIO) += g_audio.o
47obj-$(CONFIG_USB_ETH) += g_ether.o 49obj-$(CONFIG_USB_ETH) += g_ether.o
48obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 50obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
49obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 51obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
52obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o
50obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 53obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
51obj-$(CONFIG_USB_G_PRINTER) += g_printer.o 54obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
52obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o 55obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
53obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o 56obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
57obj-$(CONFIG_USB_G_MULTI) += g_multi.o
54 58
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 66450a1abc22..043e04db2a05 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -892,7 +892,7 @@ static void pullup(struct at91_udc *udc, int is_on)
892 892
893 txvc |= AT91_UDP_TXVC_PUON; 893 txvc |= AT91_UDP_TXVC_PUON;
894 at91_udp_write(udc, AT91_UDP_TXVC, txvc); 894 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
895 } else if (cpu_is_at91sam9261()) { 895 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
896 u32 usbpucr; 896 u32 usbpucr;
897 897
898 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR); 898 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
@@ -910,7 +910,7 @@ static void pullup(struct at91_udc *udc, int is_on)
910 910
911 txvc &= ~AT91_UDP_TXVC_PUON; 911 txvc &= ~AT91_UDP_TXVC_PUON;
912 at91_udp_write(udc, AT91_UDP_TXVC, txvc); 912 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
913 } else if (cpu_is_at91sam9261()) { 913 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
914 u32 usbpucr; 914 u32 usbpucr;
915 915
916 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR); 916 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
@@ -1692,7 +1692,7 @@ static int __init at91udc_probe(struct platform_device *pdev)
1692 udc->ep[3].maxpacket = 64; 1692 udc->ep[3].maxpacket = 64;
1693 udc->ep[4].maxpacket = 512; 1693 udc->ep[4].maxpacket = 512;
1694 udc->ep[5].maxpacket = 512; 1694 udc->ep[5].maxpacket = 512;
1695 } else if (cpu_is_at91sam9261()) { 1695 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
1696 udc->ep[3].maxpacket = 64; 1696 udc->ep[3].maxpacket = 64;
1697 } else if (cpu_is_at91sam9263()) { 1697 } else if (cpu_is_at91sam9263()) {
1698 udc->ep[0].maxpacket = 64; 1698 udc->ep[0].maxpacket = 64;
diff --git a/drivers/usb/gadget/audio.c b/drivers/usb/gadget/audio.c
index a3a0f4a27ef0..58f220323847 100644
--- a/drivers/usb/gadget/audio.c
+++ b/drivers/usb/gadget/audio.c
@@ -89,120 +89,6 @@ static const struct usb_descriptor_header *otg_desc[] = {
89 89
90/*-------------------------------------------------------------------------*/ 90/*-------------------------------------------------------------------------*/
91 91
92/**
93 * Handle USB audio endpoint set/get command in setup class request
94 */
95
96static int audio_set_endpoint_req(struct usb_configuration *c,
97 const struct usb_ctrlrequest *ctrl)
98{
99 struct usb_composite_dev *cdev = c->cdev;
100 int value = -EOPNOTSUPP;
101 u16 ep = le16_to_cpu(ctrl->wIndex);
102 u16 len = le16_to_cpu(ctrl->wLength);
103 u16 w_value = le16_to_cpu(ctrl->wValue);
104
105 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
106 ctrl->bRequest, w_value, len, ep);
107
108 switch (ctrl->bRequest) {
109 case UAC_SET_CUR:
110 value = 0;
111 break;
112
113 case UAC_SET_MIN:
114 break;
115
116 case UAC_SET_MAX:
117 break;
118
119 case UAC_SET_RES:
120 break;
121
122 case UAC_SET_MEM:
123 break;
124
125 default:
126 break;
127 }
128
129 return value;
130}
131
132static int audio_get_endpoint_req(struct usb_configuration *c,
133 const struct usb_ctrlrequest *ctrl)
134{
135 struct usb_composite_dev *cdev = c->cdev;
136 int value = -EOPNOTSUPP;
137 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
138 u16 len = le16_to_cpu(ctrl->wLength);
139 u16 w_value = le16_to_cpu(ctrl->wValue);
140
141 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
142 ctrl->bRequest, w_value, len, ep);
143
144 switch (ctrl->bRequest) {
145 case UAC_GET_CUR:
146 case UAC_GET_MIN:
147 case UAC_GET_MAX:
148 case UAC_GET_RES:
149 value = 3;
150 break;
151 case UAC_GET_MEM:
152 break;
153 default:
154 break;
155 }
156
157 return value;
158}
159
160static int
161audio_setup(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl)
162{
163 struct usb_composite_dev *cdev = c->cdev;
164 struct usb_request *req = cdev->req;
165 int value = -EOPNOTSUPP;
166 u16 w_index = le16_to_cpu(ctrl->wIndex);
167 u16 w_value = le16_to_cpu(ctrl->wValue);
168 u16 w_length = le16_to_cpu(ctrl->wLength);
169
170 /* composite driver infrastructure handles everything except
171 * Audio class messages; interface activation uses set_alt().
172 */
173 switch (ctrl->bRequestType) {
174 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
175 value = audio_set_endpoint_req(c, ctrl);
176 break;
177
178 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
179 value = audio_get_endpoint_req(c, ctrl);
180 break;
181
182 default:
183 ERROR(cdev, "Invalid control req%02x.%02x v%04x i%04x l%d\n",
184 ctrl->bRequestType, ctrl->bRequest,
185 w_value, w_index, w_length);
186 }
187
188 /* respond with data transfer or status phase? */
189 if (value >= 0) {
190 DBG(cdev, "Audio req%02x.%02x v%04x i%04x l%d\n",
191 ctrl->bRequestType, ctrl->bRequest,
192 w_value, w_index, w_length);
193 req->zero = 0;
194 req->length = value;
195 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
196 if (value < 0)
197 ERROR(cdev, "Audio response on err %d\n", value);
198 }
199
200 /* device either stalls (value < 0) or reports success */
201 return value;
202}
203
204/*-------------------------------------------------------------------------*/
205
206static int __init audio_do_config(struct usb_configuration *c) 92static int __init audio_do_config(struct usb_configuration *c)
207{ 93{
208 /* FIXME alloc iConfiguration string, set it in c->strings */ 94 /* FIXME alloc iConfiguration string, set it in c->strings */
@@ -220,7 +106,6 @@ static int __init audio_do_config(struct usb_configuration *c)
220static struct usb_configuration audio_config_driver = { 106static struct usb_configuration audio_config_driver = {
221 .label = DRIVER_DESC, 107 .label = DRIVER_DESC,
222 .bind = audio_do_config, 108 .bind = audio_do_config,
223 .setup = audio_setup,
224 .bConfigurationValue = 1, 109 .bConfigurationValue = 1,
225 /* .iConfiguration = DYNAMIC */ 110 /* .iConfiguration = DYNAMIC */
226 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 111 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index d05397ec8a18..09289bb1e20f 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -373,6 +373,8 @@ static void reset_config(struct usb_composite_dev *cdev)
373 list_for_each_entry(f, &cdev->config->functions, list) { 373 list_for_each_entry(f, &cdev->config->functions, list) {
374 if (f->disable) 374 if (f->disable)
375 f->disable(f); 375 f->disable(f);
376
377 bitmap_zero(f->endpoints, 32);
376 } 378 }
377 cdev->config = NULL; 379 cdev->config = NULL;
378} 380}
@@ -418,10 +420,35 @@ static int set_config(struct usb_composite_dev *cdev,
418 /* Initialize all interfaces by setting them to altsetting zero. */ 420 /* Initialize all interfaces by setting them to altsetting zero. */
419 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { 421 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
420 struct usb_function *f = c->interface[tmp]; 422 struct usb_function *f = c->interface[tmp];
423 struct usb_descriptor_header **descriptors;
421 424
422 if (!f) 425 if (!f)
423 break; 426 break;
424 427
428 /*
429 * Record which endpoints are used by the function. This is used
430 * to dispatch control requests targeted at that endpoint to the
431 * function's setup callback instead of the current
432 * configuration's setup callback.
433 */
434 if (gadget->speed == USB_SPEED_HIGH)
435 descriptors = f->hs_descriptors;
436 else
437 descriptors = f->descriptors;
438
439 for (; *descriptors; ++descriptors) {
440 struct usb_endpoint_descriptor *ep;
441 int addr;
442
443 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
444 continue;
445
446 ep = (struct usb_endpoint_descriptor *)*descriptors;
447 addr = ((ep->bEndpointAddress & 0x80) >> 3)
448 | (ep->bEndpointAddress & 0x0f);
449 set_bit(addr, f->endpoints);
450 }
451
425 result = f->set_alt(f, tmp, 0); 452 result = f->set_alt(f, tmp, 0);
426 if (result < 0) { 453 if (result < 0) {
427 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", 454 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
@@ -688,6 +715,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
688 u16 w_value = le16_to_cpu(ctrl->wValue); 715 u16 w_value = le16_to_cpu(ctrl->wValue);
689 u16 w_length = le16_to_cpu(ctrl->wLength); 716 u16 w_length = le16_to_cpu(ctrl->wLength);
690 struct usb_function *f = NULL; 717 struct usb_function *f = NULL;
718 u8 endp;
691 719
692 /* partial re-init of the response message; the function or the 720 /* partial re-init of the response message; the function or the
693 * gadget might need to intercept e.g. a control-OUT completion 721 * gadget might need to intercept e.g. a control-OUT completion
@@ -800,23 +828,33 @@ unknown:
800 ctrl->bRequestType, ctrl->bRequest, 828 ctrl->bRequestType, ctrl->bRequest,
801 w_value, w_index, w_length); 829 w_value, w_index, w_length);
802 830
803 /* functions always handle their interfaces ... punt other 831 /* functions always handle their interfaces and endpoints...
804 * recipients (endpoint, other, WUSB, ...) to the current 832 * punt other recipients (other, WUSB, ...) to the current
805 * configuration code. 833 * configuration code.
806 * 834 *
807 * REVISIT it could make sense to let the composite device 835 * REVISIT it could make sense to let the composite device
808 * take such requests too, if that's ever needed: to work 836 * take such requests too, if that's ever needed: to work
809 * in config 0, etc. 837 * in config 0, etc.
810 */ 838 */
811 if ((ctrl->bRequestType & USB_RECIP_MASK) 839 switch (ctrl->bRequestType & USB_RECIP_MASK) {
812 == USB_RECIP_INTERFACE) { 840 case USB_RECIP_INTERFACE:
813 f = cdev->config->interface[intf]; 841 f = cdev->config->interface[intf];
814 if (f && f->setup) 842 break;
815 value = f->setup(f, ctrl); 843
816 else 844 case USB_RECIP_ENDPOINT:
845 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
846 list_for_each_entry(f, &cdev->config->functions, list) {
847 if (test_bit(endp, f->endpoints))
848 break;
849 }
850 if (&f->list == &cdev->config->functions)
817 f = NULL; 851 f = NULL;
852 break;
818 } 853 }
819 if (value < 0 && !f) { 854
855 if (f && f->setup)
856 value = f->setup(f, ctrl);
857 else {
820 struct usb_configuration *c; 858 struct usb_configuration *c;
821 859
822 c = cdev->config; 860 c = cdev->config;
@@ -1054,7 +1092,8 @@ static struct usb_gadget_driver composite_driver = {
1054 .speed = USB_SPEED_HIGH, 1092 .speed = USB_SPEED_HIGH,
1055 1093
1056 .bind = composite_bind, 1094 .bind = composite_bind,
1057 .unbind = __exit_p(composite_unbind), 1095 /* .unbind = __exit_p(composite_unbind), */
1096 .unbind = composite_unbind,
1058 1097
1059 .setup = composite_setup, 1098 .setup = composite_setup,
1060 .disconnect = composite_disconnect, 1099 .disconnect = composite_disconnect,
@@ -1103,7 +1142,7 @@ int __init usb_composite_register(struct usb_composite_driver *driver)
1103 * This function is used to unregister drivers using the composite 1142 * This function is used to unregister drivers using the composite
1104 * driver framework. 1143 * driver framework.
1105 */ 1144 */
1106void __exit usb_composite_unregister(struct usb_composite_driver *driver) 1145void /* __exit */ usb_composite_unregister(struct usb_composite_driver *driver)
1107{ 1146{
1108 if (composite != driver) 1147 if (composite != driver)
1109 return; 1148 return;
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 167cb2a8ecef..141372b6e7a1 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -25,6 +25,14 @@
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26#include <linux/utsname.h> 26#include <linux/utsname.h>
27 27
28
29#if defined USB_ETH_RNDIS
30# undef USB_ETH_RNDIS
31#endif
32#ifdef CONFIG_USB_ETH_RNDIS
33# define USB_ETH_RNDIS y
34#endif
35
28#include "u_ether.h" 36#include "u_ether.h"
29 37
30 38
@@ -66,7 +74,7 @@
66#define DRIVER_DESC "Ethernet Gadget" 74#define DRIVER_DESC "Ethernet Gadget"
67#define DRIVER_VERSION "Memorial Day 2008" 75#define DRIVER_VERSION "Memorial Day 2008"
68 76
69#ifdef CONFIG_USB_ETH_RNDIS 77#ifdef USB_ETH_RNDIS
70#define PREFIX "RNDIS/" 78#define PREFIX "RNDIS/"
71#else 79#else
72#define PREFIX "" 80#define PREFIX ""
@@ -87,7 +95,7 @@
87 95
88static inline bool has_rndis(void) 96static inline bool has_rndis(void)
89{ 97{
90#ifdef CONFIG_USB_ETH_RNDIS 98#ifdef USB_ETH_RNDIS
91 return true; 99 return true;
92#else 100#else
93 return false; 101 return false;
@@ -110,7 +118,7 @@ static inline bool has_rndis(void)
110 118
111#include "f_ecm.c" 119#include "f_ecm.c"
112#include "f_subset.c" 120#include "f_subset.c"
113#ifdef CONFIG_USB_ETH_RNDIS 121#ifdef USB_ETH_RNDIS
114#include "f_rndis.c" 122#include "f_rndis.c"
115#include "rndis.c" 123#include "rndis.c"
116#endif 124#endif
@@ -251,7 +259,7 @@ static struct usb_configuration rndis_config_driver = {
251 259
252/*-------------------------------------------------------------------------*/ 260/*-------------------------------------------------------------------------*/
253 261
254#ifdef CONFIG_USB_ETH_EEM 262#ifdef USB_ETH_EEM
255static int use_eem = 1; 263static int use_eem = 1;
256#else 264#else
257static int use_eem; 265static int use_eem;
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index 4e3657808b0f..d10353d46b86 100644
--- a/drivers/usb/gadget/f_acm.c
+++ b/drivers/usb/gadget/f_acm.c
@@ -4,6 +4,8 @@
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) 4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell 5 * Copyright (C) 2008 by David Brownell
6 * Copyright (C) 2008 by Nokia Corporation 6 * Copyright (C) 2008 by Nokia Corporation
7 * Copyright (C) 2009 by Samsung Electronics
8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
7 * 9 *
8 * This software is distributed under the terms of the GNU General 10 * This software is distributed under the terms of the GNU General
9 * Public License ("GPL") as published by the Free Software Foundation, 11 * Public License ("GPL") as published by the Free Software Foundation,
@@ -99,6 +101,20 @@ static inline struct f_acm *port_to_acm(struct gserial *p)
99 101
100/* interface and class descriptors: */ 102/* interface and class descriptors: */
101 103
104static struct usb_interface_assoc_descriptor
105acm_iad_descriptor = {
106 .bLength = sizeof acm_iad_descriptor,
107 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
108
109 /* .bFirstInterface = DYNAMIC, */
110 .bInterfaceCount = 2, // control + data
111 .bFunctionClass = USB_CLASS_COMM,
112 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
113 .bFunctionProtocol = USB_CDC_PROTO_NONE,
114 /* .iFunction = DYNAMIC */
115};
116
117
102static struct usb_interface_descriptor acm_control_interface_desc __initdata = { 118static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
103 .bLength = USB_DT_INTERFACE_SIZE, 119 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE, 120 .bDescriptorType = USB_DT_INTERFACE,
@@ -178,6 +194,7 @@ static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
178}; 194};
179 195
180static struct usb_descriptor_header *acm_fs_function[] __initdata = { 196static struct usb_descriptor_header *acm_fs_function[] __initdata = {
197 (struct usb_descriptor_header *) &acm_iad_descriptor,
181 (struct usb_descriptor_header *) &acm_control_interface_desc, 198 (struct usb_descriptor_header *) &acm_control_interface_desc,
182 (struct usb_descriptor_header *) &acm_header_desc, 199 (struct usb_descriptor_header *) &acm_header_desc,
183 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 200 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
@@ -216,6 +233,7 @@ static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
216}; 233};
217 234
218static struct usb_descriptor_header *acm_hs_function[] __initdata = { 235static struct usb_descriptor_header *acm_hs_function[] __initdata = {
236 (struct usb_descriptor_header *) &acm_iad_descriptor,
219 (struct usb_descriptor_header *) &acm_control_interface_desc, 237 (struct usb_descriptor_header *) &acm_control_interface_desc,
220 (struct usb_descriptor_header *) &acm_header_desc, 238 (struct usb_descriptor_header *) &acm_header_desc,
221 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 239 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
@@ -232,11 +250,13 @@ static struct usb_descriptor_header *acm_hs_function[] __initdata = {
232 250
233#define ACM_CTRL_IDX 0 251#define ACM_CTRL_IDX 0
234#define ACM_DATA_IDX 1 252#define ACM_DATA_IDX 1
253#define ACM_IAD_IDX 2
235 254
236/* static strings, in UTF-8 */ 255/* static strings, in UTF-8 */
237static struct usb_string acm_string_defs[] = { 256static struct usb_string acm_string_defs[] = {
238 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)", 257 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
239 [ACM_DATA_IDX].s = "CDC ACM Data", 258 [ACM_DATA_IDX].s = "CDC ACM Data",
259 [ACM_IAD_IDX ].s = "CDC Serial",
240 { /* ZEROES END LIST */ }, 260 { /* ZEROES END LIST */ },
241}; 261};
242 262
@@ -563,6 +583,7 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
563 if (status < 0) 583 if (status < 0)
564 goto fail; 584 goto fail;
565 acm->ctrl_id = status; 585 acm->ctrl_id = status;
586 acm_iad_descriptor.bFirstInterface = status;
566 587
567 acm_control_interface_desc.bInterfaceNumber = status; 588 acm_control_interface_desc.bInterfaceNumber = status;
568 acm_union_desc .bMasterInterface0 = status; 589 acm_union_desc .bMasterInterface0 = status;
@@ -732,6 +753,13 @@ int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
732 acm_string_defs[ACM_DATA_IDX].id = status; 753 acm_string_defs[ACM_DATA_IDX].id = status;
733 754
734 acm_data_interface_desc.iInterface = status; 755 acm_data_interface_desc.iInterface = status;
756
757 status = usb_string_id(c->cdev);
758 if (status < 0)
759 return status;
760 acm_string_defs[ACM_IAD_IDX].id = status;
761
762 acm_iad_descriptor.iFunction = status;
735 } 763 }
736 764
737 /* allocate and initialize one new instance */ 765 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_audio.c b/drivers/usb/gadget/f_audio.c
index 98e9bb977291..c43c89ffa2c8 100644
--- a/drivers/usb/gadget/f_audio.c
+++ b/drivers/usb/gadget/f_audio.c
@@ -445,6 +445,70 @@ static int audio_get_intf_req(struct usb_function *f,
445 return len; 445 return len;
446} 446}
447 447
448static int audio_set_endpoint_req(struct usb_function *f,
449 const struct usb_ctrlrequest *ctrl)
450{
451 struct usb_composite_dev *cdev = f->config->cdev;
452 int value = -EOPNOTSUPP;
453 u16 ep = le16_to_cpu(ctrl->wIndex);
454 u16 len = le16_to_cpu(ctrl->wLength);
455 u16 w_value = le16_to_cpu(ctrl->wValue);
456
457 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
458 ctrl->bRequest, w_value, len, ep);
459
460 switch (ctrl->bRequest) {
461 case UAC_SET_CUR:
462 value = 0;
463 break;
464
465 case UAC_SET_MIN:
466 break;
467
468 case UAC_SET_MAX:
469 break;
470
471 case UAC_SET_RES:
472 break;
473
474 case UAC_SET_MEM:
475 break;
476
477 default:
478 break;
479 }
480
481 return value;
482}
483
484static int audio_get_endpoint_req(struct usb_function *f,
485 const struct usb_ctrlrequest *ctrl)
486{
487 struct usb_composite_dev *cdev = f->config->cdev;
488 int value = -EOPNOTSUPP;
489 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
490 u16 len = le16_to_cpu(ctrl->wLength);
491 u16 w_value = le16_to_cpu(ctrl->wValue);
492
493 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
494 ctrl->bRequest, w_value, len, ep);
495
496 switch (ctrl->bRequest) {
497 case UAC_GET_CUR:
498 case UAC_GET_MIN:
499 case UAC_GET_MAX:
500 case UAC_GET_RES:
501 value = 3;
502 break;
503 case UAC_GET_MEM:
504 break;
505 default:
506 break;
507 }
508
509 return value;
510}
511
448static int 512static int
449f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 513f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
450{ 514{
@@ -455,8 +519,8 @@ f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
455 u16 w_value = le16_to_cpu(ctrl->wValue); 519 u16 w_value = le16_to_cpu(ctrl->wValue);
456 u16 w_length = le16_to_cpu(ctrl->wLength); 520 u16 w_length = le16_to_cpu(ctrl->wLength);
457 521
458 /* composite driver infrastructure handles everything except 522 /* composite driver infrastructure handles everything; interface
459 * Audio class messages; interface activation uses set_alt(). 523 * activation uses set_alt().
460 */ 524 */
461 switch (ctrl->bRequestType) { 525 switch (ctrl->bRequestType) {
462 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 526 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
@@ -467,6 +531,14 @@ f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
467 value = audio_get_intf_req(f, ctrl); 531 value = audio_get_intf_req(f, ctrl);
468 break; 532 break;
469 533
534 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
535 value = audio_set_endpoint_req(f, ctrl);
536 break;
537
538 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
539 value = audio_get_endpoint_req(f, ctrl);
540 break;
541
470 default: 542 default:
471 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 543 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
472 ctrl->bRequestType, ctrl->bRequest, 544 ctrl->bRequestType, ctrl->bRequest,
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
new file mode 100644
index 000000000000..a37640eba434
--- /dev/null
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -0,0 +1,3091 @@
1/*
2 * f_mass_storage.c -- Mass Storage USB Composite Function
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") as published by the Free Software
24 * Foundation, either version 2 of that License or (at your option) any
25 * later version.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40
41/*
42 * The Mass Storage Function acts as a USB Mass Storage device,
43 * appearing to the host as a disk drive or as a CD-ROM drive. In
44 * addition to providing an example of a genuinely useful composite
45 * function for a USB device, it also illustrates a technique of
46 * double-buffering for increased throughput.
47 *
48 * Function supports multiple logical units (LUNs). Backing storage
49 * for each LUN is provided by a regular file or a block device.
50 * Access for each LUN can be limited to read-only. Moreover, the
51 * function can indicate that LUN is removable and/or CD-ROM. (The
52 * later implies read-only access.)
53 *
54 * MSF is configured by specifying a fsg_config structure. It has the
55 * following fields:
56 *
57 * nluns Number of LUNs function have (anywhere from 1
58 * to FSG_MAX_LUNS which is 8).
59 * luns An array of LUN configuration values. This
60 * should be filled for each LUN that
61 * function will include (ie. for "nluns"
62 * LUNs). Each element of the array has
63 * the following fields:
64 * ->filename The path to the backing file for the LUN.
65 * Required if LUN is not marked as
66 * removable.
67 * ->ro Flag specifying access to the LUN shall be
68 * read-only. This is implied if CD-ROM
69 * emulation is enabled as well as when
70 * it was impossible to open "filename"
71 * in R/W mode.
72 * ->removable Flag specifying that LUN shall be indicated as
73 * being removable.
74 * ->cdrom Flag specifying that LUN shall be reported as
75 * being a CD-ROM.
76 *
77 * lun_name_format A printf-like format for names of the LUN
78 * devices. This determines how the
79 * directory in sysfs will be named.
80 * Unless you are using several MSFs in
81 * a single gadget (as opposed to single
82 * MSF in many configurations) you may
83 * leave it as NULL (in which case
84 * "lun%d" will be used). In the format
85 * you can use "%d" to index LUNs for
86 * MSF's with more than one LUN. (Beware
87 * that there is only one integer given
88 * as an argument for the format and
89 * specifying invalid format may cause
90 * unspecified behaviour.)
91 * thread_name Name of the kernel thread process used by the
92 * MSF. You can safely set it to NULL
93 * (in which case default "file-storage"
94 * will be used).
95 *
96 * vendor_name
97 * product_name
98 * release Information used as a reply to INQUIRY
99 * request. To use default set to NULL,
100 * NULL, 0xffff respectively. The first
101 * field should be 8 and the second 16
102 * characters or less.
103 *
104 * can_stall Set to permit function to halt bulk endpoints.
105 * Disabled on some USB devices known not
106 * to work correctly. You should set it
107 * to true.
108 *
109 * If "removable" is not set for a LUN then a backing file must be
110 * specified. If it is set, then NULL filename means the LUN's medium
111 * is not loaded (an empty string as "filename" in the fsg_config
112 * structure causes error). The CD-ROM emulation includes a single
113 * data track and no audio tracks; hence there need be only one
114 * backing file per LUN. Note also that the CD-ROM block length is
115 * set to 512 rather than the more common value 2048.
116 *
117 *
118 * MSF includes support for module parameters. If gadget using it
119 * decides to use it, the following module parameters will be
120 * available:
121 *
122 * file=filename[,filename...]
123 * Names of the files or block devices used for
124 * backing storage.
125 * ro=b[,b...] Default false, boolean for read-only access.
126 * removable=b[,b...]
127 * Default true, boolean for removable media.
128 * cdrom=b[,b...] Default false, boolean for whether to emulate
129 * a CD-ROM drive.
130 * luns=N Default N = number of filenames, number of
131 * LUNs to support.
132 * stall Default determined according to the type of
133 * USB device controller (usually true),
134 * boolean to permit the driver to halt
135 * bulk endpoints.
136 *
137 * The module parameters may be prefixed with some string. You need
138 * to consult gadget's documentation or source to verify whether it is
139 * using those module parameters and if it does what are the prefixes
140 * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
141 * the prefix).
142 *
143 *
144 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
145 * needed. The memory requirement amounts to two 16K buffers, size
146 * configurable by a parameter. Support is included for both
147 * full-speed and high-speed operation.
148 *
149 * Note that the driver is slightly non-portable in that it assumes a
150 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
151 * interrupt-in endpoints. With most device controllers this isn't an
152 * issue, but there may be some with hardware restrictions that prevent
153 * a buffer from being used by more than one endpoint.
154 *
155 *
156 * The pathnames of the backing files and the ro settings are
157 * available in the attribute files "file" and "ro" in the lun<n> (or
158 * to be more precise in a directory which name comes from
159 * "lun_name_format" option!) subdirectory of the gadget's sysfs
160 * directory. If the "removable" option is set, writing to these
161 * files will simulate ejecting/loading the medium (writing an empty
162 * line means eject) and adjusting a write-enable tab. Changes to the
163 * ro setting are not allowed when the medium is loaded or if CD-ROM
164 * emulation is being used.
165 *
166 *
167 * This function is heavily based on "File-backed Storage Gadget" by
168 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
169 * Brownell. The driver's SCSI command interface was based on the
170 * "Information technology - Small Computer System Interface - 2"
171 * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
172 * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
173 * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
174 * was based on the "Universal Serial Bus Mass Storage Class UFI
175 * Command Specification" document, Revision 1.0, December 14, 1998,
176 * available at
177 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
178 */
179
180
181/*
182 * Driver Design
183 *
184 * The MSF is fairly straightforward. There is a main kernel
185 * thread that handles most of the work. Interrupt routines field
186 * callbacks from the controller driver: bulk- and interrupt-request
187 * completion notifications, endpoint-0 events, and disconnect events.
188 * Completion events are passed to the main thread by wakeup calls. Many
189 * ep0 requests are handled at interrupt time, but SetInterface,
190 * SetConfiguration, and device reset requests are forwarded to the
191 * thread in the form of "exceptions" using SIGUSR1 signals (since they
192 * should interrupt any ongoing file I/O operations).
193 *
194 * The thread's main routine implements the standard command/data/status
195 * parts of a SCSI interaction. It and its subroutines are full of tests
196 * for pending signals/exceptions -- all this polling is necessary since
197 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
198 * indication that the driver really wants to be running in userspace.)
199 * An important point is that so long as the thread is alive it keeps an
200 * open reference to the backing file. This will prevent unmounting
201 * the backing file's underlying filesystem and could cause problems
202 * during system shutdown, for example. To prevent such problems, the
203 * thread catches INT, TERM, and KILL signals and converts them into
204 * an EXIT exception.
205 *
206 * In normal operation the main thread is started during the gadget's
207 * fsg_bind() callback and stopped during fsg_unbind(). But it can
208 * also exit when it receives a signal, and there's no point leaving
209 * the gadget running when the thread is dead. At of this moment, MSF
210 * provides no way to deregister the gadget when thread dies -- maybe
211 * a callback functions is needed.
212 *
213 * To provide maximum throughput, the driver uses a circular pipeline of
214 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
215 * arbitrarily long; in practice the benefits don't justify having more
216 * than 2 stages (i.e., double buffering). But it helps to think of the
217 * pipeline as being a long one. Each buffer head contains a bulk-in and
218 * a bulk-out request pointer (since the buffer can be used for both
219 * output and input -- directions always are given from the host's
220 * point of view) as well as a pointer to the buffer and various state
221 * variables.
222 *
223 * Use of the pipeline follows a simple protocol. There is a variable
224 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
225 * At any time that buffer head may still be in use from an earlier
226 * request, so each buffer head has a state variable indicating whether
227 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
228 * buffer head to be EMPTY, filling the buffer either by file I/O or by
229 * USB I/O (during which the buffer head is BUSY), and marking the buffer
230 * head FULL when the I/O is complete. Then the buffer will be emptied
231 * (again possibly by USB I/O, during which it is marked BUSY) and
232 * finally marked EMPTY again (possibly by a completion routine).
233 *
234 * A module parameter tells the driver to avoid stalling the bulk
235 * endpoints wherever the transport specification allows. This is
236 * necessary for some UDCs like the SuperH, which cannot reliably clear a
237 * halt on a bulk endpoint. However, under certain circumstances the
238 * Bulk-only specification requires a stall. In such cases the driver
239 * will halt the endpoint and set a flag indicating that it should clear
240 * the halt in software during the next device reset. Hopefully this
241 * will permit everything to work correctly. Furthermore, although the
242 * specification allows the bulk-out endpoint to halt when the host sends
243 * too much data, implementing this would cause an unavoidable race.
244 * The driver will always use the "no-stall" approach for OUT transfers.
245 *
246 * One subtle point concerns sending status-stage responses for ep0
247 * requests. Some of these requests, such as device reset, can involve
248 * interrupting an ongoing file I/O operation, which might take an
249 * arbitrarily long time. During that delay the host might give up on
250 * the original ep0 request and issue a new one. When that happens the
251 * driver should not notify the host about completion of the original
252 * request, as the host will no longer be waiting for it. So the driver
253 * assigns to each ep0 request a unique tag, and it keeps track of the
254 * tag value of the request associated with a long-running exception
255 * (device-reset, interface-change, or configuration-change). When the
256 * exception handler is finished, the status-stage response is submitted
257 * only if the current ep0 request tag is equal to the exception request
258 * tag. Thus only the most recently received ep0 request will get a
259 * status-stage response.
260 *
261 * Warning: This driver source file is too long. It ought to be split up
262 * into a header file plus about 3 separate .c files, to handle the details
263 * of the Gadget, USB Mass Storage, and SCSI protocols.
264 */
265
266
267/* #define VERBOSE_DEBUG */
268/* #define DUMP_MSGS */
269
270
271#include <linux/blkdev.h>
272#include <linux/completion.h>
273#include <linux/dcache.h>
274#include <linux/delay.h>
275#include <linux/device.h>
276#include <linux/fcntl.h>
277#include <linux/file.h>
278#include <linux/fs.h>
279#include <linux/kref.h>
280#include <linux/kthread.h>
281#include <linux/limits.h>
282#include <linux/rwsem.h>
283#include <linux/slab.h>
284#include <linux/spinlock.h>
285#include <linux/string.h>
286#include <linux/freezer.h>
287#include <linux/utsname.h>
288
289#include <linux/usb/ch9.h>
290#include <linux/usb/gadget.h>
291
292#include "gadget_chips.h"
293
294
295
296/*------------------------------------------------------------------------*/
297
298#define FSG_DRIVER_DESC "Mass Storage Function"
299#define FSG_DRIVER_VERSION "2009/09/11"
300
301static const char fsg_string_interface[] = "Mass Storage";
302
303
304#define FSG_NO_INTR_EP 1
305#define FSG_BUFFHD_STATIC_BUFFER 1
306#define FSG_NO_DEVICE_STRINGS 1
307#define FSG_NO_OTG 1
308#define FSG_NO_INTR_EP 1
309
310#include "storage_common.c"
311
312
313/*-------------------------------------------------------------------------*/
314
315struct fsg_dev;
316
317
318/* Data shared by all the FSG instances. */
319struct fsg_common {
320 struct usb_gadget *gadget;
321 struct fsg_dev *fsg;
322 struct fsg_dev *prev_fsg;
323
324 /* filesem protects: backing files in use */
325 struct rw_semaphore filesem;
326
327 /* lock protects: state, all the req_busy's */
328 spinlock_t lock;
329
330 struct usb_ep *ep0; /* Copy of gadget->ep0 */
331 struct usb_request *ep0req; /* Copy of cdev->req */
332 unsigned int ep0_req_tag;
333 const char *ep0req_name;
334
335 struct fsg_buffhd *next_buffhd_to_fill;
336 struct fsg_buffhd *next_buffhd_to_drain;
337 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
338
339 int cmnd_size;
340 u8 cmnd[MAX_COMMAND_SIZE];
341
342 unsigned int nluns;
343 unsigned int lun;
344 struct fsg_lun *luns;
345 struct fsg_lun *curlun;
346
347 unsigned int bulk_out_maxpacket;
348 enum fsg_state state; /* For exception handling */
349 unsigned int exception_req_tag;
350
351 u8 config, new_config;
352 enum data_direction data_dir;
353 u32 data_size;
354 u32 data_size_from_cmnd;
355 u32 tag;
356 u32 residue;
357 u32 usb_amount_left;
358
359 unsigned int can_stall:1;
360 unsigned int free_storage_on_release:1;
361 unsigned int phase_error:1;
362 unsigned int short_packet_received:1;
363 unsigned int bad_lun_okay:1;
364 unsigned int running:1;
365
366 int thread_wakeup_needed;
367 struct completion thread_notifier;
368 struct task_struct *thread_task;
369
370 /* Callback function to call when thread exits. */
371 void (*thread_exits)(struct fsg_common *common);
372 /* Gadget's private data. */
373 void *private_data;
374
375 /* Vendor (8 chars), product (16 chars), release (4
376 * hexadecimal digits) and NUL byte */
377 char inquiry_string[8 + 16 + 4 + 1];
378
379 struct kref ref;
380};
381
382
383struct fsg_config {
384 unsigned nluns;
385 struct fsg_lun_config {
386 const char *filename;
387 char ro;
388 char removable;
389 char cdrom;
390 } luns[FSG_MAX_LUNS];
391
392 const char *lun_name_format;
393 const char *thread_name;
394
395 /* Callback function to call when thread exits. */
396 void (*thread_exits)(struct fsg_common *common);
397 /* Gadget's private data. */
398 void *private_data;
399
400 const char *vendor_name; /* 8 characters or less */
401 const char *product_name; /* 16 characters or less */
402 u16 release;
403
404 char can_stall;
405};
406
407
408struct fsg_dev {
409 struct usb_function function;
410 struct usb_gadget *gadget; /* Copy of cdev->gadget */
411 struct fsg_common *common;
412
413 u16 interface_number;
414
415 unsigned int bulk_in_enabled:1;
416 unsigned int bulk_out_enabled:1;
417
418 unsigned long atomic_bitflags;
419#define IGNORE_BULK_OUT 0
420
421 struct usb_ep *bulk_in;
422 struct usb_ep *bulk_out;
423};
424
425
426static inline int __fsg_is_set(struct fsg_common *common,
427 const char *func, unsigned line)
428{
429 if (common->fsg)
430 return 1;
431 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
432 return 0;
433}
434
435#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
436
437
438static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
439{
440 return container_of(f, struct fsg_dev, function);
441}
442
443
444typedef void (*fsg_routine_t)(struct fsg_dev *);
445
446static int exception_in_progress(struct fsg_common *common)
447{
448 return common->state > FSG_STATE_IDLE;
449}
450
451/* Make bulk-out requests be divisible by the maxpacket size */
452static void set_bulk_out_req_length(struct fsg_common *common,
453 struct fsg_buffhd *bh, unsigned int length)
454{
455 unsigned int rem;
456
457 bh->bulk_out_intended_length = length;
458 rem = length % common->bulk_out_maxpacket;
459 if (rem > 0)
460 length += common->bulk_out_maxpacket - rem;
461 bh->outreq->length = length;
462}
463
464/*-------------------------------------------------------------------------*/
465
466static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
467{
468 const char *name;
469
470 if (ep == fsg->bulk_in)
471 name = "bulk-in";
472 else if (ep == fsg->bulk_out)
473 name = "bulk-out";
474 else
475 name = ep->name;
476 DBG(fsg, "%s set halt\n", name);
477 return usb_ep_set_halt(ep);
478}
479
480
481/*-------------------------------------------------------------------------*/
482
483/* These routines may be called in process context or in_irq */
484
485/* Caller must hold fsg->lock */
486static void wakeup_thread(struct fsg_common *common)
487{
488 /* Tell the main thread that something has happened */
489 common->thread_wakeup_needed = 1;
490 if (common->thread_task)
491 wake_up_process(common->thread_task);
492}
493
494
495static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
496{
497 unsigned long flags;
498
499 /* Do nothing if a higher-priority exception is already in progress.
500 * If a lower-or-equal priority exception is in progress, preempt it
501 * and notify the main thread by sending it a signal. */
502 spin_lock_irqsave(&common->lock, flags);
503 if (common->state <= new_state) {
504 common->exception_req_tag = common->ep0_req_tag;
505 common->state = new_state;
506 if (common->thread_task)
507 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
508 common->thread_task);
509 }
510 spin_unlock_irqrestore(&common->lock, flags);
511}
512
513
514/*-------------------------------------------------------------------------*/
515
516static int ep0_queue(struct fsg_common *common)
517{
518 int rc;
519
520 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
521 common->ep0->driver_data = common;
522 if (rc != 0 && rc != -ESHUTDOWN) {
523 /* We can't do much more than wait for a reset */
524 WARNING(common, "error in submission: %s --> %d\n",
525 common->ep0->name, rc);
526 }
527 return rc;
528}
529
530/*-------------------------------------------------------------------------*/
531
532/* Bulk and interrupt endpoint completion handlers.
533 * These always run in_irq. */
534
535static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
536{
537 struct fsg_common *common = ep->driver_data;
538 struct fsg_buffhd *bh = req->context;
539
540 if (req->status || req->actual != req->length)
541 DBG(common, "%s --> %d, %u/%u\n", __func__,
542 req->status, req->actual, req->length);
543 if (req->status == -ECONNRESET) /* Request was cancelled */
544 usb_ep_fifo_flush(ep);
545
546 /* Hold the lock while we update the request and buffer states */
547 smp_wmb();
548 spin_lock(&common->lock);
549 bh->inreq_busy = 0;
550 bh->state = BUF_STATE_EMPTY;
551 wakeup_thread(common);
552 spin_unlock(&common->lock);
553}
554
555static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
556{
557 struct fsg_common *common = ep->driver_data;
558 struct fsg_buffhd *bh = req->context;
559
560 dump_msg(common, "bulk-out", req->buf, req->actual);
561 if (req->status || req->actual != bh->bulk_out_intended_length)
562 DBG(common, "%s --> %d, %u/%u\n", __func__,
563 req->status, req->actual,
564 bh->bulk_out_intended_length);
565 if (req->status == -ECONNRESET) /* Request was cancelled */
566 usb_ep_fifo_flush(ep);
567
568 /* Hold the lock while we update the request and buffer states */
569 smp_wmb();
570 spin_lock(&common->lock);
571 bh->outreq_busy = 0;
572 bh->state = BUF_STATE_FULL;
573 wakeup_thread(common);
574 spin_unlock(&common->lock);
575}
576
577
578/*-------------------------------------------------------------------------*/
579
580/* Ep0 class-specific handlers. These always run in_irq. */
581
582static int fsg_setup(struct usb_function *f,
583 const struct usb_ctrlrequest *ctrl)
584{
585 struct fsg_dev *fsg = fsg_from_func(f);
586 struct usb_request *req = fsg->common->ep0req;
587 u16 w_index = le16_to_cpu(ctrl->wIndex);
588 u16 w_value = le16_to_cpu(ctrl->wValue);
589 u16 w_length = le16_to_cpu(ctrl->wLength);
590
591 if (!fsg->common->config)
592 return -EOPNOTSUPP;
593
594 switch (ctrl->bRequest) {
595
596 case USB_BULK_RESET_REQUEST:
597 if (ctrl->bRequestType !=
598 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
599 break;
600 if (w_index != fsg->interface_number || w_value != 0)
601 return -EDOM;
602
603 /* Raise an exception to stop the current operation
604 * and reinitialize our state. */
605 DBG(fsg, "bulk reset request\n");
606 raise_exception(fsg->common, FSG_STATE_RESET);
607 return DELAYED_STATUS;
608
609 case USB_BULK_GET_MAX_LUN_REQUEST:
610 if (ctrl->bRequestType !=
611 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
612 break;
613 if (w_index != fsg->interface_number || w_value != 0)
614 return -EDOM;
615 VDBG(fsg, "get max LUN\n");
616 *(u8 *) req->buf = fsg->common->nluns - 1;
617 return 1;
618 }
619
620 VDBG(fsg,
621 "unknown class-specific control req "
622 "%02x.%02x v%04x i%04x l%u\n",
623 ctrl->bRequestType, ctrl->bRequest,
624 le16_to_cpu(ctrl->wValue), w_index, w_length);
625 return -EOPNOTSUPP;
626}
627
628
629/*-------------------------------------------------------------------------*/
630
631/* All the following routines run in process context */
632
633
634/* Use this for bulk or interrupt transfers, not ep0 */
635static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
636 struct usb_request *req, int *pbusy,
637 enum fsg_buffer_state *state)
638{
639 int rc;
640
641 if (ep == fsg->bulk_in)
642 dump_msg(fsg, "bulk-in", req->buf, req->length);
643
644 spin_lock_irq(&fsg->common->lock);
645 *pbusy = 1;
646 *state = BUF_STATE_BUSY;
647 spin_unlock_irq(&fsg->common->lock);
648 rc = usb_ep_queue(ep, req, GFP_KERNEL);
649 if (rc != 0) {
650 *pbusy = 0;
651 *state = BUF_STATE_EMPTY;
652
653 /* We can't do much more than wait for a reset */
654
655 /* Note: currently the net2280 driver fails zero-length
656 * submissions if DMA is enabled. */
657 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
658 req->length == 0))
659 WARNING(fsg, "error in submission: %s --> %d\n",
660 ep->name, rc);
661 }
662}
663
664#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
665 if (fsg_is_set(common)) \
666 start_transfer((common)->fsg, (common)->fsg->ep_name, \
667 req, pbusy, state); \
668 else
669
670#define START_TRANSFER(common, ep_name, req, pbusy, state) \
671 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
672
673
674
675static int sleep_thread(struct fsg_common *common)
676{
677 int rc = 0;
678
679 /* Wait until a signal arrives or we are woken up */
680 for (;;) {
681 try_to_freeze();
682 set_current_state(TASK_INTERRUPTIBLE);
683 if (signal_pending(current)) {
684 rc = -EINTR;
685 break;
686 }
687 if (common->thread_wakeup_needed)
688 break;
689 schedule();
690 }
691 __set_current_state(TASK_RUNNING);
692 common->thread_wakeup_needed = 0;
693 return rc;
694}
695
696
697/*-------------------------------------------------------------------------*/
698
699static int do_read(struct fsg_common *common)
700{
701 struct fsg_lun *curlun = common->curlun;
702 u32 lba;
703 struct fsg_buffhd *bh;
704 int rc;
705 u32 amount_left;
706 loff_t file_offset, file_offset_tmp;
707 unsigned int amount;
708 unsigned int partial_page;
709 ssize_t nread;
710
711 /* Get the starting Logical Block Address and check that it's
712 * not too big */
713 if (common->cmnd[0] == SC_READ_6)
714 lba = get_unaligned_be24(&common->cmnd[1]);
715 else {
716 lba = get_unaligned_be32(&common->cmnd[2]);
717
718 /* We allow DPO (Disable Page Out = don't save data in the
719 * cache) and FUA (Force Unit Access = don't read from the
720 * cache), but we don't implement them. */
721 if ((common->cmnd[1] & ~0x18) != 0) {
722 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
723 return -EINVAL;
724 }
725 }
726 if (lba >= curlun->num_sectors) {
727 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
728 return -EINVAL;
729 }
730 file_offset = ((loff_t) lba) << 9;
731
732 /* Carry out the file reads */
733 amount_left = common->data_size_from_cmnd;
734 if (unlikely(amount_left == 0))
735 return -EIO; /* No default reply */
736
737 for (;;) {
738
739 /* Figure out how much we need to read:
740 * Try to read the remaining amount.
741 * But don't read more than the buffer size.
742 * And don't try to read past the end of the file.
743 * Finally, if we're not at a page boundary, don't read past
744 * the next page.
745 * If this means reading 0 then we were asked to read past
746 * the end of file. */
747 amount = min(amount_left, FSG_BUFLEN);
748 amount = min((loff_t) amount,
749 curlun->file_length - file_offset);
750 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
751 if (partial_page > 0)
752 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
753 partial_page);
754
755 /* Wait for the next buffer to become available */
756 bh = common->next_buffhd_to_fill;
757 while (bh->state != BUF_STATE_EMPTY) {
758 rc = sleep_thread(common);
759 if (rc)
760 return rc;
761 }
762
763 /* If we were asked to read past the end of file,
764 * end with an empty buffer. */
765 if (amount == 0) {
766 curlun->sense_data =
767 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
768 curlun->sense_data_info = file_offset >> 9;
769 curlun->info_valid = 1;
770 bh->inreq->length = 0;
771 bh->state = BUF_STATE_FULL;
772 break;
773 }
774
775 /* Perform the read */
776 file_offset_tmp = file_offset;
777 nread = vfs_read(curlun->filp,
778 (char __user *) bh->buf,
779 amount, &file_offset_tmp);
780 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
781 (unsigned long long) file_offset,
782 (int) nread);
783 if (signal_pending(current))
784 return -EINTR;
785
786 if (nread < 0) {
787 LDBG(curlun, "error in file read: %d\n",
788 (int) nread);
789 nread = 0;
790 } else if (nread < amount) {
791 LDBG(curlun, "partial file read: %d/%u\n",
792 (int) nread, amount);
793 nread -= (nread & 511); /* Round down to a block */
794 }
795 file_offset += nread;
796 amount_left -= nread;
797 common->residue -= nread;
798 bh->inreq->length = nread;
799 bh->state = BUF_STATE_FULL;
800
801 /* If an error occurred, report it and its position */
802 if (nread < amount) {
803 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
804 curlun->sense_data_info = file_offset >> 9;
805 curlun->info_valid = 1;
806 break;
807 }
808
809 if (amount_left == 0)
810 break; /* No more left to read */
811
812 /* Send this buffer and go read some more */
813 bh->inreq->zero = 0;
814 START_TRANSFER_OR(common, bulk_in, bh->inreq,
815 &bh->inreq_busy, &bh->state)
816 /* Don't know what to do if
817 * common->fsg is NULL */
818 return -EIO;
819 common->next_buffhd_to_fill = bh->next;
820 }
821
822 return -EIO; /* No default reply */
823}
824
825
826/*-------------------------------------------------------------------------*/
827
828static int do_write(struct fsg_common *common)
829{
830 struct fsg_lun *curlun = common->curlun;
831 u32 lba;
832 struct fsg_buffhd *bh;
833 int get_some_more;
834 u32 amount_left_to_req, amount_left_to_write;
835 loff_t usb_offset, file_offset, file_offset_tmp;
836 unsigned int amount;
837 unsigned int partial_page;
838 ssize_t nwritten;
839 int rc;
840
841 if (curlun->ro) {
842 curlun->sense_data = SS_WRITE_PROTECTED;
843 return -EINVAL;
844 }
845 spin_lock(&curlun->filp->f_lock);
846 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
847 spin_unlock(&curlun->filp->f_lock);
848
849 /* Get the starting Logical Block Address and check that it's
850 * not too big */
851 if (common->cmnd[0] == SC_WRITE_6)
852 lba = get_unaligned_be24(&common->cmnd[1]);
853 else {
854 lba = get_unaligned_be32(&common->cmnd[2]);
855
856 /* We allow DPO (Disable Page Out = don't save data in the
857 * cache) and FUA (Force Unit Access = write directly to the
858 * medium). We don't implement DPO; we implement FUA by
859 * performing synchronous output. */
860 if (common->cmnd[1] & ~0x18) {
861 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
862 return -EINVAL;
863 }
864 if (common->cmnd[1] & 0x08) { /* FUA */
865 spin_lock(&curlun->filp->f_lock);
866 curlun->filp->f_flags |= O_SYNC;
867 spin_unlock(&curlun->filp->f_lock);
868 }
869 }
870 if (lba >= curlun->num_sectors) {
871 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
872 return -EINVAL;
873 }
874
875 /* Carry out the file writes */
876 get_some_more = 1;
877 file_offset = usb_offset = ((loff_t) lba) << 9;
878 amount_left_to_req = common->data_size_from_cmnd;
879 amount_left_to_write = common->data_size_from_cmnd;
880
881 while (amount_left_to_write > 0) {
882
883 /* Queue a request for more data from the host */
884 bh = common->next_buffhd_to_fill;
885 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
886
887 /* Figure out how much we want to get:
888 * Try to get the remaining amount.
889 * But don't get more than the buffer size.
890 * And don't try to go past the end of the file.
891 * If we're not at a page boundary,
892 * don't go past the next page.
893 * If this means getting 0, then we were asked
894 * to write past the end of file.
895 * Finally, round down to a block boundary. */
896 amount = min(amount_left_to_req, FSG_BUFLEN);
897 amount = min((loff_t) amount, curlun->file_length -
898 usb_offset);
899 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
900 if (partial_page > 0)
901 amount = min(amount,
902 (unsigned int) PAGE_CACHE_SIZE - partial_page);
903
904 if (amount == 0) {
905 get_some_more = 0;
906 curlun->sense_data =
907 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
908 curlun->sense_data_info = usb_offset >> 9;
909 curlun->info_valid = 1;
910 continue;
911 }
912 amount -= (amount & 511);
913 if (amount == 0) {
914
915 /* Why were we were asked to transfer a
916 * partial block? */
917 get_some_more = 0;
918 continue;
919 }
920
921 /* Get the next buffer */
922 usb_offset += amount;
923 common->usb_amount_left -= amount;
924 amount_left_to_req -= amount;
925 if (amount_left_to_req == 0)
926 get_some_more = 0;
927
928 /* amount is always divisible by 512, hence by
929 * the bulk-out maxpacket size */
930 bh->outreq->length = amount;
931 bh->bulk_out_intended_length = amount;
932 bh->outreq->short_not_ok = 1;
933 START_TRANSFER_OR(common, bulk_out, bh->outreq,
934 &bh->outreq_busy, &bh->state)
935 /* Don't know what to do if
936 * common->fsg is NULL */
937 return -EIO;
938 common->next_buffhd_to_fill = bh->next;
939 continue;
940 }
941
942 /* Write the received data to the backing file */
943 bh = common->next_buffhd_to_drain;
944 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
945 break; /* We stopped early */
946 if (bh->state == BUF_STATE_FULL) {
947 smp_rmb();
948 common->next_buffhd_to_drain = bh->next;
949 bh->state = BUF_STATE_EMPTY;
950
951 /* Did something go wrong with the transfer? */
952 if (bh->outreq->status != 0) {
953 curlun->sense_data = SS_COMMUNICATION_FAILURE;
954 curlun->sense_data_info = file_offset >> 9;
955 curlun->info_valid = 1;
956 break;
957 }
958
959 amount = bh->outreq->actual;
960 if (curlun->file_length - file_offset < amount) {
961 LERROR(curlun,
962 "write %u @ %llu beyond end %llu\n",
963 amount, (unsigned long long) file_offset,
964 (unsigned long long) curlun->file_length);
965 amount = curlun->file_length - file_offset;
966 }
967
968 /* Perform the write */
969 file_offset_tmp = file_offset;
970 nwritten = vfs_write(curlun->filp,
971 (char __user *) bh->buf,
972 amount, &file_offset_tmp);
973 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
974 (unsigned long long) file_offset,
975 (int) nwritten);
976 if (signal_pending(current))
977 return -EINTR; /* Interrupted! */
978
979 if (nwritten < 0) {
980 LDBG(curlun, "error in file write: %d\n",
981 (int) nwritten);
982 nwritten = 0;
983 } else if (nwritten < amount) {
984 LDBG(curlun, "partial file write: %d/%u\n",
985 (int) nwritten, amount);
986 nwritten -= (nwritten & 511);
987 /* Round down to a block */
988 }
989 file_offset += nwritten;
990 amount_left_to_write -= nwritten;
991 common->residue -= nwritten;
992
993 /* If an error occurred, report it and its position */
994 if (nwritten < amount) {
995 curlun->sense_data = SS_WRITE_ERROR;
996 curlun->sense_data_info = file_offset >> 9;
997 curlun->info_valid = 1;
998 break;
999 }
1000
1001 /* Did the host decide to stop early? */
1002 if (bh->outreq->actual != bh->outreq->length) {
1003 common->short_packet_received = 1;
1004 break;
1005 }
1006 continue;
1007 }
1008
1009 /* Wait for something to happen */
1010 rc = sleep_thread(common);
1011 if (rc)
1012 return rc;
1013 }
1014
1015 return -EIO; /* No default reply */
1016}
1017
1018
1019/*-------------------------------------------------------------------------*/
1020
1021static int do_synchronize_cache(struct fsg_common *common)
1022{
1023 struct fsg_lun *curlun = common->curlun;
1024 int rc;
1025
1026 /* We ignore the requested LBA and write out all file's
1027 * dirty data buffers. */
1028 rc = fsg_lun_fsync_sub(curlun);
1029 if (rc)
1030 curlun->sense_data = SS_WRITE_ERROR;
1031 return 0;
1032}
1033
1034
1035/*-------------------------------------------------------------------------*/
1036
1037static void invalidate_sub(struct fsg_lun *curlun)
1038{
1039 struct file *filp = curlun->filp;
1040 struct inode *inode = filp->f_path.dentry->d_inode;
1041 unsigned long rc;
1042
1043 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1044 VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc);
1045}
1046
1047static int do_verify(struct fsg_common *common)
1048{
1049 struct fsg_lun *curlun = common->curlun;
1050 u32 lba;
1051 u32 verification_length;
1052 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
1053 loff_t file_offset, file_offset_tmp;
1054 u32 amount_left;
1055 unsigned int amount;
1056 ssize_t nread;
1057
1058 /* Get the starting Logical Block Address and check that it's
1059 * not too big */
1060 lba = get_unaligned_be32(&common->cmnd[2]);
1061 if (lba >= curlun->num_sectors) {
1062 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1063 return -EINVAL;
1064 }
1065
1066 /* We allow DPO (Disable Page Out = don't save data in the
1067 * cache) but we don't implement it. */
1068 if (common->cmnd[1] & ~0x10) {
1069 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1070 return -EINVAL;
1071 }
1072
1073 verification_length = get_unaligned_be16(&common->cmnd[7]);
1074 if (unlikely(verification_length == 0))
1075 return -EIO; /* No default reply */
1076
1077 /* Prepare to carry out the file verify */
1078 amount_left = verification_length << 9;
1079 file_offset = ((loff_t) lba) << 9;
1080
1081 /* Write out all the dirty buffers before invalidating them */
1082 fsg_lun_fsync_sub(curlun);
1083 if (signal_pending(current))
1084 return -EINTR;
1085
1086 invalidate_sub(curlun);
1087 if (signal_pending(current))
1088 return -EINTR;
1089
1090 /* Just try to read the requested blocks */
1091 while (amount_left > 0) {
1092
1093 /* Figure out how much we need to read:
1094 * Try to read the remaining amount, but not more than
1095 * the buffer size.
1096 * And don't try to read past the end of the file.
1097 * If this means reading 0 then we were asked to read
1098 * past the end of file. */
1099 amount = min(amount_left, FSG_BUFLEN);
1100 amount = min((loff_t) amount,
1101 curlun->file_length - file_offset);
1102 if (amount == 0) {
1103 curlun->sense_data =
1104 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1105 curlun->sense_data_info = file_offset >> 9;
1106 curlun->info_valid = 1;
1107 break;
1108 }
1109
1110 /* Perform the read */
1111 file_offset_tmp = file_offset;
1112 nread = vfs_read(curlun->filp,
1113 (char __user *) bh->buf,
1114 amount, &file_offset_tmp);
1115 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1116 (unsigned long long) file_offset,
1117 (int) nread);
1118 if (signal_pending(current))
1119 return -EINTR;
1120
1121 if (nread < 0) {
1122 LDBG(curlun, "error in file verify: %d\n",
1123 (int) nread);
1124 nread = 0;
1125 } else if (nread < amount) {
1126 LDBG(curlun, "partial file verify: %d/%u\n",
1127 (int) nread, amount);
1128 nread -= (nread & 511); /* Round down to a sector */
1129 }
1130 if (nread == 0) {
1131 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1132 curlun->sense_data_info = file_offset >> 9;
1133 curlun->info_valid = 1;
1134 break;
1135 }
1136 file_offset += nread;
1137 amount_left -= nread;
1138 }
1139 return 0;
1140}
1141
1142
1143/*-------------------------------------------------------------------------*/
1144
1145static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1146{
1147 struct fsg_lun *curlun = common->curlun;
1148 u8 *buf = (u8 *) bh->buf;
1149
1150 if (!curlun) { /* Unsupported LUNs are okay */
1151 common->bad_lun_okay = 1;
1152 memset(buf, 0, 36);
1153 buf[0] = 0x7f; /* Unsupported, no device-type */
1154 buf[4] = 31; /* Additional length */
1155 return 36;
1156 }
1157
1158 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1159 buf[1] = curlun->removable ? 0x80 : 0;
1160 buf[2] = 2; /* ANSI SCSI level 2 */
1161 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1162 buf[4] = 31; /* Additional length */
1163 buf[5] = 0; /* No special options */
1164 buf[6] = 0;
1165 buf[7] = 0;
1166 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
1167 return 36;
1168}
1169
1170
1171static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1172{
1173 struct fsg_lun *curlun = common->curlun;
1174 u8 *buf = (u8 *) bh->buf;
1175 u32 sd, sdinfo;
1176 int valid;
1177
1178 /*
1179 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1180 *
1181 * If a REQUEST SENSE command is received from an initiator
1182 * with a pending unit attention condition (before the target
1183 * generates the contingent allegiance condition), then the
1184 * target shall either:
1185 * a) report any pending sense data and preserve the unit
1186 * attention condition on the logical unit, or,
1187 * b) report the unit attention condition, may discard any
1188 * pending sense data, and clear the unit attention
1189 * condition on the logical unit for that initiator.
1190 *
1191 * FSG normally uses option a); enable this code to use option b).
1192 */
1193#if 0
1194 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1195 curlun->sense_data = curlun->unit_attention_data;
1196 curlun->unit_attention_data = SS_NO_SENSE;
1197 }
1198#endif
1199
1200 if (!curlun) { /* Unsupported LUNs are okay */
1201 common->bad_lun_okay = 1;
1202 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1203 sdinfo = 0;
1204 valid = 0;
1205 } else {
1206 sd = curlun->sense_data;
1207 sdinfo = curlun->sense_data_info;
1208 valid = curlun->info_valid << 7;
1209 curlun->sense_data = SS_NO_SENSE;
1210 curlun->sense_data_info = 0;
1211 curlun->info_valid = 0;
1212 }
1213
1214 memset(buf, 0, 18);
1215 buf[0] = valid | 0x70; /* Valid, current error */
1216 buf[2] = SK(sd);
1217 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1218 buf[7] = 18 - 8; /* Additional sense length */
1219 buf[12] = ASC(sd);
1220 buf[13] = ASCQ(sd);
1221 return 18;
1222}
1223
1224
1225static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1226{
1227 struct fsg_lun *curlun = common->curlun;
1228 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1229 int pmi = common->cmnd[8];
1230 u8 *buf = (u8 *) bh->buf;
1231
1232 /* Check the PMI and LBA fields */
1233 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1234 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1235 return -EINVAL;
1236 }
1237
1238 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1239 /* Max logical block */
1240 put_unaligned_be32(512, &buf[4]); /* Block length */
1241 return 8;
1242}
1243
1244
1245static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1246{
1247 struct fsg_lun *curlun = common->curlun;
1248 int msf = common->cmnd[1] & 0x02;
1249 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1250 u8 *buf = (u8 *) bh->buf;
1251
1252 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
1253 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1254 return -EINVAL;
1255 }
1256 if (lba >= curlun->num_sectors) {
1257 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1258 return -EINVAL;
1259 }
1260
1261 memset(buf, 0, 8);
1262 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1263 store_cdrom_address(&buf[4], msf, lba);
1264 return 8;
1265}
1266
1267
1268static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1269{
1270 struct fsg_lun *curlun = common->curlun;
1271 int msf = common->cmnd[1] & 0x02;
1272 int start_track = common->cmnd[6];
1273 u8 *buf = (u8 *) bh->buf;
1274
1275 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1276 start_track > 1) {
1277 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1278 return -EINVAL;
1279 }
1280
1281 memset(buf, 0, 20);
1282 buf[1] = (20-2); /* TOC data length */
1283 buf[2] = 1; /* First track number */
1284 buf[3] = 1; /* Last track number */
1285 buf[5] = 0x16; /* Data track, copying allowed */
1286 buf[6] = 0x01; /* Only track is number 1 */
1287 store_cdrom_address(&buf[8], msf, 0);
1288
1289 buf[13] = 0x16; /* Lead-out track is data */
1290 buf[14] = 0xAA; /* Lead-out track number */
1291 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1292 return 20;
1293}
1294
1295
1296static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1297{
1298 struct fsg_lun *curlun = common->curlun;
1299 int mscmnd = common->cmnd[0];
1300 u8 *buf = (u8 *) bh->buf;
1301 u8 *buf0 = buf;
1302 int pc, page_code;
1303 int changeable_values, all_pages;
1304 int valid_page = 0;
1305 int len, limit;
1306
1307 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
1308 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1309 return -EINVAL;
1310 }
1311 pc = common->cmnd[2] >> 6;
1312 page_code = common->cmnd[2] & 0x3f;
1313 if (pc == 3) {
1314 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1315 return -EINVAL;
1316 }
1317 changeable_values = (pc == 1);
1318 all_pages = (page_code == 0x3f);
1319
1320 /* Write the mode parameter header. Fixed values are: default
1321 * medium type, no cache control (DPOFUA), and no block descriptors.
1322 * The only variable value is the WriteProtect bit. We will fill in
1323 * the mode data length later. */
1324 memset(buf, 0, 8);
1325 if (mscmnd == SC_MODE_SENSE_6) {
1326 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1327 buf += 4;
1328 limit = 255;
1329 } else { /* SC_MODE_SENSE_10 */
1330 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1331 buf += 8;
1332 limit = 65535; /* Should really be FSG_BUFLEN */
1333 }
1334
1335 /* No block descriptors */
1336
1337 /* The mode pages, in numerical order. The only page we support
1338 * is the Caching page. */
1339 if (page_code == 0x08 || all_pages) {
1340 valid_page = 1;
1341 buf[0] = 0x08; /* Page code */
1342 buf[1] = 10; /* Page length */
1343 memset(buf+2, 0, 10); /* None of the fields are changeable */
1344
1345 if (!changeable_values) {
1346 buf[2] = 0x04; /* Write cache enable, */
1347 /* Read cache not disabled */
1348 /* No cache retention priorities */
1349 put_unaligned_be16(0xffff, &buf[4]);
1350 /* Don't disable prefetch */
1351 /* Minimum prefetch = 0 */
1352 put_unaligned_be16(0xffff, &buf[8]);
1353 /* Maximum prefetch */
1354 put_unaligned_be16(0xffff, &buf[10]);
1355 /* Maximum prefetch ceiling */
1356 }
1357 buf += 12;
1358 }
1359
1360 /* Check that a valid page was requested and the mode data length
1361 * isn't too long. */
1362 len = buf - buf0;
1363 if (!valid_page || len > limit) {
1364 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1365 return -EINVAL;
1366 }
1367
1368 /* Store the mode data length */
1369 if (mscmnd == SC_MODE_SENSE_6)
1370 buf0[0] = len - 1;
1371 else
1372 put_unaligned_be16(len - 2, buf0);
1373 return len;
1374}
1375
1376
1377static int do_start_stop(struct fsg_common *common)
1378{
1379 if (!common->curlun) {
1380 return -EINVAL;
1381 } else if (!common->curlun->removable) {
1382 common->curlun->sense_data = SS_INVALID_COMMAND;
1383 return -EINVAL;
1384 }
1385 return 0;
1386}
1387
1388
1389static int do_prevent_allow(struct fsg_common *common)
1390{
1391 struct fsg_lun *curlun = common->curlun;
1392 int prevent;
1393
1394 if (!common->curlun) {
1395 return -EINVAL;
1396 } else if (!common->curlun->removable) {
1397 common->curlun->sense_data = SS_INVALID_COMMAND;
1398 return -EINVAL;
1399 }
1400
1401 prevent = common->cmnd[4] & 0x01;
1402 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
1403 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1404 return -EINVAL;
1405 }
1406
1407 if (curlun->prevent_medium_removal && !prevent)
1408 fsg_lun_fsync_sub(curlun);
1409 curlun->prevent_medium_removal = prevent;
1410 return 0;
1411}
1412
1413
1414static int do_read_format_capacities(struct fsg_common *common,
1415 struct fsg_buffhd *bh)
1416{
1417 struct fsg_lun *curlun = common->curlun;
1418 u8 *buf = (u8 *) bh->buf;
1419
1420 buf[0] = buf[1] = buf[2] = 0;
1421 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
1422 buf += 4;
1423
1424 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1425 /* Number of blocks */
1426 put_unaligned_be32(512, &buf[4]); /* Block length */
1427 buf[4] = 0x02; /* Current capacity */
1428 return 12;
1429}
1430
1431
1432static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1433{
1434 struct fsg_lun *curlun = common->curlun;
1435
1436 /* We don't support MODE SELECT */
1437 if (curlun)
1438 curlun->sense_data = SS_INVALID_COMMAND;
1439 return -EINVAL;
1440}
1441
1442
1443/*-------------------------------------------------------------------------*/
1444
1445static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1446{
1447 int rc;
1448
1449 rc = fsg_set_halt(fsg, fsg->bulk_in);
1450 if (rc == -EAGAIN)
1451 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1452 while (rc != 0) {
1453 if (rc != -EAGAIN) {
1454 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1455 rc = 0;
1456 break;
1457 }
1458
1459 /* Wait for a short time and then try again */
1460 if (msleep_interruptible(100) != 0)
1461 return -EINTR;
1462 rc = usb_ep_set_halt(fsg->bulk_in);
1463 }
1464 return rc;
1465}
1466
1467static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1468{
1469 int rc;
1470
1471 DBG(fsg, "bulk-in set wedge\n");
1472 rc = usb_ep_set_wedge(fsg->bulk_in);
1473 if (rc == -EAGAIN)
1474 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1475 while (rc != 0) {
1476 if (rc != -EAGAIN) {
1477 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1478 rc = 0;
1479 break;
1480 }
1481
1482 /* Wait for a short time and then try again */
1483 if (msleep_interruptible(100) != 0)
1484 return -EINTR;
1485 rc = usb_ep_set_wedge(fsg->bulk_in);
1486 }
1487 return rc;
1488}
1489
1490static int pad_with_zeros(struct fsg_dev *fsg)
1491{
1492 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1493 u32 nkeep = bh->inreq->length;
1494 u32 nsend;
1495 int rc;
1496
1497 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
1498 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1499 while (fsg->common->usb_amount_left > 0) {
1500
1501 /* Wait for the next buffer to be free */
1502 while (bh->state != BUF_STATE_EMPTY) {
1503 rc = sleep_thread(fsg->common);
1504 if (rc)
1505 return rc;
1506 }
1507
1508 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
1509 memset(bh->buf + nkeep, 0, nsend - nkeep);
1510 bh->inreq->length = nsend;
1511 bh->inreq->zero = 0;
1512 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1513 &bh->inreq_busy, &bh->state);
1514 bh = fsg->common->next_buffhd_to_fill = bh->next;
1515 fsg->common->usb_amount_left -= nsend;
1516 nkeep = 0;
1517 }
1518 return 0;
1519}
1520
1521static int throw_away_data(struct fsg_common *common)
1522{
1523 struct fsg_buffhd *bh;
1524 u32 amount;
1525 int rc;
1526
1527 for (bh = common->next_buffhd_to_drain;
1528 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1529 bh = common->next_buffhd_to_drain) {
1530
1531 /* Throw away the data in a filled buffer */
1532 if (bh->state == BUF_STATE_FULL) {
1533 smp_rmb();
1534 bh->state = BUF_STATE_EMPTY;
1535 common->next_buffhd_to_drain = bh->next;
1536
1537 /* A short packet or an error ends everything */
1538 if (bh->outreq->actual != bh->outreq->length ||
1539 bh->outreq->status != 0) {
1540 raise_exception(common,
1541 FSG_STATE_ABORT_BULK_OUT);
1542 return -EINTR;
1543 }
1544 continue;
1545 }
1546
1547 /* Try to submit another request if we need one */
1548 bh = common->next_buffhd_to_fill;
1549 if (bh->state == BUF_STATE_EMPTY
1550 && common->usb_amount_left > 0) {
1551 amount = min(common->usb_amount_left, FSG_BUFLEN);
1552
1553 /* amount is always divisible by 512, hence by
1554 * the bulk-out maxpacket size */
1555 bh->outreq->length = amount;
1556 bh->bulk_out_intended_length = amount;
1557 bh->outreq->short_not_ok = 1;
1558 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1559 &bh->outreq_busy, &bh->state)
1560 /* Don't know what to do if
1561 * common->fsg is NULL */
1562 return -EIO;
1563 common->next_buffhd_to_fill = bh->next;
1564 common->usb_amount_left -= amount;
1565 continue;
1566 }
1567
1568 /* Otherwise wait for something to happen */
1569 rc = sleep_thread(common);
1570 if (rc)
1571 return rc;
1572 }
1573 return 0;
1574}
1575
1576
1577static int finish_reply(struct fsg_common *common)
1578{
1579 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
1580 int rc = 0;
1581
1582 switch (common->data_dir) {
1583 case DATA_DIR_NONE:
1584 break; /* Nothing to send */
1585
1586 /* If we don't know whether the host wants to read or write,
1587 * this must be CB or CBI with an unknown command. We mustn't
1588 * try to send or receive any data. So stall both bulk pipes
1589 * if we can and wait for a reset. */
1590 case DATA_DIR_UNKNOWN:
1591 if (!common->can_stall) {
1592 /* Nothing */
1593 } else if (fsg_is_set(common)) {
1594 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1595 rc = halt_bulk_in_endpoint(common->fsg);
1596 } else {
1597 /* Don't know what to do if common->fsg is NULL */
1598 rc = -EIO;
1599 }
1600 break;
1601
1602 /* All but the last buffer of data must have already been sent */
1603 case DATA_DIR_TO_HOST:
1604 if (common->data_size == 0) {
1605 /* Nothing to send */
1606
1607 /* If there's no residue, simply send the last buffer */
1608 } else if (common->residue == 0) {
1609 bh->inreq->zero = 0;
1610 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1611 &bh->inreq_busy, &bh->state)
1612 return -EIO;
1613 common->next_buffhd_to_fill = bh->next;
1614
1615 /* For Bulk-only, if we're allowed to stall then send the
1616 * short packet and halt the bulk-in endpoint. If we can't
1617 * stall, pad out the remaining data with 0's. */
1618 } else if (common->can_stall) {
1619 bh->inreq->zero = 1;
1620 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1621 &bh->inreq_busy, &bh->state)
1622 /* Don't know what to do if
1623 * common->fsg is NULL */
1624 rc = -EIO;
1625 common->next_buffhd_to_fill = bh->next;
1626 if (common->fsg)
1627 rc = halt_bulk_in_endpoint(common->fsg);
1628 } else if (fsg_is_set(common)) {
1629 rc = pad_with_zeros(common->fsg);
1630 } else {
1631 /* Don't know what to do if common->fsg is NULL */
1632 rc = -EIO;
1633 }
1634 break;
1635
1636 /* We have processed all we want from the data the host has sent.
1637 * There may still be outstanding bulk-out requests. */
1638 case DATA_DIR_FROM_HOST:
1639 if (common->residue == 0) {
1640 /* Nothing to receive */
1641
1642 /* Did the host stop sending unexpectedly early? */
1643 } else if (common->short_packet_received) {
1644 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1645 rc = -EINTR;
1646
1647 /* We haven't processed all the incoming data. Even though
1648 * we may be allowed to stall, doing so would cause a race.
1649 * The controller may already have ACK'ed all the remaining
1650 * bulk-out packets, in which case the host wouldn't see a
1651 * STALL. Not realizing the endpoint was halted, it wouldn't
1652 * clear the halt -- leading to problems later on. */
1653#if 0
1654 } else if (common->can_stall) {
1655 if (fsg_is_set(common))
1656 fsg_set_halt(common->fsg,
1657 common->fsg->bulk_out);
1658 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1659 rc = -EINTR;
1660#endif
1661
1662 /* We can't stall. Read in the excess data and throw it
1663 * all away. */
1664 } else {
1665 rc = throw_away_data(common);
1666 }
1667 break;
1668 }
1669 return rc;
1670}
1671
1672
1673static int send_status(struct fsg_common *common)
1674{
1675 struct fsg_lun *curlun = common->curlun;
1676 struct fsg_buffhd *bh;
1677 struct bulk_cs_wrap *csw;
1678 int rc;
1679 u8 status = USB_STATUS_PASS;
1680 u32 sd, sdinfo = 0;
1681
1682 /* Wait for the next buffer to become available */
1683 bh = common->next_buffhd_to_fill;
1684 while (bh->state != BUF_STATE_EMPTY) {
1685 rc = sleep_thread(common);
1686 if (rc)
1687 return rc;
1688 }
1689
1690 if (curlun) {
1691 sd = curlun->sense_data;
1692 sdinfo = curlun->sense_data_info;
1693 } else if (common->bad_lun_okay)
1694 sd = SS_NO_SENSE;
1695 else
1696 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1697
1698 if (common->phase_error) {
1699 DBG(common, "sending phase-error status\n");
1700 status = USB_STATUS_PHASE_ERROR;
1701 sd = SS_INVALID_COMMAND;
1702 } else if (sd != SS_NO_SENSE) {
1703 DBG(common, "sending command-failure status\n");
1704 status = USB_STATUS_FAIL;
1705 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
1706 " info x%x\n",
1707 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1708 }
1709
1710 /* Store and send the Bulk-only CSW */
1711 csw = (void *)bh->buf;
1712
1713 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
1714 csw->Tag = common->tag;
1715 csw->Residue = cpu_to_le32(common->residue);
1716 csw->Status = status;
1717
1718 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1719 bh->inreq->zero = 0;
1720 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1721 &bh->inreq_busy, &bh->state)
1722 /* Don't know what to do if common->fsg is NULL */
1723 return -EIO;
1724
1725 common->next_buffhd_to_fill = bh->next;
1726 return 0;
1727}
1728
1729
1730/*-------------------------------------------------------------------------*/
1731
1732/* Check whether the command is properly formed and whether its data size
1733 * and direction agree with the values we already have. */
1734static int check_command(struct fsg_common *common, int cmnd_size,
1735 enum data_direction data_dir, unsigned int mask,
1736 int needs_medium, const char *name)
1737{
1738 int i;
1739 int lun = common->cmnd[1] >> 5;
1740 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1741 char hdlen[20];
1742 struct fsg_lun *curlun;
1743
1744 hdlen[0] = 0;
1745 if (common->data_dir != DATA_DIR_UNKNOWN)
1746 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1747 common->data_size);
1748 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1749 name, cmnd_size, dirletter[(int) data_dir],
1750 common->data_size_from_cmnd, common->cmnd_size, hdlen);
1751
1752 /* We can't reply at all until we know the correct data direction
1753 * and size. */
1754 if (common->data_size_from_cmnd == 0)
1755 data_dir = DATA_DIR_NONE;
1756 if (common->data_size < common->data_size_from_cmnd) {
1757 /* Host data size < Device data size is a phase error.
1758 * Carry out the command, but only transfer as much as
1759 * we are allowed. */
1760 common->data_size_from_cmnd = common->data_size;
1761 common->phase_error = 1;
1762 }
1763 common->residue = common->data_size;
1764 common->usb_amount_left = common->data_size;
1765
1766 /* Conflicting data directions is a phase error */
1767 if (common->data_dir != data_dir
1768 && common->data_size_from_cmnd > 0) {
1769 common->phase_error = 1;
1770 return -EINVAL;
1771 }
1772
1773 /* Verify the length of the command itself */
1774 if (cmnd_size != common->cmnd_size) {
1775
1776 /* Special case workaround: There are plenty of buggy SCSI
1777 * implementations. Many have issues with cbw->Length
1778 * field passing a wrong command size. For those cases we
1779 * always try to work around the problem by using the length
1780 * sent by the host side provided it is at least as large
1781 * as the correct command length.
1782 * Examples of such cases would be MS-Windows, which issues
1783 * REQUEST SENSE with cbw->Length == 12 where it should
1784 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1785 * REQUEST SENSE with cbw->Length == 10 where it should
1786 * be 6 as well.
1787 */
1788 if (cmnd_size <= common->cmnd_size) {
1789 DBG(common, "%s is buggy! Expected length %d "
1790 "but we got %d\n", name,
1791 cmnd_size, common->cmnd_size);
1792 cmnd_size = common->cmnd_size;
1793 } else {
1794 common->phase_error = 1;
1795 return -EINVAL;
1796 }
1797 }
1798
1799 /* Check that the LUN values are consistent */
1800 if (common->lun != lun)
1801 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1802 common->lun, lun);
1803
1804 /* Check the LUN */
1805 if (common->lun >= 0 && common->lun < common->nluns) {
1806 curlun = &common->luns[common->lun];
1807 common->curlun = curlun;
1808 if (common->cmnd[0] != SC_REQUEST_SENSE) {
1809 curlun->sense_data = SS_NO_SENSE;
1810 curlun->sense_data_info = 0;
1811 curlun->info_valid = 0;
1812 }
1813 } else {
1814 common->curlun = NULL;
1815 curlun = NULL;
1816 common->bad_lun_okay = 0;
1817
1818 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1819 * to use unsupported LUNs; all others may not. */
1820 if (common->cmnd[0] != SC_INQUIRY &&
1821 common->cmnd[0] != SC_REQUEST_SENSE) {
1822 DBG(common, "unsupported LUN %d\n", common->lun);
1823 return -EINVAL;
1824 }
1825 }
1826
1827 /* If a unit attention condition exists, only INQUIRY and
1828 * REQUEST SENSE commands are allowed; anything else must fail. */
1829 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1830 common->cmnd[0] != SC_INQUIRY &&
1831 common->cmnd[0] != SC_REQUEST_SENSE) {
1832 curlun->sense_data = curlun->unit_attention_data;
1833 curlun->unit_attention_data = SS_NO_SENSE;
1834 return -EINVAL;
1835 }
1836
1837 /* Check that only command bytes listed in the mask are non-zero */
1838 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
1839 for (i = 1; i < cmnd_size; ++i) {
1840 if (common->cmnd[i] && !(mask & (1 << i))) {
1841 if (curlun)
1842 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1843 return -EINVAL;
1844 }
1845 }
1846
1847 /* If the medium isn't mounted and the command needs to access
1848 * it, return an error. */
1849 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1850 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1851 return -EINVAL;
1852 }
1853
1854 return 0;
1855}
1856
1857
1858static int do_scsi_command(struct fsg_common *common)
1859{
1860 struct fsg_buffhd *bh;
1861 int rc;
1862 int reply = -EINVAL;
1863 int i;
1864 static char unknown[16];
1865
1866 dump_cdb(common);
1867
1868 /* Wait for the next buffer to become available for data or status */
1869 bh = common->next_buffhd_to_fill;
1870 common->next_buffhd_to_drain = bh;
1871 while (bh->state != BUF_STATE_EMPTY) {
1872 rc = sleep_thread(common);
1873 if (rc)
1874 return rc;
1875 }
1876 common->phase_error = 0;
1877 common->short_packet_received = 0;
1878
1879 down_read(&common->filesem); /* We're using the backing file */
1880 switch (common->cmnd[0]) {
1881
1882 case SC_INQUIRY:
1883 common->data_size_from_cmnd = common->cmnd[4];
1884 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1885 (1<<4), 0,
1886 "INQUIRY");
1887 if (reply == 0)
1888 reply = do_inquiry(common, bh);
1889 break;
1890
1891 case SC_MODE_SELECT_6:
1892 common->data_size_from_cmnd = common->cmnd[4];
1893 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1894 (1<<1) | (1<<4), 0,
1895 "MODE SELECT(6)");
1896 if (reply == 0)
1897 reply = do_mode_select(common, bh);
1898 break;
1899
1900 case SC_MODE_SELECT_10:
1901 common->data_size_from_cmnd =
1902 get_unaligned_be16(&common->cmnd[7]);
1903 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
1904 (1<<1) | (3<<7), 0,
1905 "MODE SELECT(10)");
1906 if (reply == 0)
1907 reply = do_mode_select(common, bh);
1908 break;
1909
1910 case SC_MODE_SENSE_6:
1911 common->data_size_from_cmnd = common->cmnd[4];
1912 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1913 (1<<1) | (1<<2) | (1<<4), 0,
1914 "MODE SENSE(6)");
1915 if (reply == 0)
1916 reply = do_mode_sense(common, bh);
1917 break;
1918
1919 case SC_MODE_SENSE_10:
1920 common->data_size_from_cmnd =
1921 get_unaligned_be16(&common->cmnd[7]);
1922 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1923 (1<<1) | (1<<2) | (3<<7), 0,
1924 "MODE SENSE(10)");
1925 if (reply == 0)
1926 reply = do_mode_sense(common, bh);
1927 break;
1928
1929 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1930 common->data_size_from_cmnd = 0;
1931 reply = check_command(common, 6, DATA_DIR_NONE,
1932 (1<<4), 0,
1933 "PREVENT-ALLOW MEDIUM REMOVAL");
1934 if (reply == 0)
1935 reply = do_prevent_allow(common);
1936 break;
1937
1938 case SC_READ_6:
1939 i = common->cmnd[4];
1940 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1941 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1942 (7<<1) | (1<<4), 1,
1943 "READ(6)");
1944 if (reply == 0)
1945 reply = do_read(common);
1946 break;
1947
1948 case SC_READ_10:
1949 common->data_size_from_cmnd =
1950 get_unaligned_be16(&common->cmnd[7]) << 9;
1951 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1952 (1<<1) | (0xf<<2) | (3<<7), 1,
1953 "READ(10)");
1954 if (reply == 0)
1955 reply = do_read(common);
1956 break;
1957
1958 case SC_READ_12:
1959 common->data_size_from_cmnd =
1960 get_unaligned_be32(&common->cmnd[6]) << 9;
1961 reply = check_command(common, 12, DATA_DIR_TO_HOST,
1962 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1963 "READ(12)");
1964 if (reply == 0)
1965 reply = do_read(common);
1966 break;
1967
1968 case SC_READ_CAPACITY:
1969 common->data_size_from_cmnd = 8;
1970 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1971 (0xf<<2) | (1<<8), 1,
1972 "READ CAPACITY");
1973 if (reply == 0)
1974 reply = do_read_capacity(common, bh);
1975 break;
1976
1977 case SC_READ_HEADER:
1978 if (!common->curlun || !common->curlun->cdrom)
1979 goto unknown_cmnd;
1980 common->data_size_from_cmnd =
1981 get_unaligned_be16(&common->cmnd[7]);
1982 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1983 (3<<7) | (0x1f<<1), 1,
1984 "READ HEADER");
1985 if (reply == 0)
1986 reply = do_read_header(common, bh);
1987 break;
1988
1989 case SC_READ_TOC:
1990 if (!common->curlun || !common->curlun->cdrom)
1991 goto unknown_cmnd;
1992 common->data_size_from_cmnd =
1993 get_unaligned_be16(&common->cmnd[7]);
1994 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1995 (7<<6) | (1<<1), 1,
1996 "READ TOC");
1997 if (reply == 0)
1998 reply = do_read_toc(common, bh);
1999 break;
2000
2001 case SC_READ_FORMAT_CAPACITIES:
2002 common->data_size_from_cmnd =
2003 get_unaligned_be16(&common->cmnd[7]);
2004 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2005 (3<<7), 1,
2006 "READ FORMAT CAPACITIES");
2007 if (reply == 0)
2008 reply = do_read_format_capacities(common, bh);
2009 break;
2010
2011 case SC_REQUEST_SENSE:
2012 common->data_size_from_cmnd = common->cmnd[4];
2013 reply = check_command(common, 6, DATA_DIR_TO_HOST,
2014 (1<<4), 0,
2015 "REQUEST SENSE");
2016 if (reply == 0)
2017 reply = do_request_sense(common, bh);
2018 break;
2019
2020 case SC_START_STOP_UNIT:
2021 common->data_size_from_cmnd = 0;
2022 reply = check_command(common, 6, DATA_DIR_NONE,
2023 (1<<1) | (1<<4), 0,
2024 "START-STOP UNIT");
2025 if (reply == 0)
2026 reply = do_start_stop(common);
2027 break;
2028
2029 case SC_SYNCHRONIZE_CACHE:
2030 common->data_size_from_cmnd = 0;
2031 reply = check_command(common, 10, DATA_DIR_NONE,
2032 (0xf<<2) | (3<<7), 1,
2033 "SYNCHRONIZE CACHE");
2034 if (reply == 0)
2035 reply = do_synchronize_cache(common);
2036 break;
2037
2038 case SC_TEST_UNIT_READY:
2039 common->data_size_from_cmnd = 0;
2040 reply = check_command(common, 6, DATA_DIR_NONE,
2041 0, 1,
2042 "TEST UNIT READY");
2043 break;
2044
2045 /* Although optional, this command is used by MS-Windows. We
2046 * support a minimal version: BytChk must be 0. */
2047 case SC_VERIFY:
2048 common->data_size_from_cmnd = 0;
2049 reply = check_command(common, 10, DATA_DIR_NONE,
2050 (1<<1) | (0xf<<2) | (3<<7), 1,
2051 "VERIFY");
2052 if (reply == 0)
2053 reply = do_verify(common);
2054 break;
2055
2056 case SC_WRITE_6:
2057 i = common->cmnd[4];
2058 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2059 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
2060 (7<<1) | (1<<4), 1,
2061 "WRITE(6)");
2062 if (reply == 0)
2063 reply = do_write(common);
2064 break;
2065
2066 case SC_WRITE_10:
2067 common->data_size_from_cmnd =
2068 get_unaligned_be16(&common->cmnd[7]) << 9;
2069 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
2070 (1<<1) | (0xf<<2) | (3<<7), 1,
2071 "WRITE(10)");
2072 if (reply == 0)
2073 reply = do_write(common);
2074 break;
2075
2076 case SC_WRITE_12:
2077 common->data_size_from_cmnd =
2078 get_unaligned_be32(&common->cmnd[6]) << 9;
2079 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
2080 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2081 "WRITE(12)");
2082 if (reply == 0)
2083 reply = do_write(common);
2084 break;
2085
2086 /* Some mandatory commands that we recognize but don't implement.
2087 * They don't mean much in this setting. It's left as an exercise
2088 * for anyone interested to implement RESERVE and RELEASE in terms
2089 * of Posix locks. */
2090 case SC_FORMAT_UNIT:
2091 case SC_RELEASE:
2092 case SC_RESERVE:
2093 case SC_SEND_DIAGNOSTIC:
2094 /* Fall through */
2095
2096 default:
2097unknown_cmnd:
2098 common->data_size_from_cmnd = 0;
2099 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2100 reply = check_command(common, common->cmnd_size,
2101 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2102 if (reply == 0) {
2103 common->curlun->sense_data = SS_INVALID_COMMAND;
2104 reply = -EINVAL;
2105 }
2106 break;
2107 }
2108 up_read(&common->filesem);
2109
2110 if (reply == -EINTR || signal_pending(current))
2111 return -EINTR;
2112
2113 /* Set up the single reply buffer for finish_reply() */
2114 if (reply == -EINVAL)
2115 reply = 0; /* Error reply length */
2116 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2117 reply = min((u32) reply, common->data_size_from_cmnd);
2118 bh->inreq->length = reply;
2119 bh->state = BUF_STATE_FULL;
2120 common->residue -= reply;
2121 } /* Otherwise it's already set */
2122
2123 return 0;
2124}
2125
2126
2127/*-------------------------------------------------------------------------*/
2128
2129static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2130{
2131 struct usb_request *req = bh->outreq;
2132 struct fsg_bulk_cb_wrap *cbw = req->buf;
2133 struct fsg_common *common = fsg->common;
2134
2135 /* Was this a real packet? Should it be ignored? */
2136 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2137 return -EINVAL;
2138
2139 /* Is the CBW valid? */
2140 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2141 cbw->Signature != cpu_to_le32(
2142 USB_BULK_CB_SIG)) {
2143 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2144 req->actual,
2145 le32_to_cpu(cbw->Signature));
2146
2147 /* The Bulk-only spec says we MUST stall the IN endpoint
2148 * (6.6.1), so it's unavoidable. It also says we must
2149 * retain this state until the next reset, but there's
2150 * no way to tell the controller driver it should ignore
2151 * Clear-Feature(HALT) requests.
2152 *
2153 * We aren't required to halt the OUT endpoint; instead
2154 * we can simply accept and discard any data received
2155 * until the next reset. */
2156 wedge_bulk_in_endpoint(fsg);
2157 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2158 return -EINVAL;
2159 }
2160
2161 /* Is the CBW meaningful? */
2162 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2163 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2164 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2165 "cmdlen %u\n",
2166 cbw->Lun, cbw->Flags, cbw->Length);
2167
2168 /* We can do anything we want here, so let's stall the
2169 * bulk pipes if we are allowed to. */
2170 if (common->can_stall) {
2171 fsg_set_halt(fsg, fsg->bulk_out);
2172 halt_bulk_in_endpoint(fsg);
2173 }
2174 return -EINVAL;
2175 }
2176
2177 /* Save the command for later */
2178 common->cmnd_size = cbw->Length;
2179 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
2180 if (cbw->Flags & USB_BULK_IN_FLAG)
2181 common->data_dir = DATA_DIR_TO_HOST;
2182 else
2183 common->data_dir = DATA_DIR_FROM_HOST;
2184 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2185 if (common->data_size == 0)
2186 common->data_dir = DATA_DIR_NONE;
2187 common->lun = cbw->Lun;
2188 common->tag = cbw->Tag;
2189 return 0;
2190}
2191
2192
2193static int get_next_command(struct fsg_common *common)
2194{
2195 struct fsg_buffhd *bh;
2196 int rc = 0;
2197
2198 /* Wait for the next buffer to become available */
2199 bh = common->next_buffhd_to_fill;
2200 while (bh->state != BUF_STATE_EMPTY) {
2201 rc = sleep_thread(common);
2202 if (rc)
2203 return rc;
2204 }
2205
2206 /* Queue a request to read a Bulk-only CBW */
2207 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2208 bh->outreq->short_not_ok = 1;
2209 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2210 &bh->outreq_busy, &bh->state)
2211 /* Don't know what to do if common->fsg is NULL */
2212 return -EIO;
2213
2214 /* We will drain the buffer in software, which means we
2215 * can reuse it for the next filling. No need to advance
2216 * next_buffhd_to_fill. */
2217
2218 /* Wait for the CBW to arrive */
2219 while (bh->state != BUF_STATE_FULL) {
2220 rc = sleep_thread(common);
2221 if (rc)
2222 return rc;
2223 }
2224 smp_rmb();
2225 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
2226 bh->state = BUF_STATE_EMPTY;
2227
2228 return rc;
2229}
2230
2231
2232/*-------------------------------------------------------------------------*/
2233
2234static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
2235 const struct usb_endpoint_descriptor *d)
2236{
2237 int rc;
2238
2239 ep->driver_data = common;
2240 rc = usb_ep_enable(ep, d);
2241 if (rc)
2242 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
2243 return rc;
2244}
2245
2246static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
2247 struct usb_request **preq)
2248{
2249 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2250 if (*preq)
2251 return 0;
2252 ERROR(common, "can't allocate request for %s\n", ep->name);
2253 return -ENOMEM;
2254}
2255
2256/*
2257 * Reset interface setting and re-init endpoint state (toggle etc).
2258 * Call with altsetting < 0 to disable the interface. The only other
2259 * available altsetting is 0, which enables the interface.
2260 */
2261static int do_set_interface(struct fsg_common *common, int altsetting)
2262{
2263 int rc = 0;
2264 int i;
2265 const struct usb_endpoint_descriptor *d;
2266
2267 if (common->running)
2268 DBG(common, "reset interface\n");
2269
2270reset:
2271 /* Deallocate the requests */
2272 if (common->prev_fsg) {
2273 struct fsg_dev *fsg = common->prev_fsg;
2274
2275 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2276 struct fsg_buffhd *bh = &common->buffhds[i];
2277
2278 if (bh->inreq) {
2279 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2280 bh->inreq = NULL;
2281 }
2282 if (bh->outreq) {
2283 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2284 bh->outreq = NULL;
2285 }
2286 }
2287
2288 /* Disable the endpoints */
2289 if (fsg->bulk_in_enabled) {
2290 usb_ep_disable(fsg->bulk_in);
2291 fsg->bulk_in_enabled = 0;
2292 }
2293 if (fsg->bulk_out_enabled) {
2294 usb_ep_disable(fsg->bulk_out);
2295 fsg->bulk_out_enabled = 0;
2296 }
2297
2298 common->prev_fsg = 0;
2299 }
2300
2301 common->running = 0;
2302 if (altsetting < 0 || rc != 0)
2303 return rc;
2304
2305 DBG(common, "set interface %d\n", altsetting);
2306
2307 if (fsg_is_set(common)) {
2308 struct fsg_dev *fsg = common->fsg;
2309 common->prev_fsg = common->fsg;
2310
2311 /* Enable the endpoints */
2312 d = fsg_ep_desc(common->gadget,
2313 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2314 rc = enable_endpoint(common, fsg->bulk_in, d);
2315 if (rc)
2316 goto reset;
2317 fsg->bulk_in_enabled = 1;
2318
2319 d = fsg_ep_desc(common->gadget,
2320 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2321 rc = enable_endpoint(common, fsg->bulk_out, d);
2322 if (rc)
2323 goto reset;
2324 fsg->bulk_out_enabled = 1;
2325 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2326 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2327
2328 /* Allocate the requests */
2329 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2330 struct fsg_buffhd *bh = &common->buffhds[i];
2331
2332 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2333 if (rc)
2334 goto reset;
2335 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2336 if (rc)
2337 goto reset;
2338 bh->inreq->buf = bh->outreq->buf = bh->buf;
2339 bh->inreq->context = bh->outreq->context = bh;
2340 bh->inreq->complete = bulk_in_complete;
2341 bh->outreq->complete = bulk_out_complete;
2342 }
2343
2344 common->running = 1;
2345 for (i = 0; i < common->nluns; ++i)
2346 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2347 return rc;
2348 } else {
2349 return -EIO;
2350 }
2351}
2352
2353
2354/*
2355 * Change our operational configuration. This code must agree with the code
2356 * that returns config descriptors, and with interface altsetting code.
2357 *
2358 * It's also responsible for power management interactions. Some
2359 * configurations might not work with our current power sources.
2360 * For now we just assume the gadget is always self-powered.
2361 */
2362static int do_set_config(struct fsg_common *common, u8 new_config)
2363{
2364 int rc = 0;
2365
2366 /* Disable the single interface */
2367 if (common->config != 0) {
2368 DBG(common, "reset config\n");
2369 common->config = 0;
2370 rc = do_set_interface(common, -1);
2371 }
2372
2373 /* Enable the interface */
2374 if (new_config != 0) {
2375 common->config = new_config;
2376 rc = do_set_interface(common, 0);
2377 if (rc != 0)
2378 common->config = 0; /* Reset on errors */
2379 }
2380 return rc;
2381}
2382
2383
2384/****************************** ALT CONFIGS ******************************/
2385
2386
2387static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2388{
2389 struct fsg_dev *fsg = fsg_from_func(f);
2390 fsg->common->prev_fsg = fsg->common->fsg;
2391 fsg->common->fsg = fsg;
2392 fsg->common->new_config = 1;
2393 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2394 return 0;
2395}
2396
2397static void fsg_disable(struct usb_function *f)
2398{
2399 struct fsg_dev *fsg = fsg_from_func(f);
2400 fsg->common->prev_fsg = fsg->common->fsg;
2401 fsg->common->fsg = fsg;
2402 fsg->common->new_config = 0;
2403 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2404}
2405
2406
2407/*-------------------------------------------------------------------------*/
2408
2409static void handle_exception(struct fsg_common *common)
2410{
2411 siginfo_t info;
2412 int sig;
2413 int i;
2414 struct fsg_buffhd *bh;
2415 enum fsg_state old_state;
2416 u8 new_config;
2417 struct fsg_lun *curlun;
2418 unsigned int exception_req_tag;
2419 int rc;
2420
2421 /* Clear the existing signals. Anything but SIGUSR1 is converted
2422 * into a high-priority EXIT exception. */
2423 for (;;) {
2424 sig = dequeue_signal_lock(current, &current->blocked, &info);
2425 if (!sig)
2426 break;
2427 if (sig != SIGUSR1) {
2428 if (common->state < FSG_STATE_EXIT)
2429 DBG(common, "Main thread exiting on signal\n");
2430 raise_exception(common, FSG_STATE_EXIT);
2431 }
2432 }
2433
2434 /* Cancel all the pending transfers */
2435 if (fsg_is_set(common)) {
2436 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2437 bh = &common->buffhds[i];
2438 if (bh->inreq_busy)
2439 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2440 if (bh->outreq_busy)
2441 usb_ep_dequeue(common->fsg->bulk_out,
2442 bh->outreq);
2443 }
2444
2445 /* Wait until everything is idle */
2446 for (;;) {
2447 int num_active = 0;
2448 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2449 bh = &common->buffhds[i];
2450 num_active += bh->inreq_busy + bh->outreq_busy;
2451 }
2452 if (num_active == 0)
2453 break;
2454 if (sleep_thread(common))
2455 return;
2456 }
2457
2458 /* Clear out the controller's fifos */
2459 if (common->fsg->bulk_in_enabled)
2460 usb_ep_fifo_flush(common->fsg->bulk_in);
2461 if (common->fsg->bulk_out_enabled)
2462 usb_ep_fifo_flush(common->fsg->bulk_out);
2463 }
2464
2465 /* Reset the I/O buffer states and pointers, the SCSI
2466 * state, and the exception. Then invoke the handler. */
2467 spin_lock_irq(&common->lock);
2468
2469 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2470 bh = &common->buffhds[i];
2471 bh->state = BUF_STATE_EMPTY;
2472 }
2473 common->next_buffhd_to_fill = &common->buffhds[0];
2474 common->next_buffhd_to_drain = &common->buffhds[0];
2475 exception_req_tag = common->exception_req_tag;
2476 new_config = common->new_config;
2477 old_state = common->state;
2478
2479 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2480 common->state = FSG_STATE_STATUS_PHASE;
2481 else {
2482 for (i = 0; i < common->nluns; ++i) {
2483 curlun = &common->luns[i];
2484 curlun->prevent_medium_removal = 0;
2485 curlun->sense_data = SS_NO_SENSE;
2486 curlun->unit_attention_data = SS_NO_SENSE;
2487 curlun->sense_data_info = 0;
2488 curlun->info_valid = 0;
2489 }
2490 common->state = FSG_STATE_IDLE;
2491 }
2492 spin_unlock_irq(&common->lock);
2493
2494 /* Carry out any extra actions required for the exception */
2495 switch (old_state) {
2496 case FSG_STATE_ABORT_BULK_OUT:
2497 send_status(common);
2498 spin_lock_irq(&common->lock);
2499 if (common->state == FSG_STATE_STATUS_PHASE)
2500 common->state = FSG_STATE_IDLE;
2501 spin_unlock_irq(&common->lock);
2502 break;
2503
2504 case FSG_STATE_RESET:
2505 /* In case we were forced against our will to halt a
2506 * bulk endpoint, clear the halt now. (The SuperH UDC
2507 * requires this.) */
2508 if (!fsg_is_set(common))
2509 break;
2510 if (test_and_clear_bit(IGNORE_BULK_OUT,
2511 &common->fsg->atomic_bitflags))
2512 usb_ep_clear_halt(common->fsg->bulk_in);
2513
2514 if (common->ep0_req_tag == exception_req_tag)
2515 ep0_queue(common); /* Complete the status stage */
2516
2517 /* Technically this should go here, but it would only be
2518 * a waste of time. Ditto for the INTERFACE_CHANGE and
2519 * CONFIG_CHANGE cases. */
2520 /* for (i = 0; i < common->nluns; ++i) */
2521 /* common->luns[i].unit_attention_data = */
2522 /* SS_RESET_OCCURRED; */
2523 break;
2524
2525 case FSG_STATE_CONFIG_CHANGE:
2526 rc = do_set_config(common, new_config);
2527 if (common->ep0_req_tag != exception_req_tag)
2528 break;
2529 if (rc != 0) { /* STALL on errors */
2530 DBG(common, "ep0 set halt\n");
2531 usb_ep_set_halt(common->ep0);
2532 } else { /* Complete the status stage */
2533 ep0_queue(common);
2534 }
2535 break;
2536
2537 case FSG_STATE_EXIT:
2538 case FSG_STATE_TERMINATED:
2539 do_set_config(common, 0); /* Free resources */
2540 spin_lock_irq(&common->lock);
2541 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2542 spin_unlock_irq(&common->lock);
2543 break;
2544
2545 case FSG_STATE_INTERFACE_CHANGE:
2546 case FSG_STATE_DISCONNECT:
2547 case FSG_STATE_COMMAND_PHASE:
2548 case FSG_STATE_DATA_PHASE:
2549 case FSG_STATE_STATUS_PHASE:
2550 case FSG_STATE_IDLE:
2551 break;
2552 }
2553}
2554
2555
2556/*-------------------------------------------------------------------------*/
2557
2558static int fsg_main_thread(void *common_)
2559{
2560 struct fsg_common *common = common_;
2561
2562 /* Allow the thread to be killed by a signal, but set the signal mask
2563 * to block everything but INT, TERM, KILL, and USR1. */
2564 allow_signal(SIGINT);
2565 allow_signal(SIGTERM);
2566 allow_signal(SIGKILL);
2567 allow_signal(SIGUSR1);
2568
2569 /* Allow the thread to be frozen */
2570 set_freezable();
2571
2572 /* Arrange for userspace references to be interpreted as kernel
2573 * pointers. That way we can pass a kernel pointer to a routine
2574 * that expects a __user pointer and it will work okay. */
2575 set_fs(get_ds());
2576
2577 /* The main loop */
2578 while (common->state != FSG_STATE_TERMINATED) {
2579 if (exception_in_progress(common) || signal_pending(current)) {
2580 handle_exception(common);
2581 continue;
2582 }
2583
2584 if (!common->running) {
2585 sleep_thread(common);
2586 continue;
2587 }
2588
2589 if (get_next_command(common))
2590 continue;
2591
2592 spin_lock_irq(&common->lock);
2593 if (!exception_in_progress(common))
2594 common->state = FSG_STATE_DATA_PHASE;
2595 spin_unlock_irq(&common->lock);
2596
2597 if (do_scsi_command(common) || finish_reply(common))
2598 continue;
2599
2600 spin_lock_irq(&common->lock);
2601 if (!exception_in_progress(common))
2602 common->state = FSG_STATE_STATUS_PHASE;
2603 spin_unlock_irq(&common->lock);
2604
2605 if (send_status(common))
2606 continue;
2607
2608 spin_lock_irq(&common->lock);
2609 if (!exception_in_progress(common))
2610 common->state = FSG_STATE_IDLE;
2611 spin_unlock_irq(&common->lock);
2612 }
2613
2614 spin_lock_irq(&common->lock);
2615 common->thread_task = NULL;
2616 spin_unlock_irq(&common->lock);
2617
2618 if (common->thread_exits)
2619 common->thread_exits(common);
2620
2621 /* Let the unbind and cleanup routines know the thread has exited */
2622 complete_and_exit(&common->thread_notifier, 0);
2623}
2624
2625
2626/*************************** DEVICE ATTRIBUTES ***************************/
2627
2628/* Write permission is checked per LUN in store_*() functions. */
2629static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2630static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
2631
2632
2633/****************************** FSG COMMON ******************************/
2634
2635static void fsg_common_release(struct kref *ref);
2636
2637static void fsg_lun_release(struct device *dev)
2638{
2639 /* Nothing needs to be done */
2640}
2641
2642static inline void fsg_common_get(struct fsg_common *common)
2643{
2644 kref_get(&common->ref);
2645}
2646
2647static inline void fsg_common_put(struct fsg_common *common)
2648{
2649 kref_put(&common->ref, fsg_common_release);
2650}
2651
2652
2653static struct fsg_common *fsg_common_init(struct fsg_common *common,
2654 struct usb_composite_dev *cdev,
2655 struct fsg_config *cfg)
2656{
2657 struct usb_gadget *gadget = cdev->gadget;
2658 struct fsg_buffhd *bh;
2659 struct fsg_lun *curlun;
2660 struct fsg_lun_config *lcfg;
2661 int nluns, i, rc;
2662 char *pathbuf;
2663
2664 /* Find out how many LUNs there should be */
2665 nluns = cfg->nluns;
2666 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2667 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2668 return ERR_PTR(-EINVAL);
2669 }
2670
2671 /* Allocate? */
2672 if (!common) {
2673 common = kzalloc(sizeof *common, GFP_KERNEL);
2674 if (!common)
2675 return ERR_PTR(-ENOMEM);
2676 common->free_storage_on_release = 1;
2677 } else {
2678 memset(common, 0, sizeof common);
2679 common->free_storage_on_release = 0;
2680 }
2681
2682 common->private_data = cfg->private_data;
2683
2684 common->gadget = gadget;
2685 common->ep0 = gadget->ep0;
2686 common->ep0req = cdev->req;
2687
2688 /* Maybe allocate device-global string IDs, and patch descriptors */
2689 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2690 rc = usb_string_id(cdev);
2691 if (rc < 0) {
2692 kfree(common);
2693 return ERR_PTR(rc);
2694 }
2695 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2696 fsg_intf_desc.iInterface = rc;
2697 }
2698
2699 /* Create the LUNs, open their backing files, and register the
2700 * LUN devices in sysfs. */
2701 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2702 if (!curlun) {
2703 kfree(common);
2704 return ERR_PTR(-ENOMEM);
2705 }
2706 common->luns = curlun;
2707
2708 init_rwsem(&common->filesem);
2709
2710 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2711 curlun->cdrom = !!lcfg->cdrom;
2712 curlun->ro = lcfg->cdrom || lcfg->ro;
2713 curlun->removable = lcfg->removable;
2714 curlun->dev.release = fsg_lun_release;
2715 curlun->dev.parent = &gadget->dev;
2716 /* curlun->dev.driver = &fsg_driver.driver; XXX */
2717 dev_set_drvdata(&curlun->dev, &common->filesem);
2718 dev_set_name(&curlun->dev,
2719 cfg->lun_name_format
2720 ? cfg->lun_name_format
2721 : "lun%d",
2722 i);
2723
2724 rc = device_register(&curlun->dev);
2725 if (rc) {
2726 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2727 common->nluns = i;
2728 goto error_release;
2729 }
2730
2731 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2732 if (rc)
2733 goto error_luns;
2734 rc = device_create_file(&curlun->dev, &dev_attr_file);
2735 if (rc)
2736 goto error_luns;
2737
2738 if (lcfg->filename) {
2739 rc = fsg_lun_open(curlun, lcfg->filename);
2740 if (rc)
2741 goto error_luns;
2742 } else if (!curlun->removable) {
2743 ERROR(common, "no file given for LUN%d\n", i);
2744 rc = -EINVAL;
2745 goto error_luns;
2746 }
2747 }
2748 common->nluns = nluns;
2749
2750
2751 /* Data buffers cyclic list */
2752 /* Buffers in buffhds are static -- no need for additional
2753 * allocation. */
2754 bh = common->buffhds;
2755 i = FSG_NUM_BUFFERS - 1;
2756 do {
2757 bh->next = bh + 1;
2758 } while (++bh, --i);
2759 bh->next = common->buffhds;
2760
2761
2762 /* Prepare inquiryString */
2763 if (cfg->release != 0xffff) {
2764 i = cfg->release;
2765 } else {
2766 /* The sa1100 controller is not supported */
2767 i = gadget_is_sa1100(gadget)
2768 ? -1
2769 : usb_gadget_controller_number(gadget);
2770 if (i >= 0) {
2771 i = 0x0300 + i;
2772 } else {
2773 WARNING(common, "controller '%s' not recognized\n",
2774 gadget->name);
2775 i = 0x0399;
2776 }
2777 }
2778#define OR(x, y) ((x) ? (x) : (y))
2779 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2780 "%-8s%-16s%04x",
2781 OR(cfg->vendor_name, "Linux "),
2782 /* Assume product name dependent on the first LUN */
2783 OR(cfg->product_name, common->luns->cdrom
2784 ? "File-Stor Gadget"
2785 : "File-CD Gadget "),
2786 i);
2787
2788
2789 /* Some peripheral controllers are known not to be able to
2790 * halt bulk endpoints correctly. If one of them is present,
2791 * disable stalls.
2792 */
2793 common->can_stall = cfg->can_stall &&
2794 !(gadget_is_sh(common->gadget) ||
2795 gadget_is_at91(common->gadget));
2796
2797
2798 spin_lock_init(&common->lock);
2799 kref_init(&common->ref);
2800
2801
2802 /* Tell the thread to start working */
2803 common->thread_exits = cfg->thread_exits;
2804 common->thread_task =
2805 kthread_create(fsg_main_thread, common,
2806 OR(cfg->thread_name, "file-storage"));
2807 if (IS_ERR(common->thread_task)) {
2808 rc = PTR_ERR(common->thread_task);
2809 goto error_release;
2810 }
2811 init_completion(&common->thread_notifier);
2812#undef OR
2813
2814
2815 /* Information */
2816 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2817 INFO(common, "Number of LUNs=%d\n", common->nluns);
2818
2819 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2820 for (i = 0, nluns = common->nluns, curlun = common->luns;
2821 i < nluns;
2822 ++curlun, ++i) {
2823 char *p = "(no medium)";
2824 if (fsg_lun_is_open(curlun)) {
2825 p = "(error)";
2826 if (pathbuf) {
2827 p = d_path(&curlun->filp->f_path,
2828 pathbuf, PATH_MAX);
2829 if (IS_ERR(p))
2830 p = "(error)";
2831 }
2832 }
2833 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2834 curlun->removable ? "removable " : "",
2835 curlun->ro ? "read only " : "",
2836 curlun->cdrom ? "CD-ROM " : "",
2837 p);
2838 }
2839 kfree(pathbuf);
2840
2841 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2842
2843 wake_up_process(common->thread_task);
2844
2845 return common;
2846
2847
2848error_luns:
2849 common->nluns = i + 1;
2850error_release:
2851 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
2852 /* Call fsg_common_release() directly, ref might be not
2853 * initialised */
2854 fsg_common_release(&common->ref);
2855 complete(&common->thread_notifier);
2856 return ERR_PTR(rc);
2857}
2858
2859
2860static void fsg_common_release(struct kref *ref)
2861{
2862 struct fsg_common *common =
2863 container_of(ref, struct fsg_common, ref);
2864 unsigned i = common->nluns;
2865 struct fsg_lun *lun = common->luns;
2866
2867 /* If the thread isn't already dead, tell it to exit now */
2868 if (common->state != FSG_STATE_TERMINATED) {
2869 raise_exception(common, FSG_STATE_EXIT);
2870 wait_for_completion(&common->thread_notifier);
2871
2872 /* The cleanup routine waits for this completion also */
2873 complete(&common->thread_notifier);
2874 }
2875
2876 /* Beware tempting for -> do-while optimization: when in error
2877 * recovery nluns may be zero. */
2878
2879 for (; i; --i, ++lun) {
2880 device_remove_file(&lun->dev, &dev_attr_ro);
2881 device_remove_file(&lun->dev, &dev_attr_file);
2882 fsg_lun_close(lun);
2883 device_unregister(&lun->dev);
2884 }
2885
2886 kfree(common->luns);
2887 if (common->free_storage_on_release)
2888 kfree(common);
2889}
2890
2891
2892/*-------------------------------------------------------------------------*/
2893
2894
2895static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2896{
2897 struct fsg_dev *fsg = fsg_from_func(f);
2898
2899 DBG(fsg, "unbind\n");
2900 fsg_common_put(fsg->common);
2901 kfree(fsg);
2902}
2903
2904
2905static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2906{
2907 struct fsg_dev *fsg = fsg_from_func(f);
2908 struct usb_gadget *gadget = c->cdev->gadget;
2909 int rc;
2910 int i;
2911 struct usb_ep *ep;
2912
2913 fsg->gadget = gadget;
2914
2915 /* New interface */
2916 i = usb_interface_id(c, f);
2917 if (i < 0)
2918 return i;
2919 fsg_intf_desc.bInterfaceNumber = i;
2920 fsg->interface_number = i;
2921
2922 /* Find all the endpoints we will use */
2923 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2924 if (!ep)
2925 goto autoconf_fail;
2926 ep->driver_data = fsg->common; /* claim the endpoint */
2927 fsg->bulk_in = ep;
2928
2929 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2930 if (!ep)
2931 goto autoconf_fail;
2932 ep->driver_data = fsg->common; /* claim the endpoint */
2933 fsg->bulk_out = ep;
2934
2935 if (gadget_is_dualspeed(gadget)) {
2936 /* Assume endpoint addresses are the same for both speeds */
2937 fsg_hs_bulk_in_desc.bEndpointAddress =
2938 fsg_fs_bulk_in_desc.bEndpointAddress;
2939 fsg_hs_bulk_out_desc.bEndpointAddress =
2940 fsg_fs_bulk_out_desc.bEndpointAddress;
2941 f->hs_descriptors = fsg_hs_function;
2942 }
2943
2944 return 0;
2945
2946autoconf_fail:
2947 ERROR(fsg, "unable to autoconfigure all endpoints\n");
2948 rc = -ENOTSUPP;
2949 fsg_unbind(c, f);
2950 return rc;
2951}
2952
2953
2954/****************************** ADD FUNCTION ******************************/
2955
2956static struct usb_gadget_strings *fsg_strings_array[] = {
2957 &fsg_stringtab,
2958 NULL,
2959};
2960
2961static int fsg_add(struct usb_composite_dev *cdev,
2962 struct usb_configuration *c,
2963 struct fsg_common *common)
2964{
2965 struct fsg_dev *fsg;
2966 int rc;
2967
2968 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
2969 if (unlikely(!fsg))
2970 return -ENOMEM;
2971
2972 fsg->function.name = FSG_DRIVER_DESC;
2973 fsg->function.strings = fsg_strings_array;
2974 fsg->function.descriptors = fsg_fs_function;
2975 fsg->function.bind = fsg_bind;
2976 fsg->function.unbind = fsg_unbind;
2977 fsg->function.setup = fsg_setup;
2978 fsg->function.set_alt = fsg_set_alt;
2979 fsg->function.disable = fsg_disable;
2980
2981 fsg->common = common;
2982 /* Our caller holds a reference to common structure so we
2983 * don't have to be worry about it being freed until we return
2984 * from this function. So instead of incrementing counter now
2985 * and decrement in error recovery we increment it only when
2986 * call to usb_add_function() was successful. */
2987
2988 rc = usb_add_function(c, &fsg->function);
2989
2990 if (likely(rc == 0))
2991 fsg_common_get(fsg->common);
2992 else
2993 kfree(fsg);
2994
2995 return rc;
2996}
2997
2998
2999
3000/************************* Module parameters *************************/
3001
3002
3003struct fsg_module_parameters {
3004 char *file[FSG_MAX_LUNS];
3005 int ro[FSG_MAX_LUNS];
3006 int removable[FSG_MAX_LUNS];
3007 int cdrom[FSG_MAX_LUNS];
3008
3009 unsigned int file_count, ro_count, removable_count, cdrom_count;
3010 unsigned int luns; /* nluns */
3011 int stall; /* can_stall */
3012};
3013
3014
3015#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3016 module_param_array_named(prefix ## name, params.name, type, \
3017 &prefix ## params.name ## _count, \
3018 S_IRUGO); \
3019 MODULE_PARM_DESC(prefix ## name, desc)
3020
3021#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3022 module_param_named(prefix ## name, params.name, type, \
3023 S_IRUGO); \
3024 MODULE_PARM_DESC(prefix ## name, desc)
3025
3026#define FSG_MODULE_PARAMETERS(prefix, params) \
3027 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3028 "names of backing files or devices"); \
3029 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3030 "true to force read-only"); \
3031 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3032 "true to simulate removable media"); \
3033 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3034 "true to simulate CD-ROM instead of disk"); \
3035 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3036 "number of LUNs"); \
3037 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3038 "false to prevent bulk stalls")
3039
3040
3041static void
3042fsg_config_from_params(struct fsg_config *cfg,
3043 const struct fsg_module_parameters *params)
3044{
3045 struct fsg_lun_config *lun;
3046 unsigned i;
3047
3048 /* Configure LUNs */
3049 cfg->nluns =
3050 min(params->luns ?: (params->file_count ?: 1u),
3051 (unsigned)FSG_MAX_LUNS);
3052 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
3053 lun->ro = !!params->ro[i];
3054 lun->cdrom = !!params->cdrom[i];
3055 lun->removable = /* Removable by default */
3056 params->removable_count <= i || params->removable[i];
3057 lun->filename =
3058 params->file_count > i && params->file[i][0]
3059 ? params->file[i]
3060 : 0;
3061 }
3062
3063 /* Let MSF use defaults */
3064 cfg->lun_name_format = 0;
3065 cfg->thread_name = 0;
3066 cfg->vendor_name = 0;
3067 cfg->product_name = 0;
3068 cfg->release = 0xffff;
3069
3070 cfg->thread_exits = 0;
3071 cfg->private_data = 0;
3072
3073 /* Finalise */
3074 cfg->can_stall = params->stall;
3075}
3076
3077static inline struct fsg_common *
3078fsg_common_from_params(struct fsg_common *common,
3079 struct usb_composite_dev *cdev,
3080 const struct fsg_module_parameters *params)
3081 __attribute__((unused));
3082static inline struct fsg_common *
3083fsg_common_from_params(struct fsg_common *common,
3084 struct usb_composite_dev *cdev,
3085 const struct fsg_module_parameters *params)
3086{
3087 struct fsg_config cfg;
3088 fsg_config_from_params(&cfg, params);
3089 return fsg_common_init(common, cdev, &cfg);
3090}
3091
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index c9966cc07d3a..95dae4c1ea40 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -4,6 +4,8 @@
4 * Copyright (C) 2003-2005,2008 David Brownell 4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger 5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation 6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
7 * 9 *
8 * This program is free software; you can redistribute it and/or modify 10 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 11 * it under the terms of the GNU General Public License as published by
@@ -149,8 +151,8 @@ static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
149 .bDataInterface = 0x01, 151 .bDataInterface = 0x01,
150}; 152};
151 153
152static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { 154static struct usb_cdc_acm_descriptor rndis_acm_descriptor __initdata = {
153 .bLength = sizeof acm_descriptor, 155 .bLength = sizeof rndis_acm_descriptor,
154 .bDescriptorType = USB_DT_CS_INTERFACE, 156 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_ACM_TYPE, 157 .bDescriptorSubType = USB_CDC_ACM_TYPE,
156 158
@@ -179,6 +181,20 @@ static struct usb_interface_descriptor rndis_data_intf __initdata = {
179 /* .iInterface = DYNAMIC */ 181 /* .iInterface = DYNAMIC */
180}; 182};
181 183
184
185static struct usb_interface_assoc_descriptor
186rndis_iad_descriptor = {
187 .bLength = sizeof rndis_iad_descriptor,
188 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
189
190 .bFirstInterface = 0, /* XXX, hardcoded */
191 .bInterfaceCount = 2, // control + data
192 .bFunctionClass = USB_CLASS_COMM,
193 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
194 .bFunctionProtocol = USB_CDC_PROTO_NONE,
195 /* .iFunction = DYNAMIC */
196};
197
182/* full speed support: */ 198/* full speed support: */
183 199
184static struct usb_endpoint_descriptor fs_notify_desc __initdata = { 200static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
@@ -208,11 +224,12 @@ static struct usb_endpoint_descriptor fs_out_desc __initdata = {
208}; 224};
209 225
210static struct usb_descriptor_header *eth_fs_function[] __initdata = { 226static struct usb_descriptor_header *eth_fs_function[] __initdata = {
227 (struct usb_descriptor_header *) &rndis_iad_descriptor,
211 /* control interface matches ACM, not Ethernet */ 228 /* control interface matches ACM, not Ethernet */
212 (struct usb_descriptor_header *) &rndis_control_intf, 229 (struct usb_descriptor_header *) &rndis_control_intf,
213 (struct usb_descriptor_header *) &header_desc, 230 (struct usb_descriptor_header *) &header_desc,
214 (struct usb_descriptor_header *) &call_mgmt_descriptor, 231 (struct usb_descriptor_header *) &call_mgmt_descriptor,
215 (struct usb_descriptor_header *) &acm_descriptor, 232 (struct usb_descriptor_header *) &rndis_acm_descriptor,
216 (struct usb_descriptor_header *) &rndis_union_desc, 233 (struct usb_descriptor_header *) &rndis_union_desc,
217 (struct usb_descriptor_header *) &fs_notify_desc, 234 (struct usb_descriptor_header *) &fs_notify_desc,
218 /* data interface has no altsetting */ 235 /* data interface has no altsetting */
@@ -252,11 +269,12 @@ static struct usb_endpoint_descriptor hs_out_desc __initdata = {
252}; 269};
253 270
254static struct usb_descriptor_header *eth_hs_function[] __initdata = { 271static struct usb_descriptor_header *eth_hs_function[] __initdata = {
272 (struct usb_descriptor_header *) &rndis_iad_descriptor,
255 /* control interface matches ACM, not Ethernet */ 273 /* control interface matches ACM, not Ethernet */
256 (struct usb_descriptor_header *) &rndis_control_intf, 274 (struct usb_descriptor_header *) &rndis_control_intf,
257 (struct usb_descriptor_header *) &header_desc, 275 (struct usb_descriptor_header *) &header_desc,
258 (struct usb_descriptor_header *) &call_mgmt_descriptor, 276 (struct usb_descriptor_header *) &call_mgmt_descriptor,
259 (struct usb_descriptor_header *) &acm_descriptor, 277 (struct usb_descriptor_header *) &rndis_acm_descriptor,
260 (struct usb_descriptor_header *) &rndis_union_desc, 278 (struct usb_descriptor_header *) &rndis_union_desc,
261 (struct usb_descriptor_header *) &hs_notify_desc, 279 (struct usb_descriptor_header *) &hs_notify_desc,
262 /* data interface has no altsetting */ 280 /* data interface has no altsetting */
@@ -271,6 +289,7 @@ static struct usb_descriptor_header *eth_hs_function[] __initdata = {
271static struct usb_string rndis_string_defs[] = { 289static struct usb_string rndis_string_defs[] = {
272 [0].s = "RNDIS Communications Control", 290 [0].s = "RNDIS Communications Control",
273 [1].s = "RNDIS Ethernet Data", 291 [1].s = "RNDIS Ethernet Data",
292 [2].s = "RNDIS",
274 { } /* end of list */ 293 { } /* end of list */
275}; 294};
276 295
@@ -587,6 +606,7 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
587 if (status < 0) 606 if (status < 0)
588 goto fail; 607 goto fail;
589 rndis->ctrl_id = status; 608 rndis->ctrl_id = status;
609 rndis_iad_descriptor.bFirstInterface = status;
590 610
591 rndis_control_intf.bInterfaceNumber = status; 611 rndis_control_intf.bInterfaceNumber = status;
592 rndis_union_desc.bMasterInterface0 = status; 612 rndis_union_desc.bMasterInterface0 = status;
@@ -798,6 +818,13 @@ int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
798 return status; 818 return status;
799 rndis_string_defs[1].id = status; 819 rndis_string_defs[1].id = status;
800 rndis_data_intf.iInterface = status; 820 rndis_data_intf.iInterface = status;
821
822 /* IAD iFunction label */
823 status = usb_string_id(c->cdev);
824 if (status < 0)
825 return status;
826 rndis_string_defs[2].id = status;
827 rndis_iad_descriptor.iFunction = status;
801 } 828 }
802 829
803 /* allocate and initialize one new instance */ 830 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 1e6aa504d58a..fca3407e48f2 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -248,8 +248,6 @@
248#include <linux/freezer.h> 248#include <linux/freezer.h>
249#include <linux/utsname.h> 249#include <linux/utsname.h>
250 250
251#include <asm/unaligned.h>
252
253#include <linux/usb/ch9.h> 251#include <linux/usb/ch9.h>
254#include <linux/usb/gadget.h> 252#include <linux/usb/gadget.h>
255 253
@@ -274,21 +272,20 @@
274#define DRIVER_NAME "g_file_storage" 272#define DRIVER_NAME "g_file_storage"
275#define DRIVER_VERSION "20 November 2008" 273#define DRIVER_VERSION "20 November 2008"
276 274
277static const char longname[] = DRIVER_DESC; 275static char fsg_string_manufacturer[64];
278static const char shortname[] = DRIVER_NAME; 276static const char fsg_string_product[] = DRIVER_DESC;
277static char fsg_string_serial[13];
278static const char fsg_string_config[] = "Self-powered";
279static const char fsg_string_interface[] = "Mass Storage";
280
281
282#include "storage_common.c"
283
279 284
280MODULE_DESCRIPTION(DRIVER_DESC); 285MODULE_DESCRIPTION(DRIVER_DESC);
281MODULE_AUTHOR("Alan Stern"); 286MODULE_AUTHOR("Alan Stern");
282MODULE_LICENSE("Dual BSD/GPL"); 287MODULE_LICENSE("Dual BSD/GPL");
283 288
284/* Thanks to NetChip Technologies for donating this product ID.
285 *
286 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
287 * Instead: allocate your own, using normal USB-IF procedures. */
288#define DRIVER_VENDOR_ID 0x0525 // NetChip
289#define DRIVER_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget
290
291
292/* 289/*
293 * This driver assumes self-powered hardware and has no way for users to 290 * This driver assumes self-powered hardware and has no way for users to
294 * trigger remote wakeup. It uses autoconfiguration to select endpoints 291 * trigger remote wakeup. It uses autoconfiguration to select endpoints
@@ -298,54 +295,12 @@ MODULE_LICENSE("Dual BSD/GPL");
298 295
299/*-------------------------------------------------------------------------*/ 296/*-------------------------------------------------------------------------*/
300 297
301#define LDBG(lun,fmt,args...) \
302 dev_dbg(&(lun)->dev , fmt , ## args)
303#define MDBG(fmt,args...) \
304 pr_debug(DRIVER_NAME ": " fmt , ## args)
305
306#ifndef DEBUG
307#undef VERBOSE_DEBUG
308#undef DUMP_MSGS
309#endif /* !DEBUG */
310
311#ifdef VERBOSE_DEBUG
312#define VLDBG LDBG
313#else
314#define VLDBG(lun,fmt,args...) \
315 do { } while (0)
316#endif /* VERBOSE_DEBUG */
317
318#define LERROR(lun,fmt,args...) \
319 dev_err(&(lun)->dev , fmt , ## args)
320#define LWARN(lun,fmt,args...) \
321 dev_warn(&(lun)->dev , fmt , ## args)
322#define LINFO(lun,fmt,args...) \
323 dev_info(&(lun)->dev , fmt , ## args)
324
325#define MINFO(fmt,args...) \
326 pr_info(DRIVER_NAME ": " fmt , ## args)
327
328#define DBG(d, fmt, args...) \
329 dev_dbg(&(d)->gadget->dev , fmt , ## args)
330#define VDBG(d, fmt, args...) \
331 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
332#define ERROR(d, fmt, args...) \
333 dev_err(&(d)->gadget->dev , fmt , ## args)
334#define WARNING(d, fmt, args...) \
335 dev_warn(&(d)->gadget->dev , fmt , ## args)
336#define INFO(d, fmt, args...) \
337 dev_info(&(d)->gadget->dev , fmt , ## args)
338
339
340/*-------------------------------------------------------------------------*/
341 298
342/* Encapsulate the module parameter settings */ 299/* Encapsulate the module parameter settings */
343 300
344#define MAX_LUNS 8
345
346static struct { 301static struct {
347 char *file[MAX_LUNS]; 302 char *file[FSG_MAX_LUNS];
348 int ro[MAX_LUNS]; 303 int ro[FSG_MAX_LUNS];
349 unsigned int num_filenames; 304 unsigned int num_filenames;
350 unsigned int num_ros; 305 unsigned int num_ros;
351 unsigned int nluns; 306 unsigned int nluns;
@@ -372,8 +327,8 @@ static struct {
372 .removable = 0, 327 .removable = 0,
373 .can_stall = 1, 328 .can_stall = 1,
374 .cdrom = 0, 329 .cdrom = 0,
375 .vendor = DRIVER_VENDOR_ID, 330 .vendor = FSG_VENDOR_ID,
376 .product = DRIVER_PRODUCT_ID, 331 .product = FSG_PRODUCT_ID,
377 .release = 0xffff, // Use controller chip type 332 .release = 0xffff, // Use controller chip type
378 .buflen = 16384, 333 .buflen = 16384,
379 }; 334 };
@@ -425,125 +380,6 @@ MODULE_PARM_DESC(buflen, "I/O buffer size");
425#endif /* CONFIG_USB_FILE_STORAGE_TEST */ 380#endif /* CONFIG_USB_FILE_STORAGE_TEST */
426 381
427 382
428/*-------------------------------------------------------------------------*/
429
430/* SCSI device types */
431#define TYPE_DISK 0x00
432#define TYPE_CDROM 0x05
433
434/* USB protocol value = the transport method */
435#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
436#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
437#define USB_PR_BULK 0x50 // Bulk-only
438
439/* USB subclass value = the protocol encapsulation */
440#define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
441#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
442#define USB_SC_QIC 0x03 // QIC-157 (tape)
443#define USB_SC_UFI 0x04 // UFI (floppy)
444#define USB_SC_8070 0x05 // SFF-8070i (removable)
445#define USB_SC_SCSI 0x06 // Transparent SCSI
446
447/* Bulk-only data structures */
448
449/* Command Block Wrapper */
450struct bulk_cb_wrap {
451 __le32 Signature; // Contains 'USBC'
452 u32 Tag; // Unique per command id
453 __le32 DataTransferLength; // Size of the data
454 u8 Flags; // Direction in bit 7
455 u8 Lun; // LUN (normally 0)
456 u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
457 u8 CDB[16]; // Command Data Block
458};
459
460#define USB_BULK_CB_WRAP_LEN 31
461#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
462#define USB_BULK_IN_FLAG 0x80
463
464/* Command Status Wrapper */
465struct bulk_cs_wrap {
466 __le32 Signature; // Should = 'USBS'
467 u32 Tag; // Same as original command
468 __le32 Residue; // Amount not transferred
469 u8 Status; // See below
470};
471
472#define USB_BULK_CS_WRAP_LEN 13
473#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
474#define USB_STATUS_PASS 0
475#define USB_STATUS_FAIL 1
476#define USB_STATUS_PHASE_ERROR 2
477
478/* Bulk-only class specific requests */
479#define USB_BULK_RESET_REQUEST 0xff
480#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
481
482
483/* CBI Interrupt data structure */
484struct interrupt_data {
485 u8 bType;
486 u8 bValue;
487};
488
489#define CBI_INTERRUPT_DATA_LEN 2
490
491/* CBI Accept Device-Specific Command request */
492#define USB_CBI_ADSC_REQUEST 0x00
493
494
495#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
496
497/* SCSI commands that we recognize */
498#define SC_FORMAT_UNIT 0x04
499#define SC_INQUIRY 0x12
500#define SC_MODE_SELECT_6 0x15
501#define SC_MODE_SELECT_10 0x55
502#define SC_MODE_SENSE_6 0x1a
503#define SC_MODE_SENSE_10 0x5a
504#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
505#define SC_READ_6 0x08
506#define SC_READ_10 0x28
507#define SC_READ_12 0xa8
508#define SC_READ_CAPACITY 0x25
509#define SC_READ_FORMAT_CAPACITIES 0x23
510#define SC_READ_HEADER 0x44
511#define SC_READ_TOC 0x43
512#define SC_RELEASE 0x17
513#define SC_REQUEST_SENSE 0x03
514#define SC_RESERVE 0x16
515#define SC_SEND_DIAGNOSTIC 0x1d
516#define SC_START_STOP_UNIT 0x1b
517#define SC_SYNCHRONIZE_CACHE 0x35
518#define SC_TEST_UNIT_READY 0x00
519#define SC_VERIFY 0x2f
520#define SC_WRITE_6 0x0a
521#define SC_WRITE_10 0x2a
522#define SC_WRITE_12 0xaa
523
524/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
525#define SS_NO_SENSE 0
526#define SS_COMMUNICATION_FAILURE 0x040800
527#define SS_INVALID_COMMAND 0x052000
528#define SS_INVALID_FIELD_IN_CDB 0x052400
529#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
530#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
531#define SS_MEDIUM_NOT_PRESENT 0x023a00
532#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
533#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
534#define SS_RESET_OCCURRED 0x062900
535#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
536#define SS_UNRECOVERED_READ_ERROR 0x031100
537#define SS_WRITE_ERROR 0x030c02
538#define SS_WRITE_PROTECTED 0x072700
539
540#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
541#define ASC(x) ((u8) ((x) >> 8))
542#define ASCQ(x) ((u8) (x))
543
544
545/*-------------------------------------------------------------------------*/
546
547/* 383/*
548 * These definitions will permit the compiler to avoid generating code for 384 * These definitions will permit the compiler to avoid generating code for
549 * parts of the driver that aren't used in the non-TEST version. Even gcc 385 * parts of the driver that aren't used in the non-TEST version. Even gcc
@@ -566,81 +402,8 @@ struct interrupt_data {
566#endif /* CONFIG_USB_FILE_STORAGE_TEST */ 402#endif /* CONFIG_USB_FILE_STORAGE_TEST */
567 403
568 404
569struct lun { 405/*-------------------------------------------------------------------------*/
570 struct file *filp;
571 loff_t file_length;
572 loff_t num_sectors;
573
574 unsigned int ro : 1;
575 unsigned int prevent_medium_removal : 1;
576 unsigned int registered : 1;
577 unsigned int info_valid : 1;
578
579 u32 sense_data;
580 u32 sense_data_info;
581 u32 unit_attention_data;
582
583 struct device dev;
584};
585
586#define backing_file_is_open(curlun) ((curlun)->filp != NULL)
587
588static struct lun *dev_to_lun(struct device *dev)
589{
590 return container_of(dev, struct lun, dev);
591}
592
593
594/* Big enough to hold our biggest descriptor */
595#define EP0_BUFSIZE 256
596#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
597
598/* Number of buffers we will use. 2 is enough for double-buffering */
599#define NUM_BUFFERS 2
600
601enum fsg_buffer_state {
602 BUF_STATE_EMPTY = 0,
603 BUF_STATE_FULL,
604 BUF_STATE_BUSY
605};
606
607struct fsg_buffhd {
608 void *buf;
609 enum fsg_buffer_state state;
610 struct fsg_buffhd *next;
611
612 /* The NetChip 2280 is faster, and handles some protocol faults
613 * better, if we don't submit any short bulk-out read requests.
614 * So we will record the intended request length here. */
615 unsigned int bulk_out_intended_length;
616
617 struct usb_request *inreq;
618 int inreq_busy;
619 struct usb_request *outreq;
620 int outreq_busy;
621};
622
623enum fsg_state {
624 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
625 FSG_STATE_DATA_PHASE,
626 FSG_STATE_STATUS_PHASE,
627
628 FSG_STATE_IDLE = 0,
629 FSG_STATE_ABORT_BULK_OUT,
630 FSG_STATE_RESET,
631 FSG_STATE_INTERFACE_CHANGE,
632 FSG_STATE_CONFIG_CHANGE,
633 FSG_STATE_DISCONNECT,
634 FSG_STATE_EXIT,
635 FSG_STATE_TERMINATED
636};
637 406
638enum data_direction {
639 DATA_DIR_UNKNOWN = 0,
640 DATA_DIR_FROM_HOST,
641 DATA_DIR_TO_HOST,
642 DATA_DIR_NONE
643};
644 407
645struct fsg_dev { 408struct fsg_dev {
646 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ 409 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
@@ -662,7 +425,7 @@ struct fsg_dev {
662 int intreq_busy; 425 int intreq_busy;
663 struct fsg_buffhd *intr_buffhd; 426 struct fsg_buffhd *intr_buffhd;
664 427
665 unsigned int bulk_out_maxpacket; 428 unsigned int bulk_out_maxpacket;
666 enum fsg_state state; // For exception handling 429 enum fsg_state state; // For exception handling
667 unsigned int exception_req_tag; 430 unsigned int exception_req_tag;
668 431
@@ -687,7 +450,7 @@ struct fsg_dev {
687 450
688 struct fsg_buffhd *next_buffhd_to_fill; 451 struct fsg_buffhd *next_buffhd_to_fill;
689 struct fsg_buffhd *next_buffhd_to_drain; 452 struct fsg_buffhd *next_buffhd_to_drain;
690 struct fsg_buffhd buffhds[NUM_BUFFERS]; 453 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
691 454
692 int thread_wakeup_needed; 455 int thread_wakeup_needed;
693 struct completion thread_notifier; 456 struct completion thread_notifier;
@@ -712,8 +475,8 @@ struct fsg_dev {
712 u8 cbbuf_cmnd[MAX_COMMAND_SIZE]; 475 u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
713 476
714 unsigned int nluns; 477 unsigned int nluns;
715 struct lun *luns; 478 struct fsg_lun *luns;
716 struct lun *curlun; 479 struct fsg_lun *curlun;
717}; 480};
718 481
719typedef void (*fsg_routine_t)(struct fsg_dev *); 482typedef void (*fsg_routine_t)(struct fsg_dev *);
@@ -739,49 +502,9 @@ static void set_bulk_out_req_length(struct fsg_dev *fsg,
739static struct fsg_dev *the_fsg; 502static struct fsg_dev *the_fsg;
740static struct usb_gadget_driver fsg_driver; 503static struct usb_gadget_driver fsg_driver;
741 504
742static void close_backing_file(struct lun *curlun);
743
744 505
745/*-------------------------------------------------------------------------*/ 506/*-------------------------------------------------------------------------*/
746 507
747#ifdef DUMP_MSGS
748
749static void dump_msg(struct fsg_dev *fsg, const char *label,
750 const u8 *buf, unsigned int length)
751{
752 if (length < 512) {
753 DBG(fsg, "%s, length %u:\n", label, length);
754 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
755 16, 1, buf, length, 0);
756 }
757}
758
759static void dump_cdb(struct fsg_dev *fsg)
760{}
761
762#else
763
764static void dump_msg(struct fsg_dev *fsg, const char *label,
765 const u8 *buf, unsigned int length)
766{}
767
768#ifdef VERBOSE_DEBUG
769
770static void dump_cdb(struct fsg_dev *fsg)
771{
772 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,
773 16, 1, fsg->cmnd, fsg->cmnd_size, 0);
774}
775
776#else
777
778static void dump_cdb(struct fsg_dev *fsg)
779{}
780
781#endif /* VERBOSE_DEBUG */
782#endif /* DUMP_MSGS */
783
784
785static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) 508static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
786{ 509{
787 const char *name; 510 const char *name;
@@ -799,26 +522,11 @@ static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
799 522
800/*-------------------------------------------------------------------------*/ 523/*-------------------------------------------------------------------------*/
801 524
802/* Routines for unaligned data access */
803
804static u32 get_unaligned_be24(u8 *buf)
805{
806 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
807}
808
809
810/*-------------------------------------------------------------------------*/
811
812/* 525/*
813 * DESCRIPTORS ... most are static, but strings and (full) configuration 526 * DESCRIPTORS ... most are static, but strings and (full) configuration
814 * descriptors are built on demand. Also the (static) config and interface 527 * descriptors are built on demand. Also the (static) config and interface
815 * descriptors are adjusted during fsg_bind(). 528 * descriptors are adjusted during fsg_bind().
816 */ 529 */
817#define STRING_MANUFACTURER 1
818#define STRING_PRODUCT 2
819#define STRING_SERIAL 3
820#define STRING_CONFIG 4
821#define STRING_INTERFACE 5
822 530
823/* There is only one configuration. */ 531/* There is only one configuration. */
824#define CONFIG_VALUE 1 532#define CONFIG_VALUE 1
@@ -832,13 +540,13 @@ device_desc = {
832 .bDeviceClass = USB_CLASS_PER_INTERFACE, 540 .bDeviceClass = USB_CLASS_PER_INTERFACE,
833 541
834 /* The next three values can be overridden by module parameters */ 542 /* The next three values can be overridden by module parameters */
835 .idVendor = cpu_to_le16(DRIVER_VENDOR_ID), 543 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
836 .idProduct = cpu_to_le16(DRIVER_PRODUCT_ID), 544 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
837 .bcdDevice = cpu_to_le16(0xffff), 545 .bcdDevice = cpu_to_le16(0xffff),
838 546
839 .iManufacturer = STRING_MANUFACTURER, 547 .iManufacturer = FSG_STRING_MANUFACTURER,
840 .iProduct = STRING_PRODUCT, 548 .iProduct = FSG_STRING_PRODUCT,
841 .iSerialNumber = STRING_SERIAL, 549 .iSerialNumber = FSG_STRING_SERIAL,
842 .bNumConfigurations = 1, 550 .bNumConfigurations = 1,
843}; 551};
844 552
@@ -850,86 +558,12 @@ config_desc = {
850 /* wTotalLength computed by usb_gadget_config_buf() */ 558 /* wTotalLength computed by usb_gadget_config_buf() */
851 .bNumInterfaces = 1, 559 .bNumInterfaces = 1,
852 .bConfigurationValue = CONFIG_VALUE, 560 .bConfigurationValue = CONFIG_VALUE,
853 .iConfiguration = STRING_CONFIG, 561 .iConfiguration = FSG_STRING_CONFIG,
854 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 562 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
855 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, 563 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
856}; 564};
857 565
858static struct usb_otg_descriptor
859otg_desc = {
860 .bLength = sizeof(otg_desc),
861 .bDescriptorType = USB_DT_OTG,
862
863 .bmAttributes = USB_OTG_SRP,
864};
865
866/* There is only one interface. */
867 566
868static struct usb_interface_descriptor
869intf_desc = {
870 .bLength = sizeof intf_desc,
871 .bDescriptorType = USB_DT_INTERFACE,
872
873 .bNumEndpoints = 2, // Adjusted during fsg_bind()
874 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
875 .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
876 .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
877 .iInterface = STRING_INTERFACE,
878};
879
880/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
881 * and interrupt-in. */
882
883static struct usb_endpoint_descriptor
884fs_bulk_in_desc = {
885 .bLength = USB_DT_ENDPOINT_SIZE,
886 .bDescriptorType = USB_DT_ENDPOINT,
887
888 .bEndpointAddress = USB_DIR_IN,
889 .bmAttributes = USB_ENDPOINT_XFER_BULK,
890 /* wMaxPacketSize set by autoconfiguration */
891};
892
893static struct usb_endpoint_descriptor
894fs_bulk_out_desc = {
895 .bLength = USB_DT_ENDPOINT_SIZE,
896 .bDescriptorType = USB_DT_ENDPOINT,
897
898 .bEndpointAddress = USB_DIR_OUT,
899 .bmAttributes = USB_ENDPOINT_XFER_BULK,
900 /* wMaxPacketSize set by autoconfiguration */
901};
902
903static struct usb_endpoint_descriptor
904fs_intr_in_desc = {
905 .bLength = USB_DT_ENDPOINT_SIZE,
906 .bDescriptorType = USB_DT_ENDPOINT,
907
908 .bEndpointAddress = USB_DIR_IN,
909 .bmAttributes = USB_ENDPOINT_XFER_INT,
910 .wMaxPacketSize = cpu_to_le16(2),
911 .bInterval = 32, // frames -> 32 ms
912};
913
914static const struct usb_descriptor_header *fs_function[] = {
915 (struct usb_descriptor_header *) &otg_desc,
916 (struct usb_descriptor_header *) &intf_desc,
917 (struct usb_descriptor_header *) &fs_bulk_in_desc,
918 (struct usb_descriptor_header *) &fs_bulk_out_desc,
919 (struct usb_descriptor_header *) &fs_intr_in_desc,
920 NULL,
921};
922#define FS_FUNCTION_PRE_EP_ENTRIES 2
923
924
925/*
926 * USB 2.0 devices need to expose both high speed and full speed
927 * descriptors, unless they only run at full speed.
928 *
929 * That means alternate endpoint descriptors (bigger packets)
930 * and a "device qualifier" ... plus more construction options
931 * for the config descriptor.
932 */
933static struct usb_qualifier_descriptor 567static struct usb_qualifier_descriptor
934dev_qualifier = { 568dev_qualifier = {
935 .bLength = sizeof dev_qualifier, 569 .bLength = sizeof dev_qualifier,
@@ -941,78 +575,6 @@ dev_qualifier = {
941 .bNumConfigurations = 1, 575 .bNumConfigurations = 1,
942}; 576};
943 577
944static struct usb_endpoint_descriptor
945hs_bulk_in_desc = {
946 .bLength = USB_DT_ENDPOINT_SIZE,
947 .bDescriptorType = USB_DT_ENDPOINT,
948
949 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
950 .bmAttributes = USB_ENDPOINT_XFER_BULK,
951 .wMaxPacketSize = cpu_to_le16(512),
952};
953
954static struct usb_endpoint_descriptor
955hs_bulk_out_desc = {
956 .bLength = USB_DT_ENDPOINT_SIZE,
957 .bDescriptorType = USB_DT_ENDPOINT,
958
959 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
960 .bmAttributes = USB_ENDPOINT_XFER_BULK,
961 .wMaxPacketSize = cpu_to_le16(512),
962 .bInterval = 1, // NAK every 1 uframe
963};
964
965static struct usb_endpoint_descriptor
966hs_intr_in_desc = {
967 .bLength = USB_DT_ENDPOINT_SIZE,
968 .bDescriptorType = USB_DT_ENDPOINT,
969
970 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
971 .bmAttributes = USB_ENDPOINT_XFER_INT,
972 .wMaxPacketSize = cpu_to_le16(2),
973 .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
974};
975
976static const struct usb_descriptor_header *hs_function[] = {
977 (struct usb_descriptor_header *) &otg_desc,
978 (struct usb_descriptor_header *) &intf_desc,
979 (struct usb_descriptor_header *) &hs_bulk_in_desc,
980 (struct usb_descriptor_header *) &hs_bulk_out_desc,
981 (struct usb_descriptor_header *) &hs_intr_in_desc,
982 NULL,
983};
984#define HS_FUNCTION_PRE_EP_ENTRIES 2
985
986/* Maxpacket and other transfer characteristics vary by speed. */
987static struct usb_endpoint_descriptor *
988ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
989 struct usb_endpoint_descriptor *hs)
990{
991 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
992 return hs;
993 return fs;
994}
995
996
997/* The CBI specification limits the serial string to 12 uppercase hexadecimal
998 * characters. */
999static char manufacturer[64];
1000static char serial[13];
1001
1002/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
1003static struct usb_string strings[] = {
1004 {STRING_MANUFACTURER, manufacturer},
1005 {STRING_PRODUCT, longname},
1006 {STRING_SERIAL, serial},
1007 {STRING_CONFIG, "Self-powered"},
1008 {STRING_INTERFACE, "Mass Storage"},
1009 {}
1010};
1011
1012static struct usb_gadget_strings stringtab = {
1013 .language = 0x0409, // en-us
1014 .strings = strings,
1015};
1016 578
1017 579
1018/* 580/*
@@ -1032,10 +594,9 @@ static int populate_config_buf(struct usb_gadget *gadget,
1032 594
1033 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG) 595 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
1034 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed; 596 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
1035 if (gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH) 597 function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
1036 function = hs_function; 598 ? (const struct usb_descriptor_header **)fsg_hs_function
1037 else 599 : (const struct usb_descriptor_header **)fsg_fs_function;
1038 function = fs_function;
1039 600
1040 /* for now, don't advertise srp-only devices */ 601 /* for now, don't advertise srp-only devices */
1041 if (!gadget_is_otg(gadget)) 602 if (!gadget_is_otg(gadget))
@@ -1386,7 +947,7 @@ get_config:
1386 VDBG(fsg, "get string descriptor\n"); 947 VDBG(fsg, "get string descriptor\n");
1387 948
1388 /* wIndex == language code */ 949 /* wIndex == language code */
1389 value = usb_gadget_get_string(&stringtab, 950 value = usb_gadget_get_string(&fsg_stringtab,
1390 w_value & 0xff, req->buf); 951 w_value & 0xff, req->buf);
1391 break; 952 break;
1392 } 953 }
@@ -1551,7 +1112,7 @@ static int sleep_thread(struct fsg_dev *fsg)
1551 1112
1552static int do_read(struct fsg_dev *fsg) 1113static int do_read(struct fsg_dev *fsg)
1553{ 1114{
1554 struct lun *curlun = fsg->curlun; 1115 struct fsg_lun *curlun = fsg->curlun;
1555 u32 lba; 1116 u32 lba;
1556 struct fsg_buffhd *bh; 1117 struct fsg_buffhd *bh;
1557 int rc; 1118 int rc;
@@ -1677,7 +1238,7 @@ static int do_read(struct fsg_dev *fsg)
1677 1238
1678static int do_write(struct fsg_dev *fsg) 1239static int do_write(struct fsg_dev *fsg)
1679{ 1240{
1680 struct lun *curlun = fsg->curlun; 1241 struct fsg_lun *curlun = fsg->curlun;
1681 u32 lba; 1242 u32 lba;
1682 struct fsg_buffhd *bh; 1243 struct fsg_buffhd *bh;
1683 int get_some_more; 1244 int get_some_more;
@@ -1864,33 +1425,14 @@ static int do_write(struct fsg_dev *fsg)
1864 1425
1865/*-------------------------------------------------------------------------*/ 1426/*-------------------------------------------------------------------------*/
1866 1427
1867/* Sync the file data, don't bother with the metadata.
1868 * This code was copied from fs/buffer.c:sys_fdatasync(). */
1869static int fsync_sub(struct lun *curlun)
1870{
1871 struct file *filp = curlun->filp;
1872
1873 if (curlun->ro || !filp)
1874 return 0;
1875 return vfs_fsync(filp, filp->f_path.dentry, 1);
1876}
1877
1878static void fsync_all(struct fsg_dev *fsg)
1879{
1880 int i;
1881
1882 for (i = 0; i < fsg->nluns; ++i)
1883 fsync_sub(&fsg->luns[i]);
1884}
1885
1886static int do_synchronize_cache(struct fsg_dev *fsg) 1428static int do_synchronize_cache(struct fsg_dev *fsg)
1887{ 1429{
1888 struct lun *curlun = fsg->curlun; 1430 struct fsg_lun *curlun = fsg->curlun;
1889 int rc; 1431 int rc;
1890 1432
1891 /* We ignore the requested LBA and write out all file's 1433 /* We ignore the requested LBA and write out all file's
1892 * dirty data buffers. */ 1434 * dirty data buffers. */
1893 rc = fsync_sub(curlun); 1435 rc = fsg_lun_fsync_sub(curlun);
1894 if (rc) 1436 if (rc)
1895 curlun->sense_data = SS_WRITE_ERROR; 1437 curlun->sense_data = SS_WRITE_ERROR;
1896 return 0; 1438 return 0;
@@ -1899,7 +1441,7 @@ static int do_synchronize_cache(struct fsg_dev *fsg)
1899 1441
1900/*-------------------------------------------------------------------------*/ 1442/*-------------------------------------------------------------------------*/
1901 1443
1902static void invalidate_sub(struct lun *curlun) 1444static void invalidate_sub(struct fsg_lun *curlun)
1903{ 1445{
1904 struct file *filp = curlun->filp; 1446 struct file *filp = curlun->filp;
1905 struct inode *inode = filp->f_path.dentry->d_inode; 1447 struct inode *inode = filp->f_path.dentry->d_inode;
@@ -1911,7 +1453,7 @@ static void invalidate_sub(struct lun *curlun)
1911 1453
1912static int do_verify(struct fsg_dev *fsg) 1454static int do_verify(struct fsg_dev *fsg)
1913{ 1455{
1914 struct lun *curlun = fsg->curlun; 1456 struct fsg_lun *curlun = fsg->curlun;
1915 u32 lba; 1457 u32 lba;
1916 u32 verification_length; 1458 u32 verification_length;
1917 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; 1459 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
@@ -1944,7 +1486,7 @@ static int do_verify(struct fsg_dev *fsg)
1944 file_offset = ((loff_t) lba) << 9; 1486 file_offset = ((loff_t) lba) << 9;
1945 1487
1946 /* Write out all the dirty buffers before invalidating them */ 1488 /* Write out all the dirty buffers before invalidating them */
1947 fsync_sub(curlun); 1489 fsg_lun_fsync_sub(curlun);
1948 if (signal_pending(current)) 1490 if (signal_pending(current))
1949 return -EINTR; 1491 return -EINTR;
1950 1492
@@ -2041,7 +1583,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2041 1583
2042static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1584static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2043{ 1585{
2044 struct lun *curlun = fsg->curlun; 1586 struct fsg_lun *curlun = fsg->curlun;
2045 u8 *buf = (u8 *) bh->buf; 1587 u8 *buf = (u8 *) bh->buf;
2046 u32 sd, sdinfo; 1588 u32 sd, sdinfo;
2047 int valid; 1589 int valid;
@@ -2095,7 +1637,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2095 1637
2096static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1638static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2097{ 1639{
2098 struct lun *curlun = fsg->curlun; 1640 struct fsg_lun *curlun = fsg->curlun;
2099 u32 lba = get_unaligned_be32(&fsg->cmnd[2]); 1641 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2100 int pmi = fsg->cmnd[8]; 1642 int pmi = fsg->cmnd[8];
2101 u8 *buf = (u8 *) bh->buf; 1643 u8 *buf = (u8 *) bh->buf;
@@ -2113,27 +1655,9 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2113} 1655}
2114 1656
2115 1657
2116static void store_cdrom_address(u8 *dest, int msf, u32 addr)
2117{
2118 if (msf) {
2119 /* Convert to Minutes-Seconds-Frames */
2120 addr >>= 2; /* Convert to 2048-byte frames */
2121 addr += 2*75; /* Lead-in occupies 2 seconds */
2122 dest[3] = addr % 75; /* Frames */
2123 addr /= 75;
2124 dest[2] = addr % 60; /* Seconds */
2125 addr /= 60;
2126 dest[1] = addr; /* Minutes */
2127 dest[0] = 0; /* Reserved */
2128 } else {
2129 /* Absolute sector */
2130 put_unaligned_be32(addr, dest);
2131 }
2132}
2133
2134static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1658static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2135{ 1659{
2136 struct lun *curlun = fsg->curlun; 1660 struct fsg_lun *curlun = fsg->curlun;
2137 int msf = fsg->cmnd[1] & 0x02; 1661 int msf = fsg->cmnd[1] & 0x02;
2138 u32 lba = get_unaligned_be32(&fsg->cmnd[2]); 1662 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2139 u8 *buf = (u8 *) bh->buf; 1663 u8 *buf = (u8 *) bh->buf;
@@ -2156,7 +1680,7 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2156 1680
2157static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1681static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2158{ 1682{
2159 struct lun *curlun = fsg->curlun; 1683 struct fsg_lun *curlun = fsg->curlun;
2160 int msf = fsg->cmnd[1] & 0x02; 1684 int msf = fsg->cmnd[1] & 0x02;
2161 int start_track = fsg->cmnd[6]; 1685 int start_track = fsg->cmnd[6];
2162 u8 *buf = (u8 *) bh->buf; 1686 u8 *buf = (u8 *) bh->buf;
@@ -2184,7 +1708,7 @@ static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2184 1708
2185static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1709static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2186{ 1710{
2187 struct lun *curlun = fsg->curlun; 1711 struct fsg_lun *curlun = fsg->curlun;
2188 int mscmnd = fsg->cmnd[0]; 1712 int mscmnd = fsg->cmnd[0];
2189 u8 *buf = (u8 *) bh->buf; 1713 u8 *buf = (u8 *) bh->buf;
2190 u8 *buf0 = buf; 1714 u8 *buf0 = buf;
@@ -2265,7 +1789,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2265 1789
2266static int do_start_stop(struct fsg_dev *fsg) 1790static int do_start_stop(struct fsg_dev *fsg)
2267{ 1791{
2268 struct lun *curlun = fsg->curlun; 1792 struct fsg_lun *curlun = fsg->curlun;
2269 int loej, start; 1793 int loej, start;
2270 1794
2271 if (!mod_data.removable) { 1795 if (!mod_data.removable) {
@@ -2295,7 +1819,7 @@ static int do_start_stop(struct fsg_dev *fsg)
2295 if (loej) { // Simulate an unload/eject 1819 if (loej) { // Simulate an unload/eject
2296 up_read(&fsg->filesem); 1820 up_read(&fsg->filesem);
2297 down_write(&fsg->filesem); 1821 down_write(&fsg->filesem);
2298 close_backing_file(curlun); 1822 fsg_lun_close(curlun);
2299 up_write(&fsg->filesem); 1823 up_write(&fsg->filesem);
2300 down_read(&fsg->filesem); 1824 down_read(&fsg->filesem);
2301 } 1825 }
@@ -2303,7 +1827,7 @@ static int do_start_stop(struct fsg_dev *fsg)
2303 1827
2304 /* Our emulation doesn't support mounting; the medium is 1828 /* Our emulation doesn't support mounting; the medium is
2305 * available for use as soon as it is loaded. */ 1829 * available for use as soon as it is loaded. */
2306 if (!backing_file_is_open(curlun)) { 1830 if (!fsg_lun_is_open(curlun)) {
2307 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 1831 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2308 return -EINVAL; 1832 return -EINVAL;
2309 } 1833 }
@@ -2315,7 +1839,7 @@ static int do_start_stop(struct fsg_dev *fsg)
2315 1839
2316static int do_prevent_allow(struct fsg_dev *fsg) 1840static int do_prevent_allow(struct fsg_dev *fsg)
2317{ 1841{
2318 struct lun *curlun = fsg->curlun; 1842 struct fsg_lun *curlun = fsg->curlun;
2319 int prevent; 1843 int prevent;
2320 1844
2321 if (!mod_data.removable) { 1845 if (!mod_data.removable) {
@@ -2330,7 +1854,7 @@ static int do_prevent_allow(struct fsg_dev *fsg)
2330 } 1854 }
2331 1855
2332 if (curlun->prevent_medium_removal && !prevent) 1856 if (curlun->prevent_medium_removal && !prevent)
2333 fsync_sub(curlun); 1857 fsg_lun_fsync_sub(curlun);
2334 curlun->prevent_medium_removal = prevent; 1858 curlun->prevent_medium_removal = prevent;
2335 return 0; 1859 return 0;
2336} 1860}
@@ -2339,7 +1863,7 @@ static int do_prevent_allow(struct fsg_dev *fsg)
2339static int do_read_format_capacities(struct fsg_dev *fsg, 1863static int do_read_format_capacities(struct fsg_dev *fsg,
2340 struct fsg_buffhd *bh) 1864 struct fsg_buffhd *bh)
2341{ 1865{
2342 struct lun *curlun = fsg->curlun; 1866 struct fsg_lun *curlun = fsg->curlun;
2343 u8 *buf = (u8 *) bh->buf; 1867 u8 *buf = (u8 *) bh->buf;
2344 1868
2345 buf[0] = buf[1] = buf[2] = 0; 1869 buf[0] = buf[1] = buf[2] = 0;
@@ -2356,7 +1880,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg,
2356 1880
2357static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1881static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2358{ 1882{
2359 struct lun *curlun = fsg->curlun; 1883 struct fsg_lun *curlun = fsg->curlun;
2360 1884
2361 /* We don't support MODE SELECT */ 1885 /* We don't support MODE SELECT */
2362 curlun->sense_data = SS_INVALID_COMMAND; 1886 curlun->sense_data = SS_INVALID_COMMAND;
@@ -2599,7 +2123,7 @@ static int finish_reply(struct fsg_dev *fsg)
2599 2123
2600static int send_status(struct fsg_dev *fsg) 2124static int send_status(struct fsg_dev *fsg)
2601{ 2125{
2602 struct lun *curlun = fsg->curlun; 2126 struct fsg_lun *curlun = fsg->curlun;
2603 struct fsg_buffhd *bh; 2127 struct fsg_buffhd *bh;
2604 int rc; 2128 int rc;
2605 u8 status = USB_STATUS_PASS; 2129 u8 status = USB_STATUS_PASS;
@@ -2691,7 +2215,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2691 int lun = fsg->cmnd[1] >> 5; 2215 int lun = fsg->cmnd[1] >> 5;
2692 static const char dirletter[4] = {'u', 'o', 'i', 'n'}; 2216 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2693 char hdlen[20]; 2217 char hdlen[20];
2694 struct lun *curlun; 2218 struct fsg_lun *curlun;
2695 2219
2696 /* Adjust the expected cmnd_size for protocol encapsulation padding. 2220 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2697 * Transparent SCSI doesn't pad. */ 2221 * Transparent SCSI doesn't pad. */
@@ -2820,7 +2344,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2820 2344
2821 /* If the medium isn't mounted and the command needs to access 2345 /* If the medium isn't mounted and the command needs to access
2822 * it, return an error. */ 2346 * it, return an error. */
2823 if (curlun && !backing_file_is_open(curlun) && needs_medium) { 2347 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
2824 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 2348 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2825 return -EINVAL; 2349 return -EINVAL;
2826 } 2350 }
@@ -3075,8 +2599,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
3075 2599
3076static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) 2600static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3077{ 2601{
3078 struct usb_request *req = bh->outreq; 2602 struct usb_request *req = bh->outreq;
3079 struct bulk_cb_wrap *cbw = req->buf; 2603 struct fsg_bulk_cb_wrap *cbw = req->buf;
3080 2604
3081 /* Was this a real packet? Should it be ignored? */ 2605 /* Was this a real packet? Should it be ignored? */
3082 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) 2606 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
@@ -3105,7 +2629,7 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3105 } 2629 }
3106 2630
3107 /* Is the CBW meaningful? */ 2631 /* Is the CBW meaningful? */
3108 if (cbw->Lun >= MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG || 2632 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
3109 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) { 2633 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
3110 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, " 2634 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3111 "cmdlen %u\n", 2635 "cmdlen %u\n",
@@ -3238,7 +2762,7 @@ static int do_set_interface(struct fsg_dev *fsg, int altsetting)
3238 2762
3239reset: 2763reset:
3240 /* Deallocate the requests */ 2764 /* Deallocate the requests */
3241 for (i = 0; i < NUM_BUFFERS; ++i) { 2765 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
3242 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2766 struct fsg_buffhd *bh = &fsg->buffhds[i];
3243 2767
3244 if (bh->inreq) { 2768 if (bh->inreq) {
@@ -3276,12 +2800,14 @@ reset:
3276 DBG(fsg, "set interface %d\n", altsetting); 2800 DBG(fsg, "set interface %d\n", altsetting);
3277 2801
3278 /* Enable the endpoints */ 2802 /* Enable the endpoints */
3279 d = ep_desc(fsg->gadget, &fs_bulk_in_desc, &hs_bulk_in_desc); 2803 d = fsg_ep_desc(fsg->gadget,
2804 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
3280 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) 2805 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
3281 goto reset; 2806 goto reset;
3282 fsg->bulk_in_enabled = 1; 2807 fsg->bulk_in_enabled = 1;
3283 2808
3284 d = ep_desc(fsg->gadget, &fs_bulk_out_desc, &hs_bulk_out_desc); 2809 d = fsg_ep_desc(fsg->gadget,
2810 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
3285 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) 2811 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
3286 goto reset; 2812 goto reset;
3287 fsg->bulk_out_enabled = 1; 2813 fsg->bulk_out_enabled = 1;
@@ -3289,14 +2815,15 @@ reset:
3289 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); 2815 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3290 2816
3291 if (transport_is_cbi()) { 2817 if (transport_is_cbi()) {
3292 d = ep_desc(fsg->gadget, &fs_intr_in_desc, &hs_intr_in_desc); 2818 d = fsg_ep_desc(fsg->gadget,
2819 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc);
3293 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0) 2820 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
3294 goto reset; 2821 goto reset;
3295 fsg->intr_in_enabled = 1; 2822 fsg->intr_in_enabled = 1;
3296 } 2823 }
3297 2824
3298 /* Allocate the requests */ 2825 /* Allocate the requests */
3299 for (i = 0; i < NUM_BUFFERS; ++i) { 2826 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
3300 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2827 struct fsg_buffhd *bh = &fsg->buffhds[i];
3301 2828
3302 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) 2829 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
@@ -3372,7 +2899,7 @@ static void handle_exception(struct fsg_dev *fsg)
3372 struct fsg_buffhd *bh; 2899 struct fsg_buffhd *bh;
3373 enum fsg_state old_state; 2900 enum fsg_state old_state;
3374 u8 new_config; 2901 u8 new_config;
3375 struct lun *curlun; 2902 struct fsg_lun *curlun;
3376 unsigned int exception_req_tag; 2903 unsigned int exception_req_tag;
3377 int rc; 2904 int rc;
3378 2905
@@ -3392,7 +2919,7 @@ static void handle_exception(struct fsg_dev *fsg)
3392 /* Cancel all the pending transfers */ 2919 /* Cancel all the pending transfers */
3393 if (fsg->intreq_busy) 2920 if (fsg->intreq_busy)
3394 usb_ep_dequeue(fsg->intr_in, fsg->intreq); 2921 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3395 for (i = 0; i < NUM_BUFFERS; ++i) { 2922 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
3396 bh = &fsg->buffhds[i]; 2923 bh = &fsg->buffhds[i];
3397 if (bh->inreq_busy) 2924 if (bh->inreq_busy)
3398 usb_ep_dequeue(fsg->bulk_in, bh->inreq); 2925 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
@@ -3403,7 +2930,7 @@ static void handle_exception(struct fsg_dev *fsg)
3403 /* Wait until everything is idle */ 2930 /* Wait until everything is idle */
3404 for (;;) { 2931 for (;;) {
3405 num_active = fsg->intreq_busy; 2932 num_active = fsg->intreq_busy;
3406 for (i = 0; i < NUM_BUFFERS; ++i) { 2933 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
3407 bh = &fsg->buffhds[i]; 2934 bh = &fsg->buffhds[i];
3408 num_active += bh->inreq_busy + bh->outreq_busy; 2935 num_active += bh->inreq_busy + bh->outreq_busy;
3409 } 2936 }
@@ -3425,7 +2952,7 @@ static void handle_exception(struct fsg_dev *fsg)
3425 * state, and the exception. Then invoke the handler. */ 2952 * state, and the exception. Then invoke the handler. */
3426 spin_lock_irq(&fsg->lock); 2953 spin_lock_irq(&fsg->lock);
3427 2954
3428 for (i = 0; i < NUM_BUFFERS; ++i) { 2955 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
3429 bh = &fsg->buffhds[i]; 2956 bh = &fsg->buffhds[i];
3430 bh->state = BUF_STATE_EMPTY; 2957 bh->state = BUF_STATE_EMPTY;
3431 } 2958 }
@@ -3506,7 +3033,8 @@ static void handle_exception(struct fsg_dev *fsg)
3506 break; 3033 break;
3507 3034
3508 case FSG_STATE_DISCONNECT: 3035 case FSG_STATE_DISCONNECT:
3509 fsync_all(fsg); 3036 for (i = 0; i < fsg->nluns; ++i)
3037 fsg_lun_fsync_sub(fsg->luns + i);
3510 do_set_config(fsg, 0); // Unconfigured state 3038 do_set_config(fsg, 0); // Unconfigured state
3511 break; 3039 break;
3512 3040
@@ -3595,201 +3123,10 @@ static int fsg_main_thread(void *fsg_)
3595 3123
3596/*-------------------------------------------------------------------------*/ 3124/*-------------------------------------------------------------------------*/
3597 3125
3598/* If the next two routines are called while the gadget is registered,
3599 * the caller must own fsg->filesem for writing. */
3600
3601static int open_backing_file(struct lun *curlun, const char *filename)
3602{
3603 int ro;
3604 struct file *filp = NULL;
3605 int rc = -EINVAL;
3606 struct inode *inode = NULL;
3607 loff_t size;
3608 loff_t num_sectors;
3609 loff_t min_sectors;
3610
3611 /* R/W if we can, R/O if we must */
3612 ro = curlun->ro;
3613 if (!ro) {
3614 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
3615 if (-EROFS == PTR_ERR(filp))
3616 ro = 1;
3617 }
3618 if (ro)
3619 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
3620 if (IS_ERR(filp)) {
3621 LINFO(curlun, "unable to open backing file: %s\n", filename);
3622 return PTR_ERR(filp);
3623 }
3624
3625 if (!(filp->f_mode & FMODE_WRITE))
3626 ro = 1;
3627
3628 if (filp->f_path.dentry)
3629 inode = filp->f_path.dentry->d_inode;
3630 if (inode && S_ISBLK(inode->i_mode)) {
3631 if (bdev_read_only(inode->i_bdev))
3632 ro = 1;
3633 } else if (!inode || !S_ISREG(inode->i_mode)) {
3634 LINFO(curlun, "invalid file type: %s\n", filename);
3635 goto out;
3636 }
3637
3638 /* If we can't read the file, it's no good.
3639 * If we can't write the file, use it read-only. */
3640 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
3641 LINFO(curlun, "file not readable: %s\n", filename);
3642 goto out;
3643 }
3644 if (!(filp->f_op->write || filp->f_op->aio_write))
3645 ro = 1;
3646
3647 size = i_size_read(inode->i_mapping->host);
3648 if (size < 0) {
3649 LINFO(curlun, "unable to find file size: %s\n", filename);
3650 rc = (int) size;
3651 goto out;
3652 }
3653 num_sectors = size >> 9; // File size in 512-byte blocks
3654 min_sectors = 1;
3655 if (mod_data.cdrom) {
3656 num_sectors &= ~3; // Reduce to a multiple of 2048
3657 min_sectors = 300*4; // Smallest track is 300 frames
3658 if (num_sectors >= 256*60*75*4) {
3659 num_sectors = (256*60*75 - 1) * 4;
3660 LINFO(curlun, "file too big: %s\n", filename);
3661 LINFO(curlun, "using only first %d blocks\n",
3662 (int) num_sectors);
3663 }
3664 }
3665 if (num_sectors < min_sectors) {
3666 LINFO(curlun, "file too small: %s\n", filename);
3667 rc = -ETOOSMALL;
3668 goto out;
3669 }
3670
3671 get_file(filp);
3672 curlun->ro = ro;
3673 curlun->filp = filp;
3674 curlun->file_length = size;
3675 curlun->num_sectors = num_sectors;
3676 LDBG(curlun, "open backing file: %s\n", filename);
3677 rc = 0;
3678
3679out:
3680 filp_close(filp, current->files);
3681 return rc;
3682}
3683
3684
3685static void close_backing_file(struct lun *curlun)
3686{
3687 if (curlun->filp) {
3688 LDBG(curlun, "close backing file\n");
3689 fput(curlun->filp);
3690 curlun->filp = NULL;
3691 }
3692}
3693
3694
3695static ssize_t show_ro(struct device *dev, struct device_attribute *attr, char *buf)
3696{
3697 struct lun *curlun = dev_to_lun(dev);
3698
3699 return sprintf(buf, "%d\n", curlun->ro);
3700}
3701
3702static ssize_t show_file(struct device *dev, struct device_attribute *attr,
3703 char *buf)
3704{
3705 struct lun *curlun = dev_to_lun(dev);
3706 struct fsg_dev *fsg = dev_get_drvdata(dev);
3707 char *p;
3708 ssize_t rc;
3709
3710 down_read(&fsg->filesem);
3711 if (backing_file_is_open(curlun)) { // Get the complete pathname
3712 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
3713 if (IS_ERR(p))
3714 rc = PTR_ERR(p);
3715 else {
3716 rc = strlen(p);
3717 memmove(buf, p, rc);
3718 buf[rc] = '\n'; // Add a newline
3719 buf[++rc] = 0;
3720 }
3721 } else { // No file, return 0 bytes
3722 *buf = 0;
3723 rc = 0;
3724 }
3725 up_read(&fsg->filesem);
3726 return rc;
3727}
3728
3729
3730static ssize_t store_ro(struct device *dev, struct device_attribute *attr,
3731 const char *buf, size_t count)
3732{
3733 ssize_t rc = count;
3734 struct lun *curlun = dev_to_lun(dev);
3735 struct fsg_dev *fsg = dev_get_drvdata(dev);
3736 int i;
3737
3738 if (sscanf(buf, "%d", &i) != 1)
3739 return -EINVAL;
3740
3741 /* Allow the write-enable status to change only while the backing file
3742 * is closed. */
3743 down_read(&fsg->filesem);
3744 if (backing_file_is_open(curlun)) {
3745 LDBG(curlun, "read-only status change prevented\n");
3746 rc = -EBUSY;
3747 } else {
3748 curlun->ro = !!i;
3749 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
3750 }
3751 up_read(&fsg->filesem);
3752 return rc;
3753}
3754
3755static ssize_t store_file(struct device *dev, struct device_attribute *attr,
3756 const char *buf, size_t count)
3757{
3758 struct lun *curlun = dev_to_lun(dev);
3759 struct fsg_dev *fsg = dev_get_drvdata(dev);
3760 int rc = 0;
3761
3762 if (curlun->prevent_medium_removal && backing_file_is_open(curlun)) {
3763 LDBG(curlun, "eject attempt prevented\n");
3764 return -EBUSY; // "Door is locked"
3765 }
3766
3767 /* Remove a trailing newline */
3768 if (count > 0 && buf[count-1] == '\n')
3769 ((char *) buf)[count-1] = 0; // Ugh!
3770
3771 /* Eject current medium */
3772 down_write(&fsg->filesem);
3773 if (backing_file_is_open(curlun)) {
3774 close_backing_file(curlun);
3775 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
3776 }
3777
3778 /* Load new medium */
3779 if (count > 0 && buf[0]) {
3780 rc = open_backing_file(curlun, buf);
3781 if (rc == 0)
3782 curlun->unit_attention_data =
3783 SS_NOT_READY_TO_READY_TRANSITION;
3784 }
3785 up_write(&fsg->filesem);
3786 return (rc < 0 ? rc : count);
3787}
3788
3789 3126
3790/* The write permissions and store_xxx pointers are set in fsg_bind() */ 3127/* The write permissions and store_xxx pointers are set in fsg_bind() */
3791static DEVICE_ATTR(ro, 0444, show_ro, NULL); 3128static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
3792static DEVICE_ATTR(file, 0444, show_file, NULL); 3129static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
3793 3130
3794 3131
3795/*-------------------------------------------------------------------------*/ 3132/*-------------------------------------------------------------------------*/
@@ -3804,7 +3141,9 @@ static void fsg_release(struct kref *ref)
3804 3141
3805static void lun_release(struct device *dev) 3142static void lun_release(struct device *dev)
3806{ 3143{
3807 struct fsg_dev *fsg = dev_get_drvdata(dev); 3144 struct rw_semaphore *filesem = dev_get_drvdata(dev);
3145 struct fsg_dev *fsg =
3146 container_of(filesem, struct fsg_dev, filesem);
3808 3147
3809 kref_put(&fsg->ref, fsg_release); 3148 kref_put(&fsg->ref, fsg_release);
3810} 3149}
@@ -3813,7 +3152,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3813{ 3152{
3814 struct fsg_dev *fsg = get_gadget_data(gadget); 3153 struct fsg_dev *fsg = get_gadget_data(gadget);
3815 int i; 3154 int i;
3816 struct lun *curlun; 3155 struct fsg_lun *curlun;
3817 struct usb_request *req = fsg->ep0req; 3156 struct usb_request *req = fsg->ep0req;
3818 3157
3819 DBG(fsg, "unbind\n"); 3158 DBG(fsg, "unbind\n");
@@ -3825,7 +3164,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3825 if (curlun->registered) { 3164 if (curlun->registered) {
3826 device_remove_file(&curlun->dev, &dev_attr_ro); 3165 device_remove_file(&curlun->dev, &dev_attr_ro);
3827 device_remove_file(&curlun->dev, &dev_attr_file); 3166 device_remove_file(&curlun->dev, &dev_attr_file);
3828 close_backing_file(curlun); 3167 fsg_lun_close(curlun);
3829 device_unregister(&curlun->dev); 3168 device_unregister(&curlun->dev);
3830 curlun->registered = 0; 3169 curlun->registered = 0;
3831 } 3170 }
@@ -3841,7 +3180,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3841 } 3180 }
3842 3181
3843 /* Free the data buffers */ 3182 /* Free the data buffers */
3844 for (i = 0; i < NUM_BUFFERS; ++i) 3183 for (i = 0; i < FSG_NUM_BUFFERS; ++i)
3845 kfree(fsg->buffhds[i].buf); 3184 kfree(fsg->buffhds[i].buf);
3846 3185
3847 /* Free the request and buffer for endpoint 0 */ 3186 /* Free the request and buffer for endpoint 0 */
@@ -3948,7 +3287,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3948 struct fsg_dev *fsg = the_fsg; 3287 struct fsg_dev *fsg = the_fsg;
3949 int rc; 3288 int rc;
3950 int i; 3289 int i;
3951 struct lun *curlun; 3290 struct fsg_lun *curlun;
3952 struct usb_ep *ep; 3291 struct usb_ep *ep;
3953 struct usb_request *req; 3292 struct usb_request *req;
3954 char *pathbuf, *p; 3293 char *pathbuf, *p;
@@ -3963,10 +3302,10 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3963 3302
3964 if (mod_data.removable) { // Enable the store_xxx attributes 3303 if (mod_data.removable) { // Enable the store_xxx attributes
3965 dev_attr_file.attr.mode = 0644; 3304 dev_attr_file.attr.mode = 0644;
3966 dev_attr_file.store = store_file; 3305 dev_attr_file.store = fsg_store_file;
3967 if (!mod_data.cdrom) { 3306 if (!mod_data.cdrom) {
3968 dev_attr_ro.attr.mode = 0644; 3307 dev_attr_ro.attr.mode = 0644;
3969 dev_attr_ro.store = store_ro; 3308 dev_attr_ro.store = fsg_store_ro;
3970 } 3309 }
3971 } 3310 }
3972 3311
@@ -3974,7 +3313,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3974 i = mod_data.nluns; 3313 i = mod_data.nluns;
3975 if (i == 0) 3314 if (i == 0)
3976 i = max(mod_data.num_filenames, 1u); 3315 i = max(mod_data.num_filenames, 1u);
3977 if (i > MAX_LUNS) { 3316 if (i > FSG_MAX_LUNS) {
3978 ERROR(fsg, "invalid number of LUNs: %d\n", i); 3317 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3979 rc = -EINVAL; 3318 rc = -EINVAL;
3980 goto out; 3319 goto out;
@@ -3982,7 +3321,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3982 3321
3983 /* Create the LUNs, open their backing files, and register the 3322 /* Create the LUNs, open their backing files, and register the
3984 * LUN devices in sysfs. */ 3323 * LUN devices in sysfs. */
3985 fsg->luns = kzalloc(i * sizeof(struct lun), GFP_KERNEL); 3324 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
3986 if (!fsg->luns) { 3325 if (!fsg->luns) {
3987 rc = -ENOMEM; 3326 rc = -ENOMEM;
3988 goto out; 3327 goto out;
@@ -3991,13 +3330,14 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3991 3330
3992 for (i = 0; i < fsg->nluns; ++i) { 3331 for (i = 0; i < fsg->nluns; ++i) {
3993 curlun = &fsg->luns[i]; 3332 curlun = &fsg->luns[i];
3994 curlun->ro = mod_data.ro[i]; 3333 curlun->cdrom = !!mod_data.cdrom;
3995 if (mod_data.cdrom) 3334 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3996 curlun->ro = 1; 3335 curlun->initially_ro = curlun->ro;
3336 curlun->removable = mod_data.removable;
3997 curlun->dev.release = lun_release; 3337 curlun->dev.release = lun_release;
3998 curlun->dev.parent = &gadget->dev; 3338 curlun->dev.parent = &gadget->dev;
3999 curlun->dev.driver = &fsg_driver.driver; 3339 curlun->dev.driver = &fsg_driver.driver;
4000 dev_set_drvdata(&curlun->dev, fsg); 3340 dev_set_drvdata(&curlun->dev, &fsg->filesem);
4001 dev_set_name(&curlun->dev,"%s-lun%d", 3341 dev_set_name(&curlun->dev,"%s-lun%d",
4002 dev_name(&gadget->dev), i); 3342 dev_name(&gadget->dev), i);
4003 3343
@@ -4016,7 +3356,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4016 kref_get(&fsg->ref); 3356 kref_get(&fsg->ref);
4017 3357
4018 if (mod_data.file[i] && *mod_data.file[i]) { 3358 if (mod_data.file[i] && *mod_data.file[i]) {
4019 if ((rc = open_backing_file(curlun, 3359 if ((rc = fsg_lun_open(curlun,
4020 mod_data.file[i])) != 0) 3360 mod_data.file[i])) != 0)
4021 goto out; 3361 goto out;
4022 } else if (!mod_data.removable) { 3362 } else if (!mod_data.removable) {
@@ -4028,20 +3368,20 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4028 3368
4029 /* Find all the endpoints we will use */ 3369 /* Find all the endpoints we will use */
4030 usb_ep_autoconfig_reset(gadget); 3370 usb_ep_autoconfig_reset(gadget);
4031 ep = usb_ep_autoconfig(gadget, &fs_bulk_in_desc); 3371 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4032 if (!ep) 3372 if (!ep)
4033 goto autoconf_fail; 3373 goto autoconf_fail;
4034 ep->driver_data = fsg; // claim the endpoint 3374 ep->driver_data = fsg; // claim the endpoint
4035 fsg->bulk_in = ep; 3375 fsg->bulk_in = ep;
4036 3376
4037 ep = usb_ep_autoconfig(gadget, &fs_bulk_out_desc); 3377 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4038 if (!ep) 3378 if (!ep)
4039 goto autoconf_fail; 3379 goto autoconf_fail;
4040 ep->driver_data = fsg; // claim the endpoint 3380 ep->driver_data = fsg; // claim the endpoint
4041 fsg->bulk_out = ep; 3381 fsg->bulk_out = ep;
4042 3382
4043 if (transport_is_cbi()) { 3383 if (transport_is_cbi()) {
4044 ep = usb_ep_autoconfig(gadget, &fs_intr_in_desc); 3384 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4045 if (!ep) 3385 if (!ep)
4046 goto autoconf_fail; 3386 goto autoconf_fail;
4047 ep->driver_data = fsg; // claim the endpoint 3387 ep->driver_data = fsg; // claim the endpoint
@@ -4055,28 +3395,28 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4055 device_desc.bcdDevice = cpu_to_le16(mod_data.release); 3395 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4056 3396
4057 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints 3397 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4058 intf_desc.bNumEndpoints = i; 3398 fsg_intf_desc.bNumEndpoints = i;
4059 intf_desc.bInterfaceSubClass = mod_data.protocol_type; 3399 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4060 intf_desc.bInterfaceProtocol = mod_data.transport_type; 3400 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4061 fs_function[i + FS_FUNCTION_PRE_EP_ENTRIES] = NULL; 3401 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4062 3402
4063 if (gadget_is_dualspeed(gadget)) { 3403 if (gadget_is_dualspeed(gadget)) {
4064 hs_function[i + HS_FUNCTION_PRE_EP_ENTRIES] = NULL; 3404 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4065 3405
4066 /* Assume ep0 uses the same maxpacket value for both speeds */ 3406 /* Assume ep0 uses the same maxpacket value for both speeds */
4067 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket; 3407 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
4068 3408
4069 /* Assume endpoint addresses are the same for both speeds */ 3409 /* Assume endpoint addresses are the same for both speeds */
4070 hs_bulk_in_desc.bEndpointAddress = 3410 fsg_hs_bulk_in_desc.bEndpointAddress =
4071 fs_bulk_in_desc.bEndpointAddress; 3411 fsg_fs_bulk_in_desc.bEndpointAddress;
4072 hs_bulk_out_desc.bEndpointAddress = 3412 fsg_hs_bulk_out_desc.bEndpointAddress =
4073 fs_bulk_out_desc.bEndpointAddress; 3413 fsg_fs_bulk_out_desc.bEndpointAddress;
4074 hs_intr_in_desc.bEndpointAddress = 3414 fsg_hs_intr_in_desc.bEndpointAddress =
4075 fs_intr_in_desc.bEndpointAddress; 3415 fsg_fs_intr_in_desc.bEndpointAddress;
4076 } 3416 }
4077 3417
4078 if (gadget_is_otg(gadget)) 3418 if (gadget_is_otg(gadget))
4079 otg_desc.bmAttributes |= USB_OTG_HNP; 3419 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4080 3420
4081 rc = -ENOMEM; 3421 rc = -ENOMEM;
4082 3422
@@ -4090,7 +3430,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4090 req->complete = ep0_complete; 3430 req->complete = ep0_complete;
4091 3431
4092 /* Allocate the data buffers */ 3432 /* Allocate the data buffers */
4093 for (i = 0; i < NUM_BUFFERS; ++i) { 3433 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
4094 struct fsg_buffhd *bh = &fsg->buffhds[i]; 3434 struct fsg_buffhd *bh = &fsg->buffhds[i];
4095 3435
4096 /* Allocate for the bulk-in endpoint. We assume that 3436 /* Allocate for the bulk-in endpoint. We assume that
@@ -4101,23 +3441,24 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4101 goto out; 3441 goto out;
4102 bh->next = bh + 1; 3442 bh->next = bh + 1;
4103 } 3443 }
4104 fsg->buffhds[NUM_BUFFERS - 1].next = &fsg->buffhds[0]; 3444 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
4105 3445
4106 /* This should reflect the actual gadget power source */ 3446 /* This should reflect the actual gadget power source */
4107 usb_gadget_set_selfpowered(gadget); 3447 usb_gadget_set_selfpowered(gadget);
4108 3448
4109 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", 3449 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3450 "%s %s with %s",
4110 init_utsname()->sysname, init_utsname()->release, 3451 init_utsname()->sysname, init_utsname()->release,
4111 gadget->name); 3452 gadget->name);
4112 3453
4113 /* On a real device, serial[] would be loaded from permanent 3454 /* On a real device, serial[] would be loaded from permanent
4114 * storage. We just encode it from the driver version string. */ 3455 * storage. We just encode it from the driver version string. */
4115 for (i = 0; i < sizeof(serial) - 2; i += 2) { 3456 for (i = 0; i < sizeof fsg_string_serial - 2; i += 2) {
4116 unsigned char c = DRIVER_VERSION[i / 2]; 3457 unsigned char c = DRIVER_VERSION[i / 2];
4117 3458
4118 if (!c) 3459 if (!c)
4119 break; 3460 break;
4120 sprintf(&serial[i], "%02X", c); 3461 sprintf(&fsg_string_serial[i], "%02X", c);
4121 } 3462 }
4122 3463
4123 fsg->thread_task = kthread_create(fsg_main_thread, fsg, 3464 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
@@ -4133,7 +3474,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4133 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 3474 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4134 for (i = 0; i < fsg->nluns; ++i) { 3475 for (i = 0; i < fsg->nluns; ++i) {
4135 curlun = &fsg->luns[i]; 3476 curlun = &fsg->luns[i];
4136 if (backing_file_is_open(curlun)) { 3477 if (fsg_lun_is_open(curlun)) {
4137 p = NULL; 3478 p = NULL;
4138 if (pathbuf) { 3479 if (pathbuf) {
4139 p = d_path(&curlun->filp->f_path, 3480 p = d_path(&curlun->filp->f_path,
@@ -4203,7 +3544,7 @@ static struct usb_gadget_driver fsg_driver = {
4203#else 3544#else
4204 .speed = USB_SPEED_FULL, 3545 .speed = USB_SPEED_FULL,
4205#endif 3546#endif
4206 .function = (char *) longname, 3547 .function = (char *) fsg_string_product,
4207 .bind = fsg_bind, 3548 .bind = fsg_bind,
4208 .unbind = fsg_unbind, 3549 .unbind = fsg_unbind,
4209 .disconnect = fsg_disconnect, 3550 .disconnect = fsg_disconnect,
@@ -4212,7 +3553,7 @@ static struct usb_gadget_driver fsg_driver = {
4212 .resume = fsg_resume, 3553 .resume = fsg_resume,
4213 3554
4214 .driver = { 3555 .driver = {
4215 .name = (char *) shortname, 3556 .name = DRIVER_NAME,
4216 .owner = THIS_MODULE, 3557 .owner = THIS_MODULE,
4217 // .release = ... 3558 // .release = ...
4218 // .suspend = ... 3559 // .suspend = ...
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
new file mode 100644
index 000000000000..19619fbf20ac
--- /dev/null
+++ b/drivers/usb/gadget/mass_storage.c
@@ -0,0 +1,240 @@
1/*
2 * mass_storage.c -- Mass Storage USB Gadget
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25/*
26 * The Mass Storage Gadget acts as a USB Mass Storage device,
27 * appearing to the host as a disk drive or as a CD-ROM drive. In
28 * addition to providing an example of a genuinely useful gadget
29 * driver for a USB device, it also illustrates a technique of
30 * double-buffering for increased throughput. Last but not least, it
31 * gives an easy way to probe the behavior of the Mass Storage drivers
32 * in a USB host.
33 *
34 * Since this file serves only administrative purposes and all the
35 * business logic is implemented in f_mass_storage.* file. Read
36 * comments in this file for more detailed description.
37 */
38
39
40#include <linux/kernel.h>
41#include <linux/utsname.h>
42#include <linux/usb/ch9.h>
43
44
45/*-------------------------------------------------------------------------*/
46
47#define DRIVER_DESC "Mass Storage Gadget"
48#define DRIVER_VERSION "2009/09/11"
49
50/*-------------------------------------------------------------------------*/
51
52/*
53 * kbuild is not very cooperative with respect to linking separately
54 * compiled library objects into one module. So for now we won't use
55 * separate compilation ... ensuring init/exit sections work to shrink
56 * the runtime footprint, and giving us at least some parts of what
57 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
58 */
59
60#include "composite.c"
61#include "usbstring.c"
62#include "config.c"
63#include "epautoconf.c"
64#include "f_mass_storage.c"
65
66/*-------------------------------------------------------------------------*/
67
68static struct usb_device_descriptor msg_device_desc = {
69 .bLength = sizeof msg_device_desc,
70 .bDescriptorType = USB_DT_DEVICE,
71
72 .bcdUSB = cpu_to_le16(0x0200),
73 .bDeviceClass = USB_CLASS_PER_INTERFACE,
74
75 /* Vendor and product id can be overridden by module parameters. */
76 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
77 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
78 /* .bcdDevice = f(hardware) */
79 /* .iManufacturer = DYNAMIC */
80 /* .iProduct = DYNAMIC */
81 /* NO SERIAL NUMBER */
82 .bNumConfigurations = 1,
83};
84
85static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG,
88
89 /* REVISIT SRP-only hardware is possible, although
90 * it would not be called "OTG" ...
91 */
92 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
93};
94
95static const struct usb_descriptor_header *otg_desc[] = {
96 (struct usb_descriptor_header *) &otg_descriptor,
97 NULL,
98};
99
100
101/* string IDs are assigned dynamically */
102
103#define STRING_MANUFACTURER_IDX 0
104#define STRING_PRODUCT_IDX 1
105#define STRING_CONFIGURATION_IDX 2
106
107static char manufacturer[50];
108
109static struct usb_string strings_dev[] = {
110 [STRING_MANUFACTURER_IDX].s = manufacturer,
111 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
112 [STRING_CONFIGURATION_IDX].s = "Self Powered",
113 { } /* end of list */
114};
115
116static struct usb_gadget_strings stringtab_dev = {
117 .language = 0x0409, /* en-us */
118 .strings = strings_dev,
119};
120
121static struct usb_gadget_strings *dev_strings[] = {
122 &stringtab_dev,
123 NULL,
124};
125
126
127
128/****************************** Configurations ******************************/
129
130static struct fsg_module_parameters mod_data = {
131 .stall = 1
132};
133FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
134
135static unsigned long msg_registered = 0;
136static void msg_cleanup(void);
137
138static int __init msg_do_config(struct usb_configuration *c)
139{
140 struct fsg_common *common;
141 struct fsg_config config;
142 int ret;
143
144 if (gadget_is_otg(c->cdev->gadget)) {
145 c->descriptors = otg_desc;
146 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
147 }
148
149 fsg_config_from_params(&config, &mod_data);
150 config.thread_exits = (void(*)(struct fsg_common*))&msg_cleanup;
151 common = fsg_common_init(0, c->cdev, &config);
152 if (IS_ERR(common))
153 return PTR_ERR(common);
154
155 ret = fsg_add(c->cdev, c, common);
156 fsg_common_put(common);
157 return ret;
158}
159
160static struct usb_configuration msg_config_driver = {
161 .label = "Linux File-Backed Storage",
162 .bind = msg_do_config,
163 .bConfigurationValue = 1,
164 /* .iConfiguration = DYNAMIC */
165 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
166};
167
168
169
170/****************************** Gadget Bind ******************************/
171
172
173static int __init msg_bind(struct usb_composite_dev *cdev)
174{
175 struct usb_gadget *gadget = cdev->gadget;
176 int status;
177
178 /* Allocate string descriptor numbers ... note that string
179 * contents can be overridden by the composite_dev glue.
180 */
181
182 /* device descriptor strings: manufacturer, product */
183 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
184 init_utsname()->sysname, init_utsname()->release,
185 gadget->name);
186 status = usb_string_id(cdev);
187 if (status < 0)
188 return status;
189 strings_dev[STRING_MANUFACTURER_IDX].id = status;
190 msg_device_desc.iManufacturer = status;
191
192 status = usb_string_id(cdev);
193 if (status < 0)
194 return status;
195 strings_dev[STRING_PRODUCT_IDX].id = status;
196 msg_device_desc.iProduct = status;
197
198 status = usb_string_id(cdev);
199 if (status < 0)
200 return status;
201 strings_dev[STRING_CONFIGURATION_IDX].id = status;
202 msg_config_driver.iConfiguration = status;
203
204 /* register our second configuration */
205 status = usb_add_config(cdev, &msg_config_driver);
206 if (status < 0)
207 return status;
208
209 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
210 set_bit(0, &msg_registered);
211 return 0;
212}
213
214
215/****************************** Some noise ******************************/
216
217
218static struct usb_composite_driver msg_driver = {
219 .name = "g_mass_storage",
220 .dev = &msg_device_desc,
221 .strings = dev_strings,
222 .bind = msg_bind,
223};
224
225MODULE_DESCRIPTION(DRIVER_DESC);
226MODULE_AUTHOR("Michal Nazarewicz");
227MODULE_LICENSE("GPL");
228
229static int __init msg_init(void)
230{
231 return usb_composite_register(&msg_driver);
232}
233module_init(msg_init);
234
235static void msg_cleanup(void)
236{
237 if (test_and_clear_bit(0, &msg_registered))
238 usb_composite_unregister(&msg_driver);
239}
240module_exit(msg_cleanup);
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
new file mode 100644
index 000000000000..429560100b10
--- /dev/null
+++ b/drivers/usb/gadget/multi.c
@@ -0,0 +1,358 @@
1/*
2 * multi.c -- Multifunction Composite driver
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25#include <linux/kernel.h>
26#include <linux/utsname.h>
27
28
29#if defined USB_ETH_RNDIS
30# undef USB_ETH_RNDIS
31#endif
32#ifdef CONFIG_USB_ETH_RNDIS
33# define USB_ETH_RNDIS y
34#endif
35
36
37#define DRIVER_DESC "Multifunction Composite Gadget"
38#define DRIVER_VERSION "2009/07/21"
39
40/*-------------------------------------------------------------------------*/
41
42#define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
43#define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
44
45/*-------------------------------------------------------------------------*/
46
47/*
48 * kbuild is not very cooperative with respect to linking separately
49 * compiled library objects into one module. So for now we won't use
50 * separate compilation ... ensuring init/exit sections work to shrink
51 * the runtime footprint, and giving us at least some parts of what
52 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
53 */
54
55#include "composite.c"
56#include "usbstring.c"
57#include "config.c"
58#include "epautoconf.c"
59
60#include "u_serial.c"
61#include "f_acm.c"
62
63#include "f_ecm.c"
64#include "f_subset.c"
65#ifdef USB_ETH_RNDIS
66# include "f_rndis.c"
67# include "rndis.c"
68#endif
69#include "u_ether.c"
70
71#undef DBG /* u_ether.c has broken idea about macros */
72#undef VDBG /* so clean up after it */
73#undef ERROR
74#undef INFO
75#include "f_mass_storage.c"
76
77/*-------------------------------------------------------------------------*/
78
79static struct usb_device_descriptor device_desc = {
80 .bLength = sizeof device_desc,
81 .bDescriptorType = USB_DT_DEVICE,
82
83 .bcdUSB = cpu_to_le16(0x0200),
84
85 /* .bDeviceClass = USB_CLASS_COMM, */
86 /* .bDeviceSubClass = 0, */
87 /* .bDeviceProtocol = 0, */
88 .bDeviceClass = 0xEF,
89 .bDeviceSubClass = 2,
90 .bDeviceProtocol = 1,
91 /* .bMaxPacketSize0 = f(hardware) */
92
93 /* Vendor and product id can be overridden by module parameters. */
94 .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
95 .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
96 /* .bcdDevice = f(hardware) */
97 /* .iManufacturer = DYNAMIC */
98 /* .iProduct = DYNAMIC */
99 /* NO SERIAL NUMBER */
100 .bNumConfigurations = 1,
101};
102
103static struct usb_otg_descriptor otg_descriptor = {
104 .bLength = sizeof otg_descriptor,
105 .bDescriptorType = USB_DT_OTG,
106
107 /* REVISIT SRP-only hardware is possible, although
108 * it would not be called "OTG" ...
109 */
110 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
111};
112
113static const struct usb_descriptor_header *otg_desc[] = {
114 (struct usb_descriptor_header *) &otg_descriptor,
115 NULL,
116};
117
118
119/* string IDs are assigned dynamically */
120
121#define STRING_MANUFACTURER_IDX 0
122#define STRING_PRODUCT_IDX 1
123
124static char manufacturer[50];
125
126static struct usb_string strings_dev[] = {
127 [STRING_MANUFACTURER_IDX].s = manufacturer,
128 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
129 { } /* end of list */
130};
131
132static struct usb_gadget_strings stringtab_dev = {
133 .language = 0x0409, /* en-us */
134 .strings = strings_dev,
135};
136
137static struct usb_gadget_strings *dev_strings[] = {
138 &stringtab_dev,
139 NULL,
140};
141
142static u8 hostaddr[ETH_ALEN];
143
144
145
146/****************************** Configurations ******************************/
147
148static struct fsg_module_parameters mod_data = {
149 .stall = 1
150};
151FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
152
153static struct fsg_common *fsg_common;
154
155
156#ifdef USB_ETH_RNDIS
157
158static int __init rndis_do_config(struct usb_configuration *c)
159{
160 int ret;
161
162 if (gadget_is_otg(c->cdev->gadget)) {
163 c->descriptors = otg_desc;
164 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
165 }
166
167 ret = rndis_bind_config(c, hostaddr);
168 if (ret < 0)
169 return ret;
170
171 ret = acm_bind_config(c, 0);
172 if (ret < 0)
173 return ret;
174
175 ret = fsg_add(c->cdev, c, fsg_common);
176 if (ret < 0)
177 return ret;
178
179 return 0;
180}
181
182static struct usb_configuration rndis_config_driver = {
183 .label = "Multifunction Composite (RNDIS + MS + ACM)",
184 .bind = rndis_do_config,
185 .bConfigurationValue = 2,
186 /* .iConfiguration = DYNAMIC */
187 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
188};
189
190#endif
191
192#ifdef CONFIG_USB_G_MULTI_CDC
193
194static int __init cdc_do_config(struct usb_configuration *c)
195{
196 int ret;
197
198 if (gadget_is_otg(c->cdev->gadget)) {
199 c->descriptors = otg_desc;
200 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
201 }
202
203 ret = ecm_bind_config(c, hostaddr);
204 if (ret < 0)
205 return ret;
206
207 ret = acm_bind_config(c, 0);
208 if (ret < 0)
209 return ret;
210
211 ret = fsg_add(c->cdev, c, fsg_common);
212 if (ret < 0)
213 return ret;
214 if (ret < 0)
215 return ret;
216
217 return 0;
218}
219
220static struct usb_configuration cdc_config_driver = {
221 .label = "Multifunction Composite (CDC + MS + ACM)",
222 .bind = cdc_do_config,
223 .bConfigurationValue = 1,
224 /* .iConfiguration = DYNAMIC */
225 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
226};
227
228#endif
229
230
231
232/****************************** Gadget Bind ******************************/
233
234
235static int __init multi_bind(struct usb_composite_dev *cdev)
236{
237 struct usb_gadget *gadget = cdev->gadget;
238 int status, gcnum;
239
240 if (!can_support_ecm(cdev->gadget)) {
241 dev_err(&gadget->dev, "controller '%s' not usable\n",
242 gadget->name);
243 return -EINVAL;
244 }
245
246 /* set up network link layer */
247 status = gether_setup(cdev->gadget, hostaddr);
248 if (status < 0)
249 return status;
250
251 /* set up serial link layer */
252 status = gserial_setup(cdev->gadget, 1);
253 if (status < 0)
254 goto fail0;
255
256 /* set up mass storage function */
257 fsg_common = fsg_common_from_params(0, cdev, &mod_data);
258 if (IS_ERR(fsg_common)) {
259 status = PTR_ERR(fsg_common);
260 goto fail1;
261 }
262
263
264 gcnum = usb_gadget_controller_number(gadget);
265 if (gcnum >= 0)
266 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
267 else {
268 /* We assume that can_support_ecm() tells the truth;
269 * but if the controller isn't recognized at all then
270 * that assumption is a bit more likely to be wrong.
271 */
272 WARNING(cdev, "controller '%s' not recognized\n",
273 gadget->name);
274 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
275 }
276
277
278 /* Allocate string descriptor numbers ... note that string
279 * contents can be overridden by the composite_dev glue.
280 */
281
282 /* device descriptor strings: manufacturer, product */
283 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
284 init_utsname()->sysname, init_utsname()->release,
285 gadget->name);
286 status = usb_string_id(cdev);
287 if (status < 0)
288 goto fail2;
289 strings_dev[STRING_MANUFACTURER_IDX].id = status;
290 device_desc.iManufacturer = status;
291
292 status = usb_string_id(cdev);
293 if (status < 0)
294 goto fail2;
295 strings_dev[STRING_PRODUCT_IDX].id = status;
296 device_desc.iProduct = status;
297
298#ifdef USB_ETH_RNDIS
299 /* register our first configuration */
300 status = usb_add_config(cdev, &rndis_config_driver);
301 if (status < 0)
302 goto fail2;
303#endif
304
305#ifdef CONFIG_USB_G_MULTI_CDC
306 /* register our second configuration */
307 status = usb_add_config(cdev, &cdc_config_driver);
308 if (status < 0)
309 goto fail2;
310#endif
311
312 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
313 fsg_common_put(fsg_common);
314 return 0;
315
316fail2:
317 fsg_common_put(fsg_common);
318fail1:
319 gserial_cleanup();
320fail0:
321 gether_cleanup();
322 return status;
323}
324
325static int __exit multi_unbind(struct usb_composite_dev *cdev)
326{
327 gserial_cleanup();
328 gether_cleanup();
329 return 0;
330}
331
332
333/****************************** Some noise ******************************/
334
335
336static struct usb_composite_driver multi_driver = {
337 .name = "g_multi",
338 .dev = &device_desc,
339 .strings = dev_strings,
340 .bind = multi_bind,
341 .unbind = __exit_p(multi_unbind),
342};
343
344MODULE_DESCRIPTION(DRIVER_DESC);
345MODULE_AUTHOR("Michal Nazarewicz");
346MODULE_LICENSE("GPL");
347
348static int __init g_multi_init(void)
349{
350 return usb_composite_register(&multi_driver);
351}
352module_init(g_multi_init);
353
354static void __exit g_multi_cleanup(void)
355{
356 usb_composite_unregister(&multi_driver);
357}
358module_exit(g_multi_cleanup);
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
new file mode 100644
index 000000000000..868d8ee86756
--- /dev/null
+++ b/drivers/usb/gadget/storage_common.c
@@ -0,0 +1,778 @@
1/*
2 * storage_common.c -- Common definitions for mass storage functionality
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23
24/*
25 * This file requires the following identifiers used in USB strings to
26 * be defined (each of type pointer to char):
27 * - fsg_string_manufacturer -- name of the manufacturer
28 * - fsg_string_product -- name of the product
29 * - fsg_string_serial -- product's serial
30 * - fsg_string_config -- name of the configuration
31 * - fsg_string_interface -- name of the interface
32 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
33 * macro is defined prior to including this file.
34 */
35
36/*
37 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
38 * fsg_hs_intr_in_desc objects as well as
39 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
40 * macros are not defined.
41 *
42 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
43 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
44 * defined (as well as corresponding entries in string tables are
45 * missing) and FSG_STRING_INTERFACE has value of zero.
46 *
47 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
48 */
49
50/*
51 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
52 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
53 * characters rather then a pointer to void.
54 */
55
56
57#include <asm/unaligned.h>
58
59
60/* Thanks to NetChip Technologies for donating this product ID.
61 *
62 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
63 * Instead: allocate your own, using normal USB-IF procedures. */
64#define FSG_VENDOR_ID 0x0525 /* NetChip */
65#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
66
67
68/*-------------------------------------------------------------------------*/
69
70
71#ifndef DEBUG
72#undef VERBOSE_DEBUG
73#undef DUMP_MSGS
74#endif /* !DEBUG */
75
76#ifdef VERBOSE_DEBUG
77#define VLDBG LDBG
78#else
79#define VLDBG(lun, fmt, args...) do { } while (0)
80#endif /* VERBOSE_DEBUG */
81
82#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
83#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
84#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
85#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
86
87/* Keep those macros in sync with thos in
88 * include/linux/ubs/composite.h or else GCC will complain. If they
89 * are identical (the same names of arguments, white spaces in the
90 * same places) GCC will allow redefinition otherwise (even if some
91 * white space is removed or added) warning will be issued. No
92 * checking if those symbols is defined is performed because warning
93 * is desired when those macros were defined by someone else to mean
94 * something else. */
95#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
96#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
97#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
98#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
99#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
100
101
102
103#ifdef DUMP_MSGS
104
105# define dump_msg(fsg, /* const char * */ label, \
106 /* const u8 * */ buf, /* unsigned */ length) do { \
107 if (length < 512) { \
108 DBG(fsg, "%s, length %u:\n", label, length); \
109 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
110 16, 1, buf, length, 0); \
111 } \
112} while (0)
113
114# define dump_cdb(fsg) do { } while (0)
115
116#else
117
118# define dump_msg(fsg, /* const char * */ label, \
119 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
120
121# ifdef VERBOSE_DEBUG
122
123# define dump_cdb(fsg) \
124 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
125 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
126
127# else
128
129# define dump_cdb(fsg) do { } while (0)
130
131# endif /* VERBOSE_DEBUG */
132
133#endif /* DUMP_MSGS */
134
135
136
137
138
139/*-------------------------------------------------------------------------*/
140
141/* SCSI device types */
142#define TYPE_DISK 0x00
143#define TYPE_CDROM 0x05
144
145/* USB protocol value = the transport method */
146#define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
147#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
148#define USB_PR_BULK 0x50 /* Bulk-only */
149
150/* USB subclass value = the protocol encapsulation */
151#define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
152#define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
153#define USB_SC_QIC 0x03 /* QIC-157 (tape) */
154#define USB_SC_UFI 0x04 /* UFI (floppy) */
155#define USB_SC_8070 0x05 /* SFF-8070i (removable) */
156#define USB_SC_SCSI 0x06 /* Transparent SCSI */
157
158/* Bulk-only data structures */
159
160/* Command Block Wrapper */
161struct fsg_bulk_cb_wrap {
162 __le32 Signature; /* Contains 'USBC' */
163 u32 Tag; /* Unique per command id */
164 __le32 DataTransferLength; /* Size of the data */
165 u8 Flags; /* Direction in bit 7 */
166 u8 Lun; /* LUN (normally 0) */
167 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
168 u8 CDB[16]; /* Command Data Block */
169};
170
171#define USB_BULK_CB_WRAP_LEN 31
172#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
173#define USB_BULK_IN_FLAG 0x80
174
175/* Command Status Wrapper */
176struct bulk_cs_wrap {
177 __le32 Signature; /* Should = 'USBS' */
178 u32 Tag; /* Same as original command */
179 __le32 Residue; /* Amount not transferred */
180 u8 Status; /* See below */
181};
182
183#define USB_BULK_CS_WRAP_LEN 13
184#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
185#define USB_STATUS_PASS 0
186#define USB_STATUS_FAIL 1
187#define USB_STATUS_PHASE_ERROR 2
188
189/* Bulk-only class specific requests */
190#define USB_BULK_RESET_REQUEST 0xff
191#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
192
193
194/* CBI Interrupt data structure */
195struct interrupt_data {
196 u8 bType;
197 u8 bValue;
198};
199
200#define CBI_INTERRUPT_DATA_LEN 2
201
202/* CBI Accept Device-Specific Command request */
203#define USB_CBI_ADSC_REQUEST 0x00
204
205
206/* Length of a SCSI Command Data Block */
207#define MAX_COMMAND_SIZE 16
208
209/* SCSI commands that we recognize */
210#define SC_FORMAT_UNIT 0x04
211#define SC_INQUIRY 0x12
212#define SC_MODE_SELECT_6 0x15
213#define SC_MODE_SELECT_10 0x55
214#define SC_MODE_SENSE_6 0x1a
215#define SC_MODE_SENSE_10 0x5a
216#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
217#define SC_READ_6 0x08
218#define SC_READ_10 0x28
219#define SC_READ_12 0xa8
220#define SC_READ_CAPACITY 0x25
221#define SC_READ_FORMAT_CAPACITIES 0x23
222#define SC_READ_HEADER 0x44
223#define SC_READ_TOC 0x43
224#define SC_RELEASE 0x17
225#define SC_REQUEST_SENSE 0x03
226#define SC_RESERVE 0x16
227#define SC_SEND_DIAGNOSTIC 0x1d
228#define SC_START_STOP_UNIT 0x1b
229#define SC_SYNCHRONIZE_CACHE 0x35
230#define SC_TEST_UNIT_READY 0x00
231#define SC_VERIFY 0x2f
232#define SC_WRITE_6 0x0a
233#define SC_WRITE_10 0x2a
234#define SC_WRITE_12 0xaa
235
236/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
237#define SS_NO_SENSE 0
238#define SS_COMMUNICATION_FAILURE 0x040800
239#define SS_INVALID_COMMAND 0x052000
240#define SS_INVALID_FIELD_IN_CDB 0x052400
241#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
242#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
243#define SS_MEDIUM_NOT_PRESENT 0x023a00
244#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
245#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
246#define SS_RESET_OCCURRED 0x062900
247#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
248#define SS_UNRECOVERED_READ_ERROR 0x031100
249#define SS_WRITE_ERROR 0x030c02
250#define SS_WRITE_PROTECTED 0x072700
251
252#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
253#define ASC(x) ((u8) ((x) >> 8))
254#define ASCQ(x) ((u8) (x))
255
256
257/*-------------------------------------------------------------------------*/
258
259
260struct fsg_lun {
261 struct file *filp;
262 loff_t file_length;
263 loff_t num_sectors;
264
265 unsigned int initially_ro:1;
266 unsigned int ro:1;
267 unsigned int removable:1;
268 unsigned int cdrom:1;
269 unsigned int prevent_medium_removal:1;
270 unsigned int registered:1;
271 unsigned int info_valid:1;
272
273 u32 sense_data;
274 u32 sense_data_info;
275 u32 unit_attention_data;
276
277 struct device dev;
278};
279
280#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
281
282static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
283{
284 return container_of(dev, struct fsg_lun, dev);
285}
286
287
288/* Big enough to hold our biggest descriptor */
289#define EP0_BUFSIZE 256
290#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
291
292/* Number of buffers we will use. 2 is enough for double-buffering */
293#define FSG_NUM_BUFFERS 2
294
295/* Default size of buffer length. */
296#define FSG_BUFLEN ((u32)16384)
297
298/* Maximal number of LUNs supported in mass storage function */
299#define FSG_MAX_LUNS 8
300
301enum fsg_buffer_state {
302 BUF_STATE_EMPTY = 0,
303 BUF_STATE_FULL,
304 BUF_STATE_BUSY
305};
306
307struct fsg_buffhd {
308#ifdef FSG_BUFFHD_STATIC_BUFFER
309 char buf[FSG_BUFLEN];
310#else
311 void *buf;
312#endif
313 enum fsg_buffer_state state;
314 struct fsg_buffhd *next;
315
316 /* The NetChip 2280 is faster, and handles some protocol faults
317 * better, if we don't submit any short bulk-out read requests.
318 * So we will record the intended request length here. */
319 unsigned int bulk_out_intended_length;
320
321 struct usb_request *inreq;
322 int inreq_busy;
323 struct usb_request *outreq;
324 int outreq_busy;
325};
326
327enum fsg_state {
328 /* This one isn't used anywhere */
329 FSG_STATE_COMMAND_PHASE = -10,
330 FSG_STATE_DATA_PHASE,
331 FSG_STATE_STATUS_PHASE,
332
333 FSG_STATE_IDLE = 0,
334 FSG_STATE_ABORT_BULK_OUT,
335 FSG_STATE_RESET,
336 FSG_STATE_INTERFACE_CHANGE,
337 FSG_STATE_CONFIG_CHANGE,
338 FSG_STATE_DISCONNECT,
339 FSG_STATE_EXIT,
340 FSG_STATE_TERMINATED
341};
342
343enum data_direction {
344 DATA_DIR_UNKNOWN = 0,
345 DATA_DIR_FROM_HOST,
346 DATA_DIR_TO_HOST,
347 DATA_DIR_NONE
348};
349
350
351/*-------------------------------------------------------------------------*/
352
353
354static inline u32 get_unaligned_be24(u8 *buf)
355{
356 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
357}
358
359
360/*-------------------------------------------------------------------------*/
361
362
363enum {
364#ifndef FSG_NO_DEVICE_STRINGS
365 FSG_STRING_MANUFACTURER = 1,
366 FSG_STRING_PRODUCT,
367 FSG_STRING_SERIAL,
368 FSG_STRING_CONFIG,
369#endif
370 FSG_STRING_INTERFACE
371};
372
373
374#ifndef FSG_NO_OTG
375static struct usb_otg_descriptor
376fsg_otg_desc = {
377 .bLength = sizeof fsg_otg_desc,
378 .bDescriptorType = USB_DT_OTG,
379
380 .bmAttributes = USB_OTG_SRP,
381};
382#endif
383
384/* There is only one interface. */
385
386static struct usb_interface_descriptor
387fsg_intf_desc = {
388 .bLength = sizeof fsg_intf_desc,
389 .bDescriptorType = USB_DT_INTERFACE,
390
391 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
392 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
393 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
394 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
395 .iInterface = FSG_STRING_INTERFACE,
396};
397
398/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
399 * and interrupt-in. */
400
401static struct usb_endpoint_descriptor
402fsg_fs_bulk_in_desc = {
403 .bLength = USB_DT_ENDPOINT_SIZE,
404 .bDescriptorType = USB_DT_ENDPOINT,
405
406 .bEndpointAddress = USB_DIR_IN,
407 .bmAttributes = USB_ENDPOINT_XFER_BULK,
408 /* wMaxPacketSize set by autoconfiguration */
409};
410
411static struct usb_endpoint_descriptor
412fsg_fs_bulk_out_desc = {
413 .bLength = USB_DT_ENDPOINT_SIZE,
414 .bDescriptorType = USB_DT_ENDPOINT,
415
416 .bEndpointAddress = USB_DIR_OUT,
417 .bmAttributes = USB_ENDPOINT_XFER_BULK,
418 /* wMaxPacketSize set by autoconfiguration */
419};
420
421#ifndef FSG_NO_INTR_EP
422
423static struct usb_endpoint_descriptor
424fsg_fs_intr_in_desc = {
425 .bLength = USB_DT_ENDPOINT_SIZE,
426 .bDescriptorType = USB_DT_ENDPOINT,
427
428 .bEndpointAddress = USB_DIR_IN,
429 .bmAttributes = USB_ENDPOINT_XFER_INT,
430 .wMaxPacketSize = cpu_to_le16(2),
431 .bInterval = 32, /* frames -> 32 ms */
432};
433
434#ifndef FSG_NO_OTG
435# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
436#else
437# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
438#endif
439
440#endif
441
442static struct usb_descriptor_header *fsg_fs_function[] = {
443#ifndef FSG_NO_OTG
444 (struct usb_descriptor_header *) &fsg_otg_desc,
445#endif
446 (struct usb_descriptor_header *) &fsg_intf_desc,
447 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
448 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
449#ifndef FSG_NO_INTR_EP
450 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
451#endif
452 NULL,
453};
454
455
456/*
457 * USB 2.0 devices need to expose both high speed and full speed
458 * descriptors, unless they only run at full speed.
459 *
460 * That means alternate endpoint descriptors (bigger packets)
461 * and a "device qualifier" ... plus more construction options
462 * for the config descriptor.
463 */
464static struct usb_endpoint_descriptor
465fsg_hs_bulk_in_desc = {
466 .bLength = USB_DT_ENDPOINT_SIZE,
467 .bDescriptorType = USB_DT_ENDPOINT,
468
469 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
470 .bmAttributes = USB_ENDPOINT_XFER_BULK,
471 .wMaxPacketSize = cpu_to_le16(512),
472};
473
474static struct usb_endpoint_descriptor
475fsg_hs_bulk_out_desc = {
476 .bLength = USB_DT_ENDPOINT_SIZE,
477 .bDescriptorType = USB_DT_ENDPOINT,
478
479 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
480 .bmAttributes = USB_ENDPOINT_XFER_BULK,
481 .wMaxPacketSize = cpu_to_le16(512),
482 .bInterval = 1, /* NAK every 1 uframe */
483};
484
485#ifndef FSG_NO_INTR_EP
486
487static struct usb_endpoint_descriptor
488fsg_hs_intr_in_desc = {
489 .bLength = USB_DT_ENDPOINT_SIZE,
490 .bDescriptorType = USB_DT_ENDPOINT,
491
492 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
493 .bmAttributes = USB_ENDPOINT_XFER_INT,
494 .wMaxPacketSize = cpu_to_le16(2),
495 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
496};
497
498#ifndef FSG_NO_OTG
499# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
500#else
501# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
502#endif
503
504#endif
505
506static struct usb_descriptor_header *fsg_hs_function[] = {
507#ifndef FSG_NO_OTG
508 (struct usb_descriptor_header *) &fsg_otg_desc,
509#endif
510 (struct usb_descriptor_header *) &fsg_intf_desc,
511 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
512 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
513#ifndef FSG_NO_INTR_EP
514 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
515#endif
516 NULL,
517};
518
519/* Maxpacket and other transfer characteristics vary by speed. */
520static struct usb_endpoint_descriptor *
521fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
522 struct usb_endpoint_descriptor *hs)
523{
524 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
525 return hs;
526 return fs;
527}
528
529
530/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
531static struct usb_string fsg_strings[] = {
532#ifndef FSG_NO_DEVICE_STRINGS
533 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
534 {FSG_STRING_PRODUCT, fsg_string_product},
535 {FSG_STRING_SERIAL, fsg_string_serial},
536 {FSG_STRING_CONFIG, fsg_string_config},
537#endif
538 {FSG_STRING_INTERFACE, fsg_string_interface},
539 {}
540};
541
542static struct usb_gadget_strings fsg_stringtab = {
543 .language = 0x0409, /* en-us */
544 .strings = fsg_strings,
545};
546
547
548 /*-------------------------------------------------------------------------*/
549
550/* If the next two routines are called while the gadget is registered,
551 * the caller must own fsg->filesem for writing. */
552
553static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
554{
555 int ro;
556 struct file *filp = NULL;
557 int rc = -EINVAL;
558 struct inode *inode = NULL;
559 loff_t size;
560 loff_t num_sectors;
561 loff_t min_sectors;
562
563 /* R/W if we can, R/O if we must */
564 ro = curlun->initially_ro;
565 if (!ro) {
566 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
567 if (-EROFS == PTR_ERR(filp))
568 ro = 1;
569 }
570 if (ro)
571 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
572 if (IS_ERR(filp)) {
573 LINFO(curlun, "unable to open backing file: %s\n", filename);
574 return PTR_ERR(filp);
575 }
576
577 if (!(filp->f_mode & FMODE_WRITE))
578 ro = 1;
579
580 if (filp->f_path.dentry)
581 inode = filp->f_path.dentry->d_inode;
582 if (inode && S_ISBLK(inode->i_mode)) {
583 if (bdev_read_only(inode->i_bdev))
584 ro = 1;
585 } else if (!inode || !S_ISREG(inode->i_mode)) {
586 LINFO(curlun, "invalid file type: %s\n", filename);
587 goto out;
588 }
589
590 /* If we can't read the file, it's no good.
591 * If we can't write the file, use it read-only. */
592 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
593 LINFO(curlun, "file not readable: %s\n", filename);
594 goto out;
595 }
596 if (!(filp->f_op->write || filp->f_op->aio_write))
597 ro = 1;
598
599 size = i_size_read(inode->i_mapping->host);
600 if (size < 0) {
601 LINFO(curlun, "unable to find file size: %s\n", filename);
602 rc = (int) size;
603 goto out;
604 }
605 num_sectors = size >> 9; /* File size in 512-byte blocks */
606 min_sectors = 1;
607 if (curlun->cdrom) {
608 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
609 min_sectors = 300*4; /* Smallest track is 300 frames */
610 if (num_sectors >= 256*60*75*4) {
611 num_sectors = (256*60*75 - 1) * 4;
612 LINFO(curlun, "file too big: %s\n", filename);
613 LINFO(curlun, "using only first %d blocks\n",
614 (int) num_sectors);
615 }
616 }
617 if (num_sectors < min_sectors) {
618 LINFO(curlun, "file too small: %s\n", filename);
619 rc = -ETOOSMALL;
620 goto out;
621 }
622
623 get_file(filp);
624 curlun->ro = ro;
625 curlun->filp = filp;
626 curlun->file_length = size;
627 curlun->num_sectors = num_sectors;
628 LDBG(curlun, "open backing file: %s\n", filename);
629 rc = 0;
630
631out:
632 filp_close(filp, current->files);
633 return rc;
634}
635
636
637static void fsg_lun_close(struct fsg_lun *curlun)
638{
639 if (curlun->filp) {
640 LDBG(curlun, "close backing file\n");
641 fput(curlun->filp);
642 curlun->filp = NULL;
643 }
644}
645
646
647/*-------------------------------------------------------------------------*/
648
649/* Sync the file data, don't bother with the metadata.
650 * This code was copied from fs/buffer.c:sys_fdatasync(). */
651static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
652{
653 struct file *filp = curlun->filp;
654
655 if (curlun->ro || !filp)
656 return 0;
657 return vfs_fsync(filp, filp->f_path.dentry, 1);
658}
659
660static void store_cdrom_address(u8 *dest, int msf, u32 addr)
661{
662 if (msf) {
663 /* Convert to Minutes-Seconds-Frames */
664 addr >>= 2; /* Convert to 2048-byte frames */
665 addr += 2*75; /* Lead-in occupies 2 seconds */
666 dest[3] = addr % 75; /* Frames */
667 addr /= 75;
668 dest[2] = addr % 60; /* Seconds */
669 addr /= 60;
670 dest[1] = addr; /* Minutes */
671 dest[0] = 0; /* Reserved */
672 } else {
673 /* Absolute sector */
674 put_unaligned_be32(addr, dest);
675 }
676}
677
678
679/*-------------------------------------------------------------------------*/
680
681
682static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
683 char *buf)
684{
685 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
686
687 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
688 ? curlun->ro
689 : curlun->initially_ro);
690}
691
692static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
693 char *buf)
694{
695 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
696 struct rw_semaphore *filesem = dev_get_drvdata(dev);
697 char *p;
698 ssize_t rc;
699
700 down_read(filesem);
701 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
702 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
703 if (IS_ERR(p))
704 rc = PTR_ERR(p);
705 else {
706 rc = strlen(p);
707 memmove(buf, p, rc);
708 buf[rc] = '\n'; /* Add a newline */
709 buf[++rc] = 0;
710 }
711 } else { /* No file, return 0 bytes */
712 *buf = 0;
713 rc = 0;
714 }
715 up_read(filesem);
716 return rc;
717}
718
719
720static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
721 const char *buf, size_t count)
722{
723 ssize_t rc = count;
724 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
725 struct rw_semaphore *filesem = dev_get_drvdata(dev);
726 int i;
727
728 if (sscanf(buf, "%d", &i) != 1)
729 return -EINVAL;
730
731 /* Allow the write-enable status to change only while the backing file
732 * is closed. */
733 down_read(filesem);
734 if (fsg_lun_is_open(curlun)) {
735 LDBG(curlun, "read-only status change prevented\n");
736 rc = -EBUSY;
737 } else {
738 curlun->ro = !!i;
739 curlun->initially_ro = !!i;
740 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
741 }
742 up_read(filesem);
743 return rc;
744}
745
746static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count)
748{
749 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
750 struct rw_semaphore *filesem = dev_get_drvdata(dev);
751 int rc = 0;
752
753 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
754 LDBG(curlun, "eject attempt prevented\n");
755 return -EBUSY; /* "Door is locked" */
756 }
757
758 /* Remove a trailing newline */
759 if (count > 0 && buf[count-1] == '\n')
760 ((char *) buf)[count-1] = 0; /* Ugh! */
761
762 /* Eject current medium */
763 down_write(filesem);
764 if (fsg_lun_is_open(curlun)) {
765 fsg_lun_close(curlun);
766 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
767 }
768
769 /* Load new medium */
770 if (count > 0 && buf[0]) {
771 rc = fsg_lun_open(curlun, buf);
772 if (rc == 0)
773 curlun->unit_attention_data =
774 SS_NOT_READY_TO_READY_TRANSITION;
775 }
776 up_write(filesem);
777 return (rc < 0 ? rc : count);
778}
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 91b39ffdf6ea..fd55f450bc0e 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -112,7 +112,7 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
112int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 112int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
113int eem_bind_config(struct usb_configuration *c); 113int eem_bind_config(struct usb_configuration *c);
114 114
115#ifdef CONFIG_USB_ETH_RNDIS 115#ifdef USB_ETH_RNDIS
116 116
117int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 117int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
118 118
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index 9b43b226817f..2678a1624fcc 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -90,14 +90,25 @@ config USB_EHCI_TT_NEWSCHED
90 90
91config USB_EHCI_BIG_ENDIAN_MMIO 91config USB_EHCI_BIG_ENDIAN_MMIO
92 bool 92 bool
93 depends on USB_EHCI_HCD && (PPC_CELLEB || PPC_PS3 || 440EPX || ARCH_IXP4XX) 93 depends on USB_EHCI_HCD && (PPC_CELLEB || PPC_PS3 || 440EPX || ARCH_IXP4XX || XPS_USB_HCD_XILINX)
94 default y 94 default y
95 95
96config USB_EHCI_BIG_ENDIAN_DESC 96config USB_EHCI_BIG_ENDIAN_DESC
97 bool 97 bool
98 depends on USB_EHCI_HCD && (440EPX || ARCH_IXP4XX) 98 depends on USB_EHCI_HCD && (440EPX || ARCH_IXP4XX || XPS_USB_HCD_XILINX)
99 default y 99 default y
100 100
101config XPS_USB_HCD_XILINX
102 bool "Use Xilinx usb host EHCI controller core"
103 depends on USB_EHCI_HCD && (PPC32 || MICROBLAZE)
104 select USB_EHCI_BIG_ENDIAN_DESC
105 select USB_EHCI_BIG_ENDIAN_MMIO
106 ---help---
107 Xilinx xps USB host controller core is EHCI compilant and has
108 transaction translator built-in. It can be configured to either
109 support both high speed and full speed devices, or high speed
110 devices only.
111
101config USB_EHCI_FSL 112config USB_EHCI_FSL
102 bool "Support for Freescale on-chip EHCI USB controller" 113 bool "Support for Freescale on-chip EHCI USB controller"
103 depends on USB_EHCI_HCD && FSL_SOC 114 depends on USB_EHCI_HCD && FSL_SOC
@@ -105,6 +116,13 @@ config USB_EHCI_FSL
105 ---help--- 116 ---help---
106 Variation of ARC USB block used in some Freescale chips. 117 Variation of ARC USB block used in some Freescale chips.
107 118
119config USB_EHCI_MXC
120 bool "Support for Freescale on-chip EHCI USB controller"
121 depends on USB_EHCI_HCD && ARCH_MXC
122 select USB_EHCI_ROOT_HUB_TT
123 ---help---
124 Variation of ARC USB block used in some Freescale chips.
125
108config USB_EHCI_HCD_PPC_OF 126config USB_EHCI_HCD_PPC_OF
109 bool "EHCI support for PPC USB controller on OF platform bus" 127 bool "EHCI support for PPC USB controller on OF platform bus"
110 depends on USB_EHCI_HCD && PPC_OF 128 depends on USB_EHCI_HCD && PPC_OF
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index d8f4aaa616f2..5859522d6edd 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -549,7 +549,7 @@ static int ehci_init(struct usb_hcd *hcd)
549 /* controllers may cache some of the periodic schedule ... */ 549 /* controllers may cache some of the periodic schedule ... */
550 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params); 550 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
551 if (HCC_ISOC_CACHE(hcc_params)) // full frame cache 551 if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
552 ehci->i_thresh = 8; 552 ehci->i_thresh = 2 + 8;
553 else // N microframes cached 553 else // N microframes cached
554 ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params); 554 ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
555 555
@@ -605,6 +605,8 @@ static int ehci_init(struct usb_hcd *hcd)
605 } 605 }
606 ehci->command = temp; 606 ehci->command = temp;
607 607
608 /* Accept arbitrarily long scatter-gather lists */
609 hcd->self.sg_tablesize = ~0;
608 return 0; 610 return 0;
609} 611}
610 612
@@ -1105,11 +1107,21 @@ MODULE_LICENSE ("GPL");
1105#define PLATFORM_DRIVER ehci_fsl_driver 1107#define PLATFORM_DRIVER ehci_fsl_driver
1106#endif 1108#endif
1107 1109
1110#ifdef CONFIG_USB_EHCI_MXC
1111#include "ehci-mxc.c"
1112#define PLATFORM_DRIVER ehci_mxc_driver
1113#endif
1114
1108#ifdef CONFIG_SOC_AU1200 1115#ifdef CONFIG_SOC_AU1200
1109#include "ehci-au1xxx.c" 1116#include "ehci-au1xxx.c"
1110#define PLATFORM_DRIVER ehci_hcd_au1xxx_driver 1117#define PLATFORM_DRIVER ehci_hcd_au1xxx_driver
1111#endif 1118#endif
1112 1119
1120#ifdef CONFIG_ARCH_OMAP34XX
1121#include "ehci-omap.c"
1122#define PLATFORM_DRIVER ehci_hcd_omap_driver
1123#endif
1124
1113#ifdef CONFIG_PPC_PS3 1125#ifdef CONFIG_PPC_PS3
1114#include "ehci-ps3.c" 1126#include "ehci-ps3.c"
1115#define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver 1127#define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
@@ -1120,6 +1132,11 @@ MODULE_LICENSE ("GPL");
1120#define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver 1132#define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
1121#endif 1133#endif
1122 1134
1135#ifdef CONFIG_XPS_USB_HCD_XILINX
1136#include "ehci-xilinx-of.c"
1137#define OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
1138#endif
1139
1123#ifdef CONFIG_PLAT_ORION 1140#ifdef CONFIG_PLAT_ORION
1124#include "ehci-orion.c" 1141#include "ehci-orion.c"
1125#define PLATFORM_DRIVER ehci_orion_driver 1142#define PLATFORM_DRIVER ehci_orion_driver
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 1b6f1c0e5cee..2c6571c05f35 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -236,7 +236,7 @@ static int ehci_bus_resume (struct usb_hcd *hcd)
236 } 236 }
237 237
238 if (unlikely(ehci->debug)) { 238 if (unlikely(ehci->debug)) {
239 if (ehci->debug && !dbgp_reset_prep()) 239 if (!dbgp_reset_prep())
240 ehci->debug = NULL; 240 ehci->debug = NULL;
241 else 241 else
242 dbgp_external_startup(); 242 dbgp_external_startup();
diff --git a/drivers/usb/host/ehci-mxc.c b/drivers/usb/host/ehci-mxc.c
new file mode 100644
index 000000000000..35c56f40bdbb
--- /dev/null
+++ b/drivers/usb/host/ehci-mxc.c
@@ -0,0 +1,296 @@
1/*
2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/platform_device.h>
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/usb/otg.h>
24
25#include <mach/mxc_ehci.h>
26
27#define ULPI_VIEWPORT_OFFSET 0x170
28#define PORTSC_OFFSET 0x184
29#define USBMODE_OFFSET 0x1a8
30#define USBMODE_CM_HOST 3
31
32struct ehci_mxc_priv {
33 struct clk *usbclk, *ahbclk;
34 struct usb_hcd *hcd;
35};
36
37/* called during probe() after chip reset completes */
38static int ehci_mxc_setup(struct usb_hcd *hcd)
39{
40 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
41 int retval;
42
43 /* EHCI registers start at offset 0x100 */
44 ehci->caps = hcd->regs + 0x100;
45 ehci->regs = hcd->regs + 0x100 +
46 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
47 dbg_hcs_params(ehci, "reset");
48 dbg_hcc_params(ehci, "reset");
49
50 /* cache this readonly data; minimize chip reads */
51 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
52
53 retval = ehci_halt(ehci);
54 if (retval)
55 return retval;
56
57 /* data structure init */
58 retval = ehci_init(hcd);
59 if (retval)
60 return retval;
61
62 hcd->has_tt = 1;
63
64 ehci->sbrn = 0x20;
65
66 ehci_reset(ehci);
67
68 ehci_port_power(ehci, 0);
69 return 0;
70}
71
72static const struct hc_driver ehci_mxc_hc_driver = {
73 .description = hcd_name,
74 .product_desc = "Freescale On-Chip EHCI Host Controller",
75 .hcd_priv_size = sizeof(struct ehci_hcd),
76
77 /*
78 * generic hardware linkage
79 */
80 .irq = ehci_irq,
81 .flags = HCD_USB2 | HCD_MEMORY,
82
83 /*
84 * basic lifecycle operations
85 */
86 .reset = ehci_mxc_setup,
87 .start = ehci_run,
88 .stop = ehci_stop,
89 .shutdown = ehci_shutdown,
90
91 /*
92 * managing i/o requests and associated device resources
93 */
94 .urb_enqueue = ehci_urb_enqueue,
95 .urb_dequeue = ehci_urb_dequeue,
96 .endpoint_disable = ehci_endpoint_disable,
97
98 /*
99 * scheduling support
100 */
101 .get_frame_number = ehci_get_frame,
102
103 /*
104 * root hub support
105 */
106 .hub_status_data = ehci_hub_status_data,
107 .hub_control = ehci_hub_control,
108 .bus_suspend = ehci_bus_suspend,
109 .bus_resume = ehci_bus_resume,
110 .relinquish_port = ehci_relinquish_port,
111 .port_handed_over = ehci_port_handed_over,
112};
113
114static int ehci_mxc_drv_probe(struct platform_device *pdev)
115{
116 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
117 struct usb_hcd *hcd;
118 struct resource *res;
119 int irq, ret, temp;
120 struct ehci_mxc_priv *priv;
121 struct device *dev = &pdev->dev;
122
123 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
124
125 if (!pdata) {
126 dev_err(dev, "No platform data given, bailing out.\n");
127 return -EINVAL;
128 }
129
130 irq = platform_get_irq(pdev, 0);
131
132 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
133 if (!hcd)
134 return -ENOMEM;
135
136 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
137 if (!priv) {
138 ret = -ENOMEM;
139 goto err_alloc;
140 }
141
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!res) {
144 dev_err(dev, "Found HC with no register addr. Check setup!\n");
145 ret = -ENODEV;
146 goto err_get_resource;
147 }
148
149 hcd->rsrc_start = res->start;
150 hcd->rsrc_len = resource_size(res);
151
152 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
153 dev_dbg(dev, "controller already in use\n");
154 ret = -EBUSY;
155 goto err_request_mem;
156 }
157
158 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
159 if (!hcd->regs) {
160 dev_err(dev, "error mapping memory\n");
161 ret = -EFAULT;
162 goto err_ioremap;
163 }
164
165 /* enable clocks */
166 priv->usbclk = clk_get(dev, "usb");
167 if (IS_ERR(priv->usbclk)) {
168 ret = PTR_ERR(priv->usbclk);
169 goto err_clk;
170 }
171 clk_enable(priv->usbclk);
172
173 if (!cpu_is_mx35()) {
174 priv->ahbclk = clk_get(dev, "usb_ahb");
175 if (IS_ERR(priv->ahbclk)) {
176 ret = PTR_ERR(priv->ahbclk);
177 goto err_clk_ahb;
178 }
179 clk_enable(priv->ahbclk);
180 }
181
182 /* set USBMODE to host mode */
183 temp = readl(hcd->regs + USBMODE_OFFSET);
184 writel(temp | USBMODE_CM_HOST, hcd->regs + USBMODE_OFFSET);
185
186 /* set up the PORTSCx register */
187 writel(pdata->portsc, hcd->regs + PORTSC_OFFSET);
188 mdelay(10);
189
190 /* setup USBCONTROL. */
191 ret = mxc_set_usbcontrol(pdev->id, pdata->flags);
192 if (ret < 0)
193 goto err_init;
194
195 /* call platform specific init function */
196 if (pdata->init) {
197 ret = pdata->init(pdev);
198 if (ret) {
199 dev_err(dev, "platform init failed\n");
200 goto err_init;
201 }
202 }
203
204 /* most platforms need some time to settle changed IO settings */
205 mdelay(10);
206
207 /* Initialize the transceiver */
208 if (pdata->otg) {
209 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
210 if (otg_init(pdata->otg) != 0)
211 dev_err(dev, "unable to init transceiver\n");
212 else if (otg_set_vbus(pdata->otg, 1) != 0)
213 dev_err(dev, "unable to enable vbus on transceiver\n");
214 }
215
216 priv->hcd = hcd;
217 platform_set_drvdata(pdev, priv);
218
219 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
220 if (ret)
221 goto err_add;
222
223 return 0;
224
225err_add:
226 if (pdata && pdata->exit)
227 pdata->exit(pdev);
228err_init:
229 if (priv->ahbclk) {
230 clk_disable(priv->ahbclk);
231 clk_put(priv->ahbclk);
232 }
233err_clk_ahb:
234 clk_disable(priv->usbclk);
235 clk_put(priv->usbclk);
236err_clk:
237 iounmap(hcd->regs);
238err_ioremap:
239 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
240err_request_mem:
241err_get_resource:
242 kfree(priv);
243err_alloc:
244 usb_put_hcd(hcd);
245 return ret;
246}
247
248static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
249{
250 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
251 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
252 struct usb_hcd *hcd = priv->hcd;
253
254 if (pdata && pdata->exit)
255 pdata->exit(pdev);
256
257 if (pdata->otg)
258 otg_shutdown(pdata->otg);
259
260 usb_remove_hcd(hcd);
261 iounmap(hcd->regs);
262 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
263 usb_put_hcd(hcd);
264 platform_set_drvdata(pdev, NULL);
265
266 clk_disable(priv->usbclk);
267 clk_put(priv->usbclk);
268 if (priv->ahbclk) {
269 clk_disable(priv->ahbclk);
270 clk_put(priv->ahbclk);
271 }
272
273 kfree(priv);
274
275 return 0;
276}
277
278static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
279{
280 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
281 struct usb_hcd *hcd = priv->hcd;
282
283 if (hcd->driver->shutdown)
284 hcd->driver->shutdown(hcd);
285}
286
287MODULE_ALIAS("platform:mxc-ehci");
288
289static struct platform_driver ehci_mxc_driver = {
290 .probe = ehci_mxc_drv_probe,
291 .remove = __exit_p(ehci_mxc_drv_remove),
292 .shutdown = ehci_mxc_drv_shutdown,
293 .driver = {
294 .name = "mxc-ehci",
295 },
296};
diff --git a/drivers/usb/host/ehci-omap.c b/drivers/usb/host/ehci-omap.c
new file mode 100644
index 000000000000..12f1ad2fd0e8
--- /dev/null
+++ b/drivers/usb/host/ehci-omap.c
@@ -0,0 +1,756 @@
1/*
2 * ehci-omap.c - driver for USBHOST on OMAP 34xx processor
3 *
4 * Bus Glue for OMAP34xx USBHOST 3 port EHCI controller
5 * Tested on OMAP3430 ES2.0 SDP
6 *
7 * Copyright (C) 2007-2008 Texas Instruments, Inc.
8 * Author: Vikram Pandita <vikram.pandita@ti.com>
9 *
10 * Copyright (C) 2009 Nokia Corporation
11 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
12 *
13 * Based on "ehci-fsl.c" and "ehci-au1xxx.c" ehci glue layers
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 * TODO (last updated Feb 23rd, 2009):
30 * - add kernel-doc
31 * - enable AUTOIDLE
32 * - move DPLL5 programming to clock fw
33 * - add suspend/resume
34 * - move workarounds to board-files
35 */
36
37#include <linux/platform_device.h>
38#include <linux/clk.h>
39#include <linux/gpio.h>
40#include <mach/usb.h>
41
42/*
43 * OMAP USBHOST Register addresses: VIRTUAL ADDRESSES
44 * Use ehci_omap_readl()/ehci_omap_writel() functions
45 */
46
47/* TLL Register Set */
48#define OMAP_USBTLL_REVISION (0x00)
49#define OMAP_USBTLL_SYSCONFIG (0x10)
50#define OMAP_USBTLL_SYSCONFIG_CACTIVITY (1 << 8)
51#define OMAP_USBTLL_SYSCONFIG_SIDLEMODE (1 << 3)
52#define OMAP_USBTLL_SYSCONFIG_ENAWAKEUP (1 << 2)
53#define OMAP_USBTLL_SYSCONFIG_SOFTRESET (1 << 1)
54#define OMAP_USBTLL_SYSCONFIG_AUTOIDLE (1 << 0)
55
56#define OMAP_USBTLL_SYSSTATUS (0x14)
57#define OMAP_USBTLL_SYSSTATUS_RESETDONE (1 << 0)
58
59#define OMAP_USBTLL_IRQSTATUS (0x18)
60#define OMAP_USBTLL_IRQENABLE (0x1C)
61
62#define OMAP_TLL_SHARED_CONF (0x30)
63#define OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN (1 << 6)
64#define OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN (1 << 5)
65#define OMAP_TLL_SHARED_CONF_USB_DIVRATION (1 << 2)
66#define OMAP_TLL_SHARED_CONF_FCLK_REQ (1 << 1)
67#define OMAP_TLL_SHARED_CONF_FCLK_IS_ON (1 << 0)
68
69#define OMAP_TLL_CHANNEL_CONF(num) (0x040 + 0x004 * num)
70#define OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF (1 << 11)
71#define OMAP_TLL_CHANNEL_CONF_ULPI_ULPIAUTOIDLE (1 << 10)
72#define OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE (1 << 9)
73#define OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE (1 << 8)
74#define OMAP_TLL_CHANNEL_CONF_CHANEN (1 << 0)
75
76#define OMAP_TLL_ULPI_FUNCTION_CTRL(num) (0x804 + 0x100 * num)
77#define OMAP_TLL_ULPI_INTERFACE_CTRL(num) (0x807 + 0x100 * num)
78#define OMAP_TLL_ULPI_OTG_CTRL(num) (0x80A + 0x100 * num)
79#define OMAP_TLL_ULPI_INT_EN_RISE(num) (0x80D + 0x100 * num)
80#define OMAP_TLL_ULPI_INT_EN_FALL(num) (0x810 + 0x100 * num)
81#define OMAP_TLL_ULPI_INT_STATUS(num) (0x813 + 0x100 * num)
82#define OMAP_TLL_ULPI_INT_LATCH(num) (0x814 + 0x100 * num)
83#define OMAP_TLL_ULPI_DEBUG(num) (0x815 + 0x100 * num)
84#define OMAP_TLL_ULPI_SCRATCH_REGISTER(num) (0x816 + 0x100 * num)
85
86#define OMAP_TLL_CHANNEL_COUNT 3
87#define OMAP_TLL_CHANNEL_1_EN_MASK (1 << 1)
88#define OMAP_TLL_CHANNEL_2_EN_MASK (1 << 2)
89#define OMAP_TLL_CHANNEL_3_EN_MASK (1 << 4)
90
91/* UHH Register Set */
92#define OMAP_UHH_REVISION (0x00)
93#define OMAP_UHH_SYSCONFIG (0x10)
94#define OMAP_UHH_SYSCONFIG_MIDLEMODE (1 << 12)
95#define OMAP_UHH_SYSCONFIG_CACTIVITY (1 << 8)
96#define OMAP_UHH_SYSCONFIG_SIDLEMODE (1 << 3)
97#define OMAP_UHH_SYSCONFIG_ENAWAKEUP (1 << 2)
98#define OMAP_UHH_SYSCONFIG_SOFTRESET (1 << 1)
99#define OMAP_UHH_SYSCONFIG_AUTOIDLE (1 << 0)
100
101#define OMAP_UHH_SYSSTATUS (0x14)
102#define OMAP_UHH_HOSTCONFIG (0x40)
103#define OMAP_UHH_HOSTCONFIG_ULPI_BYPASS (1 << 0)
104#define OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS (1 << 0)
105#define OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS (1 << 11)
106#define OMAP_UHH_HOSTCONFIG_ULPI_P3_BYPASS (1 << 12)
107#define OMAP_UHH_HOSTCONFIG_INCR4_BURST_EN (1 << 2)
108#define OMAP_UHH_HOSTCONFIG_INCR8_BURST_EN (1 << 3)
109#define OMAP_UHH_HOSTCONFIG_INCR16_BURST_EN (1 << 4)
110#define OMAP_UHH_HOSTCONFIG_INCRX_ALIGN_EN (1 << 5)
111#define OMAP_UHH_HOSTCONFIG_P1_CONNECT_STATUS (1 << 8)
112#define OMAP_UHH_HOSTCONFIG_P2_CONNECT_STATUS (1 << 9)
113#define OMAP_UHH_HOSTCONFIG_P3_CONNECT_STATUS (1 << 10)
114
115#define OMAP_UHH_DEBUG_CSR (0x44)
116
117/* EHCI Register Set */
118#define EHCI_INSNREG05_ULPI (0xA4)
119#define EHCI_INSNREG05_ULPI_CONTROL_SHIFT 31
120#define EHCI_INSNREG05_ULPI_PORTSEL_SHIFT 24
121#define EHCI_INSNREG05_ULPI_OPSEL_SHIFT 22
122#define EHCI_INSNREG05_ULPI_REGADD_SHIFT 16
123#define EHCI_INSNREG05_ULPI_EXTREGADD_SHIFT 8
124#define EHCI_INSNREG05_ULPI_WRDATA_SHIFT 0
125
126/*-------------------------------------------------------------------------*/
127
128static inline void ehci_omap_writel(void __iomem *base, u32 reg, u32 val)
129{
130 __raw_writel(val, base + reg);
131}
132
133static inline u32 ehci_omap_readl(void __iomem *base, u32 reg)
134{
135 return __raw_readl(base + reg);
136}
137
138static inline void ehci_omap_writeb(void __iomem *base, u8 reg, u8 val)
139{
140 __raw_writeb(val, base + reg);
141}
142
143static inline u8 ehci_omap_readb(void __iomem *base, u8 reg)
144{
145 return __raw_readb(base + reg);
146}
147
148/*-------------------------------------------------------------------------*/
149
150struct ehci_hcd_omap {
151 struct ehci_hcd *ehci;
152 struct device *dev;
153
154 struct clk *usbhost_ick;
155 struct clk *usbhost2_120m_fck;
156 struct clk *usbhost1_48m_fck;
157 struct clk *usbtll_fck;
158 struct clk *usbtll_ick;
159
160 /* FIXME the following two workarounds are
161 * board specific not silicon-specific so these
162 * should be moved to board-file instead.
163 *
164 * Maybe someone from TI will know better which
165 * board is affected and needs the workarounds
166 * to be applied
167 */
168
169 /* gpio for resetting phy */
170 int reset_gpio_port[OMAP3_HS_USB_PORTS];
171
172 /* phy reset workaround */
173 int phy_reset;
174
175 /* desired phy_mode: TLL, PHY */
176 enum ehci_hcd_omap_mode port_mode[OMAP3_HS_USB_PORTS];
177
178 void __iomem *uhh_base;
179 void __iomem *tll_base;
180 void __iomem *ehci_base;
181};
182
183/*-------------------------------------------------------------------------*/
184
185static void omap_usb_utmi_init(struct ehci_hcd_omap *omap, u8 tll_channel_mask)
186{
187 unsigned reg;
188 int i;
189
190 /* Program the 3 TLL channels upfront */
191 for (i = 0; i < OMAP_TLL_CHANNEL_COUNT; i++) {
192 reg = ehci_omap_readl(omap->tll_base, OMAP_TLL_CHANNEL_CONF(i));
193
194 /* Disable AutoIdle, BitStuffing and use SDR Mode */
195 reg &= ~(OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE
196 | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF
197 | OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE);
198 ehci_omap_writel(omap->tll_base, OMAP_TLL_CHANNEL_CONF(i), reg);
199 }
200
201 /* Program Common TLL register */
202 reg = ehci_omap_readl(omap->tll_base, OMAP_TLL_SHARED_CONF);
203 reg |= (OMAP_TLL_SHARED_CONF_FCLK_IS_ON
204 | OMAP_TLL_SHARED_CONF_USB_DIVRATION
205 | OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN);
206 reg &= ~OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN;
207
208 ehci_omap_writel(omap->tll_base, OMAP_TLL_SHARED_CONF, reg);
209
210 /* Enable channels now */
211 for (i = 0; i < OMAP_TLL_CHANNEL_COUNT; i++) {
212 reg = ehci_omap_readl(omap->tll_base, OMAP_TLL_CHANNEL_CONF(i));
213
214 /* Enable only the reg that is needed */
215 if (!(tll_channel_mask & 1<<i))
216 continue;
217
218 reg |= OMAP_TLL_CHANNEL_CONF_CHANEN;
219 ehci_omap_writel(omap->tll_base, OMAP_TLL_CHANNEL_CONF(i), reg);
220
221 ehci_omap_writeb(omap->tll_base,
222 OMAP_TLL_ULPI_SCRATCH_REGISTER(i), 0xbe);
223 dev_dbg(omap->dev, "ULPI_SCRATCH_REG[ch=%d]= 0x%02x\n",
224 i+1, ehci_omap_readb(omap->tll_base,
225 OMAP_TLL_ULPI_SCRATCH_REGISTER(i)));
226 }
227}
228
229/*-------------------------------------------------------------------------*/
230
231/* omap_start_ehc
232 * - Start the TI USBHOST controller
233 */
234static int omap_start_ehc(struct ehci_hcd_omap *omap, struct usb_hcd *hcd)
235{
236 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
237 u8 tll_ch_mask = 0;
238 unsigned reg = 0;
239 int ret = 0;
240
241 dev_dbg(omap->dev, "starting TI EHCI USB Controller\n");
242
243 /* Enable Clocks for USBHOST */
244 omap->usbhost_ick = clk_get(omap->dev, "usbhost_ick");
245 if (IS_ERR(omap->usbhost_ick)) {
246 ret = PTR_ERR(omap->usbhost_ick);
247 goto err_host_ick;
248 }
249 clk_enable(omap->usbhost_ick);
250
251 omap->usbhost2_120m_fck = clk_get(omap->dev, "usbhost_120m_fck");
252 if (IS_ERR(omap->usbhost2_120m_fck)) {
253 ret = PTR_ERR(omap->usbhost2_120m_fck);
254 goto err_host_120m_fck;
255 }
256 clk_enable(omap->usbhost2_120m_fck);
257
258 omap->usbhost1_48m_fck = clk_get(omap->dev, "usbhost_48m_fck");
259 if (IS_ERR(omap->usbhost1_48m_fck)) {
260 ret = PTR_ERR(omap->usbhost1_48m_fck);
261 goto err_host_48m_fck;
262 }
263 clk_enable(omap->usbhost1_48m_fck);
264
265 if (omap->phy_reset) {
266 /* Refer: ISSUE1 */
267 if (gpio_is_valid(omap->reset_gpio_port[0])) {
268 gpio_request(omap->reset_gpio_port[0],
269 "USB1 PHY reset");
270 gpio_direction_output(omap->reset_gpio_port[0], 0);
271 }
272
273 if (gpio_is_valid(omap->reset_gpio_port[1])) {
274 gpio_request(omap->reset_gpio_port[1],
275 "USB2 PHY reset");
276 gpio_direction_output(omap->reset_gpio_port[1], 0);
277 }
278
279 /* Hold the PHY in RESET for enough time till DIR is high */
280 udelay(10);
281 }
282
283 /* Configure TLL for 60Mhz clk for ULPI */
284 omap->usbtll_fck = clk_get(omap->dev, "usbtll_fck");
285 if (IS_ERR(omap->usbtll_fck)) {
286 ret = PTR_ERR(omap->usbtll_fck);
287 goto err_tll_fck;
288 }
289 clk_enable(omap->usbtll_fck);
290
291 omap->usbtll_ick = clk_get(omap->dev, "usbtll_ick");
292 if (IS_ERR(omap->usbtll_ick)) {
293 ret = PTR_ERR(omap->usbtll_ick);
294 goto err_tll_ick;
295 }
296 clk_enable(omap->usbtll_ick);
297
298 /* perform TLL soft reset, and wait until reset is complete */
299 ehci_omap_writel(omap->tll_base, OMAP_USBTLL_SYSCONFIG,
300 OMAP_USBTLL_SYSCONFIG_SOFTRESET);
301
302 /* Wait for TLL reset to complete */
303 while (!(ehci_omap_readl(omap->tll_base, OMAP_USBTLL_SYSSTATUS)
304 & OMAP_USBTLL_SYSSTATUS_RESETDONE)) {
305 cpu_relax();
306
307 if (time_after(jiffies, timeout)) {
308 dev_dbg(omap->dev, "operation timed out\n");
309 ret = -EINVAL;
310 goto err_sys_status;
311 }
312 }
313
314 dev_dbg(omap->dev, "TLL RESET DONE\n");
315
316 /* (1<<3) = no idle mode only for initial debugging */
317 ehci_omap_writel(omap->tll_base, OMAP_USBTLL_SYSCONFIG,
318 OMAP_USBTLL_SYSCONFIG_ENAWAKEUP |
319 OMAP_USBTLL_SYSCONFIG_SIDLEMODE |
320 OMAP_USBTLL_SYSCONFIG_CACTIVITY);
321
322
323 /* Put UHH in NoIdle/NoStandby mode */
324 reg = ehci_omap_readl(omap->uhh_base, OMAP_UHH_SYSCONFIG);
325 reg |= (OMAP_UHH_SYSCONFIG_ENAWAKEUP
326 | OMAP_UHH_SYSCONFIG_SIDLEMODE
327 | OMAP_UHH_SYSCONFIG_CACTIVITY
328 | OMAP_UHH_SYSCONFIG_MIDLEMODE);
329 reg &= ~OMAP_UHH_SYSCONFIG_AUTOIDLE;
330
331 ehci_omap_writel(omap->uhh_base, OMAP_UHH_SYSCONFIG, reg);
332
333 reg = ehci_omap_readl(omap->uhh_base, OMAP_UHH_HOSTCONFIG);
334
335 /* setup ULPI bypass and burst configurations */
336 reg |= (OMAP_UHH_HOSTCONFIG_INCR4_BURST_EN
337 | OMAP_UHH_HOSTCONFIG_INCR8_BURST_EN
338 | OMAP_UHH_HOSTCONFIG_INCR16_BURST_EN);
339 reg &= ~OMAP_UHH_HOSTCONFIG_INCRX_ALIGN_EN;
340
341 if (omap->port_mode[0] == EHCI_HCD_OMAP_MODE_UNKNOWN)
342 reg &= ~OMAP_UHH_HOSTCONFIG_P1_CONNECT_STATUS;
343 if (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_UNKNOWN)
344 reg &= ~OMAP_UHH_HOSTCONFIG_P2_CONNECT_STATUS;
345 if (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_UNKNOWN)
346 reg &= ~OMAP_UHH_HOSTCONFIG_P3_CONNECT_STATUS;
347
348 /* Bypass the TLL module for PHY mode operation */
349 if (omap_rev() <= OMAP3430_REV_ES2_1) {
350 dev_dbg(omap->dev, "OMAP3 ES version <= ES2.1 \n");
351 if ((omap->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY) ||
352 (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY) ||
353 (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_PHY))
354 reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
355 else
356 reg |= OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
357 } else {
358 dev_dbg(omap->dev, "OMAP3 ES version > ES2.1\n");
359 if (omap->port_mode[0] == EHCI_HCD_OMAP_MODE_PHY)
360 reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
361 else if (omap->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL)
362 reg |= OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
363
364 if (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_PHY)
365 reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS;
366 else if (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL)
367 reg |= OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS;
368
369 if (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_PHY)
370 reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_P3_BYPASS;
371 else if (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_TLL)
372 reg |= OMAP_UHH_HOSTCONFIG_ULPI_P3_BYPASS;
373
374 }
375 ehci_omap_writel(omap->uhh_base, OMAP_UHH_HOSTCONFIG, reg);
376 dev_dbg(omap->dev, "UHH setup done, uhh_hostconfig=%x\n", reg);
377
378
379 if ((omap->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL) ||
380 (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL) ||
381 (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_TLL)) {
382
383 if (omap->port_mode[0] == EHCI_HCD_OMAP_MODE_TLL)
384 tll_ch_mask |= OMAP_TLL_CHANNEL_1_EN_MASK;
385 if (omap->port_mode[1] == EHCI_HCD_OMAP_MODE_TLL)
386 tll_ch_mask |= OMAP_TLL_CHANNEL_2_EN_MASK;
387 if (omap->port_mode[2] == EHCI_HCD_OMAP_MODE_TLL)
388 tll_ch_mask |= OMAP_TLL_CHANNEL_3_EN_MASK;
389
390 /* Enable UTMI mode for required TLL channels */
391 omap_usb_utmi_init(omap, tll_ch_mask);
392 }
393
394 if (omap->phy_reset) {
395 /* Refer ISSUE1:
396 * Hold the PHY in RESET for enough time till
397 * PHY is settled and ready
398 */
399 udelay(10);
400
401 if (gpio_is_valid(omap->reset_gpio_port[0]))
402 gpio_set_value(omap->reset_gpio_port[0], 1);
403
404 if (gpio_is_valid(omap->reset_gpio_port[1]))
405 gpio_set_value(omap->reset_gpio_port[1], 1);
406 }
407
408 return 0;
409
410err_sys_status:
411 clk_disable(omap->usbtll_ick);
412 clk_put(omap->usbtll_ick);
413
414err_tll_ick:
415 clk_disable(omap->usbtll_fck);
416 clk_put(omap->usbtll_fck);
417
418err_tll_fck:
419 clk_disable(omap->usbhost1_48m_fck);
420 clk_put(omap->usbhost1_48m_fck);
421
422 if (omap->phy_reset) {
423 if (gpio_is_valid(omap->reset_gpio_port[0]))
424 gpio_free(omap->reset_gpio_port[0]);
425
426 if (gpio_is_valid(omap->reset_gpio_port[1]))
427 gpio_free(omap->reset_gpio_port[1]);
428 }
429
430err_host_48m_fck:
431 clk_disable(omap->usbhost2_120m_fck);
432 clk_put(omap->usbhost2_120m_fck);
433
434err_host_120m_fck:
435 clk_disable(omap->usbhost_ick);
436 clk_put(omap->usbhost_ick);
437
438err_host_ick:
439 return ret;
440}
441
442static void omap_stop_ehc(struct ehci_hcd_omap *omap, struct usb_hcd *hcd)
443{
444 unsigned long timeout = jiffies + msecs_to_jiffies(100);
445
446 dev_dbg(omap->dev, "stopping TI EHCI USB Controller\n");
447
448 /* Reset OMAP modules for insmod/rmmod to work */
449 ehci_omap_writel(omap->uhh_base, OMAP_UHH_SYSCONFIG,
450 OMAP_UHH_SYSCONFIG_SOFTRESET);
451 while (!(ehci_omap_readl(omap->uhh_base, OMAP_UHH_SYSSTATUS)
452 & (1 << 0))) {
453 cpu_relax();
454
455 if (time_after(jiffies, timeout))
456 dev_dbg(omap->dev, "operation timed out\n");
457 }
458
459 while (!(ehci_omap_readl(omap->uhh_base, OMAP_UHH_SYSSTATUS)
460 & (1 << 1))) {
461 cpu_relax();
462
463 if (time_after(jiffies, timeout))
464 dev_dbg(omap->dev, "operation timed out\n");
465 }
466
467 while (!(ehci_omap_readl(omap->uhh_base, OMAP_UHH_SYSSTATUS)
468 & (1 << 2))) {
469 cpu_relax();
470
471 if (time_after(jiffies, timeout))
472 dev_dbg(omap->dev, "operation timed out\n");
473 }
474
475 ehci_omap_writel(omap->tll_base, OMAP_USBTLL_SYSCONFIG, (1 << 1));
476
477 while (!(ehci_omap_readl(omap->tll_base, OMAP_USBTLL_SYSSTATUS)
478 & (1 << 0))) {
479 cpu_relax();
480
481 if (time_after(jiffies, timeout))
482 dev_dbg(omap->dev, "operation timed out\n");
483 }
484
485 if (omap->usbtll_fck != NULL) {
486 clk_disable(omap->usbtll_fck);
487 clk_put(omap->usbtll_fck);
488 omap->usbtll_fck = NULL;
489 }
490
491 if (omap->usbhost_ick != NULL) {
492 clk_disable(omap->usbhost_ick);
493 clk_put(omap->usbhost_ick);
494 omap->usbhost_ick = NULL;
495 }
496
497 if (omap->usbhost1_48m_fck != NULL) {
498 clk_disable(omap->usbhost1_48m_fck);
499 clk_put(omap->usbhost1_48m_fck);
500 omap->usbhost1_48m_fck = NULL;
501 }
502
503 if (omap->usbhost2_120m_fck != NULL) {
504 clk_disable(omap->usbhost2_120m_fck);
505 clk_put(omap->usbhost2_120m_fck);
506 omap->usbhost2_120m_fck = NULL;
507 }
508
509 if (omap->usbtll_ick != NULL) {
510 clk_disable(omap->usbtll_ick);
511 clk_put(omap->usbtll_ick);
512 omap->usbtll_ick = NULL;
513 }
514
515 if (omap->phy_reset) {
516 if (gpio_is_valid(omap->reset_gpio_port[0]))
517 gpio_free(omap->reset_gpio_port[0]);
518
519 if (gpio_is_valid(omap->reset_gpio_port[1]))
520 gpio_free(omap->reset_gpio_port[1]);
521 }
522
523 dev_dbg(omap->dev, "Clock to USB host has been disabled\n");
524}
525
526/*-------------------------------------------------------------------------*/
527
528static const struct hc_driver ehci_omap_hc_driver;
529
530/* configure so an HC device and id are always provided */
531/* always called with process context; sleeping is OK */
532
533/**
534 * ehci_hcd_omap_probe - initialize TI-based HCDs
535 *
536 * Allocates basic resources for this USB host controller, and
537 * then invokes the start() method for the HCD associated with it
538 * through the hotplug entry's driver_data.
539 */
540static int ehci_hcd_omap_probe(struct platform_device *pdev)
541{
542 struct ehci_hcd_omap_platform_data *pdata = pdev->dev.platform_data;
543 struct ehci_hcd_omap *omap;
544 struct resource *res;
545 struct usb_hcd *hcd;
546
547 int irq = platform_get_irq(pdev, 0);
548 int ret = -ENODEV;
549
550 if (!pdata) {
551 dev_dbg(&pdev->dev, "missing platform_data\n");
552 goto err_pdata;
553 }
554
555 if (usb_disabled())
556 goto err_disabled;
557
558 omap = kzalloc(sizeof(*omap), GFP_KERNEL);
559 if (!omap) {
560 ret = -ENOMEM;
561 goto err_disabled;
562 }
563
564 hcd = usb_create_hcd(&ehci_omap_hc_driver, &pdev->dev,
565 dev_name(&pdev->dev));
566 if (!hcd) {
567 dev_dbg(&pdev->dev, "failed to create hcd with err %d\n", ret);
568 ret = -ENOMEM;
569 goto err_create_hcd;
570 }
571
572 platform_set_drvdata(pdev, omap);
573 omap->dev = &pdev->dev;
574 omap->phy_reset = pdata->phy_reset;
575 omap->reset_gpio_port[0] = pdata->reset_gpio_port[0];
576 omap->reset_gpio_port[1] = pdata->reset_gpio_port[1];
577 omap->reset_gpio_port[2] = pdata->reset_gpio_port[2];
578 omap->port_mode[0] = pdata->port_mode[0];
579 omap->port_mode[1] = pdata->port_mode[1];
580 omap->port_mode[2] = pdata->port_mode[2];
581 omap->ehci = hcd_to_ehci(hcd);
582 omap->ehci->sbrn = 0x20;
583
584 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
585
586 hcd->rsrc_start = res->start;
587 hcd->rsrc_len = resource_size(res);
588
589 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
590 if (!hcd->regs) {
591 dev_err(&pdev->dev, "EHCI ioremap failed\n");
592 ret = -ENOMEM;
593 goto err_ioremap;
594 }
595
596 /* we know this is the memory we want, no need to ioremap again */
597 omap->ehci->caps = hcd->regs;
598 omap->ehci_base = hcd->regs;
599
600 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
601 omap->uhh_base = ioremap(res->start, resource_size(res));
602 if (!omap->uhh_base) {
603 dev_err(&pdev->dev, "UHH ioremap failed\n");
604 ret = -ENOMEM;
605 goto err_uhh_ioremap;
606 }
607
608 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
609 omap->tll_base = ioremap(res->start, resource_size(res));
610 if (!omap->tll_base) {
611 dev_err(&pdev->dev, "TLL ioremap failed\n");
612 ret = -ENOMEM;
613 goto err_tll_ioremap;
614 }
615
616 ret = omap_start_ehc(omap, hcd);
617 if (ret) {
618 dev_dbg(&pdev->dev, "failed to start ehci\n");
619 goto err_start;
620 }
621
622 omap->ehci->regs = hcd->regs
623 + HC_LENGTH(readl(&omap->ehci->caps->hc_capbase));
624
625 /* cache this readonly data; minimize chip reads */
626 omap->ehci->hcs_params = readl(&omap->ehci->caps->hcs_params);
627
628 /* SET 1 micro-frame Interrupt interval */
629 writel(readl(&omap->ehci->regs->command) | (1 << 16),
630 &omap->ehci->regs->command);
631
632 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
633 if (ret) {
634 dev_dbg(&pdev->dev, "failed to add hcd with err %d\n", ret);
635 goto err_add_hcd;
636 }
637
638 return 0;
639
640err_add_hcd:
641 omap_stop_ehc(omap, hcd);
642
643err_start:
644 iounmap(omap->tll_base);
645
646err_tll_ioremap:
647 iounmap(omap->uhh_base);
648
649err_uhh_ioremap:
650 iounmap(hcd->regs);
651
652err_ioremap:
653 usb_put_hcd(hcd);
654
655err_create_hcd:
656 kfree(omap);
657err_disabled:
658err_pdata:
659 return ret;
660}
661
662/* may be called without controller electrically present */
663/* may be called with controller, bus, and devices active */
664
665/**
666 * ehci_hcd_omap_remove - shutdown processing for EHCI HCDs
667 * @pdev: USB Host Controller being removed
668 *
669 * Reverses the effect of usb_ehci_hcd_omap_probe(), first invoking
670 * the HCD's stop() method. It is always called from a thread
671 * context, normally "rmmod", "apmd", or something similar.
672 */
673static int ehci_hcd_omap_remove(struct platform_device *pdev)
674{
675 struct ehci_hcd_omap *omap = platform_get_drvdata(pdev);
676 struct usb_hcd *hcd = ehci_to_hcd(omap->ehci);
677
678 usb_remove_hcd(hcd);
679 omap_stop_ehc(omap, hcd);
680 iounmap(hcd->regs);
681 iounmap(omap->tll_base);
682 iounmap(omap->uhh_base);
683 usb_put_hcd(hcd);
684
685 return 0;
686}
687
688static void ehci_hcd_omap_shutdown(struct platform_device *pdev)
689{
690 struct ehci_hcd_omap *omap = platform_get_drvdata(pdev);
691 struct usb_hcd *hcd = ehci_to_hcd(omap->ehci);
692
693 if (hcd->driver->shutdown)
694 hcd->driver->shutdown(hcd);
695}
696
697static struct platform_driver ehci_hcd_omap_driver = {
698 .probe = ehci_hcd_omap_probe,
699 .remove = ehci_hcd_omap_remove,
700 .shutdown = ehci_hcd_omap_shutdown,
701 /*.suspend = ehci_hcd_omap_suspend, */
702 /*.resume = ehci_hcd_omap_resume, */
703 .driver = {
704 .name = "ehci-omap",
705 }
706};
707
708/*-------------------------------------------------------------------------*/
709
710static const struct hc_driver ehci_omap_hc_driver = {
711 .description = hcd_name,
712 .product_desc = "OMAP-EHCI Host Controller",
713 .hcd_priv_size = sizeof(struct ehci_hcd),
714
715 /*
716 * generic hardware linkage
717 */
718 .irq = ehci_irq,
719 .flags = HCD_MEMORY | HCD_USB2,
720
721 /*
722 * basic lifecycle operations
723 */
724 .reset = ehci_init,
725 .start = ehci_run,
726 .stop = ehci_stop,
727 .shutdown = ehci_shutdown,
728
729 /*
730 * managing i/o requests and associated device resources
731 */
732 .urb_enqueue = ehci_urb_enqueue,
733 .urb_dequeue = ehci_urb_dequeue,
734 .endpoint_disable = ehci_endpoint_disable,
735 .endpoint_reset = ehci_endpoint_reset,
736
737 /*
738 * scheduling support
739 */
740 .get_frame_number = ehci_get_frame,
741
742 /*
743 * root hub support
744 */
745 .hub_status_data = ehci_hub_status_data,
746 .hub_control = ehci_hub_control,
747 .bus_suspend = ehci_bus_suspend,
748 .bus_resume = ehci_bus_resume,
749
750 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
751};
752
753MODULE_ALIAS("platform:omap-ehci");
754MODULE_AUTHOR("Texas Instruments, Inc.");
755MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
756
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 139a2cc3f641..a427d3b00634 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -616,9 +616,11 @@ qh_urb_transaction (
616) { 616) {
617 struct ehci_qtd *qtd, *qtd_prev; 617 struct ehci_qtd *qtd, *qtd_prev;
618 dma_addr_t buf; 618 dma_addr_t buf;
619 int len, maxpacket; 619 int len, this_sg_len, maxpacket;
620 int is_input; 620 int is_input;
621 u32 token; 621 u32 token;
622 int i;
623 struct scatterlist *sg;
622 624
623 /* 625 /*
624 * URBs map to sequences of QTDs: one logical transaction 626 * URBs map to sequences of QTDs: one logical transaction
@@ -659,7 +661,20 @@ qh_urb_transaction (
659 /* 661 /*
660 * data transfer stage: buffer setup 662 * data transfer stage: buffer setup
661 */ 663 */
662 buf = urb->transfer_dma; 664 i = urb->num_sgs;
665 if (len > 0 && i > 0) {
666 sg = urb->sg->sg;
667 buf = sg_dma_address(sg);
668
669 /* urb->transfer_buffer_length may be smaller than the
670 * size of the scatterlist (or vice versa)
671 */
672 this_sg_len = min_t(int, sg_dma_len(sg), len);
673 } else {
674 sg = NULL;
675 buf = urb->transfer_dma;
676 this_sg_len = len;
677 }
663 678
664 if (is_input) 679 if (is_input)
665 token |= (1 /* "in" */ << 8); 680 token |= (1 /* "in" */ << 8);
@@ -675,7 +690,9 @@ qh_urb_transaction (
675 for (;;) { 690 for (;;) {
676 int this_qtd_len; 691 int this_qtd_len;
677 692
678 this_qtd_len = qtd_fill(ehci, qtd, buf, len, token, maxpacket); 693 this_qtd_len = qtd_fill(ehci, qtd, buf, this_sg_len, token,
694 maxpacket);
695 this_sg_len -= this_qtd_len;
679 len -= this_qtd_len; 696 len -= this_qtd_len;
680 buf += this_qtd_len; 697 buf += this_qtd_len;
681 698
@@ -691,8 +708,13 @@ qh_urb_transaction (
691 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0) 708 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
692 token ^= QTD_TOGGLE; 709 token ^= QTD_TOGGLE;
693 710
694 if (likely (len <= 0)) 711 if (likely(this_sg_len <= 0)) {
695 break; 712 if (--i <= 0 || len <= 0)
713 break;
714 sg = sg_next(sg);
715 buf = sg_dma_address(sg);
716 this_sg_len = min_t(int, sg_dma_len(sg), len);
717 }
696 718
697 qtd_prev = qtd; 719 qtd_prev = qtd;
698 qtd = ehci_qtd_alloc (ehci, flags); 720 qtd = ehci_qtd_alloc (ehci, flags);
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index a5535b5e3fe2..1e391e624c8a 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -1385,7 +1385,7 @@ sitd_slot_ok (
1385 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler! 1385 * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
1386 */ 1386 */
1387 1387
1388#define SCHEDULE_SLOP 10 /* frames */ 1388#define SCHEDULE_SLOP 80 /* microframes */
1389 1389
1390static int 1390static int
1391iso_stream_schedule ( 1391iso_stream_schedule (
@@ -1394,12 +1394,13 @@ iso_stream_schedule (
1394 struct ehci_iso_stream *stream 1394 struct ehci_iso_stream *stream
1395) 1395)
1396{ 1396{
1397 u32 now, start, max, period; 1397 u32 now, next, start, period;
1398 int status; 1398 int status;
1399 unsigned mod = ehci->periodic_size << 3; 1399 unsigned mod = ehci->periodic_size << 3;
1400 struct ehci_iso_sched *sched = urb->hcpriv; 1400 struct ehci_iso_sched *sched = urb->hcpriv;
1401 struct pci_dev *pdev;
1401 1402
1402 if (sched->span > (mod - 8 * SCHEDULE_SLOP)) { 1403 if (sched->span > (mod - SCHEDULE_SLOP)) {
1403 ehci_dbg (ehci, "iso request %p too long\n", urb); 1404 ehci_dbg (ehci, "iso request %p too long\n", urb);
1404 status = -EFBIG; 1405 status = -EFBIG;
1405 goto fail; 1406 goto fail;
@@ -1418,26 +1419,35 @@ iso_stream_schedule (
1418 1419
1419 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod; 1420 now = ehci_readl(ehci, &ehci->regs->frame_index) % mod;
1420 1421
1421 /* when's the last uframe this urb could start? */
1422 max = now + mod;
1423
1424 /* Typical case: reuse current schedule, stream is still active. 1422 /* Typical case: reuse current schedule, stream is still active.
1425 * Hopefully there are no gaps from the host falling behind 1423 * Hopefully there are no gaps from the host falling behind
1426 * (irq delays etc), but if there are we'll take the next 1424 * (irq delays etc), but if there are we'll take the next
1427 * slot in the schedule, implicitly assuming URB_ISO_ASAP. 1425 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
1428 */ 1426 */
1429 if (likely (!list_empty (&stream->td_list))) { 1427 if (likely (!list_empty (&stream->td_list))) {
1428 pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
1430 start = stream->next_uframe; 1429 start = stream->next_uframe;
1431 if (start < now) 1430
1432 start += mod; 1431 /* For high speed devices, allow scheduling within the
1432 * isochronous scheduling threshold. For full speed devices,
1433 * don't. (Work around for Intel ICH9 bug.)
1434 */
1435 if (!stream->highspeed &&
1436 pdev->vendor == PCI_VENDOR_ID_INTEL)
1437 next = now + ehci->i_thresh;
1438 else
1439 next = now;
1433 1440
1434 /* Fell behind (by up to twice the slop amount)? */ 1441 /* Fell behind (by up to twice the slop amount)? */
1435 if (start >= max - 2 * 8 * SCHEDULE_SLOP) 1442 if (((start - next) & (mod - 1)) >=
1443 mod - 2 * SCHEDULE_SLOP)
1436 start += period * DIV_ROUND_UP( 1444 start += period * DIV_ROUND_UP(
1437 max - start, period) - mod; 1445 (next - start) & (mod - 1),
1446 period);
1438 1447
1439 /* Tried to schedule too far into the future? */ 1448 /* Tried to schedule too far into the future? */
1440 if (unlikely((start + sched->span) >= max)) { 1449 if (unlikely(((start - now) & (mod - 1)) + sched->span
1450 >= mod - 2 * SCHEDULE_SLOP)) {
1441 status = -EFBIG; 1451 status = -EFBIG;
1442 goto fail; 1452 goto fail;
1443 } 1453 }
@@ -1451,7 +1461,7 @@ iso_stream_schedule (
1451 * can also help high bandwidth if the dma and irq loads don't 1461 * can also help high bandwidth if the dma and irq loads don't
1452 * jump until after the queue is primed. 1462 * jump until after the queue is primed.
1453 */ 1463 */
1454 start = SCHEDULE_SLOP * 8 + (now & ~0x07); 1464 start = SCHEDULE_SLOP + (now & ~0x07);
1455 start %= mod; 1465 start %= mod;
1456 stream->next_uframe = start; 1466 stream->next_uframe = start;
1457 1467
@@ -1482,7 +1492,7 @@ iso_stream_schedule (
1482 /* no room in the schedule */ 1492 /* no room in the schedule */
1483 ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n", 1493 ehci_dbg (ehci, "iso %ssched full %p (now %d max %d)\n",
1484 list_empty (&stream->td_list) ? "" : "re", 1494 list_empty (&stream->td_list) ? "" : "re",
1485 urb, now, max); 1495 urb, now, now + mod);
1486 status = -ENOSPC; 1496 status = -ENOSPC;
1487 1497
1488fail: 1498fail:
diff --git a/drivers/usb/host/ehci-xilinx-of.c b/drivers/usb/host/ehci-xilinx-of.c
new file mode 100644
index 000000000000..a5861531ad3e
--- /dev/null
+++ b/drivers/usb/host/ehci-xilinx-of.c
@@ -0,0 +1,300 @@
1/*
2 * EHCI HCD (Host Controller Driver) for USB.
3 *
4 * Bus Glue for Xilinx EHCI core on the of_platform bus
5 *
6 * Copyright (c) 2009 Xilinx, Inc.
7 *
8 * Based on "ehci-ppc-of.c" by Valentine Barshak <vbarshak@ru.mvista.com>
9 * and "ehci-ppc-soc.c" by Stefan Roese <sr@denx.de>
10 * and "ohci-ppc-of.c" by Sylvain Munaut <tnt@246tNt.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 */
27
28#include <linux/signal.h>
29
30#include <linux/of.h>
31#include <linux/of_platform.h>
32
33/**
34 * ehci_xilinx_of_setup - Initialize the device for ehci_reset()
35 * @hcd: Pointer to the usb_hcd device to which the host controller bound
36 *
37 * called during probe() after chip reset completes.
38 */
39static int ehci_xilinx_of_setup(struct usb_hcd *hcd)
40{
41 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
42 int retval;
43
44 retval = ehci_halt(ehci);
45 if (retval)
46 return retval;
47
48 retval = ehci_init(hcd);
49 if (retval)
50 return retval;
51
52 ehci->sbrn = 0x20;
53
54 return ehci_reset(ehci);
55}
56
57/**
58 * ehci_xilinx_port_handed_over - hand the port out if failed to enable it
59 * @hcd: Pointer to the usb_hcd device to which the host controller bound
60 * @portnum:Port number to which the device is attached.
61 *
62 * This function is used as a place to tell the user that the Xilinx USB host
63 * controller does support LS devices. And in an HS only configuration, it
64 * does not support FS devices either. It is hoped that this can help a
65 * confused user.
66 *
67 * There are cases when the host controller fails to enable the port due to,
68 * for example, insufficient power that can be supplied to the device from
69 * the USB bus. In those cases, the messages printed here are not helpful.
70 */
71static int ehci_xilinx_port_handed_over(struct usb_hcd *hcd, int portnum)
72{
73 dev_warn(hcd->self.controller, "port %d cannot be enabled\n", portnum);
74 if (hcd->has_tt) {
75 dev_warn(hcd->self.controller,
76 "Maybe you have connected a low speed device?\n");
77
78 dev_warn(hcd->self.controller,
79 "We do not support low speed devices\n");
80 } else {
81 dev_warn(hcd->self.controller,
82 "Maybe your device is not a high speed device?\n");
83 dev_warn(hcd->self.controller,
84 "The USB host controller does not support full speed "
85 "nor low speed devices\n");
86 dev_warn(hcd->self.controller,
87 "You can reconfigure the host controller to have "
88 "full speed support\n");
89 }
90
91 return 0;
92}
93
94
95static const struct hc_driver ehci_xilinx_of_hc_driver = {
96 .description = hcd_name,
97 .product_desc = "OF EHCI",
98 .hcd_priv_size = sizeof(struct ehci_hcd),
99
100 /*
101 * generic hardware linkage
102 */
103 .irq = ehci_irq,
104 .flags = HCD_MEMORY | HCD_USB2,
105
106 /*
107 * basic lifecycle operations
108 */
109 .reset = ehci_xilinx_of_setup,
110 .start = ehci_run,
111 .stop = ehci_stop,
112 .shutdown = ehci_shutdown,
113
114 /*
115 * managing i/o requests and associated device resources
116 */
117 .urb_enqueue = ehci_urb_enqueue,
118 .urb_dequeue = ehci_urb_dequeue,
119 .endpoint_disable = ehci_endpoint_disable,
120
121 /*
122 * scheduling support
123 */
124 .get_frame_number = ehci_get_frame,
125
126 /*
127 * root hub support
128 */
129 .hub_status_data = ehci_hub_status_data,
130 .hub_control = ehci_hub_control,
131#ifdef CONFIG_PM
132 .bus_suspend = ehci_bus_suspend,
133 .bus_resume = ehci_bus_resume,
134#endif
135 .relinquish_port = NULL,
136 .port_handed_over = ehci_xilinx_port_handed_over,
137
138 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
139};
140
141/**
142 * ehci_hcd_xilinx_of_probe - Probe method for the USB host controller
143 * @op: pointer to the of_device to which the host controller bound
144 * @match: pointer to of_device_id structure, not used
145 *
146 * This function requests resources and sets up appropriate properties for the
147 * host controller. Because the Xilinx USB host controller can be configured
148 * as HS only or HS/FS only, it checks the configuration in the device tree
149 * entry, and sets an appropriate value for hcd->has_tt.
150 */
151static int __devinit
152ehci_hcd_xilinx_of_probe(struct of_device *op, const struct of_device_id *match)
153{
154 struct device_node *dn = op->node;
155 struct usb_hcd *hcd;
156 struct ehci_hcd *ehci;
157 struct resource res;
158 int irq;
159 int rv;
160 int *value;
161
162 if (usb_disabled())
163 return -ENODEV;
164
165 dev_dbg(&op->dev, "initializing XILINX-OF USB Controller\n");
166
167 rv = of_address_to_resource(dn, 0, &res);
168 if (rv)
169 return rv;
170
171 hcd = usb_create_hcd(&ehci_xilinx_of_hc_driver, &op->dev,
172 "XILINX-OF USB");
173 if (!hcd)
174 return -ENOMEM;
175
176 hcd->rsrc_start = res.start;
177 hcd->rsrc_len = res.end - res.start + 1;
178
179 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
180 printk(KERN_ERR __FILE__ ": request_mem_region failed\n");
181 rv = -EBUSY;
182 goto err_rmr;
183 }
184
185 irq = irq_of_parse_and_map(dn, 0);
186 if (irq == NO_IRQ) {
187 printk(KERN_ERR __FILE__ ": irq_of_parse_and_map failed\n");
188 rv = -EBUSY;
189 goto err_irq;
190 }
191
192 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
193 if (!hcd->regs) {
194 printk(KERN_ERR __FILE__ ": ioremap failed\n");
195 rv = -ENOMEM;
196 goto err_ioremap;
197 }
198
199 ehci = hcd_to_ehci(hcd);
200
201 /* This core always has big-endian register interface and uses
202 * big-endian memory descriptors.
203 */
204 ehci->big_endian_mmio = 1;
205 ehci->big_endian_desc = 1;
206
207 /* Check whether the FS support option is selected in the hardware.
208 */
209 value = (int *)of_get_property(dn, "xlnx,support-usb-fs", NULL);
210 if (value && (*value == 1)) {
211 ehci_dbg(ehci, "USB host controller supports FS devices\n");
212 hcd->has_tt = 1;
213 } else {
214 ehci_dbg(ehci,
215 "USB host controller is HS only\n");
216 hcd->has_tt = 0;
217 }
218
219 /* Debug registers are at the first 0x100 region
220 */
221 ehci->caps = hcd->regs + 0x100;
222 ehci->regs = hcd->regs + 0x100 +
223 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
224
225 /* cache this readonly data; minimize chip reads */
226 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
227
228 rv = usb_add_hcd(hcd, irq, 0);
229 if (rv == 0)
230 return 0;
231
232 iounmap(hcd->regs);
233
234err_ioremap:
235err_irq:
236 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
237err_rmr:
238 usb_put_hcd(hcd);
239
240 return rv;
241}
242
243/**
244 * ehci_hcd_xilinx_of_remove - shutdown hcd and release resources
245 * @op: pointer to of_device structure that is to be removed
246 *
247 * Remove the hcd structure, and release resources that has been requested
248 * during probe.
249 */
250static int ehci_hcd_xilinx_of_remove(struct of_device *op)
251{
252 struct usb_hcd *hcd = dev_get_drvdata(&op->dev);
253 dev_set_drvdata(&op->dev, NULL);
254
255 dev_dbg(&op->dev, "stopping XILINX-OF USB Controller\n");
256
257 usb_remove_hcd(hcd);
258
259 iounmap(hcd->regs);
260 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
261
262 usb_put_hcd(hcd);
263
264 return 0;
265}
266
267/**
268 * ehci_hcd_xilinx_of_shutdown - shutdown the hcd
269 * @op: pointer to of_device structure that is to be removed
270 *
271 * Properly shutdown the hcd, call driver's shutdown routine.
272 */
273static int ehci_hcd_xilinx_of_shutdown(struct of_device *op)
274{
275 struct usb_hcd *hcd = dev_get_drvdata(&op->dev);
276
277 if (hcd->driver->shutdown)
278 hcd->driver->shutdown(hcd);
279
280 return 0;
281}
282
283
284static struct of_device_id ehci_hcd_xilinx_of_match[] = {
285 {.compatible = "xlnx,xps-usb-host-1.00.a",},
286 {},
287};
288MODULE_DEVICE_TABLE(of, ehci_hcd_xilinx_of_match);
289
290static struct of_platform_driver ehci_hcd_xilinx_of_driver = {
291 .name = "xilinx-of-ehci",
292 .match_table = ehci_hcd_xilinx_of_match,
293 .probe = ehci_hcd_xilinx_of_probe,
294 .remove = ehci_hcd_xilinx_of_remove,
295 .shutdown = ehci_hcd_xilinx_of_shutdown,
296 .driver = {
297 .name = "xilinx-of-ehci",
298 .owner = THIS_MODULE,
299 },
300};
diff --git a/drivers/usb/host/isp1362.h b/drivers/usb/host/isp1362.h
index 1a253ebf7e50..5151516ea1de 100644
--- a/drivers/usb/host/isp1362.h
+++ b/drivers/usb/host/isp1362.h
@@ -534,8 +534,8 @@ struct isp1362_hcd {
534 534
535 /* periodic schedule: isochronous */ 535 /* periodic schedule: isochronous */
536 struct list_head isoc; 536 struct list_head isoc;
537 int istl_flip:1; 537 unsigned int istl_flip:1;
538 int irq_active:1; 538 unsigned int irq_active:1;
539 539
540 /* Schedules for the current frame */ 540 /* Schedules for the current frame */
541 struct isp1362_ep_queue atl_queue; 541 struct isp1362_ep_queue atl_queue;
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
index 7ccffcbe7b6f..68b83ab70719 100644
--- a/drivers/usb/host/ohci-at91.c
+++ b/drivers/usb/host/ohci-at91.c
@@ -35,7 +35,7 @@ extern int usb_disabled(void);
35 35
36static void at91_start_clock(void) 36static void at91_start_clock(void)
37{ 37{
38 if (cpu_is_at91sam9261()) 38 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
39 clk_enable(hclk); 39 clk_enable(hclk);
40 clk_enable(iclk); 40 clk_enable(iclk);
41 clk_enable(fclk); 41 clk_enable(fclk);
@@ -46,7 +46,7 @@ static void at91_stop_clock(void)
46{ 46{
47 clk_disable(fclk); 47 clk_disable(fclk);
48 clk_disable(iclk); 48 clk_disable(iclk);
49 if (cpu_is_at91sam9261()) 49 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
50 clk_disable(hclk); 50 clk_disable(hclk);
51 clocked = 0; 51 clocked = 0;
52} 52}
@@ -142,7 +142,7 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
142 142
143 iclk = clk_get(&pdev->dev, "ohci_clk"); 143 iclk = clk_get(&pdev->dev, "ohci_clk");
144 fclk = clk_get(&pdev->dev, "uhpck"); 144 fclk = clk_get(&pdev->dev, "uhpck");
145 if (cpu_is_at91sam9261()) 145 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
146 hclk = clk_get(&pdev->dev, "hck0"); 146 hclk = clk_get(&pdev->dev, "hck0");
147 147
148 at91_start_hc(pdev); 148 at91_start_hc(pdev);
@@ -155,7 +155,7 @@ static int usb_hcd_at91_probe(const struct hc_driver *driver,
155 /* Error handling */ 155 /* Error handling */
156 at91_stop_hc(pdev); 156 at91_stop_hc(pdev);
157 157
158 if (cpu_is_at91sam9261()) 158 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
159 clk_put(hclk); 159 clk_put(hclk);
160 clk_put(fclk); 160 clk_put(fclk);
161 clk_put(iclk); 161 clk_put(iclk);
@@ -192,7 +192,7 @@ static void usb_hcd_at91_remove(struct usb_hcd *hcd,
192 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 192 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
193 usb_put_hcd(hcd); 193 usb_put_hcd(hcd);
194 194
195 if (cpu_is_at91sam9261()) 195 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
196 clk_put(hclk); 196 clk_put(hclk);
197 clk_put(fclk); 197 clk_put(fclk);
198 clk_put(iclk); 198 clk_put(iclk);
diff --git a/drivers/usb/host/ohci-pnx4008.c b/drivers/usb/host/ohci-pnx4008.c
index 100bf3d8437c..2769326da42e 100644
--- a/drivers/usb/host/ohci-pnx4008.c
+++ b/drivers/usb/host/ohci-pnx4008.c
@@ -98,8 +98,8 @@
98#define ISP1301_I2C_INTERRUPT_RISING 0xE 98#define ISP1301_I2C_INTERRUPT_RISING 0xE
99#define ISP1301_I2C_REG_CLEAR_ADDR 1 99#define ISP1301_I2C_REG_CLEAR_ADDR 1
100 100
101struct i2c_driver isp1301_driver; 101static struct i2c_driver isp1301_driver;
102struct i2c_client *isp1301_i2c_client; 102static struct i2c_client *isp1301_i2c_client;
103 103
104extern int usb_disabled(void); 104extern int usb_disabled(void);
105extern int ocpi_enable(void); 105extern int ocpi_enable(void);
@@ -120,12 +120,12 @@ static int isp1301_remove(struct i2c_client *client)
120 return 0; 120 return 0;
121} 121}
122 122
123const struct i2c_device_id isp1301_id[] = { 123static const struct i2c_device_id isp1301_id[] = {
124 { "isp1301_pnx", 0 }, 124 { "isp1301_pnx", 0 },
125 { } 125 { }
126}; 126};
127 127
128struct i2c_driver isp1301_driver = { 128static struct i2c_driver isp1301_driver = {
129 .driver = { 129 .driver = {
130 .name = "isp1301_pnx", 130 .name = "isp1301_pnx",
131 }, 131 },
diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
index e33d36256350..41dbc70ae752 100644
--- a/drivers/usb/host/r8a66597-hcd.c
+++ b/drivers/usb/host/r8a66597-hcd.c
@@ -822,8 +822,6 @@ static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
822 return; 822 return;
823 823
824 list_for_each_entry_safe(td, next, list, queue) { 824 list_for_each_entry_safe(td, next, list, queue) {
825 if (!td)
826 continue;
827 if (td->address != address) 825 if (td->address != address)
828 continue; 826 continue;
829 827
@@ -2025,8 +2023,6 @@ static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2025 struct list_head *list = &r8a66597->child_device; 2023 struct list_head *list = &r8a66597->child_device;
2026 2024
2027 list_for_each_entry(dev, list, device_list) { 2025 list_for_each_entry(dev, list, device_list) {
2028 if (!dev)
2029 continue;
2030 if (dev->usb_address != addr) 2026 if (dev->usb_address != addr)
2031 continue; 2027 continue;
2032 2028
diff --git a/drivers/usb/host/whci/debug.c b/drivers/usb/host/whci/debug.c
index 2273c815941f..8c1c610c9513 100644
--- a/drivers/usb/host/whci/debug.c
+++ b/drivers/usb/host/whci/debug.c
@@ -31,17 +31,29 @@ struct whc_dbg {
31 31
32void qset_print(struct seq_file *s, struct whc_qset *qset) 32void qset_print(struct seq_file *s, struct whc_qset *qset)
33{ 33{
34 static const char *qh_type[] = {
35 "ctrl", "isoc", "bulk", "intr", "rsvd", "rsvd", "rsvd", "lpintr", };
34 struct whc_std *std; 36 struct whc_std *std;
35 struct urb *urb = NULL; 37 struct urb *urb = NULL;
36 int i; 38 int i;
37 39
38 seq_printf(s, "qset %08x\n", (u32)qset->qset_dma); 40 seq_printf(s, "qset %08x", (u32)qset->qset_dma);
41 if (&qset->list_node == qset->whc->async_list.prev) {
42 seq_printf(s, " (dummy)\n");
43 } else {
44 seq_printf(s, " ep%d%s-%s maxpkt: %d\n",
45 qset->qh.info1 & 0x0f,
46 (qset->qh.info1 >> 4) & 0x1 ? "in" : "out",
47 qh_type[(qset->qh.info1 >> 5) & 0x7],
48 (qset->qh.info1 >> 16) & 0xffff);
49 }
39 seq_printf(s, " -> %08x\n", (u32)qset->qh.link); 50 seq_printf(s, " -> %08x\n", (u32)qset->qh.link);
40 seq_printf(s, " info: %08x %08x %08x\n", 51 seq_printf(s, " info: %08x %08x %08x\n",
41 qset->qh.info1, qset->qh.info2, qset->qh.info3); 52 qset->qh.info1, qset->qh.info2, qset->qh.info3);
42 seq_printf(s, " sts: %04x errs: %d\n", qset->qh.status, qset->qh.err_count); 53 seq_printf(s, " sts: %04x errs: %d curwin: %08x\n",
54 qset->qh.status, qset->qh.err_count, qset->qh.cur_window);
43 seq_printf(s, " TD: sts: %08x opts: %08x\n", 55 seq_printf(s, " TD: sts: %08x opts: %08x\n",
44 qset->qh.overlay.qtd.status, qset->qh.overlay.qtd.options); 56 qset->qh.overlay.qtd.status, qset->qh.overlay.qtd.options);
45 57
46 for (i = 0; i < WHCI_QSET_TD_MAX; i++) { 58 for (i = 0; i < WHCI_QSET_TD_MAX; i++) {
47 seq_printf(s, " %c%c TD[%d]: sts: %08x opts: %08x ptr: %08x\n", 59 seq_printf(s, " %c%c TD[%d]: sts: %08x opts: %08x ptr: %08x\n",
diff --git a/drivers/usb/host/whci/hcd.c b/drivers/usb/host/whci/hcd.c
index 687b622a1612..e0d3401285c8 100644
--- a/drivers/usb/host/whci/hcd.c
+++ b/drivers/usb/host/whci/hcd.c
@@ -250,6 +250,7 @@ static int whc_probe(struct umc_dev *umc)
250 } 250 }
251 251
252 usb_hcd->wireless = 1; 252 usb_hcd->wireless = 1;
253 usb_hcd->self.sg_tablesize = 2048; /* somewhat arbitrary */
253 254
254 wusbhc = usb_hcd_to_wusbhc(usb_hcd); 255 wusbhc = usb_hcd_to_wusbhc(usb_hcd);
255 whc = wusbhc_to_whc(wusbhc); 256 whc = wusbhc_to_whc(wusbhc);
diff --git a/drivers/usb/host/whci/qset.c b/drivers/usb/host/whci/qset.c
index 1b9dc1571570..7d4204db0f61 100644
--- a/drivers/usb/host/whci/qset.c
+++ b/drivers/usb/host/whci/qset.c
@@ -49,16 +49,19 @@ struct whc_qset *qset_alloc(struct whc *whc, gfp_t mem_flags)
49 * state 49 * state
50 * @urb: an urb for a transfer to this endpoint 50 * @urb: an urb for a transfer to this endpoint
51 */ 51 */
52static void qset_fill_qh(struct whc_qset *qset, struct urb *urb) 52static void qset_fill_qh(struct whc *whc, struct whc_qset *qset, struct urb *urb)
53{ 53{
54 struct usb_device *usb_dev = urb->dev; 54 struct usb_device *usb_dev = urb->dev;
55 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
55 struct usb_wireless_ep_comp_descriptor *epcd; 56 struct usb_wireless_ep_comp_descriptor *epcd;
56 bool is_out; 57 bool is_out;
58 uint8_t phy_rate;
57 59
58 is_out = usb_pipeout(urb->pipe); 60 is_out = usb_pipeout(urb->pipe);
59 61
60 epcd = (struct usb_wireless_ep_comp_descriptor *)qset->ep->extra; 62 qset->max_packet = le16_to_cpu(urb->ep->desc.wMaxPacketSize);
61 63
64 epcd = (struct usb_wireless_ep_comp_descriptor *)qset->ep->extra;
62 if (epcd) { 65 if (epcd) {
63 qset->max_seq = epcd->bMaxSequence; 66 qset->max_seq = epcd->bMaxSequence;
64 qset->max_burst = epcd->bMaxBurst; 67 qset->max_burst = epcd->bMaxBurst;
@@ -67,12 +70,28 @@ static void qset_fill_qh(struct whc_qset *qset, struct urb *urb)
67 qset->max_burst = 1; 70 qset->max_burst = 1;
68 } 71 }
69 72
73 /*
74 * Initial PHY rate is 53.3 Mbit/s for control endpoints or
75 * the maximum supported by the device for other endpoints
76 * (unless limited by the user).
77 */
78 if (usb_pipecontrol(urb->pipe))
79 phy_rate = UWB_PHY_RATE_53;
80 else {
81 uint16_t phy_rates;
82
83 phy_rates = le16_to_cpu(wusb_dev->wusb_cap_descr->wPHYRates);
84 phy_rate = fls(phy_rates) - 1;
85 if (phy_rate > whc->wusbhc.phy_rate)
86 phy_rate = whc->wusbhc.phy_rate;
87 }
88
70 qset->qh.info1 = cpu_to_le32( 89 qset->qh.info1 = cpu_to_le32(
71 QH_INFO1_EP(usb_pipeendpoint(urb->pipe)) 90 QH_INFO1_EP(usb_pipeendpoint(urb->pipe))
72 | (is_out ? QH_INFO1_DIR_OUT : QH_INFO1_DIR_IN) 91 | (is_out ? QH_INFO1_DIR_OUT : QH_INFO1_DIR_IN)
73 | usb_pipe_to_qh_type(urb->pipe) 92 | usb_pipe_to_qh_type(urb->pipe)
74 | QH_INFO1_DEV_INFO_IDX(wusb_port_no_to_idx(usb_dev->portnum)) 93 | QH_INFO1_DEV_INFO_IDX(wusb_port_no_to_idx(usb_dev->portnum))
75 | QH_INFO1_MAX_PKT_LEN(usb_maxpacket(urb->dev, urb->pipe, is_out)) 94 | QH_INFO1_MAX_PKT_LEN(qset->max_packet)
76 ); 95 );
77 qset->qh.info2 = cpu_to_le32( 96 qset->qh.info2 = cpu_to_le32(
78 QH_INFO2_BURST(qset->max_burst) 97 QH_INFO2_BURST(qset->max_burst)
@@ -86,7 +105,7 @@ static void qset_fill_qh(struct whc_qset *qset, struct urb *urb)
86 * strength and can presumably guess the Tx power required 105 * strength and can presumably guess the Tx power required
87 * from that? */ 106 * from that? */
88 qset->qh.info3 = cpu_to_le32( 107 qset->qh.info3 = cpu_to_le32(
89 QH_INFO3_TX_RATE_53_3 108 QH_INFO3_TX_RATE(phy_rate)
90 | QH_INFO3_TX_PWR(0) /* 0 == max power */ 109 | QH_INFO3_TX_PWR(0) /* 0 == max power */
91 ); 110 );
92 111
@@ -148,7 +167,7 @@ struct whc_qset *get_qset(struct whc *whc, struct urb *urb,
148 167
149 qset->ep = urb->ep; 168 qset->ep = urb->ep;
150 urb->ep->hcpriv = qset; 169 urb->ep->hcpriv = qset;
151 qset_fill_qh(qset, urb); 170 qset_fill_qh(whc, qset, urb);
152 } 171 }
153 return qset; 172 return qset;
154} 173}
@@ -241,6 +260,36 @@ static void qset_remove_qtd(struct whc *whc, struct whc_qset *qset)
241 qset->ntds--; 260 qset->ntds--;
242} 261}
243 262
263static void qset_copy_bounce_to_sg(struct whc *whc, struct whc_std *std)
264{
265 struct scatterlist *sg;
266 void *bounce;
267 size_t remaining, offset;
268
269 bounce = std->bounce_buf;
270 remaining = std->len;
271
272 sg = std->bounce_sg;
273 offset = std->bounce_offset;
274
275 while (remaining) {
276 size_t len;
277
278 len = min(sg->length - offset, remaining);
279 memcpy(sg_virt(sg) + offset, bounce, len);
280
281 bounce += len;
282 remaining -= len;
283
284 offset += len;
285 if (offset >= sg->length) {
286 sg = sg_next(sg);
287 offset = 0;
288 }
289 }
290
291}
292
244/** 293/**
245 * qset_free_std - remove an sTD and free it. 294 * qset_free_std - remove an sTD and free it.
246 * @whc: the WHCI host controller 295 * @whc: the WHCI host controller
@@ -249,13 +298,29 @@ static void qset_remove_qtd(struct whc *whc, struct whc_qset *qset)
249void qset_free_std(struct whc *whc, struct whc_std *std) 298void qset_free_std(struct whc *whc, struct whc_std *std)
250{ 299{
251 list_del(&std->list_node); 300 list_del(&std->list_node);
252 if (std->num_pointers) { 301 if (std->bounce_buf) {
253 dma_unmap_single(whc->wusbhc.dev, std->dma_addr, 302 bool is_out = usb_pipeout(std->urb->pipe);
254 std->num_pointers * sizeof(struct whc_page_list_entry), 303 dma_addr_t dma_addr;
255 DMA_TO_DEVICE); 304
305 if (std->num_pointers)
306 dma_addr = le64_to_cpu(std->pl_virt[0].buf_ptr);
307 else
308 dma_addr = std->dma_addr;
309
310 dma_unmap_single(whc->wusbhc.dev, dma_addr,
311 std->len, is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
312 if (!is_out)
313 qset_copy_bounce_to_sg(whc, std);
314 kfree(std->bounce_buf);
315 }
316 if (std->pl_virt) {
317 if (std->dma_addr)
318 dma_unmap_single(whc->wusbhc.dev, std->dma_addr,
319 std->num_pointers * sizeof(struct whc_page_list_entry),
320 DMA_TO_DEVICE);
256 kfree(std->pl_virt); 321 kfree(std->pl_virt);
322 std->pl_virt = NULL;
257 } 323 }
258
259 kfree(std); 324 kfree(std);
260} 325}
261 326
@@ -293,12 +358,17 @@ static int qset_fill_page_list(struct whc *whc, struct whc_std *std, gfp_t mem_f
293{ 358{
294 dma_addr_t dma_addr = std->dma_addr; 359 dma_addr_t dma_addr = std->dma_addr;
295 dma_addr_t sp, ep; 360 dma_addr_t sp, ep;
296 size_t std_len = std->len;
297 size_t pl_len; 361 size_t pl_len;
298 int p; 362 int p;
299 363
300 sp = ALIGN(dma_addr, WHCI_PAGE_SIZE); 364 /* Short buffers don't need a page list. */
301 ep = dma_addr + std_len; 365 if (std->len <= WHCI_PAGE_SIZE) {
366 std->num_pointers = 0;
367 return 0;
368 }
369
370 sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
371 ep = dma_addr + std->len;
302 std->num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE); 372 std->num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
303 373
304 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry); 374 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
@@ -309,7 +379,7 @@ static int qset_fill_page_list(struct whc *whc, struct whc_std *std, gfp_t mem_f
309 379
310 for (p = 0; p < std->num_pointers; p++) { 380 for (p = 0; p < std->num_pointers; p++) {
311 std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr); 381 std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
312 dma_addr = ALIGN(dma_addr + WHCI_PAGE_SIZE, WHCI_PAGE_SIZE); 382 dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
313 } 383 }
314 384
315 return 0; 385 return 0;
@@ -339,6 +409,218 @@ static void urb_dequeue_work(struct work_struct *work)
339 spin_unlock_irqrestore(&whc->lock, flags); 409 spin_unlock_irqrestore(&whc->lock, flags);
340} 410}
341 411
412static struct whc_std *qset_new_std(struct whc *whc, struct whc_qset *qset,
413 struct urb *urb, gfp_t mem_flags)
414{
415 struct whc_std *std;
416
417 std = kzalloc(sizeof(struct whc_std), mem_flags);
418 if (std == NULL)
419 return NULL;
420
421 std->urb = urb;
422 std->qtd = NULL;
423
424 INIT_LIST_HEAD(&std->list_node);
425 list_add_tail(&std->list_node, &qset->stds);
426
427 return std;
428}
429
430static int qset_add_urb_sg(struct whc *whc, struct whc_qset *qset, struct urb *urb,
431 gfp_t mem_flags)
432{
433 size_t remaining;
434 struct scatterlist *sg;
435 int i;
436 int ntds = 0;
437 struct whc_std *std = NULL;
438 struct whc_page_list_entry *entry;
439 dma_addr_t prev_end = 0;
440 size_t pl_len;
441 int p = 0;
442
443 remaining = urb->transfer_buffer_length;
444
445 for_each_sg(urb->sg->sg, sg, urb->num_sgs, i) {
446 dma_addr_t dma_addr;
447 size_t dma_remaining;
448 dma_addr_t sp, ep;
449 int num_pointers;
450
451 if (remaining == 0) {
452 break;
453 }
454
455 dma_addr = sg_dma_address(sg);
456 dma_remaining = min_t(size_t, sg_dma_len(sg), remaining);
457
458 while (dma_remaining) {
459 size_t dma_len;
460
461 /*
462 * We can use the previous std (if it exists) provided that:
463 * - the previous one ended on a page boundary.
464 * - the current one begins on a page boundary.
465 * - the previous one isn't full.
466 *
467 * If a new std is needed but the previous one
468 * was not a whole number of packets then this
469 * sg list cannot be mapped onto multiple
470 * qTDs. Return an error and let the caller
471 * sort it out.
472 */
473 if (!std
474 || (prev_end & (WHCI_PAGE_SIZE-1))
475 || (dma_addr & (WHCI_PAGE_SIZE-1))
476 || std->len + WHCI_PAGE_SIZE > QTD_MAX_XFER_SIZE) {
477 if (std->len % qset->max_packet != 0)
478 return -EINVAL;
479 std = qset_new_std(whc, qset, urb, mem_flags);
480 if (std == NULL) {
481 return -ENOMEM;
482 }
483 ntds++;
484 p = 0;
485 }
486
487 dma_len = dma_remaining;
488
489 /*
490 * If the remainder of this element doesn't
491 * fit in a single qTD, limit the qTD to a
492 * whole number of packets. This allows the
493 * remainder to go into the next qTD.
494 */
495 if (std->len + dma_len > QTD_MAX_XFER_SIZE) {
496 dma_len = (QTD_MAX_XFER_SIZE / qset->max_packet)
497 * qset->max_packet - std->len;
498 }
499
500 std->len += dma_len;
501 std->ntds_remaining = -1; /* filled in later */
502
503 sp = dma_addr & ~(WHCI_PAGE_SIZE-1);
504 ep = dma_addr + dma_len;
505 num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
506 std->num_pointers += num_pointers;
507
508 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
509
510 std->pl_virt = krealloc(std->pl_virt, pl_len, mem_flags);
511 if (std->pl_virt == NULL) {
512 return -ENOMEM;
513 }
514
515 for (;p < std->num_pointers; p++, entry++) {
516 std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
517 dma_addr = (dma_addr + WHCI_PAGE_SIZE) & ~(WHCI_PAGE_SIZE-1);
518 }
519
520 prev_end = dma_addr = ep;
521 dma_remaining -= dma_len;
522 remaining -= dma_len;
523 }
524 }
525
526 /* Now the number of stds is know, go back and fill in
527 std->ntds_remaining. */
528 list_for_each_entry(std, &qset->stds, list_node) {
529 if (std->ntds_remaining == -1) {
530 pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
531 std->ntds_remaining = ntds--;
532 std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt,
533 pl_len, DMA_TO_DEVICE);
534 }
535 }
536 return 0;
537}
538
539/**
540 * qset_add_urb_sg_linearize - add an urb with sg list, copying the data
541 *
542 * If the URB contains an sg list whose elements cannot be directly
543 * mapped to qTDs then the data must be transferred via bounce
544 * buffers.
545 */
546static int qset_add_urb_sg_linearize(struct whc *whc, struct whc_qset *qset,
547 struct urb *urb, gfp_t mem_flags)
548{
549 bool is_out = usb_pipeout(urb->pipe);
550 size_t max_std_len;
551 size_t remaining;
552 int ntds = 0;
553 struct whc_std *std = NULL;
554 void *bounce = NULL;
555 struct scatterlist *sg;
556 int i;
557
558 /* limit maximum bounce buffer to 16 * 3.5 KiB ~= 28 k */
559 max_std_len = qset->max_burst * qset->max_packet;
560
561 remaining = urb->transfer_buffer_length;
562
563 for_each_sg(urb->sg->sg, sg, urb->sg->nents, i) {
564 size_t len;
565 size_t sg_remaining;
566 void *orig;
567
568 if (remaining == 0) {
569 break;
570 }
571
572 sg_remaining = min_t(size_t, remaining, sg->length);
573 orig = sg_virt(sg);
574
575 while (sg_remaining) {
576 if (!std || std->len == max_std_len) {
577 std = qset_new_std(whc, qset, urb, mem_flags);
578 if (std == NULL)
579 return -ENOMEM;
580 std->bounce_buf = kmalloc(max_std_len, mem_flags);
581 if (std->bounce_buf == NULL)
582 return -ENOMEM;
583 std->bounce_sg = sg;
584 std->bounce_offset = orig - sg_virt(sg);
585 bounce = std->bounce_buf;
586 ntds++;
587 }
588
589 len = min(sg_remaining, max_std_len - std->len);
590
591 if (is_out)
592 memcpy(bounce, orig, len);
593
594 std->len += len;
595 std->ntds_remaining = -1; /* filled in later */
596
597 bounce += len;
598 orig += len;
599 sg_remaining -= len;
600 remaining -= len;
601 }
602 }
603
604 /*
605 * For each of the new sTDs, map the bounce buffers, create
606 * page lists (if necessary), and fill in std->ntds_remaining.
607 */
608 list_for_each_entry(std, &qset->stds, list_node) {
609 if (std->ntds_remaining != -1)
610 continue;
611
612 std->dma_addr = dma_map_single(&whc->umc->dev, std->bounce_buf, std->len,
613 is_out ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
614
615 if (qset_fill_page_list(whc, std, mem_flags) < 0)
616 return -ENOMEM;
617
618 std->ntds_remaining = ntds--;
619 }
620
621 return 0;
622}
623
342/** 624/**
343 * qset_add_urb - add an urb to the qset's queue. 625 * qset_add_urb - add an urb to the qset's queue.
344 * 626 *
@@ -353,10 +635,7 @@ int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
353 int remaining = urb->transfer_buffer_length; 635 int remaining = urb->transfer_buffer_length;
354 u64 transfer_dma = urb->transfer_dma; 636 u64 transfer_dma = urb->transfer_dma;
355 int ntds_remaining; 637 int ntds_remaining;
356 638 int ret;
357 ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
358 if (ntds_remaining == 0)
359 ntds_remaining = 1;
360 639
361 wurb = kzalloc(sizeof(struct whc_urb), mem_flags); 640 wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
362 if (wurb == NULL) 641 if (wurb == NULL)
@@ -366,32 +645,39 @@ int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
366 wurb->urb = urb; 645 wurb->urb = urb;
367 INIT_WORK(&wurb->dequeue_work, urb_dequeue_work); 646 INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
368 647
648 if (urb->sg) {
649 ret = qset_add_urb_sg(whc, qset, urb, mem_flags);
650 if (ret == -EINVAL) {
651 qset_free_stds(qset, urb);
652 ret = qset_add_urb_sg_linearize(whc, qset, urb, mem_flags);
653 }
654 if (ret < 0)
655 goto err_no_mem;
656 return 0;
657 }
658
659 ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
660 if (ntds_remaining == 0)
661 ntds_remaining = 1;
662
369 while (ntds_remaining) { 663 while (ntds_remaining) {
370 struct whc_std *std; 664 struct whc_std *std;
371 size_t std_len; 665 size_t std_len;
372 666
373 std = kmalloc(sizeof(struct whc_std), mem_flags);
374 if (std == NULL)
375 goto err_no_mem;
376
377 std_len = remaining; 667 std_len = remaining;
378 if (std_len > QTD_MAX_XFER_SIZE) 668 if (std_len > QTD_MAX_XFER_SIZE)
379 std_len = QTD_MAX_XFER_SIZE; 669 std_len = QTD_MAX_XFER_SIZE;
380 670
381 std->urb = urb; 671 std = qset_new_std(whc, qset, urb, mem_flags);
672 if (std == NULL)
673 goto err_no_mem;
674
382 std->dma_addr = transfer_dma; 675 std->dma_addr = transfer_dma;
383 std->len = std_len; 676 std->len = std_len;
384 std->ntds_remaining = ntds_remaining; 677 std->ntds_remaining = ntds_remaining;
385 std->qtd = NULL;
386 678
387 INIT_LIST_HEAD(&std->list_node); 679 if (qset_fill_page_list(whc, std, mem_flags) < 0)
388 list_add_tail(&std->list_node, &qset->stds); 680 goto err_no_mem;
389
390 if (std_len > WHCI_PAGE_SIZE) {
391 if (qset_fill_page_list(whc, std, mem_flags) < 0)
392 goto err_no_mem;
393 } else
394 std->num_pointers = 0;
395 681
396 ntds_remaining--; 682 ntds_remaining--;
397 remaining -= std_len; 683 remaining -= std_len;
diff --git a/drivers/usb/host/whci/whcd.h b/drivers/usb/host/whci/whcd.h
index 24e94d983c5e..c80c7d93bc4a 100644
--- a/drivers/usb/host/whci/whcd.h
+++ b/drivers/usb/host/whci/whcd.h
@@ -84,6 +84,11 @@ struct whc {
84 * @len: the length of data in the associated TD. 84 * @len: the length of data in the associated TD.
85 * @ntds_remaining: number of TDs (starting from this one) in this transfer. 85 * @ntds_remaining: number of TDs (starting from this one) in this transfer.
86 * 86 *
87 * @bounce_buf: a bounce buffer if the std was from an urb with a sg
88 * list that could not be mapped to qTDs directly.
89 * @bounce_sg: the first scatterlist element bounce_buf is for.
90 * @bounce_offset: the offset into bounce_sg for the start of bounce_buf.
91 *
87 * Queued URBs may require more TDs than are available in a qset so we 92 * Queued URBs may require more TDs than are available in a qset so we
88 * use a list of these "software TDs" (sTDs) to hold per-TD data. 93 * use a list of these "software TDs" (sTDs) to hold per-TD data.
89 */ 94 */
@@ -97,6 +102,10 @@ struct whc_std {
97 int num_pointers; 102 int num_pointers;
98 dma_addr_t dma_addr; 103 dma_addr_t dma_addr;
99 struct whc_page_list_entry *pl_virt; 104 struct whc_page_list_entry *pl_virt;
105
106 void *bounce_buf;
107 struct scatterlist *bounce_sg;
108 unsigned bounce_offset;
100}; 109};
101 110
102/** 111/**
diff --git a/drivers/usb/host/whci/whci-hc.h b/drivers/usb/host/whci/whci-hc.h
index e8d0001605be..4d4cbc0730bf 100644
--- a/drivers/usb/host/whci/whci-hc.h
+++ b/drivers/usb/host/whci/whci-hc.h
@@ -172,14 +172,7 @@ struct whc_qhead {
172#define QH_INFO3_MAX_DELAY(d) ((d) << 0) /* maximum stream delay in 125 us units (isoc only) */ 172#define QH_INFO3_MAX_DELAY(d) ((d) << 0) /* maximum stream delay in 125 us units (isoc only) */
173#define QH_INFO3_INTERVAL(i) ((i) << 16) /* segment interval in 125 us units (isoc only) */ 173#define QH_INFO3_INTERVAL(i) ((i) << 16) /* segment interval in 125 us units (isoc only) */
174 174
175#define QH_INFO3_TX_RATE_53_3 (0 << 24) 175#define QH_INFO3_TX_RATE(r) ((r) << 24) /* PHY rate (see [ECMA-368] section 10.3.1.1) */
176#define QH_INFO3_TX_RATE_80 (1 << 24)
177#define QH_INFO3_TX_RATE_106_7 (2 << 24)
178#define QH_INFO3_TX_RATE_160 (3 << 24)
179#define QH_INFO3_TX_RATE_200 (4 << 24)
180#define QH_INFO3_TX_RATE_320 (5 << 24)
181#define QH_INFO3_TX_RATE_400 (6 << 24)
182#define QH_INFO3_TX_RATE_480 (7 << 24)
183#define QH_INFO3_TX_PWR(p) ((p) << 29) /* transmit power (see [WUSB] section 5.2.1.2) */ 176#define QH_INFO3_TX_PWR(p) ((p) << 29) /* transmit power (see [WUSB] section 5.2.1.2) */
184 177
185#define QH_STATUS_FLOW_CTRL (1 << 15) 178#define QH_STATUS_FLOW_CTRL (1 << 15)
@@ -267,8 +260,9 @@ struct whc_qset {
267 unsigned reset:1; 260 unsigned reset:1;
268 struct urb *pause_after_urb; 261 struct urb *pause_after_urb;
269 struct completion remove_complete; 262 struct completion remove_complete;
270 int max_burst; 263 uint16_t max_packet;
271 int max_seq; 264 uint8_t max_burst;
265 uint8_t max_seq;
272}; 266};
273 267
274static inline void whc_qset_set_link_ptr(u64 *ptr, u64 target) 268static inline void whc_qset_set_link_ptr(u64 *ptr, u64 target)
diff --git a/drivers/usb/host/xhci-hcd.c b/drivers/usb/host/xhci-hcd.c
index 932f99938481..5e92c72df642 100644
--- a/drivers/usb/host/xhci-hcd.c
+++ b/drivers/usb/host/xhci-hcd.c
@@ -67,22 +67,14 @@ static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
67} 67}
68 68
69/* 69/*
70 * Force HC into halt state. 70 * Disable interrupts and begin the xHCI halting process.
71 *
72 * Disable any IRQs and clear the run/stop bit.
73 * HC will complete any current and actively pipelined transactions, and
74 * should halt within 16 microframes of the run/stop bit being cleared.
75 * Read HC Halted bit in the status register to see when the HC is finished.
76 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
77 */ 71 */
78int xhci_halt(struct xhci_hcd *xhci) 72void xhci_quiesce(struct xhci_hcd *xhci)
79{ 73{
80 u32 halted; 74 u32 halted;
81 u32 cmd; 75 u32 cmd;
82 u32 mask; 76 u32 mask;
83 77
84 xhci_dbg(xhci, "// Halt the HC\n");
85 /* Disable all interrupts from the host controller */
86 mask = ~(XHCI_IRQS); 78 mask = ~(XHCI_IRQS);
87 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT; 79 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
88 if (!halted) 80 if (!halted)
@@ -91,6 +83,21 @@ int xhci_halt(struct xhci_hcd *xhci)
91 cmd = xhci_readl(xhci, &xhci->op_regs->command); 83 cmd = xhci_readl(xhci, &xhci->op_regs->command);
92 cmd &= mask; 84 cmd &= mask;
93 xhci_writel(xhci, cmd, &xhci->op_regs->command); 85 xhci_writel(xhci, cmd, &xhci->op_regs->command);
86}
87
88/*
89 * Force HC into halt state.
90 *
91 * Disable any IRQs and clear the run/stop bit.
92 * HC will complete any current and actively pipelined transactions, and
93 * should halt within 16 microframes of the run/stop bit being cleared.
94 * Read HC Halted bit in the status register to see when the HC is finished.
95 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
96 */
97int xhci_halt(struct xhci_hcd *xhci)
98{
99 xhci_dbg(xhci, "// Halt the HC\n");
100 xhci_quiesce(xhci);
94 101
95 return handshake(xhci, &xhci->op_regs->status, 102 return handshake(xhci, &xhci->op_regs->status,
96 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); 103 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
@@ -124,28 +131,6 @@ int xhci_reset(struct xhci_hcd *xhci)
124 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000); 131 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
125} 132}
126 133
127/*
128 * Stop the HC from processing the endpoint queues.
129 */
130static void xhci_quiesce(struct xhci_hcd *xhci)
131{
132 /*
133 * Queues are per endpoint, so we need to disable an endpoint or slot.
134 *
135 * To disable a slot, we need to insert a disable slot command on the
136 * command ring and ring the doorbell. This will also free any internal
137 * resources associated with the slot (which might not be what we want).
138 *
139 * A Release Endpoint command sounds better - doesn't free internal HC
140 * memory, but removes the endpoints from the schedule and releases the
141 * bandwidth, disables the doorbells, and clears the endpoint enable
142 * flag. Usually used prior to a set interface command.
143 *
144 * TODO: Implement after command ring code is done.
145 */
146 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
147 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
148}
149 134
150#if 0 135#if 0
151/* Set up MSI-X table for entry 0 (may claim other entries later) */ 136/* Set up MSI-X table for entry 0 (may claim other entries later) */
@@ -261,8 +246,14 @@ static void xhci_work(struct xhci_hcd *xhci)
261 /* Flush posted writes */ 246 /* Flush posted writes */
262 xhci_readl(xhci, &xhci->ir_set->irq_pending); 247 xhci_readl(xhci, &xhci->ir_set->irq_pending);
263 248
264 /* FIXME this should be a delayed service routine that clears the EHB */ 249 if (xhci->xhc_state & XHCI_STATE_DYING)
265 xhci_handle_event(xhci); 250 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
251 "Shouldn't IRQs be disabled?\n");
252 else
253 /* FIXME this should be a delayed service routine
254 * that clears the EHB.
255 */
256 xhci_handle_event(xhci);
266 257
267 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */ 258 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
268 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); 259 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
@@ -335,7 +326,7 @@ void xhci_event_ring_work(unsigned long arg)
335 spin_lock_irqsave(&xhci->lock, flags); 326 spin_lock_irqsave(&xhci->lock, flags);
336 temp = xhci_readl(xhci, &xhci->op_regs->status); 327 temp = xhci_readl(xhci, &xhci->op_regs->status);
337 xhci_dbg(xhci, "op reg status = 0x%x\n", temp); 328 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
338 if (temp == 0xffffffff) { 329 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
339 xhci_dbg(xhci, "HW died, polling stopped.\n"); 330 xhci_dbg(xhci, "HW died, polling stopped.\n");
340 spin_unlock_irqrestore(&xhci->lock, flags); 331 spin_unlock_irqrestore(&xhci->lock, flags);
341 return; 332 return;
@@ -490,8 +481,6 @@ void xhci_stop(struct usb_hcd *hcd)
490 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 481 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
491 482
492 spin_lock_irq(&xhci->lock); 483 spin_lock_irq(&xhci->lock);
493 if (HC_IS_RUNNING(hcd->state))
494 xhci_quiesce(xhci);
495 xhci_halt(xhci); 484 xhci_halt(xhci);
496 xhci_reset(xhci); 485 xhci_reset(xhci);
497 spin_unlock_irq(&xhci->lock); 486 spin_unlock_irq(&xhci->lock);
@@ -727,16 +716,22 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
727 * atomic context to this function, which may allocate memory. 716 * atomic context to this function, which may allocate memory.
728 */ 717 */
729 spin_lock_irqsave(&xhci->lock, flags); 718 spin_lock_irqsave(&xhci->lock, flags);
719 if (xhci->xhc_state & XHCI_STATE_DYING)
720 goto dying;
730 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, 721 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
731 slot_id, ep_index); 722 slot_id, ep_index);
732 spin_unlock_irqrestore(&xhci->lock, flags); 723 spin_unlock_irqrestore(&xhci->lock, flags);
733 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) { 724 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
734 spin_lock_irqsave(&xhci->lock, flags); 725 spin_lock_irqsave(&xhci->lock, flags);
726 if (xhci->xhc_state & XHCI_STATE_DYING)
727 goto dying;
735 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, 728 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
736 slot_id, ep_index); 729 slot_id, ep_index);
737 spin_unlock_irqrestore(&xhci->lock, flags); 730 spin_unlock_irqrestore(&xhci->lock, flags);
738 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) { 731 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
739 spin_lock_irqsave(&xhci->lock, flags); 732 spin_lock_irqsave(&xhci->lock, flags);
733 if (xhci->xhc_state & XHCI_STATE_DYING)
734 goto dying;
740 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, 735 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
741 slot_id, ep_index); 736 slot_id, ep_index);
742 spin_unlock_irqrestore(&xhci->lock, flags); 737 spin_unlock_irqrestore(&xhci->lock, flags);
@@ -745,6 +740,12 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
745 } 740 }
746exit: 741exit:
747 return ret; 742 return ret;
743dying:
744 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
745 "non-responsive xHCI host.\n",
746 urb->ep->desc.bEndpointAddress, urb);
747 spin_unlock_irqrestore(&xhci->lock, flags);
748 return -ESHUTDOWN;
748} 749}
749 750
750/* 751/*
@@ -806,6 +807,17 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
806 kfree(td); 807 kfree(td);
807 return ret; 808 return ret;
808 } 809 }
810 if (xhci->xhc_state & XHCI_STATE_DYING) {
811 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
812 "non-responsive xHCI host.\n",
813 urb->ep->desc.bEndpointAddress, urb);
814 /* Let the stop endpoint command watchdog timer (which set this
815 * state) finish cleaning up the endpoint TD lists. We must
816 * have caught it in the middle of dropping a lock and giving
817 * back an URB.
818 */
819 goto done;
820 }
809 821
810 xhci_dbg(xhci, "Cancel URB %p\n", urb); 822 xhci_dbg(xhci, "Cancel URB %p\n", urb);
811 xhci_dbg(xhci, "Event ring:\n"); 823 xhci_dbg(xhci, "Event ring:\n");
@@ -817,12 +829,16 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
817 xhci_debug_ring(xhci, ep_ring); 829 xhci_debug_ring(xhci, ep_ring);
818 td = (struct xhci_td *) urb->hcpriv; 830 td = (struct xhci_td *) urb->hcpriv;
819 831
820 ep->cancels_pending++;
821 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); 832 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
822 /* Queue a stop endpoint command, but only if this is 833 /* Queue a stop endpoint command, but only if this is
823 * the first cancellation to be handled. 834 * the first cancellation to be handled.
824 */ 835 */
825 if (ep->cancels_pending == 1) { 836 if (!(ep->ep_state & EP_HALT_PENDING)) {
837 ep->ep_state |= EP_HALT_PENDING;
838 ep->stop_cmds_pending++;
839 ep->stop_cmd_timer.expires = jiffies +
840 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
841 add_timer(&ep->stop_cmd_timer);
826 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index); 842 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
827 xhci_ring_cmd_db(xhci); 843 xhci_ring_cmd_db(xhci);
828 } 844 }
@@ -1246,13 +1262,35 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1246 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); 1262 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1247 1263
1248 xhci_zero_in_ctx(xhci, virt_dev); 1264 xhci_zero_in_ctx(xhci, virt_dev);
1249 /* Free any old rings */ 1265 /* Install new rings and free or cache any old rings */
1250 for (i = 1; i < 31; ++i) { 1266 for (i = 1; i < 31; ++i) {
1251 if (virt_dev->eps[i].new_ring) { 1267 int rings_cached;
1252 xhci_ring_free(xhci, virt_dev->eps[i].ring); 1268
1253 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; 1269 if (!virt_dev->eps[i].new_ring)
1254 virt_dev->eps[i].new_ring = NULL; 1270 continue;
1271 /* Only cache or free the old ring if it exists.
1272 * It may not if this is the first add of an endpoint.
1273 */
1274 if (virt_dev->eps[i].ring) {
1275 rings_cached = virt_dev->num_rings_cached;
1276 if (rings_cached < XHCI_MAX_RINGS_CACHED) {
1277 virt_dev->num_rings_cached++;
1278 rings_cached = virt_dev->num_rings_cached;
1279 virt_dev->ring_cache[rings_cached] =
1280 virt_dev->eps[i].ring;
1281 xhci_dbg(xhci, "Cached old ring, "
1282 "%d ring%s cached\n",
1283 rings_cached,
1284 (rings_cached > 1) ? "s" : "");
1285 } else {
1286 xhci_ring_free(xhci, virt_dev->eps[i].ring);
1287 xhci_dbg(xhci, "Ring cache full (%d rings), "
1288 "freeing ring\n",
1289 virt_dev->num_rings_cached);
1290 }
1255 } 1291 }
1292 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1293 virt_dev->eps[i].new_ring = NULL;
1256 } 1294 }
1257 1295
1258 return ret; 1296 return ret;
@@ -1427,16 +1465,27 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
1427void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) 1465void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1428{ 1466{
1429 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 1467 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1468 struct xhci_virt_device *virt_dev;
1430 unsigned long flags; 1469 unsigned long flags;
1431 u32 state; 1470 u32 state;
1471 int i;
1432 1472
1433 if (udev->slot_id == 0) 1473 if (udev->slot_id == 0)
1434 return; 1474 return;
1475 virt_dev = xhci->devs[udev->slot_id];
1476 if (!virt_dev)
1477 return;
1478
1479 /* Stop any wayward timer functions (which may grab the lock) */
1480 for (i = 0; i < 31; ++i) {
1481 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
1482 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
1483 }
1435 1484
1436 spin_lock_irqsave(&xhci->lock, flags); 1485 spin_lock_irqsave(&xhci->lock, flags);
1437 /* Don't disable the slot if the host controller is dead. */ 1486 /* Don't disable the slot if the host controller is dead. */
1438 state = xhci_readl(xhci, &xhci->op_regs->status); 1487 state = xhci_readl(xhci, &xhci->op_regs->status);
1439 if (state == 0xffffffff) { 1488 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
1440 xhci_free_virt_device(xhci, udev->slot_id); 1489 xhci_free_virt_device(xhci, udev->slot_id);
1441 spin_unlock_irqrestore(&xhci->lock, flags); 1490 spin_unlock_irqrestore(&xhci->lock, flags);
1442 return; 1491 return;
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index b8fd270a8b0d..bffcef7a5545 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -125,6 +125,23 @@ void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
125 kfree(ring); 125 kfree(ring);
126} 126}
127 127
128static void xhci_initialize_ring_info(struct xhci_ring *ring)
129{
130 /* The ring is empty, so the enqueue pointer == dequeue pointer */
131 ring->enqueue = ring->first_seg->trbs;
132 ring->enq_seg = ring->first_seg;
133 ring->dequeue = ring->enqueue;
134 ring->deq_seg = ring->first_seg;
135 /* The ring is initialized to 0. The producer must write 1 to the cycle
136 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
137 * compare CCS to the cycle bit to check ownership, so CCS = 1.
138 */
139 ring->cycle_state = 1;
140 /* Not necessary for new rings, but needed for re-initialized rings */
141 ring->enq_updates = 0;
142 ring->deq_updates = 0;
143}
144
128/** 145/**
129 * Create a new ring with zero or more segments. 146 * Create a new ring with zero or more segments.
130 * 147 *
@@ -173,17 +190,7 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
173 " segment %p (virtual), 0x%llx (DMA)\n", 190 " segment %p (virtual), 0x%llx (DMA)\n",
174 prev, (unsigned long long)prev->dma); 191 prev, (unsigned long long)prev->dma);
175 } 192 }
176 /* The ring is empty, so the enqueue pointer == dequeue pointer */ 193 xhci_initialize_ring_info(ring);
177 ring->enqueue = ring->first_seg->trbs;
178 ring->enq_seg = ring->first_seg;
179 ring->dequeue = ring->enqueue;
180 ring->deq_seg = ring->first_seg;
181 /* The ring is initialized to 0. The producer must write 1 to the cycle
182 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
183 * compare CCS to the cycle bit to check ownership, so CCS = 1.
184 */
185 ring->cycle_state = 1;
186
187 return ring; 194 return ring;
188 195
189fail: 196fail:
@@ -191,6 +198,27 @@ fail:
191 return 0; 198 return 0;
192} 199}
193 200
201/* Zero an endpoint ring (except for link TRBs) and move the enqueue and dequeue
202 * pointers to the beginning of the ring.
203 */
204static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
205 struct xhci_ring *ring)
206{
207 struct xhci_segment *seg = ring->first_seg;
208 do {
209 memset(seg->trbs, 0,
210 sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
211 /* All endpoint rings have link TRBs */
212 xhci_link_segments(xhci, seg, seg->next, 1);
213 seg = seg->next;
214 } while (seg != ring->first_seg);
215 xhci_initialize_ring_info(ring);
216 /* td list should be empty since all URBs have been cancelled,
217 * but just in case...
218 */
219 INIT_LIST_HEAD(&ring->td_list);
220}
221
194#define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32) 222#define CTX_SIZE(_hcc) (HCC_64BYTE_CONTEXT(_hcc) ? 64 : 32)
195 223
196struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci, 224struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
@@ -248,6 +276,15 @@ struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
248 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params))); 276 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
249} 277}
250 278
279static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
280 struct xhci_virt_ep *ep)
281{
282 init_timer(&ep->stop_cmd_timer);
283 ep->stop_cmd_timer.data = (unsigned long) ep;
284 ep->stop_cmd_timer.function = xhci_stop_endpoint_command_watchdog;
285 ep->xhci = xhci;
286}
287
251/* All the xhci_tds in the ring's TD list should be freed at this point */ 288/* All the xhci_tds in the ring's TD list should be freed at this point */
252void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) 289void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
253{ 290{
@@ -267,6 +304,12 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
267 if (dev->eps[i].ring) 304 if (dev->eps[i].ring)
268 xhci_ring_free(xhci, dev->eps[i].ring); 305 xhci_ring_free(xhci, dev->eps[i].ring);
269 306
307 if (dev->ring_cache) {
308 for (i = 0; i < dev->num_rings_cached; i++)
309 xhci_ring_free(xhci, dev->ring_cache[i]);
310 kfree(dev->ring_cache);
311 }
312
270 if (dev->in_ctx) 313 if (dev->in_ctx)
271 xhci_free_container_ctx(xhci, dev->in_ctx); 314 xhci_free_container_ctx(xhci, dev->in_ctx);
272 if (dev->out_ctx) 315 if (dev->out_ctx)
@@ -309,15 +352,25 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
309 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id, 352 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
310 (unsigned long long)dev->in_ctx->dma); 353 (unsigned long long)dev->in_ctx->dma);
311 354
312 /* Initialize the cancellation list for each endpoint */ 355 /* Initialize the cancellation list and watchdog timers for each ep */
313 for (i = 0; i < 31; i++) 356 for (i = 0; i < 31; i++) {
357 xhci_init_endpoint_timer(xhci, &dev->eps[i]);
314 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list); 358 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
359 }
315 360
316 /* Allocate endpoint 0 ring */ 361 /* Allocate endpoint 0 ring */
317 dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags); 362 dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
318 if (!dev->eps[0].ring) 363 if (!dev->eps[0].ring)
319 goto fail; 364 goto fail;
320 365
366 /* Allocate pointers to the ring cache */
367 dev->ring_cache = kzalloc(
368 sizeof(struct xhci_ring *)*XHCI_MAX_RINGS_CACHED,
369 flags);
370 if (!dev->ring_cache)
371 goto fail;
372 dev->num_rings_cached = 0;
373
321 init_completion(&dev->cmd_completion); 374 init_completion(&dev->cmd_completion);
322 INIT_LIST_HEAD(&dev->cmd_list); 375 INIT_LIST_HEAD(&dev->cmd_list);
323 376
@@ -544,8 +597,16 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
544 /* Set up the endpoint ring */ 597 /* Set up the endpoint ring */
545 virt_dev->eps[ep_index].new_ring = 598 virt_dev->eps[ep_index].new_ring =
546 xhci_ring_alloc(xhci, 1, true, mem_flags); 599 xhci_ring_alloc(xhci, 1, true, mem_flags);
547 if (!virt_dev->eps[ep_index].new_ring) 600 if (!virt_dev->eps[ep_index].new_ring) {
548 return -ENOMEM; 601 /* Attempt to use the ring cache */
602 if (virt_dev->num_rings_cached == 0)
603 return -ENOMEM;
604 virt_dev->eps[ep_index].new_ring =
605 virt_dev->ring_cache[virt_dev->num_rings_cached];
606 virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
607 virt_dev->num_rings_cached--;
608 xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
609 }
549 ep_ring = virt_dev->eps[ep_index].new_ring; 610 ep_ring = virt_dev->eps[ep_index].new_ring;
550 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state; 611 ep_ctx->deq = ep_ring->first_seg->dma | ep_ring->cycle_state;
551 612
@@ -768,14 +829,17 @@ struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
768 829
769 command->in_ctx = 830 command->in_ctx =
770 xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, mem_flags); 831 xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, mem_flags);
771 if (!command->in_ctx) 832 if (!command->in_ctx) {
833 kfree(command);
772 return NULL; 834 return NULL;
835 }
773 836
774 if (allocate_completion) { 837 if (allocate_completion) {
775 command->completion = 838 command->completion =
776 kzalloc(sizeof(struct completion), mem_flags); 839 kzalloc(sizeof(struct completion), mem_flags);
777 if (!command->completion) { 840 if (!command->completion) {
778 xhci_free_container_ctx(xhci, command->in_ctx); 841 xhci_free_container_ctx(xhci, command->in_ctx);
842 kfree(command);
779 return NULL; 843 return NULL;
780 } 844 }
781 init_completion(command->completion); 845 init_completion(command->completion);
@@ -848,6 +912,163 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
848 xhci->page_shift = 0; 912 xhci->page_shift = 0;
849} 913}
850 914
915static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
916 struct xhci_segment *input_seg,
917 union xhci_trb *start_trb,
918 union xhci_trb *end_trb,
919 dma_addr_t input_dma,
920 struct xhci_segment *result_seg,
921 char *test_name, int test_number)
922{
923 unsigned long long start_dma;
924 unsigned long long end_dma;
925 struct xhci_segment *seg;
926
927 start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
928 end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
929
930 seg = trb_in_td(input_seg, start_trb, end_trb, input_dma);
931 if (seg != result_seg) {
932 xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
933 test_name, test_number);
934 xhci_warn(xhci, "Tested TRB math w/ seg %p and "
935 "input DMA 0x%llx\n",
936 input_seg,
937 (unsigned long long) input_dma);
938 xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
939 "ending TRB %p (0x%llx DMA)\n",
940 start_trb, start_dma,
941 end_trb, end_dma);
942 xhci_warn(xhci, "Expected seg %p, got seg %p\n",
943 result_seg, seg);
944 return -1;
945 }
946 return 0;
947}
948
949/* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
950static int xhci_check_trb_in_td_math(struct xhci_hcd *xhci, gfp_t mem_flags)
951{
952 struct {
953 dma_addr_t input_dma;
954 struct xhci_segment *result_seg;
955 } simple_test_vector [] = {
956 /* A zeroed DMA field should fail */
957 { 0, NULL },
958 /* One TRB before the ring start should fail */
959 { xhci->event_ring->first_seg->dma - 16, NULL },
960 /* One byte before the ring start should fail */
961 { xhci->event_ring->first_seg->dma - 1, NULL },
962 /* Starting TRB should succeed */
963 { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
964 /* Ending TRB should succeed */
965 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
966 xhci->event_ring->first_seg },
967 /* One byte after the ring end should fail */
968 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
969 /* One TRB after the ring end should fail */
970 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
971 /* An address of all ones should fail */
972 { (dma_addr_t) (~0), NULL },
973 };
974 struct {
975 struct xhci_segment *input_seg;
976 union xhci_trb *start_trb;
977 union xhci_trb *end_trb;
978 dma_addr_t input_dma;
979 struct xhci_segment *result_seg;
980 } complex_test_vector [] = {
981 /* Test feeding a valid DMA address from a different ring */
982 { .input_seg = xhci->event_ring->first_seg,
983 .start_trb = xhci->event_ring->first_seg->trbs,
984 .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
985 .input_dma = xhci->cmd_ring->first_seg->dma,
986 .result_seg = NULL,
987 },
988 /* Test feeding a valid end TRB from a different ring */
989 { .input_seg = xhci->event_ring->first_seg,
990 .start_trb = xhci->event_ring->first_seg->trbs,
991 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
992 .input_dma = xhci->cmd_ring->first_seg->dma,
993 .result_seg = NULL,
994 },
995 /* Test feeding a valid start and end TRB from a different ring */
996 { .input_seg = xhci->event_ring->first_seg,
997 .start_trb = xhci->cmd_ring->first_seg->trbs,
998 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
999 .input_dma = xhci->cmd_ring->first_seg->dma,
1000 .result_seg = NULL,
1001 },
1002 /* TRB in this ring, but after this TD */
1003 { .input_seg = xhci->event_ring->first_seg,
1004 .start_trb = &xhci->event_ring->first_seg->trbs[0],
1005 .end_trb = &xhci->event_ring->first_seg->trbs[3],
1006 .input_dma = xhci->event_ring->first_seg->dma + 4*16,
1007 .result_seg = NULL,
1008 },
1009 /* TRB in this ring, but before this TD */
1010 { .input_seg = xhci->event_ring->first_seg,
1011 .start_trb = &xhci->event_ring->first_seg->trbs[3],
1012 .end_trb = &xhci->event_ring->first_seg->trbs[6],
1013 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
1014 .result_seg = NULL,
1015 },
1016 /* TRB in this ring, but after this wrapped TD */
1017 { .input_seg = xhci->event_ring->first_seg,
1018 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1019 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1020 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
1021 .result_seg = NULL,
1022 },
1023 /* TRB in this ring, but before this wrapped TD */
1024 { .input_seg = xhci->event_ring->first_seg,
1025 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1026 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1027 .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
1028 .result_seg = NULL,
1029 },
1030 /* TRB not in this ring, and we have a wrapped TD */
1031 { .input_seg = xhci->event_ring->first_seg,
1032 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
1033 .end_trb = &xhci->event_ring->first_seg->trbs[1],
1034 .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
1035 .result_seg = NULL,
1036 },
1037 };
1038
1039 unsigned int num_tests;
1040 int i, ret;
1041
1042 num_tests = sizeof(simple_test_vector) / sizeof(simple_test_vector[0]);
1043 for (i = 0; i < num_tests; i++) {
1044 ret = xhci_test_trb_in_td(xhci,
1045 xhci->event_ring->first_seg,
1046 xhci->event_ring->first_seg->trbs,
1047 &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
1048 simple_test_vector[i].input_dma,
1049 simple_test_vector[i].result_seg,
1050 "Simple", i);
1051 if (ret < 0)
1052 return ret;
1053 }
1054
1055 num_tests = sizeof(complex_test_vector) / sizeof(complex_test_vector[0]);
1056 for (i = 0; i < num_tests; i++) {
1057 ret = xhci_test_trb_in_td(xhci,
1058 complex_test_vector[i].input_seg,
1059 complex_test_vector[i].start_trb,
1060 complex_test_vector[i].end_trb,
1061 complex_test_vector[i].input_dma,
1062 complex_test_vector[i].result_seg,
1063 "Complex", i);
1064 if (ret < 0)
1065 return ret;
1066 }
1067 xhci_dbg(xhci, "TRB math tests passed.\n");
1068 return 0;
1069}
1070
1071
851int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) 1072int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
852{ 1073{
853 dma_addr_t dma; 1074 dma_addr_t dma;
@@ -951,6 +1172,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
951 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags); 1172 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
952 if (!xhci->event_ring) 1173 if (!xhci->event_ring)
953 goto fail; 1174 goto fail;
1175 if (xhci_check_trb_in_td_math(xhci, flags) < 0)
1176 goto fail;
954 1177
955 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev), 1178 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
956 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma); 1179 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
index 06595ec27bb7..e097008d6fb1 100644
--- a/drivers/usb/host/xhci-pci.c
+++ b/drivers/usb/host/xhci-pci.c
@@ -54,6 +54,8 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
54 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 54 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
55 int retval; 55 int retval;
56 56
57 hcd->self.sg_tablesize = TRBS_PER_SEGMENT - 1;
58
57 xhci->cap_regs = hcd->regs; 59 xhci->cap_regs = hcd->regs;
58 xhci->op_regs = hcd->regs + 60 xhci->op_regs = hcd->regs +
59 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase)); 61 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 821b7b4709de..ee7bc7ecbc59 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -306,7 +306,7 @@ static void ring_ep_doorbell(struct xhci_hcd *xhci,
306 /* Don't ring the doorbell for this endpoint if there are pending 306 /* Don't ring the doorbell for this endpoint if there are pending
307 * cancellations because the we don't want to interrupt processing. 307 * cancellations because the we don't want to interrupt processing.
308 */ 308 */
309 if (!ep->cancels_pending && !(ep_state & SET_DEQ_PENDING) 309 if (!(ep_state & EP_HALT_PENDING) && !(ep_state & SET_DEQ_PENDING)
310 && !(ep_state & EP_HALTED)) { 310 && !(ep_state & EP_HALTED)) {
311 field = xhci_readl(xhci, db_addr) & DB_MASK; 311 field = xhci_readl(xhci, db_addr) & DB_MASK;
312 xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr); 312 xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
@@ -475,6 +475,35 @@ void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
475 ep->ep_state |= SET_DEQ_PENDING; 475 ep->ep_state |= SET_DEQ_PENDING;
476} 476}
477 477
478static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
479 struct xhci_virt_ep *ep)
480{
481 ep->ep_state &= ~EP_HALT_PENDING;
482 /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
483 * timer is running on another CPU, we don't decrement stop_cmds_pending
484 * (since we didn't successfully stop the watchdog timer).
485 */
486 if (del_timer(&ep->stop_cmd_timer))
487 ep->stop_cmds_pending--;
488}
489
490/* Must be called with xhci->lock held in interrupt context */
491static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
492 struct xhci_td *cur_td, int status, char *adjective)
493{
494 struct usb_hcd *hcd = xhci_to_hcd(xhci);
495
496 cur_td->urb->hcpriv = NULL;
497 usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
498 xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
499
500 spin_unlock(&xhci->lock);
501 usb_hcd_giveback_urb(hcd, cur_td->urb, status);
502 kfree(cur_td);
503 spin_lock(&xhci->lock);
504 xhci_dbg(xhci, "%s URB given back\n", adjective);
505}
506
478/* 507/*
479 * When we get a command completion for a Stop Endpoint Command, we need to 508 * When we get a command completion for a Stop Endpoint Command, we need to
480 * unlink any cancelled TDs from the ring. There are two ways to do that: 509 * unlink any cancelled TDs from the ring. There are two ways to do that:
@@ -497,9 +526,6 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
497 struct xhci_td *last_unlinked_td; 526 struct xhci_td *last_unlinked_td;
498 527
499 struct xhci_dequeue_state deq_state; 528 struct xhci_dequeue_state deq_state;
500#ifdef CONFIG_USB_HCD_STAT
501 ktime_t stop_time = ktime_get();
502#endif
503 529
504 memset(&deq_state, 0, sizeof(deq_state)); 530 memset(&deq_state, 0, sizeof(deq_state));
505 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]); 531 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
@@ -507,8 +533,11 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
507 ep = &xhci->devs[slot_id]->eps[ep_index]; 533 ep = &xhci->devs[slot_id]->eps[ep_index];
508 ep_ring = ep->ring; 534 ep_ring = ep->ring;
509 535
510 if (list_empty(&ep->cancelled_td_list)) 536 if (list_empty(&ep->cancelled_td_list)) {
537 xhci_stop_watchdog_timer_in_irq(xhci, ep);
538 ring_ep_doorbell(xhci, slot_id, ep_index);
511 return; 539 return;
540 }
512 541
513 /* Fix up the ep ring first, so HW stops executing cancelled TDs. 542 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
514 * We have the xHCI lock, so nothing can modify this list until we drop 543 * We have the xHCI lock, so nothing can modify this list until we drop
@@ -535,9 +564,9 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
535 * the cancelled TD list for URB completion later. 564 * the cancelled TD list for URB completion later.
536 */ 565 */
537 list_del(&cur_td->td_list); 566 list_del(&cur_td->td_list);
538 ep->cancels_pending--;
539 } 567 }
540 last_unlinked_td = cur_td; 568 last_unlinked_td = cur_td;
569 xhci_stop_watchdog_timer_in_irq(xhci, ep);
541 570
542 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */ 571 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
543 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) { 572 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
@@ -561,27 +590,136 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
561 list_del(&cur_td->cancelled_td_list); 590 list_del(&cur_td->cancelled_td_list);
562 591
563 /* Clean up the cancelled URB */ 592 /* Clean up the cancelled URB */
564#ifdef CONFIG_USB_HCD_STAT
565 hcd_stat_update(xhci->tp_stat, cur_td->urb->actual_length,
566 ktime_sub(stop_time, cur_td->start_time));
567#endif
568 cur_td->urb->hcpriv = NULL;
569 usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), cur_td->urb);
570
571 xhci_dbg(xhci, "Giveback cancelled URB %p\n", cur_td->urb);
572 spin_unlock(&xhci->lock);
573 /* Doesn't matter what we pass for status, since the core will 593 /* Doesn't matter what we pass for status, since the core will
574 * just overwrite it (because the URB has been unlinked). 594 * just overwrite it (because the URB has been unlinked).
575 */ 595 */
576 usb_hcd_giveback_urb(xhci_to_hcd(xhci), cur_td->urb, 0); 596 xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
577 kfree(cur_td);
578 597
579 spin_lock(&xhci->lock); 598 /* Stop processing the cancelled list if the watchdog timer is
599 * running.
600 */
601 if (xhci->xhc_state & XHCI_STATE_DYING)
602 return;
580 } while (cur_td != last_unlinked_td); 603 } while (cur_td != last_unlinked_td);
581 604
582 /* Return to the event handler with xhci->lock re-acquired */ 605 /* Return to the event handler with xhci->lock re-acquired */
583} 606}
584 607
608/* Watchdog timer function for when a stop endpoint command fails to complete.
609 * In this case, we assume the host controller is broken or dying or dead. The
610 * host may still be completing some other events, so we have to be careful to
611 * let the event ring handler and the URB dequeueing/enqueueing functions know
612 * through xhci->state.
613 *
614 * The timer may also fire if the host takes a very long time to respond to the
615 * command, and the stop endpoint command completion handler cannot delete the
616 * timer before the timer function is called. Another endpoint cancellation may
617 * sneak in before the timer function can grab the lock, and that may queue
618 * another stop endpoint command and add the timer back. So we cannot use a
619 * simple flag to say whether there is a pending stop endpoint command for a
620 * particular endpoint.
621 *
622 * Instead we use a combination of that flag and a counter for the number of
623 * pending stop endpoint commands. If the timer is the tail end of the last
624 * stop endpoint command, and the endpoint's command is still pending, we assume
625 * the host is dying.
626 */
627void xhci_stop_endpoint_command_watchdog(unsigned long arg)
628{
629 struct xhci_hcd *xhci;
630 struct xhci_virt_ep *ep;
631 struct xhci_virt_ep *temp_ep;
632 struct xhci_ring *ring;
633 struct xhci_td *cur_td;
634 int ret, i, j;
635
636 ep = (struct xhci_virt_ep *) arg;
637 xhci = ep->xhci;
638
639 spin_lock(&xhci->lock);
640
641 ep->stop_cmds_pending--;
642 if (xhci->xhc_state & XHCI_STATE_DYING) {
643 xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
644 "xHCI as DYING, exiting.\n");
645 spin_unlock(&xhci->lock);
646 return;
647 }
648 if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
649 xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
650 "exiting.\n");
651 spin_unlock(&xhci->lock);
652 return;
653 }
654
655 xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
656 xhci_warn(xhci, "Assuming host is dying, halting host.\n");
657 /* Oops, HC is dead or dying or at least not responding to the stop
658 * endpoint command.
659 */
660 xhci->xhc_state |= XHCI_STATE_DYING;
661 /* Disable interrupts from the host controller and start halting it */
662 xhci_quiesce(xhci);
663 spin_unlock(&xhci->lock);
664
665 ret = xhci_halt(xhci);
666
667 spin_lock(&xhci->lock);
668 if (ret < 0) {
669 /* This is bad; the host is not responding to commands and it's
670 * not allowing itself to be halted. At least interrupts are
671 * disabled, so we can set HC_STATE_HALT and notify the
672 * USB core. But if we call usb_hc_died(), it will attempt to
673 * disconnect all device drivers under this host. Those
674 * disconnect() methods will wait for all URBs to be unlinked,
675 * so we must complete them.
676 */
677 xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
678 xhci_warn(xhci, "Completing active URBs anyway.\n");
679 /* We could turn all TDs on the rings to no-ops. This won't
680 * help if the host has cached part of the ring, and is slow if
681 * we want to preserve the cycle bit. Skip it and hope the host
682 * doesn't touch the memory.
683 */
684 }
685 for (i = 0; i < MAX_HC_SLOTS; i++) {
686 if (!xhci->devs[i])
687 continue;
688 for (j = 0; j < 31; j++) {
689 temp_ep = &xhci->devs[i]->eps[j];
690 ring = temp_ep->ring;
691 if (!ring)
692 continue;
693 xhci_dbg(xhci, "Killing URBs for slot ID %u, "
694 "ep index %u\n", i, j);
695 while (!list_empty(&ring->td_list)) {
696 cur_td = list_first_entry(&ring->td_list,
697 struct xhci_td,
698 td_list);
699 list_del(&cur_td->td_list);
700 if (!list_empty(&cur_td->cancelled_td_list))
701 list_del(&cur_td->cancelled_td_list);
702 xhci_giveback_urb_in_irq(xhci, cur_td,
703 -ESHUTDOWN, "killed");
704 }
705 while (!list_empty(&temp_ep->cancelled_td_list)) {
706 cur_td = list_first_entry(
707 &temp_ep->cancelled_td_list,
708 struct xhci_td,
709 cancelled_td_list);
710 list_del(&cur_td->cancelled_td_list);
711 xhci_giveback_urb_in_irq(xhci, cur_td,
712 -ESHUTDOWN, "killed");
713 }
714 }
715 }
716 spin_unlock(&xhci->lock);
717 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
718 xhci_dbg(xhci, "Calling usb_hc_died()\n");
719 usb_hc_died(xhci_to_hcd(xhci));
720 xhci_dbg(xhci, "xHCI host controller is dead.\n");
721}
722
585/* 723/*
586 * When we get a completion for a Set Transfer Ring Dequeue Pointer command, 724 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
587 * we need to clear the set deq pending flag in the endpoint ring state, so that 725 * we need to clear the set deq pending flag in the endpoint ring state, so that
@@ -765,28 +903,32 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
765 virt_dev->in_ctx); 903 virt_dev->in_ctx);
766 /* Input ctx add_flags are the endpoint index plus one */ 904 /* Input ctx add_flags are the endpoint index plus one */
767 ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1; 905 ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
768 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; 906 /* A usb_set_interface() call directly after clearing a halted
769 if (!ep_ring) { 907 * condition may race on this quirky hardware.
770 /* This must have been an initial configure endpoint */ 908 * Not worth worrying about, since this is prototype hardware.
771 xhci->devs[slot_id]->cmd_status = 909 */
772 GET_COMP_CODE(event->status);
773 complete(&xhci->devs[slot_id]->cmd_completion);
774 break;
775 }
776 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
777 xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
778 "state = %d\n", ep_index, ep_state);
779 if (xhci->quirks & XHCI_RESET_EP_QUIRK && 910 if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
780 ep_state & EP_HALTED) { 911 ep_index != (unsigned int) -1 &&
912 ctrl_ctx->add_flags - SLOT_FLAG ==
913 ctrl_ctx->drop_flags) {
914 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
915 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
916 if (!(ep_state & EP_HALTED))
917 goto bandwidth_change;
918 xhci_dbg(xhci, "Completed config ep cmd - "
919 "last ep index = %d, state = %d\n",
920 ep_index, ep_state);
781 /* Clear our internal halted state and restart ring */ 921 /* Clear our internal halted state and restart ring */
782 xhci->devs[slot_id]->eps[ep_index].ep_state &= 922 xhci->devs[slot_id]->eps[ep_index].ep_state &=
783 ~EP_HALTED; 923 ~EP_HALTED;
784 ring_ep_doorbell(xhci, slot_id, ep_index); 924 ring_ep_doorbell(xhci, slot_id, ep_index);
785 } else { 925 break;
786 xhci->devs[slot_id]->cmd_status =
787 GET_COMP_CODE(event->status);
788 complete(&xhci->devs[slot_id]->cmd_completion);
789 } 926 }
927bandwidth_change:
928 xhci_dbg(xhci, "Completed config ep cmd\n");
929 xhci->devs[slot_id]->cmd_status =
930 GET_COMP_CODE(event->status);
931 complete(&xhci->devs[slot_id]->cmd_completion);
790 break; 932 break;
791 case TRB_TYPE(TRB_EVAL_CONTEXT): 933 case TRB_TYPE(TRB_EVAL_CONTEXT):
792 virt_dev = xhci->devs[slot_id]; 934 virt_dev = xhci->devs[slot_id];
@@ -849,8 +991,7 @@ static void handle_port_status(struct xhci_hcd *xhci,
849 * TRB in this TD, this function returns that TRB's segment. Otherwise it 991 * TRB in this TD, this function returns that TRB's segment. Otherwise it
850 * returns 0. 992 * returns 0.
851 */ 993 */
852static struct xhci_segment *trb_in_td( 994struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
853 struct xhci_segment *start_seg,
854 union xhci_trb *start_trb, 995 union xhci_trb *start_trb,
855 union xhci_trb *end_trb, 996 union xhci_trb *end_trb,
856 dma_addr_t suspect_dma) 997 dma_addr_t suspect_dma)
@@ -900,6 +1041,45 @@ static struct xhci_segment *trb_in_td(
900 return 0; 1041 return 0;
901} 1042}
902 1043
1044static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
1045 unsigned int slot_id, unsigned int ep_index,
1046 struct xhci_td *td, union xhci_trb *event_trb)
1047{
1048 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
1049 ep->ep_state |= EP_HALTED;
1050 ep->stopped_td = td;
1051 ep->stopped_trb = event_trb;
1052 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1053 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
1054 xhci_ring_cmd_db(xhci);
1055}
1056
1057/* Check if an error has halted the endpoint ring. The class driver will
1058 * cleanup the halt for a non-default control endpoint if we indicate a stall.
1059 * However, a babble and other errors also halt the endpoint ring, and the class
1060 * driver won't clear the halt in that case, so we need to issue a Set Transfer
1061 * Ring Dequeue Pointer command manually.
1062 */
1063static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
1064 struct xhci_ep_ctx *ep_ctx,
1065 unsigned int trb_comp_code)
1066{
1067 /* TRB completion codes that may require a manual halt cleanup */
1068 if (trb_comp_code == COMP_TX_ERR ||
1069 trb_comp_code == COMP_BABBLE ||
1070 trb_comp_code == COMP_SPLIT_ERR)
1071 /* The 0.96 spec says a babbling control endpoint
1072 * is not halted. The 0.96 spec says it is. Some HW
1073 * claims to be 0.95 compliant, but it halts the control
1074 * endpoint anyway. Check if a babble halted the
1075 * endpoint.
1076 */
1077 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
1078 return 1;
1079
1080 return 0;
1081}
1082
903/* 1083/*
904 * If this function returns an error condition, it means it got a Transfer 1084 * If this function returns an error condition, it means it got a Transfer
905 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. 1085 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
@@ -1002,6 +1182,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1002 xhci_warn(xhci, "WARN: TRB error on endpoint\n"); 1182 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
1003 status = -EILSEQ; 1183 status = -EILSEQ;
1004 break; 1184 break;
1185 case COMP_SPLIT_ERR:
1005 case COMP_TX_ERR: 1186 case COMP_TX_ERR:
1006 xhci_warn(xhci, "WARN: transfer error on endpoint\n"); 1187 xhci_warn(xhci, "WARN: transfer error on endpoint\n");
1007 status = -EPROTO; 1188 status = -EPROTO;
@@ -1015,6 +1196,16 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1015 status = -ENOSR; 1196 status = -ENOSR;
1016 break; 1197 break;
1017 default: 1198 default:
1199 if (trb_comp_code >= 224 && trb_comp_code <= 255) {
1200 /* Vendor defined "informational" completion code,
1201 * treat as not-an-error.
1202 */
1203 xhci_dbg(xhci, "Vendor defined info completion code %u\n",
1204 trb_comp_code);
1205 xhci_dbg(xhci, "Treating code as success.\n");
1206 status = 0;
1207 break;
1208 }
1018 xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n"); 1209 xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
1019 urb = NULL; 1210 urb = NULL;
1020 goto cleanup; 1211 goto cleanup;
@@ -1043,15 +1234,14 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1043 else 1234 else
1044 status = 0; 1235 status = 0;
1045 break; 1236 break;
1046 case COMP_BABBLE: 1237
1047 /* The 0.96 spec says a babbling control endpoint 1238 default:
1048 * is not halted. The 0.96 spec says it is. Some HW 1239 if (!xhci_requires_manual_halt_cleanup(xhci,
1049 * claims to be 0.95 compliant, but it halts the control 1240 ep_ctx, trb_comp_code))
1050 * endpoint anyway. Check if a babble halted the
1051 * endpoint.
1052 */
1053 if (ep_ctx->ep_info != EP_STATE_HALTED)
1054 break; 1241 break;
1242 xhci_dbg(xhci, "TRB error code %u, "
1243 "halted endpoint index = %u\n",
1244 trb_comp_code, ep_index);
1055 /* else fall through */ 1245 /* else fall through */
1056 case COMP_STALL: 1246 case COMP_STALL:
1057 /* Did we transfer part of the data (middle) phase? */ 1247 /* Did we transfer part of the data (middle) phase? */
@@ -1063,15 +1253,9 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1063 else 1253 else
1064 td->urb->actual_length = 0; 1254 td->urb->actual_length = 0;
1065 1255
1066 ep->stopped_td = td; 1256 xhci_cleanup_halted_endpoint(xhci,
1067 ep->stopped_trb = event_trb; 1257 slot_id, ep_index, td, event_trb);
1068 xhci_queue_reset_ep(xhci, slot_id, ep_index);
1069 xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
1070 xhci_ring_cmd_db(xhci);
1071 goto td_cleanup; 1258 goto td_cleanup;
1072 default:
1073 /* Others already handled above */
1074 break;
1075 } 1259 }
1076 /* 1260 /*
1077 * Did we transfer any data, despite the errors that might have 1261 * Did we transfer any data, despite the errors that might have
@@ -1209,16 +1393,25 @@ static int handle_tx_event(struct xhci_hcd *xhci,
1209 ep->stopped_td = td; 1393 ep->stopped_td = td;
1210 ep->stopped_trb = event_trb; 1394 ep->stopped_trb = event_trb;
1211 } else { 1395 } else {
1212 if (trb_comp_code == COMP_STALL || 1396 if (trb_comp_code == COMP_STALL) {
1213 trb_comp_code == COMP_BABBLE) {
1214 /* The transfer is completed from the driver's 1397 /* The transfer is completed from the driver's
1215 * perspective, but we need to issue a set dequeue 1398 * perspective, but we need to issue a set dequeue
1216 * command for this stalled endpoint to move the dequeue 1399 * command for this stalled endpoint to move the dequeue
1217 * pointer past the TD. We can't do that here because 1400 * pointer past the TD. We can't do that here because
1218 * the halt condition must be cleared first. 1401 * the halt condition must be cleared first. Let the
1402 * USB class driver clear the stall later.
1219 */ 1403 */
1220 ep->stopped_td = td; 1404 ep->stopped_td = td;
1221 ep->stopped_trb = event_trb; 1405 ep->stopped_trb = event_trb;
1406 } else if (xhci_requires_manual_halt_cleanup(xhci,
1407 ep_ctx, trb_comp_code)) {
1408 /* Other types of errors halt the endpoint, but the
1409 * class driver doesn't call usb_reset_endpoint() unless
1410 * the error is -EPIPE. Clear the halted status in the
1411 * xHCI hardware manually.
1412 */
1413 xhci_cleanup_halted_endpoint(xhci,
1414 slot_id, ep_index, td, event_trb);
1222 } else { 1415 } else {
1223 /* Update ring dequeue pointer */ 1416 /* Update ring dequeue pointer */
1224 while (ep_ring->dequeue != td->last_trb) 1417 while (ep_ring->dequeue != td->last_trb)
@@ -1249,10 +1442,9 @@ td_cleanup:
1249 } 1442 }
1250 list_del(&td->td_list); 1443 list_del(&td->td_list);
1251 /* Was this TD slated to be cancelled but completed anyway? */ 1444 /* Was this TD slated to be cancelled but completed anyway? */
1252 if (!list_empty(&td->cancelled_td_list)) { 1445 if (!list_empty(&td->cancelled_td_list))
1253 list_del(&td->cancelled_td_list); 1446 list_del(&td->cancelled_td_list);
1254 ep->cancels_pending--; 1447
1255 }
1256 /* Leave the TD around for the reset endpoint function to use 1448 /* Leave the TD around for the reset endpoint function to use
1257 * (but only if it's not a control endpoint, since we already 1449 * (but only if it's not a control endpoint, since we already
1258 * queued the Set TR dequeue pointer command for stalled 1450 * queued the Set TR dequeue pointer command for stalled
@@ -1331,6 +1523,14 @@ void xhci_handle_event(struct xhci_hcd *xhci)
1331 default: 1523 default:
1332 xhci->error_bitmask |= 1 << 3; 1524 xhci->error_bitmask |= 1 << 3;
1333 } 1525 }
1526 /* Any of the above functions may drop and re-acquire the lock, so check
1527 * to make sure a watchdog timer didn't mark the host as non-responsive.
1528 */
1529 if (xhci->xhc_state & XHCI_STATE_DYING) {
1530 xhci_dbg(xhci, "xHCI host dying, returning from "
1531 "event handler.\n");
1532 return;
1533 }
1334 1534
1335 if (update_ptrs) { 1535 if (update_ptrs) {
1336 /* Update SW and HC event ring dequeue pointer */ 1536 /* Update SW and HC event ring dequeue pointer */
@@ -1555,6 +1755,21 @@ int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1555 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index); 1755 return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
1556} 1756}
1557 1757
1758/*
1759 * The TD size is the number of bytes remaining in the TD (including this TRB),
1760 * right shifted by 10.
1761 * It must fit in bits 21:17, so it can't be bigger than 31.
1762 */
1763static u32 xhci_td_remainder(unsigned int remainder)
1764{
1765 u32 max = (1 << (21 - 17 + 1)) - 1;
1766
1767 if ((remainder >> 10) >= max)
1768 return max << 17;
1769 else
1770 return (remainder >> 10) << 17;
1771}
1772
1558static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 1773static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1559 struct urb *urb, int slot_id, unsigned int ep_index) 1774 struct urb *urb, int slot_id, unsigned int ep_index)
1560{ 1775{
@@ -1612,6 +1827,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1612 do { 1827 do {
1613 u32 field = 0; 1828 u32 field = 0;
1614 u32 length_field = 0; 1829 u32 length_field = 0;
1830 u32 remainder = 0;
1615 1831
1616 /* Don't change the cycle bit of the first TRB until later */ 1832 /* Don't change the cycle bit of the first TRB until later */
1617 if (first_trb) 1833 if (first_trb)
@@ -1641,8 +1857,10 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1641 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1), 1857 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
1642 (unsigned int) addr + trb_buff_len); 1858 (unsigned int) addr + trb_buff_len);
1643 } 1859 }
1860 remainder = xhci_td_remainder(urb->transfer_buffer_length -
1861 running_total) ;
1644 length_field = TRB_LEN(trb_buff_len) | 1862 length_field = TRB_LEN(trb_buff_len) |
1645 TD_REMAINDER(urb->transfer_buffer_length - running_total) | 1863 remainder |
1646 TRB_INTR_TARGET(0); 1864 TRB_INTR_TARGET(0);
1647 queue_trb(xhci, ep_ring, false, 1865 queue_trb(xhci, ep_ring, false,
1648 lower_32_bits(addr), 1866 lower_32_bits(addr),
@@ -1755,6 +1973,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1755 1973
1756 /* Queue the first TRB, even if it's zero-length */ 1974 /* Queue the first TRB, even if it's zero-length */
1757 do { 1975 do {
1976 u32 remainder = 0;
1758 field = 0; 1977 field = 0;
1759 1978
1760 /* Don't change the cycle bit of the first TRB until later */ 1979 /* Don't change the cycle bit of the first TRB until later */
@@ -1773,8 +1992,10 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1773 td->last_trb = ep_ring->enqueue; 1992 td->last_trb = ep_ring->enqueue;
1774 field |= TRB_IOC; 1993 field |= TRB_IOC;
1775 } 1994 }
1995 remainder = xhci_td_remainder(urb->transfer_buffer_length -
1996 running_total);
1776 length_field = TRB_LEN(trb_buff_len) | 1997 length_field = TRB_LEN(trb_buff_len) |
1777 TD_REMAINDER(urb->transfer_buffer_length - running_total) | 1998 remainder |
1778 TRB_INTR_TARGET(0); 1999 TRB_INTR_TARGET(0);
1779 queue_trb(xhci, ep_ring, false, 2000 queue_trb(xhci, ep_ring, false,
1780 lower_32_bits(addr), 2001 lower_32_bits(addr),
@@ -1862,7 +2083,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1862 /* If there's data, queue data TRBs */ 2083 /* If there's data, queue data TRBs */
1863 field = 0; 2084 field = 0;
1864 length_field = TRB_LEN(urb->transfer_buffer_length) | 2085 length_field = TRB_LEN(urb->transfer_buffer_length) |
1865 TD_REMAINDER(urb->transfer_buffer_length) | 2086 xhci_td_remainder(urb->transfer_buffer_length) |
1866 TRB_INTR_TARGET(0); 2087 TRB_INTR_TARGET(0);
1867 if (urb->transfer_buffer_length > 0) { 2088 if (urb->transfer_buffer_length > 0) {
1868 if (setup->bRequestType & USB_DIR_IN) 2089 if (setup->bRequestType & USB_DIR_IN)
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 4b254b6fa245..877813505ef2 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -652,13 +652,17 @@ struct xhci_virt_ep {
652 struct xhci_ring *new_ring; 652 struct xhci_ring *new_ring;
653 unsigned int ep_state; 653 unsigned int ep_state;
654#define SET_DEQ_PENDING (1 << 0) 654#define SET_DEQ_PENDING (1 << 0)
655#define EP_HALTED (1 << 1) 655#define EP_HALTED (1 << 1) /* For stall handling */
656#define EP_HALT_PENDING (1 << 2) /* For URB cancellation */
656 /* ---- Related to URB cancellation ---- */ 657 /* ---- Related to URB cancellation ---- */
657 struct list_head cancelled_td_list; 658 struct list_head cancelled_td_list;
658 unsigned int cancels_pending;
659 /* The TRB that was last reported in a stopped endpoint ring */ 659 /* The TRB that was last reported in a stopped endpoint ring */
660 union xhci_trb *stopped_trb; 660 union xhci_trb *stopped_trb;
661 struct xhci_td *stopped_td; 661 struct xhci_td *stopped_td;
662 /* Watchdog timer for stop endpoint command to cancel URBs */
663 struct timer_list stop_cmd_timer;
664 int stop_cmds_pending;
665 struct xhci_hcd *xhci;
662}; 666};
663 667
664struct xhci_virt_device { 668struct xhci_virt_device {
@@ -673,6 +677,10 @@ struct xhci_virt_device {
673 struct xhci_container_ctx *out_ctx; 677 struct xhci_container_ctx *out_ctx;
674 /* Used for addressing devices and configuration changes */ 678 /* Used for addressing devices and configuration changes */
675 struct xhci_container_ctx *in_ctx; 679 struct xhci_container_ctx *in_ctx;
680 /* Rings saved to ensure old alt settings can be re-instated */
681 struct xhci_ring **ring_cache;
682 int num_rings_cached;
683#define XHCI_MAX_RINGS_CACHED 31
676 struct xhci_virt_ep eps[31]; 684 struct xhci_virt_ep eps[31];
677 struct completion cmd_completion; 685 struct completion cmd_completion;
678 /* Status of the last command issued for this device */ 686 /* Status of the last command issued for this device */
@@ -824,9 +832,6 @@ struct xhci_event_cmd {
824/* Normal TRB fields */ 832/* Normal TRB fields */
825/* transfer_len bitmasks - bits 0:16 */ 833/* transfer_len bitmasks - bits 0:16 */
826#define TRB_LEN(p) ((p) & 0x1ffff) 834#define TRB_LEN(p) ((p) & 0x1ffff)
827/* TD size - number of bytes remaining in the TD (including this TRB):
828 * bits 17 - 21. Shift the number of bytes by 10. */
829#define TD_REMAINDER(p) ((((p) >> 10) & 0x1f) << 17)
830/* Interrupter Target - which MSI-X vector to target the completion event at */ 835/* Interrupter Target - which MSI-X vector to target the completion event at */
831#define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22) 836#define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
832#define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff) 837#define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
@@ -1022,6 +1027,8 @@ struct xhci_scratchpad {
1022#define ERST_ENTRIES 1 1027#define ERST_ENTRIES 1
1023/* Poll every 60 seconds */ 1028/* Poll every 60 seconds */
1024#define POLL_TIMEOUT 60 1029#define POLL_TIMEOUT 60
1030/* Stop endpoint command timeout (secs) for URB cancellation watchdog timer */
1031#define XHCI_STOP_EP_CMD_TIMEOUT 5
1025/* XXX: Make these module parameters */ 1032/* XXX: Make these module parameters */
1026 1033
1027 1034
@@ -1083,6 +1090,21 @@ struct xhci_hcd {
1083 struct timer_list event_ring_timer; 1090 struct timer_list event_ring_timer;
1084 int zombie; 1091 int zombie;
1085#endif 1092#endif
1093 /* Host controller watchdog timer structures */
1094 unsigned int xhc_state;
1095/* Host controller is dying - not responding to commands. "I'm not dead yet!"
1096 *
1097 * xHC interrupts have been disabled and a watchdog timer will (or has already)
1098 * halt the xHCI host, and complete all URBs with an -ESHUTDOWN code. Any code
1099 * that sees this status (other than the timer that set it) should stop touching
1100 * hardware immediately. Interrupt handlers should return immediately when
1101 * they see this status (any time they drop and re-acquire xhci->lock).
1102 * xhci_urb_dequeue() should call usb_hcd_check_unlink_urb() and return without
1103 * putting the TD on the canceled list, etc.
1104 *
1105 * There are no reports of xHCI host controllers that display this issue.
1106 */
1107#define XHCI_STATE_DYING (1 << 0)
1086 /* Statistics */ 1108 /* Statistics */
1087 int noops_submitted; 1109 int noops_submitted;
1088 int noops_handled; 1110 int noops_handled;
@@ -1223,6 +1245,7 @@ void xhci_unregister_pci(void);
1223#endif 1245#endif
1224 1246
1225/* xHCI host controller glue */ 1247/* xHCI host controller glue */
1248void xhci_quiesce(struct xhci_hcd *xhci);
1226int xhci_halt(struct xhci_hcd *xhci); 1249int xhci_halt(struct xhci_hcd *xhci);
1227int xhci_reset(struct xhci_hcd *xhci); 1250int xhci_reset(struct xhci_hcd *xhci);
1228int xhci_init(struct usb_hcd *hcd); 1251int xhci_init(struct usb_hcd *hcd);
@@ -1246,6 +1269,9 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
1246 1269
1247/* xHCI ring, segment, TRB, and TD functions */ 1270/* xHCI ring, segment, TRB, and TD functions */
1248dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb); 1271dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb);
1272struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
1273 union xhci_trb *start_trb, union xhci_trb *end_trb,
1274 dma_addr_t suspect_dma);
1249void xhci_ring_cmd_db(struct xhci_hcd *xhci); 1275void xhci_ring_cmd_db(struct xhci_hcd *xhci);
1250void *xhci_setup_one_noop(struct xhci_hcd *xhci); 1276void *xhci_setup_one_noop(struct xhci_hcd *xhci);
1251void xhci_handle_event(struct xhci_hcd *xhci); 1277void xhci_handle_event(struct xhci_hcd *xhci);
@@ -1278,6 +1304,7 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1278void xhci_queue_config_ep_quirk(struct xhci_hcd *xhci, 1304void xhci_queue_config_ep_quirk(struct xhci_hcd *xhci,
1279 unsigned int slot_id, unsigned int ep_index, 1305 unsigned int slot_id, unsigned int ep_index,
1280 struct xhci_dequeue_state *deq_state); 1306 struct xhci_dequeue_state *deq_state);
1307void xhci_stop_endpoint_command_watchdog(unsigned long arg);
1281 1308
1282/* xHCI roothub code */ 1309/* xHCI roothub code */
1283int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, 1310int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index a9f06d76960f..3dab0c0b196f 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -213,8 +213,9 @@ static struct urb *simple_alloc_urb (
213} 213}
214 214
215static unsigned pattern = 0; 215static unsigned pattern = 0;
216module_param (pattern, uint, S_IRUGO); 216static unsigned mod_pattern;
217MODULE_PARM_DESC(pattern, "i/o pattern (0 == zeroes)"); 217module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
218MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
218 219
219static inline void simple_fill_buf (struct urb *urb) 220static inline void simple_fill_buf (struct urb *urb)
220{ 221{
@@ -1567,6 +1568,8 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
1567 1568
1568 // FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. 1569 // FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is.
1569 1570
1571 pattern = mod_pattern;
1572
1570 if (code != USBTEST_REQUEST) 1573 if (code != USBTEST_REQUEST)
1571 return -EOPNOTSUPP; 1574 return -EOPNOTSUPP;
1572 1575
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index 10f3205798e8..385ec0520167 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -16,6 +16,7 @@
16#include <linux/compat.h> 16#include <linux/compat.h>
17#include <linux/mm.h> 17#include <linux/mm.h>
18#include <linux/smp_lock.h> 18#include <linux/smp_lock.h>
19#include <linux/scatterlist.h>
19 20
20#include <asm/uaccess.h> 21#include <asm/uaccess.h>
21 22
@@ -221,7 +222,7 @@ static void mon_free_buff(struct mon_pgmap *map, int npages);
221/* 222/*
222 * This is a "chunked memcpy". It does not manipulate any counters. 223 * This is a "chunked memcpy". It does not manipulate any counters.
223 */ 224 */
224static void mon_copy_to_buff(const struct mon_reader_bin *this, 225static unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
225 unsigned int off, const unsigned char *from, unsigned int length) 226 unsigned int off, const unsigned char *from, unsigned int length)
226{ 227{
227 unsigned int step_len; 228 unsigned int step_len;
@@ -246,6 +247,7 @@ static void mon_copy_to_buff(const struct mon_reader_bin *this,
246 from += step_len; 247 from += step_len;
247 length -= step_len; 248 length -= step_len;
248 } 249 }
250 return off;
249} 251}
250 252
251/* 253/*
@@ -394,14 +396,44 @@ static inline char mon_bin_get_setup(unsigned char *setupb,
394 return 0; 396 return 0;
395} 397}
396 398
397static char mon_bin_get_data(const struct mon_reader_bin *rp, 399static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
398 unsigned int offset, struct urb *urb, unsigned int length) 400 unsigned int offset, struct urb *urb, unsigned int length,
401 char *flag)
399{ 402{
403 int i;
404 struct scatterlist *sg;
405 unsigned int this_len;
406
407 *flag = 0;
408 if (urb->num_sgs == 0) {
409 if (urb->transfer_buffer == NULL) {
410 *flag = 'Z';
411 return length;
412 }
413 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
414 length = 0;
400 415
401 if (urb->transfer_buffer == NULL) 416 } else {
402 return 'Z'; 417 /* If IOMMU coalescing occurred, we cannot trust sg_page */
403 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length); 418 if (urb->sg->nents != urb->num_sgs) {
404 return 0; 419 *flag = 'D';
420 return length;
421 }
422
423 /* Copy up to the first non-addressable segment */
424 for_each_sg(urb->sg->sg, sg, urb->num_sgs, i) {
425 if (length == 0 || PageHighMem(sg_page(sg)))
426 break;
427 this_len = min_t(unsigned int, sg->length, length);
428 offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
429 this_len);
430 length -= this_len;
431 }
432 if (i == 0)
433 *flag = 'D';
434 }
435
436 return length;
405} 437}
406 438
407static void mon_bin_get_isodesc(const struct mon_reader_bin *rp, 439static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
@@ -536,8 +568,9 @@ static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
536 } 568 }
537 569
538 if (length != 0) { 570 if (length != 0) {
539 ep->flag_data = mon_bin_get_data(rp, offset, urb, length); 571 length = mon_bin_get_data(rp, offset, urb, length,
540 if (ep->flag_data != 0) { /* Yes, it's 0x00, not '0' */ 572 &ep->flag_data);
573 if (length > 0) {
541 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1); 574 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
542 ep->len_cap -= length; 575 ep->len_cap -= length;
543 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1); 576 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 9f1a9227ebe6..047568ff223d 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -10,6 +10,7 @@
10#include <linux/time.h> 10#include <linux/time.h>
11#include <linux/mutex.h> 11#include <linux/mutex.h>
12#include <linux/debugfs.h> 12#include <linux/debugfs.h>
13#include <linux/scatterlist.h>
13#include <asm/uaccess.h> 14#include <asm/uaccess.h>
14 15
15#include "usb_mon.h" 16#include "usb_mon.h"
@@ -137,6 +138,8 @@ static inline char mon_text_get_setup(struct mon_event_text *ep,
137static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb, 138static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
138 int len, char ev_type, struct mon_bus *mbus) 139 int len, char ev_type, struct mon_bus *mbus)
139{ 140{
141 void *src;
142
140 if (len <= 0) 143 if (len <= 0)
141 return 'L'; 144 return 'L';
142 if (len >= DATA_MAX) 145 if (len >= DATA_MAX)
@@ -150,10 +153,24 @@ static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
150 return '>'; 153 return '>';
151 } 154 }
152 155
153 if (urb->transfer_buffer == NULL) 156 if (urb->num_sgs == 0) {
154 return 'Z'; /* '0' would be not as pretty. */ 157 src = urb->transfer_buffer;
158 if (src == NULL)
159 return 'Z'; /* '0' would be not as pretty. */
160 } else {
161 struct scatterlist *sg = urb->sg->sg;
162
163 /* If IOMMU coalescing occurred, we cannot trust sg_page */
164 if (urb->sg->nents != urb->num_sgs ||
165 PageHighMem(sg_page(sg)))
166 return 'D';
167
168 /* For the text interface we copy only the first sg buffer */
169 len = min_t(int, sg->length, len);
170 src = sg_virt(sg);
171 }
155 172
156 memcpy(ep->data, urb->transfer_buffer, len); 173 memcpy(ep->data, src, len);
157 return 0; 174 return 0;
158} 175}
159 176
diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
index b84abd8ee8a5..d9db86498022 100644
--- a/drivers/usb/musb/Kconfig
+++ b/drivers/usb/musb/Kconfig
@@ -9,10 +9,9 @@ comment "Enable Host or Gadget support to see Inventra options"
9# (M)HDRC = (Multipoint) Highspeed Dual-Role Controller 9# (M)HDRC = (Multipoint) Highspeed Dual-Role Controller
10config USB_MUSB_HDRC 10config USB_MUSB_HDRC
11 depends on (USB || USB_GADGET) 11 depends on (USB || USB_GADGET)
12 depends on (ARM || BLACKFIN) 12 depends on (ARM || (BF54x && !BF544) || (BF52x && !BF522 && !BF523))
13 select NOP_USB_XCEIV if ARCH_DAVINCI 13 select NOP_USB_XCEIV if (ARCH_DAVINCI || MACH_OMAP3EVM || BLACKFIN)
14 select TWL4030_USB if MACH_OMAP_3430SDP 14 select TWL4030_USB if MACH_OMAP_3430SDP
15 select NOP_USB_XCEIV if MACH_OMAP3EVM
16 select USB_OTG_UTILS 15 select USB_OTG_UTILS
17 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)' 16 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
18 help 17 help
diff --git a/drivers/usb/musb/blackfin.c b/drivers/usb/musb/blackfin.c
index fcec87ea709e..fe4934d9602c 100644
--- a/drivers/usb/musb/blackfin.c
+++ b/drivers/usb/musb/blackfin.c
@@ -53,13 +53,11 @@ void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
53void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst) 53void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
54{ 54{
55 void __iomem *fifo = hw_ep->fifo; 55 void __iomem *fifo = hw_ep->fifo;
56
57#ifdef CONFIG_BF52x
56 u8 epnum = hw_ep->epnum; 58 u8 epnum = hw_ep->epnum;
57 u16 dma_reg = 0; 59 u16 dma_reg = 0;
58 60
59 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
60 'R', hw_ep->epnum, fifo, len, dst);
61
62#ifdef CONFIG_BF52x
63 invalidate_dcache_range((unsigned int)dst, 61 invalidate_dcache_range((unsigned int)dst,
64 (unsigned int)(dst + len)); 62 (unsigned int)(dst + len));
65 63
@@ -102,6 +100,9 @@ void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
102 len & 0x01 ? (len >> 1) + 1 : len >> 1); 100 len & 0x01 ? (len >> 1) + 1 : len >> 1);
103#endif 101#endif
104 102
103 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
104 'R', hw_ep->epnum, fifo, len, dst);
105
105 dump_fifo_data(dst, len); 106 dump_fifo_data(dst, len);
106} 107}
107 108
@@ -225,8 +226,9 @@ int musb_platform_get_vbus_status(struct musb *musb)
225 return 0; 226 return 0;
226} 227}
227 228
228void musb_platform_set_mode(struct musb *musb, u8 musb_mode) 229int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
229{ 230{
231 return -EIO;
230} 232}
231 233
232int __init musb_platform_init(struct musb *musb) 234int __init musb_platform_init(struct musb *musb)
@@ -261,10 +263,6 @@ int __init musb_platform_init(struct musb *musb)
261 SSYNC(); 263 SSYNC();
262 } 264 }
263 265
264 /* TODO
265 * Set SIC-IVG register
266 */
267
268 /* Configure PLL oscillator register */ 266 /* Configure PLL oscillator register */
269 bfin_write_USB_PLLOSC_CTRL(0x30a8); 267 bfin_write_USB_PLLOSC_CTRL(0x30a8);
270 SSYNC(); 268 SSYNC();
diff --git a/drivers/usb/musb/blackfin.h b/drivers/usb/musb/blackfin.h
index a240c1e53d16..10b7d7584f4b 100644
--- a/drivers/usb/musb/blackfin.h
+++ b/drivers/usb/musb/blackfin.h
@@ -14,6 +14,43 @@
14 * Blackfin specific definitions 14 * Blackfin specific definitions
15 */ 15 */
16 16
17/* Anomalies notes:
18 *
19 * 05000450 - USB DMA Mode 1 Short Packet Data Corruption:
20 * MUSB driver is designed to transfer buffer of N * maxpacket size
21 * in DMA mode 1 and leave the rest of the data to the next
22 * transfer in DMA mode 0, so we never transmit a short packet in
23 * DMA mode 1.
24 *
25 * 05000463 - This anomaly doesn't affect this driver since it
26 * never uses L1 or L2 memory as data destination.
27 *
28 * 05000464 - This anomaly doesn't affect this driver since it
29 * never uses L1 or L2 memory as data source.
30 *
31 * 05000465 - The anomaly can be seen when SCLK is over 100 MHz, and there is
32 * no way to workaround for bulk endpoints. Since the wMaxPackSize
33 * of bulk is less than or equal to 512, while the fifo size of
34 * endpoint 5, 6, 7 is 1024, the double buffer mode is enabled
35 * automatically when these endpoints are used for bulk OUT.
36 *
37 * 05000466 - This anomaly doesn't affect this driver since it never mixes
38 * concurrent DMA and core accesses to the TX endpoint FIFOs.
39 *
40 * 05000467 - The workaround for this anomaly will introduce another
41 * anomaly - 05000465.
42 */
43
44/* The Mentor USB DMA engine on BF52x (silicon v0.0 and v0.1) seems to be
45 * unstable in host mode. This may be caused by Anomaly 05000380. After
46 * digging out the root cause, we will change this number accordingly.
47 * So, need to either use silicon v0.2+ or disable DMA mode in MUSB.
48 */
49#if ANOMALY_05000380 && defined(CONFIG_BF52x) && \
50 defined(CONFIG_USB_MUSB_HDRC) && !defined(CONFIG_MUSB_PIO_ONLY)
51# error "Please use PIO mode in MUSB driver on bf52x chip v0.0 and v0.1"
52#endif
53
17#undef DUMP_FIFO_DATA 54#undef DUMP_FIFO_DATA
18#ifdef DUMP_FIFO_DATA 55#ifdef DUMP_FIFO_DATA
19static void dump_fifo_data(u8 *buf, u16 len) 56static void dump_fifo_data(u8 *buf, u16 len)
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 547e0e390726..49f2346afad3 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -1319,7 +1319,6 @@ static int __init musb_core_init(u16 musb_type, struct musb *musb)
1319#endif 1319#endif
1320 u8 reg; 1320 u8 reg;
1321 char *type; 1321 char *type;
1322 u16 hwvers, rev_major, rev_minor;
1323 char aInfo[78], aRevision[32], aDate[12]; 1322 char aInfo[78], aRevision[32], aDate[12];
1324 void __iomem *mbase = musb->mregs; 1323 void __iomem *mbase = musb->mregs;
1325 int status = 0; 1324 int status = 0;
@@ -1391,11 +1390,10 @@ static int __init musb_core_init(u16 musb_type, struct musb *musb)
1391 } 1390 }
1392 1391
1393 /* log release info */ 1392 /* log release info */
1394 hwvers = musb_read_hwvers(mbase); 1393 musb->hwvers = musb_read_hwvers(mbase);
1395 rev_major = (hwvers >> 10) & 0x1f; 1394 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1396 rev_minor = hwvers & 0x3ff; 1395 MUSB_HWVERS_MINOR(musb->hwvers),
1397 snprintf(aRevision, 32, "%d.%d%s", rev_major, 1396 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
1398 rev_minor, (hwvers & 0x8000) ? "RC" : "");
1399 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n", 1397 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1400 musb_driver_name, type, aRevision, aDate); 1398 musb_driver_name, type, aRevision, aDate);
1401 1399
diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h
index 6aa5f22e5274..03d50909b078 100644
--- a/drivers/usb/musb/musb_core.h
+++ b/drivers/usb/musb/musb_core.h
@@ -322,6 +322,14 @@ struct musb {
322 struct clk *clock; 322 struct clk *clock;
323 irqreturn_t (*isr)(int, void *); 323 irqreturn_t (*isr)(int, void *);
324 struct work_struct irq_work; 324 struct work_struct irq_work;
325#define MUSB_HWVERS_MAJOR(x) ((x >> 10) & 0x1f)
326#define MUSB_HWVERS_MINOR(x) (x & 0x3ff)
327#define MUSB_HWVERS_RC 0x8000
328#define MUSB_HWVERS_1300 0x52C
329#define MUSB_HWVERS_1400 0x590
330#define MUSB_HWVERS_1800 0x720
331#define MUSB_HWVERS_2000 0x800
332 u16 hwvers;
325 333
326/* this hub status bit is reserved by USB 2.0 and not seen by usbcore */ 334/* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
327#define MUSB_PORT_STAT_RESUME (1 << 31) 335#define MUSB_PORT_STAT_RESUME (1 << 31)
diff --git a/drivers/usb/musb/musb_dma.h b/drivers/usb/musb/musb_dma.h
index 0a2c4e3602c1..916065ba9e70 100644
--- a/drivers/usb/musb/musb_dma.h
+++ b/drivers/usb/musb/musb_dma.h
@@ -80,6 +80,17 @@ struct musb_hw_ep;
80#define tusb_dma_omap() 0 80#define tusb_dma_omap() 0
81#endif 81#endif
82 82
83/* Anomaly 05000456 - USB Receive Interrupt Is Not Generated in DMA Mode 1
84 * Only allow DMA mode 1 to be used when the USB will actually generate the
85 * interrupts we expect.
86 */
87#ifdef CONFIG_BLACKFIN
88# undef USE_MODE1
89# if !ANOMALY_05000456
90# define USE_MODE1
91# endif
92#endif
93
83/* 94/*
84 * DMA channel status ... updated by the dma controller driver whenever that 95 * DMA channel status ... updated by the dma controller driver whenever that
85 * status changes, and protected by the overall controller spinlock. 96 * status changes, and protected by the overall controller spinlock.
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index 74073f9a43f0..c49b9ba025ab 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -429,112 +429,102 @@ void musb_g_tx(struct musb *musb, u8 epnum)
429 DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr); 429 DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
430 430
431 dma = is_dma_capable() ? musb_ep->dma : NULL; 431 dma = is_dma_capable() ? musb_ep->dma : NULL;
432 do { 432
433 /* REVISIT for high bandwidth, MUSB_TXCSR_P_INCOMPTX 433 /*
434 * probably rates reporting as a host error 434 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
435 * probably rates reporting as a host error.
436 */
437 if (csr & MUSB_TXCSR_P_SENTSTALL) {
438 csr |= MUSB_TXCSR_P_WZC_BITS;
439 csr &= ~MUSB_TXCSR_P_SENTSTALL;
440 musb_writew(epio, MUSB_TXCSR, csr);
441 return;
442 }
443
444 if (csr & MUSB_TXCSR_P_UNDERRUN) {
445 /* We NAKed, no big deal... little reason to care. */
446 csr |= MUSB_TXCSR_P_WZC_BITS;
447 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
448 musb_writew(epio, MUSB_TXCSR, csr);
449 DBG(20, "underrun on ep%d, req %p\n", epnum, request);
450 }
451
452 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
453 /*
454 * SHOULD NOT HAPPEN... has with CPPI though, after
455 * changing SENDSTALL (and other cases); harmless?
435 */ 456 */
436 if (csr & MUSB_TXCSR_P_SENTSTALL) { 457 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
437 csr |= MUSB_TXCSR_P_WZC_BITS; 458 return;
438 csr &= ~MUSB_TXCSR_P_SENTSTALL; 459 }
439 musb_writew(epio, MUSB_TXCSR, csr); 460
440 break; 461 if (request) {
441 } 462 u8 is_dma = 0;
442 463
443 if (csr & MUSB_TXCSR_P_UNDERRUN) { 464 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
444 /* we NAKed, no big deal ... little reason to care */ 465 is_dma = 1;
445 csr |= MUSB_TXCSR_P_WZC_BITS; 466 csr |= MUSB_TXCSR_P_WZC_BITS;
446 csr &= ~(MUSB_TXCSR_P_UNDERRUN 467 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
447 | MUSB_TXCSR_TXPKTRDY); 468 MUSB_TXCSR_TXPKTRDY);
448 musb_writew(epio, MUSB_TXCSR, csr); 469 musb_writew(epio, MUSB_TXCSR, csr);
449 DBG(20, "underrun on ep%d, req %p\n", epnum, request); 470 /* Ensure writebuffer is empty. */
471 csr = musb_readw(epio, MUSB_TXCSR);
472 request->actual += musb_ep->dma->actual_len;
473 DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
474 epnum, csr, musb_ep->dma->actual_len, request);
450 } 475 }
451 476
452 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 477 if (is_dma || request->actual == request->length) {
453 /* SHOULD NOT HAPPEN ... has with cppi though, after 478 /*
454 * changing SENDSTALL (and other cases); harmless? 479 * First, maybe a terminating short packet. Some DMA
480 * engines might handle this by themselves.
455 */ 481 */
456 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name); 482 if ((request->zero && request->length
457 break; 483 && request->length % musb_ep->packet_sz == 0)
458 }
459
460 if (request) {
461 u8 is_dma = 0;
462
463 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
464 is_dma = 1;
465 csr |= MUSB_TXCSR_P_WZC_BITS;
466 csr &= ~(MUSB_TXCSR_DMAENAB
467 | MUSB_TXCSR_P_UNDERRUN
468 | MUSB_TXCSR_TXPKTRDY);
469 musb_writew(epio, MUSB_TXCSR, csr);
470 /* ensure writebuffer is empty */
471 csr = musb_readw(epio, MUSB_TXCSR);
472 request->actual += musb_ep->dma->actual_len;
473 DBG(4, "TXCSR%d %04x, dma off, "
474 "len %zu, req %p\n",
475 epnum, csr,
476 musb_ep->dma->actual_len,
477 request);
478 }
479
480 if (is_dma || request->actual == request->length) {
481
482 /* First, maybe a terminating short packet.
483 * Some DMA engines might handle this by
484 * themselves.
485 */
486 if ((request->zero
487 && request->length
488 && (request->length
489 % musb_ep->packet_sz)
490 == 0)
491#ifdef CONFIG_USB_INVENTRA_DMA 484#ifdef CONFIG_USB_INVENTRA_DMA
492 || (is_dma && 485 || (is_dma && (!dma->desired_mode ||
493 ((!dma->desired_mode) || 486 (request->actual &
494 (request->actual & 487 (musb_ep->packet_sz - 1))))
495 (musb_ep->packet_sz - 1))))
496#endif 488#endif
497 ) { 489 ) {
498 /* on dma completion, fifo may not 490 /*
499 * be available yet ... 491 * On DMA completion, FIFO may not be
500 */ 492 * available yet...
501 if (csr & MUSB_TXCSR_TXPKTRDY)
502 break;
503
504 DBG(4, "sending zero pkt\n");
505 musb_writew(epio, MUSB_TXCSR,
506 MUSB_TXCSR_MODE
507 | MUSB_TXCSR_TXPKTRDY);
508 request->zero = 0;
509 }
510
511 /* ... or if not, then complete it */
512 musb_g_giveback(musb_ep, request, 0);
513
514 /* kickstart next transfer if appropriate;
515 * the packet that just completed might not
516 * be transmitted for hours or days.
517 * REVISIT for double buffering...
518 * FIXME revisit for stalls too...
519 */ 493 */
520 musb_ep_select(mbase, epnum); 494 if (csr & MUSB_TXCSR_TXPKTRDY)
521 csr = musb_readw(epio, MUSB_TXCSR); 495 return;
522 if (csr & MUSB_TXCSR_FIFONOTEMPTY) 496
523 break; 497 DBG(4, "sending zero pkt\n");
524 request = musb_ep->desc 498 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
525 ? next_request(musb_ep) 499 | MUSB_TXCSR_TXPKTRDY);
526 : NULL; 500 request->zero = 0;
527 if (!request) {
528 DBG(4, "%s idle now\n",
529 musb_ep->end_point.name);
530 break;
531 }
532 } 501 }
533 502
534 txstate(musb, to_musb_request(request)); 503 /* ... or if not, then complete it. */
504 musb_g_giveback(musb_ep, request, 0);
505
506 /*
507 * Kickstart next transfer if appropriate;
508 * the packet that just completed might not
509 * be transmitted for hours or days.
510 * REVISIT for double buffering...
511 * FIXME revisit for stalls too...
512 */
513 musb_ep_select(mbase, epnum);
514 csr = musb_readw(epio, MUSB_TXCSR);
515 if (csr & MUSB_TXCSR_FIFONOTEMPTY)
516 return;
517
518 if (!musb_ep->desc) {
519 DBG(4, "%s idle now\n",
520 musb_ep->end_point.name);
521 return;
522 } else
523 request = next_request(musb_ep);
535 } 524 }
536 525
537 } while (0); 526 txstate(musb, to_musb_request(request));
527 }
538} 528}
539 529
540/* ------------------------------------------------------------ */ 530/* ------------------------------------------------------------ */
@@ -966,6 +956,7 @@ static int musb_gadget_enable(struct usb_ep *ep,
966 956
967 musb_ep->desc = desc; 957 musb_ep->desc = desc;
968 musb_ep->busy = 0; 958 musb_ep->busy = 0;
959 musb_ep->wedged = 0;
969 status = 0; 960 status = 0;
970 961
971 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", 962 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
@@ -1220,7 +1211,7 @@ done:
1220 * 1211 *
1221 * exported to ep0 code 1212 * exported to ep0 code
1222 */ 1213 */
1223int musb_gadget_set_halt(struct usb_ep *ep, int value) 1214static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1224{ 1215{
1225 struct musb_ep *musb_ep = to_musb_ep(ep); 1216 struct musb_ep *musb_ep = to_musb_ep(ep);
1226 u8 epnum = musb_ep->current_epnum; 1217 u8 epnum = musb_ep->current_epnum;
@@ -1262,7 +1253,8 @@ int musb_gadget_set_halt(struct usb_ep *ep, int value)
1262 goto done; 1253 goto done;
1263 } 1254 }
1264 } 1255 }
1265 } 1256 } else
1257 musb_ep->wedged = 0;
1266 1258
1267 /* set/clear the stall and toggle bits */ 1259 /* set/clear the stall and toggle bits */
1268 DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear"); 1260 DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
@@ -1301,6 +1293,21 @@ done:
1301 return status; 1293 return status;
1302} 1294}
1303 1295
1296/*
1297 * Sets the halt feature with the clear requests ignored
1298 */
1299static int musb_gadget_set_wedge(struct usb_ep *ep)
1300{
1301 struct musb_ep *musb_ep = to_musb_ep(ep);
1302
1303 if (!ep)
1304 return -EINVAL;
1305
1306 musb_ep->wedged = 1;
1307
1308 return usb_ep_set_halt(ep);
1309}
1310
1304static int musb_gadget_fifo_status(struct usb_ep *ep) 1311static int musb_gadget_fifo_status(struct usb_ep *ep)
1305{ 1312{
1306 struct musb_ep *musb_ep = to_musb_ep(ep); 1313 struct musb_ep *musb_ep = to_musb_ep(ep);
@@ -1371,6 +1378,7 @@ static const struct usb_ep_ops musb_ep_ops = {
1371 .queue = musb_gadget_queue, 1378 .queue = musb_gadget_queue,
1372 .dequeue = musb_gadget_dequeue, 1379 .dequeue = musb_gadget_dequeue,
1373 .set_halt = musb_gadget_set_halt, 1380 .set_halt = musb_gadget_set_halt,
1381 .set_wedge = musb_gadget_set_wedge,
1374 .fifo_status = musb_gadget_fifo_status, 1382 .fifo_status = musb_gadget_fifo_status,
1375 .fifo_flush = musb_gadget_fifo_flush 1383 .fifo_flush = musb_gadget_fifo_flush
1376}; 1384};
diff --git a/drivers/usb/musb/musb_gadget.h b/drivers/usb/musb/musb_gadget.h
index 59502da9f739..c8b140325d82 100644
--- a/drivers/usb/musb/musb_gadget.h
+++ b/drivers/usb/musb/musb_gadget.h
@@ -75,6 +75,8 @@ struct musb_ep {
75 /* later things are modified based on usage */ 75 /* later things are modified based on usage */
76 struct list_head req_list; 76 struct list_head req_list;
77 77
78 u8 wedged;
79
78 /* true if lock must be dropped but req_list may not be advanced */ 80 /* true if lock must be dropped but req_list may not be advanced */
79 u8 busy; 81 u8 busy;
80}; 82};
@@ -103,6 +105,4 @@ extern void musb_gadget_cleanup(struct musb *);
103 105
104extern void musb_g_giveback(struct musb_ep *, struct usb_request *, int); 106extern void musb_g_giveback(struct musb_ep *, struct usb_request *, int);
105 107
106extern int musb_gadget_set_halt(struct usb_ep *ep, int value);
107
108#endif /* __MUSB_GADGET_H */ 108#endif /* __MUSB_GADGET_H */
diff --git a/drivers/usb/musb/musb_gadget_ep0.c b/drivers/usb/musb/musb_gadget_ep0.c
index 522efb31b56b..8fba3f11e473 100644
--- a/drivers/usb/musb/musb_gadget_ep0.c
+++ b/drivers/usb/musb/musb_gadget_ep0.c
@@ -199,7 +199,6 @@ service_in_request(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
199static void musb_g_ep0_giveback(struct musb *musb, struct usb_request *req) 199static void musb_g_ep0_giveback(struct musb *musb, struct usb_request *req)
200{ 200{
201 musb_g_giveback(&musb->endpoints[0].ep_in, req, 0); 201 musb_g_giveback(&musb->endpoints[0].ep_in, req, 0);
202 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
203} 202}
204 203
205/* 204/*
@@ -258,30 +257,53 @@ __acquires(musb->lock)
258 case USB_RECIP_INTERFACE: 257 case USB_RECIP_INTERFACE:
259 break; 258 break;
260 case USB_RECIP_ENDPOINT:{ 259 case USB_RECIP_ENDPOINT:{
261 const u8 num = ctrlrequest->wIndex & 0x0f; 260 const u8 epnum =
262 struct musb_ep *musb_ep; 261 ctrlrequest->wIndex & 0x0f;
262 struct musb_ep *musb_ep;
263 struct musb_hw_ep *ep;
264 void __iomem *regs;
265 int is_in;
266 u16 csr;
263 267
264 if (num == 0 268 if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
265 || num >= MUSB_C_NUM_EPS 269 ctrlrequest->wValue != USB_ENDPOINT_HALT)
266 || ctrlrequest->wValue
267 != USB_ENDPOINT_HALT)
268 break; 270 break;
269 271
270 if (ctrlrequest->wIndex & USB_DIR_IN) 272 ep = musb->endpoints + epnum;
271 musb_ep = &musb->endpoints[num].ep_in; 273 regs = ep->regs;
274 is_in = ctrlrequest->wIndex & USB_DIR_IN;
275 if (is_in)
276 musb_ep = &ep->ep_in;
272 else 277 else
273 musb_ep = &musb->endpoints[num].ep_out; 278 musb_ep = &ep->ep_out;
274 if (!musb_ep->desc) 279 if (!musb_ep->desc)
275 break; 280 break;
276 281
277 /* REVISIT do it directly, no locking games */ 282 handled = 1;
278 spin_unlock(&musb->lock); 283 /* Ignore request if endpoint is wedged */
279 musb_gadget_set_halt(&musb_ep->end_point, 0); 284 if (musb_ep->wedged)
280 spin_lock(&musb->lock); 285 break;
286
287 musb_ep_select(mbase, epnum);
288 if (is_in) {
289 csr = musb_readw(regs, MUSB_TXCSR);
290 csr |= MUSB_TXCSR_CLRDATATOG |
291 MUSB_TXCSR_P_WZC_BITS;
292 csr &= ~(MUSB_TXCSR_P_SENDSTALL |
293 MUSB_TXCSR_P_SENTSTALL |
294 MUSB_TXCSR_TXPKTRDY);
295 musb_writew(regs, MUSB_TXCSR, csr);
296 } else {
297 csr = musb_readw(regs, MUSB_RXCSR);
298 csr |= MUSB_RXCSR_CLRDATATOG |
299 MUSB_RXCSR_P_WZC_BITS;
300 csr &= ~(MUSB_RXCSR_P_SENDSTALL |
301 MUSB_RXCSR_P_SENTSTALL);
302 musb_writew(regs, MUSB_RXCSR, csr);
303 }
281 304
282 /* select ep0 again */ 305 /* select ep0 again */
283 musb_ep_select(mbase, 0); 306 musb_ep_select(mbase, 0);
284 handled = 1;
285 } break; 307 } break;
286 default: 308 default:
287 /* class, vendor, etc ... delegate */ 309 /* class, vendor, etc ... delegate */
@@ -374,10 +396,8 @@ stall:
374 int is_in; 396 int is_in;
375 u16 csr; 397 u16 csr;
376 398
377 if (epnum == 0 399 if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
378 || epnum >= MUSB_C_NUM_EPS 400 ctrlrequest->wValue != USB_ENDPOINT_HALT)
379 || ctrlrequest->wValue
380 != USB_ENDPOINT_HALT)
381 break; 401 break;
382 402
383 ep = musb->endpoints + epnum; 403 ep = musb->endpoints + epnum;
@@ -392,24 +412,20 @@ stall:
392 412
393 musb_ep_select(mbase, epnum); 413 musb_ep_select(mbase, epnum);
394 if (is_in) { 414 if (is_in) {
395 csr = musb_readw(regs, 415 csr = musb_readw(regs, MUSB_TXCSR);
396 MUSB_TXCSR);
397 if (csr & MUSB_TXCSR_FIFONOTEMPTY) 416 if (csr & MUSB_TXCSR_FIFONOTEMPTY)
398 csr |= MUSB_TXCSR_FLUSHFIFO; 417 csr |= MUSB_TXCSR_FLUSHFIFO;
399 csr |= MUSB_TXCSR_P_SENDSTALL 418 csr |= MUSB_TXCSR_P_SENDSTALL
400 | MUSB_TXCSR_CLRDATATOG 419 | MUSB_TXCSR_CLRDATATOG
401 | MUSB_TXCSR_P_WZC_BITS; 420 | MUSB_TXCSR_P_WZC_BITS;
402 musb_writew(regs, MUSB_TXCSR, 421 musb_writew(regs, MUSB_TXCSR, csr);
403 csr);
404 } else { 422 } else {
405 csr = musb_readw(regs, 423 csr = musb_readw(regs, MUSB_RXCSR);
406 MUSB_RXCSR);
407 csr |= MUSB_RXCSR_P_SENDSTALL 424 csr |= MUSB_RXCSR_P_SENDSTALL
408 | MUSB_RXCSR_FLUSHFIFO 425 | MUSB_RXCSR_FLUSHFIFO
409 | MUSB_RXCSR_CLRDATATOG 426 | MUSB_RXCSR_CLRDATATOG
410 | MUSB_RXCSR_P_WZC_BITS; 427 | MUSB_RXCSR_P_WZC_BITS;
411 musb_writew(regs, MUSB_RXCSR, 428 musb_writew(regs, MUSB_RXCSR, csr);
412 csr);
413 } 429 }
414 430
415 /* select ep0 again */ 431 /* select ep0 again */
diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index e3ab40a966eb..74c4c3698f1e 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -1642,18 +1642,18 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1642 c = musb->dma_controller; 1642 c = musb->dma_controller;
1643 1643
1644 if (usb_pipeisoc(pipe)) { 1644 if (usb_pipeisoc(pipe)) {
1645 int status = 0; 1645 int d_status = 0;
1646 struct usb_iso_packet_descriptor *d; 1646 struct usb_iso_packet_descriptor *d;
1647 1647
1648 d = urb->iso_frame_desc + qh->iso_idx; 1648 d = urb->iso_frame_desc + qh->iso_idx;
1649 1649
1650 if (iso_err) { 1650 if (iso_err) {
1651 status = -EILSEQ; 1651 d_status = -EILSEQ;
1652 urb->error_count++; 1652 urb->error_count++;
1653 } 1653 }
1654 if (rx_count > d->length) { 1654 if (rx_count > d->length) {
1655 if (status == 0) { 1655 if (d_status == 0) {
1656 status = -EOVERFLOW; 1656 d_status = -EOVERFLOW;
1657 urb->error_count++; 1657 urb->error_count++;
1658 } 1658 }
1659 DBG(2, "** OVERFLOW %d into %d\n",\ 1659 DBG(2, "** OVERFLOW %d into %d\n",\
@@ -1662,7 +1662,7 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1662 length = d->length; 1662 length = d->length;
1663 } else 1663 } else
1664 length = rx_count; 1664 length = rx_count;
1665 d->status = status; 1665 d->status = d_status;
1666 buf = urb->transfer_dma + d->offset; 1666 buf = urb->transfer_dma + d->offset;
1667 } else { 1667 } else {
1668 length = rx_count; 1668 length = rx_count;
diff --git a/drivers/usb/musb/musb_regs.h b/drivers/usb/musb/musb_regs.h
index cc1d71b57d3c..473a94ef905f 100644
--- a/drivers/usb/musb/musb_regs.h
+++ b/drivers/usb/musb/musb_regs.h
@@ -465,9 +465,9 @@ static inline u16 musb_read_hwvers(void __iomem *mbase)
465 return 0; 465 return 0;
466} 466}
467 467
468static inline u16 musb_read_target_reg_base(u8 i, void __iomem *mbase) 468static inline void __iomem *musb_read_target_reg_base(u8 i, void __iomem *mbase)
469{ 469{
470 return 0; 470 return NULL;
471} 471}
472 472
473static inline void musb_write_rxfunaddr(void __iomem *ep_target_regs, 473static inline void musb_write_rxfunaddr(void __iomem *ep_target_regs,
diff --git a/drivers/usb/musb/musbhsdma.c b/drivers/usb/musb/musbhsdma.c
index 5e83f96d6b77..a237550f91bf 100644
--- a/drivers/usb/musb/musbhsdma.c
+++ b/drivers/usb/musb/musbhsdma.c
@@ -259,6 +259,11 @@ static irqreturn_t dma_controller_irq(int irq, void *private_data)
259 if (!int_hsdma) 259 if (!int_hsdma)
260 goto done; 260 goto done;
261 261
262#ifdef CONFIG_BLACKFIN
263 /* Clear DMA interrupt flags */
264 musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
265#endif
266
262 for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) { 267 for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
263 if (int_hsdma & (1 << bchannel)) { 268 if (int_hsdma & (1 << bchannel)) {
264 musb_channel = (struct musb_dma_channel *) 269 musb_channel = (struct musb_dma_channel *)
@@ -280,7 +285,7 @@ static irqreturn_t dma_controller_irq(int irq, void *private_data)
280 channel->actual_len = addr 285 channel->actual_len = addr
281 - musb_channel->start_addr; 286 - musb_channel->start_addr;
282 287
283 DBG(2, "ch %p, 0x%x -> 0x%x (%d / %d) %s\n", 288 DBG(2, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
284 channel, musb_channel->start_addr, 289 channel, musb_channel->start_addr,
285 addr, channel->actual_len, 290 addr, channel->actual_len,
286 musb_channel->len, 291 musb_channel->len,
@@ -324,11 +329,6 @@ static irqreturn_t dma_controller_irq(int irq, void *private_data)
324 } 329 }
325 } 330 }
326 331
327#ifdef CONFIG_BLACKFIN
328 /* Clear DMA interrup flags */
329 musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
330#endif
331
332 retval = IRQ_HANDLED; 332 retval = IRQ_HANDLED;
333done: 333done:
334 spin_unlock_irqrestore(&musb->lock, flags); 334 spin_unlock_irqrestore(&musb->lock, flags);
diff --git a/drivers/usb/musb/omap2430.c b/drivers/usb/musb/omap2430.c
index 6761d2088db8..83beeac5e7bf 100644
--- a/drivers/usb/musb/omap2430.c
+++ b/drivers/usb/musb/omap2430.c
@@ -315,7 +315,7 @@ int musb_platform_exit(struct musb *musb)
315 musb_platform_suspend(musb); 315 musb_platform_suspend(musb);
316 316
317 clk_put(musb->clock); 317 clk_put(musb->clock);
318 musb->clock = 0; 318 musb->clock = NULL;
319 319
320 return 0; 320 return 0;
321} 321}
diff --git a/drivers/usb/otg/Kconfig b/drivers/usb/otg/Kconfig
index aa884d072f0b..de56b3d743d7 100644
--- a/drivers/usb/otg/Kconfig
+++ b/drivers/usb/otg/Kconfig
@@ -41,6 +41,15 @@ config ISP1301_OMAP
41 This driver can also be built as a module. If so, the module 41 This driver can also be built as a module. If so, the module
42 will be called isp1301_omap. 42 will be called isp1301_omap.
43 43
44config USB_ULPI
45 bool "Generic ULPI Transceiver Driver"
46 depends on ARM
47 help
48 Enable this to support ULPI connected USB OTG transceivers which
49 are likely found on embedded boards.
50
51 The only chip currently supported is NXP's ISP1504
52
44config TWL4030_USB 53config TWL4030_USB
45 tristate "TWL4030 USB Transceiver Driver" 54 tristate "TWL4030 USB Transceiver Driver"
46 depends on TWL4030_CORE && REGULATOR_TWL4030 55 depends on TWL4030_CORE && REGULATOR_TWL4030
diff --git a/drivers/usb/otg/Makefile b/drivers/usb/otg/Makefile
index 208167856529..aeb49a8ec412 100644
--- a/drivers/usb/otg/Makefile
+++ b/drivers/usb/otg/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_USB_GPIO_VBUS) += gpio_vbus.o
10obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o 10obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
11obj-$(CONFIG_TWL4030_USB) += twl4030-usb.o 11obj-$(CONFIG_TWL4030_USB) += twl4030-usb.o
12obj-$(CONFIG_NOP_USB_XCEIV) += nop-usb-xceiv.o 12obj-$(CONFIG_NOP_USB_XCEIV) += nop-usb-xceiv.o
13obj-$(CONFIG_USB_ULPI) += ulpi.o
13 14
14ccflags-$(CONFIG_USB_DEBUG) += -DDEBUG 15ccflags-$(CONFIG_USB_DEBUG) += -DDEBUG
15ccflags-$(CONFIG_USB_GADGET_DEBUG) += -DDEBUG 16ccflags-$(CONFIG_USB_GADGET_DEBUG) += -DDEBUG
diff --git a/drivers/usb/otg/twl4030-usb.c b/drivers/usb/otg/twl4030-usb.c
index 9e3e7a5c258b..bd9883f41e63 100644
--- a/drivers/usb/otg/twl4030-usb.c
+++ b/drivers/usb/otg/twl4030-usb.c
@@ -598,12 +598,12 @@ static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
598 * USB_LINK_VBUS state. musb_hdrc won't care until it 598 * USB_LINK_VBUS state. musb_hdrc won't care until it
599 * starts to handle softconnect right. 599 * starts to handle softconnect right.
600 */ 600 */
601 twl4030charger_usb_en(status == USB_LINK_VBUS);
602
603 if (status == USB_LINK_NONE) 601 if (status == USB_LINK_NONE)
604 twl4030_phy_suspend(twl, 0); 602 twl4030_phy_suspend(twl, 0);
605 else 603 else
606 twl4030_phy_resume(twl); 604 twl4030_phy_resume(twl);
605
606 twl4030charger_usb_en(status == USB_LINK_VBUS);
607 } 607 }
608 sysfs_notify(&twl->dev->kobj, NULL, "vbus"); 608 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
609 609
diff --git a/drivers/usb/otg/ulpi.c b/drivers/usb/otg/ulpi.c
new file mode 100644
index 000000000000..896527456b7e
--- /dev/null
+++ b/drivers/usb/otg/ulpi.c
@@ -0,0 +1,136 @@
1/*
2 * Generic ULPI USB transceiver support
3 *
4 * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
5 *
6 * Based on sources from
7 *
8 * Sascha Hauer <s.hauer@pengutronix.de>
9 * Freescale Semiconductors
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/kernel.h>
27#include <linux/usb.h>
28#include <linux/usb/otg.h>
29#include <linux/usb/ulpi.h>
30
31/* ULPI register addresses */
32#define ULPI_VID_LOW 0x00 /* Vendor ID low */
33#define ULPI_VID_HIGH 0x01 /* Vendor ID high */
34#define ULPI_PID_LOW 0x02 /* Product ID low */
35#define ULPI_PID_HIGH 0x03 /* Product ID high */
36#define ULPI_ITFCTL 0x07 /* Interface Control */
37#define ULPI_OTGCTL 0x0A /* OTG Control */
38
39/* add to above register address to access Set/Clear functions */
40#define ULPI_REG_SET 0x01
41#define ULPI_REG_CLEAR 0x02
42
43/* ULPI OTG Control Register bits */
44#define ID_PULL_UP (1 << 0) /* enable ID Pull Up */
45#define DP_PULL_DOWN (1 << 1) /* enable DP Pull Down */
46#define DM_PULL_DOWN (1 << 2) /* enable DM Pull Down */
47#define DISCHRG_VBUS (1 << 3) /* Discharge Vbus */
48#define CHRG_VBUS (1 << 4) /* Charge Vbus */
49#define DRV_VBUS (1 << 5) /* Drive Vbus */
50#define DRV_VBUS_EXT (1 << 6) /* Drive Vbus external */
51#define USE_EXT_VBUS_IND (1 << 7) /* Use ext. Vbus indicator */
52
53#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
54
55#define TR_FLAG(flags, a, b) (((flags) & a) ? b : 0)
56
57/* ULPI hardcoded IDs, used for probing */
58static unsigned int ulpi_ids[] = {
59 ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
60};
61
62static int ulpi_set_flags(struct otg_transceiver *otg)
63{
64 unsigned int flags = 0;
65
66 if (otg->flags & USB_OTG_PULLUP_ID)
67 flags |= ID_PULL_UP;
68
69 if (otg->flags & USB_OTG_PULLDOWN_DM)
70 flags |= DM_PULL_DOWN;
71
72 if (otg->flags & USB_OTG_PULLDOWN_DP)
73 flags |= DP_PULL_DOWN;
74
75 if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR)
76 flags |= USE_EXT_VBUS_IND;
77
78 return otg_io_write(otg, flags, ULPI_OTGCTL + ULPI_REG_SET);
79}
80
81static int ulpi_init(struct otg_transceiver *otg)
82{
83 int i, vid, pid;
84
85 vid = (otg_io_read(otg, ULPI_VID_HIGH) << 8) |
86 otg_io_read(otg, ULPI_VID_LOW);
87 pid = (otg_io_read(otg, ULPI_PID_HIGH) << 8) |
88 otg_io_read(otg, ULPI_PID_LOW);
89
90 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
91
92 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++)
93 if (ulpi_ids[i] == ULPI_ID(vid, pid))
94 return ulpi_set_flags(otg);
95
96 pr_err("ULPI ID does not match any known transceiver.\n");
97 return -ENODEV;
98}
99
100static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
101{
102 unsigned int flags = otg_io_read(otg, ULPI_OTGCTL);
103
104 flags &= ~(DRV_VBUS | DRV_VBUS_EXT);
105
106 if (on) {
107 if (otg->flags & USB_OTG_DRV_VBUS)
108 flags |= DRV_VBUS;
109
110 if (otg->flags & USB_OTG_DRV_VBUS_EXT)
111 flags |= DRV_VBUS_EXT;
112 }
113
114 return otg_io_write(otg, flags, ULPI_OTGCTL + ULPI_REG_SET);
115}
116
117struct otg_transceiver *
118otg_ulpi_create(struct otg_io_access_ops *ops,
119 unsigned int flags)
120{
121 struct otg_transceiver *otg;
122
123 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
124 if (!otg)
125 return NULL;
126
127 otg->label = "ULPI";
128 otg->flags = flags;
129 otg->io_ops = ops;
130 otg->init = ulpi_init;
131 otg->set_vbus = ulpi_set_vbus;
132
133 return otg;
134}
135EXPORT_SYMBOL_GPL(otg_ulpi_create);
136
diff --git a/drivers/usb/serial/ark3116.c b/drivers/usb/serial/ark3116.c
index 131e61adaaf7..a9c2dec8e3fb 100644
--- a/drivers/usb/serial/ark3116.c
+++ b/drivers/usb/serial/ark3116.c
@@ -1,4 +1,6 @@
1/* 1/*
2 * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
3 * Original version:
2 * Copyright (C) 2006 4 * Copyright (C) 2006
3 * Simon Schulz (ark3116_driver <at> auctionant.de) 5 * Simon Schulz (ark3116_driver <at> auctionant.de)
4 * 6 *
@@ -6,10 +8,13 @@
6 * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547, 8 * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547,
7 * productid=0x0232) (used in a datacable called KQ-U8A) 9 * productid=0x0232) (used in a datacable called KQ-U8A)
8 * 10 *
9 * - based on code by krisfx -> thanks !! 11 * Supports full modem status lines, break, hardware flow control. Does not
10 * (see http://www.linuxquestions.org/questions/showthread.php?p=2184457#post2184457) 12 * support software flow control, since I do not know how to enable it in hw.
11 * 13 *
12 * - based on logs created by usbsnoopy 14 * This driver is a essentially new implementation. I initially dug
15 * into the old ark3116.c driver and suddenly realized the ark3116 is
16 * a 16450 with a USB interface glued to it. See comments at the
17 * bottom of this file.
13 * 18 *
14 * This program is free software; you can redistribute it and/or modify it 19 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the 20 * under the terms of the GNU General Public License as published by the
@@ -19,15 +24,31 @@
19 24
20#include <linux/kernel.h> 25#include <linux/kernel.h>
21#include <linux/init.h> 26#include <linux/init.h>
27#include <linux/ioctl.h>
22#include <linux/tty.h> 28#include <linux/tty.h>
29#include <linux/tty_flip.h>
23#include <linux/module.h> 30#include <linux/module.h>
24#include <linux/usb.h> 31#include <linux/usb.h>
25#include <linux/usb/serial.h> 32#include <linux/usb/serial.h>
26#include <linux/serial.h> 33#include <linux/serial.h>
34#include <linux/serial_reg.h>
27#include <linux/uaccess.h> 35#include <linux/uaccess.h>
28 36#include <linux/mutex.h>
37#include <linux/spinlock.h>
29 38
30static int debug; 39static int debug;
40/*
41 * Version information
42 */
43
44#define DRIVER_VERSION "v0.5"
45#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
46#define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
47#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
48#define DRIVER_NAME "ark3116"
49
50/* usb timeout of 1 second */
51#define ARK_TIMEOUT (1*HZ)
31 52
32static struct usb_device_id id_table [] = { 53static struct usb_device_id id_table [] = {
33 { USB_DEVICE(0x6547, 0x0232) }, 54 { USB_DEVICE(0x6547, 0x0232) },
@@ -45,118 +66,152 @@ static int is_irda(struct usb_serial *serial)
45 return 0; 66 return 0;
46} 67}
47 68
48static inline void ARK3116_SND(struct usb_serial *serial, int seq, 69struct ark3116_private {
49 __u8 request, __u8 requesttype, 70 wait_queue_head_t delta_msr_wait;
50 __u16 value, __u16 index) 71 struct async_icount icount;
72 int irda; /* 1 for irda device */
73
74 /* protects hw register updates */
75 struct mutex hw_lock;
76
77 int quot; /* baudrate divisor */
78 __u32 lcr; /* line control register value */
79 __u32 hcr; /* handshake control register (0x8)
80 * value */
81 __u32 mcr; /* modem contol register value */
82
83 /* protects the status values below */
84 spinlock_t status_lock;
85 __u32 msr; /* modem status register value */
86 __u32 lsr; /* line status register value */
87};
88
89static int ark3116_write_reg(struct usb_serial *serial,
90 unsigned reg, __u8 val)
51{ 91{
52 int result; 92 int result;
93 /* 0xfe 0x40 are magic values taken from original driver */
53 result = usb_control_msg(serial->dev, 94 result = usb_control_msg(serial->dev,
54 usb_sndctrlpipe(serial->dev, 0), 95 usb_sndctrlpipe(serial->dev, 0),
55 request, requesttype, value, index, 96 0xfe, 0x40, val, reg,
56 NULL, 0x00, 1000); 97 NULL, 0, ARK_TIMEOUT);
57 dbg("%03d > ok", seq); 98 return result;
58} 99}
59 100
60static inline void ARK3116_RCV(struct usb_serial *serial, int seq, 101static int ark3116_read_reg(struct usb_serial *serial,
61 __u8 request, __u8 requesttype, 102 unsigned reg, unsigned char *buf)
62 __u16 value, __u16 index, __u8 expected,
63 char *buf)
64{ 103{
65 int result; 104 int result;
105 /* 0xfe 0xc0 are magic values taken from original driver */
66 result = usb_control_msg(serial->dev, 106 result = usb_control_msg(serial->dev,
67 usb_rcvctrlpipe(serial->dev, 0), 107 usb_rcvctrlpipe(serial->dev, 0),
68 request, requesttype, value, index, 108 0xfe, 0xc0, 0, reg,
69 buf, 0x0000001, 1000); 109 buf, 1, ARK_TIMEOUT);
70 if (result) 110 if (result < 0)
71 dbg("%03d < %d bytes [0x%02X]", seq, result, 111 return result;
72 ((unsigned char *)buf)[0]);
73 else 112 else
74 dbg("%03d < 0 bytes", seq); 113 return buf[0];
75} 114}
76 115
77static inline void ARK3116_RCV_QUIET(struct usb_serial *serial, 116static inline int calc_divisor(int bps)
78 __u8 request, __u8 requesttype,
79 __u16 value, __u16 index, char *buf)
80{ 117{
81 usb_control_msg(serial->dev, 118 /* Original ark3116 made some exceptions in rounding here
82 usb_rcvctrlpipe(serial->dev, 0), 119 * because windows did the same. Assume that is not really
83 request, requesttype, value, index, 120 * necessary.
84 buf, 0x0000001, 1000); 121 * Crystal is 12MHz, probably because of USB, but we divide by 4?
122 */
123 return (12000000 + 2*bps) / (4*bps);
85} 124}
86 125
87static int ark3116_attach(struct usb_serial *serial) 126static int ark3116_attach(struct usb_serial *serial)
88{ 127{
89 char *buf; 128 struct usb_serial_port *port = serial->port[0];
129 struct ark3116_private *priv;
130
131 /* make sure we have our end-points */
132 if ((serial->num_bulk_in == 0) ||
133 (serial->num_bulk_out == 0) ||
134 (serial->num_interrupt_in == 0)) {
135 dev_err(&serial->dev->dev,
136 "%s - missing endpoint - "
137 "bulk in: %d, bulk out: %d, int in %d\n",
138 KBUILD_MODNAME,
139 serial->num_bulk_in,
140 serial->num_bulk_out,
141 serial->num_interrupt_in);
142 return -EINVAL;
143 }
90 144
91 buf = kmalloc(1, GFP_KERNEL); 145 priv = kzalloc(sizeof(struct ark3116_private),
92 if (!buf) { 146 GFP_KERNEL);
93 dbg("error kmalloc -> out of mem?"); 147 if (!priv)
94 return -ENOMEM; 148 return -ENOMEM;
95 }
96 149
97 if (is_irda(serial)) 150 init_waitqueue_head(&priv->delta_msr_wait);
98 dbg("IrDA mode"); 151 mutex_init(&priv->hw_lock);
152 spin_lock_init(&priv->status_lock);
153
154 priv->irda = is_irda(serial);
99 155
100 /* 3 */ 156 usb_set_serial_port_data(port, priv);
101 ARK3116_SND(serial, 3, 0xFE, 0x40, 0x0008, 0x0002);
102 ARK3116_SND(serial, 4, 0xFE, 0x40, 0x0008, 0x0001);
103 ARK3116_SND(serial, 5, 0xFE, 0x40, 0x0000, 0x0008);
104 ARK3116_SND(serial, 6, 0xFE, 0x40, is_irda(serial) ? 0x0001 : 0x0000,
105 0x000B);
106 157
107 if (is_irda(serial)) { 158 /* setup the hardware */
108 ARK3116_SND(serial, 1001, 0xFE, 0x40, 0x0000, 0x000C); 159 ark3116_write_reg(serial, UART_IER, 0);
109 ARK3116_SND(serial, 1002, 0xFE, 0x40, 0x0041, 0x000D); 160 /* disable DMA */
110 ARK3116_SND(serial, 1003, 0xFE, 0x40, 0x0001, 0x000A); 161 ark3116_write_reg(serial, UART_FCR, 0);
162 /* handshake control */
163 priv->hcr = 0;
164 ark3116_write_reg(serial, 0x8 , 0);
165 /* modem control */
166 priv->mcr = 0;
167 ark3116_write_reg(serial, UART_MCR, 0);
168
169 if (!(priv->irda)) {
170 ark3116_write_reg(serial, 0xb , 0);
171 } else {
172 ark3116_write_reg(serial, 0xb , 1);
173 ark3116_write_reg(serial, 0xc , 0);
174 ark3116_write_reg(serial, 0xd , 0x41);
175 ark3116_write_reg(serial, 0xa , 1);
111 } 176 }
112 177
113 /* <-- seq7 */ 178 /* setup baudrate */
114 ARK3116_RCV(serial, 7, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf); 179 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
115 ARK3116_SND(serial, 8, 0xFE, 0x40, 0x0080, 0x0003);
116 ARK3116_SND(serial, 9, 0xFE, 0x40, 0x001A, 0x0000);
117 ARK3116_SND(serial, 10, 0xFE, 0x40, 0x0000, 0x0001);
118 ARK3116_SND(serial, 11, 0xFE, 0x40, 0x0000, 0x0003);
119
120 /* <-- seq12 */
121 ARK3116_RCV(serial, 12, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
122 ARK3116_SND(serial, 13, 0xFE, 0x40, 0x0000, 0x0004);
123
124 /* 14 */
125 ARK3116_RCV(serial, 14, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
126 ARK3116_SND(serial, 15, 0xFE, 0x40, 0x0000, 0x0004);
127
128 /* 16 */
129 ARK3116_RCV(serial, 16, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
130 /* --> seq17 */
131 ARK3116_SND(serial, 17, 0xFE, 0x40, 0x0001, 0x0004);
132
133 /* <-- seq18 */
134 ARK3116_RCV(serial, 18, 0xFE, 0xC0, 0x0000, 0x0004, 0x01, buf);
135
136 /* --> seq19 */
137 ARK3116_SND(serial, 19, 0xFE, 0x40, 0x0003, 0x0004);
138
139 /* <-- seq20 */
140 /* seems like serial port status info (RTS, CTS, ...) */
141 /* returns modem control line status?! */
142 ARK3116_RCV(serial, 20, 0xFE, 0xC0, 0x0000, 0x0006, 0xFF, buf);
143
144 /* set 9600 baud & do some init?! */
145 ARK3116_SND(serial, 147, 0xFE, 0x40, 0x0083, 0x0003);
146 ARK3116_SND(serial, 148, 0xFE, 0x40, 0x0038, 0x0000);
147 ARK3116_SND(serial, 149, 0xFE, 0x40, 0x0001, 0x0001);
148 if (is_irda(serial))
149 ARK3116_SND(serial, 1004, 0xFE, 0x40, 0x0000, 0x0009);
150 ARK3116_SND(serial, 150, 0xFE, 0x40, 0x0003, 0x0003);
151 ARK3116_RCV(serial, 151, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
152 ARK3116_SND(serial, 152, 0xFE, 0x40, 0x0000, 0x0003);
153 ARK3116_RCV(serial, 153, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf);
154 ARK3116_SND(serial, 154, 0xFE, 0x40, 0x0003, 0x0003);
155 180
156 kfree(buf); 181 /* setup for 9600 8N1 */
182 priv->quot = calc_divisor(9600);
183 ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
184 ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
185
186 priv->lcr = UART_LCR_WLEN8;
187 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
188
189 ark3116_write_reg(serial, 0xe, 0);
190
191 if (priv->irda)
192 ark3116_write_reg(serial, 0x9, 0);
193
194 dev_info(&serial->dev->dev,
195 "%s using %s mode\n",
196 KBUILD_MODNAME,
197 priv->irda ? "IrDA" : "RS232");
157 return 0; 198 return 0;
158} 199}
159 200
201static void ark3116_release(struct usb_serial *serial)
202{
203 struct usb_serial_port *port = serial->port[0];
204 struct ark3116_private *priv = usb_get_serial_port_data(port);
205
206 /* device is closed, so URBs and DMA should be down */
207
208 usb_set_serial_port_data(port, NULL);
209
210 mutex_destroy(&priv->hw_lock);
211
212 kfree(priv);
213}
214
160static void ark3116_init_termios(struct tty_struct *tty) 215static void ark3116_init_termios(struct tty_struct *tty)
161{ 216{
162 struct ktermios *termios = tty->termios; 217 struct ktermios *termios = tty->termios;
@@ -172,200 +227,189 @@ static void ark3116_set_termios(struct tty_struct *tty,
172 struct ktermios *old_termios) 227 struct ktermios *old_termios)
173{ 228{
174 struct usb_serial *serial = port->serial; 229 struct usb_serial *serial = port->serial;
230 struct ark3116_private *priv = usb_get_serial_port_data(port);
175 struct ktermios *termios = tty->termios; 231 struct ktermios *termios = tty->termios;
176 unsigned int cflag = termios->c_cflag; 232 unsigned int cflag = termios->c_cflag;
177 int baud; 233 int bps = tty_get_baud_rate(tty);
178 int ark3116_baud; 234 int quot;
179 char *buf; 235 __u8 lcr, hcr, eval;
180 char config; 236
181 237 /* set data bit count */
182 config = 0; 238 switch (cflag & CSIZE) {
183 239 case CS5:
184 dbg("%s - port %d", __func__, port->number); 240 lcr = UART_LCR_WLEN5;
241 break;
242 case CS6:
243 lcr = UART_LCR_WLEN6;
244 break;
245 case CS7:
246 lcr = UART_LCR_WLEN7;
247 break;
248 default:
249 case CS8:
250 lcr = UART_LCR_WLEN8;
251 break;
252 }
253 if (cflag & CSTOPB)
254 lcr |= UART_LCR_STOP;
255 if (cflag & PARENB)
256 lcr |= UART_LCR_PARITY;
257 if (!(cflag & PARODD))
258 lcr |= UART_LCR_EPAR;
259#ifdef CMSPAR
260 if (cflag & CMSPAR)
261 lcr |= UART_LCR_SPAR;
262#endif
263 /* handshake control */
264 hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
265
266 /* calc baudrate */
267 dbg("%s - setting bps to %d", __func__, bps);
268 eval = 0;
269 switch (bps) {
270 case 0:
271 quot = calc_divisor(9600);
272 break;
273 default:
274 if ((bps < 75) || (bps > 3000000))
275 bps = 9600;
276 quot = calc_divisor(bps);
277 break;
278 case 460800:
279 eval = 1;
280 quot = calc_divisor(bps);
281 break;
282 case 921600:
283 eval = 2;
284 quot = calc_divisor(bps);
285 break;
286 }
185 287
288 /* Update state: synchronize */
289 mutex_lock(&priv->hw_lock);
186 290
187 cflag = termios->c_cflag; 291 /* keep old LCR_SBC bit */
188 termios->c_cflag &= ~(CMSPAR|CRTSCTS); 292 lcr |= (priv->lcr & UART_LCR_SBC);
189 293
190 buf = kmalloc(1, GFP_KERNEL); 294 dbg("%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d",
191 if (!buf) { 295 __func__, hcr, lcr, quot);
192 dbg("error kmalloc");
193 *termios = *old_termios;
194 return;
195 }
196 296
197 /* set data bit count (8/7/6/5) */ 297 /* handshake control */
198 if (cflag & CSIZE) { 298 if (priv->hcr != hcr) {
199 switch (cflag & CSIZE) { 299 priv->hcr = hcr;
200 case CS5: 300 ark3116_write_reg(serial, 0x8, hcr);
201 config |= 0x00;
202 dbg("setting CS5");
203 break;
204 case CS6:
205 config |= 0x01;
206 dbg("setting CS6");
207 break;
208 case CS7:
209 config |= 0x02;
210 dbg("setting CS7");
211 break;
212 default:
213 dbg("CSIZE was set but not CS5-CS8, using CS8!");
214 /* fall through */
215 case CS8:
216 config |= 0x03;
217 dbg("setting CS8");
218 break;
219 }
220 } 301 }
221 302
222 /* set parity (NONE/EVEN/ODD) */ 303 /* baudrate */
223 if (cflag & PARENB) { 304 if (priv->quot != quot) {
224 if (cflag & PARODD) { 305 priv->quot = quot;
225 config |= 0x08; 306 priv->lcr = lcr; /* need to write lcr anyway */
226 dbg("setting parity to ODD"); 307
227 } else { 308 /* disable DMA since transmit/receive is
228 config |= 0x18; 309 * shadowed by UART_DLL
229 dbg("setting parity to EVEN"); 310 */
230 } 311 ark3116_write_reg(serial, UART_FCR, 0);
231 } else { 312
232 dbg("setting parity to NONE"); 313 ark3116_write_reg(serial, UART_LCR,
314 lcr|UART_LCR_DLAB);
315 ark3116_write_reg(serial, UART_DLL, quot & 0xff);
316 ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
317
318 /* restore lcr */
319 ark3116_write_reg(serial, UART_LCR, lcr);
320 /* magic baudrate thingy: not sure what it does,
321 * but windows does this as well.
322 */
323 ark3116_write_reg(serial, 0xe, eval);
324
325 /* enable DMA */
326 ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
327 } else if (priv->lcr != lcr) {
328 priv->lcr = lcr;
329 ark3116_write_reg(serial, UART_LCR, lcr);
233 } 330 }
234 331
235 /* set stop bit (1/2) */ 332 mutex_unlock(&priv->hw_lock);
236 if (cflag & CSTOPB) {
237 config |= 0x04;
238 dbg("setting 2 stop bits");
239 } else {
240 dbg("setting 1 stop bit");
241 }
242 333
243 /* set baudrate */ 334 /* check for software flow control */
244 baud = tty_get_baud_rate(tty); 335 if (I_IXOFF(tty) || I_IXON(tty)) {
245 336 dev_warn(&serial->dev->dev,
246 switch (baud) { 337 "%s: don't know how to do software flow control\n",
247 case 75: 338 KBUILD_MODNAME);
248 case 150:
249 case 300:
250 case 600:
251 case 1200:
252 case 1800:
253 case 2400:
254 case 4800:
255 case 9600:
256 case 19200:
257 case 38400:
258 case 57600:
259 case 115200:
260 case 230400:
261 case 460800:
262 /* Report the resulting rate back to the caller */
263 tty_encode_baud_rate(tty, baud, baud);
264 break;
265 /* set 9600 as default (if given baudrate is invalid for example) */
266 default:
267 tty_encode_baud_rate(tty, 9600, 9600);
268 case 0:
269 baud = 9600;
270 } 339 }
271 340
272 /* 341 /* Don't rewrite B0 */
273 * found by try'n'error, be careful, maybe there are other options 342 if (tty_termios_baud_rate(termios))
274 * for multiplicator etc! (3.5 for example) 343 tty_termios_encode_baud_rate(termios, bps, bps);
275 */ 344}
276 if (baud == 460800)
277 /* strange, for 460800 the formula is wrong
278 * if using round() then 9600baud is wrong) */
279 ark3116_baud = 7;
280 else
281 ark3116_baud = 3000000 / baud;
282
283 /* ? */
284 ARK3116_RCV(serial, 0, 0xFE, 0xC0, 0x0000, 0x0003, 0x03, buf);
285
286 /* offset = buf[0]; */
287 /* offset = 0x03; */
288 /* dbg("using 0x%04X as target for 0x0003:", 0x0080 + offset); */
289
290 /* set baudrate */
291 dbg("setting baudrate to %d (->reg=%d)", baud, ark3116_baud);
292 ARK3116_SND(serial, 147, 0xFE, 0x40, 0x0083, 0x0003);
293 ARK3116_SND(serial, 148, 0xFE, 0x40,
294 (ark3116_baud & 0x00FF), 0x0000);
295 ARK3116_SND(serial, 149, 0xFE, 0x40,
296 (ark3116_baud & 0xFF00) >> 8, 0x0001);
297 ARK3116_SND(serial, 150, 0xFE, 0x40, 0x0003, 0x0003);
298
299 /* ? */
300 ARK3116_RCV(serial, 151, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
301 ARK3116_SND(serial, 152, 0xFE, 0x40, 0x0000, 0x0003);
302
303 /* set data bit count, stop bit count & parity: */
304 dbg("updating bit count, stop bit or parity (cfg=0x%02X)", config);
305 ARK3116_RCV(serial, 153, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf);
306 ARK3116_SND(serial, 154, 0xFE, 0x40, config, 0x0003);
307 345
308 if (cflag & CRTSCTS) 346static void ark3116_close(struct usb_serial_port *port)
309 dbg("CRTSCTS not supported by chipset?!"); 347{
348 struct usb_serial *serial = port->serial;
310 349
311 /* TEST ARK3116_SND(154, 0xFE, 0x40, 0xFFFF, 0x0006); */ 350 if (serial->dev) {
351 /* disable DMA */
352 ark3116_write_reg(serial, UART_FCR, 0);
312 353
313 kfree(buf); 354 /* deactivate interrupts */
355 ark3116_write_reg(serial, UART_IER, 0);
314 356
315 return; 357 /* shutdown any bulk reads that might be going on */
358 if (serial->num_bulk_out)
359 usb_kill_urb(port->write_urb);
360 if (serial->num_bulk_in)
361 usb_kill_urb(port->read_urb);
362 if (serial->num_interrupt_in)
363 usb_kill_urb(port->interrupt_in_urb);
364 }
316} 365}
317 366
318static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port) 367static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
319{ 368{
320 struct ktermios tmp_termios; 369 struct ark3116_private *priv = usb_get_serial_port_data(port);
321 struct usb_serial *serial = port->serial; 370 struct usb_serial *serial = port->serial;
322 char *buf; 371 unsigned char *buf;
323 int result = 0; 372 int result;
324
325 dbg("%s - port %d", __func__, port->number);
326 373
327 buf = kmalloc(1, GFP_KERNEL); 374 buf = kmalloc(1, GFP_KERNEL);
328 if (!buf) { 375 if (buf == NULL)
329 dbg("error kmalloc -> out of mem?");
330 return -ENOMEM; 376 return -ENOMEM;
331 }
332 377
333 result = usb_serial_generic_open(tty, port); 378 result = usb_serial_generic_open(tty, port);
334 if (result) 379 if (result) {
380 dbg("%s - usb_serial_generic_open failed: %d",
381 __func__, result);
335 goto err_out; 382 goto err_out;
383 }
336 384
337 /* open */ 385 /* setup termios */
338 ARK3116_RCV(serial, 111, 0xFE, 0xC0, 0x0000, 0x0003, 0x02, buf); 386 if (tty)
339 387 ark3116_set_termios(tty, port, NULL);
340 ARK3116_SND(serial, 112, 0xFE, 0x40, 0x0082, 0x0003);
341 ARK3116_SND(serial, 113, 0xFE, 0x40, 0x001A, 0x0000);
342 ARK3116_SND(serial, 114, 0xFE, 0x40, 0x0000, 0x0001);
343 ARK3116_SND(serial, 115, 0xFE, 0x40, 0x0002, 0x0003);
344
345 ARK3116_RCV(serial, 116, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
346 ARK3116_SND(serial, 117, 0xFE, 0x40, 0x0002, 0x0004);
347
348 ARK3116_RCV(serial, 118, 0xFE, 0xC0, 0x0000, 0x0004, 0x02, buf);
349 ARK3116_SND(serial, 119, 0xFE, 0x40, 0x0000, 0x0004);
350
351 ARK3116_RCV(serial, 120, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
352 388
353 ARK3116_SND(serial, 121, 0xFE, 0x40, 0x0001, 0x0004); 389 /* remove any data still left: also clears error state */
390 ark3116_read_reg(serial, UART_RX, buf);
354 391
355 ARK3116_RCV(serial, 122, 0xFE, 0xC0, 0x0000, 0x0004, 0x01, buf); 392 /* read modem status */
393 priv->msr = ark3116_read_reg(serial, UART_MSR, buf);
394 /* read line status */
395 priv->lsr = ark3116_read_reg(serial, UART_LSR, buf);
356 396
357 ARK3116_SND(serial, 123, 0xFE, 0x40, 0x0003, 0x0004); 397 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
398 if (result) {
399 dev_err(&port->dev, "submit irq_in urb failed %d\n",
400 result);
401 ark3116_close(port);
402 goto err_out;
403 }
358 404
359 /* returns different values (control lines?!) */ 405 /* activate interrupts */
360 ARK3116_RCV(serial, 124, 0xFE, 0xC0, 0x0000, 0x0006, 0xFF, buf); 406 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
361 407
362 /* initialise termios */ 408 /* enable DMA */
363 if (tty) 409 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
364 ark3116_set_termios(tty, port, &tmp_termios);
365 410
366err_out: 411err_out:
367 kfree(buf); 412 kfree(buf);
368
369 return result; 413 return result;
370} 414}
371 415
@@ -373,6 +417,7 @@ static int ark3116_ioctl(struct tty_struct *tty, struct file *file,
373 unsigned int cmd, unsigned long arg) 417 unsigned int cmd, unsigned long arg)
374{ 418{
375 struct usb_serial_port *port = tty->driver_data; 419 struct usb_serial_port *port = tty->driver_data;
420 struct ark3116_private *priv = usb_get_serial_port_data(port);
376 struct serial_struct serstruct; 421 struct serial_struct serstruct;
377 void __user *user_arg = (void __user *)arg; 422 void __user *user_arg = (void __user *)arg;
378 423
@@ -394,9 +439,48 @@ static int ark3116_ioctl(struct tty_struct *tty, struct file *file,
394 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct))) 439 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
395 return -EFAULT; 440 return -EFAULT;
396 return 0; 441 return 0;
397 default: 442 case TIOCMIWAIT:
398 dbg("%s cmd 0x%04x not supported", __func__, cmd); 443 for (;;) {
444 struct async_icount prev = priv->icount;
445 interruptible_sleep_on(&priv->delta_msr_wait);
446 /* see if a signal did it */
447 if (signal_pending(current))
448 return -ERESTARTSYS;
449 if ((prev.rng == priv->icount.rng) &&
450 (prev.dsr == priv->icount.dsr) &&
451 (prev.dcd == priv->icount.dcd) &&
452 (prev.cts == priv->icount.cts))
453 return -EIO;
454 if ((arg & TIOCM_RNG &&
455 (prev.rng != priv->icount.rng)) ||
456 (arg & TIOCM_DSR &&
457 (prev.dsr != priv->icount.dsr)) ||
458 (arg & TIOCM_CD &&
459 (prev.dcd != priv->icount.dcd)) ||
460 (arg & TIOCM_CTS &&
461 (prev.cts != priv->icount.cts)))
462 return 0;
463 }
399 break; 464 break;
465 case TIOCGICOUNT: {
466 struct serial_icounter_struct icount;
467 struct async_icount cnow = priv->icount;
468 memset(&icount, 0, sizeof(icount));
469 icount.cts = cnow.cts;
470 icount.dsr = cnow.dsr;
471 icount.rng = cnow.rng;
472 icount.dcd = cnow.dcd;
473 icount.rx = cnow.rx;
474 icount.tx = cnow.tx;
475 icount.frame = cnow.frame;
476 icount.overrun = cnow.overrun;
477 icount.parity = cnow.parity;
478 icount.brk = cnow.brk;
479 icount.buf_overrun = cnow.buf_overrun;
480 if (copy_to_user(user_arg, &icount, sizeof(icount)))
481 return -EFAULT;
482 return 0;
483 }
400 } 484 }
401 485
402 return -ENOIOCTLCMD; 486 return -ENOIOCTLCMD;
@@ -405,32 +489,273 @@ static int ark3116_ioctl(struct tty_struct *tty, struct file *file,
405static int ark3116_tiocmget(struct tty_struct *tty, struct file *file) 489static int ark3116_tiocmget(struct tty_struct *tty, struct file *file)
406{ 490{
407 struct usb_serial_port *port = tty->driver_data; 491 struct usb_serial_port *port = tty->driver_data;
408 struct usb_serial *serial = port->serial; 492 struct ark3116_private *priv = usb_get_serial_port_data(port);
409 char *buf; 493 __u32 status;
410 char temp; 494 __u32 ctrl;
495 unsigned long flags;
496
497 mutex_lock(&priv->hw_lock);
498 ctrl = priv->mcr;
499 mutex_unlock(&priv->hw_lock);
500
501 spin_lock_irqsave(&priv->status_lock, flags);
502 status = priv->msr;
503 spin_unlock_irqrestore(&priv->status_lock, flags);
504
505 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
506 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
507 (status & UART_MSR_RI ? TIOCM_RI : 0) |
508 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
509 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
510 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
511 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
512 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
513}
411 514
412 /* seems like serial port status info (RTS, CTS, ...) is stored 515static int ark3116_tiocmset(struct tty_struct *tty, struct file *file,
413 * in reg(?) 0x0006 516 unsigned set, unsigned clr)
414 * pcb connection point 11 = GND -> sets bit4 of response 517{
415 * pcb connection point 7 = GND -> sets bit6 of response 518 struct usb_serial_port *port = tty->driver_data;
519 struct ark3116_private *priv = usb_get_serial_port_data(port);
520
521 /* we need to take the mutex here, to make sure that the value
522 * in priv->mcr is actually the one that is in the hardware
416 */ 523 */
417 524
418 buf = kmalloc(1, GFP_KERNEL); 525 mutex_lock(&priv->hw_lock);
419 if (!buf) { 526
420 dbg("error kmalloc"); 527 if (set & TIOCM_RTS)
421 return -ENOMEM; 528 priv->mcr |= UART_MCR_RTS;
529 if (set & TIOCM_DTR)
530 priv->mcr |= UART_MCR_DTR;
531 if (set & TIOCM_OUT1)
532 priv->mcr |= UART_MCR_OUT1;
533 if (set & TIOCM_OUT2)
534 priv->mcr |= UART_MCR_OUT2;
535 if (clr & TIOCM_RTS)
536 priv->mcr &= ~UART_MCR_RTS;
537 if (clr & TIOCM_DTR)
538 priv->mcr &= ~UART_MCR_DTR;
539 if (clr & TIOCM_OUT1)
540 priv->mcr &= ~UART_MCR_OUT1;
541 if (clr & TIOCM_OUT2)
542 priv->mcr &= ~UART_MCR_OUT2;
543
544 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
545
546 mutex_unlock(&priv->hw_lock);
547
548 return 0;
549}
550
551static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
552{
553 struct usb_serial_port *port = tty->driver_data;
554 struct ark3116_private *priv = usb_get_serial_port_data(port);
555
556 /* LCR is also used for other things: protect access */
557 mutex_lock(&priv->hw_lock);
558
559 if (break_state)
560 priv->lcr |= UART_LCR_SBC;
561 else
562 priv->lcr &= ~UART_LCR_SBC;
563
564 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
565
566 mutex_unlock(&priv->hw_lock);
567}
568
569static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
570{
571 struct ark3116_private *priv = usb_get_serial_port_data(port);
572 unsigned long flags;
573
574 spin_lock_irqsave(&priv->status_lock, flags);
575 priv->msr = msr;
576 spin_unlock_irqrestore(&priv->status_lock, flags);
577
578 if (msr & UART_MSR_ANY_DELTA) {
579 /* update input line counters */
580 if (msr & UART_MSR_DCTS)
581 priv->icount.cts++;
582 if (msr & UART_MSR_DDSR)
583 priv->icount.dsr++;
584 if (msr & UART_MSR_DDCD)
585 priv->icount.dcd++;
586 if (msr & UART_MSR_TERI)
587 priv->icount.rng++;
588 wake_up_interruptible(&priv->delta_msr_wait);
422 } 589 }
590}
423 591
424 /* read register */ 592static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
425 ARK3116_RCV_QUIET(serial, 0xFE, 0xC0, 0x0000, 0x0006, buf); 593{
426 temp = buf[0]; 594 struct ark3116_private *priv = usb_get_serial_port_data(port);
427 kfree(buf); 595 unsigned long flags;
596
597 spin_lock_irqsave(&priv->status_lock, flags);
598 /* combine bits */
599 priv->lsr |= lsr;
600 spin_unlock_irqrestore(&priv->status_lock, flags);
601
602 if (lsr&UART_LSR_BRK_ERROR_BITS) {
603 if (lsr & UART_LSR_BI)
604 priv->icount.brk++;
605 if (lsr & UART_LSR_FE)
606 priv->icount.frame++;
607 if (lsr & UART_LSR_PE)
608 priv->icount.parity++;
609 if (lsr & UART_LSR_OE)
610 priv->icount.overrun++;
611 }
612}
428 613
429 /* i do not really know if bit4=CTS and bit6=DSR... just a 614static void ark3116_read_int_callback(struct urb *urb)
430 * quick guess! 615{
431 */ 616 struct usb_serial_port *port = urb->context;
432 return (temp & (1<<4) ? TIOCM_CTS : 0) 617 int status = urb->status;
433 | (temp & (1<<6) ? TIOCM_DSR : 0); 618 const __u8 *data = urb->transfer_buffer;
619 int result;
620
621 switch (status) {
622 case -ECONNRESET:
623 case -ENOENT:
624 case -ESHUTDOWN:
625 /* this urb is terminated, clean up */
626 dbg("%s - urb shutting down with status: %d",
627 __func__, status);
628 return;
629 default:
630 dbg("%s - nonzero urb status received: %d",
631 __func__, status);
632 break;
633 case 0: /* success */
634 /* discovered this by trail and error... */
635 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
636 const __u8 id = data[1]&UART_IIR_ID;
637 dbg("%s: iir=%02x", __func__, data[1]);
638 if (id == UART_IIR_MSI) {
639 dbg("%s: msr=%02x", __func__, data[3]);
640 ark3116_update_msr(port, data[3]);
641 break;
642 } else if (id == UART_IIR_RLSI) {
643 dbg("%s: lsr=%02x", __func__, data[2]);
644 ark3116_update_lsr(port, data[2]);
645 break;
646 }
647 }
648 /*
649 * Not sure what this data meant...
650 */
651 usb_serial_debug_data(debug, &port->dev,
652 __func__,
653 urb->actual_length,
654 urb->transfer_buffer);
655 break;
656 }
657
658 result = usb_submit_urb(urb, GFP_ATOMIC);
659 if (result)
660 dev_err(&urb->dev->dev,
661 "%s - Error %d submitting interrupt urb\n",
662 __func__, result);
663}
664
665
666/* Data comes in via the bulk (data) URB, erors/interrupts via the int URB.
667 * This means that we cannot be sure which data byte has an associated error
668 * condition, so we report an error for all data in the next bulk read.
669 *
670 * Actually, there might even be a window between the bulk data leaving the
671 * ark and reading/resetting the lsr in the read_bulk_callback where an
672 * interrupt for the next data block could come in.
673 * Without somekind of ordering on the ark, we would have to report the
674 * error for the next block of data as well...
675 * For now, let's pretend this can't happen.
676 */
677
678static void send_to_tty(struct tty_struct *tty,
679 const unsigned char *chars,
680 size_t size, char flag)
681{
682 if (size == 0)
683 return;
684 if (flag == TTY_NORMAL) {
685 tty_insert_flip_string(tty, chars, size);
686 } else {
687 int i;
688 for (i = 0; i < size; ++i)
689 tty_insert_flip_char(tty, chars[i], flag);
690 }
691}
692
693static void ark3116_read_bulk_callback(struct urb *urb)
694{
695 struct usb_serial_port *port = urb->context;
696 struct ark3116_private *priv = usb_get_serial_port_data(port);
697 const __u8 *data = urb->transfer_buffer;
698 int status = urb->status;
699 struct tty_struct *tty;
700 unsigned long flags;
701 int result;
702 char flag;
703 __u32 lsr;
704
705 switch (status) {
706 case -ECONNRESET:
707 case -ENOENT:
708 case -ESHUTDOWN:
709 /* this urb is terminated, clean up */
710 dbg("%s - urb shutting down with status: %d",
711 __func__, status);
712 return;
713 default:
714 dbg("%s - nonzero urb status received: %d",
715 __func__, status);
716 break;
717 case 0: /* success */
718
719 spin_lock_irqsave(&priv->status_lock, flags);
720 lsr = priv->lsr;
721 /* clear error bits */
722 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
723 spin_unlock_irqrestore(&priv->status_lock, flags);
724
725 if (unlikely(lsr & UART_LSR_BI))
726 flag = TTY_BREAK;
727 else if (unlikely(lsr & UART_LSR_PE))
728 flag = TTY_PARITY;
729 else if (unlikely(lsr & UART_LSR_FE))
730 flag = TTY_FRAME;
731 else
732 flag = TTY_NORMAL;
733
734 tty = tty_port_tty_get(&port->port);
735 if (tty) {
736 tty_buffer_request_room(tty, urb->actual_length + 1);
737 /* overrun is special, not associated with a char */
738 if (unlikely(lsr & UART_LSR_OE))
739 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
740 send_to_tty(tty, data, urb->actual_length, flag);
741 tty_flip_buffer_push(tty);
742 tty_kref_put(tty);
743 }
744
745 /* Throttle the device if requested by tty */
746 spin_lock_irqsave(&port->lock, flags);
747 port->throttled = port->throttle_req;
748 if (port->throttled) {
749 spin_unlock_irqrestore(&port->lock, flags);
750 return;
751 } else
752 spin_unlock_irqrestore(&port->lock, flags);
753 }
754 /* Continue reading from device */
755 result = usb_submit_urb(urb, GFP_ATOMIC);
756 if (result)
757 dev_err(&urb->dev->dev, "%s - failed resubmitting"
758 " read urb, error %d\n", __func__, result);
434} 759}
435 760
436static struct usb_driver ark3116_driver = { 761static struct usb_driver ark3116_driver = {
@@ -450,11 +775,17 @@ static struct usb_serial_driver ark3116_device = {
450 .usb_driver = &ark3116_driver, 775 .usb_driver = &ark3116_driver,
451 .num_ports = 1, 776 .num_ports = 1,
452 .attach = ark3116_attach, 777 .attach = ark3116_attach,
778 .release = ark3116_release,
453 .set_termios = ark3116_set_termios, 779 .set_termios = ark3116_set_termios,
454 .init_termios = ark3116_init_termios, 780 .init_termios = ark3116_init_termios,
455 .ioctl = ark3116_ioctl, 781 .ioctl = ark3116_ioctl,
456 .tiocmget = ark3116_tiocmget, 782 .tiocmget = ark3116_tiocmget,
783 .tiocmset = ark3116_tiocmset,
457 .open = ark3116_open, 784 .open = ark3116_open,
785 .close = ark3116_close,
786 .break_ctl = ark3116_break_ctl,
787 .read_int_callback = ark3116_read_int_callback,
788 .read_bulk_callback = ark3116_read_bulk_callback,
458}; 789};
459 790
460static int __init ark3116_init(void) 791static int __init ark3116_init(void)
@@ -465,7 +796,12 @@ static int __init ark3116_init(void)
465 if (retval) 796 if (retval)
466 return retval; 797 return retval;
467 retval = usb_register(&ark3116_driver); 798 retval = usb_register(&ark3116_driver);
468 if (retval) 799 if (retval == 0) {
800 printk(KERN_INFO "%s:"
801 DRIVER_VERSION ":"
802 DRIVER_DESC "\n",
803 KBUILD_MODNAME);
804 } else
469 usb_serial_deregister(&ark3116_device); 805 usb_serial_deregister(&ark3116_device);
470 return retval; 806 return retval;
471} 807}
@@ -480,6 +816,109 @@ module_init(ark3116_init);
480module_exit(ark3116_exit); 816module_exit(ark3116_exit);
481MODULE_LICENSE("GPL"); 817MODULE_LICENSE("GPL");
482 818
819MODULE_AUTHOR(DRIVER_AUTHOR);
820MODULE_DESCRIPTION(DRIVER_DESC);
821
483module_param(debug, bool, S_IRUGO | S_IWUSR); 822module_param(debug, bool, S_IRUGO | S_IWUSR);
484MODULE_PARM_DESC(debug, "Debug enabled or not"); 823MODULE_PARM_DESC(debug, "Enable debug");
485 824
825/*
826 * The following describes what I learned from studying the old
827 * ark3116.c driver, disassembling the windows driver, and some lucky
828 * guesses. Since I do not have any datasheet or other
829 * documentation, inaccuracies are almost guaranteed.
830 *
831 * Some specs for the ARK3116 can be found here:
832 * http://web.archive.org/web/20060318000438/
833 * www.arkmicro.com/en/products/view.php?id=10
834 * On that page, 2 GPIO pins are mentioned: I assume these are the
835 * OUT1 and OUT2 pins of the UART, so I added support for those
836 * through the MCR. Since the pins are not available on my hardware,
837 * I could not verify this.
838 * Also, it states there is "on-chip hardware flow control". I have
839 * discovered how to enable that. Unfortunately, I do not know how to
840 * enable XON/XOFF (software) flow control, which would need support
841 * from the chip as well to work. Because of the wording on the web
842 * page there is a real possibility the chip simply does not support
843 * software flow control.
844 *
845 * I got my ark3116 as part of a mobile phone adapter cable. On the
846 * PCB, the following numbered contacts are present:
847 *
848 * 1:- +5V
849 * 2:o DTR
850 * 3:i RX
851 * 4:i DCD
852 * 5:o RTS
853 * 6:o TX
854 * 7:i RI
855 * 8:i DSR
856 * 10:- 0V
857 * 11:i CTS
858 *
859 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
860 * may be different for the one you have ;-).
861 *
862 * The windows driver limits the registers to 0-F, so I assume there
863 * are actually 16 present on the device.
864 *
865 * On an UART interrupt, 4 bytes of data come in on the interrupt
866 * endpoint. The bytes are 0xe8 IIR LSR MSR.
867 *
868 * The baudrate seems to be generated from the 12MHz crystal, using
869 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
870 * of register E.
871 *
872 * Registers 0-7:
873 * These seem to be the same as for a regular 16450. The FCR is set
874 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
875 * the UART and the USB bridge/DMA engine.
876 *
877 * Register 8:
878 * By trial and error, I found out that bit 0 enables hardware CTS,
879 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
880 * RTS +5V when the 3116 cannot transfer the data to the USB bus
881 * (verified by disabling the reading URB). Note that as far as I can
882 * tell, the windows driver does NOT use this, so there might be some
883 * hardware bug or something.
884 *
885 * According to a patch provided here
886 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
887 * as an IrDA dongle. Since I do not have such a thing, I could not
888 * investigate that aspect. However, I can speculate ;-).
889 *
890 * - IrDA encodes data differently than RS232. Most likely, one of
891 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
892 * - Depending on the IR transceiver, the input and output need to be
893 * inverted, so there are probably bits for that as well.
894 * - IrDA is half-duplex, so there should be a bit for selecting that.
895 *
896 * This still leaves at least two registers unaccounted for. Perhaps
897 * The chip can do XON/XOFF or CRC in HW?
898 *
899 * Register 9:
900 * Set to 0x00 for IrDA, when the baudrate is initialised.
901 *
902 * Register A:
903 * Set to 0x01 for IrDA, at init.
904 *
905 * Register B:
906 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
907 *
908 * Register C:
909 * Set to 00 for IrDA, at init.
910 *
911 * Register D:
912 * Set to 0x41 for IrDA, at init.
913 *
914 * Register E:
915 * Somekind of baudrate override. The windows driver seems to set
916 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
917 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
918 * it could be somekind of subdivisor thingy.
919 * However,it does not seem to do anything: selecting 921600 (divisor 3,
920 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
921 * work, but they don't.
922 *
923 * Register F: unknown
924 */
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index ebcc6d0e2e91..f99498fca99a 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -598,6 +598,20 @@ static struct usb_device_id id_table_combined [] = {
598 { USB_DEVICE(BANDB_VID, BANDB_USOTL4_PID) }, 598 { USB_DEVICE(BANDB_VID, BANDB_USOTL4_PID) },
599 { USB_DEVICE(BANDB_VID, BANDB_USTL4_PID) }, 599 { USB_DEVICE(BANDB_VID, BANDB_USTL4_PID) },
600 { USB_DEVICE(BANDB_VID, BANDB_USO9ML2_PID) }, 600 { USB_DEVICE(BANDB_VID, BANDB_USO9ML2_PID) },
601 { USB_DEVICE(BANDB_VID, BANDB_USOPTL4_PID) },
602 { USB_DEVICE(BANDB_VID, BANDB_USPTL4_PID) },
603 { USB_DEVICE(BANDB_VID, BANDB_USO9ML2DR_2_PID) },
604 { USB_DEVICE(BANDB_VID, BANDB_USO9ML2DR_PID) },
605 { USB_DEVICE(BANDB_VID, BANDB_USOPTL4DR2_PID) },
606 { USB_DEVICE(BANDB_VID, BANDB_USOPTL4DR_PID) },
607 { USB_DEVICE(BANDB_VID, BANDB_485USB9F_2W_PID) },
608 { USB_DEVICE(BANDB_VID, BANDB_485USB9F_4W_PID) },
609 { USB_DEVICE(BANDB_VID, BANDB_232USB9M_PID) },
610 { USB_DEVICE(BANDB_VID, BANDB_485USBTB_2W_PID) },
611 { USB_DEVICE(BANDB_VID, BANDB_485USBTB_4W_PID) },
612 { USB_DEVICE(BANDB_VID, BANDB_TTL5USB9M_PID) },
613 { USB_DEVICE(BANDB_VID, BANDB_TTL3USB9M_PID) },
614 { USB_DEVICE(BANDB_VID, BANDB_ZZ_PROG1_USB_PID) },
601 { USB_DEVICE(FTDI_VID, EVER_ECO_PRO_CDS) }, 615 { USB_DEVICE(FTDI_VID, EVER_ECO_PRO_CDS) },
602 { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_1_PID) }, 616 { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_1_PID) },
603 { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_2_PID) }, 617 { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_2_PID) },
@@ -2195,15 +2209,21 @@ static void ftdi_set_termios(struct tty_struct *tty,
2195 2209
2196 /* Set number of data bits, parity, stop bits */ 2210 /* Set number of data bits, parity, stop bits */
2197 2211
2198 termios->c_cflag &= ~CMSPAR;
2199
2200 urb_value = 0; 2212 urb_value = 0;
2201 urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 : 2213 urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 :
2202 FTDI_SIO_SET_DATA_STOP_BITS_1); 2214 FTDI_SIO_SET_DATA_STOP_BITS_1);
2203 urb_value |= (cflag & PARENB ? 2215 if (cflag & PARENB) {
2204 (cflag & PARODD ? FTDI_SIO_SET_DATA_PARITY_ODD : 2216 if (cflag & CMSPAR)
2205 FTDI_SIO_SET_DATA_PARITY_EVEN) : 2217 urb_value |= cflag & PARODD ?
2206 FTDI_SIO_SET_DATA_PARITY_NONE); 2218 FTDI_SIO_SET_DATA_PARITY_MARK :
2219 FTDI_SIO_SET_DATA_PARITY_SPACE;
2220 else
2221 urb_value |= cflag & PARODD ?
2222 FTDI_SIO_SET_DATA_PARITY_ODD :
2223 FTDI_SIO_SET_DATA_PARITY_EVEN;
2224 } else {
2225 urb_value |= FTDI_SIO_SET_DATA_PARITY_NONE;
2226 }
2207 if (cflag & CSIZE) { 2227 if (cflag & CSIZE) {
2208 switch (cflag & CSIZE) { 2228 switch (cflag & CSIZE) {
2209 case CS5: urb_value |= 5; dbg("Setting CS5"); break; 2229 case CS5: urb_value |= 5; dbg("Setting CS5"); break;
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 6f31e0d71898..4586a24fafb0 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -662,6 +662,20 @@
662#define BANDB_USOTL4_PID 0xAC01 /* USOTL4 Isolated RS-485 Converter */ 662#define BANDB_USOTL4_PID 0xAC01 /* USOTL4 Isolated RS-485 Converter */
663#define BANDB_USTL4_PID 0xAC02 /* USTL4 RS-485 Converter */ 663#define BANDB_USTL4_PID 0xAC02 /* USTL4 RS-485 Converter */
664#define BANDB_USO9ML2_PID 0xAC03 /* USO9ML2 Isolated RS-232 Converter */ 664#define BANDB_USO9ML2_PID 0xAC03 /* USO9ML2 Isolated RS-232 Converter */
665#define BANDB_USOPTL4_PID 0xAC11
666#define BANDB_USPTL4_PID 0xAC12
667#define BANDB_USO9ML2DR_2_PID 0xAC16
668#define BANDB_USO9ML2DR_PID 0xAC17
669#define BANDB_USOPTL4DR2_PID 0xAC18 /* USOPTL4R-2 2-port Isolated RS-232 Converter */
670#define BANDB_USOPTL4DR_PID 0xAC19
671#define BANDB_485USB9F_2W_PID 0xAC25
672#define BANDB_485USB9F_4W_PID 0xAC26
673#define BANDB_232USB9M_PID 0xAC27
674#define BANDB_485USBTB_2W_PID 0xAC33
675#define BANDB_485USBTB_4W_PID 0xAC34
676#define BANDB_TTL5USB9M_PID 0xAC49
677#define BANDB_TTL3USB9M_PID 0xAC50
678#define BANDB_ZZ_PROG1_USB_PID 0xBA02
665 679
666/* 680/*
667 * RM Michaelides CANview USB (http://www.rmcan.com) 681 * RM Michaelides CANview USB (http://www.rmcan.com)
diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index f11abf52be7d..485fa9c5b107 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -121,8 +121,14 @@
121 * moschip_id_table_combined 121 * moschip_id_table_combined
122 */ 122 */
123#define USB_VENDOR_ID_BANDB 0x0856 123#define USB_VENDOR_ID_BANDB 0x0856
124#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 124#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
125#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
126#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
125#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 130#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
126 132
127/* This driver also supports 133/* This driver also supports
128 * ATEN UC2324 device using Moschip MCS7840 134 * ATEN UC2324 device using Moschip MCS7840
@@ -177,8 +183,14 @@
177static struct usb_device_id moschip_port_id_table[] = { 183static struct usb_device_id moschip_port_id_table[] = {
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
181 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
182 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 194 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
183 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 195 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
184 {} /* terminating entry */ 196 {} /* terminating entry */
@@ -187,8 +199,14 @@ static struct usb_device_id moschip_port_id_table[] = {
187static __devinitdata struct usb_device_id moschip_id_table_combined[] = { 199static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
188 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 200 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 201 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
192 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
193 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 211 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
194 {} /* terminating entry */ 212 {} /* terminating entry */
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index 0577e4b61114..9a2b903492ec 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -580,12 +580,48 @@ static struct usb_device_id option_ids[] = {
580 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0086, 0xff, 0xff, 0xff) }, 580 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0086, 0xff, 0xff, 0xff) },
581 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x2002, 0xff, 0xff, 0xff) }, 581 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x2002, 0xff, 0xff, 0xff) },
582 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x2003, 0xff, 0xff, 0xff) }, 582 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x2003, 0xff, 0xff, 0xff) },
583 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0104, 0xff, 0xff, 0xff) },
584 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0106, 0xff, 0xff, 0xff) },
585 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0108, 0xff, 0xff, 0xff) },
586 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0113, 0xff, 0xff, 0xff) },
587 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0117, 0xff, 0xff, 0xff) },
588 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0118, 0xff, 0xff, 0xff) },
589 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0121, 0xff, 0xff, 0xff) },
590 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0122, 0xff, 0xff, 0xff) },
591 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0123, 0xff, 0xff, 0xff) },
592 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0124, 0xff, 0xff, 0xff) },
593 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0125, 0xff, 0xff, 0xff) },
594 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0126, 0xff, 0xff, 0xff) },
595 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0128, 0xff, 0xff, 0xff) },
596 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0142, 0xff, 0xff, 0xff) },
597 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0143, 0xff, 0xff, 0xff) },
598 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0144, 0xff, 0xff, 0xff) },
599 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0145, 0xff, 0xff, 0xff) },
600 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0146, 0xff, 0xff, 0xff) },
601 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0147, 0xff, 0xff, 0xff) },
602 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0148, 0xff, 0xff, 0xff) },
603 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0149, 0xff, 0xff, 0xff) },
604 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0150, 0xff, 0xff, 0xff) },
605 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0151, 0xff, 0xff, 0xff) },
606 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0152, 0xff, 0xff, 0xff) },
607 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0153, 0xff, 0xff, 0xff) },
608 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0154, 0xff, 0xff, 0xff) },
609 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0155, 0xff, 0xff, 0xff) },
610 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0156, 0xff, 0xff, 0xff) },
611 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0157, 0xff, 0xff, 0xff) },
612 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0158, 0xff, 0xff, 0xff) },
613 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0159, 0xff, 0xff, 0xff) },
614 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0160, 0xff, 0xff, 0xff) },
615 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0161, 0xff, 0xff, 0xff) },
616 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0162, 0xff, 0xff, 0xff) },
583 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0014, 0xff, 0xff, 0xff) }, /* ZTE CDMA products */ 617 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0014, 0xff, 0xff, 0xff) }, /* ZTE CDMA products */
584 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0027, 0xff, 0xff, 0xff) }, 618 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0027, 0xff, 0xff, 0xff) },
585 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0059, 0xff, 0xff, 0xff) }, 619 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0059, 0xff, 0xff, 0xff) },
586 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0060, 0xff, 0xff, 0xff) }, 620 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0060, 0xff, 0xff, 0xff) },
587 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0070, 0xff, 0xff, 0xff) }, 621 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0070, 0xff, 0xff, 0xff) },
588 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0073, 0xff, 0xff, 0xff) }, 622 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0073, 0xff, 0xff, 0xff) },
623 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0130, 0xff, 0xff, 0xff) },
624 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, 0x0141, 0xff, 0xff, 0xff) },
589 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_CDMA_TECH, 0xff, 0xff, 0xff) }, 625 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_CDMA_TECH, 0xff, 0xff, 0xff) },
590 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710, 0xff, 0xff, 0xff) }, 626 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC8710, 0xff, 0xff, 0xff) },
591 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC2726, 0xff, 0xff, 0xff) }, 627 { USB_DEVICE_AND_INTERFACE_INFO(ZTE_VENDOR_ID, ZTE_PRODUCT_AC2726, 0xff, 0xff, 0xff) },
@@ -599,6 +635,7 @@ static struct usb_device_id option_ids[] = {
599 { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_G450) }, 635 { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_G450) },
600 { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_HSDPA_MINICARD ) }, /* Toshiba 3G HSDPA == Novatel Expedite EU870D MiniCard */ 636 { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_HSDPA_MINICARD ) }, /* Toshiba 3G HSDPA == Novatel Expedite EU870D MiniCard */
601 { USB_DEVICE(ALINK_VENDOR_ID, 0x9000) }, 637 { USB_DEVICE(ALINK_VENDOR_ID, 0x9000) },
638 { USB_DEVICE(ALINK_VENDOR_ID, 0xce16) },
602 { USB_DEVICE_AND_INTERFACE_INFO(ALINK_VENDOR_ID, ALINK_PRODUCT_3GU, 0xff, 0xff, 0xff) }, 639 { USB_DEVICE_AND_INTERFACE_INFO(ALINK_VENDOR_ID, ALINK_PRODUCT_3GU, 0xff, 0xff, 0xff) },
603 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_X060S) }, 640 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_X060S) },
604 { USB_DEVICE(AIRPLUS_VENDOR_ID, AIRPLUS_PRODUCT_MCD650) }, 641 { USB_DEVICE(AIRPLUS_VENDOR_ID, AIRPLUS_PRODUCT_MCD650) },
@@ -1312,7 +1349,7 @@ static int option_suspend(struct usb_serial *serial, pm_message_t message)
1312 1349
1313 dbg("%s entered", __func__); 1350 dbg("%s entered", __func__);
1314 1351
1315 if (serial->dev->auto_pm) { 1352 if (message.event & PM_EVENT_AUTO) {
1316 spin_lock_irq(&intfdata->susp_lock); 1353 spin_lock_irq(&intfdata->susp_lock);
1317 b = intfdata->in_flight; 1354 b = intfdata->in_flight;
1318 spin_unlock_irq(&intfdata->susp_lock); 1355 spin_unlock_irq(&intfdata->susp_lock);
diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c
index 5019325ba25d..ac1b6449fb6a 100644
--- a/drivers/usb/serial/sierra.c
+++ b/drivers/usb/serial/sierra.c
@@ -16,8 +16,9 @@
16 Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de> 16 Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
17 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org> 17 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
18*/ 18*/
19 19/* Uncomment to log function calls */
20#define DRIVER_VERSION "v.1.3.8" 20/* #define DEBUG */
21#define DRIVER_VERSION "v.1.7.16"
21#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer" 22#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
22#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems" 23#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
23 24
@@ -33,8 +34,10 @@
33#define SWIMS_USB_REQUEST_SetPower 0x00 34#define SWIMS_USB_REQUEST_SetPower 0x00
34#define SWIMS_USB_REQUEST_SetNmea 0x07 35#define SWIMS_USB_REQUEST_SetNmea 0x07
35 36
36#define N_IN_URB 8 37#define N_IN_URB_HM 8
37#define N_OUT_URB 64 38#define N_OUT_URB_HM 64
39#define N_IN_URB 4
40#define N_OUT_URB 4
38#define IN_BUFLEN 4096 41#define IN_BUFLEN 4096
39 42
40#define MAX_TRANSFER (PAGE_SIZE - 512) 43#define MAX_TRANSFER (PAGE_SIZE - 512)
@@ -124,6 +127,23 @@ static int is_blacklisted(const u8 ifnum,
124 return 0; 127 return 0;
125} 128}
126 129
130static int is_himemory(const u8 ifnum,
131 const struct sierra_iface_info *himemorylist)
132{
133 const u8 *info;
134 int i;
135
136 if (himemorylist) {
137 info = himemorylist->ifaceinfo;
138
139 for (i=0; i < himemorylist->infolen; i++) {
140 if (info[i] == ifnum)
141 return 1;
142 }
143 }
144 return 0;
145}
146
127static int sierra_calc_interface(struct usb_serial *serial) 147static int sierra_calc_interface(struct usb_serial *serial)
128{ 148{
129 int interface; 149 int interface;
@@ -186,6 +206,20 @@ static int sierra_probe(struct usb_serial *serial,
186 return result; 206 return result;
187} 207}
188 208
209/* interfaces with higher memory requirements */
210static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
211static const struct sierra_iface_info typeA_interface_list = {
212 .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
213 .ifaceinfo = hi_memory_typeA_ifaces,
214};
215
216static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
217static const struct sierra_iface_info typeB_interface_list = {
218 .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
219 .ifaceinfo = hi_memory_typeB_ifaces,
220};
221
222/* 'blacklist' of interfaces not served by this driver */
189static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 }; 223static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
190static const struct sierra_iface_info direct_ip_interface_blacklist = { 224static const struct sierra_iface_info direct_ip_interface_blacklist = {
191 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces), 225 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
@@ -286,8 +320,10 @@ struct sierra_port_private {
286 struct usb_anchor active; 320 struct usb_anchor active;
287 struct usb_anchor delayed; 321 struct usb_anchor delayed;
288 322
323 int num_out_urbs;
324 int num_in_urbs;
289 /* Input endpoints and buffers for this port */ 325 /* Input endpoints and buffers for this port */
290 struct urb *in_urbs[N_IN_URB]; 326 struct urb *in_urbs[N_IN_URB_HM];
291 327
292 /* Settings for the port */ 328 /* Settings for the port */
293 int rts_state; /* Handshaking pins (outputs) */ 329 int rts_state; /* Handshaking pins (outputs) */
@@ -460,7 +496,7 @@ static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
460 spin_lock_irqsave(&portdata->lock, flags); 496 spin_lock_irqsave(&portdata->lock, flags);
461 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__, 497 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
462 portdata->outstanding_urbs); 498 portdata->outstanding_urbs);
463 if (portdata->outstanding_urbs > N_OUT_URB) { 499 if (portdata->outstanding_urbs > portdata->num_out_urbs) {
464 spin_unlock_irqrestore(&portdata->lock, flags); 500 spin_unlock_irqrestore(&portdata->lock, flags);
465 dev_dbg(&port->dev, "%s - write limit hit\n", __func__); 501 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
466 return 0; 502 return 0;
@@ -665,7 +701,7 @@ static int sierra_write_room(struct tty_struct *tty)
665 /* try to give a good number back based on if we have any free urbs at 701 /* try to give a good number back based on if we have any free urbs at
666 * this point in time */ 702 * this point in time */
667 spin_lock_irqsave(&portdata->lock, flags); 703 spin_lock_irqsave(&portdata->lock, flags);
668 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) { 704 if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
669 spin_unlock_irqrestore(&portdata->lock, flags); 705 spin_unlock_irqrestore(&portdata->lock, flags);
670 dev_dbg(&port->dev, "%s - write limit hit\n", __func__); 706 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
671 return 0; 707 return 0;
@@ -680,7 +716,7 @@ static void sierra_stop_rx_urbs(struct usb_serial_port *port)
680 int i; 716 int i;
681 struct sierra_port_private *portdata = usb_get_serial_port_data(port); 717 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
682 718
683 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) 719 for (i = 0; i < portdata->num_in_urbs; i++)
684 usb_kill_urb(portdata->in_urbs[i]); 720 usb_kill_urb(portdata->in_urbs[i]);
685 721
686 usb_kill_urb(port->interrupt_in_urb); 722 usb_kill_urb(port->interrupt_in_urb);
@@ -695,7 +731,7 @@ static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
695 struct sierra_port_private *portdata = usb_get_serial_port_data(port); 731 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
696 732
697 ok_cnt = 0; 733 ok_cnt = 0;
698 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) { 734 for (i = 0; i < portdata->num_in_urbs; i++) {
699 urb = portdata->in_urbs[i]; 735 urb = portdata->in_urbs[i];
700 if (!urb) 736 if (!urb)
701 continue; 737 continue;
@@ -791,7 +827,7 @@ static void sierra_close(struct usb_serial_port *port)
791 /* Stop reading urbs */ 827 /* Stop reading urbs */
792 sierra_stop_rx_urbs(port); 828 sierra_stop_rx_urbs(port);
793 /* .. and release them */ 829 /* .. and release them */
794 for (i = 0; i < N_IN_URB; i++) { 830 for (i = 0; i < portdata->num_in_urbs; i++) {
795 sierra_release_urb(portdata->in_urbs[i]); 831 sierra_release_urb(portdata->in_urbs[i]);
796 portdata->in_urbs[i] = NULL; 832 portdata->in_urbs[i] = NULL;
797 } 833 }
@@ -818,7 +854,7 @@ static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
818 854
819 855
820 endpoint = port->bulk_in_endpointAddress; 856 endpoint = port->bulk_in_endpointAddress;
821 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) { 857 for (i = 0; i < portdata->num_in_urbs; i++) {
822 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port, 858 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
823 IN_BUFLEN, GFP_KERNEL, 859 IN_BUFLEN, GFP_KERNEL,
824 sierra_indat_callback); 860 sierra_indat_callback);
@@ -869,7 +905,9 @@ static int sierra_startup(struct usb_serial *serial)
869{ 905{
870 struct usb_serial_port *port; 906 struct usb_serial_port *port;
871 struct sierra_port_private *portdata; 907 struct sierra_port_private *portdata;
908 struct sierra_iface_info *himemoryp = NULL;
872 int i; 909 int i;
910 u8 ifnum;
873 911
874 dev_dbg(&serial->dev->dev, "%s\n", __func__); 912 dev_dbg(&serial->dev->dev, "%s\n", __func__);
875 913
@@ -886,13 +924,40 @@ static int sierra_startup(struct usb_serial *serial)
886 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 924 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
887 if (!portdata) { 925 if (!portdata) {
888 dev_dbg(&port->dev, "%s: kmalloc for " 926 dev_dbg(&port->dev, "%s: kmalloc for "
889 "sierra_port_private (%d) failed!.\n", 927 "sierra_port_private (%d) failed!\n",
890 __func__, i); 928 __func__, i);
891 return -ENOMEM; 929 return -ENOMEM;
892 } 930 }
893 spin_lock_init(&portdata->lock); 931 spin_lock_init(&portdata->lock);
894 init_usb_anchor(&portdata->active); 932 init_usb_anchor(&portdata->active);
895 init_usb_anchor(&portdata->delayed); 933 init_usb_anchor(&portdata->delayed);
934 ifnum = i;
935 /* Assume low memory requirements */
936 portdata->num_out_urbs = N_OUT_URB;
937 portdata->num_in_urbs = N_IN_URB;
938
939 /* Determine actual memory requirements */
940 if (serial->num_ports == 1) {
941 /* Get interface number for composite device */
942 ifnum = sierra_calc_interface(serial);
943 himemoryp =
944 (struct sierra_iface_info *)&typeB_interface_list;
945 if (is_himemory(ifnum, himemoryp)) {
946 portdata->num_out_urbs = N_OUT_URB_HM;
947 portdata->num_in_urbs = N_IN_URB_HM;
948 }
949 }
950 else {
951 himemoryp =
952 (struct sierra_iface_info *)&typeA_interface_list;
953 if (is_himemory(i, himemoryp)) {
954 portdata->num_out_urbs = N_OUT_URB_HM;
955 portdata->num_in_urbs = N_IN_URB_HM;
956 }
957 }
958 dev_dbg(&serial->dev->dev,
959 "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
960 ifnum,portdata->num_in_urbs, portdata->num_out_urbs );
896 /* Set the port private data pointer */ 961 /* Set the port private data pointer */
897 usb_set_serial_port_data(port, portdata); 962 usb_set_serial_port_data(port, portdata);
898 } 963 }
@@ -940,7 +1005,7 @@ static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
940 struct sierra_intf_private *intfdata; 1005 struct sierra_intf_private *intfdata;
941 int b; 1006 int b;
942 1007
943 if (serial->dev->auto_pm) { 1008 if (message.event & PM_EVENT_AUTO) {
944 intfdata = serial->private; 1009 intfdata = serial->private;
945 spin_lock_irq(&intfdata->susp_lock); 1010 spin_lock_irq(&intfdata->susp_lock);
946 b = intfdata->in_flight; 1011 b = intfdata->in_flight;
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index cfa26d56ce60..e5e6df39e737 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -73,7 +73,8 @@
73 73
74static const char* host_info(struct Scsi_Host *host) 74static const char* host_info(struct Scsi_Host *host)
75{ 75{
76 return "SCSI emulation for USB Mass Storage devices"; 76 struct us_data *us = host_to_us(host);
77 return us->scsi_name;
77} 78}
78 79
79static int slave_alloc (struct scsi_device *sdev) 80static int slave_alloc (struct scsi_device *sdev)
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 589f6b4404f0..cc313d16d727 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -666,10 +666,11 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
666 * to wait for at least one CHECK_CONDITION to determine 666 * to wait for at least one CHECK_CONDITION to determine
667 * SANE_SENSE support 667 * SANE_SENSE support
668 */ 668 */
669 if ((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) && 669 if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
670 result == USB_STOR_TRANSPORT_GOOD && 670 result == USB_STOR_TRANSPORT_GOOD &&
671 !(us->fflags & US_FL_SANE_SENSE) && 671 !(us->fflags & US_FL_SANE_SENSE) &&
672 !(srb->cmnd[2] & 0x20)) { 672 !(us->fflags & US_FL_BAD_SENSE) &&
673 !(srb->cmnd[2] & 0x20))) {
673 US_DEBUGP("-- SAT supported, increasing auto-sense\n"); 674 US_DEBUGP("-- SAT supported, increasing auto-sense\n");
674 us->fflags |= US_FL_SANE_SENSE; 675 us->fflags |= US_FL_SANE_SENSE;
675 } 676 }
@@ -718,6 +719,12 @@ Retry_Sense:
718 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { 719 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
719 US_DEBUGP("-- auto-sense aborted\n"); 720 US_DEBUGP("-- auto-sense aborted\n");
720 srb->result = DID_ABORT << 16; 721 srb->result = DID_ABORT << 16;
722
723 /* If SANE_SENSE caused this problem, disable it */
724 if (sense_size != US_SENSE_SIZE) {
725 us->fflags &= ~US_FL_SANE_SENSE;
726 us->fflags |= US_FL_BAD_SENSE;
727 }
721 goto Handle_Errors; 728 goto Handle_Errors;
722 } 729 }
723 730
@@ -727,10 +734,11 @@ Retry_Sense:
727 * (small) sense request. This fixes some USB GSM modems 734 * (small) sense request. This fixes some USB GSM modems
728 */ 735 */
729 if (temp_result == USB_STOR_TRANSPORT_FAILED && 736 if (temp_result == USB_STOR_TRANSPORT_FAILED &&
730 (us->fflags & US_FL_SANE_SENSE) && 737 sense_size != US_SENSE_SIZE) {
731 sense_size != US_SENSE_SIZE) {
732 US_DEBUGP("-- auto-sense failure, retry small sense\n"); 738 US_DEBUGP("-- auto-sense failure, retry small sense\n");
733 sense_size = US_SENSE_SIZE; 739 sense_size = US_SENSE_SIZE;
740 us->fflags &= ~US_FL_SANE_SENSE;
741 us->fflags |= US_FL_BAD_SENSE;
734 goto Retry_Sense; 742 goto Retry_Sense;
735 } 743 }
736 744
@@ -754,6 +762,7 @@ Retry_Sense:
754 */ 762 */
755 if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) && 763 if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
756 !(us->fflags & US_FL_SANE_SENSE) && 764 !(us->fflags & US_FL_SANE_SENSE) &&
765 !(us->fflags & US_FL_BAD_SENSE) &&
757 (srb->sense_buffer[0] & 0x7C) == 0x70) { 766 (srb->sense_buffer[0] & 0x7C) == 0x70) {
758 US_DEBUGP("-- SANE_SENSE support enabled\n"); 767 US_DEBUGP("-- SANE_SENSE support enabled\n");
759 us->fflags |= US_FL_SANE_SENSE; 768 us->fflags |= US_FL_SANE_SENSE;
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index d4f034ebaa8a..64a0a2c27e12 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -818,6 +818,13 @@ UNUSUAL_DEV( 0x066f, 0x8000, 0x0001, 0x0001,
818 US_SC_DEVICE, US_PR_DEVICE, NULL, 818 US_SC_DEVICE, US_PR_DEVICE, NULL,
819 US_FL_FIX_CAPACITY ), 819 US_FL_FIX_CAPACITY ),
820 820
821/* Reported by Daniel Kukula <daniel.kuku@gmail.com> */
822UNUSUAL_DEV( 0x067b, 0x1063, 0x0100, 0x0100,
823 "Prolific Technology, Inc.",
824 "Prolific Storage Gadget",
825 US_SC_DEVICE, US_PR_DEVICE, NULL,
826 US_FL_BAD_SENSE ),
827
821/* Reported by Rogerio Brito <rbrito@ime.usp.br> */ 828/* Reported by Rogerio Brito <rbrito@ime.usp.br> */
822UNUSUAL_DEV( 0x067b, 0x2317, 0x0001, 0x001, 829UNUSUAL_DEV( 0x067b, 0x2317, 0x0001, 0x001,
823 "Prolific Technology, Inc.", 830 "Prolific Technology, Inc.",
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 8060b85fe1a3..5a53d4f0dd11 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -45,6 +45,10 @@
45 * 675 Mass Ave, Cambridge, MA 02139, USA. 45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */ 46 */
47 47
48#ifdef CONFIG_USB_STORAGE_DEBUG
49#define DEBUG
50#endif
51
48#include <linux/sched.h> 52#include <linux/sched.h>
49#include <linux/errno.h> 53#include <linux/errno.h>
50#include <linux/freezer.h> 54#include <linux/freezer.h>
@@ -228,6 +232,7 @@ void fill_inquiry_response(struct us_data *us, unsigned char *data,
228 if (data_len<36) // You lose. 232 if (data_len<36) // You lose.
229 return; 233 return;
230 234
235 memset(data+8, ' ', 28);
231 if(data[0]&0x20) { /* USB device currently not connected. Return 236 if(data[0]&0x20) { /* USB device currently not connected. Return
232 peripheral qualifier 001b ("...however, the 237 peripheral qualifier 001b ("...however, the
233 physical device is not currently connected 238 physical device is not currently connected
@@ -237,15 +242,15 @@ void fill_inquiry_response(struct us_data *us, unsigned char *data,
237 device, it may return zeros or ASCII spaces 242 device, it may return zeros or ASCII spaces
238 (20h) in those fields until the data is 243 (20h) in those fields until the data is
239 available from the device."). */ 244 available from the device."). */
240 memset(data+8,0,28);
241 } else { 245 } else {
242 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice); 246 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
243 memcpy(data+8, us->unusual_dev->vendorName, 247 int n;
244 strlen(us->unusual_dev->vendorName) > 8 ? 8 : 248
245 strlen(us->unusual_dev->vendorName)); 249 n = strlen(us->unusual_dev->vendorName);
246 memcpy(data+16, us->unusual_dev->productName, 250 memcpy(data+8, us->unusual_dev->vendorName, min(8, n));
247 strlen(us->unusual_dev->productName) > 16 ? 16 : 251 n = strlen(us->unusual_dev->productName);
248 strlen(us->unusual_dev->productName)); 252 memcpy(data+16, us->unusual_dev->productName, min(16, n));
253
249 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F); 254 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
250 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F); 255 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
251 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F); 256 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
@@ -459,6 +464,9 @@ static void adjust_quirks(struct us_data *us)
459 case 'a': 464 case 'a':
460 f |= US_FL_SANE_SENSE; 465 f |= US_FL_SANE_SENSE;
461 break; 466 break;
467 case 'b':
468 f |= US_FL_BAD_SENSE;
469 break;
462 case 'c': 470 case 'c':
463 f |= US_FL_FIX_CAPACITY; 471 f |= US_FL_FIX_CAPACITY;
464 break; 472 break;
@@ -808,14 +816,13 @@ static int usb_stor_scan_thread(void * __us)
808{ 816{
809 struct us_data *us = (struct us_data *)__us; 817 struct us_data *us = (struct us_data *)__us;
810 818
811 printk(KERN_DEBUG 819 dev_dbg(&us->pusb_intf->dev, "device found\n");
812 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
813 820
814 set_freezable(); 821 set_freezable();
815 /* Wait for the timeout to expire or for a disconnect */ 822 /* Wait for the timeout to expire or for a disconnect */
816 if (delay_use > 0) { 823 if (delay_use > 0) {
817 printk(KERN_DEBUG "usb-storage: waiting for device " 824 dev_dbg(&us->pusb_intf->dev, "waiting for device to settle "
818 "to settle before scanning\n"); 825 "before scanning\n");
819 wait_event_freezable_timeout(us->delay_wait, 826 wait_event_freezable_timeout(us->delay_wait,
820 test_bit(US_FLIDX_DONT_SCAN, &us->dflags), 827 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
821 delay_use * HZ); 828 delay_use * HZ);
@@ -832,7 +839,7 @@ static int usb_stor_scan_thread(void * __us)
832 mutex_unlock(&us->dev_mutex); 839 mutex_unlock(&us->dev_mutex);
833 } 840 }
834 scsi_scan_host(us_to_host(us)); 841 scsi_scan_host(us_to_host(us));
835 printk(KERN_DEBUG "usb-storage: device scan complete\n"); 842 dev_dbg(&us->pusb_intf->dev, "scan complete\n");
836 843
837 /* Should we unbind if no devices were detected? */ 844 /* Should we unbind if no devices were detected? */
838 } 845 }
@@ -840,6 +847,15 @@ static int usb_stor_scan_thread(void * __us)
840 complete_and_exit(&us->scanning_done, 0); 847 complete_and_exit(&us->scanning_done, 0);
841} 848}
842 849
850static unsigned int usb_stor_sg_tablesize(struct usb_interface *intf)
851{
852 struct usb_device *usb_dev = interface_to_usbdev(intf);
853
854 if (usb_dev->bus->sg_tablesize) {
855 return usb_dev->bus->sg_tablesize;
856 }
857 return SG_ALL;
858}
843 859
844/* First part of general USB mass-storage probing */ 860/* First part of general USB mass-storage probing */
845int usb_stor_probe1(struct us_data **pus, 861int usb_stor_probe1(struct us_data **pus,
@@ -868,6 +884,7 @@ int usb_stor_probe1(struct us_data **pus,
868 * Allow 16-byte CDBs and thus > 2TB 884 * Allow 16-byte CDBs and thus > 2TB
869 */ 885 */
870 host->max_cmd_len = 16; 886 host->max_cmd_len = 16;
887 host->sg_tablesize = usb_stor_sg_tablesize(intf);
871 *pus = us = host_to_us(host); 888 *pus = us = host_to_us(host);
872 memset(us, 0, sizeof(struct us_data)); 889 memset(us, 0, sizeof(struct us_data));
873 mutex_init(&(us->dev_mutex)); 890 mutex_init(&(us->dev_mutex));
@@ -929,6 +946,8 @@ int usb_stor_probe2(struct us_data *us)
929 result = usb_stor_acquire_resources(us); 946 result = usb_stor_acquire_resources(us);
930 if (result) 947 if (result)
931 goto BadDevice; 948 goto BadDevice;
949 snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
950 dev_name(&us->pusb_intf->dev));
932 result = scsi_add_host(us_to_host(us), &us->pusb_intf->dev); 951 result = scsi_add_host(us_to_host(us), &us->pusb_intf->dev);
933 if (result) { 952 if (result) {
934 printk(KERN_WARNING USB_STORAGE 953 printk(KERN_WARNING USB_STORAGE
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 2609efb2bd7e..69717134231b 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -132,6 +132,7 @@ struct us_data {
132 /* SCSI interfaces */ 132 /* SCSI interfaces */
133 struct scsi_cmnd *srb; /* current srb */ 133 struct scsi_cmnd *srb; /* current srb */
134 unsigned int tag; /* current dCBWTag */ 134 unsigned int tag; /* current dCBWTag */
135 char scsi_name[32]; /* scsi_host name */
135 136
136 /* control and bulk communications data */ 137 /* control and bulk communications data */
137 struct urb *current_urb; /* USB requests */ 138 struct urb *current_urb; /* USB requests */
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index b62f2bc064f6..b1e579c5c97c 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -358,7 +358,7 @@ retry:
358 rv = skel_do_read_io(dev, count); 358 rv = skel_do_read_io(dev, count);
359 if (rv < 0) 359 if (rv < 0)
360 goto exit; 360 goto exit;
361 else if (!file->f_flags & O_NONBLOCK) 361 else if (!(file->f_flags & O_NONBLOCK))
362 goto retry; 362 goto retry;
363 rv = -EAGAIN; 363 rv = -EAGAIN;
364 } 364 }
@@ -411,7 +411,7 @@ static ssize_t skel_write(struct file *file, const char *user_buffer,
411 * limit the number of URBs in flight to stop a user from using up all 411 * limit the number of URBs in flight to stop a user from using up all
412 * RAM 412 * RAM
413 */ 413 */
414 if (!file->f_flags & O_NONBLOCK) { 414 if (!(file->f_flags & O_NONBLOCK)) {
415 if (down_interruptible(&dev->limit_sem)) { 415 if (down_interruptible(&dev->limit_sem)) {
416 retval = -ERESTARTSYS; 416 retval = -ERESTARTSYS;
417 goto exit; 417 goto exit;
diff --git a/drivers/usb/wusbcore/devconnect.c b/drivers/usb/wusbcore/devconnect.c
index 4ac4300a3f9a..dced419f7aba 100644
--- a/drivers/usb/wusbcore/devconnect.c
+++ b/drivers/usb/wusbcore/devconnect.c
@@ -119,10 +119,12 @@ static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
119 urb = usb_alloc_urb(0, GFP_KERNEL); 119 urb = usb_alloc_urb(0, GFP_KERNEL);
120 if (urb == NULL) 120 if (urb == NULL)
121 goto err; 121 goto err;
122 wusb_dev->set_gtk_urb = urb;
122 123
123 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 124 req = kmalloc(sizeof(*req), GFP_KERNEL);
124 if (req == NULL) 125 if (req == NULL)
125 goto err; 126 goto err;
127 wusb_dev->set_gtk_req = req;
126 128
127 req->bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE; 129 req->bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
128 req->bRequest = USB_REQ_SET_DESCRIPTOR; 130 req->bRequest = USB_REQ_SET_DESCRIPTOR;
@@ -130,9 +132,6 @@ static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
130 req->wIndex = 0; 132 req->wIndex = 0;
131 req->wLength = cpu_to_le16(wusbhc->gtk.descr.bLength); 133 req->wLength = cpu_to_le16(wusbhc->gtk.descr.bLength);
132 134
133 wusb_dev->set_gtk_urb = urb;
134 wusb_dev->set_gtk_req = req;
135
136 return wusb_dev; 135 return wusb_dev;
137err: 136err:
138 wusb_dev_free(wusb_dev); 137 wusb_dev_free(wusb_dev);
diff --git a/drivers/usb/wusbcore/security.c b/drivers/usb/wusbcore/security.c
index 4516c36436e6..edcd2d756037 100644
--- a/drivers/usb/wusbcore/security.c
+++ b/drivers/usb/wusbcore/security.c
@@ -205,15 +205,15 @@ int wusb_dev_sec_add(struct wusbhc *wusbhc,
205 const void *itr, *top; 205 const void *itr, *top;
206 char buf[64]; 206 char buf[64];
207 207
208 secd = kmalloc(sizeof(struct usb_security_descriptor), GFP_KERNEL); 208 secd = kmalloc(sizeof(*secd), GFP_KERNEL);
209 if (secd == NULL) { 209 if (secd == NULL) {
210 result = -ENOMEM; 210 result = -ENOMEM;
211 goto out; 211 goto out;
212 } 212 }
213 213
214 result = usb_get_descriptor(usb_dev, USB_DT_SECURITY, 214 result = usb_get_descriptor(usb_dev, USB_DT_SECURITY,
215 0, secd, sizeof(struct usb_security_descriptor)); 215 0, secd, sizeof(*secd));
216 if (result < sizeof(secd)) { 216 if (result < sizeof(*secd)) {
217 dev_err(dev, "Can't read security descriptor or " 217 dev_err(dev, "Can't read security descriptor or "
218 "not enough data: %d\n", result); 218 "not enough data: %d\n", result);
219 goto out; 219 goto out;
diff --git a/drivers/usb/wusbcore/wusbhc.c b/drivers/usb/wusbcore/wusbhc.c
index ee6256f23636..eab86e4bc770 100644
--- a/drivers/usb/wusbcore/wusbhc.c
+++ b/drivers/usb/wusbcore/wusbhc.c
@@ -147,10 +147,40 @@ static ssize_t wusb_chid_store(struct device *dev,
147} 147}
148static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store); 148static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store);
149 149
150
151static ssize_t wusb_phy_rate_show(struct device *dev,
152 struct device_attribute *attr,
153 char *buf)
154{
155 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
156
157 return sprintf(buf, "%d\n", wusbhc->phy_rate);
158}
159
160static ssize_t wusb_phy_rate_store(struct device *dev,
161 struct device_attribute *attr,
162 const char *buf, size_t size)
163{
164 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
165 uint8_t phy_rate;
166 ssize_t result;
167
168 result = sscanf(buf, "%hhu", &phy_rate);
169 if (result != 1)
170 return -EINVAL;
171 if (phy_rate >= UWB_PHY_RATE_INVALID)
172 return -EINVAL;
173
174 wusbhc->phy_rate = phy_rate;
175 return size;
176}
177static DEVICE_ATTR(wusb_phy_rate, 0644, wusb_phy_rate_show, wusb_phy_rate_store);
178
150/* Group all the WUSBHC attributes */ 179/* Group all the WUSBHC attributes */
151static struct attribute *wusbhc_attrs[] = { 180static struct attribute *wusbhc_attrs[] = {
152 &dev_attr_wusb_trust_timeout.attr, 181 &dev_attr_wusb_trust_timeout.attr,
153 &dev_attr_wusb_chid.attr, 182 &dev_attr_wusb_chid.attr,
183 &dev_attr_wusb_phy_rate.attr,
154 NULL, 184 NULL,
155}; 185};
156 186
@@ -177,6 +207,8 @@ int wusbhc_create(struct wusbhc *wusbhc)
177 int result = 0; 207 int result = 0;
178 208
179 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS; 209 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
210 wusbhc->phy_rate = UWB_PHY_RATE_INVALID - 1;
211
180 mutex_init(&wusbhc->mutex); 212 mutex_init(&wusbhc->mutex);
181 result = wusbhc_mmcie_create(wusbhc); 213 result = wusbhc_mmcie_create(wusbhc);
182 if (result < 0) 214 if (result < 0)
diff --git a/drivers/usb/wusbcore/wusbhc.h b/drivers/usb/wusbcore/wusbhc.h
index 797c2453a35b..fd2fd4e277e1 100644
--- a/drivers/usb/wusbcore/wusbhc.h
+++ b/drivers/usb/wusbcore/wusbhc.h
@@ -253,6 +253,7 @@ struct wusbhc {
253 253
254 unsigned trust_timeout; /* in jiffies */ 254 unsigned trust_timeout; /* in jiffies */
255 struct wusb_ckhdid chid; 255 struct wusb_ckhdid chid;
256 uint8_t phy_rate;
256 struct wuie_host_info *wuie_host_info; 257 struct wuie_host_info *wuie_host_info;
257 258
258 struct mutex mutex; /* locks everything else */ 259 struct mutex mutex; /* locks everything else */