blob: 70e48ac3e2a9d666fcc0ae16d1058996277ce241 [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);
111static int _registered_function_count = 0;
112
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500113void android_usb_set_connected(int connected)
114{
115 if (_android_dev && _android_dev->cdev && _android_dev->cdev->gadget) {
116 if (connected)
117 usb_gadget_connect(_android_dev->cdev->gadget);
118 else
119 usb_gadget_disconnect(_android_dev->cdev->gadget);
120 }
121}
122
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530123static struct android_usb_function *get_function(const char *name)
124{
125 struct android_usb_function *f;
126 list_for_each_entry(f, &_functions, list) {
127 if (!strcmp(name, f->name))
128 return f;
129 }
130 return 0;
131}
132
133static void bind_functions(struct android_dev *dev)
134{
135 struct android_usb_function *f;
136 char **functions = dev->functions;
137 int i;
138
139 for (i = 0; i < dev->num_functions; i++) {
140 char *name = *functions++;
141 f = get_function(name);
142 if (f)
143 f->bind_config(dev->config);
144 else
145 printk(KERN_ERR "function %s not found in bind_functions\n", name);
146 }
147}
148
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500149static int __init android_bind_config(struct usb_configuration *c)
150{
151 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500152
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530153 printk(KERN_DEBUG "android_bind_config\n");
154 dev->config = c;
155
156 /* bind our functions if they have all registered */
157 if (_registered_function_count == dev->num_functions)
158 bind_functions(dev);
159
160 return 0;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500161}
162
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530163static int android_setup_config(struct usb_configuration *c,
164 const struct usb_ctrlrequest *ctrl);
165
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500166static struct usb_configuration android_config_driver = {
167 .label = "android",
168 .bind = android_bind_config,
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530169 .setup = android_setup_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500170 .bConfigurationValue = 1,
171 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
172 .bMaxPower = 0xFA, /* 500ma */
173};
174
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530175static int android_setup_config(struct usb_configuration *c,
176 const struct usb_ctrlrequest *ctrl)
177{
178 int i;
179 int ret = -EOPNOTSUPP;
180
181 for (i = 0; i < android_config_driver.next_interface_id; i++) {
182 if (android_config_driver.interface[i]->setup) {
183 ret = android_config_driver.interface[i]->setup(
184 android_config_driver.interface[i], ctrl);
185 if (ret >= 0)
186 return ret;
187 }
188 }
189 return ret;
190}
191
192static int product_has_function(struct android_usb_product *p,
193 struct usb_function *f)
194{
195 char **functions = p->functions;
196 int count = p->num_functions;
197 const char *name = f->name;
198 int i;
199
200 for (i = 0; i < count; i++) {
201 if (!strcmp(name, *functions++))
202 return 1;
203 }
204 return 0;
205}
206
207static int product_matches_functions(struct android_usb_product *p)
208{
209 struct usb_function *f;
210 list_for_each_entry(f, &android_config_driver.functions, list) {
211 if (product_has_function(p, f) == !!f->hidden)
212 return 0;
213 }
214 return 1;
215}
216
217static int get_product_id(struct android_dev *dev)
218{
219 struct android_usb_product *p = dev->products;
220 int count = dev->num_products;
221 int i;
222
223 if (p) {
224 for (i = 0; i < count; i++, p++) {
225 if (product_matches_functions(p))
226 return p->product_id;
227 }
228 }
229 /* use default product ID */
230 return dev->product_id;
231}
232
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500233static int __init android_bind(struct usb_composite_dev *cdev)
234{
235 struct android_dev *dev = _android_dev;
236 struct usb_gadget *gadget = cdev->gadget;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530237 int gcnum, id, product_id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500238
239 printk(KERN_INFO "android_bind\n");
240
241 /* Allocate string descriptor numbers ... note that string
242 * contents can be overridden by the composite_dev glue.
243 */
244 id = usb_string_id(cdev);
245 if (id < 0)
246 return id;
247 strings_dev[STRING_MANUFACTURER_IDX].id = id;
248 device_desc.iManufacturer = id;
249
250 id = usb_string_id(cdev);
251 if (id < 0)
252 return id;
253 strings_dev[STRING_PRODUCT_IDX].id = id;
254 device_desc.iProduct = id;
255
256 id = usb_string_id(cdev);
257 if (id < 0)
258 return id;
259 strings_dev[STRING_SERIAL_IDX].id = id;
260 device_desc.iSerialNumber = id;
261
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530262 if (gadget->ops->wakeup)
263 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
264
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500265 /* register our configuration */
266 ret = usb_add_config(cdev, &android_config_driver);
267 if (ret) {
268 printk(KERN_ERR "usb_add_config failed\n");
269 return ret;
270 }
271
272 gcnum = usb_gadget_controller_number(gadget);
273 if (gcnum >= 0)
274 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
275 else {
276 /* gadget zero is so simple (for now, no altsettings) that
277 * it SHOULD NOT have problems with bulk-capable hardware.
278 * so just warn about unrcognized controllers -- don't panic.
279 *
280 * things like configuration and altsetting numbering
281 * can need hardware-specific attention though.
282 */
283 pr_warning("%s: controller '%s' not recognized\n",
284 longname, gadget->name);
285 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
286 }
287
288 usb_gadget_set_selfpowered(gadget);
289 dev->cdev = cdev;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530290 product_id = get_product_id(dev);
291 device_desc.idProduct = __constant_cpu_to_le16(product_id);
292 cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500293
294 return 0;
295}
296
297static struct usb_composite_driver android_usb_driver = {
298 .name = "android_usb",
299 .dev = &device_desc,
300 .strings = dev_strings,
301 .bind = android_bind,
Mike Lockwood05329732010-02-06 21:54:31 -0500302 .enable_function = android_enable_function,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500303};
304
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530305void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500306{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530307 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500308
Mike Lockwood789ef232010-01-12 10:33:59 -0800309 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530310 list_add_tail(&f->list, &_functions);
311 _registered_function_count++;
312
313 /* bind our functions if they have all registered
314 * and the main driver has bound.
315 */
316 if (dev->config && _registered_function_count == dev->num_functions)
317 bind_functions(dev);
318}
319
320void android_enable_function(struct usb_function *f, int enable)
321{
322 struct android_dev *dev = _android_dev;
323 int disable = !enable;
324 int product_id;
325
326 if (!!f->hidden != disable) {
327 f->hidden = disable;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500328
Mike Lockwoodda574e22010-02-13 16:37:16 -0500329#ifdef CONFIG_USB_ANDROID_RNDIS
Mike Lockwoodda574e22010-02-13 16:37:16 -0500330 if (!strcmp(f->name, "rndis")) {
Mike Lockwood35220862010-02-24 10:20:59 -0500331 struct usb_function *func;
332
333 /* We need to specify the COMM class in the device descriptor
334 * if we are using RNDIS.
335 */
Mike Lockwoodda574e22010-02-13 16:37:16 -0500336 if (enable)
Steve Kondik8f5f3912010-02-17 12:59:29 -0500337#ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
338 dev->cdev->desc.bDeviceClass = USB_CLASS_WIRELESS_CONTROLLER;
339#else
Mike Lockwoodda574e22010-02-13 16:37:16 -0500340 dev->cdev->desc.bDeviceClass = USB_CLASS_COMM;
Steve Kondik8f5f3912010-02-17 12:59:29 -0500341#endif
Mike Lockwoodda574e22010-02-13 16:37:16 -0500342 else
343 dev->cdev->desc.bDeviceClass = USB_CLASS_PER_INTERFACE;
Mike Lockwood35220862010-02-24 10:20:59 -0500344
345 /* Windows does not support other interfaces when RNDIS is enabled,
346 * so we disable UMS when RNDIS is on.
347 */
348 list_for_each_entry(func, &android_config_driver.functions, list) {
349 if (!strcmp(func->name, "usb_mass_storage")) {
350 func->hidden = enable;
351 break;
352 }
353 }
Mike Lockwoodda574e22010-02-13 16:37:16 -0500354 }
355#endif
356
Mike Lockwood35220862010-02-24 10:20:59 -0500357 product_id = get_product_id(dev);
358 device_desc.idProduct = __constant_cpu_to_le16(product_id);
359 if (dev->cdev)
360 dev->cdev->desc.idProduct = device_desc.idProduct;
361
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500362 /* force reenumeration */
363 if (dev->cdev && dev->cdev->gadget &&
364 dev->cdev->gadget->speed != USB_SPEED_UNKNOWN) {
365 usb_gadget_disconnect(dev->cdev->gadget);
366 msleep(10);
367 usb_gadget_connect(dev->cdev->gadget);
368 }
369 }
370}
371
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500372static int __init android_probe(struct platform_device *pdev)
373{
374 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
375 struct android_dev *dev = _android_dev;
376
377 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
378
379 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530380 dev->products = pdata->products;
381 dev->num_products = pdata->num_products;
382 dev->functions = pdata->functions;
383 dev->num_functions = pdata->num_functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500384 if (pdata->vendor_id)
385 device_desc.idVendor =
386 __constant_cpu_to_le16(pdata->vendor_id);
387 if (pdata->product_id) {
388 dev->product_id = pdata->product_id;
389 device_desc.idProduct =
390 __constant_cpu_to_le16(pdata->product_id);
391 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500392 if (pdata->version)
393 dev->version = pdata->version;
394
395 if (pdata->product_name)
396 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
397 if (pdata->manufacturer_name)
398 strings_dev[STRING_MANUFACTURER_IDX].s =
399 pdata->manufacturer_name;
400 if (pdata->serial_number)
401 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500402 }
403
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530404 return usb_composite_register(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500405}
406
407static struct platform_driver android_platform_driver = {
408 .driver = { .name = "android_usb", },
409 .probe = android_probe,
410};
411
412static int __init init(void)
413{
414 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500415
416 printk(KERN_INFO "android init\n");
417
418 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
419 if (!dev)
420 return -ENOMEM;
421
422 /* set default values, which should be overridden by platform data */
423 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500424 _android_dev = dev;
425
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530426 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500427}
428module_init(init);
429
430static void __exit cleanup(void)
431{
432 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500433 platform_driver_unregister(&android_platform_driver);
434 kfree(_android_dev);
435 _android_dev = NULL;
436}
437module_exit(cleanup);