blob: 3e5c6f1fd0d43d1a1eb8e98e50a848dcdf6c0a84 [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>
30
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050031#include <linux/usb/ch9.h>
32#include <linux/usb/composite.h>
33#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <linux/usb/android.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050035
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050036#include "gadget_chips.h"
37
38/*
39 * Kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 */
45#include "usbstring.c"
46#include "config.c"
47#include "epautoconf.c"
48#include "composite.c"
49
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053051#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053052#include "f_rmnet_sdio.c"
53#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053054#include "f_rmnet.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070055#include "f_mass_storage.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053056#include "u_serial.c"
57#include "u_sdio.c"
58#include "u_smd.c"
59#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053060#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080061#include "u_ctrl_hsic.c"
62#include "u_data_hsic.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053063#include "f_serial.c"
Anji jonnala92be1b42011-12-19 09:44:41 +053064#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070065#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053066#include "f_ccid.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070067#include "f_mtp.c"
68#include "f_accessory.c"
69#define USB_ETH_RNDIS y
70#include "f_rndis.c"
71#include "rndis.c"
72#include "u_ether.c"
73
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050074MODULE_AUTHOR("Mike Lockwood");
75MODULE_DESCRIPTION("Android Composite USB Driver");
76MODULE_LICENSE("GPL");
77MODULE_VERSION("1.0");
78
79static const char longname[] = "Gadget Android";
80
Benoit Gobyaab96812011-04-19 20:37:33 -070081/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050082#define VENDOR_ID 0x18D1
83#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050084
Benoit Gobyaab96812011-04-19 20:37:33 -070085struct android_usb_function {
86 char *name;
87 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050088
Benoit Gobyaab96812011-04-19 20:37:33 -070089 struct device *dev;
90 char *dev_name;
91 struct device_attribute **attributes;
92
93 /* for android_dev.enabled_functions */
94 struct list_head enabled_list;
95
96 /* Optional: initialization during gadget bind */
97 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
98 /* Optional: cleanup during gadget unbind */
99 void (*cleanup)(struct android_usb_function *);
100
101 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
102
103 /* Optional: called when the configuration is removed */
104 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700105 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700106 int (*ctrlrequest)(struct android_usb_function *,
107 struct usb_composite_dev *,
108 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500109};
110
Benoit Gobyaab96812011-04-19 20:37:33 -0700111struct android_dev {
112 struct android_usb_function **functions;
113 struct list_head enabled_functions;
114 struct usb_composite_dev *cdev;
115 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700117
118 bool enabled;
Benoit Gobydc1b6342011-12-09 18:05:00 -0800119 struct mutex mutex;
Benoit Gobyaab96812011-04-19 20:37:33 -0700120 bool connected;
121 bool sw_connected;
122 struct work_struct work;
123};
124
125static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500126static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700127static int android_bind_config(struct usb_configuration *c);
128static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500129
130/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500131#define STRING_MANUFACTURER_IDX 0
132#define STRING_PRODUCT_IDX 1
133#define STRING_SERIAL_IDX 2
134
Benoit Gobyaab96812011-04-19 20:37:33 -0700135static char manufacturer_string[256];
136static char product_string[256];
137static char serial_string[256];
138
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500139/* String Table */
140static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700141 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
142 [STRING_PRODUCT_IDX].s = product_string,
143 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500144 { } /* end of list */
145};
146
147static struct usb_gadget_strings stringtab_dev = {
148 .language = 0x0409, /* en-us */
149 .strings = strings_dev,
150};
151
152static struct usb_gadget_strings *dev_strings[] = {
153 &stringtab_dev,
154 NULL,
155};
156
157static struct usb_device_descriptor device_desc = {
158 .bLength = sizeof(device_desc),
159 .bDescriptorType = USB_DT_DEVICE,
160 .bcdUSB = __constant_cpu_to_le16(0x0200),
161 .bDeviceClass = USB_CLASS_PER_INTERFACE,
162 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
163 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
164 .bcdDevice = __constant_cpu_to_le16(0xffff),
165 .bNumConfigurations = 1,
166};
167
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530168static struct usb_otg_descriptor otg_descriptor = {
169 .bLength = sizeof otg_descriptor,
170 .bDescriptorType = USB_DT_OTG,
171 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
172 .bcdOTG = __constant_cpu_to_le16(0x0200),
173};
174
175static const struct usb_descriptor_header *otg_desc[] = {
176 (struct usb_descriptor_header *) &otg_descriptor,
177 NULL,
178};
179
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500180static struct usb_configuration android_config_driver = {
181 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700182 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500183 .bConfigurationValue = 1,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500184};
185
Manu Gautama2b54142012-04-03 14:34:32 +0530186enum android_device_state {
187 USB_DISCONNECTED,
188 USB_CONNECTED,
189 USB_CONFIGURED,
190};
191
Benoit Gobyaab96812011-04-19 20:37:33 -0700192static void android_work(struct work_struct *data)
193{
194 struct android_dev *dev = container_of(data, struct android_dev, work);
195 struct usb_composite_dev *cdev = dev->cdev;
196 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
197 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
198 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700199 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530200 static enum android_device_state last_uevent, next_state;
Benoit Gobyaab96812011-04-19 20:37:33 -0700201 unsigned long flags;
202
203 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530204 if (cdev->config) {
Dima Zavinfc753492011-09-14 11:52:45 -0700205 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530206 next_state = USB_CONFIGURED;
207 } else if (dev->connected != dev->sw_connected) {
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700208 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530209 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
210 }
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700211 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700212 spin_unlock_irqrestore(&cdev->lock, flags);
213
214 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530215 /*
216 * Some userspace modules, e.g. MTP, work correctly only if
217 * CONFIGURED uevent is preceded by DISCONNECT uevent.
218 * Check if we missed sending out a DISCONNECT uevent. This can
219 * happen if host PC resets and configures device really quick.
220 */
221 if (((uevent_envp == connected) &&
222 (last_uevent != USB_DISCONNECTED)) ||
223 ((uevent_envp == configured) &&
224 (last_uevent == USB_CONFIGURED))) {
225 pr_info("%s: sent missed DISCONNECT event\n", __func__);
226 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
227 disconnected);
228 msleep(20);
229 }
230 /*
231 * Before sending out CONFIGURED uevent give function drivers
232 * a chance to wakeup userspace threads and notify disconnect
233 */
234 if (uevent_envp == configured)
235 msleep(50);
236
Dima Zavinfc753492011-09-14 11:52:45 -0700237 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530238 last_uevent = next_state;
Dima Zavinfc753492011-09-14 11:52:45 -0700239 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700240 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700241 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
242 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700243 }
244}
245
246
247/*-------------------------------------------------------------------------*/
248/* Supported functions initialization */
249
Manu Gautam8e0719b2011-09-26 14:47:55 +0530250/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530251static int rmnet_smd_function_bind_config(struct android_usb_function *f,
252 struct usb_configuration *c)
253{
254 return rmnet_smd_bind_config(c);
255}
256
257static struct android_usb_function rmnet_smd_function = {
258 .name = "rmnet_smd",
259 .bind_config = rmnet_smd_function_bind_config,
260};
261
Manu Gautam8e0719b2011-09-26 14:47:55 +0530262/* RMNET_SDIO */
263static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
264 struct usb_configuration *c)
265{
266 return rmnet_sdio_function_add(c);
267}
268
269static struct android_usb_function rmnet_sdio_function = {
270 .name = "rmnet_sdio",
271 .bind_config = rmnet_sdio_function_bind_config,
272};
273
274/* RMNET_SMD_SDIO */
275static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
276 struct usb_composite_dev *cdev)
277{
278 return rmnet_smd_sdio_init();
279}
280
281static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
282{
283 rmnet_smd_sdio_cleanup();
284}
285
286static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
287 struct usb_configuration *c)
288{
289 return rmnet_smd_sdio_function_add(c);
290}
291
292static struct device_attribute *rmnet_smd_sdio_attributes[] = {
293 &dev_attr_transport, NULL };
294
295static struct android_usb_function rmnet_smd_sdio_function = {
296 .name = "rmnet_smd_sdio",
297 .init = rmnet_smd_sdio_function_init,
298 .cleanup = rmnet_smd_sdio_function_cleanup,
299 .bind_config = rmnet_smd_sdio_bind_config,
300 .attributes = rmnet_smd_sdio_attributes,
301};
302
Hemant Kumar1b820d52011-11-03 15:08:28 -0700303/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
304#define MAX_XPORT_STR_LEN 50
305static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530306
Manu Gautame3e897c2011-09-12 17:18:46 +0530307static void rmnet_function_cleanup(struct android_usb_function *f)
308{
309 frmnet_cleanup();
310}
311
Manu Gautam2b0234a2011-09-07 16:47:52 +0530312static int rmnet_function_bind_config(struct android_usb_function *f,
313 struct usb_configuration *c)
314{
315 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700316 int err = 0;
317 char *ctrl_name;
318 char *data_name;
319 char buf[MAX_XPORT_STR_LEN], *b;
320 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530321
Hemant Kumar1b820d52011-11-03 15:08:28 -0700322 if (!rmnet_initialized) {
323 rmnet_initialized = 1;
324 strlcpy(buf, rmnet_transports, sizeof(buf));
325 b = strim(buf);
326 while (b) {
327 ctrl_name = strsep(&b, ",");
328 data_name = strsep(&b, ",");
329 if (ctrl_name && data_name) {
330 err = frmnet_init_port(ctrl_name, data_name);
331 if (err) {
332 pr_err("rmnet: Cannot open ctrl port:"
333 "'%s' data port:'%s'\n",
334 ctrl_name, data_name);
335 goto out;
336 }
337 ports++;
338 }
339 }
340
341 err = rmnet_gport_setup();
342 if (err) {
343 pr_err("rmnet: Cannot setup transports");
344 goto out;
345 }
346 }
347
348 for (i = 0; i < ports; i++) {
349 err = frmnet_bind_config(c, i);
350 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530351 pr_err("Could not bind rmnet%u config\n", i);
352 break;
353 }
354 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700355out:
356 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530357}
358
Hemant Kumar1b820d52011-11-03 15:08:28 -0700359static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530360 struct device_attribute *attr, char *buf)
361{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700362 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530363}
364
Hemant Kumar1b820d52011-11-03 15:08:28 -0700365static ssize_t rmnet_transports_store(
366 struct device *device, struct device_attribute *attr,
367 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530368{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700369 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530370
Manu Gautam2b0234a2011-09-07 16:47:52 +0530371 return size;
372}
373
Hemant Kumar1b820d52011-11-03 15:08:28 -0700374static struct device_attribute dev_attr_rmnet_transports =
375 __ATTR(transports, S_IRUGO | S_IWUSR,
376 rmnet_transports_show,
377 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530378static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700379 &dev_attr_rmnet_transports,
380 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530381
382static struct android_usb_function rmnet_function = {
383 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530384 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530385 .bind_config = rmnet_function_bind_config,
386 .attributes = rmnet_function_attributes,
387};
388
Manu Gautam8e0719b2011-09-26 14:47:55 +0530389/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530390static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391static ssize_t clients_store(
392 struct device *device, struct device_attribute *attr,
393 const char *buff, size_t size)
394{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530395 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396
397 return size;
398}
399
400static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
401static struct device_attribute *diag_function_attributes[] =
402 { &dev_attr_clients, NULL };
403
404static int diag_function_init(struct android_usb_function *f,
405 struct usb_composite_dev *cdev)
406{
407 return diag_setup();
408}
409
410static void diag_function_cleanup(struct android_usb_function *f)
411{
412 diag_cleanup();
413}
414
415static int diag_function_bind_config(struct android_usb_function *f,
416 struct usb_configuration *c)
417{
418 char *name;
419 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530420 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800421 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530423 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 b = strim(buf);
425
426 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800427 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530429 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700430 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530431 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530434 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 if (err)
436 pr_err("diag: Cannot open channel '%s'", name);
437 }
438 }
439
440 return err;
441}
442
443static struct android_usb_function diag_function = {
444 .name = "diag",
445 .init = diag_function_init,
446 .cleanup = diag_function_cleanup,
447 .bind_config = diag_function_bind_config,
448 .attributes = diag_function_attributes,
449};
450
Manu Gautam8e0719b2011-09-26 14:47:55 +0530451/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530452static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530453static ssize_t serial_transports_store(
454 struct device *device, struct device_attribute *attr,
455 const char *buff, size_t size)
456{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530457 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530458
459 return size;
460}
461
462static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
463static struct device_attribute *serial_function_attributes[] =
464 { &dev_attr_transports, NULL };
465
466static void serial_function_cleanup(struct android_usb_function *f)
467{
468 gserial_cleanup();
469}
470
471static int serial_function_bind_config(struct android_usb_function *f,
472 struct usb_configuration *c)
473{
474 char *name;
475 char buf[32], *b;
476 int err = -1, i;
477 static int serial_initialized = 0, ports = 0;
478
479 if (serial_initialized)
480 goto bind_config;
481
482 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530483 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530484 b = strim(buf);
485
486 while (b) {
487 name = strsep(&b, ",");
488
489 if (name) {
490 err = gserial_init_port(ports, name);
491 if (err) {
492 pr_err("serial: Cannot open port '%s'", name);
493 goto out;
494 }
495 ports++;
496 }
497 }
498 err = gport_setup(c);
499 if (err) {
500 pr_err("serial: Cannot setup transports");
501 goto out;
502 }
503
504bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200505 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530506 err = gser_bind_config(c, i);
507 if (err) {
508 pr_err("serial: bind_config failed for port %d", i);
509 goto out;
510 }
511 }
512
513out:
514 return err;
515}
516
517static struct android_usb_function serial_function = {
518 .name = "serial",
519 .cleanup = serial_function_cleanup,
520 .bind_config = serial_function_bind_config,
521 .attributes = serial_function_attributes,
522};
523
Anji jonnala92be1b42011-12-19 09:44:41 +0530524/* ACM */
525static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
526static ssize_t acm_transports_store(
527 struct device *device, struct device_attribute *attr,
528 const char *buff, size_t size)
529{
530 strlcpy(acm_transports, buff, sizeof(acm_transports));
531
532 return size;
533}
534
535static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
536static struct device_attribute *acm_function_attributes[] = {
537 &dev_attr_acm_transports, NULL };
538
539static void acm_function_cleanup(struct android_usb_function *f)
540{
541 gserial_cleanup();
542}
543
544static int acm_function_bind_config(struct android_usb_function *f,
545 struct usb_configuration *c)
546{
547 char *name;
548 char buf[32], *b;
549 int err = -1, i;
550 static int acm_initialized, ports;
551
552 if (acm_initialized)
553 goto bind_config;
554
555 acm_initialized = 1;
556 strlcpy(buf, acm_transports, sizeof(buf));
557 b = strim(buf);
558
559 while (b) {
560 name = strsep(&b, ",");
561
562 if (name) {
563 err = acm_init_port(ports, name);
564 if (err) {
565 pr_err("acm: Cannot open port '%s'", name);
566 goto out;
567 }
568 ports++;
569 }
570 }
571 err = acm_port_setup(c);
572 if (err) {
573 pr_err("acm: Cannot setup transports");
574 goto out;
575 }
576
577bind_config:
578 for (i = 0; i < ports; i++) {
579 err = acm_bind_config(c, i);
580 if (err) {
581 pr_err("acm: bind_config failed for port %d", i);
582 goto out;
583 }
584 }
585
586out:
587 return err;
588}
589static struct android_usb_function acm_function = {
590 .name = "acm",
591 .cleanup = acm_function_cleanup,
592 .bind_config = acm_function_bind_config,
593 .attributes = acm_function_attributes,
594};
Manu Gautama4d993f2011-08-30 18:25:55 +0530595
Manu Gautam8e0719b2011-09-26 14:47:55 +0530596/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700597static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
598{
599 return adb_setup();
600}
601
602static void adb_function_cleanup(struct android_usb_function *f)
603{
604 adb_cleanup();
605}
606
607static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
608{
609 return adb_bind_config(c);
610}
611
612static struct android_usb_function adb_function = {
613 .name = "adb",
614 .init = adb_function_init,
615 .cleanup = adb_function_cleanup,
616 .bind_config = adb_function_bind_config,
617};
618
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530619/* CCID */
620static int ccid_function_init(struct android_usb_function *f,
621 struct usb_composite_dev *cdev)
622{
623 return ccid_setup();
624}
625
626static void ccid_function_cleanup(struct android_usb_function *f)
627{
628 ccid_cleanup();
629}
630
631static int ccid_function_bind_config(struct android_usb_function *f,
632 struct usb_configuration *c)
633{
634 return ccid_bind_config(c);
635}
636
637static struct android_usb_function ccid_function = {
638 .name = "ccid",
639 .init = ccid_function_init,
640 .cleanup = ccid_function_cleanup,
641 .bind_config = ccid_function_bind_config,
642};
643
Benoit Gobyaab96812011-04-19 20:37:33 -0700644static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
645{
646 return mtp_setup();
647}
648
649static void mtp_function_cleanup(struct android_usb_function *f)
650{
651 mtp_cleanup();
652}
653
654static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
655{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400656 return mtp_bind_config(c, false);
657}
658
659static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
660{
661 /* nothing to do - initialization is handled by mtp_function_init */
662 return 0;
663}
664
665static void ptp_function_cleanup(struct android_usb_function *f)
666{
667 /* nothing to do - cleanup is handled by mtp_function_cleanup */
668}
669
670static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
671{
672 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700673}
674
675static int mtp_function_ctrlrequest(struct android_usb_function *f,
676 struct usb_composite_dev *cdev,
677 const struct usb_ctrlrequest *c)
678{
679 return mtp_ctrlrequest(cdev, c);
680}
681
682static struct android_usb_function mtp_function = {
683 .name = "mtp",
684 .init = mtp_function_init,
685 .cleanup = mtp_function_cleanup,
686 .bind_config = mtp_function_bind_config,
687 .ctrlrequest = mtp_function_ctrlrequest,
688};
689
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400690/* PTP function is same as MTP with slightly different interface descriptor */
691static struct android_usb_function ptp_function = {
692 .name = "ptp",
693 .init = ptp_function_init,
694 .cleanup = ptp_function_cleanup,
695 .bind_config = ptp_function_bind_config,
696};
697
Benoit Gobyaab96812011-04-19 20:37:33 -0700698
699struct rndis_function_config {
700 u8 ethaddr[ETH_ALEN];
701 u32 vendorID;
702 char manufacturer[256];
703 bool wceis;
704};
705
706static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
707{
708 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
709 if (!f->config)
710 return -ENOMEM;
711 return 0;
712}
713
714static void rndis_function_cleanup(struct android_usb_function *f)
715{
716 kfree(f->config);
717 f->config = NULL;
718}
719
720static int rndis_function_bind_config(struct android_usb_function *f,
721 struct usb_configuration *c)
722{
Manu Gautamf4741132011-11-25 09:08:53 +0530723 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700724 struct rndis_function_config *rndis = f->config;
725
726 if (!rndis) {
727 pr_err("%s: rndis_pdata\n", __func__);
728 return -1;
729 }
730
731 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
732 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
733 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
734
Manu Gautamf4741132011-11-25 09:08:53 +0530735 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
736 if (ret) {
737 pr_err("%s: gether_setup failed\n", __func__);
738 return ret;
739 }
740
Benoit Gobyaab96812011-04-19 20:37:33 -0700741 if (rndis->wceis) {
742 /* "Wireless" RNDIS; auto-detected by Windows */
743 rndis_iad_descriptor.bFunctionClass =
744 USB_CLASS_WIRELESS_CONTROLLER;
745 rndis_iad_descriptor.bFunctionSubClass = 0x01;
746 rndis_iad_descriptor.bFunctionProtocol = 0x03;
747 rndis_control_intf.bInterfaceClass =
748 USB_CLASS_WIRELESS_CONTROLLER;
749 rndis_control_intf.bInterfaceSubClass = 0x01;
750 rndis_control_intf.bInterfaceProtocol = 0x03;
751 }
752
753 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
754 rndis->manufacturer);
755}
756
757static void rndis_function_unbind_config(struct android_usb_function *f,
758 struct usb_configuration *c)
759{
Manu Gautamf4741132011-11-25 09:08:53 +0530760 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700761}
762
763static ssize_t rndis_manufacturer_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
765{
766 struct android_usb_function *f = dev_get_drvdata(dev);
767 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530768 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700769}
770
771static ssize_t rndis_manufacturer_store(struct device *dev,
772 struct device_attribute *attr, const char *buf, size_t size)
773{
774 struct android_usb_function *f = dev_get_drvdata(dev);
775 struct rndis_function_config *config = f->config;
776
777 if (size >= sizeof(config->manufacturer))
778 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530779 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700780 return size;
781 return -1;
782}
783
784static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
785 rndis_manufacturer_store);
786
787static ssize_t rndis_wceis_show(struct device *dev,
788 struct device_attribute *attr, char *buf)
789{
790 struct android_usb_function *f = dev_get_drvdata(dev);
791 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530792 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700793}
794
795static ssize_t rndis_wceis_store(struct device *dev,
796 struct device_attribute *attr, const char *buf, size_t size)
797{
798 struct android_usb_function *f = dev_get_drvdata(dev);
799 struct rndis_function_config *config = f->config;
800 int value;
801
802 if (sscanf(buf, "%d", &value) == 1) {
803 config->wceis = value;
804 return size;
805 }
806 return -EINVAL;
807}
808
809static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
810 rndis_wceis_store);
811
812static ssize_t rndis_ethaddr_show(struct device *dev,
813 struct device_attribute *attr, char *buf)
814{
815 struct android_usb_function *f = dev_get_drvdata(dev);
816 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530817 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700818 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
819 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
820}
821
822static ssize_t rndis_ethaddr_store(struct device *dev,
823 struct device_attribute *attr, const char *buf, size_t size)
824{
825 struct android_usb_function *f = dev_get_drvdata(dev);
826 struct rndis_function_config *rndis = f->config;
827
828 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
829 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
830 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
831 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
832 return size;
833 return -EINVAL;
834}
835
836static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
837 rndis_ethaddr_store);
838
839static ssize_t rndis_vendorID_show(struct device *dev,
840 struct device_attribute *attr, char *buf)
841{
842 struct android_usb_function *f = dev_get_drvdata(dev);
843 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530844 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700845}
846
847static ssize_t rndis_vendorID_store(struct device *dev,
848 struct device_attribute *attr, const char *buf, size_t size)
849{
850 struct android_usb_function *f = dev_get_drvdata(dev);
851 struct rndis_function_config *config = f->config;
852 int value;
853
854 if (sscanf(buf, "%04x", &value) == 1) {
855 config->vendorID = value;
856 return size;
857 }
858 return -EINVAL;
859}
860
861static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
862 rndis_vendorID_store);
863
864static struct device_attribute *rndis_function_attributes[] = {
865 &dev_attr_manufacturer,
866 &dev_attr_wceis,
867 &dev_attr_ethaddr,
868 &dev_attr_vendorID,
869 NULL
870};
871
872static struct android_usb_function rndis_function = {
873 .name = "rndis",
874 .init = rndis_function_init,
875 .cleanup = rndis_function_cleanup,
876 .bind_config = rndis_function_bind_config,
877 .unbind_config = rndis_function_unbind_config,
878 .attributes = rndis_function_attributes,
879};
880
881
882struct mass_storage_function_config {
883 struct fsg_config fsg;
884 struct fsg_common *common;
885};
886
887static int mass_storage_function_init(struct android_usb_function *f,
888 struct usb_composite_dev *cdev)
889{
890 struct mass_storage_function_config *config;
891 struct fsg_common *common;
892 int err;
893
894 config = kzalloc(sizeof(struct mass_storage_function_config),
895 GFP_KERNEL);
896 if (!config)
897 return -ENOMEM;
898
899 config->fsg.nluns = 1;
900 config->fsg.luns[0].removable = 1;
901
902 common = fsg_common_init(NULL, cdev, &config->fsg);
903 if (IS_ERR(common)) {
904 kfree(config);
905 return PTR_ERR(common);
906 }
907
908 err = sysfs_create_link(&f->dev->kobj,
909 &common->luns[0].dev.kobj,
910 "lun");
911 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530912 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700913 kfree(config);
914 return err;
915 }
916
917 config->common = common;
918 f->config = config;
919 return 0;
920}
921
922static void mass_storage_function_cleanup(struct android_usb_function *f)
923{
924 kfree(f->config);
925 f->config = NULL;
926}
927
928static int mass_storage_function_bind_config(struct android_usb_function *f,
929 struct usb_configuration *c)
930{
931 struct mass_storage_function_config *config = f->config;
932 return fsg_bind_config(c->cdev, c, config->common);
933}
934
935static ssize_t mass_storage_inquiry_show(struct device *dev,
936 struct device_attribute *attr, char *buf)
937{
938 struct android_usb_function *f = dev_get_drvdata(dev);
939 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530940 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700941}
942
943static ssize_t mass_storage_inquiry_store(struct device *dev,
944 struct device_attribute *attr, const char *buf, size_t size)
945{
946 struct android_usb_function *f = dev_get_drvdata(dev);
947 struct mass_storage_function_config *config = f->config;
948 if (size >= sizeof(config->common->inquiry_string))
949 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530950 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700951 return -EINVAL;
952 return size;
953}
954
955static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
956 mass_storage_inquiry_show,
957 mass_storage_inquiry_store);
958
959static struct device_attribute *mass_storage_function_attributes[] = {
960 &dev_attr_inquiry_string,
961 NULL
962};
963
964static struct android_usb_function mass_storage_function = {
965 .name = "mass_storage",
966 .init = mass_storage_function_init,
967 .cleanup = mass_storage_function_cleanup,
968 .bind_config = mass_storage_function_bind_config,
969 .attributes = mass_storage_function_attributes,
970};
971
972
973static int accessory_function_init(struct android_usb_function *f,
974 struct usb_composite_dev *cdev)
975{
976 return acc_setup();
977}
978
979static void accessory_function_cleanup(struct android_usb_function *f)
980{
981 acc_cleanup();
982}
983
984static int accessory_function_bind_config(struct android_usb_function *f,
985 struct usb_configuration *c)
986{
987 return acc_bind_config(c);
988}
989
990static int accessory_function_ctrlrequest(struct android_usb_function *f,
991 struct usb_composite_dev *cdev,
992 const struct usb_ctrlrequest *c)
993{
994 return acc_ctrlrequest(cdev, c);
995}
996
997static struct android_usb_function accessory_function = {
998 .name = "accessory",
999 .init = accessory_function_init,
1000 .cleanup = accessory_function_cleanup,
1001 .bind_config = accessory_function_bind_config,
1002 .ctrlrequest = accessory_function_ctrlrequest,
1003};
1004
1005
1006static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301007 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301008 &rmnet_sdio_function,
1009 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301010 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301012 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001013 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301014 &ccid_function,
Anji jonnala92be1b42011-12-19 09:44:41 +05301015 &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001016 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001017 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001018 &rndis_function,
1019 &mass_storage_function,
1020 &accessory_function,
1021 NULL
1022};
1023
Lena Salmand092f2d2012-03-12 17:27:24 +02001024static void android_cleanup_functions(struct android_usb_function **functions)
1025{
1026 struct android_usb_function *f;
1027 struct device_attribute **attrs;
1028 struct device_attribute *attr;
1029
1030 while (*functions) {
1031 f = *functions++;
1032
1033 if (f->dev) {
1034 device_destroy(android_class, f->dev->devt);
1035 kfree(f->dev_name);
1036 } else
1037 continue;
1038
1039 if (f->cleanup)
1040 f->cleanup(f);
1041
1042 attrs = f->attributes;
1043 if (attrs) {
1044 while ((attr = *attrs++))
1045 device_remove_file(f->dev, attr);
1046 }
1047 }
1048}
Benoit Gobyaab96812011-04-19 20:37:33 -07001049
1050static int android_init_functions(struct android_usb_function **functions,
1051 struct usb_composite_dev *cdev)
1052{
1053 struct android_dev *dev = _android_dev;
1054 struct android_usb_function *f;
1055 struct device_attribute **attrs;
1056 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301057 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001058 int index = 1; /* index 0 is for android0 device */
Benoit Gobyaab96812011-04-19 20:37:33 -07001059
1060 for (; (f = *functions++); index++) {
1061 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001062 if (!f->dev_name) {
1063 err = -ENOMEM;
1064 goto err_out;
1065 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001066 f->dev = device_create(android_class, dev->dev,
1067 MKDEV(0, index), f, f->dev_name);
1068 if (IS_ERR(f->dev)) {
1069 pr_err("%s: Failed to create dev %s", __func__,
1070 f->dev_name);
1071 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001072 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001073 goto err_create;
1074 }
1075
1076 if (f->init) {
1077 err = f->init(f, cdev);
1078 if (err) {
1079 pr_err("%s: Failed to init %s", __func__,
1080 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001081 goto err_init;
Benoit Gobyaab96812011-04-19 20:37:33 -07001082 }
1083 }
1084
1085 attrs = f->attributes;
1086 if (attrs) {
1087 while ((attr = *attrs++) && !err)
1088 err = device_create_file(f->dev, attr);
1089 }
1090 if (err) {
1091 pr_err("%s: Failed to create function %s attributes",
1092 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001093 goto err_attrs;
Benoit Gobyaab96812011-04-19 20:37:33 -07001094 }
1095 }
1096 return 0;
1097
Lena Salmand092f2d2012-03-12 17:27:24 +02001098err_attrs:
1099 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1100 device_remove_file(f->dev, attr);
1101 if (f->cleanup)
1102 f->cleanup(f);
1103err_init:
Benoit Gobyaab96812011-04-19 20:37:33 -07001104 device_destroy(android_class, f->dev->devt);
1105err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001106 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001107 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001108err_out:
1109 android_cleanup_functions(dev->functions);
Benoit Gobyaab96812011-04-19 20:37:33 -07001110 return err;
1111}
1112
Benoit Gobyaab96812011-04-19 20:37:33 -07001113static int
1114android_bind_enabled_functions(struct android_dev *dev,
1115 struct usb_configuration *c)
1116{
1117 struct android_usb_function *f;
1118 int ret;
1119
1120 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1121 ret = f->bind_config(f, c);
1122 if (ret) {
1123 pr_err("%s: %s failed", __func__, f->name);
1124 return ret;
1125 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301126 }
1127 return 0;
1128}
1129
Benoit Gobyaab96812011-04-19 20:37:33 -07001130static void
1131android_unbind_enabled_functions(struct android_dev *dev,
1132 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301133{
Benoit Gobyaab96812011-04-19 20:37:33 -07001134 struct android_usb_function *f;
1135
1136 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1137 if (f->unbind_config)
1138 f->unbind_config(f, c);
1139 }
1140}
1141
1142static int android_enable_function(struct android_dev *dev, char *name)
1143{
1144 struct android_usb_function **functions = dev->functions;
1145 struct android_usb_function *f;
1146 while ((f = *functions++)) {
1147 if (!strcmp(name, f->name)) {
1148 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301149 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001150 }
1151 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001152 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001153}
1154
Benoit Gobyaab96812011-04-19 20:37:33 -07001155/*-------------------------------------------------------------------------*/
1156/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301157
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301158static ssize_t remote_wakeup_show(struct device *pdev,
1159 struct device_attribute *attr, char *buf)
1160{
1161 return snprintf(buf, PAGE_SIZE, "%d\n",
1162 !!(android_config_driver.bmAttributes &
1163 USB_CONFIG_ATT_WAKEUP));
1164}
1165
1166static ssize_t remote_wakeup_store(struct device *pdev,
1167 struct device_attribute *attr, const char *buff, size_t size)
1168{
1169 int enable = 0;
1170
1171 sscanf(buff, "%d", &enable);
1172
1173 pr_debug("android_usb: %s remote wakeup\n",
1174 enable ? "enabling" : "disabling");
1175
1176 if (enable)
1177 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1178 else
1179 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1180
1181 return size;
1182}
1183
Benoit Gobyaab96812011-04-19 20:37:33 -07001184static ssize_t
1185functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1186{
1187 struct android_dev *dev = dev_get_drvdata(pdev);
1188 struct android_usb_function *f;
1189 char *buff = buf;
1190
Benoit Gobydc1b6342011-12-09 18:05:00 -08001191 mutex_lock(&dev->mutex);
1192
Benoit Gobyaab96812011-04-19 20:37:33 -07001193 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301194 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001195
1196 mutex_unlock(&dev->mutex);
1197
Benoit Gobyaab96812011-04-19 20:37:33 -07001198 if (buff != buf)
1199 *(buff-1) = '\n';
1200 return buff - buf;
1201}
1202
1203static ssize_t
1204functions_store(struct device *pdev, struct device_attribute *attr,
1205 const char *buff, size_t size)
1206{
1207 struct android_dev *dev = dev_get_drvdata(pdev);
1208 char *name;
1209 char buf[256], *b;
1210 int err;
1211
Benoit Gobydc1b6342011-12-09 18:05:00 -08001212 mutex_lock(&dev->mutex);
1213
1214 if (dev->enabled) {
1215 mutex_unlock(&dev->mutex);
1216 return -EBUSY;
1217 }
1218
Benoit Gobyaab96812011-04-19 20:37:33 -07001219 INIT_LIST_HEAD(&dev->enabled_functions);
1220
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301221 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001222 b = strim(buf);
1223
1224 while (b) {
1225 name = strsep(&b, ",");
1226 if (name) {
1227 err = android_enable_function(dev, name);
1228 if (err)
1229 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301230 }
1231 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001232
Benoit Gobydc1b6342011-12-09 18:05:00 -08001233 mutex_unlock(&dev->mutex);
1234
Benoit Gobyaab96812011-04-19 20:37:33 -07001235 return size;
1236}
1237
1238static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1239 char *buf)
1240{
1241 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301242 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001243}
1244
1245static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1246 const char *buff, size_t size)
1247{
1248 struct android_dev *dev = dev_get_drvdata(pdev);
1249 struct usb_composite_dev *cdev = dev->cdev;
1250 int enabled = 0;
1251
Benoit Gobydc1b6342011-12-09 18:05:00 -08001252 mutex_lock(&dev->mutex);
1253
Benoit Gobyaab96812011-04-19 20:37:33 -07001254 sscanf(buff, "%d", &enabled);
1255 if (enabled && !dev->enabled) {
1256 /* update values in composite driver's copy of device descriptor */
1257 cdev->desc.idVendor = device_desc.idVendor;
1258 cdev->desc.idProduct = device_desc.idProduct;
1259 cdev->desc.bcdDevice = device_desc.bcdDevice;
1260 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1261 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1262 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301263 if (usb_add_config(cdev, &android_config_driver,
1264 android_bind_config))
1265 return size;
1266
Benoit Gobyaab96812011-04-19 20:37:33 -07001267 usb_gadget_connect(cdev->gadget);
1268 dev->enabled = true;
1269 } else if (!enabled && dev->enabled) {
1270 usb_gadget_disconnect(cdev->gadget);
Benoit Gobye0de0a52011-11-29 13:49:27 -08001271 /* Cancel pending control requests */
1272 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Benoit Gobyaab96812011-04-19 20:37:33 -07001273 usb_remove_config(cdev, &android_config_driver);
1274 dev->enabled = false;
1275 } else {
1276 pr_err("android_usb: already %s\n",
1277 dev->enabled ? "enabled" : "disabled");
1278 }
Benoit Gobydc1b6342011-12-09 18:05:00 -08001279
1280 mutex_unlock(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001281 return size;
1282}
1283
1284static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1285 char *buf)
1286{
1287 struct android_dev *dev = dev_get_drvdata(pdev);
1288 struct usb_composite_dev *cdev = dev->cdev;
1289 char *state = "DISCONNECTED";
1290 unsigned long flags;
1291
1292 if (!cdev)
1293 goto out;
1294
1295 spin_lock_irqsave(&cdev->lock, flags);
1296 if (cdev->config)
1297 state = "CONFIGURED";
1298 else if (dev->connected)
1299 state = "CONNECTED";
1300 spin_unlock_irqrestore(&cdev->lock, flags);
1301out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301302 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001303}
1304
1305#define DESCRIPTOR_ATTR(field, format_string) \
1306static ssize_t \
1307field ## _show(struct device *dev, struct device_attribute *attr, \
1308 char *buf) \
1309{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301310 return snprintf(buf, PAGE_SIZE, \
1311 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001312} \
1313static ssize_t \
1314field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001315 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001316{ \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001317 int value; \
Benoit Gobyaab96812011-04-19 20:37:33 -07001318 if (sscanf(buf, format_string, &value) == 1) { \
1319 device_desc.field = value; \
1320 return size; \
1321 } \
1322 return -1; \
1323} \
1324static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1325
1326#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1327static ssize_t \
1328field ## _show(struct device *dev, struct device_attribute *attr, \
1329 char *buf) \
1330{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301331 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001332} \
1333static ssize_t \
1334field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001335 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001336{ \
1337 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301338 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001339 return size; \
1340 } \
1341 return -1; \
1342} \
1343static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1344
1345
1346DESCRIPTOR_ATTR(idVendor, "%04x\n")
1347DESCRIPTOR_ATTR(idProduct, "%04x\n")
1348DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1349DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1350DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1351DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1352DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1353DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1354DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1355
1356static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1357static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1358static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301359static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1360 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001361
1362static struct device_attribute *android_usb_attributes[] = {
1363 &dev_attr_idVendor,
1364 &dev_attr_idProduct,
1365 &dev_attr_bcdDevice,
1366 &dev_attr_bDeviceClass,
1367 &dev_attr_bDeviceSubClass,
1368 &dev_attr_bDeviceProtocol,
1369 &dev_attr_iManufacturer,
1370 &dev_attr_iProduct,
1371 &dev_attr_iSerial,
1372 &dev_attr_functions,
1373 &dev_attr_enable,
1374 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301375 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001376 NULL
1377};
1378
1379/*-------------------------------------------------------------------------*/
1380/* Composite driver */
1381
1382static int android_bind_config(struct usb_configuration *c)
1383{
1384 struct android_dev *dev = _android_dev;
1385 int ret = 0;
1386
1387 ret = android_bind_enabled_functions(dev, c);
1388 if (ret)
1389 return ret;
1390
1391 return 0;
1392}
1393
1394static void android_unbind_config(struct usb_configuration *c)
1395{
1396 struct android_dev *dev = _android_dev;
1397
1398 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301399}
1400
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001401static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001402{
1403 struct android_dev *dev = _android_dev;
1404 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001405 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001406
Benoit Gobyaab96812011-04-19 20:37:33 -07001407 usb_gadget_disconnect(gadget);
1408
1409 ret = android_init_functions(dev->functions, cdev);
1410 if (ret)
1411 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001412
1413 /* Allocate string descriptor numbers ... note that string
1414 * contents can be overridden by the composite_dev glue.
1415 */
1416 id = usb_string_id(cdev);
1417 if (id < 0)
1418 return id;
1419 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1420 device_desc.iManufacturer = id;
1421
1422 id = usb_string_id(cdev);
1423 if (id < 0)
1424 return id;
1425 strings_dev[STRING_PRODUCT_IDX].id = id;
1426 device_desc.iProduct = id;
1427
Benoit Gobyaab96812011-04-19 20:37:33 -07001428 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301429 strlcpy(manufacturer_string, "Android",
1430 sizeof(manufacturer_string) - 1);
1431 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1432 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001433
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001434 id = usb_string_id(cdev);
1435 if (id < 0)
1436 return id;
1437 strings_dev[STRING_SERIAL_IDX].id = id;
1438 device_desc.iSerialNumber = id;
1439
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301440 if (gadget_is_otg(cdev->gadget))
1441 android_config_driver.descriptors = otg_desc;
1442
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001443 gcnum = usb_gadget_controller_number(gadget);
1444 if (gcnum >= 0)
1445 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1446 else {
1447 /* gadget zero is so simple (for now, no altsettings) that
1448 * it SHOULD NOT have problems with bulk-capable hardware.
1449 * so just warn about unrcognized controllers -- don't panic.
1450 *
1451 * things like configuration and altsetting numbering
1452 * can need hardware-specific attention though.
1453 */
1454 pr_warning("%s: controller '%s' not recognized\n",
1455 longname, gadget->name);
1456 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1457 }
1458
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001459 dev->cdev = cdev;
1460
1461 return 0;
1462}
1463
Benoit Gobyaab96812011-04-19 20:37:33 -07001464static int android_usb_unbind(struct usb_composite_dev *cdev)
1465{
1466 struct android_dev *dev = _android_dev;
1467
Lena Salmand092f2d2012-03-12 17:27:24 +02001468 manufacturer_string[0] = '\0';
1469 product_string[0] = '\0';
1470 serial_string[0] = '0';
Benoit Gobyaab96812011-04-19 20:37:33 -07001471 cancel_work_sync(&dev->work);
1472 android_cleanup_functions(dev->functions);
1473 return 0;
1474}
1475
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001476static struct usb_composite_driver android_usb_driver = {
1477 .name = "android_usb",
1478 .dev = &device_desc,
1479 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001480 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001481 .max_speed = USB_SPEED_SUPER
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001482};
1483
Benoit Gobyaab96812011-04-19 20:37:33 -07001484static int
1485android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1486{
1487 struct android_dev *dev = _android_dev;
1488 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1489 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001490 struct android_usb_function *f;
1491 int value = -EOPNOTSUPP;
1492 unsigned long flags;
1493
1494 req->zero = 0;
1495 req->complete = composite_setup_complete;
1496 req->length = 0;
1497 gadget->ep0->driver_data = cdev;
1498
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001499 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001500 if (f->ctrlrequest) {
1501 value = f->ctrlrequest(f, cdev, c);
1502 if (value >= 0)
1503 break;
1504 }
1505 }
1506
Mike Lockwood686d33a2011-09-07 09:55:12 -07001507 /* Special case the accessory function.
1508 * It needs to handle control requests before it is enabled.
1509 */
1510 if (value < 0)
1511 value = acc_ctrlrequest(cdev, c);
1512
Benoit Gobyaab96812011-04-19 20:37:33 -07001513 if (value < 0)
1514 value = composite_setup(gadget, c);
1515
1516 spin_lock_irqsave(&cdev->lock, flags);
1517 if (!dev->connected) {
1518 dev->connected = 1;
1519 schedule_work(&dev->work);
1520 }
1521 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1522 schedule_work(&dev->work);
1523 }
1524 spin_unlock_irqrestore(&cdev->lock, flags);
1525
1526 return value;
1527}
1528
1529static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001530{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301531 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001532 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1533 unsigned long flags;
1534
1535 composite_disconnect(gadget);
1536
1537 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001538 dev->connected = 0;
1539 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001540 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301541}
1542
Benoit Gobyaab96812011-04-19 20:37:33 -07001543static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001544{
Benoit Gobyaab96812011-04-19 20:37:33 -07001545 struct device_attribute **attrs = android_usb_attributes;
1546 struct device_attribute *attr;
1547 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001548
Benoit Gobyaab96812011-04-19 20:37:33 -07001549 dev->dev = device_create(android_class, NULL,
1550 MKDEV(0, 0), NULL, "android0");
1551 if (IS_ERR(dev->dev))
1552 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001553
Benoit Gobyaab96812011-04-19 20:37:33 -07001554 dev_set_drvdata(dev->dev, dev);
1555
1556 while ((attr = *attrs++)) {
1557 err = device_create_file(dev->dev, attr);
1558 if (err) {
1559 device_destroy(android_class, dev->dev->devt);
1560 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001561 }
1562 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001563 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001564}
1565
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301566static void android_destroy_device(struct android_dev *dev)
1567{
1568 struct device_attribute **attrs = android_usb_attributes;
1569 struct device_attribute *attr;
1570
1571 while ((attr = *attrs++))
1572 device_remove_file(dev->dev, attr);
1573 device_destroy(android_class, dev->dev->devt);
1574}
1575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001576static int __devinit android_probe(struct platform_device *pdev)
1577{
1578 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1579 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001580 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001581
1582 dev->pdata = pdata;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301583
Lena Salmand092f2d2012-03-12 17:27:24 +02001584 ret = usb_composite_probe(&android_usb_driver, android_bind);
1585 if (ret) {
1586 pr_err("%s(): Failed to register android "
1587 "composite driver\n", __func__);
1588 }
1589
1590 return ret;
1591}
1592
1593static int android_remove(struct platform_device *pdev)
1594{
1595 usb_composite_unregister(&android_usb_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596 return 0;
1597}
1598
1599static struct platform_driver android_platform_driver = {
1600 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001601 .probe = android_probe,
1602 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001604
1605static int __init init(void)
1606{
1607 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301608 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001609
Benoit Gobyaab96812011-04-19 20:37:33 -07001610 android_class = class_create(THIS_MODULE, "android_usb");
1611 if (IS_ERR(android_class))
1612 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001613
1614 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301615 if (!dev) {
1616 pr_err("%s(): Failed to alloc memory for android_dev\n",
1617 __func__);
1618 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001619 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301620 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001621 dev->functions = supported_functions;
1622 INIT_LIST_HEAD(&dev->enabled_functions);
1623 INIT_WORK(&dev->work, android_work);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001624 mutex_init(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001625
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301626 ret = android_create_device(dev);
1627 if (ret) {
1628 pr_err("%s(): android_create_device failed\n", __func__);
1629 goto err_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001630 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001631 _android_dev = dev;
1632
Benoit Gobyaab96812011-04-19 20:37:33 -07001633 /* Override composite driver functions */
1634 composite_driver.setup = android_setup;
1635 composite_driver.disconnect = android_disconnect;
1636
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301637 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301638 if (ret) {
1639 pr_err("%s(): Failed to register android"
1640 "platform driver\n", __func__);
1641 goto err_probe;
1642 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001643
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301644 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301646err_probe:
1647 android_destroy_device(dev);
1648err_dev:
1649 kfree(dev);
1650 class_destroy(android_class);
1651 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001652}
1653module_init(init);
1654
1655static void __exit cleanup(void)
1656{
Lena Salmand092f2d2012-03-12 17:27:24 +02001657 platform_driver_unregister(&android_platform_driver);
1658 android_destroy_device(_android_dev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001659 kfree(_android_dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001660 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001661 _android_dev = NULL;
1662}
1663module_exit(cleanup);