blob: 2ebc8183fd52fa280a1446bbc1fdcd780d2af44f [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
Krishna, Vamsi83814ea2009-02-11 21:07:20 +053030#include <linux/usb/android_composite.h>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050031#include <linux/usb/ch9.h>
32#include <linux/usb/composite.h>
33#include <linux/usb/gadget.h>
34
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
49MODULE_AUTHOR("Mike Lockwood");
50MODULE_DESCRIPTION("Android Composite USB Driver");
51MODULE_LICENSE("GPL");
52MODULE_VERSION("1.0");
53
54static const char longname[] = "Gadget Android";
55
56/* Default vendor and product IDs, overridden by platform data */
57#define VENDOR_ID 0x18D1
58#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050059
60struct android_dev {
61 struct usb_composite_dev *cdev;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +053062 struct usb_configuration *config;
63 int num_products;
64 struct android_usb_product *products;
65 int num_functions;
66 char **functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050067
Mike Lockwoodaecca432011-02-09 09:38:26 -050068 int vendor_id;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050069 int product_id;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050070 int version;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050071};
72
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050073static struct android_dev *_android_dev;
74
75/* string IDs are assigned dynamically */
76
77#define STRING_MANUFACTURER_IDX 0
78#define STRING_PRODUCT_IDX 1
79#define STRING_SERIAL_IDX 2
80
81/* String Table */
82static struct usb_string strings_dev[] = {
83 /* These dummy values should be overridden by platform data */
84 [STRING_MANUFACTURER_IDX].s = "Android",
85 [STRING_PRODUCT_IDX].s = "Android",
86 [STRING_SERIAL_IDX].s = "0123456789ABCDEF",
87 { } /* end of list */
88};
89
90static struct usb_gadget_strings stringtab_dev = {
91 .language = 0x0409, /* en-us */
92 .strings = strings_dev,
93};
94
95static struct usb_gadget_strings *dev_strings[] = {
96 &stringtab_dev,
97 NULL,
98};
99
100static struct usb_device_descriptor device_desc = {
101 .bLength = sizeof(device_desc),
102 .bDescriptorType = USB_DT_DEVICE,
103 .bcdUSB = __constant_cpu_to_le16(0x0200),
104 .bDeviceClass = USB_CLASS_PER_INTERFACE,
105 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
106 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
107 .bcdDevice = __constant_cpu_to_le16(0xffff),
108 .bNumConfigurations = 1,
109};
110
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530111static struct list_head _functions = LIST_HEAD_INIT(_functions);
John Michelau51d57552010-11-30 15:36:29 -0600112static bool _are_functions_bound;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530113
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530114static struct android_usb_function *get_function(const char *name)
115{
116 struct android_usb_function *f;
117 list_for_each_entry(f, &_functions, list) {
118 if (!strcmp(name, f->name))
119 return f;
120 }
121 return 0;
122}
123
John Michelau51d57552010-11-30 15:36:29 -0600124static bool are_functions_registered(struct android_dev *dev)
125{
126 char **functions = dev->functions;
127 int i;
128
129 /* Look only for functions required by the board config */
130 for (i = 0; i < dev->num_functions; i++) {
131 char *name = *functions++;
132 bool is_match = false;
133 /* Could reuse get_function() here, but a reverse search
134 * should yield less comparisons overall */
135 struct android_usb_function *f;
136 list_for_each_entry_reverse(f, &_functions, list) {
137 if (!strcmp(name, f->name)) {
138 is_match = true;
139 break;
140 }
141 }
142 if (is_match)
143 continue;
144 else
145 return false;
146 }
147
148 return true;
149}
150
151static bool should_bind_functions(struct android_dev *dev)
152{
153 /* Don't waste time if the main driver hasn't bound */
154 if (!dev->config)
155 return false;
156
157 /* Don't waste time if we've already bound the functions */
158 if (_are_functions_bound)
159 return false;
160
161 /* This call is the most costly, so call it last */
162 if (!are_functions_registered(dev))
163 return false;
164
165 return true;
166}
167
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530168static void bind_functions(struct android_dev *dev)
169{
170 struct android_usb_function *f;
171 char **functions = dev->functions;
172 int i;
173
174 for (i = 0; i < dev->num_functions; i++) {
175 char *name = *functions++;
176 f = get_function(name);
177 if (f)
178 f->bind_config(dev->config);
179 else
180 printk(KERN_ERR "function %s not found in bind_functions\n", name);
181 }
John Michelau51d57552010-11-30 15:36:29 -0600182
183 _are_functions_bound = true;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530184}
185
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700186static int android_bind_config(struct usb_configuration *c)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500187{
188 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500189
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530190 printk(KERN_DEBUG "android_bind_config\n");
191 dev->config = c;
192
John Michelau51d57552010-11-30 15:36:29 -0600193 if (should_bind_functions(dev))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530194 bind_functions(dev);
195
196 return 0;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500197}
198
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530199static int android_setup_config(struct usb_configuration *c,
200 const struct usb_ctrlrequest *ctrl);
201
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500202static struct usb_configuration android_config_driver = {
203 .label = "android",
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530204 .setup = android_setup_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500205 .bConfigurationValue = 1,
206 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
207 .bMaxPower = 0xFA, /* 500ma */
208};
209
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530210static int android_setup_config(struct usb_configuration *c,
211 const struct usb_ctrlrequest *ctrl)
212{
213 int i;
214 int ret = -EOPNOTSUPP;
215
216 for (i = 0; i < android_config_driver.next_interface_id; i++) {
217 if (android_config_driver.interface[i]->setup) {
218 ret = android_config_driver.interface[i]->setup(
219 android_config_driver.interface[i], ctrl);
220 if (ret >= 0)
221 return ret;
222 }
223 }
224 return ret;
225}
226
227static int product_has_function(struct android_usb_product *p,
228 struct usb_function *f)
229{
230 char **functions = p->functions;
231 int count = p->num_functions;
232 const char *name = f->name;
233 int i;
234
235 for (i = 0; i < count; i++) {
John Michelau51d57552010-11-30 15:36:29 -0600236 /* For functions with multiple instances, usb_function.name
237 * will have an index appended to the core name (ex: acm0),
238 * while android_usb_product.functions[i] will only have the
239 * core name (ex: acm). So, only compare up to the length of
240 * android_usb_product.functions[i].
241 */
242 if (!strncmp(name, functions[i], strlen(functions[i])))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530243 return 1;
244 }
245 return 0;
246}
247
248static int product_matches_functions(struct android_usb_product *p)
249{
250 struct usb_function *f;
251 list_for_each_entry(f, &android_config_driver.functions, list) {
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400252 if (product_has_function(p, f) == !!f->disabled)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530253 return 0;
254 }
255 return 1;
256}
257
Mike Lockwoodaecca432011-02-09 09:38:26 -0500258static int get_vendor_id(struct android_dev *dev)
259{
260 struct android_usb_product *p = dev->products;
261 int count = dev->num_products;
262 int i;
263
264 if (p) {
265 for (i = 0; i < count; i++, p++) {
266 if (p->vendor_id && product_matches_functions(p))
267 return p->vendor_id;
268 }
269 }
270 /* use default vendor ID */
271 return dev->vendor_id;
272}
273
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530274static int get_product_id(struct android_dev *dev)
275{
276 struct android_usb_product *p = dev->products;
277 int count = dev->num_products;
278 int i;
279
280 if (p) {
281 for (i = 0; i < count; i++, p++) {
282 if (product_matches_functions(p))
283 return p->product_id;
284 }
285 }
286 /* use default product ID */
287 return dev->product_id;
288}
289
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700290static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500291{
292 struct android_dev *dev = _android_dev;
293 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500294 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500295
296 printk(KERN_INFO "android_bind\n");
297
298 /* Allocate string descriptor numbers ... note that string
299 * contents can be overridden by the composite_dev glue.
300 */
301 id = usb_string_id(cdev);
302 if (id < 0)
303 return id;
304 strings_dev[STRING_MANUFACTURER_IDX].id = id;
305 device_desc.iManufacturer = id;
306
307 id = usb_string_id(cdev);
308 if (id < 0)
309 return id;
310 strings_dev[STRING_PRODUCT_IDX].id = id;
311 device_desc.iProduct = id;
312
313 id = usb_string_id(cdev);
314 if (id < 0)
315 return id;
316 strings_dev[STRING_SERIAL_IDX].id = id;
317 device_desc.iSerialNumber = id;
318
319 /* register our configuration */
Dima Zavin8da04172011-01-05 16:13:41 -0800320 ret = usb_add_config(cdev, &android_config_driver, android_bind_config);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500321 if (ret) {
322 printk(KERN_ERR "usb_add_config failed\n");
323 return ret;
324 }
325
326 gcnum = usb_gadget_controller_number(gadget);
327 if (gcnum >= 0)
328 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
329 else {
330 /* gadget zero is so simple (for now, no altsettings) that
331 * it SHOULD NOT have problems with bulk-capable hardware.
332 * so just warn about unrcognized controllers -- don't panic.
333 *
334 * things like configuration and altsetting numbering
335 * can need hardware-specific attention though.
336 */
337 pr_warning("%s: controller '%s' not recognized\n",
338 longname, gadget->name);
339 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
340 }
341
342 usb_gadget_set_selfpowered(gadget);
343 dev->cdev = cdev;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500344 device_desc.idVendor = __constant_cpu_to_le16(get_vendor_id(dev));
345 device_desc.idProduct = __constant_cpu_to_le16(get_product_id(dev));
Mike Lockwood1adbbfb2011-02-14 13:32:05 -0500346 cdev->desc.idVendor = device_desc.idVendor;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530347 cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500348
349 return 0;
350}
351
352static struct usb_composite_driver android_usb_driver = {
353 .name = "android_usb",
354 .dev = &device_desc,
355 .strings = dev_strings,
Mike Lockwood05329732010-02-06 21:54:31 -0500356 .enable_function = android_enable_function,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500357};
358
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530359void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500360{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530361 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500362
Mike Lockwood789ef232010-01-12 10:33:59 -0800363 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530364 list_add_tail(&f->list, &_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530365
John Michelau51d57552010-11-30 15:36:29 -0600366 if (dev && should_bind_functions(dev))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530367 bind_functions(dev);
368}
369
John Michelaud5d2de62010-12-10 11:33:54 -0600370void update_dev_desc(struct android_dev *dev)
371{
372 struct usb_function *f;
373 struct usb_function *last_enabled_f = NULL;
374 int num_enabled = 0;
375 int has_iad = 0;
376
377 dev->cdev->desc.bDeviceClass = USB_CLASS_PER_INTERFACE;
378 dev->cdev->desc.bDeviceSubClass = 0x00;
379 dev->cdev->desc.bDeviceProtocol = 0x00;
380
381 list_for_each_entry(f, &android_config_driver.functions, list) {
382 if (!f->disabled) {
383 num_enabled++;
384 last_enabled_f = f;
385 if (f->descriptors[0]->bDescriptorType ==
386 USB_DT_INTERFACE_ASSOCIATION)
387 has_iad = 1;
388 }
389 if (num_enabled > 1 && has_iad) {
390 dev->cdev->desc.bDeviceClass = USB_CLASS_MISC;
391 dev->cdev->desc.bDeviceSubClass = 0x02;
392 dev->cdev->desc.bDeviceProtocol = 0x01;
393 break;
394 }
395 }
396
397 if (num_enabled == 1) {
398#ifdef CONFIG_USB_ANDROID_RNDIS
399 if (!strcmp(last_enabled_f->name, "rndis")) {
400#ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
401 dev->cdev->desc.bDeviceClass =
402 USB_CLASS_WIRELESS_CONTROLLER;
403#else
404 dev->cdev->desc.bDeviceClass = USB_CLASS_COMM;
405#endif
406 }
407#endif
408 }
409}
410
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530411void android_enable_function(struct usb_function *f, int enable)
412{
413 struct android_dev *dev = _android_dev;
414 int disable = !enable;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530415
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400416 if (!!f->disabled != disable) {
417 usb_function_set_enabled(f, !disable);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500418
Mike Lockwoodda574e22010-02-13 16:37:16 -0500419#ifdef CONFIG_USB_ANDROID_RNDIS
Mike Lockwoodda574e22010-02-13 16:37:16 -0500420 if (!strcmp(f->name, "rndis")) {
Mike Lockwood35220862010-02-24 10:20:59 -0500421 struct usb_function *func;
Mike Lockwood35220862010-02-24 10:20:59 -0500422 /* Windows does not support other interfaces when RNDIS is enabled,
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400423 * so we disable UMS and MTP when RNDIS is on.
Mike Lockwood35220862010-02-24 10:20:59 -0500424 */
425 list_for_each_entry(func, &android_config_driver.functions, list) {
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400426 if (!strcmp(func->name, "usb_mass_storage")
427 || !strcmp(func->name, "mtp")) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400428 usb_function_set_enabled(func, !enable);
Mike Lockwood35220862010-02-24 10:20:59 -0500429 }
430 }
Mike Lockwoodda574e22010-02-13 16:37:16 -0500431 }
432#endif
Mike Lockwood9902e0b2011-02-02 11:52:56 -0500433#ifdef CONFIG_USB_ANDROID_ACCESSORY
434 if (!strcmp(f->name, "accessory") && enable) {
435 struct usb_function *func;
436
437 /* disable everything else (and keep adb for now) */
438 list_for_each_entry(func, &android_config_driver.functions, list) {
439 if (strcmp(func->name, "accessory")
440 && strcmp(func->name, "adb")) {
441 usb_function_set_enabled(func, 0);
442 }
443 }
444 }
445#endif
Mike Lockwoodda574e22010-02-13 16:37:16 -0500446
John Michelaud5d2de62010-12-10 11:33:54 -0600447 update_dev_desc(dev);
448
Mike Lockwoodaecca432011-02-09 09:38:26 -0500449 device_desc.idVendor = __constant_cpu_to_le16(get_vendor_id(dev));
450 device_desc.idProduct = __constant_cpu_to_le16(get_product_id(dev));
Mike Lockwood1adbbfb2011-02-14 13:32:05 -0500451 if (dev->cdev) {
452 dev->cdev->desc.idVendor = device_desc.idVendor;
Mike Lockwood35220862010-02-24 10:20:59 -0500453 dev->cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood1adbbfb2011-02-14 13:32:05 -0500454 }
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400455 usb_composite_force_reset(dev->cdev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500456 }
457}
458
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700459static int android_probe(struct platform_device *pdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500460{
461 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
462 struct android_dev *dev = _android_dev;
463
464 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
465
466 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530467 dev->products = pdata->products;
468 dev->num_products = pdata->num_products;
469 dev->functions = pdata->functions;
470 dev->num_functions = pdata->num_functions;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500471 if (pdata->vendor_id) {
472 dev->vendor_id = pdata->vendor_id;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500473 device_desc.idVendor =
474 __constant_cpu_to_le16(pdata->vendor_id);
Mike Lockwoodaecca432011-02-09 09:38:26 -0500475 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500476 if (pdata->product_id) {
477 dev->product_id = pdata->product_id;
478 device_desc.idProduct =
479 __constant_cpu_to_le16(pdata->product_id);
480 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500481 if (pdata->version)
482 dev->version = pdata->version;
483
484 if (pdata->product_name)
485 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
486 if (pdata->manufacturer_name)
487 strings_dev[STRING_MANUFACTURER_IDX].s =
488 pdata->manufacturer_name;
489 if (pdata->serial_number)
490 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500491 }
492
Dima Zavin8da04172011-01-05 16:13:41 -0800493 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500494}
495
496static struct platform_driver android_platform_driver = {
497 .driver = { .name = "android_usb", },
498 .probe = android_probe,
499};
500
501static int __init init(void)
502{
503 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500504
505 printk(KERN_INFO "android init\n");
506
507 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
508 if (!dev)
509 return -ENOMEM;
510
511 /* set default values, which should be overridden by platform data */
512 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500513 _android_dev = dev;
514
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530515 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500516}
517module_init(init);
518
519static void __exit cleanup(void)
520{
521 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500522 platform_driver_unregister(&android_platform_driver);
523 kfree(_android_dev);
524 _android_dev = NULL;
525}
526module_exit(cleanup);