blob: 56bf2b58e8198400e39cd4e8e78720e7ea3f50e2 [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
2 * Gadget Driver for Android
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/fs.h>
24
25#include <linux/delay.h>
26#include <linux/kernel.h>
27#include <linux/utsname.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050028#include <linux/platform_device.h>
29
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050030#include <linux/usb/ch9.h>
31#include <linux/usb/composite.h>
32#include <linux/usb/gadget.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <linux/usb/android.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050034
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050035#include "gadget_chips.h"
36
37/*
38 * Kbuild is not very cooperative with respect to linking separately
39 * compiled library objects into one module. So for now we won't use
40 * separate compilation ... ensuring init/exit sections work to shrink
41 * the runtime footprint, and giving us at least some parts of what
42 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
43 */
44#include "usbstring.c"
45#include "config.c"
46#include "epautoconf.c"
47#include "composite.c"
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "f_diag.c"
Manu Gautam1c8ffd72011-09-02 16:00:49 +053050#include "f_rmnet_smd.c"
Manu Gautam8e0719b2011-09-26 14:47:55 +053051#include "f_rmnet_sdio.c"
52#include "f_rmnet_smd_sdio.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053053#include "f_rmnet.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070054#include "f_mass_storage.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053055#include "u_serial.c"
56#include "u_sdio.c"
57#include "u_smd.c"
58#include "u_bam.c"
Manu Gautam2b0234a2011-09-07 16:47:52 +053059#include "u_rmnet_ctrl_smd.c"
Manu Gautama4d993f2011-08-30 18:25:55 +053060#include "f_serial.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061//#include "f_acm.c"
Benoit Gobyaab96812011-04-19 20:37:33 -070062#include "f_adb.c"
63#include "f_mtp.c"
64#include "f_accessory.c"
65#define USB_ETH_RNDIS y
66#include "f_rndis.c"
67#include "rndis.c"
68#include "u_ether.c"
69
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050070MODULE_AUTHOR("Mike Lockwood");
71MODULE_DESCRIPTION("Android Composite USB Driver");
72MODULE_LICENSE("GPL");
73MODULE_VERSION("1.0");
74
75static const char longname[] = "Gadget Android";
76
Benoit Gobyaab96812011-04-19 20:37:33 -070077/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050078#define VENDOR_ID 0x18D1
79#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050080
Benoit Gobyaab96812011-04-19 20:37:33 -070081struct android_usb_function {
82 char *name;
83 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050084
Benoit Gobyaab96812011-04-19 20:37:33 -070085 struct device *dev;
86 char *dev_name;
87 struct device_attribute **attributes;
88
89 /* for android_dev.enabled_functions */
90 struct list_head enabled_list;
91
92 /* Optional: initialization during gadget bind */
93 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
94 /* Optional: cleanup during gadget unbind */
95 void (*cleanup)(struct android_usb_function *);
96
97 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
98
99 /* Optional: called when the configuration is removed */
100 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
Mike Lockwood686d33a2011-09-07 09:55:12 -0700101 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -0700102 int (*ctrlrequest)(struct android_usb_function *,
103 struct usb_composite_dev *,
104 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500105};
106
Benoit Gobyaab96812011-04-19 20:37:33 -0700107struct android_dev {
108 struct android_usb_function **functions;
109 struct list_head enabled_functions;
110 struct usb_composite_dev *cdev;
111 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700113
114 bool enabled;
115 bool connected;
116 bool sw_connected;
117 struct work_struct work;
118};
119
120static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500121static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700122static int android_bind_config(struct usb_configuration *c);
123static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500124
125/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500126#define STRING_MANUFACTURER_IDX 0
127#define STRING_PRODUCT_IDX 1
128#define STRING_SERIAL_IDX 2
129
Benoit Gobyaab96812011-04-19 20:37:33 -0700130static char manufacturer_string[256];
131static char product_string[256];
132static char serial_string[256];
133
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500134/* String Table */
135static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700136 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
137 [STRING_PRODUCT_IDX].s = product_string,
138 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500139 { } /* end of list */
140};
141
142static struct usb_gadget_strings stringtab_dev = {
143 .language = 0x0409, /* en-us */
144 .strings = strings_dev,
145};
146
147static struct usb_gadget_strings *dev_strings[] = {
148 &stringtab_dev,
149 NULL,
150};
151
152static struct usb_device_descriptor device_desc = {
153 .bLength = sizeof(device_desc),
154 .bDescriptorType = USB_DT_DEVICE,
155 .bcdUSB = __constant_cpu_to_le16(0x0200),
156 .bDeviceClass = USB_CLASS_PER_INTERFACE,
157 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
158 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
159 .bcdDevice = __constant_cpu_to_le16(0xffff),
160 .bNumConfigurations = 1,
161};
162
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500163static struct usb_configuration android_config_driver = {
164 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700165 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500166 .bConfigurationValue = 1,
167 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
168 .bMaxPower = 0xFA, /* 500ma */
169};
170
Benoit Gobyaab96812011-04-19 20:37:33 -0700171static void android_work(struct work_struct *data)
172{
173 struct android_dev *dev = container_of(data, struct android_dev, work);
174 struct usb_composite_dev *cdev = dev->cdev;
175 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
176 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
177 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
Dima Zavinfc753492011-09-14 11:52:45 -0700178 char **uevent_envp = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -0700179 unsigned long flags;
180
181 spin_lock_irqsave(&cdev->lock, flags);
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700182 if (cdev->config)
Dima Zavinfc753492011-09-14 11:52:45 -0700183 uevent_envp = configured;
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700184 else if (dev->connected != dev->sw_connected)
185 uevent_envp = dev->connected ? connected : disconnected;
186 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700187 spin_unlock_irqrestore(&cdev->lock, flags);
188
189 if (uevent_envp) {
190 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
191 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700192 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700193 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
194 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700195 }
196}
197
198
199/*-------------------------------------------------------------------------*/
200/* Supported functions initialization */
201
Manu Gautam8e0719b2011-09-26 14:47:55 +0530202/* RMNET_SMD */
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530203static int rmnet_smd_function_bind_config(struct android_usb_function *f,
204 struct usb_configuration *c)
205{
206 return rmnet_smd_bind_config(c);
207}
208
209static struct android_usb_function rmnet_smd_function = {
210 .name = "rmnet_smd",
211 .bind_config = rmnet_smd_function_bind_config,
212};
213
Manu Gautam8e0719b2011-09-26 14:47:55 +0530214/* RMNET_SDIO */
215static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
216 struct usb_configuration *c)
217{
218 return rmnet_sdio_function_add(c);
219}
220
221static struct android_usb_function rmnet_sdio_function = {
222 .name = "rmnet_sdio",
223 .bind_config = rmnet_sdio_function_bind_config,
224};
225
226/* RMNET_SMD_SDIO */
227static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
228 struct usb_composite_dev *cdev)
229{
230 return rmnet_smd_sdio_init();
231}
232
233static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
234{
235 rmnet_smd_sdio_cleanup();
236}
237
238static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
239 struct usb_configuration *c)
240{
241 return rmnet_smd_sdio_function_add(c);
242}
243
244static struct device_attribute *rmnet_smd_sdio_attributes[] = {
245 &dev_attr_transport, NULL };
246
247static struct android_usb_function rmnet_smd_sdio_function = {
248 .name = "rmnet_smd_sdio",
249 .init = rmnet_smd_sdio_function_init,
250 .cleanup = rmnet_smd_sdio_function_cleanup,
251 .bind_config = rmnet_smd_sdio_bind_config,
252 .attributes = rmnet_smd_sdio_attributes,
253};
254
255/* RMNET - used with BAM */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530256#define MAX_RMNET_INSTANCES 1
257static int rmnet_instances;
258static int rmnet_function_init(struct android_usb_function *f,
259 struct usb_composite_dev *cdev)
260{
261 return frmnet_init_port(MAX_RMNET_INSTANCES);
262}
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530263
Manu Gautame3e897c2011-09-12 17:18:46 +0530264static void rmnet_function_cleanup(struct android_usb_function *f)
265{
266 frmnet_cleanup();
267}
268
Manu Gautam2b0234a2011-09-07 16:47:52 +0530269static int rmnet_function_bind_config(struct android_usb_function *f,
270 struct usb_configuration *c)
271{
272 int i;
273 int ret = 0;
274
275 for (i = 0; i < rmnet_instances; i++) {
276 ret = frmnet_bind_config(c, i);
277 if (ret) {
278 pr_err("Could not bind rmnet%u config\n", i);
279 break;
280 }
281 }
282
283 return ret;
284}
285
286static ssize_t rmnet_instances_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 return snprintf(buf, PAGE_SIZE, "%d\n", rmnet_instances);
290}
291
292static ssize_t rmnet_instances_store(struct device *dev,
293 struct device_attribute *attr, const char *buf, size_t size)
294{
295 int value;
296
297 sscanf(buf, "%d", &value);
298 if (value > MAX_RMNET_INSTANCES)
299 value = MAX_RMNET_INSTANCES;
300 rmnet_instances = value;
301 return size;
302}
303
304static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, rmnet_instances_show,
305 rmnet_instances_store);
306static struct device_attribute *rmnet_function_attributes[] = {
307 &dev_attr_instances, NULL };
308
309static struct android_usb_function rmnet_function = {
310 .name = "rmnet",
311 .init = rmnet_function_init,
Manu Gautame3e897c2011-09-12 17:18:46 +0530312 .cleanup = rmnet_function_cleanup,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530313 .bind_config = rmnet_function_bind_config,
314 .attributes = rmnet_function_attributes,
315};
316
Manu Gautam8e0719b2011-09-26 14:47:55 +0530317/* DIAG */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530318static char diag_clients[32]; /*enabled DIAG clients- "diag[,diag_mdm]" */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319static ssize_t clients_store(
320 struct device *device, struct device_attribute *attr,
321 const char *buff, size_t size)
322{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530323 strlcpy(diag_clients, buff, sizeof(diag_clients));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324
325 return size;
326}
327
328static DEVICE_ATTR(clients, S_IWUSR, NULL, clients_store);
329static struct device_attribute *diag_function_attributes[] =
330 { &dev_attr_clients, NULL };
331
332static int diag_function_init(struct android_usb_function *f,
333 struct usb_composite_dev *cdev)
334{
335 return diag_setup();
336}
337
338static void diag_function_cleanup(struct android_usb_function *f)
339{
340 diag_cleanup();
341}
342
343static int diag_function_bind_config(struct android_usb_function *f,
344 struct usb_configuration *c)
345{
346 char *name;
347 char buf[32], *b;
Manu Gautamc5760302011-08-25 14:30:24 +0530348 int once = 0, err = -1;
Hemant Kumar1136c002011-10-18 22:04:06 -0700349 int (*notify)(uint32_t, const char *) = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530351 strlcpy(buf, diag_clients, sizeof(buf));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 b = strim(buf);
353
354 while (b) {
355 name = strsep(&b, ",");
Manu Gautamc5760302011-08-25 14:30:24 +0530356 /* Allow only first diag channel to update pid and serial no */
Hemant Kumar1136c002011-10-18 22:04:06 -0700357 if (_android_dev->pdata && !once++)
Manu Gautamc5760302011-08-25 14:30:24 +0530358 notify = _android_dev->pdata->update_pid_and_serial_num;
Manu Gautamc5760302011-08-25 14:30:24 +0530359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530361 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 if (err)
363 pr_err("diag: Cannot open channel '%s'", name);
364 }
365 }
366
367 return err;
368}
369
370static struct android_usb_function diag_function = {
371 .name = "diag",
372 .init = diag_function_init,
373 .cleanup = diag_function_cleanup,
374 .bind_config = diag_function_bind_config,
375 .attributes = diag_function_attributes,
376};
377
Manu Gautam8e0719b2011-09-26 14:47:55 +0530378/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530379static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530380static ssize_t serial_transports_store(
381 struct device *device, struct device_attribute *attr,
382 const char *buff, size_t size)
383{
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530384 strlcpy(serial_transports, buff, sizeof(serial_transports));
Manu Gautama4d993f2011-08-30 18:25:55 +0530385
386 return size;
387}
388
389static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
390static struct device_attribute *serial_function_attributes[] =
391 { &dev_attr_transports, NULL };
392
393static void serial_function_cleanup(struct android_usb_function *f)
394{
395 gserial_cleanup();
396}
397
398static int serial_function_bind_config(struct android_usb_function *f,
399 struct usb_configuration *c)
400{
401 char *name;
402 char buf[32], *b;
403 int err = -1, i;
404 static int serial_initialized = 0, ports = 0;
405
406 if (serial_initialized)
407 goto bind_config;
408
409 serial_initialized = 1;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530410 strlcpy(buf, serial_transports, sizeof(buf));
Manu Gautama4d993f2011-08-30 18:25:55 +0530411 b = strim(buf);
412
413 while (b) {
414 name = strsep(&b, ",");
415
416 if (name) {
417 err = gserial_init_port(ports, name);
418 if (err) {
419 pr_err("serial: Cannot open port '%s'", name);
420 goto out;
421 }
422 ports++;
423 }
424 }
425 err = gport_setup(c);
426 if (err) {
427 pr_err("serial: Cannot setup transports");
428 goto out;
429 }
430
431bind_config:
432 for (i = 0; i < ports; i++) {
433 err = gser_bind_config(c, i);
434 if (err) {
435 pr_err("serial: bind_config failed for port %d", i);
436 goto out;
437 }
438 }
439
440out:
441 return err;
442}
443
444static struct android_usb_function serial_function = {
445 .name = "serial",
446 .cleanup = serial_function_cleanup,
447 .bind_config = serial_function_bind_config,
448 .attributes = serial_function_attributes,
449};
450
451
Manu Gautam8e0719b2011-09-26 14:47:55 +0530452/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700453static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
454{
455 return adb_setup();
456}
457
458static void adb_function_cleanup(struct android_usb_function *f)
459{
460 adb_cleanup();
461}
462
463static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
464{
465 return adb_bind_config(c);
466}
467
468static struct android_usb_function adb_function = {
469 .name = "adb",
470 .init = adb_function_init,
471 .cleanup = adb_function_cleanup,
472 .bind_config = adb_function_bind_config,
473};
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475#if 0
Benoit Gobyaab96812011-04-19 20:37:33 -0700476#define MAX_ACM_INSTANCES 4
477struct acm_function_config {
478 int instances;
479};
480
481static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
482{
483 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
484 if (!f->config)
485 return -ENOMEM;
486
487 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
488}
489
490static void acm_function_cleanup(struct android_usb_function *f)
491{
492 gserial_cleanup();
493 kfree(f->config);
494 f->config = NULL;
495}
496
497static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530498{
499 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700500 int ret = 0;
501 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530502
Benoit Gobyaab96812011-04-19 20:37:33 -0700503 for (i = 0; i < config->instances; i++) {
504 ret = acm_bind_config(c, i);
505 if (ret) {
506 pr_err("Could not bind acm%u config\n", i);
507 break;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530508 }
509 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700510
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530511 return ret;
512}
513
Benoit Gobyaab96812011-04-19 20:37:33 -0700514static ssize_t acm_instances_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530516{
Benoit Gobyaab96812011-04-19 20:37:33 -0700517 struct android_usb_function *f = dev_get_drvdata(dev);
518 struct acm_function_config *config = f->config;
519 return sprintf(buf, "%d\n", config->instances);
520}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530521
Benoit Gobyaab96812011-04-19 20:37:33 -0700522static ssize_t acm_instances_store(struct device *dev,
523 struct device_attribute *attr, const char *buf, size_t size)
524{
525 struct android_usb_function *f = dev_get_drvdata(dev);
526 struct acm_function_config *config = f->config;
527 int value;
528
529 sscanf(buf, "%d", &value);
530 if (value > MAX_ACM_INSTANCES)
531 value = MAX_ACM_INSTANCES;
532 config->instances = value;
533 return size;
534}
535
536static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
537static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
538
539static struct android_usb_function acm_function = {
540 .name = "acm",
541 .init = acm_function_init,
542 .cleanup = acm_function_cleanup,
543 .bind_config = acm_function_bind_config,
544 .attributes = acm_function_attributes,
545};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546#endif
Benoit Gobyaab96812011-04-19 20:37:33 -0700547
548static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
549{
550 return mtp_setup();
551}
552
553static void mtp_function_cleanup(struct android_usb_function *f)
554{
555 mtp_cleanup();
556}
557
558static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
559{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400560 return mtp_bind_config(c, false);
561}
562
563static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
564{
565 /* nothing to do - initialization is handled by mtp_function_init */
566 return 0;
567}
568
569static void ptp_function_cleanup(struct android_usb_function *f)
570{
571 /* nothing to do - cleanup is handled by mtp_function_cleanup */
572}
573
574static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
575{
576 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700577}
578
579static int mtp_function_ctrlrequest(struct android_usb_function *f,
580 struct usb_composite_dev *cdev,
581 const struct usb_ctrlrequest *c)
582{
583 return mtp_ctrlrequest(cdev, c);
584}
585
586static struct android_usb_function mtp_function = {
587 .name = "mtp",
588 .init = mtp_function_init,
589 .cleanup = mtp_function_cleanup,
590 .bind_config = mtp_function_bind_config,
591 .ctrlrequest = mtp_function_ctrlrequest,
592};
593
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400594/* PTP function is same as MTP with slightly different interface descriptor */
595static struct android_usb_function ptp_function = {
596 .name = "ptp",
597 .init = ptp_function_init,
598 .cleanup = ptp_function_cleanup,
599 .bind_config = ptp_function_bind_config,
600};
601
Benoit Gobyaab96812011-04-19 20:37:33 -0700602
603struct rndis_function_config {
604 u8 ethaddr[ETH_ALEN];
605 u32 vendorID;
606 char manufacturer[256];
607 bool wceis;
608};
609
610static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
611{
612 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
613 if (!f->config)
614 return -ENOMEM;
615 return 0;
616}
617
618static void rndis_function_cleanup(struct android_usb_function *f)
619{
620 kfree(f->config);
621 f->config = NULL;
622}
623
624static int rndis_function_bind_config(struct android_usb_function *f,
625 struct usb_configuration *c)
626{
Manu Gautamf4741132011-11-25 09:08:53 +0530627 int ret;
Benoit Gobyaab96812011-04-19 20:37:33 -0700628 struct rndis_function_config *rndis = f->config;
629
630 if (!rndis) {
631 pr_err("%s: rndis_pdata\n", __func__);
632 return -1;
633 }
634
635 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
636 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
637 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
638
Manu Gautamf4741132011-11-25 09:08:53 +0530639 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
640 if (ret) {
641 pr_err("%s: gether_setup failed\n", __func__);
642 return ret;
643 }
644
Benoit Gobyaab96812011-04-19 20:37:33 -0700645 if (rndis->wceis) {
646 /* "Wireless" RNDIS; auto-detected by Windows */
647 rndis_iad_descriptor.bFunctionClass =
648 USB_CLASS_WIRELESS_CONTROLLER;
649 rndis_iad_descriptor.bFunctionSubClass = 0x01;
650 rndis_iad_descriptor.bFunctionProtocol = 0x03;
651 rndis_control_intf.bInterfaceClass =
652 USB_CLASS_WIRELESS_CONTROLLER;
653 rndis_control_intf.bInterfaceSubClass = 0x01;
654 rndis_control_intf.bInterfaceProtocol = 0x03;
655 }
656
657 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
658 rndis->manufacturer);
659}
660
661static void rndis_function_unbind_config(struct android_usb_function *f,
662 struct usb_configuration *c)
663{
Manu Gautamf4741132011-11-25 09:08:53 +0530664 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700665}
666
667static ssize_t rndis_manufacturer_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
669{
670 struct android_usb_function *f = dev_get_drvdata(dev);
671 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530672 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700673}
674
675static ssize_t rndis_manufacturer_store(struct device *dev,
676 struct device_attribute *attr, const char *buf, size_t size)
677{
678 struct android_usb_function *f = dev_get_drvdata(dev);
679 struct rndis_function_config *config = f->config;
680
681 if (size >= sizeof(config->manufacturer))
682 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530683 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700684 return size;
685 return -1;
686}
687
688static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
689 rndis_manufacturer_store);
690
691static ssize_t rndis_wceis_show(struct device *dev,
692 struct device_attribute *attr, char *buf)
693{
694 struct android_usb_function *f = dev_get_drvdata(dev);
695 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530696 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700697}
698
699static ssize_t rndis_wceis_store(struct device *dev,
700 struct device_attribute *attr, const char *buf, size_t size)
701{
702 struct android_usb_function *f = dev_get_drvdata(dev);
703 struct rndis_function_config *config = f->config;
704 int value;
705
706 if (sscanf(buf, "%d", &value) == 1) {
707 config->wceis = value;
708 return size;
709 }
710 return -EINVAL;
711}
712
713static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
714 rndis_wceis_store);
715
716static ssize_t rndis_ethaddr_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
718{
719 struct android_usb_function *f = dev_get_drvdata(dev);
720 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530721 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700722 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
723 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
724}
725
726static ssize_t rndis_ethaddr_store(struct device *dev,
727 struct device_attribute *attr, const char *buf, size_t size)
728{
729 struct android_usb_function *f = dev_get_drvdata(dev);
730 struct rndis_function_config *rndis = f->config;
731
732 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
733 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
734 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
735 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
736 return size;
737 return -EINVAL;
738}
739
740static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
741 rndis_ethaddr_store);
742
743static ssize_t rndis_vendorID_show(struct device *dev,
744 struct device_attribute *attr, char *buf)
745{
746 struct android_usb_function *f = dev_get_drvdata(dev);
747 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530748 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700749}
750
751static ssize_t rndis_vendorID_store(struct device *dev,
752 struct device_attribute *attr, const char *buf, size_t size)
753{
754 struct android_usb_function *f = dev_get_drvdata(dev);
755 struct rndis_function_config *config = f->config;
756 int value;
757
758 if (sscanf(buf, "%04x", &value) == 1) {
759 config->vendorID = value;
760 return size;
761 }
762 return -EINVAL;
763}
764
765static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
766 rndis_vendorID_store);
767
768static struct device_attribute *rndis_function_attributes[] = {
769 &dev_attr_manufacturer,
770 &dev_attr_wceis,
771 &dev_attr_ethaddr,
772 &dev_attr_vendorID,
773 NULL
774};
775
776static struct android_usb_function rndis_function = {
777 .name = "rndis",
778 .init = rndis_function_init,
779 .cleanup = rndis_function_cleanup,
780 .bind_config = rndis_function_bind_config,
781 .unbind_config = rndis_function_unbind_config,
782 .attributes = rndis_function_attributes,
783};
784
785
786struct mass_storage_function_config {
787 struct fsg_config fsg;
788 struct fsg_common *common;
789};
790
791static int mass_storage_function_init(struct android_usb_function *f,
792 struct usb_composite_dev *cdev)
793{
794 struct mass_storage_function_config *config;
795 struct fsg_common *common;
796 int err;
797
798 config = kzalloc(sizeof(struct mass_storage_function_config),
799 GFP_KERNEL);
800 if (!config)
801 return -ENOMEM;
802
803 config->fsg.nluns = 1;
804 config->fsg.luns[0].removable = 1;
805
806 common = fsg_common_init(NULL, cdev, &config->fsg);
807 if (IS_ERR(common)) {
808 kfree(config);
809 return PTR_ERR(common);
810 }
811
812 err = sysfs_create_link(&f->dev->kobj,
813 &common->luns[0].dev.kobj,
814 "lun");
815 if (err) {
Rajkumar Raghupathy01f599c2011-10-25 15:32:43 +0530816 fsg_common_release(&common->ref);
Benoit Gobyaab96812011-04-19 20:37:33 -0700817 kfree(config);
818 return err;
819 }
820
821 config->common = common;
822 f->config = config;
823 return 0;
824}
825
826static void mass_storage_function_cleanup(struct android_usb_function *f)
827{
828 kfree(f->config);
829 f->config = NULL;
830}
831
832static int mass_storage_function_bind_config(struct android_usb_function *f,
833 struct usb_configuration *c)
834{
835 struct mass_storage_function_config *config = f->config;
836 return fsg_bind_config(c->cdev, c, config->common);
837}
838
839static ssize_t mass_storage_inquiry_show(struct device *dev,
840 struct device_attribute *attr, char *buf)
841{
842 struct android_usb_function *f = dev_get_drvdata(dev);
843 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530844 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700845}
846
847static ssize_t mass_storage_inquiry_store(struct device *dev,
848 struct device_attribute *attr, const char *buf, size_t size)
849{
850 struct android_usb_function *f = dev_get_drvdata(dev);
851 struct mass_storage_function_config *config = f->config;
852 if (size >= sizeof(config->common->inquiry_string))
853 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530854 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700855 return -EINVAL;
856 return size;
857}
858
859static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
860 mass_storage_inquiry_show,
861 mass_storage_inquiry_store);
862
863static struct device_attribute *mass_storage_function_attributes[] = {
864 &dev_attr_inquiry_string,
865 NULL
866};
867
868static struct android_usb_function mass_storage_function = {
869 .name = "mass_storage",
870 .init = mass_storage_function_init,
871 .cleanup = mass_storage_function_cleanup,
872 .bind_config = mass_storage_function_bind_config,
873 .attributes = mass_storage_function_attributes,
874};
875
876
877static int accessory_function_init(struct android_usb_function *f,
878 struct usb_composite_dev *cdev)
879{
880 return acc_setup();
881}
882
883static void accessory_function_cleanup(struct android_usb_function *f)
884{
885 acc_cleanup();
886}
887
888static int accessory_function_bind_config(struct android_usb_function *f,
889 struct usb_configuration *c)
890{
891 return acc_bind_config(c);
892}
893
894static int accessory_function_ctrlrequest(struct android_usb_function *f,
895 struct usb_composite_dev *cdev,
896 const struct usb_ctrlrequest *c)
897{
898 return acc_ctrlrequest(cdev, c);
899}
900
901static struct android_usb_function accessory_function = {
902 .name = "accessory",
903 .init = accessory_function_init,
904 .cleanup = accessory_function_cleanup,
905 .bind_config = accessory_function_bind_config,
906 .ctrlrequest = accessory_function_ctrlrequest,
907};
908
909
910static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530911 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +0530912 &rmnet_sdio_function,
913 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530914 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +0530916 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700917 &adb_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918// &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700919 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400920 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700921 &rndis_function,
922 &mass_storage_function,
923 &accessory_function,
924 NULL
925};
926
927
928static int android_init_functions(struct android_usb_function **functions,
929 struct usb_composite_dev *cdev)
930{
931 struct android_dev *dev = _android_dev;
932 struct android_usb_function *f;
933 struct device_attribute **attrs;
934 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530935 int err = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700936 int index = 0;
937
938 for (; (f = *functions++); index++) {
939 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
940 f->dev = device_create(android_class, dev->dev,
941 MKDEV(0, index), f, f->dev_name);
942 if (IS_ERR(f->dev)) {
943 pr_err("%s: Failed to create dev %s", __func__,
944 f->dev_name);
945 err = PTR_ERR(f->dev);
946 goto err_create;
947 }
948
949 if (f->init) {
950 err = f->init(f, cdev);
951 if (err) {
952 pr_err("%s: Failed to init %s", __func__,
953 f->name);
954 goto err_out;
955 }
956 }
957
958 attrs = f->attributes;
959 if (attrs) {
960 while ((attr = *attrs++) && !err)
961 err = device_create_file(f->dev, attr);
962 }
963 if (err) {
964 pr_err("%s: Failed to create function %s attributes",
965 __func__, f->name);
966 goto err_out;
967 }
968 }
969 return 0;
970
971err_out:
972 device_destroy(android_class, f->dev->devt);
973err_create:
974 kfree(f->dev_name);
975 return err;
976}
977
978static void android_cleanup_functions(struct android_usb_function **functions)
979{
980 struct android_usb_function *f;
981
982 while (*functions) {
983 f = *functions++;
984
985 if (f->dev) {
986 device_destroy(android_class, f->dev->devt);
987 kfree(f->dev_name);
988 }
989
990 if (f->cleanup)
991 f->cleanup(f);
992 }
993}
994
995static int
996android_bind_enabled_functions(struct android_dev *dev,
997 struct usb_configuration *c)
998{
999 struct android_usb_function *f;
1000 int ret;
1001
1002 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1003 ret = f->bind_config(f, c);
1004 if (ret) {
1005 pr_err("%s: %s failed", __func__, f->name);
1006 return ret;
1007 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301008 }
1009 return 0;
1010}
1011
Benoit Gobyaab96812011-04-19 20:37:33 -07001012static void
1013android_unbind_enabled_functions(struct android_dev *dev,
1014 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301015{
Benoit Gobyaab96812011-04-19 20:37:33 -07001016 struct android_usb_function *f;
1017
1018 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1019 if (f->unbind_config)
1020 f->unbind_config(f, c);
1021 }
1022}
1023
1024static int android_enable_function(struct android_dev *dev, char *name)
1025{
1026 struct android_usb_function **functions = dev->functions;
1027 struct android_usb_function *f;
1028 while ((f = *functions++)) {
1029 if (!strcmp(name, f->name)) {
1030 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301031 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001032 }
1033 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001034 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001035}
1036
Benoit Gobyaab96812011-04-19 20:37:33 -07001037/*-------------------------------------------------------------------------*/
1038/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301039
Benoit Gobyaab96812011-04-19 20:37:33 -07001040static ssize_t
1041functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1042{
1043 struct android_dev *dev = dev_get_drvdata(pdev);
1044 struct android_usb_function *f;
1045 char *buff = buf;
1046
1047 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301048 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobyaab96812011-04-19 20:37:33 -07001049 if (buff != buf)
1050 *(buff-1) = '\n';
1051 return buff - buf;
1052}
1053
1054static ssize_t
1055functions_store(struct device *pdev, struct device_attribute *attr,
1056 const char *buff, size_t size)
1057{
1058 struct android_dev *dev = dev_get_drvdata(pdev);
1059 char *name;
1060 char buf[256], *b;
1061 int err;
1062
1063 INIT_LIST_HEAD(&dev->enabled_functions);
1064
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301065 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001066 b = strim(buf);
1067
1068 while (b) {
1069 name = strsep(&b, ",");
1070 if (name) {
1071 err = android_enable_function(dev, name);
1072 if (err)
1073 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301074 }
1075 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001076
1077 return size;
1078}
1079
1080static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1081 char *buf)
1082{
1083 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301084 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001085}
1086
1087static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1088 const char *buff, size_t size)
1089{
1090 struct android_dev *dev = dev_get_drvdata(pdev);
1091 struct usb_composite_dev *cdev = dev->cdev;
1092 int enabled = 0;
1093
1094 sscanf(buff, "%d", &enabled);
1095 if (enabled && !dev->enabled) {
1096 /* update values in composite driver's copy of device descriptor */
1097 cdev->desc.idVendor = device_desc.idVendor;
1098 cdev->desc.idProduct = device_desc.idProduct;
1099 cdev->desc.bcdDevice = device_desc.bcdDevice;
1100 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1101 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1102 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301103 if (usb_add_config(cdev, &android_config_driver,
1104 android_bind_config))
1105 return size;
1106
Benoit Gobyaab96812011-04-19 20:37:33 -07001107 usb_gadget_connect(cdev->gadget);
1108 dev->enabled = true;
1109 } else if (!enabled && dev->enabled) {
1110 usb_gadget_disconnect(cdev->gadget);
1111 usb_remove_config(cdev, &android_config_driver);
1112 dev->enabled = false;
1113 } else {
1114 pr_err("android_usb: already %s\n",
1115 dev->enabled ? "enabled" : "disabled");
1116 }
1117 return size;
1118}
1119
1120static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1121 char *buf)
1122{
1123 struct android_dev *dev = dev_get_drvdata(pdev);
1124 struct usb_composite_dev *cdev = dev->cdev;
1125 char *state = "DISCONNECTED";
1126 unsigned long flags;
1127
1128 if (!cdev)
1129 goto out;
1130
1131 spin_lock_irqsave(&cdev->lock, flags);
1132 if (cdev->config)
1133 state = "CONFIGURED";
1134 else if (dev->connected)
1135 state = "CONNECTED";
1136 spin_unlock_irqrestore(&cdev->lock, flags);
1137out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301138 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001139}
1140
1141#define DESCRIPTOR_ATTR(field, format_string) \
1142static ssize_t \
1143field ## _show(struct device *dev, struct device_attribute *attr, \
1144 char *buf) \
1145{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301146 return snprintf(buf, PAGE_SIZE, \
1147 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001148} \
1149static ssize_t \
1150field ## _store(struct device *dev, struct device_attribute *attr, \
1151 const char *buf, size_t size) \
1152{ \
1153 int value; \
1154 if (sscanf(buf, format_string, &value) == 1) { \
1155 device_desc.field = value; \
1156 return size; \
1157 } \
1158 return -1; \
1159} \
1160static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1161
1162#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1163static ssize_t \
1164field ## _show(struct device *dev, struct device_attribute *attr, \
1165 char *buf) \
1166{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301167 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001168} \
1169static ssize_t \
1170field ## _store(struct device *dev, struct device_attribute *attr, \
1171 const char *buf, size_t size) \
1172{ \
1173 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301174 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001175 return size; \
1176 } \
1177 return -1; \
1178} \
1179static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1180
1181
1182DESCRIPTOR_ATTR(idVendor, "%04x\n")
1183DESCRIPTOR_ATTR(idProduct, "%04x\n")
1184DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1185DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1186DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1187DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1188DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1189DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1190DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1191
1192static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1193static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1194static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1195
1196static struct device_attribute *android_usb_attributes[] = {
1197 &dev_attr_idVendor,
1198 &dev_attr_idProduct,
1199 &dev_attr_bcdDevice,
1200 &dev_attr_bDeviceClass,
1201 &dev_attr_bDeviceSubClass,
1202 &dev_attr_bDeviceProtocol,
1203 &dev_attr_iManufacturer,
1204 &dev_attr_iProduct,
1205 &dev_attr_iSerial,
1206 &dev_attr_functions,
1207 &dev_attr_enable,
1208 &dev_attr_state,
1209 NULL
1210};
1211
1212/*-------------------------------------------------------------------------*/
1213/* Composite driver */
1214
1215static int android_bind_config(struct usb_configuration *c)
1216{
1217 struct android_dev *dev = _android_dev;
1218 int ret = 0;
1219
1220 ret = android_bind_enabled_functions(dev, c);
1221 if (ret)
1222 return ret;
1223
1224 return 0;
1225}
1226
1227static void android_unbind_config(struct usb_configuration *c)
1228{
1229 struct android_dev *dev = _android_dev;
1230
1231 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301232}
1233
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001234static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001235{
1236 struct android_dev *dev = _android_dev;
1237 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001238 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001239
Benoit Gobyaab96812011-04-19 20:37:33 -07001240 usb_gadget_disconnect(gadget);
1241
1242 ret = android_init_functions(dev->functions, cdev);
1243 if (ret)
1244 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001245
1246 /* Allocate string descriptor numbers ... note that string
1247 * contents can be overridden by the composite_dev glue.
1248 */
1249 id = usb_string_id(cdev);
1250 if (id < 0)
1251 return id;
1252 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1253 device_desc.iManufacturer = id;
1254
1255 id = usb_string_id(cdev);
1256 if (id < 0)
1257 return id;
1258 strings_dev[STRING_PRODUCT_IDX].id = id;
1259 device_desc.iProduct = id;
1260
Benoit Gobyaab96812011-04-19 20:37:33 -07001261 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301262 strlcpy(manufacturer_string, "Android",
1263 sizeof(manufacturer_string) - 1);
1264 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1265 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001266
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001267 id = usb_string_id(cdev);
1268 if (id < 0)
1269 return id;
1270 strings_dev[STRING_SERIAL_IDX].id = id;
1271 device_desc.iSerialNumber = id;
1272
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001273 gcnum = usb_gadget_controller_number(gadget);
1274 if (gcnum >= 0)
1275 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1276 else {
1277 /* gadget zero is so simple (for now, no altsettings) that
1278 * it SHOULD NOT have problems with bulk-capable hardware.
1279 * so just warn about unrcognized controllers -- don't panic.
1280 *
1281 * things like configuration and altsetting numbering
1282 * can need hardware-specific attention though.
1283 */
1284 pr_warning("%s: controller '%s' not recognized\n",
1285 longname, gadget->name);
1286 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1287 }
1288
1289 usb_gadget_set_selfpowered(gadget);
1290 dev->cdev = cdev;
1291
1292 return 0;
1293}
1294
Benoit Gobyaab96812011-04-19 20:37:33 -07001295static int android_usb_unbind(struct usb_composite_dev *cdev)
1296{
1297 struct android_dev *dev = _android_dev;
1298
1299 cancel_work_sync(&dev->work);
1300 android_cleanup_functions(dev->functions);
1301 return 0;
1302}
1303
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001304static struct usb_composite_driver android_usb_driver = {
1305 .name = "android_usb",
1306 .dev = &device_desc,
1307 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001308 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001309};
1310
Benoit Gobyaab96812011-04-19 20:37:33 -07001311static int
1312android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1313{
1314 struct android_dev *dev = _android_dev;
1315 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1316 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001317 struct android_usb_function *f;
1318 int value = -EOPNOTSUPP;
1319 unsigned long flags;
1320
1321 req->zero = 0;
1322 req->complete = composite_setup_complete;
1323 req->length = 0;
1324 gadget->ep0->driver_data = cdev;
1325
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001326 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001327 if (f->ctrlrequest) {
1328 value = f->ctrlrequest(f, cdev, c);
1329 if (value >= 0)
1330 break;
1331 }
1332 }
1333
Mike Lockwood686d33a2011-09-07 09:55:12 -07001334 /* Special case the accessory function.
1335 * It needs to handle control requests before it is enabled.
1336 */
1337 if (value < 0)
1338 value = acc_ctrlrequest(cdev, c);
1339
Benoit Gobyaab96812011-04-19 20:37:33 -07001340 if (value < 0)
1341 value = composite_setup(gadget, c);
1342
1343 spin_lock_irqsave(&cdev->lock, flags);
1344 if (!dev->connected) {
1345 dev->connected = 1;
1346 schedule_work(&dev->work);
1347 }
1348 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1349 schedule_work(&dev->work);
1350 }
1351 spin_unlock_irqrestore(&cdev->lock, flags);
1352
1353 return value;
1354}
1355
1356static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001357{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301358 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001359 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1360 unsigned long flags;
1361
1362 composite_disconnect(gadget);
1363
1364 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001365 dev->connected = 0;
1366 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001367 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301368}
1369
Benoit Gobyaab96812011-04-19 20:37:33 -07001370static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001371{
Benoit Gobyaab96812011-04-19 20:37:33 -07001372 struct device_attribute **attrs = android_usb_attributes;
1373 struct device_attribute *attr;
1374 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001375
Benoit Gobyaab96812011-04-19 20:37:33 -07001376 dev->dev = device_create(android_class, NULL,
1377 MKDEV(0, 0), NULL, "android0");
1378 if (IS_ERR(dev->dev))
1379 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001380
Benoit Gobyaab96812011-04-19 20:37:33 -07001381 dev_set_drvdata(dev->dev, dev);
1382
1383 while ((attr = *attrs++)) {
1384 err = device_create_file(dev->dev, attr);
1385 if (err) {
1386 device_destroy(android_class, dev->dev->devt);
1387 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001388 }
1389 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001390 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001391}
1392
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393static int __devinit android_probe(struct platform_device *pdev)
1394{
1395 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1396 struct android_dev *dev = _android_dev;
1397
1398 dev->pdata = pdata;
1399
1400 return 0;
1401}
1402
1403static struct platform_driver android_platform_driver = {
1404 .driver = { .name = "android_usb"},
1405};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001406
1407static int __init init(void)
1408{
1409 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001410 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001411
Benoit Gobyaab96812011-04-19 20:37:33 -07001412 android_class = class_create(THIS_MODULE, "android_usb");
1413 if (IS_ERR(android_class))
1414 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001415
1416 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1417 if (!dev)
1418 return -ENOMEM;
1419
Benoit Gobyaab96812011-04-19 20:37:33 -07001420 dev->functions = supported_functions;
1421 INIT_LIST_HEAD(&dev->enabled_functions);
1422 INIT_WORK(&dev->work, android_work);
1423
1424 err = android_create_device(dev);
1425 if (err) {
1426 class_destroy(android_class);
1427 kfree(dev);
1428 return err;
1429 }
1430
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001431 _android_dev = dev;
1432
Benoit Gobyaab96812011-04-19 20:37:33 -07001433 /* Override composite driver functions */
1434 composite_driver.setup = android_setup;
1435 composite_driver.disconnect = android_disconnect;
1436
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437 platform_driver_probe(&android_platform_driver, android_probe);
1438
Benoit Gobyaab96812011-04-19 20:37:33 -07001439 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001440}
1441module_init(init);
1442
1443static void __exit cleanup(void)
1444{
1445 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001446 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001447 kfree(_android_dev);
1448 _android_dev = NULL;
1449}
1450module_exit(cleanup);