blob: bc1fa077873cdbf435b97698f6318ea4c6d564b8 [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;
Amit Blay637e5b22013-05-07 17:19:09 +0300429 struct android_dev *dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700430};
431
Benoit Goby2b6862d2011-12-19 14:38:41 -0800432static int
433adb_function_init(struct android_usb_function *f,
434 struct usb_composite_dev *cdev)
435{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700436 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
437 if (!f->config)
438 return -ENOMEM;
439
Benoit Goby2b6862d2011-12-19 14:38:41 -0800440 return adb_setup();
441}
442
443static void adb_function_cleanup(struct android_usb_function *f)
444{
445 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700446 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800447}
448
449static int
450adb_function_bind_config(struct android_usb_function *f,
451 struct usb_configuration *c)
452{
453 return adb_bind_config(c);
454}
455
Benoit Goby80ba14d2012-03-19 18:56:52 -0700456static void adb_android_function_enable(struct android_usb_function *f)
457{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300458 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700459 struct adb_data *data = f->config;
460
461 data->enabled = true;
462
Amit Blay637e5b22013-05-07 17:19:09 +0300463
Benoit Goby80ba14d2012-03-19 18:56:52 -0700464 /* Disable the gadget until adbd is ready */
465 if (!data->opened)
466 android_disable(dev);
467}
468
469static void adb_android_function_disable(struct android_usb_function *f)
470{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300471 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700472 struct adb_data *data = f->config;
473
474 data->enabled = false;
475
476 /* Balance the disable that was called in closed_callback */
477 if (!data->opened)
478 android_enable(dev);
479}
480
Benoit Goby2b6862d2011-12-19 14:38:41 -0800481static struct android_usb_function adb_function = {
482 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700483 .enable = adb_android_function_enable,
484 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800485 .init = adb_function_init,
486 .cleanup = adb_function_cleanup,
487 .bind_config = adb_function_bind_config,
488};
489
Benoit Goby80ba14d2012-03-19 18:56:52 -0700490static void adb_ready_callback(void)
491{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300492 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700493 struct adb_data *data = adb_function.config;
494
Amit Blay637e5b22013-05-07 17:19:09 +0300495 /* dev is null in case ADB is not in the composition */
496 if (dev)
497 mutex_lock(&dev->mutex);
498
499 /* Save dev in case the adb function will get disabled */
500 data->dev = dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700501 data->opened = true;
502
Amit Blay637e5b22013-05-07 17:19:09 +0300503 if (data->enabled && dev)
Benoit Goby80ba14d2012-03-19 18:56:52 -0700504 android_enable(dev);
Amit Blay637e5b22013-05-07 17:19:09 +0300505
506 if (dev)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300507 mutex_unlock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700508}
509
510static void adb_closed_callback(void)
511{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700512 struct adb_data *data = adb_function.config;
Amit Blay637e5b22013-05-07 17:19:09 +0300513 struct android_dev *dev = adb_function.android_dev;
514
515 /* In case new composition is without ADB, use saved one */
516 if (!dev)
517 dev = data->dev;
518
519 if (!dev)
520 pr_err("adb_closed_callback: data->dev is NULL");
521
522 if (dev)
523 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700524
Benoit Goby80ba14d2012-03-19 18:56:52 -0700525 data->opened = false;
526
Amit Blay637e5b22013-05-07 17:19:09 +0300527 if (data->enabled)
Benoit Goby80ba14d2012-03-19 18:56:52 -0700528 android_disable(dev);
Amit Blay637e5b22013-05-07 17:19:09 +0300529
530 data->dev = NULL;
531
532 if (dev)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300533 mutex_unlock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700534}
535
Benoit Goby2b6862d2011-12-19 14:38:41 -0800536
Benoit Gobyaab96812011-04-19 20:37:33 -0700537/*-------------------------------------------------------------------------*/
538/* Supported functions initialization */
539
Stephen Boyd83ed8e22013-03-08 17:04:08 -0800540/* ACM */
541static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
542static ssize_t acm_transports_store(
543 struct device *device, struct device_attribute *attr,
544 const char *buff, size_t size)
545{
546 strlcpy(acm_transports, buff, sizeof(acm_transports));
547
548 return size;
549}
550
551static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
552static struct device_attribute *acm_function_attributes[] = {
553 &dev_attr_acm_transports,
554 NULL
555};
556
557static void acm_function_cleanup(struct android_usb_function *f)
558{
559 gserial_cleanup();
560}
561
562static int
563acm_function_bind_config(struct android_usb_function *f,
564 struct usb_configuration *c)
565{
566 char *name;
567 char buf[32], *b;
568 int err = -1, i;
569 static int acm_initialized, ports;
570
571 if (acm_initialized)
572 goto bind_config;
573
574 acm_initialized = 1;
575 strlcpy(buf, acm_transports, sizeof(buf));
576 b = strim(buf);
577
578 while (b) {
579 name = strsep(&b, ",");
580
581 if (name) {
582 err = acm_init_port(ports, name);
583 if (err) {
584 pr_err("acm: Cannot open port '%s'", name);
585 goto out;
586 }
587 ports++;
588 }
589 }
590 err = acm_port_setup(c);
591 if (err) {
592 pr_err("acm: Cannot setup transports");
593 goto out;
594 }
595
596bind_config:
597 for (i = 0; i < ports; i++) {
598 err = acm_bind_config(c, i);
599 if (err) {
600 pr_err("acm: bind_config failed for port %d", i);
601 goto out;
602 }
603 }
604
605out:
606 return err;
607}
608
609static struct android_usb_function acm_function = {
610 .name = "acm",
611 .cleanup = acm_function_cleanup,
612 .bind_config = acm_function_bind_config,
613 .attributes = acm_function_attributes,
614};
615
Manu Gautam8e0719b2011-09-26 14:47:55 +0530616/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530617static int rmnet_smd_function_bind_config(struct android_usb_function *f,
618 struct usb_configuration *c)
619{
620 return rmnet_smd_bind_config(c);
621}
622
623static struct android_usb_function rmnet_smd_function = {
624 .name = "rmnet_smd",
625 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800626};
627
Manu Gautam8e0719b2011-09-26 14:47:55 +0530628/* RMNET_SDIO */
629static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
630 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800631{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530632 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800633}
634
Manu Gautam8e0719b2011-09-26 14:47:55 +0530635static struct android_usb_function rmnet_sdio_function = {
636 .name = "rmnet_sdio",
637 .bind_config = rmnet_sdio_function_bind_config,
638};
639
640/* RMNET_SMD_SDIO */
641static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
642 struct usb_composite_dev *cdev)
643{
644 return rmnet_smd_sdio_init();
645}
646
647static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
648{
649 rmnet_smd_sdio_cleanup();
650}
651
652static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
653 struct usb_configuration *c)
654{
655 return rmnet_smd_sdio_function_add(c);
656}
657
658static struct device_attribute *rmnet_smd_sdio_attributes[] = {
659 &dev_attr_transport, NULL };
660
661static struct android_usb_function rmnet_smd_sdio_function = {
662 .name = "rmnet_smd_sdio",
663 .init = rmnet_smd_sdio_function_init,
664 .cleanup = rmnet_smd_sdio_function_cleanup,
665 .bind_config = rmnet_smd_sdio_bind_config,
666 .attributes = rmnet_smd_sdio_attributes,
667};
668
Hemant Kumar1b820d52011-11-03 15:08:28 -0700669/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
670#define MAX_XPORT_STR_LEN 50
671static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530672
Manu Gautame3e897c2011-09-12 17:18:46 +0530673static void rmnet_function_cleanup(struct android_usb_function *f)
674{
675 frmnet_cleanup();
676}
677
Manu Gautam2b0234a2011-09-07 16:47:52 +0530678static int rmnet_function_bind_config(struct android_usb_function *f,
679 struct usb_configuration *c)
680{
681 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700682 int err = 0;
683 char *ctrl_name;
684 char *data_name;
685 char buf[MAX_XPORT_STR_LEN], *b;
686 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530687
Hemant Kumar1b820d52011-11-03 15:08:28 -0700688 if (!rmnet_initialized) {
689 rmnet_initialized = 1;
690 strlcpy(buf, rmnet_transports, sizeof(buf));
691 b = strim(buf);
692 while (b) {
693 ctrl_name = strsep(&b, ",");
694 data_name = strsep(&b, ",");
695 if (ctrl_name && data_name) {
696 err = frmnet_init_port(ctrl_name, data_name);
697 if (err) {
698 pr_err("rmnet: Cannot open ctrl port:"
699 "'%s' data port:'%s'\n",
700 ctrl_name, data_name);
701 goto out;
702 }
703 ports++;
704 }
705 }
706
707 err = rmnet_gport_setup();
708 if (err) {
709 pr_err("rmnet: Cannot setup transports");
710 goto out;
711 }
712 }
713
714 for (i = 0; i < ports; i++) {
715 err = frmnet_bind_config(c, i);
716 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530717 pr_err("Could not bind rmnet%u config\n", i);
718 break;
719 }
720 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700721out:
722 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530723}
724
Hemant Kumar1b820d52011-11-03 15:08:28 -0700725static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530726 struct device_attribute *attr, char *buf)
727{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700728 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530729}
730
Hemant Kumar1b820d52011-11-03 15:08:28 -0700731static ssize_t rmnet_transports_store(
732 struct device *device, struct device_attribute *attr,
733 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530734{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700735 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530736
Manu Gautam2b0234a2011-09-07 16:47:52 +0530737 return size;
738}
739
Hemant Kumar1b820d52011-11-03 15:08:28 -0700740static struct device_attribute dev_attr_rmnet_transports =
741 __ATTR(transports, S_IRUGO | S_IWUSR,
742 rmnet_transports_show,
743 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530744static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700745 &dev_attr_rmnet_transports,
746 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530747
748static struct android_usb_function rmnet_function = {
749 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530750 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530751 .bind_config = rmnet_function_bind_config,
752 .attributes = rmnet_function_attributes,
753};
754
Bar Weinered1059e2013-04-16 11:23:55 +0300755static void gps_function_cleanup(struct android_usb_function *f)
756{
757 gps_cleanup();
758}
759
760static int gps_function_bind_config(struct android_usb_function *f,
761 struct usb_configuration *c)
762{
763 int err;
764 static int gps_initialized;
765
766 if (!gps_initialized) {
767 gps_initialized = 1;
768 err = gps_init_port();
769 if (err) {
770 pr_err("gps: Cannot init gps port");
771 return err;
772 }
773 }
774
775 err = gps_gport_setup();
776 if (err) {
777 pr_err("gps: Cannot setup transports");
778 return err;
779 }
780 err = gps_bind_config(c);
781 if (err) {
782 pr_err("Could not bind gps config\n");
783 return err;
784 }
785
786 return 0;
787}
788
789static struct android_usb_function gps_function = {
790 .name = "gps",
791 .cleanup = gps_function_cleanup,
792 .bind_config = gps_function_bind_config,
793};
794
795
Amit Blayf9b352b2013-03-04 15:01:40 +0200796/* ecm transport string */
797static char ecm_transports[MAX_XPORT_STR_LEN];
798
Ofir Cohen7b155422012-07-31 13:02:49 +0300799struct ecm_function_config {
800 u8 ethaddr[ETH_ALEN];
801};
802
803static int ecm_function_init(struct android_usb_function *f,
804 struct usb_composite_dev *cdev)
805{
806 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
807 if (!f->config)
808 return -ENOMEM;
809 return 0;
810}
811
812static void ecm_function_cleanup(struct android_usb_function *f)
813{
814 kfree(f->config);
815 f->config = NULL;
816}
817
818static int ecm_qc_function_bind_config(struct android_usb_function *f,
819 struct usb_configuration *c)
820{
821 int ret;
Amit Blayf9b352b2013-03-04 15:01:40 +0200822 char *trans;
Ofir Cohen7b155422012-07-31 13:02:49 +0300823 struct ecm_function_config *ecm = f->config;
824
825 if (!ecm) {
826 pr_err("%s: ecm_pdata\n", __func__);
827 return -EINVAL;
828 }
829
830 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
831 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
832 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
833
Amit Blayf9b352b2013-03-04 15:01:40 +0200834 pr_debug("%s: ecm_transport is %s", __func__, ecm_transports);
835
836 trans = strim(ecm_transports);
837 if (strcmp("BAM2BAM_IPA", trans)) {
838 ret = gether_qc_setup_name(c->cdev->gadget,
839 ecm->ethaddr, "ecm");
840 if (ret) {
841 pr_err("%s: gether_setup failed\n", __func__);
842 return ret;
843 }
Ofir Cohen7b155422012-07-31 13:02:49 +0300844 }
845
Amit Blayf9b352b2013-03-04 15:01:40 +0200846 return ecm_qc_bind_config(c, ecm->ethaddr, trans);
Ofir Cohen7b155422012-07-31 13:02:49 +0300847}
848
849static void ecm_qc_function_unbind_config(struct android_usb_function *f,
850 struct usb_configuration *c)
851{
Amit Blayf9b352b2013-03-04 15:01:40 +0200852 char *trans = strim(ecm_transports);
853
854 if (strcmp("BAM2BAM_IPA", trans))
855 gether_qc_cleanup_name("ecm0");
Ofir Cohen7b155422012-07-31 13:02:49 +0300856}
857
858static ssize_t ecm_ethaddr_show(struct device *dev,
859 struct device_attribute *attr, char *buf)
860{
861 struct android_usb_function *f = dev_get_drvdata(dev);
862 struct ecm_function_config *ecm = f->config;
863 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
864 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
865 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
866}
867
868static ssize_t ecm_ethaddr_store(struct device *dev,
869 struct device_attribute *attr, const char *buf, size_t size)
870{
871 struct android_usb_function *f = dev_get_drvdata(dev);
872 struct ecm_function_config *ecm = f->config;
873
874 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
875 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
876 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
877 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
878 return size;
879 return -EINVAL;
880}
881
882static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
883 ecm_ethaddr_store);
884
Amit Blayf9b352b2013-03-04 15:01:40 +0200885static ssize_t ecm_transports_show(struct device *dev,
886 struct device_attribute *attr, char *buf)
887{
888 return snprintf(buf, PAGE_SIZE, "%s\n", ecm_transports);
889}
890
891static ssize_t ecm_transports_store(struct device *dev,
892 struct device_attribute *attr, const char *buf, size_t size)
893{
894 strlcpy(ecm_transports, buf, sizeof(ecm_transports));
895 return size;
896}
897
898static DEVICE_ATTR(ecm_transports, S_IRUGO | S_IWUSR, ecm_transports_show,
899 ecm_transports_store);
900
Ofir Cohen7b155422012-07-31 13:02:49 +0300901static struct device_attribute *ecm_function_attributes[] = {
Amit Blayf9b352b2013-03-04 15:01:40 +0200902 &dev_attr_ecm_transports,
Ofir Cohen7b155422012-07-31 13:02:49 +0300903 &dev_attr_ecm_ethaddr,
904 NULL
905};
906
907static struct android_usb_function ecm_qc_function = {
908 .name = "ecm_qc",
909 .init = ecm_function_init,
910 .cleanup = ecm_function_cleanup,
911 .bind_config = ecm_qc_function_bind_config,
912 .unbind_config = ecm_qc_function_unbind_config,
913 .attributes = ecm_function_attributes,
914};
Anna Perela8c991d2012-04-09 16:44:46 +0300915
916/* MBIM - used with BAM */
917#define MAX_MBIM_INSTANCES 1
918
919static int mbim_function_init(struct android_usb_function *f,
920 struct usb_composite_dev *cdev)
921{
922 return mbim_init(MAX_MBIM_INSTANCES);
923}
924
925static void mbim_function_cleanup(struct android_usb_function *f)
926{
927 fmbim_cleanup();
928}
929
Lena Salmandf7e7992013-03-15 09:46:27 +0200930
931/* mbim transport string */
932static char mbim_transports[MAX_XPORT_STR_LEN];
933
Anna Perela8c991d2012-04-09 16:44:46 +0300934static int mbim_function_bind_config(struct android_usb_function *f,
935 struct usb_configuration *c)
936{
Lena Salmandf7e7992013-03-15 09:46:27 +0200937 char *trans;
938
939 pr_debug("%s: mbim transport is %s", __func__, mbim_transports);
940 trans = strim(mbim_transports);
941 return mbim_bind_config(c, 0, trans);
Anna Perela8c991d2012-04-09 16:44:46 +0300942}
943
Jack Pham2df2f702012-10-11 19:08:24 -0700944static int mbim_function_ctrlrequest(struct android_usb_function *f,
945 struct usb_composite_dev *cdev,
946 const struct usb_ctrlrequest *c)
947{
948 return mbim_ctrlrequest(cdev, c);
949}
950
Lena Salmandf7e7992013-03-15 09:46:27 +0200951static ssize_t mbim_transports_show(struct device *dev,
952 struct device_attribute *attr, char *buf)
953{
954 return snprintf(buf, PAGE_SIZE, "%s\n", mbim_transports);
955}
956
957static ssize_t mbim_transports_store(struct device *dev,
958 struct device_attribute *attr, const char *buf, size_t size)
959{
960 strlcpy(mbim_transports, buf, sizeof(mbim_transports));
961 return size;
962}
963
964static DEVICE_ATTR(mbim_transports, S_IRUGO | S_IWUSR, mbim_transports_show,
965 mbim_transports_store);
966
967static struct device_attribute *mbim_function_attributes[] = {
968 &dev_attr_mbim_transports,
969 NULL
970};
971
Anna Perela8c991d2012-04-09 16:44:46 +0300972static struct android_usb_function mbim_function = {
973 .name = "usb_mbim",
974 .cleanup = mbim_function_cleanup,
975 .bind_config = mbim_function_bind_config,
976 .init = mbim_function_init,
Jack Pham2df2f702012-10-11 19:08:24 -0700977 .ctrlrequest = mbim_function_ctrlrequest,
Lena Salmandf7e7992013-03-15 09:46:27 +0200978 .attributes = mbim_function_attributes,
Anna Perela8c991d2012-04-09 16:44:46 +0300979};
980
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700981#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +0300982/* PERIPHERAL AUDIO */
983static int audio_function_bind_config(struct android_usb_function *f,
984 struct usb_configuration *c)
985{
986 return audio_bind_config(c);
987}
988
989static struct android_usb_function audio_function = {
990 .name = "audio",
991 .bind_config = audio_function_bind_config,
992};
Jack Pham2ec5fdc2012-09-26 10:13:48 -0700993#endif
Anna Perel432367a2012-09-20 10:55:32 +0300994
Anna Perela8c991d2012-04-09 16:44:46 +0300995
Manu Gautam8e0719b2011-09-26 14:47:55 +0530996/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530997static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998static ssize_t clients_store(
999 struct device *device, struct device_attribute *attr,
1000 const char *buff, size_t size)
1001{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301002 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
1004 return size;
1005}
1006
1007static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
1008static struct device_attribute *diag_function_attributes[] =
1009 { &dev_attr_clients, NULL };
1010
1011static int diag_function_init(struct android_usb_function *f,
1012 struct usb_composite_dev *cdev)
1013{
1014 return diag_setup();
1015}
1016
1017static void diag_function_cleanup(struct android_usb_function *f)
1018{
1019 diag_cleanup();
1020}
1021
1022static int diag_function_bind_config(struct android_usb_function *f,
1023 struct usb_configuration *c)
1024{
1025 char *name;
1026 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +05301027 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -08001028 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001029 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301031 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 b = strim(buf);
1033
1034 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -08001035 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +05301037 /* Allow only first diag channel to update pid and serial no */
Manu Gautam43c61a12012-08-22 17:09:37 -07001038 if (!once++) {
1039 if (dev->pdata && dev->pdata->update_pid_and_serial_num)
1040 notify = dev->pdata->update_pid_and_serial_num;
1041 else
1042 notify = usb_diag_update_pid_and_serial_num;
1043 }
Manu Gautamc5760302011-08-25 14:30:24 +05301044
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +05301046 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 if (err)
1048 pr_err("diag: Cannot open channel '%s'", name);
1049 }
1050 }
1051
1052 return err;
1053}
1054
1055static struct android_usb_function diag_function = {
1056 .name = "diag",
1057 .init = diag_function_init,
1058 .cleanup = diag_function_cleanup,
1059 .bind_config = diag_function_bind_config,
1060 .attributes = diag_function_attributes,
1061};
1062
Shimrit Malichia00d7322012-08-05 13:56:28 +03001063/* DEBUG */
1064static int qdss_function_init(struct android_usb_function *f,
1065 struct usb_composite_dev *cdev)
1066{
1067 return qdss_setup();
1068}
1069
1070static void qdss_function_cleanup(struct android_usb_function *f)
1071{
1072 qdss_cleanup();
1073}
1074
1075static int qdss_function_bind_config(struct android_usb_function *f,
1076 struct usb_configuration *c)
1077{
1078 int err = -1;
1079
1080 err = qdss_bind_config(c, "qdss");
1081 if (err)
1082 pr_err("qdss: Cannot open channel qdss");
1083
1084 return err;
1085}
1086
1087static struct android_usb_function qdss_function = {
1088 .name = "qdss",
1089 .init = qdss_function_init,
1090 .cleanup = qdss_function_cleanup,
1091 .bind_config = qdss_function_bind_config,
1092};
1093
Manu Gautam8e0719b2011-09-26 14:47:55 +05301094/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +05301095static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +05301096static ssize_t serial_transports_store(
1097 struct device *device, struct device_attribute *attr,
1098 const char *buff, size_t size)
1099{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301100 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +05301101
1102 return size;
1103}
1104
1105static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
1106static struct device_attribute *serial_function_attributes[] =
1107 { &dev_attr_transports, NULL };
1108
1109static void serial_function_cleanup(struct android_usb_function *f)
1110{
1111 gserial_cleanup();
1112}
1113
1114static int serial_function_bind_config(struct android_usb_function *f,
1115 struct usb_configuration *c)
1116{
1117 char *name;
1118 char buf[32], *b;
1119 int err = -1, i;
1120 static int serial_initialized = 0, ports = 0;
1121
1122 if (serial_initialized)
1123 goto bind_config;
1124
1125 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301126 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +05301127 b = strim(buf);
1128
1129 while (b) {
1130 name = strsep(&b, ",");
1131
1132 if (name) {
1133 err = gserial_init_port(ports, name);
1134 if (err) {
1135 pr_err("serial: Cannot open port '%s'", name);
1136 goto out;
1137 }
1138 ports++;
1139 }
1140 }
1141 err = gport_setup(c);
1142 if (err) {
1143 pr_err("serial: Cannot setup transports");
1144 goto out;
1145 }
1146
1147bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +02001148 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +05301149 err = gser_bind_config(c, i);
1150 if (err) {
1151 pr_err("serial: bind_config failed for port %d", i);
1152 goto out;
1153 }
1154 }
1155
1156out:
1157 return err;
1158}
1159
1160static struct android_usb_function serial_function = {
1161 .name = "serial",
1162 .cleanup = serial_function_cleanup,
1163 .bind_config = serial_function_bind_config,
1164 .attributes = serial_function_attributes,
1165};
1166
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301167/* CCID */
1168static int ccid_function_init(struct android_usb_function *f,
1169 struct usb_composite_dev *cdev)
1170{
1171 return ccid_setup();
1172}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001173
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301174static void ccid_function_cleanup(struct android_usb_function *f)
1175{
1176 ccid_cleanup();
1177}
1178
1179static int ccid_function_bind_config(struct android_usb_function *f,
1180 struct usb_configuration *c)
1181{
1182 return ccid_bind_config(c);
1183}
1184
1185static struct android_usb_function ccid_function = {
1186 .name = "ccid",
1187 .init = ccid_function_init,
1188 .cleanup = ccid_function_cleanup,
1189 .bind_config = ccid_function_bind_config,
1190};
1191
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001192static int
1193mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001194 struct usb_composite_dev *cdev)
1195{
1196 return mtp_setup();
1197}
1198
1199static void mtp_function_cleanup(struct android_usb_function *f)
1200{
1201 mtp_cleanup();
1202}
1203
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001204static int
1205mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001206 struct usb_configuration *c)
1207{
1208 return mtp_bind_config(c, false);
1209}
1210
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001211static int
1212ptp_function_init(struct android_usb_function *f,
1213 struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001214{
1215 /* nothing to do - initialization is handled by mtp_function_init */
1216 return 0;
1217}
1218
1219static void ptp_function_cleanup(struct android_usb_function *f)
1220{
1221 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1222}
1223
Stephen Boyd83ed8e22013-03-08 17:04:08 -08001224static int
1225ptp_function_bind_config(struct android_usb_function *f,
1226 struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001227{
1228 return mtp_bind_config(c, true);
1229}
1230
1231static int mtp_function_ctrlrequest(struct android_usb_function *f,
1232 struct usb_composite_dev *cdev,
1233 const struct usb_ctrlrequest *c)
1234{
1235 return mtp_ctrlrequest(cdev, c);
1236}
1237
1238static struct android_usb_function mtp_function = {
1239 .name = "mtp",
1240 .init = mtp_function_init,
1241 .cleanup = mtp_function_cleanup,
1242 .bind_config = mtp_function_bind_config,
1243 .ctrlrequest = mtp_function_ctrlrequest,
1244};
1245
1246/* PTP function is same as MTP with slightly different interface descriptor */
1247static struct android_usb_function ptp_function = {
1248 .name = "ptp",
1249 .init = ptp_function_init,
1250 .cleanup = ptp_function_cleanup,
1251 .bind_config = ptp_function_bind_config,
1252};
1253
1254
Benoit Goby1e8ce152011-12-12 13:01:23 -08001255struct rndis_function_config {
1256 u8 ethaddr[ETH_ALEN];
1257 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001258 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001259 char manufacturer[256];
1260 /* "Wireless" RNDIS; auto-detected by Windows */
1261 bool wceis;
1262};
1263
1264static int
1265rndis_function_init(struct android_usb_function *f,
1266 struct usb_composite_dev *cdev)
1267{
1268 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1269 if (!f->config)
1270 return -ENOMEM;
1271 return 0;
1272}
1273
1274static void rndis_function_cleanup(struct android_usb_function *f)
1275{
1276 kfree(f->config);
1277 f->config = NULL;
1278}
1279
Ofir Cohenaef90b72012-07-31 12:37:04 +02001280static int rndis_qc_function_init(struct android_usb_function *f,
1281 struct usb_composite_dev *cdev)
1282{
1283 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1284 if (!f->config)
1285 return -ENOMEM;
1286
1287 return rndis_qc_init();
1288}
1289
1290static void rndis_qc_function_cleanup(struct android_usb_function *f)
1291{
1292 rndis_qc_cleanup();
1293 kfree(f->config);
1294}
1295
Benoit Goby1e8ce152011-12-12 13:01:23 -08001296static int
1297rndis_function_bind_config(struct android_usb_function *f,
1298 struct usb_configuration *c)
1299{
1300 int ret;
1301 struct rndis_function_config *rndis = f->config;
1302
1303 if (!rndis) {
1304 pr_err("%s: rndis_pdata\n", __func__);
1305 return -1;
1306 }
1307
1308 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1309 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1310 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1311
1312 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1313 if (ret) {
1314 pr_err("%s: gether_setup failed\n", __func__);
1315 return ret;
1316 }
1317
1318 if (rndis->wceis) {
1319 /* "Wireless" RNDIS; auto-detected by Windows */
1320 rndis_iad_descriptor.bFunctionClass =
1321 USB_CLASS_WIRELESS_CONTROLLER;
1322 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1323 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1324 rndis_control_intf.bInterfaceClass =
1325 USB_CLASS_WIRELESS_CONTROLLER;
1326 rndis_control_intf.bInterfaceSubClass = 0x01;
1327 rndis_control_intf.bInterfaceProtocol = 0x03;
1328 }
1329
1330 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1331 rndis->manufacturer);
1332}
1333
Ofir Cohenaef90b72012-07-31 12:37:04 +02001334static int rndis_qc_function_bind_config(struct android_usb_function *f,
1335 struct usb_configuration *c)
1336{
1337 int ret;
1338 struct rndis_function_config *rndis = f->config;
1339
1340 if (!rndis) {
1341 pr_err("%s: rndis_pdata\n", __func__);
1342 return -EINVAL;
1343 }
1344
1345 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1346 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1347 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1348
1349 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1350 if (ret) {
1351 pr_err("%s: gether_setup failed\n", __func__);
1352 return ret;
1353 }
1354
1355 if (rndis->wceis) {
1356 /* "Wireless" RNDIS; auto-detected by Windows */
1357 rndis_qc_iad_descriptor.bFunctionClass =
1358 USB_CLASS_WIRELESS_CONTROLLER;
1359 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1360 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1361 rndis_qc_control_intf.bInterfaceClass =
1362 USB_CLASS_WIRELESS_CONTROLLER;
1363 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1364 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1365 }
1366
1367 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1368 rndis->manufacturer,
1369 rndis->max_pkt_per_xfer);
1370}
1371
Benoit Goby1e8ce152011-12-12 13:01:23 -08001372static void rndis_function_unbind_config(struct android_usb_function *f,
1373 struct usb_configuration *c)
1374{
1375 gether_cleanup();
1376}
1377
Ofir Cohenaef90b72012-07-31 12:37:04 +02001378static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1379 struct usb_configuration *c)
1380{
Amit Blayd6d690a2012-10-16 13:37:42 +02001381 gether_qc_cleanup_name("rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +02001382}
1383
Benoit Goby1e8ce152011-12-12 13:01:23 -08001384static ssize_t rndis_manufacturer_show(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387 struct android_usb_function *f = dev_get_drvdata(dev);
1388 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301390 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001391}
1392
1393static ssize_t rndis_manufacturer_store(struct device *dev,
1394 struct device_attribute *attr, const char *buf, size_t size)
1395{
1396 struct android_usb_function *f = dev_get_drvdata(dev);
1397 struct rndis_function_config *config = f->config;
1398
1399 if (size >= sizeof(config->manufacturer))
1400 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001401
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301402 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001403 return size;
1404 return -1;
1405}
1406
1407static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1408 rndis_manufacturer_store);
1409
1410static ssize_t rndis_wceis_show(struct device *dev,
1411 struct device_attribute *attr, char *buf)
1412{
1413 struct android_usb_function *f = dev_get_drvdata(dev);
1414 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001415
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301416 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001417}
1418
1419static ssize_t rndis_wceis_store(struct device *dev,
1420 struct device_attribute *attr, const char *buf, size_t size)
1421{
1422 struct android_usb_function *f = dev_get_drvdata(dev);
1423 struct rndis_function_config *config = f->config;
1424 int value;
1425
1426 if (sscanf(buf, "%d", &value) == 1) {
1427 config->wceis = value;
1428 return size;
1429 }
1430 return -EINVAL;
1431}
1432
1433static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1434 rndis_wceis_store);
1435
1436static ssize_t rndis_ethaddr_show(struct device *dev,
1437 struct device_attribute *attr, char *buf)
1438{
1439 struct android_usb_function *f = dev_get_drvdata(dev);
1440 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001441
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301442 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001443 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1444 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1445}
1446
1447static ssize_t rndis_ethaddr_store(struct device *dev,
1448 struct device_attribute *attr, const char *buf, size_t size)
1449{
1450 struct android_usb_function *f = dev_get_drvdata(dev);
1451 struct rndis_function_config *rndis = f->config;
1452
1453 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1454 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1455 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1456 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1457 return size;
1458 return -EINVAL;
1459}
1460
1461static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1462 rndis_ethaddr_store);
1463
1464static ssize_t rndis_vendorID_show(struct device *dev,
1465 struct device_attribute *attr, char *buf)
1466{
1467 struct android_usb_function *f = dev_get_drvdata(dev);
1468 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001469
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301470 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001471}
1472
1473static ssize_t rndis_vendorID_store(struct device *dev,
1474 struct device_attribute *attr, const char *buf, size_t size)
1475{
1476 struct android_usb_function *f = dev_get_drvdata(dev);
1477 struct rndis_function_config *config = f->config;
1478 int value;
1479
1480 if (sscanf(buf, "%04x", &value) == 1) {
1481 config->vendorID = value;
1482 return size;
1483 }
1484 return -EINVAL;
1485}
1486
1487static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1488 rndis_vendorID_store);
1489
Ofir Cohenaef90b72012-07-31 12:37:04 +02001490static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1491 struct device_attribute *attr, char *buf)
1492{
1493 struct android_usb_function *f = dev_get_drvdata(dev);
1494 struct rndis_function_config *config = f->config;
1495 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1496}
1497
1498static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1499 struct device_attribute *attr, const char *buf, size_t size)
1500{
1501 struct android_usb_function *f = dev_get_drvdata(dev);
1502 struct rndis_function_config *config = f->config;
1503 int value;
1504
1505 if (sscanf(buf, "%d", &value) == 1) {
1506 config->max_pkt_per_xfer = value;
1507 return size;
1508 }
1509 return -EINVAL;
1510}
1511
1512static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1513 rndis_max_pkt_per_xfer_show,
1514 rndis_max_pkt_per_xfer_store);
1515
Benoit Goby1e8ce152011-12-12 13:01:23 -08001516static struct device_attribute *rndis_function_attributes[] = {
1517 &dev_attr_manufacturer,
1518 &dev_attr_wceis,
1519 &dev_attr_ethaddr,
1520 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001521 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001522 NULL
1523};
1524
1525static struct android_usb_function rndis_function = {
1526 .name = "rndis",
1527 .init = rndis_function_init,
1528 .cleanup = rndis_function_cleanup,
1529 .bind_config = rndis_function_bind_config,
1530 .unbind_config = rndis_function_unbind_config,
1531 .attributes = rndis_function_attributes,
1532};
1533
Ofir Cohenaef90b72012-07-31 12:37:04 +02001534static struct android_usb_function rndis_qc_function = {
1535 .name = "rndis_qc",
1536 .init = rndis_qc_function_init,
1537 .cleanup = rndis_qc_function_cleanup,
1538 .bind_config = rndis_qc_function_bind_config,
1539 .unbind_config = rndis_qc_function_unbind_config,
1540 .attributes = rndis_function_attributes,
1541};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001542
Anna Perelf9d01552012-11-20 15:56:32 +02001543static int ecm_function_bind_config(struct android_usb_function *f,
1544 struct usb_configuration *c)
1545{
1546 int ret;
1547 struct ecm_function_config *ecm = f->config;
1548
1549 if (!ecm) {
1550 pr_err("%s: ecm_pdata\n", __func__);
1551 return -EINVAL;
1552 }
1553
1554 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1555 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1556 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1557
1558 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
1559 if (ret) {
1560 pr_err("%s: gether_setup failed\n", __func__);
1561 return ret;
1562 }
1563
1564 ret = ecm_bind_config(c, ecm->ethaddr);
1565 if (ret) {
1566 pr_err("%s: ecm_bind_config failed\n", __func__);
1567 gether_cleanup();
1568 }
1569 return ret;
1570}
1571
1572static void ecm_function_unbind_config(struct android_usb_function *f,
1573 struct usb_configuration *c)
1574{
1575 gether_cleanup();
1576}
1577
1578static struct android_usb_function ecm_function = {
1579 .name = "ecm",
1580 .init = ecm_function_init,
1581 .cleanup = ecm_function_cleanup,
1582 .bind_config = ecm_function_bind_config,
1583 .unbind_config = ecm_function_unbind_config,
1584 .attributes = ecm_function_attributes,
1585};
1586
Benoit Goby1e8ce152011-12-12 13:01:23 -08001587struct mass_storage_function_config {
1588 struct fsg_config fsg;
1589 struct fsg_common *common;
1590};
1591
1592static int mass_storage_function_init(struct android_usb_function *f,
1593 struct usb_composite_dev *cdev)
1594{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001595 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001596 struct mass_storage_function_config *config;
1597 struct fsg_common *common;
1598 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301599 int i;
1600 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001601
1602 config = kzalloc(sizeof(struct mass_storage_function_config),
1603 GFP_KERNEL);
1604 if (!config)
1605 return -ENOMEM;
1606
1607 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301608 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301609 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301610 config->fsg.nluns = 2;
1611 config->fsg.luns[1].cdrom = 1;
1612 config->fsg.luns[1].ro = 1;
Rajkumar Raghupathy39595002012-08-24 16:34:03 +05301613 config->fsg.luns[1].removable = 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301614 name[1] = "lun0";
1615 }
1616
Benoit Goby1e8ce152011-12-12 13:01:23 -08001617 config->fsg.luns[0].removable = 1;
1618
1619 common = fsg_common_init(NULL, cdev, &config->fsg);
1620 if (IS_ERR(common)) {
1621 kfree(config);
1622 return PTR_ERR(common);
1623 }
1624
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301625 for (i = 0; i < config->fsg.nluns; i++) {
1626 err = sysfs_create_link(&f->dev->kobj,
1627 &common->luns[i].dev.kobj,
1628 name[i]);
1629 if (err)
1630 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001631 }
1632
1633 config->common = common;
1634 f->config = config;
1635 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301636error:
1637 for (; i > 0 ; i--)
1638 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1639
1640 fsg_common_release(&common->ref);
1641 kfree(config);
1642 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001643}
1644
1645static void mass_storage_function_cleanup(struct android_usb_function *f)
1646{
1647 kfree(f->config);
1648 f->config = NULL;
1649}
1650
1651static int mass_storage_function_bind_config(struct android_usb_function *f,
1652 struct usb_configuration *c)
1653{
1654 struct mass_storage_function_config *config = f->config;
1655 return fsg_bind_config(c->cdev, c, config->common);
1656}
1657
1658static ssize_t mass_storage_inquiry_show(struct device *dev,
1659 struct device_attribute *attr, char *buf)
1660{
1661 struct android_usb_function *f = dev_get_drvdata(dev);
1662 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301663 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001664}
1665
1666static ssize_t mass_storage_inquiry_store(struct device *dev,
1667 struct device_attribute *attr, const char *buf, size_t size)
1668{
1669 struct android_usb_function *f = dev_get_drvdata(dev);
1670 struct mass_storage_function_config *config = f->config;
1671 if (size >= sizeof(config->common->inquiry_string))
1672 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301673 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001674 return -EINVAL;
1675 return size;
1676}
1677
1678static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1679 mass_storage_inquiry_show,
1680 mass_storage_inquiry_store);
1681
1682static struct device_attribute *mass_storage_function_attributes[] = {
1683 &dev_attr_inquiry_string,
1684 NULL
1685};
1686
1687static struct android_usb_function mass_storage_function = {
1688 .name = "mass_storage",
1689 .init = mass_storage_function_init,
1690 .cleanup = mass_storage_function_cleanup,
1691 .bind_config = mass_storage_function_bind_config,
1692 .attributes = mass_storage_function_attributes,
1693};
1694
1695
Benoit Gobycf3fc062011-12-19 14:39:37 -08001696static int accessory_function_init(struct android_usb_function *f,
1697 struct usb_composite_dev *cdev)
1698{
1699 return acc_setup();
1700}
1701
1702static void accessory_function_cleanup(struct android_usb_function *f)
1703{
1704 acc_cleanup();
1705}
1706
1707static int accessory_function_bind_config(struct android_usb_function *f,
1708 struct usb_configuration *c)
1709{
1710 return acc_bind_config(c);
1711}
1712
1713static int accessory_function_ctrlrequest(struct android_usb_function *f,
1714 struct usb_composite_dev *cdev,
1715 const struct usb_ctrlrequest *c)
1716{
1717 return acc_ctrlrequest(cdev, c);
1718}
1719
1720static struct android_usb_function accessory_function = {
1721 .name = "accessory",
1722 .init = accessory_function_init,
1723 .cleanup = accessory_function_cleanup,
1724 .bind_config = accessory_function_bind_config,
1725 .ctrlrequest = accessory_function_ctrlrequest,
1726};
1727
Anna Perel3ee23dd2013-02-26 16:06:40 +02001728#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301729static int audio_source_function_init(struct android_usb_function *f,
1730 struct usb_composite_dev *cdev)
1731{
1732 struct audio_source_config *config;
1733
1734 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1735 if (!config)
1736 return -ENOMEM;
1737 config->card = -1;
1738 config->device = -1;
1739 f->config = config;
1740 return 0;
1741}
1742
1743static void audio_source_function_cleanup(struct android_usb_function *f)
1744{
1745 kfree(f->config);
1746}
1747
1748static int audio_source_function_bind_config(struct android_usb_function *f,
1749 struct usb_configuration *c)
1750{
1751 struct audio_source_config *config = f->config;
1752
1753 return audio_source_bind_config(c, config);
1754}
1755
1756static void audio_source_function_unbind_config(struct android_usb_function *f,
1757 struct usb_configuration *c)
1758{
1759 struct audio_source_config *config = f->config;
1760
1761 config->card = -1;
1762 config->device = -1;
1763}
1764
1765static ssize_t audio_source_pcm_show(struct device *dev,
1766 struct device_attribute *attr, char *buf)
1767{
1768 struct android_usb_function *f = dev_get_drvdata(dev);
1769 struct audio_source_config *config = f->config;
1770
1771 /* print PCM card and device numbers */
1772 return sprintf(buf, "%d %d\n", config->card, config->device);
1773}
1774
1775static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1776
1777static struct device_attribute *audio_source_function_attributes[] = {
1778 &dev_attr_pcm,
1779 NULL
1780};
1781
1782static struct android_usb_function audio_source_function = {
1783 .name = "audio_source",
1784 .init = audio_source_function_init,
1785 .cleanup = audio_source_function_cleanup,
1786 .bind_config = audio_source_function_bind_config,
1787 .unbind_config = audio_source_function_unbind_config,
1788 .attributes = audio_source_function_attributes,
1789};
Anna Perel3ee23dd2013-02-26 16:06:40 +02001790#endif
Mike Lockwood11874822012-08-27 16:43:53 +05301791
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301792static int android_uasp_connect_cb(bool connect)
1793{
1794 /*
1795 * TODO
1796 * We may have to disable gadget till UASP configfs nodes
1797 * are configured which includes mapping LUN with the
1798 * backing file. It is a fundamental difference between
1799 * f_mass_storage and f_tcp. That means UASP can not be
1800 * in default composition.
1801 *
1802 * For now, assume that UASP configfs nodes are configured
1803 * before enabling android gadget. Or cable should be
1804 * reconnected after mapping the LUN.
1805 *
1806 * Also consider making UASP to respond to Host requests when
1807 * Lun is not mapped.
1808 */
1809 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1810
1811 return 0;
1812}
1813
1814static int uasp_function_init(struct android_usb_function *f,
1815 struct usb_composite_dev *cdev)
1816{
1817 return f_tcm_init(&android_uasp_connect_cb);
1818}
1819
1820static void uasp_function_cleanup(struct android_usb_function *f)
1821{
1822 f_tcm_exit();
1823}
1824
1825static int uasp_function_bind_config(struct android_usb_function *f,
1826 struct usb_configuration *c)
1827{
1828 return tcm_bind_config(c);
1829}
1830
1831static struct android_usb_function uasp_function = {
1832 .name = "uasp",
1833 .init = uasp_function_init,
1834 .cleanup = uasp_function_cleanup,
1835 .bind_config = uasp_function_bind_config,
1836};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001837
Benoit Goby1e8ce152011-12-12 13:01:23 -08001838static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001839 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001840 &ecm_qc_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001841#ifdef CONFIG_SND_PCM
Anna Perel432367a2012-09-20 10:55:32 +03001842 &audio_function,
Jack Pham2ec5fdc2012-09-26 10:13:48 -07001843#endif
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301844 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301845 &rmnet_sdio_function,
1846 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301847 &rmnet_function,
Bar Weinered1059e2013-04-16 11:23:55 +03001848 &gps_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001850 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301851 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001852 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301853 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001854 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001855 &mtp_function,
1856 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001857 &rndis_function,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001858 &rndis_qc_function,
Anna Perelf9d01552012-11-20 15:56:32 +02001859 &ecm_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001860 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001861 &accessory_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001862#ifdef CONFIG_SND_PCM
Mike Lockwood11874822012-08-27 16:43:53 +05301863 &audio_source_function,
Anna Perel3ee23dd2013-02-26 16:06:40 +02001864#endif
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301865 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001866 NULL
1867};
1868
Lena Salmand092f2d2012-03-12 17:27:24 +02001869static void android_cleanup_functions(struct android_usb_function **functions)
1870{
1871 struct android_usb_function *f;
1872 struct device_attribute **attrs;
1873 struct device_attribute *attr;
1874
1875 while (*functions) {
1876 f = *functions++;
1877
1878 if (f->dev) {
1879 device_destroy(android_class, f->dev->devt);
1880 kfree(f->dev_name);
1881 } else
1882 continue;
1883
1884 if (f->cleanup)
1885 f->cleanup(f);
1886
1887 attrs = f->attributes;
1888 if (attrs) {
1889 while ((attr = *attrs++))
1890 device_remove_file(f->dev, attr);
1891 }
1892 }
1893}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001894
1895static int android_init_functions(struct android_usb_function **functions,
1896 struct usb_composite_dev *cdev)
1897{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001898 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001899 struct android_usb_function *f;
1900 struct device_attribute **attrs;
1901 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301902 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001903 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001904
1905 for (; (f = *functions++); index++) {
1906 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001907 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001908 if (!f->dev_name) {
1909 err = -ENOMEM;
1910 goto err_out;
1911 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001912 f->dev = device_create(android_class, dev->dev,
1913 MKDEV(0, index), f, f->dev_name);
1914 if (IS_ERR(f->dev)) {
1915 pr_err("%s: Failed to create dev %s", __func__,
1916 f->dev_name);
1917 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001918 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001919 goto err_create;
1920 }
1921
1922 if (f->init) {
1923 err = f->init(f, cdev);
1924 if (err) {
1925 pr_err("%s: Failed to init %s", __func__,
1926 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001927 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001928 }
1929 }
1930
1931 attrs = f->attributes;
1932 if (attrs) {
1933 while ((attr = *attrs++) && !err)
1934 err = device_create_file(f->dev, attr);
1935 }
1936 if (err) {
1937 pr_err("%s: Failed to create function %s attributes",
1938 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001939 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001940 }
1941 }
1942 return 0;
1943
Lena Salmand092f2d2012-03-12 17:27:24 +02001944err_attrs:
1945 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1946 device_remove_file(f->dev, attr);
1947 if (f->cleanup)
1948 f->cleanup(f);
1949err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001950 device_destroy(android_class, f->dev->devt);
1951err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001952 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001953 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001954err_out:
1955 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001956 return err;
1957}
1958
Benoit Goby1e8ce152011-12-12 13:01:23 -08001959static int
1960android_bind_enabled_functions(struct android_dev *dev,
1961 struct usb_configuration *c)
1962{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001963 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001964 struct android_configuration *conf =
1965 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001966 int ret;
1967
Ido Shayevitz68557e32012-11-06 12:40:37 +02001968 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1969 ret = f_holder->f->bind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001970 if (ret) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02001971 pr_err("%s: %s failed", __func__, f_holder->f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001972 return ret;
1973 }
1974 }
1975 return 0;
1976}
1977
1978static void
1979android_unbind_enabled_functions(struct android_dev *dev,
1980 struct usb_configuration *c)
1981{
Ido Shayevitz68557e32012-11-06 12:40:37 +02001982 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001983 struct android_configuration *conf =
1984 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001985
Ido Shayevitz68557e32012-11-06 12:40:37 +02001986 list_for_each_entry(f_holder, &conf->enabled_functions, enabled_list) {
1987 if (f_holder->f->unbind_config)
1988 f_holder->f->unbind_config(f_holder->f, c);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001989 }
1990}
1991
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001992static int android_enable_function(struct android_dev *dev,
1993 struct android_configuration *conf,
1994 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001995{
1996 struct android_usb_function **functions = dev->functions;
1997 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02001998 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001999 while ((f = *functions++)) {
2000 if (!strcmp(name, f->name)) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002001 if (f->android_dev && f->android_dev != dev)
2002 pr_err("%s is enabled in other device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002003 f->name);
2004 else {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002005 f_holder = kzalloc(sizeof(*f_holder),
2006 GFP_KERNEL);
2007 if (!f_holder) {
2008 pr_err("Failed to alloc f_holder\n");
2009 return -ENOMEM;
2010 }
2011
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002012 f->android_dev = dev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002013 f_holder->f = f;
2014 list_add_tail(&f_holder->enabled_list,
2015 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002016 return 0;
2017 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002018 }
2019 }
2020 return -EINVAL;
2021}
2022
2023/*-------------------------------------------------------------------------*/
2024/* /sys/class/android_usb/android%d/ interface */
2025
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302026static ssize_t remote_wakeup_show(struct device *pdev,
2027 struct device_attribute *attr, char *buf)
2028{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002029 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002030 struct android_configuration *conf;
2031
2032 /*
2033 * Show the wakeup attribute of the first configuration,
2034 * since all configurations have the same wakeup attribute
2035 */
2036 if (dev->configs_num == 0)
2037 return 0;
2038 conf = list_entry(dev->configs.next,
2039 struct android_configuration,
2040 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002041
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302042 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002043 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302044 USB_CONFIG_ATT_WAKEUP));
2045}
2046
2047static ssize_t remote_wakeup_store(struct device *pdev,
2048 struct device_attribute *attr, const char *buff, size_t size)
2049{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002050 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002051 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302052 int enable = 0;
2053
2054 sscanf(buff, "%d", &enable);
2055
2056 pr_debug("android_usb: %s remote wakeup\n",
2057 enable ? "enabling" : "disabling");
2058
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002059 list_for_each_entry(conf, &dev->configs, list_item)
2060 if (enable)
2061 conf->usb_config.bmAttributes |=
2062 USB_CONFIG_ATT_WAKEUP;
2063 else
2064 conf->usb_config.bmAttributes &=
2065 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302066
2067 return size;
2068}
2069
Benoit Goby1e8ce152011-12-12 13:01:23 -08002070static ssize_t
2071functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2072{
2073 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002074 struct android_configuration *conf;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002075 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002076 char *buff = buf;
2077
2078 mutex_lock(&dev->mutex);
2079
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002080 list_for_each_entry(conf, &dev->configs, list_item) {
2081 if (buff != buf)
2082 *(buff-1) = ':';
Ido Shayevitz68557e32012-11-06 12:40:37 +02002083 list_for_each_entry(f_holder, &conf->enabled_functions,
2084 enabled_list)
2085 buff += snprintf(buff, PAGE_SIZE, "%s,",
2086 f_holder->f->name);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002087 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002088
2089 mutex_unlock(&dev->mutex);
2090
2091 if (buff != buf)
2092 *(buff-1) = '\n';
2093 return buff - buf;
2094}
2095
2096static ssize_t
2097functions_store(struct device *pdev, struct device_attribute *attr,
2098 const char *buff, size_t size)
2099{
2100 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002101 struct list_head *curr_conf = &dev->configs;
2102 struct android_configuration *conf;
2103 char *conf_str;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002104 struct android_usb_function_holder *f_holder;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002105 char *name;
2106 char buf[256], *b;
2107 int err;
2108
2109 mutex_lock(&dev->mutex);
2110
2111 if (dev->enabled) {
2112 mutex_unlock(&dev->mutex);
2113 return -EBUSY;
2114 }
2115
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002116 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002117 list_for_each_entry(conf, &dev->configs, list_item) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002118 while (conf->enabled_functions.next !=
2119 &conf->enabled_functions) {
2120 f_holder = list_entry(conf->enabled_functions.next,
2121 typeof(*f_holder),
2122 enabled_list);
2123 f_holder->f->android_dev = NULL;
2124 list_del(&f_holder->enabled_list);
2125 kfree(f_holder);
2126 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002127 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002128 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002129
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302130 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002131 b = strim(buf);
2132
2133 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002134 conf_str = strsep(&b, ":");
2135 if (conf_str) {
2136 /* If the next not equal to the head, take it */
2137 if (curr_conf->next != &dev->configs)
2138 conf = list_entry(curr_conf->next,
2139 struct android_configuration,
2140 list_item);
2141 else
2142 conf = alloc_android_config(dev);
2143
2144 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002145 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002146
2147 while (conf_str) {
2148 name = strsep(&conf_str, ",");
2149 if (name) {
2150 err = android_enable_function(dev, conf, name);
2151 if (err)
2152 pr_err("android_usb: Cannot enable %s",
2153 name);
2154 }
2155 }
2156 }
2157
2158 /* Free uneeded configurations if exists */
2159 while (curr_conf->next != &dev->configs) {
2160 conf = list_entry(curr_conf->next,
2161 struct android_configuration, list_item);
2162 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002163 }
2164
2165 mutex_unlock(&dev->mutex);
2166
2167 return size;
2168}
2169
2170static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2171 char *buf)
2172{
2173 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002174
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302175 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002176}
2177
2178static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2179 const char *buff, size_t size)
2180{
2181 struct android_dev *dev = dev_get_drvdata(pdev);
2182 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002183 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002184 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002185 int enabled = 0;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302186 bool audio_enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302187 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002188
Stephen Boyd42517402013-01-14 16:41:42 -08002189
Benoit Gobycf3fc062011-12-19 14:39:37 -08002190 if (!cdev)
2191 return -ENODEV;
2192
Benoit Goby1e8ce152011-12-12 13:01:23 -08002193 mutex_lock(&dev->mutex);
2194
2195 sscanf(buff, "%d", &enabled);
2196 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002197 /*
2198 * Update values in composite driver's copy of
2199 * device descriptor.
2200 */
2201 cdev->desc.idVendor = device_desc.idVendor;
2202 cdev->desc.idProduct = device_desc.idProduct;
2203 cdev->desc.bcdDevice = device_desc.bcdDevice;
2204 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2205 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2206 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302207
2208 /* Audio dock accessory is unable to enumerate device if
2209 * pull-up is enabled immediately. The enumeration is
2210 * reliable with 100 msec delay.
2211 */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002212 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002213 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002214 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002215 if (f_holder->f->enable)
2216 f_holder->f->enable(f_holder->f);
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302217 if (!strncmp(f_holder->f->name,
2218 "audio_source", 12))
2219 audio_enabled = true;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002220 }
Rajkumar Raghupathya79363b2013-01-02 19:08:49 +05302221 if (audio_enabled)
2222 msleep(100);
Benoit Goby80ba14d2012-03-19 18:56:52 -07002223 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002224 dev->enabled = true;
2225 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002226 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002227 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002228 list_for_each_entry(f_holder, &conf->enabled_functions,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002229 enabled_list) {
Ido Shayevitz68557e32012-11-06 12:40:37 +02002230 if (f_holder->f->disable)
2231 f_holder->f->disable(f_holder->f);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002232 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002233 dev->enabled = false;
Pavankumar Kondeti19d8bc62013-02-28 10:19:40 +05302234 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002235 pr_err("android_usb: already %s\n",
2236 dev->enabled ? "enabled" : "disabled");
2237 }
2238
2239 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002240
Benoit Gobyaab96812011-04-19 20:37:33 -07002241 return size;
2242}
2243
Ofir Cohen94213a72012-05-03 14:26:32 +03002244static ssize_t pm_qos_show(struct device *pdev,
2245 struct device_attribute *attr, char *buf)
2246{
2247 struct android_dev *dev = dev_get_drvdata(pdev);
2248
2249 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2250}
2251
2252static ssize_t pm_qos_store(struct device *pdev,
2253 struct device_attribute *attr,
2254 const char *buff, size_t size)
2255{
2256 struct android_dev *dev = dev_get_drvdata(pdev);
2257
2258 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2259
Benoit Goby1e8ce152011-12-12 13:01:23 -08002260 return size;
2261}
2262
2263static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2264 char *buf)
2265{
2266 struct android_dev *dev = dev_get_drvdata(pdev);
2267 struct usb_composite_dev *cdev = dev->cdev;
2268 char *state = "DISCONNECTED";
2269 unsigned long flags;
2270
2271 if (!cdev)
2272 goto out;
2273
2274 spin_lock_irqsave(&cdev->lock, flags);
2275 if (cdev->config)
2276 state = "CONFIGURED";
2277 else if (dev->connected)
2278 state = "CONNECTED";
2279 spin_unlock_irqrestore(&cdev->lock, flags);
2280out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302281 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002282}
2283
2284#define DESCRIPTOR_ATTR(field, format_string) \
2285static ssize_t \
2286field ## _show(struct device *dev, struct device_attribute *attr, \
2287 char *buf) \
2288{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302289 return snprintf(buf, PAGE_SIZE, \
2290 format_string, device_desc.field); \
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 int value; \
2297 if (sscanf(buf, format_string, &value) == 1) { \
2298 device_desc.field = value; \
2299 return size; \
2300 } \
2301 return -1; \
2302} \
2303static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2304
2305#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2306static ssize_t \
2307field ## _show(struct device *dev, struct device_attribute *attr, \
2308 char *buf) \
2309{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302310 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002311} \
2312static ssize_t \
2313field ## _store(struct device *dev, struct device_attribute *attr, \
2314 const char *buf, size_t size) \
2315{ \
2316 if (size >= sizeof(buffer)) \
2317 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302318 strlcpy(buffer, buf, sizeof(buffer)); \
2319 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302320 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002321} \
2322static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2323
2324
2325DESCRIPTOR_ATTR(idVendor, "%04x\n")
2326DESCRIPTOR_ATTR(idProduct, "%04x\n")
2327DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2328DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2329DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2330DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2331DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2332DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2333DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2334
2335static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2336 functions_store);
2337static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002338static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2339 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002340static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302341static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2342 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002343
2344static struct device_attribute *android_usb_attributes[] = {
2345 &dev_attr_idVendor,
2346 &dev_attr_idProduct,
2347 &dev_attr_bcdDevice,
2348 &dev_attr_bDeviceClass,
2349 &dev_attr_bDeviceSubClass,
2350 &dev_attr_bDeviceProtocol,
2351 &dev_attr_iManufacturer,
2352 &dev_attr_iProduct,
2353 &dev_attr_iSerial,
2354 &dev_attr_functions,
2355 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002356 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002357 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302358 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002359 NULL
2360};
2361
2362/*-------------------------------------------------------------------------*/
2363/* Composite driver */
2364
2365static int android_bind_config(struct usb_configuration *c)
2366{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002367 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002368 int ret = 0;
2369
2370 ret = android_bind_enabled_functions(dev, c);
2371 if (ret)
2372 return ret;
2373
2374 return 0;
2375}
2376
2377static void android_unbind_config(struct usb_configuration *c)
2378{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002379 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002380
2381 android_unbind_enabled_functions(dev, c);
2382}
2383
2384static int android_bind(struct usb_composite_dev *cdev)
2385{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002386 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002387 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002388 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002389 int gcnum, id, ret;
2390
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002391 /* Bind to the last android_dev that was probed */
2392 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2393
2394 dev->cdev = cdev;
2395
Benoit Goby1e8ce152011-12-12 13:01:23 -08002396 /*
2397 * Start disconnected. Userspace will connect the gadget once
2398 * it is done configuring the functions.
2399 */
2400 usb_gadget_disconnect(gadget);
2401
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002402 /* Init the supported functions only once, on the first android_dev */
2403 if (android_dev_count == 1) {
2404 ret = android_init_functions(dev->functions, cdev);
2405 if (ret)
2406 return ret;
2407 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002408
2409 /* Allocate string descriptor numbers ... note that string
2410 * contents can be overridden by the composite_dev glue.
2411 */
2412 id = usb_string_id(cdev);
2413 if (id < 0)
2414 return id;
2415 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2416 device_desc.iManufacturer = id;
2417
2418 id = usb_string_id(cdev);
2419 if (id < 0)
2420 return id;
2421 strings_dev[STRING_PRODUCT_IDX].id = id;
2422 device_desc.iProduct = id;
2423
2424 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302425 strlcpy(manufacturer_string, "Android",
2426 sizeof(manufacturer_string) - 1);
2427 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2428 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002429
2430 id = usb_string_id(cdev);
2431 if (id < 0)
2432 return id;
2433 strings_dev[STRING_SERIAL_IDX].id = id;
2434 device_desc.iSerialNumber = id;
2435
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302436 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002437 list_for_each_entry(conf, &dev->configs, list_item)
2438 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302439
Benoit Goby1e8ce152011-12-12 13:01:23 -08002440 gcnum = usb_gadget_controller_number(gadget);
2441 if (gcnum >= 0)
2442 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2443 else {
2444 pr_warning("%s: controller '%s' not recognized\n",
2445 longname, gadget->name);
2446 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2447 }
2448
Benoit Goby1e8ce152011-12-12 13:01:23 -08002449 return 0;
2450}
2451
2452static int android_usb_unbind(struct usb_composite_dev *cdev)
2453{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002454 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002455
Lena Salmand092f2d2012-03-12 17:27:24 +02002456 manufacturer_string[0] = '\0';
2457 product_string[0] = '\0';
2458 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002459 cancel_work_sync(&dev->work);
2460 android_cleanup_functions(dev->functions);
2461 return 0;
2462}
2463
2464static struct usb_composite_driver android_usb_driver = {
2465 .name = "android_usb",
2466 .dev = &device_desc,
2467 .strings = dev_strings,
2468 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002469 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002470};
2471
2472static int
2473android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2474{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002475 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002476 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002477 struct usb_request *req = cdev->req;
2478 struct android_usb_function *f;
Ido Shayevitz68557e32012-11-06 12:40:37 +02002479 struct android_usb_function_holder *f_holder;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002480 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002481 int value = -EOPNOTSUPP;
2482 unsigned long flags;
2483
2484 req->zero = 0;
2485 req->complete = composite_setup_complete;
2486 req->length = 0;
2487 gadget->ep0->driver_data = cdev;
2488
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002489 list_for_each_entry(conf, &dev->configs, list_item)
Ido Shayevitz68557e32012-11-06 12:40:37 +02002490 list_for_each_entry(f_holder,
2491 &conf->enabled_functions,
2492 enabled_list) {
2493 f = f_holder->f;
2494 if (f->ctrlrequest) {
2495 value = f->ctrlrequest(f, cdev, c);
2496 if (value >= 0)
2497 break;
2498 }
2499 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002500
Benoit Gobycf3fc062011-12-19 14:39:37 -08002501 /* Special case the accessory function.
2502 * It needs to handle control requests before it is enabled.
2503 */
2504 if (value < 0)
2505 value = acc_ctrlrequest(cdev, c);
2506
Benoit Goby1e8ce152011-12-12 13:01:23 -08002507 if (value < 0)
2508 value = composite_setup(gadget, c);
2509
2510 spin_lock_irqsave(&cdev->lock, flags);
2511 if (!dev->connected) {
2512 dev->connected = 1;
2513 schedule_work(&dev->work);
2514 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2515 cdev->config) {
2516 schedule_work(&dev->work);
2517 }
2518 spin_unlock_irqrestore(&cdev->lock, flags);
2519
2520 return value;
2521}
2522
2523static void android_disconnect(struct usb_gadget *gadget)
2524{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002525 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002526 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002527 unsigned long flags;
2528
2529 composite_disconnect(gadget);
Mike Lockwoode7558bb2012-08-27 16:23:48 +05302530 /* accessory HID support can be active while the
2531 accessory function is not actually enabled,
2532 so we need to inform it when we are disconnected.
2533 */
2534 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002535
2536 spin_lock_irqsave(&cdev->lock, flags);
2537 dev->connected = 0;
2538 schedule_work(&dev->work);
2539 spin_unlock_irqrestore(&cdev->lock, flags);
2540}
2541
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002542static void android_suspend(struct usb_gadget *gadget)
2543{
2544 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2545 struct android_dev *dev = cdev_to_android_dev(cdev);
2546 unsigned long flags;
2547
2548 spin_lock_irqsave(&cdev->lock, flags);
2549 dev->suspended = 1;
2550 schedule_work(&dev->work);
2551 spin_unlock_irqrestore(&cdev->lock, flags);
2552
2553 composite_suspend(gadget);
2554}
2555
2556static void android_resume(struct usb_gadget *gadget)
2557{
2558 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2559 struct android_dev *dev = cdev_to_android_dev(cdev);
2560 unsigned long flags;
2561
2562 spin_lock_irqsave(&cdev->lock, flags);
2563 dev->suspended = 0;
2564 schedule_work(&dev->work);
2565 spin_unlock_irqrestore(&cdev->lock, flags);
2566
2567 composite_resume(gadget);
2568}
2569
2570
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002571static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002572{
2573 struct device_attribute **attrs = android_usb_attributes;
2574 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002575 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002576 int err;
2577
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002578 /*
2579 * The primary usb core should always have usb_core_id=0, since
2580 * Android user space is currently interested in android0 events.
2581 */
2582 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2583 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002584 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002585 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002586 if (IS_ERR(dev->dev))
2587 return PTR_ERR(dev->dev);
2588
2589 dev_set_drvdata(dev->dev, dev);
2590
2591 while ((attr = *attrs++)) {
2592 err = device_create_file(dev->dev, attr);
2593 if (err) {
2594 device_destroy(android_class, dev->dev->devt);
2595 return err;
2596 }
2597 }
2598 return 0;
2599}
2600
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302601static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002602{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302603 struct device_attribute **attrs = android_usb_attributes;
2604 struct device_attribute *attr;
2605
2606 while ((attr = *attrs++))
2607 device_remove_file(dev->dev, attr);
2608 device_destroy(android_class, dev->dev->devt);
2609}
2610
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002611static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2612{
2613 struct android_dev *dev = NULL;
2614
2615 /* Find the android dev from the list */
2616 list_for_each_entry(dev, &android_dev_list, list_item) {
2617 if (dev->cdev == cdev)
2618 break;
2619 }
2620
2621 return dev;
2622}
2623
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002624static struct android_configuration *alloc_android_config
2625 (struct android_dev *dev)
2626{
2627 struct android_configuration *conf;
2628
2629 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2630 if (!conf) {
2631 pr_err("%s(): Failed to alloc memory for android conf\n",
2632 __func__);
2633 return ERR_PTR(-ENOMEM);
2634 }
2635
2636 dev->configs_num++;
2637 conf->usb_config.label = dev->name;
2638 conf->usb_config.unbind = android_unbind_config;
2639 conf->usb_config.bConfigurationValue = dev->configs_num;
2640
2641 INIT_LIST_HEAD(&conf->enabled_functions);
2642
2643 list_add_tail(&conf->list_item, &dev->configs);
2644
2645 return conf;
2646}
2647
2648static void free_android_config(struct android_dev *dev,
2649 struct android_configuration *conf)
2650{
2651 list_del(&conf->list_item);
2652 dev->configs_num--;
2653 kfree(conf);
2654}
2655
Manu Gautam43c61a12012-08-22 17:09:37 -07002656static int usb_diag_update_pid_and_serial_num(u32 pid, const char *snum)
2657{
2658 struct dload_struct local_diag_dload = { 0 };
2659 int *src, *dst, i;
2660
2661 if (!diag_dload) {
2662 pr_debug("%s: unable to update PID and serial_no\n", __func__);
2663 return -ENODEV;
2664 }
2665
2666 pr_debug("%s: dload:%p pid:%x serial_num:%s\n",
2667 __func__, diag_dload, pid, snum);
2668
2669 /* update pid */
2670 local_diag_dload.magic_struct.pid = PID_MAGIC_ID;
2671 local_diag_dload.pid = pid;
2672
2673 /* update serial number */
2674 if (!snum) {
2675 local_diag_dload.magic_struct.serial_num = 0;
2676 memset(&local_diag_dload.serial_number, 0,
2677 SERIAL_NUMBER_LENGTH);
2678 } else {
2679 local_diag_dload.magic_struct.serial_num = SERIAL_NUM_MAGIC_ID;
2680 strlcpy((char *)&local_diag_dload.serial_number, snum,
2681 SERIAL_NUMBER_LENGTH);
2682 }
2683
2684 /* Copy to shared struct (accesses need to be 32 bit aligned) */
2685 src = (int *)&local_diag_dload;
2686 dst = (int *)diag_dload;
2687
2688 for (i = 0; i < sizeof(*diag_dload) / 4; i++)
2689 *dst++ = *src++;
2690
2691 return 0;
2692}
2693
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002694static int __devinit android_probe(struct platform_device *pdev)
2695{
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302696 struct android_usb_platform_data *pdata;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002697 struct android_dev *android_dev;
Manu Gautam43c61a12012-08-22 17:09:37 -07002698 struct resource *res;
Lena Salmand092f2d2012-03-12 17:27:24 +02002699 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002700
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302701 if (pdev->dev.of_node) {
2702 dev_dbg(&pdev->dev, "device tree enabled\n");
2703 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2704 if (!pdata) {
2705 pr_err("unable to allocate platform data\n");
2706 return -ENOMEM;
2707 }
2708
2709 of_property_read_u32(pdev->dev.of_node,
2710 "qcom,android-usb-swfi-latency",
2711 &pdata->swfi_latency);
Chiranjeevi Velempati9d797662013-04-22 15:45:38 +05302712 pdata->cdrom = of_property_read_bool(pdev->dev.of_node,
2713 "qcom,android-usb-cdrom");
Vijayavardhan Vennapusa8ceade82012-11-01 15:11:21 +05302714 } else {
2715 pdata = pdev->dev.platform_data;
2716 }
2717
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002718 if (!android_class) {
2719 android_class = class_create(THIS_MODULE, "android_usb");
2720 if (IS_ERR(android_class))
2721 return PTR_ERR(android_class);
2722 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002723
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002724 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2725 if (!android_dev) {
2726 pr_err("%s(): Failed to alloc memory for android_dev\n",
2727 __func__);
2728 ret = -ENOMEM;
2729 goto err_alloc;
2730 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002731
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002732 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002733 android_dev->disable_depth = 1;
2734 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002735 android_dev->configs_num = 0;
2736 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002737 INIT_WORK(&android_dev->work, android_work);
2738 mutex_init(&android_dev->mutex);
2739
2740 android_dev->pdata = pdata;
2741
2742 list_add_tail(&android_dev->list_item, &android_dev_list);
2743 android_dev_count++;
2744
2745 if (pdata)
2746 composite_driver.usb_core_id = pdata->usb_core_id;
2747 else
2748 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2749
Manu Gautam43c61a12012-08-22 17:09:37 -07002750 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2751 if (res) {
2752 diag_dload = devm_ioremap(&pdev->dev, res->start,
2753 resource_size(res));
2754 if (!diag_dload) {
2755 dev_err(&pdev->dev, "ioremap failed\n");
2756 ret = -ENOMEM;
2757 goto err_dev;
2758 }
2759 } else {
2760 dev_dbg(&pdev->dev, "failed to get mem resource\n");
2761 }
2762
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002763 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302764 if (ret) {
2765 pr_err("%s(): android_create_device failed\n", __func__);
2766 goto err_dev;
2767 }
2768
Lena Salmand092f2d2012-03-12 17:27:24 +02002769 ret = usb_composite_probe(&android_usb_driver, android_bind);
2770 if (ret) {
2771 pr_err("%s(): Failed to register android "
2772 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302773 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002774 }
2775
Ofir Cohen94213a72012-05-03 14:26:32 +03002776 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302777 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002778 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002779 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002780 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002781
Lena Salmand092f2d2012-03-12 17:27:24 +02002782 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302783err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002784 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302785err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002786 list_del(&android_dev->list_item);
2787 android_dev_count--;
2788 kfree(android_dev);
2789err_alloc:
2790 if (list_empty(&android_dev_list)) {
2791 class_destroy(android_class);
2792 android_class = NULL;
2793 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302794 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002795}
2796
2797static int android_remove(struct platform_device *pdev)
2798{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002799 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002800 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002801 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302802
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002803 if (pdata)
2804 usb_core_id = pdata->usb_core_id;
2805
2806 /* Find the android dev from the list */
2807 list_for_each_entry(dev, &android_dev_list, list_item) {
2808 if (!dev->pdata)
2809 break; /*To backward compatibility*/
2810 if (dev->pdata->usb_core_id == usb_core_id)
2811 break;
2812 }
2813
2814 if (dev) {
2815 android_destroy_device(dev);
2816 if (pdata && pdata->swfi_latency)
2817 pm_qos_remove_request(&dev->pm_qos_req_dma);
2818 list_del(&dev->list_item);
2819 android_dev_count--;
2820 kfree(dev);
2821 }
2822
2823 if (list_empty(&android_dev_list)) {
2824 class_destroy(android_class);
2825 android_class = NULL;
2826 usb_composite_unregister(&android_usb_driver);
2827 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002829 return 0;
2830}
2831
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002832static const struct platform_device_id android_id_table[] __devinitconst = {
2833 {
2834 .name = "android_usb",
2835 },
2836 {
2837 .name = "android_usb_hsic",
2838 },
2839};
2840
Manu Gautam43c61a12012-08-22 17:09:37 -07002841static struct of_device_id usb_android_dt_match[] = {
2842 { .compatible = "qcom,android-usb",
2843 },
2844 {}
2845};
2846
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002847static struct platform_driver android_platform_driver = {
Manu Gautam43c61a12012-08-22 17:09:37 -07002848 .driver = {
2849 .name = "android_usb",
2850 .of_match_table = usb_android_dt_match,
2851 },
Lena Salmand092f2d2012-03-12 17:27:24 +02002852 .probe = android_probe,
2853 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002854 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002855};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002856
2857static int __init init(void)
2858{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302859 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002860
Benoit Goby1e8ce152011-12-12 13:01:23 -08002861 /* Override composite driver functions */
2862 composite_driver.setup = android_setup;
2863 composite_driver.disconnect = android_disconnect;
Ido Shayevitzfb5edfe2012-12-26 14:26:37 +02002864 composite_driver.suspend = android_suspend;
2865 composite_driver.resume = android_resume;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002866
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002867 INIT_LIST_HEAD(&android_dev_list);
2868 android_dev_count = 0;
2869
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302870 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302871 if (ret) {
2872 pr_err("%s(): Failed to register android"
2873 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302874 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002875
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302876 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002877}
2878module_init(init);
2879
2880static void __exit cleanup(void)
2881{
Lena Salmand092f2d2012-03-12 17:27:24 +02002882 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002883}
2884module_exit(cleanup);