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 | { |
| 323 | strncpy(diag_clients, buff, sizeof(diag_clients)); |
| 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; |
| 349 | int (*notify)(uint32_t, const char *); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | |
| 351 | strncpy(buf, diag_clients, sizeof(buf)); |
| 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 */ |
| 357 | if (!once++) |
| 358 | notify = _android_dev->pdata->update_pid_and_serial_num; |
| 359 | else |
| 360 | notify = NULL; |
| 361 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 362 | if (name) { |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 363 | err = diag_function_add(c, name, notify); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | if (err) |
| 365 | pr_err("diag: Cannot open channel '%s'", name); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return err; |
| 370 | } |
| 371 | |
| 372 | static struct android_usb_function diag_function = { |
| 373 | .name = "diag", |
| 374 | .init = diag_function_init, |
| 375 | .cleanup = diag_function_cleanup, |
| 376 | .bind_config = diag_function_bind_config, |
| 377 | .attributes = diag_function_attributes, |
| 378 | }; |
| 379 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame^] | 380 | /* SERIAL */ |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 381 | static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/ |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 382 | static ssize_t serial_transports_store( |
| 383 | struct device *device, struct device_attribute *attr, |
| 384 | const char *buff, size_t size) |
| 385 | { |
| 386 | strncpy(serial_transports, buff, sizeof(serial_transports)); |
| 387 | |
| 388 | return size; |
| 389 | } |
| 390 | |
| 391 | static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store); |
| 392 | static struct device_attribute *serial_function_attributes[] = |
| 393 | { &dev_attr_transports, NULL }; |
| 394 | |
| 395 | static void serial_function_cleanup(struct android_usb_function *f) |
| 396 | { |
| 397 | gserial_cleanup(); |
| 398 | } |
| 399 | |
| 400 | static int serial_function_bind_config(struct android_usb_function *f, |
| 401 | struct usb_configuration *c) |
| 402 | { |
| 403 | char *name; |
| 404 | char buf[32], *b; |
| 405 | int err = -1, i; |
| 406 | static int serial_initialized = 0, ports = 0; |
| 407 | |
| 408 | if (serial_initialized) |
| 409 | goto bind_config; |
| 410 | |
| 411 | serial_initialized = 1; |
| 412 | strncpy(buf, serial_transports, sizeof(buf)); |
| 413 | b = strim(buf); |
| 414 | |
| 415 | while (b) { |
| 416 | name = strsep(&b, ","); |
| 417 | |
| 418 | if (name) { |
| 419 | err = gserial_init_port(ports, name); |
| 420 | if (err) { |
| 421 | pr_err("serial: Cannot open port '%s'", name); |
| 422 | goto out; |
| 423 | } |
| 424 | ports++; |
| 425 | } |
| 426 | } |
| 427 | err = gport_setup(c); |
| 428 | if (err) { |
| 429 | pr_err("serial: Cannot setup transports"); |
| 430 | goto out; |
| 431 | } |
| 432 | |
| 433 | bind_config: |
| 434 | for (i = 0; i < ports; i++) { |
| 435 | err = gser_bind_config(c, i); |
| 436 | if (err) { |
| 437 | pr_err("serial: bind_config failed for port %d", i); |
| 438 | goto out; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | out: |
| 443 | return err; |
| 444 | } |
| 445 | |
| 446 | static struct android_usb_function serial_function = { |
| 447 | .name = "serial", |
| 448 | .cleanup = serial_function_cleanup, |
| 449 | .bind_config = serial_function_bind_config, |
| 450 | .attributes = serial_function_attributes, |
| 451 | }; |
| 452 | |
| 453 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame^] | 454 | /* ADB */ |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 455 | static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
| 456 | { |
| 457 | return adb_setup(); |
| 458 | } |
| 459 | |
| 460 | static void adb_function_cleanup(struct android_usb_function *f) |
| 461 | { |
| 462 | adb_cleanup(); |
| 463 | } |
| 464 | |
| 465 | static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c) |
| 466 | { |
| 467 | return adb_bind_config(c); |
| 468 | } |
| 469 | |
| 470 | static struct android_usb_function adb_function = { |
| 471 | .name = "adb", |
| 472 | .init = adb_function_init, |
| 473 | .cleanup = adb_function_cleanup, |
| 474 | .bind_config = adb_function_bind_config, |
| 475 | }; |
| 476 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 477 | #if 0 |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 478 | #define MAX_ACM_INSTANCES 4 |
| 479 | struct acm_function_config { |
| 480 | int instances; |
| 481 | }; |
| 482 | |
| 483 | static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
| 484 | { |
| 485 | f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL); |
| 486 | if (!f->config) |
| 487 | return -ENOMEM; |
| 488 | |
| 489 | return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES); |
| 490 | } |
| 491 | |
| 492 | static void acm_function_cleanup(struct android_usb_function *f) |
| 493 | { |
| 494 | gserial_cleanup(); |
| 495 | kfree(f->config); |
| 496 | f->config = NULL; |
| 497 | } |
| 498 | |
| 499 | 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] | 500 | { |
| 501 | int i; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 502 | int ret = 0; |
| 503 | struct acm_function_config *config = f->config; |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 504 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 505 | for (i = 0; i < config->instances; i++) { |
| 506 | ret = acm_bind_config(c, i); |
| 507 | if (ret) { |
| 508 | pr_err("Could not bind acm%u config\n", i); |
| 509 | break; |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 510 | } |
| 511 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 512 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 513 | return ret; |
| 514 | } |
| 515 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 516 | static ssize_t acm_instances_show(struct device *dev, |
| 517 | struct device_attribute *attr, char *buf) |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 518 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 519 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 520 | struct acm_function_config *config = f->config; |
| 521 | return sprintf(buf, "%d\n", config->instances); |
| 522 | } |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 523 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 524 | static ssize_t acm_instances_store(struct device *dev, |
| 525 | struct device_attribute *attr, const char *buf, size_t size) |
| 526 | { |
| 527 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 528 | struct acm_function_config *config = f->config; |
| 529 | int value; |
| 530 | |
| 531 | sscanf(buf, "%d", &value); |
| 532 | if (value > MAX_ACM_INSTANCES) |
| 533 | value = MAX_ACM_INSTANCES; |
| 534 | config->instances = value; |
| 535 | return size; |
| 536 | } |
| 537 | |
| 538 | static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store); |
| 539 | static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL }; |
| 540 | |
| 541 | static struct android_usb_function acm_function = { |
| 542 | .name = "acm", |
| 543 | .init = acm_function_init, |
| 544 | .cleanup = acm_function_cleanup, |
| 545 | .bind_config = acm_function_bind_config, |
| 546 | .attributes = acm_function_attributes, |
| 547 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 548 | #endif |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 549 | |
| 550 | static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
| 551 | { |
| 552 | return mtp_setup(); |
| 553 | } |
| 554 | |
| 555 | static void mtp_function_cleanup(struct android_usb_function *f) |
| 556 | { |
| 557 | mtp_cleanup(); |
| 558 | } |
| 559 | |
| 560 | static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c) |
| 561 | { |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 562 | return mtp_bind_config(c, false); |
| 563 | } |
| 564 | |
| 565 | static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
| 566 | { |
| 567 | /* nothing to do - initialization is handled by mtp_function_init */ |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static void ptp_function_cleanup(struct android_usb_function *f) |
| 572 | { |
| 573 | /* nothing to do - cleanup is handled by mtp_function_cleanup */ |
| 574 | } |
| 575 | |
| 576 | static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c) |
| 577 | { |
| 578 | return mtp_bind_config(c, true); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static int mtp_function_ctrlrequest(struct android_usb_function *f, |
| 582 | struct usb_composite_dev *cdev, |
| 583 | const struct usb_ctrlrequest *c) |
| 584 | { |
| 585 | return mtp_ctrlrequest(cdev, c); |
| 586 | } |
| 587 | |
| 588 | static struct android_usb_function mtp_function = { |
| 589 | .name = "mtp", |
| 590 | .init = mtp_function_init, |
| 591 | .cleanup = mtp_function_cleanup, |
| 592 | .bind_config = mtp_function_bind_config, |
| 593 | .ctrlrequest = mtp_function_ctrlrequest, |
| 594 | }; |
| 595 | |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 596 | /* PTP function is same as MTP with slightly different interface descriptor */ |
| 597 | static struct android_usb_function ptp_function = { |
| 598 | .name = "ptp", |
| 599 | .init = ptp_function_init, |
| 600 | .cleanup = ptp_function_cleanup, |
| 601 | .bind_config = ptp_function_bind_config, |
| 602 | }; |
| 603 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 604 | |
| 605 | struct rndis_function_config { |
| 606 | u8 ethaddr[ETH_ALEN]; |
| 607 | u32 vendorID; |
| 608 | char manufacturer[256]; |
| 609 | bool wceis; |
| 610 | }; |
| 611 | |
| 612 | static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
| 613 | { |
Manu Gautam | 6925d5c | 2011-09-27 17:04:37 +0530 | [diff] [blame] | 614 | int ret; |
| 615 | struct rndis_function_config *rndis; |
| 616 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 617 | f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL); |
| 618 | if (!f->config) |
| 619 | return -ENOMEM; |
Manu Gautam | 6925d5c | 2011-09-27 17:04:37 +0530 | [diff] [blame] | 620 | |
| 621 | rndis = f->config; |
| 622 | ret = gether_setup_name(cdev->gadget, rndis->ethaddr, "usb"); |
| 623 | if (ret) { |
| 624 | pr_err("%s: gether_setup failed\n", __func__); |
| 625 | return ret; |
| 626 | } |
| 627 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static void rndis_function_cleanup(struct android_usb_function *f) |
| 632 | { |
Manu Gautam | 6925d5c | 2011-09-27 17:04:37 +0530 | [diff] [blame] | 633 | gether_cleanup(); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 634 | kfree(f->config); |
| 635 | f->config = NULL; |
| 636 | } |
| 637 | |
| 638 | static int rndis_function_bind_config(struct android_usb_function *f, |
| 639 | struct usb_configuration *c) |
| 640 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 641 | struct rndis_function_config *rndis = f->config; |
| 642 | |
| 643 | if (!rndis) { |
| 644 | pr_err("%s: rndis_pdata\n", __func__); |
| 645 | return -1; |
| 646 | } |
| 647 | |
| 648 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 649 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 650 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 651 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 652 | if (rndis->wceis) { |
| 653 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 654 | rndis_iad_descriptor.bFunctionClass = |
| 655 | USB_CLASS_WIRELESS_CONTROLLER; |
| 656 | rndis_iad_descriptor.bFunctionSubClass = 0x01; |
| 657 | rndis_iad_descriptor.bFunctionProtocol = 0x03; |
| 658 | rndis_control_intf.bInterfaceClass = |
| 659 | USB_CLASS_WIRELESS_CONTROLLER; |
| 660 | rndis_control_intf.bInterfaceSubClass = 0x01; |
| 661 | rndis_control_intf.bInterfaceProtocol = 0x03; |
| 662 | } |
| 663 | |
| 664 | return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID, |
| 665 | rndis->manufacturer); |
| 666 | } |
| 667 | |
| 668 | static void rndis_function_unbind_config(struct android_usb_function *f, |
| 669 | struct usb_configuration *c) |
| 670 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static ssize_t rndis_manufacturer_show(struct device *dev, |
| 674 | struct device_attribute *attr, char *buf) |
| 675 | { |
| 676 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 677 | struct rndis_function_config *config = f->config; |
| 678 | return sprintf(buf, "%s\n", config->manufacturer); |
| 679 | } |
| 680 | |
| 681 | static ssize_t rndis_manufacturer_store(struct device *dev, |
| 682 | struct device_attribute *attr, const char *buf, size_t size) |
| 683 | { |
| 684 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 685 | struct rndis_function_config *config = f->config; |
| 686 | |
| 687 | if (size >= sizeof(config->manufacturer)) |
| 688 | return -EINVAL; |
| 689 | if (sscanf(buf, "%s", config->manufacturer) == 1) |
| 690 | return size; |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show, |
| 695 | rndis_manufacturer_store); |
| 696 | |
| 697 | static ssize_t rndis_wceis_show(struct device *dev, |
| 698 | struct device_attribute *attr, char *buf) |
| 699 | { |
| 700 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 701 | struct rndis_function_config *config = f->config; |
| 702 | return sprintf(buf, "%d\n", config->wceis); |
| 703 | } |
| 704 | |
| 705 | static ssize_t rndis_wceis_store(struct device *dev, |
| 706 | struct device_attribute *attr, const char *buf, size_t size) |
| 707 | { |
| 708 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 709 | struct rndis_function_config *config = f->config; |
| 710 | int value; |
| 711 | |
| 712 | if (sscanf(buf, "%d", &value) == 1) { |
| 713 | config->wceis = value; |
| 714 | return size; |
| 715 | } |
| 716 | return -EINVAL; |
| 717 | } |
| 718 | |
| 719 | static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show, |
| 720 | rndis_wceis_store); |
| 721 | |
| 722 | static ssize_t rndis_ethaddr_show(struct device *dev, |
| 723 | struct device_attribute *attr, char *buf) |
| 724 | { |
| 725 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 726 | struct rndis_function_config *rndis = f->config; |
| 727 | return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 728 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 729 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 730 | } |
| 731 | |
| 732 | static ssize_t rndis_ethaddr_store(struct device *dev, |
| 733 | struct device_attribute *attr, const char *buf, size_t size) |
| 734 | { |
| 735 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 736 | struct rndis_function_config *rndis = f->config; |
| 737 | |
| 738 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 739 | (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1], |
| 740 | (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3], |
| 741 | (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6) |
| 742 | return size; |
| 743 | return -EINVAL; |
| 744 | } |
| 745 | |
| 746 | static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show, |
| 747 | rndis_ethaddr_store); |
| 748 | |
| 749 | static ssize_t rndis_vendorID_show(struct device *dev, |
| 750 | struct device_attribute *attr, char *buf) |
| 751 | { |
| 752 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 753 | struct rndis_function_config *config = f->config; |
| 754 | return sprintf(buf, "%04x\n", config->vendorID); |
| 755 | } |
| 756 | |
| 757 | static ssize_t rndis_vendorID_store(struct device *dev, |
| 758 | struct device_attribute *attr, const char *buf, size_t size) |
| 759 | { |
| 760 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 761 | struct rndis_function_config *config = f->config; |
| 762 | int value; |
| 763 | |
| 764 | if (sscanf(buf, "%04x", &value) == 1) { |
| 765 | config->vendorID = value; |
| 766 | return size; |
| 767 | } |
| 768 | return -EINVAL; |
| 769 | } |
| 770 | |
| 771 | static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show, |
| 772 | rndis_vendorID_store); |
| 773 | |
| 774 | static struct device_attribute *rndis_function_attributes[] = { |
| 775 | &dev_attr_manufacturer, |
| 776 | &dev_attr_wceis, |
| 777 | &dev_attr_ethaddr, |
| 778 | &dev_attr_vendorID, |
| 779 | NULL |
| 780 | }; |
| 781 | |
| 782 | static struct android_usb_function rndis_function = { |
| 783 | .name = "rndis", |
| 784 | .init = rndis_function_init, |
| 785 | .cleanup = rndis_function_cleanup, |
| 786 | .bind_config = rndis_function_bind_config, |
| 787 | .unbind_config = rndis_function_unbind_config, |
| 788 | .attributes = rndis_function_attributes, |
| 789 | }; |
| 790 | |
| 791 | |
| 792 | struct mass_storage_function_config { |
| 793 | struct fsg_config fsg; |
| 794 | struct fsg_common *common; |
| 795 | }; |
| 796 | |
| 797 | static int mass_storage_function_init(struct android_usb_function *f, |
| 798 | struct usb_composite_dev *cdev) |
| 799 | { |
| 800 | struct mass_storage_function_config *config; |
| 801 | struct fsg_common *common; |
| 802 | int err; |
| 803 | |
| 804 | config = kzalloc(sizeof(struct mass_storage_function_config), |
| 805 | GFP_KERNEL); |
| 806 | if (!config) |
| 807 | return -ENOMEM; |
| 808 | |
| 809 | config->fsg.nluns = 1; |
| 810 | config->fsg.luns[0].removable = 1; |
| 811 | |
| 812 | common = fsg_common_init(NULL, cdev, &config->fsg); |
| 813 | if (IS_ERR(common)) { |
| 814 | kfree(config); |
| 815 | return PTR_ERR(common); |
| 816 | } |
| 817 | |
| 818 | err = sysfs_create_link(&f->dev->kobj, |
| 819 | &common->luns[0].dev.kobj, |
| 820 | "lun"); |
| 821 | if (err) { |
| 822 | kfree(config); |
| 823 | return err; |
| 824 | } |
| 825 | |
| 826 | config->common = common; |
| 827 | f->config = config; |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static void mass_storage_function_cleanup(struct android_usb_function *f) |
| 832 | { |
| 833 | kfree(f->config); |
| 834 | f->config = NULL; |
| 835 | } |
| 836 | |
| 837 | static int mass_storage_function_bind_config(struct android_usb_function *f, |
| 838 | struct usb_configuration *c) |
| 839 | { |
| 840 | struct mass_storage_function_config *config = f->config; |
| 841 | return fsg_bind_config(c->cdev, c, config->common); |
| 842 | } |
| 843 | |
| 844 | static ssize_t mass_storage_inquiry_show(struct device *dev, |
| 845 | struct device_attribute *attr, char *buf) |
| 846 | { |
| 847 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 848 | struct mass_storage_function_config *config = f->config; |
| 849 | return sprintf(buf, "%s\n", config->common->inquiry_string); |
| 850 | } |
| 851 | |
| 852 | static ssize_t mass_storage_inquiry_store(struct device *dev, |
| 853 | struct device_attribute *attr, const char *buf, size_t size) |
| 854 | { |
| 855 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 856 | struct mass_storage_function_config *config = f->config; |
| 857 | if (size >= sizeof(config->common->inquiry_string)) |
| 858 | return -EINVAL; |
| 859 | if (sscanf(buf, "%s", config->common->inquiry_string) != 1) |
| 860 | return -EINVAL; |
| 861 | return size; |
| 862 | } |
| 863 | |
| 864 | static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR, |
| 865 | mass_storage_inquiry_show, |
| 866 | mass_storage_inquiry_store); |
| 867 | |
| 868 | static struct device_attribute *mass_storage_function_attributes[] = { |
| 869 | &dev_attr_inquiry_string, |
| 870 | NULL |
| 871 | }; |
| 872 | |
| 873 | static struct android_usb_function mass_storage_function = { |
| 874 | .name = "mass_storage", |
| 875 | .init = mass_storage_function_init, |
| 876 | .cleanup = mass_storage_function_cleanup, |
| 877 | .bind_config = mass_storage_function_bind_config, |
| 878 | .attributes = mass_storage_function_attributes, |
| 879 | }; |
| 880 | |
| 881 | |
| 882 | static int accessory_function_init(struct android_usb_function *f, |
| 883 | struct usb_composite_dev *cdev) |
| 884 | { |
| 885 | return acc_setup(); |
| 886 | } |
| 887 | |
| 888 | static void accessory_function_cleanup(struct android_usb_function *f) |
| 889 | { |
| 890 | acc_cleanup(); |
| 891 | } |
| 892 | |
| 893 | static int accessory_function_bind_config(struct android_usb_function *f, |
| 894 | struct usb_configuration *c) |
| 895 | { |
| 896 | return acc_bind_config(c); |
| 897 | } |
| 898 | |
| 899 | static int accessory_function_ctrlrequest(struct android_usb_function *f, |
| 900 | struct usb_composite_dev *cdev, |
| 901 | const struct usb_ctrlrequest *c) |
| 902 | { |
| 903 | return acc_ctrlrequest(cdev, c); |
| 904 | } |
| 905 | |
| 906 | static struct android_usb_function accessory_function = { |
| 907 | .name = "accessory", |
| 908 | .init = accessory_function_init, |
| 909 | .cleanup = accessory_function_cleanup, |
| 910 | .bind_config = accessory_function_bind_config, |
| 911 | .ctrlrequest = accessory_function_ctrlrequest, |
| 912 | }; |
| 913 | |
| 914 | |
| 915 | static struct android_usb_function *supported_functions[] = { |
Manu Gautam | 1c8ffd7 | 2011-09-02 16:00:49 +0530 | [diff] [blame] | 916 | &rmnet_smd_function, |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame^] | 917 | &rmnet_sdio_function, |
| 918 | &rmnet_smd_sdio_function, |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 919 | &rmnet_function, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 920 | &diag_function, |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 921 | &serial_function, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 922 | &adb_function, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 923 | // &acm_function, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 924 | &mtp_function, |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 925 | &ptp_function, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 926 | &rndis_function, |
| 927 | &mass_storage_function, |
| 928 | &accessory_function, |
| 929 | NULL |
| 930 | }; |
| 931 | |
| 932 | |
| 933 | static int android_init_functions(struct android_usb_function **functions, |
| 934 | struct usb_composite_dev *cdev) |
| 935 | { |
| 936 | struct android_dev *dev = _android_dev; |
| 937 | struct android_usb_function *f; |
| 938 | struct device_attribute **attrs; |
| 939 | struct device_attribute *attr; |
| 940 | int err; |
| 941 | int index = 0; |
| 942 | |
| 943 | for (; (f = *functions++); index++) { |
| 944 | f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name); |
| 945 | f->dev = device_create(android_class, dev->dev, |
| 946 | MKDEV(0, index), f, f->dev_name); |
| 947 | if (IS_ERR(f->dev)) { |
| 948 | pr_err("%s: Failed to create dev %s", __func__, |
| 949 | f->dev_name); |
| 950 | err = PTR_ERR(f->dev); |
| 951 | goto err_create; |
| 952 | } |
| 953 | |
| 954 | if (f->init) { |
| 955 | err = f->init(f, cdev); |
| 956 | if (err) { |
| 957 | pr_err("%s: Failed to init %s", __func__, |
| 958 | f->name); |
| 959 | goto err_out; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | attrs = f->attributes; |
| 964 | if (attrs) { |
| 965 | while ((attr = *attrs++) && !err) |
| 966 | err = device_create_file(f->dev, attr); |
| 967 | } |
| 968 | if (err) { |
| 969 | pr_err("%s: Failed to create function %s attributes", |
| 970 | __func__, f->name); |
| 971 | goto err_out; |
| 972 | } |
| 973 | } |
| 974 | return 0; |
| 975 | |
| 976 | err_out: |
| 977 | device_destroy(android_class, f->dev->devt); |
| 978 | err_create: |
| 979 | kfree(f->dev_name); |
| 980 | return err; |
| 981 | } |
| 982 | |
| 983 | static void android_cleanup_functions(struct android_usb_function **functions) |
| 984 | { |
| 985 | struct android_usb_function *f; |
| 986 | |
| 987 | while (*functions) { |
| 988 | f = *functions++; |
| 989 | |
| 990 | if (f->dev) { |
| 991 | device_destroy(android_class, f->dev->devt); |
| 992 | kfree(f->dev_name); |
| 993 | } |
| 994 | |
| 995 | if (f->cleanup) |
| 996 | f->cleanup(f); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | static int |
| 1001 | android_bind_enabled_functions(struct android_dev *dev, |
| 1002 | struct usb_configuration *c) |
| 1003 | { |
| 1004 | struct android_usb_function *f; |
| 1005 | int ret; |
| 1006 | |
| 1007 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1008 | ret = f->bind_config(f, c); |
| 1009 | if (ret) { |
| 1010 | pr_err("%s: %s failed", __func__, f->name); |
| 1011 | return ret; |
| 1012 | } |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1013 | } |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1017 | static void |
| 1018 | android_unbind_enabled_functions(struct android_dev *dev, |
| 1019 | struct usb_configuration *c) |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1020 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1021 | struct android_usb_function *f; |
| 1022 | |
| 1023 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1024 | if (f->unbind_config) |
| 1025 | f->unbind_config(f, c); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static int android_enable_function(struct android_dev *dev, char *name) |
| 1030 | { |
| 1031 | struct android_usb_function **functions = dev->functions; |
| 1032 | struct android_usb_function *f; |
| 1033 | while ((f = *functions++)) { |
| 1034 | if (!strcmp(name, f->name)) { |
| 1035 | list_add_tail(&f->enabled_list, &dev->enabled_functions); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1036 | return 0; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 1037 | } |
| 1038 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1039 | return -EINVAL; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 1040 | } |
| 1041 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1042 | /*-------------------------------------------------------------------------*/ |
| 1043 | /* /sys/class/android_usb/android%d/ interface */ |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1044 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1045 | static ssize_t |
| 1046 | functions_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 1047 | { |
| 1048 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1049 | struct android_usb_function *f; |
| 1050 | char *buff = buf; |
| 1051 | |
| 1052 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) |
| 1053 | buff += sprintf(buff, "%s,", f->name); |
| 1054 | if (buff != buf) |
| 1055 | *(buff-1) = '\n'; |
| 1056 | return buff - buf; |
| 1057 | } |
| 1058 | |
| 1059 | static ssize_t |
| 1060 | functions_store(struct device *pdev, struct device_attribute *attr, |
| 1061 | const char *buff, size_t size) |
| 1062 | { |
| 1063 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1064 | char *name; |
| 1065 | char buf[256], *b; |
| 1066 | int err; |
| 1067 | |
| 1068 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1069 | |
| 1070 | strncpy(buf, buff, sizeof(buf)); |
| 1071 | b = strim(buf); |
| 1072 | |
| 1073 | while (b) { |
| 1074 | name = strsep(&b, ","); |
| 1075 | if (name) { |
| 1076 | err = android_enable_function(dev, name); |
| 1077 | if (err) |
| 1078 | pr_err("android_usb: Cannot enable '%s'", name); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1079 | } |
| 1080 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1081 | |
| 1082 | return size; |
| 1083 | } |
| 1084 | |
| 1085 | static ssize_t enable_show(struct device *pdev, struct device_attribute *attr, |
| 1086 | char *buf) |
| 1087 | { |
| 1088 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1089 | return sprintf(buf, "%d\n", dev->enabled); |
| 1090 | } |
| 1091 | |
| 1092 | static ssize_t enable_store(struct device *pdev, struct device_attribute *attr, |
| 1093 | const char *buff, size_t size) |
| 1094 | { |
| 1095 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1096 | struct usb_composite_dev *cdev = dev->cdev; |
| 1097 | int enabled = 0; |
| 1098 | |
| 1099 | sscanf(buff, "%d", &enabled); |
| 1100 | if (enabled && !dev->enabled) { |
| 1101 | /* update values in composite driver's copy of device descriptor */ |
| 1102 | cdev->desc.idVendor = device_desc.idVendor; |
| 1103 | cdev->desc.idProduct = device_desc.idProduct; |
| 1104 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 1105 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 1106 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 1107 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
| 1108 | usb_add_config(cdev, &android_config_driver, |
| 1109 | android_bind_config); |
| 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: |
| 1141 | return sprintf(buf, "%s\n", state); |
| 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 | { \ |
| 1149 | return sprintf(buf, format_string, device_desc.field); \ |
| 1150 | } \ |
| 1151 | static ssize_t \ |
| 1152 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 1153 | const char *buf, size_t size) \ |
| 1154 | { \ |
| 1155 | int value; \ |
| 1156 | if (sscanf(buf, format_string, &value) == 1) { \ |
| 1157 | device_desc.field = value; \ |
| 1158 | return size; \ |
| 1159 | } \ |
| 1160 | return -1; \ |
| 1161 | } \ |
| 1162 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 1163 | |
| 1164 | #define DESCRIPTOR_STRING_ATTR(field, buffer) \ |
| 1165 | static ssize_t \ |
| 1166 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 1167 | char *buf) \ |
| 1168 | { \ |
| 1169 | return sprintf(buf, "%s", buffer); \ |
| 1170 | } \ |
| 1171 | static ssize_t \ |
| 1172 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 1173 | const char *buf, size_t size) \ |
| 1174 | { \ |
| 1175 | if (size >= sizeof(buffer)) return -EINVAL; \ |
| 1176 | if (sscanf(buf, "%s", buffer) == 1) { \ |
| 1177 | return size; \ |
| 1178 | } \ |
| 1179 | return -1; \ |
| 1180 | } \ |
| 1181 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 1182 | |
| 1183 | |
| 1184 | DESCRIPTOR_ATTR(idVendor, "%04x\n") |
| 1185 | DESCRIPTOR_ATTR(idProduct, "%04x\n") |
| 1186 | DESCRIPTOR_ATTR(bcdDevice, "%04x\n") |
| 1187 | DESCRIPTOR_ATTR(bDeviceClass, "%d\n") |
| 1188 | DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n") |
| 1189 | DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n") |
| 1190 | DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string) |
| 1191 | DESCRIPTOR_STRING_ATTR(iProduct, product_string) |
| 1192 | DESCRIPTOR_STRING_ATTR(iSerial, serial_string) |
| 1193 | |
| 1194 | static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store); |
| 1195 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
| 1196 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
| 1197 | |
| 1198 | static struct device_attribute *android_usb_attributes[] = { |
| 1199 | &dev_attr_idVendor, |
| 1200 | &dev_attr_idProduct, |
| 1201 | &dev_attr_bcdDevice, |
| 1202 | &dev_attr_bDeviceClass, |
| 1203 | &dev_attr_bDeviceSubClass, |
| 1204 | &dev_attr_bDeviceProtocol, |
| 1205 | &dev_attr_iManufacturer, |
| 1206 | &dev_attr_iProduct, |
| 1207 | &dev_attr_iSerial, |
| 1208 | &dev_attr_functions, |
| 1209 | &dev_attr_enable, |
| 1210 | &dev_attr_state, |
| 1211 | NULL |
| 1212 | }; |
| 1213 | |
| 1214 | /*-------------------------------------------------------------------------*/ |
| 1215 | /* Composite driver */ |
| 1216 | |
| 1217 | static int android_bind_config(struct usb_configuration *c) |
| 1218 | { |
| 1219 | struct android_dev *dev = _android_dev; |
| 1220 | int ret = 0; |
| 1221 | |
| 1222 | ret = android_bind_enabled_functions(dev, c); |
| 1223 | if (ret) |
| 1224 | return ret; |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | static void android_unbind_config(struct usb_configuration *c) |
| 1230 | { |
| 1231 | struct android_dev *dev = _android_dev; |
| 1232 | |
| 1233 | android_unbind_enabled_functions(dev, c); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1234 | } |
| 1235 | |
Dmitry Shmidt | 577e37a | 2010-07-02 12:46:34 -0700 | [diff] [blame] | 1236 | static int android_bind(struct usb_composite_dev *cdev) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1237 | { |
| 1238 | struct android_dev *dev = _android_dev; |
| 1239 | struct usb_gadget *gadget = cdev->gadget; |
Mike Lockwood | aecca43 | 2011-02-09 09:38:26 -0500 | [diff] [blame] | 1240 | int gcnum, id, ret; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1241 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1242 | usb_gadget_disconnect(gadget); |
| 1243 | |
| 1244 | ret = android_init_functions(dev->functions, cdev); |
| 1245 | if (ret) |
| 1246 | return ret; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1247 | |
| 1248 | /* Allocate string descriptor numbers ... note that string |
| 1249 | * contents can be overridden by the composite_dev glue. |
| 1250 | */ |
| 1251 | id = usb_string_id(cdev); |
| 1252 | if (id < 0) |
| 1253 | return id; |
| 1254 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
| 1255 | device_desc.iManufacturer = id; |
| 1256 | |
| 1257 | id = usb_string_id(cdev); |
| 1258 | if (id < 0) |
| 1259 | return id; |
| 1260 | strings_dev[STRING_PRODUCT_IDX].id = id; |
| 1261 | device_desc.iProduct = id; |
| 1262 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1263 | /* Default strings - should be updated by userspace */ |
| 1264 | strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1); |
| 1265 | strncpy(product_string, "Android", sizeof(product_string) - 1); |
| 1266 | strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1); |
| 1267 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1268 | id = usb_string_id(cdev); |
| 1269 | if (id < 0) |
| 1270 | return id; |
| 1271 | strings_dev[STRING_SERIAL_IDX].id = id; |
| 1272 | device_desc.iSerialNumber = id; |
| 1273 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1274 | gcnum = usb_gadget_controller_number(gadget); |
| 1275 | if (gcnum >= 0) |
| 1276 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
| 1277 | else { |
| 1278 | /* gadget zero is so simple (for now, no altsettings) that |
| 1279 | * it SHOULD NOT have problems with bulk-capable hardware. |
| 1280 | * so just warn about unrcognized controllers -- don't panic. |
| 1281 | * |
| 1282 | * things like configuration and altsetting numbering |
| 1283 | * can need hardware-specific attention though. |
| 1284 | */ |
| 1285 | pr_warning("%s: controller '%s' not recognized\n", |
| 1286 | longname, gadget->name); |
| 1287 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 1288 | } |
| 1289 | |
| 1290 | usb_gadget_set_selfpowered(gadget); |
| 1291 | dev->cdev = cdev; |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1296 | static int android_usb_unbind(struct usb_composite_dev *cdev) |
| 1297 | { |
| 1298 | struct android_dev *dev = _android_dev; |
| 1299 | |
| 1300 | cancel_work_sync(&dev->work); |
| 1301 | android_cleanup_functions(dev->functions); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1305 | static struct usb_composite_driver android_usb_driver = { |
| 1306 | .name = "android_usb", |
| 1307 | .dev = &device_desc, |
| 1308 | .strings = dev_strings, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1309 | .unbind = android_usb_unbind, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1310 | }; |
| 1311 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1312 | static int |
| 1313 | android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c) |
| 1314 | { |
| 1315 | struct android_dev *dev = _android_dev; |
| 1316 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1317 | struct usb_request *req = cdev->req; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1318 | struct android_usb_function *f; |
| 1319 | int value = -EOPNOTSUPP; |
| 1320 | unsigned long flags; |
| 1321 | |
| 1322 | req->zero = 0; |
| 1323 | req->complete = composite_setup_complete; |
| 1324 | req->length = 0; |
| 1325 | gadget->ep0->driver_data = cdev; |
| 1326 | |
Mike Lockwood | 6c7dd4b | 2011-08-02 11:13:48 -0400 | [diff] [blame] | 1327 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1328 | if (f->ctrlrequest) { |
| 1329 | value = f->ctrlrequest(f, cdev, c); |
| 1330 | if (value >= 0) |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (value < 0) |
| 1336 | value = composite_setup(gadget, c); |
| 1337 | |
| 1338 | spin_lock_irqsave(&cdev->lock, flags); |
| 1339 | if (!dev->connected) { |
| 1340 | dev->connected = 1; |
| 1341 | schedule_work(&dev->work); |
| 1342 | } |
| 1343 | else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) { |
| 1344 | schedule_work(&dev->work); |
| 1345 | } |
| 1346 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1347 | |
| 1348 | return value; |
| 1349 | } |
| 1350 | |
| 1351 | static void android_disconnect(struct usb_gadget *gadget) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1352 | { |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1353 | struct android_dev *dev = _android_dev; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1354 | dev->connected = 0; |
| 1355 | schedule_work(&dev->work); |
| 1356 | composite_disconnect(gadget); |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 1357 | } |
| 1358 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1359 | static int android_create_device(struct android_dev *dev) |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1360 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1361 | struct device_attribute **attrs = android_usb_attributes; |
| 1362 | struct device_attribute *attr; |
| 1363 | int err; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1364 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1365 | dev->dev = device_create(android_class, NULL, |
| 1366 | MKDEV(0, 0), NULL, "android0"); |
| 1367 | if (IS_ERR(dev->dev)) |
| 1368 | return PTR_ERR(dev->dev); |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1369 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1370 | dev_set_drvdata(dev->dev, dev); |
| 1371 | |
| 1372 | while ((attr = *attrs++)) { |
| 1373 | err = device_create_file(dev->dev, attr); |
| 1374 | if (err) { |
| 1375 | device_destroy(android_class, dev->dev->devt); |
| 1376 | return err; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1377 | } |
| 1378 | } |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1379 | return 0; |
John Michelau | d5d2de6 | 2010-12-10 11:33:54 -0600 | [diff] [blame] | 1380 | } |
| 1381 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1382 | static int __devinit android_probe(struct platform_device *pdev) |
| 1383 | { |
| 1384 | struct android_usb_platform_data *pdata = pdev->dev.platform_data; |
| 1385 | struct android_dev *dev = _android_dev; |
| 1386 | |
| 1387 | dev->pdata = pdata; |
| 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | static struct platform_driver android_platform_driver = { |
| 1393 | .driver = { .name = "android_usb"}, |
| 1394 | }; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1395 | |
| 1396 | static int __init init(void) |
| 1397 | { |
| 1398 | struct android_dev *dev; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1399 | int err; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1400 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1401 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 1402 | if (IS_ERR(android_class)) |
| 1403 | return PTR_ERR(android_class); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1404 | |
| 1405 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1406 | if (!dev) |
| 1407 | return -ENOMEM; |
| 1408 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1409 | dev->functions = supported_functions; |
| 1410 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1411 | INIT_WORK(&dev->work, android_work); |
| 1412 | |
| 1413 | err = android_create_device(dev); |
| 1414 | if (err) { |
| 1415 | class_destroy(android_class); |
| 1416 | kfree(dev); |
| 1417 | return err; |
| 1418 | } |
| 1419 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1420 | _android_dev = dev; |
| 1421 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1422 | /* Override composite driver functions */ |
| 1423 | composite_driver.setup = android_setup; |
| 1424 | composite_driver.disconnect = android_disconnect; |
| 1425 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1426 | platform_driver_probe(&android_platform_driver, android_probe); |
| 1427 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1428 | return usb_composite_probe(&android_usb_driver, android_bind); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1429 | } |
| 1430 | module_init(init); |
| 1431 | |
| 1432 | static void __exit cleanup(void) |
| 1433 | { |
| 1434 | usb_composite_unregister(&android_usb_driver); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1435 | class_destroy(android_class); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1436 | kfree(_android_dev); |
| 1437 | _android_dev = NULL; |
| 1438 | } |
| 1439 | module_exit(cleanup); |