blob: 377df5b4d52e25afdc97b43ad2be2684c0ee64cc [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>
33
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050034#include "gadget_chips.h"
35
36/*
37 * Kbuild is not very cooperative with respect to linking separately
38 * compiled library objects into one module. So for now we won't use
39 * separate compilation ... ensuring init/exit sections work to shrink
40 * the runtime footprint, and giving us at least some parts of what
41 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
42 */
43#include "usbstring.c"
44#include "config.c"
45#include "epautoconf.c"
46#include "composite.c"
47
Benoit Gobyaab96812011-04-19 20:37:33 -070048#include "f_mass_storage.c"
49#include "u_serial.c"
50#include "f_acm.c"
51#include "f_adb.c"
52#include "f_mtp.c"
53#include "f_accessory.c"
54#define USB_ETH_RNDIS y
55#include "f_rndis.c"
56#include "rndis.c"
57#include "u_ether.c"
58
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050059MODULE_AUTHOR("Mike Lockwood");
60MODULE_DESCRIPTION("Android Composite USB Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION("1.0");
63
64static const char longname[] = "Gadget Android";
65
Benoit Gobyaab96812011-04-19 20:37:33 -070066/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050067#define VENDOR_ID 0x18D1
68#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050069
Benoit Gobyaab96812011-04-19 20:37:33 -070070struct android_usb_function {
71 char *name;
72 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050073
Benoit Gobyaab96812011-04-19 20:37:33 -070074 struct device *dev;
75 char *dev_name;
76 struct device_attribute **attributes;
77
78 /* for android_dev.enabled_functions */
79 struct list_head enabled_list;
80
81 /* Optional: initialization during gadget bind */
82 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
83 /* Optional: cleanup during gadget unbind */
84 void (*cleanup)(struct android_usb_function *);
85
86 int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
87
88 /* Optional: called when the configuration is removed */
89 void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
90 /* Optional: handle ctrl requests before the device is configured
91 * and/or before the function is enabled */
92 int (*ctrlrequest)(struct android_usb_function *,
93 struct usb_composite_dev *,
94 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050095};
96
Benoit Gobyaab96812011-04-19 20:37:33 -070097struct android_dev {
98 struct android_usb_function **functions;
99 struct list_head enabled_functions;
100 struct usb_composite_dev *cdev;
101 struct device *dev;
102
103 bool enabled;
104 bool connected;
105 bool sw_connected;
106 struct work_struct work;
107};
108
109static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500110static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700111static int android_bind_config(struct usb_configuration *c);
112static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500113
114/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500115#define STRING_MANUFACTURER_IDX 0
116#define STRING_PRODUCT_IDX 1
117#define STRING_SERIAL_IDX 2
118
Benoit Gobyaab96812011-04-19 20:37:33 -0700119static char manufacturer_string[256];
120static char product_string[256];
121static char serial_string[256];
122
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500123/* String Table */
124static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700125 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
126 [STRING_PRODUCT_IDX].s = product_string,
127 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500128 { } /* end of list */
129};
130
131static struct usb_gadget_strings stringtab_dev = {
132 .language = 0x0409, /* en-us */
133 .strings = strings_dev,
134};
135
136static struct usb_gadget_strings *dev_strings[] = {
137 &stringtab_dev,
138 NULL,
139};
140
141static struct usb_device_descriptor device_desc = {
142 .bLength = sizeof(device_desc),
143 .bDescriptorType = USB_DT_DEVICE,
144 .bcdUSB = __constant_cpu_to_le16(0x0200),
145 .bDeviceClass = USB_CLASS_PER_INTERFACE,
146 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
147 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
148 .bcdDevice = __constant_cpu_to_le16(0xffff),
149 .bNumConfigurations = 1,
150};
151
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500152static struct usb_configuration android_config_driver = {
153 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700154 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500155 .bConfigurationValue = 1,
156 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
157 .bMaxPower = 0xFA, /* 500ma */
158};
159
Benoit Gobyaab96812011-04-19 20:37:33 -0700160static void android_work(struct work_struct *data)
161{
162 struct android_dev *dev = container_of(data, struct android_dev, work);
163 struct usb_composite_dev *cdev = dev->cdev;
164 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
165 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
166 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
167 unsigned long flags;
168
169 spin_lock_irqsave(&cdev->lock, flags);
170 if (cdev->config) {
171 spin_unlock_irqrestore(&cdev->lock, flags);
172 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
173 configured);
174 return;
175 }
176 if (dev->connected != dev->sw_connected) {
177 dev->sw_connected = dev->connected;
178 spin_unlock_irqrestore(&cdev->lock, flags);
179 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
180 dev->sw_connected ? connected : disconnected);
181 } else {
182 spin_unlock_irqrestore(&cdev->lock, flags);
183 }
184}
185
186
187/*-------------------------------------------------------------------------*/
188/* Supported functions initialization */
189
190static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
191{
192 return adb_setup();
193}
194
195static void adb_function_cleanup(struct android_usb_function *f)
196{
197 adb_cleanup();
198}
199
200static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
201{
202 return adb_bind_config(c);
203}
204
205static struct android_usb_function adb_function = {
206 .name = "adb",
207 .init = adb_function_init,
208 .cleanup = adb_function_cleanup,
209 .bind_config = adb_function_bind_config,
210};
211
212
213#define MAX_ACM_INSTANCES 4
214struct acm_function_config {
215 int instances;
216};
217
218static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
219{
220 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
221 if (!f->config)
222 return -ENOMEM;
223
224 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
225}
226
227static void acm_function_cleanup(struct android_usb_function *f)
228{
229 gserial_cleanup();
230 kfree(f->config);
231 f->config = NULL;
232}
233
234static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530235{
236 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700237 int ret = 0;
238 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530239
Benoit Gobyaab96812011-04-19 20:37:33 -0700240 for (i = 0; i < config->instances; i++) {
241 ret = acm_bind_config(c, i);
242 if (ret) {
243 pr_err("Could not bind acm%u config\n", i);
244 break;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530245 }
246 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700247
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530248 return ret;
249}
250
Benoit Gobyaab96812011-04-19 20:37:33 -0700251static ssize_t acm_instances_show(struct device *dev,
252 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530253{
Benoit Gobyaab96812011-04-19 20:37:33 -0700254 struct android_usb_function *f = dev_get_drvdata(dev);
255 struct acm_function_config *config = f->config;
256 return sprintf(buf, "%d\n", config->instances);
257}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530258
Benoit Gobyaab96812011-04-19 20:37:33 -0700259static ssize_t acm_instances_store(struct device *dev,
260 struct device_attribute *attr, const char *buf, size_t size)
261{
262 struct android_usb_function *f = dev_get_drvdata(dev);
263 struct acm_function_config *config = f->config;
264 int value;
265
266 sscanf(buf, "%d", &value);
267 if (value > MAX_ACM_INSTANCES)
268 value = MAX_ACM_INSTANCES;
269 config->instances = value;
270 return size;
271}
272
273static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
274static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
275
276static struct android_usb_function acm_function = {
277 .name = "acm",
278 .init = acm_function_init,
279 .cleanup = acm_function_cleanup,
280 .bind_config = acm_function_bind_config,
281 .attributes = acm_function_attributes,
282};
283
284
285static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
286{
287 return mtp_setup();
288}
289
290static void mtp_function_cleanup(struct android_usb_function *f)
291{
292 mtp_cleanup();
293}
294
295static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
296{
297 return mtp_bind_config(c);
298}
299
300static int mtp_function_ctrlrequest(struct android_usb_function *f,
301 struct usb_composite_dev *cdev,
302 const struct usb_ctrlrequest *c)
303{
304 return mtp_ctrlrequest(cdev, c);
305}
306
307static struct android_usb_function mtp_function = {
308 .name = "mtp",
309 .init = mtp_function_init,
310 .cleanup = mtp_function_cleanup,
311 .bind_config = mtp_function_bind_config,
312 .ctrlrequest = mtp_function_ctrlrequest,
313};
314
315
316struct rndis_function_config {
317 u8 ethaddr[ETH_ALEN];
318 u32 vendorID;
319 char manufacturer[256];
320 bool wceis;
321};
322
323static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
324{
325 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
326 if (!f->config)
327 return -ENOMEM;
328 return 0;
329}
330
331static void rndis_function_cleanup(struct android_usb_function *f)
332{
333 kfree(f->config);
334 f->config = NULL;
335}
336
337static int rndis_function_bind_config(struct android_usb_function *f,
338 struct usb_configuration *c)
339{
340 int ret;
341 struct rndis_function_config *rndis = f->config;
342
343 if (!rndis) {
344 pr_err("%s: rndis_pdata\n", __func__);
345 return -1;
346 }
347
348 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
349 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
350 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
351
352 ret = gether_setup(c->cdev->gadget, rndis->ethaddr);
353 if (ret) {
354 pr_err("%s: gether_setup failed\n", __func__);
355 return ret;
356 }
357
358 if (rndis->wceis) {
359 /* "Wireless" RNDIS; auto-detected by Windows */
360 rndis_iad_descriptor.bFunctionClass =
361 USB_CLASS_WIRELESS_CONTROLLER;
362 rndis_iad_descriptor.bFunctionSubClass = 0x01;
363 rndis_iad_descriptor.bFunctionProtocol = 0x03;
364 rndis_control_intf.bInterfaceClass =
365 USB_CLASS_WIRELESS_CONTROLLER;
366 rndis_control_intf.bInterfaceSubClass = 0x01;
367 rndis_control_intf.bInterfaceProtocol = 0x03;
368 }
369
370 return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
371 rndis->manufacturer);
372}
373
374static void rndis_function_unbind_config(struct android_usb_function *f,
375 struct usb_configuration *c)
376{
377 gether_cleanup();
378}
379
380static ssize_t rndis_manufacturer_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
382{
383 struct android_usb_function *f = dev_get_drvdata(dev);
384 struct rndis_function_config *config = f->config;
385 return sprintf(buf, "%s\n", config->manufacturer);
386}
387
388static ssize_t rndis_manufacturer_store(struct device *dev,
389 struct device_attribute *attr, const char *buf, size_t size)
390{
391 struct android_usb_function *f = dev_get_drvdata(dev);
392 struct rndis_function_config *config = f->config;
393
394 if (size >= sizeof(config->manufacturer))
395 return -EINVAL;
396 if (sscanf(buf, "%s", config->manufacturer) == 1)
397 return size;
398 return -1;
399}
400
401static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
402 rndis_manufacturer_store);
403
404static ssize_t rndis_wceis_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
407 struct android_usb_function *f = dev_get_drvdata(dev);
408 struct rndis_function_config *config = f->config;
409 return sprintf(buf, "%d\n", config->wceis);
410}
411
412static ssize_t rndis_wceis_store(struct device *dev,
413 struct device_attribute *attr, const char *buf, size_t size)
414{
415 struct android_usb_function *f = dev_get_drvdata(dev);
416 struct rndis_function_config *config = f->config;
417 int value;
418
419 if (sscanf(buf, "%d", &value) == 1) {
420 config->wceis = value;
421 return size;
422 }
423 return -EINVAL;
424}
425
426static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
427 rndis_wceis_store);
428
429static ssize_t rndis_ethaddr_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
432 struct android_usb_function *f = dev_get_drvdata(dev);
433 struct rndis_function_config *rndis = f->config;
434 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
435 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
436 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
437}
438
439static ssize_t rndis_ethaddr_store(struct device *dev,
440 struct device_attribute *attr, const char *buf, size_t size)
441{
442 struct android_usb_function *f = dev_get_drvdata(dev);
443 struct rndis_function_config *rndis = f->config;
444
445 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
446 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
447 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
448 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
449 return size;
450 return -EINVAL;
451}
452
453static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
454 rndis_ethaddr_store);
455
456static ssize_t rndis_vendorID_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
459 struct android_usb_function *f = dev_get_drvdata(dev);
460 struct rndis_function_config *config = f->config;
461 return sprintf(buf, "%04x\n", config->vendorID);
462}
463
464static ssize_t rndis_vendorID_store(struct device *dev,
465 struct device_attribute *attr, const char *buf, size_t size)
466{
467 struct android_usb_function *f = dev_get_drvdata(dev);
468 struct rndis_function_config *config = f->config;
469 int value;
470
471 if (sscanf(buf, "%04x", &value) == 1) {
472 config->vendorID = value;
473 return size;
474 }
475 return -EINVAL;
476}
477
478static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
479 rndis_vendorID_store);
480
481static struct device_attribute *rndis_function_attributes[] = {
482 &dev_attr_manufacturer,
483 &dev_attr_wceis,
484 &dev_attr_ethaddr,
485 &dev_attr_vendorID,
486 NULL
487};
488
489static struct android_usb_function rndis_function = {
490 .name = "rndis",
491 .init = rndis_function_init,
492 .cleanup = rndis_function_cleanup,
493 .bind_config = rndis_function_bind_config,
494 .unbind_config = rndis_function_unbind_config,
495 .attributes = rndis_function_attributes,
496};
497
498
499struct mass_storage_function_config {
500 struct fsg_config fsg;
501 struct fsg_common *common;
502};
503
504static int mass_storage_function_init(struct android_usb_function *f,
505 struct usb_composite_dev *cdev)
506{
507 struct mass_storage_function_config *config;
508 struct fsg_common *common;
509 int err;
510
511 config = kzalloc(sizeof(struct mass_storage_function_config),
512 GFP_KERNEL);
513 if (!config)
514 return -ENOMEM;
515
516 config->fsg.nluns = 1;
517 config->fsg.luns[0].removable = 1;
518
519 common = fsg_common_init(NULL, cdev, &config->fsg);
520 if (IS_ERR(common)) {
521 kfree(config);
522 return PTR_ERR(common);
523 }
524
525 err = sysfs_create_link(&f->dev->kobj,
526 &common->luns[0].dev.kobj,
527 "lun");
528 if (err) {
529 kfree(config);
530 return err;
531 }
532
533 config->common = common;
534 f->config = config;
535 return 0;
536}
537
538static void mass_storage_function_cleanup(struct android_usb_function *f)
539{
540 kfree(f->config);
541 f->config = NULL;
542}
543
544static int mass_storage_function_bind_config(struct android_usb_function *f,
545 struct usb_configuration *c)
546{
547 struct mass_storage_function_config *config = f->config;
548 return fsg_bind_config(c->cdev, c, config->common);
549}
550
551static ssize_t mass_storage_inquiry_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 struct android_usb_function *f = dev_get_drvdata(dev);
555 struct mass_storage_function_config *config = f->config;
556 return sprintf(buf, "%s\n", config->common->inquiry_string);
557}
558
559static ssize_t mass_storage_inquiry_store(struct device *dev,
560 struct device_attribute *attr, const char *buf, size_t size)
561{
562 struct android_usb_function *f = dev_get_drvdata(dev);
563 struct mass_storage_function_config *config = f->config;
564 if (size >= sizeof(config->common->inquiry_string))
565 return -EINVAL;
566 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
567 return -EINVAL;
568 return size;
569}
570
571static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
572 mass_storage_inquiry_show,
573 mass_storage_inquiry_store);
574
575static struct device_attribute *mass_storage_function_attributes[] = {
576 &dev_attr_inquiry_string,
577 NULL
578};
579
580static struct android_usb_function mass_storage_function = {
581 .name = "mass_storage",
582 .init = mass_storage_function_init,
583 .cleanup = mass_storage_function_cleanup,
584 .bind_config = mass_storage_function_bind_config,
585 .attributes = mass_storage_function_attributes,
586};
587
588
589static int accessory_function_init(struct android_usb_function *f,
590 struct usb_composite_dev *cdev)
591{
592 return acc_setup();
593}
594
595static void accessory_function_cleanup(struct android_usb_function *f)
596{
597 acc_cleanup();
598}
599
600static int accessory_function_bind_config(struct android_usb_function *f,
601 struct usb_configuration *c)
602{
603 return acc_bind_config(c);
604}
605
606static int accessory_function_ctrlrequest(struct android_usb_function *f,
607 struct usb_composite_dev *cdev,
608 const struct usb_ctrlrequest *c)
609{
610 return acc_ctrlrequest(cdev, c);
611}
612
613static struct android_usb_function accessory_function = {
614 .name = "accessory",
615 .init = accessory_function_init,
616 .cleanup = accessory_function_cleanup,
617 .bind_config = accessory_function_bind_config,
618 .ctrlrequest = accessory_function_ctrlrequest,
619};
620
621
622static struct android_usb_function *supported_functions[] = {
623 &adb_function,
624 &acm_function,
625 &mtp_function,
626 &rndis_function,
627 &mass_storage_function,
628 &accessory_function,
629 NULL
630};
631
632
633static int android_init_functions(struct android_usb_function **functions,
634 struct usb_composite_dev *cdev)
635{
636 struct android_dev *dev = _android_dev;
637 struct android_usb_function *f;
638 struct device_attribute **attrs;
639 struct device_attribute *attr;
640 int err;
641 int index = 0;
642
643 for (; (f = *functions++); index++) {
644 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
645 f->dev = device_create(android_class, dev->dev,
646 MKDEV(0, index), f, f->dev_name);
647 if (IS_ERR(f->dev)) {
648 pr_err("%s: Failed to create dev %s", __func__,
649 f->dev_name);
650 err = PTR_ERR(f->dev);
651 goto err_create;
652 }
653
654 if (f->init) {
655 err = f->init(f, cdev);
656 if (err) {
657 pr_err("%s: Failed to init %s", __func__,
658 f->name);
659 goto err_out;
660 }
661 }
662
663 attrs = f->attributes;
664 if (attrs) {
665 while ((attr = *attrs++) && !err)
666 err = device_create_file(f->dev, attr);
667 }
668 if (err) {
669 pr_err("%s: Failed to create function %s attributes",
670 __func__, f->name);
671 goto err_out;
672 }
673 }
674 return 0;
675
676err_out:
677 device_destroy(android_class, f->dev->devt);
678err_create:
679 kfree(f->dev_name);
680 return err;
681}
682
683static void android_cleanup_functions(struct android_usb_function **functions)
684{
685 struct android_usb_function *f;
686
687 while (*functions) {
688 f = *functions++;
689
690 if (f->dev) {
691 device_destroy(android_class, f->dev->devt);
692 kfree(f->dev_name);
693 }
694
695 if (f->cleanup)
696 f->cleanup(f);
697 }
698}
699
700static int
701android_bind_enabled_functions(struct android_dev *dev,
702 struct usb_configuration *c)
703{
704 struct android_usb_function *f;
705 int ret;
706
707 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
708 ret = f->bind_config(f, c);
709 if (ret) {
710 pr_err("%s: %s failed", __func__, f->name);
711 return ret;
712 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530713 }
714 return 0;
715}
716
Benoit Gobyaab96812011-04-19 20:37:33 -0700717static void
718android_unbind_enabled_functions(struct android_dev *dev,
719 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530720{
Benoit Gobyaab96812011-04-19 20:37:33 -0700721 struct android_usb_function *f;
722
723 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
724 if (f->unbind_config)
725 f->unbind_config(f, c);
726 }
727}
728
729static int android_enable_function(struct android_dev *dev, char *name)
730{
731 struct android_usb_function **functions = dev->functions;
732 struct android_usb_function *f;
733 while ((f = *functions++)) {
734 if (!strcmp(name, f->name)) {
735 list_add_tail(&f->enabled_list, &dev->enabled_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530736 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500737 }
738 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700739 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500740}
741
Benoit Gobyaab96812011-04-19 20:37:33 -0700742/*-------------------------------------------------------------------------*/
743/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530744
Benoit Gobyaab96812011-04-19 20:37:33 -0700745static ssize_t
746functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
747{
748 struct android_dev *dev = dev_get_drvdata(pdev);
749 struct android_usb_function *f;
750 char *buff = buf;
751
752 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
753 buff += sprintf(buff, "%s,", f->name);
754 if (buff != buf)
755 *(buff-1) = '\n';
756 return buff - buf;
757}
758
759static ssize_t
760functions_store(struct device *pdev, struct device_attribute *attr,
761 const char *buff, size_t size)
762{
763 struct android_dev *dev = dev_get_drvdata(pdev);
764 char *name;
765 char buf[256], *b;
766 int err;
767
768 INIT_LIST_HEAD(&dev->enabled_functions);
769
770 strncpy(buf, buff, sizeof(buf));
771 b = strim(buf);
772
773 while (b) {
774 name = strsep(&b, ",");
775 if (name) {
776 err = android_enable_function(dev, name);
777 if (err)
778 pr_err("android_usb: Cannot enable '%s'", name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530779 }
780 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700781
782 return size;
783}
784
785static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
786 char *buf)
787{
788 struct android_dev *dev = dev_get_drvdata(pdev);
789 return sprintf(buf, "%d\n", dev->enabled);
790}
791
792static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
793 const char *buff, size_t size)
794{
795 struct android_dev *dev = dev_get_drvdata(pdev);
796 struct usb_composite_dev *cdev = dev->cdev;
797 int enabled = 0;
798
799 sscanf(buff, "%d", &enabled);
800 if (enabled && !dev->enabled) {
801 /* update values in composite driver's copy of device descriptor */
802 cdev->desc.idVendor = device_desc.idVendor;
803 cdev->desc.idProduct = device_desc.idProduct;
804 cdev->desc.bcdDevice = device_desc.bcdDevice;
805 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
806 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
807 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
808 usb_add_config(cdev, &android_config_driver,
809 android_bind_config);
810 usb_gadget_connect(cdev->gadget);
811 dev->enabled = true;
812 } else if (!enabled && dev->enabled) {
813 usb_gadget_disconnect(cdev->gadget);
814 usb_remove_config(cdev, &android_config_driver);
815 dev->enabled = false;
816 } else {
817 pr_err("android_usb: already %s\n",
818 dev->enabled ? "enabled" : "disabled");
819 }
820 return size;
821}
822
823static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
824 char *buf)
825{
826 struct android_dev *dev = dev_get_drvdata(pdev);
827 struct usb_composite_dev *cdev = dev->cdev;
828 char *state = "DISCONNECTED";
829 unsigned long flags;
830
831 if (!cdev)
832 goto out;
833
834 spin_lock_irqsave(&cdev->lock, flags);
835 if (cdev->config)
836 state = "CONFIGURED";
837 else if (dev->connected)
838 state = "CONNECTED";
839 spin_unlock_irqrestore(&cdev->lock, flags);
840out:
841 return sprintf(buf, "%s\n", state);
842}
843
844#define DESCRIPTOR_ATTR(field, format_string) \
845static ssize_t \
846field ## _show(struct device *dev, struct device_attribute *attr, \
847 char *buf) \
848{ \
849 return sprintf(buf, format_string, device_desc.field); \
850} \
851static ssize_t \
852field ## _store(struct device *dev, struct device_attribute *attr, \
853 const char *buf, size_t size) \
854{ \
855 int value; \
856 if (sscanf(buf, format_string, &value) == 1) { \
857 device_desc.field = value; \
858 return size; \
859 } \
860 return -1; \
861} \
862static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
863
864#define DESCRIPTOR_STRING_ATTR(field, buffer) \
865static ssize_t \
866field ## _show(struct device *dev, struct device_attribute *attr, \
867 char *buf) \
868{ \
869 return sprintf(buf, "%s", buffer); \
870} \
871static ssize_t \
872field ## _store(struct device *dev, struct device_attribute *attr, \
873 const char *buf, size_t size) \
874{ \
875 if (size >= sizeof(buffer)) return -EINVAL; \
876 if (sscanf(buf, "%s", buffer) == 1) { \
877 return size; \
878 } \
879 return -1; \
880} \
881static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
882
883
884DESCRIPTOR_ATTR(idVendor, "%04x\n")
885DESCRIPTOR_ATTR(idProduct, "%04x\n")
886DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
887DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
888DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
889DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
890DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
891DESCRIPTOR_STRING_ATTR(iProduct, product_string)
892DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
893
894static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
895static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
896static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
897
898static struct device_attribute *android_usb_attributes[] = {
899 &dev_attr_idVendor,
900 &dev_attr_idProduct,
901 &dev_attr_bcdDevice,
902 &dev_attr_bDeviceClass,
903 &dev_attr_bDeviceSubClass,
904 &dev_attr_bDeviceProtocol,
905 &dev_attr_iManufacturer,
906 &dev_attr_iProduct,
907 &dev_attr_iSerial,
908 &dev_attr_functions,
909 &dev_attr_enable,
910 &dev_attr_state,
911 NULL
912};
913
914/*-------------------------------------------------------------------------*/
915/* Composite driver */
916
917static int android_bind_config(struct usb_configuration *c)
918{
919 struct android_dev *dev = _android_dev;
920 int ret = 0;
921
922 ret = android_bind_enabled_functions(dev, c);
923 if (ret)
924 return ret;
925
926 return 0;
927}
928
929static void android_unbind_config(struct usb_configuration *c)
930{
931 struct android_dev *dev = _android_dev;
932
933 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530934}
935
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700936static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500937{
938 struct android_dev *dev = _android_dev;
939 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500940 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500941
Benoit Gobyaab96812011-04-19 20:37:33 -0700942 usb_gadget_disconnect(gadget);
943
944 ret = android_init_functions(dev->functions, cdev);
945 if (ret)
946 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500947
948 /* Allocate string descriptor numbers ... note that string
949 * contents can be overridden by the composite_dev glue.
950 */
951 id = usb_string_id(cdev);
952 if (id < 0)
953 return id;
954 strings_dev[STRING_MANUFACTURER_IDX].id = id;
955 device_desc.iManufacturer = id;
956
957 id = usb_string_id(cdev);
958 if (id < 0)
959 return id;
960 strings_dev[STRING_PRODUCT_IDX].id = id;
961 device_desc.iProduct = id;
962
Benoit Gobyaab96812011-04-19 20:37:33 -0700963 /* Default strings - should be updated by userspace */
964 strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1);
965 strncpy(product_string, "Android", sizeof(product_string) - 1);
966 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
967
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500968 id = usb_string_id(cdev);
969 if (id < 0)
970 return id;
971 strings_dev[STRING_SERIAL_IDX].id = id;
972 device_desc.iSerialNumber = id;
973
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500974 gcnum = usb_gadget_controller_number(gadget);
975 if (gcnum >= 0)
976 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
977 else {
978 /* gadget zero is so simple (for now, no altsettings) that
979 * it SHOULD NOT have problems with bulk-capable hardware.
980 * so just warn about unrcognized controllers -- don't panic.
981 *
982 * things like configuration and altsetting numbering
983 * can need hardware-specific attention though.
984 */
985 pr_warning("%s: controller '%s' not recognized\n",
986 longname, gadget->name);
987 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
988 }
989
990 usb_gadget_set_selfpowered(gadget);
991 dev->cdev = cdev;
992
993 return 0;
994}
995
Benoit Gobyaab96812011-04-19 20:37:33 -0700996static int android_usb_unbind(struct usb_composite_dev *cdev)
997{
998 struct android_dev *dev = _android_dev;
999
1000 cancel_work_sync(&dev->work);
1001 android_cleanup_functions(dev->functions);
1002 return 0;
1003}
1004
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001005static struct usb_composite_driver android_usb_driver = {
1006 .name = "android_usb",
1007 .dev = &device_desc,
1008 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001009 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001010};
1011
Benoit Gobyaab96812011-04-19 20:37:33 -07001012static int
1013android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1014{
1015 struct android_dev *dev = _android_dev;
1016 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1017 struct usb_request *req = cdev->req;
1018 struct android_usb_function **functions = dev->functions;
1019 struct android_usb_function *f;
1020 int value = -EOPNOTSUPP;
1021 unsigned long flags;
1022
1023 req->zero = 0;
1024 req->complete = composite_setup_complete;
1025 req->length = 0;
1026 gadget->ep0->driver_data = cdev;
1027
1028 while ((f = *functions++)) {
1029 if (f->ctrlrequest) {
1030 value = f->ctrlrequest(f, cdev, c);
1031 if (value >= 0)
1032 break;
1033 }
1034 }
1035
1036 if (value < 0)
1037 value = composite_setup(gadget, c);
1038
1039 spin_lock_irqsave(&cdev->lock, flags);
1040 if (!dev->connected) {
1041 dev->connected = 1;
1042 schedule_work(&dev->work);
1043 }
1044 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1045 schedule_work(&dev->work);
1046 }
1047 spin_unlock_irqrestore(&cdev->lock, flags);
1048
1049 return value;
1050}
1051
1052static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001053{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301054 struct android_dev *dev = _android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001055 dev->connected = 0;
1056 schedule_work(&dev->work);
1057 composite_disconnect(gadget);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301058}
1059
Benoit Gobyaab96812011-04-19 20:37:33 -07001060static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001061{
Benoit Gobyaab96812011-04-19 20:37:33 -07001062 struct device_attribute **attrs = android_usb_attributes;
1063 struct device_attribute *attr;
1064 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001065
Benoit Gobyaab96812011-04-19 20:37:33 -07001066 dev->dev = device_create(android_class, NULL,
1067 MKDEV(0, 0), NULL, "android0");
1068 if (IS_ERR(dev->dev))
1069 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001070
Benoit Gobyaab96812011-04-19 20:37:33 -07001071 dev_set_drvdata(dev->dev, dev);
1072
1073 while ((attr = *attrs++)) {
1074 err = device_create_file(dev->dev, attr);
1075 if (err) {
1076 device_destroy(android_class, dev->dev->devt);
1077 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001078 }
1079 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001080 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001081}
1082
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001083
1084static int __init init(void)
1085{
1086 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001087 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001088
Benoit Gobyaab96812011-04-19 20:37:33 -07001089 android_class = class_create(THIS_MODULE, "android_usb");
1090 if (IS_ERR(android_class))
1091 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001092
1093 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1094 if (!dev)
1095 return -ENOMEM;
1096
Benoit Gobyaab96812011-04-19 20:37:33 -07001097 dev->functions = supported_functions;
1098 INIT_LIST_HEAD(&dev->enabled_functions);
1099 INIT_WORK(&dev->work, android_work);
1100
1101 err = android_create_device(dev);
1102 if (err) {
1103 class_destroy(android_class);
1104 kfree(dev);
1105 return err;
1106 }
1107
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001108 _android_dev = dev;
1109
Benoit Gobyaab96812011-04-19 20:37:33 -07001110 /* Override composite driver functions */
1111 composite_driver.setup = android_setup;
1112 composite_driver.disconnect = android_disconnect;
1113
1114 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001115}
1116module_init(init);
1117
1118static void __exit cleanup(void)
1119{
1120 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001121 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001122 kfree(_android_dev);
1123 _android_dev = NULL;
1124}
1125module_exit(cleanup);