blob: da3bf5ac3151457f0313bdc3dc6776a831e0496f [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"
34
35/*
36 * Kbuild is not very cooperative with respect to linking separately
37 * compiled library objects into one module. So for now we won't use
38 * separate compilation ... ensuring init/exit sections work to shrink
39 * the runtime footprint, and giving us at least some parts of what
40 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 */
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45#include "composite.c"
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "f_diag.c"
Shimrit Malichia00d7322012-08-05 13:56:28 +030048#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053049#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053050#include "f_rmnet_sdio.c"
51#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053052#include "f_rmnet.c"
Ajay Dudani34b1e302012-08-27 16:43:53 +053053#include "f_audio_source.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080054#include "f_mass_storage.c"
55#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053056#include "u_sdio.c"
57#include "u_smd.c"
58#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053059#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080060#include "u_ctrl_hsic.c"
61#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053062#include "u_ctrl_hsuart.c"
63#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053064#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080065#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080066#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053067#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080068#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080069#include "f_accessory.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070070#ifdef CONFIG_USB_ANDROID_CDC_ECM
71#include "f_ecm.c"
72#else
Benoit Goby1e8ce152011-12-12 13:01:23 -080073#define USB_ETH_RNDIS y
74#include "f_rndis.c"
75#include "rndis.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070076#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080077#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030078#include "u_bam_data.c"
79#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030080#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020081#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030082#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053083#ifdef CONFIG_TARGET_CORE
84#include "f_tcm.c"
85#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080086
87MODULE_AUTHOR("Mike Lockwood");
88MODULE_DESCRIPTION("Android Composite USB Driver");
89MODULE_LICENSE("GPL");
90MODULE_VERSION("1.0");
91
92static const char longname[] = "Gadget Android";
93
94/* Default vendor and product IDs, overridden by userspace */
95#define VENDOR_ID 0x18D1
96#define PRODUCT_ID 0x0001
97
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030098#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
99
Benoit Goby1e8ce152011-12-12 13:01:23 -0800100struct android_usb_function {
101 char *name;
102 void *config;
103
104 struct device *dev;
105 char *dev_name;
106 struct device_attribute **attributes;
107
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300108 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800109 struct list_head enabled_list;
110
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300111 struct android_dev *android_dev;
112
Benoit Goby1e8ce152011-12-12 13:01:23 -0800113 /* Optional: initialization during gadget bind */
114 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
115 /* Optional: cleanup during gadget unbind */
116 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700117 /* Optional: called when the function is added the list of
118 * enabled functions */
119 void (*enable)(struct android_usb_function *);
120 /* Optional: called when it is removed */
121 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800122
123 int (*bind_config)(struct android_usb_function *,
124 struct usb_configuration *);
125
126 /* Optional: called when the configuration is removed */
127 void (*unbind_config)(struct android_usb_function *,
128 struct usb_configuration *);
129 /* Optional: handle ctrl requests before the device is configured */
130 int (*ctrlrequest)(struct android_usb_function *,
131 struct usb_composite_dev *,
132 const struct usb_ctrlrequest *);
133};
134
135struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300136 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800137 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800138 struct usb_composite_dev *cdev;
139 struct device *dev;
140
141 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700142 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800143 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700144 struct android_usb_platform_data *pdata;
145
Benoit Goby1e8ce152011-12-12 13:01:23 -0800146 bool connected;
147 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300148 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700149 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800150 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300151
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300152 /* A list of struct android_configuration */
153 struct list_head configs;
154 int configs_num;
155
156 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300157 struct list_head list_item;
158
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300159};
160
161struct android_configuration {
162 struct usb_configuration usb_config;
163
164 /* A list of the functions supported by this config */
165 struct list_head enabled_functions;
166
167 /* A list node inside the struct android_dev.configs list */
168 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800169};
170
171static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300172static struct list_head android_dev_list;
173static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800174static int android_bind_config(struct usb_configuration *c);
175static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300176static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300177static struct android_configuration *alloc_android_config
178 (struct android_dev *dev);
179static void free_android_config(struct android_dev *dev,
180 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800181
182/* string IDs are assigned dynamically */
183#define STRING_MANUFACTURER_IDX 0
184#define STRING_PRODUCT_IDX 1
185#define STRING_SERIAL_IDX 2
186
187static char manufacturer_string[256];
188static char product_string[256];
189static char serial_string[256];
190
191/* String Table */
192static struct usb_string strings_dev[] = {
193 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
194 [STRING_PRODUCT_IDX].s = product_string,
195 [STRING_SERIAL_IDX].s = serial_string,
196 { } /* end of list */
197};
198
199static struct usb_gadget_strings stringtab_dev = {
200 .language = 0x0409, /* en-us */
201 .strings = strings_dev,
202};
203
204static struct usb_gadget_strings *dev_strings[] = {
205 &stringtab_dev,
206 NULL,
207};
208
209static struct usb_device_descriptor device_desc = {
210 .bLength = sizeof(device_desc),
211 .bDescriptorType = USB_DT_DEVICE,
212 .bcdUSB = __constant_cpu_to_le16(0x0200),
213 .bDeviceClass = USB_CLASS_PER_INTERFACE,
214 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
215 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
216 .bcdDevice = __constant_cpu_to_le16(0xffff),
217 .bNumConfigurations = 1,
218};
219
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530220static struct usb_otg_descriptor otg_descriptor = {
221 .bLength = sizeof otg_descriptor,
222 .bDescriptorType = USB_DT_OTG,
223 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
224 .bcdOTG = __constant_cpu_to_le16(0x0200),
225};
226
227static const struct usb_descriptor_header *otg_desc[] = {
228 (struct usb_descriptor_header *) &otg_descriptor,
229 NULL,
230};
231
Manu Gautama2b54142012-04-03 14:34:32 +0530232enum android_device_state {
233 USB_DISCONNECTED,
234 USB_CONNECTED,
235 USB_CONFIGURED,
236};
237
Ofir Cohen94213a72012-05-03 14:26:32 +0300238static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
239{
240 struct android_usb_platform_data *pdata = dev->pdata;
241 u32 swfi_latency = 0;
242 static int last_vote = -1;
243
Ofir Cohen56eb7072012-05-20 11:41:39 +0300244 if (!pdata || vote == last_vote
245 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300246 return;
247
248 swfi_latency = pdata->swfi_latency + 1;
249 if (vote)
250 pm_qos_update_request(&dev->pm_qos_req_dma,
251 swfi_latency);
252 else
253 pm_qos_update_request(&dev->pm_qos_req_dma,
254 PM_QOS_DEFAULT_VALUE);
255 last_vote = vote;
256}
257
Benoit Goby1e8ce152011-12-12 13:01:23 -0800258static void android_work(struct work_struct *data)
259{
260 struct android_dev *dev = container_of(data, struct android_dev, work);
261 struct usb_composite_dev *cdev = dev->cdev;
262 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
263 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
264 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
265 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530266 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800267 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300268 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800269
270 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530271 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800272 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530273 next_state = USB_CONFIGURED;
274 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800275 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530276 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300277 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300278 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300279 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300280 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530281 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800282 dev->sw_connected = dev->connected;
283 spin_unlock_irqrestore(&cdev->lock, flags);
284
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300285 if (pm_qos_vote != -1)
286 android_pm_qos_update_latency(dev, pm_qos_vote);
287
Benoit Goby1e8ce152011-12-12 13:01:23 -0800288 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530289 /*
290 * Some userspace modules, e.g. MTP, work correctly only if
291 * CONFIGURED uevent is preceded by DISCONNECT uevent.
292 * Check if we missed sending out a DISCONNECT uevent. This can
293 * happen if host PC resets and configures device really quick.
294 */
295 if (((uevent_envp == connected) &&
296 (last_uevent != USB_DISCONNECTED)) ||
297 ((uevent_envp == configured) &&
298 (last_uevent == USB_CONFIGURED))) {
299 pr_info("%s: sent missed DISCONNECT event\n", __func__);
300 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
301 disconnected);
302 msleep(20);
303 }
304 /*
305 * Before sending out CONFIGURED uevent give function drivers
306 * a chance to wakeup userspace threads and notify disconnect
307 */
308 if (uevent_envp == configured)
309 msleep(50);
310
Benoit Goby1e8ce152011-12-12 13:01:23 -0800311 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530312 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800313 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
314 } else {
315 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
316 dev->connected, dev->sw_connected, cdev->config);
317 }
318}
319
Benoit Goby80ba14d2012-03-19 18:56:52 -0700320static void android_enable(struct android_dev *dev)
321{
322 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300323 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700324
325 if (WARN_ON(!dev->disable_depth))
326 return;
327
328 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300329
330 list_for_each_entry(conf, &dev->configs, list_item)
331 usb_add_config(cdev, &conf->usb_config,
332 android_bind_config);
333
Benoit Goby80ba14d2012-03-19 18:56:52 -0700334 usb_gadget_connect(cdev->gadget);
335 }
336}
337
338static void android_disable(struct android_dev *dev)
339{
340 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300341 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700342
343 if (dev->disable_depth++ == 0) {
344 usb_gadget_disconnect(cdev->gadget);
345 /* Cancel pending control requests */
346 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300347
348 list_for_each_entry(conf, &dev->configs, list_item)
349 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700350 }
351}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800352
353/*-------------------------------------------------------------------------*/
354/* Supported functions initialization */
355
Benoit Goby80ba14d2012-03-19 18:56:52 -0700356struct adb_data {
357 bool opened;
358 bool enabled;
359};
360
Benoit Goby2b6862d2011-12-19 14:38:41 -0800361static int
362adb_function_init(struct android_usb_function *f,
363 struct usb_composite_dev *cdev)
364{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700365 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
366 if (!f->config)
367 return -ENOMEM;
368
Benoit Goby2b6862d2011-12-19 14:38:41 -0800369 return adb_setup();
370}
371
372static void adb_function_cleanup(struct android_usb_function *f)
373{
374 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700375 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800376}
377
378static int
379adb_function_bind_config(struct android_usb_function *f,
380 struct usb_configuration *c)
381{
382 return adb_bind_config(c);
383}
384
Benoit Goby80ba14d2012-03-19 18:56:52 -0700385static void adb_android_function_enable(struct android_usb_function *f)
386{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300387 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700388 struct adb_data *data = f->config;
389
390 data->enabled = true;
391
392 /* Disable the gadget until adbd is ready */
393 if (!data->opened)
394 android_disable(dev);
395}
396
397static void adb_android_function_disable(struct android_usb_function *f)
398{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300399 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700400 struct adb_data *data = f->config;
401
402 data->enabled = false;
403
404 /* Balance the disable that was called in closed_callback */
405 if (!data->opened)
406 android_enable(dev);
407}
408
Benoit Goby2b6862d2011-12-19 14:38:41 -0800409static struct android_usb_function adb_function = {
410 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700411 .enable = adb_android_function_enable,
412 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800413 .init = adb_function_init,
414 .cleanup = adb_function_cleanup,
415 .bind_config = adb_function_bind_config,
416};
417
Benoit Goby80ba14d2012-03-19 18:56:52 -0700418static void adb_ready_callback(void)
419{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300420 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700421 struct adb_data *data = adb_function.config;
422
Benoit Goby80ba14d2012-03-19 18:56:52 -0700423 data->opened = true;
424
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300425 if (data->enabled && dev) {
426 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700427 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300428 mutex_unlock(&dev->mutex);
429 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700430}
431
432static void adb_closed_callback(void)
433{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300434 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700435 struct adb_data *data = adb_function.config;
436
Benoit Goby80ba14d2012-03-19 18:56:52 -0700437 data->opened = false;
438
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300439 if (data->enabled) {
440 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700441 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300442 mutex_unlock(&dev->mutex);
443 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700444}
445
Benoit Goby2b6862d2011-12-19 14:38:41 -0800446
Benoit Gobyaab96812011-04-19 20:37:33 -0700447/*-------------------------------------------------------------------------*/
448/* Supported functions initialization */
449
Manu Gautam8e0719b2011-09-26 14:47:55 +0530450/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530451static int rmnet_smd_function_bind_config(struct android_usb_function *f,
452 struct usb_configuration *c)
453{
454 return rmnet_smd_bind_config(c);
455}
456
457static struct android_usb_function rmnet_smd_function = {
458 .name = "rmnet_smd",
459 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800460};
461
Manu Gautam8e0719b2011-09-26 14:47:55 +0530462/* RMNET_SDIO */
463static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
464 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800465{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530466 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800467}
468
Manu Gautam8e0719b2011-09-26 14:47:55 +0530469static struct android_usb_function rmnet_sdio_function = {
470 .name = "rmnet_sdio",
471 .bind_config = rmnet_sdio_function_bind_config,
472};
473
474/* RMNET_SMD_SDIO */
475static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
476 struct usb_composite_dev *cdev)
477{
478 return rmnet_smd_sdio_init();
479}
480
481static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
482{
483 rmnet_smd_sdio_cleanup();
484}
485
486static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
487 struct usb_configuration *c)
488{
489 return rmnet_smd_sdio_function_add(c);
490}
491
492static struct device_attribute *rmnet_smd_sdio_attributes[] = {
493 &dev_attr_transport, NULL };
494
495static struct android_usb_function rmnet_smd_sdio_function = {
496 .name = "rmnet_smd_sdio",
497 .init = rmnet_smd_sdio_function_init,
498 .cleanup = rmnet_smd_sdio_function_cleanup,
499 .bind_config = rmnet_smd_sdio_bind_config,
500 .attributes = rmnet_smd_sdio_attributes,
501};
502
Hemant Kumar1b820d52011-11-03 15:08:28 -0700503/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
504#define MAX_XPORT_STR_LEN 50
505static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530506
Manu Gautame3e897c2011-09-12 17:18:46 +0530507static void rmnet_function_cleanup(struct android_usb_function *f)
508{
509 frmnet_cleanup();
510}
511
Manu Gautam2b0234a2011-09-07 16:47:52 +0530512static int rmnet_function_bind_config(struct android_usb_function *f,
513 struct usb_configuration *c)
514{
515 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700516 int err = 0;
517 char *ctrl_name;
518 char *data_name;
519 char buf[MAX_XPORT_STR_LEN], *b;
520 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530521
Hemant Kumar1b820d52011-11-03 15:08:28 -0700522 if (!rmnet_initialized) {
523 rmnet_initialized = 1;
524 strlcpy(buf, rmnet_transports, sizeof(buf));
525 b = strim(buf);
526 while (b) {
527 ctrl_name = strsep(&b, ",");
528 data_name = strsep(&b, ",");
529 if (ctrl_name && data_name) {
530 err = frmnet_init_port(ctrl_name, data_name);
531 if (err) {
532 pr_err("rmnet: Cannot open ctrl port:"
533 "'%s' data port:'%s'\n",
534 ctrl_name, data_name);
535 goto out;
536 }
537 ports++;
538 }
539 }
540
541 err = rmnet_gport_setup();
542 if (err) {
543 pr_err("rmnet: Cannot setup transports");
544 goto out;
545 }
546 }
547
548 for (i = 0; i < ports; i++) {
549 err = frmnet_bind_config(c, i);
550 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530551 pr_err("Could not bind rmnet%u config\n", i);
552 break;
553 }
554 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700555out:
556 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530557}
558
Hemant Kumar1b820d52011-11-03 15:08:28 -0700559static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530560 struct device_attribute *attr, char *buf)
561{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700562 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530563}
564
Hemant Kumar1b820d52011-11-03 15:08:28 -0700565static ssize_t rmnet_transports_store(
566 struct device *device, struct device_attribute *attr,
567 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530568{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700569 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530570
Manu Gautam2b0234a2011-09-07 16:47:52 +0530571 return size;
572}
573
Hemant Kumar1b820d52011-11-03 15:08:28 -0700574static struct device_attribute dev_attr_rmnet_transports =
575 __ATTR(transports, S_IRUGO | S_IWUSR,
576 rmnet_transports_show,
577 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530578static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700579 &dev_attr_rmnet_transports,
580 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530581
582static struct android_usb_function rmnet_function = {
583 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530584 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530585 .bind_config = rmnet_function_bind_config,
586 .attributes = rmnet_function_attributes,
587};
588
Ofir Cohen7b155422012-07-31 13:02:49 +0300589struct ecm_function_config {
590 u8 ethaddr[ETH_ALEN];
591};
592
593static int ecm_function_init(struct android_usb_function *f,
594 struct usb_composite_dev *cdev)
595{
596 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
597 if (!f->config)
598 return -ENOMEM;
599 return 0;
600}
601
602static void ecm_function_cleanup(struct android_usb_function *f)
603{
604 kfree(f->config);
605 f->config = NULL;
606}
607
608static int ecm_qc_function_bind_config(struct android_usb_function *f,
609 struct usb_configuration *c)
610{
611 int ret;
612 struct ecm_function_config *ecm = f->config;
613
614 if (!ecm) {
615 pr_err("%s: ecm_pdata\n", __func__);
616 return -EINVAL;
617 }
618
619 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
620 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
621 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
622
623 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
624 if (ret) {
625 pr_err("%s: gether_setup failed\n", __func__);
626 return ret;
627 }
628
629 return ecm_qc_bind_config(c, ecm->ethaddr);
630}
631
632static void ecm_qc_function_unbind_config(struct android_usb_function *f,
633 struct usb_configuration *c)
634{
635 gether_qc_cleanup();
636}
637
638static ssize_t ecm_ethaddr_show(struct device *dev,
639 struct device_attribute *attr, char *buf)
640{
641 struct android_usb_function *f = dev_get_drvdata(dev);
642 struct ecm_function_config *ecm = f->config;
643 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
644 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
645 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
646}
647
648static ssize_t ecm_ethaddr_store(struct device *dev,
649 struct device_attribute *attr, const char *buf, size_t size)
650{
651 struct android_usb_function *f = dev_get_drvdata(dev);
652 struct ecm_function_config *ecm = f->config;
653
654 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
655 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
656 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
657 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
658 return size;
659 return -EINVAL;
660}
661
662static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
663 ecm_ethaddr_store);
664
665static struct device_attribute *ecm_function_attributes[] = {
666 &dev_attr_ecm_ethaddr,
667 NULL
668};
669
670static struct android_usb_function ecm_qc_function = {
671 .name = "ecm_qc",
672 .init = ecm_function_init,
673 .cleanup = ecm_function_cleanup,
674 .bind_config = ecm_qc_function_bind_config,
675 .unbind_config = ecm_qc_function_unbind_config,
676 .attributes = ecm_function_attributes,
677};
Anna Perela8c991d2012-04-09 16:44:46 +0300678
679/* MBIM - used with BAM */
680#define MAX_MBIM_INSTANCES 1
681
682static int mbim_function_init(struct android_usb_function *f,
683 struct usb_composite_dev *cdev)
684{
685 return mbim_init(MAX_MBIM_INSTANCES);
686}
687
688static void mbim_function_cleanup(struct android_usb_function *f)
689{
690 fmbim_cleanup();
691}
692
693static int mbim_function_bind_config(struct android_usb_function *f,
694 struct usb_configuration *c)
695{
696 return mbim_bind_config(c, 0);
697}
698
699static struct android_usb_function mbim_function = {
700 .name = "usb_mbim",
701 .cleanup = mbim_function_cleanup,
702 .bind_config = mbim_function_bind_config,
703 .init = mbim_function_init,
704};
705
706
Manu Gautam8e0719b2011-09-26 14:47:55 +0530707/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530708static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709static ssize_t clients_store(
710 struct device *device, struct device_attribute *attr,
711 const char *buff, size_t size)
712{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530713 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714
715 return size;
716}
717
718static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
719static struct device_attribute *diag_function_attributes[] =
720 { &dev_attr_clients, NULL };
721
722static int diag_function_init(struct android_usb_function *f,
723 struct usb_composite_dev *cdev)
724{
725 return diag_setup();
726}
727
728static void diag_function_cleanup(struct android_usb_function *f)
729{
730 diag_cleanup();
731}
732
733static int diag_function_bind_config(struct android_usb_function *f,
734 struct usb_configuration *c)
735{
736 char *name;
737 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530738 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800739 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300740 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530742 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 b = strim(buf);
744
745 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800746 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530748 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300749 if (dev->pdata && !once++)
750 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530753 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 if (err)
755 pr_err("diag: Cannot open channel '%s'", name);
756 }
757 }
758
759 return err;
760}
761
762static struct android_usb_function diag_function = {
763 .name = "diag",
764 .init = diag_function_init,
765 .cleanup = diag_function_cleanup,
766 .bind_config = diag_function_bind_config,
767 .attributes = diag_function_attributes,
768};
769
Shimrit Malichia00d7322012-08-05 13:56:28 +0300770/* DEBUG */
771static int qdss_function_init(struct android_usb_function *f,
772 struct usb_composite_dev *cdev)
773{
774 return qdss_setup();
775}
776
777static void qdss_function_cleanup(struct android_usb_function *f)
778{
779 qdss_cleanup();
780}
781
782static int qdss_function_bind_config(struct android_usb_function *f,
783 struct usb_configuration *c)
784{
785 int err = -1;
786
787 err = qdss_bind_config(c, "qdss");
788 if (err)
789 pr_err("qdss: Cannot open channel qdss");
790
791 return err;
792}
793
794static struct android_usb_function qdss_function = {
795 .name = "qdss",
796 .init = qdss_function_init,
797 .cleanup = qdss_function_cleanup,
798 .bind_config = qdss_function_bind_config,
799};
800
Manu Gautam8e0719b2011-09-26 14:47:55 +0530801/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530802static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530803static ssize_t serial_transports_store(
804 struct device *device, struct device_attribute *attr,
805 const char *buff, size_t size)
806{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530807 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530808
809 return size;
810}
811
812static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
813static struct device_attribute *serial_function_attributes[] =
814 { &dev_attr_transports, NULL };
815
816static void serial_function_cleanup(struct android_usb_function *f)
817{
818 gserial_cleanup();
819}
820
821static int serial_function_bind_config(struct android_usb_function *f,
822 struct usb_configuration *c)
823{
824 char *name;
825 char buf[32], *b;
826 int err = -1, i;
827 static int serial_initialized = 0, ports = 0;
828
829 if (serial_initialized)
830 goto bind_config;
831
832 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530833 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530834 b = strim(buf);
835
836 while (b) {
837 name = strsep(&b, ",");
838
839 if (name) {
840 err = gserial_init_port(ports, name);
841 if (err) {
842 pr_err("serial: Cannot open port '%s'", name);
843 goto out;
844 }
845 ports++;
846 }
847 }
848 err = gport_setup(c);
849 if (err) {
850 pr_err("serial: Cannot setup transports");
851 goto out;
852 }
853
854bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200855 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530856 err = gser_bind_config(c, i);
857 if (err) {
858 pr_err("serial: bind_config failed for port %d", i);
859 goto out;
860 }
861 }
862
863out:
864 return err;
865}
866
867static struct android_usb_function serial_function = {
868 .name = "serial",
869 .cleanup = serial_function_cleanup,
870 .bind_config = serial_function_bind_config,
871 .attributes = serial_function_attributes,
872};
873
Anji jonnala92be1b42011-12-19 09:44:41 +0530874/* ACM */
875static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
876static ssize_t acm_transports_store(
877 struct device *device, struct device_attribute *attr,
878 const char *buff, size_t size)
879{
880 strlcpy(acm_transports, buff, sizeof(acm_transports));
881
882 return size;
883}
884
885static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
886static struct device_attribute *acm_function_attributes[] = {
887 &dev_attr_acm_transports, NULL };
888
Benoit Goby1e8ce152011-12-12 13:01:23 -0800889static void acm_function_cleanup(struct android_usb_function *f)
890{
891 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800892}
893
Anji jonnala92be1b42011-12-19 09:44:41 +0530894static int acm_function_bind_config(struct android_usb_function *f,
895 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800896{
Anji jonnala92be1b42011-12-19 09:44:41 +0530897 char *name;
898 char buf[32], *b;
899 int err = -1, i;
900 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800901
Anji jonnala92be1b42011-12-19 09:44:41 +0530902 if (acm_initialized)
903 goto bind_config;
904
905 acm_initialized = 1;
906 strlcpy(buf, acm_transports, sizeof(buf));
907 b = strim(buf);
908
909 while (b) {
910 name = strsep(&b, ",");
911
912 if (name) {
913 err = acm_init_port(ports, name);
914 if (err) {
915 pr_err("acm: Cannot open port '%s'", name);
916 goto out;
917 }
918 ports++;
919 }
920 }
921 err = acm_port_setup(c);
922 if (err) {
923 pr_err("acm: Cannot setup transports");
924 goto out;
925 }
926
927bind_config:
928 for (i = 0; i < ports; i++) {
929 err = acm_bind_config(c, i);
930 if (err) {
931 pr_err("acm: bind_config failed for port %d", i);
932 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800933 }
934 }
935
Anji jonnala92be1b42011-12-19 09:44:41 +0530936out:
937 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800938}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800939static struct android_usb_function acm_function = {
940 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800941 .cleanup = acm_function_cleanup,
942 .bind_config = acm_function_bind_config,
943 .attributes = acm_function_attributes,
944};
945
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530946/* CCID */
947static int ccid_function_init(struct android_usb_function *f,
948 struct usb_composite_dev *cdev)
949{
950 return ccid_setup();
951}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800952
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530953static void ccid_function_cleanup(struct android_usb_function *f)
954{
955 ccid_cleanup();
956}
957
958static int ccid_function_bind_config(struct android_usb_function *f,
959 struct usb_configuration *c)
960{
961 return ccid_bind_config(c);
962}
963
964static struct android_usb_function ccid_function = {
965 .name = "ccid",
966 .init = ccid_function_init,
967 .cleanup = ccid_function_cleanup,
968 .bind_config = ccid_function_bind_config,
969};
970
Steve Mucklef132c6c2012-06-06 18:30:57 -0700971static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800972 struct usb_composite_dev *cdev)
973{
974 return mtp_setup();
975}
976
977static void mtp_function_cleanup(struct android_usb_function *f)
978{
979 mtp_cleanup();
980}
981
Steve Mucklef132c6c2012-06-06 18:30:57 -0700982static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800983 struct usb_configuration *c)
984{
985 return mtp_bind_config(c, false);
986}
987
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400988static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800989{
990 /* nothing to do - initialization is handled by mtp_function_init */
991 return 0;
992}
993
994static void ptp_function_cleanup(struct android_usb_function *f)
995{
996 /* nothing to do - cleanup is handled by mtp_function_cleanup */
997}
998
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400999static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001000{
1001 return mtp_bind_config(c, true);
1002}
1003
1004static int mtp_function_ctrlrequest(struct android_usb_function *f,
1005 struct usb_composite_dev *cdev,
1006 const struct usb_ctrlrequest *c)
1007{
1008 return mtp_ctrlrequest(cdev, c);
1009}
1010
1011static struct android_usb_function mtp_function = {
1012 .name = "mtp",
1013 .init = mtp_function_init,
1014 .cleanup = mtp_function_cleanup,
1015 .bind_config = mtp_function_bind_config,
1016 .ctrlrequest = mtp_function_ctrlrequest,
1017};
1018
1019/* PTP function is same as MTP with slightly different interface descriptor */
1020static struct android_usb_function ptp_function = {
1021 .name = "ptp",
1022 .init = ptp_function_init,
1023 .cleanup = ptp_function_cleanup,
1024 .bind_config = ptp_function_bind_config,
1025};
1026
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001027#ifdef CONFIG_USB_ANDROID_CDC_ECM
1028struct ecm_function_config {
1029 u8 ethaddr[ETH_ALEN];
1030 u32 vendorID;
1031 char manufacturer[256];
1032};
1033
1034static int ecm_function_init(struct android_usb_function *f,
1035 struct usb_composite_dev *cdev)
1036{
1037 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
1038 if (!f->config)
1039 return -ENOMEM;
1040 return 0;
1041}
1042
1043static void ecm_function_cleanup(struct android_usb_function *f)
1044{
1045 kfree(f->config);
1046 f->config = NULL;
1047}
1048
1049static int ecm_function_bind_config(struct android_usb_function *f,
1050 struct usb_configuration *c)
1051{
1052 int ret;
1053 struct ecm_function_config *ecm = f->config;
1054
1055 if (!ecm) {
1056 pr_err("%s: ecm_function_config\n", __func__);
1057 return -1;
1058 }
1059
1060 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1061 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1062 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1063
1064 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usb");
1065 if (ret) {
1066 pr_err("%s: gether_setup failed\n", __func__);
1067 return ret;
1068 }
1069
1070 return ecm_bind_config(c, ecm->ethaddr);
1071}
1072
1073static void ecm_function_unbind_config(struct android_usb_function *f,
1074 struct usb_configuration *c)
1075{
1076 gether_cleanup();
1077}
1078
1079static ssize_t ecm_manufacturer_show(struct device *dev,
1080 struct device_attribute *attr, char *buf)
1081{
1082 struct android_usb_function *f = dev_get_drvdata(dev);
1083 struct ecm_function_config *config = f->config;
1084
1085 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
1086}
1087
1088static ssize_t ecm_manufacturer_store(struct device *dev,
1089 struct device_attribute *attr, const char *buf, size_t size)
1090{
1091 struct android_usb_function *f = dev_get_drvdata(dev);
1092 struct ecm_function_config *config = f->config;
1093
1094 if (size >= sizeof(config->manufacturer))
1095 return -EINVAL;
1096
1097 if (sscanf(buf, "%255s", config->manufacturer) == 1)
1098 return size;
1099 return -1;
1100}
1101
1102static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, ecm_manufacturer_show,
1103 ecm_manufacturer_store);
1104
1105static ssize_t ecm_ethaddr_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1107{
1108 struct android_usb_function *f = dev_get_drvdata(dev);
1109 struct ecm_function_config *ecm = f->config;
1110 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1111 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1112 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1113}
1114
1115static ssize_t ecm_ethaddr_store(struct device *dev,
1116 struct device_attribute *attr, const char *buf, size_t size)
1117{
1118 struct android_usb_function *f = dev_get_drvdata(dev);
1119 struct ecm_function_config *ecm = f->config;
1120
1121 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1122 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1123 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1124 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1125 return size;
1126 return -EINVAL;
1127}
1128
1129static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1130 ecm_ethaddr_store);
1131
1132static ssize_t ecm_vendorID_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1134{
1135 struct android_usb_function *f = dev_get_drvdata(dev);
1136 struct ecm_function_config *config = f->config;
1137
1138 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
1139}
1140
1141static ssize_t ecm_vendorID_store(struct device *dev,
1142 struct device_attribute *attr, const char *buf, size_t size)
1143{
1144 struct android_usb_function *f = dev_get_drvdata(dev);
1145 struct ecm_function_config *config = f->config;
1146 int value;
1147
1148 if (sscanf(buf, "%04x", &value) == 1) {
1149 config->vendorID = value;
1150 return size;
1151 }
1152 return -EINVAL;
1153}
1154
1155static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, ecm_vendorID_show,
1156 ecm_vendorID_store);
1157
1158static struct device_attribute *ecm_function_attributes[] = {
1159 &dev_attr_manufacturer,
1160 &dev_attr_ethaddr,
1161 &dev_attr_vendorID,
1162 NULL
1163};
1164
1165static struct android_usb_function ecm_function = {
1166 .name = "ecm",
1167 .init = ecm_function_init,
1168 .cleanup = ecm_function_cleanup,
1169 .bind_config = ecm_function_bind_config,
1170 .unbind_config = ecm_function_unbind_config,
1171 .attributes = ecm_function_attributes,
1172};
1173
1174#else
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001175
Benoit Goby1e8ce152011-12-12 13:01:23 -08001176struct rndis_function_config {
1177 u8 ethaddr[ETH_ALEN];
1178 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001179 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001180 char manufacturer[256];
1181 /* "Wireless" RNDIS; auto-detected by Windows */
1182 bool wceis;
1183};
1184
1185static int
1186rndis_function_init(struct android_usb_function *f,
1187 struct usb_composite_dev *cdev)
1188{
1189 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1190 if (!f->config)
1191 return -ENOMEM;
1192 return 0;
1193}
1194
1195static void rndis_function_cleanup(struct android_usb_function *f)
1196{
1197 kfree(f->config);
1198 f->config = NULL;
1199}
1200
Ofir Cohenaef90b72012-07-31 12:37:04 +02001201static int rndis_qc_function_init(struct android_usb_function *f,
1202 struct usb_composite_dev *cdev)
1203{
1204 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1205 if (!f->config)
1206 return -ENOMEM;
1207
1208 return rndis_qc_init();
1209}
1210
1211static void rndis_qc_function_cleanup(struct android_usb_function *f)
1212{
1213 rndis_qc_cleanup();
1214 kfree(f->config);
1215}
1216
Benoit Goby1e8ce152011-12-12 13:01:23 -08001217static int
1218rndis_function_bind_config(struct android_usb_function *f,
1219 struct usb_configuration *c)
1220{
1221 int ret;
1222 struct rndis_function_config *rndis = f->config;
1223
1224 if (!rndis) {
1225 pr_err("%s: rndis_pdata\n", __func__);
1226 return -1;
1227 }
1228
1229 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1230 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1231 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1232
Devin Kim78e07192012-07-17 08:40:54 -07001233 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "usb");
Benoit Goby1e8ce152011-12-12 13:01:23 -08001234 if (ret) {
1235 pr_err("%s: gether_setup failed\n", __func__);
1236 return ret;
1237 }
1238
1239 if (rndis->wceis) {
1240 /* "Wireless" RNDIS; auto-detected by Windows */
1241 rndis_iad_descriptor.bFunctionClass =
1242 USB_CLASS_WIRELESS_CONTROLLER;
1243 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1244 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1245 rndis_control_intf.bInterfaceClass =
1246 USB_CLASS_WIRELESS_CONTROLLER;
1247 rndis_control_intf.bInterfaceSubClass = 0x01;
1248 rndis_control_intf.bInterfaceProtocol = 0x03;
1249 }
1250
1251 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1252 rndis->manufacturer);
1253}
1254
Ofir Cohenaef90b72012-07-31 12:37:04 +02001255static int rndis_qc_function_bind_config(struct android_usb_function *f,
1256 struct usb_configuration *c)
1257{
1258 int ret;
1259 struct rndis_function_config *rndis = f->config;
1260
1261 if (!rndis) {
1262 pr_err("%s: rndis_pdata\n", __func__);
1263 return -EINVAL;
1264 }
1265
1266 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1267 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1268 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1269
1270 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1271 if (ret) {
1272 pr_err("%s: gether_setup failed\n", __func__);
1273 return ret;
1274 }
1275
1276 if (rndis->wceis) {
1277 /* "Wireless" RNDIS; auto-detected by Windows */
1278 rndis_qc_iad_descriptor.bFunctionClass =
1279 USB_CLASS_WIRELESS_CONTROLLER;
1280 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1281 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1282 rndis_qc_control_intf.bInterfaceClass =
1283 USB_CLASS_WIRELESS_CONTROLLER;
1284 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1285 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1286 }
1287
1288 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1289 rndis->manufacturer,
1290 rndis->max_pkt_per_xfer);
1291}
1292
Benoit Goby1e8ce152011-12-12 13:01:23 -08001293static void rndis_function_unbind_config(struct android_usb_function *f,
1294 struct usb_configuration *c)
1295{
1296 gether_cleanup();
1297}
1298
Ofir Cohenaef90b72012-07-31 12:37:04 +02001299static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1300 struct usb_configuration *c)
1301{
1302 gether_qc_cleanup();
1303}
1304
Benoit Goby1e8ce152011-12-12 13:01:23 -08001305static ssize_t rndis_manufacturer_show(struct device *dev,
1306 struct device_attribute *attr, char *buf)
1307{
1308 struct android_usb_function *f = dev_get_drvdata(dev);
1309 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001310
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301311 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001312}
1313
1314static ssize_t rndis_manufacturer_store(struct device *dev,
1315 struct device_attribute *attr, const char *buf, size_t size)
1316{
1317 struct android_usb_function *f = dev_get_drvdata(dev);
1318 struct rndis_function_config *config = f->config;
1319
1320 if (size >= sizeof(config->manufacturer))
1321 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001322
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301323 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001324 return size;
1325 return -1;
1326}
1327
1328static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1329 rndis_manufacturer_store);
1330
1331static ssize_t rndis_wceis_show(struct device *dev,
1332 struct device_attribute *attr, char *buf)
1333{
1334 struct android_usb_function *f = dev_get_drvdata(dev);
1335 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001336
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301337 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001338}
1339
1340static ssize_t rndis_wceis_store(struct device *dev,
1341 struct device_attribute *attr, const char *buf, size_t size)
1342{
1343 struct android_usb_function *f = dev_get_drvdata(dev);
1344 struct rndis_function_config *config = f->config;
1345 int value;
1346
1347 if (sscanf(buf, "%d", &value) == 1) {
1348 config->wceis = value;
1349 return size;
1350 }
1351 return -EINVAL;
1352}
1353
1354static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1355 rndis_wceis_store);
1356
1357static ssize_t rndis_ethaddr_show(struct device *dev,
1358 struct device_attribute *attr, char *buf)
1359{
1360 struct android_usb_function *f = dev_get_drvdata(dev);
1361 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001362
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301363 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001364 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1365 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1366}
1367
1368static ssize_t rndis_ethaddr_store(struct device *dev,
1369 struct device_attribute *attr, const char *buf, size_t size)
1370{
1371 struct android_usb_function *f = dev_get_drvdata(dev);
1372 struct rndis_function_config *rndis = f->config;
1373
1374 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1375 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1376 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1377 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1378 return size;
1379 return -EINVAL;
1380}
1381
1382static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1383 rndis_ethaddr_store);
1384
1385static ssize_t rndis_vendorID_show(struct device *dev,
1386 struct device_attribute *attr, char *buf)
1387{
1388 struct android_usb_function *f = dev_get_drvdata(dev);
1389 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001390
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301391 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001392}
1393
1394static ssize_t rndis_vendorID_store(struct device *dev,
1395 struct device_attribute *attr, const char *buf, size_t size)
1396{
1397 struct android_usb_function *f = dev_get_drvdata(dev);
1398 struct rndis_function_config *config = f->config;
1399 int value;
1400
1401 if (sscanf(buf, "%04x", &value) == 1) {
1402 config->vendorID = value;
1403 return size;
1404 }
1405 return -EINVAL;
1406}
1407
1408static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1409 rndis_vendorID_store);
1410
Ofir Cohenaef90b72012-07-31 12:37:04 +02001411static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1412 struct device_attribute *attr, char *buf)
1413{
1414 struct android_usb_function *f = dev_get_drvdata(dev);
1415 struct rndis_function_config *config = f->config;
1416 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1417}
1418
1419static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1420 struct device_attribute *attr, const char *buf, size_t size)
1421{
1422 struct android_usb_function *f = dev_get_drvdata(dev);
1423 struct rndis_function_config *config = f->config;
1424 int value;
1425
1426 if (sscanf(buf, "%d", &value) == 1) {
1427 config->max_pkt_per_xfer = value;
1428 return size;
1429 }
1430 return -EINVAL;
1431}
1432
1433static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1434 rndis_max_pkt_per_xfer_show,
1435 rndis_max_pkt_per_xfer_store);
1436
Benoit Goby1e8ce152011-12-12 13:01:23 -08001437static struct device_attribute *rndis_function_attributes[] = {
1438 &dev_attr_manufacturer,
1439 &dev_attr_wceis,
1440 &dev_attr_ethaddr,
1441 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001442 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001443 NULL
1444};
1445
1446static struct android_usb_function rndis_function = {
1447 .name = "rndis",
1448 .init = rndis_function_init,
1449 .cleanup = rndis_function_cleanup,
1450 .bind_config = rndis_function_bind_config,
1451 .unbind_config = rndis_function_unbind_config,
1452 .attributes = rndis_function_attributes,
1453};
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001454#endif /* CONFIG_USB_ANDROID_CDC_ECM */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001455
Ofir Cohenaef90b72012-07-31 12:37:04 +02001456static struct android_usb_function rndis_qc_function = {
1457 .name = "rndis_qc",
1458 .init = rndis_qc_function_init,
1459 .cleanup = rndis_qc_function_cleanup,
1460 .bind_config = rndis_qc_function_bind_config,
1461 .unbind_config = rndis_qc_function_unbind_config,
1462 .attributes = rndis_function_attributes,
1463};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001464
1465struct mass_storage_function_config {
1466 struct fsg_config fsg;
1467 struct fsg_common *common;
1468};
1469
1470static int mass_storage_function_init(struct android_usb_function *f,
1471 struct usb_composite_dev *cdev)
1472{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001473 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001474 struct mass_storage_function_config *config;
1475 struct fsg_common *common;
1476 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301477 int i;
1478 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001479
1480 config = kzalloc(sizeof(struct mass_storage_function_config),
1481 GFP_KERNEL);
1482 if (!config)
1483 return -ENOMEM;
1484
1485 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301486 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301487 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301488 config->fsg.nluns = 2;
1489 config->fsg.luns[1].cdrom = 1;
1490 config->fsg.luns[1].ro = 1;
1491 config->fsg.luns[1].removable = 1;
1492 name[1] = "lun0";
1493 }
1494
Benoit Goby1e8ce152011-12-12 13:01:23 -08001495 config->fsg.luns[0].removable = 1;
1496
1497 common = fsg_common_init(NULL, cdev, &config->fsg);
1498 if (IS_ERR(common)) {
1499 kfree(config);
1500 return PTR_ERR(common);
1501 }
1502
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301503 for (i = 0; i < config->fsg.nluns; i++) {
1504 err = sysfs_create_link(&f->dev->kobj,
1505 &common->luns[i].dev.kobj,
1506 name[i]);
1507 if (err)
1508 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001509 }
1510
1511 config->common = common;
1512 f->config = config;
1513 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301514error:
1515 for (; i > 0 ; i--)
1516 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1517
1518 fsg_common_release(&common->ref);
1519 kfree(config);
1520 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001521}
1522
1523static void mass_storage_function_cleanup(struct android_usb_function *f)
1524{
1525 kfree(f->config);
1526 f->config = NULL;
1527}
1528
1529static int mass_storage_function_bind_config(struct android_usb_function *f,
1530 struct usb_configuration *c)
1531{
1532 struct mass_storage_function_config *config = f->config;
1533 return fsg_bind_config(c->cdev, c, config->common);
1534}
1535
1536static ssize_t mass_storage_inquiry_show(struct device *dev,
1537 struct device_attribute *attr, char *buf)
1538{
1539 struct android_usb_function *f = dev_get_drvdata(dev);
1540 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301541 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001542}
1543
1544static ssize_t mass_storage_inquiry_store(struct device *dev,
1545 struct device_attribute *attr, const char *buf, size_t size)
1546{
1547 struct android_usb_function *f = dev_get_drvdata(dev);
1548 struct mass_storage_function_config *config = f->config;
1549 if (size >= sizeof(config->common->inquiry_string))
1550 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301551 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001552 return -EINVAL;
1553 return size;
1554}
1555
1556static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1557 mass_storage_inquiry_show,
1558 mass_storage_inquiry_store);
1559
1560static struct device_attribute *mass_storage_function_attributes[] = {
1561 &dev_attr_inquiry_string,
1562 NULL
1563};
1564
1565static struct android_usb_function mass_storage_function = {
1566 .name = "mass_storage",
1567 .init = mass_storage_function_init,
1568 .cleanup = mass_storage_function_cleanup,
1569 .bind_config = mass_storage_function_bind_config,
1570 .attributes = mass_storage_function_attributes,
1571};
1572
1573
Benoit Gobycf3fc062011-12-19 14:39:37 -08001574static int accessory_function_init(struct android_usb_function *f,
1575 struct usb_composite_dev *cdev)
1576{
1577 return acc_setup();
1578}
1579
1580static void accessory_function_cleanup(struct android_usb_function *f)
1581{
1582 acc_cleanup();
1583}
1584
1585static int accessory_function_bind_config(struct android_usb_function *f,
1586 struct usb_configuration *c)
1587{
1588 return acc_bind_config(c);
1589}
1590
1591static int accessory_function_ctrlrequest(struct android_usb_function *f,
1592 struct usb_composite_dev *cdev,
1593 const struct usb_ctrlrequest *c)
1594{
1595 return acc_ctrlrequest(cdev, c);
1596}
1597
1598static struct android_usb_function accessory_function = {
1599 .name = "accessory",
1600 .init = accessory_function_init,
1601 .cleanup = accessory_function_cleanup,
1602 .bind_config = accessory_function_bind_config,
1603 .ctrlrequest = accessory_function_ctrlrequest,
1604};
1605
Ajay Dudani34b1e302012-08-27 16:43:53 +05301606static int audio_source_function_init(struct android_usb_function *f,
1607 struct usb_composite_dev *cdev)
1608{
1609 struct audio_source_config *config;
1610
1611 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1612 if (!config)
1613 return -ENOMEM;
1614 config->card = -1;
1615 config->device = -1;
1616 f->config = config;
1617 return 0;
1618}
1619
1620static void audio_source_function_cleanup(struct android_usb_function *f)
1621{
1622 kfree(f->config);
1623}
1624
1625static int audio_source_function_bind_config(struct android_usb_function *f,
1626 struct usb_configuration *c)
1627{
1628 struct audio_source_config *config = f->config;
1629
1630 return audio_source_bind_config(c, config);
1631}
1632
1633static void audio_source_function_unbind_config(struct android_usb_function *f,
1634 struct usb_configuration *c)
1635{
1636 struct audio_source_config *config = f->config;
1637
1638 config->card = -1;
1639 config->device = -1;
1640}
1641
1642static ssize_t audio_source_pcm_show(struct device *dev,
1643 struct device_attribute *attr, char *buf)
1644{
1645 struct android_usb_function *f = dev_get_drvdata(dev);
1646 struct audio_source_config *config = f->config;
1647
1648 /* print PCM card and device numbers */
1649 return sprintf(buf, "%d %d\n", config->card, config->device);
1650}
1651
1652static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1653
1654static struct device_attribute *audio_source_function_attributes[] = {
1655 &dev_attr_pcm,
1656 NULL
1657};
1658
1659static struct android_usb_function audio_source_function = {
1660 .name = "audio_source",
1661 .init = audio_source_function_init,
1662 .cleanup = audio_source_function_cleanup,
1663 .bind_config = audio_source_function_bind_config,
1664 .unbind_config = audio_source_function_unbind_config,
1665 .attributes = audio_source_function_attributes,
1666};
1667
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301668static int android_uasp_connect_cb(bool connect)
1669{
1670 /*
1671 * TODO
1672 * We may have to disable gadget till UASP configfs nodes
1673 * are configured which includes mapping LUN with the
1674 * backing file. It is a fundamental difference between
1675 * f_mass_storage and f_tcp. That means UASP can not be
1676 * in default composition.
1677 *
1678 * For now, assume that UASP configfs nodes are configured
1679 * before enabling android gadget. Or cable should be
1680 * reconnected after mapping the LUN.
1681 *
1682 * Also consider making UASP to respond to Host requests when
1683 * Lun is not mapped.
1684 */
1685 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1686
1687 return 0;
1688}
1689
1690static int uasp_function_init(struct android_usb_function *f,
1691 struct usb_composite_dev *cdev)
1692{
1693 return f_tcm_init(&android_uasp_connect_cb);
1694}
1695
1696static void uasp_function_cleanup(struct android_usb_function *f)
1697{
1698 f_tcm_exit();
1699}
1700
1701static int uasp_function_bind_config(struct android_usb_function *f,
1702 struct usb_configuration *c)
1703{
1704 return tcm_bind_config(c);
1705}
1706
1707static struct android_usb_function uasp_function = {
1708 .name = "uasp",
1709 .init = uasp_function_init,
1710 .cleanup = uasp_function_cleanup,
1711 .bind_config = uasp_function_bind_config,
1712};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001713
Benoit Goby1e8ce152011-12-12 13:01:23 -08001714static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001715 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001716 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301717 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301718 &rmnet_sdio_function,
1719 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301720 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001722 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301723 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001724 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301725 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001726 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001727 &mtp_function,
1728 &ptp_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001729#ifdef CONFIG_USB_ANDROID_CDC_ECM
1730 &ecm_function,
1731#else
Benoit Goby1e8ce152011-12-12 13:01:23 -08001732 &rndis_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001733#endif
Ofir Cohenaef90b72012-07-31 12:37:04 +02001734 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001735 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001736 &accessory_function,
Ajay Dudani34b1e302012-08-27 16:43:53 +05301737 &audio_source_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301738 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001739 NULL
1740};
1741
Lena Salmand092f2d2012-03-12 17:27:24 +02001742static void android_cleanup_functions(struct android_usb_function **functions)
1743{
1744 struct android_usb_function *f;
1745 struct device_attribute **attrs;
1746 struct device_attribute *attr;
1747
1748 while (*functions) {
1749 f = *functions++;
1750
1751 if (f->dev) {
1752 device_destroy(android_class, f->dev->devt);
1753 kfree(f->dev_name);
1754 } else
1755 continue;
1756
1757 if (f->cleanup)
1758 f->cleanup(f);
1759
1760 attrs = f->attributes;
1761 if (attrs) {
1762 while ((attr = *attrs++))
1763 device_remove_file(f->dev, attr);
1764 }
1765 }
1766}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001767
1768static int android_init_functions(struct android_usb_function **functions,
1769 struct usb_composite_dev *cdev)
1770{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001771 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001772 struct android_usb_function *f;
1773 struct device_attribute **attrs;
1774 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301775 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001776 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001777
1778 for (; (f = *functions++); index++) {
1779 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001780 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001781 if (!f->dev_name) {
1782 err = -ENOMEM;
1783 goto err_out;
1784 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001785 f->dev = device_create(android_class, dev->dev,
1786 MKDEV(0, index), f, f->dev_name);
1787 if (IS_ERR(f->dev)) {
1788 pr_err("%s: Failed to create dev %s", __func__,
1789 f->dev_name);
1790 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001791 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001792 goto err_create;
1793 }
1794
1795 if (f->init) {
1796 err = f->init(f, cdev);
1797 if (err) {
1798 pr_err("%s: Failed to init %s", __func__,
1799 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001800 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001801 }
1802 }
1803
1804 attrs = f->attributes;
1805 if (attrs) {
1806 while ((attr = *attrs++) && !err)
1807 err = device_create_file(f->dev, attr);
1808 }
1809 if (err) {
1810 pr_err("%s: Failed to create function %s attributes",
1811 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001812 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001813 }
1814 }
1815 return 0;
1816
Lena Salmand092f2d2012-03-12 17:27:24 +02001817err_attrs:
1818 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1819 device_remove_file(f->dev, attr);
1820 if (f->cleanup)
1821 f->cleanup(f);
1822err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001823 device_destroy(android_class, f->dev->devt);
1824err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001825 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001826 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001827err_out:
1828 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001829 return err;
1830}
1831
Benoit Goby1e8ce152011-12-12 13:01:23 -08001832static int
1833android_bind_enabled_functions(struct android_dev *dev,
1834 struct usb_configuration *c)
1835{
1836 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001837 struct android_configuration *conf =
1838 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001839 int ret;
1840
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001841 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001842 ret = f->bind_config(f, c);
1843 if (ret) {
1844 pr_err("%s: %s failed", __func__, f->name);
1845 return ret;
1846 }
1847 }
1848 return 0;
1849}
1850
1851static void
1852android_unbind_enabled_functions(struct android_dev *dev,
1853 struct usb_configuration *c)
1854{
1855 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001856 struct android_configuration *conf =
1857 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001858
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001859 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001860 if (f->unbind_config)
1861 f->unbind_config(f, c);
1862 }
1863}
1864
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001865static int android_enable_function(struct android_dev *dev,
1866 struct android_configuration *conf,
1867 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001868{
1869 struct android_usb_function **functions = dev->functions;
1870 struct android_usb_function *f;
1871 while ((f = *functions++)) {
1872 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001873 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001874 pr_err("%s already enabled in other " \
1875 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001876 f->name);
1877 else {
1878 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001879 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001880 f->android_dev = dev;
1881 return 0;
1882 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001883 }
1884 }
1885 return -EINVAL;
1886}
1887
1888/*-------------------------------------------------------------------------*/
1889/* /sys/class/android_usb/android%d/ interface */
1890
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301891static ssize_t remote_wakeup_show(struct device *pdev,
1892 struct device_attribute *attr, char *buf)
1893{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001894 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001895 struct android_configuration *conf;
1896
1897 /*
1898 * Show the wakeup attribute of the first configuration,
1899 * since all configurations have the same wakeup attribute
1900 */
1901 if (dev->configs_num == 0)
1902 return 0;
1903 conf = list_entry(dev->configs.next,
1904 struct android_configuration,
1905 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001906
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301907 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001908 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301909 USB_CONFIG_ATT_WAKEUP));
1910}
1911
1912static ssize_t remote_wakeup_store(struct device *pdev,
1913 struct device_attribute *attr, const char *buff, size_t size)
1914{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001915 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001916 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301917 int enable = 0;
1918
1919 sscanf(buff, "%d", &enable);
1920
1921 pr_debug("android_usb: %s remote wakeup\n",
1922 enable ? "enabling" : "disabling");
1923
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001924 list_for_each_entry(conf, &dev->configs, list_item)
1925 if (enable)
1926 conf->usb_config.bmAttributes |=
1927 USB_CONFIG_ATT_WAKEUP;
1928 else
1929 conf->usb_config.bmAttributes &=
1930 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301931
1932 return size;
1933}
1934
Benoit Goby1e8ce152011-12-12 13:01:23 -08001935static ssize_t
1936functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1937{
1938 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001939 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001940 struct android_usb_function *f;
1941 char *buff = buf;
1942
1943 mutex_lock(&dev->mutex);
1944
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001945 list_for_each_entry(conf, &dev->configs, list_item) {
1946 if (buff != buf)
1947 *(buff-1) = ':';
1948 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1949 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
1950 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001951
1952 mutex_unlock(&dev->mutex);
1953
1954 if (buff != buf)
1955 *(buff-1) = '\n';
1956 return buff - buf;
1957}
1958
1959static ssize_t
1960functions_store(struct device *pdev, struct device_attribute *attr,
1961 const char *buff, size_t size)
1962{
1963 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001964 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001965 struct list_head *curr_conf = &dev->configs;
1966 struct android_configuration *conf;
1967 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001968 char *name;
1969 char buf[256], *b;
1970 int err;
1971
1972 mutex_lock(&dev->mutex);
1973
1974 if (dev->enabled) {
1975 mutex_unlock(&dev->mutex);
1976 return -EBUSY;
1977 }
1978
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001979 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001980 list_for_each_entry(conf, &dev->configs, list_item) {
1981 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1982 f->android_dev = NULL;
1983 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001984 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001985
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301986 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001987 b = strim(buf);
1988
1989 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001990 conf_str = strsep(&b, ":");
1991 if (conf_str) {
1992 /* If the next not equal to the head, take it */
1993 if (curr_conf->next != &dev->configs)
1994 conf = list_entry(curr_conf->next,
1995 struct android_configuration,
1996 list_item);
1997 else
1998 conf = alloc_android_config(dev);
1999
2000 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002001 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002002
2003 while (conf_str) {
2004 name = strsep(&conf_str, ",");
2005 if (name) {
2006 err = android_enable_function(dev, conf, name);
2007 if (err)
2008 pr_err("android_usb: Cannot enable %s",
2009 name);
2010 }
2011 }
2012 }
2013
2014 /* Free uneeded configurations if exists */
2015 while (curr_conf->next != &dev->configs) {
2016 conf = list_entry(curr_conf->next,
2017 struct android_configuration, list_item);
2018 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002019 }
2020
2021 mutex_unlock(&dev->mutex);
2022
2023 return size;
2024}
2025
2026static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2027 char *buf)
2028{
2029 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002030
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302031 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002032}
2033
2034static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2035 const char *buff, size_t size)
2036{
2037 struct android_dev *dev = dev_get_drvdata(pdev);
2038 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07002039 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002040 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002041 int enabled = 0;
2042
Benoit Gobycf3fc062011-12-19 14:39:37 -08002043 if (!cdev)
2044 return -ENODEV;
2045
Benoit Goby1e8ce152011-12-12 13:01:23 -08002046 mutex_lock(&dev->mutex);
2047
2048 sscanf(buff, "%d", &enabled);
2049 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002050 /*
2051 * Update values in composite driver's copy of
2052 * device descriptor.
2053 */
2054 cdev->desc.idVendor = device_desc.idVendor;
2055 cdev->desc.idProduct = device_desc.idProduct;
2056 cdev->desc.bcdDevice = device_desc.bcdDevice;
2057 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2058 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2059 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002060 list_for_each_entry(conf, &dev->configs, list_item)
2061 list_for_each_entry(f, &conf->enabled_functions,
2062 enabled_list) {
2063 if (f->enable)
2064 f->enable(f);
2065 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07002066 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002067 dev->enabled = true;
2068 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002069 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002070 list_for_each_entry(conf, &dev->configs, list_item)
2071 list_for_each_entry(f, &conf->enabled_functions,
2072 enabled_list) {
2073 if (f->disable)
2074 f->disable(f);
2075 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002076 dev->enabled = false;
2077 } else {
2078 pr_err("android_usb: already %s\n",
2079 dev->enabled ? "enabled" : "disabled");
2080 }
2081
2082 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002083
Benoit Gobyaab96812011-04-19 20:37:33 -07002084 return size;
2085}
2086
Ofir Cohen94213a72012-05-03 14:26:32 +03002087static ssize_t pm_qos_show(struct device *pdev,
2088 struct device_attribute *attr, char *buf)
2089{
2090 struct android_dev *dev = dev_get_drvdata(pdev);
2091
2092 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2093}
2094
2095static ssize_t pm_qos_store(struct device *pdev,
2096 struct device_attribute *attr,
2097 const char *buff, size_t size)
2098{
2099 struct android_dev *dev = dev_get_drvdata(pdev);
2100
2101 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2102
Benoit Goby1e8ce152011-12-12 13:01:23 -08002103 return size;
2104}
2105
2106static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2107 char *buf)
2108{
2109 struct android_dev *dev = dev_get_drvdata(pdev);
2110 struct usb_composite_dev *cdev = dev->cdev;
2111 char *state = "DISCONNECTED";
2112 unsigned long flags;
2113
2114 if (!cdev)
2115 goto out;
2116
2117 spin_lock_irqsave(&cdev->lock, flags);
2118 if (cdev->config)
2119 state = "CONFIGURED";
2120 else if (dev->connected)
2121 state = "CONNECTED";
2122 spin_unlock_irqrestore(&cdev->lock, flags);
2123out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302124 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002125}
2126
2127#define DESCRIPTOR_ATTR(field, format_string) \
2128static ssize_t \
2129field ## _show(struct device *dev, struct device_attribute *attr, \
2130 char *buf) \
2131{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302132 return snprintf(buf, PAGE_SIZE, \
2133 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002134} \
2135static ssize_t \
2136field ## _store(struct device *dev, struct device_attribute *attr, \
2137 const char *buf, size_t size) \
2138{ \
2139 int value; \
2140 if (sscanf(buf, format_string, &value) == 1) { \
2141 device_desc.field = value; \
2142 return size; \
2143 } \
2144 return -1; \
2145} \
2146static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2147
2148#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2149static ssize_t \
2150field ## _show(struct device *dev, struct device_attribute *attr, \
2151 char *buf) \
2152{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302153 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002154} \
2155static ssize_t \
2156field ## _store(struct device *dev, struct device_attribute *attr, \
2157 const char *buf, size_t size) \
2158{ \
2159 if (size >= sizeof(buffer)) \
2160 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302161 strlcpy(buffer, buf, sizeof(buffer)); \
2162 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302163 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002164} \
2165static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2166
2167
2168DESCRIPTOR_ATTR(idVendor, "%04x\n")
2169DESCRIPTOR_ATTR(idProduct, "%04x\n")
2170DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2171DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2172DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2173DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2174DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2175DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2176DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2177
2178static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2179 functions_store);
2180static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002181static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2182 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002183static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302184static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2185 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002186
2187static struct device_attribute *android_usb_attributes[] = {
2188 &dev_attr_idVendor,
2189 &dev_attr_idProduct,
2190 &dev_attr_bcdDevice,
2191 &dev_attr_bDeviceClass,
2192 &dev_attr_bDeviceSubClass,
2193 &dev_attr_bDeviceProtocol,
2194 &dev_attr_iManufacturer,
2195 &dev_attr_iProduct,
2196 &dev_attr_iSerial,
2197 &dev_attr_functions,
2198 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002199 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002200 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302201 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002202 NULL
2203};
2204
2205/*-------------------------------------------------------------------------*/
2206/* Composite driver */
2207
2208static int android_bind_config(struct usb_configuration *c)
2209{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002210 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002211 int ret = 0;
2212
2213 ret = android_bind_enabled_functions(dev, c);
2214 if (ret)
2215 return ret;
2216
2217 return 0;
2218}
2219
2220static void android_unbind_config(struct usb_configuration *c)
2221{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002222 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002223
2224 android_unbind_enabled_functions(dev, c);
2225}
2226
2227static int android_bind(struct usb_composite_dev *cdev)
2228{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002229 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002230 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002231 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002232 int gcnum, id, ret;
2233
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002234 /* Bind to the last android_dev that was probed */
2235 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2236
2237 dev->cdev = cdev;
2238
Benoit Goby1e8ce152011-12-12 13:01:23 -08002239 /*
2240 * Start disconnected. Userspace will connect the gadget once
2241 * it is done configuring the functions.
2242 */
2243 usb_gadget_disconnect(gadget);
2244
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002245 /* Init the supported functions only once, on the first android_dev */
2246 if (android_dev_count == 1) {
2247 ret = android_init_functions(dev->functions, cdev);
2248 if (ret)
2249 return ret;
2250 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002251
2252 /* Allocate string descriptor numbers ... note that string
2253 * contents can be overridden by the composite_dev glue.
2254 */
2255 id = usb_string_id(cdev);
2256 if (id < 0)
2257 return id;
2258 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2259 device_desc.iManufacturer = id;
2260
2261 id = usb_string_id(cdev);
2262 if (id < 0)
2263 return id;
2264 strings_dev[STRING_PRODUCT_IDX].id = id;
2265 device_desc.iProduct = id;
2266
2267 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302268 strlcpy(manufacturer_string, "Android",
2269 sizeof(manufacturer_string) - 1);
2270 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2271 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002272
2273 id = usb_string_id(cdev);
2274 if (id < 0)
2275 return id;
2276 strings_dev[STRING_SERIAL_IDX].id = id;
2277 device_desc.iSerialNumber = id;
2278
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302279 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002280 list_for_each_entry(conf, &dev->configs, list_item)
2281 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302282
Benoit Goby1e8ce152011-12-12 13:01:23 -08002283 gcnum = usb_gadget_controller_number(gadget);
2284 if (gcnum >= 0)
2285 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2286 else {
2287 pr_warning("%s: controller '%s' not recognized\n",
2288 longname, gadget->name);
2289 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2290 }
2291
Benoit Goby1e8ce152011-12-12 13:01:23 -08002292 return 0;
2293}
2294
2295static int android_usb_unbind(struct usb_composite_dev *cdev)
2296{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002297 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002298
Lena Salmand092f2d2012-03-12 17:27:24 +02002299 manufacturer_string[0] = '\0';
2300 product_string[0] = '\0';
2301 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002302 cancel_work_sync(&dev->work);
2303 android_cleanup_functions(dev->functions);
2304 return 0;
2305}
2306
2307static struct usb_composite_driver android_usb_driver = {
2308 .name = "android_usb",
2309 .dev = &device_desc,
2310 .strings = dev_strings,
2311 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002312 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002313};
2314
2315static int
2316android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2317{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002318 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002319 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002320 struct usb_request *req = cdev->req;
2321 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002322 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002323 int value = -EOPNOTSUPP;
2324 unsigned long flags;
2325
2326 req->zero = 0;
2327 req->complete = composite_setup_complete;
2328 req->length = 0;
2329 gadget->ep0->driver_data = cdev;
2330
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002331 list_for_each_entry(conf, &dev->configs, list_item)
taeju.park257b5082012-10-09 10:52:01 +09002332 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
2333 if (f->ctrlrequest) {
2334 value = f->ctrlrequest(f, cdev, c);
2335 if (value >= 0)
2336 break;
2337 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002338
Benoit Gobycf3fc062011-12-19 14:39:37 -08002339 /* Special case the accessory function.
2340 * It needs to handle control requests before it is enabled.
2341 */
2342 if (value < 0)
2343 value = acc_ctrlrequest(cdev, c);
2344
Benoit Goby1e8ce152011-12-12 13:01:23 -08002345 if (value < 0)
2346 value = composite_setup(gadget, c);
2347
2348 spin_lock_irqsave(&cdev->lock, flags);
2349 if (!dev->connected) {
2350 dev->connected = 1;
2351 schedule_work(&dev->work);
2352 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2353 cdev->config) {
2354 schedule_work(&dev->work);
2355 }
2356 spin_unlock_irqrestore(&cdev->lock, flags);
2357
2358 return value;
2359}
2360
2361static void android_disconnect(struct usb_gadget *gadget)
2362{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002363 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002364 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002365 unsigned long flags;
2366
2367 composite_disconnect(gadget);
Ajay Dudanic2f0cc72012-08-27 16:23:48 +05302368 /* accessory HID support can be active while the
2369 accessory function is not actually enabled,
2370 so we need to inform it when we are disconnected.
2371 */
2372 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002373
2374 spin_lock_irqsave(&cdev->lock, flags);
2375 dev->connected = 0;
2376 schedule_work(&dev->work);
2377 spin_unlock_irqrestore(&cdev->lock, flags);
2378}
2379
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002380static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002381{
2382 struct device_attribute **attrs = android_usb_attributes;
2383 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002384 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002385 int err;
2386
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002387 /*
2388 * The primary usb core should always have usb_core_id=0, since
2389 * Android user space is currently interested in android0 events.
2390 */
2391 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2392 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002393 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002394 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002395 if (IS_ERR(dev->dev))
2396 return PTR_ERR(dev->dev);
2397
2398 dev_set_drvdata(dev->dev, dev);
2399
2400 while ((attr = *attrs++)) {
2401 err = device_create_file(dev->dev, attr);
2402 if (err) {
2403 device_destroy(android_class, dev->dev->devt);
2404 return err;
2405 }
2406 }
2407 return 0;
2408}
2409
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302410static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002411{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302412 struct device_attribute **attrs = android_usb_attributes;
2413 struct device_attribute *attr;
2414
2415 while ((attr = *attrs++))
2416 device_remove_file(dev->dev, attr);
2417 device_destroy(android_class, dev->dev->devt);
2418}
2419
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002420static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2421{
2422 struct android_dev *dev = NULL;
2423
2424 /* Find the android dev from the list */
2425 list_for_each_entry(dev, &android_dev_list, list_item) {
2426 if (dev->cdev == cdev)
2427 break;
2428 }
2429
2430 return dev;
2431}
2432
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002433static struct android_configuration *alloc_android_config
2434 (struct android_dev *dev)
2435{
2436 struct android_configuration *conf;
2437
2438 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2439 if (!conf) {
2440 pr_err("%s(): Failed to alloc memory for android conf\n",
2441 __func__);
2442 return ERR_PTR(-ENOMEM);
2443 }
2444
2445 dev->configs_num++;
2446 conf->usb_config.label = dev->name;
2447 conf->usb_config.unbind = android_unbind_config;
2448 conf->usb_config.bConfigurationValue = dev->configs_num;
2449
2450 INIT_LIST_HEAD(&conf->enabled_functions);
2451
2452 list_add_tail(&conf->list_item, &dev->configs);
2453
2454 return conf;
2455}
2456
2457static void free_android_config(struct android_dev *dev,
2458 struct android_configuration *conf)
2459{
2460 list_del(&conf->list_item);
2461 dev->configs_num--;
2462 kfree(conf);
2463}
2464
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465static int __devinit android_probe(struct platform_device *pdev)
2466{
2467 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002468 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002469 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002470
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002471 if (!android_class) {
2472 android_class = class_create(THIS_MODULE, "android_usb");
2473 if (IS_ERR(android_class))
2474 return PTR_ERR(android_class);
2475 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002476
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002477 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2478 if (!android_dev) {
2479 pr_err("%s(): Failed to alloc memory for android_dev\n",
2480 __func__);
2481 ret = -ENOMEM;
2482 goto err_alloc;
2483 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002484
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002485 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002486 android_dev->disable_depth = 1;
2487 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002488 android_dev->configs_num = 0;
2489 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002490 INIT_WORK(&android_dev->work, android_work);
2491 mutex_init(&android_dev->mutex);
2492
2493 android_dev->pdata = pdata;
2494
2495 list_add_tail(&android_dev->list_item, &android_dev_list);
2496 android_dev_count++;
2497
2498 if (pdata)
2499 composite_driver.usb_core_id = pdata->usb_core_id;
2500 else
2501 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2502
2503 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302504 if (ret) {
2505 pr_err("%s(): android_create_device failed\n", __func__);
2506 goto err_dev;
2507 }
2508
Lena Salmand092f2d2012-03-12 17:27:24 +02002509 ret = usb_composite_probe(&android_usb_driver, android_bind);
2510 if (ret) {
2511 pr_err("%s(): Failed to register android "
2512 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302513 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002514 }
2515
Ofir Cohen94213a72012-05-03 14:26:32 +03002516 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302517 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002518 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002519 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002520 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002521
Lena Salmand092f2d2012-03-12 17:27:24 +02002522 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302523err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002524 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302525err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002526 list_del(&android_dev->list_item);
2527 android_dev_count--;
2528 kfree(android_dev);
2529err_alloc:
2530 if (list_empty(&android_dev_list)) {
2531 class_destroy(android_class);
2532 android_class = NULL;
2533 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302534 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002535}
2536
2537static int android_remove(struct platform_device *pdev)
2538{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002539 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002540 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002541 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302542
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002543 if (pdata)
2544 usb_core_id = pdata->usb_core_id;
2545
2546 /* Find the android dev from the list */
2547 list_for_each_entry(dev, &android_dev_list, list_item) {
2548 if (!dev->pdata)
2549 break; /*To backward compatibility*/
2550 if (dev->pdata->usb_core_id == usb_core_id)
2551 break;
2552 }
2553
2554 if (dev) {
2555 android_destroy_device(dev);
2556 if (pdata && pdata->swfi_latency)
2557 pm_qos_remove_request(&dev->pm_qos_req_dma);
2558 list_del(&dev->list_item);
2559 android_dev_count--;
2560 kfree(dev);
2561 }
2562
2563 if (list_empty(&android_dev_list)) {
2564 class_destroy(android_class);
2565 android_class = NULL;
2566 usb_composite_unregister(&android_usb_driver);
2567 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 return 0;
2570}
2571
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002572static const struct platform_device_id android_id_table[] __devinitconst = {
2573 {
2574 .name = "android_usb",
2575 },
2576 {
2577 .name = "android_usb_hsic",
2578 },
2579};
2580
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581static struct platform_driver android_platform_driver = {
2582 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002583 .probe = android_probe,
2584 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002585 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002586};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002587
2588static int __init init(void)
2589{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302590 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002591
Benoit Goby1e8ce152011-12-12 13:01:23 -08002592 /* Override composite driver functions */
2593 composite_driver.setup = android_setup;
2594 composite_driver.disconnect = android_disconnect;
2595
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002596 INIT_LIST_HEAD(&android_dev_list);
2597 android_dev_count = 0;
2598
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302599 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302600 if (ret) {
2601 pr_err("%s(): Failed to register android"
2602 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302603 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002604
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302605 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002606}
2607module_init(init);
2608
2609static void __exit cleanup(void)
2610{
Lena Salmand092f2d2012-03-12 17:27:24 +02002611 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002612}
2613module_exit(cleanup);