Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options |
| 3 | * |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 4 | * Copyright (C) 2003-2005,2008 David Brownell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 6 | * Copyright (C) 2008 Nokia Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
David Brownell | 0cf4f2d | 2007-08-02 00:00:38 -0700 | [diff] [blame] | 14 | /* #define VERBOSE_DEBUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 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 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 25 | #include "u_ether.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
David Brownell | 11d5489 | 2006-12-11 15:59:04 -0800 | [diff] [blame] | 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".) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 60 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | #define DRIVER_DESC "Ethernet Gadget" |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 64 | #define DRIVER_VERSION "Memorial Day 2008" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 66 | #ifdef USB_ETH_RNDIS |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 67 | #define PREFIX "RNDIS/" |
| 68 | #else |
| 69 | #define PREFIX "" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #endif |
| 71 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 72 | /* |
| 73 | * This driver aims for interoperability by using CDC ECM unless |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 75 | * can_support_ecm() |
| 76 | * |
| 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! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 85 | static inline bool has_rndis(void) |
| 86 | { |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 87 | #ifdef USB_ETH_RNDIS |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 88 | return true; |
| 89 | #else |
| 90 | return false; |
| 91 | #endif |
| 92 | } |
| 93 | |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 94 | #include <linux/module.h> |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /*-------------------------------------------------------------------------*/ |
| 97 | |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 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 | */ |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 105 | #include "u_ecm.h" |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 106 | #include "f_subset.c" |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 107 | #ifdef USB_ETH_RNDIS |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 108 | #include "f_rndis.c" |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 109 | #include "rndis.h" |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 110 | #endif |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 111 | |
| 112 | #define USB_FEEM_INCLUDED |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 113 | #include "f_eem.c" |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 114 | |
| 115 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 116 | USB_GADGET_COMPOSITE_OPTIONS(); |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 117 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 118 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 121 | * Instead: allocate your own, using normal USB-IF procedures. |
| 122 | */ |
| 123 | |
| 124 | /* Thanks to NetChip Technologies for donating this product ID. |
| 125 | * It's for devices with only CDC Ethernet configurations. |
| 126 | */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 127 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 128 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /* For hardware that can't talk CDC, we use the same vendor ID that |
| 131 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
| 132 | * with pxa250. We're protocol-compatible, if the host-side drivers |
| 133 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so |
| 134 | * drivers that need to hard-wire endpoint numbers have a hook. |
| 135 | * |
| 136 | * The protocol is a minimal subset of CDC Ether, which works on any bulk |
| 137 | * hardware that's not deeply broken ... even on hardware that can't talk |
| 138 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that |
| 139 | * doesn't handle control-OUT). |
| 140 | */ |
| 141 | #define SIMPLE_VENDOR_NUM 0x049f |
| 142 | #define SIMPLE_PRODUCT_NUM 0x505a |
| 143 | |
| 144 | /* For hardware that can talk RNDIS and either of the above protocols, |
| 145 | * use this ID ... the windows INF files will know it. Unless it's |
| 146 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose |
| 147 | * the non-RNDIS configuration. |
| 148 | */ |
| 149 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ |
| 150 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ |
| 151 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 152 | /* For EEM gadgets */ |
Brian Niebuhr | 4238ef5 | 2009-10-14 12:04:33 -0500 | [diff] [blame] | 153 | #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
| 154 | #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */ |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /*-------------------------------------------------------------------------*/ |
| 157 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 158 | static struct usb_device_descriptor device_desc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | .bLength = sizeof device_desc, |
| 160 | .bDescriptorType = USB_DT_DEVICE, |
| 161 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 162 | .bcdUSB = cpu_to_le16 (0x0200), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | .bDeviceClass = USB_CLASS_COMM, |
| 165 | .bDeviceSubClass = 0, |
| 166 | .bDeviceProtocol = 0, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 167 | /* .bMaxPacketSize0 = f(hardware) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 169 | /* Vendor and product id defaults change according to what configs |
| 170 | * we support. (As does bNumConfigurations.) These values can |
| 171 | * also be overridden by module parameters. |
| 172 | */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 173 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
| 174 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 175 | /* .bcdDevice = f(hardware) */ |
| 176 | /* .iManufacturer = DYNAMIC */ |
| 177 | /* .iProduct = DYNAMIC */ |
| 178 | /* NO SERIAL NUMBER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | .bNumConfigurations = 1, |
| 180 | }; |
| 181 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 182 | static struct usb_otg_descriptor otg_descriptor = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | .bLength = sizeof otg_descriptor, |
| 184 | .bDescriptorType = USB_DT_OTG, |
| 185 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 186 | /* REVISIT SRP-only hardware is possible, although |
| 187 | * it would not be called "OTG" ... |
| 188 | */ |
| 189 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 192 | static const struct usb_descriptor_header *otg_desc[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | (struct usb_descriptor_header *) &otg_descriptor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | NULL, |
| 195 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 197 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 198 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 199 | [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC, |
| 200 | [USB_GADGET_SERIAL_IDX].s = "", |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 201 | { } /* end of list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 204 | static struct usb_gadget_strings stringtab_dev = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | .language = 0x0409, /* en-us */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 206 | .strings = strings_dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 209 | static struct usb_gadget_strings *dev_strings[] = { |
| 210 | &stringtab_dev, |
| 211 | NULL, |
| 212 | }; |
| 213 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 214 | static u8 host_mac[ETH_ALEN]; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 215 | static struct eth_dev *the_dev; |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 216 | |
| 217 | static struct usb_function_instance *fi_ecm; |
| 218 | static struct usb_function *f_ecm; |
| 219 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 220 | /*-------------------------------------------------------------------------*/ |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 223 | * We may not have an RNDIS configuration, but if we do it needs to be |
| 224 | * the first one present. That's to make Microsoft's drivers happy, |
| 225 | * and to follow DOCSIS 1.0 (cable modem standard). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | */ |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 227 | static int __init rndis_do_config(struct usb_configuration *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 229 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 231 | if (gadget_is_otg(c->cdev->gadget)) { |
| 232 | c->descriptors = otg_desc; |
| 233 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 236 | return rndis_bind_config(c, host_mac, the_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 239 | static struct usb_configuration rndis_config_driver = { |
| 240 | .label = "RNDIS", |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 241 | .bConfigurationValue = 2, |
| 242 | /* .iConfiguration = DYNAMIC */ |
| 243 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /*-------------------------------------------------------------------------*/ |
| 247 | |
Robert P. J. Day | c9188ad | 2009-12-28 06:31:08 -0500 | [diff] [blame] | 248 | #ifdef CONFIG_USB_ETH_EEM |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 249 | static bool use_eem = 1; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 250 | #else |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 251 | static bool use_eem; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 252 | #endif |
| 253 | module_param(use_eem, bool, 0); |
| 254 | MODULE_PARM_DESC(use_eem, "use CDC EEM mode"); |
| 255 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 256 | /* |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 257 | * We _always_ have an ECM, CDC Subset, or EEM configuration. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | */ |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 259 | static int __init eth_do_config(struct usb_configuration *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 261 | int status = 0; |
| 262 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 263 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 265 | if (gadget_is_otg(c->cdev->gadget)) { |
| 266 | c->descriptors = otg_desc; |
| 267 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 270 | if (use_eem) |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 271 | return eem_bind_config(c, the_dev); |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 272 | else if (can_support_ecm(c->cdev->gadget)) { |
| 273 | f_ecm = usb_get_function(fi_ecm); |
| 274 | if (IS_ERR(f_ecm)) |
| 275 | return PTR_ERR(f_ecm); |
| 276 | |
| 277 | status = usb_add_function(c, f_ecm); |
| 278 | if (status < 0) |
| 279 | usb_put_function(f_ecm); |
| 280 | |
| 281 | return status; |
| 282 | } else |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 283 | return geth_bind_config(c, host_mac, the_dev); |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 287 | static struct usb_configuration eth_config_driver = { |
| 288 | /* .label = f(hardware) */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 289 | .bConfigurationValue = 1, |
| 290 | /* .iConfiguration = DYNAMIC */ |
| 291 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | /*-------------------------------------------------------------------------*/ |
| 295 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 296 | static int __init eth_bind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 298 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 299 | struct f_ecm_opts *ecm_opts = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | int status; |
David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 301 | |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 302 | if (use_eem || !can_support_ecm(gadget)) { |
| 303 | /* set up network link layer */ |
| 304 | the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, |
| 305 | host_mac, qmult); |
| 306 | if (IS_ERR(the_dev)) |
| 307 | return PTR_ERR(the_dev); |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 310 | /* set up main config label and device descriptor */ |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 311 | if (use_eem) { |
| 312 | /* EEM */ |
| 313 | eth_config_driver.label = "CDC Ethernet (EEM)"; |
| 314 | device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM); |
| 315 | device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM); |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 316 | } else if (can_support_ecm(gadget)) { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 317 | /* ECM */ |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 318 | |
| 319 | fi_ecm = usb_get_function_instance("ecm"); |
| 320 | if (IS_ERR(fi_ecm)) |
| 321 | return PTR_ERR(fi_ecm); |
| 322 | |
| 323 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
| 324 | |
| 325 | gether_set_qmult(ecm_opts->net, qmult); |
| 326 | if (!gether_set_host_addr(ecm_opts->net, host_addr)) |
| 327 | pr_info("using host ethernet address: %s", host_addr); |
| 328 | if (!gether_set_dev_addr(ecm_opts->net, dev_addr)) |
| 329 | pr_info("using self ethernet address: %s", dev_addr); |
| 330 | |
| 331 | the_dev = netdev_priv(ecm_opts->net); |
| 332 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 333 | eth_config_driver.label = "CDC Ethernet (ECM)"; |
| 334 | } else { |
| 335 | /* CDC Subset */ |
| 336 | eth_config_driver.label = "CDC Subset/SAFE"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 338 | device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM); |
| 339 | device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM); |
| 340 | if (!has_rndis()) |
| 341 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 344 | if (has_rndis()) { |
| 345 | /* RNDIS plus ECM-or-Subset */ |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 346 | if (!use_eem && can_support_ecm(gadget)) { |
| 347 | gether_set_gadget(ecm_opts->net, cdev->gadget); |
| 348 | status = gether_register_netdev(ecm_opts->net); |
| 349 | if (status) |
| 350 | goto fail; |
| 351 | ecm_opts->bound = true; |
| 352 | gether_get_host_addr_u8(ecm_opts->net, host_mac); |
| 353 | } |
| 354 | |
David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 355 | device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM); |
| 356 | device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 357 | device_desc.bNumConfigurations = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 360 | /* Allocate string descriptor numbers ... note that string |
| 361 | * contents can be overridden by the composite_dev glue. |
| 362 | */ |
| 363 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 364 | status = usb_string_ids_tab(cdev, strings_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (status < 0) |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 366 | goto fail; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 367 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 368 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 370 | /* register our configuration(s); RNDIS first, if it's used */ |
| 371 | if (has_rndis()) { |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 372 | status = usb_add_config(cdev, &rndis_config_driver, |
| 373 | rndis_do_config); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 374 | if (status < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 378 | status = usb_add_config(cdev, ð_config_driver, eth_do_config); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 379 | if (status < 0) |
| 380 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 382 | usb_composite_overwrite_options(cdev, &coverwrite); |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 383 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
| 384 | DRIVER_DESC); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | fail: |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 389 | if (use_eem || !can_support_ecm(gadget)) |
| 390 | gether_cleanup(the_dev); |
| 391 | else |
| 392 | usb_put_function_instance(fi_ecm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return status; |
| 394 | } |
| 395 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 396 | static int __exit eth_unbind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 398 | if (use_eem || !can_support_ecm(cdev->gadget)) |
| 399 | gether_cleanup(the_dev); |
| 400 | else |
| 401 | usb_put_function_instance(fi_ecm); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 402 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 405 | static __refdata struct usb_composite_driver eth_driver = { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 406 | .name = "g_ether", |
| 407 | .dev = &device_desc, |
| 408 | .strings = dev_strings, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 409 | .max_speed = USB_SPEED_SUPER, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 410 | .bind = eth_bind, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 411 | .unbind = __exit_p(eth_unbind), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 414 | MODULE_DESCRIPTION(PREFIX DRIVER_DESC); |
| 415 | MODULE_AUTHOR("David Brownell, Benedikt Spanger"); |
| 416 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 418 | static int __init init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 420 | return usb_composite_probe(ð_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 422 | module_init(init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 424 | static void __exit cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 426 | usb_composite_unregister(ð_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 428 | module_exit(cleanup); |