blob: 20b73a1349b33476a63a8282f1e7eacb6c1089bc [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{
323 strncpy(diag_clients, buff, sizeof(diag_clients));
324
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;
349 int (*notify)(uint32_t, const char *);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350
351 strncpy(buf, diag_clients, sizeof(buf));
352 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 */
357 if (!once++)
358 notify = _android_dev->pdata->update_pid_and_serial_num;
359 else
360 notify = NULL;
361
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 if (name) {
Manu Gautamc5760302011-08-25 14:30:24 +0530363 err = diag_function_add(c, name, notify);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 if (err)
365 pr_err("diag: Cannot open channel '%s'", name);
366 }
367 }
368
369 return err;
370}
371
372static struct android_usb_function diag_function = {
373 .name = "diag",
374 .init = diag_function_init,
375 .cleanup = diag_function_cleanup,
376 .bind_config = diag_function_bind_config,
377 .attributes = diag_function_attributes,
378};
379
Manu Gautam8e0719b2011-09-26 14:47:55 +0530380/* SERIAL */
Manu Gautam2b0234a2011-09-07 16:47:52 +0530381static char serial_transports[32]; /*enabled FSERIAL ports - "tty[,sdio]"*/
Manu Gautama4d993f2011-08-30 18:25:55 +0530382static ssize_t serial_transports_store(
383 struct device *device, struct device_attribute *attr,
384 const char *buff, size_t size)
385{
386 strncpy(serial_transports, buff, sizeof(serial_transports));
387
388 return size;
389}
390
391static DEVICE_ATTR(transports, S_IWUSR, NULL, serial_transports_store);
392static struct device_attribute *serial_function_attributes[] =
393 { &dev_attr_transports, NULL };
394
395static void serial_function_cleanup(struct android_usb_function *f)
396{
397 gserial_cleanup();
398}
399
400static int serial_function_bind_config(struct android_usb_function *f,
401 struct usb_configuration *c)
402{
403 char *name;
404 char buf[32], *b;
405 int err = -1, i;
406 static int serial_initialized = 0, ports = 0;
407
408 if (serial_initialized)
409 goto bind_config;
410
411 serial_initialized = 1;
412 strncpy(buf, serial_transports, sizeof(buf));
413 b = strim(buf);
414
415 while (b) {
416 name = strsep(&b, ",");
417
418 if (name) {
419 err = gserial_init_port(ports, name);
420 if (err) {
421 pr_err("serial: Cannot open port '%s'", name);
422 goto out;
423 }
424 ports++;
425 }
426 }
427 err = gport_setup(c);
428 if (err) {
429 pr_err("serial: Cannot setup transports");
430 goto out;
431 }
432
433bind_config:
434 for (i = 0; i < ports; i++) {
435 err = gser_bind_config(c, i);
436 if (err) {
437 pr_err("serial: bind_config failed for port %d", i);
438 goto out;
439 }
440 }
441
442out:
443 return err;
444}
445
446static struct android_usb_function serial_function = {
447 .name = "serial",
448 .cleanup = serial_function_cleanup,
449 .bind_config = serial_function_bind_config,
450 .attributes = serial_function_attributes,
451};
452
453
Manu Gautam8e0719b2011-09-26 14:47:55 +0530454/* ADB */
Benoit Gobyaab96812011-04-19 20:37:33 -0700455static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
456{
457 return adb_setup();
458}
459
460static void adb_function_cleanup(struct android_usb_function *f)
461{
462 adb_cleanup();
463}
464
465static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
466{
467 return adb_bind_config(c);
468}
469
470static struct android_usb_function adb_function = {
471 .name = "adb",
472 .init = adb_function_init,
473 .cleanup = adb_function_cleanup,
474 .bind_config = adb_function_bind_config,
475};
476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477#if 0
Benoit Gobyaab96812011-04-19 20:37:33 -0700478#define MAX_ACM_INSTANCES 4
479struct acm_function_config {
480 int instances;
481};
482
483static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
484{
485 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
486 if (!f->config)
487 return -ENOMEM;
488
489 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
490}
491
492static void acm_function_cleanup(struct android_usb_function *f)
493{
494 gserial_cleanup();
495 kfree(f->config);
496 f->config = NULL;
497}
498
499static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530500{
501 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700502 int ret = 0;
503 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530504
Benoit Gobyaab96812011-04-19 20:37:33 -0700505 for (i = 0; i < config->instances; i++) {
506 ret = acm_bind_config(c, i);
507 if (ret) {
508 pr_err("Could not bind acm%u config\n", i);
509 break;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530510 }
511 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700512
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530513 return ret;
514}
515
Benoit Gobyaab96812011-04-19 20:37:33 -0700516static ssize_t acm_instances_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530518{
Benoit Gobyaab96812011-04-19 20:37:33 -0700519 struct android_usb_function *f = dev_get_drvdata(dev);
520 struct acm_function_config *config = f->config;
521 return sprintf(buf, "%d\n", config->instances);
522}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530523
Benoit Gobyaab96812011-04-19 20:37:33 -0700524static ssize_t acm_instances_store(struct device *dev,
525 struct device_attribute *attr, const char *buf, size_t size)
526{
527 struct android_usb_function *f = dev_get_drvdata(dev);
528 struct acm_function_config *config = f->config;
529 int value;
530
531 sscanf(buf, "%d", &value);
532 if (value > MAX_ACM_INSTANCES)
533 value = MAX_ACM_INSTANCES;
534 config->instances = value;
535 return size;
536}
537
538static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
539static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
540
541static struct android_usb_function acm_function = {
542 .name = "acm",
543 .init = acm_function_init,
544 .cleanup = acm_function_cleanup,
545 .bind_config = acm_function_bind_config,
546 .attributes = acm_function_attributes,
547};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548#endif
Benoit Gobyaab96812011-04-19 20:37:33 -0700549
550static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
551{
552 return mtp_setup();
553}
554
555static void mtp_function_cleanup(struct android_usb_function *f)
556{
557 mtp_cleanup();
558}
559
560static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
561{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400562 return mtp_bind_config(c, false);
563}
564
565static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
566{
567 /* nothing to do - initialization is handled by mtp_function_init */
568 return 0;
569}
570
571static void ptp_function_cleanup(struct android_usb_function *f)
572{
573 /* nothing to do - cleanup is handled by mtp_function_cleanup */
574}
575
576static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
577{
578 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700579}
580
581static int mtp_function_ctrlrequest(struct android_usb_function *f,
582 struct usb_composite_dev *cdev,
583 const struct usb_ctrlrequest *c)
584{
585 return mtp_ctrlrequest(cdev, c);
586}
587
588static struct android_usb_function mtp_function = {
589 .name = "mtp",
590 .init = mtp_function_init,
591 .cleanup = mtp_function_cleanup,
592 .bind_config = mtp_function_bind_config,
593 .ctrlrequest = mtp_function_ctrlrequest,
594};
595
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400596/* PTP function is same as MTP with slightly different interface descriptor */
597static struct android_usb_function ptp_function = {
598 .name = "ptp",
599 .init = ptp_function_init,
600 .cleanup = ptp_function_cleanup,
601 .bind_config = ptp_function_bind_config,
602};
603
Benoit Gobyaab96812011-04-19 20:37:33 -0700604
605struct rndis_function_config {
606 u8 ethaddr[ETH_ALEN];
607 u32 vendorID;
608 char manufacturer[256];
609 bool wceis;
610};
611
612static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
613{
Manu Gautam6925d5c2011-09-27 17:04:37 +0530614 int ret;
615 struct rndis_function_config *rndis;
616
Benoit Gobyaab96812011-04-19 20:37:33 -0700617 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
618 if (!f->config)
619 return -ENOMEM;
Manu Gautam6925d5c2011-09-27 17:04:37 +0530620
621 rndis = f->config;
622 ret = gether_setup_name(cdev->gadget, rndis->ethaddr, "usb");
623 if (ret) {
624 pr_err("%s: gether_setup failed\n", __func__);
625 return ret;
626 }
627
Benoit Gobyaab96812011-04-19 20:37:33 -0700628 return 0;
629}
630
631static void rndis_function_cleanup(struct android_usb_function *f)
632{
Manu Gautam6925d5c2011-09-27 17:04:37 +0530633 gether_cleanup();
Benoit Gobyaab96812011-04-19 20:37:33 -0700634 kfree(f->config);
635 f->config = NULL;
636}
637
638static int rndis_function_bind_config(struct android_usb_function *f,
639 struct usb_configuration *c)
640{
Benoit Gobyaab96812011-04-19 20:37:33 -0700641 struct rndis_function_config *rndis = f->config;
642
643 if (!rndis) {
644 pr_err("%s: rndis_pdata\n", __func__);
645 return -1;
646 }
647
648 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
649 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
650 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
651
Benoit Gobyaab96812011-04-19 20:37:33 -0700652 if (rndis->wceis) {
653 /* "Wireless" RNDIS; auto-detected by Windows */
654 rndis_iad_descriptor.bFunctionClass =
655 USB_CLASS_WIRELESS_CONTROLLER;
656 rndis_iad_descriptor.bFunctionSubClass = 0x01;
657 rndis_iad_descriptor.bFunctionProtocol = 0x03;
658 rndis_control_intf.bInterfaceClass =
659 USB_CLASS_WIRELESS_CONTROLLER;
660 rndis_control_intf.bInterfaceSubClass = 0x01;
661 rndis_control_intf.bInterfaceProtocol = 0x03;
662 }
663
664 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
665 rndis->manufacturer);
666}
667
668static void rndis_function_unbind_config(struct android_usb_function *f,
669 struct usb_configuration *c)
670{
Benoit Gobyaab96812011-04-19 20:37:33 -0700671}
672
673static ssize_t rndis_manufacturer_show(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
676 struct android_usb_function *f = dev_get_drvdata(dev);
677 struct rndis_function_config *config = f->config;
678 return sprintf(buf, "%s\n", config->manufacturer);
679}
680
681static ssize_t rndis_manufacturer_store(struct device *dev,
682 struct device_attribute *attr, const char *buf, size_t size)
683{
684 struct android_usb_function *f = dev_get_drvdata(dev);
685 struct rndis_function_config *config = f->config;
686
687 if (size >= sizeof(config->manufacturer))
688 return -EINVAL;
689 if (sscanf(buf, "%s", config->manufacturer) == 1)
690 return size;
691 return -1;
692}
693
694static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
695 rndis_manufacturer_store);
696
697static ssize_t rndis_wceis_show(struct device *dev,
698 struct device_attribute *attr, char *buf)
699{
700 struct android_usb_function *f = dev_get_drvdata(dev);
701 struct rndis_function_config *config = f->config;
702 return sprintf(buf, "%d\n", config->wceis);
703}
704
705static ssize_t rndis_wceis_store(struct device *dev,
706 struct device_attribute *attr, const char *buf, size_t size)
707{
708 struct android_usb_function *f = dev_get_drvdata(dev);
709 struct rndis_function_config *config = f->config;
710 int value;
711
712 if (sscanf(buf, "%d", &value) == 1) {
713 config->wceis = value;
714 return size;
715 }
716 return -EINVAL;
717}
718
719static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
720 rndis_wceis_store);
721
722static ssize_t rndis_ethaddr_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
724{
725 struct android_usb_function *f = dev_get_drvdata(dev);
726 struct rndis_function_config *rndis = f->config;
727 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
728 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
729 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
730}
731
732static ssize_t rndis_ethaddr_store(struct device *dev,
733 struct device_attribute *attr, const char *buf, size_t size)
734{
735 struct android_usb_function *f = dev_get_drvdata(dev);
736 struct rndis_function_config *rndis = f->config;
737
738 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
739 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
740 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
741 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
742 return size;
743 return -EINVAL;
744}
745
746static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
747 rndis_ethaddr_store);
748
749static ssize_t rndis_vendorID_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
751{
752 struct android_usb_function *f = dev_get_drvdata(dev);
753 struct rndis_function_config *config = f->config;
754 return sprintf(buf, "%04x\n", config->vendorID);
755}
756
757static ssize_t rndis_vendorID_store(struct device *dev,
758 struct device_attribute *attr, const char *buf, size_t size)
759{
760 struct android_usb_function *f = dev_get_drvdata(dev);
761 struct rndis_function_config *config = f->config;
762 int value;
763
764 if (sscanf(buf, "%04x", &value) == 1) {
765 config->vendorID = value;
766 return size;
767 }
768 return -EINVAL;
769}
770
771static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
772 rndis_vendorID_store);
773
774static struct device_attribute *rndis_function_attributes[] = {
775 &dev_attr_manufacturer,
776 &dev_attr_wceis,
777 &dev_attr_ethaddr,
778 &dev_attr_vendorID,
779 NULL
780};
781
782static struct android_usb_function rndis_function = {
783 .name = "rndis",
784 .init = rndis_function_init,
785 .cleanup = rndis_function_cleanup,
786 .bind_config = rndis_function_bind_config,
787 .unbind_config = rndis_function_unbind_config,
788 .attributes = rndis_function_attributes,
789};
790
791
792struct mass_storage_function_config {
793 struct fsg_config fsg;
794 struct fsg_common *common;
795};
796
797static int mass_storage_function_init(struct android_usb_function *f,
798 struct usb_composite_dev *cdev)
799{
800 struct mass_storage_function_config *config;
801 struct fsg_common *common;
802 int err;
803
804 config = kzalloc(sizeof(struct mass_storage_function_config),
805 GFP_KERNEL);
806 if (!config)
807 return -ENOMEM;
808
809 config->fsg.nluns = 1;
810 config->fsg.luns[0].removable = 1;
811
812 common = fsg_common_init(NULL, cdev, &config->fsg);
813 if (IS_ERR(common)) {
814 kfree(config);
815 return PTR_ERR(common);
816 }
817
818 err = sysfs_create_link(&f->dev->kobj,
819 &common->luns[0].dev.kobj,
820 "lun");
821 if (err) {
822 kfree(config);
823 return err;
824 }
825
826 config->common = common;
827 f->config = config;
828 return 0;
829}
830
831static void mass_storage_function_cleanup(struct android_usb_function *f)
832{
833 kfree(f->config);
834 f->config = NULL;
835}
836
837static int mass_storage_function_bind_config(struct android_usb_function *f,
838 struct usb_configuration *c)
839{
840 struct mass_storage_function_config *config = f->config;
841 return fsg_bind_config(c->cdev, c, config->common);
842}
843
844static ssize_t mass_storage_inquiry_show(struct device *dev,
845 struct device_attribute *attr, char *buf)
846{
847 struct android_usb_function *f = dev_get_drvdata(dev);
848 struct mass_storage_function_config *config = f->config;
849 return sprintf(buf, "%s\n", config->common->inquiry_string);
850}
851
852static ssize_t mass_storage_inquiry_store(struct device *dev,
853 struct device_attribute *attr, const char *buf, size_t size)
854{
855 struct android_usb_function *f = dev_get_drvdata(dev);
856 struct mass_storage_function_config *config = f->config;
857 if (size >= sizeof(config->common->inquiry_string))
858 return -EINVAL;
859 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
860 return -EINVAL;
861 return size;
862}
863
864static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
865 mass_storage_inquiry_show,
866 mass_storage_inquiry_store);
867
868static struct device_attribute *mass_storage_function_attributes[] = {
869 &dev_attr_inquiry_string,
870 NULL
871};
872
873static struct android_usb_function mass_storage_function = {
874 .name = "mass_storage",
875 .init = mass_storage_function_init,
876 .cleanup = mass_storage_function_cleanup,
877 .bind_config = mass_storage_function_bind_config,
878 .attributes = mass_storage_function_attributes,
879};
880
881
882static int accessory_function_init(struct android_usb_function *f,
883 struct usb_composite_dev *cdev)
884{
885 return acc_setup();
886}
887
888static void accessory_function_cleanup(struct android_usb_function *f)
889{
890 acc_cleanup();
891}
892
893static int accessory_function_bind_config(struct android_usb_function *f,
894 struct usb_configuration *c)
895{
896 return acc_bind_config(c);
897}
898
899static int accessory_function_ctrlrequest(struct android_usb_function *f,
900 struct usb_composite_dev *cdev,
901 const struct usb_ctrlrequest *c)
902{
903 return acc_ctrlrequest(cdev, c);
904}
905
906static struct android_usb_function accessory_function = {
907 .name = "accessory",
908 .init = accessory_function_init,
909 .cleanup = accessory_function_cleanup,
910 .bind_config = accessory_function_bind_config,
911 .ctrlrequest = accessory_function_ctrlrequest,
912};
913
914
915static struct android_usb_function *supported_functions[] = {
Manu Gautam1c8ffd72011-09-02 16:00:49 +0530916 &rmnet_smd_function,
Manu Gautam8e0719b2011-09-26 14:47:55 +0530917 &rmnet_sdio_function,
918 &rmnet_smd_sdio_function,
Manu Gautam2b0234a2011-09-07 16:47:52 +0530919 &rmnet_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 &diag_function,
Manu Gautama4d993f2011-08-30 18:25:55 +0530921 &serial_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700922 &adb_function,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923// &acm_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700924 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400925 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700926 &rndis_function,
927 &mass_storage_function,
928 &accessory_function,
929 NULL
930};
931
932
933static int android_init_functions(struct android_usb_function **functions,
934 struct usb_composite_dev *cdev)
935{
936 struct android_dev *dev = _android_dev;
937 struct android_usb_function *f;
938 struct device_attribute **attrs;
939 struct device_attribute *attr;
940 int err;
941 int index = 0;
942
943 for (; (f = *functions++); index++) {
944 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
945 f->dev = device_create(android_class, dev->dev,
946 MKDEV(0, index), f, f->dev_name);
947 if (IS_ERR(f->dev)) {
948 pr_err("%s: Failed to create dev %s", __func__,
949 f->dev_name);
950 err = PTR_ERR(f->dev);
951 goto err_create;
952 }
953
954 if (f->init) {
955 err = f->init(f, cdev);
956 if (err) {
957 pr_err("%s: Failed to init %s", __func__,
958 f->name);
959 goto err_out;
960 }
961 }
962
963 attrs = f->attributes;
964 if (attrs) {
965 while ((attr = *attrs++) && !err)
966 err = device_create_file(f->dev, attr);
967 }
968 if (err) {
969 pr_err("%s: Failed to create function %s attributes",
970 __func__, f->name);
971 goto err_out;
972 }
973 }
974 return 0;
975
976err_out:
977 device_destroy(android_class, f->dev->devt);
978err_create:
979 kfree(f->dev_name);
980 return err;
981}
982
983static void android_cleanup_functions(struct android_usb_function **functions)
984{
985 struct android_usb_function *f;
986
987 while (*functions) {
988 f = *functions++;
989
990 if (f->dev) {
991 device_destroy(android_class, f->dev->devt);
992 kfree(f->dev_name);
993 }
994
995 if (f->cleanup)
996 f->cleanup(f);
997 }
998}
999
1000static int
1001android_bind_enabled_functions(struct android_dev *dev,
1002 struct usb_configuration *c)
1003{
1004 struct android_usb_function *f;
1005 int ret;
1006
1007 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1008 ret = f->bind_config(f, c);
1009 if (ret) {
1010 pr_err("%s: %s failed", __func__, f->name);
1011 return ret;
1012 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301013 }
1014 return 0;
1015}
1016
Benoit Gobyaab96812011-04-19 20:37:33 -07001017static void
1018android_unbind_enabled_functions(struct android_dev *dev,
1019 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301020{
Benoit Gobyaab96812011-04-19 20:37:33 -07001021 struct android_usb_function *f;
1022
1023 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1024 if (f->unbind_config)
1025 f->unbind_config(f, c);
1026 }
1027}
1028
1029static int android_enable_function(struct android_dev *dev, char *name)
1030{
1031 struct android_usb_function **functions = dev->functions;
1032 struct android_usb_function *f;
1033 while ((f = *functions++)) {
1034 if (!strcmp(name, f->name)) {
1035 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301036 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001037 }
1038 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001039 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001040}
1041
Benoit Gobyaab96812011-04-19 20:37:33 -07001042/*-------------------------------------------------------------------------*/
1043/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301044
Benoit Gobyaab96812011-04-19 20:37:33 -07001045static ssize_t
1046functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1047{
1048 struct android_dev *dev = dev_get_drvdata(pdev);
1049 struct android_usb_function *f;
1050 char *buff = buf;
1051
1052 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
1053 buff += sprintf(buff, "%s,", f->name);
1054 if (buff != buf)
1055 *(buff-1) = '\n';
1056 return buff - buf;
1057}
1058
1059static ssize_t
1060functions_store(struct device *pdev, struct device_attribute *attr,
1061 const char *buff, size_t size)
1062{
1063 struct android_dev *dev = dev_get_drvdata(pdev);
1064 char *name;
1065 char buf[256], *b;
1066 int err;
1067
1068 INIT_LIST_HEAD(&dev->enabled_functions);
1069
1070 strncpy(buf, buff, sizeof(buf));
1071 b = strim(buf);
1072
1073 while (b) {
1074 name = strsep(&b, ",");
1075 if (name) {
1076 err = android_enable_function(dev, name);
1077 if (err)
1078 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301079 }
1080 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001081
1082 return size;
1083}
1084
1085static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1086 char *buf)
1087{
1088 struct android_dev *dev = dev_get_drvdata(pdev);
1089 return sprintf(buf, "%d\n", dev->enabled);
1090}
1091
1092static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1093 const char *buff, size_t size)
1094{
1095 struct android_dev *dev = dev_get_drvdata(pdev);
1096 struct usb_composite_dev *cdev = dev->cdev;
1097 int enabled = 0;
1098
1099 sscanf(buff, "%d", &enabled);
1100 if (enabled && !dev->enabled) {
1101 /* update values in composite driver's copy of device descriptor */
1102 cdev->desc.idVendor = device_desc.idVendor;
1103 cdev->desc.idProduct = device_desc.idProduct;
1104 cdev->desc.bcdDevice = device_desc.bcdDevice;
1105 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1106 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1107 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1108 usb_add_config(cdev, &android_config_driver,
1109 android_bind_config);
1110 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:
1141 return sprintf(buf, "%s\n", state);
1142}
1143
1144#define DESCRIPTOR_ATTR(field, format_string) \
1145static ssize_t \
1146field ## _show(struct device *dev, struct device_attribute *attr, \
1147 char *buf) \
1148{ \
1149 return sprintf(buf, format_string, device_desc.field); \
1150} \
1151static ssize_t \
1152field ## _store(struct device *dev, struct device_attribute *attr, \
1153 const char *buf, size_t size) \
1154{ \
1155 int value; \
1156 if (sscanf(buf, format_string, &value) == 1) { \
1157 device_desc.field = value; \
1158 return size; \
1159 } \
1160 return -1; \
1161} \
1162static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1163
1164#define DESCRIPTOR_STRING_ATTR(field, buffer) \
1165static ssize_t \
1166field ## _show(struct device *dev, struct device_attribute *attr, \
1167 char *buf) \
1168{ \
1169 return sprintf(buf, "%s", buffer); \
1170} \
1171static ssize_t \
1172field ## _store(struct device *dev, struct device_attribute *attr, \
1173 const char *buf, size_t size) \
1174{ \
1175 if (size >= sizeof(buffer)) return -EINVAL; \
1176 if (sscanf(buf, "%s", buffer) == 1) { \
1177 return size; \
1178 } \
1179 return -1; \
1180} \
1181static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1182
1183
1184DESCRIPTOR_ATTR(idVendor, "%04x\n")
1185DESCRIPTOR_ATTR(idProduct, "%04x\n")
1186DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1187DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1188DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1189DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1190DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1191DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1192DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1193
1194static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
1195static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1196static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1197
1198static struct device_attribute *android_usb_attributes[] = {
1199 &dev_attr_idVendor,
1200 &dev_attr_idProduct,
1201 &dev_attr_bcdDevice,
1202 &dev_attr_bDeviceClass,
1203 &dev_attr_bDeviceSubClass,
1204 &dev_attr_bDeviceProtocol,
1205 &dev_attr_iManufacturer,
1206 &dev_attr_iProduct,
1207 &dev_attr_iSerial,
1208 &dev_attr_functions,
1209 &dev_attr_enable,
1210 &dev_attr_state,
1211 NULL
1212};
1213
1214/*-------------------------------------------------------------------------*/
1215/* Composite driver */
1216
1217static int android_bind_config(struct usb_configuration *c)
1218{
1219 struct android_dev *dev = _android_dev;
1220 int ret = 0;
1221
1222 ret = android_bind_enabled_functions(dev, c);
1223 if (ret)
1224 return ret;
1225
1226 return 0;
1227}
1228
1229static void android_unbind_config(struct usb_configuration *c)
1230{
1231 struct android_dev *dev = _android_dev;
1232
1233 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301234}
1235
Dmitry Shmidt577e37a2010-07-02 12:46:34 -07001236static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001237{
1238 struct android_dev *dev = _android_dev;
1239 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -05001240 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001241
Benoit Gobyaab96812011-04-19 20:37:33 -07001242 usb_gadget_disconnect(gadget);
1243
1244 ret = android_init_functions(dev->functions, cdev);
1245 if (ret)
1246 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001247
1248 /* Allocate string descriptor numbers ... note that string
1249 * contents can be overridden by the composite_dev glue.
1250 */
1251 id = usb_string_id(cdev);
1252 if (id < 0)
1253 return id;
1254 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1255 device_desc.iManufacturer = id;
1256
1257 id = usb_string_id(cdev);
1258 if (id < 0)
1259 return id;
1260 strings_dev[STRING_PRODUCT_IDX].id = id;
1261 device_desc.iProduct = id;
1262
Benoit Gobyaab96812011-04-19 20:37:33 -07001263 /* Default strings - should be updated by userspace */
1264 strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1);
1265 strncpy(product_string, "Android", sizeof(product_string) - 1);
1266 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
1267
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001268 id = usb_string_id(cdev);
1269 if (id < 0)
1270 return id;
1271 strings_dev[STRING_SERIAL_IDX].id = id;
1272 device_desc.iSerialNumber = id;
1273
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001274 gcnum = usb_gadget_controller_number(gadget);
1275 if (gcnum >= 0)
1276 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1277 else {
1278 /* gadget zero is so simple (for now, no altsettings) that
1279 * it SHOULD NOT have problems with bulk-capable hardware.
1280 * so just warn about unrcognized controllers -- don't panic.
1281 *
1282 * things like configuration and altsetting numbering
1283 * can need hardware-specific attention though.
1284 */
1285 pr_warning("%s: controller '%s' not recognized\n",
1286 longname, gadget->name);
1287 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1288 }
1289
1290 usb_gadget_set_selfpowered(gadget);
1291 dev->cdev = cdev;
1292
1293 return 0;
1294}
1295
Benoit Gobyaab96812011-04-19 20:37:33 -07001296static int android_usb_unbind(struct usb_composite_dev *cdev)
1297{
1298 struct android_dev *dev = _android_dev;
1299
1300 cancel_work_sync(&dev->work);
1301 android_cleanup_functions(dev->functions);
1302 return 0;
1303}
1304
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001305static struct usb_composite_driver android_usb_driver = {
1306 .name = "android_usb",
1307 .dev = &device_desc,
1308 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001309 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001310};
1311
Benoit Gobyaab96812011-04-19 20:37:33 -07001312static int
1313android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1314{
1315 struct android_dev *dev = _android_dev;
1316 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1317 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001318 struct android_usb_function *f;
1319 int value = -EOPNOTSUPP;
1320 unsigned long flags;
1321
1322 req->zero = 0;
1323 req->complete = composite_setup_complete;
1324 req->length = 0;
1325 gadget->ep0->driver_data = cdev;
1326
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001327 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001328 if (f->ctrlrequest) {
1329 value = f->ctrlrequest(f, cdev, c);
1330 if (value >= 0)
1331 break;
1332 }
1333 }
1334
1335 if (value < 0)
1336 value = composite_setup(gadget, c);
1337
1338 spin_lock_irqsave(&cdev->lock, flags);
1339 if (!dev->connected) {
1340 dev->connected = 1;
1341 schedule_work(&dev->work);
1342 }
1343 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1344 schedule_work(&dev->work);
1345 }
1346 spin_unlock_irqrestore(&cdev->lock, flags);
1347
1348 return value;
1349}
1350
1351static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001352{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301353 struct android_dev *dev = _android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001354 dev->connected = 0;
1355 schedule_work(&dev->work);
1356 composite_disconnect(gadget);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301357}
1358
Benoit Gobyaab96812011-04-19 20:37:33 -07001359static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001360{
Benoit Gobyaab96812011-04-19 20:37:33 -07001361 struct device_attribute **attrs = android_usb_attributes;
1362 struct device_attribute *attr;
1363 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001364
Benoit Gobyaab96812011-04-19 20:37:33 -07001365 dev->dev = device_create(android_class, NULL,
1366 MKDEV(0, 0), NULL, "android0");
1367 if (IS_ERR(dev->dev))
1368 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001369
Benoit Gobyaab96812011-04-19 20:37:33 -07001370 dev_set_drvdata(dev->dev, dev);
1371
1372 while ((attr = *attrs++)) {
1373 err = device_create_file(dev->dev, attr);
1374 if (err) {
1375 device_destroy(android_class, dev->dev->devt);
1376 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001377 }
1378 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001379 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001380}
1381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382static int __devinit android_probe(struct platform_device *pdev)
1383{
1384 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
1385 struct android_dev *dev = _android_dev;
1386
1387 dev->pdata = pdata;
1388
1389 return 0;
1390}
1391
1392static struct platform_driver android_platform_driver = {
1393 .driver = { .name = "android_usb"},
1394};
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001395
1396static int __init init(void)
1397{
1398 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001399 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001400
Benoit Gobyaab96812011-04-19 20:37:33 -07001401 android_class = class_create(THIS_MODULE, "android_usb");
1402 if (IS_ERR(android_class))
1403 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001404
1405 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1406 if (!dev)
1407 return -ENOMEM;
1408
Benoit Gobyaab96812011-04-19 20:37:33 -07001409 dev->functions = supported_functions;
1410 INIT_LIST_HEAD(&dev->enabled_functions);
1411 INIT_WORK(&dev->work, android_work);
1412
1413 err = android_create_device(dev);
1414 if (err) {
1415 class_destroy(android_class);
1416 kfree(dev);
1417 return err;
1418 }
1419
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001420 _android_dev = dev;
1421
Benoit Gobyaab96812011-04-19 20:37:33 -07001422 /* Override composite driver functions */
1423 composite_driver.setup = android_setup;
1424 composite_driver.disconnect = android_disconnect;
1425
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426 platform_driver_probe(&android_platform_driver, android_probe);
1427
Benoit Gobyaab96812011-04-19 20:37:33 -07001428 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001429}
1430module_init(init);
1431
1432static void __exit cleanup(void)
1433{
1434 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001435 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001436 kfree(_android_dev);
1437 _android_dev = NULL;
1438}
1439module_exit(cleanup);