2  * f_subset.c -- "CDC Subset" Ethernet link function driver
 
   4  * Copyright (C) 2003-2005,2008 David Brownell
 
   5  * Copyright (C) 2008 Nokia Corporation
 
   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.
 
  12  * This program is distributed in the hope that it will be useful,
 
  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15  * GNU General Public License for more details.
 
  17  * You should have received a copy of the GNU General Public License
 
  18  * along with this program; if not, write to the Free Software
 
  19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  22 #include <linux/kernel.h>
 
  23 #include <linux/device.h>
 
  24 #include <linux/etherdevice.h>
 
  30  * This function packages a simple "CDC Subset" Ethernet port with no real
 
  31  * control mechanisms; just raw data transfer over two bulk endpoints.
 
  32  * The data transfer model is exactly that of CDC Ethernet, which is
 
  33  * why we call it the "CDC Subset".
 
  35  * Because it's not standardized, this has some interoperability issues.
 
  36  * They mostly relate to driver binding, since the data transfer model is
 
  37  * so simple (CDC Ethernet).  The original versions of this protocol used
 
  38  * specific product/vendor IDs:  byteswapped IDs for Digital Equipment's
 
  39  * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
 
  40  * daughtercards with USB peripheral connectors.  (It was used more often
 
  41  * with other boards, using the Itsy identifiers.)  Linux hosts recognized
 
  42  * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
 
  45  * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
 
  46  * "SAFE", which happens to have a mode which is identical to the "CDC
 
  47  * Subset" in terms of data transfer and lack of control model.  This was
 
  48  * adopted by later Sharp Zaurus models, and by some other software which
 
  49  * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
 
  51  * Because Microsoft's RNDIS drivers are far from robust, we added a few
 
  52  * descriptors to the CDC Subset code, making this code look like a SAFE
 
  53  * implementation.  This lets you use MCCI's host side MS-Windows drivers
 
  54  * if you get fed up with RNDIS.  It also makes it easier for composite
 
  55  * drivers to work, since they can use class based binding instead of
 
  56  * caring about specific product and vendor IDs.
 
  60         struct usb_endpoint_descriptor  *in;
 
  61         struct usb_endpoint_descriptor  *out;
 
  73 static inline struct f_gether *func_to_geth(struct usb_function *f)
 
  75         return container_of(f, struct f_gether, port.func);
 
  78 /*-------------------------------------------------------------------------*/
 
  81  * "Simple" CDC-subset option is a simple vendor-neutral model that most
 
  82  * full speed controllers can handle:  one interface, two bulk endpoints.
 
  83  * To assist host side drivers, we fancy it up a bit, and add descriptors so
 
  84  * some host side drivers will understand it as a "SAFE" variant.
 
  86  * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
 
  87  * Data endpoints live in the control interface, there's no data interface.
 
  88  * And it's not used to talk to a cell phone radio.
 
  91 /* interface descriptor: */
 
  93 static struct usb_interface_descriptor subset_data_intf __initdata = {
 
  94         .bLength =              sizeof subset_data_intf,
 
  95         .bDescriptorType =      USB_DT_INTERFACE,
 
  97         /* .bInterfaceNumber = DYNAMIC */
 
  98         .bAlternateSetting =    0,
 
 100         .bInterfaceClass =      USB_CLASS_COMM,
 
 101         .bInterfaceSubClass =   USB_CDC_SUBCLASS_MDLM,
 
 102         .bInterfaceProtocol =   0,
 
 103         /* .iInterface = DYNAMIC */
 
 106 static struct usb_cdc_header_desc header_desc __initdata = {
 
 107         .bLength =              sizeof header_desc,
 
 108         .bDescriptorType =      USB_DT_CS_INTERFACE,
 
 109         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
 
 111         .bcdCDC =               __constant_cpu_to_le16(0x0110),
 
 114 static struct usb_cdc_mdlm_desc mdlm_desc __initdata = {
 
 115         .bLength =              sizeof mdlm_desc,
 
 116         .bDescriptorType =      USB_DT_CS_INTERFACE,
 
 117         .bDescriptorSubType =   USB_CDC_MDLM_TYPE,
 
 119         .bcdVersion =           __constant_cpu_to_le16(0x0100),
 
 121                 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
 
 122                 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
 
 126 /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
 
 127  * can't really use its struct.  All we do here is say that we're using
 
 128  * the submode of "SAFE" which directly matches the CDC Subset.
 
 130 static u8 mdlm_detail_desc[] __initdata = {
 
 133         USB_CDC_MDLM_DETAIL_TYPE,
 
 136         0,      /* network control capabilities (none) */
 
 137         0,      /* network data capabilities ("raw" encapsulation) */
 
 140 static struct usb_cdc_ether_desc ether_desc __initdata = {
 
 141         .bLength =              sizeof ether_desc,
 
 142         .bDescriptorType =      USB_DT_CS_INTERFACE,
 
 143         .bDescriptorSubType =   USB_CDC_ETHERNET_TYPE,
 
 145         /* this descriptor actually adds value, surprise! */
 
 146         /* .iMACAddress = DYNAMIC */
 
 147         .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
 
 148         .wMaxSegmentSize =      __constant_cpu_to_le16(ETH_FRAME_LEN),
 
 149         .wNumberMCFilters =     __constant_cpu_to_le16(0),
 
 150         .bNumberPowerFilters =  0,
 
 153 /* full speed support: */
 
 155 static struct usb_endpoint_descriptor fs_in_desc __initdata = {
 
 156         .bLength =              USB_DT_ENDPOINT_SIZE,
 
 157         .bDescriptorType =      USB_DT_ENDPOINT,
 
 159         .bEndpointAddress =     USB_DIR_IN,
 
 160         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 
 163 static struct usb_endpoint_descriptor fs_out_desc __initdata = {
 
 164         .bLength =              USB_DT_ENDPOINT_SIZE,
 
 165         .bDescriptorType =      USB_DT_ENDPOINT,
 
 167         .bEndpointAddress =     USB_DIR_OUT,
 
 168         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 
 171 static struct usb_descriptor_header *fs_eth_function[] __initdata = {
 
 172         (struct usb_descriptor_header *) &subset_data_intf,
 
 173         (struct usb_descriptor_header *) &header_desc,
 
 174         (struct usb_descriptor_header *) &mdlm_desc,
 
 175         (struct usb_descriptor_header *) &mdlm_detail_desc,
 
 176         (struct usb_descriptor_header *) ðer_desc,
 
 177         (struct usb_descriptor_header *) &fs_in_desc,
 
 178         (struct usb_descriptor_header *) &fs_out_desc,
 
 182 /* high speed support: */
 
 184 static struct usb_endpoint_descriptor hs_in_desc __initdata = {
 
 185         .bLength =              USB_DT_ENDPOINT_SIZE,
 
 186         .bDescriptorType =      USB_DT_ENDPOINT,
 
 188         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 
 189         .wMaxPacketSize =       __constant_cpu_to_le16(512),
 
 192 static struct usb_endpoint_descriptor hs_out_desc __initdata = {
 
 193         .bLength =              USB_DT_ENDPOINT_SIZE,
 
 194         .bDescriptorType =      USB_DT_ENDPOINT,
 
 196         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 
 197         .wMaxPacketSize =       __constant_cpu_to_le16(512),
 
 200 static struct usb_descriptor_header *hs_eth_function[] __initdata = {
 
 201         (struct usb_descriptor_header *) &subset_data_intf,
 
 202         (struct usb_descriptor_header *) &header_desc,
 
 203         (struct usb_descriptor_header *) &mdlm_desc,
 
 204         (struct usb_descriptor_header *) &mdlm_detail_desc,
 
 205         (struct usb_descriptor_header *) ðer_desc,
 
 206         (struct usb_descriptor_header *) &hs_in_desc,
 
 207         (struct usb_descriptor_header *) &hs_out_desc,
 
 211 /* string descriptors: */
 
 213 static struct usb_string geth_string_defs[] = {
 
 214         [0].s = "CDC Ethernet Subset/SAFE",
 
 215         [1].s = NULL /* DYNAMIC */,
 
 216         {  } /* end of list */
 
 219 static struct usb_gadget_strings geth_string_table = {
 
 220         .language =             0x0409, /* en-us */
 
 221         .strings =              geth_string_defs,
 
 224 static struct usb_gadget_strings *geth_strings[] = {
 
 229 /*-------------------------------------------------------------------------*/
 
 231 static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 
 233         struct f_gether         *geth = func_to_geth(f);
 
 234         struct usb_composite_dev *cdev = f->config->cdev;
 
 235         struct net_device       *net;
 
 237         /* we know alt == 0, so this is an activation or a reset */
 
 239         if (geth->port.in_ep->driver_data) {
 
 240                 DBG(cdev, "reset cdc subset\n");
 
 241                 gether_disconnect(&geth->port);
 
 244         DBG(cdev, "init + activate cdc subset\n");
 
 245         geth->port.in = ep_choose(cdev->gadget,
 
 246                         geth->hs.in, geth->fs.in);
 
 247         geth->port.out = ep_choose(cdev->gadget,
 
 248                         geth->hs.out, geth->fs.out);
 
 250         net = gether_connect(&geth->port);
 
 251         return IS_ERR(net) ? PTR_ERR(net) : 0;
 
 254 static void geth_disable(struct usb_function *f)
 
 256         struct f_gether *geth = func_to_geth(f);
 
 257         struct usb_composite_dev *cdev = f->config->cdev;
 
 259         DBG(cdev, "net deactivated\n");
 
 260         gether_disconnect(&geth->port);
 
 263 /*-------------------------------------------------------------------------*/
 
 265 /* serial function driver setup/binding */
 
 268 geth_bind(struct usb_configuration *c, struct usb_function *f)
 
 270         struct usb_composite_dev *cdev = c->cdev;
 
 271         struct f_gether         *geth = func_to_geth(f);
 
 275         /* allocate instance-specific interface IDs */
 
 276         status = usb_interface_id(c, f);
 
 279         subset_data_intf.bInterfaceNumber = status;
 
 283         /* allocate instance-specific endpoints */
 
 284         ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
 
 287         geth->port.in_ep = ep;
 
 288         ep->driver_data = cdev; /* claim */
 
 290         ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
 
 293         geth->port.out_ep = ep;
 
 294         ep->driver_data = cdev; /* claim */
 
 296         /* copy descriptors, and track endpoint copies */
 
 297         f->descriptors = usb_copy_descriptors(fs_eth_function);
 
 299         geth->fs.in = usb_find_endpoint(fs_eth_function,
 
 300                         f->descriptors, &fs_in_desc);
 
 301         geth->fs.out = usb_find_endpoint(fs_eth_function,
 
 302                         f->descriptors, &fs_out_desc);
 
 305         /* support all relevant hardware speeds... we expect that when
 
 306          * hardware is dual speed, all bulk-capable endpoints work at
 
 309         if (gadget_is_dualspeed(c->cdev->gadget)) {
 
 310                 hs_in_desc.bEndpointAddress =
 
 311                                 fs_in_desc.bEndpointAddress;
 
 312                 hs_out_desc.bEndpointAddress =
 
 313                                 fs_out_desc.bEndpointAddress;
 
 315                 /* copy descriptors, and track endpoint copies */
 
 316                 f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
 
 318                 geth->hs.in = usb_find_endpoint(hs_eth_function,
 
 319                                 f->hs_descriptors, &hs_in_desc);
 
 320                 geth->hs.out = usb_find_endpoint(hs_eth_function,
 
 321                                 f->hs_descriptors, &hs_out_desc);
 
 324         /* NOTE:  all that is done without knowing or caring about
 
 325          * the network link ... which is unavailable to this code
 
 326          * until we're activated via set_alt().
 
 329         DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
 
 330                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
 
 331                         geth->port.in_ep->name, geth->port.out_ep->name);
 
 335         /* we might as well release our claims on endpoints */
 
 337                 geth->port.out_ep->driver_data = NULL;
 
 339                 geth->port.in_ep->driver_data = NULL;
 
 341         ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
 
 347 geth_unbind(struct usb_configuration *c, struct usb_function *f)
 
 349         if (gadget_is_dualspeed(c->cdev->gadget))
 
 350                 usb_free_descriptors(f->hs_descriptors);
 
 351         usb_free_descriptors(f->descriptors);
 
 352         geth_string_defs[1].s = NULL;
 
 353         kfree(func_to_geth(f));
 
 357  * geth_bind_config - add CDC Subset network link to a configuration
 
 358  * @c: the configuration to support the network link
 
 359  * @ethaddr: a buffer in which the ethernet address of the host side
 
 360  *      side of the link was recorded
 
 361  * Context: single threaded during gadget setup
 
 363  * Returns zero on success, else negative errno.
 
 365  * Caller must have called @gether_setup().  Caller is also responsible
 
 366  * for calling @gether_cleanup() before module unload.
 
 368 int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
 
 370         struct f_gether *geth;
 
 376         /* maybe allocate device-global string IDs */
 
 377         if (geth_string_defs[0].id == 0) {
 
 379                 /* interface label */
 
 380                 status = usb_string_id(c->cdev);
 
 383                 geth_string_defs[0].id = status;
 
 384                 subset_data_intf.iInterface = status;
 
 387                 status = usb_string_id(c->cdev);
 
 390                 geth_string_defs[1].id = status;
 
 391                 ether_desc.iMACAddress = status;
 
 394         /* allocate and initialize one new instance */
 
 395         geth = kzalloc(sizeof *geth, GFP_KERNEL);
 
 399         /* export host's Ethernet address in CDC format */
 
 400         snprintf(geth->ethaddr, sizeof geth->ethaddr,
 
 401                 "%02X%02X%02X%02X%02X%02X",
 
 402                 ethaddr[0], ethaddr[1], ethaddr[2],
 
 403                 ethaddr[3], ethaddr[4], ethaddr[5]);
 
 404         geth_string_defs[1].s = geth->ethaddr;
 
 406         geth->port.cdc_filter = DEFAULT_FILTER;
 
 408         geth->port.func.name = "cdc_subset";
 
 409         geth->port.func.strings = geth_strings;
 
 410         geth->port.func.bind = geth_bind;
 
 411         geth->port.func.unbind = geth_unbind;
 
 412         geth->port.func.set_alt = geth_set_alt;
 
 413         geth->port.func.disable = geth_disable;
 
 415         status = usb_add_function(c, &geth->port.func);
 
 417                 geth_string_defs[1].s = NULL;