blob: 38b1967fd2be554943b476077d16af9c0e74e16a [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
Hemant Kumarc2b17782013-02-03 15:56:29 -0800674/*rmnet transport name string - "rmnet_hsic[,rmnet_hsusb]" */
675static char rmnet_xport_names[MAX_XPORT_STR_LEN];
676
Manu Gautame3e897c2011-09-12 17:18:46 +0530677static void rmnet_function_cleanup(struct android_usb_function *f)
678{
679 frmnet_cleanup();
680}
681
Manu Gautam2b0234a2011-09-07 16:47:52 +0530682static int rmnet_function_bind_config(struct android_usb_function *f,
683 struct usb_configuration *c)
684{
685 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700686 int err = 0;
687 char *ctrl_name;
688 char *data_name;
Hemant Kumarc2b17782013-02-03 15:56:29 -0800689 char *tname = NULL;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700690 char buf[MAX_XPORT_STR_LEN], *b;
Hemant Kumarc2b17782013-02-03 15:56:29 -0800691 char xport_name_buf[MAX_XPORT_STR_LEN], *tb;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700692 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530693
Hemant Kumar1b820d52011-11-03 15:08:28 -0700694 if (!rmnet_initialized) {
695 rmnet_initialized = 1;
696 strlcpy(buf, rmnet_transports, sizeof(buf));
697 b = strim(buf);
Hemant Kumarc2b17782013-02-03 15:56:29 -0800698
699 strlcpy(xport_name_buf, rmnet_xport_names,
700 sizeof(xport_name_buf));
701 tb = strim(xport_name_buf);
702
Hemant Kumar1b820d52011-11-03 15:08:28 -0700703 while (b) {
704 ctrl_name = strsep(&b, ",");
705 data_name = strsep(&b, ",");
706 if (ctrl_name && data_name) {
Hemant Kumarc2b17782013-02-03 15:56:29 -0800707 if (tb)
708 tname = strsep(&tb, ",");
709 err = frmnet_init_port(ctrl_name, data_name,
710 tname);
Hemant Kumar1b820d52011-11-03 15:08:28 -0700711 if (err) {
712 pr_err("rmnet: Cannot open ctrl port:"
713 "'%s' data port:'%s'\n",
714 ctrl_name, data_name);
715 goto out;
716 }
717 ports++;
718 }
719 }
720
721 err = rmnet_gport_setup();
722 if (err) {
723 pr_err("rmnet: Cannot setup transports");
724 goto out;
725 }
726 }
727
728 for (i = 0; i < ports; i++) {
729 err = frmnet_bind_config(c, i);
730 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530731 pr_err("Could not bind rmnet%u config\n", i);
732 break;
733 }
734 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700735out:
736 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530737}
738
Hemant Kumar1b820d52011-11-03 15:08:28 -0700739static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530740 struct device_attribute *attr, char *buf)
741{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700742 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530743}
744
Hemant Kumar1b820d52011-11-03 15:08:28 -0700745static ssize_t rmnet_transports_store(
746 struct device *device, struct device_attribute *attr,
747 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530748{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700749 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530750
Manu Gautam2b0234a2011-09-07 16:47:52 +0530751 return size;
752}
753
Hemant Kumarc2b17782013-02-03 15:56:29 -0800754static ssize_t rmnet_xport_names_show(struct device *dev,
755 struct device_attribute *attr, char *buf)
756{
757 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_xport_names);
758}
759
760static ssize_t rmnet_xport_names_store(
761 struct device *device, struct device_attribute *attr,
762 const char *buff, size_t size)
763{
764 strlcpy(rmnet_xport_names, buff, sizeof(rmnet_xport_names));
765
766 return size;
767}
768
Hemant Kumar1b820d52011-11-03 15:08:28 -0700769static struct device_attribute dev_attr_rmnet_transports =
770 __ATTR(transports, S_IRUGO | S_IWUSR,
771 rmnet_transports_show,
772 rmnet_transports_store);
Hemant Kumarc2b17782013-02-03 15:56:29 -0800773
774static struct device_attribute dev_attr_rmnet_xport_names =
775 __ATTR(transport_names, S_IRUGO | S_IWUSR,
776 rmnet_xport_names_show,
777 rmnet_xport_names_store);
778
Manu Gautam2b0234a2011-09-07 16:47:52 +0530779static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700780 &dev_attr_rmnet_transports,
Hemant Kumarc2b17782013-02-03 15:56:29 -0800781 &dev_attr_rmnet_xport_names,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700782 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530783
784static struct android_usb_function rmnet_function = {
785 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530786 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530787 .bind_config = rmnet_function_bind_config,
788 .attributes = rmnet_function_attributes,
789};
790
Bar Weinered1059e2013-04-16 11:23:55 +0300791static void gps_function_cleanup(struct android_usb_function *f)
792{
793 gps_cleanup();
794}
795
796static int gps_function_bind_config(struct android_usb_function *f,
797 struct usb_configuration *c)
798{
799 int err;
800 static int gps_initialized;
801
802 if (!gps_initialized) {
803 gps_initialized = 1;
804 err = gps_init_port();
805 if (err) {
806 pr_err("gps: Cannot init gps port");
807 return err;
808 }
809 }
810
811 err = gps_gport_setup();
812 if (err) {
813 pr_err("gps: Cannot setup transports");
814 return err;
815 }
816 err = gps_bind_config(c);
817 if (err) {
818 pr_err("Could not bind gps config\n");
819 return err;
820 }
821
822 return 0;
823}
824
825static struct android_usb_function gps_function = {
826 .name = "gps",
827 .cleanup = gps_function_cleanup,
828 .bind_config = gps_function_bind_config,
829};
830
Vamsi Krishna932c9de2013-05-22 12:18:05 -0700831/* ncm */
832struct ncm_function_config {
833 u8 ethaddr[ETH_ALEN];
834};
835static int
836ncm_function_init(struct android_usb_function *f, struct usb_composite_dev *c)
837{
838 f->config = kzalloc(sizeof(struct ncm_function_config), GFP_KERNEL);
839 if (!f->config)
840 return -ENOMEM;
Bar Weinered1059e2013-04-16 11:23:55 +0300841
Vamsi Krishna932c9de2013-05-22 12:18:05 -0700842 return 0;
843}
844
845static void ncm_function_cleanup(struct android_usb_function *f)
846{
847 kfree(f->config);
848 f->config = NULL;
849}
850
851static int
852ncm_function_bind_config(struct android_usb_function *f,
853 struct usb_configuration *c)
854{
855 struct ncm_function_config *ncm = f->config;
856 int ret;
857
858 if (!ncm) {
859 pr_err("%s: ncm config is null\n", __func__);
860 return -EINVAL;
861 }
862
863 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
864 ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
865 ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
866
867 ret = gether_setup_name(c->cdev->gadget, ncm->ethaddr, "ncm");
868 if (ret) {
869 pr_err("%s: gether setup failed err:%d\n", __func__, ret);
870 return ret;
871 }
872
873 ret = ncm_bind_config(c, ncm->ethaddr);
874 if (ret) {
875 pr_err("%s: ncm bind config failed err:%d", __func__, ret);
876 gether_cleanup();
877 return ret;
878 }
879
880 return ret;
881}
882
883static void ncm_function_unbind_config(struct android_usb_function *f,
884 struct usb_configuration *c)
885{
886 gether_cleanup();
887}
888
889static ssize_t ncm_ethaddr_show(struct device *dev,
890 struct device_attribute *attr, char *buf)
891{
892 struct android_usb_function *f = dev_get_drvdata(dev);
893 struct ncm_function_config *ncm = f->config;
894 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
895 ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
896 ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
897}
898
899static ssize_t ncm_ethaddr_store(struct device *dev,
900 struct device_attribute *attr, const char *buf, size_t size)
901{
902 struct android_usb_function *f = dev_get_drvdata(dev);
903 struct ncm_function_config *ncm = f->config;
904
905 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
906 (int *)&ncm->ethaddr[0], (int *)&ncm->ethaddr[1],
907 (int *)&ncm->ethaddr[2], (int *)&ncm->ethaddr[3],
908 (int *)&ncm->ethaddr[4], (int *)&ncm->ethaddr[5]) == 6)
909 return size;
910 return -EINVAL;
911}
912
913static DEVICE_ATTR(ncm_ethaddr, S_IRUGO | S_IWUSR, ncm_ethaddr_show,
914 ncm_ethaddr_store);
915static struct device_attribute *ncm_function_attributes[] = {
916 &dev_attr_ncm_ethaddr,
917 NULL
918};
919
920static struct android_usb_function ncm_function = {
921 .name = "ncm",
922 .init = ncm_function_init,
923 .cleanup = ncm_function_cleanup,
924 .bind_config = ncm_function_bind_config,
925 .unbind_config = ncm_function_unbind_config,
926 .attributes = ncm_function_attributes,
927};
Amit Blayf9b352b2013-03-04 15:01:40 +0200928/* ecm transport string */
929static char ecm_transports[MAX_XPORT_STR_LEN];
930
Ofir Cohen7b155422012-07-31 13:02:49 +0300931struct ecm_function_config {
932 u8 ethaddr[ETH_ALEN];
933};
934
935static int ecm_function_init(struct android_usb_function *f,
936 struct usb_composite_dev *cdev)
937{
938 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
939 if (!f->config)
940 return -ENOMEM;
941 return 0;
942}
943
944static void ecm_function_cleanup(struct android_usb_function *f)
945{
946 kfree(f->config);
947 f->config = NULL;
948}
949
950static int ecm_qc_function_bind_config(struct android_usb_function *f,
951 struct usb_configuration *c)
952{
953 int ret;
Amit Blayf9b352b2013-03-04 15:01:40 +0200954 char *trans;
Ofir Cohen7b155422012-07-31 13:02:49 +0300955 struct ecm_function_config *ecm = f->config;
956
957 if (!ecm) {
958 pr_err("%s: ecm_pdata\n", __func__);
959 return -EINVAL;
960 }
961
962 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
963 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
964 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
965
Amit Blayf9b352b2013-03-04 15:01:40 +0200966 pr_debug("%s: ecm_transport is %s", __func__, ecm_transports);
967
968 trans = strim(ecm_transports);
969 if (strcmp("BAM2BAM_IPA", trans)) {
970 ret = gether_qc_setup_name(c->cdev->gadget,
971 ecm->ethaddr, "ecm");
972 if (ret) {
973 pr_err("%s: gether_setup failed\n", __func__);
974 return ret;
975 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300976 }
977
Amit Blayf9b352b2013-03-04 15:01:40 +0200978 return ecm_qc_bind_config(c, ecm->ethaddr, trans);
Ofir Cohen7b155422012-07-31 13:02:49 +0300979}
980
981static void ecm_qc_function_unbind_config(struct android_usb_function *f,
982 struct usb_configuration *c)
983{
Amit Blayf9b352b2013-03-04 15:01:40 +0200984 char *trans = strim(ecm_transports);
985
986 if (strcmp("BAM2BAM_IPA", trans))
987 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300988}
989
990static ssize_t ecm_ethaddr_show(struct device *dev,
991 struct device_attribute *attr, char *buf)
992{
993 struct android_usb_function *f = dev_get_drvdata(dev);
994 struct ecm_function_config *ecm = f->config;
995 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
996 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
997 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
998}
999
1000static ssize_t ecm_ethaddr_store(struct device *dev,
1001 struct device_attribute *attr, const char *buf, size_t size)
1002{
1003 struct android_usb_function *f = dev_get_drvdata(dev);
1004 struct ecm_function_config *ecm = f->config;
1005
1006 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1007 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1008 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1009 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1010 return size;
1011 return -EINVAL;
1012}
1013
1014static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1015 ecm_ethaddr_store);
1016
Amit Blayf9b352b2013-03-04 15:01:40 +02001017static ssize_t ecm_transports_show(struct device *dev,
1018 struct device_attribute *attr, char *buf)
1019{
1020 return snprintf(buf, PAGE_SIZE, "%s\n", ecm_transports);
1021}
1022
1023static ssize_t ecm_transports_store(struct device *dev,
1024 struct device_attribute *attr, const char *buf, size_t size)
1025{
1026 strlcpy(ecm_transports, buf, sizeof(ecm_transports));
1027 return size;
1028}
1029
1030static DEVICE_ATTR(ecm_transports, S_IRUGO | S_IWUSR, ecm_transports_show,
1031 ecm_transports_store);
1032
Ofir Cohen7b155422012-07-31 13:02:49 +03001033static struct device_attribute *ecm_function_attributes[] = {
Amit Blayf9b352b2013-03-04 15:01:40 +02001034 &dev_attr_ecm_transports,
Ofir Cohen7b155422012-07-31 13:02:49 +03001035 &dev_attr_ecm_ethaddr,
1036 NULL
1037};
1038
1039static struct android_usb_function ecm_qc_function = {
1040 .name = "ecm_qc",
1041 .init = ecm_function_init,
1042 .cleanup = ecm_function_cleanup,
1043 .bind_config = ecm_qc_function_bind_config,
1044 .unbind_config = ecm_qc_function_unbind_config,
1045 .attributes = ecm_function_attributes,
1046};
Anna Perela8c991d2012-04-09 16:44:46 +03001047
1048/* MBIM - used with BAM */
1049#define MAX_MBIM_INSTANCES 1
1050
1051static int mbim_function_init(struct android_usb_function *f,
1052 struct usb_composite_dev *cdev)
1053{
1054 return mbim_init(MAX_MBIM_INSTANCES);
1055}
1056
1057static void mbim_function_cleanup(struct android_usb_function *f)
1058{
1059 fmbim_cleanup();
1060}
1061
Lena Salmandf7e7992013-03-15 09:46:27 +02001062
1063/* mbim transport string */
1064static char mbim_transports[MAX_XPORT_STR_LEN];
1065
Anna Perela8c991d2012-04-09 16:44:46 +03001066static int mbim_function_bind_config(struct android_usb_function *f,
1067 struct usb_configuration *c)
1068{
Lena Salmandf7e7992013-03-15 09:46:27 +02001069 char *trans;
1070
1071 pr_debug("%s: mbim transport is %s", __func__, mbim_transports);
1072 trans = strim(mbim_transports);
1073 return mbim_bind_config(c, 0, trans);
Anna Perela8c991d2012-04-09 16:44:46 +03001074}
1075
Jack Pham2df2f702012-10-11 19:08:24 -07001076static int mbim_function_ctrlrequest(struct android_usb_function *f,
1077 struct usb_composite_dev *cdev,
1078 const struct usb_ctrlrequest *c)
1079{
1080 return mbim_ctrlrequest(cdev, c);
1081}
1082
Lena Salmandf7e7992013-03-15 09:46:27 +02001083static ssize_t mbim_transports_show(struct device *dev,
1084 struct device_attribute *attr, char *buf)
1085{
1086 return snprintf(buf, PAGE_SIZE, "%s\n", mbim_transports);
1087}
1088
1089static ssize_t mbim_transports_store(struct device *dev,
1090 struct device_attribute *attr, const char *buf, size_t size)
1091{
1092 strlcpy(mbim_transports, buf, sizeof(mbim_transports));
1093 return size;
1094}
1095
1096static DEVICE_ATTR(mbim_transports, S_IRUGO | S_IWUSR, mbim_transports_show,
1097 mbim_transports_store);
1098
1099static struct device_attribute *mbim_function_attributes[] = {
1100 &dev_attr_mbim_transports,
1101 NULL
1102};
1103
Anna Perela8c991d2012-04-09 16:44:46 +03001104static struct android_usb_function mbim_function = {
1105 .name = "usb_mbim",
1106 .cleanup = mbim_function_cleanup,
1107 .bind_config = mbim_function_bind_config,
1108 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -07001109 .ctrlrequest = mbim_function_ctrlrequest,
Lena Salmandf7e7992013-03-15 09:46:27 +02001110 .attributes = mbim_function_attributes,
Anna Perela8c991d2012-04-09 16:44:46 +03001111};
1112
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001113#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001114/* PERIPHERAL AUDIO */
1115static int audio_function_bind_config(struct android_usb_function *f,
1116 struct usb_configuration *c)
1117{
1118 return audio_bind_config(c);
1119}
1120
1121static struct android_usb_function audio_function = {
1122 .name = "audio",
1123 .bind_config = audio_function_bind_config,
1124};
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001125#endif
Anna Perel432367a2012-09-20 10:55:32 +03001126
Anna Perela8c991d2012-04-09 16:44:46 +03001127
Manu Gautam8e0719b2011-09-26 14:47:55 +05301128/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301129static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130static ssize_t clients_store(
1131 struct device *device, struct device_attribute *attr,
1132 const char *buff, size_t size)
1133{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301134 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135
1136 return size;
1137}
1138
1139static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
1140static struct device_attribute *diag_function_attributes[] =
1141 { &dev_attr_clients, NULL };
1142
1143static int diag_function_init(struct android_usb_function *f,
1144 struct usb_composite_dev *cdev)
1145{
1146 return diag_setup();
1147}
1148
1149static void diag_function_cleanup(struct android_usb_function *f)
1150{
1151 diag_cleanup();
1152}
1153
1154static int diag_function_bind_config(struct android_usb_function *f,
1155 struct usb_configuration *c)
1156{
1157 char *name;
1158 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +05301159 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -08001160 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001161 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301163 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 b = strim(buf);
1165
1166 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -08001167 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +05301169 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -07001170 if (!once++) {
1171 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
1172 notify = dev->pdata->update_pid_and_serial_num;
1173 else
1174 notify = usb_diag_update_pid_and_serial_num;
1175 }
Manu Gautamc5760302011-08-25 14:30:24 +05301176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +05301178 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 if (err)
1180 pr_err("diag: Cannot open channel '%s'", name);
1181 }
1182 }
1183
1184 return err;
1185}
1186
1187static struct android_usb_function diag_function = {
1188 .name = "diag",
1189 .init = diag_function_init,
1190 .cleanup = diag_function_cleanup,
1191 .bind_config = diag_function_bind_config,
1192 .attributes = diag_function_attributes,
1193};
1194
Shimrit Malichia00d7322012-08-05 13:56:28 +03001195/* DEBUG */
1196static int qdss_function_init(struct android_usb_function *f,
1197 struct usb_composite_dev *cdev)
1198{
1199 return qdss_setup();
1200}
1201
1202static void qdss_function_cleanup(struct android_usb_function *f)
1203{
1204 qdss_cleanup();
1205}
1206
1207static int qdss_function_bind_config(struct android_usb_function *f,
1208 struct usb_configuration *c)
1209{
1210 int err = -1;
1211
1212 err = qdss_bind_config(c, "qdss");
1213 if (err)
1214 pr_err("qdss: Cannot open channel qdss");
1215
1216 return err;
1217}
1218
1219static struct android_usb_function qdss_function = {
1220 .name = "qdss",
1221 .init = qdss_function_init,
1222 .cleanup = qdss_function_cleanup,
1223 .bind_config = qdss_function_bind_config,
1224};
1225
Manu Gautam8e0719b2011-09-26 14:47:55 +05301226/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301227static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +05301228static ssize_t serial_transports_store(
1229 struct device *device, struct device_attribute *attr,
1230 const char *buff, size_t size)
1231{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301232 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +05301233
1234 return size;
1235}
1236
Hemant Kumarc2b17782013-02-03 15:56:29 -08001237/*enabled FSERIAL transport names - "serial_hsic[,serial_hsusb]"*/
1238static char serial_xport_names[32];
1239static ssize_t serial_xport_names_store(
1240 struct device *device, struct device_attribute *attr,
1241 const char *buff, size_t size)
1242{
1243 strlcpy(serial_xport_names, buff, sizeof(serial_xport_names));
1244
1245 return size;
1246}
1247
1248static ssize_t serial_xport_names_show(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1250{
1251 return snprintf(buf, PAGE_SIZE, "%s\n", serial_xport_names);
1252}
1253
Manu Gautama4d993f2011-08-30 18:25:55 +05301254static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
Hemant Kumarc2b17782013-02-03 15:56:29 -08001255static struct device_attribute dev_attr_serial_xport_names =
1256 __ATTR(transport_names, S_IRUGO | S_IWUSR,
1257 serial_xport_names_show,
1258 serial_xport_names_store);
1259
1260static struct device_attribute *serial_function_attributes[] = {
1261 &dev_attr_transports,
1262 &dev_attr_serial_xport_names,
1263 NULL };
Manu Gautama4d993f2011-08-30 18:25:55 +05301264
1265static void serial_function_cleanup(struct android_usb_function *f)
1266{
1267 gserial_cleanup();
1268}
1269
1270static int serial_function_bind_config(struct android_usb_function *f,
1271 struct usb_configuration *c)
1272{
Hemant Kumarc2b17782013-02-03 15:56:29 -08001273 char *name, *xport_name = NULL;
1274 char buf[32], *b, xport_name_buf[32], *tb;
Manu Gautama4d993f2011-08-30 18:25:55 +05301275 int err = -1, i;
1276 static int serial_initialized = 0, ports = 0;
1277
1278 if (serial_initialized)
1279 goto bind_config;
1280
1281 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301282 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +05301283 b = strim(buf);
1284
Hemant Kumarc2b17782013-02-03 15:56:29 -08001285 strlcpy(xport_name_buf, serial_xport_names, sizeof(xport_name_buf));
1286 tb = strim(xport_name_buf);
1287
Manu Gautama4d993f2011-08-30 18:25:55 +05301288 while (b) {
1289 name = strsep(&b, ",");
1290
1291 if (name) {
Hemant Kumarc2b17782013-02-03 15:56:29 -08001292 if (tb)
1293 xport_name = strsep(&tb, ",");
1294 err = gserial_init_port(ports, name, xport_name);
Manu Gautama4d993f2011-08-30 18:25:55 +05301295 if (err) {
1296 pr_err("serial: Cannot open port '%s'", name);
1297 goto out;
1298 }
1299 ports++;
1300 }
1301 }
1302 err = gport_setup(c);
1303 if (err) {
1304 pr_err("serial: Cannot setup transports");
1305 goto out;
1306 }
1307
1308bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +02001309 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +05301310 err = gser_bind_config(c, i);
1311 if (err) {
1312 pr_err("serial: bind_config failed for port %d", i);
1313 goto out;
1314 }
1315 }
1316
1317out:
1318 return err;
1319}
1320
1321static struct android_usb_function serial_function = {
1322 .name = "serial",
1323 .cleanup = serial_function_cleanup,
1324 .bind_config = serial_function_bind_config,
1325 .attributes = serial_function_attributes,
1326};
1327
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301328/* CCID */
1329static int ccid_function_init(struct android_usb_function *f,
1330 struct usb_composite_dev *cdev)
1331{
1332 return ccid_setup();
1333}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001334
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301335static void ccid_function_cleanup(struct android_usb_function *f)
1336{
1337 ccid_cleanup();
1338}
1339
1340static int ccid_function_bind_config(struct android_usb_function *f,
1341 struct usb_configuration *c)
1342{
1343 return ccid_bind_config(c);
1344}
1345
1346static struct android_usb_function ccid_function = {
1347 .name = "ccid",
1348 .init = ccid_function_init,
1349 .cleanup = ccid_function_cleanup,
1350 .bind_config = ccid_function_bind_config,
1351};
1352
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001353static int
1354mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001355 struct usb_composite_dev *cdev)
1356{
1357 return mtp_setup();
1358}
1359
1360static void mtp_function_cleanup(struct android_usb_function *f)
1361{
1362 mtp_cleanup();
1363}
1364
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001365static int
1366mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001367 struct usb_configuration *c)
1368{
1369 return mtp_bind_config(c, false);
1370}
1371
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001372static int
1373ptp_function_init(struct android_usb_function *f,
1374 struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001375{
1376 /* nothing to do - initialization is handled by mtp_function_init */
1377 return 0;
1378}
1379
1380static void ptp_function_cleanup(struct android_usb_function *f)
1381{
1382 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1383}
1384
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001385static int
1386ptp_function_bind_config(struct android_usb_function *f,
1387 struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001388{
1389 return mtp_bind_config(c, true);
1390}
1391
1392static int mtp_function_ctrlrequest(struct android_usb_function *f,
1393 struct usb_composite_dev *cdev,
1394 const struct usb_ctrlrequest *c)
1395{
1396 return mtp_ctrlrequest(cdev, c);
1397}
1398
1399static struct android_usb_function mtp_function = {
1400 .name = "mtp",
1401 .init = mtp_function_init,
1402 .cleanup = mtp_function_cleanup,
1403 .bind_config = mtp_function_bind_config,
1404 .ctrlrequest = mtp_function_ctrlrequest,
1405};
1406
1407/* PTP function is same as MTP with slightly different interface descriptor */
1408static struct android_usb_function ptp_function = {
1409 .name = "ptp",
1410 .init = ptp_function_init,
1411 .cleanup = ptp_function_cleanup,
1412 .bind_config = ptp_function_bind_config,
1413};
1414
1415
Benoit Goby1e8ce152011-12-12 13:01:23 -08001416struct rndis_function_config {
1417 u8 ethaddr[ETH_ALEN];
1418 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001419 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001420 char manufacturer[256];
1421 /* "Wireless" RNDIS; auto-detected by Windows */
1422 bool wceis;
1423};
1424
1425static int
1426rndis_function_init(struct android_usb_function *f,
1427 struct usb_composite_dev *cdev)
1428{
1429 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1430 if (!f->config)
1431 return -ENOMEM;
1432 return 0;
1433}
1434
1435static void rndis_function_cleanup(struct android_usb_function *f)
1436{
1437 kfree(f->config);
1438 f->config = NULL;
1439}
1440
Ofir Cohenaef90b72012-07-31 12:37:04 +02001441static int rndis_qc_function_init(struct android_usb_function *f,
1442 struct usb_composite_dev *cdev)
1443{
1444 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1445 if (!f->config)
1446 return -ENOMEM;
1447
1448 return rndis_qc_init();
1449}
1450
1451static void rndis_qc_function_cleanup(struct android_usb_function *f)
1452{
1453 rndis_qc_cleanup();
1454 kfree(f->config);
1455}
1456
Benoit Goby1e8ce152011-12-12 13:01:23 -08001457static int
1458rndis_function_bind_config(struct android_usb_function *f,
1459 struct usb_configuration *c)
1460{
1461 int ret;
1462 struct rndis_function_config *rndis = f->config;
1463
1464 if (!rndis) {
1465 pr_err("%s: rndis_pdata\n", __func__);
1466 return -1;
1467 }
1468
1469 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1470 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1471 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1472
1473 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1474 if (ret) {
1475 pr_err("%s: gether_setup failed\n", __func__);
1476 return ret;
1477 }
1478
1479 if (rndis->wceis) {
1480 /* "Wireless" RNDIS; auto-detected by Windows */
1481 rndis_iad_descriptor.bFunctionClass =
1482 USB_CLASS_WIRELESS_CONTROLLER;
1483 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1484 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1485 rndis_control_intf.bInterfaceClass =
1486 USB_CLASS_WIRELESS_CONTROLLER;
1487 rndis_control_intf.bInterfaceSubClass = 0x01;
1488 rndis_control_intf.bInterfaceProtocol = 0x03;
1489 }
1490
1491 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1492 rndis->manufacturer);
1493}
1494
Ofir Cohenaef90b72012-07-31 12:37:04 +02001495static int rndis_qc_function_bind_config(struct android_usb_function *f,
1496 struct usb_configuration *c)
1497{
1498 int ret;
1499 struct rndis_function_config *rndis = f->config;
1500
1501 if (!rndis) {
1502 pr_err("%s: rndis_pdata\n", __func__);
1503 return -EINVAL;
1504 }
1505
1506 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1507 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1508 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1509
1510 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1511 if (ret) {
1512 pr_err("%s: gether_setup failed\n", __func__);
1513 return ret;
1514 }
1515
1516 if (rndis->wceis) {
1517 /* "Wireless" RNDIS; auto-detected by Windows */
1518 rndis_qc_iad_descriptor.bFunctionClass =
1519 USB_CLASS_WIRELESS_CONTROLLER;
1520 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1521 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1522 rndis_qc_control_intf.bInterfaceClass =
1523 USB_CLASS_WIRELESS_CONTROLLER;
1524 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1525 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1526 }
1527
1528 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1529 rndis->manufacturer,
1530 rndis->max_pkt_per_xfer);
1531}
1532
Benoit Goby1e8ce152011-12-12 13:01:23 -08001533static void rndis_function_unbind_config(struct android_usb_function *f,
1534 struct usb_configuration *c)
1535{
1536 gether_cleanup();
1537}
1538
Ofir Cohenaef90b72012-07-31 12:37:04 +02001539static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1540 struct usb_configuration *c)
1541{
Amit Blayd6d690a2012-10-16 13:37:42 +02001542 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001543}
1544
Benoit Goby1e8ce152011-12-12 13:01:23 -08001545static ssize_t rndis_manufacturer_show(struct device *dev,
1546 struct device_attribute *attr, char *buf)
1547{
1548 struct android_usb_function *f = dev_get_drvdata(dev);
1549 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001550
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301551 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001552}
1553
1554static ssize_t rndis_manufacturer_store(struct device *dev,
1555 struct device_attribute *attr, const char *buf, size_t size)
1556{
1557 struct android_usb_function *f = dev_get_drvdata(dev);
1558 struct rndis_function_config *config = f->config;
1559
1560 if (size >= sizeof(config->manufacturer))
1561 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001562
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301563 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001564 return size;
1565 return -1;
1566}
1567
1568static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1569 rndis_manufacturer_store);
1570
1571static ssize_t rndis_wceis_show(struct device *dev,
1572 struct device_attribute *attr, char *buf)
1573{
1574 struct android_usb_function *f = dev_get_drvdata(dev);
1575 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001576
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301577 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001578}
1579
1580static ssize_t rndis_wceis_store(struct device *dev,
1581 struct device_attribute *attr, const char *buf, size_t size)
1582{
1583 struct android_usb_function *f = dev_get_drvdata(dev);
1584 struct rndis_function_config *config = f->config;
1585 int value;
1586
1587 if (sscanf(buf, "%d", &value) == 1) {
1588 config->wceis = value;
1589 return size;
1590 }
1591 return -EINVAL;
1592}
1593
1594static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1595 rndis_wceis_store);
1596
1597static ssize_t rndis_ethaddr_show(struct device *dev,
1598 struct device_attribute *attr, char *buf)
1599{
1600 struct android_usb_function *f = dev_get_drvdata(dev);
1601 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001602
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301603 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001604 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1605 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1606}
1607
1608static ssize_t rndis_ethaddr_store(struct device *dev,
1609 struct device_attribute *attr, const char *buf, size_t size)
1610{
1611 struct android_usb_function *f = dev_get_drvdata(dev);
1612 struct rndis_function_config *rndis = f->config;
1613
1614 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1615 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1616 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1617 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1618 return size;
1619 return -EINVAL;
1620}
1621
1622static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1623 rndis_ethaddr_store);
1624
1625static ssize_t rndis_vendorID_show(struct device *dev,
1626 struct device_attribute *attr, char *buf)
1627{
1628 struct android_usb_function *f = dev_get_drvdata(dev);
1629 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001630
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301631 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001632}
1633
1634static ssize_t rndis_vendorID_store(struct device *dev,
1635 struct device_attribute *attr, const char *buf, size_t size)
1636{
1637 struct android_usb_function *f = dev_get_drvdata(dev);
1638 struct rndis_function_config *config = f->config;
1639 int value;
1640
1641 if (sscanf(buf, "%04x", &value) == 1) {
1642 config->vendorID = value;
1643 return size;
1644 }
1645 return -EINVAL;
1646}
1647
1648static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1649 rndis_vendorID_store);
1650
Ofir Cohenaef90b72012-07-31 12:37:04 +02001651static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1652 struct device_attribute *attr, char *buf)
1653{
1654 struct android_usb_function *f = dev_get_drvdata(dev);
1655 struct rndis_function_config *config = f->config;
1656 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1657}
1658
1659static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1660 struct device_attribute *attr, const char *buf, size_t size)
1661{
1662 struct android_usb_function *f = dev_get_drvdata(dev);
1663 struct rndis_function_config *config = f->config;
1664 int value;
1665
1666 if (sscanf(buf, "%d", &value) == 1) {
1667 config->max_pkt_per_xfer = value;
1668 return size;
1669 }
1670 return -EINVAL;
1671}
1672
1673static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1674 rndis_max_pkt_per_xfer_show,
1675 rndis_max_pkt_per_xfer_store);
1676
Benoit Goby1e8ce152011-12-12 13:01:23 -08001677static struct device_attribute *rndis_function_attributes[] = {
1678 &dev_attr_manufacturer,
1679 &dev_attr_wceis,
1680 &dev_attr_ethaddr,
1681 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001682 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001683 NULL
1684};
1685
1686static struct android_usb_function rndis_function = {
1687 .name = "rndis",
1688 .init = rndis_function_init,
1689 .cleanup = rndis_function_cleanup,
1690 .bind_config = rndis_function_bind_config,
1691 .unbind_config = rndis_function_unbind_config,
1692 .attributes = rndis_function_attributes,
1693};
1694
Ofir Cohenaef90b72012-07-31 12:37:04 +02001695static struct android_usb_function rndis_qc_function = {
1696 .name = "rndis_qc",
1697 .init = rndis_qc_function_init,
1698 .cleanup = rndis_qc_function_cleanup,
1699 .bind_config = rndis_qc_function_bind_config,
1700 .unbind_config = rndis_qc_function_unbind_config,
1701 .attributes = rndis_function_attributes,
1702};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001703
Anna Perelf9d01552012-11-20 15:56:32 +02001704static int ecm_function_bind_config(struct android_usb_function *f,
1705 struct usb_configuration *c)
1706{
1707 int ret;
1708 struct ecm_function_config *ecm = f->config;
1709
1710 if (!ecm) {
1711 pr_err("%s: ecm_pdata\n", __func__);
1712 return -EINVAL;
1713 }
1714
1715 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1716 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1717 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1718
1719 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1720 if (ret) {
1721 pr_err("%s: gether_setup failed\n", __func__);
1722 return ret;
1723 }
1724
1725 ret = ecm_bind_config(c, ecm->ethaddr);
1726 if (ret) {
1727 pr_err("%s: ecm_bind_config failed\n", __func__);
1728 gether_cleanup();
1729 }
1730 return ret;
1731}
1732
1733static void ecm_function_unbind_config(struct android_usb_function *f,
1734 struct usb_configuration *c)
1735{
1736 gether_cleanup();
1737}
1738
1739static struct android_usb_function ecm_function = {
1740 .name = "ecm",
1741 .init = ecm_function_init,
1742 .cleanup = ecm_function_cleanup,
1743 .bind_config = ecm_function_bind_config,
1744 .unbind_config = ecm_function_unbind_config,
1745 .attributes = ecm_function_attributes,
1746};
1747
Benoit Goby1e8ce152011-12-12 13:01:23 -08001748struct mass_storage_function_config {
1749 struct fsg_config fsg;
1750 struct fsg_common *common;
1751};
1752
1753static int mass_storage_function_init(struct android_usb_function *f,
1754 struct usb_composite_dev *cdev)
1755{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001756 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001757 struct mass_storage_function_config *config;
1758 struct fsg_common *common;
1759 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301760 int i;
1761 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001762
1763 config = kzalloc(sizeof(struct mass_storage_function_config),
1764 GFP_KERNEL);
1765 if (!config)
1766 return -ENOMEM;
1767
1768 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301769 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301770 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301771 config->fsg.nluns = 2;
1772 config->fsg.luns[1].cdrom = 1;
1773 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301774 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301775 name[1] = "lun0";
1776 }
1777
Benoit Goby1e8ce152011-12-12 13:01:23 -08001778 config->fsg.luns[0].removable = 1;
1779
1780 common = fsg_common_init(NULL, cdev, &config->fsg);
1781 if (IS_ERR(common)) {
1782 kfree(config);
1783 return PTR_ERR(common);
1784 }
1785
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301786 for (i = 0; i < config->fsg.nluns; i++) {
1787 err = sysfs_create_link(&f->dev->kobj,
1788 &common->luns[i].dev.kobj,
1789 name[i]);
1790 if (err)
1791 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001792 }
1793
1794 config->common = common;
1795 f->config = config;
1796 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301797error:
1798 for (; i > 0 ; i--)
1799 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1800
1801 fsg_common_release(&common->ref);
1802 kfree(config);
1803 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001804}
1805
1806static void mass_storage_function_cleanup(struct android_usb_function *f)
1807{
1808 kfree(f->config);
1809 f->config = NULL;
1810}
1811
1812static int mass_storage_function_bind_config(struct android_usb_function *f,
1813 struct usb_configuration *c)
1814{
1815 struct mass_storage_function_config *config = f->config;
1816 return fsg_bind_config(c->cdev, c, config->common);
1817}
1818
1819static ssize_t mass_storage_inquiry_show(struct device *dev,
1820 struct device_attribute *attr, char *buf)
1821{
1822 struct android_usb_function *f = dev_get_drvdata(dev);
1823 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301824 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001825}
1826
1827static ssize_t mass_storage_inquiry_store(struct device *dev,
1828 struct device_attribute *attr, const char *buf, size_t size)
1829{
1830 struct android_usb_function *f = dev_get_drvdata(dev);
1831 struct mass_storage_function_config *config = f->config;
1832 if (size >= sizeof(config->common->inquiry_string))
1833 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301834 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001835 return -EINVAL;
1836 return size;
1837}
1838
1839static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1840 mass_storage_inquiry_show,
1841 mass_storage_inquiry_store);
1842
1843static struct device_attribute *mass_storage_function_attributes[] = {
1844 &dev_attr_inquiry_string,
1845 NULL
1846};
1847
1848static struct android_usb_function mass_storage_function = {
1849 .name = "mass_storage",
1850 .init = mass_storage_function_init,
1851 .cleanup = mass_storage_function_cleanup,
1852 .bind_config = mass_storage_function_bind_config,
1853 .attributes = mass_storage_function_attributes,
1854};
1855
1856
Benoit Gobycf3fc062011-12-19 14:39:37 -08001857static int accessory_function_init(struct android_usb_function *f,
1858 struct usb_composite_dev *cdev)
1859{
1860 return acc_setup();
1861}
1862
1863static void accessory_function_cleanup(struct android_usb_function *f)
1864{
1865 acc_cleanup();
1866}
1867
1868static int accessory_function_bind_config(struct android_usb_function *f,
1869 struct usb_configuration *c)
1870{
1871 return acc_bind_config(c);
1872}
1873
1874static int accessory_function_ctrlrequest(struct android_usb_function *f,
1875 struct usb_composite_dev *cdev,
1876 const struct usb_ctrlrequest *c)
1877{
1878 return acc_ctrlrequest(cdev, c);
1879}
1880
1881static struct android_usb_function accessory_function = {
1882 .name = "accessory",
1883 .init = accessory_function_init,
1884 .cleanup = accessory_function_cleanup,
1885 .bind_config = accessory_function_bind_config,
1886 .ctrlrequest = accessory_function_ctrlrequest,
1887};
1888
Anna Perel3ee23dd2013-02-26 16:06:40 +02001889#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301890static int audio_source_function_init(struct android_usb_function *f,
1891 struct usb_composite_dev *cdev)
1892{
1893 struct audio_source_config *config;
1894
1895 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1896 if (!config)
1897 return -ENOMEM;
1898 config->card = -1;
1899 config->device = -1;
1900 f->config = config;
1901 return 0;
1902}
1903
1904static void audio_source_function_cleanup(struct android_usb_function *f)
1905{
1906 kfree(f->config);
1907}
1908
1909static int audio_source_function_bind_config(struct android_usb_function *f,
1910 struct usb_configuration *c)
1911{
1912 struct audio_source_config *config = f->config;
1913
1914 return audio_source_bind_config(c, config);
1915}
1916
1917static void audio_source_function_unbind_config(struct android_usb_function *f,
1918 struct usb_configuration *c)
1919{
1920 struct audio_source_config *config = f->config;
1921
1922 config->card = -1;
1923 config->device = -1;
1924}
1925
1926static ssize_t audio_source_pcm_show(struct device *dev,
1927 struct device_attribute *attr, char *buf)
1928{
1929 struct android_usb_function *f = dev_get_drvdata(dev);
1930 struct audio_source_config *config = f->config;
1931
1932 /* print PCM card and device numbers */
1933 return sprintf(buf, "%d %d\n", config->card, config->device);
1934}
1935
1936static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1937
1938static struct device_attribute *audio_source_function_attributes[] = {
1939 &dev_attr_pcm,
1940 NULL
1941};
1942
1943static struct android_usb_function audio_source_function = {
1944 .name = "audio_source",
1945 .init = audio_source_function_init,
1946 .cleanup = audio_source_function_cleanup,
1947 .bind_config = audio_source_function_bind_config,
1948 .unbind_config = audio_source_function_unbind_config,
1949 .attributes = audio_source_function_attributes,
1950};
Anna Perel3ee23dd2013-02-26 16:06:40 +02001951#endif
Mike Lockwood11874822012-08-27 16:43:53 +05301952
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301953static int android_uasp_connect_cb(bool connect)
1954{
1955 /*
1956 * TODO
1957 * We may have to disable gadget till UASP configfs nodes
1958 * are configured which includes mapping LUN with the
1959 * backing file. It is a fundamental difference between
1960 * f_mass_storage and f_tcp. That means UASP can not be
1961 * in default composition.
1962 *
1963 * For now, assume that UASP configfs nodes are configured
1964 * before enabling android gadget. Or cable should be
1965 * reconnected after mapping the LUN.
1966 *
1967 * Also consider making UASP to respond to Host requests when
1968 * Lun is not mapped.
1969 */
1970 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1971
1972 return 0;
1973}
1974
1975static int uasp_function_init(struct android_usb_function *f,
1976 struct usb_composite_dev *cdev)
1977{
1978 return f_tcm_init(&android_uasp_connect_cb);
1979}
1980
1981static void uasp_function_cleanup(struct android_usb_function *f)
1982{
1983 f_tcm_exit();
1984}
1985
1986static int uasp_function_bind_config(struct android_usb_function *f,
1987 struct usb_configuration *c)
1988{
1989 return tcm_bind_config(c);
1990}
1991
1992static struct android_usb_function uasp_function = {
1993 .name = "uasp",
1994 .init = uasp_function_init,
1995 .cleanup = uasp_function_cleanup,
1996 .bind_config = uasp_function_bind_config,
1997};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001998
Benoit Goby1e8ce152011-12-12 13:01:23 -08001999static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03002000 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03002001 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07002002#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03002003 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07002004#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05302005 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05302006 &rmnet_sdio_function,
2007 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05302008 &rmnet_function,
Bar Weinered1059e2013-04-16 11:23:55 +03002009 &gps_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03002011 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05302012 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08002013 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05302014 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002015 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08002016 &mtp_function,
2017 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002018 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02002019 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02002020 &ecm_function,
Vamsi Krishna932c9de2013-05-22 12:18:05 -07002021 &ncm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002022 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08002023 &accessory_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02002024#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05302025 &audio_source_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02002026#endif
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05302027 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002028 NULL
2029};
2030
Lena Salmand092f2d2012-03-12 17:27:24 +02002031static void android_cleanup_functions(struct android_usb_function **functions)
2032{
2033 struct android_usb_function *f;
2034 struct device_attribute **attrs;
2035 struct device_attribute *attr;
2036
2037 while (*functions) {
2038 f = *functions++;
2039
2040 if (f->dev) {
2041 device_destroy(android_class, f->dev->devt);
2042 kfree(f->dev_name);
2043 } else
2044 continue;
2045
2046 if (f->cleanup)
2047 f->cleanup(f);
2048
2049 attrs = f->attributes;
2050 if (attrs) {
2051 while ((attr = *attrs++))
2052 device_remove_file(f->dev, attr);
2053 }
2054 }
2055}
Benoit Goby1e8ce152011-12-12 13:01:23 -08002056
2057static int android_init_functions(struct android_usb_function **functions,
2058 struct usb_composite_dev *cdev)
2059{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002060 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002061 struct android_usb_function *f;
2062 struct device_attribute **attrs;
2063 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302064 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02002065 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08002066
2067 for (; (f = *functions++); index++) {
2068 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002069 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02002070 if (!f->dev_name) {
2071 err = -ENOMEM;
2072 goto err_out;
2073 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002074 f->dev = device_create(android_class, dev->dev,
2075 MKDEV(0, index), f, f->dev_name);
2076 if (IS_ERR(f->dev)) {
2077 pr_err("%s: Failed to create dev %s", __func__,
2078 f->dev_name);
2079 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02002080 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002081 goto err_create;
2082 }
2083
2084 if (f->init) {
2085 err = f->init(f, cdev);
2086 if (err) {
2087 pr_err("%s: Failed to init %s", __func__,
2088 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002089 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002090 }
2091 }
2092
2093 attrs = f->attributes;
2094 if (attrs) {
2095 while ((attr = *attrs++) && !err)
2096 err = device_create_file(f->dev, attr);
2097 }
2098 if (err) {
2099 pr_err("%s: Failed to create function %s attributes",
2100 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002101 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002102 }
2103 }
2104 return 0;
2105
Lena Salmand092f2d2012-03-12 17:27:24 +02002106err_attrs:
2107 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
2108 device_remove_file(f->dev, attr);
2109 if (f->cleanup)
2110 f->cleanup(f);
2111err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08002112 device_destroy(android_class, f->dev->devt);
2113err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02002114 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002115 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02002116err_out:
2117 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002118 return err;
2119}
2120
Benoit Goby1e8ce152011-12-12 13:01:23 -08002121static int
2122android_bind_enabled_functions(struct android_dev *dev,
2123 struct usb_configuration *c)
2124{
Ido Shayevitz68557e32012-11-06 12:40:37 +02002125 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002126 struct android_configuration *conf =
2127 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002128 int ret;
2129
Ido Shayevitz68557e32012-11-06 12:40:37 +02002130 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
2131 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002132 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002133 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002134 return ret;
2135 }
2136 }
2137 return 0;
2138}
2139
2140static void
2141android_unbind_enabled_functions(struct android_dev *dev,
2142 struct usb_configuration *c)
2143{
Ido Shayevitz68557e32012-11-06 12:40:37 +02002144 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002145 struct android_configuration *conf =
2146 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002147
Ido Shayevitz68557e32012-11-06 12:40:37 +02002148 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
2149 if (f_holder->f->unbind_config)
2150 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002151 }
2152}
2153
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002154static int android_enable_function(struct android_dev *dev,
2155 struct android_configuration *conf,
2156 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002157{
2158 struct android_usb_function **functions = dev->functions;
2159 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002160 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002161 while ((f = *functions++)) {
2162 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002163 if (f->android_dev && f->android_dev != dev)
2164 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002165 f->name);
2166 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002167 f_holder = kzalloc(sizeof(*f_holder),
2168 GFP_KERNEL);
2169 if (!f_holder) {
2170 pr_err("Failed to alloc f_holder\n");
2171 return -ENOMEM;
2172 }
2173
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002174 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002175 f_holder->f = f;
2176 list_add_tail(&f_holder->enabled_list,
2177 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002178 return 0;
2179 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002180 }
2181 }
2182 return -EINVAL;
2183}
2184
2185/*-------------------------------------------------------------------------*/
2186/* /sys/class/android_usb/android%d/ interface */
2187
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302188static ssize_t remote_wakeup_show(struct device *pdev,
2189 struct device_attribute *attr, char *buf)
2190{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002191 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002192 struct android_configuration *conf;
2193
2194 /*
2195 * Show the wakeup attribute of the first configuration,
2196 * since all configurations have the same wakeup attribute
2197 */
2198 if (dev->configs_num == 0)
2199 return 0;
2200 conf = list_entry(dev->configs.next,
2201 struct android_configuration,
2202 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002203
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302204 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002205 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302206 USB_CONFIG_ATT_WAKEUP));
2207}
2208
2209static ssize_t remote_wakeup_store(struct device *pdev,
2210 struct device_attribute *attr, const char *buff, size_t size)
2211{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002212 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002213 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302214 int enable = 0;
2215
2216 sscanf(buff, "%d", &enable);
2217
2218 pr_debug("android_usb: %s remote wakeup\n",
2219 enable ? "enabling" : "disabling");
2220
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002221 list_for_each_entry(conf, &dev->configs, list_item)
2222 if (enable)
2223 conf->usb_config.bmAttributes |=
2224 USB_CONFIG_ATT_WAKEUP;
2225 else
2226 conf->usb_config.bmAttributes &=
2227 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302228
2229 return size;
2230}
2231
Benoit Goby1e8ce152011-12-12 13:01:23 -08002232static ssize_t
2233functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2234{
2235 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002236 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002237 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002238 char *buff = buf;
2239
2240 mutex_lock(&dev->mutex);
2241
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002242 list_for_each_entry(conf, &dev->configs, list_item) {
2243 if (buff != buf)
2244 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02002245 list_for_each_entry(f_holder, &conf->enabled_functions,
2246 enabled_list)
2247 buff += snprintf(buff, PAGE_SIZE, "%s,",
2248 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002249 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002250
2251 mutex_unlock(&dev->mutex);
2252
2253 if (buff != buf)
2254 *(buff-1) = '\n';
2255 return buff - buf;
2256}
2257
2258static ssize_t
2259functions_store(struct device *pdev, struct device_attribute *attr,
2260 const char *buff, size_t size)
2261{
2262 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002263 struct list_head *curr_conf = &dev->configs;
2264 struct android_configuration *conf;
2265 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002266 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002267 char *name;
2268 char buf[256], *b;
2269 int err;
2270
2271 mutex_lock(&dev->mutex);
2272
2273 if (dev->enabled) {
2274 mutex_unlock(&dev->mutex);
2275 return -EBUSY;
2276 }
2277
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002278 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002279 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002280 while (conf->enabled_functions.next !=
2281 &conf->enabled_functions) {
2282 f_holder = list_entry(conf->enabled_functions.next,
2283 typeof(*f_holder),
2284 enabled_list);
2285 f_holder->f->android_dev = NULL;
2286 list_del(&f_holder->enabled_list);
2287 kfree(f_holder);
2288 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002289 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002290 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002291
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302292 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002293 b = strim(buf);
2294
2295 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002296 conf_str = strsep(&b, ":");
2297 if (conf_str) {
2298 /* If the next not equal to the head, take it */
2299 if (curr_conf->next != &dev->configs)
2300 conf = list_entry(curr_conf->next,
2301 struct android_configuration,
2302 list_item);
2303 else
2304 conf = alloc_android_config(dev);
2305
2306 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002307 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002308
2309 while (conf_str) {
2310 name = strsep(&conf_str, ",");
2311 if (name) {
2312 err = android_enable_function(dev, conf, name);
2313 if (err)
2314 pr_err("android_usb: Cannot enable %s",
2315 name);
2316 }
2317 }
2318 }
2319
2320 /* Free uneeded configurations if exists */
2321 while (curr_conf->next != &dev->configs) {
2322 conf = list_entry(curr_conf->next,
2323 struct android_configuration, list_item);
2324 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002325 }
2326
2327 mutex_unlock(&dev->mutex);
2328
2329 return size;
2330}
2331
2332static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2333 char *buf)
2334{
2335 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002336
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302337 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002338}
2339
2340static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2341 const char *buff, size_t size)
2342{
2343 struct android_dev *dev = dev_get_drvdata(pdev);
2344 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002345 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002346 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002347 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302348 bool audio_enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302349 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002350
Stephen Boyd42517402013-01-14 16:41:42 -08002351
Benoit Gobycf3fc062011-12-19 14:39:37 -08002352 if (!cdev)
2353 return -ENODEV;
2354
Benoit Goby1e8ce152011-12-12 13:01:23 -08002355 mutex_lock(&dev->mutex);
2356
2357 sscanf(buff, "%d", &enabled);
2358 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002359 /*
2360 * Update values in composite driver's copy of
2361 * device descriptor.
2362 */
2363 cdev->desc.idVendor = device_desc.idVendor;
2364 cdev->desc.idProduct = device_desc.idProduct;
2365 cdev->desc.bcdDevice = device_desc.bcdDevice;
2366 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2367 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2368 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302369
2370 /* Audio dock accessory is unable to enumerate device if
2371 * pull-up is enabled immediately. The enumeration is
2372 * reliable with 100 msec delay.
2373 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002374 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002375 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002376 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002377 if (f_holder->f->enable)
2378 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302379 if (!strncmp(f_holder->f->name,
2380 "audio_source", 12))
2381 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002382 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302383 if (audio_enabled)
2384 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002385 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002386 dev->enabled = true;
2387 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002388 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002389 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002390 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002391 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002392 if (f_holder->f->disable)
2393 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002394 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002395 dev->enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302396 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002397 pr_err("android_usb: already %s\n",
2398 dev->enabled ? "enabled" : "disabled");
2399 }
2400
2401 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002402
Benoit Gobyaab96812011-04-19 20:37:33 -07002403 return size;
2404}
2405
Ofir Cohen94213a72012-05-03 14:26:32 +03002406static ssize_t pm_qos_show(struct device *pdev,
2407 struct device_attribute *attr, char *buf)
2408{
2409 struct android_dev *dev = dev_get_drvdata(pdev);
2410
2411 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2412}
2413
2414static ssize_t pm_qos_store(struct device *pdev,
2415 struct device_attribute *attr,
2416 const char *buff, size_t size)
2417{
2418 struct android_dev *dev = dev_get_drvdata(pdev);
2419
2420 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2421
Benoit Goby1e8ce152011-12-12 13:01:23 -08002422 return size;
2423}
2424
2425static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2426 char *buf)
2427{
2428 struct android_dev *dev = dev_get_drvdata(pdev);
2429 struct usb_composite_dev *cdev = dev->cdev;
2430 char *state = "DISCONNECTED";
2431 unsigned long flags;
2432
2433 if (!cdev)
2434 goto out;
2435
2436 spin_lock_irqsave(&cdev->lock, flags);
2437 if (cdev->config)
2438 state = "CONFIGURED";
2439 else if (dev->connected)
2440 state = "CONNECTED";
2441 spin_unlock_irqrestore(&cdev->lock, flags);
2442out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302443 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002444}
2445
2446#define DESCRIPTOR_ATTR(field, format_string) \
2447static ssize_t \
2448field ## _show(struct device *dev, struct device_attribute *attr, \
2449 char *buf) \
2450{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302451 return snprintf(buf, PAGE_SIZE, \
2452 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002453} \
2454static ssize_t \
2455field ## _store(struct device *dev, struct device_attribute *attr, \
2456 const char *buf, size_t size) \
2457{ \
2458 int value; \
2459 if (sscanf(buf, format_string, &value) == 1) { \
2460 device_desc.field = value; \
2461 return size; \
2462 } \
2463 return -1; \
2464} \
2465static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2466
2467#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2468static ssize_t \
2469field ## _show(struct device *dev, struct device_attribute *attr, \
2470 char *buf) \
2471{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302472 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002473} \
2474static ssize_t \
2475field ## _store(struct device *dev, struct device_attribute *attr, \
2476 const char *buf, size_t size) \
2477{ \
2478 if (size >= sizeof(buffer)) \
2479 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302480 strlcpy(buffer, buf, sizeof(buffer)); \
2481 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302482 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002483} \
2484static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2485
2486
2487DESCRIPTOR_ATTR(idVendor, "%04x\n")
2488DESCRIPTOR_ATTR(idProduct, "%04x\n")
2489DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2490DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2491DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2492DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2493DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2494DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2495DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2496
2497static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2498 functions_store);
2499static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002500static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2501 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002502static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302503static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2504 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002505
2506static struct device_attribute *android_usb_attributes[] = {
2507 &dev_attr_idVendor,
2508 &dev_attr_idProduct,
2509 &dev_attr_bcdDevice,
2510 &dev_attr_bDeviceClass,
2511 &dev_attr_bDeviceSubClass,
2512 &dev_attr_bDeviceProtocol,
2513 &dev_attr_iManufacturer,
2514 &dev_attr_iProduct,
2515 &dev_attr_iSerial,
2516 &dev_attr_functions,
2517 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002518 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002519 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302520 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002521 NULL
2522};
2523
2524/*-------------------------------------------------------------------------*/
2525/* Composite driver */
2526
2527static int android_bind_config(struct usb_configuration *c)
2528{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002529 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002530 int ret = 0;
2531
2532 ret = android_bind_enabled_functions(dev, c);
2533 if (ret)
2534 return ret;
2535
2536 return 0;
2537}
2538
2539static void android_unbind_config(struct usb_configuration *c)
2540{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002541 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002542
2543 android_unbind_enabled_functions(dev, c);
2544}
2545
2546static int android_bind(struct usb_composite_dev *cdev)
2547{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002548 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002549 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002550 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002551 int gcnum, id, ret;
2552
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002553 /* Bind to the last android_dev that was probed */
2554 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2555
2556 dev->cdev = cdev;
2557
Benoit Goby1e8ce152011-12-12 13:01:23 -08002558 /*
2559 * Start disconnected. Userspace will connect the gadget once
2560 * it is done configuring the functions.
2561 */
2562 usb_gadget_disconnect(gadget);
2563
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002564 /* Init the supported functions only once, on the first android_dev */
2565 if (android_dev_count == 1) {
2566 ret = android_init_functions(dev->functions, cdev);
2567 if (ret)
2568 return ret;
2569 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002570
2571 /* Allocate string descriptor numbers ... note that string
2572 * contents can be overridden by the composite_dev glue.
2573 */
2574 id = usb_string_id(cdev);
2575 if (id < 0)
2576 return id;
2577 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2578 device_desc.iManufacturer = id;
2579
2580 id = usb_string_id(cdev);
2581 if (id < 0)
2582 return id;
2583 strings_dev[STRING_PRODUCT_IDX].id = id;
2584 device_desc.iProduct = id;
2585
2586 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302587 strlcpy(manufacturer_string, "Android",
2588 sizeof(manufacturer_string) - 1);
2589 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2590 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002591
2592 id = usb_string_id(cdev);
2593 if (id < 0)
2594 return id;
2595 strings_dev[STRING_SERIAL_IDX].id = id;
2596 device_desc.iSerialNumber = id;
2597
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302598 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002599 list_for_each_entry(conf, &dev->configs, list_item)
2600 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302601
Benoit Goby1e8ce152011-12-12 13:01:23 -08002602 gcnum = usb_gadget_controller_number(gadget);
2603 if (gcnum >= 0)
2604 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2605 else {
2606 pr_warning("%s: controller '%s' not recognized\n",
2607 longname, gadget->name);
2608 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2609 }
2610
Benoit Goby1e8ce152011-12-12 13:01:23 -08002611 return 0;
2612}
2613
2614static int android_usb_unbind(struct usb_composite_dev *cdev)
2615{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002616 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002617
Lena Salmand092f2d2012-03-12 17:27:24 +02002618 manufacturer_string[0] = '\0';
2619 product_string[0] = '\0';
2620 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002621 cancel_work_sync(&dev->work);
2622 android_cleanup_functions(dev->functions);
2623 return 0;
2624}
2625
2626static struct usb_composite_driver android_usb_driver = {
2627 .name = "android_usb",
2628 .dev = &device_desc,
2629 .strings = dev_strings,
2630 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002631 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002632};
2633
2634static int
2635android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2636{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002637 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002638 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002639 struct usb_request *req = cdev->req;
2640 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002641 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002642 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002643 int value = -EOPNOTSUPP;
2644 unsigned long flags;
2645
2646 req->zero = 0;
2647 req->complete = composite_setup_complete;
2648 req->length = 0;
2649 gadget->ep0->driver_data = cdev;
2650
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002651 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002652 list_for_each_entry(f_holder,
2653 &conf->enabled_functions,
2654 enabled_list) {
2655 f = f_holder->f;
2656 if (f->ctrlrequest) {
2657 value = f->ctrlrequest(f, cdev, c);
2658 if (value >= 0)
2659 break;
2660 }
2661 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002662
Benoit Gobycf3fc062011-12-19 14:39:37 -08002663 /* Special case the accessory function.
2664 * It needs to handle control requests before it is enabled.
2665 */
2666 if (value < 0)
2667 value = acc_ctrlrequest(cdev, c);
2668
Benoit Goby1e8ce152011-12-12 13:01:23 -08002669 if (value < 0)
2670 value = composite_setup(gadget, c);
2671
2672 spin_lock_irqsave(&cdev->lock, flags);
2673 if (!dev->connected) {
2674 dev->connected = 1;
2675 schedule_work(&dev->work);
2676 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2677 cdev->config) {
2678 schedule_work(&dev->work);
2679 }
2680 spin_unlock_irqrestore(&cdev->lock, flags);
2681
2682 return value;
2683}
2684
2685static void android_disconnect(struct usb_gadget *gadget)
2686{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002687 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002688 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002689 unsigned long flags;
2690
2691 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302692 /* accessory HID support can be active while the
2693 accessory function is not actually enabled,
2694 so we need to inform it when we are disconnected.
2695 */
2696 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002697
2698 spin_lock_irqsave(&cdev->lock, flags);
2699 dev->connected = 0;
2700 schedule_work(&dev->work);
2701 spin_unlock_irqrestore(&cdev->lock, flags);
2702}
2703
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002704static void android_suspend(struct usb_gadget *gadget)
2705{
2706 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2707 struct android_dev *dev = cdev_to_android_dev(cdev);
2708 unsigned long flags;
2709
2710 spin_lock_irqsave(&cdev->lock, flags);
2711 dev->suspended = 1;
2712 schedule_work(&dev->work);
2713 spin_unlock_irqrestore(&cdev->lock, flags);
2714
2715 composite_suspend(gadget);
2716}
2717
2718static void android_resume(struct usb_gadget *gadget)
2719{
2720 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2721 struct android_dev *dev = cdev_to_android_dev(cdev);
2722 unsigned long flags;
2723
2724 spin_lock_irqsave(&cdev->lock, flags);
2725 dev->suspended = 0;
2726 schedule_work(&dev->work);
2727 spin_unlock_irqrestore(&cdev->lock, flags);
2728
2729 composite_resume(gadget);
2730}
2731
2732
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002733static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002734{
2735 struct device_attribute **attrs = android_usb_attributes;
2736 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002737 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002738 int err;
2739
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002740 /*
2741 * The primary usb core should always have usb_core_id=0, since
2742 * Android user space is currently interested in android0 events.
2743 */
2744 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2745 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002746 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002747 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002748 if (IS_ERR(dev->dev))
2749 return PTR_ERR(dev->dev);
2750
2751 dev_set_drvdata(dev->dev, dev);
2752
2753 while ((attr = *attrs++)) {
2754 err = device_create_file(dev->dev, attr);
2755 if (err) {
2756 device_destroy(android_class, dev->dev->devt);
2757 return err;
2758 }
2759 }
2760 return 0;
2761}
2762
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302763static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002764{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302765 struct device_attribute **attrs = android_usb_attributes;
2766 struct device_attribute *attr;
2767
2768 while ((attr = *attrs++))
2769 device_remove_file(dev->dev, attr);
2770 device_destroy(android_class, dev->dev->devt);
2771}
2772
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002773static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2774{
2775 struct android_dev *dev = NULL;
2776
2777 /* Find the android dev from the list */
2778 list_for_each_entry(dev, &android_dev_list, list_item) {
2779 if (dev->cdev == cdev)
2780 break;
2781 }
2782
2783 return dev;
2784}
2785
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002786static struct android_configuration *alloc_android_config
2787 (struct android_dev *dev)
2788{
2789 struct android_configuration *conf;
2790
2791 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2792 if (!conf) {
2793 pr_err("%s(): Failed to alloc memory for android conf\n",
2794 __func__);
2795 return ERR_PTR(-ENOMEM);
2796 }
2797
2798 dev->configs_num++;
2799 conf->usb_config.label = dev->name;
2800 conf->usb_config.unbind = android_unbind_config;
2801 conf->usb_config.bConfigurationValue = dev->configs_num;
2802
2803 INIT_LIST_HEAD(&conf->enabled_functions);
2804
2805 list_add_tail(&conf->list_item, &dev->configs);
2806
2807 return conf;
2808}
2809
2810static void free_android_config(struct android_dev *dev,
2811 struct android_configuration *conf)
2812{
2813 list_del(&conf->list_item);
2814 dev->configs_num--;
2815 kfree(conf);
2816}
2817
Manu Gautam43c61a12012-08-22 17:09:37 -07002818static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2819{
2820 struct dload_struct local_diag_dload = { 0 };
2821 int *src, *dst, i;
2822
2823 if (!diag_dload) {
2824 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2825 return -ENODEV;
2826 }
2827
2828 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2829 __func__, diag_dload, pid, snum);
2830
2831 /* update pid */
2832 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2833 local_diag_dload.pid = pid;
2834
2835 /* update serial number */
2836 if (!snum) {
2837 local_diag_dload.magic_struct.serial_num = 0;
2838 memset(&local_diag_dload.serial_number, 0,
2839 SERIAL_NUMBER_LENGTH);
2840 } else {
2841 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2842 strlcpy((char *)&local_diag_dload.serial_number, snum,
2843 SERIAL_NUMBER_LENGTH);
2844 }
2845
2846 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2847 src = (int *)&local_diag_dload;
2848 dst = (int *)diag_dload;
2849
2850 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2851 *dst++ = *src++;
2852
2853 return 0;
2854}
2855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856static int __devinit android_probe(struct platform_device *pdev)
2857{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302858 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002859 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002860 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002861 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302863 if (pdev->dev.of_node) {
2864 dev_dbg(&pdev->dev, "device tree enabled\n");
2865 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2866 if (!pdata) {
2867 pr_err("unable to allocate platform data\n");
2868 return -ENOMEM;
2869 }
2870
2871 of_property_read_u32(pdev->dev.of_node,
2872 "qcom,android-usb-swfi-latency",
2873 &pdata->swfi_latency);
Chiranjeevi Velempati9d797662013-04-22 15:45:38 +05302874 pdata->cdrom = of_property_read_bool(pdev->dev.of_node,
2875 "qcom,android-usb-cdrom");
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302876 } else {
2877 pdata = pdev->dev.platform_data;
2878 }
2879
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002880 if (!android_class) {
2881 android_class = class_create(THIS_MODULE, "android_usb");
2882 if (IS_ERR(android_class))
2883 return PTR_ERR(android_class);
2884 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002885
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002886 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2887 if (!android_dev) {
2888 pr_err("%s(): Failed to alloc memory for android_dev\n",
2889 __func__);
2890 ret = -ENOMEM;
2891 goto err_alloc;
2892 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002893
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002894 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002895 android_dev->disable_depth = 1;
2896 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002897 android_dev->configs_num = 0;
2898 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002899 INIT_WORK(&android_dev->work, android_work);
2900 mutex_init(&android_dev->mutex);
2901
2902 android_dev->pdata = pdata;
2903
2904 list_add_tail(&android_dev->list_item, &android_dev_list);
2905 android_dev_count++;
2906
2907 if (pdata)
2908 composite_driver.usb_core_id = pdata->usb_core_id;
2909 else
2910 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2911
Manu Gautam43c61a12012-08-22 17:09:37 -07002912 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2913 if (res) {
2914 diag_dload = devm_ioremap(&pdev->dev, res->start,
2915 resource_size(res));
2916 if (!diag_dload) {
2917 dev_err(&pdev->dev, "ioremap failed\n");
2918 ret = -ENOMEM;
2919 goto err_dev;
2920 }
2921 } else {
2922 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2923 }
2924
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002925 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302926 if (ret) {
2927 pr_err("%s(): android_create_device failed\n", __func__);
2928 goto err_dev;
2929 }
2930
Lena Salmand092f2d2012-03-12 17:27:24 +02002931 ret = usb_composite_probe(&android_usb_driver, android_bind);
2932 if (ret) {
2933 pr_err("%s(): Failed to register android "
2934 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302935 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002936 }
2937
Ofir Cohen94213a72012-05-03 14:26:32 +03002938 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302939 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002940 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002941 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002942 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002943
Lena Salmand092f2d2012-03-12 17:27:24 +02002944 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302945err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002946 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302947err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002948 list_del(&android_dev->list_item);
2949 android_dev_count--;
2950 kfree(android_dev);
2951err_alloc:
2952 if (list_empty(&android_dev_list)) {
2953 class_destroy(android_class);
2954 android_class = NULL;
2955 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302956 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002957}
2958
2959static int android_remove(struct platform_device *pdev)
2960{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002961 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002962 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002963 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302964
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002965 if (pdata)
2966 usb_core_id = pdata->usb_core_id;
2967
2968 /* Find the android dev from the list */
2969 list_for_each_entry(dev, &android_dev_list, list_item) {
2970 if (!dev->pdata)
2971 break; /*To backward compatibility*/
2972 if (dev->pdata->usb_core_id == usb_core_id)
2973 break;
2974 }
2975
2976 if (dev) {
2977 android_destroy_device(dev);
2978 if (pdata && pdata->swfi_latency)
2979 pm_qos_remove_request(&dev->pm_qos_req_dma);
2980 list_del(&dev->list_item);
2981 android_dev_count--;
2982 kfree(dev);
2983 }
2984
2985 if (list_empty(&android_dev_list)) {
2986 class_destroy(android_class);
2987 android_class = NULL;
2988 usb_composite_unregister(&android_usb_driver);
2989 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002991 return 0;
2992}
2993
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002994static const struct platform_device_id android_id_table[] __devinitconst = {
2995 {
2996 .name = "android_usb",
2997 },
2998 {
2999 .name = "android_usb_hsic",
3000 },
3001};
3002
Manu Gautam43c61a12012-08-22 17:09:37 -07003003static struct of_device_id usb_android_dt_match[] = {
3004 { .compatible = "qcom,android-usb",
3005 },
3006 {}
3007};
3008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003009static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07003010 .driver = {
3011 .name = "android_usb",
3012 .of_match_table = usb_android_dt_match,
3013 },
Lena Salmand092f2d2012-03-12 17:27:24 +02003014 .probe = android_probe,
3015 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03003016 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003017};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05003018
3019static int __init init(void)
3020{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05303021 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05003022
Benoit Goby1e8ce152011-12-12 13:01:23 -08003023 /* Override composite driver functions */
3024 composite_driver.setup = android_setup;
3025 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02003026 composite_driver.suspend = android_suspend;
3027 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08003028
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03003029 INIT_LIST_HEAD(&android_dev_list);
3030 android_dev_count = 0;
3031
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05303032 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05303033 if (ret) {
3034 pr_err("%s(): Failed to register android"
3035 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05303036 }
Lena Salmand092f2d2012-03-12 17:27:24 +02003037
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05303038 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08003039}
3040module_init(init);
3041
3042static void __exit cleanup(void)
3043{
Lena Salmand092f2d2012-03-12 17:27:24 +02003044 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08003045}
3046module_exit(cleanup);