blob: f728295909ec2099c909f6c003ab1b253a774851 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
34
35/*
36 * Kbuild is not very cooperative with respect to linking separately
37 * compiled library objects into one module. So for now we won't use
38 * separate compilation ... ensuring init/exit sections work to shrink
39 * the runtime footprint, and giving us at least some parts of what
40 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 */
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45#include "composite.c"
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "f_diag.c"
Shimrit Malichia00d7322012-08-05 13:56:28 +030048#include "f_qdss.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053049#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053050#include "f_rmnet_sdio.c"
51#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053052#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080053#include "f_mass_storage.c"
54#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053055#include "u_sdio.c"
56#include "u_smd.c"
57#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053058#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080059#include "u_ctrl_hsic.c"
60#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053061#include "u_ctrl_hsuart.c"
62#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053063#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080064#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080065#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053066#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080067#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080068#include "f_accessory.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070069#ifdef CONFIG_USB_ANDROID_CDC_ECM
70#include "f_ecm.c"
71#else
Benoit Goby1e8ce152011-12-12 13:01:23 -080072#define USB_ETH_RNDIS y
73#include "f_rndis.c"
74#include "rndis.c"
Devin Kim0c5b9ce2012-06-19 21:34:44 -070075#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080076#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030077#include "u_bam_data.c"
78#include "f_mbim.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030079#include "f_qc_ecm.c"
Ofir Cohenaef90b72012-07-31 12:37:04 +020080#include "f_qc_rndis.c"
Ofir Cohen7b155422012-07-31 13:02:49 +030081#include "u_qc_ether.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053082#ifdef CONFIG_TARGET_CORE
83#include "f_tcm.c"
84#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080085
86MODULE_AUTHOR("Mike Lockwood");
87MODULE_DESCRIPTION("Android Composite USB Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION("1.0");
90
91static const char longname[] = "Gadget Android";
92
93/* Default vendor and product IDs, overridden by userspace */
94#define VENDOR_ID 0x18D1
95#define PRODUCT_ID 0x0001
96
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030097#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
98
Benoit Goby1e8ce152011-12-12 13:01:23 -080099struct android_usb_function {
100 char *name;
101 void *config;
102
103 struct device *dev;
104 char *dev_name;
105 struct device_attribute **attributes;
106
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300107 /* for android_conf.enabled_functions */
Benoit Goby1e8ce152011-12-12 13:01:23 -0800108 struct list_head enabled_list;
109
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300110 struct android_dev *android_dev;
111
Benoit Goby1e8ce152011-12-12 13:01:23 -0800112 /* Optional: initialization during gadget bind */
113 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
114 /* Optional: cleanup during gadget unbind */
115 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700116 /* Optional: called when the function is added the list of
117 * enabled functions */
118 void (*enable)(struct android_usb_function *);
119 /* Optional: called when it is removed */
120 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800121
122 int (*bind_config)(struct android_usb_function *,
123 struct usb_configuration *);
124
125 /* Optional: called when the configuration is removed */
126 void (*unbind_config)(struct android_usb_function *,
127 struct usb_configuration *);
128 /* Optional: handle ctrl requests before the device is configured */
129 int (*ctrlrequest)(struct android_usb_function *,
130 struct usb_composite_dev *,
131 const struct usb_ctrlrequest *);
132};
133
134struct android_dev {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300135 const char *name;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800136 struct android_usb_function **functions;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800137 struct usb_composite_dev *cdev;
138 struct device *dev;
139
140 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700141 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800142 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700143 struct android_usb_platform_data *pdata;
144
Benoit Goby1e8ce152011-12-12 13:01:23 -0800145 bool connected;
146 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300147 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700148 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800149 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300150
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300151 /* A list of struct android_configuration */
152 struct list_head configs;
153 int configs_num;
154
155 /* A list node inside the android_dev_list */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300156 struct list_head list_item;
157
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300158};
159
160struct android_configuration {
161 struct usb_configuration usb_config;
162
163 /* A list of the functions supported by this config */
164 struct list_head enabled_functions;
165
166 /* A list node inside the struct android_dev.configs list */
167 struct list_head list_item;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800168};
169
170static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300171static struct list_head android_dev_list;
172static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800173static int android_bind_config(struct usb_configuration *c);
174static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300175static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300176static struct android_configuration *alloc_android_config
177 (struct android_dev *dev);
178static void free_android_config(struct android_dev *dev,
179 struct android_configuration *conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800180
181/* string IDs are assigned dynamically */
182#define STRING_MANUFACTURER_IDX 0
183#define STRING_PRODUCT_IDX 1
184#define STRING_SERIAL_IDX 2
185
186static char manufacturer_string[256];
187static char product_string[256];
188static char serial_string[256];
189
190/* String Table */
191static struct usb_string strings_dev[] = {
192 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
193 [STRING_PRODUCT_IDX].s = product_string,
194 [STRING_SERIAL_IDX].s = serial_string,
195 { } /* end of list */
196};
197
198static struct usb_gadget_strings stringtab_dev = {
199 .language = 0x0409, /* en-us */
200 .strings = strings_dev,
201};
202
203static struct usb_gadget_strings *dev_strings[] = {
204 &stringtab_dev,
205 NULL,
206};
207
208static struct usb_device_descriptor device_desc = {
209 .bLength = sizeof(device_desc),
210 .bDescriptorType = USB_DT_DEVICE,
211 .bcdUSB = __constant_cpu_to_le16(0x0200),
212 .bDeviceClass = USB_CLASS_PER_INTERFACE,
213 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
214 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
215 .bcdDevice = __constant_cpu_to_le16(0xffff),
216 .bNumConfigurations = 1,
217};
218
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530219static struct usb_otg_descriptor otg_descriptor = {
220 .bLength = sizeof otg_descriptor,
221 .bDescriptorType = USB_DT_OTG,
222 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
223 .bcdOTG = __constant_cpu_to_le16(0x0200),
224};
225
226static const struct usb_descriptor_header *otg_desc[] = {
227 (struct usb_descriptor_header *) &otg_descriptor,
228 NULL,
229};
230
Manu Gautama2b54142012-04-03 14:34:32 +0530231enum android_device_state {
232 USB_DISCONNECTED,
233 USB_CONNECTED,
234 USB_CONFIGURED,
235};
236
Ofir Cohen94213a72012-05-03 14:26:32 +0300237static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
238{
239 struct android_usb_platform_data *pdata = dev->pdata;
240 u32 swfi_latency = 0;
241 static int last_vote = -1;
242
Ofir Cohen56eb7072012-05-20 11:41:39 +0300243 if (!pdata || vote == last_vote
244 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300245 return;
246
247 swfi_latency = pdata->swfi_latency + 1;
248 if (vote)
249 pm_qos_update_request(&dev->pm_qos_req_dma,
250 swfi_latency);
251 else
252 pm_qos_update_request(&dev->pm_qos_req_dma,
253 PM_QOS_DEFAULT_VALUE);
254 last_vote = vote;
255}
256
Benoit Goby1e8ce152011-12-12 13:01:23 -0800257static void android_work(struct work_struct *data)
258{
259 struct android_dev *dev = container_of(data, struct android_dev, work);
260 struct usb_composite_dev *cdev = dev->cdev;
261 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
262 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
263 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
264 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530265 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800266 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300267 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800268
269 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530270 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800271 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530272 next_state = USB_CONFIGURED;
273 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800274 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530275 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300276 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300277 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300278 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300279 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530280 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800281 dev->sw_connected = dev->connected;
282 spin_unlock_irqrestore(&cdev->lock, flags);
283
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300284 if (pm_qos_vote != -1)
285 android_pm_qos_update_latency(dev, pm_qos_vote);
286
Benoit Goby1e8ce152011-12-12 13:01:23 -0800287 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530288 /*
289 * Some userspace modules, e.g. MTP, work correctly only if
290 * CONFIGURED uevent is preceded by DISCONNECT uevent.
291 * Check if we missed sending out a DISCONNECT uevent. This can
292 * happen if host PC resets and configures device really quick.
293 */
294 if (((uevent_envp == connected) &&
295 (last_uevent != USB_DISCONNECTED)) ||
296 ((uevent_envp == configured) &&
297 (last_uevent == USB_CONFIGURED))) {
298 pr_info("%s: sent missed DISCONNECT event\n", __func__);
299 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
300 disconnected);
301 msleep(20);
302 }
303 /*
304 * Before sending out CONFIGURED uevent give function drivers
305 * a chance to wakeup userspace threads and notify disconnect
306 */
307 if (uevent_envp == configured)
308 msleep(50);
309
Benoit Goby1e8ce152011-12-12 13:01:23 -0800310 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530311 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800312 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
313 } else {
314 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
315 dev->connected, dev->sw_connected, cdev->config);
316 }
317}
318
Benoit Goby80ba14d2012-03-19 18:56:52 -0700319static void android_enable(struct android_dev *dev)
320{
321 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300322 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700323
324 if (WARN_ON(!dev->disable_depth))
325 return;
326
327 if (--dev->disable_depth == 0) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300328
329 list_for_each_entry(conf, &dev->configs, list_item)
330 usb_add_config(cdev, &conf->usb_config,
331 android_bind_config);
332
Benoit Goby80ba14d2012-03-19 18:56:52 -0700333 usb_gadget_connect(cdev->gadget);
334 }
335}
336
337static void android_disable(struct android_dev *dev)
338{
339 struct usb_composite_dev *cdev = dev->cdev;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300340 struct android_configuration *conf;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700341
342 if (dev->disable_depth++ == 0) {
343 usb_gadget_disconnect(cdev->gadget);
344 /* Cancel pending control requests */
345 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +0300346
347 list_for_each_entry(conf, &dev->configs, list_item)
348 usb_remove_config(cdev, &conf->usb_config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700349 }
350}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800351
352/*-------------------------------------------------------------------------*/
353/* Supported functions initialization */
354
Benoit Goby80ba14d2012-03-19 18:56:52 -0700355struct adb_data {
356 bool opened;
357 bool enabled;
358};
359
Benoit Goby2b6862d2011-12-19 14:38:41 -0800360static int
361adb_function_init(struct android_usb_function *f,
362 struct usb_composite_dev *cdev)
363{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700364 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
365 if (!f->config)
366 return -ENOMEM;
367
Benoit Goby2b6862d2011-12-19 14:38:41 -0800368 return adb_setup();
369}
370
371static void adb_function_cleanup(struct android_usb_function *f)
372{
373 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700374 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800375}
376
377static int
378adb_function_bind_config(struct android_usb_function *f,
379 struct usb_configuration *c)
380{
381 return adb_bind_config(c);
382}
383
Benoit Goby80ba14d2012-03-19 18:56:52 -0700384static void adb_android_function_enable(struct android_usb_function *f)
385{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300386 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387 struct adb_data *data = f->config;
388
389 data->enabled = true;
390
391 /* Disable the gadget until adbd is ready */
392 if (!data->opened)
393 android_disable(dev);
394}
395
396static void adb_android_function_disable(struct android_usb_function *f)
397{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300398 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700399 struct adb_data *data = f->config;
400
401 data->enabled = false;
402
403 /* Balance the disable that was called in closed_callback */
404 if (!data->opened)
405 android_enable(dev);
406}
407
Benoit Goby2b6862d2011-12-19 14:38:41 -0800408static struct android_usb_function adb_function = {
409 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700410 .enable = adb_android_function_enable,
411 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800412 .init = adb_function_init,
413 .cleanup = adb_function_cleanup,
414 .bind_config = adb_function_bind_config,
415};
416
Benoit Goby80ba14d2012-03-19 18:56:52 -0700417static void adb_ready_callback(void)
418{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300419 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700420 struct adb_data *data = adb_function.config;
421
Benoit Goby80ba14d2012-03-19 18:56:52 -0700422 data->opened = true;
423
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300424 if (data->enabled && dev) {
425 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700426 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300427 mutex_unlock(&dev->mutex);
428 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700429}
430
431static void adb_closed_callback(void)
432{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300433 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700434 struct adb_data *data = adb_function.config;
435
Benoit Goby80ba14d2012-03-19 18:56:52 -0700436 data->opened = false;
437
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300438 if (data->enabled) {
439 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700440 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300441 mutex_unlock(&dev->mutex);
442 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700443}
444
Benoit Goby2b6862d2011-12-19 14:38:41 -0800445
Benoit Gobyaab96812011-04-19 20:37:33 -0700446/*-------------------------------------------------------------------------*/
447/* Supported functions initialization */
448
Manu Gautam8e0719b2011-09-26 14:47:55 +0530449/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530450static int rmnet_smd_function_bind_config(struct android_usb_function *f,
451 struct usb_configuration *c)
452{
453 return rmnet_smd_bind_config(c);
454}
455
456static struct android_usb_function rmnet_smd_function = {
457 .name = "rmnet_smd",
458 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800459};
460
Manu Gautam8e0719b2011-09-26 14:47:55 +0530461/* RMNET_SDIO */
462static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
463 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800464{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530465 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800466}
467
Manu Gautam8e0719b2011-09-26 14:47:55 +0530468static struct android_usb_function rmnet_sdio_function = {
469 .name = "rmnet_sdio",
470 .bind_config = rmnet_sdio_function_bind_config,
471};
472
473/* RMNET_SMD_SDIO */
474static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
475 struct usb_composite_dev *cdev)
476{
477 return rmnet_smd_sdio_init();
478}
479
480static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
481{
482 rmnet_smd_sdio_cleanup();
483}
484
485static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
486 struct usb_configuration *c)
487{
488 return rmnet_smd_sdio_function_add(c);
489}
490
491static struct device_attribute *rmnet_smd_sdio_attributes[] = {
492 &dev_attr_transport, NULL };
493
494static struct android_usb_function rmnet_smd_sdio_function = {
495 .name = "rmnet_smd_sdio",
496 .init = rmnet_smd_sdio_function_init,
497 .cleanup = rmnet_smd_sdio_function_cleanup,
498 .bind_config = rmnet_smd_sdio_bind_config,
499 .attributes = rmnet_smd_sdio_attributes,
500};
501
Hemant Kumar1b820d52011-11-03 15:08:28 -0700502/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
503#define MAX_XPORT_STR_LEN 50
504static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530505
Manu Gautame3e897c2011-09-12 17:18:46 +0530506static void rmnet_function_cleanup(struct android_usb_function *f)
507{
508 frmnet_cleanup();
509}
510
Manu Gautam2b0234a2011-09-07 16:47:52 +0530511static int rmnet_function_bind_config(struct android_usb_function *f,
512 struct usb_configuration *c)
513{
514 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700515 int err = 0;
516 char *ctrl_name;
517 char *data_name;
518 char buf[MAX_XPORT_STR_LEN], *b;
519 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530520
Hemant Kumar1b820d52011-11-03 15:08:28 -0700521 if (!rmnet_initialized) {
522 rmnet_initialized = 1;
523 strlcpy(buf, rmnet_transports, sizeof(buf));
524 b = strim(buf);
525 while (b) {
526 ctrl_name = strsep(&b, ",");
527 data_name = strsep(&b, ",");
528 if (ctrl_name && data_name) {
529 err = frmnet_init_port(ctrl_name, data_name);
530 if (err) {
531 pr_err("rmnet: Cannot open ctrl port:"
532 "'%s' data port:'%s'\n",
533 ctrl_name, data_name);
534 goto out;
535 }
536 ports++;
537 }
538 }
539
540 err = rmnet_gport_setup();
541 if (err) {
542 pr_err("rmnet: Cannot setup transports");
543 goto out;
544 }
545 }
546
547 for (i = 0; i < ports; i++) {
548 err = frmnet_bind_config(c, i);
549 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530550 pr_err("Could not bind rmnet%u config\n", i);
551 break;
552 }
553 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700554out:
555 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530556}
557
Hemant Kumar1b820d52011-11-03 15:08:28 -0700558static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530559 struct device_attribute *attr, char *buf)
560{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700561 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530562}
563
Hemant Kumar1b820d52011-11-03 15:08:28 -0700564static ssize_t rmnet_transports_store(
565 struct device *device, struct device_attribute *attr,
566 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530567{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700568 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530569
Manu Gautam2b0234a2011-09-07 16:47:52 +0530570 return size;
571}
572
Hemant Kumar1b820d52011-11-03 15:08:28 -0700573static struct device_attribute dev_attr_rmnet_transports =
574 __ATTR(transports, S_IRUGO | S_IWUSR,
575 rmnet_transports_show,
576 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530577static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700578 &dev_attr_rmnet_transports,
579 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530580
581static struct android_usb_function rmnet_function = {
582 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530583 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530584 .bind_config = rmnet_function_bind_config,
585 .attributes = rmnet_function_attributes,
586};
587
Ofir Cohen7b155422012-07-31 13:02:49 +0300588struct ecm_function_config {
589 u8 ethaddr[ETH_ALEN];
590};
591
592static int ecm_function_init(struct android_usb_function *f,
593 struct usb_composite_dev *cdev)
594{
595 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
596 if (!f->config)
597 return -ENOMEM;
598 return 0;
599}
600
601static void ecm_function_cleanup(struct android_usb_function *f)
602{
603 kfree(f->config);
604 f->config = NULL;
605}
606
607static int ecm_qc_function_bind_config(struct android_usb_function *f,
608 struct usb_configuration *c)
609{
610 int ret;
611 struct ecm_function_config *ecm = f->config;
612
613 if (!ecm) {
614 pr_err("%s: ecm_pdata\n", __func__);
615 return -EINVAL;
616 }
617
618 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
619 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
620 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
621
622 ret = gether_qc_setup_name(c->cdev->gadget, ecm->ethaddr, "ecm");
623 if (ret) {
624 pr_err("%s: gether_setup failed\n", __func__);
625 return ret;
626 }
627
628 return ecm_qc_bind_config(c, ecm->ethaddr);
629}
630
631static void ecm_qc_function_unbind_config(struct android_usb_function *f,
632 struct usb_configuration *c)
633{
634 gether_qc_cleanup();
635}
636
637static ssize_t ecm_ethaddr_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
639{
640 struct android_usb_function *f = dev_get_drvdata(dev);
641 struct ecm_function_config *ecm = f->config;
642 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
643 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
644 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
645}
646
647static ssize_t ecm_ethaddr_store(struct device *dev,
648 struct device_attribute *attr, const char *buf, size_t size)
649{
650 struct android_usb_function *f = dev_get_drvdata(dev);
651 struct ecm_function_config *ecm = f->config;
652
653 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
654 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
655 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
656 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
657 return size;
658 return -EINVAL;
659}
660
661static DEVICE_ATTR(ecm_ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
662 ecm_ethaddr_store);
663
664static struct device_attribute *ecm_function_attributes[] = {
665 &dev_attr_ecm_ethaddr,
666 NULL
667};
668
669static struct android_usb_function ecm_qc_function = {
670 .name = "ecm_qc",
671 .init = ecm_function_init,
672 .cleanup = ecm_function_cleanup,
673 .bind_config = ecm_qc_function_bind_config,
674 .unbind_config = ecm_qc_function_unbind_config,
675 .attributes = ecm_function_attributes,
676};
Anna Perela8c991d2012-04-09 16:44:46 +0300677
678/* MBIM - used with BAM */
679#define MAX_MBIM_INSTANCES 1
680
681static int mbim_function_init(struct android_usb_function *f,
682 struct usb_composite_dev *cdev)
683{
684 return mbim_init(MAX_MBIM_INSTANCES);
685}
686
687static void mbim_function_cleanup(struct android_usb_function *f)
688{
689 fmbim_cleanup();
690}
691
692static int mbim_function_bind_config(struct android_usb_function *f,
693 struct usb_configuration *c)
694{
695 return mbim_bind_config(c, 0);
696}
697
698static struct android_usb_function mbim_function = {
699 .name = "usb_mbim",
700 .cleanup = mbim_function_cleanup,
701 .bind_config = mbim_function_bind_config,
702 .init = mbim_function_init,
703};
704
705
Manu Gautam8e0719b2011-09-26 14:47:55 +0530706/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530707static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708static ssize_t clients_store(
709 struct device *device, struct device_attribute *attr,
710 const char *buff, size_t size)
711{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530712 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713
714 return size;
715}
716
717static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
718static struct device_attribute *diag_function_attributes[] =
719 { &dev_attr_clients, NULL };
720
721static int diag_function_init(struct android_usb_function *f,
722 struct usb_composite_dev *cdev)
723{
724 return diag_setup();
725}
726
727static void diag_function_cleanup(struct android_usb_function *f)
728{
729 diag_cleanup();
730}
731
732static int diag_function_bind_config(struct android_usb_function *f,
733 struct usb_configuration *c)
734{
735 char *name;
736 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530737 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800738 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300739 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530741 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 b = strim(buf);
743
744 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800745 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530747 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300748 if (dev->pdata && !once++)
749 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530752 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 if (err)
754 pr_err("diag: Cannot open channel '%s'", name);
755 }
756 }
757
758 return err;
759}
760
761static struct android_usb_function diag_function = {
762 .name = "diag",
763 .init = diag_function_init,
764 .cleanup = diag_function_cleanup,
765 .bind_config = diag_function_bind_config,
766 .attributes = diag_function_attributes,
767};
768
Shimrit Malichia00d7322012-08-05 13:56:28 +0300769/* DEBUG */
770static int qdss_function_init(struct android_usb_function *f,
771 struct usb_composite_dev *cdev)
772{
773 return qdss_setup();
774}
775
776static void qdss_function_cleanup(struct android_usb_function *f)
777{
778 qdss_cleanup();
779}
780
781static int qdss_function_bind_config(struct android_usb_function *f,
782 struct usb_configuration *c)
783{
784 int err = -1;
785
786 err = qdss_bind_config(c, "qdss");
787 if (err)
788 pr_err("qdss: Cannot open channel qdss");
789
790 return err;
791}
792
793static struct android_usb_function qdss_function = {
794 .name = "qdss",
795 .init = qdss_function_init,
796 .cleanup = qdss_function_cleanup,
797 .bind_config = qdss_function_bind_config,
798};
799
Manu Gautam8e0719b2011-09-26 14:47:55 +0530800/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530801static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530802static ssize_t serial_transports_store(
803 struct device *device, struct device_attribute *attr,
804 const char *buff, size_t size)
805{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530806 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530807
808 return size;
809}
810
811static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
812static struct device_attribute *serial_function_attributes[] =
813 { &dev_attr_transports, NULL };
814
815static void serial_function_cleanup(struct android_usb_function *f)
816{
817 gserial_cleanup();
818}
819
820static int serial_function_bind_config(struct android_usb_function *f,
821 struct usb_configuration *c)
822{
823 char *name;
824 char buf[32], *b;
825 int err = -1, i;
826 static int serial_initialized = 0, ports = 0;
827
828 if (serial_initialized)
829 goto bind_config;
830
831 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530832 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530833 b = strim(buf);
834
835 while (b) {
836 name = strsep(&b, ",");
837
838 if (name) {
839 err = gserial_init_port(ports, name);
840 if (err) {
841 pr_err("serial: Cannot open port '%s'", name);
842 goto out;
843 }
844 ports++;
845 }
846 }
847 err = gport_setup(c);
848 if (err) {
849 pr_err("serial: Cannot setup transports");
850 goto out;
851 }
852
853bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200854 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530855 err = gser_bind_config(c, i);
856 if (err) {
857 pr_err("serial: bind_config failed for port %d", i);
858 goto out;
859 }
860 }
861
862out:
863 return err;
864}
865
866static struct android_usb_function serial_function = {
867 .name = "serial",
868 .cleanup = serial_function_cleanup,
869 .bind_config = serial_function_bind_config,
870 .attributes = serial_function_attributes,
871};
872
Anji jonnala92be1b42011-12-19 09:44:41 +0530873/* ACM */
874static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
875static ssize_t acm_transports_store(
876 struct device *device, struct device_attribute *attr,
877 const char *buff, size_t size)
878{
879 strlcpy(acm_transports, buff, sizeof(acm_transports));
880
881 return size;
882}
883
884static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
885static struct device_attribute *acm_function_attributes[] = {
886 &dev_attr_acm_transports, NULL };
887
Benoit Goby1e8ce152011-12-12 13:01:23 -0800888static void acm_function_cleanup(struct android_usb_function *f)
889{
890 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800891}
892
Anji jonnala92be1b42011-12-19 09:44:41 +0530893static int acm_function_bind_config(struct android_usb_function *f,
894 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800895{
Anji jonnala92be1b42011-12-19 09:44:41 +0530896 char *name;
897 char buf[32], *b;
898 int err = -1, i;
899 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800900
Anji jonnala92be1b42011-12-19 09:44:41 +0530901 if (acm_initialized)
902 goto bind_config;
903
904 acm_initialized = 1;
905 strlcpy(buf, acm_transports, sizeof(buf));
906 b = strim(buf);
907
908 while (b) {
909 name = strsep(&b, ",");
910
911 if (name) {
912 err = acm_init_port(ports, name);
913 if (err) {
914 pr_err("acm: Cannot open port '%s'", name);
915 goto out;
916 }
917 ports++;
918 }
919 }
920 err = acm_port_setup(c);
921 if (err) {
922 pr_err("acm: Cannot setup transports");
923 goto out;
924 }
925
926bind_config:
927 for (i = 0; i < ports; i++) {
928 err = acm_bind_config(c, i);
929 if (err) {
930 pr_err("acm: bind_config failed for port %d", i);
931 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800932 }
933 }
934
Anji jonnala92be1b42011-12-19 09:44:41 +0530935out:
936 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800937}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800938static struct android_usb_function acm_function = {
939 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800940 .cleanup = acm_function_cleanup,
941 .bind_config = acm_function_bind_config,
942 .attributes = acm_function_attributes,
943};
944
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530945/* CCID */
946static int ccid_function_init(struct android_usb_function *f,
947 struct usb_composite_dev *cdev)
948{
949 return ccid_setup();
950}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800951
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530952static void ccid_function_cleanup(struct android_usb_function *f)
953{
954 ccid_cleanup();
955}
956
957static int ccid_function_bind_config(struct android_usb_function *f,
958 struct usb_configuration *c)
959{
960 return ccid_bind_config(c);
961}
962
963static struct android_usb_function ccid_function = {
964 .name = "ccid",
965 .init = ccid_function_init,
966 .cleanup = ccid_function_cleanup,
967 .bind_config = ccid_function_bind_config,
968};
969
Steve Mucklef132c6c2012-06-06 18:30:57 -0700970static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800971 struct usb_composite_dev *cdev)
972{
973 return mtp_setup();
974}
975
976static void mtp_function_cleanup(struct android_usb_function *f)
977{
978 mtp_cleanup();
979}
980
Steve Mucklef132c6c2012-06-06 18:30:57 -0700981static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800982 struct usb_configuration *c)
983{
984 return mtp_bind_config(c, false);
985}
986
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400987static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800988{
989 /* nothing to do - initialization is handled by mtp_function_init */
990 return 0;
991}
992
993static void ptp_function_cleanup(struct android_usb_function *f)
994{
995 /* nothing to do - cleanup is handled by mtp_function_cleanup */
996}
997
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400998static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800999{
1000 return mtp_bind_config(c, true);
1001}
1002
1003static int mtp_function_ctrlrequest(struct android_usb_function *f,
1004 struct usb_composite_dev *cdev,
1005 const struct usb_ctrlrequest *c)
1006{
1007 return mtp_ctrlrequest(cdev, c);
1008}
1009
1010static struct android_usb_function mtp_function = {
1011 .name = "mtp",
1012 .init = mtp_function_init,
1013 .cleanup = mtp_function_cleanup,
1014 .bind_config = mtp_function_bind_config,
1015 .ctrlrequest = mtp_function_ctrlrequest,
1016};
1017
1018/* PTP function is same as MTP with slightly different interface descriptor */
1019static struct android_usb_function ptp_function = {
1020 .name = "ptp",
1021 .init = ptp_function_init,
1022 .cleanup = ptp_function_cleanup,
1023 .bind_config = ptp_function_bind_config,
1024};
1025
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001026#ifdef CONFIG_USB_ANDROID_CDC_ECM
1027struct ecm_function_config {
1028 u8 ethaddr[ETH_ALEN];
1029 u32 vendorID;
1030 char manufacturer[256];
1031};
1032
1033static int ecm_function_init(struct android_usb_function *f,
1034 struct usb_composite_dev *cdev)
1035{
1036 f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
1037 if (!f->config)
1038 return -ENOMEM;
1039 return 0;
1040}
1041
1042static void ecm_function_cleanup(struct android_usb_function *f)
1043{
1044 kfree(f->config);
1045 f->config = NULL;
1046}
1047
1048static int ecm_function_bind_config(struct android_usb_function *f,
1049 struct usb_configuration *c)
1050{
1051 int ret;
1052 struct ecm_function_config *ecm = f->config;
1053
1054 if (!ecm) {
1055 pr_err("%s: ecm_function_config\n", __func__);
1056 return -1;
1057 }
1058
1059 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1060 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1061 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1062
1063 ret = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usb");
1064 if (ret) {
1065 pr_err("%s: gether_setup failed\n", __func__);
1066 return ret;
1067 }
1068
1069 return ecm_bind_config(c, ecm->ethaddr);
1070}
1071
1072static void ecm_function_unbind_config(struct android_usb_function *f,
1073 struct usb_configuration *c)
1074{
1075 gether_cleanup();
1076}
1077
1078static ssize_t ecm_manufacturer_show(struct device *dev,
1079 struct device_attribute *attr, char *buf)
1080{
1081 struct android_usb_function *f = dev_get_drvdata(dev);
1082 struct ecm_function_config *config = f->config;
1083
1084 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
1085}
1086
1087static ssize_t ecm_manufacturer_store(struct device *dev,
1088 struct device_attribute *attr, const char *buf, size_t size)
1089{
1090 struct android_usb_function *f = dev_get_drvdata(dev);
1091 struct ecm_function_config *config = f->config;
1092
1093 if (size >= sizeof(config->manufacturer))
1094 return -EINVAL;
1095
1096 if (sscanf(buf, "%255s", config->manufacturer) == 1)
1097 return size;
1098 return -1;
1099}
1100
1101static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, ecm_manufacturer_show,
1102 ecm_manufacturer_store);
1103
1104static ssize_t ecm_ethaddr_show(struct device *dev,
1105 struct device_attribute *attr, char *buf)
1106{
1107 struct android_usb_function *f = dev_get_drvdata(dev);
1108 struct ecm_function_config *ecm = f->config;
1109 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1110 ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
1111 ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
1112}
1113
1114static ssize_t ecm_ethaddr_store(struct device *dev,
1115 struct device_attribute *attr, const char *buf, size_t size)
1116{
1117 struct android_usb_function *f = dev_get_drvdata(dev);
1118 struct ecm_function_config *ecm = f->config;
1119
1120 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1121 (int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
1122 (int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
1123 (int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
1124 return size;
1125 return -EINVAL;
1126}
1127
1128static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, ecm_ethaddr_show,
1129 ecm_ethaddr_store);
1130
1131static ssize_t ecm_vendorID_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct android_usb_function *f = dev_get_drvdata(dev);
1135 struct ecm_function_config *config = f->config;
1136
1137 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
1138}
1139
1140static ssize_t ecm_vendorID_store(struct device *dev,
1141 struct device_attribute *attr, const char *buf, size_t size)
1142{
1143 struct android_usb_function *f = dev_get_drvdata(dev);
1144 struct ecm_function_config *config = f->config;
1145 int value;
1146
1147 if (sscanf(buf, "%04x", &value) == 1) {
1148 config->vendorID = value;
1149 return size;
1150 }
1151 return -EINVAL;
1152}
1153
1154static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, ecm_vendorID_show,
1155 ecm_vendorID_store);
1156
1157static struct device_attribute *ecm_function_attributes[] = {
1158 &dev_attr_manufacturer,
1159 &dev_attr_ethaddr,
1160 &dev_attr_vendorID,
1161 NULL
1162};
1163
1164static struct android_usb_function ecm_function = {
1165 .name = "ecm",
1166 .init = ecm_function_init,
1167 .cleanup = ecm_function_cleanup,
1168 .bind_config = ecm_function_bind_config,
1169 .unbind_config = ecm_function_unbind_config,
1170 .attributes = ecm_function_attributes,
1171};
1172
1173#else
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001174
Benoit Goby1e8ce152011-12-12 13:01:23 -08001175struct rndis_function_config {
1176 u8 ethaddr[ETH_ALEN];
1177 u32 vendorID;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001178 u8 max_pkt_per_xfer;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001179 char manufacturer[256];
1180 /* "Wireless" RNDIS; auto-detected by Windows */
1181 bool wceis;
1182};
1183
1184static int
1185rndis_function_init(struct android_usb_function *f,
1186 struct usb_composite_dev *cdev)
1187{
1188 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1189 if (!f->config)
1190 return -ENOMEM;
1191 return 0;
1192}
1193
1194static void rndis_function_cleanup(struct android_usb_function *f)
1195{
1196 kfree(f->config);
1197 f->config = NULL;
1198}
1199
Ofir Cohenaef90b72012-07-31 12:37:04 +02001200static int rndis_qc_function_init(struct android_usb_function *f,
1201 struct usb_composite_dev *cdev)
1202{
1203 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
1204 if (!f->config)
1205 return -ENOMEM;
1206
1207 return rndis_qc_init();
1208}
1209
1210static void rndis_qc_function_cleanup(struct android_usb_function *f)
1211{
1212 rndis_qc_cleanup();
1213 kfree(f->config);
1214}
1215
Benoit Goby1e8ce152011-12-12 13:01:23 -08001216static int
1217rndis_function_bind_config(struct android_usb_function *f,
1218 struct usb_configuration *c)
1219{
1220 int ret;
1221 struct rndis_function_config *rndis = f->config;
1222
1223 if (!rndis) {
1224 pr_err("%s: rndis_pdata\n", __func__);
1225 return -1;
1226 }
1227
1228 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1229 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1230 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1231
Devin Kim78e07192012-07-17 08:40:54 -07001232 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "usb");
Benoit Goby1e8ce152011-12-12 13:01:23 -08001233 if (ret) {
1234 pr_err("%s: gether_setup failed\n", __func__);
1235 return ret;
1236 }
1237
1238 if (rndis->wceis) {
1239 /* "Wireless" RNDIS; auto-detected by Windows */
1240 rndis_iad_descriptor.bFunctionClass =
1241 USB_CLASS_WIRELESS_CONTROLLER;
1242 rndis_iad_descriptor.bFunctionSubClass = 0x01;
1243 rndis_iad_descriptor.bFunctionProtocol = 0x03;
1244 rndis_control_intf.bInterfaceClass =
1245 USB_CLASS_WIRELESS_CONTROLLER;
1246 rndis_control_intf.bInterfaceSubClass = 0x01;
1247 rndis_control_intf.bInterfaceProtocol = 0x03;
1248 }
1249
1250 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1251 rndis->manufacturer);
1252}
1253
Ofir Cohenaef90b72012-07-31 12:37:04 +02001254static int rndis_qc_function_bind_config(struct android_usb_function *f,
1255 struct usb_configuration *c)
1256{
1257 int ret;
1258 struct rndis_function_config *rndis = f->config;
1259
1260 if (!rndis) {
1261 pr_err("%s: rndis_pdata\n", __func__);
1262 return -EINVAL;
1263 }
1264
1265 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
1266 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1267 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1268
1269 ret = gether_qc_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
1270 if (ret) {
1271 pr_err("%s: gether_setup failed\n", __func__);
1272 return ret;
1273 }
1274
1275 if (rndis->wceis) {
1276 /* "Wireless" RNDIS; auto-detected by Windows */
1277 rndis_qc_iad_descriptor.bFunctionClass =
1278 USB_CLASS_WIRELESS_CONTROLLER;
1279 rndis_qc_iad_descriptor.bFunctionSubClass = 0x01;
1280 rndis_qc_iad_descriptor.bFunctionProtocol = 0x03;
1281 rndis_qc_control_intf.bInterfaceClass =
1282 USB_CLASS_WIRELESS_CONTROLLER;
1283 rndis_qc_control_intf.bInterfaceSubClass = 0x01;
1284 rndis_qc_control_intf.bInterfaceProtocol = 0x03;
1285 }
1286
1287 return rndis_qc_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
1288 rndis->manufacturer,
1289 rndis->max_pkt_per_xfer);
1290}
1291
Benoit Goby1e8ce152011-12-12 13:01:23 -08001292static void rndis_function_unbind_config(struct android_usb_function *f,
1293 struct usb_configuration *c)
1294{
1295 gether_cleanup();
1296}
1297
Ofir Cohenaef90b72012-07-31 12:37:04 +02001298static void rndis_qc_function_unbind_config(struct android_usb_function *f,
1299 struct usb_configuration *c)
1300{
1301 gether_qc_cleanup();
1302}
1303
Benoit Goby1e8ce152011-12-12 13:01:23 -08001304static ssize_t rndis_manufacturer_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1306{
1307 struct android_usb_function *f = dev_get_drvdata(dev);
1308 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001309
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301310 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001311}
1312
1313static ssize_t rndis_manufacturer_store(struct device *dev,
1314 struct device_attribute *attr, const char *buf, size_t size)
1315{
1316 struct android_usb_function *f = dev_get_drvdata(dev);
1317 struct rndis_function_config *config = f->config;
1318
1319 if (size >= sizeof(config->manufacturer))
1320 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001321
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301322 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001323 return size;
1324 return -1;
1325}
1326
1327static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
1328 rndis_manufacturer_store);
1329
1330static ssize_t rndis_wceis_show(struct device *dev,
1331 struct device_attribute *attr, char *buf)
1332{
1333 struct android_usb_function *f = dev_get_drvdata(dev);
1334 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001335
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301336 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001337}
1338
1339static ssize_t rndis_wceis_store(struct device *dev,
1340 struct device_attribute *attr, const char *buf, size_t size)
1341{
1342 struct android_usb_function *f = dev_get_drvdata(dev);
1343 struct rndis_function_config *config = f->config;
1344 int value;
1345
1346 if (sscanf(buf, "%d", &value) == 1) {
1347 config->wceis = value;
1348 return size;
1349 }
1350 return -EINVAL;
1351}
1352
1353static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
1354 rndis_wceis_store);
1355
1356static ssize_t rndis_ethaddr_show(struct device *dev,
1357 struct device_attribute *attr, char *buf)
1358{
1359 struct android_usb_function *f = dev_get_drvdata(dev);
1360 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001361
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301362 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001363 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1364 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1365}
1366
1367static ssize_t rndis_ethaddr_store(struct device *dev,
1368 struct device_attribute *attr, const char *buf, size_t size)
1369{
1370 struct android_usb_function *f = dev_get_drvdata(dev);
1371 struct rndis_function_config *rndis = f->config;
1372
1373 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1374 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1375 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1376 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1377 return size;
1378 return -EINVAL;
1379}
1380
1381static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1382 rndis_ethaddr_store);
1383
1384static ssize_t rndis_vendorID_show(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387 struct android_usb_function *f = dev_get_drvdata(dev);
1388 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301390 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001391}
1392
1393static ssize_t rndis_vendorID_store(struct device *dev,
1394 struct device_attribute *attr, const char *buf, size_t size)
1395{
1396 struct android_usb_function *f = dev_get_drvdata(dev);
1397 struct rndis_function_config *config = f->config;
1398 int value;
1399
1400 if (sscanf(buf, "%04x", &value) == 1) {
1401 config->vendorID = value;
1402 return size;
1403 }
1404 return -EINVAL;
1405}
1406
1407static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1408 rndis_vendorID_store);
1409
Ofir Cohenaef90b72012-07-31 12:37:04 +02001410static ssize_t rndis_max_pkt_per_xfer_show(struct device *dev,
1411 struct device_attribute *attr, char *buf)
1412{
1413 struct android_usb_function *f = dev_get_drvdata(dev);
1414 struct rndis_function_config *config = f->config;
1415 return snprintf(buf, PAGE_SIZE, "%d\n", config->max_pkt_per_xfer);
1416}
1417
1418static ssize_t rndis_max_pkt_per_xfer_store(struct device *dev,
1419 struct device_attribute *attr, const char *buf, size_t size)
1420{
1421 struct android_usb_function *f = dev_get_drvdata(dev);
1422 struct rndis_function_config *config = f->config;
1423 int value;
1424
1425 if (sscanf(buf, "%d", &value) == 1) {
1426 config->max_pkt_per_xfer = value;
1427 return size;
1428 }
1429 return -EINVAL;
1430}
1431
1432static DEVICE_ATTR(max_pkt_per_xfer, S_IRUGO | S_IWUSR,
1433 rndis_max_pkt_per_xfer_show,
1434 rndis_max_pkt_per_xfer_store);
1435
Benoit Goby1e8ce152011-12-12 13:01:23 -08001436static struct device_attribute *rndis_function_attributes[] = {
1437 &dev_attr_manufacturer,
1438 &dev_attr_wceis,
1439 &dev_attr_ethaddr,
1440 &dev_attr_vendorID,
Ofir Cohenaef90b72012-07-31 12:37:04 +02001441 &dev_attr_max_pkt_per_xfer,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001442 NULL
1443};
1444
1445static struct android_usb_function rndis_function = {
1446 .name = "rndis",
1447 .init = rndis_function_init,
1448 .cleanup = rndis_function_cleanup,
1449 .bind_config = rndis_function_bind_config,
1450 .unbind_config = rndis_function_unbind_config,
1451 .attributes = rndis_function_attributes,
1452};
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001453#endif /* CONFIG_USB_ANDROID_CDC_ECM */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001454
Ofir Cohenaef90b72012-07-31 12:37:04 +02001455static struct android_usb_function rndis_qc_function = {
1456 .name = "rndis_qc",
1457 .init = rndis_qc_function_init,
1458 .cleanup = rndis_qc_function_cleanup,
1459 .bind_config = rndis_qc_function_bind_config,
1460 .unbind_config = rndis_qc_function_unbind_config,
1461 .attributes = rndis_function_attributes,
1462};
Benoit Goby1e8ce152011-12-12 13:01:23 -08001463
1464struct mass_storage_function_config {
1465 struct fsg_config fsg;
1466 struct fsg_common *common;
1467};
1468
1469static int mass_storage_function_init(struct android_usb_function *f,
1470 struct usb_composite_dev *cdev)
1471{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001472 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001473 struct mass_storage_function_config *config;
1474 struct fsg_common *common;
1475 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301476 int i;
1477 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001478
1479 config = kzalloc(sizeof(struct mass_storage_function_config),
1480 GFP_KERNEL);
1481 if (!config)
1482 return -ENOMEM;
1483
1484 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301485 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301486 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301487 config->fsg.nluns = 2;
1488 config->fsg.luns[1].cdrom = 1;
1489 config->fsg.luns[1].ro = 1;
1490 config->fsg.luns[1].removable = 1;
1491 name[1] = "lun0";
1492 }
1493
Benoit Goby1e8ce152011-12-12 13:01:23 -08001494 config->fsg.luns[0].removable = 1;
1495
1496 common = fsg_common_init(NULL, cdev, &config->fsg);
1497 if (IS_ERR(common)) {
1498 kfree(config);
1499 return PTR_ERR(common);
1500 }
1501
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301502 for (i = 0; i < config->fsg.nluns; i++) {
1503 err = sysfs_create_link(&f->dev->kobj,
1504 &common->luns[i].dev.kobj,
1505 name[i]);
1506 if (err)
1507 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001508 }
1509
1510 config->common = common;
1511 f->config = config;
1512 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301513error:
1514 for (; i > 0 ; i--)
1515 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1516
1517 fsg_common_release(&common->ref);
1518 kfree(config);
1519 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001520}
1521
1522static void mass_storage_function_cleanup(struct android_usb_function *f)
1523{
1524 kfree(f->config);
1525 f->config = NULL;
1526}
1527
1528static int mass_storage_function_bind_config(struct android_usb_function *f,
1529 struct usb_configuration *c)
1530{
1531 struct mass_storage_function_config *config = f->config;
1532 return fsg_bind_config(c->cdev, c, config->common);
1533}
1534
1535static ssize_t mass_storage_inquiry_show(struct device *dev,
1536 struct device_attribute *attr, char *buf)
1537{
1538 struct android_usb_function *f = dev_get_drvdata(dev);
1539 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301540 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001541}
1542
1543static ssize_t mass_storage_inquiry_store(struct device *dev,
1544 struct device_attribute *attr, const char *buf, size_t size)
1545{
1546 struct android_usb_function *f = dev_get_drvdata(dev);
1547 struct mass_storage_function_config *config = f->config;
1548 if (size >= sizeof(config->common->inquiry_string))
1549 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301550 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001551 return -EINVAL;
1552 return size;
1553}
1554
1555static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1556 mass_storage_inquiry_show,
1557 mass_storage_inquiry_store);
1558
1559static struct device_attribute *mass_storage_function_attributes[] = {
1560 &dev_attr_inquiry_string,
1561 NULL
1562};
1563
1564static struct android_usb_function mass_storage_function = {
1565 .name = "mass_storage",
1566 .init = mass_storage_function_init,
1567 .cleanup = mass_storage_function_cleanup,
1568 .bind_config = mass_storage_function_bind_config,
1569 .attributes = mass_storage_function_attributes,
1570};
1571
1572
Benoit Gobycf3fc062011-12-19 14:39:37 -08001573static int accessory_function_init(struct android_usb_function *f,
1574 struct usb_composite_dev *cdev)
1575{
1576 return acc_setup();
1577}
1578
1579static void accessory_function_cleanup(struct android_usb_function *f)
1580{
1581 acc_cleanup();
1582}
1583
1584static int accessory_function_bind_config(struct android_usb_function *f,
1585 struct usb_configuration *c)
1586{
1587 return acc_bind_config(c);
1588}
1589
1590static int accessory_function_ctrlrequest(struct android_usb_function *f,
1591 struct usb_composite_dev *cdev,
1592 const struct usb_ctrlrequest *c)
1593{
1594 return acc_ctrlrequest(cdev, c);
1595}
1596
1597static struct android_usb_function accessory_function = {
1598 .name = "accessory",
1599 .init = accessory_function_init,
1600 .cleanup = accessory_function_cleanup,
1601 .bind_config = accessory_function_bind_config,
1602 .ctrlrequest = accessory_function_ctrlrequest,
1603};
1604
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301605static int android_uasp_connect_cb(bool connect)
1606{
1607 /*
1608 * TODO
1609 * We may have to disable gadget till UASP configfs nodes
1610 * are configured which includes mapping LUN with the
1611 * backing file. It is a fundamental difference between
1612 * f_mass_storage and f_tcp. That means UASP can not be
1613 * in default composition.
1614 *
1615 * For now, assume that UASP configfs nodes are configured
1616 * before enabling android gadget. Or cable should be
1617 * reconnected after mapping the LUN.
1618 *
1619 * Also consider making UASP to respond to Host requests when
1620 * Lun is not mapped.
1621 */
1622 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1623
1624 return 0;
1625}
1626
1627static int uasp_function_init(struct android_usb_function *f,
1628 struct usb_composite_dev *cdev)
1629{
1630 return f_tcm_init(&android_uasp_connect_cb);
1631}
1632
1633static void uasp_function_cleanup(struct android_usb_function *f)
1634{
1635 f_tcm_exit();
1636}
1637
1638static int uasp_function_bind_config(struct android_usb_function *f,
1639 struct usb_configuration *c)
1640{
1641 return tcm_bind_config(c);
1642}
1643
1644static struct android_usb_function uasp_function = {
1645 .name = "uasp",
1646 .init = uasp_function_init,
1647 .cleanup = uasp_function_cleanup,
1648 .bind_config = uasp_function_bind_config,
1649};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001650
Benoit Goby1e8ce152011-12-12 13:01:23 -08001651static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001652 &mbim_function,
Ofir Cohen7b155422012-07-31 13:02:49 +03001653 &ecm_qc_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301654 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301655 &rmnet_sdio_function,
1656 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301657 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 &diag_function,
Shimrit Malichia00d7322012-08-05 13:56:28 +03001659 &qdss_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301660 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001661 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301662 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001663 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001664 &mtp_function,
1665 &ptp_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001666#ifdef CONFIG_USB_ANDROID_CDC_ECM
1667 &ecm_function,
1668#else
Benoit Goby1e8ce152011-12-12 13:01:23 -08001669 &rndis_function,
Devin Kim0c5b9ce2012-06-19 21:34:44 -07001670#endif
Ofir Cohenaef90b72012-07-31 12:37:04 +02001671 &rndis_qc_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001672 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001673 &accessory_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301674 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001675 NULL
1676};
1677
Lena Salmand092f2d2012-03-12 17:27:24 +02001678static void android_cleanup_functions(struct android_usb_function **functions)
1679{
1680 struct android_usb_function *f;
1681 struct device_attribute **attrs;
1682 struct device_attribute *attr;
1683
1684 while (*functions) {
1685 f = *functions++;
1686
1687 if (f->dev) {
1688 device_destroy(android_class, f->dev->devt);
1689 kfree(f->dev_name);
1690 } else
1691 continue;
1692
1693 if (f->cleanup)
1694 f->cleanup(f);
1695
1696 attrs = f->attributes;
1697 if (attrs) {
1698 while ((attr = *attrs++))
1699 device_remove_file(f->dev, attr);
1700 }
1701 }
1702}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001703
1704static int android_init_functions(struct android_usb_function **functions,
1705 struct usb_composite_dev *cdev)
1706{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001707 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001708 struct android_usb_function *f;
1709 struct device_attribute **attrs;
1710 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301711 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001712 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001713
1714 for (; (f = *functions++); index++) {
1715 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001716 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001717 if (!f->dev_name) {
1718 err = -ENOMEM;
1719 goto err_out;
1720 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001721 f->dev = device_create(android_class, dev->dev,
1722 MKDEV(0, index), f, f->dev_name);
1723 if (IS_ERR(f->dev)) {
1724 pr_err("%s: Failed to create dev %s", __func__,
1725 f->dev_name);
1726 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001727 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001728 goto err_create;
1729 }
1730
1731 if (f->init) {
1732 err = f->init(f, cdev);
1733 if (err) {
1734 pr_err("%s: Failed to init %s", __func__,
1735 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001736 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001737 }
1738 }
1739
1740 attrs = f->attributes;
1741 if (attrs) {
1742 while ((attr = *attrs++) && !err)
1743 err = device_create_file(f->dev, attr);
1744 }
1745 if (err) {
1746 pr_err("%s: Failed to create function %s attributes",
1747 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001748 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001749 }
1750 }
1751 return 0;
1752
Lena Salmand092f2d2012-03-12 17:27:24 +02001753err_attrs:
1754 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1755 device_remove_file(f->dev, attr);
1756 if (f->cleanup)
1757 f->cleanup(f);
1758err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001759 device_destroy(android_class, f->dev->devt);
1760err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001761 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001762 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001763err_out:
1764 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001765 return err;
1766}
1767
Benoit Goby1e8ce152011-12-12 13:01:23 -08001768static int
1769android_bind_enabled_functions(struct android_dev *dev,
1770 struct usb_configuration *c)
1771{
1772 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001773 struct android_configuration *conf =
1774 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001775 int ret;
1776
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001777 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001778 ret = f->bind_config(f, c);
1779 if (ret) {
1780 pr_err("%s: %s failed", __func__, f->name);
1781 return ret;
1782 }
1783 }
1784 return 0;
1785}
1786
1787static void
1788android_unbind_enabled_functions(struct android_dev *dev,
1789 struct usb_configuration *c)
1790{
1791 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001792 struct android_configuration *conf =
1793 container_of(c, struct android_configuration, usb_config);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001794
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001795 list_for_each_entry(f, &conf->enabled_functions, enabled_list) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001796 if (f->unbind_config)
1797 f->unbind_config(f, c);
1798 }
1799}
1800
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001801static int android_enable_function(struct android_dev *dev,
1802 struct android_configuration *conf,
1803 char *name)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001804{
1805 struct android_usb_function **functions = dev->functions;
1806 struct android_usb_function *f;
1807 while ((f = *functions++)) {
1808 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001809 if (f->android_dev)
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001810 pr_err("%s already enabled in other " \
1811 "configuration or device\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001812 f->name);
1813 else {
1814 list_add_tail(&f->enabled_list,
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001815 &conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001816 f->android_dev = dev;
1817 return 0;
1818 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001819 }
1820 }
1821 return -EINVAL;
1822}
1823
1824/*-------------------------------------------------------------------------*/
1825/* /sys/class/android_usb/android%d/ interface */
1826
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301827static ssize_t remote_wakeup_show(struct device *pdev,
1828 struct device_attribute *attr, char *buf)
1829{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001830 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001831 struct android_configuration *conf;
1832
1833 /*
1834 * Show the wakeup attribute of the first configuration,
1835 * since all configurations have the same wakeup attribute
1836 */
1837 if (dev->configs_num == 0)
1838 return 0;
1839 conf = list_entry(dev->configs.next,
1840 struct android_configuration,
1841 list_item);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001842
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301843 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001844 !!(conf->usb_config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301845 USB_CONFIG_ATT_WAKEUP));
1846}
1847
1848static ssize_t remote_wakeup_store(struct device *pdev,
1849 struct device_attribute *attr, const char *buff, size_t size)
1850{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001851 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001852 struct android_configuration *conf;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301853 int enable = 0;
1854
1855 sscanf(buff, "%d", &enable);
1856
1857 pr_debug("android_usb: %s remote wakeup\n",
1858 enable ? "enabling" : "disabling");
1859
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001860 list_for_each_entry(conf, &dev->configs, list_item)
1861 if (enable)
1862 conf->usb_config.bmAttributes |=
1863 USB_CONFIG_ATT_WAKEUP;
1864 else
1865 conf->usb_config.bmAttributes &=
1866 ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301867
1868 return size;
1869}
1870
Benoit Goby1e8ce152011-12-12 13:01:23 -08001871static ssize_t
1872functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1873{
1874 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001875 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001876 struct android_usb_function *f;
1877 char *buff = buf;
1878
1879 mutex_lock(&dev->mutex);
1880
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001881 list_for_each_entry(conf, &dev->configs, list_item) {
1882 if (buff != buf)
1883 *(buff-1) = ':';
1884 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1885 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
1886 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001887
1888 mutex_unlock(&dev->mutex);
1889
1890 if (buff != buf)
1891 *(buff-1) = '\n';
1892 return buff - buf;
1893}
1894
1895static ssize_t
1896functions_store(struct device *pdev, struct device_attribute *attr,
1897 const char *buff, size_t size)
1898{
1899 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001900 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001901 struct list_head *curr_conf = &dev->configs;
1902 struct android_configuration *conf;
1903 char *conf_str;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001904 char *name;
1905 char buf[256], *b;
1906 int err;
1907
1908 mutex_lock(&dev->mutex);
1909
1910 if (dev->enabled) {
1911 mutex_unlock(&dev->mutex);
1912 return -EBUSY;
1913 }
1914
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001915 /* Clear previous enabled list */
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001916 list_for_each_entry(conf, &dev->configs, list_item) {
1917 list_for_each_entry(f, &conf->enabled_functions, enabled_list)
1918 f->android_dev = NULL;
1919 INIT_LIST_HEAD(&conf->enabled_functions);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001920 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001921
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301922 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001923 b = strim(buf);
1924
1925 while (b) {
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001926 conf_str = strsep(&b, ":");
1927 if (conf_str) {
1928 /* If the next not equal to the head, take it */
1929 if (curr_conf->next != &dev->configs)
1930 conf = list_entry(curr_conf->next,
1931 struct android_configuration,
1932 list_item);
1933 else
1934 conf = alloc_android_config(dev);
1935
1936 curr_conf = curr_conf->next;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001937 }
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001938
1939 while (conf_str) {
1940 name = strsep(&conf_str, ",");
1941 if (name) {
1942 err = android_enable_function(dev, conf, name);
1943 if (err)
1944 pr_err("android_usb: Cannot enable %s",
1945 name);
1946 }
1947 }
1948 }
1949
1950 /* Free uneeded configurations if exists */
1951 while (curr_conf->next != &dev->configs) {
1952 conf = list_entry(curr_conf->next,
1953 struct android_configuration, list_item);
1954 free_android_config(dev, conf);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001955 }
1956
1957 mutex_unlock(&dev->mutex);
1958
1959 return size;
1960}
1961
1962static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1963 char *buf)
1964{
1965 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001966
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301967 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001968}
1969
1970static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1971 const char *buff, size_t size)
1972{
1973 struct android_dev *dev = dev_get_drvdata(pdev);
1974 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001975 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001976 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001977 int enabled = 0;
1978
Benoit Gobycf3fc062011-12-19 14:39:37 -08001979 if (!cdev)
1980 return -ENODEV;
1981
Benoit Goby1e8ce152011-12-12 13:01:23 -08001982 mutex_lock(&dev->mutex);
1983
1984 sscanf(buff, "%d", &enabled);
1985 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001986 /*
1987 * Update values in composite driver's copy of
1988 * device descriptor.
1989 */
1990 cdev->desc.idVendor = device_desc.idVendor;
1991 cdev->desc.idProduct = device_desc.idProduct;
1992 cdev->desc.bcdDevice = device_desc.bcdDevice;
1993 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1994 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1995 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03001996 list_for_each_entry(conf, &dev->configs, list_item)
1997 list_for_each_entry(f, &conf->enabled_functions,
1998 enabled_list) {
1999 if (f->enable)
2000 f->enable(f);
2001 }
Benoit Goby80ba14d2012-03-19 18:56:52 -07002002 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002003 dev->enabled = true;
2004 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07002005 android_disable(dev);
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002006 list_for_each_entry(conf, &dev->configs, list_item)
2007 list_for_each_entry(f, &conf->enabled_functions,
2008 enabled_list) {
2009 if (f->disable)
2010 f->disable(f);
2011 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002012 dev->enabled = false;
2013 } else {
2014 pr_err("android_usb: already %s\n",
2015 dev->enabled ? "enabled" : "disabled");
2016 }
2017
2018 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002019
Benoit Gobyaab96812011-04-19 20:37:33 -07002020 return size;
2021}
2022
Ofir Cohen94213a72012-05-03 14:26:32 +03002023static ssize_t pm_qos_show(struct device *pdev,
2024 struct device_attribute *attr, char *buf)
2025{
2026 struct android_dev *dev = dev_get_drvdata(pdev);
2027
2028 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
2029}
2030
2031static ssize_t pm_qos_store(struct device *pdev,
2032 struct device_attribute *attr,
2033 const char *buff, size_t size)
2034{
2035 struct android_dev *dev = dev_get_drvdata(pdev);
2036
2037 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
2038
Benoit Goby1e8ce152011-12-12 13:01:23 -08002039 return size;
2040}
2041
2042static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
2043 char *buf)
2044{
2045 struct android_dev *dev = dev_get_drvdata(pdev);
2046 struct usb_composite_dev *cdev = dev->cdev;
2047 char *state = "DISCONNECTED";
2048 unsigned long flags;
2049
2050 if (!cdev)
2051 goto out;
2052
2053 spin_lock_irqsave(&cdev->lock, flags);
2054 if (cdev->config)
2055 state = "CONFIGURED";
2056 else if (dev->connected)
2057 state = "CONNECTED";
2058 spin_unlock_irqrestore(&cdev->lock, flags);
2059out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302060 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002061}
2062
2063#define DESCRIPTOR_ATTR(field, format_string) \
2064static ssize_t \
2065field ## _show(struct device *dev, struct device_attribute *attr, \
2066 char *buf) \
2067{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302068 return snprintf(buf, PAGE_SIZE, \
2069 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002070} \
2071static ssize_t \
2072field ## _store(struct device *dev, struct device_attribute *attr, \
2073 const char *buf, size_t size) \
2074{ \
2075 int value; \
2076 if (sscanf(buf, format_string, &value) == 1) { \
2077 device_desc.field = value; \
2078 return size; \
2079 } \
2080 return -1; \
2081} \
2082static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2083
2084#define DESCRIPTOR_STRING_ATTR(field, buffer) \
2085static ssize_t \
2086field ## _show(struct device *dev, struct device_attribute *attr, \
2087 char *buf) \
2088{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302089 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002090} \
2091static ssize_t \
2092field ## _store(struct device *dev, struct device_attribute *attr, \
2093 const char *buf, size_t size) \
2094{ \
2095 if (size >= sizeof(buffer)) \
2096 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05302097 strlcpy(buffer, buf, sizeof(buffer)); \
2098 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05302099 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08002100} \
2101static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
2102
2103
2104DESCRIPTOR_ATTR(idVendor, "%04x\n")
2105DESCRIPTOR_ATTR(idProduct, "%04x\n")
2106DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
2107DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
2108DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
2109DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
2110DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
2111DESCRIPTOR_STRING_ATTR(iProduct, product_string)
2112DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
2113
2114static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
2115 functions_store);
2116static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03002117static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
2118 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002119static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302120static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
2121 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002122
2123static struct device_attribute *android_usb_attributes[] = {
2124 &dev_attr_idVendor,
2125 &dev_attr_idProduct,
2126 &dev_attr_bcdDevice,
2127 &dev_attr_bDeviceClass,
2128 &dev_attr_bDeviceSubClass,
2129 &dev_attr_bDeviceProtocol,
2130 &dev_attr_iManufacturer,
2131 &dev_attr_iProduct,
2132 &dev_attr_iSerial,
2133 &dev_attr_functions,
2134 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03002135 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002136 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05302137 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08002138 NULL
2139};
2140
2141/*-------------------------------------------------------------------------*/
2142/* Composite driver */
2143
2144static int android_bind_config(struct usb_configuration *c)
2145{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002146 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002147 int ret = 0;
2148
2149 ret = android_bind_enabled_functions(dev, c);
2150 if (ret)
2151 return ret;
2152
2153 return 0;
2154}
2155
2156static void android_unbind_config(struct usb_configuration *c)
2157{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002158 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002159
2160 android_unbind_enabled_functions(dev, c);
2161}
2162
2163static int android_bind(struct usb_composite_dev *cdev)
2164{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002165 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002166 struct usb_gadget *gadget = cdev->gadget;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002167 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002168 int gcnum, id, ret;
2169
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002170 /* Bind to the last android_dev that was probed */
2171 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
2172
2173 dev->cdev = cdev;
2174
Benoit Goby1e8ce152011-12-12 13:01:23 -08002175 /*
2176 * Start disconnected. Userspace will connect the gadget once
2177 * it is done configuring the functions.
2178 */
2179 usb_gadget_disconnect(gadget);
2180
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002181 /* Init the supported functions only once, on the first android_dev */
2182 if (android_dev_count == 1) {
2183 ret = android_init_functions(dev->functions, cdev);
2184 if (ret)
2185 return ret;
2186 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002187
2188 /* Allocate string descriptor numbers ... note that string
2189 * contents can be overridden by the composite_dev glue.
2190 */
2191 id = usb_string_id(cdev);
2192 if (id < 0)
2193 return id;
2194 strings_dev[STRING_MANUFACTURER_IDX].id = id;
2195 device_desc.iManufacturer = id;
2196
2197 id = usb_string_id(cdev);
2198 if (id < 0)
2199 return id;
2200 strings_dev[STRING_PRODUCT_IDX].id = id;
2201 device_desc.iProduct = id;
2202
2203 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05302204 strlcpy(manufacturer_string, "Android",
2205 sizeof(manufacturer_string) - 1);
2206 strlcpy(product_string, "Android", sizeof(product_string) - 1);
2207 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002208
2209 id = usb_string_id(cdev);
2210 if (id < 0)
2211 return id;
2212 strings_dev[STRING_SERIAL_IDX].id = id;
2213 device_desc.iSerialNumber = id;
2214
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302215 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002216 list_for_each_entry(conf, &dev->configs, list_item)
2217 conf->usb_config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05302218
Benoit Goby1e8ce152011-12-12 13:01:23 -08002219 gcnum = usb_gadget_controller_number(gadget);
2220 if (gcnum >= 0)
2221 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
2222 else {
2223 pr_warning("%s: controller '%s' not recognized\n",
2224 longname, gadget->name);
2225 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
2226 }
2227
Benoit Goby1e8ce152011-12-12 13:01:23 -08002228 return 0;
2229}
2230
2231static int android_usb_unbind(struct usb_composite_dev *cdev)
2232{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002233 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002234
Lena Salmand092f2d2012-03-12 17:27:24 +02002235 manufacturer_string[0] = '\0';
2236 product_string[0] = '\0';
2237 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08002238 cancel_work_sync(&dev->work);
2239 android_cleanup_functions(dev->functions);
2240 return 0;
2241}
2242
2243static struct usb_composite_driver android_usb_driver = {
2244 .name = "android_usb",
2245 .dev = &device_desc,
2246 .strings = dev_strings,
2247 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03002248 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08002249};
2250
2251static int
2252android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
2253{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002254 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002255 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002256 struct usb_request *req = cdev->req;
2257 struct android_usb_function *f;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002258 struct android_configuration *conf;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002259 int value = -EOPNOTSUPP;
2260 unsigned long flags;
2261
2262 req->zero = 0;
2263 req->complete = composite_setup_complete;
2264 req->length = 0;
2265 gadget->ep0->driver_data = cdev;
2266
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002267 list_for_each_entry(conf, &dev->configs, list_item)
2268 if (&conf->usb_config == cdev->config)
2269 list_for_each_entry(f,
2270 &conf->enabled_functions,
2271 enabled_list)
2272 if (f->ctrlrequest) {
2273 value = f->ctrlrequest(f, cdev, c);
2274 if (value >= 0)
2275 break;
2276 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002277
Benoit Gobycf3fc062011-12-19 14:39:37 -08002278 /* Special case the accessory function.
2279 * It needs to handle control requests before it is enabled.
2280 */
2281 if (value < 0)
2282 value = acc_ctrlrequest(cdev, c);
2283
Benoit Goby1e8ce152011-12-12 13:01:23 -08002284 if (value < 0)
2285 value = composite_setup(gadget, c);
2286
2287 spin_lock_irqsave(&cdev->lock, flags);
2288 if (!dev->connected) {
2289 dev->connected = 1;
2290 schedule_work(&dev->work);
2291 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
2292 cdev->config) {
2293 schedule_work(&dev->work);
2294 }
2295 spin_unlock_irqrestore(&cdev->lock, flags);
2296
2297 return value;
2298}
2299
2300static void android_disconnect(struct usb_gadget *gadget)
2301{
Benoit Goby1e8ce152011-12-12 13:01:23 -08002302 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002303 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002304 unsigned long flags;
2305
2306 composite_disconnect(gadget);
Ajay Dudanic2f0cc72012-08-27 16:23:48 +05302307 /* accessory HID support can be active while the
2308 accessory function is not actually enabled,
2309 so we need to inform it when we are disconnected.
2310 */
2311 acc_disconnect();
Benoit Goby1e8ce152011-12-12 13:01:23 -08002312
2313 spin_lock_irqsave(&cdev->lock, flags);
2314 dev->connected = 0;
2315 schedule_work(&dev->work);
2316 spin_unlock_irqrestore(&cdev->lock, flags);
2317}
2318
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002319static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002320{
2321 struct device_attribute **attrs = android_usb_attributes;
2322 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002323 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08002324 int err;
2325
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002326 /*
2327 * The primary usb core should always have usb_core_id=0, since
2328 * Android user space is currently interested in android0 events.
2329 */
2330 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
2331 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002332 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002333 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002334 if (IS_ERR(dev->dev))
2335 return PTR_ERR(dev->dev);
2336
2337 dev_set_drvdata(dev->dev, dev);
2338
2339 while ((attr = *attrs++)) {
2340 err = device_create_file(dev->dev, attr);
2341 if (err) {
2342 device_destroy(android_class, dev->dev->devt);
2343 return err;
2344 }
2345 }
2346 return 0;
2347}
2348
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302349static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08002350{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302351 struct device_attribute **attrs = android_usb_attributes;
2352 struct device_attribute *attr;
2353
2354 while ((attr = *attrs++))
2355 device_remove_file(dev->dev, attr);
2356 device_destroy(android_class, dev->dev->devt);
2357}
2358
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002359static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
2360{
2361 struct android_dev *dev = NULL;
2362
2363 /* Find the android dev from the list */
2364 list_for_each_entry(dev, &android_dev_list, list_item) {
2365 if (dev->cdev == cdev)
2366 break;
2367 }
2368
2369 return dev;
2370}
2371
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002372static struct android_configuration *alloc_android_config
2373 (struct android_dev *dev)
2374{
2375 struct android_configuration *conf;
2376
2377 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
2378 if (!conf) {
2379 pr_err("%s(): Failed to alloc memory for android conf\n",
2380 __func__);
2381 return ERR_PTR(-ENOMEM);
2382 }
2383
2384 dev->configs_num++;
2385 conf->usb_config.label = dev->name;
2386 conf->usb_config.unbind = android_unbind_config;
2387 conf->usb_config.bConfigurationValue = dev->configs_num;
2388
2389 INIT_LIST_HEAD(&conf->enabled_functions);
2390
2391 list_add_tail(&conf->list_item, &dev->configs);
2392
2393 return conf;
2394}
2395
2396static void free_android_config(struct android_dev *dev,
2397 struct android_configuration *conf)
2398{
2399 list_del(&conf->list_item);
2400 dev->configs_num--;
2401 kfree(conf);
2402}
2403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002404static int __devinit android_probe(struct platform_device *pdev)
2405{
2406 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002407 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02002408 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002410 if (!android_class) {
2411 android_class = class_create(THIS_MODULE, "android_usb");
2412 if (IS_ERR(android_class))
2413 return PTR_ERR(android_class);
2414 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002415
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002416 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
2417 if (!android_dev) {
2418 pr_err("%s(): Failed to alloc memory for android_dev\n",
2419 __func__);
2420 ret = -ENOMEM;
2421 goto err_alloc;
2422 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08002423
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002424 android_dev->name = pdev->name;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002425 android_dev->disable_depth = 1;
2426 android_dev->functions = supported_functions;
Ido Shayevitz2a65e7c2012-08-02 13:34:18 +03002427 android_dev->configs_num = 0;
2428 INIT_LIST_HEAD(&android_dev->configs);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002429 INIT_WORK(&android_dev->work, android_work);
2430 mutex_init(&android_dev->mutex);
2431
2432 android_dev->pdata = pdata;
2433
2434 list_add_tail(&android_dev->list_item, &android_dev_list);
2435 android_dev_count++;
2436
2437 if (pdata)
2438 composite_driver.usb_core_id = pdata->usb_core_id;
2439 else
2440 composite_driver.usb_core_id = 0; /*To backward compatibility*/
2441
2442 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302443 if (ret) {
2444 pr_err("%s(): android_create_device failed\n", __func__);
2445 goto err_dev;
2446 }
2447
Lena Salmand092f2d2012-03-12 17:27:24 +02002448 ret = usb_composite_probe(&android_usb_driver, android_bind);
2449 if (ret) {
2450 pr_err("%s(): Failed to register android "
2451 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302452 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02002453 }
2454
Ofir Cohen94213a72012-05-03 14:26:32 +03002455 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05302456 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002457 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03002458 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002459 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03002460
Lena Salmand092f2d2012-03-12 17:27:24 +02002461 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302462err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002463 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302464err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002465 list_del(&android_dev->list_item);
2466 android_dev_count--;
2467 kfree(android_dev);
2468err_alloc:
2469 if (list_empty(&android_dev_list)) {
2470 class_destroy(android_class);
2471 android_class = NULL;
2472 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302473 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02002474}
2475
2476static int android_remove(struct platform_device *pdev)
2477{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002478 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03002479 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002480 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05302481
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002482 if (pdata)
2483 usb_core_id = pdata->usb_core_id;
2484
2485 /* Find the android dev from the list */
2486 list_for_each_entry(dev, &android_dev_list, list_item) {
2487 if (!dev->pdata)
2488 break; /*To backward compatibility*/
2489 if (dev->pdata->usb_core_id == usb_core_id)
2490 break;
2491 }
2492
2493 if (dev) {
2494 android_destroy_device(dev);
2495 if (pdata && pdata->swfi_latency)
2496 pm_qos_remove_request(&dev->pm_qos_req_dma);
2497 list_del(&dev->list_item);
2498 android_dev_count--;
2499 kfree(dev);
2500 }
2501
2502 if (list_empty(&android_dev_list)) {
2503 class_destroy(android_class);
2504 android_class = NULL;
2505 usb_composite_unregister(&android_usb_driver);
2506 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508 return 0;
2509}
2510
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002511static const struct platform_device_id android_id_table[] __devinitconst = {
2512 {
2513 .name = "android_usb",
2514 },
2515 {
2516 .name = "android_usb_hsic",
2517 },
2518};
2519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520static struct platform_driver android_platform_driver = {
2521 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002522 .probe = android_probe,
2523 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002524 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002526
2527static int __init init(void)
2528{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302529 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002530
Benoit Goby1e8ce152011-12-12 13:01:23 -08002531 /* Override composite driver functions */
2532 composite_driver.setup = android_setup;
2533 composite_driver.disconnect = android_disconnect;
2534
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002535 INIT_LIST_HEAD(&android_dev_list);
2536 android_dev_count = 0;
2537
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302538 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302539 if (ret) {
2540 pr_err("%s(): Failed to register android"
2541 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302542 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002543
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302544 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002545}
2546module_init(init);
2547
2548static void __exit cleanup(void)
2549{
Lena Salmand092f2d2012-03-12 17:27:24 +02002550 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002551}
2552module_exit(cleanup);