blob: ed4f5739cb9c5c05a793af860497d84e6b118a44 [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
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
123static void bind_functions(struct android_dev *dev)
124{
125 struct android_usb_function *f;
126 char **functions = dev->functions;
127 int i;
128
129 for (i = 0; i < dev->num_functions; i++) {
130 char *name = *functions++;
131 f = get_function(name);
132 if (f)
133 f->bind_config(dev->config);
134 else
135 printk(KERN_ERR "function %s not found in bind_functions\n", name);
136 }
137}
138
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700139static int android_bind_config(struct usb_configuration *c)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500140{
141 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500142
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530143 printk(KERN_DEBUG "android_bind_config\n");
144 dev->config = c;
145
146 /* bind our functions if they have all registered */
147 if (_registered_function_count == dev->num_functions)
148 bind_functions(dev);
149
150 return 0;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500151}
152
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530153static int android_setup_config(struct usb_configuration *c,
154 const struct usb_ctrlrequest *ctrl);
155
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500156static struct usb_configuration android_config_driver = {
157 .label = "android",
158 .bind = android_bind_config,
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530159 .setup = android_setup_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500160 .bConfigurationValue = 1,
161 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
162 .bMaxPower = 0xFA, /* 500ma */
163};
164
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530165static int android_setup_config(struct usb_configuration *c,
166 const struct usb_ctrlrequest *ctrl)
167{
168 int i;
169 int ret = -EOPNOTSUPP;
170
171 for (i = 0; i < android_config_driver.next_interface_id; i++) {
172 if (android_config_driver.interface[i]->setup) {
173 ret = android_config_driver.interface[i]->setup(
174 android_config_driver.interface[i], ctrl);
175 if (ret >= 0)
176 return ret;
177 }
178 }
179 return ret;
180}
181
182static int product_has_function(struct android_usb_product *p,
183 struct usb_function *f)
184{
185 char **functions = p->functions;
186 int count = p->num_functions;
187 const char *name = f->name;
188 int i;
189
190 for (i = 0; i < count; i++) {
191 if (!strcmp(name, *functions++))
192 return 1;
193 }
194 return 0;
195}
196
197static int product_matches_functions(struct android_usb_product *p)
198{
199 struct usb_function *f;
200 list_for_each_entry(f, &android_config_driver.functions, list) {
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400201 if (product_has_function(p, f) == !!f->disabled)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530202 return 0;
203 }
204 return 1;
205}
206
207static int get_product_id(struct android_dev *dev)
208{
209 struct android_usb_product *p = dev->products;
210 int count = dev->num_products;
211 int i;
212
213 if (p) {
214 for (i = 0; i < count; i++, p++) {
215 if (product_matches_functions(p))
216 return p->product_id;
217 }
218 }
219 /* use default product ID */
220 return dev->product_id;
221}
222
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700223static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500224{
225 struct android_dev *dev = _android_dev;
226 struct usb_gadget *gadget = cdev->gadget;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530227 int gcnum, id, product_id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500228
229 printk(KERN_INFO "android_bind\n");
230
231 /* Allocate string descriptor numbers ... note that string
232 * contents can be overridden by the composite_dev glue.
233 */
234 id = usb_string_id(cdev);
235 if (id < 0)
236 return id;
237 strings_dev[STRING_MANUFACTURER_IDX].id = id;
238 device_desc.iManufacturer = id;
239
240 id = usb_string_id(cdev);
241 if (id < 0)
242 return id;
243 strings_dev[STRING_PRODUCT_IDX].id = id;
244 device_desc.iProduct = id;
245
246 id = usb_string_id(cdev);
247 if (id < 0)
248 return id;
249 strings_dev[STRING_SERIAL_IDX].id = id;
250 device_desc.iSerialNumber = id;
251
252 /* register our configuration */
253 ret = usb_add_config(cdev, &android_config_driver);
254 if (ret) {
255 printk(KERN_ERR "usb_add_config failed\n");
256 return ret;
257 }
258
259 gcnum = usb_gadget_controller_number(gadget);
260 if (gcnum >= 0)
261 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
262 else {
263 /* gadget zero is so simple (for now, no altsettings) that
264 * it SHOULD NOT have problems with bulk-capable hardware.
265 * so just warn about unrcognized controllers -- don't panic.
266 *
267 * things like configuration and altsetting numbering
268 * can need hardware-specific attention though.
269 */
270 pr_warning("%s: controller '%s' not recognized\n",
271 longname, gadget->name);
272 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
273 }
274
275 usb_gadget_set_selfpowered(gadget);
276 dev->cdev = cdev;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530277 product_id = get_product_id(dev);
278 device_desc.idProduct = __constant_cpu_to_le16(product_id);
279 cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500280
281 return 0;
282}
283
284static struct usb_composite_driver android_usb_driver = {
285 .name = "android_usb",
286 .dev = &device_desc,
287 .strings = dev_strings,
288 .bind = android_bind,
Mike Lockwood05329732010-02-06 21:54:31 -0500289 .enable_function = android_enable_function,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500290};
291
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530292void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500293{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530294 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500295
Mike Lockwood789ef232010-01-12 10:33:59 -0800296 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530297 list_add_tail(&f->list, &_functions);
298 _registered_function_count++;
299
300 /* bind our functions if they have all registered
301 * and the main driver has bound.
302 */
Mike Lockwood0a4c12d2010-03-17 15:42:29 -0400303 if (dev && dev->config && _registered_function_count == dev->num_functions)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530304 bind_functions(dev);
305}
306
307void android_enable_function(struct usb_function *f, int enable)
308{
309 struct android_dev *dev = _android_dev;
310 int disable = !enable;
311 int product_id;
312
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400313 if (!!f->disabled != disable) {
314 usb_function_set_enabled(f, !disable);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500315
Mike Lockwoodda574e22010-02-13 16:37:16 -0500316#ifdef CONFIG_USB_ANDROID_RNDIS
Mike Lockwoodda574e22010-02-13 16:37:16 -0500317 if (!strcmp(f->name, "rndis")) {
Mike Lockwood35220862010-02-24 10:20:59 -0500318 struct usb_function *func;
319
320 /* We need to specify the COMM class in the device descriptor
321 * if we are using RNDIS.
322 */
Mike Lockwoodda574e22010-02-13 16:37:16 -0500323 if (enable)
Steve Kondik8f5f3912010-02-17 12:59:29 -0500324#ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
325 dev->cdev->desc.bDeviceClass = USB_CLASS_WIRELESS_CONTROLLER;
326#else
Mike Lockwoodda574e22010-02-13 16:37:16 -0500327 dev->cdev->desc.bDeviceClass = USB_CLASS_COMM;
Steve Kondik8f5f3912010-02-17 12:59:29 -0500328#endif
Mike Lockwoodda574e22010-02-13 16:37:16 -0500329 else
330 dev->cdev->desc.bDeviceClass = USB_CLASS_PER_INTERFACE;
Mike Lockwood35220862010-02-24 10:20:59 -0500331
332 /* Windows does not support other interfaces when RNDIS is enabled,
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400333 * so we disable UMS and MTP when RNDIS is on.
Mike Lockwood35220862010-02-24 10:20:59 -0500334 */
335 list_for_each_entry(func, &android_config_driver.functions, list) {
Mike Lockwoodd6b1d732010-08-23 08:17:21 -0400336 if (!strcmp(func->name, "usb_mass_storage")
337 || !strcmp(func->name, "mtp")) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400338 usb_function_set_enabled(func, !enable);
Mike Lockwood35220862010-02-24 10:20:59 -0500339 }
340 }
Mike Lockwoodda574e22010-02-13 16:37:16 -0500341 }
342#endif
343
Mike Lockwood35220862010-02-24 10:20:59 -0500344 product_id = get_product_id(dev);
345 device_desc.idProduct = __constant_cpu_to_le16(product_id);
346 if (dev->cdev)
347 dev->cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400348 usb_composite_force_reset(dev->cdev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500349 }
350}
351
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700352static int android_probe(struct platform_device *pdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500353{
354 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
355 struct android_dev *dev = _android_dev;
356
357 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
358
359 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530360 dev->products = pdata->products;
361 dev->num_products = pdata->num_products;
362 dev->functions = pdata->functions;
363 dev->num_functions = pdata->num_functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500364 if (pdata->vendor_id)
365 device_desc.idVendor =
366 __constant_cpu_to_le16(pdata->vendor_id);
367 if (pdata->product_id) {
368 dev->product_id = pdata->product_id;
369 device_desc.idProduct =
370 __constant_cpu_to_le16(pdata->product_id);
371 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500372 if (pdata->version)
373 dev->version = pdata->version;
374
375 if (pdata->product_name)
376 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
377 if (pdata->manufacturer_name)
378 strings_dev[STRING_MANUFACTURER_IDX].s =
379 pdata->manufacturer_name;
380 if (pdata->serial_number)
381 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500382 }
383
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530384 return usb_composite_register(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500385}
386
387static struct platform_driver android_platform_driver = {
388 .driver = { .name = "android_usb", },
389 .probe = android_probe,
390};
391
392static int __init init(void)
393{
394 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500395
396 printk(KERN_INFO "android init\n");
397
398 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
399 if (!dev)
400 return -ENOMEM;
401
402 /* set default values, which should be overridden by platform data */
403 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500404 _android_dev = dev;
405
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530406 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500407}
408module_init(init);
409
410static void __exit cleanup(void)
411{
412 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500413 platform_driver_unregister(&android_platform_driver);
414 kfree(_android_dev);
415 _android_dev = NULL;
416}
417module_exit(cleanup);