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