blob: 36a43c34c8fef0a424316b7c0b74881da902b186 [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"
Bar Weinered1059e2013-04-16 11:23:55 +030056#include "f_gps.c"
Anna Perel3ee23dd2013-02-26 16:06:40 +020057#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +053058#include "f_audio_source.c"
Anna Perel3ee23dd2013-02-26 16:06:40 +020059#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080060#include "f_mass_storage.c"
61#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "u_sdio.c"
63#include "u_smd.c"
64#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053065#include "u_rmnet_ctrl_smd.c"
Bar Weiner0dae81b2013-02-14 13:53:54 +020066#include "u_rmnet_ctrl_qti.c"
Jack Pham427f6922011-11-23 19:42:00 -080067#include "u_ctrl_hsic.c"
68#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053069#include "u_ctrl_hsuart.c"
70#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053071#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080072#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080073#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053074#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080075#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080076#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080077#define USB_ETH_RNDIS y
78#include "f_rndis.c"
79#include "rndis.c"
Amit Blayf9b352b2013-03-04 15:01:40 +020080#include "f_qc_ecm.c"
Anna Perela8c991d2012-04-09 16:44:46 +030081#include "f_mbim.c"
Lena Salmandf7e7992013-03-15 09:46:27 +020082#include "u_bam_data.c"
Anna Perelf9d01552012-11-20 15:56:32 +020083#include "f_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020084#include "f_qc_rndis.c"
Jack Pham0ad82e62012-09-27 17:31:08 -070085#include "u_ether.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030086#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053087#ifdef CONFIG_TARGET_CORE
88#include "f_tcm.c"
89#endif
Jack Pham2ec5fdc2012-09-26 10:13:48 -070090#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +030091#include "u_uac1.c"
92#include "f_uac1.c"
Jack Pham2ec5fdc2012-09-26 10:13:48 -070093#endif
Vamsi Krishna932c9de2013-05-22 12:18:05 -070094#include "f_ncm.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080095
96MODULE_AUTHOR("Mike Lockwood");
97MODULE_DESCRIPTION("Android Composite USB Driver");
98MODULE_LICENSE("GPL");
99MODULE_VERSION("1.0");
100
101static const char longname[] = "Gadget Android";
102
103/* Default vendor and product IDs, overridden by userspace */
104#define VENDOR_ID 0x18D1
105#define PRODUCT_ID 0x0001
106
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300107#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
108
Benoit Goby1e8ce152011-12-12 13:01:23 -0800109struct android_usb_function {
110 char *name;
111 void *config;
112
113 struct device *dev;
114 char *dev_name;
115 struct device_attribute **attributes;
116
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300117 struct android_dev *android_dev;
118
Benoit Goby1e8ce152011-12-12 13:01:23 -0800119 /* Optional: initialization during gadget bind */
120 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
121 /* Optional: cleanup during gadget unbind */
122 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700123 /* Optional: called when the function is added the list of
124 * enabled functions */
125 void (*enable)(struct android_usb_function *);
126 /* Optional: called when it is removed */
127 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800128
129 int (*bind_config)(struct android_usb_function *,
130 struct usb_configuration *);
131
132 /* Optional: called when the configuration is removed */
133 void (*unbind_config)(struct android_usb_function *,
134 struct usb_configuration *);
135 /* Optional: handle ctrl requests before the device is configured */
136 int (*ctrlrequest)(struct android_usb_function *,
137 struct usb_composite_dev *,
138 const struct usb_ctrlrequest *);
139};
140
Ido Shayevitz68557e32012-11-06 12:40:37 +0200141struct android_usb_function_holder {
142
143 struct android_usb_function *f;
144
145 /* for android_conf.enabled_functions */
146 struct list_head enabled_list;
147};
148
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200149/**
150* struct android_dev - represents android USB gadget device
151* @name: device name.
152* @functions: an array of all the supported USB function
153* drivers that this gadget support but not necessarily
154* added to one of the gadget configurations.
155* @cdev: The internal composite device. Android gadget device
156* is a composite device, such that it can support configurations
157* with more than one function driver.
158* @dev: The kernel device that represents this android device.
159* @enabled: True if the android gadget is enabled, means all
160* the configurations were set and all function drivers were
161* bind and ready for USB enumeration.
162* @disable_depth: Number of times the device was disabled, after
163* symmetrical number of enables the device willl be enabled.
164* Used for controlling ADB userspace disable/enable requests.
165* @mutex: Internal mutex for protecting device member fields.
166* @pdata: Platform data fetched from the kernel device platfrom data.
167* @connected: True if got connect notification from the gadget UDC.
168* False if got disconnect notification from the gadget UDC.
169* @sw_connected: Equal to 'connected' only after the connect
170* notification was handled by the android gadget work function.
171* @suspended: True if got suspend notification from the gadget UDC.
172* False if got resume notification from the gadget UDC.
173* @sw_suspended: Equal to 'suspended' only after the susped
174* notification was handled by the android gadget work function.
175* @pm_qos: An attribute string that can be set by user space in order to
176* determine pm_qos policy. Set to 'high' for always demand pm_qos
177* when USB bus is connected and resumed. Set to 'low' for disable
178* any setting of pm_qos by this driver. Default = 'high'.
179* @work: workqueue used for handling notifications from the gadget UDC.
180* @configs: List of configurations currently configured into the device.
181* The android gadget supports more than one configuration. The host
182* may choose one configuration from the suggested.
183* @configs_num: Number of configurations currently configured and existing
184* in the configs list.
185* @list_item: This driver supports more than one android gadget device (for
186* example in order to support multiple USB cores), therefore this is
187* a item in a linked list of android devices.
188*/
Benoit Goby1e8ce152011-12-12 13:01:23 -0800189struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300190 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800191 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800192 struct usb_composite_dev *cdev;
193 struct device *dev;
194
195 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700196 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800197 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700198 struct android_usb_platform_data *pdata;
199
Benoit Goby1e8ce152011-12-12 13:01:23 -0800200 bool connected;
201 bool sw_connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200202 bool suspended;
203 bool sw_suspended;
Ofir Cohen94213a72012-05-03 14:26:32 +0300204 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700205 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800206 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300207
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300208 /* A list of struct android_configuration */
209 struct list_head configs;
210 int configs_num;
211
212 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300213 struct list_head list_item;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300214};
215
216struct android_configuration {
217 struct usb_configuration usb_config;
218
219 /* A list of the functions supported by this config */
220 struct list_head enabled_functions;
221
222 /* A list node inside the struct android_dev.configs list */
223 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800224};
225
Manu Gautam43c61a12012-08-22 17:09:37 -0700226struct dload_struct __iomem *diag_dload;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800227static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300228static struct list_head android_dev_list;
229static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800230static int android_bind_config(struct usb_configuration *c);
231static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300232static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300233static struct android_configuration *alloc_android_config
234 (struct android_dev *dev);
235static void free_android_config(struct android_dev *dev,
236 struct android_configuration *conf);
Manu Gautam43c61a12012-08-22 17:09:37 -0700237static int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800238
239/* string IDs are assigned dynamically */
240#define STRING_MANUFACTURER_IDX 0
241#define STRING_PRODUCT_IDX 1
242#define STRING_SERIAL_IDX 2
243
244static char manufacturer_string[256];
245static char product_string[256];
246static char serial_string[256];
247
248/* String Table */
249static struct usb_string strings_dev[] = {
250 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
251 [STRING_PRODUCT_IDX].s = product_string,
252 [STRING_SERIAL_IDX].s = serial_string,
253 { } /* end of list */
254};
255
256static struct usb_gadget_strings stringtab_dev = {
257 .language = 0x0409, /* en-us */
258 .strings = strings_dev,
259};
260
261static struct usb_gadget_strings *dev_strings[] = {
262 &stringtab_dev,
263 NULL,
264};
265
266static struct usb_device_descriptor device_desc = {
267 .bLength = sizeof(device_desc),
268 .bDescriptorType = USB_DT_DEVICE,
269 .bcdUSB = __constant_cpu_to_le16(0x0200),
270 .bDeviceClass = USB_CLASS_PER_INTERFACE,
271 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
272 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
273 .bcdDevice = __constant_cpu_to_le16(0xffff),
274 .bNumConfigurations = 1,
275};
276
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530277static struct usb_otg_descriptor otg_descriptor = {
278 .bLength = sizeof otg_descriptor,
279 .bDescriptorType = USB_DT_OTG,
280 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
281 .bcdOTG = __constant_cpu_to_le16(0x0200),
282};
283
284static const struct usb_descriptor_header *otg_desc[] = {
285 (struct usb_descriptor_header *) &otg_descriptor,
286 NULL,
287};
288
Manu Gautama2b54142012-04-03 14:34:32 +0530289enum android_device_state {
290 USB_DISCONNECTED,
291 USB_CONNECTED,
292 USB_CONFIGURED,
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200293 USB_SUSPENDED,
294 USB_RESUMED
Manu Gautama2b54142012-04-03 14:34:32 +0530295};
296
Ofir Cohen94213a72012-05-03 14:26:32 +0300297static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
298{
299 struct android_usb_platform_data *pdata = dev->pdata;
300 u32 swfi_latency = 0;
301 static int last_vote = -1;
302
Ofir Cohen56eb7072012-05-20 11:41:39 +0300303 if (!pdata || vote == last_vote
304 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300305 return;
306
307 swfi_latency = pdata->swfi_latency + 1;
308 if (vote)
309 pm_qos_update_request(&dev->pm_qos_req_dma,
310 swfi_latency);
311 else
312 pm_qos_update_request(&dev->pm_qos_req_dma,
313 PM_QOS_DEFAULT_VALUE);
314 last_vote = vote;
315}
316
Benoit Goby1e8ce152011-12-12 13:01:23 -0800317static void android_work(struct work_struct *data)
318{
319 struct android_dev *dev = container_of(data, struct android_dev, work);
320 struct usb_composite_dev *cdev = dev->cdev;
321 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
322 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
323 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200324 char *suspended[2] = { "USB_STATE=SUSPENDED", NULL };
325 char *resumed[2] = { "USB_STATE=RESUMED", NULL };
Benoit Goby1e8ce152011-12-12 13:01:23 -0800326 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530327 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800328 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300329 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800330
331 spin_lock_irqsave(&cdev->lock, flags);
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200332 if (dev->suspended != dev->sw_suspended && cdev->config) {
333 if (strncmp(dev->pm_qos, "low", 3))
334 pm_qos_vote = dev->suspended ? 0 : 1;
335 next_state = dev->suspended ? USB_SUSPENDED : USB_RESUMED;
336 uevent_envp = dev->suspended ? suspended : resumed;
337 } else if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800338 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530339 next_state = USB_CONFIGURED;
340 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800341 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530342 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300343 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300344 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300345 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300346 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530347 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800348 dev->sw_connected = dev->connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200349 dev->sw_suspended = dev->suspended;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800350 spin_unlock_irqrestore(&cdev->lock, flags);
351
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300352 if (pm_qos_vote != -1)
353 android_pm_qos_update_latency(dev, pm_qos_vote);
354
Benoit Goby1e8ce152011-12-12 13:01:23 -0800355 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530356 /*
357 * Some userspace modules, e.g. MTP, work correctly only if
358 * CONFIGURED uevent is preceded by DISCONNECT uevent.
359 * Check if we missed sending out a DISCONNECT uevent. This can
360 * happen if host PC resets and configures device really quick.
361 */
362 if (((uevent_envp == connected) &&
363 (last_uevent != USB_DISCONNECTED)) ||
364 ((uevent_envp == configured) &&
365 (last_uevent == USB_CONFIGURED))) {
366 pr_info("%s: sent missed DISCONNECT event\n", __func__);
367 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
368 disconnected);
369 msleep(20);
370 }
371 /*
372 * Before sending out CONFIGURED uevent give function drivers
373 * a chance to wakeup userspace threads and notify disconnect
374 */
375 if (uevent_envp == configured)
376 msleep(50);
377
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200378 /* Do not notify on suspend / resume */
379 if (next_state != USB_SUSPENDED && next_state != USB_RESUMED) {
380 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
381 uevent_envp);
382 last_uevent = next_state;
383 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800384 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
385 } else {
386 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
387 dev->connected, dev->sw_connected, cdev->config);
388 }
389}
390
Benoit Goby80ba14d2012-03-19 18:56:52 -0700391static void android_enable(struct android_dev *dev)
392{
393 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300394 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700395
396 if (WARN_ON(!dev->disable_depth))
397 return;
398
399 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300400
401 list_for_each_entry(conf, &dev->configs, list_item)
402 usb_add_config(cdev, &conf->usb_config,
403 android_bind_config);
404
Benoit Goby80ba14d2012-03-19 18:56:52 -0700405 usb_gadget_connect(cdev->gadget);
406 }
407}
408
409static void android_disable(struct android_dev *dev)
410{
411 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300412 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700413
414 if (dev->disable_depth++ == 0) {
415 usb_gadget_disconnect(cdev->gadget);
416 /* Cancel pending control requests */
417 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300418
419 list_for_each_entry(conf, &dev->configs, list_item)
420 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700421 }
422}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800423
424/*-------------------------------------------------------------------------*/
425/* Supported functions initialization */
426
Benoit Goby80ba14d2012-03-19 18:56:52 -0700427struct adb_data {
428 bool opened;
429 bool enabled;
Amit Blay637e5b22013-05-07 17:19:09 +0300430 struct android_dev *dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700431};
432
Benoit Goby2b6862d2011-12-19 14:38:41 -0800433static int
434adb_function_init(struct android_usb_function *f,
435 struct usb_composite_dev *cdev)
436{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700437 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
438 if (!f->config)
439 return -ENOMEM;
440
Benoit Goby2b6862d2011-12-19 14:38:41 -0800441 return adb_setup();
442}
443
444static void adb_function_cleanup(struct android_usb_function *f)
445{
446 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700447 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800448}
449
450static int
451adb_function_bind_config(struct android_usb_function *f,
452 struct usb_configuration *c)
453{
454 return adb_bind_config(c);
455}
456
Benoit Goby80ba14d2012-03-19 18:56:52 -0700457static void adb_android_function_enable(struct android_usb_function *f)
458{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300459 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700460 struct adb_data *data = f->config;
461
462 data->enabled = true;
463
Amit Blay637e5b22013-05-07 17:19:09 +0300464
Benoit Goby80ba14d2012-03-19 18:56:52 -0700465 /* Disable the gadget until adbd is ready */
466 if (!data->opened)
467 android_disable(dev);
468}
469
470static void adb_android_function_disable(struct android_usb_function *f)
471{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300472 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700473 struct adb_data *data = f->config;
474
475 data->enabled = false;
476
477 /* Balance the disable that was called in closed_callback */
478 if (!data->opened)
479 android_enable(dev);
480}
481
Benoit Goby2b6862d2011-12-19 14:38:41 -0800482static struct android_usb_function adb_function = {
483 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700484 .enable = adb_android_function_enable,
485 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800486 .init = adb_function_init,
487 .cleanup = adb_function_cleanup,
488 .bind_config = adb_function_bind_config,
489};
490
Benoit Goby80ba14d2012-03-19 18:56:52 -0700491static void adb_ready_callback(void)
492{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300493 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700494 struct adb_data *data = adb_function.config;
495
Amit Blay637e5b22013-05-07 17:19:09 +0300496 /* dev is null in case ADB is not in the composition */
497 if (dev)
498 mutex_lock(&dev->mutex);
499
500 /* Save dev in case the adb function will get disabled */
501 data->dev = dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700502 data->opened = true;
503
Amit Blay637e5b22013-05-07 17:19:09 +0300504 if (data->enabled && dev)
Benoit Goby80ba14d2012-03-19 18:56:52 -0700505 android_enable(dev);
Amit Blay637e5b22013-05-07 17:19:09 +0300506
507 if (dev)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300508 mutex_unlock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700509}
510
511static void adb_closed_callback(void)
512{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700513 struct adb_data *data = adb_function.config;
Amit Blay637e5b22013-05-07 17:19:09 +0300514 struct android_dev *dev = adb_function.android_dev;
515
516 /* In case new composition is without ADB, use saved one */
517 if (!dev)
518 dev = data->dev;
519
520 if (!dev)
521 pr_err("adb_closed_callback: data->dev is NULL");
522
523 if (dev)
524 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700525
Benoit Goby80ba14d2012-03-19 18:56:52 -0700526 data->opened = false;
527
Pavankumar Kondetidc1f67e2013-05-28 12:10:24 +0530528 if (data->enabled && dev)
Benoit Goby80ba14d2012-03-19 18:56:52 -0700529 android_disable(dev);
Amit Blay637e5b22013-05-07 17:19:09 +0300530
531 data->dev = NULL;
532
533 if (dev)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300534 mutex_unlock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700535}
536
Benoit Goby2b6862d2011-12-19 14:38:41 -0800537
Benoit Gobyaab96812011-04-19 20:37:33 -0700538/*-------------------------------------------------------------------------*/
539/* Supported functions initialization */
540
Stephen Boyd83ed8e22013-03-08 17:04:08 -0800541/* ACM */
542static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
543static ssize_t acm_transports_store(
544 struct device *device, struct device_attribute *attr,
545 const char *buff, size_t size)
546{
547 strlcpy(acm_transports, buff, sizeof(acm_transports));
548
549 return size;
550}
551
552static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
553static struct device_attribute *acm_function_attributes[] = {
554 &dev_attr_acm_transports,
555 NULL
556};
557
558static void acm_function_cleanup(struct android_usb_function *f)
559{
560 gserial_cleanup();
561}
562
563static int
564acm_function_bind_config(struct android_usb_function *f,
565 struct usb_configuration *c)
566{
567 char *name;
568 char buf[32], *b;
569 int err = -1, i;
570 static int acm_initialized, ports;
571
572 if (acm_initialized)
573 goto bind_config;
574
575 acm_initialized = 1;
576 strlcpy(buf, acm_transports, sizeof(buf));
577 b = strim(buf);
578
579 while (b) {
580 name = strsep(&b, ",");
581
582 if (name) {
583 err = acm_init_port(ports, name);
584 if (err) {
585 pr_err("acm: Cannot open port '%s'", name);
586 goto out;
587 }
588 ports++;
589 }
590 }
591 err = acm_port_setup(c);
592 if (err) {
593 pr_err("acm: Cannot setup transports");
594 goto out;
595 }
596
597bind_config:
598 for (i = 0; i < ports; i++) {
599 err = acm_bind_config(c, i);
600 if (err) {
601 pr_err("acm: bind_config failed for port %d", i);
602 goto out;
603 }
604 }
605
606out:
607 return err;
608}
609
610static struct android_usb_function acm_function = {
611 .name = "acm",
612 .cleanup = acm_function_cleanup,
613 .bind_config = acm_function_bind_config,
614 .attributes = acm_function_attributes,
615};
616
Manu Gautam8e0719b2011-09-26 14:47:55 +0530617/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530618static int rmnet_smd_function_bind_config(struct android_usb_function *f,
619 struct usb_configuration *c)
620{
621 return rmnet_smd_bind_config(c);
622}
623
624static struct android_usb_function rmnet_smd_function = {
625 .name = "rmnet_smd",
626 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800627};
628
Manu Gautam8e0719b2011-09-26 14:47:55 +0530629/* RMNET_SDIO */
630static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
631 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800632{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530633 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800634}
635
Manu Gautam8e0719b2011-09-26 14:47:55 +0530636static struct android_usb_function rmnet_sdio_function = {
637 .name = "rmnet_sdio",
638 .bind_config = rmnet_sdio_function_bind_config,
639};
640
641/* RMNET_SMD_SDIO */
642static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
643 struct usb_composite_dev *cdev)
644{
645 return rmnet_smd_sdio_init();
646}
647
648static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
649{
650 rmnet_smd_sdio_cleanup();
651}
652
653static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
654 struct usb_configuration *c)
655{
656 return rmnet_smd_sdio_function_add(c);
657}
658
659static struct device_attribute *rmnet_smd_sdio_attributes[] = {
660 &dev_attr_transport, NULL };
661
662static struct android_usb_function rmnet_smd_sdio_function = {
663 .name = "rmnet_smd_sdio",
664 .init = rmnet_smd_sdio_function_init,
665 .cleanup = rmnet_smd_sdio_function_cleanup,
666 .bind_config = rmnet_smd_sdio_bind_config,
667 .attributes = rmnet_smd_sdio_attributes,
668};
669
Hemant Kumar1b820d52011-11-03 15:08:28 -0700670/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
671#define MAX_XPORT_STR_LEN 50
672static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530673
Manu Gautame3e897c2011-09-12 17:18:46 +0530674static void rmnet_function_cleanup(struct android_usb_function *f)
675{
676 frmnet_cleanup();
677}
678
Manu Gautam2b0234a2011-09-07 16:47:52 +0530679static int rmnet_function_bind_config(struct android_usb_function *f,
680 struct usb_configuration *c)
681{
682 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700683 int err = 0;
684 char *ctrl_name;
685 char *data_name;
686 char buf[MAX_XPORT_STR_LEN], *b;
687 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530688
Hemant Kumar1b820d52011-11-03 15:08:28 -0700689 if (!rmnet_initialized) {
690 rmnet_initialized = 1;
691 strlcpy(buf, rmnet_transports, sizeof(buf));
692 b = strim(buf);
693 while (b) {
694 ctrl_name = strsep(&b, ",");
695 data_name = strsep(&b, ",");
696 if (ctrl_name && data_name) {
697 err = frmnet_init_port(ctrl_name, data_name);
698 if (err) {
699 pr_err("rmnet: Cannot open ctrl port:"
700 "'%s' data port:'%s'\n",
701 ctrl_name, data_name);
702 goto out;
703 }
704 ports++;
705 }
706 }
707
708 err = rmnet_gport_setup();
709 if (err) {
710 pr_err("rmnet: Cannot setup transports");
711 goto out;
712 }
713 }
714
715 for (i = 0; i < ports; i++) {
716 err = frmnet_bind_config(c, i);
717 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530718 pr_err("Could not bind rmnet%u config\n", i);
719 break;
720 }
721 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700722out:
723 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530724}
725
Hemant Kumar1b820d52011-11-03 15:08:28 -0700726static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530727 struct device_attribute *attr, char *buf)
728{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700729 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530730}
731
Hemant Kumar1b820d52011-11-03 15:08:28 -0700732static ssize_t rmnet_transports_store(
733 struct device *device, struct device_attribute *attr,
734 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530735{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700736 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530737
Manu Gautam2b0234a2011-09-07 16:47:52 +0530738 return size;
739}
740
Hemant Kumar1b820d52011-11-03 15:08:28 -0700741static struct device_attribute dev_attr_rmnet_transports =
742 __ATTR(transports, S_IRUGO | S_IWUSR,
743 rmnet_transports_show,
744 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530745static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700746 &dev_attr_rmnet_transports,
747 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530748
749static struct android_usb_function rmnet_function = {
750 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530751 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530752 .bind_config = rmnet_function_bind_config,
753 .attributes = rmnet_function_attributes,
754};
755
Bar Weinered1059e2013-04-16 11:23:55 +0300756static void gps_function_cleanup(struct android_usb_function *f)
757{
758 gps_cleanup();
759}
760
761static int gps_function_bind_config(struct android_usb_function *f,
762 struct usb_configuration *c)
763{
764 int err;
765 static int gps_initialized;
766
767 if (!gps_initialized) {
768 gps_initialized = 1;
769 err = gps_init_port();
770 if (err) {
771 pr_err("gps: Cannot init gps port");
772 return err;
773 }
774 }
775
776 err = gps_gport_setup();
777 if (err) {
778 pr_err("gps: Cannot setup transports");
779 return err;
780 }
781 err = gps_bind_config(c);
782 if (err) {
783 pr_err("Could not bind gps config\n");
784 return err;
785 }
786
787 return 0;
788}
789
790static struct android_usb_function gps_function = {
791 .name = "gps",
792 .cleanup = gps_function_cleanup,
793 .bind_config = gps_function_bind_config,
794};
795
Vamsi Krishna932c9de2013-05-22 12:18:05 -0700796/* ncm */
797struct ncm_function_config {
798 u8 ethaddr[ETH_ALEN];
799};
800static int
801ncm_function_init(struct android_usb_function *f, struct usb_composite_dev *c)
802{
803 f->config = kzalloc(sizeof(struct ncm_function_config), GFP_KERNEL);
804 if (!f->config)
805 return -ENOMEM;
Bar Weinered1059e2013-04-16 11:23:55 +0300806
Vamsi Krishna932c9de2013-05-22 12:18:05 -0700807 return 0;
808}
809
810static void ncm_function_cleanup(struct android_usb_function *f)
811{
812 kfree(f->config);
813 f->config = NULL;
814}
815
816static int
817ncm_function_bind_config(struct android_usb_function *f,
818 struct usb_configuration *c)
819{
820 struct ncm_function_config *ncm = f->config;
821 int ret;
822
823 if (!ncm) {
824 pr_err("%s: ncm config is null\n", __func__);
825 return -EINVAL;
826 }
827
828 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
829 ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
830 ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
831
832 ret = gether_setup_name(c->cdev->gadget, ncm->ethaddr, "ncm");
833 if (ret) {
834 pr_err("%s: gether setup failed err:%d\n", __func__, ret);
835 return ret;
836 }
837
838 ret = ncm_bind_config(c, ncm->ethaddr);
839 if (ret) {
840 pr_err("%s: ncm bind config failed err:%d", __func__, ret);
841 gether_cleanup();
842 return ret;
843 }
844
845 return ret;
846}
847
848static void ncm_function_unbind_config(struct android_usb_function *f,
849 struct usb_configuration *c)
850{
851 gether_cleanup();
852}
853
854static ssize_t ncm_ethaddr_show(struct device *dev,
855 struct device_attribute *attr, char *buf)
856{
857 struct android_usb_function *f = dev_get_drvdata(dev);
858 struct ncm_function_config *ncm = f->config;
859 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
860 ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
861 ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
862}
863
864static ssize_t ncm_ethaddr_store(struct device *dev,
865 struct device_attribute *attr, const char *buf, size_t size)
866{
867 struct android_usb_function *f = dev_get_drvdata(dev);
868 struct ncm_function_config *ncm = f->config;
869
870 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
871 (int *)&ncm->ethaddr[0], (int *)&ncm->ethaddr[1],
872 (int *)&ncm->ethaddr[2], (int *)&ncm->ethaddr[3],
873 (int *)&ncm->ethaddr[4], (int *)&ncm->ethaddr[5]) == 6)
874 return size;
875 return -EINVAL;
876}
877
878static DEVICE_ATTR(ncm_ethaddr, S_IRUGO | S_IWUSR, ncm_ethaddr_show,
879 ncm_ethaddr_store);
880static struct device_attribute *ncm_function_attributes[] = {
881 &dev_attr_ncm_ethaddr,
882 NULL
883};
884
885static struct android_usb_function ncm_function = {
886 .name = "ncm",
887 .init = ncm_function_init,
888 .cleanup = ncm_function_cleanup,
889 .bind_config = ncm_function_bind_config,
890 .unbind_config = ncm_function_unbind_config,
891 .attributes = ncm_function_attributes,
892};
Amit Blayf9b352b2013-03-04 15:01:40 +0200893/* ecm transport string */
894static char ecm_transports[MAX_XPORT_STR_LEN];
895
Ofir Cohen7b155422012-07-31 13:02:49 +0300896struct ecm_function_config {
897 u8 ethaddr[ETH_ALEN];
898};
899
900static int ecm_function_init(struct android_usb_function *f,
901 struct usb_composite_dev *cdev)
902{
903 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
904 if (!f->config)
905 return -ENOMEM;
906 return 0;
907}
908
909static void ecm_function_cleanup(struct android_usb_function *f)
910{
911 kfree(f->config);
912 f->config = NULL;
913}
914
915static int ecm_qc_function_bind_config(struct android_usb_function *f,
916 struct usb_configuration *c)
917{
918 int ret;
Amit Blayf9b352b2013-03-04 15:01:40 +0200919 char *trans;
Ofir Cohen7b155422012-07-31 13:02:49 +0300920 struct ecm_function_config *ecm = f->config;
921
922 if (!ecm) {
923 pr_err("%s: ecm_pdata\n", __func__);
924 return -EINVAL;
925 }
926
927 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
928 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
929 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
930
Amit Blayf9b352b2013-03-04 15:01:40 +0200931 pr_debug("%s: ecm_transport is %s", __func__, ecm_transports);
932
933 trans = strim(ecm_transports);
934 if (strcmp("BAM2BAM_IPA", trans)) {
935 ret = gether_qc_setup_name(c->cdev->gadget,
936 ecm->ethaddr, "ecm");
937 if (ret) {
938 pr_err("%s: gether_setup failed\n", __func__);
939 return ret;
940 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300941 }
942
Amit Blayf9b352b2013-03-04 15:01:40 +0200943 return ecm_qc_bind_config(c, ecm->ethaddr, trans);
Ofir Cohen7b155422012-07-31 13:02:49 +0300944}
945
946static void ecm_qc_function_unbind_config(struct android_usb_function *f,
947 struct usb_configuration *c)
948{
Amit Blayf9b352b2013-03-04 15:01:40 +0200949 char *trans = strim(ecm_transports);
950
951 if (strcmp("BAM2BAM_IPA", trans))
952 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300953}
954
955static ssize_t ecm_ethaddr_show(struct device *dev,
956 struct device_attribute *attr, char *buf)
957{
958 struct android_usb_function *f = dev_get_drvdata(dev);
959 struct ecm_function_config *ecm = f->config;
960 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
961 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
962 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
963}
964
965static ssize_t ecm_ethaddr_store(struct device *dev,
966 struct device_attribute *attr, const char *buf, size_t size)
967{
968 struct android_usb_function *f = dev_get_drvdata(dev);
969 struct ecm_function_config *ecm = f->config;
970
971 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
972 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
973 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
974 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
975 return size;
976 return -EINVAL;
977}
978
979static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
980 ecm_ethaddr_store);
981
Amit Blayf9b352b2013-03-04 15:01:40 +0200982static ssize_t ecm_transports_show(struct device *dev,
983 struct device_attribute *attr, char *buf)
984{
985 return snprintf(buf, PAGE_SIZE, "%s\n", ecm_transports);
986}
987
988static ssize_t ecm_transports_store(struct device *dev,
989 struct device_attribute *attr, const char *buf, size_t size)
990{
991 strlcpy(ecm_transports, buf, sizeof(ecm_transports));
992 return size;
993}
994
995static DEVICE_ATTR(ecm_transports, S_IRUGO | S_IWUSR, ecm_transports_show,
996 ecm_transports_store);
997
Ofir Cohen7b155422012-07-31 13:02:49 +0300998static struct device_attribute *ecm_function_attributes[] = {
Amit Blayf9b352b2013-03-04 15:01:40 +0200999 &dev_attr_ecm_transports,
Ofir Cohen7b155422012-07-31 13:02:49 +03001000 &dev_attr_ecm_ethaddr,
1001 NULL
1002};
1003
1004static struct android_usb_function ecm_qc_function = {
1005 .name = "ecm_qc",
1006 .init = ecm_function_init,
1007 .cleanup = ecm_function_cleanup,
1008 .bind_config = ecm_qc_function_bind_config,
1009 .unbind_config = ecm_qc_function_unbind_config,
1010 .attributes = ecm_function_attributes,
1011};
Anna Perela8c991d2012-04-09 16:44:46 +03001012
1013/* MBIM - used with BAM */
1014#define MAX_MBIM_INSTANCES 1
1015
1016static int mbim_function_init(struct android_usb_function *f,
1017 struct usb_composite_dev *cdev)
1018{
1019 return mbim_init(MAX_MBIM_INSTANCES);
1020}
1021
1022static void mbim_function_cleanup(struct android_usb_function *f)
1023{
1024 fmbim_cleanup();
1025}
1026
Lena Salmandf7e7992013-03-15 09:46:27 +02001027
1028/* mbim transport string */
1029static char mbim_transports[MAX_XPORT_STR_LEN];
1030
Anna Perela8c991d2012-04-09 16:44:46 +03001031static int mbim_function_bind_config(struct android_usb_function *f,
1032 struct usb_configuration *c)
1033{
Lena Salmandf7e7992013-03-15 09:46:27 +02001034 char *trans;
1035
1036 pr_debug("%s: mbim transport is %s", __func__, mbim_transports);
1037 trans = strim(mbim_transports);
1038 return mbim_bind_config(c, 0, trans);
Anna Perela8c991d2012-04-09 16:44:46 +03001039}
1040
Jack Pham2df2f702012-10-11 19:08:24 -07001041static int mbim_function_ctrlrequest(struct android_usb_function *f,
1042 struct usb_composite_dev *cdev,
1043 const struct usb_ctrlrequest *c)
1044{
1045 return mbim_ctrlrequest(cdev, c);
1046}
1047
Lena Salmandf7e7992013-03-15 09:46:27 +02001048static ssize_t mbim_transports_show(struct device *dev,
1049 struct device_attribute *attr, char *buf)
1050{
1051 return snprintf(buf, PAGE_SIZE, "%s\n", mbim_transports);
1052}
1053
1054static ssize_t mbim_transports_store(struct device *dev,
1055 struct device_attribute *attr, const char *buf, size_t size)
1056{
1057 strlcpy(mbim_transports, buf, sizeof(mbim_transports));
1058 return size;
1059}
1060
1061static DEVICE_ATTR(mbim_transports, S_IRUGO | S_IWUSR, mbim_transports_show,
1062 mbim_transports_store);
1063
1064static struct device_attribute *mbim_function_attributes[] = {
1065 &dev_attr_mbim_transports,
1066 NULL
1067};
1068
Anna Perela8c991d2012-04-09 16:44:46 +03001069static struct android_usb_function mbim_function = {
1070 .name = "usb_mbim",
1071 .cleanup = mbim_function_cleanup,
1072 .bind_config = mbim_function_bind_config,
1073 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -07001074 .ctrlrequest = mbim_function_ctrlrequest,
Lena Salmandf7e7992013-03-15 09:46:27 +02001075 .attributes = mbim_function_attributes,
Anna Perela8c991d2012-04-09 16:44:46 +03001076};
1077
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001078#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001079/* PERIPHERAL AUDIO */
1080static int audio_function_bind_config(struct android_usb_function *f,
1081 struct usb_configuration *c)
1082{
1083 return audio_bind_config(c);
1084}
1085
1086static struct android_usb_function audio_function = {
1087 .name = "audio",
1088 .bind_config = audio_function_bind_config,
1089};
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001090#endif
Anna Perel432367a2012-09-20 10:55:32 +03001091
Anna Perela8c991d2012-04-09 16:44:46 +03001092
Manu Gautam8e0719b2011-09-26 14:47:55 +05301093/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301094static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095static ssize_t clients_store(
1096 struct device *device, struct device_attribute *attr,
1097 const char *buff, size_t size)
1098{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301099 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100
1101 return size;
1102}
1103
1104static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
1105static struct device_attribute *diag_function_attributes[] =
1106 { &dev_attr_clients, NULL };
1107
1108static int diag_function_init(struct android_usb_function *f,
1109 struct usb_composite_dev *cdev)
1110{
1111 return diag_setup();
1112}
1113
1114static void diag_function_cleanup(struct android_usb_function *f)
1115{
1116 diag_cleanup();
1117}
1118
1119static int diag_function_bind_config(struct android_usb_function *f,
1120 struct usb_configuration *c)
1121{
1122 char *name;
1123 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +05301124 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -08001125 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001126 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301128 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 b = strim(buf);
1130
1131 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -08001132 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +05301134 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -07001135 if (!once++) {
1136 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
1137 notify = dev->pdata->update_pid_and_serial_num;
1138 else
1139 notify = usb_diag_update_pid_and_serial_num;
1140 }
Manu Gautamc5760302011-08-25 14:30:24 +05301141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +05301143 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144 if (err)
1145 pr_err("diag: Cannot open channel '%s'", name);
1146 }
1147 }
1148
1149 return err;
1150}
1151
1152static struct android_usb_function diag_function = {
1153 .name = "diag",
1154 .init = diag_function_init,
1155 .cleanup = diag_function_cleanup,
1156 .bind_config = diag_function_bind_config,
1157 .attributes = diag_function_attributes,
1158};
1159
Shimrit Malichia00d7322012-08-05 13:56:28 +03001160/* DEBUG */
1161static int qdss_function_init(struct android_usb_function *f,
1162 struct usb_composite_dev *cdev)
1163{
1164 return qdss_setup();
1165}
1166
1167static void qdss_function_cleanup(struct android_usb_function *f)
1168{
1169 qdss_cleanup();
1170}
1171
1172static int qdss_function_bind_config(struct android_usb_function *f,
1173 struct usb_configuration *c)
1174{
1175 int err = -1;
1176
1177 err = qdss_bind_config(c, "qdss");
1178 if (err)
1179 pr_err("qdss: Cannot open channel qdss");
1180
1181 return err;
1182}
1183
1184static struct android_usb_function qdss_function = {
1185 .name = "qdss",
1186 .init = qdss_function_init,
1187 .cleanup = qdss_function_cleanup,
1188 .bind_config = qdss_function_bind_config,
1189};
1190
Manu Gautam8e0719b2011-09-26 14:47:55 +05301191/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301192static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +05301193static ssize_t serial_transports_store(
1194 struct device *device, struct device_attribute *attr,
1195 const char *buff, size_t size)
1196{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301197 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +05301198
1199 return size;
1200}
1201
1202static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
1203static struct device_attribute *serial_function_attributes[] =
1204 { &dev_attr_transports, NULL };
1205
1206static void serial_function_cleanup(struct android_usb_function *f)
1207{
1208 gserial_cleanup();
1209}
1210
1211static int serial_function_bind_config(struct android_usb_function *f,
1212 struct usb_configuration *c)
1213{
1214 char *name;
1215 char buf[32], *b;
1216 int err = -1, i;
1217 static int serial_initialized = 0, ports = 0;
1218
1219 if (serial_initialized)
1220 goto bind_config;
1221
1222 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301223 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +05301224 b = strim(buf);
1225
1226 while (b) {
1227 name = strsep(&b, ",");
1228
1229 if (name) {
1230 err = gserial_init_port(ports, name);
1231 if (err) {
1232 pr_err("serial: Cannot open port '%s'", name);
1233 goto out;
1234 }
1235 ports++;
1236 }
1237 }
1238 err = gport_setup(c);
1239 if (err) {
1240 pr_err("serial: Cannot setup transports");
1241 goto out;
1242 }
1243
1244bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +02001245 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +05301246 err = gser_bind_config(c, i);
1247 if (err) {
1248 pr_err("serial: bind_config failed for port %d", i);
1249 goto out;
1250 }
1251 }
1252
1253out:
1254 return err;
1255}
1256
1257static struct android_usb_function serial_function = {
1258 .name = "serial",
1259 .cleanup = serial_function_cleanup,
1260 .bind_config = serial_function_bind_config,
1261 .attributes = serial_function_attributes,
1262};
1263
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301264/* CCID */
1265static int ccid_function_init(struct android_usb_function *f,
1266 struct usb_composite_dev *cdev)
1267{
1268 return ccid_setup();
1269}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001270
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301271static void ccid_function_cleanup(struct android_usb_function *f)
1272{
1273 ccid_cleanup();
1274}
1275
1276static int ccid_function_bind_config(struct android_usb_function *f,
1277 struct usb_configuration *c)
1278{
1279 return ccid_bind_config(c);
1280}
1281
1282static struct android_usb_function ccid_function = {
1283 .name = "ccid",
1284 .init = ccid_function_init,
1285 .cleanup = ccid_function_cleanup,
1286 .bind_config = ccid_function_bind_config,
1287};
1288
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001289static int
1290mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001291 struct usb_composite_dev *cdev)
1292{
1293 return mtp_setup();
1294}
1295
1296static void mtp_function_cleanup(struct android_usb_function *f)
1297{
1298 mtp_cleanup();
1299}
1300
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001301static int
1302mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001303 struct usb_configuration *c)
1304{
1305 return mtp_bind_config(c, false);
1306}
1307
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001308static int
1309ptp_function_init(struct android_usb_function *f,
1310 struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001311{
1312 /* nothing to do - initialization is handled by mtp_function_init */
1313 return 0;
1314}
1315
1316static void ptp_function_cleanup(struct android_usb_function *f)
1317{
1318 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1319}
1320
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001321static int
1322ptp_function_bind_config(struct android_usb_function *f,
1323 struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001324{
1325 return mtp_bind_config(c, true);
1326}
1327
1328static int mtp_function_ctrlrequest(struct android_usb_function *f,
1329 struct usb_composite_dev *cdev,
1330 const struct usb_ctrlrequest *c)
1331{
1332 return mtp_ctrlrequest(cdev, c);
1333}
1334
1335static struct android_usb_function mtp_function = {
1336 .name = "mtp",
1337 .init = mtp_function_init,
1338 .cleanup = mtp_function_cleanup,
1339 .bind_config = mtp_function_bind_config,
1340 .ctrlrequest = mtp_function_ctrlrequest,
1341};
1342
1343/* PTP function is same as MTP with slightly different interface descriptor */
1344static struct android_usb_function ptp_function = {
1345 .name = "ptp",
1346 .init = ptp_function_init,
1347 .cleanup = ptp_function_cleanup,
1348 .bind_config = ptp_function_bind_config,
1349};
1350
1351
Benoit Goby1e8ce152011-12-12 13:01:23 -08001352struct rndis_function_config {
1353 u8 ethaddr[ETH_ALEN];
1354 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001355 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001356 char manufacturer[256];
1357 /* "Wireless" RNDIS; auto-detected by Windows */
1358 bool wceis;
1359};
1360
1361static int
1362rndis_function_init(struct android_usb_function *f,
1363 struct usb_composite_dev *cdev)
1364{
1365 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1366 if (!f->config)
1367 return -ENOMEM;
1368 return 0;
1369}
1370
1371static void rndis_function_cleanup(struct android_usb_function *f)
1372{
1373 kfree(f->config);
1374 f->config = NULL;
1375}
1376
Ofir Cohenaef90b72012-07-31 12:37:04 +02001377static int rndis_qc_function_init(struct android_usb_function *f,
1378 struct usb_composite_dev *cdev)
1379{
1380 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1381 if (!f->config)
1382 return -ENOMEM;
1383
1384 return rndis_qc_init();
1385}
1386
1387static void rndis_qc_function_cleanup(struct android_usb_function *f)
1388{
1389 rndis_qc_cleanup();
1390 kfree(f->config);
1391}
1392
Benoit Goby1e8ce152011-12-12 13:01:23 -08001393static int
1394rndis_function_bind_config(struct android_usb_function *f,
1395 struct usb_configuration *c)
1396{
1397 int ret;
1398 struct rndis_function_config *rndis = f->config;
1399
1400 if (!rndis) {
1401 pr_err("%s: rndis_pdata\n", __func__);
1402 return -1;
1403 }
1404
1405 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1406 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1407 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1408
1409 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1410 if (ret) {
1411 pr_err("%s: gether_setup failed\n", __func__);
1412 return ret;
1413 }
1414
1415 if (rndis->wceis) {
1416 /* "Wireless" RNDIS; auto-detected by Windows */
1417 rndis_iad_descriptor.bFunctionClass =
1418 USB_CLASS_WIRELESS_CONTROLLER;
1419 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1420 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1421 rndis_control_intf.bInterfaceClass =
1422 USB_CLASS_WIRELESS_CONTROLLER;
1423 rndis_control_intf.bInterfaceSubClass = 0x01;
1424 rndis_control_intf.bInterfaceProtocol = 0x03;
1425 }
1426
1427 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1428 rndis->manufacturer);
1429}
1430
Ofir Cohenaef90b72012-07-31 12:37:04 +02001431static int rndis_qc_function_bind_config(struct android_usb_function *f,
1432 struct usb_configuration *c)
1433{
1434 int ret;
1435 struct rndis_function_config *rndis = f->config;
1436
1437 if (!rndis) {
1438 pr_err("%s: rndis_pdata\n", __func__);
1439 return -EINVAL;
1440 }
1441
1442 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1443 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1444 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1445
1446 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1447 if (ret) {
1448 pr_err("%s: gether_setup failed\n", __func__);
1449 return ret;
1450 }
1451
1452 if (rndis->wceis) {
1453 /* "Wireless" RNDIS; auto-detected by Windows */
1454 rndis_qc_iad_descriptor.bFunctionClass =
1455 USB_CLASS_WIRELESS_CONTROLLER;
1456 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1457 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1458 rndis_qc_control_intf.bInterfaceClass =
1459 USB_CLASS_WIRELESS_CONTROLLER;
1460 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1461 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1462 }
1463
1464 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1465 rndis->manufacturer,
1466 rndis->max_pkt_per_xfer);
1467}
1468
Benoit Goby1e8ce152011-12-12 13:01:23 -08001469static void rndis_function_unbind_config(struct android_usb_function *f,
1470 struct usb_configuration *c)
1471{
1472 gether_cleanup();
1473}
1474
Ofir Cohenaef90b72012-07-31 12:37:04 +02001475static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1476 struct usb_configuration *c)
1477{
Amit Blayd6d690a2012-10-16 13:37:42 +02001478 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001479}
1480
Benoit Goby1e8ce152011-12-12 13:01:23 -08001481static ssize_t rndis_manufacturer_show(struct device *dev,
1482 struct device_attribute *attr, char *buf)
1483{
1484 struct android_usb_function *f = dev_get_drvdata(dev);
1485 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001486
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301487 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001488}
1489
1490static ssize_t rndis_manufacturer_store(struct device *dev,
1491 struct device_attribute *attr, const char *buf, size_t size)
1492{
1493 struct android_usb_function *f = dev_get_drvdata(dev);
1494 struct rndis_function_config *config = f->config;
1495
1496 if (size >= sizeof(config->manufacturer))
1497 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001498
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301499 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001500 return size;
1501 return -1;
1502}
1503
1504static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1505 rndis_manufacturer_store);
1506
1507static ssize_t rndis_wceis_show(struct device *dev,
1508 struct device_attribute *attr, char *buf)
1509{
1510 struct android_usb_function *f = dev_get_drvdata(dev);
1511 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001512
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301513 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001514}
1515
1516static ssize_t rndis_wceis_store(struct device *dev,
1517 struct device_attribute *attr, const char *buf, size_t size)
1518{
1519 struct android_usb_function *f = dev_get_drvdata(dev);
1520 struct rndis_function_config *config = f->config;
1521 int value;
1522
1523 if (sscanf(buf, "%d", &value) == 1) {
1524 config->wceis = value;
1525 return size;
1526 }
1527 return -EINVAL;
1528}
1529
1530static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1531 rndis_wceis_store);
1532
1533static ssize_t rndis_ethaddr_show(struct device *dev,
1534 struct device_attribute *attr, char *buf)
1535{
1536 struct android_usb_function *f = dev_get_drvdata(dev);
1537 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001538
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301539 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001540 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1541 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1542}
1543
1544static ssize_t rndis_ethaddr_store(struct device *dev,
1545 struct device_attribute *attr, const char *buf, size_t size)
1546{
1547 struct android_usb_function *f = dev_get_drvdata(dev);
1548 struct rndis_function_config *rndis = f->config;
1549
1550 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1551 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1552 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1553 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1554 return size;
1555 return -EINVAL;
1556}
1557
1558static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1559 rndis_ethaddr_store);
1560
1561static ssize_t rndis_vendorID_show(struct device *dev,
1562 struct device_attribute *attr, char *buf)
1563{
1564 struct android_usb_function *f = dev_get_drvdata(dev);
1565 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001566
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301567 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001568}
1569
1570static ssize_t rndis_vendorID_store(struct device *dev,
1571 struct device_attribute *attr, const char *buf, size_t size)
1572{
1573 struct android_usb_function *f = dev_get_drvdata(dev);
1574 struct rndis_function_config *config = f->config;
1575 int value;
1576
1577 if (sscanf(buf, "%04x", &value) == 1) {
1578 config->vendorID = value;
1579 return size;
1580 }
1581 return -EINVAL;
1582}
1583
1584static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1585 rndis_vendorID_store);
1586
Ofir Cohenaef90b72012-07-31 12:37:04 +02001587static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1588 struct device_attribute *attr, char *buf)
1589{
1590 struct android_usb_function *f = dev_get_drvdata(dev);
1591 struct rndis_function_config *config = f->config;
1592 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1593}
1594
1595static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1596 struct device_attribute *attr, const char *buf, size_t size)
1597{
1598 struct android_usb_function *f = dev_get_drvdata(dev);
1599 struct rndis_function_config *config = f->config;
1600 int value;
1601
1602 if (sscanf(buf, "%d", &value) == 1) {
1603 config->max_pkt_per_xfer = value;
1604 return size;
1605 }
1606 return -EINVAL;
1607}
1608
1609static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1610 rndis_max_pkt_per_xfer_show,
1611 rndis_max_pkt_per_xfer_store);
1612
Benoit Goby1e8ce152011-12-12 13:01:23 -08001613static struct device_attribute *rndis_function_attributes[] = {
1614 &dev_attr_manufacturer,
1615 &dev_attr_wceis,
1616 &dev_attr_ethaddr,
1617 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001618 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001619 NULL
1620};
1621
1622static struct android_usb_function rndis_function = {
1623 .name = "rndis",
1624 .init = rndis_function_init,
1625 .cleanup = rndis_function_cleanup,
1626 .bind_config = rndis_function_bind_config,
1627 .unbind_config = rndis_function_unbind_config,
1628 .attributes = rndis_function_attributes,
1629};
1630
Ofir Cohenaef90b72012-07-31 12:37:04 +02001631static struct android_usb_function rndis_qc_function = {
1632 .name = "rndis_qc",
1633 .init = rndis_qc_function_init,
1634 .cleanup = rndis_qc_function_cleanup,
1635 .bind_config = rndis_qc_function_bind_config,
1636 .unbind_config = rndis_qc_function_unbind_config,
1637 .attributes = rndis_function_attributes,
1638};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001639
Anna Perelf9d01552012-11-20 15:56:32 +02001640static int ecm_function_bind_config(struct android_usb_function *f,
1641 struct usb_configuration *c)
1642{
1643 int ret;
1644 struct ecm_function_config *ecm = f->config;
1645
1646 if (!ecm) {
1647 pr_err("%s: ecm_pdata\n", __func__);
1648 return -EINVAL;
1649 }
1650
1651 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1652 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1653 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1654
1655 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1656 if (ret) {
1657 pr_err("%s: gether_setup failed\n", __func__);
1658 return ret;
1659 }
1660
1661 ret = ecm_bind_config(c, ecm->ethaddr);
1662 if (ret) {
1663 pr_err("%s: ecm_bind_config failed\n", __func__);
1664 gether_cleanup();
1665 }
1666 return ret;
1667}
1668
1669static void ecm_function_unbind_config(struct android_usb_function *f,
1670 struct usb_configuration *c)
1671{
1672 gether_cleanup();
1673}
1674
1675static struct android_usb_function ecm_function = {
1676 .name = "ecm",
1677 .init = ecm_function_init,
1678 .cleanup = ecm_function_cleanup,
1679 .bind_config = ecm_function_bind_config,
1680 .unbind_config = ecm_function_unbind_config,
1681 .attributes = ecm_function_attributes,
1682};
1683
Benoit Goby1e8ce152011-12-12 13:01:23 -08001684struct mass_storage_function_config {
1685 struct fsg_config fsg;
1686 struct fsg_common *common;
1687};
1688
1689static int mass_storage_function_init(struct android_usb_function *f,
1690 struct usb_composite_dev *cdev)
1691{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001692 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001693 struct mass_storage_function_config *config;
1694 struct fsg_common *common;
1695 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301696 int i;
1697 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001698
1699 config = kzalloc(sizeof(struct mass_storage_function_config),
1700 GFP_KERNEL);
1701 if (!config)
1702 return -ENOMEM;
1703
1704 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301705 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301706 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301707 config->fsg.nluns = 2;
1708 config->fsg.luns[1].cdrom = 1;
1709 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301710 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301711 name[1] = "lun0";
1712 }
1713
Benoit Goby1e8ce152011-12-12 13:01:23 -08001714 config->fsg.luns[0].removable = 1;
1715
1716 common = fsg_common_init(NULL, cdev, &config->fsg);
1717 if (IS_ERR(common)) {
1718 kfree(config);
1719 return PTR_ERR(common);
1720 }
1721
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301722 for (i = 0; i < config->fsg.nluns; i++) {
1723 err = sysfs_create_link(&f->dev->kobj,
1724 &common->luns[i].dev.kobj,
1725 name[i]);
1726 if (err)
1727 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001728 }
1729
1730 config->common = common;
1731 f->config = config;
1732 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301733error:
1734 for (; i > 0 ; i--)
1735 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1736
1737 fsg_common_release(&common->ref);
1738 kfree(config);
1739 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001740}
1741
1742static void mass_storage_function_cleanup(struct android_usb_function *f)
1743{
1744 kfree(f->config);
1745 f->config = NULL;
1746}
1747
1748static int mass_storage_function_bind_config(struct android_usb_function *f,
1749 struct usb_configuration *c)
1750{
1751 struct mass_storage_function_config *config = f->config;
1752 return fsg_bind_config(c->cdev, c, config->common);
1753}
1754
1755static ssize_t mass_storage_inquiry_show(struct device *dev,
1756 struct device_attribute *attr, char *buf)
1757{
1758 struct android_usb_function *f = dev_get_drvdata(dev);
1759 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301760 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001761}
1762
1763static ssize_t mass_storage_inquiry_store(struct device *dev,
1764 struct device_attribute *attr, const char *buf, size_t size)
1765{
1766 struct android_usb_function *f = dev_get_drvdata(dev);
1767 struct mass_storage_function_config *config = f->config;
1768 if (size >= sizeof(config->common->inquiry_string))
1769 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301770 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001771 return -EINVAL;
1772 return size;
1773}
1774
1775static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1776 mass_storage_inquiry_show,
1777 mass_storage_inquiry_store);
1778
1779static struct device_attribute *mass_storage_function_attributes[] = {
1780 &dev_attr_inquiry_string,
1781 NULL
1782};
1783
1784static struct android_usb_function mass_storage_function = {
1785 .name = "mass_storage",
1786 .init = mass_storage_function_init,
1787 .cleanup = mass_storage_function_cleanup,
1788 .bind_config = mass_storage_function_bind_config,
1789 .attributes = mass_storage_function_attributes,
1790};
1791
1792
Benoit Gobycf3fc062011-12-19 14:39:37 -08001793static int accessory_function_init(struct android_usb_function *f,
1794 struct usb_composite_dev *cdev)
1795{
1796 return acc_setup();
1797}
1798
1799static void accessory_function_cleanup(struct android_usb_function *f)
1800{
1801 acc_cleanup();
1802}
1803
1804static int accessory_function_bind_config(struct android_usb_function *f,
1805 struct usb_configuration *c)
1806{
1807 return acc_bind_config(c);
1808}
1809
1810static int accessory_function_ctrlrequest(struct android_usb_function *f,
1811 struct usb_composite_dev *cdev,
1812 const struct usb_ctrlrequest *c)
1813{
1814 return acc_ctrlrequest(cdev, c);
1815}
1816
1817static struct android_usb_function accessory_function = {
1818 .name = "accessory",
1819 .init = accessory_function_init,
1820 .cleanup = accessory_function_cleanup,
1821 .bind_config = accessory_function_bind_config,
1822 .ctrlrequest = accessory_function_ctrlrequest,
1823};
1824
Anna Perel3ee23dd2013-02-26 16:06:40 +02001825#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301826static int audio_source_function_init(struct android_usb_function *f,
1827 struct usb_composite_dev *cdev)
1828{
1829 struct audio_source_config *config;
1830
1831 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1832 if (!config)
1833 return -ENOMEM;
1834 config->card = -1;
1835 config->device = -1;
1836 f->config = config;
1837 return 0;
1838}
1839
1840static void audio_source_function_cleanup(struct android_usb_function *f)
1841{
1842 kfree(f->config);
1843}
1844
1845static int audio_source_function_bind_config(struct android_usb_function *f,
1846 struct usb_configuration *c)
1847{
1848 struct audio_source_config *config = f->config;
1849
1850 return audio_source_bind_config(c, config);
1851}
1852
1853static void audio_source_function_unbind_config(struct android_usb_function *f,
1854 struct usb_configuration *c)
1855{
1856 struct audio_source_config *config = f->config;
1857
1858 config->card = -1;
1859 config->device = -1;
1860}
1861
1862static ssize_t audio_source_pcm_show(struct device *dev,
1863 struct device_attribute *attr, char *buf)
1864{
1865 struct android_usb_function *f = dev_get_drvdata(dev);
1866 struct audio_source_config *config = f->config;
1867
1868 /* print PCM card and device numbers */
1869 return sprintf(buf, "%d %d\n", config->card, config->device);
1870}
1871
1872static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1873
1874static struct device_attribute *audio_source_function_attributes[] = {
1875 &dev_attr_pcm,
1876 NULL
1877};
1878
1879static struct android_usb_function audio_source_function = {
1880 .name = "audio_source",
1881 .init = audio_source_function_init,
1882 .cleanup = audio_source_function_cleanup,
1883 .bind_config = audio_source_function_bind_config,
1884 .unbind_config = audio_source_function_unbind_config,
1885 .attributes = audio_source_function_attributes,
1886};
Anna Perel3ee23dd2013-02-26 16:06:40 +02001887#endif
Mike Lockwood11874822012-08-27 16:43:53 +05301888
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301889static int android_uasp_connect_cb(bool connect)
1890{
1891 /*
1892 * TODO
1893 * We may have to disable gadget till UASP configfs nodes
1894 * are configured which includes mapping LUN with the
1895 * backing file. It is a fundamental difference between
1896 * f_mass_storage and f_tcp. That means UASP can not be
1897 * in default composition.
1898 *
1899 * For now, assume that UASP configfs nodes are configured
1900 * before enabling android gadget. Or cable should be
1901 * reconnected after mapping the LUN.
1902 *
1903 * Also consider making UASP to respond to Host requests when
1904 * Lun is not mapped.
1905 */
1906 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1907
1908 return 0;
1909}
1910
1911static int uasp_function_init(struct android_usb_function *f,
1912 struct usb_composite_dev *cdev)
1913{
1914 return f_tcm_init(&android_uasp_connect_cb);
1915}
1916
1917static void uasp_function_cleanup(struct android_usb_function *f)
1918{
1919 f_tcm_exit();
1920}
1921
1922static int uasp_function_bind_config(struct android_usb_function *f,
1923 struct usb_configuration *c)
1924{
1925 return tcm_bind_config(c);
1926}
1927
1928static struct android_usb_function uasp_function = {
1929 .name = "uasp",
1930 .init = uasp_function_init,
1931 .cleanup = uasp_function_cleanup,
1932 .bind_config = uasp_function_bind_config,
1933};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001934
Benoit Goby1e8ce152011-12-12 13:01:23 -08001935static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001936 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001937 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001938#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001939 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001940#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301941 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301942 &rmnet_sdio_function,
1943 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301944 &rmnet_function,
Bar Weinered1059e2013-04-16 11:23:55 +03001945 &gps_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001947 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301948 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001949 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301950 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001951 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001952 &mtp_function,
1953 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001954 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001955 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02001956 &ecm_function,
Vamsi Krishna932c9de2013-05-22 12:18:05 -07001957 &ncm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001958 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001959 &accessory_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001960#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301961 &audio_source_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001962#endif
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301963 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001964 NULL
1965};
1966
Lena Salmand092f2d2012-03-12 17:27:24 +02001967static void android_cleanup_functions(struct android_usb_function **functions)
1968{
1969 struct android_usb_function *f;
1970 struct device_attribute **attrs;
1971 struct device_attribute *attr;
1972
1973 while (*functions) {
1974 f = *functions++;
1975
1976 if (f->dev) {
1977 device_destroy(android_class, f->dev->devt);
1978 kfree(f->dev_name);
1979 } else
1980 continue;
1981
1982 if (f->cleanup)
1983 f->cleanup(f);
1984
1985 attrs = f->attributes;
1986 if (attrs) {
1987 while ((attr = *attrs++))
1988 device_remove_file(f->dev, attr);
1989 }
1990 }
1991}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001992
1993static int android_init_functions(struct android_usb_function **functions,
1994 struct usb_composite_dev *cdev)
1995{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001996 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001997 struct android_usb_function *f;
1998 struct device_attribute **attrs;
1999 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302000 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02002001 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08002002
2003 for (; (f = *functions++); index++) {
2004 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002005 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02002006 if (!f->dev_name) {
2007 err = -ENOMEM;
2008 goto err_out;
2009 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002010 f->dev = device_create(android_class, dev->dev,
2011 MKDEV(0, index), f, f->dev_name);
2012 if (IS_ERR(f->dev)) {
2013 pr_err("%s: Failed to create dev %s", __func__,
2014 f->dev_name);
2015 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02002016 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002017 goto err_create;
2018 }
2019
2020 if (f->init) {
2021 err = f->init(f, cdev);
2022 if (err) {
2023 pr_err("%s: Failed to init %s", __func__,
2024 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002025 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002026 }
2027 }
2028
2029 attrs = f->attributes;
2030 if (attrs) {
2031 while ((attr = *attrs++) && !err)
2032 err = device_create_file(f->dev, attr);
2033 }
2034 if (err) {
2035 pr_err("%s: Failed to create function %s attributes",
2036 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002037 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002038 }
2039 }
2040 return 0;
2041
Lena Salmand092f2d2012-03-12 17:27:24 +02002042err_attrs:
2043 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
2044 device_remove_file(f->dev, attr);
2045 if (f->cleanup)
2046 f->cleanup(f);
2047err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08002048 device_destroy(android_class, f->dev->devt);
2049err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02002050 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002051 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002052err_out:
2053 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002054 return err;
2055}
2056
Benoit Goby1e8ce152011-12-12 13:01:23 -08002057static int
2058android_bind_enabled_functions(struct android_dev *dev,
2059 struct usb_configuration *c)
2060{
Ido Shayevitz68557e32012-11-06 12:40:37 +02002061 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002062 struct android_configuration *conf =
2063 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002064 int ret;
2065
Ido Shayevitz68557e32012-11-06 12:40:37 +02002066 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
2067 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002068 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002069 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002070 return ret;
2071 }
2072 }
2073 return 0;
2074}
2075
2076static void
2077android_unbind_enabled_functions(struct android_dev *dev,
2078 struct usb_configuration *c)
2079{
Ido Shayevitz68557e32012-11-06 12:40:37 +02002080 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002081 struct android_configuration *conf =
2082 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002083
Ido Shayevitz68557e32012-11-06 12:40:37 +02002084 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
2085 if (f_holder->f->unbind_config)
2086 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002087 }
2088}
2089
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002090static int android_enable_function(struct android_dev *dev,
2091 struct android_configuration *conf,
2092 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002093{
2094 struct android_usb_function **functions = dev->functions;
2095 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002096 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002097 while ((f = *functions++)) {
2098 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002099 if (f->android_dev && f->android_dev != dev)
2100 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002101 f->name);
2102 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002103 f_holder = kzalloc(sizeof(*f_holder),
2104 GFP_KERNEL);
2105 if (!f_holder) {
2106 pr_err("Failed to alloc f_holder\n");
2107 return -ENOMEM;
2108 }
2109
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002110 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002111 f_holder->f = f;
2112 list_add_tail(&f_holder->enabled_list,
2113 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002114 return 0;
2115 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002116 }
2117 }
2118 return -EINVAL;
2119}
2120
2121/*-------------------------------------------------------------------------*/
2122/* /sys/class/android_usb/android%d/ interface */
2123
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302124static ssize_t remote_wakeup_show(struct device *pdev,
2125 struct device_attribute *attr, char *buf)
2126{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002127 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002128 struct android_configuration *conf;
2129
2130 /*
2131 * Show the wakeup attribute of the first configuration,
2132 * since all configurations have the same wakeup attribute
2133 */
2134 if (dev->configs_num == 0)
2135 return 0;
2136 conf = list_entry(dev->configs.next,
2137 struct android_configuration,
2138 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002139
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302140 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002141 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302142 USB_CONFIG_ATT_WAKEUP));
2143}
2144
2145static ssize_t remote_wakeup_store(struct device *pdev,
2146 struct device_attribute *attr, const char *buff, size_t size)
2147{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002148 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002149 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302150 int enable = 0;
2151
2152 sscanf(buff, "%d", &enable);
2153
2154 pr_debug("android_usb: %s remote wakeup\n",
2155 enable ? "enabling" : "disabling");
2156
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002157 list_for_each_entry(conf, &dev->configs, list_item)
2158 if (enable)
2159 conf->usb_config.bmAttributes |=
2160 USB_CONFIG_ATT_WAKEUP;
2161 else
2162 conf->usb_config.bmAttributes &=
2163 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302164
2165 return size;
2166}
2167
Benoit Goby1e8ce152011-12-12 13:01:23 -08002168static ssize_t
2169functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2170{
2171 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002172 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002173 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002174 char *buff = buf;
2175
2176 mutex_lock(&dev->mutex);
2177
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002178 list_for_each_entry(conf, &dev->configs, list_item) {
2179 if (buff != buf)
2180 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02002181 list_for_each_entry(f_holder, &conf->enabled_functions,
2182 enabled_list)
2183 buff += snprintf(buff, PAGE_SIZE, "%s,",
2184 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002185 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002186
2187 mutex_unlock(&dev->mutex);
2188
2189 if (buff != buf)
2190 *(buff-1) = '\n';
2191 return buff - buf;
2192}
2193
2194static ssize_t
2195functions_store(struct device *pdev, struct device_attribute *attr,
2196 const char *buff, size_t size)
2197{
2198 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002199 struct list_head *curr_conf = &dev->configs;
2200 struct android_configuration *conf;
2201 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002202 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002203 char *name;
2204 char buf[256], *b;
2205 int err;
2206
2207 mutex_lock(&dev->mutex);
2208
2209 if (dev->enabled) {
2210 mutex_unlock(&dev->mutex);
2211 return -EBUSY;
2212 }
2213
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002214 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002215 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002216 while (conf->enabled_functions.next !=
2217 &conf->enabled_functions) {
2218 f_holder = list_entry(conf->enabled_functions.next,
2219 typeof(*f_holder),
2220 enabled_list);
2221 f_holder->f->android_dev = NULL;
2222 list_del(&f_holder->enabled_list);
2223 kfree(f_holder);
2224 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002225 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002226 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002227
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302228 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002229 b = strim(buf);
2230
2231 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002232 conf_str = strsep(&b, ":");
2233 if (conf_str) {
2234 /* If the next not equal to the head, take it */
2235 if (curr_conf->next != &dev->configs)
2236 conf = list_entry(curr_conf->next,
2237 struct android_configuration,
2238 list_item);
2239 else
2240 conf = alloc_android_config(dev);
2241
2242 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002243 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002244
2245 while (conf_str) {
2246 name = strsep(&conf_str, ",");
2247 if (name) {
2248 err = android_enable_function(dev, conf, name);
2249 if (err)
2250 pr_err("android_usb: Cannot enable %s",
2251 name);
2252 }
2253 }
2254 }
2255
2256 /* Free uneeded configurations if exists */
2257 while (curr_conf->next != &dev->configs) {
2258 conf = list_entry(curr_conf->next,
2259 struct android_configuration, list_item);
2260 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002261 }
2262
2263 mutex_unlock(&dev->mutex);
2264
2265 return size;
2266}
2267
2268static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2269 char *buf)
2270{
2271 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002272
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302273 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002274}
2275
2276static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2277 const char *buff, size_t size)
2278{
2279 struct android_dev *dev = dev_get_drvdata(pdev);
2280 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002281 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002282 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002283 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302284 bool audio_enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302285 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002286
Stephen Boyd42517402013-01-14 16:41:42 -08002287
Benoit Gobycf3fc062011-12-19 14:39:37 -08002288 if (!cdev)
2289 return -ENODEV;
2290
Benoit Goby1e8ce152011-12-12 13:01:23 -08002291 mutex_lock(&dev->mutex);
2292
2293 sscanf(buff, "%d", &enabled);
2294 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002295 /*
2296 * Update values in composite driver's copy of
2297 * device descriptor.
2298 */
2299 cdev->desc.idVendor = device_desc.idVendor;
2300 cdev->desc.idProduct = device_desc.idProduct;
2301 cdev->desc.bcdDevice = device_desc.bcdDevice;
2302 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2303 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2304 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302305
2306 /* Audio dock accessory is unable to enumerate device if
2307 * pull-up is enabled immediately. The enumeration is
2308 * reliable with 100 msec delay.
2309 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002310 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002311 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002312 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002313 if (f_holder->f->enable)
2314 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302315 if (!strncmp(f_holder->f->name,
2316 "audio_source", 12))
2317 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002318 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302319 if (audio_enabled)
2320 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002321 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002322 dev->enabled = true;
2323 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002324 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002325 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002326 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002327 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002328 if (f_holder->f->disable)
2329 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002330 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002331 dev->enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302332 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002333 pr_err("android_usb: already %s\n",
2334 dev->enabled ? "enabled" : "disabled");
2335 }
2336
2337 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002338
Benoit Gobyaab96812011-04-19 20:37:33 -07002339 return size;
2340}
2341
Ofir Cohen94213a72012-05-03 14:26:32 +03002342static ssize_t pm_qos_show(struct device *pdev,
2343 struct device_attribute *attr, char *buf)
2344{
2345 struct android_dev *dev = dev_get_drvdata(pdev);
2346
2347 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2348}
2349
2350static ssize_t pm_qos_store(struct device *pdev,
2351 struct device_attribute *attr,
2352 const char *buff, size_t size)
2353{
2354 struct android_dev *dev = dev_get_drvdata(pdev);
2355
2356 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2357
Benoit Goby1e8ce152011-12-12 13:01:23 -08002358 return size;
2359}
2360
2361static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2362 char *buf)
2363{
2364 struct android_dev *dev = dev_get_drvdata(pdev);
2365 struct usb_composite_dev *cdev = dev->cdev;
2366 char *state = "DISCONNECTED";
2367 unsigned long flags;
2368
2369 if (!cdev)
2370 goto out;
2371
2372 spin_lock_irqsave(&cdev->lock, flags);
2373 if (cdev->config)
2374 state = "CONFIGURED";
2375 else if (dev->connected)
2376 state = "CONNECTED";
2377 spin_unlock_irqrestore(&cdev->lock, flags);
2378out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302379 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002380}
2381
2382#define DESCRIPTOR_ATTR(field, format_string) \
2383static ssize_t \
2384field ## _show(struct device *dev, struct device_attribute *attr, \
2385 char *buf) \
2386{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302387 return snprintf(buf, PAGE_SIZE, \
2388 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002389} \
2390static ssize_t \
2391field ## _store(struct device *dev, struct device_attribute *attr, \
2392 const char *buf, size_t size) \
2393{ \
2394 int value; \
2395 if (sscanf(buf, format_string, &value) == 1) { \
2396 device_desc.field = value; \
2397 return size; \
2398 } \
2399 return -1; \
2400} \
2401static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2402
2403#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2404static ssize_t \
2405field ## _show(struct device *dev, struct device_attribute *attr, \
2406 char *buf) \
2407{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302408 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002409} \
2410static ssize_t \
2411field ## _store(struct device *dev, struct device_attribute *attr, \
2412 const char *buf, size_t size) \
2413{ \
2414 if (size >= sizeof(buffer)) \
2415 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302416 strlcpy(buffer, buf, sizeof(buffer)); \
2417 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302418 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002419} \
2420static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2421
2422
2423DESCRIPTOR_ATTR(idVendor, "%04x\n")
2424DESCRIPTOR_ATTR(idProduct, "%04x\n")
2425DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2426DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2427DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2428DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2429DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2430DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2431DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2432
2433static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2434 functions_store);
2435static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002436static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2437 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002438static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302439static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2440 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002441
2442static struct device_attribute *android_usb_attributes[] = {
2443 &dev_attr_idVendor,
2444 &dev_attr_idProduct,
2445 &dev_attr_bcdDevice,
2446 &dev_attr_bDeviceClass,
2447 &dev_attr_bDeviceSubClass,
2448 &dev_attr_bDeviceProtocol,
2449 &dev_attr_iManufacturer,
2450 &dev_attr_iProduct,
2451 &dev_attr_iSerial,
2452 &dev_attr_functions,
2453 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002454 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002455 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302456 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002457 NULL
2458};
2459
2460/*-------------------------------------------------------------------------*/
2461/* Composite driver */
2462
2463static int android_bind_config(struct usb_configuration *c)
2464{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002465 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002466 int ret = 0;
2467
2468 ret = android_bind_enabled_functions(dev, c);
2469 if (ret)
2470 return ret;
2471
2472 return 0;
2473}
2474
2475static void android_unbind_config(struct usb_configuration *c)
2476{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002477 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002478
2479 android_unbind_enabled_functions(dev, c);
2480}
2481
2482static int android_bind(struct usb_composite_dev *cdev)
2483{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002484 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002485 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002486 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002487 int gcnum, id, ret;
2488
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002489 /* Bind to the last android_dev that was probed */
2490 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2491
2492 dev->cdev = cdev;
2493
Benoit Goby1e8ce152011-12-12 13:01:23 -08002494 /*
2495 * Start disconnected. Userspace will connect the gadget once
2496 * it is done configuring the functions.
2497 */
2498 usb_gadget_disconnect(gadget);
2499
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002500 /* Init the supported functions only once, on the first android_dev */
2501 if (android_dev_count == 1) {
2502 ret = android_init_functions(dev->functions, cdev);
2503 if (ret)
2504 return ret;
2505 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002506
2507 /* Allocate string descriptor numbers ... note that string
2508 * contents can be overridden by the composite_dev glue.
2509 */
2510 id = usb_string_id(cdev);
2511 if (id < 0)
2512 return id;
2513 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2514 device_desc.iManufacturer = id;
2515
2516 id = usb_string_id(cdev);
2517 if (id < 0)
2518 return id;
2519 strings_dev[STRING_PRODUCT_IDX].id = id;
2520 device_desc.iProduct = id;
2521
2522 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302523 strlcpy(manufacturer_string, "Android",
2524 sizeof(manufacturer_string) - 1);
2525 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2526 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002527
2528 id = usb_string_id(cdev);
2529 if (id < 0)
2530 return id;
2531 strings_dev[STRING_SERIAL_IDX].id = id;
2532 device_desc.iSerialNumber = id;
2533
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302534 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002535 list_for_each_entry(conf, &dev->configs, list_item)
2536 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302537
Benoit Goby1e8ce152011-12-12 13:01:23 -08002538 gcnum = usb_gadget_controller_number(gadget);
2539 if (gcnum >= 0)
2540 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2541 else {
2542 pr_warning("%s: controller '%s' not recognized\n",
2543 longname, gadget->name);
2544 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2545 }
2546
Benoit Goby1e8ce152011-12-12 13:01:23 -08002547 return 0;
2548}
2549
2550static int android_usb_unbind(struct usb_composite_dev *cdev)
2551{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002552 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002553
Lena Salmand092f2d2012-03-12 17:27:24 +02002554 manufacturer_string[0] = '\0';
2555 product_string[0] = '\0';
2556 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002557 cancel_work_sync(&dev->work);
2558 android_cleanup_functions(dev->functions);
2559 return 0;
2560}
2561
2562static struct usb_composite_driver android_usb_driver = {
2563 .name = "android_usb",
2564 .dev = &device_desc,
2565 .strings = dev_strings,
2566 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002567 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002568};
2569
2570static int
2571android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2572{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002573 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002574 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002575 struct usb_request *req = cdev->req;
2576 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002577 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002578 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002579 int value = -EOPNOTSUPP;
2580 unsigned long flags;
2581
2582 req->zero = 0;
2583 req->complete = composite_setup_complete;
2584 req->length = 0;
2585 gadget->ep0->driver_data = cdev;
2586
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002587 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002588 list_for_each_entry(f_holder,
2589 &conf->enabled_functions,
2590 enabled_list) {
2591 f = f_holder->f;
2592 if (f->ctrlrequest) {
2593 value = f->ctrlrequest(f, cdev, c);
2594 if (value >= 0)
2595 break;
2596 }
2597 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002598
Benoit Gobycf3fc062011-12-19 14:39:37 -08002599 /* Special case the accessory function.
2600 * It needs to handle control requests before it is enabled.
2601 */
2602 if (value < 0)
2603 value = acc_ctrlrequest(cdev, c);
2604
Benoit Goby1e8ce152011-12-12 13:01:23 -08002605 if (value < 0)
2606 value = composite_setup(gadget, c);
2607
2608 spin_lock_irqsave(&cdev->lock, flags);
2609 if (!dev->connected) {
2610 dev->connected = 1;
2611 schedule_work(&dev->work);
2612 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2613 cdev->config) {
2614 schedule_work(&dev->work);
2615 }
2616 spin_unlock_irqrestore(&cdev->lock, flags);
2617
2618 return value;
2619}
2620
2621static void android_disconnect(struct usb_gadget *gadget)
2622{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002623 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002624 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002625 unsigned long flags;
2626
2627 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302628 /* accessory HID support can be active while the
2629 accessory function is not actually enabled,
2630 so we need to inform it when we are disconnected.
2631 */
2632 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002633
2634 spin_lock_irqsave(&cdev->lock, flags);
2635 dev->connected = 0;
2636 schedule_work(&dev->work);
2637 spin_unlock_irqrestore(&cdev->lock, flags);
2638}
2639
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002640static void android_suspend(struct usb_gadget *gadget)
2641{
2642 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2643 struct android_dev *dev = cdev_to_android_dev(cdev);
2644 unsigned long flags;
2645
2646 spin_lock_irqsave(&cdev->lock, flags);
2647 dev->suspended = 1;
2648 schedule_work(&dev->work);
2649 spin_unlock_irqrestore(&cdev->lock, flags);
2650
2651 composite_suspend(gadget);
2652}
2653
2654static void android_resume(struct usb_gadget *gadget)
2655{
2656 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2657 struct android_dev *dev = cdev_to_android_dev(cdev);
2658 unsigned long flags;
2659
2660 spin_lock_irqsave(&cdev->lock, flags);
2661 dev->suspended = 0;
2662 schedule_work(&dev->work);
2663 spin_unlock_irqrestore(&cdev->lock, flags);
2664
2665 composite_resume(gadget);
2666}
2667
2668
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002669static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002670{
2671 struct device_attribute **attrs = android_usb_attributes;
2672 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002673 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002674 int err;
2675
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002676 /*
2677 * The primary usb core should always have usb_core_id=0, since
2678 * Android user space is currently interested in android0 events.
2679 */
2680 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2681 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002682 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002683 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002684 if (IS_ERR(dev->dev))
2685 return PTR_ERR(dev->dev);
2686
2687 dev_set_drvdata(dev->dev, dev);
2688
2689 while ((attr = *attrs++)) {
2690 err = device_create_file(dev->dev, attr);
2691 if (err) {
2692 device_destroy(android_class, dev->dev->devt);
2693 return err;
2694 }
2695 }
2696 return 0;
2697}
2698
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302699static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002700{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302701 struct device_attribute **attrs = android_usb_attributes;
2702 struct device_attribute *attr;
2703
2704 while ((attr = *attrs++))
2705 device_remove_file(dev->dev, attr);
2706 device_destroy(android_class, dev->dev->devt);
2707}
2708
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002709static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2710{
2711 struct android_dev *dev = NULL;
2712
2713 /* Find the android dev from the list */
2714 list_for_each_entry(dev, &android_dev_list, list_item) {
2715 if (dev->cdev == cdev)
2716 break;
2717 }
2718
2719 return dev;
2720}
2721
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002722static struct android_configuration *alloc_android_config
2723 (struct android_dev *dev)
2724{
2725 struct android_configuration *conf;
2726
2727 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2728 if (!conf) {
2729 pr_err("%s(): Failed to alloc memory for android conf\n",
2730 __func__);
2731 return ERR_PTR(-ENOMEM);
2732 }
2733
2734 dev->configs_num++;
2735 conf->usb_config.label = dev->name;
2736 conf->usb_config.unbind = android_unbind_config;
2737 conf->usb_config.bConfigurationValue = dev->configs_num;
2738
2739 INIT_LIST_HEAD(&conf->enabled_functions);
2740
2741 list_add_tail(&conf->list_item, &dev->configs);
2742
2743 return conf;
2744}
2745
2746static void free_android_config(struct android_dev *dev,
2747 struct android_configuration *conf)
2748{
2749 list_del(&conf->list_item);
2750 dev->configs_num--;
2751 kfree(conf);
2752}
2753
Manu Gautam43c61a12012-08-22 17:09:37 -07002754static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2755{
2756 struct dload_struct local_diag_dload = { 0 };
2757 int *src, *dst, i;
2758
2759 if (!diag_dload) {
2760 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2761 return -ENODEV;
2762 }
2763
2764 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2765 __func__, diag_dload, pid, snum);
2766
2767 /* update pid */
2768 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2769 local_diag_dload.pid = pid;
2770
2771 /* update serial number */
2772 if (!snum) {
2773 local_diag_dload.magic_struct.serial_num = 0;
2774 memset(&local_diag_dload.serial_number, 0,
2775 SERIAL_NUMBER_LENGTH);
2776 } else {
2777 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2778 strlcpy((char *)&local_diag_dload.serial_number, snum,
2779 SERIAL_NUMBER_LENGTH);
2780 }
2781
2782 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2783 src = (int *)&local_diag_dload;
2784 dst = (int *)diag_dload;
2785
2786 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2787 *dst++ = *src++;
2788
2789 return 0;
2790}
2791
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002792static int __devinit android_probe(struct platform_device *pdev)
2793{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302794 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002795 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002796 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002797 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002798
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302799 if (pdev->dev.of_node) {
2800 dev_dbg(&pdev->dev, "device tree enabled\n");
2801 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2802 if (!pdata) {
2803 pr_err("unable to allocate platform data\n");
2804 return -ENOMEM;
2805 }
2806
2807 of_property_read_u32(pdev->dev.of_node,
2808 "qcom,android-usb-swfi-latency",
2809 &pdata->swfi_latency);
Chiranjeevi Velempati9d797662013-04-22 15:45:38 +05302810 pdata->cdrom = of_property_read_bool(pdev->dev.of_node,
2811 "qcom,android-usb-cdrom");
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302812 } else {
2813 pdata = pdev->dev.platform_data;
2814 }
2815
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002816 if (!android_class) {
2817 android_class = class_create(THIS_MODULE, "android_usb");
2818 if (IS_ERR(android_class))
2819 return PTR_ERR(android_class);
2820 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002821
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002822 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2823 if (!android_dev) {
2824 pr_err("%s(): Failed to alloc memory for android_dev\n",
2825 __func__);
2826 ret = -ENOMEM;
2827 goto err_alloc;
2828 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002829
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002830 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002831 android_dev->disable_depth = 1;
2832 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002833 android_dev->configs_num = 0;
2834 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002835 INIT_WORK(&android_dev->work, android_work);
2836 mutex_init(&android_dev->mutex);
2837
2838 android_dev->pdata = pdata;
2839
2840 list_add_tail(&android_dev->list_item, &android_dev_list);
2841 android_dev_count++;
2842
2843 if (pdata)
2844 composite_driver.usb_core_id = pdata->usb_core_id;
2845 else
2846 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2847
Manu Gautam43c61a12012-08-22 17:09:37 -07002848 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2849 if (res) {
2850 diag_dload = devm_ioremap(&pdev->dev, res->start,
2851 resource_size(res));
2852 if (!diag_dload) {
2853 dev_err(&pdev->dev, "ioremap failed\n");
2854 ret = -ENOMEM;
2855 goto err_dev;
2856 }
2857 } else {
2858 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2859 }
2860
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002861 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302862 if (ret) {
2863 pr_err("%s(): android_create_device failed\n", __func__);
2864 goto err_dev;
2865 }
2866
Lena Salmand092f2d2012-03-12 17:27:24 +02002867 ret = usb_composite_probe(&android_usb_driver, android_bind);
2868 if (ret) {
2869 pr_err("%s(): Failed to register android "
2870 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302871 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002872 }
2873
Ofir Cohen94213a72012-05-03 14:26:32 +03002874 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302875 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002876 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002877 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002878 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002879
Lena Salmand092f2d2012-03-12 17:27:24 +02002880 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302881err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002882 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302883err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002884 list_del(&android_dev->list_item);
2885 android_dev_count--;
2886 kfree(android_dev);
2887err_alloc:
2888 if (list_empty(&android_dev_list)) {
2889 class_destroy(android_class);
2890 android_class = NULL;
2891 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302892 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002893}
2894
2895static int android_remove(struct platform_device *pdev)
2896{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002897 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002898 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002899 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302900
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002901 if (pdata)
2902 usb_core_id = pdata->usb_core_id;
2903
2904 /* Find the android dev from the list */
2905 list_for_each_entry(dev, &android_dev_list, list_item) {
2906 if (!dev->pdata)
2907 break; /*To backward compatibility*/
2908 if (dev->pdata->usb_core_id == usb_core_id)
2909 break;
2910 }
2911
2912 if (dev) {
2913 android_destroy_device(dev);
2914 if (pdata && pdata->swfi_latency)
2915 pm_qos_remove_request(&dev->pm_qos_req_dma);
2916 list_del(&dev->list_item);
2917 android_dev_count--;
2918 kfree(dev);
2919 }
2920
2921 if (list_empty(&android_dev_list)) {
2922 class_destroy(android_class);
2923 android_class = NULL;
2924 usb_composite_unregister(&android_usb_driver);
2925 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002926
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002927 return 0;
2928}
2929
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002930static const struct platform_device_id android_id_table[] __devinitconst = {
2931 {
2932 .name = "android_usb",
2933 },
2934 {
2935 .name = "android_usb_hsic",
2936 },
2937};
2938
Manu Gautam43c61a12012-08-22 17:09:37 -07002939static struct of_device_id usb_android_dt_match[] = {
2940 { .compatible = "qcom,android-usb",
2941 },
2942 {}
2943};
2944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002945static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07002946 .driver = {
2947 .name = "android_usb",
2948 .of_match_table = usb_android_dt_match,
2949 },
Lena Salmand092f2d2012-03-12 17:27:24 +02002950 .probe = android_probe,
2951 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002952 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002953};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002954
2955static int __init init(void)
2956{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302957 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002958
Benoit Goby1e8ce152011-12-12 13:01:23 -08002959 /* Override composite driver functions */
2960 composite_driver.setup = android_setup;
2961 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002962 composite_driver.suspend = android_suspend;
2963 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002964
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002965 INIT_LIST_HEAD(&android_dev_list);
2966 android_dev_count = 0;
2967
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302968 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302969 if (ret) {
2970 pr_err("%s(): Failed to register android"
2971 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302972 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002973
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302974 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002975}
2976module_init(init);
2977
2978static void __exit cleanup(void)
2979{
Lena Salmand092f2d2012-03-12 17:27:24 +02002980 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002981}
2982module_exit(cleanup);