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