]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/ether.c
MLK-9617-2 usb: gadget: set bcdOTG of OTG descriptor for gadget drivers
[karo-tx-linux.git] / drivers / usb / gadget / ether.c
1 /*
2  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
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
14 /* #define VERBOSE_DEBUG */
15
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18
19 #if defined USB_ETH_RNDIS
20 #  undef USB_ETH_RNDIS
21 #endif
22 #ifdef CONFIG_USB_ETH_RNDIS
23 #  define USB_ETH_RNDIS y
24 #endif
25
26 #include "u_ether.h"
27
28
29 /*
30  * Ethernet gadget driver -- with CDC and non-CDC options
31  * Builds on hardware support for a full duplex link.
32  *
33  * CDC Ethernet is the standard USB solution for sending Ethernet frames
34  * using USB.  Real hardware tends to use the same framing protocol but look
35  * different for control features.  This driver strongly prefers to use
36  * this USB-IF standard as its open-systems interoperability solution;
37  * most host side USB stacks (except from Microsoft) support it.
38  *
39  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
40  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new
41  * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
42  *
43  * There's some hardware that can't talk CDC ECM.  We make that hardware
44  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but
45  * link-level setup only requires activating the configuration.  Only the
46  * endpoint descriptors, and product/vendor IDs, are relevant; no control
47  * operations are available.  Linux supports it, but other host operating
48  * systems may not.  (This is a subset of CDC Ethernet.)
49  *
50  * It turns out that if you add a few descriptors to that "CDC Subset",
51  * (Windows) host side drivers from MCCI can treat it as one submode of
52  * a proprietary scheme called "SAFE" ... without needing to know about
53  * specific product/vendor IDs.  So we do that, making it easier to use
54  * those MS-Windows drivers.  Those added descriptors make it resemble a
55  * CDC MDLM device, but they don't change device behavior at all.  (See
56  * MCCI Engineering report 950198 "SAFE Networking Functions".)
57  *
58  * A third option is also in use.  Rather than CDC Ethernet, or something
59  * simpler, Microsoft pushes their own approach: RNDIS.  The published
60  * RNDIS specs are ambiguous and appear to be incomplete, and are also
61  * needlessly complex.  They borrow more from CDC ACM than CDC ECM.
62  */
63
64 #define DRIVER_DESC             "Ethernet Gadget"
65 #define DRIVER_VERSION          "Memorial Day 2008"
66
67 #ifdef USB_ETH_RNDIS
68 #define PREFIX                  "RNDIS/"
69 #else
70 #define PREFIX                  ""
71 #endif
72
73 /*
74  * This driver aims for interoperability by using CDC ECM unless
75  *
76  *              can_support_ecm()
77  *
78  * returns false, in which case it supports the CDC Subset.  By default,
79  * that returns true; most hardware has no problems with CDC ECM, that's
80  * a good default.  Previous versions of this driver had no default; this
81  * version changes that, removing overhead for new controller support.
82  *
83  *      IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
84  */
85
86 static inline bool has_rndis(void)
87 {
88 #ifdef  USB_ETH_RNDIS
89         return true;
90 #else
91         return false;
92 #endif
93 }
94
95 #include <linux/module.h>
96
97 #include "u_ecm.h"
98 #include "u_gether.h"
99 #ifdef  USB_ETH_RNDIS
100 #include "u_rndis.h"
101 #include "rndis.h"
102 #else
103 #define rndis_borrow_net(...) do {} while (0)
104 #endif
105 #include "u_eem.h"
106
107 /*-------------------------------------------------------------------------*/
108 USB_GADGET_COMPOSITE_OPTIONS();
109
110 USB_ETHERNET_MODULE_PARAMETERS();
111
112 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
113  * Instead:  allocate your own, using normal USB-IF procedures.
114  */
115
116 /* Thanks to NetChip Technologies for donating this product ID.
117  * It's for devices with only CDC Ethernet configurations.
118  */
119 #define CDC_VENDOR_NUM          0x0525  /* NetChip */
120 #define CDC_PRODUCT_NUM         0xa4a1  /* Linux-USB Ethernet Gadget */
121
122 /* For hardware that can't talk CDC, we use the same vendor ID that
123  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
124  * with pxa250.  We're protocol-compatible, if the host-side drivers
125  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
126  * drivers that need to hard-wire endpoint numbers have a hook.
127  *
128  * The protocol is a minimal subset of CDC Ether, which works on any bulk
129  * hardware that's not deeply broken ... even on hardware that can't talk
130  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
131  * doesn't handle control-OUT).
132  */
133 #define SIMPLE_VENDOR_NUM       0x049f
134 #define SIMPLE_PRODUCT_NUM      0x505a
135
136 /* For hardware that can talk RNDIS and either of the above protocols,
137  * use this ID ... the windows INF files will know it.  Unless it's
138  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
139  * the non-RNDIS configuration.
140  */
141 #define RNDIS_VENDOR_NUM        0x0525  /* NetChip */
142 #define RNDIS_PRODUCT_NUM       0xa4a2  /* Ethernet/RNDIS Gadget */
143
144 /* For EEM gadgets */
145 #define EEM_VENDOR_NUM          0x1d6b  /* Linux Foundation */
146 #define EEM_PRODUCT_NUM         0x0102  /* EEM Gadget */
147
148 /*-------------------------------------------------------------------------*/
149
150 static struct usb_device_descriptor device_desc = {
151         .bLength =              sizeof device_desc,
152         .bDescriptorType =      USB_DT_DEVICE,
153
154         .bcdUSB =               cpu_to_le16 (0x0200),
155
156         .bDeviceClass =         USB_CLASS_COMM,
157         .bDeviceSubClass =      0,
158         .bDeviceProtocol =      0,
159         /* .bMaxPacketSize0 = f(hardware) */
160
161         /* Vendor and product id defaults change according to what configs
162          * we support.  (As does bNumConfigurations.)  These values can
163          * also be overridden by module parameters.
164          */
165         .idVendor =             cpu_to_le16 (CDC_VENDOR_NUM),
166         .idProduct =            cpu_to_le16 (CDC_PRODUCT_NUM),
167         /* .bcdDevice = f(hardware) */
168         /* .iManufacturer = DYNAMIC */
169         /* .iProduct = DYNAMIC */
170         /* NO SERIAL NUMBER */
171         .bNumConfigurations =   1,
172 };
173
174 static struct usb_otg_descriptor otg_descriptor = {
175         .bLength =              sizeof otg_descriptor,
176         .bDescriptorType =      USB_DT_OTG,
177
178         /* REVISIT SRP-only hardware is possible, although
179          * it would not be called "OTG" ...
180          */
181         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
182         .bcdOTG =               cpu_to_le16(0x0200),
183 };
184
185 static const struct usb_descriptor_header *otg_desc[] = {
186         (struct usb_descriptor_header *) &otg_descriptor,
187         NULL,
188 };
189
190 static struct usb_string strings_dev[] = {
191         [USB_GADGET_MANUFACTURER_IDX].s = "",
192         [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
193         [USB_GADGET_SERIAL_IDX].s = "",
194         {  } /* end of list */
195 };
196
197 static struct usb_gadget_strings stringtab_dev = {
198         .language       = 0x0409,       /* en-us */
199         .strings        = strings_dev,
200 };
201
202 static struct usb_gadget_strings *dev_strings[] = {
203         &stringtab_dev,
204         NULL,
205 };
206
207 static struct usb_function_instance *fi_ecm;
208 static struct usb_function *f_ecm;
209
210 static struct usb_function_instance *fi_eem;
211 static struct usb_function *f_eem;
212
213 static struct usb_function_instance *fi_geth;
214 static struct usb_function *f_geth;
215
216 static struct usb_function_instance *fi_rndis;
217 static struct usb_function *f_rndis;
218
219 /*-------------------------------------------------------------------------*/
220
221 /*
222  * We may not have an RNDIS configuration, but if we do it needs to be
223  * the first one present.  That's to make Microsoft's drivers happy,
224  * and to follow DOCSIS 1.0 (cable modem standard).
225  */
226 static int __init rndis_do_config(struct usb_configuration *c)
227 {
228         int status;
229
230         /* FIXME alloc iConfiguration string, set it in c->strings */
231
232         if (gadget_is_otg(c->cdev->gadget)) {
233                 c->descriptors = otg_desc;
234                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
235         }
236
237         f_rndis = usb_get_function(fi_rndis);
238         if (IS_ERR(f_rndis))
239                 return PTR_ERR(f_rndis);
240
241         status = usb_add_function(c, f_rndis);
242         if (status < 0)
243                 usb_put_function(f_rndis);
244
245         return status;
246 }
247
248 static struct usb_configuration rndis_config_driver = {
249         .label                  = "RNDIS",
250         .bConfigurationValue    = 2,
251         /* .iConfiguration = DYNAMIC */
252         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
253 };
254
255 /*-------------------------------------------------------------------------*/
256
257 #ifdef CONFIG_USB_ETH_EEM
258 static bool use_eem = 1;
259 #else
260 static bool use_eem;
261 #endif
262 module_param(use_eem, bool, 0);
263 MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
264
265 /*
266  * We _always_ have an ECM, CDC Subset, or EEM configuration.
267  */
268 static int __init eth_do_config(struct usb_configuration *c)
269 {
270         int status = 0;
271
272         /* FIXME alloc iConfiguration string, set it in c->strings */
273
274         if (gadget_is_otg(c->cdev->gadget)) {
275                 c->descriptors = otg_desc;
276                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
277         }
278
279         if (use_eem) {
280                 f_eem = usb_get_function(fi_eem);
281                 if (IS_ERR(f_eem))
282                         return PTR_ERR(f_eem);
283
284                 status = usb_add_function(c, f_eem);
285                 if (status < 0)
286                         usb_put_function(f_eem);
287
288                 return status;
289         } else if (can_support_ecm(c->cdev->gadget)) {
290                 f_ecm = usb_get_function(fi_ecm);
291                 if (IS_ERR(f_ecm))
292                         return PTR_ERR(f_ecm);
293
294                 status = usb_add_function(c, f_ecm);
295                 if (status < 0)
296                         usb_put_function(f_ecm);
297
298                 return status;
299         } else {
300                 f_geth = usb_get_function(fi_geth);
301                 if (IS_ERR(f_geth))
302                         return PTR_ERR(f_geth);
303
304                 status = usb_add_function(c, f_geth);
305                 if (status < 0)
306                         usb_put_function(f_geth);
307
308                 return status;
309         }
310
311 }
312
313 static struct usb_configuration eth_config_driver = {
314         /* .label = f(hardware) */
315         .bConfigurationValue    = 1,
316         /* .iConfiguration = DYNAMIC */
317         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
318 };
319
320 /*-------------------------------------------------------------------------*/
321
322 static int __init eth_bind(struct usb_composite_dev *cdev)
323 {
324         struct usb_gadget       *gadget = cdev->gadget;
325         struct f_eem_opts       *eem_opts = NULL;
326         struct f_ecm_opts       *ecm_opts = NULL;
327         struct f_gether_opts    *geth_opts = NULL;
328         struct net_device       *net;
329         int                     status;
330
331         /* set up main config label and device descriptor */
332         if (use_eem) {
333                 /* EEM */
334                 fi_eem = usb_get_function_instance("eem");
335                 if (IS_ERR(fi_eem))
336                         return PTR_ERR(fi_eem);
337
338                 eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
339
340                 net = eem_opts->net;
341
342                 eth_config_driver.label = "CDC Ethernet (EEM)";
343                 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
344                 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
345         } else if (can_support_ecm(gadget)) {
346                 /* ECM */
347
348                 fi_ecm = usb_get_function_instance("ecm");
349                 if (IS_ERR(fi_ecm))
350                         return PTR_ERR(fi_ecm);
351
352                 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
353
354                 net = ecm_opts->net;
355
356                 eth_config_driver.label = "CDC Ethernet (ECM)";
357         } else {
358                 /* CDC Subset */
359
360                 fi_geth = usb_get_function_instance("geth");
361                 if (IS_ERR(fi_geth))
362                         return PTR_ERR(fi_geth);
363
364                 geth_opts = container_of(fi_geth, struct f_gether_opts,
365                                          func_inst);
366
367                 net = geth_opts->net;
368
369                 eth_config_driver.label = "CDC Subset/SAFE";
370
371                 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
372                 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
373                 if (!has_rndis())
374                         device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
375         }
376
377         gether_set_qmult(net, qmult);
378         if (!gether_set_host_addr(net, host_addr))
379                 pr_info("using host ethernet address: %s", host_addr);
380         if (!gether_set_dev_addr(net, dev_addr))
381                 pr_info("using self ethernet address: %s", dev_addr);
382
383         if (has_rndis()) {
384                 /* RNDIS plus ECM-or-Subset */
385                 gether_set_gadget(net, cdev->gadget);
386                 status = gether_register_netdev(net);
387                 if (status)
388                         goto fail;
389
390                 if (use_eem)
391                         eem_opts->bound = true;
392                 else if (can_support_ecm(gadget))
393                         ecm_opts->bound = true;
394                 else
395                         geth_opts->bound = true;
396
397                 fi_rndis = usb_get_function_instance("rndis");
398                 if (IS_ERR(fi_rndis)) {
399                         status = PTR_ERR(fi_rndis);
400                         goto fail;
401                 }
402
403                 rndis_borrow_net(fi_rndis, net);
404
405                 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
406                 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
407                 device_desc.bNumConfigurations = 2;
408         }
409
410         /* Allocate string descriptor numbers ... note that string
411          * contents can be overridden by the composite_dev glue.
412          */
413
414         status = usb_string_ids_tab(cdev, strings_dev);
415         if (status < 0)
416                 goto fail1;
417         device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
418         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
419
420         /* register our configuration(s); RNDIS first, if it's used */
421         if (has_rndis()) {
422                 status = usb_add_config(cdev, &rndis_config_driver,
423                                 rndis_do_config);
424                 if (status < 0)
425                         goto fail1;
426         }
427
428         status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
429         if (status < 0)
430                 goto fail1;
431
432         usb_composite_overwrite_options(cdev, &coverwrite);
433         dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
434                         DRIVER_DESC);
435
436         return 0;
437
438 fail1:
439         if (has_rndis())
440                 usb_put_function_instance(fi_rndis);
441 fail:
442         if (use_eem)
443                 usb_put_function_instance(fi_eem);
444         else if (can_support_ecm(gadget))
445                 usb_put_function_instance(fi_ecm);
446         else
447                 usb_put_function_instance(fi_geth);
448         return status;
449 }
450
451 static int __exit eth_unbind(struct usb_composite_dev *cdev)
452 {
453         if (has_rndis()) {
454                 usb_put_function(f_rndis);
455                 usb_put_function_instance(fi_rndis);
456         }
457         if (use_eem) {
458                 usb_put_function(f_eem);
459                 usb_put_function_instance(fi_eem);
460         } else if (can_support_ecm(cdev->gadget)) {
461                 usb_put_function(f_ecm);
462                 usb_put_function_instance(fi_ecm);
463         } else {
464                 usb_put_function(f_geth);
465                 usb_put_function_instance(fi_geth);
466         }
467         return 0;
468 }
469
470 static __refdata struct usb_composite_driver eth_driver = {
471         .name           = "g_ether",
472         .dev            = &device_desc,
473         .strings        = dev_strings,
474         .max_speed      = USB_SPEED_SUPER,
475         .bind           = eth_bind,
476         .unbind         = __exit_p(eth_unbind),
477 };
478
479 MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
480 MODULE_AUTHOR("David Brownell, Benedikt Spanger");
481 MODULE_LICENSE("GPL");
482
483 static int __init init(void)
484 {
485         return usb_composite_probe(&eth_driver);
486 }
487 module_init(init);
488
489 static void __exit cleanup(void)
490 {
491         usb_composite_unregister(&eth_driver);
492 }
493 module_exit(cleanup);