Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options | |
3 | * | |
0391c828 | 4 | * Copyright (C) 2003-2005,2008 David Brownell |
1da177e4 | 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
0391c828 | 6 | * Copyright (C) 2008 Nokia Corporation |
1da177e4 LT |
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 | ||
0cf4f2de | 23 | /* #define VERBOSE_DEBUG */ |
1da177e4 | 24 | |
1da177e4 | 25 | #include <linux/kernel.h> |
1da177e4 | 26 | #include <linux/utsname.h> |
1da177e4 | 27 | |
396cda90 MN |
28 | |
29 | #if defined USB_ETH_RNDIS | |
30 | # undef USB_ETH_RNDIS | |
31 | #endif | |
32 | #ifdef CONFIG_USB_ETH_RNDIS | |
33 | # define USB_ETH_RNDIS y | |
34 | #endif | |
35 | ||
0391c828 | 36 | #include "u_ether.h" |
1da177e4 | 37 | |
1da177e4 LT |
38 | |
39 | /* | |
40 | * Ethernet gadget driver -- with CDC and non-CDC options | |
41 | * Builds on hardware support for a full duplex link. | |
42 | * | |
43 | * CDC Ethernet is the standard USB solution for sending Ethernet frames | |
44 | * using USB. Real hardware tends to use the same framing protocol but look | |
45 | * different for control features. This driver strongly prefers to use | |
46 | * this USB-IF standard as its open-systems interoperability solution; | |
47 | * most host side USB stacks (except from Microsoft) support it. | |
48 | * | |
0391c828 DB |
49 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support |
50 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new | |
51 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. | |
52 | * | |
53 | * There's some hardware that can't talk CDC ECM. We make that hardware | |
1da177e4 | 54 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
11d54898 DB |
55 | * link-level setup only requires activating the configuration. Only the |
56 | * endpoint descriptors, and product/vendor IDs, are relevant; no control | |
57 | * operations are available. Linux supports it, but other host operating | |
58 | * systems may not. (This is a subset of CDC Ethernet.) | |
59 | * | |
60 | * It turns out that if you add a few descriptors to that "CDC Subset", | |
61 | * (Windows) host side drivers from MCCI can treat it as one submode of | |
62 | * a proprietary scheme called "SAFE" ... without needing to know about | |
63 | * specific product/vendor IDs. So we do that, making it easier to use | |
64 | * those MS-Windows drivers. Those added descriptors make it resemble a | |
65 | * CDC MDLM device, but they don't change device behavior at all. (See | |
66 | * MCCI Engineering report 950198 "SAFE Networking Functions".) | |
1da177e4 LT |
67 | * |
68 | * A third option is also in use. Rather than CDC Ethernet, or something | |
69 | * simpler, Microsoft pushes their own approach: RNDIS. The published | |
70 | * RNDIS specs are ambiguous and appear to be incomplete, and are also | |
0391c828 | 71 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
1da177e4 LT |
72 | */ |
73 | ||
74 | #define DRIVER_DESC "Ethernet Gadget" | |
0391c828 | 75 | #define DRIVER_VERSION "Memorial Day 2008" |
1da177e4 | 76 | |
396cda90 | 77 | #ifdef USB_ETH_RNDIS |
0391c828 DB |
78 | #define PREFIX "RNDIS/" |
79 | #else | |
80 | #define PREFIX "" | |
1da177e4 LT |
81 | #endif |
82 | ||
0391c828 DB |
83 | /* |
84 | * This driver aims for interoperability by using CDC ECM unless | |
85 | * | |
86 | * can_support_ecm() | |
1da177e4 | 87 | * |
0391c828 DB |
88 | * returns false, in which case it supports the CDC Subset. By default, |
89 | * that returns true; most hardware has no problems with CDC ECM, that's | |
90 | * a good default. Previous versions of this driver had no default; this | |
91 | * version changes that, removing overhead for new controller support. | |
92 | * | |
93 | * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE! | |
1da177e4 | 94 | */ |
1da177e4 | 95 | |
0391c828 DB |
96 | static inline bool has_rndis(void) |
97 | { | |
396cda90 | 98 | #ifdef USB_ETH_RNDIS |
0391c828 DB |
99 | return true; |
100 | #else | |
101 | return false; | |
102 | #endif | |
103 | } | |
104 | ||
1da177e4 LT |
105 | /*-------------------------------------------------------------------------*/ |
106 | ||
33376c1c DB |
107 | /* |
108 | * Kbuild is not very cooperative with respect to linking separately | |
109 | * compiled library objects into one module. So for now we won't use | |
110 | * separate compilation ... ensuring init/exit sections work to shrink | |
111 | * the runtime footprint, and giving us at least some parts of what | |
112 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
113 | */ | |
114 | #include "composite.c" | |
115 | #include "usbstring.c" | |
116 | #include "config.c" | |
117 | #include "epautoconf.c" | |
118 | ||
119 | #include "f_ecm.c" | |
120 | #include "f_subset.c" | |
396cda90 | 121 | #ifdef USB_ETH_RNDIS |
33376c1c DB |
122 | #include "f_rndis.c" |
123 | #include "rndis.c" | |
124 | #endif | |
9b39e9dd | 125 | #include "f_eem.c" |
33376c1c DB |
126 | #include "u_ether.c" |
127 | ||
128 | /*-------------------------------------------------------------------------*/ | |
129 | ||
1da177e4 LT |
130 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
131 | * Instead: allocate your own, using normal USB-IF procedures. | |
132 | */ | |
133 | ||
134 | /* Thanks to NetChip Technologies for donating this product ID. | |
135 | * It's for devices with only CDC Ethernet configurations. | |
136 | */ | |
0391c828 DB |
137 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
138 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ | |
1da177e4 LT |
139 | |
140 | /* For hardware that can't talk CDC, we use the same vendor ID that | |
141 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and | |
142 | * with pxa250. We're protocol-compatible, if the host-side drivers | |
143 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so | |
144 | * drivers that need to hard-wire endpoint numbers have a hook. | |
145 | * | |
146 | * The protocol is a minimal subset of CDC Ether, which works on any bulk | |
147 | * hardware that's not deeply broken ... even on hardware that can't talk | |
148 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that | |
149 | * doesn't handle control-OUT). | |
150 | */ | |
151 | #define SIMPLE_VENDOR_NUM 0x049f | |
152 | #define SIMPLE_PRODUCT_NUM 0x505a | |
153 | ||
154 | /* For hardware that can talk RNDIS and either of the above protocols, | |
155 | * use this ID ... the windows INF files will know it. Unless it's | |
156 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose | |
157 | * the non-RNDIS configuration. | |
158 | */ | |
159 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ | |
160 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ | |
161 | ||
9b39e9dd | 162 | /* For EEM gadgets */ |
4238ef54 BN |
163 | #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
164 | #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */ | |
9b39e9dd | 165 | |
1da177e4 LT |
166 | /*-------------------------------------------------------------------------*/ |
167 | ||
0391c828 | 168 | static struct usb_device_descriptor device_desc = { |
1da177e4 LT |
169 | .bLength = sizeof device_desc, |
170 | .bDescriptorType = USB_DT_DEVICE, | |
171 | ||
551509d2 | 172 | .bcdUSB = cpu_to_le16 (0x0200), |
1da177e4 LT |
173 | |
174 | .bDeviceClass = USB_CLASS_COMM, | |
175 | .bDeviceSubClass = 0, | |
176 | .bDeviceProtocol = 0, | |
0391c828 | 177 | /* .bMaxPacketSize0 = f(hardware) */ |
1da177e4 | 178 | |
0391c828 DB |
179 | /* Vendor and product id defaults change according to what configs |
180 | * we support. (As does bNumConfigurations.) These values can | |
181 | * also be overridden by module parameters. | |
182 | */ | |
551509d2 HH |
183 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
184 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), | |
0391c828 DB |
185 | /* .bcdDevice = f(hardware) */ |
186 | /* .iManufacturer = DYNAMIC */ | |
187 | /* .iProduct = DYNAMIC */ | |
188 | /* NO SERIAL NUMBER */ | |
1da177e4 LT |
189 | .bNumConfigurations = 1, |
190 | }; | |
191 | ||
0391c828 | 192 | static struct usb_otg_descriptor otg_descriptor = { |
1da177e4 LT |
193 | .bLength = sizeof otg_descriptor, |
194 | .bDescriptorType = USB_DT_OTG, | |
195 | ||
0391c828 DB |
196 | /* REVISIT SRP-only hardware is possible, although |
197 | * it would not be called "OTG" ... | |
198 | */ | |
199 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
1da177e4 | 200 | }; |
7e27f18c | 201 | |
0391c828 DB |
202 | static const struct usb_descriptor_header *otg_desc[] = { |
203 | (struct usb_descriptor_header *) &otg_descriptor, | |
204 | NULL, | |
1da177e4 | 205 | }; |
1da177e4 | 206 | |
1da177e4 | 207 | |
0391c828 | 208 | /* string IDs are assigned dynamically */ |
1da177e4 | 209 | |
0391c828 DB |
210 | #define STRING_MANUFACTURER_IDX 0 |
211 | #define STRING_PRODUCT_IDX 1 | |
11d54898 | 212 | |
0391c828 | 213 | static char manufacturer[50]; |
1da177e4 | 214 | |
0391c828 DB |
215 | static struct usb_string strings_dev[] = { |
216 | [STRING_MANUFACTURER_IDX].s = manufacturer, | |
217 | [STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC, | |
218 | { } /* end of list */ | |
1da177e4 LT |
219 | }; |
220 | ||
0391c828 DB |
221 | static struct usb_gadget_strings stringtab_dev = { |
222 | .language = 0x0409, /* en-us */ | |
223 | .strings = strings_dev, | |
1da177e4 LT |
224 | }; |
225 | ||
0391c828 DB |
226 | static struct usb_gadget_strings *dev_strings[] = { |
227 | &stringtab_dev, | |
228 | NULL, | |
1da177e4 LT |
229 | }; |
230 | ||
0391c828 | 231 | static u8 hostaddr[ETH_ALEN]; |
11d54898 | 232 | |
0391c828 | 233 | /*-------------------------------------------------------------------------*/ |
11d54898 | 234 | |
0391c828 DB |
235 | /* |
236 | * We may not have an RNDIS configuration, but if we do it needs to be | |
237 | * the first one present. That's to make Microsoft's drivers happy, | |
238 | * and to follow DOCSIS 1.0 (cable modem standard). | |
11d54898 | 239 | */ |
e12995ec | 240 | static int __init rndis_do_config(struct usb_configuration *c) |
0391c828 DB |
241 | { |
242 | /* FIXME alloc iConfiguration string, set it in c->strings */ | |
11d54898 | 243 | |
0391c828 DB |
244 | if (gadget_is_otg(c->cdev->gadget)) { |
245 | c->descriptors = otg_desc; | |
246 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
247 | } | |
1da177e4 | 248 | |
0391c828 DB |
249 | return rndis_bind_config(c, hostaddr); |
250 | } | |
1da177e4 | 251 | |
0391c828 DB |
252 | static struct usb_configuration rndis_config_driver = { |
253 | .label = "RNDIS", | |
0391c828 DB |
254 | .bConfigurationValue = 2, |
255 | /* .iConfiguration = DYNAMIC */ | |
256 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
1da177e4 LT |
257 | }; |
258 | ||
0391c828 | 259 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 260 | |
c9188ad2 | 261 | #ifdef CONFIG_USB_ETH_EEM |
9b39e9dd BN |
262 | static int use_eem = 1; |
263 | #else | |
264 | static int use_eem; | |
265 | #endif | |
266 | module_param(use_eem, bool, 0); | |
267 | MODULE_PARM_DESC(use_eem, "use CDC EEM mode"); | |
268 | ||
0391c828 | 269 | /* |
9b39e9dd | 270 | * We _always_ have an ECM, CDC Subset, or EEM configuration. |
1da177e4 | 271 | */ |
e12995ec | 272 | static int __init eth_do_config(struct usb_configuration *c) |
0391c828 DB |
273 | { |
274 | /* FIXME alloc iConfiguration string, set it in c->strings */ | |
7e27f18c | 275 | |
0391c828 DB |
276 | if (gadget_is_otg(c->cdev->gadget)) { |
277 | c->descriptors = otg_desc; | |
278 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
279 | } | |
1da177e4 | 280 | |
9b39e9dd BN |
281 | if (use_eem) |
282 | return eem_bind_config(c); | |
283 | else if (can_support_ecm(c->cdev->gadget)) | |
0391c828 DB |
284 | return ecm_bind_config(c, hostaddr); |
285 | else | |
286 | return geth_bind_config(c, hostaddr); | |
287 | } | |
1da177e4 | 288 | |
0391c828 DB |
289 | static struct usb_configuration eth_config_driver = { |
290 | /* .label = f(hardware) */ | |
0391c828 DB |
291 | .bConfigurationValue = 1, |
292 | /* .iConfiguration = DYNAMIC */ | |
293 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
1da177e4 LT |
294 | }; |
295 | ||
0391c828 | 296 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 297 | |
e12995ec | 298 | static int __init eth_bind(struct usb_composite_dev *cdev) |
0391c828 DB |
299 | { |
300 | int gcnum; | |
301 | struct usb_gadget *gadget = cdev->gadget; | |
302 | int status; | |
1da177e4 | 303 | |
0391c828 DB |
304 | /* set up network link layer */ |
305 | status = gether_setup(cdev->gadget, hostaddr); | |
306 | if (status < 0) | |
307 | return status; | |
1da177e4 | 308 | |
0391c828 | 309 | /* set up main config label and device descriptor */ |
9b39e9dd BN |
310 | if (use_eem) { |
311 | /* EEM */ | |
312 | eth_config_driver.label = "CDC Ethernet (EEM)"; | |
313 | device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM); | |
314 | device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM); | |
315 | } else if (can_support_ecm(cdev->gadget)) { | |
0391c828 DB |
316 | /* ECM */ |
317 | eth_config_driver.label = "CDC Ethernet (ECM)"; | |
318 | } else { | |
319 | /* CDC Subset */ | |
320 | eth_config_driver.label = "CDC Subset/SAFE"; | |
1da177e4 | 321 | |
4e19f220 DB |
322 | device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM); |
323 | device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM); | |
324 | if (!has_rndis()) | |
325 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; | |
0391c828 | 326 | } |
1da177e4 | 327 | |
0391c828 DB |
328 | if (has_rndis()) { |
329 | /* RNDIS plus ECM-or-Subset */ | |
4e19f220 DB |
330 | device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM); |
331 | device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM); | |
0391c828 DB |
332 | device_desc.bNumConfigurations = 2; |
333 | } | |
1da177e4 | 334 | |
0391c828 DB |
335 | gcnum = usb_gadget_controller_number(gadget); |
336 | if (gcnum >= 0) | |
337 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | |
338 | else { | |
339 | /* We assume that can_support_ecm() tells the truth; | |
340 | * but if the controller isn't recognized at all then | |
341 | * that assumption is a bit more likely to be wrong. | |
342 | */ | |
33376c1c DB |
343 | dev_warn(&gadget->dev, |
344 | "controller '%s' not recognized; trying %s\n", | |
0391c828 DB |
345 | gadget->name, |
346 | eth_config_driver.label); | |
347 | device_desc.bcdDevice = | |
551509d2 | 348 | cpu_to_le16(0x0300 | 0x0099); |
0391c828 | 349 | } |
1da177e4 | 350 | |
1da177e4 | 351 | |
0391c828 DB |
352 | /* Allocate string descriptor numbers ... note that string |
353 | * contents can be overridden by the composite_dev glue. | |
354 | */ | |
1da177e4 | 355 | |
0391c828 DB |
356 | /* device descriptor strings: manufacturer, product */ |
357 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | |
358 | init_utsname()->sysname, init_utsname()->release, | |
359 | gadget->name); | |
360 | status = usb_string_id(cdev); | |
361 | if (status < 0) | |
362 | goto fail; | |
363 | strings_dev[STRING_MANUFACTURER_IDX].id = status; | |
364 | device_desc.iManufacturer = status; | |
1da177e4 | 365 | |
0391c828 DB |
366 | status = usb_string_id(cdev); |
367 | if (status < 0) | |
368 | goto fail; | |
369 | strings_dev[STRING_PRODUCT_IDX].id = status; | |
370 | device_desc.iProduct = status; | |
1da177e4 | 371 | |
0391c828 DB |
372 | /* register our configuration(s); RNDIS first, if it's used */ |
373 | if (has_rndis()) { | |
c9bfff9c UKK |
374 | status = usb_add_config(cdev, &rndis_config_driver, |
375 | rndis_do_config); | |
0391c828 DB |
376 | if (status < 0) |
377 | goto fail; | |
378 | } | |
1da177e4 | 379 | |
c9bfff9c | 380 | status = usb_add_config(cdev, ð_config_driver, eth_do_config); |
0391c828 DB |
381 | if (status < 0) |
382 | goto fail; | |
1da177e4 | 383 | |
33376c1c DB |
384 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
385 | DRIVER_DESC); | |
1da177e4 | 386 | |
0391c828 | 387 | return 0; |
1da177e4 | 388 | |
0391c828 DB |
389 | fail: |
390 | gether_cleanup(); | |
391 | return status; | |
392 | } | |
1da177e4 | 393 | |
0391c828 DB |
394 | static int __exit eth_unbind(struct usb_composite_dev *cdev) |
395 | { | |
396 | gether_cleanup(); | |
397 | return 0; | |
398 | } | |
1da177e4 | 399 | |
0391c828 DB |
400 | static struct usb_composite_driver eth_driver = { |
401 | .name = "g_ether", | |
402 | .dev = &device_desc, | |
403 | .strings = dev_strings, | |
35a0e0bf | 404 | .max_speed = USB_SPEED_HIGH, |
0391c828 | 405 | .unbind = __exit_p(eth_unbind), |
1da177e4 LT |
406 | }; |
407 | ||
0391c828 DB |
408 | MODULE_DESCRIPTION(PREFIX DRIVER_DESC); |
409 | MODULE_AUTHOR("David Brownell, Benedikt Spanger"); | |
410 | MODULE_LICENSE("GPL"); | |
1da177e4 | 411 | |
0391c828 | 412 | static int __init init(void) |
1da177e4 | 413 | { |
07a18bd7 | 414 | return usb_composite_probe(ð_driver, eth_bind); |
1da177e4 | 415 | } |
0391c828 | 416 | module_init(init); |
1da177e4 | 417 | |
0391c828 | 418 | static void __exit cleanup(void) |
1da177e4 | 419 | { |
0391c828 | 420 | usb_composite_unregister(ð_driver); |
1da177e4 | 421 | } |
0391c828 | 422 | module_exit(cleanup); |