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