]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options | |
3 | * | |
4 | * Copyright (C) 2003-2005 David Brownell | |
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | |
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 | ||
23 | // #define DEBUG 1 | |
24 | // #define VERBOSE | |
25 | ||
1da177e4 LT |
26 | #include <linux/module.h> |
27 | #include <linux/kernel.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/ioport.h> | |
1da177e4 | 30 | #include <linux/slab.h> |
1da177e4 LT |
31 | #include <linux/errno.h> |
32 | #include <linux/init.h> | |
33 | #include <linux/timer.h> | |
34 | #include <linux/list.h> | |
35 | #include <linux/interrupt.h> | |
36 | #include <linux/utsname.h> | |
37 | #include <linux/device.h> | |
38 | #include <linux/moduleparam.h> | |
39 | #include <linux/ctype.h> | |
40 | ||
41 | #include <asm/byteorder.h> | |
42 | #include <asm/io.h> | |
43 | #include <asm/irq.h> | |
44 | #include <asm/system.h> | |
45 | #include <asm/uaccess.h> | |
46 | #include <asm/unaligned.h> | |
47 | ||
5f848137 | 48 | #include <linux/usb/ch9.h> |
a8c28f23 | 49 | #include <linux/usb/cdc.h> |
1da177e4 LT |
50 | #include <linux/usb_gadget.h> |
51 | ||
52 | #include <linux/random.h> | |
53 | #include <linux/netdevice.h> | |
54 | #include <linux/etherdevice.h> | |
55 | #include <linux/ethtool.h> | |
56 | ||
57 | #include "gadget_chips.h" | |
58 | ||
59 | /*-------------------------------------------------------------------------*/ | |
60 | ||
61 | /* | |
62 | * Ethernet gadget driver -- with CDC and non-CDC options | |
63 | * Builds on hardware support for a full duplex link. | |
64 | * | |
65 | * CDC Ethernet is the standard USB solution for sending Ethernet frames | |
66 | * using USB. Real hardware tends to use the same framing protocol but look | |
67 | * different for control features. This driver strongly prefers to use | |
68 | * this USB-IF standard as its open-systems interoperability solution; | |
69 | * most host side USB stacks (except from Microsoft) support it. | |
70 | * | |
71 | * There's some hardware that can't talk CDC. We make that hardware | |
72 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but | |
11d54898 DB |
73 | * link-level setup only requires activating the configuration. Only the |
74 | * endpoint descriptors, and product/vendor IDs, are relevant; no control | |
75 | * operations are available. Linux supports it, but other host operating | |
76 | * systems may not. (This is a subset of CDC Ethernet.) | |
77 | * | |
78 | * It turns out that if you add a few descriptors to that "CDC Subset", | |
79 | * (Windows) host side drivers from MCCI can treat it as one submode of | |
80 | * a proprietary scheme called "SAFE" ... without needing to know about | |
81 | * specific product/vendor IDs. So we do that, making it easier to use | |
82 | * those MS-Windows drivers. Those added descriptors make it resemble a | |
83 | * CDC MDLM device, but they don't change device behavior at all. (See | |
84 | * MCCI Engineering report 950198 "SAFE Networking Functions".) | |
1da177e4 LT |
85 | * |
86 | * A third option is also in use. Rather than CDC Ethernet, or something | |
87 | * simpler, Microsoft pushes their own approach: RNDIS. The published | |
88 | * RNDIS specs are ambiguous and appear to be incomplete, and are also | |
89 | * needlessly complex. | |
90 | */ | |
91 | ||
92 | #define DRIVER_DESC "Ethernet Gadget" | |
907cba35 | 93 | #define DRIVER_VERSION "May Day 2005" |
1da177e4 LT |
94 | |
95 | static const char shortname [] = "ether"; | |
96 | static const char driver_desc [] = DRIVER_DESC; | |
97 | ||
98 | #define RX_EXTRA 20 /* guard against rx overflows */ | |
99 | ||
1da177e4 | 100 | #include "rndis.h" |
45e45ab4 DB |
101 | |
102 | #ifndef CONFIG_USB_ETH_RNDIS | |
103 | #define rndis_uninit(x) do{}while(0) | |
104 | #define rndis_deregister(c) do{}while(0) | |
105 | #define rndis_exit() do{}while(0) | |
1da177e4 LT |
106 | #endif |
107 | ||
108 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ | |
109 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ | |
7e27f18c DB |
110 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
111 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ | |
112 | |USB_CDC_PACKET_TYPE_DIRECTED) | |
1da177e4 LT |
113 | |
114 | ||
115 | /*-------------------------------------------------------------------------*/ | |
116 | ||
117 | struct eth_dev { | |
118 | spinlock_t lock; | |
119 | struct usb_gadget *gadget; | |
120 | struct usb_request *req; /* for control responses */ | |
121 | struct usb_request *stat_req; /* for cdc & rndis status */ | |
122 | ||
123 | u8 config; | |
124 | struct usb_ep *in_ep, *out_ep, *status_ep; | |
125 | const struct usb_endpoint_descriptor | |
126 | *in, *out, *status; | |
789851cf DB |
127 | |
128 | spinlock_t req_lock; | |
1da177e4 LT |
129 | struct list_head tx_reqs, rx_reqs; |
130 | ||
131 | struct net_device *net; | |
132 | struct net_device_stats stats; | |
133 | atomic_t tx_qlen; | |
134 | ||
135 | struct work_struct work; | |
136 | unsigned zlp:1; | |
137 | unsigned cdc:1; | |
138 | unsigned rndis:1; | |
139 | unsigned suspended:1; | |
140 | u16 cdc_filter; | |
141 | unsigned long todo; | |
142 | #define WORK_RX_MEMORY 0 | |
143 | int rndis_config; | |
144 | u8 host_mac [ETH_ALEN]; | |
145 | }; | |
146 | ||
147 | /* This version autoconfigures as much as possible at run-time. | |
148 | * | |
149 | * It also ASSUMES a self-powered device, without remote wakeup, | |
150 | * although remote wakeup support would make sense. | |
151 | */ | |
1da177e4 LT |
152 | |
153 | /*-------------------------------------------------------------------------*/ | |
154 | ||
155 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
156 | * Instead: allocate your own, using normal USB-IF procedures. | |
157 | */ | |
158 | ||
159 | /* Thanks to NetChip Technologies for donating this product ID. | |
160 | * It's for devices with only CDC Ethernet configurations. | |
161 | */ | |
162 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | |
163 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ | |
164 | ||
165 | /* For hardware that can't talk CDC, we use the same vendor ID that | |
166 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and | |
167 | * with pxa250. We're protocol-compatible, if the host-side drivers | |
168 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so | |
169 | * drivers that need to hard-wire endpoint numbers have a hook. | |
170 | * | |
171 | * The protocol is a minimal subset of CDC Ether, which works on any bulk | |
172 | * hardware that's not deeply broken ... even on hardware that can't talk | |
173 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that | |
174 | * doesn't handle control-OUT). | |
175 | */ | |
176 | #define SIMPLE_VENDOR_NUM 0x049f | |
177 | #define SIMPLE_PRODUCT_NUM 0x505a | |
178 | ||
179 | /* For hardware that can talk RNDIS and either of the above protocols, | |
180 | * use this ID ... the windows INF files will know it. Unless it's | |
181 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose | |
182 | * the non-RNDIS configuration. | |
183 | */ | |
184 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ | |
185 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ | |
186 | ||
187 | ||
188 | /* Some systems will want different product identifers published in the | |
189 | * device descriptor, either numbers or strings or both. These string | |
190 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
191 | */ | |
192 | ||
1afc64a3 | 193 | static ushort idVendor; |
1da177e4 LT |
194 | module_param(idVendor, ushort, S_IRUGO); |
195 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | |
196 | ||
1afc64a3 | 197 | static ushort idProduct; |
1da177e4 LT |
198 | module_param(idProduct, ushort, S_IRUGO); |
199 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | |
200 | ||
1afc64a3 | 201 | static ushort bcdDevice; |
1da177e4 LT |
202 | module_param(bcdDevice, ushort, S_IRUGO); |
203 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | |
204 | ||
1afc64a3 | 205 | static char *iManufacturer; |
1da177e4 LT |
206 | module_param(iManufacturer, charp, S_IRUGO); |
207 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | |
208 | ||
1afc64a3 | 209 | static char *iProduct; |
1da177e4 LT |
210 | module_param(iProduct, charp, S_IRUGO); |
211 | MODULE_PARM_DESC(iProduct, "USB Product string"); | |
212 | ||
1afc64a3 AV |
213 | static char *iSerialNumber; |
214 | module_param(iSerialNumber, charp, S_IRUGO); | |
215 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber"); | |
216 | ||
1da177e4 | 217 | /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ |
1afc64a3 | 218 | static char *dev_addr; |
1da177e4 LT |
219 | module_param(dev_addr, charp, S_IRUGO); |
220 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); | |
221 | ||
222 | /* this address is invisible to ifconfig */ | |
1afc64a3 | 223 | static char *host_addr; |
1da177e4 LT |
224 | module_param(host_addr, charp, S_IRUGO); |
225 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); | |
226 | ||
227 | ||
228 | /*-------------------------------------------------------------------------*/ | |
229 | ||
230 | /* Include CDC support if we could run on CDC-capable hardware. */ | |
231 | ||
232 | #ifdef CONFIG_USB_GADGET_NET2280 | |
233 | #define DEV_CONFIG_CDC | |
234 | #endif | |
235 | ||
236 | #ifdef CONFIG_USB_GADGET_DUMMY_HCD | |
237 | #define DEV_CONFIG_CDC | |
238 | #endif | |
239 | ||
240 | #ifdef CONFIG_USB_GADGET_GOKU | |
241 | #define DEV_CONFIG_CDC | |
242 | #endif | |
243 | ||
244 | #ifdef CONFIG_USB_GADGET_LH7A40X | |
245 | #define DEV_CONFIG_CDC | |
246 | #endif | |
247 | ||
248 | #ifdef CONFIG_USB_GADGET_MQ11XX | |
249 | #define DEV_CONFIG_CDC | |
250 | #endif | |
251 | ||
252 | #ifdef CONFIG_USB_GADGET_OMAP | |
253 | #define DEV_CONFIG_CDC | |
254 | #endif | |
255 | ||
256 | #ifdef CONFIG_USB_GADGET_N9604 | |
257 | #define DEV_CONFIG_CDC | |
258 | #endif | |
259 | ||
260 | #ifdef CONFIG_USB_GADGET_PXA27X | |
261 | #define DEV_CONFIG_CDC | |
262 | #endif | |
263 | ||
11d54898 DB |
264 | #ifdef CONFIG_USB_GADGET_S3C2410 |
265 | #define DEV_CONFIG_CDC | |
266 | #endif | |
267 | ||
1da177e4 LT |
268 | #ifdef CONFIG_USB_GADGET_AT91 |
269 | #define DEV_CONFIG_CDC | |
270 | #endif | |
271 | ||
1c05ad44 DB |
272 | #ifdef CONFIG_USB_GADGET_MUSBHSFC |
273 | #define DEV_CONFIG_CDC | |
274 | #endif | |
275 | ||
bfb2c965 | 276 | #ifdef CONFIG_USB_GADGET_MUSB_HDRC |
1c05ad44 DB |
277 | #define DEV_CONFIG_CDC |
278 | #endif | |
279 | ||