blob: d4bdf99d190adf89d27c2b7e576e9615639e176b [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"
Anna Perel3ee23dd2013-02-26 16:06:40 +020056#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +053057#include "f_audio_source.c"
Anna Perel3ee23dd2013-02-26 16:06:40 +020058#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080059#include "f_mass_storage.c"
60#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053061#include "u_sdio.c"
62#include "u_smd.c"
63#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053064#include "u_rmnet_ctrl_smd.c"
Bar Weiner0dae81b2013-02-14 13:53:54 +020065#include "u_rmnet_ctrl_qti.c"
Jack Pham427f6922011-11-23 19:42:00 -080066#include "u_ctrl_hsic.c"
67#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053068#include "u_ctrl_hsuart.c"
69#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053070#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080071#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080072#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053073#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080074#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080075#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080076#define USB_ETH_RNDIS y
77#include "f_rndis.c"
78#include "rndis.c"
Amit Blayf9b352b2013-03-04 15:01:40 +020079#include "f_qc_ecm.c"
Anna Perela8c991d2012-04-09 16:44:46 +030080#include "f_mbim.c"
Lena Salmandf7e7992013-03-15 09:46:27 +020081#include "u_bam_data.c"
Anna Perelf9d01552012-11-20 15:56:32 +020082#include "f_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020083#include "f_qc_rndis.c"
Jack Pham0ad82e62012-09-27 17:31:08 -070084#include "u_ether.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030085#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053086#ifdef CONFIG_TARGET_CORE
87#include "f_tcm.c"
88#endif
Jack Pham2ec5fdc2012-09-26 10:13:48 -070089#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +030090#include "u_uac1.c"
91#include "f_uac1.c"
Jack Pham2ec5fdc2012-09-26 10:13:48 -070092#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080093
94MODULE_AUTHOR("Mike Lockwood");
95MODULE_DESCRIPTION("Android Composite USB Driver");
96MODULE_LICENSE("GPL");
97MODULE_VERSION("1.0");
98
99static const char longname[] = "Gadget Android";
100
101/* Default vendor and product IDs, overridden by userspace */
102#define VENDOR_ID 0x18D1
103#define PRODUCT_ID 0x0001
104
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300105#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
106
Benoit Goby1e8ce152011-12-12 13:01:23 -0800107struct android_usb_function {
108 char *name;
109 void *config;
110
111 struct device *dev;
112 char *dev_name;
113 struct device_attribute **attributes;
114
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300115 struct android_dev *android_dev;
116
Benoit Goby1e8ce152011-12-12 13:01:23 -0800117 /* Optional: initialization during gadget bind */
118 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
119 /* Optional: cleanup during gadget unbind */
120 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700121 /* Optional: called when the function is added the list of
122 * enabled functions */
123 void (*enable)(struct android_usb_function *);
124 /* Optional: called when it is removed */
125 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800126
127 int (*bind_config)(struct android_usb_function *,
128 struct usb_configuration *);
129
130 /* Optional: called when the configuration is removed */
131 void (*unbind_config)(struct android_usb_function *,
132 struct usb_configuration *);
133 /* Optional: handle ctrl requests before the device is configured */
134 int (*ctrlrequest)(struct android_usb_function *,
135 struct usb_composite_dev *,
136 const struct usb_ctrlrequest *);
137};
138
Ido Shayevitz68557e32012-11-06 12:40:37 +0200139struct android_usb_function_holder {
140
141 struct android_usb_function *f;
142
143 /* for android_conf.enabled_functions */
144 struct list_head enabled_list;
145};
146
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200147/**
148* struct android_dev - represents android USB gadget device
149* @name: device name.
150* @functions: an array of all the supported USB function
151* drivers that this gadget support but not necessarily
152* added to one of the gadget configurations.
153* @cdev: The internal composite device. Android gadget device
154* is a composite device, such that it can support configurations
155* with more than one function driver.
156* @dev: The kernel device that represents this android device.
157* @enabled: True if the android gadget is enabled, means all
158* the configurations were set and all function drivers were
159* bind and ready for USB enumeration.
160* @disable_depth: Number of times the device was disabled, after
161* symmetrical number of enables the device willl be enabled.
162* Used for controlling ADB userspace disable/enable requests.
163* @mutex: Internal mutex for protecting device member fields.
164* @pdata: Platform data fetched from the kernel device platfrom data.
165* @connected: True if got connect notification from the gadget UDC.
166* False if got disconnect notification from the gadget UDC.
167* @sw_connected: Equal to 'connected' only after the connect
168* notification was handled by the android gadget work function.
169* @suspended: True if got suspend notification from the gadget UDC.
170* False if got resume notification from the gadget UDC.
171* @sw_suspended: Equal to 'suspended' only after the susped
172* notification was handled by the android gadget work function.
173* @pm_qos: An attribute string that can be set by user space in order to
174* determine pm_qos policy. Set to 'high' for always demand pm_qos
175* when USB bus is connected and resumed. Set to 'low' for disable
176* any setting of pm_qos by this driver. Default = 'high'.
177* @work: workqueue used for handling notifications from the gadget UDC.
178* @configs: List of configurations currently configured into the device.
179* The android gadget supports more than one configuration. The host
180* may choose one configuration from the suggested.
181* @configs_num: Number of configurations currently configured and existing
182* in the configs list.
183* @list_item: This driver supports more than one android gadget device (for
184* example in order to support multiple USB cores), therefore this is
185* a item in a linked list of android devices.
186*/
Benoit Goby1e8ce152011-12-12 13:01:23 -0800187struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300188 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800189 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800190 struct usb_composite_dev *cdev;
191 struct device *dev;
192
193 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700194 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800195 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700196 struct android_usb_platform_data *pdata;
197
Benoit Goby1e8ce152011-12-12 13:01:23 -0800198 bool connected;
199 bool sw_connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200200 bool suspended;
201 bool sw_suspended;
Ofir Cohen94213a72012-05-03 14:26:32 +0300202 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700203 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800204 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300205
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300206 /* A list of struct android_configuration */
207 struct list_head configs;
208 int configs_num;
209
210 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300211 struct list_head list_item;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300212};
213
214struct android_configuration {
215 struct usb_configuration usb_config;
216
217 /* A list of the functions supported by this config */
218 struct list_head enabled_functions;
219
220 /* A list node inside the struct android_dev.configs list */
221 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800222};
223
Manu Gautam43c61a12012-08-22 17:09:37 -0700224struct dload_struct __iomem *diag_dload;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800225static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300226static struct list_head android_dev_list;
227static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800228static int android_bind_config(struct usb_configuration *c);
229static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300230static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300231static struct android_configuration *alloc_android_config
232 (struct android_dev *dev);
233static void free_android_config(struct android_dev *dev,
234 struct android_configuration *conf);
Manu Gautam43c61a12012-08-22 17:09:37 -0700235static int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800236
237/* string IDs are assigned dynamically */
238#define STRING_MANUFACTURER_IDX 0
239#define STRING_PRODUCT_IDX 1
240#define STRING_SERIAL_IDX 2
241
242static char manufacturer_string[256];
243static char product_string[256];
244static char serial_string[256];
245
246/* String Table */
247static struct usb_string strings_dev[] = {
248 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
249 [STRING_PRODUCT_IDX].s = product_string,
250 [STRING_SERIAL_IDX].s = serial_string,
251 { } /* end of list */
252};
253
254static struct usb_gadget_strings stringtab_dev = {
255 .language = 0x0409, /* en-us */
256 .strings = strings_dev,
257};
258
259static struct usb_gadget_strings *dev_strings[] = {
260 &stringtab_dev,
261 NULL,
262};
263
264static struct usb_device_descriptor device_desc = {
265 .bLength = sizeof(device_desc),
266 .bDescriptorType = USB_DT_DEVICE,
267 .bcdUSB = __constant_cpu_to_le16(0x0200),
268 .bDeviceClass = USB_CLASS_PER_INTERFACE,
269 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
270 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
271 .bcdDevice = __constant_cpu_to_le16(0xffff),
272 .bNumConfigurations = 1,
273};
274
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530275static struct usb_otg_descriptor otg_descriptor = {
276 .bLength = sizeof otg_descriptor,
277 .bDescriptorType = USB_DT_OTG,
278 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
279 .bcdOTG = __constant_cpu_to_le16(0x0200),
280};
281
282static const struct usb_descriptor_header *otg_desc[] = {
283 (struct usb_descriptor_header *) &otg_descriptor,
284 NULL,
285};
286
Manu Gautama2b54142012-04-03 14:34:32 +0530287enum android_device_state {
288 USB_DISCONNECTED,
289 USB_CONNECTED,
290 USB_CONFIGURED,
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200291 USB_SUSPENDED,
292 USB_RESUMED
Manu Gautama2b54142012-04-03 14:34:32 +0530293};
294
Ofir Cohen94213a72012-05-03 14:26:32 +0300295static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
296{
297 struct android_usb_platform_data *pdata = dev->pdata;
298 u32 swfi_latency = 0;
299 static int last_vote = -1;
300
Ofir Cohen56eb7072012-05-20 11:41:39 +0300301 if (!pdata || vote == last_vote
302 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300303 return;
304
305 swfi_latency = pdata->swfi_latency + 1;
306 if (vote)
307 pm_qos_update_request(&dev->pm_qos_req_dma,
308 swfi_latency);
309 else
310 pm_qos_update_request(&dev->pm_qos_req_dma,
311 PM_QOS_DEFAULT_VALUE);
312 last_vote = vote;
313}
314
Benoit Goby1e8ce152011-12-12 13:01:23 -0800315static void android_work(struct work_struct *data)
316{
317 struct android_dev *dev = container_of(data, struct android_dev, work);
318 struct usb_composite_dev *cdev = dev->cdev;
319 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
320 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
321 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200322 char *suspended[2] = { "USB_STATE=SUSPENDED", NULL };
323 char *resumed[2] = { "USB_STATE=RESUMED", NULL };
Benoit Goby1e8ce152011-12-12 13:01:23 -0800324 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530325 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800326 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300327 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800328
329 spin_lock_irqsave(&cdev->lock, flags);
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200330 if (dev->suspended != dev->sw_suspended && cdev->config) {
331 if (strncmp(dev->pm_qos, "low", 3))
332 pm_qos_vote = dev->suspended ? 0 : 1;
333 next_state = dev->suspended ? USB_SUSPENDED : USB_RESUMED;
334 uevent_envp = dev->suspended ? suspended : resumed;
335 } else if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800336 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530337 next_state = USB_CONFIGURED;
338 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800339 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530340 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300341 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300342 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300343 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300344 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530345 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800346 dev->sw_connected = dev->connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200347 dev->sw_suspended = dev->suspended;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800348 spin_unlock_irqrestore(&cdev->lock, flags);
349
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300350 if (pm_qos_vote != -1)
351 android_pm_qos_update_latency(dev, pm_qos_vote);
352
Benoit Goby1e8ce152011-12-12 13:01:23 -0800353 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530354 /*
355 * Some userspace modules, e.g. MTP, work correctly only if
356 * CONFIGURED uevent is preceded by DISCONNECT uevent.
357 * Check if we missed sending out a DISCONNECT uevent. This can
358 * happen if host PC resets and configures device really quick.
359 */
360 if (((uevent_envp == connected) &&
361 (last_uevent != USB_DISCONNECTED)) ||
362 ((uevent_envp == configured) &&
363 (last_uevent == USB_CONFIGURED))) {
364 pr_info("%s: sent missed DISCONNECT event\n", __func__);
365 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
366 disconnected);
367 msleep(20);
368 }
369 /*
370 * Before sending out CONFIGURED uevent give function drivers
371 * a chance to wakeup userspace threads and notify disconnect
372 */
373 if (uevent_envp == configured)
374 msleep(50);
375
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200376 /* Do not notify on suspend / resume */
377 if (next_state != USB_SUSPENDED && next_state != USB_RESUMED) {
378 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
379 uevent_envp);
380 last_uevent = next_state;
381 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800382 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
383 } else {
384 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
385 dev->connected, dev->sw_connected, cdev->config);
386 }
387}
388
Benoit Goby80ba14d2012-03-19 18:56:52 -0700389static void android_enable(struct android_dev *dev)
390{
391 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300392 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700393
394 if (WARN_ON(!dev->disable_depth))
395 return;
396
397 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300398
399 list_for_each_entry(conf, &dev->configs, list_item)
400 usb_add_config(cdev, &conf->usb_config,
401 android_bind_config);
402
Benoit Goby80ba14d2012-03-19 18:56:52 -0700403 usb_gadget_connect(cdev->gadget);
404 }
405}
406
407static void android_disable(struct android_dev *dev)
408{
409 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300410 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700411
412 if (dev->disable_depth++ == 0) {
413 usb_gadget_disconnect(cdev->gadget);
414 /* Cancel pending control requests */
415 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300416
417 list_for_each_entry(conf, &dev->configs, list_item)
418 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700419 }
420}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800421
422/*-------------------------------------------------------------------------*/
423/* Supported functions initialization */
424
Benoit Goby80ba14d2012-03-19 18:56:52 -0700425struct adb_data {
426 bool opened;
427 bool enabled;
428};
429
Benoit Goby2b6862d2011-12-19 14:38:41 -0800430static int
431adb_function_init(struct android_usb_function *f,
432 struct usb_composite_dev *cdev)
433{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700434 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
435 if (!f->config)
436 return -ENOMEM;
437
Benoit Goby2b6862d2011-12-19 14:38:41 -0800438 return adb_setup();
439}
440
441static void adb_function_cleanup(struct android_usb_function *f)
442{
443 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700444 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800445}
446
447static int
448adb_function_bind_config(struct android_usb_function *f,
449 struct usb_configuration *c)
450{
451 return adb_bind_config(c);
452}
453
Benoit Goby80ba14d2012-03-19 18:56:52 -0700454static void adb_android_function_enable(struct android_usb_function *f)
455{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300456 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700457 struct adb_data *data = f->config;
458
459 data->enabled = true;
460
461 /* Disable the gadget until adbd is ready */
462 if (!data->opened)
463 android_disable(dev);
464}
465
466static void adb_android_function_disable(struct android_usb_function *f)
467{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300468 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700469 struct adb_data *data = f->config;
470
471 data->enabled = false;
472
473 /* Balance the disable that was called in closed_callback */
474 if (!data->opened)
475 android_enable(dev);
476}
477
Benoit Goby2b6862d2011-12-19 14:38:41 -0800478static struct android_usb_function adb_function = {
479 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700480 .enable = adb_android_function_enable,
481 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800482 .init = adb_function_init,
483 .cleanup = adb_function_cleanup,
484 .bind_config = adb_function_bind_config,
485};
486
Benoit Goby80ba14d2012-03-19 18:56:52 -0700487static void adb_ready_callback(void)
488{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300489 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700490 struct adb_data *data = adb_function.config;
491
Benoit Goby80ba14d2012-03-19 18:56:52 -0700492 data->opened = true;
493
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300494 if (data->enabled && dev) {
495 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700496 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300497 mutex_unlock(&dev->mutex);
498 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700499}
500
501static void adb_closed_callback(void)
502{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300503 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700504 struct adb_data *data = adb_function.config;
505
Benoit Goby80ba14d2012-03-19 18:56:52 -0700506 data->opened = false;
507
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300508 if (data->enabled) {
509 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700510 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300511 mutex_unlock(&dev->mutex);
512 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700513}
514
Benoit Goby2b6862d2011-12-19 14:38:41 -0800515
Benoit Gobyaab96812011-04-19 20:37:33 -0700516/*-------------------------------------------------------------------------*/
517/* Supported functions initialization */
518
Stephen Boyd83ed8e22013-03-08 17:04:08 -0800519/* ACM */
520static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
521static ssize_t acm_transports_store(
522 struct device *device, struct device_attribute *attr,
523 const char *buff, size_t size)
524{
525 strlcpy(acm_transports, buff, sizeof(acm_transports));
526
527 return size;
528}
529
530static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
531static struct device_attribute *acm_function_attributes[] = {
532 &dev_attr_acm_transports,
533 NULL
534};
535
536static void acm_function_cleanup(struct android_usb_function *f)
537{
538 gserial_cleanup();
539}
540
541static int
542acm_function_bind_config(struct android_usb_function *f,
543 struct usb_configuration *c)
544{
545 char *name;
546 char buf[32], *b;
547 int err = -1, i;
548 static int acm_initialized, ports;
549
550 if (acm_initialized)
551 goto bind_config;
552
553 acm_initialized = 1;
554 strlcpy(buf, acm_transports, sizeof(buf));
555 b = strim(buf);
556
557 while (b) {
558 name = strsep(&b, ",");
559
560 if (name) {
561 err = acm_init_port(ports, name);
562 if (err) {
563 pr_err("acm: Cannot open port '%s'", name);
564 goto out;
565 }
566 ports++;
567 }
568 }
569 err = acm_port_setup(c);
570 if (err) {
571 pr_err("acm: Cannot setup transports");
572 goto out;
573 }
574
575bind_config:
576 for (i = 0; i < ports; i++) {
577 err = acm_bind_config(c, i);
578 if (err) {
579 pr_err("acm: bind_config failed for port %d", i);
580 goto out;
581 }
582 }
583
584out:
585 return err;
586}
587
588static struct android_usb_function acm_function = {
589 .name = "acm",
590 .cleanup = acm_function_cleanup,
591 .bind_config = acm_function_bind_config,
592 .attributes = acm_function_attributes,
593};
594
Manu Gautam8e0719b2011-09-26 14:47:55 +0530595/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530596static int rmnet_smd_function_bind_config(struct android_usb_function *f,
597 struct usb_configuration *c)
598{
599 return rmnet_smd_bind_config(c);
600}
601
602static struct android_usb_function rmnet_smd_function = {
603 .name = "rmnet_smd",
604 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800605};
606
Manu Gautam8e0719b2011-09-26 14:47:55 +0530607/* RMNET_SDIO */
608static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
609 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800610{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530611 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800612}
613
Manu Gautam8e0719b2011-09-26 14:47:55 +0530614static struct android_usb_function rmnet_sdio_function = {
615 .name = "rmnet_sdio",
616 .bind_config = rmnet_sdio_function_bind_config,
617};
618
619/* RMNET_SMD_SDIO */
620static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
621 struct usb_composite_dev *cdev)
622{
623 return rmnet_smd_sdio_init();
624}
625
626static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
627{
628 rmnet_smd_sdio_cleanup();
629}
630
631static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
632 struct usb_configuration *c)
633{
634 return rmnet_smd_sdio_function_add(c);
635}
636
637static struct device_attribute *rmnet_smd_sdio_attributes[] = {
638 &dev_attr_transport, NULL };
639
640static struct android_usb_function rmnet_smd_sdio_function = {
641 .name = "rmnet_smd_sdio",
642 .init = rmnet_smd_sdio_function_init,
643 .cleanup = rmnet_smd_sdio_function_cleanup,
644 .bind_config = rmnet_smd_sdio_bind_config,
645 .attributes = rmnet_smd_sdio_attributes,
646};
647
Hemant Kumar1b820d52011-11-03 15:08:28 -0700648/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
649#define MAX_XPORT_STR_LEN 50
650static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530651
Manu Gautame3e897c2011-09-12 17:18:46 +0530652static void rmnet_function_cleanup(struct android_usb_function *f)
653{
654 frmnet_cleanup();
655}
656
Manu Gautam2b0234a2011-09-07 16:47:52 +0530657static int rmnet_function_bind_config(struct android_usb_function *f,
658 struct usb_configuration *c)
659{
660 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700661 int err = 0;
662 char *ctrl_name;
663 char *data_name;
664 char buf[MAX_XPORT_STR_LEN], *b;
665 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530666
Hemant Kumar1b820d52011-11-03 15:08:28 -0700667 if (!rmnet_initialized) {
668 rmnet_initialized = 1;
669 strlcpy(buf, rmnet_transports, sizeof(buf));
670 b = strim(buf);
671 while (b) {
672 ctrl_name = strsep(&b, ",");
673 data_name = strsep(&b, ",");
674 if (ctrl_name && data_name) {
675 err = frmnet_init_port(ctrl_name, data_name);
676 if (err) {
677 pr_err("rmnet: Cannot open ctrl port:"
678 "'%s' data port:'%s'\n",
679 ctrl_name, data_name);
680 goto out;
681 }
682 ports++;
683 }
684 }
685
686 err = rmnet_gport_setup();
687 if (err) {
688 pr_err("rmnet: Cannot setup transports");
689 goto out;
690 }
691 }
692
693 for (i = 0; i < ports; i++) {
694 err = frmnet_bind_config(c, i);
695 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530696 pr_err("Could not bind rmnet%u config\n", i);
697 break;
698 }
699 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700700out:
701 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530702}
703
Hemant Kumar1b820d52011-11-03 15:08:28 -0700704static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530705 struct device_attribute *attr, char *buf)
706{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700707 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530708}
709
Hemant Kumar1b820d52011-11-03 15:08:28 -0700710static ssize_t rmnet_transports_store(
711 struct device *device, struct device_attribute *attr,
712 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530713{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700714 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530715
Manu Gautam2b0234a2011-09-07 16:47:52 +0530716 return size;
717}
718
Hemant Kumar1b820d52011-11-03 15:08:28 -0700719static struct device_attribute dev_attr_rmnet_transports =
720 __ATTR(transports, S_IRUGO | S_IWUSR,
721 rmnet_transports_show,
722 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530723static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700724 &dev_attr_rmnet_transports,
725 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530726
727static struct android_usb_function rmnet_function = {
728 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530729 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530730 .bind_config = rmnet_function_bind_config,
731 .attributes = rmnet_function_attributes,
732};
733
Amit Blayf9b352b2013-03-04 15:01:40 +0200734/* ecm transport string */
735static char ecm_transports[MAX_XPORT_STR_LEN];
736
Ofir Cohen7b155422012-07-31 13:02:49 +0300737struct ecm_function_config {
738 u8 ethaddr[ETH_ALEN];
739};
740
741static int ecm_function_init(struct android_usb_function *f,
742 struct usb_composite_dev *cdev)
743{
744 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
745 if (!f->config)
746 return -ENOMEM;
747 return 0;
748}
749
750static void ecm_function_cleanup(struct android_usb_function *f)
751{
752 kfree(f->config);
753 f->config = NULL;
754}
755
756static int ecm_qc_function_bind_config(struct android_usb_function *f,
757 struct usb_configuration *c)
758{
759 int ret;
Amit Blayf9b352b2013-03-04 15:01:40 +0200760 char *trans;
Ofir Cohen7b155422012-07-31 13:02:49 +0300761 struct ecm_function_config *ecm = f->config;
762
763 if (!ecm) {
764 pr_err("%s: ecm_pdata\n", __func__);
765 return -EINVAL;
766 }
767
768 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
769 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
770 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
771
Amit Blayf9b352b2013-03-04 15:01:40 +0200772 pr_debug("%s: ecm_transport is %s", __func__, ecm_transports);
773
774 trans = strim(ecm_transports);
775 if (strcmp("BAM2BAM_IPA", trans)) {
776 ret = gether_qc_setup_name(c->cdev->gadget,
777 ecm->ethaddr, "ecm");
778 if (ret) {
779 pr_err("%s: gether_setup failed\n", __func__);
780 return ret;
781 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300782 }
783
Amit Blayf9b352b2013-03-04 15:01:40 +0200784 return ecm_qc_bind_config(c, ecm->ethaddr, trans);
Ofir Cohen7b155422012-07-31 13:02:49 +0300785}
786
787static void ecm_qc_function_unbind_config(struct android_usb_function *f,
788 struct usb_configuration *c)
789{
Amit Blayf9b352b2013-03-04 15:01:40 +0200790 char *trans = strim(ecm_transports);
791
792 if (strcmp("BAM2BAM_IPA", trans))
793 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300794}
795
796static ssize_t ecm_ethaddr_show(struct device *dev,
797 struct device_attribute *attr, char *buf)
798{
799 struct android_usb_function *f = dev_get_drvdata(dev);
800 struct ecm_function_config *ecm = f->config;
801 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
802 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
803 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
804}
805
806static ssize_t ecm_ethaddr_store(struct device *dev,
807 struct device_attribute *attr, const char *buf, size_t size)
808{
809 struct android_usb_function *f = dev_get_drvdata(dev);
810 struct ecm_function_config *ecm = f->config;
811
812 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
813 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
814 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
815 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
816 return size;
817 return -EINVAL;
818}
819
820static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
821 ecm_ethaddr_store);
822
Amit Blayf9b352b2013-03-04 15:01:40 +0200823static ssize_t ecm_transports_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
825{
826 return snprintf(buf, PAGE_SIZE, "%s\n", ecm_transports);
827}
828
829static ssize_t ecm_transports_store(struct device *dev,
830 struct device_attribute *attr, const char *buf, size_t size)
831{
832 strlcpy(ecm_transports, buf, sizeof(ecm_transports));
833 return size;
834}
835
836static DEVICE_ATTR(ecm_transports, S_IRUGO | S_IWUSR, ecm_transports_show,
837 ecm_transports_store);
838
Ofir Cohen7b155422012-07-31 13:02:49 +0300839static struct device_attribute *ecm_function_attributes[] = {
Amit Blayf9b352b2013-03-04 15:01:40 +0200840 &dev_attr_ecm_transports,
Ofir Cohen7b155422012-07-31 13:02:49 +0300841 &dev_attr_ecm_ethaddr,
842 NULL
843};
844
845static struct android_usb_function ecm_qc_function = {
846 .name = "ecm_qc",
847 .init = ecm_function_init,
848 .cleanup = ecm_function_cleanup,
849 .bind_config = ecm_qc_function_bind_config,
850 .unbind_config = ecm_qc_function_unbind_config,
851 .attributes = ecm_function_attributes,
852};
Anna Perela8c991d2012-04-09 16:44:46 +0300853
854/* MBIM - used with BAM */
855#define MAX_MBIM_INSTANCES 1
856
857static int mbim_function_init(struct android_usb_function *f,
858 struct usb_composite_dev *cdev)
859{
860 return mbim_init(MAX_MBIM_INSTANCES);
861}
862
863static void mbim_function_cleanup(struct android_usb_function *f)
864{
865 fmbim_cleanup();
866}
867
Lena Salmandf7e7992013-03-15 09:46:27 +0200868
869/* mbim transport string */
870static char mbim_transports[MAX_XPORT_STR_LEN];
871
Anna Perela8c991d2012-04-09 16:44:46 +0300872static int mbim_function_bind_config(struct android_usb_function *f,
873 struct usb_configuration *c)
874{
Lena Salmandf7e7992013-03-15 09:46:27 +0200875 char *trans;
876
877 pr_debug("%s: mbim transport is %s", __func__, mbim_transports);
878 trans = strim(mbim_transports);
879 return mbim_bind_config(c, 0, trans);
Anna Perela8c991d2012-04-09 16:44:46 +0300880}
881
Jack Pham2df2f702012-10-11 19:08:24 -0700882static int mbim_function_ctrlrequest(struct android_usb_function *f,
883 struct usb_composite_dev *cdev,
884 const struct usb_ctrlrequest *c)
885{
886 return mbim_ctrlrequest(cdev, c);
887}
888
Lena Salmandf7e7992013-03-15 09:46:27 +0200889static ssize_t mbim_transports_show(struct device *dev,
890 struct device_attribute *attr, char *buf)
891{
892 return snprintf(buf, PAGE_SIZE, "%s\n", mbim_transports);
893}
894
895static ssize_t mbim_transports_store(struct device *dev,
896 struct device_attribute *attr, const char *buf, size_t size)
897{
898 strlcpy(mbim_transports, buf, sizeof(mbim_transports));
899 return size;
900}
901
902static DEVICE_ATTR(mbim_transports, S_IRUGO | S_IWUSR, mbim_transports_show,
903 mbim_transports_store);
904
905static struct device_attribute *mbim_function_attributes[] = {
906 &dev_attr_mbim_transports,
907 NULL
908};
909
Anna Perela8c991d2012-04-09 16:44:46 +0300910static struct android_usb_function mbim_function = {
911 .name = "usb_mbim",
912 .cleanup = mbim_function_cleanup,
913 .bind_config = mbim_function_bind_config,
914 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -0700915 .ctrlrequest = mbim_function_ctrlrequest,
Lena Salmandf7e7992013-03-15 09:46:27 +0200916 .attributes = mbim_function_attributes,
Anna Perela8c991d2012-04-09 16:44:46 +0300917};
918
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700919#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +0300920/* PERIPHERAL AUDIO */
921static int audio_function_bind_config(struct android_usb_function *f,
922 struct usb_configuration *c)
923{
924 return audio_bind_config(c);
925}
926
927static struct android_usb_function audio_function = {
928 .name = "audio",
929 .bind_config = audio_function_bind_config,
930};
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700931#endif
Anna Perel432367a2012-09-20 10:55:32 +0300932
Anna Perela8c991d2012-04-09 16:44:46 +0300933
Manu Gautam8e0719b2011-09-26 14:47:55 +0530934/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530935static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936static ssize_t clients_store(
937 struct device *device, struct device_attribute *attr,
938 const char *buff, size_t size)
939{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530940 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
942 return size;
943}
944
945static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
946static struct device_attribute *diag_function_attributes[] =
947 { &dev_attr_clients, NULL };
948
949static int diag_function_init(struct android_usb_function *f,
950 struct usb_composite_dev *cdev)
951{
952 return diag_setup();
953}
954
955static void diag_function_cleanup(struct android_usb_function *f)
956{
957 diag_cleanup();
958}
959
960static int diag_function_bind_config(struct android_usb_function *f,
961 struct usb_configuration *c)
962{
963 char *name;
964 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530965 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800966 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300967 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530969 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 b = strim(buf);
971
972 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800973 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530975 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -0700976 if (!once++) {
977 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
978 notify = dev->pdata->update_pid_and_serial_num;
979 else
980 notify = usb_diag_update_pid_and_serial_num;
981 }
Manu Gautamc5760302011-08-25 14:30:24 +0530982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530984 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 if (err)
986 pr_err("diag: Cannot open channel '%s'", name);
987 }
988 }
989
990 return err;
991}
992
993static struct android_usb_function diag_function = {
994 .name = "diag",
995 .init = diag_function_init,
996 .cleanup = diag_function_cleanup,
997 .bind_config = diag_function_bind_config,
998 .attributes = diag_function_attributes,
999};
1000
Shimrit Malichia00d7322012-08-05 13:56:28 +03001001/* DEBUG */
1002static int qdss_function_init(struct android_usb_function *f,
1003 struct usb_composite_dev *cdev)
1004{
1005 return qdss_setup();
1006}
1007
1008static void qdss_function_cleanup(struct android_usb_function *f)
1009{
1010 qdss_cleanup();
1011}
1012
1013static int qdss_function_bind_config(struct android_usb_function *f,
1014 struct usb_configuration *c)
1015{
1016 int err = -1;
1017
1018 err = qdss_bind_config(c, "qdss");
1019 if (err)
1020 pr_err("qdss: Cannot open channel qdss");
1021
1022 return err;
1023}
1024
1025static struct android_usb_function qdss_function = {
1026 .name = "qdss",
1027 .init = qdss_function_init,
1028 .cleanup = qdss_function_cleanup,
1029 .bind_config = qdss_function_bind_config,
1030};
1031
Manu Gautam8e0719b2011-09-26 14:47:55 +05301032/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301033static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +05301034static ssize_t serial_transports_store(
1035 struct device *device, struct device_attribute *attr,
1036 const char *buff, size_t size)
1037{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301038 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +05301039
1040 return size;
1041}
1042
1043static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
1044static struct device_attribute *serial_function_attributes[] =
1045 { &dev_attr_transports, NULL };
1046
1047static void serial_function_cleanup(struct android_usb_function *f)
1048{
1049 gserial_cleanup();
1050}
1051
1052static int serial_function_bind_config(struct android_usb_function *f,
1053 struct usb_configuration *c)
1054{
1055 char *name;
1056 char buf[32], *b;
1057 int err = -1, i;
1058 static int serial_initialized = 0, ports = 0;
1059
1060 if (serial_initialized)
1061 goto bind_config;
1062
1063 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301064 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +05301065 b = strim(buf);
1066
1067 while (b) {
1068 name = strsep(&b, ",");
1069
1070 if (name) {
1071 err = gserial_init_port(ports, name);
1072 if (err) {
1073 pr_err("serial: Cannot open port '%s'", name);
1074 goto out;
1075 }
1076 ports++;
1077 }
1078 }
1079 err = gport_setup(c);
1080 if (err) {
1081 pr_err("serial: Cannot setup transports");
1082 goto out;
1083 }
1084
1085bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +02001086 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +05301087 err = gser_bind_config(c, i);
1088 if (err) {
1089 pr_err("serial: bind_config failed for port %d", i);
1090 goto out;
1091 }
1092 }
1093
1094out:
1095 return err;
1096}
1097
1098static struct android_usb_function serial_function = {
1099 .name = "serial",
1100 .cleanup = serial_function_cleanup,
1101 .bind_config = serial_function_bind_config,
1102 .attributes = serial_function_attributes,
1103};
1104
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301105/* CCID */
1106static int ccid_function_init(struct android_usb_function *f,
1107 struct usb_composite_dev *cdev)
1108{
1109 return ccid_setup();
1110}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001111
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301112static void ccid_function_cleanup(struct android_usb_function *f)
1113{
1114 ccid_cleanup();
1115}
1116
1117static int ccid_function_bind_config(struct android_usb_function *f,
1118 struct usb_configuration *c)
1119{
1120 return ccid_bind_config(c);
1121}
1122
1123static struct android_usb_function ccid_function = {
1124 .name = "ccid",
1125 .init = ccid_function_init,
1126 .cleanup = ccid_function_cleanup,
1127 .bind_config = ccid_function_bind_config,
1128};
1129
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001130static int
1131mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001132 struct usb_composite_dev *cdev)
1133{
1134 return mtp_setup();
1135}
1136
1137static void mtp_function_cleanup(struct android_usb_function *f)
1138{
1139 mtp_cleanup();
1140}
1141
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001142static int
1143mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001144 struct usb_configuration *c)
1145{
1146 return mtp_bind_config(c, false);
1147}
1148
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001149static int
1150ptp_function_init(struct android_usb_function *f,
1151 struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001152{
1153 /* nothing to do - initialization is handled by mtp_function_init */
1154 return 0;
1155}
1156
1157static void ptp_function_cleanup(struct android_usb_function *f)
1158{
1159 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1160}
1161
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001162static int
1163ptp_function_bind_config(struct android_usb_function *f,
1164 struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001165{
1166 return mtp_bind_config(c, true);
1167}
1168
1169static int mtp_function_ctrlrequest(struct android_usb_function *f,
1170 struct usb_composite_dev *cdev,
1171 const struct usb_ctrlrequest *c)
1172{
1173 return mtp_ctrlrequest(cdev, c);
1174}
1175
1176static struct android_usb_function mtp_function = {
1177 .name = "mtp",
1178 .init = mtp_function_init,
1179 .cleanup = mtp_function_cleanup,
1180 .bind_config = mtp_function_bind_config,
1181 .ctrlrequest = mtp_function_ctrlrequest,
1182};
1183
1184/* PTP function is same as MTP with slightly different interface descriptor */
1185static struct android_usb_function ptp_function = {
1186 .name = "ptp",
1187 .init = ptp_function_init,
1188 .cleanup = ptp_function_cleanup,
1189 .bind_config = ptp_function_bind_config,
1190};
1191
1192
Benoit Goby1e8ce152011-12-12 13:01:23 -08001193struct rndis_function_config {
1194 u8 ethaddr[ETH_ALEN];
1195 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001196 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001197 char manufacturer[256];
1198 /* "Wireless" RNDIS; auto-detected by Windows */
1199 bool wceis;
1200};
1201
1202static int
1203rndis_function_init(struct android_usb_function *f,
1204 struct usb_composite_dev *cdev)
1205{
1206 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1207 if (!f->config)
1208 return -ENOMEM;
1209 return 0;
1210}
1211
1212static void rndis_function_cleanup(struct android_usb_function *f)
1213{
1214 kfree(f->config);
1215 f->config = NULL;
1216}
1217
Ofir Cohenaef90b72012-07-31 12:37:04 +02001218static int rndis_qc_function_init(struct android_usb_function *f,
1219 struct usb_composite_dev *cdev)
1220{
1221 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1222 if (!f->config)
1223 return -ENOMEM;
1224
1225 return rndis_qc_init();
1226}
1227
1228static void rndis_qc_function_cleanup(struct android_usb_function *f)
1229{
1230 rndis_qc_cleanup();
1231 kfree(f->config);
1232}
1233
Benoit Goby1e8ce152011-12-12 13:01:23 -08001234static int
1235rndis_function_bind_config(struct android_usb_function *f,
1236 struct usb_configuration *c)
1237{
1238 int ret;
1239 struct rndis_function_config *rndis = f->config;
1240
1241 if (!rndis) {
1242 pr_err("%s: rndis_pdata\n", __func__);
1243 return -1;
1244 }
1245
1246 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1247 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1248 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1249
1250 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1251 if (ret) {
1252 pr_err("%s: gether_setup failed\n", __func__);
1253 return ret;
1254 }
1255
1256 if (rndis->wceis) {
1257 /* "Wireless" RNDIS; auto-detected by Windows */
1258 rndis_iad_descriptor.bFunctionClass =
1259 USB_CLASS_WIRELESS_CONTROLLER;
1260 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1261 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1262 rndis_control_intf.bInterfaceClass =
1263 USB_CLASS_WIRELESS_CONTROLLER;
1264 rndis_control_intf.bInterfaceSubClass = 0x01;
1265 rndis_control_intf.bInterfaceProtocol = 0x03;
1266 }
1267
1268 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1269 rndis->manufacturer);
1270}
1271
Ofir Cohenaef90b72012-07-31 12:37:04 +02001272static int rndis_qc_function_bind_config(struct android_usb_function *f,
1273 struct usb_configuration *c)
1274{
1275 int ret;
1276 struct rndis_function_config *rndis = f->config;
1277
1278 if (!rndis) {
1279 pr_err("%s: rndis_pdata\n", __func__);
1280 return -EINVAL;
1281 }
1282
1283 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1284 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1285 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1286
1287 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1288 if (ret) {
1289 pr_err("%s: gether_setup failed\n", __func__);
1290 return ret;
1291 }
1292
1293 if (rndis->wceis) {
1294 /* "Wireless" RNDIS; auto-detected by Windows */
1295 rndis_qc_iad_descriptor.bFunctionClass =
1296 USB_CLASS_WIRELESS_CONTROLLER;
1297 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1298 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1299 rndis_qc_control_intf.bInterfaceClass =
1300 USB_CLASS_WIRELESS_CONTROLLER;
1301 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1302 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1303 }
1304
1305 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1306 rndis->manufacturer,
1307 rndis->max_pkt_per_xfer);
1308}
1309
Benoit Goby1e8ce152011-12-12 13:01:23 -08001310static void rndis_function_unbind_config(struct android_usb_function *f,
1311 struct usb_configuration *c)
1312{
1313 gether_cleanup();
1314}
1315
Ofir Cohenaef90b72012-07-31 12:37:04 +02001316static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1317 struct usb_configuration *c)
1318{
Amit Blayd6d690a2012-10-16 13:37:42 +02001319 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001320}
1321
Benoit Goby1e8ce152011-12-12 13:01:23 -08001322static ssize_t rndis_manufacturer_show(struct device *dev,
1323 struct device_attribute *attr, char *buf)
1324{
1325 struct android_usb_function *f = dev_get_drvdata(dev);
1326 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001327
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301328 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001329}
1330
1331static ssize_t rndis_manufacturer_store(struct device *dev,
1332 struct device_attribute *attr, const char *buf, size_t size)
1333{
1334 struct android_usb_function *f = dev_get_drvdata(dev);
1335 struct rndis_function_config *config = f->config;
1336
1337 if (size >= sizeof(config->manufacturer))
1338 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001339
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301340 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001341 return size;
1342 return -1;
1343}
1344
1345static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1346 rndis_manufacturer_store);
1347
1348static ssize_t rndis_wceis_show(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1350{
1351 struct android_usb_function *f = dev_get_drvdata(dev);
1352 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001353
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301354 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001355}
1356
1357static ssize_t rndis_wceis_store(struct device *dev,
1358 struct device_attribute *attr, const char *buf, size_t size)
1359{
1360 struct android_usb_function *f = dev_get_drvdata(dev);
1361 struct rndis_function_config *config = f->config;
1362 int value;
1363
1364 if (sscanf(buf, "%d", &value) == 1) {
1365 config->wceis = value;
1366 return size;
1367 }
1368 return -EINVAL;
1369}
1370
1371static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1372 rndis_wceis_store);
1373
1374static ssize_t rndis_ethaddr_show(struct device *dev,
1375 struct device_attribute *attr, char *buf)
1376{
1377 struct android_usb_function *f = dev_get_drvdata(dev);
1378 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001379
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301380 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001381 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1382 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1383}
1384
1385static ssize_t rndis_ethaddr_store(struct device *dev,
1386 struct device_attribute *attr, const char *buf, size_t size)
1387{
1388 struct android_usb_function *f = dev_get_drvdata(dev);
1389 struct rndis_function_config *rndis = f->config;
1390
1391 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1392 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1393 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1394 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1395 return size;
1396 return -EINVAL;
1397}
1398
1399static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1400 rndis_ethaddr_store);
1401
1402static ssize_t rndis_vendorID_show(struct device *dev,
1403 struct device_attribute *attr, char *buf)
1404{
1405 struct android_usb_function *f = dev_get_drvdata(dev);
1406 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001407
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301408 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001409}
1410
1411static ssize_t rndis_vendorID_store(struct device *dev,
1412 struct device_attribute *attr, const char *buf, size_t size)
1413{
1414 struct android_usb_function *f = dev_get_drvdata(dev);
1415 struct rndis_function_config *config = f->config;
1416 int value;
1417
1418 if (sscanf(buf, "%04x", &value) == 1) {
1419 config->vendorID = value;
1420 return size;
1421 }
1422 return -EINVAL;
1423}
1424
1425static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1426 rndis_vendorID_store);
1427
Ofir Cohenaef90b72012-07-31 12:37:04 +02001428static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1430{
1431 struct android_usb_function *f = dev_get_drvdata(dev);
1432 struct rndis_function_config *config = f->config;
1433 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1434}
1435
1436static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1437 struct device_attribute *attr, const char *buf, size_t size)
1438{
1439 struct android_usb_function *f = dev_get_drvdata(dev);
1440 struct rndis_function_config *config = f->config;
1441 int value;
1442
1443 if (sscanf(buf, "%d", &value) == 1) {
1444 config->max_pkt_per_xfer = value;
1445 return size;
1446 }
1447 return -EINVAL;
1448}
1449
1450static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1451 rndis_max_pkt_per_xfer_show,
1452 rndis_max_pkt_per_xfer_store);
1453
Benoit Goby1e8ce152011-12-12 13:01:23 -08001454static struct device_attribute *rndis_function_attributes[] = {
1455 &dev_attr_manufacturer,
1456 &dev_attr_wceis,
1457 &dev_attr_ethaddr,
1458 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001459 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001460 NULL
1461};
1462
1463static struct android_usb_function rndis_function = {
1464 .name = "rndis",
1465 .init = rndis_function_init,
1466 .cleanup = rndis_function_cleanup,
1467 .bind_config = rndis_function_bind_config,
1468 .unbind_config = rndis_function_unbind_config,
1469 .attributes = rndis_function_attributes,
1470};
1471
Ofir Cohenaef90b72012-07-31 12:37:04 +02001472static struct android_usb_function rndis_qc_function = {
1473 .name = "rndis_qc",
1474 .init = rndis_qc_function_init,
1475 .cleanup = rndis_qc_function_cleanup,
1476 .bind_config = rndis_qc_function_bind_config,
1477 .unbind_config = rndis_qc_function_unbind_config,
1478 .attributes = rndis_function_attributes,
1479};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001480
Anna Perelf9d01552012-11-20 15:56:32 +02001481static int ecm_function_bind_config(struct android_usb_function *f,
1482 struct usb_configuration *c)
1483{
1484 int ret;
1485 struct ecm_function_config *ecm = f->config;
1486
1487 if (!ecm) {
1488 pr_err("%s: ecm_pdata\n", __func__);
1489 return -EINVAL;
1490 }
1491
1492 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1493 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1494 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1495
1496 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1497 if (ret) {
1498 pr_err("%s: gether_setup failed\n", __func__);
1499 return ret;
1500 }
1501
1502 ret = ecm_bind_config(c, ecm->ethaddr);
1503 if (ret) {
1504 pr_err("%s: ecm_bind_config failed\n", __func__);
1505 gether_cleanup();
1506 }
1507 return ret;
1508}
1509
1510static void ecm_function_unbind_config(struct android_usb_function *f,
1511 struct usb_configuration *c)
1512{
1513 gether_cleanup();
1514}
1515
1516static struct android_usb_function ecm_function = {
1517 .name = "ecm",
1518 .init = ecm_function_init,
1519 .cleanup = ecm_function_cleanup,
1520 .bind_config = ecm_function_bind_config,
1521 .unbind_config = ecm_function_unbind_config,
1522 .attributes = ecm_function_attributes,
1523};
1524
Benoit Goby1e8ce152011-12-12 13:01:23 -08001525struct mass_storage_function_config {
1526 struct fsg_config fsg;
1527 struct fsg_common *common;
1528};
1529
1530static int mass_storage_function_init(struct android_usb_function *f,
1531 struct usb_composite_dev *cdev)
1532{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001533 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001534 struct mass_storage_function_config *config;
1535 struct fsg_common *common;
1536 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301537 int i;
1538 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001539
1540 config = kzalloc(sizeof(struct mass_storage_function_config),
1541 GFP_KERNEL);
1542 if (!config)
1543 return -ENOMEM;
1544
1545 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301546 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301547 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301548 config->fsg.nluns = 2;
1549 config->fsg.luns[1].cdrom = 1;
1550 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301551 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301552 name[1] = "lun0";
1553 }
1554
Benoit Goby1e8ce152011-12-12 13:01:23 -08001555 config->fsg.luns[0].removable = 1;
1556
1557 common = fsg_common_init(NULL, cdev, &config->fsg);
1558 if (IS_ERR(common)) {
1559 kfree(config);
1560 return PTR_ERR(common);
1561 }
1562
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301563 for (i = 0; i < config->fsg.nluns; i++) {
1564 err = sysfs_create_link(&f->dev->kobj,
1565 &common->luns[i].dev.kobj,
1566 name[i]);
1567 if (err)
1568 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001569 }
1570
1571 config->common = common;
1572 f->config = config;
1573 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301574error:
1575 for (; i > 0 ; i--)
1576 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1577
1578 fsg_common_release(&common->ref);
1579 kfree(config);
1580 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001581}
1582
1583static void mass_storage_function_cleanup(struct android_usb_function *f)
1584{
1585 kfree(f->config);
1586 f->config = NULL;
1587}
1588
1589static int mass_storage_function_bind_config(struct android_usb_function *f,
1590 struct usb_configuration *c)
1591{
1592 struct mass_storage_function_config *config = f->config;
1593 return fsg_bind_config(c->cdev, c, config->common);
1594}
1595
1596static ssize_t mass_storage_inquiry_show(struct device *dev,
1597 struct device_attribute *attr, char *buf)
1598{
1599 struct android_usb_function *f = dev_get_drvdata(dev);
1600 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301601 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001602}
1603
1604static ssize_t mass_storage_inquiry_store(struct device *dev,
1605 struct device_attribute *attr, const char *buf, size_t size)
1606{
1607 struct android_usb_function *f = dev_get_drvdata(dev);
1608 struct mass_storage_function_config *config = f->config;
1609 if (size >= sizeof(config->common->inquiry_string))
1610 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301611 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001612 return -EINVAL;
1613 return size;
1614}
1615
1616static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1617 mass_storage_inquiry_show,
1618 mass_storage_inquiry_store);
1619
1620static struct device_attribute *mass_storage_function_attributes[] = {
1621 &dev_attr_inquiry_string,
1622 NULL
1623};
1624
1625static struct android_usb_function mass_storage_function = {
1626 .name = "mass_storage",
1627 .init = mass_storage_function_init,
1628 .cleanup = mass_storage_function_cleanup,
1629 .bind_config = mass_storage_function_bind_config,
1630 .attributes = mass_storage_function_attributes,
1631};
1632
1633
Benoit Gobycf3fc062011-12-19 14:39:37 -08001634static int accessory_function_init(struct android_usb_function *f,
1635 struct usb_composite_dev *cdev)
1636{
1637 return acc_setup();
1638}
1639
1640static void accessory_function_cleanup(struct android_usb_function *f)
1641{
1642 acc_cleanup();
1643}
1644
1645static int accessory_function_bind_config(struct android_usb_function *f,
1646 struct usb_configuration *c)
1647{
1648 return acc_bind_config(c);
1649}
1650
1651static int accessory_function_ctrlrequest(struct android_usb_function *f,
1652 struct usb_composite_dev *cdev,
1653 const struct usb_ctrlrequest *c)
1654{
1655 return acc_ctrlrequest(cdev, c);
1656}
1657
1658static struct android_usb_function accessory_function = {
1659 .name = "accessory",
1660 .init = accessory_function_init,
1661 .cleanup = accessory_function_cleanup,
1662 .bind_config = accessory_function_bind_config,
1663 .ctrlrequest = accessory_function_ctrlrequest,
1664};
1665
Anna Perel3ee23dd2013-02-26 16:06:40 +02001666#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301667static int audio_source_function_init(struct android_usb_function *f,
1668 struct usb_composite_dev *cdev)
1669{
1670 struct audio_source_config *config;
1671
1672 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1673 if (!config)
1674 return -ENOMEM;
1675 config->card = -1;
1676 config->device = -1;
1677 f->config = config;
1678 return 0;
1679}
1680
1681static void audio_source_function_cleanup(struct android_usb_function *f)
1682{
1683 kfree(f->config);
1684}
1685
1686static int audio_source_function_bind_config(struct android_usb_function *f,
1687 struct usb_configuration *c)
1688{
1689 struct audio_source_config *config = f->config;
1690
1691 return audio_source_bind_config(c, config);
1692}
1693
1694static void audio_source_function_unbind_config(struct android_usb_function *f,
1695 struct usb_configuration *c)
1696{
1697 struct audio_source_config *config = f->config;
1698
1699 config->card = -1;
1700 config->device = -1;
1701}
1702
1703static ssize_t audio_source_pcm_show(struct device *dev,
1704 struct device_attribute *attr, char *buf)
1705{
1706 struct android_usb_function *f = dev_get_drvdata(dev);
1707 struct audio_source_config *config = f->config;
1708
1709 /* print PCM card and device numbers */
1710 return sprintf(buf, "%d %d\n", config->card, config->device);
1711}
1712
1713static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1714
1715static struct device_attribute *audio_source_function_attributes[] = {
1716 &dev_attr_pcm,
1717 NULL
1718};
1719
1720static struct android_usb_function audio_source_function = {
1721 .name = "audio_source",
1722 .init = audio_source_function_init,
1723 .cleanup = audio_source_function_cleanup,
1724 .bind_config = audio_source_function_bind_config,
1725 .unbind_config = audio_source_function_unbind_config,
1726 .attributes = audio_source_function_attributes,
1727};
Anna Perel3ee23dd2013-02-26 16:06:40 +02001728#endif
Mike Lockwood11874822012-08-27 16:43:53 +05301729
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301730static int android_uasp_connect_cb(bool connect)
1731{
1732 /*
1733 * TODO
1734 * We may have to disable gadget till UASP configfs nodes
1735 * are configured which includes mapping LUN with the
1736 * backing file. It is a fundamental difference between
1737 * f_mass_storage and f_tcp. That means UASP can not be
1738 * in default composition.
1739 *
1740 * For now, assume that UASP configfs nodes are configured
1741 * before enabling android gadget. Or cable should be
1742 * reconnected after mapping the LUN.
1743 *
1744 * Also consider making UASP to respond to Host requests when
1745 * Lun is not mapped.
1746 */
1747 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1748
1749 return 0;
1750}
1751
1752static int uasp_function_init(struct android_usb_function *f,
1753 struct usb_composite_dev *cdev)
1754{
1755 return f_tcm_init(&android_uasp_connect_cb);
1756}
1757
1758static void uasp_function_cleanup(struct android_usb_function *f)
1759{
1760 f_tcm_exit();
1761}
1762
1763static int uasp_function_bind_config(struct android_usb_function *f,
1764 struct usb_configuration *c)
1765{
1766 return tcm_bind_config(c);
1767}
1768
1769static struct android_usb_function uasp_function = {
1770 .name = "uasp",
1771 .init = uasp_function_init,
1772 .cleanup = uasp_function_cleanup,
1773 .bind_config = uasp_function_bind_config,
1774};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001775
Benoit Goby1e8ce152011-12-12 13:01:23 -08001776static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001777 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001778 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001779#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001780 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001781#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301782 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301783 &rmnet_sdio_function,
1784 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301785 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001787 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301788 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001789 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301790 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001791 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001792 &mtp_function,
1793 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001794 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001795 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02001796 &ecm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001797 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001798 &accessory_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001799#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301800 &audio_source_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001801#endif
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301802 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001803 NULL
1804};
1805
Lena Salmand092f2d2012-03-12 17:27:24 +02001806static void android_cleanup_functions(struct android_usb_function **functions)
1807{
1808 struct android_usb_function *f;
1809 struct device_attribute **attrs;
1810 struct device_attribute *attr;
1811
1812 while (*functions) {
1813 f = *functions++;
1814
1815 if (f->dev) {
1816 device_destroy(android_class, f->dev->devt);
1817 kfree(f->dev_name);
1818 } else
1819 continue;
1820
1821 if (f->cleanup)
1822 f->cleanup(f);
1823
1824 attrs = f->attributes;
1825 if (attrs) {
1826 while ((attr = *attrs++))
1827 device_remove_file(f->dev, attr);
1828 }
1829 }
1830}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001831
1832static int android_init_functions(struct android_usb_function **functions,
1833 struct usb_composite_dev *cdev)
1834{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001835 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001836 struct android_usb_function *f;
1837 struct device_attribute **attrs;
1838 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301839 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001840 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001841
1842 for (; (f = *functions++); index++) {
1843 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001844 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001845 if (!f->dev_name) {
1846 err = -ENOMEM;
1847 goto err_out;
1848 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001849 f->dev = device_create(android_class, dev->dev,
1850 MKDEV(0, index), f, f->dev_name);
1851 if (IS_ERR(f->dev)) {
1852 pr_err("%s: Failed to create dev %s", __func__,
1853 f->dev_name);
1854 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001855 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001856 goto err_create;
1857 }
1858
1859 if (f->init) {
1860 err = f->init(f, cdev);
1861 if (err) {
1862 pr_err("%s: Failed to init %s", __func__,
1863 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001864 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001865 }
1866 }
1867
1868 attrs = f->attributes;
1869 if (attrs) {
1870 while ((attr = *attrs++) && !err)
1871 err = device_create_file(f->dev, attr);
1872 }
1873 if (err) {
1874 pr_err("%s: Failed to create function %s attributes",
1875 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001876 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001877 }
1878 }
1879 return 0;
1880
Lena Salmand092f2d2012-03-12 17:27:24 +02001881err_attrs:
1882 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1883 device_remove_file(f->dev, attr);
1884 if (f->cleanup)
1885 f->cleanup(f);
1886err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001887 device_destroy(android_class, f->dev->devt);
1888err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001889 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001890 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001891err_out:
1892 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001893 return err;
1894}
1895
Benoit Goby1e8ce152011-12-12 13:01:23 -08001896static int
1897android_bind_enabled_functions(struct android_dev *dev,
1898 struct usb_configuration *c)
1899{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001900 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001901 struct android_configuration *conf =
1902 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001903 int ret;
1904
Ido Shayevitz68557e32012-11-06 12:40:37 +02001905 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1906 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001907 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001908 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001909 return ret;
1910 }
1911 }
1912 return 0;
1913}
1914
1915static void
1916android_unbind_enabled_functions(struct android_dev *dev,
1917 struct usb_configuration *c)
1918{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001919 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001920 struct android_configuration *conf =
1921 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001922
Ido Shayevitz68557e32012-11-06 12:40:37 +02001923 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1924 if (f_holder->f->unbind_config)
1925 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001926 }
1927}
1928
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001929static int android_enable_function(struct android_dev *dev,
1930 struct android_configuration *conf,
1931 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001932{
1933 struct android_usb_function **functions = dev->functions;
1934 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001935 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001936 while ((f = *functions++)) {
1937 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001938 if (f->android_dev && f->android_dev != dev)
1939 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001940 f->name);
1941 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001942 f_holder = kzalloc(sizeof(*f_holder),
1943 GFP_KERNEL);
1944 if (!f_holder) {
1945 pr_err("Failed to alloc f_holder\n");
1946 return -ENOMEM;
1947 }
1948
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001949 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001950 f_holder->f = f;
1951 list_add_tail(&f_holder->enabled_list,
1952 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001953 return 0;
1954 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001955 }
1956 }
1957 return -EINVAL;
1958}
1959
1960/*-------------------------------------------------------------------------*/
1961/* /sys/class/android_usb/android%d/ interface */
1962
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301963static ssize_t remote_wakeup_show(struct device *pdev,
1964 struct device_attribute *attr, char *buf)
1965{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001966 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001967 struct android_configuration *conf;
1968
1969 /*
1970 * Show the wakeup attribute of the first configuration,
1971 * since all configurations have the same wakeup attribute
1972 */
1973 if (dev->configs_num == 0)
1974 return 0;
1975 conf = list_entry(dev->configs.next,
1976 struct android_configuration,
1977 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001978
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301979 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001980 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301981 USB_CONFIG_ATT_WAKEUP));
1982}
1983
1984static ssize_t remote_wakeup_store(struct device *pdev,
1985 struct device_attribute *attr, const char *buff, size_t size)
1986{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001987 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001988 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301989 int enable = 0;
1990
1991 sscanf(buff, "%d", &enable);
1992
1993 pr_debug("android_usb: %s remote wakeup\n",
1994 enable ? "enabling" : "disabling");
1995
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001996 list_for_each_entry(conf, &dev->configs, list_item)
1997 if (enable)
1998 conf->usb_config.bmAttributes |=
1999 USB_CONFIG_ATT_WAKEUP;
2000 else
2001 conf->usb_config.bmAttributes &=
2002 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302003
2004 return size;
2005}
2006
Benoit Goby1e8ce152011-12-12 13:01:23 -08002007static ssize_t
2008functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2009{
2010 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002011 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002012 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002013 char *buff = buf;
2014
2015 mutex_lock(&dev->mutex);
2016
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002017 list_for_each_entry(conf, &dev->configs, list_item) {
2018 if (buff != buf)
2019 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02002020 list_for_each_entry(f_holder, &conf->enabled_functions,
2021 enabled_list)
2022 buff += snprintf(buff, PAGE_SIZE, "%s,",
2023 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002024 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002025
2026 mutex_unlock(&dev->mutex);
2027
2028 if (buff != buf)
2029 *(buff-1) = '\n';
2030 return buff - buf;
2031}
2032
2033static ssize_t
2034functions_store(struct device *pdev, struct device_attribute *attr,
2035 const char *buff, size_t size)
2036{
2037 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002038 struct list_head *curr_conf = &dev->configs;
2039 struct android_configuration *conf;
2040 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002041 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002042 char *name;
2043 char buf[256], *b;
2044 int err;
2045
2046 mutex_lock(&dev->mutex);
2047
2048 if (dev->enabled) {
2049 mutex_unlock(&dev->mutex);
2050 return -EBUSY;
2051 }
2052
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002053 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002054 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002055 while (conf->enabled_functions.next !=
2056 &conf->enabled_functions) {
2057 f_holder = list_entry(conf->enabled_functions.next,
2058 typeof(*f_holder),
2059 enabled_list);
2060 f_holder->f->android_dev = NULL;
2061 list_del(&f_holder->enabled_list);
2062 kfree(f_holder);
2063 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002064 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002065 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002066
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302067 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002068 b = strim(buf);
2069
2070 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002071 conf_str = strsep(&b, ":");
2072 if (conf_str) {
2073 /* If the next not equal to the head, take it */
2074 if (curr_conf->next != &dev->configs)
2075 conf = list_entry(curr_conf->next,
2076 struct android_configuration,
2077 list_item);
2078 else
2079 conf = alloc_android_config(dev);
2080
2081 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002082 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002083
2084 while (conf_str) {
2085 name = strsep(&conf_str, ",");
2086 if (name) {
2087 err = android_enable_function(dev, conf, name);
2088 if (err)
2089 pr_err("android_usb: Cannot enable %s",
2090 name);
2091 }
2092 }
2093 }
2094
2095 /* Free uneeded configurations if exists */
2096 while (curr_conf->next != &dev->configs) {
2097 conf = list_entry(curr_conf->next,
2098 struct android_configuration, list_item);
2099 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002100 }
2101
2102 mutex_unlock(&dev->mutex);
2103
2104 return size;
2105}
2106
2107static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2108 char *buf)
2109{
2110 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002111
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302112 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002113}
2114
2115static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2116 const char *buff, size_t size)
2117{
2118 struct android_dev *dev = dev_get_drvdata(pdev);
2119 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002120 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002121 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002122 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302123 bool audio_enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302124 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002125
Stephen Boyd42517402013-01-14 16:41:42 -08002126
Benoit Gobycf3fc062011-12-19 14:39:37 -08002127 if (!cdev)
2128 return -ENODEV;
2129
Benoit Goby1e8ce152011-12-12 13:01:23 -08002130 mutex_lock(&dev->mutex);
2131
2132 sscanf(buff, "%d", &enabled);
2133 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002134 /*
2135 * Update values in composite driver's copy of
2136 * device descriptor.
2137 */
2138 cdev->desc.idVendor = device_desc.idVendor;
2139 cdev->desc.idProduct = device_desc.idProduct;
2140 cdev->desc.bcdDevice = device_desc.bcdDevice;
2141 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2142 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2143 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302144
2145 /* Audio dock accessory is unable to enumerate device if
2146 * pull-up is enabled immediately. The enumeration is
2147 * reliable with 100 msec delay.
2148 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002149 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002150 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002151 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002152 if (f_holder->f->enable)
2153 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302154 if (!strncmp(f_holder->f->name,
2155 "audio_source", 12))
2156 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002157 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302158 if (audio_enabled)
2159 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002160 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002161 dev->enabled = true;
2162 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002163 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002164 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002165 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002166 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002167 if (f_holder->f->disable)
2168 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002169 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002170 dev->enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302171 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002172 pr_err("android_usb: already %s\n",
2173 dev->enabled ? "enabled" : "disabled");
2174 }
2175
2176 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002177
Benoit Gobyaab96812011-04-19 20:37:33 -07002178 return size;
2179}
2180
Ofir Cohen94213a72012-05-03 14:26:32 +03002181static ssize_t pm_qos_show(struct device *pdev,
2182 struct device_attribute *attr, char *buf)
2183{
2184 struct android_dev *dev = dev_get_drvdata(pdev);
2185
2186 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2187}
2188
2189static ssize_t pm_qos_store(struct device *pdev,
2190 struct device_attribute *attr,
2191 const char *buff, size_t size)
2192{
2193 struct android_dev *dev = dev_get_drvdata(pdev);
2194
2195 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2196
Benoit Goby1e8ce152011-12-12 13:01:23 -08002197 return size;
2198}
2199
2200static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2201 char *buf)
2202{
2203 struct android_dev *dev = dev_get_drvdata(pdev);
2204 struct usb_composite_dev *cdev = dev->cdev;
2205 char *state = "DISCONNECTED";
2206 unsigned long flags;
2207
2208 if (!cdev)
2209 goto out;
2210
2211 spin_lock_irqsave(&cdev->lock, flags);
2212 if (cdev->config)
2213 state = "CONFIGURED";
2214 else if (dev->connected)
2215 state = "CONNECTED";
2216 spin_unlock_irqrestore(&cdev->lock, flags);
2217out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302218 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002219}
2220
2221#define DESCRIPTOR_ATTR(field, format_string) \
2222static ssize_t \
2223field ## _show(struct device *dev, struct device_attribute *attr, \
2224 char *buf) \
2225{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302226 return snprintf(buf, PAGE_SIZE, \
2227 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002228} \
2229static ssize_t \
2230field ## _store(struct device *dev, struct device_attribute *attr, \
2231 const char *buf, size_t size) \
2232{ \
2233 int value; \
2234 if (sscanf(buf, format_string, &value) == 1) { \
2235 device_desc.field = value; \
2236 return size; \
2237 } \
2238 return -1; \
2239} \
2240static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2241
2242#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2243static ssize_t \
2244field ## _show(struct device *dev, struct device_attribute *attr, \
2245 char *buf) \
2246{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302247 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002248} \
2249static ssize_t \
2250field ## _store(struct device *dev, struct device_attribute *attr, \
2251 const char *buf, size_t size) \
2252{ \
2253 if (size >= sizeof(buffer)) \
2254 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302255 strlcpy(buffer, buf, sizeof(buffer)); \
2256 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302257 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002258} \
2259static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2260
2261
2262DESCRIPTOR_ATTR(idVendor, "%04x\n")
2263DESCRIPTOR_ATTR(idProduct, "%04x\n")
2264DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2265DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2266DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2267DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2268DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2269DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2270DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2271
2272static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2273 functions_store);
2274static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002275static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2276 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002277static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302278static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2279 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002280
2281static struct device_attribute *android_usb_attributes[] = {
2282 &dev_attr_idVendor,
2283 &dev_attr_idProduct,
2284 &dev_attr_bcdDevice,
2285 &dev_attr_bDeviceClass,
2286 &dev_attr_bDeviceSubClass,
2287 &dev_attr_bDeviceProtocol,
2288 &dev_attr_iManufacturer,
2289 &dev_attr_iProduct,
2290 &dev_attr_iSerial,
2291 &dev_attr_functions,
2292 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002293 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002294 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302295 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002296 NULL
2297};
2298
2299/*-------------------------------------------------------------------------*/
2300/* Composite driver */
2301
2302static int android_bind_config(struct usb_configuration *c)
2303{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002304 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002305 int ret = 0;
2306
2307 ret = android_bind_enabled_functions(dev, c);
2308 if (ret)
2309 return ret;
2310
2311 return 0;
2312}
2313
2314static void android_unbind_config(struct usb_configuration *c)
2315{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002316 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002317
2318 android_unbind_enabled_functions(dev, c);
2319}
2320
2321static int android_bind(struct usb_composite_dev *cdev)
2322{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002323 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002324 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002325 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002326 int gcnum, id, ret;
2327
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002328 /* Bind to the last android_dev that was probed */
2329 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2330
2331 dev->cdev = cdev;
2332
Benoit Goby1e8ce152011-12-12 13:01:23 -08002333 /*
2334 * Start disconnected. Userspace will connect the gadget once
2335 * it is done configuring the functions.
2336 */
2337 usb_gadget_disconnect(gadget);
2338
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002339 /* Init the supported functions only once, on the first android_dev */
2340 if (android_dev_count == 1) {
2341 ret = android_init_functions(dev->functions, cdev);
2342 if (ret)
2343 return ret;
2344 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002345
2346 /* Allocate string descriptor numbers ... note that string
2347 * contents can be overridden by the composite_dev glue.
2348 */
2349 id = usb_string_id(cdev);
2350 if (id < 0)
2351 return id;
2352 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2353 device_desc.iManufacturer = id;
2354
2355 id = usb_string_id(cdev);
2356 if (id < 0)
2357 return id;
2358 strings_dev[STRING_PRODUCT_IDX].id = id;
2359 device_desc.iProduct = id;
2360
2361 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302362 strlcpy(manufacturer_string, "Android",
2363 sizeof(manufacturer_string) - 1);
2364 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2365 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002366
2367 id = usb_string_id(cdev);
2368 if (id < 0)
2369 return id;
2370 strings_dev[STRING_SERIAL_IDX].id = id;
2371 device_desc.iSerialNumber = id;
2372
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302373 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002374 list_for_each_entry(conf, &dev->configs, list_item)
2375 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302376
Benoit Goby1e8ce152011-12-12 13:01:23 -08002377 gcnum = usb_gadget_controller_number(gadget);
2378 if (gcnum >= 0)
2379 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2380 else {
2381 pr_warning("%s: controller '%s' not recognized\n",
2382 longname, gadget->name);
2383 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2384 }
2385
Benoit Goby1e8ce152011-12-12 13:01:23 -08002386 return 0;
2387}
2388
2389static int android_usb_unbind(struct usb_composite_dev *cdev)
2390{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002391 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002392
Lena Salmand092f2d2012-03-12 17:27:24 +02002393 manufacturer_string[0] = '\0';
2394 product_string[0] = '\0';
2395 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002396 cancel_work_sync(&dev->work);
2397 android_cleanup_functions(dev->functions);
2398 return 0;
2399}
2400
2401static struct usb_composite_driver android_usb_driver = {
2402 .name = "android_usb",
2403 .dev = &device_desc,
2404 .strings = dev_strings,
2405 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002406 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002407};
2408
2409static int
2410android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2411{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002412 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002413 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002414 struct usb_request *req = cdev->req;
2415 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002416 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002417 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002418 int value = -EOPNOTSUPP;
2419 unsigned long flags;
2420
2421 req->zero = 0;
2422 req->complete = composite_setup_complete;
2423 req->length = 0;
2424 gadget->ep0->driver_data = cdev;
2425
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002426 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002427 list_for_each_entry(f_holder,
2428 &conf->enabled_functions,
2429 enabled_list) {
2430 f = f_holder->f;
2431 if (f->ctrlrequest) {
2432 value = f->ctrlrequest(f, cdev, c);
2433 if (value >= 0)
2434 break;
2435 }
2436 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002437
Benoit Gobycf3fc062011-12-19 14:39:37 -08002438 /* Special case the accessory function.
2439 * It needs to handle control requests before it is enabled.
2440 */
2441 if (value < 0)
2442 value = acc_ctrlrequest(cdev, c);
2443
Benoit Goby1e8ce152011-12-12 13:01:23 -08002444 if (value < 0)
2445 value = composite_setup(gadget, c);
2446
2447 spin_lock_irqsave(&cdev->lock, flags);
2448 if (!dev->connected) {
2449 dev->connected = 1;
2450 schedule_work(&dev->work);
2451 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2452 cdev->config) {
2453 schedule_work(&dev->work);
2454 }
2455 spin_unlock_irqrestore(&cdev->lock, flags);
2456
2457 return value;
2458}
2459
2460static void android_disconnect(struct usb_gadget *gadget)
2461{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002462 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002463 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002464 unsigned long flags;
2465
2466 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302467 /* accessory HID support can be active while the
2468 accessory function is not actually enabled,
2469 so we need to inform it when we are disconnected.
2470 */
2471 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002472
2473 spin_lock_irqsave(&cdev->lock, flags);
2474 dev->connected = 0;
2475 schedule_work(&dev->work);
2476 spin_unlock_irqrestore(&cdev->lock, flags);
2477}
2478
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002479static void android_suspend(struct usb_gadget *gadget)
2480{
2481 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2482 struct android_dev *dev = cdev_to_android_dev(cdev);
2483 unsigned long flags;
2484
2485 spin_lock_irqsave(&cdev->lock, flags);
2486 dev->suspended = 1;
2487 schedule_work(&dev->work);
2488 spin_unlock_irqrestore(&cdev->lock, flags);
2489
2490 composite_suspend(gadget);
2491}
2492
2493static void android_resume(struct usb_gadget *gadget)
2494{
2495 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2496 struct android_dev *dev = cdev_to_android_dev(cdev);
2497 unsigned long flags;
2498
2499 spin_lock_irqsave(&cdev->lock, flags);
2500 dev->suspended = 0;
2501 schedule_work(&dev->work);
2502 spin_unlock_irqrestore(&cdev->lock, flags);
2503
2504 composite_resume(gadget);
2505}
2506
2507
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002508static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002509{
2510 struct device_attribute **attrs = android_usb_attributes;
2511 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002512 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002513 int err;
2514
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002515 /*
2516 * The primary usb core should always have usb_core_id=0, since
2517 * Android user space is currently interested in android0 events.
2518 */
2519 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2520 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002521 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002522 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002523 if (IS_ERR(dev->dev))
2524 return PTR_ERR(dev->dev);
2525
2526 dev_set_drvdata(dev->dev, dev);
2527
2528 while ((attr = *attrs++)) {
2529 err = device_create_file(dev->dev, attr);
2530 if (err) {
2531 device_destroy(android_class, dev->dev->devt);
2532 return err;
2533 }
2534 }
2535 return 0;
2536}
2537
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302538static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002539{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302540 struct device_attribute **attrs = android_usb_attributes;
2541 struct device_attribute *attr;
2542
2543 while ((attr = *attrs++))
2544 device_remove_file(dev->dev, attr);
2545 device_destroy(android_class, dev->dev->devt);
2546}
2547
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002548static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2549{
2550 struct android_dev *dev = NULL;
2551
2552 /* Find the android dev from the list */
2553 list_for_each_entry(dev, &android_dev_list, list_item) {
2554 if (dev->cdev == cdev)
2555 break;
2556 }
2557
2558 return dev;
2559}
2560
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002561static struct android_configuration *alloc_android_config
2562 (struct android_dev *dev)
2563{
2564 struct android_configuration *conf;
2565
2566 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2567 if (!conf) {
2568 pr_err("%s(): Failed to alloc memory for android conf\n",
2569 __func__);
2570 return ERR_PTR(-ENOMEM);
2571 }
2572
2573 dev->configs_num++;
2574 conf->usb_config.label = dev->name;
2575 conf->usb_config.unbind = android_unbind_config;
2576 conf->usb_config.bConfigurationValue = dev->configs_num;
2577
2578 INIT_LIST_HEAD(&conf->enabled_functions);
2579
2580 list_add_tail(&conf->list_item, &dev->configs);
2581
2582 return conf;
2583}
2584
2585static void free_android_config(struct android_dev *dev,
2586 struct android_configuration *conf)
2587{
2588 list_del(&conf->list_item);
2589 dev->configs_num--;
2590 kfree(conf);
2591}
2592
Manu Gautam43c61a12012-08-22 17:09:37 -07002593static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2594{
2595 struct dload_struct local_diag_dload = { 0 };
2596 int *src, *dst, i;
2597
2598 if (!diag_dload) {
2599 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2600 return -ENODEV;
2601 }
2602
2603 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2604 __func__, diag_dload, pid, snum);
2605
2606 /* update pid */
2607 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2608 local_diag_dload.pid = pid;
2609
2610 /* update serial number */
2611 if (!snum) {
2612 local_diag_dload.magic_struct.serial_num = 0;
2613 memset(&local_diag_dload.serial_number, 0,
2614 SERIAL_NUMBER_LENGTH);
2615 } else {
2616 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2617 strlcpy((char *)&local_diag_dload.serial_number, snum,
2618 SERIAL_NUMBER_LENGTH);
2619 }
2620
2621 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2622 src = (int *)&local_diag_dload;
2623 dst = (int *)diag_dload;
2624
2625 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2626 *dst++ = *src++;
2627
2628 return 0;
2629}
2630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002631static int __devinit android_probe(struct platform_device *pdev)
2632{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302633 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002634 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002635 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002636 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002637
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302638 if (pdev->dev.of_node) {
2639 dev_dbg(&pdev->dev, "device tree enabled\n");
2640 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2641 if (!pdata) {
2642 pr_err("unable to allocate platform data\n");
2643 return -ENOMEM;
2644 }
2645
2646 of_property_read_u32(pdev->dev.of_node,
2647 "qcom,android-usb-swfi-latency",
2648 &pdata->swfi_latency);
2649 } else {
2650 pdata = pdev->dev.platform_data;
2651 }
2652
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002653 if (!android_class) {
2654 android_class = class_create(THIS_MODULE, "android_usb");
2655 if (IS_ERR(android_class))
2656 return PTR_ERR(android_class);
2657 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002658
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002659 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2660 if (!android_dev) {
2661 pr_err("%s(): Failed to alloc memory for android_dev\n",
2662 __func__);
2663 ret = -ENOMEM;
2664 goto err_alloc;
2665 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002666
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002667 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002668 android_dev->disable_depth = 1;
2669 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002670 android_dev->configs_num = 0;
2671 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002672 INIT_WORK(&android_dev->work, android_work);
2673 mutex_init(&android_dev->mutex);
2674
2675 android_dev->pdata = pdata;
2676
2677 list_add_tail(&android_dev->list_item, &android_dev_list);
2678 android_dev_count++;
2679
2680 if (pdata)
2681 composite_driver.usb_core_id = pdata->usb_core_id;
2682 else
2683 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2684
Manu Gautam43c61a12012-08-22 17:09:37 -07002685 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2686 if (res) {
2687 diag_dload = devm_ioremap(&pdev->dev, res->start,
2688 resource_size(res));
2689 if (!diag_dload) {
2690 dev_err(&pdev->dev, "ioremap failed\n");
2691 ret = -ENOMEM;
2692 goto err_dev;
2693 }
2694 } else {
2695 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2696 }
2697
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002698 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302699 if (ret) {
2700 pr_err("%s(): android_create_device failed\n", __func__);
2701 goto err_dev;
2702 }
2703
Lena Salmand092f2d2012-03-12 17:27:24 +02002704 ret = usb_composite_probe(&android_usb_driver, android_bind);
2705 if (ret) {
2706 pr_err("%s(): Failed to register android "
2707 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302708 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002709 }
2710
Ofir Cohen94213a72012-05-03 14:26:32 +03002711 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302712 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002713 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002714 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002715 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002716
Lena Salmand092f2d2012-03-12 17:27:24 +02002717 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302718err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002719 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302720err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002721 list_del(&android_dev->list_item);
2722 android_dev_count--;
2723 kfree(android_dev);
2724err_alloc:
2725 if (list_empty(&android_dev_list)) {
2726 class_destroy(android_class);
2727 android_class = NULL;
2728 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302729 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002730}
2731
2732static int android_remove(struct platform_device *pdev)
2733{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002734 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002735 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002736 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302737
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002738 if (pdata)
2739 usb_core_id = pdata->usb_core_id;
2740
2741 /* Find the android dev from the list */
2742 list_for_each_entry(dev, &android_dev_list, list_item) {
2743 if (!dev->pdata)
2744 break; /*To backward compatibility*/
2745 if (dev->pdata->usb_core_id == usb_core_id)
2746 break;
2747 }
2748
2749 if (dev) {
2750 android_destroy_device(dev);
2751 if (pdata && pdata->swfi_latency)
2752 pm_qos_remove_request(&dev->pm_qos_req_dma);
2753 list_del(&dev->list_item);
2754 android_dev_count--;
2755 kfree(dev);
2756 }
2757
2758 if (list_empty(&android_dev_list)) {
2759 class_destroy(android_class);
2760 android_class = NULL;
2761 usb_composite_unregister(&android_usb_driver);
2762 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002764 return 0;
2765}
2766
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002767static const struct platform_device_id android_id_table[] __devinitconst = {
2768 {
2769 .name = "android_usb",
2770 },
2771 {
2772 .name = "android_usb_hsic",
2773 },
2774};
2775
Manu Gautam43c61a12012-08-22 17:09:37 -07002776static struct of_device_id usb_android_dt_match[] = {
2777 { .compatible = "qcom,android-usb",
2778 },
2779 {}
2780};
2781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07002783 .driver = {
2784 .name = "android_usb",
2785 .of_match_table = usb_android_dt_match,
2786 },
Lena Salmand092f2d2012-03-12 17:27:24 +02002787 .probe = android_probe,
2788 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002789 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002790};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002791
2792static int __init init(void)
2793{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302794 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002795
Benoit Goby1e8ce152011-12-12 13:01:23 -08002796 /* Override composite driver functions */
2797 composite_driver.setup = android_setup;
2798 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002799 composite_driver.suspend = android_suspend;
2800 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002801
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002802 INIT_LIST_HEAD(&android_dev_list);
2803 android_dev_count = 0;
2804
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302805 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302806 if (ret) {
2807 pr_err("%s(): Failed to register android"
2808 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302809 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002810
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302811 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002812}
2813module_init(init);
2814
2815static void __exit cleanup(void)
2816{
Lena Salmand092f2d2012-03-12 17:27:24 +02002817 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002818}
2819module_exit(cleanup);