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