blob: 76d75ea08dbfeb00e88bc1b8d0fc5e0b52c11750 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +053027#include <linux/of.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080028
29#include <linux/usb/ch9.h>
30#include <linux/usb/composite.h>
31#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080033
Manu Gautam43c61a12012-08-22 17:09:37 -070034#include <mach/diag_dload.h>
35
Benoit Goby1e8ce152011-12-12 13:01:23 -080036#include "gadget_chips.h"
37
38/*
39 * Kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 */
45#include "usbstring.c"
46#include "config.c"
47#include "epautoconf.c"
48#include "composite.c"
49
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#include "f_diag.c"
Shimrit Malichia00d7322012-08-05 13:56:28 +030051#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053052#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053053#include "f_rmnet_sdio.c"
54#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053055#include "f_rmnet.c"
Mike Lockwood11874822012-08-27 16:43:53 +053056#include "f_audio_source.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080057#include "f_mass_storage.c"
58#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053059#include "u_sdio.c"
60#include "u_smd.c"
61#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053062#include "u_rmnet_ctrl_smd.c"
Bar Weiner0dae81b2013-02-14 13:53:54 +020063#include "u_rmnet_ctrl_qti.c"
Jack Pham427f6922011-11-23 19:42:00 -080064#include "u_ctrl_hsic.c"
65#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053066#include "u_ctrl_hsuart.c"
67#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053068#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080069#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080070#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053071#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080072#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080073#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080074#define USB_ETH_RNDIS y
75#include "f_rndis.c"
76#include "rndis.c"
Anna Perela8c991d2012-04-09 16:44:46 +030077#include "u_bam_data.c"
78#include "f_mbim.c"
Anna Perelf9d01552012-11-20 15:56:32 +020079#include "f_ecm.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030080#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020081#include "f_qc_rndis.c"
Jack Pham0ad82e62012-09-27 17:31:08 -070082#include "u_ether.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030083#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053084#ifdef CONFIG_TARGET_CORE
85#include "f_tcm.c"
86#endif
Jack Pham2ec5fdc2012-09-26 10:13:48 -070087#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +030088#include "u_uac1.c"
89#include "f_uac1.c"
Jack Pham2ec5fdc2012-09-26 10:13:48 -070090#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080091
92MODULE_AUTHOR("Mike Lockwood");
93MODULE_DESCRIPTION("Android Composite USB Driver");
94MODULE_LICENSE("GPL");
95MODULE_VERSION("1.0");
96
97static const char longname[] = "Gadget Android";
98
99/* Default vendor and product IDs, overridden by userspace */
100#define VENDOR_ID 0x18D1
101#define PRODUCT_ID 0x0001
102
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300103#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
104
Benoit Goby1e8ce152011-12-12 13:01:23 -0800105struct android_usb_function {
106 char *name;
107 void *config;
108
109 struct device *dev;
110 char *dev_name;
111 struct device_attribute **attributes;
112
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300113 struct android_dev *android_dev;
114
Benoit Goby1e8ce152011-12-12 13:01:23 -0800115 /* Optional: initialization during gadget bind */
116 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
117 /* Optional: cleanup during gadget unbind */
118 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700119 /* Optional: called when the function is added the list of
120 * enabled functions */
121 void (*enable)(struct android_usb_function *);
122 /* Optional: called when it is removed */
123 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800124
125 int (*bind_config)(struct android_usb_function *,
126 struct usb_configuration *);
127
128 /* Optional: called when the configuration is removed */
129 void (*unbind_config)(struct android_usb_function *,
130 struct usb_configuration *);
131 /* Optional: handle ctrl requests before the device is configured */
132 int (*ctrlrequest)(struct android_usb_function *,
133 struct usb_composite_dev *,
134 const struct usb_ctrlrequest *);
135};
136
Ido Shayevitz68557e32012-11-06 12:40:37 +0200137struct android_usb_function_holder {
138
139 struct android_usb_function *f;
140
141 /* for android_conf.enabled_functions */
142 struct list_head enabled_list;
143};
144
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200145/**
146* struct android_dev - represents android USB gadget device
147* @name: device name.
148* @functions: an array of all the supported USB function
149* drivers that this gadget support but not necessarily
150* added to one of the gadget configurations.
151* @cdev: The internal composite device. Android gadget device
152* is a composite device, such that it can support configurations
153* with more than one function driver.
154* @dev: The kernel device that represents this android device.
155* @enabled: True if the android gadget is enabled, means all
156* the configurations were set and all function drivers were
157* bind and ready for USB enumeration.
158* @disable_depth: Number of times the device was disabled, after
159* symmetrical number of enables the device willl be enabled.
160* Used for controlling ADB userspace disable/enable requests.
161* @mutex: Internal mutex for protecting device member fields.
162* @pdata: Platform data fetched from the kernel device platfrom data.
163* @connected: True if got connect notification from the gadget UDC.
164* False if got disconnect notification from the gadget UDC.
165* @sw_connected: Equal to 'connected' only after the connect
166* notification was handled by the android gadget work function.
167* @suspended: True if got suspend notification from the gadget UDC.
168* False if got resume notification from the gadget UDC.
169* @sw_suspended: Equal to 'suspended' only after the susped
170* notification was handled by the android gadget work function.
171* @pm_qos: An attribute string that can be set by user space in order to
172* determine pm_qos policy. Set to 'high' for always demand pm_qos
173* when USB bus is connected and resumed. Set to 'low' for disable
174* any setting of pm_qos by this driver. Default = 'high'.
175* @work: workqueue used for handling notifications from the gadget UDC.
176* @configs: List of configurations currently configured into the device.
177* The android gadget supports more than one configuration. The host
178* may choose one configuration from the suggested.
179* @configs_num: Number of configurations currently configured and existing
180* in the configs list.
181* @list_item: This driver supports more than one android gadget device (for
182* example in order to support multiple USB cores), therefore this is
183* a item in a linked list of android devices.
184*/
Benoit Goby1e8ce152011-12-12 13:01:23 -0800185struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300186 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800187 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800188 struct usb_composite_dev *cdev;
189 struct device *dev;
190
191 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700192 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800193 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700194 struct android_usb_platform_data *pdata;
195
Benoit Goby1e8ce152011-12-12 13:01:23 -0800196 bool connected;
197 bool sw_connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200198 bool suspended;
199 bool sw_suspended;
Ofir Cohen94213a72012-05-03 14:26:32 +0300200 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700201 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800202 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300203
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300204 /* A list of struct android_configuration */
205 struct list_head configs;
206 int configs_num;
207
208 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300209 struct list_head list_item;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300210};
211
212struct android_configuration {
213 struct usb_configuration usb_config;
214
215 /* A list of the functions supported by this config */
216 struct list_head enabled_functions;
217
218 /* A list node inside the struct android_dev.configs list */
219 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800220};
221
Manu Gautam43c61a12012-08-22 17:09:37 -0700222struct dload_struct __iomem *diag_dload;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800223static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300224static struct list_head android_dev_list;
225static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800226static int android_bind_config(struct usb_configuration *c);
227static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300228static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300229static struct android_configuration *alloc_android_config
230 (struct android_dev *dev);
231static void free_android_config(struct android_dev *dev,
232 struct android_configuration *conf);
Manu Gautam43c61a12012-08-22 17:09:37 -0700233static int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800234
235/* string IDs are assigned dynamically */
236#define STRING_MANUFACTURER_IDX 0
237#define STRING_PRODUCT_IDX 1
238#define STRING_SERIAL_IDX 2
239
240static char manufacturer_string[256];
241static char product_string[256];
242static char serial_string[256];
243
244/* String Table */
245static struct usb_string strings_dev[] = {
246 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
247 [STRING_PRODUCT_IDX].s = product_string,
248 [STRING_SERIAL_IDX].s = serial_string,
249 { } /* end of list */
250};
251
252static struct usb_gadget_strings stringtab_dev = {
253 .language = 0x0409, /* en-us */
254 .strings = strings_dev,
255};
256
257static struct usb_gadget_strings *dev_strings[] = {
258 &stringtab_dev,
259 NULL,
260};
261
262static struct usb_device_descriptor device_desc = {
263 .bLength = sizeof(device_desc),
264 .bDescriptorType = USB_DT_DEVICE,
265 .bcdUSB = __constant_cpu_to_le16(0x0200),
266 .bDeviceClass = USB_CLASS_PER_INTERFACE,
267 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
268 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
269 .bcdDevice = __constant_cpu_to_le16(0xffff),
270 .bNumConfigurations = 1,
271};
272
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530273static struct usb_otg_descriptor otg_descriptor = {
274 .bLength = sizeof otg_descriptor,
275 .bDescriptorType = USB_DT_OTG,
276 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
277 .bcdOTG = __constant_cpu_to_le16(0x0200),
278};
279
280static const struct usb_descriptor_header *otg_desc[] = {
281 (struct usb_descriptor_header *) &otg_descriptor,
282 NULL,
283};
284
Manu Gautama2b54142012-04-03 14:34:32 +0530285enum android_device_state {
286 USB_DISCONNECTED,
287 USB_CONNECTED,
288 USB_CONFIGURED,
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200289 USB_SUSPENDED,
290 USB_RESUMED
Manu Gautama2b54142012-04-03 14:34:32 +0530291};
292
Ofir Cohen94213a72012-05-03 14:26:32 +0300293static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
294{
295 struct android_usb_platform_data *pdata = dev->pdata;
296 u32 swfi_latency = 0;
297 static int last_vote = -1;
298
Ofir Cohen56eb7072012-05-20 11:41:39 +0300299 if (!pdata || vote == last_vote
300 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300301 return;
302
303 swfi_latency = pdata->swfi_latency + 1;
304 if (vote)
305 pm_qos_update_request(&dev->pm_qos_req_dma,
306 swfi_latency);
307 else
308 pm_qos_update_request(&dev->pm_qos_req_dma,
309 PM_QOS_DEFAULT_VALUE);
310 last_vote = vote;
311}
312
Benoit Goby1e8ce152011-12-12 13:01:23 -0800313static void android_work(struct work_struct *data)
314{
315 struct android_dev *dev = container_of(data, struct android_dev, work);
316 struct usb_composite_dev *cdev = dev->cdev;
317 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
318 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
319 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200320 char *suspended[2] = { "USB_STATE=SUSPENDED", NULL };
321 char *resumed[2] = { "USB_STATE=RESUMED", NULL };
Benoit Goby1e8ce152011-12-12 13:01:23 -0800322 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530323 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800324 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300325 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800326
327 spin_lock_irqsave(&cdev->lock, flags);
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200328 if (dev->suspended != dev->sw_suspended && cdev->config) {
329 if (strncmp(dev->pm_qos, "low", 3))
330 pm_qos_vote = dev->suspended ? 0 : 1;
331 next_state = dev->suspended ? USB_SUSPENDED : USB_RESUMED;
332 uevent_envp = dev->suspended ? suspended : resumed;
333 } else if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800334 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530335 next_state = USB_CONFIGURED;
336 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800337 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530338 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300339 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300340 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300341 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300342 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530343 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800344 dev->sw_connected = dev->connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200345 dev->sw_suspended = dev->suspended;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800346 spin_unlock_irqrestore(&cdev->lock, flags);
347
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300348 if (pm_qos_vote != -1)
349 android_pm_qos_update_latency(dev, pm_qos_vote);
350
Benoit Goby1e8ce152011-12-12 13:01:23 -0800351 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530352 /*
353 * Some userspace modules, e.g. MTP, work correctly only if
354 * CONFIGURED uevent is preceded by DISCONNECT uevent.
355 * Check if we missed sending out a DISCONNECT uevent. This can
356 * happen if host PC resets and configures device really quick.
357 */
358 if (((uevent_envp == connected) &&
359 (last_uevent != USB_DISCONNECTED)) ||
360 ((uevent_envp == configured) &&
361 (last_uevent == USB_CONFIGURED))) {
362 pr_info("%s: sent missed DISCONNECT event\n", __func__);
363 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
364 disconnected);
365 msleep(20);
366 }
367 /*
368 * Before sending out CONFIGURED uevent give function drivers
369 * a chance to wakeup userspace threads and notify disconnect
370 */
371 if (uevent_envp == configured)
372 msleep(50);
373
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200374 /* Do not notify on suspend / resume */
375 if (next_state != USB_SUSPENDED && next_state != USB_RESUMED) {
376 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
377 uevent_envp);
378 last_uevent = next_state;
379 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800380 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
381 } else {
382 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
383 dev->connected, dev->sw_connected, cdev->config);
384 }
385}
386
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387static void android_enable(struct android_dev *dev)
388{
389 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300390 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700391
392 if (WARN_ON(!dev->disable_depth))
393 return;
394
395 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300396
397 list_for_each_entry(conf, &dev->configs, list_item)
398 usb_add_config(cdev, &conf->usb_config,
399 android_bind_config);
400
Benoit Goby80ba14d2012-03-19 18:56:52 -0700401 usb_gadget_connect(cdev->gadget);
402 }
403}
404
405static void android_disable(struct android_dev *dev)
406{
407 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300408 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700409
410 if (dev->disable_depth++ == 0) {
411 usb_gadget_disconnect(cdev->gadget);
412 /* Cancel pending control requests */
413 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300414
415 list_for_each_entry(conf, &dev->configs, list_item)
416 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700417 }
418}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800419
420/*-------------------------------------------------------------------------*/
421/* Supported functions initialization */
422
Benoit Goby80ba14d2012-03-19 18:56:52 -0700423struct adb_data {
424 bool opened;
425 bool enabled;
426};
427
Benoit Goby2b6862d2011-12-19 14:38:41 -0800428static int
429adb_function_init(struct android_usb_function *f,
430 struct usb_composite_dev *cdev)
431{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700432 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
433 if (!f->config)
434 return -ENOMEM;
435
Benoit Goby2b6862d2011-12-19 14:38:41 -0800436 return adb_setup();
437}
438
439static void adb_function_cleanup(struct android_usb_function *f)
440{
441 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700442 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800443}
444
445static int
446adb_function_bind_config(struct android_usb_function *f,
447 struct usb_configuration *c)
448{
449 return adb_bind_config(c);
450}
451
Benoit Goby80ba14d2012-03-19 18:56:52 -0700452static void adb_android_function_enable(struct android_usb_function *f)
453{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300454 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700455 struct adb_data *data = f->config;
456
457 data->enabled = true;
458
459 /* Disable the gadget until adbd is ready */
460 if (!data->opened)
461 android_disable(dev);
462}
463
464static void adb_android_function_disable(struct android_usb_function *f)
465{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300466 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700467 struct adb_data *data = f->config;
468
469 data->enabled = false;
470
471 /* Balance the disable that was called in closed_callback */
472 if (!data->opened)
473 android_enable(dev);
474}
475
Benoit Goby2b6862d2011-12-19 14:38:41 -0800476static struct android_usb_function adb_function = {
477 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700478 .enable = adb_android_function_enable,
479 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800480 .init = adb_function_init,
481 .cleanup = adb_function_cleanup,
482 .bind_config = adb_function_bind_config,
483};
484
Benoit Goby80ba14d2012-03-19 18:56:52 -0700485static void adb_ready_callback(void)
486{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300487 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700488 struct adb_data *data = adb_function.config;
489
Benoit Goby80ba14d2012-03-19 18:56:52 -0700490 data->opened = true;
491
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300492 if (data->enabled && dev) {
493 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700494 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300495 mutex_unlock(&dev->mutex);
496 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700497}
498
499static void adb_closed_callback(void)
500{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300501 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700502 struct adb_data *data = adb_function.config;
503
Benoit Goby80ba14d2012-03-19 18:56:52 -0700504 data->opened = false;
505
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300506 if (data->enabled) {
507 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700508 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300509 mutex_unlock(&dev->mutex);
510 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700511}
512
Benoit Goby2b6862d2011-12-19 14:38:41 -0800513
Benoit Gobyaab96812011-04-19 20:37:33 -0700514/*-------------------------------------------------------------------------*/
515/* Supported functions initialization */
516
Manu Gautam8e0719b2011-09-26 14:47:55 +0530517/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530518static int rmnet_smd_function_bind_config(struct android_usb_function *f,
519 struct usb_configuration *c)
520{
521 return rmnet_smd_bind_config(c);
522}
523
524static struct android_usb_function rmnet_smd_function = {
525 .name = "rmnet_smd",
526 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800527};
528
Manu Gautam8e0719b2011-09-26 14:47:55 +0530529/* RMNET_SDIO */
530static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
531 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800532{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530533 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800534}
535
Manu Gautam8e0719b2011-09-26 14:47:55 +0530536static struct android_usb_function rmnet_sdio_function = {
537 .name = "rmnet_sdio",
538 .bind_config = rmnet_sdio_function_bind_config,
539};
540
541/* RMNET_SMD_SDIO */
542static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
543 struct usb_composite_dev *cdev)
544{
545 return rmnet_smd_sdio_init();
546}
547
548static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
549{
550 rmnet_smd_sdio_cleanup();
551}
552
553static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
554 struct usb_configuration *c)
555{
556 return rmnet_smd_sdio_function_add(c);
557}
558
559static struct device_attribute *rmnet_smd_sdio_attributes[] = {
560 &dev_attr_transport, NULL };
561
562static struct android_usb_function rmnet_smd_sdio_function = {
563 .name = "rmnet_smd_sdio",
564 .init = rmnet_smd_sdio_function_init,
565 .cleanup = rmnet_smd_sdio_function_cleanup,
566 .bind_config = rmnet_smd_sdio_bind_config,
567 .attributes = rmnet_smd_sdio_attributes,
568};
569
Hemant Kumar1b820d52011-11-03 15:08:28 -0700570/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
571#define MAX_XPORT_STR_LEN 50
572static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530573
Manu Gautame3e897c2011-09-12 17:18:46 +0530574static void rmnet_function_cleanup(struct android_usb_function *f)
575{
576 frmnet_cleanup();
577}
578
Manu Gautam2b0234a2011-09-07 16:47:52 +0530579static int rmnet_function_bind_config(struct android_usb_function *f,
580 struct usb_configuration *c)
581{
582 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700583 int err = 0;
584 char *ctrl_name;
585 char *data_name;
586 char buf[MAX_XPORT_STR_LEN], *b;
587 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530588
Hemant Kumar1b820d52011-11-03 15:08:28 -0700589 if (!rmnet_initialized) {
590 rmnet_initialized = 1;
591 strlcpy(buf, rmnet_transports, sizeof(buf));
592 b = strim(buf);
593 while (b) {
594 ctrl_name = strsep(&b, ",");
595 data_name = strsep(&b, ",");
596 if (ctrl_name && data_name) {
597 err = frmnet_init_port(ctrl_name, data_name);
598 if (err) {
599 pr_err("rmnet: Cannot open ctrl port:"
600 "'%s' data port:'%s'\n",
601 ctrl_name, data_name);
602 goto out;
603 }
604 ports++;
605 }
606 }
607
608 err = rmnet_gport_setup();
609 if (err) {
610 pr_err("rmnet: Cannot setup transports");
611 goto out;
612 }
613 }
614
615 for (i = 0; i < ports; i++) {
616 err = frmnet_bind_config(c, i);
617 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530618 pr_err("Could not bind rmnet%u config\n", i);
619 break;
620 }
621 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700622out:
623 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530624}
625
Hemant Kumar1b820d52011-11-03 15:08:28 -0700626static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530627 struct device_attribute *attr, char *buf)
628{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700629 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530630}
631
Hemant Kumar1b820d52011-11-03 15:08:28 -0700632static ssize_t rmnet_transports_store(
633 struct device *device, struct device_attribute *attr,
634 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530635{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700636 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530637
Manu Gautam2b0234a2011-09-07 16:47:52 +0530638 return size;
639}
640
Hemant Kumar1b820d52011-11-03 15:08:28 -0700641static struct device_attribute dev_attr_rmnet_transports =
642 __ATTR(transports, S_IRUGO | S_IWUSR,
643 rmnet_transports_show,
644 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530645static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700646 &dev_attr_rmnet_transports,
647 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530648
649static struct android_usb_function rmnet_function = {
650 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530651 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530652 .bind_config = rmnet_function_bind_config,
653 .attributes = rmnet_function_attributes,
654};
655
Ofir Cohen7b155422012-07-31 13:02:49 +0300656struct ecm_function_config {
657 u8 ethaddr[ETH_ALEN];
658};
659
660static int ecm_function_init(struct android_usb_function *f,
661 struct usb_composite_dev *cdev)
662{
663 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
664 if (!f->config)
665 return -ENOMEM;
666 return 0;
667}
668
669static void ecm_function_cleanup(struct android_usb_function *f)
670{
671 kfree(f->config);
672 f->config = NULL;
673}
674
675static int ecm_qc_function_bind_config(struct android_usb_function *f,
676 struct usb_configuration *c)
677{
678 int ret;
679 struct ecm_function_config *ecm = f->config;
680
681 if (!ecm) {
682 pr_err("%s: ecm_pdata\n", __func__);
683 return -EINVAL;
684 }
685
686 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
687 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
688 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
689
690 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
691 if (ret) {
692 pr_err("%s: gether_setup failed\n", __func__);
693 return ret;
694 }
695
696 return ecm_qc_bind_config(c, ecm->ethaddr);
697}
698
699static void ecm_qc_function_unbind_config(struct android_usb_function *f,
700 struct usb_configuration *c)
701{
Amit Blayd6d690a2012-10-16 13:37:42 +0200702 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300703}
704
705static ssize_t ecm_ethaddr_show(struct device *dev,
706 struct device_attribute *attr, char *buf)
707{
708 struct android_usb_function *f = dev_get_drvdata(dev);
709 struct ecm_function_config *ecm = f->config;
710 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
711 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
712 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
713}
714
715static ssize_t ecm_ethaddr_store(struct device *dev,
716 struct device_attribute *attr, const char *buf, size_t size)
717{
718 struct android_usb_function *f = dev_get_drvdata(dev);
719 struct ecm_function_config *ecm = f->config;
720
721 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
722 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
723 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
724 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
725 return size;
726 return -EINVAL;
727}
728
729static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
730 ecm_ethaddr_store);
731
732static struct device_attribute *ecm_function_attributes[] = {
733 &dev_attr_ecm_ethaddr,
734 NULL
735};
736
737static struct android_usb_function ecm_qc_function = {
738 .name = "ecm_qc",
739 .init = ecm_function_init,
740 .cleanup = ecm_function_cleanup,
741 .bind_config = ecm_qc_function_bind_config,
742 .unbind_config = ecm_qc_function_unbind_config,
743 .attributes = ecm_function_attributes,
744};
Anna Perela8c991d2012-04-09 16:44:46 +0300745
746/* MBIM - used with BAM */
747#define MAX_MBIM_INSTANCES 1
748
749static int mbim_function_init(struct android_usb_function *f,
750 struct usb_composite_dev *cdev)
751{
752 return mbim_init(MAX_MBIM_INSTANCES);
753}
754
755static void mbim_function_cleanup(struct android_usb_function *f)
756{
757 fmbim_cleanup();
758}
759
760static int mbim_function_bind_config(struct android_usb_function *f,
761 struct usb_configuration *c)
762{
763 return mbim_bind_config(c, 0);
764}
765
Jack Pham2df2f702012-10-11 19:08:24 -0700766static int mbim_function_ctrlrequest(struct android_usb_function *f,
767 struct usb_composite_dev *cdev,
768 const struct usb_ctrlrequest *c)
769{
770 return mbim_ctrlrequest(cdev, c);
771}
772
Anna Perela8c991d2012-04-09 16:44:46 +0300773static struct android_usb_function mbim_function = {
774 .name = "usb_mbim",
775 .cleanup = mbim_function_cleanup,
776 .bind_config = mbim_function_bind_config,
777 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -0700778 .ctrlrequest = mbim_function_ctrlrequest,
Anna Perela8c991d2012-04-09 16:44:46 +0300779};
780
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700781#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +0300782/* PERIPHERAL AUDIO */
783static int audio_function_bind_config(struct android_usb_function *f,
784 struct usb_configuration *c)
785{
786 return audio_bind_config(c);
787}
788
789static struct android_usb_function audio_function = {
790 .name = "audio",
791 .bind_config = audio_function_bind_config,
792};
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700793#endif
Anna Perel432367a2012-09-20 10:55:32 +0300794
Anna Perela8c991d2012-04-09 16:44:46 +0300795
Manu Gautam8e0719b2011-09-26 14:47:55 +0530796/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530797static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798static ssize_t clients_store(
799 struct device *device, struct device_attribute *attr,
800 const char *buff, size_t size)
801{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530802 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
804 return size;
805}
806
807static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
808static struct device_attribute *diag_function_attributes[] =
809 { &dev_attr_clients, NULL };
810
811static int diag_function_init(struct android_usb_function *f,
812 struct usb_composite_dev *cdev)
813{
814 return diag_setup();
815}
816
817static void diag_function_cleanup(struct android_usb_function *f)
818{
819 diag_cleanup();
820}
821
822static int diag_function_bind_config(struct android_usb_function *f,
823 struct usb_configuration *c)
824{
825 char *name;
826 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530827 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800828 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300829 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530831 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 b = strim(buf);
833
834 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800835 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530837 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -0700838 if (!once++) {
839 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
840 notify = dev->pdata->update_pid_and_serial_num;
841 else
842 notify = usb_diag_update_pid_and_serial_num;
843 }
Manu Gautamc5760302011-08-25 14:30:24 +0530844
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530846 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 if (err)
848 pr_err("diag: Cannot open channel '%s'", name);
849 }
850 }
851
852 return err;
853}
854
855static struct android_usb_function diag_function = {
856 .name = "diag",
857 .init = diag_function_init,
858 .cleanup = diag_function_cleanup,
859 .bind_config = diag_function_bind_config,
860 .attributes = diag_function_attributes,
861};
862
Shimrit Malichia00d7322012-08-05 13:56:28 +0300863/* DEBUG */
864static int qdss_function_init(struct android_usb_function *f,
865 struct usb_composite_dev *cdev)
866{
867 return qdss_setup();
868}
869
870static void qdss_function_cleanup(struct android_usb_function *f)
871{
872 qdss_cleanup();
873}
874
875static int qdss_function_bind_config(struct android_usb_function *f,
876 struct usb_configuration *c)
877{
878 int err = -1;
879
880 err = qdss_bind_config(c, "qdss");
881 if (err)
882 pr_err("qdss: Cannot open channel qdss");
883
884 return err;
885}
886
887static struct android_usb_function qdss_function = {
888 .name = "qdss",
889 .init = qdss_function_init,
890 .cleanup = qdss_function_cleanup,
891 .bind_config = qdss_function_bind_config,
892};
893
Manu Gautam8e0719b2011-09-26 14:47:55 +0530894/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530895static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530896static ssize_t serial_transports_store(
897 struct device *device, struct device_attribute *attr,
898 const char *buff, size_t size)
899{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530900 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530901
902 return size;
903}
904
905static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
906static struct device_attribute *serial_function_attributes[] =
907 { &dev_attr_transports, NULL };
908
909static void serial_function_cleanup(struct android_usb_function *f)
910{
911 gserial_cleanup();
912}
913
914static int serial_function_bind_config(struct android_usb_function *f,
915 struct usb_configuration *c)
916{
917 char *name;
918 char buf[32], *b;
919 int err = -1, i;
920 static int serial_initialized = 0, ports = 0;
921
922 if (serial_initialized)
923 goto bind_config;
924
925 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530926 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530927 b = strim(buf);
928
929 while (b) {
930 name = strsep(&b, ",");
931
932 if (name) {
933 err = gserial_init_port(ports, name);
934 if (err) {
935 pr_err("serial: Cannot open port '%s'", name);
936 goto out;
937 }
938 ports++;
939 }
940 }
941 err = gport_setup(c);
942 if (err) {
943 pr_err("serial: Cannot setup transports");
944 goto out;
945 }
946
947bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200948 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530949 err = gser_bind_config(c, i);
950 if (err) {
951 pr_err("serial: bind_config failed for port %d", i);
952 goto out;
953 }
954 }
955
956out:
957 return err;
958}
959
960static struct android_usb_function serial_function = {
961 .name = "serial",
962 .cleanup = serial_function_cleanup,
963 .bind_config = serial_function_bind_config,
964 .attributes = serial_function_attributes,
965};
966
Anji jonnala92be1b42011-12-19 09:44:41 +0530967/* ACM */
968static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
969static ssize_t acm_transports_store(
970 struct device *device, struct device_attribute *attr,
971 const char *buff, size_t size)
972{
973 strlcpy(acm_transports, buff, sizeof(acm_transports));
974
975 return size;
976}
977
978static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
979static struct device_attribute *acm_function_attributes[] = {
980 &dev_attr_acm_transports, NULL };
981
Benoit Goby1e8ce152011-12-12 13:01:23 -0800982static void acm_function_cleanup(struct android_usb_function *f)
983{
984 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800985}
986
Anji jonnala92be1b42011-12-19 09:44:41 +0530987static int acm_function_bind_config(struct android_usb_function *f,
988 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800989{
Anji jonnala92be1b42011-12-19 09:44:41 +0530990 char *name;
991 char buf[32], *b;
992 int err = -1, i;
993 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800994
Anji jonnala92be1b42011-12-19 09:44:41 +0530995 if (acm_initialized)
996 goto bind_config;
997
998 acm_initialized = 1;
999 strlcpy(buf, acm_transports, sizeof(buf));
1000 b = strim(buf);
1001
1002 while (b) {
1003 name = strsep(&b, ",");
1004
1005 if (name) {
1006 err = acm_init_port(ports, name);
1007 if (err) {
1008 pr_err("acm: Cannot open port '%s'", name);
1009 goto out;
1010 }
1011 ports++;
1012 }
1013 }
1014 err = acm_port_setup(c);
1015 if (err) {
1016 pr_err("acm: Cannot setup transports");
1017 goto out;
1018 }
1019
1020bind_config:
1021 for (i = 0; i < ports; i++) {
1022 err = acm_bind_config(c, i);
1023 if (err) {
1024 pr_err("acm: bind_config failed for port %d", i);
1025 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001026 }
1027 }
1028
Anji jonnala92be1b42011-12-19 09:44:41 +05301029out:
1030 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001031}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001032static struct android_usb_function acm_function = {
1033 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001034 .cleanup = acm_function_cleanup,
1035 .bind_config = acm_function_bind_config,
1036 .attributes = acm_function_attributes,
1037};
1038
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301039/* CCID */
1040static int ccid_function_init(struct android_usb_function *f,
1041 struct usb_composite_dev *cdev)
1042{
1043 return ccid_setup();
1044}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001045
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301046static void ccid_function_cleanup(struct android_usb_function *f)
1047{
1048 ccid_cleanup();
1049}
1050
1051static int ccid_function_bind_config(struct android_usb_function *f,
1052 struct usb_configuration *c)
1053{
1054 return ccid_bind_config(c);
1055}
1056
1057static struct android_usb_function ccid_function = {
1058 .name = "ccid",
1059 .init = ccid_function_init,
1060 .cleanup = ccid_function_cleanup,
1061 .bind_config = ccid_function_bind_config,
1062};
1063
Steve Mucklef132c6c2012-06-06 18:30:57 -07001064static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001065 struct usb_composite_dev *cdev)
1066{
1067 return mtp_setup();
1068}
1069
1070static void mtp_function_cleanup(struct android_usb_function *f)
1071{
1072 mtp_cleanup();
1073}
1074
Steve Mucklef132c6c2012-06-06 18:30:57 -07001075static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001076 struct usb_configuration *c)
1077{
1078 return mtp_bind_config(c, false);
1079}
1080
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001081static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001082{
1083 /* nothing to do - initialization is handled by mtp_function_init */
1084 return 0;
1085}
1086
1087static void ptp_function_cleanup(struct android_usb_function *f)
1088{
1089 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1090}
1091
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001092static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001093{
1094 return mtp_bind_config(c, true);
1095}
1096
1097static int mtp_function_ctrlrequest(struct android_usb_function *f,
1098 struct usb_composite_dev *cdev,
1099 const struct usb_ctrlrequest *c)
1100{
1101 return mtp_ctrlrequest(cdev, c);
1102}
1103
1104static struct android_usb_function mtp_function = {
1105 .name = "mtp",
1106 .init = mtp_function_init,
1107 .cleanup = mtp_function_cleanup,
1108 .bind_config = mtp_function_bind_config,
1109 .ctrlrequest = mtp_function_ctrlrequest,
1110};
1111
1112/* PTP function is same as MTP with slightly different interface descriptor */
1113static struct android_usb_function ptp_function = {
1114 .name = "ptp",
1115 .init = ptp_function_init,
1116 .cleanup = ptp_function_cleanup,
1117 .bind_config = ptp_function_bind_config,
1118};
1119
1120
Benoit Goby1e8ce152011-12-12 13:01:23 -08001121struct rndis_function_config {
1122 u8 ethaddr[ETH_ALEN];
1123 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001124 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001125 char manufacturer[256];
1126 /* "Wireless" RNDIS; auto-detected by Windows */
1127 bool wceis;
1128};
1129
1130static int
1131rndis_function_init(struct android_usb_function *f,
1132 struct usb_composite_dev *cdev)
1133{
1134 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1135 if (!f->config)
1136 return -ENOMEM;
1137 return 0;
1138}
1139
1140static void rndis_function_cleanup(struct android_usb_function *f)
1141{
1142 kfree(f->config);
1143 f->config = NULL;
1144}
1145
Ofir Cohenaef90b72012-07-31 12:37:04 +02001146static int rndis_qc_function_init(struct android_usb_function *f,
1147 struct usb_composite_dev *cdev)
1148{
1149 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1150 if (!f->config)
1151 return -ENOMEM;
1152
1153 return rndis_qc_init();
1154}
1155
1156static void rndis_qc_function_cleanup(struct android_usb_function *f)
1157{
1158 rndis_qc_cleanup();
1159 kfree(f->config);
1160}
1161
Benoit Goby1e8ce152011-12-12 13:01:23 -08001162static int
1163rndis_function_bind_config(struct android_usb_function *f,
1164 struct usb_configuration *c)
1165{
1166 int ret;
1167 struct rndis_function_config *rndis = f->config;
1168
1169 if (!rndis) {
1170 pr_err("%s: rndis_pdata\n", __func__);
1171 return -1;
1172 }
1173
1174 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1175 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1176 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1177
1178 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1179 if (ret) {
1180 pr_err("%s: gether_setup failed\n", __func__);
1181 return ret;
1182 }
1183
1184 if (rndis->wceis) {
1185 /* "Wireless" RNDIS; auto-detected by Windows */
1186 rndis_iad_descriptor.bFunctionClass =
1187 USB_CLASS_WIRELESS_CONTROLLER;
1188 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1189 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1190 rndis_control_intf.bInterfaceClass =
1191 USB_CLASS_WIRELESS_CONTROLLER;
1192 rndis_control_intf.bInterfaceSubClass = 0x01;
1193 rndis_control_intf.bInterfaceProtocol = 0x03;
1194 }
1195
1196 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1197 rndis->manufacturer);
1198}
1199
Ofir Cohenaef90b72012-07-31 12:37:04 +02001200static int rndis_qc_function_bind_config(struct android_usb_function *f,
1201 struct usb_configuration *c)
1202{
1203 int ret;
1204 struct rndis_function_config *rndis = f->config;
1205
1206 if (!rndis) {
1207 pr_err("%s: rndis_pdata\n", __func__);
1208 return -EINVAL;
1209 }
1210
1211 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1212 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1213 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1214
1215 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1216 if (ret) {
1217 pr_err("%s: gether_setup failed\n", __func__);
1218 return ret;
1219 }
1220
1221 if (rndis->wceis) {
1222 /* "Wireless" RNDIS; auto-detected by Windows */
1223 rndis_qc_iad_descriptor.bFunctionClass =
1224 USB_CLASS_WIRELESS_CONTROLLER;
1225 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1226 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1227 rndis_qc_control_intf.bInterfaceClass =
1228 USB_CLASS_WIRELESS_CONTROLLER;
1229 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1230 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1231 }
1232
1233 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1234 rndis->manufacturer,
1235 rndis->max_pkt_per_xfer);
1236}
1237
Benoit Goby1e8ce152011-12-12 13:01:23 -08001238static void rndis_function_unbind_config(struct android_usb_function *f,
1239 struct usb_configuration *c)
1240{
1241 gether_cleanup();
1242}
1243
Ofir Cohenaef90b72012-07-31 12:37:04 +02001244static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1245 struct usb_configuration *c)
1246{
Amit Blayd6d690a2012-10-16 13:37:42 +02001247 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001248}
1249
Benoit Goby1e8ce152011-12-12 13:01:23 -08001250static ssize_t rndis_manufacturer_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1252{
1253 struct android_usb_function *f = dev_get_drvdata(dev);
1254 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001255
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301256 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001257}
1258
1259static ssize_t rndis_manufacturer_store(struct device *dev,
1260 struct device_attribute *attr, const char *buf, size_t size)
1261{
1262 struct android_usb_function *f = dev_get_drvdata(dev);
1263 struct rndis_function_config *config = f->config;
1264
1265 if (size >= sizeof(config->manufacturer))
1266 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001267
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301268 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001269 return size;
1270 return -1;
1271}
1272
1273static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1274 rndis_manufacturer_store);
1275
1276static ssize_t rndis_wceis_show(struct device *dev,
1277 struct device_attribute *attr, char *buf)
1278{
1279 struct android_usb_function *f = dev_get_drvdata(dev);
1280 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001281
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301282 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001283}
1284
1285static ssize_t rndis_wceis_store(struct device *dev,
1286 struct device_attribute *attr, const char *buf, size_t size)
1287{
1288 struct android_usb_function *f = dev_get_drvdata(dev);
1289 struct rndis_function_config *config = f->config;
1290 int value;
1291
1292 if (sscanf(buf, "%d", &value) == 1) {
1293 config->wceis = value;
1294 return size;
1295 }
1296 return -EINVAL;
1297}
1298
1299static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1300 rndis_wceis_store);
1301
1302static ssize_t rndis_ethaddr_show(struct device *dev,
1303 struct device_attribute *attr, char *buf)
1304{
1305 struct android_usb_function *f = dev_get_drvdata(dev);
1306 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001307
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301308 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001309 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1310 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1311}
1312
1313static ssize_t rndis_ethaddr_store(struct device *dev,
1314 struct device_attribute *attr, const char *buf, size_t size)
1315{
1316 struct android_usb_function *f = dev_get_drvdata(dev);
1317 struct rndis_function_config *rndis = f->config;
1318
1319 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1320 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1321 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1322 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1323 return size;
1324 return -EINVAL;
1325}
1326
1327static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1328 rndis_ethaddr_store);
1329
1330static ssize_t rndis_vendorID_show(struct device *dev,
1331 struct device_attribute *attr, char *buf)
1332{
1333 struct android_usb_function *f = dev_get_drvdata(dev);
1334 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001335
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301336 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001337}
1338
1339static ssize_t rndis_vendorID_store(struct device *dev,
1340 struct device_attribute *attr, const char *buf, size_t size)
1341{
1342 struct android_usb_function *f = dev_get_drvdata(dev);
1343 struct rndis_function_config *config = f->config;
1344 int value;
1345
1346 if (sscanf(buf, "%04x", &value) == 1) {
1347 config->vendorID = value;
1348 return size;
1349 }
1350 return -EINVAL;
1351}
1352
1353static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1354 rndis_vendorID_store);
1355
Ofir Cohenaef90b72012-07-31 12:37:04 +02001356static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1357 struct device_attribute *attr, char *buf)
1358{
1359 struct android_usb_function *f = dev_get_drvdata(dev);
1360 struct rndis_function_config *config = f->config;
1361 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1362}
1363
1364static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1365 struct device_attribute *attr, const char *buf, size_t size)
1366{
1367 struct android_usb_function *f = dev_get_drvdata(dev);
1368 struct rndis_function_config *config = f->config;
1369 int value;
1370
1371 if (sscanf(buf, "%d", &value) == 1) {
1372 config->max_pkt_per_xfer = value;
1373 return size;
1374 }
1375 return -EINVAL;
1376}
1377
1378static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1379 rndis_max_pkt_per_xfer_show,
1380 rndis_max_pkt_per_xfer_store);
1381
Benoit Goby1e8ce152011-12-12 13:01:23 -08001382static struct device_attribute *rndis_function_attributes[] = {
1383 &dev_attr_manufacturer,
1384 &dev_attr_wceis,
1385 &dev_attr_ethaddr,
1386 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001387 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001388 NULL
1389};
1390
1391static struct android_usb_function rndis_function = {
1392 .name = "rndis",
1393 .init = rndis_function_init,
1394 .cleanup = rndis_function_cleanup,
1395 .bind_config = rndis_function_bind_config,
1396 .unbind_config = rndis_function_unbind_config,
1397 .attributes = rndis_function_attributes,
1398};
1399
Ofir Cohenaef90b72012-07-31 12:37:04 +02001400static struct android_usb_function rndis_qc_function = {
1401 .name = "rndis_qc",
1402 .init = rndis_qc_function_init,
1403 .cleanup = rndis_qc_function_cleanup,
1404 .bind_config = rndis_qc_function_bind_config,
1405 .unbind_config = rndis_qc_function_unbind_config,
1406 .attributes = rndis_function_attributes,
1407};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001408
Anna Perelf9d01552012-11-20 15:56:32 +02001409static int ecm_function_bind_config(struct android_usb_function *f,
1410 struct usb_configuration *c)
1411{
1412 int ret;
1413 struct ecm_function_config *ecm = f->config;
1414
1415 if (!ecm) {
1416 pr_err("%s: ecm_pdata\n", __func__);
1417 return -EINVAL;
1418 }
1419
1420 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1421 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1422 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1423
1424 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1425 if (ret) {
1426 pr_err("%s: gether_setup failed\n", __func__);
1427 return ret;
1428 }
1429
1430 ret = ecm_bind_config(c, ecm->ethaddr);
1431 if (ret) {
1432 pr_err("%s: ecm_bind_config failed\n", __func__);
1433 gether_cleanup();
1434 }
1435 return ret;
1436}
1437
1438static void ecm_function_unbind_config(struct android_usb_function *f,
1439 struct usb_configuration *c)
1440{
1441 gether_cleanup();
1442}
1443
1444static struct android_usb_function ecm_function = {
1445 .name = "ecm",
1446 .init = ecm_function_init,
1447 .cleanup = ecm_function_cleanup,
1448 .bind_config = ecm_function_bind_config,
1449 .unbind_config = ecm_function_unbind_config,
1450 .attributes = ecm_function_attributes,
1451};
1452
Benoit Goby1e8ce152011-12-12 13:01:23 -08001453struct mass_storage_function_config {
1454 struct fsg_config fsg;
1455 struct fsg_common *common;
1456};
1457
1458static int mass_storage_function_init(struct android_usb_function *f,
1459 struct usb_composite_dev *cdev)
1460{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001461 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001462 struct mass_storage_function_config *config;
1463 struct fsg_common *common;
1464 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301465 int i;
1466 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001467
1468 config = kzalloc(sizeof(struct mass_storage_function_config),
1469 GFP_KERNEL);
1470 if (!config)
1471 return -ENOMEM;
1472
1473 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301474 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301475 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301476 config->fsg.nluns = 2;
1477 config->fsg.luns[1].cdrom = 1;
1478 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301479 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301480 name[1] = "lun0";
1481 }
1482
Benoit Goby1e8ce152011-12-12 13:01:23 -08001483 config->fsg.luns[0].removable = 1;
1484
1485 common = fsg_common_init(NULL, cdev, &config->fsg);
1486 if (IS_ERR(common)) {
1487 kfree(config);
1488 return PTR_ERR(common);
1489 }
1490
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301491 for (i = 0; i < config->fsg.nluns; i++) {
1492 err = sysfs_create_link(&f->dev->kobj,
1493 &common->luns[i].dev.kobj,
1494 name[i]);
1495 if (err)
1496 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001497 }
1498
1499 config->common = common;
1500 f->config = config;
1501 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301502error:
1503 for (; i > 0 ; i--)
1504 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1505
1506 fsg_common_release(&common->ref);
1507 kfree(config);
1508 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001509}
1510
1511static void mass_storage_function_cleanup(struct android_usb_function *f)
1512{
1513 kfree(f->config);
1514 f->config = NULL;
1515}
1516
1517static int mass_storage_function_bind_config(struct android_usb_function *f,
1518 struct usb_configuration *c)
1519{
1520 struct mass_storage_function_config *config = f->config;
1521 return fsg_bind_config(c->cdev, c, config->common);
1522}
1523
1524static ssize_t mass_storage_inquiry_show(struct device *dev,
1525 struct device_attribute *attr, char *buf)
1526{
1527 struct android_usb_function *f = dev_get_drvdata(dev);
1528 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301529 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001530}
1531
1532static ssize_t mass_storage_inquiry_store(struct device *dev,
1533 struct device_attribute *attr, const char *buf, size_t size)
1534{
1535 struct android_usb_function *f = dev_get_drvdata(dev);
1536 struct mass_storage_function_config *config = f->config;
1537 if (size >= sizeof(config->common->inquiry_string))
1538 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301539 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001540 return -EINVAL;
1541 return size;
1542}
1543
1544static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1545 mass_storage_inquiry_show,
1546 mass_storage_inquiry_store);
1547
1548static struct device_attribute *mass_storage_function_attributes[] = {
1549 &dev_attr_inquiry_string,
1550 NULL
1551};
1552
1553static struct android_usb_function mass_storage_function = {
1554 .name = "mass_storage",
1555 .init = mass_storage_function_init,
1556 .cleanup = mass_storage_function_cleanup,
1557 .bind_config = mass_storage_function_bind_config,
1558 .attributes = mass_storage_function_attributes,
1559};
1560
1561
Benoit Gobycf3fc062011-12-19 14:39:37 -08001562static int accessory_function_init(struct android_usb_function *f,
1563 struct usb_composite_dev *cdev)
1564{
1565 return acc_setup();
1566}
1567
1568static void accessory_function_cleanup(struct android_usb_function *f)
1569{
1570 acc_cleanup();
1571}
1572
1573static int accessory_function_bind_config(struct android_usb_function *f,
1574 struct usb_configuration *c)
1575{
1576 return acc_bind_config(c);
1577}
1578
1579static int accessory_function_ctrlrequest(struct android_usb_function *f,
1580 struct usb_composite_dev *cdev,
1581 const struct usb_ctrlrequest *c)
1582{
1583 return acc_ctrlrequest(cdev, c);
1584}
1585
1586static struct android_usb_function accessory_function = {
1587 .name = "accessory",
1588 .init = accessory_function_init,
1589 .cleanup = accessory_function_cleanup,
1590 .bind_config = accessory_function_bind_config,
1591 .ctrlrequest = accessory_function_ctrlrequest,
1592};
1593
Mike Lockwood11874822012-08-27 16:43:53 +05301594static int audio_source_function_init(struct android_usb_function *f,
1595 struct usb_composite_dev *cdev)
1596{
1597 struct audio_source_config *config;
1598
1599 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1600 if (!config)
1601 return -ENOMEM;
1602 config->card = -1;
1603 config->device = -1;
1604 f->config = config;
1605 return 0;
1606}
1607
1608static void audio_source_function_cleanup(struct android_usb_function *f)
1609{
1610 kfree(f->config);
1611}
1612
1613static int audio_source_function_bind_config(struct android_usb_function *f,
1614 struct usb_configuration *c)
1615{
1616 struct audio_source_config *config = f->config;
1617
1618 return audio_source_bind_config(c, config);
1619}
1620
1621static void audio_source_function_unbind_config(struct android_usb_function *f,
1622 struct usb_configuration *c)
1623{
1624 struct audio_source_config *config = f->config;
1625
1626 config->card = -1;
1627 config->device = -1;
1628}
1629
1630static ssize_t audio_source_pcm_show(struct device *dev,
1631 struct device_attribute *attr, char *buf)
1632{
1633 struct android_usb_function *f = dev_get_drvdata(dev);
1634 struct audio_source_config *config = f->config;
1635
1636 /* print PCM card and device numbers */
1637 return sprintf(buf, "%d %d\n", config->card, config->device);
1638}
1639
1640static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1641
1642static struct device_attribute *audio_source_function_attributes[] = {
1643 &dev_attr_pcm,
1644 NULL
1645};
1646
1647static struct android_usb_function audio_source_function = {
1648 .name = "audio_source",
1649 .init = audio_source_function_init,
1650 .cleanup = audio_source_function_cleanup,
1651 .bind_config = audio_source_function_bind_config,
1652 .unbind_config = audio_source_function_unbind_config,
1653 .attributes = audio_source_function_attributes,
1654};
1655
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301656static int android_uasp_connect_cb(bool connect)
1657{
1658 /*
1659 * TODO
1660 * We may have to disable gadget till UASP configfs nodes
1661 * are configured which includes mapping LUN with the
1662 * backing file. It is a fundamental difference between
1663 * f_mass_storage and f_tcp. That means UASP can not be
1664 * in default composition.
1665 *
1666 * For now, assume that UASP configfs nodes are configured
1667 * before enabling android gadget. Or cable should be
1668 * reconnected after mapping the LUN.
1669 *
1670 * Also consider making UASP to respond to Host requests when
1671 * Lun is not mapped.
1672 */
1673 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1674
1675 return 0;
1676}
1677
1678static int uasp_function_init(struct android_usb_function *f,
1679 struct usb_composite_dev *cdev)
1680{
1681 return f_tcm_init(&android_uasp_connect_cb);
1682}
1683
1684static void uasp_function_cleanup(struct android_usb_function *f)
1685{
1686 f_tcm_exit();
1687}
1688
1689static int uasp_function_bind_config(struct android_usb_function *f,
1690 struct usb_configuration *c)
1691{
1692 return tcm_bind_config(c);
1693}
1694
1695static struct android_usb_function uasp_function = {
1696 .name = "uasp",
1697 .init = uasp_function_init,
1698 .cleanup = uasp_function_cleanup,
1699 .bind_config = uasp_function_bind_config,
1700};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001701
Benoit Goby1e8ce152011-12-12 13:01:23 -08001702static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001703 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001704 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001705#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001706 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001707#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301708 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301709 &rmnet_sdio_function,
1710 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301711 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001713 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301714 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001715 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301716 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001717 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001718 &mtp_function,
1719 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001720 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001721 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02001722 &ecm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001723 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001724 &accessory_function,
Mike Lockwood11874822012-08-27 16:43:53 +05301725 &audio_source_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301726 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001727 NULL
1728};
1729
Lena Salmand092f2d2012-03-12 17:27:24 +02001730static void android_cleanup_functions(struct android_usb_function **functions)
1731{
1732 struct android_usb_function *f;
1733 struct device_attribute **attrs;
1734 struct device_attribute *attr;
1735
1736 while (*functions) {
1737 f = *functions++;
1738
1739 if (f->dev) {
1740 device_destroy(android_class, f->dev->devt);
1741 kfree(f->dev_name);
1742 } else
1743 continue;
1744
1745 if (f->cleanup)
1746 f->cleanup(f);
1747
1748 attrs = f->attributes;
1749 if (attrs) {
1750 while ((attr = *attrs++))
1751 device_remove_file(f->dev, attr);
1752 }
1753 }
1754}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001755
1756static int android_init_functions(struct android_usb_function **functions,
1757 struct usb_composite_dev *cdev)
1758{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001759 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001760 struct android_usb_function *f;
1761 struct device_attribute **attrs;
1762 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301763 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001764 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001765
1766 for (; (f = *functions++); index++) {
1767 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001768 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001769 if (!f->dev_name) {
1770 err = -ENOMEM;
1771 goto err_out;
1772 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001773 f->dev = device_create(android_class, dev->dev,
1774 MKDEV(0, index), f, f->dev_name);
1775 if (IS_ERR(f->dev)) {
1776 pr_err("%s: Failed to create dev %s", __func__,
1777 f->dev_name);
1778 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001779 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001780 goto err_create;
1781 }
1782
1783 if (f->init) {
1784 err = f->init(f, cdev);
1785 if (err) {
1786 pr_err("%s: Failed to init %s", __func__,
1787 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001788 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001789 }
1790 }
1791
1792 attrs = f->attributes;
1793 if (attrs) {
1794 while ((attr = *attrs++) && !err)
1795 err = device_create_file(f->dev, attr);
1796 }
1797 if (err) {
1798 pr_err("%s: Failed to create function %s attributes",
1799 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001800 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001801 }
1802 }
1803 return 0;
1804
Lena Salmand092f2d2012-03-12 17:27:24 +02001805err_attrs:
1806 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1807 device_remove_file(f->dev, attr);
1808 if (f->cleanup)
1809 f->cleanup(f);
1810err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001811 device_destroy(android_class, f->dev->devt);
1812err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001813 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001814 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001815err_out:
1816 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001817 return err;
1818}
1819
Benoit Goby1e8ce152011-12-12 13:01:23 -08001820static int
1821android_bind_enabled_functions(struct android_dev *dev,
1822 struct usb_configuration *c)
1823{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001824 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001825 struct android_configuration *conf =
1826 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001827 int ret;
1828
Ido Shayevitz68557e32012-11-06 12:40:37 +02001829 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1830 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001831 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001832 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001833 return ret;
1834 }
1835 }
1836 return 0;
1837}
1838
1839static void
1840android_unbind_enabled_functions(struct android_dev *dev,
1841 struct usb_configuration *c)
1842{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001843 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001844 struct android_configuration *conf =
1845 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001846
Ido Shayevitz68557e32012-11-06 12:40:37 +02001847 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1848 if (f_holder->f->unbind_config)
1849 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001850 }
1851}
1852
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001853static int android_enable_function(struct android_dev *dev,
1854 struct android_configuration *conf,
1855 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001856{
1857 struct android_usb_function **functions = dev->functions;
1858 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001859 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001860 while ((f = *functions++)) {
1861 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001862 if (f->android_dev && f->android_dev != dev)
1863 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001864 f->name);
1865 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001866 f_holder = kzalloc(sizeof(*f_holder),
1867 GFP_KERNEL);
1868 if (!f_holder) {
1869 pr_err("Failed to alloc f_holder\n");
1870 return -ENOMEM;
1871 }
1872
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001873 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001874 f_holder->f = f;
1875 list_add_tail(&f_holder->enabled_list,
1876 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001877 return 0;
1878 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001879 }
1880 }
1881 return -EINVAL;
1882}
1883
1884/*-------------------------------------------------------------------------*/
1885/* /sys/class/android_usb/android%d/ interface */
1886
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301887static ssize_t remote_wakeup_show(struct device *pdev,
1888 struct device_attribute *attr, char *buf)
1889{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001890 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001891 struct android_configuration *conf;
1892
1893 /*
1894 * Show the wakeup attribute of the first configuration,
1895 * since all configurations have the same wakeup attribute
1896 */
1897 if (dev->configs_num == 0)
1898 return 0;
1899 conf = list_entry(dev->configs.next,
1900 struct android_configuration,
1901 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001902
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301903 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001904 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301905 USB_CONFIG_ATT_WAKEUP));
1906}
1907
1908static ssize_t remote_wakeup_store(struct device *pdev,
1909 struct device_attribute *attr, const char *buff, size_t size)
1910{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001911 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001912 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301913 int enable = 0;
1914
1915 sscanf(buff, "%d", &enable);
1916
1917 pr_debug("android_usb: %s remote wakeup\n",
1918 enable ? "enabling" : "disabling");
1919
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001920 list_for_each_entry(conf, &dev->configs, list_item)
1921 if (enable)
1922 conf->usb_config.bmAttributes |=
1923 USB_CONFIG_ATT_WAKEUP;
1924 else
1925 conf->usb_config.bmAttributes &=
1926 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301927
1928 return size;
1929}
1930
Benoit Goby1e8ce152011-12-12 13:01:23 -08001931static ssize_t
1932functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1933{
1934 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001935 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001936 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001937 char *buff = buf;
1938
1939 mutex_lock(&dev->mutex);
1940
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001941 list_for_each_entry(conf, &dev->configs, list_item) {
1942 if (buff != buf)
1943 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02001944 list_for_each_entry(f_holder, &conf->enabled_functions,
1945 enabled_list)
1946 buff += snprintf(buff, PAGE_SIZE, "%s,",
1947 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001948 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001949
1950 mutex_unlock(&dev->mutex);
1951
1952 if (buff != buf)
1953 *(buff-1) = '\n';
1954 return buff - buf;
1955}
1956
1957static ssize_t
1958functions_store(struct device *pdev, struct device_attribute *attr,
1959 const char *buff, size_t size)
1960{
1961 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001962 struct list_head *curr_conf = &dev->configs;
1963 struct android_configuration *conf;
1964 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001965 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001966 char *name;
1967 char buf[256], *b;
1968 int err;
1969
1970 mutex_lock(&dev->mutex);
1971
1972 if (dev->enabled) {
1973 mutex_unlock(&dev->mutex);
1974 return -EBUSY;
1975 }
1976
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001977 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001978 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001979 while (conf->enabled_functions.next !=
1980 &conf->enabled_functions) {
1981 f_holder = list_entry(conf->enabled_functions.next,
1982 typeof(*f_holder),
1983 enabled_list);
1984 f_holder->f->android_dev = NULL;
1985 list_del(&f_holder->enabled_list);
1986 kfree(f_holder);
1987 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001988 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001989 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001990
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301991 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001992 b = strim(buf);
1993
1994 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001995 conf_str = strsep(&b, ":");
1996 if (conf_str) {
1997 /* If the next not equal to the head, take it */
1998 if (curr_conf->next != &dev->configs)
1999 conf = list_entry(curr_conf->next,
2000 struct android_configuration,
2001 list_item);
2002 else
2003 conf = alloc_android_config(dev);
2004
2005 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002006 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002007
2008 while (conf_str) {
2009 name = strsep(&conf_str, ",");
2010 if (name) {
2011 err = android_enable_function(dev, conf, name);
2012 if (err)
2013 pr_err("android_usb: Cannot enable %s",
2014 name);
2015 }
2016 }
2017 }
2018
2019 /* Free uneeded configurations if exists */
2020 while (curr_conf->next != &dev->configs) {
2021 conf = list_entry(curr_conf->next,
2022 struct android_configuration, list_item);
2023 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002024 }
2025
2026 mutex_unlock(&dev->mutex);
2027
2028 return size;
2029}
2030
2031static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2032 char *buf)
2033{
2034 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002035
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302036 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002037}
2038
2039static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2040 const char *buff, size_t size)
2041{
2042 struct android_dev *dev = dev_get_drvdata(pdev);
2043 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002044 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002045 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002046 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302047 bool audio_enabled = false;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002048
Benoit Gobycf3fc062011-12-19 14:39:37 -08002049 if (!cdev)
2050 return -ENODEV;
2051
Benoit Goby1e8ce152011-12-12 13:01:23 -08002052 mutex_lock(&dev->mutex);
2053
2054 sscanf(buff, "%d", &enabled);
2055 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002056 /*
2057 * Update values in composite driver's copy of
2058 * device descriptor.
2059 */
2060 cdev->desc.idVendor = device_desc.idVendor;
2061 cdev->desc.idProduct = device_desc.idProduct;
2062 cdev->desc.bcdDevice = device_desc.bcdDevice;
2063 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2064 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2065 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302066
2067 /* Audio dock accessory is unable to enumerate device if
2068 * pull-up is enabled immediately. The enumeration is
2069 * reliable with 100 msec delay.
2070 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002071 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002072 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002073 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002074 if (f_holder->f->enable)
2075 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302076 if (!strncmp(f_holder->f->name,
2077 "audio_source", 12))
2078 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002079 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302080 if (audio_enabled)
2081 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002082 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002083 dev->enabled = true;
2084 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002085 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002086 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002087 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002088 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002089 if (f_holder->f->disable)
2090 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002091 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002092 dev->enabled = false;
2093 } else {
2094 pr_err("android_usb: already %s\n",
2095 dev->enabled ? "enabled" : "disabled");
2096 }
2097
2098 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002099
Benoit Gobyaab96812011-04-19 20:37:33 -07002100 return size;
2101}
2102
Ofir Cohen94213a72012-05-03 14:26:32 +03002103static ssize_t pm_qos_show(struct device *pdev,
2104 struct device_attribute *attr, char *buf)
2105{
2106 struct android_dev *dev = dev_get_drvdata(pdev);
2107
2108 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2109}
2110
2111static ssize_t pm_qos_store(struct device *pdev,
2112 struct device_attribute *attr,
2113 const char *buff, size_t size)
2114{
2115 struct android_dev *dev = dev_get_drvdata(pdev);
2116
2117 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2118
Benoit Goby1e8ce152011-12-12 13:01:23 -08002119 return size;
2120}
2121
2122static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2123 char *buf)
2124{
2125 struct android_dev *dev = dev_get_drvdata(pdev);
2126 struct usb_composite_dev *cdev = dev->cdev;
2127 char *state = "DISCONNECTED";
2128 unsigned long flags;
2129
2130 if (!cdev)
2131 goto out;
2132
2133 spin_lock_irqsave(&cdev->lock, flags);
2134 if (cdev->config)
2135 state = "CONFIGURED";
2136 else if (dev->connected)
2137 state = "CONNECTED";
2138 spin_unlock_irqrestore(&cdev->lock, flags);
2139out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302140 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002141}
2142
2143#define DESCRIPTOR_ATTR(field, format_string) \
2144static ssize_t \
2145field ## _show(struct device *dev, struct device_attribute *attr, \
2146 char *buf) \
2147{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302148 return snprintf(buf, PAGE_SIZE, \
2149 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002150} \
2151static ssize_t \
2152field ## _store(struct device *dev, struct device_attribute *attr, \
2153 const char *buf, size_t size) \
2154{ \
2155 int value; \
2156 if (sscanf(buf, format_string, &value) == 1) { \
2157 device_desc.field = value; \
2158 return size; \
2159 } \
2160 return -1; \
2161} \
2162static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2163
2164#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2165static ssize_t \
2166field ## _show(struct device *dev, struct device_attribute *attr, \
2167 char *buf) \
2168{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302169 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002170} \
2171static ssize_t \
2172field ## _store(struct device *dev, struct device_attribute *attr, \
2173 const char *buf, size_t size) \
2174{ \
2175 if (size >= sizeof(buffer)) \
2176 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302177 strlcpy(buffer, buf, sizeof(buffer)); \
2178 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302179 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002180} \
2181static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2182
2183
2184DESCRIPTOR_ATTR(idVendor, "%04x\n")
2185DESCRIPTOR_ATTR(idProduct, "%04x\n")
2186DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2187DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2188DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2189DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2190DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2191DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2192DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2193
2194static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2195 functions_store);
2196static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002197static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2198 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002199static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302200static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2201 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002202
2203static struct device_attribute *android_usb_attributes[] = {
2204 &dev_attr_idVendor,
2205 &dev_attr_idProduct,
2206 &dev_attr_bcdDevice,
2207 &dev_attr_bDeviceClass,
2208 &dev_attr_bDeviceSubClass,
2209 &dev_attr_bDeviceProtocol,
2210 &dev_attr_iManufacturer,
2211 &dev_attr_iProduct,
2212 &dev_attr_iSerial,
2213 &dev_attr_functions,
2214 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002215 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002216 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302217 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002218 NULL
2219};
2220
2221/*-------------------------------------------------------------------------*/
2222/* Composite driver */
2223
2224static int android_bind_config(struct usb_configuration *c)
2225{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002226 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002227 int ret = 0;
2228
2229 ret = android_bind_enabled_functions(dev, c);
2230 if (ret)
2231 return ret;
2232
2233 return 0;
2234}
2235
2236static void android_unbind_config(struct usb_configuration *c)
2237{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002238 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002239
2240 android_unbind_enabled_functions(dev, c);
2241}
2242
2243static int android_bind(struct usb_composite_dev *cdev)
2244{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002245 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002246 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002247 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002248 int gcnum, id, ret;
2249
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002250 /* Bind to the last android_dev that was probed */
2251 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2252
2253 dev->cdev = cdev;
2254
Benoit Goby1e8ce152011-12-12 13:01:23 -08002255 /*
2256 * Start disconnected. Userspace will connect the gadget once
2257 * it is done configuring the functions.
2258 */
2259 usb_gadget_disconnect(gadget);
2260
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002261 /* Init the supported functions only once, on the first android_dev */
2262 if (android_dev_count == 1) {
2263 ret = android_init_functions(dev->functions, cdev);
2264 if (ret)
2265 return ret;
2266 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002267
2268 /* Allocate string descriptor numbers ... note that string
2269 * contents can be overridden by the composite_dev glue.
2270 */
2271 id = usb_string_id(cdev);
2272 if (id < 0)
2273 return id;
2274 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2275 device_desc.iManufacturer = id;
2276
2277 id = usb_string_id(cdev);
2278 if (id < 0)
2279 return id;
2280 strings_dev[STRING_PRODUCT_IDX].id = id;
2281 device_desc.iProduct = id;
2282
2283 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302284 strlcpy(manufacturer_string, "Android",
2285 sizeof(manufacturer_string) - 1);
2286 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2287 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002288
2289 id = usb_string_id(cdev);
2290 if (id < 0)
2291 return id;
2292 strings_dev[STRING_SERIAL_IDX].id = id;
2293 device_desc.iSerialNumber = id;
2294
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302295 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002296 list_for_each_entry(conf, &dev->configs, list_item)
2297 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302298
Benoit Goby1e8ce152011-12-12 13:01:23 -08002299 gcnum = usb_gadget_controller_number(gadget);
2300 if (gcnum >= 0)
2301 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2302 else {
2303 pr_warning("%s: controller '%s' not recognized\n",
2304 longname, gadget->name);
2305 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2306 }
2307
Benoit Goby1e8ce152011-12-12 13:01:23 -08002308 return 0;
2309}
2310
2311static int android_usb_unbind(struct usb_composite_dev *cdev)
2312{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002313 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002314
Lena Salmand092f2d2012-03-12 17:27:24 +02002315 manufacturer_string[0] = '\0';
2316 product_string[0] = '\0';
2317 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002318 cancel_work_sync(&dev->work);
2319 android_cleanup_functions(dev->functions);
2320 return 0;
2321}
2322
2323static struct usb_composite_driver android_usb_driver = {
2324 .name = "android_usb",
2325 .dev = &device_desc,
2326 .strings = dev_strings,
2327 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002328 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002329};
2330
2331static int
2332android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2333{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002334 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002335 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002336 struct usb_request *req = cdev->req;
2337 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002338 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002339 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002340 int value = -EOPNOTSUPP;
2341 unsigned long flags;
2342
2343 req->zero = 0;
2344 req->complete = composite_setup_complete;
2345 req->length = 0;
2346 gadget->ep0->driver_data = cdev;
2347
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002348 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002349 list_for_each_entry(f_holder,
2350 &conf->enabled_functions,
2351 enabled_list) {
2352 f = f_holder->f;
2353 if (f->ctrlrequest) {
2354 value = f->ctrlrequest(f, cdev, c);
2355 if (value >= 0)
2356 break;
2357 }
2358 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002359
Benoit Gobycf3fc062011-12-19 14:39:37 -08002360 /* Special case the accessory function.
2361 * It needs to handle control requests before it is enabled.
2362 */
2363 if (value < 0)
2364 value = acc_ctrlrequest(cdev, c);
2365
Benoit Goby1e8ce152011-12-12 13:01:23 -08002366 if (value < 0)
2367 value = composite_setup(gadget, c);
2368
2369 spin_lock_irqsave(&cdev->lock, flags);
2370 if (!dev->connected) {
2371 dev->connected = 1;
2372 schedule_work(&dev->work);
2373 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2374 cdev->config) {
2375 schedule_work(&dev->work);
2376 }
2377 spin_unlock_irqrestore(&cdev->lock, flags);
2378
2379 return value;
2380}
2381
2382static void android_disconnect(struct usb_gadget *gadget)
2383{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002384 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002385 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002386 unsigned long flags;
2387
2388 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302389 /* accessory HID support can be active while the
2390 accessory function is not actually enabled,
2391 so we need to inform it when we are disconnected.
2392 */
2393 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002394
2395 spin_lock_irqsave(&cdev->lock, flags);
2396 dev->connected = 0;
2397 schedule_work(&dev->work);
2398 spin_unlock_irqrestore(&cdev->lock, flags);
2399}
2400
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002401static void android_suspend(struct usb_gadget *gadget)
2402{
2403 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2404 struct android_dev *dev = cdev_to_android_dev(cdev);
2405 unsigned long flags;
2406
2407 spin_lock_irqsave(&cdev->lock, flags);
2408 dev->suspended = 1;
2409 schedule_work(&dev->work);
2410 spin_unlock_irqrestore(&cdev->lock, flags);
2411
2412 composite_suspend(gadget);
2413}
2414
2415static void android_resume(struct usb_gadget *gadget)
2416{
2417 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2418 struct android_dev *dev = cdev_to_android_dev(cdev);
2419 unsigned long flags;
2420
2421 spin_lock_irqsave(&cdev->lock, flags);
2422 dev->suspended = 0;
2423 schedule_work(&dev->work);
2424 spin_unlock_irqrestore(&cdev->lock, flags);
2425
2426 composite_resume(gadget);
2427}
2428
2429
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002430static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002431{
2432 struct device_attribute **attrs = android_usb_attributes;
2433 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002434 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002435 int err;
2436
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002437 /*
2438 * The primary usb core should always have usb_core_id=0, since
2439 * Android user space is currently interested in android0 events.
2440 */
2441 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2442 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002443 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002444 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002445 if (IS_ERR(dev->dev))
2446 return PTR_ERR(dev->dev);
2447
2448 dev_set_drvdata(dev->dev, dev);
2449
2450 while ((attr = *attrs++)) {
2451 err = device_create_file(dev->dev, attr);
2452 if (err) {
2453 device_destroy(android_class, dev->dev->devt);
2454 return err;
2455 }
2456 }
2457 return 0;
2458}
2459
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302460static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002461{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302462 struct device_attribute **attrs = android_usb_attributes;
2463 struct device_attribute *attr;
2464
2465 while ((attr = *attrs++))
2466 device_remove_file(dev->dev, attr);
2467 device_destroy(android_class, dev->dev->devt);
2468}
2469
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002470static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2471{
2472 struct android_dev *dev = NULL;
2473
2474 /* Find the android dev from the list */
2475 list_for_each_entry(dev, &android_dev_list, list_item) {
2476 if (dev->cdev == cdev)
2477 break;
2478 }
2479
2480 return dev;
2481}
2482
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002483static struct android_configuration *alloc_android_config
2484 (struct android_dev *dev)
2485{
2486 struct android_configuration *conf;
2487
2488 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2489 if (!conf) {
2490 pr_err("%s(): Failed to alloc memory for android conf\n",
2491 __func__);
2492 return ERR_PTR(-ENOMEM);
2493 }
2494
2495 dev->configs_num++;
2496 conf->usb_config.label = dev->name;
2497 conf->usb_config.unbind = android_unbind_config;
2498 conf->usb_config.bConfigurationValue = dev->configs_num;
2499
2500 INIT_LIST_HEAD(&conf->enabled_functions);
2501
2502 list_add_tail(&conf->list_item, &dev->configs);
2503
2504 return conf;
2505}
2506
2507static void free_android_config(struct android_dev *dev,
2508 struct android_configuration *conf)
2509{
2510 list_del(&conf->list_item);
2511 dev->configs_num--;
2512 kfree(conf);
2513}
2514
Manu Gautam43c61a12012-08-22 17:09:37 -07002515static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2516{
2517 struct dload_struct local_diag_dload = { 0 };
2518 int *src, *dst, i;
2519
2520 if (!diag_dload) {
2521 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2522 return -ENODEV;
2523 }
2524
2525 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2526 __func__, diag_dload, pid, snum);
2527
2528 /* update pid */
2529 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2530 local_diag_dload.pid = pid;
2531
2532 /* update serial number */
2533 if (!snum) {
2534 local_diag_dload.magic_struct.serial_num = 0;
2535 memset(&local_diag_dload.serial_number, 0,
2536 SERIAL_NUMBER_LENGTH);
2537 } else {
2538 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2539 strlcpy((char *)&local_diag_dload.serial_number, snum,
2540 SERIAL_NUMBER_LENGTH);
2541 }
2542
2543 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2544 src = (int *)&local_diag_dload;
2545 dst = (int *)diag_dload;
2546
2547 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2548 *dst++ = *src++;
2549
2550 return 0;
2551}
2552
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002553static int __devinit android_probe(struct platform_device *pdev)
2554{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302555 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002556 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002557 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002558 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002559
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302560 if (pdev->dev.of_node) {
2561 dev_dbg(&pdev->dev, "device tree enabled\n");
2562 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2563 if (!pdata) {
2564 pr_err("unable to allocate platform data\n");
2565 return -ENOMEM;
2566 }
2567
2568 of_property_read_u32(pdev->dev.of_node,
2569 "qcom,android-usb-swfi-latency",
2570 &pdata->swfi_latency);
2571 } else {
2572 pdata = pdev->dev.platform_data;
2573 }
2574
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002575 if (!android_class) {
2576 android_class = class_create(THIS_MODULE, "android_usb");
2577 if (IS_ERR(android_class))
2578 return PTR_ERR(android_class);
2579 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002580
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002581 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2582 if (!android_dev) {
2583 pr_err("%s(): Failed to alloc memory for android_dev\n",
2584 __func__);
2585 ret = -ENOMEM;
2586 goto err_alloc;
2587 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002588
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002589 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002590 android_dev->disable_depth = 1;
2591 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002592 android_dev->configs_num = 0;
2593 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002594 INIT_WORK(&android_dev->work, android_work);
2595 mutex_init(&android_dev->mutex);
2596
2597 android_dev->pdata = pdata;
2598
2599 list_add_tail(&android_dev->list_item, &android_dev_list);
2600 android_dev_count++;
2601
2602 if (pdata)
2603 composite_driver.usb_core_id = pdata->usb_core_id;
2604 else
2605 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2606
Manu Gautam43c61a12012-08-22 17:09:37 -07002607 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2608 if (res) {
2609 diag_dload = devm_ioremap(&pdev->dev, res->start,
2610 resource_size(res));
2611 if (!diag_dload) {
2612 dev_err(&pdev->dev, "ioremap failed\n");
2613 ret = -ENOMEM;
2614 goto err_dev;
2615 }
2616 } else {
2617 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2618 }
2619
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002620 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302621 if (ret) {
2622 pr_err("%s(): android_create_device failed\n", __func__);
2623 goto err_dev;
2624 }
2625
Lena Salmand092f2d2012-03-12 17:27:24 +02002626 ret = usb_composite_probe(&android_usb_driver, android_bind);
2627 if (ret) {
2628 pr_err("%s(): Failed to register android "
2629 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302630 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002631 }
2632
Ofir Cohen94213a72012-05-03 14:26:32 +03002633 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302634 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002635 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002636 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002637 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002638
Lena Salmand092f2d2012-03-12 17:27:24 +02002639 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302640err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002641 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302642err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002643 list_del(&android_dev->list_item);
2644 android_dev_count--;
2645 kfree(android_dev);
2646err_alloc:
2647 if (list_empty(&android_dev_list)) {
2648 class_destroy(android_class);
2649 android_class = NULL;
2650 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302651 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002652}
2653
2654static int android_remove(struct platform_device *pdev)
2655{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002656 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002657 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002658 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302659
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002660 if (pdata)
2661 usb_core_id = pdata->usb_core_id;
2662
2663 /* Find the android dev from the list */
2664 list_for_each_entry(dev, &android_dev_list, list_item) {
2665 if (!dev->pdata)
2666 break; /*To backward compatibility*/
2667 if (dev->pdata->usb_core_id == usb_core_id)
2668 break;
2669 }
2670
2671 if (dev) {
2672 android_destroy_device(dev);
2673 if (pdata && pdata->swfi_latency)
2674 pm_qos_remove_request(&dev->pm_qos_req_dma);
2675 list_del(&dev->list_item);
2676 android_dev_count--;
2677 kfree(dev);
2678 }
2679
2680 if (list_empty(&android_dev_list)) {
2681 class_destroy(android_class);
2682 android_class = NULL;
2683 usb_composite_unregister(&android_usb_driver);
2684 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002686 return 0;
2687}
2688
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002689static const struct platform_device_id android_id_table[] __devinitconst = {
2690 {
2691 .name = "android_usb",
2692 },
2693 {
2694 .name = "android_usb_hsic",
2695 },
2696};
2697
Manu Gautam43c61a12012-08-22 17:09:37 -07002698static struct of_device_id usb_android_dt_match[] = {
2699 { .compatible = "qcom,android-usb",
2700 },
2701 {}
2702};
2703
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002704static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07002705 .driver = {
2706 .name = "android_usb",
2707 .of_match_table = usb_android_dt_match,
2708 },
Lena Salmand092f2d2012-03-12 17:27:24 +02002709 .probe = android_probe,
2710 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002711 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002712};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002713
2714static int __init init(void)
2715{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302716 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002717
Benoit Goby1e8ce152011-12-12 13:01:23 -08002718 /* Override composite driver functions */
2719 composite_driver.setup = android_setup;
2720 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002721 composite_driver.suspend = android_suspend;
2722 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002723
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002724 INIT_LIST_HEAD(&android_dev_list);
2725 android_dev_count = 0;
2726
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302727 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302728 if (ret) {
2729 pr_err("%s(): Failed to register android"
2730 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302731 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002732
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302733 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002734}
2735module_init(init);
2736
2737static void __exit cleanup(void)
2738{
Lena Salmand092f2d2012-03-12 17:27:24 +02002739 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002740}
2741module_exit(cleanup);