Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ncm.c -- NCM gadget driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
| 5 | * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com> |
| 6 | * |
| 7 | * The driver borrows from ether.c which is: |
| 8 | * |
| 9 | * Copyright (C) 2003-2005,2008 David Brownell |
| 10 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 11 | * Copyright (C) 2008 Nokia Corporation |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* #define DEBUG */ |
| 20 | /* #define VERBOSE_DEBUG */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/usb/composite.h> |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 25 | |
| 26 | #include "u_ether.h" |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 27 | #include "u_ncm.h" |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 28 | |
| 29 | #define DRIVER_DESC "NCM Gadget" |
| 30 | |
| 31 | /*-------------------------------------------------------------------------*/ |
| 32 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 33 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 34 | * Instead: allocate your own, using normal USB-IF procedures. |
| 35 | */ |
| 36 | |
| 37 | /* Thanks to NetChip Technologies for donating this product ID. |
| 38 | * It's for devices with only CDC Ethernet configurations. |
| 39 | */ |
| 40 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 41 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 42 | |
| 43 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 44 | USB_GADGET_COMPOSITE_OPTIONS(); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 45 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 46 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 47 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 48 | static struct usb_device_descriptor device_desc = { |
| 49 | .bLength = sizeof device_desc, |
| 50 | .bDescriptorType = USB_DT_DEVICE, |
| 51 | |
| 52 | .bcdUSB = cpu_to_le16 (0x0200), |
| 53 | |
| 54 | .bDeviceClass = USB_CLASS_COMM, |
| 55 | .bDeviceSubClass = 0, |
| 56 | .bDeviceProtocol = 0, |
| 57 | /* .bMaxPacketSize0 = f(hardware) */ |
| 58 | |
| 59 | /* Vendor and product id defaults change according to what configs |
| 60 | * we support. (As does bNumConfigurations.) These values can |
| 61 | * also be overridden by module parameters. |
| 62 | */ |
| 63 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
| 64 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), |
| 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 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 87 | /* string IDs are assigned dynamically */ |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 88 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 89 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 90 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 91 | [USB_GADGET_SERIAL_IDX].s = "", |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 92 | { } /* end of list */ |
| 93 | }; |
| 94 | |
| 95 | static struct usb_gadget_strings stringtab_dev = { |
| 96 | .language = 0x0409, /* en-us */ |
| 97 | .strings = strings_dev, |
| 98 | }; |
| 99 | |
| 100 | static struct usb_gadget_strings *dev_strings[] = { |
| 101 | &stringtab_dev, |
| 102 | NULL, |
| 103 | }; |
| 104 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 105 | static struct usb_function_instance *f_ncm_inst; |
| 106 | static struct usb_function *f_ncm; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 107 | |
| 108 | /*-------------------------------------------------------------------------*/ |
| 109 | |
| 110 | static int __init ncm_do_config(struct usb_configuration *c) |
| 111 | { |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 112 | int status; |
| 113 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 114 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
| 115 | |
| 116 | if (gadget_is_otg(c->cdev->gadget)) { |
| 117 | c->descriptors = otg_desc; |
| 118 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 119 | } |
| 120 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 121 | f_ncm = usb_get_function(f_ncm_inst); |
| 122 | if (IS_ERR(f_ncm)) { |
| 123 | status = PTR_ERR(f_ncm); |
| 124 | return status; |
| 125 | } |
| 126 | |
| 127 | status = usb_add_function(c, f_ncm); |
| 128 | if (status < 0) { |
| 129 | usb_put_function(f_ncm); |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | return 0; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static struct usb_configuration ncm_config_driver = { |
| 137 | /* .label = f(hardware) */ |
| 138 | .label = "CDC Ethernet (NCM)", |
| 139 | .bConfigurationValue = 1, |
| 140 | /* .iConfiguration = DYNAMIC */ |
| 141 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 142 | }; |
| 143 | |
| 144 | /*-------------------------------------------------------------------------*/ |
| 145 | |
| 146 | static int __init gncm_bind(struct usb_composite_dev *cdev) |
| 147 | { |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 148 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 149 | struct f_ncm_opts *ncm_opts; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 150 | int status; |
| 151 | |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 152 | f_ncm_inst = usb_get_function_instance("ncm"); |
| 153 | if (IS_ERR(f_ncm_inst)) |
| 154 | return PTR_ERR(f_ncm_inst); |
| 155 | |
| 156 | ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst); |
| 157 | |
| 158 | gether_set_qmult(ncm_opts->net, qmult); |
| 159 | if (!gether_set_host_addr(ncm_opts->net, host_addr)) |
| 160 | pr_info("using host ethernet address: %s", host_addr); |
| 161 | if (!gether_set_dev_addr(ncm_opts->net, dev_addr)) |
| 162 | pr_info("using self ethernet address: %s", dev_addr); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 163 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 164 | /* Allocate string descriptor numbers ... note that string |
| 165 | * contents can be overridden by the composite_dev glue. |
| 166 | */ |
| 167 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 168 | status = usb_string_ids_tab(cdev, strings_dev); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 169 | if (status < 0) |
| 170 | goto fail; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 171 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 172 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 173 | |
| 174 | status = usb_add_config(cdev, &ncm_config_driver, |
| 175 | ncm_do_config); |
| 176 | if (status < 0) |
| 177 | goto fail; |
| 178 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 179 | usb_composite_overwrite_options(cdev, &coverwrite); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 180 | dev_info(&gadget->dev, "%s\n", DRIVER_DESC); |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | fail: |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 185 | usb_put_function_instance(f_ncm_inst); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 186 | return status; |
| 187 | } |
| 188 | |
| 189 | static int __exit gncm_unbind(struct usb_composite_dev *cdev) |
| 190 | { |
Andrzej Pietrasiewicz | 9575bcf | 2013-05-23 09:22:07 +0200 | [diff] [blame] | 191 | if (!IS_ERR_OR_NULL(f_ncm)) |
| 192 | usb_put_function(f_ncm); |
| 193 | if (!IS_ERR_OR_NULL(f_ncm_inst)) |
| 194 | usb_put_function_instance(f_ncm_inst); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 198 | static __refdata 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, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 202 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 203 | .bind = gncm_bind, |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 204 | .unbind = __exit_p(gncm_unbind), |
| 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"); |