blob: 705600d56303456de4c74e911c3abb855fd99e5e [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
Benoit Goby1e8ce152011-12-12 13:01:23 -080094
95MODULE_AUTHOR("Mike Lockwood");
96MODULE_DESCRIPTION("Android Composite USB Driver");
97MODULE_LICENSE("GPL");
98MODULE_VERSION("1.0");
99
100static const char longname[] = "Gadget Android";
101
102/* Default vendor and product IDs, overridden by userspace */
103#define VENDOR_ID 0x18D1
104#define PRODUCT_ID 0x0001
105
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300106#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
107
Benoit Goby1e8ce152011-12-12 13:01:23 -0800108struct android_usb_function {
109 char *name;
110 void *config;
111
112 struct device *dev;
113 char *dev_name;
114 struct device_attribute **attributes;
115
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300116 struct android_dev *android_dev;
117
Benoit Goby1e8ce152011-12-12 13:01:23 -0800118 /* Optional: initialization during gadget bind */
119 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
120 /* Optional: cleanup during gadget unbind */
121 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700122 /* Optional: called when the function is added the list of
123 * enabled functions */
124 void (*enable)(struct android_usb_function *);
125 /* Optional: called when it is removed */
126 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800127
128 int (*bind_config)(struct android_usb_function *,
129 struct usb_configuration *);
130
131 /* Optional: called when the configuration is removed */
132 void (*unbind_config)(struct android_usb_function *,
133 struct usb_configuration *);
134 /* Optional: handle ctrl requests before the device is configured */
135 int (*ctrlrequest)(struct android_usb_function *,
136 struct usb_composite_dev *,
137 const struct usb_ctrlrequest *);
138};
139
Ido Shayevitz68557e32012-11-06 12:40:37 +0200140struct android_usb_function_holder {
141
142 struct android_usb_function *f;
143
144 /* for android_conf.enabled_functions */
145 struct list_head enabled_list;
146};
147
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200148/**
149* struct android_dev - represents android USB gadget device
150* @name: device name.
151* @functions: an array of all the supported USB function
152* drivers that this gadget support but not necessarily
153* added to one of the gadget configurations.
154* @cdev: The internal composite device. Android gadget device
155* is a composite device, such that it can support configurations
156* with more than one function driver.
157* @dev: The kernel device that represents this android device.
158* @enabled: True if the android gadget is enabled, means all
159* the configurations were set and all function drivers were
160* bind and ready for USB enumeration.
161* @disable_depth: Number of times the device was disabled, after
162* symmetrical number of enables the device willl be enabled.
163* Used for controlling ADB userspace disable/enable requests.
164* @mutex: Internal mutex for protecting device member fields.
165* @pdata: Platform data fetched from the kernel device platfrom data.
166* @connected: True if got connect notification from the gadget UDC.
167* False if got disconnect notification from the gadget UDC.
168* @sw_connected: Equal to 'connected' only after the connect
169* notification was handled by the android gadget work function.
170* @suspended: True if got suspend notification from the gadget UDC.
171* False if got resume notification from the gadget UDC.
172* @sw_suspended: Equal to 'suspended' only after the susped
173* notification was handled by the android gadget work function.
174* @pm_qos: An attribute string that can be set by user space in order to
175* determine pm_qos policy. Set to 'high' for always demand pm_qos
176* when USB bus is connected and resumed. Set to 'low' for disable
177* any setting of pm_qos by this driver. Default = 'high'.
178* @work: workqueue used for handling notifications from the gadget UDC.
179* @configs: List of configurations currently configured into the device.
180* The android gadget supports more than one configuration. The host
181* may choose one configuration from the suggested.
182* @configs_num: Number of configurations currently configured and existing
183* in the configs list.
184* @list_item: This driver supports more than one android gadget device (for
185* example in order to support multiple USB cores), therefore this is
186* a item in a linked list of android devices.
187*/
Benoit Goby1e8ce152011-12-12 13:01:23 -0800188struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300189 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800190 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800191 struct usb_composite_dev *cdev;
192 struct device *dev;
193
194 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700195 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800196 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 struct android_usb_platform_data *pdata;
198
Benoit Goby1e8ce152011-12-12 13:01:23 -0800199 bool connected;
200 bool sw_connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200201 bool suspended;
202 bool sw_suspended;
Ofir Cohen94213a72012-05-03 14:26:32 +0300203 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700204 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800205 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300206
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300207 /* A list of struct android_configuration */
208 struct list_head configs;
209 int configs_num;
210
211 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300212 struct list_head list_item;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300213};
214
215struct android_configuration {
216 struct usb_configuration usb_config;
217
218 /* A list of the functions supported by this config */
219 struct list_head enabled_functions;
220
221 /* A list node inside the struct android_dev.configs list */
222 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800223};
224
Manu Gautam43c61a12012-08-22 17:09:37 -0700225struct dload_struct __iomem *diag_dload;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800226static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300227static struct list_head android_dev_list;
228static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800229static int android_bind_config(struct usb_configuration *c);
230static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300231static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300232static struct android_configuration *alloc_android_config
233 (struct android_dev *dev);
234static void free_android_config(struct android_dev *dev,
235 struct android_configuration *conf);
Manu Gautam43c61a12012-08-22 17:09:37 -0700236static int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800237
238/* string IDs are assigned dynamically */
239#define STRING_MANUFACTURER_IDX 0
240#define STRING_PRODUCT_IDX 1
241#define STRING_SERIAL_IDX 2
242
243static char manufacturer_string[256];
244static char product_string[256];
245static char serial_string[256];
246
247/* String Table */
248static struct usb_string strings_dev[] = {
249 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
250 [STRING_PRODUCT_IDX].s = product_string,
251 [STRING_SERIAL_IDX].s = serial_string,
252 { } /* end of list */
253};
254
255static struct usb_gadget_strings stringtab_dev = {
256 .language = 0x0409, /* en-us */
257 .strings = strings_dev,
258};
259
260static struct usb_gadget_strings *dev_strings[] = {
261 &stringtab_dev,
262 NULL,
263};
264
265static struct usb_device_descriptor device_desc = {
266 .bLength = sizeof(device_desc),
267 .bDescriptorType = USB_DT_DEVICE,
268 .bcdUSB = __constant_cpu_to_le16(0x0200),
269 .bDeviceClass = USB_CLASS_PER_INTERFACE,
270 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
271 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
272 .bcdDevice = __constant_cpu_to_le16(0xffff),
273 .bNumConfigurations = 1,
274};
275
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530276static struct usb_otg_descriptor otg_descriptor = {
277 .bLength = sizeof otg_descriptor,
278 .bDescriptorType = USB_DT_OTG,
279 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
280 .bcdOTG = __constant_cpu_to_le16(0x0200),
281};
282
283static const struct usb_descriptor_header *otg_desc[] = {
284 (struct usb_descriptor_header *) &otg_descriptor,
285 NULL,
286};
287
Manu Gautama2b54142012-04-03 14:34:32 +0530288enum android_device_state {
289 USB_DISCONNECTED,
290 USB_CONNECTED,
291 USB_CONFIGURED,
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200292 USB_SUSPENDED,
293 USB_RESUMED
Manu Gautama2b54142012-04-03 14:34:32 +0530294};
295
Ofir Cohen94213a72012-05-03 14:26:32 +0300296static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
297{
298 struct android_usb_platform_data *pdata = dev->pdata;
299 u32 swfi_latency = 0;
300 static int last_vote = -1;
301
Ofir Cohen56eb7072012-05-20 11:41:39 +0300302 if (!pdata || vote == last_vote
303 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300304 return;
305
306 swfi_latency = pdata->swfi_latency + 1;
307 if (vote)
308 pm_qos_update_request(&dev->pm_qos_req_dma,
309 swfi_latency);
310 else
311 pm_qos_update_request(&dev->pm_qos_req_dma,
312 PM_QOS_DEFAULT_VALUE);
313 last_vote = vote;
314}
315
Benoit Goby1e8ce152011-12-12 13:01:23 -0800316static void android_work(struct work_struct *data)
317{
318 struct android_dev *dev = container_of(data, struct android_dev, work);
319 struct usb_composite_dev *cdev = dev->cdev;
320 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
321 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
322 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200323 char *suspended[2] = { "USB_STATE=SUSPENDED", NULL };
324 char *resumed[2] = { "USB_STATE=RESUMED", NULL };
Benoit Goby1e8ce152011-12-12 13:01:23 -0800325 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530326 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800327 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300328 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800329
330 spin_lock_irqsave(&cdev->lock, flags);
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200331 if (dev->suspended != dev->sw_suspended && cdev->config) {
332 if (strncmp(dev->pm_qos, "low", 3))
333 pm_qos_vote = dev->suspended ? 0 : 1;
334 next_state = dev->suspended ? USB_SUSPENDED : USB_RESUMED;
335 uevent_envp = dev->suspended ? suspended : resumed;
336 } else if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800337 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530338 next_state = USB_CONFIGURED;
339 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800340 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530341 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300342 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300343 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300344 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300345 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530346 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800347 dev->sw_connected = dev->connected;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200348 dev->sw_suspended = dev->suspended;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800349 spin_unlock_irqrestore(&cdev->lock, flags);
350
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300351 if (pm_qos_vote != -1)
352 android_pm_qos_update_latency(dev, pm_qos_vote);
353
Benoit Goby1e8ce152011-12-12 13:01:23 -0800354 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530355 /*
356 * Some userspace modules, e.g. MTP, work correctly only if
357 * CONFIGURED uevent is preceded by DISCONNECT uevent.
358 * Check if we missed sending out a DISCONNECT uevent. This can
359 * happen if host PC resets and configures device really quick.
360 */
361 if (((uevent_envp == connected) &&
362 (last_uevent != USB_DISCONNECTED)) ||
363 ((uevent_envp == configured) &&
364 (last_uevent == USB_CONFIGURED))) {
365 pr_info("%s: sent missed DISCONNECT event\n", __func__);
366 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
367 disconnected);
368 msleep(20);
369 }
370 /*
371 * Before sending out CONFIGURED uevent give function drivers
372 * a chance to wakeup userspace threads and notify disconnect
373 */
374 if (uevent_envp == configured)
375 msleep(50);
376
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +0200377 /* Do not notify on suspend / resume */
378 if (next_state != USB_SUSPENDED && next_state != USB_RESUMED) {
379 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
380 uevent_envp);
381 last_uevent = next_state;
382 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800383 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
384 } else {
385 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
386 dev->connected, dev->sw_connected, cdev->config);
387 }
388}
389
Benoit Goby80ba14d2012-03-19 18:56:52 -0700390static void android_enable(struct android_dev *dev)
391{
392 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300393 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700394
395 if (WARN_ON(!dev->disable_depth))
396 return;
397
398 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300399
400 list_for_each_entry(conf, &dev->configs, list_item)
401 usb_add_config(cdev, &conf->usb_config,
402 android_bind_config);
403
Benoit Goby80ba14d2012-03-19 18:56:52 -0700404 usb_gadget_connect(cdev->gadget);
405 }
406}
407
408static void android_disable(struct android_dev *dev)
409{
410 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300411 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700412
413 if (dev->disable_depth++ == 0) {
414 usb_gadget_disconnect(cdev->gadget);
415 /* Cancel pending control requests */
416 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300417
418 list_for_each_entry(conf, &dev->configs, list_item)
419 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700420 }
421}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800422
423/*-------------------------------------------------------------------------*/
424/* Supported functions initialization */
425
Benoit Goby80ba14d2012-03-19 18:56:52 -0700426struct adb_data {
427 bool opened;
428 bool enabled;
429};
430
Benoit Goby2b6862d2011-12-19 14:38:41 -0800431static int
432adb_function_init(struct android_usb_function *f,
433 struct usb_composite_dev *cdev)
434{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700435 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
436 if (!f->config)
437 return -ENOMEM;
438
Benoit Goby2b6862d2011-12-19 14:38:41 -0800439 return adb_setup();
440}
441
442static void adb_function_cleanup(struct android_usb_function *f)
443{
444 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700445 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800446}
447
448static int
449adb_function_bind_config(struct android_usb_function *f,
450 struct usb_configuration *c)
451{
452 return adb_bind_config(c);
453}
454
Benoit Goby80ba14d2012-03-19 18:56:52 -0700455static void adb_android_function_enable(struct android_usb_function *f)
456{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300457 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700458 struct adb_data *data = f->config;
459
460 data->enabled = true;
461
462 /* Disable the gadget until adbd is ready */
463 if (!data->opened)
464 android_disable(dev);
465}
466
467static void adb_android_function_disable(struct android_usb_function *f)
468{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300469 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700470 struct adb_data *data = f->config;
471
472 data->enabled = false;
473
474 /* Balance the disable that was called in closed_callback */
475 if (!data->opened)
476 android_enable(dev);
477}
478
Benoit Goby2b6862d2011-12-19 14:38:41 -0800479static struct android_usb_function adb_function = {
480 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700481 .enable = adb_android_function_enable,
482 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800483 .init = adb_function_init,
484 .cleanup = adb_function_cleanup,
485 .bind_config = adb_function_bind_config,
486};
487
Benoit Goby80ba14d2012-03-19 18:56:52 -0700488static void adb_ready_callback(void)
489{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300490 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700491 struct adb_data *data = adb_function.config;
492
Benoit Goby80ba14d2012-03-19 18:56:52 -0700493 data->opened = true;
494
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300495 if (data->enabled && dev) {
496 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700497 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300498 mutex_unlock(&dev->mutex);
499 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700500}
501
502static void adb_closed_callback(void)
503{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300504 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700505 struct adb_data *data = adb_function.config;
506
Benoit Goby80ba14d2012-03-19 18:56:52 -0700507 data->opened = false;
508
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300509 if (data->enabled) {
510 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700511 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300512 mutex_unlock(&dev->mutex);
513 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700514}
515
Benoit Goby2b6862d2011-12-19 14:38:41 -0800516
Benoit Gobyaab96812011-04-19 20:37:33 -0700517/*-------------------------------------------------------------------------*/
518/* Supported functions initialization */
519
Stephen Boyd83ed8e22013-03-08 17:04:08 -0800520/* ACM */
521static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
522static ssize_t acm_transports_store(
523 struct device *device, struct device_attribute *attr,
524 const char *buff, size_t size)
525{
526 strlcpy(acm_transports, buff, sizeof(acm_transports));
527
528 return size;
529}
530
531static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
532static struct device_attribute *acm_function_attributes[] = {
533 &dev_attr_acm_transports,
534 NULL
535};
536
537static void acm_function_cleanup(struct android_usb_function *f)
538{
539 gserial_cleanup();
540}
541
542static int
543acm_function_bind_config(struct android_usb_function *f,
544 struct usb_configuration *c)
545{
546 char *name;
547 char buf[32], *b;
548 int err = -1, i;
549 static int acm_initialized, ports;
550
551 if (acm_initialized)
552 goto bind_config;
553
554 acm_initialized = 1;
555 strlcpy(buf, acm_transports, sizeof(buf));
556 b = strim(buf);
557
558 while (b) {
559 name = strsep(&b, ",");
560
561 if (name) {
562 err = acm_init_port(ports, name);
563 if (err) {
564 pr_err("acm: Cannot open port '%s'", name);
565 goto out;
566 }
567 ports++;
568 }
569 }
570 err = acm_port_setup(c);
571 if (err) {
572 pr_err("acm: Cannot setup transports");
573 goto out;
574 }
575
576bind_config:
577 for (i = 0; i < ports; i++) {
578 err = acm_bind_config(c, i);
579 if (err) {
580 pr_err("acm: bind_config failed for port %d", i);
581 goto out;
582 }
583 }
584
585out:
586 return err;
587}
588
589static struct android_usb_function acm_function = {
590 .name = "acm",
591 .cleanup = acm_function_cleanup,
592 .bind_config = acm_function_bind_config,
593 .attributes = acm_function_attributes,
594};
595
Manu Gautam8e0719b2011-09-26 14:47:55 +0530596/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530597static int rmnet_smd_function_bind_config(struct android_usb_function *f,
598 struct usb_configuration *c)
599{
600 return rmnet_smd_bind_config(c);
601}
602
603static struct android_usb_function rmnet_smd_function = {
604 .name = "rmnet_smd",
605 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800606};
607
Manu Gautam8e0719b2011-09-26 14:47:55 +0530608/* RMNET_SDIO */
609static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
610 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800611{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530612 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800613}
614
Manu Gautam8e0719b2011-09-26 14:47:55 +0530615static struct android_usb_function rmnet_sdio_function = {
616 .name = "rmnet_sdio",
617 .bind_config = rmnet_sdio_function_bind_config,
618};
619
620/* RMNET_SMD_SDIO */
621static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
622 struct usb_composite_dev *cdev)
623{
624 return rmnet_smd_sdio_init();
625}
626
627static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
628{
629 rmnet_smd_sdio_cleanup();
630}
631
632static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
633 struct usb_configuration *c)
634{
635 return rmnet_smd_sdio_function_add(c);
636}
637
638static struct device_attribute *rmnet_smd_sdio_attributes[] = {
639 &dev_attr_transport, NULL };
640
641static struct android_usb_function rmnet_smd_sdio_function = {
642 .name = "rmnet_smd_sdio",
643 .init = rmnet_smd_sdio_function_init,
644 .cleanup = rmnet_smd_sdio_function_cleanup,
645 .bind_config = rmnet_smd_sdio_bind_config,
646 .attributes = rmnet_smd_sdio_attributes,
647};
648
Hemant Kumar1b820d52011-11-03 15:08:28 -0700649/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
650#define MAX_XPORT_STR_LEN 50
651static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530652
Manu Gautame3e897c2011-09-12 17:18:46 +0530653static void rmnet_function_cleanup(struct android_usb_function *f)
654{
655 frmnet_cleanup();
656}
657
Manu Gautam2b0234a2011-09-07 16:47:52 +0530658static int rmnet_function_bind_config(struct android_usb_function *f,
659 struct usb_configuration *c)
660{
661 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700662 int err = 0;
663 char *ctrl_name;
664 char *data_name;
665 char buf[MAX_XPORT_STR_LEN], *b;
666 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530667
Hemant Kumar1b820d52011-11-03 15:08:28 -0700668 if (!rmnet_initialized) {
669 rmnet_initialized = 1;
670 strlcpy(buf, rmnet_transports, sizeof(buf));
671 b = strim(buf);
672 while (b) {
673 ctrl_name = strsep(&b, ",");
674 data_name = strsep(&b, ",");
675 if (ctrl_name && data_name) {
676 err = frmnet_init_port(ctrl_name, data_name);
677 if (err) {
678 pr_err("rmnet: Cannot open ctrl port:"
679 "'%s' data port:'%s'\n",
680 ctrl_name, data_name);
681 goto out;
682 }
683 ports++;
684 }
685 }
686
687 err = rmnet_gport_setup();
688 if (err) {
689 pr_err("rmnet: Cannot setup transports");
690 goto out;
691 }
692 }
693
694 for (i = 0; i < ports; i++) {
695 err = frmnet_bind_config(c, i);
696 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530697 pr_err("Could not bind rmnet%u config\n", i);
698 break;
699 }
700 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700701out:
702 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530703}
704
Hemant Kumar1b820d52011-11-03 15:08:28 -0700705static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530706 struct device_attribute *attr, char *buf)
707{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700708 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530709}
710
Hemant Kumar1b820d52011-11-03 15:08:28 -0700711static ssize_t rmnet_transports_store(
712 struct device *device, struct device_attribute *attr,
713 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530714{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700715 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530716
Manu Gautam2b0234a2011-09-07 16:47:52 +0530717 return size;
718}
719
Hemant Kumar1b820d52011-11-03 15:08:28 -0700720static struct device_attribute dev_attr_rmnet_transports =
721 __ATTR(transports, S_IRUGO | S_IWUSR,
722 rmnet_transports_show,
723 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530724static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700725 &dev_attr_rmnet_transports,
726 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530727
728static struct android_usb_function rmnet_function = {
729 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530730 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530731 .bind_config = rmnet_function_bind_config,
732 .attributes = rmnet_function_attributes,
733};
734
Bar Weinered1059e2013-04-16 11:23:55 +0300735static void gps_function_cleanup(struct android_usb_function *f)
736{
737 gps_cleanup();
738}
739
740static int gps_function_bind_config(struct android_usb_function *f,
741 struct usb_configuration *c)
742{
743 int err;
744 static int gps_initialized;
745
746 if (!gps_initialized) {
747 gps_initialized = 1;
748 err = gps_init_port();
749 if (err) {
750 pr_err("gps: Cannot init gps port");
751 return err;
752 }
753 }
754
755 err = gps_gport_setup();
756 if (err) {
757 pr_err("gps: Cannot setup transports");
758 return err;
759 }
760 err = gps_bind_config(c);
761 if (err) {
762 pr_err("Could not bind gps config\n");
763 return err;
764 }
765
766 return 0;
767}
768
769static struct android_usb_function gps_function = {
770 .name = "gps",
771 .cleanup = gps_function_cleanup,
772 .bind_config = gps_function_bind_config,
773};
774
775
Amit Blayf9b352b2013-03-04 15:01:40 +0200776/* ecm transport string */
777static char ecm_transports[MAX_XPORT_STR_LEN];
778
Ofir Cohen7b155422012-07-31 13:02:49 +0300779struct ecm_function_config {
780 u8 ethaddr[ETH_ALEN];
781};
782
783static int ecm_function_init(struct android_usb_function *f,
784 struct usb_composite_dev *cdev)
785{
786 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
787 if (!f->config)
788 return -ENOMEM;
789 return 0;
790}
791
792static void ecm_function_cleanup(struct android_usb_function *f)
793{
794 kfree(f->config);
795 f->config = NULL;
796}
797
798static int ecm_qc_function_bind_config(struct android_usb_function *f,
799 struct usb_configuration *c)
800{
801 int ret;
Amit Blayf9b352b2013-03-04 15:01:40 +0200802 char *trans;
Ofir Cohen7b155422012-07-31 13:02:49 +0300803 struct ecm_function_config *ecm = f->config;
804
805 if (!ecm) {
806 pr_err("%s: ecm_pdata\n", __func__);
807 return -EINVAL;
808 }
809
810 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
811 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
812 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
813
Amit Blayf9b352b2013-03-04 15:01:40 +0200814 pr_debug("%s: ecm_transport is %s", __func__, ecm_transports);
815
816 trans = strim(ecm_transports);
817 if (strcmp("BAM2BAM_IPA", trans)) {
818 ret = gether_qc_setup_name(c->cdev->gadget,
819 ecm->ethaddr, "ecm");
820 if (ret) {
821 pr_err("%s: gether_setup failed\n", __func__);
822 return ret;
823 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300824 }
825
Amit Blayf9b352b2013-03-04 15:01:40 +0200826 return ecm_qc_bind_config(c, ecm->ethaddr, trans);
Ofir Cohen7b155422012-07-31 13:02:49 +0300827}
828
829static void ecm_qc_function_unbind_config(struct android_usb_function *f,
830 struct usb_configuration *c)
831{
Amit Blayf9b352b2013-03-04 15:01:40 +0200832 char *trans = strim(ecm_transports);
833
834 if (strcmp("BAM2BAM_IPA", trans))
835 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300836}
837
838static ssize_t ecm_ethaddr_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
840{
841 struct android_usb_function *f = dev_get_drvdata(dev);
842 struct ecm_function_config *ecm = f->config;
843 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
844 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
845 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
846}
847
848static ssize_t ecm_ethaddr_store(struct device *dev,
849 struct device_attribute *attr, const char *buf, size_t size)
850{
851 struct android_usb_function *f = dev_get_drvdata(dev);
852 struct ecm_function_config *ecm = f->config;
853
854 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
855 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
856 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
857 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
858 return size;
859 return -EINVAL;
860}
861
862static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
863 ecm_ethaddr_store);
864
Amit Blayf9b352b2013-03-04 15:01:40 +0200865static ssize_t ecm_transports_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
867{
868 return snprintf(buf, PAGE_SIZE, "%s\n", ecm_transports);
869}
870
871static ssize_t ecm_transports_store(struct device *dev,
872 struct device_attribute *attr, const char *buf, size_t size)
873{
874 strlcpy(ecm_transports, buf, sizeof(ecm_transports));
875 return size;
876}
877
878static DEVICE_ATTR(ecm_transports, S_IRUGO | S_IWUSR, ecm_transports_show,
879 ecm_transports_store);
880
Ofir Cohen7b155422012-07-31 13:02:49 +0300881static struct device_attribute *ecm_function_attributes[] = {
Amit Blayf9b352b2013-03-04 15:01:40 +0200882 &dev_attr_ecm_transports,
Ofir Cohen7b155422012-07-31 13:02:49 +0300883 &dev_attr_ecm_ethaddr,
884 NULL
885};
886
887static struct android_usb_function ecm_qc_function = {
888 .name = "ecm_qc",
889 .init = ecm_function_init,
890 .cleanup = ecm_function_cleanup,
891 .bind_config = ecm_qc_function_bind_config,
892 .unbind_config = ecm_qc_function_unbind_config,
893 .attributes = ecm_function_attributes,
894};
Anna Perela8c991d2012-04-09 16:44:46 +0300895
896/* MBIM - used with BAM */
897#define MAX_MBIM_INSTANCES 1
898
899static int mbim_function_init(struct android_usb_function *f,
900 struct usb_composite_dev *cdev)
901{
902 return mbim_init(MAX_MBIM_INSTANCES);
903}
904
905static void mbim_function_cleanup(struct android_usb_function *f)
906{
907 fmbim_cleanup();
908}
909
Lena Salmandf7e7992013-03-15 09:46:27 +0200910
911/* mbim transport string */
912static char mbim_transports[MAX_XPORT_STR_LEN];
913
Anna Perela8c991d2012-04-09 16:44:46 +0300914static int mbim_function_bind_config(struct android_usb_function *f,
915 struct usb_configuration *c)
916{
Lena Salmandf7e7992013-03-15 09:46:27 +0200917 char *trans;
918
919 pr_debug("%s: mbim transport is %s", __func__, mbim_transports);
920 trans = strim(mbim_transports);
921 return mbim_bind_config(c, 0, trans);
Anna Perela8c991d2012-04-09 16:44:46 +0300922}
923
Jack Pham2df2f702012-10-11 19:08:24 -0700924static int mbim_function_ctrlrequest(struct android_usb_function *f,
925 struct usb_composite_dev *cdev,
926 const struct usb_ctrlrequest *c)
927{
928 return mbim_ctrlrequest(cdev, c);
929}
930
Lena Salmandf7e7992013-03-15 09:46:27 +0200931static ssize_t mbim_transports_show(struct device *dev,
932 struct device_attribute *attr, char *buf)
933{
934 return snprintf(buf, PAGE_SIZE, "%s\n", mbim_transports);
935}
936
937static ssize_t mbim_transports_store(struct device *dev,
938 struct device_attribute *attr, const char *buf, size_t size)
939{
940 strlcpy(mbim_transports, buf, sizeof(mbim_transports));
941 return size;
942}
943
944static DEVICE_ATTR(mbim_transports, S_IRUGO | S_IWUSR, mbim_transports_show,
945 mbim_transports_store);
946
947static struct device_attribute *mbim_function_attributes[] = {
948 &dev_attr_mbim_transports,
949 NULL
950};
951
Anna Perela8c991d2012-04-09 16:44:46 +0300952static struct android_usb_function mbim_function = {
953 .name = "usb_mbim",
954 .cleanup = mbim_function_cleanup,
955 .bind_config = mbim_function_bind_config,
956 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -0700957 .ctrlrequest = mbim_function_ctrlrequest,
Lena Salmandf7e7992013-03-15 09:46:27 +0200958 .attributes = mbim_function_attributes,
Anna Perela8c991d2012-04-09 16:44:46 +0300959};
960
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700961#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +0300962/* PERIPHERAL AUDIO */
963static int audio_function_bind_config(struct android_usb_function *f,
964 struct usb_configuration *c)
965{
966 return audio_bind_config(c);
967}
968
969static struct android_usb_function audio_function = {
970 .name = "audio",
971 .bind_config = audio_function_bind_config,
972};
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700973#endif
Anna Perel432367a2012-09-20 10:55:32 +0300974
Anna Perela8c991d2012-04-09 16:44:46 +0300975
Manu Gautam8e0719b2011-09-26 14:47:55 +0530976/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530977static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978static ssize_t clients_store(
979 struct device *device, struct device_attribute *attr,
980 const char *buff, size_t size)
981{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530982 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983
984 return size;
985}
986
987static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
988static struct device_attribute *diag_function_attributes[] =
989 { &dev_attr_clients, NULL };
990
991static int diag_function_init(struct android_usb_function *f,
992 struct usb_composite_dev *cdev)
993{
994 return diag_setup();
995}
996
997static void diag_function_cleanup(struct android_usb_function *f)
998{
999 diag_cleanup();
1000}
1001
1002static int diag_function_bind_config(struct android_usb_function *f,
1003 struct usb_configuration *c)
1004{
1005 char *name;
1006 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +05301007 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -08001008 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001009 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301011 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 b = strim(buf);
1013
1014 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -08001015 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +05301017 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -07001018 if (!once++) {
1019 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
1020 notify = dev->pdata->update_pid_and_serial_num;
1021 else
1022 notify = usb_diag_update_pid_and_serial_num;
1023 }
Manu Gautamc5760302011-08-25 14:30:24 +05301024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +05301026 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 if (err)
1028 pr_err("diag: Cannot open channel '%s'", name);
1029 }
1030 }
1031
1032 return err;
1033}
1034
1035static struct android_usb_function diag_function = {
1036 .name = "diag",
1037 .init = diag_function_init,
1038 .cleanup = diag_function_cleanup,
1039 .bind_config = diag_function_bind_config,
1040 .attributes = diag_function_attributes,
1041};
1042
Shimrit Malichia00d7322012-08-05 13:56:28 +03001043/* DEBUG */
1044static int qdss_function_init(struct android_usb_function *f,
1045 struct usb_composite_dev *cdev)
1046{
1047 return qdss_setup();
1048}
1049
1050static void qdss_function_cleanup(struct android_usb_function *f)
1051{
1052 qdss_cleanup();
1053}
1054
1055static int qdss_function_bind_config(struct android_usb_function *f,
1056 struct usb_configuration *c)
1057{
1058 int err = -1;
1059
1060 err = qdss_bind_config(c, "qdss");
1061 if (err)
1062 pr_err("qdss: Cannot open channel qdss");
1063
1064 return err;
1065}
1066
1067static struct android_usb_function qdss_function = {
1068 .name = "qdss",
1069 .init = qdss_function_init,
1070 .cleanup = qdss_function_cleanup,
1071 .bind_config = qdss_function_bind_config,
1072};
1073
Manu Gautam8e0719b2011-09-26 14:47:55 +05301074/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301075static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +05301076static ssize_t serial_transports_store(
1077 struct device *device, struct device_attribute *attr,
1078 const char *buff, size_t size)
1079{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301080 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +05301081
1082 return size;
1083}
1084
1085static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
1086static struct device_attribute *serial_function_attributes[] =
1087 { &dev_attr_transports, NULL };
1088
1089static void serial_function_cleanup(struct android_usb_function *f)
1090{
1091 gserial_cleanup();
1092}
1093
1094static int serial_function_bind_config(struct android_usb_function *f,
1095 struct usb_configuration *c)
1096{
1097 char *name;
1098 char buf[32], *b;
1099 int err = -1, i;
1100 static int serial_initialized = 0, ports = 0;
1101
1102 if (serial_initialized)
1103 goto bind_config;
1104
1105 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301106 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +05301107 b = strim(buf);
1108
1109 while (b) {
1110 name = strsep(&b, ",");
1111
1112 if (name) {
1113 err = gserial_init_port(ports, name);
1114 if (err) {
1115 pr_err("serial: Cannot open port '%s'", name);
1116 goto out;
1117 }
1118 ports++;
1119 }
1120 }
1121 err = gport_setup(c);
1122 if (err) {
1123 pr_err("serial: Cannot setup transports");
1124 goto out;
1125 }
1126
1127bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +02001128 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +05301129 err = gser_bind_config(c, i);
1130 if (err) {
1131 pr_err("serial: bind_config failed for port %d", i);
1132 goto out;
1133 }
1134 }
1135
1136out:
1137 return err;
1138}
1139
1140static struct android_usb_function serial_function = {
1141 .name = "serial",
1142 .cleanup = serial_function_cleanup,
1143 .bind_config = serial_function_bind_config,
1144 .attributes = serial_function_attributes,
1145};
1146
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301147/* CCID */
1148static int ccid_function_init(struct android_usb_function *f,
1149 struct usb_composite_dev *cdev)
1150{
1151 return ccid_setup();
1152}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001153
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301154static void ccid_function_cleanup(struct android_usb_function *f)
1155{
1156 ccid_cleanup();
1157}
1158
1159static int ccid_function_bind_config(struct android_usb_function *f,
1160 struct usb_configuration *c)
1161{
1162 return ccid_bind_config(c);
1163}
1164
1165static struct android_usb_function ccid_function = {
1166 .name = "ccid",
1167 .init = ccid_function_init,
1168 .cleanup = ccid_function_cleanup,
1169 .bind_config = ccid_function_bind_config,
1170};
1171
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001172static int
1173mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001174 struct usb_composite_dev *cdev)
1175{
1176 return mtp_setup();
1177}
1178
1179static void mtp_function_cleanup(struct android_usb_function *f)
1180{
1181 mtp_cleanup();
1182}
1183
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001184static int
1185mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001186 struct usb_configuration *c)
1187{
1188 return mtp_bind_config(c, false);
1189}
1190
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001191static int
1192ptp_function_init(struct android_usb_function *f,
1193 struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001194{
1195 /* nothing to do - initialization is handled by mtp_function_init */
1196 return 0;
1197}
1198
1199static void ptp_function_cleanup(struct android_usb_function *f)
1200{
1201 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1202}
1203
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001204static int
1205ptp_function_bind_config(struct android_usb_function *f,
1206 struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001207{
1208 return mtp_bind_config(c, true);
1209}
1210
1211static int mtp_function_ctrlrequest(struct android_usb_function *f,
1212 struct usb_composite_dev *cdev,
1213 const struct usb_ctrlrequest *c)
1214{
1215 return mtp_ctrlrequest(cdev, c);
1216}
1217
1218static struct android_usb_function mtp_function = {
1219 .name = "mtp",
1220 .init = mtp_function_init,
1221 .cleanup = mtp_function_cleanup,
1222 .bind_config = mtp_function_bind_config,
1223 .ctrlrequest = mtp_function_ctrlrequest,
1224};
1225
1226/* PTP function is same as MTP with slightly different interface descriptor */
1227static struct android_usb_function ptp_function = {
1228 .name = "ptp",
1229 .init = ptp_function_init,
1230 .cleanup = ptp_function_cleanup,
1231 .bind_config = ptp_function_bind_config,
1232};
1233
1234
Benoit Goby1e8ce152011-12-12 13:01:23 -08001235struct rndis_function_config {
1236 u8 ethaddr[ETH_ALEN];
1237 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001238 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001239 char manufacturer[256];
1240 /* "Wireless" RNDIS; auto-detected by Windows */
1241 bool wceis;
1242};
1243
1244static int
1245rndis_function_init(struct android_usb_function *f,
1246 struct usb_composite_dev *cdev)
1247{
1248 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1249 if (!f->config)
1250 return -ENOMEM;
1251 return 0;
1252}
1253
1254static void rndis_function_cleanup(struct android_usb_function *f)
1255{
1256 kfree(f->config);
1257 f->config = NULL;
1258}
1259
Ofir Cohenaef90b72012-07-31 12:37:04 +02001260static int rndis_qc_function_init(struct android_usb_function *f,
1261 struct usb_composite_dev *cdev)
1262{
1263 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1264 if (!f->config)
1265 return -ENOMEM;
1266
1267 return rndis_qc_init();
1268}
1269
1270static void rndis_qc_function_cleanup(struct android_usb_function *f)
1271{
1272 rndis_qc_cleanup();
1273 kfree(f->config);
1274}
1275
Benoit Goby1e8ce152011-12-12 13:01:23 -08001276static int
1277rndis_function_bind_config(struct android_usb_function *f,
1278 struct usb_configuration *c)
1279{
1280 int ret;
1281 struct rndis_function_config *rndis = f->config;
1282
1283 if (!rndis) {
1284 pr_err("%s: rndis_pdata\n", __func__);
1285 return -1;
1286 }
1287
1288 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1289 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1290 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1291
1292 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1293 if (ret) {
1294 pr_err("%s: gether_setup failed\n", __func__);
1295 return ret;
1296 }
1297
1298 if (rndis->wceis) {
1299 /* "Wireless" RNDIS; auto-detected by Windows */
1300 rndis_iad_descriptor.bFunctionClass =
1301 USB_CLASS_WIRELESS_CONTROLLER;
1302 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1303 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1304 rndis_control_intf.bInterfaceClass =
1305 USB_CLASS_WIRELESS_CONTROLLER;
1306 rndis_control_intf.bInterfaceSubClass = 0x01;
1307 rndis_control_intf.bInterfaceProtocol = 0x03;
1308 }
1309
1310 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1311 rndis->manufacturer);
1312}
1313
Ofir Cohenaef90b72012-07-31 12:37:04 +02001314static int rndis_qc_function_bind_config(struct android_usb_function *f,
1315 struct usb_configuration *c)
1316{
1317 int ret;
1318 struct rndis_function_config *rndis = f->config;
1319
1320 if (!rndis) {
1321 pr_err("%s: rndis_pdata\n", __func__);
1322 return -EINVAL;
1323 }
1324
1325 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1326 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1327 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1328
1329 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1330 if (ret) {
1331 pr_err("%s: gether_setup failed\n", __func__);
1332 return ret;
1333 }
1334
1335 if (rndis->wceis) {
1336 /* "Wireless" RNDIS; auto-detected by Windows */
1337 rndis_qc_iad_descriptor.bFunctionClass =
1338 USB_CLASS_WIRELESS_CONTROLLER;
1339 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1340 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1341 rndis_qc_control_intf.bInterfaceClass =
1342 USB_CLASS_WIRELESS_CONTROLLER;
1343 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1344 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1345 }
1346
1347 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1348 rndis->manufacturer,
1349 rndis->max_pkt_per_xfer);
1350}
1351
Benoit Goby1e8ce152011-12-12 13:01:23 -08001352static void rndis_function_unbind_config(struct android_usb_function *f,
1353 struct usb_configuration *c)
1354{
1355 gether_cleanup();
1356}
1357
Ofir Cohenaef90b72012-07-31 12:37:04 +02001358static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1359 struct usb_configuration *c)
1360{
Amit Blayd6d690a2012-10-16 13:37:42 +02001361 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001362}
1363
Benoit Goby1e8ce152011-12-12 13:01:23 -08001364static ssize_t rndis_manufacturer_show(struct device *dev,
1365 struct device_attribute *attr, char *buf)
1366{
1367 struct android_usb_function *f = dev_get_drvdata(dev);
1368 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001369
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301370 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001371}
1372
1373static ssize_t rndis_manufacturer_store(struct device *dev,
1374 struct device_attribute *attr, const char *buf, size_t size)
1375{
1376 struct android_usb_function *f = dev_get_drvdata(dev);
1377 struct rndis_function_config *config = f->config;
1378
1379 if (size >= sizeof(config->manufacturer))
1380 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001381
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301382 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001383 return size;
1384 return -1;
1385}
1386
1387static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1388 rndis_manufacturer_store);
1389
1390static ssize_t rndis_wceis_show(struct device *dev,
1391 struct device_attribute *attr, char *buf)
1392{
1393 struct android_usb_function *f = dev_get_drvdata(dev);
1394 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001395
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301396 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001397}
1398
1399static ssize_t rndis_wceis_store(struct device *dev,
1400 struct device_attribute *attr, const char *buf, size_t size)
1401{
1402 struct android_usb_function *f = dev_get_drvdata(dev);
1403 struct rndis_function_config *config = f->config;
1404 int value;
1405
1406 if (sscanf(buf, "%d", &value) == 1) {
1407 config->wceis = value;
1408 return size;
1409 }
1410 return -EINVAL;
1411}
1412
1413static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1414 rndis_wceis_store);
1415
1416static ssize_t rndis_ethaddr_show(struct device *dev,
1417 struct device_attribute *attr, char *buf)
1418{
1419 struct android_usb_function *f = dev_get_drvdata(dev);
1420 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001421
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301422 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001423 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1424 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1425}
1426
1427static ssize_t rndis_ethaddr_store(struct device *dev,
1428 struct device_attribute *attr, const char *buf, size_t size)
1429{
1430 struct android_usb_function *f = dev_get_drvdata(dev);
1431 struct rndis_function_config *rndis = f->config;
1432
1433 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1434 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1435 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1436 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1437 return size;
1438 return -EINVAL;
1439}
1440
1441static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1442 rndis_ethaddr_store);
1443
1444static ssize_t rndis_vendorID_show(struct device *dev,
1445 struct device_attribute *attr, char *buf)
1446{
1447 struct android_usb_function *f = dev_get_drvdata(dev);
1448 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001449
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301450 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001451}
1452
1453static ssize_t rndis_vendorID_store(struct device *dev,
1454 struct device_attribute *attr, const char *buf, size_t size)
1455{
1456 struct android_usb_function *f = dev_get_drvdata(dev);
1457 struct rndis_function_config *config = f->config;
1458 int value;
1459
1460 if (sscanf(buf, "%04x", &value) == 1) {
1461 config->vendorID = value;
1462 return size;
1463 }
1464 return -EINVAL;
1465}
1466
1467static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1468 rndis_vendorID_store);
1469
Ofir Cohenaef90b72012-07-31 12:37:04 +02001470static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1471 struct device_attribute *attr, char *buf)
1472{
1473 struct android_usb_function *f = dev_get_drvdata(dev);
1474 struct rndis_function_config *config = f->config;
1475 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1476}
1477
1478static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1479 struct device_attribute *attr, const char *buf, size_t size)
1480{
1481 struct android_usb_function *f = dev_get_drvdata(dev);
1482 struct rndis_function_config *config = f->config;
1483 int value;
1484
1485 if (sscanf(buf, "%d", &value) == 1) {
1486 config->max_pkt_per_xfer = value;
1487 return size;
1488 }
1489 return -EINVAL;
1490}
1491
1492static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1493 rndis_max_pkt_per_xfer_show,
1494 rndis_max_pkt_per_xfer_store);
1495
Benoit Goby1e8ce152011-12-12 13:01:23 -08001496static struct device_attribute *rndis_function_attributes[] = {
1497 &dev_attr_manufacturer,
1498 &dev_attr_wceis,
1499 &dev_attr_ethaddr,
1500 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001501 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001502 NULL
1503};
1504
1505static struct android_usb_function rndis_function = {
1506 .name = "rndis",
1507 .init = rndis_function_init,
1508 .cleanup = rndis_function_cleanup,
1509 .bind_config = rndis_function_bind_config,
1510 .unbind_config = rndis_function_unbind_config,
1511 .attributes = rndis_function_attributes,
1512};
1513
Ofir Cohenaef90b72012-07-31 12:37:04 +02001514static struct android_usb_function rndis_qc_function = {
1515 .name = "rndis_qc",
1516 .init = rndis_qc_function_init,
1517 .cleanup = rndis_qc_function_cleanup,
1518 .bind_config = rndis_qc_function_bind_config,
1519 .unbind_config = rndis_qc_function_unbind_config,
1520 .attributes = rndis_function_attributes,
1521};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001522
Anna Perelf9d01552012-11-20 15:56:32 +02001523static int ecm_function_bind_config(struct android_usb_function *f,
1524 struct usb_configuration *c)
1525{
1526 int ret;
1527 struct ecm_function_config *ecm = f->config;
1528
1529 if (!ecm) {
1530 pr_err("%s: ecm_pdata\n", __func__);
1531 return -EINVAL;
1532 }
1533
1534 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1535 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1536 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1537
1538 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1539 if (ret) {
1540 pr_err("%s: gether_setup failed\n", __func__);
1541 return ret;
1542 }
1543
1544 ret = ecm_bind_config(c, ecm->ethaddr);
1545 if (ret) {
1546 pr_err("%s: ecm_bind_config failed\n", __func__);
1547 gether_cleanup();
1548 }
1549 return ret;
1550}
1551
1552static void ecm_function_unbind_config(struct android_usb_function *f,
1553 struct usb_configuration *c)
1554{
1555 gether_cleanup();
1556}
1557
1558static struct android_usb_function ecm_function = {
1559 .name = "ecm",
1560 .init = ecm_function_init,
1561 .cleanup = ecm_function_cleanup,
1562 .bind_config = ecm_function_bind_config,
1563 .unbind_config = ecm_function_unbind_config,
1564 .attributes = ecm_function_attributes,
1565};
1566
Benoit Goby1e8ce152011-12-12 13:01:23 -08001567struct mass_storage_function_config {
1568 struct fsg_config fsg;
1569 struct fsg_common *common;
1570};
1571
1572static int mass_storage_function_init(struct android_usb_function *f,
1573 struct usb_composite_dev *cdev)
1574{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001575 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001576 struct mass_storage_function_config *config;
1577 struct fsg_common *common;
1578 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301579 int i;
1580 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001581
1582 config = kzalloc(sizeof(struct mass_storage_function_config),
1583 GFP_KERNEL);
1584 if (!config)
1585 return -ENOMEM;
1586
1587 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301588 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301589 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301590 config->fsg.nluns = 2;
1591 config->fsg.luns[1].cdrom = 1;
1592 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301593 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301594 name[1] = "lun0";
1595 }
1596
Benoit Goby1e8ce152011-12-12 13:01:23 -08001597 config->fsg.luns[0].removable = 1;
1598
1599 common = fsg_common_init(NULL, cdev, &config->fsg);
1600 if (IS_ERR(common)) {
1601 kfree(config);
1602 return PTR_ERR(common);
1603 }
1604
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301605 for (i = 0; i < config->fsg.nluns; i++) {
1606 err = sysfs_create_link(&f->dev->kobj,
1607 &common->luns[i].dev.kobj,
1608 name[i]);
1609 if (err)
1610 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001611 }
1612
1613 config->common = common;
1614 f->config = config;
1615 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301616error:
1617 for (; i > 0 ; i--)
1618 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1619
1620 fsg_common_release(&common->ref);
1621 kfree(config);
1622 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001623}
1624
1625static void mass_storage_function_cleanup(struct android_usb_function *f)
1626{
1627 kfree(f->config);
1628 f->config = NULL;
1629}
1630
1631static int mass_storage_function_bind_config(struct android_usb_function *f,
1632 struct usb_configuration *c)
1633{
1634 struct mass_storage_function_config *config = f->config;
1635 return fsg_bind_config(c->cdev, c, config->common);
1636}
1637
1638static ssize_t mass_storage_inquiry_show(struct device *dev,
1639 struct device_attribute *attr, char *buf)
1640{
1641 struct android_usb_function *f = dev_get_drvdata(dev);
1642 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301643 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001644}
1645
1646static ssize_t mass_storage_inquiry_store(struct device *dev,
1647 struct device_attribute *attr, const char *buf, size_t size)
1648{
1649 struct android_usb_function *f = dev_get_drvdata(dev);
1650 struct mass_storage_function_config *config = f->config;
1651 if (size >= sizeof(config->common->inquiry_string))
1652 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301653 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001654 return -EINVAL;
1655 return size;
1656}
1657
1658static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1659 mass_storage_inquiry_show,
1660 mass_storage_inquiry_store);
1661
1662static struct device_attribute *mass_storage_function_attributes[] = {
1663 &dev_attr_inquiry_string,
1664 NULL
1665};
1666
1667static struct android_usb_function mass_storage_function = {
1668 .name = "mass_storage",
1669 .init = mass_storage_function_init,
1670 .cleanup = mass_storage_function_cleanup,
1671 .bind_config = mass_storage_function_bind_config,
1672 .attributes = mass_storage_function_attributes,
1673};
1674
1675
Benoit Gobycf3fc062011-12-19 14:39:37 -08001676static int accessory_function_init(struct android_usb_function *f,
1677 struct usb_composite_dev *cdev)
1678{
1679 return acc_setup();
1680}
1681
1682static void accessory_function_cleanup(struct android_usb_function *f)
1683{
1684 acc_cleanup();
1685}
1686
1687static int accessory_function_bind_config(struct android_usb_function *f,
1688 struct usb_configuration *c)
1689{
1690 return acc_bind_config(c);
1691}
1692
1693static int accessory_function_ctrlrequest(struct android_usb_function *f,
1694 struct usb_composite_dev *cdev,
1695 const struct usb_ctrlrequest *c)
1696{
1697 return acc_ctrlrequest(cdev, c);
1698}
1699
1700static struct android_usb_function accessory_function = {
1701 .name = "accessory",
1702 .init = accessory_function_init,
1703 .cleanup = accessory_function_cleanup,
1704 .bind_config = accessory_function_bind_config,
1705 .ctrlrequest = accessory_function_ctrlrequest,
1706};
1707
Anna Perel3ee23dd2013-02-26 16:06:40 +02001708#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301709static int audio_source_function_init(struct android_usb_function *f,
1710 struct usb_composite_dev *cdev)
1711{
1712 struct audio_source_config *config;
1713
1714 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1715 if (!config)
1716 return -ENOMEM;
1717 config->card = -1;
1718 config->device = -1;
1719 f->config = config;
1720 return 0;
1721}
1722
1723static void audio_source_function_cleanup(struct android_usb_function *f)
1724{
1725 kfree(f->config);
1726}
1727
1728static int audio_source_function_bind_config(struct android_usb_function *f,
1729 struct usb_configuration *c)
1730{
1731 struct audio_source_config *config = f->config;
1732
1733 return audio_source_bind_config(c, config);
1734}
1735
1736static void audio_source_function_unbind_config(struct android_usb_function *f,
1737 struct usb_configuration *c)
1738{
1739 struct audio_source_config *config = f->config;
1740
1741 config->card = -1;
1742 config->device = -1;
1743}
1744
1745static ssize_t audio_source_pcm_show(struct device *dev,
1746 struct device_attribute *attr, char *buf)
1747{
1748 struct android_usb_function *f = dev_get_drvdata(dev);
1749 struct audio_source_config *config = f->config;
1750
1751 /* print PCM card and device numbers */
1752 return sprintf(buf, "%d %d\n", config->card, config->device);
1753}
1754
1755static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1756
1757static struct device_attribute *audio_source_function_attributes[] = {
1758 &dev_attr_pcm,
1759 NULL
1760};
1761
1762static struct android_usb_function audio_source_function = {
1763 .name = "audio_source",
1764 .init = audio_source_function_init,
1765 .cleanup = audio_source_function_cleanup,
1766 .bind_config = audio_source_function_bind_config,
1767 .unbind_config = audio_source_function_unbind_config,
1768 .attributes = audio_source_function_attributes,
1769};
Anna Perel3ee23dd2013-02-26 16:06:40 +02001770#endif
Mike Lockwood11874822012-08-27 16:43:53 +05301771
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301772static int android_uasp_connect_cb(bool connect)
1773{
1774 /*
1775 * TODO
1776 * We may have to disable gadget till UASP configfs nodes
1777 * are configured which includes mapping LUN with the
1778 * backing file. It is a fundamental difference between
1779 * f_mass_storage and f_tcp. That means UASP can not be
1780 * in default composition.
1781 *
1782 * For now, assume that UASP configfs nodes are configured
1783 * before enabling android gadget. Or cable should be
1784 * reconnected after mapping the LUN.
1785 *
1786 * Also consider making UASP to respond to Host requests when
1787 * Lun is not mapped.
1788 */
1789 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1790
1791 return 0;
1792}
1793
1794static int uasp_function_init(struct android_usb_function *f,
1795 struct usb_composite_dev *cdev)
1796{
1797 return f_tcm_init(&android_uasp_connect_cb);
1798}
1799
1800static void uasp_function_cleanup(struct android_usb_function *f)
1801{
1802 f_tcm_exit();
1803}
1804
1805static int uasp_function_bind_config(struct android_usb_function *f,
1806 struct usb_configuration *c)
1807{
1808 return tcm_bind_config(c);
1809}
1810
1811static struct android_usb_function uasp_function = {
1812 .name = "uasp",
1813 .init = uasp_function_init,
1814 .cleanup = uasp_function_cleanup,
1815 .bind_config = uasp_function_bind_config,
1816};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001817
Benoit Goby1e8ce152011-12-12 13:01:23 -08001818static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001819 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001820 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001821#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001822 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001823#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301824 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301825 &rmnet_sdio_function,
1826 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301827 &rmnet_function,
Bar Weinered1059e2013-04-16 11:23:55 +03001828 &gps_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001830 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301831 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001832 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301833 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001834 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001835 &mtp_function,
1836 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001837 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001838 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02001839 &ecm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001840 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001841 &accessory_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001842#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301843 &audio_source_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001844#endif
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301845 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001846 NULL
1847};
1848
Lena Salmand092f2d2012-03-12 17:27:24 +02001849static void android_cleanup_functions(struct android_usb_function **functions)
1850{
1851 struct android_usb_function *f;
1852 struct device_attribute **attrs;
1853 struct device_attribute *attr;
1854
1855 while (*functions) {
1856 f = *functions++;
1857
1858 if (f->dev) {
1859 device_destroy(android_class, f->dev->devt);
1860 kfree(f->dev_name);
1861 } else
1862 continue;
1863
1864 if (f->cleanup)
1865 f->cleanup(f);
1866
1867 attrs = f->attributes;
1868 if (attrs) {
1869 while ((attr = *attrs++))
1870 device_remove_file(f->dev, attr);
1871 }
1872 }
1873}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001874
1875static int android_init_functions(struct android_usb_function **functions,
1876 struct usb_composite_dev *cdev)
1877{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001878 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001879 struct android_usb_function *f;
1880 struct device_attribute **attrs;
1881 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301882 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001883 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001884
1885 for (; (f = *functions++); index++) {
1886 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001887 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001888 if (!f->dev_name) {
1889 err = -ENOMEM;
1890 goto err_out;
1891 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001892 f->dev = device_create(android_class, dev->dev,
1893 MKDEV(0, index), f, f->dev_name);
1894 if (IS_ERR(f->dev)) {
1895 pr_err("%s: Failed to create dev %s", __func__,
1896 f->dev_name);
1897 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001898 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001899 goto err_create;
1900 }
1901
1902 if (f->init) {
1903 err = f->init(f, cdev);
1904 if (err) {
1905 pr_err("%s: Failed to init %s", __func__,
1906 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001907 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001908 }
1909 }
1910
1911 attrs = f->attributes;
1912 if (attrs) {
1913 while ((attr = *attrs++) && !err)
1914 err = device_create_file(f->dev, attr);
1915 }
1916 if (err) {
1917 pr_err("%s: Failed to create function %s attributes",
1918 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001919 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001920 }
1921 }
1922 return 0;
1923
Lena Salmand092f2d2012-03-12 17:27:24 +02001924err_attrs:
1925 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1926 device_remove_file(f->dev, attr);
1927 if (f->cleanup)
1928 f->cleanup(f);
1929err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001930 device_destroy(android_class, f->dev->devt);
1931err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001932 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001933 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001934err_out:
1935 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001936 return err;
1937}
1938
Benoit Goby1e8ce152011-12-12 13:01:23 -08001939static int
1940android_bind_enabled_functions(struct android_dev *dev,
1941 struct usb_configuration *c)
1942{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001943 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001944 struct android_configuration *conf =
1945 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001946 int ret;
1947
Ido Shayevitz68557e32012-11-06 12:40:37 +02001948 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1949 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001950 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001951 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001952 return ret;
1953 }
1954 }
1955 return 0;
1956}
1957
1958static void
1959android_unbind_enabled_functions(struct android_dev *dev,
1960 struct usb_configuration *c)
1961{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001962 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001963 struct android_configuration *conf =
1964 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001965
Ido Shayevitz68557e32012-11-06 12:40:37 +02001966 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1967 if (f_holder->f->unbind_config)
1968 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001969 }
1970}
1971
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001972static int android_enable_function(struct android_dev *dev,
1973 struct android_configuration *conf,
1974 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001975{
1976 struct android_usb_function **functions = dev->functions;
1977 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001978 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001979 while ((f = *functions++)) {
1980 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001981 if (f->android_dev && f->android_dev != dev)
1982 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001983 f->name);
1984 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001985 f_holder = kzalloc(sizeof(*f_holder),
1986 GFP_KERNEL);
1987 if (!f_holder) {
1988 pr_err("Failed to alloc f_holder\n");
1989 return -ENOMEM;
1990 }
1991
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001992 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001993 f_holder->f = f;
1994 list_add_tail(&f_holder->enabled_list,
1995 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001996 return 0;
1997 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001998 }
1999 }
2000 return -EINVAL;
2001}
2002
2003/*-------------------------------------------------------------------------*/
2004/* /sys/class/android_usb/android%d/ interface */
2005
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302006static ssize_t remote_wakeup_show(struct device *pdev,
2007 struct device_attribute *attr, char *buf)
2008{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002009 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002010 struct android_configuration *conf;
2011
2012 /*
2013 * Show the wakeup attribute of the first configuration,
2014 * since all configurations have the same wakeup attribute
2015 */
2016 if (dev->configs_num == 0)
2017 return 0;
2018 conf = list_entry(dev->configs.next,
2019 struct android_configuration,
2020 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002021
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302022 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002023 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302024 USB_CONFIG_ATT_WAKEUP));
2025}
2026
2027static ssize_t remote_wakeup_store(struct device *pdev,
2028 struct device_attribute *attr, const char *buff, size_t size)
2029{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002030 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002031 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302032 int enable = 0;
2033
2034 sscanf(buff, "%d", &enable);
2035
2036 pr_debug("android_usb: %s remote wakeup\n",
2037 enable ? "enabling" : "disabling");
2038
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002039 list_for_each_entry(conf, &dev->configs, list_item)
2040 if (enable)
2041 conf->usb_config.bmAttributes |=
2042 USB_CONFIG_ATT_WAKEUP;
2043 else
2044 conf->usb_config.bmAttributes &=
2045 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302046
2047 return size;
2048}
2049
Benoit Goby1e8ce152011-12-12 13:01:23 -08002050static ssize_t
2051functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2052{
2053 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002054 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002055 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002056 char *buff = buf;
2057
2058 mutex_lock(&dev->mutex);
2059
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002060 list_for_each_entry(conf, &dev->configs, list_item) {
2061 if (buff != buf)
2062 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02002063 list_for_each_entry(f_holder, &conf->enabled_functions,
2064 enabled_list)
2065 buff += snprintf(buff, PAGE_SIZE, "%s,",
2066 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002067 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002068
2069 mutex_unlock(&dev->mutex);
2070
2071 if (buff != buf)
2072 *(buff-1) = '\n';
2073 return buff - buf;
2074}
2075
2076static ssize_t
2077functions_store(struct device *pdev, struct device_attribute *attr,
2078 const char *buff, size_t size)
2079{
2080 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002081 struct list_head *curr_conf = &dev->configs;
2082 struct android_configuration *conf;
2083 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002084 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002085 char *name;
2086 char buf[256], *b;
2087 int err;
2088
2089 mutex_lock(&dev->mutex);
2090
2091 if (dev->enabled) {
2092 mutex_unlock(&dev->mutex);
2093 return -EBUSY;
2094 }
2095
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002096 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002097 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002098 while (conf->enabled_functions.next !=
2099 &conf->enabled_functions) {
2100 f_holder = list_entry(conf->enabled_functions.next,
2101 typeof(*f_holder),
2102 enabled_list);
2103 f_holder->f->android_dev = NULL;
2104 list_del(&f_holder->enabled_list);
2105 kfree(f_holder);
2106 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002107 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002108 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002109
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302110 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002111 b = strim(buf);
2112
2113 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002114 conf_str = strsep(&b, ":");
2115 if (conf_str) {
2116 /* If the next not equal to the head, take it */
2117 if (curr_conf->next != &dev->configs)
2118 conf = list_entry(curr_conf->next,
2119 struct android_configuration,
2120 list_item);
2121 else
2122 conf = alloc_android_config(dev);
2123
2124 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002125 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002126
2127 while (conf_str) {
2128 name = strsep(&conf_str, ",");
2129 if (name) {
2130 err = android_enable_function(dev, conf, name);
2131 if (err)
2132 pr_err("android_usb: Cannot enable %s",
2133 name);
2134 }
2135 }
2136 }
2137
2138 /* Free uneeded configurations if exists */
2139 while (curr_conf->next != &dev->configs) {
2140 conf = list_entry(curr_conf->next,
2141 struct android_configuration, list_item);
2142 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002143 }
2144
2145 mutex_unlock(&dev->mutex);
2146
2147 return size;
2148}
2149
2150static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2151 char *buf)
2152{
2153 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002154
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302155 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002156}
2157
2158static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2159 const char *buff, size_t size)
2160{
2161 struct android_dev *dev = dev_get_drvdata(pdev);
2162 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002163 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002164 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002165 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302166 bool audio_enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302167 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002168
Stephen Boyd42517402013-01-14 16:41:42 -08002169
Benoit Gobycf3fc062011-12-19 14:39:37 -08002170 if (!cdev)
2171 return -ENODEV;
2172
Benoit Goby1e8ce152011-12-12 13:01:23 -08002173 mutex_lock(&dev->mutex);
2174
2175 sscanf(buff, "%d", &enabled);
2176 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002177 /*
2178 * Update values in composite driver's copy of
2179 * device descriptor.
2180 */
2181 cdev->desc.idVendor = device_desc.idVendor;
2182 cdev->desc.idProduct = device_desc.idProduct;
2183 cdev->desc.bcdDevice = device_desc.bcdDevice;
2184 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2185 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2186 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302187
2188 /* Audio dock accessory is unable to enumerate device if
2189 * pull-up is enabled immediately. The enumeration is
2190 * reliable with 100 msec delay.
2191 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002192 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002193 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002194 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002195 if (f_holder->f->enable)
2196 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302197 if (!strncmp(f_holder->f->name,
2198 "audio_source", 12))
2199 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002200 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302201 if (audio_enabled)
2202 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002203 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002204 dev->enabled = true;
2205 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002206 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002207 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002208 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002209 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002210 if (f_holder->f->disable)
2211 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002212 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002213 dev->enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302214 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002215 pr_err("android_usb: already %s\n",
2216 dev->enabled ? "enabled" : "disabled");
2217 }
2218
2219 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002220
Benoit Gobyaab96812011-04-19 20:37:33 -07002221 return size;
2222}
2223
Ofir Cohen94213a72012-05-03 14:26:32 +03002224static ssize_t pm_qos_show(struct device *pdev,
2225 struct device_attribute *attr, char *buf)
2226{
2227 struct android_dev *dev = dev_get_drvdata(pdev);
2228
2229 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2230}
2231
2232static ssize_t pm_qos_store(struct device *pdev,
2233 struct device_attribute *attr,
2234 const char *buff, size_t size)
2235{
2236 struct android_dev *dev = dev_get_drvdata(pdev);
2237
2238 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2239
Benoit Goby1e8ce152011-12-12 13:01:23 -08002240 return size;
2241}
2242
2243static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2244 char *buf)
2245{
2246 struct android_dev *dev = dev_get_drvdata(pdev);
2247 struct usb_composite_dev *cdev = dev->cdev;
2248 char *state = "DISCONNECTED";
2249 unsigned long flags;
2250
2251 if (!cdev)
2252 goto out;
2253
2254 spin_lock_irqsave(&cdev->lock, flags);
2255 if (cdev->config)
2256 state = "CONFIGURED";
2257 else if (dev->connected)
2258 state = "CONNECTED";
2259 spin_unlock_irqrestore(&cdev->lock, flags);
2260out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302261 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002262}
2263
2264#define DESCRIPTOR_ATTR(field, format_string) \
2265static ssize_t \
2266field ## _show(struct device *dev, struct device_attribute *attr, \
2267 char *buf) \
2268{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302269 return snprintf(buf, PAGE_SIZE, \
2270 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002271} \
2272static ssize_t \
2273field ## _store(struct device *dev, struct device_attribute *attr, \
2274 const char *buf, size_t size) \
2275{ \
2276 int value; \
2277 if (sscanf(buf, format_string, &value) == 1) { \
2278 device_desc.field = value; \
2279 return size; \
2280 } \
2281 return -1; \
2282} \
2283static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2284
2285#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2286static ssize_t \
2287field ## _show(struct device *dev, struct device_attribute *attr, \
2288 char *buf) \
2289{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302290 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002291} \
2292static ssize_t \
2293field ## _store(struct device *dev, struct device_attribute *attr, \
2294 const char *buf, size_t size) \
2295{ \
2296 if (size >= sizeof(buffer)) \
2297 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302298 strlcpy(buffer, buf, sizeof(buffer)); \
2299 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302300 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002301} \
2302static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2303
2304
2305DESCRIPTOR_ATTR(idVendor, "%04x\n")
2306DESCRIPTOR_ATTR(idProduct, "%04x\n")
2307DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2308DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2309DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2310DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2311DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2312DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2313DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2314
2315static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2316 functions_store);
2317static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002318static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2319 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002320static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302321static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2322 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002323
2324static struct device_attribute *android_usb_attributes[] = {
2325 &dev_attr_idVendor,
2326 &dev_attr_idProduct,
2327 &dev_attr_bcdDevice,
2328 &dev_attr_bDeviceClass,
2329 &dev_attr_bDeviceSubClass,
2330 &dev_attr_bDeviceProtocol,
2331 &dev_attr_iManufacturer,
2332 &dev_attr_iProduct,
2333 &dev_attr_iSerial,
2334 &dev_attr_functions,
2335 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002336 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002337 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302338 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002339 NULL
2340};
2341
2342/*-------------------------------------------------------------------------*/
2343/* Composite driver */
2344
2345static int android_bind_config(struct usb_configuration *c)
2346{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002347 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002348 int ret = 0;
2349
2350 ret = android_bind_enabled_functions(dev, c);
2351 if (ret)
2352 return ret;
2353
2354 return 0;
2355}
2356
2357static void android_unbind_config(struct usb_configuration *c)
2358{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002359 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002360
2361 android_unbind_enabled_functions(dev, c);
2362}
2363
2364static int android_bind(struct usb_composite_dev *cdev)
2365{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002366 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002367 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002368 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002369 int gcnum, id, ret;
2370
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002371 /* Bind to the last android_dev that was probed */
2372 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2373
2374 dev->cdev = cdev;
2375
Benoit Goby1e8ce152011-12-12 13:01:23 -08002376 /*
2377 * Start disconnected. Userspace will connect the gadget once
2378 * it is done configuring the functions.
2379 */
2380 usb_gadget_disconnect(gadget);
2381
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002382 /* Init the supported functions only once, on the first android_dev */
2383 if (android_dev_count == 1) {
2384 ret = android_init_functions(dev->functions, cdev);
2385 if (ret)
2386 return ret;
2387 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002388
2389 /* Allocate string descriptor numbers ... note that string
2390 * contents can be overridden by the composite_dev glue.
2391 */
2392 id = usb_string_id(cdev);
2393 if (id < 0)
2394 return id;
2395 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2396 device_desc.iManufacturer = id;
2397
2398 id = usb_string_id(cdev);
2399 if (id < 0)
2400 return id;
2401 strings_dev[STRING_PRODUCT_IDX].id = id;
2402 device_desc.iProduct = id;
2403
2404 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302405 strlcpy(manufacturer_string, "Android",
2406 sizeof(manufacturer_string) - 1);
2407 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2408 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002409
2410 id = usb_string_id(cdev);
2411 if (id < 0)
2412 return id;
2413 strings_dev[STRING_SERIAL_IDX].id = id;
2414 device_desc.iSerialNumber = id;
2415
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302416 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002417 list_for_each_entry(conf, &dev->configs, list_item)
2418 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302419
Benoit Goby1e8ce152011-12-12 13:01:23 -08002420 gcnum = usb_gadget_controller_number(gadget);
2421 if (gcnum >= 0)
2422 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2423 else {
2424 pr_warning("%s: controller '%s' not recognized\n",
2425 longname, gadget->name);
2426 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2427 }
2428
Benoit Goby1e8ce152011-12-12 13:01:23 -08002429 return 0;
2430}
2431
2432static int android_usb_unbind(struct usb_composite_dev *cdev)
2433{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002434 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002435
Lena Salmand092f2d2012-03-12 17:27:24 +02002436 manufacturer_string[0] = '\0';
2437 product_string[0] = '\0';
2438 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002439 cancel_work_sync(&dev->work);
2440 android_cleanup_functions(dev->functions);
2441 return 0;
2442}
2443
2444static struct usb_composite_driver android_usb_driver = {
2445 .name = "android_usb",
2446 .dev = &device_desc,
2447 .strings = dev_strings,
2448 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002449 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002450};
2451
2452static int
2453android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2454{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002455 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002456 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002457 struct usb_request *req = cdev->req;
2458 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002459 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002460 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002461 int value = -EOPNOTSUPP;
2462 unsigned long flags;
2463
2464 req->zero = 0;
2465 req->complete = composite_setup_complete;
2466 req->length = 0;
2467 gadget->ep0->driver_data = cdev;
2468
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002469 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002470 list_for_each_entry(f_holder,
2471 &conf->enabled_functions,
2472 enabled_list) {
2473 f = f_holder->f;
2474 if (f->ctrlrequest) {
2475 value = f->ctrlrequest(f, cdev, c);
2476 if (value >= 0)
2477 break;
2478 }
2479 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002480
Benoit Gobycf3fc062011-12-19 14:39:37 -08002481 /* Special case the accessory function.
2482 * It needs to handle control requests before it is enabled.
2483 */
2484 if (value < 0)
2485 value = acc_ctrlrequest(cdev, c);
2486
Benoit Goby1e8ce152011-12-12 13:01:23 -08002487 if (value < 0)
2488 value = composite_setup(gadget, c);
2489
2490 spin_lock_irqsave(&cdev->lock, flags);
2491 if (!dev->connected) {
2492 dev->connected = 1;
2493 schedule_work(&dev->work);
2494 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2495 cdev->config) {
2496 schedule_work(&dev->work);
2497 }
2498 spin_unlock_irqrestore(&cdev->lock, flags);
2499
2500 return value;
2501}
2502
2503static void android_disconnect(struct usb_gadget *gadget)
2504{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002505 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002506 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002507 unsigned long flags;
2508
2509 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302510 /* accessory HID support can be active while the
2511 accessory function is not actually enabled,
2512 so we need to inform it when we are disconnected.
2513 */
2514 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002515
2516 spin_lock_irqsave(&cdev->lock, flags);
2517 dev->connected = 0;
2518 schedule_work(&dev->work);
2519 spin_unlock_irqrestore(&cdev->lock, flags);
2520}
2521
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002522static void android_suspend(struct usb_gadget *gadget)
2523{
2524 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2525 struct android_dev *dev = cdev_to_android_dev(cdev);
2526 unsigned long flags;
2527
2528 spin_lock_irqsave(&cdev->lock, flags);
2529 dev->suspended = 1;
2530 schedule_work(&dev->work);
2531 spin_unlock_irqrestore(&cdev->lock, flags);
2532
2533 composite_suspend(gadget);
2534}
2535
2536static void android_resume(struct usb_gadget *gadget)
2537{
2538 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2539 struct android_dev *dev = cdev_to_android_dev(cdev);
2540 unsigned long flags;
2541
2542 spin_lock_irqsave(&cdev->lock, flags);
2543 dev->suspended = 0;
2544 schedule_work(&dev->work);
2545 spin_unlock_irqrestore(&cdev->lock, flags);
2546
2547 composite_resume(gadget);
2548}
2549
2550
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002551static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002552{
2553 struct device_attribute **attrs = android_usb_attributes;
2554 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002555 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002556 int err;
2557
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002558 /*
2559 * The primary usb core should always have usb_core_id=0, since
2560 * Android user space is currently interested in android0 events.
2561 */
2562 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2563 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002564 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002565 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002566 if (IS_ERR(dev->dev))
2567 return PTR_ERR(dev->dev);
2568
2569 dev_set_drvdata(dev->dev, dev);
2570
2571 while ((attr = *attrs++)) {
2572 err = device_create_file(dev->dev, attr);
2573 if (err) {
2574 device_destroy(android_class, dev->dev->devt);
2575 return err;
2576 }
2577 }
2578 return 0;
2579}
2580
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302581static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002582{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302583 struct device_attribute **attrs = android_usb_attributes;
2584 struct device_attribute *attr;
2585
2586 while ((attr = *attrs++))
2587 device_remove_file(dev->dev, attr);
2588 device_destroy(android_class, dev->dev->devt);
2589}
2590
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002591static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2592{
2593 struct android_dev *dev = NULL;
2594
2595 /* Find the android dev from the list */
2596 list_for_each_entry(dev, &android_dev_list, list_item) {
2597 if (dev->cdev == cdev)
2598 break;
2599 }
2600
2601 return dev;
2602}
2603
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002604static struct android_configuration *alloc_android_config
2605 (struct android_dev *dev)
2606{
2607 struct android_configuration *conf;
2608
2609 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2610 if (!conf) {
2611 pr_err("%s(): Failed to alloc memory for android conf\n",
2612 __func__);
2613 return ERR_PTR(-ENOMEM);
2614 }
2615
2616 dev->configs_num++;
2617 conf->usb_config.label = dev->name;
2618 conf->usb_config.unbind = android_unbind_config;
2619 conf->usb_config.bConfigurationValue = dev->configs_num;
2620
2621 INIT_LIST_HEAD(&conf->enabled_functions);
2622
2623 list_add_tail(&conf->list_item, &dev->configs);
2624
2625 return conf;
2626}
2627
2628static void free_android_config(struct android_dev *dev,
2629 struct android_configuration *conf)
2630{
2631 list_del(&conf->list_item);
2632 dev->configs_num--;
2633 kfree(conf);
2634}
2635
Manu Gautam43c61a12012-08-22 17:09:37 -07002636static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2637{
2638 struct dload_struct local_diag_dload = { 0 };
2639 int *src, *dst, i;
2640
2641 if (!diag_dload) {
2642 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2643 return -ENODEV;
2644 }
2645
2646 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2647 __func__, diag_dload, pid, snum);
2648
2649 /* update pid */
2650 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2651 local_diag_dload.pid = pid;
2652
2653 /* update serial number */
2654 if (!snum) {
2655 local_diag_dload.magic_struct.serial_num = 0;
2656 memset(&local_diag_dload.serial_number, 0,
2657 SERIAL_NUMBER_LENGTH);
2658 } else {
2659 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2660 strlcpy((char *)&local_diag_dload.serial_number, snum,
2661 SERIAL_NUMBER_LENGTH);
2662 }
2663
2664 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2665 src = (int *)&local_diag_dload;
2666 dst = (int *)diag_dload;
2667
2668 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2669 *dst++ = *src++;
2670
2671 return 0;
2672}
2673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002674static int __devinit android_probe(struct platform_device *pdev)
2675{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302676 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002677 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002678 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002679 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302681 if (pdev->dev.of_node) {
2682 dev_dbg(&pdev->dev, "device tree enabled\n");
2683 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2684 if (!pdata) {
2685 pr_err("unable to allocate platform data\n");
2686 return -ENOMEM;
2687 }
2688
2689 of_property_read_u32(pdev->dev.of_node,
2690 "qcom,android-usb-swfi-latency",
2691 &pdata->swfi_latency);
2692 } else {
2693 pdata = pdev->dev.platform_data;
2694 }
2695
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002696 if (!android_class) {
2697 android_class = class_create(THIS_MODULE, "android_usb");
2698 if (IS_ERR(android_class))
2699 return PTR_ERR(android_class);
2700 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002701
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002702 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2703 if (!android_dev) {
2704 pr_err("%s(): Failed to alloc memory for android_dev\n",
2705 __func__);
2706 ret = -ENOMEM;
2707 goto err_alloc;
2708 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002709
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002710 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002711 android_dev->disable_depth = 1;
2712 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002713 android_dev->configs_num = 0;
2714 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002715 INIT_WORK(&android_dev->work, android_work);
2716 mutex_init(&android_dev->mutex);
2717
2718 android_dev->pdata = pdata;
2719
2720 list_add_tail(&android_dev->list_item, &android_dev_list);
2721 android_dev_count++;
2722
2723 if (pdata)
2724 composite_driver.usb_core_id = pdata->usb_core_id;
2725 else
2726 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2727
Manu Gautam43c61a12012-08-22 17:09:37 -07002728 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2729 if (res) {
2730 diag_dload = devm_ioremap(&pdev->dev, res->start,
2731 resource_size(res));
2732 if (!diag_dload) {
2733 dev_err(&pdev->dev, "ioremap failed\n");
2734 ret = -ENOMEM;
2735 goto err_dev;
2736 }
2737 } else {
2738 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2739 }
2740
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002741 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302742 if (ret) {
2743 pr_err("%s(): android_create_device failed\n", __func__);
2744 goto err_dev;
2745 }
2746
Lena Salmand092f2d2012-03-12 17:27:24 +02002747 ret = usb_composite_probe(&android_usb_driver, android_bind);
2748 if (ret) {
2749 pr_err("%s(): Failed to register android "
2750 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302751 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002752 }
2753
Ofir Cohen94213a72012-05-03 14:26:32 +03002754 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302755 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002756 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002757 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002758 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002759
Lena Salmand092f2d2012-03-12 17:27:24 +02002760 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302761err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002762 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302763err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002764 list_del(&android_dev->list_item);
2765 android_dev_count--;
2766 kfree(android_dev);
2767err_alloc:
2768 if (list_empty(&android_dev_list)) {
2769 class_destroy(android_class);
2770 android_class = NULL;
2771 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302772 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002773}
2774
2775static int android_remove(struct platform_device *pdev)
2776{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002777 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002778 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002779 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302780
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002781 if (pdata)
2782 usb_core_id = pdata->usb_core_id;
2783
2784 /* Find the android dev from the list */
2785 list_for_each_entry(dev, &android_dev_list, list_item) {
2786 if (!dev->pdata)
2787 break; /*To backward compatibility*/
2788 if (dev->pdata->usb_core_id == usb_core_id)
2789 break;
2790 }
2791
2792 if (dev) {
2793 android_destroy_device(dev);
2794 if (pdata && pdata->swfi_latency)
2795 pm_qos_remove_request(&dev->pm_qos_req_dma);
2796 list_del(&dev->list_item);
2797 android_dev_count--;
2798 kfree(dev);
2799 }
2800
2801 if (list_empty(&android_dev_list)) {
2802 class_destroy(android_class);
2803 android_class = NULL;
2804 usb_composite_unregister(&android_usb_driver);
2805 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002807 return 0;
2808}
2809
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002810static const struct platform_device_id android_id_table[] __devinitconst = {
2811 {
2812 .name = "android_usb",
2813 },
2814 {
2815 .name = "android_usb_hsic",
2816 },
2817};
2818
Manu Gautam43c61a12012-08-22 17:09:37 -07002819static struct of_device_id usb_android_dt_match[] = {
2820 { .compatible = "qcom,android-usb",
2821 },
2822 {}
2823};
2824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002825static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07002826 .driver = {
2827 .name = "android_usb",
2828 .of_match_table = usb_android_dt_match,
2829 },
Lena Salmand092f2d2012-03-12 17:27:24 +02002830 .probe = android_probe,
2831 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002832 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002833};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002834
2835static int __init init(void)
2836{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302837 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002838
Benoit Goby1e8ce152011-12-12 13:01:23 -08002839 /* Override composite driver functions */
2840 composite_driver.setup = android_setup;
2841 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002842 composite_driver.suspend = android_suspend;
2843 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002844
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002845 INIT_LIST_HEAD(&android_dev_list);
2846 android_dev_count = 0;
2847
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302848 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302849 if (ret) {
2850 pr_err("%s(): Failed to register android"
2851 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302852 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002853
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302854 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002855}
2856module_init(init);
2857
2858static void __exit cleanup(void)
2859{
Lena Salmand092f2d2012-03-12 17:27:24 +02002860 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002861}
2862module_exit(cleanup);