blob: 92e95a6e900706ef6443f9df97eaaff85fb748d8 [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"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053064#include "u_ctrl_hsuart.c"
65#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053066#include "f_serial.c"
Anji jonnala92be1b42011-12-19 09:44:41 +053067#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070068#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053069#include "f_ccid.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070070#include "f_mtp.c"
71#include "f_accessory.c"
72#define USB_ETH_RNDIS y
73#include "f_rndis.c"
74#include "rndis.c"
75#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030076#include "u_bam_data.c"
77#include "f_mbim.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070078
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050079MODULE_AUTHOR("Mike Lockwood");
80MODULE_DESCRIPTION("Android Composite USB Driver");
81MODULE_LICENSE("GPL");
82MODULE_VERSION("1.0");
83
84static const char longname[] = "Gadget Android";
85
Benoit Gobyaab96812011-04-19 20:37:33 -070086/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050087#define VENDOR_ID 0x18D1
88#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050089
Benoit Gobyaab96812011-04-19 20:37:33 -070090struct android_usb_function {
91 char *name;
92 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050093
Benoit Gobyaab96812011-04-19 20:37:33 -070094 struct device *dev;
95 char *dev_name;
96 struct device_attribute **attributes;
97
98 /* for android_dev.enabled_functions */
99 struct list_head enabled_list;
100
101 /* Optional: initialization during gadget bind */
102 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
103 /* Optional: cleanup during gadget unbind */
104 void (*cleanup)(struct android_usb_function *);
105
106 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
107
108 /* Optional: called when the configuration is removed */
109 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700110 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700111 int (*ctrlrequest)(struct android_usb_function *,
112 struct usb_composite_dev *,
113 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500114};
115
Benoit Gobyaab96812011-04-19 20:37:33 -0700116struct android_dev {
117 struct android_usb_function **functions;
118 struct list_head enabled_functions;
119 struct usb_composite_dev *cdev;
120 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700122
123 bool enabled;
Benoit Gobydc1b6342011-12-09 18:05:00 -0800124 struct mutex mutex;
Benoit Gobyaab96812011-04-19 20:37:33 -0700125 bool connected;
126 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300127 char pm_qos[5];
128 struct pm_qos_request_list pm_qos_req_dma;
Benoit Gobyaab96812011-04-19 20:37:33 -0700129 struct work_struct work;
130};
131
132static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500133static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700134static int android_bind_config(struct usb_configuration *c);
135static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500136
137/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500138#define STRING_MANUFACTURER_IDX 0
139#define STRING_PRODUCT_IDX 1
140#define STRING_SERIAL_IDX 2
141
Benoit Gobyaab96812011-04-19 20:37:33 -0700142static char manufacturer_string[256];
143static char product_string[256];
144static char serial_string[256];
145
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500146/* String Table */
147static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700148 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
149 [STRING_PRODUCT_IDX].s = product_string,
150 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500151 { } /* end of list */
152};
153
154static struct usb_gadget_strings stringtab_dev = {
155 .language = 0x0409, /* en-us */
156 .strings = strings_dev,
157};
158
159static struct usb_gadget_strings *dev_strings[] = {
160 &stringtab_dev,
161 NULL,
162};
163
164static struct usb_device_descriptor device_desc = {
165 .bLength = sizeof(device_desc),
166 .bDescriptorType = USB_DT_DEVICE,
167 .bcdUSB = __constant_cpu_to_le16(0x0200),
168 .bDeviceClass = USB_CLASS_PER_INTERFACE,
169 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
170 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
171 .bcdDevice = __constant_cpu_to_le16(0xffff),
172 .bNumConfigurations = 1,
173};
174
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530175static struct usb_otg_descriptor otg_descriptor = {
176 .bLength = sizeof otg_descriptor,
177 .bDescriptorType = USB_DT_OTG,
178 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
179 .bcdOTG = __constant_cpu_to_le16(0x0200),
180};
181
182static const struct usb_descriptor_header *otg_desc[] = {
183 (struct usb_descriptor_header *) &otg_descriptor,
184 NULL,
185};
186
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500187static struct usb_configuration android_config_driver = {
188 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700189 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500190 .bConfigurationValue = 1,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500191};
192
Manu Gautama2b54142012-04-03 14:34:32 +0530193enum android_device_state {
194 USB_DISCONNECTED,
195 USB_CONNECTED,
196 USB_CONFIGURED,
197};
198
Ofir Cohen94213a72012-05-03 14:26:32 +0300199static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
200{
201 struct android_usb_platform_data *pdata = dev->pdata;
202 u32 swfi_latency = 0;
203 static int last_vote = -1;
204
Ofir Cohen56eb7072012-05-20 11:41:39 +0300205 if (!pdata || vote == last_vote
206 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300207 return;
208
209 swfi_latency = pdata->swfi_latency + 1;
210 if (vote)
211 pm_qos_update_request(&dev->pm_qos_req_dma,
212 swfi_latency);
213 else
214 pm_qos_update_request(&dev->pm_qos_req_dma,
215 PM_QOS_DEFAULT_VALUE);
216 last_vote = vote;
217}
218
Benoit Gobyaab96812011-04-19 20:37:33 -0700219static void android_work(struct work_struct *data)
220{
221 struct android_dev *dev = container_of(data, struct android_dev, work);
222 struct usb_composite_dev *cdev = dev->cdev;
223 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
224 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
225 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700226 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530227 static enum android_device_state last_uevent, next_state;
Benoit Gobyaab96812011-04-19 20:37:33 -0700228 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300229 int pm_qos_vote = -1;
Benoit Gobyaab96812011-04-19 20:37:33 -0700230
231 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530232 if (cdev->config) {
Dima Zavinfc753492011-09-14 11:52:45 -0700233 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530234 next_state = USB_CONFIGURED;
235 } else if (dev->connected != dev->sw_connected) {
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700236 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530237 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300238 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300239 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300240 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300241 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530242 }
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700243 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700244 spin_unlock_irqrestore(&cdev->lock, flags);
245
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300246 if (pm_qos_vote != -1)
247 android_pm_qos_update_latency(dev, pm_qos_vote);
248
Dima Zavinfc753492011-09-14 11:52:45 -0700249 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530250 /*
251 * Some userspace modules, e.g. MTP, work correctly only if
252 * CONFIGURED uevent is preceded by DISCONNECT uevent.
253 * Check if we missed sending out a DISCONNECT uevent. This can
254 * happen if host PC resets and configures device really quick.
255 */
256 if (((uevent_envp == connected) &&
257 (last_uevent != USB_DISCONNECTED)) ||
258 ((uevent_envp == configured) &&
259 (last_uevent == USB_CONFIGURED))) {
260 pr_info("%s: sent missed DISCONNECT event\n", __func__);
261 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
262 disconnected);
263 msleep(20);
264 }
265 /*
266 * Before sending out CONFIGURED uevent give function drivers
267 * a chance to wakeup userspace threads and notify disconnect
268 */
269 if (uevent_envp == configured)
270 msleep(50);
271
Dima Zavinfc753492011-09-14 11:52:45 -0700272 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530273 last_uevent = next_state;
Dima Zavinfc753492011-09-14 11:52:45 -0700274 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700275 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700276 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
277 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700278 }
279}
280
281
282/*-------------------------------------------------------------------------*/
283/* Supported functions initialization */
284
Manu Gautam8e0719b2011-09-26 14:47:55 +0530285/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530286static int rmnet_smd_function_bind_config(struct android_usb_function *f,
287 struct usb_configuration *c)
288{
289 return rmnet_smd_bind_config(c);
290}
291
292static struct android_usb_function rmnet_smd_function = {
293 .name = "rmnet_smd",
294 .bind_config = rmnet_smd_function_bind_config,
295};
296
Manu Gautam8e0719b2011-09-26 14:47:55 +0530297/* RMNET_SDIO */
298static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
299 struct usb_configuration *c)
300{
301 return rmnet_sdio_function_add(c);
302}
303
304static struct android_usb_function rmnet_sdio_function = {
305 .name = "rmnet_sdio",
306 .bind_config = rmnet_sdio_function_bind_config,
307};
308
309/* RMNET_SMD_SDIO */
310static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
311 struct usb_composite_dev *cdev)
312{
313 return rmnet_smd_sdio_init();
314}
315
316static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
317{
318 rmnet_smd_sdio_cleanup();
319}
320
321static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
322 struct usb_configuration *c)
323{
324 return rmnet_smd_sdio_function_add(c);
325}
326
327static struct device_attribute *rmnet_smd_sdio_attributes[] = {
328 &dev_attr_transport, NULL };
329
330static struct android_usb_function rmnet_smd_sdio_function = {
331 .name = "rmnet_smd_sdio",
332 .init = rmnet_smd_sdio_function_init,
333 .cleanup = rmnet_smd_sdio_function_cleanup,
334 .bind_config = rmnet_smd_sdio_bind_config,
335 .attributes = rmnet_smd_sdio_attributes,
336};
337
Hemant Kumar1b820d52011-11-03 15:08:28 -0700338/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
339#define MAX_XPORT_STR_LEN 50
340static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530341
Manu Gautame3e897c2011-09-12 17:18:46 +0530342static void rmnet_function_cleanup(struct android_usb_function *f)
343{
344 frmnet_cleanup();
345}
346
Manu Gautam2b0234a2011-09-07 16:47:52 +0530347static int rmnet_function_bind_config(struct android_usb_function *f,
348 struct usb_configuration *c)
349{
350 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700351 int err = 0;
352 char *ctrl_name;
353 char *data_name;
354 char buf[MAX_XPORT_STR_LEN], *b;
355 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530356
Hemant Kumar1b820d52011-11-03 15:08:28 -0700357 if (!rmnet_initialized) {
358 rmnet_initialized = 1;
359 strlcpy(buf, rmnet_transports, sizeof(buf));
360 b = strim(buf);
361 while (b) {
362 ctrl_name = strsep(&b, ",");
363 data_name = strsep(&b, ",");
364 if (ctrl_name && data_name) {
365 err = frmnet_init_port(ctrl_name, data_name);
366 if (err) {
367 pr_err("rmnet: Cannot open ctrl port:"
368 "'%s' data port:'%s'\n",
369 ctrl_name, data_name);
370 goto out;
371 }
372 ports++;
373 }
374 }
375
376 err = rmnet_gport_setup();
377 if (err) {
378 pr_err("rmnet: Cannot setup transports");
379 goto out;
380 }
381 }
382
383 for (i = 0; i < ports; i++) {
384 err = frmnet_bind_config(c, i);
385 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530386 pr_err("Could not bind rmnet%u config\n", i);
387 break;
388 }
389 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700390out:
391 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530392}
393
Hemant Kumar1b820d52011-11-03 15:08:28 -0700394static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530395 struct device_attribute *attr, char *buf)
396{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700397 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530398}
399
Hemant Kumar1b820d52011-11-03 15:08:28 -0700400static ssize_t rmnet_transports_store(
401 struct device *device, struct device_attribute *attr,
402 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530403{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700404 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530405
Manu Gautam2b0234a2011-09-07 16:47:52 +0530406 return size;
407}
408
Hemant Kumar1b820d52011-11-03 15:08:28 -0700409static struct device_attribute dev_attr_rmnet_transports =
410 __ATTR(transports, S_IRUGO | S_IWUSR,
411 rmnet_transports_show,
412 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530413static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700414 &dev_attr_rmnet_transports,
415 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530416
417static struct android_usb_function rmnet_function = {
418 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530419 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530420 .bind_config = rmnet_function_bind_config,
421 .attributes = rmnet_function_attributes,
422};
423
Anna Perela8c991d2012-04-09 16:44:46 +0300424
425/* MBIM - used with BAM */
426#define MAX_MBIM_INSTANCES 1
427
428static int mbim_function_init(struct android_usb_function *f,
429 struct usb_composite_dev *cdev)
430{
431 return mbim_init(MAX_MBIM_INSTANCES);
432}
433
434static void mbim_function_cleanup(struct android_usb_function *f)
435{
436 fmbim_cleanup();
437}
438
439static int mbim_function_bind_config(struct android_usb_function *f,
440 struct usb_configuration *c)
441{
442 return mbim_bind_config(c, 0);
443}
444
445static struct android_usb_function mbim_function = {
446 .name = "usb_mbim",
447 .cleanup = mbim_function_cleanup,
448 .bind_config = mbim_function_bind_config,
449 .init = mbim_function_init,
450};
451
452
Manu Gautam8e0719b2011-09-26 14:47:55 +0530453/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530454static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455static ssize_t clients_store(
456 struct device *device, struct device_attribute *attr,
457 const char *buff, size_t size)
458{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530459 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460
461 return size;
462}
463
464static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
465static struct device_attribute *diag_function_attributes[] =
466 { &dev_attr_clients, NULL };
467
468static int diag_function_init(struct android_usb_function *f,
469 struct usb_composite_dev *cdev)
470{
471 return diag_setup();
472}
473
474static void diag_function_cleanup(struct android_usb_function *f)
475{
476 diag_cleanup();
477}
478
479static int diag_function_bind_config(struct android_usb_function *f,
480 struct usb_configuration *c)
481{
482 char *name;
483 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530484 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800485 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530487 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 b = strim(buf);
489
490 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800491 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530493 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700494 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530495 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530498 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499 if (err)
500 pr_err("diag: Cannot open channel '%s'", name);
501 }
502 }
503
504 return err;
505}
506
507static struct android_usb_function diag_function = {
508 .name = "diag",
509 .init = diag_function_init,
510 .cleanup = diag_function_cleanup,
511 .bind_config = diag_function_bind_config,
512 .attributes = diag_function_attributes,
513};
514
Manu Gautam8e0719b2011-09-26 14:47:55 +0530515/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530516static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530517static ssize_t serial_transports_store(
518 struct device *device, struct device_attribute *attr,
519 const char *buff, size_t size)
520{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530521 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530522
523 return size;
524}
525
526static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
527static struct device_attribute *serial_function_attributes[] =
528 { &dev_attr_transports, NULL };
529
530static void serial_function_cleanup(struct android_usb_function *f)
531{
532 gserial_cleanup();
533}
534
535static int serial_function_bind_config(struct android_usb_function *f,
536 struct usb_configuration *c)
537{
538 char *name;
539 char buf[32], *b;
540 int err = -1, i;
541 static int serial_initialized = 0, ports = 0;
542
543 if (serial_initialized)
544 goto bind_config;
545
546 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530547 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530548 b = strim(buf);
549
550 while (b) {
551 name = strsep(&b, ",");
552
553 if (name) {
554 err = gserial_init_port(ports, name);
555 if (err) {
556 pr_err("serial: Cannot open port '%s'", name);
557 goto out;
558 }
559 ports++;
560 }
561 }
562 err = gport_setup(c);
563 if (err) {
564 pr_err("serial: Cannot setup transports");
565 goto out;
566 }
567
568bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200569 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530570 err = gser_bind_config(c, i);
571 if (err) {
572 pr_err("serial: bind_config failed for port %d", i);
573 goto out;
574 }
575 }
576
577out:
578 return err;
579}
580
581static struct android_usb_function serial_function = {
582 .name = "serial",
583 .cleanup = serial_function_cleanup,
584 .bind_config = serial_function_bind_config,
585 .attributes = serial_function_attributes,
586};
587
Anji jonnala92be1b42011-12-19 09:44:41 +0530588/* ACM */
589static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
590static ssize_t acm_transports_store(
591 struct device *device, struct device_attribute *attr,
592 const char *buff, size_t size)
593{
594 strlcpy(acm_transports, buff, sizeof(acm_transports));
595
596 return size;
597}
598
599static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
600static struct device_attribute *acm_function_attributes[] = {
601 &dev_attr_acm_transports, NULL };
602
603static void acm_function_cleanup(struct android_usb_function *f)
604{
605 gserial_cleanup();
606}
607
608static int acm_function_bind_config(struct android_usb_function *f,
609 struct usb_configuration *c)
610{
611 char *name;
612 char buf[32], *b;
613 int err = -1, i;
614 static int acm_initialized, ports;
615
616 if (acm_initialized)
617 goto bind_config;
618
619 acm_initialized = 1;
620 strlcpy(buf, acm_transports, sizeof(buf));
621 b = strim(buf);
622
623 while (b) {
624 name = strsep(&b, ",");
625
626 if (name) {
627 err = acm_init_port(ports, name);
628 if (err) {
629 pr_err("acm: Cannot open port '%s'", name);
630 goto out;
631 }
632 ports++;
633 }
634 }
635 err = acm_port_setup(c);
636 if (err) {
637 pr_err("acm: Cannot setup transports");
638 goto out;
639 }
640
641bind_config:
642 for (i = 0; i < ports; i++) {
643 err = acm_bind_config(c, i);
644 if (err) {
645 pr_err("acm: bind_config failed for port %d", i);
646 goto out;
647 }
648 }
649
650out:
651 return err;
652}
653static struct android_usb_function acm_function = {
654 .name = "acm",
655 .cleanup = acm_function_cleanup,
656 .bind_config = acm_function_bind_config,
657 .attributes = acm_function_attributes,
658};
Manu Gautama4d993f2011-08-30 18:25:55 +0530659
Manu Gautam8e0719b2011-09-26 14:47:55 +0530660/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700661static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
662{
663 return adb_setup();
664}
665
666static void adb_function_cleanup(struct android_usb_function *f)
667{
668 adb_cleanup();
669}
670
671static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
672{
673 return adb_bind_config(c);
674}
675
676static struct android_usb_function adb_function = {
677 .name = "adb",
678 .init = adb_function_init,
679 .cleanup = adb_function_cleanup,
680 .bind_config = adb_function_bind_config,
681};
682
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530683/* CCID */
684static int ccid_function_init(struct android_usb_function *f,
685 struct usb_composite_dev *cdev)
686{
687 return ccid_setup();
688}
689
690static void ccid_function_cleanup(struct android_usb_function *f)
691{
692 ccid_cleanup();
693}
694
695static int ccid_function_bind_config(struct android_usb_function *f,
696 struct usb_configuration *c)
697{
698 return ccid_bind_config(c);
699}
700
701static struct android_usb_function ccid_function = {
702 .name = "ccid",
703 .init = ccid_function_init,
704 .cleanup = ccid_function_cleanup,
705 .bind_config = ccid_function_bind_config,
706};
707
Benoit Gobyaab96812011-04-19 20:37:33 -0700708static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
709{
710 return mtp_setup();
711}
712
713static void mtp_function_cleanup(struct android_usb_function *f)
714{
715 mtp_cleanup();
716}
717
718static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
719{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400720 return mtp_bind_config(c, false);
721}
722
723static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
724{
725 /* nothing to do - initialization is handled by mtp_function_init */
726 return 0;
727}
728
729static void ptp_function_cleanup(struct android_usb_function *f)
730{
731 /* nothing to do - cleanup is handled by mtp_function_cleanup */
732}
733
734static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
735{
736 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700737}
738
739static int mtp_function_ctrlrequest(struct android_usb_function *f,
740 struct usb_composite_dev *cdev,
741 const struct usb_ctrlrequest *c)
742{
743 return mtp_ctrlrequest(cdev, c);
744}
745
746static struct android_usb_function mtp_function = {
747 .name = "mtp",
748 .init = mtp_function_init,
749 .cleanup = mtp_function_cleanup,
750 .bind_config = mtp_function_bind_config,
751 .ctrlrequest = mtp_function_ctrlrequest,
752};
753
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400754/* PTP function is same as MTP with slightly different interface descriptor */
755static struct android_usb_function ptp_function = {
756 .name = "ptp",
757 .init = ptp_function_init,
758 .cleanup = ptp_function_cleanup,
759 .bind_config = ptp_function_bind_config,
760};
761
Benoit Gobyaab96812011-04-19 20:37:33 -0700762
763struct rndis_function_config {
764 u8 ethaddr[ETH_ALEN];
765 u32 vendorID;
766 char manufacturer[256];
767 bool wceis;
768};
769
770static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
771{
772 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
773 if (!f->config)
774 return -ENOMEM;
775 return 0;
776}
777
778static void rndis_function_cleanup(struct android_usb_function *f)
779{
780 kfree(f->config);
781 f->config = NULL;
782}
783
784static int rndis_function_bind_config(struct android_usb_function *f,
785 struct usb_configuration *c)
786{
Manu Gautamf4741132011-11-25 09:08:53 +0530787 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700788 struct rndis_function_config *rndis = f->config;
789
790 if (!rndis) {
791 pr_err("%s: rndis_pdata\n", __func__);
792 return -1;
793 }
794
795 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
796 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
797 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
798
Manu Gautamf4741132011-11-25 09:08:53 +0530799 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
800 if (ret) {
801 pr_err("%s: gether_setup failed\n", __func__);
802 return ret;
803 }
804
Benoit Gobyaab96812011-04-19 20:37:33 -0700805 if (rndis->wceis) {
806 /* "Wireless" RNDIS; auto-detected by Windows */
807 rndis_iad_descriptor.bFunctionClass =
808 USB_CLASS_WIRELESS_CONTROLLER;
809 rndis_iad_descriptor.bFunctionSubClass = 0x01;
810 rndis_iad_descriptor.bFunctionProtocol = 0x03;
811 rndis_control_intf.bInterfaceClass =
812 USB_CLASS_WIRELESS_CONTROLLER;
813 rndis_control_intf.bInterfaceSubClass = 0x01;
814 rndis_control_intf.bInterfaceProtocol = 0x03;
815 }
816
817 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
818 rndis->manufacturer);
819}
820
821static void rndis_function_unbind_config(struct android_usb_function *f,
822 struct usb_configuration *c)
823{
Manu Gautamf4741132011-11-25 09:08:53 +0530824 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700825}
826
827static ssize_t rndis_manufacturer_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 struct android_usb_function *f = dev_get_drvdata(dev);
831 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530832 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700833}
834
835static ssize_t rndis_manufacturer_store(struct device *dev,
836 struct device_attribute *attr, const char *buf, size_t size)
837{
838 struct android_usb_function *f = dev_get_drvdata(dev);
839 struct rndis_function_config *config = f->config;
840
841 if (size >= sizeof(config->manufacturer))
842 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530843 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700844 return size;
845 return -1;
846}
847
848static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
849 rndis_manufacturer_store);
850
851static ssize_t rndis_wceis_show(struct device *dev,
852 struct device_attribute *attr, char *buf)
853{
854 struct android_usb_function *f = dev_get_drvdata(dev);
855 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530856 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700857}
858
859static ssize_t rndis_wceis_store(struct device *dev,
860 struct device_attribute *attr, const char *buf, size_t size)
861{
862 struct android_usb_function *f = dev_get_drvdata(dev);
863 struct rndis_function_config *config = f->config;
864 int value;
865
866 if (sscanf(buf, "%d", &value) == 1) {
867 config->wceis = value;
868 return size;
869 }
870 return -EINVAL;
871}
872
873static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
874 rndis_wceis_store);
875
876static ssize_t rndis_ethaddr_show(struct device *dev,
877 struct device_attribute *attr, char *buf)
878{
879 struct android_usb_function *f = dev_get_drvdata(dev);
880 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530881 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700882 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
883 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
884}
885
886static ssize_t rndis_ethaddr_store(struct device *dev,
887 struct device_attribute *attr, const char *buf, size_t size)
888{
889 struct android_usb_function *f = dev_get_drvdata(dev);
890 struct rndis_function_config *rndis = f->config;
891
892 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
893 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
894 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
895 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
896 return size;
897 return -EINVAL;
898}
899
900static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
901 rndis_ethaddr_store);
902
903static ssize_t rndis_vendorID_show(struct device *dev,
904 struct device_attribute *attr, char *buf)
905{
906 struct android_usb_function *f = dev_get_drvdata(dev);
907 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530908 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700909}
910
911static ssize_t rndis_vendorID_store(struct device *dev,
912 struct device_attribute *attr, const char *buf, size_t size)
913{
914 struct android_usb_function *f = dev_get_drvdata(dev);
915 struct rndis_function_config *config = f->config;
916 int value;
917
918 if (sscanf(buf, "%04x", &value) == 1) {
919 config->vendorID = value;
920 return size;
921 }
922 return -EINVAL;
923}
924
925static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
926 rndis_vendorID_store);
927
928static struct device_attribute *rndis_function_attributes[] = {
929 &dev_attr_manufacturer,
930 &dev_attr_wceis,
931 &dev_attr_ethaddr,
932 &dev_attr_vendorID,
933 NULL
934};
935
936static struct android_usb_function rndis_function = {
937 .name = "rndis",
938 .init = rndis_function_init,
939 .cleanup = rndis_function_cleanup,
940 .bind_config = rndis_function_bind_config,
941 .unbind_config = rndis_function_unbind_config,
942 .attributes = rndis_function_attributes,
943};
944
945
946struct mass_storage_function_config {
947 struct fsg_config fsg;
948 struct fsg_common *common;
949};
950
951static int mass_storage_function_init(struct android_usb_function *f,
952 struct usb_composite_dev *cdev)
953{
954 struct mass_storage_function_config *config;
955 struct fsg_common *common;
956 int err;
957
958 config = kzalloc(sizeof(struct mass_storage_function_config),
959 GFP_KERNEL);
960 if (!config)
961 return -ENOMEM;
962
963 config->fsg.nluns = 1;
964 config->fsg.luns[0].removable = 1;
965
966 common = fsg_common_init(NULL, cdev, &config->fsg);
967 if (IS_ERR(common)) {
968 kfree(config);
969 return PTR_ERR(common);
970 }
971
972 err = sysfs_create_link(&f->dev->kobj,
973 &common->luns[0].dev.kobj,
974 "lun");
975 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530976 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700977 kfree(config);
978 return err;
979 }
980
981 config->common = common;
982 f->config = config;
983 return 0;
984}
985
986static void mass_storage_function_cleanup(struct android_usb_function *f)
987{
988 kfree(f->config);
989 f->config = NULL;
990}
991
992static int mass_storage_function_bind_config(struct android_usb_function *f,
993 struct usb_configuration *c)
994{
995 struct mass_storage_function_config *config = f->config;
996 return fsg_bind_config(c->cdev, c, config->common);
997}
998
999static ssize_t mass_storage_inquiry_show(struct device *dev,
1000 struct device_attribute *attr, char *buf)
1001{
1002 struct android_usb_function *f = dev_get_drvdata(dev);
1003 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301004 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -07001005}
1006
1007static ssize_t mass_storage_inquiry_store(struct device *dev,
1008 struct device_attribute *attr, const char *buf, size_t size)
1009{
1010 struct android_usb_function *f = dev_get_drvdata(dev);
1011 struct mass_storage_function_config *config = f->config;
1012 if (size >= sizeof(config->common->inquiry_string))
1013 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301014 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -07001015 return -EINVAL;
1016 return size;
1017}
1018
1019static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1020 mass_storage_inquiry_show,
1021 mass_storage_inquiry_store);
1022
1023static struct device_attribute *mass_storage_function_attributes[] = {
1024 &dev_attr_inquiry_string,
1025 NULL
1026};
1027
1028static struct android_usb_function mass_storage_function = {
1029 .name = "mass_storage",
1030 .init = mass_storage_function_init,
1031 .cleanup = mass_storage_function_cleanup,
1032 .bind_config = mass_storage_function_bind_config,
1033 .attributes = mass_storage_function_attributes,
1034};
1035
1036
1037static int accessory_function_init(struct android_usb_function *f,
1038 struct usb_composite_dev *cdev)
1039{
1040 return acc_setup();
1041}
1042
1043static void accessory_function_cleanup(struct android_usb_function *f)
1044{
1045 acc_cleanup();
1046}
1047
1048static int accessory_function_bind_config(struct android_usb_function *f,
1049 struct usb_configuration *c)
1050{
1051 return acc_bind_config(c);
1052}
1053
1054static int accessory_function_ctrlrequest(struct android_usb_function *f,
1055 struct usb_composite_dev *cdev,
1056 const struct usb_ctrlrequest *c)
1057{
1058 return acc_ctrlrequest(cdev, c);
1059}
1060
1061static struct android_usb_function accessory_function = {
1062 .name = "accessory",
1063 .init = accessory_function_init,
1064 .cleanup = accessory_function_cleanup,
1065 .bind_config = accessory_function_bind_config,
1066 .ctrlrequest = accessory_function_ctrlrequest,
1067};
1068
1069
1070static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001071 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301072 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301073 &rmnet_sdio_function,
1074 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301075 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301077 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001078 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301079 &ccid_function,
Anji jonnala92be1b42011-12-19 09:44:41 +05301080 &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001081 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001082 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001083 &rndis_function,
1084 &mass_storage_function,
1085 &accessory_function,
1086 NULL
1087};
1088
Lena Salmand092f2d2012-03-12 17:27:24 +02001089static void android_cleanup_functions(struct android_usb_function **functions)
1090{
1091 struct android_usb_function *f;
1092 struct device_attribute **attrs;
1093 struct device_attribute *attr;
1094
1095 while (*functions) {
1096 f = *functions++;
1097
1098 if (f->dev) {
1099 device_destroy(android_class, f->dev->devt);
1100 kfree(f->dev_name);
1101 } else
1102 continue;
1103
1104 if (f->cleanup)
1105 f->cleanup(f);
1106
1107 attrs = f->attributes;
1108 if (attrs) {
1109 while ((attr = *attrs++))
1110 device_remove_file(f->dev, attr);
1111 }
1112 }
1113}
Benoit Gobyaab96812011-04-19 20:37:33 -07001114
1115static int android_init_functions(struct android_usb_function **functions,
1116 struct usb_composite_dev *cdev)
1117{
1118 struct android_dev *dev = _android_dev;
1119 struct android_usb_function *f;
1120 struct device_attribute **attrs;
1121 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301122 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001123 int index = 1; /* index 0 is for android0 device */
Benoit Gobyaab96812011-04-19 20:37:33 -07001124
1125 for (; (f = *functions++); index++) {
1126 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001127 if (!f->dev_name) {
1128 err = -ENOMEM;
1129 goto err_out;
1130 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001131 f->dev = device_create(android_class, dev->dev,
1132 MKDEV(0, index), f, f->dev_name);
1133 if (IS_ERR(f->dev)) {
1134 pr_err("%s: Failed to create dev %s", __func__,
1135 f->dev_name);
1136 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001137 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001138 goto err_create;
1139 }
1140
1141 if (f->init) {
1142 err = f->init(f, cdev);
1143 if (err) {
1144 pr_err("%s: Failed to init %s", __func__,
1145 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001146 goto err_init;
Benoit Gobyaab96812011-04-19 20:37:33 -07001147 }
1148 }
1149
1150 attrs = f->attributes;
1151 if (attrs) {
1152 while ((attr = *attrs++) && !err)
1153 err = device_create_file(f->dev, attr);
1154 }
1155 if (err) {
1156 pr_err("%s: Failed to create function %s attributes",
1157 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001158 goto err_attrs;
Benoit Gobyaab96812011-04-19 20:37:33 -07001159 }
1160 }
1161 return 0;
1162
Lena Salmand092f2d2012-03-12 17:27:24 +02001163err_attrs:
1164 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1165 device_remove_file(f->dev, attr);
1166 if (f->cleanup)
1167 f->cleanup(f);
1168err_init:
Benoit Gobyaab96812011-04-19 20:37:33 -07001169 device_destroy(android_class, f->dev->devt);
1170err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001171 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001172 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001173err_out:
1174 android_cleanup_functions(dev->functions);
Benoit Gobyaab96812011-04-19 20:37:33 -07001175 return err;
1176}
1177
Benoit Gobyaab96812011-04-19 20:37:33 -07001178static int
1179android_bind_enabled_functions(struct android_dev *dev,
1180 struct usb_configuration *c)
1181{
1182 struct android_usb_function *f;
1183 int ret;
1184
1185 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1186 ret = f->bind_config(f, c);
1187 if (ret) {
1188 pr_err("%s: %s failed", __func__, f->name);
1189 return ret;
1190 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301191 }
1192 return 0;
1193}
1194
Benoit Gobyaab96812011-04-19 20:37:33 -07001195static void
1196android_unbind_enabled_functions(struct android_dev *dev,
1197 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301198{
Benoit Gobyaab96812011-04-19 20:37:33 -07001199 struct android_usb_function *f;
1200
1201 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1202 if (f->unbind_config)
1203 f->unbind_config(f, c);
1204 }
1205}
1206
1207static int android_enable_function(struct android_dev *dev, char *name)
1208{
1209 struct android_usb_function **functions = dev->functions;
1210 struct android_usb_function *f;
1211 while ((f = *functions++)) {
1212 if (!strcmp(name, f->name)) {
1213 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301214 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001215 }
1216 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001217 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001218}
1219
Benoit Gobyaab96812011-04-19 20:37:33 -07001220/*-------------------------------------------------------------------------*/
1221/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301222
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301223static ssize_t remote_wakeup_show(struct device *pdev,
1224 struct device_attribute *attr, char *buf)
1225{
1226 return snprintf(buf, PAGE_SIZE, "%d\n",
1227 !!(android_config_driver.bmAttributes &
1228 USB_CONFIG_ATT_WAKEUP));
1229}
1230
1231static ssize_t remote_wakeup_store(struct device *pdev,
1232 struct device_attribute *attr, const char *buff, size_t size)
1233{
1234 int enable = 0;
1235
1236 sscanf(buff, "%d", &enable);
1237
1238 pr_debug("android_usb: %s remote wakeup\n",
1239 enable ? "enabling" : "disabling");
1240
1241 if (enable)
1242 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1243 else
1244 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1245
1246 return size;
1247}
1248
Benoit Gobyaab96812011-04-19 20:37:33 -07001249static ssize_t
1250functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1251{
1252 struct android_dev *dev = dev_get_drvdata(pdev);
1253 struct android_usb_function *f;
1254 char *buff = buf;
1255
Benoit Gobydc1b6342011-12-09 18:05:00 -08001256 mutex_lock(&dev->mutex);
1257
Benoit Gobyaab96812011-04-19 20:37:33 -07001258 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301259 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001260
1261 mutex_unlock(&dev->mutex);
1262
Benoit Gobyaab96812011-04-19 20:37:33 -07001263 if (buff != buf)
1264 *(buff-1) = '\n';
1265 return buff - buf;
1266}
1267
1268static ssize_t
1269functions_store(struct device *pdev, struct device_attribute *attr,
1270 const char *buff, size_t size)
1271{
1272 struct android_dev *dev = dev_get_drvdata(pdev);
1273 char *name;
1274 char buf[256], *b;
1275 int err;
1276
Benoit Gobydc1b6342011-12-09 18:05:00 -08001277 mutex_lock(&dev->mutex);
1278
1279 if (dev->enabled) {
1280 mutex_unlock(&dev->mutex);
1281 return -EBUSY;
1282 }
1283
Benoit Gobyaab96812011-04-19 20:37:33 -07001284 INIT_LIST_HEAD(&dev->enabled_functions);
1285
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301286 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001287 b = strim(buf);
1288
1289 while (b) {
1290 name = strsep(&b, ",");
1291 if (name) {
1292 err = android_enable_function(dev, name);
1293 if (err)
1294 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301295 }
1296 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001297
Benoit Gobydc1b6342011-12-09 18:05:00 -08001298 mutex_unlock(&dev->mutex);
1299
Benoit Gobyaab96812011-04-19 20:37:33 -07001300 return size;
1301}
1302
1303static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1304 char *buf)
1305{
1306 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301307 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001308}
1309
1310static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1311 const char *buff, size_t size)
1312{
1313 struct android_dev *dev = dev_get_drvdata(pdev);
1314 struct usb_composite_dev *cdev = dev->cdev;
1315 int enabled = 0;
1316
Benoit Gobydc1b6342011-12-09 18:05:00 -08001317 mutex_lock(&dev->mutex);
1318
Benoit Gobyaab96812011-04-19 20:37:33 -07001319 sscanf(buff, "%d", &enabled);
1320 if (enabled && !dev->enabled) {
1321 /* update values in composite driver's copy of device descriptor */
1322 cdev->desc.idVendor = device_desc.idVendor;
1323 cdev->desc.idProduct = device_desc.idProduct;
1324 cdev->desc.bcdDevice = device_desc.bcdDevice;
1325 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1326 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1327 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301328 if (usb_add_config(cdev, &android_config_driver,
1329 android_bind_config))
1330 return size;
1331
Benoit Gobyaab96812011-04-19 20:37:33 -07001332 usb_gadget_connect(cdev->gadget);
1333 dev->enabled = true;
1334 } else if (!enabled && dev->enabled) {
1335 usb_gadget_disconnect(cdev->gadget);
Benoit Gobye0de0a52011-11-29 13:49:27 -08001336 /* Cancel pending control requests */
1337 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Benoit Gobyaab96812011-04-19 20:37:33 -07001338 usb_remove_config(cdev, &android_config_driver);
1339 dev->enabled = false;
1340 } else {
1341 pr_err("android_usb: already %s\n",
1342 dev->enabled ? "enabled" : "disabled");
1343 }
Benoit Gobydc1b6342011-12-09 18:05:00 -08001344
1345 mutex_unlock(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001346 return size;
1347}
1348
Ofir Cohen94213a72012-05-03 14:26:32 +03001349static ssize_t pm_qos_show(struct device *pdev,
1350 struct device_attribute *attr, char *buf)
1351{
1352 struct android_dev *dev = dev_get_drvdata(pdev);
1353
1354 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1355}
1356
1357static ssize_t pm_qos_store(struct device *pdev,
1358 struct device_attribute *attr,
1359 const char *buff, size_t size)
1360{
1361 struct android_dev *dev = dev_get_drvdata(pdev);
1362
1363 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1364
1365 return size;
1366}
1367
Benoit Gobyaab96812011-04-19 20:37:33 -07001368static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1369 char *buf)
1370{
1371 struct android_dev *dev = dev_get_drvdata(pdev);
1372 struct usb_composite_dev *cdev = dev->cdev;
1373 char *state = "DISCONNECTED";
1374 unsigned long flags;
1375
1376 if (!cdev)
1377 goto out;
1378
1379 spin_lock_irqsave(&cdev->lock, flags);
1380 if (cdev->config)
1381 state = "CONFIGURED";
1382 else if (dev->connected)
1383 state = "CONNECTED";
1384 spin_unlock_irqrestore(&cdev->lock, flags);
1385out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301386 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001387}
1388
1389#define DESCRIPTOR_ATTR(field, format_string) \
1390static ssize_t \
1391field ## _show(struct device *dev, struct device_attribute *attr, \
1392 char *buf) \
1393{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301394 return snprintf(buf, PAGE_SIZE, \
1395 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001396} \
1397static ssize_t \
1398field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001399 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001400{ \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001401 int value; \
Benoit Gobyaab96812011-04-19 20:37:33 -07001402 if (sscanf(buf, format_string, &value) == 1) { \
1403 device_desc.field = value; \
1404 return size; \
1405 } \
1406 return -1; \
1407} \
1408static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1409
1410#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1411static ssize_t \
1412field ## _show(struct device *dev, struct device_attribute *attr, \
1413 char *buf) \
1414{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301415 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001416} \
1417static ssize_t \
1418field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001419 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001420{ \
1421 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301422 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001423 return size; \
1424 } \
1425 return -1; \
1426} \
1427static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1428
1429
1430DESCRIPTOR_ATTR(idVendor, "%04x\n")
1431DESCRIPTOR_ATTR(idProduct, "%04x\n")
1432DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1433DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1434DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1435DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1436DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1437DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1438DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1439
1440static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1441static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001442static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1443 pm_qos_show, pm_qos_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001444static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301445static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1446 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001447
1448static struct device_attribute *android_usb_attributes[] = {
1449 &dev_attr_idVendor,
1450 &dev_attr_idProduct,
1451 &dev_attr_bcdDevice,
1452 &dev_attr_bDeviceClass,
1453 &dev_attr_bDeviceSubClass,
1454 &dev_attr_bDeviceProtocol,
1455 &dev_attr_iManufacturer,
1456 &dev_attr_iProduct,
1457 &dev_attr_iSerial,
1458 &dev_attr_functions,
1459 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001460 &dev_attr_pm_qos,
Benoit Gobyaab96812011-04-19 20:37:33 -07001461 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301462 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001463 NULL
1464};
1465
1466/*-------------------------------------------------------------------------*/
1467/* Composite driver */
1468
1469static int android_bind_config(struct usb_configuration *c)
1470{
1471 struct android_dev *dev = _android_dev;
1472 int ret = 0;
1473
1474 ret = android_bind_enabled_functions(dev, c);
1475 if (ret)
1476 return ret;
1477
1478 return 0;
1479}
1480
1481static void android_unbind_config(struct usb_configuration *c)
1482{
1483 struct android_dev *dev = _android_dev;
1484
1485 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301486}
1487
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001488static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001489{
1490 struct android_dev *dev = _android_dev;
1491 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001492 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001493
Benoit Gobyaab96812011-04-19 20:37:33 -07001494 usb_gadget_disconnect(gadget);
1495
1496 ret = android_init_functions(dev->functions, cdev);
1497 if (ret)
1498 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001499
1500 /* Allocate string descriptor numbers ... note that string
1501 * contents can be overridden by the composite_dev glue.
1502 */
1503 id = usb_string_id(cdev);
1504 if (id < 0)
1505 return id;
1506 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1507 device_desc.iManufacturer = id;
1508
1509 id = usb_string_id(cdev);
1510 if (id < 0)
1511 return id;
1512 strings_dev[STRING_PRODUCT_IDX].id = id;
1513 device_desc.iProduct = id;
1514
Benoit Gobyaab96812011-04-19 20:37:33 -07001515 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301516 strlcpy(manufacturer_string, "Android",
1517 sizeof(manufacturer_string) - 1);
1518 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1519 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001520
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001521 id = usb_string_id(cdev);
1522 if (id < 0)
1523 return id;
1524 strings_dev[STRING_SERIAL_IDX].id = id;
1525 device_desc.iSerialNumber = id;
1526
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301527 if (gadget_is_otg(cdev->gadget))
1528 android_config_driver.descriptors = otg_desc;
1529
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001530 gcnum = usb_gadget_controller_number(gadget);
1531 if (gcnum >= 0)
1532 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1533 else {
1534 /* gadget zero is so simple (for now, no altsettings) that
1535 * it SHOULD NOT have problems with bulk-capable hardware.
1536 * so just warn about unrcognized controllers -- don't panic.
1537 *
1538 * things like configuration and altsetting numbering
1539 * can need hardware-specific attention though.
1540 */
1541 pr_warning("%s: controller '%s' not recognized\n",
1542 longname, gadget->name);
1543 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1544 }
1545
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001546 dev->cdev = cdev;
1547
1548 return 0;
1549}
1550
Benoit Gobyaab96812011-04-19 20:37:33 -07001551static int android_usb_unbind(struct usb_composite_dev *cdev)
1552{
1553 struct android_dev *dev = _android_dev;
1554
Lena Salmand092f2d2012-03-12 17:27:24 +02001555 manufacturer_string[0] = '\0';
1556 product_string[0] = '\0';
1557 serial_string[0] = '0';
Benoit Gobyaab96812011-04-19 20:37:33 -07001558 cancel_work_sync(&dev->work);
1559 android_cleanup_functions(dev->functions);
1560 return 0;
1561}
1562
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001563static struct usb_composite_driver android_usb_driver = {
1564 .name = "android_usb",
1565 .dev = &device_desc,
1566 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001567 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001568 .max_speed = USB_SPEED_SUPER
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001569};
1570
Benoit Gobyaab96812011-04-19 20:37:33 -07001571static int
1572android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1573{
1574 struct android_dev *dev = _android_dev;
1575 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1576 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001577 struct android_usb_function *f;
1578 int value = -EOPNOTSUPP;
1579 unsigned long flags;
1580
1581 req->zero = 0;
1582 req->complete = composite_setup_complete;
1583 req->length = 0;
1584 gadget->ep0->driver_data = cdev;
1585
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001586 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001587 if (f->ctrlrequest) {
1588 value = f->ctrlrequest(f, cdev, c);
1589 if (value >= 0)
1590 break;
1591 }
1592 }
1593
Mike Lockwood686d33a2011-09-07 09:55:12 -07001594 /* Special case the accessory function.
1595 * It needs to handle control requests before it is enabled.
1596 */
1597 if (value < 0)
1598 value = acc_ctrlrequest(cdev, c);
1599
Benoit Gobyaab96812011-04-19 20:37:33 -07001600 if (value < 0)
1601 value = composite_setup(gadget, c);
1602
1603 spin_lock_irqsave(&cdev->lock, flags);
1604 if (!dev->connected) {
1605 dev->connected = 1;
1606 schedule_work(&dev->work);
1607 }
1608 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1609 schedule_work(&dev->work);
1610 }
1611 spin_unlock_irqrestore(&cdev->lock, flags);
1612
1613 return value;
1614}
1615
1616static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001617{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301618 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001619 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1620 unsigned long flags;
1621
1622 composite_disconnect(gadget);
1623
1624 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001625 dev->connected = 0;
1626 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001627 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301628}
1629
Benoit Gobyaab96812011-04-19 20:37:33 -07001630static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001631{
Benoit Gobyaab96812011-04-19 20:37:33 -07001632 struct device_attribute **attrs = android_usb_attributes;
1633 struct device_attribute *attr;
1634 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001635
Benoit Gobyaab96812011-04-19 20:37:33 -07001636 dev->dev = device_create(android_class, NULL,
1637 MKDEV(0, 0), NULL, "android0");
1638 if (IS_ERR(dev->dev))
1639 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001640
Benoit Gobyaab96812011-04-19 20:37:33 -07001641 dev_set_drvdata(dev->dev, dev);
1642
1643 while ((attr = *attrs++)) {
1644 err = device_create_file(dev->dev, attr);
1645 if (err) {
1646 device_destroy(android_class, dev->dev->devt);
1647 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001648 }
1649 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001650 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001651}
1652
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301653static void android_destroy_device(struct android_dev *dev)
1654{
1655 struct device_attribute **attrs = android_usb_attributes;
1656 struct device_attribute *attr;
1657
1658 while ((attr = *attrs++))
1659 device_remove_file(dev->dev, attr);
1660 device_destroy(android_class, dev->dev->devt);
1661}
1662
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001663static int __devinit android_probe(struct platform_device *pdev)
1664{
1665 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1666 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001667 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668
1669 dev->pdata = pdata;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301670
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301671 android_class = class_create(THIS_MODULE, "android_usb");
1672 if (IS_ERR(android_class))
1673 return PTR_ERR(android_class);
1674
1675 ret = android_create_device(dev);
1676 if (ret) {
1677 pr_err("%s(): android_create_device failed\n", __func__);
1678 goto err_dev;
1679 }
1680
Lena Salmand092f2d2012-03-12 17:27:24 +02001681 ret = usb_composite_probe(&android_usb_driver, android_bind);
1682 if (ret) {
1683 pr_err("%s(): Failed to register android "
1684 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301685 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001686 }
1687
Ofir Cohen94213a72012-05-03 14:26:32 +03001688 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301689 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001690 pm_qos_add_request(&dev->pm_qos_req_dma,
1691 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1692 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1693
Lena Salmand092f2d2012-03-12 17:27:24 +02001694 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301695err_probe:
1696 android_destroy_device(dev);
1697err_dev:
1698 class_destroy(android_class);
1699 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001700}
1701
1702static int android_remove(struct platform_device *pdev)
1703{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301704 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001705 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301706
1707 android_destroy_device(dev);
1708 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001709 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301710 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001711 pm_qos_remove_request(&dev->pm_qos_req_dma);
1712
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 return 0;
1714}
1715
1716static struct platform_driver android_platform_driver = {
1717 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001718 .probe = android_probe,
1719 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001721
1722static int __init init(void)
1723{
1724 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301725 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001726
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001727 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301728 if (!dev) {
1729 pr_err("%s(): Failed to alloc memory for android_dev\n",
1730 __func__);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001731 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301732 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001733 dev->functions = supported_functions;
1734 INIT_LIST_HEAD(&dev->enabled_functions);
1735 INIT_WORK(&dev->work, android_work);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001736 mutex_init(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001737
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001738 _android_dev = dev;
1739
Benoit Gobyaab96812011-04-19 20:37:33 -07001740 /* Override composite driver functions */
1741 composite_driver.setup = android_setup;
1742 composite_driver.disconnect = android_disconnect;
1743
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301744 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301745 if (ret) {
1746 pr_err("%s(): Failed to register android"
1747 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301748 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301749 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001750
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301751 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001752}
1753module_init(init);
1754
1755static void __exit cleanup(void)
1756{
Lena Salmand092f2d2012-03-12 17:27:24 +02001757 platform_driver_unregister(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001758 kfree(_android_dev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001759 _android_dev = NULL;
1760}
1761module_exit(cleanup);