Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 2 | /* |
| 3 | * ncm.c -- NCM gadget driver |
| 4 | * |
| 5 | * Copyright (C) 2010 Nokia Corporation |
| 6 | * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com> |
| 7 | * |
| 8 | * The driver borrows from ether.c which is: |
| 9 | * |
| 10 | * Copyright (C) 2003-2005,2008 David Brownell |
| 11 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 12 | * Copyright (C) 2008 Nokia Corporation |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | /* #define DEBUG */ |
| 16 | /* #define VERBOSE_DEBUG */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/usb/composite.h> |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 21 | |
| 22 | #include "u_ether.h" |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 23 | #include "u_ncm.h" |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 24 | |
| 25 | #define DRIVER_DESC "NCM Gadget" |
| 26 | |
| 27 | /*-------------------------------------------------------------------------*/ |
| 28 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 29 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 30 | * Instead: allocate your own, using normal USB-IF procedures. |
| 31 | */ |
| 32 | |
| 33 | /* Thanks to NetChip Technologies for donating this product ID. |
| 34 | * It's for devices with only CDC Ethernet configurations. |
| 35 | */ |
| 36 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 37 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 38 | |
| 39 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 40 | USB_GADGET_COMPOSITE_OPTIONS(); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 41 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 42 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 43 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 44 | static struct usb_device_descriptor device_desc = { |
| 45 | .bLength = sizeof device_desc, |
| 46 | .bDescriptorType = USB_DT_DEVICE, |
| 47 | |
Igor Kotrasinski | 0aecfc1 | 2015-10-20 18:33:13 +0200 | [diff] [blame] | 48 | /* .bcdUSB = DYNAMIC */ |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 49 | |
| 50 | .bDeviceClass = USB_CLASS_COMM, |
| 51 | .bDeviceSubClass = 0, |
| 52 | .bDeviceProtocol = 0, |
| 53 | /* .bMaxPacketSize0 = f(hardware) */ |
| 54 | |
| 55 | /* Vendor and product id defaults change according to what configs |
| 56 | * we support. (As does bNumConfigurations.) These values can |
| 57 | * also be overridden by module parameters. |
| 58 | */ |
| 59 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
| 60 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), |
| 61 | /* .bcdDevice = f(hardware) */ |
| 62 | /* .iManufacturer = DYNAMIC */ |
| 63 | /* .iProduct = DYNAMIC */ |
| 64 | /* NO SERIAL NUMBER */ |
| 65 | .bNumConfigurations = 1, |
| 66 | }; |
| 67 | |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 68 | static const struct usb_descriptor_header *otg_desc[2]; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 69 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 70 | /* string IDs are assigned dynamically */ |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 71 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 72 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 73 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 74 | [USB_GADGET_SERIAL_IDX].s = "", |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 75 | { } /* end of list */ |
| 76 | }; |
| 77 | |
| 78 | static struct usb_gadget_strings stringtab_dev = { |
| 79 | .language = 0x0409, /* en-us */ |
| 80 | .strings = strings_dev, |
| 81 | }; |
| 82 | |
| 83 | static struct usb_gadget_strings *dev_strings[] = { |
| 84 | &stringtab_dev, |
| 85 | NULL, |
| 86 | }; |
| 87 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 88 | static struct usb_function_instance *f_ncm_inst; |
| 89 | static struct usb_function *f_ncm; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 90 | |
| 91 | /*-------------------------------------------------------------------------*/ |
| 92 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 93 | static int ncm_do_config(struct usb_configuration *c) |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 94 | { |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 95 | int status; |
| 96 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 97 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
| 98 | |
| 99 | if (gadget_is_otg(c->cdev->gadget)) { |
| 100 | c->descriptors = otg_desc; |
| 101 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 102 | } |
| 103 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 104 | f_ncm = usb_get_function(f_ncm_inst); |
Gustavo A. R. Silva | 0cb5818 | 2018-01-18 16:25:51 -0600 | [diff] [blame] | 105 | if (IS_ERR(f_ncm)) |
| 106 | return PTR_ERR(f_ncm); |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 107 | |
| 108 | status = usb_add_function(c, f_ncm); |
| 109 | if (status < 0) { |
| 110 | usb_put_function(f_ncm); |
| 111 | return status; |
| 112 | } |
| 113 | |
| 114 | return 0; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static struct usb_configuration ncm_config_driver = { |
| 118 | /* .label = f(hardware) */ |
| 119 | .label = "CDC Ethernet (NCM)", |
| 120 | .bConfigurationValue = 1, |
| 121 | /* .iConfiguration = DYNAMIC */ |
| 122 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 123 | }; |
| 124 | |
| 125 | /*-------------------------------------------------------------------------*/ |
| 126 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 127 | static int gncm_bind(struct usb_composite_dev *cdev) |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 128 | { |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 129 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 130 | struct f_ncm_opts *ncm_opts; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 131 | int status; |
| 132 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 133 | f_ncm_inst = usb_get_function_instance("ncm"); |
| 134 | if (IS_ERR(f_ncm_inst)) |
| 135 | return PTR_ERR(f_ncm_inst); |
| 136 | |
| 137 | ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst); |
| 138 | |
| 139 | gether_set_qmult(ncm_opts->net, qmult); |
| 140 | if (!gether_set_host_addr(ncm_opts->net, host_addr)) |
| 141 | pr_info("using host ethernet address: %s", host_addr); |
| 142 | if (!gether_set_dev_addr(ncm_opts->net, dev_addr)) |
| 143 | pr_info("using self ethernet address: %s", dev_addr); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 144 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 145 | /* Allocate string descriptor numbers ... note that string |
| 146 | * contents can be overridden by the composite_dev glue. |
| 147 | */ |
| 148 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 149 | status = usb_string_ids_tab(cdev, strings_dev); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 150 | if (status < 0) |
| 151 | goto fail; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 152 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 153 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 154 | |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 155 | if (gadget_is_otg(gadget) && !otg_desc[0]) { |
| 156 | struct usb_descriptor_header *usb_desc; |
| 157 | |
| 158 | usb_desc = usb_otg_descriptor_alloc(gadget); |
Wei Yongjun | e27d4b3 | 2020-05-07 05:13:23 +0000 | [diff] [blame] | 159 | if (!usb_desc) { |
| 160 | status = -ENOMEM; |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 161 | goto fail; |
Wei Yongjun | e27d4b3 | 2020-05-07 05:13:23 +0000 | [diff] [blame] | 162 | } |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 163 | usb_otg_descriptor_init(gadget, usb_desc); |
| 164 | otg_desc[0] = usb_desc; |
| 165 | otg_desc[1] = NULL; |
| 166 | } |
| 167 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 168 | status = usb_add_config(cdev, &ncm_config_driver, |
| 169 | ncm_do_config); |
| 170 | if (status < 0) |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 171 | goto fail1; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 172 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 173 | usb_composite_overwrite_options(cdev, &coverwrite); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 174 | dev_info(&gadget->dev, "%s\n", DRIVER_DESC); |
| 175 | |
| 176 | return 0; |
| 177 | |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 178 | fail1: |
| 179 | kfree(otg_desc[0]); |
| 180 | otg_desc[0] = NULL; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 181 | fail: |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 182 | usb_put_function_instance(f_ncm_inst); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 183 | return status; |
| 184 | } |
| 185 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 186 | static int gncm_unbind(struct usb_composite_dev *cdev) |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 187 | { |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 188 | if (!IS_ERR_OR_NULL(f_ncm)) |
| 189 | usb_put_function(f_ncm); |
| 190 | if (!IS_ERR_OR_NULL(f_ncm_inst)) |
| 191 | usb_put_function_instance(f_ncm_inst); |
Li Jun | 1156e91 | 2015-07-09 15:18:57 +0800 | [diff] [blame] | 192 | kfree(otg_desc[0]); |
| 193 | otg_desc[0] = NULL; |
| 194 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 198 | static struct usb_composite_driver ncm_driver = { |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 199 | .name = "g_ncm", |
| 200 | .dev = &device_desc, |
| 201 | .strings = dev_strings, |
Roger Quadros | 463f67a | 2019-12-23 08:47:35 +0200 | [diff] [blame] | 202 | .max_speed = USB_SPEED_SUPER, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 203 | .bind = gncm_bind, |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 204 | .unbind = gncm_unbind, |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
Tobias Klauser | 909346a | 2014-07-09 18:09:56 +0200 | [diff] [blame] | 207 | module_usb_composite_driver(ncm_driver); |
| 208 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 209 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 210 | MODULE_AUTHOR("Yauheni Kaliuta"); |
| 211 | MODULE_LICENSE("GPL"); |