blob: 5430e116e8b42ca06621cc8ad74f4040314c195d [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"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053048#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053049#include "f_rmnet_sdio.c"
50#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053051#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080052#include "f_mass_storage.c"
53#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053054#include "u_sdio.c"
55#include "u_smd.c"
56#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053057#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080058#include "u_ctrl_hsic.c"
59#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053060#include "u_ctrl_hsuart.c"
61#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080063#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080064#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053065#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080066#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080067#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080068#define USB_ETH_RNDIS y
69#include "f_rndis.c"
70#include "rndis.c"
71#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030072#include "u_bam_data.c"
73#include "f_mbim.c"
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +053074#ifdef CONFIG_TARGET_CORE
75#include "f_tcm.c"
76#endif
Benoit Goby1e8ce152011-12-12 13:01:23 -080077
78MODULE_AUTHOR("Mike Lockwood");
79MODULE_DESCRIPTION("Android Composite USB Driver");
80MODULE_LICENSE("GPL");
81MODULE_VERSION("1.0");
82
83static const char longname[] = "Gadget Android";
84
85/* Default vendor and product IDs, overridden by userspace */
86#define VENDOR_ID 0x18D1
87#define PRODUCT_ID 0x0001
88
Ido Shayevitz23dc77c2012-07-18 16:16:06 +030089#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
90
Benoit Goby1e8ce152011-12-12 13:01:23 -080091struct android_usb_function {
92 char *name;
93 void *config;
94
95 struct device *dev;
96 char *dev_name;
97 struct device_attribute **attributes;
98
99 /* for android_dev.enabled_functions */
100 struct list_head enabled_list;
101
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300102 struct android_dev *android_dev;
103
Benoit Goby1e8ce152011-12-12 13:01:23 -0800104 /* Optional: initialization during gadget bind */
105 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
106 /* Optional: cleanup during gadget unbind */
107 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700108 /* Optional: called when the function is added the list of
109 * enabled functions */
110 void (*enable)(struct android_usb_function *);
111 /* Optional: called when it is removed */
112 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800113
114 int (*bind_config)(struct android_usb_function *,
115 struct usb_configuration *);
116
117 /* Optional: called when the configuration is removed */
118 void (*unbind_config)(struct android_usb_function *,
119 struct usb_configuration *);
120 /* Optional: handle ctrl requests before the device is configured */
121 int (*ctrlrequest)(struct android_usb_function *,
122 struct usb_composite_dev *,
123 const struct usb_ctrlrequest *);
124};
125
126struct android_dev {
127 struct android_usb_function **functions;
128 struct list_head enabled_functions;
129 struct usb_composite_dev *cdev;
130 struct device *dev;
131
132 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700133 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800134 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700135 struct android_usb_platform_data *pdata;
136
Benoit Goby1e8ce152011-12-12 13:01:23 -0800137 bool connected;
138 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300139 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700140 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800141 struct work_struct work;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300142
143 struct list_head list_item;
144
145 struct usb_configuration config;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800146};
147
148static struct class *android_class;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300149static struct list_head android_dev_list;
150static int android_dev_count;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800151static int android_bind_config(struct usb_configuration *c);
152static void android_unbind_config(struct usb_configuration *c);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300153static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800154
155/* string IDs are assigned dynamically */
156#define STRING_MANUFACTURER_IDX 0
157#define STRING_PRODUCT_IDX 1
158#define STRING_SERIAL_IDX 2
159
160static char manufacturer_string[256];
161static char product_string[256];
162static char serial_string[256];
163
164/* String Table */
165static struct usb_string strings_dev[] = {
166 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
167 [STRING_PRODUCT_IDX].s = product_string,
168 [STRING_SERIAL_IDX].s = serial_string,
169 { } /* end of list */
170};
171
172static struct usb_gadget_strings stringtab_dev = {
173 .language = 0x0409, /* en-us */
174 .strings = strings_dev,
175};
176
177static struct usb_gadget_strings *dev_strings[] = {
178 &stringtab_dev,
179 NULL,
180};
181
182static struct usb_device_descriptor device_desc = {
183 .bLength = sizeof(device_desc),
184 .bDescriptorType = USB_DT_DEVICE,
185 .bcdUSB = __constant_cpu_to_le16(0x0200),
186 .bDeviceClass = USB_CLASS_PER_INTERFACE,
187 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
188 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
189 .bcdDevice = __constant_cpu_to_le16(0xffff),
190 .bNumConfigurations = 1,
191};
192
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530193static struct usb_otg_descriptor otg_descriptor = {
194 .bLength = sizeof otg_descriptor,
195 .bDescriptorType = USB_DT_OTG,
196 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
197 .bcdOTG = __constant_cpu_to_le16(0x0200),
198};
199
200static const struct usb_descriptor_header *otg_desc[] = {
201 (struct usb_descriptor_header *) &otg_descriptor,
202 NULL,
203};
204
Manu Gautama2b54142012-04-03 14:34:32 +0530205enum android_device_state {
206 USB_DISCONNECTED,
207 USB_CONNECTED,
208 USB_CONFIGURED,
209};
210
Ofir Cohen94213a72012-05-03 14:26:32 +0300211static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
212{
213 struct android_usb_platform_data *pdata = dev->pdata;
214 u32 swfi_latency = 0;
215 static int last_vote = -1;
216
Ofir Cohen56eb7072012-05-20 11:41:39 +0300217 if (!pdata || vote == last_vote
218 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300219 return;
220
221 swfi_latency = pdata->swfi_latency + 1;
222 if (vote)
223 pm_qos_update_request(&dev->pm_qos_req_dma,
224 swfi_latency);
225 else
226 pm_qos_update_request(&dev->pm_qos_req_dma,
227 PM_QOS_DEFAULT_VALUE);
228 last_vote = vote;
229}
230
Benoit Goby1e8ce152011-12-12 13:01:23 -0800231static void android_work(struct work_struct *data)
232{
233 struct android_dev *dev = container_of(data, struct android_dev, work);
234 struct usb_composite_dev *cdev = dev->cdev;
235 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
236 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
237 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
238 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530239 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800240 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300241 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800242
243 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530244 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800245 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530246 next_state = USB_CONFIGURED;
247 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800248 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530249 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300250 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300251 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300252 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300253 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530254 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800255 dev->sw_connected = dev->connected;
256 spin_unlock_irqrestore(&cdev->lock, flags);
257
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300258 if (pm_qos_vote != -1)
259 android_pm_qos_update_latency(dev, pm_qos_vote);
260
Benoit Goby1e8ce152011-12-12 13:01:23 -0800261 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530262 /*
263 * Some userspace modules, e.g. MTP, work correctly only if
264 * CONFIGURED uevent is preceded by DISCONNECT uevent.
265 * Check if we missed sending out a DISCONNECT uevent. This can
266 * happen if host PC resets and configures device really quick.
267 */
268 if (((uevent_envp == connected) &&
269 (last_uevent != USB_DISCONNECTED)) ||
270 ((uevent_envp == configured) &&
271 (last_uevent == USB_CONFIGURED))) {
272 pr_info("%s: sent missed DISCONNECT event\n", __func__);
273 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
274 disconnected);
275 msleep(20);
276 }
277 /*
278 * Before sending out CONFIGURED uevent give function drivers
279 * a chance to wakeup userspace threads and notify disconnect
280 */
281 if (uevent_envp == configured)
282 msleep(50);
283
Benoit Goby1e8ce152011-12-12 13:01:23 -0800284 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530285 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800286 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
287 } else {
288 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
289 dev->connected, dev->sw_connected, cdev->config);
290 }
291}
292
Benoit Goby80ba14d2012-03-19 18:56:52 -0700293static void android_enable(struct android_dev *dev)
294{
295 struct usb_composite_dev *cdev = dev->cdev;
296
297 if (WARN_ON(!dev->disable_depth))
298 return;
299
300 if (--dev->disable_depth == 0) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300301 usb_add_config(cdev, &dev->config,
Benoit Goby80ba14d2012-03-19 18:56:52 -0700302 android_bind_config);
303 usb_gadget_connect(cdev->gadget);
304 }
305}
306
307static void android_disable(struct android_dev *dev)
308{
309 struct usb_composite_dev *cdev = dev->cdev;
310
311 if (dev->disable_depth++ == 0) {
312 usb_gadget_disconnect(cdev->gadget);
313 /* Cancel pending control requests */
314 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300315 usb_remove_config(cdev, &dev->config);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700316 }
317}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800318
319/*-------------------------------------------------------------------------*/
320/* Supported functions initialization */
321
Benoit Goby80ba14d2012-03-19 18:56:52 -0700322struct adb_data {
323 bool opened;
324 bool enabled;
325};
326
Benoit Goby2b6862d2011-12-19 14:38:41 -0800327static int
328adb_function_init(struct android_usb_function *f,
329 struct usb_composite_dev *cdev)
330{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700331 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
332 if (!f->config)
333 return -ENOMEM;
334
Benoit Goby2b6862d2011-12-19 14:38:41 -0800335 return adb_setup();
336}
337
338static void adb_function_cleanup(struct android_usb_function *f)
339{
340 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700341 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800342}
343
344static int
345adb_function_bind_config(struct android_usb_function *f,
346 struct usb_configuration *c)
347{
348 return adb_bind_config(c);
349}
350
Benoit Goby80ba14d2012-03-19 18:56:52 -0700351static void adb_android_function_enable(struct android_usb_function *f)
352{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300353 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700354 struct adb_data *data = f->config;
355
356 data->enabled = true;
357
358 /* Disable the gadget until adbd is ready */
359 if (!data->opened)
360 android_disable(dev);
361}
362
363static void adb_android_function_disable(struct android_usb_function *f)
364{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300365 struct android_dev *dev = f->android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700366 struct adb_data *data = f->config;
367
368 data->enabled = false;
369
370 /* Balance the disable that was called in closed_callback */
371 if (!data->opened)
372 android_enable(dev);
373}
374
Benoit Goby2b6862d2011-12-19 14:38:41 -0800375static struct android_usb_function adb_function = {
376 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700377 .enable = adb_android_function_enable,
378 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800379 .init = adb_function_init,
380 .cleanup = adb_function_cleanup,
381 .bind_config = adb_function_bind_config,
382};
383
Benoit Goby80ba14d2012-03-19 18:56:52 -0700384static void adb_ready_callback(void)
385{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300386 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700387 struct adb_data *data = adb_function.config;
388
Benoit Goby80ba14d2012-03-19 18:56:52 -0700389 data->opened = true;
390
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300391 if (data->enabled && dev) {
392 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700393 android_enable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300394 mutex_unlock(&dev->mutex);
395 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700396}
397
398static void adb_closed_callback(void)
399{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300400 struct android_dev *dev = adb_function.android_dev;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700401 struct adb_data *data = adb_function.config;
402
Benoit Goby80ba14d2012-03-19 18:56:52 -0700403 data->opened = false;
404
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300405 if (data->enabled) {
406 mutex_lock(&dev->mutex);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700407 android_disable(dev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300408 mutex_unlock(&dev->mutex);
409 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700410}
411
Benoit Goby2b6862d2011-12-19 14:38:41 -0800412
Benoit Gobyaab96812011-04-19 20:37:33 -0700413/*-------------------------------------------------------------------------*/
414/* Supported functions initialization */
415
Manu Gautam8e0719b2011-09-26 14:47:55 +0530416/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530417static int rmnet_smd_function_bind_config(struct android_usb_function *f,
418 struct usb_configuration *c)
419{
420 return rmnet_smd_bind_config(c);
421}
422
423static struct android_usb_function rmnet_smd_function = {
424 .name = "rmnet_smd",
425 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800426};
427
Manu Gautam8e0719b2011-09-26 14:47:55 +0530428/* RMNET_SDIO */
429static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
430 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800431{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530432 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800433}
434
Manu Gautam8e0719b2011-09-26 14:47:55 +0530435static struct android_usb_function rmnet_sdio_function = {
436 .name = "rmnet_sdio",
437 .bind_config = rmnet_sdio_function_bind_config,
438};
439
440/* RMNET_SMD_SDIO */
441static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
442 struct usb_composite_dev *cdev)
443{
444 return rmnet_smd_sdio_init();
445}
446
447static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
448{
449 rmnet_smd_sdio_cleanup();
450}
451
452static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
453 struct usb_configuration *c)
454{
455 return rmnet_smd_sdio_function_add(c);
456}
457
458static struct device_attribute *rmnet_smd_sdio_attributes[] = {
459 &dev_attr_transport, NULL };
460
461static struct android_usb_function rmnet_smd_sdio_function = {
462 .name = "rmnet_smd_sdio",
463 .init = rmnet_smd_sdio_function_init,
464 .cleanup = rmnet_smd_sdio_function_cleanup,
465 .bind_config = rmnet_smd_sdio_bind_config,
466 .attributes = rmnet_smd_sdio_attributes,
467};
468
Hemant Kumar1b820d52011-11-03 15:08:28 -0700469/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
470#define MAX_XPORT_STR_LEN 50
471static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530472
Manu Gautame3e897c2011-09-12 17:18:46 +0530473static void rmnet_function_cleanup(struct android_usb_function *f)
474{
475 frmnet_cleanup();
476}
477
Manu Gautam2b0234a2011-09-07 16:47:52 +0530478static int rmnet_function_bind_config(struct android_usb_function *f,
479 struct usb_configuration *c)
480{
481 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700482 int err = 0;
483 char *ctrl_name;
484 char *data_name;
485 char buf[MAX_XPORT_STR_LEN], *b;
486 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530487
Hemant Kumar1b820d52011-11-03 15:08:28 -0700488 if (!rmnet_initialized) {
489 rmnet_initialized = 1;
490 strlcpy(buf, rmnet_transports, sizeof(buf));
491 b = strim(buf);
492 while (b) {
493 ctrl_name = strsep(&b, ",");
494 data_name = strsep(&b, ",");
495 if (ctrl_name && data_name) {
496 err = frmnet_init_port(ctrl_name, data_name);
497 if (err) {
498 pr_err("rmnet: Cannot open ctrl port:"
499 "'%s' data port:'%s'\n",
500 ctrl_name, data_name);
501 goto out;
502 }
503 ports++;
504 }
505 }
506
507 err = rmnet_gport_setup();
508 if (err) {
509 pr_err("rmnet: Cannot setup transports");
510 goto out;
511 }
512 }
513
514 for (i = 0; i < ports; i++) {
515 err = frmnet_bind_config(c, i);
516 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530517 pr_err("Could not bind rmnet%u config\n", i);
518 break;
519 }
520 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700521out:
522 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530523}
524
Hemant Kumar1b820d52011-11-03 15:08:28 -0700525static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530526 struct device_attribute *attr, char *buf)
527{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700528 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530529}
530
Hemant Kumar1b820d52011-11-03 15:08:28 -0700531static ssize_t rmnet_transports_store(
532 struct device *device, struct device_attribute *attr,
533 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530534{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700535 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530536
Manu Gautam2b0234a2011-09-07 16:47:52 +0530537 return size;
538}
539
Hemant Kumar1b820d52011-11-03 15:08:28 -0700540static struct device_attribute dev_attr_rmnet_transports =
541 __ATTR(transports, S_IRUGO | S_IWUSR,
542 rmnet_transports_show,
543 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530544static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700545 &dev_attr_rmnet_transports,
546 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530547
548static struct android_usb_function rmnet_function = {
549 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530550 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530551 .bind_config = rmnet_function_bind_config,
552 .attributes = rmnet_function_attributes,
553};
554
Anna Perela8c991d2012-04-09 16:44:46 +0300555
556/* MBIM - used with BAM */
557#define MAX_MBIM_INSTANCES 1
558
559static int mbim_function_init(struct android_usb_function *f,
560 struct usb_composite_dev *cdev)
561{
562 return mbim_init(MAX_MBIM_INSTANCES);
563}
564
565static void mbim_function_cleanup(struct android_usb_function *f)
566{
567 fmbim_cleanup();
568}
569
570static int mbim_function_bind_config(struct android_usb_function *f,
571 struct usb_configuration *c)
572{
573 return mbim_bind_config(c, 0);
574}
575
576static struct android_usb_function mbim_function = {
577 .name = "usb_mbim",
578 .cleanup = mbim_function_cleanup,
579 .bind_config = mbim_function_bind_config,
580 .init = mbim_function_init,
581};
582
583
Manu Gautam8e0719b2011-09-26 14:47:55 +0530584/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530585static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586static ssize_t clients_store(
587 struct device *device, struct device_attribute *attr,
588 const char *buff, size_t size)
589{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530590 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
592 return size;
593}
594
595static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
596static struct device_attribute *diag_function_attributes[] =
597 { &dev_attr_clients, NULL };
598
599static int diag_function_init(struct android_usb_function *f,
600 struct usb_composite_dev *cdev)
601{
602 return diag_setup();
603}
604
605static void diag_function_cleanup(struct android_usb_function *f)
606{
607 diag_cleanup();
608}
609
610static int diag_function_bind_config(struct android_usb_function *f,
611 struct usb_configuration *c)
612{
613 char *name;
614 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530615 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800616 int (*notify)(uint32_t, const char *);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300617 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530619 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 b = strim(buf);
621
622 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800623 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530625 /* Allow only first diag channel to update pid and serial no */
Ido Shayevitz23dc77c2012-07-18 16:16:06 +0300626 if (dev->pdata && !once++)
627 notify = dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530628
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700629 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530630 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 if (err)
632 pr_err("diag: Cannot open channel '%s'", name);
633 }
634 }
635
636 return err;
637}
638
639static struct android_usb_function diag_function = {
640 .name = "diag",
641 .init = diag_function_init,
642 .cleanup = diag_function_cleanup,
643 .bind_config = diag_function_bind_config,
644 .attributes = diag_function_attributes,
645};
646
Manu Gautam8e0719b2011-09-26 14:47:55 +0530647/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530648static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530649static ssize_t serial_transports_store(
650 struct device *device, struct device_attribute *attr,
651 const char *buff, size_t size)
652{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530653 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530654
655 return size;
656}
657
658static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
659static struct device_attribute *serial_function_attributes[] =
660 { &dev_attr_transports, NULL };
661
662static void serial_function_cleanup(struct android_usb_function *f)
663{
664 gserial_cleanup();
665}
666
667static int serial_function_bind_config(struct android_usb_function *f,
668 struct usb_configuration *c)
669{
670 char *name;
671 char buf[32], *b;
672 int err = -1, i;
673 static int serial_initialized = 0, ports = 0;
674
675 if (serial_initialized)
676 goto bind_config;
677
678 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530679 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530680 b = strim(buf);
681
682 while (b) {
683 name = strsep(&b, ",");
684
685 if (name) {
686 err = gserial_init_port(ports, name);
687 if (err) {
688 pr_err("serial: Cannot open port '%s'", name);
689 goto out;
690 }
691 ports++;
692 }
693 }
694 err = gport_setup(c);
695 if (err) {
696 pr_err("serial: Cannot setup transports");
697 goto out;
698 }
699
700bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200701 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530702 err = gser_bind_config(c, i);
703 if (err) {
704 pr_err("serial: bind_config failed for port %d", i);
705 goto out;
706 }
707 }
708
709out:
710 return err;
711}
712
713static struct android_usb_function serial_function = {
714 .name = "serial",
715 .cleanup = serial_function_cleanup,
716 .bind_config = serial_function_bind_config,
717 .attributes = serial_function_attributes,
718};
719
Anji jonnala92be1b42011-12-19 09:44:41 +0530720/* ACM */
721static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
722static ssize_t acm_transports_store(
723 struct device *device, struct device_attribute *attr,
724 const char *buff, size_t size)
725{
726 strlcpy(acm_transports, buff, sizeof(acm_transports));
727
728 return size;
729}
730
731static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
732static struct device_attribute *acm_function_attributes[] = {
733 &dev_attr_acm_transports, NULL };
734
Benoit Goby1e8ce152011-12-12 13:01:23 -0800735static void acm_function_cleanup(struct android_usb_function *f)
736{
737 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800738}
739
Anji jonnala92be1b42011-12-19 09:44:41 +0530740static int acm_function_bind_config(struct android_usb_function *f,
741 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800742{
Anji jonnala92be1b42011-12-19 09:44:41 +0530743 char *name;
744 char buf[32], *b;
745 int err = -1, i;
746 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800747
Anji jonnala92be1b42011-12-19 09:44:41 +0530748 if (acm_initialized)
749 goto bind_config;
750
751 acm_initialized = 1;
752 strlcpy(buf, acm_transports, sizeof(buf));
753 b = strim(buf);
754
755 while (b) {
756 name = strsep(&b, ",");
757
758 if (name) {
759 err = acm_init_port(ports, name);
760 if (err) {
761 pr_err("acm: Cannot open port '%s'", name);
762 goto out;
763 }
764 ports++;
765 }
766 }
767 err = acm_port_setup(c);
768 if (err) {
769 pr_err("acm: Cannot setup transports");
770 goto out;
771 }
772
773bind_config:
774 for (i = 0; i < ports; i++) {
775 err = acm_bind_config(c, i);
776 if (err) {
777 pr_err("acm: bind_config failed for port %d", i);
778 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800779 }
780 }
781
Anji jonnala92be1b42011-12-19 09:44:41 +0530782out:
783 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800784}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800785static struct android_usb_function acm_function = {
786 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800787 .cleanup = acm_function_cleanup,
788 .bind_config = acm_function_bind_config,
789 .attributes = acm_function_attributes,
790};
791
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530792/* CCID */
793static int ccid_function_init(struct android_usb_function *f,
794 struct usb_composite_dev *cdev)
795{
796 return ccid_setup();
797}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800798
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530799static void ccid_function_cleanup(struct android_usb_function *f)
800{
801 ccid_cleanup();
802}
803
804static int ccid_function_bind_config(struct android_usb_function *f,
805 struct usb_configuration *c)
806{
807 return ccid_bind_config(c);
808}
809
810static struct android_usb_function ccid_function = {
811 .name = "ccid",
812 .init = ccid_function_init,
813 .cleanup = ccid_function_cleanup,
814 .bind_config = ccid_function_bind_config,
815};
816
Steve Mucklef132c6c2012-06-06 18:30:57 -0700817static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800818 struct usb_composite_dev *cdev)
819{
820 return mtp_setup();
821}
822
823static void mtp_function_cleanup(struct android_usb_function *f)
824{
825 mtp_cleanup();
826}
827
Steve Mucklef132c6c2012-06-06 18:30:57 -0700828static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800829 struct usb_configuration *c)
830{
831 return mtp_bind_config(c, false);
832}
833
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400834static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800835{
836 /* nothing to do - initialization is handled by mtp_function_init */
837 return 0;
838}
839
840static void ptp_function_cleanup(struct android_usb_function *f)
841{
842 /* nothing to do - cleanup is handled by mtp_function_cleanup */
843}
844
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400845static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800846{
847 return mtp_bind_config(c, true);
848}
849
850static int mtp_function_ctrlrequest(struct android_usb_function *f,
851 struct usb_composite_dev *cdev,
852 const struct usb_ctrlrequest *c)
853{
854 return mtp_ctrlrequest(cdev, c);
855}
856
857static struct android_usb_function mtp_function = {
858 .name = "mtp",
859 .init = mtp_function_init,
860 .cleanup = mtp_function_cleanup,
861 .bind_config = mtp_function_bind_config,
862 .ctrlrequest = mtp_function_ctrlrequest,
863};
864
865/* PTP function is same as MTP with slightly different interface descriptor */
866static struct android_usb_function ptp_function = {
867 .name = "ptp",
868 .init = ptp_function_init,
869 .cleanup = ptp_function_cleanup,
870 .bind_config = ptp_function_bind_config,
871};
872
873
Benoit Goby1e8ce152011-12-12 13:01:23 -0800874struct rndis_function_config {
875 u8 ethaddr[ETH_ALEN];
876 u32 vendorID;
877 char manufacturer[256];
878 /* "Wireless" RNDIS; auto-detected by Windows */
879 bool wceis;
880};
881
882static int
883rndis_function_init(struct android_usb_function *f,
884 struct usb_composite_dev *cdev)
885{
886 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
887 if (!f->config)
888 return -ENOMEM;
889 return 0;
890}
891
892static void rndis_function_cleanup(struct android_usb_function *f)
893{
894 kfree(f->config);
895 f->config = NULL;
896}
897
898static int
899rndis_function_bind_config(struct android_usb_function *f,
900 struct usb_configuration *c)
901{
902 int ret;
903 struct rndis_function_config *rndis = f->config;
904
905 if (!rndis) {
906 pr_err("%s: rndis_pdata\n", __func__);
907 return -1;
908 }
909
910 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
911 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
912 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
913
914 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
915 if (ret) {
916 pr_err("%s: gether_setup failed\n", __func__);
917 return ret;
918 }
919
920 if (rndis->wceis) {
921 /* "Wireless" RNDIS; auto-detected by Windows */
922 rndis_iad_descriptor.bFunctionClass =
923 USB_CLASS_WIRELESS_CONTROLLER;
924 rndis_iad_descriptor.bFunctionSubClass = 0x01;
925 rndis_iad_descriptor.bFunctionProtocol = 0x03;
926 rndis_control_intf.bInterfaceClass =
927 USB_CLASS_WIRELESS_CONTROLLER;
928 rndis_control_intf.bInterfaceSubClass = 0x01;
929 rndis_control_intf.bInterfaceProtocol = 0x03;
930 }
931
932 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
933 rndis->manufacturer);
934}
935
936static void rndis_function_unbind_config(struct android_usb_function *f,
937 struct usb_configuration *c)
938{
939 gether_cleanup();
940}
941
942static ssize_t rndis_manufacturer_show(struct device *dev,
943 struct device_attribute *attr, char *buf)
944{
945 struct android_usb_function *f = dev_get_drvdata(dev);
946 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700947
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530948 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800949}
950
951static ssize_t rndis_manufacturer_store(struct device *dev,
952 struct device_attribute *attr, const char *buf, size_t size)
953{
954 struct android_usb_function *f = dev_get_drvdata(dev);
955 struct rndis_function_config *config = f->config;
956
957 if (size >= sizeof(config->manufacturer))
958 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700959
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530960 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800961 return size;
962 return -1;
963}
964
965static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
966 rndis_manufacturer_store);
967
968static ssize_t rndis_wceis_show(struct device *dev,
969 struct device_attribute *attr, char *buf)
970{
971 struct android_usb_function *f = dev_get_drvdata(dev);
972 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700973
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530974 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800975}
976
977static ssize_t rndis_wceis_store(struct device *dev,
978 struct device_attribute *attr, const char *buf, size_t size)
979{
980 struct android_usb_function *f = dev_get_drvdata(dev);
981 struct rndis_function_config *config = f->config;
982 int value;
983
984 if (sscanf(buf, "%d", &value) == 1) {
985 config->wceis = value;
986 return size;
987 }
988 return -EINVAL;
989}
990
991static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
992 rndis_wceis_store);
993
994static ssize_t rndis_ethaddr_show(struct device *dev,
995 struct device_attribute *attr, char *buf)
996{
997 struct android_usb_function *f = dev_get_drvdata(dev);
998 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700999
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301000 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -08001001 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
1002 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
1003}
1004
1005static ssize_t rndis_ethaddr_store(struct device *dev,
1006 struct device_attribute *attr, const char *buf, size_t size)
1007{
1008 struct android_usb_function *f = dev_get_drvdata(dev);
1009 struct rndis_function_config *rndis = f->config;
1010
1011 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1012 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1013 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1014 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1015 return size;
1016 return -EINVAL;
1017}
1018
1019static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1020 rndis_ethaddr_store);
1021
1022static ssize_t rndis_vendorID_show(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1024{
1025 struct android_usb_function *f = dev_get_drvdata(dev);
1026 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001027
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301028 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001029}
1030
1031static ssize_t rndis_vendorID_store(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t size)
1033{
1034 struct android_usb_function *f = dev_get_drvdata(dev);
1035 struct rndis_function_config *config = f->config;
1036 int value;
1037
1038 if (sscanf(buf, "%04x", &value) == 1) {
1039 config->vendorID = value;
1040 return size;
1041 }
1042 return -EINVAL;
1043}
1044
1045static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1046 rndis_vendorID_store);
1047
1048static struct device_attribute *rndis_function_attributes[] = {
1049 &dev_attr_manufacturer,
1050 &dev_attr_wceis,
1051 &dev_attr_ethaddr,
1052 &dev_attr_vendorID,
1053 NULL
1054};
1055
1056static struct android_usb_function rndis_function = {
1057 .name = "rndis",
1058 .init = rndis_function_init,
1059 .cleanup = rndis_function_cleanup,
1060 .bind_config = rndis_function_bind_config,
1061 .unbind_config = rndis_function_unbind_config,
1062 .attributes = rndis_function_attributes,
1063};
1064
1065
1066struct mass_storage_function_config {
1067 struct fsg_config fsg;
1068 struct fsg_common *common;
1069};
1070
1071static int mass_storage_function_init(struct android_usb_function *f,
1072 struct usb_composite_dev *cdev)
1073{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001074 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001075 struct mass_storage_function_config *config;
1076 struct fsg_common *common;
1077 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301078 int i;
1079 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001080
1081 config = kzalloc(sizeof(struct mass_storage_function_config),
1082 GFP_KERNEL);
1083 if (!config)
1084 return -ENOMEM;
1085
1086 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301087 name[0] = "lun";
Pavankumar Kondeti2043e302012-07-19 08:54:04 +05301088 if (dev->pdata && dev->pdata->cdrom) {
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301089 config->fsg.nluns = 2;
1090 config->fsg.luns[1].cdrom = 1;
1091 config->fsg.luns[1].ro = 1;
1092 config->fsg.luns[1].removable = 1;
1093 name[1] = "lun0";
1094 }
1095
Benoit Goby1e8ce152011-12-12 13:01:23 -08001096 config->fsg.luns[0].removable = 1;
1097
1098 common = fsg_common_init(NULL, cdev, &config->fsg);
1099 if (IS_ERR(common)) {
1100 kfree(config);
1101 return PTR_ERR(common);
1102 }
1103
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301104 for (i = 0; i < config->fsg.nluns; i++) {
1105 err = sysfs_create_link(&f->dev->kobj,
1106 &common->luns[i].dev.kobj,
1107 name[i]);
1108 if (err)
1109 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001110 }
1111
1112 config->common = common;
1113 f->config = config;
1114 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301115error:
1116 for (; i > 0 ; i--)
1117 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1118
1119 fsg_common_release(&common->ref);
1120 kfree(config);
1121 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001122}
1123
1124static void mass_storage_function_cleanup(struct android_usb_function *f)
1125{
1126 kfree(f->config);
1127 f->config = NULL;
1128}
1129
1130static int mass_storage_function_bind_config(struct android_usb_function *f,
1131 struct usb_configuration *c)
1132{
1133 struct mass_storage_function_config *config = f->config;
1134 return fsg_bind_config(c->cdev, c, config->common);
1135}
1136
1137static ssize_t mass_storage_inquiry_show(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1139{
1140 struct android_usb_function *f = dev_get_drvdata(dev);
1141 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301142 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001143}
1144
1145static ssize_t mass_storage_inquiry_store(struct device *dev,
1146 struct device_attribute *attr, const char *buf, size_t size)
1147{
1148 struct android_usb_function *f = dev_get_drvdata(dev);
1149 struct mass_storage_function_config *config = f->config;
1150 if (size >= sizeof(config->common->inquiry_string))
1151 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301152 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001153 return -EINVAL;
1154 return size;
1155}
1156
1157static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1158 mass_storage_inquiry_show,
1159 mass_storage_inquiry_store);
1160
1161static struct device_attribute *mass_storage_function_attributes[] = {
1162 &dev_attr_inquiry_string,
1163 NULL
1164};
1165
1166static struct android_usb_function mass_storage_function = {
1167 .name = "mass_storage",
1168 .init = mass_storage_function_init,
1169 .cleanup = mass_storage_function_cleanup,
1170 .bind_config = mass_storage_function_bind_config,
1171 .attributes = mass_storage_function_attributes,
1172};
1173
1174
Benoit Gobycf3fc062011-12-19 14:39:37 -08001175static int accessory_function_init(struct android_usb_function *f,
1176 struct usb_composite_dev *cdev)
1177{
1178 return acc_setup();
1179}
1180
1181static void accessory_function_cleanup(struct android_usb_function *f)
1182{
1183 acc_cleanup();
1184}
1185
1186static int accessory_function_bind_config(struct android_usb_function *f,
1187 struct usb_configuration *c)
1188{
1189 return acc_bind_config(c);
1190}
1191
1192static int accessory_function_ctrlrequest(struct android_usb_function *f,
1193 struct usb_composite_dev *cdev,
1194 const struct usb_ctrlrequest *c)
1195{
1196 return acc_ctrlrequest(cdev, c);
1197}
1198
1199static struct android_usb_function accessory_function = {
1200 .name = "accessory",
1201 .init = accessory_function_init,
1202 .cleanup = accessory_function_cleanup,
1203 .bind_config = accessory_function_bind_config,
1204 .ctrlrequest = accessory_function_ctrlrequest,
1205};
1206
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301207static int android_uasp_connect_cb(bool connect)
1208{
1209 /*
1210 * TODO
1211 * We may have to disable gadget till UASP configfs nodes
1212 * are configured which includes mapping LUN with the
1213 * backing file. It is a fundamental difference between
1214 * f_mass_storage and f_tcp. That means UASP can not be
1215 * in default composition.
1216 *
1217 * For now, assume that UASP configfs nodes are configured
1218 * before enabling android gadget. Or cable should be
1219 * reconnected after mapping the LUN.
1220 *
1221 * Also consider making UASP to respond to Host requests when
1222 * Lun is not mapped.
1223 */
1224 pr_debug("UASP %s\n", connect ? "connect" : "disconnect");
1225
1226 return 0;
1227}
1228
1229static int uasp_function_init(struct android_usb_function *f,
1230 struct usb_composite_dev *cdev)
1231{
1232 return f_tcm_init(&android_uasp_connect_cb);
1233}
1234
1235static void uasp_function_cleanup(struct android_usb_function *f)
1236{
1237 f_tcm_exit();
1238}
1239
1240static int uasp_function_bind_config(struct android_usb_function *f,
1241 struct usb_configuration *c)
1242{
1243 return tcm_bind_config(c);
1244}
1245
1246static struct android_usb_function uasp_function = {
1247 .name = "uasp",
1248 .init = uasp_function_init,
1249 .cleanup = uasp_function_cleanup,
1250 .bind_config = uasp_function_bind_config,
1251};
Benoit Gobycf3fc062011-12-19 14:39:37 -08001252
Benoit Goby1e8ce152011-12-12 13:01:23 -08001253static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001254 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301255 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301256 &rmnet_sdio_function,
1257 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301258 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301260 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001261 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301262 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001263 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001264 &mtp_function,
1265 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001266 &rndis_function,
1267 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001268 &accessory_function,
Pavankumar Kondeti8f6ca4f2012-06-26 09:44:36 +05301269 &uasp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001270 NULL
1271};
1272
Lena Salmand092f2d2012-03-12 17:27:24 +02001273static void android_cleanup_functions(struct android_usb_function **functions)
1274{
1275 struct android_usb_function *f;
1276 struct device_attribute **attrs;
1277 struct device_attribute *attr;
1278
1279 while (*functions) {
1280 f = *functions++;
1281
1282 if (f->dev) {
1283 device_destroy(android_class, f->dev->devt);
1284 kfree(f->dev_name);
1285 } else
1286 continue;
1287
1288 if (f->cleanup)
1289 f->cleanup(f);
1290
1291 attrs = f->attributes;
1292 if (attrs) {
1293 while ((attr = *attrs++))
1294 device_remove_file(f->dev, attr);
1295 }
1296 }
1297}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001298
1299static int android_init_functions(struct android_usb_function **functions,
1300 struct usb_composite_dev *cdev)
1301{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001302 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001303 struct android_usb_function *f;
1304 struct device_attribute **attrs;
1305 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301306 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001307 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001308
1309 for (; (f = *functions++); index++) {
1310 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001311 f->android_dev = NULL;
Lena Salmand092f2d2012-03-12 17:27:24 +02001312 if (!f->dev_name) {
1313 err = -ENOMEM;
1314 goto err_out;
1315 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001316 f->dev = device_create(android_class, dev->dev,
1317 MKDEV(0, index), f, f->dev_name);
1318 if (IS_ERR(f->dev)) {
1319 pr_err("%s: Failed to create dev %s", __func__,
1320 f->dev_name);
1321 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001322 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001323 goto err_create;
1324 }
1325
1326 if (f->init) {
1327 err = f->init(f, cdev);
1328 if (err) {
1329 pr_err("%s: Failed to init %s", __func__,
1330 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001331 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001332 }
1333 }
1334
1335 attrs = f->attributes;
1336 if (attrs) {
1337 while ((attr = *attrs++) && !err)
1338 err = device_create_file(f->dev, attr);
1339 }
1340 if (err) {
1341 pr_err("%s: Failed to create function %s attributes",
1342 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001343 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001344 }
1345 }
1346 return 0;
1347
Lena Salmand092f2d2012-03-12 17:27:24 +02001348err_attrs:
1349 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1350 device_remove_file(f->dev, attr);
1351 if (f->cleanup)
1352 f->cleanup(f);
1353err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001354 device_destroy(android_class, f->dev->devt);
1355err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001356 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001357 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001358err_out:
1359 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001360 return err;
1361}
1362
Benoit Goby1e8ce152011-12-12 13:01:23 -08001363static int
1364android_bind_enabled_functions(struct android_dev *dev,
1365 struct usb_configuration *c)
1366{
1367 struct android_usb_function *f;
1368 int ret;
1369
1370 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1371 ret = f->bind_config(f, c);
1372 if (ret) {
1373 pr_err("%s: %s failed", __func__, f->name);
1374 return ret;
1375 }
1376 }
1377 return 0;
1378}
1379
1380static void
1381android_unbind_enabled_functions(struct android_dev *dev,
1382 struct usb_configuration *c)
1383{
1384 struct android_usb_function *f;
1385
1386 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1387 if (f->unbind_config)
1388 f->unbind_config(f, c);
1389 }
1390}
1391
1392static int android_enable_function(struct android_dev *dev, char *name)
1393{
1394 struct android_usb_function **functions = dev->functions;
1395 struct android_usb_function *f;
1396 while ((f = *functions++)) {
1397 if (!strcmp(name, f->name)) {
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001398 if (f->android_dev)
1399 pr_err("%s cannot be enabled on two devices\n",
1400 f->name);
1401 else {
1402 list_add_tail(&f->enabled_list,
1403 &dev->enabled_functions);
1404 f->android_dev = dev;
1405 return 0;
1406 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001407 }
1408 }
1409 return -EINVAL;
1410}
1411
1412/*-------------------------------------------------------------------------*/
1413/* /sys/class/android_usb/android%d/ interface */
1414
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301415static ssize_t remote_wakeup_show(struct device *pdev,
1416 struct device_attribute *attr, char *buf)
1417{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001418 struct android_dev *dev = dev_get_drvdata(pdev);
1419
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301420 return snprintf(buf, PAGE_SIZE, "%d\n",
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001421 !!(dev->config.bmAttributes &
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301422 USB_CONFIG_ATT_WAKEUP));
1423}
1424
1425static ssize_t remote_wakeup_store(struct device *pdev,
1426 struct device_attribute *attr, const char *buff, size_t size)
1427{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001428 struct android_dev *dev = dev_get_drvdata(pdev);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301429 int enable = 0;
1430
1431 sscanf(buff, "%d", &enable);
1432
1433 pr_debug("android_usb: %s remote wakeup\n",
1434 enable ? "enabling" : "disabling");
1435
1436 if (enable)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001437 dev->config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301438 else
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001439 dev->config.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301440
1441 return size;
1442}
1443
Benoit Goby1e8ce152011-12-12 13:01:23 -08001444static ssize_t
1445functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1446{
1447 struct android_dev *dev = dev_get_drvdata(pdev);
1448 struct android_usb_function *f;
1449 char *buff = buf;
1450
1451 mutex_lock(&dev->mutex);
1452
1453 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301454 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001455
1456 mutex_unlock(&dev->mutex);
1457
1458 if (buff != buf)
1459 *(buff-1) = '\n';
1460 return buff - buf;
1461}
1462
1463static ssize_t
1464functions_store(struct device *pdev, struct device_attribute *attr,
1465 const char *buff, size_t size)
1466{
1467 struct android_dev *dev = dev_get_drvdata(pdev);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001468 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001469 char *name;
1470 char buf[256], *b;
1471 int err;
1472
1473 mutex_lock(&dev->mutex);
1474
1475 if (dev->enabled) {
1476 mutex_unlock(&dev->mutex);
1477 return -EBUSY;
1478 }
1479
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001480 /* Clear previous enabled list */
1481 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1482 f->android_dev = NULL;
1483 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001484 INIT_LIST_HEAD(&dev->enabled_functions);
1485
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301486 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001487 b = strim(buf);
1488
1489 while (b) {
1490 name = strsep(&b, ",");
1491 if (name) {
1492 err = android_enable_function(dev, name);
1493 if (err)
1494 pr_err("android_usb: Cannot enable '%s'", name);
1495 }
1496 }
1497
1498 mutex_unlock(&dev->mutex);
1499
1500 return size;
1501}
1502
1503static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1504 char *buf)
1505{
1506 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001507
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301508 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001509}
1510
1511static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1512 const char *buff, size_t size)
1513{
1514 struct android_dev *dev = dev_get_drvdata(pdev);
1515 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001516 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001517 int enabled = 0;
1518
Benoit Gobycf3fc062011-12-19 14:39:37 -08001519 if (!cdev)
1520 return -ENODEV;
1521
Benoit Goby1e8ce152011-12-12 13:01:23 -08001522 mutex_lock(&dev->mutex);
1523
1524 sscanf(buff, "%d", &enabled);
1525 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001526 /*
1527 * Update values in composite driver's copy of
1528 * device descriptor.
1529 */
1530 cdev->desc.idVendor = device_desc.idVendor;
1531 cdev->desc.idProduct = device_desc.idProduct;
1532 cdev->desc.bcdDevice = device_desc.bcdDevice;
1533 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1534 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1535 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001536 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1537 if (f->enable)
1538 f->enable(f);
1539 }
1540 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001541 dev->enabled = true;
1542 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001543 android_disable(dev);
1544 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1545 if (f->disable)
1546 f->disable(f);
1547 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001548 dev->enabled = false;
1549 } else {
1550 pr_err("android_usb: already %s\n",
1551 dev->enabled ? "enabled" : "disabled");
1552 }
1553
1554 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001555
Benoit Gobyaab96812011-04-19 20:37:33 -07001556 return size;
1557}
1558
Ofir Cohen94213a72012-05-03 14:26:32 +03001559static ssize_t pm_qos_show(struct device *pdev,
1560 struct device_attribute *attr, char *buf)
1561{
1562 struct android_dev *dev = dev_get_drvdata(pdev);
1563
1564 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1565}
1566
1567static ssize_t pm_qos_store(struct device *pdev,
1568 struct device_attribute *attr,
1569 const char *buff, size_t size)
1570{
1571 struct android_dev *dev = dev_get_drvdata(pdev);
1572
1573 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1574
Benoit Goby1e8ce152011-12-12 13:01:23 -08001575 return size;
1576}
1577
1578static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1579 char *buf)
1580{
1581 struct android_dev *dev = dev_get_drvdata(pdev);
1582 struct usb_composite_dev *cdev = dev->cdev;
1583 char *state = "DISCONNECTED";
1584 unsigned long flags;
1585
1586 if (!cdev)
1587 goto out;
1588
1589 spin_lock_irqsave(&cdev->lock, flags);
1590 if (cdev->config)
1591 state = "CONFIGURED";
1592 else if (dev->connected)
1593 state = "CONNECTED";
1594 spin_unlock_irqrestore(&cdev->lock, flags);
1595out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301596 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001597}
1598
1599#define DESCRIPTOR_ATTR(field, format_string) \
1600static ssize_t \
1601field ## _show(struct device *dev, struct device_attribute *attr, \
1602 char *buf) \
1603{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301604 return snprintf(buf, PAGE_SIZE, \
1605 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001606} \
1607static ssize_t \
1608field ## _store(struct device *dev, struct device_attribute *attr, \
1609 const char *buf, size_t size) \
1610{ \
1611 int value; \
1612 if (sscanf(buf, format_string, &value) == 1) { \
1613 device_desc.field = value; \
1614 return size; \
1615 } \
1616 return -1; \
1617} \
1618static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1619
1620#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1621static ssize_t \
1622field ## _show(struct device *dev, struct device_attribute *attr, \
1623 char *buf) \
1624{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301625 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001626} \
1627static ssize_t \
1628field ## _store(struct device *dev, struct device_attribute *attr, \
1629 const char *buf, size_t size) \
1630{ \
1631 if (size >= sizeof(buffer)) \
1632 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301633 strlcpy(buffer, buf, sizeof(buffer)); \
1634 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301635 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001636} \
1637static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1638
1639
1640DESCRIPTOR_ATTR(idVendor, "%04x\n")
1641DESCRIPTOR_ATTR(idProduct, "%04x\n")
1642DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1643DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1644DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1645DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1646DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1647DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1648DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1649
1650static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1651 functions_store);
1652static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001653static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1654 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001655static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301656static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1657 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001658
1659static struct device_attribute *android_usb_attributes[] = {
1660 &dev_attr_idVendor,
1661 &dev_attr_idProduct,
1662 &dev_attr_bcdDevice,
1663 &dev_attr_bDeviceClass,
1664 &dev_attr_bDeviceSubClass,
1665 &dev_attr_bDeviceProtocol,
1666 &dev_attr_iManufacturer,
1667 &dev_attr_iProduct,
1668 &dev_attr_iSerial,
1669 &dev_attr_functions,
1670 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001671 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001672 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301673 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001674 NULL
1675};
1676
1677/*-------------------------------------------------------------------------*/
1678/* Composite driver */
1679
1680static int android_bind_config(struct usb_configuration *c)
1681{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001682 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001683 int ret = 0;
1684
1685 ret = android_bind_enabled_functions(dev, c);
1686 if (ret)
1687 return ret;
1688
1689 return 0;
1690}
1691
1692static void android_unbind_config(struct usb_configuration *c)
1693{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001694 struct android_dev *dev = cdev_to_android_dev(c->cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001695
1696 android_unbind_enabled_functions(dev, c);
1697}
1698
1699static int android_bind(struct usb_composite_dev *cdev)
1700{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001701 struct android_dev *dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001702 struct usb_gadget *gadget = cdev->gadget;
1703 int gcnum, id, ret;
1704
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001705 /* Bind to the last android_dev that was probed */
1706 dev = list_entry(android_dev_list.prev, struct android_dev, list_item);
1707
1708 dev->cdev = cdev;
1709
Benoit Goby1e8ce152011-12-12 13:01:23 -08001710 /*
1711 * Start disconnected. Userspace will connect the gadget once
1712 * it is done configuring the functions.
1713 */
1714 usb_gadget_disconnect(gadget);
1715
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001716 /* Init the supported functions only once, on the first android_dev */
1717 if (android_dev_count == 1) {
1718 ret = android_init_functions(dev->functions, cdev);
1719 if (ret)
1720 return ret;
1721 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001722
1723 /* Allocate string descriptor numbers ... note that string
1724 * contents can be overridden by the composite_dev glue.
1725 */
1726 id = usb_string_id(cdev);
1727 if (id < 0)
1728 return id;
1729 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1730 device_desc.iManufacturer = id;
1731
1732 id = usb_string_id(cdev);
1733 if (id < 0)
1734 return id;
1735 strings_dev[STRING_PRODUCT_IDX].id = id;
1736 device_desc.iProduct = id;
1737
1738 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301739 strlcpy(manufacturer_string, "Android",
1740 sizeof(manufacturer_string) - 1);
1741 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1742 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001743
1744 id = usb_string_id(cdev);
1745 if (id < 0)
1746 return id;
1747 strings_dev[STRING_SERIAL_IDX].id = id;
1748 device_desc.iSerialNumber = id;
1749
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301750 if (gadget_is_otg(cdev->gadget))
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001751 dev->config.descriptors = otg_desc;
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301752
Benoit Goby1e8ce152011-12-12 13:01:23 -08001753 gcnum = usb_gadget_controller_number(gadget);
1754 if (gcnum >= 0)
1755 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1756 else {
1757 pr_warning("%s: controller '%s' not recognized\n",
1758 longname, gadget->name);
1759 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1760 }
1761
Benoit Goby1e8ce152011-12-12 13:01:23 -08001762 return 0;
1763}
1764
1765static int android_usb_unbind(struct usb_composite_dev *cdev)
1766{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001767 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001768
Lena Salmand092f2d2012-03-12 17:27:24 +02001769 manufacturer_string[0] = '\0';
1770 product_string[0] = '\0';
1771 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001772 cancel_work_sync(&dev->work);
1773 android_cleanup_functions(dev->functions);
1774 return 0;
1775}
1776
1777static struct usb_composite_driver android_usb_driver = {
1778 .name = "android_usb",
1779 .dev = &device_desc,
1780 .strings = dev_strings,
1781 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001782 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001783};
1784
1785static int
1786android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1787{
Benoit Goby1e8ce152011-12-12 13:01:23 -08001788 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001789 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001790 struct usb_request *req = cdev->req;
1791 struct android_usb_function *f;
1792 int value = -EOPNOTSUPP;
1793 unsigned long flags;
1794
1795 req->zero = 0;
1796 req->complete = composite_setup_complete;
1797 req->length = 0;
1798 gadget->ep0->driver_data = cdev;
1799
1800 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1801 if (f->ctrlrequest) {
1802 value = f->ctrlrequest(f, cdev, c);
1803 if (value >= 0)
1804 break;
1805 }
1806 }
1807
Benoit Gobycf3fc062011-12-19 14:39:37 -08001808 /* Special case the accessory function.
1809 * It needs to handle control requests before it is enabled.
1810 */
1811 if (value < 0)
1812 value = acc_ctrlrequest(cdev, c);
1813
Benoit Goby1e8ce152011-12-12 13:01:23 -08001814 if (value < 0)
1815 value = composite_setup(gadget, c);
1816
1817 spin_lock_irqsave(&cdev->lock, flags);
1818 if (!dev->connected) {
1819 dev->connected = 1;
1820 schedule_work(&dev->work);
1821 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1822 cdev->config) {
1823 schedule_work(&dev->work);
1824 }
1825 spin_unlock_irqrestore(&cdev->lock, flags);
1826
1827 return value;
1828}
1829
1830static void android_disconnect(struct usb_gadget *gadget)
1831{
Benoit Goby1e8ce152011-12-12 13:01:23 -08001832 struct usb_composite_dev *cdev = get_gadget_data(gadget);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001833 struct android_dev *dev = cdev_to_android_dev(cdev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001834 unsigned long flags;
1835
1836 composite_disconnect(gadget);
1837
1838 spin_lock_irqsave(&cdev->lock, flags);
1839 dev->connected = 0;
1840 schedule_work(&dev->work);
1841 spin_unlock_irqrestore(&cdev->lock, flags);
1842}
1843
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001844static int android_create_device(struct android_dev *dev, u8 usb_core_id)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001845{
1846 struct device_attribute **attrs = android_usb_attributes;
1847 struct device_attribute *attr;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001848 char device_node_name[ANDROID_DEVICE_NODE_NAME_LENGTH];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001849 int err;
1850
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001851 /*
1852 * The primary usb core should always have usb_core_id=0, since
1853 * Android user space is currently interested in android0 events.
1854 */
1855 snprintf(device_node_name, ANDROID_DEVICE_NODE_NAME_LENGTH,
1856 "android%d", usb_core_id);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001857 dev->dev = device_create(android_class, NULL,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001858 MKDEV(0, 0), NULL, device_node_name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001859 if (IS_ERR(dev->dev))
1860 return PTR_ERR(dev->dev);
1861
1862 dev_set_drvdata(dev->dev, dev);
1863
1864 while ((attr = *attrs++)) {
1865 err = device_create_file(dev->dev, attr);
1866 if (err) {
1867 device_destroy(android_class, dev->dev->devt);
1868 return err;
1869 }
1870 }
1871 return 0;
1872}
1873
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301874static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001875{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301876 struct device_attribute **attrs = android_usb_attributes;
1877 struct device_attribute *attr;
1878
1879 while ((attr = *attrs++))
1880 device_remove_file(dev->dev, attr);
1881 device_destroy(android_class, dev->dev->devt);
1882}
1883
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001884static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev)
1885{
1886 struct android_dev *dev = NULL;
1887
1888 /* Find the android dev from the list */
1889 list_for_each_entry(dev, &android_dev_list, list_item) {
1890 if (dev->cdev == cdev)
1891 break;
1892 }
1893
1894 return dev;
1895}
1896
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897static int __devinit android_probe(struct platform_device *pdev)
1898{
1899 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001900 struct android_dev *android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001901 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001903 if (!android_class) {
1904 android_class = class_create(THIS_MODULE, "android_usb");
1905 if (IS_ERR(android_class))
1906 return PTR_ERR(android_class);
1907 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001908
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001909 android_dev = kzalloc(sizeof(*android_dev), GFP_KERNEL);
1910 if (!android_dev) {
1911 pr_err("%s(): Failed to alloc memory for android_dev\n",
1912 __func__);
1913 ret = -ENOMEM;
1914 goto err_alloc;
1915 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001916
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001917 android_dev->config.label = pdev->name;
1918 android_dev->config.unbind = android_unbind_config;
1919 android_dev->config.bConfigurationValue = 1;
1920 android_dev->disable_depth = 1;
1921 android_dev->functions = supported_functions;
1922 INIT_LIST_HEAD(&android_dev->enabled_functions);
1923 INIT_WORK(&android_dev->work, android_work);
1924 mutex_init(&android_dev->mutex);
1925
1926 android_dev->pdata = pdata;
1927
1928 list_add_tail(&android_dev->list_item, &android_dev_list);
1929 android_dev_count++;
1930
1931 if (pdata)
1932 composite_driver.usb_core_id = pdata->usb_core_id;
1933 else
1934 composite_driver.usb_core_id = 0; /*To backward compatibility*/
1935
1936 ret = android_create_device(android_dev, composite_driver.usb_core_id);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301937 if (ret) {
1938 pr_err("%s(): android_create_device failed\n", __func__);
1939 goto err_dev;
1940 }
1941
Lena Salmand092f2d2012-03-12 17:27:24 +02001942 ret = usb_composite_probe(&android_usb_driver, android_bind);
1943 if (ret) {
1944 pr_err("%s(): Failed to register android "
1945 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301946 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001947 }
1948
Ofir Cohen94213a72012-05-03 14:26:32 +03001949 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301950 if (pdata && pdata->swfi_latency)
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001951 pm_qos_add_request(&android_dev->pm_qos_req_dma,
Ofir Cohen94213a72012-05-03 14:26:32 +03001952 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001953 strlcpy(android_dev->pm_qos, "high", sizeof(android_dev->pm_qos));
Ofir Cohen94213a72012-05-03 14:26:32 +03001954
Lena Salmand092f2d2012-03-12 17:27:24 +02001955 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301956err_probe:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001957 android_destroy_device(android_dev);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301958err_dev:
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001959 list_del(&android_dev->list_item);
1960 android_dev_count--;
1961 kfree(android_dev);
1962err_alloc:
1963 if (list_empty(&android_dev_list)) {
1964 class_destroy(android_class);
1965 android_class = NULL;
1966 }
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301967 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001968}
1969
1970static int android_remove(struct platform_device *pdev)
1971{
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001972 struct android_dev *dev = NULL;
Ofir Cohen94213a72012-05-03 14:26:32 +03001973 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001974 int usb_core_id = 0;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301975
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03001976 if (pdata)
1977 usb_core_id = pdata->usb_core_id;
1978
1979 /* Find the android dev from the list */
1980 list_for_each_entry(dev, &android_dev_list, list_item) {
1981 if (!dev->pdata)
1982 break; /*To backward compatibility*/
1983 if (dev->pdata->usb_core_id == usb_core_id)
1984 break;
1985 }
1986
1987 if (dev) {
1988 android_destroy_device(dev);
1989 if (pdata && pdata->swfi_latency)
1990 pm_qos_remove_request(&dev->pm_qos_req_dma);
1991 list_del(&dev->list_item);
1992 android_dev_count--;
1993 kfree(dev);
1994 }
1995
1996 if (list_empty(&android_dev_list)) {
1997 class_destroy(android_class);
1998 android_class = NULL;
1999 usb_composite_unregister(&android_usb_driver);
2000 }
Ofir Cohen94213a72012-05-03 14:26:32 +03002001
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002 return 0;
2003}
2004
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002005static const struct platform_device_id android_id_table[] __devinitconst = {
2006 {
2007 .name = "android_usb",
2008 },
2009 {
2010 .name = "android_usb_hsic",
2011 },
2012};
2013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002014static struct platform_driver android_platform_driver = {
2015 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02002016 .probe = android_probe,
2017 .remove = android_remove,
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002018 .id_table = android_id_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002019};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002020
2021static int __init init(void)
2022{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302023 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05002024
Benoit Goby1e8ce152011-12-12 13:01:23 -08002025 /* Override composite driver functions */
2026 composite_driver.setup = android_setup;
2027 composite_driver.disconnect = android_disconnect;
2028
Ido Shayevitz23dc77c2012-07-18 16:16:06 +03002029 INIT_LIST_HEAD(&android_dev_list);
2030 android_dev_count = 0;
2031
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05302032 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302033 if (ret) {
2034 pr_err("%s(): Failed to register android"
2035 "platform driver\n", __func__);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302036 }
Lena Salmand092f2d2012-03-12 17:27:24 +02002037
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05302038 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08002039}
2040module_init(init);
2041
2042static void __exit cleanup(void)
2043{
Lena Salmand092f2d2012-03-12 17:27:24 +02002044 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08002045}
2046module_exit(cleanup);