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