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