blob: f4ca8e1b22522eca1bc13a1fe6bcbdd80bc74e8d [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05305 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05006 * Author: Mike Lockwood <lockwood@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/* #define DEBUG */
20/* #define VERBOSE_DEBUG */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/fs.h>
25
26#include <linux/delay.h>
27#include <linux/kernel.h>
28#include <linux/utsname.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050029#include <linux/platform_device.h>
Ofir Cohen94213a72012-05-03 14:26:32 +030030#include <linux/pm_qos_params.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050031
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050032#include <linux/usb/ch9.h>
33#include <linux/usb/composite.h>
34#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <linux/usb/android.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050036
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050037#include "gadget_chips.h"
38
39/*
40 * Kbuild is not very cooperative with respect to linking separately
41 * compiled library objects into one module. So for now we won't use
42 * separate compilation ... ensuring init/exit sections work to shrink
43 * the runtime footprint, and giving us at least some parts of what
44 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
45 */
46#include "usbstring.c"
47#include "config.c"
48#include "epautoconf.c"
49#include "composite.c"
50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053052#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053053#include "f_rmnet_sdio.c"
54#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053055#include "f_rmnet.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070056#include "f_mass_storage.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053057#include "u_serial.c"
58#include "u_sdio.c"
59#include "u_smd.c"
60#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053061#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080062#include "u_ctrl_hsic.c"
63#include "u_data_hsic.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053064#include "f_serial.c"
Anji jonnala92be1b42011-12-19 09:44:41 +053065#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070066#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053067#include "f_ccid.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070068#include "f_mtp.c"
69#include "f_accessory.c"
70#define USB_ETH_RNDIS y
71#include "f_rndis.c"
72#include "rndis.c"
73#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030074#include "u_bam_data.c"
75#include "f_mbim.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070076
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050077MODULE_AUTHOR("Mike Lockwood");
78MODULE_DESCRIPTION("Android Composite USB Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION("1.0");
81
82static const char longname[] = "Gadget Android";
83
Benoit Gobyaab96812011-04-19 20:37:33 -070084/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050085#define VENDOR_ID 0x18D1
86#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050087
Benoit Gobyaab96812011-04-19 20:37:33 -070088struct android_usb_function {
89 char *name;
90 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050091
Benoit Gobyaab96812011-04-19 20:37:33 -070092 struct device *dev;
93 char *dev_name;
94 struct device_attribute **attributes;
95
96 /* for android_dev.enabled_functions */
97 struct list_head enabled_list;
98
99 /* Optional: initialization during gadget bind */
100 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
101 /* Optional: cleanup during gadget unbind */
102 void (*cleanup)(struct android_usb_function *);
103
104 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
105
106 /* Optional: called when the configuration is removed */
107 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700108 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700109 int (*ctrlrequest)(struct android_usb_function *,
110 struct usb_composite_dev *,
111 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500112};
113
Benoit Gobyaab96812011-04-19 20:37:33 -0700114struct android_dev {
115 struct android_usb_function **functions;
116 struct list_head enabled_functions;
117 struct usb_composite_dev *cdev;
118 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700120
121 bool enabled;
Benoit Gobydc1b6342011-12-09 18:05:00 -0800122 struct mutex mutex;
Benoit Gobyaab96812011-04-19 20:37:33 -0700123 bool connected;
124 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300125 char pm_qos[5];
126 struct pm_qos_request_list pm_qos_req_dma;
Benoit Gobyaab96812011-04-19 20:37:33 -0700127 struct work_struct work;
128};
129
130static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500131static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700132static int android_bind_config(struct usb_configuration *c);
133static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500134
135/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500136#define STRING_MANUFACTURER_IDX 0
137#define STRING_PRODUCT_IDX 1
138#define STRING_SERIAL_IDX 2
139
Benoit Gobyaab96812011-04-19 20:37:33 -0700140static char manufacturer_string[256];
141static char product_string[256];
142static char serial_string[256];
143
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500144/* String Table */
145static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700146 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
147 [STRING_PRODUCT_IDX].s = product_string,
148 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500149 { } /* end of list */
150};
151
152static struct usb_gadget_strings stringtab_dev = {
153 .language = 0x0409, /* en-us */
154 .strings = strings_dev,
155};
156
157static struct usb_gadget_strings *dev_strings[] = {
158 &stringtab_dev,
159 NULL,
160};
161
162static struct usb_device_descriptor device_desc = {
163 .bLength = sizeof(device_desc),
164 .bDescriptorType = USB_DT_DEVICE,
165 .bcdUSB = __constant_cpu_to_le16(0x0200),
166 .bDeviceClass = USB_CLASS_PER_INTERFACE,
167 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
168 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
169 .bcdDevice = __constant_cpu_to_le16(0xffff),
170 .bNumConfigurations = 1,
171};
172
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530173static struct usb_otg_descriptor otg_descriptor = {
174 .bLength = sizeof otg_descriptor,
175 .bDescriptorType = USB_DT_OTG,
176 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
177 .bcdOTG = __constant_cpu_to_le16(0x0200),
178};
179
180static const struct usb_descriptor_header *otg_desc[] = {
181 (struct usb_descriptor_header *) &otg_descriptor,
182 NULL,
183};
184
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500185static struct usb_configuration android_config_driver = {
186 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700187 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500188 .bConfigurationValue = 1,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500189};
190
Manu Gautama2b54142012-04-03 14:34:32 +0530191enum android_device_state {
192 USB_DISCONNECTED,
193 USB_CONNECTED,
194 USB_CONFIGURED,
195};
196
Ofir Cohen94213a72012-05-03 14:26:32 +0300197static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
198{
199 struct android_usb_platform_data *pdata = dev->pdata;
200 u32 swfi_latency = 0;
201 static int last_vote = -1;
202
Ofir Cohen56eb7072012-05-20 11:41:39 +0300203 if (!pdata || vote == last_vote
204 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300205 return;
206
207 swfi_latency = pdata->swfi_latency + 1;
208 if (vote)
209 pm_qos_update_request(&dev->pm_qos_req_dma,
210 swfi_latency);
211 else
212 pm_qos_update_request(&dev->pm_qos_req_dma,
213 PM_QOS_DEFAULT_VALUE);
214 last_vote = vote;
215}
216
Benoit Gobyaab96812011-04-19 20:37:33 -0700217static void android_work(struct work_struct *data)
218{
219 struct android_dev *dev = container_of(data, struct android_dev, work);
220 struct usb_composite_dev *cdev = dev->cdev;
221 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
222 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
223 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700224 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530225 static enum android_device_state last_uevent, next_state;
Benoit Gobyaab96812011-04-19 20:37:33 -0700226 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300227 int pm_qos_vote = -1;
Benoit Gobyaab96812011-04-19 20:37:33 -0700228
229 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530230 if (cdev->config) {
Dima Zavinfc753492011-09-14 11:52:45 -0700231 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530232 next_state = USB_CONFIGURED;
233 } else if (dev->connected != dev->sw_connected) {
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700234 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530235 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300236 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300237 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300238 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300239 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530240 }
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700241 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700242 spin_unlock_irqrestore(&cdev->lock, flags);
243
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300244 if (pm_qos_vote != -1)
245 android_pm_qos_update_latency(dev, pm_qos_vote);
246
Dima Zavinfc753492011-09-14 11:52:45 -0700247 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530248 /*
249 * Some userspace modules, e.g. MTP, work correctly only if
250 * CONFIGURED uevent is preceded by DISCONNECT uevent.
251 * Check if we missed sending out a DISCONNECT uevent. This can
252 * happen if host PC resets and configures device really quick.
253 */
254 if (((uevent_envp == connected) &&
255 (last_uevent != USB_DISCONNECTED)) ||
256 ((uevent_envp == configured) &&
257 (last_uevent == USB_CONFIGURED))) {
258 pr_info("%s: sent missed DISCONNECT event\n", __func__);
259 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
260 disconnected);
261 msleep(20);
262 }
263 /*
264 * Before sending out CONFIGURED uevent give function drivers
265 * a chance to wakeup userspace threads and notify disconnect
266 */
267 if (uevent_envp == configured)
268 msleep(50);
269
Dima Zavinfc753492011-09-14 11:52:45 -0700270 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530271 last_uevent = next_state;
Dima Zavinfc753492011-09-14 11:52:45 -0700272 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700273 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700274 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
275 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700276 }
277}
278
279
280/*-------------------------------------------------------------------------*/
281/* Supported functions initialization */
282
Manu Gautam8e0719b2011-09-26 14:47:55 +0530283/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530284static int rmnet_smd_function_bind_config(struct android_usb_function *f,
285 struct usb_configuration *c)
286{
287 return rmnet_smd_bind_config(c);
288}
289
290static struct android_usb_function rmnet_smd_function = {
291 .name = "rmnet_smd",
292 .bind_config = rmnet_smd_function_bind_config,
293};
294
Manu Gautam8e0719b2011-09-26 14:47:55 +0530295/* RMNET_SDIO */
296static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
297 struct usb_configuration *c)
298{
299 return rmnet_sdio_function_add(c);
300}
301
302static struct android_usb_function rmnet_sdio_function = {
303 .name = "rmnet_sdio",
304 .bind_config = rmnet_sdio_function_bind_config,
305};
306
307/* RMNET_SMD_SDIO */
308static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
309 struct usb_composite_dev *cdev)
310{
311 return rmnet_smd_sdio_init();
312}
313
314static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
315{
316 rmnet_smd_sdio_cleanup();
317}
318
319static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
320 struct usb_configuration *c)
321{
322 return rmnet_smd_sdio_function_add(c);
323}
324
325static struct device_attribute *rmnet_smd_sdio_attributes[] = {
326 &dev_attr_transport, NULL };
327
328static struct android_usb_function rmnet_smd_sdio_function = {
329 .name = "rmnet_smd_sdio",
330 .init = rmnet_smd_sdio_function_init,
331 .cleanup = rmnet_smd_sdio_function_cleanup,
332 .bind_config = rmnet_smd_sdio_bind_config,
333 .attributes = rmnet_smd_sdio_attributes,
334};
335
Hemant Kumar1b820d52011-11-03 15:08:28 -0700336/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
337#define MAX_XPORT_STR_LEN 50
338static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530339
Manu Gautame3e897c2011-09-12 17:18:46 +0530340static void rmnet_function_cleanup(struct android_usb_function *f)
341{
342 frmnet_cleanup();
343}
344
Manu Gautam2b0234a2011-09-07 16:47:52 +0530345static int rmnet_function_bind_config(struct android_usb_function *f,
346 struct usb_configuration *c)
347{
348 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700349 int err = 0;
350 char *ctrl_name;
351 char *data_name;
352 char buf[MAX_XPORT_STR_LEN], *b;
353 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530354
Hemant Kumar1b820d52011-11-03 15:08:28 -0700355 if (!rmnet_initialized) {
356 rmnet_initialized = 1;
357 strlcpy(buf, rmnet_transports, sizeof(buf));
358 b = strim(buf);
359 while (b) {
360 ctrl_name = strsep(&b, ",");
361 data_name = strsep(&b, ",");
362 if (ctrl_name && data_name) {
363 err = frmnet_init_port(ctrl_name, data_name);
364 if (err) {
365 pr_err("rmnet: Cannot open ctrl port:"
366 "'%s' data port:'%s'\n",
367 ctrl_name, data_name);
368 goto out;
369 }
370 ports++;
371 }
372 }
373
374 err = rmnet_gport_setup();
375 if (err) {
376 pr_err("rmnet: Cannot setup transports");
377 goto out;
378 }
379 }
380
381 for (i = 0; i < ports; i++) {
382 err = frmnet_bind_config(c, i);
383 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530384 pr_err("Could not bind rmnet%u config\n", i);
385 break;
386 }
387 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700388out:
389 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530390}
391
Hemant Kumar1b820d52011-11-03 15:08:28 -0700392static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530393 struct device_attribute *attr, char *buf)
394{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700395 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530396}
397
Hemant Kumar1b820d52011-11-03 15:08:28 -0700398static ssize_t rmnet_transports_store(
399 struct device *device, struct device_attribute *attr,
400 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530401{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700402 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530403
Manu Gautam2b0234a2011-09-07 16:47:52 +0530404 return size;
405}
406
Hemant Kumar1b820d52011-11-03 15:08:28 -0700407static struct device_attribute dev_attr_rmnet_transports =
408 __ATTR(transports, S_IRUGO | S_IWUSR,
409 rmnet_transports_show,
410 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530411static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700412 &dev_attr_rmnet_transports,
413 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530414
415static struct android_usb_function rmnet_function = {
416 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530417 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530418 .bind_config = rmnet_function_bind_config,
419 .attributes = rmnet_function_attributes,
420};
421
Anna Perela8c991d2012-04-09 16:44:46 +0300422
423/* MBIM - used with BAM */
424#define MAX_MBIM_INSTANCES 1
425
426static int mbim_function_init(struct android_usb_function *f,
427 struct usb_composite_dev *cdev)
428{
429 return mbim_init(MAX_MBIM_INSTANCES);
430}
431
432static void mbim_function_cleanup(struct android_usb_function *f)
433{
434 fmbim_cleanup();
435}
436
437static int mbim_function_bind_config(struct android_usb_function *f,
438 struct usb_configuration *c)
439{
440 return mbim_bind_config(c, 0);
441}
442
443static struct android_usb_function mbim_function = {
444 .name = "usb_mbim",
445 .cleanup = mbim_function_cleanup,
446 .bind_config = mbim_function_bind_config,
447 .init = mbim_function_init,
448};
449
450
Manu Gautam8e0719b2011-09-26 14:47:55 +0530451/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530452static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453static ssize_t clients_store(
454 struct device *device, struct device_attribute *attr,
455 const char *buff, size_t size)
456{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530457 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
459 return size;
460}
461
462static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
463static struct device_attribute *diag_function_attributes[] =
464 { &dev_attr_clients, NULL };
465
466static int diag_function_init(struct android_usb_function *f,
467 struct usb_composite_dev *cdev)
468{
469 return diag_setup();
470}
471
472static void diag_function_cleanup(struct android_usb_function *f)
473{
474 diag_cleanup();
475}
476
477static int diag_function_bind_config(struct android_usb_function *f,
478 struct usb_configuration *c)
479{
480 char *name;
481 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530482 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800483 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530485 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 b = strim(buf);
487
488 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800489 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530491 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700492 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530493 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530494
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530496 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 if (err)
498 pr_err("diag: Cannot open channel '%s'", name);
499 }
500 }
501
502 return err;
503}
504
505static struct android_usb_function diag_function = {
506 .name = "diag",
507 .init = diag_function_init,
508 .cleanup = diag_function_cleanup,
509 .bind_config = diag_function_bind_config,
510 .attributes = diag_function_attributes,
511};
512
Manu Gautam8e0719b2011-09-26 14:47:55 +0530513/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530514static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530515static ssize_t serial_transports_store(
516 struct device *device, struct device_attribute *attr,
517 const char *buff, size_t size)
518{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530519 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530520
521 return size;
522}
523
524static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
525static struct device_attribute *serial_function_attributes[] =
526 { &dev_attr_transports, NULL };
527
528static void serial_function_cleanup(struct android_usb_function *f)
529{
530 gserial_cleanup();
531}
532
533static int serial_function_bind_config(struct android_usb_function *f,
534 struct usb_configuration *c)
535{
536 char *name;
537 char buf[32], *b;
538 int err = -1, i;
539 static int serial_initialized = 0, ports = 0;
540
541 if (serial_initialized)
542 goto bind_config;
543
544 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530545 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530546 b = strim(buf);
547
548 while (b) {
549 name = strsep(&b, ",");
550
551 if (name) {
552 err = gserial_init_port(ports, name);
553 if (err) {
554 pr_err("serial: Cannot open port '%s'", name);
555 goto out;
556 }
557 ports++;
558 }
559 }
560 err = gport_setup(c);
561 if (err) {
562 pr_err("serial: Cannot setup transports");
563 goto out;
564 }
565
566bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200567 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530568 err = gser_bind_config(c, i);
569 if (err) {
570 pr_err("serial: bind_config failed for port %d", i);
571 goto out;
572 }
573 }
574
575out:
576 return err;
577}
578
579static struct android_usb_function serial_function = {
580 .name = "serial",
581 .cleanup = serial_function_cleanup,
582 .bind_config = serial_function_bind_config,
583 .attributes = serial_function_attributes,
584};
585
Anji jonnala92be1b42011-12-19 09:44:41 +0530586/* ACM */
587static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
588static ssize_t acm_transports_store(
589 struct device *device, struct device_attribute *attr,
590 const char *buff, size_t size)
591{
592 strlcpy(acm_transports, buff, sizeof(acm_transports));
593
594 return size;
595}
596
597static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
598static struct device_attribute *acm_function_attributes[] = {
599 &dev_attr_acm_transports, NULL };
600
601static void acm_function_cleanup(struct android_usb_function *f)
602{
603 gserial_cleanup();
604}
605
606static int acm_function_bind_config(struct android_usb_function *f,
607 struct usb_configuration *c)
608{
609 char *name;
610 char buf[32], *b;
611 int err = -1, i;
612 static int acm_initialized, ports;
613
614 if (acm_initialized)
615 goto bind_config;
616
617 acm_initialized = 1;
618 strlcpy(buf, acm_transports, sizeof(buf));
619 b = strim(buf);
620
621 while (b) {
622 name = strsep(&b, ",");
623
624 if (name) {
625 err = acm_init_port(ports, name);
626 if (err) {
627 pr_err("acm: Cannot open port '%s'", name);
628 goto out;
629 }
630 ports++;
631 }
632 }
633 err = acm_port_setup(c);
634 if (err) {
635 pr_err("acm: Cannot setup transports");
636 goto out;
637 }
638
639bind_config:
640 for (i = 0; i < ports; i++) {
641 err = acm_bind_config(c, i);
642 if (err) {
643 pr_err("acm: bind_config failed for port %d", i);
644 goto out;
645 }
646 }
647
648out:
649 return err;
650}
651static struct android_usb_function acm_function = {
652 .name = "acm",
653 .cleanup = acm_function_cleanup,
654 .bind_config = acm_function_bind_config,
655 .attributes = acm_function_attributes,
656};
Manu Gautama4d993f2011-08-30 18:25:55 +0530657
Manu Gautam8e0719b2011-09-26 14:47:55 +0530658/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700659static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
660{
661 return adb_setup();
662}
663
664static void adb_function_cleanup(struct android_usb_function *f)
665{
666 adb_cleanup();
667}
668
669static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
670{
671 return adb_bind_config(c);
672}
673
674static struct android_usb_function adb_function = {
675 .name = "adb",
676 .init = adb_function_init,
677 .cleanup = adb_function_cleanup,
678 .bind_config = adb_function_bind_config,
679};
680
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530681/* CCID */
682static int ccid_function_init(struct android_usb_function *f,
683 struct usb_composite_dev *cdev)
684{
685 return ccid_setup();
686}
687
688static void ccid_function_cleanup(struct android_usb_function *f)
689{
690 ccid_cleanup();
691}
692
693static int ccid_function_bind_config(struct android_usb_function *f,
694 struct usb_configuration *c)
695{
696 return ccid_bind_config(c);
697}
698
699static struct android_usb_function ccid_function = {
700 .name = "ccid",
701 .init = ccid_function_init,
702 .cleanup = ccid_function_cleanup,
703 .bind_config = ccid_function_bind_config,
704};
705
Benoit Gobyaab96812011-04-19 20:37:33 -0700706static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
707{
708 return mtp_setup();
709}
710
711static void mtp_function_cleanup(struct android_usb_function *f)
712{
713 mtp_cleanup();
714}
715
716static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
717{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400718 return mtp_bind_config(c, false);
719}
720
721static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
722{
723 /* nothing to do - initialization is handled by mtp_function_init */
724 return 0;
725}
726
727static void ptp_function_cleanup(struct android_usb_function *f)
728{
729 /* nothing to do - cleanup is handled by mtp_function_cleanup */
730}
731
732static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
733{
734 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700735}
736
737static int mtp_function_ctrlrequest(struct android_usb_function *f,
738 struct usb_composite_dev *cdev,
739 const struct usb_ctrlrequest *c)
740{
741 return mtp_ctrlrequest(cdev, c);
742}
743
744static struct android_usb_function mtp_function = {
745 .name = "mtp",
746 .init = mtp_function_init,
747 .cleanup = mtp_function_cleanup,
748 .bind_config = mtp_function_bind_config,
749 .ctrlrequest = mtp_function_ctrlrequest,
750};
751
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400752/* PTP function is same as MTP with slightly different interface descriptor */
753static struct android_usb_function ptp_function = {
754 .name = "ptp",
755 .init = ptp_function_init,
756 .cleanup = ptp_function_cleanup,
757 .bind_config = ptp_function_bind_config,
758};
759
Benoit Gobyaab96812011-04-19 20:37:33 -0700760
761struct rndis_function_config {
762 u8 ethaddr[ETH_ALEN];
763 u32 vendorID;
764 char manufacturer[256];
765 bool wceis;
766};
767
768static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
769{
770 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
771 if (!f->config)
772 return -ENOMEM;
773 return 0;
774}
775
776static void rndis_function_cleanup(struct android_usb_function *f)
777{
778 kfree(f->config);
779 f->config = NULL;
780}
781
782static int rndis_function_bind_config(struct android_usb_function *f,
783 struct usb_configuration *c)
784{
Manu Gautamf4741132011-11-25 09:08:53 +0530785 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700786 struct rndis_function_config *rndis = f->config;
787
788 if (!rndis) {
789 pr_err("%s: rndis_pdata\n", __func__);
790 return -1;
791 }
792
793 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
794 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
795 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
796
Manu Gautamf4741132011-11-25 09:08:53 +0530797 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
798 if (ret) {
799 pr_err("%s: gether_setup failed\n", __func__);
800 return ret;
801 }
802
Benoit Gobyaab96812011-04-19 20:37:33 -0700803 if (rndis->wceis) {
804 /* "Wireless" RNDIS; auto-detected by Windows */
805 rndis_iad_descriptor.bFunctionClass =
806 USB_CLASS_WIRELESS_CONTROLLER;
807 rndis_iad_descriptor.bFunctionSubClass = 0x01;
808 rndis_iad_descriptor.bFunctionProtocol = 0x03;
809 rndis_control_intf.bInterfaceClass =
810 USB_CLASS_WIRELESS_CONTROLLER;
811 rndis_control_intf.bInterfaceSubClass = 0x01;
812 rndis_control_intf.bInterfaceProtocol = 0x03;
813 }
814
815 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
816 rndis->manufacturer);
817}
818
819static void rndis_function_unbind_config(struct android_usb_function *f,
820 struct usb_configuration *c)
821{
Manu Gautamf4741132011-11-25 09:08:53 +0530822 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700823}
824
825static ssize_t rndis_manufacturer_show(struct device *dev,
826 struct device_attribute *attr, char *buf)
827{
828 struct android_usb_function *f = dev_get_drvdata(dev);
829 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530830 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700831}
832
833static ssize_t rndis_manufacturer_store(struct device *dev,
834 struct device_attribute *attr, const char *buf, size_t size)
835{
836 struct android_usb_function *f = dev_get_drvdata(dev);
837 struct rndis_function_config *config = f->config;
838
839 if (size >= sizeof(config->manufacturer))
840 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530841 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700842 return size;
843 return -1;
844}
845
846static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
847 rndis_manufacturer_store);
848
849static ssize_t rndis_wceis_show(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
852 struct android_usb_function *f = dev_get_drvdata(dev);
853 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530854 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700855}
856
857static ssize_t rndis_wceis_store(struct device *dev,
858 struct device_attribute *attr, const char *buf, size_t size)
859{
860 struct android_usb_function *f = dev_get_drvdata(dev);
861 struct rndis_function_config *config = f->config;
862 int value;
863
864 if (sscanf(buf, "%d", &value) == 1) {
865 config->wceis = value;
866 return size;
867 }
868 return -EINVAL;
869}
870
871static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
872 rndis_wceis_store);
873
874static ssize_t rndis_ethaddr_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
876{
877 struct android_usb_function *f = dev_get_drvdata(dev);
878 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530879 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700880 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
881 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
882}
883
884static ssize_t rndis_ethaddr_store(struct device *dev,
885 struct device_attribute *attr, const char *buf, size_t size)
886{
887 struct android_usb_function *f = dev_get_drvdata(dev);
888 struct rndis_function_config *rndis = f->config;
889
890 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
891 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
892 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
893 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
894 return size;
895 return -EINVAL;
896}
897
898static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
899 rndis_ethaddr_store);
900
901static ssize_t rndis_vendorID_show(struct device *dev,
902 struct device_attribute *attr, char *buf)
903{
904 struct android_usb_function *f = dev_get_drvdata(dev);
905 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530906 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700907}
908
909static ssize_t rndis_vendorID_store(struct device *dev,
910 struct device_attribute *attr, const char *buf, size_t size)
911{
912 struct android_usb_function *f = dev_get_drvdata(dev);
913 struct rndis_function_config *config = f->config;
914 int value;
915
916 if (sscanf(buf, "%04x", &value) == 1) {
917 config->vendorID = value;
918 return size;
919 }
920 return -EINVAL;
921}
922
923static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
924 rndis_vendorID_store);
925
926static struct device_attribute *rndis_function_attributes[] = {
927 &dev_attr_manufacturer,
928 &dev_attr_wceis,
929 &dev_attr_ethaddr,
930 &dev_attr_vendorID,
931 NULL
932};
933
934static struct android_usb_function rndis_function = {
935 .name = "rndis",
936 .init = rndis_function_init,
937 .cleanup = rndis_function_cleanup,
938 .bind_config = rndis_function_bind_config,
939 .unbind_config = rndis_function_unbind_config,
940 .attributes = rndis_function_attributes,
941};
942
943
944struct mass_storage_function_config {
945 struct fsg_config fsg;
946 struct fsg_common *common;
947};
948
949static int mass_storage_function_init(struct android_usb_function *f,
950 struct usb_composite_dev *cdev)
951{
952 struct mass_storage_function_config *config;
953 struct fsg_common *common;
954 int err;
955
956 config = kzalloc(sizeof(struct mass_storage_function_config),
957 GFP_KERNEL);
958 if (!config)
959 return -ENOMEM;
960
961 config->fsg.nluns = 1;
962 config->fsg.luns[0].removable = 1;
963
964 common = fsg_common_init(NULL, cdev, &config->fsg);
965 if (IS_ERR(common)) {
966 kfree(config);
967 return PTR_ERR(common);
968 }
969
970 err = sysfs_create_link(&f->dev->kobj,
971 &common->luns[0].dev.kobj,
972 "lun");
973 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530974 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700975 kfree(config);
976 return err;
977 }
978
979 config->common = common;
980 f->config = config;
981 return 0;
982}
983
984static void mass_storage_function_cleanup(struct android_usb_function *f)
985{
986 kfree(f->config);
987 f->config = NULL;
988}
989
990static int mass_storage_function_bind_config(struct android_usb_function *f,
991 struct usb_configuration *c)
992{
993 struct mass_storage_function_config *config = f->config;
994 return fsg_bind_config(c->cdev, c, config->common);
995}
996
997static ssize_t mass_storage_inquiry_show(struct device *dev,
998 struct device_attribute *attr, char *buf)
999{
1000 struct android_usb_function *f = dev_get_drvdata(dev);
1001 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301002 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -07001003}
1004
1005static ssize_t mass_storage_inquiry_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 mass_storage_function_config *config = f->config;
1010 if (size >= sizeof(config->common->inquiry_string))
1011 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301012 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -07001013 return -EINVAL;
1014 return size;
1015}
1016
1017static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1018 mass_storage_inquiry_show,
1019 mass_storage_inquiry_store);
1020
1021static struct device_attribute *mass_storage_function_attributes[] = {
1022 &dev_attr_inquiry_string,
1023 NULL
1024};
1025
1026static struct android_usb_function mass_storage_function = {
1027 .name = "mass_storage",
1028 .init = mass_storage_function_init,
1029 .cleanup = mass_storage_function_cleanup,
1030 .bind_config = mass_storage_function_bind_config,
1031 .attributes = mass_storage_function_attributes,
1032};
1033
1034
1035static int accessory_function_init(struct android_usb_function *f,
1036 struct usb_composite_dev *cdev)
1037{
1038 return acc_setup();
1039}
1040
1041static void accessory_function_cleanup(struct android_usb_function *f)
1042{
1043 acc_cleanup();
1044}
1045
1046static int accessory_function_bind_config(struct android_usb_function *f,
1047 struct usb_configuration *c)
1048{
1049 return acc_bind_config(c);
1050}
1051
1052static int accessory_function_ctrlrequest(struct android_usb_function *f,
1053 struct usb_composite_dev *cdev,
1054 const struct usb_ctrlrequest *c)
1055{
1056 return acc_ctrlrequest(cdev, c);
1057}
1058
1059static struct android_usb_function accessory_function = {
1060 .name = "accessory",
1061 .init = accessory_function_init,
1062 .cleanup = accessory_function_cleanup,
1063 .bind_config = accessory_function_bind_config,
1064 .ctrlrequest = accessory_function_ctrlrequest,
1065};
1066
1067
1068static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001069 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301070 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301071 &rmnet_sdio_function,
1072 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301073 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301075 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001076 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301077 &ccid_function,
Anji jonnala92be1b42011-12-19 09:44:41 +05301078 &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001079 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001080 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001081 &rndis_function,
1082 &mass_storage_function,
1083 &accessory_function,
1084 NULL
1085};
1086
Lena Salmand092f2d2012-03-12 17:27:24 +02001087static void android_cleanup_functions(struct android_usb_function **functions)
1088{
1089 struct android_usb_function *f;
1090 struct device_attribute **attrs;
1091 struct device_attribute *attr;
1092
1093 while (*functions) {
1094 f = *functions++;
1095
1096 if (f->dev) {
1097 device_destroy(android_class, f->dev->devt);
1098 kfree(f->dev_name);
1099 } else
1100 continue;
1101
1102 if (f->cleanup)
1103 f->cleanup(f);
1104
1105 attrs = f->attributes;
1106 if (attrs) {
1107 while ((attr = *attrs++))
1108 device_remove_file(f->dev, attr);
1109 }
1110 }
1111}
Benoit Gobyaab96812011-04-19 20:37:33 -07001112
1113static int android_init_functions(struct android_usb_function **functions,
1114 struct usb_composite_dev *cdev)
1115{
1116 struct android_dev *dev = _android_dev;
1117 struct android_usb_function *f;
1118 struct device_attribute **attrs;
1119 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301120 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001121 int index = 1; /* index 0 is for android0 device */
Benoit Gobyaab96812011-04-19 20:37:33 -07001122
1123 for (; (f = *functions++); index++) {
1124 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001125 if (!f->dev_name) {
1126 err = -ENOMEM;
1127 goto err_out;
1128 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001129 f->dev = device_create(android_class, dev->dev,
1130 MKDEV(0, index), f, f->dev_name);
1131 if (IS_ERR(f->dev)) {
1132 pr_err("%s: Failed to create dev %s", __func__,
1133 f->dev_name);
1134 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001135 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001136 goto err_create;
1137 }
1138
1139 if (f->init) {
1140 err = f->init(f, cdev);
1141 if (err) {
1142 pr_err("%s: Failed to init %s", __func__,
1143 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001144 goto err_init;
Benoit Gobyaab96812011-04-19 20:37:33 -07001145 }
1146 }
1147
1148 attrs = f->attributes;
1149 if (attrs) {
1150 while ((attr = *attrs++) && !err)
1151 err = device_create_file(f->dev, attr);
1152 }
1153 if (err) {
1154 pr_err("%s: Failed to create function %s attributes",
1155 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001156 goto err_attrs;
Benoit Gobyaab96812011-04-19 20:37:33 -07001157 }
1158 }
1159 return 0;
1160
Lena Salmand092f2d2012-03-12 17:27:24 +02001161err_attrs:
1162 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1163 device_remove_file(f->dev, attr);
1164 if (f->cleanup)
1165 f->cleanup(f);
1166err_init:
Benoit Gobyaab96812011-04-19 20:37:33 -07001167 device_destroy(android_class, f->dev->devt);
1168err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001169 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001170 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001171err_out:
1172 android_cleanup_functions(dev->functions);
Benoit Gobyaab96812011-04-19 20:37:33 -07001173 return err;
1174}
1175
Benoit Gobyaab96812011-04-19 20:37:33 -07001176static int
1177android_bind_enabled_functions(struct android_dev *dev,
1178 struct usb_configuration *c)
1179{
1180 struct android_usb_function *f;
1181 int ret;
1182
1183 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1184 ret = f->bind_config(f, c);
1185 if (ret) {
1186 pr_err("%s: %s failed", __func__, f->name);
1187 return ret;
1188 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301189 }
1190 return 0;
1191}
1192
Benoit Gobyaab96812011-04-19 20:37:33 -07001193static void
1194android_unbind_enabled_functions(struct android_dev *dev,
1195 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301196{
Benoit Gobyaab96812011-04-19 20:37:33 -07001197 struct android_usb_function *f;
1198
1199 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1200 if (f->unbind_config)
1201 f->unbind_config(f, c);
1202 }
1203}
1204
1205static int android_enable_function(struct android_dev *dev, char *name)
1206{
1207 struct android_usb_function **functions = dev->functions;
1208 struct android_usb_function *f;
1209 while ((f = *functions++)) {
1210 if (!strcmp(name, f->name)) {
1211 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301212 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001213 }
1214 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001215 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001216}
1217
Benoit Gobyaab96812011-04-19 20:37:33 -07001218/*-------------------------------------------------------------------------*/
1219/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301220
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301221static ssize_t remote_wakeup_show(struct device *pdev,
1222 struct device_attribute *attr, char *buf)
1223{
1224 return snprintf(buf, PAGE_SIZE, "%d\n",
1225 !!(android_config_driver.bmAttributes &
1226 USB_CONFIG_ATT_WAKEUP));
1227}
1228
1229static ssize_t remote_wakeup_store(struct device *pdev,
1230 struct device_attribute *attr, const char *buff, size_t size)
1231{
1232 int enable = 0;
1233
1234 sscanf(buff, "%d", &enable);
1235
1236 pr_debug("android_usb: %s remote wakeup\n",
1237 enable ? "enabling" : "disabling");
1238
1239 if (enable)
1240 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1241 else
1242 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1243
1244 return size;
1245}
1246
Benoit Gobyaab96812011-04-19 20:37:33 -07001247static ssize_t
1248functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1249{
1250 struct android_dev *dev = dev_get_drvdata(pdev);
1251 struct android_usb_function *f;
1252 char *buff = buf;
1253
Benoit Gobydc1b6342011-12-09 18:05:00 -08001254 mutex_lock(&dev->mutex);
1255
Benoit Gobyaab96812011-04-19 20:37:33 -07001256 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301257 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001258
1259 mutex_unlock(&dev->mutex);
1260
Benoit Gobyaab96812011-04-19 20:37:33 -07001261 if (buff != buf)
1262 *(buff-1) = '\n';
1263 return buff - buf;
1264}
1265
1266static ssize_t
1267functions_store(struct device *pdev, struct device_attribute *attr,
1268 const char *buff, size_t size)
1269{
1270 struct android_dev *dev = dev_get_drvdata(pdev);
1271 char *name;
1272 char buf[256], *b;
1273 int err;
1274
Benoit Gobydc1b6342011-12-09 18:05:00 -08001275 mutex_lock(&dev->mutex);
1276
1277 if (dev->enabled) {
1278 mutex_unlock(&dev->mutex);
1279 return -EBUSY;
1280 }
1281
Benoit Gobyaab96812011-04-19 20:37:33 -07001282 INIT_LIST_HEAD(&dev->enabled_functions);
1283
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301284 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001285 b = strim(buf);
1286
1287 while (b) {
1288 name = strsep(&b, ",");
1289 if (name) {
1290 err = android_enable_function(dev, name);
1291 if (err)
1292 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301293 }
1294 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001295
Benoit Gobydc1b6342011-12-09 18:05:00 -08001296 mutex_unlock(&dev->mutex);
1297
Benoit Gobyaab96812011-04-19 20:37:33 -07001298 return size;
1299}
1300
1301static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1302 char *buf)
1303{
1304 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301305 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001306}
1307
1308static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1309 const char *buff, size_t size)
1310{
1311 struct android_dev *dev = dev_get_drvdata(pdev);
1312 struct usb_composite_dev *cdev = dev->cdev;
1313 int enabled = 0;
1314
Benoit Gobydc1b6342011-12-09 18:05:00 -08001315 mutex_lock(&dev->mutex);
1316
Benoit Gobyaab96812011-04-19 20:37:33 -07001317 sscanf(buff, "%d", &enabled);
1318 if (enabled && !dev->enabled) {
1319 /* update values in composite driver's copy of device descriptor */
1320 cdev->desc.idVendor = device_desc.idVendor;
1321 cdev->desc.idProduct = device_desc.idProduct;
1322 cdev->desc.bcdDevice = device_desc.bcdDevice;
1323 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1324 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1325 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301326 if (usb_add_config(cdev, &android_config_driver,
1327 android_bind_config))
1328 return size;
1329
Benoit Gobyaab96812011-04-19 20:37:33 -07001330 usb_gadget_connect(cdev->gadget);
1331 dev->enabled = true;
1332 } else if (!enabled && dev->enabled) {
1333 usb_gadget_disconnect(cdev->gadget);
Benoit Gobye0de0a52011-11-29 13:49:27 -08001334 /* Cancel pending control requests */
1335 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Benoit Gobyaab96812011-04-19 20:37:33 -07001336 usb_remove_config(cdev, &android_config_driver);
1337 dev->enabled = false;
1338 } else {
1339 pr_err("android_usb: already %s\n",
1340 dev->enabled ? "enabled" : "disabled");
1341 }
Benoit Gobydc1b6342011-12-09 18:05:00 -08001342
1343 mutex_unlock(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001344 return size;
1345}
1346
Ofir Cohen94213a72012-05-03 14:26:32 +03001347static ssize_t pm_qos_show(struct device *pdev,
1348 struct device_attribute *attr, char *buf)
1349{
1350 struct android_dev *dev = dev_get_drvdata(pdev);
1351
1352 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1353}
1354
1355static ssize_t pm_qos_store(struct device *pdev,
1356 struct device_attribute *attr,
1357 const char *buff, size_t size)
1358{
1359 struct android_dev *dev = dev_get_drvdata(pdev);
1360
1361 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1362
1363 return size;
1364}
1365
Benoit Gobyaab96812011-04-19 20:37:33 -07001366static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1367 char *buf)
1368{
1369 struct android_dev *dev = dev_get_drvdata(pdev);
1370 struct usb_composite_dev *cdev = dev->cdev;
1371 char *state = "DISCONNECTED";
1372 unsigned long flags;
1373
1374 if (!cdev)
1375 goto out;
1376
1377 spin_lock_irqsave(&cdev->lock, flags);
1378 if (cdev->config)
1379 state = "CONFIGURED";
1380 else if (dev->connected)
1381 state = "CONNECTED";
1382 spin_unlock_irqrestore(&cdev->lock, flags);
1383out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301384 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001385}
1386
1387#define DESCRIPTOR_ATTR(field, format_string) \
1388static ssize_t \
1389field ## _show(struct device *dev, struct device_attribute *attr, \
1390 char *buf) \
1391{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301392 return snprintf(buf, PAGE_SIZE, \
1393 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001394} \
1395static ssize_t \
1396field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001397 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001398{ \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001399 int value; \
Benoit Gobyaab96812011-04-19 20:37:33 -07001400 if (sscanf(buf, format_string, &value) == 1) { \
1401 device_desc.field = value; \
1402 return size; \
1403 } \
1404 return -1; \
1405} \
1406static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1407
1408#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1409static ssize_t \
1410field ## _show(struct device *dev, struct device_attribute *attr, \
1411 char *buf) \
1412{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301413 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001414} \
1415static ssize_t \
1416field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001417 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001418{ \
1419 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301420 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001421 return size; \
1422 } \
1423 return -1; \
1424} \
1425static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1426
1427
1428DESCRIPTOR_ATTR(idVendor, "%04x\n")
1429DESCRIPTOR_ATTR(idProduct, "%04x\n")
1430DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1431DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1432DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1433DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1434DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1435DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1436DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1437
1438static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1439static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001440static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1441 pm_qos_show, pm_qos_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001442static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301443static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1444 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001445
1446static struct device_attribute *android_usb_attributes[] = {
1447 &dev_attr_idVendor,
1448 &dev_attr_idProduct,
1449 &dev_attr_bcdDevice,
1450 &dev_attr_bDeviceClass,
1451 &dev_attr_bDeviceSubClass,
1452 &dev_attr_bDeviceProtocol,
1453 &dev_attr_iManufacturer,
1454 &dev_attr_iProduct,
1455 &dev_attr_iSerial,
1456 &dev_attr_functions,
1457 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001458 &dev_attr_pm_qos,
Benoit Gobyaab96812011-04-19 20:37:33 -07001459 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301460 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001461 NULL
1462};
1463
1464/*-------------------------------------------------------------------------*/
1465/* Composite driver */
1466
1467static int android_bind_config(struct usb_configuration *c)
1468{
1469 struct android_dev *dev = _android_dev;
1470 int ret = 0;
1471
1472 ret = android_bind_enabled_functions(dev, c);
1473 if (ret)
1474 return ret;
1475
1476 return 0;
1477}
1478
1479static void android_unbind_config(struct usb_configuration *c)
1480{
1481 struct android_dev *dev = _android_dev;
1482
1483 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301484}
1485
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001486static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001487{
1488 struct android_dev *dev = _android_dev;
1489 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001490 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001491
Benoit Gobyaab96812011-04-19 20:37:33 -07001492 usb_gadget_disconnect(gadget);
1493
1494 ret = android_init_functions(dev->functions, cdev);
1495 if (ret)
1496 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001497
1498 /* Allocate string descriptor numbers ... note that string
1499 * contents can be overridden by the composite_dev glue.
1500 */
1501 id = usb_string_id(cdev);
1502 if (id < 0)
1503 return id;
1504 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1505 device_desc.iManufacturer = id;
1506
1507 id = usb_string_id(cdev);
1508 if (id < 0)
1509 return id;
1510 strings_dev[STRING_PRODUCT_IDX].id = id;
1511 device_desc.iProduct = id;
1512
Benoit Gobyaab96812011-04-19 20:37:33 -07001513 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301514 strlcpy(manufacturer_string, "Android",
1515 sizeof(manufacturer_string) - 1);
1516 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1517 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001518
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001519 id = usb_string_id(cdev);
1520 if (id < 0)
1521 return id;
1522 strings_dev[STRING_SERIAL_IDX].id = id;
1523 device_desc.iSerialNumber = id;
1524
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301525 if (gadget_is_otg(cdev->gadget))
1526 android_config_driver.descriptors = otg_desc;
1527
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001528 gcnum = usb_gadget_controller_number(gadget);
1529 if (gcnum >= 0)
1530 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1531 else {
1532 /* gadget zero is so simple (for now, no altsettings) that
1533 * it SHOULD NOT have problems with bulk-capable hardware.
1534 * so just warn about unrcognized controllers -- don't panic.
1535 *
1536 * things like configuration and altsetting numbering
1537 * can need hardware-specific attention though.
1538 */
1539 pr_warning("%s: controller '%s' not recognized\n",
1540 longname, gadget->name);
1541 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1542 }
1543
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001544 dev->cdev = cdev;
1545
1546 return 0;
1547}
1548
Benoit Gobyaab96812011-04-19 20:37:33 -07001549static int android_usb_unbind(struct usb_composite_dev *cdev)
1550{
1551 struct android_dev *dev = _android_dev;
1552
Lena Salmand092f2d2012-03-12 17:27:24 +02001553 manufacturer_string[0] = '\0';
1554 product_string[0] = '\0';
1555 serial_string[0] = '0';
Benoit Gobyaab96812011-04-19 20:37:33 -07001556 cancel_work_sync(&dev->work);
1557 android_cleanup_functions(dev->functions);
1558 return 0;
1559}
1560
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001561static struct usb_composite_driver android_usb_driver = {
1562 .name = "android_usb",
1563 .dev = &device_desc,
1564 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001565 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001566 .max_speed = USB_SPEED_SUPER
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001567};
1568
Benoit Gobyaab96812011-04-19 20:37:33 -07001569static int
1570android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1571{
1572 struct android_dev *dev = _android_dev;
1573 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1574 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001575 struct android_usb_function *f;
1576 int value = -EOPNOTSUPP;
1577 unsigned long flags;
1578
1579 req->zero = 0;
1580 req->complete = composite_setup_complete;
1581 req->length = 0;
1582 gadget->ep0->driver_data = cdev;
1583
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001584 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001585 if (f->ctrlrequest) {
1586 value = f->ctrlrequest(f, cdev, c);
1587 if (value >= 0)
1588 break;
1589 }
1590 }
1591
Mike Lockwood686d33a2011-09-07 09:55:12 -07001592 /* Special case the accessory function.
1593 * It needs to handle control requests before it is enabled.
1594 */
1595 if (value < 0)
1596 value = acc_ctrlrequest(cdev, c);
1597
Benoit Gobyaab96812011-04-19 20:37:33 -07001598 if (value < 0)
1599 value = composite_setup(gadget, c);
1600
1601 spin_lock_irqsave(&cdev->lock, flags);
1602 if (!dev->connected) {
1603 dev->connected = 1;
1604 schedule_work(&dev->work);
1605 }
1606 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1607 schedule_work(&dev->work);
1608 }
1609 spin_unlock_irqrestore(&cdev->lock, flags);
1610
1611 return value;
1612}
1613
1614static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001615{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301616 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001617 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1618 unsigned long flags;
1619
1620 composite_disconnect(gadget);
1621
1622 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001623 dev->connected = 0;
1624 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001625 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301626}
1627
Benoit Gobyaab96812011-04-19 20:37:33 -07001628static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001629{
Benoit Gobyaab96812011-04-19 20:37:33 -07001630 struct device_attribute **attrs = android_usb_attributes;
1631 struct device_attribute *attr;
1632 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001633
Benoit Gobyaab96812011-04-19 20:37:33 -07001634 dev->dev = device_create(android_class, NULL,
1635 MKDEV(0, 0), NULL, "android0");
1636 if (IS_ERR(dev->dev))
1637 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001638
Benoit Gobyaab96812011-04-19 20:37:33 -07001639 dev_set_drvdata(dev->dev, dev);
1640
1641 while ((attr = *attrs++)) {
1642 err = device_create_file(dev->dev, attr);
1643 if (err) {
1644 device_destroy(android_class, dev->dev->devt);
1645 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001646 }
1647 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001648 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001649}
1650
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301651static void android_destroy_device(struct android_dev *dev)
1652{
1653 struct device_attribute **attrs = android_usb_attributes;
1654 struct device_attribute *attr;
1655
1656 while ((attr = *attrs++))
1657 device_remove_file(dev->dev, attr);
1658 device_destroy(android_class, dev->dev->devt);
1659}
1660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661static int __devinit android_probe(struct platform_device *pdev)
1662{
1663 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1664 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001665 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666
1667 dev->pdata = pdata;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301668
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301669 android_class = class_create(THIS_MODULE, "android_usb");
1670 if (IS_ERR(android_class))
1671 return PTR_ERR(android_class);
1672
1673 ret = android_create_device(dev);
1674 if (ret) {
1675 pr_err("%s(): android_create_device failed\n", __func__);
1676 goto err_dev;
1677 }
1678
Lena Salmand092f2d2012-03-12 17:27:24 +02001679 ret = usb_composite_probe(&android_usb_driver, android_bind);
1680 if (ret) {
1681 pr_err("%s(): Failed to register android "
1682 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301683 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001684 }
1685
Ofir Cohen94213a72012-05-03 14:26:32 +03001686 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301687 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001688 pm_qos_add_request(&dev->pm_qos_req_dma,
1689 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1690 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1691
Lena Salmand092f2d2012-03-12 17:27:24 +02001692 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301693err_probe:
1694 android_destroy_device(dev);
1695err_dev:
1696 class_destroy(android_class);
1697 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001698}
1699
1700static int android_remove(struct platform_device *pdev)
1701{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301702 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001703 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301704
1705 android_destroy_device(dev);
1706 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001707 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301708 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001709 pm_qos_remove_request(&dev->pm_qos_req_dma);
1710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 return 0;
1712}
1713
1714static struct platform_driver android_platform_driver = {
1715 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001716 .probe = android_probe,
1717 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001719
1720static int __init init(void)
1721{
1722 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301723 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001724
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001725 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301726 if (!dev) {
1727 pr_err("%s(): Failed to alloc memory for android_dev\n",
1728 __func__);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001729 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301730 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001731 dev->functions = supported_functions;
1732 INIT_LIST_HEAD(&dev->enabled_functions);
1733 INIT_WORK(&dev->work, android_work);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001734 mutex_init(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001735
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001736 _android_dev = dev;
1737
Benoit Gobyaab96812011-04-19 20:37:33 -07001738 /* Override composite driver functions */
1739 composite_driver.setup = android_setup;
1740 composite_driver.disconnect = android_disconnect;
1741
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301742 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301743 if (ret) {
1744 pr_err("%s(): Failed to register android"
1745 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301746 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301747 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001748
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301749 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001750}
1751module_init(init);
1752
1753static void __exit cleanup(void)
1754{
Lena Salmand092f2d2012-03-12 17:27:24 +02001755 platform_driver_unregister(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001756 kfree(_android_dev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001757 _android_dev = NULL;
1758}
1759module_exit(cleanup);