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