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" |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 18 | #include "u_ecm.h" |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | #define DRIVER_DESC "CDC Composite Gadget" |
| 22 | #define DRIVER_VERSION "King Kamehameha Day 2008" |
| 23 | |
| 24 | /*-------------------------------------------------------------------------*/ |
| 25 | |
| 26 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 27 | * Instead: allocate your own, using normal USB-IF procedures. |
| 28 | */ |
| 29 | |
| 30 | /* Thanks to NetChip Technologies for donating this product ID. |
| 31 | * It's for devices with only this composite CDC configuration. |
| 32 | */ |
| 33 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 34 | #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */ |
| 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 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 42 | static struct usb_device_descriptor device_desc = { |
| 43 | .bLength = sizeof device_desc, |
| 44 | .bDescriptorType = USB_DT_DEVICE, |
| 45 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 46 | .bcdUSB = cpu_to_le16(0x0200), |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 47 | |
| 48 | .bDeviceClass = USB_CLASS_COMM, |
| 49 | .bDeviceSubClass = 0, |
| 50 | .bDeviceProtocol = 0, |
| 51 | /* .bMaxPacketSize0 = f(hardware) */ |
| 52 | |
| 53 | /* Vendor and product id can be overridden by module parameters. */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 54 | .idVendor = cpu_to_le16(CDC_VENDOR_NUM), |
| 55 | .idProduct = cpu_to_le16(CDC_PRODUCT_NUM), |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 56 | /* .bcdDevice = f(hardware) */ |
| 57 | /* .iManufacturer = DYNAMIC */ |
| 58 | /* .iProduct = DYNAMIC */ |
| 59 | /* NO SERIAL NUMBER */ |
| 60 | .bNumConfigurations = 1, |
| 61 | }; |
| 62 | |
| 63 | static struct usb_otg_descriptor otg_descriptor = { |
| 64 | .bLength = sizeof otg_descriptor, |
| 65 | .bDescriptorType = USB_DT_OTG, |
| 66 | |
| 67 | /* REVISIT SRP-only hardware is possible, although |
| 68 | * it would not be called "OTG" ... |
| 69 | */ |
| 70 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 71 | }; |
| 72 | |
| 73 | static const struct usb_descriptor_header *otg_desc[] = { |
| 74 | (struct usb_descriptor_header *) &otg_descriptor, |
| 75 | NULL, |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | /* string IDs are assigned dynamically */ |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 80 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 81 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 82 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 83 | [USB_GADGET_SERIAL_IDX].s = "", |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 84 | { } /* end of list */ |
| 85 | }; |
| 86 | |
| 87 | static struct usb_gadget_strings stringtab_dev = { |
| 88 | .language = 0x0409, /* en-us */ |
| 89 | .strings = strings_dev, |
| 90 | }; |
| 91 | |
| 92 | static struct usb_gadget_strings *dev_strings[] = { |
| 93 | &stringtab_dev, |
| 94 | NULL, |
| 95 | }; |
| 96 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 97 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 98 | static struct usb_function *f_acm; |
| 99 | static struct usb_function_instance *fi_serial; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 100 | |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 101 | static struct usb_function *f_ecm; |
| 102 | static struct usb_function_instance *fi_ecm; |
| 103 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 104 | /* |
| 105 | * We _always_ have both CDC ECM and CDC ACM functions. |
| 106 | */ |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 107 | static int cdc_do_config(struct usb_configuration *c) |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 108 | { |
| 109 | int status; |
| 110 | |
| 111 | if (gadget_is_otg(c->cdev->gadget)) { |
| 112 | c->descriptors = otg_desc; |
| 113 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 114 | } |
| 115 | |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 116 | f_ecm = usb_get_function(fi_ecm); |
| 117 | if (IS_ERR(f_ecm)) { |
| 118 | status = PTR_ERR(f_ecm); |
| 119 | goto err_get_ecm; |
| 120 | } |
| 121 | |
| 122 | status = usb_add_function(c, f_ecm); |
| 123 | if (status) |
| 124 | goto err_add_ecm; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 125 | |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 126 | f_acm = usb_get_function(fi_serial); |
Wei Yongjun | 8d6f51b | 2013-04-06 17:25:21 +0800 | [diff] [blame] | 127 | if (IS_ERR(f_acm)) { |
| 128 | status = PTR_ERR(f_acm); |
Andrzej Pietrasiewicz | d2ff405 | 2013-08-01 16:07:48 +0200 | [diff] [blame] | 129 | goto err_get_acm; |
Wei Yongjun | 8d6f51b | 2013-04-06 17:25:21 +0800 | [diff] [blame] | 130 | } |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 131 | |
| 132 | status = usb_add_function(c, f_acm); |
| 133 | if (status) |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 134 | goto err_add_acm; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 135 | return 0; |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 136 | |
| 137 | err_add_acm: |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 138 | usb_put_function(f_acm); |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 139 | err_get_acm: |
| 140 | usb_remove_function(c, f_ecm); |
| 141 | err_add_ecm: |
| 142 | usb_put_function(f_ecm); |
| 143 | err_get_ecm: |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 144 | return status; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static struct usb_configuration cdc_config_driver = { |
| 148 | .label = "CDC Composite (ECM + ACM)", |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 149 | .bConfigurationValue = 1, |
| 150 | /* .iConfiguration = DYNAMIC */ |
| 151 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /*-------------------------------------------------------------------------*/ |
| 155 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 156 | static int cdc_bind(struct usb_composite_dev *cdev) |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 157 | { |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 158 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 159 | struct f_ecm_opts *ecm_opts; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 160 | int status; |
| 161 | |
| 162 | if (!can_support_ecm(cdev->gadget)) { |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 163 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
| 164 | gadget->name); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | } |
| 167 | |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 168 | fi_ecm = usb_get_function_instance("ecm"); |
| 169 | if (IS_ERR(fi_ecm)) |
| 170 | return PTR_ERR(fi_ecm); |
| 171 | |
| 172 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
| 173 | |
| 174 | gether_set_qmult(ecm_opts->net, qmult); |
| 175 | if (!gether_set_host_addr(ecm_opts->net, host_addr)) |
| 176 | pr_info("using host ethernet address: %s", host_addr); |
| 177 | if (!gether_set_dev_addr(ecm_opts->net, dev_addr)) |
| 178 | pr_info("using self ethernet address: %s", dev_addr); |
| 179 | |
| 180 | fi_serial = usb_get_function_instance("acm"); |
| 181 | if (IS_ERR(fi_serial)) { |
| 182 | status = PTR_ERR(fi_serial); |
| 183 | goto fail; |
| 184 | } |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 185 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 186 | /* Allocate string descriptor numbers ... note that string |
| 187 | * contents can be overridden by the composite_dev glue. |
| 188 | */ |
| 189 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 190 | status = usb_string_ids_tab(cdev, strings_dev); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 191 | if (status < 0) |
| 192 | goto fail1; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 193 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 194 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 195 | |
| 196 | /* register our configuration */ |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 197 | status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 198 | if (status < 0) |
| 199 | goto fail1; |
| 200 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 201 | usb_composite_overwrite_options(cdev, &coverwrite); |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 202 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
| 203 | DRIVER_DESC); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | |
| 207 | fail1: |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 208 | usb_put_function_instance(fi_serial); |
| 209 | fail: |
| 210 | usb_put_function_instance(fi_ecm); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 211 | return status; |
| 212 | } |
| 213 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 214 | static int cdc_unbind(struct usb_composite_dev *cdev) |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 215 | { |
Sebastian Andrzej Siewior | 29a6645 | 2012-12-23 21:10:09 +0100 | [diff] [blame] | 216 | usb_put_function(f_acm); |
| 217 | usb_put_function_instance(fi_serial); |
Andrzej Pietrasiewicz | a38a275 | 2013-05-23 10:32:04 +0200 | [diff] [blame] | 218 | if (!IS_ERR_OR_NULL(f_ecm)) |
| 219 | usb_put_function(f_ecm); |
| 220 | if (!IS_ERR_OR_NULL(fi_ecm)) |
| 221 | usb_put_function_instance(fi_ecm); |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 225 | static struct usb_composite_driver cdc_driver = { |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 226 | .name = "g_cdc", |
| 227 | .dev = &device_desc, |
| 228 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 229 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 230 | .bind = cdc_bind, |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 231 | .unbind = cdc_unbind, |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
Tobias Klauser | 909346a | 2014-07-09 18:09:56 +0200 | [diff] [blame] | 234 | module_usb_composite_driver(cdc_driver); |
| 235 | |
David Brownell | 19e2068 | 2008-06-19 18:20:26 -0700 | [diff] [blame] | 236 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 237 | MODULE_AUTHOR("David Brownell"); |
| 238 | MODULE_LICENSE("GPL"); |