Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Gadget Driver for Android |
| 3 | * |
| 4 | * Copyright (C) 2008 Google, Inc. |
| 5 | * Author: Mike Lockwood <lockwood@android.com> |
| 6 | * Benoit Goby <benoit@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 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/utsname.h> |
| 25 | #include <linux/platform_device.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 26 | #include <linux/pm_qos.h> |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 27 | |
| 28 | #include <linux/usb/ch9.h> |
| 29 | #include <linux/usb/composite.h> |
| 30 | #include <linux/usb/gadget.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | #include <linux/usb/android.h> |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 32 | |
| 33 | #include "gadget_chips.h" |
| 34 | |
| 35 | /* |
| 36 | * Kbuild is not very cooperative with respect to linking separately |
| 37 | * compiled library objects into one module. So for now we won't use |
| 38 | * separate compilation ... ensuring init/exit sections work to shrink |
| 39 | * the runtime footprint, and giving us at least some parts of what |
| 40 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 41 | */ |
| 42 | #include "usbstring.c" |
| 43 | #include "config.c" |
| 44 | #include "epautoconf.c" |
| 45 | #include "composite.c" |
| 46 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | #include "f_diag.c" |
Manu Gautam | 1c8ffd7 | 2011-09-02 16:00:49 +0530 | [diff] [blame] | 48 | #include "f_rmnet_smd.c" |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 49 | #include "f_rmnet_sdio.c" |
| 50 | #include "f_rmnet_smd_sdio.c" |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 51 | #include "f_rmnet.c" |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 52 | #include "f_mass_storage.c" |
| 53 | #include "u_serial.c" |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 54 | #include "u_sdio.c" |
| 55 | #include "u_smd.c" |
| 56 | #include "u_bam.c" |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 57 | #include "u_rmnet_ctrl_smd.c" |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 58 | #include "u_ctrl_hsic.c" |
| 59 | #include "u_data_hsic.c" |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 60 | #include "u_ctrl_hsuart.c" |
| 61 | #include "u_data_hsuart.c" |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 62 | #include "f_serial.c" |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 63 | #include "f_acm.c" |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 64 | #include "f_adb.c" |
Chiranjeevi Velempati | e130fd0 | 2011-11-29 05:06:13 +0530 | [diff] [blame] | 65 | #include "f_ccid.c" |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 66 | #include "f_mtp.c" |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 67 | #include "f_accessory.c" |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 68 | #define USB_ETH_RNDIS y |
| 69 | #include "f_rndis.c" |
| 70 | #include "rndis.c" |
| 71 | #include "u_ether.c" |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 72 | #include "u_bam_data.c" |
| 73 | #include "f_mbim.c" |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 74 | |
| 75 | MODULE_AUTHOR("Mike Lockwood"); |
| 76 | MODULE_DESCRIPTION("Android Composite USB Driver"); |
| 77 | MODULE_LICENSE("GPL"); |
| 78 | MODULE_VERSION("1.0"); |
| 79 | |
| 80 | static const char longname[] = "Gadget Android"; |
| 81 | |
| 82 | /* Default vendor and product IDs, overridden by userspace */ |
| 83 | #define VENDOR_ID 0x18D1 |
| 84 | #define PRODUCT_ID 0x0001 |
| 85 | |
| 86 | struct android_usb_function { |
| 87 | char *name; |
| 88 | void *config; |
| 89 | |
| 90 | struct device *dev; |
| 91 | char *dev_name; |
| 92 | struct device_attribute **attributes; |
| 93 | |
| 94 | /* for android_dev.enabled_functions */ |
| 95 | struct list_head enabled_list; |
| 96 | |
| 97 | /* Optional: initialization during gadget bind */ |
| 98 | int (*init)(struct android_usb_function *, struct usb_composite_dev *); |
| 99 | /* Optional: cleanup during gadget unbind */ |
| 100 | void (*cleanup)(struct android_usb_function *); |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 101 | /* Optional: called when the function is added the list of |
| 102 | * enabled functions */ |
| 103 | void (*enable)(struct android_usb_function *); |
| 104 | /* Optional: called when it is removed */ |
| 105 | void (*disable)(struct android_usb_function *); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 106 | |
| 107 | int (*bind_config)(struct android_usb_function *, |
| 108 | struct usb_configuration *); |
| 109 | |
| 110 | /* Optional: called when the configuration is removed */ |
| 111 | void (*unbind_config)(struct android_usb_function *, |
| 112 | struct usb_configuration *); |
| 113 | /* Optional: handle ctrl requests before the device is configured */ |
| 114 | int (*ctrlrequest)(struct android_usb_function *, |
| 115 | struct usb_composite_dev *, |
| 116 | const struct usb_ctrlrequest *); |
| 117 | }; |
| 118 | |
| 119 | struct android_dev { |
| 120 | struct android_usb_function **functions; |
| 121 | struct list_head enabled_functions; |
| 122 | struct usb_composite_dev *cdev; |
| 123 | struct device *dev; |
| 124 | |
| 125 | bool enabled; |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 126 | int disable_depth; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 127 | struct mutex mutex; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 128 | struct android_usb_platform_data *pdata; |
| 129 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 130 | bool connected; |
| 131 | bool sw_connected; |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 132 | char pm_qos[5]; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 133 | struct pm_qos_request pm_qos_req_dma; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 134 | struct work_struct work; |
| 135 | }; |
| 136 | |
| 137 | static struct class *android_class; |
| 138 | static struct android_dev *_android_dev; |
| 139 | static int android_bind_config(struct usb_configuration *c); |
| 140 | static void android_unbind_config(struct usb_configuration *c); |
| 141 | |
| 142 | /* string IDs are assigned dynamically */ |
| 143 | #define STRING_MANUFACTURER_IDX 0 |
| 144 | #define STRING_PRODUCT_IDX 1 |
| 145 | #define STRING_SERIAL_IDX 2 |
| 146 | |
| 147 | static char manufacturer_string[256]; |
| 148 | static char product_string[256]; |
| 149 | static char serial_string[256]; |
| 150 | |
| 151 | /* String Table */ |
| 152 | static struct usb_string strings_dev[] = { |
| 153 | [STRING_MANUFACTURER_IDX].s = manufacturer_string, |
| 154 | [STRING_PRODUCT_IDX].s = product_string, |
| 155 | [STRING_SERIAL_IDX].s = serial_string, |
| 156 | { } /* end of list */ |
| 157 | }; |
| 158 | |
| 159 | static struct usb_gadget_strings stringtab_dev = { |
| 160 | .language = 0x0409, /* en-us */ |
| 161 | .strings = strings_dev, |
| 162 | }; |
| 163 | |
| 164 | static struct usb_gadget_strings *dev_strings[] = { |
| 165 | &stringtab_dev, |
| 166 | NULL, |
| 167 | }; |
| 168 | |
| 169 | static struct usb_device_descriptor device_desc = { |
| 170 | .bLength = sizeof(device_desc), |
| 171 | .bDescriptorType = USB_DT_DEVICE, |
| 172 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 173 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 174 | .idVendor = __constant_cpu_to_le16(VENDOR_ID), |
| 175 | .idProduct = __constant_cpu_to_le16(PRODUCT_ID), |
| 176 | .bcdDevice = __constant_cpu_to_le16(0xffff), |
| 177 | .bNumConfigurations = 1, |
| 178 | }; |
| 179 | |
Vijayavardhan Vennapusa | 56e6052 | 2012-02-16 15:40:16 +0530 | [diff] [blame] | 180 | static struct usb_otg_descriptor otg_descriptor = { |
| 181 | .bLength = sizeof otg_descriptor, |
| 182 | .bDescriptorType = USB_DT_OTG, |
| 183 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 184 | .bcdOTG = __constant_cpu_to_le16(0x0200), |
| 185 | }; |
| 186 | |
| 187 | static const struct usb_descriptor_header *otg_desc[] = { |
| 188 | (struct usb_descriptor_header *) &otg_descriptor, |
| 189 | NULL, |
| 190 | }; |
| 191 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 192 | static struct usb_configuration android_config_driver = { |
| 193 | .label = "android", |
| 194 | .unbind = android_unbind_config, |
| 195 | .bConfigurationValue = 1, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 198 | enum android_device_state { |
| 199 | USB_DISCONNECTED, |
| 200 | USB_CONNECTED, |
| 201 | USB_CONFIGURED, |
| 202 | }; |
| 203 | |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 204 | static void android_pm_qos_update_latency(struct android_dev *dev, int vote) |
| 205 | { |
| 206 | struct android_usb_platform_data *pdata = dev->pdata; |
| 207 | u32 swfi_latency = 0; |
| 208 | static int last_vote = -1; |
| 209 | |
Ofir Cohen | 56eb707 | 2012-05-20 11:41:39 +0300 | [diff] [blame] | 210 | if (!pdata || vote == last_vote |
| 211 | || !pdata->swfi_latency) |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 212 | return; |
| 213 | |
| 214 | swfi_latency = pdata->swfi_latency + 1; |
| 215 | if (vote) |
| 216 | pm_qos_update_request(&dev->pm_qos_req_dma, |
| 217 | swfi_latency); |
| 218 | else |
| 219 | pm_qos_update_request(&dev->pm_qos_req_dma, |
| 220 | PM_QOS_DEFAULT_VALUE); |
| 221 | last_vote = vote; |
| 222 | } |
| 223 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 224 | static void android_work(struct work_struct *data) |
| 225 | { |
| 226 | struct android_dev *dev = container_of(data, struct android_dev, work); |
| 227 | struct usb_composite_dev *cdev = dev->cdev; |
| 228 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 229 | char *connected[2] = { "USB_STATE=CONNECTED", NULL }; |
| 230 | char *configured[2] = { "USB_STATE=CONFIGURED", NULL }; |
| 231 | char **uevent_envp = NULL; |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 232 | static enum android_device_state last_uevent, next_state; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 233 | unsigned long flags; |
Ofir Cohen | bcbb1a7 | 2012-05-20 16:28:15 +0300 | [diff] [blame] | 234 | int pm_qos_vote = -1; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 235 | |
| 236 | spin_lock_irqsave(&cdev->lock, flags); |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 237 | if (cdev->config) { |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 238 | uevent_envp = configured; |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 239 | next_state = USB_CONFIGURED; |
| 240 | } else if (dev->connected != dev->sw_connected) { |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 241 | uevent_envp = dev->connected ? connected : disconnected; |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 242 | next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED; |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 243 | if (dev->connected && strncmp(dev->pm_qos, "low", 3)) |
Ofir Cohen | bcbb1a7 | 2012-05-20 16:28:15 +0300 | [diff] [blame] | 244 | pm_qos_vote = 1; |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 245 | else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3)) |
Ofir Cohen | bcbb1a7 | 2012-05-20 16:28:15 +0300 | [diff] [blame] | 246 | pm_qos_vote = 0; |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 247 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 248 | dev->sw_connected = dev->connected; |
| 249 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 250 | |
Ofir Cohen | bcbb1a7 | 2012-05-20 16:28:15 +0300 | [diff] [blame] | 251 | if (pm_qos_vote != -1) |
| 252 | android_pm_qos_update_latency(dev, pm_qos_vote); |
| 253 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 254 | if (uevent_envp) { |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 255 | /* |
| 256 | * Some userspace modules, e.g. MTP, work correctly only if |
| 257 | * CONFIGURED uevent is preceded by DISCONNECT uevent. |
| 258 | * Check if we missed sending out a DISCONNECT uevent. This can |
| 259 | * happen if host PC resets and configures device really quick. |
| 260 | */ |
| 261 | if (((uevent_envp == connected) && |
| 262 | (last_uevent != USB_DISCONNECTED)) || |
| 263 | ((uevent_envp == configured) && |
| 264 | (last_uevent == USB_CONFIGURED))) { |
| 265 | pr_info("%s: sent missed DISCONNECT event\n", __func__); |
| 266 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, |
| 267 | disconnected); |
| 268 | msleep(20); |
| 269 | } |
| 270 | /* |
| 271 | * Before sending out CONFIGURED uevent give function drivers |
| 272 | * a chance to wakeup userspace threads and notify disconnect |
| 273 | */ |
| 274 | if (uevent_envp == configured) |
| 275 | msleep(50); |
| 276 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 277 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp); |
Manu Gautam | a2b5414 | 2012-04-03 14:34:32 +0530 | [diff] [blame] | 278 | last_uevent = next_state; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 279 | pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]); |
| 280 | } else { |
| 281 | pr_info("%s: did not send uevent (%d %d %p)\n", __func__, |
| 282 | dev->connected, dev->sw_connected, cdev->config); |
| 283 | } |
| 284 | } |
| 285 | |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 286 | static void android_enable(struct android_dev *dev) |
| 287 | { |
| 288 | struct usb_composite_dev *cdev = dev->cdev; |
| 289 | |
| 290 | if (WARN_ON(!dev->disable_depth)) |
| 291 | return; |
| 292 | |
| 293 | if (--dev->disable_depth == 0) { |
| 294 | usb_add_config(cdev, &android_config_driver, |
| 295 | android_bind_config); |
| 296 | usb_gadget_connect(cdev->gadget); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static void android_disable(struct android_dev *dev) |
| 301 | { |
| 302 | struct usb_composite_dev *cdev = dev->cdev; |
| 303 | |
| 304 | if (dev->disable_depth++ == 0) { |
| 305 | usb_gadget_disconnect(cdev->gadget); |
| 306 | /* Cancel pending control requests */ |
| 307 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); |
| 308 | usb_remove_config(cdev, &android_config_driver); |
| 309 | } |
| 310 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 311 | |
| 312 | /*-------------------------------------------------------------------------*/ |
| 313 | /* Supported functions initialization */ |
| 314 | |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 315 | struct adb_data { |
| 316 | bool opened; |
| 317 | bool enabled; |
| 318 | }; |
| 319 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 320 | static int |
| 321 | adb_function_init(struct android_usb_function *f, |
| 322 | struct usb_composite_dev *cdev) |
| 323 | { |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 324 | f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL); |
| 325 | if (!f->config) |
| 326 | return -ENOMEM; |
| 327 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 328 | return adb_setup(); |
| 329 | } |
| 330 | |
| 331 | static void adb_function_cleanup(struct android_usb_function *f) |
| 332 | { |
| 333 | adb_cleanup(); |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 334 | kfree(f->config); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int |
| 338 | adb_function_bind_config(struct android_usb_function *f, |
| 339 | struct usb_configuration *c) |
| 340 | { |
| 341 | return adb_bind_config(c); |
| 342 | } |
| 343 | |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 344 | static void adb_android_function_enable(struct android_usb_function *f) |
| 345 | { |
| 346 | struct android_dev *dev = _android_dev; |
| 347 | struct adb_data *data = f->config; |
| 348 | |
| 349 | data->enabled = true; |
| 350 | |
| 351 | /* Disable the gadget until adbd is ready */ |
| 352 | if (!data->opened) |
| 353 | android_disable(dev); |
| 354 | } |
| 355 | |
| 356 | static void adb_android_function_disable(struct android_usb_function *f) |
| 357 | { |
| 358 | struct android_dev *dev = _android_dev; |
| 359 | struct adb_data *data = f->config; |
| 360 | |
| 361 | data->enabled = false; |
| 362 | |
| 363 | /* Balance the disable that was called in closed_callback */ |
| 364 | if (!data->opened) |
| 365 | android_enable(dev); |
| 366 | } |
| 367 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 368 | static struct android_usb_function adb_function = { |
| 369 | .name = "adb", |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 370 | .enable = adb_android_function_enable, |
| 371 | .disable = adb_android_function_disable, |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 372 | .init = adb_function_init, |
| 373 | .cleanup = adb_function_cleanup, |
| 374 | .bind_config = adb_function_bind_config, |
| 375 | }; |
| 376 | |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 377 | static void adb_ready_callback(void) |
| 378 | { |
| 379 | struct android_dev *dev = _android_dev; |
| 380 | struct adb_data *data = adb_function.config; |
| 381 | |
| 382 | mutex_lock(&dev->mutex); |
| 383 | |
| 384 | data->opened = true; |
| 385 | |
| 386 | if (data->enabled) |
| 387 | android_enable(dev); |
| 388 | |
| 389 | mutex_unlock(&dev->mutex); |
| 390 | } |
| 391 | |
| 392 | static void adb_closed_callback(void) |
| 393 | { |
| 394 | struct android_dev *dev = _android_dev; |
| 395 | struct adb_data *data = adb_function.config; |
| 396 | |
| 397 | mutex_lock(&dev->mutex); |
| 398 | |
| 399 | data->opened = false; |
| 400 | |
| 401 | if (data->enabled) |
| 402 | android_disable(dev); |
| 403 | |
| 404 | mutex_unlock(&dev->mutex); |
| 405 | } |
| 406 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 407 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 408 | /*-------------------------------------------------------------------------*/ |
| 409 | /* Supported functions initialization */ |
| 410 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 411 | /* RMNET_SMD */ |
Manu Gautam | 1c8ffd7 | 2011-09-02 16:00:49 +0530 | [diff] [blame] | 412 | static int rmnet_smd_function_bind_config(struct android_usb_function *f, |
| 413 | struct usb_configuration *c) |
| 414 | { |
| 415 | return rmnet_smd_bind_config(c); |
| 416 | } |
| 417 | |
| 418 | static struct android_usb_function rmnet_smd_function = { |
| 419 | .name = "rmnet_smd", |
| 420 | .bind_config = rmnet_smd_function_bind_config, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 421 | }; |
| 422 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 423 | /* RMNET_SDIO */ |
| 424 | static int rmnet_sdio_function_bind_config(struct android_usb_function *f, |
| 425 | struct usb_configuration *c) |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 426 | { |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 427 | return rmnet_sdio_function_add(c); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 430 | static struct android_usb_function rmnet_sdio_function = { |
| 431 | .name = "rmnet_sdio", |
| 432 | .bind_config = rmnet_sdio_function_bind_config, |
| 433 | }; |
| 434 | |
| 435 | /* RMNET_SMD_SDIO */ |
| 436 | static int rmnet_smd_sdio_function_init(struct android_usb_function *f, |
| 437 | struct usb_composite_dev *cdev) |
| 438 | { |
| 439 | return rmnet_smd_sdio_init(); |
| 440 | } |
| 441 | |
| 442 | static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f) |
| 443 | { |
| 444 | rmnet_smd_sdio_cleanup(); |
| 445 | } |
| 446 | |
| 447 | static int rmnet_smd_sdio_bind_config(struct android_usb_function *f, |
| 448 | struct usb_configuration *c) |
| 449 | { |
| 450 | return rmnet_smd_sdio_function_add(c); |
| 451 | } |
| 452 | |
| 453 | static struct device_attribute *rmnet_smd_sdio_attributes[] = { |
| 454 | &dev_attr_transport, NULL }; |
| 455 | |
| 456 | static struct android_usb_function rmnet_smd_sdio_function = { |
| 457 | .name = "rmnet_smd_sdio", |
| 458 | .init = rmnet_smd_sdio_function_init, |
| 459 | .cleanup = rmnet_smd_sdio_function_cleanup, |
| 460 | .bind_config = rmnet_smd_sdio_bind_config, |
| 461 | .attributes = rmnet_smd_sdio_attributes, |
| 462 | }; |
| 463 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 464 | /*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */ |
| 465 | #define MAX_XPORT_STR_LEN 50 |
| 466 | static char rmnet_transports[MAX_XPORT_STR_LEN]; |
Manu Gautam | 1c8ffd7 | 2011-09-02 16:00:49 +0530 | [diff] [blame] | 467 | |
Manu Gautam | e3e897c | 2011-09-12 17:18:46 +0530 | [diff] [blame] | 468 | static void rmnet_function_cleanup(struct android_usb_function *f) |
| 469 | { |
| 470 | frmnet_cleanup(); |
| 471 | } |
| 472 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 473 | static int rmnet_function_bind_config(struct android_usb_function *f, |
| 474 | struct usb_configuration *c) |
| 475 | { |
| 476 | int i; |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 477 | int err = 0; |
| 478 | char *ctrl_name; |
| 479 | char *data_name; |
| 480 | char buf[MAX_XPORT_STR_LEN], *b; |
| 481 | static int rmnet_initialized, ports; |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 482 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 483 | if (!rmnet_initialized) { |
| 484 | rmnet_initialized = 1; |
| 485 | strlcpy(buf, rmnet_transports, sizeof(buf)); |
| 486 | b = strim(buf); |
| 487 | while (b) { |
| 488 | ctrl_name = strsep(&b, ","); |
| 489 | data_name = strsep(&b, ","); |
| 490 | if (ctrl_name && data_name) { |
| 491 | err = frmnet_init_port(ctrl_name, data_name); |
| 492 | if (err) { |
| 493 | pr_err("rmnet: Cannot open ctrl port:" |
| 494 | "'%s' data port:'%s'\n", |
| 495 | ctrl_name, data_name); |
| 496 | goto out; |
| 497 | } |
| 498 | ports++; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | err = rmnet_gport_setup(); |
| 503 | if (err) { |
| 504 | pr_err("rmnet: Cannot setup transports"); |
| 505 | goto out; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < ports; i++) { |
| 510 | err = frmnet_bind_config(c, i); |
| 511 | if (err) { |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 512 | pr_err("Could not bind rmnet%u config\n", i); |
| 513 | break; |
| 514 | } |
| 515 | } |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 516 | out: |
| 517 | return err; |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 518 | } |
| 519 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 520 | static ssize_t rmnet_transports_show(struct device *dev, |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 521 | struct device_attribute *attr, char *buf) |
| 522 | { |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 523 | return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports); |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 524 | } |
| 525 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 526 | static ssize_t rmnet_transports_store( |
| 527 | struct device *device, struct device_attribute *attr, |
| 528 | const char *buff, size_t size) |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 529 | { |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 530 | strlcpy(rmnet_transports, buff, sizeof(rmnet_transports)); |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 531 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 532 | return size; |
| 533 | } |
| 534 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 535 | static struct device_attribute dev_attr_rmnet_transports = |
| 536 | __ATTR(transports, S_IRUGO | S_IWUSR, |
| 537 | rmnet_transports_show, |
| 538 | rmnet_transports_store); |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 539 | static struct device_attribute *rmnet_function_attributes[] = { |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 540 | &dev_attr_rmnet_transports, |
| 541 | NULL }; |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 542 | |
| 543 | static struct android_usb_function rmnet_function = { |
| 544 | .name = "rmnet", |
Manu Gautam | e3e897c | 2011-09-12 17:18:46 +0530 | [diff] [blame] | 545 | .cleanup = rmnet_function_cleanup, |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 546 | .bind_config = rmnet_function_bind_config, |
| 547 | .attributes = rmnet_function_attributes, |
| 548 | }; |
| 549 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 550 | |
| 551 | /* MBIM - used with BAM */ |
| 552 | #define MAX_MBIM_INSTANCES 1 |
| 553 | |
| 554 | static int mbim_function_init(struct android_usb_function *f, |
| 555 | struct usb_composite_dev *cdev) |
| 556 | { |
| 557 | return mbim_init(MAX_MBIM_INSTANCES); |
| 558 | } |
| 559 | |
| 560 | static void mbim_function_cleanup(struct android_usb_function *f) |
| 561 | { |
| 562 | fmbim_cleanup(); |
| 563 | } |
| 564 | |
| 565 | static int mbim_function_bind_config(struct android_usb_function *f, |
| 566 | struct usb_configuration *c) |
| 567 | { |
| 568 | return mbim_bind_config(c, 0); |
| 569 | } |
| 570 | |
| 571 | static struct android_usb_function mbim_function = { |
| 572 | .name = "usb_mbim", |
| 573 | .cleanup = mbim_function_cleanup, |
| 574 | .bind_config = mbim_function_bind_config, |
| 575 | .init = mbim_function_init, |
| 576 | }; |
| 577 | |
| 578 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 579 | /* DIAG */ |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 580 | static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 581 | static ssize_t clients_store( |
| 582 | struct device *device, struct device_attribute *attr, |
| 583 | const char *buff, size_t size) |
| 584 | { |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 585 | strlcpy(diag_clients, buff, sizeof(diag_clients)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 586 | |
| 587 | return size; |
| 588 | } |
| 589 | |
| 590 | static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store); |
| 591 | static struct device_attribute *diag_function_attributes[] = |
| 592 | { &dev_attr_clients, NULL }; |
| 593 | |
| 594 | static int diag_function_init(struct android_usb_function *f, |
| 595 | struct usb_composite_dev *cdev) |
| 596 | { |
| 597 | return diag_setup(); |
| 598 | } |
| 599 | |
| 600 | static void diag_function_cleanup(struct android_usb_function *f) |
| 601 | { |
| 602 | diag_cleanup(); |
| 603 | } |
| 604 | |
| 605 | static int diag_function_bind_config(struct android_usb_function *f, |
| 606 | struct usb_configuration *c) |
| 607 | { |
| 608 | char *name; |
| 609 | char buf[32], *b; |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 610 | int once = 0, err = -1; |
Jack Pham | b830a6c | 2011-12-12 22:35:27 -0800 | [diff] [blame] | 611 | int (*notify)(uint32_t, const char *); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 612 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 613 | strlcpy(buf, diag_clients, sizeof(buf)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 614 | b = strim(buf); |
| 615 | |
| 616 | while (b) { |
Jack Pham | b830a6c | 2011-12-12 22:35:27 -0800 | [diff] [blame] | 617 | notify = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | name = strsep(&b, ","); |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 619 | /* Allow only first diag channel to update pid and serial no */ |
Hemant Kumar | 1136c00 | 2011-10-18 22:04:06 -0700 | [diff] [blame] | 620 | if (_android_dev->pdata && !once++) |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 621 | notify = _android_dev->pdata->update_pid_and_serial_num; |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 622 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 623 | if (name) { |
Manu Gautam | c576030 | 2011-08-25 14:30:24 +0530 | [diff] [blame] | 624 | err = diag_function_add(c, name, notify); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | if (err) |
| 626 | pr_err("diag: Cannot open channel '%s'", name); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | static struct android_usb_function diag_function = { |
| 634 | .name = "diag", |
| 635 | .init = diag_function_init, |
| 636 | .cleanup = diag_function_cleanup, |
| 637 | .bind_config = diag_function_bind_config, |
| 638 | .attributes = diag_function_attributes, |
| 639 | }; |
| 640 | |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 641 | /* SERIAL */ |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 642 | static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/ |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 643 | static ssize_t serial_transports_store( |
| 644 | struct device *device, struct device_attribute *attr, |
| 645 | const char *buff, size_t size) |
| 646 | { |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 647 | strlcpy(serial_transports, buff, sizeof(serial_transports)); |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 648 | |
| 649 | return size; |
| 650 | } |
| 651 | |
| 652 | static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store); |
| 653 | static struct device_attribute *serial_function_attributes[] = |
| 654 | { &dev_attr_transports, NULL }; |
| 655 | |
| 656 | static void serial_function_cleanup(struct android_usb_function *f) |
| 657 | { |
| 658 | gserial_cleanup(); |
| 659 | } |
| 660 | |
| 661 | static int serial_function_bind_config(struct android_usb_function *f, |
| 662 | struct usb_configuration *c) |
| 663 | { |
| 664 | char *name; |
| 665 | char buf[32], *b; |
| 666 | int err = -1, i; |
| 667 | static int serial_initialized = 0, ports = 0; |
| 668 | |
| 669 | if (serial_initialized) |
| 670 | goto bind_config; |
| 671 | |
| 672 | serial_initialized = 1; |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 673 | strlcpy(buf, serial_transports, sizeof(buf)); |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 674 | b = strim(buf); |
| 675 | |
| 676 | while (b) { |
| 677 | name = strsep(&b, ","); |
| 678 | |
| 679 | if (name) { |
| 680 | err = gserial_init_port(ports, name); |
| 681 | if (err) { |
| 682 | pr_err("serial: Cannot open port '%s'", name); |
| 683 | goto out; |
| 684 | } |
| 685 | ports++; |
| 686 | } |
| 687 | } |
| 688 | err = gport_setup(c); |
| 689 | if (err) { |
| 690 | pr_err("serial: Cannot setup transports"); |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | bind_config: |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 695 | for (i = 0; i < ports; i++) { |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 696 | err = gser_bind_config(c, i); |
| 697 | if (err) { |
| 698 | pr_err("serial: bind_config failed for port %d", i); |
| 699 | goto out; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | out: |
| 704 | return err; |
| 705 | } |
| 706 | |
| 707 | static struct android_usb_function serial_function = { |
| 708 | .name = "serial", |
| 709 | .cleanup = serial_function_cleanup, |
| 710 | .bind_config = serial_function_bind_config, |
| 711 | .attributes = serial_function_attributes, |
| 712 | }; |
| 713 | |
Anji jonnala | 92be1b4 | 2011-12-19 09:44:41 +0530 | [diff] [blame] | 714 | /* ACM */ |
| 715 | static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/ |
| 716 | static ssize_t acm_transports_store( |
| 717 | struct device *device, struct device_attribute *attr, |
| 718 | const char *buff, size_t size) |
| 719 | { |
| 720 | strlcpy(acm_transports, buff, sizeof(acm_transports)); |
| 721 | |
| 722 | return size; |
| 723 | } |
| 724 | |
| 725 | static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store); |
| 726 | static struct device_attribute *acm_function_attributes[] = { |
| 727 | &dev_attr_acm_transports, NULL }; |
| 728 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 729 | static void acm_function_cleanup(struct android_usb_function *f) |
| 730 | { |
| 731 | gserial_cleanup(); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Anji jonnala | 92be1b4 | 2011-12-19 09:44:41 +0530 | [diff] [blame] | 734 | static int acm_function_bind_config(struct android_usb_function *f, |
| 735 | struct usb_configuration *c) |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 736 | { |
Anji jonnala | 92be1b4 | 2011-12-19 09:44:41 +0530 | [diff] [blame] | 737 | char *name; |
| 738 | char buf[32], *b; |
| 739 | int err = -1, i; |
| 740 | static int acm_initialized, ports; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 741 | |
Anji jonnala | 92be1b4 | 2011-12-19 09:44:41 +0530 | [diff] [blame] | 742 | if (acm_initialized) |
| 743 | goto bind_config; |
| 744 | |
| 745 | acm_initialized = 1; |
| 746 | strlcpy(buf, acm_transports, sizeof(buf)); |
| 747 | b = strim(buf); |
| 748 | |
| 749 | while (b) { |
| 750 | name = strsep(&b, ","); |
| 751 | |
| 752 | if (name) { |
| 753 | err = acm_init_port(ports, name); |
| 754 | if (err) { |
| 755 | pr_err("acm: Cannot open port '%s'", name); |
| 756 | goto out; |
| 757 | } |
| 758 | ports++; |
| 759 | } |
| 760 | } |
| 761 | err = acm_port_setup(c); |
| 762 | if (err) { |
| 763 | pr_err("acm: Cannot setup transports"); |
| 764 | goto out; |
| 765 | } |
| 766 | |
| 767 | bind_config: |
| 768 | for (i = 0; i < ports; i++) { |
| 769 | err = acm_bind_config(c, i); |
| 770 | if (err) { |
| 771 | pr_err("acm: bind_config failed for port %d", i); |
| 772 | goto out; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Anji jonnala | 92be1b4 | 2011-12-19 09:44:41 +0530 | [diff] [blame] | 776 | out: |
| 777 | return err; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 778 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 779 | static struct android_usb_function acm_function = { |
| 780 | .name = "acm", |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 781 | .cleanup = acm_function_cleanup, |
| 782 | .bind_config = acm_function_bind_config, |
| 783 | .attributes = acm_function_attributes, |
| 784 | }; |
| 785 | |
Chiranjeevi Velempati | e130fd0 | 2011-11-29 05:06:13 +0530 | [diff] [blame] | 786 | /* CCID */ |
| 787 | static int ccid_function_init(struct android_usb_function *f, |
| 788 | struct usb_composite_dev *cdev) |
| 789 | { |
| 790 | return ccid_setup(); |
| 791 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 792 | |
Chiranjeevi Velempati | e130fd0 | 2011-11-29 05:06:13 +0530 | [diff] [blame] | 793 | static void ccid_function_cleanup(struct android_usb_function *f) |
| 794 | { |
| 795 | ccid_cleanup(); |
| 796 | } |
| 797 | |
| 798 | static int ccid_function_bind_config(struct android_usb_function *f, |
| 799 | struct usb_configuration *c) |
| 800 | { |
| 801 | return ccid_bind_config(c); |
| 802 | } |
| 803 | |
| 804 | static struct android_usb_function ccid_function = { |
| 805 | .name = "ccid", |
| 806 | .init = ccid_function_init, |
| 807 | .cleanup = ccid_function_cleanup, |
| 808 | .bind_config = ccid_function_bind_config, |
| 809 | }; |
| 810 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 811 | static int mtp_function_init(struct android_usb_function *f, |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 812 | struct usb_composite_dev *cdev) |
| 813 | { |
| 814 | return mtp_setup(); |
| 815 | } |
| 816 | |
| 817 | static void mtp_function_cleanup(struct android_usb_function *f) |
| 818 | { |
| 819 | mtp_cleanup(); |
| 820 | } |
| 821 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 822 | static int mtp_function_bind_config(struct android_usb_function *f, |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 823 | struct usb_configuration *c) |
| 824 | { |
| 825 | return mtp_bind_config(c, false); |
| 826 | } |
| 827 | |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 828 | static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev) |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 829 | { |
| 830 | /* nothing to do - initialization is handled by mtp_function_init */ |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static void ptp_function_cleanup(struct android_usb_function *f) |
| 835 | { |
| 836 | /* nothing to do - cleanup is handled by mtp_function_cleanup */ |
| 837 | } |
| 838 | |
Mike Lockwood | cf7addf | 2011-06-01 22:17:36 -0400 | [diff] [blame] | 839 | static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c) |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 840 | { |
| 841 | return mtp_bind_config(c, true); |
| 842 | } |
| 843 | |
| 844 | static int mtp_function_ctrlrequest(struct android_usb_function *f, |
| 845 | struct usb_composite_dev *cdev, |
| 846 | const struct usb_ctrlrequest *c) |
| 847 | { |
| 848 | return mtp_ctrlrequest(cdev, c); |
| 849 | } |
| 850 | |
| 851 | static struct android_usb_function mtp_function = { |
| 852 | .name = "mtp", |
| 853 | .init = mtp_function_init, |
| 854 | .cleanup = mtp_function_cleanup, |
| 855 | .bind_config = mtp_function_bind_config, |
| 856 | .ctrlrequest = mtp_function_ctrlrequest, |
| 857 | }; |
| 858 | |
| 859 | /* PTP function is same as MTP with slightly different interface descriptor */ |
| 860 | static struct android_usb_function ptp_function = { |
| 861 | .name = "ptp", |
| 862 | .init = ptp_function_init, |
| 863 | .cleanup = ptp_function_cleanup, |
| 864 | .bind_config = ptp_function_bind_config, |
| 865 | }; |
| 866 | |
| 867 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 868 | struct rndis_function_config { |
| 869 | u8 ethaddr[ETH_ALEN]; |
| 870 | u32 vendorID; |
| 871 | char manufacturer[256]; |
| 872 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 873 | bool wceis; |
| 874 | }; |
| 875 | |
| 876 | static int |
| 877 | rndis_function_init(struct android_usb_function *f, |
| 878 | struct usb_composite_dev *cdev) |
| 879 | { |
| 880 | f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL); |
| 881 | if (!f->config) |
| 882 | return -ENOMEM; |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static void rndis_function_cleanup(struct android_usb_function *f) |
| 887 | { |
| 888 | kfree(f->config); |
| 889 | f->config = NULL; |
| 890 | } |
| 891 | |
| 892 | static int |
| 893 | rndis_function_bind_config(struct android_usb_function *f, |
| 894 | struct usb_configuration *c) |
| 895 | { |
| 896 | int ret; |
| 897 | struct rndis_function_config *rndis = f->config; |
| 898 | |
| 899 | if (!rndis) { |
| 900 | pr_err("%s: rndis_pdata\n", __func__); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 905 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 906 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 907 | |
| 908 | ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis"); |
| 909 | if (ret) { |
| 910 | pr_err("%s: gether_setup failed\n", __func__); |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | if (rndis->wceis) { |
| 915 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 916 | rndis_iad_descriptor.bFunctionClass = |
| 917 | USB_CLASS_WIRELESS_CONTROLLER; |
| 918 | rndis_iad_descriptor.bFunctionSubClass = 0x01; |
| 919 | rndis_iad_descriptor.bFunctionProtocol = 0x03; |
| 920 | rndis_control_intf.bInterfaceClass = |
| 921 | USB_CLASS_WIRELESS_CONTROLLER; |
| 922 | rndis_control_intf.bInterfaceSubClass = 0x01; |
| 923 | rndis_control_intf.bInterfaceProtocol = 0x03; |
| 924 | } |
| 925 | |
| 926 | return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID, |
| 927 | rndis->manufacturer); |
| 928 | } |
| 929 | |
| 930 | static void rndis_function_unbind_config(struct android_usb_function *f, |
| 931 | struct usb_configuration *c) |
| 932 | { |
| 933 | gether_cleanup(); |
| 934 | } |
| 935 | |
| 936 | static ssize_t rndis_manufacturer_show(struct device *dev, |
| 937 | struct device_attribute *attr, char *buf) |
| 938 | { |
| 939 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 940 | struct rndis_function_config *config = f->config; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 941 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 942 | return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static ssize_t rndis_manufacturer_store(struct device *dev, |
| 946 | struct device_attribute *attr, const char *buf, size_t size) |
| 947 | { |
| 948 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 949 | struct rndis_function_config *config = f->config; |
| 950 | |
| 951 | if (size >= sizeof(config->manufacturer)) |
| 952 | return -EINVAL; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 953 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 954 | if (sscanf(buf, "%255s", config->manufacturer) == 1) |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 955 | return size; |
| 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show, |
| 960 | rndis_manufacturer_store); |
| 961 | |
| 962 | static ssize_t rndis_wceis_show(struct device *dev, |
| 963 | struct device_attribute *attr, char *buf) |
| 964 | { |
| 965 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 966 | struct rndis_function_config *config = f->config; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 967 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 968 | return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static ssize_t rndis_wceis_store(struct device *dev, |
| 972 | struct device_attribute *attr, const char *buf, size_t size) |
| 973 | { |
| 974 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 975 | struct rndis_function_config *config = f->config; |
| 976 | int value; |
| 977 | |
| 978 | if (sscanf(buf, "%d", &value) == 1) { |
| 979 | config->wceis = value; |
| 980 | return size; |
| 981 | } |
| 982 | return -EINVAL; |
| 983 | } |
| 984 | |
| 985 | static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show, |
| 986 | rndis_wceis_store); |
| 987 | |
| 988 | static ssize_t rndis_ethaddr_show(struct device *dev, |
| 989 | struct device_attribute *attr, char *buf) |
| 990 | { |
| 991 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 992 | struct rndis_function_config *rndis = f->config; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 993 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 994 | return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 995 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 996 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 997 | } |
| 998 | |
| 999 | static ssize_t rndis_ethaddr_store(struct device *dev, |
| 1000 | struct device_attribute *attr, const char *buf, size_t size) |
| 1001 | { |
| 1002 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1003 | struct rndis_function_config *rndis = f->config; |
| 1004 | |
| 1005 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1006 | (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1], |
| 1007 | (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3], |
| 1008 | (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6) |
| 1009 | return size; |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show, |
| 1014 | rndis_ethaddr_store); |
| 1015 | |
| 1016 | static ssize_t rndis_vendorID_show(struct device *dev, |
| 1017 | struct device_attribute *attr, char *buf) |
| 1018 | { |
| 1019 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1020 | struct rndis_function_config *config = f->config; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1021 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1022 | return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static ssize_t rndis_vendorID_store(struct device *dev, |
| 1026 | struct device_attribute *attr, const char *buf, size_t size) |
| 1027 | { |
| 1028 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1029 | struct rndis_function_config *config = f->config; |
| 1030 | int value; |
| 1031 | |
| 1032 | if (sscanf(buf, "%04x", &value) == 1) { |
| 1033 | config->vendorID = value; |
| 1034 | return size; |
| 1035 | } |
| 1036 | return -EINVAL; |
| 1037 | } |
| 1038 | |
| 1039 | static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show, |
| 1040 | rndis_vendorID_store); |
| 1041 | |
| 1042 | static struct device_attribute *rndis_function_attributes[] = { |
| 1043 | &dev_attr_manufacturer, |
| 1044 | &dev_attr_wceis, |
| 1045 | &dev_attr_ethaddr, |
| 1046 | &dev_attr_vendorID, |
| 1047 | NULL |
| 1048 | }; |
| 1049 | |
| 1050 | static struct android_usb_function rndis_function = { |
| 1051 | .name = "rndis", |
| 1052 | .init = rndis_function_init, |
| 1053 | .cleanup = rndis_function_cleanup, |
| 1054 | .bind_config = rndis_function_bind_config, |
| 1055 | .unbind_config = rndis_function_unbind_config, |
| 1056 | .attributes = rndis_function_attributes, |
| 1057 | }; |
| 1058 | |
| 1059 | |
| 1060 | struct mass_storage_function_config { |
| 1061 | struct fsg_config fsg; |
| 1062 | struct fsg_common *common; |
| 1063 | }; |
| 1064 | |
| 1065 | static int mass_storage_function_init(struct android_usb_function *f, |
| 1066 | struct usb_composite_dev *cdev) |
| 1067 | { |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1068 | struct android_dev *dev = _android_dev; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1069 | struct mass_storage_function_config *config; |
| 1070 | struct fsg_common *common; |
| 1071 | int err; |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1072 | int i; |
| 1073 | const char *name[2]; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1074 | |
| 1075 | config = kzalloc(sizeof(struct mass_storage_function_config), |
| 1076 | GFP_KERNEL); |
| 1077 | if (!config) |
| 1078 | return -ENOMEM; |
| 1079 | |
| 1080 | config->fsg.nluns = 1; |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1081 | name[0] = "lun"; |
Pavankumar Kondeti | 2043e30 | 2012-07-19 08:54:04 +0530 | [diff] [blame^] | 1082 | if (dev->pdata && dev->pdata->cdrom) { |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1083 | config->fsg.nluns = 2; |
| 1084 | config->fsg.luns[1].cdrom = 1; |
| 1085 | config->fsg.luns[1].ro = 1; |
| 1086 | config->fsg.luns[1].removable = 1; |
| 1087 | name[1] = "lun0"; |
| 1088 | } |
| 1089 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1090 | config->fsg.luns[0].removable = 1; |
| 1091 | |
| 1092 | common = fsg_common_init(NULL, cdev, &config->fsg); |
| 1093 | if (IS_ERR(common)) { |
| 1094 | kfree(config); |
| 1095 | return PTR_ERR(common); |
| 1096 | } |
| 1097 | |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1098 | for (i = 0; i < config->fsg.nluns; i++) { |
| 1099 | err = sysfs_create_link(&f->dev->kobj, |
| 1100 | &common->luns[i].dev.kobj, |
| 1101 | name[i]); |
| 1102 | if (err) |
| 1103 | goto error; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | config->common = common; |
| 1107 | f->config = config; |
| 1108 | return 0; |
Rajkumar Raghupathy | c9cb205 | 2012-04-26 13:14:10 +0530 | [diff] [blame] | 1109 | error: |
| 1110 | for (; i > 0 ; i--) |
| 1111 | sysfs_remove_link(&f->dev->kobj, name[i-1]); |
| 1112 | |
| 1113 | fsg_common_release(&common->ref); |
| 1114 | kfree(config); |
| 1115 | return err; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | static void mass_storage_function_cleanup(struct android_usb_function *f) |
| 1119 | { |
| 1120 | kfree(f->config); |
| 1121 | f->config = NULL; |
| 1122 | } |
| 1123 | |
| 1124 | static int mass_storage_function_bind_config(struct android_usb_function *f, |
| 1125 | struct usb_configuration *c) |
| 1126 | { |
| 1127 | struct mass_storage_function_config *config = f->config; |
| 1128 | return fsg_bind_config(c->cdev, c, config->common); |
| 1129 | } |
| 1130 | |
| 1131 | static ssize_t mass_storage_inquiry_show(struct device *dev, |
| 1132 | struct device_attribute *attr, char *buf) |
| 1133 | { |
| 1134 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1135 | struct mass_storage_function_config *config = f->config; |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1136 | return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static ssize_t mass_storage_inquiry_store(struct device *dev, |
| 1140 | struct device_attribute *attr, const char *buf, size_t size) |
| 1141 | { |
| 1142 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1143 | struct mass_storage_function_config *config = f->config; |
| 1144 | if (size >= sizeof(config->common->inquiry_string)) |
| 1145 | return -EINVAL; |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1146 | if (sscanf(buf, "%28s", config->common->inquiry_string) != 1) |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1147 | return -EINVAL; |
| 1148 | return size; |
| 1149 | } |
| 1150 | |
| 1151 | static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR, |
| 1152 | mass_storage_inquiry_show, |
| 1153 | mass_storage_inquiry_store); |
| 1154 | |
| 1155 | static struct device_attribute *mass_storage_function_attributes[] = { |
| 1156 | &dev_attr_inquiry_string, |
| 1157 | NULL |
| 1158 | }; |
| 1159 | |
| 1160 | static struct android_usb_function mass_storage_function = { |
| 1161 | .name = "mass_storage", |
| 1162 | .init = mass_storage_function_init, |
| 1163 | .cleanup = mass_storage_function_cleanup, |
| 1164 | .bind_config = mass_storage_function_bind_config, |
| 1165 | .attributes = mass_storage_function_attributes, |
| 1166 | }; |
| 1167 | |
| 1168 | |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 1169 | static int accessory_function_init(struct android_usb_function *f, |
| 1170 | struct usb_composite_dev *cdev) |
| 1171 | { |
| 1172 | return acc_setup(); |
| 1173 | } |
| 1174 | |
| 1175 | static void accessory_function_cleanup(struct android_usb_function *f) |
| 1176 | { |
| 1177 | acc_cleanup(); |
| 1178 | } |
| 1179 | |
| 1180 | static int accessory_function_bind_config(struct android_usb_function *f, |
| 1181 | struct usb_configuration *c) |
| 1182 | { |
| 1183 | return acc_bind_config(c); |
| 1184 | } |
| 1185 | |
| 1186 | static int accessory_function_ctrlrequest(struct android_usb_function *f, |
| 1187 | struct usb_composite_dev *cdev, |
| 1188 | const struct usb_ctrlrequest *c) |
| 1189 | { |
| 1190 | return acc_ctrlrequest(cdev, c); |
| 1191 | } |
| 1192 | |
| 1193 | static struct android_usb_function accessory_function = { |
| 1194 | .name = "accessory", |
| 1195 | .init = accessory_function_init, |
| 1196 | .cleanup = accessory_function_cleanup, |
| 1197 | .bind_config = accessory_function_bind_config, |
| 1198 | .ctrlrequest = accessory_function_ctrlrequest, |
| 1199 | }; |
| 1200 | |
| 1201 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1202 | static struct android_usb_function *supported_functions[] = { |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 1203 | &mbim_function, |
Manu Gautam | 1c8ffd7 | 2011-09-02 16:00:49 +0530 | [diff] [blame] | 1204 | &rmnet_smd_function, |
Manu Gautam | 8e0719b | 2011-09-26 14:47:55 +0530 | [diff] [blame] | 1205 | &rmnet_sdio_function, |
| 1206 | &rmnet_smd_sdio_function, |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 1207 | &rmnet_function, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1208 | &diag_function, |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 1209 | &serial_function, |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 1210 | &adb_function, |
Chiranjeevi Velempati | e130fd0 | 2011-11-29 05:06:13 +0530 | [diff] [blame] | 1211 | &ccid_function, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1212 | &acm_function, |
Benoit Goby | f0fbc48 | 2011-12-19 14:37:50 -0800 | [diff] [blame] | 1213 | &mtp_function, |
| 1214 | &ptp_function, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1215 | &rndis_function, |
| 1216 | &mass_storage_function, |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 1217 | &accessory_function, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1218 | NULL |
| 1219 | }; |
| 1220 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1221 | static void android_cleanup_functions(struct android_usb_function **functions) |
| 1222 | { |
| 1223 | struct android_usb_function *f; |
| 1224 | struct device_attribute **attrs; |
| 1225 | struct device_attribute *attr; |
| 1226 | |
| 1227 | while (*functions) { |
| 1228 | f = *functions++; |
| 1229 | |
| 1230 | if (f->dev) { |
| 1231 | device_destroy(android_class, f->dev->devt); |
| 1232 | kfree(f->dev_name); |
| 1233 | } else |
| 1234 | continue; |
| 1235 | |
| 1236 | if (f->cleanup) |
| 1237 | f->cleanup(f); |
| 1238 | |
| 1239 | attrs = f->attributes; |
| 1240 | if (attrs) { |
| 1241 | while ((attr = *attrs++)) |
| 1242 | device_remove_file(f->dev, attr); |
| 1243 | } |
| 1244 | } |
| 1245 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1246 | |
| 1247 | static int android_init_functions(struct android_usb_function **functions, |
| 1248 | struct usb_composite_dev *cdev) |
| 1249 | { |
| 1250 | struct android_dev *dev = _android_dev; |
| 1251 | struct android_usb_function *f; |
| 1252 | struct device_attribute **attrs; |
| 1253 | struct device_attribute *attr; |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1254 | int err = 0; |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1255 | int index = 1; /* index 0 is for android0 device */ |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1256 | |
| 1257 | for (; (f = *functions++); index++) { |
| 1258 | f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1259 | if (!f->dev_name) { |
| 1260 | err = -ENOMEM; |
| 1261 | goto err_out; |
| 1262 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1263 | f->dev = device_create(android_class, dev->dev, |
| 1264 | MKDEV(0, index), f, f->dev_name); |
| 1265 | if (IS_ERR(f->dev)) { |
| 1266 | pr_err("%s: Failed to create dev %s", __func__, |
| 1267 | f->dev_name); |
| 1268 | err = PTR_ERR(f->dev); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1269 | f->dev = NULL; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1270 | goto err_create; |
| 1271 | } |
| 1272 | |
| 1273 | if (f->init) { |
| 1274 | err = f->init(f, cdev); |
| 1275 | if (err) { |
| 1276 | pr_err("%s: Failed to init %s", __func__, |
| 1277 | f->name); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1278 | goto err_init; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | attrs = f->attributes; |
| 1283 | if (attrs) { |
| 1284 | while ((attr = *attrs++) && !err) |
| 1285 | err = device_create_file(f->dev, attr); |
| 1286 | } |
| 1287 | if (err) { |
| 1288 | pr_err("%s: Failed to create function %s attributes", |
| 1289 | __func__, f->name); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1290 | goto err_attrs; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | return 0; |
| 1294 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1295 | err_attrs: |
| 1296 | for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--)) |
| 1297 | device_remove_file(f->dev, attr); |
| 1298 | if (f->cleanup) |
| 1299 | f->cleanup(f); |
| 1300 | err_init: |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1301 | device_destroy(android_class, f->dev->devt); |
| 1302 | err_create: |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1303 | f->dev = NULL; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1304 | kfree(f->dev_name); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1305 | err_out: |
| 1306 | android_cleanup_functions(dev->functions); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1307 | return err; |
| 1308 | } |
| 1309 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1310 | static int |
| 1311 | android_bind_enabled_functions(struct android_dev *dev, |
| 1312 | struct usb_configuration *c) |
| 1313 | { |
| 1314 | struct android_usb_function *f; |
| 1315 | int ret; |
| 1316 | |
| 1317 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1318 | ret = f->bind_config(f, c); |
| 1319 | if (ret) { |
| 1320 | pr_err("%s: %s failed", __func__, f->name); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | } |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static void |
| 1328 | android_unbind_enabled_functions(struct android_dev *dev, |
| 1329 | struct usb_configuration *c) |
| 1330 | { |
| 1331 | struct android_usb_function *f; |
| 1332 | |
| 1333 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1334 | if (f->unbind_config) |
| 1335 | f->unbind_config(f, c); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | static int android_enable_function(struct android_dev *dev, char *name) |
| 1340 | { |
| 1341 | struct android_usb_function **functions = dev->functions; |
| 1342 | struct android_usb_function *f; |
| 1343 | while ((f = *functions++)) { |
| 1344 | if (!strcmp(name, f->name)) { |
| 1345 | list_add_tail(&f->enabled_list, |
| 1346 | &dev->enabled_functions); |
| 1347 | return 0; |
| 1348 | } |
| 1349 | } |
| 1350 | return -EINVAL; |
| 1351 | } |
| 1352 | |
| 1353 | /*-------------------------------------------------------------------------*/ |
| 1354 | /* /sys/class/android_usb/android%d/ interface */ |
| 1355 | |
Pavankumar Kondeti | 352396c | 2011-12-07 13:32:40 +0530 | [diff] [blame] | 1356 | static ssize_t remote_wakeup_show(struct device *pdev, |
| 1357 | struct device_attribute *attr, char *buf) |
| 1358 | { |
| 1359 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 1360 | !!(android_config_driver.bmAttributes & |
| 1361 | USB_CONFIG_ATT_WAKEUP)); |
| 1362 | } |
| 1363 | |
| 1364 | static ssize_t remote_wakeup_store(struct device *pdev, |
| 1365 | struct device_attribute *attr, const char *buff, size_t size) |
| 1366 | { |
| 1367 | int enable = 0; |
| 1368 | |
| 1369 | sscanf(buff, "%d", &enable); |
| 1370 | |
| 1371 | pr_debug("android_usb: %s remote wakeup\n", |
| 1372 | enable ? "enabling" : "disabling"); |
| 1373 | |
| 1374 | if (enable) |
| 1375 | android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 1376 | else |
| 1377 | android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; |
| 1378 | |
| 1379 | return size; |
| 1380 | } |
| 1381 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1382 | static ssize_t |
| 1383 | functions_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 1384 | { |
| 1385 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1386 | struct android_usb_function *f; |
| 1387 | char *buff = buf; |
| 1388 | |
| 1389 | mutex_lock(&dev->mutex); |
| 1390 | |
| 1391 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1392 | buff += snprintf(buff, PAGE_SIZE, "%s,", f->name); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1393 | |
| 1394 | mutex_unlock(&dev->mutex); |
| 1395 | |
| 1396 | if (buff != buf) |
| 1397 | *(buff-1) = '\n'; |
| 1398 | return buff - buf; |
| 1399 | } |
| 1400 | |
| 1401 | static ssize_t |
| 1402 | functions_store(struct device *pdev, struct device_attribute *attr, |
| 1403 | const char *buff, size_t size) |
| 1404 | { |
| 1405 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1406 | char *name; |
| 1407 | char buf[256], *b; |
| 1408 | int err; |
| 1409 | |
| 1410 | mutex_lock(&dev->mutex); |
| 1411 | |
| 1412 | if (dev->enabled) { |
| 1413 | mutex_unlock(&dev->mutex); |
| 1414 | return -EBUSY; |
| 1415 | } |
| 1416 | |
| 1417 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1418 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1419 | strlcpy(buf, buff, sizeof(buf)); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1420 | b = strim(buf); |
| 1421 | |
| 1422 | while (b) { |
| 1423 | name = strsep(&b, ","); |
| 1424 | if (name) { |
| 1425 | err = android_enable_function(dev, name); |
| 1426 | if (err) |
| 1427 | pr_err("android_usb: Cannot enable '%s'", name); |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | mutex_unlock(&dev->mutex); |
| 1432 | |
| 1433 | return size; |
| 1434 | } |
| 1435 | |
| 1436 | static ssize_t enable_show(struct device *pdev, struct device_attribute *attr, |
| 1437 | char *buf) |
| 1438 | { |
| 1439 | struct android_dev *dev = dev_get_drvdata(pdev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1440 | |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1441 | return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static ssize_t enable_store(struct device *pdev, struct device_attribute *attr, |
| 1445 | const char *buff, size_t size) |
| 1446 | { |
| 1447 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1448 | struct usb_composite_dev *cdev = dev->cdev; |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 1449 | struct android_usb_function *f; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1450 | int enabled = 0; |
| 1451 | |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 1452 | if (!cdev) |
| 1453 | return -ENODEV; |
| 1454 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1455 | mutex_lock(&dev->mutex); |
| 1456 | |
| 1457 | sscanf(buff, "%d", &enabled); |
| 1458 | if (enabled && !dev->enabled) { |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1459 | /* |
| 1460 | * Update values in composite driver's copy of |
| 1461 | * device descriptor. |
| 1462 | */ |
| 1463 | cdev->desc.idVendor = device_desc.idVendor; |
| 1464 | cdev->desc.idProduct = device_desc.idProduct; |
| 1465 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 1466 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 1467 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 1468 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 1469 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1470 | if (f->enable) |
| 1471 | f->enable(f); |
| 1472 | } |
| 1473 | android_enable(dev); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1474 | dev->enabled = true; |
| 1475 | } else if (!enabled && dev->enabled) { |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 1476 | android_disable(dev); |
| 1477 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1478 | if (f->disable) |
| 1479 | f->disable(f); |
| 1480 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1481 | dev->enabled = false; |
| 1482 | } else { |
| 1483 | pr_err("android_usb: already %s\n", |
| 1484 | dev->enabled ? "enabled" : "disabled"); |
| 1485 | } |
| 1486 | |
| 1487 | mutex_unlock(&dev->mutex); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1488 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 1489 | return size; |
| 1490 | } |
| 1491 | |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1492 | static ssize_t pm_qos_show(struct device *pdev, |
| 1493 | struct device_attribute *attr, char *buf) |
| 1494 | { |
| 1495 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1496 | |
| 1497 | return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos); |
| 1498 | } |
| 1499 | |
| 1500 | static ssize_t pm_qos_store(struct device *pdev, |
| 1501 | struct device_attribute *attr, |
| 1502 | const char *buff, size_t size) |
| 1503 | { |
| 1504 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1505 | |
| 1506 | strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos)); |
| 1507 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1508 | return size; |
| 1509 | } |
| 1510 | |
| 1511 | static ssize_t state_show(struct device *pdev, struct device_attribute *attr, |
| 1512 | char *buf) |
| 1513 | { |
| 1514 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 1515 | struct usb_composite_dev *cdev = dev->cdev; |
| 1516 | char *state = "DISCONNECTED"; |
| 1517 | unsigned long flags; |
| 1518 | |
| 1519 | if (!cdev) |
| 1520 | goto out; |
| 1521 | |
| 1522 | spin_lock_irqsave(&cdev->lock, flags); |
| 1523 | if (cdev->config) |
| 1524 | state = "CONFIGURED"; |
| 1525 | else if (dev->connected) |
| 1526 | state = "CONNECTED"; |
| 1527 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1528 | out: |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1529 | return snprintf(buf, PAGE_SIZE, "%s\n", state); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | #define DESCRIPTOR_ATTR(field, format_string) \ |
| 1533 | static ssize_t \ |
| 1534 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 1535 | char *buf) \ |
| 1536 | { \ |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1537 | return snprintf(buf, PAGE_SIZE, \ |
| 1538 | format_string, device_desc.field); \ |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1539 | } \ |
| 1540 | static ssize_t \ |
| 1541 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 1542 | const char *buf, size_t size) \ |
| 1543 | { \ |
| 1544 | int value; \ |
| 1545 | if (sscanf(buf, format_string, &value) == 1) { \ |
| 1546 | device_desc.field = value; \ |
| 1547 | return size; \ |
| 1548 | } \ |
| 1549 | return -1; \ |
| 1550 | } \ |
| 1551 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 1552 | |
| 1553 | #define DESCRIPTOR_STRING_ATTR(field, buffer) \ |
| 1554 | static ssize_t \ |
| 1555 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 1556 | char *buf) \ |
| 1557 | { \ |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1558 | return snprintf(buf, PAGE_SIZE, "%s", buffer); \ |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1559 | } \ |
| 1560 | static ssize_t \ |
| 1561 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 1562 | const char *buf, size_t size) \ |
| 1563 | { \ |
| 1564 | if (size >= sizeof(buffer)) \ |
| 1565 | return -EINVAL; \ |
Pavankumar Kondeti | e02a51a | 2012-06-20 08:52:37 +0530 | [diff] [blame] | 1566 | strlcpy(buffer, buf, sizeof(buffer)); \ |
| 1567 | strim(buffer); \ |
Pavankumar Kondeti | 4c22c10 | 2012-06-15 10:59:05 +0530 | [diff] [blame] | 1568 | return size; \ |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1569 | } \ |
| 1570 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 1571 | |
| 1572 | |
| 1573 | DESCRIPTOR_ATTR(idVendor, "%04x\n") |
| 1574 | DESCRIPTOR_ATTR(idProduct, "%04x\n") |
| 1575 | DESCRIPTOR_ATTR(bcdDevice, "%04x\n") |
| 1576 | DESCRIPTOR_ATTR(bDeviceClass, "%d\n") |
| 1577 | DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n") |
| 1578 | DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n") |
| 1579 | DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string) |
| 1580 | DESCRIPTOR_STRING_ATTR(iProduct, product_string) |
| 1581 | DESCRIPTOR_STRING_ATTR(iSerial, serial_string) |
| 1582 | |
| 1583 | static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, |
| 1584 | functions_store); |
| 1585 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1586 | static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR, |
| 1587 | pm_qos_show, pm_qos_store); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1588 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
Pavankumar Kondeti | 352396c | 2011-12-07 13:32:40 +0530 | [diff] [blame] | 1589 | static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, |
| 1590 | remote_wakeup_show, remote_wakeup_store); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1591 | |
| 1592 | static struct device_attribute *android_usb_attributes[] = { |
| 1593 | &dev_attr_idVendor, |
| 1594 | &dev_attr_idProduct, |
| 1595 | &dev_attr_bcdDevice, |
| 1596 | &dev_attr_bDeviceClass, |
| 1597 | &dev_attr_bDeviceSubClass, |
| 1598 | &dev_attr_bDeviceProtocol, |
| 1599 | &dev_attr_iManufacturer, |
| 1600 | &dev_attr_iProduct, |
| 1601 | &dev_attr_iSerial, |
| 1602 | &dev_attr_functions, |
| 1603 | &dev_attr_enable, |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1604 | &dev_attr_pm_qos, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1605 | &dev_attr_state, |
Pavankumar Kondeti | 352396c | 2011-12-07 13:32:40 +0530 | [diff] [blame] | 1606 | &dev_attr_remote_wakeup, |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1607 | NULL |
| 1608 | }; |
| 1609 | |
| 1610 | /*-------------------------------------------------------------------------*/ |
| 1611 | /* Composite driver */ |
| 1612 | |
| 1613 | static int android_bind_config(struct usb_configuration *c) |
| 1614 | { |
| 1615 | struct android_dev *dev = _android_dev; |
| 1616 | int ret = 0; |
| 1617 | |
| 1618 | ret = android_bind_enabled_functions(dev, c); |
| 1619 | if (ret) |
| 1620 | return ret; |
| 1621 | |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | static void android_unbind_config(struct usb_configuration *c) |
| 1626 | { |
| 1627 | struct android_dev *dev = _android_dev; |
| 1628 | |
| 1629 | android_unbind_enabled_functions(dev, c); |
| 1630 | } |
| 1631 | |
| 1632 | static int android_bind(struct usb_composite_dev *cdev) |
| 1633 | { |
| 1634 | struct android_dev *dev = _android_dev; |
| 1635 | struct usb_gadget *gadget = cdev->gadget; |
| 1636 | int gcnum, id, ret; |
| 1637 | |
| 1638 | /* |
| 1639 | * Start disconnected. Userspace will connect the gadget once |
| 1640 | * it is done configuring the functions. |
| 1641 | */ |
| 1642 | usb_gadget_disconnect(gadget); |
| 1643 | |
| 1644 | ret = android_init_functions(dev->functions, cdev); |
| 1645 | if (ret) |
| 1646 | return ret; |
| 1647 | |
| 1648 | /* Allocate string descriptor numbers ... note that string |
| 1649 | * contents can be overridden by the composite_dev glue. |
| 1650 | */ |
| 1651 | id = usb_string_id(cdev); |
| 1652 | if (id < 0) |
| 1653 | return id; |
| 1654 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
| 1655 | device_desc.iManufacturer = id; |
| 1656 | |
| 1657 | id = usb_string_id(cdev); |
| 1658 | if (id < 0) |
| 1659 | return id; |
| 1660 | strings_dev[STRING_PRODUCT_IDX].id = id; |
| 1661 | device_desc.iProduct = id; |
| 1662 | |
| 1663 | /* Default strings - should be updated by userspace */ |
Rajkumar Raghupathy | 42ec8da | 2011-10-21 18:58:53 +0530 | [diff] [blame] | 1664 | strlcpy(manufacturer_string, "Android", |
| 1665 | sizeof(manufacturer_string) - 1); |
| 1666 | strlcpy(product_string, "Android", sizeof(product_string) - 1); |
| 1667 | strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1668 | |
| 1669 | id = usb_string_id(cdev); |
| 1670 | if (id < 0) |
| 1671 | return id; |
| 1672 | strings_dev[STRING_SERIAL_IDX].id = id; |
| 1673 | device_desc.iSerialNumber = id; |
| 1674 | |
Vijayavardhan Vennapusa | 56e6052 | 2012-02-16 15:40:16 +0530 | [diff] [blame] | 1675 | if (gadget_is_otg(cdev->gadget)) |
| 1676 | android_config_driver.descriptors = otg_desc; |
| 1677 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1678 | gcnum = usb_gadget_controller_number(gadget); |
| 1679 | if (gcnum >= 0) |
| 1680 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); |
| 1681 | else { |
| 1682 | pr_warning("%s: controller '%s' not recognized\n", |
| 1683 | longname, gadget->name); |
| 1684 | device_desc.bcdDevice = __constant_cpu_to_le16(0x9999); |
| 1685 | } |
| 1686 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1687 | dev->cdev = cdev; |
| 1688 | |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | static int android_usb_unbind(struct usb_composite_dev *cdev) |
| 1693 | { |
| 1694 | struct android_dev *dev = _android_dev; |
| 1695 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1696 | manufacturer_string[0] = '\0'; |
| 1697 | product_string[0] = '\0'; |
| 1698 | serial_string[0] = '0'; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1699 | cancel_work_sync(&dev->work); |
| 1700 | android_cleanup_functions(dev->functions); |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | static struct usb_composite_driver android_usb_driver = { |
| 1705 | .name = "android_usb", |
| 1706 | .dev = &device_desc, |
| 1707 | .strings = dev_strings, |
| 1708 | .unbind = android_usb_unbind, |
Tatyana Brokhman | 3ba2890 | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 1709 | .max_speed = USB_SPEED_SUPER |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1710 | }; |
| 1711 | |
| 1712 | static int |
| 1713 | android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c) |
| 1714 | { |
| 1715 | struct android_dev *dev = _android_dev; |
| 1716 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1717 | struct usb_request *req = cdev->req; |
| 1718 | struct android_usb_function *f; |
| 1719 | int value = -EOPNOTSUPP; |
| 1720 | unsigned long flags; |
| 1721 | |
| 1722 | req->zero = 0; |
| 1723 | req->complete = composite_setup_complete; |
| 1724 | req->length = 0; |
| 1725 | gadget->ep0->driver_data = cdev; |
| 1726 | |
| 1727 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 1728 | if (f->ctrlrequest) { |
| 1729 | value = f->ctrlrequest(f, cdev, c); |
| 1730 | if (value >= 0) |
| 1731 | break; |
| 1732 | } |
| 1733 | } |
| 1734 | |
Benoit Goby | cf3fc06 | 2011-12-19 14:39:37 -0800 | [diff] [blame] | 1735 | /* Special case the accessory function. |
| 1736 | * It needs to handle control requests before it is enabled. |
| 1737 | */ |
| 1738 | if (value < 0) |
| 1739 | value = acc_ctrlrequest(cdev, c); |
| 1740 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1741 | if (value < 0) |
| 1742 | value = composite_setup(gadget, c); |
| 1743 | |
| 1744 | spin_lock_irqsave(&cdev->lock, flags); |
| 1745 | if (!dev->connected) { |
| 1746 | dev->connected = 1; |
| 1747 | schedule_work(&dev->work); |
| 1748 | } else if (c->bRequest == USB_REQ_SET_CONFIGURATION && |
| 1749 | cdev->config) { |
| 1750 | schedule_work(&dev->work); |
| 1751 | } |
| 1752 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1753 | |
| 1754 | return value; |
| 1755 | } |
| 1756 | |
| 1757 | static void android_disconnect(struct usb_gadget *gadget) |
| 1758 | { |
| 1759 | struct android_dev *dev = _android_dev; |
| 1760 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1761 | unsigned long flags; |
| 1762 | |
| 1763 | composite_disconnect(gadget); |
| 1764 | |
| 1765 | spin_lock_irqsave(&cdev->lock, flags); |
| 1766 | dev->connected = 0; |
| 1767 | schedule_work(&dev->work); |
| 1768 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1769 | } |
| 1770 | |
| 1771 | static int android_create_device(struct android_dev *dev) |
| 1772 | { |
| 1773 | struct device_attribute **attrs = android_usb_attributes; |
| 1774 | struct device_attribute *attr; |
| 1775 | int err; |
| 1776 | |
| 1777 | dev->dev = device_create(android_class, NULL, |
| 1778 | MKDEV(0, 0), NULL, "android0"); |
| 1779 | if (IS_ERR(dev->dev)) |
| 1780 | return PTR_ERR(dev->dev); |
| 1781 | |
| 1782 | dev_set_drvdata(dev->dev, dev); |
| 1783 | |
| 1784 | while ((attr = *attrs++)) { |
| 1785 | err = device_create_file(dev->dev, attr); |
| 1786 | if (err) { |
| 1787 | device_destroy(android_class, dev->dev->devt); |
| 1788 | return err; |
| 1789 | } |
| 1790 | } |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1794 | static void android_destroy_device(struct android_dev *dev) |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1795 | { |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1796 | struct device_attribute **attrs = android_usb_attributes; |
| 1797 | struct device_attribute *attr; |
| 1798 | |
| 1799 | while ((attr = *attrs++)) |
| 1800 | device_remove_file(dev->dev, attr); |
| 1801 | device_destroy(android_class, dev->dev->devt); |
| 1802 | } |
| 1803 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1804 | static int __devinit android_probe(struct platform_device *pdev) |
| 1805 | { |
| 1806 | struct android_usb_platform_data *pdata = pdev->dev.platform_data; |
| 1807 | struct android_dev *dev = _android_dev; |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1808 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1809 | |
| 1810 | dev->pdata = pdata; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1811 | |
| 1812 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 1813 | if (IS_ERR(android_class)) |
| 1814 | return PTR_ERR(android_class); |
| 1815 | |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1816 | ret = android_create_device(dev); |
| 1817 | if (ret) { |
| 1818 | pr_err("%s(): android_create_device failed\n", __func__); |
| 1819 | goto err_dev; |
| 1820 | } |
| 1821 | |
Ido Shayevitz | 3987b8d | 2012-06-24 13:27:41 +0300 | [diff] [blame] | 1822 | if (pdata) |
| 1823 | composite_driver.usb_core_id = pdata->usb_core_id; |
| 1824 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1825 | ret = usb_composite_probe(&android_usb_driver, android_bind); |
| 1826 | if (ret) { |
| 1827 | pr_err("%s(): Failed to register android " |
| 1828 | "composite driver\n", __func__); |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1829 | goto err_probe; |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1832 | /* pm qos request to prevent apps idle power collapse */ |
Manu Gautam | 94dc614 | 2012-05-08 14:35:24 +0530 | [diff] [blame] | 1833 | if (pdata && pdata->swfi_latency) |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1834 | pm_qos_add_request(&dev->pm_qos_req_dma, |
| 1835 | PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE); |
| 1836 | strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos)); |
| 1837 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1838 | return ret; |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1839 | err_probe: |
| 1840 | android_destroy_device(dev); |
| 1841 | err_dev: |
| 1842 | class_destroy(android_class); |
| 1843 | return ret; |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static int android_remove(struct platform_device *pdev) |
| 1847 | { |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1848 | struct android_dev *dev = _android_dev; |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1849 | struct android_usb_platform_data *pdata = pdev->dev.platform_data; |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1850 | |
| 1851 | android_destroy_device(dev); |
| 1852 | class_destroy(android_class); |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1853 | usb_composite_unregister(&android_usb_driver); |
Manu Gautam | 94dc614 | 2012-05-08 14:35:24 +0530 | [diff] [blame] | 1854 | if (pdata && pdata->swfi_latency) |
Ofir Cohen | 94213a7 | 2012-05-03 14:26:32 +0300 | [diff] [blame] | 1855 | pm_qos_remove_request(&dev->pm_qos_req_dma); |
| 1856 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1857 | return 0; |
| 1858 | } |
| 1859 | |
| 1860 | static struct platform_driver android_platform_driver = { |
| 1861 | .driver = { .name = "android_usb"}, |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1862 | .probe = android_probe, |
| 1863 | .remove = android_remove, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1864 | }; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1865 | |
| 1866 | static int __init init(void) |
| 1867 | { |
| 1868 | struct android_dev *dev; |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1869 | int ret; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 1870 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1871 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1872 | if (!dev) { |
| 1873 | pr_err("%s(): Failed to alloc memory for android_dev\n", |
| 1874 | __func__); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1875 | return -ENOMEM; |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1876 | } |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1877 | |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 1878 | dev->disable_depth = 1; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1879 | dev->functions = supported_functions; |
| 1880 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 1881 | INIT_WORK(&dev->work, android_work); |
| 1882 | mutex_init(&dev->mutex); |
| 1883 | |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1884 | _android_dev = dev; |
| 1885 | |
| 1886 | /* Override composite driver functions */ |
| 1887 | composite_driver.setup = android_setup; |
| 1888 | composite_driver.disconnect = android_disconnect; |
| 1889 | |
Pavankumar Kondeti | 044914d | 2012-01-31 12:56:13 +0530 | [diff] [blame] | 1890 | ret = platform_driver_register(&android_platform_driver); |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1891 | if (ret) { |
| 1892 | pr_err("%s(): Failed to register android" |
| 1893 | "platform driver\n", __func__); |
Rajkumar Raghupathy | 5d3fc39 | 2012-04-11 16:53:19 +0530 | [diff] [blame] | 1894 | kfree(dev); |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1895 | } |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1896 | |
Rajkumar Raghupathy | a1df77e | 2012-01-19 17:45:55 +0530 | [diff] [blame] | 1897 | return ret; |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1898 | } |
| 1899 | module_init(init); |
| 1900 | |
| 1901 | static void __exit cleanup(void) |
| 1902 | { |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1903 | platform_driver_unregister(&android_platform_driver); |
Benoit Goby | 1e8ce15 | 2011-12-12 13:01:23 -0800 | [diff] [blame] | 1904 | kfree(_android_dev); |
| 1905 | _android_dev = NULL; |
| 1906 | } |
| 1907 | module_exit(cleanup); |