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