blob: f944c1c5f3219aca27aebed21ae401a365be0f28 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
Anh Nguyen64376432012-10-17 16:08:48 -070034#ifndef DEBUG
35#define DEBUG 1
36#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080037/*
38 * Kbuild is not very cooperative with respect to linking separately
39 * compiled library objects into one module. So for now we won't use
40 * separate compilation ... ensuring init/exit sections work to shrink
41 * the runtime footprint, and giving us at least some parts of what
42 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
43 */
44#include "usbstring.c"
45#include "config.c"
46#include "epautoconf.c"
47#include "composite.c"
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "f_diag.c"
Shimrit Malichia00d7322012-08-05 13:56:28 +030050#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053051#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053052#include "f_rmnet_sdio.c"
53#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053054#include "f_rmnet.c"
Ajay Dudani34b1e302012-08-27 16:43:53 +053055#include "f_audio_source.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080056#include "f_mass_storage.c"
57#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053058#include "u_sdio.c"
59#include "u_smd.c"
60#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053061#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080062#include "u_ctrl_hsic.c"
63#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053064#include "u_ctrl_hsuart.c"
65#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053066#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080067#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080068#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053069#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080070#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080071#include "f_accessory.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070072#ifdef CONFIG_USB_ANDROID_CDC_ECM
73#include "f_ecm.c"
74#else
Benoit Goby1e8ce152011-12-12 13:01:23 -080075#define USB_ETH_RNDIS y
76#include "f_rndis.c"
77#include "rndis.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070078#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080079#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030080#include "u_bam_data.c"
81#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030082#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020083#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030084#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053085#ifdef CONFIG_TARGET_CORE
86#include "f_tcm.c"
87#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080088
89MODULE_AUTHOR("Mike Lockwood");
90MODULE_DESCRIPTION("Android Composite USB Driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION("1.0");
93
94static const char longname[] = "Gadget Android";
95
96/* Default vendor and product IDs, overridden by userspace */
97#define VENDOR_ID 0x18D1
98#define PRODUCT_ID 0x0001
99
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300100#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
101
Benoit Goby1e8ce152011-12-12 13:01:23 -0800102struct android_usb_function {
103 char *name;
104 void *config;
105
106 struct device *dev;
107 char *dev_name;
108 struct device_attribute **attributes;
109
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300110 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800111 struct list_head enabled_list;
112
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300113 struct android_dev *android_dev;
114
Benoit Goby1e8ce152011-12-12 13:01:23 -0800115 /* Optional: initialization during gadget bind */
116 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
117 /* Optional: cleanup during gadget unbind */
118 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700119 /* Optional: called when the function is added the list of
120 * enabled functions */
121 void (*enable)(struct android_usb_function *);
122 /* Optional: called when it is removed */
123 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800124
125 int (*bind_config)(struct android_usb_function *,
126 struct usb_configuration *);
127
128 /* Optional: called when the configuration is removed */
129 void (*unbind_config)(struct android_usb_function *,
130 struct usb_configuration *);
131 /* Optional: handle ctrl requests before the device is configured */
132 int (*ctrlrequest)(struct android_usb_function *,
133 struct usb_composite_dev *,
134 const struct usb_ctrlrequest *);
135};
136
137struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300138 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800139 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800140 struct usb_composite_dev *cdev;
141 struct device *dev;
142
143 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700144 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800145 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700146 struct android_usb_platform_data *pdata;
147
Benoit Goby1e8ce152011-12-12 13:01:23 -0800148 bool connected;
149 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300150 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700151 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800152 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300153
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300154 /* A list of struct android_configuration */
155 struct list_head configs;
156 int configs_num;
157
158 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300159 struct list_head list_item;
160
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300161};
162
163struct android_configuration {
164 struct usb_configuration usb_config;
165
166 /* A list of the functions supported by this config */
167 struct list_head enabled_functions;
168
169 /* A list node inside the struct android_dev.configs list */
170 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800171};
172
173static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300174static struct list_head android_dev_list;
175static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800176static int android_bind_config(struct usb_configuration *c);
177static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300178static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300179static struct android_configuration *alloc_android_config
180 (struct android_dev *dev);
181static void free_android_config(struct android_dev *dev,
182 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800183
184/* string IDs are assigned dynamically */
185#define STRING_MANUFACTURER_IDX 0
186#define STRING_PRODUCT_IDX 1
187#define STRING_SERIAL_IDX 2
188
189static char manufacturer_string[256];
190static char product_string[256];
191static char serial_string[256];
192
193/* String Table */
194static struct usb_string strings_dev[] = {
195 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
196 [STRING_PRODUCT_IDX].s = product_string,
197 [STRING_SERIAL_IDX].s = serial_string,
198 { } /* end of list */
199};
200
201static struct usb_gadget_strings stringtab_dev = {
202 .language = 0x0409, /* en-us */
203 .strings = strings_dev,
204};
205
206static struct usb_gadget_strings *dev_strings[] = {
207 &stringtab_dev,
208 NULL,
209};
210
211static struct usb_device_descriptor device_desc = {
212 .bLength = sizeof(device_desc),
213 .bDescriptorType = USB_DT_DEVICE,
214 .bcdUSB = __constant_cpu_to_le16(0x0200),
215 .bDeviceClass = USB_CLASS_PER_INTERFACE,
216 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
217 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
218 .bcdDevice = __constant_cpu_to_le16(0xffff),
219 .bNumConfigurations = 1,
220};
221
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530222static struct usb_otg_descriptor otg_descriptor = {
223 .bLength = sizeof otg_descriptor,
224 .bDescriptorType = USB_DT_OTG,
225 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
226 .bcdOTG = __constant_cpu_to_le16(0x0200),
227};
228
229static const struct usb_descriptor_header *otg_desc[] = {
230 (struct usb_descriptor_header *) &otg_descriptor,
231 NULL,
232};
233
Manu Gautama2b54142012-04-03 14:34:32 +0530234enum android_device_state {
235 USB_DISCONNECTED,
236 USB_CONNECTED,
237 USB_CONFIGURED,
238};
239
Ofir Cohen94213a72012-05-03 14:26:32 +0300240static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
241{
242 struct android_usb_platform_data *pdata = dev->pdata;
243 u32 swfi_latency = 0;
244 static int last_vote = -1;
245
Ofir Cohen56eb7072012-05-20 11:41:39 +0300246 if (!pdata || vote == last_vote
247 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300248 return;
249
250 swfi_latency = pdata->swfi_latency + 1;
251 if (vote)
252 pm_qos_update_request(&dev->pm_qos_req_dma,
253 swfi_latency);
254 else
255 pm_qos_update_request(&dev->pm_qos_req_dma,
256 PM_QOS_DEFAULT_VALUE);
257 last_vote = vote;
258}
259
Benoit Goby1e8ce152011-12-12 13:01:23 -0800260static void android_work(struct work_struct *data)
261{
262 struct android_dev *dev = container_of(data, struct android_dev, work);
263 struct usb_composite_dev *cdev = dev->cdev;
264 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
265 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
266 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
267 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530268 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800269 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300270 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800271
272 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530273 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800274 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530275 next_state = USB_CONFIGURED;
276 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800277 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530278 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300279 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300280 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300281 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300282 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530283 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800284 dev->sw_connected = dev->connected;
285 spin_unlock_irqrestore(&cdev->lock, flags);
286
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300287 if (pm_qos_vote != -1)
288 android_pm_qos_update_latency(dev, pm_qos_vote);
289
Benoit Goby1e8ce152011-12-12 13:01:23 -0800290 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530291 /*
292 * Some userspace modules, e.g. MTP, work correctly only if
293 * CONFIGURED uevent is preceded by DISCONNECT uevent.
294 * Check if we missed sending out a DISCONNECT uevent. This can
295 * happen if host PC resets and configures device really quick.
296 */
297 if (((uevent_envp == connected) &&
298 (last_uevent != USB_DISCONNECTED)) ||
299 ((uevent_envp == configured) &&
300 (last_uevent == USB_CONFIGURED))) {
301 pr_info("%s: sent missed DISCONNECT event\n", __func__);
302 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
303 disconnected);
304 msleep(20);
305 }
306 /*
307 * Before sending out CONFIGURED uevent give function drivers
308 * a chance to wakeup userspace threads and notify disconnect
309 */
310 if (uevent_envp == configured)
311 msleep(50);
312
Benoit Goby1e8ce152011-12-12 13:01:23 -0800313 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530314 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800315 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
316 } else {
317 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
318 dev->connected, dev->sw_connected, cdev->config);
319 }
320}
321
Benoit Goby80ba14d2012-03-19 18:56:52 -0700322static void android_enable(struct android_dev *dev)
323{
324 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300325 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700326
327 if (WARN_ON(!dev->disable_depth))
328 return;
329
330 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300331
332 list_for_each_entry(conf, &dev->configs, list_item)
333 usb_add_config(cdev, &conf->usb_config,
334 android_bind_config);
335
Benoit Goby80ba14d2012-03-19 18:56:52 -0700336 usb_gadget_connect(cdev->gadget);
337 }
338}
339
340static void android_disable(struct android_dev *dev)
341{
342 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300343 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700344
345 if (dev->disable_depth++ == 0) {
346 usb_gadget_disconnect(cdev->gadget);
347 /* Cancel pending control requests */
348 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300349
350 list_for_each_entry(conf, &dev->configs, list_item)
351 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700352 }
353}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800354
355/*-------------------------------------------------------------------------*/
356/* Supported functions initialization */
357
Benoit Goby80ba14d2012-03-19 18:56:52 -0700358struct adb_data {
359 bool opened;
360 bool enabled;
361};
362
Benoit Goby2b6862d2011-12-19 14:38:41 -0800363static int
364adb_function_init(struct android_usb_function *f,
365 struct usb_composite_dev *cdev)
366{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700367 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
368 if (!f->config)
369 return -ENOMEM;
370
Benoit Goby2b6862d2011-12-19 14:38:41 -0800371 return adb_setup();
372}
373
374static void adb_function_cleanup(struct android_usb_function *f)
375{
376 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700377 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800378}
379
380static int
381adb_function_bind_config(struct android_usb_function *f,
382 struct usb_configuration *c)
383{
384 return adb_bind_config(c);
385}
386
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387static void adb_android_function_enable(struct android_usb_function *f)
388{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300389 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700390 struct adb_data *data = f->config;
391
392 data->enabled = true;
393
394 /* Disable the gadget until adbd is ready */
395 if (!data->opened)
396 android_disable(dev);
397}
398
399static void adb_android_function_disable(struct android_usb_function *f)
400{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300401 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700402 struct adb_data *data = f->config;
403
404 data->enabled = false;
405
406 /* Balance the disable that was called in closed_callback */
407 if (!data->opened)
408 android_enable(dev);
409}
410
Benoit Goby2b6862d2011-12-19 14:38:41 -0800411static struct android_usb_function adb_function = {
412 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700413 .enable = adb_android_function_enable,
414 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800415 .init = adb_function_init,
416 .cleanup = adb_function_cleanup,
417 .bind_config = adb_function_bind_config,
418};
419
Benoit Goby80ba14d2012-03-19 18:56:52 -0700420static void adb_ready_callback(void)
421{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300422 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700423 struct adb_data *data = adb_function.config;
424
Benoit Goby80ba14d2012-03-19 18:56:52 -0700425 data->opened = true;
426
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300427 if (data->enabled && dev) {
428 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700429 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300430 mutex_unlock(&dev->mutex);
431 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700432}
433
434static void adb_closed_callback(void)
435{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300436 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700437 struct adb_data *data = adb_function.config;
438
Benoit Goby80ba14d2012-03-19 18:56:52 -0700439 data->opened = false;
440
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300441 if (data->enabled) {
442 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700443 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300444 mutex_unlock(&dev->mutex);
445 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700446}
447
Benoit Goby2b6862d2011-12-19 14:38:41 -0800448
Benoit Gobyaab96812011-04-19 20:37:33 -0700449/*-------------------------------------------------------------------------*/
450/* Supported functions initialization */
451
Manu Gautam8e0719b2011-09-26 14:47:55 +0530452/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530453static int rmnet_smd_function_bind_config(struct android_usb_function *f,
454 struct usb_configuration *c)
455{
456 return rmnet_smd_bind_config(c);
457}
458
459static struct android_usb_function rmnet_smd_function = {
460 .name = "rmnet_smd",
461 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800462};
463
Manu Gautam8e0719b2011-09-26 14:47:55 +0530464/* RMNET_SDIO */
465static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
466 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800467{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530468 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800469}
470
Manu Gautam8e0719b2011-09-26 14:47:55 +0530471static struct android_usb_function rmnet_sdio_function = {
472 .name = "rmnet_sdio",
473 .bind_config = rmnet_sdio_function_bind_config,
474};
475
476/* RMNET_SMD_SDIO */
477static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
478 struct usb_composite_dev *cdev)
479{
480 return rmnet_smd_sdio_init();
481}
482
483static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
484{
485 rmnet_smd_sdio_cleanup();
486}
487
488static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
489 struct usb_configuration *c)
490{
491 return rmnet_smd_sdio_function_add(c);
492}
493
494static struct device_attribute *rmnet_smd_sdio_attributes[] = {
495 &dev_attr_transport, NULL };
496
497static struct android_usb_function rmnet_smd_sdio_function = {
498 .name = "rmnet_smd_sdio",
499 .init = rmnet_smd_sdio_function_init,
500 .cleanup = rmnet_smd_sdio_function_cleanup,
501 .bind_config = rmnet_smd_sdio_bind_config,
502 .attributes = rmnet_smd_sdio_attributes,
503};
504
Hemant Kumar1b820d52011-11-03 15:08:28 -0700505/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
506#define MAX_XPORT_STR_LEN 50
507static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530508
Manu Gautame3e897c2011-09-12 17:18:46 +0530509static void rmnet_function_cleanup(struct android_usb_function *f)
510{
511 frmnet_cleanup();
512}
513
Manu Gautam2b0234a2011-09-07 16:47:52 +0530514static int rmnet_function_bind_config(struct android_usb_function *f,
515 struct usb_configuration *c)
516{
517 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700518 int err = 0;
519 char *ctrl_name;
520 char *data_name;
521 char buf[MAX_XPORT_STR_LEN], *b;
522 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530523
Hemant Kumar1b820d52011-11-03 15:08:28 -0700524 if (!rmnet_initialized) {
525 rmnet_initialized = 1;
526 strlcpy(buf, rmnet_transports, sizeof(buf));
527 b = strim(buf);
528 while (b) {
529 ctrl_name = strsep(&b, ",");
530 data_name = strsep(&b, ",");
531 if (ctrl_name && data_name) {
532 err = frmnet_init_port(ctrl_name, data_name);
533 if (err) {
534 pr_err("rmnet: Cannot open ctrl port:"
535 "'%s' data port:'%s'\n",
536 ctrl_name, data_name);
537 goto out;
538 }
539 ports++;
540 }
541 }
542
543 err = rmnet_gport_setup();
544 if (err) {
545 pr_err("rmnet: Cannot setup transports");
546 goto out;
547 }
548 }
549
550 for (i = 0; i < ports; i++) {
551 err = frmnet_bind_config(c, i);
552 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530553 pr_err("Could not bind rmnet%u config\n", i);
554 break;
555 }
556 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700557out:
558 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530559}
560
Hemant Kumar1b820d52011-11-03 15:08:28 -0700561static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530562 struct device_attribute *attr, char *buf)
563{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700564 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530565}
566
Hemant Kumar1b820d52011-11-03 15:08:28 -0700567static ssize_t rmnet_transports_store(
568 struct device *device, struct device_attribute *attr,
569 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530570{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700571 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530572
Manu Gautam2b0234a2011-09-07 16:47:52 +0530573 return size;
574}
575
Hemant Kumar1b820d52011-11-03 15:08:28 -0700576static struct device_attribute dev_attr_rmnet_transports =
577 __ATTR(transports, S_IRUGO | S_IWUSR,
578 rmnet_transports_show,
579 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530580static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700581 &dev_attr_rmnet_transports,
582 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530583
584static struct android_usb_function rmnet_function = {
585 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530586 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530587 .bind_config = rmnet_function_bind_config,
588 .attributes = rmnet_function_attributes,
589};
590
Ofir Cohen7b155422012-07-31 13:02:49 +0300591struct ecm_function_config {
592 u8 ethaddr[ETH_ALEN];
593};
594
595static int ecm_function_init(struct android_usb_function *f,
596 struct usb_composite_dev *cdev)
597{
598 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
599 if (!f->config)
600 return -ENOMEM;
601 return 0;
602}
603
604static void ecm_function_cleanup(struct android_usb_function *f)
605{
606 kfree(f->config);
607 f->config = NULL;
608}
609
610static int ecm_qc_function_bind_config(struct android_usb_function *f,
611 struct usb_configuration *c)
612{
613 int ret;
614 struct ecm_function_config *ecm = f->config;
615
616 if (!ecm) {
617 pr_err("%s: ecm_pdata\n", __func__);
618 return -EINVAL;
619 }
620
621 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
622 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
623 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
624
625 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
626 if (ret) {
627 pr_err("%s: gether_setup failed\n", __func__);
628 return ret;
629 }
630
631 return ecm_qc_bind_config(c, ecm->ethaddr);
632}
633
634static void ecm_qc_function_unbind_config(struct android_usb_function *f,
635 struct usb_configuration *c)
636{
637 gether_qc_cleanup();
638}
639
640static ssize_t ecm_ethaddr_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
642{
643 struct android_usb_function *f = dev_get_drvdata(dev);
644 struct ecm_function_config *ecm = f->config;
645 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
646 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
647 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
648}
649
650static ssize_t ecm_ethaddr_store(struct device *dev,
651 struct device_attribute *attr, const char *buf, size_t size)
652{
653 struct android_usb_function *f = dev_get_drvdata(dev);
654 struct ecm_function_config *ecm = f->config;
655
656 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
657 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
658 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
659 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
660 return size;
661 return -EINVAL;
662}
663
664static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
665 ecm_ethaddr_store);
666
667static struct device_attribute *ecm_function_attributes[] = {
668 &dev_attr_ecm_ethaddr,
669 NULL
670};
671
672static struct android_usb_function ecm_qc_function = {
673 .name = "ecm_qc",
674 .init = ecm_function_init,
675 .cleanup = ecm_function_cleanup,
676 .bind_config = ecm_qc_function_bind_config,
677 .unbind_config = ecm_qc_function_unbind_config,
678 .attributes = ecm_function_attributes,
679};
Anna Perela8c991d2012-04-09 16:44:46 +0300680
681/* MBIM - used with BAM */
682#define MAX_MBIM_INSTANCES 1
683
684static int mbim_function_init(struct android_usb_function *f,
685 struct usb_composite_dev *cdev)
686{
687 return mbim_init(MAX_MBIM_INSTANCES);
688}
689
690static void mbim_function_cleanup(struct android_usb_function *f)
691{
692 fmbim_cleanup();
693}
694
695static int mbim_function_bind_config(struct android_usb_function *f,
696 struct usb_configuration *c)
697{
698 return mbim_bind_config(c, 0);
699}
700
701static struct android_usb_function mbim_function = {
702 .name = "usb_mbim",
703 .cleanup = mbim_function_cleanup,
704 .bind_config = mbim_function_bind_config,
705 .init = mbim_function_init,
706};
707
708
Manu Gautam8e0719b2011-09-26 14:47:55 +0530709/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530710static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711static ssize_t clients_store(
712 struct device *device, struct device_attribute *attr,
713 const char *buff, size_t size)
714{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530715 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716
717 return size;
718}
719
720static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
721static struct device_attribute *diag_function_attributes[] =
722 { &dev_attr_clients, NULL };
723
724static int diag_function_init(struct android_usb_function *f,
725 struct usb_composite_dev *cdev)
726{
727 return diag_setup();
728}
729
730static void diag_function_cleanup(struct android_usb_function *f)
731{
732 diag_cleanup();
733}
734
735static int diag_function_bind_config(struct android_usb_function *f,
736 struct usb_configuration *c)
737{
738 char *name;
739 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530740 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800741 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300742 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530744 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 b = strim(buf);
746
747 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800748 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530750 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300751 if (dev->pdata && !once++)
752 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530753
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530755 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 if (err)
757 pr_err("diag: Cannot open channel '%s'", name);
758 }
759 }
760
761 return err;
762}
763
764static struct android_usb_function diag_function = {
765 .name = "diag",
766 .init = diag_function_init,
767 .cleanup = diag_function_cleanup,
768 .bind_config = diag_function_bind_config,
769 .attributes = diag_function_attributes,
770};
771
Shimrit Malichia00d7322012-08-05 13:56:28 +0300772/* DEBUG */
773static int qdss_function_init(struct android_usb_function *f,
774 struct usb_composite_dev *cdev)
775{
776 return qdss_setup();
777}
778
779static void qdss_function_cleanup(struct android_usb_function *f)
780{
781 qdss_cleanup();
782}
783
784static int qdss_function_bind_config(struct android_usb_function *f,
785 struct usb_configuration *c)
786{
787 int err = -1;
788
789 err = qdss_bind_config(c, "qdss");
790 if (err)
791 pr_err("qdss: Cannot open channel qdss");
792
793 return err;
794}
795
796static struct android_usb_function qdss_function = {
797 .name = "qdss",
798 .init = qdss_function_init,
799 .cleanup = qdss_function_cleanup,
800 .bind_config = qdss_function_bind_config,
801};
802
Manu Gautam8e0719b2011-09-26 14:47:55 +0530803/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530804static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530805static ssize_t serial_transports_store(
806 struct device *device, struct device_attribute *attr,
807 const char *buff, size_t size)
808{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530809 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530810
811 return size;
812}
813
814static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
815static struct device_attribute *serial_function_attributes[] =
816 { &dev_attr_transports, NULL };
817
818static void serial_function_cleanup(struct android_usb_function *f)
819{
820 gserial_cleanup();
821}
822
823static int serial_function_bind_config(struct android_usb_function *f,
824 struct usb_configuration *c)
825{
826 char *name;
827 char buf[32], *b;
828 int err = -1, i;
829 static int serial_initialized = 0, ports = 0;
830
831 if (serial_initialized)
832 goto bind_config;
833
834 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530835 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530836 b = strim(buf);
837
838 while (b) {
839 name = strsep(&b, ",");
840
841 if (name) {
842 err = gserial_init_port(ports, name);
843 if (err) {
844 pr_err("serial: Cannot open port '%s'", name);
845 goto out;
846 }
847 ports++;
848 }
849 }
850 err = gport_setup(c);
851 if (err) {
852 pr_err("serial: Cannot setup transports");
853 goto out;
854 }
855
856bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200857 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530858 err = gser_bind_config(c, i);
859 if (err) {
860 pr_err("serial: bind_config failed for port %d", i);
861 goto out;
862 }
863 }
864
865out:
866 return err;
867}
868
869static struct android_usb_function serial_function = {
870 .name = "serial",
871 .cleanup = serial_function_cleanup,
872 .bind_config = serial_function_bind_config,
873 .attributes = serial_function_attributes,
874};
875
Anji jonnala92be1b42011-12-19 09:44:41 +0530876/* ACM */
877static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
878static ssize_t acm_transports_store(
879 struct device *device, struct device_attribute *attr,
880 const char *buff, size_t size)
881{
882 strlcpy(acm_transports, buff, sizeof(acm_transports));
883
884 return size;
885}
886
887static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
888static struct device_attribute *acm_function_attributes[] = {
889 &dev_attr_acm_transports, NULL };
890
Benoit Goby1e8ce152011-12-12 13:01:23 -0800891static void acm_function_cleanup(struct android_usb_function *f)
892{
893 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800894}
895
Anji jonnala92be1b42011-12-19 09:44:41 +0530896static int acm_function_bind_config(struct android_usb_function *f,
897 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800898{
Anji jonnala92be1b42011-12-19 09:44:41 +0530899 char *name;
900 char buf[32], *b;
901 int err = -1, i;
902 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800903
Anji jonnala92be1b42011-12-19 09:44:41 +0530904 if (acm_initialized)
905 goto bind_config;
906
907 acm_initialized = 1;
908 strlcpy(buf, acm_transports, sizeof(buf));
909 b = strim(buf);
910
911 while (b) {
912 name = strsep(&b, ",");
913
914 if (name) {
915 err = acm_init_port(ports, name);
916 if (err) {
917 pr_err("acm: Cannot open port '%s'", name);
918 goto out;
919 }
920 ports++;
921 }
922 }
923 err = acm_port_setup(c);
924 if (err) {
925 pr_err("acm: Cannot setup transports");
926 goto out;
927 }
928
929bind_config:
930 for (i = 0; i < ports; i++) {
931 err = acm_bind_config(c, i);
932 if (err) {
933 pr_err("acm: bind_config failed for port %d", i);
934 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800935 }
936 }
937
Anji jonnala92be1b42011-12-19 09:44:41 +0530938out:
939 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800940}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800941static struct android_usb_function acm_function = {
942 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800943 .cleanup = acm_function_cleanup,
944 .bind_config = acm_function_bind_config,
945 .attributes = acm_function_attributes,
946};
947
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530948/* CCID */
949static int ccid_function_init(struct android_usb_function *f,
950 struct usb_composite_dev *cdev)
951{
952 return ccid_setup();
953}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800954
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530955static void ccid_function_cleanup(struct android_usb_function *f)
956{
957 ccid_cleanup();
958}
959
960static int ccid_function_bind_config(struct android_usb_function *f,
961 struct usb_configuration *c)
962{
963 return ccid_bind_config(c);
964}
965
966static struct android_usb_function ccid_function = {
967 .name = "ccid",
968 .init = ccid_function_init,
969 .cleanup = ccid_function_cleanup,
970 .bind_config = ccid_function_bind_config,
971};
972
Steve Mucklef132c6c2012-06-06 18:30:57 -0700973static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800974 struct usb_composite_dev *cdev)
975{
976 return mtp_setup();
977}
978
979static void mtp_function_cleanup(struct android_usb_function *f)
980{
981 mtp_cleanup();
982}
983
Steve Mucklef132c6c2012-06-06 18:30:57 -0700984static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800985 struct usb_configuration *c)
986{
987 return mtp_bind_config(c, false);
988}
989
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400990static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800991{
992 /* nothing to do - initialization is handled by mtp_function_init */
993 return 0;
994}
995
996static void ptp_function_cleanup(struct android_usb_function *f)
997{
998 /* nothing to do - cleanup is handled by mtp_function_cleanup */
999}
1000
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001001static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001002{
1003 return mtp_bind_config(c, true);
1004}
1005
1006static int mtp_function_ctrlrequest(struct android_usb_function *f,
1007 struct usb_composite_dev *cdev,
1008 const struct usb_ctrlrequest *c)
1009{
1010 return mtp_ctrlrequest(cdev, c);
1011}
1012
1013static struct android_usb_function mtp_function = {
1014 .name = "mtp",
1015 .init = mtp_function_init,
1016 .cleanup = mtp_function_cleanup,
1017 .bind_config = mtp_function_bind_config,
1018 .ctrlrequest = mtp_function_ctrlrequest,
1019};
1020
1021/* PTP function is same as MTP with slightly different interface descriptor */
1022static struct android_usb_function ptp_function = {
1023 .name = "ptp",
1024 .init = ptp_function_init,
1025 .cleanup = ptp_function_cleanup,
1026 .bind_config = ptp_function_bind_config,
1027};
1028
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001029#ifdef CONFIG_USB_ANDROID_CDC_ECM
1030struct ecm_function_config {
1031 u8 ethaddr[ETH_ALEN];
1032 u32 vendorID;
1033 char manufacturer[256];
1034};
1035
1036static int ecm_function_init(struct android_usb_function *f,
1037 struct usb_composite_dev *cdev)
1038{
1039 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
1040 if (!f->config)
1041 return -ENOMEM;
1042 return 0;
1043}
1044
1045static void ecm_function_cleanup(struct android_usb_function *f)
1046{
1047 kfree(f->config);
1048 f->config = NULL;
1049}
1050
1051static int ecm_function_bind_config(struct android_usb_function *f,
1052 struct usb_configuration *c)
1053{
1054 int ret;
1055 struct ecm_function_config *ecm = f->config;
1056
1057 if (!ecm) {
1058 pr_err("%s: ecm_function_config\n", __func__);
1059 return -1;
1060 }
1061
1062 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1063 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1064 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1065
1066 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usb");
1067 if (ret) {
1068 pr_err("%s: gether_setup failed\n", __func__);
1069 return ret;
1070 }
1071
1072 return ecm_bind_config(c, ecm->ethaddr);
1073}
1074
1075static void ecm_function_unbind_config(struct android_usb_function *f,
1076 struct usb_configuration *c)
1077{
1078 gether_cleanup();
1079}
1080
1081static ssize_t ecm_manufacturer_show(struct device *dev,
1082 struct device_attribute *attr, char *buf)
1083{
1084 struct android_usb_function *f = dev_get_drvdata(dev);
1085 struct ecm_function_config *config = f->config;
1086
1087 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
1088}
1089
1090static ssize_t ecm_manufacturer_store(struct device *dev,
1091 struct device_attribute *attr, const char *buf, size_t size)
1092{
1093 struct android_usb_function *f = dev_get_drvdata(dev);
1094 struct ecm_function_config *config = f->config;
1095
1096 if (size >= sizeof(config->manufacturer))
1097 return -EINVAL;
1098
1099 if (sscanf(buf, "%255s", config->manufacturer) == 1)
1100 return size;
1101 return -1;
1102}
1103
1104static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, ecm_manufacturer_show,
1105 ecm_manufacturer_store);
1106
1107static ssize_t ecm_ethaddr_show(struct device *dev,
1108 struct device_attribute *attr, char *buf)
1109{
1110 struct android_usb_function *f = dev_get_drvdata(dev);
1111 struct ecm_function_config *ecm = f->config;
1112 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1113 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1114 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1115}
1116
1117static ssize_t ecm_ethaddr_store(struct device *dev,
1118 struct device_attribute *attr, const char *buf, size_t size)
1119{
1120 struct android_usb_function *f = dev_get_drvdata(dev);
1121 struct ecm_function_config *ecm = f->config;
1122
1123 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1124 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1125 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1126 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1127 return size;
1128 return -EINVAL;
1129}
1130
1131static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1132 ecm_ethaddr_store);
1133
1134static ssize_t ecm_vendorID_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136{
1137 struct android_usb_function *f = dev_get_drvdata(dev);
1138 struct ecm_function_config *config = f->config;
1139
1140 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
1141}
1142
1143static ssize_t ecm_vendorID_store(struct device *dev,
1144 struct device_attribute *attr, const char *buf, size_t size)
1145{
1146 struct android_usb_function *f = dev_get_drvdata(dev);
1147 struct ecm_function_config *config = f->config;
1148 int value;
1149
1150 if (sscanf(buf, "%04x", &value) == 1) {
1151 config->vendorID = value;
1152 return size;
1153 }
1154 return -EINVAL;
1155}
1156
1157static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, ecm_vendorID_show,
1158 ecm_vendorID_store);
1159
1160static struct device_attribute *ecm_function_attributes[] = {
1161 &dev_attr_manufacturer,
1162 &dev_attr_ethaddr,
1163 &dev_attr_vendorID,
1164 NULL
1165};
1166
1167static struct android_usb_function ecm_function = {
1168 .name = "ecm",
1169 .init = ecm_function_init,
1170 .cleanup = ecm_function_cleanup,
1171 .bind_config = ecm_function_bind_config,
1172 .unbind_config = ecm_function_unbind_config,
1173 .attributes = ecm_function_attributes,
1174};
1175
1176#else
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001177
Benoit Goby1e8ce152011-12-12 13:01:23 -08001178struct rndis_function_config {
1179 u8 ethaddr[ETH_ALEN];
1180 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001181 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001182 char manufacturer[256];
1183 /* "Wireless" RNDIS; auto-detected by Windows */
1184 bool wceis;
1185};
1186
1187static int
1188rndis_function_init(struct android_usb_function *f,
1189 struct usb_composite_dev *cdev)
1190{
1191 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1192 if (!f->config)
1193 return -ENOMEM;
1194 return 0;
1195}
1196
1197static void rndis_function_cleanup(struct android_usb_function *f)
1198{
1199 kfree(f->config);
1200 f->config = NULL;
1201}
1202
Ofir Cohenaef90b72012-07-31 12:37:04 +02001203static int rndis_qc_function_init(struct android_usb_function *f,
1204 struct usb_composite_dev *cdev)
1205{
1206 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1207 if (!f->config)
1208 return -ENOMEM;
1209
1210 return rndis_qc_init();
1211}
1212
1213static void rndis_qc_function_cleanup(struct android_usb_function *f)
1214{
1215 rndis_qc_cleanup();
1216 kfree(f->config);
1217}
1218
Benoit Goby1e8ce152011-12-12 13:01:23 -08001219static int
1220rndis_function_bind_config(struct android_usb_function *f,
1221 struct usb_configuration *c)
1222{
1223 int ret;
1224 struct rndis_function_config *rndis = f->config;
1225
1226 if (!rndis) {
1227 pr_err("%s: rndis_pdata\n", __func__);
1228 return -1;
1229 }
1230
1231 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1232 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1233 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1234
Devin Kim78e07192012-07-17 08:40:54 -07001235 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "usb");
Benoit Goby1e8ce152011-12-12 13:01:23 -08001236 if (ret) {
1237 pr_err("%s: gether_setup failed\n", __func__);
1238 return ret;
1239 }
1240
1241 if (rndis->wceis) {
1242 /* "Wireless" RNDIS; auto-detected by Windows */
1243 rndis_iad_descriptor.bFunctionClass =
1244 USB_CLASS_WIRELESS_CONTROLLER;
1245 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1246 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1247 rndis_control_intf.bInterfaceClass =
1248 USB_CLASS_WIRELESS_CONTROLLER;
1249 rndis_control_intf.bInterfaceSubClass = 0x01;
1250 rndis_control_intf.bInterfaceProtocol = 0x03;
1251 }
1252
1253 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1254 rndis->manufacturer);
1255}
1256
Ofir Cohenaef90b72012-07-31 12:37:04 +02001257static int rndis_qc_function_bind_config(struct android_usb_function *f,
1258 struct usb_configuration *c)
1259{
1260 int ret;
1261 struct rndis_function_config *rndis = f->config;
1262
1263 if (!rndis) {
1264 pr_err("%s: rndis_pdata\n", __func__);
1265 return -EINVAL;
1266 }
1267
1268 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1269 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1270 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1271
1272 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1273 if (ret) {
1274 pr_err("%s: gether_setup failed\n", __func__);
1275 return ret;
1276 }
1277
1278 if (rndis->wceis) {
1279 /* "Wireless" RNDIS; auto-detected by Windows */
1280 rndis_qc_iad_descriptor.bFunctionClass =
1281 USB_CLASS_WIRELESS_CONTROLLER;
1282 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1283 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1284 rndis_qc_control_intf.bInterfaceClass =
1285 USB_CLASS_WIRELESS_CONTROLLER;
1286 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1287 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1288 }
1289
1290 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1291 rndis->manufacturer,
1292 rndis->max_pkt_per_xfer);
1293}
1294
Benoit Goby1e8ce152011-12-12 13:01:23 -08001295static void rndis_function_unbind_config(struct android_usb_function *f,
1296 struct usb_configuration *c)
1297{
1298 gether_cleanup();
1299}
1300
Ofir Cohenaef90b72012-07-31 12:37:04 +02001301static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1302 struct usb_configuration *c)
1303{
1304 gether_qc_cleanup();
1305}
1306
Benoit Goby1e8ce152011-12-12 13:01:23 -08001307static ssize_t rndis_manufacturer_show(struct device *dev,
1308 struct device_attribute *attr, char *buf)
1309{
1310 struct android_usb_function *f = dev_get_drvdata(dev);
1311 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001312
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301313 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001314}
1315
1316static ssize_t rndis_manufacturer_store(struct device *dev,
1317 struct device_attribute *attr, const char *buf, size_t size)
1318{
1319 struct android_usb_function *f = dev_get_drvdata(dev);
1320 struct rndis_function_config *config = f->config;
1321
1322 if (size >= sizeof(config->manufacturer))
1323 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001324
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301325 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001326 return size;
1327 return -1;
1328}
1329
1330static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1331 rndis_manufacturer_store);
1332
1333static ssize_t rndis_wceis_show(struct device *dev,
1334 struct device_attribute *attr, char *buf)
1335{
1336 struct android_usb_function *f = dev_get_drvdata(dev);
1337 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001338
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301339 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001340}
1341
1342static ssize_t rndis_wceis_store(struct device *dev,
1343 struct device_attribute *attr, const char *buf, size_t size)
1344{
1345 struct android_usb_function *f = dev_get_drvdata(dev);
1346 struct rndis_function_config *config = f->config;
1347 int value;
1348
1349 if (sscanf(buf, "%d", &value) == 1) {
1350 config->wceis = value;
1351 return size;
1352 }
1353 return -EINVAL;
1354}
1355
1356static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1357 rndis_wceis_store);
1358
1359static ssize_t rndis_ethaddr_show(struct device *dev,
1360 struct device_attribute *attr, char *buf)
1361{
1362 struct android_usb_function *f = dev_get_drvdata(dev);
1363 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001364
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301365 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001366 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1367 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1368}
1369
1370static ssize_t rndis_ethaddr_store(struct device *dev,
1371 struct device_attribute *attr, const char *buf, size_t size)
1372{
1373 struct android_usb_function *f = dev_get_drvdata(dev);
1374 struct rndis_function_config *rndis = f->config;
1375
1376 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1377 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1378 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1379 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1380 return size;
1381 return -EINVAL;
1382}
1383
1384static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1385 rndis_ethaddr_store);
1386
1387static ssize_t rndis_vendorID_show(struct device *dev,
1388 struct device_attribute *attr, char *buf)
1389{
1390 struct android_usb_function *f = dev_get_drvdata(dev);
1391 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001392
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301393 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001394}
1395
1396static ssize_t rndis_vendorID_store(struct device *dev,
1397 struct device_attribute *attr, const char *buf, size_t size)
1398{
1399 struct android_usb_function *f = dev_get_drvdata(dev);
1400 struct rndis_function_config *config = f->config;
1401 int value;
1402
1403 if (sscanf(buf, "%04x", &value) == 1) {
1404 config->vendorID = value;
1405 return size;
1406 }
1407 return -EINVAL;
1408}
1409
1410static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1411 rndis_vendorID_store);
1412
Ofir Cohenaef90b72012-07-31 12:37:04 +02001413static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1414 struct device_attribute *attr, char *buf)
1415{
1416 struct android_usb_function *f = dev_get_drvdata(dev);
1417 struct rndis_function_config *config = f->config;
1418 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1419}
1420
1421static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1422 struct device_attribute *attr, const char *buf, size_t size)
1423{
1424 struct android_usb_function *f = dev_get_drvdata(dev);
1425 struct rndis_function_config *config = f->config;
1426 int value;
1427
1428 if (sscanf(buf, "%d", &value) == 1) {
1429 config->max_pkt_per_xfer = value;
1430 return size;
1431 }
1432 return -EINVAL;
1433}
1434
1435static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1436 rndis_max_pkt_per_xfer_show,
1437 rndis_max_pkt_per_xfer_store);
1438
Benoit Goby1e8ce152011-12-12 13:01:23 -08001439static struct device_attribute *rndis_function_attributes[] = {
1440 &dev_attr_manufacturer,
1441 &dev_attr_wceis,
1442 &dev_attr_ethaddr,
1443 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001444 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001445 NULL
1446};
1447
1448static struct android_usb_function rndis_function = {
1449 .name = "rndis",
1450 .init = rndis_function_init,
1451 .cleanup = rndis_function_cleanup,
1452 .bind_config = rndis_function_bind_config,
1453 .unbind_config = rndis_function_unbind_config,
1454 .attributes = rndis_function_attributes,
1455};
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001456#endif /* CONFIG_USB_ANDROID_CDC_ECM */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001457
Ofir Cohenaef90b72012-07-31 12:37:04 +02001458static struct android_usb_function rndis_qc_function = {
1459 .name = "rndis_qc",
1460 .init = rndis_qc_function_init,
1461 .cleanup = rndis_qc_function_cleanup,
1462 .bind_config = rndis_qc_function_bind_config,
1463 .unbind_config = rndis_qc_function_unbind_config,
1464 .attributes = rndis_function_attributes,
1465};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001466
1467struct mass_storage_function_config {
1468 struct fsg_config fsg;
1469 struct fsg_common *common;
1470};
1471
1472static int mass_storage_function_init(struct android_usb_function *f,
1473 struct usb_composite_dev *cdev)
1474{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001475 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001476 struct mass_storage_function_config *config;
1477 struct fsg_common *common;
1478 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301479 int i;
1480 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001481
1482 config = kzalloc(sizeof(struct mass_storage_function_config),
1483 GFP_KERNEL);
1484 if (!config)
1485 return -ENOMEM;
1486
1487 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301488 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301489 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301490 config->fsg.nluns = 2;
1491 config->fsg.luns[1].cdrom = 1;
1492 config->fsg.luns[1].ro = 1;
1493 config->fsg.luns[1].removable = 1;
1494 name[1] = "lun0";
1495 }
1496
Benoit Goby1e8ce152011-12-12 13:01:23 -08001497 config->fsg.luns[0].removable = 1;
1498
1499 common = fsg_common_init(NULL, cdev, &config->fsg);
1500 if (IS_ERR(common)) {
1501 kfree(config);
1502 return PTR_ERR(common);
1503 }
1504
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301505 for (i = 0; i < config->fsg.nluns; i++) {
1506 err = sysfs_create_link(&f->dev->kobj,
1507 &common->luns[i].dev.kobj,
1508 name[i]);
1509 if (err)
1510 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001511 }
1512
1513 config->common = common;
1514 f->config = config;
1515 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301516error:
1517 for (; i > 0 ; i--)
1518 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1519
1520 fsg_common_release(&common->ref);
1521 kfree(config);
1522 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001523}
1524
1525static void mass_storage_function_cleanup(struct android_usb_function *f)
1526{
1527 kfree(f->config);
1528 f->config = NULL;
1529}
1530
1531static int mass_storage_function_bind_config(struct android_usb_function *f,
1532 struct usb_configuration *c)
1533{
1534 struct mass_storage_function_config *config = f->config;
1535 return fsg_bind_config(c->cdev, c, config->common);
1536}
1537
1538static ssize_t mass_storage_inquiry_show(struct device *dev,
1539 struct device_attribute *attr, char *buf)
1540{
1541 struct android_usb_function *f = dev_get_drvdata(dev);
1542 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301543 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001544}
1545
1546static ssize_t mass_storage_inquiry_store(struct device *dev,
1547 struct device_attribute *attr, const char *buf, size_t size)
1548{
1549 struct android_usb_function *f = dev_get_drvdata(dev);
1550 struct mass_storage_function_config *config = f->config;
1551 if (size >= sizeof(config->common->inquiry_string))
1552 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301553 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001554 return -EINVAL;
1555 return size;
1556}
1557
1558static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1559 mass_storage_inquiry_show,
1560 mass_storage_inquiry_store);
1561
1562static struct device_attribute *mass_storage_function_attributes[] = {
1563 &dev_attr_inquiry_string,
1564 NULL
1565};
1566
1567static struct android_usb_function mass_storage_function = {
1568 .name = "mass_storage",
1569 .init = mass_storage_function_init,
1570 .cleanup = mass_storage_function_cleanup,
1571 .bind_config = mass_storage_function_bind_config,
1572 .attributes = mass_storage_function_attributes,
1573};
1574
1575
Benoit Gobycf3fc062011-12-19 14:39:37 -08001576static int accessory_function_init(struct android_usb_function *f,
1577 struct usb_composite_dev *cdev)
1578{
1579 return acc_setup();
1580}
1581
1582static void accessory_function_cleanup(struct android_usb_function *f)
1583{
1584 acc_cleanup();
1585}
1586
1587static int accessory_function_bind_config(struct android_usb_function *f,
1588 struct usb_configuration *c)
1589{
1590 return acc_bind_config(c);
1591}
1592
1593static int accessory_function_ctrlrequest(struct android_usb_function *f,
1594 struct usb_composite_dev *cdev,
1595 const struct usb_ctrlrequest *c)
1596{
1597 return acc_ctrlrequest(cdev, c);
1598}
1599
1600static struct android_usb_function accessory_function = {
1601 .name = "accessory",
1602 .init = accessory_function_init,
1603 .cleanup = accessory_function_cleanup,
1604 .bind_config = accessory_function_bind_config,
1605 .ctrlrequest = accessory_function_ctrlrequest,
1606};
1607
Ajay Dudani34b1e302012-08-27 16:43:53 +05301608static int audio_source_function_init(struct android_usb_function *f,
1609 struct usb_composite_dev *cdev)
1610{
1611 struct audio_source_config *config;
1612
1613 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
1614 if (!config)
1615 return -ENOMEM;
1616 config->card = -1;
1617 config->device = -1;
1618 f->config = config;
1619 return 0;
1620}
1621
1622static void audio_source_function_cleanup(struct android_usb_function *f)
1623{
1624 kfree(f->config);
1625}
1626
1627static int audio_source_function_bind_config(struct android_usb_function *f,
1628 struct usb_configuration *c)
1629{
1630 struct audio_source_config *config = f->config;
1631
1632 return audio_source_bind_config(c, config);
1633}
1634
1635static void audio_source_function_unbind_config(struct android_usb_function *f,
1636 struct usb_configuration *c)
1637{
1638 struct audio_source_config *config = f->config;
1639
1640 config->card = -1;
1641 config->device = -1;
1642}
1643
1644static ssize_t audio_source_pcm_show(struct device *dev,
1645 struct device_attribute *attr, char *buf)
1646{
1647 struct android_usb_function *f = dev_get_drvdata(dev);
1648 struct audio_source_config *config = f->config;
1649
1650 /* print PCM card and device numbers */
1651 return sprintf(buf, "%d %d\n", config->card, config->device);
1652}
1653
1654static DEVICE_ATTR(pcm, S_IRUGO | S_IWUSR, audio_source_pcm_show, NULL);
1655
1656static struct device_attribute *audio_source_function_attributes[] = {
1657 &dev_attr_pcm,
1658 NULL
1659};
1660
1661static struct android_usb_function audio_source_function = {
1662 .name = "audio_source",
1663 .init = audio_source_function_init,
1664 .cleanup = audio_source_function_cleanup,
1665 .bind_config = audio_source_function_bind_config,
1666 .unbind_config = audio_source_function_unbind_config,
1667 .attributes = audio_source_function_attributes,
1668};
1669
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301670static int android_uasp_connect_cb(bool connect)
1671{
1672 /*
1673 * TODO
1674 * We may have to disable gadget till UASP configfs nodes
1675 * are configured which includes mapping LUN with the
1676 * backing file. It is a fundamental difference between
1677 * f_mass_storage and f_tcp. That means UASP can not be
1678 * in default composition.
1679 *
1680 * For now, assume that UASP configfs nodes are configured
1681 * before enabling android gadget. Or cable should be
1682 * reconnected after mapping the LUN.
1683 *
1684 * Also consider making UASP to respond to Host requests when
1685 * Lun is not mapped.
1686 */
1687 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1688
1689 return 0;
1690}
1691
1692static int uasp_function_init(struct android_usb_function *f,
1693 struct usb_composite_dev *cdev)
1694{
1695 return f_tcm_init(&android_uasp_connect_cb);
1696}
1697
1698static void uasp_function_cleanup(struct android_usb_function *f)
1699{
1700 f_tcm_exit();
1701}
1702
1703static int uasp_function_bind_config(struct android_usb_function *f,
1704 struct usb_configuration *c)
1705{
1706 return tcm_bind_config(c);
1707}
1708
1709static struct android_usb_function uasp_function = {
1710 .name = "uasp",
1711 .init = uasp_function_init,
1712 .cleanup = uasp_function_cleanup,
1713 .bind_config = uasp_function_bind_config,
1714};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001715
Benoit Goby1e8ce152011-12-12 13:01:23 -08001716static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001717 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001718 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301719 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301720 &rmnet_sdio_function,
1721 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301722 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001724 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301725 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001726 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301727 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001728 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001729 &mtp_function,
1730 &ptp_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001731#ifdef CONFIG_USB_ANDROID_CDC_ECM
1732 &ecm_function,
1733#else
Benoit Goby1e8ce152011-12-12 13:01:23 -08001734 &rndis_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001735#endif
Ofir Cohenaef90b72012-07-31 12:37:04 +02001736 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001737 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001738 &accessory_function,
Ajay Dudani34b1e302012-08-27 16:43:53 +05301739 &audio_source_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301740 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001741 NULL
1742};
1743
Lena Salmand092f2d2012-03-12 17:27:24 +02001744static void android_cleanup_functions(struct android_usb_function **functions)
1745{
1746 struct android_usb_function *f;
1747 struct device_attribute **attrs;
1748 struct device_attribute *attr;
1749
1750 while (*functions) {
1751 f = *functions++;
1752
1753 if (f->dev) {
1754 device_destroy(android_class, f->dev->devt);
1755 kfree(f->dev_name);
1756 } else
1757 continue;
1758
1759 if (f->cleanup)
1760 f->cleanup(f);
1761
1762 attrs = f->attributes;
1763 if (attrs) {
1764 while ((attr = *attrs++))
1765 device_remove_file(f->dev, attr);
1766 }
1767 }
1768}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001769
1770static int android_init_functions(struct android_usb_function **functions,
1771 struct usb_composite_dev *cdev)
1772{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001773 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001774 struct android_usb_function *f;
1775 struct device_attribute **attrs;
1776 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301777 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001778 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001779
1780 for (; (f = *functions++); index++) {
1781 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001782 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001783 if (!f->dev_name) {
1784 err = -ENOMEM;
1785 goto err_out;
1786 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001787 f->dev = device_create(android_class, dev->dev,
1788 MKDEV(0, index), f, f->dev_name);
1789 if (IS_ERR(f->dev)) {
1790 pr_err("%s: Failed to create dev %s", __func__,
1791 f->dev_name);
1792 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001793 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001794 goto err_create;
1795 }
1796
1797 if (f->init) {
1798 err = f->init(f, cdev);
1799 if (err) {
1800 pr_err("%s: Failed to init %s", __func__,
1801 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001802 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001803 }
1804 }
1805
1806 attrs = f->attributes;
1807 if (attrs) {
1808 while ((attr = *attrs++) && !err)
1809 err = device_create_file(f->dev, attr);
1810 }
1811 if (err) {
1812 pr_err("%s: Failed to create function %s attributes",
1813 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001814 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001815 }
1816 }
1817 return 0;
1818
Lena Salmand092f2d2012-03-12 17:27:24 +02001819err_attrs:
1820 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1821 device_remove_file(f->dev, attr);
1822 if (f->cleanup)
1823 f->cleanup(f);
1824err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001825 device_destroy(android_class, f->dev->devt);
1826err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001827 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001828 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001829err_out:
1830 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001831 return err;
1832}
1833
Benoit Goby1e8ce152011-12-12 13:01:23 -08001834static int
1835android_bind_enabled_functions(struct android_dev *dev,
1836 struct usb_configuration *c)
1837{
1838 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001839 struct android_configuration *conf =
1840 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001841 int ret;
1842
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001843 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001844 ret = f->bind_config(f, c);
1845 if (ret) {
1846 pr_err("%s: %s failed", __func__, f->name);
1847 return ret;
1848 }
1849 }
1850 return 0;
1851}
1852
1853static void
1854android_unbind_enabled_functions(struct android_dev *dev,
1855 struct usb_configuration *c)
1856{
1857 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001858 struct android_configuration *conf =
1859 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001860
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001861 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001862 if (f->unbind_config)
1863 f->unbind_config(f, c);
1864 }
1865}
1866
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001867static int android_enable_function(struct android_dev *dev,
1868 struct android_configuration *conf,
1869 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001870{
1871 struct android_usb_function **functions = dev->functions;
1872 struct android_usb_function *f;
1873 while ((f = *functions++)) {
1874 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001875 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001876 pr_err("%s already enabled in other " \
1877 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001878 f->name);
1879 else {
1880 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001881 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001882 f->android_dev = dev;
1883 return 0;
1884 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001885 }
1886 }
1887 return -EINVAL;
1888}
1889
1890/*-------------------------------------------------------------------------*/
1891/* /sys/class/android_usb/android%d/ interface */
1892
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301893static ssize_t remote_wakeup_show(struct device *pdev,
1894 struct device_attribute *attr, char *buf)
1895{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001896 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001897 struct android_configuration *conf;
1898
1899 /*
1900 * Show the wakeup attribute of the first configuration,
1901 * since all configurations have the same wakeup attribute
1902 */
1903 if (dev->configs_num == 0)
1904 return 0;
1905 conf = list_entry(dev->configs.next,
1906 struct android_configuration,
1907 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001908
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301909 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001910 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301911 USB_CONFIG_ATT_WAKEUP));
1912}
1913
1914static ssize_t remote_wakeup_store(struct device *pdev,
1915 struct device_attribute *attr, const char *buff, size_t size)
1916{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001917 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001918 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301919 int enable = 0;
1920
1921 sscanf(buff, "%d", &enable);
1922
1923 pr_debug("android_usb: %s remote wakeup\n",
1924 enable ? "enabling" : "disabling");
1925
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001926 list_for_each_entry(conf, &dev->configs, list_item)
1927 if (enable)
1928 conf->usb_config.bmAttributes |=
1929 USB_CONFIG_ATT_WAKEUP;
1930 else
1931 conf->usb_config.bmAttributes &=
1932 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301933
1934 return size;
1935}
1936
Benoit Goby1e8ce152011-12-12 13:01:23 -08001937static ssize_t
1938functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1939{
1940 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001941 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001942 struct android_usb_function *f;
1943 char *buff = buf;
1944
1945 mutex_lock(&dev->mutex);
1946
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001947 list_for_each_entry(conf, &dev->configs, list_item) {
1948 if (buff != buf)
1949 *(buff-1) = ':';
1950 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1951 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
1952 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001953
1954 mutex_unlock(&dev->mutex);
1955
1956 if (buff != buf)
1957 *(buff-1) = '\n';
1958 return buff - buf;
1959}
1960
1961static ssize_t
1962functions_store(struct device *pdev, struct device_attribute *attr,
1963 const char *buff, size_t size)
1964{
1965 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001966 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001967 struct list_head *curr_conf = &dev->configs;
1968 struct android_configuration *conf;
1969 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001970 char *name;
1971 char buf[256], *b;
1972 int err;
1973
1974 mutex_lock(&dev->mutex);
1975
1976 if (dev->enabled) {
1977 mutex_unlock(&dev->mutex);
1978 return -EBUSY;
1979 }
1980
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001981 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001982 list_for_each_entry(conf, &dev->configs, list_item) {
1983 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1984 f->android_dev = NULL;
1985 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001986 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001987
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301988 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001989 b = strim(buf);
1990
1991 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001992 conf_str = strsep(&b, ":");
1993 if (conf_str) {
1994 /* If the next not equal to the head, take it */
1995 if (curr_conf->next != &dev->configs)
1996 conf = list_entry(curr_conf->next,
1997 struct android_configuration,
1998 list_item);
1999 else
2000 conf = alloc_android_config(dev);
2001
2002 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002003 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002004
2005 while (conf_str) {
2006 name = strsep(&conf_str, ",");
2007 if (name) {
2008 err = android_enable_function(dev, conf, name);
2009 if (err)
2010 pr_err("android_usb: Cannot enable %s",
2011 name);
2012 }
2013 }
2014 }
2015
2016 /* Free uneeded configurations if exists */
2017 while (curr_conf->next != &dev->configs) {
2018 conf = list_entry(curr_conf->next,
2019 struct android_configuration, list_item);
2020 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002021 }
2022
2023 mutex_unlock(&dev->mutex);
2024
2025 return size;
2026}
2027
2028static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
2029 char *buf)
2030{
2031 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002032
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302033 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002034}
2035
2036static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
2037 const char *buff, size_t size)
2038{
2039 struct android_dev *dev = dev_get_drvdata(pdev);
2040 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07002041 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002042 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002043 int enabled = 0;
2044
Benoit Gobycf3fc062011-12-19 14:39:37 -08002045 if (!cdev)
2046 return -ENODEV;
2047
Benoit Goby1e8ce152011-12-12 13:01:23 -08002048 mutex_lock(&dev->mutex);
2049
2050 sscanf(buff, "%d", &enabled);
2051 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08002052 /*
2053 * Update values in composite driver's copy of
2054 * device descriptor.
2055 */
2056 cdev->desc.idVendor = device_desc.idVendor;
2057 cdev->desc.idProduct = device_desc.idProduct;
2058 cdev->desc.bcdDevice = device_desc.bcdDevice;
2059 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
2060 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
2061 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002062 list_for_each_entry(conf, &dev->configs, list_item)
2063 list_for_each_entry(f, &conf->enabled_functions,
2064 enabled_list) {
2065 if (f->enable)
2066 f->enable(f);
2067 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07002068 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002069 dev->enabled = true;
2070 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002071 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002072 list_for_each_entry(conf, &dev->configs, list_item)
2073 list_for_each_entry(f, &conf->enabled_functions,
2074 enabled_list) {
2075 if (f->disable)
2076 f->disable(f);
2077 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002078 dev->enabled = false;
2079 } else {
2080 pr_err("android_usb: already %s\n",
2081 dev->enabled ? "enabled" : "disabled");
2082 }
2083
2084 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002085
Benoit Gobyaab96812011-04-19 20:37:33 -07002086 return size;
2087}
2088
Ofir Cohen94213a72012-05-03 14:26:32 +03002089static ssize_t pm_qos_show(struct device *pdev,
2090 struct device_attribute *attr, char *buf)
2091{
2092 struct android_dev *dev = dev_get_drvdata(pdev);
2093
2094 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2095}
2096
2097static ssize_t pm_qos_store(struct device *pdev,
2098 struct device_attribute *attr,
2099 const char *buff, size_t size)
2100{
2101 struct android_dev *dev = dev_get_drvdata(pdev);
2102
2103 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2104
Benoit Goby1e8ce152011-12-12 13:01:23 -08002105 return size;
2106}
2107
2108static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2109 char *buf)
2110{
2111 struct android_dev *dev = dev_get_drvdata(pdev);
2112 struct usb_composite_dev *cdev = dev->cdev;
2113 char *state = "DISCONNECTED";
2114 unsigned long flags;
2115
2116 if (!cdev)
2117 goto out;
2118
2119 spin_lock_irqsave(&cdev->lock, flags);
2120 if (cdev->config)
2121 state = "CONFIGURED";
2122 else if (dev->connected)
2123 state = "CONNECTED";
2124 spin_unlock_irqrestore(&cdev->lock, flags);
2125out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302126 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002127}
2128
2129#define DESCRIPTOR_ATTR(field, format_string) \
2130static ssize_t \
2131field ## _show(struct device *dev, struct device_attribute *attr, \
2132 char *buf) \
2133{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302134 return snprintf(buf, PAGE_SIZE, \
2135 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002136} \
2137static ssize_t \
2138field ## _store(struct device *dev, struct device_attribute *attr, \
2139 const char *buf, size_t size) \
2140{ \
2141 int value; \
2142 if (sscanf(buf, format_string, &value) == 1) { \
2143 device_desc.field = value; \
2144 return size; \
2145 } \
2146 return -1; \
2147} \
2148static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2149
2150#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2151static ssize_t \
2152field ## _show(struct device *dev, struct device_attribute *attr, \
2153 char *buf) \
2154{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302155 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002156} \
2157static ssize_t \
2158field ## _store(struct device *dev, struct device_attribute *attr, \
2159 const char *buf, size_t size) \
2160{ \
2161 if (size >= sizeof(buffer)) \
2162 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302163 strlcpy(buffer, buf, sizeof(buffer)); \
2164 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302165 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002166} \
2167static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2168
2169
2170DESCRIPTOR_ATTR(idVendor, "%04x\n")
2171DESCRIPTOR_ATTR(idProduct, "%04x\n")
2172DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2173DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2174DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2175DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2176DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2177DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2178DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2179
2180static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2181 functions_store);
2182static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002183static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2184 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002185static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302186static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2187 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002188
2189static struct device_attribute *android_usb_attributes[] = {
2190 &dev_attr_idVendor,
2191 &dev_attr_idProduct,
2192 &dev_attr_bcdDevice,
2193 &dev_attr_bDeviceClass,
2194 &dev_attr_bDeviceSubClass,
2195 &dev_attr_bDeviceProtocol,
2196 &dev_attr_iManufacturer,
2197 &dev_attr_iProduct,
2198 &dev_attr_iSerial,
2199 &dev_attr_functions,
2200 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002201 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002202 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302203 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002204 NULL
2205};
2206
2207/*-------------------------------------------------------------------------*/
2208/* Composite driver */
2209
2210static int android_bind_config(struct usb_configuration *c)
2211{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002212 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002213 int ret = 0;
2214
2215 ret = android_bind_enabled_functions(dev, c);
2216 if (ret)
2217 return ret;
2218
2219 return 0;
2220}
2221
2222static void android_unbind_config(struct usb_configuration *c)
2223{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002224 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002225
2226 android_unbind_enabled_functions(dev, c);
2227}
2228
2229static int android_bind(struct usb_composite_dev *cdev)
2230{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002231 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002232 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002233 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002234 int gcnum, id, ret;
2235
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002236 /* Bind to the last android_dev that was probed */
2237 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2238
2239 dev->cdev = cdev;
2240
Benoit Goby1e8ce152011-12-12 13:01:23 -08002241 /*
2242 * Start disconnected. Userspace will connect the gadget once
2243 * it is done configuring the functions.
2244 */
2245 usb_gadget_disconnect(gadget);
2246
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002247 /* Init the supported functions only once, on the first android_dev */
2248 if (android_dev_count == 1) {
2249 ret = android_init_functions(dev->functions, cdev);
2250 if (ret)
2251 return ret;
2252 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002253
2254 /* Allocate string descriptor numbers ... note that string
2255 * contents can be overridden by the composite_dev glue.
2256 */
2257 id = usb_string_id(cdev);
2258 if (id < 0)
2259 return id;
2260 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2261 device_desc.iManufacturer = id;
2262
2263 id = usb_string_id(cdev);
2264 if (id < 0)
2265 return id;
2266 strings_dev[STRING_PRODUCT_IDX].id = id;
2267 device_desc.iProduct = id;
2268
2269 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302270 strlcpy(manufacturer_string, "Android",
2271 sizeof(manufacturer_string) - 1);
2272 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2273 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002274
2275 id = usb_string_id(cdev);
2276 if (id < 0)
2277 return id;
2278 strings_dev[STRING_SERIAL_IDX].id = id;
2279 device_desc.iSerialNumber = id;
2280
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302281 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002282 list_for_each_entry(conf, &dev->configs, list_item)
2283 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302284
Benoit Goby1e8ce152011-12-12 13:01:23 -08002285 gcnum = usb_gadget_controller_number(gadget);
2286 if (gcnum >= 0)
2287 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2288 else {
2289 pr_warning("%s: controller '%s' not recognized\n",
2290 longname, gadget->name);
2291 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2292 }
2293
Benoit Goby1e8ce152011-12-12 13:01:23 -08002294 return 0;
2295}
2296
2297static int android_usb_unbind(struct usb_composite_dev *cdev)
2298{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002299 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002300
Lena Salmand092f2d2012-03-12 17:27:24 +02002301 manufacturer_string[0] = '\0';
2302 product_string[0] = '\0';
2303 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002304 cancel_work_sync(&dev->work);
2305 android_cleanup_functions(dev->functions);
2306 return 0;
2307}
2308
2309static struct usb_composite_driver android_usb_driver = {
2310 .name = "android_usb",
2311 .dev = &device_desc,
2312 .strings = dev_strings,
2313 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002314 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002315};
2316
2317static int
2318android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2319{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002320 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002321 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002322 struct usb_request *req = cdev->req;
2323 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002324 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002325 int value = -EOPNOTSUPP;
2326 unsigned long flags;
2327
2328 req->zero = 0;
2329 req->complete = composite_setup_complete;
2330 req->length = 0;
2331 gadget->ep0->driver_data = cdev;
2332
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002333 list_for_each_entry(conf, &dev->configs, list_item)
taeju.park257b5082012-10-09 10:52:01 +09002334 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
2335 if (f->ctrlrequest) {
2336 value = f->ctrlrequest(f, cdev, c);
2337 if (value >= 0)
2338 break;
2339 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002340
Benoit Gobycf3fc062011-12-19 14:39:37 -08002341 /* Special case the accessory function.
2342 * It needs to handle control requests before it is enabled.
2343 */
2344 if (value < 0)
2345 value = acc_ctrlrequest(cdev, c);
2346
Benoit Goby1e8ce152011-12-12 13:01:23 -08002347 if (value < 0)
2348 value = composite_setup(gadget, c);
2349
2350 spin_lock_irqsave(&cdev->lock, flags);
2351 if (!dev->connected) {
2352 dev->connected = 1;
2353 schedule_work(&dev->work);
2354 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2355 cdev->config) {
2356 schedule_work(&dev->work);
2357 }
2358 spin_unlock_irqrestore(&cdev->lock, flags);
2359
2360 return value;
2361}
2362
2363static void android_disconnect(struct usb_gadget *gadget)
2364{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002365 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002366 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002367 unsigned long flags;
2368
2369 composite_disconnect(gadget);
Ajay Dudanic2f0cc72012-08-27 16:23:48 +05302370 /* accessory HID support can be active while the
2371 accessory function is not actually enabled,
2372 so we need to inform it when we are disconnected.
2373 */
2374 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002375
2376 spin_lock_irqsave(&cdev->lock, flags);
2377 dev->connected = 0;
2378 schedule_work(&dev->work);
2379 spin_unlock_irqrestore(&cdev->lock, flags);
2380}
2381
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002382static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002383{
2384 struct device_attribute **attrs = android_usb_attributes;
2385 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002386 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002387 int err;
2388
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002389 /*
2390 * The primary usb core should always have usb_core_id=0, since
2391 * Android user space is currently interested in android0 events.
2392 */
2393 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2394 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002395 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002396 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002397 if (IS_ERR(dev->dev))
2398 return PTR_ERR(dev->dev);
2399
2400 dev_set_drvdata(dev->dev, dev);
2401
2402 while ((attr = *attrs++)) {
2403 err = device_create_file(dev->dev, attr);
2404 if (err) {
2405 device_destroy(android_class, dev->dev->devt);
2406 return err;
2407 }
2408 }
2409 return 0;
2410}
2411
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302412static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002413{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302414 struct device_attribute **attrs = android_usb_attributes;
2415 struct device_attribute *attr;
2416
2417 while ((attr = *attrs++))
2418 device_remove_file(dev->dev, attr);
2419 device_destroy(android_class, dev->dev->devt);
2420}
2421
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002422static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2423{
2424 struct android_dev *dev = NULL;
2425
2426 /* Find the android dev from the list */
2427 list_for_each_entry(dev, &android_dev_list, list_item) {
2428 if (dev->cdev == cdev)
2429 break;
2430 }
2431
2432 return dev;
2433}
2434
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002435static struct android_configuration *alloc_android_config
2436 (struct android_dev *dev)
2437{
2438 struct android_configuration *conf;
2439
2440 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2441 if (!conf) {
2442 pr_err("%s(): Failed to alloc memory for android conf\n",
2443 __func__);
2444 return ERR_PTR(-ENOMEM);
2445 }
2446
2447 dev->configs_num++;
2448 conf->usb_config.label = dev->name;
2449 conf->usb_config.unbind = android_unbind_config;
2450 conf->usb_config.bConfigurationValue = dev->configs_num;
2451
2452 INIT_LIST_HEAD(&conf->enabled_functions);
2453
2454 list_add_tail(&conf->list_item, &dev->configs);
2455
2456 return conf;
2457}
2458
2459static void free_android_config(struct android_dev *dev,
2460 struct android_configuration *conf)
2461{
2462 list_del(&conf->list_item);
2463 dev->configs_num--;
2464 kfree(conf);
2465}
2466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467static int __devinit android_probe(struct platform_device *pdev)
2468{
2469 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002470 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002471 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002473 if (!android_class) {
2474 android_class = class_create(THIS_MODULE, "android_usb");
2475 if (IS_ERR(android_class))
2476 return PTR_ERR(android_class);
2477 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002478
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002479 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2480 if (!android_dev) {
2481 pr_err("%s(): Failed to alloc memory for android_dev\n",
2482 __func__);
2483 ret = -ENOMEM;
2484 goto err_alloc;
2485 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002486
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002487 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002488 android_dev->disable_depth = 1;
2489 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002490 android_dev->configs_num = 0;
2491 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002492 INIT_WORK(&android_dev->work, android_work);
2493 mutex_init(&android_dev->mutex);
2494
2495 android_dev->pdata = pdata;
2496
2497 list_add_tail(&android_dev->list_item, &android_dev_list);
2498 android_dev_count++;
2499
2500 if (pdata)
2501 composite_driver.usb_core_id = pdata->usb_core_id;
2502 else
2503 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2504
2505 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302506 if (ret) {
2507 pr_err("%s(): android_create_device failed\n", __func__);
2508 goto err_dev;
2509 }
2510
Lena Salmand092f2d2012-03-12 17:27:24 +02002511 ret = usb_composite_probe(&android_usb_driver, android_bind);
2512 if (ret) {
2513 pr_err("%s(): Failed to register android "
2514 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302515 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002516 }
2517
Ofir Cohen94213a72012-05-03 14:26:32 +03002518 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302519 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002520 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002521 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002522 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002523
Lena Salmand092f2d2012-03-12 17:27:24 +02002524 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302525err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002526 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302527err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002528 list_del(&android_dev->list_item);
2529 android_dev_count--;
2530 kfree(android_dev);
2531err_alloc:
2532 if (list_empty(&android_dev_list)) {
2533 class_destroy(android_class);
2534 android_class = NULL;
2535 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302536 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002537}
2538
2539static int android_remove(struct platform_device *pdev)
2540{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002541 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002542 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002543 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302544
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002545 if (pdata)
2546 usb_core_id = pdata->usb_core_id;
2547
2548 /* Find the android dev from the list */
2549 list_for_each_entry(dev, &android_dev_list, list_item) {
2550 if (!dev->pdata)
2551 break; /*To backward compatibility*/
2552 if (dev->pdata->usb_core_id == usb_core_id)
2553 break;
2554 }
2555
2556 if (dev) {
2557 android_destroy_device(dev);
2558 if (pdata && pdata->swfi_latency)
2559 pm_qos_remove_request(&dev->pm_qos_req_dma);
2560 list_del(&dev->list_item);
2561 android_dev_count--;
2562 kfree(dev);
2563 }
2564
2565 if (list_empty(&android_dev_list)) {
2566 class_destroy(android_class);
2567 android_class = NULL;
2568 usb_composite_unregister(&android_usb_driver);
2569 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002571 return 0;
2572}
2573
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002574static const struct platform_device_id android_id_table[] __devinitconst = {
2575 {
2576 .name = "android_usb",
2577 },
2578 {
2579 .name = "android_usb_hsic",
2580 },
2581};
2582
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002583static struct platform_driver android_platform_driver = {
2584 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002585 .probe = android_probe,
2586 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002587 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002589
2590static int __init init(void)
2591{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302592 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002593
Benoit Goby1e8ce152011-12-12 13:01:23 -08002594 /* Override composite driver functions */
2595 composite_driver.setup = android_setup;
2596 composite_driver.disconnect = android_disconnect;
2597
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002598 INIT_LIST_HEAD(&android_dev_list);
2599 android_dev_count = 0;
2600
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302601 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302602 if (ret) {
2603 pr_err("%s(): Failed to register android"
2604 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302605 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002606
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302607 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002608}
2609module_init(init);
2610
2611static void __exit cleanup(void)
2612{
Lena Salmand092f2d2012-03-12 17:27:24 +02002613 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002614}
2615module_exit(cleanup);