Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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> |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 30 | #include <linux/usb/ch9.h> |
| 31 | #include <linux/usb/composite.h> |
| 32 | #include <linux/usb/gadget.h> |
| 33 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 66 | /* Default vendor and product IDs, overridden by userspace */ |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 67 | #define VENDOR_ID 0x18D1 |
| 68 | #define PRODUCT_ID 0x0001 |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 69 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 70 | struct android_usb_function { |
| 71 | char *name; |
| 72 | void *config; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 73 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 *); |
Mike Lockwood | 686d33a | 2011-09-07 09:55:12 -0700 | [diff] [blame] | 90 | /* Optional: handle ctrl requests before the device is configured */ |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 91 | int (*ctrlrequest)(struct android_usb_function *, |
| 92 | struct usb_composite_dev *, |
| 93 | const struct usb_ctrlrequest *); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | bool connected; |
| 104 | bool sw_connected; |
| 105 | struct work_struct work; |
| 106 | }; |
| 107 | |
| 108 | static struct class *android_class; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 109 | static struct android_dev *_android_dev; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 110 | static int android_bind_config(struct usb_configuration *c); |
| 111 | static void android_unbind_config(struct usb_configuration *c); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 112 | |
| 113 | /* string IDs are assigned dynamically */ |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 114 | #define STRING_MANUFACTURER_IDX 0 |
| 115 | #define STRING_PRODUCT_IDX 1 |
| 116 | #define STRING_SERIAL_IDX 2 |
| 117 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 118 | static char manufacturer_string[256]; |
| 119 | static char product_string[256]; |
| 120 | static char serial_string[256]; |
| 121 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 122 | /* String Table */ |
| 123 | static struct usb_string strings_dev[] = { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 124 | [STRING_MANUFACTURER_IDX].s = manufacturer_string, |
| 125 | [STRING_PRODUCT_IDX].s = product_string, |
| 126 | [STRING_SERIAL_IDX].s = serial_string, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 151 | static struct usb_configuration android_config_driver = { |
| 152 | .label = "android", |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 153 | .unbind = android_unbind_config, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 154 | .bConfigurationValue = 1, |
| 155 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 156 | .bMaxPower = 0xFA, /* 500ma */ |
| 157 | }; |
| 158 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | unsigned long flags; |
| 167 | |
| 168 | spin_lock_irqsave(&cdev->lock, flags); |
| 169 | if (cdev->config) { |
| 170 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 171 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, |
| 172 | configured); |
| 173 | return; |
| 174 | } |
| 175 | if (dev->connected != dev->sw_connected) { |
| 176 | dev->sw_connected = dev->connected; |
| 177 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 178 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, |
| 179 | dev->sw_connected ? connected : disconnected); |
| 180 | } else { |
| 181 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 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) |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 234 | { |
| 235 | int i; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 236 | int ret = 0; |
| 237 | struct acm_function_config *config = f->config; |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 238 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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; |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 244 | } |
| 245 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 246 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 250 | static ssize_t acm_instances_show(struct device *dev, |
| 251 | struct device_attribute *attr, char *buf) |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 252 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | } |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 257 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | { |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 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); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 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 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | |
Mike Lockwood | 394bf63 | 2011-08-12 14:35:42 -0700 | [diff] [blame] | 375 | ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis"); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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, |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 649 | &ptp_function, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | } |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 737 | } |
| 738 | return 0; |
| 739 | } |
| 740 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 741 | static void |
| 742 | android_unbind_enabled_functions(struct android_dev *dev, |
| 743 | struct usb_configuration *c) |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 744 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 760 | return 0; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 761 | } |
| 762 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 763 | return -EINVAL; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 764 | } |
| 765 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 766 | /*-------------------------------------------------------------------------*/ |
| 767 | /* /sys/class/android_usb/android%d/ interface */ |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 768 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 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 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) |
| 777 | buff += sprintf(buff, "%s,", f->name); |
| 778 | if (buff != buf) |
| 779 | *(buff-1) = '\n'; |
| 780 | return buff - buf; |
| 781 | } |
| 782 | |
| 783 | static ssize_t |
| 784 | functions_store(struct device *pdev, struct device_attribute *attr, |
| 785 | const char *buff, size_t size) |
| 786 | { |
| 787 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 788 | char *name; |
| 789 | char buf[256], *b; |
| 790 | int err; |
| 791 | |
| 792 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 793 | |
| 794 | strncpy(buf, buff, sizeof(buf)); |
| 795 | b = strim(buf); |
| 796 | |
| 797 | while (b) { |
| 798 | name = strsep(&b, ","); |
| 799 | if (name) { |
| 800 | err = android_enable_function(dev, name); |
| 801 | if (err) |
| 802 | pr_err("android_usb: Cannot enable '%s'", name); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 803 | } |
| 804 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 805 | |
| 806 | return size; |
| 807 | } |
| 808 | |
| 809 | static ssize_t enable_show(struct device *pdev, struct device_attribute *attr, |
| 810 | char *buf) |
| 811 | { |
| 812 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 813 | return sprintf(buf, "%d\n", dev->enabled); |
| 814 | } |
| 815 | |
| 816 | static ssize_t enable_store(struct device *pdev, struct device_attribute *attr, |
| 817 | const char *buff, size_t size) |
| 818 | { |
| 819 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 820 | struct usb_composite_dev *cdev = dev->cdev; |
| 821 | int enabled = 0; |
| 822 | |
| 823 | sscanf(buff, "%d", &enabled); |
| 824 | if (enabled && !dev->enabled) { |
| 825 | /* update values in composite driver's copy of device descriptor */ |
| 826 | cdev->desc.idVendor = device_desc.idVendor; |
| 827 | cdev->desc.idProduct = device_desc.idProduct; |
| 828 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 829 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 830 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 831 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
| 832 | usb_add_config(cdev, &android_config_driver, |
| 833 | android_bind_config); |
| 834 | usb_gadget_connect(cdev->gadget); |
| 835 | dev->enabled = true; |
| 836 | } else if (!enabled && dev->enabled) { |
| 837 | usb_gadget_disconnect(cdev->gadget); |
| 838 | usb_remove_config(cdev, &android_config_driver); |
| 839 | dev->enabled = false; |
| 840 | } else { |
| 841 | pr_err("android_usb: already %s\n", |
| 842 | dev->enabled ? "enabled" : "disabled"); |
| 843 | } |
| 844 | return size; |
| 845 | } |
| 846 | |
| 847 | static ssize_t state_show(struct device *pdev, struct device_attribute *attr, |
| 848 | char *buf) |
| 849 | { |
| 850 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 851 | struct usb_composite_dev *cdev = dev->cdev; |
| 852 | char *state = "DISCONNECTED"; |
| 853 | unsigned long flags; |
| 854 | |
| 855 | if (!cdev) |
| 856 | goto out; |
| 857 | |
| 858 | spin_lock_irqsave(&cdev->lock, flags); |
| 859 | if (cdev->config) |
| 860 | state = "CONFIGURED"; |
| 861 | else if (dev->connected) |
| 862 | state = "CONNECTED"; |
| 863 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 864 | out: |
| 865 | return sprintf(buf, "%s\n", state); |
| 866 | } |
| 867 | |
| 868 | #define DESCRIPTOR_ATTR(field, format_string) \ |
| 869 | static ssize_t \ |
| 870 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 871 | char *buf) \ |
| 872 | { \ |
| 873 | return sprintf(buf, format_string, device_desc.field); \ |
| 874 | } \ |
| 875 | static ssize_t \ |
| 876 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 877 | const char *buf, size_t size) \ |
| 878 | { \ |
| 879 | int value; \ |
| 880 | if (sscanf(buf, format_string, &value) == 1) { \ |
| 881 | device_desc.field = value; \ |
| 882 | return size; \ |
| 883 | } \ |
| 884 | return -1; \ |
| 885 | } \ |
| 886 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 887 | |
| 888 | #define DESCRIPTOR_STRING_ATTR(field, buffer) \ |
| 889 | static ssize_t \ |
| 890 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 891 | char *buf) \ |
| 892 | { \ |
| 893 | return sprintf(buf, "%s", buffer); \ |
| 894 | } \ |
| 895 | static ssize_t \ |
| 896 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 897 | const char *buf, size_t size) \ |
| 898 | { \ |
| 899 | if (size >= sizeof(buffer)) return -EINVAL; \ |
| 900 | if (sscanf(buf, "%s", buffer) == 1) { \ |
| 901 | return size; \ |
| 902 | } \ |
| 903 | return -1; \ |
| 904 | } \ |
| 905 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 906 | |
| 907 | |
| 908 | DESCRIPTOR_ATTR(idVendor, "%04x\n") |
| 909 | DESCRIPTOR_ATTR(idProduct, "%04x\n") |
| 910 | DESCRIPTOR_ATTR(bcdDevice, "%04x\n") |
| 911 | DESCRIPTOR_ATTR(bDeviceClass, "%d\n") |
| 912 | DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n") |
| 913 | DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n") |
| 914 | DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string) |
| 915 | DESCRIPTOR_STRING_ATTR(iProduct, product_string) |
| 916 | DESCRIPTOR_STRING_ATTR(iSerial, serial_string) |
| 917 | |
| 918 | static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store); |
| 919 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
| 920 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
| 921 | |
| 922 | static struct device_attribute *android_usb_attributes[] = { |
| 923 | &dev_attr_idVendor, |
| 924 | &dev_attr_idProduct, |
| 925 | &dev_attr_bcdDevice, |
| 926 | &dev_attr_bDeviceClass, |
| 927 | &dev_attr_bDeviceSubClass, |
| 928 | &dev_attr_bDeviceProtocol, |
| 929 | &dev_attr_iManufacturer, |
| 930 | &dev_attr_iProduct, |
| 931 | &dev_attr_iSerial, |
| 932 | &dev_attr_functions, |
| 933 | &dev_attr_enable, |
| 934 | &dev_attr_state, |
| 935 | NULL |
| 936 | }; |
| 937 | |
| 938 | /*-------------------------------------------------------------------------*/ |
| 939 | /* Composite driver */ |
| 940 | |
| 941 | static int android_bind_config(struct usb_configuration *c) |
| 942 | { |
| 943 | struct android_dev *dev = _android_dev; |
| 944 | int ret = 0; |
| 945 | |
| 946 | ret = android_bind_enabled_functions(dev, c); |
| 947 | if (ret) |
| 948 | return ret; |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static void android_unbind_config(struct usb_configuration *c) |
| 954 | { |
| 955 | struct android_dev *dev = _android_dev; |
| 956 | |
| 957 | android_unbind_enabled_functions(dev, c); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 958 | } |
| 959 | |
Dmitry Shmidt | 577e37a | 2010-07-02 12:46:34 -0700 | [diff] [blame] | 960 | static int android_bind(struct usb_composite_dev *cdev) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 961 | { |
| 962 | struct android_dev *dev = _android_dev; |
| 963 | struct usb_gadget *gadget = cdev->gadget; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 964 | int gcnum, id, ret; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 965 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 966 | usb_gadget_disconnect(gadget); |
| 967 | |
| 968 | ret = android_init_functions(dev->functions, cdev); |
| 969 | if (ret) |
| 970 | return ret; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 971 | |
| 972 | /* Allocate string descriptor numbers ... note that string |
| 973 | * contents can be overridden by the composite_dev glue. |
| 974 | */ |
| 975 | id = usb_string_id(cdev); |
| 976 | if (id < 0) |
| 977 | return id; |
| 978 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
| 979 | device_desc.iManufacturer = id; |
| 980 | |
| 981 | id = usb_string_id(cdev); |
| 982 | if (id < 0) |
| 983 | return id; |
| 984 | strings_dev[STRING_PRODUCT_IDX].id = id; |
| 985 | device_desc.iProduct = id; |
| 986 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 987 | /* Default strings - should be updated by userspace */ |
| 988 | strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1); |
| 989 | strncpy(product_string, "Android", sizeof(product_string) - 1); |
| 990 | strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1); |
| 991 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 992 | id = usb_string_id(cdev); |
| 993 | if (id < 0) |
| 994 | return id; |
| 995 | strings_dev[STRING_SERIAL_IDX].id = id; |
| 996 | device_desc.iSerialNumber = id; |
| 997 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 998 | gcnum = usb_gadget_controller_number(gadget); |
| 999 | if (gcnum >= 0) |
| 1000 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
| 1001 | else { |
| 1002 | /* gadget zero is so simple (for now, no altsettings) that |
| 1003 | * it SHOULD NOT have problems with bulk-capable hardware. |
| 1004 | * so just warn about unrcognized controllers -- don't panic. |
| 1005 | * |
| 1006 | * things like configuration and altsetting numbering |
| 1007 | * can need hardware-specific attention though. |
| 1008 | */ |
| 1009 | pr_warning("%s: controller '%s' not recognized\n", |
| 1010 | longname, gadget->name); |
| 1011 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 1012 | } |
| 1013 | |
| 1014 | usb_gadget_set_selfpowered(gadget); |
| 1015 | dev->cdev = cdev; |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1020 | static int android_usb_unbind(struct usb_composite_dev *cdev) |
| 1021 | { |
| 1022 | struct android_dev *dev = _android_dev; |
| 1023 | |
| 1024 | cancel_work_sync(&dev->work); |
| 1025 | android_cleanup_functions(dev->functions); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1029 | static struct usb_composite_driver android_usb_driver = { |
| 1030 | .name = "android_usb", |
| 1031 | .dev = &device_desc, |
| 1032 | .strings = dev_strings, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1033 | .unbind = android_usb_unbind, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1034 | }; |
| 1035 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1036 | static int |
| 1037 | android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c) |
| 1038 | { |
| 1039 | struct android_dev *dev = _android_dev; |
| 1040 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1041 | struct usb_request *req = cdev->req; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1042 | struct android_usb_function *f; |
| 1043 | int value = -EOPNOTSUPP; |
| 1044 | unsigned long flags; |
| 1045 | |
| 1046 | req->zero = 0; |
| 1047 | req->complete = composite_setup_complete; |
| 1048 | req->length = 0; |
| 1049 | gadget->ep0->driver_data = cdev; |
| 1050 | |
Mike Lockwood | 6c7dd4b | 2011-08-02 11:13:48 -0400 | [diff] [blame] | 1051 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1052 | if (f->ctrlrequest) { |
| 1053 | value = f->ctrlrequest(f, cdev, c); |
| 1054 | if (value >= 0) |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Mike Lockwood | 686d33a | 2011-09-07 09:55:12 -0700 | [diff] [blame] | 1059 | /* Special case the accessory function. |
| 1060 | * It needs to handle control requests before it is enabled. |
| 1061 | */ |
| 1062 | if (value < 0) |
| 1063 | value = acc_ctrlrequest(cdev, c); |
| 1064 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1065 | if (value < 0) |
| 1066 | value = composite_setup(gadget, c); |
| 1067 | |
| 1068 | spin_lock_irqsave(&cdev->lock, flags); |
| 1069 | if (!dev->connected) { |
| 1070 | dev->connected = 1; |
| 1071 | schedule_work(&dev->work); |
| 1072 | } |
| 1073 | else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) { |
| 1074 | schedule_work(&dev->work); |
| 1075 | } |
| 1076 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1077 | |
| 1078 | return value; |
| 1079 | } |
| 1080 | |
| 1081 | static void android_disconnect(struct usb_gadget *gadget) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1082 | { |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1083 | struct android_dev *dev = _android_dev; |
Dima Zavin | 21bad75 | 2011-09-14 11:53:11 -0700 | [diff] [blame^] | 1084 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1085 | unsigned long flags; |
| 1086 | |
| 1087 | composite_disconnect(gadget); |
| 1088 | |
| 1089 | spin_lock_irqsave(&cdev->lock, flags); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1090 | dev->connected = 0; |
| 1091 | schedule_work(&dev->work); |
Dima Zavin | 21bad75 | 2011-09-14 11:53:11 -0700 | [diff] [blame^] | 1092 | spin_unlock_irqrestore(&cdev->lock, flags); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1093 | } |
| 1094 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1095 | static int android_create_device(struct android_dev *dev) |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1096 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1097 | struct device_attribute **attrs = android_usb_attributes; |
| 1098 | struct device_attribute *attr; |
| 1099 | int err; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1100 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1101 | dev->dev = device_create(android_class, NULL, |
| 1102 | MKDEV(0, 0), NULL, "android0"); |
| 1103 | if (IS_ERR(dev->dev)) |
| 1104 | return PTR_ERR(dev->dev); |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1105 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1106 | dev_set_drvdata(dev->dev, dev); |
| 1107 | |
| 1108 | while ((attr = *attrs++)) { |
| 1109 | err = device_create_file(dev->dev, attr); |
| 1110 | if (err) { |
| 1111 | device_destroy(android_class, dev->dev->devt); |
| 1112 | return err; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1113 | } |
| 1114 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1115 | return 0; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1118 | |
| 1119 | static int __init init(void) |
| 1120 | { |
| 1121 | struct android_dev *dev; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1122 | int err; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1123 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1124 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 1125 | if (IS_ERR(android_class)) |
| 1126 | return PTR_ERR(android_class); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1127 | |
| 1128 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1129 | if (!dev) |
| 1130 | return -ENOMEM; |
| 1131 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1132 | dev->functions = supported_functions; |
| 1133 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1134 | INIT_WORK(&dev->work, android_work); |
| 1135 | |
| 1136 | err = android_create_device(dev); |
| 1137 | if (err) { |
| 1138 | class_destroy(android_class); |
| 1139 | kfree(dev); |
| 1140 | return err; |
| 1141 | } |
| 1142 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1143 | _android_dev = dev; |
| 1144 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1145 | /* Override composite driver functions */ |
| 1146 | composite_driver.setup = android_setup; |
| 1147 | composite_driver.disconnect = android_disconnect; |
| 1148 | |
| 1149 | return usb_composite_probe(&android_usb_driver, android_bind); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1150 | } |
| 1151 | module_init(init); |
| 1152 | |
| 1153 | static void __exit cleanup(void) |
| 1154 | { |
| 1155 | usb_composite_unregister(&android_usb_driver); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1156 | class_destroy(android_class); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1157 | kfree(_android_dev); |
| 1158 | _android_dev = NULL; |
| 1159 | } |
| 1160 | module_exit(cleanup); |