b.liu | e958203 | 2025-04-17 19:18:16 +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> |
| 26 | |
| 27 | #include <linux/usb/ch9.h> |
| 28 | #include <linux/usb/composite.h> |
| 29 | #include <linux/usb/gadget.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include "./function/gadget_chips.h" |
| 32 | |
| 33 | #include "./function/f_adb.c" |
| 34 | |
| 35 | #ifndef CONFIG_USB_TELEPHONY |
| 36 | #include "./function/pxa910_u_serial.c" |
| 37 | #include "./function/pxa910_f_diag.c" |
| 38 | #include "./function/pxa910_f_modem.c" |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 42 | #include "./function/f_fs.c" |
| 43 | #endif |
| 44 | |
| 45 | #ifndef CONFIG_USB_TELEPHONY |
| 46 | #include "./function/pxa182x_serial_debug.c" |
| 47 | #include "./function/pxa182x_acm_debug.c" |
| 48 | #endif |
| 49 | |
| 50 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 51 | #include "./function/f_midi.c" |
| 52 | #include "./function/f_audio_source.c" |
| 53 | #include "./function/f_accessory.c" |
| 54 | #endif |
| 55 | |
| 56 | #define DEF_CDROM_PATH "/etc/cdrom/CDROM.iso" |
| 57 | #define APPLE_CDROM_PATH "/etc/cdrom/CDROM.iso" |
| 58 | #define LINUX_CDROM_PATH "/etc/cdrom/CDROM.iso" |
| 59 | #define WIN_CDROM_PATH "/etc/cdrom/CDROM.iso" |
| 60 | |
| 61 | #include "./function/f_mass_storage.c" |
| 62 | |
| 63 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 64 | #include "./function/f_mtp.c" |
| 65 | #endif |
| 66 | |
| 67 | #define USB_ETH_RNDIS y |
| 68 | #include "./function/f_rndis.c" |
| 69 | #include "./function/rndis.c" |
| 70 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 71 | #include "./function/f_ecm.c" |
| 72 | #endif |
| 73 | |
| 74 | #if defined(CONFIG_USB_ETH_RNDIS_ECM) |
| 75 | #include "./function/u_ether_rndis_ecm.c" |
| 76 | #elif defined(CONFIG_ASR_TOE) |
| 77 | #include "./function/u_ether_toe.c" |
| 78 | #else |
| 79 | #include "./function/u_ether.c" |
| 80 | #endif |
| 81 | |
| 82 | #if defined (CONFIG_USB_G_MBIM) |
| 83 | #include "./function/f_mbim.c" |
| 84 | #endif |
| 85 | |
| 86 | #if defined (CONFIG_USB_TELEPHONY) |
| 87 | #include "f_usbtel.c" |
| 88 | #endif |
| 89 | |
| 90 | #if defined (CONFIG_ASR_UAC1) |
| 91 | #include "./function/u_asr_uac1.c" |
| 92 | #endif |
| 93 | |
| 94 | MODULE_AUTHOR("Mike Lockwood"); |
| 95 | MODULE_DESCRIPTION("Android Composite USB Driver"); |
| 96 | MODULE_LICENSE("GPL"); |
| 97 | MODULE_VERSION("1.0"); |
| 98 | |
| 99 | |
| 100 | static const char longname[] = "Gadget Android"; |
| 101 | |
| 102 | #if defined (CONFIG_USB_G_MBIM) |
| 103 | static bool mbim_enabled = 0; |
| 104 | #endif |
| 105 | |
| 106 | /* Default vendor and product IDs, overridden by userspace */ |
| 107 | #define VENDOR_ID 0x18D1 |
| 108 | #define PRODUCT_ID 0x0001 |
| 109 | |
| 110 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 111 | /* f_midi configuration */ |
| 112 | #define MIDI_INPUT_PORTS 1 |
| 113 | #define MIDI_OUTPUT_PORTS 1 |
| 114 | #define MIDI_BUFFER_SIZE 256 |
| 115 | #define MIDI_QUEUE_LENGTH 32 |
| 116 | #endif |
| 117 | |
| 118 | struct android_usb_function { |
| 119 | char *name; |
| 120 | void *config; |
| 121 | |
| 122 | struct device *dev; |
| 123 | char *dev_name; |
| 124 | struct device_attribute **attributes; |
| 125 | |
| 126 | /* for android_dev.enabled_functions */ |
| 127 | struct list_head enabled_list; |
| 128 | |
| 129 | /* Optional: initialization during gadget bind */ |
| 130 | int (*init)(struct android_usb_function *, struct usb_composite_dev *); |
| 131 | /* Optional: cleanup during gadget unbind */ |
| 132 | void (*cleanup)(struct android_usb_function *); |
| 133 | /* Optional: called when the function is added the list of |
| 134 | * enabled functions */ |
| 135 | void (*enable)(struct android_usb_function *); |
| 136 | /* Optional: called when it is removed */ |
| 137 | void (*disable)(struct android_usb_function *); |
| 138 | |
| 139 | int (*bind_config)(struct android_usb_function *, |
| 140 | struct usb_configuration *); |
| 141 | |
| 142 | /* Optional: called when the configuration is removed */ |
| 143 | void (*unbind_config)(struct android_usb_function *, |
| 144 | struct usb_configuration *); |
| 145 | /* Optional: handle ctrl requests before the device is configured */ |
| 146 | int (*ctrlrequest)(struct android_usb_function *, |
| 147 | struct usb_composite_dev *, |
| 148 | const struct usb_ctrlrequest *); |
| 149 | }; |
| 150 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 151 | struct acm_dev_paramter { |
| 152 | int instances; |
| 153 | }; |
| 154 | struct uac1_dev_paramter { |
| 155 | int instances; |
| 156 | }; |
| 157 | struct rndis_dev_paramter { |
| 158 | u32 vendorID; |
| 159 | char manufacturer[256]; |
| 160 | bool wceis; |
| 161 | }; |
| 162 | struct gser_dev_paramter { |
| 163 | int instances; |
| 164 | }; |
| 165 | #endif |
| 166 | struct android_dev { |
| 167 | struct android_usb_function **functions; |
| 168 | struct list_head enabled_functions; |
| 169 | struct usb_composite_dev *cdev; |
| 170 | struct device *dev; |
| 171 | |
| 172 | void (*setup_complete)(struct usb_ep *ep, |
| 173 | struct usb_request *req); |
| 174 | |
| 175 | bool enabled; |
| 176 | int disable_depth; |
| 177 | struct mutex mutex; |
| 178 | bool connected; |
| 179 | bool sw_connected; |
| 180 | struct work_struct work; |
| 181 | char ffs_aliases[256]; |
| 182 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 183 | struct acm_dev_paramter acm_paramter; |
| 184 | #ifdef CONFIG_ASR_UAC1 |
| 185 | struct uac1_dev_paramter uac1_paramter; |
| 186 | #endif |
| 187 | struct rndis_dev_paramter rndis_paramter; |
| 188 | struct gser_dev_paramter gser_paramter; |
| 189 | #endif |
| 190 | #ifdef CONFIG_USB_REMOTE_WAKEUP |
| 191 | bool remote_wakeup_enable; |
| 192 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 193 | char ethaddr[32]; |
| 194 | #endif |
| 195 | struct list_head evt_list; |
| 196 | #endif |
| 197 | }; |
| 198 | |
| 199 | enum { |
| 200 | USB_DEV_DISCONNECT, |
| 201 | USB_DEV_CONNECT, |
| 202 | USB_DEV_CONFIGURE, |
| 203 | NR_USB_DEV_EVENT, |
| 204 | }; |
| 205 | |
| 206 | struct android_dev_event { |
| 207 | struct list_head list; |
| 208 | u32 event_code; |
| 209 | }; |
| 210 | |
| 211 | static struct class *android_class; |
| 212 | static struct android_dev *_android_dev; |
| 213 | static int android_bind_config(struct usb_configuration *c); |
| 214 | static void android_unbind_config(struct usb_configuration *c); |
| 215 | |
| 216 | /* string IDs are assigned dynamically */ |
| 217 | #define STRING_MANUFACTURER_IDX 0 |
| 218 | #define STRING_PRODUCT_IDX 1 |
| 219 | #define STRING_SERIAL_IDX 2 |
| 220 | |
| 221 | static char manufacturer_string[256]; |
| 222 | static char product_string[256]; |
| 223 | static char serial_string[256]; |
| 224 | |
| 225 | /* String Table */ |
| 226 | static struct usb_string strings_dev[] = { |
| 227 | [STRING_MANUFACTURER_IDX].s = manufacturer_string, |
| 228 | [STRING_PRODUCT_IDX].s = product_string, |
| 229 | [STRING_SERIAL_IDX].s = serial_string, |
| 230 | { } /* end of list */ |
| 231 | }; |
| 232 | |
| 233 | static struct usb_gadget_strings stringtab_dev = { |
| 234 | .language = 0x0409, /* en-us */ |
| 235 | .strings = strings_dev, |
| 236 | }; |
| 237 | |
| 238 | static struct usb_gadget_strings *dev_strings[] = { |
| 239 | &stringtab_dev, |
| 240 | NULL, |
| 241 | }; |
| 242 | |
| 243 | static struct usb_device_descriptor device_desc = { |
| 244 | .bLength = sizeof(device_desc), |
| 245 | .bDescriptorType = USB_DT_DEVICE, |
| 246 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 247 | .bDeviceClass = USB_CLASS_MISC /* 0xEF */, |
| 248 | .bDeviceSubClass = 2, |
| 249 | .bDeviceProtocol = 1, |
| 250 | .idVendor = __constant_cpu_to_le16(VENDOR_ID), |
| 251 | .idProduct = __constant_cpu_to_le16(PRODUCT_ID), |
| 252 | .bcdDevice = __constant_cpu_to_le16(0xffff), |
| 253 | .bNumConfigurations = 1, |
| 254 | }; |
| 255 | |
| 256 | static struct usb_configuration android_config_driver = { |
| 257 | .label = "android", |
| 258 | .unbind = android_unbind_config, |
| 259 | .bConfigurationValue = 1, |
| 260 | }; |
| 261 | |
| 262 | #ifdef CONFIG_USB_ETH_RNDIS_ECM |
| 263 | static bool is_rndis_enabled = false; |
| 264 | #endif |
| 265 | |
| 266 | static struct android_dev_event *create_dev_evt(u32 evt_code) |
| 267 | { |
| 268 | struct android_dev_event *evt; |
| 269 | |
| 270 | evt = kzalloc(sizeof(*evt), GFP_ATOMIC); |
| 271 | if (unlikely(!evt)) { |
| 272 | pr_err("%s: failed to alloc dev evt\n", __func__); |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | evt->event_code = evt_code; |
| 277 | return evt; |
| 278 | } |
| 279 | |
| 280 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 281 | static void save_net_mac(char *hostaddr) |
| 282 | { |
| 283 | struct android_dev *dev = _android_dev; |
| 284 | sprintf(dev->ethaddr , "USB_MAC=%pM" , hostaddr); |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | static void _android_work(struct android_dev *dev, u32 evt_code) |
| 289 | { |
| 290 | struct usb_composite_dev *cdev = dev->cdev; |
| 291 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 292 | char *connected[2] = { "USB_STATE=CONNECTED", NULL }; |
| 293 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 294 | char *configured[3] = { "USB_STATE=CONFIGURED", dev->ethaddr , NULL }; |
| 295 | #else |
| 296 | char *configured[2] = { "USB_STATE=CONFIGURED", NULL }; |
| 297 | #endif |
| 298 | char **uevent_envp = NULL; |
| 299 | |
| 300 | if (!cdev) { |
| 301 | pr_err("comp dev is NULL\n"); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | if (USB_DEV_CONFIGURE == evt_code) { |
| 306 | uevent_envp = configured; |
| 307 | dev->sw_connected = true; |
| 308 | } else if (USB_DEV_DISCONNECT == evt_code) { |
| 309 | uevent_envp = disconnected; |
| 310 | dev->sw_connected = false; |
| 311 | } else { |
| 312 | uevent_envp = connected; |
| 313 | dev->sw_connected = true; |
| 314 | } |
| 315 | |
| 316 | if (uevent_envp) { |
| 317 | #ifndef CONFIG_USB_TELEPHONY |
| 318 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp); |
| 319 | pr_info("%s:%d: sent uevent %s %d:%p\n", |
| 320 | __func__, evt_code, uevent_envp[0], |
| 321 | dev->connected, cdev->config); |
| 322 | if (USB_DEV_DISCONNECT == evt_code) |
| 323 | msleep(100); |
| 324 | #endif |
| 325 | } else { |
| 326 | pr_info("%s: did not send uevent (%d %d %p)\n", __func__, |
| 327 | dev->connected, dev->sw_connected, cdev->config); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void android_work(struct work_struct *data) |
| 332 | { |
| 333 | struct android_dev *dev = container_of(data, struct android_dev, work); |
| 334 | struct usb_composite_dev *cdev = dev->cdev; |
| 335 | unsigned long flags; |
| 336 | struct android_dev_event *evt; |
| 337 | |
| 338 | if (!cdev) |
| 339 | return; |
| 340 | |
| 341 | spin_lock_irqsave(&cdev->lock, flags); |
| 342 | while (!list_empty(&dev->evt_list)) { |
| 343 | evt = list_entry(dev->evt_list.next, struct android_dev_event, list); |
| 344 | list_del_init(&evt->list); |
| 345 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 346 | _android_work(dev, evt->event_code); |
| 347 | kfree(evt); |
| 348 | spin_lock_irqsave(&cdev->lock, flags); |
| 349 | } |
| 350 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 351 | } |
| 352 | |
| 353 | static void android_enable(struct android_dev *dev) |
| 354 | { |
| 355 | struct usb_composite_dev *cdev = dev->cdev; |
| 356 | |
| 357 | if (WARN_ON(!dev->disable_depth)) |
| 358 | return; |
| 359 | pr_err("USB_Debug Enter android_enable:%d\n", dev->disable_depth); |
| 360 | if (--dev->disable_depth == 0) { |
| 361 | usb_add_config(cdev, &android_config_driver, |
| 362 | android_bind_config); |
| 363 | usb_gadget_connect(cdev->gadget); |
| 364 | } |
| 365 | pr_debug("USB_Debug Exit android_enable:%d\n", dev->disable_depth); |
| 366 | } |
| 367 | |
| 368 | extern bool dwc3_hwsulog_is_on(void); |
| 369 | #ifdef CONFIG_DWC3_HWSULOG |
| 370 | static void android_disconnect(struct usb_composite_dev *cdev); |
| 371 | #endif |
| 372 | |
| 373 | static void android_disable(struct android_dev *dev) |
| 374 | { |
| 375 | struct usb_composite_dev *cdev = dev->cdev; |
| 376 | pr_err("USB_Debug Enter android_disable:%d\n", dev->disable_depth); |
| 377 | if (dev->disable_depth++ == 0) { |
| 378 | #ifdef CONFIG_DWC3_HWSULOG |
| 379 | if (dwc3_hwsulog_is_on()) { |
| 380 | android_disconnect(cdev); |
| 381 | mutex_unlock(&dev->mutex); |
| 382 | msleep(2000); |
| 383 | pr_err("hwsulog stop done\n"); |
| 384 | mutex_lock(&dev->mutex); |
| 385 | pr_err("usb stop\n"); |
| 386 | } |
| 387 | #endif |
| 388 | usb_gadget_disconnect(cdev->gadget); |
| 389 | /* Cancel pending control requests */ |
| 390 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); |
| 391 | usb_remove_config(cdev, &android_config_driver); |
| 392 | } |
| 393 | pr_debug("USB_Debug Exit android_disable:%d\n", dev->disable_depth); |
| 394 | } |
| 395 | |
| 396 | /*-------------------------------------------------------------------------*/ |
| 397 | /* Supported functions initialization */ |
| 398 | struct adb_data { |
| 399 | bool opened; |
| 400 | bool enabled; |
| 401 | }; |
| 402 | |
| 403 | static int |
| 404 | adb_function_init(struct android_usb_function *f, |
| 405 | struct usb_composite_dev *cdev) |
| 406 | { |
| 407 | f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL); |
| 408 | if (!f->config) |
| 409 | return -ENOMEM; |
| 410 | |
| 411 | return adb_setup(); |
| 412 | } |
| 413 | |
| 414 | static void adb_function_cleanup(struct android_usb_function *f) |
| 415 | { |
| 416 | adb_cleanup(); |
| 417 | kfree(f->config); |
| 418 | } |
| 419 | |
| 420 | static int |
| 421 | adb_function_bind_config(struct android_usb_function *f, |
| 422 | struct usb_configuration *c) |
| 423 | { |
| 424 | int ret; |
| 425 | pr_debug("USB_Debug Enter adb_function_bind_config.\n"); |
| 426 | ret = adb_bind_config(c); |
| 427 | pr_debug("USB_Debug Exit adb_function_bind_config.\n"); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | static void adb_android_function_enable(struct android_usb_function *f) |
| 432 | { |
| 433 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 434 | struct android_dev *dev = _android_dev; |
| 435 | #endif |
| 436 | struct adb_data *data = f->config; |
| 437 | pr_debug("USB_Debug Enter adb_android_function_enable.\n"); |
| 438 | |
| 439 | data->enabled = true; |
| 440 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 441 | /* Disable the gadget until adbd is ready */ |
| 442 | if (!data->opened) |
| 443 | android_disable(dev); |
| 444 | #endif |
| 445 | pr_debug("USB_Debug Exit adb_android_function_enable.\n"); |
| 446 | |
| 447 | } |
| 448 | |
| 449 | static void adb_android_function_disable(struct android_usb_function *f) |
| 450 | { |
| 451 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 452 | struct android_dev *dev = _android_dev; |
| 453 | #endif |
| 454 | struct adb_data *data = f->config; |
| 455 | |
| 456 | data->enabled = false; |
| 457 | |
| 458 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 459 | /* Balance the disable that was called in closed_callback */ |
| 460 | if (!data->opened) |
| 461 | android_enable(dev); |
| 462 | #endif |
| 463 | } |
| 464 | |
| 465 | static struct android_usb_function adb_function = { |
| 466 | .name = "adb", |
| 467 | .enable = adb_android_function_enable, |
| 468 | .disable = adb_android_function_disable, |
| 469 | .init = adb_function_init, |
| 470 | .cleanup = adb_function_cleanup, |
| 471 | .bind_config = adb_function_bind_config, |
| 472 | }; |
| 473 | |
| 474 | static void adb_ready_callback(void) |
| 475 | { |
| 476 | struct android_dev *dev = _android_dev; |
| 477 | struct adb_data *data = adb_function.config; |
| 478 | |
| 479 | mutex_lock(&dev->mutex); |
| 480 | |
| 481 | data->opened = true; |
| 482 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 483 | if (data->enabled) |
| 484 | android_enable(dev); |
| 485 | #endif |
| 486 | mutex_unlock(&dev->mutex); |
| 487 | } |
| 488 | |
| 489 | static void adb_closed_callback(void) |
| 490 | { |
| 491 | struct android_dev *dev = _android_dev; |
| 492 | struct adb_data *data = adb_function.config; |
| 493 | |
| 494 | mutex_lock(&dev->mutex); |
| 495 | |
| 496 | data->opened = false; |
| 497 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 498 | if (data->enabled) |
| 499 | android_disable(dev); |
| 500 | #endif |
| 501 | mutex_unlock(&dev->mutex); |
| 502 | } |
| 503 | |
| 504 | #ifdef CONFIG_USB_GADGET_CHARGE_ONLY |
| 505 | static int charge_only_function_bind_config(struct android_usb_function *f, |
| 506 | struct usb_configuration *c) |
| 507 | { |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static struct android_usb_function charge_only_function = { |
| 512 | .name = "charge_only", |
| 513 | .bind_config = charge_only_function_bind_config, |
| 514 | }; |
| 515 | |
| 516 | static int charge_only_connected; |
| 517 | static int charge_only_configured; |
| 518 | static int charge_only_disconnected; |
| 519 | void charge_only_send_uevent(int event) |
| 520 | { |
| 521 | struct android_dev *dev = _android_dev; |
| 522 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 523 | char *connected[2] = { "USB_STATE=CONNECTED", NULL }; |
| 524 | char *configured[2] = { "USB_STATE=CONFIGURED", NULL }; |
| 525 | |
| 526 | if (!dev) |
| 527 | return; |
| 528 | |
| 529 | switch (event) { |
| 530 | case 1: |
| 531 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, connected); |
| 532 | charge_only_connected = 1; |
| 533 | charge_only_configured = 0; |
| 534 | charge_only_disconnected = 0; |
| 535 | break; |
| 536 | case 2: |
| 537 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, configured); |
| 538 | charge_only_connected = 1; |
| 539 | charge_only_configured = 1; |
| 540 | charge_only_disconnected = 0; |
| 541 | break; |
| 542 | case 3: |
| 543 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, disconnected); |
| 544 | charge_only_connected = 0; |
| 545 | charge_only_configured = 0; |
| 546 | charge_only_disconnected = 1; |
| 547 | break; |
| 548 | default: |
| 549 | charge_only_connected = 0; |
| 550 | charge_only_configured = 0; |
| 551 | charge_only_disconnected = 0; |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static int charge_only_mode; |
| 557 | int is_charge_only_mode(void) |
| 558 | { |
| 559 | return charge_only_mode; |
| 560 | } |
| 561 | #endif /* CONFIG_USB_GADGET_CHARGE_ONLY */ |
| 562 | |
| 563 | #ifndef CONFIG_USB_TELEPHONY |
| 564 | /* Marvell modem function initialization */ |
| 565 | static int marvell_modem_function_init(struct android_usb_function *f, |
| 566 | struct usb_composite_dev *cdev) |
| 567 | { |
| 568 | return pxa910_modem_gserial_setup(cdev->gadget, 1); |
| 569 | } |
| 570 | |
| 571 | static void marvell_modem_function_cleanup(struct android_usb_function *f) |
| 572 | { |
| 573 | pxa910_modem_gserial_cleanup(); |
| 574 | } |
| 575 | |
| 576 | int marvell_modem_function_bind_config(struct android_usb_function *f, |
| 577 | struct usb_configuration *c) |
| 578 | { |
| 579 | int ret; |
| 580 | pr_debug("USB_Debug Enter marvell_modem_function_bind_config.\n"); |
| 581 | ret = pxa910_acm_bind_config(c, 0); |
| 582 | pr_debug("USB_Debug Exit marvell_modem_function_bind_config.\n"); |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static struct android_usb_function marvell_modem_function = { |
| 587 | .name = "marvell_modem", |
| 588 | .init = marvell_modem_function_init, |
| 589 | .cleanup = marvell_modem_function_cleanup, |
| 590 | .bind_config = marvell_modem_function_bind_config, |
| 591 | }; |
| 592 | |
| 593 | /* Marvell diag function initialization */ |
| 594 | static int marvell_diag_function_init(struct android_usb_function *f, |
| 595 | struct usb_composite_dev *cdev) |
| 596 | { |
| 597 | return pxa910_diag_gserial_setup(cdev->gadget, 1); |
| 598 | } |
| 599 | |
| 600 | static void marvell_diag_function_cleanup(struct android_usb_function *f) |
| 601 | { |
| 602 | pxa910_diag_gserial_cleanup(); |
| 603 | } |
| 604 | |
| 605 | int marvell_diag_function_bind_config(struct android_usb_function *f, |
| 606 | struct usb_configuration *c) |
| 607 | { |
| 608 | int ret; |
| 609 | pr_debug("USB_Debug Enter marvell_diag_function_bind_config.\n"); |
| 610 | ret = pxa910_diag_bind_config(c, 1); |
| 611 | pr_debug("USB_Debug Exit marvell_diag_function_bind_config:%d.\n", ret); |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | static struct android_usb_function marvell_diag_function = { |
| 616 | .name = "marvell_diag", |
| 617 | .init = marvell_diag_function_init, |
| 618 | .cleanup = marvell_diag_function_cleanup, |
| 619 | .bind_config = marvell_diag_function_bind_config, |
| 620 | }; |
| 621 | #endif |
| 622 | |
| 623 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 624 | struct functionfs_config { |
| 625 | bool opened; |
| 626 | bool enabled; |
| 627 | struct ffs_data *data; |
| 628 | }; |
| 629 | |
| 630 | static int ffs_function_init(struct android_usb_function *f, |
| 631 | struct usb_composite_dev *cdev) |
| 632 | { |
| 633 | f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL); |
| 634 | if (!f->config) |
| 635 | return -ENOMEM; |
| 636 | |
| 637 | return functionfs_init(); |
| 638 | } |
| 639 | |
| 640 | static void ffs_function_cleanup(struct android_usb_function *f) |
| 641 | { |
| 642 | functionfs_cleanup(); |
| 643 | kfree(f->config); |
| 644 | } |
| 645 | |
| 646 | static void ffs_function_enable(struct android_usb_function *f) |
| 647 | { |
| 648 | struct android_dev *dev = _android_dev; |
| 649 | struct functionfs_config *config = f->config; |
| 650 | pr_debug("USB_Debug Enter ffs_function_enable.\n"); |
| 651 | |
| 652 | config->enabled = true; |
| 653 | |
| 654 | /* Disable the gadget until the function is ready */ |
| 655 | if (!config->opened) |
| 656 | android_disable(dev); |
| 657 | pr_debug("USB_Debug Exit ffs_function_enable.\n"); |
| 658 | } |
| 659 | |
| 660 | static void ffs_function_disable(struct android_usb_function *f) |
| 661 | { |
| 662 | struct android_dev *dev = _android_dev; |
| 663 | struct functionfs_config *config = f->config; |
| 664 | |
| 665 | config->enabled = false; |
| 666 | |
| 667 | /* Balance the disable that was called in closed_callback */ |
| 668 | if (!config->opened) |
| 669 | android_enable(dev); |
| 670 | } |
| 671 | |
| 672 | static int ffs_function_bind_config(struct android_usb_function *f, |
| 673 | struct usb_configuration *c) |
| 674 | { |
| 675 | struct functionfs_config *config = f->config; |
| 676 | int ret; |
| 677 | pr_debug("USB_Debug Enter ffs_function_bind_config.\n"); |
| 678 | ret = functionfs_bind_config(c->cdev, c, config->data); |
| 679 | pr_debug("USB_Debug Exit ffs_function_bind_config:%d.\n", ret); |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static ssize_t |
| 684 | ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 685 | { |
| 686 | struct android_dev *dev = _android_dev; |
| 687 | int ret; |
| 688 | |
| 689 | mutex_lock(&dev->mutex); |
| 690 | ret = sprintf(buf, "%s\n", dev->ffs_aliases); |
| 691 | mutex_unlock(&dev->mutex); |
| 692 | |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | static ssize_t |
| 697 | ffs_aliases_store(struct device *pdev, struct device_attribute *attr, |
| 698 | const char *buf, size_t size) |
| 699 | { |
| 700 | struct android_dev *dev = _android_dev; |
| 701 | char buff[256]; |
| 702 | |
| 703 | mutex_lock(&dev->mutex); |
| 704 | |
| 705 | if (dev->enabled) { |
| 706 | mutex_unlock(&dev->mutex); |
| 707 | return -EBUSY; |
| 708 | } |
| 709 | |
| 710 | strlcpy(buff, buf, sizeof(buff)); |
| 711 | strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases)); |
| 712 | |
| 713 | mutex_unlock(&dev->mutex); |
| 714 | |
| 715 | return size; |
| 716 | } |
| 717 | |
| 718 | static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show, |
| 719 | ffs_aliases_store); |
| 720 | static struct device_attribute *ffs_function_attributes[] = { |
| 721 | &dev_attr_aliases, |
| 722 | NULL |
| 723 | }; |
| 724 | |
| 725 | static struct android_usb_function ffs_function = { |
| 726 | .name = "ffs", |
| 727 | .init = ffs_function_init, |
| 728 | .enable = ffs_function_enable, |
| 729 | .disable = ffs_function_disable, |
| 730 | .cleanup = ffs_function_cleanup, |
| 731 | .bind_config = ffs_function_bind_config, |
| 732 | .attributes = ffs_function_attributes, |
| 733 | }; |
| 734 | |
| 735 | static int functionfs_ready_callback(struct ffs_data *ffs) |
| 736 | { |
| 737 | struct android_dev *dev = _android_dev; |
| 738 | struct functionfs_config *config = ffs_function.config; |
| 739 | int ret = 0; |
| 740 | |
| 741 | mutex_lock(&dev->mutex); |
| 742 | |
| 743 | ret = functionfs_bind(ffs, dev->cdev); |
| 744 | if (ret) |
| 745 | goto err; |
| 746 | |
| 747 | config->data = ffs; |
| 748 | config->opened = true; |
| 749 | |
| 750 | if (config->enabled) |
| 751 | android_enable(dev); |
| 752 | |
| 753 | err: |
| 754 | mutex_unlock(&dev->mutex); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | static void functionfs_closed_callback(struct ffs_data *ffs) |
| 759 | { |
| 760 | struct android_dev *dev = _android_dev; |
| 761 | struct functionfs_config *config = ffs_function.config; |
| 762 | |
| 763 | mutex_lock(&dev->mutex); |
| 764 | |
| 765 | if (config->enabled) |
| 766 | android_disable(dev); |
| 767 | |
| 768 | config->opened = false; |
| 769 | config->data = NULL; |
| 770 | |
| 771 | functionfs_unbind(ffs); |
| 772 | |
| 773 | mutex_unlock(&dev->mutex); |
| 774 | } |
| 775 | |
| 776 | static void *functionfs_acquire_dev_callback(const char *dev_name) |
| 777 | { |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static void functionfs_release_dev_callback(struct ffs_data *ffs_data) |
| 782 | { |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | #ifdef CONFIG_USB_MV_HSIC_UDC |
| 787 | #define MAX_ACM_INSTANCES 2 |
| 788 | #else |
| 789 | #define MAX_ACM_INSTANCES 2 |
| 790 | #endif |
| 791 | |
| 792 | struct acm_function_config { |
| 793 | int instances; |
| 794 | int instances_on; |
| 795 | struct usb_function *f_acm[MAX_ACM_INSTANCES]; |
| 796 | struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES]; |
| 797 | }; |
| 798 | |
| 799 | static int |
| 800 | acm_function_init(struct android_usb_function *f, |
| 801 | struct usb_composite_dev *cdev) |
| 802 | { |
| 803 | int i; |
| 804 | int ret; |
| 805 | struct acm_function_config *config; |
| 806 | |
| 807 | config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL); |
| 808 | if (!config) |
| 809 | return -ENOMEM; |
| 810 | f->config = config; |
| 811 | |
| 812 | for (i = 0; i < MAX_ACM_INSTANCES; i++) { |
| 813 | config->f_acm_inst[i] = usb_get_function_instance("acm"); |
| 814 | if (IS_ERR(config->f_acm_inst[i])) { |
| 815 | ret = PTR_ERR(config->f_acm_inst[i]); |
| 816 | goto err_usb_get_function_instance; |
| 817 | } |
| 818 | config->f_acm[i] = usb_get_function(config->f_acm_inst[i]); |
| 819 | if (IS_ERR(config->f_acm[i])) { |
| 820 | ret = PTR_ERR(config->f_acm[i]); |
| 821 | goto err_usb_get_function; |
| 822 | } |
| 823 | } |
| 824 | return 0; |
| 825 | err_usb_get_function_instance: |
| 826 | while (i-- > 0) { |
| 827 | usb_put_function(config->f_acm[i]); |
| 828 | err_usb_get_function: |
| 829 | usb_put_function_instance(config->f_acm_inst[i]); |
| 830 | } |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static void acm_function_cleanup(struct android_usb_function *f) |
| 835 | { |
| 836 | int i; |
| 837 | struct acm_function_config *config = f->config; |
| 838 | |
| 839 | for (i = 0; i < MAX_ACM_INSTANCES; i++) { |
| 840 | usb_put_function(config->f_acm[i]); |
| 841 | usb_put_function_instance(config->f_acm_inst[i]); |
| 842 | } |
| 843 | kfree(f->config); |
| 844 | f->config = NULL; |
| 845 | } |
| 846 | |
| 847 | static int |
| 848 | acm_function_bind_config(struct android_usb_function *f, |
| 849 | struct usb_configuration *c) |
| 850 | { |
| 851 | int i; |
| 852 | int ret = 0; |
| 853 | struct acm_function_config *config = f->config; |
| 854 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 855 | struct android_dev *dev = _android_dev; |
| 856 | |
| 857 | if (dev->acm_paramter.instances) |
| 858 | config->instances = dev->acm_paramter.instances; |
| 859 | #endif |
| 860 | pr_debug("USB_Debug Enter acm_function_bind_config.\n"); |
| 861 | config->instances_on = config->instances; |
| 862 | for (i = 0; i < config->instances_on; i++) { |
| 863 | ret = usb_add_function(c, config->f_acm[i]); |
| 864 | if (ret) { |
| 865 | pr_err("Could not bind acm%u config\n", i); |
| 866 | goto err_usb_add_function; |
| 867 | } |
| 868 | } |
| 869 | pr_debug("USB_Debug Exit acm_function_bind_config.\n"); |
| 870 | return 0; |
| 871 | |
| 872 | err_usb_add_function: |
| 873 | while (i-- > 0) |
| 874 | usb_remove_function(c, config->f_acm[i]); |
| 875 | pr_debug("USB_Debug Exit acm_function_bind_config:%d.\n", ret); |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | static ssize_t acm_instances_show(struct device *dev, |
| 880 | struct device_attribute *attr, char *buf) |
| 881 | { |
| 882 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 883 | struct acm_function_config *config = f->config; |
| 884 | return sprintf(buf, "%d\n", config->instances); |
| 885 | } |
| 886 | |
| 887 | static ssize_t acm_instances_store(struct device *dev, |
| 888 | struct device_attribute *attr, const char *buf, size_t size) |
| 889 | { |
| 890 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 891 | struct acm_function_config *config = f->config; |
| 892 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 893 | struct android_dev *_dev = _android_dev; |
| 894 | #endif |
| 895 | int value; |
| 896 | |
| 897 | sscanf(buf, "%d", &value); |
| 898 | if (value > MAX_ACM_INSTANCES) |
| 899 | value = MAX_ACM_INSTANCES; |
| 900 | config->instances = value; |
| 901 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 902 | _dev->acm_paramter.instances = value; |
| 903 | #endif |
| 904 | return size; |
| 905 | } |
| 906 | |
| 907 | static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, |
| 908 | acm_instances_store); |
| 909 | static struct device_attribute *acm_function_attributes[] = { |
| 910 | &dev_attr_instances, |
| 911 | NULL |
| 912 | }; |
| 913 | |
| 914 | static struct android_usb_function acm_function = { |
| 915 | .name = "acm", |
| 916 | .init = acm_function_init, |
| 917 | .cleanup = acm_function_cleanup, |
| 918 | .bind_config = acm_function_bind_config, |
| 919 | .attributes = acm_function_attributes, |
| 920 | }; |
| 921 | |
| 922 | #ifndef CONFIG_USB_TELEPHONY |
| 923 | /* Marvell debug function initialization */ |
| 924 | static int marvell_debug_function_init(struct android_usb_function *f, |
| 925 | struct usb_composite_dev *cdev) |
| 926 | { |
| 927 | return pxa182x_debug_gserial_setup(cdev->gadget, 1); |
| 928 | } |
| 929 | |
| 930 | static void marvell_debug_function_cleanup(struct android_usb_function *f) |
| 931 | { |
| 932 | pxa182x_debug_gserial_cleanup(); |
| 933 | } |
| 934 | |
| 935 | int marvell_debug_function_bind_config(struct android_usb_function *f, |
| 936 | struct usb_configuration *c) |
| 937 | { |
| 938 | int ret; |
| 939 | pr_debug("USB_Debug Enter marvell_debug_function_bind_config\n"); |
| 940 | ret = pxa182x_debug_bind_config(c, 1); |
| 941 | pr_debug("USB_Debug Exit marvell_debug_function_bind_config:%d\n", ret); |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | static struct android_usb_function marvell_debug_function = { |
| 946 | .name = "marvell_debug", |
| 947 | .init = marvell_debug_function_init, |
| 948 | .cleanup = marvell_debug_function_cleanup, |
| 949 | .bind_config = marvell_debug_function_bind_config, |
| 950 | }; |
| 951 | #endif |
| 952 | |
| 953 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 954 | static int |
| 955 | mtp_function_init(struct android_usb_function *f, |
| 956 | struct usb_composite_dev *cdev) |
| 957 | { |
| 958 | return mtp_setup(); |
| 959 | } |
| 960 | |
| 961 | static void mtp_function_cleanup(struct android_usb_function *f) |
| 962 | { |
| 963 | mtp_cleanup(); |
| 964 | } |
| 965 | |
| 966 | static int |
| 967 | mtp_function_bind_config(struct android_usb_function *f, |
| 968 | struct usb_configuration *c) |
| 969 | { |
| 970 | int ret; |
| 971 | pr_debug("USB_Debug Enter mtp_function_bind_config.\n"); |
| 972 | ret = mtp_bind_config(c, false); |
| 973 | pr_debug("USB_Debug Exit mtp_function_bind_config:%d.\n", ret); |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | static int |
| 978 | ptp_function_init(struct android_usb_function *f, |
| 979 | struct usb_composite_dev *cdev) |
| 980 | { |
| 981 | /* nothing to do - initialization is handled by mtp_function_init */ |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static void ptp_function_cleanup(struct android_usb_function *f) |
| 986 | { |
| 987 | /* nothing to do - cleanup is handled by mtp_function_cleanup */ |
| 988 | } |
| 989 | |
| 990 | static int |
| 991 | ptp_function_bind_config(struct android_usb_function *f, |
| 992 | struct usb_configuration *c) |
| 993 | { |
| 994 | int ret; |
| 995 | pr_debug("USB_Debug Enter ptp_function_bind_config.\n"); |
| 996 | ret = mtp_bind_config(c, true); |
| 997 | pr_debug("USB_Debug Exit ptp_function_bind_config:%d.\n", ret); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static int mtp_function_ctrlrequest(struct android_usb_function *f, |
| 1002 | struct usb_composite_dev *cdev, |
| 1003 | const struct usb_ctrlrequest *c) |
| 1004 | { |
| 1005 | return mtp_ctrlrequest(cdev, c); |
| 1006 | } |
| 1007 | |
| 1008 | static int ptp_function_ctrlrequest(struct android_usb_function *f, |
| 1009 | struct usb_composite_dev *cdev, |
| 1010 | const struct usb_ctrlrequest *c) |
| 1011 | { |
| 1012 | return ptp_ctrlrequest(cdev, c); |
| 1013 | } |
| 1014 | |
| 1015 | static struct android_usb_function mtp_function = { |
| 1016 | .name = "mtp", |
| 1017 | .init = mtp_function_init, |
| 1018 | .cleanup = mtp_function_cleanup, |
| 1019 | .bind_config = mtp_function_bind_config, |
| 1020 | .ctrlrequest = mtp_function_ctrlrequest, |
| 1021 | }; |
| 1022 | |
| 1023 | /* PTP function is same as MTP with slightly different interface descriptor */ |
| 1024 | static struct android_usb_function ptp_function = { |
| 1025 | .name = "ptp", |
| 1026 | .init = ptp_function_init, |
| 1027 | .cleanup = ptp_function_cleanup, |
| 1028 | .bind_config = ptp_function_bind_config, |
| 1029 | .ctrlrequest = ptp_function_ctrlrequest, |
| 1030 | }; |
| 1031 | #endif |
| 1032 | |
| 1033 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 1034 | struct ecm_function_config { |
| 1035 | char ethaddr[ETH_ALEN]; |
| 1036 | struct eth_dev *dev; |
| 1037 | }; |
| 1038 | |
| 1039 | static ssize_t ecm_ethaddr_show(struct device *dev, |
| 1040 | struct device_attribute *attr, char *buf) |
| 1041 | { |
| 1042 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1043 | struct ecm_function_config *ecm = f->config; |
| 1044 | return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1045 | ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2], |
| 1046 | ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]); |
| 1047 | } |
| 1048 | |
| 1049 | static ssize_t ecm_ethaddr_store(struct device *dev, |
| 1050 | struct device_attribute *attr, const char *buf, size_t size) |
| 1051 | { |
| 1052 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1053 | struct ecm_function_config *ecm = f->config; |
| 1054 | |
| 1055 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1056 | (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1], |
| 1057 | (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3], |
| 1058 | (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6) |
| 1059 | return size; |
| 1060 | return -EINVAL; |
| 1061 | } |
| 1062 | |
| 1063 | static DEVICE_ATTR(ecm_ethaddr, S_IRUGO|S_IWUSR, ecm_ethaddr_show, |
| 1064 | ecm_ethaddr_store); |
| 1065 | static struct device_attribute *ecm_function_attributes[] = { |
| 1066 | &dev_attr_ecm_ethaddr, |
| 1067 | NULL |
| 1068 | }; |
| 1069 | |
| 1070 | static int |
| 1071 | ecm_function_init(struct android_usb_function *f, |
| 1072 | struct usb_composite_dev *cdev) |
| 1073 | { |
| 1074 | f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL); |
| 1075 | if (!f->config) |
| 1076 | return -ENOMEM; |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | static void |
| 1081 | ecm_function_cleanup(struct android_usb_function *f) |
| 1082 | { |
| 1083 | kfree(f->config); |
| 1084 | f->config = NULL; |
| 1085 | } |
| 1086 | |
| 1087 | static int |
| 1088 | ecm_function_bind_config(struct android_usb_function *f, |
| 1089 | struct usb_configuration *c) |
| 1090 | { |
| 1091 | int ret; |
| 1092 | struct eth_dev *dev; |
| 1093 | struct ecm_function_config *ecm = f->config; |
| 1094 | |
| 1095 | if (!ecm) { |
| 1096 | pr_err("%s: ecm_pdata\n", __func__); |
| 1097 | return -EINVAL; |
| 1098 | } |
| 1099 | pr_debug("USB_Debug Enter ecm_function_bind_config.\n"); |
| 1100 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 1101 | ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2], |
| 1102 | ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]); |
| 1103 | |
| 1104 | #ifdef CONFIG_USB_ETH_RNDIS_ECM |
| 1105 | pr_info("%s: is_rndis_eanabled: %d\n", __func__, is_rndis_enabled); |
| 1106 | save_ecm_usbnet_host_ethaddr(ecm->ethaddr); |
| 1107 | if (is_rndis_enabled) |
| 1108 | dev = gether_setup_name_ecm(c->cdev->gadget, ecm->ethaddr, "usbnet"); |
| 1109 | else |
| 1110 | dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usbnet"); |
| 1111 | #else |
| 1112 | save_usbnet_host_ethaddr(ecm->ethaddr); |
| 1113 | dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usbnet"); |
| 1114 | #endif |
| 1115 | |
| 1116 | if (IS_ERR(dev)) { |
| 1117 | ret = PTR_ERR(dev); |
| 1118 | pr_err("%s: gether_setup failed\n", __func__); |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | ecm->dev = dev; |
| 1123 | |
| 1124 | ret = ecm_bind_config(c, ecm->ethaddr, dev); |
| 1125 | if (ret) { |
| 1126 | pr_err("%s:ecm_bind_config failed\n", __func__); |
| 1127 | gether_cleanup(ecm->dev); |
| 1128 | } |
| 1129 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 1130 | save_net_mac(ecm->ethaddr); |
| 1131 | #endif |
| 1132 | pr_debug("USB_Debug Exit ecm_function_bind_config:%d.\n", ret); |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static void ecm_function_unbind_config(struct android_usb_function *f, |
| 1137 | struct usb_configuration *c) |
| 1138 | { |
| 1139 | struct ecm_function_config *ecm = f->config; |
| 1140 | gether_cleanup(ecm->dev); |
| 1141 | } |
| 1142 | |
| 1143 | static struct android_usb_function ecm_function = { |
| 1144 | .name = "ecm", |
| 1145 | .init = ecm_function_init, |
| 1146 | .cleanup = ecm_function_cleanup, |
| 1147 | .bind_config = ecm_function_bind_config, |
| 1148 | .unbind_config = ecm_function_unbind_config, |
| 1149 | .attributes = ecm_function_attributes, |
| 1150 | }; |
| 1151 | |
| 1152 | #endif |
| 1153 | |
| 1154 | #if defined(CONFIG_CPU_ASR18XX) && defined(CONFIG_USB_TELEPHONY) |
| 1155 | struct usbtel_function_config { |
| 1156 | /* |
| 1157 | * TODO: ADD needed params here |
| 1158 | */ |
| 1159 | int data; |
| 1160 | }; |
| 1161 | |
| 1162 | static int |
| 1163 | usbtel_function_init(struct android_usb_function *f, |
| 1164 | struct usb_composite_dev *cdev) |
| 1165 | { |
| 1166 | f->config = kzalloc(sizeof(struct usbtel_function_config), GFP_KERNEL); |
| 1167 | if (!f->config) |
| 1168 | return -ENOMEM; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static void |
| 1173 | usbtel_function_cleanup(struct android_usb_function *f) |
| 1174 | { |
| 1175 | kfree(f->config); |
| 1176 | f->config = NULL; |
| 1177 | } |
| 1178 | |
| 1179 | static int |
| 1180 | usbtel_function_bind_config(struct android_usb_function *f, |
| 1181 | struct usb_configuration *c) |
| 1182 | { |
| 1183 | int ret; |
| 1184 | struct usbtel_function_config *usbtel = f->config; |
| 1185 | |
| 1186 | if (!usbtel) { |
| 1187 | pr_err("%s: usbtel_pdata\n", __func__); |
| 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | pr_info("USB_Debug Enter usbtel_function_bind_config.\n"); |
| 1191 | |
| 1192 | ret = usbtel_bind_config(c); |
| 1193 | if (ret) { |
| 1194 | pr_err("%s:usbtel_bind_config failed\n", __func__); |
| 1195 | } |
| 1196 | pr_debug("USB_Debug Exit usbtel_function_bind_config:%d.\n", ret); |
| 1197 | return ret; |
| 1198 | } |
| 1199 | |
| 1200 | static void usbtel_function_unbind_config(struct android_usb_function *f, |
| 1201 | struct usb_configuration *c) |
| 1202 | { |
| 1203 | /* TODO */ |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | static struct android_usb_function usbtel_function = { |
| 1208 | .name = "usbtel", |
| 1209 | .init = usbtel_function_init, |
| 1210 | .cleanup = usbtel_function_cleanup, |
| 1211 | .bind_config = usbtel_function_bind_config, |
| 1212 | .unbind_config = usbtel_function_unbind_config, |
| 1213 | }; |
| 1214 | #endif |
| 1215 | |
| 1216 | #if defined(CONFIG_CPU_ASR18XX) && defined(CONFIG_ASR_UAC1) |
| 1217 | #define MAX_UAC1_INSTANCES 1 |
| 1218 | struct uac1_function_config { |
| 1219 | int instances; |
| 1220 | int instances_on; |
| 1221 | struct usb_function *f_uac1[MAX_UAC1_INSTANCES]; |
| 1222 | struct usb_function_instance *f_uac1_inst[MAX_UAC1_INSTANCES]; |
| 1223 | }; |
| 1224 | |
| 1225 | static int |
| 1226 | uac1_function_init(struct android_usb_function *f, |
| 1227 | struct usb_composite_dev *cdev) |
| 1228 | { |
| 1229 | int i; |
| 1230 | int ret; |
| 1231 | struct uac1_function_config *config; |
| 1232 | |
| 1233 | config = kzalloc(sizeof(struct uac1_function_config), GFP_KERNEL); |
| 1234 | if (!config) |
| 1235 | return -ENOMEM; |
| 1236 | f->config = config; |
| 1237 | |
| 1238 | for (i = 0; i < MAX_UAC1_INSTANCES; i++) { |
| 1239 | config->f_uac1_inst[i] = usb_get_function_instance("uac1"); |
| 1240 | if (IS_ERR(config->f_uac1_inst[i])) { |
| 1241 | ret = PTR_ERR(config->f_uac1_inst[i]); |
| 1242 | goto err_usb_get_function_instance; |
| 1243 | } |
| 1244 | config->f_uac1[i] = usb_get_function(config->f_uac1_inst[i]); |
| 1245 | if (IS_ERR(config->f_uac1[i])) { |
| 1246 | ret = PTR_ERR(config->f_uac1[i]); |
| 1247 | goto err_usb_get_function; |
| 1248 | } |
| 1249 | } |
| 1250 | return 0; |
| 1251 | err_usb_get_function_instance: |
| 1252 | while (i-- > 0) { |
| 1253 | usb_put_function(config->f_uac1[i]); |
| 1254 | err_usb_get_function: |
| 1255 | usb_put_function_instance(config->f_uac1_inst[i]); |
| 1256 | } |
| 1257 | return ret; |
| 1258 | } |
| 1259 | |
| 1260 | static void uac1_function_cleanup(struct android_usb_function *f) |
| 1261 | { |
| 1262 | int i; |
| 1263 | struct uac1_function_config *config = f->config; |
| 1264 | |
| 1265 | for (i = 0; i < MAX_UAC1_INSTANCES; i++) { |
| 1266 | usb_put_function(config->f_uac1[i]); |
| 1267 | usb_put_function_instance(config->f_uac1_inst[i]); |
| 1268 | } |
| 1269 | kfree(f->config); |
| 1270 | f->config = NULL; |
| 1271 | } |
| 1272 | |
| 1273 | static int |
| 1274 | uac1_function_bind_config(struct android_usb_function *f, |
| 1275 | struct usb_configuration *c) |
| 1276 | { |
| 1277 | int i; |
| 1278 | int ret = 0; |
| 1279 | struct uac1_function_config *config = f->config; |
| 1280 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1281 | struct android_dev *dev = _android_dev; |
| 1282 | |
| 1283 | if (dev->uac1_paramter.instances) |
| 1284 | config->instances = dev->uac1_paramter.instances; |
| 1285 | config->instances = MAX_UAC1_INSTANCES; //dev->uac1_paramter.instances; |
| 1286 | #endif |
| 1287 | pr_debug("USB_Debug Enter uac1_function_bind_config.\n"); |
| 1288 | config->instances_on = config->instances; |
| 1289 | for (i = 0; i < config->instances_on; i++) { |
| 1290 | ret = usb_add_function(c, config->f_uac1[i]); |
| 1291 | if (ret) { |
| 1292 | pr_err("Could not bind uac1_%u config\n", i); |
| 1293 | goto err_usb_add_function; |
| 1294 | } |
| 1295 | } |
| 1296 | pr_debug("USB_Debug Exit uac1_function_bind_config.\n"); |
| 1297 | return 0; |
| 1298 | |
| 1299 | err_usb_add_function: |
| 1300 | while (i-- > 0) |
| 1301 | usb_remove_function(c, config->f_uac1[i]); |
| 1302 | pr_debug("USB_Debug Exit uac1_function_bind_config:%d.\n", ret); |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | static ssize_t uac1_instances_show(struct device *dev, |
| 1307 | struct device_attribute *attr, char *buf) |
| 1308 | { |
| 1309 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1310 | struct uac1_function_config *config = f->config; |
| 1311 | return sprintf(buf, "%d\n", config->instances); |
| 1312 | } |
| 1313 | |
| 1314 | static ssize_t uac1_instances_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 uac1_function_config *config = f->config; |
| 1319 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1320 | struct android_dev *_dev = _android_dev; |
| 1321 | #endif |
| 1322 | int value; |
| 1323 | |
| 1324 | sscanf(buf, "%d", &value); |
| 1325 | if (value > MAX_UAC1_INSTANCES) |
| 1326 | value = MAX_UAC1_INSTANCES; |
| 1327 | config->instances = value; |
| 1328 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1329 | _dev->uac1_paramter.instances = value; |
| 1330 | #endif |
| 1331 | return size; |
| 1332 | } |
| 1333 | |
| 1334 | static DEVICE_ATTR(uacinstances, S_IRUGO | S_IWUSR, uac1_instances_show, |
| 1335 | uac1_instances_store); |
| 1336 | static struct device_attribute *uac1_function_attributes[] = { |
| 1337 | &dev_attr_uacinstances, |
| 1338 | NULL |
| 1339 | }; |
| 1340 | |
| 1341 | static struct android_usb_function uac1_function = { |
| 1342 | .name = "uac1", |
| 1343 | .init = uac1_function_init, |
| 1344 | .cleanup = uac1_function_cleanup, |
| 1345 | .bind_config = uac1_function_bind_config, |
| 1346 | .attributes = uac1_function_attributes, |
| 1347 | }; |
| 1348 | #endif |
| 1349 | |
| 1350 | #if defined(CONFIG_USB_F_SERIAL) |
| 1351 | #define MAX_GSER_INSTANCES 2 |
| 1352 | struct gser_function_config { |
| 1353 | int instances; |
| 1354 | int instances_on; |
| 1355 | struct usb_function *f_gser[MAX_GSER_INSTANCES]; |
| 1356 | struct usb_function_instance *f_gser_inst[MAX_GSER_INSTANCES]; |
| 1357 | }; |
| 1358 | |
| 1359 | static int |
| 1360 | gser_function_init(struct android_usb_function *f, |
| 1361 | struct usb_composite_dev *cdev) |
| 1362 | { |
| 1363 | int i; |
| 1364 | int ret; |
| 1365 | struct gser_function_config *config; |
| 1366 | |
| 1367 | config = kzalloc(sizeof(struct gser_function_config), GFP_KERNEL); |
| 1368 | if (!config) |
| 1369 | return -ENOMEM; |
| 1370 | f->config = config; |
| 1371 | |
| 1372 | for (i = 0; i < MAX_GSER_INSTANCES; i++) { |
| 1373 | config->f_gser_inst[i] = usb_get_function_instance("gser"); |
| 1374 | if (IS_ERR(config->f_gser_inst[i])) { |
| 1375 | ret = PTR_ERR(config->f_gser_inst[i]); |
| 1376 | goto err_usb_get_function_instance; |
| 1377 | } |
| 1378 | config->f_gser[i] = usb_get_function(config->f_gser_inst[i]); |
| 1379 | if (IS_ERR(config->f_gser[i])) { |
| 1380 | ret = PTR_ERR(config->f_gser[i]); |
| 1381 | goto err_usb_get_function; |
| 1382 | } |
| 1383 | } |
| 1384 | return 0; |
| 1385 | err_usb_get_function_instance: |
| 1386 | while (i-- > 0) { |
| 1387 | usb_put_function(config->f_gser[i]); |
| 1388 | err_usb_get_function: |
| 1389 | usb_put_function_instance(config->f_gser_inst[i]); |
| 1390 | } |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | static void gser_function_cleanup(struct android_usb_function *f) |
| 1395 | { |
| 1396 | int i; |
| 1397 | struct gser_function_config *config = f->config; |
| 1398 | |
| 1399 | for (i = 0; i < MAX_GSER_INSTANCES; i++) { |
| 1400 | usb_put_function(config->f_gser[i]); |
| 1401 | usb_put_function_instance(config->f_gser_inst[i]); |
| 1402 | } |
| 1403 | kfree(f->config); |
| 1404 | f->config = NULL; |
| 1405 | } |
| 1406 | |
| 1407 | static int |
| 1408 | gser_function_bind_config(struct android_usb_function *f, |
| 1409 | struct usb_configuration *c) |
| 1410 | { |
| 1411 | int i; |
| 1412 | int ret = 0; |
| 1413 | struct gser_function_config *config = f->config; |
| 1414 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1415 | struct android_dev *dev = _android_dev; |
| 1416 | |
| 1417 | if (dev->gser_paramter.instances) |
| 1418 | config->instances = dev->gser_paramter.instances; |
| 1419 | #endif |
| 1420 | pr_debug("USB_Debug Enter gser_function_bind_config.\n"); |
| 1421 | config->instances_on = config->instances; |
| 1422 | for (i = 0; i < config->instances_on; i++) { |
| 1423 | ret = usb_add_function(c, config->f_gser[i]); |
| 1424 | if (ret) { |
| 1425 | pr_err("Could not bind gser%u config\n", i); |
| 1426 | goto err_usb_add_function; |
| 1427 | } |
| 1428 | } |
| 1429 | pr_debug("USB_Debug Exit gser_function_bind_config.\n"); |
| 1430 | return 0; |
| 1431 | |
| 1432 | err_usb_add_function: |
| 1433 | while (i-- > 0) |
| 1434 | usb_remove_function(c, config->f_gser[i]); |
| 1435 | pr_debug("USB_Debug Exit gser_function_bind_config:%d.\n", ret); |
| 1436 | return ret; |
| 1437 | } |
| 1438 | |
| 1439 | static ssize_t gser_instances_show(struct device *dev, |
| 1440 | struct device_attribute *attr, char *buf) |
| 1441 | { |
| 1442 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1443 | struct gser_function_config *config = f->config; |
| 1444 | return sprintf(buf, "%d\n", config->instances); |
| 1445 | } |
| 1446 | |
| 1447 | static ssize_t gser_instances_store(struct device *dev, |
| 1448 | struct device_attribute *attr, const char *buf, size_t size) |
| 1449 | { |
| 1450 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1451 | struct gser_function_config *config = f->config; |
| 1452 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1453 | struct android_dev *_dev = _android_dev; |
| 1454 | #endif |
| 1455 | int value; |
| 1456 | |
| 1457 | sscanf(buf, "%d", &value); |
| 1458 | if (value > MAX_GSER_INSTANCES) |
| 1459 | value = MAX_GSER_INSTANCES; |
| 1460 | config->instances = value; |
| 1461 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1462 | _dev->gser_paramter.instances = value; |
| 1463 | #endif |
| 1464 | return size; |
| 1465 | } |
| 1466 | |
| 1467 | static DEVICE_ATTR(gser_instances, S_IRUGO | S_IWUSR, gser_instances_show, |
| 1468 | gser_instances_store); |
| 1469 | static struct device_attribute *gser_function_attributes[] = { |
| 1470 | &dev_attr_gser_instances, |
| 1471 | NULL |
| 1472 | }; |
| 1473 | |
| 1474 | static struct android_usb_function gser_function = { |
| 1475 | .name = "gser", |
| 1476 | .init = gser_function_init, |
| 1477 | .cleanup = gser_function_cleanup, |
| 1478 | .bind_config = gser_function_bind_config, |
| 1479 | .attributes = gser_function_attributes, |
| 1480 | }; |
| 1481 | #endif /* end of CONFIG_USB_F_SERIAL */ |
| 1482 | |
| 1483 | struct rndis_function_config { |
| 1484 | u8 ethaddr[ETH_ALEN]; |
| 1485 | u32 vendorID; |
| 1486 | char manufacturer[256]; |
| 1487 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 1488 | bool wceis; |
| 1489 | struct eth_dev *dev; |
| 1490 | }; |
| 1491 | |
| 1492 | static int |
| 1493 | rndis_function_init(struct android_usb_function *f, |
| 1494 | struct usb_composite_dev *cdev) |
| 1495 | { |
| 1496 | f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL); |
| 1497 | if (!f->config) |
| 1498 | return -ENOMEM; |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static void rndis_function_cleanup(struct android_usb_function *f) |
| 1503 | { |
| 1504 | kfree(f->config); |
| 1505 | f->config = NULL; |
| 1506 | } |
| 1507 | |
| 1508 | static int |
| 1509 | rndis_function_bind_config(struct android_usb_function *f, |
| 1510 | struct usb_configuration *c) |
| 1511 | { |
| 1512 | int ret; |
| 1513 | struct eth_dev *dev; |
| 1514 | struct rndis_function_config *rndis = f->config; |
| 1515 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1516 | struct android_dev *_dev = _android_dev; |
| 1517 | #endif |
| 1518 | if (!rndis) { |
| 1519 | pr_err("%s: rndis_pdata\n", __func__); |
| 1520 | return -1; |
| 1521 | } |
| 1522 | pr_debug("USB_Debug Enter rndis_function_bind_config.\n"); |
| 1523 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 1524 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 1525 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 1526 | |
| 1527 | save_usbnet_host_ethaddr(rndis->ethaddr); |
| 1528 | if (rndis->ethaddr[0]) |
| 1529 | dev = gether_setup_name(c->cdev->gadget, NULL, "usbnet"); |
| 1530 | else |
| 1531 | dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr, |
| 1532 | "usbnet"); |
| 1533 | #ifdef CONFIG_USB_ETH_RNDIS_ECM |
| 1534 | is_rndis_enabled = true; |
| 1535 | #endif |
| 1536 | if (IS_ERR(dev)) { |
| 1537 | ret = PTR_ERR(dev); |
| 1538 | pr_err("%s: gether_setup failed\n", __func__); |
| 1539 | return ret; |
| 1540 | } |
| 1541 | rndis->dev = dev; |
| 1542 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1543 | strlcpy(rndis->manufacturer, _dev->rndis_paramter.manufacturer, |
| 1544 | sizeof(_dev->rndis_paramter.manufacturer)); |
| 1545 | rndis->wceis = _dev->rndis_paramter.wceis; |
| 1546 | rndis->vendorID = _dev->rndis_paramter.vendorID; |
| 1547 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 1548 | save_net_mac(rndis->ethaddr); |
| 1549 | #endif |
| 1550 | #endif |
| 1551 | if (rndis->wceis) { |
| 1552 | /* "Wireless" RNDIS; auto-detected by Windows */ |
| 1553 | rndis_iad_descriptor.bFunctionClass = |
| 1554 | USB_CLASS_WIRELESS_CONTROLLER; |
| 1555 | rndis_iad_descriptor.bFunctionSubClass = 0x01; |
| 1556 | rndis_iad_descriptor.bFunctionProtocol = 0x03; |
| 1557 | rndis_control_intf.bInterfaceClass = |
| 1558 | USB_CLASS_WIRELESS_CONTROLLER; |
| 1559 | rndis_control_intf.bInterfaceSubClass = 0x01; |
| 1560 | rndis_control_intf.bInterfaceProtocol = 0x03; |
| 1561 | } |
| 1562 | ret = rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID, |
| 1563 | rndis->manufacturer, rndis->dev); |
| 1564 | pr_debug("USB_Debug Exit rndis_function_bind_config:%d.\n", ret); |
| 1565 | return ret; |
| 1566 | } |
| 1567 | |
| 1568 | static void rndis_function_unbind_config(struct android_usb_function *f, |
| 1569 | struct usb_configuration *c) |
| 1570 | { |
| 1571 | struct rndis_function_config *rndis = f->config; |
| 1572 | gether_cleanup(rndis->dev); |
| 1573 | #ifdef CONFIG_USB_ETH_RNDIS_ECM |
| 1574 | is_rndis_enabled = false; |
| 1575 | #endif |
| 1576 | } |
| 1577 | |
| 1578 | static ssize_t rndis_manufacturer_show(struct device *dev, |
| 1579 | struct device_attribute *attr, char *buf) |
| 1580 | { |
| 1581 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1582 | struct rndis_function_config *config = f->config; |
| 1583 | return sprintf(buf, "%s\n", config->manufacturer); |
| 1584 | } |
| 1585 | |
| 1586 | static ssize_t rndis_manufacturer_store(struct device *dev, |
| 1587 | struct device_attribute *attr, const char *buf, size_t size) |
| 1588 | { |
| 1589 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1590 | struct rndis_function_config *config = f->config; |
| 1591 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1592 | struct android_dev *_dev = _android_dev; |
| 1593 | #endif |
| 1594 | |
| 1595 | if (size >= sizeof(config->manufacturer)) |
| 1596 | return -EINVAL; |
| 1597 | if (sscanf(buf, "%s", config->manufacturer) == 1) { |
| 1598 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1599 | memset(_dev->rndis_paramter.manufacturer, 0, |
| 1600 | sizeof(_dev->rndis_paramter.manufacturer)); |
| 1601 | strlcpy(_dev->rndis_paramter.manufacturer, |
| 1602 | config->manufacturer , size); |
| 1603 | #endif |
| 1604 | return size; |
| 1605 | |
| 1606 | } |
| 1607 | return -1; |
| 1608 | } |
| 1609 | |
| 1610 | static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show, |
| 1611 | rndis_manufacturer_store); |
| 1612 | |
| 1613 | static ssize_t rndis_wceis_show(struct device *dev, |
| 1614 | struct device_attribute *attr, char *buf) |
| 1615 | { |
| 1616 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1617 | struct rndis_function_config *config = f->config; |
| 1618 | return sprintf(buf, "%d\n", config->wceis); |
| 1619 | } |
| 1620 | |
| 1621 | static ssize_t rndis_wceis_store(struct device *dev, |
| 1622 | struct device_attribute *attr, const char *buf, size_t size) |
| 1623 | { |
| 1624 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1625 | struct rndis_function_config *config = f->config; |
| 1626 | int value; |
| 1627 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1628 | struct android_dev *_dev = _android_dev; |
| 1629 | #endif |
| 1630 | |
| 1631 | if (sscanf(buf, "%d", &value) == 1) { |
| 1632 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1633 | _dev->rndis_paramter.wceis = value; |
| 1634 | #endif |
| 1635 | config->wceis = value; |
| 1636 | return size; |
| 1637 | } |
| 1638 | return -EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show, |
| 1642 | rndis_wceis_store); |
| 1643 | |
| 1644 | static ssize_t rndis_ethaddr_show(struct device *dev, |
| 1645 | struct device_attribute *attr, char *buf) |
| 1646 | { |
| 1647 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1648 | struct rndis_function_config *rndis = f->config; |
| 1649 | return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1650 | rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2], |
| 1651 | rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]); |
| 1652 | } |
| 1653 | |
| 1654 | static ssize_t rndis_ethaddr_store(struct device *dev, |
| 1655 | struct device_attribute *attr, const char *buf, size_t size) |
| 1656 | { |
| 1657 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1658 | struct rndis_function_config *rndis = f->config; |
| 1659 | |
| 1660 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1661 | (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1], |
| 1662 | (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3], |
| 1663 | (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6) |
| 1664 | return size; |
| 1665 | return -EINVAL; |
| 1666 | } |
| 1667 | |
| 1668 | static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show, |
| 1669 | rndis_ethaddr_store); |
| 1670 | |
| 1671 | static ssize_t rndis_vendorID_show(struct device *dev, |
| 1672 | struct device_attribute *attr, char *buf) |
| 1673 | { |
| 1674 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1675 | struct rndis_function_config *config = f->config; |
| 1676 | return sprintf(buf, "%04x\n", config->vendorID); |
| 1677 | } |
| 1678 | |
| 1679 | static ssize_t rndis_vendorID_store(struct device *dev, |
| 1680 | struct device_attribute *attr, const char *buf, size_t size) |
| 1681 | { |
| 1682 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1683 | struct rndis_function_config *config = f->config; |
| 1684 | int value; |
| 1685 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1686 | struct android_dev *_dev = _android_dev; |
| 1687 | #endif |
| 1688 | |
| 1689 | if (sscanf(buf, "%04x", &value) == 1) { |
| 1690 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 1691 | _dev->rndis_paramter.vendorID = value; |
| 1692 | #endif |
| 1693 | config->vendorID = value; |
| 1694 | return size; |
| 1695 | } |
| 1696 | return -EINVAL; |
| 1697 | } |
| 1698 | |
| 1699 | static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show, |
| 1700 | rndis_vendorID_store); |
| 1701 | |
| 1702 | static struct device_attribute *rndis_function_attributes[] = { |
| 1703 | &dev_attr_manufacturer, |
| 1704 | &dev_attr_wceis, |
| 1705 | &dev_attr_ethaddr, |
| 1706 | &dev_attr_vendorID, |
| 1707 | NULL |
| 1708 | }; |
| 1709 | |
| 1710 | static struct android_usb_function rndis_function = { |
| 1711 | .name = "rndis", |
| 1712 | .init = rndis_function_init, |
| 1713 | .cleanup = rndis_function_cleanup, |
| 1714 | .bind_config = rndis_function_bind_config, |
| 1715 | .unbind_config = rndis_function_unbind_config, |
| 1716 | .attributes = rndis_function_attributes, |
| 1717 | }; |
| 1718 | |
| 1719 | #if defined (CONFIG_USB_G_MBIM) |
| 1720 | |
| 1721 | struct mbim_function_config { |
| 1722 | u8 ethaddr[ETH_ALEN]; |
| 1723 | u32 vendorID; |
| 1724 | char manufacturer[256]; |
| 1725 | struct eth_dev *dev; |
| 1726 | enum ncm_mbim_mode mode; |
| 1727 | }; |
| 1728 | |
| 1729 | static int |
| 1730 | mbim_function_init(struct android_usb_function *f, |
| 1731 | struct usb_composite_dev *cdev) |
| 1732 | { |
| 1733 | f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL); |
| 1734 | if (!f->config) |
| 1735 | return -ENOMEM; |
| 1736 | return mbim_init(1); |
| 1737 | } |
| 1738 | |
| 1739 | static int |
| 1740 | ncm_function_init(struct android_usb_function *f, |
| 1741 | struct usb_composite_dev *cdev) |
| 1742 | { |
| 1743 | f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL); |
| 1744 | if (!f->config) |
| 1745 | return -ENOMEM; |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | static int |
| 1750 | ncm_mbim_function_init(struct android_usb_function *f, |
| 1751 | struct usb_composite_dev *cdev) |
| 1752 | { |
| 1753 | f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL); |
| 1754 | if (!f->config) |
| 1755 | return -ENOMEM; |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | static int |
| 1760 | mbim_handle_function_bind_config(struct android_usb_function *f, |
| 1761 | struct usb_configuration *c); |
| 1762 | static int |
| 1763 | ncm_function_bind_config(struct android_usb_function *f, |
| 1764 | struct usb_configuration *c) |
| 1765 | { |
| 1766 | struct mbim_function_config *ncm = f->config; |
| 1767 | int ret; |
| 1768 | |
| 1769 | if (!ncm) { |
| 1770 | pr_err("%s: ncm_pdata\n", __func__); |
| 1771 | return -1; |
| 1772 | } |
| 1773 | pr_debug("USB_Debug Enter ncm_function_bind_config.\n"); |
| 1774 | ncm->mode = MODE_NCM; |
| 1775 | ret = mbim_handle_function_bind_config(f, c); |
| 1776 | pr_debug("USB_Debug Exit ncm_function_bind_config:%d.\n", ret); |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
| 1780 | static int |
| 1781 | ncm_mbim_function_bind_config(struct android_usb_function *f, |
| 1782 | struct usb_configuration *c) |
| 1783 | { |
| 1784 | struct mbim_function_config *ncm_mbim = f->config; |
| 1785 | int ret; |
| 1786 | |
| 1787 | if (!ncm_mbim) { |
| 1788 | pr_err("%s: ncm_mbim_pdata\n", __func__); |
| 1789 | return -1; |
| 1790 | } |
| 1791 | pr_debug("USB_Debug Enter ncm_mbim_function_bind_config.\n"); |
| 1792 | ncm_mbim->mode = MODE_NCM_MBIM; |
| 1793 | ret = mbim_handle_function_bind_config(f, c); |
| 1794 | pr_debug("USB_Debug Exit ncm_mbim_function_bind_config:%d.\n", ret); |
| 1795 | return ret; |
| 1796 | } |
| 1797 | |
| 1798 | static void ncm_function_cleanup(struct android_usb_function *f) |
| 1799 | { |
| 1800 | kfree(f->config); |
| 1801 | f->config = NULL; |
| 1802 | } |
| 1803 | |
| 1804 | static void ncm_mbim_function_cleanup(struct android_usb_function *f) |
| 1805 | { |
| 1806 | kfree(f->config); |
| 1807 | f->config = NULL; |
| 1808 | } |
| 1809 | |
| 1810 | static void mbim_function_cleanup(struct android_usb_function *f) |
| 1811 | { |
| 1812 | fmbim_cleanup(); |
| 1813 | kfree(f->config); |
| 1814 | f->config = NULL; |
| 1815 | } |
| 1816 | |
| 1817 | static int |
| 1818 | mbim_handle_function_bind_config(struct android_usb_function *f, |
| 1819 | struct usb_configuration *c) |
| 1820 | { |
| 1821 | int ret; |
| 1822 | struct eth_dev *dev; |
| 1823 | struct mbim_function_config *mbim = f->config; |
| 1824 | |
| 1825 | if (!mbim) { |
| 1826 | pr_err("%s: mbim_pdata\n", __func__); |
| 1827 | return -1; |
| 1828 | } |
| 1829 | |
| 1830 | pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__, |
| 1831 | mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2], |
| 1832 | mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]); |
| 1833 | |
| 1834 | save_usbnet_host_ethaddr(mbim->ethaddr); |
| 1835 | if (mbim->ethaddr[0]) |
| 1836 | dev = gether_setup_name(c->cdev->gadget, NULL, "usbnet"); |
| 1837 | else |
| 1838 | dev = gether_setup_name(c->cdev->gadget, mbim->ethaddr, |
| 1839 | "usbnet"); |
| 1840 | if (IS_ERR(dev)) { |
| 1841 | ret = PTR_ERR(dev); |
| 1842 | pr_err("%s: gether_setup failed\n", __func__); |
| 1843 | return ret; |
| 1844 | } |
| 1845 | mbim->dev = dev; |
| 1846 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 1847 | save_net_mac(mbim->ethaddr); |
| 1848 | #endif |
| 1849 | return mbim_bind_config(c, mbim->ethaddr, dev, mbim->mode); |
| 1850 | } |
| 1851 | |
| 1852 | static int |
| 1853 | mbim_function_bind_config(struct android_usb_function *f, |
| 1854 | struct usb_configuration *c) |
| 1855 | { |
| 1856 | struct mbim_function_config *mbim = f->config; |
| 1857 | int ret; |
| 1858 | |
| 1859 | if (!mbim) { |
| 1860 | pr_err("%s: mbim_pdata\n", __func__); |
| 1861 | return -1; |
| 1862 | } |
| 1863 | pr_debug("USB_Debug Enter mbim_function_bind_config.\n"); |
| 1864 | mbim->mode = MODE_MBIM; |
| 1865 | ret = mbim_handle_function_bind_config(f, c); |
| 1866 | pr_debug("USB_Debug Exit mbim_function_bind_config:%d.\n", ret); |
| 1867 | return ret; |
| 1868 | } |
| 1869 | |
| 1870 | static void mbim_function_unbind_config(struct android_usb_function *f, |
| 1871 | struct usb_configuration *c) |
| 1872 | { |
| 1873 | struct mbim_function_config *mbim = f->config; |
| 1874 | gether_cleanup(mbim->dev); |
| 1875 | } |
| 1876 | |
| 1877 | static ssize_t mbim_manufacturer_show(struct device *dev, |
| 1878 | struct device_attribute *attr, char *buf) |
| 1879 | { |
| 1880 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1881 | struct mbim_function_config *config = f->config; |
| 1882 | return sprintf(buf, "%s\n", config->manufacturer); |
| 1883 | } |
| 1884 | |
| 1885 | static ssize_t mbim_manufacturer_store(struct device *dev, |
| 1886 | struct device_attribute *attr, const char *buf, size_t size) |
| 1887 | { |
| 1888 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1889 | struct mbim_function_config *config = f->config; |
| 1890 | |
| 1891 | if (size >= sizeof(config->manufacturer)) |
| 1892 | return -EINVAL; |
| 1893 | if (sscanf(buf, "%s", config->manufacturer) == 1) |
| 1894 | return size; |
| 1895 | return -1; |
| 1896 | } |
| 1897 | |
| 1898 | static DEVICE_ATTR(mbim_manufacturer, S_IRUGO | S_IWUSR, mbim_manufacturer_show, |
| 1899 | mbim_manufacturer_store); |
| 1900 | |
| 1901 | static ssize_t mbim_ethaddr_show(struct device *dev, |
| 1902 | struct device_attribute *attr, char *buf) |
| 1903 | { |
| 1904 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1905 | struct mbim_function_config *mbim = f->config; |
| 1906 | return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1907 | mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2], |
| 1908 | mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]); |
| 1909 | } |
| 1910 | |
| 1911 | static ssize_t mbim_ethaddr_store(struct device *dev, |
| 1912 | struct device_attribute *attr, const char *buf, size_t size) |
| 1913 | { |
| 1914 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1915 | struct mbim_function_config *mbim = f->config; |
| 1916 | |
| 1917 | if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1918 | (int *)&mbim->ethaddr[0], (int *)&mbim->ethaddr[1], |
| 1919 | (int *)&mbim->ethaddr[2], (int *)&mbim->ethaddr[3], |
| 1920 | (int *)&mbim->ethaddr[4], (int *)&mbim->ethaddr[5]) == 6) |
| 1921 | return size; |
| 1922 | return -EINVAL; |
| 1923 | } |
| 1924 | |
| 1925 | static DEVICE_ATTR(mbim_ethaddr, S_IRUGO | S_IWUSR, mbim_ethaddr_show, |
| 1926 | mbim_ethaddr_store); |
| 1927 | |
| 1928 | static ssize_t mbim_vendorID_show(struct device *dev, |
| 1929 | struct device_attribute *attr, char *buf) |
| 1930 | { |
| 1931 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1932 | struct mbim_function_config *config = f->config; |
| 1933 | return sprintf(buf, "%04x\n", config->vendorID); |
| 1934 | } |
| 1935 | |
| 1936 | static ssize_t mbim_vendorID_store(struct device *dev, |
| 1937 | struct device_attribute *attr, const char *buf, size_t size) |
| 1938 | { |
| 1939 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 1940 | struct mbim_function_config *config = f->config; |
| 1941 | int value; |
| 1942 | |
| 1943 | if (sscanf(buf, "%04x", &value) == 1) { |
| 1944 | config->vendorID = value; |
| 1945 | return size; |
| 1946 | } |
| 1947 | return -EINVAL; |
| 1948 | } |
| 1949 | |
| 1950 | static DEVICE_ATTR(mbim_vendorID, S_IRUGO | S_IWUSR, mbim_vendorID_show, |
| 1951 | mbim_vendorID_store); |
| 1952 | |
| 1953 | static struct device_attribute *mbim_function_attributes[] = { |
| 1954 | &dev_attr_mbim_manufacturer, |
| 1955 | &dev_attr_mbim_ethaddr, |
| 1956 | &dev_attr_mbim_vendorID, |
| 1957 | NULL |
| 1958 | }; |
| 1959 | |
| 1960 | static struct android_usb_function mbim_function = { |
| 1961 | .name = "mbim", |
| 1962 | .init = mbim_function_init, |
| 1963 | .cleanup = mbim_function_cleanup, |
| 1964 | .bind_config = mbim_function_bind_config, |
| 1965 | .unbind_config = mbim_function_unbind_config, |
| 1966 | .attributes = mbim_function_attributes, |
| 1967 | }; |
| 1968 | |
| 1969 | static struct android_usb_function ncm_function = { |
| 1970 | .name = "ncm", |
| 1971 | .init = ncm_function_init, |
| 1972 | .cleanup = ncm_function_cleanup, |
| 1973 | .bind_config = ncm_function_bind_config, |
| 1974 | .unbind_config = mbim_function_unbind_config, |
| 1975 | .attributes = NULL, |
| 1976 | }; |
| 1977 | |
| 1978 | static struct android_usb_function ncm_mbim_function = { |
| 1979 | .name = "ncm_mbim", |
| 1980 | .init = ncm_mbim_function_init, |
| 1981 | .cleanup = ncm_mbim_function_cleanup, |
| 1982 | .bind_config = ncm_mbim_function_bind_config, |
| 1983 | .unbind_config = mbim_function_unbind_config, |
| 1984 | .attributes = NULL, |
| 1985 | }; |
| 1986 | |
| 1987 | #endif |
| 1988 | |
| 1989 | #define MASS_STORAGE_INSTANCES 1 |
| 1990 | |
| 1991 | struct mass_storage_function_config { |
| 1992 | int instances_on; |
| 1993 | struct usb_function *f_mass_storage[MASS_STORAGE_INSTANCES]; |
| 1994 | struct usb_function_instance *f_mass_storage_inst[MASS_STORAGE_INSTANCES]; |
| 1995 | }; |
| 1996 | |
| 1997 | static int |
| 1998 | mass_storage_function_init(struct android_usb_function *f, |
| 1999 | struct usb_composite_dev *cdev) |
| 2000 | { |
| 2001 | int i; |
| 2002 | int ret; |
| 2003 | struct mass_storage_function_config *config; |
| 2004 | |
| 2005 | config = kzalloc(sizeof(struct mass_storage_function_config), GFP_KERNEL); |
| 2006 | if (!config) |
| 2007 | return -ENOMEM; |
| 2008 | f->config = config; |
| 2009 | |
| 2010 | for (i = 0; i < MASS_STORAGE_INSTANCES; i++) { |
| 2011 | config->f_mass_storage_inst[i] = usb_get_function_instance("mass_storage"); |
| 2012 | if (IS_ERR(config->f_mass_storage_inst[i])) { |
| 2013 | ret = PTR_ERR(config->f_mass_storage_inst[i]); |
| 2014 | goto err_usb_get_function_instance; |
| 2015 | } |
| 2016 | config->f_mass_storage[i] = usb_get_function(config->f_mass_storage_inst[i]); |
| 2017 | if (IS_ERR(config->f_mass_storage[i])) { |
| 2018 | ret = PTR_ERR(config->f_mass_storage[i]); |
| 2019 | goto err_usb_get_function; |
| 2020 | } |
| 2021 | } |
| 2022 | return 0; |
| 2023 | err_usb_get_function_instance: |
| 2024 | while (i-- > 0) { |
| 2025 | usb_put_function(config->f_mass_storage[i]); |
| 2026 | err_usb_get_function: |
| 2027 | usb_put_function_instance(config->f_mass_storage_inst[i]); |
| 2028 | } |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | static void mass_storage_function_cleanup(struct android_usb_function *f) |
| 2033 | { |
| 2034 | int i; |
| 2035 | struct mass_storage_function_config *config = f->config; |
| 2036 | |
| 2037 | for (i = 0; i < MASS_STORAGE_INSTANCES; i++) { |
| 2038 | usb_put_function(config->f_mass_storage[i]); |
| 2039 | usb_put_function_instance(config->f_mass_storage_inst[i]); |
| 2040 | } |
| 2041 | kfree(f->config); |
| 2042 | f->config = NULL; |
| 2043 | } |
| 2044 | |
| 2045 | static int |
| 2046 | mass_storage_function_bind_config(struct android_usb_function *f, |
| 2047 | struct usb_configuration *c) |
| 2048 | { |
| 2049 | int i; |
| 2050 | int ret = 0; |
| 2051 | struct mass_storage_function_config *config = f->config; |
| 2052 | |
| 2053 | pr_info("USB_Debug Enter mass_storage_function_bind_config.\n"); |
| 2054 | config->instances_on = MASS_STORAGE_INSTANCES; |
| 2055 | for (i = 0; i < config->instances_on; i++) { |
| 2056 | ret = usb_add_function(c, config->f_mass_storage[i]); |
| 2057 | if (ret) { |
| 2058 | pr_err("Could not bind mass_storage%u config\n", i); |
| 2059 | goto err_usb_add_function; |
| 2060 | } |
| 2061 | } |
| 2062 | pr_info("USB_Debug Exit mass_storage_function_bind_config.\n"); |
| 2063 | return 0; |
| 2064 | |
| 2065 | err_usb_add_function: |
| 2066 | while (i-- > 0) |
| 2067 | usb_remove_function(c, config->f_mass_storage[i]); |
| 2068 | pr_debug("USB_Debug Exit mass_storage_function_bind_config:%d.\n", ret); |
| 2069 | return ret; |
| 2070 | } |
| 2071 | |
| 2072 | static struct android_usb_function mass_storage_function = { |
| 2073 | .name = "mass_storage", |
| 2074 | .init = mass_storage_function_init, |
| 2075 | .cleanup = mass_storage_function_cleanup, |
| 2076 | .bind_config = mass_storage_function_bind_config, |
| 2077 | }; |
| 2078 | |
| 2079 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 2080 | static int accessory_function_init(struct android_usb_function *f, |
| 2081 | struct usb_composite_dev *cdev) |
| 2082 | { |
| 2083 | return acc_setup(); |
| 2084 | } |
| 2085 | |
| 2086 | static void accessory_function_cleanup(struct android_usb_function *f) |
| 2087 | { |
| 2088 | acc_cleanup(); |
| 2089 | } |
| 2090 | |
| 2091 | static int accessory_function_bind_config(struct android_usb_function *f, |
| 2092 | struct usb_configuration *c) |
| 2093 | { |
| 2094 | int ret; |
| 2095 | pr_debug("USB_Debug Enter accessory_function_bind_config.\n"); |
| 2096 | ret = acc_bind_config(c); |
| 2097 | pr_debug("USB_Debug Exit accessory_function_bind_config:%d.\n", ret); |
| 2098 | return ret; |
| 2099 | } |
| 2100 | |
| 2101 | static int accessory_function_ctrlrequest(struct android_usb_function *f, |
| 2102 | struct usb_composite_dev *cdev, |
| 2103 | const struct usb_ctrlrequest *c) |
| 2104 | { |
| 2105 | return acc_ctrlrequest(cdev, c); |
| 2106 | } |
| 2107 | |
| 2108 | static struct android_usb_function accessory_function = { |
| 2109 | .name = "accessory", |
| 2110 | .init = accessory_function_init, |
| 2111 | .cleanup = accessory_function_cleanup, |
| 2112 | .bind_config = accessory_function_bind_config, |
| 2113 | .ctrlrequest = accessory_function_ctrlrequest, |
| 2114 | }; |
| 2115 | |
| 2116 | static int audio_source_function_init(struct android_usb_function *f, |
| 2117 | struct usb_composite_dev *cdev) |
| 2118 | { |
| 2119 | struct audio_source_config *config; |
| 2120 | |
| 2121 | config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL); |
| 2122 | if (!config) |
| 2123 | return -ENOMEM; |
| 2124 | config->card = -1; |
| 2125 | config->device = -1; |
| 2126 | f->config = config; |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | static void audio_source_function_cleanup(struct android_usb_function *f) |
| 2131 | { |
| 2132 | kfree(f->config); |
| 2133 | } |
| 2134 | |
| 2135 | static int audio_source_function_bind_config(struct android_usb_function *f, |
| 2136 | struct usb_configuration *c) |
| 2137 | { |
| 2138 | struct audio_source_config *config = f->config; |
| 2139 | int ret; |
| 2140 | pr_debug("USB_Debug Enter audio_source_function_bind_config.\n"); |
| 2141 | ret = audio_source_bind_config(c, config); |
| 2142 | pr_debug("USB_Debug Exit audio_source_function_bind_config:%d\n", ret); |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | static void audio_source_function_unbind_config(struct android_usb_function *f, |
| 2147 | struct usb_configuration *c) |
| 2148 | { |
| 2149 | struct audio_source_config *config = f->config; |
| 2150 | |
| 2151 | config->card = -1; |
| 2152 | config->device = -1; |
| 2153 | } |
| 2154 | |
| 2155 | static ssize_t audio_source_pcm_show(struct device *dev, |
| 2156 | struct device_attribute *attr, char *buf) |
| 2157 | { |
| 2158 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 2159 | struct audio_source_config *config = f->config; |
| 2160 | |
| 2161 | /* print PCM card and device numbers */ |
| 2162 | return sprintf(buf, "%d %d\n", config->card, config->device); |
| 2163 | } |
| 2164 | |
| 2165 | static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL); |
| 2166 | |
| 2167 | static struct device_attribute *audio_source_function_attributes[] = { |
| 2168 | &dev_attr_pcm, |
| 2169 | NULL |
| 2170 | }; |
| 2171 | |
| 2172 | static struct android_usb_function audio_source_function = { |
| 2173 | .name = "audio_source", |
| 2174 | .init = audio_source_function_init, |
| 2175 | .cleanup = audio_source_function_cleanup, |
| 2176 | .bind_config = audio_source_function_bind_config, |
| 2177 | .unbind_config = audio_source_function_unbind_config, |
| 2178 | .attributes = audio_source_function_attributes, |
| 2179 | }; |
| 2180 | |
| 2181 | #define CD_ROM_PATH0 "/system/pcsuite/pcsuite.iso" |
| 2182 | |
| 2183 | struct pcsuite_function_config { |
| 2184 | struct fsg_config fsg; |
| 2185 | struct fsg_common *common; |
| 2186 | int nluns; |
| 2187 | }; |
| 2188 | |
| 2189 | static int pcsuite_fsg_init(struct usb_composite_dev *cdev, |
| 2190 | struct android_usb_function *f, |
| 2191 | struct pcsuite_function_config *config) |
| 2192 | { |
| 2193 | struct fsg_common *common; |
| 2194 | int nluns = config->nluns; |
| 2195 | char *name; |
| 2196 | int i, ret; |
| 2197 | |
| 2198 | config->fsg.nluns = nluns; |
| 2199 | for (i = 0; i < nluns; i++) { |
| 2200 | config->fsg.luns[i].removable = 1; |
| 2201 | config->fsg.luns[i].cdrom = 1; |
| 2202 | } |
| 2203 | |
| 2204 | common = fsg_common_init(NULL, cdev, &config->fsg); |
| 2205 | if (IS_ERR(common)) |
| 2206 | return PTR_ERR(common); |
| 2207 | |
| 2208 | /* Create symlinks */ |
| 2209 | for (i = 0; i < nluns; i++) { |
| 2210 | name = get_lun_name(i, nluns); |
| 2211 | ret = sysfs_create_link(&f->dev->kobj, |
| 2212 | &common->luns[i].dev.kobj, name); |
| 2213 | if (ret) { |
| 2214 | while (i-- > 0) { |
| 2215 | name = get_lun_name(i, nluns); |
| 2216 | sysfs_remove_link(&f->dev->kobj, name); |
| 2217 | } |
| 2218 | goto err_create_symlink; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | config->common = common; |
| 2223 | return 0; |
| 2224 | |
| 2225 | err_create_symlink: |
| 2226 | fsg_common_release(&common->ref); |
| 2227 | return ret; |
| 2228 | } |
| 2229 | |
| 2230 | static void pcsuite_fsg_release(struct android_usb_function *f) |
| 2231 | { |
| 2232 | struct pcsuite_function_config *config = f->config; |
| 2233 | struct fsg_common *common = config->common; |
| 2234 | int nluns = config->fsg.nluns; |
| 2235 | char *name; |
| 2236 | int i; |
| 2237 | |
| 2238 | if (nluns < 1) |
| 2239 | return; |
| 2240 | /* Remove symlinks */ |
| 2241 | i = nluns - 1; |
| 2242 | while (i >= 0) { |
| 2243 | name = get_lun_name(i, nluns); |
| 2244 | sysfs_remove_link(&f->dev->kobj, name); |
| 2245 | i--; |
| 2246 | } |
| 2247 | |
| 2248 | fsg_common_release(&common->ref); |
| 2249 | } |
| 2250 | |
| 2251 | static int pcsuite_function_init(struct android_usb_function *f, |
| 2252 | struct usb_composite_dev *cdev) |
| 2253 | { |
| 2254 | struct pcsuite_function_config *config; |
| 2255 | |
| 2256 | config = kzalloc(sizeof(struct pcsuite_function_config), GFP_KERNEL); |
| 2257 | if (!config) |
| 2258 | return -ENOMEM; |
| 2259 | |
| 2260 | /* Init nluns=1 by default */ |
| 2261 | config->nluns = 1; |
| 2262 | config->fsg.luns[0].filename = CD_ROM_PATH0; |
| 2263 | |
| 2264 | f->config = config; |
| 2265 | return 0; |
| 2266 | } |
| 2267 | |
| 2268 | static void pcsuite_function_cleanup(struct android_usb_function *f) |
| 2269 | { |
| 2270 | pcsuite_fsg_release(f); |
| 2271 | kfree(f->config); |
| 2272 | f->config = NULL; |
| 2273 | } |
| 2274 | |
| 2275 | static int pcsuite_function_bind_config(struct android_usb_function *f, |
| 2276 | struct usb_configuration *c) |
| 2277 | { |
| 2278 | struct pcsuite_function_config *config = f->config; |
| 2279 | int ret; |
| 2280 | static int pcsuite_inited = 0; |
| 2281 | |
| 2282 | printk("%s, filename is %s\n", __func__, config->fsg.luns[0].filename); |
| 2283 | |
| 2284 | if (pcsuite_inited == 0) { |
| 2285 | ret = pcsuite_fsg_init(c->cdev, f, config); |
| 2286 | if (ret) |
| 2287 | return ret; |
| 2288 | pcsuite_inited = 1; |
| 2289 | } |
| 2290 | |
| 2291 | return fsg_bind_config(c->cdev, c, config->common); |
| 2292 | } |
| 2293 | |
| 2294 | static struct android_usb_function pcsuite_function = { |
| 2295 | .name = "pcsuite", |
| 2296 | .init = pcsuite_function_init, |
| 2297 | .cleanup = pcsuite_function_cleanup, |
| 2298 | .bind_config = pcsuite_function_bind_config, |
| 2299 | }; |
| 2300 | |
| 2301 | static int midi_function_init(struct android_usb_function *f, |
| 2302 | struct usb_composite_dev *cdev) |
| 2303 | { |
| 2304 | struct midi_alsa_config *config; |
| 2305 | |
| 2306 | config = kzalloc(sizeof(struct midi_alsa_config), GFP_KERNEL); |
| 2307 | f->config = config; |
| 2308 | if (!config) |
| 2309 | return -ENOMEM; |
| 2310 | config->card = -1; |
| 2311 | config->device = -1; |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | static void midi_function_cleanup(struct android_usb_function *f) |
| 2316 | { |
| 2317 | kfree(f->config); |
| 2318 | } |
| 2319 | |
| 2320 | tatic int midi_function_bind_config(struct android_usb_function *f, |
| 2321 | struct usb_configuration *c) |
| 2322 | { |
| 2323 | struct midi_alsa_config *config = f->config; |
| 2324 | |
| 2325 | return f_midi_bind_config(c, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
| 2326 | MIDI_INPUT_PORTS, MIDI_OUTPUT_PORTS, MIDI_BUFFER_SIZE, |
| 2327 | MIDI_QUEUE_LENGTH, config); |
| 2328 | } |
| 2329 | |
| 2330 | static ssize_t midi_alsa_show(struct device *dev, |
| 2331 | struct device_attribute *attr, char *buf) |
| 2332 | { |
| 2333 | struct android_usb_function *f = dev_get_drvdata(dev); |
| 2334 | struct midi_alsa_config *config = f->config; |
| 2335 | |
| 2336 | /* print ALSA card and device numbers */ |
| 2337 | return sprintf(buf, "%d %d\n", config->card, config->device); |
| 2338 | } |
| 2339 | |
| 2340 | static DEVICE_ATTR(alsa, S_IRUGO, midi_alsa_show, NULL); |
| 2341 | |
| 2342 | static struct device_attribute *midi_function_attributes[] = { |
| 2343 | &dev_attr_alsa, |
| 2344 | NULL |
| 2345 | }; |
| 2346 | |
| 2347 | static struct android_usb_function midi_function = { |
| 2348 | .name = "midi", |
| 2349 | .init = midi_function_init, |
| 2350 | .cleanup = midi_function_cleanup, |
| 2351 | .bind_config = midi_function_bind_config, |
| 2352 | .attributes = midi_function_attributes, |
| 2353 | }; |
| 2354 | |
| 2355 | #endif /* ifndef CONFIG_CPU_ASR18XX */ |
| 2356 | |
| 2357 | static struct android_usb_function *supported_functions[] = { |
| 2358 | &adb_function, |
| 2359 | #ifndef CONFIG_USB_TELEPHONY |
| 2360 | &marvell_modem_function, |
| 2361 | &marvell_diag_function, |
| 2362 | #endif |
| 2363 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 2364 | &ffs_function, |
| 2365 | #endif |
| 2366 | &acm_function, |
| 2367 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 2368 | &mtp_function, |
| 2369 | &ptp_function, |
| 2370 | #endif |
| 2371 | &rndis_function, |
| 2372 | #ifndef CONFIG_USB_TELEPHONY |
| 2373 | &marvell_debug_function, |
| 2374 | #endif |
| 2375 | #if defined (CONFIG_USB_G_MBIM) |
| 2376 | &mbim_function, |
| 2377 | &ncm_function, |
| 2378 | &ncm_mbim_function, |
| 2379 | #endif |
| 2380 | &mass_storage_function, |
| 2381 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 2382 | &accessory_function, |
| 2383 | &pcsuite_function, |
| 2384 | &audio_source_function, |
| 2385 | &midi_function, |
| 2386 | #endif |
| 2387 | |
| 2388 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 2389 | &ecm_function, |
| 2390 | #if defined(CONFIG_USB_TELEPHONY) |
| 2391 | &usbtel_function, |
| 2392 | #endif |
| 2393 | |
| 2394 | #if defined(CONFIG_ASR_UAC1) |
| 2395 | &uac1_function, |
| 2396 | #endif |
| 2397 | |
| 2398 | #ifdef CONFIG_USB_F_SERIAL |
| 2399 | &gser_function, |
| 2400 | #endif |
| 2401 | |
| 2402 | #endif |
| 2403 | #ifdef CONFIG_USB_GADGET_CHARGE_ONLY |
| 2404 | &charge_only_function, |
| 2405 | #endif /* CONFIG_USB_GADGET_CHARGE_ONLY */ |
| 2406 | NULL |
| 2407 | }; |
| 2408 | |
| 2409 | |
| 2410 | static int android_init_functions(struct android_usb_function **functions, |
| 2411 | struct usb_composite_dev *cdev) |
| 2412 | { |
| 2413 | struct android_dev *dev = _android_dev; |
| 2414 | struct android_usb_function *f; |
| 2415 | struct device_attribute **attrs; |
| 2416 | struct device_attribute *attr; |
| 2417 | int err; |
| 2418 | // This index should be 1, not 0 |
| 2419 | int index = 1; |
| 2420 | |
| 2421 | for (; (f = *functions++); index++) { |
| 2422 | f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name); |
| 2423 | f->dev = device_create(android_class, dev->dev, |
| 2424 | MKDEV(0, index), f, f->dev_name); |
| 2425 | if (IS_ERR(f->dev)) { |
| 2426 | pr_err("%s: Failed to create dev %s", __func__, |
| 2427 | f->dev_name); |
| 2428 | err = PTR_ERR(f->dev); |
| 2429 | goto err_create; |
| 2430 | } |
| 2431 | |
| 2432 | if (f->init) { |
| 2433 | err = f->init(f, cdev); |
| 2434 | if (err) { |
| 2435 | pr_err("%s: Failed to init %s", __func__, |
| 2436 | f->name); |
| 2437 | goto err_out; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | attrs = f->attributes; |
| 2442 | if (attrs) { |
| 2443 | while ((attr = *attrs++) && !err) |
| 2444 | err = device_create_file(f->dev, attr); |
| 2445 | } |
| 2446 | if (err) { |
| 2447 | pr_err("%s: Failed to create function %s attributes", |
| 2448 | __func__, f->name); |
| 2449 | goto err_out; |
| 2450 | } |
| 2451 | } |
| 2452 | return 0; |
| 2453 | |
| 2454 | err_out: |
| 2455 | device_destroy(android_class, f->dev->devt); |
| 2456 | err_create: |
| 2457 | kfree(f->dev_name); |
| 2458 | return err; |
| 2459 | } |
| 2460 | |
| 2461 | static void android_cleanup_functions(struct android_usb_function **functions) |
| 2462 | { |
| 2463 | struct android_usb_function *f; |
| 2464 | |
| 2465 | while (*functions) { |
| 2466 | f = *functions++; |
| 2467 | /* NOTE: must clean up first, then device_destroy */ |
| 2468 | if (f->cleanup) |
| 2469 | f->cleanup(f); |
| 2470 | if (f->dev) { |
| 2471 | device_destroy(android_class, f->dev->devt); |
| 2472 | kfree(f->dev_name); |
| 2473 | } |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | static int |
| 2478 | android_bind_enabled_functions(struct android_dev *dev, |
| 2479 | struct usb_configuration *c) |
| 2480 | { |
| 2481 | struct android_usb_function *f; |
| 2482 | int ret; |
| 2483 | |
| 2484 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2485 | ret = f->bind_config(f, c); |
| 2486 | if (ret) { |
| 2487 | pr_err("%s: %s failed", __func__, f->name); |
| 2488 | return ret; |
| 2489 | } |
| 2490 | } |
| 2491 | return 0; |
| 2492 | } |
| 2493 | |
| 2494 | static void |
| 2495 | android_unbind_enabled_functions(struct android_dev *dev, |
| 2496 | struct usb_configuration *c) |
| 2497 | { |
| 2498 | struct android_usb_function *f; |
| 2499 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2500 | if (f->unbind_config) |
| 2501 | f->unbind_config(f, c); |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | static int android_enable_function(struct android_dev *dev, char *name) |
| 2506 | { |
| 2507 | struct android_usb_function **functions = dev->functions; |
| 2508 | struct android_usb_function *f; |
| 2509 | while ((f = *functions++)) { |
| 2510 | if (!strcmp(name, f->name)) { |
| 2511 | #if defined (CONFIG_USB_G_MBIM) |
| 2512 | if (f == &mbim_function) |
| 2513 | mbim_enabled = 1; |
| 2514 | #endif |
| 2515 | list_add_tail(&f->enabled_list, |
| 2516 | &dev->enabled_functions); |
| 2517 | return 0; |
| 2518 | } |
| 2519 | } |
| 2520 | return -EINVAL; |
| 2521 | } |
| 2522 | |
| 2523 | /*-------------------------------------------------------------------------*/ |
| 2524 | /* /sys/class/android_usb/android%d/ interface */ |
| 2525 | |
| 2526 | static ssize_t |
| 2527 | functions_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 2528 | { |
| 2529 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2530 | struct android_usb_function *f; |
| 2531 | char *buff = buf; |
| 2532 | |
| 2533 | mutex_lock(&dev->mutex); |
| 2534 | |
| 2535 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) |
| 2536 | buff += sprintf(buff, "%s,", f->name); |
| 2537 | |
| 2538 | mutex_unlock(&dev->mutex); |
| 2539 | |
| 2540 | if (buff != buf) |
| 2541 | *(buff-1) = '\n'; |
| 2542 | return buff - buf; |
| 2543 | } |
| 2544 | |
| 2545 | static ssize_t |
| 2546 | functions_store(struct device *pdev, struct device_attribute *attr, |
| 2547 | const char *buff, size_t size) |
| 2548 | { |
| 2549 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2550 | char *name; |
| 2551 | char buf[256], *b; |
| 2552 | char aliases[256], *a; |
| 2553 | int err; |
| 2554 | int is_ffs; |
| 2555 | int ffs_enabled = 0; |
| 2556 | |
| 2557 | mutex_lock(&dev->mutex); |
| 2558 | |
| 2559 | if (dev->enabled) { |
| 2560 | mutex_unlock(&dev->mutex); |
| 2561 | return -EBUSY; |
| 2562 | } |
| 2563 | |
| 2564 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 2565 | |
| 2566 | strlcpy(buf, buff, sizeof(buf)); |
| 2567 | b = strim(buf); |
| 2568 | pr_info("enabled functions: %s\n", b); |
| 2569 | #if defined (CONFIG_USB_G_MBIM) |
| 2570 | mbim_enabled = 0; |
| 2571 | #endif |
| 2572 | while (b) { |
| 2573 | name = strsep(&b, ","); |
| 2574 | if (!name) |
| 2575 | continue; |
| 2576 | |
| 2577 | is_ffs = 0; |
| 2578 | strlcpy(aliases, dev->ffs_aliases, sizeof(aliases)); |
| 2579 | a = aliases; |
| 2580 | |
| 2581 | while (a) { |
| 2582 | char *alias = strsep(&a, ","); |
| 2583 | if (alias && !strcmp(name, alias)) { |
| 2584 | is_ffs = 1; |
| 2585 | break; |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | if (is_ffs) { |
| 2590 | if (ffs_enabled) |
| 2591 | continue; |
| 2592 | err = android_enable_function(dev, "ffs"); |
| 2593 | if (err) |
| 2594 | pr_err("android_usb: Cannot enable ffs (%d)", |
| 2595 | err); |
| 2596 | else |
| 2597 | ffs_enabled = 1; |
| 2598 | continue; |
| 2599 | } |
| 2600 | |
| 2601 | err = android_enable_function(dev, name); |
| 2602 | if (err) |
| 2603 | pr_err("android_usb: Cannot enable '%s' (%d)", |
| 2604 | name, err); |
| 2605 | } |
| 2606 | |
| 2607 | mutex_unlock(&dev->mutex); |
| 2608 | |
| 2609 | return size; |
| 2610 | } |
| 2611 | |
| 2612 | static ssize_t enable_show(struct device *pdev, struct device_attribute *attr, |
| 2613 | char *buf) |
| 2614 | { |
| 2615 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2616 | return sprintf(buf, "%d\n", dev->enabled); |
| 2617 | } |
| 2618 | |
| 2619 | static ssize_t enable_store(struct device *pdev, struct device_attribute *attr, |
| 2620 | const char *buff, size_t size) |
| 2621 | { |
| 2622 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2623 | struct usb_composite_dev *cdev = dev->cdev; |
| 2624 | struct android_usb_function *f; |
| 2625 | int enabled = 0; |
| 2626 | |
| 2627 | |
| 2628 | if (!cdev) |
| 2629 | return -ENODEV; |
| 2630 | |
| 2631 | mutex_lock(&dev->mutex); |
| 2632 | |
| 2633 | sscanf(buff, "%d", &enabled); |
| 2634 | |
| 2635 | #ifdef CONFIG_USB_GADGET_CHARGE_ONLY |
| 2636 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2637 | if (strcmp(f->name, "charge_only") == 0) { |
| 2638 | charge_only_mode = 1; |
| 2639 | |
| 2640 | if (dev->enabled) |
| 2641 | usb_gadget_disconnect(cdev->gadget); |
| 2642 | |
| 2643 | if (enabled) { |
| 2644 | dev->enabled = true; |
| 2645 | usb_gadget_vbus_connect(cdev->gadget); |
| 2646 | } else { |
| 2647 | dev->enabled = false; |
| 2648 | usb_gadget_vbus_disconnect(cdev->gadget); |
| 2649 | } |
| 2650 | |
| 2651 | pr_info("android_usb: charge_only mode %s\n", |
| 2652 | enabled ? "enabled" : "disabled"); |
| 2653 | |
| 2654 | mutex_unlock(&dev->mutex); |
| 2655 | return size; |
| 2656 | } |
| 2657 | } |
| 2658 | charge_only_mode = 0; |
| 2659 | #endif /* CONFIG_USB_GADGET_CHARGE_ONLY */ |
| 2660 | |
| 2661 | if (enabled && !dev->enabled) { |
| 2662 | /* |
| 2663 | * Update values in composite driver's copy of |
| 2664 | * device descriptor. |
| 2665 | */ |
| 2666 | cdev->desc.idVendor = device_desc.idVendor; |
| 2667 | cdev->desc.idProduct = device_desc.idProduct; |
| 2668 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 2669 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 2670 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 2671 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
| 2672 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2673 | if (f->enable) |
| 2674 | f->enable(f); |
| 2675 | } |
| 2676 | android_enable(dev); |
| 2677 | dev->enabled = true; |
| 2678 | } else if (!enabled && dev->enabled) { |
| 2679 | android_disable(dev); |
| 2680 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2681 | if (f->disable) |
| 2682 | f->disable(f); |
| 2683 | } |
| 2684 | dev->enabled = false; |
| 2685 | } else { |
| 2686 | pr_err("android_usb: already %s\n", |
| 2687 | dev->enabled ? "enabled" : "disabled"); |
| 2688 | } |
| 2689 | |
| 2690 | mutex_unlock(&dev->mutex); |
| 2691 | return size; |
| 2692 | } |
| 2693 | |
| 2694 | static ssize_t state_show(struct device *pdev, struct device_attribute *attr, |
| 2695 | char *buf) |
| 2696 | { |
| 2697 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2698 | struct usb_composite_dev *cdev = dev->cdev; |
| 2699 | char *state = "DISCONNECTED"; |
| 2700 | unsigned long flags; |
| 2701 | |
| 2702 | if (!cdev) |
| 2703 | goto out; |
| 2704 | |
| 2705 | spin_lock_irqsave(&cdev->lock, flags); |
| 2706 | #ifdef CONFIG_USB_GADGET_CHARGE_ONLY |
| 2707 | if (!charge_only_mode) { |
| 2708 | if (cdev->config) |
| 2709 | state = "CONFIGURED"; |
| 2710 | else if (dev->connected) |
| 2711 | state = "CONNECTED"; |
| 2712 | } else { |
| 2713 | if (charge_only_configured) |
| 2714 | state = "CONFIGURED"; |
| 2715 | else if (charge_only_connected) |
| 2716 | state = "CONNECTED"; |
| 2717 | } |
| 2718 | #else |
| 2719 | if (cdev->config) |
| 2720 | state = "CONFIGURED"; |
| 2721 | else if (dev->connected) |
| 2722 | state = "CONNECTED"; |
| 2723 | #endif |
| 2724 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 2725 | out: |
| 2726 | return sprintf(buf, "%s\n", state); |
| 2727 | } |
| 2728 | |
| 2729 | #define DESCRIPTOR_ATTR(field, format_string) \ |
| 2730 | static ssize_t \ |
| 2731 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 2732 | char *buf) \ |
| 2733 | { \ |
| 2734 | return sprintf(buf, format_string, device_desc.field); \ |
| 2735 | } \ |
| 2736 | static ssize_t \ |
| 2737 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 2738 | const char *buf, size_t size) \ |
| 2739 | { \ |
| 2740 | int value; \ |
| 2741 | if (sscanf(buf, format_string, &value) == 1) { \ |
| 2742 | device_desc.field = value; \ |
| 2743 | return size; \ |
| 2744 | } \ |
| 2745 | return -1; \ |
| 2746 | } \ |
| 2747 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 2748 | |
| 2749 | #define DESCRIPTOR_STRING_ATTR(field, buffer) \ |
| 2750 | static ssize_t \ |
| 2751 | field ## _show(struct device *dev, struct device_attribute *attr, \ |
| 2752 | char *buf) \ |
| 2753 | { \ |
| 2754 | return sprintf(buf, "%s", buffer); \ |
| 2755 | } \ |
| 2756 | static ssize_t \ |
| 2757 | field ## _store(struct device *dev, struct device_attribute *attr, \ |
| 2758 | const char *buf, size_t size) \ |
| 2759 | { \ |
| 2760 | if (size >= sizeof(buffer)) \ |
| 2761 | return -EINVAL; \ |
| 2762 | memset(buffer, 0, sizeof(buffer));\ |
| 2763 | /* @size is equal to the length of @buf |
| 2764 | * NOTE: the string in @buf contains \n at last |
| 2765 | * And we must remove the \n, otherwise Win8 will |
| 2766 | * treat the string as illegal and will cause |
| 2767 | * enumeration fail. |
| 2768 | */\ |
| 2769 | strncpy(buffer, buf, size - 1);\ |
| 2770 | return size; \ |
| 2771 | } \ |
| 2772 | static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store); |
| 2773 | |
| 2774 | |
| 2775 | DESCRIPTOR_ATTR(idVendor, "%04x\n") |
| 2776 | DESCRIPTOR_ATTR(idProduct, "%04x\n") |
| 2777 | DESCRIPTOR_ATTR(bcdDevice, "%04x\n") |
| 2778 | DESCRIPTOR_ATTR(bDeviceClass, "%d\n") |
| 2779 | DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n") |
| 2780 | DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n") |
| 2781 | DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string) |
| 2782 | DESCRIPTOR_STRING_ATTR(iProduct, product_string) |
| 2783 | DESCRIPTOR_STRING_ATTR(iSerial, serial_string) |
| 2784 | |
| 2785 | static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, |
| 2786 | functions_store); |
| 2787 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
| 2788 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
| 2789 | |
| 2790 | #ifdef CONFIG_USB_REMOTE_WAKEUP |
| 2791 | static struct usb_otg_descriptor otg_descriptor = { |
| 2792 | .bLength = sizeof(otg_descriptor), |
| 2793 | .bDescriptorType = USB_DT_OTG, |
| 2794 | |
| 2795 | /* REVISIT SRP-only hardware is possible, although |
| 2796 | * it would not be called "OTG" ... |
| 2797 | */ |
| 2798 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 2799 | }; |
| 2800 | |
| 2801 | static const struct usb_descriptor_header __maybe_unused *otg_desc[] = { |
| 2802 | (struct usb_descriptor_header *) &otg_descriptor, |
| 2803 | NULL, |
| 2804 | }; |
| 2805 | |
| 2806 | static ssize_t |
| 2807 | wakeup_enable_show(struct device *pdev, |
| 2808 | struct device_attribute *attr, char *buf) |
| 2809 | { |
| 2810 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2811 | return sprintf(buf , "%d\n" , dev->remote_wakeup_enable); |
| 2812 | |
| 2813 | } |
| 2814 | |
| 2815 | static ssize_t |
| 2816 | wakeup_enable_store(struct device *pdev, struct device_attribute *attr, |
| 2817 | const char *buff, size_t size) |
| 2818 | { |
| 2819 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2820 | struct usb_composite_dev *cdev = dev->cdev; |
| 2821 | int enabled; |
| 2822 | mutex_lock(&dev->mutex); |
| 2823 | sscanf(buff , "%d" , &enabled); |
| 2824 | dev->remote_wakeup_enable = enabled; |
| 2825 | if (enabled) |
| 2826 | usb_gadget_wakeup(cdev->gadget); |
| 2827 | |
| 2828 | mutex_unlock(&dev->mutex); |
| 2829 | |
| 2830 | return size; |
| 2831 | } |
| 2832 | static DEVICE_ATTR(wakeup, S_IRUGO | S_IWUSR, |
| 2833 | wakeup_enable_show, wakeup_enable_store); |
| 2834 | #endif |
| 2835 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 2836 | |
| 2837 | static ssize_t hostEthaddr_show(struct device *pdev, |
| 2838 | struct device_attribute *attr , char *buf) |
| 2839 | { |
| 2840 | struct android_dev *dev = dev_get_drvdata(pdev); |
| 2841 | return sprintf(buf , "%s\n" , dev->ethaddr); |
| 2842 | } |
| 2843 | |
| 2844 | static DEVICE_ATTR(hostEthaddr , S_IRUGO, hostEthaddr_show , NULL); |
| 2845 | #endif |
| 2846 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 2847 | static int force_usb_disconnect; |
| 2848 | void enable_force_usb_disconnect(void) |
| 2849 | { |
| 2850 | force_usb_disconnect = 1; |
| 2851 | } |
| 2852 | |
| 2853 | void disable_force_usb_disconnect(void) |
| 2854 | { |
| 2855 | force_usb_disconnect = 0; |
| 2856 | } |
| 2857 | |
| 2858 | void force_send_usb_dis_event(struct android_dev *dev) |
| 2859 | { |
| 2860 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 2861 | |
| 2862 | if (force_usb_disconnect) |
| 2863 | kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, disconnected); |
| 2864 | } |
| 2865 | |
| 2866 | void android_dev_enable(uint8_t enabled) |
| 2867 | { |
| 2868 | struct android_dev *dev = _android_dev; |
| 2869 | struct usb_composite_dev *cdev = dev->cdev; |
| 2870 | struct android_usb_function *f; |
| 2871 | |
| 2872 | if (!cdev) { |
| 2873 | pr_info("cdev is null\n"); |
| 2874 | return; |
| 2875 | } |
| 2876 | |
| 2877 | /* send disconnect event to APP to close the file handle */ |
| 2878 | force_send_usb_dis_event(dev); |
| 2879 | |
| 2880 | pr_err("USB_Debug Enter android_dev_enable:%d.\n", enabled); |
| 2881 | mutex_lock(&dev->mutex); |
| 2882 | |
| 2883 | if (enabled && !dev->enabled) { |
| 2884 | /* |
| 2885 | * Update values in composite driver's copy of |
| 2886 | * device descriptor. |
| 2887 | */ |
| 2888 | cdev->desc.idVendor = device_desc.idVendor; |
| 2889 | cdev->desc.idProduct = device_desc.idProduct; |
| 2890 | cdev->desc.bcdDevice = device_desc.bcdDevice; |
| 2891 | cdev->desc.bDeviceClass = device_desc.bDeviceClass; |
| 2892 | cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass; |
| 2893 | cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol; |
| 2894 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2895 | if (f->enable) |
| 2896 | f->enable(f); |
| 2897 | } |
| 2898 | android_enable(dev); |
| 2899 | dev->enabled = true; |
| 2900 | mutex_unlock(&dev->mutex); |
| 2901 | } else if (!enabled && dev->enabled) { |
| 2902 | /* |
| 2903 | * check the netifd status before usb plug out instead of after |
| 2904 | * plug in to avoid the corner case netifd is initiating itself |
| 2905 | * while usb plug happens and confict with each other, here we |
| 2906 | * we can wait for netifd init done and exec the plug routine |
| 2907 | */ |
| 2908 | #ifndef CONFIG_USB_TELEPHONY |
| 2909 | wait_usbnet_br_up_and_brigded(); |
| 2910 | #endif |
| 2911 | android_disable(dev); |
| 2912 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 2913 | if (f->disable) |
| 2914 | f->disable(f); |
| 2915 | } |
| 2916 | dev->enabled = false; |
| 2917 | mutex_unlock(&dev->mutex); |
| 2918 | /* |
| 2919 | * wait until usbnet IFF_DOWN is set when usb plugout routine |
| 2920 | * finishes, together with wait_usbnet_br_up_and_brigded we |
| 2921 | * garrantee following sequence, |
| 2922 | * usb_plug_in->usbnet_up->usb_plug_out->usbnet_down->.... |
| 2923 | */ |
| 2924 | #ifndef CONFIG_USB_TELEPHONY |
| 2925 | wait_usbnet_if_down(); |
| 2926 | #endif |
| 2927 | } else { |
| 2928 | pr_err("android_usb: already %s\n", |
| 2929 | dev->enabled ? "enabled" : "disabled"); |
| 2930 | mutex_unlock(&dev->mutex); |
| 2931 | } |
| 2932 | |
| 2933 | pr_err("USB_Debug Exit android_dev_enable:%d.\n", enabled); |
| 2934 | } |
| 2935 | #endif |
| 2936 | |
| 2937 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 2938 | #include "os_detect.c" |
| 2939 | #endif |
| 2940 | |
| 2941 | static struct device_attribute *android_usb_attributes[] = { |
| 2942 | &dev_attr_idVendor, |
| 2943 | &dev_attr_idProduct, |
| 2944 | &dev_attr_bcdDevice, |
| 2945 | &dev_attr_bDeviceClass, |
| 2946 | &dev_attr_bDeviceSubClass, |
| 2947 | &dev_attr_bDeviceProtocol, |
| 2948 | &dev_attr_iManufacturer, |
| 2949 | &dev_attr_iProduct, |
| 2950 | &dev_attr_iSerial, |
| 2951 | &dev_attr_functions, |
| 2952 | &dev_attr_enable, |
| 2953 | &dev_attr_state, |
| 2954 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 2955 | &dev_attr_win7, |
| 2956 | &dev_attr_win8, |
| 2957 | &dev_attr_apple, |
| 2958 | &dev_attr_olinux, |
| 2959 | &dev_attr_os, |
| 2960 | &dev_attr_win7_s2, |
| 2961 | &dev_attr_win8_s2, |
| 2962 | &dev_attr_apple_s2, |
| 2963 | &dev_attr_olinux_s2, |
| 2964 | #endif |
| 2965 | #ifdef CONFIG_USB_REMOTE_WAKEUP |
| 2966 | &dev_attr_wakeup, |
| 2967 | #endif |
| 2968 | #if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901) |
| 2969 | &dev_attr_hostEthaddr, |
| 2970 | #endif |
| 2971 | NULL |
| 2972 | }; |
| 2973 | |
| 2974 | /*-------------------------------------------------------------------------*/ |
| 2975 | /* Composite driver */ |
| 2976 | |
| 2977 | static int android_bind_config(struct usb_configuration *c) |
| 2978 | { |
| 2979 | struct android_dev *dev = _android_dev; |
| 2980 | int ret = 0; |
| 2981 | #ifdef CONFIG_USB_REMOTE_WAKEUP |
| 2982 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 2983 | #endif |
| 2984 | ret = android_bind_enabled_functions(dev, c); |
| 2985 | if (ret) |
| 2986 | return ret; |
| 2987 | |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
| 2991 | static void android_unbind_config(struct usb_configuration *c) |
| 2992 | { |
| 2993 | struct android_dev *dev = _android_dev; |
| 2994 | |
| 2995 | android_unbind_enabled_functions(dev, c); |
| 2996 | } |
| 2997 | |
| 2998 | static int android_bind(struct usb_composite_dev *cdev) |
| 2999 | { |
| 3000 | struct android_dev *dev = _android_dev; |
| 3001 | struct usb_gadget *gadget = cdev->gadget; |
| 3002 | int id, ret; |
| 3003 | |
| 3004 | /* Save the default handler */ |
| 3005 | dev->setup_complete = cdev->req->complete; |
| 3006 | |
| 3007 | /* |
| 3008 | * Start disconnected. Userspace will connect the gadget once |
| 3009 | * it is done configuring the functions. |
| 3010 | */ |
| 3011 | usb_gadget_disconnect(gadget); |
| 3012 | |
| 3013 | ret = android_init_functions(dev->functions, cdev); |
| 3014 | if (ret) |
| 3015 | return ret; |
| 3016 | |
| 3017 | /* Allocate string descriptor numbers ... note that string |
| 3018 | * contents can be overridden by the composite_dev glue. |
| 3019 | */ |
| 3020 | id = usb_string_id(cdev); |
| 3021 | if (id < 0) |
| 3022 | return id; |
| 3023 | strings_dev[STRING_MANUFACTURER_IDX].id = id; |
| 3024 | device_desc.iManufacturer = id; |
| 3025 | |
| 3026 | id = usb_string_id(cdev); |
| 3027 | if (id < 0) |
| 3028 | return id; |
| 3029 | strings_dev[STRING_PRODUCT_IDX].id = id; |
| 3030 | device_desc.iProduct = id; |
| 3031 | |
| 3032 | /* Default strings - should be updated by userspace */ |
| 3033 | #if 0 |
| 3034 | strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1); |
| 3035 | strncpy(product_string, "Android", sizeof(product_string) - 1); |
| 3036 | strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1); |
| 3037 | #endif |
| 3038 | id = usb_string_id(cdev); |
| 3039 | if (id < 0) |
| 3040 | return id; |
| 3041 | strings_dev[STRING_SERIAL_IDX].id = id; |
| 3042 | device_desc.iSerialNumber = id; |
| 3043 | |
| 3044 | dev->cdev = cdev; |
| 3045 | |
| 3046 | return 0; |
| 3047 | } |
| 3048 | |
| 3049 | static int android_usb_unbind(struct usb_composite_dev *cdev) |
| 3050 | { |
| 3051 | struct android_dev *dev = _android_dev; |
| 3052 | |
| 3053 | cancel_work_sync(&dev->work); |
| 3054 | dev->cdev = NULL; |
| 3055 | android_cleanup_functions(dev->functions); |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
| 3059 | /* HACK: android needs to override setup for accessory to work */ |
| 3060 | static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c); |
| 3061 | |
| 3062 | static int |
| 3063 | android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c) |
| 3064 | { |
| 3065 | struct android_dev *dev = _android_dev; |
| 3066 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 3067 | struct usb_request *req = cdev->req; |
| 3068 | struct android_usb_function *f; |
| 3069 | int value = -EOPNOTSUPP; |
| 3070 | unsigned long flags; |
| 3071 | struct android_dev_event *evt; |
| 3072 | |
| 3073 | req->zero = 0; |
| 3074 | req->length = 0; |
| 3075 | req->complete = dev->setup_complete; |
| 3076 | gadget->ep0->driver_data = cdev; |
| 3077 | |
| 3078 | list_for_each_entry(f, &dev->enabled_functions, enabled_list) { |
| 3079 | if (f->ctrlrequest) { |
| 3080 | value = f->ctrlrequest(f, cdev, c); |
| 3081 | if (value >= 0) |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | /* Special case the accessory function. |
| 3087 | * It needs to handle control requests before it is enabled. |
| 3088 | */ |
| 3089 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 3090 | if (value < 0) |
| 3091 | value = acc_ctrlrequest(cdev, c); |
| 3092 | #endif |
| 3093 | |
| 3094 | if (value < 0) |
| 3095 | value = composite_setup_func(gadget, c); |
| 3096 | |
| 3097 | spin_lock_irqsave(&cdev->lock, flags); |
| 3098 | if (!dev->connected) { |
| 3099 | dev->connected = true; |
| 3100 | evt = create_dev_evt(USB_DEV_CONNECT); |
| 3101 | /*spin lock is held */ |
| 3102 | if (likely(evt)) |
| 3103 | list_add_tail(&evt->list, &dev->evt_list); |
| 3104 | schedule_work(&dev->work); |
| 3105 | } else if (c->bRequest == USB_REQ_SET_CONFIGURATION && |
| 3106 | c->wValue && cdev->config) { |
| 3107 | evt = create_dev_evt(USB_DEV_CONFIGURE); |
| 3108 | /* spin lock is held */ |
| 3109 | if (likely(evt)) |
| 3110 | list_add_tail(&evt->list, &dev->evt_list); |
| 3111 | schedule_work(&dev->work); |
| 3112 | } else if (c->bRequest == USB_REQ_SET_CONFIGURATION && |
| 3113 | (!c->wValue)) { |
| 3114 | evt = create_dev_evt(USB_DEV_DISCONNECT); |
| 3115 | /* spin lock is held */ |
| 3116 | if (likely(evt)) |
| 3117 | list_add_tail(&evt->list, &dev->evt_list); |
| 3118 | schedule_work(&dev->work); |
| 3119 | } |
| 3120 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 3121 | |
| 3122 | return value; |
| 3123 | } |
| 3124 | |
| 3125 | static void android_disconnect(struct usb_composite_dev *cdev) |
| 3126 | { |
| 3127 | struct android_dev *dev = _android_dev; |
| 3128 | struct android_dev_event *evt; |
| 3129 | |
| 3130 | /* accessory HID support can be active while the |
| 3131 | accessory function is not actually enabled, |
| 3132 | so we need to inform it when we are disconnected. |
| 3133 | */ |
| 3134 | #if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901) |
| 3135 | acc_disconnect(); |
| 3136 | #endif |
| 3137 | if (!in_interrupt()) { |
| 3138 | pr_info("+preempt disable\n"); |
| 3139 | preempt_disable(); |
| 3140 | } |
| 3141 | |
| 3142 | evt = create_dev_evt(USB_DEV_DISCONNECT); |
| 3143 | /* spin lock is held */ |
| 3144 | if (likely(evt)) |
| 3145 | list_add_tail(&evt->list, &dev->evt_list); |
| 3146 | if (!in_interrupt()) { |
| 3147 | pr_info("-preempt enable\n"); |
| 3148 | preempt_enable(); |
| 3149 | } |
| 3150 | |
| 3151 | dev->connected = false; |
| 3152 | schedule_work(&dev->work); |
| 3153 | } |
| 3154 | |
| 3155 | static struct usb_composite_driver android_usb_driver = { |
| 3156 | #ifdef CONFIG_USB_TELEPHONY |
| 3157 | .name = "android_usbtel", |
| 3158 | #else |
| 3159 | .name = "android_usb", |
| 3160 | #endif |
| 3161 | .dev = &device_desc, |
| 3162 | .strings = dev_strings, |
| 3163 | .bind = android_bind, |
| 3164 | .unbind = android_usb_unbind, |
| 3165 | .disconnect = android_disconnect, |
| 3166 | #if defined(CONFIG_CPU_ASR1901) |
| 3167 | .max_speed = USB_SPEED_SUPER_PLUS, |
| 3168 | #elif defined(CONFIG_CPU_ASR18XX) |
| 3169 | .max_speed = USB_SPEED_SUPER, |
| 3170 | #else |
| 3171 | .max_speed = USB_SPEED_HIGH, |
| 3172 | #endif |
| 3173 | }; |
| 3174 | |
| 3175 | static int android_create_device(struct android_dev *dev) |
| 3176 | { |
| 3177 | struct device_attribute **attrs = android_usb_attributes; |
| 3178 | struct device_attribute *attr; |
| 3179 | int err; |
| 3180 | |
| 3181 | dev->dev = device_create(android_class, NULL, |
| 3182 | MKDEV(0, 0), NULL, "android0"); |
| 3183 | if (IS_ERR(dev->dev)) |
| 3184 | return PTR_ERR(dev->dev); |
| 3185 | |
| 3186 | dev_set_drvdata(dev->dev, dev); |
| 3187 | |
| 3188 | while ((attr = *attrs++)) { |
| 3189 | err = device_create_file(dev->dev, attr); |
| 3190 | if (err) { |
| 3191 | device_destroy(android_class, dev->dev->devt); |
| 3192 | return err; |
| 3193 | } |
| 3194 | } |
| 3195 | return 0; |
| 3196 | } |
| 3197 | |
| 3198 | static int __init init(void) |
| 3199 | { |
| 3200 | struct android_dev *dev; |
| 3201 | int err; |
| 3202 | |
| 3203 | #ifdef CONFIG_CPU_ASR18XX |
| 3204 | if (cpu_is_asr1803()) |
| 3205 | android_usb_driver.max_speed = USB_SPEED_HIGH; |
| 3206 | #endif |
| 3207 | |
| 3208 | #ifdef CONFIG_USB_TELEPHONY |
| 3209 | android_class = class_create(THIS_MODULE, "android_usbtel"); |
| 3210 | #else |
| 3211 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 3212 | #endif |
| 3213 | if (IS_ERR(android_class)) |
| 3214 | return PTR_ERR(android_class); |
| 3215 | |
| 3216 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 3217 | if (!dev) { |
| 3218 | err = -ENOMEM; |
| 3219 | goto err_dev; |
| 3220 | } |
| 3221 | |
| 3222 | dev->disable_depth = 1; |
| 3223 | dev->functions = supported_functions; |
| 3224 | INIT_LIST_HEAD(&dev->enabled_functions); |
| 3225 | INIT_WORK(&dev->work, android_work); |
| 3226 | mutex_init(&dev->mutex); |
| 3227 | |
| 3228 | err = android_create_device(dev); |
| 3229 | if (err) { |
| 3230 | pr_err("%s: failed to create android device %d", __func__, err); |
| 3231 | goto err_create; |
| 3232 | } |
| 3233 | |
| 3234 | _android_dev = dev; |
| 3235 | INIT_LIST_HEAD(&_android_dev->evt_list); |
| 3236 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 3237 | usb_os_detect_init(); |
| 3238 | #endif |
| 3239 | |
| 3240 | err = usb_composite_probe(&android_usb_driver); |
| 3241 | if (err) { |
| 3242 | pr_err("%s: failed to probe driver %d", __func__, err); |
| 3243 | goto err_probe; |
| 3244 | } |
| 3245 | |
| 3246 | /* HACK: exchange composite's setup with ours */ |
| 3247 | composite_setup_func = android_usb_driver.gadget_driver.setup; |
| 3248 | android_usb_driver.gadget_driver.setup = android_setup; |
| 3249 | |
| 3250 | return 0; |
| 3251 | |
| 3252 | err_probe: |
| 3253 | device_destroy(android_class, dev->dev->devt); |
| 3254 | err_create: |
| 3255 | kfree(dev); |
| 3256 | err_dev: |
| 3257 | class_destroy(android_class); |
| 3258 | return err; |
| 3259 | } |
| 3260 | late_initcall(init); |
| 3261 | |
| 3262 | static void __exit cleanup(void) |
| 3263 | { |
| 3264 | usb_composite_unregister(&android_usb_driver); |
| 3265 | class_destroy(android_class); |
| 3266 | kfree(_android_dev); |
| 3267 | _android_dev = NULL; |
| 3268 | } |
| 3269 | module_exit(cleanup); |