David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * f_subset.c -- "CDC Subset" Ethernet link function driver |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | |
| 18 | #include "u_ether.h" |
| 19 | |
| 20 | |
| 21 | /* |
| 22 | * This function packages a simple "CDC Subset" Ethernet port with no real |
| 23 | * control mechanisms; just raw data transfer over two bulk endpoints. |
| 24 | * The data transfer model is exactly that of CDC Ethernet, which is |
| 25 | * why we call it the "CDC Subset". |
| 26 | * |
| 27 | * Because it's not standardized, this has some interoperability issues. |
| 28 | * They mostly relate to driver binding, since the data transfer model is |
| 29 | * so simple (CDC Ethernet). The original versions of this protocol used |
| 30 | * specific product/vendor IDs: byteswapped IDs for Digital Equipment's |
| 31 | * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported |
| 32 | * daughtercards with USB peripheral connectors. (It was used more often |
| 33 | * with other boards, using the Itsy identifiers.) Linux hosts recognized |
| 34 | * this with CONFIG_USB_ARMLINUX; these devices have only one configuration |
| 35 | * and one interface. |
| 36 | * |
| 37 | * At some point, MCCI defined a (nonconformant) CDC MDLM variant called |
| 38 | * "SAFE", which happens to have a mode which is identical to the "CDC |
| 39 | * Subset" in terms of data transfer and lack of control model. This was |
| 40 | * adopted by later Sharp Zaurus models, and by some other software which |
| 41 | * Linux hosts recognize with CONFIG_USB_NET_ZAURUS. |
| 42 | * |
| 43 | * Because Microsoft's RNDIS drivers are far from robust, we added a few |
| 44 | * descriptors to the CDC Subset code, making this code look like a SAFE |
| 45 | * implementation. This lets you use MCCI's host side MS-Windows drivers |
| 46 | * if you get fed up with RNDIS. It also makes it easier for composite |
| 47 | * drivers to work, since they can use class based binding instead of |
| 48 | * caring about specific product and vendor IDs. |
| 49 | */ |
| 50 | |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 51 | struct f_gether { |
| 52 | struct gether port; |
| 53 | |
| 54 | char ethaddr[14]; |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static inline struct f_gether *func_to_geth(struct usb_function *f) |
| 58 | { |
| 59 | return container_of(f, struct f_gether, port.func); |
| 60 | } |
| 61 | |
| 62 | /*-------------------------------------------------------------------------*/ |
| 63 | |
| 64 | /* |
| 65 | * "Simple" CDC-subset option is a simple vendor-neutral model that most |
| 66 | * full speed controllers can handle: one interface, two bulk endpoints. |
| 67 | * To assist host side drivers, we fancy it up a bit, and add descriptors so |
| 68 | * some host side drivers will understand it as a "SAFE" variant. |
| 69 | * |
| 70 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways. |
| 71 | * Data endpoints live in the control interface, there's no data interface. |
| 72 | * And it's not used to talk to a cell phone radio. |
| 73 | */ |
| 74 | |
| 75 | /* interface descriptor: */ |
| 76 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 77 | static struct usb_interface_descriptor subset_data_intf = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 78 | .bLength = sizeof subset_data_intf, |
| 79 | .bDescriptorType = USB_DT_INTERFACE, |
| 80 | |
| 81 | /* .bInterfaceNumber = DYNAMIC */ |
| 82 | .bAlternateSetting = 0, |
| 83 | .bNumEndpoints = 2, |
| 84 | .bInterfaceClass = USB_CLASS_COMM, |
| 85 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, |
| 86 | .bInterfaceProtocol = 0, |
| 87 | /* .iInterface = DYNAMIC */ |
| 88 | }; |
| 89 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 90 | static struct usb_cdc_header_desc mdlm_header_desc = { |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 91 | .bLength = sizeof mdlm_header_desc, |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 92 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 93 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 94 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 95 | .bcdCDC = cpu_to_le16(0x0110), |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 98 | static struct usb_cdc_mdlm_desc mdlm_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 99 | .bLength = sizeof mdlm_desc, |
| 100 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 101 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, |
| 102 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 103 | .bcdVersion = cpu_to_le16(0x0100), |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 104 | .bGUID = { |
| 105 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, |
| 106 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we |
| 111 | * can't really use its struct. All we do here is say that we're using |
| 112 | * the submode of "SAFE" which directly matches the CDC Subset. |
| 113 | */ |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 114 | static u8 mdlm_detail_desc[] = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 115 | 6, |
| 116 | USB_DT_CS_INTERFACE, |
| 117 | USB_CDC_MDLM_DETAIL_TYPE, |
| 118 | |
| 119 | 0, /* "SAFE" */ |
| 120 | 0, /* network control capabilities (none) */ |
| 121 | 0, /* network data capabilities ("raw" encapsulation) */ |
| 122 | }; |
| 123 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 124 | static struct usb_cdc_ether_desc ether_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 125 | .bLength = sizeof ether_desc, |
| 126 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 127 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, |
| 128 | |
| 129 | /* this descriptor actually adds value, surprise! */ |
| 130 | /* .iMACAddress = DYNAMIC */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 131 | .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */ |
| 132 | .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN), |
| 133 | .wNumberMCFilters = cpu_to_le16(0), |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 134 | .bNumberPowerFilters = 0, |
| 135 | }; |
| 136 | |
| 137 | /* full speed support: */ |
| 138 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 139 | static struct usb_endpoint_descriptor fs_subset_in_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 140 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 141 | .bDescriptorType = USB_DT_ENDPOINT, |
| 142 | |
| 143 | .bEndpointAddress = USB_DIR_IN, |
| 144 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 145 | }; |
| 146 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 147 | static struct usb_endpoint_descriptor fs_subset_out_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 148 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 149 | .bDescriptorType = USB_DT_ENDPOINT, |
| 150 | |
| 151 | .bEndpointAddress = USB_DIR_OUT, |
| 152 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 153 | }; |
| 154 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 155 | static struct usb_descriptor_header *fs_eth_function[] = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 156 | (struct usb_descriptor_header *) &subset_data_intf, |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 157 | (struct usb_descriptor_header *) &mdlm_header_desc, |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 158 | (struct usb_descriptor_header *) &mdlm_desc, |
| 159 | (struct usb_descriptor_header *) &mdlm_detail_desc, |
| 160 | (struct usb_descriptor_header *) ðer_desc, |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 161 | (struct usb_descriptor_header *) &fs_subset_in_desc, |
| 162 | (struct usb_descriptor_header *) &fs_subset_out_desc, |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 163 | NULL, |
| 164 | }; |
| 165 | |
| 166 | /* high speed support: */ |
| 167 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 168 | static struct usb_endpoint_descriptor hs_subset_in_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 169 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 170 | .bDescriptorType = USB_DT_ENDPOINT, |
| 171 | |
| 172 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 173 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 176 | static struct usb_endpoint_descriptor hs_subset_out_desc = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 177 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 178 | .bDescriptorType = USB_DT_ENDPOINT, |
| 179 | |
| 180 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 181 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 184 | static struct usb_descriptor_header *hs_eth_function[] = { |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 185 | (struct usb_descriptor_header *) &subset_data_intf, |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 186 | (struct usb_descriptor_header *) &mdlm_header_desc, |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 187 | (struct usb_descriptor_header *) &mdlm_desc, |
| 188 | (struct usb_descriptor_header *) &mdlm_detail_desc, |
| 189 | (struct usb_descriptor_header *) ðer_desc, |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 190 | (struct usb_descriptor_header *) &hs_subset_in_desc, |
| 191 | (struct usb_descriptor_header *) &hs_subset_out_desc, |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 192 | NULL, |
| 193 | }; |
| 194 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 195 | /* super speed support: */ |
| 196 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 197 | static struct usb_endpoint_descriptor ss_subset_in_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 198 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 199 | .bDescriptorType = USB_DT_ENDPOINT, |
| 200 | |
| 201 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 202 | .wMaxPacketSize = cpu_to_le16(1024), |
| 203 | }; |
| 204 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 205 | static struct usb_endpoint_descriptor ss_subset_out_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 206 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 207 | .bDescriptorType = USB_DT_ENDPOINT, |
| 208 | |
| 209 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 210 | .wMaxPacketSize = cpu_to_le16(1024), |
| 211 | }; |
| 212 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 213 | static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 214 | .bLength = sizeof ss_subset_bulk_comp_desc, |
| 215 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 216 | |
| 217 | /* the following 2 values can be tweaked if necessary */ |
| 218 | /* .bMaxBurst = 0, */ |
| 219 | /* .bmAttributes = 0, */ |
| 220 | }; |
| 221 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 222 | static struct usb_descriptor_header *ss_eth_function[] = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 223 | (struct usb_descriptor_header *) &subset_data_intf, |
| 224 | (struct usb_descriptor_header *) &mdlm_header_desc, |
| 225 | (struct usb_descriptor_header *) &mdlm_desc, |
| 226 | (struct usb_descriptor_header *) &mdlm_detail_desc, |
| 227 | (struct usb_descriptor_header *) ðer_desc, |
| 228 | (struct usb_descriptor_header *) &ss_subset_in_desc, |
| 229 | (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc, |
| 230 | (struct usb_descriptor_header *) &ss_subset_out_desc, |
| 231 | (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc, |
| 232 | NULL, |
| 233 | }; |
| 234 | |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 235 | /* string descriptors: */ |
| 236 | |
| 237 | static struct usb_string geth_string_defs[] = { |
| 238 | [0].s = "CDC Ethernet Subset/SAFE", |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 239 | [1].s = "", |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 240 | { } /* end of list */ |
| 241 | }; |
| 242 | |
| 243 | static struct usb_gadget_strings geth_string_table = { |
| 244 | .language = 0x0409, /* en-us */ |
| 245 | .strings = geth_string_defs, |
| 246 | }; |
| 247 | |
| 248 | static struct usb_gadget_strings *geth_strings[] = { |
| 249 | &geth_string_table, |
| 250 | NULL, |
| 251 | }; |
| 252 | |
| 253 | /*-------------------------------------------------------------------------*/ |
| 254 | |
| 255 | static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 256 | { |
| 257 | struct f_gether *geth = func_to_geth(f); |
| 258 | struct usb_composite_dev *cdev = f->config->cdev; |
| 259 | struct net_device *net; |
| 260 | |
| 261 | /* we know alt == 0, so this is an activation or a reset */ |
| 262 | |
| 263 | if (geth->port.in_ep->driver_data) { |
| 264 | DBG(cdev, "reset cdc subset\n"); |
| 265 | gether_disconnect(&geth->port); |
| 266 | } |
| 267 | |
| 268 | DBG(cdev, "init + activate cdc subset\n"); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 269 | if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) || |
| 270 | config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) { |
| 271 | geth->port.in_ep->desc = NULL; |
| 272 | geth->port.out_ep->desc = NULL; |
| 273 | return -EINVAL; |
| 274 | } |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 275 | |
| 276 | net = gether_connect(&geth->port); |
| 277 | return IS_ERR(net) ? PTR_ERR(net) : 0; |
| 278 | } |
| 279 | |
| 280 | static void geth_disable(struct usb_function *f) |
| 281 | { |
| 282 | struct f_gether *geth = func_to_geth(f); |
| 283 | struct usb_composite_dev *cdev = f->config->cdev; |
| 284 | |
| 285 | DBG(cdev, "net deactivated\n"); |
| 286 | gether_disconnect(&geth->port); |
| 287 | } |
| 288 | |
| 289 | /*-------------------------------------------------------------------------*/ |
| 290 | |
| 291 | /* serial function driver setup/binding */ |
| 292 | |
Lothar Waßmann | 8d06984 | 2012-03-11 15:08:46 +0100 | [diff] [blame] | 293 | static int |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 294 | geth_bind(struct usb_configuration *c, struct usb_function *f) |
| 295 | { |
| 296 | struct usb_composite_dev *cdev = c->cdev; |
| 297 | struct f_gether *geth = func_to_geth(f); |
| 298 | int status; |
| 299 | struct usb_ep *ep; |
| 300 | |
| 301 | /* allocate instance-specific interface IDs */ |
| 302 | status = usb_interface_id(c, f); |
| 303 | if (status < 0) |
| 304 | goto fail; |
| 305 | subset_data_intf.bInterfaceNumber = status; |
| 306 | |
| 307 | status = -ENODEV; |
| 308 | |
| 309 | /* allocate instance-specific endpoints */ |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 310 | ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 311 | if (!ep) |
| 312 | goto fail; |
| 313 | geth->port.in_ep = ep; |
| 314 | ep->driver_data = cdev; /* claim */ |
| 315 | |
David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 316 | ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 317 | if (!ep) |
| 318 | goto fail; |
| 319 | geth->port.out_ep = ep; |
| 320 | ep->driver_data = cdev; /* claim */ |
| 321 | |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 322 | /* support all relevant hardware speeds... we expect that when |
| 323 | * hardware is dual speed, all bulk-capable endpoints work at |
| 324 | * both speeds |
| 325 | */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 326 | hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress; |
| 327 | hs_subset_out_desc.bEndpointAddress = |
| 328 | fs_subset_out_desc.bEndpointAddress; |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 329 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 330 | ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress; |
| 331 | ss_subset_out_desc.bEndpointAddress = |
| 332 | fs_subset_out_desc.bEndpointAddress; |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 333 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 334 | status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function, |
| 335 | ss_eth_function); |
| 336 | if (status) |
| 337 | goto fail; |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 338 | |
| 339 | /* NOTE: all that is done without knowing or caring about |
| 340 | * the network link ... which is unavailable to this code |
| 341 | * until we're activated via set_alt(). |
| 342 | */ |
| 343 | |
| 344 | DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n", |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 345 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 346 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 347 | geth->port.in_ep->name, geth->port.out_ep->name); |
| 348 | return 0; |
| 349 | |
| 350 | fail: |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 351 | usb_free_all_descriptors(f); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 352 | /* we might as well release our claims on endpoints */ |
Sebastian Andrzej Siewior | e79cc61 | 2012-10-22 22:15:00 +0200 | [diff] [blame] | 353 | if (geth->port.out_ep) |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 354 | geth->port.out_ep->driver_data = NULL; |
Sebastian Andrzej Siewior | e79cc61 | 2012-10-22 22:15:00 +0200 | [diff] [blame] | 355 | if (geth->port.in_ep) |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 356 | geth->port.in_ep->driver_data = NULL; |
| 357 | |
| 358 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 359 | |
| 360 | return status; |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | geth_unbind(struct usb_configuration *c, struct usb_function *f) |
| 365 | { |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 366 | geth_string_defs[0].id = 0; |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 367 | usb_free_all_descriptors(f); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 368 | kfree(func_to_geth(f)); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * geth_bind_config - add CDC Subset network link to a configuration |
| 373 | * @c: the configuration to support the network link |
| 374 | * @ethaddr: a buffer in which the ethernet address of the host side |
| 375 | * side of the link was recorded |
| 376 | * Context: single threaded during gadget setup |
| 377 | * |
| 378 | * Returns zero on success, else negative errno. |
| 379 | * |
| 380 | * Caller must have called @gether_setup(). Caller is also responsible |
| 381 | * for calling @gether_cleanup() before module unload. |
| 382 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame^] | 383 | int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 384 | struct eth_dev *dev) |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 385 | { |
| 386 | struct f_gether *geth; |
| 387 | int status; |
| 388 | |
| 389 | if (!ethaddr) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | /* maybe allocate device-global string IDs */ |
| 393 | if (geth_string_defs[0].id == 0) { |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 394 | status = usb_string_ids_tab(c->cdev, geth_string_defs); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 395 | if (status < 0) |
| 396 | return status; |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 397 | subset_data_intf.iInterface = geth_string_defs[0].id; |
| 398 | ether_desc.iMACAddress = geth_string_defs[1].id; |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* allocate and initialize one new instance */ |
| 402 | geth = kzalloc(sizeof *geth, GFP_KERNEL); |
| 403 | if (!geth) |
| 404 | return -ENOMEM; |
| 405 | |
| 406 | /* export host's Ethernet address in CDC format */ |
Andy Shevchenko | 8c7ca99 | 2012-07-06 18:30:21 +0300 | [diff] [blame] | 407 | snprintf(geth->ethaddr, sizeof geth->ethaddr, "%pm", ethaddr); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 408 | geth_string_defs[1].s = geth->ethaddr; |
| 409 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame^] | 410 | geth->port.ioport = dev; |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 411 | geth->port.cdc_filter = DEFAULT_FILTER; |
| 412 | |
| 413 | geth->port.func.name = "cdc_subset"; |
| 414 | geth->port.func.strings = geth_strings; |
| 415 | geth->port.func.bind = geth_bind; |
| 416 | geth->port.func.unbind = geth_unbind; |
| 417 | geth->port.func.set_alt = geth_set_alt; |
| 418 | geth->port.func.disable = geth_disable; |
| 419 | |
| 420 | status = usb_add_function(c, &geth->port.func); |
Sebastian Andrzej Siewior | 1616e99d | 2012-10-22 22:15:10 +0200 | [diff] [blame] | 421 | if (status) |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 422 | kfree(geth); |
David Brownell | 8a40819 | 2008-06-19 18:19:32 -0700 | [diff] [blame] | 423 | return status; |
| 424 | } |