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