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