blob: 2ed642a6d7f950a2b5877405c63680b091438f14 [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{
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301068 struct android_dev *dev = _android_dev;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001069 struct mass_storage_function_config *config;
1070 struct fsg_common *common;
1071 int err;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301072 int i;
1073 const char *name[2];
Benoit Goby1e8ce152011-12-12 13:01:23 -08001074
1075 config = kzalloc(sizeof(struct mass_storage_function_config),
1076 GFP_KERNEL);
1077 if (!config)
1078 return -ENOMEM;
1079
1080 config->fsg.nluns = 1;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301081 name[0] = "lun";
1082 if (dev->pdata->cdrom) {
1083 config->fsg.nluns = 2;
1084 config->fsg.luns[1].cdrom = 1;
1085 config->fsg.luns[1].ro = 1;
1086 config->fsg.luns[1].removable = 1;
1087 name[1] = "lun0";
1088 }
1089
Benoit Goby1e8ce152011-12-12 13:01:23 -08001090 config->fsg.luns[0].removable = 1;
1091
1092 common = fsg_common_init(NULL, cdev, &config->fsg);
1093 if (IS_ERR(common)) {
1094 kfree(config);
1095 return PTR_ERR(common);
1096 }
1097
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301098 for (i = 0; i < config->fsg.nluns; i++) {
1099 err = sysfs_create_link(&f->dev->kobj,
1100 &common->luns[i].dev.kobj,
1101 name[i]);
1102 if (err)
1103 goto error;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001104 }
1105
1106 config->common = common;
1107 f->config = config;
1108 return 0;
Rajkumar Raghupathyc9cb2052012-04-26 13:14:10 +05301109error:
1110 for (; i > 0 ; i--)
1111 sysfs_remove_link(&f->dev->kobj, name[i-1]);
1112
1113 fsg_common_release(&common->ref);
1114 kfree(config);
1115 return err;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001116}
1117
1118static void mass_storage_function_cleanup(struct android_usb_function *f)
1119{
1120 kfree(f->config);
1121 f->config = NULL;
1122}
1123
1124static int mass_storage_function_bind_config(struct android_usb_function *f,
1125 struct usb_configuration *c)
1126{
1127 struct mass_storage_function_config *config = f->config;
1128 return fsg_bind_config(c->cdev, c, config->common);
1129}
1130
1131static ssize_t mass_storage_inquiry_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct android_usb_function *f = dev_get_drvdata(dev);
1135 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301136 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001137}
1138
1139static ssize_t mass_storage_inquiry_store(struct device *dev,
1140 struct device_attribute *attr, const char *buf, size_t size)
1141{
1142 struct android_usb_function *f = dev_get_drvdata(dev);
1143 struct mass_storage_function_config *config = f->config;
1144 if (size >= sizeof(config->common->inquiry_string))
1145 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301146 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001147 return -EINVAL;
1148 return size;
1149}
1150
1151static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
1152 mass_storage_inquiry_show,
1153 mass_storage_inquiry_store);
1154
1155static struct device_attribute *mass_storage_function_attributes[] = {
1156 &dev_attr_inquiry_string,
1157 NULL
1158};
1159
1160static struct android_usb_function mass_storage_function = {
1161 .name = "mass_storage",
1162 .init = mass_storage_function_init,
1163 .cleanup = mass_storage_function_cleanup,
1164 .bind_config = mass_storage_function_bind_config,
1165 .attributes = mass_storage_function_attributes,
1166};
1167
1168
Benoit Gobycf3fc062011-12-19 14:39:37 -08001169static int accessory_function_init(struct android_usb_function *f,
1170 struct usb_composite_dev *cdev)
1171{
1172 return acc_setup();
1173}
1174
1175static void accessory_function_cleanup(struct android_usb_function *f)
1176{
1177 acc_cleanup();
1178}
1179
1180static int accessory_function_bind_config(struct android_usb_function *f,
1181 struct usb_configuration *c)
1182{
1183 return acc_bind_config(c);
1184}
1185
1186static int accessory_function_ctrlrequest(struct android_usb_function *f,
1187 struct usb_composite_dev *cdev,
1188 const struct usb_ctrlrequest *c)
1189{
1190 return acc_ctrlrequest(cdev, c);
1191}
1192
1193static struct android_usb_function accessory_function = {
1194 .name = "accessory",
1195 .init = accessory_function_init,
1196 .cleanup = accessory_function_cleanup,
1197 .bind_config = accessory_function_bind_config,
1198 .ctrlrequest = accessory_function_ctrlrequest,
1199};
1200
1201
Benoit Goby1e8ce152011-12-12 13:01:23 -08001202static struct android_usb_function *supported_functions[] = {
Anna Perela8c991d2012-04-09 16:44:46 +03001203 &mbim_function,
Manu Gautam1c8ffd72011-09-02 16:00:49 +05301204 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +05301205 &rmnet_sdio_function,
1206 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +05301207 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +05301209 &serial_function,
Benoit Goby2b6862d2011-12-19 14:38:41 -08001210 &adb_function,
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301211 &ccid_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001212 &acm_function,
Benoit Gobyf0fbc482011-12-19 14:37:50 -08001213 &mtp_function,
1214 &ptp_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001215 &rndis_function,
1216 &mass_storage_function,
Benoit Gobycf3fc062011-12-19 14:39:37 -08001217 &accessory_function,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001218 NULL
1219};
1220
Lena Salmand092f2d2012-03-12 17:27:24 +02001221static void android_cleanup_functions(struct android_usb_function **functions)
1222{
1223 struct android_usb_function *f;
1224 struct device_attribute **attrs;
1225 struct device_attribute *attr;
1226
1227 while (*functions) {
1228 f = *functions++;
1229
1230 if (f->dev) {
1231 device_destroy(android_class, f->dev->devt);
1232 kfree(f->dev_name);
1233 } else
1234 continue;
1235
1236 if (f->cleanup)
1237 f->cleanup(f);
1238
1239 attrs = f->attributes;
1240 if (attrs) {
1241 while ((attr = *attrs++))
1242 device_remove_file(f->dev, attr);
1243 }
1244 }
1245}
Benoit Goby1e8ce152011-12-12 13:01:23 -08001246
1247static int android_init_functions(struct android_usb_function **functions,
1248 struct usb_composite_dev *cdev)
1249{
1250 struct android_dev *dev = _android_dev;
1251 struct android_usb_function *f;
1252 struct device_attribute **attrs;
1253 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301254 int err = 0;
Lena Salmand092f2d2012-03-12 17:27:24 +02001255 int index = 1; /* index 0 is for android0 device */
Benoit Goby1e8ce152011-12-12 13:01:23 -08001256
1257 for (; (f = *functions++); index++) {
1258 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001259 if (!f->dev_name) {
1260 err = -ENOMEM;
1261 goto err_out;
1262 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001263 f->dev = device_create(android_class, dev->dev,
1264 MKDEV(0, index), f, f->dev_name);
1265 if (IS_ERR(f->dev)) {
1266 pr_err("%s: Failed to create dev %s", __func__,
1267 f->dev_name);
1268 err = PTR_ERR(f->dev);
Lena Salmand092f2d2012-03-12 17:27:24 +02001269 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001270 goto err_create;
1271 }
1272
1273 if (f->init) {
1274 err = f->init(f, cdev);
1275 if (err) {
1276 pr_err("%s: Failed to init %s", __func__,
1277 f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001278 goto err_init;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001279 }
1280 }
1281
1282 attrs = f->attributes;
1283 if (attrs) {
1284 while ((attr = *attrs++) && !err)
1285 err = device_create_file(f->dev, attr);
1286 }
1287 if (err) {
1288 pr_err("%s: Failed to create function %s attributes",
1289 __func__, f->name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001290 goto err_attrs;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001291 }
1292 }
1293 return 0;
1294
Lena Salmand092f2d2012-03-12 17:27:24 +02001295err_attrs:
1296 for (attr = *(attrs -= 2); attrs != f->attributes; attr = *(attrs--))
1297 device_remove_file(f->dev, attr);
1298 if (f->cleanup)
1299 f->cleanup(f);
1300err_init:
Benoit Goby1e8ce152011-12-12 13:01:23 -08001301 device_destroy(android_class, f->dev->devt);
1302err_create:
Lena Salmand092f2d2012-03-12 17:27:24 +02001303 f->dev = NULL;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001304 kfree(f->dev_name);
Lena Salmand092f2d2012-03-12 17:27:24 +02001305err_out:
1306 android_cleanup_functions(dev->functions);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001307 return err;
1308}
1309
Benoit Goby1e8ce152011-12-12 13:01:23 -08001310static int
1311android_bind_enabled_functions(struct android_dev *dev,
1312 struct usb_configuration *c)
1313{
1314 struct android_usb_function *f;
1315 int ret;
1316
1317 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1318 ret = f->bind_config(f, c);
1319 if (ret) {
1320 pr_err("%s: %s failed", __func__, f->name);
1321 return ret;
1322 }
1323 }
1324 return 0;
1325}
1326
1327static void
1328android_unbind_enabled_functions(struct android_dev *dev,
1329 struct usb_configuration *c)
1330{
1331 struct android_usb_function *f;
1332
1333 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1334 if (f->unbind_config)
1335 f->unbind_config(f, c);
1336 }
1337}
1338
1339static int android_enable_function(struct android_dev *dev, char *name)
1340{
1341 struct android_usb_function **functions = dev->functions;
1342 struct android_usb_function *f;
1343 while ((f = *functions++)) {
1344 if (!strcmp(name, f->name)) {
1345 list_add_tail(&f->enabled_list,
1346 &dev->enabled_functions);
1347 return 0;
1348 }
1349 }
1350 return -EINVAL;
1351}
1352
1353/*-------------------------------------------------------------------------*/
1354/* /sys/class/android_usb/android%d/ interface */
1355
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301356static ssize_t remote_wakeup_show(struct device *pdev,
1357 struct device_attribute *attr, char *buf)
1358{
1359 return snprintf(buf, PAGE_SIZE, "%d\n",
1360 !!(android_config_driver.bmAttributes &
1361 USB_CONFIG_ATT_WAKEUP));
1362}
1363
1364static ssize_t remote_wakeup_store(struct device *pdev,
1365 struct device_attribute *attr, const char *buff, size_t size)
1366{
1367 int enable = 0;
1368
1369 sscanf(buff, "%d", &enable);
1370
1371 pr_debug("android_usb: %s remote wakeup\n",
1372 enable ? "enabling" : "disabling");
1373
1374 if (enable)
1375 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1376 else
1377 android_config_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
1378
1379 return size;
1380}
1381
Benoit Goby1e8ce152011-12-12 13:01:23 -08001382static ssize_t
1383functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1384{
1385 struct android_dev *dev = dev_get_drvdata(pdev);
1386 struct android_usb_function *f;
1387 char *buff = buf;
1388
1389 mutex_lock(&dev->mutex);
1390
1391 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301392 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001393
1394 mutex_unlock(&dev->mutex);
1395
1396 if (buff != buf)
1397 *(buff-1) = '\n';
1398 return buff - buf;
1399}
1400
1401static ssize_t
1402functions_store(struct device *pdev, struct device_attribute *attr,
1403 const char *buff, size_t size)
1404{
1405 struct android_dev *dev = dev_get_drvdata(pdev);
1406 char *name;
1407 char buf[256], *b;
1408 int err;
1409
1410 mutex_lock(&dev->mutex);
1411
1412 if (dev->enabled) {
1413 mutex_unlock(&dev->mutex);
1414 return -EBUSY;
1415 }
1416
1417 INIT_LIST_HEAD(&dev->enabled_functions);
1418
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301419 strlcpy(buf, buff, sizeof(buf));
Benoit Goby1e8ce152011-12-12 13:01:23 -08001420 b = strim(buf);
1421
1422 while (b) {
1423 name = strsep(&b, ",");
1424 if (name) {
1425 err = android_enable_function(dev, name);
1426 if (err)
1427 pr_err("android_usb: Cannot enable '%s'", name);
1428 }
1429 }
1430
1431 mutex_unlock(&dev->mutex);
1432
1433 return size;
1434}
1435
1436static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1437 char *buf)
1438{
1439 struct android_dev *dev = dev_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001440
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301441 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001442}
1443
1444static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1445 const char *buff, size_t size)
1446{
1447 struct android_dev *dev = dev_get_drvdata(pdev);
1448 struct usb_composite_dev *cdev = dev->cdev;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001449 struct android_usb_function *f;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001450 int enabled = 0;
1451
Benoit Gobycf3fc062011-12-19 14:39:37 -08001452 if (!cdev)
1453 return -ENODEV;
1454
Benoit Goby1e8ce152011-12-12 13:01:23 -08001455 mutex_lock(&dev->mutex);
1456
1457 sscanf(buff, "%d", &enabled);
1458 if (enabled && !dev->enabled) {
Benoit Goby1e8ce152011-12-12 13:01:23 -08001459 /*
1460 * Update values in composite driver's copy of
1461 * device descriptor.
1462 */
1463 cdev->desc.idVendor = device_desc.idVendor;
1464 cdev->desc.idProduct = device_desc.idProduct;
1465 cdev->desc.bcdDevice = device_desc.bcdDevice;
1466 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1467 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1468 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Benoit Goby80ba14d2012-03-19 18:56:52 -07001469 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1470 if (f->enable)
1471 f->enable(f);
1472 }
1473 android_enable(dev);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001474 dev->enabled = true;
1475 } else if (!enabled && dev->enabled) {
Benoit Goby80ba14d2012-03-19 18:56:52 -07001476 android_disable(dev);
1477 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1478 if (f->disable)
1479 f->disable(f);
1480 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001481 dev->enabled = false;
1482 } else {
1483 pr_err("android_usb: already %s\n",
1484 dev->enabled ? "enabled" : "disabled");
1485 }
1486
1487 mutex_unlock(&dev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001488
Benoit Gobyaab96812011-04-19 20:37:33 -07001489 return size;
1490}
1491
Ofir Cohen94213a72012-05-03 14:26:32 +03001492static ssize_t pm_qos_show(struct device *pdev,
1493 struct device_attribute *attr, char *buf)
1494{
1495 struct android_dev *dev = dev_get_drvdata(pdev);
1496
1497 return snprintf(buf, PAGE_SIZE, "%s\n", dev->pm_qos);
1498}
1499
1500static ssize_t pm_qos_store(struct device *pdev,
1501 struct device_attribute *attr,
1502 const char *buff, size_t size)
1503{
1504 struct android_dev *dev = dev_get_drvdata(pdev);
1505
1506 strlcpy(dev->pm_qos, buff, sizeof(dev->pm_qos));
1507
Benoit Goby1e8ce152011-12-12 13:01:23 -08001508 return size;
1509}
1510
1511static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1512 char *buf)
1513{
1514 struct android_dev *dev = dev_get_drvdata(pdev);
1515 struct usb_composite_dev *cdev = dev->cdev;
1516 char *state = "DISCONNECTED";
1517 unsigned long flags;
1518
1519 if (!cdev)
1520 goto out;
1521
1522 spin_lock_irqsave(&cdev->lock, flags);
1523 if (cdev->config)
1524 state = "CONFIGURED";
1525 else if (dev->connected)
1526 state = "CONNECTED";
1527 spin_unlock_irqrestore(&cdev->lock, flags);
1528out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301529 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001530}
1531
1532#define DESCRIPTOR_ATTR(field, format_string) \
1533static ssize_t \
1534field ## _show(struct device *dev, struct device_attribute *attr, \
1535 char *buf) \
1536{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301537 return snprintf(buf, PAGE_SIZE, \
1538 format_string, device_desc.field); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001539} \
1540static ssize_t \
1541field ## _store(struct device *dev, struct device_attribute *attr, \
1542 const char *buf, size_t size) \
1543{ \
1544 int value; \
1545 if (sscanf(buf, format_string, &value) == 1) { \
1546 device_desc.field = value; \
1547 return size; \
1548 } \
1549 return -1; \
1550} \
1551static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1552
1553#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1554static ssize_t \
1555field ## _show(struct device *dev, struct device_attribute *attr, \
1556 char *buf) \
1557{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301558 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001559} \
1560static ssize_t \
1561field ## _store(struct device *dev, struct device_attribute *attr, \
1562 const char *buf, size_t size) \
1563{ \
1564 if (size >= sizeof(buffer)) \
1565 return -EINVAL; \
Pavankumar Kondetie02a51a2012-06-20 08:52:37 +05301566 strlcpy(buffer, buf, sizeof(buffer)); \
1567 strim(buffer); \
Pavankumar Kondeti4c22c102012-06-15 10:59:05 +05301568 return size; \
Benoit Goby1e8ce152011-12-12 13:01:23 -08001569} \
1570static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1571
1572
1573DESCRIPTOR_ATTR(idVendor, "%04x\n")
1574DESCRIPTOR_ATTR(idProduct, "%04x\n")
1575DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1576DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1577DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1578DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1579DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1580DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1581DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1582
1583static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1584 functions_store);
1585static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
Ofir Cohen94213a72012-05-03 14:26:32 +03001586static DEVICE_ATTR(pm_qos, S_IRUGO | S_IWUSR,
1587 pm_qos_show, pm_qos_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001588static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301589static DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR,
1590 remote_wakeup_show, remote_wakeup_store);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001591
1592static struct device_attribute *android_usb_attributes[] = {
1593 &dev_attr_idVendor,
1594 &dev_attr_idProduct,
1595 &dev_attr_bcdDevice,
1596 &dev_attr_bDeviceClass,
1597 &dev_attr_bDeviceSubClass,
1598 &dev_attr_bDeviceProtocol,
1599 &dev_attr_iManufacturer,
1600 &dev_attr_iProduct,
1601 &dev_attr_iSerial,
1602 &dev_attr_functions,
1603 &dev_attr_enable,
Ofir Cohen94213a72012-05-03 14:26:32 +03001604 &dev_attr_pm_qos,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001605 &dev_attr_state,
Pavankumar Kondeti352396c2011-12-07 13:32:40 +05301606 &dev_attr_remote_wakeup,
Benoit Goby1e8ce152011-12-12 13:01:23 -08001607 NULL
1608};
1609
1610/*-------------------------------------------------------------------------*/
1611/* Composite driver */
1612
1613static int android_bind_config(struct usb_configuration *c)
1614{
1615 struct android_dev *dev = _android_dev;
1616 int ret = 0;
1617
1618 ret = android_bind_enabled_functions(dev, c);
1619 if (ret)
1620 return ret;
1621
1622 return 0;
1623}
1624
1625static void android_unbind_config(struct usb_configuration *c)
1626{
1627 struct android_dev *dev = _android_dev;
1628
1629 android_unbind_enabled_functions(dev, c);
1630}
1631
1632static int android_bind(struct usb_composite_dev *cdev)
1633{
1634 struct android_dev *dev = _android_dev;
1635 struct usb_gadget *gadget = cdev->gadget;
1636 int gcnum, id, ret;
1637
1638 /*
1639 * Start disconnected. Userspace will connect the gadget once
1640 * it is done configuring the functions.
1641 */
1642 usb_gadget_disconnect(gadget);
1643
1644 ret = android_init_functions(dev->functions, cdev);
1645 if (ret)
1646 return ret;
1647
1648 /* Allocate string descriptor numbers ... note that string
1649 * contents can be overridden by the composite_dev glue.
1650 */
1651 id = usb_string_id(cdev);
1652 if (id < 0)
1653 return id;
1654 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1655 device_desc.iManufacturer = id;
1656
1657 id = usb_string_id(cdev);
1658 if (id < 0)
1659 return id;
1660 strings_dev[STRING_PRODUCT_IDX].id = id;
1661 device_desc.iProduct = id;
1662
1663 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301664 strlcpy(manufacturer_string, "Android",
1665 sizeof(manufacturer_string) - 1);
1666 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1667 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001668
1669 id = usb_string_id(cdev);
1670 if (id < 0)
1671 return id;
1672 strings_dev[STRING_SERIAL_IDX].id = id;
1673 device_desc.iSerialNumber = id;
1674
Vijayavardhan Vennapusa56e60522012-02-16 15:40:16 +05301675 if (gadget_is_otg(cdev->gadget))
1676 android_config_driver.descriptors = otg_desc;
1677
Benoit Goby1e8ce152011-12-12 13:01:23 -08001678 gcnum = usb_gadget_controller_number(gadget);
1679 if (gcnum >= 0)
1680 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1681 else {
1682 pr_warning("%s: controller '%s' not recognized\n",
1683 longname, gadget->name);
1684 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1685 }
1686
Benoit Goby1e8ce152011-12-12 13:01:23 -08001687 dev->cdev = cdev;
1688
1689 return 0;
1690}
1691
1692static int android_usb_unbind(struct usb_composite_dev *cdev)
1693{
1694 struct android_dev *dev = _android_dev;
1695
Lena Salmand092f2d2012-03-12 17:27:24 +02001696 manufacturer_string[0] = '\0';
1697 product_string[0] = '\0';
1698 serial_string[0] = '0';
Benoit Goby1e8ce152011-12-12 13:01:23 -08001699 cancel_work_sync(&dev->work);
1700 android_cleanup_functions(dev->functions);
1701 return 0;
1702}
1703
1704static struct usb_composite_driver android_usb_driver = {
1705 .name = "android_usb",
1706 .dev = &device_desc,
1707 .strings = dev_strings,
1708 .unbind = android_usb_unbind,
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001709 .max_speed = USB_SPEED_SUPER
Benoit Goby1e8ce152011-12-12 13:01:23 -08001710};
1711
1712static int
1713android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1714{
1715 struct android_dev *dev = _android_dev;
1716 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1717 struct usb_request *req = cdev->req;
1718 struct android_usb_function *f;
1719 int value = -EOPNOTSUPP;
1720 unsigned long flags;
1721
1722 req->zero = 0;
1723 req->complete = composite_setup_complete;
1724 req->length = 0;
1725 gadget->ep0->driver_data = cdev;
1726
1727 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1728 if (f->ctrlrequest) {
1729 value = f->ctrlrequest(f, cdev, c);
1730 if (value >= 0)
1731 break;
1732 }
1733 }
1734
Benoit Gobycf3fc062011-12-19 14:39:37 -08001735 /* Special case the accessory function.
1736 * It needs to handle control requests before it is enabled.
1737 */
1738 if (value < 0)
1739 value = acc_ctrlrequest(cdev, c);
1740
Benoit Goby1e8ce152011-12-12 13:01:23 -08001741 if (value < 0)
1742 value = composite_setup(gadget, c);
1743
1744 spin_lock_irqsave(&cdev->lock, flags);
1745 if (!dev->connected) {
1746 dev->connected = 1;
1747 schedule_work(&dev->work);
1748 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1749 cdev->config) {
1750 schedule_work(&dev->work);
1751 }
1752 spin_unlock_irqrestore(&cdev->lock, flags);
1753
1754 return value;
1755}
1756
1757static void android_disconnect(struct usb_gadget *gadget)
1758{
1759 struct android_dev *dev = _android_dev;
1760 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1761 unsigned long flags;
1762
1763 composite_disconnect(gadget);
1764
1765 spin_lock_irqsave(&cdev->lock, flags);
1766 dev->connected = 0;
1767 schedule_work(&dev->work);
1768 spin_unlock_irqrestore(&cdev->lock, flags);
1769}
1770
1771static int android_create_device(struct android_dev *dev)
1772{
1773 struct device_attribute **attrs = android_usb_attributes;
1774 struct device_attribute *attr;
1775 int err;
1776
1777 dev->dev = device_create(android_class, NULL,
1778 MKDEV(0, 0), NULL, "android0");
1779 if (IS_ERR(dev->dev))
1780 return PTR_ERR(dev->dev);
1781
1782 dev_set_drvdata(dev->dev, dev);
1783
1784 while ((attr = *attrs++)) {
1785 err = device_create_file(dev->dev, attr);
1786 if (err) {
1787 device_destroy(android_class, dev->dev->devt);
1788 return err;
1789 }
1790 }
1791 return 0;
1792}
1793
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301794static void android_destroy_device(struct android_dev *dev)
Benoit Goby1e8ce152011-12-12 13:01:23 -08001795{
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301796 struct device_attribute **attrs = android_usb_attributes;
1797 struct device_attribute *attr;
1798
1799 while ((attr = *attrs++))
1800 device_remove_file(dev->dev, attr);
1801 device_destroy(android_class, dev->dev->devt);
1802}
1803
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804static int __devinit android_probe(struct platform_device *pdev)
1805{
1806 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1807 struct android_dev *dev = _android_dev;
Lena Salmand092f2d2012-03-12 17:27:24 +02001808 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809
1810 dev->pdata = pdata;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001811
1812 android_class = class_create(THIS_MODULE, "android_usb");
1813 if (IS_ERR(android_class))
1814 return PTR_ERR(android_class);
1815
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301816 ret = android_create_device(dev);
1817 if (ret) {
1818 pr_err("%s(): android_create_device failed\n", __func__);
1819 goto err_dev;
1820 }
1821
Ido Shayevitz3987b8d2012-06-24 13:27:41 +03001822 if (pdata)
1823 composite_driver.usb_core_id = pdata->usb_core_id;
1824
Lena Salmand092f2d2012-03-12 17:27:24 +02001825 ret = usb_composite_probe(&android_usb_driver, android_bind);
1826 if (ret) {
1827 pr_err("%s(): Failed to register android "
1828 "composite driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301829 goto err_probe;
Lena Salmand092f2d2012-03-12 17:27:24 +02001830 }
1831
Ofir Cohen94213a72012-05-03 14:26:32 +03001832 /* pm qos request to prevent apps idle power collapse */
Manu Gautam94dc6142012-05-08 14:35:24 +05301833 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001834 pm_qos_add_request(&dev->pm_qos_req_dma,
1835 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1836 strlcpy(dev->pm_qos, "high", sizeof(dev->pm_qos));
1837
Lena Salmand092f2d2012-03-12 17:27:24 +02001838 return ret;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301839err_probe:
1840 android_destroy_device(dev);
1841err_dev:
1842 class_destroy(android_class);
1843 return ret;
Lena Salmand092f2d2012-03-12 17:27:24 +02001844}
1845
1846static int android_remove(struct platform_device *pdev)
1847{
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301848 struct android_dev *dev = _android_dev;
Ofir Cohen94213a72012-05-03 14:26:32 +03001849 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301850
1851 android_destroy_device(dev);
1852 class_destroy(android_class);
Lena Salmand092f2d2012-03-12 17:27:24 +02001853 usb_composite_unregister(&android_usb_driver);
Manu Gautam94dc6142012-05-08 14:35:24 +05301854 if (pdata && pdata->swfi_latency)
Ofir Cohen94213a72012-05-03 14:26:32 +03001855 pm_qos_remove_request(&dev->pm_qos_req_dma);
1856
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 return 0;
1858}
1859
1860static struct platform_driver android_platform_driver = {
1861 .driver = { .name = "android_usb"},
Lena Salmand092f2d2012-03-12 17:27:24 +02001862 .probe = android_probe,
1863 .remove = android_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001865
1866static int __init init(void)
1867{
1868 struct android_dev *dev;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301869 int ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001870
Benoit Goby1e8ce152011-12-12 13:01:23 -08001871 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301872 if (!dev) {
1873 pr_err("%s(): Failed to alloc memory for android_dev\n",
1874 __func__);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001875 return -ENOMEM;
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301876 }
Benoit Goby1e8ce152011-12-12 13:01:23 -08001877
Benoit Goby80ba14d2012-03-19 18:56:52 -07001878 dev->disable_depth = 1;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001879 dev->functions = supported_functions;
1880 INIT_LIST_HEAD(&dev->enabled_functions);
1881 INIT_WORK(&dev->work, android_work);
1882 mutex_init(&dev->mutex);
1883
Benoit Goby1e8ce152011-12-12 13:01:23 -08001884 _android_dev = dev;
1885
1886 /* Override composite driver functions */
1887 composite_driver.setup = android_setup;
1888 composite_driver.disconnect = android_disconnect;
1889
Pavankumar Kondeti044914d2012-01-31 12:56:13 +05301890 ret = platform_driver_register(&android_platform_driver);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301891 if (ret) {
1892 pr_err("%s(): Failed to register android"
1893 "platform driver\n", __func__);
Rajkumar Raghupathy5d3fc392012-04-11 16:53:19 +05301894 kfree(dev);
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301895 }
Lena Salmand092f2d2012-03-12 17:27:24 +02001896
Rajkumar Raghupathya1df77e2012-01-19 17:45:55 +05301897 return ret;
Benoit Goby1e8ce152011-12-12 13:01:23 -08001898}
1899module_init(init);
1900
1901static void __exit cleanup(void)
1902{
Lena Salmand092f2d2012-03-12 17:27:24 +02001903 platform_driver_unregister(&android_platform_driver);
Benoit Goby1e8ce152011-12-12 13:01:23 -08001904 kfree(_android_dev);
1905 _android_dev = NULL;
1906}
1907module_exit(cleanup);