aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig29
-rw-r--r--drivers/usb/gadget/Makefile2
-rw-r--r--drivers/usb/gadget/at91_udc.c4
-rw-r--r--drivers/usb/gadget/bcm63xx_udc.c6
-rw-r--r--drivers/usb/gadget/composite.c8
-rw-r--r--drivers/usb/gadget/config.c39
-rw-r--r--drivers/usb/gadget/dummy_hcd.c170
-rw-r--r--drivers/usb/gadget/f_acm.c79
-rw-r--r--drivers/usb/gadget/f_ecm.c112
-rw-r--r--drivers/usb/gadget/f_eem.c51
-rw-r--r--drivers/usb/gadget/f_fs.c4
-rw-r--r--drivers/usb/gadget/f_hid.c30
-rw-r--r--drivers/usb/gadget/f_loopback.c28
-rw-r--r--drivers/usb/gadget/f_mass_storage.c63
-rw-r--r--drivers/usb/gadget/f_midi.c14
-rw-r--r--drivers/usb/gadget/f_ncm.c94
-rw-r--r--drivers/usb/gadget/f_obex.c42
-rw-r--r--drivers/usb/gadget/f_phonet.c21
-rw-r--r--drivers/usb/gadget/f_rndis.c94
-rw-r--r--drivers/usb/gadget/f_serial.c38
-rw-r--r--drivers/usb/gadget/f_sourcesink.c104
-rw-r--r--drivers/usb/gadget/f_subset.c75
-rw-r--r--drivers/usb/gadget/f_uac1.c23
-rw-r--r--drivers/usb/gadget/f_uac2.c222
-rw-r--r--drivers/usb/gadget/f_uvc.c138
-rw-r--r--drivers/usb/gadget/file_storage.c3656
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.c14
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c2
-rw-r--r--drivers/usb/gadget/hid.c4
-rw-r--r--drivers/usb/gadget/inode.c3
-rw-r--r--drivers/usb/gadget/lpc32xx_udc.c6
-rw-r--r--drivers/usb/gadget/mv_u3d_core.c2
-rw-r--r--drivers/usb/gadget/mv_udc_core.c4
-rw-r--r--drivers/usb/gadget/net2272.c29
-rw-r--r--drivers/usb/gadget/net2280.c2
-rw-r--r--drivers/usb/gadget/omap_udc.c10
-rw-r--r--drivers/usb/gadget/printer.c12
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h2
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c12
-rw-r--r--drivers/usb/gadget/s3c-hsudc.c2
-rw-r--r--drivers/usb/gadget/storage_common.c165
-rw-r--r--drivers/usb/gadget/tcm_usb_gadget.c13
-rw-r--r--drivers/usb/gadget/udc-core.c11
43 files changed, 666 insertions, 4773 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index e0ff51b89529..14625fd2cecd 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -721,31 +721,6 @@ config USB_FUNCTIONFS_GENERIC
721 Include a configuration with the Function Filesystem alone with 721 Include a configuration with the Function Filesystem alone with
722 no Ethernet interface. 722 no Ethernet interface.
723 723
724config USB_FILE_STORAGE
725 tristate "File-backed Storage Gadget (DEPRECATED)"
726 depends on BLOCK
727 help
728 The File-backed Storage Gadget acts as a USB Mass Storage
729 disk drive. As its storage repository it can use a regular
730 file or a block device (in much the same way as the "loop"
731 device driver), specified as a module parameter.
732
733 Say "y" to link the driver statically, or "m" to build a
734 dynamically linked module called "g_file_storage".
735
736 NOTE: This driver is deprecated. Its replacement is the
737 Mass Storage Gadget.
738
739config USB_FILE_STORAGE_TEST
740 bool "File-backed Storage Gadget testing version"
741 depends on USB_FILE_STORAGE
742 default n
743 help
744 Say "y" to generate the larger testing version of the
745 File-backed Storage Gadget, useful for probing the
746 behavior of USB Mass Storage hosts. Not needed for
747 normal operation.
748
749config USB_MASS_STORAGE 724config USB_MASS_STORAGE
750 tristate "Mass Storage Gadget" 725 tristate "Mass Storage Gadget"
751 depends on BLOCK 726 depends on BLOCK
@@ -756,8 +731,8 @@ config USB_MASS_STORAGE
756 device (in much the same way as the "loop" device driver), 731 device (in much the same way as the "loop" device driver),
757 specified as a module parameter or sysfs option. 732 specified as a module parameter or sysfs option.
758 733
759 This driver is an updated replacement for the deprecated 734 This driver is a replacement for now removed File-backed
760 File-backed Storage Gadget (g_file_storage). 735 Storage Gadget (g_file_storage).
761 736
762 Say "y" to link the driver statically, or "m" to build 737 Say "y" to link the driver statically, or "m" to build
763 a dynamically linked module called "g_mass_storage". 738 a dynamically linked module called "g_mass_storage".
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 307be5fa824c..8b4acfd92aa3 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -44,7 +44,6 @@ g_ether-y := ether.o
44g_serial-y := serial.o 44g_serial-y := serial.o
45g_midi-y := gmidi.o 45g_midi-y := gmidi.o
46gadgetfs-y := inode.o 46gadgetfs-y := inode.o
47g_file_storage-y := file_storage.o
48g_mass_storage-y := mass_storage.o 47g_mass_storage-y := mass_storage.o
49g_printer-y := printer.o 48g_printer-y := printer.o
50g_cdc-y := cdc2.o 49g_cdc-y := cdc2.o
@@ -62,7 +61,6 @@ obj-$(CONFIG_USB_AUDIO) += g_audio.o
62obj-$(CONFIG_USB_ETH) += g_ether.o 61obj-$(CONFIG_USB_ETH) += g_ether.o
63obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 62obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
64obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o 63obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o
65obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
66obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o 64obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o
67obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 65obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
68obj-$(CONFIG_USB_G_PRINTER) += g_printer.o 66obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 89d90b5fb787..e6135faabc3a 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -1673,7 +1673,7 @@ static void at91udc_shutdown(struct platform_device *dev)
1673 spin_unlock_irqrestore(&udc->lock, flags); 1673 spin_unlock_irqrestore(&udc->lock, flags);
1674} 1674}
1675 1675
1676static void __devinit at91udc_of_init(struct at91_udc *udc, 1676static void at91udc_of_init(struct at91_udc *udc,
1677 struct device_node *np) 1677 struct device_node *np)
1678{ 1678{
1679 struct at91_udc_data *board = &udc->board; 1679 struct at91_udc_data *board = &udc->board;
@@ -1693,7 +1693,7 @@ static void __devinit at91udc_of_init(struct at91_udc *udc,
1693 board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0; 1693 board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
1694} 1694}
1695 1695
1696static int __devinit at91udc_probe(struct platform_device *pdev) 1696static int at91udc_probe(struct platform_device *pdev)
1697{ 1697{
1698 struct device *dev = &pdev->dev; 1698 struct device *dev = &pdev->dev;
1699 struct at91_udc *udc; 1699 struct at91_udc *udc;
diff --git a/drivers/usb/gadget/bcm63xx_udc.c b/drivers/usb/gadget/bcm63xx_udc.c
index 9ca792224cd4..47a49931361e 100644
--- a/drivers/usb/gadget/bcm63xx_udc.c
+++ b/drivers/usb/gadget/bcm63xx_udc.c
@@ -2323,7 +2323,7 @@ static void bcm63xx_udc_gadget_release(struct device *dev)
2323 * Note that platform data is required, because pd.port_no varies from chip 2323 * Note that platform data is required, because pd.port_no varies from chip
2324 * to chip and is used to switch the correct USB port to device mode. 2324 * to chip and is used to switch the correct USB port to device mode.
2325 */ 2325 */
2326static int __devinit bcm63xx_udc_probe(struct platform_device *pdev) 2326static int bcm63xx_udc_probe(struct platform_device *pdev)
2327{ 2327{
2328 struct device *dev = &pdev->dev; 2328 struct device *dev = &pdev->dev;
2329 struct bcm63xx_usbd_platform_data *pd = dev->platform_data; 2329 struct bcm63xx_usbd_platform_data *pd = dev->platform_data;
@@ -2433,7 +2433,7 @@ out_uninit:
2433 * bcm63xx_udc_remove - Remove the device from the system. 2433 * bcm63xx_udc_remove - Remove the device from the system.
2434 * @pdev: Platform device struct from the bcm63xx BSP code. 2434 * @pdev: Platform device struct from the bcm63xx BSP code.
2435 */ 2435 */
2436static int __devexit bcm63xx_udc_remove(struct platform_device *pdev) 2436static int bcm63xx_udc_remove(struct platform_device *pdev)
2437{ 2437{
2438 struct bcm63xx_udc *udc = platform_get_drvdata(pdev); 2438 struct bcm63xx_udc *udc = platform_get_drvdata(pdev);
2439 2439
@@ -2450,7 +2450,7 @@ static int __devexit bcm63xx_udc_remove(struct platform_device *pdev)
2450 2450
2451static struct platform_driver bcm63xx_udc_driver = { 2451static struct platform_driver bcm63xx_udc_driver = {
2452 .probe = bcm63xx_udc_probe, 2452 .probe = bcm63xx_udc_probe,
2453 .remove = __devexit_p(bcm63xx_udc_remove), 2453 .remove = bcm63xx_udc_remove,
2454 .driver = { 2454 .driver = {
2455 .name = DRV_MODULE_NAME, 2455 .name = DRV_MODULE_NAME,
2456 .owner = THIS_MODULE, 2456 .owner = THIS_MODULE,
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 957f973dd96a..2a6bfe759c29 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -107,7 +107,7 @@ int config_ep_by_speed(struct usb_gadget *g,
107 } 107 }
108 /* else: fall through */ 108 /* else: fall through */
109 default: 109 default:
110 speed_desc = f->descriptors; 110 speed_desc = f->fs_descriptors;
111 } 111 }
112 /* find descriptors */ 112 /* find descriptors */
113 for_each_ep_desc(speed_desc, d_spd) { 113 for_each_ep_desc(speed_desc, d_spd) {
@@ -200,7 +200,7 @@ int usb_add_function(struct usb_configuration *config,
200 * as full speed ... it's the function drivers that will need 200 * as full speed ... it's the function drivers that will need
201 * to avoid bulk and ISO transfers. 201 * to avoid bulk and ISO transfers.
202 */ 202 */
203 if (!config->fullspeed && function->descriptors) 203 if (!config->fullspeed && function->fs_descriptors)
204 config->fullspeed = true; 204 config->fullspeed = true;
205 if (!config->highspeed && function->hs_descriptors) 205 if (!config->highspeed && function->hs_descriptors)
206 config->highspeed = true; 206 config->highspeed = true;
@@ -363,7 +363,7 @@ static int config_buf(struct usb_configuration *config,
363 descriptors = f->hs_descriptors; 363 descriptors = f->hs_descriptors;
364 break; 364 break;
365 default: 365 default:
366 descriptors = f->descriptors; 366 descriptors = f->fs_descriptors;
367 } 367 }
368 368
369 if (!descriptors) 369 if (!descriptors)
@@ -620,7 +620,7 @@ static int set_config(struct usb_composite_dev *cdev,
620 descriptors = f->hs_descriptors; 620 descriptors = f->hs_descriptors;
621 break; 621 break;
622 default: 622 default:
623 descriptors = f->descriptors; 623 descriptors = f->fs_descriptors;
624 } 624 }
625 625
626 for (; *descriptors; ++descriptors) { 626 for (; *descriptors; ++descriptors) {
diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c
index e3a98929d346..34e12fc52c23 100644
--- a/drivers/usb/gadget/config.c
+++ b/drivers/usb/gadget/config.c
@@ -19,7 +19,7 @@
19 19
20#include <linux/usb/ch9.h> 20#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h> 21#include <linux/usb/gadget.h>
22 22#include <linux/usb/composite.h>
23 23
24/** 24/**
25 * usb_descriptor_fillbuf - fill buffer with descriptors 25 * usb_descriptor_fillbuf - fill buffer with descriptors
@@ -158,3 +158,40 @@ usb_copy_descriptors(struct usb_descriptor_header **src)
158 return ret; 158 return ret;
159} 159}
160EXPORT_SYMBOL_GPL(usb_copy_descriptors); 160EXPORT_SYMBOL_GPL(usb_copy_descriptors);
161
162int usb_assign_descriptors(struct usb_function *f,
163 struct usb_descriptor_header **fs,
164 struct usb_descriptor_header **hs,
165 struct usb_descriptor_header **ss)
166{
167 struct usb_gadget *g = f->config->cdev->gadget;
168
169 if (fs) {
170 f->fs_descriptors = usb_copy_descriptors(fs);
171 if (!f->fs_descriptors)
172 goto err;
173 }
174 if (hs && gadget_is_dualspeed(g)) {
175 f->hs_descriptors = usb_copy_descriptors(hs);
176 if (!f->hs_descriptors)
177 goto err;
178 }
179 if (ss && gadget_is_superspeed(g)) {
180 f->ss_descriptors = usb_copy_descriptors(ss);
181 if (!f->ss_descriptors)
182 goto err;
183 }
184 return 0;
185err:
186 usb_free_all_descriptors(f);
187 return -ENOMEM;
188}
189EXPORT_SYMBOL_GPL(usb_assign_descriptors);
190
191void usb_free_all_descriptors(struct usb_function *f)
192{
193 usb_free_descriptors(f->fs_descriptors);
194 usb_free_descriptors(f->hs_descriptors);
195 usb_free_descriptors(f->ss_descriptors);
196}
197EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 0f7541be28f3..95d584dbed13 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -63,16 +63,20 @@ MODULE_LICENSE("GPL");
63struct dummy_hcd_module_parameters { 63struct dummy_hcd_module_parameters {
64 bool is_super_speed; 64 bool is_super_speed;
65 bool is_high_speed; 65 bool is_high_speed;
66 unsigned int num;
66}; 67};
67 68
68static struct dummy_hcd_module_parameters mod_data = { 69static struct dummy_hcd_module_parameters mod_data = {
69 .is_super_speed = false, 70 .is_super_speed = false,
70 .is_high_speed = true, 71 .is_high_speed = true,
72 .num = 1,
71}; 73};
72module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO); 74module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
73MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection"); 75MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
74module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO); 76module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
75MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection"); 77MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
78module_param_named(num, mod_data.num, uint, S_IRUGO);
79MODULE_PARM_DESC(num, "number of emulated controllers");
76/*-------------------------------------------------------------------------*/ 80/*-------------------------------------------------------------------------*/
77 81
78/* gadget side driver data structres */ 82/* gadget side driver data structres */
@@ -238,8 +242,6 @@ static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
238 return container_of(dev, struct dummy, gadget.dev); 242 return container_of(dev, struct dummy, gadget.dev);
239} 243}
240 244
241static struct dummy the_controller;
242
243/*-------------------------------------------------------------------------*/ 245/*-------------------------------------------------------------------------*/
244 246
245/* SLAVE/GADGET SIDE UTILITY ROUTINES */ 247/* SLAVE/GADGET SIDE UTILITY ROUTINES */
@@ -973,9 +975,10 @@ static void init_dummy_udc_hw(struct dummy *dum)
973 975
974static int dummy_udc_probe(struct platform_device *pdev) 976static int dummy_udc_probe(struct platform_device *pdev)
975{ 977{
976 struct dummy *dum = &the_controller; 978 struct dummy *dum;
977 int rc; 979 int rc;
978 980
981 dum = *((void **)dev_get_platdata(&pdev->dev));
979 dum->gadget.name = gadget_name; 982 dum->gadget.name = gadget_name;
980 dum->gadget.ops = &dummy_ops; 983 dum->gadget.ops = &dummy_ops;
981 dum->gadget.max_speed = USB_SPEED_SUPER; 984 dum->gadget.max_speed = USB_SPEED_SUPER;
@@ -2398,10 +2401,13 @@ static int dummy_h_get_frame(struct usb_hcd *hcd)
2398 2401
2399static int dummy_setup(struct usb_hcd *hcd) 2402static int dummy_setup(struct usb_hcd *hcd)
2400{ 2403{
2404 struct dummy *dum;
2405
2406 dum = *((void **)dev_get_platdata(hcd->self.controller));
2401 hcd->self.sg_tablesize = ~0; 2407 hcd->self.sg_tablesize = ~0;
2402 if (usb_hcd_is_primary_hcd(hcd)) { 2408 if (usb_hcd_is_primary_hcd(hcd)) {
2403 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd); 2409 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2404 the_controller.hs_hcd->dum = &the_controller; 2410 dum->hs_hcd->dum = dum;
2405 /* 2411 /*
2406 * Mark the first roothub as being USB 2.0. 2412 * Mark the first roothub as being USB 2.0.
2407 * The USB 3.0 roothub will be registered later by 2413 * The USB 3.0 roothub will be registered later by
@@ -2410,8 +2416,8 @@ static int dummy_setup(struct usb_hcd *hcd)
2410 hcd->speed = HCD_USB2; 2416 hcd->speed = HCD_USB2;
2411 hcd->self.root_hub->speed = USB_SPEED_HIGH; 2417 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2412 } else { 2418 } else {
2413 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd); 2419 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2414 the_controller.ss_hcd->dum = &the_controller; 2420 dum->ss_hcd->dum = dum;
2415 hcd->speed = HCD_USB3; 2421 hcd->speed = HCD_USB3;
2416 hcd->self.root_hub->speed = USB_SPEED_SUPER; 2422 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2417 } 2423 }
@@ -2524,11 +2530,13 @@ static struct hc_driver dummy_hcd = {
2524 2530
2525static int dummy_hcd_probe(struct platform_device *pdev) 2531static int dummy_hcd_probe(struct platform_device *pdev)
2526{ 2532{
2533 struct dummy *dum;
2527 struct usb_hcd *hs_hcd; 2534 struct usb_hcd *hs_hcd;
2528 struct usb_hcd *ss_hcd; 2535 struct usb_hcd *ss_hcd;
2529 int retval; 2536 int retval;
2530 2537
2531 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); 2538 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2539 dum = *((void **)dev_get_platdata(&pdev->dev));
2532 2540
2533 if (!mod_data.is_super_speed) 2541 if (!mod_data.is_super_speed)
2534 dummy_hcd.flags = HCD_USB2; 2542 dummy_hcd.flags = HCD_USB2;
@@ -2561,7 +2569,7 @@ dealloc_usb2_hcd:
2561 usb_remove_hcd(hs_hcd); 2569 usb_remove_hcd(hs_hcd);
2562put_usb2_hcd: 2570put_usb2_hcd:
2563 usb_put_hcd(hs_hcd); 2571 usb_put_hcd(hs_hcd);
2564 the_controller.hs_hcd = the_controller.ss_hcd = NULL; 2572 dum->hs_hcd = dum->ss_hcd = NULL;
2565 return retval; 2573 return retval;
2566} 2574}
2567 2575
@@ -2579,8 +2587,8 @@ static int dummy_hcd_remove(struct platform_device *pdev)
2579 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); 2587 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2580 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); 2588 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2581 2589
2582 the_controller.hs_hcd = NULL; 2590 dum->hs_hcd = NULL;
2583 the_controller.ss_hcd = NULL; 2591 dum->ss_hcd = NULL;
2584 2592
2585 return 0; 2593 return 0;
2586} 2594}
@@ -2627,13 +2635,15 @@ static struct platform_driver dummy_hcd_driver = {
2627}; 2635};
2628 2636
2629/*-------------------------------------------------------------------------*/ 2637/*-------------------------------------------------------------------------*/
2630 2638#define MAX_NUM_UDC 2
2631static struct platform_device *the_udc_pdev; 2639static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2632static struct platform_device *the_hcd_pdev; 2640static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
2633 2641
2634static int __init init(void) 2642static int __init init(void)
2635{ 2643{
2636 int retval = -ENOMEM; 2644 int retval = -ENOMEM;
2645 int i;
2646 struct dummy *dum[MAX_NUM_UDC];
2637 2647
2638 if (usb_disabled()) 2648 if (usb_disabled())
2639 return -ENODEV; 2649 return -ENODEV;
@@ -2641,65 +2651,129 @@ static int __init init(void)
2641 if (!mod_data.is_high_speed && mod_data.is_super_speed) 2651 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2642 return -EINVAL; 2652 return -EINVAL;
2643 2653
2644 the_hcd_pdev = platform_device_alloc(driver_name, -1); 2654 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2645 if (!the_hcd_pdev) 2655 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2646 return retval; 2656 MAX_NUM_UDC);
2647 the_udc_pdev = platform_device_alloc(gadget_name, -1); 2657 return -EINVAL;
2648 if (!the_udc_pdev) 2658 }
2649 goto err_alloc_udc; 2659
2660 for (i = 0; i < mod_data.num; i++) {
2661 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2662 if (!the_hcd_pdev[i]) {
2663 i--;
2664 while (i >= 0)
2665 platform_device_put(the_hcd_pdev[i--]);
2666 return retval;
2667 }
2668 }
2669 for (i = 0; i < mod_data.num; i++) {
2670 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2671 if (!the_udc_pdev[i]) {
2672 i--;
2673 while (i >= 0)
2674 platform_device_put(the_udc_pdev[i--]);
2675 goto err_alloc_udc;
2676 }
2677 }
2678 for (i = 0; i < mod_data.num; i++) {
2679 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2680 if (!dum[i])
2681 goto err_add_pdata;
2682 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2683 sizeof(void *));
2684 if (retval)
2685 goto err_add_pdata;
2686 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2687 sizeof(void *));
2688 if (retval)
2689 goto err_add_pdata;
2690 }
2650 2691
2651 retval = platform_driver_register(&dummy_hcd_driver); 2692 retval = platform_driver_register(&dummy_hcd_driver);
2652 if (retval < 0) 2693 if (retval < 0)
2653 goto err_register_hcd_driver; 2694 goto err_add_pdata;
2654 retval = platform_driver_register(&dummy_udc_driver); 2695 retval = platform_driver_register(&dummy_udc_driver);
2655 if (retval < 0) 2696 if (retval < 0)
2656 goto err_register_udc_driver; 2697 goto err_register_udc_driver;
2657 2698
2658 retval = platform_device_add(the_hcd_pdev); 2699 for (i = 0; i < mod_data.num; i++) {
2659 if (retval < 0) 2700 retval = platform_device_add(the_hcd_pdev[i]);
2660 goto err_add_hcd; 2701 if (retval < 0) {
2661 if (!the_controller.hs_hcd || 2702 i--;
2662 (!the_controller.ss_hcd && mod_data.is_super_speed)) { 2703 while (i >= 0)
2663 /* 2704 platform_device_del(the_hcd_pdev[i--]);
2664 * The hcd was added successfully but its probe function failed 2705 goto err_add_hcd;
2665 * for some reason. 2706 }
2666 */
2667 retval = -EINVAL;
2668 goto err_add_udc;
2669 } 2707 }
2670 retval = platform_device_add(the_udc_pdev); 2708 for (i = 0; i < mod_data.num; i++) {
2671 if (retval < 0) 2709 if (!dum[i]->hs_hcd ||
2672 goto err_add_udc; 2710 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2673 if (!platform_get_drvdata(the_udc_pdev)) { 2711 /*
2674 /* 2712 * The hcd was added successfully but its probe
2675 * The udc was added successfully but its probe function failed 2713 * function failed for some reason.
2676 * for some reason. 2714 */
2677 */ 2715 retval = -EINVAL;
2678 retval = -EINVAL; 2716 goto err_add_udc;
2679 goto err_probe_udc; 2717 }
2718 }
2719
2720 for (i = 0; i < mod_data.num; i++) {
2721 retval = platform_device_add(the_udc_pdev[i]);
2722 if (retval < 0) {
2723 i--;
2724 while (i >= 0)
2725 platform_device_del(the_udc_pdev[i]);
2726 goto err_add_udc;
2727 }
2728 }
2729
2730 for (i = 0; i < mod_data.num; i++) {
2731 if (!platform_get_drvdata(the_udc_pdev[i])) {
2732 /*
2733 * The udc was added successfully but its probe
2734 * function failed for some reason.
2735 */
2736 retval = -EINVAL;
2737 goto err_probe_udc;
2738 }
2680 } 2739 }
2681 return retval; 2740 return retval;
2682 2741
2683err_probe_udc: 2742err_probe_udc:
2684 platform_device_del(the_udc_pdev); 2743 for (i = 0; i < mod_data.num; i++)
2744 platform_device_del(the_udc_pdev[i]);
2685err_add_udc: 2745err_add_udc:
2686 platform_device_del(the_hcd_pdev); 2746 for (i = 0; i < mod_data.num; i++)
2747 platform_device_del(the_hcd_pdev[i]);
2687err_add_hcd: 2748err_add_hcd:
2688 platform_driver_unregister(&dummy_udc_driver); 2749 platform_driver_unregister(&dummy_udc_driver);
2689err_register_udc_driver: 2750err_register_udc_driver:
2690 platform_driver_unregister(&dummy_hcd_driver); 2751 platform_driver_unregister(&dummy_hcd_driver);
2691err_register_hcd_driver: 2752err_add_pdata:
2692 platform_device_put(the_udc_pdev); 2753 for (i = 0; i < mod_data.num; i++)
2754 kfree(dum[i]);
2755 for (i = 0; i < mod_data.num; i++)
2756 platform_device_put(the_udc_pdev[i]);
2693err_alloc_udc: 2757err_alloc_udc:
2694 platform_device_put(the_hcd_pdev); 2758 for (i = 0; i < mod_data.num; i++)
2759 platform_device_put(the_hcd_pdev[i]);
2695 return retval; 2760 return retval;
2696} 2761}
2697module_init(init); 2762module_init(init);
2698 2763
2699static void __exit cleanup(void) 2764static void __exit cleanup(void)
2700{ 2765{
2701 platform_device_unregister(the_udc_pdev); 2766 int i;
2702 platform_device_unregister(the_hcd_pdev); 2767
2768 for (i = 0; i < mod_data.num; i++) {
2769 struct dummy *dum;
2770
2771 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2772
2773 platform_device_unregister(the_udc_pdev[i]);
2774 platform_device_unregister(the_hcd_pdev[i]);
2775 kfree(dum);
2776 }
2703 platform_driver_unregister(&dummy_udc_driver); 2777 platform_driver_unregister(&dummy_udc_driver);
2704 platform_driver_unregister(&dummy_hcd_driver); 2778 platform_driver_unregister(&dummy_hcd_driver);
2705} 2779}
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index d672250a61fa..549174466c21 100644
--- a/drivers/usb/gadget/f_acm.c
+++ b/drivers/usb/gadget/f_acm.c
@@ -87,7 +87,7 @@ static inline struct f_acm *port_to_acm(struct gserial *p)
87 87
88/* notification endpoint uses smallish and infrequent fixed-size messages */ 88/* notification endpoint uses smallish and infrequent fixed-size messages */
89 89
90#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ 90#define GS_NOTIFY_INTERVAL_MS 32
91#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */ 91#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
92 92
93/* interface and class descriptors: */ 93/* interface and class descriptors: */
@@ -167,7 +167,7 @@ static struct usb_endpoint_descriptor acm_fs_notify_desc = {
167 .bEndpointAddress = USB_DIR_IN, 167 .bEndpointAddress = USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_INT, 168 .bmAttributes = USB_ENDPOINT_XFER_INT,
169 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET), 169 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
170 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, 170 .bInterval = GS_NOTIFY_INTERVAL_MS,
171}; 171};
172 172
173static struct usb_endpoint_descriptor acm_fs_in_desc = { 173static struct usb_endpoint_descriptor acm_fs_in_desc = {
@@ -199,14 +199,13 @@ static struct usb_descriptor_header *acm_fs_function[] = {
199}; 199};
200 200
201/* high speed support: */ 201/* high speed support: */
202
203static struct usb_endpoint_descriptor acm_hs_notify_desc = { 202static struct usb_endpoint_descriptor acm_hs_notify_desc = {
204 .bLength = USB_DT_ENDPOINT_SIZE, 203 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT, 204 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_IN, 205 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT, 206 .bmAttributes = USB_ENDPOINT_XFER_INT,
208 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET), 207 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
209 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, 208 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
210}; 209};
211 210
212static struct usb_endpoint_descriptor acm_hs_in_desc = { 211static struct usb_endpoint_descriptor acm_hs_in_desc = {
@@ -659,37 +658,22 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
659 acm->notify_req->complete = acm_cdc_notify_complete; 658 acm->notify_req->complete = acm_cdc_notify_complete;
660 acm->notify_req->context = acm; 659 acm->notify_req->context = acm;
661 660
662 /* copy descriptors */
663 f->descriptors = usb_copy_descriptors(acm_fs_function);
664 if (!f->descriptors)
665 goto fail;
666
667 /* support all relevant hardware speeds... we expect that when 661 /* support all relevant hardware speeds... we expect that when
668 * hardware is dual speed, all bulk-capable endpoints work at 662 * hardware is dual speed, all bulk-capable endpoints work at
669 * both speeds 663 * both speeds
670 */ 664 */
671 if (gadget_is_dualspeed(c->cdev->gadget)) { 665 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
672 acm_hs_in_desc.bEndpointAddress = 666 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
673 acm_fs_in_desc.bEndpointAddress; 667 acm_hs_notify_desc.bEndpointAddress =
674 acm_hs_out_desc.bEndpointAddress = 668 acm_fs_notify_desc.bEndpointAddress;
675 acm_fs_out_desc.bEndpointAddress; 669
676 acm_hs_notify_desc.bEndpointAddress = 670 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
677 acm_fs_notify_desc.bEndpointAddress; 671 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
678 672
679 /* copy descriptors */ 673 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
680 f->hs_descriptors = usb_copy_descriptors(acm_hs_function); 674 acm_ss_function);
681 } 675 if (status)
682 if (gadget_is_superspeed(c->cdev->gadget)) { 676 goto fail;
683 acm_ss_in_desc.bEndpointAddress =
684 acm_fs_in_desc.bEndpointAddress;
685 acm_ss_out_desc.bEndpointAddress =
686 acm_fs_out_desc.bEndpointAddress;
687
688 /* copy descriptors, and track endpoint copies */
689 f->ss_descriptors = usb_copy_descriptors(acm_ss_function);
690 if (!f->ss_descriptors)
691 goto fail;
692 }
693 677
694 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", 678 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
695 acm->port_num, 679 acm->port_num,
@@ -721,11 +705,8 @@ acm_unbind(struct usb_configuration *c, struct usb_function *f)
721{ 705{
722 struct f_acm *acm = func_to_acm(f); 706 struct f_acm *acm = func_to_acm(f);
723 707
724 if (gadget_is_dualspeed(c->cdev->gadget)) 708 acm_string_defs[0].id = 0;
725 usb_free_descriptors(f->hs_descriptors); 709 usb_free_all_descriptors(f);
726 if (gadget_is_superspeed(c->cdev->gadget))
727 usb_free_descriptors(f->ss_descriptors);
728 usb_free_descriptors(f->descriptors);
729 gs_free_req(acm->notify, acm->notify_req); 710 gs_free_req(acm->notify, acm->notify_req);
730 kfree(acm); 711 kfree(acm);
731} 712}
@@ -762,27 +743,15 @@ int acm_bind_config(struct usb_configuration *c, u8 port_num)
762 */ 743 */
763 744
764 /* maybe allocate device-global string IDs, and patch descriptors */ 745 /* maybe allocate device-global string IDs, and patch descriptors */
765 if (acm_string_defs[ACM_CTRL_IDX].id == 0) { 746 if (acm_string_defs[0].id == 0) {
766 status = usb_string_id(c->cdev); 747 status = usb_string_ids_tab(c->cdev, acm_string_defs);
767 if (status < 0) 748 if (status < 0)
768 return status; 749 return status;
769 acm_string_defs[ACM_CTRL_IDX].id = status; 750 acm_control_interface_desc.iInterface =
770 751 acm_string_defs[ACM_CTRL_IDX].id;
771 acm_control_interface_desc.iInterface = status; 752 acm_data_interface_desc.iInterface =
772 753 acm_string_defs[ACM_DATA_IDX].id;
773 status = usb_string_id(c->cdev); 754 acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
774 if (status < 0)
775 return status;
776 acm_string_defs[ACM_DATA_IDX].id = status;
777
778 acm_data_interface_desc.iInterface = status;
779
780 status = usb_string_id(c->cdev);
781 if (status < 0)
782 return status;
783 acm_string_defs[ACM_IAD_IDX].id = status;
784
785 acm_iad_descriptor.iFunction = status;
786 } 755 }
787 756
788 /* allocate and initialize one new instance */ 757 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
index 95bc94f8e570..83420a310fb7 100644
--- a/drivers/usb/gadget/f_ecm.c
+++ b/drivers/usb/gadget/f_ecm.c
@@ -91,7 +91,7 @@ static inline unsigned ecm_bitrate(struct usb_gadget *g)
91 * encapsulated commands (vendor-specific, using control-OUT). 91 * encapsulated commands (vendor-specific, using control-OUT).
92 */ 92 */
93 93
94#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 94#define ECM_STATUS_INTERVAL_MS 32
95#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ 95#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
96 96
97 97
@@ -192,7 +192,7 @@ static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
192 .bEndpointAddress = USB_DIR_IN, 192 .bEndpointAddress = USB_DIR_IN,
193 .bmAttributes = USB_ENDPOINT_XFER_INT, 193 .bmAttributes = USB_ENDPOINT_XFER_INT,
194 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 194 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
195 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 195 .bInterval = ECM_STATUS_INTERVAL_MS,
196}; 196};
197 197
198static struct usb_endpoint_descriptor fs_ecm_in_desc = { 198static struct usb_endpoint_descriptor fs_ecm_in_desc = {
@@ -239,7 +239,7 @@ static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
239 .bEndpointAddress = USB_DIR_IN, 239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT, 240 .bmAttributes = USB_ENDPOINT_XFER_INT,
241 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 241 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 242 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
243}; 243};
244 244
245static struct usb_endpoint_descriptor hs_ecm_in_desc = { 245static struct usb_endpoint_descriptor hs_ecm_in_desc = {
@@ -288,7 +288,7 @@ static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
288 .bEndpointAddress = USB_DIR_IN, 288 .bEndpointAddress = USB_DIR_IN,
289 .bmAttributes = USB_ENDPOINT_XFER_INT, 289 .bmAttributes = USB_ENDPOINT_XFER_INT,
290 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 290 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
291 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 291 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
292}; 292};
293 293
294static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = { 294static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
@@ -330,6 +330,7 @@ static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
330 330
331static struct usb_descriptor_header *ecm_ss_function[] = { 331static struct usb_descriptor_header *ecm_ss_function[] = {
332 /* CDC ECM control descriptors */ 332 /* CDC ECM control descriptors */
333 (struct usb_descriptor_header *) &ecm_iad_descriptor,
333 (struct usb_descriptor_header *) &ecm_control_intf, 334 (struct usb_descriptor_header *) &ecm_control_intf,
334 (struct usb_descriptor_header *) &ecm_header_desc, 335 (struct usb_descriptor_header *) &ecm_header_desc,
335 (struct usb_descriptor_header *) &ecm_union_desc, 336 (struct usb_descriptor_header *) &ecm_union_desc,
@@ -353,7 +354,7 @@ static struct usb_descriptor_header *ecm_ss_function[] = {
353 354
354static struct usb_string ecm_string_defs[] = { 355static struct usb_string ecm_string_defs[] = {
355 [0].s = "CDC Ethernet Control Model (ECM)", 356 [0].s = "CDC Ethernet Control Model (ECM)",
356 [1].s = NULL /* DYNAMIC */, 357 [1].s = "",
357 [2].s = "CDC Ethernet Data", 358 [2].s = "CDC Ethernet Data",
358 [3].s = "CDC ECM", 359 [3].s = "CDC ECM",
359 { } /* end of list */ 360 { } /* end of list */
@@ -742,42 +743,24 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f)
742 ecm->notify_req->context = ecm; 743 ecm->notify_req->context = ecm;
743 ecm->notify_req->complete = ecm_notify_complete; 744 ecm->notify_req->complete = ecm_notify_complete;
744 745
745 /* copy descriptors, and track endpoint copies */
746 f->descriptors = usb_copy_descriptors(ecm_fs_function);
747 if (!f->descriptors)
748 goto fail;
749
750 /* support all relevant hardware speeds... we expect that when 746 /* support all relevant hardware speeds... we expect that when
751 * hardware is dual speed, all bulk-capable endpoints work at 747 * hardware is dual speed, all bulk-capable endpoints work at
752 * both speeds 748 * both speeds
753 */ 749 */
754 if (gadget_is_dualspeed(c->cdev->gadget)) { 750 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
755 hs_ecm_in_desc.bEndpointAddress = 751 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
756 fs_ecm_in_desc.bEndpointAddress; 752 hs_ecm_notify_desc.bEndpointAddress =
757 hs_ecm_out_desc.bEndpointAddress = 753 fs_ecm_notify_desc.bEndpointAddress;
758 fs_ecm_out_desc.bEndpointAddress; 754
759 hs_ecm_notify_desc.bEndpointAddress = 755 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
760 fs_ecm_notify_desc.bEndpointAddress; 756 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
761 757 ss_ecm_notify_desc.bEndpointAddress =
762 /* copy descriptors, and track endpoint copies */ 758 fs_ecm_notify_desc.bEndpointAddress;
763 f->hs_descriptors = usb_copy_descriptors(ecm_hs_function); 759
764 if (!f->hs_descriptors) 760 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
765 goto fail; 761 ecm_ss_function);
766 } 762 if (status)
767 763 goto fail;
768 if (gadget_is_superspeed(c->cdev->gadget)) {
769 ss_ecm_in_desc.bEndpointAddress =
770 fs_ecm_in_desc.bEndpointAddress;
771 ss_ecm_out_desc.bEndpointAddress =
772 fs_ecm_out_desc.bEndpointAddress;
773 ss_ecm_notify_desc.bEndpointAddress =
774 fs_ecm_notify_desc.bEndpointAddress;
775
776 /* copy descriptors, and track endpoint copies */
777 f->ss_descriptors = usb_copy_descriptors(ecm_ss_function);
778 if (!f->ss_descriptors)
779 goto fail;
780 }
781 764
782 /* NOTE: all that is done without knowing or caring about 765 /* NOTE: all that is done without knowing or caring about
783 * the network link ... which is unavailable to this code 766 * the network link ... which is unavailable to this code
@@ -795,11 +778,6 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f)
795 return 0; 778 return 0;
796 779
797fail: 780fail:
798 if (f->descriptors)
799 usb_free_descriptors(f->descriptors);
800 if (f->hs_descriptors)
801 usb_free_descriptors(f->hs_descriptors);
802
803 if (ecm->notify_req) { 781 if (ecm->notify_req) {
804 kfree(ecm->notify_req->buf); 782 kfree(ecm->notify_req->buf);
805 usb_ep_free_request(ecm->notify, ecm->notify_req); 783 usb_ep_free_request(ecm->notify, ecm->notify_req);
@@ -808,9 +786,9 @@ fail:
808 /* we might as well release our claims on endpoints */ 786 /* we might as well release our claims on endpoints */
809 if (ecm->notify) 787 if (ecm->notify)
810 ecm->notify->driver_data = NULL; 788 ecm->notify->driver_data = NULL;
811 if (ecm->port.out_ep->desc) 789 if (ecm->port.out_ep)
812 ecm->port.out_ep->driver_data = NULL; 790 ecm->port.out_ep->driver_data = NULL;
813 if (ecm->port.in_ep->desc) 791 if (ecm->port.in_ep)
814 ecm->port.in_ep->driver_data = NULL; 792 ecm->port.in_ep->driver_data = NULL;
815 793
816 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 794 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
@@ -825,16 +803,11 @@ ecm_unbind(struct usb_configuration *c, struct usb_function *f)
825 803
826 DBG(c->cdev, "ecm unbind\n"); 804 DBG(c->cdev, "ecm unbind\n");
827 805
828 if (gadget_is_superspeed(c->cdev->gadget)) 806 ecm_string_defs[0].id = 0;
829 usb_free_descriptors(f->ss_descriptors); 807 usb_free_all_descriptors(f);
830 if (gadget_is_dualspeed(c->cdev->gadget))
831 usb_free_descriptors(f->hs_descriptors);
832 usb_free_descriptors(f->descriptors);
833 808
834 kfree(ecm->notify_req->buf); 809 kfree(ecm->notify_req->buf);
835 usb_ep_free_request(ecm->notify, ecm->notify_req); 810 usb_ep_free_request(ecm->notify, ecm->notify_req);
836
837 ecm_string_defs[1].s = NULL;
838 kfree(ecm); 811 kfree(ecm);
839} 812}
840 813
@@ -859,36 +832,15 @@ ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
859 if (!can_support_ecm(c->cdev->gadget) || !ethaddr) 832 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
860 return -EINVAL; 833 return -EINVAL;
861 834
862 /* maybe allocate device-global string IDs */
863 if (ecm_string_defs[0].id == 0) { 835 if (ecm_string_defs[0].id == 0) {
864 836 status = usb_string_ids_tab(c->cdev, ecm_string_defs);
865 /* control interface label */ 837 if (status)
866 status = usb_string_id(c->cdev);
867 if (status < 0)
868 return status;
869 ecm_string_defs[0].id = status;
870 ecm_control_intf.iInterface = status;
871
872 /* data interface label */
873 status = usb_string_id(c->cdev);
874 if (status < 0)
875 return status; 838 return status;
876 ecm_string_defs[2].id = status;
877 ecm_data_intf.iInterface = status;
878 839
879 /* MAC address */ 840 ecm_control_intf.iInterface = ecm_string_defs[0].id;
880 status = usb_string_id(c->cdev); 841 ecm_data_intf.iInterface = ecm_string_defs[2].id;
881 if (status < 0) 842 ecm_desc.iMACAddress = ecm_string_defs[1].id;
882 return status; 843 ecm_iad_descriptor.iFunction = ecm_string_defs[3].id;
883 ecm_string_defs[1].id = status;
884 ecm_desc.iMACAddress = status;
885
886 /* IAD label */
887 status = usb_string_id(c->cdev);
888 if (status < 0)
889 return status;
890 ecm_string_defs[3].id = status;
891 ecm_iad_descriptor.iFunction = status;
892 } 844 }
893 845
894 /* allocate and initialize one new instance */ 846 /* allocate and initialize one new instance */
@@ -913,9 +865,7 @@ ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
913 ecm->port.func.disable = ecm_disable; 865 ecm->port.func.disable = ecm_disable;
914 866
915 status = usb_add_function(c, &ecm->port.func); 867 status = usb_add_function(c, &ecm->port.func);
916 if (status) { 868 if (status)
917 ecm_string_defs[1].s = NULL;
918 kfree(ecm); 869 kfree(ecm);
919 }
920 return status; 870 return status;
921} 871}
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
index 1a7b2dd7d408..cf0ebee85563 100644
--- a/drivers/usb/gadget/f_eem.c
+++ b/drivers/usb/gadget/f_eem.c
@@ -274,38 +274,20 @@ eem_bind(struct usb_configuration *c, struct usb_function *f)
274 274
275 status = -ENOMEM; 275 status = -ENOMEM;
276 276
277 /* copy descriptors, and track endpoint copies */
278 f->descriptors = usb_copy_descriptors(eem_fs_function);
279 if (!f->descriptors)
280 goto fail;
281
282 /* support all relevant hardware speeds... we expect that when 277 /* support all relevant hardware speeds... we expect that when
283 * hardware is dual speed, all bulk-capable endpoints work at 278 * hardware is dual speed, all bulk-capable endpoints work at
284 * both speeds 279 * both speeds
285 */ 280 */
286 if (gadget_is_dualspeed(c->cdev->gadget)) { 281 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
287 eem_hs_in_desc.bEndpointAddress = 282 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
288 eem_fs_in_desc.bEndpointAddress;
289 eem_hs_out_desc.bEndpointAddress =
290 eem_fs_out_desc.bEndpointAddress;
291
292 /* copy descriptors, and track endpoint copies */
293 f->hs_descriptors = usb_copy_descriptors(eem_hs_function);
294 if (!f->hs_descriptors)
295 goto fail;
296 }
297 283
298 if (gadget_is_superspeed(c->cdev->gadget)) { 284 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
299 eem_ss_in_desc.bEndpointAddress = 285 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
300 eem_fs_in_desc.bEndpointAddress;
301 eem_ss_out_desc.bEndpointAddress =
302 eem_fs_out_desc.bEndpointAddress;
303 286
304 /* copy descriptors, and track endpoint copies */ 287 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
305 f->ss_descriptors = usb_copy_descriptors(eem_ss_function); 288 eem_ss_function);
306 if (!f->ss_descriptors) 289 if (status)
307 goto fail; 290 goto fail;
308 }
309 291
310 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n", 292 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
311 gadget_is_superspeed(c->cdev->gadget) ? "super" : 293 gadget_is_superspeed(c->cdev->gadget) ? "super" :
@@ -314,15 +296,10 @@ eem_bind(struct usb_configuration *c, struct usb_function *f)
314 return 0; 296 return 0;
315 297
316fail: 298fail:
317 if (f->descriptors) 299 usb_free_all_descriptors(f);
318 usb_free_descriptors(f->descriptors); 300 if (eem->port.out_ep)
319 if (f->hs_descriptors)
320 usb_free_descriptors(f->hs_descriptors);
321
322 /* we might as well release our claims on endpoints */
323 if (eem->port.out_ep->desc)
324 eem->port.out_ep->driver_data = NULL; 301 eem->port.out_ep->driver_data = NULL;
325 if (eem->port.in_ep->desc) 302 if (eem->port.in_ep)
326 eem->port.in_ep->driver_data = NULL; 303 eem->port.in_ep->driver_data = NULL;
327 304
328 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 305 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
@@ -337,11 +314,7 @@ eem_unbind(struct usb_configuration *c, struct usb_function *f)
337 314
338 DBG(c->cdev, "eem unbind\n"); 315 DBG(c->cdev, "eem unbind\n");
339 316
340 if (gadget_is_superspeed(c->cdev->gadget)) 317 usb_free_all_descriptors(f);
341 usb_free_descriptors(f->ss_descriptors);
342 if (gadget_is_dualspeed(c->cdev->gadget))
343 usb_free_descriptors(f->hs_descriptors);
344 usb_free_descriptors(f->descriptors);
345 kfree(eem); 318 kfree(eem);
346} 319}
347 320
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index 64c4ec10d1fc..4a6961c517f2 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -2097,7 +2097,7 @@ static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2097 if (isHS) 2097 if (isHS)
2098 func->function.hs_descriptors[(long)valuep] = desc; 2098 func->function.hs_descriptors[(long)valuep] = desc;
2099 else 2099 else
2100 func->function.descriptors[(long)valuep] = desc; 2100 func->function.fs_descriptors[(long)valuep] = desc;
2101 2101
2102 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) 2102 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2103 return 0; 2103 return 0;
@@ -2249,7 +2249,7 @@ static int ffs_func_bind(struct usb_configuration *c,
2249 * numbers without worrying that it may be described later on. 2249 * numbers without worrying that it may be described later on.
2250 */ 2250 */
2251 if (likely(full)) { 2251 if (likely(full)) {
2252 func->function.descriptors = data->fs_descs; 2252 func->function.fs_descriptors = data->fs_descs;
2253 ret = ffs_do_descs(ffs->fs_descs_count, 2253 ret = ffs_do_descs(ffs->fs_descs_count,
2254 data->raw_descs, 2254 data->raw_descs,
2255 sizeof data->raw_descs, 2255 sizeof data->raw_descs,
diff --git a/drivers/usb/gadget/f_hid.c b/drivers/usb/gadget/f_hid.c
index 511e527178e2..6e69a8e8d22a 100644
--- a/drivers/usb/gadget/f_hid.c
+++ b/drivers/usb/gadget/f_hid.c
@@ -573,7 +573,6 @@ static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
573 goto fail; 573 goto fail;
574 hidg_interface_desc.bInterfaceNumber = status; 574 hidg_interface_desc.bInterfaceNumber = status;
575 575
576
577 /* allocate instance-specific endpoints */ 576 /* allocate instance-specific endpoints */
578 status = -ENODEV; 577 status = -ENODEV;
579 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc); 578 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
@@ -609,20 +608,15 @@ static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
609 hidg_desc.desc[0].wDescriptorLength = 608 hidg_desc.desc[0].wDescriptorLength =
610 cpu_to_le16(hidg->report_desc_length); 609 cpu_to_le16(hidg->report_desc_length);
611 610
612 /* copy descriptors */ 611 hidg_hs_in_ep_desc.bEndpointAddress =
613 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors); 612 hidg_fs_in_ep_desc.bEndpointAddress;
614 if (!f->descriptors) 613 hidg_hs_out_ep_desc.bEndpointAddress =
615 goto fail; 614 hidg_fs_out_ep_desc.bEndpointAddress;
616 615
617 if (gadget_is_dualspeed(c->cdev->gadget)) { 616 status = usb_assign_descriptors(f, hidg_fs_descriptors,
618 hidg_hs_in_ep_desc.bEndpointAddress = 617 hidg_hs_descriptors, NULL);
619 hidg_fs_in_ep_desc.bEndpointAddress; 618 if (status)
620 hidg_hs_out_ep_desc.bEndpointAddress = 619 goto fail;
621 hidg_fs_out_ep_desc.bEndpointAddress;
622 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
623 if (!f->hs_descriptors)
624 goto fail;
625 }
626 620
627 mutex_init(&hidg->lock); 621 mutex_init(&hidg->lock);
628 spin_lock_init(&hidg->spinlock); 622 spin_lock_init(&hidg->spinlock);
@@ -649,9 +643,7 @@ fail:
649 usb_ep_free_request(hidg->in_ep, hidg->req); 643 usb_ep_free_request(hidg->in_ep, hidg->req);
650 } 644 }
651 645
652 usb_free_descriptors(f->hs_descriptors); 646 usb_free_all_descriptors(f);
653 usb_free_descriptors(f->descriptors);
654
655 return status; 647 return status;
656} 648}
657 649
@@ -668,9 +660,7 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
668 kfree(hidg->req->buf); 660 kfree(hidg->req->buf);
669 usb_ep_free_request(hidg->in_ep, hidg->req); 661 usb_ep_free_request(hidg->in_ep, hidg->req);
670 662
671 /* free descriptors copies */ 663 usb_free_all_descriptors(f);
672 usb_free_descriptors(f->hs_descriptors);
673 usb_free_descriptors(f->descriptors);
674 664
675 kfree(hidg->report_desc); 665 kfree(hidg->report_desc);
676 kfree(hidg); 666 kfree(hidg);
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c
index 7275706caeb0..bb39cb2bb3a3 100644
--- a/drivers/usb/gadget/f_loopback.c
+++ b/drivers/usb/gadget/f_loopback.c
@@ -177,6 +177,7 @@ loopback_bind(struct usb_configuration *c, struct usb_function *f)
177 struct usb_composite_dev *cdev = c->cdev; 177 struct usb_composite_dev *cdev = c->cdev;
178 struct f_loopback *loop = func_to_loop(f); 178 struct f_loopback *loop = func_to_loop(f);
179 int id; 179 int id;
180 int ret;
180 181
181 /* allocate interface ID(s) */ 182 /* allocate interface ID(s) */
182 id = usb_interface_id(c, f); 183 id = usb_interface_id(c, f);
@@ -201,22 +202,19 @@ autoconf_fail:
201 loop->out_ep->driver_data = cdev; /* claim */ 202 loop->out_ep->driver_data = cdev; /* claim */
202 203
203 /* support high speed hardware */ 204 /* support high speed hardware */
204 if (gadget_is_dualspeed(c->cdev->gadget)) { 205 hs_loop_source_desc.bEndpointAddress =
205 hs_loop_source_desc.bEndpointAddress = 206 fs_loop_source_desc.bEndpointAddress;
206 fs_loop_source_desc.bEndpointAddress; 207 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
207 hs_loop_sink_desc.bEndpointAddress =
208 fs_loop_sink_desc.bEndpointAddress;
209 f->hs_descriptors = hs_loopback_descs;
210 }
211 208
212 /* support super speed hardware */ 209 /* support super speed hardware */
213 if (gadget_is_superspeed(c->cdev->gadget)) { 210 ss_loop_source_desc.bEndpointAddress =
214 ss_loop_source_desc.bEndpointAddress = 211 fs_loop_source_desc.bEndpointAddress;
215 fs_loop_source_desc.bEndpointAddress; 212 ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
216 ss_loop_sink_desc.bEndpointAddress = 213
217 fs_loop_sink_desc.bEndpointAddress; 214 ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
218 f->ss_descriptors = ss_loopback_descs; 215 ss_loopback_descs);
219 } 216 if (ret)
217 return ret;
220 218
221 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", 219 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
222 (gadget_is_superspeed(c->cdev->gadget) ? "super" : 220 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
@@ -228,6 +226,7 @@ autoconf_fail:
228static void 226static void
229loopback_unbind(struct usb_configuration *c, struct usb_function *f) 227loopback_unbind(struct usb_configuration *c, struct usb_function *f)
230{ 228{
229 usb_free_all_descriptors(f);
231 kfree(func_to_loop(f)); 230 kfree(func_to_loop(f));
232} 231}
233 232
@@ -379,7 +378,6 @@ static int __init loopback_bind_config(struct usb_configuration *c)
379 return -ENOMEM; 378 return -ENOMEM;
380 379
381 loop->function.name = "loopback"; 380 loop->function.name = "loopback";
382 loop->function.descriptors = fs_loopback_descs;
383 loop->function.bind = loopback_bind; 381 loop->function.bind = loopback_bind;
384 loop->function.unbind = loopback_unbind; 382 loop->function.unbind = loopback_unbind;
385 loop->function.set_alt = loopback_set_alt; 383 loop->function.set_alt = loopback_set_alt;
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 3a7668bde3ef..5d027b3e1ef0 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -228,10 +228,6 @@
228 228
229static const char fsg_string_interface[] = "Mass Storage"; 229static const char fsg_string_interface[] = "Mass Storage";
230 230
231#define FSG_NO_DEVICE_STRINGS 1
232#define FSG_NO_OTG 1
233#define FSG_NO_INTR_EP 1
234
235#include "storage_common.c" 231#include "storage_common.c"
236 232
237 233
@@ -2904,9 +2900,7 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2904 } 2900 }
2905 2901
2906 fsg_common_put(common); 2902 fsg_common_put(common);
2907 usb_free_descriptors(fsg->function.descriptors); 2903 usb_free_all_descriptors(&fsg->function);
2908 usb_free_descriptors(fsg->function.hs_descriptors);
2909 usb_free_descriptors(fsg->function.ss_descriptors);
2910 kfree(fsg); 2904 kfree(fsg);
2911} 2905}
2912 2906
@@ -2916,6 +2910,8 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2916 struct usb_gadget *gadget = c->cdev->gadget; 2910 struct usb_gadget *gadget = c->cdev->gadget;
2917 int i; 2911 int i;
2918 struct usb_ep *ep; 2912 struct usb_ep *ep;
2913 unsigned max_burst;
2914 int ret;
2919 2915
2920 fsg->gadget = gadget; 2916 fsg->gadget = gadget;
2921 2917
@@ -2939,45 +2935,27 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2939 ep->driver_data = fsg->common; /* claim the endpoint */ 2935 ep->driver_data = fsg->common; /* claim the endpoint */
2940 fsg->bulk_out = ep; 2936 fsg->bulk_out = ep;
2941 2937
2942 /* Copy descriptors */ 2938 /* Assume endpoint addresses are the same for both speeds */
2943 f->descriptors = usb_copy_descriptors(fsg_fs_function); 2939 fsg_hs_bulk_in_desc.bEndpointAddress =
2944 if (unlikely(!f->descriptors)) 2940 fsg_fs_bulk_in_desc.bEndpointAddress;
2945 return -ENOMEM; 2941 fsg_hs_bulk_out_desc.bEndpointAddress =
2946 2942 fsg_fs_bulk_out_desc.bEndpointAddress;
2947 if (gadget_is_dualspeed(gadget)) {
2948 /* Assume endpoint addresses are the same for both speeds */
2949 fsg_hs_bulk_in_desc.bEndpointAddress =
2950 fsg_fs_bulk_in_desc.bEndpointAddress;
2951 fsg_hs_bulk_out_desc.bEndpointAddress =
2952 fsg_fs_bulk_out_desc.bEndpointAddress;
2953 f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
2954 if (unlikely(!f->hs_descriptors)) {
2955 usb_free_descriptors(f->descriptors);
2956 return -ENOMEM;
2957 }
2958 }
2959
2960 if (gadget_is_superspeed(gadget)) {
2961 unsigned max_burst;
2962 2943
2963 /* Calculate bMaxBurst, we know packet size is 1024 */ 2944 /* Calculate bMaxBurst, we know packet size is 1024 */
2964 max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15); 2945 max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15);
2965 2946
2966 fsg_ss_bulk_in_desc.bEndpointAddress = 2947 fsg_ss_bulk_in_desc.bEndpointAddress =
2967 fsg_fs_bulk_in_desc.bEndpointAddress; 2948 fsg_fs_bulk_in_desc.bEndpointAddress;
2968 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst; 2949 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
2969 2950
2970 fsg_ss_bulk_out_desc.bEndpointAddress = 2951 fsg_ss_bulk_out_desc.bEndpointAddress =
2971 fsg_fs_bulk_out_desc.bEndpointAddress; 2952 fsg_fs_bulk_out_desc.bEndpointAddress;
2972 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst; 2953 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
2973 2954
2974 f->ss_descriptors = usb_copy_descriptors(fsg_ss_function); 2955 ret = usb_assign_descriptors(f, fsg_fs_function, fsg_hs_function,
2975 if (unlikely(!f->ss_descriptors)) { 2956 fsg_ss_function);
2976 usb_free_descriptors(f->hs_descriptors); 2957 if (ret)
2977 usb_free_descriptors(f->descriptors); 2958 goto autoconf_fail;
2978 return -ENOMEM;
2979 }
2980 }
2981 2959
2982 return 0; 2960 return 0;
2983 2961
@@ -2986,7 +2964,6 @@ autoconf_fail:
2986 return -ENOTSUPP; 2964 return -ENOTSUPP;
2987} 2965}
2988 2966
2989
2990/****************************** ADD FUNCTION ******************************/ 2967/****************************** ADD FUNCTION ******************************/
2991 2968
2992static struct usb_gadget_strings *fsg_strings_array[] = { 2969static struct usb_gadget_strings *fsg_strings_array[] = {
diff --git a/drivers/usb/gadget/f_midi.c b/drivers/usb/gadget/f_midi.c
index 8ed1259fe80d..263e721c2694 100644
--- a/drivers/usb/gadget/f_midi.c
+++ b/drivers/usb/gadget/f_midi.c
@@ -414,7 +414,7 @@ static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
414 kfree(midi->id); 414 kfree(midi->id);
415 midi->id = NULL; 415 midi->id = NULL;
416 416
417 usb_free_descriptors(f->descriptors); 417 usb_free_all_descriptors(f);
418 kfree(midi); 418 kfree(midi);
419} 419}
420 420
@@ -881,19 +881,25 @@ f_midi_bind(struct usb_configuration *c, struct usb_function *f)
881 * both speeds 881 * both speeds
882 */ 882 */
883 /* copy descriptors, and track endpoint copies */ 883 /* copy descriptors, and track endpoint copies */
884 f->fs_descriptors = usb_copy_descriptors(midi_function);
885 if (!f->fs_descriptors)
886 goto fail_f_midi;
887
884 if (gadget_is_dualspeed(c->cdev->gadget)) { 888 if (gadget_is_dualspeed(c->cdev->gadget)) {
885 c->highspeed = true;
886 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512); 889 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
887 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512); 890 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
888 f->hs_descriptors = usb_copy_descriptors(midi_function); 891 f->hs_descriptors = usb_copy_descriptors(midi_function);
889 } else { 892 if (!f->hs_descriptors)
890 f->descriptors = usb_copy_descriptors(midi_function); 893 goto fail_f_midi;
891 } 894 }
892 895
893 kfree(midi_function); 896 kfree(midi_function);
894 897
895 return 0; 898 return 0;
896 899
900fail_f_midi:
901 kfree(midi_function);
902 usb_free_descriptors(f->hs_descriptors);
897fail: 903fail:
898 /* we might as well release our claims on endpoints */ 904 /* we might as well release our claims on endpoints */
899 if (midi->out_ep) 905 if (midi->out_ep)
diff --git a/drivers/usb/gadget/f_ncm.c b/drivers/usb/gadget/f_ncm.c
index b651b529c67f..6c8362f937be 100644
--- a/drivers/usb/gadget/f_ncm.c
+++ b/drivers/usb/gadget/f_ncm.c
@@ -102,7 +102,7 @@ static inline unsigned ncm_bitrate(struct usb_gadget *g)
102 USB_CDC_NCM_NTB32_SUPPORTED) 102 USB_CDC_NCM_NTB32_SUPPORTED)
103 103
104static struct usb_cdc_ncm_ntb_parameters ntb_parameters = { 104static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
105 .wLength = sizeof ntb_parameters, 105 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
106 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED), 106 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
107 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE), 107 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
108 .wNdpInDivisor = cpu_to_le16(4), 108 .wNdpInDivisor = cpu_to_le16(4),
@@ -121,7 +121,7 @@ static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
121 * waste less bandwidth. 121 * waste less bandwidth.
122 */ 122 */
123 123
124#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 124#define NCM_STATUS_INTERVAL_MS 32
125#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ 125#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
126 126
127static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata = { 127static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata = {
@@ -230,7 +230,7 @@ static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata = {
230 .bEndpointAddress = USB_DIR_IN, 230 .bEndpointAddress = USB_DIR_IN,
231 .bmAttributes = USB_ENDPOINT_XFER_INT, 231 .bmAttributes = USB_ENDPOINT_XFER_INT,
232 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), 232 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
233 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 233 .bInterval = NCM_STATUS_INTERVAL_MS,
234}; 234};
235 235
236static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = { 236static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = {
@@ -275,7 +275,7 @@ static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata = {
275 .bEndpointAddress = USB_DIR_IN, 275 .bEndpointAddress = USB_DIR_IN,
276 .bmAttributes = USB_ENDPOINT_XFER_INT, 276 .bmAttributes = USB_ENDPOINT_XFER_INT,
277 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), 277 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
278 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 278 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
279}; 279};
280static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = { 280static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = {
281 .bLength = USB_DT_ENDPOINT_SIZE, 281 .bLength = USB_DT_ENDPOINT_SIZE,
@@ -321,7 +321,7 @@ static struct usb_descriptor_header *ncm_hs_function[] __initdata = {
321 321
322static struct usb_string ncm_string_defs[] = { 322static struct usb_string ncm_string_defs[] = {
323 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)", 323 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
324 [STRING_MAC_IDX].s = NULL /* DYNAMIC */, 324 [STRING_MAC_IDX].s = "",
325 [STRING_DATA_IDX].s = "CDC Network Data", 325 [STRING_DATA_IDX].s = "CDC Network Data",
326 [STRING_IAD_IDX].s = "CDC NCM", 326 [STRING_IAD_IDX].s = "CDC NCM",
327 { } /* end of list */ 327 { } /* end of list */
@@ -869,15 +869,19 @@ static struct sk_buff *ncm_wrap_ntb(struct gether *port,
869 struct sk_buff *skb2; 869 struct sk_buff *skb2;
870 int ncb_len = 0; 870 int ncb_len = 0;
871 __le16 *tmp; 871 __le16 *tmp;
872 int div = ntb_parameters.wNdpInDivisor; 872 int div;
873 int rem = ntb_parameters.wNdpInPayloadRemainder; 873 int rem;
874 int pad; 874 int pad;
875 int ndp_align = ntb_parameters.wNdpInAlignment; 875 int ndp_align;
876 int ndp_pad; 876 int ndp_pad;
877 unsigned max_size = ncm->port.fixed_in_len; 877 unsigned max_size = ncm->port.fixed_in_len;
878 struct ndp_parser_opts *opts = ncm->parser_opts; 878 struct ndp_parser_opts *opts = ncm->parser_opts;
879 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0; 879 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
880 880
881 div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
882 rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
883 ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
884
881 ncb_len += opts->nth_size; 885 ncb_len += opts->nth_size;
882 ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len; 886 ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len;
883 ncb_len += ndp_pad; 887 ncb_len += ndp_pad;
@@ -1208,30 +1212,18 @@ ncm_bind(struct usb_configuration *c, struct usb_function *f)
1208 ncm->notify_req->context = ncm; 1212 ncm->notify_req->context = ncm;
1209 ncm->notify_req->complete = ncm_notify_complete; 1213 ncm->notify_req->complete = ncm_notify_complete;
1210 1214
1211 /* copy descriptors, and track endpoint copies */
1212 f->descriptors = usb_copy_descriptors(ncm_fs_function);
1213 if (!f->descriptors)
1214 goto fail;
1215
1216 /* 1215 /*
1217 * support all relevant hardware speeds... we expect that when 1216 * support all relevant hardware speeds... we expect that when
1218 * hardware is dual speed, all bulk-capable endpoints work at 1217 * hardware is dual speed, all bulk-capable endpoints work at
1219 * both speeds 1218 * both speeds
1220 */ 1219 */
1221 if (gadget_is_dualspeed(c->cdev->gadget)) { 1220 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1222 hs_ncm_in_desc.bEndpointAddress = 1221 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1223 fs_ncm_in_desc.bEndpointAddress; 1222 hs_ncm_notify_desc.bEndpointAddress =
1224 hs_ncm_out_desc.bEndpointAddress = 1223 fs_ncm_notify_desc.bEndpointAddress;
1225 fs_ncm_out_desc.bEndpointAddress;
1226 hs_ncm_notify_desc.bEndpointAddress =
1227 fs_ncm_notify_desc.bEndpointAddress;
1228
1229 /* copy descriptors, and track endpoint copies */
1230 f->hs_descriptors = usb_copy_descriptors(ncm_hs_function);
1231 if (!f->hs_descriptors)
1232 goto fail;
1233 }
1234 1224
1225 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1226 NULL);
1235 /* 1227 /*
1236 * NOTE: all that is done without knowing or caring about 1228 * NOTE: all that is done without knowing or caring about
1237 * the network link ... which is unavailable to this code 1229 * the network link ... which is unavailable to this code
@@ -1248,9 +1240,7 @@ ncm_bind(struct usb_configuration *c, struct usb_function *f)
1248 return 0; 1240 return 0;
1249 1241
1250fail: 1242fail:
1251 if (f->descriptors) 1243 usb_free_all_descriptors(f);
1252 usb_free_descriptors(f->descriptors);
1253
1254 if (ncm->notify_req) { 1244 if (ncm->notify_req) {
1255 kfree(ncm->notify_req->buf); 1245 kfree(ncm->notify_req->buf);
1256 usb_ep_free_request(ncm->notify, ncm->notify_req); 1246 usb_ep_free_request(ncm->notify, ncm->notify_req);
@@ -1259,9 +1249,9 @@ fail:
1259 /* we might as well release our claims on endpoints */ 1249 /* we might as well release our claims on endpoints */
1260 if (ncm->notify) 1250 if (ncm->notify)
1261 ncm->notify->driver_data = NULL; 1251 ncm->notify->driver_data = NULL;
1262 if (ncm->port.out_ep->desc) 1252 if (ncm->port.out_ep)
1263 ncm->port.out_ep->driver_data = NULL; 1253 ncm->port.out_ep->driver_data = NULL;
1264 if (ncm->port.in_ep->desc) 1254 if (ncm->port.in_ep)
1265 ncm->port.in_ep->driver_data = NULL; 1255 ncm->port.in_ep->driver_data = NULL;
1266 1256
1267 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 1257 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
@@ -1276,14 +1266,12 @@ ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1276 1266
1277 DBG(c->cdev, "ncm unbind\n"); 1267 DBG(c->cdev, "ncm unbind\n");
1278 1268
1279 if (gadget_is_dualspeed(c->cdev->gadget)) 1269 ncm_string_defs[0].id = 0;
1280 usb_free_descriptors(f->hs_descriptors); 1270 usb_free_all_descriptors(f);
1281 usb_free_descriptors(f->descriptors);
1282 1271
1283 kfree(ncm->notify_req->buf); 1272 kfree(ncm->notify_req->buf);
1284 usb_ep_free_request(ncm->notify, ncm->notify_req); 1273 usb_ep_free_request(ncm->notify, ncm->notify_req);
1285 1274
1286 ncm_string_defs[1].s = NULL;
1287 kfree(ncm); 1275 kfree(ncm);
1288} 1276}
1289 1277
@@ -1307,37 +1295,19 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1307 if (!can_support_ecm(c->cdev->gadget) || !ethaddr) 1295 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
1308 return -EINVAL; 1296 return -EINVAL;
1309 1297
1310 /* maybe allocate device-global string IDs */
1311 if (ncm_string_defs[0].id == 0) { 1298 if (ncm_string_defs[0].id == 0) {
1312 1299 status = usb_string_ids_tab(c->cdev, ncm_string_defs);
1313 /* control interface label */
1314 status = usb_string_id(c->cdev);
1315 if (status < 0) 1300 if (status < 0)
1316 return status; 1301 return status;
1317 ncm_string_defs[STRING_CTRL_IDX].id = status; 1302 ncm_control_intf.iInterface =
1318 ncm_control_intf.iInterface = status; 1303 ncm_string_defs[STRING_CTRL_IDX].id;
1319 1304
1320 /* data interface label */ 1305 status = ncm_string_defs[STRING_DATA_IDX].id;
1321 status = usb_string_id(c->cdev);
1322 if (status < 0)
1323 return status;
1324 ncm_string_defs[STRING_DATA_IDX].id = status;
1325 ncm_data_nop_intf.iInterface = status; 1306 ncm_data_nop_intf.iInterface = status;
1326 ncm_data_intf.iInterface = status; 1307 ncm_data_intf.iInterface = status;
1327 1308
1328 /* MAC address */ 1309 ecm_desc.iMACAddress = ncm_string_defs[STRING_MAC_IDX].id;
1329 status = usb_string_id(c->cdev); 1310 ncm_iad_desc.iFunction = ncm_string_defs[STRING_IAD_IDX].id;
1330 if (status < 0)
1331 return status;
1332 ncm_string_defs[STRING_MAC_IDX].id = status;
1333 ecm_desc.iMACAddress = status;
1334
1335 /* IAD */
1336 status = usb_string_id(c->cdev);
1337 if (status < 0)
1338 return status;
1339 ncm_string_defs[STRING_IAD_IDX].id = status;
1340 ncm_iad_desc.iFunction = status;
1341 } 1311 }
1342 1312
1343 /* allocate and initialize one new instance */ 1313 /* allocate and initialize one new instance */
@@ -1347,7 +1317,7 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1347 1317
1348 /* export host's Ethernet address in CDC format */ 1318 /* export host's Ethernet address in CDC format */
1349 snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr); 1319 snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr);
1350 ncm_string_defs[1].s = ncm->ethaddr; 1320 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1351 1321
1352 spin_lock_init(&ncm->lock); 1322 spin_lock_init(&ncm->lock);
1353 ncm_reset_values(ncm); 1323 ncm_reset_values(ncm);
@@ -1367,9 +1337,7 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1367 ncm->port.unwrap = ncm_unwrap_ntb; 1337 ncm->port.unwrap = ncm_unwrap_ntb;
1368 1338
1369 status = usb_add_function(c, &ncm->port.func); 1339 status = usb_add_function(c, &ncm->port.func);
1370 if (status) { 1340 if (status)
1371 ncm_string_defs[1].s = NULL;
1372 kfree(ncm); 1341 kfree(ncm);
1373 }
1374 return status; 1342 return status;
1375} 1343}
diff --git a/drivers/usb/gadget/f_obex.c b/drivers/usb/gadget/f_obex.c
index 5f400f66aa9b..d8dd8782768c 100644
--- a/drivers/usb/gadget/f_obex.c
+++ b/drivers/usb/gadget/f_obex.c
@@ -331,23 +331,19 @@ obex_bind(struct usb_configuration *c, struct usb_function *f)
331 obex->port.out = ep; 331 obex->port.out = ep;
332 ep->driver_data = cdev; /* claim */ 332 ep->driver_data = cdev; /* claim */
333 333
334 /* copy descriptors, and track endpoint copies */
335 f->descriptors = usb_copy_descriptors(fs_function);
336
337 /* support all relevant hardware speeds... we expect that when 334 /* support all relevant hardware speeds... we expect that when
338 * hardware is dual speed, all bulk-capable endpoints work at 335 * hardware is dual speed, all bulk-capable endpoints work at
339 * both speeds 336 * both speeds
340 */ 337 */
341 if (gadget_is_dualspeed(c->cdev->gadget)) {
342 338
343 obex_hs_ep_in_desc.bEndpointAddress = 339 obex_hs_ep_in_desc.bEndpointAddress =
344 obex_fs_ep_in_desc.bEndpointAddress; 340 obex_fs_ep_in_desc.bEndpointAddress;
345 obex_hs_ep_out_desc.bEndpointAddress = 341 obex_hs_ep_out_desc.bEndpointAddress =
346 obex_fs_ep_out_desc.bEndpointAddress; 342 obex_fs_ep_out_desc.bEndpointAddress;
347 343
348 /* copy descriptors, and track endpoint copies */ 344 status = usb_assign_descriptors(f, fs_function, hs_function, NULL);
349 f->hs_descriptors = usb_copy_descriptors(hs_function); 345 if (status)
350 } 346 goto fail;
351 347
352 /* Avoid letting this gadget enumerate until the userspace 348 /* Avoid letting this gadget enumerate until the userspace
353 * OBEX server is active. 349 * OBEX server is active.
@@ -368,6 +364,7 @@ obex_bind(struct usb_configuration *c, struct usb_function *f)
368 return 0; 364 return 0;
369 365
370fail: 366fail:
367 usb_free_all_descriptors(f);
371 /* we might as well release our claims on endpoints */ 368 /* we might as well release our claims on endpoints */
372 if (obex->port.out) 369 if (obex->port.out)
373 obex->port.out->driver_data = NULL; 370 obex->port.out->driver_data = NULL;
@@ -382,9 +379,8 @@ fail:
382static void 379static void
383obex_unbind(struct usb_configuration *c, struct usb_function *f) 380obex_unbind(struct usb_configuration *c, struct usb_function *f)
384{ 381{
385 if (gadget_is_dualspeed(c->cdev->gadget)) 382 obex_string_defs[OBEX_CTRL_IDX].id = 0;
386 usb_free_descriptors(f->hs_descriptors); 383 usb_free_all_descriptors(f);
387 usb_free_descriptors(f->descriptors);
388 kfree(func_to_obex(f)); 384 kfree(func_to_obex(f));
389} 385}
390 386
@@ -423,22 +419,16 @@ int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
423 if (!can_support_obex(c)) 419 if (!can_support_obex(c))
424 return -EINVAL; 420 return -EINVAL;
425 421
426 /* maybe allocate device-global string IDs, and patch descriptors */
427 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) { 422 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
428 status = usb_string_id(c->cdev); 423 status = usb_string_ids_tab(c->cdev, obex_string_defs);
429 if (status < 0)
430 return status;
431 obex_string_defs[OBEX_CTRL_IDX].id = status;
432
433 obex_control_intf.iInterface = status;
434
435 status = usb_string_id(c->cdev);
436 if (status < 0) 424 if (status < 0)
437 return status; 425 return status;
438 obex_string_defs[OBEX_DATA_IDX].id = status; 426 obex_control_intf.iInterface =
427 obex_string_defs[OBEX_CTRL_IDX].id;
439 428
440 obex_data_nop_intf.iInterface = 429 status = obex_string_defs[OBEX_DATA_IDX].id;
441 obex_data_intf.iInterface = status; 430 obex_data_nop_intf.iInterface = status;
431 obex_data_intf.iInterface = status;
442 } 432 }
443 433
444 /* allocate and initialize one new instance */ 434 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c
index 8ee9268fe253..b21ab558b6c0 100644
--- a/drivers/usb/gadget/f_phonet.c
+++ b/drivers/usb/gadget/f_phonet.c
@@ -515,14 +515,14 @@ int pn_bind(struct usb_configuration *c, struct usb_function *f)
515 fp->in_ep = ep; 515 fp->in_ep = ep;
516 ep->driver_data = fp; /* Claim */ 516 ep->driver_data = fp; /* Claim */
517 517
518 pn_hs_sink_desc.bEndpointAddress = 518 pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress;
519 pn_fs_sink_desc.bEndpointAddress; 519 pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress;
520 pn_hs_source_desc.bEndpointAddress =
521 pn_fs_source_desc.bEndpointAddress;
522 520
523 /* Do not try to bind Phonet twice... */ 521 /* Do not try to bind Phonet twice... */
524 fp->function.descriptors = fs_pn_function; 522 status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function,
525 fp->function.hs_descriptors = hs_pn_function; 523 NULL);
524 if (status)
525 goto err;
526 526
527 /* Incoming USB requests */ 527 /* Incoming USB requests */
528 status = -ENOMEM; 528 status = -ENOMEM;
@@ -531,7 +531,7 @@ int pn_bind(struct usb_configuration *c, struct usb_function *f)
531 531
532 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL); 532 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
533 if (!req) 533 if (!req)
534 goto err; 534 goto err_req;
535 535
536 req->complete = pn_rx_complete; 536 req->complete = pn_rx_complete;
537 fp->out_reqv[i] = req; 537 fp->out_reqv[i] = req;
@@ -540,14 +540,18 @@ int pn_bind(struct usb_configuration *c, struct usb_function *f)
540 /* Outgoing USB requests */ 540 /* Outgoing USB requests */
541 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL); 541 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
542 if (!fp->in_req) 542 if (!fp->in_req)
543 goto err; 543 goto err_req;
544 544
545 INFO(cdev, "USB CDC Phonet function\n"); 545 INFO(cdev, "USB CDC Phonet function\n");
546 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name, 546 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
547 fp->out_ep->name, fp->in_ep->name); 547 fp->out_ep->name, fp->in_ep->name);
548 return 0; 548 return 0;
549 549
550err_req:
551 for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++)
552 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
550err: 553err:
554 usb_free_all_descriptors(f);
551 if (fp->out_ep) 555 if (fp->out_ep)
552 fp->out_ep->driver_data = NULL; 556 fp->out_ep->driver_data = NULL;
553 if (fp->in_ep) 557 if (fp->in_ep)
@@ -569,6 +573,7 @@ pn_unbind(struct usb_configuration *c, struct usb_function *f)
569 if (fp->out_reqv[i]) 573 if (fp->out_reqv[i])
570 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]); 574 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
571 575
576 usb_free_all_descriptors(f);
572 kfree(fp); 577 kfree(fp);
573} 578}
574 579
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index b1681e45aca7..71beeb833558 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -101,7 +101,7 @@ static unsigned int bitrate(struct usb_gadget *g)
101/* 101/*
102 */ 102 */
103 103
104#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ 104#define RNDIS_STATUS_INTERVAL_MS 32
105#define STATUS_BYTECOUNT 8 /* 8 bytes data */ 105#define STATUS_BYTECOUNT 8 /* 8 bytes data */
106 106
107 107
@@ -190,7 +190,7 @@ static struct usb_endpoint_descriptor fs_notify_desc = {
190 .bEndpointAddress = USB_DIR_IN, 190 .bEndpointAddress = USB_DIR_IN,
191 .bmAttributes = USB_ENDPOINT_XFER_INT, 191 .bmAttributes = USB_ENDPOINT_XFER_INT,
192 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), 192 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
193 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 193 .bInterval = RNDIS_STATUS_INTERVAL_MS,
194}; 194};
195 195
196static struct usb_endpoint_descriptor fs_in_desc = { 196static struct usb_endpoint_descriptor fs_in_desc = {
@@ -236,7 +236,7 @@ static struct usb_endpoint_descriptor hs_notify_desc = {
236 .bEndpointAddress = USB_DIR_IN, 236 .bEndpointAddress = USB_DIR_IN,
237 .bmAttributes = USB_ENDPOINT_XFER_INT, 237 .bmAttributes = USB_ENDPOINT_XFER_INT,
238 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), 238 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
239 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 239 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
240}; 240};
241 241
242static struct usb_endpoint_descriptor hs_in_desc = { 242static struct usb_endpoint_descriptor hs_in_desc = {
@@ -284,7 +284,7 @@ static struct usb_endpoint_descriptor ss_notify_desc = {
284 .bEndpointAddress = USB_DIR_IN, 284 .bEndpointAddress = USB_DIR_IN,
285 .bmAttributes = USB_ENDPOINT_XFER_INT, 285 .bmAttributes = USB_ENDPOINT_XFER_INT,
286 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), 286 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
287 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 287 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
288}; 288};
289 289
290static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = { 290static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
@@ -722,42 +722,22 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
722 rndis->notify_req->context = rndis; 722 rndis->notify_req->context = rndis;
723 rndis->notify_req->complete = rndis_response_complete; 723 rndis->notify_req->complete = rndis_response_complete;
724 724
725 /* copy descriptors, and track endpoint copies */
726 f->descriptors = usb_copy_descriptors(eth_fs_function);
727 if (!f->descriptors)
728 goto fail;
729
730 /* support all relevant hardware speeds... we expect that when 725 /* support all relevant hardware speeds... we expect that when
731 * hardware is dual speed, all bulk-capable endpoints work at 726 * hardware is dual speed, all bulk-capable endpoints work at
732 * both speeds 727 * both speeds
733 */ 728 */
734 if (gadget_is_dualspeed(c->cdev->gadget)) { 729 hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
735 hs_in_desc.bEndpointAddress = 730 hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
736 fs_in_desc.bEndpointAddress; 731 hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
737 hs_out_desc.bEndpointAddress =
738 fs_out_desc.bEndpointAddress;
739 hs_notify_desc.bEndpointAddress =
740 fs_notify_desc.bEndpointAddress;
741
742 /* copy descriptors, and track endpoint copies */
743 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
744 if (!f->hs_descriptors)
745 goto fail;
746 }
747 732
748 if (gadget_is_superspeed(c->cdev->gadget)) { 733 ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
749 ss_in_desc.bEndpointAddress = 734 ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
750 fs_in_desc.bEndpointAddress; 735 ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
751 ss_out_desc.bEndpointAddress = 736
752 fs_out_desc.bEndpointAddress; 737 status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
753 ss_notify_desc.bEndpointAddress = 738 eth_ss_function);
754 fs_notify_desc.bEndpointAddress; 739 if (status)
755 740 goto fail;
756 /* copy descriptors, and track endpoint copies */
757 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
758 if (!f->ss_descriptors)
759 goto fail;
760 }
761 741
762 rndis->port.open = rndis_open; 742 rndis->port.open = rndis_open;
763 rndis->port.close = rndis_close; 743 rndis->port.close = rndis_close;
@@ -788,12 +768,7 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f)
788 return 0; 768 return 0;
789 769
790fail: 770fail:
791 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors) 771 usb_free_all_descriptors(f);
792 usb_free_descriptors(f->ss_descriptors);
793 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
794 usb_free_descriptors(f->hs_descriptors);
795 if (f->descriptors)
796 usb_free_descriptors(f->descriptors);
797 772
798 if (rndis->notify_req) { 773 if (rndis->notify_req) {
799 kfree(rndis->notify_req->buf); 774 kfree(rndis->notify_req->buf);
@@ -803,9 +778,9 @@ fail:
803 /* we might as well release our claims on endpoints */ 778 /* we might as well release our claims on endpoints */
804 if (rndis->notify) 779 if (rndis->notify)
805 rndis->notify->driver_data = NULL; 780 rndis->notify->driver_data = NULL;
806 if (rndis->port.out_ep->desc) 781 if (rndis->port.out_ep)
807 rndis->port.out_ep->driver_data = NULL; 782 rndis->port.out_ep->driver_data = NULL;
808 if (rndis->port.in_ep->desc) 783 if (rndis->port.in_ep)
809 rndis->port.in_ep->driver_data = NULL; 784 rndis->port.in_ep->driver_data = NULL;
810 785
811 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 786 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
@@ -820,13 +795,9 @@ rndis_unbind(struct usb_configuration *c, struct usb_function *f)
820 795
821 rndis_deregister(rndis->config); 796 rndis_deregister(rndis->config);
822 rndis_exit(); 797 rndis_exit();
823 rndis_string_defs[0].id = 0;
824 798
825 if (gadget_is_superspeed(c->cdev->gadget)) 799 rndis_string_defs[0].id = 0;
826 usb_free_descriptors(f->ss_descriptors); 800 usb_free_all_descriptors(f);
827 if (gadget_is_dualspeed(c->cdev->gadget))
828 usb_free_descriptors(f->hs_descriptors);
829 usb_free_descriptors(f->descriptors);
830 801
831 kfree(rndis->notify_req->buf); 802 kfree(rndis->notify_req->buf);
832 usb_ep_free_request(rndis->notify, rndis->notify_req); 803 usb_ep_free_request(rndis->notify, rndis->notify_req);
@@ -851,34 +822,19 @@ rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
851 if (!can_support_rndis(c) || !ethaddr) 822 if (!can_support_rndis(c) || !ethaddr)
852 return -EINVAL; 823 return -EINVAL;
853 824
854 /* maybe allocate device-global string IDs */
855 if (rndis_string_defs[0].id == 0) { 825 if (rndis_string_defs[0].id == 0) {
856
857 /* ... and setup RNDIS itself */ 826 /* ... and setup RNDIS itself */
858 status = rndis_init(); 827 status = rndis_init();
859 if (status < 0) 828 if (status < 0)
860 return status; 829 return status;
861 830
862 /* control interface label */ 831 status = usb_string_ids_tab(c->cdev, rndis_string_defs);
863 status = usb_string_id(c->cdev); 832 if (status)
864 if (status < 0)
865 return status; 833 return status;
866 rndis_string_defs[0].id = status;
867 rndis_control_intf.iInterface = status;
868 834
869 /* data interface label */ 835 rndis_control_intf.iInterface = rndis_string_defs[0].id;
870 status = usb_string_id(c->cdev); 836 rndis_data_intf.iInterface = rndis_string_defs[1].id;
871 if (status < 0) 837 rndis_iad_descriptor.iFunction = rndis_string_defs[2].id;
872 return status;
873 rndis_string_defs[1].id = status;
874 rndis_data_intf.iInterface = status;
875
876 /* IAD iFunction label */
877 status = usb_string_id(c->cdev);
878 if (status < 0)
879 return status;
880 rndis_string_defs[2].id = status;
881 rndis_iad_descriptor.iFunction = status;
882 } 838 }
883 839
884 /* allocate and initialize one new instance */ 840 /* allocate and initialize one new instance */
diff --git a/drivers/usb/gadget/f_serial.c b/drivers/usb/gadget/f_serial.c
index 07197d63d9b1..98fa7795df5f 100644
--- a/drivers/usb/gadget/f_serial.c
+++ b/drivers/usb/gadget/f_serial.c
@@ -213,34 +213,20 @@ gser_bind(struct usb_configuration *c, struct usb_function *f)
213 gser->port.out = ep; 213 gser->port.out = ep;
214 ep->driver_data = cdev; /* claim */ 214 ep->driver_data = cdev; /* claim */
215 215
216 /* copy descriptors, and track endpoint copies */
217 f->descriptors = usb_copy_descriptors(gser_fs_function);
218
219 /* support all relevant hardware speeds... we expect that when 216 /* support all relevant hardware speeds... we expect that when
220 * hardware is dual speed, all bulk-capable endpoints work at 217 * hardware is dual speed, all bulk-capable endpoints work at
221 * both speeds 218 * both speeds
222 */ 219 */
223 if (gadget_is_dualspeed(c->cdev->gadget)) { 220 gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
224 gser_hs_in_desc.bEndpointAddress = 221 gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
225 gser_fs_in_desc.bEndpointAddress;
226 gser_hs_out_desc.bEndpointAddress =
227 gser_fs_out_desc.bEndpointAddress;
228
229 /* copy descriptors, and track endpoint copies */
230 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
231 }
232 if (gadget_is_superspeed(c->cdev->gadget)) {
233 gser_ss_in_desc.bEndpointAddress =
234 gser_fs_in_desc.bEndpointAddress;
235 gser_ss_out_desc.bEndpointAddress =
236 gser_fs_out_desc.bEndpointAddress;
237
238 /* copy descriptors, and track endpoint copies */
239 f->ss_descriptors = usb_copy_descriptors(gser_ss_function);
240 if (!f->ss_descriptors)
241 goto fail;
242 }
243 222
223 gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
224 gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
225
226 status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
227 gser_ss_function);
228 if (status)
229 goto fail;
244 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n", 230 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
245 gser->port_num, 231 gser->port_num,
246 gadget_is_superspeed(c->cdev->gadget) ? "super" : 232 gadget_is_superspeed(c->cdev->gadget) ? "super" :
@@ -263,11 +249,7 @@ fail:
263static void 249static void
264gser_unbind(struct usb_configuration *c, struct usb_function *f) 250gser_unbind(struct usb_configuration *c, struct usb_function *f)
265{ 251{
266 if (gadget_is_dualspeed(c->cdev->gadget)) 252 usb_free_all_descriptors(f);
267 usb_free_descriptors(f->hs_descriptors);
268 if (gadget_is_superspeed(c->cdev->gadget))
269 usb_free_descriptors(f->ss_descriptors);
270 usb_free_descriptors(f->descriptors);
271 kfree(func_to_gser(f)); 253 kfree(func_to_gser(f));
272} 254}
273 255
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c
index 3c126fde6e7e..102d49beb9df 100644
--- a/drivers/usb/gadget/f_sourcesink.c
+++ b/drivers/usb/gadget/f_sourcesink.c
@@ -319,6 +319,7 @@ sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
319 struct usb_composite_dev *cdev = c->cdev; 319 struct usb_composite_dev *cdev = c->cdev;
320 struct f_sourcesink *ss = func_to_ss(f); 320 struct f_sourcesink *ss = func_to_ss(f);
321 int id; 321 int id;
322 int ret;
322 323
323 /* allocate interface ID(s) */ 324 /* allocate interface ID(s) */
324 id = usb_interface_id(c, f); 325 id = usb_interface_id(c, f);
@@ -387,64 +388,57 @@ no_iso:
387 isoc_maxpacket = 1024; 388 isoc_maxpacket = 1024;
388 389
389 /* support high speed hardware */ 390 /* support high speed hardware */
390 if (gadget_is_dualspeed(c->cdev->gadget)) { 391 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
391 hs_source_desc.bEndpointAddress = 392 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
392 fs_source_desc.bEndpointAddress;
393 hs_sink_desc.bEndpointAddress =
394 fs_sink_desc.bEndpointAddress;
395 393
396 /* 394 /*
397 * Fill in the HS isoc descriptors from the module parameters. 395 * Fill in the HS isoc descriptors from the module parameters.
398 * We assume that the user knows what they are doing and won't 396 * We assume that the user knows what they are doing and won't
399 * give parameters that their UDC doesn't support. 397 * give parameters that their UDC doesn't support.
400 */ 398 */
401 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket; 399 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
402 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11; 400 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
403 hs_iso_source_desc.bInterval = isoc_interval; 401 hs_iso_source_desc.bInterval = isoc_interval;
404 hs_iso_source_desc.bEndpointAddress = 402 hs_iso_source_desc.bEndpointAddress =
405 fs_iso_source_desc.bEndpointAddress; 403 fs_iso_source_desc.bEndpointAddress;
406 404
407 hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; 405 hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
408 hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11; 406 hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
409 hs_iso_sink_desc.bInterval = isoc_interval; 407 hs_iso_sink_desc.bInterval = isoc_interval;
410 hs_iso_sink_desc.bEndpointAddress = 408 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
411 fs_iso_sink_desc.bEndpointAddress;
412
413 f->hs_descriptors = hs_source_sink_descs;
414 }
415 409
416 /* support super speed hardware */ 410 /* support super speed hardware */
417 if (gadget_is_superspeed(c->cdev->gadget)) { 411 ss_source_desc.bEndpointAddress =
418 ss_source_desc.bEndpointAddress = 412 fs_source_desc.bEndpointAddress;
419 fs_source_desc.bEndpointAddress; 413 ss_sink_desc.bEndpointAddress =
420 ss_sink_desc.bEndpointAddress = 414 fs_sink_desc.bEndpointAddress;
421 fs_sink_desc.bEndpointAddress;
422 415
423 /* 416 /*
424 * Fill in the SS isoc descriptors from the module parameters. 417 * Fill in the SS isoc descriptors from the module parameters.
425 * We assume that the user knows what they are doing and won't 418 * We assume that the user knows what they are doing and won't
426 * give parameters that their UDC doesn't support. 419 * give parameters that their UDC doesn't support.
427 */ 420 */
428 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket; 421 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
429 ss_iso_source_desc.bInterval = isoc_interval; 422 ss_iso_source_desc.bInterval = isoc_interval;
430 ss_iso_source_comp_desc.bmAttributes = isoc_mult; 423 ss_iso_source_comp_desc.bmAttributes = isoc_mult;
431 ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst; 424 ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
432 ss_iso_source_comp_desc.wBytesPerInterval = 425 ss_iso_source_comp_desc.wBytesPerInterval =
433 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); 426 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
434 ss_iso_source_desc.bEndpointAddress = 427 ss_iso_source_desc.bEndpointAddress =
435 fs_iso_source_desc.bEndpointAddress; 428 fs_iso_source_desc.bEndpointAddress;
436 429
437 ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; 430 ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
438 ss_iso_sink_desc.bInterval = isoc_interval; 431 ss_iso_sink_desc.bInterval = isoc_interval;
439 ss_iso_sink_comp_desc.bmAttributes = isoc_mult; 432 ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
440 ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst; 433 ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
441 ss_iso_sink_comp_desc.wBytesPerInterval = 434 ss_iso_sink_comp_desc.wBytesPerInterval =
442 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); 435 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
443 ss_iso_sink_desc.bEndpointAddress = 436 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
444 fs_iso_sink_desc.bEndpointAddress; 437
445 438 ret = usb_assign_descriptors(f, fs_source_sink_descs,
446 f->ss_descriptors = ss_source_sink_descs; 439 hs_source_sink_descs, ss_source_sink_descs);
447 } 440 if (ret)
441 return ret;
448 442
449 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n", 443 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
450 (gadget_is_superspeed(c->cdev->gadget) ? "super" : 444 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
@@ -458,6 +452,7 @@ no_iso:
458static void 452static void
459sourcesink_unbind(struct usb_configuration *c, struct usb_function *f) 453sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
460{ 454{
455 usb_free_all_descriptors(f);
461 kfree(func_to_ss(f)); 456 kfree(func_to_ss(f));
462} 457}
463 458
@@ -773,7 +768,6 @@ static int __init sourcesink_bind_config(struct usb_configuration *c)
773 return -ENOMEM; 768 return -ENOMEM;
774 769
775 ss->function.name = "source/sink"; 770 ss->function.name = "source/sink";
776 ss->function.descriptors = fs_source_sink_descs;
777 ss->function.bind = sourcesink_bind; 771 ss->function.bind = sourcesink_bind;
778 ss->function.unbind = sourcesink_unbind; 772 ss->function.unbind = sourcesink_unbind;
779 ss->function.set_alt = sourcesink_set_alt; 773 ss->function.set_alt = sourcesink_set_alt;
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c
index 4060c0bd9785..f172bd152fbb 100644
--- a/drivers/usb/gadget/f_subset.c
+++ b/drivers/usb/gadget/f_subset.c
@@ -236,7 +236,7 @@ static struct usb_descriptor_header *ss_eth_function[] = {
236 236
237static struct usb_string geth_string_defs[] = { 237static struct usb_string geth_string_defs[] = {
238 [0].s = "CDC Ethernet Subset/SAFE", 238 [0].s = "CDC Ethernet Subset/SAFE",
239 [1].s = NULL /* DYNAMIC */, 239 [1].s = "",
240 { } /* end of list */ 240 { } /* end of list */
241}; 241};
242 242
@@ -319,38 +319,22 @@ geth_bind(struct usb_configuration *c, struct usb_function *f)
319 geth->port.out_ep = ep; 319 geth->port.out_ep = ep;
320 ep->driver_data = cdev; /* claim */ 320 ep->driver_data = cdev; /* claim */
321 321
322 /* copy descriptors, and track endpoint copies */
323 f->descriptors = usb_copy_descriptors(fs_eth_function);
324 if (!f->descriptors)
325 goto fail;
326
327 /* support all relevant hardware speeds... we expect that when 322 /* support all relevant hardware speeds... we expect that when
328 * hardware is dual speed, all bulk-capable endpoints work at 323 * hardware is dual speed, all bulk-capable endpoints work at
329 * both speeds 324 * both speeds
330 */ 325 */
331 if (gadget_is_dualspeed(c->cdev->gadget)) { 326 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
332 hs_subset_in_desc.bEndpointAddress = 327 hs_subset_out_desc.bEndpointAddress =
333 fs_subset_in_desc.bEndpointAddress; 328 fs_subset_out_desc.bEndpointAddress;
334 hs_subset_out_desc.bEndpointAddress =
335 fs_subset_out_desc.bEndpointAddress;
336
337 /* copy descriptors, and track endpoint copies */
338 f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
339 if (!f->hs_descriptors)
340 goto fail;
341 }
342 329
343 if (gadget_is_superspeed(c->cdev->gadget)) { 330 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
344 ss_subset_in_desc.bEndpointAddress = 331 ss_subset_out_desc.bEndpointAddress =
345 fs_subset_in_desc.bEndpointAddress; 332 fs_subset_out_desc.bEndpointAddress;
346 ss_subset_out_desc.bEndpointAddress =
347 fs_subset_out_desc.bEndpointAddress;
348 333
349 /* copy descriptors, and track endpoint copies */ 334 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
350 f->ss_descriptors = usb_copy_descriptors(ss_eth_function); 335 ss_eth_function);
351 if (!f->ss_descriptors) 336 if (status)
352 goto fail; 337 goto fail;
353 }
354 338
355 /* NOTE: all that is done without knowing or caring about 339 /* NOTE: all that is done without knowing or caring about
356 * the network link ... which is unavailable to this code 340 * the network link ... which is unavailable to this code
@@ -364,15 +348,11 @@ geth_bind(struct usb_configuration *c, struct usb_function *f)
364 return 0; 348 return 0;
365 349
366fail: 350fail:
367 if (f->descriptors) 351 usb_free_all_descriptors(f);
368 usb_free_descriptors(f->descriptors);
369 if (f->hs_descriptors)
370 usb_free_descriptors(f->hs_descriptors);
371
372 /* we might as well release our claims on endpoints */ 352 /* we might as well release our claims on endpoints */
373 if (geth->port.out_ep->desc) 353 if (geth->port.out_ep)
374 geth->port.out_ep->driver_data = NULL; 354 geth->port.out_ep->driver_data = NULL;
375 if (geth->port.in_ep->desc) 355 if (geth->port.in_ep)
376 geth->port.in_ep->driver_data = NULL; 356 geth->port.in_ep->driver_data = NULL;
377 357
378 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 358 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
@@ -383,12 +363,8 @@ fail:
383static void 363static void
384geth_unbind(struct usb_configuration *c, struct usb_function *f) 364geth_unbind(struct usb_configuration *c, struct usb_function *f)
385{ 365{
386 if (gadget_is_superspeed(c->cdev->gadget)) 366 geth_string_defs[0].id = 0;
387 usb_free_descriptors(f->ss_descriptors); 367 usb_free_all_descriptors(f);
388 if (gadget_is_dualspeed(c->cdev->gadget))
389 usb_free_descriptors(f->hs_descriptors);
390 usb_free_descriptors(f->descriptors);
391 geth_string_defs[1].s = NULL;
392 kfree(func_to_geth(f)); 368 kfree(func_to_geth(f));
393} 369}
394 370
@@ -414,20 +390,11 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
414 390
415 /* maybe allocate device-global string IDs */ 391 /* maybe allocate device-global string IDs */
416 if (geth_string_defs[0].id == 0) { 392 if (geth_string_defs[0].id == 0) {
417 393 status = usb_string_ids_tab(c->cdev, geth_string_defs);
418 /* interface label */
419 status = usb_string_id(c->cdev);
420 if (status < 0) 394 if (status < 0)
421 return status; 395 return status;
422 geth_string_defs[0].id = status; 396 subset_data_intf.iInterface = geth_string_defs[0].id;
423 subset_data_intf.iInterface = status; 397 ether_desc.iMACAddress = geth_string_defs[1].id;
424
425 /* MAC address */
426 status = usb_string_id(c->cdev);
427 if (status < 0)
428 return status;
429 geth_string_defs[1].id = status;
430 ether_desc.iMACAddress = status;
431 } 398 }
432 399
433 /* allocate and initialize one new instance */ 400 /* allocate and initialize one new instance */
@@ -449,9 +416,7 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
449 geth->port.func.disable = geth_disable; 416 geth->port.func.disable = geth_disable;
450 417
451 status = usb_add_function(c, &geth->port.func); 418 status = usb_add_function(c, &geth->port.func);
452 if (status) { 419 if (status)
453 geth_string_defs[1].s = NULL;
454 kfree(geth); 420 kfree(geth);
455 }
456 return status; 421 return status;
457} 422}
diff --git a/drivers/usb/gadget/f_uac1.c b/drivers/usb/gadget/f_uac1.c
index 1a5dcd5565e3..f570e667a640 100644
--- a/drivers/usb/gadget/f_uac1.c
+++ b/drivers/usb/gadget/f_uac1.c
@@ -630,7 +630,7 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
630 struct usb_composite_dev *cdev = c->cdev; 630 struct usb_composite_dev *cdev = c->cdev;
631 struct f_audio *audio = func_to_audio(f); 631 struct f_audio *audio = func_to_audio(f);
632 int status; 632 int status;
633 struct usb_ep *ep; 633 struct usb_ep *ep = NULL;
634 634
635 f_audio_build_desc(audio); 635 f_audio_build_desc(audio);
636 636
@@ -659,22 +659,14 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
659 status = -ENOMEM; 659 status = -ENOMEM;
660 660
661 /* copy descriptors, and track endpoint copies */ 661 /* copy descriptors, and track endpoint copies */
662 f->descriptors = usb_copy_descriptors(f_audio_desc); 662 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL);
663 663 if (status)
664 /* 664 goto fail;
665 * support all relevant hardware speeds... we expect that when
666 * hardware is dual speed, all bulk-capable endpoints work at
667 * both speeds
668 */
669 if (gadget_is_dualspeed(c->cdev->gadget)) {
670 c->highspeed = true;
671 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
672 }
673
674 return 0; 665 return 0;
675 666
676fail: 667fail:
677 668 if (ep)
669 ep->driver_data = NULL;
678 return status; 670 return status;
679} 671}
680 672
@@ -683,8 +675,7 @@ f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
683{ 675{
684 struct f_audio *audio = func_to_audio(f); 676 struct f_audio *audio = func_to_audio(f);
685 677
686 usb_free_descriptors(f->descriptors); 678 usb_free_all_descriptors(f);
687 usb_free_descriptors(f->hs_descriptors);
688 kfree(audio); 679 kfree(audio);
689} 680}
690 681
diff --git a/drivers/usb/gadget/f_uac2.c b/drivers/usb/gadget/f_uac2.c
index d3c6cffccb72..d7da258fa3f6 100644
--- a/drivers/usb/gadget/f_uac2.c
+++ b/drivers/usb/gadget/f_uac2.c
@@ -50,13 +50,6 @@ static int c_ssize = 2;
50module_param(c_ssize, uint, S_IRUGO); 50module_param(c_ssize, uint, S_IRUGO);
51MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)"); 51MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
52 52
53#define DMA_ADDR_INVALID (~(dma_addr_t)0)
54
55#define ALT_SET(x, a) do {(x) &= ~0xff; (x) |= (a); } while (0)
56#define ALT_GET(x) ((x) & 0xff)
57#define INTF_SET(x, i) do {(x) &= 0xff; (x) |= ((i) << 8); } while (0)
58#define INTF_GET(x) ((x >> 8) & 0xff)
59
60/* Keep everyone on toes */ 53/* Keep everyone on toes */
61#define USB_XFERS 2 54#define USB_XFERS 2
62 55
@@ -144,8 +137,9 @@ static struct snd_pcm_hardware uac2_pcm_hardware = {
144}; 137};
145 138
146struct audio_dev { 139struct audio_dev {
147 /* Currently active {Interface[15:8] | AltSettings[7:0]} */ 140 u8 ac_intf, ac_alt;
148 __u16 ac_alt, as_out_alt, as_in_alt; 141 u8 as_out_intf, as_out_alt;
142 u8 as_in_intf, as_in_alt;
149 143
150 struct usb_ep *in_ep, *out_ep; 144 struct usb_ep *in_ep, *out_ep;
151 struct usb_function func; 145 struct usb_function func;
@@ -408,7 +402,7 @@ static struct snd_pcm_ops uac2_pcm_ops = {
408 .prepare = uac2_pcm_null, 402 .prepare = uac2_pcm_null,
409}; 403};
410 404
411static int __devinit snd_uac2_probe(struct platform_device *pdev) 405static int snd_uac2_probe(struct platform_device *pdev)
412{ 406{
413 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev); 407 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
414 struct snd_card *card; 408 struct snd_card *card;
@@ -526,32 +520,22 @@ enum {
526 STR_AS_IN_ALT1, 520 STR_AS_IN_ALT1,
527}; 521};
528 522
529static const char ifassoc[] = "Source/Sink";
530static const char ifctrl[] = "Topology Control";
531static char clksrc_in[8]; 523static char clksrc_in[8];
532static char clksrc_out[8]; 524static char clksrc_out[8];
533static const char usb_it[] = "USBH Out";
534static const char io_it[] = "USBD Out";
535static const char usb_ot[] = "USBH In";
536static const char io_ot[] = "USBD In";
537static const char out_alt0[] = "Playback Inactive";
538static const char out_alt1[] = "Playback Active";
539static const char in_alt0[] = "Capture Inactive";
540static const char in_alt1[] = "Capture Active";
541 525
542static struct usb_string strings_fn[] = { 526static struct usb_string strings_fn[] = {
543 [STR_ASSOC].s = ifassoc, 527 [STR_ASSOC].s = "Source/Sink",
544 [STR_IF_CTRL].s = ifctrl, 528 [STR_IF_CTRL].s = "Topology Control",
545 [STR_CLKSRC_IN].s = clksrc_in, 529 [STR_CLKSRC_IN].s = clksrc_in,
546 [STR_CLKSRC_OUT].s = clksrc_out, 530 [STR_CLKSRC_OUT].s = clksrc_out,
547 [STR_USB_IT].s = usb_it, 531 [STR_USB_IT].s = "USBH Out",
548 [STR_IO_IT].s = io_it, 532 [STR_IO_IT].s = "USBD Out",
549 [STR_USB_OT].s = usb_ot, 533 [STR_USB_OT].s = "USBH In",
550 [STR_IO_OT].s = io_ot, 534 [STR_IO_OT].s = "USBD In",
551 [STR_AS_OUT_ALT0].s = out_alt0, 535 [STR_AS_OUT_ALT0].s = "Playback Inactive",
552 [STR_AS_OUT_ALT1].s = out_alt1, 536 [STR_AS_OUT_ALT1].s = "Playback Active",
553 [STR_AS_IN_ALT0].s = in_alt0, 537 [STR_AS_IN_ALT0].s = "Capture Inactive",
554 [STR_AS_IN_ALT1].s = in_alt1, 538 [STR_AS_IN_ALT1].s = "Capture Active",
555 { }, 539 { },
556}; 540};
557 541
@@ -952,8 +936,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
952 return ret; 936 return ret;
953 } 937 }
954 std_ac_if_desc.bInterfaceNumber = ret; 938 std_ac_if_desc.bInterfaceNumber = ret;
955 ALT_SET(agdev->ac_alt, 0); 939 agdev->ac_intf = ret;
956 INTF_SET(agdev->ac_alt, ret); 940 agdev->ac_alt = 0;
957 941
958 ret = usb_interface_id(cfg, fn); 942 ret = usb_interface_id(cfg, fn);
959 if (ret < 0) { 943 if (ret < 0) {
@@ -963,8 +947,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
963 } 947 }
964 std_as_out_if0_desc.bInterfaceNumber = ret; 948 std_as_out_if0_desc.bInterfaceNumber = ret;
965 std_as_out_if1_desc.bInterfaceNumber = ret; 949 std_as_out_if1_desc.bInterfaceNumber = ret;
966 ALT_SET(agdev->as_out_alt, 0); 950 agdev->as_out_intf = ret;
967 INTF_SET(agdev->as_out_alt, ret); 951 agdev->as_out_alt = 0;
968 952
969 ret = usb_interface_id(cfg, fn); 953 ret = usb_interface_id(cfg, fn);
970 if (ret < 0) { 954 if (ret < 0) {
@@ -974,19 +958,23 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
974 } 958 }
975 std_as_in_if0_desc.bInterfaceNumber = ret; 959 std_as_in_if0_desc.bInterfaceNumber = ret;
976 std_as_in_if1_desc.bInterfaceNumber = ret; 960 std_as_in_if1_desc.bInterfaceNumber = ret;
977 ALT_SET(agdev->as_in_alt, 0); 961 agdev->as_in_intf = ret;
978 INTF_SET(agdev->as_in_alt, ret); 962 agdev->as_in_alt = 0;
979 963
980 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); 964 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
981 if (!agdev->out_ep) 965 if (!agdev->out_ep) {
982 dev_err(&uac2->pdev.dev, 966 dev_err(&uac2->pdev.dev,
983 "%s:%d Error!\n", __func__, __LINE__); 967 "%s:%d Error!\n", __func__, __LINE__);
968 goto err;
969 }
984 agdev->out_ep->driver_data = agdev; 970 agdev->out_ep->driver_data = agdev;
985 971
986 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc); 972 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
987 if (!agdev->in_ep) 973 if (!agdev->in_ep) {
988 dev_err(&uac2->pdev.dev, 974 dev_err(&uac2->pdev.dev,
989 "%s:%d Error!\n", __func__, __LINE__); 975 "%s:%d Error!\n", __func__, __LINE__);
976 goto err;
977 }
990 agdev->in_ep->driver_data = agdev; 978 agdev->in_ep->driver_data = agdev;
991 979
992 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; 980 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
@@ -994,9 +982,9 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
994 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; 982 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
995 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize; 983 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
996 984
997 fn->descriptors = usb_copy_descriptors(fs_audio_desc); 985 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
998 if (gadget_is_dualspeed(gadget)) 986 if (ret)
999 fn->hs_descriptors = usb_copy_descriptors(hs_audio_desc); 987 goto err;
1000 988
1001 prm = &agdev->uac2.c_prm; 989 prm = &agdev->uac2.c_prm;
1002 prm->max_psize = hs_epout_desc.wMaxPacketSize; 990 prm->max_psize = hs_epout_desc.wMaxPacketSize;
@@ -1005,6 +993,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
1005 prm->max_psize = 0; 993 prm->max_psize = 0;
1006 dev_err(&uac2->pdev.dev, 994 dev_err(&uac2->pdev.dev,
1007 "%s:%d Error!\n", __func__, __LINE__); 995 "%s:%d Error!\n", __func__, __LINE__);
996 goto err;
1008 } 997 }
1009 998
1010 prm = &agdev->uac2.p_prm; 999 prm = &agdev->uac2.p_prm;
@@ -1014,17 +1003,28 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
1014 prm->max_psize = 0; 1003 prm->max_psize = 0;
1015 dev_err(&uac2->pdev.dev, 1004 dev_err(&uac2->pdev.dev,
1016 "%s:%d Error!\n", __func__, __LINE__); 1005 "%s:%d Error!\n", __func__, __LINE__);
1006 goto err;
1017 } 1007 }
1018 1008
1019 return alsa_uac2_init(agdev); 1009 ret = alsa_uac2_init(agdev);
1010 if (ret)
1011 goto err;
1012 return 0;
1013err:
1014 kfree(agdev->uac2.p_prm.rbuf);
1015 kfree(agdev->uac2.c_prm.rbuf);
1016 usb_free_all_descriptors(fn);
1017 if (agdev->in_ep)
1018 agdev->in_ep->driver_data = NULL;
1019 if (agdev->out_ep)
1020 agdev->out_ep->driver_data = NULL;
1021 return -EINVAL;
1020} 1022}
1021 1023
1022static void 1024static void
1023afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn) 1025afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
1024{ 1026{
1025 struct audio_dev *agdev = func_to_agdev(fn); 1027 struct audio_dev *agdev = func_to_agdev(fn);
1026 struct usb_composite_dev *cdev = cfg->cdev;
1027 struct usb_gadget *gadget = cdev->gadget;
1028 struct uac2_rtd_params *prm; 1028 struct uac2_rtd_params *prm;
1029 1029
1030 alsa_uac2_exit(agdev); 1030 alsa_uac2_exit(agdev);
@@ -1034,10 +1034,7 @@ afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
1034 1034
1035 prm = &agdev->uac2.c_prm; 1035 prm = &agdev->uac2.c_prm;
1036 kfree(prm->rbuf); 1036 kfree(prm->rbuf);
1037 1037 usb_free_all_descriptors(fn);
1038 if (gadget_is_dualspeed(gadget))
1039 usb_free_descriptors(fn->hs_descriptors);
1040 usb_free_descriptors(fn->descriptors);
1041 1038
1042 if (agdev->in_ep) 1039 if (agdev->in_ep)
1043 agdev->in_ep->driver_data = NULL; 1040 agdev->in_ep->driver_data = NULL;
@@ -1064,7 +1061,7 @@ afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1064 return -EINVAL; 1061 return -EINVAL;
1065 } 1062 }
1066 1063
1067 if (intf == INTF_GET(agdev->ac_alt)) { 1064 if (intf == agdev->ac_intf) {
1068 /* Control I/f has only 1 AltSetting - 0 */ 1065 /* Control I/f has only 1 AltSetting - 0 */
1069 if (alt) { 1066 if (alt) {
1070 dev_err(&uac2->pdev.dev, 1067 dev_err(&uac2->pdev.dev,
@@ -1074,16 +1071,16 @@ afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1074 return 0; 1071 return 0;
1075 } 1072 }
1076 1073
1077 if (intf == INTF_GET(agdev->as_out_alt)) { 1074 if (intf == agdev->as_out_intf) {
1078 ep = agdev->out_ep; 1075 ep = agdev->out_ep;
1079 prm = &uac2->c_prm; 1076 prm = &uac2->c_prm;
1080 config_ep_by_speed(gadget, fn, ep); 1077 config_ep_by_speed(gadget, fn, ep);
1081 ALT_SET(agdev->as_out_alt, alt); 1078 agdev->as_out_alt = alt;
1082 } else if (intf == INTF_GET(agdev->as_in_alt)) { 1079 } else if (intf == agdev->as_in_intf) {
1083 ep = agdev->in_ep; 1080 ep = agdev->in_ep;
1084 prm = &uac2->p_prm; 1081 prm = &uac2->p_prm;
1085 config_ep_by_speed(gadget, fn, ep); 1082 config_ep_by_speed(gadget, fn, ep);
1086 ALT_SET(agdev->as_in_alt, alt); 1083 agdev->as_in_alt = alt;
1087 } else { 1084 } else {
1088 dev_err(&uac2->pdev.dev, 1085 dev_err(&uac2->pdev.dev,
1089 "%s:%d Error!\n", __func__, __LINE__); 1086 "%s:%d Error!\n", __func__, __LINE__);
@@ -1117,7 +1114,6 @@ afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1117 prm->ureq[i].pp = prm; 1114 prm->ureq[i].pp = prm;
1118 1115
1119 req->zero = 0; 1116 req->zero = 0;
1120 req->dma = DMA_ADDR_INVALID;
1121 req->context = &prm->ureq[i]; 1117 req->context = &prm->ureq[i];
1122 req->length = prm->max_psize; 1118 req->length = prm->max_psize;
1123 req->complete = agdev_iso_complete; 1119 req->complete = agdev_iso_complete;
@@ -1136,12 +1132,12 @@ afunc_get_alt(struct usb_function *fn, unsigned intf)
1136 struct audio_dev *agdev = func_to_agdev(fn); 1132 struct audio_dev *agdev = func_to_agdev(fn);
1137 struct snd_uac2_chip *uac2 = &agdev->uac2; 1133 struct snd_uac2_chip *uac2 = &agdev->uac2;
1138 1134
1139 if (intf == INTF_GET(agdev->ac_alt)) 1135 if (intf == agdev->ac_intf)
1140 return ALT_GET(agdev->ac_alt); 1136 return agdev->ac_alt;
1141 else if (intf == INTF_GET(agdev->as_out_alt)) 1137 else if (intf == agdev->as_out_intf)
1142 return ALT_GET(agdev->as_out_alt); 1138 return agdev->as_out_alt;
1143 else if (intf == INTF_GET(agdev->as_in_alt)) 1139 else if (intf == agdev->as_in_intf)
1144 return ALT_GET(agdev->as_in_alt); 1140 return agdev->as_in_alt;
1145 else 1141 else
1146 dev_err(&uac2->pdev.dev, 1142 dev_err(&uac2->pdev.dev,
1147 "%s:%d Invalid Interface %d!\n", 1143 "%s:%d Invalid Interface %d!\n",
@@ -1157,10 +1153,10 @@ afunc_disable(struct usb_function *fn)
1157 struct snd_uac2_chip *uac2 = &agdev->uac2; 1153 struct snd_uac2_chip *uac2 = &agdev->uac2;
1158 1154
1159 free_ep(&uac2->p_prm, agdev->in_ep); 1155 free_ep(&uac2->p_prm, agdev->in_ep);
1160 ALT_SET(agdev->as_in_alt, 0); 1156 agdev->as_in_alt = 0;
1161 1157
1162 free_ep(&uac2->c_prm, agdev->out_ep); 1158 free_ep(&uac2->c_prm, agdev->out_ep);
1163 ALT_SET(agdev->as_out_alt, 0); 1159 agdev->as_out_alt = 0;
1164} 1160}
1165 1161
1166static int 1162static int
@@ -1267,7 +1263,7 @@ setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1267 u16 w_index = le16_to_cpu(cr->wIndex); 1263 u16 w_index = le16_to_cpu(cr->wIndex);
1268 u8 intf = w_index & 0xff; 1264 u8 intf = w_index & 0xff;
1269 1265
1270 if (intf != INTF_GET(agdev->ac_alt)) { 1266 if (intf != agdev->ac_intf) {
1271 dev_err(&uac2->pdev.dev, 1267 dev_err(&uac2->pdev.dev,
1272 "%s:%d Error!\n", __func__, __LINE__); 1268 "%s:%d Error!\n", __func__, __LINE__);
1273 return -EOPNOTSUPP; 1269 return -EOPNOTSUPP;
@@ -1316,7 +1312,7 @@ afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1316 1312
1317static int audio_bind_config(struct usb_configuration *cfg) 1313static int audio_bind_config(struct usb_configuration *cfg)
1318{ 1314{
1319 int id, res; 1315 int res;
1320 1316
1321 agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL); 1317 agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
1322 if (agdev_g == NULL) { 1318 if (agdev_g == NULL) {
@@ -1324,89 +1320,21 @@ static int audio_bind_config(struct usb_configuration *cfg)
1324 return -ENOMEM; 1320 return -ENOMEM;
1325 } 1321 }
1326 1322
1327 id = usb_string_id(cfg->cdev); 1323 res = usb_string_ids_tab(cfg->cdev, strings_fn);
1328 if (id < 0) 1324 if (res)
1329 return id; 1325 return res;
1330 1326 iad_desc.iFunction = strings_fn[STR_ASSOC].id;
1331 strings_fn[STR_ASSOC].id = id; 1327 std_ac_if_desc.iInterface = strings_fn[STR_IF_CTRL].id;
1332 iad_desc.iFunction = id, 1328 in_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_IN].id;
1333 1329 out_clk_src_desc.iClockSource = strings_fn[STR_CLKSRC_OUT].id;
1334 id = usb_string_id(cfg->cdev); 1330 usb_out_it_desc.iTerminal = strings_fn[STR_USB_IT].id;
1335 if (id < 0) 1331 io_in_it_desc.iTerminal = strings_fn[STR_IO_IT].id;
1336 return id; 1332 usb_in_ot_desc.iTerminal = strings_fn[STR_USB_OT].id;
1337 1333 io_out_ot_desc.iTerminal = strings_fn[STR_IO_OT].id;
1338 strings_fn[STR_IF_CTRL].id = id; 1334 std_as_out_if0_desc.iInterface = strings_fn[STR_AS_OUT_ALT0].id;
1339 std_ac_if_desc.iInterface = id, 1335 std_as_out_if1_desc.iInterface = strings_fn[STR_AS_OUT_ALT1].id;
1340 1336 std_as_in_if0_desc.iInterface = strings_fn[STR_AS_IN_ALT0].id;
1341 id = usb_string_id(cfg->cdev); 1337 std_as_in_if1_desc.iInterface = strings_fn[STR_AS_IN_ALT1].id;
1342 if (id < 0)
1343 return id;
1344
1345 strings_fn[STR_CLKSRC_IN].id = id;
1346 in_clk_src_desc.iClockSource = id,
1347
1348 id = usb_string_id(cfg->cdev);
1349 if (id < 0)
1350 return id;
1351
1352 strings_fn[STR_CLKSRC_OUT].id = id;
1353 out_clk_src_desc.iClockSource = id,
1354
1355 id = usb_string_id(cfg->cdev);
1356 if (id < 0)
1357 return id;
1358
1359 strings_fn[STR_USB_IT].id = id;
1360 usb_out_it_desc.iTerminal = id,
1361
1362 id = usb_string_id(cfg->cdev);
1363 if (id < 0)
1364 return id;
1365
1366 strings_fn[STR_IO_IT].id = id;
1367 io_in_it_desc.iTerminal = id;
1368
1369 id = usb_string_id(cfg->cdev);
1370 if (id < 0)
1371 return id;
1372
1373 strings_fn[STR_USB_OT].id = id;
1374 usb_in_ot_desc.iTerminal = id;
1375
1376 id = usb_string_id(cfg->cdev);
1377 if (id < 0)
1378 return id;
1379
1380 strings_fn[STR_IO_OT].id = id;
1381 io_out_ot_desc.iTerminal = id;
1382
1383 id = usb_string_id(cfg->cdev);
1384 if (id < 0)
1385 return id;
1386
1387 strings_fn[STR_AS_OUT_ALT0].id = id;
1388 std_as_out_if0_desc.iInterface = id;
1389
1390 id = usb_string_id(cfg->cdev);
1391 if (id < 0)
1392 return id;
1393
1394 strings_fn[STR_AS_OUT_ALT1].id = id;
1395 std_as_out_if1_desc.iInterface = id;
1396
1397 id = usb_string_id(cfg->cdev);
1398 if (id < 0)
1399 return id;
1400
1401 strings_fn[STR_AS_IN_ALT0].id = id;
1402 std_as_in_if0_desc.iInterface = id;
1403
1404 id = usb_string_id(cfg->cdev);
1405 if (id < 0)
1406 return id;
1407
1408 strings_fn[STR_AS_IN_ALT1].id = id;
1409 std_as_in_if1_desc.iInterface = id;
1410 1338
1411 agdev_g->func.name = "uac2_func"; 1339 agdev_g->func.name = "uac2_func";
1412 agdev_g->func.strings = fn_strings; 1340 agdev_g->func.strings = fn_strings;
diff --git a/drivers/usb/gadget/f_uvc.c b/drivers/usb/gadget/f_uvc.c
index 2a8bf0655c60..5b629876941b 100644
--- a/drivers/usb/gadget/f_uvc.c
+++ b/drivers/usb/gadget/f_uvc.c
@@ -417,7 +417,6 @@ uvc_register_video(struct uvc_device *uvc)
417 return -ENOMEM; 417 return -ENOMEM;
418 418
419 video->parent = &cdev->gadget->dev; 419 video->parent = &cdev->gadget->dev;
420 video->minor = -1;
421 video->fops = &uvc_v4l2_fops; 420 video->fops = &uvc_v4l2_fops;
422 video->release = video_device_release; 421 video->release = video_device_release;
423 strncpy(video->name, cdev->gadget->name, sizeof(video->name)); 422 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
@@ -577,27 +576,15 @@ uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
577 576
578 INFO(cdev, "uvc_function_unbind\n"); 577 INFO(cdev, "uvc_function_unbind\n");
579 578
580 if (uvc->vdev) { 579 video_unregister_device(uvc->vdev);
581 if (uvc->vdev->minor == -1) 580 uvc->control_ep->driver_data = NULL;
582 video_device_release(uvc->vdev); 581 uvc->video.ep->driver_data = NULL;
583 else
584 video_unregister_device(uvc->vdev);
585 uvc->vdev = NULL;
586 }
587 582
588 if (uvc->control_ep) 583 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
589 uvc->control_ep->driver_data = NULL; 584 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
590 if (uvc->video.ep) 585 kfree(uvc->control_buf);
591 uvc->video.ep->driver_data = NULL;
592
593 if (uvc->control_req) {
594 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
595 kfree(uvc->control_buf);
596 }
597 586
598 kfree(f->descriptors); 587 usb_free_all_descriptors(f);
599 kfree(f->hs_descriptors);
600 kfree(f->ss_descriptors);
601 588
602 kfree(uvc); 589 kfree(uvc);
603} 590}
@@ -663,49 +650,40 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
663 /* sanity check the streaming endpoint module parameters */ 650 /* sanity check the streaming endpoint module parameters */
664 if (streaming_maxpacket > 1024) 651 if (streaming_maxpacket > 1024)
665 streaming_maxpacket = 1024; 652 streaming_maxpacket = 1024;
653 /*
654 * Fill in the HS descriptors from the module parameters for the Video
655 * Streaming endpoint.
656 * NOTE: We assume that the user knows what they are doing and won't
657 * give parameters that their UDC doesn't support.
658 */
659 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
660 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
661 uvc_hs_streaming_ep.bInterval = streaming_interval;
662 uvc_hs_streaming_ep.bEndpointAddress =
663 uvc_fs_streaming_ep.bEndpointAddress;
666 664
667 /* Copy descriptors for FS. */ 665 /*
668 f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 666 * Fill in the SS descriptors from the module parameters for the Video
669 667 * Streaming endpoint.
670 /* support high speed hardware */ 668 * NOTE: We assume that the user knows what they are doing and won't
671 if (gadget_is_dualspeed(cdev->gadget)) { 669 * give parameters that their UDC doesn't support.
672 /* 670 */
673 * Fill in the HS descriptors from the module parameters for the 671 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
674 * Video Streaming endpoint. 672 uvc_ss_streaming_ep.bInterval = streaming_interval;
675 * NOTE: We assume that the user knows what they are doing and 673 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
676 * won't give parameters that their UDC doesn't support. 674 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
677 */ 675 uvc_ss_streaming_comp.wBytesPerInterval =
678 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket; 676 streaming_maxpacket * (streaming_mult + 1) *
679 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11; 677 (streaming_maxburst + 1);
680 uvc_hs_streaming_ep.bInterval = streaming_interval; 678 uvc_ss_streaming_ep.bEndpointAddress =
681 uvc_hs_streaming_ep.bEndpointAddress = 679 uvc_fs_streaming_ep.bEndpointAddress;
682 uvc_fs_streaming_ep.bEndpointAddress;
683
684 /* Copy descriptors. */
685 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
686 }
687 680
688 /* support super speed hardware */ 681 /* Copy descriptors */
689 if (gadget_is_superspeed(c->cdev->gadget)) { 682 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
690 /* 683 if (gadget_is_dualspeed(cdev->gadget))
691 * Fill in the SS descriptors from the module parameters for the 684 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
692 * Video Streaming endpoint. 685 if (gadget_is_superspeed(c->cdev->gadget))
693 * NOTE: We assume that the user knows what they are doing and
694 * won't give parameters that their UDC doesn't support.
695 */
696 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
697 uvc_ss_streaming_ep.bInterval = streaming_interval;
698 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
699 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
700 uvc_ss_streaming_comp.wBytesPerInterval =
701 streaming_maxpacket * (streaming_mult + 1) *
702 (streaming_maxburst + 1);
703 uvc_ss_streaming_ep.bEndpointAddress =
704 uvc_fs_streaming_ep.bEndpointAddress;
705
706 /* Copy descriptors. */
707 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER); 686 f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
708 }
709 687
710 /* Preallocate control endpoint request. */ 688 /* Preallocate control endpoint request. */
711 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); 689 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
@@ -740,7 +718,20 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
740 return 0; 718 return 0;
741 719
742error: 720error:
743 uvc_function_unbind(c, f); 721 if (uvc->vdev)
722 video_device_release(uvc->vdev);
723
724 if (uvc->control_ep)
725 uvc->control_ep->driver_data = NULL;
726 if (uvc->video.ep)
727 uvc->video.ep->driver_data = NULL;
728
729 if (uvc->control_req) {
730 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
731 kfree(uvc->control_buf);
732 }
733
734 usb_free_all_descriptors(f);
744 return ret; 735 return ret;
745} 736}
746 737
@@ -808,25 +799,16 @@ uvc_bind_config(struct usb_configuration *c,
808 uvc->desc.hs_streaming = hs_streaming; 799 uvc->desc.hs_streaming = hs_streaming;
809 uvc->desc.ss_streaming = ss_streaming; 800 uvc->desc.ss_streaming = ss_streaming;
810 801
811 /* maybe allocate device-global string IDs, and patch descriptors */ 802 /* Allocate string descriptor numbers. */
812 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) { 803 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
813 /* Allocate string descriptor numbers. */ 804 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
814 ret = usb_string_id(c->cdev); 805 if (ret)
815 if (ret < 0)
816 goto error;
817 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
818 uvc_iad.iFunction = ret;
819
820 ret = usb_string_id(c->cdev);
821 if (ret < 0)
822 goto error;
823 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
824 uvc_control_intf.iInterface = ret;
825
826 ret = usb_string_id(c->cdev);
827 if (ret < 0)
828 goto error; 806 goto error;
829 uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret; 807 uvc_iad.iFunction =
808 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
809 uvc_control_intf.iInterface =
810 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
811 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
830 uvc_streaming_intf_alt0.iInterface = ret; 812 uvc_streaming_intf_alt0.iInterface = ret;
831 uvc_streaming_intf_alt1.iInterface = ret; 813 uvc_streaming_intf_alt1.iInterface = ret;
832 } 814 }
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
deleted file mode 100644
index 3f7d640b6758..000000000000
--- a/drivers/usb/gadget/file_storage.c
+++ /dev/null
@@ -1,3656 +0,0 @@
1/*
2 * file_storage.c -- File-backed USB Storage Gadget, for USB development
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation, either version 2 of that License or (at your option) any
23 * later version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39/*
40 * The File-backed Storage Gadget acts as a USB Mass Storage device,
41 * appearing to the host as a disk drive or as a CD-ROM drive. In addition
42 * to providing an example of a genuinely useful gadget driver for a USB
43 * device, it also illustrates a technique of double-buffering for increased
44 * throughput. Last but not least, it gives an easy way to probe the
45 * behavior of the Mass Storage drivers in a USB host.
46 *
47 * Backing storage is provided by a regular file or a block device, specified
48 * by the "file" module parameter. Access can be limited to read-only by
49 * setting the optional "ro" module parameter. (For CD-ROM emulation,
50 * access is always read-only.) The gadget will indicate that it has
51 * removable media if the optional "removable" module parameter is set.
52 *
53 * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
54 * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
55 * by the optional "transport" module parameter. It also supports the
56 * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
57 * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
58 * the optional "protocol" module parameter. In addition, the default
59 * Vendor ID, Product ID, release number and serial number can be overridden.
60 *
61 * There is support for multiple logical units (LUNs), each of which has
62 * its own backing file. The number of LUNs can be set using the optional
63 * "luns" module parameter (anywhere from 1 to 8), and the corresponding
64 * files are specified using comma-separated lists for "file" and "ro".
65 * The default number of LUNs is taken from the number of "file" elements;
66 * it is 1 if "file" is not given. If "removable" is not set then a backing
67 * file must be specified for each LUN. If it is set, then an unspecified
68 * or empty backing filename means the LUN's medium is not loaded. Ideally
69 * each LUN would be settable independently as a disk drive or a CD-ROM
70 * drive, but currently all LUNs have to be the same type. The CD-ROM
71 * emulation includes a single data track and no audio tracks; hence there
72 * need be only one backing file per LUN.
73 *
74 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
75 * needed (an interrupt-out endpoint is also needed for CBI). The memory
76 * requirement amounts to two 16K buffers, size configurable by a parameter.
77 * Support is included for both full-speed and high-speed operation.
78 *
79 * Note that the driver is slightly non-portable in that it assumes a
80 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
81 * interrupt-in endpoints. With most device controllers this isn't an
82 * issue, but there may be some with hardware restrictions that prevent
83 * a buffer from being used by more than one endpoint.
84 *
85 * Module options:
86 *
87 * file=filename[,filename...]
88 * Required if "removable" is not set, names of
89 * the files or block devices used for
90 * backing storage
91 * serial=HHHH... Required serial number (string of hex chars)
92 * ro=b[,b...] Default false, booleans for read-only access
93 * removable Default false, boolean for removable media
94 * luns=N Default N = number of filenames, number of
95 * LUNs to support
96 * nofua=b[,b...] Default false, booleans for ignore FUA flag
97 * in SCSI WRITE(10,12) commands
98 * stall Default determined according to the type of
99 * USB device controller (usually true),
100 * boolean to permit the driver to halt
101 * bulk endpoints
102 * cdrom Default false, boolean for whether to emulate
103 * a CD-ROM drive
104 * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
105 * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
106 * ATAPI, QIC, UFI, 8070, or SCSI;
107 * also 1 - 6)
108 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
109 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
110 * release=0xRRRR Override the USB release number (bcdDevice)
111 * buflen=N Default N=16384, buffer size used (will be
112 * rounded down to a multiple of
113 * PAGE_CACHE_SIZE)
114 *
115 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
116 * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
117 * default values are used for everything else.
118 *
119 * The pathnames of the backing files and the ro settings are available in
120 * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
121 * the gadget's sysfs directory. If the "removable" option is set, writing to
122 * these files will simulate ejecting/loading the medium (writing an empty
123 * line means eject) and adjusting a write-enable tab. Changes to the ro
124 * setting are not allowed when the medium is loaded or if CD-ROM emulation
125 * is being used.
126 *
127 * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
128 * The driver's SCSI command interface was based on the "Information
129 * technology - Small Computer System Interface - 2" document from
130 * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
131 * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
132 * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
133 * "Universal Serial Bus Mass Storage Class UFI Command Specification"
134 * document, Revision 1.0, December 14, 1998, available at
135 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
136 */
137
138
139/*
140 * Driver Design
141 *
142 * The FSG driver is fairly straightforward. There is a main kernel
143 * thread that handles most of the work. Interrupt routines field
144 * callbacks from the controller driver: bulk- and interrupt-request
145 * completion notifications, endpoint-0 events, and disconnect events.
146 * Completion events are passed to the main thread by wakeup calls. Many
147 * ep0 requests are handled at interrupt time, but SetInterface,
148 * SetConfiguration, and device reset requests are forwarded to the
149 * thread in the form of "exceptions" using SIGUSR1 signals (since they
150 * should interrupt any ongoing file I/O operations).
151 *
152 * The thread's main routine implements the standard command/data/status
153 * parts of a SCSI interaction. It and its subroutines are full of tests
154 * for pending signals/exceptions -- all this polling is necessary since
155 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
156 * indication that the driver really wants to be running in userspace.)
157 * An important point is that so long as the thread is alive it keeps an
158 * open reference to the backing file. This will prevent unmounting
159 * the backing file's underlying filesystem and could cause problems
160 * during system shutdown, for example. To prevent such problems, the
161 * thread catches INT, TERM, and KILL signals and converts them into
162 * an EXIT exception.
163 *
164 * In normal operation the main thread is started during the gadget's
165 * fsg_bind() callback and stopped during fsg_unbind(). But it can also
166 * exit when it receives a signal, and there's no point leaving the
167 * gadget running when the thread is dead. So just before the thread
168 * exits, it deregisters the gadget driver. This makes things a little
169 * tricky: The driver is deregistered at two places, and the exiting
170 * thread can indirectly call fsg_unbind() which in turn can tell the
171 * thread to exit. The first problem is resolved through the use of the
172 * REGISTERED atomic bitflag; the driver will only be deregistered once.
173 * The second problem is resolved by having fsg_unbind() check
174 * fsg->state; it won't try to stop the thread if the state is already
175 * FSG_STATE_TERMINATED.
176 *
177 * To provide maximum throughput, the driver uses a circular pipeline of
178 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
179 * arbitrarily long; in practice the benefits don't justify having more
180 * than 2 stages (i.e., double buffering). But it helps to think of the
181 * pipeline as being a long one. Each buffer head contains a bulk-in and
182 * a bulk-out request pointer (since the buffer can be used for both
183 * output and input -- directions always are given from the host's
184 * point of view) as well as a pointer to the buffer and various state
185 * variables.
186 *
187 * Use of the pipeline follows a simple protocol. There is a variable
188 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
189 * At any time that buffer head may still be in use from an earlier
190 * request, so each buffer head has a state variable indicating whether
191 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
192 * buffer head to be EMPTY, filling the buffer either by file I/O or by
193 * USB I/O (during which the buffer head is BUSY), and marking the buffer
194 * head FULL when the I/O is complete. Then the buffer will be emptied
195 * (again possibly by USB I/O, during which it is marked BUSY) and
196 * finally marked EMPTY again (possibly by a completion routine).
197 *
198 * A module parameter tells the driver to avoid stalling the bulk
199 * endpoints wherever the transport specification allows. This is
200 * necessary for some UDCs like the SuperH, which cannot reliably clear a
201 * halt on a bulk endpoint. However, under certain circumstances the
202 * Bulk-only specification requires a stall. In such cases the driver
203 * will halt the endpoint and set a flag indicating that it should clear
204 * the halt in software during the next device reset. Hopefully this
205 * will permit everything to work correctly. Furthermore, although the
206 * specification allows the bulk-out endpoint to halt when the host sends
207 * too much data, implementing this would cause an unavoidable race.
208 * The driver will always use the "no-stall" approach for OUT transfers.
209 *
210 * One subtle point concerns sending status-stage responses for ep0
211 * requests. Some of these requests, such as device reset, can involve
212 * interrupting an ongoing file I/O operation, which might take an
213 * arbitrarily long time. During that delay the host might give up on
214 * the original ep0 request and issue a new one. When that happens the
215 * driver should not notify the host about completion of the original
216 * request, as the host will no longer be waiting for it. So the driver
217 * assigns to each ep0 request a unique tag, and it keeps track of the
218 * tag value of the request associated with a long-running exception
219 * (device-reset, interface-change, or configuration-change). When the
220 * exception handler is finished, the status-stage response is submitted
221 * only if the current ep0 request tag is equal to the exception request
222 * tag. Thus only the most recently received ep0 request will get a
223 * status-stage response.
224 *
225 * Warning: This driver source file is too long. It ought to be split up
226 * into a header file plus about 3 separate .c files, to handle the details
227 * of the Gadget, USB Mass Storage, and SCSI protocols.
228 */
229
230
231/* #define VERBOSE_DEBUG */
232/* #define DUMP_MSGS */
233
234
235#include <linux/blkdev.h>
236#include <linux/completion.h>
237#include <linux/dcache.h>
238#include <linux/delay.h>
239#include <linux/device.h>
240#include <linux/fcntl.h>
241#include <linux/file.h>
242#include <linux/fs.h>
243#include <linux/kref.h>
244#include <linux/kthread.h>
245#include <linux/limits.h>
246#include <linux/module.h>
247#include <linux/rwsem.h>
248#include <linux/slab.h>
249#include <linux/spinlock.h>
250#include <linux/string.h>
251#include <linux/freezer.h>
252#include <linux/utsname.h>
253
254#include <linux/usb/composite.h>
255#include <linux/usb/ch9.h>
256#include <linux/usb/gadget.h>
257
258#include "gadget_chips.h"
259
260#define DRIVER_DESC "File-backed Storage Gadget"
261#define DRIVER_NAME "g_file_storage"
262#define DRIVER_VERSION "1 September 2010"
263
264static char fsg_string_manufacturer[64];
265static const char fsg_string_product[] = DRIVER_DESC;
266static const char fsg_string_config[] = "Self-powered";
267static const char fsg_string_interface[] = "Mass Storage";
268
269
270#include "storage_common.c"
271
272
273MODULE_DESCRIPTION(DRIVER_DESC);
274MODULE_AUTHOR("Alan Stern");
275MODULE_LICENSE("Dual BSD/GPL");
276
277/*
278 * This driver assumes self-powered hardware and has no way for users to
279 * trigger remote wakeup. It uses autoconfiguration to select endpoints
280 * and endpoint addresses.
281 */
282
283
284/*-------------------------------------------------------------------------*/
285
286
287/* Encapsulate the module parameter settings */
288
289static struct {
290 char *file[FSG_MAX_LUNS];
291 char *serial;
292 bool ro[FSG_MAX_LUNS];
293 bool nofua[FSG_MAX_LUNS];
294 unsigned int num_filenames;
295 unsigned int num_ros;
296 unsigned int num_nofuas;
297 unsigned int nluns;
298
299 bool removable;
300 bool can_stall;
301 bool cdrom;
302
303 char *transport_parm;
304 char *protocol_parm;
305 unsigned short vendor;
306 unsigned short product;
307 unsigned short release;
308 unsigned int buflen;
309
310 int transport_type;
311 char *transport_name;
312 int protocol_type;
313 char *protocol_name;
314
315} mod_data = { // Default values
316 .transport_parm = "BBB",
317 .protocol_parm = "SCSI",
318 .removable = 0,
319 .can_stall = 1,
320 .cdrom = 0,
321 .vendor = FSG_VENDOR_ID,
322 .product = FSG_PRODUCT_ID,
323 .release = 0xffff, // Use controller chip type
324 .buflen = 16384,
325 };
326
327
328module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
329 S_IRUGO);
330MODULE_PARM_DESC(file, "names of backing files or devices");
331
332module_param_named(serial, mod_data.serial, charp, S_IRUGO);
333MODULE_PARM_DESC(serial, "USB serial number");
334
335module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
336MODULE_PARM_DESC(ro, "true to force read-only");
337
338module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
339 S_IRUGO);
340MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
341
342module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
343MODULE_PARM_DESC(luns, "number of LUNs");
344
345module_param_named(removable, mod_data.removable, bool, S_IRUGO);
346MODULE_PARM_DESC(removable, "true to simulate removable media");
347
348module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
349MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
350
351module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
352MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
353
354/* In the non-TEST version, only the module parameters listed above
355 * are available. */
356#ifdef CONFIG_USB_FILE_STORAGE_TEST
357
358module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
359MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
360
361module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
362MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
363 "8070, or SCSI)");
364
365module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
366MODULE_PARM_DESC(vendor, "USB Vendor ID");
367
368module_param_named(product, mod_data.product, ushort, S_IRUGO);
369MODULE_PARM_DESC(product, "USB Product ID");
370
371module_param_named(release, mod_data.release, ushort, S_IRUGO);
372MODULE_PARM_DESC(release, "USB release number");
373
374module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
375MODULE_PARM_DESC(buflen, "I/O buffer size");
376
377#endif /* CONFIG_USB_FILE_STORAGE_TEST */
378
379
380/*
381 * These definitions will permit the compiler to avoid generating code for
382 * parts of the driver that aren't used in the non-TEST version. Even gcc
383 * can recognize when a test of a constant expression yields a dead code
384 * path.
385 */
386
387#ifdef CONFIG_USB_FILE_STORAGE_TEST
388
389#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
390#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
391#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
392
393#else
394
395#define transport_is_bbb() 1
396#define transport_is_cbi() 0
397#define protocol_is_scsi() 1
398
399#endif /* CONFIG_USB_FILE_STORAGE_TEST */
400
401
402/*-------------------------------------------------------------------------*/
403
404
405struct fsg_dev {
406 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
407 spinlock_t lock;
408 struct usb_gadget *gadget;
409
410 /* filesem protects: backing files in use */
411 struct rw_semaphore filesem;
412
413 /* reference counting: wait until all LUNs are released */
414 struct kref ref;
415
416 struct usb_ep *ep0; // Handy copy of gadget->ep0
417 struct usb_request *ep0req; // For control responses
418 unsigned int ep0_req_tag;
419 const char *ep0req_name;
420
421 struct usb_request *intreq; // For interrupt responses
422 int intreq_busy;
423 struct fsg_buffhd *intr_buffhd;
424
425 unsigned int bulk_out_maxpacket;
426 enum fsg_state state; // For exception handling
427 unsigned int exception_req_tag;
428
429 u8 config, new_config;
430
431 unsigned int running : 1;
432 unsigned int bulk_in_enabled : 1;
433 unsigned int bulk_out_enabled : 1;
434 unsigned int intr_in_enabled : 1;
435 unsigned int phase_error : 1;
436 unsigned int short_packet_received : 1;
437 unsigned int bad_lun_okay : 1;
438
439 unsigned long atomic_bitflags;
440#define REGISTERED 0
441#define IGNORE_BULK_OUT 1
442#define SUSPENDED 2
443
444 struct usb_ep *bulk_in;
445 struct usb_ep *bulk_out;
446 struct usb_ep *intr_in;
447
448 struct fsg_buffhd *next_buffhd_to_fill;
449 struct fsg_buffhd *next_buffhd_to_drain;
450
451 int thread_wakeup_needed;
452 struct completion thread_notifier;
453 struct task_struct *thread_task;
454
455 int cmnd_size;
456 u8 cmnd[MAX_COMMAND_SIZE];
457 enum data_direction data_dir;
458 u32 data_size;
459 u32 data_size_from_cmnd;
460 u32 tag;
461 unsigned int lun;
462 u32 residue;
463 u32 usb_amount_left;
464
465 /* The CB protocol offers no way for a host to know when a command
466 * has completed. As a result the next command may arrive early,
467 * and we will still have to handle it. For that reason we need
468 * a buffer to store new commands when using CB (or CBI, which
469 * does not oblige a host to wait for command completion either). */
470 int cbbuf_cmnd_size;
471 u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
472
473 unsigned int nluns;
474 struct fsg_lun *luns;
475 struct fsg_lun *curlun;
476 /* Must be the last entry */
477 struct fsg_buffhd buffhds[];
478};
479
480typedef void (*fsg_routine_t)(struct fsg_dev *);
481
482static int exception_in_progress(struct fsg_dev *fsg)
483{
484 return (fsg->state > FSG_STATE_IDLE);
485}
486
487/* Make bulk-out requests be divisible by the maxpacket size */
488static void set_bulk_out_req_length(struct fsg_dev *fsg,
489 struct fsg_buffhd *bh, unsigned int length)
490{
491 unsigned int rem;
492
493 bh->bulk_out_intended_length = length;
494 rem = length % fsg->bulk_out_maxpacket;
495 if (rem > 0)
496 length += fsg->bulk_out_maxpacket - rem;
497 bh->outreq->length = length;
498}
499
500static struct fsg_dev *the_fsg;
501static struct usb_gadget_driver fsg_driver;
502
503
504/*-------------------------------------------------------------------------*/
505
506static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
507{
508 const char *name;
509
510 if (ep == fsg->bulk_in)
511 name = "bulk-in";
512 else if (ep == fsg->bulk_out)
513 name = "bulk-out";
514 else
515 name = ep->name;
516 DBG(fsg, "%s set halt\n", name);
517 return usb_ep_set_halt(ep);
518}
519
520
521/*-------------------------------------------------------------------------*/
522
523/*
524 * DESCRIPTORS ... most are static, but strings and (full) configuration
525 * descriptors are built on demand. Also the (static) config and interface
526 * descriptors are adjusted during fsg_bind().
527 */
528
529/* There is only one configuration. */
530#define CONFIG_VALUE 1
531
532static struct usb_device_descriptor
533device_desc = {
534 .bLength = sizeof device_desc,
535 .bDescriptorType = USB_DT_DEVICE,
536
537 .bcdUSB = cpu_to_le16(0x0200),
538 .bDeviceClass = USB_CLASS_PER_INTERFACE,
539
540 /* The next three values can be overridden by module parameters */
541 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
542 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
543 .bcdDevice = cpu_to_le16(0xffff),
544
545 .iManufacturer = FSG_STRING_MANUFACTURER,
546 .iProduct = FSG_STRING_PRODUCT,
547 .iSerialNumber = FSG_STRING_SERIAL,
548 .bNumConfigurations = 1,
549};
550
551static struct usb_config_descriptor
552config_desc = {
553 .bLength = sizeof config_desc,
554 .bDescriptorType = USB_DT_CONFIG,
555
556 /* wTotalLength computed by usb_gadget_config_buf() */
557 .bNumInterfaces = 1,
558 .bConfigurationValue = CONFIG_VALUE,
559 .iConfiguration = FSG_STRING_CONFIG,
560 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
561 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
562};
563
564
565static struct usb_qualifier_descriptor
566dev_qualifier = {
567 .bLength = sizeof dev_qualifier,
568 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
569
570 .bcdUSB = cpu_to_le16(0x0200),
571 .bDeviceClass = USB_CLASS_PER_INTERFACE,
572
573 .bNumConfigurations = 1,
574};
575
576static int populate_bos(struct fsg_dev *fsg, u8 *buf)
577{
578 memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
579 buf += USB_DT_BOS_SIZE;
580
581 memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
582 buf += USB_DT_USB_EXT_CAP_SIZE;
583
584 memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
585
586 return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
587 + USB_DT_USB_EXT_CAP_SIZE;
588}
589
590/*
591 * Config descriptors must agree with the code that sets configurations
592 * and with code managing interfaces and their altsettings. They must
593 * also handle different speeds and other-speed requests.
594 */
595static int populate_config_buf(struct usb_gadget *gadget,
596 u8 *buf, u8 type, unsigned index)
597{
598 enum usb_device_speed speed = gadget->speed;
599 int len;
600 const struct usb_descriptor_header **function;
601
602 if (index > 0)
603 return -EINVAL;
604
605 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
606 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
607 function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
608 ? (const struct usb_descriptor_header **)fsg_hs_function
609 : (const struct usb_descriptor_header **)fsg_fs_function;
610
611 /* for now, don't advertise srp-only devices */
612 if (!gadget_is_otg(gadget))
613 function++;
614
615 len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
616 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
617 return len;
618}
619
620
621/*-------------------------------------------------------------------------*/
622
623/* These routines may be called in process context or in_irq */
624
625/* Caller must hold fsg->lock */
626static void wakeup_thread(struct fsg_dev *fsg)
627{
628 /* Tell the main thread that something has happened */
629 fsg->thread_wakeup_needed = 1;
630 if (fsg->thread_task)
631 wake_up_process(fsg->thread_task);
632}
633
634
635static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
636{
637 unsigned long flags;
638
639 /* Do nothing if a higher-priority exception is already in progress.
640 * If a lower-or-equal priority exception is in progress, preempt it
641 * and notify the main thread by sending it a signal. */
642 spin_lock_irqsave(&fsg->lock, flags);
643 if (fsg->state <= new_state) {
644 fsg->exception_req_tag = fsg->ep0_req_tag;
645 fsg->state = new_state;
646 if (fsg->thread_task)
647 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
648 fsg->thread_task);
649 }
650 spin_unlock_irqrestore(&fsg->lock, flags);
651}
652
653
654/*-------------------------------------------------------------------------*/
655
656/* The disconnect callback and ep0 routines. These always run in_irq,
657 * except that ep0_queue() is called in the main thread to acknowledge
658 * completion of various requests: set config, set interface, and
659 * Bulk-only device reset. */
660
661static void fsg_disconnect(struct usb_gadget *gadget)
662{
663 struct fsg_dev *fsg = get_gadget_data(gadget);
664
665 DBG(fsg, "disconnect or port reset\n");
666 raise_exception(fsg, FSG_STATE_DISCONNECT);
667}
668
669
670static int ep0_queue(struct fsg_dev *fsg)
671{
672 int rc;
673
674 rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
675 if (rc != 0 && rc != -ESHUTDOWN) {
676
677 /* We can't do much more than wait for a reset */
678 WARNING(fsg, "error in submission: %s --> %d\n",
679 fsg->ep0->name, rc);
680 }
681 return rc;
682}
683
684static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
685{
686 struct fsg_dev *fsg = ep->driver_data;
687
688 if (req->actual > 0)
689 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
690 if (req->status || req->actual != req->length)
691 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
692 req->status, req->actual, req->length);
693 if (req->status == -ECONNRESET) // Request was cancelled
694 usb_ep_fifo_flush(ep);
695
696 if (req->status == 0 && req->context)
697 ((fsg_routine_t) (req->context))(fsg);
698}
699
700
701/*-------------------------------------------------------------------------*/
702
703/* Bulk and interrupt endpoint completion handlers.
704 * These always run in_irq. */
705
706static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
707{
708 struct fsg_dev *fsg = ep->driver_data;
709 struct fsg_buffhd *bh = req->context;
710
711 if (req->status || req->actual != req->length)
712 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
713 req->status, req->actual, req->length);
714 if (req->status == -ECONNRESET) // Request was cancelled
715 usb_ep_fifo_flush(ep);
716
717 /* Hold the lock while we update the request and buffer states */
718 smp_wmb();
719 spin_lock(&fsg->lock);
720 bh->inreq_busy = 0;
721 bh->state = BUF_STATE_EMPTY;
722 wakeup_thread(fsg);
723 spin_unlock(&fsg->lock);
724}
725
726static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
727{
728 struct fsg_dev *fsg = ep->driver_data;
729 struct fsg_buffhd *bh = req->context;
730
731 dump_msg(fsg, "bulk-out", req->buf, req->actual);
732 if (req->status || req->actual != bh->bulk_out_intended_length)
733 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
734 req->status, req->actual,
735 bh->bulk_out_intended_length);
736 if (req->status == -ECONNRESET) // Request was cancelled
737 usb_ep_fifo_flush(ep);
738
739 /* Hold the lock while we update the request and buffer states */
740 smp_wmb();
741 spin_lock(&fsg->lock);
742 bh->outreq_busy = 0;
743 bh->state = BUF_STATE_FULL;
744 wakeup_thread(fsg);
745 spin_unlock(&fsg->lock);
746}
747
748
749#ifdef CONFIG_USB_FILE_STORAGE_TEST
750static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
751{
752 struct fsg_dev *fsg = ep->driver_data;
753 struct fsg_buffhd *bh = req->context;
754
755 if (req->status || req->actual != req->length)
756 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
757 req->status, req->actual, req->length);
758 if (req->status == -ECONNRESET) // Request was cancelled
759 usb_ep_fifo_flush(ep);
760
761 /* Hold the lock while we update the request and buffer states */
762 smp_wmb();
763 spin_lock(&fsg->lock);
764 fsg->intreq_busy = 0;
765 bh->state = BUF_STATE_EMPTY;
766 wakeup_thread(fsg);
767 spin_unlock(&fsg->lock);
768}
769
770#else
771static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
772{}
773#endif /* CONFIG_USB_FILE_STORAGE_TEST */
774
775
776/*-------------------------------------------------------------------------*/
777
778/* Ep0 class-specific handlers. These always run in_irq. */
779
780#ifdef CONFIG_USB_FILE_STORAGE_TEST
781static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
782{
783 struct usb_request *req = fsg->ep0req;
784 static u8 cbi_reset_cmnd[6] = {
785 SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
786
787 /* Error in command transfer? */
788 if (req->status || req->length != req->actual ||
789 req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
790
791 /* Not all controllers allow a protocol stall after
792 * receiving control-out data, but we'll try anyway. */
793 fsg_set_halt(fsg, fsg->ep0);
794 return; // Wait for reset
795 }
796
797 /* Is it the special reset command? */
798 if (req->actual >= sizeof cbi_reset_cmnd &&
799 memcmp(req->buf, cbi_reset_cmnd,
800 sizeof cbi_reset_cmnd) == 0) {
801
802 /* Raise an exception to stop the current operation
803 * and reinitialize our state. */
804 DBG(fsg, "cbi reset request\n");
805 raise_exception(fsg, FSG_STATE_RESET);
806 return;
807 }
808
809 VDBG(fsg, "CB[I] accept device-specific command\n");
810 spin_lock(&fsg->lock);
811
812 /* Save the command for later */
813 if (fsg->cbbuf_cmnd_size)
814 WARNING(fsg, "CB[I] overwriting previous command\n");
815 fsg->cbbuf_cmnd_size = req->actual;
816 memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
817
818 wakeup_thread(fsg);
819 spin_unlock(&fsg->lock);
820}
821
822#else
823static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
824{}
825#endif /* CONFIG_USB_FILE_STORAGE_TEST */
826
827
828static int class_setup_req(struct fsg_dev *fsg,
829 const struct usb_ctrlrequest *ctrl)
830{
831 struct usb_request *req = fsg->ep0req;
832 int value = -EOPNOTSUPP;
833 u16 w_index = le16_to_cpu(ctrl->wIndex);
834 u16 w_value = le16_to_cpu(ctrl->wValue);
835 u16 w_length = le16_to_cpu(ctrl->wLength);
836
837 if (!fsg->config)
838 return value;
839
840 /* Handle Bulk-only class-specific requests */
841 if (transport_is_bbb()) {
842 switch (ctrl->bRequest) {
843
844 case US_BULK_RESET_REQUEST:
845 if (ctrl->bRequestType != (USB_DIR_OUT |
846 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
847 break;
848 if (w_index != 0 || w_value != 0 || w_length != 0) {
849 value = -EDOM;
850 break;
851 }
852
853 /* Raise an exception to stop the current operation
854 * and reinitialize our state. */
855 DBG(fsg, "bulk reset request\n");
856 raise_exception(fsg, FSG_STATE_RESET);
857 value = DELAYED_STATUS;
858 break;
859
860 case US_BULK_GET_MAX_LUN:
861 if (ctrl->bRequestType != (USB_DIR_IN |
862 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
863 break;
864 if (w_index != 0 || w_value != 0 || w_length != 1) {
865 value = -EDOM;
866 break;
867 }
868 VDBG(fsg, "get max LUN\n");
869 *(u8 *) req->buf = fsg->nluns - 1;
870 value = 1;
871 break;
872 }
873 }
874
875 /* Handle CBI class-specific requests */
876 else {
877 switch (ctrl->bRequest) {
878
879 case USB_CBI_ADSC_REQUEST:
880 if (ctrl->bRequestType != (USB_DIR_OUT |
881 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
882 break;
883 if (w_index != 0 || w_value != 0) {
884 value = -EDOM;
885 break;
886 }
887 if (w_length > MAX_COMMAND_SIZE) {
888 value = -EOVERFLOW;
889 break;
890 }
891 value = w_length;
892 fsg->ep0req->context = received_cbi_adsc;
893 break;
894 }
895 }
896
897 if (value == -EOPNOTSUPP)
898 VDBG(fsg,
899 "unknown class-specific control req "
900 "%02x.%02x v%04x i%04x l%u\n",
901 ctrl->bRequestType, ctrl->bRequest,
902 le16_to_cpu(ctrl->wValue), w_index, w_length);
903 return value;
904}
905
906
907/*-------------------------------------------------------------------------*/
908
909/* Ep0 standard request handlers. These always run in_irq. */
910
911static int standard_setup_req(struct fsg_dev *fsg,
912 const struct usb_ctrlrequest *ctrl)
913{
914 struct usb_request *req = fsg->ep0req;
915 int value = -EOPNOTSUPP;
916 u16 w_index = le16_to_cpu(ctrl->wIndex);
917 u16 w_value = le16_to_cpu(ctrl->wValue);
918
919 /* Usually this just stores reply data in the pre-allocated ep0 buffer,
920 * but config change events will also reconfigure hardware. */
921 switch (ctrl->bRequest) {
922
923 case USB_REQ_GET_DESCRIPTOR:
924 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
925 USB_RECIP_DEVICE))
926 break;
927 switch (w_value >> 8) {
928
929 case USB_DT_DEVICE:
930 VDBG(fsg, "get device descriptor\n");
931 device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
932 value = sizeof device_desc;
933 memcpy(req->buf, &device_desc, value);
934 break;
935 case USB_DT_DEVICE_QUALIFIER:
936 VDBG(fsg, "get device qualifier\n");
937 if (!gadget_is_dualspeed(fsg->gadget) ||
938 fsg->gadget->speed == USB_SPEED_SUPER)
939 break;
940 /*
941 * Assume ep0 uses the same maxpacket value for both
942 * speeds
943 */
944 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
945 value = sizeof dev_qualifier;
946 memcpy(req->buf, &dev_qualifier, value);
947 break;
948
949 case USB_DT_OTHER_SPEED_CONFIG:
950 VDBG(fsg, "get other-speed config descriptor\n");
951 if (!gadget_is_dualspeed(fsg->gadget) ||
952 fsg->gadget->speed == USB_SPEED_SUPER)
953 break;
954 goto get_config;
955 case USB_DT_CONFIG:
956 VDBG(fsg, "get configuration descriptor\n");
957get_config:
958 value = populate_config_buf(fsg->gadget,
959 req->buf,
960 w_value >> 8,
961 w_value & 0xff);
962 break;
963
964 case USB_DT_STRING:
965 VDBG(fsg, "get string descriptor\n");
966
967 /* wIndex == language code */
968 value = usb_gadget_get_string(&fsg_stringtab,
969 w_value & 0xff, req->buf);
970 break;
971
972 case USB_DT_BOS:
973 VDBG(fsg, "get bos descriptor\n");
974
975 if (gadget_is_superspeed(fsg->gadget))
976 value = populate_bos(fsg, req->buf);
977 break;
978 }
979
980 break;
981
982 /* One config, two speeds */
983 case USB_REQ_SET_CONFIGURATION:
984 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
985 USB_RECIP_DEVICE))
986 break;
987 VDBG(fsg, "set configuration\n");
988 if (w_value == CONFIG_VALUE || w_value == 0) {
989 fsg->new_config = w_value;
990
991 /* Raise an exception to wipe out previous transaction
992 * state (queued bufs, etc) and set the new config. */
993 raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
994 value = DELAYED_STATUS;
995 }
996 break;
997 case USB_REQ_GET_CONFIGURATION:
998 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
999 USB_RECIP_DEVICE))
1000 break;
1001 VDBG(fsg, "get configuration\n");
1002 *(u8 *) req->buf = fsg->config;
1003 value = 1;
1004 break;
1005
1006 case USB_REQ_SET_INTERFACE:
1007 if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
1008 USB_RECIP_INTERFACE))
1009 break;
1010 if (fsg->config && w_index == 0) {
1011
1012 /* Raise an exception to wipe out previous transaction
1013 * state (queued bufs, etc) and install the new
1014 * interface altsetting. */
1015 raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
1016 value = DELAYED_STATUS;
1017 }
1018 break;
1019 case USB_REQ_GET_INTERFACE:
1020 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1021 USB_RECIP_INTERFACE))
1022 break;
1023 if (!fsg->config)
1024 break;
1025 if (w_index != 0) {
1026 value = -EDOM;
1027 break;
1028 }
1029 VDBG(fsg, "get interface\n");
1030 *(u8 *) req->buf = 0;
1031 value = 1;
1032 break;
1033
1034 default:
1035 VDBG(fsg,
1036 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1037 ctrl->bRequestType, ctrl->bRequest,
1038 w_value, w_index, le16_to_cpu(ctrl->wLength));
1039 }
1040
1041 return value;
1042}
1043
1044
1045static int fsg_setup(struct usb_gadget *gadget,
1046 const struct usb_ctrlrequest *ctrl)
1047{
1048 struct fsg_dev *fsg = get_gadget_data(gadget);
1049 int rc;
1050 int w_length = le16_to_cpu(ctrl->wLength);
1051
1052 ++fsg->ep0_req_tag; // Record arrival of a new request
1053 fsg->ep0req->context = NULL;
1054 fsg->ep0req->length = 0;
1055 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
1056
1057 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
1058 rc = class_setup_req(fsg, ctrl);
1059 else
1060 rc = standard_setup_req(fsg, ctrl);
1061
1062 /* Respond with data/status or defer until later? */
1063 if (rc >= 0 && rc != DELAYED_STATUS) {
1064 rc = min(rc, w_length);
1065 fsg->ep0req->length = rc;
1066 fsg->ep0req->zero = rc < w_length;
1067 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
1068 "ep0-in" : "ep0-out");
1069 rc = ep0_queue(fsg);
1070 }
1071
1072 /* Device either stalls (rc < 0) or reports success */
1073 return rc;
1074}
1075
1076
1077/*-------------------------------------------------------------------------*/
1078
1079/* All the following routines run in process context */
1080
1081
1082/* Use this for bulk or interrupt transfers, not ep0 */
1083static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
1084 struct usb_request *req, int *pbusy,
1085 enum fsg_buffer_state *state)
1086{
1087 int rc;
1088
1089 if (ep == fsg->bulk_in)
1090 dump_msg(fsg, "bulk-in", req->buf, req->length);
1091 else if (ep == fsg->intr_in)
1092 dump_msg(fsg, "intr-in", req->buf, req->length);
1093
1094 spin_lock_irq(&fsg->lock);
1095 *pbusy = 1;
1096 *state = BUF_STATE_BUSY;
1097 spin_unlock_irq(&fsg->lock);
1098 rc = usb_ep_queue(ep, req, GFP_KERNEL);
1099 if (rc != 0) {
1100 *pbusy = 0;
1101 *state = BUF_STATE_EMPTY;
1102
1103 /* We can't do much more than wait for a reset */
1104
1105 /* Note: currently the net2280 driver fails zero-length
1106 * submissions if DMA is enabled. */
1107 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
1108 req->length == 0))
1109 WARNING(fsg, "error in submission: %s --> %d\n",
1110 ep->name, rc);
1111 }
1112}
1113
1114
1115static int sleep_thread(struct fsg_dev *fsg)
1116{
1117 int rc = 0;
1118
1119 /* Wait until a signal arrives or we are woken up */
1120 for (;;) {
1121 try_to_freeze();
1122 set_current_state(TASK_INTERRUPTIBLE);
1123 if (signal_pending(current)) {
1124 rc = -EINTR;
1125 break;
1126 }
1127 if (fsg->thread_wakeup_needed)
1128 break;
1129 schedule();
1130 }
1131 __set_current_state(TASK_RUNNING);
1132 fsg->thread_wakeup_needed = 0;
1133 return rc;
1134}
1135
1136
1137/*-------------------------------------------------------------------------*/
1138
1139static int do_read(struct fsg_dev *fsg)
1140{
1141 struct fsg_lun *curlun = fsg->curlun;
1142 u32 lba;
1143 struct fsg_buffhd *bh;
1144 int rc;
1145 u32 amount_left;
1146 loff_t file_offset, file_offset_tmp;
1147 unsigned int amount;
1148 ssize_t nread;
1149
1150 /* Get the starting Logical Block Address and check that it's
1151 * not too big */
1152 if (fsg->cmnd[0] == READ_6)
1153 lba = get_unaligned_be24(&fsg->cmnd[1]);
1154 else {
1155 lba = get_unaligned_be32(&fsg->cmnd[2]);
1156
1157 /* We allow DPO (Disable Page Out = don't save data in the
1158 * cache) and FUA (Force Unit Access = don't read from the
1159 * cache), but we don't implement them. */
1160 if ((fsg->cmnd[1] & ~0x18) != 0) {
1161 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1162 return -EINVAL;
1163 }
1164 }
1165 if (lba >= curlun->num_sectors) {
1166 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1167 return -EINVAL;
1168 }
1169 file_offset = ((loff_t) lba) << curlun->blkbits;
1170
1171 /* Carry out the file reads */
1172 amount_left = fsg->data_size_from_cmnd;
1173 if (unlikely(amount_left == 0))
1174 return -EIO; // No default reply
1175
1176 for (;;) {
1177
1178 /* Figure out how much we need to read:
1179 * Try to read the remaining amount.
1180 * But don't read more than the buffer size.
1181 * And don't try to read past the end of the file.
1182 */
1183 amount = min((unsigned int) amount_left, mod_data.buflen);
1184 amount = min((loff_t) amount,
1185 curlun->file_length - file_offset);
1186
1187 /* Wait for the next buffer to become available */
1188 bh = fsg->next_buffhd_to_fill;
1189 while (bh->state != BUF_STATE_EMPTY) {
1190 rc = sleep_thread(fsg);
1191 if (rc)
1192 return rc;
1193 }
1194
1195 /* If we were asked to read past the end of file,
1196 * end with an empty buffer. */
1197 if (amount == 0) {
1198 curlun->sense_data =
1199 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1200 curlun->sense_data_info = file_offset >> curlun->blkbits;
1201 curlun->info_valid = 1;
1202 bh->inreq->length = 0;
1203 bh->state = BUF_STATE_FULL;
1204 break;
1205 }
1206
1207 /* Perform the read */
1208 file_offset_tmp = file_offset;
1209 nread = vfs_read(curlun->filp,
1210 (char __user *) bh->buf,
1211 amount, &file_offset_tmp);
1212 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1213 (unsigned long long) file_offset,
1214 (int) nread);
1215 if (signal_pending(current))
1216 return -EINTR;
1217
1218 if (nread < 0) {
1219 LDBG(curlun, "error in file read: %d\n",
1220 (int) nread);
1221 nread = 0;
1222 } else if (nread < amount) {
1223 LDBG(curlun, "partial file read: %d/%u\n",
1224 (int) nread, amount);
1225 nread = round_down(nread, curlun->blksize);
1226 }
1227 file_offset += nread;
1228 amount_left -= nread;
1229 fsg->residue -= nread;
1230
1231 /* Except at the end of the transfer, nread will be
1232 * equal to the buffer size, which is divisible by the
1233 * bulk-in maxpacket size.
1234 */
1235 bh->inreq->length = nread;
1236 bh->state = BUF_STATE_FULL;
1237
1238 /* If an error occurred, report it and its position */
1239 if (nread < amount) {
1240 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1241 curlun->sense_data_info = file_offset >> curlun->blkbits;
1242 curlun->info_valid = 1;
1243 break;
1244 }
1245
1246 if (amount_left == 0)
1247 break; // No more left to read
1248
1249 /* Send this buffer and go read some more */
1250 bh->inreq->zero = 0;
1251 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1252 &bh->inreq_busy, &bh->state);
1253 fsg->next_buffhd_to_fill = bh->next;
1254 }
1255
1256 return -EIO; // No default reply
1257}
1258
1259
1260/*-------------------------------------------------------------------------*/
1261
1262static int do_write(struct fsg_dev *fsg)
1263{
1264 struct fsg_lun *curlun = fsg->curlun;
1265 u32 lba;
1266 struct fsg_buffhd *bh;
1267 int get_some_more;
1268 u32 amount_left_to_req, amount_left_to_write;
1269 loff_t usb_offset, file_offset, file_offset_tmp;
1270 unsigned int amount;
1271 ssize_t nwritten;
1272 int rc;
1273
1274 if (curlun->ro) {
1275 curlun->sense_data = SS_WRITE_PROTECTED;
1276 return -EINVAL;
1277 }
1278 spin_lock(&curlun->filp->f_lock);
1279 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
1280 spin_unlock(&curlun->filp->f_lock);
1281
1282 /* Get the starting Logical Block Address and check that it's
1283 * not too big */
1284 if (fsg->cmnd[0] == WRITE_6)
1285 lba = get_unaligned_be24(&fsg->cmnd[1]);
1286 else {
1287 lba = get_unaligned_be32(&fsg->cmnd[2]);
1288
1289 /* We allow DPO (Disable Page Out = don't save data in the
1290 * cache) and FUA (Force Unit Access = write directly to the
1291 * medium). We don't implement DPO; we implement FUA by
1292 * performing synchronous output. */
1293 if ((fsg->cmnd[1] & ~0x18) != 0) {
1294 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1295 return -EINVAL;
1296 }
1297 /* FUA */
1298 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
1299 spin_lock(&curlun->filp->f_lock);
1300 curlun->filp->f_flags |= O_DSYNC;
1301 spin_unlock(&curlun->filp->f_lock);
1302 }
1303 }
1304 if (lba >= curlun->num_sectors) {
1305 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1306 return -EINVAL;
1307 }
1308
1309 /* Carry out the file writes */
1310 get_some_more = 1;
1311 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
1312 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1313
1314 while (amount_left_to_write > 0) {
1315
1316 /* Queue a request for more data from the host */
1317 bh = fsg->next_buffhd_to_fill;
1318 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1319
1320 /* Figure out how much we want to get:
1321 * Try to get the remaining amount,
1322 * but not more than the buffer size.
1323 */
1324 amount = min(amount_left_to_req, mod_data.buflen);
1325
1326 /* Beyond the end of the backing file? */
1327 if (usb_offset >= curlun->file_length) {
1328 get_some_more = 0;
1329 curlun->sense_data =
1330 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1331 curlun->sense_data_info = usb_offset >> curlun->blkbits;
1332 curlun->info_valid = 1;
1333 continue;
1334 }
1335
1336 /* Get the next buffer */
1337 usb_offset += amount;
1338 fsg->usb_amount_left -= amount;
1339 amount_left_to_req -= amount;
1340 if (amount_left_to_req == 0)
1341 get_some_more = 0;
1342
1343 /* Except at the end of the transfer, amount will be
1344 * equal to the buffer size, which is divisible by
1345 * the bulk-out maxpacket size.
1346 */
1347 set_bulk_out_req_length(fsg, bh, amount);
1348 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1349 &bh->outreq_busy, &bh->state);
1350 fsg->next_buffhd_to_fill = bh->next;
1351 continue;
1352 }
1353
1354 /* Write the received data to the backing file */
1355 bh = fsg->next_buffhd_to_drain;
1356 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1357 break; // We stopped early
1358 if (bh->state == BUF_STATE_FULL) {
1359 smp_rmb();
1360 fsg->next_buffhd_to_drain = bh->next;
1361 bh->state = BUF_STATE_EMPTY;
1362
1363 /* Did something go wrong with the transfer? */
1364 if (bh->outreq->status != 0) {
1365 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1366 curlun->sense_data_info = file_offset >> curlun->blkbits;
1367 curlun->info_valid = 1;
1368 break;
1369 }
1370
1371 amount = bh->outreq->actual;
1372 if (curlun->file_length - file_offset < amount) {
1373 LERROR(curlun,
1374 "write %u @ %llu beyond end %llu\n",
1375 amount, (unsigned long long) file_offset,
1376 (unsigned long long) curlun->file_length);
1377 amount = curlun->file_length - file_offset;
1378 }
1379
1380 /* Don't accept excess data. The spec doesn't say
1381 * what to do in this case. We'll ignore the error.
1382 */
1383 amount = min(amount, bh->bulk_out_intended_length);
1384
1385 /* Don't write a partial block */
1386 amount = round_down(amount, curlun->blksize);
1387 if (amount == 0)
1388 goto empty_write;
1389
1390 /* Perform the write */
1391 file_offset_tmp = file_offset;
1392 nwritten = vfs_write(curlun->filp,
1393 (char __user *) bh->buf,
1394 amount, &file_offset_tmp);
1395 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1396 (unsigned long long) file_offset,
1397 (int) nwritten);
1398 if (signal_pending(current))
1399 return -EINTR; // Interrupted!
1400
1401 if (nwritten < 0) {
1402 LDBG(curlun, "error in file write: %d\n",
1403 (int) nwritten);
1404 nwritten = 0;
1405 } else if (nwritten < amount) {
1406 LDBG(curlun, "partial file write: %d/%u\n",
1407 (int) nwritten, amount);
1408 nwritten = round_down(nwritten, curlun->blksize);
1409 }
1410 file_offset += nwritten;
1411 amount_left_to_write -= nwritten;
1412 fsg->residue -= nwritten;
1413
1414 /* If an error occurred, report it and its position */
1415 if (nwritten < amount) {
1416 curlun->sense_data = SS_WRITE_ERROR;
1417 curlun->sense_data_info = file_offset >> curlun->blkbits;
1418 curlun->info_valid = 1;
1419 break;
1420 }
1421
1422 empty_write:
1423 /* Did the host decide to stop early? */
1424 if (bh->outreq->actual < bh->bulk_out_intended_length) {
1425 fsg->short_packet_received = 1;
1426 break;
1427 }
1428 continue;
1429 }
1430
1431 /* Wait for something to happen */
1432 rc = sleep_thread(fsg);
1433 if (rc)
1434 return rc;
1435 }
1436
1437 return -EIO; // No default reply
1438}
1439
1440
1441/*-------------------------------------------------------------------------*/
1442
1443static int do_synchronize_cache(struct fsg_dev *fsg)
1444{
1445 struct fsg_lun *curlun = fsg->curlun;
1446 int rc;
1447
1448 /* We ignore the requested LBA and write out all file's
1449 * dirty data buffers. */
1450 rc = fsg_lun_fsync_sub(curlun);
1451 if (rc)
1452 curlun->sense_data = SS_WRITE_ERROR;
1453 return 0;
1454}
1455
1456
1457/*-------------------------------------------------------------------------*/
1458
1459static void invalidate_sub(struct fsg_lun *curlun)
1460{
1461 struct file *filp = curlun->filp;
1462 struct inode *inode = filp->f_path.dentry->d_inode;
1463 unsigned long rc;
1464
1465 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1466 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
1467}
1468
1469static int do_verify(struct fsg_dev *fsg)
1470{
1471 struct fsg_lun *curlun = fsg->curlun;
1472 u32 lba;
1473 u32 verification_length;
1474 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1475 loff_t file_offset, file_offset_tmp;
1476 u32 amount_left;
1477 unsigned int amount;
1478 ssize_t nread;
1479
1480 /* Get the starting Logical Block Address and check that it's
1481 * not too big */
1482 lba = get_unaligned_be32(&fsg->cmnd[2]);
1483 if (lba >= curlun->num_sectors) {
1484 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1485 return -EINVAL;
1486 }
1487
1488 /* We allow DPO (Disable Page Out = don't save data in the
1489 * cache) but we don't implement it. */
1490 if ((fsg->cmnd[1] & ~0x10) != 0) {
1491 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1492 return -EINVAL;
1493 }
1494
1495 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
1496 if (unlikely(verification_length == 0))
1497 return -EIO; // No default reply
1498
1499 /* Prepare to carry out the file verify */
1500 amount_left = verification_length << curlun->blkbits;
1501 file_offset = ((loff_t) lba) << curlun->blkbits;
1502
1503 /* Write out all the dirty buffers before invalidating them */
1504 fsg_lun_fsync_sub(curlun);
1505 if (signal_pending(current))
1506 return -EINTR;
1507
1508 invalidate_sub(curlun);
1509 if (signal_pending(current))
1510 return -EINTR;
1511
1512 /* Just try to read the requested blocks */
1513 while (amount_left > 0) {
1514
1515 /* Figure out how much we need to read:
1516 * Try to read the remaining amount, but not more than
1517 * the buffer size.
1518 * And don't try to read past the end of the file.
1519 */
1520 amount = min((unsigned int) amount_left, mod_data.buflen);
1521 amount = min((loff_t) amount,
1522 curlun->file_length - file_offset);
1523 if (amount == 0) {
1524 curlun->sense_data =
1525 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1526 curlun->sense_data_info = file_offset >> curlun->blkbits;
1527 curlun->info_valid = 1;
1528 break;
1529 }
1530
1531 /* Perform the read */
1532 file_offset_tmp = file_offset;
1533 nread = vfs_read(curlun->filp,
1534 (char __user *) bh->buf,
1535 amount, &file_offset_tmp);
1536 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1537 (unsigned long long) file_offset,
1538 (int) nread);
1539 if (signal_pending(current))
1540 return -EINTR;
1541
1542 if (nread < 0) {
1543 LDBG(curlun, "error in file verify: %d\n",
1544 (int) nread);
1545 nread = 0;
1546 } else if (nread < amount) {
1547 LDBG(curlun, "partial file verify: %d/%u\n",
1548 (int) nread, amount);
1549 nread = round_down(nread, curlun->blksize);
1550 }
1551 if (nread == 0) {
1552 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1553 curlun->sense_data_info = file_offset >> curlun->blkbits;
1554 curlun->info_valid = 1;
1555 break;
1556 }
1557 file_offset += nread;
1558 amount_left -= nread;
1559 }
1560 return 0;
1561}
1562
1563
1564/*-------------------------------------------------------------------------*/
1565
1566static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1567{
1568 u8 *buf = (u8 *) bh->buf;
1569
1570 static char vendor_id[] = "Linux ";
1571 static char product_disk_id[] = "File-Stor Gadget";
1572 static char product_cdrom_id[] = "File-CD Gadget ";
1573
1574 if (!fsg->curlun) { // Unsupported LUNs are okay
1575 fsg->bad_lun_okay = 1;
1576 memset(buf, 0, 36);
1577 buf[0] = 0x7f; // Unsupported, no device-type
1578 buf[4] = 31; // Additional length
1579 return 36;
1580 }
1581
1582 memset(buf, 0, 8);
1583 buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
1584 if (mod_data.removable)
1585 buf[1] = 0x80;
1586 buf[2] = 2; // ANSI SCSI level 2
1587 buf[3] = 2; // SCSI-2 INQUIRY data format
1588 buf[4] = 31; // Additional length
1589 // No special options
1590 sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
1591 (mod_data.cdrom ? product_cdrom_id :
1592 product_disk_id),
1593 mod_data.release);
1594 return 36;
1595}
1596
1597
1598static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1599{
1600 struct fsg_lun *curlun = fsg->curlun;
1601 u8 *buf = (u8 *) bh->buf;
1602 u32 sd, sdinfo;
1603 int valid;
1604
1605 /*
1606 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1607 *
1608 * If a REQUEST SENSE command is received from an initiator
1609 * with a pending unit attention condition (before the target
1610 * generates the contingent allegiance condition), then the
1611 * target shall either:
1612 * a) report any pending sense data and preserve the unit
1613 * attention condition on the logical unit, or,
1614 * b) report the unit attention condition, may discard any
1615 * pending sense data, and clear the unit attention
1616 * condition on the logical unit for that initiator.
1617 *
1618 * FSG normally uses option a); enable this code to use option b).
1619 */
1620#if 0
1621 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1622 curlun->sense_data = curlun->unit_attention_data;
1623 curlun->unit_attention_data = SS_NO_SENSE;
1624 }
1625#endif
1626
1627 if (!curlun) { // Unsupported LUNs are okay
1628 fsg->bad_lun_okay = 1;
1629 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1630 sdinfo = 0;
1631 valid = 0;
1632 } else {
1633 sd = curlun->sense_data;
1634 sdinfo = curlun->sense_data_info;
1635 valid = curlun->info_valid << 7;
1636 curlun->sense_data = SS_NO_SENSE;
1637 curlun->sense_data_info = 0;
1638 curlun->info_valid = 0;
1639 }
1640
1641 memset(buf, 0, 18);
1642 buf[0] = valid | 0x70; // Valid, current error
1643 buf[2] = SK(sd);
1644 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1645 buf[7] = 18 - 8; // Additional sense length
1646 buf[12] = ASC(sd);
1647 buf[13] = ASCQ(sd);
1648 return 18;
1649}
1650
1651
1652static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1653{
1654 struct fsg_lun *curlun = fsg->curlun;
1655 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1656 int pmi = fsg->cmnd[8];
1657 u8 *buf = (u8 *) bh->buf;
1658
1659 /* Check the PMI and LBA fields */
1660 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1661 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1662 return -EINVAL;
1663 }
1664
1665 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1666 /* Max logical block */
1667 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
1668 return 8;
1669}
1670
1671
1672static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1673{
1674 struct fsg_lun *curlun = fsg->curlun;
1675 int msf = fsg->cmnd[1] & 0x02;
1676 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1677 u8 *buf = (u8 *) bh->buf;
1678
1679 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1680 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1681 return -EINVAL;
1682 }
1683 if (lba >= curlun->num_sectors) {
1684 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1685 return -EINVAL;
1686 }
1687
1688 memset(buf, 0, 8);
1689 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1690 store_cdrom_address(&buf[4], msf, lba);
1691 return 8;
1692}
1693
1694
1695static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1696{
1697 struct fsg_lun *curlun = fsg->curlun;
1698 int msf = fsg->cmnd[1] & 0x02;
1699 int start_track = fsg->cmnd[6];
1700 u8 *buf = (u8 *) bh->buf;
1701
1702 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1703 start_track > 1) {
1704 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1705 return -EINVAL;
1706 }
1707
1708 memset(buf, 0, 20);
1709 buf[1] = (20-2); /* TOC data length */
1710 buf[2] = 1; /* First track number */
1711 buf[3] = 1; /* Last track number */
1712 buf[5] = 0x16; /* Data track, copying allowed */
1713 buf[6] = 0x01; /* Only track is number 1 */
1714 store_cdrom_address(&buf[8], msf, 0);
1715
1716 buf[13] = 0x16; /* Lead-out track is data */
1717 buf[14] = 0xAA; /* Lead-out track number */
1718 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1719 return 20;
1720}
1721
1722
1723static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1724{
1725 struct fsg_lun *curlun = fsg->curlun;
1726 int mscmnd = fsg->cmnd[0];
1727 u8 *buf = (u8 *) bh->buf;
1728 u8 *buf0 = buf;
1729 int pc, page_code;
1730 int changeable_values, all_pages;
1731 int valid_page = 0;
1732 int len, limit;
1733
1734 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1735 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1736 return -EINVAL;
1737 }
1738 pc = fsg->cmnd[2] >> 6;
1739 page_code = fsg->cmnd[2] & 0x3f;
1740 if (pc == 3) {
1741 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1742 return -EINVAL;
1743 }
1744 changeable_values = (pc == 1);
1745 all_pages = (page_code == 0x3f);
1746
1747 /* Write the mode parameter header. Fixed values are: default
1748 * medium type, no cache control (DPOFUA), and no block descriptors.
1749 * The only variable value is the WriteProtect bit. We will fill in
1750 * the mode data length later. */
1751 memset(buf, 0, 8);
1752 if (mscmnd == MODE_SENSE) {
1753 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1754 buf += 4;
1755 limit = 255;
1756 } else { // MODE_SENSE_10
1757 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1758 buf += 8;
1759 limit = 65535; // Should really be mod_data.buflen
1760 }
1761
1762 /* No block descriptors */
1763
1764 /* The mode pages, in numerical order. The only page we support
1765 * is the Caching page. */
1766 if (page_code == 0x08 || all_pages) {
1767 valid_page = 1;
1768 buf[0] = 0x08; // Page code
1769 buf[1] = 10; // Page length
1770 memset(buf+2, 0, 10); // None of the fields are changeable
1771
1772 if (!changeable_values) {
1773 buf[2] = 0x04; // Write cache enable,
1774 // Read cache not disabled
1775 // No cache retention priorities
1776 put_unaligned_be16(0xffff, &buf[4]);
1777 /* Don't disable prefetch */
1778 /* Minimum prefetch = 0 */
1779 put_unaligned_be16(0xffff, &buf[8]);
1780 /* Maximum prefetch */
1781 put_unaligned_be16(0xffff, &buf[10]);
1782 /* Maximum prefetch ceiling */
1783 }
1784 buf += 12;
1785 }
1786
1787 /* Check that a valid page was requested and the mode data length
1788 * isn't too long. */
1789 len = buf - buf0;
1790 if (!valid_page || len > limit) {
1791 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1792 return -EINVAL;
1793 }
1794
1795 /* Store the mode data length */
1796 if (mscmnd == MODE_SENSE)
1797 buf0[0] = len - 1;
1798 else
1799 put_unaligned_be16(len - 2, buf0);
1800 return len;
1801}
1802
1803
1804static int do_start_stop(struct fsg_dev *fsg)
1805{
1806 struct fsg_lun *curlun = fsg->curlun;
1807 int loej, start;
1808
1809 if (!mod_data.removable) {
1810 curlun->sense_data = SS_INVALID_COMMAND;
1811 return -EINVAL;
1812 }
1813
1814 // int immed = fsg->cmnd[1] & 0x01;
1815 loej = fsg->cmnd[4] & 0x02;
1816 start = fsg->cmnd[4] & 0x01;
1817
1818#ifdef CONFIG_USB_FILE_STORAGE_TEST
1819 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1820 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1821 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1822 return -EINVAL;
1823 }
1824
1825 if (!start) {
1826
1827 /* Are we allowed to unload the media? */
1828 if (curlun->prevent_medium_removal) {
1829 LDBG(curlun, "unload attempt prevented\n");
1830 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1831 return -EINVAL;
1832 }
1833 if (loej) { // Simulate an unload/eject
1834 up_read(&fsg->filesem);
1835 down_write(&fsg->filesem);
1836 fsg_lun_close(curlun);
1837 up_write(&fsg->filesem);
1838 down_read(&fsg->filesem);
1839 }
1840 } else {
1841
1842 /* Our emulation doesn't support mounting; the medium is
1843 * available for use as soon as it is loaded. */
1844 if (!fsg_lun_is_open(curlun)) {
1845 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1846 return -EINVAL;
1847 }
1848 }
1849#endif
1850 return 0;
1851}
1852
1853
1854static int do_prevent_allow(struct fsg_dev *fsg)
1855{
1856 struct fsg_lun *curlun = fsg->curlun;
1857 int prevent;
1858
1859 if (!mod_data.removable) {
1860 curlun->sense_data = SS_INVALID_COMMAND;
1861 return -EINVAL;
1862 }
1863
1864 prevent = fsg->cmnd[4] & 0x01;
1865 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1866 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1867 return -EINVAL;
1868 }
1869
1870 if (curlun->prevent_medium_removal && !prevent)
1871 fsg_lun_fsync_sub(curlun);
1872 curlun->prevent_medium_removal = prevent;
1873 return 0;
1874}
1875
1876
1877static int do_read_format_capacities(struct fsg_dev *fsg,
1878 struct fsg_buffhd *bh)
1879{
1880 struct fsg_lun *curlun = fsg->curlun;
1881 u8 *buf = (u8 *) bh->buf;
1882
1883 buf[0] = buf[1] = buf[2] = 0;
1884 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
1885 buf += 4;
1886
1887 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1888 /* Number of blocks */
1889 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
1890 buf[4] = 0x02; /* Current capacity */
1891 return 12;
1892}
1893
1894
1895static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1896{
1897 struct fsg_lun *curlun = fsg->curlun;
1898
1899 /* We don't support MODE SELECT */
1900 curlun->sense_data = SS_INVALID_COMMAND;
1901 return -EINVAL;
1902}
1903
1904
1905/*-------------------------------------------------------------------------*/
1906
1907static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1908{
1909 int rc;
1910
1911 rc = fsg_set_halt(fsg, fsg->bulk_in);
1912 if (rc == -EAGAIN)
1913 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1914 while (rc != 0) {
1915 if (rc != -EAGAIN) {
1916 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1917 rc = 0;
1918 break;
1919 }
1920
1921 /* Wait for a short time and then try again */
1922 if (msleep_interruptible(100) != 0)
1923 return -EINTR;
1924 rc = usb_ep_set_halt(fsg->bulk_in);
1925 }
1926 return rc;
1927}
1928
1929static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1930{
1931 int rc;
1932
1933 DBG(fsg, "bulk-in set wedge\n");
1934 rc = usb_ep_set_wedge(fsg->bulk_in);
1935 if (rc == -EAGAIN)
1936 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1937 while (rc != 0) {
1938 if (rc != -EAGAIN) {
1939 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1940 rc = 0;
1941 break;
1942 }
1943
1944 /* Wait for a short time and then try again */
1945 if (msleep_interruptible(100) != 0)
1946 return -EINTR;
1947 rc = usb_ep_set_wedge(fsg->bulk_in);
1948 }
1949 return rc;
1950}
1951
1952static int throw_away_data(struct fsg_dev *fsg)
1953{
1954 struct fsg_buffhd *bh;
1955 u32 amount;
1956 int rc;
1957
1958 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1959 fsg->usb_amount_left > 0) {
1960
1961 /* Throw away the data in a filled buffer */
1962 if (bh->state == BUF_STATE_FULL) {
1963 smp_rmb();
1964 bh->state = BUF_STATE_EMPTY;
1965 fsg->next_buffhd_to_drain = bh->next;
1966
1967 /* A short packet or an error ends everything */
1968 if (bh->outreq->actual < bh->bulk_out_intended_length ||
1969 bh->outreq->status != 0) {
1970 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1971 return -EINTR;
1972 }
1973 continue;
1974 }
1975
1976 /* Try to submit another request if we need one */
1977 bh = fsg->next_buffhd_to_fill;
1978 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1979 amount = min(fsg->usb_amount_left,
1980 (u32) mod_data.buflen);
1981
1982 /* Except at the end of the transfer, amount will be
1983 * equal to the buffer size, which is divisible by
1984 * the bulk-out maxpacket size.
1985 */
1986 set_bulk_out_req_length(fsg, bh, amount);
1987 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1988 &bh->outreq_busy, &bh->state);
1989 fsg->next_buffhd_to_fill = bh->next;
1990 fsg->usb_amount_left -= amount;
1991 continue;
1992 }
1993
1994 /* Otherwise wait for something to happen */
1995 rc = sleep_thread(fsg);
1996 if (rc)
1997 return rc;
1998 }
1999 return 0;
2000}
2001
2002
2003static int finish_reply(struct fsg_dev *fsg)
2004{
2005 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2006 int rc = 0;
2007
2008 switch (fsg->data_dir) {
2009 case DATA_DIR_NONE:
2010 break; // Nothing to send
2011
2012 /* If we don't know whether the host wants to read or write,
2013 * this must be CB or CBI with an unknown command. We mustn't
2014 * try to send or receive any data. So stall both bulk pipes
2015 * if we can and wait for a reset. */
2016 case DATA_DIR_UNKNOWN:
2017 if (mod_data.can_stall) {
2018 fsg_set_halt(fsg, fsg->bulk_out);
2019 rc = halt_bulk_in_endpoint(fsg);
2020 }
2021 break;
2022
2023 /* All but the last buffer of data must have already been sent */
2024 case DATA_DIR_TO_HOST:
2025 if (fsg->data_size == 0)
2026 ; // Nothing to send
2027
2028 /* If there's no residue, simply send the last buffer */
2029 else if (fsg->residue == 0) {
2030 bh->inreq->zero = 0;
2031 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2032 &bh->inreq_busy, &bh->state);
2033 fsg->next_buffhd_to_fill = bh->next;
2034 }
2035
2036 /* There is a residue. For CB and CBI, simply mark the end
2037 * of the data with a short packet. However, if we are
2038 * allowed to stall, there was no data at all (residue ==
2039 * data_size), and the command failed (invalid LUN or
2040 * sense data is set), then halt the bulk-in endpoint
2041 * instead. */
2042 else if (!transport_is_bbb()) {
2043 if (mod_data.can_stall &&
2044 fsg->residue == fsg->data_size &&
2045 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2046 bh->state = BUF_STATE_EMPTY;
2047 rc = halt_bulk_in_endpoint(fsg);
2048 } else {
2049 bh->inreq->zero = 1;
2050 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2051 &bh->inreq_busy, &bh->state);
2052 fsg->next_buffhd_to_fill = bh->next;
2053 }
2054 }
2055
2056 /*
2057 * For Bulk-only, mark the end of the data with a short
2058 * packet. If we are allowed to stall, halt the bulk-in
2059 * endpoint. (Note: This violates the Bulk-Only Transport
2060 * specification, which requires us to pad the data if we
2061 * don't halt the endpoint. Presumably nobody will mind.)
2062 */
2063 else {
2064 bh->inreq->zero = 1;
2065 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2066 &bh->inreq_busy, &bh->state);
2067 fsg->next_buffhd_to_fill = bh->next;
2068 if (mod_data.can_stall)
2069 rc = halt_bulk_in_endpoint(fsg);
2070 }
2071 break;
2072
2073 /* We have processed all we want from the data the host has sent.
2074 * There may still be outstanding bulk-out requests. */
2075 case DATA_DIR_FROM_HOST:
2076 if (fsg->residue == 0)
2077 ; // Nothing to receive
2078
2079 /* Did the host stop sending unexpectedly early? */
2080 else if (fsg->short_packet_received) {
2081 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2082 rc = -EINTR;
2083 }
2084
2085 /* We haven't processed all the incoming data. Even though
2086 * we may be allowed to stall, doing so would cause a race.
2087 * The controller may already have ACK'ed all the remaining
2088 * bulk-out packets, in which case the host wouldn't see a
2089 * STALL. Not realizing the endpoint was halted, it wouldn't
2090 * clear the halt -- leading to problems later on. */
2091#if 0
2092 else if (mod_data.can_stall) {
2093 fsg_set_halt(fsg, fsg->bulk_out);
2094 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2095 rc = -EINTR;
2096 }
2097#endif
2098
2099 /* We can't stall. Read in the excess data and throw it
2100 * all away. */
2101 else
2102 rc = throw_away_data(fsg);
2103 break;
2104 }
2105 return rc;
2106}
2107
2108
2109static int send_status(struct fsg_dev *fsg)
2110{
2111 struct fsg_lun *curlun = fsg->curlun;
2112 struct fsg_buffhd *bh;
2113 int rc;
2114 u8 status = US_BULK_STAT_OK;
2115 u32 sd, sdinfo = 0;
2116
2117 /* Wait for the next buffer to become available */
2118 bh = fsg->next_buffhd_to_fill;
2119 while (bh->state != BUF_STATE_EMPTY) {
2120 rc = sleep_thread(fsg);
2121 if (rc)
2122 return rc;
2123 }
2124
2125 if (curlun) {
2126 sd = curlun->sense_data;
2127 sdinfo = curlun->sense_data_info;
2128 } else if (fsg->bad_lun_okay)
2129 sd = SS_NO_SENSE;
2130 else
2131 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2132
2133 if (fsg->phase_error) {
2134 DBG(fsg, "sending phase-error status\n");
2135 status = US_BULK_STAT_PHASE;
2136 sd = SS_INVALID_COMMAND;
2137 } else if (sd != SS_NO_SENSE) {
2138 DBG(fsg, "sending command-failure status\n");
2139 status = US_BULK_STAT_FAIL;
2140 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2141 " info x%x\n",
2142 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2143 }
2144
2145 if (transport_is_bbb()) {
2146 struct bulk_cs_wrap *csw = bh->buf;
2147
2148 /* Store and send the Bulk-only CSW */
2149 csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
2150 csw->Tag = fsg->tag;
2151 csw->Residue = cpu_to_le32(fsg->residue);
2152 csw->Status = status;
2153
2154 bh->inreq->length = US_BULK_CS_WRAP_LEN;
2155 bh->inreq->zero = 0;
2156 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2157 &bh->inreq_busy, &bh->state);
2158
2159 } else if (mod_data.transport_type == USB_PR_CB) {
2160
2161 /* Control-Bulk transport has no status phase! */
2162 return 0;
2163
2164 } else { // USB_PR_CBI
2165 struct interrupt_data *buf = bh->buf;
2166
2167 /* Store and send the Interrupt data. UFI sends the ASC
2168 * and ASCQ bytes. Everything else sends a Type (which
2169 * is always 0) and the status Value. */
2170 if (mod_data.protocol_type == USB_SC_UFI) {
2171 buf->bType = ASC(sd);
2172 buf->bValue = ASCQ(sd);
2173 } else {
2174 buf->bType = 0;
2175 buf->bValue = status;
2176 }
2177 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2178
2179 fsg->intr_buffhd = bh; // Point to the right buffhd
2180 fsg->intreq->buf = bh->inreq->buf;
2181 fsg->intreq->context = bh;
2182 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2183 &fsg->intreq_busy, &bh->state);
2184 }
2185
2186 fsg->next_buffhd_to_fill = bh->next;
2187 return 0;
2188}
2189
2190
2191/*-------------------------------------------------------------------------*/
2192
2193/* Check whether the command is properly formed and whether its data size
2194 * and direction agree with the values we already have. */
2195static int check_command(struct fsg_dev *fsg, int cmnd_size,
2196 enum data_direction data_dir, unsigned int mask,
2197 int needs_medium, const char *name)
2198{
2199 int i;
2200 int lun = fsg->cmnd[1] >> 5;
2201 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2202 char hdlen[20];
2203 struct fsg_lun *curlun;
2204
2205 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2206 * Transparent SCSI doesn't pad. */
2207 if (protocol_is_scsi())
2208 ;
2209
2210 /* There's some disagreement as to whether RBC pads commands or not.
2211 * We'll play it safe and accept either form. */
2212 else if (mod_data.protocol_type == USB_SC_RBC) {
2213 if (fsg->cmnd_size == 12)
2214 cmnd_size = 12;
2215
2216 /* All the other protocols pad to 12 bytes */
2217 } else
2218 cmnd_size = 12;
2219
2220 hdlen[0] = 0;
2221 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2222 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2223 fsg->data_size);
2224 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2225 name, cmnd_size, dirletter[(int) data_dir],
2226 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2227
2228 /* We can't reply at all until we know the correct data direction
2229 * and size. */
2230 if (fsg->data_size_from_cmnd == 0)
2231 data_dir = DATA_DIR_NONE;
2232 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2233 fsg->data_dir = data_dir;
2234 fsg->data_size = fsg->data_size_from_cmnd;
2235
2236 } else { // Bulk-only
2237 if (fsg->data_size < fsg->data_size_from_cmnd) {
2238
2239 /* Host data size < Device data size is a phase error.
2240 * Carry out the command, but only transfer as much
2241 * as we are allowed. */
2242 fsg->data_size_from_cmnd = fsg->data_size;
2243 fsg->phase_error = 1;
2244 }
2245 }
2246 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2247
2248 /* Conflicting data directions is a phase error */
2249 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2250 fsg->phase_error = 1;
2251 return -EINVAL;
2252 }
2253
2254 /* Verify the length of the command itself */
2255 if (cmnd_size != fsg->cmnd_size) {
2256
2257 /* Special case workaround: There are plenty of buggy SCSI
2258 * implementations. Many have issues with cbw->Length
2259 * field passing a wrong command size. For those cases we
2260 * always try to work around the problem by using the length
2261 * sent by the host side provided it is at least as large
2262 * as the correct command length.
2263 * Examples of such cases would be MS-Windows, which issues
2264 * REQUEST SENSE with cbw->Length == 12 where it should
2265 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2266 * REQUEST SENSE with cbw->Length == 10 where it should
2267 * be 6 as well.
2268 */
2269 if (cmnd_size <= fsg->cmnd_size) {
2270 DBG(fsg, "%s is buggy! Expected length %d "
2271 "but we got %d\n", name,
2272 cmnd_size, fsg->cmnd_size);
2273 cmnd_size = fsg->cmnd_size;
2274 } else {
2275 fsg->phase_error = 1;
2276 return -EINVAL;
2277 }
2278 }
2279
2280 /* Check that the LUN values are consistent */
2281 if (transport_is_bbb()) {
2282 if (fsg->lun != lun)
2283 DBG(fsg, "using LUN %d from CBW, "
2284 "not LUN %d from CDB\n",
2285 fsg->lun, lun);
2286 }
2287
2288 /* Check the LUN */
2289 curlun = fsg->curlun;
2290 if (curlun) {
2291 if (fsg->cmnd[0] != REQUEST_SENSE) {
2292 curlun->sense_data = SS_NO_SENSE;
2293 curlun->sense_data_info = 0;
2294 curlun->info_valid = 0;
2295 }
2296 } else {
2297 fsg->bad_lun_okay = 0;
2298
2299 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2300 * to use unsupported LUNs; all others may not. */
2301 if (fsg->cmnd[0] != INQUIRY &&
2302 fsg->cmnd[0] != REQUEST_SENSE) {
2303 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2304 return -EINVAL;
2305 }
2306 }
2307
2308 /* If a unit attention condition exists, only INQUIRY and
2309 * REQUEST SENSE commands are allowed; anything else must fail. */
2310 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
2311 fsg->cmnd[0] != INQUIRY &&
2312 fsg->cmnd[0] != REQUEST_SENSE) {
2313 curlun->sense_data = curlun->unit_attention_data;
2314 curlun->unit_attention_data = SS_NO_SENSE;
2315 return -EINVAL;
2316 }
2317
2318 /* Check that only command bytes listed in the mask are non-zero */
2319 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2320 for (i = 1; i < cmnd_size; ++i) {
2321 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2322 if (curlun)
2323 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2324 return -EINVAL;
2325 }
2326 }
2327
2328 /* If the medium isn't mounted and the command needs to access
2329 * it, return an error. */
2330 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
2331 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2332 return -EINVAL;
2333 }
2334
2335 return 0;
2336}
2337
2338/* wrapper of check_command for data size in blocks handling */
2339static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
2340 enum data_direction data_dir, unsigned int mask,
2341 int needs_medium, const char *name)
2342{
2343 if (fsg->curlun)
2344 fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
2345 return check_command(fsg, cmnd_size, data_dir,
2346 mask, needs_medium, name);
2347}
2348
2349static int do_scsi_command(struct fsg_dev *fsg)
2350{
2351 struct fsg_buffhd *bh;
2352 int rc;
2353 int reply = -EINVAL;
2354 int i;
2355 static char unknown[16];
2356
2357 dump_cdb(fsg);
2358
2359 /* Wait for the next buffer to become available for data or status */
2360 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2361 while (bh->state != BUF_STATE_EMPTY) {
2362 rc = sleep_thread(fsg);
2363 if (rc)
2364 return rc;
2365 }
2366 fsg->phase_error = 0;
2367 fsg->short_packet_received = 0;
2368
2369 down_read(&fsg->filesem); // We're using the backing file
2370 switch (fsg->cmnd[0]) {
2371
2372 case INQUIRY:
2373 fsg->data_size_from_cmnd = fsg->cmnd[4];
2374 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2375 (1<<4), 0,
2376 "INQUIRY")) == 0)
2377 reply = do_inquiry(fsg, bh);
2378 break;
2379
2380 case MODE_SELECT:
2381 fsg->data_size_from_cmnd = fsg->cmnd[4];
2382 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2383 (1<<1) | (1<<4), 0,
2384 "MODE SELECT(6)")) == 0)
2385 reply = do_mode_select(fsg, bh);
2386 break;
2387
2388 case MODE_SELECT_10:
2389 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2390 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2391 (1<<1) | (3<<7), 0,
2392 "MODE SELECT(10)")) == 0)
2393 reply = do_mode_select(fsg, bh);
2394 break;
2395
2396 case MODE_SENSE:
2397 fsg->data_size_from_cmnd = fsg->cmnd[4];
2398 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2399 (1<<1) | (1<<2) | (1<<4), 0,
2400 "MODE SENSE(6)")) == 0)
2401 reply = do_mode_sense(fsg, bh);
2402 break;
2403
2404 case MODE_SENSE_10:
2405 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2406 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2407 (1<<1) | (1<<2) | (3<<7), 0,
2408 "MODE SENSE(10)")) == 0)
2409 reply = do_mode_sense(fsg, bh);
2410 break;
2411
2412 case ALLOW_MEDIUM_REMOVAL:
2413 fsg->data_size_from_cmnd = 0;
2414 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2415 (1<<4), 0,
2416 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2417 reply = do_prevent_allow(fsg);
2418 break;
2419
2420 case READ_6:
2421 i = fsg->cmnd[4];
2422 fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
2423 if ((reply = check_command_size_in_blocks(fsg, 6,
2424 DATA_DIR_TO_HOST,
2425 (7<<1) | (1<<4), 1,
2426 "READ(6)")) == 0)
2427 reply = do_read(fsg);
2428 break;
2429
2430 case READ_10:
2431 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2432 if ((reply = check_command_size_in_blocks(fsg, 10,
2433 DATA_DIR_TO_HOST,
2434 (1<<1) | (0xf<<2) | (3<<7), 1,
2435 "READ(10)")) == 0)
2436 reply = do_read(fsg);
2437 break;
2438
2439 case READ_12:
2440 fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
2441 if ((reply = check_command_size_in_blocks(fsg, 12,
2442 DATA_DIR_TO_HOST,
2443 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2444 "READ(12)")) == 0)
2445 reply = do_read(fsg);
2446 break;
2447
2448 case READ_CAPACITY:
2449 fsg->data_size_from_cmnd = 8;
2450 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2451 (0xf<<2) | (1<<8), 1,
2452 "READ CAPACITY")) == 0)
2453 reply = do_read_capacity(fsg, bh);
2454 break;
2455
2456 case READ_HEADER:
2457 if (!mod_data.cdrom)
2458 goto unknown_cmnd;
2459 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2460 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2461 (3<<7) | (0x1f<<1), 1,
2462 "READ HEADER")) == 0)
2463 reply = do_read_header(fsg, bh);
2464 break;
2465
2466 case READ_TOC:
2467 if (!mod_data.cdrom)
2468 goto unknown_cmnd;
2469 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2470 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2471 (7<<6) | (1<<1), 1,
2472 "READ TOC")) == 0)
2473 reply = do_read_toc(fsg, bh);
2474 break;
2475
2476 case READ_FORMAT_CAPACITIES:
2477 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2478 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2479 (3<<7), 1,
2480 "READ FORMAT CAPACITIES")) == 0)
2481 reply = do_read_format_capacities(fsg, bh);
2482 break;
2483
2484 case REQUEST_SENSE:
2485 fsg->data_size_from_cmnd = fsg->cmnd[4];
2486 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2487 (1<<4), 0,
2488 "REQUEST SENSE")) == 0)
2489 reply = do_request_sense(fsg, bh);
2490 break;
2491
2492 case START_STOP:
2493 fsg->data_size_from_cmnd = 0;
2494 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2495 (1<<1) | (1<<4), 0,
2496 "START-STOP UNIT")) == 0)
2497 reply = do_start_stop(fsg);
2498 break;
2499
2500 case SYNCHRONIZE_CACHE:
2501 fsg->data_size_from_cmnd = 0;
2502 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2503 (0xf<<2) | (3<<7), 1,
2504 "SYNCHRONIZE CACHE")) == 0)
2505 reply = do_synchronize_cache(fsg);
2506 break;
2507
2508 case TEST_UNIT_READY:
2509 fsg->data_size_from_cmnd = 0;
2510 reply = check_command(fsg, 6, DATA_DIR_NONE,
2511 0, 1,
2512 "TEST UNIT READY");
2513 break;
2514
2515 /* Although optional, this command is used by MS-Windows. We
2516 * support a minimal version: BytChk must be 0. */
2517 case VERIFY:
2518 fsg->data_size_from_cmnd = 0;
2519 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2520 (1<<1) | (0xf<<2) | (3<<7), 1,
2521 "VERIFY")) == 0)
2522 reply = do_verify(fsg);
2523 break;
2524
2525 case WRITE_6:
2526 i = fsg->cmnd[4];
2527 fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
2528 if ((reply = check_command_size_in_blocks(fsg, 6,
2529 DATA_DIR_FROM_HOST,
2530 (7<<1) | (1<<4), 1,
2531 "WRITE(6)")) == 0)
2532 reply = do_write(fsg);
2533 break;
2534
2535 case WRITE_10:
2536 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2537 if ((reply = check_command_size_in_blocks(fsg, 10,
2538 DATA_DIR_FROM_HOST,
2539 (1<<1) | (0xf<<2) | (3<<7), 1,
2540 "WRITE(10)")) == 0)
2541 reply = do_write(fsg);
2542 break;
2543
2544 case WRITE_12:
2545 fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
2546 if ((reply = check_command_size_in_blocks(fsg, 12,
2547 DATA_DIR_FROM_HOST,
2548 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2549 "WRITE(12)")) == 0)
2550 reply = do_write(fsg);
2551 break;
2552
2553 /* Some mandatory commands that we recognize but don't implement.
2554 * They don't mean much in this setting. It's left as an exercise
2555 * for anyone interested to implement RESERVE and RELEASE in terms
2556 * of Posix locks. */
2557 case FORMAT_UNIT:
2558 case RELEASE:
2559 case RESERVE:
2560 case SEND_DIAGNOSTIC:
2561 // Fall through
2562
2563 default:
2564 unknown_cmnd:
2565 fsg->data_size_from_cmnd = 0;
2566 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2567 if ((reply = check_command(fsg, fsg->cmnd_size,
2568 DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
2569 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2570 reply = -EINVAL;
2571 }
2572 break;
2573 }
2574 up_read(&fsg->filesem);
2575
2576 if (reply == -EINTR || signal_pending(current))
2577 return -EINTR;
2578
2579 /* Set up the single reply buffer for finish_reply() */
2580 if (reply == -EINVAL)
2581 reply = 0; // Error reply length
2582 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2583 reply = min((u32) reply, fsg->data_size_from_cmnd);
2584 bh->inreq->length = reply;
2585 bh->state = BUF_STATE_FULL;
2586 fsg->residue -= reply;
2587 } // Otherwise it's already set
2588
2589 return 0;
2590}
2591
2592
2593/*-------------------------------------------------------------------------*/
2594
2595static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2596{
2597 struct usb_request *req = bh->outreq;
2598 struct bulk_cb_wrap *cbw = req->buf;
2599
2600 /* Was this a real packet? Should it be ignored? */
2601 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2602 return -EINVAL;
2603
2604 /* Is the CBW valid? */
2605 if (req->actual != US_BULK_CB_WRAP_LEN ||
2606 cbw->Signature != cpu_to_le32(
2607 US_BULK_CB_SIGN)) {
2608 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2609 req->actual,
2610 le32_to_cpu(cbw->Signature));
2611
2612 /* The Bulk-only spec says we MUST stall the IN endpoint
2613 * (6.6.1), so it's unavoidable. It also says we must
2614 * retain this state until the next reset, but there's
2615 * no way to tell the controller driver it should ignore
2616 * Clear-Feature(HALT) requests.
2617 *
2618 * We aren't required to halt the OUT endpoint; instead
2619 * we can simply accept and discard any data received
2620 * until the next reset. */
2621 wedge_bulk_in_endpoint(fsg);
2622 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2623 return -EINVAL;
2624 }
2625
2626 /* Is the CBW meaningful? */
2627 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
2628 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2629 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2630 "cmdlen %u\n",
2631 cbw->Lun, cbw->Flags, cbw->Length);
2632
2633 /* We can do anything we want here, so let's stall the
2634 * bulk pipes if we are allowed to. */
2635 if (mod_data.can_stall) {
2636 fsg_set_halt(fsg, fsg->bulk_out);
2637 halt_bulk_in_endpoint(fsg);
2638 }
2639 return -EINVAL;
2640 }
2641
2642 /* Save the command for later */
2643 fsg->cmnd_size = cbw->Length;
2644 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2645 if (cbw->Flags & US_BULK_FLAG_IN)
2646 fsg->data_dir = DATA_DIR_TO_HOST;
2647 else
2648 fsg->data_dir = DATA_DIR_FROM_HOST;
2649 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2650 if (fsg->data_size == 0)
2651 fsg->data_dir = DATA_DIR_NONE;
2652 fsg->lun = cbw->Lun;
2653 fsg->tag = cbw->Tag;
2654 return 0;
2655}
2656
2657
2658static int get_next_command(struct fsg_dev *fsg)
2659{
2660 struct fsg_buffhd *bh;
2661 int rc = 0;
2662
2663 if (transport_is_bbb()) {
2664
2665 /* Wait for the next buffer to become available */
2666 bh = fsg->next_buffhd_to_fill;
2667 while (bh->state != BUF_STATE_EMPTY) {
2668 rc = sleep_thread(fsg);
2669 if (rc)
2670 return rc;
2671 }
2672
2673 /* Queue a request to read a Bulk-only CBW */
2674 set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
2675 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2676 &bh->outreq_busy, &bh->state);
2677
2678 /* We will drain the buffer in software, which means we
2679 * can reuse it for the next filling. No need to advance
2680 * next_buffhd_to_fill. */
2681
2682 /* Wait for the CBW to arrive */
2683 while (bh->state != BUF_STATE_FULL) {
2684 rc = sleep_thread(fsg);
2685 if (rc)
2686 return rc;
2687 }
2688 smp_rmb();
2689 rc = received_cbw(fsg, bh);
2690 bh->state = BUF_STATE_EMPTY;
2691
2692 } else { // USB_PR_CB or USB_PR_CBI
2693
2694 /* Wait for the next command to arrive */
2695 while (fsg->cbbuf_cmnd_size == 0) {
2696 rc = sleep_thread(fsg);
2697 if (rc)
2698 return rc;
2699 }
2700
2701 /* Is the previous status interrupt request still busy?
2702 * The host is allowed to skip reading the status,
2703 * so we must cancel it. */
2704 if (fsg->intreq_busy)
2705 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2706
2707 /* Copy the command and mark the buffer empty */
2708 fsg->data_dir = DATA_DIR_UNKNOWN;
2709 spin_lock_irq(&fsg->lock);
2710 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2711 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2712 fsg->cbbuf_cmnd_size = 0;
2713 spin_unlock_irq(&fsg->lock);
2714
2715 /* Use LUN from the command */
2716 fsg->lun = fsg->cmnd[1] >> 5;
2717 }
2718
2719 /* Update current lun */
2720 if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
2721 fsg->curlun = &fsg->luns[fsg->lun];
2722 else
2723 fsg->curlun = NULL;
2724
2725 return rc;
2726}
2727
2728
2729/*-------------------------------------------------------------------------*/
2730
2731static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2732 const struct usb_endpoint_descriptor *d)
2733{
2734 int rc;
2735
2736 ep->driver_data = fsg;
2737 ep->desc = d;
2738 rc = usb_ep_enable(ep);
2739 if (rc)
2740 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2741 return rc;
2742}
2743
2744static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2745 struct usb_request **preq)
2746{
2747 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2748 if (*preq)
2749 return 0;
2750 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2751 return -ENOMEM;
2752}
2753
2754/*
2755 * Reset interface setting and re-init endpoint state (toggle etc).
2756 * Call with altsetting < 0 to disable the interface. The only other
2757 * available altsetting is 0, which enables the interface.
2758 */
2759static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2760{
2761 int rc = 0;
2762 int i;
2763 const struct usb_endpoint_descriptor *d;
2764
2765 if (fsg->running)
2766 DBG(fsg, "reset interface\n");
2767
2768reset:
2769 /* Deallocate the requests */
2770 for (i = 0; i < fsg_num_buffers; ++i) {
2771 struct fsg_buffhd *bh = &fsg->buffhds[i];
2772
2773 if (bh->inreq) {
2774 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2775 bh->inreq = NULL;
2776 }
2777 if (bh->outreq) {
2778 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2779 bh->outreq = NULL;
2780 }
2781 }
2782 if (fsg->intreq) {
2783 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2784 fsg->intreq = NULL;
2785 }
2786
2787 /* Disable the endpoints */
2788 if (fsg->bulk_in_enabled) {
2789 usb_ep_disable(fsg->bulk_in);
2790 fsg->bulk_in_enabled = 0;
2791 }
2792 if (fsg->bulk_out_enabled) {
2793 usb_ep_disable(fsg->bulk_out);
2794 fsg->bulk_out_enabled = 0;
2795 }
2796 if (fsg->intr_in_enabled) {
2797 usb_ep_disable(fsg->intr_in);
2798 fsg->intr_in_enabled = 0;
2799 }
2800
2801 fsg->running = 0;
2802 if (altsetting < 0 || rc != 0)
2803 return rc;
2804
2805 DBG(fsg, "set interface %d\n", altsetting);
2806
2807 /* Enable the endpoints */
2808 d = fsg_ep_desc(fsg->gadget,
2809 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
2810 &fsg_ss_bulk_in_desc);
2811 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2812 goto reset;
2813 fsg->bulk_in_enabled = 1;
2814
2815 d = fsg_ep_desc(fsg->gadget,
2816 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
2817 &fsg_ss_bulk_out_desc);
2818 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2819 goto reset;
2820 fsg->bulk_out_enabled = 1;
2821 fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
2822 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2823
2824 if (transport_is_cbi()) {
2825 d = fsg_ep_desc(fsg->gadget,
2826 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
2827 &fsg_ss_intr_in_desc);
2828 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2829 goto reset;
2830 fsg->intr_in_enabled = 1;
2831 }
2832
2833 /* Allocate the requests */
2834 for (i = 0; i < fsg_num_buffers; ++i) {
2835 struct fsg_buffhd *bh = &fsg->buffhds[i];
2836
2837 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2838 goto reset;
2839 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2840 goto reset;
2841 bh->inreq->buf = bh->outreq->buf = bh->buf;
2842 bh->inreq->context = bh->outreq->context = bh;
2843 bh->inreq->complete = bulk_in_complete;
2844 bh->outreq->complete = bulk_out_complete;
2845 }
2846 if (transport_is_cbi()) {
2847 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2848 goto reset;
2849 fsg->intreq->complete = intr_in_complete;
2850 }
2851
2852 fsg->running = 1;
2853 for (i = 0; i < fsg->nluns; ++i)
2854 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2855 return rc;
2856}
2857
2858
2859/*
2860 * Change our operational configuration. This code must agree with the code
2861 * that returns config descriptors, and with interface altsetting code.
2862 *
2863 * It's also responsible for power management interactions. Some
2864 * configurations might not work with our current power sources.
2865 * For now we just assume the gadget is always self-powered.
2866 */
2867static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2868{
2869 int rc = 0;
2870
2871 /* Disable the single interface */
2872 if (fsg->config != 0) {
2873 DBG(fsg, "reset config\n");
2874 fsg->config = 0;
2875 rc = do_set_interface(fsg, -1);
2876 }
2877
2878 /* Enable the interface */
2879 if (new_config != 0) {
2880 fsg->config = new_config;
2881 if ((rc = do_set_interface(fsg, 0)) != 0)
2882 fsg->config = 0; // Reset on errors
2883 else
2884 INFO(fsg, "%s config #%d\n",
2885 usb_speed_string(fsg->gadget->speed),
2886 fsg->config);
2887 }
2888 return rc;
2889}
2890
2891
2892/*-------------------------------------------------------------------------*/
2893
2894static void handle_exception(struct fsg_dev *fsg)
2895{
2896 siginfo_t info;
2897 int sig;
2898 int i;
2899 int num_active;
2900 struct fsg_buffhd *bh;
2901 enum fsg_state old_state;
2902 u8 new_config;
2903 struct fsg_lun *curlun;
2904 unsigned int exception_req_tag;
2905 int rc;
2906
2907 /* Clear the existing signals. Anything but SIGUSR1 is converted
2908 * into a high-priority EXIT exception. */
2909 for (;;) {
2910 sig = dequeue_signal_lock(current, &current->blocked, &info);
2911 if (!sig)
2912 break;
2913 if (sig != SIGUSR1) {
2914 if (fsg->state < FSG_STATE_EXIT)
2915 DBG(fsg, "Main thread exiting on signal\n");
2916 raise_exception(fsg, FSG_STATE_EXIT);
2917 }
2918 }
2919
2920 /* Cancel all the pending transfers */
2921 if (fsg->intreq_busy)
2922 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2923 for (i = 0; i < fsg_num_buffers; ++i) {
2924 bh = &fsg->buffhds[i];
2925 if (bh->inreq_busy)
2926 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2927 if (bh->outreq_busy)
2928 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2929 }
2930
2931 /* Wait until everything is idle */
2932 for (;;) {
2933 num_active = fsg->intreq_busy;
2934 for (i = 0; i < fsg_num_buffers; ++i) {
2935 bh = &fsg->buffhds[i];
2936 num_active += bh->inreq_busy + bh->outreq_busy;
2937 }
2938 if (num_active == 0)
2939 break;
2940 if (sleep_thread(fsg))
2941 return;
2942 }
2943
2944 /* Clear out the controller's fifos */
2945 if (fsg->bulk_in_enabled)
2946 usb_ep_fifo_flush(fsg->bulk_in);
2947 if (fsg->bulk_out_enabled)
2948 usb_ep_fifo_flush(fsg->bulk_out);
2949 if (fsg->intr_in_enabled)
2950 usb_ep_fifo_flush(fsg->intr_in);
2951
2952 /* Reset the I/O buffer states and pointers, the SCSI
2953 * state, and the exception. Then invoke the handler. */
2954 spin_lock_irq(&fsg->lock);
2955
2956 for (i = 0; i < fsg_num_buffers; ++i) {
2957 bh = &fsg->buffhds[i];
2958 bh->state = BUF_STATE_EMPTY;
2959 }
2960 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2961 &fsg->buffhds[0];
2962
2963 exception_req_tag = fsg->exception_req_tag;
2964 new_config = fsg->new_config;
2965 old_state = fsg->state;
2966
2967 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2968 fsg->state = FSG_STATE_STATUS_PHASE;
2969 else {
2970 for (i = 0; i < fsg->nluns; ++i) {
2971 curlun = &fsg->luns[i];
2972 curlun->prevent_medium_removal = 0;
2973 curlun->sense_data = curlun->unit_attention_data =
2974 SS_NO_SENSE;
2975 curlun->sense_data_info = 0;
2976 curlun->info_valid = 0;
2977 }
2978 fsg->state = FSG_STATE_IDLE;
2979 }
2980 spin_unlock_irq(&fsg->lock);
2981
2982 /* Carry out any extra actions required for the exception */
2983 switch (old_state) {
2984 default:
2985 break;
2986
2987 case FSG_STATE_ABORT_BULK_OUT:
2988 send_status(fsg);
2989 spin_lock_irq(&fsg->lock);
2990 if (fsg->state == FSG_STATE_STATUS_PHASE)
2991 fsg->state = FSG_STATE_IDLE;
2992 spin_unlock_irq(&fsg->lock);
2993 break;
2994
2995 case FSG_STATE_RESET:
2996 /* In case we were forced against our will to halt a
2997 * bulk endpoint, clear the halt now. (The SuperH UDC
2998 * requires this.) */
2999 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3000 usb_ep_clear_halt(fsg->bulk_in);
3001
3002 if (transport_is_bbb()) {
3003 if (fsg->ep0_req_tag == exception_req_tag)
3004 ep0_queue(fsg); // Complete the status stage
3005
3006 } else if (transport_is_cbi())
3007 send_status(fsg); // Status by interrupt pipe
3008
3009 /* Technically this should go here, but it would only be
3010 * a waste of time. Ditto for the INTERFACE_CHANGE and
3011 * CONFIG_CHANGE cases. */
3012 // for (i = 0; i < fsg->nluns; ++i)
3013 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3014 break;
3015
3016 case FSG_STATE_INTERFACE_CHANGE:
3017 rc = do_set_interface(fsg, 0);
3018 if (fsg->ep0_req_tag != exception_req_tag)
3019 break;
3020 if (rc != 0) // STALL on errors
3021 fsg_set_halt(fsg, fsg->ep0);
3022 else // Complete the status stage
3023 ep0_queue(fsg);
3024 break;
3025
3026 case FSG_STATE_CONFIG_CHANGE:
3027 rc = do_set_config(fsg, new_config);
3028 if (fsg->ep0_req_tag != exception_req_tag)
3029 break;
3030 if (rc != 0) // STALL on errors
3031 fsg_set_halt(fsg, fsg->ep0);
3032 else // Complete the status stage
3033 ep0_queue(fsg);
3034 break;
3035
3036 case FSG_STATE_DISCONNECT:
3037 for (i = 0; i < fsg->nluns; ++i)
3038 fsg_lun_fsync_sub(fsg->luns + i);
3039 do_set_config(fsg, 0); // Unconfigured state
3040 break;
3041
3042 case FSG_STATE_EXIT:
3043 case FSG_STATE_TERMINATED:
3044 do_set_config(fsg, 0); // Free resources
3045 spin_lock_irq(&fsg->lock);
3046 fsg->state = FSG_STATE_TERMINATED; // Stop the thread
3047 spin_unlock_irq(&fsg->lock);
3048 break;
3049 }
3050}
3051
3052
3053/*-------------------------------------------------------------------------*/
3054
3055static int fsg_main_thread(void *fsg_)
3056{
3057 struct fsg_dev *fsg = fsg_;
3058
3059 /* Allow the thread to be killed by a signal, but set the signal mask
3060 * to block everything but INT, TERM, KILL, and USR1. */
3061 allow_signal(SIGINT);
3062 allow_signal(SIGTERM);
3063 allow_signal(SIGKILL);
3064 allow_signal(SIGUSR1);
3065
3066 /* Allow the thread to be frozen */
3067 set_freezable();
3068
3069 /* Arrange for userspace references to be interpreted as kernel
3070 * pointers. That way we can pass a kernel pointer to a routine
3071 * that expects a __user pointer and it will work okay. */
3072 set_fs(get_ds());
3073
3074 /* The main loop */
3075 while (fsg->state != FSG_STATE_TERMINATED) {
3076 if (exception_in_progress(fsg) || signal_pending(current)) {
3077 handle_exception(fsg);
3078 continue;
3079 }
3080
3081 if (!fsg->running) {
3082 sleep_thread(fsg);
3083 continue;
3084 }
3085
3086 if (get_next_command(fsg))
3087 continue;
3088
3089 spin_lock_irq(&fsg->lock);
3090 if (!exception_in_progress(fsg))
3091 fsg->state = FSG_STATE_DATA_PHASE;
3092 spin_unlock_irq(&fsg->lock);
3093
3094 if (do_scsi_command(fsg) || finish_reply(fsg))
3095 continue;
3096
3097 spin_lock_irq(&fsg->lock);
3098 if (!exception_in_progress(fsg))
3099 fsg->state = FSG_STATE_STATUS_PHASE;
3100 spin_unlock_irq(&fsg->lock);
3101
3102 if (send_status(fsg))
3103 continue;
3104
3105 spin_lock_irq(&fsg->lock);
3106 if (!exception_in_progress(fsg))
3107 fsg->state = FSG_STATE_IDLE;
3108 spin_unlock_irq(&fsg->lock);
3109 }
3110
3111 spin_lock_irq(&fsg->lock);
3112 fsg->thread_task = NULL;
3113 spin_unlock_irq(&fsg->lock);
3114
3115 /* If we are exiting because of a signal, unregister the
3116 * gadget driver. */
3117 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3118 usb_gadget_unregister_driver(&fsg_driver);
3119
3120 /* Let the unbind and cleanup routines know the thread has exited */
3121 complete_and_exit(&fsg->thread_notifier, 0);
3122}
3123
3124
3125/*-------------------------------------------------------------------------*/
3126
3127
3128/* The write permissions and store_xxx pointers are set in fsg_bind() */
3129static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
3130static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
3131static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
3132
3133
3134/*-------------------------------------------------------------------------*/
3135
3136static void fsg_release(struct kref *ref)
3137{
3138 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
3139
3140 kfree(fsg->luns);
3141 kfree(fsg);
3142}
3143
3144static void lun_release(struct device *dev)
3145{
3146 struct rw_semaphore *filesem = dev_get_drvdata(dev);
3147 struct fsg_dev *fsg =
3148 container_of(filesem, struct fsg_dev, filesem);
3149
3150 kref_put(&fsg->ref, fsg_release);
3151}
3152
3153static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
3154{
3155 struct fsg_dev *fsg = get_gadget_data(gadget);
3156 int i;
3157 struct fsg_lun *curlun;
3158 struct usb_request *req = fsg->ep0req;
3159
3160 DBG(fsg, "unbind\n");
3161 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3162
3163 /* If the thread isn't already dead, tell it to exit now */
3164 if (fsg->state != FSG_STATE_TERMINATED) {
3165 raise_exception(fsg, FSG_STATE_EXIT);
3166 wait_for_completion(&fsg->thread_notifier);
3167
3168 /* The cleanup routine waits for this completion also */
3169 complete(&fsg->thread_notifier);
3170 }
3171
3172 /* Unregister the sysfs attribute files and the LUNs */
3173 for (i = 0; i < fsg->nluns; ++i) {
3174 curlun = &fsg->luns[i];
3175 if (curlun->registered) {
3176 device_remove_file(&curlun->dev, &dev_attr_nofua);
3177 device_remove_file(&curlun->dev, &dev_attr_ro);
3178 device_remove_file(&curlun->dev, &dev_attr_file);
3179 fsg_lun_close(curlun);
3180 device_unregister(&curlun->dev);
3181 curlun->registered = 0;
3182 }
3183 }
3184
3185 /* Free the data buffers */
3186 for (i = 0; i < fsg_num_buffers; ++i)
3187 kfree(fsg->buffhds[i].buf);
3188
3189 /* Free the request and buffer for endpoint 0 */
3190 if (req) {
3191 kfree(req->buf);
3192 usb_ep_free_request(fsg->ep0, req);
3193 }
3194
3195 set_gadget_data(gadget, NULL);
3196}
3197
3198
3199static int __init check_parameters(struct fsg_dev *fsg)
3200{
3201 int prot;
3202
3203 /* Store the default values */
3204 mod_data.transport_type = USB_PR_BULK;
3205 mod_data.transport_name = "Bulk-only";
3206 mod_data.protocol_type = USB_SC_SCSI;
3207 mod_data.protocol_name = "Transparent SCSI";
3208
3209 /* Some peripheral controllers are known not to be able to
3210 * halt bulk endpoints correctly. If one of them is present,
3211 * disable stalls.
3212 */
3213 if (gadget_is_at91(fsg->gadget))
3214 mod_data.can_stall = 0;
3215
3216 if (mod_data.release == 0xffff)
3217 mod_data.release = get_default_bcdDevice();
3218
3219 prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
3220
3221#ifdef CONFIG_USB_FILE_STORAGE_TEST
3222 if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
3223 ; // Use default setting
3224 } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
3225 mod_data.transport_type = USB_PR_CB;
3226 mod_data.transport_name = "Control-Bulk";
3227 } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
3228 mod_data.transport_type = USB_PR_CBI;
3229 mod_data.transport_name = "Control-Bulk-Interrupt";
3230 } else {
3231 ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
3232 return -EINVAL;
3233 }
3234
3235 if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
3236 prot == USB_SC_SCSI) {
3237 ; // Use default setting
3238 } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
3239 prot == USB_SC_RBC) {
3240 mod_data.protocol_type = USB_SC_RBC;
3241 mod_data.protocol_name = "RBC";
3242 } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
3243 strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
3244 prot == USB_SC_8020) {
3245 mod_data.protocol_type = USB_SC_8020;
3246 mod_data.protocol_name = "8020i (ATAPI)";
3247 } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
3248 prot == USB_SC_QIC) {
3249 mod_data.protocol_type = USB_SC_QIC;
3250 mod_data.protocol_name = "QIC-157";
3251 } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
3252 prot == USB_SC_UFI) {
3253 mod_data.protocol_type = USB_SC_UFI;
3254 mod_data.protocol_name = "UFI";
3255 } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
3256 prot == USB_SC_8070) {
3257 mod_data.protocol_type = USB_SC_8070;
3258 mod_data.protocol_name = "8070i";
3259 } else {
3260 ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
3261 return -EINVAL;
3262 }
3263
3264 mod_data.buflen &= PAGE_CACHE_MASK;
3265 if (mod_data.buflen <= 0) {
3266 ERROR(fsg, "invalid buflen\n");
3267 return -ETOOSMALL;
3268 }
3269
3270#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3271
3272 /* Serial string handling.
3273 * On a real device, the serial string would be loaded
3274 * from permanent storage. */
3275 if (mod_data.serial) {
3276 const char *ch;
3277 unsigned len = 0;
3278
3279 /* Sanity check :
3280 * The CB[I] specification limits the serial string to
3281 * 12 uppercase hexadecimal characters.
3282 * BBB need at least 12 uppercase hexadecimal characters,
3283 * with a maximum of 126. */
3284 for (ch = mod_data.serial; *ch; ++ch) {
3285 ++len;
3286 if ((*ch < '0' || *ch > '9') &&
3287 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3288 WARNING(fsg,
3289 "Invalid serial string character: %c\n",
3290 *ch);
3291 goto no_serial;
3292 }
3293 }
3294 if (len > 126 ||
3295 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3296 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
3297 WARNING(fsg, "Invalid serial string length!\n");
3298 goto no_serial;
3299 }
3300 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
3301 } else {
3302 WARNING(fsg, "No serial-number string provided!\n");
3303 no_serial:
3304 device_desc.iSerialNumber = 0;
3305 }
3306
3307 return 0;
3308}
3309
3310
3311static int __init fsg_bind(struct usb_gadget *gadget,
3312 struct usb_gadget_driver *driver)
3313{
3314 struct fsg_dev *fsg = the_fsg;
3315 int rc;
3316 int i;
3317 struct fsg_lun *curlun;
3318 struct usb_ep *ep;
3319 struct usb_request *req;
3320 char *pathbuf, *p;
3321
3322 fsg->gadget = gadget;
3323 set_gadget_data(gadget, fsg);
3324 fsg->ep0 = gadget->ep0;
3325 fsg->ep0->driver_data = fsg;
3326
3327 if ((rc = check_parameters(fsg)) != 0)
3328 goto out;
3329
3330 if (mod_data.removable) { // Enable the store_xxx attributes
3331 dev_attr_file.attr.mode = 0644;
3332 dev_attr_file.store = fsg_store_file;
3333 if (!mod_data.cdrom) {
3334 dev_attr_ro.attr.mode = 0644;
3335 dev_attr_ro.store = fsg_store_ro;
3336 }
3337 }
3338
3339 /* Only for removable media? */
3340 dev_attr_nofua.attr.mode = 0644;
3341 dev_attr_nofua.store = fsg_store_nofua;
3342
3343 /* Find out how many LUNs there should be */
3344 i = mod_data.nluns;
3345 if (i == 0)
3346 i = max(mod_data.num_filenames, 1u);
3347 if (i > FSG_MAX_LUNS) {
3348 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3349 rc = -EINVAL;
3350 goto out;
3351 }
3352
3353 /* Create the LUNs, open their backing files, and register the
3354 * LUN devices in sysfs. */
3355 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
3356 if (!fsg->luns) {
3357 rc = -ENOMEM;
3358 goto out;
3359 }
3360 fsg->nluns = i;
3361
3362 for (i = 0; i < fsg->nluns; ++i) {
3363 curlun = &fsg->luns[i];
3364 curlun->cdrom = !!mod_data.cdrom;
3365 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3366 curlun->initially_ro = curlun->ro;
3367 curlun->removable = mod_data.removable;
3368 curlun->nofua = mod_data.nofua[i];
3369 curlun->dev.release = lun_release;
3370 curlun->dev.parent = &gadget->dev;
3371 curlun->dev.driver = &fsg_driver.driver;
3372 dev_set_drvdata(&curlun->dev, &fsg->filesem);
3373 dev_set_name(&curlun->dev,"%s-lun%d",
3374 dev_name(&gadget->dev), i);
3375
3376 kref_get(&fsg->ref);
3377 rc = device_register(&curlun->dev);
3378 if (rc) {
3379 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
3380 put_device(&curlun->dev);
3381 goto out;
3382 }
3383 curlun->registered = 1;
3384
3385 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3386 if (rc)
3387 goto out;
3388 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3389 if (rc)
3390 goto out;
3391 rc = device_create_file(&curlun->dev, &dev_attr_file);
3392 if (rc)
3393 goto out;
3394
3395 if (mod_data.file[i] && *mod_data.file[i]) {
3396 rc = fsg_lun_open(curlun, mod_data.file[i]);
3397 if (rc)
3398 goto out;
3399 } else if (!mod_data.removable) {
3400 ERROR(fsg, "no file given for LUN%d\n", i);
3401 rc = -EINVAL;
3402 goto out;
3403 }
3404 }
3405
3406 /* Find all the endpoints we will use */
3407 usb_ep_autoconfig_reset(gadget);
3408 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
3409 if (!ep)
3410 goto autoconf_fail;
3411 ep->driver_data = fsg; // claim the endpoint
3412 fsg->bulk_in = ep;
3413
3414 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
3415 if (!ep)
3416 goto autoconf_fail;
3417 ep->driver_data = fsg; // claim the endpoint
3418 fsg->bulk_out = ep;
3419
3420 if (transport_is_cbi()) {
3421 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
3422 if (!ep)
3423 goto autoconf_fail;
3424 ep->driver_data = fsg; // claim the endpoint
3425 fsg->intr_in = ep;
3426 }
3427
3428 /* Fix up the descriptors */
3429 device_desc.idVendor = cpu_to_le16(mod_data.vendor);
3430 device_desc.idProduct = cpu_to_le16(mod_data.product);
3431 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
3432
3433 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
3434 fsg_intf_desc.bNumEndpoints = i;
3435 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
3436 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
3437 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
3438
3439 if (gadget_is_dualspeed(gadget)) {
3440 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
3441
3442 /* Assume endpoint addresses are the same for both speeds */
3443 fsg_hs_bulk_in_desc.bEndpointAddress =
3444 fsg_fs_bulk_in_desc.bEndpointAddress;
3445 fsg_hs_bulk_out_desc.bEndpointAddress =
3446 fsg_fs_bulk_out_desc.bEndpointAddress;
3447 fsg_hs_intr_in_desc.bEndpointAddress =
3448 fsg_fs_intr_in_desc.bEndpointAddress;
3449 }
3450
3451 if (gadget_is_superspeed(gadget)) {
3452 unsigned max_burst;
3453
3454 fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
3455
3456 /* Calculate bMaxBurst, we know packet size is 1024 */
3457 max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
3458
3459 /* Assume endpoint addresses are the same for both speeds */
3460 fsg_ss_bulk_in_desc.bEndpointAddress =
3461 fsg_fs_bulk_in_desc.bEndpointAddress;
3462 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
3463
3464 fsg_ss_bulk_out_desc.bEndpointAddress =
3465 fsg_fs_bulk_out_desc.bEndpointAddress;
3466 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
3467 }
3468
3469 if (gadget_is_otg(gadget))
3470 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
3471
3472 rc = -ENOMEM;
3473
3474 /* Allocate the request and buffer for endpoint 0 */
3475 fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
3476 if (!req)
3477 goto out;
3478 req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
3479 if (!req->buf)
3480 goto out;
3481 req->complete = ep0_complete;
3482
3483 /* Allocate the data buffers */
3484 for (i = 0; i < fsg_num_buffers; ++i) {
3485 struct fsg_buffhd *bh = &fsg->buffhds[i];
3486
3487 /* Allocate for the bulk-in endpoint. We assume that
3488 * the buffer will also work with the bulk-out (and
3489 * interrupt-in) endpoint. */
3490 bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
3491 if (!bh->buf)
3492 goto out;
3493 bh->next = bh + 1;
3494 }
3495 fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
3496
3497 /* This should reflect the actual gadget power source */
3498 usb_gadget_set_selfpowered(gadget);
3499
3500 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3501 "%s %s with %s",
3502 init_utsname()->sysname, init_utsname()->release,
3503 gadget->name);
3504
3505 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3506 "file-storage-gadget");
3507 if (IS_ERR(fsg->thread_task)) {
3508 rc = PTR_ERR(fsg->thread_task);
3509 goto out;
3510 }
3511
3512 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
3513 INFO(fsg, "NOTE: This driver is deprecated. "
3514 "Consider using g_mass_storage instead.\n");
3515 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
3516
3517 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3518 for (i = 0; i < fsg->nluns; ++i) {
3519 curlun = &fsg->luns[i];
3520 if (fsg_lun_is_open(curlun)) {
3521 p = NULL;
3522 if (pathbuf) {
3523 p = d_path(&curlun->filp->f_path,
3524 pathbuf, PATH_MAX);
3525 if (IS_ERR(p))
3526 p = NULL;
3527 }
3528 LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
3529 curlun->ro, curlun->nofua, (p ? p : "(error)"));
3530 }
3531 }
3532 kfree(pathbuf);
3533
3534 DBG(fsg, "transport=%s (x%02x)\n",
3535 mod_data.transport_name, mod_data.transport_type);
3536 DBG(fsg, "protocol=%s (x%02x)\n",
3537 mod_data.protocol_name, mod_data.protocol_type);
3538 DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3539 mod_data.vendor, mod_data.product, mod_data.release);
3540 DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
3541 mod_data.removable, mod_data.can_stall,
3542 mod_data.cdrom, mod_data.buflen);
3543 DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
3544
3545 set_bit(REGISTERED, &fsg->atomic_bitflags);
3546
3547 /* Tell the thread to start working */
3548 wake_up_process(fsg->thread_task);
3549 return 0;
3550
3551autoconf_fail:
3552 ERROR(fsg, "unable to autoconfigure all endpoints\n");
3553 rc = -ENOTSUPP;
3554
3555out:
3556 fsg->state = FSG_STATE_TERMINATED; // The thread is dead
3557 fsg_unbind(gadget);
3558 complete(&fsg->thread_notifier);
3559 return rc;
3560}
3561
3562
3563/*-------------------------------------------------------------------------*/
3564
3565static void fsg_suspend(struct usb_gadget *gadget)
3566{
3567 struct fsg_dev *fsg = get_gadget_data(gadget);
3568
3569 DBG(fsg, "suspend\n");
3570 set_bit(SUSPENDED, &fsg->atomic_bitflags);
3571}
3572
3573static void fsg_resume(struct usb_gadget *gadget)
3574{
3575 struct fsg_dev *fsg = get_gadget_data(gadget);
3576
3577 DBG(fsg, "resume\n");
3578 clear_bit(SUSPENDED, &fsg->atomic_bitflags);
3579}
3580
3581
3582/*-------------------------------------------------------------------------*/
3583
3584static __refdata struct usb_gadget_driver fsg_driver = {
3585 .max_speed = USB_SPEED_SUPER,
3586 .function = (char *) fsg_string_product,
3587 .bind = fsg_bind,
3588 .unbind = fsg_unbind,
3589 .disconnect = fsg_disconnect,
3590 .setup = fsg_setup,
3591 .suspend = fsg_suspend,
3592 .resume = fsg_resume,
3593
3594 .driver = {
3595 .name = DRIVER_NAME,
3596 .owner = THIS_MODULE,
3597 // .release = ...
3598 // .suspend = ...
3599 // .resume = ...
3600 },
3601};
3602
3603
3604static int __init fsg_alloc(void)
3605{
3606 struct fsg_dev *fsg;
3607
3608 fsg = kzalloc(sizeof *fsg +
3609 fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
3610
3611 if (!fsg)
3612 return -ENOMEM;
3613 spin_lock_init(&fsg->lock);
3614 init_rwsem(&fsg->filesem);
3615 kref_init(&fsg->ref);
3616 init_completion(&fsg->thread_notifier);
3617
3618 the_fsg = fsg;
3619 return 0;
3620}
3621
3622
3623static int __init fsg_init(void)
3624{
3625 int rc;
3626 struct fsg_dev *fsg;
3627
3628 rc = fsg_num_buffers_validate();
3629 if (rc != 0)
3630 return rc;
3631
3632 if ((rc = fsg_alloc()) != 0)
3633 return rc;
3634 fsg = the_fsg;
3635 rc = usb_gadget_probe_driver(&fsg_driver);
3636 if (rc != 0)
3637 kref_put(&fsg->ref, fsg_release);
3638 return rc;
3639}
3640module_init(fsg_init);
3641
3642
3643static void __exit fsg_cleanup(void)
3644{
3645 struct fsg_dev *fsg = the_fsg;
3646
3647 /* Unregister the driver iff the thread hasn't already done so */
3648 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3649 usb_gadget_unregister_driver(&fsg_driver);
3650
3651 /* Wait for the thread to finish up */
3652 wait_for_completion(&fsg->thread_notifier);
3653
3654 kref_put(&fsg->ref, fsg_release);
3655}
3656module_exit(fsg_cleanup);
diff --git a/drivers/usb/gadget/fsl_qe_udc.c b/drivers/usb/gadget/fsl_qe_udc.c
index b09452d6f33a..ec50f18c8890 100644
--- a/drivers/usb/gadget/fsl_qe_udc.c
+++ b/drivers/usb/gadget/fsl_qe_udc.c
@@ -2347,7 +2347,7 @@ static int fsl_qe_stop(struct usb_gadget *gadget,
2347} 2347}
2348 2348
2349/* udc structure's alloc and setup, include ep-param alloc */ 2349/* udc structure's alloc and setup, include ep-param alloc */
2350static struct qe_udc __devinit *qe_udc_config(struct platform_device *ofdev) 2350static struct qe_udc *qe_udc_config(struct platform_device *ofdev)
2351{ 2351{
2352 struct qe_udc *udc; 2352 struct qe_udc *udc;
2353 struct device_node *np = ofdev->dev.of_node; 2353 struct device_node *np = ofdev->dev.of_node;
@@ -2402,7 +2402,7 @@ cleanup:
2402} 2402}
2403 2403
2404/* USB Controller register init */ 2404/* USB Controller register init */
2405static int __devinit qe_udc_reg_init(struct qe_udc *udc) 2405static int qe_udc_reg_init(struct qe_udc *udc)
2406{ 2406{
2407 struct usb_ctlr __iomem *qe_usbregs; 2407 struct usb_ctlr __iomem *qe_usbregs;
2408 qe_usbregs = udc->usb_regs; 2408 qe_usbregs = udc->usb_regs;
@@ -2420,7 +2420,7 @@ static int __devinit qe_udc_reg_init(struct qe_udc *udc)
2420 return 0; 2420 return 0;
2421} 2421}
2422 2422
2423static int __devinit qe_ep_config(struct qe_udc *udc, unsigned char pipe_num) 2423static int qe_ep_config(struct qe_udc *udc, unsigned char pipe_num)
2424{ 2424{
2425 struct qe_ep *ep = &udc->eps[pipe_num]; 2425 struct qe_ep *ep = &udc->eps[pipe_num];
2426 2426
@@ -2473,7 +2473,7 @@ static void qe_udc_release(struct device *dev)
2473 2473
2474/* Driver probe functions */ 2474/* Driver probe functions */
2475static const struct of_device_id qe_udc_match[]; 2475static const struct of_device_id qe_udc_match[];
2476static int __devinit qe_udc_probe(struct platform_device *ofdev) 2476static int qe_udc_probe(struct platform_device *ofdev)
2477{ 2477{
2478 struct qe_udc *udc; 2478 struct qe_udc *udc;
2479 const struct of_device_id *match; 2479 const struct of_device_id *match;
@@ -2651,7 +2651,7 @@ static int qe_udc_resume(struct platform_device *dev)
2651} 2651}
2652#endif 2652#endif
2653 2653
2654static int __devexit qe_udc_remove(struct platform_device *ofdev) 2654static int qe_udc_remove(struct platform_device *ofdev)
2655{ 2655{
2656 struct qe_udc *udc = dev_get_drvdata(&ofdev->dev); 2656 struct qe_udc *udc = dev_get_drvdata(&ofdev->dev);
2657 struct qe_ep *ep; 2657 struct qe_ep *ep;
@@ -2710,7 +2710,7 @@ static int __devexit qe_udc_remove(struct platform_device *ofdev)
2710} 2710}
2711 2711
2712/*-------------------------------------------------------------------------*/ 2712/*-------------------------------------------------------------------------*/
2713static const struct of_device_id qe_udc_match[] __devinitconst = { 2713static const struct of_device_id qe_udc_match[] = {
2714 { 2714 {
2715 .compatible = "fsl,mpc8323-qe-usb", 2715 .compatible = "fsl,mpc8323-qe-usb",
2716 .data = (void *)PORT_QE, 2716 .data = (void *)PORT_QE,
@@ -2735,7 +2735,7 @@ static struct platform_driver udc_driver = {
2735 .of_match_table = qe_udc_match, 2735 .of_match_table = qe_udc_match,
2736 }, 2736 },
2737 .probe = qe_udc_probe, 2737 .probe = qe_udc_probe,
2738 .remove = __devexit_p(qe_udc_remove), 2738 .remove = qe_udc_remove,
2739#ifdef CONFIG_PM 2739#ifdef CONFIG_PM
2740 .suspend = qe_udc_suspend, 2740 .suspend = qe_udc_suspend,
2741 .resume = qe_udc_resume, 2741 .resume = qe_udc_resume,
diff --git a/drivers/usb/gadget/fsl_udc_core.c b/drivers/usb/gadget/fsl_udc_core.c
index 6ae70cba0c4a..c19f7f13790b 100644
--- a/drivers/usb/gadget/fsl_udc_core.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -2126,7 +2126,7 @@ static int fsl_proc_read(char *page, char **start, off_t off, int count,
2126 2126
2127 tmp_reg = fsl_readl(&dr_regs->usbintr); 2127 tmp_reg = fsl_readl(&dr_regs->usbintr);
2128 t = scnprintf(next, size, 2128 t = scnprintf(next, size,
2129 "USB Intrrupt Enable Reg:\n" 2129 "USB Interrupt Enable Reg:\n"
2130 "Sleep Enable: %d SOF Received Enable: %d " 2130 "Sleep Enable: %d SOF Received Enable: %d "
2131 "Reset Enable: %d\n" 2131 "Reset Enable: %d\n"
2132 "System Error Enable: %d " 2132 "System Error Enable: %d "
diff --git a/drivers/usb/gadget/hid.c b/drivers/usb/gadget/hid.c
index 74130f6c12c0..c36260ea8bf2 100644
--- a/drivers/usb/gadget/hid.c
+++ b/drivers/usb/gadget/hid.c
@@ -203,7 +203,7 @@ static int __init hidg_plat_driver_probe(struct platform_device *pdev)
203 return 0; 203 return 0;
204} 204}
205 205
206static int __devexit hidg_plat_driver_remove(struct platform_device *pdev) 206static int hidg_plat_driver_remove(struct platform_device *pdev)
207{ 207{
208 struct hidg_func_node *e, *n; 208 struct hidg_func_node *e, *n;
209 209
@@ -229,7 +229,7 @@ static __refdata struct usb_composite_driver hidg_driver = {
229}; 229};
230 230
231static struct platform_driver hidg_plat_driver = { 231static struct platform_driver hidg_plat_driver = {
232 .remove = __devexit_p(hidg_plat_driver_remove), 232 .remove = hidg_plat_driver_remove,
233 .driver = { 233 .driver = {
234 .owner = THIS_MODULE, 234 .owner = THIS_MODULE,
235 .name = "hidg", 235 .name = "hidg",
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 76494cabf4e4..8ac840f25ba9 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -76,7 +76,6 @@ MODULE_LICENSE ("GPL");
76/*----------------------------------------------------------------------*/ 76/*----------------------------------------------------------------------*/
77 77
78#define GADGETFS_MAGIC 0xaee71ee7 78#define GADGETFS_MAGIC 0xaee71ee7
79#define DMA_ADDR_INVALID (~(dma_addr_t)0)
80 79
81/* /dev/gadget/$CHIP represents ep0 and the whole device */ 80/* /dev/gadget/$CHIP represents ep0 and the whole device */
82enum ep0_state { 81enum ep0_state {
@@ -918,7 +917,6 @@ static void clean_req (struct usb_ep *ep, struct usb_request *req)
918 if (req->buf != dev->rbuf) { 917 if (req->buf != dev->rbuf) {
919 kfree(req->buf); 918 kfree(req->buf);
920 req->buf = dev->rbuf; 919 req->buf = dev->rbuf;
921 req->dma = DMA_ADDR_INVALID;
922 } 920 }
923 req->complete = epio_complete; 921 req->complete = epio_complete;
924 dev->setup_out_ready = 0; 922 dev->setup_out_ready = 0;
@@ -1408,7 +1406,6 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1408 dev->setup_abort = 1; 1406 dev->setup_abort = 1;
1409 1407
1410 req->buf = dev->rbuf; 1408 req->buf = dev->rbuf;
1411 req->dma = DMA_ADDR_INVALID;
1412 req->context = NULL; 1409 req->context = NULL;
1413 value = -EOPNOTSUPP; 1410 value = -EOPNOTSUPP;
1414 switch (ctrl->bRequest) { 1411 switch (ctrl->bRequest) {
diff --git a/drivers/usb/gadget/lpc32xx_udc.c b/drivers/usb/gadget/lpc32xx_udc.c
index 21a9861dabf0..dd1c9b1fe528 100644
--- a/drivers/usb/gadget/lpc32xx_udc.c
+++ b/drivers/usb/gadget/lpc32xx_udc.c
@@ -2399,7 +2399,7 @@ static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
2399 2399
2400 if (i < 0) { 2400 if (i < 0) {
2401 /* setup processing failed, force stall */ 2401 /* setup processing failed, force stall */
2402 dev_err(udc->dev, 2402 dev_dbg(udc->dev,
2403 "req %02x.%02x protocol STALL; stat %d\n", 2403 "req %02x.%02x protocol STALL; stat %d\n",
2404 reqtype, req, i); 2404 reqtype, req, i);
2405 udc->ep0state = WAIT_FOR_SETUP; 2405 udc->ep0state = WAIT_FOR_SETUP;
@@ -3352,7 +3352,7 @@ phy_fail:
3352 return retval; 3352 return retval;
3353} 3353}
3354 3354
3355static int __devexit lpc32xx_udc_remove(struct platform_device *pdev) 3355static int lpc32xx_udc_remove(struct platform_device *pdev)
3356{ 3356{
3357 struct lpc32xx_udc *udc = platform_get_drvdata(pdev); 3357 struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
3358 3358
@@ -3447,7 +3447,7 @@ MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
3447#endif 3447#endif
3448 3448
3449static struct platform_driver lpc32xx_udc_driver = { 3449static struct platform_driver lpc32xx_udc_driver = {
3450 .remove = __devexit_p(lpc32xx_udc_remove), 3450 .remove = lpc32xx_udc_remove,
3451 .shutdown = lpc32xx_udc_shutdown, 3451 .shutdown = lpc32xx_udc_shutdown,
3452 .suspend = lpc32xx_udc_suspend, 3452 .suspend = lpc32xx_udc_suspend,
3453 .resume = lpc32xx_udc_resume, 3453 .resume = lpc32xx_udc_resume,
diff --git a/drivers/usb/gadget/mv_u3d_core.c b/drivers/usb/gadget/mv_u3d_core.c
index 8cfd5b028dbd..b5cea273c957 100644
--- a/drivers/usb/gadget/mv_u3d_core.c
+++ b/drivers/usb/gadget/mv_u3d_core.c
@@ -1763,7 +1763,7 @@ static void mv_u3d_gadget_release(struct device *dev)
1763 dev_dbg(dev, "%s\n", __func__); 1763 dev_dbg(dev, "%s\n", __func__);
1764} 1764}
1765 1765
1766static __devexit int mv_u3d_remove(struct platform_device *dev) 1766static int mv_u3d_remove(struct platform_device *dev)
1767{ 1767{
1768 struct mv_u3d *u3d = platform_get_drvdata(dev); 1768 struct mv_u3d *u3d = platform_get_drvdata(dev);
1769 1769
diff --git a/drivers/usb/gadget/mv_udc_core.c b/drivers/usb/gadget/mv_udc_core.c
index ea45224f78c8..379aac7b82fc 100644
--- a/drivers/usb/gadget/mv_udc_core.c
+++ b/drivers/usb/gadget/mv_udc_core.c
@@ -2128,7 +2128,7 @@ static void gadget_release(struct device *_dev)
2128 complete(udc->done); 2128 complete(udc->done);
2129} 2129}
2130 2130
2131static int __devexit mv_udc_remove(struct platform_device *dev) 2131static int mv_udc_remove(struct platform_device *dev)
2132{ 2132{
2133 struct mv_udc *udc = the_controller; 2133 struct mv_udc *udc = the_controller;
2134 int clk_i; 2134 int clk_i;
@@ -2188,7 +2188,7 @@ static int __devexit mv_udc_remove(struct platform_device *dev)
2188 return 0; 2188 return 0;
2189} 2189}
2190 2190
2191static int __devinit mv_udc_probe(struct platform_device *dev) 2191static int mv_udc_probe(struct platform_device *dev)
2192{ 2192{
2193 struct mv_usb_platform_data *pdata = dev->dev.platform_data; 2193 struct mv_usb_platform_data *pdata = dev->dev.platform_data;
2194 struct mv_udc *udc; 2194 struct mv_udc *udc;
diff --git a/drivers/usb/gadget/net2272.c b/drivers/usb/gadget/net2272.c
index c009263a47e3..d226058e3b88 100644
--- a/drivers/usb/gadget/net2272.c
+++ b/drivers/usb/gadget/net2272.c
@@ -2193,7 +2193,7 @@ net2272_gadget_release(struct device *_dev)
2193 2193
2194/*---------------------------------------------------------------------------*/ 2194/*---------------------------------------------------------------------------*/
2195 2195
2196static void __devexit 2196static void
2197net2272_remove(struct net2272 *dev) 2197net2272_remove(struct net2272 *dev)
2198{ 2198{
2199 usb_del_gadget_udc(&dev->gadget); 2199 usb_del_gadget_udc(&dev->gadget);
@@ -2215,8 +2215,7 @@ net2272_remove(struct net2272 *dev)
2215 dev_info(dev->dev, "unbind\n"); 2215 dev_info(dev->dev, "unbind\n");
2216} 2216}
2217 2217
2218static struct net2272 * __devinit 2218static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq)
2219net2272_probe_init(struct device *dev, unsigned int irq)
2220{ 2219{
2221 struct net2272 *ret; 2220 struct net2272 *ret;
2222 2221
@@ -2246,7 +2245,7 @@ net2272_probe_init(struct device *dev, unsigned int irq)
2246 return ret; 2245 return ret;
2247} 2246}
2248 2247
2249static int __devinit 2248static int
2250net2272_probe_fin(struct net2272 *dev, unsigned int irqflags) 2249net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2251{ 2250{
2252 int ret; 2251 int ret;
@@ -2306,7 +2305,7 @@ err_add_udc:
2306 * don't respond over USB until a gadget driver binds to us 2305 * don't respond over USB until a gadget driver binds to us
2307 */ 2306 */
2308 2307
2309static int __devinit 2308static int
2310net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev) 2309net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2311{ 2310{
2312 unsigned long resource, len, tmp; 2311 unsigned long resource, len, tmp;
@@ -2389,7 +2388,7 @@ net2272_rdk1_probe(struct pci_dev *pdev, struct net2272 *dev)
2389 return ret; 2388 return ret;
2390} 2389}
2391 2390
2392static int __devinit 2391static int
2393net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev) 2392net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2394{ 2393{
2395 unsigned long resource, len; 2394 unsigned long resource, len;
@@ -2447,7 +2446,7 @@ net2272_rdk2_probe(struct pci_dev *pdev, struct net2272 *dev)
2447 return ret; 2446 return ret;
2448} 2447}
2449 2448
2450static int __devinit 2449static int
2451net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2450net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2452{ 2451{
2453 struct net2272 *dev; 2452 struct net2272 *dev;
@@ -2489,7 +2488,7 @@ net2272_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2489 return ret; 2488 return ret;
2490} 2489}
2491 2490
2492static void __devexit 2491static void
2493net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev) 2492net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2494{ 2493{
2495 int i; 2494 int i;
@@ -2511,7 +2510,7 @@ net2272_rdk1_remove(struct pci_dev *pdev, struct net2272 *dev)
2511 } 2510 }
2512} 2511}
2513 2512
2514static void __devexit 2513static void
2515net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev) 2514net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2516{ 2515{
2517 int i; 2516 int i;
@@ -2530,7 +2529,7 @@ net2272_rdk2_remove(struct pci_dev *pdev, struct net2272 *dev)
2530 pci_resource_len(pdev, i)); 2529 pci_resource_len(pdev, i));
2531} 2530}
2532 2531
2533static void __devexit 2532static void
2534net2272_pci_remove(struct pci_dev *pdev) 2533net2272_pci_remove(struct pci_dev *pdev)
2535{ 2534{
2536 struct net2272 *dev = pci_get_drvdata(pdev); 2535 struct net2272 *dev = pci_get_drvdata(pdev);
@@ -2549,7 +2548,7 @@ net2272_pci_remove(struct pci_dev *pdev)
2549} 2548}
2550 2549
2551/* Table of matching PCI IDs */ 2550/* Table of matching PCI IDs */
2552static struct pci_device_id __devinitdata pci_ids[] = { 2551static struct pci_device_id pci_ids[] = {
2553 { /* RDK 1 card */ 2552 { /* RDK 1 card */
2554 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe), 2553 .class = ((PCI_CLASS_BRIDGE_OTHER << 8) | 0xfe),
2555 .class_mask = 0, 2554 .class_mask = 0,
@@ -2575,7 +2574,7 @@ static struct pci_driver net2272_pci_driver = {
2575 .id_table = pci_ids, 2574 .id_table = pci_ids,
2576 2575
2577 .probe = net2272_pci_probe, 2576 .probe = net2272_pci_probe,
2578 .remove = __devexit_p(net2272_pci_remove), 2577 .remove = net2272_pci_remove,
2579}; 2578};
2580 2579
2581static int net2272_pci_register(void) 2580static int net2272_pci_register(void)
@@ -2595,7 +2594,7 @@ static inline void net2272_pci_unregister(void) { }
2595 2594
2596/*---------------------------------------------------------------------------*/ 2595/*---------------------------------------------------------------------------*/
2597 2596
2598static int __devinit 2597static int
2599net2272_plat_probe(struct platform_device *pdev) 2598net2272_plat_probe(struct platform_device *pdev)
2600{ 2599{
2601 struct net2272 *dev; 2600 struct net2272 *dev;
@@ -2661,7 +2660,7 @@ net2272_plat_probe(struct platform_device *pdev)
2661 return ret; 2660 return ret;
2662} 2661}
2663 2662
2664static int __devexit 2663static int
2665net2272_plat_remove(struct platform_device *pdev) 2664net2272_plat_remove(struct platform_device *pdev)
2666{ 2665{
2667 struct net2272 *dev = platform_get_drvdata(pdev); 2666 struct net2272 *dev = platform_get_drvdata(pdev);
@@ -2678,7 +2677,7 @@ net2272_plat_remove(struct platform_device *pdev)
2678 2677
2679static struct platform_driver net2272_plat_driver = { 2678static struct platform_driver net2272_plat_driver = {
2680 .probe = net2272_plat_probe, 2679 .probe = net2272_plat_probe,
2681 .remove = __devexit_p(net2272_plat_remove), 2680 .remove = net2272_plat_remove,
2682 .driver = { 2681 .driver = {
2683 .name = driver_name, 2682 .name = driver_name,
2684 .owner = THIS_MODULE, 2683 .owner = THIS_MODULE,
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index ac335af154ba..708c0b55dcc8 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -9,7 +9,7 @@
9 * CODE STATUS HIGHLIGHTS 9 * CODE STATUS HIGHLIGHTS
10 * 10 *
11 * This driver should work well with most "gadget" drivers, including 11 * This driver should work well with most "gadget" drivers, including
12 * the File Storage, Serial, and Ethernet/RNDIS gadget drivers 12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs. 13 * as well as Gadget Zero and Gadgetfs.
14 * 14 *
15 * DMA is enabled by default. Drivers using transfer queues might use 15 * DMA is enabled by default. Drivers using transfer queues might use
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index 2a4749c3eb3f..9412bf53b864 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -2506,7 +2506,7 @@ static inline void remove_proc_file(void) {}
2506 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that 2506 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
2507 * capability yet though. 2507 * capability yet though.
2508 */ 2508 */
2509static unsigned __devinit 2509static unsigned
2510omap_ep_setup(char *name, u8 addr, u8 type, 2510omap_ep_setup(char *name, u8 addr, u8 type,
2511 unsigned buf, unsigned maxp, int dbuf) 2511 unsigned buf, unsigned maxp, int dbuf)
2512{ 2512{
@@ -2624,7 +2624,7 @@ static void omap_udc_release(struct device *dev)
2624 udc = NULL; 2624 udc = NULL;
2625} 2625}
2626 2626
2627static int __devinit 2627static int
2628omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv) 2628omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2629{ 2629{
2630 unsigned tmp, buf; 2630 unsigned tmp, buf;
@@ -2761,7 +2761,7 @@ omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2761 return 0; 2761 return 0;
2762} 2762}
2763 2763
2764static int __devinit omap_udc_probe(struct platform_device *pdev) 2764static int omap_udc_probe(struct platform_device *pdev)
2765{ 2765{
2766 int status = -ENODEV; 2766 int status = -ENODEV;
2767 int hmc; 2767 int hmc;
@@ -2974,7 +2974,7 @@ cleanup0:
2974 return status; 2974 return status;
2975} 2975}
2976 2976
2977static int __devexit omap_udc_remove(struct platform_device *pdev) 2977static int omap_udc_remove(struct platform_device *pdev)
2978{ 2978{
2979 DECLARE_COMPLETION_ONSTACK(done); 2979 DECLARE_COMPLETION_ONSTACK(done);
2980 2980
@@ -3060,7 +3060,7 @@ static int omap_udc_resume(struct platform_device *dev)
3060 3060
3061static struct platform_driver udc_driver = { 3061static struct platform_driver udc_driver = {
3062 .probe = omap_udc_probe, 3062 .probe = omap_udc_probe,
3063 .remove = __devexit_p(omap_udc_remove), 3063 .remove = omap_udc_remove,
3064 .suspend = omap_udc_suspend, 3064 .suspend = omap_udc_suspend,
3065 .resume = omap_udc_resume, 3065 .resume = omap_udc_resume,
3066 .driver = { 3066 .driver = {
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c
index e156e3f26727..35bcc83d1e04 100644
--- a/drivers/usb/gadget/printer.c
+++ b/drivers/usb/gadget/printer.c
@@ -983,8 +983,10 @@ static int __init printer_func_bind(struct usb_configuration *c,
983{ 983{
984 struct printer_dev *dev = container_of(f, struct printer_dev, function); 984 struct printer_dev *dev = container_of(f, struct printer_dev, function);
985 struct usb_composite_dev *cdev = c->cdev; 985 struct usb_composite_dev *cdev = c->cdev;
986 struct usb_ep *in_ep, *out_ep; 986 struct usb_ep *in_ep;
987 struct usb_ep *out_ep = NULL;
987 int id; 988 int id;
989 int ret;
988 990
989 id = usb_interface_id(c, f); 991 id = usb_interface_id(c, f);
990 if (id < 0) 992 if (id < 0)
@@ -1010,6 +1012,11 @@ autoconf_fail:
1010 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; 1012 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1011 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; 1013 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1012 1014
1015 ret = usb_assign_descriptors(f, fs_printer_function,
1016 hs_printer_function, NULL);
1017 if (ret)
1018 return ret;
1019
1013 dev->in_ep = in_ep; 1020 dev->in_ep = in_ep;
1014 dev->out_ep = out_ep; 1021 dev->out_ep = out_ep;
1015 return 0; 1022 return 0;
@@ -1018,6 +1025,7 @@ autoconf_fail:
1018static void printer_func_unbind(struct usb_configuration *c, 1025static void printer_func_unbind(struct usb_configuration *c,
1019 struct usb_function *f) 1026 struct usb_function *f)
1020{ 1027{
1028 usb_free_all_descriptors(f);
1021} 1029}
1022 1030
1023static int printer_func_set_alt(struct usb_function *f, 1031static int printer_func_set_alt(struct usb_function *f,
@@ -1110,8 +1118,6 @@ static int __init printer_bind_config(struct usb_configuration *c)
1110 dev = &usb_printer_gadget; 1118 dev = &usb_printer_gadget;
1111 1119
1112 dev->function.name = shortname; 1120 dev->function.name = shortname;
1113 dev->function.descriptors = fs_printer_function;
1114 dev->function.hs_descriptors = hs_printer_function;
1115 dev->function.bind = printer_func_bind; 1121 dev->function.bind = printer_func_bind;
1116 dev->function.setup = printer_func_setup; 1122 dev->function.setup = printer_func_setup;
1117 dev->function.unbind = printer_func_unbind; 1123 dev->function.unbind = printer_func_unbind;
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index a1d268c6f2cc..79d81a4b2344 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -418,7 +418,7 @@ struct udc_stats {
418 * @irq: udc irq 418 * @irq: udc irq
419 * @clk: udc clock 419 * @clk: udc clock
420 * @usb_gadget: udc gadget structure 420 * @usb_gadget: udc gadget structure
421 * @driver: bound gadget (zero, g_ether, g_file_storage, ...) 421 * @driver: bound gadget (zero, g_ether, g_mass_storage, ...)
422 * @dev: device 422 * @dev: device
423 * @mach: machine info, used to activate specific GPIO 423 * @mach: machine info, used to activate specific GPIO
424 * @transceiver: external transceiver to handle vbus sense and D+ pullup 424 * @transceiver: external transceiver to handle vbus sense and D+ pullup
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 6f696ee8b817..141971d9051e 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -3072,7 +3072,7 @@ static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
3072 * creation) to give to the gadget driver. Setup the endpoint name, any 3072 * creation) to give to the gadget driver. Setup the endpoint name, any
3073 * direction information and other state that may be required. 3073 * direction information and other state that may be required.
3074 */ 3074 */
3075static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg, 3075static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
3076 struct s3c_hsotg_ep *hs_ep, 3076 struct s3c_hsotg_ep *hs_ep,
3077 int epnum) 3077 int epnum)
3078{ 3078{
@@ -3414,7 +3414,7 @@ static const struct file_operations ep_fops = {
3414 * with the same name as the device itself, in case we end up 3414 * with the same name as the device itself, in case we end up
3415 * with multiple blocks in future systems. 3415 * with multiple blocks in future systems.
3416 */ 3416 */
3417static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg) 3417static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3418{ 3418{
3419 struct dentry *root; 3419 struct dentry *root;
3420 unsigned epidx; 3420 unsigned epidx;
@@ -3460,7 +3460,7 @@ static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3460 * 3460 *
3461 * Cleanup (remove) the debugfs files for use on module exit. 3461 * Cleanup (remove) the debugfs files for use on module exit.
3462 */ 3462 */
3463static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg) 3463static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3464{ 3464{
3465 unsigned epidx; 3465 unsigned epidx;
3466 3466
@@ -3490,7 +3490,7 @@ static void s3c_hsotg_release(struct device *dev)
3490 * @pdev: The platform information for the driver 3490 * @pdev: The platform information for the driver
3491 */ 3491 */
3492 3492
3493static int __devinit s3c_hsotg_probe(struct platform_device *pdev) 3493static int s3c_hsotg_probe(struct platform_device *pdev)
3494{ 3494{
3495 struct s3c_hsotg_plat *plat = pdev->dev.platform_data; 3495 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3496 struct device *dev = &pdev->dev; 3496 struct device *dev = &pdev->dev;
@@ -3675,7 +3675,7 @@ err_clk:
3675 * s3c_hsotg_remove - remove function for hsotg driver 3675 * s3c_hsotg_remove - remove function for hsotg driver
3676 * @pdev: The platform information for the driver 3676 * @pdev: The platform information for the driver
3677 */ 3677 */
3678static int __devexit s3c_hsotg_remove(struct platform_device *pdev) 3678static int s3c_hsotg_remove(struct platform_device *pdev)
3679{ 3679{
3680 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev); 3680 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3681 3681
@@ -3708,7 +3708,7 @@ static struct platform_driver s3c_hsotg_driver = {
3708 .owner = THIS_MODULE, 3708 .owner = THIS_MODULE,
3709 }, 3709 },
3710 .probe = s3c_hsotg_probe, 3710 .probe = s3c_hsotg_probe,
3711 .remove = __devexit_p(s3c_hsotg_remove), 3711 .remove = s3c_hsotg_remove,
3712 .suspend = s3c_hsotg_suspend, 3712 .suspend = s3c_hsotg_suspend,
3713 .resume = s3c_hsotg_resume, 3713 .resume = s3c_hsotg_resume,
3714}; 3714};
diff --git a/drivers/usb/gadget/s3c-hsudc.c b/drivers/usb/gadget/s3c-hsudc.c
index d8e785d4ad59..52379b11f080 100644
--- a/drivers/usb/gadget/s3c-hsudc.c
+++ b/drivers/usb/gadget/s3c-hsudc.c
@@ -1261,7 +1261,7 @@ static struct usb_gadget_ops s3c_hsudc_gadget_ops = {
1261 .vbus_draw = s3c_hsudc_vbus_draw, 1261 .vbus_draw = s3c_hsudc_vbus_draw,
1262}; 1262};
1263 1263
1264static int __devinit s3c_hsudc_probe(struct platform_device *pdev) 1264static int s3c_hsudc_probe(struct platform_device *pdev)
1265{ 1265{
1266 struct device *dev = &pdev->dev; 1266 struct device *dev = &pdev->dev;
1267 struct resource *res; 1267 struct resource *res;
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 8d9bcd8207c8..0e3ae43454a2 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -11,30 +11,10 @@
11 * (at your option) any later version. 11 * (at your option) any later version.
12 */ 12 */
13 13
14
15/* 14/*
16 * This file requires the following identifiers used in USB strings to 15 * This file requires the following identifiers used in USB strings to
17 * be defined (each of type pointer to char): 16 * be defined (each of type pointer to char):
18 * - fsg_string_manufacturer -- name of the manufacturer
19 * - fsg_string_product -- name of the product
20 * - fsg_string_config -- name of the configuration
21 * - fsg_string_interface -- name of the interface 17 * - fsg_string_interface -- name of the interface
22 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
23 * macro is defined prior to including this file.
24 */
25
26/*
27 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
28 * fsg_hs_intr_in_desc objects as well as
29 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
30 * macros are not defined.
31 *
32 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
33 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
34 * defined (as well as corresponding entries in string tables are
35 * missing) and FSG_STRING_INTERFACE has value of zero.
36 *
37 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
38 */ 18 */
39 19
40/* 20/*
@@ -78,34 +58,6 @@
78#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) 58#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
79#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) 59#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
80 60
81/*
82 * Keep those macros in sync with those in
83 * include/linux/usb/composite.h or else GCC will complain. If they
84 * are identical (the same names of arguments, white spaces in the
85 * same places) GCC will allow redefinition otherwise (even if some
86 * white space is removed or added) warning will be issued.
87 *
88 * Those macros are needed here because File Storage Gadget does not
89 * include the composite.h header. For composite gadgets those macros
90 * are redundant since composite.h is included any way.
91 *
92 * One could check whether those macros are already defined (which
93 * would indicate composite.h had been included) or not (which would
94 * indicate we were in FSG) but this is not done because a warning is
95 * desired if definitions here differ from the ones in composite.h.
96 *
97 * We want the definitions to match and be the same in File Storage
98 * Gadget as well as Mass Storage Function (and so composite gadgets
99 * using MSF). If someone changes them in composite.h it will produce
100 * a warning in this file when building MSF.
101 */
102#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
103#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
104#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
105#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
106#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
107
108
109 61
110#ifdef DUMP_MSGS 62#ifdef DUMP_MSGS
111 63
@@ -203,9 +155,12 @@ struct fsg_lun {
203 struct device dev; 155 struct device dev;
204}; 156};
205 157
206#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL) 158static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
159{
160 return curlun->filp != NULL;
161}
207 162
208static struct fsg_lun *fsg_lun_from_dev(struct device *dev) 163static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
209{ 164{
210 return container_of(dev, struct fsg_lun, dev); 165 return container_of(dev, struct fsg_lun, dev);
211} 166}
@@ -308,26 +263,10 @@ static inline u32 get_unaligned_be24(u8 *buf)
308 263
309 264
310enum { 265enum {
311#ifndef FSG_NO_DEVICE_STRINGS
312 FSG_STRING_MANUFACTURER = 1,
313 FSG_STRING_PRODUCT,
314 FSG_STRING_SERIAL,
315 FSG_STRING_CONFIG,
316#endif
317 FSG_STRING_INTERFACE 266 FSG_STRING_INTERFACE
318}; 267};
319 268
320 269
321#ifndef FSG_NO_OTG
322static struct usb_otg_descriptor
323fsg_otg_desc = {
324 .bLength = sizeof fsg_otg_desc,
325 .bDescriptorType = USB_DT_OTG,
326
327 .bmAttributes = USB_OTG_SRP,
328};
329#endif
330
331/* There is only one interface. */ 270/* There is only one interface. */
332 271
333static struct usb_interface_descriptor 272static struct usb_interface_descriptor
@@ -367,37 +306,10 @@ fsg_fs_bulk_out_desc = {
367 /* wMaxPacketSize set by autoconfiguration */ 306 /* wMaxPacketSize set by autoconfiguration */
368}; 307};
369 308
370#ifndef FSG_NO_INTR_EP
371
372static struct usb_endpoint_descriptor
373fsg_fs_intr_in_desc = {
374 .bLength = USB_DT_ENDPOINT_SIZE,
375 .bDescriptorType = USB_DT_ENDPOINT,
376
377 .bEndpointAddress = USB_DIR_IN,
378 .bmAttributes = USB_ENDPOINT_XFER_INT,
379 .wMaxPacketSize = cpu_to_le16(2),
380 .bInterval = 32, /* frames -> 32 ms */
381};
382
383#ifndef FSG_NO_OTG
384# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
385#else
386# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
387#endif
388
389#endif
390
391static struct usb_descriptor_header *fsg_fs_function[] = { 309static struct usb_descriptor_header *fsg_fs_function[] = {
392#ifndef FSG_NO_OTG
393 (struct usb_descriptor_header *) &fsg_otg_desc,
394#endif
395 (struct usb_descriptor_header *) &fsg_intf_desc, 310 (struct usb_descriptor_header *) &fsg_intf_desc,
396 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, 311 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
397 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, 312 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
398#ifndef FSG_NO_INTR_EP
399 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
400#endif
401 NULL, 313 NULL,
402}; 314};
403 315
@@ -431,37 +343,11 @@ fsg_hs_bulk_out_desc = {
431 .bInterval = 1, /* NAK every 1 uframe */ 343 .bInterval = 1, /* NAK every 1 uframe */
432}; 344};
433 345
434#ifndef FSG_NO_INTR_EP
435
436static struct usb_endpoint_descriptor
437fsg_hs_intr_in_desc = {
438 .bLength = USB_DT_ENDPOINT_SIZE,
439 .bDescriptorType = USB_DT_ENDPOINT,
440
441 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
442 .bmAttributes = USB_ENDPOINT_XFER_INT,
443 .wMaxPacketSize = cpu_to_le16(2),
444 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
445};
446
447#ifndef FSG_NO_OTG
448# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
449#else
450# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
451#endif
452
453#endif
454 346
455static struct usb_descriptor_header *fsg_hs_function[] = { 347static struct usb_descriptor_header *fsg_hs_function[] = {
456#ifndef FSG_NO_OTG
457 (struct usb_descriptor_header *) &fsg_otg_desc,
458#endif
459 (struct usb_descriptor_header *) &fsg_intf_desc, 348 (struct usb_descriptor_header *) &fsg_intf_desc,
460 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, 349 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
461 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, 350 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
462#ifndef FSG_NO_INTR_EP
463 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
464#endif
465 NULL, 351 NULL,
466}; 352};
467 353
@@ -499,34 +385,6 @@ static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
499 /*.bMaxBurst = DYNAMIC, */ 385 /*.bMaxBurst = DYNAMIC, */
500}; 386};
501 387
502#ifndef FSG_NO_INTR_EP
503
504static struct usb_endpoint_descriptor
505fsg_ss_intr_in_desc = {
506 .bLength = USB_DT_ENDPOINT_SIZE,
507 .bDescriptorType = USB_DT_ENDPOINT,
508
509 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
510 .bmAttributes = USB_ENDPOINT_XFER_INT,
511 .wMaxPacketSize = cpu_to_le16(2),
512 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
513};
514
515static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = {
516 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
517 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
518
519 .wBytesPerInterval = cpu_to_le16(2),
520};
521
522#ifndef FSG_NO_OTG
523# define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
524#else
525# define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
526#endif
527
528#endif
529
530static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = { 388static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = {
531 .bLength = USB_DT_USB_EXT_CAP_SIZE, 389 .bLength = USB_DT_USB_EXT_CAP_SIZE,
532 .bDescriptorType = USB_DT_DEVICE_CAPABILITY, 390 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
@@ -563,18 +421,11 @@ static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = {
563}; 421};
564 422
565static struct usb_descriptor_header *fsg_ss_function[] = { 423static struct usb_descriptor_header *fsg_ss_function[] = {
566#ifndef FSG_NO_OTG
567 (struct usb_descriptor_header *) &fsg_otg_desc,
568#endif
569 (struct usb_descriptor_header *) &fsg_intf_desc, 424 (struct usb_descriptor_header *) &fsg_intf_desc,
570 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc, 425 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
571 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc, 426 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
572 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc, 427 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
573 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc, 428 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
574#ifndef FSG_NO_INTR_EP
575 (struct usb_descriptor_header *) &fsg_ss_intr_in_desc,
576 (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc,
577#endif
578 NULL, 429 NULL,
579}; 430};
580 431
@@ -594,12 +445,6 @@ fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
594 445
595/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ 446/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
596static struct usb_string fsg_strings[] = { 447static struct usb_string fsg_strings[] = {
597#ifndef FSG_NO_DEVICE_STRINGS
598 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
599 {FSG_STRING_PRODUCT, fsg_string_product},
600 {FSG_STRING_SERIAL, ""},
601 {FSG_STRING_CONFIG, fsg_string_config},
602#endif
603 {FSG_STRING_INTERFACE, fsg_string_interface}, 448 {FSG_STRING_INTERFACE, fsg_string_interface},
604 {} 449 {}
605}; 450};
diff --git a/drivers/usb/gadget/tcm_usb_gadget.c b/drivers/usb/gadget/tcm_usb_gadget.c
index 97e68b38cfdf..4f7f76f00c74 100644
--- a/drivers/usb/gadget/tcm_usb_gadget.c
+++ b/drivers/usb/gadget/tcm_usb_gadget.c
@@ -1384,7 +1384,7 @@ static struct se_node_acl *usbg_alloc_fabric_acl(struct se_portal_group *se_tpg)
1384 1384
1385 nacl = kzalloc(sizeof(struct usbg_nacl), GFP_KERNEL); 1385 nacl = kzalloc(sizeof(struct usbg_nacl), GFP_KERNEL);
1386 if (!nacl) { 1386 if (!nacl) {
1387 printk(KERN_ERR "Unable to alocate struct usbg_nacl\n"); 1387 printk(KERN_ERR "Unable to allocate struct usbg_nacl\n");
1388 return NULL; 1388 return NULL;
1389 } 1389 }
1390 1390
@@ -2139,6 +2139,7 @@ static struct usb_descriptor_header *uasp_fs_function_desc[] = {
2139 (struct usb_descriptor_header *) &uasp_status_pipe_desc, 2139 (struct usb_descriptor_header *) &uasp_status_pipe_desc,
2140 (struct usb_descriptor_header *) &uasp_fs_cmd_desc, 2140 (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
2141 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc, 2141 (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
2142 NULL,
2142}; 2143};
2143 2144
2144static struct usb_descriptor_header *uasp_hs_function_desc[] = { 2145static struct usb_descriptor_header *uasp_hs_function_desc[] = {
@@ -2239,6 +2240,7 @@ static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
2239 struct usb_gadget *gadget = c->cdev->gadget; 2240 struct usb_gadget *gadget = c->cdev->gadget;
2240 struct usb_ep *ep; 2241 struct usb_ep *ep;
2241 int iface; 2242 int iface;
2243 int ret;
2242 2244
2243 iface = usb_interface_id(c, f); 2245 iface = usb_interface_id(c, f);
2244 if (iface < 0) 2246 if (iface < 0)
@@ -2289,6 +2291,11 @@ static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
2289 uasp_ss_status_desc.bEndpointAddress; 2291 uasp_ss_status_desc.bEndpointAddress;
2290 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress; 2292 uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
2291 2293
2294 ret = usb_assign_descriptors(f, uasp_fs_function_desc,
2295 uasp_hs_function_desc, uasp_ss_function_desc);
2296 if (ret)
2297 goto ep_fail;
2298
2292 return 0; 2299 return 0;
2293ep_fail: 2300ep_fail:
2294 pr_err("Can't claim all required eps\n"); 2301 pr_err("Can't claim all required eps\n");
@@ -2304,6 +2311,7 @@ static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
2304{ 2311{
2305 struct f_uas *fu = to_f_uas(f); 2312 struct f_uas *fu = to_f_uas(f);
2306 2313
2314 usb_free_all_descriptors(f);
2307 kfree(fu); 2315 kfree(fu);
2308} 2316}
2309 2317
@@ -2384,9 +2392,6 @@ static int usbg_cfg_bind(struct usb_configuration *c)
2384 if (!fu) 2392 if (!fu)
2385 return -ENOMEM; 2393 return -ENOMEM;
2386 fu->function.name = "Target Function"; 2394 fu->function.name = "Target Function";
2387 fu->function.descriptors = uasp_fs_function_desc;
2388 fu->function.hs_descriptors = uasp_hs_function_desc;
2389 fu->function.ss_descriptors = uasp_ss_function_desc;
2390 fu->function.bind = usbg_bind; 2395 fu->function.bind = usbg_bind;
2391 fu->function.unbind = usbg_unbind; 2396 fu->function.unbind = usbg_unbind;
2392 fu->function.set_alt = usbg_set_alt; 2397 fu->function.set_alt = usbg_set_alt;
diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
index f3cd9690b101..4d90a800063c 100644
--- a/drivers/usb/gadget/udc-core.c
+++ b/drivers/usb/gadget/udc-core.c
@@ -439,16 +439,6 @@ static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
439static USB_UDC_SPEED_ATTR(current_speed, speed); 439static USB_UDC_SPEED_ATTR(current_speed, speed);
440static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); 440static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
441 441
442/* TODO: Scheduled for removal in 3.8. */
443static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
446 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
447 return snprintf(buf, PAGE_SIZE, "%d\n",
448 gadget_is_dualspeed(udc->gadget));
449}
450static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
451
452#define USB_UDC_ATTR(name) \ 442#define USB_UDC_ATTR(name) \
453ssize_t usb_udc_##name##_show(struct device *dev, \ 443ssize_t usb_udc_##name##_show(struct device *dev, \
454 struct device_attribute *attr, char *buf) \ 444 struct device_attribute *attr, char *buf) \
@@ -472,7 +462,6 @@ static struct attribute *usb_udc_attrs[] = {
472 &dev_attr_current_speed.attr, 462 &dev_attr_current_speed.attr,
473 &dev_attr_maximum_speed.attr, 463 &dev_attr_maximum_speed.attr,
474 464
475 &dev_attr_is_dualspeed.attr,
476 &dev_attr_is_otg.attr, 465 &dev_attr_is_otg.attr,
477 &dev_attr_is_a_peripheral.attr, 466 &dev_attr_is_a_peripheral.attr,
478 &dev_attr_b_hnp_enable.attr, 467 &dev_attr_b_hnp_enable.attr,