blob: 8d58833c14644a63407372841b765f33c274dfb3 [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 *);
101 /* Optional: handle ctrl requests before the device is configured
102 * and/or before the function is enabled */
103 int (*ctrlrequest)(struct android_usb_function *,
104 struct usb_composite_dev *,
105 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500106};
107
Benoit Gobyaab96812011-04-19 20:37:33 -0700108struct android_dev {
109 struct android_usb_function **functions;
110 struct list_head enabled_functions;
111 struct usb_composite_dev *cdev;
112 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 struct android_usb_platform_data *pdata;
Benoit Gobyaab96812011-04-19 20:37:33 -0700114
115 bool enabled;
116 bool connected;
117 bool sw_connected;
118 struct work_struct work;
119};
120
121static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500122static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700123static int android_bind_config(struct usb_configuration *c);
124static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500125
126/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500127#define STRING_MANUFACTURER_IDX 0
128#define STRING_PRODUCT_IDX 1
129#define STRING_SERIAL_IDX 2
130
Benoit Gobyaab96812011-04-19 20:37:33 -0700131static char manufacturer_string[256];
132static char product_string[256];
133static char serial_string[256];
134
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500135/* String Table */
136static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700137 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
138 [STRING_PRODUCT_IDX].s = product_string,
139 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500140 { } /* end of list */
141};
142
143static struct usb_gadget_strings stringtab_dev = {
144 .language = 0x0409, /* en-us */
145 .strings = strings_dev,
146};
147
148static struct usb_gadget_strings *dev_strings[] = {
149 &stringtab_dev,
150 NULL,
151};
152
153static struct usb_device_descriptor device_desc = {
154 .bLength = sizeof(device_desc),
155 .bDescriptorType = USB_DT_DEVICE,
156 .bcdUSB = __constant_cpu_to_le16(0x0200),
157 .bDeviceClass = USB_CLASS_PER_INTERFACE,
158 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
159 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
160 .bcdDevice = __constant_cpu_to_le16(0xffff),
161 .bNumConfigurations = 1,
162};
163
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500164static struct usb_configuration android_config_driver = {
165 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700166 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500167 .bConfigurationValue = 1,
168 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
169 .bMaxPower = 0xFA, /* 500ma */
170};
171
Benoit Gobyaab96812011-04-19 20:37:33 -0700172static void android_work(struct work_struct *data)
173{
174 struct android_dev *dev = container_of(data, struct android_dev, work);
175 struct usb_composite_dev *cdev = dev->cdev;
176 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
177 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
178 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
179 unsigned long flags;
180
181 spin_lock_irqsave(&cdev->lock, flags);
182 if (cdev->config) {
183 spin_unlock_irqrestore(&cdev->lock, flags);
184 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
185 configured);
186 return;
187 }
188 if (dev->connected != dev->sw_connected) {
189 dev->sw_connected = dev->connected;
190 spin_unlock_irqrestore(&cdev->lock, flags);
191 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
192 dev->sw_connected ? connected : disconnected);
193 } else {
194 spin_unlock_irqrestore(&cdev->lock, flags);
195 }
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{
Manu Gautam6925d5c2011-09-27 17:04:37 +0530612 int ret;
613 struct rndis_function_config *rndis;
614
Benoit Gobyaab96812011-04-19 20:37:33 -0700615 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
616 if (!f->config)
617 return -ENOMEM;
Manu Gautam6925d5c2011-09-27 17:04:37 +0530618
619 rndis = f->config;
620 ret = gether_setup_name(cdev->gadget, rndis->ethaddr, "usb");
621 if (ret) {
622 pr_err("%s: gether_setup failed\n", __func__);
623 return ret;
624 }
625
Benoit Gobyaab96812011-04-19 20:37:33 -0700626 return 0;
627}
628
629static void rndis_function_cleanup(struct android_usb_function *f)
630{
Manu Gautam6925d5c2011-09-27 17:04:37 +0530631 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700632 kfree(f->config);
633 f->config = NULL;
634}
635
636static int rndis_function_bind_config(struct android_usb_function *f,
637 struct usb_configuration *c)
638{
Benoit Gobyaab96812011-04-19 20:37:33 -0700639 struct rndis_function_config *rndis = f->config;
640
641 if (!rndis) {
642 pr_err("%s: rndis_pdata\n", __func__);
643 return -1;
644 }
645
646 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
647 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
648 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
649
Benoit Gobyaab96812011-04-19 20:37:33 -0700650 if (rndis->wceis) {
651 /* "Wireless" RNDIS; auto-detected by Windows */
652 rndis_iad_descriptor.bFunctionClass =
653 USB_CLASS_WIRELESS_CONTROLLER;
654 rndis_iad_descriptor.bFunctionSubClass = 0x01;
655 rndis_iad_descriptor.bFunctionProtocol = 0x03;
656 rndis_control_intf.bInterfaceClass =
657 USB_CLASS_WIRELESS_CONTROLLER;
658 rndis_control_intf.bInterfaceSubClass = 0x01;
659 rndis_control_intf.bInterfaceProtocol = 0x03;
660 }
661
662 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
663 rndis->manufacturer);
664}
665
666static void rndis_function_unbind_config(struct android_usb_function *f,
667 struct usb_configuration *c)
668{
Benoit Gobyaab96812011-04-19 20:37:33 -0700669}
670
671static ssize_t rndis_manufacturer_show(struct device *dev,
672 struct device_attribute *attr, char *buf)
673{
674 struct android_usb_function *f = dev_get_drvdata(dev);
675 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530676 return snprintf(buf, PAGE_SIZE, "%s\n", config->manufacturer);
Benoit Gobyaab96812011-04-19 20:37:33 -0700677}
678
679static ssize_t rndis_manufacturer_store(struct device *dev,
680 struct device_attribute *attr, const char *buf, size_t size)
681{
682 struct android_usb_function *f = dev_get_drvdata(dev);
683 struct rndis_function_config *config = f->config;
684
685 if (size >= sizeof(config->manufacturer))
686 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530687 if (sscanf(buf, "%255s", config->manufacturer) == 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700688 return size;
689 return -1;
690}
691
692static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
693 rndis_manufacturer_store);
694
695static ssize_t rndis_wceis_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct android_usb_function *f = dev_get_drvdata(dev);
699 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530700 return snprintf(buf, PAGE_SIZE, "%d\n", config->wceis);
Benoit Gobyaab96812011-04-19 20:37:33 -0700701}
702
703static ssize_t rndis_wceis_store(struct device *dev,
704 struct device_attribute *attr, const char *buf, size_t size)
705{
706 struct android_usb_function *f = dev_get_drvdata(dev);
707 struct rndis_function_config *config = f->config;
708 int value;
709
710 if (sscanf(buf, "%d", &value) == 1) {
711 config->wceis = value;
712 return size;
713 }
714 return -EINVAL;
715}
716
717static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
718 rndis_wceis_store);
719
720static ssize_t rndis_ethaddr_show(struct device *dev,
721 struct device_attribute *attr, char *buf)
722{
723 struct android_usb_function *f = dev_get_drvdata(dev);
724 struct rndis_function_config *rndis = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530725 return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
Benoit Gobyaab96812011-04-19 20:37:33 -0700726 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
727 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
728}
729
730static ssize_t rndis_ethaddr_store(struct device *dev,
731 struct device_attribute *attr, const char *buf, size_t size)
732{
733 struct android_usb_function *f = dev_get_drvdata(dev);
734 struct rndis_function_config *rndis = f->config;
735
736 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
737 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
738 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
739 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
740 return size;
741 return -EINVAL;
742}
743
744static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
745 rndis_ethaddr_store);
746
747static ssize_t rndis_vendorID_show(struct device *dev,
748 struct device_attribute *attr, char *buf)
749{
750 struct android_usb_function *f = dev_get_drvdata(dev);
751 struct rndis_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530752 return snprintf(buf, PAGE_SIZE, "%04x\n", config->vendorID);
Benoit Gobyaab96812011-04-19 20:37:33 -0700753}
754
755static ssize_t rndis_vendorID_store(struct device *dev,
756 struct device_attribute *attr, const char *buf, size_t size)
757{
758 struct android_usb_function *f = dev_get_drvdata(dev);
759 struct rndis_function_config *config = f->config;
760 int value;
761
762 if (sscanf(buf, "%04x", &value) == 1) {
763 config->vendorID = value;
764 return size;
765 }
766 return -EINVAL;
767}
768
769static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
770 rndis_vendorID_store);
771
772static struct device_attribute *rndis_function_attributes[] = {
773 &dev_attr_manufacturer,
774 &dev_attr_wceis,
775 &dev_attr_ethaddr,
776 &dev_attr_vendorID,
777 NULL
778};
779
780static struct android_usb_function rndis_function = {
781 .name = "rndis",
782 .init = rndis_function_init,
783 .cleanup = rndis_function_cleanup,
784 .bind_config = rndis_function_bind_config,
785 .unbind_config = rndis_function_unbind_config,
786 .attributes = rndis_function_attributes,
787};
788
789
790struct mass_storage_function_config {
791 struct fsg_config fsg;
792 struct fsg_common *common;
793};
794
795static int mass_storage_function_init(struct android_usb_function *f,
796 struct usb_composite_dev *cdev)
797{
798 struct mass_storage_function_config *config;
799 struct fsg_common *common;
800 int err;
801
802 config = kzalloc(sizeof(struct mass_storage_function_config),
803 GFP_KERNEL);
804 if (!config)
805 return -ENOMEM;
806
807 config->fsg.nluns = 1;
808 config->fsg.luns[0].removable = 1;
809
810 common = fsg_common_init(NULL, cdev, &config->fsg);
811 if (IS_ERR(common)) {
812 kfree(config);
813 return PTR_ERR(common);
814 }
815
816 err = sysfs_create_link(&f->dev->kobj,
817 &common->luns[0].dev.kobj,
818 "lun");
819 if (err) {
820 kfree(config);
821 return err;
822 }
823
824 config->common = common;
825 f->config = config;
826 return 0;
827}
828
829static void mass_storage_function_cleanup(struct android_usb_function *f)
830{
831 kfree(f->config);
832 f->config = NULL;
833}
834
835static int mass_storage_function_bind_config(struct android_usb_function *f,
836 struct usb_configuration *c)
837{
838 struct mass_storage_function_config *config = f->config;
839 return fsg_bind_config(c->cdev, c, config->common);
840}
841
842static ssize_t mass_storage_inquiry_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
844{
845 struct android_usb_function *f = dev_get_drvdata(dev);
846 struct mass_storage_function_config *config = f->config;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530847 return snprintf(buf, PAGE_SIZE, "%s\n", config->common->inquiry_string);
Benoit Gobyaab96812011-04-19 20:37:33 -0700848}
849
850static ssize_t mass_storage_inquiry_store(struct device *dev,
851 struct device_attribute *attr, const char *buf, size_t size)
852{
853 struct android_usb_function *f = dev_get_drvdata(dev);
854 struct mass_storage_function_config *config = f->config;
855 if (size >= sizeof(config->common->inquiry_string))
856 return -EINVAL;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530857 if (sscanf(buf, "%28s", config->common->inquiry_string) != 1)
Benoit Gobyaab96812011-04-19 20:37:33 -0700858 return -EINVAL;
859 return size;
860}
861
862static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
863 mass_storage_inquiry_show,
864 mass_storage_inquiry_store);
865
866static struct device_attribute *mass_storage_function_attributes[] = {
867 &dev_attr_inquiry_string,
868 NULL
869};
870
871static struct android_usb_function mass_storage_function = {
872 .name = "mass_storage",
873 .init = mass_storage_function_init,
874 .cleanup = mass_storage_function_cleanup,
875 .bind_config = mass_storage_function_bind_config,
876 .attributes = mass_storage_function_attributes,
877};
878
879
880static int accessory_function_init(struct android_usb_function *f,
881 struct usb_composite_dev *cdev)
882{
883 return acc_setup();
884}
885
886static void accessory_function_cleanup(struct android_usb_function *f)
887{
888 acc_cleanup();
889}
890
891static int accessory_function_bind_config(struct android_usb_function *f,
892 struct usb_configuration *c)
893{
894 return acc_bind_config(c);
895}
896
897static int accessory_function_ctrlrequest(struct android_usb_function *f,
898 struct usb_composite_dev *cdev,
899 const struct usb_ctrlrequest *c)
900{
901 return acc_ctrlrequest(cdev, c);
902}
903
904static struct android_usb_function accessory_function = {
905 .name = "accessory",
906 .init = accessory_function_init,
907 .cleanup = accessory_function_cleanup,
908 .bind_config = accessory_function_bind_config,
909 .ctrlrequest = accessory_function_ctrlrequest,
910};
911
912
913static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530914 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +0530915 &rmnet_sdio_function,
916 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530917 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +0530919 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700920 &adb_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921// &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700922 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400923 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700924 &rndis_function,
925 &mass_storage_function,
926 &accessory_function,
927 NULL
928};
929
930
931static int android_init_functions(struct android_usb_function **functions,
932 struct usb_composite_dev *cdev)
933{
934 struct android_dev *dev = _android_dev;
935 struct android_usb_function *f;
936 struct device_attribute **attrs;
937 struct device_attribute *attr;
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +0530938 int err = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700939 int index = 0;
940
941 for (; (f = *functions++); index++) {
942 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
943 f->dev = device_create(android_class, dev->dev,
944 MKDEV(0, index), f, f->dev_name);
945 if (IS_ERR(f->dev)) {
946 pr_err("%s: Failed to create dev %s", __func__,
947 f->dev_name);
948 err = PTR_ERR(f->dev);
949 goto err_create;
950 }
951
952 if (f->init) {
953 err = f->init(f, cdev);
954 if (err) {
955 pr_err("%s: Failed to init %s", __func__,
956 f->name);
957 goto err_out;
958 }
959 }
960
961 attrs = f->attributes;
962 if (attrs) {
963 while ((attr = *attrs++) && !err)
964 err = device_create_file(f->dev, attr);
965 }
966 if (err) {
967 pr_err("%s: Failed to create function %s attributes",
968 __func__, f->name);
969 goto err_out;
970 }
971 }
972 return 0;
973
974err_out:
975 device_destroy(android_class, f->dev->devt);
976err_create:
977 kfree(f->dev_name);
978 return err;
979}
980
981static void android_cleanup_functions(struct android_usb_function **functions)
982{
983 struct android_usb_function *f;
984
985 while (*functions) {
986 f = *functions++;
987
988 if (f->dev) {
989 device_destroy(android_class, f->dev->devt);
990 kfree(f->dev_name);
991 }
992
993 if (f->cleanup)
994 f->cleanup(f);
995 }
996}
997
998static int
999android_bind_enabled_functions(struct android_dev *dev,
1000 struct usb_configuration *c)
1001{
1002 struct android_usb_function *f;
1003 int ret;
1004
1005 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1006 ret = f->bind_config(f, c);
1007 if (ret) {
1008 pr_err("%s: %s failed", __func__, f->name);
1009 return ret;
1010 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301011 }
1012 return 0;
1013}
1014
Benoit Gobyaab96812011-04-19 20:37:33 -07001015static void
1016android_unbind_enabled_functions(struct android_dev *dev,
1017 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301018{
Benoit Gobyaab96812011-04-19 20:37:33 -07001019 struct android_usb_function *f;
1020
1021 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1022 if (f->unbind_config)
1023 f->unbind_config(f, c);
1024 }
1025}
1026
1027static int android_enable_function(struct android_dev *dev, char *name)
1028{
1029 struct android_usb_function **functions = dev->functions;
1030 struct android_usb_function *f;
1031 while ((f = *functions++)) {
1032 if (!strcmp(name, f->name)) {
1033 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301034 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001035 }
1036 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001037 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001038}
1039
Benoit Gobyaab96812011-04-19 20:37:33 -07001040/*-------------------------------------------------------------------------*/
1041/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301042
Benoit Gobyaab96812011-04-19 20:37:33 -07001043static ssize_t
1044functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1045{
1046 struct android_dev *dev = dev_get_drvdata(pdev);
1047 struct android_usb_function *f;
1048 char *buff = buf;
1049
1050 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301051 buff += snprintf(buff, PAGE_SIZE, "%s,", f->name);
Benoit Gobyaab96812011-04-19 20:37:33 -07001052 if (buff != buf)
1053 *(buff-1) = '\n';
1054 return buff - buf;
1055}
1056
1057static ssize_t
1058functions_store(struct device *pdev, struct device_attribute *attr,
1059 const char *buff, size_t size)
1060{
1061 struct android_dev *dev = dev_get_drvdata(pdev);
1062 char *name;
1063 char buf[256], *b;
1064 int err;
1065
1066 INIT_LIST_HEAD(&dev->enabled_functions);
1067
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301068 strlcpy(buf, buff, sizeof(buf));
Benoit Gobyaab96812011-04-19 20:37:33 -07001069 b = strim(buf);
1070
1071 while (b) {
1072 name = strsep(&b, ",");
1073 if (name) {
1074 err = android_enable_function(dev, name);
1075 if (err)
1076 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301077 }
1078 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001079
1080 return size;
1081}
1082
1083static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1084 char *buf)
1085{
1086 struct android_dev *dev = dev_get_drvdata(pdev);
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301087 return snprintf(buf, PAGE_SIZE, "%d\n", dev->enabled);
Benoit Gobyaab96812011-04-19 20:37:33 -07001088}
1089
1090static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1091 const char *buff, size_t size)
1092{
1093 struct android_dev *dev = dev_get_drvdata(pdev);
1094 struct usb_composite_dev *cdev = dev->cdev;
1095 int enabled = 0;
1096
1097 sscanf(buff, "%d", &enabled);
1098 if (enabled && !dev->enabled) {
1099 /* update values in composite driver's copy of device descriptor */
1100 cdev->desc.idVendor = device_desc.idVendor;
1101 cdev->desc.idProduct = device_desc.idProduct;
1102 cdev->desc.bcdDevice = device_desc.bcdDevice;
1103 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1104 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1105 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
Manu Gautam05b9ca42011-10-14 17:12:40 +05301106 if (usb_add_config(cdev, &android_config_driver,
1107 android_bind_config))
1108 return size;
1109
Benoit Gobyaab96812011-04-19 20:37:33 -07001110 usb_gadget_connect(cdev->gadget);
1111 dev->enabled = true;
1112 } else if (!enabled && dev->enabled) {
1113 usb_gadget_disconnect(cdev->gadget);
1114 usb_remove_config(cdev, &android_config_driver);
1115 dev->enabled = false;
1116 } else {
1117 pr_err("android_usb: already %s\n",
1118 dev->enabled ? "enabled" : "disabled");
1119 }
1120 return size;
1121}
1122
1123static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1124 char *buf)
1125{
1126 struct android_dev *dev = dev_get_drvdata(pdev);
1127 struct usb_composite_dev *cdev = dev->cdev;
1128 char *state = "DISCONNECTED";
1129 unsigned long flags;
1130
1131 if (!cdev)
1132 goto out;
1133
1134 spin_lock_irqsave(&cdev->lock, flags);
1135 if (cdev->config)
1136 state = "CONFIGURED";
1137 else if (dev->connected)
1138 state = "CONNECTED";
1139 spin_unlock_irqrestore(&cdev->lock, flags);
1140out:
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301141 return snprintf(buf, PAGE_SIZE, "%s\n", state);
Benoit Gobyaab96812011-04-19 20:37:33 -07001142}
1143
1144#define DESCRIPTOR_ATTR(field, format_string) \
1145static ssize_t \
1146field ## _show(struct device *dev, struct device_attribute *attr, \
1147 char *buf) \
1148{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301149 return snprintf(buf, PAGE_SIZE, \
1150 format_string, device_desc.field); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001151} \
1152static ssize_t \
1153field ## _store(struct device *dev, struct device_attribute *attr, \
1154 const char *buf, size_t size) \
1155{ \
1156 int value; \
1157 if (sscanf(buf, format_string, &value) == 1) { \
1158 device_desc.field = value; \
1159 return size; \
1160 } \
1161 return -1; \
1162} \
1163static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1164
1165#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1166static ssize_t \
1167field ## _show(struct device *dev, struct device_attribute *attr, \
1168 char *buf) \
1169{ \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301170 return snprintf(buf, PAGE_SIZE, "%s", buffer); \
Benoit Gobyaab96812011-04-19 20:37:33 -07001171} \
1172static ssize_t \
1173field ## _store(struct device *dev, struct device_attribute *attr, \
1174 const char *buf, size_t size) \
1175{ \
1176 if (size >= sizeof(buffer)) return -EINVAL; \
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301177 if (sscanf(buf, "%255s", buffer) == 1) { \
Benoit Gobyaab96812011-04-19 20:37:33 -07001178 return size; \
1179 } \
1180 return -1; \
1181} \
1182static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1183
1184
1185DESCRIPTOR_ATTR(idVendor, "%04x\n")
1186DESCRIPTOR_ATTR(idProduct, "%04x\n")
1187DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1188DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1189DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1190DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1191DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1192DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1193DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1194
1195static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1196static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1197static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1198
1199static struct device_attribute *android_usb_attributes[] = {
1200 &dev_attr_idVendor,
1201 &dev_attr_idProduct,
1202 &dev_attr_bcdDevice,
1203 &dev_attr_bDeviceClass,
1204 &dev_attr_bDeviceSubClass,
1205 &dev_attr_bDeviceProtocol,
1206 &dev_attr_iManufacturer,
1207 &dev_attr_iProduct,
1208 &dev_attr_iSerial,
1209 &dev_attr_functions,
1210 &dev_attr_enable,
1211 &dev_attr_state,
1212 NULL
1213};
1214
1215/*-------------------------------------------------------------------------*/
1216/* Composite driver */
1217
1218static int android_bind_config(struct usb_configuration *c)
1219{
1220 struct android_dev *dev = _android_dev;
1221 int ret = 0;
1222
1223 ret = android_bind_enabled_functions(dev, c);
1224 if (ret)
1225 return ret;
1226
1227 return 0;
1228}
1229
1230static void android_unbind_config(struct usb_configuration *c)
1231{
1232 struct android_dev *dev = _android_dev;
1233
1234 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301235}
1236
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001237static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001238{
1239 struct android_dev *dev = _android_dev;
1240 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001241 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001242
Benoit Gobyaab96812011-04-19 20:37:33 -07001243 usb_gadget_disconnect(gadget);
1244
1245 ret = android_init_functions(dev->functions, cdev);
1246 if (ret)
1247 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001248
1249 /* Allocate string descriptor numbers ... note that string
1250 * contents can be overridden by the composite_dev glue.
1251 */
1252 id = usb_string_id(cdev);
1253 if (id < 0)
1254 return id;
1255 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1256 device_desc.iManufacturer = id;
1257
1258 id = usb_string_id(cdev);
1259 if (id < 0)
1260 return id;
1261 strings_dev[STRING_PRODUCT_IDX].id = id;
1262 device_desc.iProduct = id;
1263
Benoit Gobyaab96812011-04-19 20:37:33 -07001264 /* Default strings - should be updated by userspace */
Rajkumar Raghupathy42ec8da2011-10-21 18:58:53 +05301265 strlcpy(manufacturer_string, "Android",
1266 sizeof(manufacturer_string) - 1);
1267 strlcpy(product_string, "Android", sizeof(product_string) - 1);
1268 strlcpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
Benoit Gobyaab96812011-04-19 20:37:33 -07001269
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001270 id = usb_string_id(cdev);
1271 if (id < 0)
1272 return id;
1273 strings_dev[STRING_SERIAL_IDX].id = id;
1274 device_desc.iSerialNumber = id;
1275
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001276 gcnum = usb_gadget_controller_number(gadget);
1277 if (gcnum >= 0)
1278 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1279 else {
1280 /* gadget zero is so simple (for now, no altsettings) that
1281 * it SHOULD NOT have problems with bulk-capable hardware.
1282 * so just warn about unrcognized controllers -- don't panic.
1283 *
1284 * things like configuration and altsetting numbering
1285 * can need hardware-specific attention though.
1286 */
1287 pr_warning("%s: controller '%s' not recognized\n",
1288 longname, gadget->name);
1289 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1290 }
1291
1292 usb_gadget_set_selfpowered(gadget);
1293 dev->cdev = cdev;
1294
1295 return 0;
1296}
1297
Benoit Gobyaab96812011-04-19 20:37:33 -07001298static int android_usb_unbind(struct usb_composite_dev *cdev)
1299{
1300 struct android_dev *dev = _android_dev;
1301
1302 cancel_work_sync(&dev->work);
1303 android_cleanup_functions(dev->functions);
1304 return 0;
1305}
1306
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001307static struct usb_composite_driver android_usb_driver = {
1308 .name = "android_usb",
1309 .dev = &device_desc,
1310 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001311 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001312};
1313
Benoit Gobyaab96812011-04-19 20:37:33 -07001314static int
1315android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1316{
1317 struct android_dev *dev = _android_dev;
1318 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1319 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001320 struct android_usb_function *f;
1321 int value = -EOPNOTSUPP;
1322 unsigned long flags;
1323
1324 req->zero = 0;
1325 req->complete = composite_setup_complete;
1326 req->length = 0;
1327 gadget->ep0->driver_data = cdev;
1328
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001329 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001330 if (f->ctrlrequest) {
1331 value = f->ctrlrequest(f, cdev, c);
1332 if (value >= 0)
1333 break;
1334 }
1335 }
1336
1337 if (value < 0)
1338 value = composite_setup(gadget, c);
1339
1340 spin_lock_irqsave(&cdev->lock, flags);
1341 if (!dev->connected) {
1342 dev->connected = 1;
1343 schedule_work(&dev->work);
1344 }
1345 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1346 schedule_work(&dev->work);
1347 }
1348 spin_unlock_irqrestore(&cdev->lock, flags);
1349
1350 return value;
1351}
1352
1353static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001354{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301355 struct android_dev *dev = _android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001356 dev->connected = 0;
1357 schedule_work(&dev->work);
1358 composite_disconnect(gadget);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301359}
1360
Benoit Gobyaab96812011-04-19 20:37:33 -07001361static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001362{
Benoit Gobyaab96812011-04-19 20:37:33 -07001363 struct device_attribute **attrs = android_usb_attributes;
1364 struct device_attribute *attr;
1365 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001366
Benoit Gobyaab96812011-04-19 20:37:33 -07001367 dev->dev = device_create(android_class, NULL,
1368 MKDEV(0, 0), NULL, "android0");
1369 if (IS_ERR(dev->dev))
1370 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001371
Benoit Gobyaab96812011-04-19 20:37:33 -07001372 dev_set_drvdata(dev->dev, dev);
1373
1374 while ((attr = *attrs++)) {
1375 err = device_create_file(dev->dev, attr);
1376 if (err) {
1377 device_destroy(android_class, dev->dev->devt);
1378 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001379 }
1380 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001381 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001382}
1383
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384static int __devinit android_probe(struct platform_device *pdev)
1385{
1386 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1387 struct android_dev *dev = _android_dev;
1388
1389 dev->pdata = pdata;
1390
1391 return 0;
1392}
1393
1394static struct platform_driver android_platform_driver = {
1395 .driver = { .name = "android_usb"},
1396};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001397
1398static int __init init(void)
1399{
1400 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001401 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001402
Benoit Gobyaab96812011-04-19 20:37:33 -07001403 android_class = class_create(THIS_MODULE, "android_usb");
1404 if (IS_ERR(android_class))
1405 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001406
1407 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1408 if (!dev)
1409 return -ENOMEM;
1410
Benoit Gobyaab96812011-04-19 20:37:33 -07001411 dev->functions = supported_functions;
1412 INIT_LIST_HEAD(&dev->enabled_functions);
1413 INIT_WORK(&dev->work, android_work);
1414
1415 err = android_create_device(dev);
1416 if (err) {
1417 class_destroy(android_class);
1418 kfree(dev);
1419 return err;
1420 }
1421
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001422 _android_dev = dev;
1423
Benoit Gobyaab96812011-04-19 20:37:33 -07001424 /* Override composite driver functions */
1425 composite_driver.setup = android_setup;
1426 composite_driver.disconnect = android_disconnect;
1427
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 platform_driver_probe(&android_platform_driver, android_probe);
1429
Benoit Gobyaab96812011-04-19 20:37:33 -07001430 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001431}
1432module_init(init);
1433
1434static void __exit cleanup(void)
1435{
1436 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001437 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001438 kfree(_android_dev);
1439 _android_dev = NULL;
1440}
1441module_exit(cleanup);