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> |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 23 | |
| 24 | #include "u_ether.h" |
| 25 | |
| 26 | #define DRIVER_DESC "NCM Gadget" |
| 27 | |
| 28 | /*-------------------------------------------------------------------------*/ |
| 29 | |
| 30 | /* |
| 31 | * Kbuild is not very cooperative with respect to linking separately |
| 32 | * compiled library objects into one module. So for now we won't use |
| 33 | * separate compilation ... ensuring init/exit sections work to shrink |
| 34 | * the runtime footprint, and giving us at least some parts of what |
| 35 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 36 | */ |
| 37 | #include "composite.c" |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 38 | |
| 39 | #include "f_ncm.c" |
| 40 | #include "u_ether.c" |
| 41 | |
| 42 | /*-------------------------------------------------------------------------*/ |
| 43 | |
| 44 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 45 | * Instead: allocate your own, using normal USB-IF procedures. |
| 46 | */ |
| 47 | |
| 48 | /* Thanks to NetChip Technologies for donating this product ID. |
| 49 | * It's for devices with only CDC Ethernet configurations. |
| 50 | */ |
| 51 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 52 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 53 | |
| 54 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 55 | USB_GADGET_COMPOSITE_OPTIONS(); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 56 | |
| 57 | static struct usb_device_descriptor device_desc = { |
| 58 | .bLength = sizeof device_desc, |
| 59 | .bDescriptorType = USB_DT_DEVICE, |
| 60 | |
| 61 | .bcdUSB = cpu_to_le16 (0x0200), |
| 62 | |
| 63 | .bDeviceClass = USB_CLASS_COMM, |
| 64 | .bDeviceSubClass = 0, |
| 65 | .bDeviceProtocol = 0, |
| 66 | /* .bMaxPacketSize0 = f(hardware) */ |
| 67 | |
| 68 | /* Vendor and product id defaults change according to what configs |
| 69 | * we support. (As does bNumConfigurations.) These values can |
| 70 | * also be overridden by module parameters. |
| 71 | */ |
| 72 | .idVendor = cpu_to_le16 (CDC_VENDOR_NUM), |
| 73 | .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM), |
| 74 | /* .bcdDevice = f(hardware) */ |
| 75 | /* .iManufacturer = DYNAMIC */ |
| 76 | /* .iProduct = DYNAMIC */ |
| 77 | /* NO SERIAL NUMBER */ |
| 78 | .bNumConfigurations = 1, |
| 79 | }; |
| 80 | |
| 81 | static struct usb_otg_descriptor otg_descriptor = { |
| 82 | .bLength = sizeof otg_descriptor, |
| 83 | .bDescriptorType = USB_DT_OTG, |
| 84 | |
| 85 | /* REVISIT SRP-only hardware is possible, although |
| 86 | * it would not be called "OTG" ... |
| 87 | */ |
| 88 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 89 | }; |
| 90 | |
| 91 | static const struct usb_descriptor_header *otg_desc[] = { |
| 92 | (struct usb_descriptor_header *) &otg_descriptor, |
| 93 | NULL, |
| 94 | }; |
| 95 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 96 | /* string IDs are assigned dynamically */ |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 97 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame^] | 98 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 99 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 100 | [USB_GADGET_SERIAL_IDX].s = "", |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 101 | { } /* end of list */ |
| 102 | }; |
| 103 | |
| 104 | static struct usb_gadget_strings stringtab_dev = { |
| 105 | .language = 0x0409, /* en-us */ |
| 106 | .strings = strings_dev, |
| 107 | }; |
| 108 | |
| 109 | static struct usb_gadget_strings *dev_strings[] = { |
| 110 | &stringtab_dev, |
| 111 | NULL, |
| 112 | }; |
| 113 | |
| 114 | static u8 hostaddr[ETH_ALEN]; |
| 115 | |
| 116 | /*-------------------------------------------------------------------------*/ |
| 117 | |
| 118 | static int __init ncm_do_config(struct usb_configuration *c) |
| 119 | { |
| 120 | /* FIXME alloc iConfiguration string, set it in c->strings */ |
| 121 | |
| 122 | if (gadget_is_otg(c->cdev->gadget)) { |
| 123 | c->descriptors = otg_desc; |
| 124 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 125 | } |
| 126 | |
| 127 | return ncm_bind_config(c, hostaddr); |
| 128 | } |
| 129 | |
| 130 | static struct usb_configuration ncm_config_driver = { |
| 131 | /* .label = f(hardware) */ |
| 132 | .label = "CDC Ethernet (NCM)", |
| 133 | .bConfigurationValue = 1, |
| 134 | /* .iConfiguration = DYNAMIC */ |
| 135 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 136 | }; |
| 137 | |
| 138 | /*-------------------------------------------------------------------------*/ |
| 139 | |
| 140 | static int __init gncm_bind(struct usb_composite_dev *cdev) |
| 141 | { |
| 142 | int gcnum; |
| 143 | struct usb_gadget *gadget = cdev->gadget; |
| 144 | int status; |
| 145 | |
| 146 | /* set up network link layer */ |
| 147 | status = gether_setup(cdev->gadget, hostaddr); |
| 148 | if (status < 0) |
| 149 | return status; |
| 150 | |
| 151 | gcnum = usb_gadget_controller_number(gadget); |
| 152 | if (gcnum >= 0) |
| 153 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
| 154 | else { |
| 155 | /* We assume that can_support_ecm() tells the truth; |
| 156 | * but if the controller isn't recognized at all then |
| 157 | * that assumption is a bit more likely to be wrong. |
| 158 | */ |
| 159 | dev_warn(&gadget->dev, |
| 160 | "controller '%s' not recognized; trying %s\n", |
| 161 | gadget->name, |
| 162 | ncm_config_driver.label); |
| 163 | device_desc.bcdDevice = |
| 164 | cpu_to_le16(0x0300 | 0x0099); |
| 165 | } |
| 166 | |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 167 | /* Allocate string descriptor numbers ... note that string |
| 168 | * contents can be overridden by the composite_dev glue. |
| 169 | */ |
| 170 | |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 171 | status = usb_string_ids_tab(cdev, strings_dev); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 172 | if (status < 0) |
| 173 | goto fail; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 174 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 175 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 176 | |
| 177 | status = usb_add_config(cdev, &ncm_config_driver, |
| 178 | ncm_do_config); |
| 179 | if (status < 0) |
| 180 | goto fail; |
| 181 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 182 | usb_composite_overwrite_options(cdev, &coverwrite); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 183 | dev_info(&gadget->dev, "%s\n", DRIVER_DESC); |
| 184 | |
| 185 | return 0; |
| 186 | |
| 187 | fail: |
| 188 | gether_cleanup(); |
| 189 | return status; |
| 190 | } |
| 191 | |
| 192 | static int __exit gncm_unbind(struct usb_composite_dev *cdev) |
| 193 | { |
| 194 | gether_cleanup(); |
| 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 | |
| 207 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 208 | MODULE_AUTHOR("Yauheni Kaliuta"); |
| 209 | MODULE_LICENSE("GPL"); |
| 210 | |
| 211 | static int __init init(void) |
| 212 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 213 | return usb_composite_probe(&ncm_driver); |
Yauheni Kaliuta | 6c34d28 | 2010-12-08 13:12:06 +0200 | [diff] [blame] | 214 | } |
| 215 | module_init(init); |
| 216 | |
| 217 | static void __exit cleanup(void) |
| 218 | { |
| 219 | usb_composite_unregister(&ncm_driver); |
| 220 | } |
| 221 | module_exit(cleanup); |