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