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