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