blob: 15fc0c116676481ca74074f4577ee26074bca1ef [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
203 if (!pdata || vote == last_vote)
204 return;
205
206 swfi_latency = pdata->swfi_latency + 1;
207 if (vote)
208 pm_qos_update_request(&dev->pm_qos_req_dma,
209 swfi_latency);
210 else
211 pm_qos_update_request(&dev->pm_qos_req_dma,
212 PM_QOS_DEFAULT_VALUE);
213 last_vote = vote;
214}
215
Benoit Gobyaab96812011-04-19 20:37:33 -0700216static void android_work(struct work_struct *data)
217{
218 struct android_dev *dev = container_of(data, struct android_dev, work);
219 struct usb_composite_dev *cdev = dev->cdev;
220 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
221 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
222 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700223 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530224 static enum android_device_state last_uevent, next_state;
Benoit Gobyaab96812011-04-19 20:37:33 -0700225 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300226 int pm_qos_vote = -1;
Benoit Gobyaab96812011-04-19 20:37:33 -0700227
228 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530229 if (cdev->config) {
Dima Zavinfc753492011-09-14 11:52:45 -0700230 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530231 next_state = USB_CONFIGURED;
232 } else if (dev->connected != dev->sw_connected) {
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700233 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530234 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300235 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300236 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300237 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300238 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530239 }
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700240 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700241 spin_unlock_irqrestore(&cdev->lock, flags);
242
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300243 if (pm_qos_vote != -1)
244 android_pm_qos_update_latency(dev, pm_qos_vote);
245
Dima Zavinfc753492011-09-14 11:52:45 -0700246 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530247 /*
248 * Some userspace modules, e.g. MTP, work correctly only if
249 * CONFIGURED uevent is preceded by DISCONNECT uevent.
250 * Check if we missed sending out a DISCONNECT uevent. This can
251 * happen if host PC resets and configures device really quick.
252 */
253 if (((uevent_envp == connected) &&
254 (last_uevent != USB_DISCONNECTED)) ||
255 ((uevent_envp == configured) &&
256 (last_uevent == USB_CONFIGURED))) {
257 pr_info("%s: sent missed DISCONNECT event\n", __func__);
258 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
259 disconnected);
260 msleep(20);
261 }
262 /*
263 * Before sending out CONFIGURED uevent give function drivers
264 * a chance to wakeup userspace threads and notify disconnect
265 */
266 if (uevent_envp == configured)
267 msleep(50);
268
Dima Zavinfc753492011-09-14 11:52:45 -0700269 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530270 last_uevent = next_state;
Dima Zavinfc753492011-09-14 11:52:45 -0700271 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700272 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700273 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
274 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700275 }
276}
277
278
279/*-------------------------------------------------------------------------*/
280/* Supported functions initialization */
281
Manu Gautam8e0719b2011-09-26 14:47:55 +0530282/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530283static int rmnet_smd_function_bind_config(struct android_usb_function *f,
284 struct usb_configuration *c)
285{
286 return rmnet_smd_bind_config(c);
287}
288
289static struct android_usb_function rmnet_smd_function = {
290 .name = "rmnet_smd",
291 .bind_config = rmnet_smd_function_bind_config,
292};
293
Manu Gautam8e0719b2011-09-26 14:47:55 +0530294/* RMNET_SDIO */
295static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
296 struct usb_configuration *c)
297{
298 return rmnet_sdio_function_add(c);
299}
300
301static struct android_usb_function rmnet_sdio_function = {
302 .name = "rmnet_sdio",
303 .bind_config = rmnet_sdio_function_bind_config,
304};
305
306/* RMNET_SMD_SDIO */
307static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
308 struct usb_composite_dev *cdev)
309{
310 return rmnet_smd_sdio_init();
311}
312
313static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
314{
315 rmnet_smd_sdio_cleanup();
316}
317
318static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
319 struct usb_configuration *c)
320{
321 return rmnet_smd_sdio_function_add(c);
322}
323
324static struct device_attribute *rmnet_smd_sdio_attributes[] = {
325 &dev_attr_transport, NULL };
326
327static struct android_usb_function rmnet_smd_sdio_function = {
328 .name = "rmnet_smd_sdio",
329 .init = rmnet_smd_sdio_function_init,
330 .cleanup = rmnet_smd_sdio_function_cleanup,
331 .bind_config = rmnet_smd_sdio_bind_config,
332 .attributes = rmnet_smd_sdio_attributes,
333};
334
Hemant Kumar1b820d52011-11-03 15:08:28 -0700335/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
336#define MAX_XPORT_STR_LEN 50
337static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530338
Manu Gautame3e897c2011-09-12 17:18:46 +0530339static void rmnet_function_cleanup(struct android_usb_function *f)
340{
341 frmnet_cleanup();
342}
343
Manu Gautam2b0234a2011-09-07 16:47:52 +0530344static int rmnet_function_bind_config(struct android_usb_function *f,
345 struct usb_configuration *c)
346{
347 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700348 int err = 0;
349 char *ctrl_name;
350 char *data_name;
351 char buf[MAX_XPORT_STR_LEN], *b;
352 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530353
Hemant Kumar1b820d52011-11-03 15:08:28 -0700354 if (!rmnet_initialized) {
355 rmnet_initialized = 1;
356 strlcpy(buf, rmnet_transports, sizeof(buf));
357 b = strim(buf);
358 while (b) {
359 ctrl_name = strsep(&b, ",");
360 data_name = strsep(&b, ",");
361 if (ctrl_name && data_name) {
362 err = frmnet_init_port(ctrl_name, data_name);
363 if (err) {
364 pr_err("rmnet: Cannot open ctrl port:"
365 "'%s' data port:'%s'\n",
366 ctrl_name, data_name);
367 goto out;
368 }
369 ports++;
370 }
371 }
372
373 err = rmnet_gport_setup();
374 if (err) {
375 pr_err("rmnet: Cannot setup transports");
376 goto out;
377 }
378 }
379
380 for (i = 0; i < ports; i++) {
381 err = frmnet_bind_config(c, i);
382 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530383 pr_err("Could not bind rmnet%u config\n", i);
384 break;
385 }
386 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700387out:
388 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530389}
390
Hemant Kumar1b820d52011-11-03 15:08:28 -0700391static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530392 struct device_attribute *attr, char *buf)
393{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700394 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530395}
396
Hemant Kumar1b820d52011-11-03 15:08:28 -0700397static ssize_t rmnet_transports_store(
398 struct device *device, struct device_attribute *attr,
399 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530400{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700401 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530402
Manu Gautam2b0234a2011-09-07 16:47:52 +0530403 return size;
404}
405
Hemant Kumar1b820d52011-11-03 15:08:28 -0700406static struct device_attribute dev_attr_rmnet_transports =
407 __ATTR(transports, S_IRUGO | S_IWUSR,
408 rmnet_transports_show,
409 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530410static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700411 &dev_attr_rmnet_transports,
412 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530413
414static struct android_usb_function rmnet_function = {
415 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530416 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530417 .bind_config = rmnet_function_bind_config,
418 .attributes = rmnet_function_attributes,
419};
420
Anna Perela8c991d2012-04-09 16:44:46 +0300421
422/* MBIM - used with BAM */
423#define MAX_MBIM_INSTANCES 1
424
425static int mbim_function_init(struct android_usb_function *f,
426 struct usb_composite_dev *cdev)
427{
428 return mbim_init(MAX_MBIM_INSTANCES);
429}
430
431static void mbim_function_cleanup(struct android_usb_function *f)
432{
433 fmbim_cleanup();
434}
435
436static int mbim_function_bind_config(struct android_usb_function *f,
437 struct usb_configuration *c)
438{
439 return mbim_bind_config(c, 0);
440}
441
442static struct android_usb_function mbim_function = {
443 .name = "usb_mbim",
444 .cleanup = mbim_function_cleanup,
445 .bind_config = mbim_function_bind_config,
446 .init = mbim_function_init,
447};
448
449
Manu Gautam8e0719b2011-09-26 14:47:55 +0530450/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530451static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452static ssize_t clients_store(
453 struct device *device, struct device_attribute *attr,
454 const char *buff, size_t size)
455{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530456 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457
458 return size;
459}
460
461static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
462static struct device_attribute *diag_function_attributes[] =
463 { &dev_attr_clients, NULL };
464
465static int diag_function_init(struct android_usb_function *f,
466 struct usb_composite_dev *cdev)
467{
468 return diag_setup();
469}
470
471static void diag_function_cleanup(struct android_usb_function *f)
472{
473 diag_cleanup();
474}
475
476static int diag_function_bind_config(struct android_usb_function *f,
477 struct usb_configuration *c)
478{
479 char *name;
480 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530481 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800482 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530484 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 b = strim(buf);
486
487 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800488 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530490 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700491 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530492 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530495 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 if (err)
497 pr_err("diag: Cannot open channel '%s'", name);
498 }
499 }
500
501 return err;
502}
503
504static struct android_usb_function diag_function = {
505 .name = "diag",
506 .init = diag_function_init,
507 .cleanup = diag_function_cleanup,
508 .bind_config = diag_function_bind_config,
509 .attributes = diag_function_attributes,
510};
511
Manu Gautam8e0719b2011-09-26 14:47:55 +0530512/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530513static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530514static ssize_t serial_transports_store(
515 struct device *device, struct device_attribute *attr,
516 const char *buff, size_t size)
517{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530518 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530519
520 return size;
521}
522
523static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
524static struct device_attribute *serial_function_attributes[] =
525 { &dev_attr_transports, NULL };
526
527static void serial_function_cleanup(struct android_usb_function *f)
528{
529 gserial_cleanup();
530}
531
532static int serial_function_bind_config(struct android_usb_function *f,
533 struct usb_configuration *c)
534{
535 char *name;
536 char buf[32], *b;
537 int err = -1, i;
538 static int serial_initialized = 0, ports = 0;
539
540 if (serial_initialized)
541 goto bind_config;
542
543 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530544 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530545 b = strim(buf);
546
547 while (b) {
548 name = strsep(&b, ",");
549
550 if (name) {
551 err = gserial_init_port(ports, name);
552 if (err) {
553 pr_err("serial: Cannot open port '%s'", name);
554 goto out;
555 }
556 ports++;
557 }
558 }
559 err = gport_setup(c);
560 if (err) {
561 pr_err("serial: Cannot setup transports");
562 goto out;
563 }
564
565bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200566 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530567 err = gser_bind_config(c, i);
568 if (err) {
569 pr_err("serial: bind_config failed for port %d", i);
570 goto out;
571 }
572 }
573
574out:
575 return err;
576}
577
578static struct android_usb_function serial_function = {
579 .name = "serial",
580 .cleanup = serial_function_cleanup,
581 .bind_config = serial_function_bind_config,
582 .attributes = serial_function_attributes,
583};
584
Anji jonnala92be1b42011-12-19 09:44:41 +0530585/* ACM */
586static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
587static ssize_t acm_transports_store(
588 struct device *device, struct device_attribute *attr,
589 const char *buff, size_t size)
590{
591 strlcpy(acm_transports, buff, sizeof(acm_transports));
592
593 return size;
594}
595
596static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
597static struct device_attribute *acm_function_attributes[] = {
598 &dev_attr_acm_transports, NULL };
599
600static void acm_function_cleanup(struct android_usb_function *f)
601{
602 gserial_cleanup();
603}
604
605static int acm_function_bind_config(struct android_usb_function *f,
606 struct usb_configuration *c)
607{
608 char *name;
609 char buf[32], *b;
610 int err = -1, i;
611 static int acm_initialized, ports;
612
613 if (acm_initialized)
614 goto bind_config;
615
616 acm_initialized = 1;
617 strlcpy(buf, acm_transports, sizeof(buf));
618 b = strim(buf);
619
620 while (b) {
621 name = strsep(&b, ",");
622
623 if (name) {
624 err = acm_init_port(ports, name);
625 if (err) {
626 pr_err("acm: Cannot open port '%s'", name);
627 goto out;
628 }
629 ports++;
630 }
631 }
632 err = acm_port_setup(c);
633 if (err) {
634 pr_err("acm: Cannot setup transports");
635 goto out;
636 }
637
638bind_config:
639 for (i = 0; i < ports; i++) {
640 err = acm_bind_config(c, i);
641 if (err) {
642 pr_err("acm: bind_config failed for port %d", i);
643 goto out;
644 }
645 }
646
647out:
648 return err;
649}
650static struct android_usb_function acm_function = {
651 .name = "acm",
652 .cleanup = acm_function_cleanup,
653 .bind_config = acm_function_bind_config,
654 .attributes = acm_function_attributes,
655};
Manu Gautama4d993f2011-08-30 18:25:55 +0530656
Manu Gautam8e0719b2011-09-26 14:47:55 +0530657/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700658static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
659{
660 return adb_setup();
661}
662
663static void adb_function_cleanup(struct android_usb_function *f)
664{
665 adb_cleanup();
666}
667
668static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
669{
670 return adb_bind_config(c);
671}
672
673static struct android_usb_function adb_function = {
674 .name = "adb",
675 .init = adb_function_init,
676 .cleanup = adb_function_cleanup,
677 .bind_config = adb_function_bind_config,
678};
679
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530680/* CCID */
681static int ccid_function_init(struct android_usb_function *f,
682 struct usb_composite_dev *cdev)
683{
684 return ccid_setup();
685}
686
687static void ccid_function_cleanup(struct android_usb_function *f)
688{
689 ccid_cleanup();
690}
691
692static int ccid_function_bind_config(struct android_usb_function *f,
693 struct usb_configuration *c)
694{
695 return ccid_bind_config(c);
696}
697
698static struct android_usb_function ccid_function = {
699 .name = "ccid",
700 .init = ccid_function_init,
701 .cleanup = ccid_function_cleanup,
702 .bind_config = ccid_function_bind_config,
703};
704
Benoit Gobyaab96812011-04-19 20:37:33 -0700705static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
706{
707 return mtp_setup();
708}
709
710static void mtp_function_cleanup(struct android_usb_function *f)
711{
712 mtp_cleanup();
713}
714
715static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
716{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400717 return mtp_bind_config(c, false);
718}
719
720static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
721{
722 /* nothing to do - initialization is handled by mtp_function_init */
723 return 0;
724}
725
726static void ptp_function_cleanup(struct android_usb_function *f)
727{
728 /* nothing to do - cleanup is handled by mtp_function_cleanup */
729}
730
731static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
732{
733 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700734}
735
736static int mtp_function_ctrlrequest(struct android_usb_function *f,
737 struct usb_composite_dev *cdev,
738 const struct usb_ctrlrequest *c)
739{
740 return mtp_ctrlrequest(cdev, c);
741}
742
743static struct android_usb_function mtp_function = {
744 .name = "mtp",
745 .init = mtp_function_init,
746 .cleanup = mtp_function_cleanup,
747 .bind_config = mtp_function_bind_config,
748 .ctrlrequest = mtp_function_ctrlrequest,
749};
750
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400751/* PTP function is same as MTP with slightly different interface descriptor */
752static struct android_usb_function ptp_function = {
753 .name = "ptp",
754 .init = ptp_function_init,
755 .cleanup = ptp_function_cleanup,
756 .bind_config = ptp_function_bind_config,
757};
758
Benoit Gobyaab96812011-04-19 20:37:33 -0700759
760struct rndis_function_config {
761 u8 ethaddr[ETH_ALEN];
762 u32 vendorID;
763 char manufacturer[256];
764 bool wceis;
765};
766
767static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
768{
769 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
770 if (!f->config)
771 return -ENOMEM;
772 return 0;
773}
774
775static void rndis_function_cleanup(struct android_usb_function *f)
776{
777 kfree(f->config);
778 f->config = NULL;
779}
780
781static int rndis_function_bind_config(struct android_usb_function *f,
782 struct usb_configuration *c)
783{
Manu Gautamf4741132011-11-25 09:08:53 +0530784 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700785 struct rndis_function_config *rndis = f->config;
786
787 if (!rndis) {
788 pr_err("%s: rndis_pdata\n", __func__);
789 return -1;
790 }
791
792 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
793 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
794 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
795
Manu Gautamf4741132011-11-25 09:08:53 +0530796 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
797 if (ret) {
798 pr_err("%s: gether_setup failed\n", __func__);
799 return ret;
800 }
801
Benoit Gobyaab96812011-04-19 20:37:33 -0700802 if (rndis->wceis) {
803 /* "Wireless" RNDIS; auto-detected by Windows */
804 rndis_iad_descriptor.bFunctionClass =
805 USB_CLASS_WIRELESS_CONTROLLER;
806 rndis_iad_descriptor.bFunctionSubClass = 0x01;
807 rndis_iad_descriptor.bFunctionProtocol = 0x03;
808 rndis_control_intf.bInterfaceClass =
809 USB_CLASS_WIRELESS_CONTROLLER;
810 rndis_control_intf.bInterfaceSubClass = 0x01;
811 rndis_control_intf.bInterfaceProtocol = 0x03;
812 }
813
814 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
815 rndis->manufacturer);
816}
817
818static void rndis_function_unbind_config(struct android_usb_function *f,
819 struct usb_configuration *c)
820{
Manu Gautamf4741132011-11-25 09:08:53 +0530821 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700822}
823
824static ssize_t rndis_manufacturer_show(struct device *dev,
825 struct device_attribute *attr, char *buf)
826{
827 struct android_usb_function *f = dev_get_drvdata(dev);
828 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530829 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700830}
831
832static ssize_t rndis_manufacturer_store(struct device *dev,
833 struct device_attribute *attr, const char *buf, size_t size)
834{
835 struct android_usb_function *f = dev_get_drvdata(dev);
836 struct rndis_function_config *config = f->config;
837
838 if (size >= sizeof(config->manufacturer))
839 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530840 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700841 return size;
842 return -1;
843}
844
845static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
846 rndis_manufacturer_store);
847
848static ssize_t rndis_wceis_show(struct device *dev,
849 struct device_attribute *attr, char *buf)
850{
851 struct android_usb_function *f = dev_get_drvdata(dev);
852 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530853 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700854}
855
856static ssize_t rndis_wceis_store(struct device *dev,
857 struct device_attribute *attr, const char *buf, size_t size)
858{
859 struct android_usb_function *f = dev_get_drvdata(dev);
860 struct rndis_function_config *config = f->config;
861 int value;
862
863 if (sscanf(buf, "%d", &value) == 1) {
864 config->wceis = value;
865 return size;
866 }
867 return -EINVAL;
868}
869
870static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
871 rndis_wceis_store);
872
873static ssize_t rndis_ethaddr_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
875{
876 struct android_usb_function *f = dev_get_drvdata(dev);
877 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530878 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700879 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
880 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
881}
882
883static ssize_t rndis_ethaddr_store(struct device *dev,
884 struct device_attribute *attr, const char *buf, size_t size)
885{
886 struct android_usb_function *f = dev_get_drvdata(dev);
887 struct rndis_function_config *rndis = f->config;
888
889 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
890 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
891 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
892 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
893 return size;
894 return -EINVAL;
895}
896
897static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
898 rndis_ethaddr_store);
899
900static ssize_t rndis_vendorID_show(struct device *dev,
901 struct device_attribute *attr, char *buf)
902{
903 struct android_usb_function *f = dev_get_drvdata(dev);
904 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530905 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700906}
907
908static ssize_t rndis_vendorID_store(struct device *dev,
909 struct device_attribute *attr, const char *buf, size_t size)
910{
911 struct android_usb_function *f = dev_get_drvdata(dev);
912 struct rndis_function_config *config = f->config;
913 int value;
914
915 if (sscanf(buf, "%04x", &value) == 1) {
916 config->vendorID = value;
917 return size;
918 }
919 return -EINVAL;
920}
921
922static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
923 rndis_vendorID_store);
924
925static struct device_attribute *rndis_function_attributes[] = {
926 &dev_attr_manufacturer,
927 &dev_attr_wceis,
928 &dev_attr_ethaddr,
929 &dev_attr_vendorID,
930 NULL
931};
932
933static struct android_usb_function rndis_function = {
934 .name = "rndis",
935 .init = rndis_function_init,
936 .cleanup = rndis_function_cleanup,
937 .bind_config = rndis_function_bind_config,
938 .unbind_config = rndis_function_unbind_config,
939 .attributes = rndis_function_attributes,
940};
941
942
943struct mass_storage_function_config {
944 struct fsg_config fsg;
945 struct fsg_common *common;
946};
947
948static int mass_storage_function_init(struct android_usb_function *f,
949 struct usb_composite_dev *cdev)
950{
951 struct mass_storage_function_config *config;
952 struct fsg_common *common;
953 int err;
954
955 config = kzalloc(sizeof(struct mass_storage_function_config),
956 GFP_KERNEL);
957 if (!config)
958 return -ENOMEM;
959
960 config->fsg.nluns = 1;
961 config->fsg.luns[0].removable = 1;
962
963 common = fsg_common_init(NULL, cdev, &config->fsg);
964 if (IS_ERR(common)) {
965 kfree(config);
966 return PTR_ERR(common);
967 }
968
969 err = sysfs_create_link(&f->dev->kobj,
970 &common->luns[0].dev.kobj,
971 "lun");
972 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530973 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700974 kfree(config);
975 return err;
976 }
977
978 config->common = common;
979 f->config = config;
980 return 0;
981}
982
983static void mass_storage_function_cleanup(struct android_usb_function *f)
984{
985 kfree(f->config);
986 f->config = NULL;
987}
988
989static int mass_storage_function_bind_config(struct android_usb_function *f,
990 struct usb_configuration *c)
991{
992 struct mass_storage_function_config *config = f->config;
993 return fsg_bind_config(c->cdev, c, config->common);
994}
995
996static ssize_t mass_storage_inquiry_show(struct device *dev,
997 struct device_attribute *attr, char *buf)
998{
999 struct android_usb_function *f = dev_get_drvdata(dev);
1000 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301001 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -07001002}
1003
1004static ssize_t mass_storage_inquiry_store(struct device *dev,
1005 struct device_attribute *attr, const char *buf, size_t size)
1006{
1007 struct android_usb_function *f = dev_get_drvdata(dev);
1008 struct mass_storage_function_config *config = f->config;
1009 if (size >= sizeof(config->common->inquiry_string))
1010 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301011 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -07001012 return -EINVAL;
1013 return size;
1014}
1015
1016static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1017 mass_storage_inquiry_show,
1018 mass_storage_inquiry_store);
1019
1020static struct device_attribute *mass_storage_function_attributes[] = {
1021 &dev_attr_inquiry_string,
1022 NULL
1023};
1024
1025static struct android_usb_function mass_storage_function = {
1026 .name = "mass_storage",
1027 .init = mass_storage_function_init,
1028 .cleanup = mass_storage_function_cleanup,
1029 .bind_config = mass_storage_function_bind_config,
1030 .attributes = mass_storage_function_attributes,
1031};
1032
1033
1034static int accessory_function_init(struct android_usb_function *f,
1035 struct usb_composite_dev *cdev)
1036{
1037 return acc_setup();
1038}
1039
1040static void accessory_function_cleanup(struct android_usb_function *f)
1041{
1042 acc_cleanup();
1043}
1044
1045static int accessory_function_bind_config(struct android_usb_function *f,
1046 struct usb_configuration *c)
1047{
1048 return acc_bind_config(c);
1049}
1050
1051static int accessory_function_ctrlrequest(struct android_usb_function *f,
1052 struct usb_composite_dev *cdev,
1053 const struct usb_ctrlrequest *c)
1054{
1055 return acc_ctrlrequest(cdev, c);
1056}
1057
1058static struct android_usb_function accessory_function = {
1059 .name = "accessory",
1060 .init = accessory_function_init,
1061 .cleanup = accessory_function_cleanup,
1062 .bind_config = accessory_function_bind_config,
1063 .ctrlrequest = accessory_function_ctrlrequest,
1064};
1065
1066
1067static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001068 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301069 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301070 &rmnet_sdio_function,
1071 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301072 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301074 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001075 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301076 &ccid_function,
Anji jonnala92be1b42011-12-19 09:44:41 +05301077 &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001078 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001079 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001080 &rndis_function,
1081 &mass_storage_function,
1082 &accessory_function,
1083 NULL
1084};
1085
Lena Salmand092f2d2012-03-12 17:27:24 +02001086static void android_cleanup_functions(struct android_usb_function **functions)
1087{
1088 struct android_usb_function *f;
1089 struct device_attribute **attrs;
1090 struct device_attribute *attr;
1091
1092 while (*functions) {
1093 f = *functions++;
1094
1095 if (f->dev) {
1096 device_destroy(android_class, f->dev->devt);
1097 kfree(f->dev_name);
1098 } else
1099 continue;
1100
1101 if (f->cleanup)
1102 f->cleanup(f);
1103
1104 attrs = f->attributes;
1105 if (attrs) {
1106 while ((attr = *attrs++))
1107 device_remove_file(f->dev, attr);
1108 }
1109 }
1110}
Benoit Gobyaab96812011-04-19 20:37:33 -07001111
1112static int android_init_functions(struct android_usb_function **functions,
1113 struct usb_composite_dev *cdev)
1114{
1115 struct android_dev *dev = _android_dev;
1116 struct android_usb_function *f;
1117 struct device_attribute **attrs;
1118 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301119 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001120 int index = 1; /* index 0 is for android0 device */
Benoit Gobyaab96812011-04-19 20:37:33 -07001121
1122 for (; (f = *functions++); index++) {
1123 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001124 if (!f->dev_name) {
1125 err = -ENOMEM;
1126 goto err_out;
1127 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001128 f->dev = device_create(android_class, dev->dev,
1129 MKDEV(0, index), f, f->dev_name);
1130 if (IS_ERR(f->dev)) {
1131 pr_err("%s: Failed to create dev %s", __func__,
1132 f->dev_name);
1133 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001134 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001135 goto err_create;
1136 }
1137
1138 if (f->init) {
1139 err = f->init(f, cdev);
1140 if (err) {
1141 pr_err("%s: Failed to init %s", __func__,
1142 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001143 goto err_init;
Benoit Gobyaab96812011-04-19 20:37:33 -07001144 }
1145 }
1146
1147 attrs = f->attributes;
1148 if (attrs) {
1149 while ((attr = *attrs++) && !err)
1150 err = device_create_file(f->dev, attr);
1151 }
1152 if (err) {
1153 pr_err("%s: Failed to create function %s attributes",
1154 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001155 goto err_attrs;
Benoit Gobyaab96812011-04-19 20:37:33 -07001156 }
1157 }
1158 return 0;
1159
Lena Salmand092f2d2012-03-12 17:27:24 +02001160err_attrs:
1161 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1162 device_remove_file(f->dev, attr);
1163 if (f->cleanup)
1164 f->cleanup(f);
1165err_init:
Benoit Gobyaab96812011-04-19 20:37:33 -07001166 device_destroy(android_class, f->dev->devt);
1167err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001168 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001169 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001170err_out:
1171 android_cleanup_functions(dev->functions);
Benoit Gobyaab96812011-04-19 20:37:33 -07001172 return err;
1173}
1174
Benoit Gobyaab96812011-04-19 20:37:33 -07001175static int
1176android_bind_enabled_functions(struct android_dev *dev,
1177 struct usb_configuration *c)
1178{
1179 struct android_usb_function *f;
1180 int ret;
1181
1182 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1183 ret = f->bind_config(f, c);
1184 if (ret) {
1185 pr_err("%s: %s failed", __func__, f->name);
1186 return ret;
1187 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301188 }
1189 return 0;
1190}
1191
Benoit Gobyaab96812011-04-19 20:37:33 -07001192static void
1193android_unbind_enabled_functions(struct android_dev *dev,
1194 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301195{
Benoit Gobyaab96812011-04-19 20:37:33 -07001196 struct android_usb_function *f;
1197
1198 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1199 if (f->unbind_config)
1200 f->unbind_config(f, c);
1201 }
1202}
1203
1204static int android_enable_function(struct android_dev *dev, char *name)
1205{
1206 struct android_usb_function **functions = dev->functions;
1207 struct android_usb_function *f;
1208 while ((f = *functions++)) {
1209 if (!strcmp(name, f->name)) {
1210 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301211 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001212 }
1213 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001214 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001215}
1216
Benoit Gobyaab96812011-04-19 20:37:33 -07001217/*-------------------------------------------------------------------------*/
1218/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301219
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301220static ssize_t remote_wakeup_show(struct device *pdev,
1221 struct device_attribute *attr, char *buf)
1222{
1223 return snprintf(buf, PAGE_SIZE, "%d\n",
1224 !!(android_config_driver.bmAttributes &
1225 USB_CONFIG_ATT_WAKEUP));
1226}
1227
1228static ssize_t remote_wakeup_store(struct device *pdev,
1229 struct device_attribute *attr, const char *buff, size_t size)
1230{
1231 int enable = 0;
1232
1233 sscanf(buff, "%d", &enable);
1234
1235 pr_debug("android_usb: %s remote wakeup\n",
1236 enable ? "enabling" : "disabling");
1237
1238 if (enable)
1239 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1240 else
1241 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1242
1243 return size;
1244}
1245
Benoit Gobyaab96812011-04-19 20:37:33 -07001246static ssize_t
1247functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1248{
1249 struct android_dev *dev = dev_get_drvdata(pdev);
1250 struct android_usb_function *f;
1251 char *buff = buf;
1252
Benoit Gobydc1b6342011-12-09 18:05:00 -08001253 mutex_lock(&dev->mutex);
1254
Benoit Gobyaab96812011-04-19 20:37:33 -07001255 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301256 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001257
1258 mutex_unlock(&dev->mutex);
1259
Benoit Gobyaab96812011-04-19 20:37:33 -07001260 if (buff != buf)
1261 *(buff-1) = '\n';
1262 return buff - buf;
1263}
1264
1265static ssize_t
1266functions_store(struct device *pdev, struct device_attribute *attr,
1267 const char *buff, size_t size)
1268{
1269 struct android_dev *dev = dev_get_drvdata(pdev);
1270 char *name;
1271 char buf[256], *b;
1272 int err;
1273
Benoit Gobydc1b6342011-12-09 18:05:00 -08001274 mutex_lock(&dev->mutex);
1275
1276 if (dev->enabled) {
1277 mutex_unlock(&dev->mutex);
1278 return -EBUSY;
1279 }
1280
Benoit Gobyaab96812011-04-19 20:37:33 -07001281 INIT_LIST_HEAD(&dev->enabled_functions);
1282
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301283 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001284 b = strim(buf);
1285
1286 while (b) {
1287 name = strsep(&b, ",");
1288 if (name) {
1289 err = android_enable_function(dev, name);
1290 if (err)
1291 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301292 }
1293 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001294
Benoit Gobydc1b6342011-12-09 18:05:00 -08001295 mutex_unlock(&dev->mutex);
1296
Benoit Gobyaab96812011-04-19 20:37:33 -07001297 return size;
1298}
1299
1300static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1301 char *buf)
1302{
1303 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301304 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001305}
1306
1307static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1308 const char *buff, size_t size)
1309{
1310 struct android_dev *dev = dev_get_drvdata(pdev);
1311 struct usb_composite_dev *cdev = dev->cdev;
1312 int enabled = 0;
1313
Benoit Gobydc1b6342011-12-09 18:05:00 -08001314 mutex_lock(&dev->mutex);
1315
Benoit Gobyaab96812011-04-19 20:37:33 -07001316 sscanf(buff, "%d", &enabled);
1317 if (enabled && !dev->enabled) {
1318 /* update values in composite driver's copy of device descriptor */
1319 cdev->desc.idVendor = device_desc.idVendor;
1320 cdev->desc.idProduct = device_desc.idProduct;
1321 cdev->desc.bcdDevice = device_desc.bcdDevice;
1322 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1323 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1324 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301325 if (usb_add_config(cdev, &android_config_driver,
1326 android_bind_config))
1327 return size;
1328
Benoit Gobyaab96812011-04-19 20:37:33 -07001329 usb_gadget_connect(cdev->gadget);
1330 dev->enabled = true;
1331 } else if (!enabled && dev->enabled) {
1332 usb_gadget_disconnect(cdev->gadget);
Benoit Gobye0de0a52011-11-29 13:49:27 -08001333 /* Cancel pending control requests */
1334 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Benoit Gobyaab96812011-04-19 20:37:33 -07001335 usb_remove_config(cdev, &android_config_driver);
1336 dev->enabled = false;
1337 } else {
1338 pr_err("android_usb: already %s\n",
1339 dev->enabled ? "enabled" : "disabled");
1340 }
Benoit Gobydc1b6342011-12-09 18:05:00 -08001341
1342 mutex_unlock(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001343 return size;
1344}
1345
Ofir Cohen94213a72012-05-03 14:26:32 +03001346static ssize_t pm_qos_show(struct device *pdev,
1347 struct device_attribute *attr, char *buf)
1348{
1349 struct android_dev *dev = dev_get_drvdata(pdev);
1350
1351 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1352}
1353
1354static ssize_t pm_qos_store(struct device *pdev,
1355 struct device_attribute *attr,
1356 const char *buff, size_t size)
1357{
1358 struct android_dev *dev = dev_get_drvdata(pdev);
1359
1360 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1361
1362 return size;
1363}
1364
Benoit Gobyaab96812011-04-19 20:37:33 -07001365static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1366 char *buf)
1367{
1368 struct android_dev *dev = dev_get_drvdata(pdev);
1369 struct usb_composite_dev *cdev = dev->cdev;
1370 char *state = "DISCONNECTED";
1371 unsigned long flags;
1372
1373 if (!cdev)
1374 goto out;
1375
1376 spin_lock_irqsave(&cdev->lock, flags);
1377 if (cdev->config)
1378 state = "CONFIGURED";
1379 else if (dev->connected)
1380 state = "CONNECTED";
1381 spin_unlock_irqrestore(&cdev->lock, flags);
1382out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301383 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001384}
1385
1386#define DESCRIPTOR_ATTR(field, format_string) \
1387static ssize_t \
1388field ## _show(struct device *dev, struct device_attribute *attr, \
1389 char *buf) \
1390{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301391 return snprintf(buf, PAGE_SIZE, \
1392 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001393} \
1394static ssize_t \
1395field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001396 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001397{ \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001398 int value; \
Benoit Gobyaab96812011-04-19 20:37:33 -07001399 if (sscanf(buf, format_string, &value) == 1) { \
1400 device_desc.field = value; \
1401 return size; \
1402 } \
1403 return -1; \
1404} \
1405static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1406
1407#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1408static ssize_t \
1409field ## _show(struct device *dev, struct device_attribute *attr, \
1410 char *buf) \
1411{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301412 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001413} \
1414static ssize_t \
1415field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001416 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001417{ \
1418 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301419 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001420 return size; \
1421 } \
1422 return -1; \
1423} \
1424static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1425
1426
1427DESCRIPTOR_ATTR(idVendor, "%04x\n")
1428DESCRIPTOR_ATTR(idProduct, "%04x\n")
1429DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1430DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1431DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1432DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1433DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1434DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1435DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1436
1437static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1438static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001439static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1440 pm_qos_show, pm_qos_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001441static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301442static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1443 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001444
1445static struct device_attribute *android_usb_attributes[] = {
1446 &dev_attr_idVendor,
1447 &dev_attr_idProduct,
1448 &dev_attr_bcdDevice,
1449 &dev_attr_bDeviceClass,
1450 &dev_attr_bDeviceSubClass,
1451 &dev_attr_bDeviceProtocol,
1452 &dev_attr_iManufacturer,
1453 &dev_attr_iProduct,
1454 &dev_attr_iSerial,
1455 &dev_attr_functions,
1456 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001457 &dev_attr_pm_qos,
Benoit Gobyaab96812011-04-19 20:37:33 -07001458 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301459 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001460 NULL
1461};
1462
1463/*-------------------------------------------------------------------------*/
1464/* Composite driver */
1465
1466static int android_bind_config(struct usb_configuration *c)
1467{
1468 struct android_dev *dev = _android_dev;
1469 int ret = 0;
1470
1471 ret = android_bind_enabled_functions(dev, c);
1472 if (ret)
1473 return ret;
1474
1475 return 0;
1476}
1477
1478static void android_unbind_config(struct usb_configuration *c)
1479{
1480 struct android_dev *dev = _android_dev;
1481
1482 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301483}
1484
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001485static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001486{
1487 struct android_dev *dev = _android_dev;
1488 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001489 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001490
Benoit Gobyaab96812011-04-19 20:37:33 -07001491 usb_gadget_disconnect(gadget);
1492
1493 ret = android_init_functions(dev->functions, cdev);
1494 if (ret)
1495 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001496
1497 /* Allocate string descriptor numbers ... note that string
1498 * contents can be overridden by the composite_dev glue.
1499 */
1500 id = usb_string_id(cdev);
1501 if (id < 0)
1502 return id;
1503 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1504 device_desc.iManufacturer = id;
1505
1506 id = usb_string_id(cdev);
1507 if (id < 0)
1508 return id;
1509 strings_dev[STRING_PRODUCT_IDX].id = id;
1510 device_desc.iProduct = id;
1511
Benoit Gobyaab96812011-04-19 20:37:33 -07001512 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301513 strlcpy(manufacturer_string, "Android",
1514 sizeof(manufacturer_string) - 1);
1515 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1516 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001517
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001518 id = usb_string_id(cdev);
1519 if (id < 0)
1520 return id;
1521 strings_dev[STRING_SERIAL_IDX].id = id;
1522 device_desc.iSerialNumber = id;
1523
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301524 if (gadget_is_otg(cdev->gadget))
1525 android_config_driver.descriptors = otg_desc;
1526
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001527 gcnum = usb_gadget_controller_number(gadget);
1528 if (gcnum >= 0)
1529 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1530 else {
1531 /* gadget zero is so simple (for now, no altsettings) that
1532 * it SHOULD NOT have problems with bulk-capable hardware.
1533 * so just warn about unrcognized controllers -- don't panic.
1534 *
1535 * things like configuration and altsetting numbering
1536 * can need hardware-specific attention though.
1537 */
1538 pr_warning("%s: controller '%s' not recognized\n",
1539 longname, gadget->name);
1540 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1541 }
1542
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001543 dev->cdev = cdev;
1544
1545 return 0;
1546}
1547
Benoit Gobyaab96812011-04-19 20:37:33 -07001548static int android_usb_unbind(struct usb_composite_dev *cdev)
1549{
1550 struct android_dev *dev = _android_dev;
1551
Lena Salmand092f2d2012-03-12 17:27:24 +02001552 manufacturer_string[0] = '\0';
1553 product_string[0] = '\0';
1554 serial_string[0] = '0';
Benoit Gobyaab96812011-04-19 20:37:33 -07001555 cancel_work_sync(&dev->work);
1556 android_cleanup_functions(dev->functions);
1557 return 0;
1558}
1559
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001560static struct usb_composite_driver android_usb_driver = {
1561 .name = "android_usb",
1562 .dev = &device_desc,
1563 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001564 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001565 .max_speed = USB_SPEED_SUPER
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001566};
1567
Benoit Gobyaab96812011-04-19 20:37:33 -07001568static int
1569android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1570{
1571 struct android_dev *dev = _android_dev;
1572 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1573 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001574 struct android_usb_function *f;
1575 int value = -EOPNOTSUPP;
1576 unsigned long flags;
1577
1578 req->zero = 0;
1579 req->complete = composite_setup_complete;
1580 req->length = 0;
1581 gadget->ep0->driver_data = cdev;
1582
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001583 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001584 if (f->ctrlrequest) {
1585 value = f->ctrlrequest(f, cdev, c);
1586 if (value >= 0)
1587 break;
1588 }
1589 }
1590
Mike Lockwood686d33a2011-09-07 09:55:12 -07001591 /* Special case the accessory function.
1592 * It needs to handle control requests before it is enabled.
1593 */
1594 if (value < 0)
1595 value = acc_ctrlrequest(cdev, c);
1596
Benoit Gobyaab96812011-04-19 20:37:33 -07001597 if (value < 0)
1598 value = composite_setup(gadget, c);
1599
1600 spin_lock_irqsave(&cdev->lock, flags);
1601 if (!dev->connected) {
1602 dev->connected = 1;
1603 schedule_work(&dev->work);
1604 }
1605 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1606 schedule_work(&dev->work);
1607 }
1608 spin_unlock_irqrestore(&cdev->lock, flags);
1609
1610 return value;
1611}
1612
1613static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001614{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301615 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001616 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1617 unsigned long flags;
1618
1619 composite_disconnect(gadget);
1620
1621 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001622 dev->connected = 0;
1623 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001624 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301625}
1626
Benoit Gobyaab96812011-04-19 20:37:33 -07001627static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001628{
Benoit Gobyaab96812011-04-19 20:37:33 -07001629 struct device_attribute **attrs = android_usb_attributes;
1630 struct device_attribute *attr;
1631 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001632
Benoit Gobyaab96812011-04-19 20:37:33 -07001633 dev->dev = device_create(android_class, NULL,
1634 MKDEV(0, 0), NULL, "android0");
1635 if (IS_ERR(dev->dev))
1636 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001637
Benoit Gobyaab96812011-04-19 20:37:33 -07001638 dev_set_drvdata(dev->dev, dev);
1639
1640 while ((attr = *attrs++)) {
1641 err = device_create_file(dev->dev, attr);
1642 if (err) {
1643 device_destroy(android_class, dev->dev->devt);
1644 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001645 }
1646 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001647 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001648}
1649
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301650static void android_destroy_device(struct android_dev *dev)
1651{
1652 struct device_attribute **attrs = android_usb_attributes;
1653 struct device_attribute *attr;
1654
1655 while ((attr = *attrs++))
1656 device_remove_file(dev->dev, attr);
1657 device_destroy(android_class, dev->dev->devt);
1658}
1659
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660static int __devinit android_probe(struct platform_device *pdev)
1661{
1662 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1663 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001664 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665
1666 dev->pdata = pdata;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301667
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301668 android_class = class_create(THIS_MODULE, "android_usb");
1669 if (IS_ERR(android_class))
1670 return PTR_ERR(android_class);
1671
1672 ret = android_create_device(dev);
1673 if (ret) {
1674 pr_err("%s(): android_create_device failed\n", __func__);
1675 goto err_dev;
1676 }
1677
Lena Salmand092f2d2012-03-12 17:27:24 +02001678 ret = usb_composite_probe(&android_usb_driver, android_bind);
1679 if (ret) {
1680 pr_err("%s(): Failed to register android "
1681 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301682 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001683 }
1684
Ofir Cohen94213a72012-05-03 14:26:32 +03001685 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301686 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001687 pm_qos_add_request(&dev->pm_qos_req_dma,
1688 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1689 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1690
Lena Salmand092f2d2012-03-12 17:27:24 +02001691 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301692err_probe:
1693 android_destroy_device(dev);
1694err_dev:
1695 class_destroy(android_class);
1696 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001697}
1698
1699static int android_remove(struct platform_device *pdev)
1700{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301701 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001702 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301703
1704 android_destroy_device(dev);
1705 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001706 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301707 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001708 pm_qos_remove_request(&dev->pm_qos_req_dma);
1709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710 return 0;
1711}
1712
1713static struct platform_driver android_platform_driver = {
1714 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001715 .probe = android_probe,
1716 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001718
1719static int __init init(void)
1720{
1721 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301722 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001723
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001724 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301725 if (!dev) {
1726 pr_err("%s(): Failed to alloc memory for android_dev\n",
1727 __func__);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001728 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301729 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001730 dev->functions = supported_functions;
1731 INIT_LIST_HEAD(&dev->enabled_functions);
1732 INIT_WORK(&dev->work, android_work);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001733 mutex_init(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001734
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001735 _android_dev = dev;
1736
Benoit Gobyaab96812011-04-19 20:37:33 -07001737 /* Override composite driver functions */
1738 composite_driver.setup = android_setup;
1739 composite_driver.disconnect = android_disconnect;
1740
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301741 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301742 if (ret) {
1743 pr_err("%s(): Failed to register android"
1744 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301745 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301746 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001747
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301748 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001749}
1750module_init(init);
1751
1752static void __exit cleanup(void)
1753{
Lena Salmand092f2d2012-03-12 17:27:24 +02001754 platform_driver_unregister(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001755 kfree(_android_dev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001756 _android_dev = NULL;
1757}
1758module_exit(cleanup);