2 * Gadget Driver for Android
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
19 /* #define VERBOSE_DEBUG */
21 #include <linux/init.h>
22 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/utsname.h>
28 #include <linux/platform_device.h>
30 #include <linux/usb/android_composite.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/composite.h>
33 #include <linux/usb/gadget.h>
35 #include "gadget_chips.h"
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.
44 #include "usbstring.c"
46 #include "epautoconf.c"
47 #include "composite.c"
49 MODULE_AUTHOR("Mike Lockwood");
50 MODULE_DESCRIPTION("Android Composite USB Driver");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION("1.0");
54 static const char longname[] = "Gadget Android";
56 /* Default vendor and product IDs, overridden by platform data */
57 #define VENDOR_ID 0x18D1
58 #define PRODUCT_ID 0x0001
61 struct usb_composite_dev *cdev;
62 struct usb_configuration *config;
64 struct android_usb_product *products;
72 static struct android_dev *_android_dev;
74 /* string IDs are assigned dynamically */
76 #define STRING_MANUFACTURER_IDX 0
77 #define STRING_PRODUCT_IDX 1
78 #define STRING_SERIAL_IDX 2
81 static 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",
89 static struct usb_gadget_strings stringtab_dev = {
90 .language = 0x0409, /* en-us */
91 .strings = strings_dev,
94 static struct usb_gadget_strings *dev_strings[] = {
99 static 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,
110 static struct list_head _functions = LIST_HEAD_INIT(_functions);
111 static int _registered_function_count = 0;
113 static struct android_usb_function *get_function(const char *name)
115 struct android_usb_function *f;
116 list_for_each_entry(f, &_functions, list) {
117 if (!strcmp(name, f->name))
123 static void bind_functions(struct android_dev *dev)
125 struct android_usb_function *f;
126 char **functions = dev->functions;
129 for (i = 0; i < dev->num_functions; i++) {
130 char *name = *functions++;
131 f = get_function(name);
133 f->bind_config(dev->config);
135 printk(KERN_ERR "function %s not found in bind_functions\n", name);
139 static int android_bind_config(struct usb_configuration *c)
141 struct android_dev *dev = _android_dev;
143 printk(KERN_DEBUG "android_bind_config\n");
146 /* bind our functions if they have all registered */
147 if (_registered_function_count == dev->num_functions)
153 static int android_setup_config(struct usb_configuration *c,
154 const struct usb_ctrlrequest *ctrl);
156 static struct usb_configuration android_config_driver = {
158 .bind = android_bind_config,
159 .setup = android_setup_config,
160 .bConfigurationValue = 1,
161 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
162 .bMaxPower = 0xFA, /* 500ma */
165 static int android_setup_config(struct usb_configuration *c,
166 const struct usb_ctrlrequest *ctrl)
169 int ret = -EOPNOTSUPP;
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);
182 static int product_has_function(struct android_usb_product *p,
183 struct usb_function *f)
185 char **functions = p->functions;
186 int count = p->num_functions;
187 const char *name = f->name;
190 for (i = 0; i < count; i++) {
191 if (!strcmp(name, *functions++))
197 static int product_matches_functions(struct android_usb_product *p)
199 struct usb_function *f;
200 list_for_each_entry(f, &android_config_driver.functions, list) {
201 if (product_has_function(p, f) == !!f->disabled)
207 static int get_product_id(struct android_dev *dev)
209 struct android_usb_product *p = dev->products;
210 int count = dev->num_products;
214 for (i = 0; i < count; i++, p++) {
215 if (product_matches_functions(p))
216 return p->product_id;
219 /* use default product ID */
220 return dev->product_id;
223 static int android_bind(struct usb_composite_dev *cdev)
225 struct android_dev *dev = _android_dev;
226 struct usb_gadget *gadget = cdev->gadget;
227 int gcnum, id, product_id, ret;
229 printk(KERN_INFO "android_bind\n");
231 /* Allocate string descriptor numbers ... note that string
232 * contents can be overridden by the composite_dev glue.
234 id = usb_string_id(cdev);
237 strings_dev[STRING_MANUFACTURER_IDX].id = id;
238 device_desc.iManufacturer = id;
240 id = usb_string_id(cdev);
243 strings_dev[STRING_PRODUCT_IDX].id = id;
244 device_desc.iProduct = id;
246 id = usb_string_id(cdev);
249 strings_dev[STRING_SERIAL_IDX].id = id;
250 device_desc.iSerialNumber = id;
252 if (gadget->ops->wakeup)
253 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
255 /* register our configuration */
256 ret = usb_add_config(cdev, &android_config_driver);
258 printk(KERN_ERR "usb_add_config failed\n");
262 gcnum = usb_gadget_controller_number(gadget);
264 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
266 /* gadget zero is so simple (for now, no altsettings) that
267 * it SHOULD NOT have problems with bulk-capable hardware.
268 * so just warn about unrcognized controllers -- don't panic.
270 * things like configuration and altsetting numbering
271 * can need hardware-specific attention though.
273 pr_warning("%s: controller '%s' not recognized\n",
274 longname, gadget->name);
275 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
278 usb_gadget_set_selfpowered(gadget);
280 product_id = get_product_id(dev);
281 device_desc.idProduct = __constant_cpu_to_le16(product_id);
282 cdev->desc.idProduct = device_desc.idProduct;
287 static struct usb_composite_driver android_usb_driver = {
288 .name = "android_usb",
290 .strings = dev_strings,
291 .bind = android_bind,
292 .enable_function = android_enable_function,
295 void android_register_function(struct android_usb_function *f)
297 struct android_dev *dev = _android_dev;
299 printk(KERN_INFO "android_register_function %s\n", f->name);
300 list_add_tail(&f->list, &_functions);
301 _registered_function_count++;
303 /* bind our functions if they have all registered
304 * and the main driver has bound.
306 if (dev && dev->config && _registered_function_count == dev->num_functions)
310 void android_enable_function(struct usb_function *f, int enable)
312 struct android_dev *dev = _android_dev;
313 int disable = !enable;
316 if (!!f->disabled != disable) {
317 usb_function_set_enabled(f, !disable);
319 #ifdef CONFIG_USB_ANDROID_RNDIS
320 if (!strcmp(f->name, "rndis")) {
321 struct usb_function *func;
323 /* We need to specify the COMM class in the device descriptor
324 * if we are using RNDIS.
327 #ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
328 dev->cdev->desc.bDeviceClass = USB_CLASS_WIRELESS_CONTROLLER;
330 dev->cdev->desc.bDeviceClass = USB_CLASS_COMM;
333 dev->cdev->desc.bDeviceClass = USB_CLASS_PER_INTERFACE;
335 /* Windows does not support other interfaces when RNDIS is enabled,
336 * so we disable UMS when RNDIS is on.
338 list_for_each_entry(func, &android_config_driver.functions, list) {
339 if (!strcmp(func->name, "usb_mass_storage")) {
340 usb_function_set_enabled(func, !enable);
347 product_id = get_product_id(dev);
348 device_desc.idProduct = __constant_cpu_to_le16(product_id);
350 dev->cdev->desc.idProduct = device_desc.idProduct;
351 usb_composite_force_reset(dev->cdev);
355 static int android_probe(struct platform_device *pdev)
357 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
358 struct android_dev *dev = _android_dev;
360 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
363 dev->products = pdata->products;
364 dev->num_products = pdata->num_products;
365 dev->functions = pdata->functions;
366 dev->num_functions = pdata->num_functions;
367 if (pdata->vendor_id)
368 device_desc.idVendor =
369 __constant_cpu_to_le16(pdata->vendor_id);
370 if (pdata->product_id) {
371 dev->product_id = pdata->product_id;
372 device_desc.idProduct =
373 __constant_cpu_to_le16(pdata->product_id);
376 dev->version = pdata->version;
378 if (pdata->product_name)
379 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
380 if (pdata->manufacturer_name)
381 strings_dev[STRING_MANUFACTURER_IDX].s =
382 pdata->manufacturer_name;
383 if (pdata->serial_number)
384 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
387 return usb_composite_register(&android_usb_driver);
390 static struct platform_driver android_platform_driver = {
391 .driver = { .name = "android_usb", },
392 .probe = android_probe,
395 static int __init init(void)
397 struct android_dev *dev;
399 printk(KERN_INFO "android init\n");
401 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
405 /* set default values, which should be overridden by platform data */
406 dev->product_id = PRODUCT_ID;
409 return platform_driver_register(&android_platform_driver);
413 static void __exit cleanup(void)
415 usb_composite_unregister(&android_usb_driver);
416 platform_driver_unregister(&android_platform_driver);
420 module_exit(cleanup);