Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Gadget Driver for Android |
| 3 | * |
| 4 | * Copyright (C) 2008 Google, Inc. |
| 5 | * Author: Mike Lockwood <lockwood@android.com> |
| 6 | * Benoit Goby <benoit@android.com> |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/utsname.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | |
| 27 | #include <linux/usb/ch9.h> |
| 28 | #include <linux/usb/composite.h> |
| 29 | #include <linux/usb/gadget.h> |
| 30 | |
| 31 | #include "gadget_chips.h" |
| 32 | |
| 33 | /* |
| 34 | * Kbuild is not very cooperative with respect to linking separately |
| 35 | * compiled library objects into one module. So for now we won't use |
| 36 | * separate compilation ... ensuring init/exit sections work to shrink |
| 37 | * the runtime footprint, and giving us at least some parts of what |
| 38 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 39 | */ |
| 40 | #include "usbstring.c" |
| 41 | #include "config.c" |
| 42 | #include "epautoconf.c" |
| 43 | #include "composite.c" |
| 44 | |
| 45 | #include "f_mass_storage.c" |
| 46 | #include "u_serial.c" |
| 47 | #include "f_acm.c" |
| 48 | #define USB_ETH_RNDIS y |
| 49 | #include "f_rndis.c" |
| 50 | #include "rndis.c" |
| 51 | #include "u_ether.c" |
| 52 | |
| 53 | MODULE_AUTHOR("Mike Lockwood"); |
| 54 | MODULE_DESCRIPTION("Android Composite USB Driver"); |
| 55 | MODULE_LICENSE("GPL"); |
| 56 | MODULE_VERSION("1.0"); |
| 57 | |
| 58 | static const char longname[] = "Gadget Android"; |
| 59 | |
| 60 | /* Default vendor and product IDs, overridden by userspace */ |
| 61 | #define VENDOR_ID 0x18D1 |
| 62 | #define PRODUCT_ID 0x0001 |
| 63 | |
| 64 | struct android_usb_function { |
| 65 | char *name; |
| 66 | void *config; |
| 67 | |
| 68 | struct device *dev; |
| 69 | char *dev_name; |
| 70 | struct device_attribute **attributes; |
| 71 | |
| 72 | /* for android_dev.enabled_functions */ |
| 73 | struct list_head enabled_list; |
| 74 | |
| 75 | /* Optional: initialization during gadget bind */ |
| 76 | int (*init)(struct android_usb_function *, struct usb_composite_dev *); |
| 77 | /* Optional: cleanup during gadget unbind */ |
| 78 | void (*cleanup)(struct android_usb_function *); |
| 79 | |
| 80 | int (*bind_config)(struct android_usb_function *, |
| 81 | struct usb_configuration *); |
| 82 | |
| 83 | /* Optional: called when the configuration is removed */ |
| 84 | void (*unbind_config)(struct android_usb_function *, |
| 85 | struct usb_configuration *); |
| 86 | /* Optional: handle ctrl requests before the device is configured */ |
| 87 | int (*ctrlrequest)(struct android_usb_function *, |
| 88 | struct usb_composite_dev *, |
| 89 | const struct usb_ctrlrequest *); |
| 90 | }; |
| 91 | |
| 92 | struct android_dev { |
| 93 | struct android_usb_function **functions; |
| 94 | struct list_head enabled_functions; |
| 95 | struct usb_composite_dev *cdev; |
| 96 | struct device *dev; |
| 97 | |
| 98 | bool enabled; |
| 99 | struct mutex mutex; |
| 100 | bool connected; |
| 101 | bool sw_connected; |
| 102 | struct work_struct work; |
| 103 | }; |
| 104 | |
| 105 | static struct class *android_class; |
| 106 | static struct android_dev *_android_dev; |
| 107 | static int android_bind_config(struct usb_configuration *c); |
| 108 | static void android_unbind_config(struct usb_configuration *c); |
| 109 | |
| 110 | /* string IDs are assigned dynamically */ |
| 111 | #define STRING_MANUFACTURER_IDX 0 |
| 112 | #define STRING_PRODUCT_IDX 1 |
| 113 | #define STRING_SERIAL_IDX 2 |
| 114 | |
| 115 | static char manufacturer_string[256]; |
| 116 | static char product_string[256]; |
| 117 | static char serial_string[256]; |
| 118 | |
| 119 | /* String Table */ |
| 120 | static struct usb_string strings_dev[] = { |
| 121 | [STRING_MANUFACTURER_IDX].s = manufacturer_string, |
| 122 | [STRING_PRODUCT_IDX].s = product_string, |
| 123 | [STRING_SERIAL_IDX].s = serial_string, |
| 124 | { } /* end of list */ |
| 125 | }; |
| 126 | |
| 127 | static struct usb_gadget_strings stringtab_dev = { |
| 128 | .language = 0x0409, /* en-us */ |
| 129 | .strings = strings_dev, |
| 130 | }; |
| 131 | |
| 132 | static struct usb_gadget_strings *dev_strings[] = { |
| 133 | &stringtab_dev, |
| 134 | NULL, |
| 135 | }; |
| 136 | |
| 137 | static struct usb_device_descriptor device_desc = { |
| 138 | .bLength = sizeof(device_desc), |
| 139 | .bDescriptorType = USB_DT_DEVICE, |
| 140 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 141 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 142 | .idVendor = __constant_cpu_to_le16(VENDOR_ID), |
| 143 | .idProduct = __constant_cpu_to_le16(PRODUCT_ID), |
| 144 | .bcdDevice = __constant_cpu_to_le16(0xffff), |
| 145 | .bNumConfigurations = 1, |
| 146 | }; |
| 147 | |
| 148 | static struct usb_configuration android_config_driver = { |
| 149 | .label = "android", |
| 150 | .unbind = android_unbind_config, |
| 151 | .bConfigurationValue = 1, |
| 152 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 153 | .bMaxPower = 0xFA, /* 500ma */ |
| 154 | }; |
| 155 | |
| 156 | static void android_work(struct work_struct *data) |
| 157 | { |
| 158 | struct android_dev *dev = container_of(data, struct android_dev, work); |
| 159 | struct usb_composite_dev *cdev = dev->cdev; |
| 160 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 161 | char *connected[2] = { "USB_STATE=CONNECTED", NULL }; |
| 162 | char *configured[2] = { "USB_STATE=CONFIGURED", NULL }; |
| 163 | char **uevent_envp = NULL; |
| 164 | unsigned long flags; |
| 165 | |
| 166 | spin_lock_irqsave(&cdev->lock, flags); |
| 167 | if (cdev->config) |
| 168 | uevent_envp = configured; |
| 169 | else if (dev->connected != dev->sw_connected) |
| 170 | uevent_envp = dev->connected ? connected : disconnected; |
| 171 | dev->sw_connected = dev->connected; |
| 172 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 173 | |
| 174 | if (uevent_envp) { |
| 175 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp); |
| 176 | pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]); |
| 177 | } else { |
| 178 | pr_info("%s: did not send uevent (%d %d %p)\n", __func__, |
| 179 | dev->connected, dev->sw_connected, cdev->config); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /*-------------------------------------------------------------------------*/ |
| 185 | /* Supported functions initialization */ |
| 186 | |
| 187 | #define MAX_ACM_INSTANCES 4 |
| 188 | struct acm_function_config { |
| 189 | int instances; |
| 190 | }; |
| 191 | |
| 192 | static int |
| 193 | acm_function_init(struct android_usb_function *f, |
| 194 | struct usb_composite_dev *cdev) |
| 195 | { |
| 196 | f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL); |
| 197 | if (!f->config) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES); |
| 201 | } |
| 202 | |
| 203 | static void acm_function_cleanup(struct android_usb_function *f) |
| 204 | { |
| 205 | gserial_cleanup(); |
| 206 | kfree(f->config); |
| 207 | f->config = NULL; |
| 208 | } |
| 209 | |
| 210 | static int |
| 211 | acm_function_bind_config(struct android_usb_function *f, |
| 212 | struct usb_configuration *c) |
| 213 | { |
| 214 | int i; |
| 215 | int ret = 0; |
| 216 | struct acm_function_config *config = f->config; |
| 217 | |
| 218 | for (i = 0; i < config->instances; i++) { |
| 219 | ret = acm_bind_config(c, i); |
| 220 | if (ret) { |
| 221 | pr_err("Could not bind acm%u config\n", i); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static ssize_t acm_instances_show(struct device *dev, |
| 230 | struct device_attribute *attr, char *buf) |
| 231 | { |
| 232 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 233 | struct acm_function_config *config = f->config; |
| 234 | return sprintf(buf, "%d\n", config->instances); |
| 235 | } |
| 236 | |
| 237 | static ssize_t acm_instances_store(struct device *dev, |
| 238 | struct device_attribute *attr, const char *buf, size_t size) |
| 239 | { |
| 240 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 241 | struct acm_function_config *config = f->config; |
| 242 | int value; |
| 243 | |
| 244 | sscanf(buf, "%d", &value); |
| 245 | if (value > MAX_ACM_INSTANCES) |
| 246 | value = MAX_ACM_INSTANCES; |
| 247 | config->instances = value; |
| 248 | return size; |
| 249 | } |
| 250 | |
| 251 | static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, |
| 252 | acm_instances_store); |
| 253 | static struct device_attribute *acm_function_attributes[] = { |
| 254 | &dev_attr_instances, |
| 255 | NULL |
| 256 | }; |
| 257 | |
| 258 | static struct android_usb_function acm_function = { |
| 259 | .name = "acm", |
| 260 | .init = acm_function_init, |
| 261 | .cleanup = acm_function_cleanup, |
| 262 | .bind_config = acm_function_bind_config, |
| 263 | .attributes = acm_function_attributes, |
| 264 | }; |
| 265 | |
| 266 | |
| 267 | struct rndis_function_config { |
| 268 | u8 ethaddr[ETH_ALEN]; |
| 269 | u32 vendorID; |
| 270 | char manufacturer[256]; |
| 271 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 272 | bool wceis; |
| 273 | }; |
| 274 | |
| 275 | static int |
| 276 | rndis_function_init(struct android_usb_function *f, |
| 277 | struct usb_composite_dev *cdev) |
| 278 | { |
| 279 | f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL); |
| 280 | if (!f->config) |
| 281 | return -ENOMEM; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static void rndis_function_cleanup(struct android_usb_function *f) |
| 286 | { |
| 287 | kfree(f->config); |
| 288 | f->config = NULL; |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | rndis_function_bind_config(struct android_usb_function *f, |
| 293 | struct usb_configuration *c) |
| 294 | { |
| 295 | int ret; |
| 296 | struct rndis_function_config *rndis = f->config; |
| 297 | |
| 298 | if (!rndis) { |
| 299 | pr_err("%s: rndis_pdata\n", __func__); |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 304 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 305 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 306 | |
| 307 | ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis"); |
| 308 | if (ret) { |
| 309 | pr_err("%s: gether_setup failed\n", __func__); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | if (rndis->wceis) { |
| 314 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 315 | rndis_iad_descriptor.bFunctionClass = |
| 316 | USB_CLASS_WIRELESS_CONTROLLER; |
| 317 | rndis_iad_descriptor.bFunctionSubClass = 0x01; |
| 318 | rndis_iad_descriptor.bFunctionProtocol = 0x03; |
| 319 | rndis_control_intf.bInterfaceClass = |
| 320 | USB_CLASS_WIRELESS_CONTROLLER; |
| 321 | rndis_control_intf.bInterfaceSubClass = 0x01; |
| 322 | rndis_control_intf.bInterfaceProtocol = 0x03; |
| 323 | } |
| 324 | |
| 325 | return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID, |
| 326 | rndis->manufacturer); |
| 327 | } |
| 328 | |
| 329 | static void rndis_function_unbind_config(struct android_usb_function *f, |
| 330 | struct usb_configuration *c) |
| 331 | { |
| 332 | gether_cleanup(); |
| 333 | } |
| 334 | |
| 335 | static ssize_t rndis_manufacturer_show(struct device *dev, |
| 336 | struct device_attribute *attr, char *buf) |
| 337 | { |
| 338 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 339 | struct rndis_function_config *config = f->config; |
| 340 | return sprintf(buf, "%s\n", config->manufacturer); |
| 341 | } |
| 342 | |
| 343 | static ssize_t rndis_manufacturer_store(struct device *dev, |
| 344 | struct device_attribute *attr, const char *buf, size_t size) |
| 345 | { |
| 346 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 347 | struct rndis_function_config *config = f->config; |
| 348 | |
| 349 | if (size >= sizeof(config->manufacturer)) |
| 350 | return -EINVAL; |
| 351 | if (sscanf(buf, "%s", config->manufacturer) == 1) |
| 352 | return size; |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show, |
| 357 | rndis_manufacturer_store); |
| 358 | |
| 359 | static ssize_t rndis_wceis_show(struct device *dev, |
| 360 | struct device_attribute *attr, char *buf) |
| 361 | { |
| 362 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 363 | struct rndis_function_config *config = f->config; |
| 364 | return sprintf(buf, "%d\n", config->wceis); |
| 365 | } |
| 366 | |
| 367 | static ssize_t rndis_wceis_store(struct device *dev, |
| 368 | struct device_attribute *attr, const char *buf, size_t size) |
| 369 | { |
| 370 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 371 | struct rndis_function_config *config = f->config; |
| 372 | int value; |
| 373 | |
| 374 | if (sscanf(buf, "%d", &value) == 1) { |
| 375 | config->wceis = value; |
| 376 | return size; |
| 377 | } |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show, |
| 382 | rndis_wceis_store); |
| 383 | |
| 384 | static ssize_t rndis_ethaddr_show(struct device *dev, |
| 385 | struct device_attribute *attr, char *buf) |
| 386 | { |
| 387 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 388 | struct rndis_function_config *rndis = f->config; |
| 389 | return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 390 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 391 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 392 | } |
| 393 | |
| 394 | static ssize_t rndis_ethaddr_store(struct device *dev, |
| 395 | struct device_attribute *attr, const char *buf, size_t size) |
| 396 | { |
| 397 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 398 | struct rndis_function_config *rndis = f->config; |
| 399 | |
| 400 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 401 | (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1], |
| 402 | (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3], |
| 403 | (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6) |
| 404 | return size; |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
| 408 | static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show, |
| 409 | rndis_ethaddr_store); |
| 410 | |
| 411 | static ssize_t rndis_vendorID_show(struct device *dev, |
| 412 | struct device_attribute *attr, char *buf) |
| 413 | { |
| 414 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 415 | struct rndis_function_config *config = f->config; |
| 416 | return sprintf(buf, "%04x\n", config->vendorID); |
| 417 | } |
| 418 | |
| 419 | static ssize_t rndis_vendorID_store(struct device *dev, |
| 420 | struct device_attribute *attr, const char *buf, size_t size) |
| 421 | { |
| 422 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 423 | struct rndis_function_config *config = f->config; |
| 424 | int value; |
| 425 | |
| 426 | if (sscanf(buf, "%04x", &value) == 1) { |
| 427 | config->vendorID = value; |
| 428 | return size; |
| 429 | } |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show, |
| 434 | rndis_vendorID_store); |
| 435 | |
| 436 | static struct device_attribute *rndis_function_attributes[] = { |
| 437 | &dev_attr_manufacturer, |
| 438 | &dev_attr_wceis, |
| 439 | &dev_attr_ethaddr, |
| 440 | &dev_attr_vendorID, |
| 441 | NULL |
| 442 | }; |
| 443 | |
| 444 | static struct android_usb_function rndis_function = { |
| 445 | .name = "rndis", |
| 446 | .init = rndis_function_init, |
| 447 | .cleanup = rndis_function_cleanup, |
| 448 | .bind_config = rndis_function_bind_config, |
| 449 | .unbind_config = rndis_function_unbind_config, |
| 450 | .attributes = rndis_function_attributes, |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | struct mass_storage_function_config { |
| 455 | struct fsg_config fsg; |
| 456 | struct fsg_common *common; |
| 457 | }; |
| 458 | |
| 459 | static int mass_storage_function_init(struct android_usb_function *f, |
| 460 | struct usb_composite_dev *cdev) |
| 461 | { |
| 462 | struct mass_storage_function_config *config; |
| 463 | struct fsg_common *common; |
| 464 | int err; |
| 465 | |
| 466 | config = kzalloc(sizeof(struct mass_storage_function_config), |
| 467 | GFP_KERNEL); |
| 468 | if (!config) |
| 469 | return -ENOMEM; |
| 470 | |
| 471 | config->fsg.nluns = 1; |
| 472 | config->fsg.luns[0].removable = 1; |
| 473 | |
| 474 | common = fsg_common_init(NULL, cdev, &config->fsg); |
| 475 | if (IS_ERR(common)) { |
| 476 | kfree(config); |
| 477 | return PTR_ERR(common); |
| 478 | } |
| 479 | |
| 480 | err = sysfs_create_link(&f->dev->kobj, |
| 481 | &common->luns[0].dev.kobj, |
| 482 | "lun"); |
| 483 | if (err) { |
| 484 | kfree(config); |
| 485 | return err; |
| 486 | } |
| 487 | |
| 488 | config->common = common; |
| 489 | f->config = config; |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static void mass_storage_function_cleanup(struct android_usb_function *f) |
| 494 | { |
| 495 | kfree(f->config); |
| 496 | f->config = NULL; |
| 497 | } |
| 498 | |
| 499 | static int mass_storage_function_bind_config(struct android_usb_function *f, |
| 500 | struct usb_configuration *c) |
| 501 | { |
| 502 | struct mass_storage_function_config *config = f->config; |
| 503 | return fsg_bind_config(c->cdev, c, config->common); |
| 504 | } |
| 505 | |
| 506 | static ssize_t mass_storage_inquiry_show(struct device *dev, |
| 507 | struct device_attribute *attr, char *buf) |
| 508 | { |
| 509 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 510 | struct mass_storage_function_config *config = f->config; |
| 511 | return sprintf(buf, "%s\n", config->common->inquiry_string); |
| 512 | } |
| 513 | |
| 514 | static ssize_t mass_storage_inquiry_store(struct device *dev, |
| 515 | struct device_attribute *attr, const char *buf, size_t size) |
| 516 | { |
| 517 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 518 | struct mass_storage_function_config *config = f->config; |
| 519 | if (size >= sizeof(config->common->inquiry_string)) |
| 520 | return -EINVAL; |
| 521 | if (sscanf(buf, "%s", config->common->inquiry_string) != 1) |
| 522 | return -EINVAL; |
| 523 | return size; |
| 524 | } |
| 525 | |
| 526 | static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR, |
| 527 | mass_storage_inquiry_show, |
| 528 | mass_storage_inquiry_store); |
| 529 | |
| 530 | static struct device_attribute *mass_storage_function_attributes[] = { |
| 531 | &dev_attr_inquiry_string, |
| 532 | NULL |
| 533 | }; |
| 534 | |
| 535 | static struct android_usb_function mass_storage_function = { |
| 536 | .name = "mass_storage", |
| 537 | .init = mass_storage_function_init, |
| 538 | .cleanup = mass_storage_function_cleanup, |
| 539 | .bind_config = mass_storage_function_bind_config, |
| 540 | .attributes = mass_storage_function_attributes, |
| 541 | }; |
| 542 | |
| 543 | |
| 544 | static struct android_usb_function *supported_functions[] = { |
| 545 | &acm_function, |
| 546 | &rndis_function, |
| 547 | &mass_storage_function, |
| 548 | NULL |
| 549 | }; |
| 550 | |
| 551 | |
| 552 | static int android_init_functions(struct android_usb_function **functions, |
| 553 | struct usb_composite_dev *cdev) |
| 554 | { |
| 555 | struct android_dev *dev = _android_dev; |
| 556 | struct android_usb_function *f; |
| 557 | struct device_attribute **attrs; |
| 558 | struct device_attribute *attr; |
| 559 | int err; |
| 560 | int index = 0; |
| 561 | |
| 562 | for (; (f = *functions++); index++) { |
| 563 | f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name); |
| 564 | f->dev = device_create(android_class, dev->dev, |
| 565 | MKDEV(0, index), f, f->dev_name); |
| 566 | if (IS_ERR(f->dev)) { |
| 567 | pr_err("%s: Failed to create dev %s", __func__, |
| 568 | f->dev_name); |
| 569 | err = PTR_ERR(f->dev); |
| 570 | goto err_create; |
| 571 | } |
| 572 | |
| 573 | if (f->init) { |
| 574 | err = f->init(f, cdev); |
| 575 | if (err) { |
| 576 | pr_err("%s: Failed to init %s", __func__, |
| 577 | f->name); |
| 578 | goto err_out; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | attrs = f->attributes; |
| 583 | if (attrs) { |
| 584 | while ((attr = *attrs++) && !err) |
| 585 | err = device_create_file(f->dev, attr); |
| 586 | } |
| 587 | if (err) { |
| 588 | pr_err("%s: Failed to create function %s attributes", |
| 589 | __func__, f->name); |
| 590 | goto err_out; |
| 591 | } |
| 592 | } |
| 593 | return 0; |
| 594 | |
| 595 | err_out: |
| 596 | device_destroy(android_class, f->dev->devt); |
| 597 | err_create: |
| 598 | kfree(f->dev_name); |
| 599 | return err; |
| 600 | } |
| 601 | |
| 602 | static void android_cleanup_functions(struct android_usb_function **functions) |
| 603 | { |
| 604 | struct android_usb_function *f; |
| 605 | |
| 606 | while (*functions) { |
| 607 | f = *functions++; |
| 608 | |
| 609 | if (f->dev) { |
| 610 | device_destroy(android_class, f->dev->devt); |
| 611 | kfree(f->dev_name); |
| 612 | } |
| 613 | |
| 614 | if (f->cleanup) |
| 615 | f->cleanup(f); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static int |
| 620 | android_bind_enabled_functions(struct android_dev *dev, |
| 621 | struct usb_configuration *c) |
| 622 | { |
| 623 | struct android_usb_function *f; |
| 624 | int ret; |
| 625 | |
| 626 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 627 | ret = f->bind_config(f, c); |
| 628 | if (ret) { |
| 629 | pr_err("%s: %s failed", __func__, f->name); |
| 630 | return ret; |
| 631 | } |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static void |
| 637 | android_unbind_enabled_functions(struct android_dev *dev, |
| 638 | struct usb_configuration *c) |
| 639 | { |
| 640 | struct android_usb_function *f; |
| 641 | |
| 642 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 643 | if (f->unbind_config) |
| 644 | f->unbind_config(f, c); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | static int android_enable_function(struct android_dev *dev, char *name) |
| 649 | { |
| 650 | struct android_usb_function **functions = dev->functions; |
| 651 | struct android_usb_function *f; |
| 652 | while ((f = *functions++)) { |
| 653 | if (!strcmp(name, f->name)) { |
| 654 | list_add_tail(&f->enabled_list, |
| 655 | &dev->enabled_functions); |
| 656 | return 0; |
| 657 | } |
| 658 | } |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | /*-------------------------------------------------------------------------*/ |
| 663 | /* /sys/class/android_usb/android%d/ interface */ |
| 664 | |
| 665 | static ssize_t |
| 666 | functions_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 667 | { |
| 668 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 669 | struct android_usb_function *f; |
| 670 | char *buff = buf; |
| 671 | |
| 672 | mutex_lock(&dev->mutex); |
| 673 | |
| 674 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) |
| 675 | buff += sprintf(buff, "%s,", f->name); |
| 676 | |
| 677 | mutex_unlock(&dev->mutex); |
| 678 | |
| 679 | if (buff != buf) |
| 680 | *(buff-1) = '\n'; |
| 681 | return buff - buf; |
| 682 | } |
| 683 | |
| 684 | static ssize_t |
| 685 | functions_store(struct device *pdev, struct device_attribute *attr, |
| 686 | const char *buff, size_t size) |
| 687 | { |
| 688 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 689 | char *name; |
| 690 | char buf[256], *b; |
| 691 | int err; |
| 692 | |
| 693 | mutex_lock(&dev->mutex); |
| 694 | |
| 695 | if (dev->enabled) { |
| 696 | mutex_unlock(&dev->mutex); |
| 697 | return -EBUSY; |
| 698 | } |
| 699 | |
| 700 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 701 | |
| 702 | strncpy(buf, buff, sizeof(buf)); |
| 703 | b = strim(buf); |
| 704 | |
| 705 | while (b) { |
| 706 | name = strsep(&b, ","); |
| 707 | if (name) { |
| 708 | err = android_enable_function(dev, name); |
| 709 | if (err) |
| 710 | pr_err("android_usb: Cannot enable '%s'", name); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | mutex_unlock(&dev->mutex); |
| 715 | |
| 716 | return size; |
| 717 | } |
| 718 | |
| 719 | static ssize_t enable_show(struct device *pdev, struct device_attribute *attr, |
| 720 | char *buf) |
| 721 | { |
| 722 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 723 | return sprintf(buf, "%d\n", dev->enabled); |
| 724 | } |
| 725 | |
| 726 | static ssize_t enable_store(struct device *pdev, struct device_attribute *attr, |
| 727 | const char *buff, size_t size) |
| 728 | { |
| 729 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 730 | struct usb_composite_dev *cdev = dev->cdev; |
| 731 | int enabled = 0; |
| 732 | |
| 733 | mutex_lock(&dev->mutex); |
| 734 | |
| 735 | sscanf(buff, "%d", &enabled); |
| 736 | if (enabled && !dev->enabled) { |
| 737 | cdev->next_string_id = 0; |
| 738 | /* |
| 739 | * Update values in composite driver's copy of |
| 740 | * device descriptor. |
| 741 | */ |
| 742 | cdev->desc.idVendor = device_desc.idVendor; |
| 743 | cdev->desc.idProduct = device_desc.idProduct; |
| 744 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 745 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 746 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 747 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
| 748 | |
| 749 | usb_add_config(cdev, &android_config_driver, |
| 750 | android_bind_config); |
| 751 | usb_gadget_connect(cdev->gadget); |
| 752 | dev->enabled = true; |
| 753 | } else if (!enabled && dev->enabled) { |
| 754 | usb_gadget_disconnect(cdev->gadget); |
| 755 | /* Cancel pending control requests */ |
| 756 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); |
| 757 | usb_remove_config(cdev, &android_config_driver); |
| 758 | dev->enabled = false; |
| 759 | } else { |
| 760 | pr_err("android_usb: already %s\n", |
| 761 | dev->enabled ? "enabled" : "disabled"); |
| 762 | } |
| 763 | |
| 764 | mutex_unlock(&dev->mutex); |
| 765 | return size; |
| 766 | } |
| 767 | |
| 768 | static ssize_t state_show(struct device *pdev, struct device_attribute *attr, |
| 769 | char *buf) |
| 770 | { |
| 771 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 772 | struct usb_composite_dev *cdev = dev->cdev; |
| 773 | char *state = "DISCONNECTED"; |
| 774 | unsigned long flags; |
| 775 | |
| 776 | if (!cdev) |
| 777 | goto out; |
| 778 | |
| 779 | spin_lock_irqsave(&cdev->lock, flags); |
| 780 | if (cdev->config) |
| 781 | state = "CONFIGURED"; |
| 782 | else if (dev->connected) |
| 783 | state = "CONNECTED"; |
| 784 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 785 | out: |
| 786 | return sprintf(buf, "%s\n", state); |
| 787 | } |
| 788 | |
| 789 | #define DESCRIPTOR_ATTR(field, format_string) \ |
| 790 | static ssize_t \ |
| 791 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 792 | char *buf) \ |
| 793 | { \ |
| 794 | return sprintf(buf, format_string, device_desc.field); \ |
| 795 | } \ |
| 796 | static ssize_t \ |
| 797 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 798 | const char *buf, size_t size) \ |
| 799 | { \ |
| 800 | int value; \ |
| 801 | if (sscanf(buf, format_string, &value) == 1) { \ |
| 802 | device_desc.field = value; \ |
| 803 | return size; \ |
| 804 | } \ |
| 805 | return -1; \ |
| 806 | } \ |
| 807 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 808 | |
| 809 | #define DESCRIPTOR_STRING_ATTR(field, buffer) \ |
| 810 | static ssize_t \ |
| 811 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 812 | char *buf) \ |
| 813 | { \ |
| 814 | return sprintf(buf, "%s", buffer); \ |
| 815 | } \ |
| 816 | static ssize_t \ |
| 817 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 818 | const char *buf, size_t size) \ |
| 819 | { \ |
| 820 | if (size >= sizeof(buffer)) \ |
| 821 | return -EINVAL; \ |
| 822 | if (sscanf(buf, "%s", buffer) == 1) { \ |
| 823 | return size; \ |
| 824 | } \ |
| 825 | return -1; \ |
| 826 | } \ |
| 827 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 828 | |
| 829 | |
| 830 | DESCRIPTOR_ATTR(idVendor, "%04x\n") |
| 831 | DESCRIPTOR_ATTR(idProduct, "%04x\n") |
| 832 | DESCRIPTOR_ATTR(bcdDevice, "%04x\n") |
| 833 | DESCRIPTOR_ATTR(bDeviceClass, "%d\n") |
| 834 | DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n") |
| 835 | DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n") |
| 836 | DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string) |
| 837 | DESCRIPTOR_STRING_ATTR(iProduct, product_string) |
| 838 | DESCRIPTOR_STRING_ATTR(iSerial, serial_string) |
| 839 | |
| 840 | static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, |
| 841 | functions_store); |
| 842 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
| 843 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
| 844 | |
| 845 | static struct device_attribute *android_usb_attributes[] = { |
| 846 | &dev_attr_idVendor, |
| 847 | &dev_attr_idProduct, |
| 848 | &dev_attr_bcdDevice, |
| 849 | &dev_attr_bDeviceClass, |
| 850 | &dev_attr_bDeviceSubClass, |
| 851 | &dev_attr_bDeviceProtocol, |
| 852 | &dev_attr_iManufacturer, |
| 853 | &dev_attr_iProduct, |
| 854 | &dev_attr_iSerial, |
| 855 | &dev_attr_functions, |
| 856 | &dev_attr_enable, |
| 857 | &dev_attr_state, |
| 858 | NULL |
| 859 | }; |
| 860 | |
| 861 | /*-------------------------------------------------------------------------*/ |
| 862 | /* Composite driver */ |
| 863 | |
| 864 | static int android_bind_config(struct usb_configuration *c) |
| 865 | { |
| 866 | struct android_dev *dev = _android_dev; |
| 867 | int ret = 0; |
| 868 | |
| 869 | ret = android_bind_enabled_functions(dev, c); |
| 870 | if (ret) |
| 871 | return ret; |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static void android_unbind_config(struct usb_configuration *c) |
| 877 | { |
| 878 | struct android_dev *dev = _android_dev; |
| 879 | |
| 880 | android_unbind_enabled_functions(dev, c); |
| 881 | } |
| 882 | |
| 883 | static int android_bind(struct usb_composite_dev *cdev) |
| 884 | { |
| 885 | struct android_dev *dev = _android_dev; |
| 886 | struct usb_gadget *gadget = cdev->gadget; |
| 887 | int gcnum, id, ret; |
| 888 | |
| 889 | /* |
| 890 | * Start disconnected. Userspace will connect the gadget once |
| 891 | * it is done configuring the functions. |
| 892 | */ |
| 893 | usb_gadget_disconnect(gadget); |
| 894 | |
| 895 | ret = android_init_functions(dev->functions, cdev); |
| 896 | if (ret) |
| 897 | return ret; |
| 898 | |
| 899 | /* Allocate string descriptor numbers ... note that string |
| 900 | * contents can be overridden by the composite_dev glue. |
| 901 | */ |
| 902 | id = usb_string_id(cdev); |
| 903 | if (id < 0) |
| 904 | return id; |
| 905 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
| 906 | device_desc.iManufacturer = id; |
| 907 | |
| 908 | id = usb_string_id(cdev); |
| 909 | if (id < 0) |
| 910 | return id; |
| 911 | strings_dev[STRING_PRODUCT_IDX].id = id; |
| 912 | device_desc.iProduct = id; |
| 913 | |
| 914 | /* Default strings - should be updated by userspace */ |
| 915 | strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1); |
| 916 | strncpy(product_string, "Android", sizeof(product_string) - 1); |
| 917 | strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1); |
| 918 | |
| 919 | id = usb_string_id(cdev); |
| 920 | if (id < 0) |
| 921 | return id; |
| 922 | strings_dev[STRING_SERIAL_IDX].id = id; |
| 923 | device_desc.iSerialNumber = id; |
| 924 | |
| 925 | gcnum = usb_gadget_controller_number(gadget); |
| 926 | if (gcnum >= 0) |
| 927 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
| 928 | else { |
| 929 | pr_warning("%s: controller '%s' not recognized\n", |
| 930 | longname, gadget->name); |
| 931 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 932 | } |
| 933 | |
| 934 | usb_gadget_set_selfpowered(gadget); |
| 935 | dev->cdev = cdev; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | static int android_usb_unbind(struct usb_composite_dev *cdev) |
| 941 | { |
| 942 | struct android_dev *dev = _android_dev; |
| 943 | |
| 944 | cancel_work_sync(&dev->work); |
| 945 | android_cleanup_functions(dev->functions); |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static struct usb_composite_driver android_usb_driver = { |
| 950 | .name = "android_usb", |
| 951 | .dev = &device_desc, |
| 952 | .strings = dev_strings, |
| 953 | .unbind = android_usb_unbind, |
| 954 | .max_speed = USB_SPEED_HIGH, |
| 955 | }; |
| 956 | |
| 957 | static int |
| 958 | android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c) |
| 959 | { |
| 960 | struct android_dev *dev = _android_dev; |
| 961 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 962 | struct usb_request *req = cdev->req; |
| 963 | struct android_usb_function *f; |
| 964 | int value = -EOPNOTSUPP; |
| 965 | unsigned long flags; |
| 966 | |
| 967 | req->zero = 0; |
| 968 | req->complete = composite_setup_complete; |
| 969 | req->length = 0; |
| 970 | gadget->ep0->driver_data = cdev; |
| 971 | |
| 972 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 973 | if (f->ctrlrequest) { |
| 974 | value = f->ctrlrequest(f, cdev, c); |
| 975 | if (value >= 0) |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (value < 0) |
| 981 | value = composite_setup(gadget, c); |
| 982 | |
| 983 | spin_lock_irqsave(&cdev->lock, flags); |
| 984 | if (!dev->connected) { |
| 985 | dev->connected = 1; |
| 986 | schedule_work(&dev->work); |
| 987 | } else if (c->bRequest == USB_REQ_SET_CONFIGURATION && |
| 988 | cdev->config) { |
| 989 | schedule_work(&dev->work); |
| 990 | } |
| 991 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 992 | |
| 993 | return value; |
| 994 | } |
| 995 | |
| 996 | static void android_disconnect(struct usb_gadget *gadget) |
| 997 | { |
| 998 | struct android_dev *dev = _android_dev; |
| 999 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1000 | unsigned long flags; |
| 1001 | |
| 1002 | composite_disconnect(gadget); |
| 1003 | |
| 1004 | spin_lock_irqsave(&cdev->lock, flags); |
| 1005 | dev->connected = 0; |
| 1006 | schedule_work(&dev->work); |
| 1007 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1008 | } |
| 1009 | |
| 1010 | static int android_create_device(struct android_dev *dev) |
| 1011 | { |
| 1012 | struct device_attribute **attrs = android_usb_attributes; |
| 1013 | struct device_attribute *attr; |
| 1014 | int err; |
| 1015 | |
| 1016 | dev->dev = device_create(android_class, NULL, |
| 1017 | MKDEV(0, 0), NULL, "android0"); |
| 1018 | if (IS_ERR(dev->dev)) |
| 1019 | return PTR_ERR(dev->dev); |
| 1020 | |
| 1021 | dev_set_drvdata(dev->dev, dev); |
| 1022 | |
| 1023 | while ((attr = *attrs++)) { |
| 1024 | err = device_create_file(dev->dev, attr); |
| 1025 | if (err) { |
| 1026 | device_destroy(android_class, dev->dev->devt); |
| 1027 | return err; |
| 1028 | } |
| 1029 | } |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | static int __init init(void) |
| 1035 | { |
| 1036 | struct android_dev *dev; |
| 1037 | int err; |
| 1038 | |
| 1039 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 1040 | if (IS_ERR(android_class)) |
| 1041 | return PTR_ERR(android_class); |
| 1042 | |
| 1043 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1044 | if (!dev) |
| 1045 | return -ENOMEM; |
| 1046 | |
| 1047 | dev->functions = supported_functions; |
| 1048 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1049 | INIT_WORK(&dev->work, android_work); |
| 1050 | mutex_init(&dev->mutex); |
| 1051 | |
| 1052 | err = android_create_device(dev); |
| 1053 | if (err) { |
| 1054 | class_destroy(android_class); |
| 1055 | kfree(dev); |
| 1056 | return err; |
| 1057 | } |
| 1058 | |
| 1059 | _android_dev = dev; |
| 1060 | |
| 1061 | /* Override composite driver functions */ |
| 1062 | composite_driver.setup = android_setup; |
| 1063 | composite_driver.disconnect = android_disconnect; |
| 1064 | |
| 1065 | return usb_composite_probe(&android_usb_driver, android_bind); |
| 1066 | } |
| 1067 | module_init(init); |
| 1068 | |
| 1069 | static void __exit cleanup(void) |
| 1070 | { |
| 1071 | usb_composite_unregister(&android_usb_driver); |
| 1072 | class_destroy(android_class); |
| 1073 | kfree(_android_dev); |
| 1074 | _android_dev = NULL; |
| 1075 | } |
| 1076 | module_exit(cleanup); |