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> |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 17 | #include <linux/netdevice.h> |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 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 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 26 | #include "u_ether.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
David Brownell | 11d5489 | 2006-12-11 15:59:04 -0800 | [diff] [blame] | 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".) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 61 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
| 63 | |
| 64 | #define DRIVER_DESC "Ethernet Gadget" |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 65 | #define DRIVER_VERSION "Memorial Day 2008" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 67 | #ifdef USB_ETH_RNDIS |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 68 | #define PREFIX "RNDIS/" |
| 69 | #else |
| 70 | #define PREFIX "" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #endif |
| 72 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 73 | /* |
| 74 | * This driver aims for interoperability by using CDC ECM unless |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 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! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 86 | static inline bool has_rndis(void) |
| 87 | { |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 88 | #ifdef USB_ETH_RNDIS |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 89 | return true; |
| 90 | #else |
| 91 | return false; |
| 92 | #endif |
| 93 | } |
| 94 | |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 95 | #include <linux/module.h> |
| 96 | |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 97 | #include "u_ecm.h" |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 98 | #include "u_gether.h" |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 99 | #ifdef USB_ETH_RNDIS |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 100 | #include "u_rndis.h" |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 101 | #include "rndis.h" |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 102 | #else |
| 103 | #define rndis_borrow_net(...) do {} while (0) |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 104 | #endif |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 105 | #include "u_eem.h" |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 106 | |
| 107 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 108 | USB_GADGET_COMPOSITE_OPTIONS(); |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 109 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 110 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 119 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 120 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 144 | /* For EEM gadgets */ |
Brian Niebuhr | 4238ef5 | 2009-10-14 12:04:33 -0500 | [diff] [blame] | 145 | #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
| 146 | #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */ |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /*-------------------------------------------------------------------------*/ |
| 149 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 150 | static struct usb_device_descriptor device_desc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | .bLength = sizeof device_desc, |
| 152 | .bDescriptorType = USB_DT_DEVICE, |
| 153 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 154 | .bcdUSB = cpu_to_le16 (0x0200), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | .bDeviceClass = USB_CLASS_COMM, |
| 157 | .bDeviceSubClass = 0, |
| 158 | .bDeviceProtocol = 0, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 159 | /* .bMaxPacketSize0 = f(hardware) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 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 | */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 165 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
| 166 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 167 | /* .bcdDevice = f(hardware) */ |
| 168 | /* .iManufacturer = DYNAMIC */ |
| 169 | /* .iProduct = DYNAMIC */ |
| 170 | /* NO SERIAL NUMBER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | .bNumConfigurations = 1, |
| 172 | }; |
| 173 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 174 | static struct usb_otg_descriptor otg_descriptor = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | .bLength = sizeof otg_descriptor, |
| 176 | .bDescriptorType = USB_DT_OTG, |
| 177 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 178 | /* REVISIT SRP-only hardware is possible, although |
| 179 | * it would not be called "OTG" ... |
| 180 | */ |
| 181 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 184 | static const struct usb_descriptor_header *otg_desc[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | (struct usb_descriptor_header *) &otg_descriptor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | NULL, |
| 187 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 189 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 190 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 191 | [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC, |
| 192 | [USB_GADGET_SERIAL_IDX].s = "", |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 193 | { } /* end of list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 196 | static struct usb_gadget_strings stringtab_dev = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | .language = 0x0409, /* en-us */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 198 | .strings = strings_dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 201 | static struct usb_gadget_strings *dev_strings[] = { |
| 202 | &stringtab_dev, |
| 203 | NULL, |
| 204 | }; |
| 205 | |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 206 | static struct usb_function_instance *fi_ecm; |
| 207 | static struct usb_function *f_ecm; |
| 208 | |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 209 | static struct usb_function_instance *fi_eem; |
| 210 | static struct usb_function *f_eem; |
| 211 | |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 212 | static struct usb_function_instance *fi_geth; |
| 213 | static struct usb_function *f_geth; |
| 214 | |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 215 | static struct usb_function_instance *fi_rndis; |
| 216 | static struct usb_function *f_rndis; |
| 217 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 218 | /*-------------------------------------------------------------------------*/ |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 221 | * We may not have an RNDIS configuration, but if we do it needs to be |
| 222 | * the first one present. That's to make Microsoft's drivers happy, |
| 223 | * and to follow DOCSIS 1.0 (cable modem standard). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | */ |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 225 | static int __init rndis_do_config(struct usb_configuration *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 227 | int status; |
| 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 | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 236 | f_rndis = usb_get_function(fi_rndis); |
| 237 | if (IS_ERR(f_rndis)) |
| 238 | return PTR_ERR(f_rndis); |
| 239 | |
| 240 | status = usb_add_function(c, f_rndis); |
| 241 | if (status < 0) |
| 242 | usb_put_function(f_rndis); |
| 243 | |
| 244 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 247 | static struct usb_configuration rndis_config_driver = { |
| 248 | .label = "RNDIS", |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 249 | .bConfigurationValue = 2, |
| 250 | /* .iConfiguration = DYNAMIC */ |
| 251 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /*-------------------------------------------------------------------------*/ |
| 255 | |
Robert P. J. Day | c9188ad | 2009-12-28 06:31:08 -0500 | [diff] [blame] | 256 | #ifdef CONFIG_USB_ETH_EEM |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 257 | static bool use_eem = 1; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 258 | #else |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 259 | static bool use_eem; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 260 | #endif |
| 261 | module_param(use_eem, bool, 0); |
| 262 | MODULE_PARM_DESC(use_eem, "use CDC EEM mode"); |
| 263 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 264 | /* |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 265 | * We _always_ have an ECM, CDC Subset, or EEM configuration. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | */ |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 267 | static int __init eth_do_config(struct usb_configuration *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 269 | int status = 0; |
| 270 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 271 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 273 | if (gadget_is_otg(c->cdev->gadget)) { |
| 274 | c->descriptors = otg_desc; |
| 275 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 278 | if (use_eem) { |
| 279 | f_eem = usb_get_function(fi_eem); |
| 280 | if (IS_ERR(f_eem)) |
| 281 | return PTR_ERR(f_eem); |
| 282 | |
| 283 | status = usb_add_function(c, f_eem); |
| 284 | if (status < 0) |
| 285 | usb_put_function(f_eem); |
| 286 | |
| 287 | return status; |
| 288 | } else if (can_support_ecm(c->cdev->gadget)) { |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 289 | f_ecm = usb_get_function(fi_ecm); |
| 290 | if (IS_ERR(f_ecm)) |
| 291 | return PTR_ERR(f_ecm); |
| 292 | |
| 293 | status = usb_add_function(c, f_ecm); |
| 294 | if (status < 0) |
| 295 | usb_put_function(f_ecm); |
| 296 | |
| 297 | return status; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 298 | } else { |
| 299 | f_geth = usb_get_function(fi_geth); |
| 300 | if (IS_ERR(f_geth)) |
| 301 | return PTR_ERR(f_geth); |
| 302 | |
| 303 | status = usb_add_function(c, f_geth); |
| 304 | if (status < 0) |
| 305 | usb_put_function(f_geth); |
| 306 | |
| 307 | return status; |
| 308 | } |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 312 | static struct usb_configuration eth_config_driver = { |
| 313 | /* .label = f(hardware) */ |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 314 | .bConfigurationValue = 1, |
| 315 | /* .iConfiguration = DYNAMIC */ |
| 316 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 317 | }; |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /*-------------------------------------------------------------------------*/ |
| 320 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 321 | static int __init eth_bind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 323 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 324 | struct f_eem_opts *eem_opts = NULL; |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 325 | struct f_ecm_opts *ecm_opts = NULL; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 326 | struct f_gether_opts *geth_opts = NULL; |
| 327 | struct net_device *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | int status; |
David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 329 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 330 | /* set up main config label and device descriptor */ |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 331 | if (use_eem) { |
| 332 | /* EEM */ |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 333 | fi_eem = usb_get_function_instance("eem"); |
| 334 | if (IS_ERR(fi_eem)) |
| 335 | return PTR_ERR(fi_eem); |
| 336 | |
| 337 | eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst); |
| 338 | |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 339 | net = eem_opts->net; |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 340 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 341 | eth_config_driver.label = "CDC Ethernet (EEM)"; |
| 342 | device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM); |
| 343 | device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM); |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 344 | } else if (can_support_ecm(gadget)) { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 345 | /* ECM */ |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 346 | |
| 347 | fi_ecm = usb_get_function_instance("ecm"); |
| 348 | if (IS_ERR(fi_ecm)) |
| 349 | return PTR_ERR(fi_ecm); |
| 350 | |
| 351 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
| 352 | |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 353 | net = ecm_opts->net; |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 354 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 355 | eth_config_driver.label = "CDC Ethernet (ECM)"; |
| 356 | } else { |
| 357 | /* CDC Subset */ |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 358 | |
| 359 | fi_geth = usb_get_function_instance("geth"); |
| 360 | if (IS_ERR(fi_geth)) |
| 361 | return PTR_ERR(fi_geth); |
| 362 | |
| 363 | geth_opts = container_of(fi_geth, struct f_gether_opts, |
| 364 | func_inst); |
| 365 | |
| 366 | net = geth_opts->net; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 367 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 368 | eth_config_driver.label = "CDC Subset/SAFE"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 370 | device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM); |
| 371 | device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM); |
| 372 | if (!has_rndis()) |
| 373 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 376 | gether_set_qmult(net, qmult); |
| 377 | if (!gether_set_host_addr(net, host_addr)) |
| 378 | pr_info("using host ethernet address: %s", host_addr); |
| 379 | if (!gether_set_dev_addr(net, dev_addr)) |
| 380 | pr_info("using self ethernet address: %s", dev_addr); |
| 381 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 382 | if (has_rndis()) { |
| 383 | /* RNDIS plus ECM-or-Subset */ |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 384 | gether_set_gadget(net, cdev->gadget); |
| 385 | status = gether_register_netdev(net); |
| 386 | if (status) |
| 387 | goto fail; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 388 | |
| 389 | if (use_eem) |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 390 | eem_opts->bound = true; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 391 | else if (can_support_ecm(gadget)) |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 392 | ecm_opts->bound = true; |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 393 | else |
| 394 | geth_opts->bound = true; |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 395 | |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 396 | fi_rndis = usb_get_function_instance("rndis"); |
| 397 | if (IS_ERR(fi_rndis)) { |
| 398 | status = PTR_ERR(fi_rndis); |
| 399 | goto fail; |
| 400 | } |
| 401 | |
| 402 | rndis_borrow_net(fi_rndis, net); |
| 403 | |
David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 404 | device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM); |
| 405 | device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 406 | device_desc.bNumConfigurations = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 409 | /* Allocate string descriptor numbers ... note that string |
| 410 | * contents can be overridden by the composite_dev glue. |
| 411 | */ |
| 412 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 413 | status = usb_string_ids_tab(cdev, strings_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (status < 0) |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 415 | goto fail1; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 416 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 417 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 419 | /* register our configuration(s); RNDIS first, if it's used */ |
| 420 | if (has_rndis()) { |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 421 | status = usb_add_config(cdev, &rndis_config_driver, |
| 422 | rndis_do_config); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 423 | if (status < 0) |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 424 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 427 | status = usb_add_config(cdev, ð_config_driver, eth_do_config); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 428 | if (status < 0) |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 429 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 431 | usb_composite_overwrite_options(cdev, &coverwrite); |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 432 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
| 433 | DRIVER_DESC); |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 437 | fail1: |
| 438 | if (has_rndis()) |
| 439 | usb_put_function_instance(fi_rndis); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | fail: |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 441 | if (use_eem) |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 442 | usb_put_function_instance(fi_eem); |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 443 | else if (can_support_ecm(gadget)) |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 444 | usb_put_function_instance(fi_ecm); |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 445 | else |
| 446 | usb_put_function_instance(fi_geth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return status; |
| 448 | } |
| 449 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 450 | static int __exit eth_unbind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Andrzej Pietrasiewicz | 23a113a | 2013-07-24 12:47:46 +0200 | [diff] [blame] | 452 | if (has_rndis()) { |
| 453 | usb_put_function(f_rndis); |
Andrzej Pietrasiewicz | 9bd4a10 | 2013-05-28 09:15:58 +0200 | [diff] [blame] | 454 | usb_put_function_instance(fi_rndis); |
Andrzej Pietrasiewicz | 23a113a | 2013-07-24 12:47:46 +0200 | [diff] [blame] | 455 | } |
| 456 | if (use_eem) { |
| 457 | usb_put_function(f_eem); |
Andrzej Pietrasiewicz | 94b5573 | 2013-05-28 09:15:48 +0200 | [diff] [blame] | 458 | usb_put_function_instance(fi_eem); |
Andrzej Pietrasiewicz | 23a113a | 2013-07-24 12:47:46 +0200 | [diff] [blame] | 459 | } else if (can_support_ecm(cdev->gadget)) { |
| 460 | usb_put_function(f_ecm); |
Andrzej Pietrasiewicz | 9c62ce8 | 2013-05-28 09:15:46 +0200 | [diff] [blame] | 461 | usb_put_function_instance(fi_ecm); |
Andrzej Pietrasiewicz | 23a113a | 2013-07-24 12:47:46 +0200 | [diff] [blame] | 462 | } else { |
| 463 | usb_put_function(f_geth); |
Andrzej Pietrasiewicz | 8af5232 | 2013-05-28 09:15:54 +0200 | [diff] [blame] | 464 | usb_put_function_instance(fi_geth); |
Andrzej Pietrasiewicz | 23a113a | 2013-07-24 12:47:46 +0200 | [diff] [blame] | 465 | } |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 466 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 469 | static __refdata struct usb_composite_driver eth_driver = { |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 470 | .name = "g_ether", |
| 471 | .dev = &device_desc, |
| 472 | .strings = dev_strings, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 473 | .max_speed = USB_SPEED_SUPER, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 474 | .bind = eth_bind, |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 475 | .unbind = __exit_p(eth_unbind), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | }; |
| 477 | |
Tobias Klauser | 909346a | 2014-07-09 18:09:56 +0200 | [diff] [blame] | 478 | module_usb_composite_driver(eth_driver); |
| 479 | |
David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 480 | MODULE_DESCRIPTION(PREFIX DRIVER_DESC); |
| 481 | MODULE_AUTHOR("David Brownell, Benedikt Spanger"); |
| 482 | MODULE_LICENSE("GPL"); |