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