blob: be8e6aa87d5498901886164c6f04a6d949686504 [file] [log] [blame]
Benoit Goby1e8ce152011-12-12 13:01:23 -08001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@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#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/utsname.h>
25#include <linux/platform_device.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080027
28#include <linux/usb/ch9.h>
29#include <linux/usb/composite.h>
30#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <linux/usb/android.h>
Benoit Goby1e8ce152011-12-12 13:01:23 -080032
33#include "gadget_chips.h"
34
35/*
36 * Kbuild is not very cooperative with respect to linking separately
37 * compiled library objects into one module. So for now we won't use
38 * separate compilation ... ensuring init/exit sections work to shrink
39 * the runtime footprint, and giving us at least some parts of what
40 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
41 */
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45#include "composite.c"
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053048#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053049#include "f_rmnet_sdio.c"
50#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053051#include "f_rmnet.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080052#include "f_mass_storage.c"
53#include "u_serial.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053054#include "u_sdio.c"
55#include "u_smd.c"
56#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053057#include "u_rmnet_ctrl_smd.c"
Jack Pham427f6922011-11-23 19:42:00 -080058#include "u_ctrl_hsic.c"
59#include "u_data_hsic.c"
Vijayavardhan Vennapusaeb8d2392012-04-03 18:58:49 +053060#include "u_ctrl_hsuart.c"
61#include "u_data_hsuart.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053062#include "f_serial.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080063#include "f_acm.c"
Benoit Goby2b6862d2011-12-19 14:38:41 -080064#include "f_adb.c"
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +053065#include "f_ccid.c"
Benoit Gobyf0fbc482011-12-19 14:37:50 -080066#include "f_mtp.c"
Benoit Gobycf3fc062011-12-19 14:39:37 -080067#include "f_accessory.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080068#define USB_ETH_RNDIS y
69#include "f_rndis.c"
70#include "rndis.c"
71#include "u_ether.c"
Anna Perela8c991d2012-04-09 16:44:46 +030072#include "u_bam_data.c"
73#include "f_mbim.c"
Benoit Goby1e8ce152011-12-12 13:01:23 -080074
75MODULE_AUTHOR("Mike Lockwood");
76MODULE_DESCRIPTION("Android Composite USB Driver");
77MODULE_LICENSE("GPL");
78MODULE_VERSION("1.0");
79
80static const char longname[] = "Gadget Android";
81
82/* Default vendor and product IDs, overridden by userspace */
83#define VENDOR_ID 0x18D1
84#define PRODUCT_ID 0x0001
85
86struct android_usb_function {
87 char *name;
88 void *config;
89
90 struct device *dev;
91 char *dev_name;
92 struct device_attribute **attributes;
93
94 /* for android_dev.enabled_functions */
95 struct list_head enabled_list;
96
97 /* Optional: initialization during gadget bind */
98 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
99 /* Optional: cleanup during gadget unbind */
100 void (*cleanup)(struct android_usb_function *);
Benoit Goby80ba14d2012-03-19 18:56:52 -0700101 /* Optional: called when the function is added the list of
102 * enabled functions */
103 void (*enable)(struct android_usb_function *);
104 /* Optional: called when it is removed */
105 void (*disable)(struct android_usb_function *);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800106
107 int (*bind_config)(struct android_usb_function *,
108 struct usb_configuration *);
109
110 /* Optional: called when the configuration is removed */
111 void (*unbind_config)(struct android_usb_function *,
112 struct usb_configuration *);
113 /* Optional: handle ctrl requests before the device is configured */
114 int (*ctrlrequest)(struct android_usb_function *,
115 struct usb_composite_dev *,
116 const struct usb_ctrlrequest *);
117};
118
119struct android_dev {
120 struct android_usb_function **functions;
121 struct list_head enabled_functions;
122 struct usb_composite_dev *cdev;
123 struct device *dev;
124
125 bool enabled;
Benoit Goby80ba14d2012-03-19 18:56:52 -0700126 int disable_depth;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800127 struct mutex mutex;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700128 struct android_usb_platform_data *pdata;
129
Benoit Goby1e8ce152011-12-12 13:01:23 -0800130 bool connected;
131 bool sw_connected;
Ofir Cohen94213a72012-05-03 14:26:32 +0300132 char pm_qos[5];
Steve Mucklef132c6c2012-06-06 18:30:57 -0700133 struct pm_qos_request pm_qos_req_dma;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800134 struct work_struct work;
135};
136
137static struct class *android_class;
138static struct android_dev *_android_dev;
139static int android_bind_config(struct usb_configuration *c);
140static void android_unbind_config(struct usb_configuration *c);
141
142/* string IDs are assigned dynamically */
143#define STRING_MANUFACTURER_IDX 0
144#define STRING_PRODUCT_IDX 1
145#define STRING_SERIAL_IDX 2
146
147static char manufacturer_string[256];
148static char product_string[256];
149static char serial_string[256];
150
151/* String Table */
152static struct usb_string strings_dev[] = {
153 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
154 [STRING_PRODUCT_IDX].s = product_string,
155 [STRING_SERIAL_IDX].s = serial_string,
156 { } /* end of list */
157};
158
159static struct usb_gadget_strings stringtab_dev = {
160 .language = 0x0409, /* en-us */
161 .strings = strings_dev,
162};
163
164static struct usb_gadget_strings *dev_strings[] = {
165 &stringtab_dev,
166 NULL,
167};
168
169static struct usb_device_descriptor device_desc = {
170 .bLength = sizeof(device_desc),
171 .bDescriptorType = USB_DT_DEVICE,
172 .bcdUSB = __constant_cpu_to_le16(0x0200),
173 .bDeviceClass = USB_CLASS_PER_INTERFACE,
174 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
175 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
176 .bcdDevice = __constant_cpu_to_le16(0xffff),
177 .bNumConfigurations = 1,
178};
179
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +0530180static struct usb_otg_descriptor otg_descriptor = {
181 .bLength = sizeof otg_descriptor,
182 .bDescriptorType = USB_DT_OTG,
183 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
184 .bcdOTG = __constant_cpu_to_le16(0x0200),
185};
186
187static const struct usb_descriptor_header *otg_desc[] = {
188 (struct usb_descriptor_header *) &otg_descriptor,
189 NULL,
190};
191
Benoit Goby1e8ce152011-12-12 13:01:23 -0800192static struct usb_configuration android_config_driver = {
193 .label = "android",
194 .unbind = android_unbind_config,
195 .bConfigurationValue = 1,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800196};
197
Manu Gautama2b54142012-04-03 14:34:32 +0530198enum android_device_state {
199 USB_DISCONNECTED,
200 USB_CONNECTED,
201 USB_CONFIGURED,
202};
203
Ofir Cohen94213a72012-05-03 14:26:32 +0300204static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
205{
206 struct android_usb_platform_data *pdata = dev->pdata;
207 u32 swfi_latency = 0;
208 static int last_vote = -1;
209
Ofir Cohen56eb7072012-05-20 11:41:39 +0300210 if (!pdata || vote == last_vote
211 || !pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +0300212 return;
213
214 swfi_latency = pdata->swfi_latency + 1;
215 if (vote)
216 pm_qos_update_request(&dev->pm_qos_req_dma,
217 swfi_latency);
218 else
219 pm_qos_update_request(&dev->pm_qos_req_dma,
220 PM_QOS_DEFAULT_VALUE);
221 last_vote = vote;
222}
223
Benoit Goby1e8ce152011-12-12 13:01:23 -0800224static void android_work(struct work_struct *data)
225{
226 struct android_dev *dev = container_of(data, struct android_dev, work);
227 struct usb_composite_dev *cdev = dev->cdev;
228 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
229 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
230 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
231 char **uevent_envp = NULL;
Manu Gautama2b54142012-04-03 14:34:32 +0530232 static enum android_device_state last_uevent, next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800233 unsigned long flags;
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300234 int pm_qos_vote = -1;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800235
236 spin_lock_irqsave(&cdev->lock, flags);
Manu Gautama2b54142012-04-03 14:34:32 +0530237 if (cdev->config) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800238 uevent_envp = configured;
Manu Gautama2b54142012-04-03 14:34:32 +0530239 next_state = USB_CONFIGURED;
240 } else if (dev->connected != dev->sw_connected) {
Benoit Goby1e8ce152011-12-12 13:01:23 -0800241 uevent_envp = dev->connected ? connected : disconnected;
Manu Gautama2b54142012-04-03 14:34:32 +0530242 next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
Ofir Cohen94213a72012-05-03 14:26:32 +0300243 if (dev->connected && strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300244 pm_qos_vote = 1;
Ofir Cohen94213a72012-05-03 14:26:32 +0300245 else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300246 pm_qos_vote = 0;
Manu Gautama2b54142012-04-03 14:34:32 +0530247 }
Benoit Goby1e8ce152011-12-12 13:01:23 -0800248 dev->sw_connected = dev->connected;
249 spin_unlock_irqrestore(&cdev->lock, flags);
250
Ofir Cohenbcbb1a72012-05-20 16:28:15 +0300251 if (pm_qos_vote != -1)
252 android_pm_qos_update_latency(dev, pm_qos_vote);
253
Benoit Goby1e8ce152011-12-12 13:01:23 -0800254 if (uevent_envp) {
Manu Gautama2b54142012-04-03 14:34:32 +0530255 /*
256 * Some userspace modules, e.g. MTP, work correctly only if
257 * CONFIGURED uevent is preceded by DISCONNECT uevent.
258 * Check if we missed sending out a DISCONNECT uevent. This can
259 * happen if host PC resets and configures device really quick.
260 */
261 if (((uevent_envp == connected) &&
262 (last_uevent != USB_DISCONNECTED)) ||
263 ((uevent_envp == configured) &&
264 (last_uevent == USB_CONFIGURED))) {
265 pr_info("%s: sent missed DISCONNECT event\n", __func__);
266 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
267 disconnected);
268 msleep(20);
269 }
270 /*
271 * Before sending out CONFIGURED uevent give function drivers
272 * a chance to wakeup userspace threads and notify disconnect
273 */
274 if (uevent_envp == configured)
275 msleep(50);
276
Benoit Goby1e8ce152011-12-12 13:01:23 -0800277 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
Manu Gautama2b54142012-04-03 14:34:32 +0530278 last_uevent = next_state;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800279 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
280 } else {
281 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
282 dev->connected, dev->sw_connected, cdev->config);
283 }
284}
285
Benoit Goby80ba14d2012-03-19 18:56:52 -0700286static void android_enable(struct android_dev *dev)
287{
288 struct usb_composite_dev *cdev = dev->cdev;
289
290 if (WARN_ON(!dev->disable_depth))
291 return;
292
293 if (--dev->disable_depth == 0) {
294 usb_add_config(cdev, &android_config_driver,
295 android_bind_config);
296 usb_gadget_connect(cdev->gadget);
297 }
298}
299
300static void android_disable(struct android_dev *dev)
301{
302 struct usb_composite_dev *cdev = dev->cdev;
303
304 if (dev->disable_depth++ == 0) {
305 usb_gadget_disconnect(cdev->gadget);
306 /* Cancel pending control requests */
307 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
308 usb_remove_config(cdev, &android_config_driver);
309 }
310}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800311
312/*-------------------------------------------------------------------------*/
313/* Supported functions initialization */
314
Benoit Goby80ba14d2012-03-19 18:56:52 -0700315struct adb_data {
316 bool opened;
317 bool enabled;
318};
319
Benoit Goby2b6862d2011-12-19 14:38:41 -0800320static int
321adb_function_init(struct android_usb_function *f,
322 struct usb_composite_dev *cdev)
323{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700324 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
325 if (!f->config)
326 return -ENOMEM;
327
Benoit Goby2b6862d2011-12-19 14:38:41 -0800328 return adb_setup();
329}
330
331static void adb_function_cleanup(struct android_usb_function *f)
332{
333 adb_cleanup();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700334 kfree(f->config);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800335}
336
337static int
338adb_function_bind_config(struct android_usb_function *f,
339 struct usb_configuration *c)
340{
341 return adb_bind_config(c);
342}
343
Benoit Goby80ba14d2012-03-19 18:56:52 -0700344static void adb_android_function_enable(struct android_usb_function *f)
345{
346 struct android_dev *dev = _android_dev;
347 struct adb_data *data = f->config;
348
349 data->enabled = true;
350
351 /* Disable the gadget until adbd is ready */
352 if (!data->opened)
353 android_disable(dev);
354}
355
356static void adb_android_function_disable(struct android_usb_function *f)
357{
358 struct android_dev *dev = _android_dev;
359 struct adb_data *data = f->config;
360
361 data->enabled = false;
362
363 /* Balance the disable that was called in closed_callback */
364 if (!data->opened)
365 android_enable(dev);
366}
367
Benoit Goby2b6862d2011-12-19 14:38:41 -0800368static struct android_usb_function adb_function = {
369 .name = "adb",
Benoit Goby80ba14d2012-03-19 18:56:52 -0700370 .enable = adb_android_function_enable,
371 .disable = adb_android_function_disable,
Benoit Goby2b6862d2011-12-19 14:38:41 -0800372 .init = adb_function_init,
373 .cleanup = adb_function_cleanup,
374 .bind_config = adb_function_bind_config,
375};
376
Benoit Goby80ba14d2012-03-19 18:56:52 -0700377static void adb_ready_callback(void)
378{
379 struct android_dev *dev = _android_dev;
380 struct adb_data *data = adb_function.config;
381
382 mutex_lock(&dev->mutex);
383
384 data->opened = true;
385
386 if (data->enabled)
387 android_enable(dev);
388
389 mutex_unlock(&dev->mutex);
390}
391
392static void adb_closed_callback(void)
393{
394 struct android_dev *dev = _android_dev;
395 struct adb_data *data = adb_function.config;
396
397 mutex_lock(&dev->mutex);
398
399 data->opened = false;
400
401 if (data->enabled)
402 android_disable(dev);
403
404 mutex_unlock(&dev->mutex);
405}
406
Benoit Goby2b6862d2011-12-19 14:38:41 -0800407
Benoit Gobyaab96812011-04-19 20:37:33 -0700408/*-------------------------------------------------------------------------*/
409/* Supported functions initialization */
410
Manu Gautam8e0719b2011-09-26 14:47:55 +0530411/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530412static int rmnet_smd_function_bind_config(struct android_usb_function *f,
413 struct usb_configuration *c)
414{
415 return rmnet_smd_bind_config(c);
416}
417
418static struct android_usb_function rmnet_smd_function = {
419 .name = "rmnet_smd",
420 .bind_config = rmnet_smd_function_bind_config,
Benoit Goby1e8ce152011-12-12 13:01:23 -0800421};
422
Manu Gautam8e0719b2011-09-26 14:47:55 +0530423/* RMNET_SDIO */
424static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
425 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800426{
Manu Gautam8e0719b2011-09-26 14:47:55 +0530427 return rmnet_sdio_function_add(c);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800428}
429
Manu Gautam8e0719b2011-09-26 14:47:55 +0530430static struct android_usb_function rmnet_sdio_function = {
431 .name = "rmnet_sdio",
432 .bind_config = rmnet_sdio_function_bind_config,
433};
434
435/* RMNET_SMD_SDIO */
436static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
437 struct usb_composite_dev *cdev)
438{
439 return rmnet_smd_sdio_init();
440}
441
442static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
443{
444 rmnet_smd_sdio_cleanup();
445}
446
447static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
448 struct usb_configuration *c)
449{
450 return rmnet_smd_sdio_function_add(c);
451}
452
453static struct device_attribute *rmnet_smd_sdio_attributes[] = {
454 &dev_attr_transport, NULL };
455
456static struct android_usb_function rmnet_smd_sdio_function = {
457 .name = "rmnet_smd_sdio",
458 .init = rmnet_smd_sdio_function_init,
459 .cleanup = rmnet_smd_sdio_function_cleanup,
460 .bind_config = rmnet_smd_sdio_bind_config,
461 .attributes = rmnet_smd_sdio_attributes,
462};
463
Hemant Kumar1b820d52011-11-03 15:08:28 -0700464/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
465#define MAX_XPORT_STR_LEN 50
466static char rmnet_transports[MAX_XPORT_STR_LEN];
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530467
Manu Gautame3e897c2011-09-12 17:18:46 +0530468static void rmnet_function_cleanup(struct android_usb_function *f)
469{
470 frmnet_cleanup();
471}
472
Manu Gautam2b0234a2011-09-07 16:47:52 +0530473static int rmnet_function_bind_config(struct android_usb_function *f,
474 struct usb_configuration *c)
475{
476 int i;
Hemant Kumar1b820d52011-11-03 15:08:28 -0700477 int err = 0;
478 char *ctrl_name;
479 char *data_name;
480 char buf[MAX_XPORT_STR_LEN], *b;
481 static int rmnet_initialized, ports;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530482
Hemant Kumar1b820d52011-11-03 15:08:28 -0700483 if (!rmnet_initialized) {
484 rmnet_initialized = 1;
485 strlcpy(buf, rmnet_transports, sizeof(buf));
486 b = strim(buf);
487 while (b) {
488 ctrl_name = strsep(&b, ",");
489 data_name = strsep(&b, ",");
490 if (ctrl_name && data_name) {
491 err = frmnet_init_port(ctrl_name, data_name);
492 if (err) {
493 pr_err("rmnet: Cannot open ctrl port:"
494 "'%s' data port:'%s'\n",
495 ctrl_name, data_name);
496 goto out;
497 }
498 ports++;
499 }
500 }
501
502 err = rmnet_gport_setup();
503 if (err) {
504 pr_err("rmnet: Cannot setup transports");
505 goto out;
506 }
507 }
508
509 for (i = 0; i < ports; i++) {
510 err = frmnet_bind_config(c, i);
511 if (err) {
Manu Gautam2b0234a2011-09-07 16:47:52 +0530512 pr_err("Could not bind rmnet%u config\n", i);
513 break;
514 }
515 }
Hemant Kumar1b820d52011-11-03 15:08:28 -0700516out:
517 return err;
Manu Gautam2b0234a2011-09-07 16:47:52 +0530518}
519
Hemant Kumar1b820d52011-11-03 15:08:28 -0700520static ssize_t rmnet_transports_show(struct device *dev,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530521 struct device_attribute *attr, char *buf)
522{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700523 return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530524}
525
Hemant Kumar1b820d52011-11-03 15:08:28 -0700526static ssize_t rmnet_transports_store(
527 struct device *device, struct device_attribute *attr,
528 const char *buff, size_t size)
Manu Gautam2b0234a2011-09-07 16:47:52 +0530529{
Hemant Kumar1b820d52011-11-03 15:08:28 -0700530 strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
Manu Gautam2b0234a2011-09-07 16:47:52 +0530531
Manu Gautam2b0234a2011-09-07 16:47:52 +0530532 return size;
533}
534
Hemant Kumar1b820d52011-11-03 15:08:28 -0700535static struct device_attribute dev_attr_rmnet_transports =
536 __ATTR(transports, S_IRUGO | S_IWUSR,
537 rmnet_transports_show,
538 rmnet_transports_store);
Manu Gautam2b0234a2011-09-07 16:47:52 +0530539static struct device_attribute *rmnet_function_attributes[] = {
Hemant Kumar1b820d52011-11-03 15:08:28 -0700540 &dev_attr_rmnet_transports,
541 NULL };
Manu Gautam2b0234a2011-09-07 16:47:52 +0530542
543static struct android_usb_function rmnet_function = {
544 .name = "rmnet",
Manu Gautame3e897c2011-09-12 17:18:46 +0530545 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530546 .bind_config = rmnet_function_bind_config,
547 .attributes = rmnet_function_attributes,
548};
549
Anna Perela8c991d2012-04-09 16:44:46 +0300550
551/* MBIM - used with BAM */
552#define MAX_MBIM_INSTANCES 1
553
554static int mbim_function_init(struct android_usb_function *f,
555 struct usb_composite_dev *cdev)
556{
557 return mbim_init(MAX_MBIM_INSTANCES);
558}
559
560static void mbim_function_cleanup(struct android_usb_function *f)
561{
562 fmbim_cleanup();
563}
564
565static int mbim_function_bind_config(struct android_usb_function *f,
566 struct usb_configuration *c)
567{
568 return mbim_bind_config(c, 0);
569}
570
571static struct android_usb_function mbim_function = {
572 .name = "usb_mbim",
573 .cleanup = mbim_function_cleanup,
574 .bind_config = mbim_function_bind_config,
575 .init = mbim_function_init,
576};
577
578
Manu Gautam8e0719b2011-09-26 14:47:55 +0530579/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530580static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581static ssize_t clients_store(
582 struct device *device, struct device_attribute *attr,
583 const char *buff, size_t size)
584{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530585 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586
587 return size;
588}
589
590static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
591static struct device_attribute *diag_function_attributes[] =
592 { &dev_attr_clients, NULL };
593
594static int diag_function_init(struct android_usb_function *f,
595 struct usb_composite_dev *cdev)
596{
597 return diag_setup();
598}
599
600static void diag_function_cleanup(struct android_usb_function *f)
601{
602 diag_cleanup();
603}
604
605static int diag_function_bind_config(struct android_usb_function *f,
606 struct usb_configuration *c)
607{
608 char *name;
609 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530610 int once = 0, err = -1;
Jack Phamb830a6c2011-12-12 22:35:27 -0800611 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530613 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 b = strim(buf);
615
616 while (b) {
Jack Phamb830a6c2011-12-12 22:35:27 -0800617 notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530619 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700620 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530621 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530622
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530624 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 if (err)
626 pr_err("diag: Cannot open channel '%s'", name);
627 }
628 }
629
630 return err;
631}
632
633static struct android_usb_function diag_function = {
634 .name = "diag",
635 .init = diag_function_init,
636 .cleanup = diag_function_cleanup,
637 .bind_config = diag_function_bind_config,
638 .attributes = diag_function_attributes,
639};
640
Manu Gautam8e0719b2011-09-26 14:47:55 +0530641/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530642static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530643static ssize_t serial_transports_store(
644 struct device *device, struct device_attribute *attr,
645 const char *buff, size_t size)
646{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530647 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530648
649 return size;
650}
651
652static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
653static struct device_attribute *serial_function_attributes[] =
654 { &dev_attr_transports, NULL };
655
656static void serial_function_cleanup(struct android_usb_function *f)
657{
658 gserial_cleanup();
659}
660
661static int serial_function_bind_config(struct android_usb_function *f,
662 struct usb_configuration *c)
663{
664 char *name;
665 char buf[32], *b;
666 int err = -1, i;
667 static int serial_initialized = 0, ports = 0;
668
669 if (serial_initialized)
670 goto bind_config;
671
672 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530673 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530674 b = strim(buf);
675
676 while (b) {
677 name = strsep(&b, ",");
678
679 if (name) {
680 err = gserial_init_port(ports, name);
681 if (err) {
682 pr_err("serial: Cannot open port '%s'", name);
683 goto out;
684 }
685 ports++;
686 }
687 }
688 err = gport_setup(c);
689 if (err) {
690 pr_err("serial: Cannot setup transports");
691 goto out;
692 }
693
694bind_config:
Lena Salmand092f2d2012-03-12 17:27:24 +0200695 for (i = 0; i < ports; i++) {
Manu Gautama4d993f2011-08-30 18:25:55 +0530696 err = gser_bind_config(c, i);
697 if (err) {
698 pr_err("serial: bind_config failed for port %d", i);
699 goto out;
700 }
701 }
702
703out:
704 return err;
705}
706
707static struct android_usb_function serial_function = {
708 .name = "serial",
709 .cleanup = serial_function_cleanup,
710 .bind_config = serial_function_bind_config,
711 .attributes = serial_function_attributes,
712};
713
Anji jonnala92be1b42011-12-19 09:44:41 +0530714/* ACM */
715static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
716static ssize_t acm_transports_store(
717 struct device *device, struct device_attribute *attr,
718 const char *buff, size_t size)
719{
720 strlcpy(acm_transports, buff, sizeof(acm_transports));
721
722 return size;
723}
724
725static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
726static struct device_attribute *acm_function_attributes[] = {
727 &dev_attr_acm_transports, NULL };
728
Benoit Goby1e8ce152011-12-12 13:01:23 -0800729static void acm_function_cleanup(struct android_usb_function *f)
730{
731 gserial_cleanup();
Benoit Goby1e8ce152011-12-12 13:01:23 -0800732}
733
Anji jonnala92be1b42011-12-19 09:44:41 +0530734static int acm_function_bind_config(struct android_usb_function *f,
735 struct usb_configuration *c)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800736{
Anji jonnala92be1b42011-12-19 09:44:41 +0530737 char *name;
738 char buf[32], *b;
739 int err = -1, i;
740 static int acm_initialized, ports;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800741
Anji jonnala92be1b42011-12-19 09:44:41 +0530742 if (acm_initialized)
743 goto bind_config;
744
745 acm_initialized = 1;
746 strlcpy(buf, acm_transports, sizeof(buf));
747 b = strim(buf);
748
749 while (b) {
750 name = strsep(&b, ",");
751
752 if (name) {
753 err = acm_init_port(ports, name);
754 if (err) {
755 pr_err("acm: Cannot open port '%s'", name);
756 goto out;
757 }
758 ports++;
759 }
760 }
761 err = acm_port_setup(c);
762 if (err) {
763 pr_err("acm: Cannot setup transports");
764 goto out;
765 }
766
767bind_config:
768 for (i = 0; i < ports; i++) {
769 err = acm_bind_config(c, i);
770 if (err) {
771 pr_err("acm: bind_config failed for port %d", i);
772 goto out;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800773 }
774 }
775
Anji jonnala92be1b42011-12-19 09:44:41 +0530776out:
777 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -0800778}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800779static struct android_usb_function acm_function = {
780 .name = "acm",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800781 .cleanup = acm_function_cleanup,
782 .bind_config = acm_function_bind_config,
783 .attributes = acm_function_attributes,
784};
785
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530786/* CCID */
787static int ccid_function_init(struct android_usb_function *f,
788 struct usb_composite_dev *cdev)
789{
790 return ccid_setup();
791}
Benoit Goby1e8ce152011-12-12 13:01:23 -0800792
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +0530793static void ccid_function_cleanup(struct android_usb_function *f)
794{
795 ccid_cleanup();
796}
797
798static int ccid_function_bind_config(struct android_usb_function *f,
799 struct usb_configuration *c)
800{
801 return ccid_bind_config(c);
802}
803
804static struct android_usb_function ccid_function = {
805 .name = "ccid",
806 .init = ccid_function_init,
807 .cleanup = ccid_function_cleanup,
808 .bind_config = ccid_function_bind_config,
809};
810
Steve Mucklef132c6c2012-06-06 18:30:57 -0700811static int mtp_function_init(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800812 struct usb_composite_dev *cdev)
813{
814 return mtp_setup();
815}
816
817static void mtp_function_cleanup(struct android_usb_function *f)
818{
819 mtp_cleanup();
820}
821
Steve Mucklef132c6c2012-06-06 18:30:57 -0700822static int mtp_function_bind_config(struct android_usb_function *f,
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800823 struct usb_configuration *c)
824{
825 return mtp_bind_config(c, false);
826}
827
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400828static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800829{
830 /* nothing to do - initialization is handled by mtp_function_init */
831 return 0;
832}
833
834static void ptp_function_cleanup(struct android_usb_function *f)
835{
836 /* nothing to do - cleanup is handled by mtp_function_cleanup */
837}
838
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400839static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Benoit Gobyf0fbc482011-12-19 14:37:50 -0800840{
841 return mtp_bind_config(c, true);
842}
843
844static int mtp_function_ctrlrequest(struct android_usb_function *f,
845 struct usb_composite_dev *cdev,
846 const struct usb_ctrlrequest *c)
847{
848 return mtp_ctrlrequest(cdev, c);
849}
850
851static struct android_usb_function mtp_function = {
852 .name = "mtp",
853 .init = mtp_function_init,
854 .cleanup = mtp_function_cleanup,
855 .bind_config = mtp_function_bind_config,
856 .ctrlrequest = mtp_function_ctrlrequest,
857};
858
859/* PTP function is same as MTP with slightly different interface descriptor */
860static struct android_usb_function ptp_function = {
861 .name = "ptp",
862 .init = ptp_function_init,
863 .cleanup = ptp_function_cleanup,
864 .bind_config = ptp_function_bind_config,
865};
866
867
Benoit Goby1e8ce152011-12-12 13:01:23 -0800868struct rndis_function_config {
869 u8 ethaddr[ETH_ALEN];
870 u32 vendorID;
871 char manufacturer[256];
872 /* "Wireless" RNDIS; auto-detected by Windows */
873 bool wceis;
874};
875
876static int
877rndis_function_init(struct android_usb_function *f,
878 struct usb_composite_dev *cdev)
879{
880 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
881 if (!f->config)
882 return -ENOMEM;
883 return 0;
884}
885
886static void rndis_function_cleanup(struct android_usb_function *f)
887{
888 kfree(f->config);
889 f->config = NULL;
890}
891
892static int
893rndis_function_bind_config(struct android_usb_function *f,
894 struct usb_configuration *c)
895{
896 int ret;
897 struct rndis_function_config *rndis = f->config;
898
899 if (!rndis) {
900 pr_err("%s: rndis_pdata\n", __func__);
901 return -1;
902 }
903
904 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
905 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
906 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
907
908 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
909 if (ret) {
910 pr_err("%s: gether_setup failed\n", __func__);
911 return ret;
912 }
913
914 if (rndis->wceis) {
915 /* "Wireless" RNDIS; auto-detected by Windows */
916 rndis_iad_descriptor.bFunctionClass =
917 USB_CLASS_WIRELESS_CONTROLLER;
918 rndis_iad_descriptor.bFunctionSubClass = 0x01;
919 rndis_iad_descriptor.bFunctionProtocol = 0x03;
920 rndis_control_intf.bInterfaceClass =
921 USB_CLASS_WIRELESS_CONTROLLER;
922 rndis_control_intf.bInterfaceSubClass = 0x01;
923 rndis_control_intf.bInterfaceProtocol = 0x03;
924 }
925
926 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
927 rndis->manufacturer);
928}
929
930static void rndis_function_unbind_config(struct android_usb_function *f,
931 struct usb_configuration *c)
932{
933 gether_cleanup();
934}
935
936static ssize_t rndis_manufacturer_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
938{
939 struct android_usb_function *f = dev_get_drvdata(dev);
940 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700941
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530942 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800943}
944
945static ssize_t rndis_manufacturer_store(struct device *dev,
946 struct device_attribute *attr, const char *buf, size_t size)
947{
948 struct android_usb_function *f = dev_get_drvdata(dev);
949 struct rndis_function_config *config = f->config;
950
951 if (size >= sizeof(config->manufacturer))
952 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700953
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530954 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -0800955 return size;
956 return -1;
957}
958
959static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
960 rndis_manufacturer_store);
961
962static ssize_t rndis_wceis_show(struct device *dev,
963 struct device_attribute *attr, char *buf)
964{
965 struct android_usb_function *f = dev_get_drvdata(dev);
966 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700967
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530968 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Goby1e8ce152011-12-12 13:01:23 -0800969}
970
971static ssize_t rndis_wceis_store(struct device *dev,
972 struct device_attribute *attr, const char *buf, size_t size)
973{
974 struct android_usb_function *f = dev_get_drvdata(dev);
975 struct rndis_function_config *config = f->config;
976 int value;
977
978 if (sscanf(buf, "%d", &value) == 1) {
979 config->wceis = value;
980 return size;
981 }
982 return -EINVAL;
983}
984
985static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
986 rndis_wceis_store);
987
988static ssize_t rndis_ethaddr_show(struct device *dev,
989 struct device_attribute *attr, char *buf)
990{
991 struct android_usb_function *f = dev_get_drvdata(dev);
992 struct rndis_function_config *rndis = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700993
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530994 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Goby1e8ce152011-12-12 13:01:23 -0800995 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
996 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
997}
998
999static ssize_t rndis_ethaddr_store(struct device *dev,
1000 struct device_attribute *attr, const char *buf, size_t size)
1001{
1002 struct android_usb_function *f = dev_get_drvdata(dev);
1003 struct rndis_function_config *rndis = f->config;
1004
1005 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1006 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
1007 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
1008 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
1009 return size;
1010 return -EINVAL;
1011}
1012
1013static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
1014 rndis_ethaddr_store);
1015
1016static ssize_t rndis_vendorID_show(struct device *dev,
1017 struct device_attribute *attr, char *buf)
1018{
1019 struct android_usb_function *f = dev_get_drvdata(dev);
1020 struct rndis_function_config *config = f->config;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001021
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301022 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001023}
1024
1025static ssize_t rndis_vendorID_store(struct device *dev,
1026 struct device_attribute *attr, const char *buf, size_t size)
1027{
1028 struct android_usb_function *f = dev_get_drvdata(dev);
1029 struct rndis_function_config *config = f->config;
1030 int value;
1031
1032 if (sscanf(buf, "%04x", &value) == 1) {
1033 config->vendorID = value;
1034 return size;
1035 }
1036 return -EINVAL;
1037}
1038
1039static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
1040 rndis_vendorID_store);
1041
1042static struct device_attribute *rndis_function_attributes[] = {
1043 &dev_attr_manufacturer,
1044 &dev_attr_wceis,
1045 &dev_attr_ethaddr,
1046 &dev_attr_vendorID,
1047 NULL
1048};
1049
1050static struct android_usb_function rndis_function = {
1051 .name = "rndis",
1052 .init = rndis_function_init,
1053 .cleanup = rndis_function_cleanup,
1054 .bind_config = rndis_function_bind_config,
1055 .unbind_config = rndis_function_unbind_config,
1056 .attributes = rndis_function_attributes,
1057};
1058
1059
1060struct mass_storage_function_config {
1061 struct fsg_config fsg;
1062 struct fsg_common *common;
1063};
1064
1065static int mass_storage_function_init(struct android_usb_function *f,
1066 struct usb_composite_dev *cdev)
1067{
1068 struct mass_storage_function_config *config;
1069 struct fsg_common *common;
1070 int err;
1071
1072 config = kzalloc(sizeof(struct mass_storage_function_config),
1073 GFP_KERNEL);
1074 if (!config)
1075 return -ENOMEM;
1076
1077 config->fsg.nluns = 1;
1078 config->fsg.luns[0].removable = 1;
1079
1080 common = fsg_common_init(NULL, cdev, &config->fsg);
1081 if (IS_ERR(common)) {
1082 kfree(config);
1083 return PTR_ERR(common);
1084 }
1085
1086 err = sysfs_create_link(&f->dev->kobj,
1087 &common->luns[0].dev.kobj,
1088 "lun");
1089 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +05301090 fsg_common_release(&common->ref);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001091 kfree(config);
1092 return err;
1093 }
1094
1095 config->common = common;
1096 f->config = config;
1097 return 0;
1098}
1099
1100static void mass_storage_function_cleanup(struct android_usb_function *f)
1101{
1102 kfree(f->config);
1103 f->config = NULL;
1104}
1105
1106static int mass_storage_function_bind_config(struct android_usb_function *f,
1107 struct usb_configuration *c)
1108{
1109 struct mass_storage_function_config *config = f->config;
1110 return fsg_bind_config(c->cdev, c, config->common);
1111}
1112
1113static ssize_t mass_storage_inquiry_show(struct device *dev,
1114 struct device_attribute *attr, char *buf)
1115{
1116 struct android_usb_function *f = dev_get_drvdata(dev);
1117 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301118 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001119}
1120
1121static ssize_t mass_storage_inquiry_store(struct device *dev,
1122 struct device_attribute *attr, const char *buf, size_t size)
1123{
1124 struct android_usb_function *f = dev_get_drvdata(dev);
1125 struct mass_storage_function_config *config = f->config;
1126 if (size >= sizeof(config->common->inquiry_string))
1127 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301128 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001129 return -EINVAL;
1130 return size;
1131}
1132
1133static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1134 mass_storage_inquiry_show,
1135 mass_storage_inquiry_store);
1136
1137static struct device_attribute *mass_storage_function_attributes[] = {
1138 &dev_attr_inquiry_string,
1139 NULL
1140};
1141
1142static struct android_usb_function mass_storage_function = {
1143 .name = "mass_storage",
1144 .init = mass_storage_function_init,
1145 .cleanup = mass_storage_function_cleanup,
1146 .bind_config = mass_storage_function_bind_config,
1147 .attributes = mass_storage_function_attributes,
1148};
1149
1150
Benoit Gobycf3fc062011-12-19 14:39:37 -08001151static int accessory_function_init(struct android_usb_function *f,
1152 struct usb_composite_dev *cdev)
1153{
1154 return acc_setup();
1155}
1156
1157static void accessory_function_cleanup(struct android_usb_function *f)
1158{
1159 acc_cleanup();
1160}
1161
1162static int accessory_function_bind_config(struct android_usb_function *f,
1163 struct usb_configuration *c)
1164{
1165 return acc_bind_config(c);
1166}
1167
1168static int accessory_function_ctrlrequest(struct android_usb_function *f,
1169 struct usb_composite_dev *cdev,
1170 const struct usb_ctrlrequest *c)
1171{
1172 return acc_ctrlrequest(cdev, c);
1173}
1174
1175static struct android_usb_function accessory_function = {
1176 .name = "accessory",
1177 .init = accessory_function_init,
1178 .cleanup = accessory_function_cleanup,
1179 .bind_config = accessory_function_bind_config,
1180 .ctrlrequest = accessory_function_ctrlrequest,
1181};
1182
1183
Benoit Goby1e8ce152011-12-12 13:01:23 -08001184static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001185 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301186 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301187 &rmnet_sdio_function,
1188 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301189 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301191 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001192 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301193 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001194 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001195 &mtp_function,
1196 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001197 &rndis_function,
1198 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001199 &accessory_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001200 NULL
1201};
1202
Lena Salmand092f2d2012-03-12 17:27:24 +02001203static void android_cleanup_functions(struct android_usb_function **functions)
1204{
1205 struct android_usb_function *f;
1206 struct device_attribute **attrs;
1207 struct device_attribute *attr;
1208
1209 while (*functions) {
1210 f = *functions++;
1211
1212 if (f->dev) {
1213 device_destroy(android_class, f->dev->devt);
1214 kfree(f->dev_name);
1215 } else
1216 continue;
1217
1218 if (f->cleanup)
1219 f->cleanup(f);
1220
1221 attrs = f->attributes;
1222 if (attrs) {
1223 while ((attr = *attrs++))
1224 device_remove_file(f->dev, attr);
1225 }
1226 }
1227}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001228
1229static int android_init_functions(struct android_usb_function **functions,
1230 struct usb_composite_dev *cdev)
1231{
1232 struct android_dev *dev = _android_dev;
1233 struct android_usb_function *f;
1234 struct device_attribute **attrs;
1235 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301236 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001237 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001238
1239 for (; (f = *functions++); index++) {
1240 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001241 if (!f->dev_name) {
1242 err = -ENOMEM;
1243 goto err_out;
1244 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001245 f->dev = device_create(android_class, dev->dev,
1246 MKDEV(0, index), f, f->dev_name);
1247 if (IS_ERR(f->dev)) {
1248 pr_err("%s: Failed to create dev %s", __func__,
1249 f->dev_name);
1250 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001251 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001252 goto err_create;
1253 }
1254
1255 if (f->init) {
1256 err = f->init(f, cdev);
1257 if (err) {
1258 pr_err("%s: Failed to init %s", __func__,
1259 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001260 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001261 }
1262 }
1263
1264 attrs = f->attributes;
1265 if (attrs) {
1266 while ((attr = *attrs++) && !err)
1267 err = device_create_file(f->dev, attr);
1268 }
1269 if (err) {
1270 pr_err("%s: Failed to create function %s attributes",
1271 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001272 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001273 }
1274 }
1275 return 0;
1276
Lena Salmand092f2d2012-03-12 17:27:24 +02001277err_attrs:
1278 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1279 device_remove_file(f->dev, attr);
1280 if (f->cleanup)
1281 f->cleanup(f);
1282err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001283 device_destroy(android_class, f->dev->devt);
1284err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001285 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001286 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001287err_out:
1288 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001289 return err;
1290}
1291
Benoit Goby1e8ce152011-12-12 13:01:23 -08001292static int
1293android_bind_enabled_functions(struct android_dev *dev,
1294 struct usb_configuration *c)
1295{
1296 struct android_usb_function *f;
1297 int ret;
1298
1299 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1300 ret = f->bind_config(f, c);
1301 if (ret) {
1302 pr_err("%s: %s failed", __func__, f->name);
1303 return ret;
1304 }
1305 }
1306 return 0;
1307}
1308
1309static void
1310android_unbind_enabled_functions(struct android_dev *dev,
1311 struct usb_configuration *c)
1312{
1313 struct android_usb_function *f;
1314
1315 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1316 if (f->unbind_config)
1317 f->unbind_config(f, c);
1318 }
1319}
1320
1321static int android_enable_function(struct android_dev *dev, char *name)
1322{
1323 struct android_usb_function **functions = dev->functions;
1324 struct android_usb_function *f;
1325 while ((f = *functions++)) {
1326 if (!strcmp(name, f->name)) {
1327 list_add_tail(&f->enabled_list,
1328 &dev->enabled_functions);
1329 return 0;
1330 }
1331 }
1332 return -EINVAL;
1333}
1334
1335/*-------------------------------------------------------------------------*/
1336/* /sys/class/android_usb/android%d/ interface */
1337
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301338static ssize_t remote_wakeup_show(struct device *pdev,
1339 struct device_attribute *attr, char *buf)
1340{
1341 return snprintf(buf, PAGE_SIZE, "%d\n",
1342 !!(android_config_driver.bmAttributes &
1343 USB_CONFIG_ATT_WAKEUP));
1344}
1345
1346static ssize_t remote_wakeup_store(struct device *pdev,
1347 struct device_attribute *attr, const char *buff, size_t size)
1348{
1349 int enable = 0;
1350
1351 sscanf(buff, "%d", &enable);
1352
1353 pr_debug("android_usb: %s remote wakeup\n",
1354 enable ? "enabling" : "disabling");
1355
1356 if (enable)
1357 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1358 else
1359 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1360
1361 return size;
1362}
1363
Benoit Goby1e8ce152011-12-12 13:01:23 -08001364static ssize_t
1365functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1366{
1367 struct android_dev *dev = dev_get_drvdata(pdev);
1368 struct android_usb_function *f;
1369 char *buff = buf;
1370
1371 mutex_lock(&dev->mutex);
1372
1373 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301374 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001375
1376 mutex_unlock(&dev->mutex);
1377
1378 if (buff != buf)
1379 *(buff-1) = '\n';
1380 return buff - buf;
1381}
1382
1383static ssize_t
1384functions_store(struct device *pdev, struct device_attribute *attr,
1385 const char *buff, size_t size)
1386{
1387 struct android_dev *dev = dev_get_drvdata(pdev);
1388 char *name;
1389 char buf[256], *b;
1390 int err;
1391
1392 mutex_lock(&dev->mutex);
1393
1394 if (dev->enabled) {
1395 mutex_unlock(&dev->mutex);
1396 return -EBUSY;
1397 }
1398
1399 INIT_LIST_HEAD(&dev->enabled_functions);
1400
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301401 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001402 b = strim(buf);
1403
1404 while (b) {
1405 name = strsep(&b, ",");
1406 if (name) {
1407 err = android_enable_function(dev, name);
1408 if (err)
1409 pr_err("android_usb: Cannot enable '%s'", name);
1410 }
1411 }
1412
1413 mutex_unlock(&dev->mutex);
1414
1415 return size;
1416}
1417
1418static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1419 char *buf)
1420{
1421 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001422
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301423 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001424}
1425
1426static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1427 const char *buff, size_t size)
1428{
1429 struct android_dev *dev = dev_get_drvdata(pdev);
1430 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001431 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001432 int enabled = 0;
1433
Benoit Gobycf3fc062011-12-19 14:39:37 -08001434 if (!cdev)
1435 return -ENODEV;
1436
Benoit Goby1e8ce152011-12-12 13:01:23 -08001437 mutex_lock(&dev->mutex);
1438
1439 sscanf(buff, "%d", &enabled);
1440 if (enabled && !dev->enabled) {
1441 cdev->next_string_id = 0;
1442 /*
1443 * Update values in composite driver's copy of
1444 * device descriptor.
1445 */
1446 cdev->desc.idVendor = device_desc.idVendor;
1447 cdev->desc.idProduct = device_desc.idProduct;
1448 cdev->desc.bcdDevice = device_desc.bcdDevice;
1449 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1450 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1451 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001452 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1453 if (f->enable)
1454 f->enable(f);
1455 }
1456 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001457 dev->enabled = true;
1458 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001459 android_disable(dev);
1460 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1461 if (f->disable)
1462 f->disable(f);
1463 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001464 dev->enabled = false;
1465 } else {
1466 pr_err("android_usb: already %s\n",
1467 dev->enabled ? "enabled" : "disabled");
1468 }
1469
1470 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001471
Benoit Gobyaab96812011-04-19 20:37:33 -07001472 return size;
1473}
1474
Ofir Cohen94213a72012-05-03 14:26:32 +03001475static ssize_t pm_qos_show(struct device *pdev,
1476 struct device_attribute *attr, char *buf)
1477{
1478 struct android_dev *dev = dev_get_drvdata(pdev);
1479
1480 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1481}
1482
1483static ssize_t pm_qos_store(struct device *pdev,
1484 struct device_attribute *attr,
1485 const char *buff, size_t size)
1486{
1487 struct android_dev *dev = dev_get_drvdata(pdev);
1488
1489 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1490
Benoit Goby1e8ce152011-12-12 13:01:23 -08001491 return size;
1492}
1493
1494static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1495 char *buf)
1496{
1497 struct android_dev *dev = dev_get_drvdata(pdev);
1498 struct usb_composite_dev *cdev = dev->cdev;
1499 char *state = "DISCONNECTED";
1500 unsigned long flags;
1501
1502 if (!cdev)
1503 goto out;
1504
1505 spin_lock_irqsave(&cdev->lock, flags);
1506 if (cdev->config)
1507 state = "CONFIGURED";
1508 else if (dev->connected)
1509 state = "CONNECTED";
1510 spin_unlock_irqrestore(&cdev->lock, flags);
1511out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301512 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001513}
1514
1515#define DESCRIPTOR_ATTR(field, format_string) \
1516static ssize_t \
1517field ## _show(struct device *dev, struct device_attribute *attr, \
1518 char *buf) \
1519{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301520 return snprintf(buf, PAGE_SIZE, \
1521 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001522} \
1523static ssize_t \
1524field ## _store(struct device *dev, struct device_attribute *attr, \
1525 const char *buf, size_t size) \
1526{ \
1527 int value; \
1528 if (sscanf(buf, format_string, &value) == 1) { \
1529 device_desc.field = value; \
1530 return size; \
1531 } \
1532 return -1; \
1533} \
1534static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1535
1536#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1537static ssize_t \
1538field ## _show(struct device *dev, struct device_attribute *attr, \
1539 char *buf) \
1540{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301541 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001542} \
1543static ssize_t \
1544field ## _store(struct device *dev, struct device_attribute *attr, \
1545 const char *buf, size_t size) \
1546{ \
1547 if (size >= sizeof(buffer)) \
1548 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301549 strlcpy(buffer, buf, sizeof(buffer)); \
1550 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301551 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001552} \
1553static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1554
1555
1556DESCRIPTOR_ATTR(idVendor, "%04x\n")
1557DESCRIPTOR_ATTR(idProduct, "%04x\n")
1558DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1559DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1560DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1561DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1562DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1563DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1564DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1565
1566static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1567 functions_store);
1568static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001569static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1570 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001571static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301572static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1573 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001574
1575static struct device_attribute *android_usb_attributes[] = {
1576 &dev_attr_idVendor,
1577 &dev_attr_idProduct,
1578 &dev_attr_bcdDevice,
1579 &dev_attr_bDeviceClass,
1580 &dev_attr_bDeviceSubClass,
1581 &dev_attr_bDeviceProtocol,
1582 &dev_attr_iManufacturer,
1583 &dev_attr_iProduct,
1584 &dev_attr_iSerial,
1585 &dev_attr_functions,
1586 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001587 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001588 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301589 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001590 NULL
1591};
1592
1593/*-------------------------------------------------------------------------*/
1594/* Composite driver */
1595
1596static int android_bind_config(struct usb_configuration *c)
1597{
1598 struct android_dev *dev = _android_dev;
1599 int ret = 0;
1600
1601 ret = android_bind_enabled_functions(dev, c);
1602 if (ret)
1603 return ret;
1604
1605 return 0;
1606}
1607
1608static void android_unbind_config(struct usb_configuration *c)
1609{
1610 struct android_dev *dev = _android_dev;
1611
1612 android_unbind_enabled_functions(dev, c);
1613}
1614
1615static int android_bind(struct usb_composite_dev *cdev)
1616{
1617 struct android_dev *dev = _android_dev;
1618 struct usb_gadget *gadget = cdev->gadget;
1619 int gcnum, id, ret;
1620
1621 /*
1622 * Start disconnected. Userspace will connect the gadget once
1623 * it is done configuring the functions.
1624 */
1625 usb_gadget_disconnect(gadget);
1626
1627 ret = android_init_functions(dev->functions, cdev);
1628 if (ret)
1629 return ret;
1630
1631 /* Allocate string descriptor numbers ... note that string
1632 * contents can be overridden by the composite_dev glue.
1633 */
1634 id = usb_string_id(cdev);
1635 if (id < 0)
1636 return id;
1637 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1638 device_desc.iManufacturer = id;
1639
1640 id = usb_string_id(cdev);
1641 if (id < 0)
1642 return id;
1643 strings_dev[STRING_PRODUCT_IDX].id = id;
1644 device_desc.iProduct = id;
1645
1646 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301647 strlcpy(manufacturer_string, "Android",
1648 sizeof(manufacturer_string) - 1);
1649 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1650 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001651
1652 id = usb_string_id(cdev);
1653 if (id < 0)
1654 return id;
1655 strings_dev[STRING_SERIAL_IDX].id = id;
1656 device_desc.iSerialNumber = id;
1657
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301658 if (gadget_is_otg(cdev->gadget))
1659 android_config_driver.descriptors = otg_desc;
1660
Benoit Goby1e8ce152011-12-12 13:01:23 -08001661 gcnum = usb_gadget_controller_number(gadget);
1662 if (gcnum >= 0)
1663 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1664 else {
1665 pr_warning("%s: controller '%s' not recognized\n",
1666 longname, gadget->name);
1667 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1668 }
1669
Benoit Goby1e8ce152011-12-12 13:01:23 -08001670 dev->cdev = cdev;
1671
1672 return 0;
1673}
1674
1675static int android_usb_unbind(struct usb_composite_dev *cdev)
1676{
1677 struct android_dev *dev = _android_dev;
1678
Lena Salmand092f2d2012-03-12 17:27:24 +02001679 manufacturer_string[0] = '\0';
1680 product_string[0] = '\0';
1681 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001682 cancel_work_sync(&dev->work);
1683 android_cleanup_functions(dev->functions);
1684 return 0;
1685}
1686
1687static struct usb_composite_driver android_usb_driver = {
1688 .name = "android_usb",
1689 .dev = &device_desc,
1690 .strings = dev_strings,
1691 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001692 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001693};
1694
1695static int
1696android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1697{
1698 struct android_dev *dev = _android_dev;
1699 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1700 struct usb_request *req = cdev->req;
1701 struct android_usb_function *f;
1702 int value = -EOPNOTSUPP;
1703 unsigned long flags;
1704
1705 req->zero = 0;
1706 req->complete = composite_setup_complete;
1707 req->length = 0;
1708 gadget->ep0->driver_data = cdev;
1709
1710 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1711 if (f->ctrlrequest) {
1712 value = f->ctrlrequest(f, cdev, c);
1713 if (value >= 0)
1714 break;
1715 }
1716 }
1717
Benoit Gobycf3fc062011-12-19 14:39:37 -08001718 /* Special case the accessory function.
1719 * It needs to handle control requests before it is enabled.
1720 */
1721 if (value < 0)
1722 value = acc_ctrlrequest(cdev, c);
1723
Benoit Goby1e8ce152011-12-12 13:01:23 -08001724 if (value < 0)
1725 value = composite_setup(gadget, c);
1726
1727 spin_lock_irqsave(&cdev->lock, flags);
1728 if (!dev->connected) {
1729 dev->connected = 1;
1730 schedule_work(&dev->work);
1731 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1732 cdev->config) {
1733 schedule_work(&dev->work);
1734 }
1735 spin_unlock_irqrestore(&cdev->lock, flags);
1736
1737 return value;
1738}
1739
1740static void android_disconnect(struct usb_gadget *gadget)
1741{
1742 struct android_dev *dev = _android_dev;
1743 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1744 unsigned long flags;
1745
1746 composite_disconnect(gadget);
1747
1748 spin_lock_irqsave(&cdev->lock, flags);
1749 dev->connected = 0;
1750 schedule_work(&dev->work);
1751 spin_unlock_irqrestore(&cdev->lock, flags);
1752}
1753
1754static int android_create_device(struct android_dev *dev)
1755{
1756 struct device_attribute **attrs = android_usb_attributes;
1757 struct device_attribute *attr;
1758 int err;
1759
1760 dev->dev = device_create(android_class, NULL,
1761 MKDEV(0, 0), NULL, "android0");
1762 if (IS_ERR(dev->dev))
1763 return PTR_ERR(dev->dev);
1764
1765 dev_set_drvdata(dev->dev, dev);
1766
1767 while ((attr = *attrs++)) {
1768 err = device_create_file(dev->dev, attr);
1769 if (err) {
1770 device_destroy(android_class, dev->dev->devt);
1771 return err;
1772 }
1773 }
1774 return 0;
1775}
1776
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301777static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001778{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301779 struct device_attribute **attrs = android_usb_attributes;
1780 struct device_attribute *attr;
1781
1782 while ((attr = *attrs++))
1783 device_remove_file(dev->dev, attr);
1784 device_destroy(android_class, dev->dev->devt);
1785}
1786
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787static int __devinit android_probe(struct platform_device *pdev)
1788{
1789 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1790 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001791 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001792
1793 dev->pdata = pdata;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001794
1795 android_class = class_create(THIS_MODULE, "android_usb");
1796 if (IS_ERR(android_class))
1797 return PTR_ERR(android_class);
1798
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301799 ret = android_create_device(dev);
1800 if (ret) {
1801 pr_err("%s(): android_create_device failed\n", __func__);
1802 goto err_dev;
1803 }
1804
Lena Salmand092f2d2012-03-12 17:27:24 +02001805 ret = usb_composite_probe(&android_usb_driver, android_bind);
1806 if (ret) {
1807 pr_err("%s(): Failed to register android "
1808 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301809 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001810 }
1811
Ofir Cohen94213a72012-05-03 14:26:32 +03001812 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301813 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001814 pm_qos_add_request(&dev->pm_qos_req_dma,
1815 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1816 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1817
Lena Salmand092f2d2012-03-12 17:27:24 +02001818 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301819err_probe:
1820 android_destroy_device(dev);
1821err_dev:
1822 class_destroy(android_class);
1823 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001824}
1825
1826static int android_remove(struct platform_device *pdev)
1827{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301828 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001829 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301830
1831 android_destroy_device(dev);
1832 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001833 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301834 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001835 pm_qos_remove_request(&dev->pm_qos_req_dma);
1836
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001837 return 0;
1838}
1839
1840static struct platform_driver android_platform_driver = {
1841 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001842 .probe = android_probe,
1843 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001845
1846static int __init init(void)
1847{
1848 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301849 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001850
Benoit Goby1e8ce152011-12-12 13:01:23 -08001851 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301852 if (!dev) {
1853 pr_err("%s(): Failed to alloc memory for android_dev\n",
1854 __func__);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001855 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301856 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001857
Benoit Goby80ba14d2012-03-19 18:56:52 -07001858 dev->disable_depth = 1;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001859 dev->functions = supported_functions;
1860 INIT_LIST_HEAD(&dev->enabled_functions);
1861 INIT_WORK(&dev->work, android_work);
1862 mutex_init(&dev->mutex);
1863
Benoit Goby1e8ce152011-12-12 13:01:23 -08001864 _android_dev = dev;
1865
1866 /* Override composite driver functions */
1867 composite_driver.setup = android_setup;
1868 composite_driver.disconnect = android_disconnect;
1869
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301870 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301871 if (ret) {
1872 pr_err("%s(): Failed to register android"
1873 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301874 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301875 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001876
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301877 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001878}
1879module_init(init);
1880
1881static void __exit cleanup(void)
1882{
Lena Salmand092f2d2012-03-12 17:27:24 +02001883 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001884 kfree(_android_dev);
1885 _android_dev = NULL;
1886}
1887module_exit(cleanup);