aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig70
-rw-r--r--drivers/usb/gadget/Makefile6
-rw-r--r--drivers/usb/gadget/at91_udc.c25
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c10
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.h1
-rw-r--r--drivers/usb/gadget/audio.c116
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.c1
-rw-r--r--drivers/usb/gadget/composite.c59
-rw-r--r--drivers/usb/gadget/config.c1
-rw-r--r--drivers/usb/gadget/epautoconf.c24
-rw-r--r--drivers/usb/gadget/ether.c14
-rw-r--r--drivers/usb/gadget/f_acm.c39
-rw-r--r--drivers/usb/gadget/f_audio.c98
-rw-r--r--drivers/usb/gadget/f_ecm.c8
-rw-r--r--drivers/usb/gadget/f_eem.c4
-rw-r--r--drivers/usb/gadget/f_loopback.c1
-rw-r--r--drivers/usb/gadget/f_mass_storage.c3098
-rw-r--r--drivers/usb/gadget/f_obex.c1
-rw-r--r--drivers/usb/gadget/f_phonet.c1
-rw-r--r--drivers/usb/gadget/f_rndis.c40
-rw-r--r--drivers/usb/gadget/f_serial.c1
-rw-r--r--drivers/usb/gadget/f_sourcesink.c1
-rw-r--r--drivers/usb/gadget/f_subset.c1
-rw-r--r--drivers/usb/gadget/file_storage.c891
-rw-r--r--drivers/usb/gadget/fsl_mx3_udc.c31
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.c2
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.h15
-rw-r--r--drivers/usb/gadget/gadget_chips.h67
-rw-r--r--drivers/usb/gadget/gmidi.c8
-rw-r--r--drivers/usb/gadget/goku_udc.c4
-rw-r--r--drivers/usb/gadget/imx_udc.c1
-rw-r--r--drivers/usb/gadget/inode.c39
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.c1
-rw-r--r--drivers/usb/gadget/m66592-udc.c1
-rw-r--r--drivers/usb/gadget/mass_storage.c246
-rw-r--r--drivers/usb/gadget/multi.c356
-rw-r--r--drivers/usb/gadget/nokia.c259
-rw-r--r--drivers/usb/gadget/omap_udc.c25
-rw-r--r--drivers/usb/gadget/printer.c18
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c4
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c138
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h6
-rw-r--r--drivers/usb/gadget/r8a66597-udc.c2
-rw-r--r--drivers/usb/gadget/rndis.c1
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c22
-rw-r--r--drivers/usb/gadget/storage_common.c778
-rw-r--r--drivers/usb/gadget/u_audio.c13
-rw-r--r--drivers/usb/gadget/u_audio.h2
-rw-r--r--drivers/usb/gadget/u_ether.c6
-rw-r--r--drivers/usb/gadget/u_ether.h9
-rw-r--r--drivers/usb/gadget/u_serial.c1
-rw-r--r--drivers/usb/gadget/zero.c7
52 files changed, 5411 insertions, 1162 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index a18e3c5dd82e..11a3e0fa4331 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_mass_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,58 @@ 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_NOKIA
816 tristate "Nokia composite gadget"
817 depends on PHONET
818 help
819 The Nokia composite gadget provides support for acm, obex
820 and phonet in only one composite gadget driver.
821
822 It's only really useful for N900 hardware. If you're building
823 a kernel for N900, say Y or M here. If unsure, say N.
824
825config USB_G_MULTI
826 tristate "Multifunction Composite Gadget (EXPERIMENTAL)"
827 depends on BLOCK && NET
828 help
829 The Multifunction Composite Gadget provides Ethernet (RNDIS
830 and/or CDC Ethernet), mass storage and ACM serial link
831 interfaces.
832
833 You will be asked to choose which of the two configurations is
834 to be available in the gadget. At least one configuration must
835 be chosen to make the gadget usable. Selecting more than one
836 configuration will prevent Windows from automatically detecting
837 the gadget as a composite gadget, so an INF file will be needed to
838 use the gadget.
839
840 Say "y" to link the driver statically, or "m" to build a
841 dynamically linked module called "g_multi".
842
843config USB_G_MULTI_RNDIS
844 bool "RNDIS + CDC Serial + Storage configuration"
845 depends on USB_G_MULTI
846 default y
847 help
848 This option enables a configuration with RNDIS, CDC Serial and
849 Mass Storage functions available in the Multifunction Composite
850 Gadget. This is the configuration dedicated for Windows since RNDIS
851 is Microsoft's protocol.
852
853 If unsure, say "y".
854
855config USB_G_MULTI_CDC
856 bool "CDC Ethernet + CDC Serial + Storage configuration"
857 depends on USB_G_MULTI
858 default n
859 help
860 This option enables a configuration with CDC Ethernet (ECM), CDC
861 Serial and Mass Storage functions available in the Multifunction
862 Composite Gadget.
863
864 If unsure, say "y".
865
866
797# put drivers that need isochronous transfer support (for audio 867# put drivers that need isochronous transfer support (for audio
798# or video class gadget drivers), or specific hardware, here. 868# or video class gadget drivers), or specific hardware, here.
799 869
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 9d7b87c52e9f..43b51da8d727 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -39,16 +39,22 @@ 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
46g_nokia-objs := nokia.o
44 47
45obj-$(CONFIG_USB_ZERO) += g_zero.o 48obj-$(CONFIG_USB_ZERO) += g_zero.o
46obj-$(CONFIG_USB_AUDIO) += g_audio.o 49obj-$(CONFIG_USB_AUDIO) += g_audio.o
47obj-$(CONFIG_USB_ETH) += g_ether.o 50obj-$(CONFIG_USB_ETH) += g_ether.o
48obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 51obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
49obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 52obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
53obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o
50obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 54obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
51obj-$(CONFIG_USB_G_PRINTER) += g_printer.o 55obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
52obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o 56obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
53obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o 57obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
58obj-$(CONFIG_USB_G_MULTI) += g_multi.o
59obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
54 60
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 66450a1abc22..df1bae9b048e 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);
@@ -1370,6 +1370,12 @@ static irqreturn_t at91_udc_irq (int irq, void *_udc)
1370{ 1370{
1371 struct at91_udc *udc = _udc; 1371 struct at91_udc *udc = _udc;
1372 u32 rescans = 5; 1372 u32 rescans = 5;
1373 int disable_clock = 0;
1374
1375 if (!udc->clocked) {
1376 clk_on(udc);
1377 disable_clock = 1;
1378 }
1373 1379
1374 while (rescans--) { 1380 while (rescans--) {
1375 u32 status; 1381 u32 status;
@@ -1458,6 +1464,9 @@ static irqreturn_t at91_udc_irq (int irq, void *_udc)
1458 } 1464 }
1459 } 1465 }
1460 1466
1467 if (disable_clock)
1468 clk_off(udc);
1469
1461 return IRQ_HANDLED; 1470 return IRQ_HANDLED;
1462} 1471}
1463 1472
@@ -1656,9 +1665,7 @@ static int __init at91udc_probe(struct platform_device *pdev)
1656 if (!res) 1665 if (!res)
1657 return -ENXIO; 1666 return -ENXIO;
1658 1667
1659 if (!request_mem_region(res->start, 1668 if (!request_mem_region(res->start, resource_size(res), driver_name)) {
1660 res->end - res->start + 1,
1661 driver_name)) {
1662 DBG("someone's using UDC memory\n"); 1669 DBG("someone's using UDC memory\n");
1663 return -EBUSY; 1670 return -EBUSY;
1664 } 1671 }
@@ -1692,14 +1699,14 @@ static int __init at91udc_probe(struct platform_device *pdev)
1692 udc->ep[3].maxpacket = 64; 1699 udc->ep[3].maxpacket = 64;
1693 udc->ep[4].maxpacket = 512; 1700 udc->ep[4].maxpacket = 512;
1694 udc->ep[5].maxpacket = 512; 1701 udc->ep[5].maxpacket = 512;
1695 } else if (cpu_is_at91sam9261()) { 1702 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
1696 udc->ep[3].maxpacket = 64; 1703 udc->ep[3].maxpacket = 64;
1697 } else if (cpu_is_at91sam9263()) { 1704 } else if (cpu_is_at91sam9263()) {
1698 udc->ep[0].maxpacket = 64; 1705 udc->ep[0].maxpacket = 64;
1699 udc->ep[3].maxpacket = 64; 1706 udc->ep[3].maxpacket = 64;
1700 } 1707 }
1701 1708
1702 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1); 1709 udc->udp_baseaddr = ioremap(res->start, resource_size(res));
1703 if (!udc->udp_baseaddr) { 1710 if (!udc->udp_baseaddr) {
1704 retval = -ENOMEM; 1711 retval = -ENOMEM;
1705 goto fail0a; 1712 goto fail0a;
@@ -1781,7 +1788,7 @@ fail0a:
1781 if (cpu_is_at91rm9200()) 1788 if (cpu_is_at91rm9200())
1782 gpio_free(udc->board.pullup_pin); 1789 gpio_free(udc->board.pullup_pin);
1783fail0: 1790fail0:
1784 release_mem_region(res->start, res->end - res->start + 1); 1791 release_mem_region(res->start, resource_size(res));
1785 DBG("%s probe failed, %d\n", driver_name, retval); 1792 DBG("%s probe failed, %d\n", driver_name, retval);
1786 return retval; 1793 return retval;
1787} 1794}
@@ -1813,7 +1820,7 @@ static int __exit at91udc_remove(struct platform_device *pdev)
1813 gpio_free(udc->board.pullup_pin); 1820 gpio_free(udc->board.pullup_pin);
1814 1821
1815 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1822 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1816 release_mem_region(res->start, res->end - res->start + 1); 1823 release_mem_region(res->start, resource_size(res));
1817 1824
1818 clk_put(udc->iclk); 1825 clk_put(udc->iclk);
1819 clk_put(udc->fclk); 1826 clk_put(udc->fclk);
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 4e970cf0e29a..75a256f3d45b 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -12,6 +12,7 @@
12#include <linux/init.h> 12#include <linux/init.h>
13#include <linux/interrupt.h> 13#include <linux/interrupt.h>
14#include <linux/io.h> 14#include <linux/io.h>
15#include <linux/slab.h>
15#include <linux/device.h> 16#include <linux/device.h>
16#include <linux/dma-mapping.h> 17#include <linux/dma-mapping.h>
17#include <linux/list.h> 18#include <linux/list.h>
@@ -320,7 +321,7 @@ static inline void usba_cleanup_debugfs(struct usba_udc *udc)
320static int vbus_is_present(struct usba_udc *udc) 321static int vbus_is_present(struct usba_udc *udc)
321{ 322{
322 if (gpio_is_valid(udc->vbus_pin)) 323 if (gpio_is_valid(udc->vbus_pin))
323 return gpio_get_value(udc->vbus_pin); 324 return gpio_get_value(udc->vbus_pin) ^ udc->vbus_pin_inverted;
324 325
325 /* No Vbus detection: Assume always present */ 326 /* No Vbus detection: Assume always present */
326 return 1; 327 return 1;
@@ -1763,7 +1764,7 @@ static irqreturn_t usba_vbus_irq(int irq, void *devid)
1763 if (!udc->driver) 1764 if (!udc->driver)
1764 goto out; 1765 goto out;
1765 1766
1766 vbus = gpio_get_value(udc->vbus_pin); 1767 vbus = vbus_is_present(udc);
1767 if (vbus != udc->vbus_prev) { 1768 if (vbus != udc->vbus_prev) {
1768 if (vbus) { 1769 if (vbus) {
1769 toggle_bias(1); 1770 toggle_bias(1);
@@ -1914,14 +1915,14 @@ static int __init usba_udc_probe(struct platform_device *pdev)
1914 udc->vbus_pin = -ENODEV; 1915 udc->vbus_pin = -ENODEV;
1915 1916
1916 ret = -ENOMEM; 1917 ret = -ENOMEM;
1917 udc->regs = ioremap(regs->start, regs->end - regs->start + 1); 1918 udc->regs = ioremap(regs->start, resource_size(regs));
1918 if (!udc->regs) { 1919 if (!udc->regs) {
1919 dev_err(&pdev->dev, "Unable to map I/O memory, aborting.\n"); 1920 dev_err(&pdev->dev, "Unable to map I/O memory, aborting.\n");
1920 goto err_map_regs; 1921 goto err_map_regs;
1921 } 1922 }
1922 dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p\n", 1923 dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p\n",
1923 (unsigned long)regs->start, udc->regs); 1924 (unsigned long)regs->start, udc->regs);
1924 udc->fifo = ioremap(fifo->start, fifo->end - fifo->start + 1); 1925 udc->fifo = ioremap(fifo->start, resource_size(fifo));
1925 if (!udc->fifo) { 1926 if (!udc->fifo) {
1926 dev_err(&pdev->dev, "Unable to map FIFO, aborting.\n"); 1927 dev_err(&pdev->dev, "Unable to map FIFO, aborting.\n");
1927 goto err_map_fifo; 1928 goto err_map_fifo;
@@ -2000,6 +2001,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
2000 if (gpio_is_valid(pdata->vbus_pin)) { 2001 if (gpio_is_valid(pdata->vbus_pin)) {
2001 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) { 2002 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) {
2002 udc->vbus_pin = pdata->vbus_pin; 2003 udc->vbus_pin = pdata->vbus_pin;
2004 udc->vbus_pin_inverted = pdata->vbus_pin_inverted;
2003 2005
2004 ret = request_irq(gpio_to_irq(udc->vbus_pin), 2006 ret = request_irq(gpio_to_irq(udc->vbus_pin),
2005 usba_vbus_irq, 0, 2007 usba_vbus_irq, 0,
diff --git a/drivers/usb/gadget/atmel_usba_udc.h b/drivers/usb/gadget/atmel_usba_udc.h
index f7baea307f0d..88a2e07a11a8 100644
--- a/drivers/usb/gadget/atmel_usba_udc.h
+++ b/drivers/usb/gadget/atmel_usba_udc.h
@@ -323,6 +323,7 @@ struct usba_udc {
323 struct platform_device *pdev; 323 struct platform_device *pdev;
324 int irq; 324 int irq;
325 int vbus_pin; 325 int vbus_pin;
326 int vbus_pin_inverted;
326 struct clk *pclk; 327 struct clk *pclk;
327 struct clk *hclk; 328 struct clk *hclk;
328 329
diff --git a/drivers/usb/gadget/audio.c b/drivers/usb/gadget/audio.c
index a3a0f4a27ef0..a62af7b59094 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,
@@ -273,6 +158,7 @@ fail:
273 158
274static int __exit audio_unbind(struct usb_composite_dev *cdev) 159static int __exit audio_unbind(struct usb_composite_dev *cdev)
275{ 160{
161 gaudio_cleanup();
276 return 0; 162 return 0;
277} 163}
278 164
diff --git a/drivers/usb/gadget/ci13xxx_udc.c b/drivers/usb/gadget/ci13xxx_udc.c
index c7cb87a6fee2..699695128e33 100644
--- a/drivers/usb/gadget/ci13xxx_udc.c
+++ b/drivers/usb/gadget/ci13xxx_udc.c
@@ -62,6 +62,7 @@
62#include <linux/kernel.h> 62#include <linux/kernel.h>
63#include <linux/module.h> 63#include <linux/module.h>
64#include <linux/pci.h> 64#include <linux/pci.h>
65#include <linux/slab.h>
65#include <linux/usb/ch9.h> 66#include <linux/usb/ch9.h>
66#include <linux/usb/gadget.h> 67#include <linux/usb/gadget.h>
67 68
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/config.c b/drivers/usb/gadget/config.c
index e1191b9a316a..47e8e722682c 100644
--- a/drivers/usb/gadget/config.c
+++ b/drivers/usb/gadget/config.c
@@ -19,6 +19,7 @@
19 */ 19 */
20 20
21#include <linux/errno.h> 21#include <linux/errno.h>
22#include <linux/slab.h>
22#include <linux/kernel.h> 23#include <linux/kernel.h>
23#include <linux/list.h> 24#include <linux/list.h>
24#include <linux/string.h> 25#include <linux/string.h>
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c
index cd0914ec898e..3568de210f79 100644
--- a/drivers/usb/gadget/epautoconf.c
+++ b/drivers/usb/gadget/epautoconf.c
@@ -265,16 +265,24 @@ struct usb_ep * __init usb_ep_autoconfig (
265 return ep; 265 return ep;
266 } 266 }
267 267
268 } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) { 268#ifdef CONFIG_BLACKFIN
269 /* single buffering is enough; maybe 8 byte fifo is too */ 269 } else if (gadget_is_musbhdrc(gadget)) {
270 ep = find_ep (gadget, "ep3in-bulk"); 270 if ((USB_ENDPOINT_XFER_BULK == type) ||
271 if (ep && ep_matches (gadget, ep, desc)) 271 (USB_ENDPOINT_XFER_ISOC == type)) {
272 return ep; 272 if (USB_DIR_IN & desc->bEndpointAddress)
273 273 ep = find_ep (gadget, "ep5in");
274 } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) { 274 else
275 ep = find_ep (gadget, "ep1-bulk"); 275 ep = find_ep (gadget, "ep6out");
276 } else if (USB_ENDPOINT_XFER_INT == type) {
277 if (USB_DIR_IN & desc->bEndpointAddress)
278 ep = find_ep(gadget, "ep1in");
279 else
280 ep = find_ep(gadget, "ep2out");
281 } else
282 ep = NULL;
276 if (ep && ep_matches (gadget, ep, desc)) 283 if (ep && ep_matches (gadget, ep, desc))
277 return ep; 284 return ep;
285#endif
278 } 286 }
279 287
280 /* Second, look at endpoints until an unclaimed one looks usable */ 288 /* Second, look at endpoints until an unclaimed one looks usable */
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 167cb2a8ecef..400f80372d93 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
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index 7953948bfe4a..400e1ebe6976 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,
@@ -12,6 +14,7 @@
12 14
13/* #define VERBOSE_DEBUG */ 15/* #define VERBOSE_DEBUG */
14 16
17#include <linux/slab.h>
15#include <linux/kernel.h> 18#include <linux/kernel.h>
16#include <linux/device.h> 19#include <linux/device.h>
17 20
@@ -99,6 +102,20 @@ static inline struct f_acm *port_to_acm(struct gserial *p)
99 102
100/* interface and class descriptors: */ 103/* interface and class descriptors: */
101 104
105static struct usb_interface_assoc_descriptor
106acm_iad_descriptor = {
107 .bLength = sizeof acm_iad_descriptor,
108 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
109
110 /* .bFirstInterface = DYNAMIC, */
111 .bInterfaceCount = 2, // control + data
112 .bFunctionClass = USB_CLASS_COMM,
113 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
114 .bFunctionProtocol = USB_CDC_PROTO_NONE,
115 /* .iFunction = DYNAMIC */
116};
117
118
102static struct usb_interface_descriptor acm_control_interface_desc __initdata = { 119static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
103 .bLength = USB_DT_INTERFACE_SIZE, 120 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE, 121 .bDescriptorType = USB_DT_INTERFACE,
@@ -178,6 +195,7 @@ static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
178}; 195};
179 196
180static struct usb_descriptor_header *acm_fs_function[] __initdata = { 197static struct usb_descriptor_header *acm_fs_function[] __initdata = {
198 (struct usb_descriptor_header *) &acm_iad_descriptor,
181 (struct usb_descriptor_header *) &acm_control_interface_desc, 199 (struct usb_descriptor_header *) &acm_control_interface_desc,
182 (struct usb_descriptor_header *) &acm_header_desc, 200 (struct usb_descriptor_header *) &acm_header_desc,
183 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 201 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
@@ -216,6 +234,7 @@ static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
216}; 234};
217 235
218static struct usb_descriptor_header *acm_hs_function[] __initdata = { 236static struct usb_descriptor_header *acm_hs_function[] __initdata = {
237 (struct usb_descriptor_header *) &acm_iad_descriptor,
219 (struct usb_descriptor_header *) &acm_control_interface_desc, 238 (struct usb_descriptor_header *) &acm_control_interface_desc,
220 (struct usb_descriptor_header *) &acm_header_desc, 239 (struct usb_descriptor_header *) &acm_header_desc,
221 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 240 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
@@ -232,11 +251,13 @@ static struct usb_descriptor_header *acm_hs_function[] __initdata = {
232 251
233#define ACM_CTRL_IDX 0 252#define ACM_CTRL_IDX 0
234#define ACM_DATA_IDX 1 253#define ACM_DATA_IDX 1
254#define ACM_IAD_IDX 2
235 255
236/* static strings, in UTF-8 */ 256/* static strings, in UTF-8 */
237static struct usb_string acm_string_defs[] = { 257static struct usb_string acm_string_defs[] = {
238 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)", 258 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
239 [ACM_DATA_IDX].s = "CDC ACM Data", 259 [ACM_DATA_IDX].s = "CDC ACM Data",
260 [ACM_IAD_IDX ].s = "CDC Serial",
240 { /* ZEROES END LIST */ }, 261 { /* ZEROES END LIST */ },
241}; 262};
242 263
@@ -432,7 +453,7 @@ static void acm_disable(struct usb_function *f)
432 * @length: size of data 453 * @length: size of data
433 * Context: irqs blocked, acm->lock held, acm_notify_req non-null 454 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
434 * 455 *
435 * Returns zero on sucess or a negative errno. 456 * Returns zero on success or a negative errno.
436 * 457 *
437 * See section 6.3.5 of the CDC 1.1 specification for information 458 * See section 6.3.5 of the CDC 1.1 specification for information
438 * about the only notification we issue: SerialState change. 459 * about the only notification we issue: SerialState change.
@@ -563,6 +584,7 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
563 if (status < 0) 584 if (status < 0)
564 goto fail; 585 goto fail;
565 acm->ctrl_id = status; 586 acm->ctrl_id = status;
587 acm_iad_descriptor.bFirstInterface = status;
566 588
567 acm_control_interface_desc.bInterfaceNumber = status; 589 acm_control_interface_desc.bInterfaceNumber = status;
568 acm_union_desc .bMasterInterface0 = status; 590 acm_union_desc .bMasterInterface0 = status;
@@ -681,14 +703,6 @@ acm_unbind(struct usb_configuration *c, struct usb_function *f)
681/* Some controllers can't support CDC ACM ... */ 703/* Some controllers can't support CDC ACM ... */
682static inline bool can_support_cdc(struct usb_configuration *c) 704static inline bool can_support_cdc(struct usb_configuration *c)
683{ 705{
684 /* SH3 doesn't support multiple interfaces */
685 if (gadget_is_sh(c->cdev->gadget))
686 return false;
687
688 /* sa1100 doesn't have a third interrupt endpoint */
689 if (gadget_is_sa1100(c->cdev->gadget))
690 return false;
691
692 /* everything else is *probably* fine ... */ 706 /* everything else is *probably* fine ... */
693 return true; 707 return true;
694} 708}
@@ -732,6 +746,13 @@ int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
732 acm_string_defs[ACM_DATA_IDX].id = status; 746 acm_string_defs[ACM_DATA_IDX].id = status;
733 747
734 acm_data_interface_desc.iInterface = status; 748 acm_data_interface_desc.iInterface = status;
749
750 status = usb_string_id(c->cdev);
751 if (status < 0)
752 return status;
753 acm_string_defs[ACM_IAD_IDX].id = status;
754
755 acm_iad_descriptor.iFunction = status;
735 } 756 }
736 757
737 /* allocate and initialize one new instance */ 758 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_audio.c b/drivers/usb/gadget/f_audio.c
index 98e9bb977291..43bf44514c41 100644
--- a/drivers/usb/gadget/f_audio.c
+++ b/drivers/usb/gadget/f_audio.c
@@ -9,6 +9,7 @@
9 * Licensed under the GPL-2 or later. 9 * Licensed under the GPL-2 or later.
10 */ 10 */
11 11
12#include <linux/slab.h>
12#include <linux/kernel.h> 13#include <linux/kernel.h>
13#include <linux/device.h> 14#include <linux/device.h>
14#include <asm/atomic.h> 15#include <asm/atomic.h>
@@ -56,13 +57,16 @@ static struct usb_interface_descriptor ac_interface_desc __initdata = {
56DECLARE_UAC_AC_HEADER_DESCRIPTOR(2); 57DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
57 58
58#define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) 59#define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
60/* 1 input terminal, 1 output terminal and 1 feature unit */
61#define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
62 + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
59/* B.3.2 Class-Specific AC Interface Descriptor */ 63/* B.3.2 Class-Specific AC Interface Descriptor */
60static struct uac_ac_header_descriptor_2 ac_header_desc = { 64static struct uac_ac_header_descriptor_v1_2 ac_header_desc = {
61 .bLength = UAC_DT_AC_HEADER_LENGTH, 65 .bLength = UAC_DT_AC_HEADER_LENGTH,
62 .bDescriptorType = USB_DT_CS_INTERFACE, 66 .bDescriptorType = USB_DT_CS_INTERFACE,
63 .bDescriptorSubtype = UAC_HEADER, 67 .bDescriptorSubtype = UAC_HEADER,
64 .bcdADC = __constant_cpu_to_le16(0x0100), 68 .bcdADC = __constant_cpu_to_le16(0x0100),
65 .wTotalLength = __constant_cpu_to_le16(UAC_DT_AC_HEADER_LENGTH), 69 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
66 .bInCollection = F_AUDIO_NUM_INTERFACES, 70 .bInCollection = F_AUDIO_NUM_INTERFACES,
67 .baInterfaceNr = { 71 .baInterfaceNr = {
68 [0] = F_AUDIO_AC_INTERFACE, 72 [0] = F_AUDIO_AC_INTERFACE,
@@ -121,7 +125,7 @@ static struct usb_audio_control_selector feature_unit = {
121}; 125};
122 126
123#define OUTPUT_TERMINAL_ID 3 127#define OUTPUT_TERMINAL_ID 3
124static struct uac_output_terminal_descriptor output_terminal_desc = { 128static struct uac_output_terminal_descriptor_v1 output_terminal_desc = {
125 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 129 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
126 .bDescriptorType = USB_DT_CS_INTERFACE, 130 .bDescriptorType = USB_DT_CS_INTERFACE,
127 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 131 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
@@ -151,7 +155,7 @@ static struct usb_interface_descriptor as_interface_alt_1_desc = {
151}; 155};
152 156
153/* B.4.2 Class-Specific AS Interface Descriptor */ 157/* B.4.2 Class-Specific AS Interface Descriptor */
154static struct uac_as_header_descriptor as_header_desc = { 158static struct uac_as_header_descriptor_v1 as_header_desc = {
155 .bLength = UAC_DT_AS_HEADER_SIZE, 159 .bLength = UAC_DT_AS_HEADER_SIZE,
156 .bDescriptorType = USB_DT_CS_INTERFACE, 160 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubtype = UAC_AS_GENERAL, 161 .bDescriptorSubtype = UAC_AS_GENERAL,
@@ -252,12 +256,12 @@ static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
252 256
253 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); 257 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
254 if (!copy_buf) 258 if (!copy_buf)
255 return (struct f_audio_buf *)-ENOMEM; 259 return ERR_PTR(-ENOMEM);
256 260
257 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); 261 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
258 if (!copy_buf->buf) { 262 if (!copy_buf->buf) {
259 kfree(copy_buf); 263 kfree(copy_buf);
260 return (struct f_audio_buf *)-ENOMEM; 264 return ERR_PTR(-ENOMEM);
261 } 265 }
262 266
263 return copy_buf; 267 return copy_buf;
@@ -332,7 +336,7 @@ static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
332 list_add_tail(&copy_buf->list, &audio->play_queue); 336 list_add_tail(&copy_buf->list, &audio->play_queue);
333 schedule_work(&audio->playback_work); 337 schedule_work(&audio->playback_work);
334 copy_buf = f_audio_buffer_alloc(audio_buf_size); 338 copy_buf = f_audio_buffer_alloc(audio_buf_size);
335 if (copy_buf < 0) 339 if (IS_ERR(copy_buf))
336 return -ENOMEM; 340 return -ENOMEM;
337 } 341 }
338 342
@@ -445,6 +449,70 @@ static int audio_get_intf_req(struct usb_function *f,
445 return len; 449 return len;
446} 450}
447 451
452static int audio_set_endpoint_req(struct usb_function *f,
453 const struct usb_ctrlrequest *ctrl)
454{
455 struct usb_composite_dev *cdev = f->config->cdev;
456 int value = -EOPNOTSUPP;
457 u16 ep = le16_to_cpu(ctrl->wIndex);
458 u16 len = le16_to_cpu(ctrl->wLength);
459 u16 w_value = le16_to_cpu(ctrl->wValue);
460
461 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
462 ctrl->bRequest, w_value, len, ep);
463
464 switch (ctrl->bRequest) {
465 case UAC_SET_CUR:
466 value = 0;
467 break;
468
469 case UAC_SET_MIN:
470 break;
471
472 case UAC_SET_MAX:
473 break;
474
475 case UAC_SET_RES:
476 break;
477
478 case UAC_SET_MEM:
479 break;
480
481 default:
482 break;
483 }
484
485 return value;
486}
487
488static int audio_get_endpoint_req(struct usb_function *f,
489 const struct usb_ctrlrequest *ctrl)
490{
491 struct usb_composite_dev *cdev = f->config->cdev;
492 int value = -EOPNOTSUPP;
493 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
494 u16 len = le16_to_cpu(ctrl->wLength);
495 u16 w_value = le16_to_cpu(ctrl->wValue);
496
497 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
498 ctrl->bRequest, w_value, len, ep);
499
500 switch (ctrl->bRequest) {
501 case UAC_GET_CUR:
502 case UAC_GET_MIN:
503 case UAC_GET_MAX:
504 case UAC_GET_RES:
505 value = 3;
506 break;
507 case UAC_GET_MEM:
508 break;
509 default:
510 break;
511 }
512
513 return value;
514}
515
448static int 516static int
449f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 517f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
450{ 518{
@@ -455,8 +523,8 @@ f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
455 u16 w_value = le16_to_cpu(ctrl->wValue); 523 u16 w_value = le16_to_cpu(ctrl->wValue);
456 u16 w_length = le16_to_cpu(ctrl->wLength); 524 u16 w_length = le16_to_cpu(ctrl->wLength);
457 525
458 /* composite driver infrastructure handles everything except 526 /* composite driver infrastructure handles everything; interface
459 * Audio class messages; interface activation uses set_alt(). 527 * activation uses set_alt().
460 */ 528 */
461 switch (ctrl->bRequestType) { 529 switch (ctrl->bRequestType) {
462 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 530 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
@@ -467,6 +535,14 @@ f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
467 value = audio_get_intf_req(f, ctrl); 535 value = audio_get_intf_req(f, ctrl);
468 break; 536 break;
469 537
538 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
539 value = audio_set_endpoint_req(f, ctrl);
540 break;
541
542 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
543 value = audio_get_endpoint_req(f, ctrl);
544 break;
545
470 default: 546 default:
471 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 547 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
472 ctrl->bRequestType, ctrl->bRequest, 548 ctrl->bRequestType, ctrl->bRequest,
@@ -504,6 +580,8 @@ static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
504 usb_ep_enable(out_ep, audio->out_desc); 580 usb_ep_enable(out_ep, audio->out_desc);
505 out_ep->driver_data = audio; 581 out_ep->driver_data = audio;
506 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); 582 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
583 if (IS_ERR(audio->copy_buf))
584 return -ENOMEM;
507 585
508 /* 586 /*
509 * allocate a bunch of read buffers 587 * allocate a bunch of read buffers
@@ -715,7 +793,7 @@ int __init audio_bind_config(struct usb_configuration *c)
715 return status; 793 return status;
716 794
717add_fail: 795add_fail:
718 gaudio_cleanup(&audio->card); 796 gaudio_cleanup();
719setup_fail: 797setup_fail:
720 kfree(audio); 798 kfree(audio);
721 return status; 799 return status;
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
index ecf5bdd0ae06..4e595324c614 100644
--- a/drivers/usb/gadget/f_ecm.c
+++ b/drivers/usb/gadget/f_ecm.c
@@ -21,6 +21,7 @@
21 21
22/* #define VERBOSE_DEBUG */ 22/* #define VERBOSE_DEBUG */
23 23
24#include <linux/slab.h>
24#include <linux/kernel.h> 25#include <linux/kernel.h>
25#include <linux/device.h> 26#include <linux/device.h>
26#include <linux/etherdevice.h> 27#include <linux/etherdevice.h>
@@ -497,12 +498,9 @@ static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
497 struct net_device *net; 498 struct net_device *net;
498 499
499 /* Enable zlps by default for ECM conformance; 500 /* Enable zlps by default for ECM conformance;
500 * override for musb_hdrc (avoids txdma ovhead) 501 * override for musb_hdrc (avoids txdma ovhead).
501 * and sa1100 (can't).
502 */ 502 */
503 ecm->port.is_zlp_ok = !( 503 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
504 gadget_is_sa1100(cdev->gadget)
505 || gadget_is_musbhdrc(cdev->gadget)
506 ); 504 );
507 ecm->port.cdc_filter = DEFAULT_FILTER; 505 ecm->port.cdc_filter = DEFAULT_FILTER;
508 DBG(cdev, "activate ecm\n"); 506 DBG(cdev, "activate ecm\n");
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
index 0a577d5694fd..38226e9a371d 100644
--- a/drivers/usb/gadget/f_eem.c
+++ b/drivers/usb/gadget/f_eem.c
@@ -24,6 +24,7 @@
24#include <linux/device.h> 24#include <linux/device.h>
25#include <linux/etherdevice.h> 25#include <linux/etherdevice.h>
26#include <linux/crc32.h> 26#include <linux/crc32.h>
27#include <linux/slab.h>
27 28
28#include "u_ether.h" 29#include "u_ether.h"
29 30
@@ -358,7 +359,7 @@ done:
358 * b15: bmType (0 == data) 359 * b15: bmType (0 == data)
359 */ 360 */
360 len = skb->len; 361 len = skb->len;
361 put_unaligned_le16((len & 0x3FFF) | BIT(14), skb_push(skb, 2)); 362 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
362 363
363 /* add a zero-length EEM packet, if needed */ 364 /* add a zero-length EEM packet, if needed */
364 if (padlen) 365 if (padlen)
@@ -464,7 +465,6 @@ static int eem_unwrap(struct gether *port,
464 } 465 }
465 466
466 /* validate CRC */ 467 /* validate CRC */
467 crc = get_unaligned_le32(skb->data + len - ETH_FCS_LEN);
468 if (header & BIT(14)) { 468 if (header & BIT(14)) {
469 crc = get_unaligned_le32(skb->data + len 469 crc = get_unaligned_le32(skb->data + len
470 - ETH_FCS_LEN); 470 - ETH_FCS_LEN);
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
index 6cb29d3df575..e91d1b16d9be 100644
--- a/drivers/usb/gadget/f_loopback.c
+++ b/drivers/usb/gadget/f_loopback.c
@@ -21,6 +21,7 @@
21 21
22/* #define VERBOSE_DEBUG */ 22/* #define VERBOSE_DEBUG */
23 23
24#include <linux/slab.h>
24#include <linux/kernel.h> 25#include <linux/kernel.h>
25#include <linux/device.h> 26#include <linux/device.h>
26 27
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
new file mode 100644
index 000000000000..f4911c09022e
--- /dev/null
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -0,0 +1,3098 @@
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 int (*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. If no
396 * callback is set or it returns value lower then zero MSF
397 * will force eject all LUNs it operates on (including those
398 * marked as non-removable or with prevent_medium_removal flag
399 * set). */
400 int (*thread_exits)(struct fsg_common *common);
401 /* Gadget's private data. */
402 void *private_data;
403
404 const char *vendor_name; /* 8 characters or less */
405 const char *product_name; /* 16 characters or less */
406 u16 release;
407
408 char can_stall;
409};
410
411
412struct fsg_dev {
413 struct usb_function function;
414 struct usb_gadget *gadget; /* Copy of cdev->gadget */
415 struct fsg_common *common;
416
417 u16 interface_number;
418
419 unsigned int bulk_in_enabled:1;
420 unsigned int bulk_out_enabled:1;
421
422 unsigned long atomic_bitflags;
423#define IGNORE_BULK_OUT 0
424
425 struct usb_ep *bulk_in;
426 struct usb_ep *bulk_out;
427};
428
429
430static inline int __fsg_is_set(struct fsg_common *common,
431 const char *func, unsigned line)
432{
433 if (common->fsg)
434 return 1;
435 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
436 return 0;
437}
438
439#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
440
441
442static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
443{
444 return container_of(f, struct fsg_dev, function);
445}
446
447
448typedef void (*fsg_routine_t)(struct fsg_dev *);
449
450static int exception_in_progress(struct fsg_common *common)
451{
452 return common->state > FSG_STATE_IDLE;
453}
454
455/* Make bulk-out requests be divisible by the maxpacket size */
456static void set_bulk_out_req_length(struct fsg_common *common,
457 struct fsg_buffhd *bh, unsigned int length)
458{
459 unsigned int rem;
460
461 bh->bulk_out_intended_length = length;
462 rem = length % common->bulk_out_maxpacket;
463 if (rem > 0)
464 length += common->bulk_out_maxpacket - rem;
465 bh->outreq->length = length;
466}
467
468/*-------------------------------------------------------------------------*/
469
470static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
471{
472 const char *name;
473
474 if (ep == fsg->bulk_in)
475 name = "bulk-in";
476 else if (ep == fsg->bulk_out)
477 name = "bulk-out";
478 else
479 name = ep->name;
480 DBG(fsg, "%s set halt\n", name);
481 return usb_ep_set_halt(ep);
482}
483
484
485/*-------------------------------------------------------------------------*/
486
487/* These routines may be called in process context or in_irq */
488
489/* Caller must hold fsg->lock */
490static void wakeup_thread(struct fsg_common *common)
491{
492 /* Tell the main thread that something has happened */
493 common->thread_wakeup_needed = 1;
494 if (common->thread_task)
495 wake_up_process(common->thread_task);
496}
497
498
499static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
500{
501 unsigned long flags;
502
503 /* Do nothing if a higher-priority exception is already in progress.
504 * If a lower-or-equal priority exception is in progress, preempt it
505 * and notify the main thread by sending it a signal. */
506 spin_lock_irqsave(&common->lock, flags);
507 if (common->state <= new_state) {
508 common->exception_req_tag = common->ep0_req_tag;
509 common->state = new_state;
510 if (common->thread_task)
511 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
512 common->thread_task);
513 }
514 spin_unlock_irqrestore(&common->lock, flags);
515}
516
517
518/*-------------------------------------------------------------------------*/
519
520static int ep0_queue(struct fsg_common *common)
521{
522 int rc;
523
524 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
525 common->ep0->driver_data = common;
526 if (rc != 0 && rc != -ESHUTDOWN) {
527 /* We can't do much more than wait for a reset */
528 WARNING(common, "error in submission: %s --> %d\n",
529 common->ep0->name, rc);
530 }
531 return rc;
532}
533
534/*-------------------------------------------------------------------------*/
535
536/* Bulk and interrupt endpoint completion handlers.
537 * These always run in_irq. */
538
539static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
540{
541 struct fsg_common *common = ep->driver_data;
542 struct fsg_buffhd *bh = req->context;
543
544 if (req->status || req->actual != req->length)
545 DBG(common, "%s --> %d, %u/%u\n", __func__,
546 req->status, req->actual, req->length);
547 if (req->status == -ECONNRESET) /* Request was cancelled */
548 usb_ep_fifo_flush(ep);
549
550 /* Hold the lock while we update the request and buffer states */
551 smp_wmb();
552 spin_lock(&common->lock);
553 bh->inreq_busy = 0;
554 bh->state = BUF_STATE_EMPTY;
555 wakeup_thread(common);
556 spin_unlock(&common->lock);
557}
558
559static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
560{
561 struct fsg_common *common = ep->driver_data;
562 struct fsg_buffhd *bh = req->context;
563
564 dump_msg(common, "bulk-out", req->buf, req->actual);
565 if (req->status || req->actual != bh->bulk_out_intended_length)
566 DBG(common, "%s --> %d, %u/%u\n", __func__,
567 req->status, req->actual,
568 bh->bulk_out_intended_length);
569 if (req->status == -ECONNRESET) /* Request was cancelled */
570 usb_ep_fifo_flush(ep);
571
572 /* Hold the lock while we update the request and buffer states */
573 smp_wmb();
574 spin_lock(&common->lock);
575 bh->outreq_busy = 0;
576 bh->state = BUF_STATE_FULL;
577 wakeup_thread(common);
578 spin_unlock(&common->lock);
579}
580
581
582/*-------------------------------------------------------------------------*/
583
584/* Ep0 class-specific handlers. These always run in_irq. */
585
586static int fsg_setup(struct usb_function *f,
587 const struct usb_ctrlrequest *ctrl)
588{
589 struct fsg_dev *fsg = fsg_from_func(f);
590 struct usb_request *req = fsg->common->ep0req;
591 u16 w_index = le16_to_cpu(ctrl->wIndex);
592 u16 w_value = le16_to_cpu(ctrl->wValue);
593 u16 w_length = le16_to_cpu(ctrl->wLength);
594
595 if (!fsg->common->config)
596 return -EOPNOTSUPP;
597
598 switch (ctrl->bRequest) {
599
600 case USB_BULK_RESET_REQUEST:
601 if (ctrl->bRequestType !=
602 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
603 break;
604 if (w_index != fsg->interface_number || w_value != 0)
605 return -EDOM;
606
607 /* Raise an exception to stop the current operation
608 * and reinitialize our state. */
609 DBG(fsg, "bulk reset request\n");
610 raise_exception(fsg->common, FSG_STATE_RESET);
611 return DELAYED_STATUS;
612
613 case USB_BULK_GET_MAX_LUN_REQUEST:
614 if (ctrl->bRequestType !=
615 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
616 break;
617 if (w_index != fsg->interface_number || w_value != 0)
618 return -EDOM;
619 VDBG(fsg, "get max LUN\n");
620 *(u8 *) req->buf = fsg->common->nluns - 1;
621
622 /* Respond with data/status */
623 req->length = min((u16)1, w_length);
624 fsg->common->ep0req_name =
625 ctrl->bRequestType & USB_DIR_IN ? "ep0-in" : "ep0-out";
626 return ep0_queue(fsg->common);
627 }
628
629 VDBG(fsg,
630 "unknown class-specific control req "
631 "%02x.%02x v%04x i%04x l%u\n",
632 ctrl->bRequestType, ctrl->bRequest,
633 le16_to_cpu(ctrl->wValue), w_index, w_length);
634 return -EOPNOTSUPP;
635}
636
637
638/*-------------------------------------------------------------------------*/
639
640/* All the following routines run in process context */
641
642
643/* Use this for bulk or interrupt transfers, not ep0 */
644static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
645 struct usb_request *req, int *pbusy,
646 enum fsg_buffer_state *state)
647{
648 int rc;
649
650 if (ep == fsg->bulk_in)
651 dump_msg(fsg, "bulk-in", req->buf, req->length);
652
653 spin_lock_irq(&fsg->common->lock);
654 *pbusy = 1;
655 *state = BUF_STATE_BUSY;
656 spin_unlock_irq(&fsg->common->lock);
657 rc = usb_ep_queue(ep, req, GFP_KERNEL);
658 if (rc != 0) {
659 *pbusy = 0;
660 *state = BUF_STATE_EMPTY;
661
662 /* We can't do much more than wait for a reset */
663
664 /* Note: currently the net2280 driver fails zero-length
665 * submissions if DMA is enabled. */
666 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
667 req->length == 0))
668 WARNING(fsg, "error in submission: %s --> %d\n",
669 ep->name, rc);
670 }
671}
672
673#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
674 if (fsg_is_set(common)) \
675 start_transfer((common)->fsg, (common)->fsg->ep_name, \
676 req, pbusy, state); \
677 else
678
679#define START_TRANSFER(common, ep_name, req, pbusy, state) \
680 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
681
682
683
684static int sleep_thread(struct fsg_common *common)
685{
686 int rc = 0;
687
688 /* Wait until a signal arrives or we are woken up */
689 for (;;) {
690 try_to_freeze();
691 set_current_state(TASK_INTERRUPTIBLE);
692 if (signal_pending(current)) {
693 rc = -EINTR;
694 break;
695 }
696 if (common->thread_wakeup_needed)
697 break;
698 schedule();
699 }
700 __set_current_state(TASK_RUNNING);
701 common->thread_wakeup_needed = 0;
702 return rc;
703}
704
705
706/*-------------------------------------------------------------------------*/
707
708static int do_read(struct fsg_common *common)
709{
710 struct fsg_lun *curlun = common->curlun;
711 u32 lba;
712 struct fsg_buffhd *bh;
713 int rc;
714 u32 amount_left;
715 loff_t file_offset, file_offset_tmp;
716 unsigned int amount;
717 unsigned int partial_page;
718 ssize_t nread;
719
720 /* Get the starting Logical Block Address and check that it's
721 * not too big */
722 if (common->cmnd[0] == SC_READ_6)
723 lba = get_unaligned_be24(&common->cmnd[1]);
724 else {
725 lba = get_unaligned_be32(&common->cmnd[2]);
726
727 /* We allow DPO (Disable Page Out = don't save data in the
728 * cache) and FUA (Force Unit Access = don't read from the
729 * cache), but we don't implement them. */
730 if ((common->cmnd[1] & ~0x18) != 0) {
731 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
732 return -EINVAL;
733 }
734 }
735 if (lba >= curlun->num_sectors) {
736 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
737 return -EINVAL;
738 }
739 file_offset = ((loff_t) lba) << 9;
740
741 /* Carry out the file reads */
742 amount_left = common->data_size_from_cmnd;
743 if (unlikely(amount_left == 0))
744 return -EIO; /* No default reply */
745
746 for (;;) {
747
748 /* Figure out how much we need to read:
749 * Try to read the remaining amount.
750 * But don't read more than the buffer size.
751 * And don't try to read past the end of the file.
752 * Finally, if we're not at a page boundary, don't read past
753 * the next page.
754 * If this means reading 0 then we were asked to read past
755 * the end of file. */
756 amount = min(amount_left, FSG_BUFLEN);
757 amount = min((loff_t) amount,
758 curlun->file_length - file_offset);
759 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
760 if (partial_page > 0)
761 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
762 partial_page);
763
764 /* Wait for the next buffer to become available */
765 bh = common->next_buffhd_to_fill;
766 while (bh->state != BUF_STATE_EMPTY) {
767 rc = sleep_thread(common);
768 if (rc)
769 return rc;
770 }
771
772 /* If we were asked to read past the end of file,
773 * end with an empty buffer. */
774 if (amount == 0) {
775 curlun->sense_data =
776 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
777 curlun->sense_data_info = file_offset >> 9;
778 curlun->info_valid = 1;
779 bh->inreq->length = 0;
780 bh->state = BUF_STATE_FULL;
781 break;
782 }
783
784 /* Perform the read */
785 file_offset_tmp = file_offset;
786 nread = vfs_read(curlun->filp,
787 (char __user *) bh->buf,
788 amount, &file_offset_tmp);
789 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
790 (unsigned long long) file_offset,
791 (int) nread);
792 if (signal_pending(current))
793 return -EINTR;
794
795 if (nread < 0) {
796 LDBG(curlun, "error in file read: %d\n",
797 (int) nread);
798 nread = 0;
799 } else if (nread < amount) {
800 LDBG(curlun, "partial file read: %d/%u\n",
801 (int) nread, amount);
802 nread -= (nread & 511); /* Round down to a block */
803 }
804 file_offset += nread;
805 amount_left -= nread;
806 common->residue -= nread;
807 bh->inreq->length = nread;
808 bh->state = BUF_STATE_FULL;
809
810 /* If an error occurred, report it and its position */
811 if (nread < amount) {
812 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
813 curlun->sense_data_info = file_offset >> 9;
814 curlun->info_valid = 1;
815 break;
816 }
817
818 if (amount_left == 0)
819 break; /* No more left to read */
820
821 /* Send this buffer and go read some more */
822 bh->inreq->zero = 0;
823 START_TRANSFER_OR(common, bulk_in, bh->inreq,
824 &bh->inreq_busy, &bh->state)
825 /* Don't know what to do if
826 * common->fsg is NULL */
827 return -EIO;
828 common->next_buffhd_to_fill = bh->next;
829 }
830
831 return -EIO; /* No default reply */
832}
833
834
835/*-------------------------------------------------------------------------*/
836
837static int do_write(struct fsg_common *common)
838{
839 struct fsg_lun *curlun = common->curlun;
840 u32 lba;
841 struct fsg_buffhd *bh;
842 int get_some_more;
843 u32 amount_left_to_req, amount_left_to_write;
844 loff_t usb_offset, file_offset, file_offset_tmp;
845 unsigned int amount;
846 unsigned int partial_page;
847 ssize_t nwritten;
848 int rc;
849
850 if (curlun->ro) {
851 curlun->sense_data = SS_WRITE_PROTECTED;
852 return -EINVAL;
853 }
854 spin_lock(&curlun->filp->f_lock);
855 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
856 spin_unlock(&curlun->filp->f_lock);
857
858 /* Get the starting Logical Block Address and check that it's
859 * not too big */
860 if (common->cmnd[0] == SC_WRITE_6)
861 lba = get_unaligned_be24(&common->cmnd[1]);
862 else {
863 lba = get_unaligned_be32(&common->cmnd[2]);
864
865 /* We allow DPO (Disable Page Out = don't save data in the
866 * cache) and FUA (Force Unit Access = write directly to the
867 * medium). We don't implement DPO; we implement FUA by
868 * performing synchronous output. */
869 if (common->cmnd[1] & ~0x18) {
870 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
871 return -EINVAL;
872 }
873 if (common->cmnd[1] & 0x08) { /* FUA */
874 spin_lock(&curlun->filp->f_lock);
875 curlun->filp->f_flags |= O_SYNC;
876 spin_unlock(&curlun->filp->f_lock);
877 }
878 }
879 if (lba >= curlun->num_sectors) {
880 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
881 return -EINVAL;
882 }
883
884 /* Carry out the file writes */
885 get_some_more = 1;
886 file_offset = usb_offset = ((loff_t) lba) << 9;
887 amount_left_to_req = common->data_size_from_cmnd;
888 amount_left_to_write = common->data_size_from_cmnd;
889
890 while (amount_left_to_write > 0) {
891
892 /* Queue a request for more data from the host */
893 bh = common->next_buffhd_to_fill;
894 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
895
896 /* Figure out how much we want to get:
897 * Try to get the remaining amount.
898 * But don't get more than the buffer size.
899 * And don't try to go past the end of the file.
900 * If we're not at a page boundary,
901 * don't go past the next page.
902 * If this means getting 0, then we were asked
903 * to write past the end of file.
904 * Finally, round down to a block boundary. */
905 amount = min(amount_left_to_req, FSG_BUFLEN);
906 amount = min((loff_t) amount, curlun->file_length -
907 usb_offset);
908 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
909 if (partial_page > 0)
910 amount = min(amount,
911 (unsigned int) PAGE_CACHE_SIZE - partial_page);
912
913 if (amount == 0) {
914 get_some_more = 0;
915 curlun->sense_data =
916 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
917 curlun->sense_data_info = usb_offset >> 9;
918 curlun->info_valid = 1;
919 continue;
920 }
921 amount -= (amount & 511);
922 if (amount == 0) {
923
924 /* Why were we were asked to transfer a
925 * partial block? */
926 get_some_more = 0;
927 continue;
928 }
929
930 /* Get the next buffer */
931 usb_offset += amount;
932 common->usb_amount_left -= amount;
933 amount_left_to_req -= amount;
934 if (amount_left_to_req == 0)
935 get_some_more = 0;
936
937 /* amount is always divisible by 512, hence by
938 * the bulk-out maxpacket size */
939 bh->outreq->length = amount;
940 bh->bulk_out_intended_length = amount;
941 bh->outreq->short_not_ok = 1;
942 START_TRANSFER_OR(common, bulk_out, bh->outreq,
943 &bh->outreq_busy, &bh->state)
944 /* Don't know what to do if
945 * common->fsg is NULL */
946 return -EIO;
947 common->next_buffhd_to_fill = bh->next;
948 continue;
949 }
950
951 /* Write the received data to the backing file */
952 bh = common->next_buffhd_to_drain;
953 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
954 break; /* We stopped early */
955 if (bh->state == BUF_STATE_FULL) {
956 smp_rmb();
957 common->next_buffhd_to_drain = bh->next;
958 bh->state = BUF_STATE_EMPTY;
959
960 /* Did something go wrong with the transfer? */
961 if (bh->outreq->status != 0) {
962 curlun->sense_data = SS_COMMUNICATION_FAILURE;
963 curlun->sense_data_info = file_offset >> 9;
964 curlun->info_valid = 1;
965 break;
966 }
967
968 amount = bh->outreq->actual;
969 if (curlun->file_length - file_offset < amount) {
970 LERROR(curlun,
971 "write %u @ %llu beyond end %llu\n",
972 amount, (unsigned long long) file_offset,
973 (unsigned long long) curlun->file_length);
974 amount = curlun->file_length - file_offset;
975 }
976
977 /* Perform the write */
978 file_offset_tmp = file_offset;
979 nwritten = vfs_write(curlun->filp,
980 (char __user *) bh->buf,
981 amount, &file_offset_tmp);
982 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
983 (unsigned long long) file_offset,
984 (int) nwritten);
985 if (signal_pending(current))
986 return -EINTR; /* Interrupted! */
987
988 if (nwritten < 0) {
989 LDBG(curlun, "error in file write: %d\n",
990 (int) nwritten);
991 nwritten = 0;
992 } else if (nwritten < amount) {
993 LDBG(curlun, "partial file write: %d/%u\n",
994 (int) nwritten, amount);
995 nwritten -= (nwritten & 511);
996 /* Round down to a block */
997 }
998 file_offset += nwritten;
999 amount_left_to_write -= nwritten;
1000 common->residue -= nwritten;
1001
1002 /* If an error occurred, report it and its position */
1003 if (nwritten < amount) {
1004 curlun->sense_data = SS_WRITE_ERROR;
1005 curlun->sense_data_info = file_offset >> 9;
1006 curlun->info_valid = 1;
1007 break;
1008 }
1009
1010 /* Did the host decide to stop early? */
1011 if (bh->outreq->actual != bh->outreq->length) {
1012 common->short_packet_received = 1;
1013 break;
1014 }
1015 continue;
1016 }
1017
1018 /* Wait for something to happen */
1019 rc = sleep_thread(common);
1020 if (rc)
1021 return rc;
1022 }
1023
1024 return -EIO; /* No default reply */
1025}
1026
1027
1028/*-------------------------------------------------------------------------*/
1029
1030static int do_synchronize_cache(struct fsg_common *common)
1031{
1032 struct fsg_lun *curlun = common->curlun;
1033 int rc;
1034
1035 /* We ignore the requested LBA and write out all file's
1036 * dirty data buffers. */
1037 rc = fsg_lun_fsync_sub(curlun);
1038 if (rc)
1039 curlun->sense_data = SS_WRITE_ERROR;
1040 return 0;
1041}
1042
1043
1044/*-------------------------------------------------------------------------*/
1045
1046static void invalidate_sub(struct fsg_lun *curlun)
1047{
1048 struct file *filp = curlun->filp;
1049 struct inode *inode = filp->f_path.dentry->d_inode;
1050 unsigned long rc;
1051
1052 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1053 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
1054}
1055
1056static int do_verify(struct fsg_common *common)
1057{
1058 struct fsg_lun *curlun = common->curlun;
1059 u32 lba;
1060 u32 verification_length;
1061 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
1062 loff_t file_offset, file_offset_tmp;
1063 u32 amount_left;
1064 unsigned int amount;
1065 ssize_t nread;
1066
1067 /* Get the starting Logical Block Address and check that it's
1068 * not too big */
1069 lba = get_unaligned_be32(&common->cmnd[2]);
1070 if (lba >= curlun->num_sectors) {
1071 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1072 return -EINVAL;
1073 }
1074
1075 /* We allow DPO (Disable Page Out = don't save data in the
1076 * cache) but we don't implement it. */
1077 if (common->cmnd[1] & ~0x10) {
1078 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1079 return -EINVAL;
1080 }
1081
1082 verification_length = get_unaligned_be16(&common->cmnd[7]);
1083 if (unlikely(verification_length == 0))
1084 return -EIO; /* No default reply */
1085
1086 /* Prepare to carry out the file verify */
1087 amount_left = verification_length << 9;
1088 file_offset = ((loff_t) lba) << 9;
1089
1090 /* Write out all the dirty buffers before invalidating them */
1091 fsg_lun_fsync_sub(curlun);
1092 if (signal_pending(current))
1093 return -EINTR;
1094
1095 invalidate_sub(curlun);
1096 if (signal_pending(current))
1097 return -EINTR;
1098
1099 /* Just try to read the requested blocks */
1100 while (amount_left > 0) {
1101
1102 /* Figure out how much we need to read:
1103 * Try to read the remaining amount, but not more than
1104 * the buffer size.
1105 * And don't try to read past the end of the file.
1106 * If this means reading 0 then we were asked to read
1107 * past the end of file. */
1108 amount = min(amount_left, FSG_BUFLEN);
1109 amount = min((loff_t) amount,
1110 curlun->file_length - file_offset);
1111 if (amount == 0) {
1112 curlun->sense_data =
1113 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1114 curlun->sense_data_info = file_offset >> 9;
1115 curlun->info_valid = 1;
1116 break;
1117 }
1118
1119 /* Perform the read */
1120 file_offset_tmp = file_offset;
1121 nread = vfs_read(curlun->filp,
1122 (char __user *) bh->buf,
1123 amount, &file_offset_tmp);
1124 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1125 (unsigned long long) file_offset,
1126 (int) nread);
1127 if (signal_pending(current))
1128 return -EINTR;
1129
1130 if (nread < 0) {
1131 LDBG(curlun, "error in file verify: %d\n",
1132 (int) nread);
1133 nread = 0;
1134 } else if (nread < amount) {
1135 LDBG(curlun, "partial file verify: %d/%u\n",
1136 (int) nread, amount);
1137 nread -= (nread & 511); /* Round down to a sector */
1138 }
1139 if (nread == 0) {
1140 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1141 curlun->sense_data_info = file_offset >> 9;
1142 curlun->info_valid = 1;
1143 break;
1144 }
1145 file_offset += nread;
1146 amount_left -= nread;
1147 }
1148 return 0;
1149}
1150
1151
1152/*-------------------------------------------------------------------------*/
1153
1154static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1155{
1156 struct fsg_lun *curlun = common->curlun;
1157 u8 *buf = (u8 *) bh->buf;
1158
1159 if (!curlun) { /* Unsupported LUNs are okay */
1160 common->bad_lun_okay = 1;
1161 memset(buf, 0, 36);
1162 buf[0] = 0x7f; /* Unsupported, no device-type */
1163 buf[4] = 31; /* Additional length */
1164 return 36;
1165 }
1166
1167 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1168 buf[1] = curlun->removable ? 0x80 : 0;
1169 buf[2] = 2; /* ANSI SCSI level 2 */
1170 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1171 buf[4] = 31; /* Additional length */
1172 buf[5] = 0; /* No special options */
1173 buf[6] = 0;
1174 buf[7] = 0;
1175 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
1176 return 36;
1177}
1178
1179
1180static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1181{
1182 struct fsg_lun *curlun = common->curlun;
1183 u8 *buf = (u8 *) bh->buf;
1184 u32 sd, sdinfo;
1185 int valid;
1186
1187 /*
1188 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1189 *
1190 * If a REQUEST SENSE command is received from an initiator
1191 * with a pending unit attention condition (before the target
1192 * generates the contingent allegiance condition), then the
1193 * target shall either:
1194 * a) report any pending sense data and preserve the unit
1195 * attention condition on the logical unit, or,
1196 * b) report the unit attention condition, may discard any
1197 * pending sense data, and clear the unit attention
1198 * condition on the logical unit for that initiator.
1199 *
1200 * FSG normally uses option a); enable this code to use option b).
1201 */
1202#if 0
1203 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1204 curlun->sense_data = curlun->unit_attention_data;
1205 curlun->unit_attention_data = SS_NO_SENSE;
1206 }
1207#endif
1208
1209 if (!curlun) { /* Unsupported LUNs are okay */
1210 common->bad_lun_okay = 1;
1211 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1212 sdinfo = 0;
1213 valid = 0;
1214 } else {
1215 sd = curlun->sense_data;
1216 sdinfo = curlun->sense_data_info;
1217 valid = curlun->info_valid << 7;
1218 curlun->sense_data = SS_NO_SENSE;
1219 curlun->sense_data_info = 0;
1220 curlun->info_valid = 0;
1221 }
1222
1223 memset(buf, 0, 18);
1224 buf[0] = valid | 0x70; /* Valid, current error */
1225 buf[2] = SK(sd);
1226 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1227 buf[7] = 18 - 8; /* Additional sense length */
1228 buf[12] = ASC(sd);
1229 buf[13] = ASCQ(sd);
1230 return 18;
1231}
1232
1233
1234static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1235{
1236 struct fsg_lun *curlun = common->curlun;
1237 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1238 int pmi = common->cmnd[8];
1239 u8 *buf = (u8 *) bh->buf;
1240
1241 /* Check the PMI and LBA fields */
1242 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1243 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1244 return -EINVAL;
1245 }
1246
1247 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1248 /* Max logical block */
1249 put_unaligned_be32(512, &buf[4]); /* Block length */
1250 return 8;
1251}
1252
1253
1254static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1255{
1256 struct fsg_lun *curlun = common->curlun;
1257 int msf = common->cmnd[1] & 0x02;
1258 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1259 u8 *buf = (u8 *) bh->buf;
1260
1261 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
1262 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1263 return -EINVAL;
1264 }
1265 if (lba >= curlun->num_sectors) {
1266 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1267 return -EINVAL;
1268 }
1269
1270 memset(buf, 0, 8);
1271 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1272 store_cdrom_address(&buf[4], msf, lba);
1273 return 8;
1274}
1275
1276
1277static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1278{
1279 struct fsg_lun *curlun = common->curlun;
1280 int msf = common->cmnd[1] & 0x02;
1281 int start_track = common->cmnd[6];
1282 u8 *buf = (u8 *) bh->buf;
1283
1284 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1285 start_track > 1) {
1286 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1287 return -EINVAL;
1288 }
1289
1290 memset(buf, 0, 20);
1291 buf[1] = (20-2); /* TOC data length */
1292 buf[2] = 1; /* First track number */
1293 buf[3] = 1; /* Last track number */
1294 buf[5] = 0x16; /* Data track, copying allowed */
1295 buf[6] = 0x01; /* Only track is number 1 */
1296 store_cdrom_address(&buf[8], msf, 0);
1297
1298 buf[13] = 0x16; /* Lead-out track is data */
1299 buf[14] = 0xAA; /* Lead-out track number */
1300 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1301 return 20;
1302}
1303
1304
1305static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1306{
1307 struct fsg_lun *curlun = common->curlun;
1308 int mscmnd = common->cmnd[0];
1309 u8 *buf = (u8 *) bh->buf;
1310 u8 *buf0 = buf;
1311 int pc, page_code;
1312 int changeable_values, all_pages;
1313 int valid_page = 0;
1314 int len, limit;
1315
1316 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
1317 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1318 return -EINVAL;
1319 }
1320 pc = common->cmnd[2] >> 6;
1321 page_code = common->cmnd[2] & 0x3f;
1322 if (pc == 3) {
1323 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1324 return -EINVAL;
1325 }
1326 changeable_values = (pc == 1);
1327 all_pages = (page_code == 0x3f);
1328
1329 /* Write the mode parameter header. Fixed values are: default
1330 * medium type, no cache control (DPOFUA), and no block descriptors.
1331 * The only variable value is the WriteProtect bit. We will fill in
1332 * the mode data length later. */
1333 memset(buf, 0, 8);
1334 if (mscmnd == SC_MODE_SENSE_6) {
1335 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1336 buf += 4;
1337 limit = 255;
1338 } else { /* SC_MODE_SENSE_10 */
1339 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1340 buf += 8;
1341 limit = 65535; /* Should really be FSG_BUFLEN */
1342 }
1343
1344 /* No block descriptors */
1345
1346 /* The mode pages, in numerical order. The only page we support
1347 * is the Caching page. */
1348 if (page_code == 0x08 || all_pages) {
1349 valid_page = 1;
1350 buf[0] = 0x08; /* Page code */
1351 buf[1] = 10; /* Page length */
1352 memset(buf+2, 0, 10); /* None of the fields are changeable */
1353
1354 if (!changeable_values) {
1355 buf[2] = 0x04; /* Write cache enable, */
1356 /* Read cache not disabled */
1357 /* No cache retention priorities */
1358 put_unaligned_be16(0xffff, &buf[4]);
1359 /* Don't disable prefetch */
1360 /* Minimum prefetch = 0 */
1361 put_unaligned_be16(0xffff, &buf[8]);
1362 /* Maximum prefetch */
1363 put_unaligned_be16(0xffff, &buf[10]);
1364 /* Maximum prefetch ceiling */
1365 }
1366 buf += 12;
1367 }
1368
1369 /* Check that a valid page was requested and the mode data length
1370 * isn't too long. */
1371 len = buf - buf0;
1372 if (!valid_page || len > limit) {
1373 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1374 return -EINVAL;
1375 }
1376
1377 /* Store the mode data length */
1378 if (mscmnd == SC_MODE_SENSE_6)
1379 buf0[0] = len - 1;
1380 else
1381 put_unaligned_be16(len - 2, buf0);
1382 return len;
1383}
1384
1385
1386static int do_start_stop(struct fsg_common *common)
1387{
1388 if (!common->curlun) {
1389 return -EINVAL;
1390 } else if (!common->curlun->removable) {
1391 common->curlun->sense_data = SS_INVALID_COMMAND;
1392 return -EINVAL;
1393 }
1394 return 0;
1395}
1396
1397
1398static int do_prevent_allow(struct fsg_common *common)
1399{
1400 struct fsg_lun *curlun = common->curlun;
1401 int prevent;
1402
1403 if (!common->curlun) {
1404 return -EINVAL;
1405 } else if (!common->curlun->removable) {
1406 common->curlun->sense_data = SS_INVALID_COMMAND;
1407 return -EINVAL;
1408 }
1409
1410 prevent = common->cmnd[4] & 0x01;
1411 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
1412 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1413 return -EINVAL;
1414 }
1415
1416 if (curlun->prevent_medium_removal && !prevent)
1417 fsg_lun_fsync_sub(curlun);
1418 curlun->prevent_medium_removal = prevent;
1419 return 0;
1420}
1421
1422
1423static int do_read_format_capacities(struct fsg_common *common,
1424 struct fsg_buffhd *bh)
1425{
1426 struct fsg_lun *curlun = common->curlun;
1427 u8 *buf = (u8 *) bh->buf;
1428
1429 buf[0] = buf[1] = buf[2] = 0;
1430 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
1431 buf += 4;
1432
1433 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1434 /* Number of blocks */
1435 put_unaligned_be32(512, &buf[4]); /* Block length */
1436 buf[4] = 0x02; /* Current capacity */
1437 return 12;
1438}
1439
1440
1441static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1442{
1443 struct fsg_lun *curlun = common->curlun;
1444
1445 /* We don't support MODE SELECT */
1446 if (curlun)
1447 curlun->sense_data = SS_INVALID_COMMAND;
1448 return -EINVAL;
1449}
1450
1451
1452/*-------------------------------------------------------------------------*/
1453
1454static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1455{
1456 int rc;
1457
1458 rc = fsg_set_halt(fsg, fsg->bulk_in);
1459 if (rc == -EAGAIN)
1460 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1461 while (rc != 0) {
1462 if (rc != -EAGAIN) {
1463 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1464 rc = 0;
1465 break;
1466 }
1467
1468 /* Wait for a short time and then try again */
1469 if (msleep_interruptible(100) != 0)
1470 return -EINTR;
1471 rc = usb_ep_set_halt(fsg->bulk_in);
1472 }
1473 return rc;
1474}
1475
1476static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1477{
1478 int rc;
1479
1480 DBG(fsg, "bulk-in set wedge\n");
1481 rc = usb_ep_set_wedge(fsg->bulk_in);
1482 if (rc == -EAGAIN)
1483 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1484 while (rc != 0) {
1485 if (rc != -EAGAIN) {
1486 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1487 rc = 0;
1488 break;
1489 }
1490
1491 /* Wait for a short time and then try again */
1492 if (msleep_interruptible(100) != 0)
1493 return -EINTR;
1494 rc = usb_ep_set_wedge(fsg->bulk_in);
1495 }
1496 return rc;
1497}
1498
1499static int pad_with_zeros(struct fsg_dev *fsg)
1500{
1501 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1502 u32 nkeep = bh->inreq->length;
1503 u32 nsend;
1504 int rc;
1505
1506 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
1507 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1508 while (fsg->common->usb_amount_left > 0) {
1509
1510 /* Wait for the next buffer to be free */
1511 while (bh->state != BUF_STATE_EMPTY) {
1512 rc = sleep_thread(fsg->common);
1513 if (rc)
1514 return rc;
1515 }
1516
1517 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
1518 memset(bh->buf + nkeep, 0, nsend - nkeep);
1519 bh->inreq->length = nsend;
1520 bh->inreq->zero = 0;
1521 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1522 &bh->inreq_busy, &bh->state);
1523 bh = fsg->common->next_buffhd_to_fill = bh->next;
1524 fsg->common->usb_amount_left -= nsend;
1525 nkeep = 0;
1526 }
1527 return 0;
1528}
1529
1530static int throw_away_data(struct fsg_common *common)
1531{
1532 struct fsg_buffhd *bh;
1533 u32 amount;
1534 int rc;
1535
1536 for (bh = common->next_buffhd_to_drain;
1537 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1538 bh = common->next_buffhd_to_drain) {
1539
1540 /* Throw away the data in a filled buffer */
1541 if (bh->state == BUF_STATE_FULL) {
1542 smp_rmb();
1543 bh->state = BUF_STATE_EMPTY;
1544 common->next_buffhd_to_drain = bh->next;
1545
1546 /* A short packet or an error ends everything */
1547 if (bh->outreq->actual != bh->outreq->length ||
1548 bh->outreq->status != 0) {
1549 raise_exception(common,
1550 FSG_STATE_ABORT_BULK_OUT);
1551 return -EINTR;
1552 }
1553 continue;
1554 }
1555
1556 /* Try to submit another request if we need one */
1557 bh = common->next_buffhd_to_fill;
1558 if (bh->state == BUF_STATE_EMPTY
1559 && common->usb_amount_left > 0) {
1560 amount = min(common->usb_amount_left, FSG_BUFLEN);
1561
1562 /* amount is always divisible by 512, hence by
1563 * the bulk-out maxpacket size */
1564 bh->outreq->length = amount;
1565 bh->bulk_out_intended_length = amount;
1566 bh->outreq->short_not_ok = 1;
1567 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1568 &bh->outreq_busy, &bh->state)
1569 /* Don't know what to do if
1570 * common->fsg is NULL */
1571 return -EIO;
1572 common->next_buffhd_to_fill = bh->next;
1573 common->usb_amount_left -= amount;
1574 continue;
1575 }
1576
1577 /* Otherwise wait for something to happen */
1578 rc = sleep_thread(common);
1579 if (rc)
1580 return rc;
1581 }
1582 return 0;
1583}
1584
1585
1586static int finish_reply(struct fsg_common *common)
1587{
1588 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
1589 int rc = 0;
1590
1591 switch (common->data_dir) {
1592 case DATA_DIR_NONE:
1593 break; /* Nothing to send */
1594
1595 /* If we don't know whether the host wants to read or write,
1596 * this must be CB or CBI with an unknown command. We mustn't
1597 * try to send or receive any data. So stall both bulk pipes
1598 * if we can and wait for a reset. */
1599 case DATA_DIR_UNKNOWN:
1600 if (!common->can_stall) {
1601 /* Nothing */
1602 } else if (fsg_is_set(common)) {
1603 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1604 rc = halt_bulk_in_endpoint(common->fsg);
1605 } else {
1606 /* Don't know what to do if common->fsg is NULL */
1607 rc = -EIO;
1608 }
1609 break;
1610
1611 /* All but the last buffer of data must have already been sent */
1612 case DATA_DIR_TO_HOST:
1613 if (common->data_size == 0) {
1614 /* Nothing to send */
1615
1616 /* If there's no residue, simply send the last buffer */
1617 } else if (common->residue == 0) {
1618 bh->inreq->zero = 0;
1619 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1620 &bh->inreq_busy, &bh->state)
1621 return -EIO;
1622 common->next_buffhd_to_fill = bh->next;
1623
1624 /* For Bulk-only, if we're allowed to stall then send the
1625 * short packet and halt the bulk-in endpoint. If we can't
1626 * stall, pad out the remaining data with 0's. */
1627 } else if (common->can_stall) {
1628 bh->inreq->zero = 1;
1629 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1630 &bh->inreq_busy, &bh->state)
1631 /* Don't know what to do if
1632 * common->fsg is NULL */
1633 rc = -EIO;
1634 common->next_buffhd_to_fill = bh->next;
1635 if (common->fsg)
1636 rc = halt_bulk_in_endpoint(common->fsg);
1637 } else if (fsg_is_set(common)) {
1638 rc = pad_with_zeros(common->fsg);
1639 } else {
1640 /* Don't know what to do if common->fsg is NULL */
1641 rc = -EIO;
1642 }
1643 break;
1644
1645 /* We have processed all we want from the data the host has sent.
1646 * There may still be outstanding bulk-out requests. */
1647 case DATA_DIR_FROM_HOST:
1648 if (common->residue == 0) {
1649 /* Nothing to receive */
1650
1651 /* Did the host stop sending unexpectedly early? */
1652 } else if (common->short_packet_received) {
1653 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1654 rc = -EINTR;
1655
1656 /* We haven't processed all the incoming data. Even though
1657 * we may be allowed to stall, doing so would cause a race.
1658 * The controller may already have ACK'ed all the remaining
1659 * bulk-out packets, in which case the host wouldn't see a
1660 * STALL. Not realizing the endpoint was halted, it wouldn't
1661 * clear the halt -- leading to problems later on. */
1662#if 0
1663 } else if (common->can_stall) {
1664 if (fsg_is_set(common))
1665 fsg_set_halt(common->fsg,
1666 common->fsg->bulk_out);
1667 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1668 rc = -EINTR;
1669#endif
1670
1671 /* We can't stall. Read in the excess data and throw it
1672 * all away. */
1673 } else {
1674 rc = throw_away_data(common);
1675 }
1676 break;
1677 }
1678 return rc;
1679}
1680
1681
1682static int send_status(struct fsg_common *common)
1683{
1684 struct fsg_lun *curlun = common->curlun;
1685 struct fsg_buffhd *bh;
1686 struct bulk_cs_wrap *csw;
1687 int rc;
1688 u8 status = USB_STATUS_PASS;
1689 u32 sd, sdinfo = 0;
1690
1691 /* Wait for the next buffer to become available */
1692 bh = common->next_buffhd_to_fill;
1693 while (bh->state != BUF_STATE_EMPTY) {
1694 rc = sleep_thread(common);
1695 if (rc)
1696 return rc;
1697 }
1698
1699 if (curlun) {
1700 sd = curlun->sense_data;
1701 sdinfo = curlun->sense_data_info;
1702 } else if (common->bad_lun_okay)
1703 sd = SS_NO_SENSE;
1704 else
1705 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1706
1707 if (common->phase_error) {
1708 DBG(common, "sending phase-error status\n");
1709 status = USB_STATUS_PHASE_ERROR;
1710 sd = SS_INVALID_COMMAND;
1711 } else if (sd != SS_NO_SENSE) {
1712 DBG(common, "sending command-failure status\n");
1713 status = USB_STATUS_FAIL;
1714 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
1715 " info x%x\n",
1716 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1717 }
1718
1719 /* Store and send the Bulk-only CSW */
1720 csw = (void *)bh->buf;
1721
1722 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
1723 csw->Tag = common->tag;
1724 csw->Residue = cpu_to_le32(common->residue);
1725 csw->Status = status;
1726
1727 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1728 bh->inreq->zero = 0;
1729 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1730 &bh->inreq_busy, &bh->state)
1731 /* Don't know what to do if common->fsg is NULL */
1732 return -EIO;
1733
1734 common->next_buffhd_to_fill = bh->next;
1735 return 0;
1736}
1737
1738
1739/*-------------------------------------------------------------------------*/
1740
1741/* Check whether the command is properly formed and whether its data size
1742 * and direction agree with the values we already have. */
1743static int check_command(struct fsg_common *common, int cmnd_size,
1744 enum data_direction data_dir, unsigned int mask,
1745 int needs_medium, const char *name)
1746{
1747 int i;
1748 int lun = common->cmnd[1] >> 5;
1749 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1750 char hdlen[20];
1751 struct fsg_lun *curlun;
1752
1753 hdlen[0] = 0;
1754 if (common->data_dir != DATA_DIR_UNKNOWN)
1755 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1756 common->data_size);
1757 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1758 name, cmnd_size, dirletter[(int) data_dir],
1759 common->data_size_from_cmnd, common->cmnd_size, hdlen);
1760
1761 /* We can't reply at all until we know the correct data direction
1762 * and size. */
1763 if (common->data_size_from_cmnd == 0)
1764 data_dir = DATA_DIR_NONE;
1765 if (common->data_size < common->data_size_from_cmnd) {
1766 /* Host data size < Device data size is a phase error.
1767 * Carry out the command, but only transfer as much as
1768 * we are allowed. */
1769 common->data_size_from_cmnd = common->data_size;
1770 common->phase_error = 1;
1771 }
1772 common->residue = common->data_size;
1773 common->usb_amount_left = common->data_size;
1774
1775 /* Conflicting data directions is a phase error */
1776 if (common->data_dir != data_dir
1777 && common->data_size_from_cmnd > 0) {
1778 common->phase_error = 1;
1779 return -EINVAL;
1780 }
1781
1782 /* Verify the length of the command itself */
1783 if (cmnd_size != common->cmnd_size) {
1784
1785 /* Special case workaround: There are plenty of buggy SCSI
1786 * implementations. Many have issues with cbw->Length
1787 * field passing a wrong command size. For those cases we
1788 * always try to work around the problem by using the length
1789 * sent by the host side provided it is at least as large
1790 * as the correct command length.
1791 * Examples of such cases would be MS-Windows, which issues
1792 * REQUEST SENSE with cbw->Length == 12 where it should
1793 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1794 * REQUEST SENSE with cbw->Length == 10 where it should
1795 * be 6 as well.
1796 */
1797 if (cmnd_size <= common->cmnd_size) {
1798 DBG(common, "%s is buggy! Expected length %d "
1799 "but we got %d\n", name,
1800 cmnd_size, common->cmnd_size);
1801 cmnd_size = common->cmnd_size;
1802 } else {
1803 common->phase_error = 1;
1804 return -EINVAL;
1805 }
1806 }
1807
1808 /* Check that the LUN values are consistent */
1809 if (common->lun != lun)
1810 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1811 common->lun, lun);
1812
1813 /* Check the LUN */
1814 if (common->lun >= 0 && common->lun < common->nluns) {
1815 curlun = &common->luns[common->lun];
1816 common->curlun = curlun;
1817 if (common->cmnd[0] != SC_REQUEST_SENSE) {
1818 curlun->sense_data = SS_NO_SENSE;
1819 curlun->sense_data_info = 0;
1820 curlun->info_valid = 0;
1821 }
1822 } else {
1823 common->curlun = NULL;
1824 curlun = NULL;
1825 common->bad_lun_okay = 0;
1826
1827 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1828 * to use unsupported LUNs; all others may not. */
1829 if (common->cmnd[0] != SC_INQUIRY &&
1830 common->cmnd[0] != SC_REQUEST_SENSE) {
1831 DBG(common, "unsupported LUN %d\n", common->lun);
1832 return -EINVAL;
1833 }
1834 }
1835
1836 /* If a unit attention condition exists, only INQUIRY and
1837 * REQUEST SENSE commands are allowed; anything else must fail. */
1838 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1839 common->cmnd[0] != SC_INQUIRY &&
1840 common->cmnd[0] != SC_REQUEST_SENSE) {
1841 curlun->sense_data = curlun->unit_attention_data;
1842 curlun->unit_attention_data = SS_NO_SENSE;
1843 return -EINVAL;
1844 }
1845
1846 /* Check that only command bytes listed in the mask are non-zero */
1847 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
1848 for (i = 1; i < cmnd_size; ++i) {
1849 if (common->cmnd[i] && !(mask & (1 << i))) {
1850 if (curlun)
1851 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1852 return -EINVAL;
1853 }
1854 }
1855
1856 /* If the medium isn't mounted and the command needs to access
1857 * it, return an error. */
1858 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1859 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1860 return -EINVAL;
1861 }
1862
1863 return 0;
1864}
1865
1866
1867static int do_scsi_command(struct fsg_common *common)
1868{
1869 struct fsg_buffhd *bh;
1870 int rc;
1871 int reply = -EINVAL;
1872 int i;
1873 static char unknown[16];
1874
1875 dump_cdb(common);
1876
1877 /* Wait for the next buffer to become available for data or status */
1878 bh = common->next_buffhd_to_fill;
1879 common->next_buffhd_to_drain = bh;
1880 while (bh->state != BUF_STATE_EMPTY) {
1881 rc = sleep_thread(common);
1882 if (rc)
1883 return rc;
1884 }
1885 common->phase_error = 0;
1886 common->short_packet_received = 0;
1887
1888 down_read(&common->filesem); /* We're using the backing file */
1889 switch (common->cmnd[0]) {
1890
1891 case SC_INQUIRY:
1892 common->data_size_from_cmnd = common->cmnd[4];
1893 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1894 (1<<4), 0,
1895 "INQUIRY");
1896 if (reply == 0)
1897 reply = do_inquiry(common, bh);
1898 break;
1899
1900 case SC_MODE_SELECT_6:
1901 common->data_size_from_cmnd = common->cmnd[4];
1902 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1903 (1<<1) | (1<<4), 0,
1904 "MODE SELECT(6)");
1905 if (reply == 0)
1906 reply = do_mode_select(common, bh);
1907 break;
1908
1909 case SC_MODE_SELECT_10:
1910 common->data_size_from_cmnd =
1911 get_unaligned_be16(&common->cmnd[7]);
1912 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
1913 (1<<1) | (3<<7), 0,
1914 "MODE SELECT(10)");
1915 if (reply == 0)
1916 reply = do_mode_select(common, bh);
1917 break;
1918
1919 case SC_MODE_SENSE_6:
1920 common->data_size_from_cmnd = common->cmnd[4];
1921 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1922 (1<<1) | (1<<2) | (1<<4), 0,
1923 "MODE SENSE(6)");
1924 if (reply == 0)
1925 reply = do_mode_sense(common, bh);
1926 break;
1927
1928 case SC_MODE_SENSE_10:
1929 common->data_size_from_cmnd =
1930 get_unaligned_be16(&common->cmnd[7]);
1931 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1932 (1<<1) | (1<<2) | (3<<7), 0,
1933 "MODE SENSE(10)");
1934 if (reply == 0)
1935 reply = do_mode_sense(common, bh);
1936 break;
1937
1938 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1939 common->data_size_from_cmnd = 0;
1940 reply = check_command(common, 6, DATA_DIR_NONE,
1941 (1<<4), 0,
1942 "PREVENT-ALLOW MEDIUM REMOVAL");
1943 if (reply == 0)
1944 reply = do_prevent_allow(common);
1945 break;
1946
1947 case SC_READ_6:
1948 i = common->cmnd[4];
1949 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1950 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1951 (7<<1) | (1<<4), 1,
1952 "READ(6)");
1953 if (reply == 0)
1954 reply = do_read(common);
1955 break;
1956
1957 case SC_READ_10:
1958 common->data_size_from_cmnd =
1959 get_unaligned_be16(&common->cmnd[7]) << 9;
1960 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1961 (1<<1) | (0xf<<2) | (3<<7), 1,
1962 "READ(10)");
1963 if (reply == 0)
1964 reply = do_read(common);
1965 break;
1966
1967 case SC_READ_12:
1968 common->data_size_from_cmnd =
1969 get_unaligned_be32(&common->cmnd[6]) << 9;
1970 reply = check_command(common, 12, DATA_DIR_TO_HOST,
1971 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1972 "READ(12)");
1973 if (reply == 0)
1974 reply = do_read(common);
1975 break;
1976
1977 case SC_READ_CAPACITY:
1978 common->data_size_from_cmnd = 8;
1979 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1980 (0xf<<2) | (1<<8), 1,
1981 "READ CAPACITY");
1982 if (reply == 0)
1983 reply = do_read_capacity(common, bh);
1984 break;
1985
1986 case SC_READ_HEADER:
1987 if (!common->curlun || !common->curlun->cdrom)
1988 goto unknown_cmnd;
1989 common->data_size_from_cmnd =
1990 get_unaligned_be16(&common->cmnd[7]);
1991 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1992 (3<<7) | (0x1f<<1), 1,
1993 "READ HEADER");
1994 if (reply == 0)
1995 reply = do_read_header(common, bh);
1996 break;
1997
1998 case SC_READ_TOC:
1999 if (!common->curlun || !common->curlun->cdrom)
2000 goto unknown_cmnd;
2001 common->data_size_from_cmnd =
2002 get_unaligned_be16(&common->cmnd[7]);
2003 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2004 (7<<6) | (1<<1), 1,
2005 "READ TOC");
2006 if (reply == 0)
2007 reply = do_read_toc(common, bh);
2008 break;
2009
2010 case SC_READ_FORMAT_CAPACITIES:
2011 common->data_size_from_cmnd =
2012 get_unaligned_be16(&common->cmnd[7]);
2013 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2014 (3<<7), 1,
2015 "READ FORMAT CAPACITIES");
2016 if (reply == 0)
2017 reply = do_read_format_capacities(common, bh);
2018 break;
2019
2020 case SC_REQUEST_SENSE:
2021 common->data_size_from_cmnd = common->cmnd[4];
2022 reply = check_command(common, 6, DATA_DIR_TO_HOST,
2023 (1<<4), 0,
2024 "REQUEST SENSE");
2025 if (reply == 0)
2026 reply = do_request_sense(common, bh);
2027 break;
2028
2029 case SC_START_STOP_UNIT:
2030 common->data_size_from_cmnd = 0;
2031 reply = check_command(common, 6, DATA_DIR_NONE,
2032 (1<<1) | (1<<4), 0,
2033 "START-STOP UNIT");
2034 if (reply == 0)
2035 reply = do_start_stop(common);
2036 break;
2037
2038 case SC_SYNCHRONIZE_CACHE:
2039 common->data_size_from_cmnd = 0;
2040 reply = check_command(common, 10, DATA_DIR_NONE,
2041 (0xf<<2) | (3<<7), 1,
2042 "SYNCHRONIZE CACHE");
2043 if (reply == 0)
2044 reply = do_synchronize_cache(common);
2045 break;
2046
2047 case SC_TEST_UNIT_READY:
2048 common->data_size_from_cmnd = 0;
2049 reply = check_command(common, 6, DATA_DIR_NONE,
2050 0, 1,
2051 "TEST UNIT READY");
2052 break;
2053
2054 /* Although optional, this command is used by MS-Windows. We
2055 * support a minimal version: BytChk must be 0. */
2056 case SC_VERIFY:
2057 common->data_size_from_cmnd = 0;
2058 reply = check_command(common, 10, DATA_DIR_NONE,
2059 (1<<1) | (0xf<<2) | (3<<7), 1,
2060 "VERIFY");
2061 if (reply == 0)
2062 reply = do_verify(common);
2063 break;
2064
2065 case SC_WRITE_6:
2066 i = common->cmnd[4];
2067 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2068 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
2069 (7<<1) | (1<<4), 1,
2070 "WRITE(6)");
2071 if (reply == 0)
2072 reply = do_write(common);
2073 break;
2074
2075 case SC_WRITE_10:
2076 common->data_size_from_cmnd =
2077 get_unaligned_be16(&common->cmnd[7]) << 9;
2078 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
2079 (1<<1) | (0xf<<2) | (3<<7), 1,
2080 "WRITE(10)");
2081 if (reply == 0)
2082 reply = do_write(common);
2083 break;
2084
2085 case SC_WRITE_12:
2086 common->data_size_from_cmnd =
2087 get_unaligned_be32(&common->cmnd[6]) << 9;
2088 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
2089 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2090 "WRITE(12)");
2091 if (reply == 0)
2092 reply = do_write(common);
2093 break;
2094
2095 /* Some mandatory commands that we recognize but don't implement.
2096 * They don't mean much in this setting. It's left as an exercise
2097 * for anyone interested to implement RESERVE and RELEASE in terms
2098 * of Posix locks. */
2099 case SC_FORMAT_UNIT:
2100 case SC_RELEASE:
2101 case SC_RESERVE:
2102 case SC_SEND_DIAGNOSTIC:
2103 /* Fall through */
2104
2105 default:
2106unknown_cmnd:
2107 common->data_size_from_cmnd = 0;
2108 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2109 reply = check_command(common, common->cmnd_size,
2110 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2111 if (reply == 0) {
2112 common->curlun->sense_data = SS_INVALID_COMMAND;
2113 reply = -EINVAL;
2114 }
2115 break;
2116 }
2117 up_read(&common->filesem);
2118
2119 if (reply == -EINTR || signal_pending(current))
2120 return -EINTR;
2121
2122 /* Set up the single reply buffer for finish_reply() */
2123 if (reply == -EINVAL)
2124 reply = 0; /* Error reply length */
2125 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2126 reply = min((u32) reply, common->data_size_from_cmnd);
2127 bh->inreq->length = reply;
2128 bh->state = BUF_STATE_FULL;
2129 common->residue -= reply;
2130 } /* Otherwise it's already set */
2131
2132 return 0;
2133}
2134
2135
2136/*-------------------------------------------------------------------------*/
2137
2138static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2139{
2140 struct usb_request *req = bh->outreq;
2141 struct fsg_bulk_cb_wrap *cbw = req->buf;
2142 struct fsg_common *common = fsg->common;
2143
2144 /* Was this a real packet? Should it be ignored? */
2145 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2146 return -EINVAL;
2147
2148 /* Is the CBW valid? */
2149 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2150 cbw->Signature != cpu_to_le32(
2151 USB_BULK_CB_SIG)) {
2152 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2153 req->actual,
2154 le32_to_cpu(cbw->Signature));
2155
2156 /* The Bulk-only spec says we MUST stall the IN endpoint
2157 * (6.6.1), so it's unavoidable. It also says we must
2158 * retain this state until the next reset, but there's
2159 * no way to tell the controller driver it should ignore
2160 * Clear-Feature(HALT) requests.
2161 *
2162 * We aren't required to halt the OUT endpoint; instead
2163 * we can simply accept and discard any data received
2164 * until the next reset. */
2165 wedge_bulk_in_endpoint(fsg);
2166 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2167 return -EINVAL;
2168 }
2169
2170 /* Is the CBW meaningful? */
2171 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2172 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2173 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2174 "cmdlen %u\n",
2175 cbw->Lun, cbw->Flags, cbw->Length);
2176
2177 /* We can do anything we want here, so let's stall the
2178 * bulk pipes if we are allowed to. */
2179 if (common->can_stall) {
2180 fsg_set_halt(fsg, fsg->bulk_out);
2181 halt_bulk_in_endpoint(fsg);
2182 }
2183 return -EINVAL;
2184 }
2185
2186 /* Save the command for later */
2187 common->cmnd_size = cbw->Length;
2188 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
2189 if (cbw->Flags & USB_BULK_IN_FLAG)
2190 common->data_dir = DATA_DIR_TO_HOST;
2191 else
2192 common->data_dir = DATA_DIR_FROM_HOST;
2193 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2194 if (common->data_size == 0)
2195 common->data_dir = DATA_DIR_NONE;
2196 common->lun = cbw->Lun;
2197 common->tag = cbw->Tag;
2198 return 0;
2199}
2200
2201
2202static int get_next_command(struct fsg_common *common)
2203{
2204 struct fsg_buffhd *bh;
2205 int rc = 0;
2206
2207 /* Wait for the next buffer to become available */
2208 bh = common->next_buffhd_to_fill;
2209 while (bh->state != BUF_STATE_EMPTY) {
2210 rc = sleep_thread(common);
2211 if (rc)
2212 return rc;
2213 }
2214
2215 /* Queue a request to read a Bulk-only CBW */
2216 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2217 bh->outreq->short_not_ok = 1;
2218 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2219 &bh->outreq_busy, &bh->state)
2220 /* Don't know what to do if common->fsg is NULL */
2221 return -EIO;
2222
2223 /* We will drain the buffer in software, which means we
2224 * can reuse it for the next filling. No need to advance
2225 * next_buffhd_to_fill. */
2226
2227 /* Wait for the CBW to arrive */
2228 while (bh->state != BUF_STATE_FULL) {
2229 rc = sleep_thread(common);
2230 if (rc)
2231 return rc;
2232 }
2233 smp_rmb();
2234 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
2235 bh->state = BUF_STATE_EMPTY;
2236
2237 return rc;
2238}
2239
2240
2241/*-------------------------------------------------------------------------*/
2242
2243static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
2244 const struct usb_endpoint_descriptor *d)
2245{
2246 int rc;
2247
2248 ep->driver_data = common;
2249 rc = usb_ep_enable(ep, d);
2250 if (rc)
2251 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
2252 return rc;
2253}
2254
2255static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
2256 struct usb_request **preq)
2257{
2258 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2259 if (*preq)
2260 return 0;
2261 ERROR(common, "can't allocate request for %s\n", ep->name);
2262 return -ENOMEM;
2263}
2264
2265/*
2266 * Reset interface setting and re-init endpoint state (toggle etc).
2267 * Call with altsetting < 0 to disable the interface. The only other
2268 * available altsetting is 0, which enables the interface.
2269 */
2270static int do_set_interface(struct fsg_common *common, int altsetting)
2271{
2272 int rc = 0;
2273 int i;
2274 const struct usb_endpoint_descriptor *d;
2275
2276 if (common->running)
2277 DBG(common, "reset interface\n");
2278
2279reset:
2280 /* Deallocate the requests */
2281 if (common->prev_fsg) {
2282 struct fsg_dev *fsg = common->prev_fsg;
2283
2284 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2285 struct fsg_buffhd *bh = &common->buffhds[i];
2286
2287 if (bh->inreq) {
2288 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2289 bh->inreq = NULL;
2290 }
2291 if (bh->outreq) {
2292 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2293 bh->outreq = NULL;
2294 }
2295 }
2296
2297 /* Disable the endpoints */
2298 if (fsg->bulk_in_enabled) {
2299 usb_ep_disable(fsg->bulk_in);
2300 fsg->bulk_in_enabled = 0;
2301 }
2302 if (fsg->bulk_out_enabled) {
2303 usb_ep_disable(fsg->bulk_out);
2304 fsg->bulk_out_enabled = 0;
2305 }
2306
2307 common->prev_fsg = 0;
2308 }
2309
2310 common->running = 0;
2311 if (altsetting < 0 || rc != 0)
2312 return rc;
2313
2314 DBG(common, "set interface %d\n", altsetting);
2315
2316 if (fsg_is_set(common)) {
2317 struct fsg_dev *fsg = common->fsg;
2318 common->prev_fsg = common->fsg;
2319
2320 /* Enable the endpoints */
2321 d = fsg_ep_desc(common->gadget,
2322 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2323 rc = enable_endpoint(common, fsg->bulk_in, d);
2324 if (rc)
2325 goto reset;
2326 fsg->bulk_in_enabled = 1;
2327
2328 d = fsg_ep_desc(common->gadget,
2329 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2330 rc = enable_endpoint(common, fsg->bulk_out, d);
2331 if (rc)
2332 goto reset;
2333 fsg->bulk_out_enabled = 1;
2334 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2335 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2336
2337 /* Allocate the requests */
2338 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2339 struct fsg_buffhd *bh = &common->buffhds[i];
2340
2341 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2342 if (rc)
2343 goto reset;
2344 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2345 if (rc)
2346 goto reset;
2347 bh->inreq->buf = bh->outreq->buf = bh->buf;
2348 bh->inreq->context = bh->outreq->context = bh;
2349 bh->inreq->complete = bulk_in_complete;
2350 bh->outreq->complete = bulk_out_complete;
2351 }
2352
2353 common->running = 1;
2354 for (i = 0; i < common->nluns; ++i)
2355 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2356 return rc;
2357 } else {
2358 return -EIO;
2359 }
2360}
2361
2362
2363/*
2364 * Change our operational configuration. This code must agree with the code
2365 * that returns config descriptors, and with interface altsetting code.
2366 *
2367 * It's also responsible for power management interactions. Some
2368 * configurations might not work with our current power sources.
2369 * For now we just assume the gadget is always self-powered.
2370 */
2371static int do_set_config(struct fsg_common *common, u8 new_config)
2372{
2373 int rc = 0;
2374
2375 /* Disable the single interface */
2376 if (common->config != 0) {
2377 DBG(common, "reset config\n");
2378 common->config = 0;
2379 rc = do_set_interface(common, -1);
2380 }
2381
2382 /* Enable the interface */
2383 if (new_config != 0) {
2384 common->config = new_config;
2385 rc = do_set_interface(common, 0);
2386 if (rc != 0)
2387 common->config = 0; /* Reset on errors */
2388 }
2389 return rc;
2390}
2391
2392
2393/****************************** ALT CONFIGS ******************************/
2394
2395
2396static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2397{
2398 struct fsg_dev *fsg = fsg_from_func(f);
2399 fsg->common->prev_fsg = fsg->common->fsg;
2400 fsg->common->fsg = fsg;
2401 fsg->common->new_config = 1;
2402 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2403 return 0;
2404}
2405
2406static void fsg_disable(struct usb_function *f)
2407{
2408 struct fsg_dev *fsg = fsg_from_func(f);
2409 fsg->common->prev_fsg = fsg->common->fsg;
2410 fsg->common->fsg = fsg;
2411 fsg->common->new_config = 0;
2412 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2413}
2414
2415
2416/*-------------------------------------------------------------------------*/
2417
2418static void handle_exception(struct fsg_common *common)
2419{
2420 siginfo_t info;
2421 int sig;
2422 int i;
2423 struct fsg_buffhd *bh;
2424 enum fsg_state old_state;
2425 u8 new_config;
2426 struct fsg_lun *curlun;
2427 unsigned int exception_req_tag;
2428 int rc;
2429
2430 /* Clear the existing signals. Anything but SIGUSR1 is converted
2431 * into a high-priority EXIT exception. */
2432 for (;;) {
2433 sig = dequeue_signal_lock(current, &current->blocked, &info);
2434 if (!sig)
2435 break;
2436 if (sig != SIGUSR1) {
2437 if (common->state < FSG_STATE_EXIT)
2438 DBG(common, "Main thread exiting on signal\n");
2439 raise_exception(common, FSG_STATE_EXIT);
2440 }
2441 }
2442
2443 /* Cancel all the pending transfers */
2444 if (fsg_is_set(common)) {
2445 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2446 bh = &common->buffhds[i];
2447 if (bh->inreq_busy)
2448 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2449 if (bh->outreq_busy)
2450 usb_ep_dequeue(common->fsg->bulk_out,
2451 bh->outreq);
2452 }
2453
2454 /* Wait until everything is idle */
2455 for (;;) {
2456 int num_active = 0;
2457 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2458 bh = &common->buffhds[i];
2459 num_active += bh->inreq_busy + bh->outreq_busy;
2460 }
2461 if (num_active == 0)
2462 break;
2463 if (sleep_thread(common))
2464 return;
2465 }
2466
2467 /* Clear out the controller's fifos */
2468 if (common->fsg->bulk_in_enabled)
2469 usb_ep_fifo_flush(common->fsg->bulk_in);
2470 if (common->fsg->bulk_out_enabled)
2471 usb_ep_fifo_flush(common->fsg->bulk_out);
2472 }
2473
2474 /* Reset the I/O buffer states and pointers, the SCSI
2475 * state, and the exception. Then invoke the handler. */
2476 spin_lock_irq(&common->lock);
2477
2478 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2479 bh = &common->buffhds[i];
2480 bh->state = BUF_STATE_EMPTY;
2481 }
2482 common->next_buffhd_to_fill = &common->buffhds[0];
2483 common->next_buffhd_to_drain = &common->buffhds[0];
2484 exception_req_tag = common->exception_req_tag;
2485 new_config = common->new_config;
2486 old_state = common->state;
2487
2488 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2489 common->state = FSG_STATE_STATUS_PHASE;
2490 else {
2491 for (i = 0; i < common->nluns; ++i) {
2492 curlun = &common->luns[i];
2493 curlun->prevent_medium_removal = 0;
2494 curlun->sense_data = SS_NO_SENSE;
2495 curlun->unit_attention_data = SS_NO_SENSE;
2496 curlun->sense_data_info = 0;
2497 curlun->info_valid = 0;
2498 }
2499 common->state = FSG_STATE_IDLE;
2500 }
2501 spin_unlock_irq(&common->lock);
2502
2503 /* Carry out any extra actions required for the exception */
2504 switch (old_state) {
2505 case FSG_STATE_ABORT_BULK_OUT:
2506 send_status(common);
2507 spin_lock_irq(&common->lock);
2508 if (common->state == FSG_STATE_STATUS_PHASE)
2509 common->state = FSG_STATE_IDLE;
2510 spin_unlock_irq(&common->lock);
2511 break;
2512
2513 case FSG_STATE_RESET:
2514 /* In case we were forced against our will to halt a
2515 * bulk endpoint, clear the halt now. (The SuperH UDC
2516 * requires this.) */
2517 if (!fsg_is_set(common))
2518 break;
2519 if (test_and_clear_bit(IGNORE_BULK_OUT,
2520 &common->fsg->atomic_bitflags))
2521 usb_ep_clear_halt(common->fsg->bulk_in);
2522
2523 if (common->ep0_req_tag == exception_req_tag)
2524 ep0_queue(common); /* Complete the status stage */
2525
2526 /* Technically this should go here, but it would only be
2527 * a waste of time. Ditto for the INTERFACE_CHANGE and
2528 * CONFIG_CHANGE cases. */
2529 /* for (i = 0; i < common->nluns; ++i) */
2530 /* common->luns[i].unit_attention_data = */
2531 /* SS_RESET_OCCURRED; */
2532 break;
2533
2534 case FSG_STATE_CONFIG_CHANGE:
2535 rc = do_set_config(common, new_config);
2536 break;
2537
2538 case FSG_STATE_EXIT:
2539 case FSG_STATE_TERMINATED:
2540 do_set_config(common, 0); /* Free resources */
2541 spin_lock_irq(&common->lock);
2542 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2543 spin_unlock_irq(&common->lock);
2544 break;
2545
2546 case FSG_STATE_INTERFACE_CHANGE:
2547 case FSG_STATE_DISCONNECT:
2548 case FSG_STATE_COMMAND_PHASE:
2549 case FSG_STATE_DATA_PHASE:
2550 case FSG_STATE_STATUS_PHASE:
2551 case FSG_STATE_IDLE:
2552 break;
2553 }
2554}
2555
2556
2557/*-------------------------------------------------------------------------*/
2558
2559static int fsg_main_thread(void *common_)
2560{
2561 struct fsg_common *common = common_;
2562
2563 /* Allow the thread to be killed by a signal, but set the signal mask
2564 * to block everything but INT, TERM, KILL, and USR1. */
2565 allow_signal(SIGINT);
2566 allow_signal(SIGTERM);
2567 allow_signal(SIGKILL);
2568 allow_signal(SIGUSR1);
2569
2570 /* Allow the thread to be frozen */
2571 set_freezable();
2572
2573 /* Arrange for userspace references to be interpreted as kernel
2574 * pointers. That way we can pass a kernel pointer to a routine
2575 * that expects a __user pointer and it will work okay. */
2576 set_fs(get_ds());
2577
2578 /* The main loop */
2579 while (common->state != FSG_STATE_TERMINATED) {
2580 if (exception_in_progress(common) || signal_pending(current)) {
2581 handle_exception(common);
2582 continue;
2583 }
2584
2585 if (!common->running) {
2586 sleep_thread(common);
2587 continue;
2588 }
2589
2590 if (get_next_command(common))
2591 continue;
2592
2593 spin_lock_irq(&common->lock);
2594 if (!exception_in_progress(common))
2595 common->state = FSG_STATE_DATA_PHASE;
2596 spin_unlock_irq(&common->lock);
2597
2598 if (do_scsi_command(common) || finish_reply(common))
2599 continue;
2600
2601 spin_lock_irq(&common->lock);
2602 if (!exception_in_progress(common))
2603 common->state = FSG_STATE_STATUS_PHASE;
2604 spin_unlock_irq(&common->lock);
2605
2606 if (send_status(common))
2607 continue;
2608
2609 spin_lock_irq(&common->lock);
2610 if (!exception_in_progress(common))
2611 common->state = FSG_STATE_IDLE;
2612 spin_unlock_irq(&common->lock);
2613 }
2614
2615 spin_lock_irq(&common->lock);
2616 common->thread_task = NULL;
2617 spin_unlock_irq(&common->lock);
2618
2619 if (!common->thread_exits || common->thread_exits(common) < 0) {
2620 struct fsg_lun *curlun = common->luns;
2621 unsigned i = common->nluns;
2622
2623 down_write(&common->filesem);
2624 for (; i--; ++curlun) {
2625 if (!fsg_lun_is_open(curlun))
2626 continue;
2627
2628 fsg_lun_close(curlun);
2629 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2630 }
2631 up_write(&common->filesem);
2632 }
2633
2634 /* Let the unbind and cleanup routines know the thread has exited */
2635 complete_and_exit(&common->thread_notifier, 0);
2636}
2637
2638
2639/*************************** DEVICE ATTRIBUTES ***************************/
2640
2641/* Write permission is checked per LUN in store_*() functions. */
2642static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2643static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
2644
2645
2646/****************************** FSG COMMON ******************************/
2647
2648static void fsg_common_release(struct kref *ref);
2649
2650static void fsg_lun_release(struct device *dev)
2651{
2652 /* Nothing needs to be done */
2653}
2654
2655static inline void fsg_common_get(struct fsg_common *common)
2656{
2657 kref_get(&common->ref);
2658}
2659
2660static inline void fsg_common_put(struct fsg_common *common)
2661{
2662 kref_put(&common->ref, fsg_common_release);
2663}
2664
2665
2666static struct fsg_common *fsg_common_init(struct fsg_common *common,
2667 struct usb_composite_dev *cdev,
2668 struct fsg_config *cfg)
2669{
2670 struct usb_gadget *gadget = cdev->gadget;
2671 struct fsg_buffhd *bh;
2672 struct fsg_lun *curlun;
2673 struct fsg_lun_config *lcfg;
2674 int nluns, i, rc;
2675 char *pathbuf;
2676
2677 /* Find out how many LUNs there should be */
2678 nluns = cfg->nluns;
2679 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2680 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2681 return ERR_PTR(-EINVAL);
2682 }
2683
2684 /* Allocate? */
2685 if (!common) {
2686 common = kzalloc(sizeof *common, GFP_KERNEL);
2687 if (!common)
2688 return ERR_PTR(-ENOMEM);
2689 common->free_storage_on_release = 1;
2690 } else {
2691 memset(common, 0, sizeof common);
2692 common->free_storage_on_release = 0;
2693 }
2694
2695 common->private_data = cfg->private_data;
2696
2697 common->gadget = gadget;
2698 common->ep0 = gadget->ep0;
2699 common->ep0req = cdev->req;
2700
2701 /* Maybe allocate device-global string IDs, and patch descriptors */
2702 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2703 rc = usb_string_id(cdev);
2704 if (rc < 0) {
2705 kfree(common);
2706 return ERR_PTR(rc);
2707 }
2708 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2709 fsg_intf_desc.iInterface = rc;
2710 }
2711
2712 /* Create the LUNs, open their backing files, and register the
2713 * LUN devices in sysfs. */
2714 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2715 if (!curlun) {
2716 kfree(common);
2717 return ERR_PTR(-ENOMEM);
2718 }
2719 common->luns = curlun;
2720
2721 init_rwsem(&common->filesem);
2722
2723 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2724 curlun->cdrom = !!lcfg->cdrom;
2725 curlun->ro = lcfg->cdrom || lcfg->ro;
2726 curlun->removable = lcfg->removable;
2727 curlun->dev.release = fsg_lun_release;
2728 curlun->dev.parent = &gadget->dev;
2729 /* curlun->dev.driver = &fsg_driver.driver; XXX */
2730 dev_set_drvdata(&curlun->dev, &common->filesem);
2731 dev_set_name(&curlun->dev,
2732 cfg->lun_name_format
2733 ? cfg->lun_name_format
2734 : "lun%d",
2735 i);
2736
2737 rc = device_register(&curlun->dev);
2738 if (rc) {
2739 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2740 common->nluns = i;
2741 goto error_release;
2742 }
2743
2744 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2745 if (rc)
2746 goto error_luns;
2747 rc = device_create_file(&curlun->dev, &dev_attr_file);
2748 if (rc)
2749 goto error_luns;
2750
2751 if (lcfg->filename) {
2752 rc = fsg_lun_open(curlun, lcfg->filename);
2753 if (rc)
2754 goto error_luns;
2755 } else if (!curlun->removable) {
2756 ERROR(common, "no file given for LUN%d\n", i);
2757 rc = -EINVAL;
2758 goto error_luns;
2759 }
2760 }
2761 common->nluns = nluns;
2762
2763
2764 /* Data buffers cyclic list */
2765 /* Buffers in buffhds are static -- no need for additional
2766 * allocation. */
2767 bh = common->buffhds;
2768 i = FSG_NUM_BUFFERS - 1;
2769 do {
2770 bh->next = bh + 1;
2771 } while (++bh, --i);
2772 bh->next = common->buffhds;
2773
2774
2775 /* Prepare inquiryString */
2776 if (cfg->release != 0xffff) {
2777 i = cfg->release;
2778 } else {
2779 i = usb_gadget_controller_number(gadget);
2780 if (i >= 0) {
2781 i = 0x0300 + i;
2782 } else {
2783 WARNING(common, "controller '%s' not recognized\n",
2784 gadget->name);
2785 i = 0x0399;
2786 }
2787 }
2788#define OR(x, y) ((x) ? (x) : (y))
2789 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2790 "%-8s%-16s%04x",
2791 OR(cfg->vendor_name, "Linux "),
2792 /* Assume product name dependent on the first LUN */
2793 OR(cfg->product_name, common->luns->cdrom
2794 ? "File-Stor Gadget"
2795 : "File-CD Gadget "),
2796 i);
2797
2798
2799 /* Some peripheral controllers are known not to be able to
2800 * halt bulk endpoints correctly. If one of them is present,
2801 * disable stalls.
2802 */
2803 common->can_stall = cfg->can_stall &&
2804 !(gadget_is_at91(common->gadget));
2805
2806
2807 spin_lock_init(&common->lock);
2808 kref_init(&common->ref);
2809
2810
2811 /* Tell the thread to start working */
2812 common->thread_exits = cfg->thread_exits;
2813 common->thread_task =
2814 kthread_create(fsg_main_thread, common,
2815 OR(cfg->thread_name, "file-storage"));
2816 if (IS_ERR(common->thread_task)) {
2817 rc = PTR_ERR(common->thread_task);
2818 goto error_release;
2819 }
2820 init_completion(&common->thread_notifier);
2821#undef OR
2822
2823
2824 /* Information */
2825 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2826 INFO(common, "Number of LUNs=%d\n", common->nluns);
2827
2828 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2829 for (i = 0, nluns = common->nluns, curlun = common->luns;
2830 i < nluns;
2831 ++curlun, ++i) {
2832 char *p = "(no medium)";
2833 if (fsg_lun_is_open(curlun)) {
2834 p = "(error)";
2835 if (pathbuf) {
2836 p = d_path(&curlun->filp->f_path,
2837 pathbuf, PATH_MAX);
2838 if (IS_ERR(p))
2839 p = "(error)";
2840 }
2841 }
2842 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2843 curlun->removable ? "removable " : "",
2844 curlun->ro ? "read only " : "",
2845 curlun->cdrom ? "CD-ROM " : "",
2846 p);
2847 }
2848 kfree(pathbuf);
2849
2850 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2851
2852 wake_up_process(common->thread_task);
2853
2854 return common;
2855
2856
2857error_luns:
2858 common->nluns = i + 1;
2859error_release:
2860 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
2861 /* Call fsg_common_release() directly, ref might be not
2862 * initialised */
2863 fsg_common_release(&common->ref);
2864 return ERR_PTR(rc);
2865}
2866
2867
2868static void fsg_common_release(struct kref *ref)
2869{
2870 struct fsg_common *common =
2871 container_of(ref, struct fsg_common, ref);
2872 unsigned i = common->nluns;
2873 struct fsg_lun *lun = common->luns;
2874
2875 /* If the thread isn't already dead, tell it to exit now */
2876 if (common->state != FSG_STATE_TERMINATED) {
2877 raise_exception(common, FSG_STATE_EXIT);
2878 wait_for_completion(&common->thread_notifier);
2879
2880 /* The cleanup routine waits for this completion also */
2881 complete(&common->thread_notifier);
2882 }
2883
2884 /* Beware tempting for -> do-while optimization: when in error
2885 * recovery nluns may be zero. */
2886
2887 for (; i; --i, ++lun) {
2888 device_remove_file(&lun->dev, &dev_attr_ro);
2889 device_remove_file(&lun->dev, &dev_attr_file);
2890 fsg_lun_close(lun);
2891 device_unregister(&lun->dev);
2892 }
2893
2894 kfree(common->luns);
2895 if (common->free_storage_on_release)
2896 kfree(common);
2897}
2898
2899
2900/*-------------------------------------------------------------------------*/
2901
2902
2903static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2904{
2905 struct fsg_dev *fsg = fsg_from_func(f);
2906
2907 DBG(fsg, "unbind\n");
2908 fsg_common_put(fsg->common);
2909 kfree(fsg);
2910}
2911
2912
2913static int __init fsg_bind(struct usb_configuration *c, struct usb_function *f)
2914{
2915 struct fsg_dev *fsg = fsg_from_func(f);
2916 struct usb_gadget *gadget = c->cdev->gadget;
2917 int rc;
2918 int i;
2919 struct usb_ep *ep;
2920
2921 fsg->gadget = gadget;
2922
2923 /* New interface */
2924 i = usb_interface_id(c, f);
2925 if (i < 0)
2926 return i;
2927 fsg_intf_desc.bInterfaceNumber = i;
2928 fsg->interface_number = i;
2929
2930 /* Find all the endpoints we will use */
2931 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2932 if (!ep)
2933 goto autoconf_fail;
2934 ep->driver_data = fsg->common; /* claim the endpoint */
2935 fsg->bulk_in = ep;
2936
2937 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2938 if (!ep)
2939 goto autoconf_fail;
2940 ep->driver_data = fsg->common; /* claim the endpoint */
2941 fsg->bulk_out = ep;
2942
2943 if (gadget_is_dualspeed(gadget)) {
2944 /* Assume endpoint addresses are the same for both speeds */
2945 fsg_hs_bulk_in_desc.bEndpointAddress =
2946 fsg_fs_bulk_in_desc.bEndpointAddress;
2947 fsg_hs_bulk_out_desc.bEndpointAddress =
2948 fsg_fs_bulk_out_desc.bEndpointAddress;
2949 f->hs_descriptors = fsg_hs_function;
2950 }
2951
2952 return 0;
2953
2954autoconf_fail:
2955 ERROR(fsg, "unable to autoconfigure all endpoints\n");
2956 rc = -ENOTSUPP;
2957 return rc;
2958}
2959
2960
2961/****************************** ADD FUNCTION ******************************/
2962
2963static struct usb_gadget_strings *fsg_strings_array[] = {
2964 &fsg_stringtab,
2965 NULL,
2966};
2967
2968static int fsg_add(struct usb_composite_dev *cdev,
2969 struct usb_configuration *c,
2970 struct fsg_common *common)
2971{
2972 struct fsg_dev *fsg;
2973 int rc;
2974
2975 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
2976 if (unlikely(!fsg))
2977 return -ENOMEM;
2978
2979 fsg->function.name = FSG_DRIVER_DESC;
2980 fsg->function.strings = fsg_strings_array;
2981 fsg->function.descriptors = fsg_fs_function;
2982 fsg->function.bind = fsg_bind;
2983 fsg->function.unbind = fsg_unbind;
2984 fsg->function.setup = fsg_setup;
2985 fsg->function.set_alt = fsg_set_alt;
2986 fsg->function.disable = fsg_disable;
2987
2988 fsg->common = common;
2989 /* Our caller holds a reference to common structure so we
2990 * don't have to be worry about it being freed until we return
2991 * from this function. So instead of incrementing counter now
2992 * and decrement in error recovery we increment it only when
2993 * call to usb_add_function() was successful. */
2994
2995 rc = usb_add_function(c, &fsg->function);
2996
2997 if (likely(rc == 0))
2998 fsg_common_get(fsg->common);
2999 else
3000 kfree(fsg);
3001
3002 return rc;
3003}
3004
3005
3006
3007/************************* Module parameters *************************/
3008
3009
3010struct fsg_module_parameters {
3011 char *file[FSG_MAX_LUNS];
3012 int ro[FSG_MAX_LUNS];
3013 int removable[FSG_MAX_LUNS];
3014 int cdrom[FSG_MAX_LUNS];
3015
3016 unsigned int file_count, ro_count, removable_count, cdrom_count;
3017 unsigned int luns; /* nluns */
3018 int stall; /* can_stall */
3019};
3020
3021
3022#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3023 module_param_array_named(prefix ## name, params.name, type, \
3024 &prefix ## params.name ## _count, \
3025 S_IRUGO); \
3026 MODULE_PARM_DESC(prefix ## name, desc)
3027
3028#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3029 module_param_named(prefix ## name, params.name, type, \
3030 S_IRUGO); \
3031 MODULE_PARM_DESC(prefix ## name, desc)
3032
3033#define FSG_MODULE_PARAMETERS(prefix, params) \
3034 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3035 "names of backing files or devices"); \
3036 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3037 "true to force read-only"); \
3038 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3039 "true to simulate removable media"); \
3040 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3041 "true to simulate CD-ROM instead of disk"); \
3042 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3043 "number of LUNs"); \
3044 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3045 "false to prevent bulk stalls")
3046
3047
3048static void
3049fsg_config_from_params(struct fsg_config *cfg,
3050 const struct fsg_module_parameters *params)
3051{
3052 struct fsg_lun_config *lun;
3053 unsigned i;
3054
3055 /* Configure LUNs */
3056 cfg->nluns =
3057 min(params->luns ?: (params->file_count ?: 1u),
3058 (unsigned)FSG_MAX_LUNS);
3059 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
3060 lun->ro = !!params->ro[i];
3061 lun->cdrom = !!params->cdrom[i];
3062 lun->removable = /* Removable by default */
3063 params->removable_count <= i || params->removable[i];
3064 lun->filename =
3065 params->file_count > i && params->file[i][0]
3066 ? params->file[i]
3067 : 0;
3068 }
3069
3070 /* Let MSF use defaults */
3071 cfg->lun_name_format = 0;
3072 cfg->thread_name = 0;
3073 cfg->vendor_name = 0;
3074 cfg->product_name = 0;
3075 cfg->release = 0xffff;
3076
3077 cfg->thread_exits = 0;
3078 cfg->private_data = 0;
3079
3080 /* Finalise */
3081 cfg->can_stall = params->stall;
3082}
3083
3084static inline struct fsg_common *
3085fsg_common_from_params(struct fsg_common *common,
3086 struct usb_composite_dev *cdev,
3087 const struct fsg_module_parameters *params)
3088 __attribute__((unused));
3089static inline struct fsg_common *
3090fsg_common_from_params(struct fsg_common *common,
3091 struct usb_composite_dev *cdev,
3092 const struct fsg_module_parameters *params)
3093{
3094 struct fsg_config cfg;
3095 fsg_config_from_params(&cfg, params);
3096 return fsg_common_init(common, cdev, &cfg);
3097}
3098
diff --git a/drivers/usb/gadget/f_obex.c b/drivers/usb/gadget/f_obex.c
index b4a3ba654ea5..8f8c64371475 100644
--- a/drivers/usb/gadget/f_obex.c
+++ b/drivers/usb/gadget/f_obex.c
@@ -23,6 +23,7 @@
23 23
24/* #define VERBOSE_DEBUG */ 24/* #define VERBOSE_DEBUG */
25 25
26#include <linux/slab.h>
26#include <linux/kernel.h> 27#include <linux/kernel.h>
27#include <linux/device.h> 28#include <linux/device.h>
28 29
diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c
index d2de10b9dc4b..3c6e1a058745 100644
--- a/drivers/usb/gadget/f_phonet.c
+++ b/drivers/usb/gadget/f_phonet.c
@@ -20,6 +20,7 @@
20 * 02110-1301 USA 20 * 02110-1301 USA
21 */ 21 */
22 22
23#include <linux/slab.h>
23#include <linux/kernel.h> 24#include <linux/kernel.h>
24#include <linux/device.h> 25#include <linux/device.h>
25 26
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index c9966cc07d3a..56b022150f22 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
@@ -22,6 +24,7 @@
22 24
23/* #define VERBOSE_DEBUG */ 25/* #define VERBOSE_DEBUG */
24 26
27#include <linux/slab.h>
25#include <linux/kernel.h> 28#include <linux/kernel.h>
26#include <linux/device.h> 29#include <linux/device.h>
27#include <linux/etherdevice.h> 30#include <linux/etherdevice.h>
@@ -149,8 +152,8 @@ static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
149 .bDataInterface = 0x01, 152 .bDataInterface = 0x01,
150}; 153};
151 154
152static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { 155static struct usb_cdc_acm_descriptor rndis_acm_descriptor __initdata = {
153 .bLength = sizeof acm_descriptor, 156 .bLength = sizeof rndis_acm_descriptor,
154 .bDescriptorType = USB_DT_CS_INTERFACE, 157 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_ACM_TYPE, 158 .bDescriptorSubType = USB_CDC_ACM_TYPE,
156 159
@@ -179,6 +182,20 @@ static struct usb_interface_descriptor rndis_data_intf __initdata = {
179 /* .iInterface = DYNAMIC */ 182 /* .iInterface = DYNAMIC */
180}; 183};
181 184
185
186static struct usb_interface_assoc_descriptor
187rndis_iad_descriptor = {
188 .bLength = sizeof rndis_iad_descriptor,
189 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
190
191 .bFirstInterface = 0, /* XXX, hardcoded */
192 .bInterfaceCount = 2, // control + data
193 .bFunctionClass = USB_CLASS_COMM,
194 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
195 .bFunctionProtocol = USB_CDC_PROTO_NONE,
196 /* .iFunction = DYNAMIC */
197};
198
182/* full speed support: */ 199/* full speed support: */
183 200
184static struct usb_endpoint_descriptor fs_notify_desc __initdata = { 201static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
@@ -208,11 +225,12 @@ static struct usb_endpoint_descriptor fs_out_desc __initdata = {
208}; 225};
209 226
210static struct usb_descriptor_header *eth_fs_function[] __initdata = { 227static struct usb_descriptor_header *eth_fs_function[] __initdata = {
228 (struct usb_descriptor_header *) &rndis_iad_descriptor,
211 /* control interface matches ACM, not Ethernet */ 229 /* control interface matches ACM, not Ethernet */
212 (struct usb_descriptor_header *) &rndis_control_intf, 230 (struct usb_descriptor_header *) &rndis_control_intf,
213 (struct usb_descriptor_header *) &header_desc, 231 (struct usb_descriptor_header *) &header_desc,
214 (struct usb_descriptor_header *) &call_mgmt_descriptor, 232 (struct usb_descriptor_header *) &call_mgmt_descriptor,
215 (struct usb_descriptor_header *) &acm_descriptor, 233 (struct usb_descriptor_header *) &rndis_acm_descriptor,
216 (struct usb_descriptor_header *) &rndis_union_desc, 234 (struct usb_descriptor_header *) &rndis_union_desc,
217 (struct usb_descriptor_header *) &fs_notify_desc, 235 (struct usb_descriptor_header *) &fs_notify_desc,
218 /* data interface has no altsetting */ 236 /* data interface has no altsetting */
@@ -252,11 +270,12 @@ static struct usb_endpoint_descriptor hs_out_desc __initdata = {
252}; 270};
253 271
254static struct usb_descriptor_header *eth_hs_function[] __initdata = { 272static struct usb_descriptor_header *eth_hs_function[] __initdata = {
273 (struct usb_descriptor_header *) &rndis_iad_descriptor,
255 /* control interface matches ACM, not Ethernet */ 274 /* control interface matches ACM, not Ethernet */
256 (struct usb_descriptor_header *) &rndis_control_intf, 275 (struct usb_descriptor_header *) &rndis_control_intf,
257 (struct usb_descriptor_header *) &header_desc, 276 (struct usb_descriptor_header *) &header_desc,
258 (struct usb_descriptor_header *) &call_mgmt_descriptor, 277 (struct usb_descriptor_header *) &call_mgmt_descriptor,
259 (struct usb_descriptor_header *) &acm_descriptor, 278 (struct usb_descriptor_header *) &rndis_acm_descriptor,
260 (struct usb_descriptor_header *) &rndis_union_desc, 279 (struct usb_descriptor_header *) &rndis_union_desc,
261 (struct usb_descriptor_header *) &hs_notify_desc, 280 (struct usb_descriptor_header *) &hs_notify_desc,
262 /* data interface has no altsetting */ 281 /* data interface has no altsetting */
@@ -271,6 +290,7 @@ static struct usb_descriptor_header *eth_hs_function[] __initdata = {
271static struct usb_string rndis_string_defs[] = { 290static struct usb_string rndis_string_defs[] = {
272 [0].s = "RNDIS Communications Control", 291 [0].s = "RNDIS Communications Control",
273 [1].s = "RNDIS Ethernet Data", 292 [1].s = "RNDIS Ethernet Data",
293 [2].s = "RNDIS",
274 { } /* end of list */ 294 { } /* end of list */
275}; 295};
276 296
@@ -587,6 +607,7 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
587 if (status < 0) 607 if (status < 0)
588 goto fail; 608 goto fail;
589 rndis->ctrl_id = status; 609 rndis->ctrl_id = status;
610 rndis_iad_descriptor.bFirstInterface = status;
590 611
591 rndis_control_intf.bInterfaceNumber = status; 612 rndis_control_intf.bInterfaceNumber = status;
592 rndis_union_desc.bMasterInterface0 = status; 613 rndis_union_desc.bMasterInterface0 = status;
@@ -749,10 +770,6 @@ rndis_unbind(struct usb_configuration *c, struct usb_function *f)
749/* Some controllers can't support RNDIS ... */ 770/* Some controllers can't support RNDIS ... */
750static inline bool can_support_rndis(struct usb_configuration *c) 771static inline bool can_support_rndis(struct usb_configuration *c)
751{ 772{
752 /* only two endpoints on sa1100 */
753 if (gadget_is_sa1100(c->cdev->gadget))
754 return false;
755
756 /* everything else is *presumably* fine */ 773 /* everything else is *presumably* fine */
757 return true; 774 return true;
758} 775}
@@ -798,6 +815,13 @@ int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
798 return status; 815 return status;
799 rndis_string_defs[1].id = status; 816 rndis_string_defs[1].id = status;
800 rndis_data_intf.iInterface = status; 817 rndis_data_intf.iInterface = status;
818
819 /* IAD iFunction label */
820 status = usb_string_id(c->cdev);
821 if (status < 0)
822 return status;
823 rndis_string_defs[2].id = status;
824 rndis_iad_descriptor.iFunction = status;
801 } 825 }
802 826
803 /* allocate and initialize one new instance */ 827 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_serial.c b/drivers/usb/gadget/f_serial.c
index db0aa93606ef..490b00b01a7d 100644
--- a/drivers/usb/gadget/f_serial.c
+++ b/drivers/usb/gadget/f_serial.c
@@ -10,6 +10,7 @@
10 * either version 2 of that License or (at your option) any later version. 10 * either version 2 of that License or (at your option) any later version.
11 */ 11 */
12 12
13#include <linux/slab.h>
13#include <linux/kernel.h> 14#include <linux/kernel.h>
14#include <linux/device.h> 15#include <linux/device.h>
15 16
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
index 09cba273d2db..6d3cc443d914 100644
--- a/drivers/usb/gadget/f_sourcesink.c
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -21,6 +21,7 @@
21 21
22/* #define VERBOSE_DEBUG */ 22/* #define VERBOSE_DEBUG */
23 23
24#include <linux/slab.h>
24#include <linux/kernel.h> 25#include <linux/kernel.h>
25#include <linux/device.h> 26#include <linux/device.h>
26 27
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c
index a9c98fdb626d..8675ca415329 100644
--- a/drivers/usb/gadget/f_subset.c
+++ b/drivers/usb/gadget/f_subset.c
@@ -19,6 +19,7 @@
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 20 */
21 21
22#include <linux/slab.h>
22#include <linux/kernel.h> 23#include <linux/kernel.h>
23#include <linux/device.h> 24#include <linux/device.h>
24#include <linux/etherdevice.h> 25#include <linux/etherdevice.h>
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 1e6aa504d58a..b49d86e3e45b 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
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 566
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;
@@ -1713,7 +1274,7 @@ static int do_write(struct fsg_dev *fsg)
1713 } 1274 }
1714 if (fsg->cmnd[1] & 0x08) { // FUA 1275 if (fsg->cmnd[1] & 0x08) { // FUA
1715 spin_lock(&curlun->filp->f_lock); 1276 spin_lock(&curlun->filp->f_lock);
1716 curlun->filp->f_flags |= O_SYNC; 1277 curlun->filp->f_flags |= O_DSYNC;
1717 spin_unlock(&curlun->filp->f_lock); 1278 spin_unlock(&curlun->filp->f_lock);
1718 } 1279 }
1719 } 1280 }
@@ -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,19 +1441,19 @@ 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;
1906 unsigned long rc; 1448 unsigned long rc;
1907 1449
1908 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1); 1450 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1909 VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc); 1451 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
1910} 1452}
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 */
@@ -3869,15 +3208,11 @@ static int __init check_parameters(struct fsg_dev *fsg)
3869 * halt bulk endpoints correctly. If one of them is present, 3208 * halt bulk endpoints correctly. If one of them is present,
3870 * disable stalls. 3209 * disable stalls.
3871 */ 3210 */
3872 if (gadget_is_sh(fsg->gadget) || gadget_is_at91(fsg->gadget)) 3211 if (gadget_is_at91(fsg->gadget))
3873 mod_data.can_stall = 0; 3212 mod_data.can_stall = 0;
3874 3213
3875 if (mod_data.release == 0xffff) { // Parameter wasn't set 3214 if (mod_data.release == 0xffff) { // Parameter wasn't set
3876 /* The sa1100 controller is not supported */ 3215 gcnum = usb_gadget_controller_number(fsg->gadget);
3877 if (gadget_is_sa1100(fsg->gadget))
3878 gcnum = -1;
3879 else
3880 gcnum = usb_gadget_controller_number(fsg->gadget);
3881 if (gcnum >= 0) 3216 if (gcnum >= 0)
3882 mod_data.release = 0x0300 + gcnum; 3217 mod_data.release = 0x0300 + gcnum;
3883 else { 3218 else {
@@ -3948,7 +3283,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3948 struct fsg_dev *fsg = the_fsg; 3283 struct fsg_dev *fsg = the_fsg;
3949 int rc; 3284 int rc;
3950 int i; 3285 int i;
3951 struct lun *curlun; 3286 struct fsg_lun *curlun;
3952 struct usb_ep *ep; 3287 struct usb_ep *ep;
3953 struct usb_request *req; 3288 struct usb_request *req;
3954 char *pathbuf, *p; 3289 char *pathbuf, *p;
@@ -3963,10 +3298,10 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3963 3298
3964 if (mod_data.removable) { // Enable the store_xxx attributes 3299 if (mod_data.removable) { // Enable the store_xxx attributes
3965 dev_attr_file.attr.mode = 0644; 3300 dev_attr_file.attr.mode = 0644;
3966 dev_attr_file.store = store_file; 3301 dev_attr_file.store = fsg_store_file;
3967 if (!mod_data.cdrom) { 3302 if (!mod_data.cdrom) {
3968 dev_attr_ro.attr.mode = 0644; 3303 dev_attr_ro.attr.mode = 0644;
3969 dev_attr_ro.store = store_ro; 3304 dev_attr_ro.store = fsg_store_ro;
3970 } 3305 }
3971 } 3306 }
3972 3307
@@ -3974,7 +3309,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3974 i = mod_data.nluns; 3309 i = mod_data.nluns;
3975 if (i == 0) 3310 if (i == 0)
3976 i = max(mod_data.num_filenames, 1u); 3311 i = max(mod_data.num_filenames, 1u);
3977 if (i > MAX_LUNS) { 3312 if (i > FSG_MAX_LUNS) {
3978 ERROR(fsg, "invalid number of LUNs: %d\n", i); 3313 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3979 rc = -EINVAL; 3314 rc = -EINVAL;
3980 goto out; 3315 goto out;
@@ -3982,7 +3317,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3982 3317
3983 /* Create the LUNs, open their backing files, and register the 3318 /* Create the LUNs, open their backing files, and register the
3984 * LUN devices in sysfs. */ 3319 * LUN devices in sysfs. */
3985 fsg->luns = kzalloc(i * sizeof(struct lun), GFP_KERNEL); 3320 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
3986 if (!fsg->luns) { 3321 if (!fsg->luns) {
3987 rc = -ENOMEM; 3322 rc = -ENOMEM;
3988 goto out; 3323 goto out;
@@ -3991,13 +3326,14 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3991 3326
3992 for (i = 0; i < fsg->nluns; ++i) { 3327 for (i = 0; i < fsg->nluns; ++i) {
3993 curlun = &fsg->luns[i]; 3328 curlun = &fsg->luns[i];
3994 curlun->ro = mod_data.ro[i]; 3329 curlun->cdrom = !!mod_data.cdrom;
3995 if (mod_data.cdrom) 3330 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3996 curlun->ro = 1; 3331 curlun->initially_ro = curlun->ro;
3332 curlun->removable = mod_data.removable;
3997 curlun->dev.release = lun_release; 3333 curlun->dev.release = lun_release;
3998 curlun->dev.parent = &gadget->dev; 3334 curlun->dev.parent = &gadget->dev;
3999 curlun->dev.driver = &fsg_driver.driver; 3335 curlun->dev.driver = &fsg_driver.driver;
4000 dev_set_drvdata(&curlun->dev, fsg); 3336 dev_set_drvdata(&curlun->dev, &fsg->filesem);
4001 dev_set_name(&curlun->dev,"%s-lun%d", 3337 dev_set_name(&curlun->dev,"%s-lun%d",
4002 dev_name(&gadget->dev), i); 3338 dev_name(&gadget->dev), i);
4003 3339
@@ -4016,7 +3352,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4016 kref_get(&fsg->ref); 3352 kref_get(&fsg->ref);
4017 3353
4018 if (mod_data.file[i] && *mod_data.file[i]) { 3354 if (mod_data.file[i] && *mod_data.file[i]) {
4019 if ((rc = open_backing_file(curlun, 3355 if ((rc = fsg_lun_open(curlun,
4020 mod_data.file[i])) != 0) 3356 mod_data.file[i])) != 0)
4021 goto out; 3357 goto out;
4022 } else if (!mod_data.removable) { 3358 } else if (!mod_data.removable) {
@@ -4028,20 +3364,20 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4028 3364
4029 /* Find all the endpoints we will use */ 3365 /* Find all the endpoints we will use */
4030 usb_ep_autoconfig_reset(gadget); 3366 usb_ep_autoconfig_reset(gadget);
4031 ep = usb_ep_autoconfig(gadget, &fs_bulk_in_desc); 3367 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4032 if (!ep) 3368 if (!ep)
4033 goto autoconf_fail; 3369 goto autoconf_fail;
4034 ep->driver_data = fsg; // claim the endpoint 3370 ep->driver_data = fsg; // claim the endpoint
4035 fsg->bulk_in = ep; 3371 fsg->bulk_in = ep;
4036 3372
4037 ep = usb_ep_autoconfig(gadget, &fs_bulk_out_desc); 3373 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4038 if (!ep) 3374 if (!ep)
4039 goto autoconf_fail; 3375 goto autoconf_fail;
4040 ep->driver_data = fsg; // claim the endpoint 3376 ep->driver_data = fsg; // claim the endpoint
4041 fsg->bulk_out = ep; 3377 fsg->bulk_out = ep;
4042 3378
4043 if (transport_is_cbi()) { 3379 if (transport_is_cbi()) {
4044 ep = usb_ep_autoconfig(gadget, &fs_intr_in_desc); 3380 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4045 if (!ep) 3381 if (!ep)
4046 goto autoconf_fail; 3382 goto autoconf_fail;
4047 ep->driver_data = fsg; // claim the endpoint 3383 ep->driver_data = fsg; // claim the endpoint
@@ -4055,28 +3391,28 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4055 device_desc.bcdDevice = cpu_to_le16(mod_data.release); 3391 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4056 3392
4057 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints 3393 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4058 intf_desc.bNumEndpoints = i; 3394 fsg_intf_desc.bNumEndpoints = i;
4059 intf_desc.bInterfaceSubClass = mod_data.protocol_type; 3395 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4060 intf_desc.bInterfaceProtocol = mod_data.transport_type; 3396 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4061 fs_function[i + FS_FUNCTION_PRE_EP_ENTRIES] = NULL; 3397 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4062 3398
4063 if (gadget_is_dualspeed(gadget)) { 3399 if (gadget_is_dualspeed(gadget)) {
4064 hs_function[i + HS_FUNCTION_PRE_EP_ENTRIES] = NULL; 3400 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4065 3401
4066 /* Assume ep0 uses the same maxpacket value for both speeds */ 3402 /* Assume ep0 uses the same maxpacket value for both speeds */
4067 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket; 3403 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
4068 3404
4069 /* Assume endpoint addresses are the same for both speeds */ 3405 /* Assume endpoint addresses are the same for both speeds */
4070 hs_bulk_in_desc.bEndpointAddress = 3406 fsg_hs_bulk_in_desc.bEndpointAddress =
4071 fs_bulk_in_desc.bEndpointAddress; 3407 fsg_fs_bulk_in_desc.bEndpointAddress;
4072 hs_bulk_out_desc.bEndpointAddress = 3408 fsg_hs_bulk_out_desc.bEndpointAddress =
4073 fs_bulk_out_desc.bEndpointAddress; 3409 fsg_fs_bulk_out_desc.bEndpointAddress;
4074 hs_intr_in_desc.bEndpointAddress = 3410 fsg_hs_intr_in_desc.bEndpointAddress =
4075 fs_intr_in_desc.bEndpointAddress; 3411 fsg_fs_intr_in_desc.bEndpointAddress;
4076 } 3412 }
4077 3413
4078 if (gadget_is_otg(gadget)) 3414 if (gadget_is_otg(gadget))
4079 otg_desc.bmAttributes |= USB_OTG_HNP; 3415 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4080 3416
4081 rc = -ENOMEM; 3417 rc = -ENOMEM;
4082 3418
@@ -4090,7 +3426,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4090 req->complete = ep0_complete; 3426 req->complete = ep0_complete;
4091 3427
4092 /* Allocate the data buffers */ 3428 /* Allocate the data buffers */
4093 for (i = 0; i < NUM_BUFFERS; ++i) { 3429 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
4094 struct fsg_buffhd *bh = &fsg->buffhds[i]; 3430 struct fsg_buffhd *bh = &fsg->buffhds[i];
4095 3431
4096 /* Allocate for the bulk-in endpoint. We assume that 3432 /* Allocate for the bulk-in endpoint. We assume that
@@ -4101,23 +3437,24 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4101 goto out; 3437 goto out;
4102 bh->next = bh + 1; 3438 bh->next = bh + 1;
4103 } 3439 }
4104 fsg->buffhds[NUM_BUFFERS - 1].next = &fsg->buffhds[0]; 3440 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
4105 3441
4106 /* This should reflect the actual gadget power source */ 3442 /* This should reflect the actual gadget power source */
4107 usb_gadget_set_selfpowered(gadget); 3443 usb_gadget_set_selfpowered(gadget);
4108 3444
4109 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", 3445 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3446 "%s %s with %s",
4110 init_utsname()->sysname, init_utsname()->release, 3447 init_utsname()->sysname, init_utsname()->release,
4111 gadget->name); 3448 gadget->name);
4112 3449
4113 /* On a real device, serial[] would be loaded from permanent 3450 /* On a real device, serial[] would be loaded from permanent
4114 * storage. We just encode it from the driver version string. */ 3451 * storage. We just encode it from the driver version string. */
4115 for (i = 0; i < sizeof(serial) - 2; i += 2) { 3452 for (i = 0; i < sizeof fsg_string_serial - 2; i += 2) {
4116 unsigned char c = DRIVER_VERSION[i / 2]; 3453 unsigned char c = DRIVER_VERSION[i / 2];
4117 3454
4118 if (!c) 3455 if (!c)
4119 break; 3456 break;
4120 sprintf(&serial[i], "%02X", c); 3457 sprintf(&fsg_string_serial[i], "%02X", c);
4121 } 3458 }
4122 3459
4123 fsg->thread_task = kthread_create(fsg_main_thread, fsg, 3460 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
@@ -4133,7 +3470,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
4133 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 3470 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4134 for (i = 0; i < fsg->nluns; ++i) { 3471 for (i = 0; i < fsg->nluns; ++i) {
4135 curlun = &fsg->luns[i]; 3472 curlun = &fsg->luns[i];
4136 if (backing_file_is_open(curlun)) { 3473 if (fsg_lun_is_open(curlun)) {
4137 p = NULL; 3474 p = NULL;
4138 if (pathbuf) { 3475 if (pathbuf) {
4139 p = d_path(&curlun->filp->f_path, 3476 p = d_path(&curlun->filp->f_path,
@@ -4203,7 +3540,7 @@ static struct usb_gadget_driver fsg_driver = {
4203#else 3540#else
4204 .speed = USB_SPEED_FULL, 3541 .speed = USB_SPEED_FULL,
4205#endif 3542#endif
4206 .function = (char *) longname, 3543 .function = (char *) fsg_string_product,
4207 .bind = fsg_bind, 3544 .bind = fsg_bind,
4208 .unbind = fsg_unbind, 3545 .unbind = fsg_unbind,
4209 .disconnect = fsg_disconnect, 3546 .disconnect = fsg_disconnect,
@@ -4212,7 +3549,7 @@ static struct usb_gadget_driver fsg_driver = {
4212 .resume = fsg_resume, 3549 .resume = fsg_resume,
4213 3550
4214 .driver = { 3551 .driver = {
4215 .name = (char *) shortname, 3552 .name = DRIVER_NAME,
4216 .owner = THIS_MODULE, 3553 .owner = THIS_MODULE,
4217 // .release = ... 3554 // .release = ...
4218 // .suspend = ... 3555 // .suspend = ...
diff --git a/drivers/usb/gadget/fsl_mx3_udc.c b/drivers/usb/gadget/fsl_mx3_udc.c
index 4bc2bf3d602e..20a802ecaa15 100644
--- a/drivers/usb/gadget/fsl_mx3_udc.c
+++ b/drivers/usb/gadget/fsl_mx3_udc.c
@@ -17,6 +17,8 @@
17#include <linux/fsl_devices.h> 17#include <linux/fsl_devices.h>
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19 19
20#include <mach/hardware.h>
21
20static struct clk *mxc_ahb_clk; 22static struct clk *mxc_ahb_clk;
21static struct clk *mxc_usb_clk; 23static struct clk *mxc_usb_clk;
22 24
@@ -28,14 +30,16 @@ int fsl_udc_clk_init(struct platform_device *pdev)
28 30
29 pdata = pdev->dev.platform_data; 31 pdata = pdev->dev.platform_data;
30 32
31 mxc_ahb_clk = clk_get(&pdev->dev, "usb_ahb"); 33 if (!cpu_is_mx35()) {
32 if (IS_ERR(mxc_ahb_clk)) 34 mxc_ahb_clk = clk_get(&pdev->dev, "usb_ahb");
33 return PTR_ERR(mxc_ahb_clk); 35 if (IS_ERR(mxc_ahb_clk))
36 return PTR_ERR(mxc_ahb_clk);
34 37
35 ret = clk_enable(mxc_ahb_clk); 38 ret = clk_enable(mxc_ahb_clk);
36 if (ret < 0) { 39 if (ret < 0) {
37 dev_err(&pdev->dev, "clk_enable(\"usb_ahb\") failed\n"); 40 dev_err(&pdev->dev, "clk_enable(\"usb_ahb\") failed\n");
38 goto eenahb; 41 goto eenahb;
42 }
39 } 43 }
40 44
41 /* make sure USB_CLK is running at 60 MHz +/- 1000 Hz */ 45 /* make sure USB_CLK is running at 60 MHz +/- 1000 Hz */
@@ -50,6 +54,7 @@ int fsl_udc_clk_init(struct platform_device *pdev)
50 if (pdata->phy_mode != FSL_USB2_PHY_ULPI && 54 if (pdata->phy_mode != FSL_USB2_PHY_ULPI &&
51 (freq < 59999000 || freq > 60001000)) { 55 (freq < 59999000 || freq > 60001000)) {
52 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq); 56 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq);
57 ret = -EINVAL;
53 goto eclkrate; 58 goto eclkrate;
54 } 59 }
55 60
@@ -66,9 +71,11 @@ eclkrate:
66 clk_put(mxc_usb_clk); 71 clk_put(mxc_usb_clk);
67 mxc_usb_clk = NULL; 72 mxc_usb_clk = NULL;
68egusb: 73egusb:
69 clk_disable(mxc_ahb_clk); 74 if (!cpu_is_mx35())
75 clk_disable(mxc_ahb_clk);
70eenahb: 76eenahb:
71 clk_put(mxc_ahb_clk); 77 if (!cpu_is_mx35())
78 clk_put(mxc_ahb_clk);
72 return ret; 79 return ret;
73} 80}
74 81
@@ -90,6 +97,8 @@ void fsl_udc_clk_release(void)
90 clk_disable(mxc_usb_clk); 97 clk_disable(mxc_usb_clk);
91 clk_put(mxc_usb_clk); 98 clk_put(mxc_usb_clk);
92 } 99 }
93 clk_disable(mxc_ahb_clk); 100 if (!cpu_is_mx35()) {
94 clk_put(mxc_ahb_clk); 101 clk_disable(mxc_ahb_clk);
102 clk_put(mxc_ahb_clk);
103 }
95} 104}
diff --git a/drivers/usb/gadget/fsl_qe_udc.c b/drivers/usb/gadget/fsl_qe_udc.c
index 7881f12413c4..3537d51073b2 100644
--- a/drivers/usb/gadget/fsl_qe_udc.c
+++ b/drivers/usb/gadget/fsl_qe_udc.c
@@ -2749,7 +2749,7 @@ static int __devexit qe_udc_remove(struct of_device *ofdev)
2749} 2749}
2750 2750
2751/*-------------------------------------------------------------------------*/ 2751/*-------------------------------------------------------------------------*/
2752static struct of_device_id __devinitdata qe_udc_match[] = { 2752static const struct of_device_id qe_udc_match[] __devinitconst = {
2753 { 2753 {
2754 .compatible = "fsl,mpc8323-qe-usb", 2754 .compatible = "fsl,mpc8323-qe-usb",
2755 .data = (void *)PORT_QE, 2755 .data = (void *)PORT_QE,
diff --git a/drivers/usb/gadget/fsl_qe_udc.h b/drivers/usb/gadget/fsl_qe_udc.h
index 31b2710882e4..bea5b827bebe 100644
--- a/drivers/usb/gadget/fsl_qe_udc.h
+++ b/drivers/usb/gadget/fsl_qe_udc.h
@@ -419,19 +419,4 @@ struct qe_udc {
419#define CPM_USB_RESTART_TX_OPCODE 0x0b 419#define CPM_USB_RESTART_TX_OPCODE 0x0b
420#define CPM_USB_EP_SHIFT 5 420#define CPM_USB_EP_SHIFT 5
421 421
422#ifndef CONFIG_CPM
423inline int cpm_command(u32 command, u8 opcode)
424{
425 return -EOPNOTSUPP;
426}
427#endif
428
429#ifndef CONFIG_QUICC_ENGINE
430inline int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol,
431 u32 cmd_input)
432{
433 return -EOPNOTSUPP;
434}
435#endif
436
437#endif /* __FSL_QE_UDC_H */ 422#endif /* __FSL_QE_UDC_H */
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index f2d270b202f2..e511fec9f26d 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -45,46 +45,18 @@
45#define gadget_is_goku(g) 0 45#define gadget_is_goku(g) 0
46#endif 46#endif
47 47
48/* SH3 UDC -- not yet ported 2.4 --> 2.6 */
49#ifdef CONFIG_USB_GADGET_SUPERH
50#define gadget_is_sh(g) !strcmp("sh_udc", (g)->name)
51#else
52#define gadget_is_sh(g) 0
53#endif
54
55/* not yet stable on 2.6 (would help "original Zaurus") */
56#ifdef CONFIG_USB_GADGET_SA1100
57#define gadget_is_sa1100(g) !strcmp("sa1100_udc", (g)->name)
58#else
59#define gadget_is_sa1100(g) 0
60#endif
61
62#ifdef CONFIG_USB_GADGET_LH7A40X 48#ifdef CONFIG_USB_GADGET_LH7A40X
63#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name) 49#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name)
64#else 50#else
65#define gadget_is_lh7a40x(g) 0 51#define gadget_is_lh7a40x(g) 0
66#endif 52#endif
67 53
68/* handhelds.org tree (?) */
69#ifdef CONFIG_USB_GADGET_MQ11XX
70#define gadget_is_mq11xx(g) !strcmp("mq11xx_udc", (g)->name)
71#else
72#define gadget_is_mq11xx(g) 0
73#endif
74
75#ifdef CONFIG_USB_GADGET_OMAP 54#ifdef CONFIG_USB_GADGET_OMAP
76#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name) 55#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name)
77#else 56#else
78#define gadget_is_omap(g) 0 57#define gadget_is_omap(g) 0
79#endif 58#endif
80 59
81/* not yet ported 2.4 --> 2.6 */
82#ifdef CONFIG_USB_GADGET_N9604
83#define gadget_is_n9604(g) !strcmp("n9604_udc", (g)->name)
84#else
85#define gadget_is_n9604(g) 0
86#endif
87
88/* various unstable versions available */ 60/* various unstable versions available */
89#ifdef CONFIG_USB_GADGET_PXA27X 61#ifdef CONFIG_USB_GADGET_PXA27X
90#define gadget_is_pxa27x(g) !strcmp("pxa27x_udc", (g)->name) 62#define gadget_is_pxa27x(g) !strcmp("pxa27x_udc", (g)->name)
@@ -122,14 +94,6 @@
122#define gadget_is_fsl_usb2(g) 0 94#define gadget_is_fsl_usb2(g) 0
123#endif 95#endif
124 96
125/* Mentor high speed function controller */
126/* from Montavista kernel (?) */
127#ifdef CONFIG_USB_GADGET_MUSBHSFC
128#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name)
129#else
130#define gadget_is_musbhsfc(g) 0
131#endif
132
133/* Mentor high speed "dual role" controller, in peripheral role */ 97/* Mentor high speed "dual role" controller, in peripheral role */
134#ifdef CONFIG_USB_GADGET_MUSB_HDRC 98#ifdef CONFIG_USB_GADGET_MUSB_HDRC
135#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name) 99#define gadget_is_musbhdrc(g) !strcmp("musb_hdrc", (g)->name)
@@ -143,13 +107,6 @@
143#define gadget_is_langwell(g) 0 107#define gadget_is_langwell(g) 0
144#endif 108#endif
145 109
146/* from Montavista kernel (?) */
147#ifdef CONFIG_USB_GADGET_MPC8272
148#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name)
149#else
150#define gadget_is_mpc8272(g) 0
151#endif
152
153#ifdef CONFIG_USB_GADGET_M66592 110#ifdef CONFIG_USB_GADGET_M66592
154#define gadget_is_m66592(g) !strcmp("m66592_udc", (g)->name) 111#define gadget_is_m66592(g) !strcmp("m66592_udc", (g)->name)
155#else 112#else
@@ -179,6 +136,12 @@
179#define gadget_is_r8a66597(g) 0 136#define gadget_is_r8a66597(g) 0
180#endif 137#endif
181 138
139#ifdef CONFIG_USB_S3C_HSOTG
140#define gadget_is_s3c_hsotg(g) (!strcmp("s3c-hsotg", (g)->name))
141#else
142#define gadget_is_s3c_hsotg(g) 0
143#endif
144
182 145
183/** 146/**
184 * usb_gadget_controller_number - support bcdDevice id convention 147 * usb_gadget_controller_number - support bcdDevice id convention
@@ -203,20 +166,12 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
203 return 0x02; 166 return 0x02;
204 else if (gadget_is_pxa(gadget)) 167 else if (gadget_is_pxa(gadget))
205 return 0x03; 168 return 0x03;
206 else if (gadget_is_sh(gadget))
207 return 0x04;
208 else if (gadget_is_sa1100(gadget))
209 return 0x05;
210 else if (gadget_is_goku(gadget)) 169 else if (gadget_is_goku(gadget))
211 return 0x06; 170 return 0x06;
212 else if (gadget_is_mq11xx(gadget))
213 return 0x07;
214 else if (gadget_is_omap(gadget)) 171 else if (gadget_is_omap(gadget))
215 return 0x08; 172 return 0x08;
216 else if (gadget_is_lh7a40x(gadget)) 173 else if (gadget_is_lh7a40x(gadget))
217 return 0x09; 174 return 0x09;
218 else if (gadget_is_n9604(gadget))
219 return 0x10;
220 else if (gadget_is_pxa27x(gadget)) 175 else if (gadget_is_pxa27x(gadget))
221 return 0x11; 176 return 0x11;
222 else if (gadget_is_s3c2410(gadget)) 177 else if (gadget_is_s3c2410(gadget))
@@ -225,12 +180,8 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
225 return 0x13; 180 return 0x13;
226 else if (gadget_is_imx(gadget)) 181 else if (gadget_is_imx(gadget))
227 return 0x14; 182 return 0x14;
228 else if (gadget_is_musbhsfc(gadget))
229 return 0x15;
230 else if (gadget_is_musbhdrc(gadget)) 183 else if (gadget_is_musbhdrc(gadget))
231 return 0x16; 184 return 0x16;
232 else if (gadget_is_mpc8272(gadget))
233 return 0x17;
234 else if (gadget_is_atmel_usba(gadget)) 185 else if (gadget_is_atmel_usba(gadget))
235 return 0x18; 186 return 0x18;
236 else if (gadget_is_fsl_usb2(gadget)) 187 else if (gadget_is_fsl_usb2(gadget))
@@ -247,6 +198,8 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
247 return 0x24; 198 return 0x24;
248 else if (gadget_is_r8a66597(gadget)) 199 else if (gadget_is_r8a66597(gadget))
249 return 0x25; 200 return 0x25;
201 else if (gadget_is_s3c_hsotg(gadget))
202 return 0x26;
250 return -ENOENT; 203 return -ENOENT;
251} 204}
252 205
@@ -265,10 +218,6 @@ static inline bool gadget_supports_altsettings(struct usb_gadget *gadget)
265 if (gadget_is_pxa27x(gadget)) 218 if (gadget_is_pxa27x(gadget))
266 return false; 219 return false;
267 220
268 /* SH3 hardware just doesn't do altsettings */
269 if (gadget_is_sh(gadget))
270 return false;
271
272 /* Everything else is *presumably* fine ... */ 221 /* Everything else is *presumably* fine ... */
273 return true; 222 return true;
274} 223}
diff --git a/drivers/usb/gadget/gmidi.c b/drivers/usb/gadget/gmidi.c
index d0b1e836f0e0..2b56ce621852 100644
--- a/drivers/usb/gadget/gmidi.c
+++ b/drivers/usb/gadget/gmidi.c
@@ -21,6 +21,7 @@
21/* #define VERBOSE_DEBUG */ 21/* #define VERBOSE_DEBUG */
22 22
23#include <linux/kernel.h> 23#include <linux/kernel.h>
24#include <linux/slab.h>
24#include <linux/utsname.h> 25#include <linux/utsname.h>
25#include <linux/device.h> 26#include <linux/device.h>
26 27
@@ -237,7 +238,7 @@ static const struct usb_interface_descriptor ac_interface_desc = {
237}; 238};
238 239
239/* B.3.2 Class-Specific AC Interface Descriptor */ 240/* B.3.2 Class-Specific AC Interface Descriptor */
240static const struct uac_ac_header_descriptor_1 ac_header_desc = { 241static const struct uac_ac_header_descriptor_v1_1 ac_header_desc = {
241 .bLength = UAC_DT_AC_HEADER_SIZE(1), 242 .bLength = UAC_DT_AC_HEADER_SIZE(1),
242 .bDescriptorType = USB_DT_CS_INTERFACE, 243 .bDescriptorType = USB_DT_CS_INTERFACE,
243 .bDescriptorSubtype = USB_MS_HEADER, 244 .bDescriptorSubtype = USB_MS_HEADER,
@@ -618,11 +619,6 @@ gmidi_set_config(struct gmidi_device *dev, unsigned number, gfp_t gfp_flags)
618 } 619 }
619#endif 620#endif
620 621
621 if (gadget_is_sa1100(gadget) && dev->config) {
622 /* tx fifo is full, but we can't clear it...*/
623 ERROR(dev, "can't change configurations\n");
624 return -ESPIPE;
625 }
626 gmidi_reset_config(dev); 622 gmidi_reset_config(dev);
627 623
628 switch (number) { 624 switch (number) {
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index 112bb40a427c..1088d08c7ed8 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -1768,7 +1768,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1768 * usb_gadget_driver_{register,unregister}() must change. 1768 * usb_gadget_driver_{register,unregister}() must change.
1769 */ 1769 */
1770 if (the_controller) { 1770 if (the_controller) {
1771 WARNING(dev, "ignoring %s\n", pci_name(pdev)); 1771 pr_warning("ignoring %s\n", pci_name(pdev));
1772 return -EBUSY; 1772 return -EBUSY;
1773 } 1773 }
1774 if (!pdev->irq) { 1774 if (!pdev->irq) {
@@ -1859,7 +1859,7 @@ done:
1859 1859
1860/*-------------------------------------------------------------------------*/ 1860/*-------------------------------------------------------------------------*/
1861 1861
1862static struct pci_device_id pci_ids [] = { { 1862static const struct pci_device_id pci_ids[] = { {
1863 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe), 1863 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1864 .class_mask = ~0, 1864 .class_mask = ~0,
1865 .vendor = 0x102f, /* Toshiba */ 1865 .vendor = 0x102f, /* Toshiba */
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index 01ee0b9bc957..e743122fcd93 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -29,6 +29,7 @@
29#include <linux/clk.h> 29#include <linux/clk.h>
30#include <linux/delay.h> 30#include <linux/delay.h>
31#include <linux/timer.h> 31#include <linux/timer.h>
32#include <linux/slab.h>
32 33
33#include <linux/usb/ch9.h> 34#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h> 35#include <linux/usb/gadget.h>
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index bf0f6520c6df..de8a83803505 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -194,7 +194,7 @@ enum ep_state {
194}; 194};
195 195
196struct ep_data { 196struct ep_data {
197 struct semaphore lock; 197 struct mutex lock;
198 enum ep_state state; 198 enum ep_state state;
199 atomic_t count; 199 atomic_t count;
200 struct dev_data *dev; 200 struct dev_data *dev;
@@ -298,10 +298,10 @@ get_ready_ep (unsigned f_flags, struct ep_data *epdata)
298 int val; 298 int val;
299 299
300 if (f_flags & O_NONBLOCK) { 300 if (f_flags & O_NONBLOCK) {
301 if (down_trylock (&epdata->lock) != 0) 301 if (!mutex_trylock(&epdata->lock))
302 goto nonblock; 302 goto nonblock;
303 if (epdata->state != STATE_EP_ENABLED) { 303 if (epdata->state != STATE_EP_ENABLED) {
304 up (&epdata->lock); 304 mutex_unlock(&epdata->lock);
305nonblock: 305nonblock:
306 val = -EAGAIN; 306 val = -EAGAIN;
307 } else 307 } else
@@ -309,7 +309,8 @@ nonblock:
309 return val; 309 return val;
310 } 310 }
311 311
312 if ((val = down_interruptible (&epdata->lock)) < 0) 312 val = mutex_lock_interruptible(&epdata->lock);
313 if (val < 0)
313 return val; 314 return val;
314 315
315 switch (epdata->state) { 316 switch (epdata->state) {
@@ -323,7 +324,7 @@ nonblock:
323 // FALLTHROUGH 324 // FALLTHROUGH
324 case STATE_EP_UNBOUND: /* clean disconnect */ 325 case STATE_EP_UNBOUND: /* clean disconnect */
325 val = -ENODEV; 326 val = -ENODEV;
326 up (&epdata->lock); 327 mutex_unlock(&epdata->lock);
327 } 328 }
328 return val; 329 return val;
329} 330}
@@ -393,7 +394,7 @@ ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
393 if (likely (data->ep != NULL)) 394 if (likely (data->ep != NULL))
394 usb_ep_set_halt (data->ep); 395 usb_ep_set_halt (data->ep);
395 spin_unlock_irq (&data->dev->lock); 396 spin_unlock_irq (&data->dev->lock);
396 up (&data->lock); 397 mutex_unlock(&data->lock);
397 return -EBADMSG; 398 return -EBADMSG;
398 } 399 }
399 400
@@ -411,7 +412,7 @@ ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
411 value = -EFAULT; 412 value = -EFAULT;
412 413
413free1: 414free1:
414 up (&data->lock); 415 mutex_unlock(&data->lock);
415 kfree (kbuf); 416 kfree (kbuf);
416 return value; 417 return value;
417} 418}
@@ -436,7 +437,7 @@ ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
436 if (likely (data->ep != NULL)) 437 if (likely (data->ep != NULL))
437 usb_ep_set_halt (data->ep); 438 usb_ep_set_halt (data->ep);
438 spin_unlock_irq (&data->dev->lock); 439 spin_unlock_irq (&data->dev->lock);
439 up (&data->lock); 440 mutex_unlock(&data->lock);
440 return -EBADMSG; 441 return -EBADMSG;
441 } 442 }
442 443
@@ -455,7 +456,7 @@ ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
455 VDEBUG (data->dev, "%s write %zu IN, status %d\n", 456 VDEBUG (data->dev, "%s write %zu IN, status %d\n",
456 data->name, len, (int) value); 457 data->name, len, (int) value);
457free1: 458free1:
458 up (&data->lock); 459 mutex_unlock(&data->lock);
459 kfree (kbuf); 460 kfree (kbuf);
460 return value; 461 return value;
461} 462}
@@ -466,7 +467,8 @@ ep_release (struct inode *inode, struct file *fd)
466 struct ep_data *data = fd->private_data; 467 struct ep_data *data = fd->private_data;
467 int value; 468 int value;
468 469
469 if ((value = down_interruptible(&data->lock)) < 0) 470 value = mutex_lock_interruptible(&data->lock);
471 if (value < 0)
470 return value; 472 return value;
471 473
472 /* clean up if this can be reopened */ 474 /* clean up if this can be reopened */
@@ -476,7 +478,7 @@ ep_release (struct inode *inode, struct file *fd)
476 data->hs_desc.bDescriptorType = 0; 478 data->hs_desc.bDescriptorType = 0;
477 usb_ep_disable(data->ep); 479 usb_ep_disable(data->ep);
478 } 480 }
479 up (&data->lock); 481 mutex_unlock(&data->lock);
480 put_ep (data); 482 put_ep (data);
481 return 0; 483 return 0;
482} 484}
@@ -507,7 +509,7 @@ static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
507 } else 509 } else
508 status = -ENODEV; 510 status = -ENODEV;
509 spin_unlock_irq (&data->dev->lock); 511 spin_unlock_irq (&data->dev->lock);
510 up (&data->lock); 512 mutex_unlock(&data->lock);
511 return status; 513 return status;
512} 514}
513 515
@@ -673,7 +675,7 @@ fail:
673 value = -ENODEV; 675 value = -ENODEV;
674 spin_unlock_irq(&epdata->dev->lock); 676 spin_unlock_irq(&epdata->dev->lock);
675 677
676 up(&epdata->lock); 678 mutex_unlock(&epdata->lock);
677 679
678 if (unlikely(value)) { 680 if (unlikely(value)) {
679 kfree(priv); 681 kfree(priv);
@@ -765,7 +767,8 @@ ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
765 u32 tag; 767 u32 tag;
766 int value, length = len; 768 int value, length = len;
767 769
768 if ((value = down_interruptible (&data->lock)) < 0) 770 value = mutex_lock_interruptible(&data->lock);
771 if (value < 0)
769 return value; 772 return value;
770 773
771 if (data->state != STATE_EP_READY) { 774 if (data->state != STATE_EP_READY) {
@@ -854,7 +857,7 @@ fail:
854 data->desc.bDescriptorType = 0; 857 data->desc.bDescriptorType = 0;
855 data->hs_desc.bDescriptorType = 0; 858 data->hs_desc.bDescriptorType = 0;
856 } 859 }
857 up (&data->lock); 860 mutex_unlock(&data->lock);
858 return value; 861 return value;
859fail0: 862fail0:
860 value = -EINVAL; 863 value = -EINVAL;
@@ -870,7 +873,7 @@ ep_open (struct inode *inode, struct file *fd)
870 struct ep_data *data = inode->i_private; 873 struct ep_data *data = inode->i_private;
871 int value = -EBUSY; 874 int value = -EBUSY;
872 875
873 if (down_interruptible (&data->lock) != 0) 876 if (mutex_lock_interruptible(&data->lock) != 0)
874 return -EINTR; 877 return -EINTR;
875 spin_lock_irq (&data->dev->lock); 878 spin_lock_irq (&data->dev->lock);
876 if (data->dev->state == STATE_DEV_UNBOUND) 879 if (data->dev->state == STATE_DEV_UNBOUND)
@@ -885,7 +888,7 @@ ep_open (struct inode *inode, struct file *fd)
885 DBG (data->dev, "%s state %d\n", 888 DBG (data->dev, "%s state %d\n",
886 data->name, data->state); 889 data->name, data->state);
887 spin_unlock_irq (&data->dev->lock); 890 spin_unlock_irq (&data->dev->lock);
888 up (&data->lock); 891 mutex_unlock(&data->lock);
889 return value; 892 return value;
890} 893}
891 894
@@ -1631,7 +1634,7 @@ static int activate_ep_files (struct dev_data *dev)
1631 if (!data) 1634 if (!data)
1632 goto enomem0; 1635 goto enomem0;
1633 data->state = STATE_EP_DISABLED; 1636 data->state = STATE_EP_DISABLED;
1634 init_MUTEX (&data->lock); 1637 mutex_init(&data->lock);
1635 init_waitqueue_head (&data->wait); 1638 init_waitqueue_head (&data->wait);
1636 1639
1637 strncpy (data->name, ep->name, sizeof (data->name) - 1); 1640 strncpy (data->name, ep->name, sizeof (data->name) - 1);
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c
index 6cd3d54f5640..fded3fca793b 100644
--- a/drivers/usb/gadget/lh7a40x_udc.c
+++ b/drivers/usb/gadget/lh7a40x_udc.c
@@ -22,6 +22,7 @@
22 */ 22 */
23 23
24#include <linux/platform_device.h> 24#include <linux/platform_device.h>
25#include <linux/slab.h>
25 26
26#include "lh7a40x_udc.h" 27#include "lh7a40x_udc.h"
27 28
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c
index a8c8543d1b08..166bf71fd348 100644
--- a/drivers/usb/gadget/m66592-udc.c
+++ b/drivers/usb/gadget/m66592-udc.c
@@ -25,6 +25,7 @@
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/io.h> 26#include <linux/io.h>
27#include <linux/platform_device.h> 27#include <linux/platform_device.h>
28#include <linux/slab.h>
28#include <linux/err.h> 29#include <linux/err.h>
29#include <linux/usb/ch9.h> 30#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h> 31#include <linux/usb/gadget.h>
diff --git a/drivers/usb/gadget/mass_storage.c b/drivers/usb/gadget/mass_storage.c
new file mode 100644
index 000000000000..705cc1f76327
--- /dev/null
+++ b/drivers/usb/gadget/mass_storage.c
@@ -0,0 +1,246 @@
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 msg_thread_exits(struct fsg_common *common)
139{
140 msg_cleanup();
141 return 0;
142}
143
144static int __init msg_do_config(struct usb_configuration *c)
145{
146 struct fsg_common *common;
147 struct fsg_config config;
148 int ret;
149
150 if (gadget_is_otg(c->cdev->gadget)) {
151 c->descriptors = otg_desc;
152 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
153 }
154
155 fsg_config_from_params(&config, &mod_data);
156 config.thread_exits = msg_thread_exits;
157 common = fsg_common_init(0, c->cdev, &config);
158 if (IS_ERR(common))
159 return PTR_ERR(common);
160
161 ret = fsg_add(c->cdev, c, common);
162 fsg_common_put(common);
163 return ret;
164}
165
166static struct usb_configuration msg_config_driver = {
167 .label = "Linux File-Backed Storage",
168 .bind = msg_do_config,
169 .bConfigurationValue = 1,
170 /* .iConfiguration = DYNAMIC */
171 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
172};
173
174
175
176/****************************** Gadget Bind ******************************/
177
178
179static int __init msg_bind(struct usb_composite_dev *cdev)
180{
181 struct usb_gadget *gadget = cdev->gadget;
182 int status;
183
184 /* Allocate string descriptor numbers ... note that string
185 * contents can be overridden by the composite_dev glue.
186 */
187
188 /* device descriptor strings: manufacturer, product */
189 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
190 init_utsname()->sysname, init_utsname()->release,
191 gadget->name);
192 status = usb_string_id(cdev);
193 if (status < 0)
194 return status;
195 strings_dev[STRING_MANUFACTURER_IDX].id = status;
196 msg_device_desc.iManufacturer = status;
197
198 status = usb_string_id(cdev);
199 if (status < 0)
200 return status;
201 strings_dev[STRING_PRODUCT_IDX].id = status;
202 msg_device_desc.iProduct = status;
203
204 status = usb_string_id(cdev);
205 if (status < 0)
206 return status;
207 strings_dev[STRING_CONFIGURATION_IDX].id = status;
208 msg_config_driver.iConfiguration = status;
209
210 /* register our second configuration */
211 status = usb_add_config(cdev, &msg_config_driver);
212 if (status < 0)
213 return status;
214
215 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
216 set_bit(0, &msg_registered);
217 return 0;
218}
219
220
221/****************************** Some noise ******************************/
222
223
224static struct usb_composite_driver msg_driver = {
225 .name = "g_mass_storage",
226 .dev = &msg_device_desc,
227 .strings = dev_strings,
228 .bind = msg_bind,
229};
230
231MODULE_DESCRIPTION(DRIVER_DESC);
232MODULE_AUTHOR("Michal Nazarewicz");
233MODULE_LICENSE("GPL");
234
235static int __init msg_init(void)
236{
237 return usb_composite_register(&msg_driver);
238}
239module_init(msg_init);
240
241static void msg_cleanup(void)
242{
243 if (test_and_clear_bit(0, &msg_registered))
244 usb_composite_unregister(&msg_driver);
245}
246module_exit(msg_cleanup);
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
new file mode 100644
index 000000000000..a930d7fd7e7a
--- /dev/null
+++ b/drivers/usb/gadget/multi.c
@@ -0,0 +1,356 @@
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_G_MULTI_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
215 return 0;
216}
217
218static struct usb_configuration cdc_config_driver = {
219 .label = "Multifunction Composite (CDC + MS + ACM)",
220 .bind = cdc_do_config,
221 .bConfigurationValue = 1,
222 /* .iConfiguration = DYNAMIC */
223 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
224};
225
226#endif
227
228
229
230/****************************** Gadget Bind ******************************/
231
232
233static int __init multi_bind(struct usb_composite_dev *cdev)
234{
235 struct usb_gadget *gadget = cdev->gadget;
236 int status, gcnum;
237
238 if (!can_support_ecm(cdev->gadget)) {
239 dev_err(&gadget->dev, "controller '%s' not usable\n",
240 gadget->name);
241 return -EINVAL;
242 }
243
244 /* set up network link layer */
245 status = gether_setup(cdev->gadget, hostaddr);
246 if (status < 0)
247 return status;
248
249 /* set up serial link layer */
250 status = gserial_setup(cdev->gadget, 1);
251 if (status < 0)
252 goto fail0;
253
254 /* set up mass storage function */
255 fsg_common = fsg_common_from_params(0, cdev, &mod_data);
256 if (IS_ERR(fsg_common)) {
257 status = PTR_ERR(fsg_common);
258 goto fail1;
259 }
260
261
262 gcnum = usb_gadget_controller_number(gadget);
263 if (gcnum >= 0)
264 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
265 else {
266 /* We assume that can_support_ecm() tells the truth;
267 * but if the controller isn't recognized at all then
268 * that assumption is a bit more likely to be wrong.
269 */
270 WARNING(cdev, "controller '%s' not recognized\n",
271 gadget->name);
272 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
273 }
274
275
276 /* Allocate string descriptor numbers ... note that string
277 * contents can be overridden by the composite_dev glue.
278 */
279
280 /* device descriptor strings: manufacturer, product */
281 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
282 init_utsname()->sysname, init_utsname()->release,
283 gadget->name);
284 status = usb_string_id(cdev);
285 if (status < 0)
286 goto fail2;
287 strings_dev[STRING_MANUFACTURER_IDX].id = status;
288 device_desc.iManufacturer = status;
289
290 status = usb_string_id(cdev);
291 if (status < 0)
292 goto fail2;
293 strings_dev[STRING_PRODUCT_IDX].id = status;
294 device_desc.iProduct = status;
295
296#ifdef USB_ETH_RNDIS
297 /* register our first configuration */
298 status = usb_add_config(cdev, &rndis_config_driver);
299 if (status < 0)
300 goto fail2;
301#endif
302
303#ifdef CONFIG_USB_G_MULTI_CDC
304 /* register our second configuration */
305 status = usb_add_config(cdev, &cdc_config_driver);
306 if (status < 0)
307 goto fail2;
308#endif
309
310 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
311 fsg_common_put(fsg_common);
312 return 0;
313
314fail2:
315 fsg_common_put(fsg_common);
316fail1:
317 gserial_cleanup();
318fail0:
319 gether_cleanup();
320 return status;
321}
322
323static int __exit multi_unbind(struct usb_composite_dev *cdev)
324{
325 gserial_cleanup();
326 gether_cleanup();
327 return 0;
328}
329
330
331/****************************** Some noise ******************************/
332
333
334static struct usb_composite_driver multi_driver = {
335 .name = "g_multi",
336 .dev = &device_desc,
337 .strings = dev_strings,
338 .bind = multi_bind,
339 .unbind = __exit_p(multi_unbind),
340};
341
342MODULE_DESCRIPTION(DRIVER_DESC);
343MODULE_AUTHOR("Michal Nazarewicz");
344MODULE_LICENSE("GPL");
345
346static int __init g_multi_init(void)
347{
348 return usb_composite_register(&multi_driver);
349}
350module_init(g_multi_init);
351
352static void __exit g_multi_cleanup(void)
353{
354 usb_composite_unregister(&multi_driver);
355}
356module_exit(g_multi_cleanup);
diff --git a/drivers/usb/gadget/nokia.c b/drivers/usb/gadget/nokia.c
new file mode 100644
index 000000000000..7d6b66a85724
--- /dev/null
+++ b/drivers/usb/gadget/nokia.c
@@ -0,0 +1,259 @@
1/*
2 * nokia.c -- Nokia Composite Gadget Driver
3 *
4 * Copyright (C) 2008-2010 Nokia Corporation
5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6 *
7 * This gadget driver borrows from serial.c which is:
8 *
9 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
10 * Copyright (C) 2008 by David Brownell
11 * Copyright (C) 2008 by Nokia Corporation
12 *
13 * This software is distributed under the terms of the GNU General
14 * Public License ("GPL") as published by the Free Software Foundation,
15 * version 2 of that License.
16 */
17
18#include <linux/kernel.h>
19#include <linux/utsname.h>
20#include <linux/device.h>
21
22#include "u_serial.h"
23#include "u_ether.h"
24#include "u_phonet.h"
25#include "gadget_chips.h"
26
27/* Defines */
28
29#define NOKIA_VERSION_NUM 0x0211
30#define NOKIA_LONG_NAME "N900 (PC-Suite Mode)"
31
32/*-------------------------------------------------------------------------*/
33
34/*
35 * Kbuild is not very cooperative with respect to linking separately
36 * compiled library objects into one module. So for now we won't use
37 * separate compilation ... ensuring init/exit sections work to shrink
38 * the runtime footprint, and giving us at least some parts of what
39 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
40 */
41#include "composite.c"
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45
46#include "u_serial.c"
47#include "f_acm.c"
48#include "f_ecm.c"
49#include "f_obex.c"
50#include "f_serial.c"
51#include "f_phonet.c"
52#include "u_ether.c"
53
54/*-------------------------------------------------------------------------*/
55
56#define NOKIA_VENDOR_ID 0x0421 /* Nokia */
57#define NOKIA_PRODUCT_ID 0x01c8 /* Nokia Gadget */
58
59/* string IDs are assigned dynamically */
60
61#define STRING_MANUFACTURER_IDX 0
62#define STRING_PRODUCT_IDX 1
63#define STRING_DESCRIPTION_IDX 2
64
65static char manufacturer_nokia[] = "Nokia";
66static const char product_nokia[] = NOKIA_LONG_NAME;
67static const char description_nokia[] = "PC-Suite Configuration";
68
69static struct usb_string strings_dev[] = {
70 [STRING_MANUFACTURER_IDX].s = manufacturer_nokia,
71 [STRING_PRODUCT_IDX].s = NOKIA_LONG_NAME,
72 [STRING_DESCRIPTION_IDX].s = description_nokia,
73 { } /* end of list */
74};
75
76static struct usb_gadget_strings stringtab_dev = {
77 .language = 0x0409, /* en-us */
78 .strings = strings_dev,
79};
80
81static struct usb_gadget_strings *dev_strings[] = {
82 &stringtab_dev,
83 NULL,
84};
85
86static struct usb_device_descriptor device_desc = {
87 .bLength = USB_DT_DEVICE_SIZE,
88 .bDescriptorType = USB_DT_DEVICE,
89 .bcdUSB = __constant_cpu_to_le16(0x0200),
90 .bDeviceClass = USB_CLASS_COMM,
91 .idVendor = __constant_cpu_to_le16(NOKIA_VENDOR_ID),
92 .idProduct = __constant_cpu_to_le16(NOKIA_PRODUCT_ID),
93 /* .iManufacturer = DYNAMIC */
94 /* .iProduct = DYNAMIC */
95 .bNumConfigurations = 1,
96};
97
98/*-------------------------------------------------------------------------*/
99
100/* Module */
101MODULE_DESCRIPTION("Nokia composite gadget driver for N900");
102MODULE_AUTHOR("Felipe Balbi");
103MODULE_LICENSE("GPL");
104
105/*-------------------------------------------------------------------------*/
106
107static u8 hostaddr[ETH_ALEN];
108
109static int __init nokia_bind_config(struct usb_configuration *c)
110{
111 int status = 0;
112
113 status = phonet_bind_config(c);
114 if (status)
115 printk(KERN_DEBUG "could not bind phonet config\n");
116
117 status = obex_bind_config(c, 0);
118 if (status)
119 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
120
121 status = obex_bind_config(c, 1);
122 if (status)
123 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
124
125 status = acm_bind_config(c, 2);
126 if (status)
127 printk(KERN_DEBUG "could not bind acm config\n");
128
129 status = ecm_bind_config(c, hostaddr);
130 if (status)
131 printk(KERN_DEBUG "could not bind ecm config\n");
132
133 return status;
134}
135
136static struct usb_configuration nokia_config_500ma_driver = {
137 .label = "Bus Powered",
138 .bind = nokia_bind_config,
139 .bConfigurationValue = 1,
140 /* .iConfiguration = DYNAMIC */
141 .bmAttributes = USB_CONFIG_ATT_ONE,
142 .bMaxPower = 250, /* 500mA */
143};
144
145static struct usb_configuration nokia_config_100ma_driver = {
146 .label = "Self Powered",
147 .bind = nokia_bind_config,
148 .bConfigurationValue = 2,
149 /* .iConfiguration = DYNAMIC */
150 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
151 .bMaxPower = 50, /* 100 mA */
152};
153
154static int __init nokia_bind(struct usb_composite_dev *cdev)
155{
156 int gcnum;
157 struct usb_gadget *gadget = cdev->gadget;
158 int status;
159
160 status = gphonet_setup(cdev->gadget);
161 if (status < 0)
162 goto err_phonet;
163
164 status = gserial_setup(cdev->gadget, 3);
165 if (status < 0)
166 goto err_serial;
167
168 status = gether_setup(cdev->gadget, hostaddr);
169 if (status < 0)
170 goto err_ether;
171
172 status = usb_string_id(cdev);
173 if (status < 0)
174 goto err_usb;
175 strings_dev[STRING_MANUFACTURER_IDX].id = status;
176
177 device_desc.iManufacturer = status;
178
179 status = usb_string_id(cdev);
180 if (status < 0)
181 goto err_usb;
182 strings_dev[STRING_PRODUCT_IDX].id = status;
183
184 device_desc.iProduct = status;
185
186 /* config description */
187 status = usb_string_id(cdev);
188 if (status < 0)
189 goto err_usb;
190 strings_dev[STRING_DESCRIPTION_IDX].id = status;
191
192 nokia_config_500ma_driver.iConfiguration = status;
193 nokia_config_100ma_driver.iConfiguration = status;
194
195 /* set up other descriptors */
196 gcnum = usb_gadget_controller_number(gadget);
197 if (gcnum >= 0)
198 device_desc.bcdDevice = cpu_to_le16(NOKIA_VERSION_NUM);
199 else {
200 /* this should only work with hw that supports altsettings
201 * and several endpoints, anything else, panic.
202 */
203 pr_err("nokia_bind: controller '%s' not recognized\n",
204 gadget->name);
205 goto err_usb;
206 }
207
208 /* finaly register the configuration */
209 status = usb_add_config(cdev, &nokia_config_500ma_driver);
210 if (status < 0)
211 goto err_usb;
212
213 status = usb_add_config(cdev, &nokia_config_100ma_driver);
214 if (status < 0)
215 goto err_usb;
216
217 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
218
219 return 0;
220
221err_usb:
222 gether_cleanup();
223err_ether:
224 gserial_cleanup();
225err_serial:
226 gphonet_cleanup();
227err_phonet:
228 return status;
229}
230
231static int __exit nokia_unbind(struct usb_composite_dev *cdev)
232{
233 gphonet_cleanup();
234 gserial_cleanup();
235 gether_cleanup();
236
237 return 0;
238}
239
240static struct usb_composite_driver nokia_driver = {
241 .name = "g_nokia",
242 .dev = &device_desc,
243 .strings = dev_strings,
244 .bind = nokia_bind,
245 .unbind = __exit_p(nokia_unbind),
246};
247
248static int __init nokia_init(void)
249{
250 return usb_composite_register(&nokia_driver);
251}
252module_init(nokia_init);
253
254static void __exit nokia_cleanup(void)
255{
256 usb_composite_unregister(&nokia_driver);
257}
258module_exit(nokia_cleanup);
259
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index a2db0e174f2c..f81e4f025f23 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -52,9 +52,9 @@
52#include <asm/unaligned.h> 52#include <asm/unaligned.h>
53#include <asm/mach-types.h> 53#include <asm/mach-types.h>
54 54
55#include <mach/dma.h> 55#include <plat/dma.h>
56#include <mach/usb.h> 56#include <plat/usb.h>
57#include <mach/control.h> 57#include <plat/control.h>
58 58
59#include "omap_udc.h" 59#include "omap_udc.h"
60 60
@@ -2098,6 +2098,7 @@ static inline int machine_without_vbus_sense(void)
2098 || machine_is_omap_h4() 2098 || machine_is_omap_h4()
2099#endif 2099#endif
2100 || machine_is_sx1() 2100 || machine_is_sx1()
2101 || cpu_is_omap7xx() /* No known omap7xx boards with vbus sense */
2101 ); 2102 );
2102} 2103}
2103 2104
@@ -2838,6 +2839,16 @@ static int __init omap_udc_probe(struct platform_device *pdev)
2838 udelay(100); 2839 udelay(100);
2839 } 2840 }
2840 2841
2842 if (cpu_is_omap7xx()) {
2843 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2844 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2845 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2846 /* can't use omap_udc_enable_clock yet */
2847 clk_enable(dc_clk);
2848 clk_enable(hhc_clk);
2849 udelay(100);
2850 }
2851
2841 INFO("OMAP UDC rev %d.%d%s\n", 2852 INFO("OMAP UDC rev %d.%d%s\n",
2842 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf, 2853 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
2843 config->otg ? ", Mini-AB" : ""); 2854 config->otg ? ", Mini-AB" : "");
@@ -2970,7 +2981,7 @@ known:
2970 goto cleanup3; 2981 goto cleanup3;
2971 } 2982 }
2972#endif 2983#endif
2973 if (cpu_is_omap16xx()) { 2984 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
2974 udc->dc_clk = dc_clk; 2985 udc->dc_clk = dc_clk;
2975 udc->hhc_clk = hhc_clk; 2986 udc->hhc_clk = hhc_clk;
2976 clk_disable(hhc_clk); 2987 clk_disable(hhc_clk);
@@ -3008,7 +3019,7 @@ cleanup0:
3008 if (xceiv) 3019 if (xceiv)
3009 otg_put_transceiver(xceiv); 3020 otg_put_transceiver(xceiv);
3010 3021
3011 if (cpu_is_omap16xx() || cpu_is_omap24xx()) { 3022 if (cpu_is_omap16xx() || cpu_is_omap24xx() || cpu_is_omap7xx()) {
3012 clk_disable(hhc_clk); 3023 clk_disable(hhc_clk);
3013 clk_disable(dc_clk); 3024 clk_disable(dc_clk);
3014 clk_put(hhc_clk); 3025 clk_put(hhc_clk);
@@ -3115,6 +3126,10 @@ static struct platform_driver udc_driver = {
3115 3126
3116static int __init udc_init(void) 3127static int __init udc_init(void)
3117{ 3128{
3129 /* Disable DMA for omap7xx -- it doesn't work right. */
3130 if (cpu_is_omap7xx())
3131 use_dma = 0;
3132
3118 INFO("%s, version: " DRIVER_VERSION 3133 INFO("%s, version: " DRIVER_VERSION
3119#ifdef USE_ISO 3134#ifdef USE_ISO
3120 " (iso)" 3135 " (iso)"
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index 2d867fd22413..6b8bf8c781c4 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -949,12 +949,6 @@ printer_set_config(struct printer_dev *dev, unsigned number)
949 int result = 0; 949 int result = 0;
950 struct usb_gadget *gadget = dev->gadget; 950 struct usb_gadget *gadget = dev->gadget;
951 951
952 if (gadget_is_sa1100(gadget) && dev->config) {
953 /* tx fifo is full, but we can't clear it...*/
954 INFO(dev, "can't change configurations\n");
955 return -ESPIPE;
956 }
957
958 switch (number) { 952 switch (number) {
959 case DEV_CONFIG_VALUE: 953 case DEV_CONFIG_VALUE:
960 result = 0; 954 result = 0;
@@ -1033,12 +1027,6 @@ set_interface(struct printer_dev *dev, unsigned number)
1033{ 1027{
1034 int result = 0; 1028 int result = 0;
1035 1029
1036 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1037 /* tx fifo is full, but we can't clear it...*/
1038 INFO(dev, "can't change interfaces\n");
1039 return -ESPIPE;
1040 }
1041
1042 /* Free the current interface */ 1030 /* Free the current interface */
1043 switch (dev->interface) { 1031 switch (dev->interface) {
1044 case PRINTER_INTERFACE: 1032 case PRINTER_INTERFACE:
@@ -1392,12 +1380,6 @@ printer_bind(struct usb_gadget *gadget)
1392 goto fail; 1380 goto fail;
1393 } 1381 }
1394 1382
1395 if (gadget_is_sa1100(gadget)) {
1396 /* hardware can't write zero length packets. */
1397 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1398 goto fail;
1399 }
1400
1401 gcnum = usb_gadget_controller_number(gadget); 1383 gcnum = usb_gadget_controller_number(gadget);
1402 if (gcnum >= 0) { 1384 if (gcnum >= 0) {
1403 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); 1385 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index e6fedbd5a654..be5fb34d9602 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -65,6 +65,10 @@
65#include <mach/pxa25x-udc.h> 65#include <mach/pxa25x-udc.h>
66#endif 66#endif
67 67
68#ifdef CONFIG_ARCH_LUBBOCK
69#include <mach/lubbock.h>
70#endif
71
68#include <asm/mach/udc_pxa2xx.h> 72#include <asm/mach/udc_pxa2xx.h>
69 73
70 74
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 1937d8c7b433..85b0d8921eae 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -31,6 +31,7 @@
31#include <linux/clk.h> 31#include <linux/clk.h>
32#include <linux/irq.h> 32#include <linux/irq.h>
33#include <linux/gpio.h> 33#include <linux/gpio.h>
34#include <linux/slab.h>
34 35
35#include <asm/byteorder.h> 36#include <asm/byteorder.h>
36#include <mach/hardware.h> 37#include <mach/hardware.h>
@@ -742,13 +743,17 @@ static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
742 * @ep: pxa physical endpoint 743 * @ep: pxa physical endpoint
743 * @req: pxa request 744 * @req: pxa request
744 * @status: usb request status sent to gadget API 745 * @status: usb request status sent to gadget API
746 * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
745 * 747 *
746 * Context: ep->lock held 748 * Context: ep->lock held if flags not NULL, else ep->lock released
747 * 749 *
748 * Retire a pxa27x usb request. Endpoint must be locked. 750 * Retire a pxa27x usb request. Endpoint must be locked.
749 */ 751 */
750static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status) 752static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
753 unsigned long *pflags)
751{ 754{
755 unsigned long flags;
756
752 ep_del_request(ep, req); 757 ep_del_request(ep, req);
753 if (likely(req->req.status == -EINPROGRESS)) 758 if (likely(req->req.status == -EINPROGRESS))
754 req->req.status = status; 759 req->req.status = status;
@@ -760,38 +765,48 @@ static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status)
760 &req->req, status, 765 &req->req, status,
761 req->req.actual, req->req.length); 766 req->req.actual, req->req.length);
762 767
768 if (pflags)
769 spin_unlock_irqrestore(&ep->lock, *pflags);
770 local_irq_save(flags);
763 req->req.complete(&req->udc_usb_ep->usb_ep, &req->req); 771 req->req.complete(&req->udc_usb_ep->usb_ep, &req->req);
772 local_irq_restore(flags);
773 if (pflags)
774 spin_lock_irqsave(&ep->lock, *pflags);
764} 775}
765 776
766/** 777/**
767 * ep_end_out_req - Ends endpoint OUT request 778 * ep_end_out_req - Ends endpoint OUT request
768 * @ep: physical endpoint 779 * @ep: physical endpoint
769 * @req: pxa request 780 * @req: pxa request
781 * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
770 * 782 *
771 * Context: ep->lock held 783 * Context: ep->lock held or released (see req_done())
772 * 784 *
773 * Ends endpoint OUT request (completes usb request). 785 * Ends endpoint OUT request (completes usb request).
774 */ 786 */
775static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req) 787static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
788 unsigned long *pflags)
776{ 789{
777 inc_ep_stats_reqs(ep, !USB_DIR_IN); 790 inc_ep_stats_reqs(ep, !USB_DIR_IN);
778 req_done(ep, req, 0); 791 req_done(ep, req, 0, pflags);
779} 792}
780 793
781/** 794/**
782 * ep0_end_out_req - Ends control endpoint OUT request (ends data stage) 795 * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
783 * @ep: physical endpoint 796 * @ep: physical endpoint
784 * @req: pxa request 797 * @req: pxa request
798 * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
785 * 799 *
786 * Context: ep->lock held 800 * Context: ep->lock held or released (see req_done())
787 * 801 *
788 * Ends control endpoint OUT request (completes usb request), and puts 802 * Ends control endpoint OUT request (completes usb request), and puts
789 * control endpoint into idle state 803 * control endpoint into idle state
790 */ 804 */
791static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req) 805static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
806 unsigned long *pflags)
792{ 807{
793 set_ep0state(ep->dev, OUT_STATUS_STAGE); 808 set_ep0state(ep->dev, OUT_STATUS_STAGE);
794 ep_end_out_req(ep, req); 809 ep_end_out_req(ep, req, pflags);
795 ep0_idle(ep->dev); 810 ep0_idle(ep->dev);
796} 811}
797 812
@@ -799,31 +814,35 @@ static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req)
799 * ep_end_in_req - Ends endpoint IN request 814 * ep_end_in_req - Ends endpoint IN request
800 * @ep: physical endpoint 815 * @ep: physical endpoint
801 * @req: pxa request 816 * @req: pxa request
817 * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
802 * 818 *
803 * Context: ep->lock held 819 * Context: ep->lock held or released (see req_done())
804 * 820 *
805 * Ends endpoint IN request (completes usb request). 821 * Ends endpoint IN request (completes usb request).
806 */ 822 */
807static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req) 823static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
824 unsigned long *pflags)
808{ 825{
809 inc_ep_stats_reqs(ep, USB_DIR_IN); 826 inc_ep_stats_reqs(ep, USB_DIR_IN);
810 req_done(ep, req, 0); 827 req_done(ep, req, 0, pflags);
811} 828}
812 829
813/** 830/**
814 * ep0_end_in_req - Ends control endpoint IN request (ends data stage) 831 * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
815 * @ep: physical endpoint 832 * @ep: physical endpoint
816 * @req: pxa request 833 * @req: pxa request
834 * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
817 * 835 *
818 * Context: ep->lock held 836 * Context: ep->lock held or released (see req_done())
819 * 837 *
820 * Ends control endpoint IN request (completes usb request), and puts 838 * Ends control endpoint IN request (completes usb request), and puts
821 * control endpoint into status state 839 * control endpoint into status state
822 */ 840 */
823static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req) 841static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
842 unsigned long *pflags)
824{ 843{
825 set_ep0state(ep->dev, IN_STATUS_STAGE); 844 set_ep0state(ep->dev, IN_STATUS_STAGE);
826 ep_end_in_req(ep, req); 845 ep_end_in_req(ep, req, pflags);
827} 846}
828 847
829/** 848/**
@@ -831,19 +850,22 @@ static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req)
831 * @ep: pxa endpoint 850 * @ep: pxa endpoint
832 * @status: usb request status 851 * @status: usb request status
833 * 852 *
834 * Context: ep->lock held 853 * Context: ep->lock released
835 * 854 *
836 * Dequeues all requests on an endpoint. As a side effect, interrupts will be 855 * Dequeues all requests on an endpoint. As a side effect, interrupts will be
837 * disabled on that endpoint (because no more requests). 856 * disabled on that endpoint (because no more requests).
838 */ 857 */
839static void nuke(struct pxa_ep *ep, int status) 858static void nuke(struct pxa_ep *ep, int status)
840{ 859{
841 struct pxa27x_request *req; 860 struct pxa27x_request *req;
861 unsigned long flags;
842 862
863 spin_lock_irqsave(&ep->lock, flags);
843 while (!list_empty(&ep->queue)) { 864 while (!list_empty(&ep->queue)) {
844 req = list_entry(ep->queue.next, struct pxa27x_request, queue); 865 req = list_entry(ep->queue.next, struct pxa27x_request, queue);
845 req_done(ep, req, status); 866 req_done(ep, req, status, &flags);
846 } 867 }
868 spin_unlock_irqrestore(&ep->lock, flags);
847} 869}
848 870
849/** 871/**
@@ -1123,6 +1145,7 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1123 int rc = 0; 1145 int rc = 0;
1124 int is_first_req; 1146 int is_first_req;
1125 unsigned length; 1147 unsigned length;
1148 int recursion_detected;
1126 1149
1127 req = container_of(_req, struct pxa27x_request, req); 1150 req = container_of(_req, struct pxa27x_request, req);
1128 udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep); 1151 udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
@@ -1152,6 +1175,7 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1152 return -EMSGSIZE; 1175 return -EMSGSIZE;
1153 1176
1154 spin_lock_irqsave(&ep->lock, flags); 1177 spin_lock_irqsave(&ep->lock, flags);
1178 recursion_detected = ep->in_handle_ep;
1155 1179
1156 is_first_req = list_empty(&ep->queue); 1180 is_first_req = list_empty(&ep->queue);
1157 ep_dbg(ep, "queue req %p(first=%s), len %d buf %p\n", 1181 ep_dbg(ep, "queue req %p(first=%s), len %d buf %p\n",
@@ -1161,12 +1185,12 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1161 if (!ep->enabled) { 1185 if (!ep->enabled) {
1162 _req->status = -ESHUTDOWN; 1186 _req->status = -ESHUTDOWN;
1163 rc = -ESHUTDOWN; 1187 rc = -ESHUTDOWN;
1164 goto out; 1188 goto out_locked;
1165 } 1189 }
1166 1190
1167 if (req->in_use) { 1191 if (req->in_use) {
1168 ep_err(ep, "refusing to queue req %p (already queued)\n", req); 1192 ep_err(ep, "refusing to queue req %p (already queued)\n", req);
1169 goto out; 1193 goto out_locked;
1170 } 1194 }
1171 1195
1172 length = _req->length; 1196 length = _req->length;
@@ -1174,12 +1198,13 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1174 _req->actual = 0; 1198 _req->actual = 0;
1175 1199
1176 ep_add_request(ep, req); 1200 ep_add_request(ep, req);
1201 spin_unlock_irqrestore(&ep->lock, flags);
1177 1202
1178 if (is_ep0(ep)) { 1203 if (is_ep0(ep)) {
1179 switch (dev->ep0state) { 1204 switch (dev->ep0state) {
1180 case WAIT_ACK_SET_CONF_INTERF: 1205 case WAIT_ACK_SET_CONF_INTERF:
1181 if (length == 0) { 1206 if (length == 0) {
1182 ep_end_in_req(ep, req); 1207 ep_end_in_req(ep, req, NULL);
1183 } else { 1208 } else {
1184 ep_err(ep, "got a request of %d bytes while" 1209 ep_err(ep, "got a request of %d bytes while"
1185 "in state WAIT_ACK_SET_CONF_INTERF\n", 1210 "in state WAIT_ACK_SET_CONF_INTERF\n",
@@ -1192,12 +1217,12 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1192 case IN_DATA_STAGE: 1217 case IN_DATA_STAGE:
1193 if (!ep_is_full(ep)) 1218 if (!ep_is_full(ep))
1194 if (write_ep0_fifo(ep, req)) 1219 if (write_ep0_fifo(ep, req))
1195 ep0_end_in_req(ep, req); 1220 ep0_end_in_req(ep, req, NULL);
1196 break; 1221 break;
1197 case OUT_DATA_STAGE: 1222 case OUT_DATA_STAGE:
1198 if ((length == 0) || !epout_has_pkt(ep)) 1223 if ((length == 0) || !epout_has_pkt(ep))
1199 if (read_ep0_fifo(ep, req)) 1224 if (read_ep0_fifo(ep, req))
1200 ep0_end_out_req(ep, req); 1225 ep0_end_out_req(ep, req, NULL);
1201 break; 1226 break;
1202 default: 1227 default:
1203 ep_err(ep, "odd state %s to send me a request\n", 1228 ep_err(ep, "odd state %s to send me a request\n",
@@ -1207,12 +1232,15 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1207 break; 1232 break;
1208 } 1233 }
1209 } else { 1234 } else {
1210 handle_ep(ep); 1235 if (!recursion_detected)
1236 handle_ep(ep);
1211 } 1237 }
1212 1238
1213out: 1239out:
1214 spin_unlock_irqrestore(&ep->lock, flags);
1215 return rc; 1240 return rc;
1241out_locked:
1242 spin_unlock_irqrestore(&ep->lock, flags);
1243 goto out;
1216} 1244}
1217 1245
1218/** 1246/**
@@ -1242,13 +1270,14 @@ static int pxa_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1242 /* make sure it's actually queued on this endpoint */ 1270 /* make sure it's actually queued on this endpoint */
1243 list_for_each_entry(req, &ep->queue, queue) { 1271 list_for_each_entry(req, &ep->queue, queue) {
1244 if (&req->req == _req) { 1272 if (&req->req == _req) {
1245 req_done(ep, req, -ECONNRESET);
1246 rc = 0; 1273 rc = 0;
1247 break; 1274 break;
1248 } 1275 }
1249 } 1276 }
1250 1277
1251 spin_unlock_irqrestore(&ep->lock, flags); 1278 spin_unlock_irqrestore(&ep->lock, flags);
1279 if (!rc)
1280 req_done(ep, req, -ECONNRESET, NULL);
1252 return rc; 1281 return rc;
1253} 1282}
1254 1283
@@ -1445,7 +1474,6 @@ static int pxa_ep_disable(struct usb_ep *_ep)
1445{ 1474{
1446 struct pxa_ep *ep; 1475 struct pxa_ep *ep;
1447 struct udc_usb_ep *udc_usb_ep; 1476 struct udc_usb_ep *udc_usb_ep;
1448 unsigned long flags;
1449 1477
1450 if (!_ep) 1478 if (!_ep)
1451 return -EINVAL; 1479 return -EINVAL;
@@ -1455,10 +1483,8 @@ static int pxa_ep_disable(struct usb_ep *_ep)
1455 if (!ep || is_ep0(ep) || !list_empty(&ep->queue)) 1483 if (!ep || is_ep0(ep) || !list_empty(&ep->queue))
1456 return -EINVAL; 1484 return -EINVAL;
1457 1485
1458 spin_lock_irqsave(&ep->lock, flags);
1459 ep->enabled = 0; 1486 ep->enabled = 0;
1460 nuke(ep, -ESHUTDOWN); 1487 nuke(ep, -ESHUTDOWN);
1461 spin_unlock_irqrestore(&ep->lock, flags);
1462 1488
1463 pxa_ep_fifo_flush(_ep); 1489 pxa_ep_fifo_flush(_ep);
1464 udc_usb_ep->pxa_ep = NULL; 1490 udc_usb_ep->pxa_ep = NULL;
@@ -1524,7 +1550,7 @@ static int pxa_udc_get_frame(struct usb_gadget *_gadget)
1524 * pxa_udc_wakeup - Force udc device out of suspend 1550 * pxa_udc_wakeup - Force udc device out of suspend
1525 * @_gadget: usb gadget 1551 * @_gadget: usb gadget
1526 * 1552 *
1527 * Returns 0 if succesfull, error code otherwise 1553 * Returns 0 if successfull, error code otherwise
1528 */ 1554 */
1529static int pxa_udc_wakeup(struct usb_gadget *_gadget) 1555static int pxa_udc_wakeup(struct usb_gadget *_gadget)
1530{ 1556{
@@ -1907,8 +1933,10 @@ static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1907 } u; 1933 } u;
1908 int i; 1934 int i;
1909 int have_extrabytes = 0; 1935 int have_extrabytes = 0;
1936 unsigned long flags;
1910 1937
1911 nuke(ep, -EPROTO); 1938 nuke(ep, -EPROTO);
1939 spin_lock_irqsave(&ep->lock, flags);
1912 1940
1913 /* 1941 /*
1914 * In the PXA320 manual, in the section about Back-to-Back setup 1942 * In the PXA320 manual, in the section about Back-to-Back setup
@@ -1947,10 +1975,13 @@ static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1947 /* Tell UDC to enter Data Stage */ 1975 /* Tell UDC to enter Data Stage */
1948 ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC); 1976 ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
1949 1977
1978 spin_unlock_irqrestore(&ep->lock, flags);
1950 i = udc->driver->setup(&udc->gadget, &u.r); 1979 i = udc->driver->setup(&udc->gadget, &u.r);
1980 spin_lock_irqsave(&ep->lock, flags);
1951 if (i < 0) 1981 if (i < 0)
1952 goto stall; 1982 goto stall;
1953out: 1983out:
1984 spin_unlock_irqrestore(&ep->lock, flags);
1954 return; 1985 return;
1955stall: 1986stall:
1956 ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n", 1987 ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
@@ -2055,13 +2086,13 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
2055 if (req && !ep_is_full(ep)) 2086 if (req && !ep_is_full(ep))
2056 completed = write_ep0_fifo(ep, req); 2087 completed = write_ep0_fifo(ep, req);
2057 if (completed) 2088 if (completed)
2058 ep0_end_in_req(ep, req); 2089 ep0_end_in_req(ep, req, NULL);
2059 break; 2090 break;
2060 case OUT_DATA_STAGE: /* SET_DESCRIPTOR */ 2091 case OUT_DATA_STAGE: /* SET_DESCRIPTOR */
2061 if (epout_has_pkt(ep) && req) 2092 if (epout_has_pkt(ep) && req)
2062 completed = read_ep0_fifo(ep, req); 2093 completed = read_ep0_fifo(ep, req);
2063 if (completed) 2094 if (completed)
2064 ep0_end_out_req(ep, req); 2095 ep0_end_out_req(ep, req, NULL);
2065 break; 2096 break;
2066 case STALL: 2097 case STALL:
2067 ep_write_UDCCSR(ep, UDCCSR0_FST); 2098 ep_write_UDCCSR(ep, UDCCSR0_FST);
@@ -2091,7 +2122,7 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
2091 * Tries to transfer all pending request data into the endpoint and/or 2122 * Tries to transfer all pending request data into the endpoint and/or
2092 * transfer all pending data in the endpoint into usb requests. 2123 * transfer all pending data in the endpoint into usb requests.
2093 * 2124 *
2094 * Is always called when in_interrupt() or with ep->lock held. 2125 * Is always called when in_interrupt() and with ep->lock released.
2095 */ 2126 */
2096static void handle_ep(struct pxa_ep *ep) 2127static void handle_ep(struct pxa_ep *ep)
2097{ 2128{
@@ -2100,10 +2131,17 @@ static void handle_ep(struct pxa_ep *ep)
2100 u32 udccsr; 2131 u32 udccsr;
2101 int is_in = ep->dir_in; 2132 int is_in = ep->dir_in;
2102 int loop = 0; 2133 int loop = 0;
2134 unsigned long flags;
2135
2136 spin_lock_irqsave(&ep->lock, flags);
2137 if (ep->in_handle_ep)
2138 goto recursion_detected;
2139 ep->in_handle_ep = 1;
2103 2140
2104 do { 2141 do {
2105 completed = 0; 2142 completed = 0;
2106 udccsr = udc_ep_readl(ep, UDCCSR); 2143 udccsr = udc_ep_readl(ep, UDCCSR);
2144
2107 if (likely(!list_empty(&ep->queue))) 2145 if (likely(!list_empty(&ep->queue)))
2108 req = list_entry(ep->queue.next, 2146 req = list_entry(ep->queue.next,
2109 struct pxa27x_request, queue); 2147 struct pxa27x_request, queue);
@@ -2122,15 +2160,22 @@ static void handle_ep(struct pxa_ep *ep)
2122 if (unlikely(is_in)) { 2160 if (unlikely(is_in)) {
2123 if (likely(!ep_is_full(ep))) 2161 if (likely(!ep_is_full(ep)))
2124 completed = write_fifo(ep, req); 2162 completed = write_fifo(ep, req);
2125 if (completed)
2126 ep_end_in_req(ep, req);
2127 } else { 2163 } else {
2128 if (likely(epout_has_pkt(ep))) 2164 if (likely(epout_has_pkt(ep)))
2129 completed = read_fifo(ep, req); 2165 completed = read_fifo(ep, req);
2130 if (completed) 2166 }
2131 ep_end_out_req(ep, req); 2167
2168 if (completed) {
2169 if (is_in)
2170 ep_end_in_req(ep, req, &flags);
2171 else
2172 ep_end_out_req(ep, req, &flags);
2132 } 2173 }
2133 } while (completed); 2174 } while (completed);
2175
2176 ep->in_handle_ep = 0;
2177recursion_detected:
2178 spin_unlock_irqrestore(&ep->lock, flags);
2134} 2179}
2135 2180
2136/** 2181/**
@@ -2218,9 +2263,13 @@ static void irq_handle_data(int irq, struct pxa_udc *udc)
2218 continue; 2263 continue;
2219 2264
2220 udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK)); 2265 udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK));
2221 ep = &udc->pxa_ep[i]; 2266
2222 ep->stats.irqs++; 2267 WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
2223 handle_ep(ep); 2268 if (i < ARRAY_SIZE(udc->pxa_ep)) {
2269 ep = &udc->pxa_ep[i];
2270 ep->stats.irqs++;
2271 handle_ep(ep);
2272 }
2224 } 2273 }
2225 2274
2226 for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) { 2275 for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
@@ -2228,9 +2277,12 @@ static void irq_handle_data(int irq, struct pxa_udc *udc)
2228 if (!(udcisr1 & UDCISR_INT_MASK)) 2277 if (!(udcisr1 & UDCISR_INT_MASK))
2229 continue; 2278 continue;
2230 2279
2231 ep = &udc->pxa_ep[i]; 2280 WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
2232 ep->stats.irqs++; 2281 if (i < ARRAY_SIZE(udc->pxa_ep)) {
2233 handle_ep(ep); 2282 ep = &udc->pxa_ep[i];
2283 ep->stats.irqs++;
2284 handle_ep(ep);
2285 }
2234 } 2286 }
2235 2287
2236} 2288}
@@ -2439,7 +2491,7 @@ static int __init pxa_udc_probe(struct platform_device *pdev)
2439 } 2491 }
2440 2492
2441 retval = -ENOMEM; 2493 retval = -ENOMEM;
2442 udc->regs = ioremap(regs->start, regs->end - regs->start + 1); 2494 udc->regs = ioremap(regs->start, resource_size(regs));
2443 if (!udc->regs) { 2495 if (!udc->regs) {
2444 dev_err(&pdev->dev, "Unable to map UDC I/O memory\n"); 2496 dev_err(&pdev->dev, "Unable to map UDC I/O memory\n");
2445 goto err_map; 2497 goto err_map;
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index e25225e26586..ff61e4866e8a 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -318,6 +318,11 @@ struct udc_usb_ep {
318 * @queue: requests queue 318 * @queue: requests queue
319 * @lock: lock to pxa_ep data (queues and stats) 319 * @lock: lock to pxa_ep data (queues and stats)
320 * @enabled: true when endpoint enabled (not stopped by gadget layer) 320 * @enabled: true when endpoint enabled (not stopped by gadget layer)
321 * @in_handle_ep: number of recursions of handle_ep() function
322 * Prevents deadlocks or infinite recursions of types :
323 * irq->handle_ep()->req_done()->req.complete()->pxa_ep_queue()->handle_ep()
324 * or
325 * pxa_ep_queue()->handle_ep()->req_done()->req.complete()->pxa_ep_queue()
321 * @idx: endpoint index (1 => epA, 2 => epB, ..., 24 => epX) 326 * @idx: endpoint index (1 => epA, 2 => epB, ..., 24 => epX)
322 * @name: endpoint name (for trace/debug purpose) 327 * @name: endpoint name (for trace/debug purpose)
323 * @dir_in: 1 if IN endpoint, 0 if OUT endpoint 328 * @dir_in: 1 if IN endpoint, 0 if OUT endpoint
@@ -346,6 +351,7 @@ struct pxa_ep {
346 spinlock_t lock; /* Protects this structure */ 351 spinlock_t lock; /* Protects this structure */
347 /* (queues, stats) */ 352 /* (queues, stats) */
348 unsigned enabled:1; 353 unsigned enabled:1;
354 unsigned in_handle_ep:1;
349 355
350 unsigned idx:5; 356 unsigned idx:5;
351 char *name; 357 char *name;
diff --git a/drivers/usb/gadget/r8a66597-udc.c b/drivers/usb/gadget/r8a66597-udc.c
index e220fb8091a3..888d8f166c0b 100644
--- a/drivers/usb/gadget/r8a66597-udc.c
+++ b/drivers/usb/gadget/r8a66597-udc.c
@@ -26,6 +26,8 @@
26#include <linux/io.h> 26#include <linux/io.h>
27#include <linux/platform_device.h> 27#include <linux/platform_device.h>
28#include <linux/clk.h> 28#include <linux/clk.h>
29#include <linux/err.h>
30#include <linux/slab.h>
29 31
30#include <linux/usb/ch9.h> 32#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h> 33#include <linux/usb/gadget.h>
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c
index 48267bc0b2e0..5c0d06c79a81 100644
--- a/drivers/usb/gadget/rndis.c
+++ b/drivers/usb/gadget/rndis.c
@@ -28,6 +28,7 @@
28#include <linux/init.h> 28#include <linux/init.h>
29#include <linux/list.h> 29#include <linux/list.h>
30#include <linux/proc_fs.h> 30#include <linux/proc_fs.h>
31#include <linux/slab.h>
31#include <linux/seq_file.h> 32#include <linux/seq_file.h>
32#include <linux/netdevice.h> 33#include <linux/netdevice.h>
33 34
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 4b5dbd0127f5..1f73b485732d 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -22,6 +22,7 @@
22#include <linux/seq_file.h> 22#include <linux/seq_file.h>
23#include <linux/delay.h> 23#include <linux/delay.h>
24#include <linux/io.h> 24#include <linux/io.h>
25#include <linux/slab.h>
25 26
26#include <linux/usb/ch9.h> 27#include <linux/usb/ch9.h>
27#include <linux/usb/gadget.h> 28#include <linux/usb/gadget.h>
@@ -30,7 +31,7 @@
30 31
31#include <plat/regs-usb-hsotg-phy.h> 32#include <plat/regs-usb-hsotg-phy.h>
32#include <plat/regs-usb-hsotg.h> 33#include <plat/regs-usb-hsotg.h>
33#include <plat/regs-sys.h> 34#include <mach/regs-sys.h>
34#include <plat/udc-hs.h> 35#include <plat/udc-hs.h>
35 36
36#define DMA_ADDR_INVALID (~((dma_addr_t)0)) 37#define DMA_ADDR_INVALID (~((dma_addr_t)0))
@@ -317,7 +318,8 @@ static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
317 * 318 *
318 * Allocate a new USB request structure appropriate for the specified endpoint 319 * Allocate a new USB request structure appropriate for the specified endpoint
319 */ 320 */
320struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep, gfp_t flags) 321static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
322 gfp_t flags)
321{ 323{
322 struct s3c_hsotg_req *req; 324 struct s3c_hsotg_req *req;
323 325
@@ -373,7 +375,7 @@ static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
373 req->dma = DMA_ADDR_INVALID; 375 req->dma = DMA_ADDR_INVALID;
374 hs_req->mapped = 0; 376 hs_req->mapped = 0;
375 } else { 377 } else {
376 dma_sync_single(hsotg->dev, req->dma, req->length, dir); 378 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
377 } 379 }
378} 380}
379 381
@@ -755,7 +757,7 @@ static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
755 hs_req->mapped = 1; 757 hs_req->mapped = 1;
756 req->dma = dma; 758 req->dma = dma;
757 } else { 759 } else {
758 dma_sync_single(hsotg->dev, req->dma, req->length, dir); 760 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
759 hs_req->mapped = 0; 761 hs_req->mapped = 0;
760 } 762 }
761 763
@@ -1460,7 +1462,7 @@ static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1460 * as the actual data should be sent to the memory directly and we turn 1462 * as the actual data should be sent to the memory directly and we turn
1461 * on the completion interrupts to get notifications of transfer completion. 1463 * on the completion interrupts to get notifications of transfer completion.
1462 */ 1464 */
1463void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg) 1465static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
1464{ 1466{
1465 u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP); 1467 u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP);
1466 u32 epnum, status, size; 1468 u32 epnum, status, size;
@@ -2143,6 +2145,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2143 u32 epctrl; 2145 u32 epctrl;
2144 u32 mps; 2146 u32 mps;
2145 int dir_in; 2147 int dir_in;
2148 int ret = 0;
2146 2149
2147 dev_dbg(hsotg->dev, 2150 dev_dbg(hsotg->dev,
2148 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", 2151 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
@@ -2194,7 +2197,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2194 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 2197 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2195 case USB_ENDPOINT_XFER_ISOC: 2198 case USB_ENDPOINT_XFER_ISOC:
2196 dev_err(hsotg->dev, "no current ISOC support\n"); 2199 dev_err(hsotg->dev, "no current ISOC support\n");
2197 return -EINVAL; 2200 ret = -EINVAL;
2201 goto out;
2198 2202
2199 case USB_ENDPOINT_XFER_BULK: 2203 case USB_ENDPOINT_XFER_BULK:
2200 epctrl |= S3C_DxEPCTL_EPType_Bulk; 2204 epctrl |= S3C_DxEPCTL_EPType_Bulk;
@@ -2233,8 +2237,9 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2233 /* enable the endpoint interrupt */ 2237 /* enable the endpoint interrupt */
2234 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1); 2238 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2235 2239
2240out:
2236 spin_unlock_irqrestore(&hs_ep->lock, flags); 2241 spin_unlock_irqrestore(&hs_ep->lock, flags);
2237 return 0; 2242 return ret;
2238} 2243}
2239 2244
2240static int s3c_hsotg_ep_disable(struct usb_ep *ep) 2245static int s3c_hsotg_ep_disable(struct usb_ep *ep)
@@ -2582,6 +2587,7 @@ err:
2582 hsotg->gadget.dev.driver = NULL; 2587 hsotg->gadget.dev.driver = NULL;
2583 return ret; 2588 return ret;
2584} 2589}
2590EXPORT_SYMBOL(usb_gadget_register_driver);
2585 2591
2586int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 2592int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2587{ 2593{
@@ -3093,7 +3099,7 @@ static void s3c_hsotg_gate(struct platform_device *pdev, bool on)
3093 local_irq_restore(flags); 3099 local_irq_restore(flags);
3094} 3100}
3095 3101
3096struct s3c_hsotg_plat s3c_hsotg_default_pdata; 3102static struct s3c_hsotg_plat s3c_hsotg_default_pdata;
3097 3103
3098static int __devinit s3c_hsotg_probe(struct platform_device *pdev) 3104static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3099{ 3105{
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_audio.c b/drivers/usb/gadget/u_audio.c
index 8252595d619d..7a86d2c9109c 100644
--- a/drivers/usb/gadget/u_audio.c
+++ b/drivers/usb/gadget/u_audio.c
@@ -10,6 +10,7 @@
10 */ 10 */
11 11
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/slab.h>
13#include <linux/device.h> 14#include <linux/device.h>
14#include <linux/delay.h> 15#include <linux/delay.h>
15#include <linux/ctype.h> 16#include <linux/ctype.h>
@@ -288,6 +289,7 @@ static int gaudio_close_snd_dev(struct gaudio *gau)
288 return 0; 289 return 0;
289} 290}
290 291
292static struct gaudio *the_card;
291/** 293/**
292 * gaudio_setup - setup ALSA interface and preparing for USB transfer 294 * gaudio_setup - setup ALSA interface and preparing for USB transfer
293 * 295 *
@@ -303,6 +305,9 @@ int __init gaudio_setup(struct gaudio *card)
303 if (ret) 305 if (ret)
304 ERROR(card, "we need at least one control device\n"); 306 ERROR(card, "we need at least one control device\n");
305 307
308 if (!the_card)
309 the_card = card;
310
306 return ret; 311 return ret;
307 312
308} 313}
@@ -312,9 +317,11 @@ int __init gaudio_setup(struct gaudio *card)
312 * 317 *
313 * This is called to free all resources allocated by @gaudio_setup(). 318 * This is called to free all resources allocated by @gaudio_setup().
314 */ 319 */
315void gaudio_cleanup(struct gaudio *card) 320void gaudio_cleanup(void)
316{ 321{
317 if (card) 322 if (the_card) {
318 gaudio_close_snd_dev(card); 323 gaudio_close_snd_dev(the_card);
324 the_card = NULL;
325 }
319} 326}
320 327
diff --git a/drivers/usb/gadget/u_audio.h b/drivers/usb/gadget/u_audio.h
index cc8d159c648a..08ffce3298e6 100644
--- a/drivers/usb/gadget/u_audio.h
+++ b/drivers/usb/gadget/u_audio.h
@@ -51,6 +51,6 @@ struct gaudio {
51}; 51};
52 52
53int gaudio_setup(struct gaudio *card); 53int gaudio_setup(struct gaudio *card);
54void gaudio_cleanup(struct gaudio *card); 54void gaudio_cleanup(void);
55 55
56#endif /* __U_AUDIO_H */ 56#endif /* __U_AUDIO_H */
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index 2fc02bd95848..07f4178ad178 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -23,6 +23,7 @@
23/* #define VERBOSE_DEBUG */ 23/* #define VERBOSE_DEBUG */
24 24
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26#include <linux/gfp.h>
26#include <linux/device.h> 27#include <linux/device.h>
27#include <linux/ctype.h> 28#include <linux/ctype.h>
28#include <linux/etherdevice.h> 29#include <linux/etherdevice.h>
@@ -746,6 +747,10 @@ static const struct net_device_ops eth_netdev_ops = {
746 .ndo_validate_addr = eth_validate_addr, 747 .ndo_validate_addr = eth_validate_addr,
747}; 748};
748 749
750static struct device_type gadget_type = {
751 .name = "gadget",
752};
753
749/** 754/**
750 * gether_setup - initialize one ethernet-over-usb link 755 * gether_setup - initialize one ethernet-over-usb link
751 * @g: gadget to associated with these links 756 * @g: gadget to associated with these links
@@ -808,6 +813,7 @@ int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
808 813
809 dev->gadget = g; 814 dev->gadget = g;
810 SET_NETDEV_DEV(net, &g->dev); 815 SET_NETDEV_DEV(net, &g->dev);
816 SET_NETDEV_DEVTYPE(net, &gadget_type);
811 817
812 status = register_netdev(net); 818 status = register_netdev(net);
813 if (status < 0) { 819 if (status < 0) {
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 91b39ffdf6ea..3c8c0c9f9d72 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -93,13 +93,6 @@ static inline bool can_support_ecm(struct usb_gadget *gadget)
93 if (!gadget_supports_altsettings(gadget)) 93 if (!gadget_supports_altsettings(gadget))
94 return false; 94 return false;
95 95
96 /* SA1100 can do ECM, *without* status endpoint ... but we'll
97 * only use it in non-ECM mode for backwards compatibility
98 * (and since we currently require a status endpoint)
99 */
100 if (gadget_is_sa1100(gadget))
101 return false;
102
103 /* Everything else is *presumably* fine ... but this is a bit 96 /* Everything else is *presumably* fine ... but this is a bit
104 * chancy, so be **CERTAIN** there are no hardware issues with 97 * chancy, so be **CERTAIN** there are no hardware issues with
105 * your controller. Add it above if it can't handle CDC. 98 * your controller. Add it above if it can't handle CDC.
@@ -112,7 +105,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]); 105int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
113int eem_bind_config(struct usb_configuration *c); 106int eem_bind_config(struct usb_configuration *c);
114 107
115#ifdef CONFIG_USB_ETH_RNDIS 108#ifdef USB_ETH_RNDIS
116 109
117int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 110int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
118 111
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index adf8260c3a6a..16bdf77f582a 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -23,6 +23,7 @@
23#include <linux/delay.h> 23#include <linux/delay.h>
24#include <linux/tty.h> 24#include <linux/tty.h>
25#include <linux/tty_flip.h> 25#include <linux/tty_flip.h>
26#include <linux/slab.h>
26 27
27#include "u_serial.h" 28#include "u_serial.h"
28 29
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index 2d772401b7ad..807280d069f9 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -50,6 +50,7 @@
50/* #define VERBOSE_DEBUG */ 50/* #define VERBOSE_DEBUG */
51 51
52#include <linux/kernel.h> 52#include <linux/kernel.h>
53#include <linux/slab.h>
53#include <linux/utsname.h> 54#include <linux/utsname.h>
54#include <linux/device.h> 55#include <linux/device.h>
55 56
@@ -297,12 +298,10 @@ static int __init zero_bind(struct usb_composite_dev *cdev)
297 */ 298 */
298 if (loopdefault) { 299 if (loopdefault) {
299 loopback_add(cdev, autoresume != 0); 300 loopback_add(cdev, autoresume != 0);
300 if (!gadget_is_sh(gadget)) 301 sourcesink_add(cdev, autoresume != 0);
301 sourcesink_add(cdev, autoresume != 0);
302 } else { 302 } else {
303 sourcesink_add(cdev, autoresume != 0); 303 sourcesink_add(cdev, autoresume != 0);
304 if (!gadget_is_sh(gadget)) 304 loopback_add(cdev, autoresume != 0);
305 loopback_add(cdev, autoresume != 0);
306 } 305 }
307 306
308 gcnum = usb_gadget_controller_number(gadget); 307 gcnum = usb_gadget_controller_number(gadget);