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