David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cdc2.c -- CDC Composite driver, with ECM and ACM support |
| 3 | * |
| 4 | * Copyright (C) 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 | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 15 | |
| 16 | #include "u_ether.h" |
| 17 | #include "u_serial.h" |
| 18 | |
| 19 | |
| 20 | #define DRIVER_DESC "CDC Composite Gadget" |
| 21 | #define DRIVER_VERSION "King Kamehameha Day 2008" |
| 22 | |
| 23 | /*-------------------------------------------------------------------------*/ |
| 24 | |
| 25 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 26 | * Instead: allocate your own, using normal USB-IF procedures. |
| 27 | */ |
| 28 | |
| 29 | /* Thanks to NetChip Technologies for donating this product ID. |
| 30 | * It's for devices with only this composite CDC configuration. |
| 31 | */ |
| 32 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 33 | #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */ |
| 34 | |
| 35 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 36 | USB_GADGET_COMPOSITE_OPTIONS(); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 37 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame^] | 38 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 39 | |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Kbuild is not very cooperative with respect to linking separately |
| 42 | * compiled library objects into one module. So for now we won't use |
| 43 | * separate compilation ... ensuring init/exit sections work to shrink |
| 44 | * the runtime footprint, and giving us at least some parts of what |
| 45 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 46 | */ |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 47 | #include "f_ecm.c" |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 48 | |
| 49 | /*-------------------------------------------------------------------------*/ |
| 50 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 51 | static struct usb_device_descriptor device_desc = { |
| 52 | .bLength = sizeof device_desc, |
| 53 | .bDescriptorType = USB_DT_DEVICE, |
| 54 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 55 | .bcdUSB = cpu_to_le16(0x0200), |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 56 | |
| 57 | .bDeviceClass = USB_CLASS_COMM, |
| 58 | .bDeviceSubClass = 0, |
| 59 | .bDeviceProtocol = 0, |
| 60 | /* .bMaxPacketSize0 = f(hardware) */ |
| 61 | |
| 62 | /* Vendor and product id can be overridden by module parameters. */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 63 | .idVendor = cpu_to_le16(CDC_VENDOR_NUM), |
| 64 | .idProduct = cpu_to_le16(CDC_PRODUCT_NUM), |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 65 | /* .bcdDevice = f(hardware) */ |
| 66 | /* .iManufacturer = DYNAMIC */ |
| 67 | /* .iProduct = DYNAMIC */ |
| 68 | /* NO SERIAL NUMBER */ |
| 69 | .bNumConfigurations = 1, |
| 70 | }; |
| 71 | |
| 72 | static struct usb_otg_descriptor otg_descriptor = { |
| 73 | .bLength = sizeof otg_descriptor, |
| 74 | .bDescriptorType = USB_DT_OTG, |
| 75 | |
| 76 | /* REVISIT SRP-only hardware is possible, although |
| 77 | * it would not be called "OTG" ... |
| 78 | */ |
| 79 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 80 | }; |
| 81 | |
| 82 | static const struct usb_descriptor_header *otg_desc[] = { |
| 83 | (struct usb_descriptor_header *) &otg_descriptor, |
| 84 | NULL, |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | /* string IDs are assigned dynamically */ |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 89 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 90 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 91 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 92 | [USB_GADGET_SERIAL_IDX].s = "", |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 93 | { } /* end of list */ |
| 94 | }; |
| 95 | |
| 96 | static struct usb_gadget_strings stringtab_dev = { |
| 97 | .language = 0x0409, /* en-us */ |
| 98 | .strings = strings_dev, |
| 99 | }; |
| 100 | |
| 101 | static struct usb_gadget_strings *dev_strings[] = { |
| 102 | &stringtab_dev, |
| 103 | NULL, |
| 104 | }; |
| 105 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame^] | 106 | static u8 host_mac[ETH_ALEN]; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 107 | static struct eth_dev *the_dev; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 108 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 109 | static struct usb_function *f_acm; |
| 110 | static struct usb_function_instance *fi_serial; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * We _always_ have both CDC ECM and CDC ACM functions. |
| 114 | */ |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 115 | static int __init cdc_do_config(struct usb_configuration *c) |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 116 | { |
| 117 | int status; |
| 118 | |
| 119 | if (gadget_is_otg(c->cdev->gadget)) { |
| 120 | c->descriptors = otg_desc; |
| 121 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 122 | } |
| 123 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame^] | 124 | status = ecm_bind_config(c, host_mac, the_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 125 | if (status < 0) |
| 126 | return status; |
| 127 | |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 128 | fi_serial = usb_get_function_instance("acm"); |
| 129 | if (IS_ERR(fi_serial)) |
| 130 | return PTR_ERR(fi_serial); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 131 | |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 132 | f_acm = usb_get_function(fi_serial); |
Wei Yongjun | 8d6f51b | 2013-04-06 17:25:21 +0800 | [diff] [blame] | 133 | if (IS_ERR(f_acm)) { |
| 134 | status = PTR_ERR(f_acm); |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 135 | goto err_func_acm; |
Wei Yongjun | 8d6f51b | 2013-04-06 17:25:21 +0800 | [diff] [blame] | 136 | } |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 137 | |
| 138 | status = usb_add_function(c, f_acm); |
| 139 | if (status) |
| 140 | goto err_conf; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 141 | return 0; |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 142 | err_conf: |
| 143 | usb_put_function(f_acm); |
| 144 | err_func_acm: |
| 145 | usb_put_function_instance(fi_serial); |
| 146 | return status; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static struct usb_configuration cdc_config_driver = { |
| 150 | .label = "CDC Composite (ECM + ACM)", |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 151 | .bConfigurationValue = 1, |
| 152 | /* .iConfiguration = DYNAMIC */ |
| 153 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /*-------------------------------------------------------------------------*/ |
| 157 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 158 | static int __init cdc_bind(struct usb_composite_dev *cdev) |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 159 | { |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 160 | struct usb_gadget *gadget = cdev->gadget; |
| 161 | int status; |
| 162 | |
| 163 | if (!can_support_ecm(cdev->gadget)) { |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 164 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
| 165 | gadget->name); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | /* set up network link layer */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame^] | 170 | the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac, |
| 171 | qmult); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 172 | if (IS_ERR(the_dev)) |
| 173 | return PTR_ERR(the_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 174 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 175 | /* Allocate string descriptor numbers ... note that string |
| 176 | * contents can be overridden by the composite_dev glue. |
| 177 | */ |
| 178 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 179 | status = usb_string_ids_tab(cdev, strings_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 180 | if (status < 0) |
| 181 | goto fail1; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 182 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 183 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 184 | |
| 185 | /* register our configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 186 | status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 187 | if (status < 0) |
| 188 | goto fail1; |
| 189 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 190 | usb_composite_overwrite_options(cdev, &coverwrite); |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 191 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
| 192 | DRIVER_DESC); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | |
| 196 | fail1: |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 197 | gether_cleanup(the_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 198 | return status; |
| 199 | } |
| 200 | |
| 201 | static int __exit cdc_unbind(struct usb_composite_dev *cdev) |
| 202 | { |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 203 | usb_put_function(f_acm); |
| 204 | usb_put_function_instance(fi_serial); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 205 | gether_cleanup(the_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 209 | static __refdata struct usb_composite_driver cdc_driver = { |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 210 | .name = "g_cdc", |
| 211 | .dev = &device_desc, |
| 212 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 213 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 214 | .bind = cdc_bind, |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 215 | .unbind = __exit_p(cdc_unbind), |
| 216 | }; |
| 217 | |
| 218 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 219 | MODULE_AUTHOR("David Brownell"); |
| 220 | MODULE_LICENSE("GPL"); |
| 221 | |
| 222 | static int __init init(void) |
| 223 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 224 | return usb_composite_probe(&cdc_driver); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 225 | } |
| 226 | module_init(init); |
| 227 | |
| 228 | static void __exit cleanup(void) |
| 229 | { |
| 230 | usb_composite_unregister(&cdc_driver); |
| 231 | } |
| 232 | module_exit(cleanup); |