Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * multi.c -- Multifunction Composite driver |
| 3 | * |
| 4 | * Copyright (C) 2008 David Brownell |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * Copyright (C) 2009 Samsung Electronics |
| 7 | * Author: Michal Nazarewicz (m.nazarewicz@samsung.com) |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/utsname.h> |
| 27 | |
| 28 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 29 | #if defined USB_ETH_RNDIS |
| 30 | # undef USB_ETH_RNDIS |
| 31 | #endif |
Michal Nazarewicz | dbe4a99 | 2010-01-22 15:18:21 +0100 | [diff] [blame] | 32 | #ifdef CONFIG_USB_G_MULTI_RNDIS |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 33 | # define USB_ETH_RNDIS y |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | |
| 37 | #define DRIVER_DESC "Multifunction Composite Gadget" |
| 38 | #define DRIVER_VERSION "2009/07/21" |
| 39 | |
| 40 | /*-------------------------------------------------------------------------*/ |
| 41 | |
| 42 | #define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */ |
| 43 | #define MULTI_PRODUCT_NUM 0xa4ab /* XXX */ |
| 44 | |
| 45 | /*-------------------------------------------------------------------------*/ |
| 46 | |
| 47 | /* |
| 48 | * kbuild is not very cooperative with respect to linking separately |
| 49 | * compiled library objects into one module. So for now we won't use |
| 50 | * separate compilation ... ensuring init/exit sections work to shrink |
| 51 | * the runtime footprint, and giving us at least some parts of what |
| 52 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 53 | */ |
| 54 | |
| 55 | #include "composite.c" |
| 56 | #include "usbstring.c" |
| 57 | #include "config.c" |
| 58 | #include "epautoconf.c" |
| 59 | |
| 60 | #include "u_serial.c" |
| 61 | #include "f_acm.c" |
| 62 | |
| 63 | #include "f_ecm.c" |
| 64 | #include "f_subset.c" |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 65 | #ifdef USB_ETH_RNDIS |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 66 | # include "f_rndis.c" |
| 67 | # include "rndis.c" |
| 68 | #endif |
| 69 | #include "u_ether.c" |
| 70 | |
| 71 | #undef DBG /* u_ether.c has broken idea about macros */ |
| 72 | #undef VDBG /* so clean up after it */ |
| 73 | #undef ERROR |
| 74 | #undef INFO |
| 75 | #include "f_mass_storage.c" |
| 76 | |
| 77 | /*-------------------------------------------------------------------------*/ |
| 78 | |
| 79 | static struct usb_device_descriptor device_desc = { |
| 80 | .bLength = sizeof device_desc, |
| 81 | .bDescriptorType = USB_DT_DEVICE, |
| 82 | |
| 83 | .bcdUSB = cpu_to_le16(0x0200), |
| 84 | |
| 85 | /* .bDeviceClass = USB_CLASS_COMM, */ |
| 86 | /* .bDeviceSubClass = 0, */ |
| 87 | /* .bDeviceProtocol = 0, */ |
| 88 | .bDeviceClass = 0xEF, |
| 89 | .bDeviceSubClass = 2, |
| 90 | .bDeviceProtocol = 1, |
| 91 | /* .bMaxPacketSize0 = f(hardware) */ |
| 92 | |
| 93 | /* Vendor and product id can be overridden by module parameters. */ |
| 94 | .idVendor = cpu_to_le16(MULTI_VENDOR_NUM), |
| 95 | .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM), |
| 96 | /* .bcdDevice = f(hardware) */ |
| 97 | /* .iManufacturer = DYNAMIC */ |
| 98 | /* .iProduct = DYNAMIC */ |
| 99 | /* NO SERIAL NUMBER */ |
| 100 | .bNumConfigurations = 1, |
| 101 | }; |
| 102 | |
| 103 | static struct usb_otg_descriptor otg_descriptor = { |
| 104 | .bLength = sizeof otg_descriptor, |
| 105 | .bDescriptorType = USB_DT_OTG, |
| 106 | |
| 107 | /* REVISIT SRP-only hardware is possible, although |
| 108 | * it would not be called "OTG" ... |
| 109 | */ |
| 110 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 111 | }; |
| 112 | |
| 113 | static const struct usb_descriptor_header *otg_desc[] = { |
| 114 | (struct usb_descriptor_header *) &otg_descriptor, |
| 115 | NULL, |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | /* string IDs are assigned dynamically */ |
| 120 | |
| 121 | #define STRING_MANUFACTURER_IDX 0 |
| 122 | #define STRING_PRODUCT_IDX 1 |
| 123 | |
| 124 | static char manufacturer[50]; |
| 125 | |
| 126 | static struct usb_string strings_dev[] = { |
| 127 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
| 128 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, |
| 129 | { } /* end of list */ |
| 130 | }; |
| 131 | |
| 132 | static struct usb_gadget_strings stringtab_dev = { |
| 133 | .language = 0x0409, /* en-us */ |
| 134 | .strings = strings_dev, |
| 135 | }; |
| 136 | |
| 137 | static struct usb_gadget_strings *dev_strings[] = { |
| 138 | &stringtab_dev, |
| 139 | NULL, |
| 140 | }; |
| 141 | |
| 142 | static u8 hostaddr[ETH_ALEN]; |
| 143 | |
| 144 | |
| 145 | |
| 146 | /****************************** Configurations ******************************/ |
| 147 | |
| 148 | static struct fsg_module_parameters mod_data = { |
| 149 | .stall = 1 |
| 150 | }; |
| 151 | FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); |
| 152 | |
| 153 | static struct fsg_common *fsg_common; |
| 154 | |
| 155 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 156 | #ifdef USB_ETH_RNDIS |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 157 | |
| 158 | static int __init rndis_do_config(struct usb_configuration *c) |
| 159 | { |
| 160 | int ret; |
| 161 | |
| 162 | if (gadget_is_otg(c->cdev->gadget)) { |
| 163 | c->descriptors = otg_desc; |
| 164 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 165 | } |
| 166 | |
| 167 | ret = rndis_bind_config(c, hostaddr); |
| 168 | if (ret < 0) |
| 169 | return ret; |
| 170 | |
| 171 | ret = acm_bind_config(c, 0); |
| 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | ret = fsg_add(c->cdev, c, fsg_common); |
| 176 | if (ret < 0) |
| 177 | return ret; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static struct usb_configuration rndis_config_driver = { |
| 183 | .label = "Multifunction Composite (RNDIS + MS + ACM)", |
| 184 | .bind = rndis_do_config, |
| 185 | .bConfigurationValue = 2, |
| 186 | /* .iConfiguration = DYNAMIC */ |
| 187 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 188 | }; |
| 189 | |
| 190 | #endif |
| 191 | |
| 192 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 193 | |
| 194 | static int __init cdc_do_config(struct usb_configuration *c) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | if (gadget_is_otg(c->cdev->gadget)) { |
| 199 | c->descriptors = otg_desc; |
| 200 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 201 | } |
| 202 | |
| 203 | ret = ecm_bind_config(c, hostaddr); |
| 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | |
| 207 | ret = acm_bind_config(c, 0); |
| 208 | if (ret < 0) |
| 209 | return ret; |
| 210 | |
| 211 | ret = fsg_add(c->cdev, c, fsg_common); |
| 212 | if (ret < 0) |
| 213 | return ret; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static struct usb_configuration cdc_config_driver = { |
| 219 | .label = "Multifunction Composite (CDC + MS + ACM)", |
| 220 | .bind = cdc_do_config, |
| 221 | .bConfigurationValue = 1, |
| 222 | /* .iConfiguration = DYNAMIC */ |
| 223 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 224 | }; |
| 225 | |
| 226 | #endif |
| 227 | |
| 228 | |
| 229 | |
| 230 | /****************************** Gadget Bind ******************************/ |
| 231 | |
| 232 | |
| 233 | static int __init multi_bind(struct usb_composite_dev *cdev) |
| 234 | { |
| 235 | struct usb_gadget *gadget = cdev->gadget; |
| 236 | int status, gcnum; |
| 237 | |
| 238 | if (!can_support_ecm(cdev->gadget)) { |
| 239 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
| 240 | gadget->name); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | /* set up network link layer */ |
| 245 | status = gether_setup(cdev->gadget, hostaddr); |
| 246 | if (status < 0) |
| 247 | return status; |
| 248 | |
| 249 | /* set up serial link layer */ |
| 250 | status = gserial_setup(cdev->gadget, 1); |
| 251 | if (status < 0) |
| 252 | goto fail0; |
| 253 | |
| 254 | /* set up mass storage function */ |
| 255 | fsg_common = fsg_common_from_params(0, cdev, &mod_data); |
| 256 | if (IS_ERR(fsg_common)) { |
| 257 | status = PTR_ERR(fsg_common); |
| 258 | goto fail1; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | gcnum = usb_gadget_controller_number(gadget); |
| 263 | if (gcnum >= 0) |
| 264 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); |
| 265 | else { |
| 266 | /* We assume that can_support_ecm() tells the truth; |
| 267 | * but if the controller isn't recognized at all then |
| 268 | * that assumption is a bit more likely to be wrong. |
| 269 | */ |
| 270 | WARNING(cdev, "controller '%s' not recognized\n", |
| 271 | gadget->name); |
| 272 | device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* Allocate string descriptor numbers ... note that string |
| 277 | * contents can be overridden by the composite_dev glue. |
| 278 | */ |
| 279 | |
| 280 | /* device descriptor strings: manufacturer, product */ |
| 281 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
| 282 | init_utsname()->sysname, init_utsname()->release, |
| 283 | gadget->name); |
| 284 | status = usb_string_id(cdev); |
| 285 | if (status < 0) |
| 286 | goto fail2; |
| 287 | strings_dev[STRING_MANUFACTURER_IDX].id = status; |
| 288 | device_desc.iManufacturer = status; |
| 289 | |
| 290 | status = usb_string_id(cdev); |
| 291 | if (status < 0) |
| 292 | goto fail2; |
| 293 | strings_dev[STRING_PRODUCT_IDX].id = status; |
| 294 | device_desc.iProduct = status; |
| 295 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 296 | #ifdef USB_ETH_RNDIS |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 297 | /* register our first configuration */ |
| 298 | status = usb_add_config(cdev, &rndis_config_driver); |
| 299 | if (status < 0) |
| 300 | goto fail2; |
| 301 | #endif |
| 302 | |
| 303 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 304 | /* register our second configuration */ |
| 305 | status = usb_add_config(cdev, &cdc_config_driver); |
| 306 | if (status < 0) |
| 307 | goto fail2; |
| 308 | #endif |
| 309 | |
| 310 | dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
| 311 | fsg_common_put(fsg_common); |
| 312 | return 0; |
| 313 | |
| 314 | fail2: |
| 315 | fsg_common_put(fsg_common); |
| 316 | fail1: |
| 317 | gserial_cleanup(); |
| 318 | fail0: |
| 319 | gether_cleanup(); |
| 320 | return status; |
| 321 | } |
| 322 | |
| 323 | static int __exit multi_unbind(struct usb_composite_dev *cdev) |
| 324 | { |
| 325 | gserial_cleanup(); |
| 326 | gether_cleanup(); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /****************************** Some noise ******************************/ |
| 332 | |
| 333 | |
| 334 | static struct usb_composite_driver multi_driver = { |
| 335 | .name = "g_multi", |
| 336 | .dev = &device_desc, |
| 337 | .strings = dev_strings, |
| 338 | .bind = multi_bind, |
| 339 | .unbind = __exit_p(multi_unbind), |
| 340 | }; |
| 341 | |
| 342 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 343 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 344 | MODULE_LICENSE("GPL"); |
| 345 | |
| 346 | static int __init g_multi_init(void) |
| 347 | { |
| 348 | return usb_composite_register(&multi_driver); |
| 349 | } |
| 350 | module_init(g_multi_init); |
| 351 | |
| 352 | static void __exit g_multi_cleanup(void) |
| 353 | { |
| 354 | usb_composite_unregister(&multi_driver); |
| 355 | } |
| 356 | module_exit(g_multi_cleanup); |