blob: 3d3fc795c4c72f277d1479fd34479becb043a761 [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
68 int product_id;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050069 int version;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050070};
71
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050072static struct android_dev *_android_dev;
73
74/* string IDs are assigned dynamically */
75
76#define STRING_MANUFACTURER_IDX 0
77#define STRING_PRODUCT_IDX 1
78#define STRING_SERIAL_IDX 2
79
80/* String Table */
81static struct usb_string strings_dev[] = {
82 /* These dummy values should be overridden by platform data */
83 [STRING_MANUFACTURER_IDX].s = "Android",
84 [STRING_PRODUCT_IDX].s = "Android",
85 [STRING_SERIAL_IDX].s = "0123456789ABCDEF",
86 { } /* end of list */
87};
88
89static struct usb_gadget_strings stringtab_dev = {
90 .language = 0x0409, /* en-us */
91 .strings = strings_dev,
92};
93
94static struct usb_gadget_strings *dev_strings[] = {
95 &stringtab_dev,
96 NULL,
97};
98
99static struct usb_device_descriptor device_desc = {
100 .bLength = sizeof(device_desc),
101 .bDescriptorType = USB_DT_DEVICE,
102 .bcdUSB = __constant_cpu_to_le16(0x0200),
103 .bDeviceClass = USB_CLASS_PER_INTERFACE,
104 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
105 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
106 .bcdDevice = __constant_cpu_to_le16(0xffff),
107 .bNumConfigurations = 1,
108};
109
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530110static struct list_head _functions = LIST_HEAD_INIT(_functions);
John Michelau51d57552010-11-30 15:36:29 -0600111static bool _are_functions_bound;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530112
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530113static struct android_usb_function *get_function(const char *name)
114{
115 struct android_usb_function *f;
116 list_for_each_entry(f, &_functions, list) {
117 if (!strcmp(name, f->name))
118 return f;
119 }
120 return 0;
121}
122
John Michelau51d57552010-11-30 15:36:29 -0600123static bool are_functions_registered(struct android_dev *dev)
124{
125 char **functions = dev->functions;
126 int i;
127
128 /* Look only for functions required by the board config */
129 for (i = 0; i < dev->num_functions; i++) {
130 char *name = *functions++;
131 bool is_match = false;
132 /* Could reuse get_function() here, but a reverse search
133 * should yield less comparisons overall */
134 struct android_usb_function *f;
135 list_for_each_entry_reverse(f, &_functions, list) {
136 if (!strcmp(name, f->name)) {
137 is_match = true;
138 break;
139 }
140 }
141 if (is_match)
142 continue;
143 else
144 return false;
145 }
146
147 return true;
148}
149
150static bool should_bind_functions(struct android_dev *dev)
151{
152 /* Don't waste time if the main driver hasn't bound */
153 if (!dev->config)
154 return false;
155
156 /* Don't waste time if we've already bound the functions */
157 if (_are_functions_bound)
158 return false;
159
160 /* This call is the most costly, so call it last */
161 if (!are_functions_registered(dev))
162 return false;
163
164 return true;
165}
166
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530167static void bind_functions(struct android_dev *dev)
168{
169 struct android_usb_function *f;
170 char **functions = dev->functions;
171 int i;
172
173 for (i = 0; i < dev->num_functions; i++) {
174 char *name = *functions++;
175 f = get_function(name);
176 if (f)
177 f->bind_config(dev->config);
178 else
179 printk(KERN_ERR "function %s not found in bind_functions\n", name);
180 }
John Michelau51d57552010-11-30 15:36:29 -0600181
182 _are_functions_bound = true;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530183}
184
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700185static int android_bind_config(struct usb_configuration *c)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500186{
187 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500188
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530189 printk(KERN_DEBUG "android_bind_config\n");
190 dev->config = c;
191
John Michelau51d57552010-11-30 15:36:29 -0600192 if (should_bind_functions(dev))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530193 bind_functions(dev);
194
195 return 0;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500196}
197
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530198static int android_setup_config(struct usb_configuration *c,
199 const struct usb_ctrlrequest *ctrl);
200
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500201static struct usb_configuration android_config_driver = {
202 .label = "android",
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530203 .setup = android_setup_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500204 .bConfigurationValue = 1,
205 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
206 .bMaxPower = 0xFA, /* 500ma */
207};
208
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530209static int android_setup_config(struct usb_configuration *c,
210 const struct usb_ctrlrequest *ctrl)
211{
212 int i;
213 int ret = -EOPNOTSUPP;
214
215 for (i = 0; i < android_config_driver.next_interface_id; i++) {
216 if (android_config_driver.interface[i]->setup) {
217 ret = android_config_driver.interface[i]->setup(
218 android_config_driver.interface[i], ctrl);
219 if (ret >= 0)
220 return ret;
221 }
222 }
223 return ret;
224}
225
226static int product_has_function(struct android_usb_product *p,
227 struct usb_function *f)
228{
229 char **functions = p->functions;
230 int count = p->num_functions;
231 const char *name = f->name;
232 int i;
233
234 for (i = 0; i < count; i++) {
John Michelau51d57552010-11-30 15:36:29 -0600235 /* For functions with multiple instances, usb_function.name
236 * will have an index appended to the core name (ex: acm0),
237 * while android_usb_product.functions[i] will only have the
238 * core name (ex: acm). So, only compare up to the length of
239 * android_usb_product.functions[i].
240 */
241 if (!strncmp(name, functions[i], strlen(functions[i])))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530242 return 1;
243 }
244 return 0;
245}
246
247static int product_matches_functions(struct android_usb_product *p)
248{
249 struct usb_function *f;
250 list_for_each_entry(f, &android_config_driver.functions, list) {
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400251 if (product_has_function(p, f) == !!f->disabled)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530252 return 0;
253 }
254 return 1;
255}
256
257static int get_product_id(struct android_dev *dev)
258{
259 struct android_usb_product *p = dev->products;
260 int count = dev->num_products;
261 int i;
262
263 if (p) {
264 for (i = 0; i < count; i++, p++) {
265 if (product_matches_functions(p))
266 return p->product_id;
267 }
268 }
269 /* use default product ID */
270 return dev->product_id;
271}
272
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700273static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500274{
275 struct android_dev *dev = _android_dev;
276 struct usb_gadget *gadget = cdev->gadget;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530277 int gcnum, id, product_id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500278
279 printk(KERN_INFO "android_bind\n");
280
281 /* Allocate string descriptor numbers ... note that string
282 * contents can be overridden by the composite_dev glue.
283 */
284 id = usb_string_id(cdev);
285 if (id < 0)
286 return id;
287 strings_dev[STRING_MANUFACTURER_IDX].id = id;
288 device_desc.iManufacturer = id;
289
290 id = usb_string_id(cdev);
291 if (id < 0)
292 return id;
293 strings_dev[STRING_PRODUCT_IDX].id = id;
294 device_desc.iProduct = id;
295
296 id = usb_string_id(cdev);
297 if (id < 0)
298 return id;
299 strings_dev[STRING_SERIAL_IDX].id = id;
300 device_desc.iSerialNumber = id;
301
302 /* register our configuration */
Dima Zavin8da04172011-01-05 16:13:41 -0800303 ret = usb_add_config(cdev, &android_config_driver, android_bind_config);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500304 if (ret) {
305 printk(KERN_ERR "usb_add_config failed\n");
306 return ret;
307 }
308
309 gcnum = usb_gadget_controller_number(gadget);
310 if (gcnum >= 0)
311 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
312 else {
313 /* gadget zero is so simple (for now, no altsettings) that
314 * it SHOULD NOT have problems with bulk-capable hardware.
315 * so just warn about unrcognized controllers -- don't panic.
316 *
317 * things like configuration and altsetting numbering
318 * can need hardware-specific attention though.
319 */
320 pr_warning("%s: controller '%s' not recognized\n",
321 longname, gadget->name);
322 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
323 }
324
325 usb_gadget_set_selfpowered(gadget);
326 dev->cdev = cdev;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530327 product_id = get_product_id(dev);
328 device_desc.idProduct = __constant_cpu_to_le16(product_id);
329 cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500330
331 return 0;
332}
333
334static struct usb_composite_driver android_usb_driver = {
335 .name = "android_usb",
336 .dev = &device_desc,
337 .strings = dev_strings,
Mike Lockwood05329732010-02-06 21:54:31 -0500338 .enable_function = android_enable_function,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500339};
340
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530341void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500342{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530343 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500344
Mike Lockwood789ef232010-01-12 10:33:59 -0800345 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530346 list_add_tail(&f->list, &_functions);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530347
John Michelau51d57552010-11-30 15:36:29 -0600348 if (dev && should_bind_functions(dev))
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530349 bind_functions(dev);
350}
351
John Michelaud5d2de62010-12-10 11:33:54 -0600352void update_dev_desc(struct android_dev *dev)
353{
354 struct usb_function *f;
355 struct usb_function *last_enabled_f = NULL;
356 int num_enabled = 0;
357 int has_iad = 0;
358
359 dev->cdev->desc.bDeviceClass = USB_CLASS_PER_INTERFACE;
360 dev->cdev->desc.bDeviceSubClass = 0x00;
361 dev->cdev->desc.bDeviceProtocol = 0x00;
362
363 list_for_each_entry(f, &android_config_driver.functions, list) {
364 if (!f->disabled) {
365 num_enabled++;
366 last_enabled_f = f;
367 if (f->descriptors[0]->bDescriptorType ==
368 USB_DT_INTERFACE_ASSOCIATION)
369 has_iad = 1;
370 }
371 if (num_enabled > 1 && has_iad) {
372 dev->cdev->desc.bDeviceClass = USB_CLASS_MISC;
373 dev->cdev->desc.bDeviceSubClass = 0x02;
374 dev->cdev->desc.bDeviceProtocol = 0x01;
375 break;
376 }
377 }
378
379 if (num_enabled == 1) {
380#ifdef CONFIG_USB_ANDROID_RNDIS
381 if (!strcmp(last_enabled_f->name, "rndis")) {
382#ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
383 dev->cdev->desc.bDeviceClass =
384 USB_CLASS_WIRELESS_CONTROLLER;
385#else
386 dev->cdev->desc.bDeviceClass = USB_CLASS_COMM;
387#endif
388 }
389#endif
390 }
391}
392
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530393void android_enable_function(struct usb_function *f, int enable)
394{
395 struct android_dev *dev = _android_dev;
396 int disable = !enable;
397 int product_id;
398
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400399 if (!!f->disabled != disable) {
400 usb_function_set_enabled(f, !disable);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500401
Mike Lockwoodda574e22010-02-13 16:37:16 -0500402#ifdef CONFIG_USB_ANDROID_RNDIS
Mike Lockwoodda574e22010-02-13 16:37:16 -0500403 if (!strcmp(f->name, "rndis")) {
Mike Lockwood35220862010-02-24 10:20:59 -0500404 struct usb_function *func;
Mike Lockwood35220862010-02-24 10:20:59 -0500405 /* Windows does not support other interfaces when RNDIS is enabled,
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400406 * so we disable UMS and MTP when RNDIS is on.
Mike Lockwood35220862010-02-24 10:20:59 -0500407 */
408 list_for_each_entry(func, &android_config_driver.functions, list) {
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400409 if (!strcmp(func->name, "usb_mass_storage")
410 || !strcmp(func->name, "mtp")) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400411 usb_function_set_enabled(func, !enable);
Mike Lockwood35220862010-02-24 10:20:59 -0500412 }
413 }
Mike Lockwoodda574e22010-02-13 16:37:16 -0500414 }
415#endif
416
John Michelaud5d2de62010-12-10 11:33:54 -0600417 update_dev_desc(dev);
418
Mike Lockwood35220862010-02-24 10:20:59 -0500419 product_id = get_product_id(dev);
420 device_desc.idProduct = __constant_cpu_to_le16(product_id);
421 if (dev->cdev)
422 dev->cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400423 usb_composite_force_reset(dev->cdev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500424 }
425}
426
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700427static int android_probe(struct platform_device *pdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500428{
429 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
430 struct android_dev *dev = _android_dev;
431
432 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
433
434 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530435 dev->products = pdata->products;
436 dev->num_products = pdata->num_products;
437 dev->functions = pdata->functions;
438 dev->num_functions = pdata->num_functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500439 if (pdata->vendor_id)
440 device_desc.idVendor =
441 __constant_cpu_to_le16(pdata->vendor_id);
442 if (pdata->product_id) {
443 dev->product_id = pdata->product_id;
444 device_desc.idProduct =
445 __constant_cpu_to_le16(pdata->product_id);
446 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500447 if (pdata->version)
448 dev->version = pdata->version;
449
450 if (pdata->product_name)
451 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
452 if (pdata->manufacturer_name)
453 strings_dev[STRING_MANUFACTURER_IDX].s =
454 pdata->manufacturer_name;
455 if (pdata->serial_number)
456 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500457 }
458
Dima Zavin8da04172011-01-05 16:13:41 -0800459 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500460}
461
462static struct platform_driver android_platform_driver = {
463 .driver = { .name = "android_usb", },
464 .probe = android_probe,
465};
466
467static int __init init(void)
468{
469 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500470
471 printk(KERN_INFO "android init\n");
472
473 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
474 if (!dev)
475 return -ENOMEM;
476
477 /* set default values, which should be overridden by platform data */
478 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500479 _android_dev = dev;
480
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530481 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500482}
483module_init(init);
484
485static void __exit cleanup(void)
486{
487 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500488 platform_driver_unregister(&android_platform_driver);
489 kfree(_android_dev);
490 _android_dev = NULL;
491}
492module_exit(cleanup);