aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-04-05 18:18:00 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-04-05 18:18:00 -0400
commit64dc9e2e7320f079b97c46b106133b58b8e18d40 (patch)
treeab010dc1337d44e29c2b32b7f11788620a91fe4f /drivers/usb/gadget
parent01a60e76b6392547ad3dca3ac05b9c886fa5da45 (diff)
parent9b192de60b5a584ee4ed967fb6758773c75e4643 (diff)
Merge tag 'usb-for-v3.10' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb into usb-next
Felipe writes: usb: patches for v3.10 merge window Here is the big Gadget & PHY pull request. Many of us have been really busy lately getting multiple drivers to a better position. Since this pull request is so large, I will divide it in sections so it's easier to grasp what's included. - cleanups: . UDC drivers no longer touch gadget->dev, that's now udc-core responsibility . Many more UDC drivers converted to usb_gadget_map/unmap_request() . UDC drivers no longer initialize DMA-related fields from gadget's device structure . UDC drivers don't touch gadget.dev.driver directly . UDC drivers don't assign gadget.dev.release directly . Removal of some unused DMA_ADDR_INVALID . Introduction of CONFIG_USB_PHY . All phy drivers have been moved to drivers/usb/phy and renamed to a common naming scheme . Fix PHY layer so it never returns a NULL pointer, also fix all callers to avoid using IS_ERR_OR_NULL() . Sparse fixes all over the place . drivers/usb/otg/ has been deleted . Marvel drivers (mv_udc, ehci-mv, mv_otg and mv_u3d) improved clock usage - new features: . UDC core now provides a generic way for tracking and reporting UDC's state (not attached, resuming, suspended, addressed, default, etc) . twl4030-usb learned that it shouldn't be enabled during init . Full DT support for DWC3 has been implemented . ab8500-usb learned about pinctrl framework . nop PHY learned about DeviceTree and regulators . DWC3 learned about suspend/resume . DWC3 can now be compiled in host-only and gadget-only (as well as DRD) configurations . UVC now enables streaming endpoint based on negotiated speed . isp1301 now implements the PHY API properly . configfs-based interface for gadget drivers which will lead to the removal of all code which just combines functions together to build functional gadget drivers. . f_serial and f_obex were converted to new configfs interface while maintaining old interface around. - non-critical fixes: . UVC gadget driver got fixes for Endpoint usage and stream calculation . ab8500-usb fixed unbalanced clock and regulator API usage . twl4030-usb got a fix for when OMAP3 is booted with cable connected . fusb300_udc got a fix for DMA usage . UVC got fixes for two assertions of the USB Video Class Compliance specification revision 1.1 . build warning issues caused by recent addition of __must_check to regulator API These are all changes which deserve a mention, all other changes are related to these one or minor spelling fixes and other similar tasks. Signed-of-by: Felipe Balbi <balbi@ti.com>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig18
-rw-r--r--drivers/usb/gadget/Makefile13
-rw-r--r--drivers/usb/gadget/acm_ms.c15
-rw-r--r--drivers/usb/gadget/amd5536udc.c16
-rw-r--r--drivers/usb/gadget/amd5536udc.h1
-rw-r--r--drivers/usb/gadget/at91_udc.c14
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c44
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.h6
-rw-r--r--drivers/usb/gadget/bcm63xx_udc.c23
-rw-r--r--drivers/usb/gadget/cdc2.c27
-rw-r--r--drivers/usb/gadget/composite.c1
-rw-r--r--drivers/usb/gadget/configfs.c1003
-rw-r--r--drivers/usb/gadget/dummy_hcd.c19
-rw-r--r--drivers/usb/gadget/ether.c20
-rw-r--r--drivers/usb/gadget/f_acm.c137
-rw-r--r--drivers/usb/gadget/f_ecm.c4
-rw-r--r--drivers/usb/gadget/f_eem.c3
-rw-r--r--drivers/usb/gadget/f_ncm.c4
-rw-r--r--drivers/usb/gadget/f_obex.c226
-rw-r--r--drivers/usb/gadget/f_rndis.c3
-rw-r--r--drivers/usb/gadget/f_serial.c174
-rw-r--r--drivers/usb/gadget/f_subset.c4
-rw-r--r--drivers/usb/gadget/f_uvc.c262
-rw-r--r--drivers/usb/gadget/f_uvc.h12
-rw-r--r--drivers/usb/gadget/fsl_qe_udc.c18
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c83
-rw-r--r--drivers/usb/gadget/fusb300_udc.c46
-rw-r--r--drivers/usb/gadget/fusb300_udc.h2
-rw-r--r--drivers/usb/gadget/g_ffs.c35
-rw-r--r--drivers/usb/gadget/goku_udc.c20
-rw-r--r--drivers/usb/gadget/goku_udc.h3
-rw-r--r--drivers/usb/gadget/imx_udc.c16
-rw-r--r--drivers/usb/gadget/lpc32xx_udc.c51
-rw-r--r--drivers/usb/gadget/m66592-udc.c17
-rw-r--r--drivers/usb/gadget/multi.c29
-rw-r--r--drivers/usb/gadget/mv_u3d_core.c65
-rw-r--r--drivers/usb/gadget/mv_udc.h3
-rw-r--r--drivers/usb/gadget/mv_udc_core.c117
-rw-r--r--drivers/usb/gadget/ncm.c13
-rw-r--r--drivers/usb/gadget/net2272.c25
-rw-r--r--drivers/usb/gadget/net2280.c25
-rw-r--r--drivers/usb/gadget/nokia.c95
-rw-r--r--drivers/usb/gadget/omap_udc.c21
-rw-r--r--drivers/usb/gadget/pch_udc.c21
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c16
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c51
-rw-r--r--drivers/usb/gadget/r8a66597-udc.c14
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c78
-rw-r--r--drivers/usb/gadget/s3c-hsudc.c16
-rw-r--r--drivers/usb/gadget/s3c2410_udc.c15
-rw-r--r--drivers/usb/gadget/serial.c59
-rw-r--r--drivers/usb/gadget/u_ether.c32
-rw-r--r--drivers/usb/gadget/u_ether.h27
-rw-r--r--drivers/usb/gadget/u_serial.h1
-rw-r--r--drivers/usb/gadget/udc-core.c90
-rw-r--r--drivers/usb/gadget/uvc.h3
-rw-r--r--drivers/usb/gadget/uvc_queue.c538
-rw-r--r--drivers/usb/gadget/uvc_queue.h32
-rw-r--r--drivers/usb/gadget/uvc_v4l2.c71
-rw-r--r--drivers/usb/gadget/uvc_video.c31
60 files changed, 2173 insertions, 1655 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index c7525b1cad74..a61d981cbd15 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -196,7 +196,6 @@ config USB_OMAP
196 tristate "OMAP USB Device Controller" 196 tristate "OMAP USB Device Controller"
197 depends on ARCH_OMAP1 197 depends on ARCH_OMAP1
198 select ISP1301_OMAP if MACH_OMAP_H2 || MACH_OMAP_H3 || MACH_OMAP_H4_OTG 198 select ISP1301_OMAP if MACH_OMAP_H2 || MACH_OMAP_H3 || MACH_OMAP_H4_OTG
199 select USB_OTG_UTILS if ARCH_OMAP
200 help 199 help
201 Many Texas Instruments OMAP processors have flexible full 200 Many Texas Instruments OMAP processors have flexible full
202 speed USB device controllers, with support for up to 30 201 speed USB device controllers, with support for up to 30
@@ -211,7 +210,6 @@ config USB_OMAP
211config USB_PXA25X 210config USB_PXA25X
212 tristate "PXA 25x or IXP 4xx" 211 tristate "PXA 25x or IXP 4xx"
213 depends on (ARCH_PXA && PXA25x) || ARCH_IXP4XX 212 depends on (ARCH_PXA && PXA25x) || ARCH_IXP4XX
214 select USB_OTG_UTILS
215 help 213 help
216 Intel's PXA 25x series XScale ARM-5TE processors include 214 Intel's PXA 25x series XScale ARM-5TE processors include
217 an integrated full speed USB 1.1 device controller. The 215 an integrated full speed USB 1.1 device controller. The
@@ -259,8 +257,6 @@ config USB_RENESAS_USBHS_UDC
259 257
260config USB_PXA27X 258config USB_PXA27X
261 tristate "PXA 27x" 259 tristate "PXA 27x"
262 depends on ARCH_PXA && (PXA27x || PXA3xx)
263 select USB_OTG_UTILS
264 help 260 help
265 Intel's PXA 27x series XScale ARM v5TE processors include 261 Intel's PXA 27x series XScale ARM v5TE processors include
266 an integrated full speed USB 1.1 device controller. 262 an integrated full speed USB 1.1 device controller.
@@ -329,9 +325,6 @@ config USB_MV_UDC
329 325
330config USB_MV_U3D 326config USB_MV_U3D
331 tristate "MARVELL PXA2128 USB 3.0 controller" 327 tristate "MARVELL PXA2128 USB 3.0 controller"
332 depends on CPU_MMP3
333 select USB_GADGET_DUALSPEED
334 select USB_GADGET_SUPERSPEED
335 help 328 help
336 MARVELL PXA2128 Processor series include a super speed USB3.0 device 329 MARVELL PXA2128 Processor series include a super speed USB3.0 device
337 controller, which support super speed USB peripheral. 330 controller, which support super speed USB peripheral.
@@ -501,6 +494,7 @@ endmenu
501# composite based drivers 494# composite based drivers
502config USB_LIBCOMPOSITE 495config USB_LIBCOMPOSITE
503 tristate 496 tristate
497 select CONFIGFS_FS
504 depends on USB_GADGET 498 depends on USB_GADGET
505 499
506config USB_F_ACM 500config USB_F_ACM
@@ -512,6 +506,12 @@ config USB_F_SS_LB
512config USB_U_SERIAL 506config USB_U_SERIAL
513 tristate 507 tristate
514 508
509config USB_F_SERIAL
510 tristate
511
512config USB_F_OBEX
513 tristate
514
515choice 515choice
516 tristate "USB Gadget Drivers" 516 tristate "USB Gadget Drivers"
517 default USB_ETH 517 default USB_ETH
@@ -766,6 +766,8 @@ config USB_G_SERIAL
766 depends on TTY 766 depends on TTY
767 select USB_U_SERIAL 767 select USB_U_SERIAL
768 select USB_F_ACM 768 select USB_F_ACM
769 select USB_F_SERIAL
770 select USB_F_OBEX
769 select USB_LIBCOMPOSITE 771 select USB_LIBCOMPOSITE
770 help 772 help
771 The Serial Gadget talks to the Linux-USB generic serial driver. 773 The Serial Gadget talks to the Linux-USB generic serial driver.
@@ -839,6 +841,7 @@ config USB_G_NOKIA
839 depends on PHONET 841 depends on PHONET
840 select USB_LIBCOMPOSITE 842 select USB_LIBCOMPOSITE
841 select USB_U_SERIAL 843 select USB_U_SERIAL
844 select USB_F_ACM
842 help 845 help
843 The Nokia composite gadget provides support for acm, obex 846 The Nokia composite gadget provides support for acm, obex
844 and phonet in only one composite gadget driver. 847 and phonet in only one composite gadget driver.
@@ -957,6 +960,7 @@ config USB_G_WEBCAM
957 tristate "USB Webcam Gadget" 960 tristate "USB Webcam Gadget"
958 depends on VIDEO_DEV 961 depends on VIDEO_DEV
959 select USB_LIBCOMPOSITE 962 select USB_LIBCOMPOSITE
963 select VIDEOBUF2_VMALLOC
960 help 964 help
961 The Webcam Gadget acts as a composite USB Audio and Video Class 965 The Webcam Gadget acts as a composite USB Audio and Video Class
962 device. It provides a userspace API to process UVC control requests 966 device. It provides a userspace API to process UVC control requests
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 82fb22511356..6afd16659e78 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -6,7 +6,7 @@ ccflags-$(CONFIG_USB_GADGET_DEBUG) := -DDEBUG
6obj-$(CONFIG_USB_GADGET) += udc-core.o 6obj-$(CONFIG_USB_GADGET) += udc-core.o
7obj-$(CONFIG_USB_LIBCOMPOSITE) += libcomposite.o 7obj-$(CONFIG_USB_LIBCOMPOSITE) += libcomposite.o
8libcomposite-y := usbstring.o config.o epautoconf.o 8libcomposite-y := usbstring.o config.o epautoconf.o
9libcomposite-y += composite.o functions.o 9libcomposite-y += composite.o functions.o configfs.o
10obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o 10obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o
11obj-$(CONFIG_USB_NET2272) += net2272.o 11obj-$(CONFIG_USB_NET2272) += net2272.o
12obj-$(CONFIG_USB_NET2280) += net2280.o 12obj-$(CONFIG_USB_NET2280) += net2280.o
@@ -36,10 +36,15 @@ obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
36obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o 36obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o
37 37
38# USB Functions 38# USB Functions
39obj-$(CONFIG_USB_F_ACM) += f_acm.o 39usb_f_acm-y := f_acm.o
40f_ss_lb-y := f_loopback.o f_sourcesink.o 40obj-$(CONFIG_USB_F_ACM) += usb_f_acm.o
41obj-$(CONFIG_USB_F_SS_LB) += f_ss_lb.o 41usb_f_ss_lb-y := f_loopback.o f_sourcesink.o
42obj-$(CONFIG_USB_F_SS_LB) += usb_f_ss_lb.o
42obj-$(CONFIG_USB_U_SERIAL) += u_serial.o 43obj-$(CONFIG_USB_U_SERIAL) += u_serial.o
44usb_f_serial-y := f_serial.o
45obj-$(CONFIG_USB_F_SERIAL) += usb_f_serial.o
46usb_f_obex-y := f_obex.o
47obj-$(CONFIG_USB_F_OBEX) += usb_f_obex.o
43 48
44# 49#
45# USB gadget drivers 50# USB gadget drivers
diff --git a/drivers/usb/gadget/acm_ms.c b/drivers/usb/gadget/acm_ms.c
index 8f2b0e391534..4b947bb50f62 100644
--- a/drivers/usb/gadget/acm_ms.c
+++ b/drivers/usb/gadget/acm_ms.c
@@ -109,7 +109,6 @@ FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
109static struct fsg_common fsg_common; 109static struct fsg_common fsg_common;
110 110
111/*-------------------------------------------------------------------------*/ 111/*-------------------------------------------------------------------------*/
112static unsigned char tty_line;
113static struct usb_function *f_acm; 112static struct usb_function *f_acm;
114static struct usb_function_instance *f_acm_inst; 113static struct usb_function_instance *f_acm_inst;
115/* 114/*
@@ -117,7 +116,6 @@ static struct usb_function_instance *f_acm_inst;
117 */ 116 */
118static int __init acm_ms_do_config(struct usb_configuration *c) 117static int __init acm_ms_do_config(struct usb_configuration *c)
119{ 118{
120 struct f_serial_opts *opts;
121 int status; 119 int status;
122 120
123 if (gadget_is_otg(c->cdev->gadget)) { 121 if (gadget_is_otg(c->cdev->gadget)) {
@@ -129,9 +127,6 @@ static int __init acm_ms_do_config(struct usb_configuration *c)
129 if (IS_ERR(f_acm_inst)) 127 if (IS_ERR(f_acm_inst))
130 return PTR_ERR(f_acm_inst); 128 return PTR_ERR(f_acm_inst);
131 129
132 opts = container_of(f_acm_inst, struct f_serial_opts, func_inst);
133 opts->port_num = tty_line;
134
135 f_acm = usb_get_function(f_acm_inst); 130 f_acm = usb_get_function(f_acm_inst);
136 if (IS_ERR(f_acm)) { 131 if (IS_ERR(f_acm)) {
137 status = PTR_ERR(f_acm); 132 status = PTR_ERR(f_acm);
@@ -171,16 +166,11 @@ static int __init acm_ms_bind(struct usb_composite_dev *cdev)
171 int status; 166 int status;
172 void *retp; 167 void *retp;
173 168
174 /* set up serial link layer */
175 status = gserial_alloc_line(&tty_line);
176 if (status < 0)
177 return status;
178
179 /* set up mass storage function */ 169 /* set up mass storage function */
180 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data); 170 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
181 if (IS_ERR(retp)) { 171 if (IS_ERR(retp)) {
182 status = PTR_ERR(retp); 172 status = PTR_ERR(retp);
183 goto fail0; 173 return PTR_ERR(retp);
184 } 174 }
185 175
186 /* 176 /*
@@ -207,8 +197,6 @@ static int __init acm_ms_bind(struct usb_composite_dev *cdev)
207 /* error recovery */ 197 /* error recovery */
208fail1: 198fail1:
209 fsg_common_put(&fsg_common); 199 fsg_common_put(&fsg_common);
210fail0:
211 gserial_free_line(tty_line);
212 return status; 200 return status;
213} 201}
214 202
@@ -216,7 +204,6 @@ static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
216{ 204{
217 usb_put_function(f_acm); 205 usb_put_function(f_acm);
218 usb_put_function_instance(f_acm_inst); 206 usb_put_function_instance(f_acm_inst);
219 gserial_free_line(tty_line);
220 return 0; 207 return 0;
221} 208}
222 209
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index 75973f33a4c8..f52dcfe8f545 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -1922,7 +1922,6 @@ static int amd5536_udc_start(struct usb_gadget *g,
1922 1922
1923 driver->driver.bus = NULL; 1923 driver->driver.bus = NULL;
1924 dev->driver = driver; 1924 dev->driver = driver;
1925 dev->gadget.dev.driver = &driver->driver;
1926 1925
1927 /* Some gadget drivers use both ep0 directions. 1926 /* Some gadget drivers use both ep0 directions.
1928 * NOTE: to gadget driver, ep0 is just one endpoint... 1927 * NOTE: to gadget driver, ep0 is just one endpoint...
@@ -1973,7 +1972,6 @@ static int amd5536_udc_stop(struct usb_gadget *g,
1973 shutdown(dev, driver); 1972 shutdown(dev, driver);
1974 spin_unlock_irqrestore(&dev->lock, flags); 1973 spin_unlock_irqrestore(&dev->lock, flags);
1975 1974
1976 dev->gadget.dev.driver = NULL;
1977 dev->driver = NULL; 1975 dev->driver = NULL;
1978 1976
1979 /* set SD */ 1977 /* set SD */
@@ -3080,7 +3078,6 @@ static void udc_pci_remove(struct pci_dev *pdev)
3080 if (dev->active) 3078 if (dev->active)
3081 pci_disable_device(pdev); 3079 pci_disable_device(pdev);
3082 3080
3083 device_unregister(&dev->gadget.dev);
3084 pci_set_drvdata(pdev, NULL); 3081 pci_set_drvdata(pdev, NULL);
3085 3082
3086 udc_remove(dev); 3083 udc_remove(dev);
@@ -3245,8 +3242,6 @@ static int udc_pci_probe(
3245 dev->phys_addr = resource; 3242 dev->phys_addr = resource;
3246 dev->irq = pdev->irq; 3243 dev->irq = pdev->irq;
3247 dev->pdev = pdev; 3244 dev->pdev = pdev;
3248 dev->gadget.dev.parent = &pdev->dev;
3249 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
3250 3245
3251 /* general probing */ 3246 /* general probing */
3252 if (udc_probe(dev) == 0) 3247 if (udc_probe(dev) == 0)
@@ -3273,7 +3268,6 @@ static int udc_probe(struct udc *dev)
3273 dev->gadget.ops = &udc_ops; 3268 dev->gadget.ops = &udc_ops;
3274 3269
3275 dev_set_name(&dev->gadget.dev, "gadget"); 3270 dev_set_name(&dev->gadget.dev, "gadget");
3276 dev->gadget.dev.release = gadget_release;
3277 dev->gadget.name = name; 3271 dev->gadget.name = name;
3278 dev->gadget.max_speed = USB_SPEED_HIGH; 3272 dev->gadget.max_speed = USB_SPEED_HIGH;
3279 3273
@@ -3297,17 +3291,11 @@ static int udc_probe(struct udc *dev)
3297 "driver version: %s(for Geode5536 B1)\n", tmp); 3291 "driver version: %s(for Geode5536 B1)\n", tmp);
3298 udc = dev; 3292 udc = dev;
3299 3293
3300 retval = usb_add_gadget_udc(&udc->pdev->dev, &dev->gadget); 3294 retval = usb_add_gadget_udc_release(&udc->pdev->dev, &dev->gadget,
3295 gadget_release);
3301 if (retval) 3296 if (retval)
3302 goto finished; 3297 goto finished;
3303 3298
3304 retval = device_register(&dev->gadget.dev);
3305 if (retval) {
3306 usb_del_gadget_udc(&dev->gadget);
3307 put_device(&dev->gadget.dev);
3308 goto finished;
3309 }
3310
3311 /* timer init */ 3299 /* timer init */
3312 init_timer(&udc_timer); 3300 init_timer(&udc_timer);
3313 udc_timer.function = udc_timer_function; 3301 udc_timer.function = udc_timer_function;
diff --git a/drivers/usb/gadget/amd5536udc.h b/drivers/usb/gadget/amd5536udc.h
index f1bf32e6b8d8..6744d3b83109 100644
--- a/drivers/usb/gadget/amd5536udc.h
+++ b/drivers/usb/gadget/amd5536udc.h
@@ -472,7 +472,6 @@ struct udc_request {
472 472
473 /* flags */ 473 /* flags */
474 unsigned dma_going : 1, 474 unsigned dma_going : 1,
475 dma_mapping : 1,
476 dma_done : 1; 475 dma_done : 1;
477 /* phys. address */ 476 /* phys. address */
478 dma_addr_t td_phys; 477 dma_addr_t td_phys;
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 45dd2929a671..a690d64217f4 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -1631,7 +1631,6 @@ static int at91_start(struct usb_gadget *gadget,
1631 1631
1632 udc = container_of(gadget, struct at91_udc, gadget); 1632 udc = container_of(gadget, struct at91_udc, gadget);
1633 udc->driver = driver; 1633 udc->driver = driver;
1634 udc->gadget.dev.driver = &driver->driver;
1635 udc->gadget.dev.of_node = udc->pdev->dev.of_node; 1634 udc->gadget.dev.of_node = udc->pdev->dev.of_node;
1636 udc->enabled = 1; 1635 udc->enabled = 1;
1637 udc->selfpowered = 1; 1636 udc->selfpowered = 1;
@@ -1652,7 +1651,6 @@ static int at91_stop(struct usb_gadget *gadget,
1652 at91_udp_write(udc, AT91_UDP_IDR, ~0); 1651 at91_udp_write(udc, AT91_UDP_IDR, ~0);
1653 spin_unlock_irqrestore(&udc->lock, flags); 1652 spin_unlock_irqrestore(&udc->lock, flags);
1654 1653
1655 udc->gadget.dev.driver = NULL;
1656 udc->driver = NULL; 1654 udc->driver = NULL;
1657 1655
1658 DBG("unbound from %s\n", driver->driver.name); 1656 DBG("unbound from %s\n", driver->driver.name);
@@ -1780,13 +1778,7 @@ static int at91udc_probe(struct platform_device *pdev)
1780 DBG("clocks missing\n"); 1778 DBG("clocks missing\n");
1781 retval = -ENODEV; 1779 retval = -ENODEV;
1782 /* NOTE: we "know" here that refcounts on these are NOPs */ 1780 /* NOTE: we "know" here that refcounts on these are NOPs */
1783 goto fail0b; 1781 goto fail1;
1784 }
1785
1786 retval = device_register(&udc->gadget.dev);
1787 if (retval < 0) {
1788 put_device(&udc->gadget.dev);
1789 goto fail0b;
1790 } 1782 }
1791 1783
1792 /* don't do anything until we have both gadget driver and VBUS */ 1784 /* don't do anything until we have both gadget driver and VBUS */
@@ -1857,8 +1849,6 @@ fail3:
1857fail2: 1849fail2:
1858 free_irq(udc->udp_irq, udc); 1850 free_irq(udc->udp_irq, udc);
1859fail1: 1851fail1:
1860 device_unregister(&udc->gadget.dev);
1861fail0b:
1862 iounmap(udc->udp_baseaddr); 1852 iounmap(udc->udp_baseaddr);
1863fail0a: 1853fail0a:
1864 if (cpu_is_at91rm9200()) 1854 if (cpu_is_at91rm9200())
@@ -1892,8 +1882,6 @@ static int __exit at91udc_remove(struct platform_device *pdev)
1892 gpio_free(udc->board.vbus_pin); 1882 gpio_free(udc->board.vbus_pin);
1893 } 1883 }
1894 free_irq(udc->udp_irq, udc); 1884 free_irq(udc->udp_irq, udc);
1895 device_unregister(&udc->gadget.dev);
1896
1897 iounmap(udc->udp_baseaddr); 1885 iounmap(udc->udp_baseaddr);
1898 1886
1899 if (cpu_is_at91rm9200()) 1887 if (cpu_is_at91rm9200())
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index b66130c97269..f2a970f75bfa 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -489,13 +489,8 @@ request_complete(struct usba_ep *ep, struct usba_request *req, int status)
489 if (req->req.status == -EINPROGRESS) 489 if (req->req.status == -EINPROGRESS)
490 req->req.status = status; 490 req->req.status = status;
491 491
492 if (req->mapped) { 492 if (req->using_dma)
493 dma_unmap_single( 493 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
494 &udc->pdev->dev, req->req.dma, req->req.length,
495 ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
496 req->req.dma = DMA_ADDR_INVALID;
497 req->mapped = 0;
498 }
499 494
500 DBG(DBG_GADGET | DBG_REQ, 495 DBG(DBG_GADGET | DBG_REQ,
501 "%s: req %p complete: status %d, actual %u\n", 496 "%s: req %p complete: status %d, actual %u\n",
@@ -684,7 +679,6 @@ usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
684 return NULL; 679 return NULL;
685 680
686 INIT_LIST_HEAD(&req->queue); 681 INIT_LIST_HEAD(&req->queue);
687 req->req.dma = DMA_ADDR_INVALID;
688 682
689 return &req->req; 683 return &req->req;
690} 684}
@@ -717,20 +711,11 @@ static int queue_dma(struct usba_udc *udc, struct usba_ep *ep,
717 return -EINVAL; 711 return -EINVAL;
718 } 712 }
719 713
720 req->using_dma = 1; 714 ret = usb_gadget_map_request(&udc->gadget, &req->req, ep->is_in);
721 715 if (ret)
722 if (req->req.dma == DMA_ADDR_INVALID) { 716 return ret;
723 req->req.dma = dma_map_single(
724 &udc->pdev->dev, req->req.buf, req->req.length,
725 ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
726 req->mapped = 1;
727 } else {
728 dma_sync_single_for_device(
729 &udc->pdev->dev, req->req.dma, req->req.length,
730 ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
731 req->mapped = 0;
732 }
733 717
718 req->using_dma = 1;
734 req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length) 719 req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length)
735 | USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE 720 | USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE
736 | USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE; 721 | USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE;
@@ -1799,7 +1784,6 @@ static int atmel_usba_start(struct usb_gadget *gadget,
1799 1784
1800 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED; 1785 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
1801 udc->driver = driver; 1786 udc->driver = driver;
1802 udc->gadget.dev.driver = &driver->driver;
1803 spin_unlock_irqrestore(&udc->lock, flags); 1787 spin_unlock_irqrestore(&udc->lock, flags);
1804 1788
1805 clk_enable(udc->pclk); 1789 clk_enable(udc->pclk);
@@ -1841,7 +1825,6 @@ static int atmel_usba_stop(struct usb_gadget *gadget,
1841 toggle_bias(0); 1825 toggle_bias(0);
1842 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1826 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1843 1827
1844 udc->gadget.dev.driver = NULL;
1845 udc->driver = NULL; 1828 udc->driver = NULL;
1846 1829
1847 clk_disable(udc->hclk); 1830 clk_disable(udc->hclk);
@@ -1900,10 +1883,6 @@ static int __init usba_udc_probe(struct platform_device *pdev)
1900 dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p\n", 1883 dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p\n",
1901 (unsigned long)fifo->start, udc->fifo); 1884 (unsigned long)fifo->start, udc->fifo);
1902 1885
1903 device_initialize(&udc->gadget.dev);
1904 udc->gadget.dev.parent = &pdev->dev;
1905 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
1906
1907 platform_set_drvdata(pdev, udc); 1886 platform_set_drvdata(pdev, udc);
1908 1887
1909 /* Make sure we start from a clean slate */ 1888 /* Make sure we start from a clean slate */
@@ -1962,12 +1941,6 @@ static int __init usba_udc_probe(struct platform_device *pdev)
1962 } 1941 }
1963 udc->irq = irq; 1942 udc->irq = irq;
1964 1943
1965 ret = device_add(&udc->gadget.dev);
1966 if (ret) {
1967 dev_dbg(&pdev->dev, "Could not add gadget: %d\n", ret);
1968 goto err_device_add;
1969 }
1970
1971 if (gpio_is_valid(pdata->vbus_pin)) { 1944 if (gpio_is_valid(pdata->vbus_pin)) {
1972 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) { 1945 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) {
1973 udc->vbus_pin = pdata->vbus_pin; 1946 udc->vbus_pin = pdata->vbus_pin;
@@ -2007,9 +1980,6 @@ err_add_udc:
2007 gpio_free(udc->vbus_pin); 1980 gpio_free(udc->vbus_pin);
2008 } 1981 }
2009 1982
2010 device_unregister(&udc->gadget.dev);
2011
2012err_device_add:
2013 free_irq(irq, udc); 1983 free_irq(irq, udc);
2014err_request_irq: 1984err_request_irq:
2015 kfree(usba_ep); 1985 kfree(usba_ep);
@@ -2053,8 +2023,6 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2053 clk_put(udc->hclk); 2023 clk_put(udc->hclk);
2054 clk_put(udc->pclk); 2024 clk_put(udc->pclk);
2055 2025
2056 device_unregister(&udc->gadget.dev);
2057
2058 return 0; 2026 return 0;
2059} 2027}
2060 2028
diff --git a/drivers/usb/gadget/atmel_usba_udc.h b/drivers/usb/gadget/atmel_usba_udc.h
index 9791259cbda7..d65a61851d3d 100644
--- a/drivers/usb/gadget/atmel_usba_udc.h
+++ b/drivers/usb/gadget/atmel_usba_udc.h
@@ -216,12 +216,6 @@
216#define EP0_EPT_SIZE USBA_EPT_SIZE_64 216#define EP0_EPT_SIZE USBA_EPT_SIZE_64
217#define EP0_NR_BANKS 1 217#define EP0_NR_BANKS 1
218 218
219/*
220 * REVISIT: Try to eliminate this value. Can we rely on req->mapped to
221 * provide this information?
222 */
223#define DMA_ADDR_INVALID (~(dma_addr_t)0)
224
225#define FIFO_IOMEM_ID 0 219#define FIFO_IOMEM_ID 0
226#define CTRL_IOMEM_ID 1 220#define CTRL_IOMEM_ID 1
227 221
diff --git a/drivers/usb/gadget/bcm63xx_udc.c b/drivers/usb/gadget/bcm63xx_udc.c
index 8cc8253f1100..6e6518264c42 100644
--- a/drivers/usb/gadget/bcm63xx_udc.c
+++ b/drivers/usb/gadget/bcm63xx_udc.c
@@ -1819,7 +1819,6 @@ static int bcm63xx_udc_start(struct usb_gadget *gadget,
1819 1819
1820 udc->driver = driver; 1820 udc->driver = driver;
1821 driver->driver.bus = NULL; 1821 driver->driver.bus = NULL;
1822 udc->gadget.dev.driver = &driver->driver;
1823 udc->gadget.dev.of_node = udc->dev->of_node; 1822 udc->gadget.dev.of_node = udc->dev->of_node;
1824 1823
1825 spin_unlock_irqrestore(&udc->lock, flags); 1824 spin_unlock_irqrestore(&udc->lock, flags);
@@ -1841,7 +1840,6 @@ static int bcm63xx_udc_stop(struct usb_gadget *gadget,
1841 spin_lock_irqsave(&udc->lock, flags); 1840 spin_lock_irqsave(&udc->lock, flags);
1842 1841
1843 udc->driver = NULL; 1842 udc->driver = NULL;
1844 udc->gadget.dev.driver = NULL;
1845 1843
1846 /* 1844 /*
1847 * If we switch the PHY too abruptly after dropping D+, the host 1845 * If we switch the PHY too abruptly after dropping D+, the host
@@ -2306,17 +2304,6 @@ static void bcm63xx_udc_cleanup_debugfs(struct bcm63xx_udc *udc)
2306 ***********************************************************************/ 2304 ***********************************************************************/
2307 2305
2308/** 2306/**
2309 * bcm63xx_udc_gadget_release - Called from device_release().
2310 * @dev: Unused.
2311 *
2312 * We get a warning if this function doesn't exist, but it's empty because
2313 * we don't have to free any of the memory allocated with the devm_* APIs.
2314 */
2315static void bcm63xx_udc_gadget_release(struct device *dev)
2316{
2317}
2318
2319/**
2320 * bcm63xx_udc_probe - Initialize a new instance of the UDC. 2307 * bcm63xx_udc_probe - Initialize a new instance of the UDC.
2321 * @pdev: Platform device struct from the bcm63xx BSP code. 2308 * @pdev: Platform device struct from the bcm63xx BSP code.
2322 * 2309 *
@@ -2368,13 +2355,9 @@ static int bcm63xx_udc_probe(struct platform_device *pdev)
2368 2355
2369 spin_lock_init(&udc->lock); 2356 spin_lock_init(&udc->lock);
2370 INIT_WORK(&udc->ep0_wq, bcm63xx_ep0_process); 2357 INIT_WORK(&udc->ep0_wq, bcm63xx_ep0_process);
2371 dev_set_name(&udc->gadget.dev, "gadget");
2372 2358
2373 udc->gadget.ops = &bcm63xx_udc_ops; 2359 udc->gadget.ops = &bcm63xx_udc_ops;
2374 udc->gadget.name = dev_name(dev); 2360 udc->gadget.name = dev_name(dev);
2375 udc->gadget.dev.parent = dev;
2376 udc->gadget.dev.release = bcm63xx_udc_gadget_release;
2377 udc->gadget.dev.dma_mask = dev->dma_mask;
2378 2361
2379 if (!pd->use_fullspeed && !use_fullspeed) 2362 if (!pd->use_fullspeed && !use_fullspeed)
2380 udc->gadget.max_speed = USB_SPEED_HIGH; 2363 udc->gadget.max_speed = USB_SPEED_HIGH;
@@ -2414,17 +2397,12 @@ static int bcm63xx_udc_probe(struct platform_device *pdev)
2414 } 2397 }
2415 } 2398 }
2416 2399
2417 rc = device_register(&udc->gadget.dev);
2418 if (rc)
2419 goto out_uninit;
2420
2421 bcm63xx_udc_init_debugfs(udc); 2400 bcm63xx_udc_init_debugfs(udc);
2422 rc = usb_add_gadget_udc(dev, &udc->gadget); 2401 rc = usb_add_gadget_udc(dev, &udc->gadget);
2423 if (!rc) 2402 if (!rc)
2424 return 0; 2403 return 0;
2425 2404
2426 bcm63xx_udc_cleanup_debugfs(udc); 2405 bcm63xx_udc_cleanup_debugfs(udc);
2427 device_unregister(&udc->gadget.dev);
2428out_uninit: 2406out_uninit:
2429 bcm63xx_uninit_udc_hw(udc); 2407 bcm63xx_uninit_udc_hw(udc);
2430 return rc; 2408 return rc;
@@ -2440,7 +2418,6 @@ static int bcm63xx_udc_remove(struct platform_device *pdev)
2440 2418
2441 bcm63xx_udc_cleanup_debugfs(udc); 2419 bcm63xx_udc_cleanup_debugfs(udc);
2442 usb_del_gadget_udc(&udc->gadget); 2420 usb_del_gadget_udc(&udc->gadget);
2443 device_unregister(&udc->gadget.dev);
2444 BUG_ON(udc->driver); 2421 BUG_ON(udc->driver);
2445 2422
2446 platform_set_drvdata(pdev, NULL); 2423 platform_set_drvdata(pdev, NULL);
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c
index a7d6f7026757..c6ee6f1558c3 100644
--- a/drivers/usb/gadget/cdc2.c
+++ b/drivers/usb/gadget/cdc2.c
@@ -103,18 +103,16 @@ static struct usb_gadget_strings *dev_strings[] = {
103}; 103};
104 104
105static u8 hostaddr[ETH_ALEN]; 105static u8 hostaddr[ETH_ALEN];
106 106static struct eth_dev *the_dev;
107/*-------------------------------------------------------------------------*/ 107/*-------------------------------------------------------------------------*/
108static struct usb_function *f_acm; 108static struct usb_function *f_acm;
109static struct usb_function_instance *fi_serial; 109static struct usb_function_instance *fi_serial;
110 110
111static unsigned char tty_line;
112/* 111/*
113 * We _always_ have both CDC ECM and CDC ACM functions. 112 * We _always_ have both CDC ECM and CDC ACM functions.
114 */ 113 */
115static int __init cdc_do_config(struct usb_configuration *c) 114static int __init cdc_do_config(struct usb_configuration *c)
116{ 115{
117 struct f_serial_opts *opts;
118 int status; 116 int status;
119 117
120 if (gadget_is_otg(c->cdev->gadget)) { 118 if (gadget_is_otg(c->cdev->gadget)) {
@@ -122,7 +120,7 @@ static int __init cdc_do_config(struct usb_configuration *c)
122 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 120 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
123 } 121 }
124 122
125 status = ecm_bind_config(c, hostaddr); 123 status = ecm_bind_config(c, hostaddr, the_dev);
126 if (status < 0) 124 if (status < 0)
127 return status; 125 return status;
128 126
@@ -130,9 +128,6 @@ static int __init cdc_do_config(struct usb_configuration *c)
130 if (IS_ERR(fi_serial)) 128 if (IS_ERR(fi_serial))
131 return PTR_ERR(fi_serial); 129 return PTR_ERR(fi_serial);
132 130
133 opts = container_of(fi_serial, struct f_serial_opts, func_inst);
134 opts->port_num = tty_line;
135
136 f_acm = usb_get_function(fi_serial); 131 f_acm = usb_get_function(fi_serial);
137 if (IS_ERR(f_acm)) 132 if (IS_ERR(f_acm))
138 goto err_func_acm; 133 goto err_func_acm;
@@ -169,14 +164,9 @@ static int __init cdc_bind(struct usb_composite_dev *cdev)
169 } 164 }
170 165
171 /* set up network link layer */ 166 /* set up network link layer */
172 status = gether_setup(cdev->gadget, hostaddr); 167 the_dev = gether_setup(cdev->gadget, hostaddr);
173 if (status < 0) 168 if (IS_ERR(the_dev))
174 return status; 169 return PTR_ERR(the_dev);
175
176 /* set up serial link layer */
177 status = gserial_alloc_line(&tty_line);
178 if (status < 0)
179 goto fail0;
180 170
181 /* Allocate string descriptor numbers ... note that string 171 /* Allocate string descriptor numbers ... note that string
182 * contents can be overridden by the composite_dev glue. 172 * contents can be overridden by the composite_dev glue.
@@ -200,9 +190,7 @@ static int __init cdc_bind(struct usb_composite_dev *cdev)
200 return 0; 190 return 0;
201 191
202fail1: 192fail1:
203 gserial_free_line(tty_line); 193 gether_cleanup(the_dev);
204fail0:
205 gether_cleanup();
206 return status; 194 return status;
207} 195}
208 196
@@ -210,8 +198,7 @@ static int __exit cdc_unbind(struct usb_composite_dev *cdev)
210{ 198{
211 usb_put_function(f_acm); 199 usb_put_function(f_acm);
212 usb_put_function_instance(fi_serial); 200 usb_put_function_instance(fi_serial);
213 gserial_free_line(tty_line); 201 gether_cleanup(the_dev);
214 gether_cleanup();
215 return 0; 202 return 0;
216} 203}
217 204
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index c0d62b278610..55f4df60f327 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1637,6 +1637,7 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev)
1637 kfree(cdev->req->buf); 1637 kfree(cdev->req->buf);
1638 usb_ep_free_request(cdev->gadget->ep0, cdev->req); 1638 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
1639 } 1639 }
1640 cdev->next_string_id = 0;
1640 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); 1641 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
1641} 1642}
1642 1643
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
new file mode 100644
index 000000000000..a34633a898a1
--- /dev/null
+++ b/drivers/usb/gadget/configfs.c
@@ -0,0 +1,1003 @@
1#include <linux/configfs.h>
2#include <linux/module.h>
3#include <linux/slab.h>
4#include <linux/device.h>
5#include <linux/usb/composite.h>
6#include <linux/usb/gadget_configfs.h>
7
8int check_user_usb_string(const char *name,
9 struct usb_gadget_strings *stringtab_dev)
10{
11 unsigned primary_lang;
12 unsigned sub_lang;
13 u16 num;
14 int ret;
15
16 ret = kstrtou16(name, 0, &num);
17 if (ret)
18 return ret;
19
20 primary_lang = num & 0x3ff;
21 sub_lang = num >> 10;
22
23 /* simple sanity check for valid langid */
24 switch (primary_lang) {
25 case 0:
26 case 0x62 ... 0xfe:
27 case 0x100 ... 0x3ff:
28 return -EINVAL;
29 }
30 if (!sub_lang)
31 return -EINVAL;
32
33 stringtab_dev->language = num;
34 return 0;
35}
36
37#define MAX_NAME_LEN 40
38#define MAX_USB_STRING_LANGS 2
39
40struct gadget_info {
41 struct config_group group;
42 struct config_group functions_group;
43 struct config_group configs_group;
44 struct config_group strings_group;
45 struct config_group *default_groups[4];
46
47 struct mutex lock;
48 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
49 struct list_head string_list;
50 struct list_head available_func;
51
52 const char *udc_name;
53#ifdef CONFIG_USB_OTG
54 struct usb_otg_descriptor otg;
55#endif
56 struct usb_composite_driver composite;
57 struct usb_composite_dev cdev;
58};
59
60struct config_usb_cfg {
61 struct config_group group;
62 struct config_group strings_group;
63 struct config_group *default_groups[2];
64 struct list_head string_list;
65 struct usb_configuration c;
66 struct list_head func_list;
67 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
68};
69
70struct gadget_strings {
71 struct usb_gadget_strings stringtab_dev;
72 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
73 char *manufacturer;
74 char *product;
75 char *serialnumber;
76
77 struct config_group group;
78 struct list_head list;
79};
80
81struct gadget_config_name {
82 struct usb_gadget_strings stringtab_dev;
83 struct usb_string strings;
84 char *configuration;
85
86 struct config_group group;
87 struct list_head list;
88};
89
90static int usb_string_copy(const char *s, char **s_copy)
91{
92 int ret;
93 char *str;
94 char *copy = *s_copy;
95 ret = strlen(s);
96 if (ret > 126)
97 return -EOVERFLOW;
98
99 str = kstrdup(s, GFP_KERNEL);
100 if (!str)
101 return -ENOMEM;
102 if (str[ret - 1] == '\n')
103 str[ret - 1] = '\0';
104 kfree(copy);
105 *s_copy = str;
106 return 0;
107}
108
109CONFIGFS_ATTR_STRUCT(gadget_info);
110CONFIGFS_ATTR_STRUCT(config_usb_cfg);
111
112#define GI_DEVICE_DESC_ITEM_ATTR(name) \
113 static struct gadget_info_attribute gadget_cdev_desc_##name = \
114 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
115 gadget_dev_desc_##name##_show, \
116 gadget_dev_desc_##name##_store)
117
118#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
119 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
120 char *page) \
121{ \
122 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
123}
124
125#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
126 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
127 char *page) \
128{ \
129 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
130}
131
132
133#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
134 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
135 const char *page, size_t len) \
136{ \
137 u8 val; \
138 int ret; \
139 ret = kstrtou8(page, 0, &val); \
140 if (ret) \
141 return ret; \
142 gi->cdev.desc._name = val; \
143 return len; \
144}
145
146#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
147 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
148 const char *page, size_t len) \
149{ \
150 u16 val; \
151 int ret; \
152 ret = kstrtou16(page, 0, &val); \
153 if (ret) \
154 return ret; \
155 gi->cdev.desc._name = cpu_to_le16p(&val); \
156 return len; \
157}
158
159#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
160 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
161 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
162
163GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
164GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
165GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
166GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
167GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
168GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
169GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
170GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
171
172static ssize_t is_valid_bcd(u16 bcd_val)
173{
174 if ((bcd_val & 0xf) > 9)
175 return -EINVAL;
176 if (((bcd_val >> 4) & 0xf) > 9)
177 return -EINVAL;
178 if (((bcd_val >> 8) & 0xf) > 9)
179 return -EINVAL;
180 if (((bcd_val >> 12) & 0xf) > 9)
181 return -EINVAL;
182 return 0;
183}
184
185static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
186 const char *page, size_t len)
187{
188 u16 bcdDevice;
189 int ret;
190
191 ret = kstrtou16(page, 0, &bcdDevice);
192 if (ret)
193 return ret;
194 ret = is_valid_bcd(bcdDevice);
195 if (ret)
196 return ret;
197
198 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
199 return len;
200}
201
202static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
203 const char *page, size_t len)
204{
205 u16 bcdUSB;
206 int ret;
207
208 ret = kstrtou16(page, 0, &bcdUSB);
209 if (ret)
210 return ret;
211 ret = is_valid_bcd(bcdUSB);
212 if (ret)
213 return ret;
214
215 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
216 return len;
217}
218
219static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
220{
221 return sprintf(page, "%s\n", gi->udc_name ?: "");
222}
223
224static int unregister_gadget(struct gadget_info *gi)
225{
226 int ret;
227
228 if (!gi->udc_name)
229 return -ENODEV;
230
231 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
232 if (ret)
233 return ret;
234 kfree(gi->udc_name);
235 gi->udc_name = NULL;
236 return 0;
237}
238
239static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
240 const char *page, size_t len)
241{
242 char *name;
243 int ret;
244
245 name = kstrdup(page, GFP_KERNEL);
246 if (!name)
247 return -ENOMEM;
248 if (name[len - 1] == '\n')
249 name[len - 1] = '\0';
250
251 mutex_lock(&gi->lock);
252
253 if (!strlen(name)) {
254 ret = unregister_gadget(gi);
255 if (ret)
256 goto err;
257 } else {
258 if (gi->udc_name) {
259 ret = -EBUSY;
260 goto err;
261 }
262 ret = udc_attach_driver(name, &gi->composite.gadget_driver);
263 if (ret)
264 goto err;
265 gi->udc_name = name;
266 }
267 mutex_unlock(&gi->lock);
268 return len;
269err:
270 kfree(name);
271 mutex_unlock(&gi->lock);
272 return ret;
273}
274
275GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
276GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
277GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
278GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
279GI_DEVICE_DESC_ITEM_ATTR(idVendor);
280GI_DEVICE_DESC_ITEM_ATTR(idProduct);
281GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
282GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
283GI_DEVICE_DESC_ITEM_ATTR(UDC);
284
285static struct configfs_attribute *gadget_root_attrs[] = {
286 &gadget_cdev_desc_bDeviceClass.attr,
287 &gadget_cdev_desc_bDeviceSubClass.attr,
288 &gadget_cdev_desc_bDeviceProtocol.attr,
289 &gadget_cdev_desc_bMaxPacketSize0.attr,
290 &gadget_cdev_desc_idVendor.attr,
291 &gadget_cdev_desc_idProduct.attr,
292 &gadget_cdev_desc_bcdDevice.attr,
293 &gadget_cdev_desc_bcdUSB.attr,
294 &gadget_cdev_desc_UDC.attr,
295 NULL,
296};
297
298static inline struct gadget_info *to_gadget_info(struct config_item *item)
299{
300 return container_of(to_config_group(item), struct gadget_info, group);
301}
302
303static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
304{
305 return container_of(to_config_group(item), struct gadget_strings,
306 group);
307}
308
309static inline struct gadget_config_name *to_gadget_config_name(
310 struct config_item *item)
311{
312 return container_of(to_config_group(item), struct gadget_config_name,
313 group);
314}
315
316static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
317{
318 return container_of(to_config_group(item), struct config_usb_cfg,
319 group);
320}
321
322static inline struct usb_function_instance *to_usb_function_instance(
323 struct config_item *item)
324{
325 return container_of(to_config_group(item),
326 struct usb_function_instance, group);
327}
328
329static void gadget_info_attr_release(struct config_item *item)
330{
331 struct gadget_info *gi = to_gadget_info(item);
332
333 WARN_ON(!list_empty(&gi->cdev.configs));
334 WARN_ON(!list_empty(&gi->string_list));
335 WARN_ON(!list_empty(&gi->available_func));
336 kfree(gi->composite.gadget_driver.function);
337 kfree(gi);
338}
339
340CONFIGFS_ATTR_OPS(gadget_info);
341
342static struct configfs_item_operations gadget_root_item_ops = {
343 .release = gadget_info_attr_release,
344 .show_attribute = gadget_info_attr_show,
345 .store_attribute = gadget_info_attr_store,
346};
347
348static void gadget_config_attr_release(struct config_item *item)
349{
350 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
351
352 WARN_ON(!list_empty(&cfg->c.functions));
353 list_del(&cfg->c.list);
354 kfree(cfg->c.label);
355 kfree(cfg);
356}
357
358static int config_usb_cfg_link(
359 struct config_item *usb_cfg_ci,
360 struct config_item *usb_func_ci)
361{
362 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
363 struct usb_composite_dev *cdev = cfg->c.cdev;
364 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
365
366 struct config_group *group = to_config_group(usb_func_ci);
367 struct usb_function_instance *fi = container_of(group,
368 struct usb_function_instance, group);
369 struct usb_function_instance *a_fi;
370 struct usb_function *f;
371 int ret;
372
373 mutex_lock(&gi->lock);
374 /*
375 * Make sure this function is from within our _this_ gadget and not
376 * from another gadget or a random directory.
377 * Also a function instance can only be linked once.
378 */
379 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
380 if (a_fi == fi)
381 break;
382 }
383 if (a_fi != fi) {
384 ret = -EINVAL;
385 goto out;
386 }
387
388 list_for_each_entry(f, &cfg->func_list, list) {
389 if (f->fi == fi) {
390 ret = -EEXIST;
391 goto out;
392 }
393 }
394
395 f = usb_get_function(fi);
396 if (IS_ERR(f)) {
397 ret = PTR_ERR(f);
398 goto out;
399 }
400
401 /* stash the function until we bind it to the gadget */
402 list_add_tail(&f->list, &cfg->func_list);
403 ret = 0;
404out:
405 mutex_unlock(&gi->lock);
406 return ret;
407}
408
409static int config_usb_cfg_unlink(
410 struct config_item *usb_cfg_ci,
411 struct config_item *usb_func_ci)
412{
413 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
414 struct usb_composite_dev *cdev = cfg->c.cdev;
415 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
416
417 struct config_group *group = to_config_group(usb_func_ci);
418 struct usb_function_instance *fi = container_of(group,
419 struct usb_function_instance, group);
420 struct usb_function *f;
421
422 /*
423 * ideally I would like to forbid to unlink functions while a gadget is
424 * bound to an UDC. Since this isn't possible at the moment, we simply
425 * force an unbind, the function is available here and then we can
426 * remove the function.
427 */
428 mutex_lock(&gi->lock);
429 if (gi->udc_name)
430 unregister_gadget(gi);
431 WARN_ON(gi->udc_name);
432
433 list_for_each_entry(f, &cfg->func_list, list) {
434 if (f->fi == fi) {
435 list_del(&f->list);
436 usb_put_function(f);
437 mutex_unlock(&gi->lock);
438 return 0;
439 }
440 }
441 mutex_unlock(&gi->lock);
442 __WARN_printf("Unable to locate function to unbind\n");
443 return 0;
444}
445
446CONFIGFS_ATTR_OPS(config_usb_cfg);
447
448static struct configfs_item_operations gadget_config_item_ops = {
449 .release = gadget_config_attr_release,
450 .show_attribute = config_usb_cfg_attr_show,
451 .store_attribute = config_usb_cfg_attr_store,
452 .allow_link = config_usb_cfg_link,
453 .drop_link = config_usb_cfg_unlink,
454};
455
456
457static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
458 char *page)
459{
460 return sprintf(page, "%u\n", cfg->c.MaxPower);
461}
462
463static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
464 const char *page, size_t len)
465{
466 u16 val;
467 int ret;
468 ret = kstrtou16(page, 0, &val);
469 if (ret)
470 return ret;
471 if (DIV_ROUND_UP(val, 8) > 0xff)
472 return -ERANGE;
473 cfg->c.MaxPower = val;
474 return len;
475}
476
477static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
478 char *page)
479{
480 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
481}
482
483static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
484 const char *page, size_t len)
485{
486 u8 val;
487 int ret;
488 ret = kstrtou8(page, 0, &val);
489 if (ret)
490 return ret;
491 if (!(val & USB_CONFIG_ATT_ONE))
492 return -EINVAL;
493 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
494 USB_CONFIG_ATT_WAKEUP))
495 return -EINVAL;
496 cfg->c.bmAttributes = val;
497 return len;
498}
499
500#define CFG_CONFIG_DESC_ITEM_ATTR(name) \
501 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
502 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
503 gadget_config_desc_##name##_show, \
504 gadget_config_desc_##name##_store)
505
506CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
507CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
508
509static struct configfs_attribute *gadget_config_attrs[] = {
510 &gadget_usb_cfg_MaxPower.attr,
511 &gadget_usb_cfg_bmAttributes.attr,
512 NULL,
513};
514
515static struct config_item_type gadget_config_type = {
516 .ct_item_ops = &gadget_config_item_ops,
517 .ct_attrs = gadget_config_attrs,
518 .ct_owner = THIS_MODULE,
519};
520
521static struct config_item_type gadget_root_type = {
522 .ct_item_ops = &gadget_root_item_ops,
523 .ct_attrs = gadget_root_attrs,
524 .ct_owner = THIS_MODULE,
525};
526
527static void composite_init_dev(struct usb_composite_dev *cdev)
528{
529 spin_lock_init(&cdev->lock);
530 INIT_LIST_HEAD(&cdev->configs);
531 INIT_LIST_HEAD(&cdev->gstrings);
532}
533
534static struct config_group *function_make(
535 struct config_group *group,
536 const char *name)
537{
538 struct gadget_info *gi;
539 struct usb_function_instance *fi;
540 char buf[MAX_NAME_LEN];
541 char *func_name;
542 char *instance_name;
543 int ret;
544
545 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
546 if (ret >= MAX_NAME_LEN)
547 return ERR_PTR(-ENAMETOOLONG);
548
549 func_name = buf;
550 instance_name = strchr(func_name, '.');
551 if (!instance_name) {
552 pr_err("Unable to locate . in FUNC.INSTANCE\n");
553 return ERR_PTR(-EINVAL);
554 }
555 *instance_name = '\0';
556 instance_name++;
557
558 fi = usb_get_function_instance(func_name);
559 if (IS_ERR(fi))
560 return ERR_PTR(PTR_ERR(fi));
561
562 ret = config_item_set_name(&fi->group.cg_item, name);
563 if (ret) {
564 usb_put_function_instance(fi);
565 return ERR_PTR(ret);
566 }
567
568 gi = container_of(group, struct gadget_info, functions_group);
569
570 mutex_lock(&gi->lock);
571 list_add_tail(&fi->cfs_list, &gi->available_func);
572 mutex_unlock(&gi->lock);
573 return &fi->group;
574}
575
576static void function_drop(
577 struct config_group *group,
578 struct config_item *item)
579{
580 struct usb_function_instance *fi = to_usb_function_instance(item);
581 struct gadget_info *gi;
582
583 gi = container_of(group, struct gadget_info, functions_group);
584
585 mutex_lock(&gi->lock);
586 list_del(&fi->cfs_list);
587 mutex_unlock(&gi->lock);
588 config_item_put(item);
589}
590
591static struct configfs_group_operations functions_ops = {
592 .make_group = &function_make,
593 .drop_item = &function_drop,
594};
595
596static struct config_item_type functions_type = {
597 .ct_group_ops = &functions_ops,
598 .ct_owner = THIS_MODULE,
599};
600
601CONFIGFS_ATTR_STRUCT(gadget_config_name);
602GS_STRINGS_RW(gadget_config_name, configuration);
603
604static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
605 &gadget_config_name_configuration.attr,
606 NULL,
607};
608
609static void gadget_config_name_attr_release(struct config_item *item)
610{
611 struct gadget_config_name *cn = to_gadget_config_name(item);
612
613 kfree(cn->configuration);
614
615 list_del(&cn->list);
616 kfree(cn);
617}
618
619USB_CONFIG_STRING_RW_OPS(gadget_config_name);
620USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
621
622static struct config_group *config_desc_make(
623 struct config_group *group,
624 const char *name)
625{
626 struct gadget_info *gi;
627 struct config_usb_cfg *cfg;
628 char buf[MAX_NAME_LEN];
629 char *num_str;
630 u8 num;
631 int ret;
632
633 gi = container_of(group, struct gadget_info, configs_group);
634 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
635 if (ret >= MAX_NAME_LEN)
636 return ERR_PTR(-ENAMETOOLONG);
637
638 num_str = strchr(buf, '.');
639 if (!num_str) {
640 pr_err("Unable to locate . in name.bConfigurationValue\n");
641 return ERR_PTR(-EINVAL);
642 }
643
644 *num_str = '\0';
645 num_str++;
646
647 if (!strlen(buf))
648 return ERR_PTR(-EINVAL);
649
650 ret = kstrtou8(num_str, 0, &num);
651 if (ret)
652 return ERR_PTR(ret);
653
654 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
655 if (!cfg)
656 return ERR_PTR(-ENOMEM);
657 cfg->c.label = kstrdup(buf, GFP_KERNEL);
658 if (!cfg->c.label) {
659 ret = -ENOMEM;
660 goto err;
661 }
662 cfg->c.bConfigurationValue = num;
663 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
664 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
665 INIT_LIST_HEAD(&cfg->string_list);
666 INIT_LIST_HEAD(&cfg->func_list);
667
668 cfg->group.default_groups = cfg->default_groups;
669 cfg->default_groups[0] = &cfg->strings_group;
670
671 config_group_init_type_name(&cfg->group, name,
672 &gadget_config_type);
673 config_group_init_type_name(&cfg->strings_group, "strings",
674 &gadget_config_name_strings_type);
675
676 ret = usb_add_config_only(&gi->cdev, &cfg->c);
677 if (ret)
678 goto err;
679
680 return &cfg->group;
681err:
682 kfree(cfg->c.label);
683 kfree(cfg);
684 return ERR_PTR(ret);
685}
686
687static void config_desc_drop(
688 struct config_group *group,
689 struct config_item *item)
690{
691 config_item_put(item);
692}
693
694static struct configfs_group_operations config_desc_ops = {
695 .make_group = &config_desc_make,
696 .drop_item = &config_desc_drop,
697};
698
699static struct config_item_type config_desc_type = {
700 .ct_group_ops = &config_desc_ops,
701 .ct_owner = THIS_MODULE,
702};
703
704CONFIGFS_ATTR_STRUCT(gadget_strings);
705GS_STRINGS_RW(gadget_strings, manufacturer);
706GS_STRINGS_RW(gadget_strings, product);
707GS_STRINGS_RW(gadget_strings, serialnumber);
708
709static struct configfs_attribute *gadget_strings_langid_attrs[] = {
710 &gadget_strings_manufacturer.attr,
711 &gadget_strings_product.attr,
712 &gadget_strings_serialnumber.attr,
713 NULL,
714};
715
716static void gadget_strings_attr_release(struct config_item *item)
717{
718 struct gadget_strings *gs = to_gadget_strings(item);
719
720 kfree(gs->manufacturer);
721 kfree(gs->product);
722 kfree(gs->serialnumber);
723
724 list_del(&gs->list);
725 kfree(gs);
726}
727
728USB_CONFIG_STRING_RW_OPS(gadget_strings);
729USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
730
731static int configfs_do_nothing(struct usb_composite_dev *cdev)
732{
733 __WARN();
734 return -EINVAL;
735}
736
737int composite_dev_prepare(struct usb_composite_driver *composite,
738 struct usb_composite_dev *dev);
739
740static void purge_configs_funcs(struct gadget_info *gi)
741{
742 struct usb_configuration *c;
743
744 list_for_each_entry(c, &gi->cdev.configs, list) {
745 struct usb_function *f, *tmp;
746 struct config_usb_cfg *cfg;
747
748 cfg = container_of(c, struct config_usb_cfg, c);
749
750 list_for_each_entry_safe(f, tmp, &c->functions, list) {
751
752 list_move_tail(&f->list, &cfg->func_list);
753 if (f->unbind) {
754 dev_err(&gi->cdev.gadget->dev, "unbind function"
755 " '%s'/%p\n", f->name, f);
756 f->unbind(c, f);
757 }
758 }
759 c->next_interface_id = 0;
760 c->superspeed = 0;
761 c->highspeed = 0;
762 c->fullspeed = 0;
763 }
764}
765
766static int configfs_composite_bind(struct usb_gadget *gadget,
767 struct usb_gadget_driver *gdriver)
768{
769 struct usb_composite_driver *composite = to_cdriver(gdriver);
770 struct gadget_info *gi = container_of(composite,
771 struct gadget_info, composite);
772 struct usb_composite_dev *cdev = &gi->cdev;
773 struct usb_configuration *c;
774 struct usb_string *s;
775 unsigned i;
776 int ret;
777
778 /* the gi->lock is hold by the caller */
779 cdev->gadget = gadget;
780 set_gadget_data(gadget, cdev);
781 ret = composite_dev_prepare(composite, cdev);
782 if (ret)
783 return ret;
784 /* and now the gadget bind */
785 ret = -EINVAL;
786
787 if (list_empty(&gi->cdev.configs)) {
788 pr_err("Need atleast one configuration in %s.\n",
789 gi->composite.name);
790 goto err_comp_cleanup;
791 }
792
793
794 list_for_each_entry(c, &gi->cdev.configs, list) {
795 struct config_usb_cfg *cfg;
796
797 cfg = container_of(c, struct config_usb_cfg, c);
798 if (list_empty(&cfg->func_list)) {
799 pr_err("Config %s/%d of %s needs atleast one function.\n",
800 c->label, c->bConfigurationValue,
801 gi->composite.name);
802 goto err_comp_cleanup;
803 }
804 }
805
806 /* init all strings */
807 if (!list_empty(&gi->string_list)) {
808 struct gadget_strings *gs;
809
810 i = 0;
811 list_for_each_entry(gs, &gi->string_list, list) {
812
813 gi->gstrings[i] = &gs->stringtab_dev;
814 gs->stringtab_dev.strings = gs->strings;
815 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
816 gs->manufacturer;
817 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
818 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
819 i++;
820 }
821 gi->gstrings[i] = NULL;
822 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
823 USB_GADGET_FIRST_AVAIL_IDX);
824 if (IS_ERR(s))
825 goto err_comp_cleanup;
826
827 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
828 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
829 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
830 }
831
832 /* Go through all configs, attach all functions */
833 list_for_each_entry(c, &gi->cdev.configs, list) {
834 struct config_usb_cfg *cfg;
835 struct usb_function *f;
836 struct usb_function *tmp;
837 struct gadget_config_name *cn;
838
839 cfg = container_of(c, struct config_usb_cfg, c);
840 if (!list_empty(&cfg->string_list)) {
841 i = 0;
842 list_for_each_entry(cn, &cfg->string_list, list) {
843 cfg->gstrings[i] = &cn->stringtab_dev;
844 cn->stringtab_dev.strings = &cn->strings;
845 cn->strings.s = cn->configuration;
846 i++;
847 }
848 cfg->gstrings[i] = NULL;
849 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
850 if (IS_ERR(s))
851 goto err_comp_cleanup;
852 c->iConfiguration = s[0].id;
853 }
854
855 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
856 list_del(&f->list);
857 ret = usb_add_function(c, f);
858 if (ret)
859 goto err_purge_funcs;
860 }
861 usb_ep_autoconfig_reset(cdev->gadget);
862 }
863 usb_ep_autoconfig_reset(cdev->gadget);
864 return 0;
865
866err_purge_funcs:
867 purge_configs_funcs(gi);
868err_comp_cleanup:
869 composite_dev_cleanup(cdev);
870 return ret;
871}
872
873static void configfs_composite_unbind(struct usb_gadget *gadget)
874{
875 struct usb_composite_dev *cdev;
876 struct gadget_info *gi;
877
878 /* the gi->lock is hold by the caller */
879
880 cdev = get_gadget_data(gadget);
881 gi = container_of(cdev, struct gadget_info, cdev);
882
883 purge_configs_funcs(gi);
884 composite_dev_cleanup(cdev);
885 usb_ep_autoconfig_reset(cdev->gadget);
886 cdev->gadget = NULL;
887 set_gadget_data(gadget, NULL);
888}
889
890static const struct usb_gadget_driver configfs_driver_template = {
891 .bind = configfs_composite_bind,
892 .unbind = configfs_composite_unbind,
893
894 .setup = composite_setup,
895 .disconnect = composite_disconnect,
896
897 .max_speed = USB_SPEED_SUPER,
898 .driver = {
899 .owner = THIS_MODULE,
900 .name = "configfs-gadget",
901 },
902};
903
904static struct config_group *gadgets_make(
905 struct config_group *group,
906 const char *name)
907{
908 struct gadget_info *gi;
909
910 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
911 if (!gi)
912 return ERR_PTR(-ENOMEM);
913
914 gi->group.default_groups = gi->default_groups;
915 gi->group.default_groups[0] = &gi->functions_group;
916 gi->group.default_groups[1] = &gi->configs_group;
917 gi->group.default_groups[2] = &gi->strings_group;
918
919 config_group_init_type_name(&gi->functions_group, "functions",
920 &functions_type);
921 config_group_init_type_name(&gi->configs_group, "configs",
922 &config_desc_type);
923 config_group_init_type_name(&gi->strings_group, "strings",
924 &gadget_strings_strings_type);
925
926 gi->composite.bind = configfs_do_nothing;
927 gi->composite.unbind = configfs_do_nothing;
928 gi->composite.suspend = NULL;
929 gi->composite.resume = NULL;
930 gi->composite.max_speed = USB_SPEED_SUPER;
931
932 mutex_init(&gi->lock);
933 INIT_LIST_HEAD(&gi->string_list);
934 INIT_LIST_HEAD(&gi->available_func);
935
936 composite_init_dev(&gi->cdev);
937 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
938 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
939 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
940
941 gi->composite.gadget_driver = configfs_driver_template;
942
943 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
944 gi->composite.name = gi->composite.gadget_driver.function;
945
946 if (!gi->composite.gadget_driver.function)
947 goto err;
948
949#ifdef CONFIG_USB_OTG
950 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
951 gi->otg.bDescriptorType = USB_DT_OTG;
952 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
953#endif
954
955 config_group_init_type_name(&gi->group, name,
956 &gadget_root_type);
957 return &gi->group;
958err:
959 kfree(gi);
960 return ERR_PTR(-ENOMEM);
961}
962
963static void gadgets_drop(struct config_group *group, struct config_item *item)
964{
965 config_item_put(item);
966}
967
968static struct configfs_group_operations gadgets_ops = {
969 .make_group = &gadgets_make,
970 .drop_item = &gadgets_drop,
971};
972
973static struct config_item_type gadgets_type = {
974 .ct_group_ops = &gadgets_ops,
975 .ct_owner = THIS_MODULE,
976};
977
978static struct configfs_subsystem gadget_subsys = {
979 .su_group = {
980 .cg_item = {
981 .ci_namebuf = "usb_gadget",
982 .ci_type = &gadgets_type,
983 },
984 },
985 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
986};
987
988static int __init gadget_cfs_init(void)
989{
990 int ret;
991
992 config_group_init(&gadget_subsys.su_group);
993
994 ret = configfs_register_subsystem(&gadget_subsys);
995 return ret;
996}
997module_init(gadget_cfs_init);
998
999static void __exit gadget_cfs_exit(void)
1000{
1001 configfs_unregister_subsystem(&gadget_subsys);
1002}
1003module_exit(gadget_cfs_exit);
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 8cf0c0f6fa1f..a792e322f4f1 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -912,7 +912,6 @@ static int dummy_udc_start(struct usb_gadget *g,
912 dum->devstatus = 0; 912 dum->devstatus = 0;
913 913
914 dum->driver = driver; 914 dum->driver = driver;
915 dum->gadget.dev.driver = &driver->driver;
916 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n", 915 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
917 driver->driver.name); 916 driver->driver.name);
918 return 0; 917 return 0;
@@ -927,7 +926,6 @@ static int dummy_udc_stop(struct usb_gadget *g,
927 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n", 926 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
928 driver->driver.name); 927 driver->driver.name);
929 928
930 dum->gadget.dev.driver = NULL;
931 dum->driver = NULL; 929 dum->driver = NULL;
932 930
933 return 0; 931 return 0;
@@ -937,11 +935,6 @@ static int dummy_udc_stop(struct usb_gadget *g,
937 935
938/* The gadget structure is stored inside the hcd structure and will be 936/* The gadget structure is stored inside the hcd structure and will be
939 * released along with it. */ 937 * released along with it. */
940static void dummy_gadget_release(struct device *dev)
941{
942 return;
943}
944
945static void init_dummy_udc_hw(struct dummy *dum) 938static void init_dummy_udc_hw(struct dummy *dum)
946{ 939{
947 int i; 940 int i;
@@ -984,15 +977,7 @@ static int dummy_udc_probe(struct platform_device *pdev)
984 dum->gadget.ops = &dummy_ops; 977 dum->gadget.ops = &dummy_ops;
985 dum->gadget.max_speed = USB_SPEED_SUPER; 978 dum->gadget.max_speed = USB_SPEED_SUPER;
986 979
987 dev_set_name(&dum->gadget.dev, "gadget");
988 dum->gadget.dev.parent = &pdev->dev; 980 dum->gadget.dev.parent = &pdev->dev;
989 dum->gadget.dev.release = dummy_gadget_release;
990 rc = device_register(&dum->gadget.dev);
991 if (rc < 0) {
992 put_device(&dum->gadget.dev);
993 return rc;
994 }
995
996 init_dummy_udc_hw(dum); 981 init_dummy_udc_hw(dum);
997 982
998 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget); 983 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
@@ -1008,7 +993,6 @@ static int dummy_udc_probe(struct platform_device *pdev)
1008err_dev: 993err_dev:
1009 usb_del_gadget_udc(&dum->gadget); 994 usb_del_gadget_udc(&dum->gadget);
1010err_udc: 995err_udc:
1011 device_unregister(&dum->gadget.dev);
1012 return rc; 996 return rc;
1013} 997}
1014 998
@@ -1019,7 +1003,6 @@ static int dummy_udc_remove(struct platform_device *pdev)
1019 usb_del_gadget_udc(&dum->gadget); 1003 usb_del_gadget_udc(&dum->gadget);
1020 platform_set_drvdata(pdev, NULL); 1004 platform_set_drvdata(pdev, NULL);
1021 device_remove_file(&dum->gadget.dev, &dev_attr_function); 1005 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1022 device_unregister(&dum->gadget.dev);
1023 return 0; 1006 return 0;
1024} 1007}
1025 1008
@@ -1923,7 +1906,7 @@ done:
1923} 1906}
1924 1907
1925/* usb 3.0 root hub device descriptor */ 1908/* usb 3.0 root hub device descriptor */
1926struct { 1909static struct {
1927 struct usb_bos_descriptor bos; 1910 struct usb_bos_descriptor bos;
1928 struct usb_ss_cap_descriptor ss_cap; 1911 struct usb_ss_cap_descriptor ss_cap;
1929} __packed usb3_bos_desc = { 1912} __packed usb3_bos_desc = {
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index 18c3f423706e..56c8ecae9bc3 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -207,7 +207,7 @@ static struct usb_gadget_strings *dev_strings[] = {
207}; 207};
208 208
209static u8 hostaddr[ETH_ALEN]; 209static u8 hostaddr[ETH_ALEN];
210 210static struct eth_dev *the_dev;
211/*-------------------------------------------------------------------------*/ 211/*-------------------------------------------------------------------------*/
212 212
213/* 213/*
@@ -224,7 +224,7 @@ static int __init rndis_do_config(struct usb_configuration *c)
224 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 224 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
225 } 225 }
226 226
227 return rndis_bind_config(c, hostaddr); 227 return rndis_bind_config(c, hostaddr, the_dev);
228} 228}
229 229
230static struct usb_configuration rndis_config_driver = { 230static struct usb_configuration rndis_config_driver = {
@@ -257,11 +257,11 @@ static int __init eth_do_config(struct usb_configuration *c)
257 } 257 }
258 258
259 if (use_eem) 259 if (use_eem)
260 return eem_bind_config(c); 260 return eem_bind_config(c, the_dev);
261 else if (can_support_ecm(c->cdev->gadget)) 261 else if (can_support_ecm(c->cdev->gadget))
262 return ecm_bind_config(c, hostaddr); 262 return ecm_bind_config(c, hostaddr, the_dev);
263 else 263 else
264 return geth_bind_config(c, hostaddr); 264 return geth_bind_config(c, hostaddr, the_dev);
265} 265}
266 266
267static struct usb_configuration eth_config_driver = { 267static struct usb_configuration eth_config_driver = {
@@ -279,9 +279,9 @@ static int __init eth_bind(struct usb_composite_dev *cdev)
279 int status; 279 int status;
280 280
281 /* set up network link layer */ 281 /* set up network link layer */
282 status = gether_setup(cdev->gadget, hostaddr); 282 the_dev = gether_setup(cdev->gadget, hostaddr);
283 if (status < 0) 283 if (IS_ERR(the_dev))
284 return status; 284 return PTR_ERR(the_dev);
285 285
286 /* set up main config label and device descriptor */ 286 /* set up main config label and device descriptor */
287 if (use_eem) { 287 if (use_eem) {
@@ -338,13 +338,13 @@ static int __init eth_bind(struct usb_composite_dev *cdev)
338 return 0; 338 return 0;
339 339
340fail: 340fail:
341 gether_cleanup(); 341 gether_cleanup(the_dev);
342 return status; 342 return status;
343} 343}
344 344
345static int __exit eth_unbind(struct usb_composite_dev *cdev) 345static int __exit eth_unbind(struct usb_composite_dev *cdev)
346{ 346{
347 gether_cleanup(); 347 gether_cleanup(the_dev);
348 return 0; 348 return 0;
349} 349}
350 350
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index 1ae180baa597..4b7e33e5d9c6 100644
--- a/drivers/usb/gadget/f_acm.c
+++ b/drivers/usb/gadget/f_acm.c
@@ -715,13 +715,31 @@ fail:
715 return status; 715 return status;
716} 716}
717 717
718static struct f_acm *acm_alloc_basic_func(void) 718static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
719{
720 struct f_acm *acm = func_to_acm(f);
721
722 acm_string_defs[0].id = 0;
723 usb_free_all_descriptors(f);
724 if (acm->notify_req)
725 gs_free_req(acm->notify, acm->notify_req);
726}
727
728static void acm_free_func(struct usb_function *f)
729{
730 struct f_acm *acm = func_to_acm(f);
731
732 kfree(acm);
733}
734
735static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
719{ 736{
720 struct f_acm *acm; 737 struct f_serial_opts *opts;
738 struct f_acm *acm;
721 739
722 acm = kzalloc(sizeof(*acm), GFP_KERNEL); 740 acm = kzalloc(sizeof(*acm), GFP_KERNEL);
723 if (!acm) 741 if (!acm)
724 return NULL; 742 return ERR_PTR(-ENOMEM);
725 743
726 spin_lock_init(&acm->lock); 744 spin_lock_init(&acm->lock);
727 745
@@ -730,109 +748,100 @@ static struct f_acm *acm_alloc_basic_func(void)
730 acm->port.send_break = acm_send_break; 748 acm->port.send_break = acm_send_break;
731 749
732 acm->port.func.name = "acm"; 750 acm->port.func.name = "acm";
751 acm->port.func.strings = acm_strings;
733 /* descriptors are per-instance copies */ 752 /* descriptors are per-instance copies */
734 acm->port.func.bind = acm_bind; 753 acm->port.func.bind = acm_bind;
735 acm->port.func.set_alt = acm_set_alt; 754 acm->port.func.set_alt = acm_set_alt;
736 acm->port.func.setup = acm_setup; 755 acm->port.func.setup = acm_setup;
737 acm->port.func.disable = acm_disable; 756 acm->port.func.disable = acm_disable;
738 757
739 return acm; 758 opts = container_of(fi, struct f_serial_opts, func_inst);
759 acm->port_num = opts->port_num;
760 acm->port.func.unbind = acm_unbind;
761 acm->port.func.free_func = acm_free_func;
762
763 return &acm->port.func;
740} 764}
741 765
742#ifdef USB_FACM_INCLUDED 766static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
743static void
744acm_old_unbind(struct usb_configuration *c, struct usb_function *f)
745{ 767{
746 struct f_acm *acm = func_to_acm(f); 768 return container_of(to_config_group(item), struct f_serial_opts,
747 769 func_inst.group);
748 usb_free_all_descriptors(f);
749 if (acm->notify_req)
750 gs_free_req(acm->notify, acm->notify_req);
751 kfree(acm);
752} 770}
753 771
754/** 772CONFIGFS_ATTR_STRUCT(f_serial_opts);
755 * acm_bind_config - add a CDC ACM function to a configuration 773static ssize_t f_acm_attr_show(struct config_item *item,
756 * @c: the configuration to support the CDC ACM instance 774 struct configfs_attribute *attr,
757 * @port_num: /dev/ttyGS* port this interface will use 775 char *page)
758 * Context: single threaded during gadget setup
759 *
760 * Returns zero on success, else negative errno.
761 *
762 */
763int acm_bind_config(struct usb_configuration *c, u8 port_num)
764{ 776{
765 struct f_acm *acm; 777 struct f_serial_opts *opts = to_f_serial_opts(item);
766 int status; 778 struct f_serial_opts_attribute *f_serial_opts_attr =
767 779 container_of(attr, struct f_serial_opts_attribute, attr);
768 /* allocate and initialize one new instance */ 780 ssize_t ret = 0;
769 acm = acm_alloc_basic_func(); 781
770 if (!acm) 782 if (f_serial_opts_attr->show)
771 return -ENOMEM; 783 ret = f_serial_opts_attr->show(opts, page);
772 784 return ret;
773 acm->port_num = port_num;
774 acm->port.func.unbind = acm_old_unbind;
775
776 status = usb_add_function(c, &acm->port.func);
777 if (status)
778 kfree(acm);
779 return status;
780} 785}
781 786
782#else 787static void acm_attr_release(struct config_item *item)
783
784static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
785{ 788{
786 struct f_acm *acm = func_to_acm(f); 789 struct f_serial_opts *opts = to_f_serial_opts(item);
787 790
788 acm_string_defs[0].id = 0; 791 usb_put_function_instance(&opts->func_inst);
789 usb_free_all_descriptors(f);
790 if (acm->notify_req)
791 gs_free_req(acm->notify, acm->notify_req);
792} 792}
793 793
794static void acm_free_func(struct usb_function *f) 794static struct configfs_item_operations acm_item_ops = {
795{ 795 .release = acm_attr_release,
796 struct f_acm *acm = func_to_acm(f); 796 .show_attribute = f_acm_attr_show,
797};
797 798
798 kfree(acm); 799static ssize_t f_acm_port_num_show(struct f_serial_opts *opts, char *page)
800{
801 return sprintf(page, "%u\n", opts->port_num);
799} 802}
800 803
801static struct usb_function *acm_alloc_func(struct usb_function_instance *fi) 804static struct f_serial_opts_attribute f_acm_port_num =
802{ 805 __CONFIGFS_ATTR_RO(port_num, f_acm_port_num_show);
803 struct f_serial_opts *opts;
804 struct f_acm *acm;
805 806
806 acm = acm_alloc_basic_func();
807 if (!acm)
808 return ERR_PTR(-ENOMEM);
809 807
810 opts = container_of(fi, struct f_serial_opts, func_inst); 808static struct configfs_attribute *acm_attrs[] = {
811 acm->port_num = opts->port_num; 809 &f_acm_port_num.attr,
812 acm->port.func.unbind = acm_unbind; 810 NULL,
813 acm->port.func.free_func = acm_free_func; 811};
814 812
815 return &acm->port.func; 813static struct config_item_type acm_func_type = {
816} 814 .ct_item_ops = &acm_item_ops,
815 .ct_attrs = acm_attrs,
816 .ct_owner = THIS_MODULE,
817};
817 818
818static void acm_free_instance(struct usb_function_instance *fi) 819static void acm_free_instance(struct usb_function_instance *fi)
819{ 820{
820 struct f_serial_opts *opts; 821 struct f_serial_opts *opts;
821 822
822 opts = container_of(fi, struct f_serial_opts, func_inst); 823 opts = container_of(fi, struct f_serial_opts, func_inst);
824 gserial_free_line(opts->port_num);
823 kfree(opts); 825 kfree(opts);
824} 826}
825 827
826static struct usb_function_instance *acm_alloc_instance(void) 828static struct usb_function_instance *acm_alloc_instance(void)
827{ 829{
828 struct f_serial_opts *opts; 830 struct f_serial_opts *opts;
831 int ret;
829 832
830 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 833 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
831 if (!opts) 834 if (!opts)
832 return ERR_PTR(-ENOMEM); 835 return ERR_PTR(-ENOMEM);
833 opts->func_inst.free_func_inst = acm_free_instance; 836 opts->func_inst.free_func_inst = acm_free_instance;
837 ret = gserial_alloc_line(&opts->port_num);
838 if (ret) {
839 kfree(opts);
840 return ERR_PTR(ret);
841 }
842 config_group_init_type_name(&opts->func_inst.group, "",
843 &acm_func_type);
834 return &opts->func_inst; 844 return &opts->func_inst;
835} 845}
836DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func); 846DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
837MODULE_LICENSE("GPL"); 847MODULE_LICENSE("GPL");
838#endif
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
index 83420a310fb7..d893d6929079 100644
--- a/drivers/usb/gadget/f_ecm.c
+++ b/drivers/usb/gadget/f_ecm.c
@@ -824,7 +824,8 @@ ecm_unbind(struct usb_configuration *c, struct usb_function *f)
824 * for calling @gether_cleanup() before module unload. 824 * for calling @gether_cleanup() before module unload.
825 */ 825 */
826int 826int
827ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 827ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
828 struct eth_dev *dev)
828{ 829{
829 struct f_ecm *ecm; 830 struct f_ecm *ecm;
830 int status; 831 int status;
@@ -852,6 +853,7 @@ ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
852 snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr); 853 snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
853 ecm_string_defs[1].s = ecm->ethaddr; 854 ecm_string_defs[1].s = ecm->ethaddr;
854 855
856 ecm->port.ioport = dev;
855 ecm->port.cdc_filter = DEFAULT_FILTER; 857 ecm->port.cdc_filter = DEFAULT_FILTER;
856 858
857 ecm->port.func.name = "cdc_ethernet"; 859 ecm->port.func.name = "cdc_ethernet";
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
index cf0ebee85563..f4e0bbef602a 100644
--- a/drivers/usb/gadget/f_eem.c
+++ b/drivers/usb/gadget/f_eem.c
@@ -528,7 +528,7 @@ error:
528 * Caller must have called @gether_setup(). Caller is also responsible 528 * Caller must have called @gether_setup(). Caller is also responsible
529 * for calling @gether_cleanup() before module unload. 529 * for calling @gether_cleanup() before module unload.
530 */ 530 */
531int __init eem_bind_config(struct usb_configuration *c) 531int __init eem_bind_config(struct usb_configuration *c, struct eth_dev *dev)
532{ 532{
533 struct f_eem *eem; 533 struct f_eem *eem;
534 int status; 534 int status;
@@ -549,6 +549,7 @@ int __init eem_bind_config(struct usb_configuration *c)
549 if (!eem) 549 if (!eem)
550 return -ENOMEM; 550 return -ENOMEM;
551 551
552 eem->port.ioport = dev;
552 eem->port.cdc_filter = DEFAULT_FILTER; 553 eem->port.cdc_filter = DEFAULT_FILTER;
553 554
554 eem->port.func.name = "cdc_eem"; 555 eem->port.func.name = "cdc_eem";
diff --git a/drivers/usb/gadget/f_ncm.c b/drivers/usb/gadget/f_ncm.c
index 5e7557e23ecc..ee19bc8d0040 100644
--- a/drivers/usb/gadget/f_ncm.c
+++ b/drivers/usb/gadget/f_ncm.c
@@ -1287,7 +1287,8 @@ ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1287 * Caller must have called @gether_setup(). Caller is also responsible 1287 * Caller must have called @gether_setup(). Caller is also responsible
1288 * for calling @gether_cleanup() before module unload. 1288 * for calling @gether_cleanup() before module unload.
1289 */ 1289 */
1290int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 1290int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
1291 struct eth_dev *dev)
1291{ 1292{
1292 struct f_ncm *ncm; 1293 struct f_ncm *ncm;
1293 int status; 1294 int status;
@@ -1321,6 +1322,7 @@ int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1321 1322
1322 spin_lock_init(&ncm->lock); 1323 spin_lock_init(&ncm->lock);
1323 ncm_reset_values(ncm); 1324 ncm_reset_values(ncm);
1325 ncm->port.ioport = dev;
1324 ncm->port.is_fixed = true; 1326 ncm->port.is_fixed = true;
1325 1327
1326 ncm->port.func.name = "cdc_network"; 1328 ncm->port.func.name = "cdc_network";
diff --git a/drivers/usb/gadget/f_obex.c b/drivers/usb/gadget/f_obex.c
index 36a004563b82..29a348a2a294 100644
--- a/drivers/usb/gadget/f_obex.c
+++ b/drivers/usb/gadget/f_obex.c
@@ -72,7 +72,7 @@ static struct usb_gadget_strings *obex_strings[] = {
72 72
73/*-------------------------------------------------------------------------*/ 73/*-------------------------------------------------------------------------*/
74 74
75static struct usb_interface_descriptor obex_control_intf __initdata = { 75static struct usb_interface_descriptor obex_control_intf = {
76 .bLength = sizeof(obex_control_intf), 76 .bLength = sizeof(obex_control_intf),
77 .bDescriptorType = USB_DT_INTERFACE, 77 .bDescriptorType = USB_DT_INTERFACE,
78 .bInterfaceNumber = 0, 78 .bInterfaceNumber = 0,
@@ -83,7 +83,7 @@ static struct usb_interface_descriptor obex_control_intf __initdata = {
83 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX, 83 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
84}; 84};
85 85
86static struct usb_interface_descriptor obex_data_nop_intf __initdata = { 86static struct usb_interface_descriptor obex_data_nop_intf = {
87 .bLength = sizeof(obex_data_nop_intf), 87 .bLength = sizeof(obex_data_nop_intf),
88 .bDescriptorType = USB_DT_INTERFACE, 88 .bDescriptorType = USB_DT_INTERFACE,
89 .bInterfaceNumber = 1, 89 .bInterfaceNumber = 1,
@@ -93,7 +93,7 @@ static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
93 .bInterfaceClass = USB_CLASS_CDC_DATA, 93 .bInterfaceClass = USB_CLASS_CDC_DATA,
94}; 94};
95 95
96static struct usb_interface_descriptor obex_data_intf __initdata = { 96static struct usb_interface_descriptor obex_data_intf = {
97 .bLength = sizeof(obex_data_intf), 97 .bLength = sizeof(obex_data_intf),
98 .bDescriptorType = USB_DT_INTERFACE, 98 .bDescriptorType = USB_DT_INTERFACE,
99 .bInterfaceNumber = 2, 99 .bInterfaceNumber = 2,
@@ -103,14 +103,14 @@ static struct usb_interface_descriptor obex_data_intf __initdata = {
103 .bInterfaceClass = USB_CLASS_CDC_DATA, 103 .bInterfaceClass = USB_CLASS_CDC_DATA,
104}; 104};
105 105
106static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = { 106static struct usb_cdc_header_desc obex_cdc_header_desc = {
107 .bLength = sizeof(obex_cdc_header_desc), 107 .bLength = sizeof(obex_cdc_header_desc),
108 .bDescriptorType = USB_DT_CS_INTERFACE, 108 .bDescriptorType = USB_DT_CS_INTERFACE,
109 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 109 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
110 .bcdCDC = cpu_to_le16(0x0120), 110 .bcdCDC = cpu_to_le16(0x0120),
111}; 111};
112 112
113static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = { 113static struct usb_cdc_union_desc obex_cdc_union_desc = {
114 .bLength = sizeof(obex_cdc_union_desc), 114 .bLength = sizeof(obex_cdc_union_desc),
115 .bDescriptorType = USB_DT_CS_INTERFACE, 115 .bDescriptorType = USB_DT_CS_INTERFACE,
116 .bDescriptorSubType = USB_CDC_UNION_TYPE, 116 .bDescriptorSubType = USB_CDC_UNION_TYPE,
@@ -118,7 +118,7 @@ static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
118 .bSlaveInterface0 = 2, 118 .bSlaveInterface0 = 2,
119}; 119};
120 120
121static struct usb_cdc_obex_desc obex_desc __initdata = { 121static struct usb_cdc_obex_desc obex_desc = {
122 .bLength = sizeof(obex_desc), 122 .bLength = sizeof(obex_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE, 123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_OBEX_TYPE, 124 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
@@ -127,7 +127,7 @@ static struct usb_cdc_obex_desc obex_desc __initdata = {
127 127
128/* High-Speed Support */ 128/* High-Speed Support */
129 129
130static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = { 130static struct usb_endpoint_descriptor obex_hs_ep_out_desc = {
131 .bLength = USB_DT_ENDPOINT_SIZE, 131 .bLength = USB_DT_ENDPOINT_SIZE,
132 .bDescriptorType = USB_DT_ENDPOINT, 132 .bDescriptorType = USB_DT_ENDPOINT,
133 133
@@ -136,7 +136,7 @@ static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
136 .wMaxPacketSize = cpu_to_le16(512), 136 .wMaxPacketSize = cpu_to_le16(512),
137}; 137};
138 138
139static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = { 139static struct usb_endpoint_descriptor obex_hs_ep_in_desc = {
140 .bLength = USB_DT_ENDPOINT_SIZE, 140 .bLength = USB_DT_ENDPOINT_SIZE,
141 .bDescriptorType = USB_DT_ENDPOINT, 141 .bDescriptorType = USB_DT_ENDPOINT,
142 142
@@ -145,7 +145,7 @@ static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
145 .wMaxPacketSize = cpu_to_le16(512), 145 .wMaxPacketSize = cpu_to_le16(512),
146}; 146};
147 147
148static struct usb_descriptor_header *hs_function[] __initdata = { 148static struct usb_descriptor_header *hs_function[] = {
149 (struct usb_descriptor_header *) &obex_control_intf, 149 (struct usb_descriptor_header *) &obex_control_intf,
150 (struct usb_descriptor_header *) &obex_cdc_header_desc, 150 (struct usb_descriptor_header *) &obex_cdc_header_desc,
151 (struct usb_descriptor_header *) &obex_desc, 151 (struct usb_descriptor_header *) &obex_desc,
@@ -160,7 +160,7 @@ static struct usb_descriptor_header *hs_function[] __initdata = {
160 160
161/* Full-Speed Support */ 161/* Full-Speed Support */
162 162
163static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = { 163static struct usb_endpoint_descriptor obex_fs_ep_in_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE, 164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT, 165 .bDescriptorType = USB_DT_ENDPOINT,
166 166
@@ -168,7 +168,7 @@ static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
168 .bmAttributes = USB_ENDPOINT_XFER_BULK, 168 .bmAttributes = USB_ENDPOINT_XFER_BULK,
169}; 169};
170 170
171static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = { 171static struct usb_endpoint_descriptor obex_fs_ep_out_desc = {
172 .bLength = USB_DT_ENDPOINT_SIZE, 172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT, 173 .bDescriptorType = USB_DT_ENDPOINT,
174 174
@@ -176,7 +176,7 @@ static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
176 .bmAttributes = USB_ENDPOINT_XFER_BULK, 176 .bmAttributes = USB_ENDPOINT_XFER_BULK,
177}; 177};
178 178
179static struct usb_descriptor_header *fs_function[] __initdata = { 179static struct usb_descriptor_header *fs_function[] = {
180 (struct usb_descriptor_header *) &obex_control_intf, 180 (struct usb_descriptor_header *) &obex_control_intf,
181 (struct usb_descriptor_header *) &obex_cdc_header_desc, 181 (struct usb_descriptor_header *) &obex_cdc_header_desc,
182 (struct usb_descriptor_header *) &obex_desc, 182 (struct usb_descriptor_header *) &obex_desc,
@@ -290,14 +290,43 @@ static void obex_disconnect(struct gserial *g)
290 290
291/*-------------------------------------------------------------------------*/ 291/*-------------------------------------------------------------------------*/
292 292
293static int __init 293/* Some controllers can't support CDC OBEX ... */
294obex_bind(struct usb_configuration *c, struct usb_function *f) 294static inline bool can_support_obex(struct usb_configuration *c)
295{
296 /* Since the first interface is a NOP, we can ignore the
297 * issue of multi-interface support on most controllers.
298 *
299 * Altsettings are mandatory, however...
300 */
301 if (!gadget_supports_altsettings(c->cdev->gadget))
302 return false;
303
304 /* everything else is *probably* fine ... */
305 return true;
306}
307
308static int obex_bind(struct usb_configuration *c, struct usb_function *f)
295{ 309{
296 struct usb_composite_dev *cdev = c->cdev; 310 struct usb_composite_dev *cdev = c->cdev;
297 struct f_obex *obex = func_to_obex(f); 311 struct f_obex *obex = func_to_obex(f);
298 int status; 312 int status;
299 struct usb_ep *ep; 313 struct usb_ep *ep;
300 314
315 if (!can_support_obex(c))
316 return -EINVAL;
317
318 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
319 status = usb_string_ids_tab(c->cdev, obex_string_defs);
320 if (status < 0)
321 return status;
322 obex_control_intf.iInterface =
323 obex_string_defs[OBEX_CTRL_IDX].id;
324
325 status = obex_string_defs[OBEX_DATA_IDX].id;
326 obex_data_nop_intf.iInterface = status;
327 obex_data_intf.iInterface = status;
328 }
329
301 /* allocate instance-specific interface IDs, and patch descriptors */ 330 /* allocate instance-specific interface IDs, and patch descriptors */
302 331
303 status = usb_interface_id(c, f); 332 status = usb_interface_id(c, f);
@@ -376,29 +405,16 @@ fail:
376 return status; 405 return status;
377} 406}
378 407
408#ifdef USBF_OBEX_INCLUDED
409
379static void 410static void
380obex_unbind(struct usb_configuration *c, struct usb_function *f) 411obex_old_unbind(struct usb_configuration *c, struct usb_function *f)
381{ 412{
382 obex_string_defs[OBEX_CTRL_IDX].id = 0; 413 obex_string_defs[OBEX_CTRL_IDX].id = 0;
383 usb_free_all_descriptors(f); 414 usb_free_all_descriptors(f);
384 kfree(func_to_obex(f)); 415 kfree(func_to_obex(f));
385} 416}
386 417
387/* Some controllers can't support CDC OBEX ... */
388static inline bool can_support_obex(struct usb_configuration *c)
389{
390 /* Since the first interface is a NOP, we can ignore the
391 * issue of multi-interface support on most controllers.
392 *
393 * Altsettings are mandatory, however...
394 */
395 if (!gadget_supports_altsettings(c->cdev->gadget))
396 return false;
397
398 /* everything else is *probably* fine ... */
399 return true;
400}
401
402/** 418/**
403 * obex_bind_config - add a CDC OBEX function to a configuration 419 * obex_bind_config - add a CDC OBEX function to a configuration
404 * @c: the configuration to support the CDC OBEX instance 420 * @c: the configuration to support the CDC OBEX instance
@@ -412,21 +428,6 @@ int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
412 struct f_obex *obex; 428 struct f_obex *obex;
413 int status; 429 int status;
414 430
415 if (!can_support_obex(c))
416 return -EINVAL;
417
418 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
419 status = usb_string_ids_tab(c->cdev, obex_string_defs);
420 if (status < 0)
421 return status;
422 obex_control_intf.iInterface =
423 obex_string_defs[OBEX_CTRL_IDX].id;
424
425 status = obex_string_defs[OBEX_DATA_IDX].id;
426 obex_data_nop_intf.iInterface = status;
427 obex_data_intf.iInterface = status;
428 }
429
430 /* allocate and initialize one new instance */ 431 /* allocate and initialize one new instance */
431 obex = kzalloc(sizeof *obex, GFP_KERNEL); 432 obex = kzalloc(sizeof *obex, GFP_KERNEL);
432 if (!obex) 433 if (!obex)
@@ -441,7 +442,7 @@ int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
441 obex->port.func.strings = obex_strings; 442 obex->port.func.strings = obex_strings;
442 /* descriptors are per-instance copies */ 443 /* descriptors are per-instance copies */
443 obex->port.func.bind = obex_bind; 444 obex->port.func.bind = obex_bind;
444 obex->port.func.unbind = obex_unbind; 445 obex->port.func.unbind = obex_old_unbind;
445 obex->port.func.set_alt = obex_set_alt; 446 obex->port.func.set_alt = obex_set_alt;
446 obex->port.func.get_alt = obex_get_alt; 447 obex->port.func.get_alt = obex_get_alt;
447 obex->port.func.disable = obex_disable; 448 obex->port.func.disable = obex_disable;
@@ -453,5 +454,138 @@ int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
453 return status; 454 return status;
454} 455}
455 456
457#else
458
459static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
460{
461 return container_of(to_config_group(item), struct f_serial_opts,
462 func_inst.group);
463}
464
465CONFIGFS_ATTR_STRUCT(f_serial_opts);
466static ssize_t f_obex_attr_show(struct config_item *item,
467 struct configfs_attribute *attr,
468 char *page)
469{
470 struct f_serial_opts *opts = to_f_serial_opts(item);
471 struct f_serial_opts_attribute *f_serial_opts_attr =
472 container_of(attr, struct f_serial_opts_attribute, attr);
473 ssize_t ret = 0;
474
475 if (f_serial_opts_attr->show)
476 ret = f_serial_opts_attr->show(opts, page);
477
478 return ret;
479}
480
481static void obex_attr_release(struct config_item *item)
482{
483 struct f_serial_opts *opts = to_f_serial_opts(item);
484
485 usb_put_function_instance(&opts->func_inst);
486}
487
488static struct configfs_item_operations obex_item_ops = {
489 .release = obex_attr_release,
490 .show_attribute = f_obex_attr_show,
491};
492
493static ssize_t f_obex_port_num_show(struct f_serial_opts *opts, char *page)
494{
495 return sprintf(page, "%u\n", opts->port_num);
496}
497
498static struct f_serial_opts_attribute f_obex_port_num =
499 __CONFIGFS_ATTR_RO(port_num, f_obex_port_num_show);
500
501static struct configfs_attribute *acm_attrs[] = {
502 &f_obex_port_num.attr,
503 NULL,
504};
505
506static struct config_item_type obex_func_type = {
507 .ct_item_ops = &obex_item_ops,
508 .ct_attrs = acm_attrs,
509 .ct_owner = THIS_MODULE,
510};
511
512static void obex_free_inst(struct usb_function_instance *f)
513{
514 struct f_serial_opts *opts;
515
516 opts = container_of(f, struct f_serial_opts, func_inst);
517 gserial_free_line(opts->port_num);
518 kfree(opts);
519}
520
521static struct usb_function_instance *obex_alloc_inst(void)
522{
523 struct f_serial_opts *opts;
524 int ret;
525
526 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
527 if (!opts)
528 return ERR_PTR(-ENOMEM);
529
530 opts->func_inst.free_func_inst = obex_free_inst;
531 ret = gserial_alloc_line(&opts->port_num);
532 if (ret) {
533 kfree(opts);
534 return ERR_PTR(ret);
535 }
536 config_group_init_type_name(&opts->func_inst.group, "",
537 &obex_func_type);
538
539 return &opts->func_inst;
540}
541
542static void obex_free(struct usb_function *f)
543{
544 struct f_obex *obex;
545
546 obex = func_to_obex(f);
547 kfree(obex);
548}
549
550static void obex_unbind(struct usb_configuration *c, struct usb_function *f)
551{
552 obex_string_defs[OBEX_CTRL_IDX].id = 0;
553 usb_free_all_descriptors(f);
554}
555
556struct usb_function *obex_alloc(struct usb_function_instance *fi)
557{
558 struct f_obex *obex;
559 struct f_serial_opts *opts;
560
561 /* allocate and initialize one new instance */
562 obex = kzalloc(sizeof(*obex), GFP_KERNEL);
563 if (!obex)
564 return ERR_PTR(-ENOMEM);
565
566 opts = container_of(fi, struct f_serial_opts, func_inst);
567
568 obex->port_num = opts->port_num;
569
570 obex->port.connect = obex_connect;
571 obex->port.disconnect = obex_disconnect;
572
573 obex->port.func.name = "obex";
574 obex->port.func.strings = obex_strings;
575 /* descriptors are per-instance copies */
576 obex->port.func.bind = obex_bind;
577 obex->port.func.unbind = obex_unbind;
578 obex->port.func.set_alt = obex_set_alt;
579 obex->port.func.get_alt = obex_get_alt;
580 obex->port.func.disable = obex_disable;
581 obex->port.func.free_func = obex_free;
582
583 return &obex->port.func;
584}
585
586DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc);
587
588#endif
589
456MODULE_AUTHOR("Felipe Balbi"); 590MODULE_AUTHOR("Felipe Balbi");
457MODULE_LICENSE("GPL"); 591MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index cc9c49c57c80..36e8c44d8e5e 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -813,7 +813,7 @@ static inline bool can_support_rndis(struct usb_configuration *c)
813 813
814int 814int
815rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], 815rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
816 u32 vendorID, const char *manufacturer) 816 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
817{ 817{
818 struct f_rndis *rndis; 818 struct f_rndis *rndis;
819 int status; 819 int status;
@@ -846,6 +846,7 @@ rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
846 rndis->vendorID = vendorID; 846 rndis->vendorID = vendorID;
847 rndis->manufacturer = manufacturer; 847 rndis->manufacturer = manufacturer;
848 848
849 rndis->port.ioport = dev;
849 /* RNDIS activates when the host changes this filter */ 850 /* RNDIS activates when the host changes this filter */
850 rndis->port.cdc_filter = 0; 851 rndis->port.cdc_filter = 0;
851 852
diff --git a/drivers/usb/gadget/f_serial.c b/drivers/usb/gadget/f_serial.c
index da33cfb3031d..981113c9924d 100644
--- a/drivers/usb/gadget/f_serial.c
+++ b/drivers/usb/gadget/f_serial.c
@@ -12,6 +12,7 @@
12 12
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/module.h>
15#include <linux/device.h> 16#include <linux/device.h>
16 17
17#include "u_serial.h" 18#include "u_serial.h"
@@ -42,7 +43,7 @@ static inline struct f_gser *func_to_gser(struct usb_function *f)
42 43
43/* interface descriptor: */ 44/* interface descriptor: */
44 45
45static struct usb_interface_descriptor gser_interface_desc __initdata = { 46static struct usb_interface_descriptor gser_interface_desc = {
46 .bLength = USB_DT_INTERFACE_SIZE, 47 .bLength = USB_DT_INTERFACE_SIZE,
47 .bDescriptorType = USB_DT_INTERFACE, 48 .bDescriptorType = USB_DT_INTERFACE,
48 /* .bInterfaceNumber = DYNAMIC */ 49 /* .bInterfaceNumber = DYNAMIC */
@@ -55,21 +56,21 @@ static struct usb_interface_descriptor gser_interface_desc __initdata = {
55 56
56/* full speed support: */ 57/* full speed support: */
57 58
58static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = { 59static struct usb_endpoint_descriptor gser_fs_in_desc = {
59 .bLength = USB_DT_ENDPOINT_SIZE, 60 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT, 61 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN, 62 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK, 63 .bmAttributes = USB_ENDPOINT_XFER_BULK,
63}; 64};
64 65
65static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = { 66static struct usb_endpoint_descriptor gser_fs_out_desc = {
66 .bLength = USB_DT_ENDPOINT_SIZE, 67 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT, 68 .bDescriptorType = USB_DT_ENDPOINT,
68 .bEndpointAddress = USB_DIR_OUT, 69 .bEndpointAddress = USB_DIR_OUT,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK, 70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
70}; 71};
71 72
72static struct usb_descriptor_header *gser_fs_function[] __initdata = { 73static struct usb_descriptor_header *gser_fs_function[] = {
73 (struct usb_descriptor_header *) &gser_interface_desc, 74 (struct usb_descriptor_header *) &gser_interface_desc,
74 (struct usb_descriptor_header *) &gser_fs_in_desc, 75 (struct usb_descriptor_header *) &gser_fs_in_desc,
75 (struct usb_descriptor_header *) &gser_fs_out_desc, 76 (struct usb_descriptor_header *) &gser_fs_out_desc,
@@ -78,47 +79,47 @@ static struct usb_descriptor_header *gser_fs_function[] __initdata = {
78 79
79/* high speed support: */ 80/* high speed support: */
80 81
81static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = { 82static struct usb_endpoint_descriptor gser_hs_in_desc = {
82 .bLength = USB_DT_ENDPOINT_SIZE, 83 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT, 84 .bDescriptorType = USB_DT_ENDPOINT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK, 85 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 .wMaxPacketSize = cpu_to_le16(512), 86 .wMaxPacketSize = cpu_to_le16(512),
86}; 87};
87 88
88static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = { 89static struct usb_endpoint_descriptor gser_hs_out_desc = {
89 .bLength = USB_DT_ENDPOINT_SIZE, 90 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT, 91 .bDescriptorType = USB_DT_ENDPOINT,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK, 92 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = cpu_to_le16(512), 93 .wMaxPacketSize = cpu_to_le16(512),
93}; 94};
94 95
95static struct usb_descriptor_header *gser_hs_function[] __initdata = { 96static struct usb_descriptor_header *gser_hs_function[] = {
96 (struct usb_descriptor_header *) &gser_interface_desc, 97 (struct usb_descriptor_header *) &gser_interface_desc,
97 (struct usb_descriptor_header *) &gser_hs_in_desc, 98 (struct usb_descriptor_header *) &gser_hs_in_desc,
98 (struct usb_descriptor_header *) &gser_hs_out_desc, 99 (struct usb_descriptor_header *) &gser_hs_out_desc,
99 NULL, 100 NULL,
100}; 101};
101 102
102static struct usb_endpoint_descriptor gser_ss_in_desc __initdata = { 103static struct usb_endpoint_descriptor gser_ss_in_desc = {
103 .bLength = USB_DT_ENDPOINT_SIZE, 104 .bLength = USB_DT_ENDPOINT_SIZE,
104 .bDescriptorType = USB_DT_ENDPOINT, 105 .bDescriptorType = USB_DT_ENDPOINT,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK, 106 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = cpu_to_le16(1024), 107 .wMaxPacketSize = cpu_to_le16(1024),
107}; 108};
108 109
109static struct usb_endpoint_descriptor gser_ss_out_desc __initdata = { 110static struct usb_endpoint_descriptor gser_ss_out_desc = {
110 .bLength = USB_DT_ENDPOINT_SIZE, 111 .bLength = USB_DT_ENDPOINT_SIZE,
111 .bDescriptorType = USB_DT_ENDPOINT, 112 .bDescriptorType = USB_DT_ENDPOINT,
112 .bmAttributes = USB_ENDPOINT_XFER_BULK, 113 .bmAttributes = USB_ENDPOINT_XFER_BULK,
113 .wMaxPacketSize = cpu_to_le16(1024), 114 .wMaxPacketSize = cpu_to_le16(1024),
114}; 115};
115 116
116static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc __initdata = { 117static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
117 .bLength = sizeof gser_ss_bulk_comp_desc, 118 .bLength = sizeof gser_ss_bulk_comp_desc,
118 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 119 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
119}; 120};
120 121
121static struct usb_descriptor_header *gser_ss_function[] __initdata = { 122static struct usb_descriptor_header *gser_ss_function[] = {
122 (struct usb_descriptor_header *) &gser_interface_desc, 123 (struct usb_descriptor_header *) &gser_interface_desc,
123 (struct usb_descriptor_header *) &gser_ss_in_desc, 124 (struct usb_descriptor_header *) &gser_ss_in_desc,
124 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc, 125 (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
@@ -183,14 +184,25 @@ static void gser_disable(struct usb_function *f)
183 184
184/* serial function driver setup/binding */ 185/* serial function driver setup/binding */
185 186
186static int __init 187static int gser_bind(struct usb_configuration *c, struct usb_function *f)
187gser_bind(struct usb_configuration *c, struct usb_function *f)
188{ 188{
189 struct usb_composite_dev *cdev = c->cdev; 189 struct usb_composite_dev *cdev = c->cdev;
190 struct f_gser *gser = func_to_gser(f); 190 struct f_gser *gser = func_to_gser(f);
191 int status; 191 int status;
192 struct usb_ep *ep; 192 struct usb_ep *ep;
193 193
194 /* REVISIT might want instance-specific strings to help
195 * distinguish instances ...
196 */
197
198 /* maybe allocate device-global string ID */
199 if (gser_string_defs[0].id == 0) {
200 status = usb_string_id(c->cdev);
201 if (status < 0)
202 return status;
203 gser_string_defs[0].id = status;
204 }
205
194 /* allocate instance-specific interface IDs */ 206 /* allocate instance-specific interface IDs */
195 status = usb_interface_id(c, f); 207 status = usb_interface_id(c, f);
196 if (status < 0) 208 if (status < 0)
@@ -246,44 +258,115 @@ fail:
246 return status; 258 return status;
247} 259}
248 260
249static void 261static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
250gser_unbind(struct usb_configuration *c, struct usb_function *f)
251{ 262{
252 usb_free_all_descriptors(f); 263 return container_of(to_config_group(item), struct f_serial_opts,
253 kfree(func_to_gser(f)); 264 func_inst.group);
254} 265}
255 266
256/** 267CONFIGFS_ATTR_STRUCT(f_serial_opts);
257 * gser_bind_config - add a generic serial function to a configuration 268static ssize_t f_serial_attr_show(struct config_item *item,
258 * @c: the configuration to support the serial instance 269 struct configfs_attribute *attr,
259 * @port_num: /dev/ttyGS* port this interface will use 270 char *page)
260 * Context: single threaded during gadget setup
261 *
262 * Returns zero on success, else negative errno.
263 */
264int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
265{ 271{
266 struct f_gser *gser; 272 struct f_serial_opts *opts = to_f_serial_opts(item);
267 int status; 273 struct f_serial_opts_attribute *f_serial_opts_attr =
274 container_of(attr, struct f_serial_opts_attribute, attr);
275 ssize_t ret = 0;
268 276
269 /* REVISIT might want instance-specific strings to help 277 if (f_serial_opts_attr->show)
270 * distinguish instances ... 278 ret = f_serial_opts_attr->show(opts, page);
271 */
272 279
273 /* maybe allocate device-global string ID */ 280 return ret;
274 if (gser_string_defs[0].id == 0) { 281}
275 status = usb_string_id(c->cdev); 282
276 if (status < 0) 283static void serial_attr_release(struct config_item *item)
277 return status; 284{
278 gser_string_defs[0].id = status; 285 struct f_serial_opts *opts = to_f_serial_opts(item);
286
287 usb_put_function_instance(&opts->func_inst);
288}
289
290static struct configfs_item_operations serial_item_ops = {
291 .release = serial_attr_release,
292 .show_attribute = f_serial_attr_show,
293};
294
295static ssize_t f_serial_port_num_show(struct f_serial_opts *opts, char *page)
296{
297 return sprintf(page, "%u\n", opts->port_num);
298}
299
300static struct f_serial_opts_attribute f_serial_port_num =
301 __CONFIGFS_ATTR_RO(port_num, f_serial_port_num_show);
302
303static struct configfs_attribute *acm_attrs[] = {
304 &f_serial_port_num.attr,
305 NULL,
306};
307
308static struct config_item_type serial_func_type = {
309 .ct_item_ops = &serial_item_ops,
310 .ct_attrs = acm_attrs,
311 .ct_owner = THIS_MODULE,
312};
313
314static void gser_free_inst(struct usb_function_instance *f)
315{
316 struct f_serial_opts *opts;
317
318 opts = container_of(f, struct f_serial_opts, func_inst);
319 gserial_free_line(opts->port_num);
320 kfree(opts);
321}
322
323static struct usb_function_instance *gser_alloc_inst(void)
324{
325 struct f_serial_opts *opts;
326 int ret;
327
328 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
329 if (!opts)
330 return ERR_PTR(-ENOMEM);
331
332 opts->func_inst.free_func_inst = gser_free_inst;
333 ret = gserial_alloc_line(&opts->port_num);
334 if (ret) {
335 kfree(opts);
336 return ERR_PTR(ret);
279 } 337 }
338 config_group_init_type_name(&opts->func_inst.group, "",
339 &serial_func_type);
340
341 return &opts->func_inst;
342}
343
344static void gser_free(struct usb_function *f)
345{
346 struct f_gser *serial;
347
348 serial = func_to_gser(f);
349 kfree(serial);
350}
351
352static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
353{
354 usb_free_all_descriptors(f);
355}
356
357struct usb_function *gser_alloc(struct usb_function_instance *fi)
358{
359 struct f_gser *gser;
360 struct f_serial_opts *opts;
280 361
281 /* allocate and initialize one new instance */ 362 /* allocate and initialize one new instance */
282 gser = kzalloc(sizeof *gser, GFP_KERNEL); 363 gser = kzalloc(sizeof(*gser), GFP_KERNEL);
283 if (!gser) 364 if (!gser)
284 return -ENOMEM; 365 return ERR_PTR(-ENOMEM);
285 366
286 gser->port_num = port_num; 367 opts = container_of(fi, struct f_serial_opts, func_inst);
368
369 gser->port_num = opts->port_num;
287 370
288 gser->port.func.name = "gser"; 371 gser->port.func.name = "gser";
289 gser->port.func.strings = gser_strings; 372 gser->port.func.strings = gser_strings;
@@ -291,9 +374,12 @@ int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
291 gser->port.func.unbind = gser_unbind; 374 gser->port.func.unbind = gser_unbind;
292 gser->port.func.set_alt = gser_set_alt; 375 gser->port.func.set_alt = gser_set_alt;
293 gser->port.func.disable = gser_disable; 376 gser->port.func.disable = gser_disable;
377 gser->port.func.free_func = gser_free;
294 378
295 status = usb_add_function(c, &gser->port.func); 379 return &gser->port.func;
296 if (status)
297 kfree(gser);
298 return status;
299} 380}
381
382DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
383MODULE_LICENSE("GPL");
384MODULE_AUTHOR("Al Borchers");
385MODULE_AUTHOR("David Brownell");
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c
index f172bd152fbb..185d6f5e4e4d 100644
--- a/drivers/usb/gadget/f_subset.c
+++ b/drivers/usb/gadget/f_subset.c
@@ -380,7 +380,8 @@ geth_unbind(struct usb_configuration *c, struct usb_function *f)
380 * Caller must have called @gether_setup(). Caller is also responsible 380 * Caller must have called @gether_setup(). Caller is also responsible
381 * for calling @gether_cleanup() before module unload. 381 * for calling @gether_cleanup() before module unload.
382 */ 382 */
383int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 383int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
384 struct eth_dev *dev)
384{ 385{
385 struct f_gether *geth; 386 struct f_gether *geth;
386 int status; 387 int status;
@@ -406,6 +407,7 @@ int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
406 snprintf(geth->ethaddr, sizeof geth->ethaddr, "%pm", ethaddr); 407 snprintf(geth->ethaddr, sizeof geth->ethaddr, "%pm", ethaddr);
407 geth_string_defs[1].s = geth->ethaddr; 408 geth_string_defs[1].s = geth->ethaddr;
408 409
410 geth->port.ioport = dev;
409 geth->port.cdc_filter = DEFAULT_FILTER; 411 geth->port.cdc_filter = DEFAULT_FILTER;
410 412
411 geth->port.func.name = "cdc_subset"; 413 geth->port.func.name = "cdc_subset";
diff --git a/drivers/usb/gadget/f_uvc.c b/drivers/usb/gadget/f_uvc.c
index 92efd6ec48af..38dcedddc52c 100644
--- a/drivers/usb/gadget/f_uvc.c
+++ b/drivers/usb/gadget/f_uvc.c
@@ -33,19 +33,15 @@ unsigned int uvc_gadget_trace_param;
33/*-------------------------------------------------------------------------*/ 33/*-------------------------------------------------------------------------*/
34 34
35/* module parameters specific to the Video streaming endpoint */ 35/* module parameters specific to the Video streaming endpoint */
36static unsigned streaming_interval = 1; 36static unsigned int streaming_interval = 1;
37module_param(streaming_interval, uint, S_IRUGO|S_IWUSR); 37module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
38MODULE_PARM_DESC(streaming_interval, "1 - 16"); 38MODULE_PARM_DESC(streaming_interval, "1 - 16");
39 39
40static unsigned streaming_maxpacket = 1024; 40static unsigned int streaming_maxpacket = 1024;
41module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR); 41module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
42MODULE_PARM_DESC(streaming_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); 42MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
43 43
44static unsigned streaming_mult; 44static unsigned int streaming_maxburst;
45module_param(streaming_mult, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(streaming_mult, "0 - 2 (hs/ss only)");
47
48static unsigned streaming_maxburst;
49module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR); 45module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
50MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)"); 46MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
51 47
@@ -55,13 +51,11 @@ MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
55 51
56/* string IDs are assigned dynamically */ 52/* string IDs are assigned dynamically */
57 53
58#define UVC_STRING_ASSOCIATION_IDX 0 54#define UVC_STRING_CONTROL_IDX 0
59#define UVC_STRING_CONTROL_IDX 1 55#define UVC_STRING_STREAMING_IDX 1
60#define UVC_STRING_STREAMING_IDX 2
61 56
62static struct usb_string uvc_en_us_strings[] = { 57static struct usb_string uvc_en_us_strings[] = {
63 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera", 58 [UVC_STRING_CONTROL_IDX].s = "UVC Camera",
64 [UVC_STRING_CONTROL_IDX].s = "Video Control",
65 [UVC_STRING_STREAMING_IDX].s = "Video Streaming", 59 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
66 { } 60 { }
67}; 61};
@@ -79,7 +73,7 @@ static struct usb_gadget_strings *uvc_function_strings[] = {
79#define UVC_INTF_VIDEO_CONTROL 0 73#define UVC_INTF_VIDEO_CONTROL 0
80#define UVC_INTF_VIDEO_STREAMING 1 74#define UVC_INTF_VIDEO_STREAMING 1
81 75
82#define STATUS_BYTECOUNT 16 /* 16 bytes status */ 76#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
83 77
84static struct usb_interface_assoc_descriptor uvc_iad __initdata = { 78static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
85 .bLength = sizeof(uvc_iad), 79 .bLength = sizeof(uvc_iad),
@@ -104,20 +98,29 @@ static struct usb_interface_descriptor uvc_control_intf __initdata = {
104 .iInterface = 0, 98 .iInterface = 0,
105}; 99};
106 100
107static struct usb_endpoint_descriptor uvc_fs_control_ep __initdata = { 101static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
108 .bLength = USB_DT_ENDPOINT_SIZE, 102 .bLength = USB_DT_ENDPOINT_SIZE,
109 .bDescriptorType = USB_DT_ENDPOINT, 103 .bDescriptorType = USB_DT_ENDPOINT,
110 .bEndpointAddress = USB_DIR_IN, 104 .bEndpointAddress = USB_DIR_IN,
111 .bmAttributes = USB_ENDPOINT_XFER_INT, 105 .bmAttributes = USB_ENDPOINT_XFER_INT,
112 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), 106 .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
113 .bInterval = 8, 107 .bInterval = 8,
114}; 108};
115 109
110static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
111 .bLength = sizeof(uvc_ss_control_comp),
112 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
113 /* The following 3 values can be tweaked if necessary. */
114 .bMaxBurst = 0,
115 .bmAttributes = 0,
116 .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
117};
118
116static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = { 119static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
117 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE, 120 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
118 .bDescriptorType = USB_DT_CS_ENDPOINT, 121 .bDescriptorType = USB_DT_CS_ENDPOINT,
119 .bDescriptorSubType = UVC_EP_INTERRUPT, 122 .bDescriptorSubType = UVC_EP_INTERRUPT,
120 .wMaxTransferSize = cpu_to_le16(STATUS_BYTECOUNT), 123 .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
121}; 124};
122 125
123static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = { 126static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
@@ -144,63 +147,53 @@ static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
144 .iInterface = 0, 147 .iInterface = 0,
145}; 148};
146 149
147static struct usb_endpoint_descriptor uvc_fs_streaming_ep = { 150static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
148 .bLength = USB_DT_ENDPOINT_SIZE, 151 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT, 152 .bDescriptorType = USB_DT_ENDPOINT,
150 .bEndpointAddress = USB_DIR_IN, 153 .bEndpointAddress = USB_DIR_IN,
151 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 154 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
152 .wMaxPacketSize = cpu_to_le16(512), 155 | USB_ENDPOINT_XFER_ISOC,
153 .bInterval = 1, 156 /* The wMaxPacketSize and bInterval values will be initialized from
157 * module parameters.
158 */
159 .wMaxPacketSize = 0,
160 .bInterval = 0,
154}; 161};
155 162
156static struct usb_endpoint_descriptor uvc_hs_streaming_ep = { 163static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
157 .bLength = USB_DT_ENDPOINT_SIZE, 164 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT, 165 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = USB_DIR_IN, 166 .bEndpointAddress = USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 167 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
161 .wMaxPacketSize = cpu_to_le16(1024), 168 | USB_ENDPOINT_XFER_ISOC,
162 .bInterval = 1, 169 /* The wMaxPacketSize and bInterval values will be initialized from
163}; 170 * module parameters.
164 171 */
165/* super speed support */ 172 .wMaxPacketSize = 0,
166static struct usb_endpoint_descriptor uvc_ss_control_ep __initdata = { 173 .bInterval = 0,
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169
170 .bEndpointAddress = USB_DIR_IN,
171 .bmAttributes = USB_ENDPOINT_XFER_INT,
172 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
173 .bInterval = 8,
174};
175
176static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
177 .bLength = sizeof uvc_ss_control_comp,
178 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
179
180 /* the following 3 values can be tweaked if necessary */
181 /* .bMaxBurst = 0, */
182 /* .bmAttributes = 0, */
183 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
184}; 174};
185 175
186static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = { 176static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
187 .bLength = USB_DT_ENDPOINT_SIZE, 177 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT, 178 .bDescriptorType = USB_DT_ENDPOINT,
189 179
190 .bEndpointAddress = USB_DIR_IN, 180 .bEndpointAddress = USB_DIR_IN,
191 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 181 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
192 .wMaxPacketSize = cpu_to_le16(1024), 182 | USB_ENDPOINT_XFER_ISOC,
193 .bInterval = 4, 183 /* The wMaxPacketSize and bInterval values will be initialized from
184 * module parameters.
185 */
186 .wMaxPacketSize = 0,
187 .bInterval = 0,
194}; 188};
195 189
196static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = { 190static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
197 .bLength = sizeof uvc_ss_streaming_comp, 191 .bLength = sizeof(uvc_ss_streaming_comp),
198 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
199 193 /* The following 3 values can be tweaked if necessary. */
200 /* the following 3 values can be tweaked if necessary */ 194 .bMaxBurst = 0,
201 .bMaxBurst = 0, 195 .bmAttributes = 0,
202 .bmAttributes = 0, 196 .wBytesPerInterval = cpu_to_le16(1024),
203 .wBytesPerInterval = cpu_to_le16(1024),
204}; 197};
205 198
206static const struct usb_descriptor_header * const uvc_fs_streaming[] = { 199static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
@@ -273,6 +266,13 @@ uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
273 return 0; 266 return 0;
274} 267}
275 268
269void uvc_function_setup_continue(struct uvc_device *uvc)
270{
271 struct usb_composite_dev *cdev = uvc->func.config->cdev;
272
273 usb_composite_setup_continue(cdev);
274}
275
276static int 276static int
277uvc_function_get_alt(struct usb_function *f, unsigned interface) 277uvc_function_get_alt(struct usb_function *f, unsigned interface)
278{ 278{
@@ -335,7 +335,7 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
335 v4l2_event_queue(uvc->vdev, &v4l2_event); 335 v4l2_event_queue(uvc->vdev, &v4l2_event);
336 336
337 uvc->state = UVC_STATE_CONNECTED; 337 uvc->state = UVC_STATE_CONNECTED;
338 break; 338 return 0;
339 339
340 case 1: 340 case 1:
341 if (uvc->state != UVC_STATE_CONNECTED) 341 if (uvc->state != UVC_STATE_CONNECTED)
@@ -352,15 +352,11 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
352 memset(&v4l2_event, 0, sizeof(v4l2_event)); 352 memset(&v4l2_event, 0, sizeof(v4l2_event));
353 v4l2_event.type = UVC_EVENT_STREAMON; 353 v4l2_event.type = UVC_EVENT_STREAMON;
354 v4l2_event_queue(uvc->vdev, &v4l2_event); 354 v4l2_event_queue(uvc->vdev, &v4l2_event);
355 355 return USB_GADGET_DELAYED_STATUS;
356 uvc->state = UVC_STATE_STREAMING;
357 break;
358 356
359 default: 357 default:
360 return -EINVAL; 358 return -EINVAL;
361 } 359 }
362
363 return 0;
364} 360}
365 361
366static void 362static void
@@ -454,7 +450,6 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
454 const struct uvc_descriptor_header * const *uvc_streaming_cls; 450 const struct uvc_descriptor_header * const *uvc_streaming_cls;
455 const struct usb_descriptor_header * const *uvc_streaming_std; 451 const struct usb_descriptor_header * const *uvc_streaming_std;
456 const struct usb_descriptor_header * const *src; 452 const struct usb_descriptor_header * const *src;
457 static struct usb_endpoint_descriptor *uvc_control_ep;
458 struct usb_descriptor_header **dst; 453 struct usb_descriptor_header **dst;
459 struct usb_descriptor_header **hdr; 454 struct usb_descriptor_header **hdr;
460 unsigned int control_size; 455 unsigned int control_size;
@@ -468,14 +463,12 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
468 uvc_control_desc = uvc->desc.ss_control; 463 uvc_control_desc = uvc->desc.ss_control;
469 uvc_streaming_cls = uvc->desc.ss_streaming; 464 uvc_streaming_cls = uvc->desc.ss_streaming;
470 uvc_streaming_std = uvc_ss_streaming; 465 uvc_streaming_std = uvc_ss_streaming;
471 uvc_control_ep = &uvc_ss_control_ep;
472 break; 466 break;
473 467
474 case USB_SPEED_HIGH: 468 case USB_SPEED_HIGH:
475 uvc_control_desc = uvc->desc.fs_control; 469 uvc_control_desc = uvc->desc.fs_control;
476 uvc_streaming_cls = uvc->desc.hs_streaming; 470 uvc_streaming_cls = uvc->desc.hs_streaming;
477 uvc_streaming_std = uvc_hs_streaming; 471 uvc_streaming_std = uvc_hs_streaming;
478 uvc_control_ep = &uvc_fs_control_ep;
479 break; 472 break;
480 473
481 case USB_SPEED_FULL: 474 case USB_SPEED_FULL:
@@ -483,7 +476,6 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
483 uvc_control_desc = uvc->desc.fs_control; 476 uvc_control_desc = uvc->desc.fs_control;
484 uvc_streaming_cls = uvc->desc.fs_streaming; 477 uvc_streaming_cls = uvc->desc.fs_streaming;
485 uvc_streaming_std = uvc_fs_streaming; 478 uvc_streaming_std = uvc_fs_streaming;
486 uvc_control_ep = &uvc_fs_control_ep;
487 break; 479 break;
488 } 480 }
489 481
@@ -494,6 +486,7 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
494 * Class-specific UVC control descriptors 486 * Class-specific UVC control descriptors
495 * uvc_control_ep 487 * uvc_control_ep
496 * uvc_control_cs_ep 488 * uvc_control_cs_ep
489 * uvc_ss_control_comp (for SS only)
497 * uvc_streaming_intf_alt0 490 * uvc_streaming_intf_alt0
498 * Class-specific UVC streaming descriptors 491 * Class-specific UVC streaming descriptors
499 * uvc_{fs|hs}_streaming 492 * uvc_{fs|hs}_streaming
@@ -503,7 +496,7 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
503 control_size = 0; 496 control_size = 0;
504 streaming_size = 0; 497 streaming_size = 0;
505 bytes = uvc_iad.bLength + uvc_control_intf.bLength 498 bytes = uvc_iad.bLength + uvc_control_intf.bLength
506 + uvc_control_ep->bLength + uvc_control_cs_ep.bLength 499 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
507 + uvc_streaming_intf_alt0.bLength; 500 + uvc_streaming_intf_alt0.bLength;
508 501
509 if (speed == USB_SPEED_SUPER) { 502 if (speed == USB_SPEED_SUPER) {
@@ -514,13 +507,13 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
514 } 507 }
515 508
516 for (src = (const struct usb_descriptor_header **)uvc_control_desc; 509 for (src = (const struct usb_descriptor_header **)uvc_control_desc;
517 *src; ++src) { 510 *src; ++src) {
518 control_size += (*src)->bLength; 511 control_size += (*src)->bLength;
519 bytes += (*src)->bLength; 512 bytes += (*src)->bLength;
520 n_desc++; 513 n_desc++;
521 } 514 }
522 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls; 515 for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
523 *src; ++src) { 516 *src; ++src) {
524 streaming_size += (*src)->bLength; 517 streaming_size += (*src)->bLength;
525 bytes += (*src)->bLength; 518 bytes += (*src)->bLength;
526 n_desc++; 519 n_desc++;
@@ -549,7 +542,7 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
549 uvc_control_header->bInCollection = 1; 542 uvc_control_header->bInCollection = 1;
550 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf; 543 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
551 544
552 UVC_COPY_DESCRIPTOR(mem, dst, uvc_control_ep); 545 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
553 if (speed == USB_SPEED_SUPER) 546 if (speed == USB_SPEED_SUPER)
554 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp); 547 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
555 548
@@ -560,8 +553,7 @@ uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
560 UVC_COPY_DESCRIPTORS(mem, dst, 553 UVC_COPY_DESCRIPTORS(mem, dst,
561 (const struct usb_descriptor_header**)uvc_streaming_cls); 554 (const struct usb_descriptor_header**)uvc_streaming_cls);
562 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size); 555 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
563 uvc_streaming_header->bEndpointAddress = 556 uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
564 uvc_fs_streaming_ep.bEndpointAddress;
565 557
566 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std); 558 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
567 559
@@ -581,7 +573,7 @@ uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
581 uvc->control_ep->driver_data = NULL; 573 uvc->control_ep->driver_data = NULL;
582 uvc->video.ep->driver_data = NULL; 574 uvc->video.ep->driver_data = NULL;
583 575
584 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0; 576 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = 0;
585 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req); 577 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
586 kfree(uvc->control_buf); 578 kfree(uvc->control_buf);
587 579
@@ -595,31 +587,52 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
595{ 587{
596 struct usb_composite_dev *cdev = c->cdev; 588 struct usb_composite_dev *cdev = c->cdev;
597 struct uvc_device *uvc = to_uvc(f); 589 struct uvc_device *uvc = to_uvc(f);
590 unsigned int max_packet_mult;
591 unsigned int max_packet_size;
598 struct usb_ep *ep; 592 struct usb_ep *ep;
599 int ret = -EINVAL; 593 int ret = -EINVAL;
600 594
601 INFO(cdev, "uvc_function_bind\n"); 595 INFO(cdev, "uvc_function_bind\n");
602 596
603 /* sanity check the streaming endpoint module parameters */ 597 /* Sanity check the streaming endpoint module parameters.
604 if (streaming_interval < 1)
605 streaming_interval = 1;
606 if (streaming_interval > 16)
607 streaming_interval = 16;
608 if (streaming_mult > 2)
609 streaming_mult = 2;
610 if (streaming_maxburst > 15)
611 streaming_maxburst = 15;
612
613 /*
614 * fill in the FS video streaming specific descriptors from the
615 * module parameters
616 */ 598 */
617 uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ? 599 streaming_interval = clamp(streaming_interval, 1U, 16U);
618 1023 : streaming_maxpacket; 600 streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
601 streaming_maxburst = min(streaming_maxburst, 15U);
602
603 /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
604 * module parameters.
605 *
606 * NOTE: We assume that the user knows what they are doing and won't
607 * give parameters that their UDC doesn't support.
608 */
609 if (streaming_maxpacket <= 1024) {
610 max_packet_mult = 1;
611 max_packet_size = streaming_maxpacket;
612 } else if (streaming_maxpacket <= 2048) {
613 max_packet_mult = 2;
614 max_packet_size = streaming_maxpacket / 2;
615 } else {
616 max_packet_mult = 3;
617 max_packet_size = streaming_maxpacket / 3;
618 }
619
620 uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
619 uvc_fs_streaming_ep.bInterval = streaming_interval; 621 uvc_fs_streaming_ep.bInterval = streaming_interval;
620 622
623 uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
624 uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
625 uvc_hs_streaming_ep.bInterval = streaming_interval;
626
627 uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
628 uvc_ss_streaming_ep.bInterval = streaming_interval;
629 uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
630 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
631 uvc_ss_streaming_comp.wBytesPerInterval =
632 max_packet_size * max_packet_mult * streaming_maxburst;
633
621 /* Allocate endpoints. */ 634 /* Allocate endpoints. */
622 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_control_ep); 635 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
623 if (!ep) { 636 if (!ep) {
624 INFO(cdev, "Unable to allocate control EP\n"); 637 INFO(cdev, "Unable to allocate control EP\n");
625 goto error; 638 goto error;
@@ -627,7 +640,14 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
627 uvc->control_ep = ep; 640 uvc->control_ep = ep;
628 ep->driver_data = uvc; 641 ep->driver_data = uvc;
629 642
630 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep); 643 if (gadget_is_superspeed(c->cdev->gadget))
644 ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
645 &uvc_ss_streaming_comp);
646 else if (gadget_is_dualspeed(cdev->gadget))
647 ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
648 else
649 ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
650
631 if (!ep) { 651 if (!ep) {
632 INFO(cdev, "Unable to allocate streaming EP\n"); 652 INFO(cdev, "Unable to allocate streaming EP\n");
633 goto error; 653 goto error;
@@ -635,6 +655,10 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
635 uvc->video.ep = ep; 655 uvc->video.ep = ep;
636 ep->driver_data = uvc; 656 ep->driver_data = uvc;
637 657
658 uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
659 uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
660 uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
661
638 /* Allocate interface IDs. */ 662 /* Allocate interface IDs. */
639 if ((ret = usb_interface_id(c, f)) < 0) 663 if ((ret = usb_interface_id(c, f)) < 0)
640 goto error; 664 goto error;
@@ -648,37 +672,6 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
648 uvc_streaming_intf_alt1.bInterfaceNumber = ret; 672 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
649 uvc->streaming_intf = ret; 673 uvc->streaming_intf = ret;
650 674
651 /* sanity check the streaming endpoint module parameters */
652 if (streaming_maxpacket > 1024)
653 streaming_maxpacket = 1024;
654 /*
655 * Fill in the HS descriptors from the module parameters for the Video
656 * Streaming endpoint.
657 * NOTE: We assume that the user knows what they are doing and won't
658 * give parameters that their UDC doesn't support.
659 */
660 uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
661 uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
662 uvc_hs_streaming_ep.bInterval = streaming_interval;
663 uvc_hs_streaming_ep.bEndpointAddress =
664 uvc_fs_streaming_ep.bEndpointAddress;
665
666 /*
667 * Fill in the SS descriptors from the module parameters for the Video
668 * Streaming endpoint.
669 * NOTE: We assume that the user knows what they are doing and won't
670 * give parameters that their UDC doesn't support.
671 */
672 uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
673 uvc_ss_streaming_ep.bInterval = streaming_interval;
674 uvc_ss_streaming_comp.bmAttributes = streaming_mult;
675 uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
676 uvc_ss_streaming_comp.wBytesPerInterval =
677 streaming_maxpacket * (streaming_mult + 1) *
678 (streaming_maxburst + 1);
679 uvc_ss_streaming_ep.bEndpointAddress =
680 uvc_fs_streaming_ep.bEndpointAddress;
681
682 /* Copy descriptors */ 675 /* Copy descriptors */
683 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL); 676 f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
684 if (gadget_is_dualspeed(cdev->gadget)) 677 if (gadget_is_dualspeed(cdev->gadget))
@@ -775,23 +768,23 @@ uvc_bind_config(struct usb_configuration *c,
775 768
776 /* Validate the descriptors. */ 769 /* Validate the descriptors. */
777 if (fs_control == NULL || fs_control[0] == NULL || 770 if (fs_control == NULL || fs_control[0] == NULL ||
778 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER) 771 fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
779 goto error; 772 goto error;
780 773
781 if (ss_control == NULL || ss_control[0] == NULL || 774 if (ss_control == NULL || ss_control[0] == NULL ||
782 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER) 775 ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
783 goto error; 776 goto error;
784 777
785 if (fs_streaming == NULL || fs_streaming[0] == NULL || 778 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
786 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) 779 fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
787 goto error; 780 goto error;
788 781
789 if (hs_streaming == NULL || hs_streaming[0] == NULL || 782 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
790 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) 783 hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
791 goto error; 784 goto error;
792 785
793 if (ss_streaming == NULL || ss_streaming[0] == NULL || 786 if (ss_streaming == NULL || ss_streaming[0] == NULL ||
794 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER) 787 ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
795 goto error; 788 goto error;
796 789
797 uvc->desc.fs_control = fs_control; 790 uvc->desc.fs_control = fs_control;
@@ -800,13 +793,16 @@ uvc_bind_config(struct usb_configuration *c,
800 uvc->desc.hs_streaming = hs_streaming; 793 uvc->desc.hs_streaming = hs_streaming;
801 uvc->desc.ss_streaming = ss_streaming; 794 uvc->desc.ss_streaming = ss_streaming;
802 795
803 /* Allocate string descriptor numbers. */ 796 /* String descriptors are global, we only need to allocate string IDs
804 if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) { 797 * for the first UVC function. UVC functions beyond the first (if any)
798 * will reuse the same IDs.
799 */
800 if (uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id == 0) {
805 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings); 801 ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
806 if (ret) 802 if (ret)
807 goto error; 803 goto error;
808 uvc_iad.iFunction = 804 uvc_iad.iFunction =
809 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id; 805 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
810 uvc_control_intf.iInterface = 806 uvc_control_intf.iInterface =
811 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id; 807 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
812 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id; 808 ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
diff --git a/drivers/usb/gadget/f_uvc.h b/drivers/usb/gadget/f_uvc.h
index c3d258d30188..ec52752f7326 100644
--- a/drivers/usb/gadget/f_uvc.h
+++ b/drivers/usb/gadget/f_uvc.h
@@ -16,12 +16,12 @@
16#include <linux/usb/composite.h> 16#include <linux/usb/composite.h>
17#include <linux/usb/video.h> 17#include <linux/usb/video.h>
18 18
19extern int uvc_bind_config(struct usb_configuration *c, 19int uvc_bind_config(struct usb_configuration *c,
20 const struct uvc_descriptor_header * const *fs_control, 20 const struct uvc_descriptor_header * const *fs_control,
21 const struct uvc_descriptor_header * const *hs_control, 21 const struct uvc_descriptor_header * const *hs_control,
22 const struct uvc_descriptor_header * const *fs_streaming, 22 const struct uvc_descriptor_header * const *fs_streaming,
23 const struct uvc_descriptor_header * const *hs_streaming, 23 const struct uvc_descriptor_header * const *hs_streaming,
24 const struct uvc_descriptor_header * const *ss_streaming); 24 const struct uvc_descriptor_header * const *ss_streaming);
25 25
26#endif /* _F_UVC_H_ */ 26#endif /* _F_UVC_H_ */
27 27
diff --git a/drivers/usb/gadget/fsl_qe_udc.c b/drivers/usb/gadget/fsl_qe_udc.c
index 034477ce77c6..9a7ee3347e4d 100644
--- a/drivers/usb/gadget/fsl_qe_udc.c
+++ b/drivers/usb/gadget/fsl_qe_udc.c
@@ -2296,7 +2296,6 @@ static int fsl_qe_start(struct usb_gadget *gadget,
2296 driver->driver.bus = NULL; 2296 driver->driver.bus = NULL;
2297 /* hook up the driver */ 2297 /* hook up the driver */
2298 udc->driver = driver; 2298 udc->driver = driver;
2299 udc->gadget.dev.driver = &driver->driver;
2300 udc->gadget.speed = driver->max_speed; 2299 udc->gadget.speed = driver->max_speed;
2301 2300
2302 /* Enable IRQ reg and Set usbcmd reg EN bit */ 2301 /* Enable IRQ reg and Set usbcmd reg EN bit */
@@ -2338,7 +2337,6 @@ static int fsl_qe_stop(struct usb_gadget *gadget,
2338 nuke(loop_ep, -ESHUTDOWN); 2337 nuke(loop_ep, -ESHUTDOWN);
2339 spin_unlock_irqrestore(&udc->lock, flags); 2338 spin_unlock_irqrestore(&udc->lock, flags);
2340 2339
2341 udc->gadget.dev.driver = NULL;
2342 udc->driver = NULL; 2340 udc->driver = NULL;
2343 2341
2344 dev_info(udc->dev, "unregistered gadget driver '%s'\r\n", 2342 dev_info(udc->dev, "unregistered gadget driver '%s'\r\n",
@@ -2523,12 +2521,6 @@ static int qe_udc_probe(struct platform_device *ofdev)
2523 2521
2524 /* name: Identifies the controller hardware type. */ 2522 /* name: Identifies the controller hardware type. */
2525 udc->gadget.name = driver_name; 2523 udc->gadget.name = driver_name;
2526
2527 device_initialize(&udc->gadget.dev);
2528
2529 dev_set_name(&udc->gadget.dev, "gadget");
2530
2531 udc->gadget.dev.release = qe_udc_release;
2532 udc->gadget.dev.parent = &ofdev->dev; 2524 udc->gadget.dev.parent = &ofdev->dev;
2533 2525
2534 /* initialize qe_ep struct */ 2526 /* initialize qe_ep struct */
@@ -2592,22 +2584,17 @@ static int qe_udc_probe(struct platform_device *ofdev)
2592 goto err5; 2584 goto err5;
2593 } 2585 }
2594 2586
2595 ret = device_add(&udc->gadget.dev); 2587 ret = usb_add_gadget_udc_release(&ofdev->dev, &udc->gadget,
2588 qe_udc_release);
2596 if (ret) 2589 if (ret)
2597 goto err6; 2590 goto err6;
2598 2591
2599 ret = usb_add_gadget_udc(&ofdev->dev, &udc->gadget);
2600 if (ret)
2601 goto err7;
2602
2603 dev_set_drvdata(&ofdev->dev, udc); 2592 dev_set_drvdata(&ofdev->dev, udc);
2604 dev_info(udc->dev, 2593 dev_info(udc->dev,
2605 "%s USB controller initialized as device\n", 2594 "%s USB controller initialized as device\n",
2606 (udc->soc_type == PORT_QE) ? "QE" : "CPM"); 2595 (udc->soc_type == PORT_QE) ? "QE" : "CPM");
2607 return 0; 2596 return 0;
2608 2597
2609err7:
2610 device_unregister(&udc->gadget.dev);
2611err6: 2598err6:
2612 free_irq(udc->usb_irq, udc); 2599 free_irq(udc->usb_irq, udc);
2613err5: 2600err5:
@@ -2702,7 +2689,6 @@ static int qe_udc_remove(struct platform_device *ofdev)
2702 2689
2703 iounmap(udc->usb_regs); 2690 iounmap(udc->usb_regs);
2704 2691
2705 device_unregister(&udc->gadget.dev);
2706 /* wait for release() of gadget.dev to free udc */ 2692 /* wait for release() of gadget.dev to free udc */
2707 wait_for_completion(&done); 2693 wait_for_completion(&done);
2708 2694
diff --git a/drivers/usb/gadget/fsl_udc_core.c b/drivers/usb/gadget/fsl_udc_core.c
index 04d5fef1440c..7c2a101d19ac 100644
--- a/drivers/usb/gadget/fsl_udc_core.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -185,20 +185,7 @@ static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
185 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma); 185 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
186 } 186 }
187 187
188 if (req->mapped) { 188 usb_gadget_unmap_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
189 dma_unmap_single(ep->udc->gadget.dev.parent,
190 req->req.dma, req->req.length,
191 ep_is_in(ep)
192 ? DMA_TO_DEVICE
193 : DMA_FROM_DEVICE);
194 req->req.dma = DMA_ADDR_INVALID;
195 req->mapped = 0;
196 } else
197 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
198 req->req.dma, req->req.length,
199 ep_is_in(ep)
200 ? DMA_TO_DEVICE
201 : DMA_FROM_DEVICE);
202 189
203 if (status && (status != -ESHUTDOWN)) 190 if (status && (status != -ESHUTDOWN))
204 VDBG("complete %s req %p stat %d len %u/%u", 191 VDBG("complete %s req %p stat %d len %u/%u",
@@ -888,6 +875,7 @@ fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
888 struct fsl_req *req = container_of(_req, struct fsl_req, req); 875 struct fsl_req *req = container_of(_req, struct fsl_req, req);
889 struct fsl_udc *udc; 876 struct fsl_udc *udc;
890 unsigned long flags; 877 unsigned long flags;
878 int ret;
891 879
892 /* catch various bogus parameters */ 880 /* catch various bogus parameters */
893 if (!_req || !req->req.complete || !req->req.buf 881 if (!_req || !req->req.complete || !req->req.buf
@@ -910,22 +898,9 @@ fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
910 898
911 req->ep = ep; 899 req->ep = ep;
912 900
913 /* map virtual address to hardware */ 901 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
914 if (req->req.dma == DMA_ADDR_INVALID) { 902 if (ret)
915 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent, 903 return ret;
916 req->req.buf,
917 req->req.length, ep_is_in(ep)
918 ? DMA_TO_DEVICE
919 : DMA_FROM_DEVICE);
920 req->mapped = 1;
921 } else {
922 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
923 req->req.dma, req->req.length,
924 ep_is_in(ep)
925 ? DMA_TO_DEVICE
926 : DMA_FROM_DEVICE);
927 req->mapped = 0;
928 }
929 904
930 req->req.status = -EINPROGRESS; 905 req->req.status = -EINPROGRESS;
931 req->req.actual = 0; 906 req->req.actual = 0;
@@ -1290,6 +1265,7 @@ static int ep0_prime_status(struct fsl_udc *udc, int direction)
1290{ 1265{
1291 struct fsl_req *req = udc->status_req; 1266 struct fsl_req *req = udc->status_req;
1292 struct fsl_ep *ep; 1267 struct fsl_ep *ep;
1268 int ret;
1293 1269
1294 if (direction == EP_DIR_IN) 1270 if (direction == EP_DIR_IN)
1295 udc->ep0_dir = USB_DIR_IN; 1271 udc->ep0_dir = USB_DIR_IN;
@@ -1307,10 +1283,9 @@ static int ep0_prime_status(struct fsl_udc *udc, int direction)
1307 req->req.complete = NULL; 1283 req->req.complete = NULL;
1308 req->dtd_count = 0; 1284 req->dtd_count = 0;
1309 1285
1310 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent, 1286 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
1311 req->req.buf, req->req.length, 1287 if (ret)
1312 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1288 return ret;
1313 req->mapped = 1;
1314 1289
1315 if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0) 1290 if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0)
1316 fsl_queue_td(ep, req); 1291 fsl_queue_td(ep, req);
@@ -1353,6 +1328,7 @@ static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1353 u16 tmp = 0; /* Status, cpu endian */ 1328 u16 tmp = 0; /* Status, cpu endian */
1354 struct fsl_req *req; 1329 struct fsl_req *req;
1355 struct fsl_ep *ep; 1330 struct fsl_ep *ep;
1331 int ret;
1356 1332
1357 ep = &udc->eps[0]; 1333 ep = &udc->eps[0];
1358 1334
@@ -1390,10 +1366,9 @@ static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1390 req->req.complete = NULL; 1366 req->req.complete = NULL;
1391 req->dtd_count = 0; 1367 req->dtd_count = 0;
1392 1368
1393 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent, 1369 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
1394 req->req.buf, req->req.length, 1370 if (ret)
1395 ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1371 goto stall;
1396 req->mapped = 1;
1397 1372
1398 /* prime the data phase */ 1373 /* prime the data phase */
1399 if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0)) 1374 if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0))
@@ -1964,7 +1939,6 @@ static int fsl_udc_start(struct usb_gadget *g,
1964 driver->driver.bus = NULL; 1939 driver->driver.bus = NULL;
1965 /* hook up the driver */ 1940 /* hook up the driver */
1966 udc_controller->driver = driver; 1941 udc_controller->driver = driver;
1967 udc_controller->gadget.dev.driver = &driver->driver;
1968 spin_unlock_irqrestore(&udc_controller->lock, flags); 1942 spin_unlock_irqrestore(&udc_controller->lock, flags);
1969 1943
1970 if (!IS_ERR_OR_NULL(udc_controller->transceiver)) { 1944 if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
@@ -1980,7 +1954,6 @@ static int fsl_udc_start(struct usb_gadget *g,
1980 if (retval < 0) { 1954 if (retval < 0) {
1981 ERR("can't bind to transceiver\n"); 1955 ERR("can't bind to transceiver\n");
1982 driver->unbind(&udc_controller->gadget); 1956 driver->unbind(&udc_controller->gadget);
1983 udc_controller->gadget.dev.driver = 0;
1984 udc_controller->driver = 0; 1957 udc_controller->driver = 0;
1985 return retval; 1958 return retval;
1986 } 1959 }
@@ -2023,7 +1996,6 @@ static int fsl_udc_stop(struct usb_gadget *g,
2023 nuke(loop_ep, -ESHUTDOWN); 1996 nuke(loop_ep, -ESHUTDOWN);
2024 spin_unlock_irqrestore(&udc_controller->lock, flags); 1997 spin_unlock_irqrestore(&udc_controller->lock, flags);
2025 1998
2026 udc_controller->gadget.dev.driver = NULL;
2027 udc_controller->driver = NULL; 1999 udc_controller->driver = NULL;
2028 2000
2029 return 0; 2001 return 0;
@@ -2521,12 +2493,7 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2521 2493
2522 /* Setup gadget.dev and register with kernel */ 2494 /* Setup gadget.dev and register with kernel */
2523 dev_set_name(&udc_controller->gadget.dev, "gadget"); 2495 dev_set_name(&udc_controller->gadget.dev, "gadget");
2524 udc_controller->gadget.dev.release = fsl_udc_release;
2525 udc_controller->gadget.dev.parent = &pdev->dev;
2526 udc_controller->gadget.dev.of_node = pdev->dev.of_node; 2496 udc_controller->gadget.dev.of_node = pdev->dev.of_node;
2527 ret = device_register(&udc_controller->gadget.dev);
2528 if (ret < 0)
2529 goto err_free_irq;
2530 2497
2531 if (!IS_ERR_OR_NULL(udc_controller->transceiver)) 2498 if (!IS_ERR_OR_NULL(udc_controller->transceiver))
2532 udc_controller->gadget.is_otg = 1; 2499 udc_controller->gadget.is_otg = 1;
@@ -2559,10 +2526,11 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2559 DTD_ALIGNMENT, UDC_DMA_BOUNDARY); 2526 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2560 if (udc_controller->td_pool == NULL) { 2527 if (udc_controller->td_pool == NULL) {
2561 ret = -ENOMEM; 2528 ret = -ENOMEM;
2562 goto err_unregister; 2529 goto err_free_irq;
2563 } 2530 }
2564 2531
2565 ret = usb_add_gadget_udc(&pdev->dev, &udc_controller->gadget); 2532 ret = usb_add_gadget_udc_release(&pdev->dev, &udc_controller->gadget,
2533 fsl_udc_release);
2566 if (ret) 2534 if (ret)
2567 goto err_del_udc; 2535 goto err_del_udc;
2568 2536
@@ -2571,8 +2539,6 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2571 2539
2572err_del_udc: 2540err_del_udc:
2573 dma_pool_destroy(udc_controller->td_pool); 2541 dma_pool_destroy(udc_controller->td_pool);
2574err_unregister:
2575 device_unregister(&udc_controller->gadget.dev);
2576err_free_irq: 2542err_free_irq:
2577 free_irq(udc_controller->irq, udc_controller); 2543 free_irq(udc_controller->irq, udc_controller);
2578err_iounmap: 2544err_iounmap:
@@ -2622,7 +2588,6 @@ static int __exit fsl_udc_remove(struct platform_device *pdev)
2622 if (pdata->operating_mode == FSL_USB2_DR_DEVICE) 2588 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2623 release_mem_region(res->start, resource_size(res)); 2589 release_mem_region(res->start, resource_size(res));
2624 2590
2625 device_unregister(&udc_controller->gadget.dev);
2626 /* free udc --wait for the release() finished */ 2591 /* free udc --wait for the release() finished */
2627 wait_for_completion(&done); 2592 wait_for_completion(&done);
2628 2593
@@ -2747,21 +2712,7 @@ static struct platform_driver udc_driver = {
2747 }, 2712 },
2748}; 2713};
2749 2714
2750static int __init udc_init(void) 2715module_platform_driver_probe(udc_driver, fsl_udc_probe);
2751{
2752 printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
2753 return platform_driver_probe(&udc_driver, fsl_udc_probe);
2754}
2755
2756module_init(udc_init);
2757
2758static void __exit udc_exit(void)
2759{
2760 platform_driver_unregister(&udc_driver);
2761 printk(KERN_WARNING "%s unregistered\n", driver_desc);
2762}
2763
2764module_exit(udc_exit);
2765 2716
2766MODULE_DESCRIPTION(DRIVER_DESC); 2717MODULE_DESCRIPTION(DRIVER_DESC);
2767MODULE_AUTHOR(DRIVER_AUTHOR); 2718MODULE_AUTHOR(DRIVER_AUTHOR);
diff --git a/drivers/usb/gadget/fusb300_udc.c b/drivers/usb/gadget/fusb300_udc.c
index 066cb89376de..cec8871b77f9 100644
--- a/drivers/usb/gadget/fusb300_udc.c
+++ b/drivers/usb/gadget/fusb300_udc.c
@@ -394,7 +394,7 @@ static void fusb300_clear_epnstall(struct fusb300 *fusb300, u8 ep)
394 394
395 if (reg & FUSB300_EPSET0_STL) { 395 if (reg & FUSB300_EPSET0_STL) {
396 printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep); 396 printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep);
397 reg &= ~FUSB300_EPSET0_STL; 397 reg |= FUSB300_EPSET0_STL_CLR;
398 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep)); 398 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
399 } 399 }
400} 400}
@@ -930,33 +930,33 @@ static void fusb300_wait_idma_finished(struct fusb300_ep *ep)
930 930
931 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0, 931 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0,
932 FUSB300_IGR0_EPn_PRD_INT(ep->epnum)); 932 FUSB300_IGR0_EPn_PRD_INT(ep->epnum));
933 return;
934
933IDMA_RESET: 935IDMA_RESET:
934 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGER0, 936 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGER0);
935 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum)); 937 reg &= ~FUSB300_IGER0_EEPn_PRD_INT(ep->epnum);
938 iowrite32(reg, ep->fusb300->reg + FUSB300_OFFSET_IGER0);
936} 939}
937 940
938static void fusb300_set_idma(struct fusb300_ep *ep, 941static void fusb300_set_idma(struct fusb300_ep *ep,
939 struct fusb300_request *req) 942 struct fusb300_request *req)
940{ 943{
941 dma_addr_t d; 944 int ret;
942
943 d = dma_map_single(NULL, req->req.buf, req->req.length, DMA_TO_DEVICE);
944 945
945 if (dma_mapping_error(NULL, d)) { 946 ret = usb_gadget_map_request(&ep->fusb300->gadget,
946 printk(KERN_DEBUG "dma_mapping_error\n"); 947 &req->req, DMA_TO_DEVICE);
948 if (ret)
947 return; 949 return;
948 }
949
950 dma_sync_single_for_device(NULL, d, req->req.length, DMA_TO_DEVICE);
951 950
952 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0, 951 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0,
953 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum)); 952 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
954 953
955 fusb300_fill_idma_prdtbl(ep, d, req->req.length); 954 fusb300_fill_idma_prdtbl(ep, req->req.dma, req->req.length);
956 /* check idma is done */ 955 /* check idma is done */
957 fusb300_wait_idma_finished(ep); 956 fusb300_wait_idma_finished(ep);
958 957
959 dma_unmap_single(NULL, d, req->req.length, DMA_TO_DEVICE); 958 usb_gadget_unmap_request(&ep->fusb300->gadget,
959 &req->req, DMA_TO_DEVICE);
960} 960}
961 961
962static void in_ep_fifo_handler(struct fusb300_ep *ep) 962static void in_ep_fifo_handler(struct fusb300_ep *ep)
@@ -1316,7 +1316,6 @@ static int fusb300_udc_start(struct usb_gadget *g,
1316 /* hook up the driver */ 1316 /* hook up the driver */
1317 driver->driver.bus = NULL; 1317 driver->driver.bus = NULL;
1318 fusb300->driver = driver; 1318 fusb300->driver = driver;
1319 fusb300->gadget.dev.driver = &driver->driver;
1320 1319
1321 return 0; 1320 return 0;
1322} 1321}
@@ -1327,7 +1326,6 @@ static int fusb300_udc_stop(struct usb_gadget *g,
1327 struct fusb300 *fusb300 = to_fusb300(g); 1326 struct fusb300 *fusb300 = to_fusb300(g);
1328 1327
1329 driver->unbind(&fusb300->gadget); 1328 driver->unbind(&fusb300->gadget);
1330 fusb300->gadget.dev.driver = NULL;
1331 1329
1332 init_controller(fusb300); 1330 init_controller(fusb300);
1333 fusb300->driver = NULL; 1331 fusb300->driver = NULL;
@@ -1422,14 +1420,7 @@ static int __init fusb300_probe(struct platform_device *pdev)
1422 1420
1423 fusb300->gadget.ops = &fusb300_gadget_ops; 1421 fusb300->gadget.ops = &fusb300_gadget_ops;
1424 1422
1425 device_initialize(&fusb300->gadget.dev);
1426
1427 dev_set_name(&fusb300->gadget.dev, "gadget");
1428
1429 fusb300->gadget.max_speed = USB_SPEED_HIGH; 1423 fusb300->gadget.max_speed = USB_SPEED_HIGH;
1430 fusb300->gadget.dev.parent = &pdev->dev;
1431 fusb300->gadget.dev.dma_mask = pdev->dev.dma_mask;
1432 fusb300->gadget.dev.release = pdev->dev.release;
1433 fusb300->gadget.name = udc_name; 1424 fusb300->gadget.name = udc_name;
1434 fusb300->reg = reg; 1425 fusb300->reg = reg;
1435 1426
@@ -1478,19 +1469,10 @@ static int __init fusb300_probe(struct platform_device *pdev)
1478 if (ret) 1469 if (ret)
1479 goto err_add_udc; 1470 goto err_add_udc;
1480 1471
1481 ret = device_add(&fusb300->gadget.dev);
1482 if (ret) {
1483 pr_err("device_add error (%d)\n", ret);
1484 goto err_add_device;
1485 }
1486
1487 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION); 1472 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1488 1473
1489 return 0; 1474 return 0;
1490 1475
1491err_add_device:
1492 usb_del_gadget_udc(&fusb300->gadget);
1493
1494err_add_udc: 1476err_add_udc:
1495 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req); 1477 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1496 1478
diff --git a/drivers/usb/gadget/fusb300_udc.h b/drivers/usb/gadget/fusb300_udc.h
index 6ba444ae8dd5..ae811d8d38b4 100644
--- a/drivers/usb/gadget/fusb300_udc.h
+++ b/drivers/usb/gadget/fusb300_udc.h
@@ -111,8 +111,8 @@
111/* 111/*
112 * * EPn Setting 0 (EPn_SET0, offset = 020H+(n-1)*30H, n=1~15 ) 112 * * EPn Setting 0 (EPn_SET0, offset = 020H+(n-1)*30H, n=1~15 )
113 * */ 113 * */
114#define FUSB300_EPSET0_STL_CLR (1 << 3)
114#define FUSB300_EPSET0_CLRSEQNUM (1 << 2) 115#define FUSB300_EPSET0_CLRSEQNUM (1 << 2)
115#define FUSB300_EPSET0_EPn_TX0BYTE (1 << 1)
116#define FUSB300_EPSET0_STL (1 << 0) 116#define FUSB300_EPSET0_STL (1 << 0)
117 117
118/* 118/*
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index 3b343b23e4b0..787a78e92aa2 100644
--- a/drivers/usb/gadget/g_ffs.c
+++ b/drivers/usb/gadget/g_ffs.c
@@ -13,7 +13,6 @@
13#define pr_fmt(fmt) "g_ffs: " fmt 13#define pr_fmt(fmt) "g_ffs: " fmt
14 14
15#include <linux/module.h> 15#include <linux/module.h>
16
17/* 16/*
18 * kbuild is not very cooperative with respect to linking separately 17 * kbuild is not very cooperative with respect to linking separately
19 * compiled library objects into one module. So for now we won't use 18 * compiled library objects into one module. So for now we won't use
@@ -38,13 +37,16 @@
38# include "u_ether.c" 37# include "u_ether.c"
39 38
40static u8 gfs_hostaddr[ETH_ALEN]; 39static u8 gfs_hostaddr[ETH_ALEN];
40static struct eth_dev *the_dev;
41# ifdef CONFIG_USB_FUNCTIONFS_ETH 41# ifdef CONFIG_USB_FUNCTIONFS_ETH
42static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 42static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
43 struct eth_dev *dev);
43# endif 44# endif
44#else 45#else
45# define gether_cleanup() do { } while (0) 46# define the_dev NULL
46# define gether_setup(gadget, hostaddr) ((int)0) 47# define gether_cleanup(dev) do { } while (0)
47# define gfs_hostaddr NULL 48# define gfs_hostaddr NULL
49struct eth_dev;
48#endif 50#endif
49 51
50#include "f_fs.c" 52#include "f_fs.c"
@@ -137,7 +139,8 @@ static struct usb_gadget_strings *gfs_dev_strings[] = {
137 139
138struct gfs_configuration { 140struct gfs_configuration {
139 struct usb_configuration c; 141 struct usb_configuration c;
140 int (*eth)(struct usb_configuration *c, u8 *ethaddr); 142 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
143 struct eth_dev *dev);
141} gfs_configurations[] = { 144} gfs_configurations[] = {
142#ifdef CONFIG_USB_FUNCTIONFS_RNDIS 145#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
143 { 146 {
@@ -346,10 +349,13 @@ static int gfs_bind(struct usb_composite_dev *cdev)
346 349
347 if (missing_funcs) 350 if (missing_funcs)
348 return -ENODEV; 351 return -ENODEV;
349 352#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
350 ret = gether_setup(cdev->gadget, gfs_hostaddr); 353 the_dev = gether_setup(cdev->gadget, gfs_hostaddr);
351 if (unlikely(ret < 0)) 354#endif
355 if (IS_ERR(the_dev)) {
356 ret = PTR_ERR(the_dev);
352 goto error_quick; 357 goto error_quick;
358 }
353 gfs_ether_setup = true; 359 gfs_ether_setup = true;
354 360
355 ret = usb_string_ids_tab(cdev, gfs_strings); 361 ret = usb_string_ids_tab(cdev, gfs_strings);
@@ -386,7 +392,7 @@ error_unbind:
386 for (i = 0; i < func_num; i++) 392 for (i = 0; i < func_num; i++)
387 functionfs_unbind(ffs_tab[i].ffs_data); 393 functionfs_unbind(ffs_tab[i].ffs_data);
388error: 394error:
389 gether_cleanup(); 395 gether_cleanup(the_dev);
390error_quick: 396error_quick:
391 gfs_ether_setup = false; 397 gfs_ether_setup = false;
392 return ret; 398 return ret;
@@ -410,7 +416,7 @@ static int gfs_unbind(struct usb_composite_dev *cdev)
410 * do...? 416 * do...?
411 */ 417 */
412 if (gfs_ether_setup) 418 if (gfs_ether_setup)
413 gether_cleanup(); 419 gether_cleanup(the_dev);
414 gfs_ether_setup = false; 420 gfs_ether_setup = false;
415 421
416 for (i = func_num; i--; ) 422 for (i = func_num; i--; )
@@ -440,7 +446,7 @@ static int gfs_do_config(struct usb_configuration *c)
440 } 446 }
441 447
442 if (gc->eth) { 448 if (gc->eth) {
443 ret = gc->eth(c, gfs_hostaddr); 449 ret = gc->eth(c, gfs_hostaddr, the_dev);
444 if (unlikely(ret < 0)) 450 if (unlikely(ret < 0))
445 return ret; 451 return ret;
446 } 452 }
@@ -469,11 +475,12 @@ static int gfs_do_config(struct usb_configuration *c)
469 475
470#ifdef CONFIG_USB_FUNCTIONFS_ETH 476#ifdef CONFIG_USB_FUNCTIONFS_ETH
471 477
472static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 478static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
479 struct eth_dev *dev)
473{ 480{
474 return can_support_ecm(c->cdev->gadget) 481 return can_support_ecm(c->cdev->gadget)
475 ? ecm_bind_config(c, ethaddr) 482 ? ecm_bind_config(c, ethaddr, dev)
476 : geth_bind_config(c, ethaddr); 483 : geth_bind_config(c, ethaddr, dev);
477} 484}
478 485
479#endif 486#endif
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index 85742d4c67df..991aba390d9d 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -51,8 +51,6 @@
51#define DRIVER_DESC "TC86C001 USB Device Controller" 51#define DRIVER_DESC "TC86C001 USB Device Controller"
52#define DRIVER_VERSION "30-Oct 2003" 52#define DRIVER_VERSION "30-Oct 2003"
53 53
54#define DMA_ADDR_INVALID (~(dma_addr_t)0)
55
56static const char driver_name [] = "goku_udc"; 54static const char driver_name [] = "goku_udc";
57static const char driver_desc [] = DRIVER_DESC; 55static const char driver_desc [] = DRIVER_DESC;
58 56
@@ -275,7 +273,6 @@ goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
275 if (!req) 273 if (!req)
276 return NULL; 274 return NULL;
277 275
278 req->req.dma = DMA_ADDR_INVALID;
279 INIT_LIST_HEAD(&req->queue); 276 INIT_LIST_HEAD(&req->queue);
280 return &req->req; 277 return &req->req;
281} 278}
@@ -1354,7 +1351,6 @@ static int goku_udc_start(struct usb_gadget *g,
1354 /* hook up the driver */ 1351 /* hook up the driver */
1355 driver->driver.bus = NULL; 1352 driver->driver.bus = NULL;
1356 dev->driver = driver; 1353 dev->driver = driver;
1357 dev->gadget.dev.driver = &driver->driver;
1358 1354
1359 /* 1355 /*
1360 * then enable host detection and ep0; and we're ready 1356 * then enable host detection and ep0; and we're ready
@@ -1394,7 +1390,6 @@ static int goku_udc_stop(struct usb_gadget *g,
1394 dev->driver = NULL; 1390 dev->driver = NULL;
1395 stop_activity(dev, driver); 1391 stop_activity(dev, driver);
1396 spin_unlock_irqrestore(&dev->lock, flags); 1392 spin_unlock_irqrestore(&dev->lock, flags);
1397 dev->gadget.dev.driver = NULL;
1398 1393
1399 return 0; 1394 return 0;
1400} 1395}
@@ -1716,8 +1711,6 @@ static void goku_remove(struct pci_dev *pdev)
1716 pci_resource_len (pdev, 0)); 1711 pci_resource_len (pdev, 0));
1717 if (dev->enabled) 1712 if (dev->enabled)
1718 pci_disable_device(pdev); 1713 pci_disable_device(pdev);
1719 if (dev->registered)
1720 device_unregister(&dev->gadget.dev);
1721 1714
1722 pci_set_drvdata(pdev, NULL); 1715 pci_set_drvdata(pdev, NULL);
1723 dev->regs = NULL; 1716 dev->regs = NULL;
@@ -1756,10 +1749,6 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1756 dev->gadget.max_speed = USB_SPEED_FULL; 1749 dev->gadget.max_speed = USB_SPEED_FULL;
1757 1750
1758 /* the "gadget" abstracts/virtualizes the controller */ 1751 /* the "gadget" abstracts/virtualizes the controller */
1759 dev_set_name(&dev->gadget.dev, "gadget");
1760 dev->gadget.dev.parent = &pdev->dev;
1761 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1762 dev->gadget.dev.release = gadget_release;
1763 dev->gadget.name = driver_name; 1752 dev->gadget.name = driver_name;
1764 1753
1765 /* now all the pci goodies ... */ 1754 /* now all the pci goodies ... */
@@ -1810,13 +1799,8 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1810 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev); 1799 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1811#endif 1800#endif
1812 1801
1813 retval = device_register(&dev->gadget.dev); 1802 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
1814 if (retval) { 1803 gadget_release);
1815 put_device(&dev->gadget.dev);
1816 goto err;
1817 }
1818 dev->registered = 1;
1819 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
1820 if (retval) 1804 if (retval)
1821 goto err; 1805 goto err;
1822 1806
diff --git a/drivers/usb/gadget/goku_udc.h b/drivers/usb/gadget/goku_udc.h
index b4470d2b1d86..86d2adafe149 100644
--- a/drivers/usb/gadget/goku_udc.h
+++ b/drivers/usb/gadget/goku_udc.h
@@ -250,8 +250,7 @@ struct goku_udc {
250 got_region:1, 250 got_region:1,
251 req_config:1, 251 req_config:1,
252 configured:1, 252 configured:1,
253 enabled:1, 253 enabled:1;
254 registered:1;
255 254
256 /* pci state used to access those endpoints */ 255 /* pci state used to access those endpoints */
257 struct pci_dev *pdev; 256 struct pci_dev *pdev;
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index 5bd930d779b9..b5cebd6b0d7a 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -1338,7 +1338,6 @@ static int imx_udc_start(struct usb_gadget *gadget,
1338 imx_usb = container_of(gadget, struct imx_udc_struct, gadget); 1338 imx_usb = container_of(gadget, struct imx_udc_struct, gadget);
1339 /* first hook up the driver ... */ 1339 /* first hook up the driver ... */
1340 imx_usb->driver = driver; 1340 imx_usb->driver = driver;
1341 imx_usb->gadget.dev.driver = &driver->driver;
1342 1341
1343 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n", 1342 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
1344 __func__, driver->driver.name); 1343 __func__, driver->driver.name);
@@ -1358,7 +1357,6 @@ static int imx_udc_stop(struct usb_gadget *gadget,
1358 imx_udc_disable(imx_usb); 1357 imx_udc_disable(imx_usb);
1359 del_timer(&imx_usb->timer); 1358 del_timer(&imx_usb->timer);
1360 1359
1361 imx_usb->gadget.dev.driver = NULL;
1362 imx_usb->driver = NULL; 1360 imx_usb->driver = NULL;
1363 1361
1364 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n", 1362 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
@@ -1461,15 +1459,6 @@ static int __init imx_udc_probe(struct platform_device *pdev)
1461 imx_usb->clk = clk; 1459 imx_usb->clk = clk;
1462 imx_usb->dev = &pdev->dev; 1460 imx_usb->dev = &pdev->dev;
1463 1461
1464 device_initialize(&imx_usb->gadget.dev);
1465
1466 imx_usb->gadget.dev.parent = &pdev->dev;
1467 imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
1468
1469 ret = device_add(&imx_usb->gadget.dev);
1470 if (retval)
1471 goto fail4;
1472
1473 platform_set_drvdata(pdev, imx_usb); 1462 platform_set_drvdata(pdev, imx_usb);
1474 1463
1475 usb_init_data(imx_usb); 1464 usb_init_data(imx_usb);
@@ -1481,11 +1470,9 @@ static int __init imx_udc_probe(struct platform_device *pdev)
1481 1470
1482 ret = usb_add_gadget_udc(&pdev->dev, &imx_usb->gadget); 1471 ret = usb_add_gadget_udc(&pdev->dev, &imx_usb->gadget);
1483 if (ret) 1472 if (ret)
1484 goto fail5; 1473 goto fail4;
1485 1474
1486 return 0; 1475 return 0;
1487fail5:
1488 device_unregister(&imx_usb->gadget.dev);
1489fail4: 1476fail4:
1490 for (i = 0; i < IMX_USB_NB_EP + 1; i++) 1477 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1491 free_irq(imx_usb->usbd_int[i], imx_usb); 1478 free_irq(imx_usb->usbd_int[i], imx_usb);
@@ -1509,7 +1496,6 @@ static int __exit imx_udc_remove(struct platform_device *pdev)
1509 int i; 1496 int i;
1510 1497
1511 usb_del_gadget_udc(&imx_usb->gadget); 1498 usb_del_gadget_udc(&imx_usb->gadget);
1512 device_unregister(&imx_usb->gadget.dev);
1513 imx_udc_disable(imx_usb); 1499 imx_udc_disable(imx_usb);
1514 del_timer(&imx_usb->timer); 1500 del_timer(&imx_usb->timer);
1515 1501
diff --git a/drivers/usb/gadget/lpc32xx_udc.c b/drivers/usb/gadget/lpc32xx_udc.c
index aa04089d6899..b943d8cdfbf7 100644
--- a/drivers/usb/gadget/lpc32xx_udc.c
+++ b/drivers/usb/gadget/lpc32xx_udc.c
@@ -1469,23 +1469,7 @@ static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1469 status = req->req.status; 1469 status = req->req.status;
1470 1470
1471 if (ep->lep) { 1471 if (ep->lep) {
1472 enum dma_data_direction direction; 1472 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
1473
1474 if (ep->is_in)
1475 direction = DMA_TO_DEVICE;
1476 else
1477 direction = DMA_FROM_DEVICE;
1478
1479 if (req->mapped) {
1480 dma_unmap_single(ep->udc->gadget.dev.parent,
1481 req->req.dma, req->req.length,
1482 direction);
1483 req->req.dma = 0;
1484 req->mapped = 0;
1485 } else
1486 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
1487 req->req.dma, req->req.length,
1488 direction);
1489 1473
1490 /* Free DDs */ 1474 /* Free DDs */
1491 udc_dd_free(udc, req->dd_desc_ptr); 1475 udc_dd_free(udc, req->dd_desc_ptr);
@@ -1841,26 +1825,11 @@ static int lpc32xx_ep_queue(struct usb_ep *_ep,
1841 } 1825 }
1842 1826
1843 if (ep->lep) { 1827 if (ep->lep) {
1844 enum dma_data_direction direction;
1845 struct lpc32xx_usbd_dd_gad *dd; 1828 struct lpc32xx_usbd_dd_gad *dd;
1846 1829
1847 /* Map DMA pointer */ 1830 status = usb_gadget_map_request(&udc->gadget, _req, ep->is_in);
1848 if (ep->is_in) 1831 if (status)
1849 direction = DMA_TO_DEVICE; 1832 return status;
1850 else
1851 direction = DMA_FROM_DEVICE;
1852
1853 if (req->req.dma == 0) {
1854 req->req.dma = dma_map_single(
1855 ep->udc->gadget.dev.parent,
1856 req->req.buf, req->req.length, direction);
1857 req->mapped = 1;
1858 } else {
1859 dma_sync_single_for_device(
1860 ep->udc->gadget.dev.parent, req->req.dma,
1861 req->req.length, direction);
1862 req->mapped = 0;
1863 }
1864 1833
1865 /* For the request, build a list of DDs */ 1834 /* For the request, build a list of DDs */
1866 dd = udc_dd_alloc(udc); 1835 dd = udc_dd_alloc(udc);
@@ -2977,7 +2946,6 @@ static int lpc32xx_start(struct usb_gadget *gadget,
2977 } 2946 }
2978 2947
2979 udc->driver = driver; 2948 udc->driver = driver;
2980 udc->gadget.dev.driver = &driver->driver;
2981 udc->gadget.dev.of_node = udc->dev->of_node; 2949 udc->gadget.dev.of_node = udc->dev->of_node;
2982 udc->enabled = 1; 2950 udc->enabled = 1;
2983 udc->selfpowered = 1; 2951 udc->selfpowered = 1;
@@ -3026,7 +2994,6 @@ static int lpc32xx_stop(struct usb_gadget *gadget,
3026 } 2994 }
3027 2995
3028 udc->enabled = 0; 2996 udc->enabled = 0;
3029 udc->gadget.dev.driver = NULL;
3030 udc->driver = NULL; 2997 udc->driver = NULL;
3031 2998
3032 return 0; 2999 return 0;
@@ -3248,12 +3215,6 @@ static int __init lpc32xx_udc_probe(struct platform_device *pdev)
3248 udc_disable(udc); 3215 udc_disable(udc);
3249 udc_reinit(udc); 3216 udc_reinit(udc);
3250 3217
3251 retval = device_register(&udc->gadget.dev);
3252 if (retval < 0) {
3253 dev_err(udc->dev, "Device registration failure\n");
3254 goto dev_register_fail;
3255 }
3256
3257 /* Request IRQs - low and high priority USB device IRQs are routed to 3218 /* Request IRQs - low and high priority USB device IRQs are routed to
3258 * the same handler, while the DMA interrupt is routed elsewhere */ 3219 * the same handler, while the DMA interrupt is routed elsewhere */
3259 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq, 3220 retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
@@ -3320,8 +3281,6 @@ irq_dev_fail:
3320irq_hp_fail: 3281irq_hp_fail:
3321 free_irq(udc->udp_irq[IRQ_USB_LP], udc); 3282 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3322irq_lp_fail: 3283irq_lp_fail:
3323 device_unregister(&udc->gadget.dev);
3324dev_register_fail:
3325 dma_pool_destroy(udc->dd_cache); 3284 dma_pool_destroy(udc->dd_cache);
3326dma_alloc_fail: 3285dma_alloc_fail:
3327 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE, 3286 dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
@@ -3376,8 +3335,6 @@ static int lpc32xx_udc_remove(struct platform_device *pdev)
3376 free_irq(udc->udp_irq[IRQ_USB_HP], udc); 3335 free_irq(udc->udp_irq[IRQ_USB_HP], udc);
3377 free_irq(udc->udp_irq[IRQ_USB_LP], udc); 3336 free_irq(udc->udp_irq[IRQ_USB_LP], udc);
3378 3337
3379 device_unregister(&udc->gadget.dev);
3380
3381 clk_disable(udc->usb_otg_clk); 3338 clk_disable(udc->usb_otg_clk);
3382 clk_put(udc->usb_otg_clk); 3339 clk_put(udc->usb_otg_clk);
3383 clk_disable(udc->usb_slv_clk); 3340 clk_disable(udc->usb_slv_clk);
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c
index c1b8c2dd808d..866ef0999247 100644
--- a/drivers/usb/gadget/m66592-udc.c
+++ b/drivers/usb/gadget/m66592-udc.c
@@ -1471,7 +1471,6 @@ static int m66592_udc_start(struct usb_gadget *g,
1471 /* hook up the driver */ 1471 /* hook up the driver */
1472 driver->driver.bus = NULL; 1472 driver->driver.bus = NULL;
1473 m66592->driver = driver; 1473 m66592->driver = driver;
1474 m66592->gadget.dev.driver = &driver->driver;
1475 1474
1476 m66592_bset(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0); 1475 m66592_bset(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);
1477 if (m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS) { 1476 if (m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS) {
@@ -1494,7 +1493,6 @@ static int m66592_udc_stop(struct usb_gadget *g,
1494 m66592_bclr(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0); 1493 m66592_bclr(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);
1495 1494
1496 driver->unbind(&m66592->gadget); 1495 driver->unbind(&m66592->gadget);
1497 m66592->gadget.dev.driver = NULL;
1498 1496
1499 init_controller(m66592); 1497 init_controller(m66592);
1500 disable_controller(m66592); 1498 disable_controller(m66592);
@@ -1538,7 +1536,6 @@ static int __exit m66592_remove(struct platform_device *pdev)
1538 struct m66592 *m66592 = dev_get_drvdata(&pdev->dev); 1536 struct m66592 *m66592 = dev_get_drvdata(&pdev->dev);
1539 1537
1540 usb_del_gadget_udc(&m66592->gadget); 1538 usb_del_gadget_udc(&m66592->gadget);
1541 device_del(&m66592->gadget.dev);
1542 1539
1543 del_timer_sync(&m66592->timer); 1540 del_timer_sync(&m66592->timer);
1544 iounmap(m66592->reg); 1541 iounmap(m66592->reg);
@@ -1608,12 +1605,7 @@ static int __init m66592_probe(struct platform_device *pdev)
1608 dev_set_drvdata(&pdev->dev, m66592); 1605 dev_set_drvdata(&pdev->dev, m66592);
1609 1606
1610 m66592->gadget.ops = &m66592_gadget_ops; 1607 m66592->gadget.ops = &m66592_gadget_ops;
1611 device_initialize(&m66592->gadget.dev);
1612 dev_set_name(&m66592->gadget.dev, "gadget");
1613 m66592->gadget.max_speed = USB_SPEED_HIGH; 1608 m66592->gadget.max_speed = USB_SPEED_HIGH;
1614 m66592->gadget.dev.parent = &pdev->dev;
1615 m66592->gadget.dev.dma_mask = pdev->dev.dma_mask;
1616 m66592->gadget.dev.release = pdev->dev.release;
1617 m66592->gadget.name = udc_name; 1609 m66592->gadget.name = udc_name;
1618 1610
1619 init_timer(&m66592->timer); 1611 init_timer(&m66592->timer);
@@ -1674,12 +1666,6 @@ static int __init m66592_probe(struct platform_device *pdev)
1674 1666
1675 init_controller(m66592); 1667 init_controller(m66592);
1676 1668
1677 ret = device_add(&m66592->gadget.dev);
1678 if (ret) {
1679 pr_err("device_add error (%d)\n", ret);
1680 goto err_device_add;
1681 }
1682
1683 ret = usb_add_gadget_udc(&pdev->dev, &m66592->gadget); 1669 ret = usb_add_gadget_udc(&pdev->dev, &m66592->gadget);
1684 if (ret) 1670 if (ret)
1685 goto err_add_udc; 1671 goto err_add_udc;
@@ -1688,9 +1674,6 @@ static int __init m66592_probe(struct platform_device *pdev)
1688 return 0; 1674 return 0;
1689 1675
1690err_add_udc: 1676err_add_udc:
1691 device_del(&m66592->gadget.dev);
1692
1693err_device_add:
1694 m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req); 1677 m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req);
1695 1678
1696clean_up3: 1679clean_up3:
diff --git a/drivers/usb/gadget/multi.c b/drivers/usb/gadget/multi.c
index 20bbbf917fc2..a74ebefc7682 100644
--- a/drivers/usb/gadget/multi.c
+++ b/drivers/usb/gadget/multi.c
@@ -135,8 +135,8 @@ static struct fsg_common fsg_common;
135 135
136static u8 hostaddr[ETH_ALEN]; 136static u8 hostaddr[ETH_ALEN];
137 137
138static unsigned char tty_line;
139static struct usb_function_instance *fi_acm; 138static struct usb_function_instance *fi_acm;
139static struct eth_dev *the_dev;
140 140
141/********** RNDIS **********/ 141/********** RNDIS **********/
142 142
@@ -152,7 +152,7 @@ static __init int rndis_do_config(struct usb_configuration *c)
152 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 152 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
153 } 153 }
154 154
155 ret = rndis_bind_config(c, hostaddr); 155 ret = rndis_bind_config(c, hostaddr, the_dev);
156 if (ret < 0) 156 if (ret < 0)
157 return ret; 157 return ret;
158 158
@@ -214,7 +214,7 @@ static __init int cdc_do_config(struct usb_configuration *c)
214 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 214 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
215 } 215 }
216 216
217 ret = ecm_bind_config(c, hostaddr); 217 ret = ecm_bind_config(c, hostaddr, the_dev);
218 if (ret < 0) 218 if (ret < 0)
219 return ret; 219 return ret;
220 220
@@ -269,7 +269,6 @@ static int cdc_config_register(struct usb_composite_dev *cdev)
269static int __ref multi_bind(struct usb_composite_dev *cdev) 269static int __ref multi_bind(struct usb_composite_dev *cdev)
270{ 270{
271 struct usb_gadget *gadget = cdev->gadget; 271 struct usb_gadget *gadget = cdev->gadget;
272 struct f_serial_opts *opts;
273 int status; 272 int status;
274 273
275 if (!can_support_ecm(cdev->gadget)) { 274 if (!can_support_ecm(cdev->gadget)) {
@@ -279,24 +278,17 @@ static int __ref multi_bind(struct usb_composite_dev *cdev)
279 } 278 }
280 279
281 /* set up network link layer */ 280 /* set up network link layer */
282 status = gether_setup(cdev->gadget, hostaddr); 281 the_dev = gether_setup(cdev->gadget, hostaddr);
283 if (status < 0) 282 if (IS_ERR(the_dev))
284 return status; 283 return PTR_ERR(the_dev);
285 284
286 /* set up serial link layer */ 285 /* set up serial link layer */
287 status = gserial_alloc_line(&tty_line);
288 if (status < 0)
289 goto fail0;
290
291 fi_acm = usb_get_function_instance("acm"); 286 fi_acm = usb_get_function_instance("acm");
292 if (IS_ERR(fi_acm)) { 287 if (IS_ERR(fi_acm)) {
293 status = PTR_ERR(fi_acm); 288 status = PTR_ERR(fi_acm);
294 goto fail0dot5; 289 goto fail0;
295 } 290 }
296 291
297 opts = container_of(fi_acm, struct f_serial_opts, func_inst);
298 opts->port_num = tty_line;
299
300 /* set up mass storage function */ 292 /* set up mass storage function */
301 { 293 {
302 void *retp; 294 void *retp;
@@ -334,10 +326,8 @@ fail2:
334 fsg_common_put(&fsg_common); 326 fsg_common_put(&fsg_common);
335fail1: 327fail1:
336 usb_put_function_instance(fi_acm); 328 usb_put_function_instance(fi_acm);
337fail0dot5:
338 gserial_free_line(tty_line);
339fail0: 329fail0:
340 gether_cleanup(); 330 gether_cleanup(the_dev);
341 return status; 331 return status;
342} 332}
343 333
@@ -350,8 +340,7 @@ static int __exit multi_unbind(struct usb_composite_dev *cdev)
350 usb_put_function(f_acm_rndis); 340 usb_put_function(f_acm_rndis);
351#endif 341#endif
352 usb_put_function_instance(fi_acm); 342 usb_put_function_instance(fi_acm);
353 gserial_free_line(tty_line); 343 gether_cleanup(the_dev);
354 gether_cleanup();
355 return 0; 344 return 0;
356} 345}
357 346
diff --git a/drivers/usb/gadget/mv_u3d_core.c b/drivers/usb/gadget/mv_u3d_core.c
index b5cea273c957..58288e9cf728 100644
--- a/drivers/usb/gadget/mv_u3d_core.c
+++ b/drivers/usb/gadget/mv_u3d_core.c
@@ -30,9 +30,6 @@
30#include <linux/platform_device.h> 30#include <linux/platform_device.h>
31#include <linux/platform_data/mv_usb.h> 31#include <linux/platform_data/mv_usb.h>
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <asm/system.h>
34#include <asm/unaligned.h>
35#include <asm/byteorder.h>
36 33
37#include "mv_u3d.h" 34#include "mv_u3d.h"
38 35
@@ -125,7 +122,7 @@ static int mv_u3d_process_ep_req(struct mv_u3d *u3d, int index,
125 struct mv_u3d_trb *curr_trb; 122 struct mv_u3d_trb *curr_trb;
126 dma_addr_t cur_deq_lo; 123 dma_addr_t cur_deq_lo;
127 struct mv_u3d_ep_context *curr_ep_context; 124 struct mv_u3d_ep_context *curr_ep_context;
128 int trb_complete, actual, remaining_length; 125 int trb_complete, actual, remaining_length = 0;
129 int direction, ep_num; 126 int direction, ep_num;
130 int retval = 0; 127 int retval = 0;
131 u32 tmp, status, length; 128 u32 tmp, status, length;
@@ -189,6 +186,8 @@ static int mv_u3d_process_ep_req(struct mv_u3d *u3d, int index,
189 */ 186 */
190static 187static
191void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status) 188void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status)
189 __releases(&ep->udc->lock)
190 __acquires(&ep->udc->lock)
192{ 191{
193 struct mv_u3d *u3d = (struct mv_u3d *)ep->u3d; 192 struct mv_u3d *u3d = (struct mv_u3d *)ep->u3d;
194 193
@@ -812,19 +811,19 @@ mv_u3d_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
812 return 0; 811 return 0;
813 } 812 }
814 813
815 dev_dbg(u3d->dev, "%s: %s, req: 0x%x\n", 814 dev_dbg(u3d->dev, "%s: %s, req: 0x%p\n",
816 __func__, _ep->name, (u32)req); 815 __func__, _ep->name, req);
817 816
818 /* catch various bogus parameters */ 817 /* catch various bogus parameters */
819 if (!req->req.complete || !req->req.buf 818 if (!req->req.complete || !req->req.buf
820 || !list_empty(&req->queue)) { 819 || !list_empty(&req->queue)) {
821 dev_err(u3d->dev, 820 dev_err(u3d->dev,
822 "%s, bad params, _req: 0x%x," 821 "%s, bad params, _req: 0x%p,"
823 "req->req.complete: 0x%x, req->req.buf: 0x%x," 822 "req->req.complete: 0x%p, req->req.buf: 0x%p,"
824 "list_empty: 0x%x\n", 823 "list_empty: 0x%x\n",
825 __func__, (u32)_req, 824 __func__, _req,
826 (u32)req->req.complete, (u32)req->req.buf, 825 req->req.complete, req->req.buf,
827 (u32)list_empty(&req->queue)); 826 list_empty(&req->queue));
828 return -EINVAL; 827 return -EINVAL;
829 } 828 }
830 if (unlikely(!ep->ep.desc)) { 829 if (unlikely(!ep->ep.desc)) {
@@ -905,7 +904,7 @@ static int mv_u3d_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
905 struct mv_u3d_req, queue); 904 struct mv_u3d_req, queue);
906 905
907 /* Point first TRB of next request to the EP context. */ 906 /* Point first TRB of next request to the EP context. */
908 iowrite32((u32) next_req->trb_head, 907 iowrite32((unsigned long) next_req->trb_head,
909 &ep_context->trb_addr_lo); 908 &ep_context->trb_addr_lo);
910 } else { 909 } else {
911 struct mv_u3d_ep_context *ep_context; 910 struct mv_u3d_ep_context *ep_context;
@@ -1264,7 +1263,6 @@ static int mv_u3d_start(struct usb_gadget *g,
1264 /* hook up the driver ... */ 1263 /* hook up the driver ... */
1265 driver->driver.bus = NULL; 1264 driver->driver.bus = NULL;
1266 u3d->driver = driver; 1265 u3d->driver = driver;
1267 u3d->gadget.dev.driver = &driver->driver;
1268 1266
1269 u3d->ep0_dir = USB_DIR_OUT; 1267 u3d->ep0_dir = USB_DIR_OUT;
1270 1268
@@ -1302,7 +1300,6 @@ static int mv_u3d_stop(struct usb_gadget *g,
1302 1300
1303 spin_unlock_irqrestore(&u3d->lock, flags); 1301 spin_unlock_irqrestore(&u3d->lock, flags);
1304 1302
1305 u3d->gadget.dev.driver = NULL;
1306 u3d->driver = NULL; 1303 u3d->driver = NULL;
1307 1304
1308 return 0; 1305 return 0;
@@ -1525,6 +1522,8 @@ static int mv_u3d_is_set_configuration(struct usb_ctrlrequest *setup)
1525 1522
1526static void mv_u3d_handle_setup_packet(struct mv_u3d *u3d, u8 ep_num, 1523static void mv_u3d_handle_setup_packet(struct mv_u3d *u3d, u8 ep_num,
1527 struct usb_ctrlrequest *setup) 1524 struct usb_ctrlrequest *setup)
1525 __releases(&u3c->lock)
1526 __acquires(&u3c->lock)
1528{ 1527{
1529 bool delegate = false; 1528 bool delegate = false;
1530 1529
@@ -1758,11 +1757,6 @@ static irqreturn_t mv_u3d_irq(int irq, void *dev)
1758 return IRQ_HANDLED; 1757 return IRQ_HANDLED;
1759} 1758}
1760 1759
1761static void mv_u3d_gadget_release(struct device *dev)
1762{
1763 dev_dbg(dev, "%s\n", __func__);
1764}
1765
1766static int mv_u3d_remove(struct platform_device *dev) 1760static int mv_u3d_remove(struct platform_device *dev)
1767{ 1761{
1768 struct mv_u3d *u3d = platform_get_drvdata(dev); 1762 struct mv_u3d *u3d = platform_get_drvdata(dev);
@@ -1792,8 +1786,6 @@ static int mv_u3d_remove(struct platform_device *dev)
1792 1786
1793 clk_put(u3d->clk); 1787 clk_put(u3d->clk);
1794 1788
1795 device_unregister(&u3d->gadget.dev);
1796
1797 platform_set_drvdata(dev, NULL); 1789 platform_set_drvdata(dev, NULL);
1798 1790
1799 kfree(u3d); 1791 kfree(u3d);
@@ -1829,7 +1821,7 @@ static int mv_u3d_probe(struct platform_device *dev)
1829 u3d->dev = &dev->dev; 1821 u3d->dev = &dev->dev;
1830 u3d->vbus = pdata->vbus; 1822 u3d->vbus = pdata->vbus;
1831 1823
1832 u3d->clk = clk_get(&dev->dev, pdata->clkname[0]); 1824 u3d->clk = clk_get(&dev->dev, NULL);
1833 if (IS_ERR(u3d->clk)) { 1825 if (IS_ERR(u3d->clk)) {
1834 retval = PTR_ERR(u3d->clk); 1826 retval = PTR_ERR(u3d->clk);
1835 goto err_get_clk; 1827 goto err_get_clk;
@@ -1849,8 +1841,9 @@ static int mv_u3d_probe(struct platform_device *dev)
1849 retval = -EBUSY; 1841 retval = -EBUSY;
1850 goto err_map_cap_regs; 1842 goto err_map_cap_regs;
1851 } else { 1843 } else {
1852 dev_dbg(&dev->dev, "cap_regs address: 0x%x/0x%x\n", 1844 dev_dbg(&dev->dev, "cap_regs address: 0x%lx/0x%lx\n",
1853 (unsigned int)r->start, (unsigned int)u3d->cap_regs); 1845 (unsigned long) r->start,
1846 (unsigned long) u3d->cap_regs);
1854 } 1847 }
1855 1848
1856 /* we will access controller register, so enable the u3d controller */ 1849 /* we will access controller register, so enable the u3d controller */
@@ -1864,10 +1857,10 @@ static int mv_u3d_probe(struct platform_device *dev)
1864 } 1857 }
1865 } 1858 }
1866 1859
1867 u3d->op_regs = (struct mv_u3d_op_regs __iomem *)((u32)u3d->cap_regs 1860 u3d->op_regs = (struct mv_u3d_op_regs __iomem *)(u3d->cap_regs
1868 + MV_U3D_USB3_OP_REGS_OFFSET); 1861 + MV_U3D_USB3_OP_REGS_OFFSET);
1869 1862
1870 u3d->vuc_regs = (struct mv_u3d_vuc_regs __iomem *)((u32)u3d->cap_regs 1863 u3d->vuc_regs = (struct mv_u3d_vuc_regs __iomem *)(u3d->cap_regs
1871 + ioread32(&u3d->cap_regs->vuoff)); 1864 + ioread32(&u3d->cap_regs->vuoff));
1872 1865
1873 u3d->max_eps = 16; 1866 u3d->max_eps = 16;
@@ -1957,16 +1950,8 @@ static int mv_u3d_probe(struct platform_device *dev)
1957 u3d->gadget.speed = USB_SPEED_UNKNOWN; /* speed */ 1950 u3d->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
1958 1951
1959 /* the "gadget" abstracts/virtualizes the controller */ 1952 /* the "gadget" abstracts/virtualizes the controller */
1960 dev_set_name(&u3d->gadget.dev, "gadget");
1961 u3d->gadget.dev.parent = &dev->dev;
1962 u3d->gadget.dev.dma_mask = dev->dev.dma_mask;
1963 u3d->gadget.dev.release = mv_u3d_gadget_release;
1964 u3d->gadget.name = driver_name; /* gadget name */ 1953 u3d->gadget.name = driver_name; /* gadget name */
1965 1954
1966 retval = device_register(&u3d->gadget.dev);
1967 if (retval)
1968 goto err_register_gadget_device;
1969
1970 mv_u3d_eps_init(u3d); 1955 mv_u3d_eps_init(u3d);
1971 1956
1972 /* external vbus detection */ 1957 /* external vbus detection */
@@ -1991,8 +1976,6 @@ static int mv_u3d_probe(struct platform_device *dev)
1991 return 0; 1976 return 0;
1992 1977
1993err_unregister: 1978err_unregister:
1994 device_unregister(&u3d->gadget.dev);
1995err_register_gadget_device:
1996 free_irq(u3d->irq, &dev->dev); 1979 free_irq(u3d->irq, &dev->dev);
1997err_request_irq: 1980err_request_irq:
1998err_get_irq: 1981err_get_irq:
@@ -2021,7 +2004,7 @@ err_pdata:
2021 return retval; 2004 return retval;
2022} 2005}
2023 2006
2024#ifdef CONFIG_PM 2007#ifdef CONFIG_PM_SLEEP
2025static int mv_u3d_suspend(struct device *dev) 2008static int mv_u3d_suspend(struct device *dev)
2026{ 2009{
2027 struct mv_u3d *u3d = dev_get_drvdata(dev); 2010 struct mv_u3d *u3d = dev_get_drvdata(dev);
@@ -2064,10 +2047,10 @@ static int mv_u3d_resume(struct device *dev)
2064 2047
2065 return 0; 2048 return 0;
2066} 2049}
2067
2068SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops, mv_u3d_suspend, mv_u3d_resume);
2069#endif 2050#endif
2070 2051
2052static SIMPLE_DEV_PM_OPS(mv_u3d_pm_ops, mv_u3d_suspend, mv_u3d_resume);
2053
2071static void mv_u3d_shutdown(struct platform_device *dev) 2054static void mv_u3d_shutdown(struct platform_device *dev)
2072{ 2055{
2073 struct mv_u3d *u3d = dev_get_drvdata(&dev->dev); 2056 struct mv_u3d *u3d = dev_get_drvdata(&dev->dev);
@@ -2080,14 +2063,12 @@ static void mv_u3d_shutdown(struct platform_device *dev)
2080 2063
2081static struct platform_driver mv_u3d_driver = { 2064static struct platform_driver mv_u3d_driver = {
2082 .probe = mv_u3d_probe, 2065 .probe = mv_u3d_probe,
2083 .remove = __exit_p(mv_u3d_remove), 2066 .remove = mv_u3d_remove,
2084 .shutdown = mv_u3d_shutdown, 2067 .shutdown = mv_u3d_shutdown,
2085 .driver = { 2068 .driver = {
2086 .owner = THIS_MODULE, 2069 .owner = THIS_MODULE,
2087 .name = "mv-u3d", 2070 .name = "mv-u3d",
2088#ifdef CONFIG_PM
2089 .pm = &mv_u3d_pm_ops, 2071 .pm = &mv_u3d_pm_ops,
2090#endif
2091 }, 2072 },
2092}; 2073};
2093 2074
diff --git a/drivers/usb/gadget/mv_udc.h b/drivers/usb/gadget/mv_udc.h
index 9073436d8b24..be77f207dbaf 100644
--- a/drivers/usb/gadget/mv_udc.h
+++ b/drivers/usb/gadget/mv_udc.h
@@ -222,8 +222,7 @@ struct mv_udc {
222 struct mv_usb_platform_data *pdata; 222 struct mv_usb_platform_data *pdata;
223 223
224 /* some SOC has mutiple clock sources for USB*/ 224 /* some SOC has mutiple clock sources for USB*/
225 unsigned int clknum; 225 struct clk *clk;
226 struct clk *clk[0];
227}; 226};
228 227
229/* endpoint data structure */ 228/* endpoint data structure */
diff --git a/drivers/usb/gadget/mv_udc_core.c b/drivers/usb/gadget/mv_udc_core.c
index c8cf959057fe..c2a57023e467 100644
--- a/drivers/usb/gadget/mv_udc_core.c
+++ b/drivers/usb/gadget/mv_udc_core.c
@@ -212,6 +212,8 @@ static int process_ep_req(struct mv_udc *udc, int index,
212 * request is still in progress. 212 * request is still in progress.
213 */ 213 */
214static void done(struct mv_ep *ep, struct mv_req *req, int status) 214static void done(struct mv_ep *ep, struct mv_req *req, int status)
215 __releases(&ep->udc->lock)
216 __acquires(&ep->udc->lock)
215{ 217{
216 struct mv_udc *udc = NULL; 218 struct mv_udc *udc = NULL;
217 unsigned char stopped = ep->stopped; 219 unsigned char stopped = ep->stopped;
@@ -237,18 +239,7 @@ static void done(struct mv_ep *ep, struct mv_req *req, int status)
237 dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma); 239 dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma);
238 } 240 }
239 241
240 if (req->mapped) { 242 usb_gadget_unmap_request(&udc->gadget, &req->req, ep_dir(ep));
241 dma_unmap_single(ep->udc->gadget.dev.parent,
242 req->req.dma, req->req.length,
243 ((ep_dir(ep) == EP_DIR_IN) ?
244 DMA_TO_DEVICE : DMA_FROM_DEVICE));
245 req->req.dma = DMA_ADDR_INVALID;
246 req->mapped = 0;
247 } else
248 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
249 req->req.dma, req->req.length,
250 ((ep_dir(ep) == EP_DIR_IN) ?
251 DMA_TO_DEVICE : DMA_FROM_DEVICE));
252 243
253 if (status && (status != -ESHUTDOWN)) 244 if (status && (status != -ESHUTDOWN))
254 dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u", 245 dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u",
@@ -732,21 +723,9 @@ mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
732 req->ep = ep; 723 req->ep = ep;
733 724
734 /* map virtual address to hardware */ 725 /* map virtual address to hardware */
735 if (req->req.dma == DMA_ADDR_INVALID) { 726 retval = usb_gadget_map_request(&udc->gadget, _req, ep_dir(ep));
736 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent, 727 if (retval)
737 req->req.buf, 728 return retval;
738 req->req.length, ep_dir(ep)
739 ? DMA_TO_DEVICE
740 : DMA_FROM_DEVICE);
741 req->mapped = 1;
742 } else {
743 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
744 req->req.dma, req->req.length,
745 ep_dir(ep)
746 ? DMA_TO_DEVICE
747 : DMA_FROM_DEVICE);
748 req->mapped = 0;
749 }
750 729
751 req->req.status = -EINPROGRESS; 730 req->req.status = -EINPROGRESS;
752 req->req.actual = 0; 731 req->req.actual = 0;
@@ -780,18 +759,7 @@ mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
780 return 0; 759 return 0;
781 760
782err_unmap_dma: 761err_unmap_dma:
783 if (req->mapped) { 762 usb_gadget_unmap_request(&udc->gadget, _req, ep_dir(ep));
784 dma_unmap_single(ep->udc->gadget.dev.parent,
785 req->req.dma, req->req.length,
786 ((ep_dir(ep) == EP_DIR_IN) ?
787 DMA_TO_DEVICE : DMA_FROM_DEVICE));
788 req->req.dma = DMA_ADDR_INVALID;
789 req->mapped = 0;
790 } else
791 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
792 req->req.dma, req->req.length,
793 ((ep_dir(ep) == EP_DIR_IN) ?
794 DMA_TO_DEVICE : DMA_FROM_DEVICE));
795 763
796 return retval; 764 return retval;
797} 765}
@@ -1006,18 +974,12 @@ static struct usb_ep_ops mv_ep_ops = {
1006 974
1007static void udc_clock_enable(struct mv_udc *udc) 975static void udc_clock_enable(struct mv_udc *udc)
1008{ 976{
1009 unsigned int i; 977 clk_prepare_enable(udc->clk);
1010
1011 for (i = 0; i < udc->clknum; i++)
1012 clk_prepare_enable(udc->clk[i]);
1013} 978}
1014 979
1015static void udc_clock_disable(struct mv_udc *udc) 980static void udc_clock_disable(struct mv_udc *udc)
1016{ 981{
1017 unsigned int i; 982 clk_disable_unprepare(udc->clk);
1018
1019 for (i = 0; i < udc->clknum; i++)
1020 clk_disable_unprepare(udc->clk[i]);
1021} 983}
1022 984
1023static void udc_stop(struct mv_udc *udc) 985static void udc_stop(struct mv_udc *udc)
@@ -1386,7 +1348,6 @@ static int mv_udc_start(struct usb_gadget *gadget,
1386 /* hook up the driver ... */ 1348 /* hook up the driver ... */
1387 driver->driver.bus = NULL; 1349 driver->driver.bus = NULL;
1388 udc->driver = driver; 1350 udc->driver = driver;
1389 udc->gadget.dev.driver = &driver->driver;
1390 1351
1391 udc->usb_state = USB_STATE_ATTACHED; 1352 udc->usb_state = USB_STATE_ATTACHED;
1392 udc->ep0_state = WAIT_FOR_SETUP; 1353 udc->ep0_state = WAIT_FOR_SETUP;
@@ -1401,7 +1362,6 @@ static int mv_udc_start(struct usb_gadget *gadget,
1401 dev_err(&udc->dev->dev, 1362 dev_err(&udc->dev->dev,
1402 "unable to register peripheral to otg\n"); 1363 "unable to register peripheral to otg\n");
1403 udc->driver = NULL; 1364 udc->driver = NULL;
1404 udc->gadget.dev.driver = NULL;
1405 return retval; 1365 return retval;
1406 } 1366 }
1407 } 1367 }
@@ -1437,7 +1397,6 @@ static int mv_udc_stop(struct usb_gadget *gadget,
1437 spin_unlock_irqrestore(&udc->lock, flags); 1397 spin_unlock_irqrestore(&udc->lock, flags);
1438 1398
1439 /* unbind gadget driver */ 1399 /* unbind gadget driver */
1440 udc->gadget.dev.driver = NULL;
1441 udc->driver = NULL; 1400 udc->driver = NULL;
1442 1401
1443 return 0; 1402 return 0;
@@ -1528,14 +1487,7 @@ udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
1528 1487
1529 return 0; 1488 return 0;
1530out: 1489out:
1531 if (req->mapped) { 1490 usb_gadget_unmap_request(&udc->gadget, &req->req, ep_dir(ep));
1532 dma_unmap_single(ep->udc->gadget.dev.parent,
1533 req->req.dma, req->req.length,
1534 ((ep_dir(ep) == EP_DIR_IN) ?
1535 DMA_TO_DEVICE : DMA_FROM_DEVICE));
1536 req->req.dma = DMA_ADDR_INVALID;
1537 req->mapped = 0;
1538 }
1539 1491
1540 return retval; 1492 return retval;
1541} 1493}
@@ -1695,6 +1647,8 @@ out:
1695 1647
1696static void handle_setup_packet(struct mv_udc *udc, u8 ep_num, 1648static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
1697 struct usb_ctrlrequest *setup) 1649 struct usb_ctrlrequest *setup)
1650 __releases(&ep->udc->lock)
1651 __acquires(&ep->udc->lock)
1698{ 1652{
1699 bool delegate = false; 1653 bool delegate = false;
1700 1654
@@ -1891,7 +1845,7 @@ static void irq_process_tr_complete(struct mv_udc *udc)
1891 } 1845 }
1892} 1846}
1893 1847
1894void irq_process_reset(struct mv_udc *udc) 1848static void irq_process_reset(struct mv_udc *udc)
1895{ 1849{
1896 u32 tmp; 1850 u32 tmp;
1897 unsigned int loops; 1851 unsigned int loops;
@@ -2138,8 +2092,6 @@ static int mv_udc_remove(struct platform_device *pdev)
2138 2092
2139 mv_udc_disable(udc); 2093 mv_udc_disable(udc);
2140 2094
2141 device_unregister(&udc->gadget.dev);
2142
2143 /* free dev, wait for the release() finished */ 2095 /* free dev, wait for the release() finished */
2144 wait_for_completion(udc->done); 2096 wait_for_completion(udc->done);
2145 2097
@@ -2151,7 +2103,6 @@ static int mv_udc_probe(struct platform_device *pdev)
2151 struct mv_usb_platform_data *pdata = pdev->dev.platform_data; 2103 struct mv_usb_platform_data *pdata = pdev->dev.platform_data;
2152 struct mv_udc *udc; 2104 struct mv_udc *udc;
2153 int retval = 0; 2105 int retval = 0;
2154 int clk_i = 0;
2155 struct resource *r; 2106 struct resource *r;
2156 size_t size; 2107 size_t size;
2157 2108
@@ -2160,8 +2111,7 @@ static int mv_udc_probe(struct platform_device *pdev)
2160 return -ENODEV; 2111 return -ENODEV;
2161 } 2112 }
2162 2113
2163 size = sizeof(*udc) + sizeof(struct clk *) * pdata->clknum; 2114 udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
2164 udc = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
2165 if (udc == NULL) { 2115 if (udc == NULL) {
2166 dev_err(&pdev->dev, "failed to allocate memory for udc\n"); 2116 dev_err(&pdev->dev, "failed to allocate memory for udc\n");
2167 return -ENOMEM; 2117 return -ENOMEM;
@@ -2173,26 +2123,24 @@ static int mv_udc_probe(struct platform_device *pdev)
2173 2123
2174 udc->dev = pdev; 2124 udc->dev = pdev;
2175 2125
2176#ifdef CONFIG_USB_OTG_UTILS
2177 if (pdata->mode == MV_USB_MODE_OTG) { 2126 if (pdata->mode == MV_USB_MODE_OTG) {
2178 udc->transceiver = devm_usb_get_phy(&pdev->dev, 2127 udc->transceiver = devm_usb_get_phy(&pdev->dev,
2179 USB_PHY_TYPE_USB2); 2128 USB_PHY_TYPE_USB2);
2180 if (IS_ERR_OR_NULL(udc->transceiver)) { 2129 if (IS_ERR(udc->transceiver)) {
2130 retval = PTR_ERR(udc->transceiver);
2131
2132 if (retval == -ENXIO)
2133 return retval;
2134
2181 udc->transceiver = NULL; 2135 udc->transceiver = NULL;
2182 return -ENODEV; 2136 return -EPROBE_DEFER;
2183 } 2137 }
2184 } 2138 }
2185#endif
2186 2139
2187 udc->clknum = pdata->clknum; 2140 /* udc only have one sysclk. */
2188 for (clk_i = 0; clk_i < udc->clknum; clk_i++) { 2141 udc->clk = devm_clk_get(&pdev->dev, NULL);
2189 udc->clk[clk_i] = devm_clk_get(&pdev->dev, 2142 if (IS_ERR(udc->clk))
2190 pdata->clkname[clk_i]); 2143 return PTR_ERR(udc->clk);
2191 if (IS_ERR(udc->clk[clk_i])) {
2192 retval = PTR_ERR(udc->clk[clk_i]);
2193 return retval;
2194 }
2195 }
2196 2144
2197 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "capregs"); 2145 r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "capregs");
2198 if (r == NULL) { 2146 if (r == NULL) {
@@ -2311,16 +2259,8 @@ static int mv_udc_probe(struct platform_device *pdev)
2311 udc->gadget.max_speed = USB_SPEED_HIGH; /* support dual speed */ 2259 udc->gadget.max_speed = USB_SPEED_HIGH; /* support dual speed */
2312 2260
2313 /* the "gadget" abstracts/virtualizes the controller */ 2261 /* the "gadget" abstracts/virtualizes the controller */
2314 dev_set_name(&udc->gadget.dev, "gadget");
2315 udc->gadget.dev.parent = &pdev->dev;
2316 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
2317 udc->gadget.dev.release = gadget_release;
2318 udc->gadget.name = driver_name; /* gadget name */ 2262 udc->gadget.name = driver_name; /* gadget name */
2319 2263
2320 retval = device_register(&udc->gadget.dev);
2321 if (retval)
2322 goto err_destroy_dma;
2323
2324 eps_init(udc); 2264 eps_init(udc);
2325 2265
2326 /* VBUS detect: we can disable/enable clock on demand.*/ 2266 /* VBUS detect: we can disable/enable clock on demand.*/
@@ -2342,7 +2282,7 @@ static int mv_udc_probe(struct platform_device *pdev)
2342 if (!udc->qwork) { 2282 if (!udc->qwork) {
2343 dev_err(&pdev->dev, "cannot create workqueue\n"); 2283 dev_err(&pdev->dev, "cannot create workqueue\n");
2344 retval = -ENOMEM; 2284 retval = -ENOMEM;
2345 goto err_unregister; 2285 goto err_destroy_dma;
2346 } 2286 }
2347 2287
2348 INIT_WORK(&udc->vbus_work, mv_udc_vbus_work); 2288 INIT_WORK(&udc->vbus_work, mv_udc_vbus_work);
@@ -2358,7 +2298,8 @@ static int mv_udc_probe(struct platform_device *pdev)
2358 else 2298 else
2359 udc->vbus_active = 1; 2299 udc->vbus_active = 1;
2360 2300
2361 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2301 retval = usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
2302 gadget_release);
2362 if (retval) 2303 if (retval)
2363 goto err_create_workqueue; 2304 goto err_create_workqueue;
2364 2305
@@ -2370,8 +2311,6 @@ static int mv_udc_probe(struct platform_device *pdev)
2370 2311
2371err_create_workqueue: 2312err_create_workqueue:
2372 destroy_workqueue(udc->qwork); 2313 destroy_workqueue(udc->qwork);
2373err_unregister:
2374 device_unregister(&udc->gadget.dev);
2375err_destroy_dma: 2314err_destroy_dma:
2376 dma_pool_destroy(udc->dtd_pool); 2315 dma_pool_destroy(udc->dtd_pool);
2377err_free_dma: 2316err_free_dma:
diff --git a/drivers/usb/gadget/ncm.c b/drivers/usb/gadget/ncm.c
index a22ad9af0565..3b02fd4649ce 100644
--- a/drivers/usb/gadget/ncm.c
+++ b/drivers/usb/gadget/ncm.c
@@ -111,6 +111,7 @@ static struct usb_gadget_strings *dev_strings[] = {
111 NULL, 111 NULL,
112}; 112};
113 113
114struct eth_dev *the_dev;
114static u8 hostaddr[ETH_ALEN]; 115static u8 hostaddr[ETH_ALEN];
115 116
116/*-------------------------------------------------------------------------*/ 117/*-------------------------------------------------------------------------*/
@@ -124,7 +125,7 @@ static int __init ncm_do_config(struct usb_configuration *c)
124 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 125 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
125 } 126 }
126 127
127 return ncm_bind_config(c, hostaddr); 128 return ncm_bind_config(c, hostaddr, the_dev);
128} 129}
129 130
130static struct usb_configuration ncm_config_driver = { 131static struct usb_configuration ncm_config_driver = {
@@ -143,9 +144,9 @@ static int __init gncm_bind(struct usb_composite_dev *cdev)
143 int status; 144 int status;
144 145
145 /* set up network link layer */ 146 /* set up network link layer */
146 status = gether_setup(cdev->gadget, hostaddr); 147 the_dev = gether_setup(cdev->gadget, hostaddr);
147 if (status < 0) 148 if (IS_ERR(the_dev))
148 return status; 149 return PTR_ERR(the_dev);
149 150
150 /* Allocate string descriptor numbers ... note that string 151 /* Allocate string descriptor numbers ... note that string
151 * contents can be overridden by the composite_dev glue. 152 * contents can be overridden by the composite_dev glue.
@@ -168,13 +169,13 @@ static int __init gncm_bind(struct usb_composite_dev *cdev)
168 return 0; 169 return 0;
169 170
170fail: 171fail:
171 gether_cleanup(); 172 gether_cleanup(the_dev);
172 return status; 173 return status;
173} 174}
174 175
175static int __exit gncm_unbind(struct usb_composite_dev *cdev) 176static int __exit gncm_unbind(struct usb_composite_dev *cdev)
176{ 177{
177 gether_cleanup(); 178 gether_cleanup(the_dev);
178 return 0; 179 return 0;
179} 180}
180 181
diff --git a/drivers/usb/gadget/net2272.c b/drivers/usb/gadget/net2272.c
index 32524b631959..f1e50a3e322d 100644
--- a/drivers/usb/gadget/net2272.c
+++ b/drivers/usb/gadget/net2272.c
@@ -58,7 +58,6 @@ static const char * const ep_name[] = {
58 "ep-a", "ep-b", "ep-c", 58 "ep-a", "ep-b", "ep-c",
59}; 59};
60 60
61#define DMA_ADDR_INVALID (~(dma_addr_t)0)
62#ifdef CONFIG_USB_NET2272_DMA 61#ifdef CONFIG_USB_NET2272_DMA
63/* 62/*
64 * use_dma: the NET2272 can use an external DMA controller. 63 * use_dma: the NET2272 can use an external DMA controller.
@@ -341,7 +340,6 @@ net2272_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
341 if (!req) 340 if (!req)
342 return NULL; 341 return NULL;
343 342
344 req->req.dma = DMA_ADDR_INVALID;
345 INIT_LIST_HEAD(&req->queue); 343 INIT_LIST_HEAD(&req->queue);
346 344
347 return &req->req; 345 return &req->req;
@@ -913,7 +911,7 @@ net2272_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
913 } 911 }
914 } 912 }
915 } 913 }
916 if (likely(req != 0)) 914 if (likely(req))
917 list_add_tail(&req->queue, &ep->queue); 915 list_add_tail(&req->queue, &ep->queue);
918 916
919 if (likely(!list_empty(&ep->queue))) 917 if (likely(!list_empty(&ep->queue)))
@@ -1467,7 +1465,6 @@ static int net2272_start(struct usb_gadget *_gadget,
1467 dev->softconnect = 1; 1465 dev->softconnect = 1;
1468 driver->driver.bus = NULL; 1466 driver->driver.bus = NULL;
1469 dev->driver = driver; 1467 dev->driver = driver;
1470 dev->gadget.dev.driver = &driver->driver;
1471 1468
1472 /* ... then enable host detection and ep0; and we're ready 1469 /* ... then enable host detection and ep0; and we're ready
1473 * for set_configuration as well as eventual disconnect. 1470 * for set_configuration as well as eventual disconnect.
@@ -1517,7 +1514,6 @@ static int net2272_stop(struct usb_gadget *_gadget,
1517 stop_activity(dev, driver); 1514 stop_activity(dev, driver);
1518 spin_unlock_irqrestore(&dev->lock, flags); 1515 spin_unlock_irqrestore(&dev->lock, flags);
1519 1516
1520 dev->gadget.dev.driver = NULL;
1521 dev->driver = NULL; 1517 dev->driver = NULL;
1522 1518
1523 dev_dbg(dev->dev, "unregistered driver '%s'\n", driver->driver.name); 1519 dev_dbg(dev->dev, "unregistered driver '%s'\n", driver->driver.name);
@@ -1549,7 +1545,7 @@ net2272_handle_dma(struct net2272_ep *ep)
1549 | (ep->dev->dma_eot_polarity << EOT_POLARITY) 1545 | (ep->dev->dma_eot_polarity << EOT_POLARITY)
1550 | (ep->dev->dma_dack_polarity << DACK_POLARITY) 1546 | (ep->dev->dma_dack_polarity << DACK_POLARITY)
1551 | (ep->dev->dma_dreq_polarity << DREQ_POLARITY) 1547 | (ep->dev->dma_dreq_polarity << DREQ_POLARITY)
1552 | ((ep->dma >> 1) << DMA_ENDPOINT_SELECT)); 1548 | (ep->dma << DMA_ENDPOINT_SELECT));
1553 1549
1554 ep->dev->dma_busy = 0; 1550 ep->dev->dma_busy = 0;
1555 1551
@@ -1622,7 +1618,7 @@ net2272_handle_ep(struct net2272_ep *ep)
1622 ep->irqs++; 1618 ep->irqs++;
1623 1619
1624 dev_vdbg(ep->dev->dev, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n", 1620 dev_vdbg(ep->dev->dev, "%s ack ep_stat0 %02x, ep_stat1 %02x, req %p\n",
1625 ep->ep.name, stat0, stat1, req ? &req->req : 0); 1621 ep->ep.name, stat0, stat1, req ? &req->req : NULL);
1626 1622
1627 net2272_ep_write(ep, EP_STAT0, stat0 & 1623 net2272_ep_write(ep, EP_STAT0, stat0 &
1628 ~((1 << NAK_OUT_PACKETS) 1624 ~((1 << NAK_OUT_PACKETS)
@@ -2216,7 +2212,6 @@ net2272_remove(struct net2272 *dev)
2216 free_irq(dev->irq, dev); 2212 free_irq(dev->irq, dev);
2217 iounmap(dev->base_addr); 2213 iounmap(dev->base_addr);
2218 2214
2219 device_unregister(&dev->gadget.dev);
2220 device_remove_file(dev->dev, &dev_attr_registers); 2215 device_remove_file(dev->dev, &dev_attr_registers);
2221 2216
2222 dev_info(dev->dev, "unbind\n"); 2217 dev_info(dev->dev, "unbind\n");
@@ -2243,10 +2238,6 @@ static struct net2272 *net2272_probe_init(struct device *dev, unsigned int irq)
2243 ret->gadget.max_speed = USB_SPEED_HIGH; 2238 ret->gadget.max_speed = USB_SPEED_HIGH;
2244 2239
2245 /* the "gadget" abstracts/virtualizes the controller */ 2240 /* the "gadget" abstracts/virtualizes the controller */
2246 dev_set_name(&ret->gadget.dev, "gadget");
2247 ret->gadget.dev.parent = dev;
2248 ret->gadget.dev.dma_mask = dev->dma_mask;
2249 ret->gadget.dev.release = net2272_gadget_release;
2250 ret->gadget.name = driver_name; 2241 ret->gadget.name = driver_name;
2251 2242
2252 return ret; 2243 return ret;
@@ -2282,14 +2273,12 @@ net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2282 dma_mode_string()); 2273 dma_mode_string());
2283 dev_info(dev->dev, "version: %s\n", driver_vers); 2274 dev_info(dev->dev, "version: %s\n", driver_vers);
2284 2275
2285 ret = device_register(&dev->gadget.dev);
2286 if (ret)
2287 goto err_irq;
2288 ret = device_create_file(dev->dev, &dev_attr_registers); 2276 ret = device_create_file(dev->dev, &dev_attr_registers);
2289 if (ret) 2277 if (ret)
2290 goto err_dev_reg; 2278 goto err_irq;
2291 2279
2292 ret = usb_add_gadget_udc(dev->dev, &dev->gadget); 2280 ret = usb_add_gadget_udc_release(dev->dev, &dev->gadget,
2281 net2272_gadget_release);
2293 if (ret) 2282 if (ret)
2294 goto err_add_udc; 2283 goto err_add_udc;
2295 2284
@@ -2297,8 +2286,6 @@ net2272_probe_fin(struct net2272 *dev, unsigned int irqflags)
2297 2286
2298err_add_udc: 2287err_add_udc:
2299 device_remove_file(dev->dev, &dev_attr_registers); 2288 device_remove_file(dev->dev, &dev_attr_registers);
2300 err_dev_reg:
2301 device_unregister(&dev->gadget.dev);
2302 err_irq: 2289 err_irq:
2303 free_irq(dev->irq, dev); 2290 free_irq(dev->irq, dev);
2304 err: 2291 err:
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index 3bd0f992fb49..fbd006ab31d3 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -65,7 +65,6 @@
65#define DRIVER_DESC "PLX NET228x USB Peripheral Controller" 65#define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
66#define DRIVER_VERSION "2005 Sept 27" 66#define DRIVER_VERSION "2005 Sept 27"
67 67
68#define DMA_ADDR_INVALID (~(dma_addr_t)0)
69#define EP_DONTUSE 13 /* nonzero */ 68#define EP_DONTUSE 13 /* nonzero */
70 69
71#define USE_RDK_LEDS /* GPIO pins control three LEDs */ 70#define USE_RDK_LEDS /* GPIO pins control three LEDs */
@@ -406,7 +405,6 @@ net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
406 if (!req) 405 if (!req)
407 return NULL; 406 return NULL;
408 407
409 req->req.dma = DMA_ADDR_INVALID;
410 INIT_LIST_HEAD (&req->queue); 408 INIT_LIST_HEAD (&req->queue);
411 409
412 /* this dma descriptor may be swapped with the previous dummy */ 410 /* this dma descriptor may be swapped with the previous dummy */
@@ -420,7 +418,6 @@ net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
420 return NULL; 418 return NULL;
421 } 419 }
422 td->dmacount = 0; /* not VALID */ 420 td->dmacount = 0; /* not VALID */
423 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
424 td->dmadesc = td->dmaaddr; 421 td->dmadesc = td->dmaaddr;
425 req->td = td; 422 req->td = td;
426 } 423 }
@@ -1896,7 +1893,6 @@ static int net2280_start(struct usb_gadget *_gadget,
1896 dev->softconnect = 1; 1893 dev->softconnect = 1;
1897 driver->driver.bus = NULL; 1894 driver->driver.bus = NULL;
1898 dev->driver = driver; 1895 dev->driver = driver;
1899 dev->gadget.dev.driver = &driver->driver;
1900 1896
1901 retval = device_create_file (&dev->pdev->dev, &dev_attr_function); 1897 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1902 if (retval) goto err_unbind; 1898 if (retval) goto err_unbind;
@@ -1924,7 +1920,6 @@ static int net2280_start(struct usb_gadget *_gadget,
1924err_func: 1920err_func:
1925 device_remove_file (&dev->pdev->dev, &dev_attr_function); 1921 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1926err_unbind: 1922err_unbind:
1927 dev->gadget.dev.driver = NULL;
1928 dev->driver = NULL; 1923 dev->driver = NULL;
1929 return retval; 1924 return retval;
1930} 1925}
@@ -1967,7 +1962,6 @@ static int net2280_stop(struct usb_gadget *_gadget,
1967 stop_activity (dev, driver); 1962 stop_activity (dev, driver);
1968 spin_unlock_irqrestore (&dev->lock, flags); 1963 spin_unlock_irqrestore (&dev->lock, flags);
1969 1964
1970 dev->gadget.dev.driver = NULL;
1971 dev->driver = NULL; 1965 dev->driver = NULL;
1972 1966
1973 net2280_led_active (dev, 0); 1967 net2280_led_active (dev, 0);
@@ -2072,7 +2066,7 @@ static void handle_ep_small (struct net2280_ep *ep)
2072 return; 2066 return;
2073 2067
2074 /* manual DMA queue advance after short OUT */ 2068 /* manual DMA queue advance after short OUT */
2075 if (likely (ep->dma != 0)) { 2069 if (likely (ep->dma)) {
2076 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) { 2070 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2077 u32 count; 2071 u32 count;
2078 int stopped = ep->stopped; 2072 int stopped = ep->stopped;
@@ -2330,7 +2324,7 @@ static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2330 /* hw handles device and interface status */ 2324 /* hw handles device and interface status */
2331 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT)) 2325 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2332 goto delegate; 2326 goto delegate;
2333 if ((e = get_ep_by_addr (dev, w_index)) == 0 2327 if ((e = get_ep_by_addr (dev, w_index)) == NULL
2334 || w_length > 2) 2328 || w_length > 2)
2335 goto do_stall; 2329 goto do_stall;
2336 2330
@@ -2358,7 +2352,7 @@ static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2358 if (w_value != USB_ENDPOINT_HALT 2352 if (w_value != USB_ENDPOINT_HALT
2359 || w_length != 0) 2353 || w_length != 0)
2360 goto do_stall; 2354 goto do_stall;
2361 if ((e = get_ep_by_addr (dev, w_index)) == 0) 2355 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
2362 goto do_stall; 2356 goto do_stall;
2363 if (e->wedged) { 2357 if (e->wedged) {
2364 VDEBUG(dev, "%s wedged, halt not cleared\n", 2358 VDEBUG(dev, "%s wedged, halt not cleared\n",
@@ -2380,7 +2374,7 @@ static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2380 if (w_value != USB_ENDPOINT_HALT 2374 if (w_value != USB_ENDPOINT_HALT
2381 || w_length != 0) 2375 || w_length != 0)
2382 goto do_stall; 2376 goto do_stall;
2383 if ((e = get_ep_by_addr (dev, w_index)) == 0) 2377 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
2384 goto do_stall; 2378 goto do_stall;
2385 if (e->ep.name == ep0name) 2379 if (e->ep.name == ep0name)
2386 goto do_stall; 2380 goto do_stall;
@@ -2685,7 +2679,6 @@ static void net2280_remove (struct pci_dev *pdev)
2685 pci_resource_len (pdev, 0)); 2679 pci_resource_len (pdev, 0));
2686 if (dev->enabled) 2680 if (dev->enabled)
2687 pci_disable_device (pdev); 2681 pci_disable_device (pdev);
2688 device_unregister (&dev->gadget.dev);
2689 device_remove_file (&pdev->dev, &dev_attr_registers); 2682 device_remove_file (&pdev->dev, &dev_attr_registers);
2690 pci_set_drvdata (pdev, NULL); 2683 pci_set_drvdata (pdev, NULL);
2691 2684
@@ -2717,10 +2710,6 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2717 dev->gadget.max_speed = USB_SPEED_HIGH; 2710 dev->gadget.max_speed = USB_SPEED_HIGH;
2718 2711
2719 /* the "gadget" abstracts/virtualizes the controller */ 2712 /* the "gadget" abstracts/virtualizes the controller */
2720 dev_set_name(&dev->gadget.dev, "gadget");
2721 dev->gadget.dev.parent = &pdev->dev;
2722 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2723 dev->gadget.dev.release = gadget_release;
2724 dev->gadget.name = driver_name; 2713 dev->gadget.name = driver_name;
2725 2714
2726 /* now all the pci goodies ... */ 2715 /* now all the pci goodies ... */
@@ -2802,7 +2791,6 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2802 goto done; 2791 goto done;
2803 } 2792 }
2804 td->dmacount = 0; /* not VALID */ 2793 td->dmacount = 0; /* not VALID */
2805 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
2806 td->dmadesc = td->dmaaddr; 2794 td->dmadesc = td->dmaaddr;
2807 dev->ep [i].dummy = td; 2795 dev->ep [i].dummy = td;
2808 } 2796 }
@@ -2829,12 +2817,11 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2829 use_dma 2817 use_dma
2830 ? (use_dma_chaining ? "chaining" : "enabled") 2818 ? (use_dma_chaining ? "chaining" : "enabled")
2831 : "disabled"); 2819 : "disabled");
2832 retval = device_register (&dev->gadget.dev);
2833 if (retval) goto done;
2834 retval = device_create_file (&pdev->dev, &dev_attr_registers); 2820 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2835 if (retval) goto done; 2821 if (retval) goto done;
2836 2822
2837 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget); 2823 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
2824 gadget_release);
2838 if (retval) 2825 if (retval)
2839 goto done; 2826 goto done;
2840 return 0; 2827 return 0;
diff --git a/drivers/usb/gadget/nokia.c b/drivers/usb/gadget/nokia.c
index def37403989a..3b344b41a167 100644
--- a/drivers/usb/gadget/nokia.c
+++ b/drivers/usb/gadget/nokia.c
@@ -37,11 +37,9 @@
37 * the runtime footprint, and giving us at least some parts of what 37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would. 38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */ 39 */
40#define USB_FACM_INCLUDED 40#define USBF_OBEX_INCLUDED
41#include "f_acm.c"
42#include "f_ecm.c" 41#include "f_ecm.c"
43#include "f_obex.c" 42#include "f_obex.c"
44#include "f_serial.c"
45#include "f_phonet.c" 43#include "f_phonet.c"
46#include "u_ether.c" 44#include "u_ether.c"
47 45
@@ -98,20 +96,40 @@ MODULE_AUTHOR("Felipe Balbi");
98MODULE_LICENSE("GPL"); 96MODULE_LICENSE("GPL");
99 97
100/*-------------------------------------------------------------------------*/ 98/*-------------------------------------------------------------------------*/
101 99static struct usb_function *f_acm_cfg1;
100static struct usb_function *f_acm_cfg2;
102static u8 hostaddr[ETH_ALEN]; 101static u8 hostaddr[ETH_ALEN];
102static struct eth_dev *the_dev;
103 103
104enum { 104enum {
105 TTY_PORT_OBEX0, 105 TTY_PORT_OBEX0,
106 TTY_PORT_OBEX1, 106 TTY_PORT_OBEX1,
107 TTY_PORT_ACM,
108 TTY_PORTS_MAX, 107 TTY_PORTS_MAX,
109}; 108};
110 109
111static unsigned char tty_lines[TTY_PORTS_MAX]; 110static unsigned char tty_lines[TTY_PORTS_MAX];
112 111
112static struct usb_configuration nokia_config_500ma_driver = {
113 .label = "Bus Powered",
114 .bConfigurationValue = 1,
115 /* .iConfiguration = DYNAMIC */
116 .bmAttributes = USB_CONFIG_ATT_ONE,
117 .MaxPower = 500,
118};
119
120static struct usb_configuration nokia_config_100ma_driver = {
121 .label = "Self Powered",
122 .bConfigurationValue = 2,
123 /* .iConfiguration = DYNAMIC */
124 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
125 .MaxPower = 100,
126};
127
128static struct usb_function_instance *fi_acm;
129
113static int __init nokia_bind_config(struct usb_configuration *c) 130static int __init nokia_bind_config(struct usb_configuration *c)
114{ 131{
132 struct usb_function *f_acm;
115 int status = 0; 133 int status = 0;
116 134
117 status = phonet_bind_config(c); 135 status = phonet_bind_config(c);
@@ -126,33 +144,32 @@ static int __init nokia_bind_config(struct usb_configuration *c)
126 if (status) 144 if (status)
127 printk(KERN_DEBUG "could not bind obex config %d\n", 0); 145 printk(KERN_DEBUG "could not bind obex config %d\n", 0);
128 146
129 status = acm_bind_config(c, tty_lines[TTY_PORT_ACM]); 147 f_acm = usb_get_function(fi_acm);
130 if (status) 148 if (IS_ERR(f_acm))
131 printk(KERN_DEBUG "could not bind acm config\n"); 149 return PTR_ERR(f_acm);
132 150
133 status = ecm_bind_config(c, hostaddr); 151 status = usb_add_function(c, f_acm);
134 if (status) 152 if (status)
135 printk(KERN_DEBUG "could not bind ecm config\n"); 153 goto err_conf;
154
155 status = ecm_bind_config(c, hostaddr, the_dev);
156 if (status) {
157 pr_debug("could not bind ecm config %d\n", status);
158 goto err_ecm;
159 }
160 if (c == &nokia_config_500ma_driver)
161 f_acm_cfg1 = f_acm;
162 else
163 f_acm_cfg2 = f_acm;
136 164
137 return status; 165 return status;
166err_ecm:
167 usb_remove_function(c, f_acm);
168err_conf:
169 usb_put_function(f_acm);
170 return status;
138} 171}
139 172
140static struct usb_configuration nokia_config_500ma_driver = {
141 .label = "Bus Powered",
142 .bConfigurationValue = 1,
143 /* .iConfiguration = DYNAMIC */
144 .bmAttributes = USB_CONFIG_ATT_ONE,
145 .MaxPower = 500,
146};
147
148static struct usb_configuration nokia_config_100ma_driver = {
149 .label = "Self Powered",
150 .bConfigurationValue = 2,
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
153 .MaxPower = 100,
154};
155
156static int __init nokia_bind(struct usb_composite_dev *cdev) 173static int __init nokia_bind(struct usb_composite_dev *cdev)
157{ 174{
158 struct usb_gadget *gadget = cdev->gadget; 175 struct usb_gadget *gadget = cdev->gadget;
@@ -169,9 +186,11 @@ static int __init nokia_bind(struct usb_composite_dev *cdev)
169 goto err_ether; 186 goto err_ether;
170 } 187 }
171 188
172 status = gether_setup(cdev->gadget, hostaddr); 189 the_dev = gether_setup(cdev->gadget, hostaddr);
173 if (status < 0) 190 if (IS_ERR(the_dev)) {
191 status = PTR_ERR(the_dev);
174 goto err_ether; 192 goto err_ether;
193 }
175 194
176 status = usb_string_ids_tab(cdev, strings_dev); 195 status = usb_string_ids_tab(cdev, strings_dev);
177 if (status < 0) 196 if (status < 0)
@@ -185,24 +204,32 @@ static int __init nokia_bind(struct usb_composite_dev *cdev)
185 if (!gadget_supports_altsettings(gadget)) 204 if (!gadget_supports_altsettings(gadget))
186 goto err_usb; 205 goto err_usb;
187 206
207 fi_acm = usb_get_function_instance("acm");
208 if (IS_ERR(fi_acm))
209 goto err_usb;
210
188 /* finally register the configuration */ 211 /* finally register the configuration */
189 status = usb_add_config(cdev, &nokia_config_500ma_driver, 212 status = usb_add_config(cdev, &nokia_config_500ma_driver,
190 nokia_bind_config); 213 nokia_bind_config);
191 if (status < 0) 214 if (status < 0)
192 goto err_usb; 215 goto err_acm_inst;
193 216
194 status = usb_add_config(cdev, &nokia_config_100ma_driver, 217 status = usb_add_config(cdev, &nokia_config_100ma_driver,
195 nokia_bind_config); 218 nokia_bind_config);
196 if (status < 0) 219 if (status < 0)
197 goto err_usb; 220 goto err_put_cfg1;
198 221
199 usb_composite_overwrite_options(cdev, &coverwrite); 222 usb_composite_overwrite_options(cdev, &coverwrite);
200 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME); 223 dev_info(&gadget->dev, "%s\n", NOKIA_LONG_NAME);
201 224
202 return 0; 225 return 0;
203 226
227err_put_cfg1:
228 usb_put_function(f_acm_cfg1);
229err_acm_inst:
230 usb_put_function_instance(fi_acm);
204err_usb: 231err_usb:
205 gether_cleanup(); 232 gether_cleanup(the_dev);
206err_ether: 233err_ether:
207 cur_line--; 234 cur_line--;
208 while (cur_line >= 0) 235 while (cur_line >= 0)
@@ -217,12 +244,15 @@ static int __exit nokia_unbind(struct usb_composite_dev *cdev)
217{ 244{
218 int i; 245 int i;
219 246
247 usb_put_function(f_acm_cfg1);
248 usb_put_function(f_acm_cfg2);
249 usb_put_function_instance(fi_acm);
220 gphonet_cleanup(); 250 gphonet_cleanup();
221 251
222 for (i = 0; i < TTY_PORTS_MAX; i++) 252 for (i = 0; i < TTY_PORTS_MAX; i++)
223 gserial_free_line(tty_lines[i]); 253 gserial_free_line(tty_lines[i]);
224 254
225 gether_cleanup(); 255 gether_cleanup(the_dev);
226 256
227 return 0; 257 return 0;
228} 258}
@@ -247,4 +277,3 @@ static void __exit nokia_cleanup(void)
247 usb_composite_unregister(&nokia_driver); 277 usb_composite_unregister(&nokia_driver);
248} 278}
249module_exit(nokia_cleanup); 279module_exit(nokia_cleanup);
250
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index f8445653577f..b8ed74a823cb 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -2067,7 +2067,6 @@ static int omap_udc_start(struct usb_gadget *g,
2067 /* hook up the driver */ 2067 /* hook up the driver */
2068 driver->driver.bus = NULL; 2068 driver->driver.bus = NULL;
2069 udc->driver = driver; 2069 udc->driver = driver;
2070 udc->gadget.dev.driver = &driver->driver;
2071 spin_unlock_irqrestore(&udc->lock, flags); 2070 spin_unlock_irqrestore(&udc->lock, flags);
2072 2071
2073 if (udc->dc_clk != NULL) 2072 if (udc->dc_clk != NULL)
@@ -2083,7 +2082,6 @@ static int omap_udc_start(struct usb_gadget *g,
2083 ERR("can't bind to transceiver\n"); 2082 ERR("can't bind to transceiver\n");
2084 if (driver->unbind) { 2083 if (driver->unbind) {
2085 driver->unbind(&udc->gadget); 2084 driver->unbind(&udc->gadget);
2086 udc->gadget.dev.driver = NULL;
2087 udc->driver = NULL; 2085 udc->driver = NULL;
2088 } 2086 }
2089 goto done; 2087 goto done;
@@ -2129,7 +2127,6 @@ static int omap_udc_stop(struct usb_gadget *g,
2129 udc_quiesce(udc); 2127 udc_quiesce(udc);
2130 spin_unlock_irqrestore(&udc->lock, flags); 2128 spin_unlock_irqrestore(&udc->lock, flags);
2131 2129
2132 udc->gadget.dev.driver = NULL;
2133 udc->driver = NULL; 2130 udc->driver = NULL;
2134 2131
2135 if (udc->dc_clk != NULL) 2132 if (udc->dc_clk != NULL)
@@ -2631,14 +2628,6 @@ omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2631 udc->gadget.speed = USB_SPEED_UNKNOWN; 2628 udc->gadget.speed = USB_SPEED_UNKNOWN;
2632 udc->gadget.max_speed = USB_SPEED_FULL; 2629 udc->gadget.max_speed = USB_SPEED_FULL;
2633 udc->gadget.name = driver_name; 2630 udc->gadget.name = driver_name;
2634
2635 device_initialize(&udc->gadget.dev);
2636 dev_set_name(&udc->gadget.dev, "gadget");
2637 udc->gadget.dev.release = omap_udc_release;
2638 udc->gadget.dev.parent = &odev->dev;
2639 if (use_dma)
2640 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2641
2642 udc->transceiver = xceiv; 2631 udc->transceiver = xceiv;
2643 2632
2644 /* ep0 is special; put it right after the SETUP buffer */ 2633 /* ep0 is special; put it right after the SETUP buffer */
@@ -2912,14 +2901,13 @@ bad_on_1710:
2912 } 2901 }
2913 2902
2914 create_proc_file(); 2903 create_proc_file();
2915 status = device_add(&udc->gadget.dev); 2904 status = usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
2905 omap_udc_release);
2916 if (status) 2906 if (status)
2917 goto cleanup4; 2907 goto cleanup4;
2918 2908
2919 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2909 return 0;
2920 if (!status) 2910
2921 return status;
2922 /* If fail, fall through */
2923cleanup4: 2911cleanup4:
2924 remove_proc_file(); 2912 remove_proc_file();
2925 2913
@@ -2990,7 +2978,6 @@ static int omap_udc_remove(struct platform_device *pdev)
2990 release_mem_region(pdev->resource[0].start, 2978 release_mem_region(pdev->resource[0].start,
2991 pdev->resource[0].end - pdev->resource[0].start + 1); 2979 pdev->resource[0].end - pdev->resource[0].start + 1);
2992 2980
2993 device_unregister(&udc->gadget.dev);
2994 wait_for_completion(&done); 2981 wait_for_completion(&done);
2995 2982
2996 return 0; 2983 return 0;
diff --git a/drivers/usb/gadget/pch_udc.c b/drivers/usb/gadget/pch_udc.c
index a787a8ef672b..24174e1d1564 100644
--- a/drivers/usb/gadget/pch_udc.c
+++ b/drivers/usb/gadget/pch_udc.c
@@ -358,7 +358,6 @@ struct pch_udc_dev {
358 prot_stall:1, 358 prot_stall:1,
359 irq_registered:1, 359 irq_registered:1,
360 mem_region:1, 360 mem_region:1,
361 registered:1,
362 suspended:1, 361 suspended:1,
363 connected:1, 362 connected:1,
364 vbus_session:1, 363 vbus_session:1,
@@ -1441,6 +1440,8 @@ static void pch_vbus_gpio_free(struct pch_udc_dev *dev)
1441 */ 1440 */
1442static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req, 1441static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req,
1443 int status) 1442 int status)
1443 __releases(&dev->lock)
1444 __acquires(&dev->lock)
1444{ 1445{
1445 struct pch_udc_dev *dev; 1446 struct pch_udc_dev *dev;
1446 unsigned halted = ep->halted; 1447 unsigned halted = ep->halted;
@@ -2382,6 +2383,8 @@ static void pch_udc_svc_control_in(struct pch_udc_dev *dev)
2382 * @dev: Reference to the device structure 2383 * @dev: Reference to the device structure
2383 */ 2384 */
2384static void pch_udc_svc_control_out(struct pch_udc_dev *dev) 2385static void pch_udc_svc_control_out(struct pch_udc_dev *dev)
2386 __releases(&dev->lock)
2387 __acquires(&dev->lock)
2385{ 2388{
2386 u32 stat; 2389 u32 stat;
2387 int setup_supported; 2390 int setup_supported;
@@ -2989,7 +2992,6 @@ static int pch_udc_start(struct usb_gadget *g,
2989 2992
2990 driver->driver.bus = NULL; 2993 driver->driver.bus = NULL;
2991 dev->driver = driver; 2994 dev->driver = driver;
2992 dev->gadget.dev.driver = &driver->driver;
2993 2995
2994 /* get ready for ep0 traffic */ 2996 /* get ready for ep0 traffic */
2995 pch_udc_setup_ep0(dev); 2997 pch_udc_setup_ep0(dev);
@@ -3010,7 +3012,6 @@ static int pch_udc_stop(struct usb_gadget *g,
3010 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK); 3012 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
3011 3013
3012 /* Assures that there are no pending requests with this driver */ 3014 /* Assures that there are no pending requests with this driver */
3013 dev->gadget.dev.driver = NULL;
3014 dev->driver = NULL; 3015 dev->driver = NULL;
3015 dev->connected = 0; 3016 dev->connected = 0;
3016 3017
@@ -3078,8 +3079,6 @@ static void pch_udc_remove(struct pci_dev *pdev)
3078 pci_resource_len(pdev, PCH_UDC_PCI_BAR)); 3079 pci_resource_len(pdev, PCH_UDC_PCI_BAR));
3079 if (dev->active) 3080 if (dev->active)
3080 pci_disable_device(pdev); 3081 pci_disable_device(pdev);
3081 if (dev->registered)
3082 device_unregister(&dev->gadget.dev);
3083 kfree(dev); 3082 kfree(dev);
3084 pci_set_drvdata(pdev, NULL); 3083 pci_set_drvdata(pdev, NULL);
3085} 3084}
@@ -3196,21 +3195,13 @@ static int pch_udc_probe(struct pci_dev *pdev,
3196 if (retval) 3195 if (retval)
3197 goto finished; 3196 goto finished;
3198 3197
3199 dev_set_name(&dev->gadget.dev, "gadget");
3200 dev->gadget.dev.parent = &pdev->dev;
3201 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
3202 dev->gadget.dev.release = gadget_release;
3203 dev->gadget.name = KBUILD_MODNAME; 3198 dev->gadget.name = KBUILD_MODNAME;
3204 dev->gadget.max_speed = USB_SPEED_HIGH; 3199 dev->gadget.max_speed = USB_SPEED_HIGH;
3205 3200
3206 retval = device_register(&dev->gadget.dev);
3207 if (retval)
3208 goto finished;
3209 dev->registered = 1;
3210
3211 /* Put the device in disconnected state till a driver is bound */ 3201 /* Put the device in disconnected state till a driver is bound */
3212 pch_udc_set_disconnect(dev); 3202 pch_udc_set_disconnect(dev);
3213 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget); 3203 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3204 gadget_release);
3214 if (retval) 3205 if (retval)
3215 goto finished; 3206 goto finished;
3216 return 0; 3207 return 0;
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index d0f37484b6b0..ef47495dec8f 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -1263,7 +1263,6 @@ static int pxa25x_udc_start(struct usb_gadget *g,
1263 1263
1264 /* first hook up the driver ... */ 1264 /* first hook up the driver ... */
1265 dev->driver = driver; 1265 dev->driver = driver;
1266 dev->gadget.dev.driver = &driver->driver;
1267 dev->pullup = 1; 1266 dev->pullup = 1;
1268 1267
1269 /* ... then enable host detection and ep0; and we're ready 1268 /* ... then enable host detection and ep0; and we're ready
@@ -1325,7 +1324,6 @@ static int pxa25x_udc_stop(struct usb_gadget*g,
1325 if (!IS_ERR_OR_NULL(dev->transceiver)) 1324 if (!IS_ERR_OR_NULL(dev->transceiver))
1326 (void) otg_set_peripheral(dev->transceiver->otg, NULL); 1325 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
1327 1326
1328 dev->gadget.dev.driver = NULL;
1329 dev->driver = NULL; 1327 dev->driver = NULL;
1330 1328
1331 dump_state(dev); 1329 dump_state(dev);
@@ -2138,17 +2136,6 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2138 dev->timer.function = udc_watchdog; 2136 dev->timer.function = udc_watchdog;
2139 dev->timer.data = (unsigned long) dev; 2137 dev->timer.data = (unsigned long) dev;
2140 2138
2141 device_initialize(&dev->gadget.dev);
2142 dev->gadget.dev.parent = &pdev->dev;
2143 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2144
2145 retval = device_add(&dev->gadget.dev);
2146 if (retval) {
2147 dev->driver = NULL;
2148 dev->gadget.dev.driver = NULL;
2149 goto err_device_add;
2150 }
2151
2152 the_controller = dev; 2139 the_controller = dev;
2153 platform_set_drvdata(pdev, dev); 2140 platform_set_drvdata(pdev, dev);
2154 2141
@@ -2199,8 +2186,6 @@ lubbock_fail0:
2199 free_irq(irq, dev); 2186 free_irq(irq, dev);
2200#endif 2187#endif
2201 err_irq1: 2188 err_irq1:
2202 device_unregister(&dev->gadget.dev);
2203 err_device_add:
2204 if (gpio_is_valid(dev->mach->gpio_pullup)) 2189 if (gpio_is_valid(dev->mach->gpio_pullup))
2205 gpio_free(dev->mach->gpio_pullup); 2190 gpio_free(dev->mach->gpio_pullup);
2206 err_gpio_pullup: 2191 err_gpio_pullup:
@@ -2226,7 +2211,6 @@ static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2226 return -EBUSY; 2211 return -EBUSY;
2227 2212
2228 usb_del_gadget_udc(&dev->gadget); 2213 usb_del_gadget_udc(&dev->gadget);
2229 device_unregister(&dev->gadget.dev);
2230 dev->pullup = 0; 2214 dev->pullup = 0;
2231 pullup(dev); 2215 pullup(dev);
2232 2216
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 2fc867652ef5..6b4c7d95853f 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -24,14 +24,12 @@
24#include <linux/gpio.h> 24#include <linux/gpio.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/prefetch.h> 26#include <linux/prefetch.h>
27 27#include <linux/byteorder/generic.h>
28#include <asm/byteorder.h> 28#include <linux/platform_data/pxa2xx_udc.h>
29#include <mach/hardware.h>
30 29
31#include <linux/usb.h> 30#include <linux/usb.h>
32#include <linux/usb/ch9.h> 31#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h> 32#include <linux/usb/gadget.h>
34#include <mach/udc.h>
35 33
36#include "pxa27x_udc.h" 34#include "pxa27x_udc.h"
37 35
@@ -611,7 +609,7 @@ static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
611 * 609 *
612 * Find the physical pxa27x ep, and setup its UDCCR 610 * Find the physical pxa27x ep, and setup its UDCCR
613 */ 611 */
614static __init void pxa_ep_setup(struct pxa_ep *ep) 612static void pxa_ep_setup(struct pxa_ep *ep)
615{ 613{
616 u32 new_udccr; 614 u32 new_udccr;
617 615
@@ -633,7 +631,7 @@ static __init void pxa_ep_setup(struct pxa_ep *ep)
633 * 631 *
634 * Setup all pxa physical endpoints, except ep0 632 * Setup all pxa physical endpoints, except ep0
635 */ 633 */
636static __init void pxa_eps_setup(struct pxa_udc *dev) 634static void pxa_eps_setup(struct pxa_udc *dev)
637{ 635{
638 unsigned int i; 636 unsigned int i;
639 637
@@ -1718,7 +1716,7 @@ static void udc_disable(struct pxa_udc *udc)
1718 * Initializes gadget endpoint list, endpoints locks. No action is taken 1716 * Initializes gadget endpoint list, endpoints locks. No action is taken
1719 * on the hardware. 1717 * on the hardware.
1720 */ 1718 */
1721static __init void udc_init_data(struct pxa_udc *dev) 1719static void udc_init_data(struct pxa_udc *dev)
1722{ 1720{
1723 int i; 1721 int i;
1724 struct pxa_ep *ep; 1722 struct pxa_ep *ep;
@@ -1811,7 +1809,6 @@ static int pxa27x_udc_start(struct usb_gadget *g,
1811 1809
1812 /* first hook up the driver ... */ 1810 /* first hook up the driver ... */
1813 udc->driver = driver; 1811 udc->driver = driver;
1814 udc->gadget.dev.driver = &driver->driver;
1815 dplus_pullup(udc, 1); 1812 dplus_pullup(udc, 1);
1816 1813
1817 if (!IS_ERR_OR_NULL(udc->transceiver)) { 1814 if (!IS_ERR_OR_NULL(udc->transceiver)) {
@@ -1829,7 +1826,6 @@ static int pxa27x_udc_start(struct usb_gadget *g,
1829 1826
1830fail: 1827fail:
1831 udc->driver = NULL; 1828 udc->driver = NULL;
1832 udc->gadget.dev.driver = NULL;
1833 return retval; 1829 return retval;
1834} 1830}
1835 1831
@@ -1871,7 +1867,6 @@ static int pxa27x_udc_stop(struct usb_gadget *g,
1871 1867
1872 udc->driver = NULL; 1868 udc->driver = NULL;
1873 1869
1874
1875 if (!IS_ERR_OR_NULL(udc->transceiver)) 1870 if (!IS_ERR_OR_NULL(udc->transceiver))
1876 return otg_set_peripheral(udc->transceiver->otg, NULL); 1871 return otg_set_peripheral(udc->transceiver->otg, NULL);
1877 return 0; 1872 return 0;
@@ -2413,7 +2408,7 @@ static struct pxa_udc memory = {
2413 * Perform basic init : allocates udc clock, creates sysfs files, requests 2408 * Perform basic init : allocates udc clock, creates sysfs files, requests
2414 * irq. 2409 * irq.
2415 */ 2410 */
2416static int __init pxa_udc_probe(struct platform_device *pdev) 2411static int pxa_udc_probe(struct platform_device *pdev)
2417{ 2412{
2418 struct resource *regs; 2413 struct resource *regs;
2419 struct pxa_udc *udc = &memory; 2414 struct pxa_udc *udc = &memory;
@@ -2456,9 +2451,6 @@ static int __init pxa_udc_probe(struct platform_device *pdev)
2456 goto err_map; 2451 goto err_map;
2457 } 2452 }
2458 2453
2459 device_initialize(&udc->gadget.dev);
2460 udc->gadget.dev.parent = &pdev->dev;
2461 udc->gadget.dev.dma_mask = NULL;
2462 udc->vbus_sensed = 0; 2454 udc->vbus_sensed = 0;
2463 2455
2464 the_controller = udc; 2456 the_controller = udc;
@@ -2475,12 +2467,6 @@ static int __init pxa_udc_probe(struct platform_device *pdev)
2475 goto err_irq; 2467 goto err_irq;
2476 } 2468 }
2477 2469
2478 retval = device_add(&udc->gadget.dev);
2479 if (retval) {
2480 dev_err(udc->dev, "device_add error %d\n", retval);
2481 goto err_dev_add;
2482 }
2483
2484 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2470 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2485 if (retval) 2471 if (retval)
2486 goto err_add_udc; 2472 goto err_add_udc;
@@ -2490,8 +2476,6 @@ static int __init pxa_udc_probe(struct platform_device *pdev)
2490 return 0; 2476 return 0;
2491 2477
2492err_add_udc: 2478err_add_udc:
2493 device_unregister(&udc->gadget.dev);
2494err_dev_add:
2495 free_irq(udc->irq, udc); 2479 free_irq(udc->irq, udc);
2496err_irq: 2480err_irq:
2497 iounmap(udc->regs); 2481 iounmap(udc->regs);
@@ -2506,13 +2490,12 @@ err_clk:
2506 * pxa_udc_remove - removes the udc device driver 2490 * pxa_udc_remove - removes the udc device driver
2507 * @_dev: platform device 2491 * @_dev: platform device
2508 */ 2492 */
2509static int __exit pxa_udc_remove(struct platform_device *_dev) 2493static int pxa_udc_remove(struct platform_device *_dev)
2510{ 2494{
2511 struct pxa_udc *udc = platform_get_drvdata(_dev); 2495 struct pxa_udc *udc = platform_get_drvdata(_dev);
2512 int gpio = udc->mach->gpio_pullup; 2496 int gpio = udc->mach->gpio_pullup;
2513 2497
2514 usb_del_gadget_udc(&udc->gadget); 2498 usb_del_gadget_udc(&udc->gadget);
2515 device_del(&udc->gadget.dev);
2516 usb_gadget_unregister_driver(udc->driver); 2499 usb_gadget_unregister_driver(udc->driver);
2517 free_irq(udc->irq, udc); 2500 free_irq(udc->irq, udc);
2518 pxa_cleanup_debugfs(udc); 2501 pxa_cleanup_debugfs(udc);
@@ -2625,7 +2608,8 @@ static struct platform_driver udc_driver = {
2625 .name = "pxa27x-udc", 2608 .name = "pxa27x-udc",
2626 .owner = THIS_MODULE, 2609 .owner = THIS_MODULE,
2627 }, 2610 },
2628 .remove = __exit_p(pxa_udc_remove), 2611 .probe = pxa_udc_probe,
2612 .remove = pxa_udc_remove,
2629 .shutdown = pxa_udc_shutdown, 2613 .shutdown = pxa_udc_shutdown,
2630#ifdef CONFIG_PM 2614#ifdef CONFIG_PM
2631 .suspend = pxa_udc_suspend, 2615 .suspend = pxa_udc_suspend,
@@ -2633,22 +2617,7 @@ static struct platform_driver udc_driver = {
2633#endif 2617#endif
2634}; 2618};
2635 2619
2636static int __init udc_init(void) 2620module_platform_driver(udc_driver);
2637{
2638 if (!cpu_is_pxa27x() && !cpu_is_pxa3xx())
2639 return -ENODEV;
2640
2641 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2642 return platform_driver_probe(&udc_driver, pxa_udc_probe);
2643}
2644module_init(udc_init);
2645
2646
2647static void __exit udc_exit(void)
2648{
2649 platform_driver_unregister(&udc_driver);
2650}
2651module_exit(udc_exit);
2652 2621
2653MODULE_DESCRIPTION(DRIVER_DESC); 2622MODULE_DESCRIPTION(DRIVER_DESC);
2654MODULE_AUTHOR("Robert Jarzmik"); 2623MODULE_AUTHOR("Robert Jarzmik");
diff --git a/drivers/usb/gadget/r8a66597-udc.c b/drivers/usb/gadget/r8a66597-udc.c
index f46a1b77ce3e..0b742d171843 100644
--- a/drivers/usb/gadget/r8a66597-udc.c
+++ b/drivers/usb/gadget/r8a66597-udc.c
@@ -1837,7 +1837,6 @@ static int __exit r8a66597_remove(struct platform_device *pdev)
1837 clk_put(r8a66597->clk); 1837 clk_put(r8a66597->clk);
1838 } 1838 }
1839 1839
1840 device_unregister(&r8a66597->gadget.dev);
1841 kfree(r8a66597); 1840 kfree(r8a66597);
1842 return 0; 1841 return 0;
1843} 1842}
@@ -1915,17 +1914,8 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1915 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW; 1914 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1916 1915
1917 r8a66597->gadget.ops = &r8a66597_gadget_ops; 1916 r8a66597->gadget.ops = &r8a66597_gadget_ops;
1918 dev_set_name(&r8a66597->gadget.dev, "gadget");
1919 r8a66597->gadget.max_speed = USB_SPEED_HIGH; 1917 r8a66597->gadget.max_speed = USB_SPEED_HIGH;
1920 r8a66597->gadget.dev.parent = &pdev->dev;
1921 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1922 r8a66597->gadget.dev.release = pdev->dev.release;
1923 r8a66597->gadget.name = udc_name; 1918 r8a66597->gadget.name = udc_name;
1924 ret = device_register(&r8a66597->gadget.dev);
1925 if (ret < 0) {
1926 dev_err(&pdev->dev, "device_register failed\n");
1927 goto clean_up;
1928 }
1929 1919
1930 init_timer(&r8a66597->timer); 1920 init_timer(&r8a66597->timer);
1931 r8a66597->timer.function = r8a66597_timer; 1921 r8a66597->timer.function = r8a66597_timer;
@@ -1939,7 +1929,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1939 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", 1929 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1940 clk_name); 1930 clk_name);
1941 ret = PTR_ERR(r8a66597->clk); 1931 ret = PTR_ERR(r8a66597->clk);
1942 goto clean_up_dev; 1932 goto clean_up;
1943 } 1933 }
1944 clk_enable(r8a66597->clk); 1934 clk_enable(r8a66597->clk);
1945 } 1935 }
@@ -2007,8 +1997,6 @@ clean_up2:
2007 clk_disable(r8a66597->clk); 1997 clk_disable(r8a66597->clk);
2008 clk_put(r8a66597->clk); 1998 clk_put(r8a66597->clk);
2009 } 1999 }
2010clean_up_dev:
2011 device_unregister(&r8a66597->gadget.dev);
2012clean_up: 2000clean_up:
2013 if (r8a66597) { 2001 if (r8a66597) {
2014 if (r8a66597->sudmac_reg) 2002 if (r8a66597->sudmac_reg)
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index c26564f29a2c..a3cdc32115d5 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -39,8 +39,6 @@
39 39
40#include "s3c-hsotg.h" 40#include "s3c-hsotg.h"
41 41
42#define DMA_ADDR_INVALID (~((dma_addr_t)0))
43
44static const char * const s3c_hsotg_supply_names[] = { 42static const char * const s3c_hsotg_supply_names[] = {
45 "vusb_d", /* digital USB supply, 1.2V */ 43 "vusb_d", /* digital USB supply, 1.2V */
46 "vusb_a", /* analog USB supply, 1.1V */ 44 "vusb_a", /* analog USB supply, 1.1V */
@@ -405,7 +403,6 @@ static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
405 403
406 INIT_LIST_HEAD(&req->queue); 404 INIT_LIST_HEAD(&req->queue);
407 405
408 req->req.dma = DMA_ADDR_INVALID;
409 return &req->req; 406 return &req->req;
410} 407}
411 408
@@ -435,24 +432,12 @@ static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
435 struct s3c_hsotg_req *hs_req) 432 struct s3c_hsotg_req *hs_req)
436{ 433{
437 struct usb_request *req = &hs_req->req; 434 struct usb_request *req = &hs_req->req;
438 enum dma_data_direction dir;
439
440 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
441 435
442 /* ignore this if we're not moving any data */ 436 /* ignore this if we're not moving any data */
443 if (hs_req->req.length == 0) 437 if (hs_req->req.length == 0)
444 return; 438 return;
445 439
446 if (hs_req->mapped) { 440 usb_gadget_unmap_request(&hsotg->gadget, hs_req, hs_ep->dir_in);
447 /* we mapped this, so unmap and remove the dma */
448
449 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
450
451 req->dma = DMA_ADDR_INVALID;
452 hs_req->mapped = 0;
453 } else {
454 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
455 }
456} 441}
457 442
458/** 443/**
@@ -852,37 +837,16 @@ static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
852 struct s3c_hsotg_ep *hs_ep, 837 struct s3c_hsotg_ep *hs_ep,
853 struct usb_request *req) 838 struct usb_request *req)
854{ 839{
855 enum dma_data_direction dir;
856 struct s3c_hsotg_req *hs_req = our_req(req); 840 struct s3c_hsotg_req *hs_req = our_req(req);
857 841 int ret;
858 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
859 842
860 /* if the length is zero, ignore the DMA data */ 843 /* if the length is zero, ignore the DMA data */
861 if (hs_req->req.length == 0) 844 if (hs_req->req.length == 0)
862 return 0; 845 return 0;
863 846
864 if (req->dma == DMA_ADDR_INVALID) { 847 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
865 dma_addr_t dma; 848 if (ret)
866 849 goto dma_error;
867 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
868
869 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
870 goto dma_error;
871
872 if (dma & 3) {
873 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
874 __func__);
875
876 dma_unmap_single(hsotg->dev, dma, req->length, dir);
877 return -EINVAL;
878 }
879
880 hs_req->mapped = 1;
881 req->dma = dma;
882 } else {
883 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
884 hs_req->mapped = 0;
885 }
886 850
887 return 0; 851 return 0;
888 852
@@ -2961,9 +2925,7 @@ static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2961 2925
2962 driver->driver.bus = NULL; 2926 driver->driver.bus = NULL;
2963 hsotg->driver = driver; 2927 hsotg->driver = driver;
2964 hsotg->gadget.dev.driver = &driver->driver;
2965 hsotg->gadget.dev.of_node = hsotg->dev->of_node; 2928 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
2966 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2967 hsotg->gadget.speed = USB_SPEED_UNKNOWN; 2929 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2968 2930
2969 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies), 2931 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
@@ -2979,7 +2941,6 @@ static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2979 2941
2980err: 2942err:
2981 hsotg->driver = NULL; 2943 hsotg->driver = NULL;
2982 hsotg->gadget.dev.driver = NULL;
2983 return ret; 2944 return ret;
2984} 2945}
2985 2946
@@ -3014,7 +2975,6 @@ static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
3014 2975
3015 hsotg->driver = NULL; 2976 hsotg->driver = NULL;
3016 hsotg->gadget.speed = USB_SPEED_UNKNOWN; 2977 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3017 hsotg->gadget.dev.driver = NULL;
3018 2978
3019 spin_unlock_irqrestore(&hsotg->lock, flags); 2979 spin_unlock_irqrestore(&hsotg->lock, flags);
3020 2980
@@ -3484,16 +3444,6 @@ static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3484} 3444}
3485 3445
3486/** 3446/**
3487 * s3c_hsotg_release - release callback for hsotg device
3488 * @dev: Device to for which release is called
3489 *
3490 * Nothing to do as the resource is allocated using devm_ API.
3491 */
3492static void s3c_hsotg_release(struct device *dev)
3493{
3494}
3495
3496/**
3497 * s3c_hsotg_probe - probe function for hsotg driver 3447 * s3c_hsotg_probe - probe function for hsotg driver
3498 * @pdev: The platform information for the driver 3448 * @pdev: The platform information for the driver
3499 */ 3449 */
@@ -3517,7 +3467,7 @@ static int s3c_hsotg_probe(struct platform_device *pdev)
3517 } 3467 }
3518 3468
3519 phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 3469 phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3520 if (IS_ERR_OR_NULL(phy)) { 3470 if (IS_ERR(phy)) {
3521 /* Fallback for pdata */ 3471 /* Fallback for pdata */
3522 plat = pdev->dev.platform_data; 3472 plat = pdev->dev.platform_data;
3523 if (!plat) { 3473 if (!plat) {
@@ -3567,18 +3517,10 @@ static int s3c_hsotg_probe(struct platform_device *pdev)
3567 3517
3568 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq); 3518 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3569 3519
3570 device_initialize(&hsotg->gadget.dev);
3571
3572 dev_set_name(&hsotg->gadget.dev, "gadget");
3573
3574 hsotg->gadget.max_speed = USB_SPEED_HIGH; 3520 hsotg->gadget.max_speed = USB_SPEED_HIGH;
3575 hsotg->gadget.ops = &s3c_hsotg_gadget_ops; 3521 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3576 hsotg->gadget.name = dev_name(dev); 3522 hsotg->gadget.name = dev_name(dev);
3577 3523
3578 hsotg->gadget.dev.parent = dev;
3579 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3580 hsotg->gadget.dev.release = s3c_hsotg_release;
3581
3582 /* reset the system */ 3524 /* reset the system */
3583 3525
3584 clk_prepare_enable(hsotg->clk); 3526 clk_prepare_enable(hsotg->clk);
@@ -3658,12 +3600,6 @@ static int s3c_hsotg_probe(struct platform_device *pdev)
3658 3600
3659 s3c_hsotg_phy_disable(hsotg); 3601 s3c_hsotg_phy_disable(hsotg);
3660 3602
3661 ret = device_add(&hsotg->gadget.dev);
3662 if (ret) {
3663 put_device(&hsotg->gadget.dev);
3664 goto err_ep_mem;
3665 }
3666
3667 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget); 3603 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3668 if (ret) 3604 if (ret)
3669 goto err_ep_mem; 3605 goto err_ep_mem;
@@ -3702,10 +3638,8 @@ static int s3c_hsotg_remove(struct platform_device *pdev)
3702 } 3638 }
3703 3639
3704 s3c_hsotg_phy_disable(hsotg); 3640 s3c_hsotg_phy_disable(hsotg);
3705
3706 clk_disable_unprepare(hsotg->clk); 3641 clk_disable_unprepare(hsotg->clk);
3707 3642
3708 device_unregister(&hsotg->gadget.dev);
3709 return 0; 3643 return 0;
3710} 3644}
3711 3645
diff --git a/drivers/usb/gadget/s3c-hsudc.c b/drivers/usb/gadget/s3c-hsudc.c
index 458965a1b138..b1f0771fbd3d 100644
--- a/drivers/usb/gadget/s3c-hsudc.c
+++ b/drivers/usb/gadget/s3c-hsudc.c
@@ -283,7 +283,6 @@ static void s3c_hsudc_nuke_ep(struct s3c_hsudc_ep *hsep, int status)
283/** 283/**
284 * s3c_hsudc_stop_activity - Stop activity on all endpoints. 284 * s3c_hsudc_stop_activity - Stop activity on all endpoints.
285 * @hsudc: Device controller for which EP activity is to be stopped. 285 * @hsudc: Device controller for which EP activity is to be stopped.
286 * @driver: Reference to the gadget driver which is currently active.
287 * 286 *
288 * All the endpoints are stopped and any pending transfer requests if any on 287 * All the endpoints are stopped and any pending transfer requests if any on
289 * the endpoint are terminated. 288 * the endpoint are terminated.
@@ -1154,7 +1153,6 @@ static int s3c_hsudc_start(struct usb_gadget *gadget,
1154 return -EBUSY; 1153 return -EBUSY;
1155 1154
1156 hsudc->driver = driver; 1155 hsudc->driver = driver;
1157 hsudc->gadget.dev.driver = &driver->driver;
1158 1156
1159 ret = regulator_bulk_enable(ARRAY_SIZE(hsudc->supplies), 1157 ret = regulator_bulk_enable(ARRAY_SIZE(hsudc->supplies),
1160 hsudc->supplies); 1158 hsudc->supplies);
@@ -1190,7 +1188,6 @@ err_otg:
1190 regulator_bulk_disable(ARRAY_SIZE(hsudc->supplies), hsudc->supplies); 1188 regulator_bulk_disable(ARRAY_SIZE(hsudc->supplies), hsudc->supplies);
1191err_supplies: 1189err_supplies:
1192 hsudc->driver = NULL; 1190 hsudc->driver = NULL;
1193 hsudc->gadget.dev.driver = NULL;
1194 return ret; 1191 return ret;
1195} 1192}
1196 1193
@@ -1208,7 +1205,6 @@ static int s3c_hsudc_stop(struct usb_gadget *gadget,
1208 1205
1209 spin_lock_irqsave(&hsudc->lock, flags); 1206 spin_lock_irqsave(&hsudc->lock, flags);
1210 hsudc->driver = NULL; 1207 hsudc->driver = NULL;
1211 hsudc->gadget.dev.driver = NULL;
1212 hsudc->gadget.speed = USB_SPEED_UNKNOWN; 1208 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
1213 s3c_hsudc_uninit_phy(); 1209 s3c_hsudc_uninit_phy();
1214 1210
@@ -1303,15 +1299,10 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1303 1299
1304 spin_lock_init(&hsudc->lock); 1300 spin_lock_init(&hsudc->lock);
1305 1301
1306 dev_set_name(&hsudc->gadget.dev, "gadget");
1307
1308 hsudc->gadget.max_speed = USB_SPEED_HIGH; 1302 hsudc->gadget.max_speed = USB_SPEED_HIGH;
1309 hsudc->gadget.ops = &s3c_hsudc_gadget_ops; 1303 hsudc->gadget.ops = &s3c_hsudc_gadget_ops;
1310 hsudc->gadget.name = dev_name(dev); 1304 hsudc->gadget.name = dev_name(dev);
1311 hsudc->gadget.dev.parent = dev;
1312 hsudc->gadget.dev.dma_mask = dev->dma_mask;
1313 hsudc->gadget.ep0 = &hsudc->ep[0].ep; 1305 hsudc->gadget.ep0 = &hsudc->ep[0].ep;
1314
1315 hsudc->gadget.is_otg = 0; 1306 hsudc->gadget.is_otg = 0;
1316 hsudc->gadget.is_a_peripheral = 0; 1307 hsudc->gadget.is_a_peripheral = 0;
1317 hsudc->gadget.speed = USB_SPEED_UNKNOWN; 1308 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
@@ -1345,12 +1336,6 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1345 disable_irq(hsudc->irq); 1336 disable_irq(hsudc->irq);
1346 local_irq_enable(); 1337 local_irq_enable();
1347 1338
1348 ret = device_register(&hsudc->gadget.dev);
1349 if (ret) {
1350 put_device(&hsudc->gadget.dev);
1351 goto err_add_device;
1352 }
1353
1354 ret = usb_add_gadget_udc(&pdev->dev, &hsudc->gadget); 1339 ret = usb_add_gadget_udc(&pdev->dev, &hsudc->gadget);
1355 if (ret) 1340 if (ret)
1356 goto err_add_udc; 1341 goto err_add_udc;
@@ -1359,7 +1344,6 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
1359 1344
1360 return 0; 1345 return 0;
1361err_add_udc: 1346err_add_udc:
1362 device_unregister(&hsudc->gadget.dev);
1363err_add_device: 1347err_add_device:
1364 clk_disable(hsudc->uclk); 1348 clk_disable(hsudc->uclk);
1365err_res: 1349err_res:
diff --git a/drivers/usb/gadget/s3c2410_udc.c b/drivers/usb/gadget/s3c2410_udc.c
index 08f89652533b..d0e75e1b3ccb 100644
--- a/drivers/usb/gadget/s3c2410_udc.c
+++ b/drivers/usb/gadget/s3c2410_udc.c
@@ -1674,7 +1674,6 @@ static int s3c2410_udc_start(struct usb_gadget *g,
1674 1674
1675 /* Hook the driver */ 1675 /* Hook the driver */
1676 udc->driver = driver; 1676 udc->driver = driver;
1677 udc->gadget.dev.driver = &driver->driver;
1678 1677
1679 /* Enable udc */ 1678 /* Enable udc */
1680 s3c2410_udc_enable(udc); 1679 s3c2410_udc_enable(udc);
@@ -1824,17 +1823,6 @@ static int s3c2410_udc_probe(struct platform_device *pdev)
1824 goto err_mem; 1823 goto err_mem;
1825 } 1824 }
1826 1825
1827 device_initialize(&udc->gadget.dev);
1828 udc->gadget.dev.parent = &pdev->dev;
1829 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
1830
1831 /* Bind the driver */
1832 retval = device_add(&udc->gadget.dev);
1833 if (retval) {
1834 dev_err(&udc->gadget.dev, "Error in device_add() : %d\n", retval);
1835 goto err_device_add;
1836 }
1837
1838 the_controller = udc; 1826 the_controller = udc;
1839 platform_set_drvdata(pdev, udc); 1827 platform_set_drvdata(pdev, udc);
1840 1828
@@ -1923,8 +1911,6 @@ err_gpio_claim:
1923err_int: 1911err_int:
1924 free_irq(IRQ_USBD, udc); 1912 free_irq(IRQ_USBD, udc);
1925err_map: 1913err_map:
1926 device_unregister(&udc->gadget.dev);
1927err_device_add:
1928 iounmap(base_addr); 1914 iounmap(base_addr);
1929err_mem: 1915err_mem:
1930 release_mem_region(rsrc_start, rsrc_len); 1916 release_mem_region(rsrc_start, rsrc_len);
@@ -1946,7 +1932,6 @@ static int s3c2410_udc_remove(struct platform_device *pdev)
1946 return -EBUSY; 1932 return -EBUSY;
1947 1933
1948 usb_del_gadget_udc(&udc->gadget); 1934 usb_del_gadget_udc(&udc->gadget);
1949 device_unregister(&udc->gadget.dev);
1950 debugfs_remove(udc->regs_info); 1935 debugfs_remove(udc->regs_info);
1951 1936
1952 if (udc_info && !udc_info->udc_command && 1937 if (udc_info && !udc_info->udc_command &&
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index 68d7bb06ebcb..1f5f978d35d5 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -12,6 +12,7 @@
12 12
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/module.h>
15#include <linux/tty.h> 16#include <linux/tty.h>
16#include <linux/tty_flip.h> 17#include <linux/tty_flip.h>
17 18
@@ -28,18 +29,6 @@
28#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR 29#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29 30
30/*-------------------------------------------------------------------------*/ 31/*-------------------------------------------------------------------------*/
31
32/*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39#include "f_obex.c"
40#include "f_serial.c"
41
42/*-------------------------------------------------------------------------*/
43USB_GADGET_COMPOSITE_OPTIONS(); 32USB_GADGET_COMPOSITE_OPTIONS();
44 33
45/* Thanks to NetChip Technologies for donating this product ID. 34/* Thanks to NetChip Technologies for donating this product ID.
@@ -126,27 +115,6 @@ module_param(n_ports, uint, 0);
126MODULE_PARM_DESC(n_ports, "number of ports to create, default=1"); 115MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
127 116
128/*-------------------------------------------------------------------------*/ 117/*-------------------------------------------------------------------------*/
129static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
130
131static int __init serial_bind_obex_config(struct usb_configuration *c)
132{
133 unsigned i;
134 int status = 0;
135
136 for (i = 0; i < n_ports && status == 0; i++)
137 status = obex_bind_config(c, tty_lines[i]);
138 return status;
139}
140
141static int __init serial_bind_gser_config(struct usb_configuration *c)
142{
143 unsigned i;
144 int status = 0;
145
146 for (i = 0; i < n_ports && status == 0; i++)
147 status = gser_bind_config(c, tty_lines[i]);
148 return status;
149}
150 118
151static struct usb_configuration serial_config_driver = { 119static struct usb_configuration serial_config_driver = {
152 /* .label = f(use_acm) */ 120 /* .label = f(use_acm) */
@@ -169,15 +137,12 @@ static int serial_register_ports(struct usb_composite_dev *cdev,
169 goto out; 137 goto out;
170 138
171 for (i = 0; i < n_ports; i++) { 139 for (i = 0; i < n_ports; i++) {
172 struct f_serial_opts *opts;
173 140
174 fi_serial[i] = usb_get_function_instance(f_name); 141 fi_serial[i] = usb_get_function_instance(f_name);
175 if (IS_ERR(fi_serial[i])) { 142 if (IS_ERR(fi_serial[i])) {
176 ret = PTR_ERR(fi_serial[i]); 143 ret = PTR_ERR(fi_serial[i]);
177 goto fail; 144 goto fail;
178 } 145 }
179 opts = container_of(fi_serial[i], struct f_serial_opts, func_inst);
180 opts->port_num = tty_lines[i];
181 146
182 f_serial[i] = usb_get_function(fi_serial[i]); 147 f_serial[i] = usb_get_function(fi_serial[i]);
183 if (IS_ERR(f_serial[i])) { 148 if (IS_ERR(f_serial[i])) {
@@ -212,13 +177,6 @@ out:
212static int __init gs_bind(struct usb_composite_dev *cdev) 177static int __init gs_bind(struct usb_composite_dev *cdev)
213{ 178{
214 int status; 179 int status;
215 int cur_line;
216
217 for (cur_line = 0; cur_line < n_ports; cur_line++) {
218 status = gserial_alloc_line(&tty_lines[cur_line]);
219 if (status)
220 goto fail;
221 }
222 180
223 /* Allocate string descriptor numbers ... note that string 181 /* Allocate string descriptor numbers ... note that string
224 * contents can be overridden by the composite_dev glue. 182 * contents can be overridden by the composite_dev glue.
@@ -243,11 +201,12 @@ static int __init gs_bind(struct usb_composite_dev *cdev)
243 "acm"); 201 "acm");
244 usb_ep_autoconfig_reset(cdev->gadget); 202 usb_ep_autoconfig_reset(cdev->gadget);
245 } else if (use_obex) 203 } else if (use_obex)
246 status = usb_add_config(cdev, &serial_config_driver, 204 status = serial_register_ports(cdev, &serial_config_driver,
247 serial_bind_obex_config); 205 "obex");
248 else 206 else {
249 status = usb_add_config(cdev, &serial_config_driver, 207 status = serial_register_ports(cdev, &serial_config_driver,
250 serial_bind_gser_config); 208 "gser");
209 }
251 if (status < 0) 210 if (status < 0)
252 goto fail; 211 goto fail;
253 212
@@ -257,9 +216,6 @@ static int __init gs_bind(struct usb_composite_dev *cdev)
257 return 0; 216 return 0;
258 217
259fail: 218fail:
260 cur_line--;
261 while (cur_line >= 0)
262 gserial_free_line(tty_lines[cur_line--]);
263 return status; 219 return status;
264} 220}
265 221
@@ -270,7 +226,6 @@ static int gs_unbind(struct usb_composite_dev *cdev)
270 for (i = 0; i < n_ports; i++) { 226 for (i = 0; i < n_ports; i++) {
271 usb_put_function(f_serial[i]); 227 usb_put_function(f_serial[i]);
272 usb_put_function_instance(fi_serial[i]); 228 usb_put_function_instance(fi_serial[i]);
273 gserial_free_line(tty_lines[i]);
274 } 229 }
275 return 0; 230 return 0;
276} 231}
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index a0aa721d8b21..4b76124ce96b 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -50,7 +50,6 @@
50 50
51struct eth_dev { 51struct eth_dev {
52 /* lock is held while accessing port_usb 52 /* lock is held while accessing port_usb
53 * or updating its backlink port_usb->ioport
54 */ 53 */
55 spinlock_t lock; 54 spinlock_t lock;
56 struct gether *port_usb; 55 struct gether *port_usb;
@@ -729,8 +728,6 @@ static int get_ether_addr(const char *str, u8 *dev_addr)
729 return 1; 728 return 1;
730} 729}
731 730
732static struct eth_dev *the_dev;
733
734static const struct net_device_ops eth_netdev_ops = { 731static const struct net_device_ops eth_netdev_ops = {
735 .ndo_open = eth_open, 732 .ndo_open = eth_open,
736 .ndo_stop = eth_stop, 733 .ndo_stop = eth_stop,
@@ -758,19 +755,16 @@ static struct device_type gadget_type = {
758 * 755 *
759 * Returns negative errno, or zero on success 756 * Returns negative errno, or zero on success
760 */ 757 */
761int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN], 758struct eth_dev *gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
762 const char *netname) 759 const char *netname)
763{ 760{
764 struct eth_dev *dev; 761 struct eth_dev *dev;
765 struct net_device *net; 762 struct net_device *net;
766 int status; 763 int status;
767 764
768 if (the_dev)
769 return -EBUSY;
770
771 net = alloc_etherdev(sizeof *dev); 765 net = alloc_etherdev(sizeof *dev);
772 if (!net) 766 if (!net)
773 return -ENOMEM; 767 return ERR_PTR(-ENOMEM);
774 768
775 dev = netdev_priv(net); 769 dev = netdev_priv(net);
776 spin_lock_init(&dev->lock); 770 spin_lock_init(&dev->lock);
@@ -807,12 +801,11 @@ int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
807 if (status < 0) { 801 if (status < 0) {
808 dev_dbg(&g->dev, "register_netdev failed, %d\n", status); 802 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
809 free_netdev(net); 803 free_netdev(net);
804 dev = ERR_PTR(status);
810 } else { 805 } else {
811 INFO(dev, "MAC %pM\n", net->dev_addr); 806 INFO(dev, "MAC %pM\n", net->dev_addr);
812 INFO(dev, "HOST MAC %pM\n", dev->host_mac); 807 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
813 808
814 the_dev = dev;
815
816 /* two kinds of host-initiated state changes: 809 /* two kinds of host-initiated state changes:
817 * - iff DATA transfer is active, carrier is "on" 810 * - iff DATA transfer is active, carrier is "on"
818 * - tx queueing enabled if open *and* carrier is "on" 811 * - tx queueing enabled if open *and* carrier is "on"
@@ -820,7 +813,7 @@ int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
820 netif_carrier_off(net); 813 netif_carrier_off(net);
821 } 814 }
822 815
823 return status; 816 return dev;
824} 817}
825 818
826/** 819/**
@@ -829,19 +822,16 @@ int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
829 * 822 *
830 * This is called to free all resources allocated by @gether_setup(). 823 * This is called to free all resources allocated by @gether_setup().
831 */ 824 */
832void gether_cleanup(void) 825void gether_cleanup(struct eth_dev *dev)
833{ 826{
834 if (!the_dev) 827 if (!dev)
835 return; 828 return;
836 829
837 unregister_netdev(the_dev->net); 830 unregister_netdev(dev->net);
838 flush_work(&the_dev->work); 831 flush_work(&dev->work);
839 free_netdev(the_dev->net); 832 free_netdev(dev->net);
840
841 the_dev = NULL;
842} 833}
843 834
844
845/** 835/**
846 * gether_connect - notify network layer that USB link is active 836 * gether_connect - notify network layer that USB link is active
847 * @link: the USB link, set up with endpoints, descriptors matching 837 * @link: the USB link, set up with endpoints, descriptors matching
@@ -860,7 +850,7 @@ void gether_cleanup(void)
860 */ 850 */
861struct net_device *gether_connect(struct gether *link) 851struct net_device *gether_connect(struct gether *link)
862{ 852{
863 struct eth_dev *dev = the_dev; 853 struct eth_dev *dev = link->ioport;
864 int result = 0; 854 int result = 0;
865 855
866 if (!dev) 856 if (!dev)
@@ -895,7 +885,6 @@ struct net_device *gether_connect(struct gether *link)
895 885
896 spin_lock(&dev->lock); 886 spin_lock(&dev->lock);
897 dev->port_usb = link; 887 dev->port_usb = link;
898 link->ioport = dev;
899 if (netif_running(dev->net)) { 888 if (netif_running(dev->net)) {
900 if (link->open) 889 if (link->open)
901 link->open(link); 890 link->open(link);
@@ -989,6 +978,5 @@ void gether_disconnect(struct gether *link)
989 978
990 spin_lock(&dev->lock); 979 spin_lock(&dev->lock);
991 dev->port_usb = NULL; 980 dev->port_usb = NULL;
992 link->ioport = NULL;
993 spin_unlock(&dev->lock); 981 spin_unlock(&dev->lock);
994} 982}
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 6f4a1623d854..02522338a708 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -21,6 +21,7 @@
21 21
22#include "gadget_chips.h" 22#include "gadget_chips.h"
23 23
24struct eth_dev;
24 25
25/* 26/*
26 * This represents the USB side of an "ethernet" link, managed by a USB 27 * This represents the USB side of an "ethernet" link, managed by a USB
@@ -70,7 +71,7 @@ struct gether {
70 |USB_CDC_PACKET_TYPE_DIRECTED) 71 |USB_CDC_PACKET_TYPE_DIRECTED)
71 72
72/* variant of gether_setup that allows customizing network device name */ 73/* variant of gether_setup that allows customizing network device name */
73int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN], 74struct eth_dev *gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
74 const char *netname); 75 const char *netname);
75 76
76/* netdev setup/teardown as directed by the gadget driver */ 77/* netdev setup/teardown as directed by the gadget driver */
@@ -86,12 +87,13 @@ int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
86 * 87 *
87 * Returns negative errno, or zero on success 88 * Returns negative errno, or zero on success
88 */ 89 */
89static inline int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN]) 90static inline struct eth_dev *gether_setup(struct usb_gadget *g,
91 u8 ethaddr[ETH_ALEN])
90{ 92{
91 return gether_setup_name(g, ethaddr, "usb"); 93 return gether_setup_name(g, ethaddr, "usb");
92} 94}
93 95
94void gether_cleanup(void); 96void gether_cleanup(struct eth_dev *dev);
95 97
96/* connect/disconnect is handled by individual functions */ 98/* connect/disconnect is handled by individual functions */
97struct net_device *gether_connect(struct gether *); 99struct net_device *gether_connect(struct gether *);
@@ -111,21 +113,24 @@ static inline bool can_support_ecm(struct usb_gadget *gadget)
111} 113}
112 114
113/* each configuration may bind one instance of an ethernet link */ 115/* each configuration may bind one instance of an ethernet link */
114int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 116int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
115int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 117 struct eth_dev *dev);
116int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 118int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
117int eem_bind_config(struct usb_configuration *c); 119 struct eth_dev *dev);
120int ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
121 struct eth_dev *dev);
122int eem_bind_config(struct usb_configuration *c, struct eth_dev *dev);
118 123
119#ifdef USB_ETH_RNDIS 124#ifdef USB_ETH_RNDIS
120 125
121int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], 126int rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
122 u32 vendorID, const char *manufacturer); 127 u32 vendorID, const char *manufacturer, struct eth_dev *dev);
123 128
124#else 129#else
125 130
126static inline int 131static inline int
127rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], 132rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
128 u32 vendorID, const char *manufacturer) 133 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
129{ 134{
130 return 0; 135 return 0;
131} 136}
@@ -145,9 +150,9 @@ rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
145 * for calling @gether_cleanup() before module unload. 150 * for calling @gether_cleanup() before module unload.
146 */ 151 */
147static inline int rndis_bind_config(struct usb_configuration *c, 152static inline int rndis_bind_config(struct usb_configuration *c,
148 u8 ethaddr[ETH_ALEN]) 153 u8 ethaddr[ETH_ALEN], struct eth_dev *dev)
149{ 154{
150 return rndis_bind_config_vendor(c, ethaddr, 0, NULL); 155 return rndis_bind_config_vendor(c, ethaddr, 0, NULL, dev);
151} 156}
152 157
153 158
diff --git a/drivers/usb/gadget/u_serial.h b/drivers/usb/gadget/u_serial.h
index 66ce73a00509..c20210c0babd 100644
--- a/drivers/usb/gadget/u_serial.h
+++ b/drivers/usb/gadget/u_serial.h
@@ -65,7 +65,6 @@ int gserial_connect(struct gserial *, u8 port_num);
65void gserial_disconnect(struct gserial *); 65void gserial_disconnect(struct gserial *);
66 66
67/* functions are bound to configurations by a config or gadget driver */ 67/* functions are bound to configurations by a config or gadget driver */
68int acm_bind_config(struct usb_configuration *c, u8 port_num);
69int gser_bind_config(struct usb_configuration *c, u8 port_num); 68int gser_bind_config(struct usb_configuration *c, u8 port_num);
70int obex_bind_config(struct usb_configuration *c, u8 port_num); 69int obex_bind_config(struct usb_configuration *c, u8 port_num);
71 70
diff --git a/drivers/usb/gadget/udc-core.c b/drivers/usb/gadget/udc-core.c
index f8f62c3ed65e..ffd8fa541101 100644
--- a/drivers/usb/gadget/udc-core.c
+++ b/drivers/usb/gadget/udc-core.c
@@ -101,6 +101,16 @@ EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
101 101
102/* ------------------------------------------------------------------------- */ 102/* ------------------------------------------------------------------------- */
103 103
104void usb_gadget_set_state(struct usb_gadget *gadget,
105 enum usb_device_state state)
106{
107 gadget->state = state;
108 sysfs_notify(&gadget->dev.kobj, NULL, "status");
109}
110EXPORT_SYMBOL_GPL(usb_gadget_set_state);
111
112/* ------------------------------------------------------------------------- */
113
104/** 114/**
105 * usb_gadget_udc_start - tells usb device controller to start up 115 * usb_gadget_udc_start - tells usb device controller to start up
106 * @gadget: The gadget we want to get started 116 * @gadget: The gadget we want to get started
@@ -156,15 +166,23 @@ static void usb_udc_release(struct device *dev)
156} 166}
157 167
158static const struct attribute_group *usb_udc_attr_groups[]; 168static const struct attribute_group *usb_udc_attr_groups[];
169
170static void usb_udc_nop_release(struct device *dev)
171{
172 dev_vdbg(dev, "%s\n", __func__);
173}
174
159/** 175/**
160 * usb_add_gadget_udc - adds a new gadget to the udc class driver list 176 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
161 * @parent: the parent device to this udc. Usually the controller 177 * @parent: the parent device to this udc. Usually the controller driver's
162 * driver's device. 178 * device.
163 * @gadget: the gadget to be added to the list 179 * @gadget: the gadget to be added to the list.
180 * @release: a gadget release function.
164 * 181 *
165 * Returns zero on success, negative errno otherwise. 182 * Returns zero on success, negative errno otherwise.
166 */ 183 */
167int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget) 184int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
185 void (*release)(struct device *dev))
168{ 186{
169 struct usb_udc *udc; 187 struct usb_udc *udc;
170 int ret = -ENOMEM; 188 int ret = -ENOMEM;
@@ -173,6 +191,22 @@ int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
173 if (!udc) 191 if (!udc)
174 goto err1; 192 goto err1;
175 193
194 dev_set_name(&gadget->dev, "gadget");
195 gadget->dev.parent = parent;
196
197 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
198 gadget->dev.dma_parms = parent->dma_parms;
199 gadget->dev.dma_mask = parent->dma_mask;
200
201 if (release)
202 gadget->dev.release = release;
203 else
204 gadget->dev.release = usb_udc_nop_release;
205
206 ret = device_register(&gadget->dev);
207 if (ret)
208 goto err2;
209
176 device_initialize(&udc->dev); 210 device_initialize(&udc->dev);
177 udc->dev.release = usb_udc_release; 211 udc->dev.release = usb_udc_release;
178 udc->dev.class = udc_class; 212 udc->dev.class = udc_class;
@@ -180,7 +214,7 @@ int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
180 udc->dev.parent = parent; 214 udc->dev.parent = parent;
181 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj)); 215 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
182 if (ret) 216 if (ret)
183 goto err2; 217 goto err3;
184 218
185 udc->gadget = gadget; 219 udc->gadget = gadget;
186 220
@@ -189,21 +223,42 @@ int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
189 223
190 ret = device_add(&udc->dev); 224 ret = device_add(&udc->dev);
191 if (ret) 225 if (ret)
192 goto err3; 226 goto err4;
227
228 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
193 229
194 mutex_unlock(&udc_lock); 230 mutex_unlock(&udc_lock);
195 231
196 return 0; 232 return 0;
197err3: 233
234err4:
198 list_del(&udc->list); 235 list_del(&udc->list);
199 mutex_unlock(&udc_lock); 236 mutex_unlock(&udc_lock);
200 237
201err2: 238err3:
202 put_device(&udc->dev); 239 put_device(&udc->dev);
203 240
241err2:
242 put_device(&gadget->dev);
243 kfree(udc);
244
204err1: 245err1:
205 return ret; 246 return ret;
206} 247}
248EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
249
250/**
251 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
252 * @parent: the parent device to this udc. Usually the controller
253 * driver's device.
254 * @gadget: the gadget to be added to the list
255 *
256 * Returns zero on success, negative errno otherwise.
257 */
258int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
259{
260 return usb_add_gadget_udc_release(parent, gadget, NULL);
261}
207EXPORT_SYMBOL_GPL(usb_add_gadget_udc); 262EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
208 263
209static void usb_gadget_remove_driver(struct usb_udc *udc) 264static void usb_gadget_remove_driver(struct usb_udc *udc)
@@ -220,6 +275,7 @@ static void usb_gadget_remove_driver(struct usb_udc *udc)
220 275
221 udc->driver = NULL; 276 udc->driver = NULL;
222 udc->dev.driver = NULL; 277 udc->dev.driver = NULL;
278 udc->gadget->dev.driver = NULL;
223} 279}
224 280
225/** 281/**
@@ -254,6 +310,7 @@ found:
254 310
255 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); 311 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
256 device_unregister(&udc->dev); 312 device_unregister(&udc->dev);
313 device_unregister(&gadget->dev);
257} 314}
258EXPORT_SYMBOL_GPL(usb_del_gadget_udc); 315EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
259 316
@@ -268,6 +325,7 @@ static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *dri
268 325
269 udc->driver = driver; 326 udc->driver = driver;
270 udc->dev.driver = &driver->driver; 327 udc->dev.driver = &driver->driver;
328 udc->gadget->dev.driver = &driver->driver;
271 329
272 ret = driver->bind(udc->gadget, driver); 330 ret = driver->bind(udc->gadget, driver);
273 if (ret) 331 if (ret)
@@ -286,6 +344,7 @@ err1:
286 udc->driver->function, ret); 344 udc->driver->function, ret);
287 udc->driver = NULL; 345 udc->driver = NULL;
288 udc->dev.driver = NULL; 346 udc->dev.driver = NULL;
347 udc->gadget->dev.driver = NULL;
289 return ret; 348 return ret;
290} 349}
291 350
@@ -395,6 +454,16 @@ static ssize_t usb_udc_softconn_store(struct device *dev,
395} 454}
396static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store); 455static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
397 456
457static ssize_t usb_gadget_state_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
460 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
461 struct usb_gadget *gadget = udc->gadget;
462
463 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
464}
465static DEVICE_ATTR(state, S_IRUGO, usb_gadget_state_show, NULL);
466
398#define USB_UDC_SPEED_ATTR(name, param) \ 467#define USB_UDC_SPEED_ATTR(name, param) \
399ssize_t usb_udc_##param##_show(struct device *dev, \ 468ssize_t usb_udc_##param##_show(struct device *dev, \
400 struct device_attribute *attr, char *buf) \ 469 struct device_attribute *attr, char *buf) \
@@ -403,7 +472,7 @@ ssize_t usb_udc_##param##_show(struct device *dev, \
403 return snprintf(buf, PAGE_SIZE, "%s\n", \ 472 return snprintf(buf, PAGE_SIZE, "%s\n", \
404 usb_speed_string(udc->gadget->param)); \ 473 usb_speed_string(udc->gadget->param)); \
405} \ 474} \
406static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL) 475static DEVICE_ATTR(name, S_IRUGO, usb_udc_##param##_show, NULL)
407 476
408static USB_UDC_SPEED_ATTR(current_speed, speed); 477static USB_UDC_SPEED_ATTR(current_speed, speed);
409static USB_UDC_SPEED_ATTR(maximum_speed, max_speed); 478static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
@@ -428,6 +497,7 @@ static USB_UDC_ATTR(a_alt_hnp_support);
428static struct attribute *usb_udc_attrs[] = { 497static struct attribute *usb_udc_attrs[] = {
429 &dev_attr_srp.attr, 498 &dev_attr_srp.attr,
430 &dev_attr_soft_connect.attr, 499 &dev_attr_soft_connect.attr,
500 &dev_attr_state.attr,
431 &dev_attr_current_speed.attr, 501 &dev_attr_current_speed.attr,
432 &dev_attr_maximum_speed.attr, 502 &dev_attr_maximum_speed.attr,
433 503
diff --git a/drivers/usb/gadget/uvc.h b/drivers/usb/gadget/uvc.h
index 93b0c1191115..817e9e19cecf 100644
--- a/drivers/usb/gadget/uvc.h
+++ b/drivers/usb/gadget/uvc.h
@@ -98,8 +98,6 @@ extern unsigned int uvc_gadget_trace_param;
98#define DRIVER_VERSION "0.1.0" 98#define DRIVER_VERSION "0.1.0"
99#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 0) 99#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 0)
100 100
101#define DMA_ADDR_INVALID (~(dma_addr_t)0)
102
103#define UVC_NUM_REQUESTS 4 101#define UVC_NUM_REQUESTS 4
104#define UVC_MAX_REQUEST_SIZE 64 102#define UVC_MAX_REQUEST_SIZE 64
105#define UVC_MAX_EVENTS 4 103#define UVC_MAX_EVENTS 4
@@ -190,6 +188,7 @@ struct uvc_file_handle
190 * Functions 188 * Functions
191 */ 189 */
192 190
191extern void uvc_function_setup_continue(struct uvc_device *uvc);
193extern void uvc_endpoint_stream(struct uvc_device *dev); 192extern void uvc_endpoint_stream(struct uvc_device *dev);
194 193
195extern void uvc_function_connect(struct uvc_device *uvc); 194extern void uvc_function_connect(struct uvc_device *uvc);
diff --git a/drivers/usb/gadget/uvc_queue.c b/drivers/usb/gadget/uvc_queue.c
index 104ae9c81251..7ce27e35550b 100644
--- a/drivers/usb/gadget/uvc_queue.c
+++ b/drivers/usb/gadget/uvc_queue.c
@@ -10,6 +10,7 @@
10 * (at your option) any later version. 10 * (at your option) any later version.
11 */ 11 */
12 12
13#include <linux/atomic.h>
13#include <linux/kernel.h> 14#include <linux/kernel.h>
14#include <linux/mm.h> 15#include <linux/mm.h>
15#include <linux/list.h> 16#include <linux/list.h>
@@ -18,7 +19,8 @@
18#include <linux/videodev2.h> 19#include <linux/videodev2.h>
19#include <linux/vmalloc.h> 20#include <linux/vmalloc.h>
20#include <linux/wait.h> 21#include <linux/wait.h>
21#include <linux/atomic.h> 22
23#include <media/videobuf2-vmalloc.h>
22 24
23#include "uvc.h" 25#include "uvc.h"
24 26
@@ -28,330 +30,175 @@
28 * Video queues is initialized by uvc_queue_init(). The function performs 30 * Video queues is initialized by uvc_queue_init(). The function performs
29 * basic initialization of the uvc_video_queue struct and never fails. 31 * basic initialization of the uvc_video_queue struct and never fails.
30 * 32 *
31 * Video buffer allocation and freeing are performed by uvc_alloc_buffers and 33 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
32 * uvc_free_buffers respectively. The former acquires the video queue lock, 34 * the videobuf2 queue operations by serializing calls to videobuf2 and a
33 * while the later must be called with the lock held (so that allocation can 35 * spinlock to protect the IRQ queue that holds the buffers to be processed by
34 * free previously allocated buffers). Trying to free buffers that are mapped 36 * the driver.
35 * to user space will return -EBUSY.
36 *
37 * Video buffers are managed using two queues. However, unlike most USB video
38 * drivers that use an in queue and an out queue, we use a main queue to hold
39 * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
40 * hold empty buffers. This design (copied from video-buf) minimizes locking
41 * in interrupt, as only one queue is shared between interrupt and user
42 * contexts.
43 *
44 * Use cases
45 * ---------
46 *
47 * Unless stated otherwise, all operations that modify the irq buffers queue
48 * are protected by the irq spinlock.
49 *
50 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
51 *
52 * The buffers are added to the main and irq queues. Both operations are
53 * protected by the queue lock, and the later is protected by the irq
54 * spinlock as well.
55 *
56 * The completion handler fetches a buffer from the irq queue and fills it
57 * with video data. If no buffer is available (irq queue empty), the handler
58 * returns immediately.
59 *
60 * When the buffer is full, the completion handler removes it from the irq
61 * queue, marks it as ready (UVC_BUF_STATE_DONE) and wakes its wait queue.
62 * At that point, any process waiting on the buffer will be woken up. If a
63 * process tries to dequeue a buffer after it has been marked ready, the
64 * dequeing will succeed immediately.
65 *
66 * 2. Buffers are queued, user is waiting on a buffer and the device gets
67 * disconnected.
68 *
69 * When the device is disconnected, the kernel calls the completion handler
70 * with an appropriate status code. The handler marks all buffers in the
71 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
72 * that any process waiting on a buffer gets woken up.
73 *
74 * Waking up up the first buffer on the irq list is not enough, as the
75 * process waiting on the buffer might restart the dequeue operation
76 * immediately.
77 *
78 */ 37 */
79 38
80static void 39/* -----------------------------------------------------------------------------
81uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type) 40 * videobuf2 queue operations
82{
83 mutex_init(&queue->mutex);
84 spin_lock_init(&queue->irqlock);
85 INIT_LIST_HEAD(&queue->mainqueue);
86 INIT_LIST_HEAD(&queue->irqqueue);
87 queue->type = type;
88}
89
90/*
91 * Free the video buffers.
92 *
93 * This function must be called with the queue lock held.
94 */ 41 */
95static int uvc_free_buffers(struct uvc_video_queue *queue) 42
43static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
44 unsigned int *nbuffers, unsigned int *nplanes,
45 unsigned int sizes[], void *alloc_ctxs[])
96{ 46{
97 unsigned int i; 47 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
98 49
99 for (i = 0; i < queue->count; ++i) { 50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
100 if (queue->buffer[i].vma_use_count != 0) 51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
101 return -EBUSY;
102 }
103 52
104 if (queue->count) { 53 *nplanes = 1;
105 vfree(queue->mem); 54
106 queue->count = 0; 55 sizes[0] = video->imagesize;
107 }
108 56
109 return 0; 57 return 0;
110} 58}
111 59
112/* 60static int uvc_buffer_prepare(struct vb2_buffer *vb)
113 * Allocate the video buffers.
114 *
115 * Pages are reserved to make sure they will not be swapped, as they will be
116 * filled in the URB completion handler.
117 *
118 * Buffers will be individually mapped, so they must all be page aligned.
119 */
120static int
121uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
122 unsigned int buflength)
123{ 61{
124 unsigned int bufsize = PAGE_ALIGN(buflength); 62 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
125 unsigned int i; 63 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
126 void *mem = NULL;
127 int ret;
128 64
129 if (nbuffers > UVC_MAX_VIDEO_BUFFERS) 65 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
130 nbuffers = UVC_MAX_VIDEO_BUFFERS; 66 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
67 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
68 return -EINVAL;
69 }
131 70
132 mutex_lock(&queue->mutex); 71 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
72 return -ENODEV;
133 73
134 if ((ret = uvc_free_buffers(queue)) < 0) 74 buf->state = UVC_BUF_STATE_QUEUED;
135 goto done; 75 buf->mem = vb2_plane_vaddr(vb, 0);
76 buf->length = vb2_plane_size(vb, 0);
77 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
78 buf->bytesused = 0;
79 else
80 buf->bytesused = vb2_get_plane_payload(vb, 0);
136 81
137 /* Bail out if no buffers should be allocated. */ 82 return 0;
138 if (nbuffers == 0) 83}
139 goto done;
140 84
141 /* Decrement the number of buffers until allocation succeeds. */ 85static void uvc_buffer_queue(struct vb2_buffer *vb)
142 for (; nbuffers > 0; --nbuffers) { 86{
143 mem = vmalloc_32(nbuffers * bufsize); 87 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
144 if (mem != NULL) 88 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
145 break; 89 unsigned long flags;
146 }
147 90
148 if (mem == NULL) { 91 spin_lock_irqsave(&queue->irqlock, flags);
149 ret = -ENOMEM;
150 goto done;
151 }
152 92
153 for (i = 0; i < nbuffers; ++i) { 93 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
154 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]); 94 list_add_tail(&buf->queue, &queue->irqqueue);
155 queue->buffer[i].buf.index = i; 95 } else {
156 queue->buffer[i].buf.m.offset = i * bufsize; 96 /* If the device is disconnected return the buffer to userspace
157 queue->buffer[i].buf.length = buflength; 97 * directly. The next QBUF call will fail with -ENODEV.
158 queue->buffer[i].buf.type = queue->type; 98 */
159 queue->buffer[i].buf.sequence = 0; 99 buf->state = UVC_BUF_STATE_ERROR;
160 queue->buffer[i].buf.field = V4L2_FIELD_NONE; 100 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
161 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
162 queue->buffer[i].buf.flags = 0;
163 init_waitqueue_head(&queue->buffer[i].wait);
164 } 101 }
165 102
166 queue->mem = mem; 103 spin_unlock_irqrestore(&queue->irqlock, flags);
167 queue->count = nbuffers;
168 queue->buf_size = bufsize;
169 ret = nbuffers;
170
171done:
172 mutex_unlock(&queue->mutex);
173 return ret;
174} 104}
175 105
176static void __uvc_query_buffer(struct uvc_buffer *buf, 106static struct vb2_ops uvc_queue_qops = {
177 struct v4l2_buffer *v4l2_buf) 107 .queue_setup = uvc_queue_setup,
178{ 108 .buf_prepare = uvc_buffer_prepare,
179 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf); 109 .buf_queue = uvc_buffer_queue,
180 110};
181 if (buf->vma_use_count)
182 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
183
184 switch (buf->state) {
185 case UVC_BUF_STATE_ERROR:
186 case UVC_BUF_STATE_DONE:
187 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
188 break;
189 case UVC_BUF_STATE_QUEUED:
190 case UVC_BUF_STATE_ACTIVE:
191 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
192 break;
193 case UVC_BUF_STATE_IDLE:
194 default:
195 break;
196 }
197}
198 111
199static int 112static int uvc_queue_init(struct uvc_video_queue *queue,
200uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf) 113 enum v4l2_buf_type type)
201{ 114{
202 int ret = 0; 115 int ret;
203 116
204 mutex_lock(&queue->mutex); 117 queue->queue.type = type;
205 if (v4l2_buf->index >= queue->count) { 118 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
206 ret = -EINVAL; 119 queue->queue.drv_priv = queue;
207 goto done; 120 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
208 } 121 queue->queue.ops = &uvc_queue_qops;
122 queue->queue.mem_ops = &vb2_vmalloc_memops;
123 ret = vb2_queue_init(&queue->queue);
124 if (ret)
125 return ret;
126
127 mutex_init(&queue->mutex);
128 spin_lock_init(&queue->irqlock);
129 INIT_LIST_HEAD(&queue->irqqueue);
130 queue->flags = 0;
209 131
210 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf); 132 return 0;
133}
211 134
212done: 135/*
136 * Free the video buffers.
137 */
138static void uvc_free_buffers(struct uvc_video_queue *queue)
139{
140 mutex_lock(&queue->mutex);
141 vb2_queue_release(&queue->queue);
213 mutex_unlock(&queue->mutex); 142 mutex_unlock(&queue->mutex);
214 return ret;
215} 143}
216 144
217/* 145/*
218 * Queue a video buffer. Attempting to queue a buffer that has already been 146 * Allocate the video buffers.
219 * queued will return -EINVAL.
220 */ 147 */
221static int 148static int uvc_alloc_buffers(struct uvc_video_queue *queue,
222uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf) 149 struct v4l2_requestbuffers *rb)
223{ 150{
224 struct uvc_buffer *buf; 151 int ret;
225 unsigned long flags;
226 int ret = 0;
227 152
228 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index); 153 mutex_lock(&queue->mutex);
154 ret = vb2_reqbufs(&queue->queue, rb);
155 mutex_unlock(&queue->mutex);
229 156
230 if (v4l2_buf->type != queue->type || 157 return ret ? ret : rb->count;
231 v4l2_buf->memory != V4L2_MEMORY_MMAP) { 158}
232 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
233 "and/or memory (%u).\n", v4l2_buf->type,
234 v4l2_buf->memory);
235 return -EINVAL;
236 }
237 159
238 mutex_lock(&queue->mutex); 160static int uvc_query_buffer(struct uvc_video_queue *queue,
239 if (v4l2_buf->index >= queue->count) { 161 struct v4l2_buffer *buf)
240 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n"); 162{
241 ret = -EINVAL; 163 int ret;
242 goto done;
243 }
244 164
245 buf = &queue->buffer[v4l2_buf->index]; 165 mutex_lock(&queue->mutex);
246 if (buf->state != UVC_BUF_STATE_IDLE) { 166 ret = vb2_querybuf(&queue->queue, buf);
247 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state " 167 mutex_unlock(&queue->mutex);
248 "(%u).\n", buf->state);
249 ret = -EINVAL;
250 goto done;
251 }
252 168
253 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 169 return ret;
254 v4l2_buf->bytesused > buf->buf.length) { 170}
255 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
256 ret = -EINVAL;
257 goto done;
258 }
259 171
260 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 172static int uvc_queue_buffer(struct uvc_video_queue *queue,
261 buf->buf.bytesused = 0; 173 struct v4l2_buffer *buf)
262 else 174{
263 buf->buf.bytesused = v4l2_buf->bytesused; 175 unsigned long flags;
176 int ret;
264 177
178 mutex_lock(&queue->mutex);
179 ret = vb2_qbuf(&queue->queue, buf);
265 spin_lock_irqsave(&queue->irqlock, flags); 180 spin_lock_irqsave(&queue->irqlock, flags);
266 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
267 spin_unlock_irqrestore(&queue->irqlock, flags);
268 ret = -ENODEV;
269 goto done;
270 }
271 buf->state = UVC_BUF_STATE_QUEUED;
272
273 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0; 181 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
274 queue->flags &= ~UVC_QUEUE_PAUSED; 182 queue->flags &= ~UVC_QUEUE_PAUSED;
275
276 list_add_tail(&buf->stream, &queue->mainqueue);
277 list_add_tail(&buf->queue, &queue->irqqueue);
278 spin_unlock_irqrestore(&queue->irqlock, flags); 183 spin_unlock_irqrestore(&queue->irqlock, flags);
279
280done:
281 mutex_unlock(&queue->mutex); 184 mutex_unlock(&queue->mutex);
282 return ret;
283}
284 185
285static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking) 186 return ret;
286{
287 if (nonblocking) {
288 return (buf->state != UVC_BUF_STATE_QUEUED &&
289 buf->state != UVC_BUF_STATE_ACTIVE)
290 ? 0 : -EAGAIN;
291 }
292
293 return wait_event_interruptible(buf->wait,
294 buf->state != UVC_BUF_STATE_QUEUED &&
295 buf->state != UVC_BUF_STATE_ACTIVE);
296} 187}
297 188
298/* 189/*
299 * Dequeue a video buffer. If nonblocking is false, block until a buffer is 190 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
300 * available. 191 * available.
301 */ 192 */
302static int 193static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
303uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf, 194 struct v4l2_buffer *buf, int nonblocking)
304 int nonblocking)
305{ 195{
306 struct uvc_buffer *buf; 196 int ret;
307 int ret = 0;
308
309 if (v4l2_buf->type != queue->type ||
310 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
311 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
312 "and/or memory (%u).\n", v4l2_buf->type,
313 v4l2_buf->memory);
314 return -EINVAL;
315 }
316 197
317 mutex_lock(&queue->mutex); 198 mutex_lock(&queue->mutex);
318 if (list_empty(&queue->mainqueue)) { 199 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
319 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
320 ret = -EINVAL;
321 goto done;
322 }
323
324 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
325 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
326 goto done;
327
328 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
329 buf->buf.index, buf->state, buf->buf.bytesused);
330
331 switch (buf->state) {
332 case UVC_BUF_STATE_ERROR:
333 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
334 "(transmission error).\n");
335 ret = -EIO;
336 case UVC_BUF_STATE_DONE:
337 buf->state = UVC_BUF_STATE_IDLE;
338 break;
339
340 case UVC_BUF_STATE_IDLE:
341 case UVC_BUF_STATE_QUEUED:
342 case UVC_BUF_STATE_ACTIVE:
343 default:
344 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
345 "(driver bug?).\n", buf->state);
346 ret = -EINVAL;
347 goto done;
348 }
349
350 list_del(&buf->stream);
351 __uvc_query_buffer(buf, v4l2_buf);
352
353done:
354 mutex_unlock(&queue->mutex); 200 mutex_unlock(&queue->mutex);
201
355 return ret; 202 return ret;
356} 203}
357 204
@@ -361,105 +208,47 @@ done:
361 * This function implements video queue polling and is intended to be used by 208 * This function implements video queue polling and is intended to be used by
362 * the device poll handler. 209 * the device poll handler.
363 */ 210 */
364static unsigned int 211static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
365uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, 212 struct file *file, poll_table *wait)
366 poll_table *wait)
367{ 213{
368 struct uvc_buffer *buf; 214 unsigned int ret;
369 unsigned int mask = 0;
370 215
371 mutex_lock(&queue->mutex); 216 mutex_lock(&queue->mutex);
372 if (list_empty(&queue->mainqueue)) 217 ret = vb2_poll(&queue->queue, file, wait);
373 goto done;
374
375 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
376
377 poll_wait(file, &buf->wait, wait);
378 if (buf->state == UVC_BUF_STATE_DONE ||
379 buf->state == UVC_BUF_STATE_ERROR)
380 mask |= POLLOUT | POLLWRNORM;
381
382done:
383 mutex_unlock(&queue->mutex); 218 mutex_unlock(&queue->mutex);
384 return mask;
385}
386 219
387/* 220 return ret;
388 * VMA operations.
389 */
390static void uvc_vm_open(struct vm_area_struct *vma)
391{
392 struct uvc_buffer *buffer = vma->vm_private_data;
393 buffer->vma_use_count++;
394} 221}
395 222
396static void uvc_vm_close(struct vm_area_struct *vma) 223static int uvc_queue_mmap(struct uvc_video_queue *queue,
224 struct vm_area_struct *vma)
397{ 225{
398 struct uvc_buffer *buffer = vma->vm_private_data; 226 int ret;
399 buffer->vma_use_count--;
400}
401 227
402static struct vm_operations_struct uvc_vm_ops = { 228 mutex_lock(&queue->mutex);
403 .open = uvc_vm_open, 229 ret = vb2_mmap(&queue->queue, vma);
404 .close = uvc_vm_close, 230 mutex_unlock(&queue->mutex);
405}; 231
232 return ret;
233}
406 234
235#ifndef CONFIG_MMU
407/* 236/*
408 * Memory-map a buffer. 237 * Get unmapped area.
409 * 238 *
410 * This function implements video buffer memory mapping and is intended to be 239 * NO-MMU arch need this function to make mmap() work correctly.
411 * used by the device mmap handler.
412 */ 240 */
413static int 241static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
414uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) 242 unsigned long pgoff)
415{ 243{
416 struct uvc_buffer *uninitialized_var(buffer); 244 unsigned long ret;
417 struct page *page;
418 unsigned long addr, start, size;
419 unsigned int i;
420 int ret = 0;
421
422 start = vma->vm_start;
423 size = vma->vm_end - vma->vm_start;
424 245
425 mutex_lock(&queue->mutex); 246 mutex_lock(&queue->mutex);
426 247 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
427 for (i = 0; i < queue->count; ++i) {
428 buffer = &queue->buffer[i];
429 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
430 break;
431 }
432
433 if (i == queue->count || size != queue->buf_size) {
434 ret = -EINVAL;
435 goto done;
436 }
437
438 /*
439 * VM_IO marks the area as being an mmaped region for I/O to a
440 * device. It also prevents the region from being core dumped.
441 */
442 vma->vm_flags |= VM_IO;
443
444 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
445 while (size > 0) {
446 page = vmalloc_to_page((void *)addr);
447 if ((ret = vm_insert_page(vma, start, page)) < 0)
448 goto done;
449
450 start += PAGE_SIZE;
451 addr += PAGE_SIZE;
452 size -= PAGE_SIZE;
453 }
454
455 vma->vm_ops = &uvc_vm_ops;
456 vma->vm_private_data = buffer;
457 uvc_vm_open(vma);
458
459done:
460 mutex_unlock(&queue->mutex); 248 mutex_unlock(&queue->mutex);
461 return ret; 249 return ret;
462} 250}
251#endif
463 252
464/* 253/*
465 * Cancel the video buffers queue. 254 * Cancel the video buffers queue.
@@ -484,7 +273,7 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
484 queue); 273 queue);
485 list_del(&buf->queue); 274 list_del(&buf->queue);
486 buf->state = UVC_BUF_STATE_ERROR; 275 buf->state = UVC_BUF_STATE_ERROR;
487 wake_up(&buf->wait); 276 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
488 } 277 }
489 /* This must be protected by the irqlock spinlock to avoid race 278 /* This must be protected by the irqlock spinlock to avoid race
490 * conditions between uvc_queue_buffer and the disconnection event that 279 * conditions between uvc_queue_buffer and the disconnection event that
@@ -516,26 +305,33 @@ static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
516 */ 305 */
517static int uvc_queue_enable(struct uvc_video_queue *queue, int enable) 306static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
518{ 307{
519 unsigned int i; 308 unsigned long flags;
520 int ret = 0; 309 int ret = 0;
521 310
522 mutex_lock(&queue->mutex); 311 mutex_lock(&queue->mutex);
523 if (enable) { 312 if (enable) {
524 if (uvc_queue_streaming(queue)) { 313 ret = vb2_streamon(&queue->queue, queue->queue.type);
525 ret = -EBUSY; 314 if (ret < 0)
526 goto done; 315 goto done;
527 } 316
528 queue->sequence = 0; 317 queue->sequence = 0;
529 queue->flags |= UVC_QUEUE_STREAMING;
530 queue->buf_used = 0; 318 queue->buf_used = 0;
531 } else { 319 } else {
532 uvc_queue_cancel(queue, 0); 320 ret = vb2_streamoff(&queue->queue, queue->queue.type);
533 INIT_LIST_HEAD(&queue->mainqueue); 321 if (ret < 0)
322 goto done;
534 323
535 for (i = 0; i < queue->count; ++i) 324 spin_lock_irqsave(&queue->irqlock, flags);
536 queue->buffer[i].state = UVC_BUF_STATE_IDLE; 325 INIT_LIST_HEAD(&queue->irqqueue);
537 326
538 queue->flags &= ~UVC_QUEUE_STREAMING; 327 /*
328 * FIXME: We need to clear the DISCONNECTED flag to ensure that
329 * applications will be able to queue buffers for the next
330 * streaming run. However, clearing it here doesn't guarantee
331 * that the device will be reconnected in the meantime.
332 */
333 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
334 spin_unlock_irqrestore(&queue->irqlock, flags);
539 } 335 }
540 336
541done: 337done:
@@ -544,15 +340,15 @@ done:
544} 340}
545 341
546/* called with &queue_irqlock held.. */ 342/* called with &queue_irqlock held.. */
547static struct uvc_buffer * 343static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
548uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf) 344 struct uvc_buffer *buf)
549{ 345{
550 struct uvc_buffer *nextbuf; 346 struct uvc_buffer *nextbuf;
551 347
552 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && 348 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
553 buf->buf.length != buf->buf.bytesused) { 349 buf->length != buf->bytesused) {
554 buf->state = UVC_BUF_STATE_QUEUED; 350 buf->state = UVC_BUF_STATE_QUEUED;
555 buf->buf.bytesused = 0; 351 vb2_set_plane_payload(&buf->buf, 0, 0);
556 return buf; 352 return buf;
557 } 353 }
558 354
@@ -563,10 +359,18 @@ uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf)
563 else 359 else
564 nextbuf = NULL; 360 nextbuf = NULL;
565 361
566 buf->buf.sequence = queue->sequence++; 362 /*
567 do_gettimeofday(&buf->buf.timestamp); 363 * FIXME: with videobuf2, the sequence number or timestamp fields
364 * are valid only for video capture devices and the UVC gadget usually
365 * is a video output device. Keeping these until the specs are clear on
366 * this aspect.
367 */
368 buf->buf.v4l2_buf.sequence = queue->sequence++;
369 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
370
371 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
372 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
568 373
569 wake_up(&buf->wait);
570 return nextbuf; 374 return nextbuf;
571} 375}
572 376
diff --git a/drivers/usb/gadget/uvc_queue.h b/drivers/usb/gadget/uvc_queue.h
index 1812a8ecc5d0..8e76ce982f1e 100644
--- a/drivers/usb/gadget/uvc_queue.h
+++ b/drivers/usb/gadget/uvc_queue.h
@@ -6,6 +6,7 @@
6#include <linux/kernel.h> 6#include <linux/kernel.h>
7#include <linux/poll.h> 7#include <linux/poll.h>
8#include <linux/videodev2.h> 8#include <linux/videodev2.h>
9#include <media/videobuf2-core.h>
9 10
10/* Maximum frame size in bytes, for sanity checking. */ 11/* Maximum frame size in bytes, for sanity checking. */
11#define UVC_MAX_FRAME_SIZE (16*1024*1024) 12#define UVC_MAX_FRAME_SIZE (16*1024*1024)
@@ -25,42 +26,35 @@ enum uvc_buffer_state {
25}; 26};
26 27
27struct uvc_buffer { 28struct uvc_buffer {
28 unsigned long vma_use_count; 29 struct vb2_buffer buf;
29 struct list_head stream;
30
31 /* Touched by interrupt handler. */
32 struct v4l2_buffer buf;
33 struct list_head queue; 30 struct list_head queue;
34 wait_queue_head_t wait; 31
35 enum uvc_buffer_state state; 32 enum uvc_buffer_state state;
33 void *mem;
34 unsigned int length;
35 unsigned int bytesused;
36}; 36};
37 37
38#define UVC_QUEUE_STREAMING (1 << 0) 38#define UVC_QUEUE_DISCONNECTED (1 << 0)
39#define UVC_QUEUE_DISCONNECTED (1 << 1) 39#define UVC_QUEUE_DROP_INCOMPLETE (1 << 1)
40#define UVC_QUEUE_DROP_INCOMPLETE (1 << 2) 40#define UVC_QUEUE_PAUSED (1 << 2)
41#define UVC_QUEUE_PAUSED (1 << 3)
42 41
43struct uvc_video_queue { 42struct uvc_video_queue {
44 enum v4l2_buf_type type; 43 struct vb2_queue queue;
44 struct mutex mutex; /* Protects queue */
45 45
46 void *mem;
47 unsigned int flags; 46 unsigned int flags;
48 __u32 sequence; 47 __u32 sequence;
49 48
50 unsigned int count;
51 unsigned int buf_size;
52 unsigned int buf_used; 49 unsigned int buf_used;
53 struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
54 struct mutex mutex; /* protects buffers and mainqueue */
55 spinlock_t irqlock; /* protects irqqueue */
56 50
57 struct list_head mainqueue; 51 spinlock_t irqlock; /* Protects flags and irqqueue */
58 struct list_head irqqueue; 52 struct list_head irqqueue;
59}; 53};
60 54
61static inline int uvc_queue_streaming(struct uvc_video_queue *queue) 55static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
62{ 56{
63 return queue->flags & UVC_QUEUE_STREAMING; 57 return vb2_is_streaming(&queue->queue);
64} 58}
65 59
66#endif /* __KERNEL__ */ 60#endif /* __KERNEL__ */
diff --git a/drivers/usb/gadget/uvc_v4l2.c b/drivers/usb/gadget/uvc_v4l2.c
index 2ca9386d655b..ad48e81155e2 100644
--- a/drivers/usb/gadget/uvc_v4l2.c
+++ b/drivers/usb/gadget/uvc_v4l2.c
@@ -41,9 +41,8 @@ uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
41 41
42 req->length = min_t(unsigned int, uvc->event_length, data->length); 42 req->length = min_t(unsigned int, uvc->event_length, data->length);
43 req->zero = data->length < uvc->event_length; 43 req->zero = data->length < uvc->event_length;
44 req->dma = DMA_ADDR_INVALID;
45 44
46 memcpy(req->buf, data->data, data->length); 45 memcpy(req->buf, data->data, req->length);
47 46
48 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL); 47 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
49} 48}
@@ -148,16 +147,13 @@ uvc_v4l2_release(struct file *file)
148 uvc_function_disconnect(uvc); 147 uvc_function_disconnect(uvc);
149 148
150 uvc_video_enable(video, 0); 149 uvc_video_enable(video, 0);
151 mutex_lock(&video->queue.mutex); 150 uvc_free_buffers(&video->queue);
152 if (uvc_free_buffers(&video->queue) < 0)
153 printk(KERN_ERR "uvc_v4l2_release: Unable to free "
154 "buffers.\n");
155 mutex_unlock(&video->queue.mutex);
156 151
157 file->private_data = NULL; 152 file->private_data = NULL;
158 v4l2_fh_del(&handle->vfh); 153 v4l2_fh_del(&handle->vfh);
159 v4l2_fh_exit(&handle->vfh); 154 v4l2_fh_exit(&handle->vfh);
160 kfree(handle); 155 kfree(handle);
156
161 return 0; 157 return 0;
162} 158}
163 159
@@ -178,9 +174,9 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
178 struct v4l2_capability *cap = arg; 174 struct v4l2_capability *cap = arg;
179 175
180 memset(cap, 0, sizeof *cap); 176 memset(cap, 0, sizeof *cap);
181 strncpy(cap->driver, "g_uvc", sizeof(cap->driver)); 177 strlcpy(cap->driver, "g_uvc", sizeof(cap->driver));
182 strncpy(cap->card, cdev->gadget->name, sizeof(cap->card)); 178 strlcpy(cap->card, cdev->gadget->name, sizeof(cap->card));
183 strncpy(cap->bus_info, dev_name(&cdev->gadget->dev), 179 strlcpy(cap->bus_info, dev_name(&cdev->gadget->dev),
184 sizeof cap->bus_info); 180 sizeof cap->bus_info);
185 cap->version = DRIVER_VERSION_NUMBER; 181 cap->version = DRIVER_VERSION_NUMBER;
186 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; 182 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
@@ -192,7 +188,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
192 { 188 {
193 struct v4l2_format *fmt = arg; 189 struct v4l2_format *fmt = arg;
194 190
195 if (fmt->type != video->queue.type) 191 if (fmt->type != video->queue.queue.type)
196 return -EINVAL; 192 return -EINVAL;
197 193
198 return uvc_v4l2_get_format(video, fmt); 194 return uvc_v4l2_get_format(video, fmt);
@@ -202,7 +198,7 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
202 { 198 {
203 struct v4l2_format *fmt = arg; 199 struct v4l2_format *fmt = arg;
204 200
205 if (fmt->type != video->queue.type) 201 if (fmt->type != video->queue.queue.type)
206 return -EINVAL; 202 return -EINVAL;
207 203
208 return uvc_v4l2_set_format(video, fmt); 204 return uvc_v4l2_set_format(video, fmt);
@@ -213,16 +209,13 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
213 { 209 {
214 struct v4l2_requestbuffers *rb = arg; 210 struct v4l2_requestbuffers *rb = arg;
215 211
216 if (rb->type != video->queue.type || 212 if (rb->type != video->queue.queue.type)
217 rb->memory != V4L2_MEMORY_MMAP)
218 return -EINVAL; 213 return -EINVAL;
219 214
220 ret = uvc_alloc_buffers(&video->queue, rb->count, 215 ret = uvc_alloc_buffers(&video->queue, rb);
221 video->imagesize);
222 if (ret < 0) 216 if (ret < 0)
223 return ret; 217 return ret;
224 218
225 rb->count = ret;
226 ret = 0; 219 ret = 0;
227 break; 220 break;
228 } 221 }
@@ -231,9 +224,6 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
231 { 224 {
232 struct v4l2_buffer *buf = arg; 225 struct v4l2_buffer *buf = arg;
233 226
234 if (buf->type != video->queue.type)
235 return -EINVAL;
236
237 return uvc_query_buffer(&video->queue, buf); 227 return uvc_query_buffer(&video->queue, buf);
238 } 228 }
239 229
@@ -251,24 +241,36 @@ uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
251 { 241 {
252 int *type = arg; 242 int *type = arg;
253 243
254 if (*type != video->queue.type) 244 if (*type != video->queue.queue.type)
255 return -EINVAL; 245 return -EINVAL;
256 246
257 return uvc_video_enable(video, 1); 247 /* Enable UVC video. */
248 ret = uvc_video_enable(video, 1);
249 if (ret < 0)
250 return ret;
251
252 /*
253 * Complete the alternate setting selection setup phase now that
254 * userspace is ready to provide video frames.
255 */
256 uvc_function_setup_continue(uvc);
257 uvc->state = UVC_STATE_STREAMING;
258
259 return 0;
258 } 260 }
259 261
260 case VIDIOC_STREAMOFF: 262 case VIDIOC_STREAMOFF:
261 { 263 {
262 int *type = arg; 264 int *type = arg;
263 265
264 if (*type != video->queue.type) 266 if (*type != video->queue.queue.type)
265 return -EINVAL; 267 return -EINVAL;
266 268
267 return uvc_video_enable(video, 0); 269 return uvc_video_enable(video, 0);
268 } 270 }
269 271
270 /* Events */ 272 /* Events */
271 case VIDIOC_DQEVENT: 273 case VIDIOC_DQEVENT:
272 { 274 {
273 struct v4l2_event *event = arg; 275 struct v4l2_event *event = arg;
274 276
@@ -333,17 +335,21 @@ uvc_v4l2_poll(struct file *file, poll_table *wait)
333{ 335{
334 struct video_device *vdev = video_devdata(file); 336 struct video_device *vdev = video_devdata(file);
335 struct uvc_device *uvc = video_get_drvdata(vdev); 337 struct uvc_device *uvc = video_get_drvdata(vdev);
336 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
337 unsigned int mask = 0;
338 338
339 poll_wait(file, &handle->vfh.wait, wait); 339 return uvc_queue_poll(&uvc->video.queue, file, wait);
340 if (v4l2_event_pending(&handle->vfh)) 340}
341 mask |= POLLPRI;
342 341
343 mask |= uvc_queue_poll(&uvc->video.queue, file, wait); 342#ifndef CONFIG_MMU
343static unsigned long uvc_v4l2_get_unmapped_area(struct file *file,
344 unsigned long addr, unsigned long len, unsigned long pgoff,
345 unsigned long flags)
346{
347 struct video_device *vdev = video_devdata(file);
348 struct uvc_device *uvc = video_get_drvdata(vdev);
344 349
345 return mask; 350 return uvc_queue_get_unmapped_area(&uvc->video.queue, pgoff);
346} 351}
352#endif
347 353
348static struct v4l2_file_operations uvc_v4l2_fops = { 354static struct v4l2_file_operations uvc_v4l2_fops = {
349 .owner = THIS_MODULE, 355 .owner = THIS_MODULE,
@@ -352,5 +358,8 @@ static struct v4l2_file_operations uvc_v4l2_fops = {
352 .ioctl = uvc_v4l2_ioctl, 358 .ioctl = uvc_v4l2_ioctl,
353 .mmap = uvc_v4l2_mmap, 359 .mmap = uvc_v4l2_mmap,
354 .poll = uvc_v4l2_poll, 360 .poll = uvc_v4l2_poll,
361#ifndef CONFIG_MMU
362 .get_unmapped_area = uvc_v4l2_get_unmapped_area,
363#endif
355}; 364};
356 365
diff --git a/drivers/usb/gadget/uvc_video.c b/drivers/usb/gadget/uvc_video.c
index b0e53a8ea4f7..71e896d4c5ae 100644
--- a/drivers/usb/gadget/uvc_video.c
+++ b/drivers/usb/gadget/uvc_video.c
@@ -32,7 +32,7 @@ uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
32 data[0] = 2; 32 data[0] = 2;
33 data[1] = UVC_STREAM_EOH | video->fid; 33 data[1] = UVC_STREAM_EOH | video->fid;
34 34
35 if (buf->buf.bytesused - video->queue.buf_used <= len - 2) 35 if (buf->bytesused - video->queue.buf_used <= len - 2)
36 data[1] |= UVC_STREAM_EOF; 36 data[1] |= UVC_STREAM_EOF;
37 37
38 return 2; 38 return 2;
@@ -47,8 +47,8 @@ uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
47 void *mem; 47 void *mem;
48 48
49 /* Copy video data to the USB buffer. */ 49 /* Copy video data to the USB buffer. */
50 mem = queue->mem + buf->buf.m.offset + queue->buf_used; 50 mem = buf->mem + queue->buf_used;
51 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used); 51 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
52 52
53 memcpy(data, mem, nbytes); 53 memcpy(data, mem, nbytes);
54 queue->buf_used += nbytes; 54 queue->buf_used += nbytes;
@@ -82,7 +82,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
82 req->length = video->req_size - len; 82 req->length = video->req_size - len;
83 req->zero = video->payload_size == video->max_payload_size; 83 req->zero = video->payload_size == video->max_payload_size;
84 84
85 if (buf->buf.bytesused == video->queue.buf_used) { 85 if (buf->bytesused == video->queue.buf_used) {
86 video->queue.buf_used = 0; 86 video->queue.buf_used = 0;
87 buf->state = UVC_BUF_STATE_DONE; 87 buf->state = UVC_BUF_STATE_DONE;
88 uvc_queue_next_buffer(&video->queue, buf); 88 uvc_queue_next_buffer(&video->queue, buf);
@@ -92,7 +92,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
92 } 92 }
93 93
94 if (video->payload_size == video->max_payload_size || 94 if (video->payload_size == video->max_payload_size ||
95 buf->buf.bytesused == video->queue.buf_used) 95 buf->bytesused == video->queue.buf_used)
96 video->payload_size = 0; 96 video->payload_size = 0;
97} 97}
98 98
@@ -115,7 +115,7 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
115 115
116 req->length = video->req_size - len; 116 req->length = video->req_size - len;
117 117
118 if (buf->buf.bytesused == video->queue.buf_used) { 118 if (buf->bytesused == video->queue.buf_used) {
119 video->queue.buf_used = 0; 119 video->queue.buf_used = 0;
120 buf->state = UVC_BUF_STATE_DONE; 120 buf->state = UVC_BUF_STATE_DONE;
121 uvc_queue_next_buffer(&video->queue, buf); 121 uvc_queue_next_buffer(&video->queue, buf);
@@ -161,6 +161,7 @@ static void
161uvc_video_complete(struct usb_ep *ep, struct usb_request *req) 161uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
162{ 162{
163 struct uvc_video *video = req->context; 163 struct uvc_video *video = req->context;
164 struct uvc_video_queue *queue = &video->queue;
164 struct uvc_buffer *buf; 165 struct uvc_buffer *buf;
165 unsigned long flags; 166 unsigned long flags;
166 int ret; 167 int ret;
@@ -169,13 +170,15 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
169 case 0: 170 case 0:
170 break; 171 break;
171 172
172 case -ESHUTDOWN: 173 case -ESHUTDOWN: /* disconnect from host. */
173 printk(KERN_INFO "VS request cancelled.\n"); 174 printk(KERN_INFO "VS request cancelled.\n");
175 uvc_queue_cancel(queue, 1);
174 goto requeue; 176 goto requeue;
175 177
176 default: 178 default:
177 printk(KERN_INFO "VS request completed with status %d.\n", 179 printk(KERN_INFO "VS request completed with status %d.\n",
178 req->status); 180 req->status);
181 uvc_queue_cancel(queue, 0);
179 goto requeue; 182 goto requeue;
180 } 183 }
181 184
@@ -229,13 +232,18 @@ uvc_video_free_requests(struct uvc_video *video)
229static int 232static int
230uvc_video_alloc_requests(struct uvc_video *video) 233uvc_video_alloc_requests(struct uvc_video *video)
231{ 234{
235 unsigned int req_size;
232 unsigned int i; 236 unsigned int i;
233 int ret = -ENOMEM; 237 int ret = -ENOMEM;
234 238
235 BUG_ON(video->req_size); 239 BUG_ON(video->req_size);
236 240
241 req_size = video->ep->maxpacket
242 * max_t(unsigned int, video->ep->maxburst, 1)
243 * (video->ep->mult + 1);
244
237 for (i = 0; i < UVC_NUM_REQUESTS; ++i) { 245 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
238 video->req_buffer[i] = kmalloc(video->ep->maxpacket, GFP_KERNEL); 246 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
239 if (video->req_buffer[i] == NULL) 247 if (video->req_buffer[i] == NULL)
240 goto error; 248 goto error;
241 249
@@ -245,14 +253,14 @@ uvc_video_alloc_requests(struct uvc_video *video)
245 253
246 video->req[i]->buf = video->req_buffer[i]; 254 video->req[i]->buf = video->req_buffer[i];
247 video->req[i]->length = 0; 255 video->req[i]->length = 0;
248 video->req[i]->dma = DMA_ADDR_INVALID;
249 video->req[i]->complete = uvc_video_complete; 256 video->req[i]->complete = uvc_video_complete;
250 video->req[i]->context = video; 257 video->req[i]->context = video;
251 258
252 list_add_tail(&video->req[i]->list, &video->req_free); 259 list_add_tail(&video->req[i]->list, &video->req_free);
253 } 260 }
254 261
255 video->req_size = video->ep->maxpacket; 262 video->req_size = req_size;
263
256 return 0; 264 return 0;
257 265
258error: 266error:
@@ -309,7 +317,8 @@ uvc_video_pump(struct uvc_video *video)
309 video->encode(req, video, buf); 317 video->encode(req, video, buf);
310 318
311 /* Queue the USB request */ 319 /* Queue the USB request */
312 if ((ret = usb_ep_queue(video->ep, req, GFP_KERNEL)) < 0) { 320 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
321 if (ret < 0) {
313 printk(KERN_INFO "Failed to queue request (%d)\n", ret); 322 printk(KERN_INFO "Failed to queue request (%d)\n", ret);
314 usb_ep_set_halt(video->ep); 323 usb_ep_set_halt(video->ep);
315 spin_unlock_irqrestore(&video->queue.irqlock, flags); 324 spin_unlock_irqrestore(&video->queue.irqlock, flags);