blob: 687d2b71401c917ecac8a57680a9086996fd49b9 [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>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
Anh Nguyen64376432012-10-17 16:08:48 -070034#ifndef DEBUG
35#define DEBUG 1
36#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080037/*
38 * Kbuild is not very cooperative with respect to linking separately
39 * compiled library objects into one module. So for now we won't use
40 * separate compilation ... ensuring init/exit sections work to shrink
41 * the runtime footprint, and giving us at least some parts of what
42 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
43 */
44#include "usbstring.c"
45#include "config.c"
46#include "epautoconf.c"
47#include "composite.c"
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "f_diag.c"
Shimrit Malichia00d7322012-08-05 13:56:28 +030050#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053051#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053052#include "f_rmnet_sdio.c"
53#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053054#include "f_rmnet.c"
Ajay Dudani34b1e302012-08-27 16:43:53 +053055#include "f_audio_source.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080056#include "f_mass_storage.c"
57#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053058#include "u_sdio.c"
59#include "u_smd.c"
60#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053061#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080062#include "u_ctrl_hsic.c"
63#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053064#include "u_ctrl_hsuart.c"
65#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053066#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080067#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080068#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053069#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080070#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080071#include "f_accessory.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070072#ifdef CONFIG_USB_ANDROID_CDC_ECM
73#include "f_ecm.c"
74#else
Benoit Goby1e8ce152011-12-12 13:01:23 -080075#define USB_ETH_RNDIS y
76#include "f_rndis.c"
77#include "rndis.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070078#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080079#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030080#include "u_bam_data.c"
81#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030082#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020083#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030084#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053085#ifdef CONFIG_TARGET_CORE
86#include "f_tcm.c"
87#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080088
89MODULE_AUTHOR("Mike Lockwood");
90MODULE_DESCRIPTION("Android Composite USB Driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION("1.0");
93
94static const char longname[] = "Gadget Android";
95
96/* Default vendor and product IDs, overridden by userspace */
97#define VENDOR_ID 0x18D1
98#define PRODUCT_ID 0x0001
99
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300100#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
101
Benoit Goby1e8ce152011-12-12 13:01:23 -0800102struct android_usb_function {
103 char *name;
104 void *config;
105
106 struct device *dev;
107 char *dev_name;
108 struct device_attribute **attributes;
109
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300110 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800111 struct list_head enabled_list;
112
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300113 struct android_dev *android_dev;
114
Benoit Goby1e8ce152011-12-12 13:01:23 -0800115 /* Optional: initialization during gadget bind */
116 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
117 /* Optional: cleanup during gadget unbind */
118 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700119 /* Optional: called when the function is added the list of
120 * enabled functions */
121 void (*enable)(struct android_usb_function *);
122 /* Optional: called when it is removed */
123 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800124
125 int (*bind_config)(struct android_usb_function *,
126 struct usb_configuration *);
127
128 /* Optional: called when the configuration is removed */
129 void (*unbind_config)(struct android_usb_function *,
130 struct usb_configuration *);
131 /* Optional: handle ctrl requests before the device is configured */
132 int (*ctrlrequest)(struct android_usb_function *,
133 struct usb_composite_dev *,
134 const struct usb_ctrlrequest *);
135};
136
137struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300138 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800139 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800140 struct usb_composite_dev *cdev;
141 struct device *dev;
142
143 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700144 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800145 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700146 struct android_usb_platform_data *pdata;
147
Benoit Goby1e8ce152011-12-12 13:01:23 -0800148 bool connected;
149 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300150 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700151 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800152 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300153
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300154 /* A list of struct android_configuration */
155 struct list_head configs;
156 int configs_num;
157
158 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300159 struct list_head list_item;
160
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300161};
162
163struct android_configuration {
164 struct usb_configuration usb_config;
165
166 /* A list of the functions supported by this config */
167 struct list_head enabled_functions;
168
169 /* A list node inside the struct android_dev.configs list */
170 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800171};
172
173static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300174static struct list_head android_dev_list;
175static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800176static int android_bind_config(struct usb_configuration *c);
177static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300178static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300179static struct android_configuration *alloc_android_config
180 (struct android_dev *dev);
181static void free_android_config(struct android_dev *dev,
182 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800183
184/* string IDs are assigned dynamically */
185#define STRING_MANUFACTURER_IDX 0
186#define STRING_PRODUCT_IDX 1
187#define STRING_SERIAL_IDX 2
188
189static char manufacturer_string[256];
190static char product_string[256];
191static char serial_string[256];
192
193/* String Table */
194static struct usb_string strings_dev[] = {
195 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
196 [STRING_PRODUCT_IDX].s = product_string,
197 [STRING_SERIAL_IDX].s = serial_string,
198 { } /* end of list */
199};
200
201static struct usb_gadget_strings stringtab_dev = {
202 .language = 0x0409, /* en-us */
203 .strings = strings_dev,
204};
205
206static struct usb_gadget_strings *dev_strings[] = {
207 &stringtab_dev,
208 NULL,
209};
210
211static struct usb_device_descriptor device_desc = {
212 .bLength = sizeof(device_desc),
213 .bDescriptorType = USB_DT_DEVICE,
214 .bcdUSB = __constant_cpu_to_le16(0x0200),
215 .bDeviceClass = USB_CLASS_PER_INTERFACE,
216 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
217 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
218 .bcdDevice = __constant_cpu_to_le16(0xffff),
219 .bNumConfigurations = 1,
220};
221
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530222static struct usb_otg_descriptor otg_descriptor = {
223 .bLength = sizeof otg_descriptor,
224 .bDescriptorType = USB_DT_OTG,
225 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
226 .bcdOTG = __constant_cpu_to_le16(0x0200),
227};
228
229static const struct usb_descriptor_header *otg_desc[] = {
230 (struct usb_descriptor_header *) &otg_descriptor,
231 NULL,
232};
233
Manu Gautama2b54142012-04-03 14:34:32 +0530234enum android_device_state {
235 USB_DISCONNECTED,
236 USB_CONNECTED,
237 USB_CONFIGURED,
238};
239
Ofir Cohen94213a72012-05-03 14:26:32 +0300240static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
241{
242 struct android_usb_platform_data *pdata = dev->pdata;
243 u32 swfi_latency = 0;
244 static int last_vote = -1;
245
Ofir Cohen56eb7072012-05-20 11:41:39 +0300246 if (!pdata || vote == last_vote
247 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300248 return;
249
250 swfi_latency = pdata->swfi_latency + 1;
251 if (vote)
252 pm_qos_update_request(&dev->pm_qos_req_dma,
253 swfi_latency);
254 else
255 pm_qos_update_request(&dev->pm_qos_req_dma,
256 PM_QOS_DEFAULT_VALUE);
257 last_vote = vote;
258}
259
Benoit Goby1e8ce152011-12-12 13:01:23 -0800260static void android_work(struct work_struct *data)
261{
262 struct android_dev *dev = container_of(data, struct android_dev, work);
263 struct usb_composite_dev *cdev = dev->cdev;
264 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
265 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
266 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
267 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530268 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800269 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300270 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800271
272 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530273 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800274 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530275 next_state = USB_CONFIGURED;
276 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800277 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530278 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300279 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300280 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300281 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300282 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530283 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800284 dev->sw_connected = dev->connected;
285 spin_unlock_irqrestore(&cdev->lock, flags);
286
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300287 if (pm_qos_vote != -1)
288 android_pm_qos_update_latency(dev, pm_qos_vote);
289
Benoit Goby1e8ce152011-12-12 13:01:23 -0800290 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530291 /*
292 * Some userspace modules, e.g. MTP, work correctly only if
293 * CONFIGURED uevent is preceded by DISCONNECT uevent.
294 * Check if we missed sending out a DISCONNECT uevent. This can
295 * happen if host PC resets and configures device really quick.
296 */
297 if (((uevent_envp == connected) &&
298 (last_uevent != USB_DISCONNECTED)) ||
299 ((uevent_envp == configured) &&
300 (last_uevent == USB_CONFIGURED))) {
301 pr_info("%s: sent missed DISCONNECT event\n", __func__);
302 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
303 disconnected);
304 msleep(20);
305 }
306 /*
307 * Before sending out CONFIGURED uevent give function drivers
308 * a chance to wakeup userspace threads and notify disconnect
309 */
310 if (uevent_envp == configured)
311 msleep(50);
312
Benoit Goby1e8ce152011-12-12 13:01:23 -0800313 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530314 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800315 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
316 } else {
317 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
318 dev->connected, dev->sw_connected, cdev->config);
319 }
320}
321
Benoit Goby80ba14d2012-03-19 18:56:52 -0700322static void android_enable(struct android_dev *dev)
323{
324 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300325 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700326
327 if (WARN_ON(!dev->disable_depth))
328 return;
329
330 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300331
332 list_for_each_entry(conf, &dev->configs, list_item)
333 usb_add_config(cdev, &conf->usb_config,
334 android_bind_config);
335
Benoit Goby80ba14d2012-03-19 18:56:52 -0700336 usb_gadget_connect(cdev->gadget);
337 }
338}
339
340static void android_disable(struct android_dev *dev)
341{
342 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300343 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700344
345 if (dev->disable_depth++ == 0) {
346 usb_gadget_disconnect(cdev->gadget);
347 /* Cancel pending control requests */
348 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300349
350 list_for_each_entry(conf, &dev->configs, list_item)
351 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700352 }
353}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800354
355/*-------------------------------------------------------------------------*/
356/* Supported functions initialization */
357
Benoit Goby80ba14d2012-03-19 18:56:52 -0700358struct adb_data {
359 bool opened;
360 bool enabled;
361};
362
Benoit Goby2b6862d2011-12-19 14:38:41 -0800363static int
364adb_function_init(struct android_usb_function *f,
365 struct usb_composite_dev *cdev)
366{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700367 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
368 if (!f->config)
369 return -ENOMEM;
370
Benoit Goby2b6862d2011-12-19 14:38:41 -0800371 return adb_setup();
372}
373
374static void adb_function_cleanup(struct android_usb_function *f)
375{
376 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700377 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800378}
379
380static int
381adb_function_bind_config(struct android_usb_function *f,
382 struct usb_configuration *c)
383{
384 return adb_bind_config(c);
385}
386
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387static void adb_android_function_enable(struct android_usb_function *f)
388{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300389 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700390 struct adb_data *data = f->config;
391
392 data->enabled = true;
393
394 /* Disable the gadget until adbd is ready */
395 if (!data->opened)
396 android_disable(dev);
397}
398
399static void adb_android_function_disable(struct android_usb_function *f)
400{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300401 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700402 struct adb_data *data = f->config;
403
404 data->enabled = false;
405
406 /* Balance the disable that was called in closed_callback */
407 if (!data->opened)
408 android_enable(dev);
409}
410
Benoit Goby2b6862d2011-12-19 14:38:41 -0800411static struct android_usb_function adb_function = {
412 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700413 .enable = adb_android_function_enable,
414 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800415 .init = adb_function_init,
416 .cleanup = adb_function_cleanup,
417 .bind_config = adb_function_bind_config,
418};
419
Benoit Goby80ba14d2012-03-19 18:56:52 -0700420static void adb_ready_callback(void)
421{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300422 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700423 struct adb_data *data = adb_function.config;
424
Benoit Goby80ba14d2012-03-19 18:56:52 -0700425 data->opened = true;
426
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300427 if (data->enabled && dev) {
428 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700429 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300430 mutex_unlock(&dev->mutex);
431 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700432}
433
434static void adb_closed_callback(void)
435{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300436 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700437 struct adb_data *data = adb_function.config;
438
Benoit Goby80ba14d2012-03-19 18:56:52 -0700439 data->opened = false;
440
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300441 if (data->enabled) {
442 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700443 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300444 mutex_unlock(&dev->mutex);
445 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700446}
447
Benoit Goby2b6862d2011-12-19 14:38:41 -0800448
Benoit Gobyaab96812011-04-19 20:37:33 -0700449/*-------------------------------------------------------------------------*/
450/* Supported functions initialization */
451
Manu Gautam8e0719b2011-09-26 14:47:55 +0530452/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530453static int rmnet_smd_function_bind_config(struct android_usb_function *f,
454 struct usb_configuration *c)
455{
456 return rmnet_smd_bind_config(c);
457}
458
459static struct android_usb_function rmnet_smd_function = {
460 .name = "rmnet_smd",
461 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800462};
463
Manu Gautam8e0719b2011-09-26 14:47:55 +0530464/* RMNET_SDIO */
465static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
466 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800467{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530468 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800469}
470
Manu Gautam8e0719b2011-09-26 14:47:55 +0530471static struct android_usb_function rmnet_sdio_function = {
472 .name = "rmnet_sdio",
473 .bind_config = rmnet_sdio_function_bind_config,
474};
475
476/* RMNET_SMD_SDIO */
477static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
478 struct usb_composite_dev *cdev)
479{
480 return rmnet_smd_sdio_init();
481}
482
483static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
484{
485 rmnet_smd_sdio_cleanup();
486}
487
488static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
489 struct usb_configuration *c)
490{
491 return rmnet_smd_sdio_function_add(c);
492}
493
494static struct device_attribute *rmnet_smd_sdio_attributes[] = {
495 &dev_attr_transport, NULL };
496
497static struct android_usb_function rmnet_smd_sdio_function = {
498 .name = "rmnet_smd_sdio",
499 .init = rmnet_smd_sdio_function_init,
500 .cleanup = rmnet_smd_sdio_function_cleanup,
501 .bind_config = rmnet_smd_sdio_bind_config,
502 .attributes = rmnet_smd_sdio_attributes,
503};
504
Hemant Kumar1b820d52011-11-03 15:08:28 -0700505/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
506#define MAX_XPORT_STR_LEN 50
507static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530508
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800509/*rmnet transport name string - "rmnet_hsic[,rmnet_hsusb]" */
510static char rmnet_xport_names[MAX_XPORT_STR_LEN];
511
Manu Gautame3e897c2011-09-12 17:18:46 +0530512static void rmnet_function_cleanup(struct android_usb_function *f)
513{
514 frmnet_cleanup();
515}
516
Manu Gautam2b0234a2011-09-07 16:47:52 +0530517static int rmnet_function_bind_config(struct android_usb_function *f,
518 struct usb_configuration *c)
519{
520 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700521 int err = 0;
522 char *ctrl_name;
523 char *data_name;
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800524 char *tname = NULL;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700525 char buf[MAX_XPORT_STR_LEN], *b;
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800526 char xport_name_buf[MAX_XPORT_STR_LEN], *tb;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700527 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530528
Hemant Kumar1b820d52011-11-03 15:08:28 -0700529 if (!rmnet_initialized) {
530 rmnet_initialized = 1;
531 strlcpy(buf, rmnet_transports, sizeof(buf));
532 b = strim(buf);
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800533
534 strlcpy(xport_name_buf, rmnet_xport_names,
535 sizeof(xport_name_buf));
536 tb = strim(xport_name_buf);
537
Hemant Kumar1b820d52011-11-03 15:08:28 -0700538 while (b) {
539 ctrl_name = strsep(&b, ",");
540 data_name = strsep(&b, ",");
541 if (ctrl_name && data_name) {
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800542 if (tb)
543 tname = strsep(&tb, ",");
544 err = frmnet_init_port(ctrl_name, data_name,
545 tname);
Hemant Kumar1b820d52011-11-03 15:08:28 -0700546 if (err) {
547 pr_err("rmnet: Cannot open ctrl port:"
548 "'%s' data port:'%s'\n",
549 ctrl_name, data_name);
550 goto out;
551 }
552 ports++;
553 }
554 }
555
556 err = rmnet_gport_setup();
557 if (err) {
558 pr_err("rmnet: Cannot setup transports");
559 goto out;
560 }
561 }
562
563 for (i = 0; i < ports; i++) {
564 err = frmnet_bind_config(c, i);
565 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530566 pr_err("Could not bind rmnet%u config\n", i);
567 break;
568 }
569 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700570out:
571 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530572}
573
Hemant Kumar1b820d52011-11-03 15:08:28 -0700574static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530575 struct device_attribute *attr, char *buf)
576{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700577 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530578}
579
Hemant Kumar1b820d52011-11-03 15:08:28 -0700580static ssize_t rmnet_transports_store(
581 struct device *device, struct device_attribute *attr,
582 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530583{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700584 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530585
Manu Gautam2b0234a2011-09-07 16:47:52 +0530586 return size;
587}
588
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800589static ssize_t rmnet_xport_names_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
591{
592 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_xport_names);
593}
594
595static ssize_t rmnet_xport_names_store(
596 struct device *device, struct device_attribute *attr,
597 const char *buff, size_t size)
598{
599 strlcpy(rmnet_xport_names, buff, sizeof(rmnet_xport_names));
600
601 return size;
602}
603
Hemant Kumar1b820d52011-11-03 15:08:28 -0700604static struct device_attribute dev_attr_rmnet_transports =
605 __ATTR(transports, S_IRUGO | S_IWUSR,
606 rmnet_transports_show,
607 rmnet_transports_store);
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800608
609static struct device_attribute dev_attr_rmnet_xport_names =
610 __ATTR(transport_names, S_IRUGO | S_IWUSR,
611 rmnet_xport_names_show,
612 rmnet_xport_names_store);
613
Manu Gautam2b0234a2011-09-07 16:47:52 +0530614static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700615 &dev_attr_rmnet_transports,
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800616 &dev_attr_rmnet_xport_names,
Hemant Kumar1b820d52011-11-03 15:08:28 -0700617 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530618
619static struct android_usb_function rmnet_function = {
620 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530621 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530622 .bind_config = rmnet_function_bind_config,
623 .attributes = rmnet_function_attributes,
624};
625
Ofir Cohen7b155422012-07-31 13:02:49 +0300626struct ecm_function_config {
627 u8 ethaddr[ETH_ALEN];
628};
629
630static int ecm_function_init(struct android_usb_function *f,
631 struct usb_composite_dev *cdev)
632{
633 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
634 if (!f->config)
635 return -ENOMEM;
636 return 0;
637}
638
639static void ecm_function_cleanup(struct android_usb_function *f)
640{
641 kfree(f->config);
642 f->config = NULL;
643}
644
645static int ecm_qc_function_bind_config(struct android_usb_function *f,
646 struct usb_configuration *c)
647{
648 int ret;
649 struct ecm_function_config *ecm = f->config;
650
651 if (!ecm) {
652 pr_err("%s: ecm_pdata\n", __func__);
653 return -EINVAL;
654 }
655
656 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
657 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
658 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
659
660 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
661 if (ret) {
662 pr_err("%s: gether_setup failed\n", __func__);
663 return ret;
664 }
665
666 return ecm_qc_bind_config(c, ecm->ethaddr);
667}
668
669static void ecm_qc_function_unbind_config(struct android_usb_function *f,
670 struct usb_configuration *c)
671{
672 gether_qc_cleanup();
673}
674
675static ssize_t ecm_ethaddr_show(struct device *dev,
676 struct device_attribute *attr, char *buf)
677{
678 struct android_usb_function *f = dev_get_drvdata(dev);
679 struct ecm_function_config *ecm = f->config;
680 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
681 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
682 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
683}
684
685static ssize_t ecm_ethaddr_store(struct device *dev,
686 struct device_attribute *attr, const char *buf, size_t size)
687{
688 struct android_usb_function *f = dev_get_drvdata(dev);
689 struct ecm_function_config *ecm = f->config;
690
691 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
692 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
693 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
694 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
695 return size;
696 return -EINVAL;
697}
698
699static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
700 ecm_ethaddr_store);
701
702static struct device_attribute *ecm_function_attributes[] = {
703 &dev_attr_ecm_ethaddr,
704 NULL
705};
706
707static struct android_usb_function ecm_qc_function = {
708 .name = "ecm_qc",
709 .init = ecm_function_init,
710 .cleanup = ecm_function_cleanup,
711 .bind_config = ecm_qc_function_bind_config,
712 .unbind_config = ecm_qc_function_unbind_config,
713 .attributes = ecm_function_attributes,
714};
Anna Perela8c991d2012-04-09 16:44:46 +0300715
716/* MBIM - used with BAM */
717#define MAX_MBIM_INSTANCES 1
718
719static int mbim_function_init(struct android_usb_function *f,
720 struct usb_composite_dev *cdev)
721{
722 return mbim_init(MAX_MBIM_INSTANCES);
723}
724
725static void mbim_function_cleanup(struct android_usb_function *f)
726{
727 fmbim_cleanup();
728}
729
730static int mbim_function_bind_config(struct android_usb_function *f,
731 struct usb_configuration *c)
732{
733 return mbim_bind_config(c, 0);
734}
735
736static struct android_usb_function mbim_function = {
737 .name = "usb_mbim",
738 .cleanup = mbim_function_cleanup,
739 .bind_config = mbim_function_bind_config,
740 .init = mbim_function_init,
741};
742
743
Manu Gautam8e0719b2011-09-26 14:47:55 +0530744/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530745static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746static ssize_t clients_store(
747 struct device *device, struct device_attribute *attr,
748 const char *buff, size_t size)
749{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530750 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751
752 return size;
753}
754
755static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
756static struct device_attribute *diag_function_attributes[] =
757 { &dev_attr_clients, NULL };
758
759static int diag_function_init(struct android_usb_function *f,
760 struct usb_composite_dev *cdev)
761{
762 return diag_setup();
763}
764
765static void diag_function_cleanup(struct android_usb_function *f)
766{
767 diag_cleanup();
768}
769
770static int diag_function_bind_config(struct android_usb_function *f,
771 struct usb_configuration *c)
772{
773 char *name;
774 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530775 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800776 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300777 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530779 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 b = strim(buf);
781
782 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800783 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530785 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300786 if (dev->pdata && !once++)
787 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530788
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530790 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 if (err)
792 pr_err("diag: Cannot open channel '%s'", name);
793 }
794 }
795
796 return err;
797}
798
799static struct android_usb_function diag_function = {
800 .name = "diag",
801 .init = diag_function_init,
802 .cleanup = diag_function_cleanup,
803 .bind_config = diag_function_bind_config,
804 .attributes = diag_function_attributes,
805};
806
Shimrit Malichia00d7322012-08-05 13:56:28 +0300807/* DEBUG */
808static int qdss_function_init(struct android_usb_function *f,
809 struct usb_composite_dev *cdev)
810{
811 return qdss_setup();
812}
813
814static void qdss_function_cleanup(struct android_usb_function *f)
815{
816 qdss_cleanup();
817}
818
819static int qdss_function_bind_config(struct android_usb_function *f,
820 struct usb_configuration *c)
821{
822 int err = -1;
823
824 err = qdss_bind_config(c, "qdss");
825 if (err)
826 pr_err("qdss: Cannot open channel qdss");
827
828 return err;
829}
830
831static struct android_usb_function qdss_function = {
832 .name = "qdss",
833 .init = qdss_function_init,
834 .cleanup = qdss_function_cleanup,
835 .bind_config = qdss_function_bind_config,
836};
837
Manu Gautam8e0719b2011-09-26 14:47:55 +0530838/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530839static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530840static ssize_t serial_transports_store(
841 struct device *device, struct device_attribute *attr,
842 const char *buff, size_t size)
843{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530844 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530845
846 return size;
847}
848
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800849/*enabled FSERIAL transport names - "serial_hsic[,serial_hsusb]"*/
850static char serial_xport_names[32];
851static ssize_t serial_xport_names_store(
852 struct device *device, struct device_attribute *attr,
853 const char *buff, size_t size)
854{
855 strlcpy(serial_xport_names, buff, sizeof(serial_xport_names));
856
857 return size;
858}
859
860static ssize_t serial_xport_names_show(struct device *dev,
861 struct device_attribute *attr, char *buf)
862{
863 return snprintf(buf, PAGE_SIZE, "%s\n", serial_xport_names);
864}
865
Manu Gautama4d993f2011-08-30 18:25:55 +0530866static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800867static struct device_attribute dev_attr_serial_xport_names =
868 __ATTR(transport_names, S_IRUGO | S_IWUSR,
869 serial_xport_names_show,
870 serial_xport_names_store);
871
872static struct device_attribute *serial_function_attributes[] = {
873 &dev_attr_transports,
874 &dev_attr_serial_xport_names,
875 NULL };
Manu Gautama4d993f2011-08-30 18:25:55 +0530876
877static void serial_function_cleanup(struct android_usb_function *f)
878{
879 gserial_cleanup();
880}
881
882static int serial_function_bind_config(struct android_usb_function *f,
883 struct usb_configuration *c)
884{
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800885 char *name, *xport_name = NULL;
886 char buf[32], *b, xport_name_buf[32], *tb;
Manu Gautama4d993f2011-08-30 18:25:55 +0530887 int err = -1, i;
888 static int serial_initialized = 0, ports = 0;
889
890 if (serial_initialized)
891 goto bind_config;
892
893 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530894 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530895 b = strim(buf);
896
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800897 strlcpy(xport_name_buf, serial_xport_names, sizeof(xport_name_buf));
898 tb = strim(xport_name_buf);
899
Manu Gautama4d993f2011-08-30 18:25:55 +0530900 while (b) {
901 name = strsep(&b, ",");
902
903 if (name) {
Hemant Kumar92fe88c2013-02-03 15:56:29 -0800904 if (tb)
905 xport_name = strsep(&tb, ",");
906 err = gserial_init_port(ports, name, xport_name);
Manu Gautama4d993f2011-08-30 18:25:55 +0530907 if (err) {
908 pr_err("serial: Cannot open port '%s'", name);
909 goto out;
910 }
911 ports++;
912 }
913 }
914 err = gport_setup(c);
915 if (err) {
916 pr_err("serial: Cannot setup transports");
917 goto out;
918 }
919
920bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200921 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530922 err = gser_bind_config(c, i);
923 if (err) {
924 pr_err("serial: bind_config failed for port %d", i);
925 goto out;
926 }
927 }
928
929out:
930 return err;
931}
932
933static struct android_usb_function serial_function = {
934 .name = "serial",
935 .cleanup = serial_function_cleanup,
936 .bind_config = serial_function_bind_config,
937 .attributes = serial_function_attributes,
938};
939
Anji jonnala92be1b42011-12-19 09:44:41 +0530940/* ACM */
941static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
942static ssize_t acm_transports_store(
943 struct device *device, struct device_attribute *attr,
944 const char *buff, size_t size)
945{
946 strlcpy(acm_transports, buff, sizeof(acm_transports));
947
948 return size;
949}
950
951static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530952
953/*enabled ACM transport names - "serial_hsic[,serial_hsusb]"*/
954static char acm_xport_names[32];
955static ssize_t acm_xport_names_store(
956 struct device *device, struct device_attribute *attr,
957 const char *buff, size_t size)
958{
959 strlcpy(acm_xport_names, buff, sizeof(acm_xport_names));
960
961 return size;
962}
963
964static DEVICE_ATTR(acm_transport_names, S_IWUSR, NULL, acm_xport_names_store);
965
Anji jonnala92be1b42011-12-19 09:44:41 +0530966static struct device_attribute *acm_function_attributes[] = {
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530967 &dev_attr_acm_transports,
968 &dev_attr_acm_transport_names,
969 NULL };
Anji jonnala92be1b42011-12-19 09:44:41 +0530970
Benoit Goby1e8ce152011-12-12 13:01:23 -0800971static void acm_function_cleanup(struct android_usb_function *f)
972{
973 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800974}
975
Anji jonnala92be1b42011-12-19 09:44:41 +0530976static int acm_function_bind_config(struct android_usb_function *f,
977 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800978{
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530979 char *name, *xport_name = NULL;
980 char buf[32], *b, xport_name_buf[32], *tb;
Anji jonnala92be1b42011-12-19 09:44:41 +0530981 int err = -1, i;
982 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800983
Anji jonnala92be1b42011-12-19 09:44:41 +0530984 if (acm_initialized)
985 goto bind_config;
986
987 acm_initialized = 1;
988 strlcpy(buf, acm_transports, sizeof(buf));
989 b = strim(buf);
990
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530991 strlcpy(xport_name_buf, acm_xport_names, sizeof(xport_name_buf));
992 tb = strim(xport_name_buf);
993
Anji jonnala92be1b42011-12-19 09:44:41 +0530994 while (b) {
995 name = strsep(&b, ",");
996
997 if (name) {
Pavankumar Kondetid0f3d432013-04-25 10:35:54 +0530998 if (tb)
999 xport_name = strsep(&tb, ",");
1000 err = acm_init_port(ports, name, xport_name);
Anji jonnala92be1b42011-12-19 09:44:41 +05301001 if (err) {
1002 pr_err("acm: Cannot open port '%s'", name);
1003 goto out;
1004 }
1005 ports++;
1006 }
1007 }
1008 err = acm_port_setup(c);
1009 if (err) {
1010 pr_err("acm: Cannot setup transports");
1011 goto out;
1012 }
1013
1014bind_config:
1015 for (i = 0; i < ports; i++) {
1016 err = acm_bind_config(c, i);
1017 if (err) {
1018 pr_err("acm: bind_config failed for port %d", i);
1019 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001020 }
1021 }
1022
Anji jonnala92be1b42011-12-19 09:44:41 +05301023out:
1024 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001025}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001026static struct android_usb_function acm_function = {
1027 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001028 .cleanup = acm_function_cleanup,
1029 .bind_config = acm_function_bind_config,
1030 .attributes = acm_function_attributes,
1031};
1032
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301033/* CCID */
1034static int ccid_function_init(struct android_usb_function *f,
1035 struct usb_composite_dev *cdev)
1036{
1037 return ccid_setup();
1038}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001039
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301040static void ccid_function_cleanup(struct android_usb_function *f)
1041{
1042 ccid_cleanup();
1043}
1044
1045static int ccid_function_bind_config(struct android_usb_function *f,
1046 struct usb_configuration *c)
1047{
1048 return ccid_bind_config(c);
1049}
1050
1051static struct android_usb_function ccid_function = {
1052 .name = "ccid",
1053 .init = ccid_function_init,
1054 .cleanup = ccid_function_cleanup,
1055 .bind_config = ccid_function_bind_config,
1056};
1057
Steve Mucklef132c6c2012-06-06 18:30:57 -07001058static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001059 struct usb_composite_dev *cdev)
1060{
1061 return mtp_setup();
1062}
1063
1064static void mtp_function_cleanup(struct android_usb_function *f)
1065{
1066 mtp_cleanup();
1067}
1068
Steve Mucklef132c6c2012-06-06 18:30:57 -07001069static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001070 struct usb_configuration *c)
1071{
1072 return mtp_bind_config(c, false);
1073}
1074
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001075static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001076{
1077 /* nothing to do - initialization is handled by mtp_function_init */
1078 return 0;
1079}
1080
1081static void ptp_function_cleanup(struct android_usb_function *f)
1082{
1083 /* nothing to do - cleanup is handled by mtp_function_cleanup */
1084}
1085
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001086static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001087{
1088 return mtp_bind_config(c, true);
1089}
1090
1091static int mtp_function_ctrlrequest(struct android_usb_function *f,
1092 struct usb_composite_dev *cdev,
1093 const struct usb_ctrlrequest *c)
1094{
1095 return mtp_ctrlrequest(cdev, c);
1096}
1097
1098static struct android_usb_function mtp_function = {
1099 .name = "mtp",
1100 .init = mtp_function_init,
1101 .cleanup = mtp_function_cleanup,
1102 .bind_config = mtp_function_bind_config,
1103 .ctrlrequest = mtp_function_ctrlrequest,
1104};
1105
1106/* PTP function is same as MTP with slightly different interface descriptor */
1107static struct android_usb_function ptp_function = {
1108 .name = "ptp",
1109 .init = ptp_function_init,
1110 .cleanup = ptp_function_cleanup,
1111 .bind_config = ptp_function_bind_config,
1112};
1113
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001114#ifdef CONFIG_USB_ANDROID_CDC_ECM
1115struct ecm_function_config {
1116 u8 ethaddr[ETH_ALEN];
1117 u32 vendorID;
1118 char manufacturer[256];
1119};
1120
1121static int ecm_function_init(struct android_usb_function *f,
1122 struct usb_composite_dev *cdev)
1123{
1124 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
1125 if (!f->config)
1126 return -ENOMEM;
1127 return 0;
1128}
1129
1130static void ecm_function_cleanup(struct android_usb_function *f)
1131{
1132 kfree(f->config);
1133 f->config = NULL;
1134}
1135
1136static int ecm_function_bind_config(struct android_usb_function *f,
1137 struct usb_configuration *c)
1138{
1139 int ret;
1140 struct ecm_function_config *ecm = f->config;
1141
1142 if (!ecm) {
1143 pr_err("%s: ecm_function_config\n", __func__);
1144 return -1;
1145 }
1146
1147 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1148 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1149 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1150
1151 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usb");
1152 if (ret) {
1153 pr_err("%s: gether_setup failed\n", __func__);
1154 return ret;
1155 }
1156
1157 return ecm_bind_config(c, ecm->ethaddr);
1158}
1159
1160static void ecm_function_unbind_config(struct android_usb_function *f,
1161 struct usb_configuration *c)
1162{
1163 gether_cleanup();
1164}
1165
1166static ssize_t ecm_manufacturer_show(struct device *dev,
1167 struct device_attribute *attr, char *buf)
1168{
1169 struct android_usb_function *f = dev_get_drvdata(dev);
1170 struct ecm_function_config *config = f->config;
1171
1172 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
1173}
1174
1175static ssize_t ecm_manufacturer_store(struct device *dev,
1176 struct device_attribute *attr, const char *buf, size_t size)
1177{
1178 struct android_usb_function *f = dev_get_drvdata(dev);
1179 struct ecm_function_config *config = f->config;
1180
1181 if (size >= sizeof(config->manufacturer))
1182 return -EINVAL;
1183
1184 if (sscanf(buf, "%255s", config->manufacturer) == 1)
1185 return size;
1186 return -1;
1187}
1188
1189static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, ecm_manufacturer_show,
1190 ecm_manufacturer_store);
1191
1192static ssize_t ecm_ethaddr_show(struct device *dev,
1193 struct device_attribute *attr, char *buf)
1194{
1195 struct android_usb_function *f = dev_get_drvdata(dev);
1196 struct ecm_function_config *ecm = f->config;
1197 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1198 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1199 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1200}
1201
1202static ssize_t ecm_ethaddr_store(struct device *dev,
1203 struct device_attribute *attr, const char *buf, size_t size)
1204{
1205 struct android_usb_function *f = dev_get_drvdata(dev);
1206 struct ecm_function_config *ecm = f->config;
1207
1208 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1209 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1210 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1211 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1212 return size;
1213 return -EINVAL;
1214}
1215
1216static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1217 ecm_ethaddr_store);
1218
1219static ssize_t ecm_vendorID_show(struct device *dev,
1220 struct device_attribute *attr, char *buf)
1221{
1222 struct android_usb_function *f = dev_get_drvdata(dev);
1223 struct ecm_function_config *config = f->config;
1224
1225 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
1226}
1227
1228static ssize_t ecm_vendorID_store(struct device *dev,
1229 struct device_attribute *attr, const char *buf, size_t size)
1230{
1231 struct android_usb_function *f = dev_get_drvdata(dev);
1232 struct ecm_function_config *config = f->config;
1233 int value;
1234
1235 if (sscanf(buf, "%04x", &value) == 1) {
1236 config->vendorID = value;
1237 return size;
1238 }
1239 return -EINVAL;
1240}
1241
1242static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, ecm_vendorID_show,
1243 ecm_vendorID_store);
1244
1245static struct device_attribute *ecm_function_attributes[] = {
1246 &dev_attr_manufacturer,
1247 &dev_attr_ethaddr,
1248 &dev_attr_vendorID,
1249 NULL
1250};
1251
1252static struct android_usb_function ecm_function = {
1253 .name = "ecm",
1254 .init = ecm_function_init,
1255 .cleanup = ecm_function_cleanup,
1256 .bind_config = ecm_function_bind_config,
1257 .unbind_config = ecm_function_unbind_config,
1258 .attributes = ecm_function_attributes,
1259};
1260
1261#else
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001262
Benoit Goby1e8ce152011-12-12 13:01:23 -08001263struct rndis_function_config {
1264 u8 ethaddr[ETH_ALEN];
1265 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001266 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001267 char manufacturer[256];
1268 /* "Wireless" RNDIS; auto-detected by Windows */
1269 bool wceis;
1270};
1271
1272static int
1273rndis_function_init(struct android_usb_function *f,
1274 struct usb_composite_dev *cdev)
1275{
1276 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1277 if (!f->config)
1278 return -ENOMEM;
1279 return 0;
1280}
1281
1282static void rndis_function_cleanup(struct android_usb_function *f)
1283{
1284 kfree(f->config);
1285 f->config = NULL;
1286}
1287
Ofir Cohenaef90b72012-07-31 12:37:04 +02001288static int rndis_qc_function_init(struct android_usb_function *f,
1289 struct usb_composite_dev *cdev)
1290{
1291 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1292 if (!f->config)
1293 return -ENOMEM;
1294
1295 return rndis_qc_init();
1296}
1297
1298static void rndis_qc_function_cleanup(struct android_usb_function *f)
1299{
1300 rndis_qc_cleanup();
1301 kfree(f->config);
1302}
1303
Benoit Goby1e8ce152011-12-12 13:01:23 -08001304static int
1305rndis_function_bind_config(struct android_usb_function *f,
1306 struct usb_configuration *c)
1307{
1308 int ret;
1309 struct rndis_function_config *rndis = f->config;
1310
1311 if (!rndis) {
1312 pr_err("%s: rndis_pdata\n", __func__);
1313 return -1;
1314 }
1315
1316 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1317 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1318 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1319
Devin Kim78e07192012-07-17 08:40:54 -07001320 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "usb");
Benoit Goby1e8ce152011-12-12 13:01:23 -08001321 if (ret) {
1322 pr_err("%s: gether_setup failed\n", __func__);
1323 return ret;
1324 }
1325
1326 if (rndis->wceis) {
1327 /* "Wireless" RNDIS; auto-detected by Windows */
1328 rndis_iad_descriptor.bFunctionClass =
1329 USB_CLASS_WIRELESS_CONTROLLER;
1330 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1331 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1332 rndis_control_intf.bInterfaceClass =
1333 USB_CLASS_WIRELESS_CONTROLLER;
1334 rndis_control_intf.bInterfaceSubClass = 0x01;
1335 rndis_control_intf.bInterfaceProtocol = 0x03;
1336 }
1337
1338 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1339 rndis->manufacturer);
1340}
1341
Ofir Cohenaef90b72012-07-31 12:37:04 +02001342static int rndis_qc_function_bind_config(struct android_usb_function *f,
1343 struct usb_configuration *c)
1344{
1345 int ret;
1346 struct rndis_function_config *rndis = f->config;
1347
1348 if (!rndis) {
1349 pr_err("%s: rndis_pdata\n", __func__);
1350 return -EINVAL;
1351 }
1352
1353 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1354 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1355 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1356
1357 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1358 if (ret) {
1359 pr_err("%s: gether_setup failed\n", __func__);
1360 return ret;
1361 }
1362
1363 if (rndis->wceis) {
1364 /* "Wireless" RNDIS; auto-detected by Windows */
1365 rndis_qc_iad_descriptor.bFunctionClass =
1366 USB_CLASS_WIRELESS_CONTROLLER;
1367 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1368 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1369 rndis_qc_control_intf.bInterfaceClass =
1370 USB_CLASS_WIRELESS_CONTROLLER;
1371 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1372 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1373 }
1374
1375 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1376 rndis->manufacturer,
1377 rndis->max_pkt_per_xfer);
1378}
1379
Benoit Goby1e8ce152011-12-12 13:01:23 -08001380static void rndis_function_unbind_config(struct android_usb_function *f,
1381 struct usb_configuration *c)
1382{
1383 gether_cleanup();
1384}
1385
Ofir Cohenaef90b72012-07-31 12:37:04 +02001386static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1387 struct usb_configuration *c)
1388{
1389 gether_qc_cleanup();
1390}
1391
Benoit Goby1e8ce152011-12-12 13:01:23 -08001392static ssize_t rndis_manufacturer_show(struct device *dev,
1393 struct device_attribute *attr, char *buf)
1394{
1395 struct android_usb_function *f = dev_get_drvdata(dev);
1396 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301398 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001399}
1400
1401static ssize_t rndis_manufacturer_store(struct device *dev,
1402 struct device_attribute *attr, const char *buf, size_t size)
1403{
1404 struct android_usb_function *f = dev_get_drvdata(dev);
1405 struct rndis_function_config *config = f->config;
1406
1407 if (size >= sizeof(config->manufacturer))
1408 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001409
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301410 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001411 return size;
1412 return -1;
1413}
1414
1415static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1416 rndis_manufacturer_store);
1417
1418static ssize_t rndis_wceis_show(struct device *dev,
1419 struct device_attribute *attr, char *buf)
1420{
1421 struct android_usb_function *f = dev_get_drvdata(dev);
1422 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301424 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001425}
1426
1427static ssize_t rndis_wceis_store(struct device *dev,
1428 struct device_attribute *attr, const char *buf, size_t size)
1429{
1430 struct android_usb_function *f = dev_get_drvdata(dev);
1431 struct rndis_function_config *config = f->config;
1432 int value;
1433
1434 if (sscanf(buf, "%d", &value) == 1) {
1435 config->wceis = value;
1436 return size;
1437 }
1438 return -EINVAL;
1439}
1440
1441static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1442 rndis_wceis_store);
1443
1444static ssize_t rndis_ethaddr_show(struct device *dev,
1445 struct device_attribute *attr, char *buf)
1446{
1447 struct android_usb_function *f = dev_get_drvdata(dev);
1448 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001449
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301450 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001451 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1452 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1453}
1454
1455static ssize_t rndis_ethaddr_store(struct device *dev,
1456 struct device_attribute *attr, const char *buf, size_t size)
1457{
1458 struct android_usb_function *f = dev_get_drvdata(dev);
1459 struct rndis_function_config *rndis = f->config;
1460
1461 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1462 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1463 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1464 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1465 return size;
1466 return -EINVAL;
1467}
1468
1469static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1470 rndis_ethaddr_store);
1471
1472static ssize_t rndis_vendorID_show(struct device *dev,
1473 struct device_attribute *attr, char *buf)
1474{
1475 struct android_usb_function *f = dev_get_drvdata(dev);
1476 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001477
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301478 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001479}
1480
1481static ssize_t rndis_vendorID_store(struct device *dev,
1482 struct device_attribute *attr, const char *buf, size_t size)
1483{
1484 struct android_usb_function *f = dev_get_drvdata(dev);
1485 struct rndis_function_config *config = f->config;
1486 int value;
1487
1488 if (sscanf(buf, "%04x", &value) == 1) {
1489 config->vendorID = value;
1490 return size;
1491 }
1492 return -EINVAL;
1493}
1494
1495static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1496 rndis_vendorID_store);
1497
Ofir Cohenaef90b72012-07-31 12:37:04 +02001498static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1499 struct device_attribute *attr, char *buf)
1500{
1501 struct android_usb_function *f = dev_get_drvdata(dev);
1502 struct rndis_function_config *config = f->config;
1503 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1504}
1505
1506static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1507 struct device_attribute *attr, const char *buf, size_t size)
1508{
1509 struct android_usb_function *f = dev_get_drvdata(dev);
1510 struct rndis_function_config *config = f->config;
1511 int value;
1512
1513 if (sscanf(buf, "%d", &value) == 1) {
1514 config->max_pkt_per_xfer = value;
1515 return size;
1516 }
1517 return -EINVAL;
1518}
1519
1520static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1521 rndis_max_pkt_per_xfer_show,
1522 rndis_max_pkt_per_xfer_store);
1523
Benoit Goby1e8ce152011-12-12 13:01:23 -08001524static struct device_attribute *rndis_function_attributes[] = {
1525 &dev_attr_manufacturer,
1526 &dev_attr_wceis,
1527 &dev_attr_ethaddr,
1528 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001529 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001530 NULL
1531};
1532
1533static struct android_usb_function rndis_function = {
1534 .name = "rndis",
1535 .init = rndis_function_init,
1536 .cleanup = rndis_function_cleanup,
1537 .bind_config = rndis_function_bind_config,
1538 .unbind_config = rndis_function_unbind_config,
1539 .attributes = rndis_function_attributes,
1540};
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001541#endif /* CONFIG_USB_ANDROID_CDC_ECM */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001542
Ofir Cohenaef90b72012-07-31 12:37:04 +02001543static struct android_usb_function rndis_qc_function = {
1544 .name = "rndis_qc",
1545 .init = rndis_qc_function_init,
1546 .cleanup = rndis_qc_function_cleanup,
1547 .bind_config = rndis_qc_function_bind_config,
1548 .unbind_config = rndis_qc_function_unbind_config,
1549 .attributes = rndis_function_attributes,
1550};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001551
1552struct mass_storage_function_config {
1553 struct fsg_config fsg;
1554 struct fsg_common *common;
1555};
1556
1557static int mass_storage_function_init(struct android_usb_function *f,
1558 struct usb_composite_dev *cdev)
1559{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001560 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001561 struct mass_storage_function_config *config;
1562 struct fsg_common *common;
1563 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301564 int i;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001565
1566 config = kzalloc(sizeof(struct mass_storage_function_config),
1567 GFP_KERNEL);
1568 if (!config)
1569 return -ENOMEM;
1570
Matt Mowerb9f66092013-12-11 23:03:55 -06001571 if (dev->pdata && dev->pdata->nluns) {
1572 config->fsg.nluns = dev->pdata->nluns;
1573 if (config->fsg.nluns > FSG_MAX_LUNS)
1574 config->fsg.nluns = FSG_MAX_LUNS;
1575 for (i = 0; i < config->fsg.nluns; i++) {
1576 config->fsg.luns[i].cdrom = 0;
1577 config->fsg.luns[i].removable = 1;
1578 config->fsg.luns[i].ro = 0;
1579 }
1580 if (dev->pdata->cdrom) {
1581 config->fsg.nluns += 1;
1582 config->fsg.luns[i+1].cdrom = 1;
1583 config->fsg.luns[i+1].removable = 1;
1584 config->fsg.luns[i+1].ro = 1;
1585 }
1586 } else {
1587 config->fsg.nluns = 1;
1588 config->fsg.luns[0].cdrom = 0;
1589 config->fsg.luns[0].removable = 1;
1590 config->fsg.luns[0].ro = 0;
1591 if (dev->pdata && dev->pdata->cdrom) {
1592 config->fsg.nluns = 2;
1593 config->fsg.luns[1].cdrom = 1;
1594 config->fsg.luns[1].removable = 1;
1595 config->fsg.luns[1].ro = 1;
1596 }
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301597 }
1598
Benoit Goby1e8ce152011-12-12 13:01:23 -08001599 common = fsg_common_init(NULL, cdev, &config->fsg);
1600 if (IS_ERR(common)) {
1601 kfree(config);
1602 return PTR_ERR(common);
1603 }
1604
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301605 for (i = 0; i < config->fsg.nluns; i++) {
1606 err = sysfs_create_link(&f->dev->kobj,
1607 &common->luns[i].dev.kobj,
Matt Mowerb9f66092013-12-11 23:03:55 -06001608 common->luns[i].dev.kobj.name);
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301609 if (err)
1610 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001611 }
1612
1613 config->common = common;
1614 f->config = config;
1615 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301616error:
1617 for (; i > 0 ; i--)
Matt Mowerb9f66092013-12-11 23:03:55 -06001618 sysfs_remove_link(&f->dev->kobj, common->luns[i-1].dev.kobj.name);
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301619
1620 fsg_common_release(&common->ref);
1621 kfree(config);
1622 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001623}
1624
1625static void mass_storage_function_cleanup(struct android_usb_function *f)
1626{
1627 kfree(f->config);
1628 f->config = NULL;
1629}
1630
1631static int mass_storage_function_bind_config(struct android_usb_function *f,
1632 struct usb_configuration *c)
1633{
1634 struct mass_storage_function_config *config = f->config;
1635 return fsg_bind_config(c->cdev, c, config->common);
1636}
1637
1638static ssize_t mass_storage_inquiry_show(struct device *dev,
1639 struct device_attribute *attr, char *buf)
1640{
1641 struct android_usb_function *f = dev_get_drvdata(dev);
1642 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301643 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001644}
1645
1646static ssize_t mass_storage_inquiry_store(struct device *dev,
1647 struct device_attribute *attr, const char *buf, size_t size)
1648{
1649 struct android_usb_function *f = dev_get_drvdata(dev);
1650 struct mass_storage_function_config *config = f->config;
1651 if (size >= sizeof(config->common->inquiry_string))
1652 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301653 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001654 return -EINVAL;
1655 return size;
1656}
1657
1658static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1659 mass_storage_inquiry_show,
1660 mass_storage_inquiry_store);
1661
1662static struct device_attribute *mass_storage_function_attributes[] = {
1663 &dev_attr_inquiry_string,
1664 NULL
1665};
1666
1667static struct android_usb_function mass_storage_function = {
1668 .name = "mass_storage",
1669 .init = mass_storage_function_init,
1670 .cleanup = mass_storage_function_cleanup,
1671 .bind_config = mass_storage_function_bind_config,
1672 .attributes = mass_storage_function_attributes,
1673};
1674
1675
Benoit Gobycf3fc062011-12-19 14:39:37 -08001676static int accessory_function_init(struct android_usb_function *f,
1677 struct usb_composite_dev *cdev)
1678{
1679 return acc_setup();
1680}
1681
1682static void accessory_function_cleanup(struct android_usb_function *f)
1683{
1684 acc_cleanup();
1685}
1686
1687static int accessory_function_bind_config(struct android_usb_function *f,
1688 struct usb_configuration *c)
1689{
1690 return acc_bind_config(c);
1691}
1692
1693static int accessory_function_ctrlrequest(struct android_usb_function *f,
1694 struct usb_composite_dev *cdev,
1695 const struct usb_ctrlrequest *c)
1696{
1697 return acc_ctrlrequest(cdev, c);
1698}
1699
1700static struct android_usb_function accessory_function = {
1701 .name = "accessory",
1702 .init = accessory_function_init,
1703 .cleanup = accessory_function_cleanup,
1704 .bind_config = accessory_function_bind_config,
1705 .ctrlrequest = accessory_function_ctrlrequest,
1706};
1707
Ajay Dudani34b1e302012-08-27 16:43:53 +05301708static int audio_source_function_init(struct android_usb_function *f,
1709 struct usb_composite_dev *cdev)
1710{
1711 struct audio_source_config *config;
1712
1713 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1714 if (!config)
1715 return -ENOMEM;
1716 config->card = -1;
1717 config->device = -1;
1718 f->config = config;
1719 return 0;
1720}
1721
1722static void audio_source_function_cleanup(struct android_usb_function *f)
1723{
1724 kfree(f->config);
1725}
1726
1727static int audio_source_function_bind_config(struct android_usb_function *f,
1728 struct usb_configuration *c)
1729{
1730 struct audio_source_config *config = f->config;
1731
1732 return audio_source_bind_config(c, config);
1733}
1734
1735static void audio_source_function_unbind_config(struct android_usb_function *f,
1736 struct usb_configuration *c)
1737{
1738 struct audio_source_config *config = f->config;
1739
1740 config->card = -1;
1741 config->device = -1;
1742}
1743
1744static ssize_t audio_source_pcm_show(struct device *dev,
1745 struct device_attribute *attr, char *buf)
1746{
1747 struct android_usb_function *f = dev_get_drvdata(dev);
1748 struct audio_source_config *config = f->config;
1749
1750 /* print PCM card and device numbers */
1751 return sprintf(buf, "%d %d\n", config->card, config->device);
1752}
1753
1754static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1755
1756static struct device_attribute *audio_source_function_attributes[] = {
1757 &dev_attr_pcm,
1758 NULL
1759};
1760
1761static struct android_usb_function audio_source_function = {
1762 .name = "audio_source",
1763 .init = audio_source_function_init,
1764 .cleanup = audio_source_function_cleanup,
1765 .bind_config = audio_source_function_bind_config,
1766 .unbind_config = audio_source_function_unbind_config,
1767 .attributes = audio_source_function_attributes,
1768};
1769
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301770static int android_uasp_connect_cb(bool connect)
1771{
1772 /*
1773 * TODO
1774 * We may have to disable gadget till UASP configfs nodes
1775 * are configured which includes mapping LUN with the
1776 * backing file. It is a fundamental difference between
1777 * f_mass_storage and f_tcp. That means UASP can not be
1778 * in default composition.
1779 *
1780 * For now, assume that UASP configfs nodes are configured
1781 * before enabling android gadget. Or cable should be
1782 * reconnected after mapping the LUN.
1783 *
1784 * Also consider making UASP to respond to Host requests when
1785 * Lun is not mapped.
1786 */
1787 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1788
1789 return 0;
1790}
1791
1792static int uasp_function_init(struct android_usb_function *f,
1793 struct usb_composite_dev *cdev)
1794{
1795 return f_tcm_init(&android_uasp_connect_cb);
1796}
1797
1798static void uasp_function_cleanup(struct android_usb_function *f)
1799{
1800 f_tcm_exit();
1801}
1802
1803static int uasp_function_bind_config(struct android_usb_function *f,
1804 struct usb_configuration *c)
1805{
1806 return tcm_bind_config(c);
1807}
1808
1809static struct android_usb_function uasp_function = {
1810 .name = "uasp",
1811 .init = uasp_function_init,
1812 .cleanup = uasp_function_cleanup,
1813 .bind_config = uasp_function_bind_config,
1814};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001815
Benoit Goby1e8ce152011-12-12 13:01:23 -08001816static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001817 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001818 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301819 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301820 &rmnet_sdio_function,
1821 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301822 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001824 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301825 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001826 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301827 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001828 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001829 &mtp_function,
1830 &ptp_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001831#ifdef CONFIG_USB_ANDROID_CDC_ECM
1832 &ecm_function,
1833#else
Benoit Goby1e8ce152011-12-12 13:01:23 -08001834 &rndis_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001835#endif
Ofir Cohenaef90b72012-07-31 12:37:04 +02001836 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001837 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001838 &accessory_function,
Ajay Dudani34b1e302012-08-27 16:43:53 +05301839 &audio_source_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301840 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001841 NULL
1842};
1843
Lena Salmand092f2d2012-03-12 17:27:24 +02001844static void android_cleanup_functions(struct android_usb_function **functions)
1845{
1846 struct android_usb_function *f;
1847 struct device_attribute **attrs;
1848 struct device_attribute *attr;
1849
1850 while (*functions) {
1851 f = *functions++;
1852
1853 if (f->dev) {
1854 device_destroy(android_class, f->dev->devt);
1855 kfree(f->dev_name);
1856 } else
1857 continue;
1858
1859 if (f->cleanup)
1860 f->cleanup(f);
1861
1862 attrs = f->attributes;
1863 if (attrs) {
1864 while ((attr = *attrs++))
1865 device_remove_file(f->dev, attr);
1866 }
1867 }
1868}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001869
1870static int android_init_functions(struct android_usb_function **functions,
1871 struct usb_composite_dev *cdev)
1872{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001873 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001874 struct android_usb_function *f;
1875 struct device_attribute **attrs;
1876 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301877 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001878 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001879
1880 for (; (f = *functions++); index++) {
1881 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001882 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001883 if (!f->dev_name) {
1884 err = -ENOMEM;
1885 goto err_out;
1886 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001887 f->dev = device_create(android_class, dev->dev,
1888 MKDEV(0, index), f, f->dev_name);
1889 if (IS_ERR(f->dev)) {
1890 pr_err("%s: Failed to create dev %s", __func__,
1891 f->dev_name);
1892 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001893 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001894 goto err_create;
1895 }
1896
1897 if (f->init) {
1898 err = f->init(f, cdev);
1899 if (err) {
1900 pr_err("%s: Failed to init %s", __func__,
1901 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001902 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001903 }
1904 }
1905
1906 attrs = f->attributes;
1907 if (attrs) {
1908 while ((attr = *attrs++) && !err)
1909 err = device_create_file(f->dev, attr);
1910 }
1911 if (err) {
1912 pr_err("%s: Failed to create function %s attributes",
1913 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001914 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001915 }
1916 }
1917 return 0;
1918
Lena Salmand092f2d2012-03-12 17:27:24 +02001919err_attrs:
1920 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1921 device_remove_file(f->dev, attr);
1922 if (f->cleanup)
1923 f->cleanup(f);
1924err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001925 device_destroy(android_class, f->dev->devt);
1926err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001927 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001928 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001929err_out:
1930 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001931 return err;
1932}
1933
Benoit Goby1e8ce152011-12-12 13:01:23 -08001934static int
1935android_bind_enabled_functions(struct android_dev *dev,
1936 struct usb_configuration *c)
1937{
1938 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001939 struct android_configuration *conf =
1940 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001941 int ret;
1942
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001943 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001944 ret = f->bind_config(f, c);
1945 if (ret) {
1946 pr_err("%s: %s failed", __func__, f->name);
1947 return ret;
1948 }
1949 }
1950 return 0;
1951}
1952
1953static void
1954android_unbind_enabled_functions(struct android_dev *dev,
1955 struct usb_configuration *c)
1956{
1957 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001958 struct android_configuration *conf =
1959 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001960
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001961 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001962 if (f->unbind_config)
1963 f->unbind_config(f, c);
1964 }
1965}
1966
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001967static int android_enable_function(struct android_dev *dev,
1968 struct android_configuration *conf,
1969 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001970{
1971 struct android_usb_function **functions = dev->functions;
1972 struct android_usb_function *f;
1973 while ((f = *functions++)) {
1974 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001975 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001976 pr_err("%s already enabled in other " \
1977 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001978 f->name);
1979 else {
1980 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001981 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001982 f->android_dev = dev;
1983 return 0;
1984 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001985 }
1986 }
1987 return -EINVAL;
1988}
1989
1990/*-------------------------------------------------------------------------*/
1991/* /sys/class/android_usb/android%d/ interface */
1992
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301993static ssize_t remote_wakeup_show(struct device *pdev,
1994 struct device_attribute *attr, char *buf)
1995{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001996 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001997 struct android_configuration *conf;
1998
1999 /*
2000 * Show the wakeup attribute of the first configuration,
2001 * since all configurations have the same wakeup attribute
2002 */
2003 if (dev->configs_num == 0)
2004 return 0;
2005 conf = list_entry(dev->configs.next,
2006 struct android_configuration,
2007 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002008
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302009 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002010 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302011 USB_CONFIG_ATT_WAKEUP));
2012}
2013
2014static ssize_t remote_wakeup_store(struct device *pdev,
2015 struct device_attribute *attr, const char *buff, size_t size)
2016{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002017 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002018 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302019 int enable = 0;
2020
2021 sscanf(buff, "%d", &enable);
2022
2023 pr_debug("android_usb: %s remote wakeup\n",
2024 enable ? "enabling" : "disabling");
2025
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002026 list_for_each_entry(conf, &dev->configs, list_item)
2027 if (enable)
2028 conf->usb_config.bmAttributes |=
2029 USB_CONFIG_ATT_WAKEUP;
2030 else
2031 conf->usb_config.bmAttributes &=
2032 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302033
2034 return size;
2035}
2036
Benoit Goby1e8ce152011-12-12 13:01:23 -08002037static ssize_t
2038functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
2039{
2040 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002041 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002042 struct android_usb_function *f;
2043 char *buff = buf;
2044
2045 mutex_lock(&dev->mutex);
2046
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002047 list_for_each_entry(conf, &dev->configs, list_item) {
2048 if (buff != buf)
2049 *(buff-1) = ':';
2050 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
2051 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
2052 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002053
2054 mutex_unlock(&dev->mutex);
2055
2056 if (buff != buf)
2057 *(buff-1) = '\n';
2058 return buff - buf;
2059}
2060
2061static ssize_t
2062functions_store(struct device *pdev, struct device_attribute *attr,
2063 const char *buff, size_t size)
2064{
2065 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002066 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002067 struct list_head *curr_conf = &dev->configs;
2068 struct android_configuration *conf;
2069 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002070 char *name;
2071 char buf[256], *b;
2072 int err;
2073
2074 mutex_lock(&dev->mutex);
2075
2076 if (dev->enabled) {
2077 mutex_unlock(&dev->mutex);
2078 return -EBUSY;
2079 }
2080
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002081 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002082 list_for_each_entry(conf, &dev->configs, list_item) {
2083 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
2084 f->android_dev = NULL;
2085 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002086 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002087
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302088 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08002089 b = strim(buf);
2090
2091 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002092 conf_str = strsep(&b, ":");
2093 if (conf_str) {
2094 /* If the next not equal to the head, take it */
2095 if (curr_conf->next != &dev->configs)
2096 conf = list_entry(curr_conf->next,
2097 struct android_configuration,
2098 list_item);
2099 else
2100 conf = alloc_android_config(dev);
2101
2102 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002103 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002104
2105 while (conf_str) {
2106 name = strsep(&conf_str, ",");
2107 if (name) {
2108 err = android_enable_function(dev, conf, name);
2109 if (err)
2110 pr_err("android_usb: Cannot enable %s",
2111 name);
2112 }
2113 }
2114 }
2115
2116 /* Free uneeded configurations if exists */
2117 while (curr_conf->next != &dev->configs) {
2118 conf = list_entry(curr_conf->next,
2119 struct android_configuration, list_item);
2120 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002121 }
2122
2123 mutex_unlock(&dev->mutex);
2124
2125 return size;
2126}
2127
2128static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2129 char *buf)
2130{
2131 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002132
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302133 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002134}
2135
2136static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2137 const char *buff, size_t size)
2138{
2139 struct android_dev *dev = dev_get_drvdata(pdev);
2140 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07002141 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002142 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002143 int enabled = 0;
Pavankumar Kondetia7bb2922013-02-28 10:19:40 +05302144 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002145
Benoit Gobycf3fc062011-12-19 14:39:37 -08002146 if (!cdev)
2147 return -ENODEV;
2148
Benoit Goby1e8ce152011-12-12 13:01:23 -08002149 mutex_lock(&dev->mutex);
2150
2151 sscanf(buff, "%d", &enabled);
2152 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002153 /*
2154 * Update values in composite driver's copy of
2155 * device descriptor.
2156 */
2157 cdev->desc.idVendor = device_desc.idVendor;
2158 cdev->desc.idProduct = device_desc.idProduct;
2159 cdev->desc.bcdDevice = device_desc.bcdDevice;
2160 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2161 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2162 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002163 list_for_each_entry(conf, &dev->configs, list_item)
2164 list_for_each_entry(f, &conf->enabled_functions,
2165 enabled_list) {
2166 if (f->enable)
2167 f->enable(f);
2168 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07002169 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002170 dev->enabled = true;
2171 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002172 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002173 list_for_each_entry(conf, &dev->configs, list_item)
2174 list_for_each_entry(f, &conf->enabled_functions,
2175 enabled_list) {
2176 if (f->disable)
2177 f->disable(f);
2178 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002179 dev->enabled = false;
Pavankumar Kondetia7bb2922013-02-28 10:19:40 +05302180 } else if (__ratelimit(&rl)) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002181 pr_err("android_usb: already %s\n",
2182 dev->enabled ? "enabled" : "disabled");
2183 }
2184
2185 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002186
Benoit Gobyaab96812011-04-19 20:37:33 -07002187 return size;
2188}
2189
Ofir Cohen94213a72012-05-03 14:26:32 +03002190static ssize_t pm_qos_show(struct device *pdev,
2191 struct device_attribute *attr, char *buf)
2192{
2193 struct android_dev *dev = dev_get_drvdata(pdev);
2194
2195 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2196}
2197
2198static ssize_t pm_qos_store(struct device *pdev,
2199 struct device_attribute *attr,
2200 const char *buff, size_t size)
2201{
2202 struct android_dev *dev = dev_get_drvdata(pdev);
2203
2204 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2205
Benoit Goby1e8ce152011-12-12 13:01:23 -08002206 return size;
2207}
2208
2209static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2210 char *buf)
2211{
2212 struct android_dev *dev = dev_get_drvdata(pdev);
2213 struct usb_composite_dev *cdev = dev->cdev;
2214 char *state = "DISCONNECTED";
2215 unsigned long flags;
2216
2217 if (!cdev)
2218 goto out;
2219
2220 spin_lock_irqsave(&cdev->lock, flags);
2221 if (cdev->config)
2222 state = "CONFIGURED";
2223 else if (dev->connected)
2224 state = "CONNECTED";
2225 spin_unlock_irqrestore(&cdev->lock, flags);
2226out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302227 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002228}
2229
2230#define DESCRIPTOR_ATTR(field, format_string) \
2231static ssize_t \
2232field ## _show(struct device *dev, struct device_attribute *attr, \
2233 char *buf) \
2234{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302235 return snprintf(buf, PAGE_SIZE, \
2236 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002237} \
2238static ssize_t \
2239field ## _store(struct device *dev, struct device_attribute *attr, \
2240 const char *buf, size_t size) \
2241{ \
2242 int value; \
2243 if (sscanf(buf, format_string, &value) == 1) { \
2244 device_desc.field = value; \
2245 return size; \
2246 } \
2247 return -1; \
2248} \
2249static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2250
2251#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2252static ssize_t \
2253field ## _show(struct device *dev, struct device_attribute *attr, \
2254 char *buf) \
2255{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302256 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002257} \
2258static ssize_t \
2259field ## _store(struct device *dev, struct device_attribute *attr, \
2260 const char *buf, size_t size) \
2261{ \
2262 if (size >= sizeof(buffer)) \
2263 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302264 strlcpy(buffer, buf, sizeof(buffer)); \
2265 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302266 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002267} \
2268static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2269
2270
2271DESCRIPTOR_ATTR(idVendor, "%04x\n")
2272DESCRIPTOR_ATTR(idProduct, "%04x\n")
2273DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2274DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2275DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2276DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2277DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2278DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2279DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2280
2281static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2282 functions_store);
2283static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002284static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2285 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002286static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302287static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2288 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002289
2290static struct device_attribute *android_usb_attributes[] = {
2291 &dev_attr_idVendor,
2292 &dev_attr_idProduct,
2293 &dev_attr_bcdDevice,
2294 &dev_attr_bDeviceClass,
2295 &dev_attr_bDeviceSubClass,
2296 &dev_attr_bDeviceProtocol,
2297 &dev_attr_iManufacturer,
2298 &dev_attr_iProduct,
2299 &dev_attr_iSerial,
2300 &dev_attr_functions,
2301 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002302 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002303 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302304 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002305 NULL
2306};
2307
2308/*-------------------------------------------------------------------------*/
2309/* Composite driver */
2310
2311static int android_bind_config(struct usb_configuration *c)
2312{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002313 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002314 int ret = 0;
2315
2316 ret = android_bind_enabled_functions(dev, c);
2317 if (ret)
2318 return ret;
2319
2320 return 0;
2321}
2322
2323static void android_unbind_config(struct usb_configuration *c)
2324{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002325 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002326
2327 android_unbind_enabled_functions(dev, c);
2328}
2329
2330static int android_bind(struct usb_composite_dev *cdev)
2331{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002332 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002333 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002334 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002335 int gcnum, id, ret;
2336
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002337 /* Bind to the last android_dev that was probed */
2338 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2339
2340 dev->cdev = cdev;
2341
Benoit Goby1e8ce152011-12-12 13:01:23 -08002342 /*
2343 * Start disconnected. Userspace will connect the gadget once
2344 * it is done configuring the functions.
2345 */
2346 usb_gadget_disconnect(gadget);
2347
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002348 /* Init the supported functions only once, on the first android_dev */
2349 if (android_dev_count == 1) {
2350 ret = android_init_functions(dev->functions, cdev);
2351 if (ret)
2352 return ret;
2353 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002354
2355 /* Allocate string descriptor numbers ... note that string
2356 * contents can be overridden by the composite_dev glue.
2357 */
2358 id = usb_string_id(cdev);
2359 if (id < 0)
2360 return id;
2361 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2362 device_desc.iManufacturer = id;
2363
2364 id = usb_string_id(cdev);
2365 if (id < 0)
2366 return id;
2367 strings_dev[STRING_PRODUCT_IDX].id = id;
2368 device_desc.iProduct = id;
2369
2370 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302371 strlcpy(manufacturer_string, "Android",
2372 sizeof(manufacturer_string) - 1);
2373 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2374 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002375
2376 id = usb_string_id(cdev);
2377 if (id < 0)
2378 return id;
2379 strings_dev[STRING_SERIAL_IDX].id = id;
2380 device_desc.iSerialNumber = id;
2381
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302382 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002383 list_for_each_entry(conf, &dev->configs, list_item)
2384 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302385
Benoit Goby1e8ce152011-12-12 13:01:23 -08002386 gcnum = usb_gadget_controller_number(gadget);
2387 if (gcnum >= 0)
2388 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2389 else {
2390 pr_warning("%s: controller '%s' not recognized\n",
2391 longname, gadget->name);
2392 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2393 }
2394
Benoit Goby1e8ce152011-12-12 13:01:23 -08002395 return 0;
2396}
2397
2398static int android_usb_unbind(struct usb_composite_dev *cdev)
2399{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002400 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002401
Lena Salmand092f2d2012-03-12 17:27:24 +02002402 manufacturer_string[0] = '\0';
2403 product_string[0] = '\0';
2404 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002405 cancel_work_sync(&dev->work);
2406 android_cleanup_functions(dev->functions);
2407 return 0;
2408}
2409
2410static struct usb_composite_driver android_usb_driver = {
2411 .name = "android_usb",
2412 .dev = &device_desc,
2413 .strings = dev_strings,
2414 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002415 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002416};
2417
2418static int
2419android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2420{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002421 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002422 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002423 struct usb_request *req = cdev->req;
2424 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002425 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002426 int value = -EOPNOTSUPP;
2427 unsigned long flags;
2428
2429 req->zero = 0;
2430 req->complete = composite_setup_complete;
2431 req->length = 0;
2432 gadget->ep0->driver_data = cdev;
2433
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002434 list_for_each_entry(conf, &dev->configs, list_item)
taeju.park257b5082012-10-09 10:52:01 +09002435 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
2436 if (f->ctrlrequest) {
2437 value = f->ctrlrequest(f, cdev, c);
2438 if (value >= 0)
2439 break;
2440 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002441
Benoit Gobycf3fc062011-12-19 14:39:37 -08002442 /* Special case the accessory function.
2443 * It needs to handle control requests before it is enabled.
2444 */
2445 if (value < 0)
2446 value = acc_ctrlrequest(cdev, c);
2447
Benoit Goby1e8ce152011-12-12 13:01:23 -08002448 if (value < 0)
2449 value = composite_setup(gadget, c);
2450
2451 spin_lock_irqsave(&cdev->lock, flags);
2452 if (!dev->connected) {
2453 dev->connected = 1;
2454 schedule_work(&dev->work);
2455 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2456 cdev->config) {
2457 schedule_work(&dev->work);
2458 }
2459 spin_unlock_irqrestore(&cdev->lock, flags);
2460
2461 return value;
2462}
2463
2464static void android_disconnect(struct usb_gadget *gadget)
2465{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002466 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002467 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002468 unsigned long flags;
2469
2470 composite_disconnect(gadget);
Ajay Dudanic2f0cc72012-08-27 16:23:48 +05302471 /* accessory HID support can be active while the
2472 accessory function is not actually enabled,
2473 so we need to inform it when we are disconnected.
2474 */
2475 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002476
2477 spin_lock_irqsave(&cdev->lock, flags);
2478 dev->connected = 0;
2479 schedule_work(&dev->work);
2480 spin_unlock_irqrestore(&cdev->lock, flags);
2481}
2482
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002483static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002484{
2485 struct device_attribute **attrs = android_usb_attributes;
2486 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002487 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002488 int err;
2489
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002490 /*
2491 * The primary usb core should always have usb_core_id=0, since
2492 * Android user space is currently interested in android0 events.
2493 */
2494 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2495 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002496 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002497 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002498 if (IS_ERR(dev->dev))
2499 return PTR_ERR(dev->dev);
2500
2501 dev_set_drvdata(dev->dev, dev);
2502
2503 while ((attr = *attrs++)) {
2504 err = device_create_file(dev->dev, attr);
2505 if (err) {
2506 device_destroy(android_class, dev->dev->devt);
2507 return err;
2508 }
2509 }
2510 return 0;
2511}
2512
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302513static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002514{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302515 struct device_attribute **attrs = android_usb_attributes;
2516 struct device_attribute *attr;
2517
2518 while ((attr = *attrs++))
2519 device_remove_file(dev->dev, attr);
2520 device_destroy(android_class, dev->dev->devt);
2521}
2522
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002523static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2524{
2525 struct android_dev *dev = NULL;
2526
2527 /* Find the android dev from the list */
2528 list_for_each_entry(dev, &android_dev_list, list_item) {
2529 if (dev->cdev == cdev)
2530 break;
2531 }
2532
2533 return dev;
2534}
2535
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002536static struct android_configuration *alloc_android_config
2537 (struct android_dev *dev)
2538{
2539 struct android_configuration *conf;
2540
2541 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2542 if (!conf) {
2543 pr_err("%s(): Failed to alloc memory for android conf\n",
2544 __func__);
2545 return ERR_PTR(-ENOMEM);
2546 }
2547
2548 dev->configs_num++;
2549 conf->usb_config.label = dev->name;
2550 conf->usb_config.unbind = android_unbind_config;
2551 conf->usb_config.bConfigurationValue = dev->configs_num;
2552
2553 INIT_LIST_HEAD(&conf->enabled_functions);
2554
2555 list_add_tail(&conf->list_item, &dev->configs);
2556
2557 return conf;
2558}
2559
2560static void free_android_config(struct android_dev *dev,
2561 struct android_configuration *conf)
2562{
2563 list_del(&conf->list_item);
2564 dev->configs_num--;
2565 kfree(conf);
2566}
2567
Flemmardef2717b2014-01-16 10:51:24 +01002568#if defined(CONFIG_MACH_HTC) && defined(CONFIG_ARCH_MSM8X60)
2569static ssize_t show_usb_function_switch(struct device *dev,
2570 struct device_attribute *attr, char *buf)
2571{
2572 return sprintf(buf, "ether:disable\nrndis:disable\n");
2573}
2574
2575static ssize_t store_usb_function_switch(struct device *dev,
2576 struct device_attribute *attr, const char *buf, size_t count)
2577{
2578 return 0;
2579}
2580
2581static DEVICE_ATTR(usb_function_switch, 0664,
2582 show_usb_function_switch, store_usb_function_switch);
2583
2584static struct attribute *android_htc_usb_attributes[] = {
2585 &dev_attr_usb_function_switch.attr,
2586 NULL
2587};
2588
2589static const struct attribute_group android_usb_attr_group = {
2590 .attrs = android_htc_usb_attributes,
2591};
2592#endif
2593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594static int __devinit android_probe(struct platform_device *pdev)
2595{
2596 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002597 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002598 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002599
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002600 if (!android_class) {
2601 android_class = class_create(THIS_MODULE, "android_usb");
2602 if (IS_ERR(android_class))
2603 return PTR_ERR(android_class);
2604 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002605
Flemmardef2717b2014-01-16 10:51:24 +01002606#if defined(CONFIG_MACH_HTC) && defined(CONFIG_ARCH_MSM8X60)
2607 if (sysfs_create_group(&pdev->dev.kobj, &android_usb_attr_group))
2608 pr_err("%s: fail to create sysfs\n", __func__);
2609#endif
2610
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002611 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2612 if (!android_dev) {
2613 pr_err("%s(): Failed to alloc memory for android_dev\n",
2614 __func__);
2615 ret = -ENOMEM;
2616 goto err_alloc;
2617 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002618
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002619 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002620 android_dev->disable_depth = 1;
2621 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002622 android_dev->configs_num = 0;
2623 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002624 INIT_WORK(&android_dev->work, android_work);
2625 mutex_init(&android_dev->mutex);
2626
2627 android_dev->pdata = pdata;
2628
2629 list_add_tail(&android_dev->list_item, &android_dev_list);
2630 android_dev_count++;
2631
2632 if (pdata)
2633 composite_driver.usb_core_id = pdata->usb_core_id;
2634 else
2635 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2636
2637 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302638 if (ret) {
2639 pr_err("%s(): android_create_device failed\n", __func__);
2640 goto err_dev;
2641 }
2642
Lena Salmand092f2d2012-03-12 17:27:24 +02002643 ret = usb_composite_probe(&android_usb_driver, android_bind);
2644 if (ret) {
2645 pr_err("%s(): Failed to register android "
2646 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302647 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002648 }
2649
Ofir Cohen94213a72012-05-03 14:26:32 +03002650 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302651 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002652 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002653 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002654 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002655
Lena Salmand092f2d2012-03-12 17:27:24 +02002656 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302657err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002658 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302659err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002660 list_del(&android_dev->list_item);
2661 android_dev_count--;
2662 kfree(android_dev);
2663err_alloc:
2664 if (list_empty(&android_dev_list)) {
2665 class_destroy(android_class);
2666 android_class = NULL;
2667 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302668 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002669}
2670
2671static int android_remove(struct platform_device *pdev)
2672{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002673 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002674 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002675 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302676
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002677 if (pdata)
2678 usb_core_id = pdata->usb_core_id;
2679
2680 /* Find the android dev from the list */
2681 list_for_each_entry(dev, &android_dev_list, list_item) {
2682 if (!dev->pdata)
2683 break; /*To backward compatibility*/
2684 if (dev->pdata->usb_core_id == usb_core_id)
2685 break;
2686 }
2687
2688 if (dev) {
2689 android_destroy_device(dev);
2690 if (pdata && pdata->swfi_latency)
2691 pm_qos_remove_request(&dev->pm_qos_req_dma);
2692 list_del(&dev->list_item);
2693 android_dev_count--;
2694 kfree(dev);
2695 }
2696
2697 if (list_empty(&android_dev_list)) {
2698 class_destroy(android_class);
2699 android_class = NULL;
2700 usb_composite_unregister(&android_usb_driver);
2701 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002703 return 0;
2704}
2705
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002706static const struct platform_device_id android_id_table[] __devinitconst = {
2707 {
2708 .name = "android_usb",
2709 },
2710 {
2711 .name = "android_usb_hsic",
2712 },
2713};
2714
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002715static struct platform_driver android_platform_driver = {
2716 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002717 .probe = android_probe,
2718 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002719 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002720};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002721
2722static int __init init(void)
2723{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302724 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002725
Benoit Goby1e8ce152011-12-12 13:01:23 -08002726 /* Override composite driver functions */
2727 composite_driver.setup = android_setup;
2728 composite_driver.disconnect = android_disconnect;
2729
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002730 INIT_LIST_HEAD(&android_dev_list);
2731 android_dev_count = 0;
2732
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302733 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302734 if (ret) {
2735 pr_err("%s(): Failed to register android"
2736 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302737 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002738
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302739 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002740}
2741module_init(init);
2742
2743static void __exit cleanup(void)
2744{
Lena Salmand092f2d2012-03-12 17:27:24 +02002745 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002746}
2747module_exit(cleanup);