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