diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2008-07-21 18:42:20 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-07-21 18:42:53 -0400 |
commit | 93ded9b8fd42abe2c3607097963d8de6ad9117eb (patch) | |
tree | 407a3adcf885ffd75a4d3299eaefd9b171b739be /drivers/usb/gadget | |
parent | 6d52dcbe56ca8464bcad56d98a64bcd781596663 (diff) | |
parent | f756cbd458ab71c996a069cb3928fb1e2d7cd9cc (diff) |
Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6: (100 commits)
usb-storage: revert DMA-alignment change for Wireless USB
USB: use reset_resume when normal resume fails
usb_gadget: composite cdc gadget fault handling
usb gadget: minor USBCV fix for composite framework
USB: Fix bug with byte order in isp116x-hcd.c fio write/read
USB: fix double kfree in ipaq in error case
USB: fix build error in cdc-acm for CONFIG_PM=n
USB: remove board-specific UP2OCR configuration from pxa27x-udc
USB: EHCI: Reconciling USB register differences on MPC85xx vs MPC83xx
USB: Fix pointer/int cast in USB devio code
usb gadget: g_cdc dependso on NET
USB: Au1xxx-usb: suspend/resume support.
USB: Au1xxx-usb: clean up ohci/ehci bus glue sources.
usbfs: don't store bad pointers in registration
usbfs: fix race between open and unregister
usbfs: simplify the lookup-by-minor routines
usbfs: send disconnect signals when device is unregistered
USB: Force unbinding of drivers lacking reset_resume or other methods
USB: ohci-pnx4008: I2C cleanups and fixes
USB: debug port converter does not accept more than 8 byte packets
...
Diffstat (limited to 'drivers/usb/gadget')
39 files changed, 8315 insertions, 6074 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index d6bab0d5f453..c6a8c6b1116a 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig | |||
@@ -586,6 +586,20 @@ config USB_G_PRINTER | |||
586 | For more information, see Documentation/usb/gadget_printer.txt | 586 | For more information, see Documentation/usb/gadget_printer.txt |
587 | which includes sample code for accessing the device file. | 587 | which includes sample code for accessing the device file. |
588 | 588 | ||
589 | config USB_CDC_COMPOSITE | ||
590 | tristate "CDC Composite Device (Ethernet and ACM)" | ||
591 | depends on NET | ||
592 | help | ||
593 | This driver provides two functions in one configuration: | ||
594 | a CDC Ethernet (ECM) link, and a CDC ACM (serial port) link. | ||
595 | |||
596 | This driver requires four bulk and two interrupt endpoints, | ||
597 | plus the ability to handle altsettings. Not all peripheral | ||
598 | controllers are that capable. | ||
599 | |||
600 | Say "y" to link the driver statically, or "m" to build a | ||
601 | dynamically linked module. | ||
602 | |||
589 | # put drivers that need isochronous transfer support (for audio | 603 | # put drivers that need isochronous transfer support (for audio |
590 | # or video class gadget drivers), or specific hardware, here. | 604 | # or video class gadget drivers), or specific hardware, here. |
591 | 605 | ||
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index e258afd25faf..fcb5cb9094d9 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile | |||
@@ -22,18 +22,22 @@ obj-$(CONFIG_USB_M66592) += m66592-udc.o | |||
22 | # | 22 | # |
23 | # USB gadget drivers | 23 | # USB gadget drivers |
24 | # | 24 | # |
25 | g_zero-objs := zero.o usbstring.o config.o epautoconf.o | 25 | C_UTILS = composite.o usbstring.o config.o epautoconf.o |
26 | g_ether-objs := ether.o usbstring.o config.o epautoconf.o | 26 | |
27 | g_serial-objs := serial.o usbstring.o config.o epautoconf.o | 27 | g_zero-objs := zero.o f_sourcesink.o f_loopback.o $(C_UTILS) |
28 | g_ether-objs := ether.o u_ether.o f_subset.o f_ecm.o $(C_UTILS) | ||
29 | g_serial-objs := serial.o u_serial.o f_acm.o f_serial.o $(C_UTILS) | ||
28 | g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o | 30 | g_midi-objs := gmidi.o usbstring.o config.o epautoconf.o |
29 | gadgetfs-objs := inode.o | 31 | gadgetfs-objs := inode.o |
30 | g_file_storage-objs := file_storage.o usbstring.o config.o \ | 32 | g_file_storage-objs := file_storage.o usbstring.o config.o \ |
31 | epautoconf.o | 33 | epautoconf.o |
32 | g_printer-objs := printer.o usbstring.o config.o \ | 34 | g_printer-objs := printer.o usbstring.o config.o \ |
33 | epautoconf.o | 35 | epautoconf.o |
36 | g_cdc-objs := cdc2.o u_ether.o f_ecm.o \ | ||
37 | u_serial.o f_acm.o $(C_UTILS) | ||
34 | 38 | ||
35 | ifeq ($(CONFIG_USB_ETH_RNDIS),y) | 39 | ifeq ($(CONFIG_USB_ETH_RNDIS),y) |
36 | g_ether-objs += rndis.o | 40 | g_ether-objs += f_rndis.o rndis.o |
37 | endif | 41 | endif |
38 | 42 | ||
39 | obj-$(CONFIG_USB_ZERO) += g_zero.o | 43 | obj-$(CONFIG_USB_ZERO) += g_zero.o |
@@ -43,4 +47,5 @@ obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o | |||
43 | obj-$(CONFIG_USB_G_SERIAL) += g_serial.o | 47 | obj-$(CONFIG_USB_G_SERIAL) += g_serial.o |
44 | obj-$(CONFIG_USB_G_PRINTER) += g_printer.o | 48 | obj-$(CONFIG_USB_G_PRINTER) += g_printer.o |
45 | obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o | 49 | obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o |
50 | obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o | ||
46 | 51 | ||
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c index f261d2a9a5f0..1500e1b3c302 100644 --- a/drivers/usb/gadget/amd5536udc.c +++ b/drivers/usb/gadget/amd5536udc.c | |||
@@ -3342,7 +3342,7 @@ static int udc_probe(struct udc *dev) | |||
3342 | spin_lock_init(&dev->lock); | 3342 | spin_lock_init(&dev->lock); |
3343 | dev->gadget.ops = &udc_ops; | 3343 | dev->gadget.ops = &udc_ops; |
3344 | 3344 | ||
3345 | strcpy(dev->gadget.dev.bus_id, "gadget"); | 3345 | dev_set_name(&dev->gadget.dev, "gadget"); |
3346 | dev->gadget.dev.release = gadget_release; | 3346 | dev->gadget.dev.release = gadget_release; |
3347 | dev->gadget.name = name; | 3347 | dev->gadget.name = name; |
3348 | dev->gadget.name = name; | 3348 | dev->gadget.name = name; |
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c index b6b2a0a5ba37..e2d8a5d86c40 100644 --- a/drivers/usb/gadget/at91_udc.c +++ b/drivers/usb/gadget/at91_udc.c | |||
@@ -1687,6 +1687,19 @@ static int __init at91udc_probe(struct platform_device *pdev) | |||
1687 | udc->board.pullup_active_low); | 1687 | udc->board.pullup_active_low); |
1688 | } | 1688 | } |
1689 | 1689 | ||
1690 | /* newer chips have more FIFO memory than rm9200 */ | ||
1691 | if (cpu_is_at91sam9260()) { | ||
1692 | udc->ep[0].maxpacket = 64; | ||
1693 | udc->ep[3].maxpacket = 64; | ||
1694 | udc->ep[4].maxpacket = 512; | ||
1695 | udc->ep[5].maxpacket = 512; | ||
1696 | } else if (cpu_is_at91sam9261()) { | ||
1697 | udc->ep[3].maxpacket = 64; | ||
1698 | } else if (cpu_is_at91sam9263()) { | ||
1699 | udc->ep[0].maxpacket = 64; | ||
1700 | udc->ep[3].maxpacket = 64; | ||
1701 | } | ||
1702 | |||
1690 | udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1); | 1703 | udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1); |
1691 | if (!udc->udp_baseaddr) { | 1704 | if (!udc->udp_baseaddr) { |
1692 | retval = -ENOMEM; | 1705 | retval = -ENOMEM; |
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c new file mode 100644 index 000000000000..d490d0289507 --- /dev/null +++ b/drivers/usb/gadget/cdc2.c | |||
@@ -0,0 +1,246 @@ | |||
1 | /* | ||
2 | * cdc2.c -- CDC Composite driver, with ECM and ACM support | ||
3 | * | ||
4 | * Copyright (C) 2008 David Brownell | ||
5 | * Copyright (C) 2008 Nokia Corporation | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | #include <linux/kernel.h> | ||
23 | #include <linux/utsname.h> | ||
24 | |||
25 | #include "u_ether.h" | ||
26 | #include "u_serial.h" | ||
27 | |||
28 | |||
29 | #define DRIVER_DESC "CDC Composite Gadget" | ||
30 | #define DRIVER_VERSION "King Kamehameha Day 2008" | ||
31 | |||
32 | /*-------------------------------------------------------------------------*/ | ||
33 | |||
34 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | ||
35 | * Instead: allocate your own, using normal USB-IF procedures. | ||
36 | */ | ||
37 | |||
38 | /* Thanks to NetChip Technologies for donating this product ID. | ||
39 | * It's for devices with only this composite CDC configuration. | ||
40 | */ | ||
41 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | ||
42 | #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */ | ||
43 | |||
44 | /*-------------------------------------------------------------------------*/ | ||
45 | |||
46 | static struct usb_device_descriptor device_desc = { | ||
47 | .bLength = sizeof device_desc, | ||
48 | .bDescriptorType = USB_DT_DEVICE, | ||
49 | |||
50 | .bcdUSB = __constant_cpu_to_le16(0x0200), | ||
51 | |||
52 | .bDeviceClass = USB_CLASS_COMM, | ||
53 | .bDeviceSubClass = 0, | ||
54 | .bDeviceProtocol = 0, | ||
55 | /* .bMaxPacketSize0 = f(hardware) */ | ||
56 | |||
57 | /* Vendor and product id can be overridden by module parameters. */ | ||
58 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), | ||
59 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), | ||
60 | /* .bcdDevice = f(hardware) */ | ||
61 | /* .iManufacturer = DYNAMIC */ | ||
62 | /* .iProduct = DYNAMIC */ | ||
63 | /* NO SERIAL NUMBER */ | ||
64 | .bNumConfigurations = 1, | ||
65 | }; | ||
66 | |||
67 | static struct usb_otg_descriptor otg_descriptor = { | ||
68 | .bLength = sizeof otg_descriptor, | ||
69 | .bDescriptorType = USB_DT_OTG, | ||
70 | |||
71 | /* REVISIT SRP-only hardware is possible, although | ||
72 | * it would not be called "OTG" ... | ||
73 | */ | ||
74 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | ||
75 | }; | ||
76 | |||
77 | static const struct usb_descriptor_header *otg_desc[] = { | ||
78 | (struct usb_descriptor_header *) &otg_descriptor, | ||
79 | NULL, | ||
80 | }; | ||
81 | |||
82 | |||
83 | /* string IDs are assigned dynamically */ | ||
84 | |||
85 | #define STRING_MANUFACTURER_IDX 0 | ||
86 | #define STRING_PRODUCT_IDX 1 | ||
87 | |||
88 | static char manufacturer[50]; | ||
89 | |||
90 | static struct usb_string strings_dev[] = { | ||
91 | [STRING_MANUFACTURER_IDX].s = manufacturer, | ||
92 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, | ||
93 | { } /* end of list */ | ||
94 | }; | ||
95 | |||
96 | static struct usb_gadget_strings stringtab_dev = { | ||
97 | .language = 0x0409, /* en-us */ | ||
98 | .strings = strings_dev, | ||
99 | }; | ||
100 | |||
101 | static struct usb_gadget_strings *dev_strings[] = { | ||
102 | &stringtab_dev, | ||
103 | NULL, | ||
104 | }; | ||
105 | |||
106 | static u8 hostaddr[ETH_ALEN]; | ||
107 | |||
108 | /*-------------------------------------------------------------------------*/ | ||
109 | |||
110 | /* | ||
111 | * We _always_ have both CDC ECM and CDC ACM functions. | ||
112 | */ | ||
113 | static int __init cdc_do_config(struct usb_configuration *c) | ||
114 | { | ||
115 | int status; | ||
116 | |||
117 | if (gadget_is_otg(c->cdev->gadget)) { | ||
118 | c->descriptors = otg_desc; | ||
119 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
120 | } | ||
121 | |||
122 | status = ecm_bind_config(c, hostaddr); | ||
123 | if (status < 0) | ||
124 | return status; | ||
125 | |||
126 | status = acm_bind_config(c, 0); | ||
127 | if (status < 0) | ||
128 | return status; | ||
129 | |||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | static struct usb_configuration cdc_config_driver = { | ||
134 | .label = "CDC Composite (ECM + ACM)", | ||
135 | .bind = cdc_do_config, | ||
136 | .bConfigurationValue = 1, | ||
137 | /* .iConfiguration = DYNAMIC */ | ||
138 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | ||
139 | .bMaxPower = 1, /* 2 mA, minimal */ | ||
140 | }; | ||
141 | |||
142 | /*-------------------------------------------------------------------------*/ | ||
143 | |||
144 | static int __init cdc_bind(struct usb_composite_dev *cdev) | ||
145 | { | ||
146 | int gcnum; | ||
147 | struct usb_gadget *gadget = cdev->gadget; | ||
148 | int status; | ||
149 | |||
150 | if (!can_support_ecm(cdev->gadget)) { | ||
151 | ERROR(cdev, "controller '%s' not usable\n", gadget->name); | ||
152 | return -EINVAL; | ||
153 | } | ||
154 | |||
155 | /* set up network link layer */ | ||
156 | status = gether_setup(cdev->gadget, hostaddr); | ||
157 | if (status < 0) | ||
158 | return status; | ||
159 | |||
160 | /* set up serial link layer */ | ||
161 | status = gserial_setup(cdev->gadget, 1); | ||
162 | if (status < 0) | ||
163 | goto fail0; | ||
164 | |||
165 | gcnum = usb_gadget_controller_number(gadget); | ||
166 | if (gcnum >= 0) | ||
167 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | ||
168 | else { | ||
169 | /* We assume that can_support_ecm() tells the truth; | ||
170 | * but if the controller isn't recognized at all then | ||
171 | * that assumption is a bit more likely to be wrong. | ||
172 | */ | ||
173 | WARN(cdev, "controller '%s' not recognized; trying %s\n", | ||
174 | gadget->name, | ||
175 | cdc_config_driver.label); | ||
176 | device_desc.bcdDevice = | ||
177 | __constant_cpu_to_le16(0x0300 | 0x0099); | ||
178 | } | ||
179 | |||
180 | |||
181 | /* Allocate string descriptor numbers ... note that string | ||
182 | * contents can be overridden by the composite_dev glue. | ||
183 | */ | ||
184 | |||
185 | /* device descriptor strings: manufacturer, product */ | ||
186 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | ||
187 | init_utsname()->sysname, init_utsname()->release, | ||
188 | gadget->name); | ||
189 | status = usb_string_id(cdev); | ||
190 | if (status < 0) | ||
191 | goto fail1; | ||
192 | strings_dev[STRING_MANUFACTURER_IDX].id = status; | ||
193 | device_desc.iManufacturer = status; | ||
194 | |||
195 | status = usb_string_id(cdev); | ||
196 | if (status < 0) | ||
197 | goto fail1; | ||
198 | strings_dev[STRING_PRODUCT_IDX].id = status; | ||
199 | device_desc.iProduct = status; | ||
200 | |||
201 | /* register our configuration */ | ||
202 | status = usb_add_config(cdev, &cdc_config_driver); | ||
203 | if (status < 0) | ||
204 | goto fail1; | ||
205 | |||
206 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC); | ||
207 | |||
208 | return 0; | ||
209 | |||
210 | fail1: | ||
211 | gserial_cleanup(); | ||
212 | fail0: | ||
213 | gether_cleanup(); | ||
214 | return status; | ||
215 | } | ||
216 | |||
217 | static int __exit cdc_unbind(struct usb_composite_dev *cdev) | ||
218 | { | ||
219 | gserial_cleanup(); | ||
220 | gether_cleanup(); | ||
221 | return 0; | ||
222 | } | ||
223 | |||
224 | static struct usb_composite_driver cdc_driver = { | ||
225 | .name = "g_cdc", | ||
226 | .dev = &device_desc, | ||
227 | .strings = dev_strings, | ||
228 | .bind = cdc_bind, | ||
229 | .unbind = __exit_p(cdc_unbind), | ||
230 | }; | ||
231 | |||
232 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
233 | MODULE_AUTHOR("David Brownell"); | ||
234 | MODULE_LICENSE("GPL"); | ||
235 | |||
236 | static int __init init(void) | ||
237 | { | ||
238 | return usb_composite_register(&cdc_driver); | ||
239 | } | ||
240 | module_init(init); | ||
241 | |||
242 | static void __exit cleanup(void) | ||
243 | { | ||
244 | usb_composite_unregister(&cdc_driver); | ||
245 | } | ||
246 | module_exit(cleanup); | ||
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c new file mode 100644 index 000000000000..85c876c1f150 --- /dev/null +++ b/drivers/usb/gadget/composite.c | |||
@@ -0,0 +1,1041 @@ | |||
1 | /* | ||
2 | * composite.c - infrastructure for Composite USB Gadgets | ||
3 | * | ||
4 | * Copyright (C) 2006-2008 David Brownell | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | */ | ||
20 | |||
21 | /* #define VERBOSE_DEBUG */ | ||
22 | |||
23 | #include <linux/kallsyms.h> | ||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/device.h> | ||
27 | |||
28 | #include <linux/usb/composite.h> | ||
29 | |||
30 | |||
31 | /* | ||
32 | * The code in this file is utility code, used to build a gadget driver | ||
33 | * from one or more "function" drivers, one or more "configuration" | ||
34 | * objects, and a "usb_composite_driver" by gluing them together along | ||
35 | * with the relevant device-wide data. | ||
36 | */ | ||
37 | |||
38 | /* big enough to hold our biggest descriptor */ | ||
39 | #define USB_BUFSIZ 512 | ||
40 | |||
41 | static struct usb_composite_driver *composite; | ||
42 | |||
43 | /* Some systems will need runtime overrides for the product identifers | ||
44 | * published in the device descriptor, either numbers or strings or both. | ||
45 | * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). | ||
46 | */ | ||
47 | |||
48 | static ushort idVendor; | ||
49 | module_param(idVendor, ushort, 0); | ||
50 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | ||
51 | |||
52 | static ushort idProduct; | ||
53 | module_param(idProduct, ushort, 0); | ||
54 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | ||
55 | |||
56 | static ushort bcdDevice; | ||
57 | module_param(bcdDevice, ushort, 0); | ||
58 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | ||
59 | |||
60 | static char *iManufacturer; | ||
61 | module_param(iManufacturer, charp, 0); | ||
62 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | ||
63 | |||
64 | static char *iProduct; | ||
65 | module_param(iProduct, charp, 0); | ||
66 | MODULE_PARM_DESC(iProduct, "USB Product string"); | ||
67 | |||
68 | static char *iSerialNumber; | ||
69 | module_param(iSerialNumber, charp, 0); | ||
70 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); | ||
71 | |||
72 | /*-------------------------------------------------------------------------*/ | ||
73 | |||
74 | /** | ||
75 | * usb_add_function() - add a function to a configuration | ||
76 | * @config: the configuration | ||
77 | * @function: the function being added | ||
78 | * Context: single threaded during gadget setup | ||
79 | * | ||
80 | * After initialization, each configuration must have one or more | ||
81 | * functions added to it. Adding a function involves calling its @bind() | ||
82 | * method to allocate resources such as interface and string identifiers | ||
83 | * and endpoints. | ||
84 | * | ||
85 | * This function returns the value of the function's bind(), which is | ||
86 | * zero for success else a negative errno value. | ||
87 | */ | ||
88 | int __init usb_add_function(struct usb_configuration *config, | ||
89 | struct usb_function *function) | ||
90 | { | ||
91 | int value = -EINVAL; | ||
92 | |||
93 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", | ||
94 | function->name, function, | ||
95 | config->label, config); | ||
96 | |||
97 | if (!function->set_alt || !function->disable) | ||
98 | goto done; | ||
99 | |||
100 | function->config = config; | ||
101 | list_add_tail(&function->list, &config->functions); | ||
102 | |||
103 | /* REVISIT *require* function->bind? */ | ||
104 | if (function->bind) { | ||
105 | value = function->bind(config, function); | ||
106 | if (value < 0) { | ||
107 | list_del(&function->list); | ||
108 | function->config = NULL; | ||
109 | } | ||
110 | } else | ||
111 | value = 0; | ||
112 | |||
113 | /* We allow configurations that don't work at both speeds. | ||
114 | * If we run into a lowspeed Linux system, treat it the same | ||
115 | * as full speed ... it's the function drivers that will need | ||
116 | * to avoid bulk and ISO transfers. | ||
117 | */ | ||
118 | if (!config->fullspeed && function->descriptors) | ||
119 | config->fullspeed = true; | ||
120 | if (!config->highspeed && function->hs_descriptors) | ||
121 | config->highspeed = true; | ||
122 | |||
123 | done: | ||
124 | if (value) | ||
125 | DBG(config->cdev, "adding '%s'/%p --> %d\n", | ||
126 | function->name, function, value); | ||
127 | return value; | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * usb_interface_id() - allocate an unused interface ID | ||
132 | * @config: configuration associated with the interface | ||
133 | * @function: function handling the interface | ||
134 | * Context: single threaded during gadget setup | ||
135 | * | ||
136 | * usb_interface_id() is called from usb_function.bind() callbacks to | ||
137 | * allocate new interface IDs. The function driver will then store that | ||
138 | * ID in interface, association, CDC union, and other descriptors. It | ||
139 | * will also handle any control requests targetted at that interface, | ||
140 | * particularly changing its altsetting via set_alt(). There may | ||
141 | * also be class-specific or vendor-specific requests to handle. | ||
142 | * | ||
143 | * All interface identifier should be allocated using this routine, to | ||
144 | * ensure that for example different functions don't wrongly assign | ||
145 | * different meanings to the same identifier. Note that since interface | ||
146 | * identifers are configuration-specific, functions used in more than | ||
147 | * one configuration (or more than once in a given configuration) need | ||
148 | * multiple versions of the relevant descriptors. | ||
149 | * | ||
150 | * Returns the interface ID which was allocated; or -ENODEV if no | ||
151 | * more interface IDs can be allocated. | ||
152 | */ | ||
153 | int __init usb_interface_id(struct usb_configuration *config, | ||
154 | struct usb_function *function) | ||
155 | { | ||
156 | unsigned id = config->next_interface_id; | ||
157 | |||
158 | if (id < MAX_CONFIG_INTERFACES) { | ||
159 | config->interface[id] = function; | ||
160 | config->next_interface_id = id + 1; | ||
161 | return id; | ||
162 | } | ||
163 | return -ENODEV; | ||
164 | } | ||
165 | |||
166 | static int config_buf(struct usb_configuration *config, | ||
167 | enum usb_device_speed speed, void *buf, u8 type) | ||
168 | { | ||
169 | struct usb_config_descriptor *c = buf; | ||
170 | void *next = buf + USB_DT_CONFIG_SIZE; | ||
171 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; | ||
172 | struct usb_function *f; | ||
173 | int status; | ||
174 | |||
175 | /* write the config descriptor */ | ||
176 | c = buf; | ||
177 | c->bLength = USB_DT_CONFIG_SIZE; | ||
178 | c->bDescriptorType = type; | ||
179 | /* wTotalLength is written later */ | ||
180 | c->bNumInterfaces = config->next_interface_id; | ||
181 | c->bConfigurationValue = config->bConfigurationValue; | ||
182 | c->iConfiguration = config->iConfiguration; | ||
183 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; | ||
184 | c->bMaxPower = config->bMaxPower; | ||
185 | |||
186 | /* There may be e.g. OTG descriptors */ | ||
187 | if (config->descriptors) { | ||
188 | status = usb_descriptor_fillbuf(next, len, | ||
189 | config->descriptors); | ||
190 | if (status < 0) | ||
191 | return status; | ||
192 | len -= status; | ||
193 | next += status; | ||
194 | } | ||
195 | |||
196 | /* add each function's descriptors */ | ||
197 | list_for_each_entry(f, &config->functions, list) { | ||
198 | struct usb_descriptor_header **descriptors; | ||
199 | |||
200 | if (speed == USB_SPEED_HIGH) | ||
201 | descriptors = f->hs_descriptors; | ||
202 | else | ||
203 | descriptors = f->descriptors; | ||
204 | if (!descriptors) | ||
205 | continue; | ||
206 | status = usb_descriptor_fillbuf(next, len, | ||
207 | (const struct usb_descriptor_header **) descriptors); | ||
208 | if (status < 0) | ||
209 | return status; | ||
210 | len -= status; | ||
211 | next += status; | ||
212 | } | ||
213 | |||
214 | len = next - buf; | ||
215 | c->wTotalLength = cpu_to_le16(len); | ||
216 | return len; | ||
217 | } | ||
218 | |||
219 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) | ||
220 | { | ||
221 | struct usb_gadget *gadget = cdev->gadget; | ||
222 | struct usb_configuration *c; | ||
223 | u8 type = w_value >> 8; | ||
224 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; | ||
225 | |||
226 | if (gadget_is_dualspeed(gadget)) { | ||
227 | int hs = 0; | ||
228 | |||
229 | if (gadget->speed == USB_SPEED_HIGH) | ||
230 | hs = 1; | ||
231 | if (type == USB_DT_OTHER_SPEED_CONFIG) | ||
232 | hs = !hs; | ||
233 | if (hs) | ||
234 | speed = USB_SPEED_HIGH; | ||
235 | |||
236 | } | ||
237 | |||
238 | /* This is a lookup by config *INDEX* */ | ||
239 | w_value &= 0xff; | ||
240 | list_for_each_entry(c, &cdev->configs, list) { | ||
241 | /* ignore configs that won't work at this speed */ | ||
242 | if (speed == USB_SPEED_HIGH) { | ||
243 | if (!c->highspeed) | ||
244 | continue; | ||
245 | } else { | ||
246 | if (!c->fullspeed) | ||
247 | continue; | ||
248 | } | ||
249 | if (w_value == 0) | ||
250 | return config_buf(c, speed, cdev->req->buf, type); | ||
251 | w_value--; | ||
252 | } | ||
253 | return -EINVAL; | ||
254 | } | ||
255 | |||
256 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) | ||
257 | { | ||
258 | struct usb_gadget *gadget = cdev->gadget; | ||
259 | struct usb_configuration *c; | ||
260 | unsigned count = 0; | ||
261 | int hs = 0; | ||
262 | |||
263 | if (gadget_is_dualspeed(gadget)) { | ||
264 | if (gadget->speed == USB_SPEED_HIGH) | ||
265 | hs = 1; | ||
266 | if (type == USB_DT_DEVICE_QUALIFIER) | ||
267 | hs = !hs; | ||
268 | } | ||
269 | list_for_each_entry(c, &cdev->configs, list) { | ||
270 | /* ignore configs that won't work at this speed */ | ||
271 | if (hs) { | ||
272 | if (!c->highspeed) | ||
273 | continue; | ||
274 | } else { | ||
275 | if (!c->fullspeed) | ||
276 | continue; | ||
277 | } | ||
278 | count++; | ||
279 | } | ||
280 | return count; | ||
281 | } | ||
282 | |||
283 | static void device_qual(struct usb_composite_dev *cdev) | ||
284 | { | ||
285 | struct usb_qualifier_descriptor *qual = cdev->req->buf; | ||
286 | |||
287 | qual->bLength = sizeof(*qual); | ||
288 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; | ||
289 | /* POLICY: same bcdUSB and device type info at both speeds */ | ||
290 | qual->bcdUSB = cdev->desc.bcdUSB; | ||
291 | qual->bDeviceClass = cdev->desc.bDeviceClass; | ||
292 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; | ||
293 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; | ||
294 | /* ASSUME same EP0 fifo size at both speeds */ | ||
295 | qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0; | ||
296 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); | ||
297 | qual->bRESERVED = 0; | ||
298 | } | ||
299 | |||
300 | /*-------------------------------------------------------------------------*/ | ||
301 | |||
302 | static void reset_config(struct usb_composite_dev *cdev) | ||
303 | { | ||
304 | struct usb_function *f; | ||
305 | |||
306 | DBG(cdev, "reset config\n"); | ||
307 | |||
308 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
309 | if (f->disable) | ||
310 | f->disable(f); | ||
311 | } | ||
312 | cdev->config = NULL; | ||
313 | } | ||
314 | |||
315 | static int set_config(struct usb_composite_dev *cdev, | ||
316 | const struct usb_ctrlrequest *ctrl, unsigned number) | ||
317 | { | ||
318 | struct usb_gadget *gadget = cdev->gadget; | ||
319 | struct usb_configuration *c = NULL; | ||
320 | int result = -EINVAL; | ||
321 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; | ||
322 | int tmp; | ||
323 | |||
324 | if (cdev->config) | ||
325 | reset_config(cdev); | ||
326 | |||
327 | if (number) { | ||
328 | list_for_each_entry(c, &cdev->configs, list) { | ||
329 | if (c->bConfigurationValue == number) { | ||
330 | result = 0; | ||
331 | break; | ||
332 | } | ||
333 | } | ||
334 | if (result < 0) | ||
335 | goto done; | ||
336 | } else | ||
337 | result = 0; | ||
338 | |||
339 | INFO(cdev, "%s speed config #%d: %s\n", | ||
340 | ({ char *speed; | ||
341 | switch (gadget->speed) { | ||
342 | case USB_SPEED_LOW: speed = "low"; break; | ||
343 | case USB_SPEED_FULL: speed = "full"; break; | ||
344 | case USB_SPEED_HIGH: speed = "high"; break; | ||
345 | default: speed = "?"; break; | ||
346 | } ; speed; }), number, c ? c->label : "unconfigured"); | ||
347 | |||
348 | if (!c) | ||
349 | goto done; | ||
350 | |||
351 | cdev->config = c; | ||
352 | |||
353 | /* Initialize all interfaces by setting them to altsetting zero. */ | ||
354 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { | ||
355 | struct usb_function *f = c->interface[tmp]; | ||
356 | |||
357 | if (!f) | ||
358 | break; | ||
359 | |||
360 | result = f->set_alt(f, tmp, 0); | ||
361 | if (result < 0) { | ||
362 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", | ||
363 | tmp, f->name, f, result); | ||
364 | |||
365 | reset_config(cdev); | ||
366 | goto done; | ||
367 | } | ||
368 | } | ||
369 | |||
370 | /* when we return, be sure our power usage is valid */ | ||
371 | power = 2 * c->bMaxPower; | ||
372 | done: | ||
373 | usb_gadget_vbus_draw(gadget, power); | ||
374 | return result; | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * usb_add_config() - add a configuration to a device. | ||
379 | * @cdev: wraps the USB gadget | ||
380 | * @config: the configuration, with bConfigurationValue assigned | ||
381 | * Context: single threaded during gadget setup | ||
382 | * | ||
383 | * One of the main tasks of a composite driver's bind() routine is to | ||
384 | * add each of the configurations it supports, using this routine. | ||
385 | * | ||
386 | * This function returns the value of the configuration's bind(), which | ||
387 | * is zero for success else a negative errno value. Binding configurations | ||
388 | * assigns global resources including string IDs, and per-configuration | ||
389 | * resources such as interface IDs and endpoints. | ||
390 | */ | ||
391 | int __init usb_add_config(struct usb_composite_dev *cdev, | ||
392 | struct usb_configuration *config) | ||
393 | { | ||
394 | int status = -EINVAL; | ||
395 | struct usb_configuration *c; | ||
396 | |||
397 | DBG(cdev, "adding config #%u '%s'/%p\n", | ||
398 | config->bConfigurationValue, | ||
399 | config->label, config); | ||
400 | |||
401 | if (!config->bConfigurationValue || !config->bind) | ||
402 | goto done; | ||
403 | |||
404 | /* Prevent duplicate configuration identifiers */ | ||
405 | list_for_each_entry(c, &cdev->configs, list) { | ||
406 | if (c->bConfigurationValue == config->bConfigurationValue) { | ||
407 | status = -EBUSY; | ||
408 | goto done; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | config->cdev = cdev; | ||
413 | list_add_tail(&config->list, &cdev->configs); | ||
414 | |||
415 | INIT_LIST_HEAD(&config->functions); | ||
416 | config->next_interface_id = 0; | ||
417 | |||
418 | status = config->bind(config); | ||
419 | if (status < 0) { | ||
420 | list_del(&config->list); | ||
421 | config->cdev = NULL; | ||
422 | } else { | ||
423 | unsigned i; | ||
424 | |||
425 | DBG(cdev, "cfg %d/%p speeds:%s%s\n", | ||
426 | config->bConfigurationValue, config, | ||
427 | config->highspeed ? " high" : "", | ||
428 | config->fullspeed | ||
429 | ? (gadget_is_dualspeed(cdev->gadget) | ||
430 | ? " full" | ||
431 | : " full/low") | ||
432 | : ""); | ||
433 | |||
434 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { | ||
435 | struct usb_function *f = config->interface[i]; | ||
436 | |||
437 | if (!f) | ||
438 | continue; | ||
439 | DBG(cdev, " interface %d = %s/%p\n", | ||
440 | i, f->name, f); | ||
441 | } | ||
442 | } | ||
443 | |||
444 | /* set_alt(), or next config->bind(), sets up | ||
445 | * ep->driver_data as needed. | ||
446 | */ | ||
447 | usb_ep_autoconfig_reset(cdev->gadget); | ||
448 | |||
449 | done: | ||
450 | if (status) | ||
451 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, | ||
452 | config->bConfigurationValue, status); | ||
453 | return status; | ||
454 | } | ||
455 | |||
456 | /*-------------------------------------------------------------------------*/ | ||
457 | |||
458 | /* We support strings in multiple languages ... string descriptor zero | ||
459 | * says which languages are supported. The typical case will be that | ||
460 | * only one language (probably English) is used, with I18N handled on | ||
461 | * the host side. | ||
462 | */ | ||
463 | |||
464 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) | ||
465 | { | ||
466 | const struct usb_gadget_strings *s; | ||
467 | u16 language; | ||
468 | __le16 *tmp; | ||
469 | |||
470 | while (*sp) { | ||
471 | s = *sp; | ||
472 | language = cpu_to_le16(s->language); | ||
473 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { | ||
474 | if (*tmp == language) | ||
475 | goto repeat; | ||
476 | } | ||
477 | *tmp++ = language; | ||
478 | repeat: | ||
479 | sp++; | ||
480 | } | ||
481 | } | ||
482 | |||
483 | static int lookup_string( | ||
484 | struct usb_gadget_strings **sp, | ||
485 | void *buf, | ||
486 | u16 language, | ||
487 | int id | ||
488 | ) | ||
489 | { | ||
490 | struct usb_gadget_strings *s; | ||
491 | int value; | ||
492 | |||
493 | while (*sp) { | ||
494 | s = *sp++; | ||
495 | if (s->language != language) | ||
496 | continue; | ||
497 | value = usb_gadget_get_string(s, id, buf); | ||
498 | if (value > 0) | ||
499 | return value; | ||
500 | } | ||
501 | return -EINVAL; | ||
502 | } | ||
503 | |||
504 | static int get_string(struct usb_composite_dev *cdev, | ||
505 | void *buf, u16 language, int id) | ||
506 | { | ||
507 | struct usb_configuration *c; | ||
508 | struct usb_function *f; | ||
509 | int len; | ||
510 | |||
511 | /* Yes, not only is USB's I18N support probably more than most | ||
512 | * folk will ever care about ... also, it's all supported here. | ||
513 | * (Except for UTF8 support for Unicode's "Astral Planes".) | ||
514 | */ | ||
515 | |||
516 | /* 0 == report all available language codes */ | ||
517 | if (id == 0) { | ||
518 | struct usb_string_descriptor *s = buf; | ||
519 | struct usb_gadget_strings **sp; | ||
520 | |||
521 | memset(s, 0, 256); | ||
522 | s->bDescriptorType = USB_DT_STRING; | ||
523 | |||
524 | sp = composite->strings; | ||
525 | if (sp) | ||
526 | collect_langs(sp, s->wData); | ||
527 | |||
528 | list_for_each_entry(c, &cdev->configs, list) { | ||
529 | sp = c->strings; | ||
530 | if (sp) | ||
531 | collect_langs(sp, s->wData); | ||
532 | |||
533 | list_for_each_entry(f, &c->functions, list) { | ||
534 | sp = f->strings; | ||
535 | if (sp) | ||
536 | collect_langs(sp, s->wData); | ||
537 | } | ||
538 | } | ||
539 | |||
540 | for (len = 0; s->wData[len] && len <= 126; len++) | ||
541 | continue; | ||
542 | if (!len) | ||
543 | return -EINVAL; | ||
544 | |||
545 | s->bLength = 2 * (len + 1); | ||
546 | return s->bLength; | ||
547 | } | ||
548 | |||
549 | /* Otherwise, look up and return a specified string. String IDs | ||
550 | * are device-scoped, so we look up each string table we're told | ||
551 | * about. These lookups are infrequent; simpler-is-better here. | ||
552 | */ | ||
553 | if (composite->strings) { | ||
554 | len = lookup_string(composite->strings, buf, language, id); | ||
555 | if (len > 0) | ||
556 | return len; | ||
557 | } | ||
558 | list_for_each_entry(c, &cdev->configs, list) { | ||
559 | if (c->strings) { | ||
560 | len = lookup_string(c->strings, buf, language, id); | ||
561 | if (len > 0) | ||
562 | return len; | ||
563 | } | ||
564 | list_for_each_entry(f, &c->functions, list) { | ||
565 | if (!f->strings) | ||
566 | continue; | ||
567 | len = lookup_string(f->strings, buf, language, id); | ||
568 | if (len > 0) | ||
569 | return len; | ||
570 | } | ||
571 | } | ||
572 | return -EINVAL; | ||
573 | } | ||
574 | |||
575 | /** | ||
576 | * usb_string_id() - allocate an unused string ID | ||
577 | * @cdev: the device whose string descriptor IDs are being allocated | ||
578 | * Context: single threaded during gadget setup | ||
579 | * | ||
580 | * @usb_string_id() is called from bind() callbacks to allocate | ||
581 | * string IDs. Drivers for functions, configurations, or gadgets will | ||
582 | * then store that ID in the appropriate descriptors and string table. | ||
583 | * | ||
584 | * All string identifier should be allocated using this routine, to | ||
585 | * ensure that for example different functions don't wrongly assign | ||
586 | * different meanings to the same identifier. | ||
587 | */ | ||
588 | int __init usb_string_id(struct usb_composite_dev *cdev) | ||
589 | { | ||
590 | if (cdev->next_string_id < 254) { | ||
591 | /* string id 0 is reserved */ | ||
592 | cdev->next_string_id++; | ||
593 | return cdev->next_string_id; | ||
594 | } | ||
595 | return -ENODEV; | ||
596 | } | ||
597 | |||
598 | /*-------------------------------------------------------------------------*/ | ||
599 | |||
600 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) | ||
601 | { | ||
602 | if (req->status || req->actual != req->length) | ||
603 | DBG((struct usb_composite_dev *) ep->driver_data, | ||
604 | "setup complete --> %d, %d/%d\n", | ||
605 | req->status, req->actual, req->length); | ||
606 | } | ||
607 | |||
608 | /* | ||
609 | * The setup() callback implements all the ep0 functionality that's | ||
610 | * not handled lower down, in hardware or the hardware driver(like | ||
611 | * device and endpoint feature flags, and their status). It's all | ||
612 | * housekeeping for the gadget function we're implementing. Most of | ||
613 | * the work is in config and function specific setup. | ||
614 | */ | ||
615 | static int | ||
616 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | ||
617 | { | ||
618 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
619 | struct usb_request *req = cdev->req; | ||
620 | int value = -EOPNOTSUPP; | ||
621 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
622 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
623 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
624 | struct usb_function *f = NULL; | ||
625 | |||
626 | /* partial re-init of the response message; the function or the | ||
627 | * gadget might need to intercept e.g. a control-OUT completion | ||
628 | * when we delegate to it. | ||
629 | */ | ||
630 | req->zero = 0; | ||
631 | req->complete = composite_setup_complete; | ||
632 | req->length = USB_BUFSIZ; | ||
633 | gadget->ep0->driver_data = cdev; | ||
634 | |||
635 | switch (ctrl->bRequest) { | ||
636 | |||
637 | /* we handle all standard USB descriptors */ | ||
638 | case USB_REQ_GET_DESCRIPTOR: | ||
639 | if (ctrl->bRequestType != USB_DIR_IN) | ||
640 | goto unknown; | ||
641 | switch (w_value >> 8) { | ||
642 | |||
643 | case USB_DT_DEVICE: | ||
644 | cdev->desc.bNumConfigurations = | ||
645 | count_configs(cdev, USB_DT_DEVICE); | ||
646 | value = min(w_length, (u16) sizeof cdev->desc); | ||
647 | memcpy(req->buf, &cdev->desc, value); | ||
648 | break; | ||
649 | case USB_DT_DEVICE_QUALIFIER: | ||
650 | if (!gadget_is_dualspeed(gadget)) | ||
651 | break; | ||
652 | device_qual(cdev); | ||
653 | value = min_t(int, w_length, | ||
654 | sizeof(struct usb_qualifier_descriptor)); | ||
655 | break; | ||
656 | case USB_DT_OTHER_SPEED_CONFIG: | ||
657 | if (!gadget_is_dualspeed(gadget)) | ||
658 | break; | ||
659 | /* FALLTHROUGH */ | ||
660 | case USB_DT_CONFIG: | ||
661 | value = config_desc(cdev, w_value); | ||
662 | if (value >= 0) | ||
663 | value = min(w_length, (u16) value); | ||
664 | break; | ||
665 | case USB_DT_STRING: | ||
666 | value = get_string(cdev, req->buf, | ||
667 | w_index, w_value & 0xff); | ||
668 | if (value >= 0) | ||
669 | value = min(w_length, (u16) value); | ||
670 | break; | ||
671 | } | ||
672 | break; | ||
673 | |||
674 | /* any number of configs can work */ | ||
675 | case USB_REQ_SET_CONFIGURATION: | ||
676 | if (ctrl->bRequestType != 0) | ||
677 | goto unknown; | ||
678 | if (gadget_is_otg(gadget)) { | ||
679 | if (gadget->a_hnp_support) | ||
680 | DBG(cdev, "HNP available\n"); | ||
681 | else if (gadget->a_alt_hnp_support) | ||
682 | DBG(cdev, "HNP on another port\n"); | ||
683 | else | ||
684 | VDBG(cdev, "HNP inactive\n"); | ||
685 | } | ||
686 | spin_lock(&cdev->lock); | ||
687 | value = set_config(cdev, ctrl, w_value); | ||
688 | spin_unlock(&cdev->lock); | ||
689 | break; | ||
690 | case USB_REQ_GET_CONFIGURATION: | ||
691 | if (ctrl->bRequestType != USB_DIR_IN) | ||
692 | goto unknown; | ||
693 | if (cdev->config) | ||
694 | *(u8 *)req->buf = cdev->config->bConfigurationValue; | ||
695 | else | ||
696 | *(u8 *)req->buf = 0; | ||
697 | value = min(w_length, (u16) 1); | ||
698 | break; | ||
699 | |||
700 | /* function drivers must handle get/set altsetting; if there's | ||
701 | * no get() method, we know only altsetting zero works. | ||
702 | */ | ||
703 | case USB_REQ_SET_INTERFACE: | ||
704 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) | ||
705 | goto unknown; | ||
706 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) | ||
707 | break; | ||
708 | f = cdev->config->interface[w_index]; | ||
709 | if (!f) | ||
710 | break; | ||
711 | if (w_value && !f->get_alt) | ||
712 | break; | ||
713 | value = f->set_alt(f, w_index, w_value); | ||
714 | break; | ||
715 | case USB_REQ_GET_INTERFACE: | ||
716 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) | ||
717 | goto unknown; | ||
718 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) | ||
719 | break; | ||
720 | f = cdev->config->interface[w_index]; | ||
721 | if (!f) | ||
722 | break; | ||
723 | /* lots of interfaces only need altsetting zero... */ | ||
724 | value = f->get_alt ? f->get_alt(f, w_index) : 0; | ||
725 | if (value < 0) | ||
726 | break; | ||
727 | *((u8 *)req->buf) = value; | ||
728 | value = min(w_length, (u16) 1); | ||
729 | break; | ||
730 | default: | ||
731 | unknown: | ||
732 | VDBG(cdev, | ||
733 | "non-core control req%02x.%02x v%04x i%04x l%d\n", | ||
734 | ctrl->bRequestType, ctrl->bRequest, | ||
735 | w_value, w_index, w_length); | ||
736 | |||
737 | /* functions always handle their interfaces ... punt other | ||
738 | * recipients (endpoint, other, WUSB, ...) to the current | ||
739 | * configuration code. | ||
740 | * | ||
741 | * REVISIT it could make sense to let the composite device | ||
742 | * take such requests too, if that's ever needed: to work | ||
743 | * in config 0, etc. | ||
744 | */ | ||
745 | if ((ctrl->bRequestType & USB_RECIP_MASK) | ||
746 | == USB_RECIP_INTERFACE) { | ||
747 | f = cdev->config->interface[w_index]; | ||
748 | if (f && f->setup) | ||
749 | value = f->setup(f, ctrl); | ||
750 | else | ||
751 | f = NULL; | ||
752 | } | ||
753 | if (value < 0 && !f) { | ||
754 | struct usb_configuration *c; | ||
755 | |||
756 | c = cdev->config; | ||
757 | if (c && c->setup) | ||
758 | value = c->setup(c, ctrl); | ||
759 | } | ||
760 | |||
761 | goto done; | ||
762 | } | ||
763 | |||
764 | /* respond with data transfer before status phase? */ | ||
765 | if (value >= 0) { | ||
766 | req->length = value; | ||
767 | req->zero = value < w_length; | ||
768 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | ||
769 | if (value < 0) { | ||
770 | DBG(cdev, "ep_queue --> %d\n", value); | ||
771 | req->status = 0; | ||
772 | composite_setup_complete(gadget->ep0, req); | ||
773 | } | ||
774 | } | ||
775 | |||
776 | done: | ||
777 | /* device either stalls (value < 0) or reports success */ | ||
778 | return value; | ||
779 | } | ||
780 | |||
781 | static void composite_disconnect(struct usb_gadget *gadget) | ||
782 | { | ||
783 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
784 | unsigned long flags; | ||
785 | |||
786 | /* REVISIT: should we have config and device level | ||
787 | * disconnect callbacks? | ||
788 | */ | ||
789 | spin_lock_irqsave(&cdev->lock, flags); | ||
790 | if (cdev->config) | ||
791 | reset_config(cdev); | ||
792 | spin_unlock_irqrestore(&cdev->lock, flags); | ||
793 | } | ||
794 | |||
795 | /*-------------------------------------------------------------------------*/ | ||
796 | |||
797 | static void /* __init_or_exit */ | ||
798 | composite_unbind(struct usb_gadget *gadget) | ||
799 | { | ||
800 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
801 | |||
802 | /* composite_disconnect() must already have been called | ||
803 | * by the underlying peripheral controller driver! | ||
804 | * so there's no i/o concurrency that could affect the | ||
805 | * state protected by cdev->lock. | ||
806 | */ | ||
807 | WARN_ON(cdev->config); | ||
808 | |||
809 | while (!list_empty(&cdev->configs)) { | ||
810 | struct usb_configuration *c; | ||
811 | |||
812 | c = list_first_entry(&cdev->configs, | ||
813 | struct usb_configuration, list); | ||
814 | while (!list_empty(&c->functions)) { | ||
815 | struct usb_function *f; | ||
816 | |||
817 | f = list_first_entry(&c->functions, | ||
818 | struct usb_function, list); | ||
819 | list_del(&f->list); | ||
820 | if (f->unbind) { | ||
821 | DBG(cdev, "unbind function '%s'/%p\n", | ||
822 | f->name, f); | ||
823 | f->unbind(c, f); | ||
824 | /* may free memory for "f" */ | ||
825 | } | ||
826 | } | ||
827 | list_del(&c->list); | ||
828 | if (c->unbind) { | ||
829 | DBG(cdev, "unbind config '%s'/%p\n", c->label, c); | ||
830 | c->unbind(c); | ||
831 | /* may free memory for "c" */ | ||
832 | } | ||
833 | } | ||
834 | if (composite->unbind) | ||
835 | composite->unbind(cdev); | ||
836 | |||
837 | if (cdev->req) { | ||
838 | kfree(cdev->req->buf); | ||
839 | usb_ep_free_request(gadget->ep0, cdev->req); | ||
840 | } | ||
841 | kfree(cdev); | ||
842 | set_gadget_data(gadget, NULL); | ||
843 | composite = NULL; | ||
844 | } | ||
845 | |||
846 | static void __init | ||
847 | string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s) | ||
848 | { | ||
849 | struct usb_string *str = tab->strings; | ||
850 | |||
851 | for (str = tab->strings; str->s; str++) { | ||
852 | if (str->id == id) { | ||
853 | str->s = s; | ||
854 | return; | ||
855 | } | ||
856 | } | ||
857 | } | ||
858 | |||
859 | static void __init | ||
860 | string_override(struct usb_gadget_strings **tab, u8 id, const char *s) | ||
861 | { | ||
862 | while (*tab) { | ||
863 | string_override_one(*tab, id, s); | ||
864 | tab++; | ||
865 | } | ||
866 | } | ||
867 | |||
868 | static int __init composite_bind(struct usb_gadget *gadget) | ||
869 | { | ||
870 | struct usb_composite_dev *cdev; | ||
871 | int status = -ENOMEM; | ||
872 | |||
873 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); | ||
874 | if (!cdev) | ||
875 | return status; | ||
876 | |||
877 | spin_lock_init(&cdev->lock); | ||
878 | cdev->gadget = gadget; | ||
879 | set_gadget_data(gadget, cdev); | ||
880 | INIT_LIST_HEAD(&cdev->configs); | ||
881 | |||
882 | /* preallocate control response and buffer */ | ||
883 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); | ||
884 | if (!cdev->req) | ||
885 | goto fail; | ||
886 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); | ||
887 | if (!cdev->req->buf) | ||
888 | goto fail; | ||
889 | cdev->req->complete = composite_setup_complete; | ||
890 | gadget->ep0->driver_data = cdev; | ||
891 | |||
892 | cdev->bufsiz = USB_BUFSIZ; | ||
893 | cdev->driver = composite; | ||
894 | |||
895 | usb_gadget_set_selfpowered(gadget); | ||
896 | |||
897 | /* interface and string IDs start at zero via kzalloc. | ||
898 | * we force endpoints to start unassigned; few controller | ||
899 | * drivers will zero ep->driver_data. | ||
900 | */ | ||
901 | usb_ep_autoconfig_reset(cdev->gadget); | ||
902 | |||
903 | /* composite gadget needs to assign strings for whole device (like | ||
904 | * serial number), register function drivers, potentially update | ||
905 | * power state and consumption, etc | ||
906 | */ | ||
907 | status = composite->bind(cdev); | ||
908 | if (status < 0) | ||
909 | goto fail; | ||
910 | |||
911 | cdev->desc = *composite->dev; | ||
912 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | ||
913 | |||
914 | /* standardized runtime overrides for device ID data */ | ||
915 | if (idVendor) | ||
916 | cdev->desc.idVendor = cpu_to_le16(idVendor); | ||
917 | if (idProduct) | ||
918 | cdev->desc.idProduct = cpu_to_le16(idProduct); | ||
919 | if (bcdDevice) | ||
920 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); | ||
921 | |||
922 | /* strings can't be assigned before bind() allocates the | ||
923 | * releavnt identifiers | ||
924 | */ | ||
925 | if (cdev->desc.iManufacturer && iManufacturer) | ||
926 | string_override(composite->strings, | ||
927 | cdev->desc.iManufacturer, iManufacturer); | ||
928 | if (cdev->desc.iProduct && iProduct) | ||
929 | string_override(composite->strings, | ||
930 | cdev->desc.iProduct, iProduct); | ||
931 | if (cdev->desc.iSerialNumber && iSerialNumber) | ||
932 | string_override(composite->strings, | ||
933 | cdev->desc.iSerialNumber, iSerialNumber); | ||
934 | |||
935 | INFO(cdev, "%s ready\n", composite->name); | ||
936 | return 0; | ||
937 | |||
938 | fail: | ||
939 | composite_unbind(gadget); | ||
940 | return status; | ||
941 | } | ||
942 | |||
943 | /*-------------------------------------------------------------------------*/ | ||
944 | |||
945 | static void | ||
946 | composite_suspend(struct usb_gadget *gadget) | ||
947 | { | ||
948 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
949 | struct usb_function *f; | ||
950 | |||
951 | /* REVISIT: should we have config and device level | ||
952 | * suspend/resume callbacks? | ||
953 | */ | ||
954 | DBG(cdev, "suspend\n"); | ||
955 | if (cdev->config) { | ||
956 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
957 | if (f->suspend) | ||
958 | f->suspend(f); | ||
959 | } | ||
960 | } | ||
961 | } | ||
962 | |||
963 | static void | ||
964 | composite_resume(struct usb_gadget *gadget) | ||
965 | { | ||
966 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
967 | struct usb_function *f; | ||
968 | |||
969 | /* REVISIT: should we have config and device level | ||
970 | * suspend/resume callbacks? | ||
971 | */ | ||
972 | DBG(cdev, "resume\n"); | ||
973 | if (cdev->config) { | ||
974 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
975 | if (f->resume) | ||
976 | f->resume(f); | ||
977 | } | ||
978 | } | ||
979 | } | ||
980 | |||
981 | /*-------------------------------------------------------------------------*/ | ||
982 | |||
983 | static struct usb_gadget_driver composite_driver = { | ||
984 | .speed = USB_SPEED_HIGH, | ||
985 | |||
986 | .bind = composite_bind, | ||
987 | .unbind = __exit_p(composite_unbind), | ||
988 | |||
989 | .setup = composite_setup, | ||
990 | .disconnect = composite_disconnect, | ||
991 | |||
992 | .suspend = composite_suspend, | ||
993 | .resume = composite_resume, | ||
994 | |||
995 | .driver = { | ||
996 | .owner = THIS_MODULE, | ||
997 | }, | ||
998 | }; | ||
999 | |||
1000 | /** | ||
1001 | * usb_composite_register() - register a composite driver | ||
1002 | * @driver: the driver to register | ||
1003 | * Context: single threaded during gadget setup | ||
1004 | * | ||
1005 | * This function is used to register drivers using the composite driver | ||
1006 | * framework. The return value is zero, or a negative errno value. | ||
1007 | * Those values normally come from the driver's @bind method, which does | ||
1008 | * all the work of setting up the driver to match the hardware. | ||
1009 | * | ||
1010 | * On successful return, the gadget is ready to respond to requests from | ||
1011 | * the host, unless one of its components invokes usb_gadget_disconnect() | ||
1012 | * while it was binding. That would usually be done in order to wait for | ||
1013 | * some userspace participation. | ||
1014 | */ | ||
1015 | int __init usb_composite_register(struct usb_composite_driver *driver) | ||
1016 | { | ||
1017 | if (!driver || !driver->dev || !driver->bind || composite) | ||
1018 | return -EINVAL; | ||
1019 | |||
1020 | if (!driver->name) | ||
1021 | driver->name = "composite"; | ||
1022 | composite_driver.function = (char *) driver->name; | ||
1023 | composite_driver.driver.name = driver->name; | ||
1024 | composite = driver; | ||
1025 | |||
1026 | return usb_gadget_register_driver(&composite_driver); | ||
1027 | } | ||
1028 | |||
1029 | /** | ||
1030 | * usb_composite_unregister() - unregister a composite driver | ||
1031 | * @driver: the driver to unregister | ||
1032 | * | ||
1033 | * This function is used to unregister drivers using the composite | ||
1034 | * driver framework. | ||
1035 | */ | ||
1036 | void __exit usb_composite_unregister(struct usb_composite_driver *driver) | ||
1037 | { | ||
1038 | if (composite != driver) | ||
1039 | return; | ||
1040 | usb_gadget_unregister_driver(&composite_driver); | ||
1041 | } | ||
diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c index a4e54b2743f0..1ca1c326392a 100644 --- a/drivers/usb/gadget/config.c +++ b/drivers/usb/gadget/config.c | |||
@@ -96,7 +96,7 @@ int usb_gadget_config_buf( | |||
96 | /* config descriptor first */ | 96 | /* config descriptor first */ |
97 | if (length < USB_DT_CONFIG_SIZE || !desc) | 97 | if (length < USB_DT_CONFIG_SIZE || !desc) |
98 | return -EINVAL; | 98 | return -EINVAL; |
99 | *cp = *config; | 99 | *cp = *config; |
100 | 100 | ||
101 | /* then interface/endpoint/class/vendor/... */ | 101 | /* then interface/endpoint/class/vendor/... */ |
102 | len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, | 102 | len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, |
@@ -115,3 +115,77 @@ int usb_gadget_config_buf( | |||
115 | return len; | 115 | return len; |
116 | } | 116 | } |
117 | 117 | ||
118 | /** | ||
119 | * usb_copy_descriptors - copy a vector of USB descriptors | ||
120 | * @src: null-terminated vector to copy | ||
121 | * Context: initialization code, which may sleep | ||
122 | * | ||
123 | * This makes a copy of a vector of USB descriptors. Its primary use | ||
124 | * is to support usb_function objects which can have multiple copies, | ||
125 | * each needing different descriptors. Functions may have static | ||
126 | * tables of descriptors, which are used as templates and customized | ||
127 | * with identifiers (for interfaces, strings, endpoints, and more) | ||
128 | * as needed by a given function instance. | ||
129 | */ | ||
130 | struct usb_descriptor_header **__init | ||
131 | usb_copy_descriptors(struct usb_descriptor_header **src) | ||
132 | { | ||
133 | struct usb_descriptor_header **tmp; | ||
134 | unsigned bytes; | ||
135 | unsigned n_desc; | ||
136 | void *mem; | ||
137 | struct usb_descriptor_header **ret; | ||
138 | |||
139 | /* count descriptors and their sizes; then add vector size */ | ||
140 | for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++) | ||
141 | bytes += (*tmp)->bLength; | ||
142 | bytes += (n_desc + 1) * sizeof(*tmp); | ||
143 | |||
144 | mem = kmalloc(bytes, GFP_KERNEL); | ||
145 | if (!mem) | ||
146 | return NULL; | ||
147 | |||
148 | /* fill in pointers starting at "tmp", | ||
149 | * to descriptors copied starting at "mem"; | ||
150 | * and return "ret" | ||
151 | */ | ||
152 | tmp = mem; | ||
153 | ret = mem; | ||
154 | mem += (n_desc + 1) * sizeof(*tmp); | ||
155 | while (*src) { | ||
156 | memcpy(mem, *src, (*src)->bLength); | ||
157 | *tmp = mem; | ||
158 | tmp++; | ||
159 | mem += (*src)->bLength; | ||
160 | src++; | ||
161 | } | ||
162 | *tmp = NULL; | ||
163 | |||
164 | return ret; | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * usb_find_endpoint - find a copy of an endpoint descriptor | ||
169 | * @src: original vector of descriptors | ||
170 | * @copy: copy of @src | ||
171 | * @ep: endpoint descriptor found in @src | ||
172 | * | ||
173 | * This returns the copy of the @match descriptor made for @copy. Its | ||
174 | * intended use is to help remembering the endpoint descriptor to use | ||
175 | * when enabling a given endpoint. | ||
176 | */ | ||
177 | struct usb_endpoint_descriptor *__init | ||
178 | usb_find_endpoint( | ||
179 | struct usb_descriptor_header **src, | ||
180 | struct usb_descriptor_header **copy, | ||
181 | struct usb_endpoint_descriptor *match | ||
182 | ) | ||
183 | { | ||
184 | while (*src) { | ||
185 | if (*src == (void *) match) | ||
186 | return (void *)*copy; | ||
187 | src++; | ||
188 | copy++; | ||
189 | } | ||
190 | return NULL; | ||
191 | } | ||
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c index 42036192a03c..21d1406af9ee 100644 --- a/drivers/usb/gadget/dummy_hcd.c +++ b/drivers/usb/gadget/dummy_hcd.c | |||
@@ -862,7 +862,7 @@ static int dummy_udc_probe (struct platform_device *pdev) | |||
862 | /* maybe claim OTG support, though we won't complete HNP */ | 862 | /* maybe claim OTG support, though we won't complete HNP */ |
863 | dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0); | 863 | dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0); |
864 | 864 | ||
865 | strcpy (dum->gadget.dev.bus_id, "gadget"); | 865 | dev_set_name(&dum->gadget.dev, "gadget"); |
866 | dum->gadget.dev.parent = &pdev->dev; | 866 | dum->gadget.dev.parent = &pdev->dev; |
867 | dum->gadget.dev.release = dummy_gadget_release; | 867 | dum->gadget.dev.release = dummy_gadget_release; |
868 | rc = device_register (&dum->gadget.dev); | 868 | rc = device_register (&dum->gadget.dev); |
@@ -1865,7 +1865,7 @@ static int dummy_hcd_probe(struct platform_device *pdev) | |||
1865 | 1865 | ||
1866 | dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); | 1866 | dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); |
1867 | 1867 | ||
1868 | hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id); | 1868 | hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev)); |
1869 | if (!hcd) | 1869 | if (!hcd) |
1870 | return -ENOMEM; | 1870 | return -ENOMEM; |
1871 | the_controller = hcd_to_dummy (hcd); | 1871 | the_controller = hcd_to_dummy (hcd); |
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c index 8bdad221fa91..9462e30192d8 100644 --- a/drivers/usb/gadget/epautoconf.c +++ b/drivers/usb/gadget/epautoconf.c | |||
@@ -159,6 +159,7 @@ ep_matches ( | |||
159 | /* MATCH!! */ | 159 | /* MATCH!! */ |
160 | 160 | ||
161 | /* report address */ | 161 | /* report address */ |
162 | desc->bEndpointAddress &= USB_DIR_IN; | ||
162 | if (isdigit (ep->name [2])) { | 163 | if (isdigit (ep->name [2])) { |
163 | u8 num = simple_strtol (&ep->name [2], NULL, 10); | 164 | u8 num = simple_strtol (&ep->name [2], NULL, 10); |
164 | desc->bEndpointAddress |= num; | 165 | desc->bEndpointAddress |= num; |
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c index 4ce3950b997f..d7aaaa29b1e1 100644 --- a/drivers/usb/gadget/ether.c +++ b/drivers/usb/gadget/ether.c | |||
@@ -1,8 +1,9 @@ | |||
1 | /* | 1 | /* |
2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options | 2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options |
3 | * | 3 | * |
4 | * Copyright (C) 2003-2005 David Brownell | 4 | * Copyright (C) 2003-2005,2008 David Brownell |
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
6 | * Copyright (C) 2008 Nokia Corporation | ||
6 | * | 7 | * |
7 | * This program is free software; you can redistribute it and/or modify | 8 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | 9 | * it under the terms of the GNU General Public License as published by |
@@ -23,18 +24,9 @@ | |||
23 | 24 | ||
24 | #include <linux/kernel.h> | 25 | #include <linux/kernel.h> |
25 | #include <linux/utsname.h> | 26 | #include <linux/utsname.h> |
26 | #include <linux/device.h> | ||
27 | #include <linux/ctype.h> | ||
28 | #include <linux/etherdevice.h> | ||
29 | #include <linux/ethtool.h> | ||
30 | 27 | ||
31 | #include <linux/usb/ch9.h> | 28 | #include "u_ether.h" |
32 | #include <linux/usb/cdc.h> | ||
33 | #include <linux/usb/gadget.h> | ||
34 | 29 | ||
35 | #include "gadget_chips.h" | ||
36 | |||
37 | /*-------------------------------------------------------------------------*/ | ||
38 | 30 | ||
39 | /* | 31 | /* |
40 | * Ethernet gadget driver -- with CDC and non-CDC options | 32 | * Ethernet gadget driver -- with CDC and non-CDC options |
@@ -46,7 +38,11 @@ | |||
46 | * this USB-IF standard as its open-systems interoperability solution; | 38 | * this USB-IF standard as its open-systems interoperability solution; |
47 | * most host side USB stacks (except from Microsoft) support it. | 39 | * most host side USB stacks (except from Microsoft) support it. |
48 | * | 40 | * |
49 | * There's some hardware that can't talk CDC. We make that hardware | 41 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support |
42 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new | ||
43 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. | ||
44 | * | ||
45 | * There's some hardware that can't talk CDC ECM. We make that hardware | ||
50 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but | 46 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
51 | * link-level setup only requires activating the configuration. Only the | 47 | * link-level setup only requires activating the configuration. Only the |
52 | * endpoint descriptors, and product/vendor IDs, are relevant; no control | 48 | * endpoint descriptors, and product/vendor IDs, are relevant; no control |
@@ -64,70 +60,40 @@ | |||
64 | * A third option is also in use. Rather than CDC Ethernet, or something | 60 | * A third option is also in use. Rather than CDC Ethernet, or something |
65 | * simpler, Microsoft pushes their own approach: RNDIS. The published | 61 | * simpler, Microsoft pushes their own approach: RNDIS. The published |
66 | * RNDIS specs are ambiguous and appear to be incomplete, and are also | 62 | * RNDIS specs are ambiguous and appear to be incomplete, and are also |
67 | * needlessly complex. | 63 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
68 | */ | 64 | */ |
69 | 65 | ||
70 | #define DRIVER_DESC "Ethernet Gadget" | 66 | #define DRIVER_DESC "Ethernet Gadget" |
71 | #define DRIVER_VERSION "May Day 2005" | 67 | #define DRIVER_VERSION "Memorial Day 2008" |
72 | |||
73 | static const char shortname [] = "ether"; | ||
74 | static const char driver_desc [] = DRIVER_DESC; | ||
75 | |||
76 | #define RX_EXTRA 20 /* guard against rx overflows */ | ||
77 | |||
78 | #include "rndis.h" | ||
79 | 68 | ||
80 | #ifndef CONFIG_USB_ETH_RNDIS | 69 | #ifdef CONFIG_USB_ETH_RNDIS |
81 | #define rndis_uninit(x) do{}while(0) | 70 | #define PREFIX "RNDIS/" |
82 | #define rndis_deregister(c) do{}while(0) | 71 | #else |
83 | #define rndis_exit() do{}while(0) | 72 | #define PREFIX "" |
84 | #endif | 73 | #endif |
85 | 74 | ||
86 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ | 75 | /* |
87 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ | 76 | * This driver aims for interoperability by using CDC ECM unless |
88 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ | 77 | * |
89 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ | 78 | * can_support_ecm() |
90 | |USB_CDC_PACKET_TYPE_DIRECTED) | 79 | * |
91 | 80 | * returns false, in which case it supports the CDC Subset. By default, | |
92 | 81 | * that returns true; most hardware has no problems with CDC ECM, that's | |
93 | /*-------------------------------------------------------------------------*/ | 82 | * a good default. Previous versions of this driver had no default; this |
94 | 83 | * version changes that, removing overhead for new controller support. | |
95 | struct eth_dev { | ||
96 | spinlock_t lock; | ||
97 | struct usb_gadget *gadget; | ||
98 | struct usb_request *req; /* for control responses */ | ||
99 | struct usb_request *stat_req; /* for cdc & rndis status */ | ||
100 | |||
101 | u8 config; | ||
102 | struct usb_ep *in_ep, *out_ep, *status_ep; | ||
103 | const struct usb_endpoint_descriptor | ||
104 | *in, *out, *status; | ||
105 | |||
106 | spinlock_t req_lock; | ||
107 | struct list_head tx_reqs, rx_reqs; | ||
108 | |||
109 | struct net_device *net; | ||
110 | struct net_device_stats stats; | ||
111 | atomic_t tx_qlen; | ||
112 | |||
113 | struct work_struct work; | ||
114 | unsigned zlp:1; | ||
115 | unsigned cdc:1; | ||
116 | unsigned rndis:1; | ||
117 | unsigned suspended:1; | ||
118 | u16 cdc_filter; | ||
119 | unsigned long todo; | ||
120 | #define WORK_RX_MEMORY 0 | ||
121 | int rndis_config; | ||
122 | u8 host_mac [ETH_ALEN]; | ||
123 | }; | ||
124 | |||
125 | /* This version autoconfigures as much as possible at run-time. | ||
126 | * | 84 | * |
127 | * It also ASSUMES a self-powered device, without remote wakeup, | 85 | * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE! |
128 | * although remote wakeup support would make sense. | ||
129 | */ | 86 | */ |
130 | 87 | ||
88 | static inline bool has_rndis(void) | ||
89 | { | ||
90 | #ifdef CONFIG_USB_ETH_RNDIS | ||
91 | return true; | ||
92 | #else | ||
93 | return false; | ||
94 | #endif | ||
95 | } | ||
96 | |||
131 | /*-------------------------------------------------------------------------*/ | 97 | /*-------------------------------------------------------------------------*/ |
132 | 98 | ||
133 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | 99 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
@@ -137,8 +103,8 @@ struct eth_dev { | |||
137 | /* Thanks to NetChip Technologies for donating this product ID. | 103 | /* Thanks to NetChip Technologies for donating this product ID. |
138 | * It's for devices with only CDC Ethernet configurations. | 104 | * It's for devices with only CDC Ethernet configurations. |
139 | */ | 105 | */ |
140 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | 106 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
141 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ | 107 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
142 | 108 | ||
143 | /* For hardware that can't talk CDC, we use the same vendor ID that | 109 | /* For hardware that can't talk CDC, we use the same vendor ID that |
144 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and | 110 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
@@ -162,274 +128,9 @@ struct eth_dev { | |||
162 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ | 128 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ |
163 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ | 129 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ |
164 | 130 | ||
165 | |||
166 | /* Some systems will want different product identifers published in the | ||
167 | * device descriptor, either numbers or strings or both. These string | ||
168 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | ||
169 | */ | ||
170 | |||
171 | static ushort idVendor; | ||
172 | module_param(idVendor, ushort, S_IRUGO); | ||
173 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | ||
174 | |||
175 | static ushort idProduct; | ||
176 | module_param(idProduct, ushort, S_IRUGO); | ||
177 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | ||
178 | |||
179 | static ushort bcdDevice; | ||
180 | module_param(bcdDevice, ushort, S_IRUGO); | ||
181 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | ||
182 | |||
183 | static char *iManufacturer; | ||
184 | module_param(iManufacturer, charp, S_IRUGO); | ||
185 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | ||
186 | |||
187 | static char *iProduct; | ||
188 | module_param(iProduct, charp, S_IRUGO); | ||
189 | MODULE_PARM_DESC(iProduct, "USB Product string"); | ||
190 | |||
191 | static char *iSerialNumber; | ||
192 | module_param(iSerialNumber, charp, S_IRUGO); | ||
193 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber"); | ||
194 | |||
195 | /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ | ||
196 | static char *dev_addr; | ||
197 | module_param(dev_addr, charp, S_IRUGO); | ||
198 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); | ||
199 | |||
200 | /* this address is invisible to ifconfig */ | ||
201 | static char *host_addr; | ||
202 | module_param(host_addr, charp, S_IRUGO); | ||
203 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); | ||
204 | |||
205 | |||
206 | /*-------------------------------------------------------------------------*/ | ||
207 | |||
208 | /* Include CDC support if we could run on CDC-capable hardware. */ | ||
209 | |||
210 | #ifdef CONFIG_USB_GADGET_NET2280 | ||
211 | #define DEV_CONFIG_CDC | ||
212 | #endif | ||
213 | |||
214 | #ifdef CONFIG_USB_GADGET_DUMMY_HCD | ||
215 | #define DEV_CONFIG_CDC | ||
216 | #endif | ||
217 | |||
218 | #ifdef CONFIG_USB_GADGET_GOKU | ||
219 | #define DEV_CONFIG_CDC | ||
220 | #endif | ||
221 | |||
222 | #ifdef CONFIG_USB_GADGET_LH7A40X | ||
223 | #define DEV_CONFIG_CDC | ||
224 | #endif | ||
225 | |||
226 | #ifdef CONFIG_USB_GADGET_MQ11XX | ||
227 | #define DEV_CONFIG_CDC | ||
228 | #endif | ||
229 | |||
230 | #ifdef CONFIG_USB_GADGET_OMAP | ||
231 | #define DEV_CONFIG_CDC | ||
232 | #endif | ||
233 | |||
234 | #ifdef CONFIG_USB_GADGET_N9604 | ||
235 | #define DEV_CONFIG_CDC | ||
236 | #endif | ||
237 | |||
238 | #ifdef CONFIG_USB_GADGET_S3C2410 | ||
239 | #define DEV_CONFIG_CDC | ||
240 | #endif | ||
241 | |||
242 | #ifdef CONFIG_USB_GADGET_AT91 | ||
243 | #define DEV_CONFIG_CDC | ||
244 | #endif | ||
245 | |||
246 | #ifdef CONFIG_USB_GADGET_MUSBHSFC | ||
247 | #define DEV_CONFIG_CDC | ||
248 | #endif | ||
249 | |||
250 | #ifdef CONFIG_USB_GADGET_MUSB_HDRC | ||
251 | #define DEV_CONFIG_CDC | ||
252 | #endif | ||
253 | |||
254 | #ifdef CONFIG_USB_GADGET_ATMEL_USBA | ||
255 | #define DEV_CONFIG_CDC | ||
256 | #endif | ||
257 | |||
258 | #ifdef CONFIG_USB_GADGET_FSL_USB2 | ||
259 | #define DEV_CONFIG_CDC | ||
260 | #endif | ||
261 | |||
262 | /* For CDC-incapable hardware, choose the simple cdc subset. | ||
263 | * Anything that talks bulk (without notable bugs) can do this. | ||
264 | */ | ||
265 | #ifdef CONFIG_USB_GADGET_PXA25X | ||
266 | #define DEV_CONFIG_SUBSET | ||
267 | #endif | ||
268 | |||
269 | #ifdef CONFIG_USB_GADGET_PXA27X | ||
270 | #define DEV_CONFIG_SUBSET | ||
271 | #endif | ||
272 | |||
273 | #ifdef CONFIG_USB_GADGET_SUPERH | ||
274 | #define DEV_CONFIG_SUBSET | ||
275 | #endif | ||
276 | |||
277 | #ifdef CONFIG_USB_GADGET_SA1100 | ||
278 | /* use non-CDC for backwards compatibility */ | ||
279 | #define DEV_CONFIG_SUBSET | ||
280 | #endif | ||
281 | |||
282 | #ifdef CONFIG_USB_GADGET_M66592 | ||
283 | #define DEV_CONFIG_CDC | ||
284 | #endif | ||
285 | |||
286 | #ifdef CONFIG_USB_GADGET_AMD5536UDC | ||
287 | #define DEV_CONFIG_CDC | ||
288 | #endif | ||
289 | |||
290 | |||
291 | /*-------------------------------------------------------------------------*/ | ||
292 | |||
293 | /* "main" config is either CDC, or its simple subset */ | ||
294 | static inline int is_cdc(struct eth_dev *dev) | ||
295 | { | ||
296 | #if !defined(DEV_CONFIG_SUBSET) | ||
297 | return 1; /* only cdc possible */ | ||
298 | #elif !defined (DEV_CONFIG_CDC) | ||
299 | return 0; /* only subset possible */ | ||
300 | #else | ||
301 | return dev->cdc; /* depends on what hardware we found */ | ||
302 | #endif | ||
303 | } | ||
304 | |||
305 | /* "secondary" RNDIS config may sometimes be activated */ | ||
306 | static inline int rndis_active(struct eth_dev *dev) | ||
307 | { | ||
308 | #ifdef CONFIG_USB_ETH_RNDIS | ||
309 | return dev->rndis; | ||
310 | #else | ||
311 | return 0; | ||
312 | #endif | ||
313 | } | ||
314 | |||
315 | #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev)) | ||
316 | #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev)) | ||
317 | |||
318 | |||
319 | |||
320 | #define DEFAULT_QLEN 2 /* double buffering by default */ | ||
321 | |||
322 | /* peak bulk transfer bits-per-second */ | ||
323 | #define HS_BPS (13 * 512 * 8 * 1000 * 8) | ||
324 | #define FS_BPS (19 * 64 * 1 * 1000 * 8) | ||
325 | |||
326 | #ifdef CONFIG_USB_GADGET_DUALSPEED | ||
327 | #define DEVSPEED USB_SPEED_HIGH | ||
328 | |||
329 | static unsigned qmult = 5; | ||
330 | module_param (qmult, uint, S_IRUGO|S_IWUSR); | ||
331 | |||
332 | |||
333 | /* for dual-speed hardware, use deeper queues at highspeed */ | ||
334 | #define qlen(gadget) \ | ||
335 | (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) | ||
336 | |||
337 | static inline int BITRATE(struct usb_gadget *g) | ||
338 | { | ||
339 | return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; | ||
340 | } | ||
341 | |||
342 | #else /* full speed (low speed doesn't do bulk) */ | ||
343 | |||
344 | #define qmult 1 | ||
345 | |||
346 | #define DEVSPEED USB_SPEED_FULL | ||
347 | |||
348 | #define qlen(gadget) DEFAULT_QLEN | ||
349 | |||
350 | static inline int BITRATE(struct usb_gadget *g) | ||
351 | { | ||
352 | return FS_BPS; | ||
353 | } | ||
354 | #endif | ||
355 | |||
356 | |||
357 | /*-------------------------------------------------------------------------*/ | ||
358 | |||
359 | #define xprintk(d,level,fmt,args...) \ | ||
360 | printk(level "%s: " fmt , (d)->net->name , ## args) | ||
361 | |||
362 | #ifdef DEBUG | ||
363 | #undef DEBUG | ||
364 | #define DEBUG(dev,fmt,args...) \ | ||
365 | xprintk(dev , KERN_DEBUG , fmt , ## args) | ||
366 | #else | ||
367 | #define DEBUG(dev,fmt,args...) \ | ||
368 | do { } while (0) | ||
369 | #endif /* DEBUG */ | ||
370 | |||
371 | #ifdef VERBOSE_DEBUG | ||
372 | #define VDEBUG DEBUG | ||
373 | #else | ||
374 | #define VDEBUG(dev,fmt,args...) \ | ||
375 | do { } while (0) | ||
376 | #endif /* DEBUG */ | ||
377 | |||
378 | #define ERROR(dev,fmt,args...) \ | ||
379 | xprintk(dev , KERN_ERR , fmt , ## args) | ||
380 | #define WARN(dev,fmt,args...) \ | ||
381 | xprintk(dev , KERN_WARNING , fmt , ## args) | ||
382 | #define INFO(dev,fmt,args...) \ | ||
383 | xprintk(dev , KERN_INFO , fmt , ## args) | ||
384 | |||
385 | /*-------------------------------------------------------------------------*/ | 131 | /*-------------------------------------------------------------------------*/ |
386 | 132 | ||
387 | /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly | 133 | static struct usb_device_descriptor device_desc = { |
388 | * ep0 implementation: descriptors, config management, setup(). | ||
389 | * also optional class-specific notification interrupt transfer. | ||
390 | */ | ||
391 | |||
392 | /* | ||
393 | * DESCRIPTORS ... most are static, but strings and (full) configuration | ||
394 | * descriptors are built on demand. For now we do either full CDC, or | ||
395 | * our simple subset, with RNDIS as an optional second configuration. | ||
396 | * | ||
397 | * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But | ||
398 | * the class descriptors match a modem (they're ignored; it's really just | ||
399 | * Ethernet functionality), they don't need the NOP altsetting, and the | ||
400 | * status transfer endpoint isn't optional. | ||
401 | */ | ||
402 | |||
403 | #define STRING_MANUFACTURER 1 | ||
404 | #define STRING_PRODUCT 2 | ||
405 | #define STRING_ETHADDR 3 | ||
406 | #define STRING_DATA 4 | ||
407 | #define STRING_CONTROL 5 | ||
408 | #define STRING_RNDIS_CONTROL 6 | ||
409 | #define STRING_CDC 7 | ||
410 | #define STRING_SUBSET 8 | ||
411 | #define STRING_RNDIS 9 | ||
412 | #define STRING_SERIALNUMBER 10 | ||
413 | |||
414 | /* holds our biggest descriptor (or RNDIS response) */ | ||
415 | #define USB_BUFSIZ 256 | ||
416 | |||
417 | /* | ||
418 | * This device advertises one configuration, eth_config, unless RNDIS | ||
419 | * is enabled (rndis_config) on hardware supporting at least two configs. | ||
420 | * | ||
421 | * NOTE: Controllers like superh_udc should probably be able to use | ||
422 | * an RNDIS-only configuration. | ||
423 | * | ||
424 | * FIXME define some higher-powered configurations to make it easier | ||
425 | * to recharge batteries ... | ||
426 | */ | ||
427 | |||
428 | #define DEV_CONFIG_VALUE 1 /* cdc or subset */ | ||
429 | #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */ | ||
430 | |||
431 | static struct usb_device_descriptor | ||
432 | device_desc = { | ||
433 | .bLength = sizeof device_desc, | 134 | .bLength = sizeof device_desc, |
434 | .bDescriptorType = USB_DT_DEVICE, | 135 | .bDescriptorType = USB_DT_DEVICE, |
435 | 136 | ||
@@ -438,2220 +139,234 @@ device_desc = { | |||
438 | .bDeviceClass = USB_CLASS_COMM, | 139 | .bDeviceClass = USB_CLASS_COMM, |
439 | .bDeviceSubClass = 0, | 140 | .bDeviceSubClass = 0, |
440 | .bDeviceProtocol = 0, | 141 | .bDeviceProtocol = 0, |
142 | /* .bMaxPacketSize0 = f(hardware) */ | ||
441 | 143 | ||
144 | /* Vendor and product id defaults change according to what configs | ||
145 | * we support. (As does bNumConfigurations.) These values can | ||
146 | * also be overridden by module parameters. | ||
147 | */ | ||
442 | .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), | 148 | .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM), |
443 | .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), | 149 | .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM), |
444 | .iManufacturer = STRING_MANUFACTURER, | 150 | /* .bcdDevice = f(hardware) */ |
445 | .iProduct = STRING_PRODUCT, | 151 | /* .iManufacturer = DYNAMIC */ |
152 | /* .iProduct = DYNAMIC */ | ||
153 | /* NO SERIAL NUMBER */ | ||
446 | .bNumConfigurations = 1, | 154 | .bNumConfigurations = 1, |
447 | }; | 155 | }; |
448 | 156 | ||
449 | static struct usb_otg_descriptor | 157 | static struct usb_otg_descriptor otg_descriptor = { |
450 | otg_descriptor = { | ||
451 | .bLength = sizeof otg_descriptor, | 158 | .bLength = sizeof otg_descriptor, |
452 | .bDescriptorType = USB_DT_OTG, | 159 | .bDescriptorType = USB_DT_OTG, |
453 | 160 | ||
454 | .bmAttributes = USB_OTG_SRP, | 161 | /* REVISIT SRP-only hardware is possible, although |
455 | }; | 162 | * it would not be called "OTG" ... |
456 | 163 | */ | |
457 | static struct usb_config_descriptor | 164 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
458 | eth_config = { | ||
459 | .bLength = sizeof eth_config, | ||
460 | .bDescriptorType = USB_DT_CONFIG, | ||
461 | |||
462 | /* compute wTotalLength on the fly */ | ||
463 | .bNumInterfaces = 2, | ||
464 | .bConfigurationValue = DEV_CONFIG_VALUE, | ||
465 | .iConfiguration = STRING_CDC, | ||
466 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
467 | .bMaxPower = 50, | ||
468 | }; | ||
469 | |||
470 | #ifdef CONFIG_USB_ETH_RNDIS | ||
471 | static struct usb_config_descriptor | ||
472 | rndis_config = { | ||
473 | .bLength = sizeof rndis_config, | ||
474 | .bDescriptorType = USB_DT_CONFIG, | ||
475 | |||
476 | /* compute wTotalLength on the fly */ | ||
477 | .bNumInterfaces = 2, | ||
478 | .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE, | ||
479 | .iConfiguration = STRING_RNDIS, | ||
480 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
481 | .bMaxPower = 50, | ||
482 | }; | ||
483 | #endif | ||
484 | |||
485 | /* | ||
486 | * Compared to the simple CDC subset, the full CDC Ethernet model adds | ||
487 | * three class descriptors, two interface descriptors, optional status | ||
488 | * endpoint. Both have a "data" interface and two bulk endpoints. | ||
489 | * There are also differences in how control requests are handled. | ||
490 | * | ||
491 | * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the | ||
492 | * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it | ||
493 | * may hang or oops. Since bugfixes (or accurate specs, letting Linux | ||
494 | * work around those bugs) are unlikely to ever come from MSFT, you may | ||
495 | * wish to avoid using RNDIS. | ||
496 | * | ||
497 | * MCCI offers an alternative to RNDIS if you need to connect to Windows | ||
498 | * but have hardware that can't support CDC Ethernet. We add descriptors | ||
499 | * to present the CDC Subset as a (nonconformant) CDC MDLM variant called | ||
500 | * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can | ||
501 | * get those drivers from MCCI, or bundled with various products. | ||
502 | */ | ||
503 | |||
504 | #ifdef DEV_CONFIG_CDC | ||
505 | static struct usb_interface_descriptor | ||
506 | control_intf = { | ||
507 | .bLength = sizeof control_intf, | ||
508 | .bDescriptorType = USB_DT_INTERFACE, | ||
509 | |||
510 | .bInterfaceNumber = 0, | ||
511 | /* status endpoint is optional; this may be patched later */ | ||
512 | .bNumEndpoints = 1, | ||
513 | .bInterfaceClass = USB_CLASS_COMM, | ||
514 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, | ||
515 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, | ||
516 | .iInterface = STRING_CONTROL, | ||
517 | }; | ||
518 | #endif | ||
519 | |||
520 | #ifdef CONFIG_USB_ETH_RNDIS | ||
521 | static const struct usb_interface_descriptor | ||
522 | rndis_control_intf = { | ||
523 | .bLength = sizeof rndis_control_intf, | ||
524 | .bDescriptorType = USB_DT_INTERFACE, | ||
525 | |||
526 | .bInterfaceNumber = 0, | ||
527 | .bNumEndpoints = 1, | ||
528 | .bInterfaceClass = USB_CLASS_COMM, | ||
529 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | ||
530 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, | ||
531 | .iInterface = STRING_RNDIS_CONTROL, | ||
532 | }; | ||
533 | #endif | ||
534 | |||
535 | static const struct usb_cdc_header_desc header_desc = { | ||
536 | .bLength = sizeof header_desc, | ||
537 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
538 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
539 | |||
540 | .bcdCDC = __constant_cpu_to_le16 (0x0110), | ||
541 | }; | ||
542 | |||
543 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
544 | |||
545 | static const struct usb_cdc_union_desc union_desc = { | ||
546 | .bLength = sizeof union_desc, | ||
547 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
548 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | ||
549 | |||
550 | .bMasterInterface0 = 0, /* index of control interface */ | ||
551 | .bSlaveInterface0 = 1, /* index of DATA interface */ | ||
552 | }; | ||
553 | |||
554 | #endif /* CDC || RNDIS */ | ||
555 | |||
556 | #ifdef CONFIG_USB_ETH_RNDIS | ||
557 | |||
558 | static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { | ||
559 | .bLength = sizeof call_mgmt_descriptor, | ||
560 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
561 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | ||
562 | |||
563 | .bmCapabilities = 0x00, | ||
564 | .bDataInterface = 0x01, | ||
565 | }; | ||
566 | |||
567 | static const struct usb_cdc_acm_descriptor acm_descriptor = { | ||
568 | .bLength = sizeof acm_descriptor, | ||
569 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
570 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | ||
571 | |||
572 | .bmCapabilities = 0x00, | ||
573 | }; | ||
574 | |||
575 | #endif | ||
576 | |||
577 | #ifndef DEV_CONFIG_CDC | ||
578 | |||
579 | /* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various | ||
580 | * ways: data endpoints live in the control interface, there's no data | ||
581 | * interface, and it's not used to talk to a cell phone radio. | ||
582 | */ | ||
583 | |||
584 | static const struct usb_cdc_mdlm_desc mdlm_desc = { | ||
585 | .bLength = sizeof mdlm_desc, | ||
586 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
587 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, | ||
588 | |||
589 | .bcdVersion = __constant_cpu_to_le16(0x0100), | ||
590 | .bGUID = { | ||
591 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, | ||
592 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, | ||
593 | }, | ||
594 | }; | ||
595 | |||
596 | /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we | ||
597 | * can't really use its struct. All we do here is say that we're using | ||
598 | * the submode of "SAFE" which directly matches the CDC Subset. | ||
599 | */ | ||
600 | static const u8 mdlm_detail_desc[] = { | ||
601 | 6, | ||
602 | USB_DT_CS_INTERFACE, | ||
603 | USB_CDC_MDLM_DETAIL_TYPE, | ||
604 | |||
605 | 0, /* "SAFE" */ | ||
606 | 0, /* network control capabilities (none) */ | ||
607 | 0, /* network data capabilities ("raw" encapsulation) */ | ||
608 | }; | ||
609 | |||
610 | #endif | ||
611 | |||
612 | static const struct usb_cdc_ether_desc ether_desc = { | ||
613 | .bLength = sizeof ether_desc, | ||
614 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
615 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, | ||
616 | |||
617 | /* this descriptor actually adds value, surprise! */ | ||
618 | .iMACAddress = STRING_ETHADDR, | ||
619 | .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */ | ||
620 | .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN), | ||
621 | .wNumberMCFilters = __constant_cpu_to_le16 (0), | ||
622 | .bNumberPowerFilters = 0, | ||
623 | }; | ||
624 | |||
625 | |||
626 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
627 | |||
628 | /* include the status endpoint if we can, even where it's optional. | ||
629 | * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one | ||
630 | * packet, to simplify cancellation; and a big transfer interval, to | ||
631 | * waste less bandwidth. | ||
632 | * | ||
633 | * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even | ||
634 | * if they ignore the connect/disconnect notifications that real aether | ||
635 | * can provide. more advanced cdc configurations might want to support | ||
636 | * encapsulated commands (vendor-specific, using control-OUT). | ||
637 | * | ||
638 | * RNDIS requires the status endpoint, since it uses that encapsulation | ||
639 | * mechanism for its funky RPC scheme. | ||
640 | */ | ||
641 | |||
642 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ | ||
643 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ | ||
644 | |||
645 | static struct usb_endpoint_descriptor | ||
646 | fs_status_desc = { | ||
647 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
648 | .bDescriptorType = USB_DT_ENDPOINT, | ||
649 | |||
650 | .bEndpointAddress = USB_DIR_IN, | ||
651 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
652 | .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), | ||
653 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, | ||
654 | }; | ||
655 | #endif | ||
656 | |||
657 | #ifdef DEV_CONFIG_CDC | ||
658 | |||
659 | /* the default data interface has no endpoints ... */ | ||
660 | |||
661 | static const struct usb_interface_descriptor | ||
662 | data_nop_intf = { | ||
663 | .bLength = sizeof data_nop_intf, | ||
664 | .bDescriptorType = USB_DT_INTERFACE, | ||
665 | |||
666 | .bInterfaceNumber = 1, | ||
667 | .bAlternateSetting = 0, | ||
668 | .bNumEndpoints = 0, | ||
669 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
670 | .bInterfaceSubClass = 0, | ||
671 | .bInterfaceProtocol = 0, | ||
672 | }; | ||
673 | |||
674 | /* ... but the "real" data interface has two bulk endpoints */ | ||
675 | |||
676 | static const struct usb_interface_descriptor | ||
677 | data_intf = { | ||
678 | .bLength = sizeof data_intf, | ||
679 | .bDescriptorType = USB_DT_INTERFACE, | ||
680 | |||
681 | .bInterfaceNumber = 1, | ||
682 | .bAlternateSetting = 1, | ||
683 | .bNumEndpoints = 2, | ||
684 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
685 | .bInterfaceSubClass = 0, | ||
686 | .bInterfaceProtocol = 0, | ||
687 | .iInterface = STRING_DATA, | ||
688 | }; | ||
689 | |||
690 | #endif | ||
691 | |||
692 | #ifdef CONFIG_USB_ETH_RNDIS | ||
693 | |||
694 | /* RNDIS doesn't activate by changing to the "real" altsetting */ | ||
695 | |||
696 | static const struct usb_interface_descriptor | ||
697 | rndis_data_intf = { | ||
698 | .bLength = sizeof rndis_data_intf, | ||
699 | .bDescriptorType = USB_DT_INTERFACE, | ||
700 | |||
701 | .bInterfaceNumber = 1, | ||
702 | .bAlternateSetting = 0, | ||
703 | .bNumEndpoints = 2, | ||
704 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
705 | .bInterfaceSubClass = 0, | ||
706 | .bInterfaceProtocol = 0, | ||
707 | .iInterface = STRING_DATA, | ||
708 | }; | ||
709 | |||
710 | #endif | ||
711 | |||
712 | #ifdef DEV_CONFIG_SUBSET | ||
713 | |||
714 | /* | ||
715 | * "Simple" CDC-subset option is a simple vendor-neutral model that most | ||
716 | * full speed controllers can handle: one interface, two bulk endpoints. | ||
717 | * | ||
718 | * To assist host side drivers, we fancy it up a bit, and add descriptors | ||
719 | * so some host side drivers will understand it as a "SAFE" variant. | ||
720 | */ | ||
721 | |||
722 | static const struct usb_interface_descriptor | ||
723 | subset_data_intf = { | ||
724 | .bLength = sizeof subset_data_intf, | ||
725 | .bDescriptorType = USB_DT_INTERFACE, | ||
726 | |||
727 | .bInterfaceNumber = 0, | ||
728 | .bAlternateSetting = 0, | ||
729 | .bNumEndpoints = 2, | ||
730 | .bInterfaceClass = USB_CLASS_COMM, | ||
731 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, | ||
732 | .bInterfaceProtocol = 0, | ||
733 | .iInterface = STRING_DATA, | ||
734 | }; | ||
735 | |||
736 | #endif /* SUBSET */ | ||
737 | |||
738 | |||
739 | static struct usb_endpoint_descriptor | ||
740 | fs_source_desc = { | ||
741 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
742 | .bDescriptorType = USB_DT_ENDPOINT, | ||
743 | |||
744 | .bEndpointAddress = USB_DIR_IN, | ||
745 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
746 | }; | ||
747 | |||
748 | static struct usb_endpoint_descriptor | ||
749 | fs_sink_desc = { | ||
750 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
751 | .bDescriptorType = USB_DT_ENDPOINT, | ||
752 | |||
753 | .bEndpointAddress = USB_DIR_OUT, | ||
754 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
755 | }; | 165 | }; |
756 | 166 | ||
757 | static const struct usb_descriptor_header *fs_eth_function [11] = { | 167 | static const struct usb_descriptor_header *otg_desc[] = { |
758 | (struct usb_descriptor_header *) &otg_descriptor, | 168 | (struct usb_descriptor_header *) &otg_descriptor, |
759 | #ifdef DEV_CONFIG_CDC | ||
760 | /* "cdc" mode descriptors */ | ||
761 | (struct usb_descriptor_header *) &control_intf, | ||
762 | (struct usb_descriptor_header *) &header_desc, | ||
763 | (struct usb_descriptor_header *) &union_desc, | ||
764 | (struct usb_descriptor_header *) ðer_desc, | ||
765 | /* NOTE: status endpoint may need to be removed */ | ||
766 | (struct usb_descriptor_header *) &fs_status_desc, | ||
767 | /* data interface, with altsetting */ | ||
768 | (struct usb_descriptor_header *) &data_nop_intf, | ||
769 | (struct usb_descriptor_header *) &data_intf, | ||
770 | (struct usb_descriptor_header *) &fs_source_desc, | ||
771 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
772 | NULL, | 169 | NULL, |
773 | #endif /* DEV_CONFIG_CDC */ | ||
774 | }; | 170 | }; |
775 | 171 | ||
776 | static inline void __init fs_subset_descriptors(void) | ||
777 | { | ||
778 | #ifdef DEV_CONFIG_SUBSET | ||
779 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ | ||
780 | fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | ||
781 | fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; | ||
782 | fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; | ||
783 | fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; | ||
784 | fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; | ||
785 | fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; | ||
786 | fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; | ||
787 | fs_eth_function[8] = NULL; | ||
788 | #else | ||
789 | fs_eth_function[1] = NULL; | ||
790 | #endif | ||
791 | } | ||
792 | 172 | ||
793 | #ifdef CONFIG_USB_ETH_RNDIS | 173 | /* string IDs are assigned dynamically */ |
794 | static const struct usb_descriptor_header *fs_rndis_function [] = { | ||
795 | (struct usb_descriptor_header *) &otg_descriptor, | ||
796 | /* control interface matches ACM, not Ethernet */ | ||
797 | (struct usb_descriptor_header *) &rndis_control_intf, | ||
798 | (struct usb_descriptor_header *) &header_desc, | ||
799 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | ||
800 | (struct usb_descriptor_header *) &acm_descriptor, | ||
801 | (struct usb_descriptor_header *) &union_desc, | ||
802 | (struct usb_descriptor_header *) &fs_status_desc, | ||
803 | /* data interface has no altsetting */ | ||
804 | (struct usb_descriptor_header *) &rndis_data_intf, | ||
805 | (struct usb_descriptor_header *) &fs_source_desc, | ||
806 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
807 | NULL, | ||
808 | }; | ||
809 | #endif | ||
810 | |||
811 | /* | ||
812 | * usb 2.0 devices need to expose both high speed and full speed | ||
813 | * descriptors, unless they only run at full speed. | ||
814 | */ | ||
815 | |||
816 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
817 | static struct usb_endpoint_descriptor | ||
818 | hs_status_desc = { | ||
819 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
820 | .bDescriptorType = USB_DT_ENDPOINT, | ||
821 | |||
822 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
823 | .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT), | ||
824 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, | ||
825 | }; | ||
826 | #endif /* DEV_CONFIG_CDC */ | ||
827 | |||
828 | static struct usb_endpoint_descriptor | ||
829 | hs_source_desc = { | ||
830 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
831 | .bDescriptorType = USB_DT_ENDPOINT, | ||
832 | |||
833 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
834 | .wMaxPacketSize = __constant_cpu_to_le16 (512), | ||
835 | }; | ||
836 | |||
837 | static struct usb_endpoint_descriptor | ||
838 | hs_sink_desc = { | ||
839 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
840 | .bDescriptorType = USB_DT_ENDPOINT, | ||
841 | |||
842 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
843 | .wMaxPacketSize = __constant_cpu_to_le16 (512), | ||
844 | }; | ||
845 | 174 | ||
846 | static struct usb_qualifier_descriptor | 175 | #define STRING_MANUFACTURER_IDX 0 |
847 | dev_qualifier = { | 176 | #define STRING_PRODUCT_IDX 1 |
848 | .bLength = sizeof dev_qualifier, | ||
849 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | ||
850 | 177 | ||
851 | .bcdUSB = __constant_cpu_to_le16 (0x0200), | 178 | static char manufacturer[50]; |
852 | .bDeviceClass = USB_CLASS_COMM, | ||
853 | 179 | ||
854 | .bNumConfigurations = 1, | 180 | static struct usb_string strings_dev[] = { |
181 | [STRING_MANUFACTURER_IDX].s = manufacturer, | ||
182 | [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC, | ||
183 | { } /* end of list */ | ||
855 | }; | 184 | }; |
856 | 185 | ||
857 | static const struct usb_descriptor_header *hs_eth_function [11] = { | 186 | static struct usb_gadget_strings stringtab_dev = { |
858 | (struct usb_descriptor_header *) &otg_descriptor, | 187 | .language = 0x0409, /* en-us */ |
859 | #ifdef DEV_CONFIG_CDC | 188 | .strings = strings_dev, |
860 | /* "cdc" mode descriptors */ | ||
861 | (struct usb_descriptor_header *) &control_intf, | ||
862 | (struct usb_descriptor_header *) &header_desc, | ||
863 | (struct usb_descriptor_header *) &union_desc, | ||
864 | (struct usb_descriptor_header *) ðer_desc, | ||
865 | /* NOTE: status endpoint may need to be removed */ | ||
866 | (struct usb_descriptor_header *) &hs_status_desc, | ||
867 | /* data interface, with altsetting */ | ||
868 | (struct usb_descriptor_header *) &data_nop_intf, | ||
869 | (struct usb_descriptor_header *) &data_intf, | ||
870 | (struct usb_descriptor_header *) &hs_source_desc, | ||
871 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
872 | NULL, | ||
873 | #endif /* DEV_CONFIG_CDC */ | ||
874 | }; | 189 | }; |
875 | 190 | ||
876 | static inline void __init hs_subset_descriptors(void) | 191 | static struct usb_gadget_strings *dev_strings[] = { |
877 | { | 192 | &stringtab_dev, |
878 | #ifdef DEV_CONFIG_SUBSET | ||
879 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ | ||
880 | hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | ||
881 | hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; | ||
882 | hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; | ||
883 | hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; | ||
884 | hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; | ||
885 | hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; | ||
886 | hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; | ||
887 | hs_eth_function[8] = NULL; | ||
888 | #else | ||
889 | hs_eth_function[1] = NULL; | ||
890 | #endif | ||
891 | } | ||
892 | |||
893 | #ifdef CONFIG_USB_ETH_RNDIS | ||
894 | static const struct usb_descriptor_header *hs_rndis_function [] = { | ||
895 | (struct usb_descriptor_header *) &otg_descriptor, | ||
896 | /* control interface matches ACM, not Ethernet */ | ||
897 | (struct usb_descriptor_header *) &rndis_control_intf, | ||
898 | (struct usb_descriptor_header *) &header_desc, | ||
899 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | ||
900 | (struct usb_descriptor_header *) &acm_descriptor, | ||
901 | (struct usb_descriptor_header *) &union_desc, | ||
902 | (struct usb_descriptor_header *) &hs_status_desc, | ||
903 | /* data interface has no altsetting */ | ||
904 | (struct usb_descriptor_header *) &rndis_data_intf, | ||
905 | (struct usb_descriptor_header *) &hs_source_desc, | ||
906 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
907 | NULL, | 193 | NULL, |
908 | }; | 194 | }; |
909 | #endif | ||
910 | |||
911 | |||
912 | /* maxpacket and other transfer characteristics vary by speed. */ | ||
913 | static inline struct usb_endpoint_descriptor * | ||
914 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | ||
915 | struct usb_endpoint_descriptor *fs) | ||
916 | { | ||
917 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
918 | return hs; | ||
919 | return fs; | ||
920 | } | ||
921 | 195 | ||
196 | static u8 hostaddr[ETH_ALEN]; | ||
922 | 197 | ||
923 | /*-------------------------------------------------------------------------*/ | 198 | /*-------------------------------------------------------------------------*/ |
924 | 199 | ||
925 | /* descriptors that are built on-demand */ | ||
926 | |||
927 | static char manufacturer [50]; | ||
928 | static char product_desc [40] = DRIVER_DESC; | ||
929 | static char serial_number [20]; | ||
930 | |||
931 | /* address that the host will use ... usually assigned at random */ | ||
932 | static char ethaddr [2 * ETH_ALEN + 1]; | ||
933 | |||
934 | /* static strings, in UTF-8 */ | ||
935 | static struct usb_string strings [] = { | ||
936 | { STRING_MANUFACTURER, manufacturer, }, | ||
937 | { STRING_PRODUCT, product_desc, }, | ||
938 | { STRING_SERIALNUMBER, serial_number, }, | ||
939 | { STRING_DATA, "Ethernet Data", }, | ||
940 | { STRING_ETHADDR, ethaddr, }, | ||
941 | #ifdef DEV_CONFIG_CDC | ||
942 | { STRING_CDC, "CDC Ethernet", }, | ||
943 | { STRING_CONTROL, "CDC Communications Control", }, | ||
944 | #endif | ||
945 | #ifdef DEV_CONFIG_SUBSET | ||
946 | { STRING_SUBSET, "CDC Ethernet Subset", }, | ||
947 | #endif | ||
948 | #ifdef CONFIG_USB_ETH_RNDIS | ||
949 | { STRING_RNDIS, "RNDIS", }, | ||
950 | { STRING_RNDIS_CONTROL, "RNDIS Communications Control", }, | ||
951 | #endif | ||
952 | { } /* end of list */ | ||
953 | }; | ||
954 | |||
955 | static struct usb_gadget_strings stringtab = { | ||
956 | .language = 0x0409, /* en-us */ | ||
957 | .strings = strings, | ||
958 | }; | ||
959 | |||
960 | /* | 200 | /* |
961 | * one config, two interfaces: control, data. | 201 | * We may not have an RNDIS configuration, but if we do it needs to be |
962 | * complications: class descriptors, and an altsetting. | 202 | * the first one present. That's to make Microsoft's drivers happy, |
963 | */ | 203 | * and to follow DOCSIS 1.0 (cable modem standard). |
964 | static int | ||
965 | config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) | ||
966 | { | ||
967 | int len; | ||
968 | const struct usb_config_descriptor *config; | ||
969 | const struct usb_descriptor_header **function; | ||
970 | int hs = 0; | ||
971 | |||
972 | if (gadget_is_dualspeed(g)) { | ||
973 | hs = (g->speed == USB_SPEED_HIGH); | ||
974 | if (type == USB_DT_OTHER_SPEED_CONFIG) | ||
975 | hs = !hs; | ||
976 | } | ||
977 | #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) | ||
978 | |||
979 | if (index >= device_desc.bNumConfigurations) | ||
980 | return -EINVAL; | ||
981 | |||
982 | #ifdef CONFIG_USB_ETH_RNDIS | ||
983 | /* list the RNDIS config first, to make Microsoft's drivers | ||
984 | * happy. DOCSIS 1.0 needs this too. | ||
985 | */ | ||
986 | if (device_desc.bNumConfigurations == 2 && index == 0) { | ||
987 | config = &rndis_config; | ||
988 | function = which_fn (rndis); | ||
989 | } else | ||
990 | #endif | ||
991 | { | ||
992 | config = ð_config; | ||
993 | function = which_fn (eth); | ||
994 | } | ||
995 | |||
996 | /* for now, don't advertise srp-only devices */ | ||
997 | if (!is_otg) | ||
998 | function++; | ||
999 | |||
1000 | len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function); | ||
1001 | if (len < 0) | ||
1002 | return len; | ||
1003 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; | ||
1004 | return len; | ||
1005 | } | ||
1006 | |||
1007 | /*-------------------------------------------------------------------------*/ | ||
1008 | |||
1009 | static void eth_start (struct eth_dev *dev, gfp_t gfp_flags); | ||
1010 | static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags); | ||
1011 | |||
1012 | static int | ||
1013 | set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) | ||
1014 | { | ||
1015 | int result = 0; | ||
1016 | struct usb_gadget *gadget = dev->gadget; | ||
1017 | |||
1018 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
1019 | /* status endpoint used for RNDIS and (optionally) CDC */ | ||
1020 | if (!subset_active(dev) && dev->status_ep) { | ||
1021 | dev->status = ep_desc (gadget, &hs_status_desc, | ||
1022 | &fs_status_desc); | ||
1023 | dev->status_ep->driver_data = dev; | ||
1024 | |||
1025 | result = usb_ep_enable (dev->status_ep, dev->status); | ||
1026 | if (result != 0) { | ||
1027 | DEBUG (dev, "enable %s --> %d\n", | ||
1028 | dev->status_ep->name, result); | ||
1029 | goto done; | ||
1030 | } | ||
1031 | } | ||
1032 | #endif | ||
1033 | |||
1034 | dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); | ||
1035 | dev->in_ep->driver_data = dev; | ||
1036 | |||
1037 | dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); | ||
1038 | dev->out_ep->driver_data = dev; | ||
1039 | |||
1040 | /* With CDC, the host isn't allowed to use these two data | ||
1041 | * endpoints in the default altsetting for the interface. | ||
1042 | * so we don't activate them yet. Reset from SET_INTERFACE. | ||
1043 | * | ||
1044 | * Strictly speaking RNDIS should work the same: activation is | ||
1045 | * a side effect of setting a packet filter. Deactivation is | ||
1046 | * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. | ||
1047 | */ | ||
1048 | if (!cdc_active(dev)) { | ||
1049 | result = usb_ep_enable (dev->in_ep, dev->in); | ||
1050 | if (result != 0) { | ||
1051 | DEBUG(dev, "enable %s --> %d\n", | ||
1052 | dev->in_ep->name, result); | ||
1053 | goto done; | ||
1054 | } | ||
1055 | |||
1056 | result = usb_ep_enable (dev->out_ep, dev->out); | ||
1057 | if (result != 0) { | ||
1058 | DEBUG (dev, "enable %s --> %d\n", | ||
1059 | dev->out_ep->name, result); | ||
1060 | goto done; | ||
1061 | } | ||
1062 | } | ||
1063 | |||
1064 | done: | ||
1065 | if (result == 0) | ||
1066 | result = alloc_requests (dev, qlen (gadget), gfp_flags); | ||
1067 | |||
1068 | /* on error, disable any endpoints */ | ||
1069 | if (result < 0) { | ||
1070 | if (!subset_active(dev) && dev->status_ep) | ||
1071 | (void) usb_ep_disable (dev->status_ep); | ||
1072 | dev->status = NULL; | ||
1073 | (void) usb_ep_disable (dev->in_ep); | ||
1074 | (void) usb_ep_disable (dev->out_ep); | ||
1075 | dev->in = NULL; | ||
1076 | dev->out = NULL; | ||
1077 | } | ||
1078 | |||
1079 | /* activate non-CDC configs right away | ||
1080 | * this isn't strictly according to the RNDIS spec | ||
1081 | */ | ||
1082 | else if (!cdc_active (dev)) { | ||
1083 | netif_carrier_on (dev->net); | ||
1084 | if (netif_running (dev->net)) { | ||
1085 | spin_unlock (&dev->lock); | ||
1086 | eth_start (dev, GFP_ATOMIC); | ||
1087 | spin_lock (&dev->lock); | ||
1088 | } | ||
1089 | } | ||
1090 | |||
1091 | if (result == 0) | ||
1092 | DEBUG (dev, "qlen %d\n", qlen (gadget)); | ||
1093 | |||
1094 | /* caller is responsible for cleanup on error */ | ||
1095 | return result; | ||
1096 | } | ||
1097 | |||
1098 | static void eth_reset_config (struct eth_dev *dev) | ||
1099 | { | ||
1100 | struct usb_request *req; | ||
1101 | |||
1102 | if (dev->config == 0) | ||
1103 | return; | ||
1104 | |||
1105 | DEBUG (dev, "%s\n", __func__); | ||
1106 | |||
1107 | netif_stop_queue (dev->net); | ||
1108 | netif_carrier_off (dev->net); | ||
1109 | rndis_uninit(dev->rndis_config); | ||
1110 | |||
1111 | /* disable endpoints, forcing (synchronous) completion of | ||
1112 | * pending i/o. then free the requests. | ||
1113 | */ | ||
1114 | if (dev->in) { | ||
1115 | usb_ep_disable (dev->in_ep); | ||
1116 | spin_lock(&dev->req_lock); | ||
1117 | while (likely (!list_empty (&dev->tx_reqs))) { | ||
1118 | req = container_of (dev->tx_reqs.next, | ||
1119 | struct usb_request, list); | ||
1120 | list_del (&req->list); | ||
1121 | |||
1122 | spin_unlock(&dev->req_lock); | ||
1123 | usb_ep_free_request (dev->in_ep, req); | ||
1124 | spin_lock(&dev->req_lock); | ||
1125 | } | ||
1126 | spin_unlock(&dev->req_lock); | ||
1127 | } | ||
1128 | if (dev->out) { | ||
1129 | usb_ep_disable (dev->out_ep); | ||
1130 | spin_lock(&dev->req_lock); | ||
1131 | while (likely (!list_empty (&dev->rx_reqs))) { | ||
1132 | req = container_of (dev->rx_reqs.next, | ||
1133 | struct usb_request, list); | ||
1134 | list_del (&req->list); | ||
1135 | |||
1136 | spin_unlock(&dev->req_lock); | ||
1137 | usb_ep_free_request (dev->out_ep, req); | ||
1138 | spin_lock(&dev->req_lock); | ||
1139 | } | ||
1140 | spin_unlock(&dev->req_lock); | ||
1141 | } | ||
1142 | |||
1143 | if (dev->status) { | ||
1144 | usb_ep_disable (dev->status_ep); | ||
1145 | } | ||
1146 | dev->rndis = 0; | ||
1147 | dev->cdc_filter = 0; | ||
1148 | dev->config = 0; | ||
1149 | } | ||
1150 | |||
1151 | /* change our operational config. must agree with the code | ||
1152 | * that returns config descriptors, and altsetting code. | ||
1153 | */ | 204 | */ |
1154 | static int | 205 | static int __init rndis_do_config(struct usb_configuration *c) |
1155 | eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags) | ||
1156 | { | 206 | { |
1157 | int result = 0; | 207 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
1158 | struct usb_gadget *gadget = dev->gadget; | ||
1159 | |||
1160 | if (gadget_is_sa1100 (gadget) | ||
1161 | && dev->config | ||
1162 | && atomic_read (&dev->tx_qlen) != 0) { | ||
1163 | /* tx fifo is full, but we can't clear it...*/ | ||
1164 | INFO (dev, "can't change configurations\n"); | ||
1165 | return -ESPIPE; | ||
1166 | } | ||
1167 | eth_reset_config (dev); | ||
1168 | |||
1169 | switch (number) { | ||
1170 | case DEV_CONFIG_VALUE: | ||
1171 | result = set_ether_config (dev, gfp_flags); | ||
1172 | break; | ||
1173 | #ifdef CONFIG_USB_ETH_RNDIS | ||
1174 | case DEV_RNDIS_CONFIG_VALUE: | ||
1175 | dev->rndis = 1; | ||
1176 | result = set_ether_config (dev, gfp_flags); | ||
1177 | break; | ||
1178 | #endif | ||
1179 | default: | ||
1180 | result = -EINVAL; | ||
1181 | /* FALL THROUGH */ | ||
1182 | case 0: | ||
1183 | break; | ||
1184 | } | ||
1185 | |||
1186 | if (result) { | ||
1187 | if (number) | ||
1188 | eth_reset_config (dev); | ||
1189 | usb_gadget_vbus_draw(dev->gadget, | ||
1190 | gadget_is_otg(dev->gadget) ? 8 : 100); | ||
1191 | } else { | ||
1192 | char *speed; | ||
1193 | unsigned power; | ||
1194 | |||
1195 | power = 2 * eth_config.bMaxPower; | ||
1196 | usb_gadget_vbus_draw(dev->gadget, power); | ||
1197 | 208 | ||
1198 | switch (gadget->speed) { | 209 | if (gadget_is_otg(c->cdev->gadget)) { |
1199 | case USB_SPEED_FULL: speed = "full"; break; | 210 | c->descriptors = otg_desc; |
1200 | #ifdef CONFIG_USB_GADGET_DUALSPEED | 211 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
1201 | case USB_SPEED_HIGH: speed = "high"; break; | ||
1202 | #endif | ||
1203 | default: speed = "?"; break; | ||
1204 | } | ||
1205 | |||
1206 | dev->config = number; | ||
1207 | INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n", | ||
1208 | speed, number, power, driver_desc, | ||
1209 | rndis_active(dev) | ||
1210 | ? "RNDIS" | ||
1211 | : (cdc_active(dev) | ||
1212 | ? "CDC Ethernet" | ||
1213 | : "CDC Ethernet Subset")); | ||
1214 | } | 212 | } |
1215 | return result; | ||
1216 | } | ||
1217 | |||
1218 | /*-------------------------------------------------------------------------*/ | ||
1219 | |||
1220 | #ifdef DEV_CONFIG_CDC | ||
1221 | 213 | ||
1222 | /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) | 214 | return rndis_bind_config(c, hostaddr); |
1223 | * only to notify the host about link status changes (which we support) or | ||
1224 | * report completion of some encapsulated command (as used in RNDIS). Since | ||
1225 | * we want this CDC Ethernet code to be vendor-neutral, we don't use that | ||
1226 | * command mechanism; and only one status request is ever queued. | ||
1227 | */ | ||
1228 | |||
1229 | static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) | ||
1230 | { | ||
1231 | struct usb_cdc_notification *event = req->buf; | ||
1232 | int value = req->status; | ||
1233 | struct eth_dev *dev = ep->driver_data; | ||
1234 | |||
1235 | /* issue the second notification if host reads the first */ | ||
1236 | if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION | ||
1237 | && value == 0) { | ||
1238 | __le32 *data = req->buf + sizeof *event; | ||
1239 | |||
1240 | event->bmRequestType = 0xA1; | ||
1241 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; | ||
1242 | event->wValue = __constant_cpu_to_le16 (0); | ||
1243 | event->wIndex = __constant_cpu_to_le16 (1); | ||
1244 | event->wLength = __constant_cpu_to_le16 (8); | ||
1245 | |||
1246 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ | ||
1247 | data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget)); | ||
1248 | |||
1249 | req->length = STATUS_BYTECOUNT; | ||
1250 | value = usb_ep_queue (ep, req, GFP_ATOMIC); | ||
1251 | DEBUG (dev, "send SPEED_CHANGE --> %d\n", value); | ||
1252 | if (value == 0) | ||
1253 | return; | ||
1254 | } else if (value != -ECONNRESET) | ||
1255 | DEBUG (dev, "event %02x --> %d\n", | ||
1256 | event->bNotificationType, value); | ||
1257 | req->context = NULL; | ||
1258 | } | 215 | } |
1259 | 216 | ||
1260 | static void issue_start_status (struct eth_dev *dev) | 217 | static struct usb_configuration rndis_config_driver = { |
1261 | { | 218 | .label = "RNDIS", |
1262 | struct usb_request *req = dev->stat_req; | 219 | .bind = rndis_do_config, |
1263 | struct usb_cdc_notification *event; | 220 | .bConfigurationValue = 2, |
1264 | int value; | 221 | /* .iConfiguration = DYNAMIC */ |
1265 | 222 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
1266 | DEBUG (dev, "%s, flush old status first\n", __func__); | 223 | .bMaxPower = 1, /* 2 mA, minimal */ |
1267 | 224 | }; | |
1268 | /* flush old status | ||
1269 | * | ||
1270 | * FIXME ugly idiom, maybe we'd be better with just | ||
1271 | * a "cancel the whole queue" primitive since any | ||
1272 | * unlink-one primitive has way too many error modes. | ||
1273 | * here, we "know" toggle is already clear... | ||
1274 | * | ||
1275 | * FIXME iff req->context != null just dequeue it | ||
1276 | */ | ||
1277 | usb_ep_disable (dev->status_ep); | ||
1278 | usb_ep_enable (dev->status_ep, dev->status); | ||
1279 | |||
1280 | /* 3.8.1 says to issue first NETWORK_CONNECTION, then | ||
1281 | * a SPEED_CHANGE. could be useful in some configs. | ||
1282 | */ | ||
1283 | event = req->buf; | ||
1284 | event->bmRequestType = 0xA1; | ||
1285 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; | ||
1286 | event->wValue = __constant_cpu_to_le16 (1); /* connected */ | ||
1287 | event->wIndex = __constant_cpu_to_le16 (1); | ||
1288 | event->wLength = 0; | ||
1289 | |||
1290 | req->length = sizeof *event; | ||
1291 | req->complete = eth_status_complete; | ||
1292 | req->context = dev; | ||
1293 | |||
1294 | value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); | ||
1295 | if (value < 0) | ||
1296 | DEBUG (dev, "status buf queue --> %d\n", value); | ||
1297 | } | ||
1298 | |||
1299 | #endif | ||
1300 | 225 | ||
1301 | /*-------------------------------------------------------------------------*/ | 226 | /*-------------------------------------------------------------------------*/ |
1302 | 227 | ||
1303 | static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) | ||
1304 | { | ||
1305 | if (req->status || req->actual != req->length) | ||
1306 | DEBUG ((struct eth_dev *) ep->driver_data, | ||
1307 | "setup complete --> %d, %d/%d\n", | ||
1308 | req->status, req->actual, req->length); | ||
1309 | } | ||
1310 | |||
1311 | #ifdef CONFIG_USB_ETH_RNDIS | ||
1312 | |||
1313 | static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req) | ||
1314 | { | ||
1315 | if (req->status || req->actual != req->length) | ||
1316 | DEBUG ((struct eth_dev *) ep->driver_data, | ||
1317 | "rndis response complete --> %d, %d/%d\n", | ||
1318 | req->status, req->actual, req->length); | ||
1319 | |||
1320 | /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ | ||
1321 | } | ||
1322 | |||
1323 | static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req) | ||
1324 | { | ||
1325 | struct eth_dev *dev = ep->driver_data; | ||
1326 | int status; | ||
1327 | |||
1328 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ | ||
1329 | spin_lock(&dev->lock); | ||
1330 | status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf); | ||
1331 | if (status < 0) | ||
1332 | ERROR(dev, "%s: rndis parse error %d\n", __func__, status); | ||
1333 | spin_unlock(&dev->lock); | ||
1334 | } | ||
1335 | |||
1336 | #endif /* RNDIS */ | ||
1337 | |||
1338 | /* | 228 | /* |
1339 | * The setup() callback implements all the ep0 functionality that's not | 229 | * We _always_ have an ECM or CDC Subset configuration. |
1340 | * handled lower down. CDC has a number of less-common features: | ||
1341 | * | ||
1342 | * - two interfaces: control, and ethernet data | ||
1343 | * - Ethernet data interface has two altsettings: default, and active | ||
1344 | * - class-specific descriptors for the control interface | ||
1345 | * - class-specific control requests | ||
1346 | */ | 230 | */ |
1347 | static int | 231 | static int __init eth_do_config(struct usb_configuration *c) |
1348 | eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | ||
1349 | { | 232 | { |
1350 | struct eth_dev *dev = get_gadget_data (gadget); | 233 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
1351 | struct usb_request *req = dev->req; | ||
1352 | int value = -EOPNOTSUPP; | ||
1353 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | ||
1354 | u16 wValue = le16_to_cpu(ctrl->wValue); | ||
1355 | u16 wLength = le16_to_cpu(ctrl->wLength); | ||
1356 | |||
1357 | /* descriptors just go into the pre-allocated ep0 buffer, | ||
1358 | * while config change events may enable network traffic. | ||
1359 | */ | ||
1360 | req->complete = eth_setup_complete; | ||
1361 | switch (ctrl->bRequest) { | ||
1362 | |||
1363 | case USB_REQ_GET_DESCRIPTOR: | ||
1364 | if (ctrl->bRequestType != USB_DIR_IN) | ||
1365 | break; | ||
1366 | switch (wValue >> 8) { | ||
1367 | |||
1368 | case USB_DT_DEVICE: | ||
1369 | value = min (wLength, (u16) sizeof device_desc); | ||
1370 | memcpy (req->buf, &device_desc, value); | ||
1371 | break; | ||
1372 | case USB_DT_DEVICE_QUALIFIER: | ||
1373 | if (!gadget_is_dualspeed(gadget)) | ||
1374 | break; | ||
1375 | value = min (wLength, (u16) sizeof dev_qualifier); | ||
1376 | memcpy (req->buf, &dev_qualifier, value); | ||
1377 | break; | ||
1378 | |||
1379 | case USB_DT_OTHER_SPEED_CONFIG: | ||
1380 | if (!gadget_is_dualspeed(gadget)) | ||
1381 | break; | ||
1382 | // FALLTHROUGH | ||
1383 | case USB_DT_CONFIG: | ||
1384 | value = config_buf(gadget, req->buf, | ||
1385 | wValue >> 8, | ||
1386 | wValue & 0xff, | ||
1387 | gadget_is_otg(gadget)); | ||
1388 | if (value >= 0) | ||
1389 | value = min (wLength, (u16) value); | ||
1390 | break; | ||
1391 | |||
1392 | case USB_DT_STRING: | ||
1393 | value = usb_gadget_get_string (&stringtab, | ||
1394 | wValue & 0xff, req->buf); | ||
1395 | if (value >= 0) | ||
1396 | value = min (wLength, (u16) value); | ||
1397 | break; | ||
1398 | } | ||
1399 | break; | ||
1400 | |||
1401 | case USB_REQ_SET_CONFIGURATION: | ||
1402 | if (ctrl->bRequestType != 0) | ||
1403 | break; | ||
1404 | if (gadget->a_hnp_support) | ||
1405 | DEBUG (dev, "HNP available\n"); | ||
1406 | else if (gadget->a_alt_hnp_support) | ||
1407 | DEBUG (dev, "HNP needs a different root port\n"); | ||
1408 | spin_lock (&dev->lock); | ||
1409 | value = eth_set_config (dev, wValue, GFP_ATOMIC); | ||
1410 | spin_unlock (&dev->lock); | ||
1411 | break; | ||
1412 | case USB_REQ_GET_CONFIGURATION: | ||
1413 | if (ctrl->bRequestType != USB_DIR_IN) | ||
1414 | break; | ||
1415 | *(u8 *)req->buf = dev->config; | ||
1416 | value = min (wLength, (u16) 1); | ||
1417 | break; | ||
1418 | |||
1419 | case USB_REQ_SET_INTERFACE: | ||
1420 | if (ctrl->bRequestType != USB_RECIP_INTERFACE | ||
1421 | || !dev->config | ||
1422 | || wIndex > 1) | ||
1423 | break; | ||
1424 | if (!cdc_active(dev) && wIndex != 0) | ||
1425 | break; | ||
1426 | spin_lock (&dev->lock); | ||
1427 | |||
1428 | /* PXA hardware partially handles SET_INTERFACE; | ||
1429 | * we need to kluge around that interference. | ||
1430 | */ | ||
1431 | if (gadget_is_pxa (gadget)) { | ||
1432 | value = eth_set_config (dev, DEV_CONFIG_VALUE, | ||
1433 | GFP_ATOMIC); | ||
1434 | goto done_set_intf; | ||
1435 | } | ||
1436 | |||
1437 | #ifdef DEV_CONFIG_CDC | ||
1438 | switch (wIndex) { | ||
1439 | case 0: /* control/master intf */ | ||
1440 | if (wValue != 0) | ||
1441 | break; | ||
1442 | if (dev->status) { | ||
1443 | usb_ep_disable (dev->status_ep); | ||
1444 | usb_ep_enable (dev->status_ep, dev->status); | ||
1445 | } | ||
1446 | value = 0; | ||
1447 | break; | ||
1448 | case 1: /* data intf */ | ||
1449 | if (wValue > 1) | ||
1450 | break; | ||
1451 | usb_ep_disable (dev->in_ep); | ||
1452 | usb_ep_disable (dev->out_ep); | ||
1453 | |||
1454 | /* CDC requires the data transfers not be done from | ||
1455 | * the default interface setting ... also, setting | ||
1456 | * the non-default interface resets filters etc. | ||
1457 | */ | ||
1458 | if (wValue == 1) { | ||
1459 | if (!cdc_active (dev)) | ||
1460 | break; | ||
1461 | usb_ep_enable (dev->in_ep, dev->in); | ||
1462 | usb_ep_enable (dev->out_ep, dev->out); | ||
1463 | dev->cdc_filter = DEFAULT_FILTER; | ||
1464 | netif_carrier_on (dev->net); | ||
1465 | if (dev->status) | ||
1466 | issue_start_status (dev); | ||
1467 | if (netif_running (dev->net)) { | ||
1468 | spin_unlock (&dev->lock); | ||
1469 | eth_start (dev, GFP_ATOMIC); | ||
1470 | spin_lock (&dev->lock); | ||
1471 | } | ||
1472 | } else { | ||
1473 | netif_stop_queue (dev->net); | ||
1474 | netif_carrier_off (dev->net); | ||
1475 | } | ||
1476 | value = 0; | ||
1477 | break; | ||
1478 | } | ||
1479 | #else | ||
1480 | /* FIXME this is wrong, as is the assumption that | ||
1481 | * all non-PXA hardware talks real CDC ... | ||
1482 | */ | ||
1483 | dev_warn (&gadget->dev, "set_interface ignored!\n"); | ||
1484 | #endif /* DEV_CONFIG_CDC */ | ||
1485 | |||
1486 | done_set_intf: | ||
1487 | spin_unlock (&dev->lock); | ||
1488 | break; | ||
1489 | case USB_REQ_GET_INTERFACE: | ||
1490 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) | ||
1491 | || !dev->config | ||
1492 | || wIndex > 1) | ||
1493 | break; | ||
1494 | if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) | ||
1495 | break; | ||
1496 | |||
1497 | /* for CDC, iff carrier is on, data interface is active. */ | ||
1498 | if (rndis_active(dev) || wIndex != 1) | ||
1499 | *(u8 *)req->buf = 0; | ||
1500 | else | ||
1501 | *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; | ||
1502 | value = min (wLength, (u16) 1); | ||
1503 | break; | ||
1504 | |||
1505 | #ifdef DEV_CONFIG_CDC | ||
1506 | case USB_CDC_SET_ETHERNET_PACKET_FILTER: | ||
1507 | /* see 6.2.30: no data, wIndex = interface, | ||
1508 | * wValue = packet filter bitmap | ||
1509 | */ | ||
1510 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | ||
1511 | || !cdc_active(dev) | ||
1512 | || wLength != 0 | ||
1513 | || wIndex > 1) | ||
1514 | break; | ||
1515 | DEBUG (dev, "packet filter %02x\n", wValue); | ||
1516 | dev->cdc_filter = wValue; | ||
1517 | value = 0; | ||
1518 | break; | ||
1519 | |||
1520 | /* and potentially: | ||
1521 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: | ||
1522 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: | ||
1523 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: | ||
1524 | * case USB_CDC_GET_ETHERNET_STATISTIC: | ||
1525 | */ | ||
1526 | |||
1527 | #endif /* DEV_CONFIG_CDC */ | ||
1528 | 234 | ||
1529 | #ifdef CONFIG_USB_ETH_RNDIS | 235 | if (gadget_is_otg(c->cdev->gadget)) { |
1530 | /* RNDIS uses the CDC command encapsulation mechanism to implement | 236 | c->descriptors = otg_desc; |
1531 | * an RPC scheme, with much getting/setting of attributes by OID. | 237 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
1532 | */ | ||
1533 | case USB_CDC_SEND_ENCAPSULATED_COMMAND: | ||
1534 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | ||
1535 | || !rndis_active(dev) | ||
1536 | || wLength > USB_BUFSIZ | ||
1537 | || wValue | ||
1538 | || rndis_control_intf.bInterfaceNumber | ||
1539 | != wIndex) | ||
1540 | break; | ||
1541 | /* read the request, then process it */ | ||
1542 | value = wLength; | ||
1543 | req->complete = rndis_command_complete; | ||
1544 | /* later, rndis_control_ack () sends a notification */ | ||
1545 | break; | ||
1546 | |||
1547 | case USB_CDC_GET_ENCAPSULATED_RESPONSE: | ||
1548 | if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) | ||
1549 | == ctrl->bRequestType | ||
1550 | && rndis_active(dev) | ||
1551 | // && wLength >= 0x0400 | ||
1552 | && !wValue | ||
1553 | && rndis_control_intf.bInterfaceNumber | ||
1554 | == wIndex) { | ||
1555 | u8 *buf; | ||
1556 | u32 n; | ||
1557 | |||
1558 | /* return the result */ | ||
1559 | buf = rndis_get_next_response(dev->rndis_config, &n); | ||
1560 | if (buf) { | ||
1561 | memcpy(req->buf, buf, n); | ||
1562 | req->complete = rndis_response_complete; | ||
1563 | rndis_free_response(dev->rndis_config, buf); | ||
1564 | value = n; | ||
1565 | } | ||
1566 | /* else stalls ... spec says to avoid that */ | ||
1567 | } | ||
1568 | break; | ||
1569 | #endif /* RNDIS */ | ||
1570 | |||
1571 | default: | ||
1572 | VDEBUG (dev, | ||
1573 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | ||
1574 | ctrl->bRequestType, ctrl->bRequest, | ||
1575 | wValue, wIndex, wLength); | ||
1576 | } | 238 | } |
1577 | 239 | ||
1578 | /* respond with data transfer before status phase? */ | 240 | if (can_support_ecm(c->cdev->gadget)) |
1579 | if (value >= 0) { | 241 | return ecm_bind_config(c, hostaddr); |
1580 | req->length = value; | ||
1581 | req->zero = value < wLength | ||
1582 | && (value % gadget->ep0->maxpacket) == 0; | ||
1583 | value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); | ||
1584 | if (value < 0) { | ||
1585 | DEBUG (dev, "ep_queue --> %d\n", value); | ||
1586 | req->status = 0; | ||
1587 | eth_setup_complete (gadget->ep0, req); | ||
1588 | } | ||
1589 | } | ||
1590 | |||
1591 | /* host either stalls (value < 0) or reports success */ | ||
1592 | return value; | ||
1593 | } | ||
1594 | |||
1595 | static void | ||
1596 | eth_disconnect (struct usb_gadget *gadget) | ||
1597 | { | ||
1598 | struct eth_dev *dev = get_gadget_data (gadget); | ||
1599 | unsigned long flags; | ||
1600 | |||
1601 | spin_lock_irqsave (&dev->lock, flags); | ||
1602 | netif_stop_queue (dev->net); | ||
1603 | netif_carrier_off (dev->net); | ||
1604 | eth_reset_config (dev); | ||
1605 | spin_unlock_irqrestore (&dev->lock, flags); | ||
1606 | |||
1607 | /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ | ||
1608 | |||
1609 | /* next we may get setup() calls to enumerate new connections; | ||
1610 | * or an unbind() during shutdown (including removing module). | ||
1611 | */ | ||
1612 | } | ||
1613 | |||
1614 | /*-------------------------------------------------------------------------*/ | ||
1615 | |||
1616 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ | ||
1617 | |||
1618 | static int eth_change_mtu (struct net_device *net, int new_mtu) | ||
1619 | { | ||
1620 | struct eth_dev *dev = netdev_priv(net); | ||
1621 | |||
1622 | if (dev->rndis) | ||
1623 | return -EBUSY; | ||
1624 | |||
1625 | if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) | ||
1626 | return -ERANGE; | ||
1627 | /* no zero-length packet read wanted after mtu-sized packets */ | ||
1628 | if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0) | ||
1629 | return -EDOM; | ||
1630 | net->mtu = new_mtu; | ||
1631 | return 0; | ||
1632 | } | ||
1633 | |||
1634 | static struct net_device_stats *eth_get_stats (struct net_device *net) | ||
1635 | { | ||
1636 | return &((struct eth_dev *)netdev_priv(net))->stats; | ||
1637 | } | ||
1638 | |||
1639 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) | ||
1640 | { | ||
1641 | struct eth_dev *dev = netdev_priv(net); | ||
1642 | strlcpy(p->driver, shortname, sizeof p->driver); | ||
1643 | strlcpy(p->version, DRIVER_VERSION, sizeof p->version); | ||
1644 | strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version); | ||
1645 | strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info); | ||
1646 | } | ||
1647 | |||
1648 | static u32 eth_get_link(struct net_device *net) | ||
1649 | { | ||
1650 | struct eth_dev *dev = netdev_priv(net); | ||
1651 | return dev->gadget->speed != USB_SPEED_UNKNOWN; | ||
1652 | } | ||
1653 | |||
1654 | static struct ethtool_ops ops = { | ||
1655 | .get_drvinfo = eth_get_drvinfo, | ||
1656 | .get_link = eth_get_link | ||
1657 | }; | ||
1658 | |||
1659 | static void defer_kevent (struct eth_dev *dev, int flag) | ||
1660 | { | ||
1661 | if (test_and_set_bit (flag, &dev->todo)) | ||
1662 | return; | ||
1663 | if (!schedule_work (&dev->work)) | ||
1664 | ERROR (dev, "kevent %d may have been dropped\n", flag); | ||
1665 | else | 242 | else |
1666 | DEBUG (dev, "kevent %d scheduled\n", flag); | 243 | return geth_bind_config(c, hostaddr); |
1667 | } | ||
1668 | |||
1669 | static void rx_complete (struct usb_ep *ep, struct usb_request *req); | ||
1670 | |||
1671 | static int | ||
1672 | rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) | ||
1673 | { | ||
1674 | struct sk_buff *skb; | ||
1675 | int retval = -ENOMEM; | ||
1676 | size_t size; | ||
1677 | |||
1678 | /* Padding up to RX_EXTRA handles minor disagreements with host. | ||
1679 | * Normally we use the USB "terminate on short read" convention; | ||
1680 | * so allow up to (N*maxpacket), since that memory is normally | ||
1681 | * already allocated. Some hardware doesn't deal well with short | ||
1682 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a | ||
1683 | * byte off the end (to force hardware errors on overflow). | ||
1684 | * | ||
1685 | * RNDIS uses internal framing, and explicitly allows senders to | ||
1686 | * pad to end-of-packet. That's potentially nice for speed, | ||
1687 | * but means receivers can't recover synch on their own. | ||
1688 | */ | ||
1689 | size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA); | ||
1690 | size += dev->out_ep->maxpacket - 1; | ||
1691 | if (rndis_active(dev)) | ||
1692 | size += sizeof (struct rndis_packet_msg_type); | ||
1693 | size -= size % dev->out_ep->maxpacket; | ||
1694 | |||
1695 | skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); | ||
1696 | if (skb == NULL) { | ||
1697 | DEBUG (dev, "no rx skb\n"); | ||
1698 | goto enomem; | ||
1699 | } | ||
1700 | |||
1701 | /* Some platforms perform better when IP packets are aligned, | ||
1702 | * but on at least one, checksumming fails otherwise. Note: | ||
1703 | * RNDIS headers involve variable numbers of LE32 values. | ||
1704 | */ | ||
1705 | skb_reserve(skb, NET_IP_ALIGN); | ||
1706 | |||
1707 | req->buf = skb->data; | ||
1708 | req->length = size; | ||
1709 | req->complete = rx_complete; | ||
1710 | req->context = skb; | ||
1711 | |||
1712 | retval = usb_ep_queue (dev->out_ep, req, gfp_flags); | ||
1713 | if (retval == -ENOMEM) | ||
1714 | enomem: | ||
1715 | defer_kevent (dev, WORK_RX_MEMORY); | ||
1716 | if (retval) { | ||
1717 | DEBUG (dev, "rx submit --> %d\n", retval); | ||
1718 | if (skb) | ||
1719 | dev_kfree_skb_any(skb); | ||
1720 | spin_lock(&dev->req_lock); | ||
1721 | list_add (&req->list, &dev->rx_reqs); | ||
1722 | spin_unlock(&dev->req_lock); | ||
1723 | } | ||
1724 | return retval; | ||
1725 | } | ||
1726 | |||
1727 | static void rx_complete (struct usb_ep *ep, struct usb_request *req) | ||
1728 | { | ||
1729 | struct sk_buff *skb = req->context; | ||
1730 | struct eth_dev *dev = ep->driver_data; | ||
1731 | int status = req->status; | ||
1732 | |||
1733 | switch (status) { | ||
1734 | |||
1735 | /* normal completion */ | ||
1736 | case 0: | ||
1737 | skb_put (skb, req->actual); | ||
1738 | /* we know MaxPacketsPerTransfer == 1 here */ | ||
1739 | if (rndis_active(dev)) | ||
1740 | status = rndis_rm_hdr (skb); | ||
1741 | if (status < 0 | ||
1742 | || ETH_HLEN > skb->len | ||
1743 | || skb->len > ETH_FRAME_LEN) { | ||
1744 | dev->stats.rx_errors++; | ||
1745 | dev->stats.rx_length_errors++; | ||
1746 | DEBUG (dev, "rx length %d\n", skb->len); | ||
1747 | break; | ||
1748 | } | ||
1749 | |||
1750 | skb->protocol = eth_type_trans (skb, dev->net); | ||
1751 | dev->stats.rx_packets++; | ||
1752 | dev->stats.rx_bytes += skb->len; | ||
1753 | |||
1754 | /* no buffer copies needed, unless hardware can't | ||
1755 | * use skb buffers. | ||
1756 | */ | ||
1757 | status = netif_rx (skb); | ||
1758 | skb = NULL; | ||
1759 | break; | ||
1760 | |||
1761 | /* software-driven interface shutdown */ | ||
1762 | case -ECONNRESET: // unlink | ||
1763 | case -ESHUTDOWN: // disconnect etc | ||
1764 | VDEBUG (dev, "rx shutdown, code %d\n", status); | ||
1765 | goto quiesce; | ||
1766 | |||
1767 | /* for hardware automagic (such as pxa) */ | ||
1768 | case -ECONNABORTED: // endpoint reset | ||
1769 | DEBUG (dev, "rx %s reset\n", ep->name); | ||
1770 | defer_kevent (dev, WORK_RX_MEMORY); | ||
1771 | quiesce: | ||
1772 | dev_kfree_skb_any (skb); | ||
1773 | goto clean; | ||
1774 | |||
1775 | /* data overrun */ | ||
1776 | case -EOVERFLOW: | ||
1777 | dev->stats.rx_over_errors++; | ||
1778 | // FALLTHROUGH | ||
1779 | |||
1780 | default: | ||
1781 | dev->stats.rx_errors++; | ||
1782 | DEBUG (dev, "rx status %d\n", status); | ||
1783 | break; | ||
1784 | } | ||
1785 | |||
1786 | if (skb) | ||
1787 | dev_kfree_skb_any (skb); | ||
1788 | if (!netif_running (dev->net)) { | ||
1789 | clean: | ||
1790 | spin_lock(&dev->req_lock); | ||
1791 | list_add (&req->list, &dev->rx_reqs); | ||
1792 | spin_unlock(&dev->req_lock); | ||
1793 | req = NULL; | ||
1794 | } | ||
1795 | if (req) | ||
1796 | rx_submit (dev, req, GFP_ATOMIC); | ||
1797 | } | ||
1798 | |||
1799 | static int prealloc (struct list_head *list, struct usb_ep *ep, | ||
1800 | unsigned n, gfp_t gfp_flags) | ||
1801 | { | ||
1802 | unsigned i; | ||
1803 | struct usb_request *req; | ||
1804 | |||
1805 | if (!n) | ||
1806 | return -ENOMEM; | ||
1807 | |||
1808 | /* queue/recycle up to N requests */ | ||
1809 | i = n; | ||
1810 | list_for_each_entry (req, list, list) { | ||
1811 | if (i-- == 0) | ||
1812 | goto extra; | ||
1813 | } | ||
1814 | while (i--) { | ||
1815 | req = usb_ep_alloc_request (ep, gfp_flags); | ||
1816 | if (!req) | ||
1817 | return list_empty (list) ? -ENOMEM : 0; | ||
1818 | list_add (&req->list, list); | ||
1819 | } | ||
1820 | return 0; | ||
1821 | |||
1822 | extra: | ||
1823 | /* free extras */ | ||
1824 | for (;;) { | ||
1825 | struct list_head *next; | ||
1826 | |||
1827 | next = req->list.next; | ||
1828 | list_del (&req->list); | ||
1829 | usb_ep_free_request (ep, req); | ||
1830 | |||
1831 | if (next == list) | ||
1832 | break; | ||
1833 | |||
1834 | req = container_of (next, struct usb_request, list); | ||
1835 | } | ||
1836 | return 0; | ||
1837 | } | ||
1838 | |||
1839 | static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) | ||
1840 | { | ||
1841 | int status; | ||
1842 | |||
1843 | spin_lock(&dev->req_lock); | ||
1844 | status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags); | ||
1845 | if (status < 0) | ||
1846 | goto fail; | ||
1847 | status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags); | ||
1848 | if (status < 0) | ||
1849 | goto fail; | ||
1850 | goto done; | ||
1851 | fail: | ||
1852 | DEBUG (dev, "can't alloc requests\n"); | ||
1853 | done: | ||
1854 | spin_unlock(&dev->req_lock); | ||
1855 | return status; | ||
1856 | } | ||
1857 | |||
1858 | static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags) | ||
1859 | { | ||
1860 | struct usb_request *req; | ||
1861 | unsigned long flags; | ||
1862 | |||
1863 | /* fill unused rxq slots with some skb */ | ||
1864 | spin_lock_irqsave(&dev->req_lock, flags); | ||
1865 | while (!list_empty (&dev->rx_reqs)) { | ||
1866 | req = container_of (dev->rx_reqs.next, | ||
1867 | struct usb_request, list); | ||
1868 | list_del_init (&req->list); | ||
1869 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
1870 | |||
1871 | if (rx_submit (dev, req, gfp_flags) < 0) { | ||
1872 | defer_kevent (dev, WORK_RX_MEMORY); | ||
1873 | return; | ||
1874 | } | ||
1875 | |||
1876 | spin_lock_irqsave(&dev->req_lock, flags); | ||
1877 | } | ||
1878 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
1879 | } | ||
1880 | |||
1881 | static void eth_work (struct work_struct *work) | ||
1882 | { | ||
1883 | struct eth_dev *dev = container_of(work, struct eth_dev, work); | ||
1884 | |||
1885 | if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) { | ||
1886 | if (netif_running (dev->net)) | ||
1887 | rx_fill (dev, GFP_KERNEL); | ||
1888 | } | ||
1889 | |||
1890 | if (dev->todo) | ||
1891 | DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo); | ||
1892 | } | ||
1893 | |||
1894 | static void tx_complete (struct usb_ep *ep, struct usb_request *req) | ||
1895 | { | ||
1896 | struct sk_buff *skb = req->context; | ||
1897 | struct eth_dev *dev = ep->driver_data; | ||
1898 | |||
1899 | switch (req->status) { | ||
1900 | default: | ||
1901 | dev->stats.tx_errors++; | ||
1902 | VDEBUG (dev, "tx err %d\n", req->status); | ||
1903 | /* FALLTHROUGH */ | ||
1904 | case -ECONNRESET: // unlink | ||
1905 | case -ESHUTDOWN: // disconnect etc | ||
1906 | break; | ||
1907 | case 0: | ||
1908 | dev->stats.tx_bytes += skb->len; | ||
1909 | } | ||
1910 | dev->stats.tx_packets++; | ||
1911 | |||
1912 | spin_lock(&dev->req_lock); | ||
1913 | list_add (&req->list, &dev->tx_reqs); | ||
1914 | spin_unlock(&dev->req_lock); | ||
1915 | dev_kfree_skb_any (skb); | ||
1916 | |||
1917 | atomic_dec (&dev->tx_qlen); | ||
1918 | if (netif_carrier_ok (dev->net)) | ||
1919 | netif_wake_queue (dev->net); | ||
1920 | } | ||
1921 | |||
1922 | static inline int eth_is_promisc (struct eth_dev *dev) | ||
1923 | { | ||
1924 | /* no filters for the CDC subset; always promisc */ | ||
1925 | if (subset_active (dev)) | ||
1926 | return 1; | ||
1927 | return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; | ||
1928 | } | ||
1929 | |||
1930 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) | ||
1931 | { | ||
1932 | struct eth_dev *dev = netdev_priv(net); | ||
1933 | int length = skb->len; | ||
1934 | int retval; | ||
1935 | struct usb_request *req = NULL; | ||
1936 | unsigned long flags; | ||
1937 | |||
1938 | /* apply outgoing CDC or RNDIS filters */ | ||
1939 | if (!eth_is_promisc (dev)) { | ||
1940 | u8 *dest = skb->data; | ||
1941 | |||
1942 | if (is_multicast_ether_addr(dest)) { | ||
1943 | u16 type; | ||
1944 | |||
1945 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host | ||
1946 | * SET_ETHERNET_MULTICAST_FILTERS requests | ||
1947 | */ | ||
1948 | if (is_broadcast_ether_addr(dest)) | ||
1949 | type = USB_CDC_PACKET_TYPE_BROADCAST; | ||
1950 | else | ||
1951 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; | ||
1952 | if (!(dev->cdc_filter & type)) { | ||
1953 | dev_kfree_skb_any (skb); | ||
1954 | return 0; | ||
1955 | } | ||
1956 | } | ||
1957 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ | ||
1958 | } | ||
1959 | |||
1960 | spin_lock_irqsave(&dev->req_lock, flags); | ||
1961 | /* | ||
1962 | * this freelist can be empty if an interrupt triggered disconnect() | ||
1963 | * and reconfigured the gadget (shutting down this queue) after the | ||
1964 | * network stack decided to xmit but before we got the spinlock. | ||
1965 | */ | ||
1966 | if (list_empty(&dev->tx_reqs)) { | ||
1967 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
1968 | return 1; | ||
1969 | } | ||
1970 | |||
1971 | req = container_of (dev->tx_reqs.next, struct usb_request, list); | ||
1972 | list_del (&req->list); | ||
1973 | |||
1974 | /* temporarily stop TX queue when the freelist empties */ | ||
1975 | if (list_empty (&dev->tx_reqs)) | ||
1976 | netif_stop_queue (net); | ||
1977 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
1978 | |||
1979 | /* no buffer copies needed, unless the network stack did it | ||
1980 | * or the hardware can't use skb buffers. | ||
1981 | * or there's not enough space for any RNDIS headers we need | ||
1982 | */ | ||
1983 | if (rndis_active(dev)) { | ||
1984 | struct sk_buff *skb_rndis; | ||
1985 | |||
1986 | skb_rndis = skb_realloc_headroom (skb, | ||
1987 | sizeof (struct rndis_packet_msg_type)); | ||
1988 | if (!skb_rndis) | ||
1989 | goto drop; | ||
1990 | |||
1991 | dev_kfree_skb_any (skb); | ||
1992 | skb = skb_rndis; | ||
1993 | rndis_add_hdr (skb); | ||
1994 | length = skb->len; | ||
1995 | } | ||
1996 | req->buf = skb->data; | ||
1997 | req->context = skb; | ||
1998 | req->complete = tx_complete; | ||
1999 | |||
2000 | /* use zlp framing on tx for strict CDC-Ether conformance, | ||
2001 | * though any robust network rx path ignores extra padding. | ||
2002 | * and some hardware doesn't like to write zlps. | ||
2003 | */ | ||
2004 | req->zero = 1; | ||
2005 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) | ||
2006 | length++; | ||
2007 | |||
2008 | req->length = length; | ||
2009 | |||
2010 | /* throttle highspeed IRQ rate back slightly */ | ||
2011 | if (gadget_is_dualspeed(dev->gadget)) | ||
2012 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) | ||
2013 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) | ||
2014 | : 0; | ||
2015 | |||
2016 | retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); | ||
2017 | switch (retval) { | ||
2018 | default: | ||
2019 | DEBUG (dev, "tx queue err %d\n", retval); | ||
2020 | break; | ||
2021 | case 0: | ||
2022 | net->trans_start = jiffies; | ||
2023 | atomic_inc (&dev->tx_qlen); | ||
2024 | } | ||
2025 | |||
2026 | if (retval) { | ||
2027 | drop: | ||
2028 | dev->stats.tx_dropped++; | ||
2029 | dev_kfree_skb_any (skb); | ||
2030 | spin_lock_irqsave(&dev->req_lock, flags); | ||
2031 | if (list_empty (&dev->tx_reqs)) | ||
2032 | netif_start_queue (net); | ||
2033 | list_add (&req->list, &dev->tx_reqs); | ||
2034 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
2035 | } | ||
2036 | return 0; | ||
2037 | } | ||
2038 | |||
2039 | /*-------------------------------------------------------------------------*/ | ||
2040 | |||
2041 | #ifdef CONFIG_USB_ETH_RNDIS | ||
2042 | |||
2043 | /* The interrupt endpoint is used in RNDIS to notify the host when messages | ||
2044 | * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT | ||
2045 | * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even | ||
2046 | * REMOTE_NDIS_KEEPALIVE_MSG. | ||
2047 | * | ||
2048 | * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and | ||
2049 | * normally just one notification will be queued. | ||
2050 | */ | ||
2051 | |||
2052 | static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t); | ||
2053 | static void eth_req_free (struct usb_ep *ep, struct usb_request *req); | ||
2054 | |||
2055 | static void | ||
2056 | rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req) | ||
2057 | { | ||
2058 | struct eth_dev *dev = ep->driver_data; | ||
2059 | |||
2060 | if (req->status || req->actual != req->length) | ||
2061 | DEBUG (dev, | ||
2062 | "rndis control ack complete --> %d, %d/%d\n", | ||
2063 | req->status, req->actual, req->length); | ||
2064 | req->context = NULL; | ||
2065 | |||
2066 | if (req != dev->stat_req) | ||
2067 | eth_req_free(ep, req); | ||
2068 | } | ||
2069 | |||
2070 | static int rndis_control_ack (struct net_device *net) | ||
2071 | { | ||
2072 | struct eth_dev *dev = netdev_priv(net); | ||
2073 | int length; | ||
2074 | struct usb_request *resp = dev->stat_req; | ||
2075 | |||
2076 | /* in case RNDIS calls this after disconnect */ | ||
2077 | if (!dev->status) { | ||
2078 | DEBUG (dev, "status ENODEV\n"); | ||
2079 | return -ENODEV; | ||
2080 | } | ||
2081 | |||
2082 | /* in case queue length > 1 */ | ||
2083 | if (resp->context) { | ||
2084 | resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC); | ||
2085 | if (!resp) | ||
2086 | return -ENOMEM; | ||
2087 | } | ||
2088 | |||
2089 | /* Send RNDIS RESPONSE_AVAILABLE notification; | ||
2090 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too | ||
2091 | */ | ||
2092 | resp->length = 8; | ||
2093 | resp->complete = rndis_control_ack_complete; | ||
2094 | resp->context = dev; | ||
2095 | |||
2096 | *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1); | ||
2097 | *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0); | ||
2098 | |||
2099 | length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC); | ||
2100 | if (length < 0) { | ||
2101 | resp->status = 0; | ||
2102 | rndis_control_ack_complete (dev->status_ep, resp); | ||
2103 | } | ||
2104 | |||
2105 | return 0; | ||
2106 | } | ||
2107 | |||
2108 | #else | ||
2109 | |||
2110 | #define rndis_control_ack NULL | ||
2111 | |||
2112 | #endif /* RNDIS */ | ||
2113 | |||
2114 | static void eth_start (struct eth_dev *dev, gfp_t gfp_flags) | ||
2115 | { | ||
2116 | DEBUG (dev, "%s\n", __func__); | ||
2117 | |||
2118 | /* fill the rx queue */ | ||
2119 | rx_fill (dev, gfp_flags); | ||
2120 | |||
2121 | /* and open the tx floodgates */ | ||
2122 | atomic_set (&dev->tx_qlen, 0); | ||
2123 | netif_wake_queue (dev->net); | ||
2124 | if (rndis_active(dev)) { | ||
2125 | rndis_set_param_medium (dev->rndis_config, | ||
2126 | NDIS_MEDIUM_802_3, | ||
2127 | BITRATE(dev->gadget)/100); | ||
2128 | (void) rndis_signal_connect (dev->rndis_config); | ||
2129 | } | ||
2130 | } | ||
2131 | |||
2132 | static int eth_open (struct net_device *net) | ||
2133 | { | ||
2134 | struct eth_dev *dev = netdev_priv(net); | ||
2135 | |||
2136 | DEBUG (dev, "%s\n", __func__); | ||
2137 | if (netif_carrier_ok (dev->net)) | ||
2138 | eth_start (dev, GFP_KERNEL); | ||
2139 | return 0; | ||
2140 | } | 244 | } |
2141 | 245 | ||
2142 | static int eth_stop (struct net_device *net) | 246 | static struct usb_configuration eth_config_driver = { |
2143 | { | 247 | /* .label = f(hardware) */ |
2144 | struct eth_dev *dev = netdev_priv(net); | 248 | .bind = eth_do_config, |
2145 | 249 | .bConfigurationValue = 1, | |
2146 | VDEBUG (dev, "%s\n", __func__); | 250 | /* .iConfiguration = DYNAMIC */ |
2147 | netif_stop_queue (net); | 251 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
2148 | 252 | .bMaxPower = 1, /* 2 mA, minimal */ | |
2149 | DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", | 253 | }; |
2150 | dev->stats.rx_packets, dev->stats.tx_packets, | ||
2151 | dev->stats.rx_errors, dev->stats.tx_errors | ||
2152 | ); | ||
2153 | |||
2154 | /* ensure there are no more active requests */ | ||
2155 | if (dev->config) { | ||
2156 | usb_ep_disable (dev->in_ep); | ||
2157 | usb_ep_disable (dev->out_ep); | ||
2158 | if (netif_carrier_ok (dev->net)) { | ||
2159 | DEBUG (dev, "host still using in/out endpoints\n"); | ||
2160 | // FIXME idiom may leave toggle wrong here | ||
2161 | usb_ep_enable (dev->in_ep, dev->in); | ||
2162 | usb_ep_enable (dev->out_ep, dev->out); | ||
2163 | } | ||
2164 | if (dev->status_ep) { | ||
2165 | usb_ep_disable (dev->status_ep); | ||
2166 | usb_ep_enable (dev->status_ep, dev->status); | ||
2167 | } | ||
2168 | } | ||
2169 | |||
2170 | if (rndis_active(dev)) { | ||
2171 | rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); | ||
2172 | (void) rndis_signal_disconnect (dev->rndis_config); | ||
2173 | } | ||
2174 | |||
2175 | return 0; | ||
2176 | } | ||
2177 | 254 | ||
2178 | /*-------------------------------------------------------------------------*/ | 255 | /*-------------------------------------------------------------------------*/ |
2179 | 256 | ||
2180 | static struct usb_request * | 257 | static int __init eth_bind(struct usb_composite_dev *cdev) |
2181 | eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags) | ||
2182 | { | 258 | { |
2183 | struct usb_request *req; | 259 | int gcnum; |
2184 | 260 | struct usb_gadget *gadget = cdev->gadget; | |
2185 | req = usb_ep_alloc_request (ep, gfp_flags); | 261 | int status; |
2186 | if (!req) | ||
2187 | return NULL; | ||
2188 | |||
2189 | req->buf = kmalloc (size, gfp_flags); | ||
2190 | if (!req->buf) { | ||
2191 | usb_ep_free_request (ep, req); | ||
2192 | req = NULL; | ||
2193 | } | ||
2194 | return req; | ||
2195 | } | ||
2196 | |||
2197 | static void | ||
2198 | eth_req_free (struct usb_ep *ep, struct usb_request *req) | ||
2199 | { | ||
2200 | kfree (req->buf); | ||
2201 | usb_ep_free_request (ep, req); | ||
2202 | } | ||
2203 | |||
2204 | |||
2205 | static void /* __init_or_exit */ | ||
2206 | eth_unbind (struct usb_gadget *gadget) | ||
2207 | { | ||
2208 | struct eth_dev *dev = get_gadget_data (gadget); | ||
2209 | |||
2210 | DEBUG (dev, "unbind\n"); | ||
2211 | rndis_deregister (dev->rndis_config); | ||
2212 | rndis_exit (); | ||
2213 | |||
2214 | /* we've already been disconnected ... no i/o is active */ | ||
2215 | if (dev->req) { | ||
2216 | eth_req_free (gadget->ep0, dev->req); | ||
2217 | dev->req = NULL; | ||
2218 | } | ||
2219 | if (dev->stat_req) { | ||
2220 | eth_req_free (dev->status_ep, dev->stat_req); | ||
2221 | dev->stat_req = NULL; | ||
2222 | } | ||
2223 | |||
2224 | unregister_netdev (dev->net); | ||
2225 | free_netdev(dev->net); | ||
2226 | |||
2227 | /* assuming we used keventd, it must quiesce too */ | ||
2228 | flush_scheduled_work (); | ||
2229 | set_gadget_data (gadget, NULL); | ||
2230 | } | ||
2231 | |||
2232 | static u8 __init nibble (unsigned char c) | ||
2233 | { | ||
2234 | if (likely (isdigit (c))) | ||
2235 | return c - '0'; | ||
2236 | c = toupper (c); | ||
2237 | if (likely (isxdigit (c))) | ||
2238 | return 10 + c - 'A'; | ||
2239 | return 0; | ||
2240 | } | ||
2241 | 262 | ||
2242 | static int __init get_ether_addr(const char *str, u8 *dev_addr) | 263 | /* set up network link layer */ |
2243 | { | 264 | status = gether_setup(cdev->gadget, hostaddr); |
2244 | if (str) { | 265 | if (status < 0) |
2245 | unsigned i; | 266 | return status; |
2246 | 267 | ||
2247 | for (i = 0; i < 6; i++) { | 268 | /* set up main config label and device descriptor */ |
2248 | unsigned char num; | 269 | if (can_support_ecm(cdev->gadget)) { |
270 | /* ECM */ | ||
271 | eth_config_driver.label = "CDC Ethernet (ECM)"; | ||
272 | } else { | ||
273 | /* CDC Subset */ | ||
274 | eth_config_driver.label = "CDC Subset/SAFE"; | ||
2249 | 275 | ||
2250 | if((*str == '.') || (*str == ':')) | 276 | device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM), |
2251 | str++; | 277 | device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM), |
2252 | num = nibble(*str++) << 4; | 278 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
2253 | num |= (nibble(*str++)); | ||
2254 | dev_addr [i] = num; | ||
2255 | } | ||
2256 | if (is_valid_ether_addr (dev_addr)) | ||
2257 | return 0; | ||
2258 | } | 279 | } |
2259 | random_ether_addr(dev_addr); | ||
2260 | return 1; | ||
2261 | } | ||
2262 | |||
2263 | static int __init | ||
2264 | eth_bind (struct usb_gadget *gadget) | ||
2265 | { | ||
2266 | struct eth_dev *dev; | ||
2267 | struct net_device *net; | ||
2268 | u8 cdc = 1, zlp = 1, rndis = 1; | ||
2269 | struct usb_ep *in_ep, *out_ep, *status_ep = NULL; | ||
2270 | int status = -ENOMEM; | ||
2271 | int gcnum; | ||
2272 | |||
2273 | /* these flags are only ever cleared; compiler take note */ | ||
2274 | #ifndef DEV_CONFIG_CDC | ||
2275 | cdc = 0; | ||
2276 | #endif | ||
2277 | #ifndef CONFIG_USB_ETH_RNDIS | ||
2278 | rndis = 0; | ||
2279 | #endif | ||
2280 | 280 | ||
2281 | /* Because most host side USB stacks handle CDC Ethernet, that | 281 | if (has_rndis()) { |
2282 | * standard protocol is _strongly_ preferred for interop purposes. | 282 | /* RNDIS plus ECM-or-Subset */ |
2283 | * (By everyone except Microsoft.) | 283 | device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM), |
2284 | */ | 284 | device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM), |
2285 | if (gadget_is_pxa (gadget)) { | 285 | device_desc.bNumConfigurations = 2; |
2286 | /* pxa doesn't support altsettings */ | ||
2287 | cdc = 0; | ||
2288 | } else if (gadget_is_musbhdrc(gadget)) { | ||
2289 | /* reduce tx dma overhead by avoiding special cases */ | ||
2290 | zlp = 0; | ||
2291 | } else if (gadget_is_sh(gadget)) { | ||
2292 | /* sh doesn't support multiple interfaces or configs */ | ||
2293 | cdc = 0; | ||
2294 | rndis = 0; | ||
2295 | } else if (gadget_is_sa1100 (gadget)) { | ||
2296 | /* hardware can't write zlps */ | ||
2297 | zlp = 0; | ||
2298 | /* sa1100 CAN do CDC, without status endpoint ... we use | ||
2299 | * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". | ||
2300 | */ | ||
2301 | cdc = 0; | ||
2302 | } | 286 | } |
2303 | 287 | ||
2304 | gcnum = usb_gadget_controller_number (gadget); | 288 | gcnum = usb_gadget_controller_number(gadget); |
2305 | if (gcnum >= 0) | 289 | if (gcnum >= 0) |
2306 | device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum); | 290 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
2307 | else { | 291 | else { |
2308 | /* can't assume CDC works. don't want to default to | 292 | /* We assume that can_support_ecm() tells the truth; |
2309 | * anything less functional on CDC-capable hardware, | 293 | * but if the controller isn't recognized at all then |
2310 | * so we fail in this case. | 294 | * that assumption is a bit more likely to be wrong. |
2311 | */ | 295 | */ |
2312 | dev_err (&gadget->dev, | 296 | WARN(cdev, "controller '%s' not recognized; trying %s\n", |
2313 | "controller '%s' not recognized\n", | 297 | gadget->name, |
2314 | gadget->name); | 298 | eth_config_driver.label); |
2315 | return -ENODEV; | 299 | device_desc.bcdDevice = |
2316 | } | 300 | __constant_cpu_to_le16(0x0300 | 0x0099); |
2317 | snprintf (manufacturer, sizeof manufacturer, "%s %s/%s", | ||
2318 | init_utsname()->sysname, init_utsname()->release, | ||
2319 | gadget->name); | ||
2320 | |||
2321 | /* If there's an RNDIS configuration, that's what Windows wants to | ||
2322 | * be using ... so use these product IDs here and in the "linux.inf" | ||
2323 | * needed to install MSFT drivers. Current Linux kernels will use | ||
2324 | * the second configuration if it's CDC Ethernet, and need some help | ||
2325 | * to choose the right configuration otherwise. | ||
2326 | */ | ||
2327 | if (rndis) { | ||
2328 | device_desc.idVendor = | ||
2329 | __constant_cpu_to_le16(RNDIS_VENDOR_NUM); | ||
2330 | device_desc.idProduct = | ||
2331 | __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); | ||
2332 | snprintf (product_desc, sizeof product_desc, | ||
2333 | "RNDIS/%s", driver_desc); | ||
2334 | |||
2335 | /* CDC subset ... recognized by Linux since 2.4.10, but Windows | ||
2336 | * drivers aren't widely available. (That may be improved by | ||
2337 | * supporting one submode of the "SAFE" variant of MDLM.) | ||
2338 | */ | ||
2339 | } else if (!cdc) { | ||
2340 | device_desc.idVendor = | ||
2341 | __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); | ||
2342 | device_desc.idProduct = | ||
2343 | __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); | ||
2344 | } | ||
2345 | |||
2346 | /* support optional vendor/distro customization */ | ||
2347 | if (idVendor) { | ||
2348 | if (!idProduct) { | ||
2349 | dev_err (&gadget->dev, "idVendor needs idProduct!\n"); | ||
2350 | return -ENODEV; | ||
2351 | } | ||
2352 | device_desc.idVendor = cpu_to_le16(idVendor); | ||
2353 | device_desc.idProduct = cpu_to_le16(idProduct); | ||
2354 | if (bcdDevice) | ||
2355 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); | ||
2356 | } | ||
2357 | if (iManufacturer) | ||
2358 | strlcpy (manufacturer, iManufacturer, sizeof manufacturer); | ||
2359 | if (iProduct) | ||
2360 | strlcpy (product_desc, iProduct, sizeof product_desc); | ||
2361 | if (iSerialNumber) { | ||
2362 | device_desc.iSerialNumber = STRING_SERIALNUMBER, | ||
2363 | strlcpy(serial_number, iSerialNumber, sizeof serial_number); | ||
2364 | } | ||
2365 | |||
2366 | /* all we really need is bulk IN/OUT */ | ||
2367 | usb_ep_autoconfig_reset (gadget); | ||
2368 | in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); | ||
2369 | if (!in_ep) { | ||
2370 | autoconf_fail: | ||
2371 | dev_err (&gadget->dev, | ||
2372 | "can't autoconfigure on %s\n", | ||
2373 | gadget->name); | ||
2374 | return -ENODEV; | ||
2375 | } | ||
2376 | in_ep->driver_data = in_ep; /* claim */ | ||
2377 | |||
2378 | out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc); | ||
2379 | if (!out_ep) | ||
2380 | goto autoconf_fail; | ||
2381 | out_ep->driver_data = out_ep; /* claim */ | ||
2382 | |||
2383 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
2384 | /* CDC Ethernet control interface doesn't require a status endpoint. | ||
2385 | * Since some hosts expect one, try to allocate one anyway. | ||
2386 | */ | ||
2387 | if (cdc || rndis) { | ||
2388 | status_ep = usb_ep_autoconfig (gadget, &fs_status_desc); | ||
2389 | if (status_ep) { | ||
2390 | status_ep->driver_data = status_ep; /* claim */ | ||
2391 | } else if (rndis) { | ||
2392 | dev_err (&gadget->dev, | ||
2393 | "can't run RNDIS on %s\n", | ||
2394 | gadget->name); | ||
2395 | return -ENODEV; | ||
2396 | #ifdef DEV_CONFIG_CDC | ||
2397 | /* pxa25x only does CDC subset; often used with RNDIS */ | ||
2398 | } else if (cdc) { | ||
2399 | control_intf.bNumEndpoints = 0; | ||
2400 | /* FIXME remove endpoint from descriptor list */ | ||
2401 | #endif | ||
2402 | } | ||
2403 | } | ||
2404 | #endif | ||
2405 | |||
2406 | /* one config: cdc, else minimal subset */ | ||
2407 | if (!cdc) { | ||
2408 | eth_config.bNumInterfaces = 1; | ||
2409 | eth_config.iConfiguration = STRING_SUBSET; | ||
2410 | |||
2411 | /* use functions to set these up, in case we're built to work | ||
2412 | * with multiple controllers and must override CDC Ethernet. | ||
2413 | */ | ||
2414 | fs_subset_descriptors(); | ||
2415 | hs_subset_descriptors(); | ||
2416 | } | ||
2417 | |||
2418 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | ||
2419 | usb_gadget_set_selfpowered (gadget); | ||
2420 | |||
2421 | /* For now RNDIS is always a second config */ | ||
2422 | if (rndis) | ||
2423 | device_desc.bNumConfigurations = 2; | ||
2424 | |||
2425 | if (gadget_is_dualspeed(gadget)) { | ||
2426 | if (rndis) | ||
2427 | dev_qualifier.bNumConfigurations = 2; | ||
2428 | else if (!cdc) | ||
2429 | dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; | ||
2430 | |||
2431 | /* assumes ep0 uses the same value for both speeds ... */ | ||
2432 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; | ||
2433 | |||
2434 | /* and that all endpoints are dual-speed */ | ||
2435 | hs_source_desc.bEndpointAddress = | ||
2436 | fs_source_desc.bEndpointAddress; | ||
2437 | hs_sink_desc.bEndpointAddress = | ||
2438 | fs_sink_desc.bEndpointAddress; | ||
2439 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | ||
2440 | if (status_ep) | ||
2441 | hs_status_desc.bEndpointAddress = | ||
2442 | fs_status_desc.bEndpointAddress; | ||
2443 | #endif | ||
2444 | } | 301 | } |
2445 | 302 | ||
2446 | if (gadget_is_otg(gadget)) { | ||
2447 | otg_descriptor.bmAttributes |= USB_OTG_HNP, | ||
2448 | eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
2449 | eth_config.bMaxPower = 4; | ||
2450 | #ifdef CONFIG_USB_ETH_RNDIS | ||
2451 | rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
2452 | rndis_config.bMaxPower = 4; | ||
2453 | #endif | ||
2454 | } | ||
2455 | |||
2456 | net = alloc_etherdev (sizeof *dev); | ||
2457 | if (!net) | ||
2458 | return status; | ||
2459 | dev = netdev_priv(net); | ||
2460 | spin_lock_init (&dev->lock); | ||
2461 | spin_lock_init (&dev->req_lock); | ||
2462 | INIT_WORK (&dev->work, eth_work); | ||
2463 | INIT_LIST_HEAD (&dev->tx_reqs); | ||
2464 | INIT_LIST_HEAD (&dev->rx_reqs); | ||
2465 | |||
2466 | /* network device setup */ | ||
2467 | dev->net = net; | ||
2468 | strcpy (net->name, "usb%d"); | ||
2469 | dev->cdc = cdc; | ||
2470 | dev->zlp = zlp; | ||
2471 | 303 | ||
2472 | dev->in_ep = in_ep; | 304 | /* Allocate string descriptor numbers ... note that string |
2473 | dev->out_ep = out_ep; | 305 | * contents can be overridden by the composite_dev glue. |
2474 | dev->status_ep = status_ep; | ||
2475 | |||
2476 | /* Module params for these addresses should come from ID proms. | ||
2477 | * The host side address is used with CDC and RNDIS, and commonly | ||
2478 | * ends up in a persistent config database. It's not clear if | ||
2479 | * host side code for the SAFE thing cares -- its original BLAN | ||
2480 | * thing didn't, Sharp never assigned those addresses on Zaurii. | ||
2481 | */ | 306 | */ |
2482 | if (get_ether_addr(dev_addr, net->dev_addr)) | ||
2483 | dev_warn(&gadget->dev, | ||
2484 | "using random %s ethernet address\n", "self"); | ||
2485 | if (get_ether_addr(host_addr, dev->host_mac)) | ||
2486 | dev_warn(&gadget->dev, | ||
2487 | "using random %s ethernet address\n", "host"); | ||
2488 | snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X", | ||
2489 | dev->host_mac [0], dev->host_mac [1], | ||
2490 | dev->host_mac [2], dev->host_mac [3], | ||
2491 | dev->host_mac [4], dev->host_mac [5]); | ||
2492 | |||
2493 | if (rndis) { | ||
2494 | status = rndis_init(); | ||
2495 | if (status < 0) { | ||
2496 | dev_err (&gadget->dev, "can't init RNDIS, %d\n", | ||
2497 | status); | ||
2498 | goto fail; | ||
2499 | } | ||
2500 | } | ||
2501 | 307 | ||
2502 | net->change_mtu = eth_change_mtu; | 308 | /* device descriptor strings: manufacturer, product */ |
2503 | net->get_stats = eth_get_stats; | 309 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
2504 | net->hard_start_xmit = eth_start_xmit; | 310 | init_utsname()->sysname, init_utsname()->release, |
2505 | net->open = eth_open; | 311 | gadget->name); |
2506 | net->stop = eth_stop; | 312 | status = usb_string_id(cdev); |
2507 | // watchdog_timeo, tx_timeout ... | 313 | if (status < 0) |
2508 | // set_multicast_list | 314 | goto fail; |
2509 | SET_ETHTOOL_OPS(net, &ops); | 315 | strings_dev[STRING_MANUFACTURER_IDX].id = status; |
316 | device_desc.iManufacturer = status; | ||
2510 | 317 | ||
2511 | /* preallocate control message data and buffer */ | 318 | status = usb_string_id(cdev); |
2512 | dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL); | 319 | if (status < 0) |
2513 | if (!dev->req) | ||
2514 | goto fail; | 320 | goto fail; |
2515 | dev->req->complete = eth_setup_complete; | 321 | strings_dev[STRING_PRODUCT_IDX].id = status; |
322 | device_desc.iProduct = status; | ||
2516 | 323 | ||
2517 | /* ... and maybe likewise for status transfer */ | 324 | /* register our configuration(s); RNDIS first, if it's used */ |
2518 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 325 | if (has_rndis()) { |
2519 | if (dev->status_ep) { | 326 | status = usb_add_config(cdev, &rndis_config_driver); |
2520 | dev->stat_req = eth_req_alloc (dev->status_ep, | 327 | if (status < 0) |
2521 | STATUS_BYTECOUNT, GFP_KERNEL); | ||
2522 | if (!dev->stat_req) { | ||
2523 | eth_req_free (gadget->ep0, dev->req); | ||
2524 | goto fail; | 328 | goto fail; |
2525 | } | ||
2526 | dev->stat_req->context = NULL; | ||
2527 | } | 329 | } |
2528 | #endif | ||
2529 | |||
2530 | /* finish hookup to lower layer ... */ | ||
2531 | dev->gadget = gadget; | ||
2532 | set_gadget_data (gadget, dev); | ||
2533 | gadget->ep0->driver_data = dev; | ||
2534 | 330 | ||
2535 | /* two kinds of host-initiated state changes: | 331 | status = usb_add_config(cdev, ð_config_driver); |
2536 | * - iff DATA transfer is active, carrier is "on" | ||
2537 | * - tx queueing enabled if open *and* carrier is "on" | ||
2538 | */ | ||
2539 | netif_stop_queue (dev->net); | ||
2540 | netif_carrier_off (dev->net); | ||
2541 | |||
2542 | SET_NETDEV_DEV (dev->net, &gadget->dev); | ||
2543 | status = register_netdev (dev->net); | ||
2544 | if (status < 0) | 332 | if (status < 0) |
2545 | goto fail1; | 333 | goto fail; |
2546 | |||
2547 | INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); | ||
2548 | INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name, | ||
2549 | out_ep->name, in_ep->name, | ||
2550 | status_ep ? " STATUS " : "", | ||
2551 | status_ep ? status_ep->name : "" | ||
2552 | ); | ||
2553 | INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | ||
2554 | net->dev_addr [0], net->dev_addr [1], | ||
2555 | net->dev_addr [2], net->dev_addr [3], | ||
2556 | net->dev_addr [4], net->dev_addr [5]); | ||
2557 | |||
2558 | if (cdc || rndis) | ||
2559 | INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | ||
2560 | dev->host_mac [0], dev->host_mac [1], | ||
2561 | dev->host_mac [2], dev->host_mac [3], | ||
2562 | dev->host_mac [4], dev->host_mac [5]); | ||
2563 | |||
2564 | if (rndis) { | ||
2565 | u32 vendorID = 0; | ||
2566 | |||
2567 | /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ | ||
2568 | |||
2569 | dev->rndis_config = rndis_register (rndis_control_ack); | ||
2570 | if (dev->rndis_config < 0) { | ||
2571 | fail0: | ||
2572 | unregister_netdev (dev->net); | ||
2573 | status = -ENODEV; | ||
2574 | goto fail; | ||
2575 | } | ||
2576 | 334 | ||
2577 | /* these set up a lot of the OIDs that RNDIS needs */ | 335 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC); |
2578 | rndis_set_host_mac (dev->rndis_config, dev->host_mac); | ||
2579 | if (rndis_set_param_dev (dev->rndis_config, dev->net, | ||
2580 | &dev->stats, &dev->cdc_filter)) | ||
2581 | goto fail0; | ||
2582 | if (rndis_set_param_vendor(dev->rndis_config, vendorID, | ||
2583 | manufacturer)) | ||
2584 | goto fail0; | ||
2585 | if (rndis_set_param_medium(dev->rndis_config, | ||
2586 | NDIS_MEDIUM_802_3, 0)) | ||
2587 | goto fail0; | ||
2588 | INFO (dev, "RNDIS ready\n"); | ||
2589 | } | ||
2590 | 336 | ||
2591 | return status; | 337 | return 0; |
2592 | 338 | ||
2593 | fail1: | ||
2594 | dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status); | ||
2595 | fail: | 339 | fail: |
2596 | eth_unbind (gadget); | 340 | gether_cleanup(); |
2597 | return status; | 341 | return status; |
2598 | } | 342 | } |
2599 | 343 | ||
2600 | /*-------------------------------------------------------------------------*/ | 344 | static int __exit eth_unbind(struct usb_composite_dev *cdev) |
2601 | |||
2602 | static void | ||
2603 | eth_suspend (struct usb_gadget *gadget) | ||
2604 | { | ||
2605 | struct eth_dev *dev = get_gadget_data (gadget); | ||
2606 | |||
2607 | DEBUG (dev, "suspend\n"); | ||
2608 | dev->suspended = 1; | ||
2609 | } | ||
2610 | |||
2611 | static void | ||
2612 | eth_resume (struct usb_gadget *gadget) | ||
2613 | { | 345 | { |
2614 | struct eth_dev *dev = get_gadget_data (gadget); | 346 | gether_cleanup(); |
2615 | 347 | return 0; | |
2616 | DEBUG (dev, "resume\n"); | ||
2617 | dev->suspended = 0; | ||
2618 | } | 348 | } |
2619 | 349 | ||
2620 | /*-------------------------------------------------------------------------*/ | 350 | static struct usb_composite_driver eth_driver = { |
2621 | 351 | .name = "g_ether", | |
2622 | static struct usb_gadget_driver eth_driver = { | 352 | .dev = &device_desc, |
2623 | .speed = DEVSPEED, | 353 | .strings = dev_strings, |
2624 | |||
2625 | .function = (char *) driver_desc, | ||
2626 | .bind = eth_bind, | 354 | .bind = eth_bind, |
2627 | .unbind = eth_unbind, | 355 | .unbind = __exit_p(eth_unbind), |
2628 | |||
2629 | .setup = eth_setup, | ||
2630 | .disconnect = eth_disconnect, | ||
2631 | |||
2632 | .suspend = eth_suspend, | ||
2633 | .resume = eth_resume, | ||
2634 | |||
2635 | .driver = { | ||
2636 | .name = (char *) shortname, | ||
2637 | .owner = THIS_MODULE, | ||
2638 | }, | ||
2639 | }; | 356 | }; |
2640 | 357 | ||
2641 | MODULE_DESCRIPTION (DRIVER_DESC); | 358 | MODULE_DESCRIPTION(PREFIX DRIVER_DESC); |
2642 | MODULE_AUTHOR ("David Brownell, Benedikt Spanger"); | 359 | MODULE_AUTHOR("David Brownell, Benedikt Spanger"); |
2643 | MODULE_LICENSE ("GPL"); | 360 | MODULE_LICENSE("GPL"); |
2644 | 361 | ||
2645 | 362 | static int __init init(void) | |
2646 | static int __init init (void) | ||
2647 | { | 363 | { |
2648 | return usb_gadget_register_driver (ð_driver); | 364 | return usb_composite_register(ð_driver); |
2649 | } | 365 | } |
2650 | module_init (init); | 366 | module_init(init); |
2651 | 367 | ||
2652 | static void __exit cleanup (void) | 368 | static void __exit cleanup(void) |
2653 | { | 369 | { |
2654 | usb_gadget_unregister_driver (ð_driver); | 370 | usb_composite_unregister(ð_driver); |
2655 | } | 371 | } |
2656 | module_exit (cleanup); | 372 | module_exit(cleanup); |
2657 | |||
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c new file mode 100644 index 000000000000..d8faccf27895 --- /dev/null +++ b/drivers/usb/gadget/f_acm.c | |||
@@ -0,0 +1,589 @@ | |||
1 | /* | ||
2 | * f_acm.c -- USB CDC serial (ACM) function driver | ||
3 | * | ||
4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) | ||
5 | * Copyright (C) 2008 by David Brownell | ||
6 | * Copyright (C) 2008 by Nokia Corporation | ||
7 | * | ||
8 | * This software is distributed under the terms of the GNU General | ||
9 | * Public License ("GPL") as published by the Free Software Foundation, | ||
10 | * either version 2 of that License or (at your option) any later version. | ||
11 | */ | ||
12 | |||
13 | /* #define VERBOSE_DEBUG */ | ||
14 | |||
15 | #include <linux/kernel.h> | ||
16 | #include <linux/device.h> | ||
17 | |||
18 | #include "u_serial.h" | ||
19 | #include "gadget_chips.h" | ||
20 | |||
21 | |||
22 | /* | ||
23 | * This CDC ACM function support just wraps control functions and | ||
24 | * notifications around the generic serial-over-usb code. | ||
25 | * | ||
26 | * Because CDC ACM is standardized by the USB-IF, many host operating | ||
27 | * systems have drivers for it. Accordingly, ACM is the preferred | ||
28 | * interop solution for serial-port type connections. The control | ||
29 | * models are often not necessary, and in any case don't do much in | ||
30 | * this bare-bones implementation. | ||
31 | * | ||
32 | * Note that even MS-Windows has some support for ACM. However, that | ||
33 | * support is somewhat broken because when you use ACM in a composite | ||
34 | * device, having multiple interfaces confuses the poor OS. It doesn't | ||
35 | * seem to understand CDC Union descriptors. The new "association" | ||
36 | * descriptors (roughly equivalent to CDC Unions) may sometimes help. | ||
37 | */ | ||
38 | |||
39 | struct acm_ep_descs { | ||
40 | struct usb_endpoint_descriptor *in; | ||
41 | struct usb_endpoint_descriptor *out; | ||
42 | struct usb_endpoint_descriptor *notify; | ||
43 | }; | ||
44 | |||
45 | struct f_acm { | ||
46 | struct gserial port; | ||
47 | u8 ctrl_id, data_id; | ||
48 | u8 port_num; | ||
49 | |||
50 | struct usb_descriptor_header **fs_function; | ||
51 | struct acm_ep_descs fs; | ||
52 | struct usb_descriptor_header **hs_function; | ||
53 | struct acm_ep_descs hs; | ||
54 | |||
55 | struct usb_ep *notify; | ||
56 | struct usb_endpoint_descriptor *notify_desc; | ||
57 | |||
58 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ | ||
59 | u16 port_handshake_bits; | ||
60 | #define RS232_RTS (1 << 1) /* unused with full duplex */ | ||
61 | #define RS232_DTR (1 << 0) /* host is ready for data r/w */ | ||
62 | }; | ||
63 | |||
64 | static inline struct f_acm *func_to_acm(struct usb_function *f) | ||
65 | { | ||
66 | return container_of(f, struct f_acm, port.func); | ||
67 | } | ||
68 | |||
69 | /*-------------------------------------------------------------------------*/ | ||
70 | |||
71 | /* notification endpoint uses smallish and infrequent fixed-size messages */ | ||
72 | |||
73 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ | ||
74 | #define GS_NOTIFY_MAXPACKET 8 | ||
75 | |||
76 | /* interface and class descriptors: */ | ||
77 | |||
78 | static struct usb_interface_descriptor acm_control_interface_desc __initdata = { | ||
79 | .bLength = USB_DT_INTERFACE_SIZE, | ||
80 | .bDescriptorType = USB_DT_INTERFACE, | ||
81 | /* .bInterfaceNumber = DYNAMIC */ | ||
82 | .bNumEndpoints = 1, | ||
83 | .bInterfaceClass = USB_CLASS_COMM, | ||
84 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | ||
85 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, | ||
86 | /* .iInterface = DYNAMIC */ | ||
87 | }; | ||
88 | |||
89 | static struct usb_interface_descriptor acm_data_interface_desc __initdata = { | ||
90 | .bLength = USB_DT_INTERFACE_SIZE, | ||
91 | .bDescriptorType = USB_DT_INTERFACE, | ||
92 | /* .bInterfaceNumber = DYNAMIC */ | ||
93 | .bNumEndpoints = 2, | ||
94 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
95 | .bInterfaceSubClass = 0, | ||
96 | .bInterfaceProtocol = 0, | ||
97 | /* .iInterface = DYNAMIC */ | ||
98 | }; | ||
99 | |||
100 | static struct usb_cdc_header_desc acm_header_desc __initdata = { | ||
101 | .bLength = sizeof(acm_header_desc), | ||
102 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
103 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
104 | .bcdCDC = __constant_cpu_to_le16(0x0110), | ||
105 | }; | ||
106 | |||
107 | static struct usb_cdc_call_mgmt_descriptor | ||
108 | acm_call_mgmt_descriptor __initdata = { | ||
109 | .bLength = sizeof(acm_call_mgmt_descriptor), | ||
110 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
111 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | ||
112 | .bmCapabilities = 0, | ||
113 | /* .bDataInterface = DYNAMIC */ | ||
114 | }; | ||
115 | |||
116 | static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { | ||
117 | .bLength = sizeof(acm_descriptor), | ||
118 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
119 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | ||
120 | .bmCapabilities = (1 << 1), | ||
121 | }; | ||
122 | |||
123 | static struct usb_cdc_union_desc acm_union_desc __initdata = { | ||
124 | .bLength = sizeof(acm_union_desc), | ||
125 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
126 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | ||
127 | /* .bMasterInterface0 = DYNAMIC */ | ||
128 | /* .bSlaveInterface0 = DYNAMIC */ | ||
129 | }; | ||
130 | |||
131 | /* full speed support: */ | ||
132 | |||
133 | static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = { | ||
134 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
135 | .bDescriptorType = USB_DT_ENDPOINT, | ||
136 | .bEndpointAddress = USB_DIR_IN, | ||
137 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
138 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), | ||
139 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, | ||
140 | }; | ||
141 | |||
142 | static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = { | ||
143 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
144 | .bDescriptorType = USB_DT_ENDPOINT, | ||
145 | .bEndpointAddress = USB_DIR_IN, | ||
146 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
147 | }; | ||
148 | |||
149 | static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = { | ||
150 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
151 | .bDescriptorType = USB_DT_ENDPOINT, | ||
152 | .bEndpointAddress = USB_DIR_OUT, | ||
153 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
154 | }; | ||
155 | |||
156 | static struct usb_descriptor_header *acm_fs_function[] __initdata = { | ||
157 | (struct usb_descriptor_header *) &acm_control_interface_desc, | ||
158 | (struct usb_descriptor_header *) &acm_header_desc, | ||
159 | (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, | ||
160 | (struct usb_descriptor_header *) &acm_descriptor, | ||
161 | (struct usb_descriptor_header *) &acm_union_desc, | ||
162 | (struct usb_descriptor_header *) &acm_fs_notify_desc, | ||
163 | (struct usb_descriptor_header *) &acm_data_interface_desc, | ||
164 | (struct usb_descriptor_header *) &acm_fs_in_desc, | ||
165 | (struct usb_descriptor_header *) &acm_fs_out_desc, | ||
166 | NULL, | ||
167 | }; | ||
168 | |||
169 | /* high speed support: */ | ||
170 | |||
171 | static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = { | ||
172 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
173 | .bDescriptorType = USB_DT_ENDPOINT, | ||
174 | .bEndpointAddress = USB_DIR_IN, | ||
175 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
176 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), | ||
177 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, | ||
178 | }; | ||
179 | |||
180 | static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = { | ||
181 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
182 | .bDescriptorType = USB_DT_ENDPOINT, | ||
183 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
184 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
185 | }; | ||
186 | |||
187 | static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = { | ||
188 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
189 | .bDescriptorType = USB_DT_ENDPOINT, | ||
190 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
191 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
192 | }; | ||
193 | |||
194 | static struct usb_descriptor_header *acm_hs_function[] __initdata = { | ||
195 | (struct usb_descriptor_header *) &acm_control_interface_desc, | ||
196 | (struct usb_descriptor_header *) &acm_header_desc, | ||
197 | (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, | ||
198 | (struct usb_descriptor_header *) &acm_descriptor, | ||
199 | (struct usb_descriptor_header *) &acm_union_desc, | ||
200 | (struct usb_descriptor_header *) &acm_hs_notify_desc, | ||
201 | (struct usb_descriptor_header *) &acm_data_interface_desc, | ||
202 | (struct usb_descriptor_header *) &acm_hs_in_desc, | ||
203 | (struct usb_descriptor_header *) &acm_hs_out_desc, | ||
204 | NULL, | ||
205 | }; | ||
206 | |||
207 | /* string descriptors: */ | ||
208 | |||
209 | #define ACM_CTRL_IDX 0 | ||
210 | #define ACM_DATA_IDX 1 | ||
211 | |||
212 | /* static strings, in UTF-8 */ | ||
213 | static struct usb_string acm_string_defs[] = { | ||
214 | [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)", | ||
215 | [ACM_DATA_IDX].s = "CDC ACM Data", | ||
216 | { /* ZEROES END LIST */ }, | ||
217 | }; | ||
218 | |||
219 | static struct usb_gadget_strings acm_string_table = { | ||
220 | .language = 0x0409, /* en-us */ | ||
221 | .strings = acm_string_defs, | ||
222 | }; | ||
223 | |||
224 | static struct usb_gadget_strings *acm_strings[] = { | ||
225 | &acm_string_table, | ||
226 | NULL, | ||
227 | }; | ||
228 | |||
229 | /*-------------------------------------------------------------------------*/ | ||
230 | |||
231 | /* ACM control ... data handling is delegated to tty library code. | ||
232 | * The main task of this function is to activate and deactivate | ||
233 | * that code based on device state; track parameters like line | ||
234 | * speed, handshake state, and so on; and issue notifications. | ||
235 | */ | ||
236 | |||
237 | static void acm_complete_set_line_coding(struct usb_ep *ep, | ||
238 | struct usb_request *req) | ||
239 | { | ||
240 | struct f_acm *acm = ep->driver_data; | ||
241 | struct usb_composite_dev *cdev = acm->port.func.config->cdev; | ||
242 | |||
243 | if (req->status != 0) { | ||
244 | DBG(cdev, "acm ttyGS%d completion, err %d\n", | ||
245 | acm->port_num, req->status); | ||
246 | return; | ||
247 | } | ||
248 | |||
249 | /* normal completion */ | ||
250 | if (req->actual != sizeof(acm->port_line_coding)) { | ||
251 | DBG(cdev, "acm ttyGS%d short resp, len %d\n", | ||
252 | acm->port_num, req->actual); | ||
253 | usb_ep_set_halt(ep); | ||
254 | } else { | ||
255 | struct usb_cdc_line_coding *value = req->buf; | ||
256 | |||
257 | /* REVISIT: we currently just remember this data. | ||
258 | * If we change that, (a) validate it first, then | ||
259 | * (b) update whatever hardware needs updating, | ||
260 | * (c) worry about locking. This is information on | ||
261 | * the order of 9600-8-N-1 ... most of which means | ||
262 | * nothing unless we control a real RS232 line. | ||
263 | */ | ||
264 | acm->port_line_coding = *value; | ||
265 | } | ||
266 | } | ||
267 | |||
268 | static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | ||
269 | { | ||
270 | struct f_acm *acm = func_to_acm(f); | ||
271 | struct usb_composite_dev *cdev = f->config->cdev; | ||
272 | struct usb_request *req = cdev->req; | ||
273 | int value = -EOPNOTSUPP; | ||
274 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
275 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
276 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
277 | |||
278 | /* composite driver infrastructure handles everything except | ||
279 | * CDC class messages; interface activation uses set_alt(). | ||
280 | */ | ||
281 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | ||
282 | |||
283 | /* SET_LINE_CODING ... just read and save what the host sends */ | ||
284 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
285 | | USB_CDC_REQ_SET_LINE_CODING: | ||
286 | if (w_length != sizeof(struct usb_cdc_line_coding) | ||
287 | || w_index != acm->ctrl_id) | ||
288 | goto invalid; | ||
289 | |||
290 | value = w_length; | ||
291 | cdev->gadget->ep0->driver_data = acm; | ||
292 | req->complete = acm_complete_set_line_coding; | ||
293 | break; | ||
294 | |||
295 | /* GET_LINE_CODING ... return what host sent, or initial value */ | ||
296 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
297 | | USB_CDC_REQ_GET_LINE_CODING: | ||
298 | if (w_index != acm->ctrl_id) | ||
299 | goto invalid; | ||
300 | |||
301 | value = min_t(unsigned, w_length, | ||
302 | sizeof(struct usb_cdc_line_coding)); | ||
303 | memcpy(req->buf, &acm->port_line_coding, value); | ||
304 | break; | ||
305 | |||
306 | /* SET_CONTROL_LINE_STATE ... save what the host sent */ | ||
307 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
308 | | USB_CDC_REQ_SET_CONTROL_LINE_STATE: | ||
309 | if (w_index != acm->ctrl_id) | ||
310 | goto invalid; | ||
311 | |||
312 | value = 0; | ||
313 | |||
314 | /* FIXME we should not allow data to flow until the | ||
315 | * host sets the RS232_DTR bit; and when it clears | ||
316 | * that bit, we should return to that no-flow state. | ||
317 | */ | ||
318 | acm->port_handshake_bits = w_value; | ||
319 | break; | ||
320 | |||
321 | default: | ||
322 | invalid: | ||
323 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | ||
324 | ctrl->bRequestType, ctrl->bRequest, | ||
325 | w_value, w_index, w_length); | ||
326 | } | ||
327 | |||
328 | /* respond with data transfer or status phase? */ | ||
329 | if (value >= 0) { | ||
330 | DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n", | ||
331 | acm->port_num, ctrl->bRequestType, ctrl->bRequest, | ||
332 | w_value, w_index, w_length); | ||
333 | req->zero = 0; | ||
334 | req->length = value; | ||
335 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | ||
336 | if (value < 0) | ||
337 | ERROR(cdev, "acm response on ttyGS%d, err %d\n", | ||
338 | acm->port_num, value); | ||
339 | } | ||
340 | |||
341 | /* device either stalls (value < 0) or reports success */ | ||
342 | return value; | ||
343 | } | ||
344 | |||
345 | static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | ||
346 | { | ||
347 | struct f_acm *acm = func_to_acm(f); | ||
348 | struct usb_composite_dev *cdev = f->config->cdev; | ||
349 | |||
350 | /* we know alt == 0, so this is an activation or a reset */ | ||
351 | |||
352 | if (intf == acm->ctrl_id) { | ||
353 | /* REVISIT this may need more work when we start to | ||
354 | * send notifications ... | ||
355 | */ | ||
356 | if (acm->notify->driver_data) { | ||
357 | VDBG(cdev, "reset acm control interface %d\n", intf); | ||
358 | usb_ep_disable(acm->notify); | ||
359 | } else { | ||
360 | VDBG(cdev, "init acm ctrl interface %d\n", intf); | ||
361 | acm->notify_desc = ep_choose(cdev->gadget, | ||
362 | acm->hs.notify, | ||
363 | acm->fs.notify); | ||
364 | } | ||
365 | usb_ep_enable(acm->notify, acm->notify_desc); | ||
366 | acm->notify->driver_data = acm; | ||
367 | |||
368 | } else if (intf == acm->data_id) { | ||
369 | if (acm->port.in->driver_data) { | ||
370 | DBG(cdev, "reset acm ttyGS%d\n", acm->port_num); | ||
371 | gserial_disconnect(&acm->port); | ||
372 | } else { | ||
373 | DBG(cdev, "activate acm ttyGS%d\n", acm->port_num); | ||
374 | acm->port.in_desc = ep_choose(cdev->gadget, | ||
375 | acm->hs.in, acm->fs.in); | ||
376 | acm->port.out_desc = ep_choose(cdev->gadget, | ||
377 | acm->hs.out, acm->fs.out); | ||
378 | } | ||
379 | gserial_connect(&acm->port, acm->port_num); | ||
380 | |||
381 | } else | ||
382 | return -EINVAL; | ||
383 | |||
384 | return 0; | ||
385 | } | ||
386 | |||
387 | static void acm_disable(struct usb_function *f) | ||
388 | { | ||
389 | struct f_acm *acm = func_to_acm(f); | ||
390 | struct usb_composite_dev *cdev = f->config->cdev; | ||
391 | |||
392 | DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num); | ||
393 | gserial_disconnect(&acm->port); | ||
394 | usb_ep_disable(acm->notify); | ||
395 | acm->notify->driver_data = NULL; | ||
396 | } | ||
397 | |||
398 | /*-------------------------------------------------------------------------*/ | ||
399 | |||
400 | /* ACM function driver setup/binding */ | ||
401 | static int __init | ||
402 | acm_bind(struct usb_configuration *c, struct usb_function *f) | ||
403 | { | ||
404 | struct usb_composite_dev *cdev = c->cdev; | ||
405 | struct f_acm *acm = func_to_acm(f); | ||
406 | int status; | ||
407 | struct usb_ep *ep; | ||
408 | |||
409 | /* allocate instance-specific interface IDs, and patch descriptors */ | ||
410 | status = usb_interface_id(c, f); | ||
411 | if (status < 0) | ||
412 | goto fail; | ||
413 | acm->ctrl_id = status; | ||
414 | |||
415 | acm_control_interface_desc.bInterfaceNumber = status; | ||
416 | acm_union_desc .bMasterInterface0 = status; | ||
417 | |||
418 | status = usb_interface_id(c, f); | ||
419 | if (status < 0) | ||
420 | goto fail; | ||
421 | acm->data_id = status; | ||
422 | |||
423 | acm_data_interface_desc.bInterfaceNumber = status; | ||
424 | acm_union_desc.bSlaveInterface0 = status; | ||
425 | acm_call_mgmt_descriptor.bDataInterface = status; | ||
426 | |||
427 | status = -ENODEV; | ||
428 | |||
429 | /* allocate instance-specific endpoints */ | ||
430 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc); | ||
431 | if (!ep) | ||
432 | goto fail; | ||
433 | acm->port.in = ep; | ||
434 | ep->driver_data = cdev; /* claim */ | ||
435 | |||
436 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc); | ||
437 | if (!ep) | ||
438 | goto fail; | ||
439 | acm->port.out = ep; | ||
440 | ep->driver_data = cdev; /* claim */ | ||
441 | |||
442 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc); | ||
443 | if (!ep) | ||
444 | goto fail; | ||
445 | acm->notify = ep; | ||
446 | ep->driver_data = cdev; /* claim */ | ||
447 | |||
448 | /* copy descriptors, and track endpoint copies */ | ||
449 | f->descriptors = usb_copy_descriptors(acm_fs_function); | ||
450 | |||
451 | acm->fs.in = usb_find_endpoint(acm_fs_function, | ||
452 | f->descriptors, &acm_fs_in_desc); | ||
453 | acm->fs.out = usb_find_endpoint(acm_fs_function, | ||
454 | f->descriptors, &acm_fs_out_desc); | ||
455 | acm->fs.notify = usb_find_endpoint(acm_fs_function, | ||
456 | f->descriptors, &acm_fs_notify_desc); | ||
457 | |||
458 | /* support all relevant hardware speeds... we expect that when | ||
459 | * hardware is dual speed, all bulk-capable endpoints work at | ||
460 | * both speeds | ||
461 | */ | ||
462 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
463 | acm_hs_in_desc.bEndpointAddress = | ||
464 | acm_fs_in_desc.bEndpointAddress; | ||
465 | acm_hs_out_desc.bEndpointAddress = | ||
466 | acm_fs_out_desc.bEndpointAddress; | ||
467 | acm_hs_notify_desc.bEndpointAddress = | ||
468 | acm_fs_notify_desc.bEndpointAddress; | ||
469 | |||
470 | /* copy descriptors, and track endpoint copies */ | ||
471 | f->hs_descriptors = usb_copy_descriptors(acm_hs_function); | ||
472 | |||
473 | acm->hs.in = usb_find_endpoint(acm_hs_function, | ||
474 | f->hs_descriptors, &acm_hs_in_desc); | ||
475 | acm->hs.out = usb_find_endpoint(acm_hs_function, | ||
476 | f->hs_descriptors, &acm_hs_out_desc); | ||
477 | acm->hs.notify = usb_find_endpoint(acm_hs_function, | ||
478 | f->hs_descriptors, &acm_hs_notify_desc); | ||
479 | } | ||
480 | |||
481 | /* FIXME provide a callback for triggering notifications */ | ||
482 | |||
483 | DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", | ||
484 | acm->port_num, | ||
485 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
486 | acm->port.in->name, acm->port.out->name, | ||
487 | acm->notify->name); | ||
488 | return 0; | ||
489 | |||
490 | fail: | ||
491 | /* we might as well release our claims on endpoints */ | ||
492 | if (acm->notify) | ||
493 | acm->notify->driver_data = NULL; | ||
494 | if (acm->port.out) | ||
495 | acm->port.out->driver_data = NULL; | ||
496 | if (acm->port.in) | ||
497 | acm->port.in->driver_data = NULL; | ||
498 | |||
499 | ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status); | ||
500 | |||
501 | return status; | ||
502 | } | ||
503 | |||
504 | static void | ||
505 | acm_unbind(struct usb_configuration *c, struct usb_function *f) | ||
506 | { | ||
507 | if (gadget_is_dualspeed(c->cdev->gadget)) | ||
508 | usb_free_descriptors(f->hs_descriptors); | ||
509 | usb_free_descriptors(f->descriptors); | ||
510 | kfree(func_to_acm(f)); | ||
511 | } | ||
512 | |||
513 | /* Some controllers can't support CDC ACM ... */ | ||
514 | static inline bool can_support_cdc(struct usb_configuration *c) | ||
515 | { | ||
516 | /* SH3 doesn't support multiple interfaces */ | ||
517 | if (gadget_is_sh(c->cdev->gadget)) | ||
518 | return false; | ||
519 | |||
520 | /* sa1100 doesn't have a third interrupt endpoint */ | ||
521 | if (gadget_is_sa1100(c->cdev->gadget)) | ||
522 | return false; | ||
523 | |||
524 | /* everything else is *probably* fine ... */ | ||
525 | return true; | ||
526 | } | ||
527 | |||
528 | /** | ||
529 | * acm_bind_config - add a CDC ACM function to a configuration | ||
530 | * @c: the configuration to support the CDC ACM instance | ||
531 | * @port_num: /dev/ttyGS* port this interface will use | ||
532 | * Context: single threaded during gadget setup | ||
533 | * | ||
534 | * Returns zero on success, else negative errno. | ||
535 | * | ||
536 | * Caller must have called @gserial_setup() with enough ports to | ||
537 | * handle all the ones it binds. Caller is also responsible | ||
538 | * for calling @gserial_cleanup() before module unload. | ||
539 | */ | ||
540 | int __init acm_bind_config(struct usb_configuration *c, u8 port_num) | ||
541 | { | ||
542 | struct f_acm *acm; | ||
543 | int status; | ||
544 | |||
545 | if (!can_support_cdc(c)) | ||
546 | return -EINVAL; | ||
547 | |||
548 | /* REVISIT might want instance-specific strings to help | ||
549 | * distinguish instances ... | ||
550 | */ | ||
551 | |||
552 | /* maybe allocate device-global string IDs, and patch descriptors */ | ||
553 | if (acm_string_defs[ACM_CTRL_IDX].id == 0) { | ||
554 | status = usb_string_id(c->cdev); | ||
555 | if (status < 0) | ||
556 | return status; | ||
557 | acm_string_defs[ACM_CTRL_IDX].id = status; | ||
558 | |||
559 | acm_control_interface_desc.iInterface = status; | ||
560 | |||
561 | status = usb_string_id(c->cdev); | ||
562 | if (status < 0) | ||
563 | return status; | ||
564 | acm_string_defs[ACM_DATA_IDX].id = status; | ||
565 | |||
566 | acm_data_interface_desc.iInterface = status; | ||
567 | } | ||
568 | |||
569 | /* allocate and initialize one new instance */ | ||
570 | acm = kzalloc(sizeof *acm, GFP_KERNEL); | ||
571 | if (!acm) | ||
572 | return -ENOMEM; | ||
573 | |||
574 | acm->port_num = port_num; | ||
575 | |||
576 | acm->port.func.name = "acm"; | ||
577 | acm->port.func.strings = acm_strings; | ||
578 | /* descriptors are per-instance copies */ | ||
579 | acm->port.func.bind = acm_bind; | ||
580 | acm->port.func.unbind = acm_unbind; | ||
581 | acm->port.func.set_alt = acm_set_alt; | ||
582 | acm->port.func.setup = acm_setup; | ||
583 | acm->port.func.disable = acm_disable; | ||
584 | |||
585 | status = usb_add_function(c, &acm->port.func); | ||
586 | if (status) | ||
587 | kfree(acm); | ||
588 | return status; | ||
589 | } | ||
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c new file mode 100644 index 000000000000..0822e9d7693a --- /dev/null +++ b/drivers/usb/gadget/f_ecm.c | |||
@@ -0,0 +1,833 @@ | |||
1 | /* | ||
2 | * f_ecm.c -- USB CDC Ethernet (ECM) link function driver | ||
3 | * | ||
4 | * Copyright (C) 2003-2005,2008 David Brownell | ||
5 | * Copyright (C) 2008 Nokia Corporation | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | /* #define VERBOSE_DEBUG */ | ||
23 | |||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/device.h> | ||
26 | #include <linux/etherdevice.h> | ||
27 | |||
28 | #include "u_ether.h" | ||
29 | |||
30 | |||
31 | /* | ||
32 | * This function is a "CDC Ethernet Networking Control Model" (CDC ECM) | ||
33 | * Ethernet link. The data transfer model is simple (packets sent and | ||
34 | * received over bulk endpoints using normal short packet termination), | ||
35 | * and the control model exposes various data and optional notifications. | ||
36 | * | ||
37 | * ECM is well standardized and (except for Microsoft) supported by most | ||
38 | * operating systems with USB host support. It's the preferred interop | ||
39 | * solution for Ethernet over USB, at least for firmware based solutions. | ||
40 | * (Hardware solutions tend to be more minimalist.) A newer and simpler | ||
41 | * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on. | ||
42 | * | ||
43 | * Note that ECM requires the use of "alternate settings" for its data | ||
44 | * interface. This means that the set_alt() method has real work to do, | ||
45 | * and also means that a get_alt() method is required. | ||
46 | */ | ||
47 | |||
48 | struct ecm_ep_descs { | ||
49 | struct usb_endpoint_descriptor *in; | ||
50 | struct usb_endpoint_descriptor *out; | ||
51 | struct usb_endpoint_descriptor *notify; | ||
52 | }; | ||
53 | |||
54 | enum ecm_notify_state { | ||
55 | ECM_NOTIFY_NONE, /* don't notify */ | ||
56 | ECM_NOTIFY_CONNECT, /* issue CONNECT next */ | ||
57 | ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */ | ||
58 | }; | ||
59 | |||
60 | struct f_ecm { | ||
61 | struct gether port; | ||
62 | u8 ctrl_id, data_id; | ||
63 | |||
64 | char ethaddr[14]; | ||
65 | |||
66 | struct usb_descriptor_header **fs_function; | ||
67 | struct ecm_ep_descs fs; | ||
68 | struct usb_descriptor_header **hs_function; | ||
69 | struct ecm_ep_descs hs; | ||
70 | |||
71 | struct usb_ep *notify; | ||
72 | struct usb_endpoint_descriptor *notify_desc; | ||
73 | struct usb_request *notify_req; | ||
74 | u8 notify_state; | ||
75 | bool is_open; | ||
76 | |||
77 | /* FIXME is_open needs some irq-ish locking | ||
78 | * ... possibly the same as port.ioport | ||
79 | */ | ||
80 | }; | ||
81 | |||
82 | static inline struct f_ecm *func_to_ecm(struct usb_function *f) | ||
83 | { | ||
84 | return container_of(f, struct f_ecm, port.func); | ||
85 | } | ||
86 | |||
87 | /* peak (theoretical) bulk transfer rate in bits-per-second */ | ||
88 | static inline unsigned bitrate(struct usb_gadget *g) | ||
89 | { | ||
90 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
91 | return 13 * 512 * 8 * 1000 * 8; | ||
92 | else | ||
93 | return 19 * 64 * 1 * 1000 * 8; | ||
94 | } | ||
95 | |||
96 | /*-------------------------------------------------------------------------*/ | ||
97 | |||
98 | /* | ||
99 | * Include the status endpoint if we can, even though it's optional. | ||
100 | * | ||
101 | * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one | ||
102 | * packet, to simplify cancellation; and a big transfer interval, to | ||
103 | * waste less bandwidth. | ||
104 | * | ||
105 | * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even | ||
106 | * if they ignore the connect/disconnect notifications that real aether | ||
107 | * can provide. More advanced cdc configurations might want to support | ||
108 | * encapsulated commands (vendor-specific, using control-OUT). | ||
109 | */ | ||
110 | |||
111 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ | ||
112 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ | ||
113 | |||
114 | |||
115 | /* interface descriptor: */ | ||
116 | |||
117 | static struct usb_interface_descriptor ecm_control_intf __initdata = { | ||
118 | .bLength = sizeof ecm_control_intf, | ||
119 | .bDescriptorType = USB_DT_INTERFACE, | ||
120 | |||
121 | /* .bInterfaceNumber = DYNAMIC */ | ||
122 | /* status endpoint is optional; this could be patched later */ | ||
123 | .bNumEndpoints = 1, | ||
124 | .bInterfaceClass = USB_CLASS_COMM, | ||
125 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, | ||
126 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, | ||
127 | /* .iInterface = DYNAMIC */ | ||
128 | }; | ||
129 | |||
130 | static struct usb_cdc_header_desc header_desc __initdata = { | ||
131 | .bLength = sizeof header_desc, | ||
132 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
133 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
134 | |||
135 | .bcdCDC = __constant_cpu_to_le16(0x0110), | ||
136 | }; | ||
137 | |||
138 | static struct usb_cdc_union_desc ecm_union_desc __initdata = { | ||
139 | .bLength = sizeof(ecm_union_desc), | ||
140 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
141 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | ||
142 | /* .bMasterInterface0 = DYNAMIC */ | ||
143 | /* .bSlaveInterface0 = DYNAMIC */ | ||
144 | }; | ||
145 | |||
146 | static struct usb_cdc_ether_desc ether_desc __initdata = { | ||
147 | .bLength = sizeof ether_desc, | ||
148 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
149 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, | ||
150 | |||
151 | /* this descriptor actually adds value, surprise! */ | ||
152 | /* .iMACAddress = DYNAMIC */ | ||
153 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ | ||
154 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), | ||
155 | .wNumberMCFilters = __constant_cpu_to_le16(0), | ||
156 | .bNumberPowerFilters = 0, | ||
157 | }; | ||
158 | |||
159 | /* the default data interface has no endpoints ... */ | ||
160 | |||
161 | static struct usb_interface_descriptor ecm_data_nop_intf __initdata = { | ||
162 | .bLength = sizeof ecm_data_nop_intf, | ||
163 | .bDescriptorType = USB_DT_INTERFACE, | ||
164 | |||
165 | .bInterfaceNumber = 1, | ||
166 | .bAlternateSetting = 0, | ||
167 | .bNumEndpoints = 0, | ||
168 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
169 | .bInterfaceSubClass = 0, | ||
170 | .bInterfaceProtocol = 0, | ||
171 | /* .iInterface = DYNAMIC */ | ||
172 | }; | ||
173 | |||
174 | /* ... but the "real" data interface has two bulk endpoints */ | ||
175 | |||
176 | static struct usb_interface_descriptor ecm_data_intf __initdata = { | ||
177 | .bLength = sizeof ecm_data_intf, | ||
178 | .bDescriptorType = USB_DT_INTERFACE, | ||
179 | |||
180 | .bInterfaceNumber = 1, | ||
181 | .bAlternateSetting = 1, | ||
182 | .bNumEndpoints = 2, | ||
183 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
184 | .bInterfaceSubClass = 0, | ||
185 | .bInterfaceProtocol = 0, | ||
186 | /* .iInterface = DYNAMIC */ | ||
187 | }; | ||
188 | |||
189 | /* full speed support: */ | ||
190 | |||
191 | static struct usb_endpoint_descriptor fs_notify_desc __initdata = { | ||
192 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
193 | .bDescriptorType = USB_DT_ENDPOINT, | ||
194 | |||
195 | .bEndpointAddress = USB_DIR_IN, | ||
196 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
197 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), | ||
198 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, | ||
199 | }; | ||
200 | |||
201 | static struct usb_endpoint_descriptor fs_in_desc __initdata = { | ||
202 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
203 | .bDescriptorType = USB_DT_ENDPOINT, | ||
204 | |||
205 | .bEndpointAddress = USB_DIR_IN, | ||
206 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
207 | }; | ||
208 | |||
209 | static struct usb_endpoint_descriptor fs_out_desc __initdata = { | ||
210 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
211 | .bDescriptorType = USB_DT_ENDPOINT, | ||
212 | |||
213 | .bEndpointAddress = USB_DIR_OUT, | ||
214 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
215 | }; | ||
216 | |||
217 | static struct usb_descriptor_header *eth_fs_function[] __initdata = { | ||
218 | /* CDC ECM control descriptors */ | ||
219 | (struct usb_descriptor_header *) &ecm_control_intf, | ||
220 | (struct usb_descriptor_header *) &header_desc, | ||
221 | (struct usb_descriptor_header *) &ecm_union_desc, | ||
222 | (struct usb_descriptor_header *) ðer_desc, | ||
223 | /* NOTE: status endpoint might need to be removed */ | ||
224 | (struct usb_descriptor_header *) &fs_notify_desc, | ||
225 | /* data interface, altsettings 0 and 1 */ | ||
226 | (struct usb_descriptor_header *) &ecm_data_nop_intf, | ||
227 | (struct usb_descriptor_header *) &ecm_data_intf, | ||
228 | (struct usb_descriptor_header *) &fs_in_desc, | ||
229 | (struct usb_descriptor_header *) &fs_out_desc, | ||
230 | NULL, | ||
231 | }; | ||
232 | |||
233 | /* high speed support: */ | ||
234 | |||
235 | static struct usb_endpoint_descriptor hs_notify_desc __initdata = { | ||
236 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
237 | .bDescriptorType = USB_DT_ENDPOINT, | ||
238 | |||
239 | .bEndpointAddress = USB_DIR_IN, | ||
240 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
241 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), | ||
242 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, | ||
243 | }; | ||
244 | static struct usb_endpoint_descriptor hs_in_desc __initdata = { | ||
245 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
246 | .bDescriptorType = USB_DT_ENDPOINT, | ||
247 | |||
248 | .bEndpointAddress = USB_DIR_IN, | ||
249 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
250 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
251 | }; | ||
252 | |||
253 | static struct usb_endpoint_descriptor hs_out_desc __initdata = { | ||
254 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
255 | .bDescriptorType = USB_DT_ENDPOINT, | ||
256 | |||
257 | .bEndpointAddress = USB_DIR_OUT, | ||
258 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
259 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
260 | }; | ||
261 | |||
262 | static struct usb_descriptor_header *eth_hs_function[] __initdata = { | ||
263 | /* CDC ECM control descriptors */ | ||
264 | (struct usb_descriptor_header *) &ecm_control_intf, | ||
265 | (struct usb_descriptor_header *) &header_desc, | ||
266 | (struct usb_descriptor_header *) &ecm_union_desc, | ||
267 | (struct usb_descriptor_header *) ðer_desc, | ||
268 | /* NOTE: status endpoint might need to be removed */ | ||
269 | (struct usb_descriptor_header *) &hs_notify_desc, | ||
270 | /* data interface, altsettings 0 and 1 */ | ||
271 | (struct usb_descriptor_header *) &ecm_data_nop_intf, | ||
272 | (struct usb_descriptor_header *) &ecm_data_intf, | ||
273 | (struct usb_descriptor_header *) &hs_in_desc, | ||
274 | (struct usb_descriptor_header *) &hs_out_desc, | ||
275 | NULL, | ||
276 | }; | ||
277 | |||
278 | /* string descriptors: */ | ||
279 | |||
280 | static struct usb_string ecm_string_defs[] = { | ||
281 | [0].s = "CDC Ethernet Control Model (ECM)", | ||
282 | [1].s = NULL /* DYNAMIC */, | ||
283 | [2].s = "CDC Ethernet Data", | ||
284 | { } /* end of list */ | ||
285 | }; | ||
286 | |||
287 | static struct usb_gadget_strings ecm_string_table = { | ||
288 | .language = 0x0409, /* en-us */ | ||
289 | .strings = ecm_string_defs, | ||
290 | }; | ||
291 | |||
292 | static struct usb_gadget_strings *ecm_strings[] = { | ||
293 | &ecm_string_table, | ||
294 | NULL, | ||
295 | }; | ||
296 | |||
297 | /*-------------------------------------------------------------------------*/ | ||
298 | |||
299 | static void ecm_do_notify(struct f_ecm *ecm) | ||
300 | { | ||
301 | struct usb_request *req = ecm->notify_req; | ||
302 | struct usb_cdc_notification *event; | ||
303 | struct usb_composite_dev *cdev = ecm->port.func.config->cdev; | ||
304 | __le32 *data; | ||
305 | int status; | ||
306 | |||
307 | /* notification already in flight? */ | ||
308 | if (!req) | ||
309 | return; | ||
310 | |||
311 | event = req->buf; | ||
312 | switch (ecm->notify_state) { | ||
313 | case ECM_NOTIFY_NONE: | ||
314 | return; | ||
315 | |||
316 | case ECM_NOTIFY_CONNECT: | ||
317 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; | ||
318 | if (ecm->is_open) | ||
319 | event->wValue = cpu_to_le16(1); | ||
320 | else | ||
321 | event->wValue = cpu_to_le16(0); | ||
322 | event->wLength = 0; | ||
323 | req->length = sizeof *event; | ||
324 | |||
325 | DBG(cdev, "notify connect %s\n", | ||
326 | ecm->is_open ? "true" : "false"); | ||
327 | ecm->notify_state = ECM_NOTIFY_SPEED; | ||
328 | break; | ||
329 | |||
330 | case ECM_NOTIFY_SPEED: | ||
331 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; | ||
332 | event->wValue = cpu_to_le16(0); | ||
333 | event->wLength = cpu_to_le16(8); | ||
334 | req->length = STATUS_BYTECOUNT; | ||
335 | |||
336 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ | ||
337 | data = req->buf + sizeof *event; | ||
338 | data[0] = cpu_to_le32(bitrate(cdev->gadget)); | ||
339 | data[1] = data[0]; | ||
340 | |||
341 | DBG(cdev, "notify speed %d\n", bitrate(cdev->gadget)); | ||
342 | ecm->notify_state = ECM_NOTIFY_NONE; | ||
343 | break; | ||
344 | } | ||
345 | event->bmRequestType = 0xA1; | ||
346 | event->wIndex = cpu_to_le16(ecm->ctrl_id); | ||
347 | |||
348 | ecm->notify_req = NULL; | ||
349 | status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC); | ||
350 | if (status < 0) { | ||
351 | ecm->notify_req = req; | ||
352 | DBG(cdev, "notify --> %d\n", status); | ||
353 | } | ||
354 | } | ||
355 | |||
356 | static void ecm_notify(struct f_ecm *ecm) | ||
357 | { | ||
358 | /* NOTE on most versions of Linux, host side cdc-ethernet | ||
359 | * won't listen for notifications until its netdevice opens. | ||
360 | * The first notification then sits in the FIFO for a long | ||
361 | * time, and the second one is queued. | ||
362 | */ | ||
363 | ecm->notify_state = ECM_NOTIFY_CONNECT; | ||
364 | ecm_do_notify(ecm); | ||
365 | } | ||
366 | |||
367 | static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req) | ||
368 | { | ||
369 | struct f_ecm *ecm = req->context; | ||
370 | struct usb_composite_dev *cdev = ecm->port.func.config->cdev; | ||
371 | struct usb_cdc_notification *event = req->buf; | ||
372 | |||
373 | switch (req->status) { | ||
374 | case 0: | ||
375 | /* no fault */ | ||
376 | break; | ||
377 | case -ECONNRESET: | ||
378 | case -ESHUTDOWN: | ||
379 | ecm->notify_state = ECM_NOTIFY_NONE; | ||
380 | break; | ||
381 | default: | ||
382 | DBG(cdev, "event %02x --> %d\n", | ||
383 | event->bNotificationType, req->status); | ||
384 | break; | ||
385 | } | ||
386 | ecm->notify_req = req; | ||
387 | ecm_do_notify(ecm); | ||
388 | } | ||
389 | |||
390 | static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | ||
391 | { | ||
392 | struct f_ecm *ecm = func_to_ecm(f); | ||
393 | struct usb_composite_dev *cdev = f->config->cdev; | ||
394 | struct usb_request *req = cdev->req; | ||
395 | int value = -EOPNOTSUPP; | ||
396 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
397 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
398 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
399 | |||
400 | /* composite driver infrastructure handles everything except | ||
401 | * CDC class messages; interface activation uses set_alt(). | ||
402 | */ | ||
403 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | ||
404 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
405 | | USB_CDC_SET_ETHERNET_PACKET_FILTER: | ||
406 | /* see 6.2.30: no data, wIndex = interface, | ||
407 | * wValue = packet filter bitmap | ||
408 | */ | ||
409 | if (w_length != 0 || w_index != ecm->ctrl_id) | ||
410 | goto invalid; | ||
411 | DBG(cdev, "packet filter %02x\n", w_value); | ||
412 | /* REVISIT locking of cdc_filter. This assumes the UDC | ||
413 | * driver won't have a concurrent packet TX irq running on | ||
414 | * another CPU; or that if it does, this write is atomic... | ||
415 | */ | ||
416 | ecm->port.cdc_filter = w_value; | ||
417 | value = 0; | ||
418 | break; | ||
419 | |||
420 | /* and optionally: | ||
421 | * case USB_CDC_SEND_ENCAPSULATED_COMMAND: | ||
422 | * case USB_CDC_GET_ENCAPSULATED_RESPONSE: | ||
423 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: | ||
424 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: | ||
425 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: | ||
426 | * case USB_CDC_GET_ETHERNET_STATISTIC: | ||
427 | */ | ||
428 | |||
429 | default: | ||
430 | invalid: | ||
431 | DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | ||
432 | ctrl->bRequestType, ctrl->bRequest, | ||
433 | w_value, w_index, w_length); | ||
434 | } | ||
435 | |||
436 | /* respond with data transfer or status phase? */ | ||
437 | if (value >= 0) { | ||
438 | DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n", | ||
439 | ctrl->bRequestType, ctrl->bRequest, | ||
440 | w_value, w_index, w_length); | ||
441 | req->zero = 0; | ||
442 | req->length = value; | ||
443 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | ||
444 | if (value < 0) | ||
445 | ERROR(cdev, "ecm req %02x.%02x response err %d\n", | ||
446 | ctrl->bRequestType, ctrl->bRequest, | ||
447 | value); | ||
448 | } | ||
449 | |||
450 | /* device either stalls (value < 0) or reports success */ | ||
451 | return value; | ||
452 | } | ||
453 | |||
454 | |||
455 | static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | ||
456 | { | ||
457 | struct f_ecm *ecm = func_to_ecm(f); | ||
458 | struct usb_composite_dev *cdev = f->config->cdev; | ||
459 | |||
460 | /* Control interface has only altsetting 0 */ | ||
461 | if (intf == ecm->ctrl_id) { | ||
462 | if (alt != 0) | ||
463 | goto fail; | ||
464 | |||
465 | if (ecm->notify->driver_data) { | ||
466 | VDBG(cdev, "reset ecm control %d\n", intf); | ||
467 | usb_ep_disable(ecm->notify); | ||
468 | } else { | ||
469 | VDBG(cdev, "init ecm ctrl %d\n", intf); | ||
470 | ecm->notify_desc = ep_choose(cdev->gadget, | ||
471 | ecm->hs.notify, | ||
472 | ecm->fs.notify); | ||
473 | } | ||
474 | usb_ep_enable(ecm->notify, ecm->notify_desc); | ||
475 | ecm->notify->driver_data = ecm; | ||
476 | |||
477 | /* Data interface has two altsettings, 0 and 1 */ | ||
478 | } else if (intf == ecm->data_id) { | ||
479 | if (alt > 1) | ||
480 | goto fail; | ||
481 | |||
482 | if (ecm->port.in_ep->driver_data) { | ||
483 | DBG(cdev, "reset ecm\n"); | ||
484 | gether_disconnect(&ecm->port); | ||
485 | } | ||
486 | |||
487 | if (!ecm->port.in) { | ||
488 | DBG(cdev, "init ecm\n"); | ||
489 | ecm->port.in = ep_choose(cdev->gadget, | ||
490 | ecm->hs.in, ecm->fs.in); | ||
491 | ecm->port.out = ep_choose(cdev->gadget, | ||
492 | ecm->hs.out, ecm->fs.out); | ||
493 | } | ||
494 | |||
495 | /* CDC Ethernet only sends data in non-default altsettings. | ||
496 | * Changing altsettings resets filters, statistics, etc. | ||
497 | */ | ||
498 | if (alt == 1) { | ||
499 | struct net_device *net; | ||
500 | |||
501 | /* Enable zlps by default for ECM conformance; | ||
502 | * override for musb_hdrc (avoids txdma ovhead) | ||
503 | * and sa1100 (can't). | ||
504 | */ | ||
505 | ecm->port.is_zlp_ok = !( | ||
506 | gadget_is_sa1100(cdev->gadget) | ||
507 | || gadget_is_musbhdrc(cdev->gadget) | ||
508 | ); | ||
509 | ecm->port.cdc_filter = DEFAULT_FILTER; | ||
510 | DBG(cdev, "activate ecm\n"); | ||
511 | net = gether_connect(&ecm->port); | ||
512 | if (IS_ERR(net)) | ||
513 | return PTR_ERR(net); | ||
514 | } | ||
515 | |||
516 | /* NOTE this can be a minor disagreement with the ECM spec, | ||
517 | * which says speed notifications will "always" follow | ||
518 | * connection notifications. But we allow one connect to | ||
519 | * follow another (if the first is in flight), and instead | ||
520 | * just guarantee that a speed notification is always sent. | ||
521 | */ | ||
522 | ecm_notify(ecm); | ||
523 | } else | ||
524 | goto fail; | ||
525 | |||
526 | return 0; | ||
527 | fail: | ||
528 | return -EINVAL; | ||
529 | } | ||
530 | |||
531 | /* Because the data interface supports multiple altsettings, | ||
532 | * this ECM function *MUST* implement a get_alt() method. | ||
533 | */ | ||
534 | static int ecm_get_alt(struct usb_function *f, unsigned intf) | ||
535 | { | ||
536 | struct f_ecm *ecm = func_to_ecm(f); | ||
537 | |||
538 | if (intf == ecm->ctrl_id) | ||
539 | return 0; | ||
540 | return ecm->port.in_ep->driver_data ? 1 : 0; | ||
541 | } | ||
542 | |||
543 | static void ecm_disable(struct usb_function *f) | ||
544 | { | ||
545 | struct f_ecm *ecm = func_to_ecm(f); | ||
546 | struct usb_composite_dev *cdev = f->config->cdev; | ||
547 | |||
548 | DBG(cdev, "ecm deactivated\n"); | ||
549 | |||
550 | if (ecm->port.in_ep->driver_data) | ||
551 | gether_disconnect(&ecm->port); | ||
552 | |||
553 | if (ecm->notify->driver_data) { | ||
554 | usb_ep_disable(ecm->notify); | ||
555 | ecm->notify->driver_data = NULL; | ||
556 | ecm->notify_desc = NULL; | ||
557 | } | ||
558 | } | ||
559 | |||
560 | /*-------------------------------------------------------------------------*/ | ||
561 | |||
562 | /* | ||
563 | * Callbacks let us notify the host about connect/disconnect when the | ||
564 | * net device is opened or closed. | ||
565 | * | ||
566 | * For testing, note that link states on this side include both opened | ||
567 | * and closed variants of: | ||
568 | * | ||
569 | * - disconnected/unconfigured | ||
570 | * - configured but inactive (data alt 0) | ||
571 | * - configured and active (data alt 1) | ||
572 | * | ||
573 | * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and | ||
574 | * SET_INTERFACE (altsetting). Remember also that "configured" doesn't | ||
575 | * imply the host is actually polling the notification endpoint, and | ||
576 | * likewise that "active" doesn't imply it's actually using the data | ||
577 | * endpoints for traffic. | ||
578 | */ | ||
579 | |||
580 | static void ecm_open(struct gether *geth) | ||
581 | { | ||
582 | struct f_ecm *ecm = func_to_ecm(&geth->func); | ||
583 | |||
584 | DBG(ecm->port.func.config->cdev, "%s\n", __func__); | ||
585 | |||
586 | ecm->is_open = true; | ||
587 | ecm_notify(ecm); | ||
588 | } | ||
589 | |||
590 | static void ecm_close(struct gether *geth) | ||
591 | { | ||
592 | struct f_ecm *ecm = func_to_ecm(&geth->func); | ||
593 | |||
594 | DBG(ecm->port.func.config->cdev, "%s\n", __func__); | ||
595 | |||
596 | ecm->is_open = false; | ||
597 | ecm_notify(ecm); | ||
598 | } | ||
599 | |||
600 | /*-------------------------------------------------------------------------*/ | ||
601 | |||
602 | /* ethernet function driver setup/binding */ | ||
603 | |||
604 | static int __init | ||
605 | ecm_bind(struct usb_configuration *c, struct usb_function *f) | ||
606 | { | ||
607 | struct usb_composite_dev *cdev = c->cdev; | ||
608 | struct f_ecm *ecm = func_to_ecm(f); | ||
609 | int status; | ||
610 | struct usb_ep *ep; | ||
611 | |||
612 | /* allocate instance-specific interface IDs */ | ||
613 | status = usb_interface_id(c, f); | ||
614 | if (status < 0) | ||
615 | goto fail; | ||
616 | ecm->ctrl_id = status; | ||
617 | |||
618 | ecm_control_intf.bInterfaceNumber = status; | ||
619 | ecm_union_desc.bMasterInterface0 = status; | ||
620 | |||
621 | status = usb_interface_id(c, f); | ||
622 | if (status < 0) | ||
623 | goto fail; | ||
624 | ecm->data_id = status; | ||
625 | |||
626 | ecm_data_nop_intf.bInterfaceNumber = status; | ||
627 | ecm_data_intf.bInterfaceNumber = status; | ||
628 | ecm_union_desc.bSlaveInterface0 = status; | ||
629 | |||
630 | status = -ENODEV; | ||
631 | |||
632 | /* allocate instance-specific endpoints */ | ||
633 | ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc); | ||
634 | if (!ep) | ||
635 | goto fail; | ||
636 | ecm->port.in_ep = ep; | ||
637 | ep->driver_data = cdev; /* claim */ | ||
638 | |||
639 | ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc); | ||
640 | if (!ep) | ||
641 | goto fail; | ||
642 | ecm->port.out_ep = ep; | ||
643 | ep->driver_data = cdev; /* claim */ | ||
644 | |||
645 | /* NOTE: a status/notification endpoint is *OPTIONAL* but we | ||
646 | * don't treat it that way. It's simpler, and some newer CDC | ||
647 | * profiles (wireless handsets) no longer treat it as optional. | ||
648 | */ | ||
649 | ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc); | ||
650 | if (!ep) | ||
651 | goto fail; | ||
652 | ecm->notify = ep; | ||
653 | ep->driver_data = cdev; /* claim */ | ||
654 | |||
655 | status = -ENOMEM; | ||
656 | |||
657 | /* allocate notification request and buffer */ | ||
658 | ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); | ||
659 | if (!ecm->notify_req) | ||
660 | goto fail; | ||
661 | ecm->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL); | ||
662 | if (!ecm->notify_req->buf) | ||
663 | goto fail; | ||
664 | ecm->notify_req->context = ecm; | ||
665 | ecm->notify_req->complete = ecm_notify_complete; | ||
666 | |||
667 | /* copy descriptors, and track endpoint copies */ | ||
668 | f->descriptors = usb_copy_descriptors(eth_fs_function); | ||
669 | if (!f->descriptors) | ||
670 | goto fail; | ||
671 | |||
672 | ecm->fs.in = usb_find_endpoint(eth_fs_function, | ||
673 | f->descriptors, &fs_in_desc); | ||
674 | ecm->fs.out = usb_find_endpoint(eth_fs_function, | ||
675 | f->descriptors, &fs_out_desc); | ||
676 | ecm->fs.notify = usb_find_endpoint(eth_fs_function, | ||
677 | f->descriptors, &fs_notify_desc); | ||
678 | |||
679 | /* support all relevant hardware speeds... we expect that when | ||
680 | * hardware is dual speed, all bulk-capable endpoints work at | ||
681 | * both speeds | ||
682 | */ | ||
683 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
684 | hs_in_desc.bEndpointAddress = | ||
685 | fs_in_desc.bEndpointAddress; | ||
686 | hs_out_desc.bEndpointAddress = | ||
687 | fs_out_desc.bEndpointAddress; | ||
688 | hs_notify_desc.bEndpointAddress = | ||
689 | fs_notify_desc.bEndpointAddress; | ||
690 | |||
691 | /* copy descriptors, and track endpoint copies */ | ||
692 | f->hs_descriptors = usb_copy_descriptors(eth_hs_function); | ||
693 | if (!f->hs_descriptors) | ||
694 | goto fail; | ||
695 | |||
696 | ecm->hs.in = usb_find_endpoint(eth_hs_function, | ||
697 | f->hs_descriptors, &hs_in_desc); | ||
698 | ecm->hs.out = usb_find_endpoint(eth_hs_function, | ||
699 | f->hs_descriptors, &hs_out_desc); | ||
700 | ecm->hs.notify = usb_find_endpoint(eth_hs_function, | ||
701 | f->hs_descriptors, &hs_notify_desc); | ||
702 | } | ||
703 | |||
704 | /* NOTE: all that is done without knowing or caring about | ||
705 | * the network link ... which is unavailable to this code | ||
706 | * until we're activated via set_alt(). | ||
707 | */ | ||
708 | |||
709 | ecm->port.open = ecm_open; | ||
710 | ecm->port.close = ecm_close; | ||
711 | |||
712 | DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n", | ||
713 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
714 | ecm->port.in_ep->name, ecm->port.out_ep->name, | ||
715 | ecm->notify->name); | ||
716 | return 0; | ||
717 | |||
718 | fail: | ||
719 | if (f->descriptors) | ||
720 | usb_free_descriptors(f->descriptors); | ||
721 | |||
722 | if (ecm->notify_req) { | ||
723 | kfree(ecm->notify_req->buf); | ||
724 | usb_ep_free_request(ecm->notify, ecm->notify_req); | ||
725 | } | ||
726 | |||
727 | /* we might as well release our claims on endpoints */ | ||
728 | if (ecm->notify) | ||
729 | ecm->notify->driver_data = NULL; | ||
730 | if (ecm->port.out) | ||
731 | ecm->port.out_ep->driver_data = NULL; | ||
732 | if (ecm->port.in) | ||
733 | ecm->port.in_ep->driver_data = NULL; | ||
734 | |||
735 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | ||
736 | |||
737 | return status; | ||
738 | } | ||
739 | |||
740 | static void | ||
741 | ecm_unbind(struct usb_configuration *c, struct usb_function *f) | ||
742 | { | ||
743 | struct f_ecm *ecm = func_to_ecm(f); | ||
744 | |||
745 | DBG(c->cdev, "ecm unbind\n"); | ||
746 | |||
747 | if (gadget_is_dualspeed(c->cdev->gadget)) | ||
748 | usb_free_descriptors(f->hs_descriptors); | ||
749 | usb_free_descriptors(f->descriptors); | ||
750 | |||
751 | kfree(ecm->notify_req->buf); | ||
752 | usb_ep_free_request(ecm->notify, ecm->notify_req); | ||
753 | |||
754 | ecm_string_defs[1].s = NULL; | ||
755 | kfree(ecm); | ||
756 | } | ||
757 | |||
758 | /** | ||
759 | * ecm_bind_config - add CDC Ethernet network link to a configuration | ||
760 | * @c: the configuration to support the network link | ||
761 | * @ethaddr: a buffer in which the ethernet address of the host side | ||
762 | * side of the link was recorded | ||
763 | * Context: single threaded during gadget setup | ||
764 | * | ||
765 | * Returns zero on success, else negative errno. | ||
766 | * | ||
767 | * Caller must have called @gether_setup(). Caller is also responsible | ||
768 | * for calling @gether_cleanup() before module unload. | ||
769 | */ | ||
770 | int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | ||
771 | { | ||
772 | struct f_ecm *ecm; | ||
773 | int status; | ||
774 | |||
775 | if (!can_support_ecm(c->cdev->gadget) || !ethaddr) | ||
776 | return -EINVAL; | ||
777 | |||
778 | /* maybe allocate device-global string IDs */ | ||
779 | if (ecm_string_defs[0].id == 0) { | ||
780 | |||
781 | /* control interface label */ | ||
782 | status = usb_string_id(c->cdev); | ||
783 | if (status < 0) | ||
784 | return status; | ||
785 | ecm_string_defs[0].id = status; | ||
786 | ecm_control_intf.iInterface = status; | ||
787 | |||
788 | /* data interface label */ | ||
789 | status = usb_string_id(c->cdev); | ||
790 | if (status < 0) | ||
791 | return status; | ||
792 | ecm_string_defs[2].id = status; | ||
793 | ecm_data_intf.iInterface = status; | ||
794 | |||
795 | /* MAC address */ | ||
796 | status = usb_string_id(c->cdev); | ||
797 | if (status < 0) | ||
798 | return status; | ||
799 | ecm_string_defs[1].id = status; | ||
800 | ether_desc.iMACAddress = status; | ||
801 | } | ||
802 | |||
803 | /* allocate and initialize one new instance */ | ||
804 | ecm = kzalloc(sizeof *ecm, GFP_KERNEL); | ||
805 | if (!ecm) | ||
806 | return -ENOMEM; | ||
807 | |||
808 | /* export host's Ethernet address in CDC format */ | ||
809 | snprintf(ecm->ethaddr, sizeof ecm->ethaddr, | ||
810 | "%02X%02X%02X%02X%02X%02X", | ||
811 | ethaddr[0], ethaddr[1], ethaddr[2], | ||
812 | ethaddr[3], ethaddr[4], ethaddr[5]); | ||
813 | ecm_string_defs[1].s = ecm->ethaddr; | ||
814 | |||
815 | ecm->port.cdc_filter = DEFAULT_FILTER; | ||
816 | |||
817 | ecm->port.func.name = "cdc_ethernet"; | ||
818 | ecm->port.func.strings = ecm_strings; | ||
819 | /* descriptors are per-instance copies */ | ||
820 | ecm->port.func.bind = ecm_bind; | ||
821 | ecm->port.func.unbind = ecm_unbind; | ||
822 | ecm->port.func.set_alt = ecm_set_alt; | ||
823 | ecm->port.func.get_alt = ecm_get_alt; | ||
824 | ecm->port.func.setup = ecm_setup; | ||
825 | ecm->port.func.disable = ecm_disable; | ||
826 | |||
827 | status = usb_add_function(c, &ecm->port.func); | ||
828 | if (status) { | ||
829 | ecm_string_defs[1].s = NULL; | ||
830 | kfree(ecm); | ||
831 | } | ||
832 | return status; | ||
833 | } | ||
diff --git a/drivers/usb/gadget/f_loopback.c b/drivers/usb/gadget/f_loopback.c new file mode 100644 index 000000000000..eda4cde72c82 --- /dev/null +++ b/drivers/usb/gadget/f_loopback.c | |||
@@ -0,0 +1,381 @@ | |||
1 | /* | ||
2 | * f_loopback.c - USB peripheral loopback configuration driver | ||
3 | * | ||
4 | * Copyright (C) 2003-2008 David Brownell | ||
5 | * Copyright (C) 2008 by Nokia Corporation | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | /* #define VERBOSE_DEBUG */ | ||
23 | |||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/utsname.h> | ||
26 | #include <linux/device.h> | ||
27 | |||
28 | #include "g_zero.h" | ||
29 | #include "gadget_chips.h" | ||
30 | |||
31 | |||
32 | /* | ||
33 | * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals, | ||
34 | * | ||
35 | * This takes messages of various sizes written OUT to a device, and loops | ||
36 | * them back so they can be read IN from it. It has been used by certain | ||
37 | * test applications. It supports limited testing of data queueing logic. | ||
38 | * | ||
39 | * | ||
40 | * This is currently packaged as a configuration driver, which can't be | ||
41 | * combined with other functions to make composite devices. However, it | ||
42 | * can be combined with other independent configurations. | ||
43 | */ | ||
44 | struct f_loopback { | ||
45 | struct usb_function function; | ||
46 | |||
47 | struct usb_ep *in_ep; | ||
48 | struct usb_ep *out_ep; | ||
49 | }; | ||
50 | |||
51 | static inline struct f_loopback *func_to_loop(struct usb_function *f) | ||
52 | { | ||
53 | return container_of(f, struct f_loopback, function); | ||
54 | } | ||
55 | |||
56 | static unsigned qlen = 32; | ||
57 | module_param(qlen, uint, 0); | ||
58 | MODULE_PARM_DESC(qlenn, "depth of loopback queue"); | ||
59 | |||
60 | /*-------------------------------------------------------------------------*/ | ||
61 | |||
62 | static struct usb_interface_descriptor loopback_intf = { | ||
63 | .bLength = sizeof loopback_intf, | ||
64 | .bDescriptorType = USB_DT_INTERFACE, | ||
65 | |||
66 | .bNumEndpoints = 2, | ||
67 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
68 | /* .iInterface = DYNAMIC */ | ||
69 | }; | ||
70 | |||
71 | /* full speed support: */ | ||
72 | |||
73 | static struct usb_endpoint_descriptor fs_source_desc = { | ||
74 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
75 | .bDescriptorType = USB_DT_ENDPOINT, | ||
76 | |||
77 | .bEndpointAddress = USB_DIR_IN, | ||
78 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
79 | }; | ||
80 | |||
81 | static struct usb_endpoint_descriptor fs_sink_desc = { | ||
82 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
83 | .bDescriptorType = USB_DT_ENDPOINT, | ||
84 | |||
85 | .bEndpointAddress = USB_DIR_OUT, | ||
86 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
87 | }; | ||
88 | |||
89 | static struct usb_descriptor_header *fs_loopback_descs[] = { | ||
90 | (struct usb_descriptor_header *) &loopback_intf, | ||
91 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
92 | (struct usb_descriptor_header *) &fs_source_desc, | ||
93 | NULL, | ||
94 | }; | ||
95 | |||
96 | /* high speed support: */ | ||
97 | |||
98 | static struct usb_endpoint_descriptor hs_source_desc = { | ||
99 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
100 | .bDescriptorType = USB_DT_ENDPOINT, | ||
101 | |||
102 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
103 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
104 | }; | ||
105 | |||
106 | static struct usb_endpoint_descriptor hs_sink_desc = { | ||
107 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
108 | .bDescriptorType = USB_DT_ENDPOINT, | ||
109 | |||
110 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
111 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
112 | }; | ||
113 | |||
114 | static struct usb_descriptor_header *hs_loopback_descs[] = { | ||
115 | (struct usb_descriptor_header *) &loopback_intf, | ||
116 | (struct usb_descriptor_header *) &hs_source_desc, | ||
117 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
118 | NULL, | ||
119 | }; | ||
120 | |||
121 | /* function-specific strings: */ | ||
122 | |||
123 | static struct usb_string strings_loopback[] = { | ||
124 | [0].s = "loop input to output", | ||
125 | { } /* end of list */ | ||
126 | }; | ||
127 | |||
128 | static struct usb_gadget_strings stringtab_loop = { | ||
129 | .language = 0x0409, /* en-us */ | ||
130 | .strings = strings_loopback, | ||
131 | }; | ||
132 | |||
133 | static struct usb_gadget_strings *loopback_strings[] = { | ||
134 | &stringtab_loop, | ||
135 | NULL, | ||
136 | }; | ||
137 | |||
138 | /*-------------------------------------------------------------------------*/ | ||
139 | |||
140 | static int __init | ||
141 | loopback_bind(struct usb_configuration *c, struct usb_function *f) | ||
142 | { | ||
143 | struct usb_composite_dev *cdev = c->cdev; | ||
144 | struct f_loopback *loop = func_to_loop(f); | ||
145 | int id; | ||
146 | |||
147 | /* allocate interface ID(s) */ | ||
148 | id = usb_interface_id(c, f); | ||
149 | if (id < 0) | ||
150 | return id; | ||
151 | loopback_intf.bInterfaceNumber = id; | ||
152 | |||
153 | /* allocate endpoints */ | ||
154 | |||
155 | loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); | ||
156 | if (!loop->in_ep) { | ||
157 | autoconf_fail: | ||
158 | ERROR(cdev, "%s: can't autoconfigure on %s\n", | ||
159 | f->name, cdev->gadget->name); | ||
160 | return -ENODEV; | ||
161 | } | ||
162 | loop->in_ep->driver_data = cdev; /* claim */ | ||
163 | |||
164 | loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); | ||
165 | if (!loop->out_ep) | ||
166 | goto autoconf_fail; | ||
167 | loop->out_ep->driver_data = cdev; /* claim */ | ||
168 | |||
169 | /* support high speed hardware */ | ||
170 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
171 | hs_source_desc.bEndpointAddress = | ||
172 | fs_source_desc.bEndpointAddress; | ||
173 | hs_sink_desc.bEndpointAddress = | ||
174 | fs_sink_desc.bEndpointAddress; | ||
175 | f->hs_descriptors = hs_loopback_descs; | ||
176 | } | ||
177 | |||
178 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", | ||
179 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
180 | f->name, loop->in_ep->name, loop->out_ep->name); | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | static void | ||
185 | loopback_unbind(struct usb_configuration *c, struct usb_function *f) | ||
186 | { | ||
187 | kfree(func_to_loop(f)); | ||
188 | } | ||
189 | |||
190 | static void loopback_complete(struct usb_ep *ep, struct usb_request *req) | ||
191 | { | ||
192 | struct f_loopback *loop = ep->driver_data; | ||
193 | struct usb_composite_dev *cdev = loop->function.config->cdev; | ||
194 | int status = req->status; | ||
195 | |||
196 | switch (status) { | ||
197 | |||
198 | case 0: /* normal completion? */ | ||
199 | if (ep == loop->out_ep) { | ||
200 | /* loop this OUT packet back IN to the host */ | ||
201 | req->zero = (req->actual < req->length); | ||
202 | req->length = req->actual; | ||
203 | status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC); | ||
204 | if (status == 0) | ||
205 | return; | ||
206 | |||
207 | /* "should never get here" */ | ||
208 | ERROR(cdev, "can't loop %s to %s: %d\n", | ||
209 | ep->name, loop->in_ep->name, | ||
210 | status); | ||
211 | } | ||
212 | |||
213 | /* queue the buffer for some later OUT packet */ | ||
214 | req->length = buflen; | ||
215 | status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC); | ||
216 | if (status == 0) | ||
217 | return; | ||
218 | |||
219 | /* "should never get here" */ | ||
220 | /* FALLTHROUGH */ | ||
221 | |||
222 | default: | ||
223 | ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name, | ||
224 | status, req->actual, req->length); | ||
225 | /* FALLTHROUGH */ | ||
226 | |||
227 | /* NOTE: since this driver doesn't maintain an explicit record | ||
228 | * of requests it submitted (just maintains qlen count), we | ||
229 | * rely on the hardware driver to clean up on disconnect or | ||
230 | * endpoint disable. | ||
231 | */ | ||
232 | case -ECONNABORTED: /* hardware forced ep reset */ | ||
233 | case -ECONNRESET: /* request dequeued */ | ||
234 | case -ESHUTDOWN: /* disconnect from host */ | ||
235 | free_ep_req(ep, req); | ||
236 | return; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | static void disable_loopback(struct f_loopback *loop) | ||
241 | { | ||
242 | struct usb_composite_dev *cdev; | ||
243 | |||
244 | cdev = loop->function.config->cdev; | ||
245 | disable_endpoints(cdev, loop->in_ep, loop->out_ep); | ||
246 | VDBG(cdev, "%s disabled\n", loop->function.name); | ||
247 | } | ||
248 | |||
249 | static int | ||
250 | enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop) | ||
251 | { | ||
252 | int result = 0; | ||
253 | const struct usb_endpoint_descriptor *src, *sink; | ||
254 | struct usb_ep *ep; | ||
255 | struct usb_request *req; | ||
256 | unsigned i; | ||
257 | |||
258 | src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc); | ||
259 | sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc); | ||
260 | |||
261 | /* one endpoint writes data back IN to the host */ | ||
262 | ep = loop->in_ep; | ||
263 | result = usb_ep_enable(ep, src); | ||
264 | if (result < 0) | ||
265 | return result; | ||
266 | ep->driver_data = loop; | ||
267 | |||
268 | /* one endpoint just reads OUT packets */ | ||
269 | ep = loop->out_ep; | ||
270 | result = usb_ep_enable(ep, sink); | ||
271 | if (result < 0) { | ||
272 | fail0: | ||
273 | ep = loop->in_ep; | ||
274 | usb_ep_disable(ep); | ||
275 | ep->driver_data = NULL; | ||
276 | return result; | ||
277 | } | ||
278 | ep->driver_data = loop; | ||
279 | |||
280 | /* allocate a bunch of read buffers and queue them all at once. | ||
281 | * we buffer at most 'qlen' transfers; fewer if any need more | ||
282 | * than 'buflen' bytes each. | ||
283 | */ | ||
284 | for (i = 0; i < qlen && result == 0; i++) { | ||
285 | req = alloc_ep_req(ep); | ||
286 | if (req) { | ||
287 | req->complete = loopback_complete; | ||
288 | result = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
289 | if (result) | ||
290 | ERROR(cdev, "%s queue req --> %d\n", | ||
291 | ep->name, result); | ||
292 | } else { | ||
293 | usb_ep_disable(ep); | ||
294 | ep->driver_data = NULL; | ||
295 | result = -ENOMEM; | ||
296 | goto fail0; | ||
297 | } | ||
298 | } | ||
299 | |||
300 | DBG(cdev, "%s enabled\n", loop->function.name); | ||
301 | return result; | ||
302 | } | ||
303 | |||
304 | static int loopback_set_alt(struct usb_function *f, | ||
305 | unsigned intf, unsigned alt) | ||
306 | { | ||
307 | struct f_loopback *loop = func_to_loop(f); | ||
308 | struct usb_composite_dev *cdev = f->config->cdev; | ||
309 | |||
310 | /* we know alt is zero */ | ||
311 | if (loop->in_ep->driver_data) | ||
312 | disable_loopback(loop); | ||
313 | return enable_loopback(cdev, loop); | ||
314 | } | ||
315 | |||
316 | static void loopback_disable(struct usb_function *f) | ||
317 | { | ||
318 | struct f_loopback *loop = func_to_loop(f); | ||
319 | |||
320 | disable_loopback(loop); | ||
321 | } | ||
322 | |||
323 | /*-------------------------------------------------------------------------*/ | ||
324 | |||
325 | static int __init loopback_bind_config(struct usb_configuration *c) | ||
326 | { | ||
327 | struct f_loopback *loop; | ||
328 | int status; | ||
329 | |||
330 | loop = kzalloc(sizeof *loop, GFP_KERNEL); | ||
331 | if (!loop) | ||
332 | return -ENOMEM; | ||
333 | |||
334 | loop->function.name = "loopback"; | ||
335 | loop->function.descriptors = fs_loopback_descs; | ||
336 | loop->function.bind = loopback_bind; | ||
337 | loop->function.unbind = loopback_unbind; | ||
338 | loop->function.set_alt = loopback_set_alt; | ||
339 | loop->function.disable = loopback_disable; | ||
340 | |||
341 | status = usb_add_function(c, &loop->function); | ||
342 | if (status) | ||
343 | kfree(loop); | ||
344 | return status; | ||
345 | } | ||
346 | |||
347 | static struct usb_configuration loopback_driver = { | ||
348 | .label = "loopback", | ||
349 | .strings = loopback_strings, | ||
350 | .bind = loopback_bind_config, | ||
351 | .bConfigurationValue = 2, | ||
352 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | ||
353 | .bMaxPower = 1, /* 2 mA, minimal */ | ||
354 | /* .iConfiguration = DYNAMIC */ | ||
355 | }; | ||
356 | |||
357 | /** | ||
358 | * loopback_add - add a loopback testing configuration to a device | ||
359 | * @cdev: the device to support the loopback configuration | ||
360 | */ | ||
361 | int __init loopback_add(struct usb_composite_dev *cdev) | ||
362 | { | ||
363 | int id; | ||
364 | |||
365 | /* allocate string ID(s) */ | ||
366 | id = usb_string_id(cdev); | ||
367 | if (id < 0) | ||
368 | return id; | ||
369 | strings_loopback[0].id = id; | ||
370 | |||
371 | loopback_intf.iInterface = id; | ||
372 | loopback_driver.iConfiguration = id; | ||
373 | |||
374 | /* support OTG systems */ | ||
375 | if (gadget_is_otg(cdev->gadget)) { | ||
376 | loopback_driver.descriptors = otg_desc; | ||
377 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
378 | } | ||
379 | |||
380 | return usb_add_config(cdev, &loopback_driver); | ||
381 | } | ||
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c new file mode 100644 index 000000000000..61652f0f13fd --- /dev/null +++ b/drivers/usb/gadget/f_rndis.c | |||
@@ -0,0 +1,827 @@ | |||
1 | /* | ||
2 | * f_rndis.c -- RNDIS link function driver | ||
3 | * | ||
4 | * Copyright (C) 2003-2005,2008 David Brownell | ||
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | ||
6 | * Copyright (C) 2008 Nokia Corporation | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | /* #define VERBOSE_DEBUG */ | ||
24 | |||
25 | #include <linux/kernel.h> | ||
26 | #include <linux/device.h> | ||
27 | #include <linux/etherdevice.h> | ||
28 | |||
29 | #include <asm/atomic.h> | ||
30 | |||
31 | #include "u_ether.h" | ||
32 | #include "rndis.h" | ||
33 | |||
34 | |||
35 | /* | ||
36 | * This function is an RNDIS Ethernet port -- a Microsoft protocol that's | ||
37 | * been promoted instead of the standard CDC Ethernet. The published RNDIS | ||
38 | * spec is ambiguous, incomplete, and needlessly complex. Variants such as | ||
39 | * ActiveSync have even worse status in terms of specification. | ||
40 | * | ||
41 | * In short: it's a protocol controlled by (and for) Microsoft, not for an | ||
42 | * Open ecosystem or markets. Linux supports it *only* because Microsoft | ||
43 | * doesn't support the CDC Ethernet standard. | ||
44 | * | ||
45 | * The RNDIS data transfer model is complex, with multiple Ethernet packets | ||
46 | * per USB message, and out of band data. The control model is built around | ||
47 | * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM | ||
48 | * (modem, not Ethernet) veneer, with those ACM descriptors being entirely | ||
49 | * useless (they're ignored). RNDIS expects to be the only function in its | ||
50 | * configuration, so it's no real help if you need composite devices; and | ||
51 | * it expects to be the first configuration too. | ||
52 | * | ||
53 | * There is a single technical advantage of RNDIS over CDC Ethernet, if you | ||
54 | * discount the fluff that its RPC can be made to deliver: it doesn't need | ||
55 | * a NOP altsetting for the data interface. That lets it work on some of the | ||
56 | * "so smart it's stupid" hardware which takes over configuration changes | ||
57 | * from the software, and adds restrictions like "no altsettings". | ||
58 | * | ||
59 | * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and | ||
60 | * have all sorts of contrary-to-specification oddities that can prevent | ||
61 | * them from working sanely. Since bugfixes (or accurate specs, letting | ||
62 | * Linux work around those bugs) are unlikely to ever come from MSFT, you | ||
63 | * may want to avoid using RNDIS on purely operational grounds. | ||
64 | * | ||
65 | * Omissions from the RNDIS 1.0 specification include: | ||
66 | * | ||
67 | * - Power management ... references data that's scattered around lots | ||
68 | * of other documentation, which is incorrect/incomplete there too. | ||
69 | * | ||
70 | * - There are various undocumented protocol requirements, like the need | ||
71 | * to send garbage in some control-OUT messages. | ||
72 | * | ||
73 | * - MS-Windows drivers sometimes emit undocumented requests. | ||
74 | */ | ||
75 | |||
76 | struct rndis_ep_descs { | ||
77 | struct usb_endpoint_descriptor *in; | ||
78 | struct usb_endpoint_descriptor *out; | ||
79 | struct usb_endpoint_descriptor *notify; | ||
80 | }; | ||
81 | |||
82 | struct f_rndis { | ||
83 | struct gether port; | ||
84 | u8 ctrl_id, data_id; | ||
85 | u8 ethaddr[ETH_ALEN]; | ||
86 | int config; | ||
87 | |||
88 | struct usb_descriptor_header **fs_function; | ||
89 | struct rndis_ep_descs fs; | ||
90 | struct usb_descriptor_header **hs_function; | ||
91 | struct rndis_ep_descs hs; | ||
92 | |||
93 | struct usb_ep *notify; | ||
94 | struct usb_endpoint_descriptor *notify_desc; | ||
95 | struct usb_request *notify_req; | ||
96 | atomic_t notify_count; | ||
97 | }; | ||
98 | |||
99 | static inline struct f_rndis *func_to_rndis(struct usb_function *f) | ||
100 | { | ||
101 | return container_of(f, struct f_rndis, port.func); | ||
102 | } | ||
103 | |||
104 | /* peak (theoretical) bulk transfer rate in bits-per-second */ | ||
105 | static unsigned int bitrate(struct usb_gadget *g) | ||
106 | { | ||
107 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
108 | return 13 * 512 * 8 * 1000 * 8; | ||
109 | else | ||
110 | return 19 * 64 * 1 * 1000 * 8; | ||
111 | } | ||
112 | |||
113 | /*-------------------------------------------------------------------------*/ | ||
114 | |||
115 | /* | ||
116 | */ | ||
117 | |||
118 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ | ||
119 | #define STATUS_BYTECOUNT 8 /* 8 bytes data */ | ||
120 | |||
121 | |||
122 | /* interface descriptor: */ | ||
123 | |||
124 | static struct usb_interface_descriptor rndis_control_intf __initdata = { | ||
125 | .bLength = sizeof rndis_control_intf, | ||
126 | .bDescriptorType = USB_DT_INTERFACE, | ||
127 | |||
128 | /* .bInterfaceNumber = DYNAMIC */ | ||
129 | /* status endpoint is optional; this could be patched later */ | ||
130 | .bNumEndpoints = 1, | ||
131 | .bInterfaceClass = USB_CLASS_COMM, | ||
132 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | ||
133 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, | ||
134 | /* .iInterface = DYNAMIC */ | ||
135 | }; | ||
136 | |||
137 | static struct usb_cdc_header_desc header_desc __initdata = { | ||
138 | .bLength = sizeof header_desc, | ||
139 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
140 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
141 | |||
142 | .bcdCDC = __constant_cpu_to_le16(0x0110), | ||
143 | }; | ||
144 | |||
145 | static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = { | ||
146 | .bLength = sizeof call_mgmt_descriptor, | ||
147 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
148 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | ||
149 | |||
150 | .bmCapabilities = 0x00, | ||
151 | .bDataInterface = 0x01, | ||
152 | }; | ||
153 | |||
154 | static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { | ||
155 | .bLength = sizeof acm_descriptor, | ||
156 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
157 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | ||
158 | |||
159 | .bmCapabilities = 0x00, | ||
160 | }; | ||
161 | |||
162 | static struct usb_cdc_union_desc rndis_union_desc __initdata = { | ||
163 | .bLength = sizeof(rndis_union_desc), | ||
164 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
165 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | ||
166 | /* .bMasterInterface0 = DYNAMIC */ | ||
167 | /* .bSlaveInterface0 = DYNAMIC */ | ||
168 | }; | ||
169 | |||
170 | /* the data interface has two bulk endpoints */ | ||
171 | |||
172 | static struct usb_interface_descriptor rndis_data_intf __initdata = { | ||
173 | .bLength = sizeof rndis_data_intf, | ||
174 | .bDescriptorType = USB_DT_INTERFACE, | ||
175 | |||
176 | /* .bInterfaceNumber = DYNAMIC */ | ||
177 | .bAlternateSetting = 1, | ||
178 | .bNumEndpoints = 2, | ||
179 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
180 | .bInterfaceSubClass = 0, | ||
181 | .bInterfaceProtocol = 0, | ||
182 | /* .iInterface = DYNAMIC */ | ||
183 | }; | ||
184 | |||
185 | /* full speed support: */ | ||
186 | |||
187 | static struct usb_endpoint_descriptor fs_notify_desc __initdata = { | ||
188 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
189 | .bDescriptorType = USB_DT_ENDPOINT, | ||
190 | |||
191 | .bEndpointAddress = USB_DIR_IN, | ||
192 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
193 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), | ||
194 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, | ||
195 | }; | ||
196 | |||
197 | static struct usb_endpoint_descriptor fs_in_desc __initdata = { | ||
198 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
199 | .bDescriptorType = USB_DT_ENDPOINT, | ||
200 | |||
201 | .bEndpointAddress = USB_DIR_IN, | ||
202 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
203 | }; | ||
204 | |||
205 | static struct usb_endpoint_descriptor fs_out_desc __initdata = { | ||
206 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
207 | .bDescriptorType = USB_DT_ENDPOINT, | ||
208 | |||
209 | .bEndpointAddress = USB_DIR_OUT, | ||
210 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
211 | }; | ||
212 | |||
213 | static struct usb_descriptor_header *eth_fs_function[] __initdata = { | ||
214 | /* control interface matches ACM, not Ethernet */ | ||
215 | (struct usb_descriptor_header *) &rndis_control_intf, | ||
216 | (struct usb_descriptor_header *) &header_desc, | ||
217 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | ||
218 | (struct usb_descriptor_header *) &acm_descriptor, | ||
219 | (struct usb_descriptor_header *) &rndis_union_desc, | ||
220 | (struct usb_descriptor_header *) &fs_notify_desc, | ||
221 | /* data interface has no altsetting */ | ||
222 | (struct usb_descriptor_header *) &rndis_data_intf, | ||
223 | (struct usb_descriptor_header *) &fs_in_desc, | ||
224 | (struct usb_descriptor_header *) &fs_out_desc, | ||
225 | NULL, | ||
226 | }; | ||
227 | |||
228 | /* high speed support: */ | ||
229 | |||
230 | static struct usb_endpoint_descriptor hs_notify_desc __initdata = { | ||
231 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
232 | .bDescriptorType = USB_DT_ENDPOINT, | ||
233 | |||
234 | .bEndpointAddress = USB_DIR_IN, | ||
235 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
236 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), | ||
237 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, | ||
238 | }; | ||
239 | static struct usb_endpoint_descriptor hs_in_desc __initdata = { | ||
240 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
241 | .bDescriptorType = USB_DT_ENDPOINT, | ||
242 | |||
243 | .bEndpointAddress = USB_DIR_IN, | ||
244 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
245 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
246 | }; | ||
247 | |||
248 | static struct usb_endpoint_descriptor hs_out_desc __initdata = { | ||
249 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
250 | .bDescriptorType = USB_DT_ENDPOINT, | ||
251 | |||
252 | .bEndpointAddress = USB_DIR_OUT, | ||
253 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
254 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
255 | }; | ||
256 | |||
257 | static struct usb_descriptor_header *eth_hs_function[] __initdata = { | ||
258 | /* control interface matches ACM, not Ethernet */ | ||
259 | (struct usb_descriptor_header *) &rndis_control_intf, | ||
260 | (struct usb_descriptor_header *) &header_desc, | ||
261 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | ||
262 | (struct usb_descriptor_header *) &acm_descriptor, | ||
263 | (struct usb_descriptor_header *) &rndis_union_desc, | ||
264 | (struct usb_descriptor_header *) &hs_notify_desc, | ||
265 | /* data interface has no altsetting */ | ||
266 | (struct usb_descriptor_header *) &rndis_data_intf, | ||
267 | (struct usb_descriptor_header *) &hs_in_desc, | ||
268 | (struct usb_descriptor_header *) &hs_out_desc, | ||
269 | NULL, | ||
270 | }; | ||
271 | |||
272 | /* string descriptors: */ | ||
273 | |||
274 | static struct usb_string rndis_string_defs[] = { | ||
275 | [0].s = "RNDIS Communications Control", | ||
276 | [1].s = "RNDIS Ethernet Data", | ||
277 | { } /* end of list */ | ||
278 | }; | ||
279 | |||
280 | static struct usb_gadget_strings rndis_string_table = { | ||
281 | .language = 0x0409, /* en-us */ | ||
282 | .strings = rndis_string_defs, | ||
283 | }; | ||
284 | |||
285 | static struct usb_gadget_strings *rndis_strings[] = { | ||
286 | &rndis_string_table, | ||
287 | NULL, | ||
288 | }; | ||
289 | |||
290 | /*-------------------------------------------------------------------------*/ | ||
291 | |||
292 | static struct sk_buff *rndis_add_header(struct sk_buff *skb) | ||
293 | { | ||
294 | skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); | ||
295 | if (skb) | ||
296 | rndis_add_hdr(skb); | ||
297 | return skb; | ||
298 | } | ||
299 | |||
300 | static void rndis_response_available(void *_rndis) | ||
301 | { | ||
302 | struct f_rndis *rndis = _rndis; | ||
303 | struct usb_request *req = rndis->notify_req; | ||
304 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; | ||
305 | __le32 *data = req->buf; | ||
306 | int status; | ||
307 | |||
308 | if (atomic_inc_return(&rndis->notify_count)) | ||
309 | return; | ||
310 | |||
311 | /* Send RNDIS RESPONSE_AVAILABLE notification; a | ||
312 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too | ||
313 | * | ||
314 | * This is the only notification defined by RNDIS. | ||
315 | */ | ||
316 | data[0] = cpu_to_le32(1); | ||
317 | data[1] = cpu_to_le32(0); | ||
318 | |||
319 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); | ||
320 | if (status) { | ||
321 | atomic_dec(&rndis->notify_count); | ||
322 | DBG(cdev, "notify/0 --> %d\n", status); | ||
323 | } | ||
324 | } | ||
325 | |||
326 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) | ||
327 | { | ||
328 | struct f_rndis *rndis = req->context; | ||
329 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; | ||
330 | int status = req->status; | ||
331 | |||
332 | /* after TX: | ||
333 | * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control) | ||
334 | * - RNDIS_RESPONSE_AVAILABLE (status/irq) | ||
335 | */ | ||
336 | switch (status) { | ||
337 | case -ECONNRESET: | ||
338 | case -ESHUTDOWN: | ||
339 | /* connection gone */ | ||
340 | atomic_set(&rndis->notify_count, 0); | ||
341 | break; | ||
342 | default: | ||
343 | DBG(cdev, "RNDIS %s response error %d, %d/%d\n", | ||
344 | ep->name, status, | ||
345 | req->actual, req->length); | ||
346 | /* FALLTHROUGH */ | ||
347 | case 0: | ||
348 | if (ep != rndis->notify) | ||
349 | break; | ||
350 | |||
351 | /* handle multiple pending RNDIS_RESPONSE_AVAILABLE | ||
352 | * notifications by resending until we're done | ||
353 | */ | ||
354 | if (atomic_dec_and_test(&rndis->notify_count)) | ||
355 | break; | ||
356 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); | ||
357 | if (status) { | ||
358 | atomic_dec(&rndis->notify_count); | ||
359 | DBG(cdev, "notify/1 --> %d\n", status); | ||
360 | } | ||
361 | break; | ||
362 | } | ||
363 | } | ||
364 | |||
365 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) | ||
366 | { | ||
367 | struct f_rndis *rndis = req->context; | ||
368 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; | ||
369 | int status; | ||
370 | |||
371 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ | ||
372 | // spin_lock(&dev->lock); | ||
373 | status = rndis_msg_parser(rndis->config, (u8 *) req->buf); | ||
374 | if (status < 0) | ||
375 | ERROR(cdev, "RNDIS command error %d, %d/%d\n", | ||
376 | status, req->actual, req->length); | ||
377 | // spin_unlock(&dev->lock); | ||
378 | } | ||
379 | |||
380 | static int | ||
381 | rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | ||
382 | { | ||
383 | struct f_rndis *rndis = func_to_rndis(f); | ||
384 | struct usb_composite_dev *cdev = f->config->cdev; | ||
385 | struct usb_request *req = cdev->req; | ||
386 | int value = -EOPNOTSUPP; | ||
387 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
388 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
389 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
390 | |||
391 | /* composite driver infrastructure handles everything except | ||
392 | * CDC class messages; interface activation uses set_alt(). | ||
393 | */ | ||
394 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | ||
395 | |||
396 | /* RNDIS uses the CDC command encapsulation mechanism to implement | ||
397 | * an RPC scheme, with much getting/setting of attributes by OID. | ||
398 | */ | ||
399 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
400 | | USB_CDC_SEND_ENCAPSULATED_COMMAND: | ||
401 | if (w_length > req->length || w_value | ||
402 | || w_index != rndis->ctrl_id) | ||
403 | goto invalid; | ||
404 | /* read the request; process it later */ | ||
405 | value = w_length; | ||
406 | req->complete = rndis_command_complete; | ||
407 | req->context = rndis; | ||
408 | /* later, rndis_response_available() sends a notification */ | ||
409 | break; | ||
410 | |||
411 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | ||
412 | | USB_CDC_GET_ENCAPSULATED_RESPONSE: | ||
413 | if (w_value || w_index != rndis->ctrl_id) | ||
414 | goto invalid; | ||
415 | else { | ||
416 | u8 *buf; | ||
417 | u32 n; | ||
418 | |||
419 | /* return the result */ | ||
420 | buf = rndis_get_next_response(rndis->config, &n); | ||
421 | if (buf) { | ||
422 | memcpy(req->buf, buf, n); | ||
423 | req->complete = rndis_response_complete; | ||
424 | rndis_free_response(rndis->config, buf); | ||
425 | value = n; | ||
426 | } | ||
427 | /* else stalls ... spec says to avoid that */ | ||
428 | } | ||
429 | break; | ||
430 | |||
431 | default: | ||
432 | invalid: | ||
433 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | ||
434 | ctrl->bRequestType, ctrl->bRequest, | ||
435 | w_value, w_index, w_length); | ||
436 | } | ||
437 | |||
438 | /* respond with data transfer or status phase? */ | ||
439 | if (value >= 0) { | ||
440 | DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n", | ||
441 | ctrl->bRequestType, ctrl->bRequest, | ||
442 | w_value, w_index, w_length); | ||
443 | req->zero = 0; | ||
444 | req->length = value; | ||
445 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | ||
446 | if (value < 0) | ||
447 | ERROR(cdev, "rndis response on err %d\n", value); | ||
448 | } | ||
449 | |||
450 | /* device either stalls (value < 0) or reports success */ | ||
451 | return value; | ||
452 | } | ||
453 | |||
454 | |||
455 | static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | ||
456 | { | ||
457 | struct f_rndis *rndis = func_to_rndis(f); | ||
458 | struct usb_composite_dev *cdev = f->config->cdev; | ||
459 | |||
460 | /* we know alt == 0 */ | ||
461 | |||
462 | if (intf == rndis->ctrl_id) { | ||
463 | if (rndis->notify->driver_data) { | ||
464 | VDBG(cdev, "reset rndis control %d\n", intf); | ||
465 | usb_ep_disable(rndis->notify); | ||
466 | } else { | ||
467 | VDBG(cdev, "init rndis ctrl %d\n", intf); | ||
468 | rndis->notify_desc = ep_choose(cdev->gadget, | ||
469 | rndis->hs.notify, | ||
470 | rndis->fs.notify); | ||
471 | } | ||
472 | usb_ep_enable(rndis->notify, rndis->notify_desc); | ||
473 | rndis->notify->driver_data = rndis; | ||
474 | |||
475 | } else if (intf == rndis->data_id) { | ||
476 | struct net_device *net; | ||
477 | |||
478 | if (rndis->port.in_ep->driver_data) { | ||
479 | DBG(cdev, "reset rndis\n"); | ||
480 | gether_disconnect(&rndis->port); | ||
481 | } else { | ||
482 | DBG(cdev, "init rndis\n"); | ||
483 | rndis->port.in = ep_choose(cdev->gadget, | ||
484 | rndis->hs.in, rndis->fs.in); | ||
485 | rndis->port.out = ep_choose(cdev->gadget, | ||
486 | rndis->hs.out, rndis->fs.out); | ||
487 | } | ||
488 | |||
489 | /* Avoid ZLPs; they can be troublesome. */ | ||
490 | rndis->port.is_zlp_ok = false; | ||
491 | |||
492 | /* RNDIS should be in the "RNDIS uninitialized" state, | ||
493 | * either never activated or after rndis_uninit(). | ||
494 | * | ||
495 | * We don't want data to flow here until a nonzero packet | ||
496 | * filter is set, at which point it enters "RNDIS data | ||
497 | * initialized" state ... but we do want the endpoints | ||
498 | * to be activated. It's a strange little state. | ||
499 | * | ||
500 | * REVISIT the RNDIS gadget code has done this wrong for a | ||
501 | * very long time. We need another call to the link layer | ||
502 | * code -- gether_updown(...bool) maybe -- to do it right. | ||
503 | */ | ||
504 | rndis->port.cdc_filter = 0; | ||
505 | |||
506 | DBG(cdev, "RNDIS RX/TX early activation ... \n"); | ||
507 | net = gether_connect(&rndis->port); | ||
508 | if (IS_ERR(net)) | ||
509 | return PTR_ERR(net); | ||
510 | |||
511 | rndis_set_param_dev(rndis->config, net, | ||
512 | &rndis->port.cdc_filter); | ||
513 | } else | ||
514 | goto fail; | ||
515 | |||
516 | return 0; | ||
517 | fail: | ||
518 | return -EINVAL; | ||
519 | } | ||
520 | |||
521 | static void rndis_disable(struct usb_function *f) | ||
522 | { | ||
523 | struct f_rndis *rndis = func_to_rndis(f); | ||
524 | struct usb_composite_dev *cdev = f->config->cdev; | ||
525 | |||
526 | if (!rndis->notify->driver_data) | ||
527 | return; | ||
528 | |||
529 | DBG(cdev, "rndis deactivated\n"); | ||
530 | |||
531 | rndis_uninit(rndis->config); | ||
532 | gether_disconnect(&rndis->port); | ||
533 | |||
534 | usb_ep_disable(rndis->notify); | ||
535 | rndis->notify->driver_data = NULL; | ||
536 | } | ||
537 | |||
538 | /*-------------------------------------------------------------------------*/ | ||
539 | |||
540 | /* | ||
541 | * This isn't quite the same mechanism as CDC Ethernet, since the | ||
542 | * notification scheme passes less data, but the same set of link | ||
543 | * states must be tested. A key difference is that altsettings are | ||
544 | * not used to tell whether the link should send packets or not. | ||
545 | */ | ||
546 | |||
547 | static void rndis_open(struct gether *geth) | ||
548 | { | ||
549 | struct f_rndis *rndis = func_to_rndis(&geth->func); | ||
550 | struct usb_composite_dev *cdev = geth->func.config->cdev; | ||
551 | |||
552 | DBG(cdev, "%s\n", __func__); | ||
553 | |||
554 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, | ||
555 | bitrate(cdev->gadget) / 100); | ||
556 | rndis_signal_connect(rndis->config); | ||
557 | } | ||
558 | |||
559 | static void rndis_close(struct gether *geth) | ||
560 | { | ||
561 | struct f_rndis *rndis = func_to_rndis(&geth->func); | ||
562 | |||
563 | DBG(geth->func.config->cdev, "%s\n", __func__); | ||
564 | |||
565 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); | ||
566 | rndis_signal_disconnect(rndis->config); | ||
567 | } | ||
568 | |||
569 | /*-------------------------------------------------------------------------*/ | ||
570 | |||
571 | /* ethernet function driver setup/binding */ | ||
572 | |||
573 | static int __init | ||
574 | rndis_bind(struct usb_configuration *c, struct usb_function *f) | ||
575 | { | ||
576 | struct usb_composite_dev *cdev = c->cdev; | ||
577 | struct f_rndis *rndis = func_to_rndis(f); | ||
578 | int status; | ||
579 | struct usb_ep *ep; | ||
580 | |||
581 | /* allocate instance-specific interface IDs */ | ||
582 | status = usb_interface_id(c, f); | ||
583 | if (status < 0) | ||
584 | goto fail; | ||
585 | rndis->ctrl_id = status; | ||
586 | |||
587 | rndis_control_intf.bInterfaceNumber = status; | ||
588 | rndis_union_desc.bMasterInterface0 = status; | ||
589 | |||
590 | status = usb_interface_id(c, f); | ||
591 | if (status < 0) | ||
592 | goto fail; | ||
593 | rndis->data_id = status; | ||
594 | |||
595 | rndis_data_intf.bInterfaceNumber = status; | ||
596 | rndis_union_desc.bSlaveInterface0 = status; | ||
597 | |||
598 | status = -ENODEV; | ||
599 | |||
600 | /* allocate instance-specific endpoints */ | ||
601 | ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc); | ||
602 | if (!ep) | ||
603 | goto fail; | ||
604 | rndis->port.in_ep = ep; | ||
605 | ep->driver_data = cdev; /* claim */ | ||
606 | |||
607 | ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc); | ||
608 | if (!ep) | ||
609 | goto fail; | ||
610 | rndis->port.out_ep = ep; | ||
611 | ep->driver_data = cdev; /* claim */ | ||
612 | |||
613 | /* NOTE: a status/notification endpoint is, strictly speaking, | ||
614 | * optional. We don't treat it that way though! It's simpler, | ||
615 | * and some newer profiles don't treat it as optional. | ||
616 | */ | ||
617 | ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc); | ||
618 | if (!ep) | ||
619 | goto fail; | ||
620 | rndis->notify = ep; | ||
621 | ep->driver_data = cdev; /* claim */ | ||
622 | |||
623 | status = -ENOMEM; | ||
624 | |||
625 | /* allocate notification request and buffer */ | ||
626 | rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); | ||
627 | if (!rndis->notify_req) | ||
628 | goto fail; | ||
629 | rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL); | ||
630 | if (!rndis->notify_req->buf) | ||
631 | goto fail; | ||
632 | rndis->notify_req->length = STATUS_BYTECOUNT; | ||
633 | rndis->notify_req->context = rndis; | ||
634 | rndis->notify_req->complete = rndis_response_complete; | ||
635 | |||
636 | /* copy descriptors, and track endpoint copies */ | ||
637 | f->descriptors = usb_copy_descriptors(eth_fs_function); | ||
638 | if (!f->descriptors) | ||
639 | goto fail; | ||
640 | |||
641 | rndis->fs.in = usb_find_endpoint(eth_fs_function, | ||
642 | f->descriptors, &fs_in_desc); | ||
643 | rndis->fs.out = usb_find_endpoint(eth_fs_function, | ||
644 | f->descriptors, &fs_out_desc); | ||
645 | rndis->fs.notify = usb_find_endpoint(eth_fs_function, | ||
646 | f->descriptors, &fs_notify_desc); | ||
647 | |||
648 | /* support all relevant hardware speeds... we expect that when | ||
649 | * hardware is dual speed, all bulk-capable endpoints work at | ||
650 | * both speeds | ||
651 | */ | ||
652 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
653 | hs_in_desc.bEndpointAddress = | ||
654 | fs_in_desc.bEndpointAddress; | ||
655 | hs_out_desc.bEndpointAddress = | ||
656 | fs_out_desc.bEndpointAddress; | ||
657 | |||
658 | /* copy descriptors, and track endpoint copies */ | ||
659 | f->hs_descriptors = usb_copy_descriptors(eth_hs_function); | ||
660 | |||
661 | if (!f->hs_descriptors) | ||
662 | goto fail; | ||
663 | |||
664 | rndis->hs.in = usb_find_endpoint(eth_hs_function, | ||
665 | f->hs_descriptors, &hs_in_desc); | ||
666 | rndis->hs.out = usb_find_endpoint(eth_hs_function, | ||
667 | f->hs_descriptors, &hs_out_desc); | ||
668 | } | ||
669 | |||
670 | rndis->port.open = rndis_open; | ||
671 | rndis->port.close = rndis_close; | ||
672 | |||
673 | status = rndis_register(rndis_response_available, rndis); | ||
674 | if (status < 0) | ||
675 | goto fail; | ||
676 | rndis->config = status; | ||
677 | |||
678 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); | ||
679 | rndis_set_host_mac(rndis->config, rndis->ethaddr); | ||
680 | |||
681 | #if 0 | ||
682 | // FIXME | ||
683 | if (rndis_set_param_vendor(rndis->config, vendorID, | ||
684 | manufacturer)) | ||
685 | goto fail0; | ||
686 | #endif | ||
687 | |||
688 | /* NOTE: all that is done without knowing or caring about | ||
689 | * the network link ... which is unavailable to this code | ||
690 | * until we're activated via set_alt(). | ||
691 | */ | ||
692 | |||
693 | DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n", | ||
694 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
695 | rndis->port.in_ep->name, rndis->port.out_ep->name, | ||
696 | rndis->notify->name); | ||
697 | return 0; | ||
698 | |||
699 | fail: | ||
700 | if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors) | ||
701 | usb_free_descriptors(f->hs_descriptors); | ||
702 | if (f->descriptors) | ||
703 | usb_free_descriptors(f->descriptors); | ||
704 | |||
705 | if (rndis->notify_req) { | ||
706 | kfree(rndis->notify_req->buf); | ||
707 | usb_ep_free_request(rndis->notify, rndis->notify_req); | ||
708 | } | ||
709 | |||
710 | /* we might as well release our claims on endpoints */ | ||
711 | if (rndis->notify) | ||
712 | rndis->notify->driver_data = NULL; | ||
713 | if (rndis->port.out) | ||
714 | rndis->port.out_ep->driver_data = NULL; | ||
715 | if (rndis->port.in) | ||
716 | rndis->port.in_ep->driver_data = NULL; | ||
717 | |||
718 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | ||
719 | |||
720 | return status; | ||
721 | } | ||
722 | |||
723 | static void | ||
724 | rndis_unbind(struct usb_configuration *c, struct usb_function *f) | ||
725 | { | ||
726 | struct f_rndis *rndis = func_to_rndis(f); | ||
727 | |||
728 | rndis_deregister(rndis->config); | ||
729 | rndis_exit(); | ||
730 | |||
731 | if (gadget_is_dualspeed(c->cdev->gadget)) | ||
732 | usb_free_descriptors(f->hs_descriptors); | ||
733 | usb_free_descriptors(f->descriptors); | ||
734 | |||
735 | kfree(rndis->notify_req->buf); | ||
736 | usb_ep_free_request(rndis->notify, rndis->notify_req); | ||
737 | |||
738 | kfree(rndis); | ||
739 | } | ||
740 | |||
741 | /* Some controllers can't support RNDIS ... */ | ||
742 | static inline bool can_support_rndis(struct usb_configuration *c) | ||
743 | { | ||
744 | /* only two endpoints on sa1100 */ | ||
745 | if (gadget_is_sa1100(c->cdev->gadget)) | ||
746 | return false; | ||
747 | |||
748 | /* everything else is *presumably* fine */ | ||
749 | return true; | ||
750 | } | ||
751 | |||
752 | /** | ||
753 | * rndis_bind_config - add RNDIS network link to a configuration | ||
754 | * @c: the configuration to support the network link | ||
755 | * @ethaddr: a buffer in which the ethernet address of the host side | ||
756 | * side of the link was recorded | ||
757 | * Context: single threaded during gadget setup | ||
758 | * | ||
759 | * Returns zero on success, else negative errno. | ||
760 | * | ||
761 | * Caller must have called @gether_setup(). Caller is also responsible | ||
762 | * for calling @gether_cleanup() before module unload. | ||
763 | */ | ||
764 | int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | ||
765 | { | ||
766 | struct f_rndis *rndis; | ||
767 | int status; | ||
768 | |||
769 | if (!can_support_rndis(c) || !ethaddr) | ||
770 | return -EINVAL; | ||
771 | |||
772 | /* maybe allocate device-global string IDs */ | ||
773 | if (rndis_string_defs[0].id == 0) { | ||
774 | |||
775 | /* ... and setup RNDIS itself */ | ||
776 | status = rndis_init(); | ||
777 | if (status < 0) | ||
778 | return status; | ||
779 | |||
780 | /* control interface label */ | ||
781 | status = usb_string_id(c->cdev); | ||
782 | if (status < 0) | ||
783 | return status; | ||
784 | rndis_string_defs[0].id = status; | ||
785 | rndis_control_intf.iInterface = status; | ||
786 | |||
787 | /* data interface label */ | ||
788 | status = usb_string_id(c->cdev); | ||
789 | if (status < 0) | ||
790 | return status; | ||
791 | rndis_string_defs[1].id = status; | ||
792 | rndis_data_intf.iInterface = status; | ||
793 | } | ||
794 | |||
795 | /* allocate and initialize one new instance */ | ||
796 | status = -ENOMEM; | ||
797 | rndis = kzalloc(sizeof *rndis, GFP_KERNEL); | ||
798 | if (!rndis) | ||
799 | goto fail; | ||
800 | |||
801 | memcpy(rndis->ethaddr, ethaddr, ETH_ALEN); | ||
802 | |||
803 | /* RNDIS activates when the host changes this filter */ | ||
804 | rndis->port.cdc_filter = 0; | ||
805 | |||
806 | /* RNDIS has special (and complex) framing */ | ||
807 | rndis->port.header_len = sizeof(struct rndis_packet_msg_type); | ||
808 | rndis->port.wrap = rndis_add_header; | ||
809 | rndis->port.unwrap = rndis_rm_hdr; | ||
810 | |||
811 | rndis->port.func.name = "rndis"; | ||
812 | rndis->port.func.strings = rndis_strings; | ||
813 | /* descriptors are per-instance copies */ | ||
814 | rndis->port.func.bind = rndis_bind; | ||
815 | rndis->port.func.unbind = rndis_unbind; | ||
816 | rndis->port.func.set_alt = rndis_set_alt; | ||
817 | rndis->port.func.setup = rndis_setup; | ||
818 | rndis->port.func.disable = rndis_disable; | ||
819 | |||
820 | status = usb_add_function(c, &rndis->port.func); | ||
821 | if (status) { | ||
822 | kfree(rndis); | ||
823 | fail: | ||
824 | rndis_exit(); | ||
825 | } | ||
826 | return status; | ||
827 | } | ||
diff --git a/drivers/usb/gadget/f_serial.c b/drivers/usb/gadget/f_serial.c new file mode 100644 index 000000000000..1b6bde9aaed5 --- /dev/null +++ b/drivers/usb/gadget/f_serial.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* | ||
2 | * f_serial.c - generic USB serial function driver | ||
3 | * | ||
4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) | ||
5 | * Copyright (C) 2008 by David Brownell | ||
6 | * Copyright (C) 2008 by Nokia Corporation | ||
7 | * | ||
8 | * This software is distributed under the terms of the GNU General | ||
9 | * Public License ("GPL") as published by the Free Software Foundation, | ||
10 | * either version 2 of that License or (at your option) any later version. | ||
11 | */ | ||
12 | |||
13 | #include <linux/kernel.h> | ||
14 | #include <linux/device.h> | ||
15 | |||
16 | #include "u_serial.h" | ||
17 | #include "gadget_chips.h" | ||
18 | |||
19 | |||
20 | /* | ||
21 | * This function packages a simple "generic serial" port with no real | ||
22 | * control mechanisms, just raw data transfer over two bulk endpoints. | ||
23 | * | ||
24 | * Because it's not standardized, this isn't as interoperable as the | ||
25 | * CDC ACM driver. However, for many purposes it's just as functional | ||
26 | * if you can arrange appropriate host side drivers. | ||
27 | */ | ||
28 | |||
29 | struct gser_descs { | ||
30 | struct usb_endpoint_descriptor *in; | ||
31 | struct usb_endpoint_descriptor *out; | ||
32 | }; | ||
33 | |||
34 | struct f_gser { | ||
35 | struct gserial port; | ||
36 | u8 data_id; | ||
37 | u8 port_num; | ||
38 | |||
39 | struct usb_descriptor_header **fs_function; | ||
40 | struct gser_descs fs; | ||
41 | struct usb_descriptor_header **hs_function; | ||
42 | struct gser_descs hs; | ||
43 | }; | ||
44 | |||
45 | static inline struct f_gser *func_to_gser(struct usb_function *f) | ||
46 | { | ||
47 | return container_of(f, struct f_gser, port.func); | ||
48 | } | ||
49 | |||
50 | /*-------------------------------------------------------------------------*/ | ||
51 | |||
52 | /* interface descriptor: */ | ||
53 | |||
54 | static struct usb_interface_descriptor gser_interface_desc __initdata = { | ||
55 | .bLength = USB_DT_INTERFACE_SIZE, | ||
56 | .bDescriptorType = USB_DT_INTERFACE, | ||
57 | /* .bInterfaceNumber = DYNAMIC */ | ||
58 | .bNumEndpoints = 2, | ||
59 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
60 | .bInterfaceSubClass = 0, | ||
61 | .bInterfaceProtocol = 0, | ||
62 | /* .iInterface = DYNAMIC */ | ||
63 | }; | ||
64 | |||
65 | /* full speed support: */ | ||
66 | |||
67 | static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = { | ||
68 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
69 | .bDescriptorType = USB_DT_ENDPOINT, | ||
70 | .bEndpointAddress = USB_DIR_IN, | ||
71 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
72 | }; | ||
73 | |||
74 | static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = { | ||
75 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
76 | .bDescriptorType = USB_DT_ENDPOINT, | ||
77 | .bEndpointAddress = USB_DIR_OUT, | ||
78 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
79 | }; | ||
80 | |||
81 | static struct usb_descriptor_header *gser_fs_function[] __initdata = { | ||
82 | (struct usb_descriptor_header *) &gser_interface_desc, | ||
83 | (struct usb_descriptor_header *) &gser_fs_in_desc, | ||
84 | (struct usb_descriptor_header *) &gser_fs_out_desc, | ||
85 | NULL, | ||
86 | }; | ||
87 | |||
88 | /* high speed support: */ | ||
89 | |||
90 | static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = { | ||
91 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
92 | .bDescriptorType = USB_DT_ENDPOINT, | ||
93 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
94 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
95 | }; | ||
96 | |||
97 | static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = { | ||
98 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
99 | .bDescriptorType = USB_DT_ENDPOINT, | ||
100 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
101 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
102 | }; | ||
103 | |||
104 | static struct usb_descriptor_header *gser_hs_function[] __initdata = { | ||
105 | (struct usb_descriptor_header *) &gser_interface_desc, | ||
106 | (struct usb_descriptor_header *) &gser_hs_in_desc, | ||
107 | (struct usb_descriptor_header *) &gser_hs_out_desc, | ||
108 | NULL, | ||
109 | }; | ||
110 | |||
111 | /* string descriptors: */ | ||
112 | |||
113 | static struct usb_string gser_string_defs[] = { | ||
114 | [0].s = "Generic Serial", | ||
115 | { } /* end of list */ | ||
116 | }; | ||
117 | |||
118 | static struct usb_gadget_strings gser_string_table = { | ||
119 | .language = 0x0409, /* en-us */ | ||
120 | .strings = gser_string_defs, | ||
121 | }; | ||
122 | |||
123 | static struct usb_gadget_strings *gser_strings[] = { | ||
124 | &gser_string_table, | ||
125 | NULL, | ||
126 | }; | ||
127 | |||
128 | /*-------------------------------------------------------------------------*/ | ||
129 | |||
130 | static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | ||
131 | { | ||
132 | struct f_gser *gser = func_to_gser(f); | ||
133 | struct usb_composite_dev *cdev = f->config->cdev; | ||
134 | |||
135 | /* we know alt == 0, so this is an activation or a reset */ | ||
136 | |||
137 | if (gser->port.in->driver_data) { | ||
138 | DBG(cdev, "reset generic ttyGS%d\n", gser->port_num); | ||
139 | gserial_disconnect(&gser->port); | ||
140 | } else { | ||
141 | DBG(cdev, "activate generic ttyGS%d\n", gser->port_num); | ||
142 | gser->port.in_desc = ep_choose(cdev->gadget, | ||
143 | gser->hs.in, gser->fs.in); | ||
144 | gser->port.out_desc = ep_choose(cdev->gadget, | ||
145 | gser->hs.out, gser->fs.out); | ||
146 | } | ||
147 | gserial_connect(&gser->port, gser->port_num); | ||
148 | return 0; | ||
149 | } | ||
150 | |||
151 | static void gser_disable(struct usb_function *f) | ||
152 | { | ||
153 | struct f_gser *gser = func_to_gser(f); | ||
154 | struct usb_composite_dev *cdev = f->config->cdev; | ||
155 | |||
156 | DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num); | ||
157 | gserial_disconnect(&gser->port); | ||
158 | } | ||
159 | |||
160 | /*-------------------------------------------------------------------------*/ | ||
161 | |||
162 | /* serial function driver setup/binding */ | ||
163 | |||
164 | static int __init | ||
165 | gser_bind(struct usb_configuration *c, struct usb_function *f) | ||
166 | { | ||
167 | struct usb_composite_dev *cdev = c->cdev; | ||
168 | struct f_gser *gser = func_to_gser(f); | ||
169 | int status; | ||
170 | struct usb_ep *ep; | ||
171 | |||
172 | /* allocate instance-specific interface IDs */ | ||
173 | status = usb_interface_id(c, f); | ||
174 | if (status < 0) | ||
175 | goto fail; | ||
176 | gser->data_id = status; | ||
177 | gser_interface_desc.bInterfaceNumber = status; | ||
178 | |||
179 | status = -ENODEV; | ||
180 | |||
181 | /* allocate instance-specific endpoints */ | ||
182 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc); | ||
183 | if (!ep) | ||
184 | goto fail; | ||
185 | gser->port.in = ep; | ||
186 | ep->driver_data = cdev; /* claim */ | ||
187 | |||
188 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc); | ||
189 | if (!ep) | ||
190 | goto fail; | ||
191 | gser->port.out = ep; | ||
192 | ep->driver_data = cdev; /* claim */ | ||
193 | |||
194 | /* copy descriptors, and track endpoint copies */ | ||
195 | f->descriptors = usb_copy_descriptors(gser_fs_function); | ||
196 | |||
197 | gser->fs.in = usb_find_endpoint(gser_fs_function, | ||
198 | f->descriptors, &gser_fs_in_desc); | ||
199 | gser->fs.out = usb_find_endpoint(gser_fs_function, | ||
200 | f->descriptors, &gser_fs_out_desc); | ||
201 | |||
202 | |||
203 | /* support all relevant hardware speeds... we expect that when | ||
204 | * hardware is dual speed, all bulk-capable endpoints work at | ||
205 | * both speeds | ||
206 | */ | ||
207 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
208 | gser_hs_in_desc.bEndpointAddress = | ||
209 | gser_fs_in_desc.bEndpointAddress; | ||
210 | gser_hs_out_desc.bEndpointAddress = | ||
211 | gser_fs_out_desc.bEndpointAddress; | ||
212 | |||
213 | /* copy descriptors, and track endpoint copies */ | ||
214 | f->hs_descriptors = usb_copy_descriptors(gser_hs_function); | ||
215 | |||
216 | gser->hs.in = usb_find_endpoint(gser_hs_function, | ||
217 | f->hs_descriptors, &gser_hs_in_desc); | ||
218 | gser->hs.out = usb_find_endpoint(gser_hs_function, | ||
219 | f->hs_descriptors, &gser_hs_out_desc); | ||
220 | } | ||
221 | |||
222 | DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n", | ||
223 | gser->port_num, | ||
224 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
225 | gser->port.in->name, gser->port.out->name); | ||
226 | return 0; | ||
227 | |||
228 | fail: | ||
229 | /* we might as well release our claims on endpoints */ | ||
230 | if (gser->port.out) | ||
231 | gser->port.out->driver_data = NULL; | ||
232 | if (gser->port.in) | ||
233 | gser->port.in->driver_data = NULL; | ||
234 | |||
235 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | ||
236 | |||
237 | return status; | ||
238 | } | ||
239 | |||
240 | static void | ||
241 | gser_unbind(struct usb_configuration *c, struct usb_function *f) | ||
242 | { | ||
243 | if (gadget_is_dualspeed(c->cdev->gadget)) | ||
244 | usb_free_descriptors(f->hs_descriptors); | ||
245 | usb_free_descriptors(f->descriptors); | ||
246 | kfree(func_to_gser(f)); | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * gser_bind_config - add a generic serial function to a configuration | ||
251 | * @c: the configuration to support the serial instance | ||
252 | * @port_num: /dev/ttyGS* port this interface will use | ||
253 | * Context: single threaded during gadget setup | ||
254 | * | ||
255 | * Returns zero on success, else negative errno. | ||
256 | * | ||
257 | * Caller must have called @gserial_setup() with enough ports to | ||
258 | * handle all the ones it binds. Caller is also responsible | ||
259 | * for calling @gserial_cleanup() before module unload. | ||
260 | */ | ||
261 | int __init gser_bind_config(struct usb_configuration *c, u8 port_num) | ||
262 | { | ||
263 | struct f_gser *gser; | ||
264 | int status; | ||
265 | |||
266 | /* REVISIT might want instance-specific strings to help | ||
267 | * distinguish instances ... | ||
268 | */ | ||
269 | |||
270 | /* maybe allocate device-global string ID */ | ||
271 | if (gser_string_defs[0].id == 0) { | ||
272 | status = usb_string_id(c->cdev); | ||
273 | if (status < 0) | ||
274 | return status; | ||
275 | gser_string_defs[0].id = status; | ||
276 | } | ||
277 | |||
278 | /* allocate and initialize one new instance */ | ||
279 | gser = kzalloc(sizeof *gser, GFP_KERNEL); | ||
280 | if (!gser) | ||
281 | return -ENOMEM; | ||
282 | |||
283 | gser->port_num = port_num; | ||
284 | |||
285 | gser->port.func.name = "gser"; | ||
286 | gser->port.func.strings = gser_strings; | ||
287 | gser->port.func.bind = gser_bind; | ||
288 | gser->port.func.unbind = gser_unbind; | ||
289 | gser->port.func.set_alt = gser_set_alt; | ||
290 | gser->port.func.disable = gser_disable; | ||
291 | |||
292 | status = usb_add_function(c, &gser->port.func); | ||
293 | if (status) | ||
294 | kfree(gser); | ||
295 | return status; | ||
296 | } | ||
diff --git a/drivers/usb/gadget/f_sourcesink.c b/drivers/usb/gadget/f_sourcesink.c new file mode 100644 index 000000000000..f18c3a14d72a --- /dev/null +++ b/drivers/usb/gadget/f_sourcesink.c | |||
@@ -0,0 +1,587 @@ | |||
1 | /* | ||
2 | * f_sourcesink.c - USB peripheral source/sink configuration driver | ||
3 | * | ||
4 | * Copyright (C) 2003-2008 David Brownell | ||
5 | * Copyright (C) 2008 by Nokia Corporation | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | /* #define VERBOSE_DEBUG */ | ||
23 | |||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/utsname.h> | ||
26 | #include <linux/device.h> | ||
27 | |||
28 | #include "g_zero.h" | ||
29 | #include "gadget_chips.h" | ||
30 | |||
31 | |||
32 | /* | ||
33 | * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral | ||
34 | * controller drivers. | ||
35 | * | ||
36 | * This just sinks bulk packets OUT to the peripheral and sources them IN | ||
37 | * to the host, optionally with specific data patterns for integrity tests. | ||
38 | * As such it supports basic functionality and load tests. | ||
39 | * | ||
40 | * In terms of control messaging, this supports all the standard requests | ||
41 | * plus two that support control-OUT tests. If the optional "autoresume" | ||
42 | * mode is enabled, it provides good functional coverage for the "USBCV" | ||
43 | * test harness from USB-IF. | ||
44 | * | ||
45 | * Note that because this doesn't queue more than one request at a time, | ||
46 | * some other function must be used to test queueing logic. The network | ||
47 | * link (g_ether) is the best overall option for that, since its TX and RX | ||
48 | * queues are relatively independent, will receive a range of packet sizes, | ||
49 | * and can often be made to run out completely. Those issues are important | ||
50 | * when stress testing peripheral controller drivers. | ||
51 | * | ||
52 | * | ||
53 | * This is currently packaged as a configuration driver, which can't be | ||
54 | * combined with other functions to make composite devices. However, it | ||
55 | * can be combined with other independent configurations. | ||
56 | */ | ||
57 | struct f_sourcesink { | ||
58 | struct usb_function function; | ||
59 | |||
60 | struct usb_ep *in_ep; | ||
61 | struct usb_ep *out_ep; | ||
62 | struct timer_list resume; | ||
63 | }; | ||
64 | |||
65 | static inline struct f_sourcesink *func_to_ss(struct usb_function *f) | ||
66 | { | ||
67 | return container_of(f, struct f_sourcesink, function); | ||
68 | } | ||
69 | |||
70 | static unsigned autoresume; | ||
71 | module_param(autoresume, uint, 0); | ||
72 | MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup"); | ||
73 | |||
74 | static unsigned pattern; | ||
75 | module_param(pattern, uint, 0); | ||
76 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 "); | ||
77 | |||
78 | /*-------------------------------------------------------------------------*/ | ||
79 | |||
80 | static struct usb_interface_descriptor source_sink_intf = { | ||
81 | .bLength = sizeof source_sink_intf, | ||
82 | .bDescriptorType = USB_DT_INTERFACE, | ||
83 | |||
84 | .bNumEndpoints = 2, | ||
85 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
86 | /* .iInterface = DYNAMIC */ | ||
87 | }; | ||
88 | |||
89 | /* full speed support: */ | ||
90 | |||
91 | static struct usb_endpoint_descriptor fs_source_desc = { | ||
92 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
93 | .bDescriptorType = USB_DT_ENDPOINT, | ||
94 | |||
95 | .bEndpointAddress = USB_DIR_IN, | ||
96 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
97 | }; | ||
98 | |||
99 | static struct usb_endpoint_descriptor fs_sink_desc = { | ||
100 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
101 | .bDescriptorType = USB_DT_ENDPOINT, | ||
102 | |||
103 | .bEndpointAddress = USB_DIR_OUT, | ||
104 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
105 | }; | ||
106 | |||
107 | static struct usb_descriptor_header *fs_source_sink_descs[] = { | ||
108 | (struct usb_descriptor_header *) &source_sink_intf, | ||
109 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
110 | (struct usb_descriptor_header *) &fs_source_desc, | ||
111 | NULL, | ||
112 | }; | ||
113 | |||
114 | /* high speed support: */ | ||
115 | |||
116 | static struct usb_endpoint_descriptor hs_source_desc = { | ||
117 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
118 | .bDescriptorType = USB_DT_ENDPOINT, | ||
119 | |||
120 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
121 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
122 | }; | ||
123 | |||
124 | static struct usb_endpoint_descriptor hs_sink_desc = { | ||
125 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
126 | .bDescriptorType = USB_DT_ENDPOINT, | ||
127 | |||
128 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
129 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
130 | }; | ||
131 | |||
132 | static struct usb_descriptor_header *hs_source_sink_descs[] = { | ||
133 | (struct usb_descriptor_header *) &source_sink_intf, | ||
134 | (struct usb_descriptor_header *) &hs_source_desc, | ||
135 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
136 | NULL, | ||
137 | }; | ||
138 | |||
139 | /* function-specific strings: */ | ||
140 | |||
141 | static struct usb_string strings_sourcesink[] = { | ||
142 | [0].s = "source and sink data", | ||
143 | { } /* end of list */ | ||
144 | }; | ||
145 | |||
146 | static struct usb_gadget_strings stringtab_sourcesink = { | ||
147 | .language = 0x0409, /* en-us */ | ||
148 | .strings = strings_sourcesink, | ||
149 | }; | ||
150 | |||
151 | static struct usb_gadget_strings *sourcesink_strings[] = { | ||
152 | &stringtab_sourcesink, | ||
153 | NULL, | ||
154 | }; | ||
155 | |||
156 | /*-------------------------------------------------------------------------*/ | ||
157 | |||
158 | static void sourcesink_autoresume(unsigned long _c) | ||
159 | { | ||
160 | struct usb_composite_dev *cdev = (void *)_c; | ||
161 | struct usb_gadget *g = cdev->gadget; | ||
162 | |||
163 | /* Normally the host would be woken up for something | ||
164 | * more significant than just a timer firing; likely | ||
165 | * because of some direct user request. | ||
166 | */ | ||
167 | if (g->speed != USB_SPEED_UNKNOWN) { | ||
168 | int status = usb_gadget_wakeup(g); | ||
169 | DBG(cdev, "%s --> %d\n", __func__, status); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | static int __init | ||
174 | sourcesink_bind(struct usb_configuration *c, struct usb_function *f) | ||
175 | { | ||
176 | struct usb_composite_dev *cdev = c->cdev; | ||
177 | struct f_sourcesink *ss = func_to_ss(f); | ||
178 | int id; | ||
179 | |||
180 | /* allocate interface ID(s) */ | ||
181 | id = usb_interface_id(c, f); | ||
182 | if (id < 0) | ||
183 | return id; | ||
184 | source_sink_intf.bInterfaceNumber = id; | ||
185 | |||
186 | /* allocate endpoints */ | ||
187 | ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); | ||
188 | if (!ss->in_ep) { | ||
189 | autoconf_fail: | ||
190 | ERROR(cdev, "%s: can't autoconfigure on %s\n", | ||
191 | f->name, cdev->gadget->name); | ||
192 | return -ENODEV; | ||
193 | } | ||
194 | ss->in_ep->driver_data = cdev; /* claim */ | ||
195 | |||
196 | ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); | ||
197 | if (!ss->out_ep) | ||
198 | goto autoconf_fail; | ||
199 | ss->out_ep->driver_data = cdev; /* claim */ | ||
200 | |||
201 | setup_timer(&ss->resume, sourcesink_autoresume, | ||
202 | (unsigned long) c->cdev); | ||
203 | |||
204 | /* support high speed hardware */ | ||
205 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
206 | hs_source_desc.bEndpointAddress = | ||
207 | fs_source_desc.bEndpointAddress; | ||
208 | hs_sink_desc.bEndpointAddress = | ||
209 | fs_sink_desc.bEndpointAddress; | ||
210 | f->hs_descriptors = hs_source_sink_descs; | ||
211 | } | ||
212 | |||
213 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", | ||
214 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
215 | f->name, ss->in_ep->name, ss->out_ep->name); | ||
216 | return 0; | ||
217 | } | ||
218 | |||
219 | static void | ||
220 | sourcesink_unbind(struct usb_configuration *c, struct usb_function *f) | ||
221 | { | ||
222 | kfree(func_to_ss(f)); | ||
223 | } | ||
224 | |||
225 | /* optionally require specific source/sink data patterns */ | ||
226 | static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) | ||
227 | { | ||
228 | unsigned i; | ||
229 | u8 *buf = req->buf; | ||
230 | struct usb_composite_dev *cdev = ss->function.config->cdev; | ||
231 | |||
232 | for (i = 0; i < req->actual; i++, buf++) { | ||
233 | switch (pattern) { | ||
234 | |||
235 | /* all-zeroes has no synchronization issues */ | ||
236 | case 0: | ||
237 | if (*buf == 0) | ||
238 | continue; | ||
239 | break; | ||
240 | |||
241 | /* "mod63" stays in sync with short-terminated transfers, | ||
242 | * OR otherwise when host and gadget agree on how large | ||
243 | * each usb transfer request should be. Resync is done | ||
244 | * with set_interface or set_config. (We *WANT* it to | ||
245 | * get quickly out of sync if controllers or their drivers | ||
246 | * stutter for any reason, including buffer duplcation...) | ||
247 | */ | ||
248 | case 1: | ||
249 | if (*buf == (u8)(i % 63)) | ||
250 | continue; | ||
251 | break; | ||
252 | } | ||
253 | ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); | ||
254 | usb_ep_set_halt(ss->out_ep); | ||
255 | return -EINVAL; | ||
256 | } | ||
257 | return 0; | ||
258 | } | ||
259 | |||
260 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) | ||
261 | { | ||
262 | unsigned i; | ||
263 | u8 *buf = req->buf; | ||
264 | |||
265 | switch (pattern) { | ||
266 | case 0: | ||
267 | memset(req->buf, 0, req->length); | ||
268 | break; | ||
269 | case 1: | ||
270 | for (i = 0; i < req->length; i++) | ||
271 | *buf++ = (u8) (i % 63); | ||
272 | break; | ||
273 | } | ||
274 | } | ||
275 | |||
276 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) | ||
277 | { | ||
278 | struct f_sourcesink *ss = ep->driver_data; | ||
279 | struct usb_composite_dev *cdev = ss->function.config->cdev; | ||
280 | int status = req->status; | ||
281 | |||
282 | switch (status) { | ||
283 | |||
284 | case 0: /* normal completion? */ | ||
285 | if (ep == ss->out_ep) { | ||
286 | check_read_data(ss, req); | ||
287 | memset(req->buf, 0x55, req->length); | ||
288 | } else | ||
289 | reinit_write_data(ep, req); | ||
290 | break; | ||
291 | |||
292 | /* this endpoint is normally active while we're configured */ | ||
293 | case -ECONNABORTED: /* hardware forced ep reset */ | ||
294 | case -ECONNRESET: /* request dequeued */ | ||
295 | case -ESHUTDOWN: /* disconnect from host */ | ||
296 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, | ||
297 | req->actual, req->length); | ||
298 | if (ep == ss->out_ep) | ||
299 | check_read_data(ss, req); | ||
300 | free_ep_req(ep, req); | ||
301 | return; | ||
302 | |||
303 | case -EOVERFLOW: /* buffer overrun on read means that | ||
304 | * we didn't provide a big enough | ||
305 | * buffer. | ||
306 | */ | ||
307 | default: | ||
308 | #if 1 | ||
309 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, | ||
310 | status, req->actual, req->length); | ||
311 | #endif | ||
312 | case -EREMOTEIO: /* short read */ | ||
313 | break; | ||
314 | } | ||
315 | |||
316 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
317 | if (status) { | ||
318 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", | ||
319 | ep->name, req->length, status); | ||
320 | usb_ep_set_halt(ep); | ||
321 | /* FIXME recover later ... somehow */ | ||
322 | } | ||
323 | } | ||
324 | |||
325 | static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in) | ||
326 | { | ||
327 | struct usb_ep *ep; | ||
328 | struct usb_request *req; | ||
329 | int status; | ||
330 | |||
331 | ep = is_in ? ss->in_ep : ss->out_ep; | ||
332 | req = alloc_ep_req(ep); | ||
333 | if (!req) | ||
334 | return -ENOMEM; | ||
335 | |||
336 | req->complete = source_sink_complete; | ||
337 | if (is_in) | ||
338 | reinit_write_data(ep, req); | ||
339 | else | ||
340 | memset(req->buf, 0x55, req->length); | ||
341 | |||
342 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
343 | if (status) { | ||
344 | struct usb_composite_dev *cdev; | ||
345 | |||
346 | cdev = ss->function.config->cdev; | ||
347 | ERROR(cdev, "start %s %s --> %d\n", | ||
348 | is_in ? "IN" : "OUT", | ||
349 | ep->name, status); | ||
350 | free_ep_req(ep, req); | ||
351 | } | ||
352 | |||
353 | return status; | ||
354 | } | ||
355 | |||
356 | static void disable_source_sink(struct f_sourcesink *ss) | ||
357 | { | ||
358 | struct usb_composite_dev *cdev; | ||
359 | |||
360 | cdev = ss->function.config->cdev; | ||
361 | disable_endpoints(cdev, ss->in_ep, ss->out_ep); | ||
362 | del_timer(&ss->resume); | ||
363 | VDBG(cdev, "%s disabled\n", ss->function.name); | ||
364 | } | ||
365 | |||
366 | static int | ||
367 | enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss) | ||
368 | { | ||
369 | int result = 0; | ||
370 | const struct usb_endpoint_descriptor *src, *sink; | ||
371 | struct usb_ep *ep; | ||
372 | |||
373 | src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc); | ||
374 | sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc); | ||
375 | |||
376 | /* one endpoint writes (sources) zeroes IN (to the host) */ | ||
377 | ep = ss->in_ep; | ||
378 | result = usb_ep_enable(ep, src); | ||
379 | if (result < 0) | ||
380 | return result; | ||
381 | ep->driver_data = ss; | ||
382 | |||
383 | result = source_sink_start_ep(ss, true); | ||
384 | if (result < 0) { | ||
385 | fail: | ||
386 | ep = ss->in_ep; | ||
387 | usb_ep_disable(ep); | ||
388 | ep->driver_data = NULL; | ||
389 | return result; | ||
390 | } | ||
391 | |||
392 | /* one endpoint reads (sinks) anything OUT (from the host) */ | ||
393 | ep = ss->out_ep; | ||
394 | result = usb_ep_enable(ep, sink); | ||
395 | if (result < 0) | ||
396 | goto fail; | ||
397 | ep->driver_data = ss; | ||
398 | |||
399 | result = source_sink_start_ep(ss, false); | ||
400 | if (result < 0) { | ||
401 | usb_ep_disable(ep); | ||
402 | ep->driver_data = NULL; | ||
403 | goto fail; | ||
404 | } | ||
405 | |||
406 | DBG(cdev, "%s enabled\n", ss->function.name); | ||
407 | return result; | ||
408 | } | ||
409 | |||
410 | static int sourcesink_set_alt(struct usb_function *f, | ||
411 | unsigned intf, unsigned alt) | ||
412 | { | ||
413 | struct f_sourcesink *ss = func_to_ss(f); | ||
414 | struct usb_composite_dev *cdev = f->config->cdev; | ||
415 | |||
416 | /* we know alt is zero */ | ||
417 | if (ss->in_ep->driver_data) | ||
418 | disable_source_sink(ss); | ||
419 | return enable_source_sink(cdev, ss); | ||
420 | } | ||
421 | |||
422 | static void sourcesink_disable(struct usb_function *f) | ||
423 | { | ||
424 | struct f_sourcesink *ss = func_to_ss(f); | ||
425 | |||
426 | disable_source_sink(ss); | ||
427 | } | ||
428 | |||
429 | static void sourcesink_suspend(struct usb_function *f) | ||
430 | { | ||
431 | struct f_sourcesink *ss = func_to_ss(f); | ||
432 | struct usb_composite_dev *cdev = f->config->cdev; | ||
433 | |||
434 | if (cdev->gadget->speed == USB_SPEED_UNKNOWN) | ||
435 | return; | ||
436 | |||
437 | if (autoresume) { | ||
438 | mod_timer(&ss->resume, jiffies + (HZ * autoresume)); | ||
439 | DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume); | ||
440 | } else | ||
441 | DBG(cdev, "%s\n", __func__); | ||
442 | } | ||
443 | |||
444 | static void sourcesink_resume(struct usb_function *f) | ||
445 | { | ||
446 | struct f_sourcesink *ss = func_to_ss(f); | ||
447 | struct usb_composite_dev *cdev = f->config->cdev; | ||
448 | |||
449 | DBG(cdev, "%s\n", __func__); | ||
450 | del_timer(&ss->resume); | ||
451 | } | ||
452 | |||
453 | /*-------------------------------------------------------------------------*/ | ||
454 | |||
455 | static int __init sourcesink_bind_config(struct usb_configuration *c) | ||
456 | { | ||
457 | struct f_sourcesink *ss; | ||
458 | int status; | ||
459 | |||
460 | ss = kzalloc(sizeof *ss, GFP_KERNEL); | ||
461 | if (!ss) | ||
462 | return -ENOMEM; | ||
463 | |||
464 | ss->function.name = "source/sink"; | ||
465 | ss->function.descriptors = fs_source_sink_descs; | ||
466 | ss->function.bind = sourcesink_bind; | ||
467 | ss->function.unbind = sourcesink_unbind; | ||
468 | ss->function.set_alt = sourcesink_set_alt; | ||
469 | ss->function.disable = sourcesink_disable; | ||
470 | ss->function.suspend = sourcesink_suspend; | ||
471 | ss->function.resume = sourcesink_resume; | ||
472 | |||
473 | status = usb_add_function(c, &ss->function); | ||
474 | if (status) | ||
475 | kfree(ss); | ||
476 | return status; | ||
477 | } | ||
478 | |||
479 | static int sourcesink_setup(struct usb_configuration *c, | ||
480 | const struct usb_ctrlrequest *ctrl) | ||
481 | { | ||
482 | struct usb_request *req = c->cdev->req; | ||
483 | int value = -EOPNOTSUPP; | ||
484 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
485 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
486 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
487 | |||
488 | /* composite driver infrastructure handles everything except | ||
489 | * the two control test requests. | ||
490 | */ | ||
491 | switch (ctrl->bRequest) { | ||
492 | |||
493 | /* | ||
494 | * These are the same vendor-specific requests supported by | ||
495 | * Intel's USB 2.0 compliance test devices. We exceed that | ||
496 | * device spec by allowing multiple-packet requests. | ||
497 | * | ||
498 | * NOTE: the Control-OUT data stays in req->buf ... better | ||
499 | * would be copying it into a scratch buffer, so that other | ||
500 | * requests may safely intervene. | ||
501 | */ | ||
502 | case 0x5b: /* control WRITE test -- fill the buffer */ | ||
503 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) | ||
504 | goto unknown; | ||
505 | if (w_value || w_index) | ||
506 | break; | ||
507 | /* just read that many bytes into the buffer */ | ||
508 | if (w_length > req->length) | ||
509 | break; | ||
510 | value = w_length; | ||
511 | break; | ||
512 | case 0x5c: /* control READ test -- return the buffer */ | ||
513 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) | ||
514 | goto unknown; | ||
515 | if (w_value || w_index) | ||
516 | break; | ||
517 | /* expect those bytes are still in the buffer; send back */ | ||
518 | if (w_length > req->length) | ||
519 | break; | ||
520 | value = w_length; | ||
521 | break; | ||
522 | |||
523 | default: | ||
524 | unknown: | ||
525 | VDBG(c->cdev, | ||
526 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | ||
527 | ctrl->bRequestType, ctrl->bRequest, | ||
528 | w_value, w_index, w_length); | ||
529 | } | ||
530 | |||
531 | /* respond with data transfer or status phase? */ | ||
532 | if (value >= 0) { | ||
533 | VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", | ||
534 | ctrl->bRequestType, ctrl->bRequest, | ||
535 | w_value, w_index, w_length); | ||
536 | req->zero = 0; | ||
537 | req->length = value; | ||
538 | value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); | ||
539 | if (value < 0) | ||
540 | ERROR(c->cdev, "source/sinkc response, err %d\n", | ||
541 | value); | ||
542 | } | ||
543 | |||
544 | /* device either stalls (value < 0) or reports success */ | ||
545 | return value; | ||
546 | } | ||
547 | |||
548 | static struct usb_configuration sourcesink_driver = { | ||
549 | .label = "source/sink", | ||
550 | .strings = sourcesink_strings, | ||
551 | .bind = sourcesink_bind_config, | ||
552 | .setup = sourcesink_setup, | ||
553 | .bConfigurationValue = 3, | ||
554 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | ||
555 | .bMaxPower = 1, /* 2 mA, minimal */ | ||
556 | /* .iConfiguration = DYNAMIC */ | ||
557 | }; | ||
558 | |||
559 | /** | ||
560 | * sourcesink_add - add a source/sink testing configuration to a device | ||
561 | * @cdev: the device to support the configuration | ||
562 | */ | ||
563 | int __init sourcesink_add(struct usb_composite_dev *cdev) | ||
564 | { | ||
565 | int id; | ||
566 | |||
567 | /* allocate string ID(s) */ | ||
568 | id = usb_string_id(cdev); | ||
569 | if (id < 0) | ||
570 | return id; | ||
571 | strings_sourcesink[0].id = id; | ||
572 | |||
573 | source_sink_intf.iInterface = id; | ||
574 | sourcesink_driver.iConfiguration = id; | ||
575 | |||
576 | /* support autoresume for remote wakeup testing */ | ||
577 | if (autoresume) | ||
578 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
579 | |||
580 | /* support OTG systems */ | ||
581 | if (gadget_is_otg(cdev->gadget)) { | ||
582 | sourcesink_driver.descriptors = otg_desc; | ||
583 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
584 | } | ||
585 | |||
586 | return usb_add_config(cdev, &sourcesink_driver); | ||
587 | } | ||
diff --git a/drivers/usb/gadget/f_subset.c b/drivers/usb/gadget/f_subset.c new file mode 100644 index 000000000000..afeab9a0523f --- /dev/null +++ b/drivers/usb/gadget/f_subset.c | |||
@@ -0,0 +1,423 @@ | |||
1 | /* | ||
2 | * f_subset.c -- "CDC Subset" Ethernet link function driver | ||
3 | * | ||
4 | * Copyright (C) 2003-2005,2008 David Brownell | ||
5 | * Copyright (C) 2008 Nokia Corporation | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | */ | ||
21 | |||
22 | #include <linux/kernel.h> | ||
23 | #include <linux/device.h> | ||
24 | #include <linux/etherdevice.h> | ||
25 | |||
26 | #include "u_ether.h" | ||
27 | |||
28 | |||
29 | /* | ||
30 | * This function packages a simple "CDC Subset" Ethernet port with no real | ||
31 | * control mechanisms; just raw data transfer over two bulk endpoints. | ||
32 | * The data transfer model is exactly that of CDC Ethernet, which is | ||
33 | * why we call it the "CDC Subset". | ||
34 | * | ||
35 | * Because it's not standardized, this has some interoperability issues. | ||
36 | * They mostly relate to driver binding, since the data transfer model is | ||
37 | * so simple (CDC Ethernet). The original versions of this protocol used | ||
38 | * specific product/vendor IDs: byteswapped IDs for Digital Equipment's | ||
39 | * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported | ||
40 | * daughtercards with USB peripheral connectors. (It was used more often | ||
41 | * with other boards, using the Itsy identifiers.) Linux hosts recognized | ||
42 | * this with CONFIG_USB_ARMLINUX; these devices have only one configuration | ||
43 | * and one interface. | ||
44 | * | ||
45 | * At some point, MCCI defined a (nonconformant) CDC MDLM variant called | ||
46 | * "SAFE", which happens to have a mode which is identical to the "CDC | ||
47 | * Subset" in terms of data transfer and lack of control model. This was | ||
48 | * adopted by later Sharp Zaurus models, and by some other software which | ||
49 | * Linux hosts recognize with CONFIG_USB_NET_ZAURUS. | ||
50 | * | ||
51 | * Because Microsoft's RNDIS drivers are far from robust, we added a few | ||
52 | * descriptors to the CDC Subset code, making this code look like a SAFE | ||
53 | * implementation. This lets you use MCCI's host side MS-Windows drivers | ||
54 | * if you get fed up with RNDIS. It also makes it easier for composite | ||
55 | * drivers to work, since they can use class based binding instead of | ||
56 | * caring about specific product and vendor IDs. | ||
57 | */ | ||
58 | |||
59 | struct geth_descs { | ||
60 | struct usb_endpoint_descriptor *in; | ||
61 | struct usb_endpoint_descriptor *out; | ||
62 | }; | ||
63 | |||
64 | struct f_gether { | ||
65 | struct gether port; | ||
66 | |||
67 | char ethaddr[14]; | ||
68 | |||
69 | struct usb_descriptor_header **fs_function; | ||
70 | struct geth_descs fs; | ||
71 | struct usb_descriptor_header **hs_function; | ||
72 | struct geth_descs hs; | ||
73 | }; | ||
74 | |||
75 | static inline struct f_gether *func_to_geth(struct usb_function *f) | ||
76 | { | ||
77 | return container_of(f, struct f_gether, port.func); | ||
78 | } | ||
79 | |||
80 | /*-------------------------------------------------------------------------*/ | ||
81 | |||
82 | /* | ||
83 | * "Simple" CDC-subset option is a simple vendor-neutral model that most | ||
84 | * full speed controllers can handle: one interface, two bulk endpoints. | ||
85 | * To assist host side drivers, we fancy it up a bit, and add descriptors so | ||
86 | * some host side drivers will understand it as a "SAFE" variant. | ||
87 | * | ||
88 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways. | ||
89 | * Data endpoints live in the control interface, there's no data interface. | ||
90 | * And it's not used to talk to a cell phone radio. | ||
91 | */ | ||
92 | |||
93 | /* interface descriptor: */ | ||
94 | |||
95 | static struct usb_interface_descriptor subset_data_intf __initdata = { | ||
96 | .bLength = sizeof subset_data_intf, | ||
97 | .bDescriptorType = USB_DT_INTERFACE, | ||
98 | |||
99 | /* .bInterfaceNumber = DYNAMIC */ | ||
100 | .bAlternateSetting = 0, | ||
101 | .bNumEndpoints = 2, | ||
102 | .bInterfaceClass = USB_CLASS_COMM, | ||
103 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, | ||
104 | .bInterfaceProtocol = 0, | ||
105 | /* .iInterface = DYNAMIC */ | ||
106 | }; | ||
107 | |||
108 | static struct usb_cdc_header_desc header_desc __initdata = { | ||
109 | .bLength = sizeof header_desc, | ||
110 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
111 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
112 | |||
113 | .bcdCDC = __constant_cpu_to_le16(0x0110), | ||
114 | }; | ||
115 | |||
116 | static struct usb_cdc_mdlm_desc mdlm_desc __initdata = { | ||
117 | .bLength = sizeof mdlm_desc, | ||
118 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
119 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, | ||
120 | |||
121 | .bcdVersion = __constant_cpu_to_le16(0x0100), | ||
122 | .bGUID = { | ||
123 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, | ||
124 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, | ||
125 | }, | ||
126 | }; | ||
127 | |||
128 | /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we | ||
129 | * can't really use its struct. All we do here is say that we're using | ||
130 | * the submode of "SAFE" which directly matches the CDC Subset. | ||
131 | */ | ||
132 | static u8 mdlm_detail_desc[] __initdata = { | ||
133 | 6, | ||
134 | USB_DT_CS_INTERFACE, | ||
135 | USB_CDC_MDLM_DETAIL_TYPE, | ||
136 | |||
137 | 0, /* "SAFE" */ | ||
138 | 0, /* network control capabilities (none) */ | ||
139 | 0, /* network data capabilities ("raw" encapsulation) */ | ||
140 | }; | ||
141 | |||
142 | static struct usb_cdc_ether_desc ether_desc __initdata = { | ||
143 | .bLength = sizeof ether_desc, | ||
144 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
145 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, | ||
146 | |||
147 | /* this descriptor actually adds value, surprise! */ | ||
148 | /* .iMACAddress = DYNAMIC */ | ||
149 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ | ||
150 | .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN), | ||
151 | .wNumberMCFilters = __constant_cpu_to_le16(0), | ||
152 | .bNumberPowerFilters = 0, | ||
153 | }; | ||
154 | |||
155 | /* full speed support: */ | ||
156 | |||
157 | static struct usb_endpoint_descriptor fs_in_desc __initdata = { | ||
158 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
159 | .bDescriptorType = USB_DT_ENDPOINT, | ||
160 | |||
161 | .bEndpointAddress = USB_DIR_IN, | ||
162 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
163 | }; | ||
164 | |||
165 | static struct usb_endpoint_descriptor fs_out_desc __initdata = { | ||
166 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
167 | .bDescriptorType = USB_DT_ENDPOINT, | ||
168 | |||
169 | .bEndpointAddress = USB_DIR_OUT, | ||
170 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
171 | }; | ||
172 | |||
173 | static struct usb_descriptor_header *fs_eth_function[] __initdata = { | ||
174 | (struct usb_descriptor_header *) &subset_data_intf, | ||
175 | (struct usb_descriptor_header *) &header_desc, | ||
176 | (struct usb_descriptor_header *) &mdlm_desc, | ||
177 | (struct usb_descriptor_header *) &mdlm_detail_desc, | ||
178 | (struct usb_descriptor_header *) ðer_desc, | ||
179 | (struct usb_descriptor_header *) &fs_in_desc, | ||
180 | (struct usb_descriptor_header *) &fs_out_desc, | ||
181 | NULL, | ||
182 | }; | ||
183 | |||
184 | /* high speed support: */ | ||
185 | |||
186 | static struct usb_endpoint_descriptor hs_in_desc __initdata = { | ||
187 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
188 | .bDescriptorType = USB_DT_ENDPOINT, | ||
189 | |||
190 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
191 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
192 | }; | ||
193 | |||
194 | static struct usb_endpoint_descriptor hs_out_desc __initdata = { | ||
195 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
196 | .bDescriptorType = USB_DT_ENDPOINT, | ||
197 | |||
198 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
199 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
200 | }; | ||
201 | |||
202 | static struct usb_descriptor_header *hs_eth_function[] __initdata = { | ||
203 | (struct usb_descriptor_header *) &subset_data_intf, | ||
204 | (struct usb_descriptor_header *) &header_desc, | ||
205 | (struct usb_descriptor_header *) &mdlm_desc, | ||
206 | (struct usb_descriptor_header *) &mdlm_detail_desc, | ||
207 | (struct usb_descriptor_header *) ðer_desc, | ||
208 | (struct usb_descriptor_header *) &hs_in_desc, | ||
209 | (struct usb_descriptor_header *) &hs_out_desc, | ||
210 | NULL, | ||
211 | }; | ||
212 | |||
213 | /* string descriptors: */ | ||
214 | |||
215 | static struct usb_string geth_string_defs[] = { | ||
216 | [0].s = "CDC Ethernet Subset/SAFE", | ||
217 | [1].s = NULL /* DYNAMIC */, | ||
218 | { } /* end of list */ | ||
219 | }; | ||
220 | |||
221 | static struct usb_gadget_strings geth_string_table = { | ||
222 | .language = 0x0409, /* en-us */ | ||
223 | .strings = geth_string_defs, | ||
224 | }; | ||
225 | |||
226 | static struct usb_gadget_strings *geth_strings[] = { | ||
227 | &geth_string_table, | ||
228 | NULL, | ||
229 | }; | ||
230 | |||
231 | /*-------------------------------------------------------------------------*/ | ||
232 | |||
233 | static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | ||
234 | { | ||
235 | struct f_gether *geth = func_to_geth(f); | ||
236 | struct usb_composite_dev *cdev = f->config->cdev; | ||
237 | struct net_device *net; | ||
238 | |||
239 | /* we know alt == 0, so this is an activation or a reset */ | ||
240 | |||
241 | if (geth->port.in_ep->driver_data) { | ||
242 | DBG(cdev, "reset cdc subset\n"); | ||
243 | gether_disconnect(&geth->port); | ||
244 | } | ||
245 | |||
246 | DBG(cdev, "init + activate cdc subset\n"); | ||
247 | geth->port.in = ep_choose(cdev->gadget, | ||
248 | geth->hs.in, geth->fs.in); | ||
249 | geth->port.out = ep_choose(cdev->gadget, | ||
250 | geth->hs.out, geth->fs.out); | ||
251 | |||
252 | net = gether_connect(&geth->port); | ||
253 | return IS_ERR(net) ? PTR_ERR(net) : 0; | ||
254 | } | ||
255 | |||
256 | static void geth_disable(struct usb_function *f) | ||
257 | { | ||
258 | struct f_gether *geth = func_to_geth(f); | ||
259 | struct usb_composite_dev *cdev = f->config->cdev; | ||
260 | |||
261 | DBG(cdev, "net deactivated\n"); | ||
262 | gether_disconnect(&geth->port); | ||
263 | } | ||
264 | |||
265 | /*-------------------------------------------------------------------------*/ | ||
266 | |||
267 | /* serial function driver setup/binding */ | ||
268 | |||
269 | static int __init | ||
270 | geth_bind(struct usb_configuration *c, struct usb_function *f) | ||
271 | { | ||
272 | struct usb_composite_dev *cdev = c->cdev; | ||
273 | struct f_gether *geth = func_to_geth(f); | ||
274 | int status; | ||
275 | struct usb_ep *ep; | ||
276 | |||
277 | /* allocate instance-specific interface IDs */ | ||
278 | status = usb_interface_id(c, f); | ||
279 | if (status < 0) | ||
280 | goto fail; | ||
281 | subset_data_intf.bInterfaceNumber = status; | ||
282 | |||
283 | status = -ENODEV; | ||
284 | |||
285 | /* allocate instance-specific endpoints */ | ||
286 | ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc); | ||
287 | if (!ep) | ||
288 | goto fail; | ||
289 | geth->port.in_ep = ep; | ||
290 | ep->driver_data = cdev; /* claim */ | ||
291 | |||
292 | ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc); | ||
293 | if (!ep) | ||
294 | goto fail; | ||
295 | geth->port.out_ep = ep; | ||
296 | ep->driver_data = cdev; /* claim */ | ||
297 | |||
298 | /* copy descriptors, and track endpoint copies */ | ||
299 | f->descriptors = usb_copy_descriptors(fs_eth_function); | ||
300 | |||
301 | geth->fs.in = usb_find_endpoint(fs_eth_function, | ||
302 | f->descriptors, &fs_in_desc); | ||
303 | geth->fs.out = usb_find_endpoint(fs_eth_function, | ||
304 | f->descriptors, &fs_out_desc); | ||
305 | |||
306 | |||
307 | /* support all relevant hardware speeds... we expect that when | ||
308 | * hardware is dual speed, all bulk-capable endpoints work at | ||
309 | * both speeds | ||
310 | */ | ||
311 | if (gadget_is_dualspeed(c->cdev->gadget)) { | ||
312 | hs_in_desc.bEndpointAddress = | ||
313 | fs_in_desc.bEndpointAddress; | ||
314 | hs_out_desc.bEndpointAddress = | ||
315 | fs_out_desc.bEndpointAddress; | ||
316 | |||
317 | /* copy descriptors, and track endpoint copies */ | ||
318 | f->hs_descriptors = usb_copy_descriptors(hs_eth_function); | ||
319 | |||
320 | geth->hs.in = usb_find_endpoint(hs_eth_function, | ||
321 | f->hs_descriptors, &hs_in_desc); | ||
322 | geth->hs.out = usb_find_endpoint(hs_eth_function, | ||
323 | f->hs_descriptors, &hs_out_desc); | ||
324 | } | ||
325 | |||
326 | /* NOTE: all that is done without knowing or caring about | ||
327 | * the network link ... which is unavailable to this code | ||
328 | * until we're activated via set_alt(). | ||
329 | */ | ||
330 | |||
331 | DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n", | ||
332 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | ||
333 | geth->port.in_ep->name, geth->port.out_ep->name); | ||
334 | return 0; | ||
335 | |||
336 | fail: | ||
337 | /* we might as well release our claims on endpoints */ | ||
338 | if (geth->port.out) | ||
339 | geth->port.out_ep->driver_data = NULL; | ||
340 | if (geth->port.in) | ||
341 | geth->port.in_ep->driver_data = NULL; | ||
342 | |||
343 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | ||
344 | |||
345 | return status; | ||
346 | } | ||
347 | |||
348 | static void | ||
349 | geth_unbind(struct usb_configuration *c, struct usb_function *f) | ||
350 | { | ||
351 | if (gadget_is_dualspeed(c->cdev->gadget)) | ||
352 | usb_free_descriptors(f->hs_descriptors); | ||
353 | usb_free_descriptors(f->descriptors); | ||
354 | geth_string_defs[1].s = NULL; | ||
355 | kfree(func_to_geth(f)); | ||
356 | } | ||
357 | |||
358 | /** | ||
359 | * geth_bind_config - add CDC Subset network link to a configuration | ||
360 | * @c: the configuration to support the network link | ||
361 | * @ethaddr: a buffer in which the ethernet address of the host side | ||
362 | * side of the link was recorded | ||
363 | * Context: single threaded during gadget setup | ||
364 | * | ||
365 | * Returns zero on success, else negative errno. | ||
366 | * | ||
367 | * Caller must have called @gether_setup(). Caller is also responsible | ||
368 | * for calling @gether_cleanup() before module unload. | ||
369 | */ | ||
370 | int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | ||
371 | { | ||
372 | struct f_gether *geth; | ||
373 | int status; | ||
374 | |||
375 | if (!ethaddr) | ||
376 | return -EINVAL; | ||
377 | |||
378 | /* maybe allocate device-global string IDs */ | ||
379 | if (geth_string_defs[0].id == 0) { | ||
380 | |||
381 | /* interface label */ | ||
382 | status = usb_string_id(c->cdev); | ||
383 | if (status < 0) | ||
384 | return status; | ||
385 | geth_string_defs[0].id = status; | ||
386 | subset_data_intf.iInterface = status; | ||
387 | |||
388 | /* MAC address */ | ||
389 | status = usb_string_id(c->cdev); | ||
390 | if (status < 0) | ||
391 | return status; | ||
392 | geth_string_defs[1].id = status; | ||
393 | ether_desc.iMACAddress = status; | ||
394 | } | ||
395 | |||
396 | /* allocate and initialize one new instance */ | ||
397 | geth = kzalloc(sizeof *geth, GFP_KERNEL); | ||
398 | if (!geth) | ||
399 | return -ENOMEM; | ||
400 | |||
401 | /* export host's Ethernet address in CDC format */ | ||
402 | snprintf(geth->ethaddr, sizeof geth->ethaddr, | ||
403 | "%02X%02X%02X%02X%02X%02X", | ||
404 | ethaddr[0], ethaddr[1], ethaddr[2], | ||
405 | ethaddr[3], ethaddr[4], ethaddr[5]); | ||
406 | geth_string_defs[1].s = geth->ethaddr; | ||
407 | |||
408 | geth->port.cdc_filter = DEFAULT_FILTER; | ||
409 | |||
410 | geth->port.func.name = "cdc_subset"; | ||
411 | geth->port.func.strings = geth_strings; | ||
412 | geth->port.func.bind = geth_bind; | ||
413 | geth->port.func.unbind = geth_unbind; | ||
414 | geth->port.func.set_alt = geth_set_alt; | ||
415 | geth->port.func.disable = geth_disable; | ||
416 | |||
417 | status = usb_add_function(c, &geth->port.func); | ||
418 | if (status) { | ||
419 | geth_string_defs[1].s = NULL; | ||
420 | kfree(geth); | ||
421 | } | ||
422 | return status; | ||
423 | } | ||
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c index 47bb9f09a1aa..15c24edbb61a 100644 --- a/drivers/usb/gadget/file_storage.c +++ b/drivers/usb/gadget/file_storage.c | |||
@@ -3867,8 +3867,8 @@ static int __init fsg_bind(struct usb_gadget *gadget) | |||
3867 | curlun->dev.parent = &gadget->dev; | 3867 | curlun->dev.parent = &gadget->dev; |
3868 | curlun->dev.driver = &fsg_driver.driver; | 3868 | curlun->dev.driver = &fsg_driver.driver; |
3869 | dev_set_drvdata(&curlun->dev, fsg); | 3869 | dev_set_drvdata(&curlun->dev, fsg); |
3870 | snprintf(curlun->dev.bus_id, BUS_ID_SIZE, | 3870 | dev_set_name(&curlun->dev,"%s-lun%d", |
3871 | "%s-lun%d", gadget->dev.bus_id, i); | 3871 | dev_name(&gadget->dev), i); |
3872 | 3872 | ||
3873 | if ((rc = device_register(&curlun->dev)) != 0) { | 3873 | if ((rc = device_register(&curlun->dev)) != 0) { |
3874 | INFO(fsg, "failed to register LUN%d: %d\n", i, rc); | 3874 | INFO(fsg, "failed to register LUN%d: %d\n", i, rc); |
diff --git a/drivers/usb/gadget/fsl_usb2_udc.c b/drivers/usb/gadget/fsl_usb2_udc.c index 18687543d7fa..1695382f30fe 100644 --- a/drivers/usb/gadget/fsl_usb2_udc.c +++ b/drivers/usb/gadget/fsl_usb2_udc.c | |||
@@ -2331,7 +2331,7 @@ static int __init fsl_udc_probe(struct platform_device *pdev) | |||
2331 | udc_controller->gadget.name = driver_name; | 2331 | udc_controller->gadget.name = driver_name; |
2332 | 2332 | ||
2333 | /* Setup gadget.dev and register with kernel */ | 2333 | /* Setup gadget.dev and register with kernel */ |
2334 | strcpy(udc_controller->gadget.dev.bus_id, "gadget"); | 2334 | dev_set_name(&udc_controller->gadget.dev, "gadget"); |
2335 | udc_controller->gadget.dev.release = fsl_udc_release; | 2335 | udc_controller->gadget.dev.release = fsl_udc_release; |
2336 | udc_controller->gadget.dev.parent = &pdev->dev; | 2336 | udc_controller->gadget.dev.parent = &pdev->dev; |
2337 | ret = device_register(&udc_controller->gadget.dev); | 2337 | ret = device_register(&udc_controller->gadget.dev); |
diff --git a/drivers/usb/gadget/g_zero.h b/drivers/usb/gadget/g_zero.h new file mode 100644 index 000000000000..dd2f16ad5a88 --- /dev/null +++ b/drivers/usb/gadget/g_zero.h | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * This header declares the utility functions used by "Gadget Zero", plus | ||
3 | * interfaces to its two single-configuration function drivers. | ||
4 | */ | ||
5 | |||
6 | #ifndef __G_ZERO_H | ||
7 | #define __G_ZERO_H | ||
8 | |||
9 | #include <linux/usb/composite.h> | ||
10 | |||
11 | /* global state */ | ||
12 | extern unsigned buflen; | ||
13 | extern const struct usb_descriptor_header *otg_desc[]; | ||
14 | |||
15 | /* common utilities */ | ||
16 | struct usb_request *alloc_ep_req(struct usb_ep *ep); | ||
17 | void free_ep_req(struct usb_ep *ep, struct usb_request *req); | ||
18 | void disable_endpoints(struct usb_composite_dev *cdev, | ||
19 | struct usb_ep *in, struct usb_ep *out); | ||
20 | |||
21 | /* configuration-specific linkup */ | ||
22 | int sourcesink_add(struct usb_composite_dev *cdev); | ||
23 | int loopback_add(struct usb_composite_dev *cdev); | ||
24 | |||
25 | #endif /* __G_ZERO_H */ | ||
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h index ca5149ea7312..5246e8fef2b2 100644 --- a/drivers/usb/gadget/gadget_chips.h +++ b/drivers/usb/gadget/gadget_chips.h | |||
@@ -214,3 +214,26 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget) | |||
214 | return 0x21; | 214 | return 0x21; |
215 | return -ENOENT; | 215 | return -ENOENT; |
216 | } | 216 | } |
217 | |||
218 | |||
219 | /** | ||
220 | * gadget_supports_altsettings - return true if altsettings work | ||
221 | * @gadget: the gadget in question | ||
222 | */ | ||
223 | static inline bool gadget_supports_altsettings(struct usb_gadget *gadget) | ||
224 | { | ||
225 | /* PXA 21x/25x/26x has no altsettings at all */ | ||
226 | if (gadget_is_pxa(gadget)) | ||
227 | return false; | ||
228 | |||
229 | /* PXA 27x and 3xx have *broken* altsetting support */ | ||
230 | if (gadget_is_pxa27x(gadget)) | ||
231 | return false; | ||
232 | |||
233 | /* SH3 hardware just doesn't do altsettings */ | ||
234 | if (gadget_is_sh(gadget)) | ||
235 | return false; | ||
236 | |||
237 | /* Everything else is *presumably* fine ... */ | ||
238 | return true; | ||
239 | } | ||
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c index be6613afedbf..48f1c63b7013 100644 --- a/drivers/usb/gadget/goku_udc.c +++ b/drivers/usb/gadget/goku_udc.c | |||
@@ -1790,7 +1790,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |||
1790 | dev->gadget.ops = &goku_ops; | 1790 | dev->gadget.ops = &goku_ops; |
1791 | 1791 | ||
1792 | /* the "gadget" abstracts/virtualizes the controller */ | 1792 | /* the "gadget" abstracts/virtualizes the controller */ |
1793 | strcpy(dev->gadget.dev.bus_id, "gadget"); | 1793 | dev_set_name(&dev->gadget.dev, "gadget"); |
1794 | dev->gadget.dev.parent = &pdev->dev; | 1794 | dev->gadget.dev.parent = &pdev->dev; |
1795 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; | 1795 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; |
1796 | dev->gadget.dev.release = gadget_release; | 1796 | dev->gadget.dev.release = gadget_release; |
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c index f132a9219e11..04692d59fc1c 100644 --- a/drivers/usb/gadget/inode.c +++ b/drivers/usb/gadget/inode.c | |||
@@ -32,6 +32,7 @@ | |||
32 | #include <asm/uaccess.h> | 32 | #include <asm/uaccess.h> |
33 | #include <linux/slab.h> | 33 | #include <linux/slab.h> |
34 | #include <linux/poll.h> | 34 | #include <linux/poll.h> |
35 | #include <linux/smp_lock.h> | ||
35 | 36 | ||
36 | #include <linux/device.h> | 37 | #include <linux/device.h> |
37 | #include <linux/moduleparam.h> | 38 | #include <linux/moduleparam.h> |
@@ -483,8 +484,7 @@ ep_release (struct inode *inode, struct file *fd) | |||
483 | return 0; | 484 | return 0; |
484 | } | 485 | } |
485 | 486 | ||
486 | static int ep_ioctl (struct inode *inode, struct file *fd, | 487 | static long ep_ioctl(struct file *fd, unsigned code, unsigned long value) |
487 | unsigned code, unsigned long value) | ||
488 | { | 488 | { |
489 | struct ep_data *data = fd->private_data; | 489 | struct ep_data *data = fd->private_data; |
490 | int status; | 490 | int status; |
@@ -740,7 +740,7 @@ static const struct file_operations ep_io_operations = { | |||
740 | 740 | ||
741 | .read = ep_read, | 741 | .read = ep_read, |
742 | .write = ep_write, | 742 | .write = ep_write, |
743 | .ioctl = ep_ioctl, | 743 | .unlocked_ioctl = ep_ioctl, |
744 | .release = ep_release, | 744 | .release = ep_release, |
745 | 745 | ||
746 | .aio_read = ep_aio_read, | 746 | .aio_read = ep_aio_read, |
@@ -1294,15 +1294,18 @@ out: | |||
1294 | return mask; | 1294 | return mask; |
1295 | } | 1295 | } |
1296 | 1296 | ||
1297 | static int dev_ioctl (struct inode *inode, struct file *fd, | 1297 | static long dev_ioctl (struct file *fd, unsigned code, unsigned long value) |
1298 | unsigned code, unsigned long value) | ||
1299 | { | 1298 | { |
1300 | struct dev_data *dev = fd->private_data; | 1299 | struct dev_data *dev = fd->private_data; |
1301 | struct usb_gadget *gadget = dev->gadget; | 1300 | struct usb_gadget *gadget = dev->gadget; |
1301 | long ret = -ENOTTY; | ||
1302 | 1302 | ||
1303 | if (gadget->ops->ioctl) | 1303 | if (gadget->ops->ioctl) { |
1304 | return gadget->ops->ioctl (gadget, code, value); | 1304 | lock_kernel(); |
1305 | return -ENOTTY; | 1305 | ret = gadget->ops->ioctl (gadget, code, value); |
1306 | unlock_kernel(); | ||
1307 | } | ||
1308 | return ret; | ||
1306 | } | 1309 | } |
1307 | 1310 | ||
1308 | /* used after device configuration */ | 1311 | /* used after device configuration */ |
@@ -1314,7 +1317,7 @@ static const struct file_operations ep0_io_operations = { | |||
1314 | .write = ep0_write, | 1317 | .write = ep0_write, |
1315 | .fasync = ep0_fasync, | 1318 | .fasync = ep0_fasync, |
1316 | .poll = ep0_poll, | 1319 | .poll = ep0_poll, |
1317 | .ioctl = dev_ioctl, | 1320 | .unlocked_ioctl = dev_ioctl, |
1318 | .release = dev_release, | 1321 | .release = dev_release, |
1319 | }; | 1322 | }; |
1320 | 1323 | ||
@@ -1964,7 +1967,7 @@ static const struct file_operations dev_init_operations = { | |||
1964 | .open = dev_open, | 1967 | .open = dev_open, |
1965 | .write = dev_config, | 1968 | .write = dev_config, |
1966 | .fasync = ep0_fasync, | 1969 | .fasync = ep0_fasync, |
1967 | .ioctl = dev_ioctl, | 1970 | .unlocked_ioctl = dev_ioctl, |
1968 | .release = dev_release, | 1971 | .release = dev_release, |
1969 | }; | 1972 | }; |
1970 | 1973 | ||
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c index 825abd2621b3..c6e7df04c69a 100644 --- a/drivers/usb/gadget/lh7a40x_udc.c +++ b/drivers/usb/gadget/lh7a40x_udc.c | |||
@@ -1970,7 +1970,7 @@ static const struct usb_gadget_ops lh7a40x_udc_ops = { | |||
1970 | 1970 | ||
1971 | static void nop_release(struct device *dev) | 1971 | static void nop_release(struct device *dev) |
1972 | { | 1972 | { |
1973 | DEBUG("%s %s\n", __func__, dev->bus_id); | 1973 | DEBUG("%s %s\n", __func__, dev_name(dev)); |
1974 | } | 1974 | } |
1975 | 1975 | ||
1976 | static struct lh7a40x_udc memory = { | 1976 | static struct lh7a40x_udc memory = { |
diff --git a/drivers/usb/gadget/m66592-udc.c b/drivers/usb/gadget/m66592-udc.c index ee6b35fa870f..8da7535c0c70 100644 --- a/drivers/usb/gadget/m66592-udc.c +++ b/drivers/usb/gadget/m66592-udc.c | |||
@@ -1593,7 +1593,7 @@ static int __init m66592_probe(struct platform_device *pdev) | |||
1593 | 1593 | ||
1594 | m66592->gadget.ops = &m66592_gadget_ops; | 1594 | m66592->gadget.ops = &m66592_gadget_ops; |
1595 | device_initialize(&m66592->gadget.dev); | 1595 | device_initialize(&m66592->gadget.dev); |
1596 | strcpy(m66592->gadget.dev.bus_id, "gadget"); | 1596 | dev_set_name(&m66592->gadget, "gadget"); |
1597 | m66592->gadget.is_dualspeed = 1; | 1597 | m66592->gadget.is_dualspeed = 1; |
1598 | m66592->gadget.dev.parent = &pdev->dev; | 1598 | m66592->gadget.dev.parent = &pdev->dev; |
1599 | m66592->gadget.dev.dma_mask = pdev->dev.dma_mask; | 1599 | m66592->gadget.dev.dma_mask = pdev->dev.dma_mask; |
diff --git a/drivers/usb/gadget/ndis.h b/drivers/usb/gadget/ndis.h index 09e3ee4eeae1..df886cec5ef4 100644 --- a/drivers/usb/gadget/ndis.h +++ b/drivers/usb/gadget/ndis.h | |||
@@ -1,11 +1,11 @@ | |||
1 | /* | 1 | /* |
2 | * ndis.h | 2 | * ndis.h |
3 | * | 3 | * |
4 | * ntddndis.h modified by Benedikt Spranger <b.spranger@pengutronix.de> | 4 | * ntddndis.h modified by Benedikt Spranger <b.spranger@pengutronix.de> |
5 | * | 5 | * |
6 | * Thanks to the cygwin development team, | 6 | * Thanks to the cygwin development team, |
7 | * espacially to Casper S. Hornstrup <chorns@users.sourceforge.net> | 7 | * espacially to Casper S. Hornstrup <chorns@users.sourceforge.net> |
8 | * | 8 | * |
9 | * THIS SOFTWARE IS NOT COPYRIGHTED | 9 | * THIS SOFTWARE IS NOT COPYRIGHTED |
10 | * | 10 | * |
11 | * This source code is offered for use in the public domain. You may | 11 | * This source code is offered for use in the public domain. You may |
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c index e01862300169..b67ab677af72 100644 --- a/drivers/usb/gadget/net2280.c +++ b/drivers/usb/gadget/net2280.c | |||
@@ -2768,7 +2768,7 @@ static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id) | |||
2768 | dev->gadget.is_dualspeed = 1; | 2768 | dev->gadget.is_dualspeed = 1; |
2769 | 2769 | ||
2770 | /* the "gadget" abstracts/virtualizes the controller */ | 2770 | /* the "gadget" abstracts/virtualizes the controller */ |
2771 | strcpy (dev->gadget.dev.bus_id, "gadget"); | 2771 | dev_set_name(&dev->gadget.dev, "gadget"); |
2772 | dev->gadget.dev.parent = &pdev->dev; | 2772 | dev->gadget.dev.parent = &pdev->dev; |
2773 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; | 2773 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; |
2774 | dev->gadget.dev.release = gadget_release; | 2774 | dev->gadget.dev.release = gadget_release; |
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c index 03a7f49d207d..4b79a8509e84 100644 --- a/drivers/usb/gadget/omap_udc.c +++ b/drivers/usb/gadget/omap_udc.c | |||
@@ -2686,7 +2686,7 @@ omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv) | |||
2686 | udc->gadget.name = driver_name; | 2686 | udc->gadget.name = driver_name; |
2687 | 2687 | ||
2688 | device_initialize(&udc->gadget.dev); | 2688 | device_initialize(&udc->gadget.dev); |
2689 | strcpy (udc->gadget.dev.bus_id, "gadget"); | 2689 | dev_set_name(&udc->gadget.dev, "gadget"); |
2690 | udc->gadget.dev.release = omap_udc_release; | 2690 | udc->gadget.dev.release = omap_udc_release; |
2691 | udc->gadget.dev.parent = &odev->dev; | 2691 | udc->gadget.dev.parent = &odev->dev; |
2692 | if (use_dma) | 2692 | if (use_dma) |
diff --git a/drivers/usb/gadget/printer.c b/drivers/usb/gadget/printer.c index ec8f2eb041ca..9caaec3ae9a3 100644 --- a/drivers/usb/gadget/printer.c +++ b/drivers/usb/gadget/printer.c | |||
@@ -828,9 +828,8 @@ printer_poll(struct file *fd, poll_table *wait) | |||
828 | return status; | 828 | return status; |
829 | } | 829 | } |
830 | 830 | ||
831 | static int | 831 | static long |
832 | printer_ioctl(struct inode *inode, struct file *fd, unsigned int code, | 832 | printer_ioctl(struct file *fd, unsigned int code, unsigned long arg) |
833 | unsigned long arg) | ||
834 | { | 833 | { |
835 | struct printer_dev *dev = fd->private_data; | 834 | struct printer_dev *dev = fd->private_data; |
836 | unsigned long flags; | 835 | unsigned long flags; |
@@ -869,7 +868,7 @@ static struct file_operations printer_io_operations = { | |||
869 | .write = printer_write, | 868 | .write = printer_write, |
870 | .fsync = printer_fsync, | 869 | .fsync = printer_fsync, |
871 | .poll = printer_poll, | 870 | .poll = printer_poll, |
872 | .ioctl = printer_ioctl, | 871 | .unlocked_ioctl = printer_ioctl, |
873 | .release = printer_close | 872 | .release = printer_close |
874 | }; | 873 | }; |
875 | 874 | ||
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c index 031dceb93023..fbd6289977c8 100644 --- a/drivers/usb/gadget/pxa25x_udc.c +++ b/drivers/usb/gadget/pxa25x_udc.c | |||
@@ -1818,7 +1818,7 @@ pxa25x_udc_irq(int irq, void *_dev) | |||
1818 | 1818 | ||
1819 | static void nop_release (struct device *dev) | 1819 | static void nop_release (struct device *dev) |
1820 | { | 1820 | { |
1821 | DMSG("%s %s\n", __func__, dev->bus_id); | 1821 | DMSG("%s %s\n", __func__, dev_name(dev)); |
1822 | } | 1822 | } |
1823 | 1823 | ||
1824 | /* this uses load-time allocation and initialization (instead of | 1824 | /* this uses load-time allocation and initialization (instead of |
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c index 9c0e82ec5c43..9d447d8cfc0c 100644 --- a/drivers/usb/gadget/pxa27x_udc.c +++ b/drivers/usb/gadget/pxa27x_udc.c | |||
@@ -1575,7 +1575,6 @@ static void udc_enable(struct pxa_udc *udc) | |||
1575 | { | 1575 | { |
1576 | udc_writel(udc, UDCICR0, 0); | 1576 | udc_writel(udc, UDCICR0, 0); |
1577 | udc_writel(udc, UDCICR1, 0); | 1577 | udc_writel(udc, UDCICR1, 0); |
1578 | udc_writel(udc, UP2OCR, UP2OCR_HXOE); | ||
1579 | udc_clear_mask_UDCCR(udc, UDCCR_UDE); | 1578 | udc_clear_mask_UDCCR(udc, UDCCR_UDE); |
1580 | 1579 | ||
1581 | clk_enable(udc->clk); | 1580 | clk_enable(udc->clk); |
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c index d0677f5d3cd5..7228e8562236 100644 --- a/drivers/usb/gadget/rndis.c +++ b/drivers/usb/gadget/rndis.c | |||
@@ -1,8 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * RNDIS MSG parser | 2 | * RNDIS MSG parser |
3 | * | 3 | * |
4 | * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $ | ||
5 | * | ||
6 | * Authors: Benedikt Spranger, Pengutronix | 4 | * Authors: Benedikt Spranger, Pengutronix |
7 | * Robert Schwebel, Pengutronix | 5 | * Robert Schwebel, Pengutronix |
8 | * | 6 | * |
@@ -30,6 +28,7 @@ | |||
30 | #include <linux/init.h> | 28 | #include <linux/init.h> |
31 | #include <linux/list.h> | 29 | #include <linux/list.h> |
32 | #include <linux/proc_fs.h> | 30 | #include <linux/proc_fs.h> |
31 | #include <linux/seq_file.h> | ||
33 | #include <linux/netdevice.h> | 32 | #include <linux/netdevice.h> |
34 | 33 | ||
35 | #include <asm/io.h> | 34 | #include <asm/io.h> |
@@ -38,9 +37,7 @@ | |||
38 | #include <asm/unaligned.h> | 37 | #include <asm/unaligned.h> |
39 | 38 | ||
40 | 39 | ||
41 | #undef RNDIS_PM | 40 | #undef VERBOSE_DEBUG |
42 | #undef RNDIS_WAKEUP | ||
43 | #undef VERBOSE | ||
44 | 41 | ||
45 | #include "rndis.h" | 42 | #include "rndis.h" |
46 | 43 | ||
@@ -96,9 +93,6 @@ static const u32 oid_supported_list [] = | |||
96 | OID_GEN_MAXIMUM_TOTAL_SIZE, | 93 | OID_GEN_MAXIMUM_TOTAL_SIZE, |
97 | OID_GEN_MEDIA_CONNECT_STATUS, | 94 | OID_GEN_MEDIA_CONNECT_STATUS, |
98 | OID_GEN_PHYSICAL_MEDIUM, | 95 | OID_GEN_PHYSICAL_MEDIUM, |
99 | #if 0 | ||
100 | OID_GEN_RNDIS_CONFIG_PARAMETER, | ||
101 | #endif | ||
102 | 96 | ||
103 | /* the statistical stuff */ | 97 | /* the statistical stuff */ |
104 | OID_GEN_XMIT_OK, | 98 | OID_GEN_XMIT_OK, |
@@ -146,7 +140,14 @@ static const u32 oid_supported_list [] = | |||
146 | #endif /* RNDIS_OPTIONAL_STATS */ | 140 | #endif /* RNDIS_OPTIONAL_STATS */ |
147 | 141 | ||
148 | #ifdef RNDIS_PM | 142 | #ifdef RNDIS_PM |
149 | /* PM and wakeup are mandatory for USB: */ | 143 | /* PM and wakeup are "mandatory" for USB, but the RNDIS specs |
144 | * don't say what they mean ... and the NDIS specs are often | ||
145 | * confusing and/or ambiguous in this context. (That is, more | ||
146 | * so than their specs for the other OIDs.) | ||
147 | * | ||
148 | * FIXME someone who knows what these should do, please | ||
149 | * implement them! | ||
150 | */ | ||
150 | 151 | ||
151 | /* power management */ | 152 | /* power management */ |
152 | OID_PNP_CAPABILITIES, | 153 | OID_PNP_CAPABILITIES, |
@@ -173,6 +174,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
173 | __le32 *outbuf; | 174 | __le32 *outbuf; |
174 | int i, count; | 175 | int i, count; |
175 | rndis_query_cmplt_type *resp; | 176 | rndis_query_cmplt_type *resp; |
177 | struct net_device *net; | ||
178 | struct net_device_stats *stats; | ||
176 | 179 | ||
177 | if (!r) return -ENOMEM; | 180 | if (!r) return -ENOMEM; |
178 | resp = (rndis_query_cmplt_type *) r->buf; | 181 | resp = (rndis_query_cmplt_type *) r->buf; |
@@ -194,6 +197,12 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
194 | outbuf = (__le32 *) &resp[1]; | 197 | outbuf = (__le32 *) &resp[1]; |
195 | resp->InformationBufferOffset = __constant_cpu_to_le32 (16); | 198 | resp->InformationBufferOffset = __constant_cpu_to_le32 (16); |
196 | 199 | ||
200 | net = rndis_per_dev_params[configNr].dev; | ||
201 | if (net->get_stats) | ||
202 | stats = net->get_stats(net); | ||
203 | else | ||
204 | stats = NULL; | ||
205 | |||
197 | switch (OID) { | 206 | switch (OID) { |
198 | 207 | ||
199 | /* general oids (table 4-1) */ | 208 | /* general oids (table 4-1) */ |
@@ -350,11 +359,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
350 | case OID_GEN_XMIT_OK: | 359 | case OID_GEN_XMIT_OK: |
351 | if (rndis_debug > 1) | 360 | if (rndis_debug > 1) |
352 | DBG("%s: OID_GEN_XMIT_OK\n", __func__); | 361 | DBG("%s: OID_GEN_XMIT_OK\n", __func__); |
353 | if (rndis_per_dev_params [configNr].stats) { | 362 | if (stats) { |
354 | *outbuf = cpu_to_le32 ( | 363 | *outbuf = cpu_to_le32(stats->tx_packets |
355 | rndis_per_dev_params [configNr].stats->tx_packets - | 364 | - stats->tx_errors - stats->tx_dropped); |
356 | rndis_per_dev_params [configNr].stats->tx_errors - | ||
357 | rndis_per_dev_params [configNr].stats->tx_dropped); | ||
358 | retval = 0; | 365 | retval = 0; |
359 | } | 366 | } |
360 | break; | 367 | break; |
@@ -363,11 +370,9 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
363 | case OID_GEN_RCV_OK: | 370 | case OID_GEN_RCV_OK: |
364 | if (rndis_debug > 1) | 371 | if (rndis_debug > 1) |
365 | DBG("%s: OID_GEN_RCV_OK\n", __func__); | 372 | DBG("%s: OID_GEN_RCV_OK\n", __func__); |
366 | if (rndis_per_dev_params [configNr].stats) { | 373 | if (stats) { |
367 | *outbuf = cpu_to_le32 ( | 374 | *outbuf = cpu_to_le32(stats->rx_packets |
368 | rndis_per_dev_params [configNr].stats->rx_packets - | 375 | - stats->rx_errors - stats->rx_dropped); |
369 | rndis_per_dev_params [configNr].stats->rx_errors - | ||
370 | rndis_per_dev_params [configNr].stats->rx_dropped); | ||
371 | retval = 0; | 376 | retval = 0; |
372 | } | 377 | } |
373 | break; | 378 | break; |
@@ -376,9 +381,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
376 | case OID_GEN_XMIT_ERROR: | 381 | case OID_GEN_XMIT_ERROR: |
377 | if (rndis_debug > 1) | 382 | if (rndis_debug > 1) |
378 | DBG("%s: OID_GEN_XMIT_ERROR\n", __func__); | 383 | DBG("%s: OID_GEN_XMIT_ERROR\n", __func__); |
379 | if (rndis_per_dev_params [configNr].stats) { | 384 | if (stats) { |
380 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | 385 | *outbuf = cpu_to_le32(stats->tx_errors); |
381 | .stats->tx_errors); | ||
382 | retval = 0; | 386 | retval = 0; |
383 | } | 387 | } |
384 | break; | 388 | break; |
@@ -387,9 +391,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
387 | case OID_GEN_RCV_ERROR: | 391 | case OID_GEN_RCV_ERROR: |
388 | if (rndis_debug > 1) | 392 | if (rndis_debug > 1) |
389 | DBG("%s: OID_GEN_RCV_ERROR\n", __func__); | 393 | DBG("%s: OID_GEN_RCV_ERROR\n", __func__); |
390 | if (rndis_per_dev_params [configNr].stats) { | 394 | if (stats) { |
391 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | 395 | *outbuf = cpu_to_le32(stats->rx_errors); |
392 | .stats->rx_errors); | ||
393 | retval = 0; | 396 | retval = 0; |
394 | } | 397 | } |
395 | break; | 398 | break; |
@@ -397,150 +400,12 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
397 | /* mandatory */ | 400 | /* mandatory */ |
398 | case OID_GEN_RCV_NO_BUFFER: | 401 | case OID_GEN_RCV_NO_BUFFER: |
399 | DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__); | 402 | DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__); |
400 | if (rndis_per_dev_params [configNr].stats) { | 403 | if (stats) { |
401 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | 404 | *outbuf = cpu_to_le32(stats->rx_dropped); |
402 | .stats->rx_dropped); | ||
403 | retval = 0; | ||
404 | } | ||
405 | break; | ||
406 | |||
407 | #ifdef RNDIS_OPTIONAL_STATS | ||
408 | case OID_GEN_DIRECTED_BYTES_XMIT: | ||
409 | DBG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__); | ||
410 | /* | ||
411 | * Aunt Tilly's size of shoes | ||
412 | * minus antarctica count of penguins | ||
413 | * divided by weight of Alpha Centauri | ||
414 | */ | ||
415 | if (rndis_per_dev_params [configNr].stats) { | ||
416 | *outbuf = cpu_to_le32 ( | ||
417 | (rndis_per_dev_params [configNr] | ||
418 | .stats->tx_packets - | ||
419 | rndis_per_dev_params [configNr] | ||
420 | .stats->tx_errors - | ||
421 | rndis_per_dev_params [configNr] | ||
422 | .stats->tx_dropped) | ||
423 | * 123); | ||
424 | retval = 0; | ||
425 | } | ||
426 | break; | ||
427 | |||
428 | case OID_GEN_DIRECTED_FRAMES_XMIT: | ||
429 | DBG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__); | ||
430 | /* dito */ | ||
431 | if (rndis_per_dev_params [configNr].stats) { | ||
432 | *outbuf = cpu_to_le32 ( | ||
433 | (rndis_per_dev_params [configNr] | ||
434 | .stats->tx_packets - | ||
435 | rndis_per_dev_params [configNr] | ||
436 | .stats->tx_errors - | ||
437 | rndis_per_dev_params [configNr] | ||
438 | .stats->tx_dropped) | ||
439 | / 123); | ||
440 | retval = 0; | ||
441 | } | ||
442 | break; | ||
443 | |||
444 | case OID_GEN_MULTICAST_BYTES_XMIT: | ||
445 | DBG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__); | ||
446 | if (rndis_per_dev_params [configNr].stats) { | ||
447 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
448 | .stats->multicast*1234); | ||
449 | retval = 0; | ||
450 | } | ||
451 | break; | ||
452 | |||
453 | case OID_GEN_MULTICAST_FRAMES_XMIT: | ||
454 | DBG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__); | ||
455 | if (rndis_per_dev_params [configNr].stats) { | ||
456 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
457 | .stats->multicast); | ||
458 | retval = 0; | ||
459 | } | ||
460 | break; | ||
461 | |||
462 | case OID_GEN_BROADCAST_BYTES_XMIT: | ||
463 | DBG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__); | ||
464 | if (rndis_per_dev_params [configNr].stats) { | ||
465 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
466 | .stats->tx_packets/42*255); | ||
467 | retval = 0; | ||
468 | } | ||
469 | break; | ||
470 | |||
471 | case OID_GEN_BROADCAST_FRAMES_XMIT: | ||
472 | DBG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__); | ||
473 | if (rndis_per_dev_params [configNr].stats) { | ||
474 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
475 | .stats->tx_packets/42); | ||
476 | retval = 0; | ||
477 | } | ||
478 | break; | ||
479 | |||
480 | case OID_GEN_DIRECTED_BYTES_RCV: | ||
481 | DBG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__); | ||
482 | *outbuf = __constant_cpu_to_le32 (0); | ||
483 | retval = 0; | ||
484 | break; | ||
485 | |||
486 | case OID_GEN_DIRECTED_FRAMES_RCV: | ||
487 | DBG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__); | ||
488 | *outbuf = __constant_cpu_to_le32 (0); | ||
489 | retval = 0; | ||
490 | break; | ||
491 | |||
492 | case OID_GEN_MULTICAST_BYTES_RCV: | ||
493 | DBG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__); | ||
494 | if (rndis_per_dev_params [configNr].stats) { | ||
495 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
496 | .stats->multicast * 1111); | ||
497 | retval = 0; | ||
498 | } | ||
499 | break; | ||
500 | |||
501 | case OID_GEN_MULTICAST_FRAMES_RCV: | ||
502 | DBG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__); | ||
503 | if (rndis_per_dev_params [configNr].stats) { | ||
504 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
505 | .stats->multicast); | ||
506 | retval = 0; | ||
507 | } | ||
508 | break; | ||
509 | |||
510 | case OID_GEN_BROADCAST_BYTES_RCV: | ||
511 | DBG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__); | ||
512 | if (rndis_per_dev_params [configNr].stats) { | ||
513 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
514 | .stats->rx_packets/42*255); | ||
515 | retval = 0; | ||
516 | } | ||
517 | break; | ||
518 | |||
519 | case OID_GEN_BROADCAST_FRAMES_RCV: | ||
520 | DBG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__); | ||
521 | if (rndis_per_dev_params [configNr].stats) { | ||
522 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
523 | .stats->rx_packets/42); | ||
524 | retval = 0; | 405 | retval = 0; |
525 | } | 406 | } |
526 | break; | 407 | break; |
527 | 408 | ||
528 | case OID_GEN_RCV_CRC_ERROR: | ||
529 | DBG("%s: OID_GEN_RCV_CRC_ERROR\n", __func__); | ||
530 | if (rndis_per_dev_params [configNr].stats) { | ||
531 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | ||
532 | .stats->rx_crc_errors); | ||
533 | retval = 0; | ||
534 | } | ||
535 | break; | ||
536 | |||
537 | case OID_GEN_TRANSMIT_QUEUE_LENGTH: | ||
538 | DBG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__); | ||
539 | *outbuf = __constant_cpu_to_le32 (0); | ||
540 | retval = 0; | ||
541 | break; | ||
542 | #endif /* RNDIS_OPTIONAL_STATS */ | ||
543 | |||
544 | /* ieee802.3 OIDs (table 4-3) */ | 409 | /* ieee802.3 OIDs (table 4-3) */ |
545 | 410 | ||
546 | /* mandatory */ | 411 | /* mandatory */ |
@@ -592,9 +457,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
592 | /* mandatory */ | 457 | /* mandatory */ |
593 | case OID_802_3_RCV_ERROR_ALIGNMENT: | 458 | case OID_802_3_RCV_ERROR_ALIGNMENT: |
594 | DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__); | 459 | DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__); |
595 | if (rndis_per_dev_params [configNr].stats) { | 460 | if (stats) { |
596 | *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr] | 461 | *outbuf = cpu_to_le32(stats->rx_frame_errors); |
597 | .stats->rx_frame_errors); | ||
598 | retval = 0; | 462 | retval = 0; |
599 | } | 463 | } |
600 | break; | 464 | break; |
@@ -613,64 +477,6 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len, | |||
613 | retval = 0; | 477 | retval = 0; |
614 | break; | 478 | break; |
615 | 479 | ||
616 | #ifdef RNDIS_OPTIONAL_STATS | ||
617 | case OID_802_3_XMIT_DEFERRED: | ||
618 | DBG("%s: OID_802_3_XMIT_DEFERRED\n", __func__); | ||
619 | /* TODO */ | ||
620 | break; | ||
621 | |||
622 | case OID_802_3_XMIT_MAX_COLLISIONS: | ||
623 | DBG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__); | ||
624 | /* TODO */ | ||
625 | break; | ||
626 | |||
627 | case OID_802_3_RCV_OVERRUN: | ||
628 | DBG("%s: OID_802_3_RCV_OVERRUN\n", __func__); | ||
629 | /* TODO */ | ||
630 | break; | ||
631 | |||
632 | case OID_802_3_XMIT_UNDERRUN: | ||
633 | DBG("%s: OID_802_3_XMIT_UNDERRUN\n", __func__); | ||
634 | /* TODO */ | ||
635 | break; | ||
636 | |||
637 | case OID_802_3_XMIT_HEARTBEAT_FAILURE: | ||
638 | DBG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__); | ||
639 | /* TODO */ | ||
640 | break; | ||
641 | |||
642 | case OID_802_3_XMIT_TIMES_CRS_LOST: | ||
643 | DBG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__); | ||
644 | /* TODO */ | ||
645 | break; | ||
646 | |||
647 | case OID_802_3_XMIT_LATE_COLLISIONS: | ||
648 | DBG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__); | ||
649 | /* TODO */ | ||
650 | break; | ||
651 | #endif /* RNDIS_OPTIONAL_STATS */ | ||
652 | |||
653 | #ifdef RNDIS_PM | ||
654 | /* power management OIDs (table 4-5) */ | ||
655 | case OID_PNP_CAPABILITIES: | ||
656 | DBG("%s: OID_PNP_CAPABILITIES\n", __func__); | ||
657 | |||
658 | /* for now, no wakeup capabilities */ | ||
659 | length = sizeof (struct NDIS_PNP_CAPABILITIES); | ||
660 | memset(outbuf, 0, length); | ||
661 | retval = 0; | ||
662 | break; | ||
663 | case OID_PNP_QUERY_POWER: | ||
664 | DBG("%s: OID_PNP_QUERY_POWER D%d\n", __func__, | ||
665 | get_unaligned_le32(buf) - 1); | ||
666 | /* only suspend is a real power state, and | ||
667 | * it can't be entered by OID_PNP_SET_POWER... | ||
668 | */ | ||
669 | length = 0; | ||
670 | retval = 0; | ||
671 | break; | ||
672 | #endif | ||
673 | |||
674 | default: | 480 | default: |
675 | pr_warning("%s: query unknown OID 0x%08X\n", | 481 | pr_warning("%s: query unknown OID 0x%08X\n", |
676 | __func__, OID); | 482 | __func__, OID); |
@@ -726,9 +532,6 @@ static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len, | |||
726 | * what makes the packet flow start and stop, like | 532 | * what makes the packet flow start and stop, like |
727 | * activating the CDC Ethernet altsetting. | 533 | * activating the CDC Ethernet altsetting. |
728 | */ | 534 | */ |
729 | #ifdef RNDIS_PM | ||
730 | update_linkstate: | ||
731 | #endif | ||
732 | retval = 0; | 535 | retval = 0; |
733 | if (*params->filter) { | 536 | if (*params->filter) { |
734 | params->state = RNDIS_DATA_INITIALIZED; | 537 | params->state = RNDIS_DATA_INITIALIZED; |
@@ -747,49 +550,6 @@ update_linkstate: | |||
747 | DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__); | 550 | DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__); |
748 | retval = 0; | 551 | retval = 0; |
749 | break; | 552 | break; |
750 | #if 0 | ||
751 | case OID_GEN_RNDIS_CONFIG_PARAMETER: | ||
752 | { | ||
753 | struct rndis_config_parameter *param; | ||
754 | param = (struct rndis_config_parameter *) buf; | ||
755 | DBG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n", | ||
756 | __func__, | ||
757 | min(cpu_to_le32(param->ParameterNameLength),80), | ||
758 | buf + param->ParameterNameOffset); | ||
759 | retval = 0; | ||
760 | } | ||
761 | break; | ||
762 | #endif | ||
763 | |||
764 | #ifdef RNDIS_PM | ||
765 | case OID_PNP_SET_POWER: | ||
766 | /* The only real power state is USB suspend, and RNDIS requests | ||
767 | * can't enter it; this one isn't really about power. After | ||
768 | * resuming, Windows forces a reset, and then SET_POWER D0. | ||
769 | * FIXME ... then things go batty; Windows wedges itself. | ||
770 | */ | ||
771 | i = get_unaligned_le32(buf); | ||
772 | DBG("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1); | ||
773 | switch (i) { | ||
774 | case NdisDeviceStateD0: | ||
775 | *params->filter = params->saved_filter; | ||
776 | goto update_linkstate; | ||
777 | case NdisDeviceStateD3: | ||
778 | case NdisDeviceStateD2: | ||
779 | case NdisDeviceStateD1: | ||
780 | params->saved_filter = *params->filter; | ||
781 | retval = 0; | ||
782 | break; | ||
783 | } | ||
784 | break; | ||
785 | |||
786 | #ifdef RNDIS_WAKEUP | ||
787 | // no wakeup support advertised, so wakeup OIDs always fail: | ||
788 | // - OID_PNP_ENABLE_WAKE_UP | ||
789 | // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN | ||
790 | #endif | ||
791 | |||
792 | #endif /* RNDIS_PM */ | ||
793 | 553 | ||
794 | default: | 554 | default: |
795 | pr_warning("%s: set unknown OID 0x%08X, size %d\n", | 555 | pr_warning("%s: set unknown OID 0x%08X, size %d\n", |
@@ -807,8 +567,10 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf) | |||
807 | { | 567 | { |
808 | rndis_init_cmplt_type *resp; | 568 | rndis_init_cmplt_type *resp; |
809 | rndis_resp_t *r; | 569 | rndis_resp_t *r; |
570 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
810 | 571 | ||
811 | if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP; | 572 | if (!params->dev) |
573 | return -ENOTSUPP; | ||
812 | 574 | ||
813 | r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type)); | 575 | r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type)); |
814 | if (!r) | 576 | if (!r) |
@@ -826,7 +588,7 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf) | |||
826 | resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3); | 588 | resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3); |
827 | resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1); | 589 | resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1); |
828 | resp->MaxTransferSize = cpu_to_le32 ( | 590 | resp->MaxTransferSize = cpu_to_le32 ( |
829 | rndis_per_dev_params [configNr].dev->mtu | 591 | params->dev->mtu |
830 | + sizeof (struct ethhdr) | 592 | + sizeof (struct ethhdr) |
831 | + sizeof (struct rndis_packet_msg_type) | 593 | + sizeof (struct rndis_packet_msg_type) |
832 | + 22); | 594 | + 22); |
@@ -834,10 +596,7 @@ static int rndis_init_response (int configNr, rndis_init_msg_type *buf) | |||
834 | resp->AFListOffset = __constant_cpu_to_le32 (0); | 596 | resp->AFListOffset = __constant_cpu_to_le32 (0); |
835 | resp->AFListSize = __constant_cpu_to_le32 (0); | 597 | resp->AFListSize = __constant_cpu_to_le32 (0); |
836 | 598 | ||
837 | if (rndis_per_dev_params [configNr].ack) | 599 | params->resp_avail(params->v); |
838 | rndis_per_dev_params [configNr].ack ( | ||
839 | rndis_per_dev_params [configNr].dev); | ||
840 | |||
841 | return 0; | 600 | return 0; |
842 | } | 601 | } |
843 | 602 | ||
@@ -845,9 +604,11 @@ static int rndis_query_response (int configNr, rndis_query_msg_type *buf) | |||
845 | { | 604 | { |
846 | rndis_query_cmplt_type *resp; | 605 | rndis_query_cmplt_type *resp; |
847 | rndis_resp_t *r; | 606 | rndis_resp_t *r; |
607 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
848 | 608 | ||
849 | // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); | 609 | // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); |
850 | if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP; | 610 | if (!params->dev) |
611 | return -ENOTSUPP; | ||
851 | 612 | ||
852 | /* | 613 | /* |
853 | * we need more memory: | 614 | * we need more memory: |
@@ -878,9 +639,7 @@ static int rndis_query_response (int configNr, rndis_query_msg_type *buf) | |||
878 | } else | 639 | } else |
879 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); | 640 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); |
880 | 641 | ||
881 | if (rndis_per_dev_params [configNr].ack) | 642 | params->resp_avail(params->v); |
882 | rndis_per_dev_params [configNr].ack ( | ||
883 | rndis_per_dev_params [configNr].dev); | ||
884 | return 0; | 643 | return 0; |
885 | } | 644 | } |
886 | 645 | ||
@@ -889,6 +648,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf) | |||
889 | u32 BufLength, BufOffset; | 648 | u32 BufLength, BufOffset; |
890 | rndis_set_cmplt_type *resp; | 649 | rndis_set_cmplt_type *resp; |
891 | rndis_resp_t *r; | 650 | rndis_resp_t *r; |
651 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
892 | 652 | ||
893 | r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type)); | 653 | r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type)); |
894 | if (!r) | 654 | if (!r) |
@@ -898,7 +658,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf) | |||
898 | BufLength = le32_to_cpu (buf->InformationBufferLength); | 658 | BufLength = le32_to_cpu (buf->InformationBufferLength); |
899 | BufOffset = le32_to_cpu (buf->InformationBufferOffset); | 659 | BufOffset = le32_to_cpu (buf->InformationBufferOffset); |
900 | 660 | ||
901 | #ifdef VERBOSE | 661 | #ifdef VERBOSE_DEBUG |
902 | DBG("%s: Length: %d\n", __func__, BufLength); | 662 | DBG("%s: Length: %d\n", __func__, BufLength); |
903 | DBG("%s: Offset: %d\n", __func__, BufOffset); | 663 | DBG("%s: Offset: %d\n", __func__, BufOffset); |
904 | DBG("%s: InfoBuffer: ", __func__); | 664 | DBG("%s: InfoBuffer: ", __func__); |
@@ -919,10 +679,7 @@ static int rndis_set_response (int configNr, rndis_set_msg_type *buf) | |||
919 | else | 679 | else |
920 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); | 680 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); |
921 | 681 | ||
922 | if (rndis_per_dev_params [configNr].ack) | 682 | params->resp_avail(params->v); |
923 | rndis_per_dev_params [configNr].ack ( | ||
924 | rndis_per_dev_params [configNr].dev); | ||
925 | |||
926 | return 0; | 683 | return 0; |
927 | } | 684 | } |
928 | 685 | ||
@@ -930,6 +687,7 @@ static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf) | |||
930 | { | 687 | { |
931 | rndis_reset_cmplt_type *resp; | 688 | rndis_reset_cmplt_type *resp; |
932 | rndis_resp_t *r; | 689 | rndis_resp_t *r; |
690 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
933 | 691 | ||
934 | r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type)); | 692 | r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type)); |
935 | if (!r) | 693 | if (!r) |
@@ -942,10 +700,7 @@ static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf) | |||
942 | /* resent information */ | 700 | /* resent information */ |
943 | resp->AddressingReset = __constant_cpu_to_le32 (1); | 701 | resp->AddressingReset = __constant_cpu_to_le32 (1); |
944 | 702 | ||
945 | if (rndis_per_dev_params [configNr].ack) | 703 | params->resp_avail(params->v); |
946 | rndis_per_dev_params [configNr].ack ( | ||
947 | rndis_per_dev_params [configNr].dev); | ||
948 | |||
949 | return 0; | 704 | return 0; |
950 | } | 705 | } |
951 | 706 | ||
@@ -954,6 +709,7 @@ static int rndis_keepalive_response (int configNr, | |||
954 | { | 709 | { |
955 | rndis_keepalive_cmplt_type *resp; | 710 | rndis_keepalive_cmplt_type *resp; |
956 | rndis_resp_t *r; | 711 | rndis_resp_t *r; |
712 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
957 | 713 | ||
958 | /* host "should" check only in RNDIS_DATA_INITIALIZED state */ | 714 | /* host "should" check only in RNDIS_DATA_INITIALIZED state */ |
959 | 715 | ||
@@ -968,10 +724,7 @@ static int rndis_keepalive_response (int configNr, | |||
968 | resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ | 724 | resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ |
969 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); | 725 | resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS); |
970 | 726 | ||
971 | if (rndis_per_dev_params [configNr].ack) | 727 | params->resp_avail(params->v); |
972 | rndis_per_dev_params [configNr].ack ( | ||
973 | rndis_per_dev_params [configNr].dev); | ||
974 | |||
975 | return 0; | 728 | return 0; |
976 | } | 729 | } |
977 | 730 | ||
@@ -983,8 +736,9 @@ static int rndis_indicate_status_msg (int configNr, u32 status) | |||
983 | { | 736 | { |
984 | rndis_indicate_status_msg_type *resp; | 737 | rndis_indicate_status_msg_type *resp; |
985 | rndis_resp_t *r; | 738 | rndis_resp_t *r; |
739 | struct rndis_params *params = rndis_per_dev_params + configNr; | ||
986 | 740 | ||
987 | if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED) | 741 | if (params->state == RNDIS_UNINITIALIZED) |
988 | return -ENOTSUPP; | 742 | return -ENOTSUPP; |
989 | 743 | ||
990 | r = rndis_add_response (configNr, | 744 | r = rndis_add_response (configNr, |
@@ -1000,9 +754,7 @@ static int rndis_indicate_status_msg (int configNr, u32 status) | |||
1000 | resp->StatusBufferLength = __constant_cpu_to_le32 (0); | 754 | resp->StatusBufferLength = __constant_cpu_to_le32 (0); |
1001 | resp->StatusBufferOffset = __constant_cpu_to_le32 (0); | 755 | resp->StatusBufferOffset = __constant_cpu_to_le32 (0); |
1002 | 756 | ||
1003 | if (rndis_per_dev_params [configNr].ack) | 757 | params->resp_avail(params->v); |
1004 | rndis_per_dev_params [configNr].ack ( | ||
1005 | rndis_per_dev_params [configNr].dev); | ||
1006 | return 0; | 758 | return 0; |
1007 | } | 759 | } |
1008 | 760 | ||
@@ -1029,7 +781,6 @@ void rndis_uninit (int configNr) | |||
1029 | 781 | ||
1030 | if (configNr >= RNDIS_MAX_CONFIGS) | 782 | if (configNr >= RNDIS_MAX_CONFIGS) |
1031 | return; | 783 | return; |
1032 | rndis_per_dev_params [configNr].used = 0; | ||
1033 | rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED; | 784 | rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED; |
1034 | 785 | ||
1035 | /* drain the response queue */ | 786 | /* drain the response queue */ |
@@ -1142,21 +893,25 @@ int rndis_msg_parser (u8 configNr, u8 *buf) | |||
1142 | return -ENOTSUPP; | 893 | return -ENOTSUPP; |
1143 | } | 894 | } |
1144 | 895 | ||
1145 | int rndis_register (int (* rndis_control_ack) (struct net_device *)) | 896 | int rndis_register(void (*resp_avail)(void *v), void *v) |
1146 | { | 897 | { |
1147 | u8 i; | 898 | u8 i; |
1148 | 899 | ||
900 | if (!resp_avail) | ||
901 | return -EINVAL; | ||
902 | |||
1149 | for (i = 0; i < RNDIS_MAX_CONFIGS; i++) { | 903 | for (i = 0; i < RNDIS_MAX_CONFIGS; i++) { |
1150 | if (!rndis_per_dev_params [i].used) { | 904 | if (!rndis_per_dev_params [i].used) { |
1151 | rndis_per_dev_params [i].used = 1; | 905 | rndis_per_dev_params [i].used = 1; |
1152 | rndis_per_dev_params [i].ack = rndis_control_ack; | 906 | rndis_per_dev_params [i].resp_avail = resp_avail; |
907 | rndis_per_dev_params [i].v = v; | ||
1153 | DBG("%s: configNr = %d\n", __func__, i); | 908 | DBG("%s: configNr = %d\n", __func__, i); |
1154 | return i; | 909 | return i; |
1155 | } | 910 | } |
1156 | } | 911 | } |
1157 | DBG("failed\n"); | 912 | DBG("failed\n"); |
1158 | 913 | ||
1159 | return -1; | 914 | return -ENODEV; |
1160 | } | 915 | } |
1161 | 916 | ||
1162 | void rndis_deregister (int configNr) | 917 | void rndis_deregister (int configNr) |
@@ -1169,16 +924,14 @@ void rndis_deregister (int configNr) | |||
1169 | return; | 924 | return; |
1170 | } | 925 | } |
1171 | 926 | ||
1172 | int rndis_set_param_dev (u8 configNr, struct net_device *dev, | 927 | int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter) |
1173 | struct net_device_stats *stats, | ||
1174 | u16 *cdc_filter) | ||
1175 | { | 928 | { |
1176 | DBG("%s:\n", __func__ ); | 929 | DBG("%s:\n", __func__ ); |
1177 | if (!dev || !stats) return -1; | 930 | if (!dev) |
931 | return -EINVAL; | ||
1178 | if (configNr >= RNDIS_MAX_CONFIGS) return -1; | 932 | if (configNr >= RNDIS_MAX_CONFIGS) return -1; |
1179 | 933 | ||
1180 | rndis_per_dev_params [configNr].dev = dev; | 934 | rndis_per_dev_params [configNr].dev = dev; |
1181 | rndis_per_dev_params [configNr].stats = stats; | ||
1182 | rndis_per_dev_params [configNr].filter = cdc_filter; | 935 | rndis_per_dev_params [configNr].filter = cdc_filter; |
1183 | 936 | ||
1184 | return 0; | 937 | return 0; |
@@ -1296,14 +1049,11 @@ int rndis_rm_hdr(struct sk_buff *skb) | |||
1296 | 1049 | ||
1297 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | 1050 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
1298 | 1051 | ||
1299 | static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof, | 1052 | static int rndis_proc_show(struct seq_file *m, void *v) |
1300 | void *data) | ||
1301 | { | 1053 | { |
1302 | char *out = page; | 1054 | rndis_params *param = m->private; |
1303 | int len; | ||
1304 | rndis_params *param = (rndis_params *) data; | ||
1305 | 1055 | ||
1306 | out += snprintf (out, count, | 1056 | seq_printf(m, |
1307 | "Config Nr. %d\n" | 1057 | "Config Nr. %d\n" |
1308 | "used : %s\n" | 1058 | "used : %s\n" |
1309 | "state : %s\n" | 1059 | "state : %s\n" |
@@ -1326,25 +1076,13 @@ static int rndis_proc_read (char *page, char **start, off_t off, int count, int | |||
1326 | (param->media_state) ? 0 : param->speed*100, | 1076 | (param->media_state) ? 0 : param->speed*100, |
1327 | (param->media_state) ? "disconnected" : "connected", | 1077 | (param->media_state) ? "disconnected" : "connected", |
1328 | param->vendorID, param->vendorDescr); | 1078 | param->vendorID, param->vendorDescr); |
1329 | 1079 | return 0; | |
1330 | len = out - page; | ||
1331 | len -= off; | ||
1332 | |||
1333 | if (len < count) { | ||
1334 | *eof = 1; | ||
1335 | if (len <= 0) | ||
1336 | return 0; | ||
1337 | } else | ||
1338 | len = count; | ||
1339 | |||
1340 | *start = page + off; | ||
1341 | return len; | ||
1342 | } | 1080 | } |
1343 | 1081 | ||
1344 | static int rndis_proc_write (struct file *file, const char __user *buffer, | 1082 | static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, |
1345 | unsigned long count, void *data) | 1083 | size_t count, loff_t *ppos) |
1346 | { | 1084 | { |
1347 | rndis_params *p = data; | 1085 | rndis_params *p = PDE(file->f_path.dentry->d_inode)->data; |
1348 | u32 speed = 0; | 1086 | u32 speed = 0; |
1349 | int i, fl_speed = 0; | 1087 | int i, fl_speed = 0; |
1350 | 1088 | ||
@@ -1386,6 +1124,20 @@ static int rndis_proc_write (struct file *file, const char __user *buffer, | |||
1386 | return count; | 1124 | return count; |
1387 | } | 1125 | } |
1388 | 1126 | ||
1127 | static int rndis_proc_open(struct inode *inode, struct file *file) | ||
1128 | { | ||
1129 | return single_open(file, rndis_proc_show, PDE(inode)->data); | ||
1130 | } | ||
1131 | |||
1132 | static const struct file_operations rndis_proc_fops = { | ||
1133 | .owner = THIS_MODULE, | ||
1134 | .open = rndis_proc_open, | ||
1135 | .read = seq_read, | ||
1136 | .llseek = seq_lseek, | ||
1137 | .release = single_release, | ||
1138 | .write = rndis_proc_write, | ||
1139 | }; | ||
1140 | |||
1389 | #define NAME_TEMPLATE "driver/rndis-%03d" | 1141 | #define NAME_TEMPLATE "driver/rndis-%03d" |
1390 | 1142 | ||
1391 | static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS]; | 1143 | static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS]; |
@@ -1403,7 +1155,9 @@ int __init rndis_init (void) | |||
1403 | 1155 | ||
1404 | sprintf (name, NAME_TEMPLATE, i); | 1156 | sprintf (name, NAME_TEMPLATE, i); |
1405 | if (!(rndis_connect_state [i] | 1157 | if (!(rndis_connect_state [i] |
1406 | = create_proc_entry (name, 0660, NULL))) | 1158 | = proc_create_data(name, 0660, NULL, |
1159 | &rndis_proc_fops, | ||
1160 | (void *)(rndis_per_dev_params + i)))) | ||
1407 | { | 1161 | { |
1408 | DBG("%s :remove entries", __func__); | 1162 | DBG("%s :remove entries", __func__); |
1409 | while (i) { | 1163 | while (i) { |
@@ -1413,11 +1167,6 @@ int __init rndis_init (void) | |||
1413 | DBG("\n"); | 1167 | DBG("\n"); |
1414 | return -EIO; | 1168 | return -EIO; |
1415 | } | 1169 | } |
1416 | |||
1417 | rndis_connect_state [i]->write_proc = rndis_proc_write; | ||
1418 | rndis_connect_state [i]->read_proc = rndis_proc_read; | ||
1419 | rndis_connect_state [i]->data = (void *) | ||
1420 | (rndis_per_dev_params + i); | ||
1421 | #endif | 1170 | #endif |
1422 | rndis_per_dev_params [i].confignr = i; | 1171 | rndis_per_dev_params [i].confignr = i; |
1423 | rndis_per_dev_params [i].used = 0; | 1172 | rndis_per_dev_params [i].used = 0; |
diff --git a/drivers/usb/gadget/rndis.h b/drivers/usb/gadget/rndis.h index 397b149f3ca7..aac61dfe0f03 100644 --- a/drivers/usb/gadget/rndis.h +++ b/drivers/usb/gadget/rndis.h | |||
@@ -1,8 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * RNDIS Definitions for Remote NDIS | 2 | * RNDIS Definitions for Remote NDIS |
3 | * | 3 | * |
4 | * Version: $Id: rndis.h,v 1.15 2004/03/25 21:33:46 robert Exp $ | ||
5 | * | ||
6 | * Authors: Benedikt Spranger, Pengutronix | 4 | * Authors: Benedikt Spranger, Pengutronix |
7 | * Robert Schwebel, Pengutronix | 5 | * Robert Schwebel, Pengutronix |
8 | * | 6 | * |
@@ -235,20 +233,19 @@ typedef struct rndis_params | |||
235 | const u8 *host_mac; | 233 | const u8 *host_mac; |
236 | u16 *filter; | 234 | u16 *filter; |
237 | struct net_device *dev; | 235 | struct net_device *dev; |
238 | struct net_device_stats *stats; | ||
239 | 236 | ||
240 | u32 vendorID; | 237 | u32 vendorID; |
241 | const char *vendorDescr; | 238 | const char *vendorDescr; |
242 | int (*ack) (struct net_device *); | 239 | void (*resp_avail)(void *v); |
240 | void *v; | ||
243 | struct list_head resp_queue; | 241 | struct list_head resp_queue; |
244 | } rndis_params; | 242 | } rndis_params; |
245 | 243 | ||
246 | /* RNDIS Message parser and other useless functions */ | 244 | /* RNDIS Message parser and other useless functions */ |
247 | int rndis_msg_parser (u8 configNr, u8 *buf); | 245 | int rndis_msg_parser (u8 configNr, u8 *buf); |
248 | int rndis_register (int (*rndis_control_ack) (struct net_device *)); | 246 | int rndis_register(void (*resp_avail)(void *v), void *v); |
249 | void rndis_deregister (int configNr); | 247 | void rndis_deregister (int configNr); |
250 | int rndis_set_param_dev (u8 configNr, struct net_device *dev, | 248 | int rndis_set_param_dev (u8 configNr, struct net_device *dev, |
251 | struct net_device_stats *stats, | ||
252 | u16 *cdc_filter); | 249 | u16 *cdc_filter); |
253 | int rndis_set_param_vendor (u8 configNr, u32 vendorID, | 250 | int rndis_set_param_vendor (u8 configNr, u32 vendorID, |
254 | const char *vendorDescr); | 251 | const char *vendorDescr); |
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c index fa019fa73334..b3699afff002 100644 --- a/drivers/usb/gadget/serial.c +++ b/drivers/usb/gadget/serial.c | |||
@@ -1,15 +1,9 @@ | |||
1 | /* | 1 | /* |
2 | * g_serial.c -- USB gadget serial driver | 2 | * serial.c -- USB gadget serial driver |
3 | * | 3 | * |
4 | * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com) | 4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) |
5 | * | 5 | * Copyright (C) 2008 by David Brownell |
6 | * This code is based in part on the Gadget Zero driver, which | 6 | * Copyright (C) 2008 by Nokia Corporation |
7 | * is Copyright (C) 2003 by David Brownell, all rights reserved. | ||
8 | * | ||
9 | * This code also borrows from usbserial.c, which is | ||
10 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | ||
11 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) | ||
12 | * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) | ||
13 | * | 7 | * |
14 | * This software is distributed under the terms of the GNU General | 8 | * This software is distributed under the terms of the GNU General |
15 | * Public License ("GPL") as published by the Free Software Foundation, | 9 | * Public License ("GPL") as published by the Free Software Foundation, |
@@ -22,2254 +16,237 @@ | |||
22 | #include <linux/tty.h> | 16 | #include <linux/tty.h> |
23 | #include <linux/tty_flip.h> | 17 | #include <linux/tty_flip.h> |
24 | 18 | ||
25 | #include <linux/usb/ch9.h> | 19 | #include "u_serial.h" |
26 | #include <linux/usb/cdc.h> | ||
27 | #include <linux/usb/gadget.h> | ||
28 | |||
29 | #include "gadget_chips.h" | 20 | #include "gadget_chips.h" |
30 | 21 | ||
31 | 22 | ||
32 | /* Defines */ | 23 | /* Defines */ |
33 | 24 | ||
34 | #define GS_VERSION_STR "v2.2" | 25 | #define GS_VERSION_STR "v2.4" |
35 | #define GS_VERSION_NUM 0x2200 | 26 | #define GS_VERSION_NUM 0x2400 |
36 | 27 | ||
37 | #define GS_LONG_NAME "Gadget Serial" | 28 | #define GS_LONG_NAME "Gadget Serial" |
38 | #define GS_SHORT_NAME "g_serial" | 29 | #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR |
39 | |||
40 | #define GS_MAJOR 127 | ||
41 | #define GS_MINOR_START 0 | ||
42 | |||
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 | ||
48 | |||
49 | #define GS_NUM_CONFIGS 1 | ||
50 | #define GS_NO_CONFIG_ID 0 | ||
51 | #define GS_BULK_CONFIG_ID 1 | ||
52 | #define GS_ACM_CONFIG_ID 2 | ||
53 | |||
54 | #define GS_MAX_NUM_INTERFACES 2 | ||
55 | #define GS_BULK_INTERFACE_ID 0 | ||
56 | #define GS_CONTROL_INTERFACE_ID 0 | ||
57 | #define GS_DATA_INTERFACE_ID 1 | ||
58 | |||
59 | #define GS_MAX_DESC_LEN 256 | ||
60 | |||
61 | #define GS_DEFAULT_READ_Q_SIZE 32 | ||
62 | #define GS_DEFAULT_WRITE_Q_SIZE 32 | ||
63 | |||
64 | #define GS_DEFAULT_WRITE_BUF_SIZE 8192 | ||
65 | #define GS_TMP_BUF_SIZE 8192 | ||
66 | |||
67 | #define GS_CLOSE_TIMEOUT 15 | ||
68 | |||
69 | #define GS_DEFAULT_USE_ACM 0 | ||
70 | |||
71 | /* 9600-8-N-1 ... matches init_termios.c_cflag and defaults | ||
72 | * expected by "usbser.sys" on MS-Windows. | ||
73 | */ | ||
74 | #define GS_DEFAULT_DTE_RATE 9600 | ||
75 | #define GS_DEFAULT_DATA_BITS 8 | ||
76 | #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY | ||
77 | #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS | ||
78 | |||
79 | /* maxpacket and other transfer characteristics vary by speed. */ | ||
80 | static inline struct usb_endpoint_descriptor * | ||
81 | choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | ||
82 | struct usb_endpoint_descriptor *fs) | ||
83 | { | ||
84 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
85 | return hs; | ||
86 | return fs; | ||
87 | } | ||
88 | |||
89 | |||
90 | /* debug settings */ | ||
91 | #ifdef DEBUG | ||
92 | static int debug = 1; | ||
93 | #else | ||
94 | #define debug 0 | ||
95 | #endif | ||
96 | |||
97 | #define gs_debug(format, arg...) \ | ||
98 | do { if (debug) pr_debug(format, ## arg); } while (0) | ||
99 | #define gs_debug_level(level, format, arg...) \ | ||
100 | do { if (debug >= level) pr_debug(format, ## arg); } while (0) | ||
101 | 30 | ||
31 | /*-------------------------------------------------------------------------*/ | ||
102 | 32 | ||
103 | /* Thanks to NetChip Technologies for donating this product ID. | 33 | /* Thanks to NetChip Technologies for donating this product ID. |
104 | * | 34 | * |
105 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | 35 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
106 | * Instead: allocate your own, using normal USB-IF procedures. | 36 | * Instead: allocate your own, using normal USB-IF procedures. |
107 | */ | 37 | */ |
108 | #define GS_VENDOR_ID 0x0525 /* NetChip */ | 38 | #define GS_VENDOR_ID 0x0525 /* NetChip */ |
109 | #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ | 39 | #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ |
110 | #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ | 40 | #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ |
111 | 41 | ||
112 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ | 42 | /* string IDs are assigned dynamically */ |
113 | #define GS_NOTIFY_MAXPACKET 8 | ||
114 | 43 | ||
44 | #define STRING_MANUFACTURER_IDX 0 | ||
45 | #define STRING_PRODUCT_IDX 1 | ||
46 | #define STRING_DESCRIPTION_IDX 2 | ||
115 | 47 | ||
116 | /* circular buffer */ | 48 | static char manufacturer[50]; |
117 | struct gs_buf { | ||
118 | unsigned int buf_size; | ||
119 | char *buf_buf; | ||
120 | char *buf_get; | ||
121 | char *buf_put; | ||
122 | }; | ||
123 | 49 | ||
124 | /* the port structure holds info for each port, one for each minor number */ | 50 | static struct usb_string strings_dev[] = { |
125 | struct gs_port { | 51 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
126 | struct gs_dev *port_dev; /* pointer to device struct */ | 52 | [STRING_PRODUCT_IDX].s = GS_VERSION_NAME, |
127 | struct tty_struct *port_tty; /* pointer to tty struct */ | 53 | [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, |
128 | spinlock_t port_lock; | 54 | { } /* end of list */ |
129 | int port_num; | ||
130 | int port_open_count; | ||
131 | int port_in_use; /* open/close in progress */ | ||
132 | wait_queue_head_t port_write_wait;/* waiting to write */ | ||
133 | struct gs_buf *port_write_buf; | ||
134 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ | ||
135 | u16 port_handshake_bits; | ||
136 | #define RS232_RTS (1 << 1) | ||
137 | #define RS232_DTE (1 << 0) | ||
138 | }; | 55 | }; |
139 | 56 | ||
140 | /* the device structure holds info for the USB device */ | 57 | static struct usb_gadget_strings stringtab_dev = { |
141 | struct gs_dev { | 58 | .language = 0x0409, /* en-us */ |
142 | struct usb_gadget *dev_gadget; /* gadget device pointer */ | 59 | .strings = strings_dev, |
143 | spinlock_t dev_lock; /* lock for set/reset config */ | ||
144 | int dev_config; /* configuration number */ | ||
145 | struct usb_ep *dev_notify_ep; /* address of notify endpoint */ | ||
146 | struct usb_ep *dev_in_ep; /* address of in endpoint */ | ||
147 | struct usb_ep *dev_out_ep; /* address of out endpoint */ | ||
148 | struct usb_endpoint_descriptor /* descriptor of notify ep */ | ||
149 | *dev_notify_ep_desc; | ||
150 | struct usb_endpoint_descriptor /* descriptor of in endpoint */ | ||
151 | *dev_in_ep_desc; | ||
152 | struct usb_endpoint_descriptor /* descriptor of out endpoint */ | ||
153 | *dev_out_ep_desc; | ||
154 | struct usb_request *dev_ctrl_req; /* control request */ | ||
155 | struct list_head dev_req_list; /* list of write requests */ | ||
156 | int dev_sched_port; /* round robin port scheduled */ | ||
157 | struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */ | ||
158 | }; | 60 | }; |
159 | 61 | ||
160 | 62 | static struct usb_gadget_strings *dev_strings[] = { | |
161 | /* Functions */ | 63 | &stringtab_dev, |
162 | 64 | NULL, | |
163 | /* tty driver internals */ | ||
164 | static int gs_send(struct gs_dev *dev); | ||
165 | static int gs_send_packet(struct gs_dev *dev, char *packet, | ||
166 | unsigned int size); | ||
167 | static int gs_recv_packet(struct gs_dev *dev, char *packet, | ||
168 | unsigned int size); | ||
169 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req); | ||
170 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req); | ||
171 | |||
172 | /* gadget driver internals */ | ||
173 | static int gs_set_config(struct gs_dev *dev, unsigned config); | ||
174 | static void gs_reset_config(struct gs_dev *dev); | ||
175 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, | ||
176 | u8 type, unsigned int index, int is_otg); | ||
177 | |||
178 | static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, | ||
179 | gfp_t kmalloc_flags); | ||
180 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req); | ||
181 | |||
182 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags); | ||
183 | static void gs_free_ports(struct gs_dev *dev); | ||
184 | |||
185 | /* circular buffer */ | ||
186 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags); | ||
187 | static void gs_buf_free(struct gs_buf *gb); | ||
188 | static void gs_buf_clear(struct gs_buf *gb); | ||
189 | static unsigned int gs_buf_data_avail(struct gs_buf *gb); | ||
190 | static unsigned int gs_buf_space_avail(struct gs_buf *gb); | ||
191 | static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, | ||
192 | unsigned int count); | ||
193 | static unsigned int gs_buf_get(struct gs_buf *gb, char *buf, | ||
194 | unsigned int count); | ||
195 | |||
196 | |||
197 | /* Globals */ | ||
198 | |||
199 | static struct gs_dev *gs_device; | ||
200 | |||
201 | static struct mutex gs_open_close_lock[GS_NUM_PORTS]; | ||
202 | |||
203 | |||
204 | /*-------------------------------------------------------------------------*/ | ||
205 | |||
206 | /* USB descriptors */ | ||
207 | |||
208 | #define GS_MANUFACTURER_STR_ID 1 | ||
209 | #define GS_PRODUCT_STR_ID 2 | ||
210 | #define GS_SERIAL_STR_ID 3 | ||
211 | #define GS_BULK_CONFIG_STR_ID 4 | ||
212 | #define GS_ACM_CONFIG_STR_ID 5 | ||
213 | #define GS_CONTROL_STR_ID 6 | ||
214 | #define GS_DATA_STR_ID 7 | ||
215 | |||
216 | /* static strings, in UTF-8 */ | ||
217 | static char manufacturer[50]; | ||
218 | static struct usb_string gs_strings[] = { | ||
219 | { GS_MANUFACTURER_STR_ID, manufacturer }, | ||
220 | { GS_PRODUCT_STR_ID, GS_LONG_NAME }, | ||
221 | { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, | ||
222 | { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, | ||
223 | { GS_CONTROL_STR_ID, "Gadget Serial Control" }, | ||
224 | { GS_DATA_STR_ID, "Gadget Serial Data" }, | ||
225 | { } /* end of list */ | ||
226 | }; | ||
227 | |||
228 | static struct usb_gadget_strings gs_string_table = { | ||
229 | .language = 0x0409, /* en-us */ | ||
230 | .strings = gs_strings, | ||
231 | }; | 65 | }; |
232 | 66 | ||
233 | static struct usb_device_descriptor gs_device_desc = { | 67 | static struct usb_device_descriptor device_desc = { |
234 | .bLength = USB_DT_DEVICE_SIZE, | 68 | .bLength = USB_DT_DEVICE_SIZE, |
235 | .bDescriptorType = USB_DT_DEVICE, | 69 | .bDescriptorType = USB_DT_DEVICE, |
236 | .bcdUSB = __constant_cpu_to_le16(0x0200), | 70 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
71 | /* .bDeviceClass = f(use_acm) */ | ||
237 | .bDeviceSubClass = 0, | 72 | .bDeviceSubClass = 0, |
238 | .bDeviceProtocol = 0, | 73 | .bDeviceProtocol = 0, |
74 | /* .bMaxPacketSize0 = f(hardware) */ | ||
239 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), | 75 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), |
240 | .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID), | 76 | /* .idProduct = f(use_acm) */ |
241 | .iManufacturer = GS_MANUFACTURER_STR_ID, | 77 | /* .bcdDevice = f(hardware) */ |
242 | .iProduct = GS_PRODUCT_STR_ID, | 78 | /* .iManufacturer = DYNAMIC */ |
243 | .bNumConfigurations = GS_NUM_CONFIGS, | 79 | /* .iProduct = DYNAMIC */ |
80 | .bNumConfigurations = 1, | ||
244 | }; | 81 | }; |
245 | 82 | ||
246 | static struct usb_otg_descriptor gs_otg_descriptor = { | 83 | static struct usb_otg_descriptor otg_descriptor = { |
247 | .bLength = sizeof(gs_otg_descriptor), | 84 | .bLength = sizeof otg_descriptor, |
248 | .bDescriptorType = USB_DT_OTG, | 85 | .bDescriptorType = USB_DT_OTG, |
249 | .bmAttributes = USB_OTG_SRP, | ||
250 | }; | ||
251 | |||
252 | static struct usb_config_descriptor gs_bulk_config_desc = { | ||
253 | .bLength = USB_DT_CONFIG_SIZE, | ||
254 | .bDescriptorType = USB_DT_CONFIG, | ||
255 | /* .wTotalLength computed dynamically */ | ||
256 | .bNumInterfaces = 1, | ||
257 | .bConfigurationValue = GS_BULK_CONFIG_ID, | ||
258 | .iConfiguration = GS_BULK_CONFIG_STR_ID, | ||
259 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
260 | .bMaxPower = 1, | ||
261 | }; | ||
262 | |||
263 | static struct usb_config_descriptor gs_acm_config_desc = { | ||
264 | .bLength = USB_DT_CONFIG_SIZE, | ||
265 | .bDescriptorType = USB_DT_CONFIG, | ||
266 | /* .wTotalLength computed dynamically */ | ||
267 | .bNumInterfaces = 2, | ||
268 | .bConfigurationValue = GS_ACM_CONFIG_ID, | ||
269 | .iConfiguration = GS_ACM_CONFIG_STR_ID, | ||
270 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
271 | .bMaxPower = 1, | ||
272 | }; | ||
273 | |||
274 | static const struct usb_interface_descriptor gs_bulk_interface_desc = { | ||
275 | .bLength = USB_DT_INTERFACE_SIZE, | ||
276 | .bDescriptorType = USB_DT_INTERFACE, | ||
277 | .bInterfaceNumber = GS_BULK_INTERFACE_ID, | ||
278 | .bNumEndpoints = 2, | ||
279 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
280 | .bInterfaceSubClass = 0, | ||
281 | .bInterfaceProtocol = 0, | ||
282 | .iInterface = GS_DATA_STR_ID, | ||
283 | }; | ||
284 | |||
285 | static const struct usb_interface_descriptor gs_control_interface_desc = { | ||
286 | .bLength = USB_DT_INTERFACE_SIZE, | ||
287 | .bDescriptorType = USB_DT_INTERFACE, | ||
288 | .bInterfaceNumber = GS_CONTROL_INTERFACE_ID, | ||
289 | .bNumEndpoints = 1, | ||
290 | .bInterfaceClass = USB_CLASS_COMM, | ||
291 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | ||
292 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, | ||
293 | .iInterface = GS_CONTROL_STR_ID, | ||
294 | }; | ||
295 | |||
296 | static const struct usb_interface_descriptor gs_data_interface_desc = { | ||
297 | .bLength = USB_DT_INTERFACE_SIZE, | ||
298 | .bDescriptorType = USB_DT_INTERFACE, | ||
299 | .bInterfaceNumber = GS_DATA_INTERFACE_ID, | ||
300 | .bNumEndpoints = 2, | ||
301 | .bInterfaceClass = USB_CLASS_CDC_DATA, | ||
302 | .bInterfaceSubClass = 0, | ||
303 | .bInterfaceProtocol = 0, | ||
304 | .iInterface = GS_DATA_STR_ID, | ||
305 | }; | ||
306 | |||
307 | static const struct usb_cdc_header_desc gs_header_desc = { | ||
308 | .bLength = sizeof(gs_header_desc), | ||
309 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
310 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | ||
311 | .bcdCDC = __constant_cpu_to_le16(0x0110), | ||
312 | }; | ||
313 | |||
314 | static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = { | ||
315 | .bLength = sizeof(gs_call_mgmt_descriptor), | ||
316 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
317 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | ||
318 | .bmCapabilities = 0, | ||
319 | .bDataInterface = 1, /* index of data interface */ | ||
320 | }; | ||
321 | |||
322 | static struct usb_cdc_acm_descriptor gs_acm_descriptor = { | ||
323 | .bLength = sizeof(gs_acm_descriptor), | ||
324 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
325 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | ||
326 | .bmCapabilities = (1 << 1), | ||
327 | }; | ||
328 | |||
329 | static const struct usb_cdc_union_desc gs_union_desc = { | ||
330 | .bLength = sizeof(gs_union_desc), | ||
331 | .bDescriptorType = USB_DT_CS_INTERFACE, | ||
332 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | ||
333 | .bMasterInterface0 = 0, /* index of control interface */ | ||
334 | .bSlaveInterface0 = 1, /* index of data interface */ | ||
335 | }; | ||
336 | |||
337 | static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = { | ||
338 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
339 | .bDescriptorType = USB_DT_ENDPOINT, | ||
340 | .bEndpointAddress = USB_DIR_IN, | ||
341 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
342 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), | ||
343 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, | ||
344 | }; | ||
345 | |||
346 | static struct usb_endpoint_descriptor gs_fullspeed_in_desc = { | ||
347 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
348 | .bDescriptorType = USB_DT_ENDPOINT, | ||
349 | .bEndpointAddress = USB_DIR_IN, | ||
350 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
351 | }; | ||
352 | |||
353 | static struct usb_endpoint_descriptor gs_fullspeed_out_desc = { | ||
354 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
355 | .bDescriptorType = USB_DT_ENDPOINT, | ||
356 | .bEndpointAddress = USB_DIR_OUT, | ||
357 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
358 | }; | ||
359 | |||
360 | static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = { | ||
361 | (struct usb_descriptor_header *) &gs_otg_descriptor, | ||
362 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, | ||
363 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, | ||
364 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, | ||
365 | NULL, | ||
366 | }; | ||
367 | |||
368 | static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = { | ||
369 | (struct usb_descriptor_header *) &gs_otg_descriptor, | ||
370 | (struct usb_descriptor_header *) &gs_control_interface_desc, | ||
371 | (struct usb_descriptor_header *) &gs_header_desc, | ||
372 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, | ||
373 | (struct usb_descriptor_header *) &gs_acm_descriptor, | ||
374 | (struct usb_descriptor_header *) &gs_union_desc, | ||
375 | (struct usb_descriptor_header *) &gs_fullspeed_notify_desc, | ||
376 | (struct usb_descriptor_header *) &gs_data_interface_desc, | ||
377 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, | ||
378 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, | ||
379 | NULL, | ||
380 | }; | ||
381 | 86 | ||
382 | static struct usb_endpoint_descriptor gs_highspeed_notify_desc = { | 87 | /* REVISIT SRP-only hardware is possible, although |
383 | .bLength = USB_DT_ENDPOINT_SIZE, | 88 | * it would not be called "OTG" ... |
384 | .bDescriptorType = USB_DT_ENDPOINT, | 89 | */ |
385 | .bEndpointAddress = USB_DIR_IN, | 90 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
386 | .bmAttributes = USB_ENDPOINT_XFER_INT, | ||
387 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), | ||
388 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, | ||
389 | }; | ||
390 | |||
391 | static struct usb_endpoint_descriptor gs_highspeed_in_desc = { | ||
392 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
393 | .bDescriptorType = USB_DT_ENDPOINT, | ||
394 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
395 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
396 | }; | ||
397 | |||
398 | static struct usb_endpoint_descriptor gs_highspeed_out_desc = { | ||
399 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
400 | .bDescriptorType = USB_DT_ENDPOINT, | ||
401 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
402 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
403 | }; | ||
404 | |||
405 | static struct usb_qualifier_descriptor gs_qualifier_desc = { | ||
406 | .bLength = sizeof(struct usb_qualifier_descriptor), | ||
407 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | ||
408 | .bcdUSB = __constant_cpu_to_le16 (0x0200), | ||
409 | /* assumes ep0 uses the same value for both speeds ... */ | ||
410 | .bNumConfigurations = GS_NUM_CONFIGS, | ||
411 | }; | ||
412 | |||
413 | static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = { | ||
414 | (struct usb_descriptor_header *) &gs_otg_descriptor, | ||
415 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, | ||
416 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, | ||
417 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, | ||
418 | NULL, | ||
419 | }; | 91 | }; |
420 | 92 | ||
421 | static const struct usb_descriptor_header *gs_acm_highspeed_function[] = { | 93 | static const struct usb_descriptor_header *otg_desc[] = { |
422 | (struct usb_descriptor_header *) &gs_otg_descriptor, | 94 | (struct usb_descriptor_header *) &otg_descriptor, |
423 | (struct usb_descriptor_header *) &gs_control_interface_desc, | ||
424 | (struct usb_descriptor_header *) &gs_header_desc, | ||
425 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, | ||
426 | (struct usb_descriptor_header *) &gs_acm_descriptor, | ||
427 | (struct usb_descriptor_header *) &gs_union_desc, | ||
428 | (struct usb_descriptor_header *) &gs_highspeed_notify_desc, | ||
429 | (struct usb_descriptor_header *) &gs_data_interface_desc, | ||
430 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, | ||
431 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, | ||
432 | NULL, | 95 | NULL, |
433 | }; | 96 | }; |
434 | 97 | ||
435 | |||
436 | /*-------------------------------------------------------------------------*/ | 98 | /*-------------------------------------------------------------------------*/ |
437 | 99 | ||
438 | /* Module */ | 100 | /* Module */ |
439 | MODULE_DESCRIPTION(GS_LONG_NAME); | 101 | MODULE_DESCRIPTION(GS_VERSION_NAME); |
440 | MODULE_AUTHOR("Al Borchers"); | 102 | MODULE_AUTHOR("Al Borchers"); |
103 | MODULE_AUTHOR("David Brownell"); | ||
441 | MODULE_LICENSE("GPL"); | 104 | MODULE_LICENSE("GPL"); |
442 | 105 | ||
443 | #ifdef DEBUG | 106 | static int use_acm = true; |
444 | module_param(debug, int, S_IRUGO|S_IWUSR); | 107 | module_param(use_acm, bool, 0); |
445 | MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); | 108 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); |
446 | #endif | ||
447 | |||
448 | static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE; | ||
449 | module_param(read_q_size, uint, S_IRUGO); | ||
450 | MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32"); | ||
451 | |||
452 | static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE; | ||
453 | module_param(write_q_size, uint, S_IRUGO); | ||
454 | MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32"); | ||
455 | 109 | ||
456 | static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE; | 110 | static unsigned n_ports = 1; |
457 | module_param(write_buf_size, uint, S_IRUGO); | 111 | module_param(n_ports, uint, 0); |
458 | MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); | 112 | MODULE_PARM_DESC(n_ports, "number of ports to create, default=1"); |
459 | |||
460 | static unsigned int use_acm = GS_DEFAULT_USE_ACM; | ||
461 | module_param(use_acm, uint, S_IRUGO); | ||
462 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); | ||
463 | 113 | ||
464 | /*-------------------------------------------------------------------------*/ | 114 | /*-------------------------------------------------------------------------*/ |
465 | 115 | ||
466 | /* TTY Driver */ | 116 | static int __init serial_bind_config(struct usb_configuration *c) |
467 | |||
468 | /* | ||
469 | * gs_open | ||
470 | */ | ||
471 | static int gs_open(struct tty_struct *tty, struct file *file) | ||
472 | { | ||
473 | int port_num; | ||
474 | unsigned long flags; | ||
475 | struct gs_port *port; | ||
476 | struct gs_dev *dev; | ||
477 | struct gs_buf *buf; | ||
478 | struct mutex *mtx; | ||
479 | int ret; | ||
480 | |||
481 | port_num = tty->index; | ||
482 | |||
483 | gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file); | ||
484 | |||
485 | if (port_num < 0 || port_num >= GS_NUM_PORTS) { | ||
486 | pr_err("gs_open: (%d,%p,%p) invalid port number\n", | ||
487 | port_num, tty, file); | ||
488 | return -ENODEV; | ||
489 | } | ||
490 | |||
491 | dev = gs_device; | ||
492 | |||
493 | if (dev == NULL) { | ||
494 | pr_err("gs_open: (%d,%p,%p) NULL device pointer\n", | ||
495 | port_num, tty, file); | ||
496 | return -ENODEV; | ||
497 | } | ||
498 | |||
499 | mtx = &gs_open_close_lock[port_num]; | ||
500 | if (mutex_lock_interruptible(mtx)) { | ||
501 | pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n", | ||
502 | port_num, tty, file); | ||
503 | return -ERESTARTSYS; | ||
504 | } | ||
505 | |||
506 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
507 | |||
508 | if (dev->dev_config == GS_NO_CONFIG_ID) { | ||
509 | pr_err("gs_open: (%d,%p,%p) device is not connected\n", | ||
510 | port_num, tty, file); | ||
511 | ret = -ENODEV; | ||
512 | goto exit_unlock_dev; | ||
513 | } | ||
514 | |||
515 | port = dev->dev_port[port_num]; | ||
516 | |||
517 | if (port == NULL) { | ||
518 | pr_err("gs_open: (%d,%p,%p) NULL port pointer\n", | ||
519 | port_num, tty, file); | ||
520 | ret = -ENODEV; | ||
521 | goto exit_unlock_dev; | ||
522 | } | ||
523 | |||
524 | spin_lock(&port->port_lock); | ||
525 | spin_unlock(&dev->dev_lock); | ||
526 | |||
527 | if (port->port_dev == NULL) { | ||
528 | pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n", | ||
529 | port_num, tty, file); | ||
530 | ret = -EIO; | ||
531 | goto exit_unlock_port; | ||
532 | } | ||
533 | |||
534 | if (port->port_open_count > 0) { | ||
535 | ++port->port_open_count; | ||
536 | gs_debug("gs_open: (%d,%p,%p) already open\n", | ||
537 | port_num, tty, file); | ||
538 | ret = 0; | ||
539 | goto exit_unlock_port; | ||
540 | } | ||
541 | |||
542 | tty->driver_data = NULL; | ||
543 | |||
544 | /* mark port as in use, we can drop port lock and sleep if necessary */ | ||
545 | port->port_in_use = 1; | ||
546 | |||
547 | /* allocate write buffer on first open */ | ||
548 | if (port->port_write_buf == NULL) { | ||
549 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
550 | buf = gs_buf_alloc(write_buf_size, GFP_KERNEL); | ||
551 | spin_lock_irqsave(&port->port_lock, flags); | ||
552 | |||
553 | /* might have been disconnected while asleep, check */ | ||
554 | if (port->port_dev == NULL) { | ||
555 | pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n", | ||
556 | port_num, tty, file); | ||
557 | port->port_in_use = 0; | ||
558 | ret = -EIO; | ||
559 | goto exit_unlock_port; | ||
560 | } | ||
561 | |||
562 | if ((port->port_write_buf=buf) == NULL) { | ||
563 | pr_err("gs_open: (%d,%p,%p) cannot allocate " | ||
564 | "port write buffer\n", | ||
565 | port_num, tty, file); | ||
566 | port->port_in_use = 0; | ||
567 | ret = -ENOMEM; | ||
568 | goto exit_unlock_port; | ||
569 | } | ||
570 | |||
571 | } | ||
572 | |||
573 | /* wait for carrier detect (not implemented) */ | ||
574 | |||
575 | /* might have been disconnected while asleep, check */ | ||
576 | if (port->port_dev == NULL) { | ||
577 | pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n", | ||
578 | port_num, tty, file); | ||
579 | port->port_in_use = 0; | ||
580 | ret = -EIO; | ||
581 | goto exit_unlock_port; | ||
582 | } | ||
583 | |||
584 | tty->driver_data = port; | ||
585 | port->port_tty = tty; | ||
586 | port->port_open_count = 1; | ||
587 | port->port_in_use = 0; | ||
588 | |||
589 | gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file); | ||
590 | |||
591 | ret = 0; | ||
592 | |||
593 | exit_unlock_port: | ||
594 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
595 | mutex_unlock(mtx); | ||
596 | return ret; | ||
597 | |||
598 | exit_unlock_dev: | ||
599 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
600 | mutex_unlock(mtx); | ||
601 | return ret; | ||
602 | |||
603 | } | ||
604 | |||
605 | /* | ||
606 | * gs_close | ||
607 | */ | ||
608 | |||
609 | static int gs_write_finished_event_safely(struct gs_port *p) | ||
610 | { | ||
611 | int cond; | ||
612 | |||
613 | spin_lock_irq(&(p)->port_lock); | ||
614 | cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); | ||
615 | spin_unlock_irq(&(p)->port_lock); | ||
616 | return cond; | ||
617 | } | ||
618 | |||
619 | static void gs_close(struct tty_struct *tty, struct file *file) | ||
620 | { | ||
621 | struct gs_port *port = tty->driver_data; | ||
622 | struct mutex *mtx; | ||
623 | |||
624 | if (port == NULL) { | ||
625 | pr_err("gs_close: NULL port pointer\n"); | ||
626 | return; | ||
627 | } | ||
628 | |||
629 | gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file); | ||
630 | |||
631 | mtx = &gs_open_close_lock[port->port_num]; | ||
632 | mutex_lock(mtx); | ||
633 | |||
634 | spin_lock_irq(&port->port_lock); | ||
635 | |||
636 | if (port->port_open_count == 0) { | ||
637 | pr_err("gs_close: (%d,%p,%p) port is already closed\n", | ||
638 | port->port_num, tty, file); | ||
639 | goto exit; | ||
640 | } | ||
641 | |||
642 | if (port->port_open_count > 1) { | ||
643 | --port->port_open_count; | ||
644 | goto exit; | ||
645 | } | ||
646 | |||
647 | /* free disconnected port on final close */ | ||
648 | if (port->port_dev == NULL) { | ||
649 | kfree(port); | ||
650 | goto exit; | ||
651 | } | ||
652 | |||
653 | /* mark port as closed but in use, we can drop port lock */ | ||
654 | /* and sleep if necessary */ | ||
655 | port->port_in_use = 1; | ||
656 | port->port_open_count = 0; | ||
657 | |||
658 | /* wait for write buffer to drain, or */ | ||
659 | /* at most GS_CLOSE_TIMEOUT seconds */ | ||
660 | if (gs_buf_data_avail(port->port_write_buf) > 0) { | ||
661 | spin_unlock_irq(&port->port_lock); | ||
662 | wait_event_interruptible_timeout(port->port_write_wait, | ||
663 | gs_write_finished_event_safely(port), | ||
664 | GS_CLOSE_TIMEOUT * HZ); | ||
665 | spin_lock_irq(&port->port_lock); | ||
666 | } | ||
667 | |||
668 | /* free disconnected port on final close */ | ||
669 | /* (might have happened during the above sleep) */ | ||
670 | if (port->port_dev == NULL) { | ||
671 | kfree(port); | ||
672 | goto exit; | ||
673 | } | ||
674 | |||
675 | gs_buf_clear(port->port_write_buf); | ||
676 | |||
677 | tty->driver_data = NULL; | ||
678 | port->port_tty = NULL; | ||
679 | port->port_in_use = 0; | ||
680 | |||
681 | gs_debug("gs_close: (%d,%p,%p) completed\n", | ||
682 | port->port_num, tty, file); | ||
683 | |||
684 | exit: | ||
685 | spin_unlock_irq(&port->port_lock); | ||
686 | mutex_unlock(mtx); | ||
687 | } | ||
688 | |||
689 | /* | ||
690 | * gs_write | ||
691 | */ | ||
692 | static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
693 | { | 117 | { |
694 | unsigned long flags; | 118 | unsigned i; |
695 | struct gs_port *port = tty->driver_data; | 119 | int status = 0; |
696 | int ret; | ||
697 | |||
698 | if (port == NULL) { | ||
699 | pr_err("gs_write: NULL port pointer\n"); | ||
700 | return -EIO; | ||
701 | } | ||
702 | |||
703 | gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty, | ||
704 | count); | ||
705 | |||
706 | if (count == 0) | ||
707 | return 0; | ||
708 | |||
709 | spin_lock_irqsave(&port->port_lock, flags); | ||
710 | |||
711 | if (port->port_dev == NULL) { | ||
712 | pr_err("gs_write: (%d,%p) port is not connected\n", | ||
713 | port->port_num, tty); | ||
714 | ret = -EIO; | ||
715 | goto exit; | ||
716 | } | ||
717 | |||
718 | if (port->port_open_count == 0) { | ||
719 | pr_err("gs_write: (%d,%p) port is closed\n", | ||
720 | port->port_num, tty); | ||
721 | ret = -EBADF; | ||
722 | goto exit; | ||
723 | } | ||
724 | |||
725 | count = gs_buf_put(port->port_write_buf, buf, count); | ||
726 | |||
727 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
728 | |||
729 | gs_send(gs_device); | ||
730 | |||
731 | gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty, | ||
732 | count); | ||
733 | 120 | ||
734 | return count; | 121 | for (i = 0; i < n_ports && status == 0; i++) { |
735 | 122 | if (use_acm) | |
736 | exit: | 123 | status = acm_bind_config(c, i); |
737 | spin_unlock_irqrestore(&port->port_lock, flags); | 124 | else |
738 | return ret; | 125 | status = gser_bind_config(c, i); |
739 | } | ||
740 | |||
741 | /* | ||
742 | * gs_put_char | ||
743 | */ | ||
744 | static int gs_put_char(struct tty_struct *tty, unsigned char ch) | ||
745 | { | ||
746 | unsigned long flags; | ||
747 | struct gs_port *port = tty->driver_data; | ||
748 | int ret = 0; | ||
749 | |||
750 | if (port == NULL) { | ||
751 | pr_err("gs_put_char: NULL port pointer\n"); | ||
752 | return 0; | ||
753 | } | ||
754 | |||
755 | gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n", | ||
756 | port->port_num, tty, ch, __builtin_return_address(0)); | ||
757 | |||
758 | spin_lock_irqsave(&port->port_lock, flags); | ||
759 | |||
760 | if (port->port_dev == NULL) { | ||
761 | pr_err("gs_put_char: (%d,%p) port is not connected\n", | ||
762 | port->port_num, tty); | ||
763 | goto exit; | ||
764 | } | ||
765 | |||
766 | if (port->port_open_count == 0) { | ||
767 | pr_err("gs_put_char: (%d,%p) port is closed\n", | ||
768 | port->port_num, tty); | ||
769 | goto exit; | ||
770 | } | ||
771 | |||
772 | ret = gs_buf_put(port->port_write_buf, &ch, 1); | ||
773 | |||
774 | exit: | ||
775 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
776 | return ret; | ||
777 | } | ||
778 | |||
779 | /* | ||
780 | * gs_flush_chars | ||
781 | */ | ||
782 | static void gs_flush_chars(struct tty_struct *tty) | ||
783 | { | ||
784 | unsigned long flags; | ||
785 | struct gs_port *port = tty->driver_data; | ||
786 | |||
787 | if (port == NULL) { | ||
788 | pr_err("gs_flush_chars: NULL port pointer\n"); | ||
789 | return; | ||
790 | } | ||
791 | |||
792 | gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); | ||
793 | |||
794 | spin_lock_irqsave(&port->port_lock, flags); | ||
795 | |||
796 | if (port->port_dev == NULL) { | ||
797 | pr_err("gs_flush_chars: (%d,%p) port is not connected\n", | ||
798 | port->port_num, tty); | ||
799 | goto exit; | ||
800 | } | ||
801 | |||
802 | if (port->port_open_count == 0) { | ||
803 | pr_err("gs_flush_chars: (%d,%p) port is closed\n", | ||
804 | port->port_num, tty); | ||
805 | goto exit; | ||
806 | } | ||
807 | |||
808 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
809 | |||
810 | gs_send(gs_device); | ||
811 | |||
812 | return; | ||
813 | |||
814 | exit: | ||
815 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
816 | } | ||
817 | |||
818 | /* | ||
819 | * gs_write_room | ||
820 | */ | ||
821 | static int gs_write_room(struct tty_struct *tty) | ||
822 | { | ||
823 | |||
824 | int room = 0; | ||
825 | unsigned long flags; | ||
826 | struct gs_port *port = tty->driver_data; | ||
827 | |||
828 | |||
829 | if (port == NULL) | ||
830 | return 0; | ||
831 | |||
832 | spin_lock_irqsave(&port->port_lock, flags); | ||
833 | |||
834 | if (port->port_dev != NULL && port->port_open_count > 0 | ||
835 | && port->port_write_buf != NULL) | ||
836 | room = gs_buf_space_avail(port->port_write_buf); | ||
837 | |||
838 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
839 | |||
840 | gs_debug("gs_write_room: (%d,%p) room=%d\n", | ||
841 | port->port_num, tty, room); | ||
842 | |||
843 | return room; | ||
844 | } | ||
845 | |||
846 | /* | ||
847 | * gs_chars_in_buffer | ||
848 | */ | ||
849 | static int gs_chars_in_buffer(struct tty_struct *tty) | ||
850 | { | ||
851 | int chars = 0; | ||
852 | unsigned long flags; | ||
853 | struct gs_port *port = tty->driver_data; | ||
854 | |||
855 | if (port == NULL) | ||
856 | return 0; | ||
857 | |||
858 | spin_lock_irqsave(&port->port_lock, flags); | ||
859 | |||
860 | if (port->port_dev != NULL && port->port_open_count > 0 | ||
861 | && port->port_write_buf != NULL) | ||
862 | chars = gs_buf_data_avail(port->port_write_buf); | ||
863 | |||
864 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
865 | |||
866 | gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n", | ||
867 | port->port_num, tty, chars); | ||
868 | |||
869 | return chars; | ||
870 | } | ||
871 | |||
872 | /* | ||
873 | * gs_throttle | ||
874 | */ | ||
875 | static void gs_throttle(struct tty_struct *tty) | ||
876 | { | ||
877 | } | ||
878 | |||
879 | /* | ||
880 | * gs_unthrottle | ||
881 | */ | ||
882 | static void gs_unthrottle(struct tty_struct *tty) | ||
883 | { | ||
884 | } | ||
885 | |||
886 | /* | ||
887 | * gs_break | ||
888 | */ | ||
889 | static void gs_break(struct tty_struct *tty, int break_state) | ||
890 | { | ||
891 | } | ||
892 | |||
893 | /* | ||
894 | * gs_ioctl | ||
895 | */ | ||
896 | static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) | ||
897 | { | ||
898 | struct gs_port *port = tty->driver_data; | ||
899 | |||
900 | if (port == NULL) { | ||
901 | pr_err("gs_ioctl: NULL port pointer\n"); | ||
902 | return -EIO; | ||
903 | } | 126 | } |
904 | 127 | return status; | |
905 | gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n", | ||
906 | port->port_num, tty, file, cmd, arg); | ||
907 | |||
908 | /* handle ioctls */ | ||
909 | |||
910 | /* could not handle ioctl */ | ||
911 | return -ENOIOCTLCMD; | ||
912 | } | ||
913 | |||
914 | /* | ||
915 | * gs_set_termios | ||
916 | */ | ||
917 | static void gs_set_termios(struct tty_struct *tty, struct ktermios *old) | ||
918 | { | ||
919 | } | 128 | } |
920 | 129 | ||
921 | static const struct tty_operations gs_tty_ops = { | 130 | static struct usb_configuration serial_config_driver = { |
922 | .open = gs_open, | 131 | /* .label = f(use_acm) */ |
923 | .close = gs_close, | 132 | .bind = serial_bind_config, |
924 | .write = gs_write, | 133 | /* .bConfigurationValue = f(use_acm) */ |
925 | .put_char = gs_put_char, | 134 | /* .iConfiguration = DYNAMIC */ |
926 | .flush_chars = gs_flush_chars, | 135 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
927 | .write_room = gs_write_room, | 136 | .bMaxPower = 1, /* 2 mA, minimal */ |
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 | }; | 137 | }; |
935 | 138 | ||
936 | /*-------------------------------------------------------------------------*/ | 139 | static int __init gs_bind(struct usb_composite_dev *cdev) |
937 | |||
938 | /* | ||
939 | * gs_send | ||
940 | * | ||
941 | * This function finds available write requests, calls | ||
942 | * gs_send_packet to fill these packets with data, and | ||
943 | * continues until either there are no more write requests | ||
944 | * available or no more data to send. This function is | ||
945 | * run whenever data arrives or write requests are available. | ||
946 | */ | ||
947 | static int gs_send(struct gs_dev *dev) | ||
948 | { | ||
949 | int ret,len; | ||
950 | unsigned long flags; | ||
951 | struct usb_ep *ep; | ||
952 | struct usb_request *req; | ||
953 | |||
954 | if (dev == NULL) { | ||
955 | pr_err("gs_send: NULL device pointer\n"); | ||
956 | return -ENODEV; | ||
957 | } | ||
958 | |||
959 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
960 | |||
961 | ep = dev->dev_in_ep; | ||
962 | |||
963 | while(!list_empty(&dev->dev_req_list)) { | ||
964 | |||
965 | req = list_entry(dev->dev_req_list.next, | ||
966 | struct usb_request, list); | ||
967 | |||
968 | len = gs_send_packet(dev, req->buf, ep->maxpacket); | ||
969 | |||
970 | if (len > 0) { | ||
971 | gs_debug_level(3, "gs_send: len=%d, 0x%2.2x " | ||
972 | "0x%2.2x 0x%2.2x ...\n", len, | ||
973 | *((unsigned char *)req->buf), | ||
974 | *((unsigned char *)req->buf+1), | ||
975 | *((unsigned char *)req->buf+2)); | ||
976 | list_del(&req->list); | ||
977 | req->length = len; | ||
978 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
979 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
980 | pr_err( | ||
981 | "gs_send: cannot queue read request, ret=%d\n", | ||
982 | ret); | ||
983 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
984 | break; | ||
985 | } | ||
986 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
987 | } else { | ||
988 | break; | ||
989 | } | ||
990 | |||
991 | } | ||
992 | |||
993 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
994 | |||
995 | return 0; | ||
996 | } | ||
997 | |||
998 | /* | ||
999 | * gs_send_packet | ||
1000 | * | ||
1001 | * If there is data to send, a packet is built in the given | ||
1002 | * buffer and the size is returned. If there is no data to | ||
1003 | * send, 0 is returned. If there is any error a negative | ||
1004 | * error number is returned. | ||
1005 | * | ||
1006 | * Called during USB completion routine, on interrupt time. | ||
1007 | * | ||
1008 | * We assume that disconnect will not happen until all completion | ||
1009 | * routines have completed, so we can assume that the dev_port | ||
1010 | * array does not change during the lifetime of this function. | ||
1011 | */ | ||
1012 | static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size) | ||
1013 | { | ||
1014 | unsigned int len; | ||
1015 | struct gs_port *port; | ||
1016 | |||
1017 | /* TEMPORARY -- only port 0 is supported right now */ | ||
1018 | port = dev->dev_port[0]; | ||
1019 | |||
1020 | if (port == NULL) { | ||
1021 | pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0); | ||
1022 | return -EIO; | ||
1023 | } | ||
1024 | |||
1025 | spin_lock(&port->port_lock); | ||
1026 | |||
1027 | len = gs_buf_data_avail(port->port_write_buf); | ||
1028 | if (len < size) | ||
1029 | size = len; | ||
1030 | |||
1031 | if (size == 0) | ||
1032 | goto exit; | ||
1033 | |||
1034 | size = gs_buf_get(port->port_write_buf, packet, size); | ||
1035 | |||
1036 | if (port->port_tty) | ||
1037 | wake_up_interruptible(&port->port_tty->write_wait); | ||
1038 | |||
1039 | exit: | ||
1040 | spin_unlock(&port->port_lock); | ||
1041 | return size; | ||
1042 | } | ||
1043 | |||
1044 | /* | ||
1045 | * gs_recv_packet | ||
1046 | * | ||
1047 | * Called for each USB packet received. Reads the packet | ||
1048 | * header and stuffs the data in the appropriate tty buffer. | ||
1049 | * Returns 0 if successful, or a negative error number. | ||
1050 | * | ||
1051 | * Called during USB completion routine, on interrupt time. | ||
1052 | * | ||
1053 | * We assume that disconnect will not happen until all completion | ||
1054 | * routines have completed, so we can assume that the dev_port | ||
1055 | * array does not change during the lifetime of this function. | ||
1056 | */ | ||
1057 | static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size) | ||
1058 | { | ||
1059 | unsigned int len; | ||
1060 | struct gs_port *port; | ||
1061 | int ret; | ||
1062 | struct tty_struct *tty; | ||
1063 | |||
1064 | /* TEMPORARY -- only port 0 is supported right now */ | ||
1065 | port = dev->dev_port[0]; | ||
1066 | |||
1067 | if (port == NULL) { | ||
1068 | pr_err("gs_recv_packet: port=%d, NULL port pointer\n", | ||
1069 | port->port_num); | ||
1070 | return -EIO; | ||
1071 | } | ||
1072 | |||
1073 | spin_lock(&port->port_lock); | ||
1074 | |||
1075 | if (port->port_open_count == 0) { | ||
1076 | pr_err("gs_recv_packet: port=%d, port is closed\n", | ||
1077 | port->port_num); | ||
1078 | ret = -EIO; | ||
1079 | goto exit; | ||
1080 | } | ||
1081 | |||
1082 | |||
1083 | tty = port->port_tty; | ||
1084 | |||
1085 | if (tty == NULL) { | ||
1086 | pr_err("gs_recv_packet: port=%d, NULL tty pointer\n", | ||
1087 | port->port_num); | ||
1088 | ret = -EIO; | ||
1089 | goto exit; | ||
1090 | } | ||
1091 | |||
1092 | if (port->port_tty->magic != TTY_MAGIC) { | ||
1093 | pr_err("gs_recv_packet: port=%d, bad tty magic\n", | ||
1094 | port->port_num); | ||
1095 | ret = -EIO; | ||
1096 | goto exit; | ||
1097 | } | ||
1098 | |||
1099 | len = tty_buffer_request_room(tty, size); | ||
1100 | if (len > 0) { | ||
1101 | tty_insert_flip_string(tty, packet, len); | ||
1102 | tty_flip_buffer_push(port->port_tty); | ||
1103 | wake_up_interruptible(&port->port_tty->read_wait); | ||
1104 | } | ||
1105 | ret = 0; | ||
1106 | exit: | ||
1107 | spin_unlock(&port->port_lock); | ||
1108 | return ret; | ||
1109 | } | ||
1110 | |||
1111 | /* | ||
1112 | * gs_read_complete | ||
1113 | */ | ||
1114 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) | ||
1115 | { | 140 | { |
1116 | int ret; | 141 | int gcnum; |
1117 | struct gs_dev *dev = ep->driver_data; | 142 | struct usb_gadget *gadget = cdev->gadget; |
1118 | 143 | int status; | |
1119 | if (dev == NULL) { | ||
1120 | pr_err("gs_read_complete: NULL device pointer\n"); | ||
1121 | return; | ||
1122 | } | ||
1123 | 144 | ||
1124 | switch(req->status) { | 145 | status = gserial_setup(cdev->gadget, n_ports); |
1125 | case 0: | 146 | if (status < 0) |
1126 | /* normal completion */ | 147 | return status; |
1127 | gs_recv_packet(dev, req->buf, req->actual); | ||
1128 | requeue: | ||
1129 | req->length = ep->maxpacket; | ||
1130 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
1131 | pr_err( | ||
1132 | "gs_read_complete: cannot queue read request, ret=%d\n", | ||
1133 | ret); | ||
1134 | } | ||
1135 | break; | ||
1136 | |||
1137 | case -ESHUTDOWN: | ||
1138 | /* disconnect */ | ||
1139 | gs_debug("gs_read_complete: shutdown\n"); | ||
1140 | gs_free_req(ep, req); | ||
1141 | break; | ||
1142 | |||
1143 | default: | ||
1144 | /* unexpected */ | ||
1145 | pr_err( | ||
1146 | "gs_read_complete: unexpected status error, status=%d\n", | ||
1147 | req->status); | ||
1148 | goto requeue; | ||
1149 | break; | ||
1150 | } | ||
1151 | } | ||
1152 | |||
1153 | /* | ||
1154 | * gs_write_complete | ||
1155 | */ | ||
1156 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) | ||
1157 | { | ||
1158 | struct gs_dev *dev = ep->driver_data; | ||
1159 | |||
1160 | if (dev == NULL) { | ||
1161 | pr_err("gs_write_complete: NULL device pointer\n"); | ||
1162 | return; | ||
1163 | } | ||
1164 | 148 | ||
1165 | switch(req->status) { | 149 | /* Allocate string descriptor numbers ... note that string |
1166 | case 0: | 150 | * contents can be overridden by the composite_dev glue. |
1167 | /* normal completion */ | 151 | */ |
1168 | requeue: | ||
1169 | spin_lock(&dev->dev_lock); | ||
1170 | list_add(&req->list, &dev->dev_req_list); | ||
1171 | spin_unlock(&dev->dev_lock); | ||
1172 | |||
1173 | gs_send(dev); | ||
1174 | |||
1175 | break; | ||
1176 | |||
1177 | case -ESHUTDOWN: | ||
1178 | /* disconnect */ | ||
1179 | gs_debug("gs_write_complete: shutdown\n"); | ||
1180 | gs_free_req(ep, req); | ||
1181 | break; | ||
1182 | |||
1183 | default: | ||
1184 | pr_err( | ||
1185 | "gs_write_complete: unexpected status error, status=%d\n", | ||
1186 | req->status); | ||
1187 | goto requeue; | ||
1188 | break; | ||
1189 | } | ||
1190 | } | ||
1191 | 152 | ||
1192 | /*-------------------------------------------------------------------------*/ | 153 | /* device description: manufacturer, product */ |
154 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | ||
155 | init_utsname()->sysname, init_utsname()->release, | ||
156 | gadget->name); | ||
157 | status = usb_string_id(cdev); | ||
158 | if (status < 0) | ||
159 | goto fail; | ||
160 | strings_dev[STRING_MANUFACTURER_IDX].id = status; | ||
1193 | 161 | ||
1194 | /* Gadget Driver */ | 162 | device_desc.iManufacturer = status; |
1195 | 163 | ||
1196 | /* | 164 | status = usb_string_id(cdev); |
1197 | * gs_unbind | 165 | if (status < 0) |
1198 | * | 166 | goto fail; |
1199 | * Called on module unload. Frees the control request and device | 167 | strings_dev[STRING_PRODUCT_IDX].id = status; |
1200 | * structure. | ||
1201 | */ | ||
1202 | static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget) | ||
1203 | { | ||
1204 | struct gs_dev *dev = get_gadget_data(gadget); | ||
1205 | 168 | ||
1206 | gs_device = NULL; | 169 | device_desc.iProduct = status; |
1207 | 170 | ||
1208 | /* read/write requests already freed, only control request remains */ | 171 | /* config description */ |
1209 | if (dev != NULL) { | 172 | status = usb_string_id(cdev); |
1210 | if (dev->dev_ctrl_req != NULL) { | 173 | if (status < 0) |
1211 | gs_free_req(gadget->ep0, dev->dev_ctrl_req); | 174 | goto fail; |
1212 | dev->dev_ctrl_req = NULL; | 175 | strings_dev[STRING_DESCRIPTION_IDX].id = status; |
1213 | } | ||
1214 | gs_reset_config(dev); | ||
1215 | gs_free_ports(dev); | ||
1216 | kfree(dev); | ||
1217 | set_gadget_data(gadget, NULL); | ||
1218 | } | ||
1219 | 176 | ||
1220 | pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME, | 177 | serial_config_driver.iConfiguration = status; |
1221 | GS_VERSION_STR); | ||
1222 | } | ||
1223 | |||
1224 | /* | ||
1225 | * gs_bind | ||
1226 | * | ||
1227 | * Called on module load. Allocates and initializes the device | ||
1228 | * structure and a control request. | ||
1229 | */ | ||
1230 | static int __init gs_bind(struct usb_gadget *gadget) | ||
1231 | { | ||
1232 | int ret; | ||
1233 | struct usb_ep *ep; | ||
1234 | struct gs_dev *dev; | ||
1235 | int gcnum; | ||
1236 | |||
1237 | /* Some controllers can't support CDC ACM: | ||
1238 | * - sh doesn't support multiple interfaces or configs; | ||
1239 | * - sa1100 doesn't have a third interrupt endpoint | ||
1240 | */ | ||
1241 | if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget)) | ||
1242 | use_acm = 0; | ||
1243 | 178 | ||
179 | /* set up other descriptors */ | ||
1244 | gcnum = usb_gadget_controller_number(gadget); | 180 | gcnum = usb_gadget_controller_number(gadget); |
1245 | if (gcnum >= 0) | 181 | if (gcnum >= 0) |
1246 | gs_device_desc.bcdDevice = | 182 | device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum); |
1247 | cpu_to_le16(GS_VERSION_NUM | gcnum); | ||
1248 | else { | 183 | else { |
184 | /* this is so simple (for now, no altsettings) that it | ||
185 | * SHOULD NOT have problems with bulk-capable hardware. | ||
186 | * so warn about unrcognized controllers -- don't panic. | ||
187 | * | ||
188 | * things like configuration and altsetting numbering | ||
189 | * can need hardware-specific attention though. | ||
190 | */ | ||
1249 | pr_warning("gs_bind: controller '%s' not recognized\n", | 191 | pr_warning("gs_bind: controller '%s' not recognized\n", |
1250 | gadget->name); | 192 | gadget->name); |
1251 | /* unrecognized, but safe unless bulk is REALLY quirky */ | 193 | device_desc.bcdDevice = |
1252 | gs_device_desc.bcdDevice = | 194 | __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099); |
1253 | __constant_cpu_to_le16(GS_VERSION_NUM|0x0099); | ||
1254 | } | ||
1255 | |||
1256 | dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); | ||
1257 | if (dev == NULL) | ||
1258 | return -ENOMEM; | ||
1259 | |||
1260 | usb_ep_autoconfig_reset(gadget); | ||
1261 | |||
1262 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); | ||
1263 | if (!ep) | ||
1264 | goto autoconf_fail; | ||
1265 | dev->dev_in_ep = ep; | ||
1266 | ep->driver_data = dev; /* claim the endpoint */ | ||
1267 | |||
1268 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); | ||
1269 | if (!ep) | ||
1270 | goto autoconf_fail; | ||
1271 | dev->dev_out_ep = ep; | ||
1272 | ep->driver_data = dev; /* claim the endpoint */ | ||
1273 | |||
1274 | if (use_acm) { | ||
1275 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc); | ||
1276 | if (!ep) { | ||
1277 | pr_err("gs_bind: cannot run ACM on %s\n", gadget->name); | ||
1278 | goto autoconf_fail; | ||
1279 | } | ||
1280 | gs_device_desc.idProduct = __constant_cpu_to_le16( | ||
1281 | GS_CDC_PRODUCT_ID), | ||
1282 | dev->dev_notify_ep = ep; | ||
1283 | ep->driver_data = dev; /* claim the endpoint */ | ||
1284 | } | ||
1285 | |||
1286 | gs_device_desc.bDeviceClass = use_acm | ||
1287 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; | ||
1288 | gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | ||
1289 | |||
1290 | if (gadget_is_dualspeed(gadget)) { | ||
1291 | gs_qualifier_desc.bDeviceClass = use_acm | ||
1292 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; | ||
1293 | /* assume ep0 uses the same packet size for both speeds */ | ||
1294 | gs_qualifier_desc.bMaxPacketSize0 = | ||
1295 | gs_device_desc.bMaxPacketSize0; | ||
1296 | /* assume endpoints are dual-speed */ | ||
1297 | gs_highspeed_notify_desc.bEndpointAddress = | ||
1298 | gs_fullspeed_notify_desc.bEndpointAddress; | ||
1299 | gs_highspeed_in_desc.bEndpointAddress = | ||
1300 | gs_fullspeed_in_desc.bEndpointAddress; | ||
1301 | gs_highspeed_out_desc.bEndpointAddress = | ||
1302 | gs_fullspeed_out_desc.bEndpointAddress; | ||
1303 | } | ||
1304 | |||
1305 | usb_gadget_set_selfpowered(gadget); | ||
1306 | |||
1307 | if (gadget_is_otg(gadget)) { | ||
1308 | gs_otg_descriptor.bmAttributes |= USB_OTG_HNP, | ||
1309 | gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1310 | gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1311 | } | 195 | } |
1312 | 196 | ||
1313 | gs_device = dev; | 197 | if (gadget_is_otg(cdev->gadget)) { |
1314 | 198 | serial_config_driver.descriptors = otg_desc; | |
1315 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", | 199 | serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
1316 | init_utsname()->sysname, init_utsname()->release, | ||
1317 | gadget->name); | ||
1318 | |||
1319 | dev->dev_gadget = gadget; | ||
1320 | spin_lock_init(&dev->dev_lock); | ||
1321 | INIT_LIST_HEAD(&dev->dev_req_list); | ||
1322 | set_gadget_data(gadget, dev); | ||
1323 | |||
1324 | if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) { | ||
1325 | pr_err("gs_bind: cannot allocate ports\n"); | ||
1326 | gs_unbind(gadget); | ||
1327 | return ret; | ||
1328 | } | 200 | } |
1329 | 201 | ||
1330 | /* preallocate control response and buffer */ | 202 | /* register our configuration */ |
1331 | dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, | 203 | status = usb_add_config(cdev, &serial_config_driver); |
1332 | GFP_KERNEL); | 204 | if (status < 0) |
1333 | if (dev->dev_ctrl_req == NULL) { | 205 | goto fail; |
1334 | gs_unbind(gadget); | ||
1335 | return -ENOMEM; | ||
1336 | } | ||
1337 | gadget->ep0->driver_data = dev; | ||
1338 | 206 | ||
1339 | pr_info("gs_bind: %s %s bound\n", | 207 | INFO(cdev, "%s\n", GS_VERSION_NAME); |
1340 | GS_LONG_NAME, GS_VERSION_STR); | ||
1341 | 208 | ||
1342 | return 0; | 209 | return 0; |
1343 | 210 | ||
1344 | autoconf_fail: | 211 | fail: |
1345 | kfree(dev); | 212 | gserial_cleanup(); |
1346 | pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name); | 213 | return status; |
1347 | return -ENODEV; | ||
1348 | } | ||
1349 | |||
1350 | static int gs_setup_standard(struct usb_gadget *gadget, | ||
1351 | const struct usb_ctrlrequest *ctrl) | ||
1352 | { | ||
1353 | int ret = -EOPNOTSUPP; | ||
1354 | struct gs_dev *dev = get_gadget_data(gadget); | ||
1355 | struct usb_request *req = dev->dev_ctrl_req; | ||
1356 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | ||
1357 | u16 wValue = le16_to_cpu(ctrl->wValue); | ||
1358 | u16 wLength = le16_to_cpu(ctrl->wLength); | ||
1359 | |||
1360 | switch (ctrl->bRequest) { | ||
1361 | case USB_REQ_GET_DESCRIPTOR: | ||
1362 | if (ctrl->bRequestType != USB_DIR_IN) | ||
1363 | break; | ||
1364 | |||
1365 | switch (wValue >> 8) { | ||
1366 | case USB_DT_DEVICE: | ||
1367 | ret = min(wLength, | ||
1368 | (u16)sizeof(struct usb_device_descriptor)); | ||
1369 | memcpy(req->buf, &gs_device_desc, ret); | ||
1370 | break; | ||
1371 | |||
1372 | case USB_DT_DEVICE_QUALIFIER: | ||
1373 | if (!gadget_is_dualspeed(gadget)) | ||
1374 | break; | ||
1375 | ret = min(wLength, | ||
1376 | (u16)sizeof(struct usb_qualifier_descriptor)); | ||
1377 | memcpy(req->buf, &gs_qualifier_desc, ret); | ||
1378 | break; | ||
1379 | |||
1380 | case USB_DT_OTHER_SPEED_CONFIG: | ||
1381 | if (!gadget_is_dualspeed(gadget)) | ||
1382 | break; | ||
1383 | /* fall through */ | ||
1384 | case USB_DT_CONFIG: | ||
1385 | ret = gs_build_config_buf(req->buf, gadget, | ||
1386 | wValue >> 8, wValue & 0xff, | ||
1387 | gadget_is_otg(gadget)); | ||
1388 | if (ret >= 0) | ||
1389 | ret = min(wLength, (u16)ret); | ||
1390 | break; | ||
1391 | |||
1392 | case USB_DT_STRING: | ||
1393 | /* wIndex == language code. */ | ||
1394 | ret = usb_gadget_get_string(&gs_string_table, | ||
1395 | wValue & 0xff, req->buf); | ||
1396 | if (ret >= 0) | ||
1397 | ret = min(wLength, (u16)ret); | ||
1398 | break; | ||
1399 | } | ||
1400 | break; | ||
1401 | |||
1402 | case USB_REQ_SET_CONFIGURATION: | ||
1403 | if (ctrl->bRequestType != 0) | ||
1404 | break; | ||
1405 | spin_lock(&dev->dev_lock); | ||
1406 | ret = gs_set_config(dev, wValue); | ||
1407 | spin_unlock(&dev->dev_lock); | ||
1408 | break; | ||
1409 | |||
1410 | case USB_REQ_GET_CONFIGURATION: | ||
1411 | if (ctrl->bRequestType != USB_DIR_IN) | ||
1412 | break; | ||
1413 | *(u8 *)req->buf = dev->dev_config; | ||
1414 | ret = min(wLength, (u16)1); | ||
1415 | break; | ||
1416 | |||
1417 | case USB_REQ_SET_INTERFACE: | ||
1418 | if (ctrl->bRequestType != USB_RECIP_INTERFACE | ||
1419 | || !dev->dev_config | ||
1420 | || wIndex >= GS_MAX_NUM_INTERFACES) | ||
1421 | break; | ||
1422 | if (dev->dev_config == GS_BULK_CONFIG_ID | ||
1423 | && wIndex != GS_BULK_INTERFACE_ID) | ||
1424 | break; | ||
1425 | /* no alternate interface settings */ | ||
1426 | if (wValue != 0) | ||
1427 | break; | ||
1428 | spin_lock(&dev->dev_lock); | ||
1429 | /* PXA hardware partially handles SET_INTERFACE; | ||
1430 | * we need to kluge around that interference. */ | ||
1431 | if (gadget_is_pxa(gadget)) { | ||
1432 | ret = gs_set_config(dev, use_acm ? | ||
1433 | GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID); | ||
1434 | goto set_interface_done; | ||
1435 | } | ||
1436 | if (dev->dev_config != GS_BULK_CONFIG_ID | ||
1437 | && wIndex == GS_CONTROL_INTERFACE_ID) { | ||
1438 | if (dev->dev_notify_ep) { | ||
1439 | usb_ep_disable(dev->dev_notify_ep); | ||
1440 | usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc); | ||
1441 | } | ||
1442 | } else { | ||
1443 | usb_ep_disable(dev->dev_in_ep); | ||
1444 | usb_ep_disable(dev->dev_out_ep); | ||
1445 | usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc); | ||
1446 | usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc); | ||
1447 | } | ||
1448 | ret = 0; | ||
1449 | set_interface_done: | ||
1450 | spin_unlock(&dev->dev_lock); | ||
1451 | break; | ||
1452 | |||
1453 | case USB_REQ_GET_INTERFACE: | ||
1454 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) | ||
1455 | || dev->dev_config == GS_NO_CONFIG_ID) | ||
1456 | break; | ||
1457 | if (wIndex >= GS_MAX_NUM_INTERFACES | ||
1458 | || (dev->dev_config == GS_BULK_CONFIG_ID | ||
1459 | && wIndex != GS_BULK_INTERFACE_ID)) { | ||
1460 | ret = -EDOM; | ||
1461 | break; | ||
1462 | } | ||
1463 | /* no alternate interface settings */ | ||
1464 | *(u8 *)req->buf = 0; | ||
1465 | ret = min(wLength, (u16)1); | ||
1466 | break; | ||
1467 | |||
1468 | default: | ||
1469 | pr_err("gs_setup: unknown standard request, type=%02x, " | ||
1470 | "request=%02x, value=%04x, index=%04x, length=%d\n", | ||
1471 | ctrl->bRequestType, ctrl->bRequest, | ||
1472 | wValue, wIndex, wLength); | ||
1473 | break; | ||
1474 | } | ||
1475 | |||
1476 | return ret; | ||
1477 | } | 214 | } |
1478 | 215 | ||
1479 | static void gs_setup_complete_set_line_coding(struct usb_ep *ep, | 216 | static struct usb_composite_driver gserial_driver = { |
1480 | struct usb_request *req) | 217 | .name = "g_serial", |
1481 | { | 218 | .dev = &device_desc, |
1482 | struct gs_dev *dev = ep->driver_data; | 219 | .strings = dev_strings, |
1483 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ | 220 | .bind = gs_bind, |
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 | |||
1515 | static int gs_setup_class(struct usb_gadget *gadget, | ||
1516 | const struct usb_ctrlrequest *ctrl) | ||
1517 | { | ||
1518 | int ret = -EOPNOTSUPP; | ||
1519 | struct gs_dev *dev = get_gadget_data(gadget); | ||
1520 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ | ||
1521 | struct usb_request *req = dev->dev_ctrl_req; | ||
1522 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | ||
1523 | u16 wValue = le16_to_cpu(ctrl->wValue); | ||
1524 | u16 wLength = le16_to_cpu(ctrl->wLength); | ||
1525 | |||
1526 | switch (ctrl->bRequest) { | ||
1527 | case USB_CDC_REQ_SET_LINE_CODING: | ||
1528 | if (wLength != sizeof(struct usb_cdc_line_coding)) | ||
1529 | break; | ||
1530 | ret = wLength; | ||
1531 | req->complete = gs_setup_complete_set_line_coding; | ||
1532 | break; | ||
1533 | |||
1534 | case USB_CDC_REQ_GET_LINE_CODING: | ||
1535 | ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding)); | ||
1536 | if (port) { | ||
1537 | spin_lock(&port->port_lock); | ||
1538 | memcpy(req->buf, &port->port_line_coding, ret); | ||
1539 | spin_unlock(&port->port_lock); | ||
1540 | } | ||
1541 | break; | ||
1542 | |||
1543 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: | ||
1544 | if (wLength != 0) | ||
1545 | break; | ||
1546 | ret = 0; | ||
1547 | if (port) { | ||
1548 | /* REVISIT: we currently just remember this data. | ||
1549 | * If we change that, update whatever hardware needs | ||
1550 | * updating. | ||
1551 | */ | ||
1552 | spin_lock(&port->port_lock); | ||
1553 | port->port_handshake_bits = wValue; | ||
1554 | spin_unlock(&port->port_lock); | ||
1555 | } | ||
1556 | break; | ||
1557 | |||
1558 | default: | ||
1559 | /* NOTE: strictly speaking, we should accept AT-commands | ||
1560 | * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE. | ||
1561 | * But our call management descriptor says we don't handle | ||
1562 | * call management, so we should be able to get by without | ||
1563 | * handling those "required" commands (except by stalling). | ||
1564 | */ | ||
1565 | pr_err("gs_setup: unknown class request, " | ||
1566 | "type=%02x, request=%02x, value=%04x, " | ||
1567 | "index=%04x, length=%d\n", | ||
1568 | ctrl->bRequestType, ctrl->bRequest, | ||
1569 | wValue, wIndex, wLength); | ||
1570 | break; | ||
1571 | } | ||
1572 | |||
1573 | return ret; | ||
1574 | } | ||
1575 | |||
1576 | /* | ||
1577 | * gs_setup_complete | ||
1578 | */ | ||
1579 | static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req) | ||
1580 | { | ||
1581 | if (req->status || req->actual != req->length) { | ||
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 | } | ||
1587 | |||
1588 | /* | ||
1589 | * gs_setup | ||
1590 | * | ||
1591 | * Implements all the control endpoint functionality that's not | ||
1592 | * handled in hardware or the hardware driver. | ||
1593 | * | ||
1594 | * Returns the size of the data sent to the host, or a negative | ||
1595 | * error number. | ||
1596 | */ | ||
1597 | static 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); | ||
1606 | |||
1607 | req->complete = gs_setup_complete; | ||
1608 | |||
1609 | switch (ctrl->bRequestType & USB_TYPE_MASK) { | ||
1610 | case USB_TYPE_STANDARD: | ||
1611 | ret = gs_setup_standard(gadget, ctrl); | ||
1612 | break; | ||
1613 | |||
1614 | case USB_TYPE_CLASS: | ||
1615 | ret = gs_setup_class(gadget, ctrl); | ||
1616 | break; | ||
1617 | |||
1618 | default: | ||
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); | ||
1623 | break; | ||
1624 | } | ||
1625 | |||
1626 | /* respond with data transfer before status phase? */ | ||
1627 | if (ret >= 0) { | ||
1628 | req->length = ret; | ||
1629 | req->zero = ret < wLength | ||
1630 | && (ret % gadget->ep0->maxpacket) == 0; | ||
1631 | ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | ||
1632 | if (ret < 0) { | ||
1633 | pr_err("gs_setup: cannot queue response, ret=%d\n", | ||
1634 | ret); | ||
1635 | req->status = 0; | ||
1636 | gs_setup_complete(gadget->ep0, req); | ||
1637 | } | ||
1638 | } | ||
1639 | |||
1640 | /* device either stalls (ret < 0) or reports success */ | ||
1641 | return ret; | ||
1642 | } | ||
1643 | |||
1644 | /* | ||
1645 | * gs_disconnect | ||
1646 | * | ||
1647 | * Called when the device is disconnected. Frees the closed | ||
1648 | * ports and disconnects open ports. Open ports will be freed | ||
1649 | * on close. Then reallocates the ports for the next connection. | ||
1650 | */ | ||
1651 | static void gs_disconnect(struct usb_gadget *gadget) | ||
1652 | { | ||
1653 | unsigned long flags; | ||
1654 | struct gs_dev *dev = get_gadget_data(gadget); | ||
1655 | |||
1656 | spin_lock_irqsave(&dev->dev_lock, flags); | ||
1657 | |||
1658 | gs_reset_config(dev); | ||
1659 | |||
1660 | /* free closed ports and disconnect open ports */ | ||
1661 | /* (open ports will be freed when closed) */ | ||
1662 | gs_free_ports(dev); | ||
1663 | |||
1664 | /* re-allocate ports for the next connection */ | ||
1665 | if (gs_alloc_ports(dev, GFP_ATOMIC) != 0) | ||
1666 | pr_err("gs_disconnect: cannot re-allocate ports\n"); | ||
1667 | |||
1668 | spin_unlock_irqrestore(&dev->dev_lock, flags); | ||
1669 | |||
1670 | pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); | ||
1671 | } | ||
1672 | |||
1673 | static 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 | }; | 221 | }; |
1689 | 222 | ||
1690 | /* | 223 | static int __init init(void) |
1691 | * gs_set_config | ||
1692 | * | ||
1693 | * Configures the device by enabling device specific | ||
1694 | * optimizations, setting up the endpoints, allocating | ||
1695 | * read and write requests and queuing read requests. | ||
1696 | * | ||
1697 | * The device lock must be held when calling this function. | ||
1698 | */ | ||
1699 | static int gs_set_config(struct gs_dev *dev, unsigned config) | ||
1700 | { | 224 | { |
1701 | int i; | 225 | /* We *could* export two configs; that'd be much cleaner... |
1702 | int ret = 0; | 226 | * but neither of these product IDs was defined that way. |
1703 | struct usb_gadget *gadget = dev->dev_gadget; | ||
1704 | struct usb_ep *ep; | ||
1705 | struct usb_endpoint_descriptor *out, *in, *notify; | ||
1706 | struct usb_request *req; | ||
1707 | |||
1708 | if (dev == NULL) { | ||
1709 | pr_err("gs_set_config: NULL device pointer\n"); | ||
1710 | return 0; | ||
1711 | } | ||
1712 | |||
1713 | if (config == dev->dev_config) | ||
1714 | return 0; | ||
1715 | |||
1716 | gs_reset_config(dev); | ||
1717 | |||
1718 | switch (config) { | ||
1719 | case GS_NO_CONFIG_ID: | ||
1720 | return 0; | ||
1721 | case GS_BULK_CONFIG_ID: | ||
1722 | if (use_acm) | ||
1723 | return -EINVAL; | ||
1724 | break; | ||
1725 | case GS_ACM_CONFIG_ID: | ||
1726 | if (!use_acm) | ||
1727 | return -EINVAL; | ||
1728 | break; | ||
1729 | default: | ||
1730 | return -EINVAL; | ||
1731 | } | ||
1732 | |||
1733 | in = choose_ep_desc(gadget, | ||
1734 | &gs_highspeed_in_desc, | ||
1735 | &gs_fullspeed_in_desc); | ||
1736 | out = choose_ep_desc(gadget, | ||
1737 | &gs_highspeed_out_desc, | ||
1738 | &gs_fullspeed_out_desc); | ||
1739 | notify = dev->dev_notify_ep | ||
1740 | ? choose_ep_desc(gadget, | ||
1741 | &gs_highspeed_notify_desc, | ||
1742 | &gs_fullspeed_notify_desc) | ||
1743 | : NULL; | ||
1744 | |||
1745 | ret = usb_ep_enable(dev->dev_in_ep, in); | ||
1746 | if (ret == 0) { | ||
1747 | dev->dev_in_ep_desc = in; | ||
1748 | } else { | ||
1749 | pr_debug("%s: cannot enable %s %s, ret=%d\n", | ||
1750 | __func__, "IN", dev->dev_in_ep->name, ret); | ||
1751 | return ret; | ||
1752 | } | ||
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); | ||
1760 | fail0: | ||
1761 | usb_ep_disable(dev->dev_in_ep); | ||
1762 | return ret; | ||
1763 | } | ||
1764 | |||
1765 | if (notify) { | ||
1766 | ret = usb_ep_enable(dev->dev_notify_ep, notify); | ||
1767 | if (ret == 0) { | ||
1768 | dev->dev_notify_ep_desc = notify; | ||
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 | } | ||
1776 | } | ||
1777 | |||
1778 | dev->dev_config = config; | ||
1779 | |||
1780 | /* allocate and queue read requests */ | ||
1781 | ep = dev->dev_out_ep; | ||
1782 | for (i=0; i<read_q_size && ret == 0; i++) { | ||
1783 | if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) { | ||
1784 | req->complete = gs_read_complete; | ||
1785 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { | ||
1786 | pr_err("gs_set_config: cannot queue read " | ||
1787 | "request, ret=%d\n", ret); | ||
1788 | } | ||
1789 | } else { | ||
1790 | pr_err("gs_set_config: cannot allocate " | ||
1791 | "read requests\n"); | ||
1792 | ret = -ENOMEM; | ||
1793 | goto exit_reset_config; | ||
1794 | } | ||
1795 | } | ||
1796 | |||
1797 | /* allocate write requests, and put on free list */ | ||
1798 | ep = dev->dev_in_ep; | ||
1799 | for (i=0; i<write_q_size; i++) { | ||
1800 | req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); | ||
1801 | if (req) { | ||
1802 | req->complete = gs_write_complete; | ||
1803 | list_add(&req->list, &dev->dev_req_list); | ||
1804 | } else { | ||
1805 | pr_err("gs_set_config: cannot allocate " | ||
1806 | "write requests\n"); | ||
1807 | ret = -ENOMEM; | ||
1808 | goto exit_reset_config; | ||
1809 | } | ||
1810 | } | ||
1811 | |||
1812 | /* REVISIT the ACM mode should be able to actually *issue* some | ||
1813 | * notifications, for at least serial state change events if | ||
1814 | * not also for network connection; say so in bmCapabilities. | ||
1815 | */ | 227 | */ |
1816 | |||
1817 | pr_info("gs_set_config: %s configured, %s speed %s config\n", | ||
1818 | GS_LONG_NAME, | ||
1819 | gadget->speed == USB_SPEED_HIGH ? "high" : "full", | ||
1820 | config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM"); | ||
1821 | |||
1822 | return 0; | ||
1823 | |||
1824 | exit_reset_config: | ||
1825 | gs_reset_config(dev); | ||
1826 | return ret; | ||
1827 | } | ||
1828 | |||
1829 | /* | ||
1830 | * gs_reset_config | ||
1831 | * | ||
1832 | * Mark the device as not configured, disable all endpoints, | ||
1833 | * which forces completion of pending I/O and frees queued | ||
1834 | * requests, and free the remaining write requests on the | ||
1835 | * free list. | ||
1836 | * | ||
1837 | * The device lock must be held when calling this function. | ||
1838 | */ | ||
1839 | static void gs_reset_config(struct gs_dev *dev) | ||
1840 | { | ||
1841 | struct usb_request *req; | ||
1842 | |||
1843 | if (dev == NULL) { | ||
1844 | pr_err("gs_reset_config: NULL device pointer\n"); | ||
1845 | return; | ||
1846 | } | ||
1847 | |||
1848 | if (dev->dev_config == GS_NO_CONFIG_ID) | ||
1849 | return; | ||
1850 | |||
1851 | dev->dev_config = GS_NO_CONFIG_ID; | ||
1852 | |||
1853 | /* free write requests on the free list */ | ||
1854 | while(!list_empty(&dev->dev_req_list)) { | ||
1855 | req = list_entry(dev->dev_req_list.next, | ||
1856 | struct usb_request, list); | ||
1857 | list_del(&req->list); | ||
1858 | gs_free_req(dev->dev_in_ep, req); | ||
1859 | } | ||
1860 | |||
1861 | /* disable endpoints, forcing completion of pending i/o; */ | ||
1862 | /* completion handlers free their requests in this case */ | ||
1863 | if (dev->dev_notify_ep) | ||
1864 | usb_ep_disable(dev->dev_notify_ep); | ||
1865 | usb_ep_disable(dev->dev_in_ep); | ||
1866 | usb_ep_disable(dev->dev_out_ep); | ||
1867 | } | ||
1868 | |||
1869 | /* | ||
1870 | * gs_build_config_buf | ||
1871 | * | ||
1872 | * Builds the config descriptors in the given buffer and returns the | ||
1873 | * length, or a negative error number. | ||
1874 | */ | ||
1875 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, | ||
1876 | u8 type, unsigned int index, int is_otg) | ||
1877 | { | ||
1878 | int len; | ||
1879 | int high_speed = 0; | ||
1880 | const struct usb_config_descriptor *config_desc; | ||
1881 | const struct usb_descriptor_header **function; | ||
1882 | |||
1883 | if (index >= gs_device_desc.bNumConfigurations) | ||
1884 | return -EINVAL; | ||
1885 | |||
1886 | /* other speed switches high and full speed */ | ||
1887 | if (gadget_is_dualspeed(g)) { | ||
1888 | high_speed = (g->speed == USB_SPEED_HIGH); | ||
1889 | if (type == USB_DT_OTHER_SPEED_CONFIG) | ||
1890 | high_speed = !high_speed; | ||
1891 | } | ||
1892 | |||
1893 | if (use_acm) { | 228 | if (use_acm) { |
1894 | config_desc = &gs_acm_config_desc; | 229 | serial_config_driver.label = "CDC ACM config"; |
1895 | function = high_speed | 230 | serial_config_driver.bConfigurationValue = 2; |
1896 | ? gs_acm_highspeed_function | 231 | device_desc.bDeviceClass = USB_CLASS_COMM; |
1897 | : gs_acm_fullspeed_function; | 232 | device_desc.idProduct = |
233 | __constant_cpu_to_le16(GS_CDC_PRODUCT_ID); | ||
1898 | } else { | 234 | } else { |
1899 | config_desc = &gs_bulk_config_desc; | 235 | serial_config_driver.label = "Generic Serial config"; |
1900 | function = high_speed | 236 | serial_config_driver.bConfigurationValue = 1; |
1901 | ? gs_bulk_highspeed_function | 237 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
1902 | : gs_bulk_fullspeed_function; | 238 | device_desc.idProduct = |
1903 | } | 239 | __constant_cpu_to_le16(GS_PRODUCT_ID); |
1904 | |||
1905 | /* for now, don't advertise srp-only devices */ | ||
1906 | if (!is_otg) | ||
1907 | function++; | ||
1908 | |||
1909 | len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function); | ||
1910 | if (len < 0) | ||
1911 | return len; | ||
1912 | |||
1913 | ((struct usb_config_descriptor *)buf)->bDescriptorType = type; | ||
1914 | |||
1915 | return len; | ||
1916 | } | ||
1917 | |||
1918 | /* | ||
1919 | * gs_alloc_req | ||
1920 | * | ||
1921 | * Allocate a usb_request and its buffer. Returns a pointer to the | ||
1922 | * usb_request or NULL if there is an error. | ||
1923 | */ | ||
1924 | static struct usb_request * | ||
1925 | gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags) | ||
1926 | { | ||
1927 | struct usb_request *req; | ||
1928 | |||
1929 | if (ep == NULL) | ||
1930 | return NULL; | ||
1931 | |||
1932 | req = usb_ep_alloc_request(ep, kmalloc_flags); | ||
1933 | |||
1934 | if (req != NULL) { | ||
1935 | req->length = len; | ||
1936 | req->buf = kmalloc(len, kmalloc_flags); | ||
1937 | if (req->buf == NULL) { | ||
1938 | usb_ep_free_request(ep, req); | ||
1939 | return NULL; | ||
1940 | } | ||
1941 | } | 240 | } |
241 | strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; | ||
1942 | 242 | ||
1943 | return req; | 243 | return usb_composite_register(&gserial_driver); |
1944 | } | 244 | } |
245 | module_init(init); | ||
1945 | 246 | ||
1946 | /* | 247 | static void __exit cleanup(void) |
1947 | * gs_free_req | ||
1948 | * | ||
1949 | * Free a usb_request and its buffer. | ||
1950 | */ | ||
1951 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req) | ||
1952 | { | 248 | { |
1953 | if (ep != NULL && req != NULL) { | 249 | usb_composite_unregister(&gserial_driver); |
1954 | kfree(req->buf); | 250 | gserial_cleanup(); |
1955 | usb_ep_free_request(ep, req); | ||
1956 | } | ||
1957 | } | ||
1958 | |||
1959 | /* | ||
1960 | * gs_alloc_ports | ||
1961 | * | ||
1962 | * Allocate all ports and set the gs_dev struct to point to them. | ||
1963 | * Return 0 if successful, or a negative error number. | ||
1964 | * | ||
1965 | * The device lock is normally held when calling this function. | ||
1966 | */ | ||
1967 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags) | ||
1968 | { | ||
1969 | int i; | ||
1970 | struct gs_port *port; | ||
1971 | |||
1972 | if (dev == NULL) | ||
1973 | return -EIO; | ||
1974 | |||
1975 | for (i=0; i<GS_NUM_PORTS; i++) { | ||
1976 | if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL) | ||
1977 | return -ENOMEM; | ||
1978 | |||
1979 | port->port_dev = dev; | ||
1980 | port->port_num = i; | ||
1981 | port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE); | ||
1982 | port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT; | ||
1983 | port->port_line_coding.bParityType = GS_DEFAULT_PARITY; | ||
1984 | port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS; | ||
1985 | spin_lock_init(&port->port_lock); | ||
1986 | init_waitqueue_head(&port->port_write_wait); | ||
1987 | |||
1988 | dev->dev_port[i] = port; | ||
1989 | } | ||
1990 | |||
1991 | return 0; | ||
1992 | } | ||
1993 | |||
1994 | /* | ||
1995 | * gs_free_ports | ||
1996 | * | ||
1997 | * Free all closed ports. Open ports are disconnected by | ||
1998 | * freeing their write buffers, setting their device pointers | ||
1999 | * and the pointers to them in the device to NULL. These | ||
2000 | * ports will be freed when closed. | ||
2001 | * | ||
2002 | * The device lock is normally held when calling this function. | ||
2003 | */ | ||
2004 | static void gs_free_ports(struct gs_dev *dev) | ||
2005 | { | ||
2006 | int i; | ||
2007 | unsigned long flags; | ||
2008 | struct gs_port *port; | ||
2009 | |||
2010 | if (dev == NULL) | ||
2011 | return; | ||
2012 | |||
2013 | for (i=0; i<GS_NUM_PORTS; i++) { | ||
2014 | if ((port=dev->dev_port[i]) != NULL) { | ||
2015 | dev->dev_port[i] = NULL; | ||
2016 | |||
2017 | spin_lock_irqsave(&port->port_lock, flags); | ||
2018 | |||
2019 | if (port->port_write_buf != NULL) { | ||
2020 | gs_buf_free(port->port_write_buf); | ||
2021 | port->port_write_buf = NULL; | ||
2022 | } | ||
2023 | |||
2024 | if (port->port_open_count > 0 || port->port_in_use) { | ||
2025 | port->port_dev = NULL; | ||
2026 | wake_up_interruptible(&port->port_write_wait); | ||
2027 | if (port->port_tty) { | ||
2028 | tty_hangup(port->port_tty); | ||
2029 | } | ||
2030 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
2031 | } else { | ||
2032 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
2033 | kfree(port); | ||
2034 | } | ||
2035 | |||
2036 | } | ||
2037 | } | ||
2038 | } | ||
2039 | |||
2040 | /*-------------------------------------------------------------------------*/ | ||
2041 | |||
2042 | /* Circular Buffer */ | ||
2043 | |||
2044 | /* | ||
2045 | * gs_buf_alloc | ||
2046 | * | ||
2047 | * Allocate a circular buffer and all associated memory. | ||
2048 | */ | ||
2049 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags) | ||
2050 | { | ||
2051 | struct gs_buf *gb; | ||
2052 | |||
2053 | if (size == 0) | ||
2054 | return NULL; | ||
2055 | |||
2056 | gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags); | ||
2057 | if (gb == NULL) | ||
2058 | return NULL; | ||
2059 | |||
2060 | gb->buf_buf = kmalloc(size, kmalloc_flags); | ||
2061 | if (gb->buf_buf == NULL) { | ||
2062 | kfree(gb); | ||
2063 | return NULL; | ||
2064 | } | ||
2065 | |||
2066 | gb->buf_size = size; | ||
2067 | gb->buf_get = gb->buf_put = gb->buf_buf; | ||
2068 | |||
2069 | return gb; | ||
2070 | } | ||
2071 | |||
2072 | /* | ||
2073 | * gs_buf_free | ||
2074 | * | ||
2075 | * Free the buffer and all associated memory. | ||
2076 | */ | ||
2077 | static void gs_buf_free(struct gs_buf *gb) | ||
2078 | { | ||
2079 | if (gb) { | ||
2080 | kfree(gb->buf_buf); | ||
2081 | kfree(gb); | ||
2082 | } | ||
2083 | } | ||
2084 | |||
2085 | /* | ||
2086 | * gs_buf_clear | ||
2087 | * | ||
2088 | * Clear out all data in the circular buffer. | ||
2089 | */ | ||
2090 | static void gs_buf_clear(struct gs_buf *gb) | ||
2091 | { | ||
2092 | if (gb != NULL) | ||
2093 | gb->buf_get = gb->buf_put; | ||
2094 | /* equivalent to a get of all data available */ | ||
2095 | } | ||
2096 | |||
2097 | /* | ||
2098 | * gs_buf_data_avail | ||
2099 | * | ||
2100 | * Return the number of bytes of data available in the circular | ||
2101 | * buffer. | ||
2102 | */ | ||
2103 | static unsigned int gs_buf_data_avail(struct gs_buf *gb) | ||
2104 | { | ||
2105 | if (gb != NULL) | ||
2106 | return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size; | ||
2107 | else | ||
2108 | return 0; | ||
2109 | } | ||
2110 | |||
2111 | /* | ||
2112 | * gs_buf_space_avail | ||
2113 | * | ||
2114 | * Return the number of bytes of space available in the circular | ||
2115 | * buffer. | ||
2116 | */ | ||
2117 | static unsigned int gs_buf_space_avail(struct gs_buf *gb) | ||
2118 | { | ||
2119 | if (gb != NULL) | ||
2120 | return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size; | ||
2121 | else | ||
2122 | return 0; | ||
2123 | } | ||
2124 | |||
2125 | /* | ||
2126 | * gs_buf_put | ||
2127 | * | ||
2128 | * Copy data data from a user buffer and put it into the circular buffer. | ||
2129 | * Restrict to the amount of space available. | ||
2130 | * | ||
2131 | * Return the number of bytes copied. | ||
2132 | */ | ||
2133 | static unsigned int | ||
2134 | gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count) | ||
2135 | { | ||
2136 | unsigned int len; | ||
2137 | |||
2138 | if (gb == NULL) | ||
2139 | return 0; | ||
2140 | |||
2141 | len = gs_buf_space_avail(gb); | ||
2142 | if (count > len) | ||
2143 | count = len; | ||
2144 | |||
2145 | if (count == 0) | ||
2146 | return 0; | ||
2147 | |||
2148 | len = gb->buf_buf + gb->buf_size - gb->buf_put; | ||
2149 | if (count > len) { | ||
2150 | memcpy(gb->buf_put, buf, len); | ||
2151 | memcpy(gb->buf_buf, buf+len, count - len); | ||
2152 | gb->buf_put = gb->buf_buf + count - len; | ||
2153 | } else { | ||
2154 | memcpy(gb->buf_put, buf, count); | ||
2155 | if (count < len) | ||
2156 | gb->buf_put += count; | ||
2157 | else /* count == len */ | ||
2158 | gb->buf_put = gb->buf_buf; | ||
2159 | } | ||
2160 | |||
2161 | return count; | ||
2162 | } | ||
2163 | |||
2164 | /* | ||
2165 | * gs_buf_get | ||
2166 | * | ||
2167 | * Get data from the circular buffer and copy to the given buffer. | ||
2168 | * Restrict to the amount of data available. | ||
2169 | * | ||
2170 | * Return the number of bytes copied. | ||
2171 | */ | ||
2172 | static unsigned int | ||
2173 | gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count) | ||
2174 | { | ||
2175 | unsigned int len; | ||
2176 | |||
2177 | if (gb == NULL) | ||
2178 | return 0; | ||
2179 | |||
2180 | len = gs_buf_data_avail(gb); | ||
2181 | if (count > len) | ||
2182 | count = len; | ||
2183 | |||
2184 | if (count == 0) | ||
2185 | return 0; | ||
2186 | |||
2187 | len = gb->buf_buf + gb->buf_size - gb->buf_get; | ||
2188 | if (count > len) { | ||
2189 | memcpy(buf, gb->buf_get, len); | ||
2190 | memcpy(buf+len, gb->buf_buf, count - len); | ||
2191 | gb->buf_get = gb->buf_buf + count - len; | ||
2192 | } else { | ||
2193 | memcpy(buf, gb->buf_get, count); | ||
2194 | if (count < len) | ||
2195 | gb->buf_get += count; | ||
2196 | else /* count == len */ | ||
2197 | gb->buf_get = gb->buf_buf; | ||
2198 | } | ||
2199 | |||
2200 | return count; | ||
2201 | } | ||
2202 | |||
2203 | /*-------------------------------------------------------------------------*/ | ||
2204 | |||
2205 | static 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 | */ | ||
2212 | static 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 | } | ||
2259 | module_init(gs_module_init); | ||
2260 | |||
2261 | /* | ||
2262 | * gs_module_exit | ||
2263 | * | ||
2264 | * Unregister as a tty driver and a USB gadget driver. | ||
2265 | */ | ||
2266 | static 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 | } | 251 | } |
2275 | module_exit(gs_module_exit); | 252 | module_exit(cleanup); |
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c new file mode 100644 index 000000000000..5458f43a8668 --- /dev/null +++ b/drivers/usb/gadget/u_ether.c | |||
@@ -0,0 +1,967 @@ | |||
1 | /* | ||
2 | * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack | ||
3 | * | ||
4 | * Copyright (C) 2003-2005,2008 David Brownell | ||
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | ||
6 | * Copyright (C) 2008 Nokia Corporation | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | /* #define VERBOSE_DEBUG */ | ||
24 | |||
25 | #include <linux/kernel.h> | ||
26 | #include <linux/utsname.h> | ||
27 | #include <linux/device.h> | ||
28 | #include <linux/ctype.h> | ||
29 | #include <linux/etherdevice.h> | ||
30 | #include <linux/ethtool.h> | ||
31 | |||
32 | #include "u_ether.h" | ||
33 | |||
34 | |||
35 | /* | ||
36 | * This component encapsulates the Ethernet link glue needed to provide | ||
37 | * one (!) network link through the USB gadget stack, normally "usb0". | ||
38 | * | ||
39 | * The control and data models are handled by the function driver which | ||
40 | * connects to this code; such as CDC Ethernet, "CDC Subset", or RNDIS. | ||
41 | * That includes all descriptor and endpoint management. | ||
42 | * | ||
43 | * Link level addressing is handled by this component using module | ||
44 | * parameters; if no such parameters are provided, random link level | ||
45 | * addresses are used. Each end of the link uses one address. The | ||
46 | * host end address is exported in various ways, and is often recorded | ||
47 | * in configuration databases. | ||
48 | * | ||
49 | * The driver which assembles each configuration using such a link is | ||
50 | * responsible for ensuring that each configuration includes at most one | ||
51 | * instance of is network link. (The network layer provides ways for | ||
52 | * this single "physical" link to be used by multiple virtual links.) | ||
53 | */ | ||
54 | |||
55 | #define DRIVER_VERSION "29-May-2008" | ||
56 | |||
57 | struct eth_dev { | ||
58 | /* lock is held while accessing port_usb | ||
59 | * or updating its backlink port_usb->ioport | ||
60 | */ | ||
61 | spinlock_t lock; | ||
62 | struct gether *port_usb; | ||
63 | |||
64 | struct net_device *net; | ||
65 | struct usb_gadget *gadget; | ||
66 | |||
67 | spinlock_t req_lock; /* guard {rx,tx}_reqs */ | ||
68 | struct list_head tx_reqs, rx_reqs; | ||
69 | atomic_t tx_qlen; | ||
70 | |||
71 | unsigned header_len; | ||
72 | struct sk_buff *(*wrap)(struct sk_buff *skb); | ||
73 | int (*unwrap)(struct sk_buff *skb); | ||
74 | |||
75 | struct work_struct work; | ||
76 | |||
77 | unsigned long todo; | ||
78 | #define WORK_RX_MEMORY 0 | ||
79 | |||
80 | bool zlp; | ||
81 | u8 host_mac[ETH_ALEN]; | ||
82 | }; | ||
83 | |||
84 | /*-------------------------------------------------------------------------*/ | ||
85 | |||
86 | #define RX_EXTRA 20 /* bytes guarding against rx overflows */ | ||
87 | |||
88 | #define DEFAULT_QLEN 2 /* double buffering by default */ | ||
89 | |||
90 | |||
91 | #ifdef CONFIG_USB_GADGET_DUALSPEED | ||
92 | |||
93 | static unsigned qmult = 5; | ||
94 | module_param(qmult, uint, S_IRUGO|S_IWUSR); | ||
95 | MODULE_PARM_DESC(qmult, "queue length multiplier at high speed"); | ||
96 | |||
97 | #else /* full speed (low speed doesn't do bulk) */ | ||
98 | #define qmult 1 | ||
99 | #endif | ||
100 | |||
101 | /* for dual-speed hardware, use deeper queues at highspeed */ | ||
102 | static inline int qlen(struct usb_gadget *gadget) | ||
103 | { | ||
104 | if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH) | ||
105 | return qmult * DEFAULT_QLEN; | ||
106 | else | ||
107 | return DEFAULT_QLEN; | ||
108 | } | ||
109 | |||
110 | /*-------------------------------------------------------------------------*/ | ||
111 | |||
112 | /* REVISIT there must be a better way than having two sets | ||
113 | * of debug calls ... | ||
114 | */ | ||
115 | |||
116 | #undef DBG | ||
117 | #undef VDBG | ||
118 | #undef ERROR | ||
119 | #undef WARN | ||
120 | #undef INFO | ||
121 | |||
122 | #define xprintk(d, level, fmt, args...) \ | ||
123 | printk(level "%s: " fmt , (d)->net->name , ## args) | ||
124 | |||
125 | #ifdef DEBUG | ||
126 | #undef DEBUG | ||
127 | #define DBG(dev, fmt, args...) \ | ||
128 | xprintk(dev , KERN_DEBUG , fmt , ## args) | ||
129 | #else | ||
130 | #define DBG(dev, fmt, args...) \ | ||
131 | do { } while (0) | ||
132 | #endif /* DEBUG */ | ||
133 | |||
134 | #ifdef VERBOSE_DEBUG | ||
135 | #define VDBG DBG | ||
136 | #else | ||
137 | #define VDBG(dev, fmt, args...) \ | ||
138 | do { } while (0) | ||
139 | #endif /* DEBUG */ | ||
140 | |||
141 | #define ERROR(dev, fmt, args...) \ | ||
142 | xprintk(dev , KERN_ERR , fmt , ## args) | ||
143 | #define WARN(dev, fmt, args...) \ | ||
144 | xprintk(dev , KERN_WARNING , fmt , ## args) | ||
145 | #define INFO(dev, fmt, args...) \ | ||
146 | xprintk(dev , KERN_INFO , fmt , ## args) | ||
147 | |||
148 | /*-------------------------------------------------------------------------*/ | ||
149 | |||
150 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ | ||
151 | |||
152 | static int eth_change_mtu(struct net_device *net, int new_mtu) | ||
153 | { | ||
154 | struct eth_dev *dev = netdev_priv(net); | ||
155 | unsigned long flags; | ||
156 | int status = 0; | ||
157 | |||
158 | /* don't change MTU on "live" link (peer won't know) */ | ||
159 | spin_lock_irqsave(&dev->lock, flags); | ||
160 | if (dev->port_usb) | ||
161 | status = -EBUSY; | ||
162 | else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) | ||
163 | status = -ERANGE; | ||
164 | else | ||
165 | net->mtu = new_mtu; | ||
166 | spin_unlock_irqrestore(&dev->lock, flags); | ||
167 | |||
168 | return status; | ||
169 | } | ||
170 | |||
171 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) | ||
172 | { | ||
173 | struct eth_dev *dev = netdev_priv(net); | ||
174 | |||
175 | strlcpy(p->driver, "g_ether", sizeof p->driver); | ||
176 | strlcpy(p->version, DRIVER_VERSION, sizeof p->version); | ||
177 | strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version); | ||
178 | strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info); | ||
179 | } | ||
180 | |||
181 | static u32 eth_get_link(struct net_device *net) | ||
182 | { | ||
183 | struct eth_dev *dev = netdev_priv(net); | ||
184 | return dev->gadget->speed != USB_SPEED_UNKNOWN; | ||
185 | } | ||
186 | |||
187 | /* REVISIT can also support: | ||
188 | * - WOL (by tracking suspends and issuing remote wakeup) | ||
189 | * - msglevel (implies updated messaging) | ||
190 | * - ... probably more ethtool ops | ||
191 | */ | ||
192 | |||
193 | static struct ethtool_ops ops = { | ||
194 | .get_drvinfo = eth_get_drvinfo, | ||
195 | .get_link = eth_get_link | ||
196 | }; | ||
197 | |||
198 | static void defer_kevent(struct eth_dev *dev, int flag) | ||
199 | { | ||
200 | if (test_and_set_bit(flag, &dev->todo)) | ||
201 | return; | ||
202 | if (!schedule_work(&dev->work)) | ||
203 | ERROR(dev, "kevent %d may have been dropped\n", flag); | ||
204 | else | ||
205 | DBG(dev, "kevent %d scheduled\n", flag); | ||
206 | } | ||
207 | |||
208 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); | ||
209 | |||
210 | static int | ||
211 | rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) | ||
212 | { | ||
213 | struct sk_buff *skb; | ||
214 | int retval = -ENOMEM; | ||
215 | size_t size = 0; | ||
216 | struct usb_ep *out; | ||
217 | unsigned long flags; | ||
218 | |||
219 | spin_lock_irqsave(&dev->lock, flags); | ||
220 | if (dev->port_usb) | ||
221 | out = dev->port_usb->out_ep; | ||
222 | else | ||
223 | out = NULL; | ||
224 | spin_unlock_irqrestore(&dev->lock, flags); | ||
225 | |||
226 | if (!out) | ||
227 | return -ENOTCONN; | ||
228 | |||
229 | |||
230 | /* Padding up to RX_EXTRA handles minor disagreements with host. | ||
231 | * Normally we use the USB "terminate on short read" convention; | ||
232 | * so allow up to (N*maxpacket), since that memory is normally | ||
233 | * already allocated. Some hardware doesn't deal well with short | ||
234 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a | ||
235 | * byte off the end (to force hardware errors on overflow). | ||
236 | * | ||
237 | * RNDIS uses internal framing, and explicitly allows senders to | ||
238 | * pad to end-of-packet. That's potentially nice for speed, but | ||
239 | * means receivers can't recover lost synch on their own (because | ||
240 | * new packets don't only start after a short RX). | ||
241 | */ | ||
242 | size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA; | ||
243 | size += dev->port_usb->header_len; | ||
244 | size += out->maxpacket - 1; | ||
245 | size -= size % out->maxpacket; | ||
246 | |||
247 | skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); | ||
248 | if (skb == NULL) { | ||
249 | DBG(dev, "no rx skb\n"); | ||
250 | goto enomem; | ||
251 | } | ||
252 | |||
253 | /* Some platforms perform better when IP packets are aligned, | ||
254 | * but on at least one, checksumming fails otherwise. Note: | ||
255 | * RNDIS headers involve variable numbers of LE32 values. | ||
256 | */ | ||
257 | skb_reserve(skb, NET_IP_ALIGN); | ||
258 | |||
259 | req->buf = skb->data; | ||
260 | req->length = size; | ||
261 | req->complete = rx_complete; | ||
262 | req->context = skb; | ||
263 | |||
264 | retval = usb_ep_queue(out, req, gfp_flags); | ||
265 | if (retval == -ENOMEM) | ||
266 | enomem: | ||
267 | defer_kevent(dev, WORK_RX_MEMORY); | ||
268 | if (retval) { | ||
269 | DBG(dev, "rx submit --> %d\n", retval); | ||
270 | if (skb) | ||
271 | dev_kfree_skb_any(skb); | ||
272 | spin_lock_irqsave(&dev->req_lock, flags); | ||
273 | list_add(&req->list, &dev->rx_reqs); | ||
274 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
275 | } | ||
276 | return retval; | ||
277 | } | ||
278 | |||
279 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) | ||
280 | { | ||
281 | struct sk_buff *skb = req->context; | ||
282 | struct eth_dev *dev = ep->driver_data; | ||
283 | int status = req->status; | ||
284 | |||
285 | switch (status) { | ||
286 | |||
287 | /* normal completion */ | ||
288 | case 0: | ||
289 | skb_put(skb, req->actual); | ||
290 | if (dev->unwrap) | ||
291 | status = dev->unwrap(skb); | ||
292 | if (status < 0 | ||
293 | || ETH_HLEN > skb->len | ||
294 | || skb->len > ETH_FRAME_LEN) { | ||
295 | dev->net->stats.rx_errors++; | ||
296 | dev->net->stats.rx_length_errors++; | ||
297 | DBG(dev, "rx length %d\n", skb->len); | ||
298 | break; | ||
299 | } | ||
300 | |||
301 | skb->protocol = eth_type_trans(skb, dev->net); | ||
302 | dev->net->stats.rx_packets++; | ||
303 | dev->net->stats.rx_bytes += skb->len; | ||
304 | |||
305 | /* no buffer copies needed, unless hardware can't | ||
306 | * use skb buffers. | ||
307 | */ | ||
308 | status = netif_rx(skb); | ||
309 | skb = NULL; | ||
310 | break; | ||
311 | |||
312 | /* software-driven interface shutdown */ | ||
313 | case -ECONNRESET: /* unlink */ | ||
314 | case -ESHUTDOWN: /* disconnect etc */ | ||
315 | VDBG(dev, "rx shutdown, code %d\n", status); | ||
316 | goto quiesce; | ||
317 | |||
318 | /* for hardware automagic (such as pxa) */ | ||
319 | case -ECONNABORTED: /* endpoint reset */ | ||
320 | DBG(dev, "rx %s reset\n", ep->name); | ||
321 | defer_kevent(dev, WORK_RX_MEMORY); | ||
322 | quiesce: | ||
323 | dev_kfree_skb_any(skb); | ||
324 | goto clean; | ||
325 | |||
326 | /* data overrun */ | ||
327 | case -EOVERFLOW: | ||
328 | dev->net->stats.rx_over_errors++; | ||
329 | /* FALLTHROUGH */ | ||
330 | |||
331 | default: | ||
332 | dev->net->stats.rx_errors++; | ||
333 | DBG(dev, "rx status %d\n", status); | ||
334 | break; | ||
335 | } | ||
336 | |||
337 | if (skb) | ||
338 | dev_kfree_skb_any(skb); | ||
339 | if (!netif_running(dev->net)) { | ||
340 | clean: | ||
341 | spin_lock(&dev->req_lock); | ||
342 | list_add(&req->list, &dev->rx_reqs); | ||
343 | spin_unlock(&dev->req_lock); | ||
344 | req = NULL; | ||
345 | } | ||
346 | if (req) | ||
347 | rx_submit(dev, req, GFP_ATOMIC); | ||
348 | } | ||
349 | |||
350 | static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n) | ||
351 | { | ||
352 | unsigned i; | ||
353 | struct usb_request *req; | ||
354 | |||
355 | if (!n) | ||
356 | return -ENOMEM; | ||
357 | |||
358 | /* queue/recycle up to N requests */ | ||
359 | i = n; | ||
360 | list_for_each_entry(req, list, list) { | ||
361 | if (i-- == 0) | ||
362 | goto extra; | ||
363 | } | ||
364 | while (i--) { | ||
365 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); | ||
366 | if (!req) | ||
367 | return list_empty(list) ? -ENOMEM : 0; | ||
368 | list_add(&req->list, list); | ||
369 | } | ||
370 | return 0; | ||
371 | |||
372 | extra: | ||
373 | /* free extras */ | ||
374 | for (;;) { | ||
375 | struct list_head *next; | ||
376 | |||
377 | next = req->list.next; | ||
378 | list_del(&req->list); | ||
379 | usb_ep_free_request(ep, req); | ||
380 | |||
381 | if (next == list) | ||
382 | break; | ||
383 | |||
384 | req = container_of(next, struct usb_request, list); | ||
385 | } | ||
386 | return 0; | ||
387 | } | ||
388 | |||
389 | static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n) | ||
390 | { | ||
391 | int status; | ||
392 | |||
393 | spin_lock(&dev->req_lock); | ||
394 | status = prealloc(&dev->tx_reqs, link->in_ep, n); | ||
395 | if (status < 0) | ||
396 | goto fail; | ||
397 | status = prealloc(&dev->rx_reqs, link->out_ep, n); | ||
398 | if (status < 0) | ||
399 | goto fail; | ||
400 | goto done; | ||
401 | fail: | ||
402 | DBG(dev, "can't alloc requests\n"); | ||
403 | done: | ||
404 | spin_unlock(&dev->req_lock); | ||
405 | return status; | ||
406 | } | ||
407 | |||
408 | static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags) | ||
409 | { | ||
410 | struct usb_request *req; | ||
411 | unsigned long flags; | ||
412 | |||
413 | /* fill unused rxq slots with some skb */ | ||
414 | spin_lock_irqsave(&dev->req_lock, flags); | ||
415 | while (!list_empty(&dev->rx_reqs)) { | ||
416 | req = container_of(dev->rx_reqs.next, | ||
417 | struct usb_request, list); | ||
418 | list_del_init(&req->list); | ||
419 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
420 | |||
421 | if (rx_submit(dev, req, gfp_flags) < 0) { | ||
422 | defer_kevent(dev, WORK_RX_MEMORY); | ||
423 | return; | ||
424 | } | ||
425 | |||
426 | spin_lock_irqsave(&dev->req_lock, flags); | ||
427 | } | ||
428 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
429 | } | ||
430 | |||
431 | static void eth_work(struct work_struct *work) | ||
432 | { | ||
433 | struct eth_dev *dev = container_of(work, struct eth_dev, work); | ||
434 | |||
435 | if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) { | ||
436 | if (netif_running(dev->net)) | ||
437 | rx_fill(dev, GFP_KERNEL); | ||
438 | } | ||
439 | |||
440 | if (dev->todo) | ||
441 | DBG(dev, "work done, flags = 0x%lx\n", dev->todo); | ||
442 | } | ||
443 | |||
444 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) | ||
445 | { | ||
446 | struct sk_buff *skb = req->context; | ||
447 | struct eth_dev *dev = ep->driver_data; | ||
448 | |||
449 | switch (req->status) { | ||
450 | default: | ||
451 | dev->net->stats.tx_errors++; | ||
452 | VDBG(dev, "tx err %d\n", req->status); | ||
453 | /* FALLTHROUGH */ | ||
454 | case -ECONNRESET: /* unlink */ | ||
455 | case -ESHUTDOWN: /* disconnect etc */ | ||
456 | break; | ||
457 | case 0: | ||
458 | dev->net->stats.tx_bytes += skb->len; | ||
459 | } | ||
460 | dev->net->stats.tx_packets++; | ||
461 | |||
462 | spin_lock(&dev->req_lock); | ||
463 | list_add(&req->list, &dev->tx_reqs); | ||
464 | spin_unlock(&dev->req_lock); | ||
465 | dev_kfree_skb_any(skb); | ||
466 | |||
467 | atomic_dec(&dev->tx_qlen); | ||
468 | if (netif_carrier_ok(dev->net)) | ||
469 | netif_wake_queue(dev->net); | ||
470 | } | ||
471 | |||
472 | static inline int is_promisc(u16 cdc_filter) | ||
473 | { | ||
474 | return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; | ||
475 | } | ||
476 | |||
477 | static int eth_start_xmit(struct sk_buff *skb, struct net_device *net) | ||
478 | { | ||
479 | struct eth_dev *dev = netdev_priv(net); | ||
480 | int length = skb->len; | ||
481 | int retval; | ||
482 | struct usb_request *req = NULL; | ||
483 | unsigned long flags; | ||
484 | struct usb_ep *in; | ||
485 | u16 cdc_filter; | ||
486 | |||
487 | spin_lock_irqsave(&dev->lock, flags); | ||
488 | if (dev->port_usb) { | ||
489 | in = dev->port_usb->in_ep; | ||
490 | cdc_filter = dev->port_usb->cdc_filter; | ||
491 | } else { | ||
492 | in = NULL; | ||
493 | cdc_filter = 0; | ||
494 | } | ||
495 | spin_unlock_irqrestore(&dev->lock, flags); | ||
496 | |||
497 | if (!in) { | ||
498 | dev_kfree_skb_any(skb); | ||
499 | return 0; | ||
500 | } | ||
501 | |||
502 | /* apply outgoing CDC or RNDIS filters */ | ||
503 | if (!is_promisc(cdc_filter)) { | ||
504 | u8 *dest = skb->data; | ||
505 | |||
506 | if (is_multicast_ether_addr(dest)) { | ||
507 | u16 type; | ||
508 | |||
509 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host | ||
510 | * SET_ETHERNET_MULTICAST_FILTERS requests | ||
511 | */ | ||
512 | if (is_broadcast_ether_addr(dest)) | ||
513 | type = USB_CDC_PACKET_TYPE_BROADCAST; | ||
514 | else | ||
515 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; | ||
516 | if (!(cdc_filter & type)) { | ||
517 | dev_kfree_skb_any(skb); | ||
518 | return 0; | ||
519 | } | ||
520 | } | ||
521 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ | ||
522 | } | ||
523 | |||
524 | spin_lock_irqsave(&dev->req_lock, flags); | ||
525 | /* | ||
526 | * this freelist can be empty if an interrupt triggered disconnect() | ||
527 | * and reconfigured the gadget (shutting down this queue) after the | ||
528 | * network stack decided to xmit but before we got the spinlock. | ||
529 | */ | ||
530 | if (list_empty(&dev->tx_reqs)) { | ||
531 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
532 | return 1; | ||
533 | } | ||
534 | |||
535 | req = container_of(dev->tx_reqs.next, struct usb_request, list); | ||
536 | list_del(&req->list); | ||
537 | |||
538 | /* temporarily stop TX queue when the freelist empties */ | ||
539 | if (list_empty(&dev->tx_reqs)) | ||
540 | netif_stop_queue(net); | ||
541 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
542 | |||
543 | /* no buffer copies needed, unless the network stack did it | ||
544 | * or the hardware can't use skb buffers. | ||
545 | * or there's not enough space for extra headers we need | ||
546 | */ | ||
547 | if (dev->wrap) { | ||
548 | struct sk_buff *skb_new; | ||
549 | |||
550 | skb_new = dev->wrap(skb); | ||
551 | if (!skb_new) | ||
552 | goto drop; | ||
553 | |||
554 | dev_kfree_skb_any(skb); | ||
555 | skb = skb_new; | ||
556 | length = skb->len; | ||
557 | } | ||
558 | req->buf = skb->data; | ||
559 | req->context = skb; | ||
560 | req->complete = tx_complete; | ||
561 | |||
562 | /* use zlp framing on tx for strict CDC-Ether conformance, | ||
563 | * though any robust network rx path ignores extra padding. | ||
564 | * and some hardware doesn't like to write zlps. | ||
565 | */ | ||
566 | req->zero = 1; | ||
567 | if (!dev->zlp && (length % in->maxpacket) == 0) | ||
568 | length++; | ||
569 | |||
570 | req->length = length; | ||
571 | |||
572 | /* throttle highspeed IRQ rate back slightly */ | ||
573 | if (gadget_is_dualspeed(dev->gadget)) | ||
574 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) | ||
575 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) | ||
576 | : 0; | ||
577 | |||
578 | retval = usb_ep_queue(in, req, GFP_ATOMIC); | ||
579 | switch (retval) { | ||
580 | default: | ||
581 | DBG(dev, "tx queue err %d\n", retval); | ||
582 | break; | ||
583 | case 0: | ||
584 | net->trans_start = jiffies; | ||
585 | atomic_inc(&dev->tx_qlen); | ||
586 | } | ||
587 | |||
588 | if (retval) { | ||
589 | drop: | ||
590 | dev->net->stats.tx_dropped++; | ||
591 | dev_kfree_skb_any(skb); | ||
592 | spin_lock_irqsave(&dev->req_lock, flags); | ||
593 | if (list_empty(&dev->tx_reqs)) | ||
594 | netif_start_queue(net); | ||
595 | list_add(&req->list, &dev->tx_reqs); | ||
596 | spin_unlock_irqrestore(&dev->req_lock, flags); | ||
597 | } | ||
598 | return 0; | ||
599 | } | ||
600 | |||
601 | /*-------------------------------------------------------------------------*/ | ||
602 | |||
603 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) | ||
604 | { | ||
605 | DBG(dev, "%s\n", __func__); | ||
606 | |||
607 | /* fill the rx queue */ | ||
608 | rx_fill(dev, gfp_flags); | ||
609 | |||
610 | /* and open the tx floodgates */ | ||
611 | atomic_set(&dev->tx_qlen, 0); | ||
612 | netif_wake_queue(dev->net); | ||
613 | } | ||
614 | |||
615 | static int eth_open(struct net_device *net) | ||
616 | { | ||
617 | struct eth_dev *dev = netdev_priv(net); | ||
618 | struct gether *link; | ||
619 | |||
620 | DBG(dev, "%s\n", __func__); | ||
621 | if (netif_carrier_ok(dev->net)) | ||
622 | eth_start(dev, GFP_KERNEL); | ||
623 | |||
624 | spin_lock_irq(&dev->lock); | ||
625 | link = dev->port_usb; | ||
626 | if (link && link->open) | ||
627 | link->open(link); | ||
628 | spin_unlock_irq(&dev->lock); | ||
629 | |||
630 | return 0; | ||
631 | } | ||
632 | |||
633 | static int eth_stop(struct net_device *net) | ||
634 | { | ||
635 | struct eth_dev *dev = netdev_priv(net); | ||
636 | unsigned long flags; | ||
637 | |||
638 | VDBG(dev, "%s\n", __func__); | ||
639 | netif_stop_queue(net); | ||
640 | |||
641 | DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", | ||
642 | dev->net->stats.rx_packets, dev->net->stats.tx_packets, | ||
643 | dev->net->stats.rx_errors, dev->net->stats.tx_errors | ||
644 | ); | ||
645 | |||
646 | /* ensure there are no more active requests */ | ||
647 | spin_lock_irqsave(&dev->lock, flags); | ||
648 | if (dev->port_usb) { | ||
649 | struct gether *link = dev->port_usb; | ||
650 | |||
651 | if (link->close) | ||
652 | link->close(link); | ||
653 | |||
654 | /* NOTE: we have no abort-queue primitive we could use | ||
655 | * to cancel all pending I/O. Instead, we disable then | ||
656 | * reenable the endpoints ... this idiom may leave toggle | ||
657 | * wrong, but that's a self-correcting error. | ||
658 | * | ||
659 | * REVISIT: we *COULD* just let the transfers complete at | ||
660 | * their own pace; the network stack can handle old packets. | ||
661 | * For the moment we leave this here, since it works. | ||
662 | */ | ||
663 | usb_ep_disable(link->in_ep); | ||
664 | usb_ep_disable(link->out_ep); | ||
665 | if (netif_carrier_ok(net)) { | ||
666 | DBG(dev, "host still using in/out endpoints\n"); | ||
667 | usb_ep_enable(link->in_ep, link->in); | ||
668 | usb_ep_enable(link->out_ep, link->out); | ||
669 | } | ||
670 | } | ||
671 | spin_unlock_irqrestore(&dev->lock, flags); | ||
672 | |||
673 | return 0; | ||
674 | } | ||
675 | |||
676 | /*-------------------------------------------------------------------------*/ | ||
677 | |||
678 | /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ | ||
679 | static char *dev_addr; | ||
680 | module_param(dev_addr, charp, S_IRUGO); | ||
681 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); | ||
682 | |||
683 | /* this address is invisible to ifconfig */ | ||
684 | static char *host_addr; | ||
685 | module_param(host_addr, charp, S_IRUGO); | ||
686 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); | ||
687 | |||
688 | |||
689 | static u8 __init nibble(unsigned char c) | ||
690 | { | ||
691 | if (isdigit(c)) | ||
692 | return c - '0'; | ||
693 | c = toupper(c); | ||
694 | if (isxdigit(c)) | ||
695 | return 10 + c - 'A'; | ||
696 | return 0; | ||
697 | } | ||
698 | |||
699 | static int __init get_ether_addr(const char *str, u8 *dev_addr) | ||
700 | { | ||
701 | if (str) { | ||
702 | unsigned i; | ||
703 | |||
704 | for (i = 0; i < 6; i++) { | ||
705 | unsigned char num; | ||
706 | |||
707 | if ((*str == '.') || (*str == ':')) | ||
708 | str++; | ||
709 | num = nibble(*str++) << 4; | ||
710 | num |= (nibble(*str++)); | ||
711 | dev_addr [i] = num; | ||
712 | } | ||
713 | if (is_valid_ether_addr(dev_addr)) | ||
714 | return 0; | ||
715 | } | ||
716 | random_ether_addr(dev_addr); | ||
717 | return 1; | ||
718 | } | ||
719 | |||
720 | static struct eth_dev *the_dev; | ||
721 | |||
722 | |||
723 | /** | ||
724 | * gether_setup - initialize one ethernet-over-usb link | ||
725 | * @g: gadget to associated with these links | ||
726 | * @ethaddr: NULL, or a buffer in which the ethernet address of the | ||
727 | * host side of the link is recorded | ||
728 | * Context: may sleep | ||
729 | * | ||
730 | * This sets up the single network link that may be exported by a | ||
731 | * gadget driver using this framework. The link layer addresses are | ||
732 | * set up using module parameters. | ||
733 | * | ||
734 | * Returns negative errno, or zero on success | ||
735 | */ | ||
736 | int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN]) | ||
737 | { | ||
738 | struct eth_dev *dev; | ||
739 | struct net_device *net; | ||
740 | int status; | ||
741 | |||
742 | if (the_dev) | ||
743 | return -EBUSY; | ||
744 | |||
745 | net = alloc_etherdev(sizeof *dev); | ||
746 | if (!net) | ||
747 | return -ENOMEM; | ||
748 | |||
749 | dev = netdev_priv(net); | ||
750 | spin_lock_init(&dev->lock); | ||
751 | spin_lock_init(&dev->req_lock); | ||
752 | INIT_WORK(&dev->work, eth_work); | ||
753 | INIT_LIST_HEAD(&dev->tx_reqs); | ||
754 | INIT_LIST_HEAD(&dev->rx_reqs); | ||
755 | |||
756 | /* network device setup */ | ||
757 | dev->net = net; | ||
758 | strcpy(net->name, "usb%d"); | ||
759 | |||
760 | if (get_ether_addr(dev_addr, net->dev_addr)) | ||
761 | dev_warn(&g->dev, | ||
762 | "using random %s ethernet address\n", "self"); | ||
763 | if (get_ether_addr(host_addr, dev->host_mac)) | ||
764 | dev_warn(&g->dev, | ||
765 | "using random %s ethernet address\n", "host"); | ||
766 | |||
767 | if (ethaddr) | ||
768 | memcpy(ethaddr, dev->host_mac, ETH_ALEN); | ||
769 | |||
770 | net->change_mtu = eth_change_mtu; | ||
771 | net->hard_start_xmit = eth_start_xmit; | ||
772 | net->open = eth_open; | ||
773 | net->stop = eth_stop; | ||
774 | /* watchdog_timeo, tx_timeout ... */ | ||
775 | /* set_multicast_list */ | ||
776 | SET_ETHTOOL_OPS(net, &ops); | ||
777 | |||
778 | /* two kinds of host-initiated state changes: | ||
779 | * - iff DATA transfer is active, carrier is "on" | ||
780 | * - tx queueing enabled if open *and* carrier is "on" | ||
781 | */ | ||
782 | netif_stop_queue(net); | ||
783 | netif_carrier_off(net); | ||
784 | |||
785 | dev->gadget = g; | ||
786 | SET_NETDEV_DEV(net, &g->dev); | ||
787 | |||
788 | status = register_netdev(net); | ||
789 | if (status < 0) { | ||
790 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); | ||
791 | free_netdev(net); | ||
792 | } else { | ||
793 | DECLARE_MAC_BUF(tmp); | ||
794 | |||
795 | INFO(dev, "MAC %s\n", print_mac(tmp, net->dev_addr)); | ||
796 | INFO(dev, "HOST MAC %s\n", print_mac(tmp, dev->host_mac)); | ||
797 | |||
798 | the_dev = dev; | ||
799 | } | ||
800 | |||
801 | return status; | ||
802 | } | ||
803 | |||
804 | /** | ||
805 | * gether_cleanup - remove Ethernet-over-USB device | ||
806 | * Context: may sleep | ||
807 | * | ||
808 | * This is called to free all resources allocated by @gether_setup(). | ||
809 | */ | ||
810 | void gether_cleanup(void) | ||
811 | { | ||
812 | if (!the_dev) | ||
813 | return; | ||
814 | |||
815 | unregister_netdev(the_dev->net); | ||
816 | free_netdev(the_dev->net); | ||
817 | |||
818 | /* assuming we used keventd, it must quiesce too */ | ||
819 | flush_scheduled_work(); | ||
820 | |||
821 | the_dev = NULL; | ||
822 | } | ||
823 | |||
824 | |||
825 | /** | ||
826 | * gether_connect - notify network layer that USB link is active | ||
827 | * @link: the USB link, set up with endpoints, descriptors matching | ||
828 | * current device speed, and any framing wrapper(s) set up. | ||
829 | * Context: irqs blocked | ||
830 | * | ||
831 | * This is called to activate endpoints and let the network layer know | ||
832 | * the connection is active ("carrier detect"). It may cause the I/O | ||
833 | * queues to open and start letting network packets flow, but will in | ||
834 | * any case activate the endpoints so that they respond properly to the | ||
835 | * USB host. | ||
836 | * | ||
837 | * Verify net_device pointer returned using IS_ERR(). If it doesn't | ||
838 | * indicate some error code (negative errno), ep->driver_data values | ||
839 | * have been overwritten. | ||
840 | */ | ||
841 | struct net_device *gether_connect(struct gether *link) | ||
842 | { | ||
843 | struct eth_dev *dev = the_dev; | ||
844 | int result = 0; | ||
845 | |||
846 | if (!dev) | ||
847 | return ERR_PTR(-EINVAL); | ||
848 | |||
849 | link->in_ep->driver_data = dev; | ||
850 | result = usb_ep_enable(link->in_ep, link->in); | ||
851 | if (result != 0) { | ||
852 | DBG(dev, "enable %s --> %d\n", | ||
853 | link->in_ep->name, result); | ||
854 | goto fail0; | ||
855 | } | ||
856 | |||
857 | link->out_ep->driver_data = dev; | ||
858 | result = usb_ep_enable(link->out_ep, link->out); | ||
859 | if (result != 0) { | ||
860 | DBG(dev, "enable %s --> %d\n", | ||
861 | link->out_ep->name, result); | ||
862 | goto fail1; | ||
863 | } | ||
864 | |||
865 | if (result == 0) | ||
866 | result = alloc_requests(dev, link, qlen(dev->gadget)); | ||
867 | |||
868 | if (result == 0) { | ||
869 | dev->zlp = link->is_zlp_ok; | ||
870 | DBG(dev, "qlen %d\n", qlen(dev->gadget)); | ||
871 | |||
872 | dev->header_len = link->header_len; | ||
873 | dev->unwrap = link->unwrap; | ||
874 | dev->wrap = link->wrap; | ||
875 | |||
876 | spin_lock(&dev->lock); | ||
877 | dev->port_usb = link; | ||
878 | link->ioport = dev; | ||
879 | spin_unlock(&dev->lock); | ||
880 | |||
881 | netif_carrier_on(dev->net); | ||
882 | if (netif_running(dev->net)) | ||
883 | eth_start(dev, GFP_ATOMIC); | ||
884 | |||
885 | /* on error, disable any endpoints */ | ||
886 | } else { | ||
887 | (void) usb_ep_disable(link->out_ep); | ||
888 | fail1: | ||
889 | (void) usb_ep_disable(link->in_ep); | ||
890 | } | ||
891 | fail0: | ||
892 | /* caller is responsible for cleanup on error */ | ||
893 | if (result < 0) | ||
894 | return ERR_PTR(result); | ||
895 | return dev->net; | ||
896 | } | ||
897 | |||
898 | /** | ||
899 | * gether_disconnect - notify network layer that USB link is inactive | ||
900 | * @link: the USB link, on which gether_connect() was called | ||
901 | * Context: irqs blocked | ||
902 | * | ||
903 | * This is called to deactivate endpoints and let the network layer know | ||
904 | * the connection went inactive ("no carrier"). | ||
905 | * | ||
906 | * On return, the state is as if gether_connect() had never been called. | ||
907 | * The endpoints are inactive, and accordingly without active USB I/O. | ||
908 | * Pointers to endpoint descriptors and endpoint private data are nulled. | ||
909 | */ | ||
910 | void gether_disconnect(struct gether *link) | ||
911 | { | ||
912 | struct eth_dev *dev = link->ioport; | ||
913 | struct usb_request *req; | ||
914 | |||
915 | WARN_ON(!dev); | ||
916 | if (!dev) | ||
917 | return; | ||
918 | |||
919 | DBG(dev, "%s\n", __func__); | ||
920 | |||
921 | netif_stop_queue(dev->net); | ||
922 | netif_carrier_off(dev->net); | ||
923 | |||
924 | /* disable endpoints, forcing (synchronous) completion | ||
925 | * of all pending i/o. then free the request objects | ||
926 | * and forget about the endpoints. | ||
927 | */ | ||
928 | usb_ep_disable(link->in_ep); | ||
929 | spin_lock(&dev->req_lock); | ||
930 | while (!list_empty(&dev->tx_reqs)) { | ||
931 | req = container_of(dev->tx_reqs.next, | ||
932 | struct usb_request, list); | ||
933 | list_del(&req->list); | ||
934 | |||
935 | spin_unlock(&dev->req_lock); | ||
936 | usb_ep_free_request(link->in_ep, req); | ||
937 | spin_lock(&dev->req_lock); | ||
938 | } | ||
939 | spin_unlock(&dev->req_lock); | ||
940 | link->in_ep->driver_data = NULL; | ||
941 | link->in = NULL; | ||
942 | |||
943 | usb_ep_disable(link->out_ep); | ||
944 | spin_lock(&dev->req_lock); | ||
945 | while (!list_empty(&dev->rx_reqs)) { | ||
946 | req = container_of(dev->rx_reqs.next, | ||
947 | struct usb_request, list); | ||
948 | list_del(&req->list); | ||
949 | |||
950 | spin_unlock(&dev->req_lock); | ||
951 | usb_ep_free_request(link->out_ep, req); | ||
952 | spin_lock(&dev->req_lock); | ||
953 | } | ||
954 | spin_unlock(&dev->req_lock); | ||
955 | link->out_ep->driver_data = NULL; | ||
956 | link->out = NULL; | ||
957 | |||
958 | /* finish forgetting about this USB link episode */ | ||
959 | dev->header_len = 0; | ||
960 | dev->unwrap = NULL; | ||
961 | dev->wrap = NULL; | ||
962 | |||
963 | spin_lock(&dev->lock); | ||
964 | dev->port_usb = NULL; | ||
965 | link->ioport = NULL; | ||
966 | spin_unlock(&dev->lock); | ||
967 | } | ||
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h new file mode 100644 index 000000000000..0d1f7ae3b071 --- /dev/null +++ b/drivers/usb/gadget/u_ether.h | |||
@@ -0,0 +1,127 @@ | |||
1 | /* | ||
2 | * u_ether.h -- interface to USB gadget "ethernet link" utilities | ||
3 | * | ||
4 | * Copyright (C) 2003-2005,2008 David Brownell | ||
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | ||
6 | * Copyright (C) 2008 Nokia Corporation | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | #ifndef __U_ETHER_H | ||
24 | #define __U_ETHER_H | ||
25 | |||
26 | #include <linux/err.h> | ||
27 | #include <linux/if_ether.h> | ||
28 | #include <linux/usb/composite.h> | ||
29 | #include <linux/usb/cdc.h> | ||
30 | |||
31 | #include "gadget_chips.h" | ||
32 | |||
33 | |||
34 | /* | ||
35 | * This represents the USB side of an "ethernet" link, managed by a USB | ||
36 | * function which provides control and (maybe) framing. Two functions | ||
37 | * in different configurations could share the same ethernet link/netdev, | ||
38 | * using different host interaction models. | ||
39 | * | ||
40 | * There is a current limitation that only one instance of this link may | ||
41 | * be present in any given configuration. When that's a problem, network | ||
42 | * layer facilities can be used to package multiple logical links on this | ||
43 | * single "physical" one. | ||
44 | */ | ||
45 | struct gether { | ||
46 | struct usb_function func; | ||
47 | |||
48 | /* updated by gether_{connect,disconnect} */ | ||
49 | struct eth_dev *ioport; | ||
50 | |||
51 | /* endpoints handle full and/or high speeds */ | ||
52 | struct usb_ep *in_ep; | ||
53 | struct usb_ep *out_ep; | ||
54 | |||
55 | /* descriptors match device speed at gether_connect() time */ | ||
56 | struct usb_endpoint_descriptor *in; | ||
57 | struct usb_endpoint_descriptor *out; | ||
58 | |||
59 | bool is_zlp_ok; | ||
60 | |||
61 | u16 cdc_filter; | ||
62 | |||
63 | /* hooks for added framing, as needed for RNDIS and EEM. | ||
64 | * we currently don't support multiple frames per SKB. | ||
65 | */ | ||
66 | u32 header_len; | ||
67 | struct sk_buff *(*wrap)(struct sk_buff *skb); | ||
68 | int (*unwrap)(struct sk_buff *skb); | ||
69 | |||
70 | /* called on network open/close */ | ||
71 | void (*open)(struct gether *); | ||
72 | void (*close)(struct gether *); | ||
73 | }; | ||
74 | |||
75 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ | ||
76 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ | ||
77 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ | ||
78 | |USB_CDC_PACKET_TYPE_DIRECTED) | ||
79 | |||
80 | |||
81 | /* netdev setup/teardown as directed by the gadget driver */ | ||
82 | int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN]); | ||
83 | void gether_cleanup(void); | ||
84 | |||
85 | /* connect/disconnect is handled by individual functions */ | ||
86 | struct net_device *gether_connect(struct gether *); | ||
87 | void gether_disconnect(struct gether *); | ||
88 | |||
89 | /* Some controllers can't support CDC Ethernet (ECM) ... */ | ||
90 | static inline bool can_support_ecm(struct usb_gadget *gadget) | ||
91 | { | ||
92 | if (!gadget_supports_altsettings(gadget)) | ||
93 | return false; | ||
94 | |||
95 | /* SA1100 can do ECM, *without* status endpoint ... but we'll | ||
96 | * only use it in non-ECM mode for backwards compatibility | ||
97 | * (and since we currently require a status endpoint) | ||
98 | */ | ||
99 | if (gadget_is_sa1100(gadget)) | ||
100 | return false; | ||
101 | |||
102 | /* Everything else is *presumably* fine ... but this is a bit | ||
103 | * chancy, so be **CERTAIN** there are no hardware issues with | ||
104 | * your controller. Add it above if it can't handle CDC. | ||
105 | */ | ||
106 | return true; | ||
107 | } | ||
108 | |||
109 | /* each configuration may bind one instance of an ethernet link */ | ||
110 | int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); | ||
111 | int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); | ||
112 | |||
113 | #ifdef CONFIG_USB_ETH_RNDIS | ||
114 | |||
115 | int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); | ||
116 | |||
117 | #else | ||
118 | |||
119 | static inline int | ||
120 | rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | ||
121 | { | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | #endif | ||
126 | |||
127 | #endif /* __U_ETHER_H */ | ||
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c new file mode 100644 index 000000000000..abf9505d3a75 --- /dev/null +++ b/drivers/usb/gadget/u_serial.c | |||
@@ -0,0 +1,1246 @@ | |||
1 | /* | ||
2 | * u_serial.c - utilities for USB gadget "serial port"/TTY support | ||
3 | * | ||
4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) | ||
5 | * Copyright (C) 2008 David Brownell | ||
6 | * Copyright (C) 2008 by Nokia Corporation | ||
7 | * | ||
8 | * This code also borrows from usbserial.c, which is | ||
9 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | ||
10 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) | ||
11 | * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) | ||
12 | * | ||
13 | * This software is distributed under the terms of the GNU General | ||
14 | * Public License ("GPL") as published by the Free Software Foundation, | ||
15 | * either version 2 of that License or (at your option) any later version. | ||
16 | */ | ||
17 | |||
18 | /* #define VERBOSE_DEBUG */ | ||
19 | |||
20 | #include <linux/kernel.h> | ||
21 | #include <linux/interrupt.h> | ||
22 | #include <linux/device.h> | ||
23 | #include <linux/delay.h> | ||
24 | #include <linux/tty.h> | ||
25 | #include <linux/tty_flip.h> | ||
26 | |||
27 | #include "u_serial.h" | ||
28 | |||
29 | |||
30 | /* | ||
31 | * This component encapsulates the TTY layer glue needed to provide basic | ||
32 | * "serial port" functionality through the USB gadget stack. Each such | ||
33 | * port is exposed through a /dev/ttyGS* node. | ||
34 | * | ||
35 | * After initialization (gserial_setup), these TTY port devices stay | ||
36 | * available until they are removed (gserial_cleanup). Each one may be | ||
37 | * connected to a USB function (gserial_connect), or disconnected (with | ||
38 | * gserial_disconnect) when the USB host issues a config change event. | ||
39 | * Data can only flow when the port is connected to the host. | ||
40 | * | ||
41 | * A given TTY port can be made available in multiple configurations. | ||
42 | * For example, each one might expose a ttyGS0 node which provides a | ||
43 | * login application. In one case that might use CDC ACM interface 0, | ||
44 | * while another configuration might use interface 3 for that. The | ||
45 | * work to handle that (including descriptor management) is not part | ||
46 | * of this component. | ||
47 | * | ||
48 | * Configurations may expose more than one TTY port. For example, if | ||
49 | * ttyGS0 provides login service, then ttyGS1 might provide dialer access | ||
50 | * for a telephone or fax link. And ttyGS2 might be something that just | ||
51 | * needs a simple byte stream interface for some messaging protocol that | ||
52 | * is managed in userspace ... OBEX, PTP, and MTP have been mentioned. | ||
53 | */ | ||
54 | |||
55 | /* | ||
56 | * gserial is the lifecycle interface, used by USB functions | ||
57 | * gs_port is the I/O nexus, used by the tty driver | ||
58 | * tty_struct links to the tty/filesystem framework | ||
59 | * | ||
60 | * gserial <---> gs_port ... links will be null when the USB link is | ||
61 | * inactive; managed by gserial_{connect,disconnect}(). | ||
62 | * gserial->ioport == usb_ep->driver_data ... gs_port | ||
63 | * gs_port->port_usb ... gserial | ||
64 | * | ||
65 | * gs_port <---> tty_struct ... links will be null when the TTY file | ||
66 | * isn't opened; managed by gs_open()/gs_close() | ||
67 | * gserial->port_tty ... tty_struct | ||
68 | * tty_struct->driver_data ... gserial | ||
69 | */ | ||
70 | |||
71 | /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the | ||
72 | * next layer of buffering. For TX that's a circular buffer; for RX | ||
73 | * consider it a NOP. A third layer is provided by the TTY code. | ||
74 | */ | ||
75 | #define QUEUE_SIZE 16 | ||
76 | #define WRITE_BUF_SIZE 8192 /* TX only */ | ||
77 | |||
78 | /* circular buffer */ | ||
79 | struct gs_buf { | ||
80 | unsigned buf_size; | ||
81 | char *buf_buf; | ||
82 | char *buf_get; | ||
83 | char *buf_put; | ||
84 | }; | ||
85 | |||
86 | /* | ||
87 | * The port structure holds info for each port, one for each minor number | ||
88 | * (and thus for each /dev/ node). | ||
89 | */ | ||
90 | struct gs_port { | ||
91 | spinlock_t port_lock; /* guard port_* access */ | ||
92 | |||
93 | struct gserial *port_usb; | ||
94 | struct tty_struct *port_tty; | ||
95 | |||
96 | unsigned open_count; | ||
97 | bool openclose; /* open/close in progress */ | ||
98 | u8 port_num; | ||
99 | |||
100 | wait_queue_head_t close_wait; /* wait for last close */ | ||
101 | |||
102 | struct list_head read_pool; | ||
103 | struct tasklet_struct push; | ||
104 | |||
105 | struct list_head write_pool; | ||
106 | struct gs_buf port_write_buf; | ||
107 | wait_queue_head_t drain_wait; /* wait while writes drain */ | ||
108 | |||
109 | /* REVISIT this state ... */ | ||
110 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ | ||
111 | }; | ||
112 | |||
113 | /* increase N_PORTS if you need more */ | ||
114 | #define N_PORTS 4 | ||
115 | static struct portmaster { | ||
116 | struct mutex lock; /* protect open/close */ | ||
117 | struct gs_port *port; | ||
118 | } ports[N_PORTS]; | ||
119 | static unsigned n_ports; | ||
120 | |||
121 | #define GS_CLOSE_TIMEOUT 15 /* seconds */ | ||
122 | |||
123 | |||
124 | |||
125 | #ifdef VERBOSE_DEBUG | ||
126 | #define pr_vdebug(fmt, arg...) \ | ||
127 | pr_debug(fmt, ##arg) | ||
128 | #else | ||
129 | #define pr_vdebug(fmt, arg...) \ | ||
130 | ({ if (0) pr_debug(fmt, ##arg); }) | ||
131 | #endif | ||
132 | |||
133 | /*-------------------------------------------------------------------------*/ | ||
134 | |||
135 | /* Circular Buffer */ | ||
136 | |||
137 | /* | ||
138 | * gs_buf_alloc | ||
139 | * | ||
140 | * Allocate a circular buffer and all associated memory. | ||
141 | */ | ||
142 | static int gs_buf_alloc(struct gs_buf *gb, unsigned size) | ||
143 | { | ||
144 | gb->buf_buf = kmalloc(size, GFP_KERNEL); | ||
145 | if (gb->buf_buf == NULL) | ||
146 | return -ENOMEM; | ||
147 | |||
148 | gb->buf_size = size; | ||
149 | gb->buf_put = gb->buf_buf; | ||
150 | gb->buf_get = gb->buf_buf; | ||
151 | |||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | /* | ||
156 | * gs_buf_free | ||
157 | * | ||
158 | * Free the buffer and all associated memory. | ||
159 | */ | ||
160 | static void gs_buf_free(struct gs_buf *gb) | ||
161 | { | ||
162 | kfree(gb->buf_buf); | ||
163 | gb->buf_buf = NULL; | ||
164 | } | ||
165 | |||
166 | /* | ||
167 | * gs_buf_clear | ||
168 | * | ||
169 | * Clear out all data in the circular buffer. | ||
170 | */ | ||
171 | static void gs_buf_clear(struct gs_buf *gb) | ||
172 | { | ||
173 | gb->buf_get = gb->buf_put; | ||
174 | /* equivalent to a get of all data available */ | ||
175 | } | ||
176 | |||
177 | /* | ||
178 | * gs_buf_data_avail | ||
179 | * | ||
180 | * Return the number of bytes of data available in the circular | ||
181 | * buffer. | ||
182 | */ | ||
183 | static unsigned gs_buf_data_avail(struct gs_buf *gb) | ||
184 | { | ||
185 | return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size; | ||
186 | } | ||
187 | |||
188 | /* | ||
189 | * gs_buf_space_avail | ||
190 | * | ||
191 | * Return the number of bytes of space available in the circular | ||
192 | * buffer. | ||
193 | */ | ||
194 | static unsigned gs_buf_space_avail(struct gs_buf *gb) | ||
195 | { | ||
196 | return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size; | ||
197 | } | ||
198 | |||
199 | /* | ||
200 | * gs_buf_put | ||
201 | * | ||
202 | * Copy data data from a user buffer and put it into the circular buffer. | ||
203 | * Restrict to the amount of space available. | ||
204 | * | ||
205 | * Return the number of bytes copied. | ||
206 | */ | ||
207 | static unsigned | ||
208 | gs_buf_put(struct gs_buf *gb, const char *buf, unsigned count) | ||
209 | { | ||
210 | unsigned len; | ||
211 | |||
212 | len = gs_buf_space_avail(gb); | ||
213 | if (count > len) | ||
214 | count = len; | ||
215 | |||
216 | if (count == 0) | ||
217 | return 0; | ||
218 | |||
219 | len = gb->buf_buf + gb->buf_size - gb->buf_put; | ||
220 | if (count > len) { | ||
221 | memcpy(gb->buf_put, buf, len); | ||
222 | memcpy(gb->buf_buf, buf+len, count - len); | ||
223 | gb->buf_put = gb->buf_buf + count - len; | ||
224 | } else { | ||
225 | memcpy(gb->buf_put, buf, count); | ||
226 | if (count < len) | ||
227 | gb->buf_put += count; | ||
228 | else /* count == len */ | ||
229 | gb->buf_put = gb->buf_buf; | ||
230 | } | ||
231 | |||
232 | return count; | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * gs_buf_get | ||
237 | * | ||
238 | * Get data from the circular buffer and copy to the given buffer. | ||
239 | * Restrict to the amount of data available. | ||
240 | * | ||
241 | * Return the number of bytes copied. | ||
242 | */ | ||
243 | static unsigned | ||
244 | gs_buf_get(struct gs_buf *gb, char *buf, unsigned count) | ||
245 | { | ||
246 | unsigned len; | ||
247 | |||
248 | len = gs_buf_data_avail(gb); | ||
249 | if (count > len) | ||
250 | count = len; | ||
251 | |||
252 | if (count == 0) | ||
253 | return 0; | ||
254 | |||
255 | len = gb->buf_buf + gb->buf_size - gb->buf_get; | ||
256 | if (count > len) { | ||
257 | memcpy(buf, gb->buf_get, len); | ||
258 | memcpy(buf+len, gb->buf_buf, count - len); | ||
259 | gb->buf_get = gb->buf_buf + count - len; | ||
260 | } else { | ||
261 | memcpy(buf, gb->buf_get, count); | ||
262 | if (count < len) | ||
263 | gb->buf_get += count; | ||
264 | else /* count == len */ | ||
265 | gb->buf_get = gb->buf_buf; | ||
266 | } | ||
267 | |||
268 | return count; | ||
269 | } | ||
270 | |||
271 | /*-------------------------------------------------------------------------*/ | ||
272 | |||
273 | /* I/O glue between TTY (upper) and USB function (lower) driver layers */ | ||
274 | |||
275 | /* | ||
276 | * gs_alloc_req | ||
277 | * | ||
278 | * Allocate a usb_request and its buffer. Returns a pointer to the | ||
279 | * usb_request or NULL if there is an error. | ||
280 | */ | ||
281 | static struct usb_request * | ||
282 | gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags) | ||
283 | { | ||
284 | struct usb_request *req; | ||
285 | |||
286 | req = usb_ep_alloc_request(ep, kmalloc_flags); | ||
287 | |||
288 | if (req != NULL) { | ||
289 | req->length = len; | ||
290 | req->buf = kmalloc(len, kmalloc_flags); | ||
291 | if (req->buf == NULL) { | ||
292 | usb_ep_free_request(ep, req); | ||
293 | return NULL; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | return req; | ||
298 | } | ||
299 | |||
300 | /* | ||
301 | * gs_free_req | ||
302 | * | ||
303 | * Free a usb_request and its buffer. | ||
304 | */ | ||
305 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req) | ||
306 | { | ||
307 | kfree(req->buf); | ||
308 | usb_ep_free_request(ep, req); | ||
309 | } | ||
310 | |||
311 | /* | ||
312 | * gs_send_packet | ||
313 | * | ||
314 | * If there is data to send, a packet is built in the given | ||
315 | * buffer and the size is returned. If there is no data to | ||
316 | * send, 0 is returned. | ||
317 | * | ||
318 | * Called with port_lock held. | ||
319 | */ | ||
320 | static unsigned | ||
321 | gs_send_packet(struct gs_port *port, char *packet, unsigned size) | ||
322 | { | ||
323 | unsigned len; | ||
324 | |||
325 | len = gs_buf_data_avail(&port->port_write_buf); | ||
326 | if (len < size) | ||
327 | size = len; | ||
328 | if (size != 0) | ||
329 | size = gs_buf_get(&port->port_write_buf, packet, size); | ||
330 | return size; | ||
331 | } | ||
332 | |||
333 | /* | ||
334 | * gs_start_tx | ||
335 | * | ||
336 | * This function finds available write requests, calls | ||
337 | * gs_send_packet to fill these packets with data, and | ||
338 | * continues until either there are no more write requests | ||
339 | * available or no more data to send. This function is | ||
340 | * run whenever data arrives or write requests are available. | ||
341 | * | ||
342 | * Context: caller owns port_lock; port_usb is non-null. | ||
343 | */ | ||
344 | static int gs_start_tx(struct gs_port *port) | ||
345 | /* | ||
346 | __releases(&port->port_lock) | ||
347 | __acquires(&port->port_lock) | ||
348 | */ | ||
349 | { | ||
350 | struct list_head *pool = &port->write_pool; | ||
351 | struct usb_ep *in = port->port_usb->in; | ||
352 | int status = 0; | ||
353 | bool do_tty_wake = false; | ||
354 | |||
355 | while (!list_empty(pool)) { | ||
356 | struct usb_request *req; | ||
357 | int len; | ||
358 | |||
359 | req = list_entry(pool->next, struct usb_request, list); | ||
360 | len = gs_send_packet(port, req->buf, in->maxpacket); | ||
361 | if (len == 0) { | ||
362 | wake_up_interruptible(&port->drain_wait); | ||
363 | break; | ||
364 | } | ||
365 | do_tty_wake = true; | ||
366 | |||
367 | req->length = len; | ||
368 | list_del(&req->list); | ||
369 | |||
370 | #ifdef VERBOSE_DEBUG | ||
371 | pr_debug("%s: %s, len=%d, 0x%02x 0x%02x 0x%02x ...\n", | ||
372 | __func__, in->name, len, *((u8 *)req->buf), | ||
373 | *((u8 *)req->buf+1), *((u8 *)req->buf+2)); | ||
374 | #endif | ||
375 | |||
376 | /* Drop lock while we call out of driver; completions | ||
377 | * could be issued while we do so. Disconnection may | ||
378 | * happen too; maybe immediately before we queue this! | ||
379 | * | ||
380 | * NOTE that we may keep sending data for a while after | ||
381 | * the TTY closed (dev->ioport->port_tty is NULL). | ||
382 | */ | ||
383 | spin_unlock(&port->port_lock); | ||
384 | status = usb_ep_queue(in, req, GFP_ATOMIC); | ||
385 | spin_lock(&port->port_lock); | ||
386 | |||
387 | if (status) { | ||
388 | pr_debug("%s: %s %s err %d\n", | ||
389 | __func__, "queue", in->name, status); | ||
390 | list_add(&req->list, pool); | ||
391 | break; | ||
392 | } | ||
393 | |||
394 | /* abort immediately after disconnect */ | ||
395 | if (!port->port_usb) | ||
396 | break; | ||
397 | } | ||
398 | |||
399 | if (do_tty_wake && port->port_tty) | ||
400 | tty_wakeup(port->port_tty); | ||
401 | return status; | ||
402 | } | ||
403 | |||
404 | static void gs_rx_push(unsigned long _port) | ||
405 | { | ||
406 | struct gs_port *port = (void *)_port; | ||
407 | struct tty_struct *tty = port->port_tty; | ||
408 | |||
409 | /* With low_latency, tty_flip_buffer_push() doesn't put its | ||
410 | * real work through a workqueue, so the ldisc has a better | ||
411 | * chance to keep up with peak USB data rates. | ||
412 | */ | ||
413 | if (tty) { | ||
414 | tty_flip_buffer_push(tty); | ||
415 | wake_up_interruptible(&tty->read_wait); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | /* | ||
420 | * gs_recv_packet | ||
421 | * | ||
422 | * Called for each USB packet received. Reads the packet | ||
423 | * header and stuffs the data in the appropriate tty buffer. | ||
424 | * Returns 0 if successful, or a negative error number. | ||
425 | * | ||
426 | * Called during USB completion routine, on interrupt time. | ||
427 | * With port_lock. | ||
428 | */ | ||
429 | static int gs_recv_packet(struct gs_port *port, char *packet, unsigned size) | ||
430 | { | ||
431 | unsigned len; | ||
432 | struct tty_struct *tty; | ||
433 | |||
434 | /* I/O completions can continue for a while after close(), until the | ||
435 | * request queue empties. Just discard any data we receive, until | ||
436 | * something reopens this TTY ... as if there were no HW flow control. | ||
437 | */ | ||
438 | tty = port->port_tty; | ||
439 | if (tty == NULL) { | ||
440 | pr_vdebug("%s: ttyGS%d, after close\n", | ||
441 | __func__, port->port_num); | ||
442 | return -EIO; | ||
443 | } | ||
444 | |||
445 | len = tty_insert_flip_string(tty, packet, size); | ||
446 | if (len > 0) | ||
447 | tasklet_schedule(&port->push); | ||
448 | if (len < size) | ||
449 | pr_debug("%s: ttyGS%d, drop %d bytes\n", | ||
450 | __func__, port->port_num, size - len); | ||
451 | return 0; | ||
452 | } | ||
453 | |||
454 | /* | ||
455 | * Context: caller owns port_lock, and port_usb is set | ||
456 | */ | ||
457 | static unsigned gs_start_rx(struct gs_port *port) | ||
458 | /* | ||
459 | __releases(&port->port_lock) | ||
460 | __acquires(&port->port_lock) | ||
461 | */ | ||
462 | { | ||
463 | struct list_head *pool = &port->read_pool; | ||
464 | struct usb_ep *out = port->port_usb->out; | ||
465 | unsigned started = 0; | ||
466 | |||
467 | while (!list_empty(pool)) { | ||
468 | struct usb_request *req; | ||
469 | int status; | ||
470 | struct tty_struct *tty; | ||
471 | |||
472 | /* no more rx if closed or throttled */ | ||
473 | tty = port->port_tty; | ||
474 | if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) | ||
475 | break; | ||
476 | |||
477 | req = list_entry(pool->next, struct usb_request, list); | ||
478 | list_del(&req->list); | ||
479 | req->length = out->maxpacket; | ||
480 | |||
481 | /* drop lock while we call out; the controller driver | ||
482 | * may need to call us back (e.g. for disconnect) | ||
483 | */ | ||
484 | spin_unlock(&port->port_lock); | ||
485 | status = usb_ep_queue(out, req, GFP_ATOMIC); | ||
486 | spin_lock(&port->port_lock); | ||
487 | |||
488 | if (status) { | ||
489 | pr_debug("%s: %s %s err %d\n", | ||
490 | __func__, "queue", out->name, status); | ||
491 | list_add(&req->list, pool); | ||
492 | break; | ||
493 | } | ||
494 | started++; | ||
495 | |||
496 | /* abort immediately after disconnect */ | ||
497 | if (!port->port_usb) | ||
498 | break; | ||
499 | } | ||
500 | return started; | ||
501 | } | ||
502 | |||
503 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) | ||
504 | { | ||
505 | int status; | ||
506 | struct gs_port *port = ep->driver_data; | ||
507 | |||
508 | spin_lock(&port->port_lock); | ||
509 | list_add(&req->list, &port->read_pool); | ||
510 | |||
511 | switch (req->status) { | ||
512 | case 0: | ||
513 | /* normal completion */ | ||
514 | status = gs_recv_packet(port, req->buf, req->actual); | ||
515 | if (status && status != -EIO) | ||
516 | pr_debug("%s: %s %s err %d\n", | ||
517 | __func__, "recv", ep->name, status); | ||
518 | gs_start_rx(port); | ||
519 | break; | ||
520 | |||
521 | case -ESHUTDOWN: | ||
522 | /* disconnect */ | ||
523 | pr_vdebug("%s: %s shutdown\n", __func__, ep->name); | ||
524 | break; | ||
525 | |||
526 | default: | ||
527 | /* presumably a transient fault */ | ||
528 | pr_warning("%s: unexpected %s status %d\n", | ||
529 | __func__, ep->name, req->status); | ||
530 | gs_start_rx(port); | ||
531 | break; | ||
532 | } | ||
533 | spin_unlock(&port->port_lock); | ||
534 | } | ||
535 | |||
536 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) | ||
537 | { | ||
538 | struct gs_port *port = ep->driver_data; | ||
539 | |||
540 | spin_lock(&port->port_lock); | ||
541 | list_add(&req->list, &port->write_pool); | ||
542 | |||
543 | switch (req->status) { | ||
544 | default: | ||
545 | /* presumably a transient fault */ | ||
546 | pr_warning("%s: unexpected %s status %d\n", | ||
547 | __func__, ep->name, req->status); | ||
548 | /* FALL THROUGH */ | ||
549 | case 0: | ||
550 | /* normal completion */ | ||
551 | gs_start_tx(port); | ||
552 | break; | ||
553 | |||
554 | case -ESHUTDOWN: | ||
555 | /* disconnect */ | ||
556 | pr_vdebug("%s: %s shutdown\n", __func__, ep->name); | ||
557 | break; | ||
558 | } | ||
559 | |||
560 | spin_unlock(&port->port_lock); | ||
561 | } | ||
562 | |||
563 | static void gs_free_requests(struct usb_ep *ep, struct list_head *head) | ||
564 | { | ||
565 | struct usb_request *req; | ||
566 | |||
567 | while (!list_empty(head)) { | ||
568 | req = list_entry(head->next, struct usb_request, list); | ||
569 | list_del(&req->list); | ||
570 | gs_free_req(ep, req); | ||
571 | } | ||
572 | } | ||
573 | |||
574 | static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head, | ||
575 | void (*fn)(struct usb_ep *, struct usb_request *)) | ||
576 | { | ||
577 | int i; | ||
578 | struct usb_request *req; | ||
579 | |||
580 | /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't | ||
581 | * do quite that many this time, don't fail ... we just won't | ||
582 | * be as speedy as we might otherwise be. | ||
583 | */ | ||
584 | for (i = 0; i < QUEUE_SIZE; i++) { | ||
585 | req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); | ||
586 | if (!req) | ||
587 | return list_empty(head) ? -ENOMEM : 0; | ||
588 | req->complete = fn; | ||
589 | list_add_tail(&req->list, head); | ||
590 | } | ||
591 | return 0; | ||
592 | } | ||
593 | |||
594 | /** | ||
595 | * gs_start_io - start USB I/O streams | ||
596 | * @dev: encapsulates endpoints to use | ||
597 | * Context: holding port_lock; port_tty and port_usb are non-null | ||
598 | * | ||
599 | * We only start I/O when something is connected to both sides of | ||
600 | * this port. If nothing is listening on the host side, we may | ||
601 | * be pointlessly filling up our TX buffers and FIFO. | ||
602 | */ | ||
603 | static int gs_start_io(struct gs_port *port) | ||
604 | { | ||
605 | struct list_head *head = &port->read_pool; | ||
606 | struct usb_ep *ep = port->port_usb->out; | ||
607 | int status; | ||
608 | unsigned started; | ||
609 | |||
610 | /* Allocate RX and TX I/O buffers. We can't easily do this much | ||
611 | * earlier (with GFP_KERNEL) because the requests are coupled to | ||
612 | * endpoints, as are the packet sizes we'll be using. Different | ||
613 | * configurations may use different endpoints with a given port; | ||
614 | * and high speed vs full speed changes packet sizes too. | ||
615 | */ | ||
616 | status = gs_alloc_requests(ep, head, gs_read_complete); | ||
617 | if (status) | ||
618 | return status; | ||
619 | |||
620 | status = gs_alloc_requests(port->port_usb->in, &port->write_pool, | ||
621 | gs_write_complete); | ||
622 | if (status) { | ||
623 | gs_free_requests(ep, head); | ||
624 | return status; | ||
625 | } | ||
626 | |||
627 | /* queue read requests */ | ||
628 | started = gs_start_rx(port); | ||
629 | |||
630 | /* unblock any pending writes into our circular buffer */ | ||
631 | if (started) { | ||
632 | tty_wakeup(port->port_tty); | ||
633 | } else { | ||
634 | gs_free_requests(ep, head); | ||
635 | gs_free_requests(port->port_usb->in, &port->write_pool); | ||
636 | } | ||
637 | |||
638 | return started ? 0 : status; | ||
639 | } | ||
640 | |||
641 | /*-------------------------------------------------------------------------*/ | ||
642 | |||
643 | /* TTY Driver */ | ||
644 | |||
645 | /* | ||
646 | * gs_open sets up the link between a gs_port and its associated TTY. | ||
647 | * That link is broken *only* by TTY close(), and all driver methods | ||
648 | * know that. | ||
649 | */ | ||
650 | static int gs_open(struct tty_struct *tty, struct file *file) | ||
651 | { | ||
652 | int port_num = tty->index; | ||
653 | struct gs_port *port; | ||
654 | int status; | ||
655 | |||
656 | if (port_num < 0 || port_num >= n_ports) | ||
657 | return -ENXIO; | ||
658 | |||
659 | do { | ||
660 | mutex_lock(&ports[port_num].lock); | ||
661 | port = ports[port_num].port; | ||
662 | if (!port) | ||
663 | status = -ENODEV; | ||
664 | else { | ||
665 | spin_lock_irq(&port->port_lock); | ||
666 | |||
667 | /* already open? Great. */ | ||
668 | if (port->open_count) { | ||
669 | status = 0; | ||
670 | port->open_count++; | ||
671 | |||
672 | /* currently opening/closing? wait ... */ | ||
673 | } else if (port->openclose) { | ||
674 | status = -EBUSY; | ||
675 | |||
676 | /* ... else we do the work */ | ||
677 | } else { | ||
678 | status = -EAGAIN; | ||
679 | port->openclose = true; | ||
680 | } | ||
681 | spin_unlock_irq(&port->port_lock); | ||
682 | } | ||
683 | mutex_unlock(&ports[port_num].lock); | ||
684 | |||
685 | switch (status) { | ||
686 | default: | ||
687 | /* fully handled */ | ||
688 | return status; | ||
689 | case -EAGAIN: | ||
690 | /* must do the work */ | ||
691 | break; | ||
692 | case -EBUSY: | ||
693 | /* wait for EAGAIN task to finish */ | ||
694 | msleep(1); | ||
695 | /* REVISIT could have a waitchannel here, if | ||
696 | * concurrent open performance is important | ||
697 | */ | ||
698 | break; | ||
699 | } | ||
700 | } while (status != -EAGAIN); | ||
701 | |||
702 | /* Do the "real open" */ | ||
703 | spin_lock_irq(&port->port_lock); | ||
704 | |||
705 | /* allocate circular buffer on first open */ | ||
706 | if (port->port_write_buf.buf_buf == NULL) { | ||
707 | |||
708 | spin_unlock_irq(&port->port_lock); | ||
709 | status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE); | ||
710 | spin_lock_irq(&port->port_lock); | ||
711 | |||
712 | if (status) { | ||
713 | pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n", | ||
714 | port->port_num, tty, file); | ||
715 | port->openclose = false; | ||
716 | goto exit_unlock_port; | ||
717 | } | ||
718 | } | ||
719 | |||
720 | /* REVISIT if REMOVED (ports[].port NULL), abort the open | ||
721 | * to let rmmod work faster (but this way isn't wrong). | ||
722 | */ | ||
723 | |||
724 | /* REVISIT maybe wait for "carrier detect" */ | ||
725 | |||
726 | tty->driver_data = port; | ||
727 | port->port_tty = tty; | ||
728 | |||
729 | port->open_count = 1; | ||
730 | port->openclose = false; | ||
731 | |||
732 | /* low_latency means ldiscs work in tasklet context, without | ||
733 | * needing a workqueue schedule ... easier to keep up. | ||
734 | */ | ||
735 | tty->low_latency = 1; | ||
736 | |||
737 | /* if connected, start the I/O stream */ | ||
738 | if (port->port_usb) { | ||
739 | pr_debug("gs_open: start ttyGS%d\n", port->port_num); | ||
740 | gs_start_io(port); | ||
741 | |||
742 | /* REVISIT for ACM, issue "network connected" event */ | ||
743 | } | ||
744 | |||
745 | pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); | ||
746 | |||
747 | status = 0; | ||
748 | |||
749 | exit_unlock_port: | ||
750 | spin_unlock_irq(&port->port_lock); | ||
751 | return status; | ||
752 | } | ||
753 | |||
754 | static int gs_writes_finished(struct gs_port *p) | ||
755 | { | ||
756 | int cond; | ||
757 | |||
758 | /* return true on disconnect or empty buffer */ | ||
759 | spin_lock_irq(&p->port_lock); | ||
760 | cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf); | ||
761 | spin_unlock_irq(&p->port_lock); | ||
762 | |||
763 | return cond; | ||
764 | } | ||
765 | |||
766 | static void gs_close(struct tty_struct *tty, struct file *file) | ||
767 | { | ||
768 | struct gs_port *port = tty->driver_data; | ||
769 | |||
770 | spin_lock_irq(&port->port_lock); | ||
771 | |||
772 | if (port->open_count != 1) { | ||
773 | if (port->open_count == 0) | ||
774 | WARN_ON(1); | ||
775 | else | ||
776 | --port->open_count; | ||
777 | goto exit; | ||
778 | } | ||
779 | |||
780 | pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); | ||
781 | |||
782 | /* mark port as closing but in use; we can drop port lock | ||
783 | * and sleep if necessary | ||
784 | */ | ||
785 | port->openclose = true; | ||
786 | port->open_count = 0; | ||
787 | |||
788 | if (port->port_usb) | ||
789 | /* REVISIT for ACM, issue "network disconnected" event */; | ||
790 | |||
791 | /* wait for circular write buffer to drain, disconnect, or at | ||
792 | * most GS_CLOSE_TIMEOUT seconds; then discard the rest | ||
793 | */ | ||
794 | if (gs_buf_data_avail(&port->port_write_buf) > 0 | ||
795 | && port->port_usb) { | ||
796 | spin_unlock_irq(&port->port_lock); | ||
797 | wait_event_interruptible_timeout(port->drain_wait, | ||
798 | gs_writes_finished(port), | ||
799 | GS_CLOSE_TIMEOUT * HZ); | ||
800 | spin_lock_irq(&port->port_lock); | ||
801 | } | ||
802 | |||
803 | /* Iff we're disconnected, there can be no I/O in flight so it's | ||
804 | * ok to free the circular buffer; else just scrub it. And don't | ||
805 | * let the push tasklet fire again until we're re-opened. | ||
806 | */ | ||
807 | if (port->port_usb == NULL) | ||
808 | gs_buf_free(&port->port_write_buf); | ||
809 | else | ||
810 | gs_buf_clear(&port->port_write_buf); | ||
811 | |||
812 | tasklet_kill(&port->push); | ||
813 | |||
814 | tty->driver_data = NULL; | ||
815 | port->port_tty = NULL; | ||
816 | |||
817 | port->openclose = false; | ||
818 | |||
819 | pr_debug("gs_close: ttyGS%d (%p,%p) done!\n", | ||
820 | port->port_num, tty, file); | ||
821 | |||
822 | wake_up_interruptible(&port->close_wait); | ||
823 | exit: | ||
824 | spin_unlock_irq(&port->port_lock); | ||
825 | } | ||
826 | |||
827 | static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
828 | { | ||
829 | struct gs_port *port = tty->driver_data; | ||
830 | unsigned long flags; | ||
831 | int status; | ||
832 | |||
833 | pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", | ||
834 | port->port_num, tty, count); | ||
835 | |||
836 | spin_lock_irqsave(&port->port_lock, flags); | ||
837 | if (count) | ||
838 | count = gs_buf_put(&port->port_write_buf, buf, count); | ||
839 | /* treat count == 0 as flush_chars() */ | ||
840 | if (port->port_usb) | ||
841 | status = gs_start_tx(port); | ||
842 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
843 | |||
844 | return count; | ||
845 | } | ||
846 | |||
847 | static int gs_put_char(struct tty_struct *tty, unsigned char ch) | ||
848 | { | ||
849 | struct gs_port *port = tty->driver_data; | ||
850 | unsigned long flags; | ||
851 | int status; | ||
852 | |||
853 | pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %p\n", | ||
854 | port->port_num, tty, ch, __builtin_return_address(0)); | ||
855 | |||
856 | spin_lock_irqsave(&port->port_lock, flags); | ||
857 | status = gs_buf_put(&port->port_write_buf, &ch, 1); | ||
858 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
859 | |||
860 | return status; | ||
861 | } | ||
862 | |||
863 | static void gs_flush_chars(struct tty_struct *tty) | ||
864 | { | ||
865 | struct gs_port *port = tty->driver_data; | ||
866 | unsigned long flags; | ||
867 | |||
868 | pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); | ||
869 | |||
870 | spin_lock_irqsave(&port->port_lock, flags); | ||
871 | if (port->port_usb) | ||
872 | gs_start_tx(port); | ||
873 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
874 | } | ||
875 | |||
876 | static int gs_write_room(struct tty_struct *tty) | ||
877 | { | ||
878 | struct gs_port *port = tty->driver_data; | ||
879 | unsigned long flags; | ||
880 | int room = 0; | ||
881 | |||
882 | spin_lock_irqsave(&port->port_lock, flags); | ||
883 | if (port->port_usb) | ||
884 | room = gs_buf_space_avail(&port->port_write_buf); | ||
885 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
886 | |||
887 | pr_vdebug("gs_write_room: (%d,%p) room=%d\n", | ||
888 | port->port_num, tty, room); | ||
889 | |||
890 | return room; | ||
891 | } | ||
892 | |||
893 | static int gs_chars_in_buffer(struct tty_struct *tty) | ||
894 | { | ||
895 | struct gs_port *port = tty->driver_data; | ||
896 | unsigned long flags; | ||
897 | int chars = 0; | ||
898 | |||
899 | spin_lock_irqsave(&port->port_lock, flags); | ||
900 | chars = gs_buf_data_avail(&port->port_write_buf); | ||
901 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
902 | |||
903 | pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n", | ||
904 | port->port_num, tty, chars); | ||
905 | |||
906 | return chars; | ||
907 | } | ||
908 | |||
909 | /* undo side effects of setting TTY_THROTTLED */ | ||
910 | static void gs_unthrottle(struct tty_struct *tty) | ||
911 | { | ||
912 | struct gs_port *port = tty->driver_data; | ||
913 | unsigned long flags; | ||
914 | unsigned started = 0; | ||
915 | |||
916 | spin_lock_irqsave(&port->port_lock, flags); | ||
917 | if (port->port_usb) | ||
918 | started = gs_start_rx(port); | ||
919 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
920 | |||
921 | pr_vdebug("gs_unthrottle: ttyGS%d, %d packets\n", | ||
922 | port->port_num, started); | ||
923 | } | ||
924 | |||
925 | static const struct tty_operations gs_tty_ops = { | ||
926 | .open = gs_open, | ||
927 | .close = gs_close, | ||
928 | .write = gs_write, | ||
929 | .put_char = gs_put_char, | ||
930 | .flush_chars = gs_flush_chars, | ||
931 | .write_room = gs_write_room, | ||
932 | .chars_in_buffer = gs_chars_in_buffer, | ||
933 | .unthrottle = gs_unthrottle, | ||
934 | }; | ||
935 | |||
936 | /*-------------------------------------------------------------------------*/ | ||
937 | |||
938 | static struct tty_driver *gs_tty_driver; | ||
939 | |||
940 | static int __init | ||
941 | gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding) | ||
942 | { | ||
943 | struct gs_port *port; | ||
944 | |||
945 | port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); | ||
946 | if (port == NULL) | ||
947 | return -ENOMEM; | ||
948 | |||
949 | spin_lock_init(&port->port_lock); | ||
950 | init_waitqueue_head(&port->close_wait); | ||
951 | init_waitqueue_head(&port->drain_wait); | ||
952 | |||
953 | tasklet_init(&port->push, gs_rx_push, (unsigned long) port); | ||
954 | |||
955 | INIT_LIST_HEAD(&port->read_pool); | ||
956 | INIT_LIST_HEAD(&port->write_pool); | ||
957 | |||
958 | port->port_num = port_num; | ||
959 | port->port_line_coding = *coding; | ||
960 | |||
961 | ports[port_num].port = port; | ||
962 | |||
963 | return 0; | ||
964 | } | ||
965 | |||
966 | /** | ||
967 | * gserial_setup - initialize TTY driver for one or more ports | ||
968 | * @g: gadget to associate with these ports | ||
969 | * @count: how many ports to support | ||
970 | * Context: may sleep | ||
971 | * | ||
972 | * The TTY stack needs to know in advance how many devices it should | ||
973 | * plan to manage. Use this call to set up the ports you will be | ||
974 | * exporting through USB. Later, connect them to functions based | ||
975 | * on what configuration is activated by the USB host; and disconnect | ||
976 | * them as appropriate. | ||
977 | * | ||
978 | * An example would be a two-configuration device in which both | ||
979 | * configurations expose port 0, but through different functions. | ||
980 | * One configuration could even expose port 1 while the other | ||
981 | * one doesn't. | ||
982 | * | ||
983 | * Returns negative errno or zero. | ||
984 | */ | ||
985 | int __init gserial_setup(struct usb_gadget *g, unsigned count) | ||
986 | { | ||
987 | unsigned i; | ||
988 | struct usb_cdc_line_coding coding; | ||
989 | int status; | ||
990 | |||
991 | if (count == 0 || count > N_PORTS) | ||
992 | return -EINVAL; | ||
993 | |||
994 | gs_tty_driver = alloc_tty_driver(count); | ||
995 | if (!gs_tty_driver) | ||
996 | return -ENOMEM; | ||
997 | |||
998 | gs_tty_driver->owner = THIS_MODULE; | ||
999 | gs_tty_driver->driver_name = "g_serial"; | ||
1000 | gs_tty_driver->name = "ttyGS"; | ||
1001 | /* uses dynamically assigned dev_t values */ | ||
1002 | |||
1003 | gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; | ||
1004 | gs_tty_driver->subtype = SERIAL_TYPE_NORMAL; | ||
1005 | gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; | ||
1006 | gs_tty_driver->init_termios = tty_std_termios; | ||
1007 | |||
1008 | /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on | ||
1009 | * MS-Windows. Otherwise, most of these flags shouldn't affect | ||
1010 | * anything unless we were to actually hook up to a serial line. | ||
1011 | */ | ||
1012 | gs_tty_driver->init_termios.c_cflag = | ||
1013 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; | ||
1014 | gs_tty_driver->init_termios.c_ispeed = 9600; | ||
1015 | gs_tty_driver->init_termios.c_ospeed = 9600; | ||
1016 | |||
1017 | coding.dwDTERate = __constant_cpu_to_le32(9600); | ||
1018 | coding.bCharFormat = 8; | ||
1019 | coding.bParityType = USB_CDC_NO_PARITY; | ||
1020 | coding.bDataBits = USB_CDC_1_STOP_BITS; | ||
1021 | |||
1022 | tty_set_operations(gs_tty_driver, &gs_tty_ops); | ||
1023 | |||
1024 | /* make devices be openable */ | ||
1025 | for (i = 0; i < count; i++) { | ||
1026 | mutex_init(&ports[i].lock); | ||
1027 | status = gs_port_alloc(i, &coding); | ||
1028 | if (status) { | ||
1029 | count = i; | ||
1030 | goto fail; | ||
1031 | } | ||
1032 | } | ||
1033 | n_ports = count; | ||
1034 | |||
1035 | /* export the driver ... */ | ||
1036 | status = tty_register_driver(gs_tty_driver); | ||
1037 | if (status) { | ||
1038 | put_tty_driver(gs_tty_driver); | ||
1039 | pr_err("%s: cannot register, err %d\n", | ||
1040 | __func__, status); | ||
1041 | goto fail; | ||
1042 | } | ||
1043 | |||
1044 | /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */ | ||
1045 | for (i = 0; i < count; i++) { | ||
1046 | struct device *tty_dev; | ||
1047 | |||
1048 | tty_dev = tty_register_device(gs_tty_driver, i, &g->dev); | ||
1049 | if (IS_ERR(tty_dev)) | ||
1050 | pr_warning("%s: no classdev for port %d, err %ld\n", | ||
1051 | __func__, i, PTR_ERR(tty_dev)); | ||
1052 | } | ||
1053 | |||
1054 | pr_debug("%s: registered %d ttyGS* device%s\n", __func__, | ||
1055 | count, (count == 1) ? "" : "s"); | ||
1056 | |||
1057 | return status; | ||
1058 | fail: | ||
1059 | while (count--) | ||
1060 | kfree(ports[count].port); | ||
1061 | put_tty_driver(gs_tty_driver); | ||
1062 | gs_tty_driver = NULL; | ||
1063 | return status; | ||
1064 | } | ||
1065 | |||
1066 | static int gs_closed(struct gs_port *port) | ||
1067 | { | ||
1068 | int cond; | ||
1069 | |||
1070 | spin_lock_irq(&port->port_lock); | ||
1071 | cond = (port->open_count == 0) && !port->openclose; | ||
1072 | spin_unlock_irq(&port->port_lock); | ||
1073 | return cond; | ||
1074 | } | ||
1075 | |||
1076 | /** | ||
1077 | * gserial_cleanup - remove TTY-over-USB driver and devices | ||
1078 | * Context: may sleep | ||
1079 | * | ||
1080 | * This is called to free all resources allocated by @gserial_setup(). | ||
1081 | * Accordingly, it may need to wait until some open /dev/ files have | ||
1082 | * closed. | ||
1083 | * | ||
1084 | * The caller must have issued @gserial_disconnect() for any ports | ||
1085 | * that had previously been connected, so that there is never any | ||
1086 | * I/O pending when it's called. | ||
1087 | */ | ||
1088 | void gserial_cleanup(void) | ||
1089 | { | ||
1090 | unsigned i; | ||
1091 | struct gs_port *port; | ||
1092 | |||
1093 | if (!gs_tty_driver) | ||
1094 | return; | ||
1095 | |||
1096 | /* start sysfs and /dev/ttyGS* node removal */ | ||
1097 | for (i = 0; i < n_ports; i++) | ||
1098 | tty_unregister_device(gs_tty_driver, i); | ||
1099 | |||
1100 | for (i = 0; i < n_ports; i++) { | ||
1101 | /* prevent new opens */ | ||
1102 | mutex_lock(&ports[i].lock); | ||
1103 | port = ports[i].port; | ||
1104 | ports[i].port = NULL; | ||
1105 | mutex_unlock(&ports[i].lock); | ||
1106 | |||
1107 | /* wait for old opens to finish */ | ||
1108 | wait_event(port->close_wait, gs_closed(port)); | ||
1109 | |||
1110 | WARN_ON(port->port_usb != NULL); | ||
1111 | |||
1112 | kfree(port); | ||
1113 | } | ||
1114 | n_ports = 0; | ||
1115 | |||
1116 | tty_unregister_driver(gs_tty_driver); | ||
1117 | gs_tty_driver = NULL; | ||
1118 | |||
1119 | pr_debug("%s: cleaned up ttyGS* support\n", __func__); | ||
1120 | } | ||
1121 | |||
1122 | /** | ||
1123 | * gserial_connect - notify TTY I/O glue that USB link is active | ||
1124 | * @gser: the function, set up with endpoints and descriptors | ||
1125 | * @port_num: which port is active | ||
1126 | * Context: any (usually from irq) | ||
1127 | * | ||
1128 | * This is called activate endpoints and let the TTY layer know that | ||
1129 | * the connection is active ... not unlike "carrier detect". It won't | ||
1130 | * necessarily start I/O queues; unless the TTY is held open by any | ||
1131 | * task, there would be no point. However, the endpoints will be | ||
1132 | * activated so the USB host can perform I/O, subject to basic USB | ||
1133 | * hardware flow control. | ||
1134 | * | ||
1135 | * Caller needs to have set up the endpoints and USB function in @dev | ||
1136 | * before calling this, as well as the appropriate (speed-specific) | ||
1137 | * endpoint descriptors, and also have set up the TTY driver by calling | ||
1138 | * @gserial_setup(). | ||
1139 | * | ||
1140 | * Returns negative errno or zero. | ||
1141 | * On success, ep->driver_data will be overwritten. | ||
1142 | */ | ||
1143 | int gserial_connect(struct gserial *gser, u8 port_num) | ||
1144 | { | ||
1145 | struct gs_port *port; | ||
1146 | unsigned long flags; | ||
1147 | int status; | ||
1148 | |||
1149 | if (!gs_tty_driver || port_num >= n_ports) | ||
1150 | return -ENXIO; | ||
1151 | |||
1152 | /* we "know" gserial_cleanup() hasn't been called */ | ||
1153 | port = ports[port_num].port; | ||
1154 | |||
1155 | /* activate the endpoints */ | ||
1156 | status = usb_ep_enable(gser->in, gser->in_desc); | ||
1157 | if (status < 0) | ||
1158 | return status; | ||
1159 | gser->in->driver_data = port; | ||
1160 | |||
1161 | status = usb_ep_enable(gser->out, gser->out_desc); | ||
1162 | if (status < 0) | ||
1163 | goto fail_out; | ||
1164 | gser->out->driver_data = port; | ||
1165 | |||
1166 | /* then tell the tty glue that I/O can work */ | ||
1167 | spin_lock_irqsave(&port->port_lock, flags); | ||
1168 | gser->ioport = port; | ||
1169 | port->port_usb = gser; | ||
1170 | |||
1171 | /* REVISIT unclear how best to handle this state... | ||
1172 | * we don't really couple it with the Linux TTY. | ||
1173 | */ | ||
1174 | gser->port_line_coding = port->port_line_coding; | ||
1175 | |||
1176 | /* REVISIT if waiting on "carrier detect", signal. */ | ||
1177 | |||
1178 | /* REVISIT for ACM, issue "network connection" status notification: | ||
1179 | * connected if open_count, else disconnected. | ||
1180 | */ | ||
1181 | |||
1182 | /* if it's already open, start I/O */ | ||
1183 | if (port->open_count) { | ||
1184 | pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); | ||
1185 | gs_start_io(port); | ||
1186 | } | ||
1187 | |||
1188 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
1189 | |||
1190 | return status; | ||
1191 | |||
1192 | fail_out: | ||
1193 | usb_ep_disable(gser->in); | ||
1194 | gser->in->driver_data = NULL; | ||
1195 | return status; | ||
1196 | } | ||
1197 | |||
1198 | /** | ||
1199 | * gserial_disconnect - notify TTY I/O glue that USB link is inactive | ||
1200 | * @gser: the function, on which gserial_connect() was called | ||
1201 | * Context: any (usually from irq) | ||
1202 | * | ||
1203 | * This is called to deactivate endpoints and let the TTY layer know | ||
1204 | * that the connection went inactive ... not unlike "hangup". | ||
1205 | * | ||
1206 | * On return, the state is as if gserial_connect() had never been called; | ||
1207 | * there is no active USB I/O on these endpoints. | ||
1208 | */ | ||
1209 | void gserial_disconnect(struct gserial *gser) | ||
1210 | { | ||
1211 | struct gs_port *port = gser->ioport; | ||
1212 | unsigned long flags; | ||
1213 | |||
1214 | if (!port) | ||
1215 | return; | ||
1216 | |||
1217 | /* tell the TTY glue not to do I/O here any more */ | ||
1218 | spin_lock_irqsave(&port->port_lock, flags); | ||
1219 | |||
1220 | /* REVISIT as above: how best to track this? */ | ||
1221 | port->port_line_coding = gser->port_line_coding; | ||
1222 | |||
1223 | port->port_usb = NULL; | ||
1224 | gser->ioport = NULL; | ||
1225 | if (port->open_count > 0 || port->openclose) { | ||
1226 | wake_up_interruptible(&port->drain_wait); | ||
1227 | if (port->port_tty) | ||
1228 | tty_hangup(port->port_tty); | ||
1229 | } | ||
1230 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
1231 | |||
1232 | /* disable endpoints, aborting down any active I/O */ | ||
1233 | usb_ep_disable(gser->out); | ||
1234 | gser->out->driver_data = NULL; | ||
1235 | |||
1236 | usb_ep_disable(gser->in); | ||
1237 | gser->in->driver_data = NULL; | ||
1238 | |||
1239 | /* finally, free any unused/unusable I/O buffers */ | ||
1240 | spin_lock_irqsave(&port->port_lock, flags); | ||
1241 | if (port->open_count == 0 && !port->openclose) | ||
1242 | gs_buf_free(&port->port_write_buf); | ||
1243 | gs_free_requests(gser->out, &port->read_pool); | ||
1244 | gs_free_requests(gser->in, &port->write_pool); | ||
1245 | spin_unlock_irqrestore(&port->port_lock, flags); | ||
1246 | } | ||
diff --git a/drivers/usb/gadget/u_serial.h b/drivers/usb/gadget/u_serial.h new file mode 100644 index 000000000000..7b561138f90e --- /dev/null +++ b/drivers/usb/gadget/u_serial.h | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * u_serial.h - interface to USB gadget "serial port"/TTY utilities | ||
3 | * | ||
4 | * Copyright (C) 2008 David Brownell | ||
5 | * Copyright (C) 2008 by Nokia Corporation | ||
6 | * | ||
7 | * This software is distributed under the terms of the GNU General | ||
8 | * Public License ("GPL") as published by the Free Software Foundation, | ||
9 | * either version 2 of that License or (at your option) any later version. | ||
10 | */ | ||
11 | |||
12 | #ifndef __U_SERIAL_H | ||
13 | #define __U_SERIAL_H | ||
14 | |||
15 | #include <linux/usb/composite.h> | ||
16 | #include <linux/usb/cdc.h> | ||
17 | |||
18 | /* | ||
19 | * One non-multiplexed "serial" I/O port ... there can be several of these | ||
20 | * on any given USB peripheral device, if it provides enough endpoints. | ||
21 | * | ||
22 | * The "u_serial" utility component exists to do one thing: manage TTY | ||
23 | * style I/O using the USB peripheral endpoints listed here, including | ||
24 | * hookups to sysfs and /dev for each logical "tty" device. | ||
25 | * | ||
26 | * REVISIT need TTY --> USB event flow too, so ACM can report open/close | ||
27 | * as carrier detect events. Model after ECM. There's more ACM state too. | ||
28 | * | ||
29 | * REVISIT someday, allow multiplexing several TTYs over these endpoints. | ||
30 | */ | ||
31 | struct gserial { | ||
32 | struct usb_function func; | ||
33 | |||
34 | /* port is managed by gserial_{connect,disconnect} */ | ||
35 | struct gs_port *ioport; | ||
36 | |||
37 | struct usb_ep *in; | ||
38 | struct usb_ep *out; | ||
39 | struct usb_endpoint_descriptor *in_desc; | ||
40 | struct usb_endpoint_descriptor *out_desc; | ||
41 | |||
42 | /* REVISIT avoid this CDC-ACM support harder ... */ | ||
43 | struct usb_cdc_line_coding port_line_coding; /* 9600-8-N-1 etc */ | ||
44 | }; | ||
45 | |||
46 | /* port setup/teardown is handled by gadget driver */ | ||
47 | int gserial_setup(struct usb_gadget *g, unsigned n_ports); | ||
48 | void gserial_cleanup(void); | ||
49 | |||
50 | /* connect/disconnect is handled by individual functions */ | ||
51 | int gserial_connect(struct gserial *, u8 port_num); | ||
52 | void gserial_disconnect(struct gserial *); | ||
53 | |||
54 | /* functions are bound to configurations by a config or gadget driver */ | ||
55 | int acm_bind_config(struct usb_configuration *c, u8 port_num); | ||
56 | int gser_bind_config(struct usb_configuration *c, u8 port_num); | ||
57 | |||
58 | #endif /* __U_SERIAL_H */ | ||
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c index fce4924dbbe8..aa0bd4f126a1 100644 --- a/drivers/usb/gadget/zero.c +++ b/drivers/usb/gadget/zero.c | |||
@@ -1,8 +1,8 @@ | |||
1 | /* | 1 | /* |
2 | * zero.c -- Gadget Zero, for USB development | 2 | * zero.c -- Gadget Zero, for USB development |
3 | * | 3 | * |
4 | * Copyright (C) 2003-2007 David Brownell | 4 | * Copyright (C) 2003-2008 David Brownell |
5 | * All rights reserved. | 5 | * Copyright (C) 2008 by Nokia Corporation |
6 | * | 6 | * |
7 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | 8 | * it under the terms of the GNU General Public License as published by |
@@ -30,12 +30,7 @@ | |||
30 | * | 30 | * |
31 | * It supports two similar configurations. One sinks whatever the usb host | 31 | * It supports two similar configurations. One sinks whatever the usb host |
32 | * writes, and in return sources zeroes. The other loops whatever the host | 32 | * writes, and in return sources zeroes. The other loops whatever the host |
33 | * writes back, so the host can read it. Module options include: | 33 | * writes back, so the host can read it. |
34 | * | ||
35 | * buflen=N default N=4096, buffer size used | ||
36 | * qlen=N default N=32, how many buffers in the loopback queue | ||
37 | * loopdefault default false, list loopback config first | ||
38 | * autoresume=N default N=0, seconds before triggering remote wakeup | ||
39 | * | 34 | * |
40 | * Many drivers will only have one configuration, letting them be much | 35 | * Many drivers will only have one configuration, letting them be much |
41 | * simpler if they also don't support high speed operation (like this | 36 | * simpler if they also don't support high speed operation (like this |
@@ -47,94 +42,35 @@ | |||
47 | * work with low capability USB controllers without four bulk endpoints. | 42 | * work with low capability USB controllers without four bulk endpoints. |
48 | */ | 43 | */ |
49 | 44 | ||
45 | /* | ||
46 | * driver assumes self-powered hardware, and | ||
47 | * has no way for users to trigger remote wakeup. | ||
48 | */ | ||
49 | |||
50 | /* #define VERBOSE_DEBUG */ | 50 | /* #define VERBOSE_DEBUG */ |
51 | 51 | ||
52 | #include <linux/kernel.h> | 52 | #include <linux/kernel.h> |
53 | #include <linux/utsname.h> | 53 | #include <linux/utsname.h> |
54 | #include <linux/device.h> | 54 | #include <linux/device.h> |
55 | 55 | ||
56 | #include <linux/usb/ch9.h> | 56 | #include "g_zero.h" |
57 | #include <linux/usb/gadget.h> | ||
58 | |||
59 | #include "gadget_chips.h" | 57 | #include "gadget_chips.h" |
60 | 58 | ||
61 | 59 | ||
62 | /*-------------------------------------------------------------------------*/ | 60 | /*-------------------------------------------------------------------------*/ |
63 | 61 | ||
64 | #define DRIVER_VERSION "Earth Day 2008" | 62 | #define DRIVER_VERSION "Cinco de Mayo 2008" |
65 | 63 | ||
66 | static const char shortname[] = "zero"; | ||
67 | static const char longname[] = "Gadget Zero"; | 64 | static const char longname[] = "Gadget Zero"; |
68 | 65 | ||
69 | static const char source_sink[] = "source and sink data"; | 66 | unsigned buflen = 4096; |
70 | static const char loopback[] = "loop input to output"; | 67 | module_param(buflen, uint, 0); |
71 | |||
72 | /*-------------------------------------------------------------------------*/ | ||
73 | |||
74 | /* | ||
75 | * driver assumes self-powered hardware, and | ||
76 | * has no way for users to trigger remote wakeup. | ||
77 | * | ||
78 | * this version autoconfigures as much as possible, | ||
79 | * which is reasonable for most "bulk-only" drivers. | ||
80 | */ | ||
81 | static const char *EP_IN_NAME; /* source */ | ||
82 | static const char *EP_OUT_NAME; /* sink */ | ||
83 | |||
84 | /*-------------------------------------------------------------------------*/ | ||
85 | |||
86 | /* big enough to hold our biggest descriptor */ | ||
87 | #define USB_BUFSIZ 256 | ||
88 | |||
89 | struct zero_dev { | ||
90 | spinlock_t lock; | ||
91 | struct usb_gadget *gadget; | ||
92 | struct usb_request *req; /* for control responses */ | ||
93 | |||
94 | /* when configured, we have one of two configs: | ||
95 | * - source data (in to host) and sink it (out from host) | ||
96 | * - or loop it back (out from host back in to host) | ||
97 | */ | ||
98 | u8 config; | ||
99 | struct usb_ep *in_ep, *out_ep; | ||
100 | |||
101 | /* autoresume timer */ | ||
102 | struct timer_list resume; | ||
103 | }; | ||
104 | |||
105 | #define DBG(d, fmt, args...) \ | ||
106 | dev_dbg(&(d)->gadget->dev , fmt , ## args) | ||
107 | #define VDBG(d, fmt, args...) \ | ||
108 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) | ||
109 | #define ERROR(d, fmt, args...) \ | ||
110 | dev_err(&(d)->gadget->dev , fmt , ## args) | ||
111 | #define WARN(d, fmt, args...) \ | ||
112 | dev_warn(&(d)->gadget->dev , fmt , ## args) | ||
113 | #define INFO(d, fmt, args...) \ | ||
114 | dev_info(&(d)->gadget->dev , fmt , ## args) | ||
115 | |||
116 | /*-------------------------------------------------------------------------*/ | ||
117 | |||
118 | static unsigned buflen = 4096; | ||
119 | static unsigned qlen = 32; | ||
120 | static unsigned pattern = 0; | ||
121 | |||
122 | module_param(buflen, uint, S_IRUGO); | ||
123 | module_param(qlen, uint, S_IRUGO); | ||
124 | module_param(pattern, uint, S_IRUGO|S_IWUSR); | ||
125 | |||
126 | /* | ||
127 | * if it's nonzero, autoresume says how many seconds to wait | ||
128 | * before trying to wake up the host after suspend. | ||
129 | */ | ||
130 | static unsigned autoresume = 0; | ||
131 | module_param(autoresume, uint, 0); | ||
132 | 68 | ||
133 | /* | 69 | /* |
134 | * Normally the "loopback" configuration is second (index 1) so | 70 | * Normally the "loopback" configuration is second (index 1) so |
135 | * it's not the default. Here's where to change that order, to | 71 | * it's not the default. Here's where to change that order, to |
136 | * work better with hosts where config changes are problematic. | 72 | * work better with hosts where config changes are problematic or |
137 | * Or controllers (like superh) that only support one config. | 73 | * controllers (like original superh) that only support one config. |
138 | */ | 74 | */ |
139 | static int loopdefault = 0; | 75 | static int loopdefault = 0; |
140 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); | 76 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); |
@@ -156,24 +92,6 @@ module_param(loopdefault, bool, S_IRUGO|S_IWUSR); | |||
156 | 92 | ||
157 | /*-------------------------------------------------------------------------*/ | 93 | /*-------------------------------------------------------------------------*/ |
158 | 94 | ||
159 | /* | ||
160 | * DESCRIPTORS ... most are static, but strings and (full) | ||
161 | * configuration descriptors are built on demand. | ||
162 | */ | ||
163 | |||
164 | #define STRING_MANUFACTURER 25 | ||
165 | #define STRING_PRODUCT 42 | ||
166 | #define STRING_SERIAL 101 | ||
167 | #define STRING_SOURCE_SINK 250 | ||
168 | #define STRING_LOOPBACK 251 | ||
169 | |||
170 | /* | ||
171 | * This device advertises two configurations; these numbers work | ||
172 | * on a pxa250 as well as more flexible hardware. | ||
173 | */ | ||
174 | #define CONFIG_SOURCE_SINK 3 | ||
175 | #define CONFIG_LOOPBACK 2 | ||
176 | |||
177 | static struct usb_device_descriptor device_desc = { | 95 | static struct usb_device_descriptor device_desc = { |
178 | .bLength = sizeof device_desc, | 96 | .bLength = sizeof device_desc, |
179 | .bDescriptorType = USB_DT_DEVICE, | 97 | .bDescriptorType = USB_DT_DEVICE, |
@@ -183,248 +101,64 @@ static struct usb_device_descriptor device_desc = { | |||
183 | 101 | ||
184 | .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM), | 102 | .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM), |
185 | .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM), | 103 | .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM), |
186 | .iManufacturer = STRING_MANUFACTURER, | ||
187 | .iProduct = STRING_PRODUCT, | ||
188 | .iSerialNumber = STRING_SERIAL, | ||
189 | .bNumConfigurations = 2, | 104 | .bNumConfigurations = 2, |
190 | }; | 105 | }; |
191 | 106 | ||
192 | static struct usb_config_descriptor source_sink_config = { | 107 | #ifdef CONFIG_USB_OTG |
193 | .bLength = sizeof source_sink_config, | ||
194 | .bDescriptorType = USB_DT_CONFIG, | ||
195 | |||
196 | /* compute wTotalLength on the fly */ | ||
197 | .bNumInterfaces = 1, | ||
198 | .bConfigurationValue = CONFIG_SOURCE_SINK, | ||
199 | .iConfiguration = STRING_SOURCE_SINK, | ||
200 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
201 | .bMaxPower = 1, /* self-powered */ | ||
202 | }; | ||
203 | |||
204 | static struct usb_config_descriptor loopback_config = { | ||
205 | .bLength = sizeof loopback_config, | ||
206 | .bDescriptorType = USB_DT_CONFIG, | ||
207 | |||
208 | /* compute wTotalLength on the fly */ | ||
209 | .bNumInterfaces = 1, | ||
210 | .bConfigurationValue = CONFIG_LOOPBACK, | ||
211 | .iConfiguration = STRING_LOOPBACK, | ||
212 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | ||
213 | .bMaxPower = 1, /* self-powered */ | ||
214 | }; | ||
215 | |||
216 | static struct usb_otg_descriptor otg_descriptor = { | 108 | static struct usb_otg_descriptor otg_descriptor = { |
217 | .bLength = sizeof otg_descriptor, | 109 | .bLength = sizeof otg_descriptor, |
218 | .bDescriptorType = USB_DT_OTG, | 110 | .bDescriptorType = USB_DT_OTG, |
219 | 111 | ||
220 | .bmAttributes = USB_OTG_SRP, | 112 | /* REVISIT SRP-only hardware is possible, although |
221 | }; | 113 | * it would not be called "OTG" ... |
222 | 114 | */ | |
223 | /* one interface in each configuration */ | 115 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
224 | |||
225 | static const struct usb_interface_descriptor source_sink_intf = { | ||
226 | .bLength = sizeof source_sink_intf, | ||
227 | .bDescriptorType = USB_DT_INTERFACE, | ||
228 | |||
229 | .bNumEndpoints = 2, | ||
230 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
231 | .iInterface = STRING_SOURCE_SINK, | ||
232 | }; | ||
233 | |||
234 | static const struct usb_interface_descriptor loopback_intf = { | ||
235 | .bLength = sizeof loopback_intf, | ||
236 | .bDescriptorType = USB_DT_INTERFACE, | ||
237 | |||
238 | .bNumEndpoints = 2, | ||
239 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | ||
240 | .iInterface = STRING_LOOPBACK, | ||
241 | }; | ||
242 | |||
243 | /* two full speed bulk endpoints; their use is config-dependent */ | ||
244 | |||
245 | static struct usb_endpoint_descriptor fs_source_desc = { | ||
246 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
247 | .bDescriptorType = USB_DT_ENDPOINT, | ||
248 | |||
249 | .bEndpointAddress = USB_DIR_IN, | ||
250 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
251 | }; | ||
252 | |||
253 | static struct usb_endpoint_descriptor fs_sink_desc = { | ||
254 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
255 | .bDescriptorType = USB_DT_ENDPOINT, | ||
256 | |||
257 | .bEndpointAddress = USB_DIR_OUT, | ||
258 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
259 | }; | ||
260 | |||
261 | static const struct usb_descriptor_header *fs_source_sink_function[] = { | ||
262 | (struct usb_descriptor_header *) &otg_descriptor, | ||
263 | (struct usb_descriptor_header *) &source_sink_intf, | ||
264 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
265 | (struct usb_descriptor_header *) &fs_source_desc, | ||
266 | NULL, | ||
267 | }; | ||
268 | |||
269 | static const struct usb_descriptor_header *fs_loopback_function[] = { | ||
270 | (struct usb_descriptor_header *) &otg_descriptor, | ||
271 | (struct usb_descriptor_header *) &loopback_intf, | ||
272 | (struct usb_descriptor_header *) &fs_sink_desc, | ||
273 | (struct usb_descriptor_header *) &fs_source_desc, | ||
274 | NULL, | ||
275 | }; | ||
276 | |||
277 | /* | ||
278 | * usb 2.0 devices need to expose both high speed and full speed | ||
279 | * descriptors, unless they only run at full speed. | ||
280 | * | ||
281 | * that means alternate endpoint descriptors (bigger packets) | ||
282 | * and a "device qualifier" ... plus more construction options | ||
283 | * for the config descriptor. | ||
284 | */ | ||
285 | |||
286 | static struct usb_endpoint_descriptor hs_source_desc = { | ||
287 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
288 | .bDescriptorType = USB_DT_ENDPOINT, | ||
289 | |||
290 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
291 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
292 | }; | ||
293 | |||
294 | static struct usb_endpoint_descriptor hs_sink_desc = { | ||
295 | .bLength = USB_DT_ENDPOINT_SIZE, | ||
296 | .bDescriptorType = USB_DT_ENDPOINT, | ||
297 | |||
298 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | ||
299 | .wMaxPacketSize = __constant_cpu_to_le16(512), | ||
300 | }; | ||
301 | |||
302 | static struct usb_qualifier_descriptor dev_qualifier = { | ||
303 | .bLength = sizeof dev_qualifier, | ||
304 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | ||
305 | |||
306 | .bcdUSB = __constant_cpu_to_le16(0x0200), | ||
307 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, | ||
308 | |||
309 | .bNumConfigurations = 2, | ||
310 | }; | 116 | }; |
311 | 117 | ||
312 | static const struct usb_descriptor_header *hs_source_sink_function[] = { | 118 | const struct usb_descriptor_header *otg_desc[] = { |
313 | (struct usb_descriptor_header *) &otg_descriptor, | 119 | (struct usb_descriptor_header *) &otg_descriptor, |
314 | (struct usb_descriptor_header *) &source_sink_intf, | ||
315 | (struct usb_descriptor_header *) &hs_source_desc, | ||
316 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
317 | NULL, | 120 | NULL, |
318 | }; | 121 | }; |
122 | #endif | ||
319 | 123 | ||
320 | static const struct usb_descriptor_header *hs_loopback_function[] = { | 124 | /* string IDs are assigned dynamically */ |
321 | (struct usb_descriptor_header *) &otg_descriptor, | ||
322 | (struct usb_descriptor_header *) &loopback_intf, | ||
323 | (struct usb_descriptor_header *) &hs_source_desc, | ||
324 | (struct usb_descriptor_header *) &hs_sink_desc, | ||
325 | NULL, | ||
326 | }; | ||
327 | 125 | ||
328 | /* maxpacket and other transfer characteristics vary by speed. */ | 126 | #define STRING_MANUFACTURER_IDX 0 |
329 | static inline struct usb_endpoint_descriptor * | 127 | #define STRING_PRODUCT_IDX 1 |
330 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | 128 | #define STRING_SERIAL_IDX 2 |
331 | struct usb_endpoint_descriptor *fs) | ||
332 | { | ||
333 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
334 | return hs; | ||
335 | return fs; | ||
336 | } | ||
337 | 129 | ||
338 | static char manufacturer[50]; | 130 | static char manufacturer[50]; |
339 | 131 | ||
340 | /* default serial number takes at least two packets */ | 132 | /* default serial number takes at least two packets */ |
341 | static char serial[] = "0123456789.0123456789.0123456789"; | 133 | static char serial[] = "0123456789.0123456789.0123456789"; |
342 | 134 | ||
343 | 135 | static struct usb_string strings_dev[] = { | |
344 | /* static strings, in UTF-8 */ | 136 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
345 | static struct usb_string strings[] = { | 137 | [STRING_PRODUCT_IDX].s = longname, |
346 | { STRING_MANUFACTURER, manufacturer, }, | 138 | [STRING_SERIAL_IDX].s = serial, |
347 | { STRING_PRODUCT, longname, }, | ||
348 | { STRING_SERIAL, serial, }, | ||
349 | { STRING_LOOPBACK, loopback, }, | ||
350 | { STRING_SOURCE_SINK, source_sink, }, | ||
351 | { } /* end of list */ | 139 | { } /* end of list */ |
352 | }; | 140 | }; |
353 | 141 | ||
354 | static struct usb_gadget_strings stringtab = { | 142 | static struct usb_gadget_strings stringtab_dev = { |
355 | .language = 0x0409, /* en-us */ | 143 | .language = 0x0409, /* en-us */ |
356 | .strings = strings, | 144 | .strings = strings_dev, |
357 | }; | 145 | }; |
358 | 146 | ||
359 | /* | 147 | static struct usb_gadget_strings *dev_strings[] = { |
360 | * config descriptors are also handcrafted. these must agree with code | 148 | &stringtab_dev, |
361 | * that sets configurations, and with code managing interfaces and their | 149 | NULL, |
362 | * altsettings. other complexity may come from: | 150 | }; |
363 | * | ||
364 | * - high speed support, including "other speed config" rules | ||
365 | * - multiple configurations | ||
366 | * - interfaces with alternate settings | ||
367 | * - embedded class or vendor-specific descriptors | ||
368 | * | ||
369 | * this handles high speed, and has a second config that could as easily | ||
370 | * have been an alternate interface setting (on most hardware). | ||
371 | * | ||
372 | * NOTE: to demonstrate (and test) more USB capabilities, this driver | ||
373 | * should include an altsetting to test interrupt transfers, including | ||
374 | * high bandwidth modes at high speed. (Maybe work like Intel's test | ||
375 | * device?) | ||
376 | */ | ||
377 | static int config_buf(struct usb_gadget *gadget, | ||
378 | u8 *buf, u8 type, unsigned index) | ||
379 | { | ||
380 | int is_source_sink; | ||
381 | int len; | ||
382 | const struct usb_descriptor_header **function; | ||
383 | int hs = 0; | ||
384 | |||
385 | /* two configurations will always be index 0 and index 1 */ | ||
386 | if (index > 1) | ||
387 | return -EINVAL; | ||
388 | is_source_sink = loopdefault ? (index == 1) : (index == 0); | ||
389 | |||
390 | if (gadget_is_dualspeed(gadget)) { | ||
391 | hs = (gadget->speed == USB_SPEED_HIGH); | ||
392 | if (type == USB_DT_OTHER_SPEED_CONFIG) | ||
393 | hs = !hs; | ||
394 | } | ||
395 | if (hs) | ||
396 | function = is_source_sink | ||
397 | ? hs_source_sink_function | ||
398 | : hs_loopback_function; | ||
399 | else | ||
400 | function = is_source_sink | ||
401 | ? fs_source_sink_function | ||
402 | : fs_loopback_function; | ||
403 | |||
404 | /* for now, don't advertise srp-only devices */ | ||
405 | if (!gadget_is_otg(gadget)) | ||
406 | function++; | ||
407 | |||
408 | len = usb_gadget_config_buf(is_source_sink | ||
409 | ? &source_sink_config | ||
410 | : &loopback_config, | ||
411 | buf, USB_BUFSIZ, function); | ||
412 | if (len < 0) | ||
413 | return len; | ||
414 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; | ||
415 | return len; | ||
416 | } | ||
417 | 151 | ||
418 | /*-------------------------------------------------------------------------*/ | 152 | /*-------------------------------------------------------------------------*/ |
419 | 153 | ||
420 | static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) | 154 | struct usb_request *alloc_ep_req(struct usb_ep *ep) |
421 | { | 155 | { |
422 | struct usb_request *req; | 156 | struct usb_request *req; |
423 | 157 | ||
424 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); | 158 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
425 | if (req) { | 159 | if (req) { |
426 | req->length = length; | 160 | req->length = buflen; |
427 | req->buf = kmalloc(length, GFP_ATOMIC); | 161 | req->buf = kmalloc(buflen, GFP_ATOMIC); |
428 | if (!req->buf) { | 162 | if (!req->buf) { |
429 | usb_ep_free_request(ep, req); | 163 | usb_ep_free_request(ep, req); |
430 | req = NULL; | 164 | req = NULL; |
@@ -433,681 +167,73 @@ static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) | |||
433 | return req; | 167 | return req; |
434 | } | 168 | } |
435 | 169 | ||
436 | static void free_ep_req(struct usb_ep *ep, struct usb_request *req) | 170 | void free_ep_req(struct usb_ep *ep, struct usb_request *req) |
437 | { | 171 | { |
438 | kfree(req->buf); | 172 | kfree(req->buf); |
439 | usb_ep_free_request(ep, req); | 173 | usb_ep_free_request(ep, req); |
440 | } | 174 | } |
441 | 175 | ||
442 | /*-------------------------------------------------------------------------*/ | 176 | static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) |
443 | |||
444 | /* | ||
445 | * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals, | ||
446 | * this just sinks bulk packets OUT to the peripheral and sources them IN | ||
447 | * to the host, optionally with specific data patterns. | ||
448 | * | ||
449 | * In terms of control messaging, this supports all the standard requests | ||
450 | * plus two that support control-OUT tests. | ||
451 | * | ||
452 | * Note that because this doesn't queue more than one request at a time, | ||
453 | * some other function must be used to test queueing logic. The network | ||
454 | * link (g_ether) is probably the best option for that. | ||
455 | */ | ||
456 | |||
457 | /* optionally require specific source/sink data patterns */ | ||
458 | |||
459 | static int | ||
460 | check_read_data( | ||
461 | struct zero_dev *dev, | ||
462 | struct usb_ep *ep, | ||
463 | struct usb_request *req | ||
464 | ) | ||
465 | { | 177 | { |
466 | unsigned i; | 178 | int value; |
467 | u8 *buf = req->buf; | 179 | |
468 | 180 | if (ep->driver_data) { | |
469 | for (i = 0; i < req->actual; i++, buf++) { | 181 | value = usb_ep_disable(ep); |
470 | switch (pattern) { | 182 | if (value < 0) |
471 | /* all-zeroes has no synchronization issues */ | 183 | DBG(cdev, "disable %s --> %d\n", |
472 | case 0: | 184 | ep->name, value); |
473 | if (*buf == 0) | 185 | ep->driver_data = NULL; |
474 | continue; | ||
475 | break; | ||
476 | /* mod63 stays in sync with short-terminated transfers, | ||
477 | * or otherwise when host and gadget agree on how large | ||
478 | * each usb transfer request should be. resync is done | ||
479 | * with set_interface or set_config. | ||
480 | */ | ||
481 | case 1: | ||
482 | if (*buf == (u8)(i % 63)) | ||
483 | continue; | ||
484 | break; | ||
485 | } | ||
486 | ERROR(dev, "bad OUT byte, buf[%d] = %d\n", i, *buf); | ||
487 | usb_ep_set_halt(ep); | ||
488 | return -EINVAL; | ||
489 | } | 186 | } |
490 | return 0; | ||
491 | } | 187 | } |
492 | 188 | ||
493 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) | 189 | void disable_endpoints(struct usb_composite_dev *cdev, |
190 | struct usb_ep *in, struct usb_ep *out) | ||
494 | { | 191 | { |
495 | unsigned i; | 192 | disable_ep(cdev, in); |
496 | u8 *buf = req->buf; | 193 | disable_ep(cdev, out); |
497 | |||
498 | switch (pattern) { | ||
499 | case 0: | ||
500 | memset(req->buf, 0, req->length); | ||
501 | break; | ||
502 | case 1: | ||
503 | for (i = 0; i < req->length; i++) | ||
504 | *buf++ = (u8) (i % 63); | ||
505 | break; | ||
506 | } | ||
507 | } | ||
508 | |||
509 | /* if there is only one request in the queue, there'll always be an | ||
510 | * irq delay between end of one request and start of the next. | ||
511 | * that prevents using hardware dma queues. | ||
512 | */ | ||
513 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) | ||
514 | { | ||
515 | struct zero_dev *dev = ep->driver_data; | ||
516 | int status = req->status; | ||
517 | |||
518 | switch (status) { | ||
519 | |||
520 | case 0: /* normal completion? */ | ||
521 | if (ep == dev->out_ep) { | ||
522 | check_read_data(dev, ep, req); | ||
523 | memset(req->buf, 0x55, req->length); | ||
524 | } else | ||
525 | reinit_write_data(ep, req); | ||
526 | break; | ||
527 | |||
528 | /* this endpoint is normally active while we're configured */ | ||
529 | case -ECONNABORTED: /* hardware forced ep reset */ | ||
530 | case -ECONNRESET: /* request dequeued */ | ||
531 | case -ESHUTDOWN: /* disconnect from host */ | ||
532 | VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status, | ||
533 | req->actual, req->length); | ||
534 | if (ep == dev->out_ep) | ||
535 | check_read_data(dev, ep, req); | ||
536 | free_ep_req(ep, req); | ||
537 | return; | ||
538 | |||
539 | case -EOVERFLOW: /* buffer overrun on read means that | ||
540 | * we didn't provide a big enough | ||
541 | * buffer. | ||
542 | */ | ||
543 | default: | ||
544 | #if 1 | ||
545 | DBG(dev, "%s complete --> %d, %d/%d\n", ep->name, | ||
546 | status, req->actual, req->length); | ||
547 | #endif | ||
548 | case -EREMOTEIO: /* short read */ | ||
549 | break; | ||
550 | } | ||
551 | |||
552 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
553 | if (status) { | ||
554 | ERROR(dev, "kill %s: resubmit %d bytes --> %d\n", | ||
555 | ep->name, req->length, status); | ||
556 | usb_ep_set_halt(ep); | ||
557 | /* FIXME recover later ... somehow */ | ||
558 | } | ||
559 | } | ||
560 | |||
561 | static struct usb_request *source_sink_start_ep(struct usb_ep *ep) | ||
562 | { | ||
563 | struct usb_request *req; | ||
564 | int status; | ||
565 | |||
566 | req = alloc_ep_req(ep, buflen); | ||
567 | if (!req) | ||
568 | return NULL; | ||
569 | |||
570 | memset(req->buf, 0, req->length); | ||
571 | req->complete = source_sink_complete; | ||
572 | |||
573 | if (strcmp(ep->name, EP_IN_NAME) == 0) | ||
574 | reinit_write_data(ep, req); | ||
575 | else | ||
576 | memset(req->buf, 0x55, req->length); | ||
577 | |||
578 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
579 | if (status) { | ||
580 | struct zero_dev *dev = ep->driver_data; | ||
581 | |||
582 | ERROR(dev, "start %s --> %d\n", ep->name, status); | ||
583 | free_ep_req(ep, req); | ||
584 | req = NULL; | ||
585 | } | ||
586 | |||
587 | return req; | ||
588 | } | ||
589 | |||
590 | static int set_source_sink_config(struct zero_dev *dev) | ||
591 | { | ||
592 | int result = 0; | ||
593 | struct usb_ep *ep; | ||
594 | struct usb_gadget *gadget = dev->gadget; | ||
595 | |||
596 | gadget_for_each_ep(ep, gadget) { | ||
597 | const struct usb_endpoint_descriptor *d; | ||
598 | |||
599 | /* one endpoint writes (sources) zeroes in (to the host) */ | ||
600 | if (strcmp(ep->name, EP_IN_NAME) == 0) { | ||
601 | d = ep_desc(gadget, &hs_source_desc, &fs_source_desc); | ||
602 | result = usb_ep_enable(ep, d); | ||
603 | if (result == 0) { | ||
604 | ep->driver_data = dev; | ||
605 | if (source_sink_start_ep(ep) != NULL) { | ||
606 | dev->in_ep = ep; | ||
607 | continue; | ||
608 | } | ||
609 | usb_ep_disable(ep); | ||
610 | result = -EIO; | ||
611 | } | ||
612 | |||
613 | /* one endpoint reads (sinks) anything out (from the host) */ | ||
614 | } else if (strcmp(ep->name, EP_OUT_NAME) == 0) { | ||
615 | d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); | ||
616 | result = usb_ep_enable(ep, d); | ||
617 | if (result == 0) { | ||
618 | ep->driver_data = dev; | ||
619 | if (source_sink_start_ep(ep) != NULL) { | ||
620 | dev->out_ep = ep; | ||
621 | continue; | ||
622 | } | ||
623 | usb_ep_disable(ep); | ||
624 | result = -EIO; | ||
625 | } | ||
626 | |||
627 | /* ignore any other endpoints */ | ||
628 | } else | ||
629 | continue; | ||
630 | |||
631 | /* stop on error */ | ||
632 | ERROR(dev, "can't start %s, result %d\n", ep->name, result); | ||
633 | break; | ||
634 | } | ||
635 | if (result == 0) | ||
636 | DBG(dev, "buflen %d\n", buflen); | ||
637 | |||
638 | /* caller is responsible for cleanup on error */ | ||
639 | return result; | ||
640 | } | ||
641 | |||
642 | /*-------------------------------------------------------------------------*/ | ||
643 | |||
644 | static void loopback_complete(struct usb_ep *ep, struct usb_request *req) | ||
645 | { | ||
646 | struct zero_dev *dev = ep->driver_data; | ||
647 | int status = req->status; | ||
648 | |||
649 | switch (status) { | ||
650 | |||
651 | case 0: /* normal completion? */ | ||
652 | if (ep == dev->out_ep) { | ||
653 | /* loop this OUT packet back IN to the host */ | ||
654 | req->zero = (req->actual < req->length); | ||
655 | req->length = req->actual; | ||
656 | status = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); | ||
657 | if (status == 0) | ||
658 | return; | ||
659 | |||
660 | /* "should never get here" */ | ||
661 | ERROR(dev, "can't loop %s to %s: %d\n", | ||
662 | ep->name, dev->in_ep->name, | ||
663 | status); | ||
664 | } | ||
665 | |||
666 | /* queue the buffer for some later OUT packet */ | ||
667 | req->length = buflen; | ||
668 | status = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); | ||
669 | if (status == 0) | ||
670 | return; | ||
671 | |||
672 | /* "should never get here" */ | ||
673 | /* FALLTHROUGH */ | ||
674 | |||
675 | default: | ||
676 | ERROR(dev, "%s loop complete --> %d, %d/%d\n", ep->name, | ||
677 | status, req->actual, req->length); | ||
678 | /* FALLTHROUGH */ | ||
679 | |||
680 | /* NOTE: since this driver doesn't maintain an explicit record | ||
681 | * of requests it submitted (just maintains qlen count), we | ||
682 | * rely on the hardware driver to clean up on disconnect or | ||
683 | * endpoint disable. | ||
684 | */ | ||
685 | case -ECONNABORTED: /* hardware forced ep reset */ | ||
686 | case -ECONNRESET: /* request dequeued */ | ||
687 | case -ESHUTDOWN: /* disconnect from host */ | ||
688 | free_ep_req(ep, req); | ||
689 | return; | ||
690 | } | ||
691 | } | ||
692 | |||
693 | static int set_loopback_config(struct zero_dev *dev) | ||
694 | { | ||
695 | int result = 0; | ||
696 | struct usb_ep *ep; | ||
697 | struct usb_gadget *gadget = dev->gadget; | ||
698 | |||
699 | gadget_for_each_ep(ep, gadget) { | ||
700 | const struct usb_endpoint_descriptor *d; | ||
701 | |||
702 | /* one endpoint writes data back IN to the host */ | ||
703 | if (strcmp(ep->name, EP_IN_NAME) == 0) { | ||
704 | d = ep_desc(gadget, &hs_source_desc, &fs_source_desc); | ||
705 | result = usb_ep_enable(ep, d); | ||
706 | if (result == 0) { | ||
707 | ep->driver_data = dev; | ||
708 | dev->in_ep = ep; | ||
709 | continue; | ||
710 | } | ||
711 | |||
712 | /* one endpoint just reads OUT packets */ | ||
713 | } else if (strcmp(ep->name, EP_OUT_NAME) == 0) { | ||
714 | d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); | ||
715 | result = usb_ep_enable(ep, d); | ||
716 | if (result == 0) { | ||
717 | ep->driver_data = dev; | ||
718 | dev->out_ep = ep; | ||
719 | continue; | ||
720 | } | ||
721 | |||
722 | /* ignore any other endpoints */ | ||
723 | } else | ||
724 | continue; | ||
725 | |||
726 | /* stop on error */ | ||
727 | ERROR(dev, "can't enable %s, result %d\n", ep->name, result); | ||
728 | break; | ||
729 | } | ||
730 | |||
731 | /* allocate a bunch of read buffers and queue them all at once. | ||
732 | * we buffer at most 'qlen' transfers; fewer if any need more | ||
733 | * than 'buflen' bytes each. | ||
734 | */ | ||
735 | if (result == 0) { | ||
736 | struct usb_request *req; | ||
737 | unsigned i; | ||
738 | |||
739 | ep = dev->out_ep; | ||
740 | for (i = 0; i < qlen && result == 0; i++) { | ||
741 | req = alloc_ep_req(ep, buflen); | ||
742 | if (req) { | ||
743 | req->complete = loopback_complete; | ||
744 | result = usb_ep_queue(ep, req, GFP_ATOMIC); | ||
745 | if (result) | ||
746 | DBG(dev, "%s queue req --> %d\n", | ||
747 | ep->name, result); | ||
748 | } else | ||
749 | result = -ENOMEM; | ||
750 | } | ||
751 | } | ||
752 | if (result == 0) | ||
753 | DBG(dev, "qlen %d, buflen %d\n", qlen, buflen); | ||
754 | |||
755 | /* caller is responsible for cleanup on error */ | ||
756 | return result; | ||
757 | } | ||
758 | |||
759 | /*-------------------------------------------------------------------------*/ | ||
760 | |||
761 | static void zero_reset_config(struct zero_dev *dev) | ||
762 | { | ||
763 | if (dev->config == 0) | ||
764 | return; | ||
765 | |||
766 | DBG(dev, "reset config\n"); | ||
767 | |||
768 | /* just disable endpoints, forcing completion of pending i/o. | ||
769 | * all our completion handlers free their requests in this case. | ||
770 | */ | ||
771 | if (dev->in_ep) { | ||
772 | usb_ep_disable(dev->in_ep); | ||
773 | dev->in_ep = NULL; | ||
774 | } | ||
775 | if (dev->out_ep) { | ||
776 | usb_ep_disable(dev->out_ep); | ||
777 | dev->out_ep = NULL; | ||
778 | } | ||
779 | dev->config = 0; | ||
780 | del_timer(&dev->resume); | ||
781 | } | ||
782 | |||
783 | /* change our operational config. this code must agree with the code | ||
784 | * that returns config descriptors, and altsetting code. | ||
785 | * | ||
786 | * it's also responsible for power management interactions. some | ||
787 | * configurations might not work with our current power sources. | ||
788 | * | ||
789 | * note that some device controller hardware will constrain what this | ||
790 | * code can do, perhaps by disallowing more than one configuration or | ||
791 | * by limiting configuration choices (like the pxa2xx). | ||
792 | */ | ||
793 | static int zero_set_config(struct zero_dev *dev, unsigned number) | ||
794 | { | ||
795 | int result = 0; | ||
796 | struct usb_gadget *gadget = dev->gadget; | ||
797 | |||
798 | if (number == dev->config) | ||
799 | return 0; | ||
800 | |||
801 | if (gadget_is_sa1100(gadget) && dev->config) { | ||
802 | /* tx fifo is full, but we can't clear it...*/ | ||
803 | ERROR(dev, "can't change configurations\n"); | ||
804 | return -ESPIPE; | ||
805 | } | ||
806 | zero_reset_config(dev); | ||
807 | |||
808 | switch (number) { | ||
809 | case CONFIG_SOURCE_SINK: | ||
810 | result = set_source_sink_config(dev); | ||
811 | break; | ||
812 | case CONFIG_LOOPBACK: | ||
813 | result = set_loopback_config(dev); | ||
814 | break; | ||
815 | default: | ||
816 | result = -EINVAL; | ||
817 | /* FALL THROUGH */ | ||
818 | case 0: | ||
819 | return result; | ||
820 | } | ||
821 | |||
822 | if (!result && (!dev->in_ep || !dev->out_ep)) | ||
823 | result = -ENODEV; | ||
824 | if (result) | ||
825 | zero_reset_config(dev); | ||
826 | else { | ||
827 | char *speed; | ||
828 | |||
829 | switch (gadget->speed) { | ||
830 | case USB_SPEED_LOW: speed = "low"; break; | ||
831 | case USB_SPEED_FULL: speed = "full"; break; | ||
832 | case USB_SPEED_HIGH: speed = "high"; break; | ||
833 | default: speed = "?"; break; | ||
834 | } | ||
835 | |||
836 | dev->config = number; | ||
837 | INFO(dev, "%s speed config #%d: %s\n", speed, number, | ||
838 | (number == CONFIG_SOURCE_SINK) | ||
839 | ? source_sink : loopback); | ||
840 | } | ||
841 | return result; | ||
842 | } | ||
843 | |||
844 | /*-------------------------------------------------------------------------*/ | ||
845 | |||
846 | static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req) | ||
847 | { | ||
848 | if (req->status || req->actual != req->length) | ||
849 | DBG((struct zero_dev *) ep->driver_data, | ||
850 | "setup complete --> %d, %d/%d\n", | ||
851 | req->status, req->actual, req->length); | ||
852 | } | ||
853 | |||
854 | /* | ||
855 | * The setup() callback implements all the ep0 functionality that's | ||
856 | * not handled lower down, in hardware or the hardware driver (like | ||
857 | * device and endpoint feature flags, and their status). It's all | ||
858 | * housekeeping for the gadget function we're implementing. Most of | ||
859 | * the work is in config-specific setup. | ||
860 | */ | ||
861 | static int | ||
862 | zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | ||
863 | { | ||
864 | struct zero_dev *dev = get_gadget_data(gadget); | ||
865 | struct usb_request *req = dev->req; | ||
866 | int value = -EOPNOTSUPP; | ||
867 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
868 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
869 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
870 | |||
871 | /* usually this stores reply data in the pre-allocated ep0 buffer, | ||
872 | * but config change events will reconfigure hardware. | ||
873 | */ | ||
874 | req->zero = 0; | ||
875 | switch (ctrl->bRequest) { | ||
876 | |||
877 | case USB_REQ_GET_DESCRIPTOR: | ||
878 | if (ctrl->bRequestType != USB_DIR_IN) | ||
879 | goto unknown; | ||
880 | switch (w_value >> 8) { | ||
881 | |||
882 | case USB_DT_DEVICE: | ||
883 | value = min(w_length, (u16) sizeof device_desc); | ||
884 | memcpy(req->buf, &device_desc, value); | ||
885 | break; | ||
886 | case USB_DT_DEVICE_QUALIFIER: | ||
887 | if (!gadget_is_dualspeed(gadget)) | ||
888 | break; | ||
889 | value = min(w_length, (u16) sizeof dev_qualifier); | ||
890 | memcpy(req->buf, &dev_qualifier, value); | ||
891 | break; | ||
892 | |||
893 | case USB_DT_OTHER_SPEED_CONFIG: | ||
894 | if (!gadget_is_dualspeed(gadget)) | ||
895 | break; | ||
896 | // FALLTHROUGH | ||
897 | case USB_DT_CONFIG: | ||
898 | value = config_buf(gadget, req->buf, | ||
899 | w_value >> 8, | ||
900 | w_value & 0xff); | ||
901 | if (value >= 0) | ||
902 | value = min(w_length, (u16) value); | ||
903 | break; | ||
904 | |||
905 | case USB_DT_STRING: | ||
906 | /* wIndex == language code. | ||
907 | * this driver only handles one language, you can | ||
908 | * add string tables for other languages, using | ||
909 | * any UTF-8 characters | ||
910 | */ | ||
911 | value = usb_gadget_get_string(&stringtab, | ||
912 | w_value & 0xff, req->buf); | ||
913 | if (value >= 0) | ||
914 | value = min(w_length, (u16) value); | ||
915 | break; | ||
916 | } | ||
917 | break; | ||
918 | |||
919 | /* currently two configs, two speeds */ | ||
920 | case USB_REQ_SET_CONFIGURATION: | ||
921 | if (ctrl->bRequestType != 0) | ||
922 | goto unknown; | ||
923 | if (gadget->a_hnp_support) | ||
924 | DBG(dev, "HNP available\n"); | ||
925 | else if (gadget->a_alt_hnp_support) | ||
926 | DBG(dev, "HNP needs a different root port\n"); | ||
927 | else | ||
928 | VDBG(dev, "HNP inactive\n"); | ||
929 | spin_lock(&dev->lock); | ||
930 | value = zero_set_config(dev, w_value); | ||
931 | spin_unlock(&dev->lock); | ||
932 | break; | ||
933 | case USB_REQ_GET_CONFIGURATION: | ||
934 | if (ctrl->bRequestType != USB_DIR_IN) | ||
935 | goto unknown; | ||
936 | *(u8 *)req->buf = dev->config; | ||
937 | value = min(w_length, (u16) 1); | ||
938 | break; | ||
939 | |||
940 | /* until we add altsetting support, or other interfaces, | ||
941 | * only 0/0 are possible. pxa2xx only supports 0/0 (poorly) | ||
942 | * and already killed pending endpoint I/O. | ||
943 | */ | ||
944 | case USB_REQ_SET_INTERFACE: | ||
945 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) | ||
946 | goto unknown; | ||
947 | spin_lock(&dev->lock); | ||
948 | if (dev->config && w_index == 0 && w_value == 0) { | ||
949 | u8 config = dev->config; | ||
950 | |||
951 | /* resets interface configuration, forgets about | ||
952 | * previous transaction state (queued bufs, etc) | ||
953 | * and re-inits endpoint state (toggle etc) | ||
954 | * no response queued, just zero status == success. | ||
955 | * if we had more than one interface we couldn't | ||
956 | * use this "reset the config" shortcut. | ||
957 | */ | ||
958 | zero_reset_config(dev); | ||
959 | zero_set_config(dev, config); | ||
960 | value = 0; | ||
961 | } | ||
962 | spin_unlock(&dev->lock); | ||
963 | break; | ||
964 | case USB_REQ_GET_INTERFACE: | ||
965 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) | ||
966 | goto unknown; | ||
967 | if (!dev->config) | ||
968 | break; | ||
969 | if (w_index != 0) { | ||
970 | value = -EDOM; | ||
971 | break; | ||
972 | } | ||
973 | *(u8 *)req->buf = 0; | ||
974 | value = min(w_length, (u16) 1); | ||
975 | break; | ||
976 | |||
977 | /* | ||
978 | * These are the same vendor-specific requests supported by | ||
979 | * Intel's USB 2.0 compliance test devices. We exceed that | ||
980 | * device spec by allowing multiple-packet requests. | ||
981 | */ | ||
982 | case 0x5b: /* control WRITE test -- fill the buffer */ | ||
983 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) | ||
984 | goto unknown; | ||
985 | if (w_value || w_index) | ||
986 | break; | ||
987 | /* just read that many bytes into the buffer */ | ||
988 | if (w_length > USB_BUFSIZ) | ||
989 | break; | ||
990 | value = w_length; | ||
991 | break; | ||
992 | case 0x5c: /* control READ test -- return the buffer */ | ||
993 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) | ||
994 | goto unknown; | ||
995 | if (w_value || w_index) | ||
996 | break; | ||
997 | /* expect those bytes are still in the buffer; send back */ | ||
998 | if (w_length > USB_BUFSIZ | ||
999 | || w_length != req->length) | ||
1000 | break; | ||
1001 | value = w_length; | ||
1002 | break; | ||
1003 | |||
1004 | default: | ||
1005 | unknown: | ||
1006 | VDBG(dev, | ||
1007 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | ||
1008 | ctrl->bRequestType, ctrl->bRequest, | ||
1009 | w_value, w_index, w_length); | ||
1010 | } | ||
1011 | |||
1012 | /* respond with data transfer before status phase? */ | ||
1013 | if (value >= 0) { | ||
1014 | req->length = value; | ||
1015 | req->zero = value < w_length; | ||
1016 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | ||
1017 | if (value < 0) { | ||
1018 | DBG(dev, "ep_queue --> %d\n", value); | ||
1019 | req->status = 0; | ||
1020 | zero_setup_complete(gadget->ep0, req); | ||
1021 | } | ||
1022 | } | ||
1023 | |||
1024 | /* device either stalls (value < 0) or reports success */ | ||
1025 | return value; | ||
1026 | } | ||
1027 | |||
1028 | static void zero_disconnect(struct usb_gadget *gadget) | ||
1029 | { | ||
1030 | struct zero_dev *dev = get_gadget_data(gadget); | ||
1031 | unsigned long flags; | ||
1032 | |||
1033 | spin_lock_irqsave(&dev->lock, flags); | ||
1034 | zero_reset_config(dev); | ||
1035 | |||
1036 | /* a more significant application might have some non-usb | ||
1037 | * activities to quiesce here, saving resources like power | ||
1038 | * or pushing the notification up a network stack. | ||
1039 | */ | ||
1040 | spin_unlock_irqrestore(&dev->lock, flags); | ||
1041 | |||
1042 | /* next we may get setup() calls to enumerate new connections; | ||
1043 | * or an unbind() during shutdown (including removing module). | ||
1044 | */ | ||
1045 | } | ||
1046 | |||
1047 | static void zero_autoresume(unsigned long _dev) | ||
1048 | { | ||
1049 | struct zero_dev *dev = (struct zero_dev *) _dev; | ||
1050 | int status; | ||
1051 | |||
1052 | /* normally the host would be woken up for something | ||
1053 | * more significant than just a timer firing... | ||
1054 | */ | ||
1055 | if (dev->gadget->speed != USB_SPEED_UNKNOWN) { | ||
1056 | status = usb_gadget_wakeup(dev->gadget); | ||
1057 | DBG(dev, "wakeup --> %d\n", status); | ||
1058 | } | ||
1059 | } | 194 | } |
1060 | 195 | ||
1061 | /*-------------------------------------------------------------------------*/ | 196 | /*-------------------------------------------------------------------------*/ |
1062 | 197 | ||
1063 | static void zero_unbind(struct usb_gadget *gadget) | 198 | static int __init zero_bind(struct usb_composite_dev *cdev) |
1064 | { | 199 | { |
1065 | struct zero_dev *dev = get_gadget_data(gadget); | ||
1066 | |||
1067 | DBG(dev, "unbind\n"); | ||
1068 | |||
1069 | /* we've already been disconnected ... no i/o is active */ | ||
1070 | if (dev->req) { | ||
1071 | dev->req->length = USB_BUFSIZ; | ||
1072 | free_ep_req(gadget->ep0, dev->req); | ||
1073 | } | ||
1074 | del_timer_sync(&dev->resume); | ||
1075 | kfree(dev); | ||
1076 | set_gadget_data(gadget, NULL); | ||
1077 | } | ||
1078 | |||
1079 | static int __init zero_bind(struct usb_gadget *gadget) | ||
1080 | { | ||
1081 | struct zero_dev *dev; | ||
1082 | struct usb_ep *ep; | ||
1083 | int gcnum; | 200 | int gcnum; |
201 | struct usb_gadget *gadget = cdev->gadget; | ||
202 | int id; | ||
1084 | 203 | ||
1085 | /* FIXME this can't yet work right with SH ... it has only | 204 | /* Allocate string descriptor numbers ... note that string |
1086 | * one configuration, numbered one. | 205 | * contents can be overridden by the composite_dev glue. |
1087 | */ | 206 | */ |
1088 | if (gadget_is_sh(gadget)) | 207 | id = usb_string_id(cdev); |
1089 | return -ENODEV; | 208 | if (id < 0) |
1090 | 209 | return id; | |
1091 | /* Bulk-only drivers like this one SHOULD be able to | 210 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
1092 | * autoconfigure on any sane usb controller driver, | 211 | device_desc.iManufacturer = id; |
1093 | * but there may also be important quirks to address. | 212 | |
213 | id = usb_string_id(cdev); | ||
214 | if (id < 0) | ||
215 | return id; | ||
216 | strings_dev[STRING_PRODUCT_IDX].id = id; | ||
217 | device_desc.iProduct = id; | ||
218 | |||
219 | id = usb_string_id(cdev); | ||
220 | if (id < 0) | ||
221 | return id; | ||
222 | strings_dev[STRING_SERIAL_IDX].id = id; | ||
223 | device_desc.iSerialNumber = id; | ||
224 | |||
225 | /* Register primary, then secondary configuration. Note that | ||
226 | * SH3 only allows one config... | ||
1094 | */ | 227 | */ |
1095 | usb_ep_autoconfig_reset(gadget); | 228 | if (loopdefault) { |
1096 | ep = usb_ep_autoconfig(gadget, &fs_source_desc); | 229 | loopback_add(cdev); |
1097 | if (!ep) { | 230 | if (!gadget_is_sh(gadget)) |
1098 | autoconf_fail: | 231 | sourcesink_add(cdev); |
1099 | pr_err("%s: can't autoconfigure on %s\n", | 232 | } else { |
1100 | shortname, gadget->name); | 233 | sourcesink_add(cdev); |
1101 | return -ENODEV; | 234 | if (!gadget_is_sh(gadget)) |
235 | loopback_add(cdev); | ||
1102 | } | 236 | } |
1103 | EP_IN_NAME = ep->name; | ||
1104 | ep->driver_data = ep; /* claim */ | ||
1105 | |||
1106 | ep = usb_ep_autoconfig(gadget, &fs_sink_desc); | ||
1107 | if (!ep) | ||
1108 | goto autoconf_fail; | ||
1109 | EP_OUT_NAME = ep->name; | ||
1110 | ep->driver_data = ep; /* claim */ | ||
1111 | 237 | ||
1112 | gcnum = usb_gadget_controller_number(gadget); | 238 | gcnum = usb_gadget_controller_number(gadget); |
1113 | if (gcnum >= 0) | 239 | if (gcnum >= 0) |
@@ -1115,144 +241,44 @@ autoconf_fail: | |||
1115 | else { | 241 | else { |
1116 | /* gadget zero is so simple (for now, no altsettings) that | 242 | /* gadget zero is so simple (for now, no altsettings) that |
1117 | * it SHOULD NOT have problems with bulk-capable hardware. | 243 | * it SHOULD NOT have problems with bulk-capable hardware. |
1118 | * so warn about unrcognized controllers, don't panic. | 244 | * so just warn about unrcognized controllers -- don't panic. |
1119 | * | 245 | * |
1120 | * things like configuration and altsetting numbering | 246 | * things like configuration and altsetting numbering |
1121 | * can need hardware-specific attention though. | 247 | * can need hardware-specific attention though. |
1122 | */ | 248 | */ |
1123 | pr_warning("%s: controller '%s' not recognized\n", | 249 | pr_warning("%s: controller '%s' not recognized\n", |
1124 | shortname, gadget->name); | 250 | longname, gadget->name); |
1125 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); | 251 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
1126 | } | 252 | } |
1127 | 253 | ||
1128 | 254 | ||
1129 | /* ok, we made sense of the hardware ... */ | 255 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); |
1130 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | ||
1131 | if (!dev) | ||
1132 | return -ENOMEM; | ||
1133 | spin_lock_init(&dev->lock); | ||
1134 | dev->gadget = gadget; | ||
1135 | set_gadget_data(gadget, dev); | ||
1136 | |||
1137 | init_timer(&dev->resume); | ||
1138 | dev->resume.function = zero_autoresume; | ||
1139 | dev->resume.data = (unsigned long) dev; | ||
1140 | |||
1141 | /* preallocate control response and buffer */ | ||
1142 | dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); | ||
1143 | if (!dev->req) | ||
1144 | goto enomem; | ||
1145 | dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); | ||
1146 | if (!dev->req->buf) | ||
1147 | goto enomem; | ||
1148 | |||
1149 | dev->req->complete = zero_setup_complete; | ||
1150 | |||
1151 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | ||
1152 | |||
1153 | if (gadget_is_dualspeed(gadget)) { | ||
1154 | /* assume ep0 uses the same value for both speeds ... */ | ||
1155 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; | ||
1156 | |||
1157 | /* and that all endpoints are dual-speed */ | ||
1158 | hs_source_desc.bEndpointAddress = | ||
1159 | fs_source_desc.bEndpointAddress; | ||
1160 | hs_sink_desc.bEndpointAddress = | ||
1161 | fs_sink_desc.bEndpointAddress; | ||
1162 | } | ||
1163 | |||
1164 | if (gadget_is_otg(gadget)) { | ||
1165 | otg_descriptor.bmAttributes |= USB_OTG_HNP, | ||
1166 | source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1167 | loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1168 | } | ||
1169 | |||
1170 | usb_gadget_set_selfpowered(gadget); | ||
1171 | |||
1172 | if (autoresume) { | ||
1173 | source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1174 | loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
1175 | } | ||
1176 | |||
1177 | gadget->ep0->driver_data = dev; | ||
1178 | |||
1179 | INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname); | ||
1180 | INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, | ||
1181 | EP_OUT_NAME, EP_IN_NAME); | ||
1182 | 256 | ||
1183 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | 257 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
1184 | init_utsname()->sysname, init_utsname()->release, | 258 | init_utsname()->sysname, init_utsname()->release, |
1185 | gadget->name); | 259 | gadget->name); |
1186 | 260 | ||
1187 | return 0; | 261 | return 0; |
1188 | |||
1189 | enomem: | ||
1190 | zero_unbind(gadget); | ||
1191 | return -ENOMEM; | ||
1192 | } | 262 | } |
1193 | 263 | ||
1194 | /*-------------------------------------------------------------------------*/ | 264 | static struct usb_composite_driver zero_driver = { |
1195 | 265 | .name = "zero", | |
1196 | static void zero_suspend(struct usb_gadget *gadget) | 266 | .dev = &device_desc, |
1197 | { | 267 | .strings = dev_strings, |
1198 | struct zero_dev *dev = get_gadget_data(gadget); | ||
1199 | |||
1200 | if (gadget->speed == USB_SPEED_UNKNOWN) | ||
1201 | return; | ||
1202 | |||
1203 | if (autoresume) { | ||
1204 | mod_timer(&dev->resume, jiffies + (HZ * autoresume)); | ||
1205 | DBG(dev, "suspend, wakeup in %d seconds\n", autoresume); | ||
1206 | } else | ||
1207 | DBG(dev, "suspend\n"); | ||
1208 | } | ||
1209 | |||
1210 | static void zero_resume(struct usb_gadget *gadget) | ||
1211 | { | ||
1212 | struct zero_dev *dev = get_gadget_data(gadget); | ||
1213 | |||
1214 | DBG(dev, "resume\n"); | ||
1215 | del_timer(&dev->resume); | ||
1216 | } | ||
1217 | |||
1218 | |||
1219 | /*-------------------------------------------------------------------------*/ | ||
1220 | |||
1221 | static struct usb_gadget_driver zero_driver = { | ||
1222 | #ifdef CONFIG_USB_GADGET_DUALSPEED | ||
1223 | .speed = USB_SPEED_HIGH, | ||
1224 | #else | ||
1225 | .speed = USB_SPEED_FULL, | ||
1226 | #endif | ||
1227 | .function = (char *) longname, | ||
1228 | .bind = zero_bind, | 268 | .bind = zero_bind, |
1229 | .unbind = __exit_p(zero_unbind), | ||
1230 | |||
1231 | .setup = zero_setup, | ||
1232 | .disconnect = zero_disconnect, | ||
1233 | |||
1234 | .suspend = zero_suspend, | ||
1235 | .resume = zero_resume, | ||
1236 | |||
1237 | .driver = { | ||
1238 | .name = (char *) shortname, | ||
1239 | .owner = THIS_MODULE, | ||
1240 | }, | ||
1241 | }; | 269 | }; |
1242 | 270 | ||
1243 | MODULE_AUTHOR("David Brownell"); | 271 | MODULE_AUTHOR("David Brownell"); |
1244 | MODULE_LICENSE("GPL"); | 272 | MODULE_LICENSE("GPL"); |
1245 | 273 | ||
1246 | |||
1247 | static int __init init(void) | 274 | static int __init init(void) |
1248 | { | 275 | { |
1249 | return usb_gadget_register_driver(&zero_driver); | 276 | return usb_composite_register(&zero_driver); |
1250 | } | 277 | } |
1251 | module_init(init); | 278 | module_init(init); |
1252 | 279 | ||
1253 | static void __exit cleanup(void) | 280 | static void __exit cleanup(void) |
1254 | { | 281 | { |
1255 | usb_gadget_unregister_driver(&zero_driver); | 282 | usb_composite_unregister(&zero_driver); |
1256 | } | 283 | } |
1257 | module_exit(cleanup); | 284 | module_exit(cleanup); |
1258 | |||