blob: d74959e0926cf9d2bc7e327cbd010656ca23b0df [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05305 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05006 * Author: Mike Lockwood <lockwood@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19/* #define DEBUG */
20/* #define VERBOSE_DEBUG */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/fs.h>
25
26#include <linux/delay.h>
27#include <linux/kernel.h>
28#include <linux/utsname.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050029#include <linux/platform_device.h>
Ofir Cohen94213a72012-05-03 14:26:32 +030030#include <linux/pm_qos_params.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050031
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050032#include <linux/usb/ch9.h>
33#include <linux/usb/composite.h>
34#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <linux/usb/android.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050036
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050037#include "gadget_chips.h"
38
39/*
40 * Kbuild is not very cooperative with respect to linking separately
41 * compiled library objects into one module. So for now we won't use
42 * separate compilation ... ensuring init/exit sections work to shrink
43 * the runtime footprint, and giving us at least some parts of what
44 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
45 */
46#include "usbstring.c"
47#include "config.c"
48#include "epautoconf.c"
49#include "composite.c"
50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053052#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053053#include "f_rmnet_sdio.c"
54#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053055#include "f_rmnet.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070056#include "f_mass_storage.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053057#include "u_serial.c"
58#include "u_sdio.c"
59#include "u_smd.c"
60#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053061#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080062#include "u_ctrl_hsic.c"
63#include "u_data_hsic.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053064#include "f_serial.c"
Anji jonnala92be1b42011-12-19 09:44:41 +053065#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070066#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053067#include "f_ccid.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070068#include "f_mtp.c"
69#include "f_accessory.c"
70#define USB_ETH_RNDIS y
71#include "f_rndis.c"
72#include "rndis.c"
73#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030074#include "u_bam_data.c"
75#include "f_mbim.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070076
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050077MODULE_AUTHOR("Mike Lockwood");
78MODULE_DESCRIPTION("Android Composite USB Driver");
79MODULE_LICENSE("GPL");
80MODULE_VERSION("1.0");
81
82static const char longname[] = "Gadget Android";
83
Benoit Gobyaab96812011-04-19 20:37:33 -070084/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050085#define VENDOR_ID 0x18D1
86#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050087
Benoit Gobyaab96812011-04-19 20:37:33 -070088struct android_usb_function {
89 char *name;
90 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050091
Benoit Gobyaab96812011-04-19 20:37:33 -070092 struct device *dev;
93 char *dev_name;
94 struct device_attribute **attributes;
95
96 /* for android_dev.enabled_functions */
97 struct list_head enabled_list;
98
99 /* Optional: initialization during gadget bind */
100 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
101 /* Optional: cleanup during gadget unbind */
102 void (*cleanup)(struct android_usb_function *);
103
104 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
105
106 /* Optional: called when the configuration is removed */
107 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700108 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700109 int (*ctrlrequest)(struct android_usb_function *,
110 struct usb_composite_dev *,
111 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500112};
113
Benoit Gobyaab96812011-04-19 20:37:33 -0700114struct android_dev {
115 struct android_usb_function **functions;
116 struct list_head enabled_functions;
117 struct usb_composite_dev *cdev;
118 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700120
121 bool enabled;
Benoit Gobydc1b6342011-12-09 18:05:00 -0800122 struct mutex mutex;
Benoit Gobyaab96812011-04-19 20:37:33 -0700123 bool connected;
124 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300125 char pm_qos[5];
126 struct pm_qos_request_list pm_qos_req_dma;
Benoit Gobyaab96812011-04-19 20:37:33 -0700127 struct work_struct work;
128};
129
130static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500131static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700132static int android_bind_config(struct usb_configuration *c);
133static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500134
135/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500136#define STRING_MANUFACTURER_IDX 0
137#define STRING_PRODUCT_IDX 1
138#define STRING_SERIAL_IDX 2
139
Benoit Gobyaab96812011-04-19 20:37:33 -0700140static char manufacturer_string[256];
141static char product_string[256];
142static char serial_string[256];
143
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500144/* String Table */
145static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700146 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
147 [STRING_PRODUCT_IDX].s = product_string,
148 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500149 { } /* end of list */
150};
151
152static struct usb_gadget_strings stringtab_dev = {
153 .language = 0x0409, /* en-us */
154 .strings = strings_dev,
155};
156
157static struct usb_gadget_strings *dev_strings[] = {
158 &stringtab_dev,
159 NULL,
160};
161
162static struct usb_device_descriptor device_desc = {
163 .bLength = sizeof(device_desc),
164 .bDescriptorType = USB_DT_DEVICE,
165 .bcdUSB = __constant_cpu_to_le16(0x0200),
166 .bDeviceClass = USB_CLASS_PER_INTERFACE,
167 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
168 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
169 .bcdDevice = __constant_cpu_to_le16(0xffff),
170 .bNumConfigurations = 1,
171};
172
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530173static struct usb_otg_descriptor otg_descriptor = {
174 .bLength = sizeof otg_descriptor,
175 .bDescriptorType = USB_DT_OTG,
176 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
177 .bcdOTG = __constant_cpu_to_le16(0x0200),
178};
179
180static const struct usb_descriptor_header *otg_desc[] = {
181 (struct usb_descriptor_header *) &otg_descriptor,
182 NULL,
183};
184
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500185static struct usb_configuration android_config_driver = {
186 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700187 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500188 .bConfigurationValue = 1,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500189};
190
Manu Gautama2b54142012-04-03 14:34:32 +0530191enum android_device_state {
192 USB_DISCONNECTED,
193 USB_CONNECTED,
194 USB_CONFIGURED,
195};
196
Ofir Cohen94213a72012-05-03 14:26:32 +0300197static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
198{
199 struct android_usb_platform_data *pdata = dev->pdata;
200 u32 swfi_latency = 0;
201 static int last_vote = -1;
202
203 if (!pdata || vote == last_vote)
204 return;
205
206 swfi_latency = pdata->swfi_latency + 1;
207 if (vote)
208 pm_qos_update_request(&dev->pm_qos_req_dma,
209 swfi_latency);
210 else
211 pm_qos_update_request(&dev->pm_qos_req_dma,
212 PM_QOS_DEFAULT_VALUE);
213 last_vote = vote;
214}
215
Benoit Gobyaab96812011-04-19 20:37:33 -0700216static void android_work(struct work_struct *data)
217{
218 struct android_dev *dev = container_of(data, struct android_dev, work);
219 struct usb_composite_dev *cdev = dev->cdev;
220 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
221 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
222 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700223 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530224 static enum android_device_state last_uevent, next_state;
Benoit Gobyaab96812011-04-19 20:37:33 -0700225 unsigned long flags;
226
227 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530228 if (cdev->config) {
Dima Zavinfc753492011-09-14 11:52:45 -0700229 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530230 next_state = USB_CONFIGURED;
231 } else if (dev->connected != dev->sw_connected) {
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700232 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530233 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300234 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
235 android_pm_qos_update_latency(dev, 1);
236 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
237 android_pm_qos_update_latency(dev, 0);
Manu Gautama2b54142012-04-03 14:34:32 +0530238 }
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700239 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700240 spin_unlock_irqrestore(&cdev->lock, flags);
241
242 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530243 /*
244 * Some userspace modules, e.g. MTP, work correctly only if
245 * CONFIGURED uevent is preceded by DISCONNECT uevent.
246 * Check if we missed sending out a DISCONNECT uevent. This can
247 * happen if host PC resets and configures device really quick.
248 */
249 if (((uevent_envp == connected) &&
250 (last_uevent != USB_DISCONNECTED)) ||
251 ((uevent_envp == configured) &&
252 (last_uevent == USB_CONFIGURED))) {
253 pr_info("%s: sent missed DISCONNECT event\n", __func__);
254 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
255 disconnected);
256 msleep(20);
257 }
258 /*
259 * Before sending out CONFIGURED uevent give function drivers
260 * a chance to wakeup userspace threads and notify disconnect
261 */
262 if (uevent_envp == configured)
263 msleep(50);
264
Dima Zavinfc753492011-09-14 11:52:45 -0700265 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530266 last_uevent = next_state;
Dima Zavinfc753492011-09-14 11:52:45 -0700267 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700268 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700269 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
270 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700271 }
272}
273
274
275/*-------------------------------------------------------------------------*/
276/* Supported functions initialization */
277
Manu Gautam8e0719b2011-09-26 14:47:55 +0530278/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530279static int rmnet_smd_function_bind_config(struct android_usb_function *f,
280 struct usb_configuration *c)
281{
282 return rmnet_smd_bind_config(c);
283}
284
285static struct android_usb_function rmnet_smd_function = {
286 .name = "rmnet_smd",
287 .bind_config = rmnet_smd_function_bind_config,
288};
289
Manu Gautam8e0719b2011-09-26 14:47:55 +0530290/* RMNET_SDIO */
291static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
292 struct usb_configuration *c)
293{
294 return rmnet_sdio_function_add(c);
295}
296
297static struct android_usb_function rmnet_sdio_function = {
298 .name = "rmnet_sdio",
299 .bind_config = rmnet_sdio_function_bind_config,
300};
301
302/* RMNET_SMD_SDIO */
303static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
304 struct usb_composite_dev *cdev)
305{
306 return rmnet_smd_sdio_init();
307}
308
309static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
310{
311 rmnet_smd_sdio_cleanup();
312}
313
314static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
315 struct usb_configuration *c)
316{
317 return rmnet_smd_sdio_function_add(c);
318}
319
320static struct device_attribute *rmnet_smd_sdio_attributes[] = {
321 &dev_attr_transport, NULL };
322
323static struct android_usb_function rmnet_smd_sdio_function = {
324 .name = "rmnet_smd_sdio",
325 .init = rmnet_smd_sdio_function_init,
326 .cleanup = rmnet_smd_sdio_function_cleanup,
327 .bind_config = rmnet_smd_sdio_bind_config,
328 .attributes = rmnet_smd_sdio_attributes,
329};
330
Hemant Kumar1b820d52011-11-03 15:08:28 -0700331/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
332#define MAX_XPORT_STR_LEN 50
333static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530334
Manu Gautame3e897c2011-09-12 17:18:46 +0530335static void rmnet_function_cleanup(struct android_usb_function *f)
336{
337 frmnet_cleanup();
338}
339
Manu Gautam2b0234a2011-09-07 16:47:52 +0530340static int rmnet_function_bind_config(struct android_usb_function *f,
341 struct usb_configuration *c)
342{
343 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700344 int err = 0;
345 char *ctrl_name;
346 char *data_name;
347 char buf[MAX_XPORT_STR_LEN], *b;
348 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530349
Hemant Kumar1b820d52011-11-03 15:08:28 -0700350 if (!rmnet_initialized) {
351 rmnet_initialized = 1;
352 strlcpy(buf, rmnet_transports, sizeof(buf));
353 b = strim(buf);
354 while (b) {
355 ctrl_name = strsep(&b, ",");
356 data_name = strsep(&b, ",");
357 if (ctrl_name && data_name) {
358 err = frmnet_init_port(ctrl_name, data_name);
359 if (err) {
360 pr_err("rmnet: Cannot open ctrl port:"
361 "'%s' data port:'%s'\n",
362 ctrl_name, data_name);
363 goto out;
364 }
365 ports++;
366 }
367 }
368
369 err = rmnet_gport_setup();
370 if (err) {
371 pr_err("rmnet: Cannot setup transports");
372 goto out;
373 }
374 }
375
376 for (i = 0; i < ports; i++) {
377 err = frmnet_bind_config(c, i);
378 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530379 pr_err("Could not bind rmnet%u config\n", i);
380 break;
381 }
382 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700383out:
384 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530385}
386
Hemant Kumar1b820d52011-11-03 15:08:28 -0700387static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530388 struct device_attribute *attr, char *buf)
389{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700390 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530391}
392
Hemant Kumar1b820d52011-11-03 15:08:28 -0700393static ssize_t rmnet_transports_store(
394 struct device *device, struct device_attribute *attr,
395 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530396{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700397 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530398
Manu Gautam2b0234a2011-09-07 16:47:52 +0530399 return size;
400}
401
Hemant Kumar1b820d52011-11-03 15:08:28 -0700402static struct device_attribute dev_attr_rmnet_transports =
403 __ATTR(transports, S_IRUGO | S_IWUSR,
404 rmnet_transports_show,
405 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530406static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700407 &dev_attr_rmnet_transports,
408 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530409
410static struct android_usb_function rmnet_function = {
411 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530412 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530413 .bind_config = rmnet_function_bind_config,
414 .attributes = rmnet_function_attributes,
415};
416
Anna Perela8c991d2012-04-09 16:44:46 +0300417
418/* MBIM - used with BAM */
419#define MAX_MBIM_INSTANCES 1
420
421static int mbim_function_init(struct android_usb_function *f,
422 struct usb_composite_dev *cdev)
423{
424 return mbim_init(MAX_MBIM_INSTANCES);
425}
426
427static void mbim_function_cleanup(struct android_usb_function *f)
428{
429 fmbim_cleanup();
430}
431
432static int mbim_function_bind_config(struct android_usb_function *f,
433 struct usb_configuration *c)
434{
435 return mbim_bind_config(c, 0);
436}
437
438static struct android_usb_function mbim_function = {
439 .name = "usb_mbim",
440 .cleanup = mbim_function_cleanup,
441 .bind_config = mbim_function_bind_config,
442 .init = mbim_function_init,
443};
444
445
Manu Gautam8e0719b2011-09-26 14:47:55 +0530446/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530447static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448static ssize_t clients_store(
449 struct device *device, struct device_attribute *attr,
450 const char *buff, size_t size)
451{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530452 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 return size;
455}
456
457static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
458static struct device_attribute *diag_function_attributes[] =
459 { &dev_attr_clients, NULL };
460
461static int diag_function_init(struct android_usb_function *f,
462 struct usb_composite_dev *cdev)
463{
464 return diag_setup();
465}
466
467static void diag_function_cleanup(struct android_usb_function *f)
468{
469 diag_cleanup();
470}
471
472static int diag_function_bind_config(struct android_usb_function *f,
473 struct usb_configuration *c)
474{
475 char *name;
476 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530477 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800478 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530480 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 b = strim(buf);
482
483 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800484 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530486 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700487 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530488 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530489
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530491 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 if (err)
493 pr_err("diag: Cannot open channel '%s'", name);
494 }
495 }
496
497 return err;
498}
499
500static struct android_usb_function diag_function = {
501 .name = "diag",
502 .init = diag_function_init,
503 .cleanup = diag_function_cleanup,
504 .bind_config = diag_function_bind_config,
505 .attributes = diag_function_attributes,
506};
507
Manu Gautam8e0719b2011-09-26 14:47:55 +0530508/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530509static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530510static ssize_t serial_transports_store(
511 struct device *device, struct device_attribute *attr,
512 const char *buff, size_t size)
513{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530514 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530515
516 return size;
517}
518
519static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
520static struct device_attribute *serial_function_attributes[] =
521 { &dev_attr_transports, NULL };
522
523static void serial_function_cleanup(struct android_usb_function *f)
524{
525 gserial_cleanup();
526}
527
528static int serial_function_bind_config(struct android_usb_function *f,
529 struct usb_configuration *c)
530{
531 char *name;
532 char buf[32], *b;
533 int err = -1, i;
534 static int serial_initialized = 0, ports = 0;
535
536 if (serial_initialized)
537 goto bind_config;
538
539 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530540 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530541 b = strim(buf);
542
543 while (b) {
544 name = strsep(&b, ",");
545
546 if (name) {
547 err = gserial_init_port(ports, name);
548 if (err) {
549 pr_err("serial: Cannot open port '%s'", name);
550 goto out;
551 }
552 ports++;
553 }
554 }
555 err = gport_setup(c);
556 if (err) {
557 pr_err("serial: Cannot setup transports");
558 goto out;
559 }
560
561bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200562 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530563 err = gser_bind_config(c, i);
564 if (err) {
565 pr_err("serial: bind_config failed for port %d", i);
566 goto out;
567 }
568 }
569
570out:
571 return err;
572}
573
574static struct android_usb_function serial_function = {
575 .name = "serial",
576 .cleanup = serial_function_cleanup,
577 .bind_config = serial_function_bind_config,
578 .attributes = serial_function_attributes,
579};
580
Anji jonnala92be1b42011-12-19 09:44:41 +0530581/* ACM */
582static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
583static ssize_t acm_transports_store(
584 struct device *device, struct device_attribute *attr,
585 const char *buff, size_t size)
586{
587 strlcpy(acm_transports, buff, sizeof(acm_transports));
588
589 return size;
590}
591
592static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
593static struct device_attribute *acm_function_attributes[] = {
594 &dev_attr_acm_transports, NULL };
595
596static void acm_function_cleanup(struct android_usb_function *f)
597{
598 gserial_cleanup();
599}
600
601static int acm_function_bind_config(struct android_usb_function *f,
602 struct usb_configuration *c)
603{
604 char *name;
605 char buf[32], *b;
606 int err = -1, i;
607 static int acm_initialized, ports;
608
609 if (acm_initialized)
610 goto bind_config;
611
612 acm_initialized = 1;
613 strlcpy(buf, acm_transports, sizeof(buf));
614 b = strim(buf);
615
616 while (b) {
617 name = strsep(&b, ",");
618
619 if (name) {
620 err = acm_init_port(ports, name);
621 if (err) {
622 pr_err("acm: Cannot open port '%s'", name);
623 goto out;
624 }
625 ports++;
626 }
627 }
628 err = acm_port_setup(c);
629 if (err) {
630 pr_err("acm: Cannot setup transports");
631 goto out;
632 }
633
634bind_config:
635 for (i = 0; i < ports; i++) {
636 err = acm_bind_config(c, i);
637 if (err) {
638 pr_err("acm: bind_config failed for port %d", i);
639 goto out;
640 }
641 }
642
643out:
644 return err;
645}
646static struct android_usb_function acm_function = {
647 .name = "acm",
648 .cleanup = acm_function_cleanup,
649 .bind_config = acm_function_bind_config,
650 .attributes = acm_function_attributes,
651};
Manu Gautama4d993f2011-08-30 18:25:55 +0530652
Manu Gautam8e0719b2011-09-26 14:47:55 +0530653/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700654static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
655{
656 return adb_setup();
657}
658
659static void adb_function_cleanup(struct android_usb_function *f)
660{
661 adb_cleanup();
662}
663
664static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
665{
666 return adb_bind_config(c);
667}
668
669static struct android_usb_function adb_function = {
670 .name = "adb",
671 .init = adb_function_init,
672 .cleanup = adb_function_cleanup,
673 .bind_config = adb_function_bind_config,
674};
675
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530676/* CCID */
677static int ccid_function_init(struct android_usb_function *f,
678 struct usb_composite_dev *cdev)
679{
680 return ccid_setup();
681}
682
683static void ccid_function_cleanup(struct android_usb_function *f)
684{
685 ccid_cleanup();
686}
687
688static int ccid_function_bind_config(struct android_usb_function *f,
689 struct usb_configuration *c)
690{
691 return ccid_bind_config(c);
692}
693
694static struct android_usb_function ccid_function = {
695 .name = "ccid",
696 .init = ccid_function_init,
697 .cleanup = ccid_function_cleanup,
698 .bind_config = ccid_function_bind_config,
699};
700
Benoit Gobyaab96812011-04-19 20:37:33 -0700701static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
702{
703 return mtp_setup();
704}
705
706static void mtp_function_cleanup(struct android_usb_function *f)
707{
708 mtp_cleanup();
709}
710
711static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
712{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400713 return mtp_bind_config(c, false);
714}
715
716static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
717{
718 /* nothing to do - initialization is handled by mtp_function_init */
719 return 0;
720}
721
722static void ptp_function_cleanup(struct android_usb_function *f)
723{
724 /* nothing to do - cleanup is handled by mtp_function_cleanup */
725}
726
727static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
728{
729 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700730}
731
732static int mtp_function_ctrlrequest(struct android_usb_function *f,
733 struct usb_composite_dev *cdev,
734 const struct usb_ctrlrequest *c)
735{
736 return mtp_ctrlrequest(cdev, c);
737}
738
739static struct android_usb_function mtp_function = {
740 .name = "mtp",
741 .init = mtp_function_init,
742 .cleanup = mtp_function_cleanup,
743 .bind_config = mtp_function_bind_config,
744 .ctrlrequest = mtp_function_ctrlrequest,
745};
746
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400747/* PTP function is same as MTP with slightly different interface descriptor */
748static struct android_usb_function ptp_function = {
749 .name = "ptp",
750 .init = ptp_function_init,
751 .cleanup = ptp_function_cleanup,
752 .bind_config = ptp_function_bind_config,
753};
754
Benoit Gobyaab96812011-04-19 20:37:33 -0700755
756struct rndis_function_config {
757 u8 ethaddr[ETH_ALEN];
758 u32 vendorID;
759 char manufacturer[256];
760 bool wceis;
761};
762
763static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
764{
765 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
766 if (!f->config)
767 return -ENOMEM;
768 return 0;
769}
770
771static void rndis_function_cleanup(struct android_usb_function *f)
772{
773 kfree(f->config);
774 f->config = NULL;
775}
776
777static int rndis_function_bind_config(struct android_usb_function *f,
778 struct usb_configuration *c)
779{
Manu Gautamf4741132011-11-25 09:08:53 +0530780 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700781 struct rndis_function_config *rndis = f->config;
782
783 if (!rndis) {
784 pr_err("%s: rndis_pdata\n", __func__);
785 return -1;
786 }
787
788 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
789 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
790 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
791
Manu Gautamf4741132011-11-25 09:08:53 +0530792 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
793 if (ret) {
794 pr_err("%s: gether_setup failed\n", __func__);
795 return ret;
796 }
797
Benoit Gobyaab96812011-04-19 20:37:33 -0700798 if (rndis->wceis) {
799 /* "Wireless" RNDIS; auto-detected by Windows */
800 rndis_iad_descriptor.bFunctionClass =
801 USB_CLASS_WIRELESS_CONTROLLER;
802 rndis_iad_descriptor.bFunctionSubClass = 0x01;
803 rndis_iad_descriptor.bFunctionProtocol = 0x03;
804 rndis_control_intf.bInterfaceClass =
805 USB_CLASS_WIRELESS_CONTROLLER;
806 rndis_control_intf.bInterfaceSubClass = 0x01;
807 rndis_control_intf.bInterfaceProtocol = 0x03;
808 }
809
810 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
811 rndis->manufacturer);
812}
813
814static void rndis_function_unbind_config(struct android_usb_function *f,
815 struct usb_configuration *c)
816{
Manu Gautamf4741132011-11-25 09:08:53 +0530817 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700818}
819
820static ssize_t rndis_manufacturer_show(struct device *dev,
821 struct device_attribute *attr, char *buf)
822{
823 struct android_usb_function *f = dev_get_drvdata(dev);
824 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530825 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700826}
827
828static ssize_t rndis_manufacturer_store(struct device *dev,
829 struct device_attribute *attr, const char *buf, size_t size)
830{
831 struct android_usb_function *f = dev_get_drvdata(dev);
832 struct rndis_function_config *config = f->config;
833
834 if (size >= sizeof(config->manufacturer))
835 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530836 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700837 return size;
838 return -1;
839}
840
841static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
842 rndis_manufacturer_store);
843
844static ssize_t rndis_wceis_show(struct device *dev,
845 struct device_attribute *attr, char *buf)
846{
847 struct android_usb_function *f = dev_get_drvdata(dev);
848 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530849 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700850}
851
852static ssize_t rndis_wceis_store(struct device *dev,
853 struct device_attribute *attr, const char *buf, size_t size)
854{
855 struct android_usb_function *f = dev_get_drvdata(dev);
856 struct rndis_function_config *config = f->config;
857 int value;
858
859 if (sscanf(buf, "%d", &value) == 1) {
860 config->wceis = value;
861 return size;
862 }
863 return -EINVAL;
864}
865
866static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
867 rndis_wceis_store);
868
869static ssize_t rndis_ethaddr_show(struct device *dev,
870 struct device_attribute *attr, char *buf)
871{
872 struct android_usb_function *f = dev_get_drvdata(dev);
873 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530874 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700875 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
876 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
877}
878
879static ssize_t rndis_ethaddr_store(struct device *dev,
880 struct device_attribute *attr, const char *buf, size_t size)
881{
882 struct android_usb_function *f = dev_get_drvdata(dev);
883 struct rndis_function_config *rndis = f->config;
884
885 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
886 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
887 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
888 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
889 return size;
890 return -EINVAL;
891}
892
893static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
894 rndis_ethaddr_store);
895
896static ssize_t rndis_vendorID_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
898{
899 struct android_usb_function *f = dev_get_drvdata(dev);
900 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530901 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700902}
903
904static ssize_t rndis_vendorID_store(struct device *dev,
905 struct device_attribute *attr, const char *buf, size_t size)
906{
907 struct android_usb_function *f = dev_get_drvdata(dev);
908 struct rndis_function_config *config = f->config;
909 int value;
910
911 if (sscanf(buf, "%04x", &value) == 1) {
912 config->vendorID = value;
913 return size;
914 }
915 return -EINVAL;
916}
917
918static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
919 rndis_vendorID_store);
920
921static struct device_attribute *rndis_function_attributes[] = {
922 &dev_attr_manufacturer,
923 &dev_attr_wceis,
924 &dev_attr_ethaddr,
925 &dev_attr_vendorID,
926 NULL
927};
928
929static struct android_usb_function rndis_function = {
930 .name = "rndis",
931 .init = rndis_function_init,
932 .cleanup = rndis_function_cleanup,
933 .bind_config = rndis_function_bind_config,
934 .unbind_config = rndis_function_unbind_config,
935 .attributes = rndis_function_attributes,
936};
937
938
939struct mass_storage_function_config {
940 struct fsg_config fsg;
941 struct fsg_common *common;
942};
943
944static int mass_storage_function_init(struct android_usb_function *f,
945 struct usb_composite_dev *cdev)
946{
947 struct mass_storage_function_config *config;
948 struct fsg_common *common;
949 int err;
950
951 config = kzalloc(sizeof(struct mass_storage_function_config),
952 GFP_KERNEL);
953 if (!config)
954 return -ENOMEM;
955
956 config->fsg.nluns = 1;
957 config->fsg.luns[0].removable = 1;
958
959 common = fsg_common_init(NULL, cdev, &config->fsg);
960 if (IS_ERR(common)) {
961 kfree(config);
962 return PTR_ERR(common);
963 }
964
965 err = sysfs_create_link(&f->dev->kobj,
966 &common->luns[0].dev.kobj,
967 "lun");
968 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530969 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700970 kfree(config);
971 return err;
972 }
973
974 config->common = common;
975 f->config = config;
976 return 0;
977}
978
979static void mass_storage_function_cleanup(struct android_usb_function *f)
980{
981 kfree(f->config);
982 f->config = NULL;
983}
984
985static int mass_storage_function_bind_config(struct android_usb_function *f,
986 struct usb_configuration *c)
987{
988 struct mass_storage_function_config *config = f->config;
989 return fsg_bind_config(c->cdev, c, config->common);
990}
991
992static ssize_t mass_storage_inquiry_show(struct device *dev,
993 struct device_attribute *attr, char *buf)
994{
995 struct android_usb_function *f = dev_get_drvdata(dev);
996 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530997 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700998}
999
1000static ssize_t mass_storage_inquiry_store(struct device *dev,
1001 struct device_attribute *attr, const char *buf, size_t size)
1002{
1003 struct android_usb_function *f = dev_get_drvdata(dev);
1004 struct mass_storage_function_config *config = f->config;
1005 if (size >= sizeof(config->common->inquiry_string))
1006 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301007 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -07001008 return -EINVAL;
1009 return size;
1010}
1011
1012static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1013 mass_storage_inquiry_show,
1014 mass_storage_inquiry_store);
1015
1016static struct device_attribute *mass_storage_function_attributes[] = {
1017 &dev_attr_inquiry_string,
1018 NULL
1019};
1020
1021static struct android_usb_function mass_storage_function = {
1022 .name = "mass_storage",
1023 .init = mass_storage_function_init,
1024 .cleanup = mass_storage_function_cleanup,
1025 .bind_config = mass_storage_function_bind_config,
1026 .attributes = mass_storage_function_attributes,
1027};
1028
1029
1030static int accessory_function_init(struct android_usb_function *f,
1031 struct usb_composite_dev *cdev)
1032{
1033 return acc_setup();
1034}
1035
1036static void accessory_function_cleanup(struct android_usb_function *f)
1037{
1038 acc_cleanup();
1039}
1040
1041static int accessory_function_bind_config(struct android_usb_function *f,
1042 struct usb_configuration *c)
1043{
1044 return acc_bind_config(c);
1045}
1046
1047static int accessory_function_ctrlrequest(struct android_usb_function *f,
1048 struct usb_composite_dev *cdev,
1049 const struct usb_ctrlrequest *c)
1050{
1051 return acc_ctrlrequest(cdev, c);
1052}
1053
1054static struct android_usb_function accessory_function = {
1055 .name = "accessory",
1056 .init = accessory_function_init,
1057 .cleanup = accessory_function_cleanup,
1058 .bind_config = accessory_function_bind_config,
1059 .ctrlrequest = accessory_function_ctrlrequest,
1060};
1061
1062
1063static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001064 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301065 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301066 &rmnet_sdio_function,
1067 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301068 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301070 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001071 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301072 &ccid_function,
Anji jonnala92be1b42011-12-19 09:44:41 +05301073 &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001074 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -04001075 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -07001076 &rndis_function,
1077 &mass_storage_function,
1078 &accessory_function,
1079 NULL
1080};
1081
Lena Salmand092f2d2012-03-12 17:27:24 +02001082static void android_cleanup_functions(struct android_usb_function **functions)
1083{
1084 struct android_usb_function *f;
1085 struct device_attribute **attrs;
1086 struct device_attribute *attr;
1087
1088 while (*functions) {
1089 f = *functions++;
1090
1091 if (f->dev) {
1092 device_destroy(android_class, f->dev->devt);
1093 kfree(f->dev_name);
1094 } else
1095 continue;
1096
1097 if (f->cleanup)
1098 f->cleanup(f);
1099
1100 attrs = f->attributes;
1101 if (attrs) {
1102 while ((attr = *attrs++))
1103 device_remove_file(f->dev, attr);
1104 }
1105 }
1106}
Benoit Gobyaab96812011-04-19 20:37:33 -07001107
1108static int android_init_functions(struct android_usb_function **functions,
1109 struct usb_composite_dev *cdev)
1110{
1111 struct android_dev *dev = _android_dev;
1112 struct android_usb_function *f;
1113 struct device_attribute **attrs;
1114 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301115 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001116 int index = 1; /* index 0 is for android0 device */
Benoit Gobyaab96812011-04-19 20:37:33 -07001117
1118 for (; (f = *functions++); index++) {
1119 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001120 if (!f->dev_name) {
1121 err = -ENOMEM;
1122 goto err_out;
1123 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001124 f->dev = device_create(android_class, dev->dev,
1125 MKDEV(0, index), f, f->dev_name);
1126 if (IS_ERR(f->dev)) {
1127 pr_err("%s: Failed to create dev %s", __func__,
1128 f->dev_name);
1129 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001130 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001131 goto err_create;
1132 }
1133
1134 if (f->init) {
1135 err = f->init(f, cdev);
1136 if (err) {
1137 pr_err("%s: Failed to init %s", __func__,
1138 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001139 goto err_init;
Benoit Gobyaab96812011-04-19 20:37:33 -07001140 }
1141 }
1142
1143 attrs = f->attributes;
1144 if (attrs) {
1145 while ((attr = *attrs++) && !err)
1146 err = device_create_file(f->dev, attr);
1147 }
1148 if (err) {
1149 pr_err("%s: Failed to create function %s attributes",
1150 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001151 goto err_attrs;
Benoit Gobyaab96812011-04-19 20:37:33 -07001152 }
1153 }
1154 return 0;
1155
Lena Salmand092f2d2012-03-12 17:27:24 +02001156err_attrs:
1157 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1158 device_remove_file(f->dev, attr);
1159 if (f->cleanup)
1160 f->cleanup(f);
1161err_init:
Benoit Gobyaab96812011-04-19 20:37:33 -07001162 device_destroy(android_class, f->dev->devt);
1163err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001164 f->dev = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -07001165 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001166err_out:
1167 android_cleanup_functions(dev->functions);
Benoit Gobyaab96812011-04-19 20:37:33 -07001168 return err;
1169}
1170
Benoit Gobyaab96812011-04-19 20:37:33 -07001171static int
1172android_bind_enabled_functions(struct android_dev *dev,
1173 struct usb_configuration *c)
1174{
1175 struct android_usb_function *f;
1176 int ret;
1177
1178 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1179 ret = f->bind_config(f, c);
1180 if (ret) {
1181 pr_err("%s: %s failed", __func__, f->name);
1182 return ret;
1183 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301184 }
1185 return 0;
1186}
1187
Benoit Gobyaab96812011-04-19 20:37:33 -07001188static void
1189android_unbind_enabled_functions(struct android_dev *dev,
1190 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301191{
Benoit Gobyaab96812011-04-19 20:37:33 -07001192 struct android_usb_function *f;
1193
1194 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1195 if (f->unbind_config)
1196 f->unbind_config(f, c);
1197 }
1198}
1199
1200static int android_enable_function(struct android_dev *dev, char *name)
1201{
1202 struct android_usb_function **functions = dev->functions;
1203 struct android_usb_function *f;
1204 while ((f = *functions++)) {
1205 if (!strcmp(name, f->name)) {
1206 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301207 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001208 }
1209 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001210 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001211}
1212
Benoit Gobyaab96812011-04-19 20:37:33 -07001213/*-------------------------------------------------------------------------*/
1214/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301215
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301216static ssize_t remote_wakeup_show(struct device *pdev,
1217 struct device_attribute *attr, char *buf)
1218{
1219 return snprintf(buf, PAGE_SIZE, "%d\n",
1220 !!(android_config_driver.bmAttributes &
1221 USB_CONFIG_ATT_WAKEUP));
1222}
1223
1224static ssize_t remote_wakeup_store(struct device *pdev,
1225 struct device_attribute *attr, const char *buff, size_t size)
1226{
1227 int enable = 0;
1228
1229 sscanf(buff, "%d", &enable);
1230
1231 pr_debug("android_usb: %s remote wakeup\n",
1232 enable ? "enabling" : "disabling");
1233
1234 if (enable)
1235 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1236 else
1237 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1238
1239 return size;
1240}
1241
Benoit Gobyaab96812011-04-19 20:37:33 -07001242static ssize_t
1243functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1244{
1245 struct android_dev *dev = dev_get_drvdata(pdev);
1246 struct android_usb_function *f;
1247 char *buff = buf;
1248
Benoit Gobydc1b6342011-12-09 18:05:00 -08001249 mutex_lock(&dev->mutex);
1250
Benoit Gobyaab96812011-04-19 20:37:33 -07001251 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301252 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001253
1254 mutex_unlock(&dev->mutex);
1255
Benoit Gobyaab96812011-04-19 20:37:33 -07001256 if (buff != buf)
1257 *(buff-1) = '\n';
1258 return buff - buf;
1259}
1260
1261static ssize_t
1262functions_store(struct device *pdev, struct device_attribute *attr,
1263 const char *buff, size_t size)
1264{
1265 struct android_dev *dev = dev_get_drvdata(pdev);
1266 char *name;
1267 char buf[256], *b;
1268 int err;
1269
Benoit Gobydc1b6342011-12-09 18:05:00 -08001270 mutex_lock(&dev->mutex);
1271
1272 if (dev->enabled) {
1273 mutex_unlock(&dev->mutex);
1274 return -EBUSY;
1275 }
1276
Benoit Gobyaab96812011-04-19 20:37:33 -07001277 INIT_LIST_HEAD(&dev->enabled_functions);
1278
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301279 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001280 b = strim(buf);
1281
1282 while (b) {
1283 name = strsep(&b, ",");
1284 if (name) {
1285 err = android_enable_function(dev, name);
1286 if (err)
1287 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301288 }
1289 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001290
Benoit Gobydc1b6342011-12-09 18:05:00 -08001291 mutex_unlock(&dev->mutex);
1292
Benoit Gobyaab96812011-04-19 20:37:33 -07001293 return size;
1294}
1295
1296static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1297 char *buf)
1298{
1299 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301300 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001301}
1302
1303static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1304 const char *buff, size_t size)
1305{
1306 struct android_dev *dev = dev_get_drvdata(pdev);
1307 struct usb_composite_dev *cdev = dev->cdev;
1308 int enabled = 0;
1309
Benoit Gobydc1b6342011-12-09 18:05:00 -08001310 mutex_lock(&dev->mutex);
1311
Benoit Gobyaab96812011-04-19 20:37:33 -07001312 sscanf(buff, "%d", &enabled);
1313 if (enabled && !dev->enabled) {
1314 /* update values in composite driver's copy of device descriptor */
1315 cdev->desc.idVendor = device_desc.idVendor;
1316 cdev->desc.idProduct = device_desc.idProduct;
1317 cdev->desc.bcdDevice = device_desc.bcdDevice;
1318 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1319 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1320 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301321 if (usb_add_config(cdev, &android_config_driver,
1322 android_bind_config))
1323 return size;
1324
Benoit Gobyaab96812011-04-19 20:37:33 -07001325 usb_gadget_connect(cdev->gadget);
1326 dev->enabled = true;
1327 } else if (!enabled && dev->enabled) {
1328 usb_gadget_disconnect(cdev->gadget);
Benoit Gobye0de0a52011-11-29 13:49:27 -08001329 /* Cancel pending control requests */
1330 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
Benoit Gobyaab96812011-04-19 20:37:33 -07001331 usb_remove_config(cdev, &android_config_driver);
1332 dev->enabled = false;
1333 } else {
1334 pr_err("android_usb: already %s\n",
1335 dev->enabled ? "enabled" : "disabled");
1336 }
Benoit Gobydc1b6342011-12-09 18:05:00 -08001337
1338 mutex_unlock(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001339 return size;
1340}
1341
Ofir Cohen94213a72012-05-03 14:26:32 +03001342static ssize_t pm_qos_show(struct device *pdev,
1343 struct device_attribute *attr, char *buf)
1344{
1345 struct android_dev *dev = dev_get_drvdata(pdev);
1346
1347 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1348}
1349
1350static ssize_t pm_qos_store(struct device *pdev,
1351 struct device_attribute *attr,
1352 const char *buff, size_t size)
1353{
1354 struct android_dev *dev = dev_get_drvdata(pdev);
1355
1356 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1357
1358 return size;
1359}
1360
Benoit Gobyaab96812011-04-19 20:37:33 -07001361static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1362 char *buf)
1363{
1364 struct android_dev *dev = dev_get_drvdata(pdev);
1365 struct usb_composite_dev *cdev = dev->cdev;
1366 char *state = "DISCONNECTED";
1367 unsigned long flags;
1368
1369 if (!cdev)
1370 goto out;
1371
1372 spin_lock_irqsave(&cdev->lock, flags);
1373 if (cdev->config)
1374 state = "CONFIGURED";
1375 else if (dev->connected)
1376 state = "CONNECTED";
1377 spin_unlock_irqrestore(&cdev->lock, flags);
1378out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301379 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001380}
1381
1382#define DESCRIPTOR_ATTR(field, format_string) \
1383static ssize_t \
1384field ## _show(struct device *dev, struct device_attribute *attr, \
1385 char *buf) \
1386{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301387 return snprintf(buf, PAGE_SIZE, \
1388 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001389} \
1390static ssize_t \
1391field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001392 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001393{ \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001394 int value; \
Benoit Gobyaab96812011-04-19 20:37:33 -07001395 if (sscanf(buf, format_string, &value) == 1) { \
1396 device_desc.field = value; \
1397 return size; \
1398 } \
1399 return -1; \
1400} \
1401static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1402
1403#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1404static ssize_t \
1405field ## _show(struct device *dev, struct device_attribute *attr, \
1406 char *buf) \
1407{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301408 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001409} \
1410static ssize_t \
1411field ## _store(struct device *dev, struct device_attribute *attr, \
Benoit Gobydc1b6342011-12-09 18:05:00 -08001412 const char *buf, size_t size) \
Benoit Gobyaab96812011-04-19 20:37:33 -07001413{ \
1414 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301415 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001416 return size; \
1417 } \
1418 return -1; \
1419} \
1420static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1421
1422
1423DESCRIPTOR_ATTR(idVendor, "%04x\n")
1424DESCRIPTOR_ATTR(idProduct, "%04x\n")
1425DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1426DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1427DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1428DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1429DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1430DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1431DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1432
1433static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1434static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001435static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1436 pm_qos_show, pm_qos_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001437static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301438static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1439 remote_wakeup_show, remote_wakeup_store);
Benoit Gobyaab96812011-04-19 20:37:33 -07001440
1441static struct device_attribute *android_usb_attributes[] = {
1442 &dev_attr_idVendor,
1443 &dev_attr_idProduct,
1444 &dev_attr_bcdDevice,
1445 &dev_attr_bDeviceClass,
1446 &dev_attr_bDeviceSubClass,
1447 &dev_attr_bDeviceProtocol,
1448 &dev_attr_iManufacturer,
1449 &dev_attr_iProduct,
1450 &dev_attr_iSerial,
1451 &dev_attr_functions,
1452 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001453 &dev_attr_pm_qos,
Benoit Gobyaab96812011-04-19 20:37:33 -07001454 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301455 &dev_attr_remote_wakeup,
Benoit Gobyaab96812011-04-19 20:37:33 -07001456 NULL
1457};
1458
1459/*-------------------------------------------------------------------------*/
1460/* Composite driver */
1461
1462static int android_bind_config(struct usb_configuration *c)
1463{
1464 struct android_dev *dev = _android_dev;
1465 int ret = 0;
1466
1467 ret = android_bind_enabled_functions(dev, c);
1468 if (ret)
1469 return ret;
1470
1471 return 0;
1472}
1473
1474static void android_unbind_config(struct usb_configuration *c)
1475{
1476 struct android_dev *dev = _android_dev;
1477
1478 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301479}
1480
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001481static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001482{
1483 struct android_dev *dev = _android_dev;
1484 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001485 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001486
Benoit Gobyaab96812011-04-19 20:37:33 -07001487 usb_gadget_disconnect(gadget);
1488
1489 ret = android_init_functions(dev->functions, cdev);
1490 if (ret)
1491 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001492
1493 /* Allocate string descriptor numbers ... note that string
1494 * contents can be overridden by the composite_dev glue.
1495 */
1496 id = usb_string_id(cdev);
1497 if (id < 0)
1498 return id;
1499 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1500 device_desc.iManufacturer = id;
1501
1502 id = usb_string_id(cdev);
1503 if (id < 0)
1504 return id;
1505 strings_dev[STRING_PRODUCT_IDX].id = id;
1506 device_desc.iProduct = id;
1507
Benoit Gobyaab96812011-04-19 20:37:33 -07001508 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301509 strlcpy(manufacturer_string, "Android",
1510 sizeof(manufacturer_string) - 1);
1511 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1512 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001513
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001514 id = usb_string_id(cdev);
1515 if (id < 0)
1516 return id;
1517 strings_dev[STRING_SERIAL_IDX].id = id;
1518 device_desc.iSerialNumber = id;
1519
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301520 if (gadget_is_otg(cdev->gadget))
1521 android_config_driver.descriptors = otg_desc;
1522
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001523 gcnum = usb_gadget_controller_number(gadget);
1524 if (gcnum >= 0)
1525 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1526 else {
1527 /* gadget zero is so simple (for now, no altsettings) that
1528 * it SHOULD NOT have problems with bulk-capable hardware.
1529 * so just warn about unrcognized controllers -- don't panic.
1530 *
1531 * things like configuration and altsetting numbering
1532 * can need hardware-specific attention though.
1533 */
1534 pr_warning("%s: controller '%s' not recognized\n",
1535 longname, gadget->name);
1536 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1537 }
1538
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001539 dev->cdev = cdev;
1540
1541 return 0;
1542}
1543
Benoit Gobyaab96812011-04-19 20:37:33 -07001544static int android_usb_unbind(struct usb_composite_dev *cdev)
1545{
1546 struct android_dev *dev = _android_dev;
1547
Lena Salmand092f2d2012-03-12 17:27:24 +02001548 manufacturer_string[0] = '\0';
1549 product_string[0] = '\0';
1550 serial_string[0] = '0';
Benoit Gobyaab96812011-04-19 20:37:33 -07001551 cancel_work_sync(&dev->work);
1552 android_cleanup_functions(dev->functions);
1553 return 0;
1554}
1555
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001556static struct usb_composite_driver android_usb_driver = {
1557 .name = "android_usb",
1558 .dev = &device_desc,
1559 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001560 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001561 .max_speed = USB_SPEED_SUPER
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001562};
1563
Benoit Gobyaab96812011-04-19 20:37:33 -07001564static int
1565android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1566{
1567 struct android_dev *dev = _android_dev;
1568 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1569 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001570 struct android_usb_function *f;
1571 int value = -EOPNOTSUPP;
1572 unsigned long flags;
1573
1574 req->zero = 0;
1575 req->complete = composite_setup_complete;
1576 req->length = 0;
1577 gadget->ep0->driver_data = cdev;
1578
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001579 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001580 if (f->ctrlrequest) {
1581 value = f->ctrlrequest(f, cdev, c);
1582 if (value >= 0)
1583 break;
1584 }
1585 }
1586
Mike Lockwood686d33a2011-09-07 09:55:12 -07001587 /* Special case the accessory function.
1588 * It needs to handle control requests before it is enabled.
1589 */
1590 if (value < 0)
1591 value = acc_ctrlrequest(cdev, c);
1592
Benoit Gobyaab96812011-04-19 20:37:33 -07001593 if (value < 0)
1594 value = composite_setup(gadget, c);
1595
1596 spin_lock_irqsave(&cdev->lock, flags);
1597 if (!dev->connected) {
1598 dev->connected = 1;
1599 schedule_work(&dev->work);
1600 }
1601 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1602 schedule_work(&dev->work);
1603 }
1604 spin_unlock_irqrestore(&cdev->lock, flags);
1605
1606 return value;
1607}
1608
1609static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001610{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301611 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001612 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1613 unsigned long flags;
1614
1615 composite_disconnect(gadget);
1616
1617 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001618 dev->connected = 0;
1619 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001620 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301621}
1622
Benoit Gobyaab96812011-04-19 20:37:33 -07001623static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001624{
Benoit Gobyaab96812011-04-19 20:37:33 -07001625 struct device_attribute **attrs = android_usb_attributes;
1626 struct device_attribute *attr;
1627 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001628
Benoit Gobyaab96812011-04-19 20:37:33 -07001629 dev->dev = device_create(android_class, NULL,
1630 MKDEV(0, 0), NULL, "android0");
1631 if (IS_ERR(dev->dev))
1632 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001633
Benoit Gobyaab96812011-04-19 20:37:33 -07001634 dev_set_drvdata(dev->dev, dev);
1635
1636 while ((attr = *attrs++)) {
1637 err = device_create_file(dev->dev, attr);
1638 if (err) {
1639 device_destroy(android_class, dev->dev->devt);
1640 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001641 }
1642 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001643 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001644}
1645
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301646static void android_destroy_device(struct android_dev *dev)
1647{
1648 struct device_attribute **attrs = android_usb_attributes;
1649 struct device_attribute *attr;
1650
1651 while ((attr = *attrs++))
1652 device_remove_file(dev->dev, attr);
1653 device_destroy(android_class, dev->dev->devt);
1654}
1655
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001656static int __devinit android_probe(struct platform_device *pdev)
1657{
1658 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1659 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001660 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661
1662 dev->pdata = pdata;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301663
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301664 android_class = class_create(THIS_MODULE, "android_usb");
1665 if (IS_ERR(android_class))
1666 return PTR_ERR(android_class);
1667
1668 ret = android_create_device(dev);
1669 if (ret) {
1670 pr_err("%s(): android_create_device failed\n", __func__);
1671 goto err_dev;
1672 }
1673
Lena Salmand092f2d2012-03-12 17:27:24 +02001674 ret = usb_composite_probe(&android_usb_driver, android_bind);
1675 if (ret) {
1676 pr_err("%s(): Failed to register android "
1677 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301678 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001679 }
1680
Ofir Cohen94213a72012-05-03 14:26:32 +03001681 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301682 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001683 pm_qos_add_request(&dev->pm_qos_req_dma,
1684 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1685 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1686
Lena Salmand092f2d2012-03-12 17:27:24 +02001687 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301688err_probe:
1689 android_destroy_device(dev);
1690err_dev:
1691 class_destroy(android_class);
1692 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001693}
1694
1695static int android_remove(struct platform_device *pdev)
1696{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301697 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001698 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301699
1700 android_destroy_device(dev);
1701 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001702 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301703 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001704 pm_qos_remove_request(&dev->pm_qos_req_dma);
1705
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001706 return 0;
1707}
1708
1709static struct platform_driver android_platform_driver = {
1710 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001711 .probe = android_probe,
1712 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001714
1715static int __init init(void)
1716{
1717 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301718 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001719
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001720 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301721 if (!dev) {
1722 pr_err("%s(): Failed to alloc memory for android_dev\n",
1723 __func__);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001724 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301725 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001726 dev->functions = supported_functions;
1727 INIT_LIST_HEAD(&dev->enabled_functions);
1728 INIT_WORK(&dev->work, android_work);
Benoit Gobydc1b6342011-12-09 18:05:00 -08001729 mutex_init(&dev->mutex);
Benoit Gobyaab96812011-04-19 20:37:33 -07001730
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001731 _android_dev = dev;
1732
Benoit Gobyaab96812011-04-19 20:37:33 -07001733 /* Override composite driver functions */
1734 composite_driver.setup = android_setup;
1735 composite_driver.disconnect = android_disconnect;
1736
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301737 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301738 if (ret) {
1739 pr_err("%s(): Failed to register android"
1740 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301741 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301742 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001743
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301744 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001745}
1746module_init(init);
1747
1748static void __exit cleanup(void)
1749{
Lena Salmand092f2d2012-03-12 17:27:24 +02001750 platform_driver_unregister(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001751 kfree(_android_dev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001752 _android_dev = NULL;
1753}
1754module_exit(cleanup);