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