aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/amd5536udc.c10
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c48
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c17
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h8
-rw-r--r--drivers/usb/gadget/serial.c778
5 files changed, 388 insertions, 473 deletions
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index ce337cb5d137..f261d2a9a5f0 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -3251,7 +3251,7 @@ static int udc_pci_probe(
3251 /* pci setup */ 3251 /* pci setup */
3252 if (pci_enable_device(pdev) < 0) { 3252 if (pci_enable_device(pdev) < 0) {
3253 kfree(dev); 3253 kfree(dev);
3254 dev = 0; 3254 dev = NULL;
3255 retval = -ENODEV; 3255 retval = -ENODEV;
3256 goto finished; 3256 goto finished;
3257 } 3257 }
@@ -3264,7 +3264,7 @@ static int udc_pci_probe(
3264 if (!request_mem_region(resource, len, name)) { 3264 if (!request_mem_region(resource, len, name)) {
3265 dev_dbg(&pdev->dev, "pci device used already\n"); 3265 dev_dbg(&pdev->dev, "pci device used already\n");
3266 kfree(dev); 3266 kfree(dev);
3267 dev = 0; 3267 dev = NULL;
3268 retval = -EBUSY; 3268 retval = -EBUSY;
3269 goto finished; 3269 goto finished;
3270 } 3270 }
@@ -3274,7 +3274,7 @@ static int udc_pci_probe(
3274 if (dev->virt_addr == NULL) { 3274 if (dev->virt_addr == NULL) {
3275 dev_dbg(&pdev->dev, "start address cannot be mapped\n"); 3275 dev_dbg(&pdev->dev, "start address cannot be mapped\n");
3276 kfree(dev); 3276 kfree(dev);
3277 dev = 0; 3277 dev = NULL;
3278 retval = -EFAULT; 3278 retval = -EFAULT;
3279 goto finished; 3279 goto finished;
3280 } 3280 }
@@ -3282,7 +3282,7 @@ static int udc_pci_probe(
3282 if (!pdev->irq) { 3282 if (!pdev->irq) {
3283 dev_err(&dev->pdev->dev, "irq not set\n"); 3283 dev_err(&dev->pdev->dev, "irq not set\n");
3284 kfree(dev); 3284 kfree(dev);
3285 dev = 0; 3285 dev = NULL;
3286 retval = -ENODEV; 3286 retval = -ENODEV;
3287 goto finished; 3287 goto finished;
3288 } 3288 }
@@ -3290,7 +3290,7 @@ static int udc_pci_probe(
3290 if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) { 3290 if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) {
3291 dev_dbg(&dev->pdev->dev, "request_irq(%d) fail\n", pdev->irq); 3291 dev_dbg(&dev->pdev->dev, "request_irq(%d) fail\n", pdev->irq);
3292 kfree(dev); 3292 kfree(dev);
3293 dev = 0; 3293 dev = NULL;
3294 retval = -EBUSY; 3294 retval = -EBUSY;
3295 goto finished; 3295 goto finished;
3296 } 3296 }
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index e756023362c2..07e5a0b5dcda 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -649,7 +649,13 @@ static int usba_ep_disable(struct usb_ep *_ep)
649 649
650 if (!ep->desc) { 650 if (!ep->desc) {
651 spin_unlock_irqrestore(&udc->lock, flags); 651 spin_unlock_irqrestore(&udc->lock, flags);
652 DBG(DBG_ERR, "ep_disable: %s not enabled\n", ep->ep.name); 652 /* REVISIT because this driver disables endpoints in
653 * reset_all_endpoints() before calling disconnect(),
654 * most gadget drivers would trigger this non-error ...
655 */
656 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
657 DBG(DBG_ERR, "ep_disable: %s not enabled\n",
658 ep->ep.name);
653 return -EINVAL; 659 return -EINVAL;
654 } 660 }
655 ep->desc = NULL; 661 ep->desc = NULL;
@@ -1032,8 +1038,6 @@ static struct usba_udc the_udc = {
1032 .release = nop_release, 1038 .release = nop_release,
1033 }, 1039 },
1034 }, 1040 },
1035
1036 .lock = SPIN_LOCK_UNLOCKED,
1037}; 1041};
1038 1042
1039/* 1043/*
@@ -1052,6 +1056,12 @@ static void reset_all_endpoints(struct usba_udc *udc)
1052 request_complete(ep, req, -ECONNRESET); 1056 request_complete(ep, req, -ECONNRESET);
1053 } 1057 }
1054 1058
1059 /* NOTE: normally, the next call to the gadget driver is in
1060 * charge of disabling endpoints... usually disconnect().
1061 * The exception would be entering a high speed test mode.
1062 *
1063 * FIXME remove this code ... and retest thoroughly.
1064 */
1055 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) { 1065 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
1056 if (ep->desc) { 1066 if (ep->desc) {
1057 spin_unlock(&udc->lock); 1067 spin_unlock(&udc->lock);
@@ -1219,7 +1229,7 @@ static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq)
1219static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep, 1229static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
1220 struct usb_ctrlrequest *crq) 1230 struct usb_ctrlrequest *crq)
1221{ 1231{
1222 int retval = 0;; 1232 int retval = 0;
1223 1233
1224 switch (crq->bRequest) { 1234 switch (crq->bRequest) {
1225 case USB_REQ_GET_STATUS: { 1235 case USB_REQ_GET_STATUS: {
@@ -1693,6 +1703,14 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1693 usba_writel(udc, INT_CLR, USBA_END_OF_RESET); 1703 usba_writel(udc, INT_CLR, USBA_END_OF_RESET);
1694 reset_all_endpoints(udc); 1704 reset_all_endpoints(udc);
1695 1705
1706 if (udc->gadget.speed != USB_SPEED_UNKNOWN
1707 && udc->driver->disconnect) {
1708 udc->gadget.speed = USB_SPEED_UNKNOWN;
1709 spin_unlock(&udc->lock);
1710 udc->driver->disconnect(&udc->gadget);
1711 spin_lock(&udc->lock);
1712 }
1713
1696 if (status & USBA_HIGH_SPEED) { 1714 if (status & USBA_HIGH_SPEED) {
1697 DBG(DBG_BUS, "High-speed bus reset detected\n"); 1715 DBG(DBG_BUS, "High-speed bus reset detected\n");
1698 udc->gadget.speed = USB_SPEED_HIGH; 1716 udc->gadget.speed = USB_SPEED_HIGH;
@@ -1716,9 +1734,13 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1716 | USBA_DET_SUSPEND 1734 | USBA_DET_SUSPEND
1717 | USBA_END_OF_RESUME)); 1735 | USBA_END_OF_RESUME));
1718 1736
1737 /*
1738 * Unclear why we hit this irregularly, e.g. in usbtest,
1739 * but it's clearly harmless...
1740 */
1719 if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED)) 1741 if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED))
1720 dev_warn(&udc->pdev->dev, 1742 dev_dbg(&udc->pdev->dev,
1721 "WARNING: EP0 configuration is invalid!\n"); 1743 "ODD: EP0 configuration is invalid!\n");
1722 } 1744 }
1723 1745
1724 spin_unlock(&udc->lock); 1746 spin_unlock(&udc->lock);
@@ -1751,9 +1773,11 @@ static irqreturn_t usba_vbus_irq(int irq, void *devid)
1751 reset_all_endpoints(udc); 1773 reset_all_endpoints(udc);
1752 toggle_bias(0); 1774 toggle_bias(0);
1753 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1775 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1754 spin_unlock(&udc->lock); 1776 if (udc->driver->disconnect) {
1755 udc->driver->disconnect(&udc->gadget); 1777 spin_unlock(&udc->lock);
1756 spin_lock(&udc->lock); 1778 udc->driver->disconnect(&udc->gadget);
1779 spin_lock(&udc->lock);
1780 }
1757 } 1781 }
1758 udc->vbus_prev = vbus; 1782 udc->vbus_prev = vbus;
1759 } 1783 }
@@ -1825,7 +1849,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1825 1849
1826 if (!udc->pdev) 1850 if (!udc->pdev)
1827 return -ENODEV; 1851 return -ENODEV;
1828 if (driver != udc->driver) 1852 if (driver != udc->driver || !driver->unbind)
1829 return -EINVAL; 1853 return -EINVAL;
1830 1854
1831 if (udc->vbus_pin != -1) 1855 if (udc->vbus_pin != -1)
@@ -1840,6 +1864,9 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1840 toggle_bias(0); 1864 toggle_bias(0);
1841 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1865 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1842 1866
1867 if (udc->driver->disconnect)
1868 udc->driver->disconnect(&udc->gadget);
1869
1843 driver->unbind(&udc->gadget); 1870 driver->unbind(&udc->gadget);
1844 udc->gadget.dev.driver = NULL; 1871 udc->gadget.dev.driver = NULL;
1845 udc->driver = NULL; 1872 udc->driver = NULL;
@@ -1879,6 +1906,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
1879 goto err_get_hclk; 1906 goto err_get_hclk;
1880 } 1907 }
1881 1908
1909 spin_lock_init(&udc->lock);
1882 udc->pdev = pdev; 1910 udc->pdev = pdev;
1883 udc->pclk = pclk; 1911 udc->pclk = pclk;
1884 udc->hclk = hclk; 1912 udc->hclk = hclk;
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 75eba202f737..499b7a23f351 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -1546,7 +1546,6 @@ static __init void udc_init_data(struct pxa_udc *dev)
1546 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list); 1546 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1547 dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0]; 1547 dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0];
1548 ep0_idle(dev); 1548 ep0_idle(dev);
1549 strcpy(dev->dev->bus_id, "");
1550 1549
1551 /* PXA endpoints init */ 1550 /* PXA endpoints init */
1552 for (i = 0; i < NR_PXA_ENDPOINTS; i++) { 1551 for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
@@ -1746,13 +1745,10 @@ static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1746 ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i); 1745 ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i);
1747 } 1746 }
1748 1747
1749 le16_to_cpus(&u.r.wValue);
1750 le16_to_cpus(&u.r.wIndex);
1751 le16_to_cpus(&u.r.wLength);
1752
1753 ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n", 1748 ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1754 u.r.bRequestType, u.r.bRequest, 1749 u.r.bRequestType, u.r.bRequest,
1755 u.r.wValue, u.r.wIndex, u.r.wLength); 1750 le16_to_cpu(u.r.wValue), le16_to_cpu(u.r.wIndex),
1751 le16_to_cpu(u.r.wLength));
1756 if (unlikely(have_extrabytes)) 1752 if (unlikely(have_extrabytes))
1757 goto stall; 1753 goto stall;
1758 1754
@@ -2296,7 +2292,8 @@ static void pxa_udc_shutdown(struct platform_device *_dev)
2296{ 2292{
2297 struct pxa_udc *udc = platform_get_drvdata(_dev); 2293 struct pxa_udc *udc = platform_get_drvdata(_dev);
2298 2294
2299 udc_disable(udc); 2295 if (udc_readl(udc, UDCCR) & UDCCR_UDE)
2296 udc_disable(udc);
2300} 2297}
2301 2298
2302#ifdef CONFIG_PM 2299#ifdef CONFIG_PM
@@ -2361,9 +2358,8 @@ static int pxa_udc_resume(struct platform_device *_dev)
2361 * Upon exit from sleep mode and before clearing OTGPH, 2358 * Upon exit from sleep mode and before clearing OTGPH,
2362 * Software must configure the USB OTG pad, UDC, and UHC 2359 * Software must configure the USB OTG pad, UDC, and UHC
2363 * to the state they were in before entering sleep mode. 2360 * to the state they were in before entering sleep mode.
2364 *
2365 * Should be : PSSR |= PSSR_OTGPH;
2366 */ 2361 */
2362 PSSR |= PSSR_OTGPH;
2367 2363
2368 return 0; 2364 return 0;
2369} 2365}
@@ -2387,6 +2383,9 @@ static struct platform_driver udc_driver = {
2387 2383
2388static int __init udc_init(void) 2384static int __init udc_init(void)
2389{ 2385{
2386 if (!cpu_is_pxa27x())
2387 return -ENODEV;
2388
2390 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION); 2389 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2391 return platform_driver_probe(&udc_driver, pxa_udc_probe); 2390 return platform_driver_probe(&udc_driver, pxa_udc_probe);
2392} 2391}
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index 1d1b7936ee11..97453db924ff 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -484,4 +484,12 @@ static inline struct pxa_udc *to_gadget_udc(struct usb_gadget *gadget)
484#define ep_warn(ep, fmt, arg...) \ 484#define ep_warn(ep, fmt, arg...) \
485 dev_warn(ep->dev->dev, "%s:%s:" fmt, EPNAME(ep), __func__, ## arg) 485 dev_warn(ep->dev->dev, "%s:%s:" fmt, EPNAME(ep), __func__, ## arg)
486 486
487/*
488 * Cannot include pxa-regs.h, as register names are similar.
489 * So PSSR is redefined here. This should be removed once UDC registers will
490 * be gone from pxa-regs.h.
491 */
492#define PSSR __REG(0x40F00004) /* Power Manager Sleep Status */
493#define PSSR_OTGPH (1 << 6) /* OTG Peripheral Hold */
494
487#endif /* __LINUX_USB_GADGET_PXA27X_H */ 495#endif /* __LINUX_USB_GADGET_PXA27X_H */
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index 54cdd6f94034..fa019fa73334 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -14,7 +14,6 @@
14 * This software is distributed under the terms of the GNU General 14 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation, 15 * Public License ("GPL") as published by the Free Software Foundation,
16 * either version 2 of that License or (at your option) any later version. 16 * either version 2 of that License or (at your option) any later version.
17 *
18 */ 17 */
19 18
20#include <linux/kernel.h> 19#include <linux/kernel.h>
@@ -33,7 +32,7 @@
33/* Defines */ 32/* Defines */
34 33
35#define GS_VERSION_STR "v2.2" 34#define GS_VERSION_STR "v2.2"
36#define GS_VERSION_NUM 0x0202 35#define GS_VERSION_NUM 0x2200
37 36
38#define GS_LONG_NAME "Gadget Serial" 37#define GS_LONG_NAME "Gadget Serial"
39#define GS_SHORT_NAME "g_serial" 38#define GS_SHORT_NAME "g_serial"
@@ -41,7 +40,11 @@
41#define GS_MAJOR 127 40#define GS_MAJOR 127
42#define GS_MINOR_START 0 41#define GS_MINOR_START 0
43 42
44#define GS_NUM_PORTS 16 43/* REVISIT only one port is supported for now;
44 * see gs_{send,recv}_packet() ... no multiplexing,
45 * and no support for multiple ACM devices.
46 */
47#define GS_NUM_PORTS 1
45 48
46#define GS_NUM_CONFIGS 1 49#define GS_NUM_CONFIGS 1
47#define GS_NO_CONFIG_ID 0 50#define GS_NO_CONFIG_ID 0
@@ -65,6 +68,9 @@
65 68
66#define GS_DEFAULT_USE_ACM 0 69#define GS_DEFAULT_USE_ACM 0
67 70
71/* 9600-8-N-1 ... matches init_termios.c_cflag and defaults
72 * expected by "usbser.sys" on MS-Windows.
73 */
68#define GS_DEFAULT_DTE_RATE 9600 74#define GS_DEFAULT_DTE_RATE 9600
69#define GS_DEFAULT_DATA_BITS 8 75#define GS_DEFAULT_DATA_BITS 8
70#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY 76#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
@@ -107,10 +113,6 @@ static int debug = 1;
107#define GS_NOTIFY_MAXPACKET 8 113#define GS_NOTIFY_MAXPACKET 8
108 114
109 115
110/* Structures */
111
112struct gs_dev;
113
114/* circular buffer */ 116/* circular buffer */
115struct gs_buf { 117struct gs_buf {
116 unsigned int buf_size; 118 unsigned int buf_size;
@@ -119,12 +121,6 @@ struct gs_buf {
119 char *buf_put; 121 char *buf_put;
120}; 122};
121 123
122/* list of requests */
123struct gs_req_entry {
124 struct list_head re_entry;
125 struct usb_request *re_req;
126};
127
128/* the port structure holds info for each port, one for each minor number */ 124/* the port structure holds info for each port, one for each minor number */
129struct gs_port { 125struct gs_port {
130 struct gs_dev *port_dev; /* pointer to device struct */ 126 struct gs_dev *port_dev; /* pointer to device struct */
@@ -164,26 +160,7 @@ struct gs_dev {
164 160
165/* Functions */ 161/* Functions */
166 162
167/* module */ 163/* tty driver internals */
168static int __init gs_module_init(void);
169static void __exit gs_module_exit(void);
170
171/* tty driver */
172static int gs_open(struct tty_struct *tty, struct file *file);
173static void gs_close(struct tty_struct *tty, struct file *file);
174static int gs_write(struct tty_struct *tty,
175 const unsigned char *buf, int count);
176static int gs_put_char(struct tty_struct *tty, unsigned char ch);
177static void gs_flush_chars(struct tty_struct *tty);
178static int gs_write_room(struct tty_struct *tty);
179static int gs_chars_in_buffer(struct tty_struct *tty);
180static void gs_throttle(struct tty_struct * tty);
181static void gs_unthrottle(struct tty_struct * tty);
182static void gs_break(struct tty_struct *tty, int break_state);
183static int gs_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
186
187static int gs_send(struct gs_dev *dev); 164static int gs_send(struct gs_dev *dev);
188static int gs_send_packet(struct gs_dev *dev, char *packet, 165static int gs_send_packet(struct gs_dev *dev, char *packet,
189 unsigned int size); 166 unsigned int size);
@@ -192,19 +169,7 @@ static int gs_recv_packet(struct gs_dev *dev, char *packet,
192static void gs_read_complete(struct usb_ep *ep, struct usb_request *req); 169static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
193static void gs_write_complete(struct usb_ep *ep, struct usb_request *req); 170static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
194 171
195/* gadget driver */ 172/* gadget driver internals */
196static int gs_bind(struct usb_gadget *gadget);
197static void gs_unbind(struct usb_gadget *gadget);
198static int gs_setup(struct usb_gadget *gadget,
199 const struct usb_ctrlrequest *ctrl);
200static int gs_setup_standard(struct usb_gadget *gadget,
201 const struct usb_ctrlrequest *ctrl);
202static int gs_setup_class(struct usb_gadget *gadget,
203 const struct usb_ctrlrequest *ctrl);
204static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
205static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
206 struct usb_request *req);
207static void gs_disconnect(struct usb_gadget *gadget);
208static int gs_set_config(struct gs_dev *dev, unsigned config); 173static int gs_set_config(struct gs_dev *dev, unsigned config);
209static void gs_reset_config(struct gs_dev *dev); 174static void gs_reset_config(struct gs_dev *dev);
210static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, 175static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
@@ -214,10 +179,6 @@ static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
214 gfp_t kmalloc_flags); 179 gfp_t kmalloc_flags);
215static void gs_free_req(struct usb_ep *ep, struct usb_request *req); 180static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
216 181
217static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
218 gfp_t kmalloc_flags);
219static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
220
221static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags); 182static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
222static void gs_free_ports(struct gs_dev *dev); 183static void gs_free_ports(struct gs_dev *dev);
223 184
@@ -232,62 +193,15 @@ static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
232static unsigned int gs_buf_get(struct gs_buf *gb, char *buf, 193static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
233 unsigned int count); 194 unsigned int count);
234 195
235/* external functions */
236extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
237
238 196
239/* Globals */ 197/* Globals */
240 198
241static struct gs_dev *gs_device; 199static struct gs_dev *gs_device;
242 200
243static const char *EP_IN_NAME;
244static const char *EP_OUT_NAME;
245static const char *EP_NOTIFY_NAME;
246
247static struct mutex gs_open_close_lock[GS_NUM_PORTS]; 201static struct mutex gs_open_close_lock[GS_NUM_PORTS];
248 202
249static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
250static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
251
252static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
253
254static unsigned int use_acm = GS_DEFAULT_USE_ACM;
255
256
257/* tty driver struct */
258static const struct tty_operations gs_tty_ops = {
259 .open = gs_open,
260 .close = gs_close,
261 .write = gs_write,
262 .put_char = gs_put_char,
263 .flush_chars = gs_flush_chars,
264 .write_room = gs_write_room,
265 .ioctl = gs_ioctl,
266 .set_termios = gs_set_termios,
267 .throttle = gs_throttle,
268 .unthrottle = gs_unthrottle,
269 .break_ctl = gs_break,
270 .chars_in_buffer = gs_chars_in_buffer,
271};
272static struct tty_driver *gs_tty_driver;
273
274/* gadget driver struct */
275static struct usb_gadget_driver gs_gadget_driver = {
276#ifdef CONFIG_USB_GADGET_DUALSPEED
277 .speed = USB_SPEED_HIGH,
278#else
279 .speed = USB_SPEED_FULL,
280#endif /* CONFIG_USB_GADGET_DUALSPEED */
281 .function = GS_LONG_NAME,
282 .bind = gs_bind,
283 .unbind = gs_unbind,
284 .setup = gs_setup,
285 .disconnect = gs_disconnect,
286 .driver = {
287 .name = GS_SHORT_NAME,
288 },
289};
290 203
204/*-------------------------------------------------------------------------*/
291 205
292/* USB descriptors */ 206/* USB descriptors */
293 207
@@ -304,7 +218,6 @@ static char manufacturer[50];
304static struct usb_string gs_strings[] = { 218static struct usb_string gs_strings[] = {
305 { GS_MANUFACTURER_STR_ID, manufacturer }, 219 { GS_MANUFACTURER_STR_ID, manufacturer },
306 { GS_PRODUCT_STR_ID, GS_LONG_NAME }, 220 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
307 { GS_SERIAL_STR_ID, "0" },
308 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, 221 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
309 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, 222 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
310 { GS_CONTROL_STR_ID, "Gadget Serial Control" }, 223 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
@@ -327,7 +240,6 @@ static struct usb_device_descriptor gs_device_desc = {
327 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID), 240 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
328 .iManufacturer = GS_MANUFACTURER_STR_ID, 241 .iManufacturer = GS_MANUFACTURER_STR_ID,
329 .iProduct = GS_PRODUCT_STR_ID, 242 .iProduct = GS_PRODUCT_STR_ID,
330 .iSerialNumber = GS_SERIAL_STR_ID,
331 .bNumConfigurations = GS_NUM_CONFIGS, 243 .bNumConfigurations = GS_NUM_CONFIGS,
332}; 244};
333 245
@@ -364,7 +276,7 @@ static const struct usb_interface_descriptor gs_bulk_interface_desc = {
364 .bDescriptorType = USB_DT_INTERFACE, 276 .bDescriptorType = USB_DT_INTERFACE,
365 .bInterfaceNumber = GS_BULK_INTERFACE_ID, 277 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
366 .bNumEndpoints = 2, 278 .bNumEndpoints = 2,
367 .bInterfaceClass = USB_CLASS_CDC_DATA, 279 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
368 .bInterfaceSubClass = 0, 280 .bInterfaceSubClass = 0,
369 .bInterfaceProtocol = 0, 281 .bInterfaceProtocol = 0,
370 .iInterface = GS_DATA_STR_ID, 282 .iInterface = GS_DATA_STR_ID,
@@ -521,6 +433,8 @@ static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
521}; 433};
522 434
523 435
436/*-------------------------------------------------------------------------*/
437
524/* Module */ 438/* Module */
525MODULE_DESCRIPTION(GS_LONG_NAME); 439MODULE_DESCRIPTION(GS_LONG_NAME);
526MODULE_AUTHOR("Al Borchers"); 440MODULE_AUTHOR("Al Borchers");
@@ -531,84 +445,23 @@ module_param(debug, int, S_IRUGO|S_IWUSR);
531MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); 445MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
532#endif 446#endif
533 447
448static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
534module_param(read_q_size, uint, S_IRUGO); 449module_param(read_q_size, uint, S_IRUGO);
535MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32"); 450MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
536 451
452static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
537module_param(write_q_size, uint, S_IRUGO); 453module_param(write_q_size, uint, S_IRUGO);
538MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32"); 454MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
539 455
456static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
540module_param(write_buf_size, uint, S_IRUGO); 457module_param(write_buf_size, uint, S_IRUGO);
541MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); 458MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
542 459
460static unsigned int use_acm = GS_DEFAULT_USE_ACM;
543module_param(use_acm, uint, S_IRUGO); 461module_param(use_acm, uint, S_IRUGO);
544MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); 462MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
545 463
546module_init(gs_module_init); 464/*-------------------------------------------------------------------------*/
547module_exit(gs_module_exit);
548
549/*
550* gs_module_init
551*
552* Register as a USB gadget driver and a tty driver.
553*/
554static int __init gs_module_init(void)
555{
556 int i;
557 int retval;
558
559 retval = usb_gadget_register_driver(&gs_gadget_driver);
560 if (retval) {
561 pr_err("gs_module_init: cannot register gadget driver, "
562 "ret=%d\n", retval);
563 return retval;
564 }
565
566 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
567 if (!gs_tty_driver)
568 return -ENOMEM;
569 gs_tty_driver->owner = THIS_MODULE;
570 gs_tty_driver->driver_name = GS_SHORT_NAME;
571 gs_tty_driver->name = "ttygs";
572 gs_tty_driver->major = GS_MAJOR;
573 gs_tty_driver->minor_start = GS_MINOR_START;
574 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
575 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
576 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
577 gs_tty_driver->init_termios = tty_std_termios;
578 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
579 tty_set_operations(gs_tty_driver, &gs_tty_ops);
580
581 for (i=0; i < GS_NUM_PORTS; i++)
582 mutex_init(&gs_open_close_lock[i]);
583
584 retval = tty_register_driver(gs_tty_driver);
585 if (retval) {
586 usb_gadget_unregister_driver(&gs_gadget_driver);
587 put_tty_driver(gs_tty_driver);
588 pr_err("gs_module_init: cannot register tty driver, "
589 "ret=%d\n", retval);
590 return retval;
591 }
592
593 pr_info("gs_module_init: %s %s loaded\n",
594 GS_LONG_NAME, GS_VERSION_STR);
595 return 0;
596}
597
598/*
599* gs_module_exit
600*
601* Unregister as a tty driver and a USB gadget driver.
602*/
603static void __exit gs_module_exit(void)
604{
605 tty_unregister_driver(gs_tty_driver);
606 put_tty_driver(gs_tty_driver);
607 usb_gadget_unregister_driver(&gs_gadget_driver);
608
609 pr_info("gs_module_exit: %s %s unloaded\n",
610 GS_LONG_NAME, GS_VERSION_STR);
611}
612 465
613/* TTY Driver */ 466/* TTY Driver */
614 467
@@ -753,15 +606,15 @@ exit_unlock_dev:
753 * gs_close 606 * gs_close
754 */ 607 */
755 608
756#define GS_WRITE_FINISHED_EVENT_SAFELY(p) \ 609static int gs_write_finished_event_safely(struct gs_port *p)
757({ \ 610{
758 int cond; \ 611 int cond;
759 \ 612
760 spin_lock_irq(&(p)->port_lock); \ 613 spin_lock_irq(&(p)->port_lock);
761 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \ 614 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf);
762 spin_unlock_irq(&(p)->port_lock); \ 615 spin_unlock_irq(&(p)->port_lock);
763 cond; \ 616 return cond;
764}) 617}
765 618
766static void gs_close(struct tty_struct *tty, struct file *file) 619static void gs_close(struct tty_struct *tty, struct file *file)
767{ 620{
@@ -807,7 +660,7 @@ static void gs_close(struct tty_struct *tty, struct file *file)
807 if (gs_buf_data_avail(port->port_write_buf) > 0) { 660 if (gs_buf_data_avail(port->port_write_buf) > 0) {
808 spin_unlock_irq(&port->port_lock); 661 spin_unlock_irq(&port->port_lock);
809 wait_event_interruptible_timeout(port->port_write_wait, 662 wait_event_interruptible_timeout(port->port_write_wait,
810 GS_WRITE_FINISHED_EVENT_SAFELY(port), 663 gs_write_finished_event_safely(port),
811 GS_CLOSE_TIMEOUT * HZ); 664 GS_CLOSE_TIMEOUT * HZ);
812 spin_lock_irq(&port->port_lock); 665 spin_lock_irq(&port->port_lock);
813 } 666 }
@@ -1065,6 +918,23 @@ static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1065{ 918{
1066} 919}
1067 920
921static const struct tty_operations gs_tty_ops = {
922 .open = gs_open,
923 .close = gs_close,
924 .write = gs_write,
925 .put_char = gs_put_char,
926 .flush_chars = gs_flush_chars,
927 .write_room = gs_write_room,
928 .ioctl = gs_ioctl,
929 .set_termios = gs_set_termios,
930 .throttle = gs_throttle,
931 .unthrottle = gs_unthrottle,
932 .break_ctl = gs_break,
933 .chars_in_buffer = gs_chars_in_buffer,
934};
935
936/*-------------------------------------------------------------------------*/
937
1068/* 938/*
1069* gs_send 939* gs_send
1070* 940*
@@ -1080,7 +950,6 @@ static int gs_send(struct gs_dev *dev)
1080 unsigned long flags; 950 unsigned long flags;
1081 struct usb_ep *ep; 951 struct usb_ep *ep;
1082 struct usb_request *req; 952 struct usb_request *req;
1083 struct gs_req_entry *req_entry;
1084 953
1085 if (dev == NULL) { 954 if (dev == NULL) {
1086 pr_err("gs_send: NULL device pointer\n"); 955 pr_err("gs_send: NULL device pointer\n");
@@ -1093,10 +962,8 @@ static int gs_send(struct gs_dev *dev)
1093 962
1094 while(!list_empty(&dev->dev_req_list)) { 963 while(!list_empty(&dev->dev_req_list)) {
1095 964
1096 req_entry = list_entry(dev->dev_req_list.next, 965 req = list_entry(dev->dev_req_list.next,
1097 struct gs_req_entry, re_entry); 966 struct usb_request, list);
1098
1099 req = req_entry->re_req;
1100 967
1101 len = gs_send_packet(dev, req->buf, ep->maxpacket); 968 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1102 969
@@ -1106,7 +973,7 @@ static int gs_send(struct gs_dev *dev)
1106 *((unsigned char *)req->buf), 973 *((unsigned char *)req->buf),
1107 *((unsigned char *)req->buf+1), 974 *((unsigned char *)req->buf+1),
1108 *((unsigned char *)req->buf+2)); 975 *((unsigned char *)req->buf+2));
1109 list_del(&req_entry->re_entry); 976 list_del(&req->list);
1110 req->length = len; 977 req->length = len;
1111 spin_unlock_irqrestore(&dev->dev_lock, flags); 978 spin_unlock_irqrestore(&dev->dev_lock, flags);
1112 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { 979 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
@@ -1289,7 +1156,6 @@ requeue:
1289static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) 1156static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1290{ 1157{
1291 struct gs_dev *dev = ep->driver_data; 1158 struct gs_dev *dev = ep->driver_data;
1292 struct gs_req_entry *gs_req = req->context;
1293 1159
1294 if (dev == NULL) { 1160 if (dev == NULL) {
1295 pr_err("gs_write_complete: NULL device pointer\n"); 1161 pr_err("gs_write_complete: NULL device pointer\n");
@@ -1300,13 +1166,8 @@ static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1300 case 0: 1166 case 0:
1301 /* normal completion */ 1167 /* normal completion */
1302requeue: 1168requeue:
1303 if (gs_req == NULL) {
1304 pr_err("gs_write_complete: NULL request pointer\n");
1305 return;
1306 }
1307
1308 spin_lock(&dev->dev_lock); 1169 spin_lock(&dev->dev_lock);
1309 list_add(&gs_req->re_entry, &dev->dev_req_list); 1170 list_add(&req->list, &dev->dev_req_list);
1310 spin_unlock(&dev->dev_lock); 1171 spin_unlock(&dev->dev_lock);
1311 1172
1312 gs_send(dev); 1173 gs_send(dev);
@@ -1328,9 +1189,39 @@ requeue:
1328 } 1189 }
1329} 1190}
1330 1191
1192/*-------------------------------------------------------------------------*/
1193
1331/* Gadget Driver */ 1194/* Gadget Driver */
1332 1195
1333/* 1196/*
1197 * gs_unbind
1198 *
1199 * Called on module unload. Frees the control request and device
1200 * structure.
1201 */
1202static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1203{
1204 struct gs_dev *dev = get_gadget_data(gadget);
1205
1206 gs_device = NULL;
1207
1208 /* read/write requests already freed, only control request remains */
1209 if (dev != NULL) {
1210 if (dev->dev_ctrl_req != NULL) {
1211 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1212 dev->dev_ctrl_req = NULL;
1213 }
1214 gs_reset_config(dev);
1215 gs_free_ports(dev);
1216 kfree(dev);
1217 set_gadget_data(gadget, NULL);
1218 }
1219
1220 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1221 GS_VERSION_STR);
1222}
1223
1224/*
1334 * gs_bind 1225 * gs_bind
1335 * 1226 *
1336 * Called on module load. Allocates and initializes the device 1227 * Called on module load. Allocates and initializes the device
@@ -1362,19 +1253,23 @@ static int __init gs_bind(struct usb_gadget *gadget)
1362 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099); 1253 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1363 } 1254 }
1364 1255
1256 dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
1257 if (dev == NULL)
1258 return -ENOMEM;
1259
1365 usb_ep_autoconfig_reset(gadget); 1260 usb_ep_autoconfig_reset(gadget);
1366 1261
1367 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); 1262 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1368 if (!ep) 1263 if (!ep)
1369 goto autoconf_fail; 1264 goto autoconf_fail;
1370 EP_IN_NAME = ep->name; 1265 dev->dev_in_ep = ep;
1371 ep->driver_data = ep; /* claim the endpoint */ 1266 ep->driver_data = dev; /* claim the endpoint */
1372 1267
1373 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); 1268 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1374 if (!ep) 1269 if (!ep)
1375 goto autoconf_fail; 1270 goto autoconf_fail;
1376 EP_OUT_NAME = ep->name; 1271 dev->dev_out_ep = ep;
1377 ep->driver_data = ep; /* claim the endpoint */ 1272 ep->driver_data = dev; /* claim the endpoint */
1378 1273
1379 if (use_acm) { 1274 if (use_acm) {
1380 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc); 1275 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
@@ -1384,8 +1279,8 @@ static int __init gs_bind(struct usb_gadget *gadget)
1384 } 1279 }
1385 gs_device_desc.idProduct = __constant_cpu_to_le16( 1280 gs_device_desc.idProduct = __constant_cpu_to_le16(
1386 GS_CDC_PRODUCT_ID), 1281 GS_CDC_PRODUCT_ID),
1387 EP_NOTIFY_NAME = ep->name; 1282 dev->dev_notify_ep = ep;
1388 ep->driver_data = ep; /* claim the endpoint */ 1283 ep->driver_data = dev; /* claim the endpoint */
1389 } 1284 }
1390 1285
1391 gs_device_desc.bDeviceClass = use_acm 1286 gs_device_desc.bDeviceClass = use_acm
@@ -1415,9 +1310,7 @@ static int __init gs_bind(struct usb_gadget *gadget)
1415 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 1310 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1416 } 1311 }
1417 1312
1418 gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); 1313 gs_device = dev;
1419 if (dev == NULL)
1420 return -ENOMEM;
1421 1314
1422 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", 1315 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1423 init_utsname()->sysname, init_utsname()->release, 1316 init_utsname()->sysname, init_utsname()->release,
@@ -1441,8 +1334,6 @@ static int __init gs_bind(struct usb_gadget *gadget)
1441 gs_unbind(gadget); 1334 gs_unbind(gadget);
1442 return -ENOMEM; 1335 return -ENOMEM;
1443 } 1336 }
1444 dev->dev_ctrl_req->complete = gs_setup_complete;
1445
1446 gadget->ep0->driver_data = dev; 1337 gadget->ep0->driver_data = dev;
1447 1338
1448 pr_info("gs_bind: %s %s bound\n", 1339 pr_info("gs_bind: %s %s bound\n",
@@ -1451,99 +1342,11 @@ static int __init gs_bind(struct usb_gadget *gadget)
1451 return 0; 1342 return 0;
1452 1343
1453autoconf_fail: 1344autoconf_fail:
1345 kfree(dev);
1454 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name); 1346 pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
1455 return -ENODEV; 1347 return -ENODEV;
1456} 1348}
1457 1349
1458/*
1459 * gs_unbind
1460 *
1461 * Called on module unload. Frees the control request and device
1462 * structure.
1463 */
1464static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1465{
1466 struct gs_dev *dev = get_gadget_data(gadget);
1467
1468 gs_device = NULL;
1469
1470 /* read/write requests already freed, only control request remains */
1471 if (dev != NULL) {
1472 if (dev->dev_ctrl_req != NULL) {
1473 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1474 dev->dev_ctrl_req = NULL;
1475 }
1476 gs_free_ports(dev);
1477 if (dev->dev_notify_ep)
1478 usb_ep_disable(dev->dev_notify_ep);
1479 if (dev->dev_in_ep)
1480 usb_ep_disable(dev->dev_in_ep);
1481 if (dev->dev_out_ep)
1482 usb_ep_disable(dev->dev_out_ep);
1483 kfree(dev);
1484 set_gadget_data(gadget, NULL);
1485 }
1486
1487 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1488 GS_VERSION_STR);
1489}
1490
1491/*
1492 * gs_setup
1493 *
1494 * Implements all the control endpoint functionality that's not
1495 * handled in hardware or the hardware driver.
1496 *
1497 * Returns the size of the data sent to the host, or a negative
1498 * error number.
1499 */
1500static int gs_setup(struct usb_gadget *gadget,
1501 const struct usb_ctrlrequest *ctrl)
1502{
1503 int ret = -EOPNOTSUPP;
1504 struct gs_dev *dev = get_gadget_data(gadget);
1505 struct usb_request *req = dev->dev_ctrl_req;
1506 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1507 u16 wValue = le16_to_cpu(ctrl->wValue);
1508 u16 wLength = le16_to_cpu(ctrl->wLength);
1509
1510 req->complete = gs_setup_complete;
1511
1512 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1513 case USB_TYPE_STANDARD:
1514 ret = gs_setup_standard(gadget,ctrl);
1515 break;
1516
1517 case USB_TYPE_CLASS:
1518 ret = gs_setup_class(gadget,ctrl);
1519 break;
1520
1521 default:
1522 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1523 "value=%04x, index=%04x, length=%d\n",
1524 ctrl->bRequestType, ctrl->bRequest,
1525 wValue, wIndex, wLength);
1526 break;
1527 }
1528
1529 /* respond with data transfer before status phase? */
1530 if (ret >= 0) {
1531 req->length = ret;
1532 req->zero = ret < wLength
1533 && (ret % gadget->ep0->maxpacket) == 0;
1534 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1535 if (ret < 0) {
1536 pr_err("gs_setup: cannot queue response, ret=%d\n",
1537 ret);
1538 req->status = 0;
1539 gs_setup_complete(gadget->ep0, req);
1540 }
1541 }
1542
1543 /* device either stalls (ret < 0) or reports success */
1544 return ret;
1545}
1546
1547static int gs_setup_standard(struct usb_gadget *gadget, 1350static int gs_setup_standard(struct usb_gadget *gadget,
1548 const struct usb_ctrlrequest *ctrl) 1351 const struct usb_ctrlrequest *ctrl)
1549{ 1352{
@@ -1673,6 +1476,42 @@ set_interface_done:
1673 return ret; 1476 return ret;
1674} 1477}
1675 1478
1479static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1480 struct usb_request *req)
1481{
1482 struct gs_dev *dev = ep->driver_data;
1483 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1484
1485 switch (req->status) {
1486 case 0:
1487 /* normal completion */
1488 if (req->actual != sizeof(port->port_line_coding))
1489 usb_ep_set_halt(ep);
1490 else if (port) {
1491 struct usb_cdc_line_coding *value = req->buf;
1492
1493 /* REVISIT: we currently just remember this data.
1494 * If we change that, (a) validate it first, then
1495 * (b) update whatever hardware needs updating.
1496 */
1497 spin_lock(&port->port_lock);
1498 port->port_line_coding = *value;
1499 spin_unlock(&port->port_lock);
1500 }
1501 break;
1502
1503 case -ESHUTDOWN:
1504 /* disconnect */
1505 gs_free_req(ep, req);
1506 break;
1507
1508 default:
1509 /* unexpected */
1510 break;
1511 }
1512 return;
1513}
1514
1676static int gs_setup_class(struct usb_gadget *gadget, 1515static int gs_setup_class(struct usb_gadget *gadget,
1677 const struct usb_ctrlrequest *ctrl) 1516 const struct usb_ctrlrequest *ctrl)
1678{ 1517{
@@ -1734,52 +1573,72 @@ static int gs_setup_class(struct usb_gadget *gadget,
1734 return ret; 1573 return ret;
1735} 1574}
1736 1575
1737static void gs_setup_complete_set_line_coding(struct usb_ep *ep, 1576/*
1738 struct usb_request *req) 1577 * gs_setup_complete
1578 */
1579static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1739{ 1580{
1740 struct gs_dev *dev = ep->driver_data; 1581 if (req->status || req->actual != req->length) {
1741 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ 1582 pr_err("gs_setup_complete: status error, status=%d, "
1583 "actual=%d, length=%d\n",
1584 req->status, req->actual, req->length);
1585 }
1586}
1742 1587
1743 switch (req->status) { 1588/*
1744 case 0: 1589 * gs_setup
1745 /* normal completion */ 1590 *
1746 if (req->actual != sizeof(port->port_line_coding)) 1591 * Implements all the control endpoint functionality that's not
1747 usb_ep_set_halt(ep); 1592 * handled in hardware or the hardware driver.
1748 else if (port) { 1593 *
1749 struct usb_cdc_line_coding *value = req->buf; 1594 * Returns the size of the data sent to the host, or a negative
1595 * error number.
1596 */
1597static int gs_setup(struct usb_gadget *gadget,
1598 const struct usb_ctrlrequest *ctrl)
1599{
1600 int ret = -EOPNOTSUPP;
1601 struct gs_dev *dev = get_gadget_data(gadget);
1602 struct usb_request *req = dev->dev_ctrl_req;
1603 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1604 u16 wValue = le16_to_cpu(ctrl->wValue);
1605 u16 wLength = le16_to_cpu(ctrl->wLength);
1750 1606
1751 /* REVISIT: we currently just remember this data. 1607 req->complete = gs_setup_complete;
1752 * If we change that, (a) validate it first, then 1608
1753 * (b) update whatever hardware needs updating. 1609 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1754 */ 1610 case USB_TYPE_STANDARD:
1755 spin_lock(&port->port_lock); 1611 ret = gs_setup_standard(gadget, ctrl);
1756 port->port_line_coding = *value;
1757 spin_unlock(&port->port_lock);
1758 }
1759 break; 1612 break;
1760 1613
1761 case -ESHUTDOWN: 1614 case USB_TYPE_CLASS:
1762 /* disconnect */ 1615 ret = gs_setup_class(gadget, ctrl);
1763 gs_free_req(ep, req);
1764 break; 1616 break;
1765 1617
1766 default: 1618 default:
1767 /* unexpected */ 1619 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1620 "value=%04x, index=%04x, length=%d\n",
1621 ctrl->bRequestType, ctrl->bRequest,
1622 wValue, wIndex, wLength);
1768 break; 1623 break;
1769 } 1624 }
1770 return;
1771}
1772 1625
1773/* 1626 /* respond with data transfer before status phase? */
1774 * gs_setup_complete 1627 if (ret >= 0) {
1775 */ 1628 req->length = ret;
1776static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req) 1629 req->zero = ret < wLength
1777{ 1630 && (ret % gadget->ep0->maxpacket) == 0;
1778 if (req->status || req->actual != req->length) { 1631 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1779 pr_err("gs_setup_complete: status error, status=%d, " 1632 if (ret < 0) {
1780 "actual=%d, length=%d\n", 1633 pr_err("gs_setup: cannot queue response, ret=%d\n",
1781 req->status, req->actual, req->length); 1634 ret);
1635 req->status = 0;
1636 gs_setup_complete(gadget->ep0, req);
1637 }
1782 } 1638 }
1639
1640 /* device either stalls (ret < 0) or reports success */
1641 return ret;
1783} 1642}
1784 1643
1785/* 1644/*
@@ -1811,6 +1670,23 @@ static void gs_disconnect(struct usb_gadget *gadget)
1811 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); 1670 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1812} 1671}
1813 1672
1673static struct usb_gadget_driver gs_gadget_driver = {
1674#ifdef CONFIG_USB_GADGET_DUALSPEED
1675 .speed = USB_SPEED_HIGH,
1676#else
1677 .speed = USB_SPEED_FULL,
1678#endif /* CONFIG_USB_GADGET_DUALSPEED */
1679 .function = GS_LONG_NAME,
1680 .bind = gs_bind,
1681 .unbind = gs_unbind,
1682 .setup = gs_setup,
1683 .disconnect = gs_disconnect,
1684 .driver = {
1685 .name = GS_SHORT_NAME,
1686 .owner = THIS_MODULE,
1687 },
1688};
1689
1814/* 1690/*
1815 * gs_set_config 1691 * gs_set_config
1816 * 1692 *
@@ -1826,9 +1702,8 @@ static int gs_set_config(struct gs_dev *dev, unsigned config)
1826 int ret = 0; 1702 int ret = 0;
1827 struct usb_gadget *gadget = dev->dev_gadget; 1703 struct usb_gadget *gadget = dev->dev_gadget;
1828 struct usb_ep *ep; 1704 struct usb_ep *ep;
1829 struct usb_endpoint_descriptor *ep_desc; 1705 struct usb_endpoint_descriptor *out, *in, *notify;
1830 struct usb_request *req; 1706 struct usb_request *req;
1831 struct gs_req_entry *req_entry;
1832 1707
1833 if (dev == NULL) { 1708 if (dev == NULL) {
1834 pr_err("gs_set_config: NULL device pointer\n"); 1709 pr_err("gs_set_config: NULL device pointer\n");
@@ -1846,86 +1721,62 @@ static int gs_set_config(struct gs_dev *dev, unsigned config)
1846 case GS_BULK_CONFIG_ID: 1721 case GS_BULK_CONFIG_ID:
1847 if (use_acm) 1722 if (use_acm)
1848 return -EINVAL; 1723 return -EINVAL;
1849 /* device specific optimizations */
1850 if (gadget_is_net2280(gadget))
1851 net2280_set_fifo_mode(gadget, 1);
1852 break; 1724 break;
1853 case GS_ACM_CONFIG_ID: 1725 case GS_ACM_CONFIG_ID:
1854 if (!use_acm) 1726 if (!use_acm)
1855 return -EINVAL; 1727 return -EINVAL;
1856 /* device specific optimizations */
1857 if (gadget_is_net2280(gadget))
1858 net2280_set_fifo_mode(gadget, 1);
1859 break; 1728 break;
1860 default: 1729 default:
1861 return -EINVAL; 1730 return -EINVAL;
1862 } 1731 }
1863 1732
1864 dev->dev_config = config; 1733 in = choose_ep_desc(gadget,
1865 1734 &gs_highspeed_in_desc,
1866 gadget_for_each_ep(ep, gadget) { 1735 &gs_fullspeed_in_desc);
1867 1736 out = choose_ep_desc(gadget,
1868 if (EP_NOTIFY_NAME 1737 &gs_highspeed_out_desc,
1869 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) { 1738 &gs_fullspeed_out_desc);
1870 ep_desc = choose_ep_desc(gadget, 1739 notify = dev->dev_notify_ep
1740 ? choose_ep_desc(gadget,
1871 &gs_highspeed_notify_desc, 1741 &gs_highspeed_notify_desc,
1872 &gs_fullspeed_notify_desc); 1742 &gs_fullspeed_notify_desc)
1873 ret = usb_ep_enable(ep,ep_desc); 1743 : NULL;
1874 if (ret == 0) {
1875 ep->driver_data = dev;
1876 dev->dev_notify_ep = ep;
1877 dev->dev_notify_ep_desc = ep_desc;
1878 } else {
1879 pr_err("gs_set_config: cannot enable NOTIFY "
1880 "endpoint %s, ret=%d\n",
1881 ep->name, ret);
1882 goto exit_reset_config;
1883 }
1884 }
1885
1886 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1887 ep_desc = choose_ep_desc(gadget,
1888 &gs_highspeed_in_desc,
1889 &gs_fullspeed_in_desc);
1890 ret = usb_ep_enable(ep,ep_desc);
1891 if (ret == 0) {
1892 ep->driver_data = dev;
1893 dev->dev_in_ep = ep;
1894 dev->dev_in_ep_desc = ep_desc;
1895 } else {
1896 pr_err("gs_set_config: cannot enable IN "
1897 "endpoint %s, ret=%d\n",
1898 ep->name, ret);
1899 goto exit_reset_config;
1900 }
1901 }
1902 1744
1903 else if (strcmp(ep->name, EP_OUT_NAME) == 0) { 1745 ret = usb_ep_enable(dev->dev_in_ep, in);
1904 ep_desc = choose_ep_desc(gadget, 1746 if (ret == 0) {
1905 &gs_highspeed_out_desc, 1747 dev->dev_in_ep_desc = in;
1906 &gs_fullspeed_out_desc); 1748 } else {
1907 ret = usb_ep_enable(ep,ep_desc); 1749 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1908 if (ret == 0) { 1750 __func__, "IN", dev->dev_in_ep->name, ret);
1909 ep->driver_data = dev; 1751 return ret;
1910 dev->dev_out_ep = ep; 1752 }
1911 dev->dev_out_ep_desc = ep_desc;
1912 } else {
1913 pr_err("gs_set_config: cannot enable OUT "
1914 "endpoint %s, ret=%d\n",
1915 ep->name, ret);
1916 goto exit_reset_config;
1917 }
1918 }
1919 1753
1754 ret = usb_ep_enable(dev->dev_out_ep, out);
1755 if (ret == 0) {
1756 dev->dev_out_ep_desc = out;
1757 } else {
1758 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1759 __func__, "OUT", dev->dev_out_ep->name, ret);
1760fail0:
1761 usb_ep_disable(dev->dev_in_ep);
1762 return ret;
1920 } 1763 }
1921 1764
1922 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL 1765 if (notify) {
1923 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) { 1766 ret = usb_ep_enable(dev->dev_notify_ep, notify);
1924 pr_err("gs_set_config: cannot find endpoints\n"); 1767 if (ret == 0) {
1925 ret = -ENODEV; 1768 dev->dev_notify_ep_desc = notify;
1926 goto exit_reset_config; 1769 } else {
1770 pr_debug("%s: cannot enable %s %s, ret=%d\n",
1771 __func__, "NOTIFY",
1772 dev->dev_notify_ep->name, ret);
1773 usb_ep_disable(dev->dev_out_ep);
1774 goto fail0;
1775 }
1927 } 1776 }
1928 1777
1778 dev->dev_config = config;
1779
1929 /* allocate and queue read requests */ 1780 /* allocate and queue read requests */
1930 ep = dev->dev_out_ep; 1781 ep = dev->dev_out_ep;
1931 for (i=0; i<read_q_size && ret == 0; i++) { 1782 for (i=0; i<read_q_size && ret == 0; i++) {
@@ -1946,9 +1797,10 @@ static int gs_set_config(struct gs_dev *dev, unsigned config)
1946 /* allocate write requests, and put on free list */ 1797 /* allocate write requests, and put on free list */
1947 ep = dev->dev_in_ep; 1798 ep = dev->dev_in_ep;
1948 for (i=0; i<write_q_size; i++) { 1799 for (i=0; i<write_q_size; i++) {
1949 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) { 1800 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
1950 req_entry->re_req->complete = gs_write_complete; 1801 if (req) {
1951 list_add(&req_entry->re_entry, &dev->dev_req_list); 1802 req->complete = gs_write_complete;
1803 list_add(&req->list, &dev->dev_req_list);
1952 } else { 1804 } else {
1953 pr_err("gs_set_config: cannot allocate " 1805 pr_err("gs_set_config: cannot allocate "
1954 "write requests\n"); 1806 "write requests\n");
@@ -1986,7 +1838,7 @@ exit_reset_config:
1986 */ 1838 */
1987static void gs_reset_config(struct gs_dev *dev) 1839static void gs_reset_config(struct gs_dev *dev)
1988{ 1840{
1989 struct gs_req_entry *req_entry; 1841 struct usb_request *req;
1990 1842
1991 if (dev == NULL) { 1843 if (dev == NULL) {
1992 pr_err("gs_reset_config: NULL device pointer\n"); 1844 pr_err("gs_reset_config: NULL device pointer\n");
@@ -2000,26 +1852,18 @@ static void gs_reset_config(struct gs_dev *dev)
2000 1852
2001 /* free write requests on the free list */ 1853 /* free write requests on the free list */
2002 while(!list_empty(&dev->dev_req_list)) { 1854 while(!list_empty(&dev->dev_req_list)) {
2003 req_entry = list_entry(dev->dev_req_list.next, 1855 req = list_entry(dev->dev_req_list.next,
2004 struct gs_req_entry, re_entry); 1856 struct usb_request, list);
2005 list_del(&req_entry->re_entry); 1857 list_del(&req->list);
2006 gs_free_req_entry(dev->dev_in_ep, req_entry); 1858 gs_free_req(dev->dev_in_ep, req);
2007 } 1859 }
2008 1860
2009 /* disable endpoints, forcing completion of pending i/o; */ 1861 /* disable endpoints, forcing completion of pending i/o; */
2010 /* completion handlers free their requests in this case */ 1862 /* completion handlers free their requests in this case */
2011 if (dev->dev_notify_ep) { 1863 if (dev->dev_notify_ep)
2012 usb_ep_disable(dev->dev_notify_ep); 1864 usb_ep_disable(dev->dev_notify_ep);
2013 dev->dev_notify_ep = NULL; 1865 usb_ep_disable(dev->dev_in_ep);
2014 } 1866 usb_ep_disable(dev->dev_out_ep);
2015 if (dev->dev_in_ep) {
2016 usb_ep_disable(dev->dev_in_ep);
2017 dev->dev_in_ep = NULL;
2018 }
2019 if (dev->dev_out_ep) {
2020 usb_ep_disable(dev->dev_out_ep);
2021 dev->dev_out_ep = NULL;
2022 }
2023} 1867}
2024 1868
2025/* 1869/*
@@ -2113,46 +1957,6 @@ static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2113} 1957}
2114 1958
2115/* 1959/*
2116 * gs_alloc_req_entry
2117 *
2118 * Allocates a request and its buffer, using the given
2119 * endpoint, buffer len, and kmalloc flags.
2120 */
2121static struct gs_req_entry *
2122gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
2123{
2124 struct gs_req_entry *req;
2125
2126 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2127 if (req == NULL)
2128 return NULL;
2129
2130 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2131 if (req->re_req == NULL) {
2132 kfree(req);
2133 return NULL;
2134 }
2135
2136 req->re_req->context = req;
2137
2138 return req;
2139}
2140
2141/*
2142 * gs_free_req_entry
2143 *
2144 * Frees a request and its buffer.
2145 */
2146static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2147{
2148 if (ep != NULL && req != NULL) {
2149 if (req->re_req != NULL)
2150 gs_free_req(ep, req->re_req);
2151 kfree(req);
2152 }
2153}
2154
2155/*
2156 * gs_alloc_ports 1960 * gs_alloc_ports
2157 * 1961 *
2158 * Allocate all ports and set the gs_dev struct to point to them. 1962 * Allocate all ports and set the gs_dev struct to point to them.
@@ -2233,6 +2037,8 @@ static void gs_free_ports(struct gs_dev *dev)
2233 } 2037 }
2234} 2038}
2235 2039
2040/*-------------------------------------------------------------------------*/
2041
2236/* Circular Buffer */ 2042/* Circular Buffer */
2237 2043
2238/* 2044/*
@@ -2393,3 +2199,77 @@ gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2393 2199
2394 return count; 2200 return count;
2395} 2201}
2202
2203/*-------------------------------------------------------------------------*/
2204
2205static struct tty_driver *gs_tty_driver;
2206
2207/*
2208 * gs_module_init
2209 *
2210 * Register as a USB gadget driver and a tty driver.
2211 */
2212static int __init gs_module_init(void)
2213{
2214 int i;
2215 int retval;
2216
2217 retval = usb_gadget_register_driver(&gs_gadget_driver);
2218 if (retval) {
2219 pr_err("gs_module_init: cannot register gadget driver, "
2220 "ret=%d\n", retval);
2221 return retval;
2222 }
2223
2224 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
2225 if (!gs_tty_driver)
2226 return -ENOMEM;
2227 gs_tty_driver->owner = THIS_MODULE;
2228 gs_tty_driver->driver_name = GS_SHORT_NAME;
2229 gs_tty_driver->name = "ttygs";
2230 gs_tty_driver->major = GS_MAJOR;
2231 gs_tty_driver->minor_start = GS_MINOR_START;
2232 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2233 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2234 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2235 gs_tty_driver->init_termios = tty_std_termios;
2236 /* must match GS_DEFAULT_DTE_RATE and friends */
2237 gs_tty_driver->init_termios.c_cflag =
2238 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2239 gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE;
2240 gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE;
2241 tty_set_operations(gs_tty_driver, &gs_tty_ops);
2242
2243 for (i = 0; i < GS_NUM_PORTS; i++)
2244 mutex_init(&gs_open_close_lock[i]);
2245
2246 retval = tty_register_driver(gs_tty_driver);
2247 if (retval) {
2248 usb_gadget_unregister_driver(&gs_gadget_driver);
2249 put_tty_driver(gs_tty_driver);
2250 pr_err("gs_module_init: cannot register tty driver, "
2251 "ret=%d\n", retval);
2252 return retval;
2253 }
2254
2255 pr_info("gs_module_init: %s %s loaded\n",
2256 GS_LONG_NAME, GS_VERSION_STR);
2257 return 0;
2258}
2259module_init(gs_module_init);
2260
2261/*
2262 * gs_module_exit
2263 *
2264 * Unregister as a tty driver and a USB gadget driver.
2265 */
2266static void __exit gs_module_exit(void)
2267{
2268 tty_unregister_driver(gs_tty_driver);
2269 put_tty_driver(gs_tty_driver);
2270 usb_gadget_unregister_driver(&gs_gadget_driver);
2271
2272 pr_info("gs_module_exit: %s %s unloaded\n",
2273 GS_LONG_NAME, GS_VERSION_STR);
2274}
2275module_exit(gs_module_exit);