blob: 73fc4b465ef5ddf604db3fd5a5ed5c82ac052482 [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),
Mike Lockwood573cea52010-02-10 15:26:58 -0500103#ifdef CONFIG_USB_ANDROID_RNDIS
104 /* we need to specify the class in the device descriptor
105 * if we are using RNDIS.
106 */
107 .bDeviceClass = USB_CLASS_COMM,
108#else
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500109 .bDeviceClass = USB_CLASS_PER_INTERFACE,
Mike Lockwood573cea52010-02-10 15:26:58 -0500110#endif
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500111 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
112 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
113 .bcdDevice = __constant_cpu_to_le16(0xffff),
114 .bNumConfigurations = 1,
115};
116
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530117static struct list_head _functions = LIST_HEAD_INIT(_functions);
118static int _registered_function_count = 0;
119
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500120void android_usb_set_connected(int connected)
121{
122 if (_android_dev && _android_dev->cdev && _android_dev->cdev->gadget) {
123 if (connected)
124 usb_gadget_connect(_android_dev->cdev->gadget);
125 else
126 usb_gadget_disconnect(_android_dev->cdev->gadget);
127 }
128}
129
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530130static struct android_usb_function *get_function(const char *name)
131{
132 struct android_usb_function *f;
133 list_for_each_entry(f, &_functions, list) {
134 if (!strcmp(name, f->name))
135 return f;
136 }
137 return 0;
138}
139
140static void bind_functions(struct android_dev *dev)
141{
142 struct android_usb_function *f;
143 char **functions = dev->functions;
144 int i;
145
146 for (i = 0; i < dev->num_functions; i++) {
147 char *name = *functions++;
148 f = get_function(name);
149 if (f)
150 f->bind_config(dev->config);
151 else
152 printk(KERN_ERR "function %s not found in bind_functions\n", name);
153 }
154}
155
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500156static int __init android_bind_config(struct usb_configuration *c)
157{
158 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500159
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530160 printk(KERN_DEBUG "android_bind_config\n");
161 dev->config = c;
162
163 /* bind our functions if they have all registered */
164 if (_registered_function_count == dev->num_functions)
165 bind_functions(dev);
166
167 return 0;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500168}
169
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530170static int android_setup_config(struct usb_configuration *c,
171 const struct usb_ctrlrequest *ctrl);
172
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500173static struct usb_configuration android_config_driver = {
174 .label = "android",
175 .bind = android_bind_config,
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530176 .setup = android_setup_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500177 .bConfigurationValue = 1,
178 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
179 .bMaxPower = 0xFA, /* 500ma */
180};
181
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530182static int android_setup_config(struct usb_configuration *c,
183 const struct usb_ctrlrequest *ctrl)
184{
185 int i;
186 int ret = -EOPNOTSUPP;
187
188 for (i = 0; i < android_config_driver.next_interface_id; i++) {
189 if (android_config_driver.interface[i]->setup) {
190 ret = android_config_driver.interface[i]->setup(
191 android_config_driver.interface[i], ctrl);
192 if (ret >= 0)
193 return ret;
194 }
195 }
196 return ret;
197}
198
199static int product_has_function(struct android_usb_product *p,
200 struct usb_function *f)
201{
202 char **functions = p->functions;
203 int count = p->num_functions;
204 const char *name = f->name;
205 int i;
206
207 for (i = 0; i < count; i++) {
208 if (!strcmp(name, *functions++))
209 return 1;
210 }
211 return 0;
212}
213
214static int product_matches_functions(struct android_usb_product *p)
215{
216 struct usb_function *f;
217 list_for_each_entry(f, &android_config_driver.functions, list) {
218 if (product_has_function(p, f) == !!f->hidden)
219 return 0;
220 }
221 return 1;
222}
223
224static int get_product_id(struct android_dev *dev)
225{
226 struct android_usb_product *p = dev->products;
227 int count = dev->num_products;
228 int i;
229
230 if (p) {
231 for (i = 0; i < count; i++, p++) {
232 if (product_matches_functions(p))
233 return p->product_id;
234 }
235 }
236 /* use default product ID */
237 return dev->product_id;
238}
239
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500240static int __init android_bind(struct usb_composite_dev *cdev)
241{
242 struct android_dev *dev = _android_dev;
243 struct usb_gadget *gadget = cdev->gadget;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530244 int gcnum, id, product_id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500245
246 printk(KERN_INFO "android_bind\n");
247
248 /* Allocate string descriptor numbers ... note that string
249 * contents can be overridden by the composite_dev glue.
250 */
251 id = usb_string_id(cdev);
252 if (id < 0)
253 return id;
254 strings_dev[STRING_MANUFACTURER_IDX].id = id;
255 device_desc.iManufacturer = id;
256
257 id = usb_string_id(cdev);
258 if (id < 0)
259 return id;
260 strings_dev[STRING_PRODUCT_IDX].id = id;
261 device_desc.iProduct = id;
262
263 id = usb_string_id(cdev);
264 if (id < 0)
265 return id;
266 strings_dev[STRING_SERIAL_IDX].id = id;
267 device_desc.iSerialNumber = id;
268
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530269 if (gadget->ops->wakeup)
270 android_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
271
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500272 /* register our configuration */
273 ret = usb_add_config(cdev, &android_config_driver);
274 if (ret) {
275 printk(KERN_ERR "usb_add_config failed\n");
276 return ret;
277 }
278
279 gcnum = usb_gadget_controller_number(gadget);
280 if (gcnum >= 0)
281 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
282 else {
283 /* gadget zero is so simple (for now, no altsettings) that
284 * it SHOULD NOT have problems with bulk-capable hardware.
285 * so just warn about unrcognized controllers -- don't panic.
286 *
287 * things like configuration and altsetting numbering
288 * can need hardware-specific attention though.
289 */
290 pr_warning("%s: controller '%s' not recognized\n",
291 longname, gadget->name);
292 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
293 }
294
295 usb_gadget_set_selfpowered(gadget);
296 dev->cdev = cdev;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530297 product_id = get_product_id(dev);
298 device_desc.idProduct = __constant_cpu_to_le16(product_id);
299 cdev->desc.idProduct = device_desc.idProduct;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500300
301 return 0;
302}
303
304static struct usb_composite_driver android_usb_driver = {
305 .name = "android_usb",
306 .dev = &device_desc,
307 .strings = dev_strings,
308 .bind = android_bind,
Mike Lockwood05329732010-02-06 21:54:31 -0500309 .enable_function = android_enable_function,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500310};
311
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530312void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500313{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530314 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500315
Mike Lockwood789ef232010-01-12 10:33:59 -0800316 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530317 list_add_tail(&f->list, &_functions);
318 _registered_function_count++;
319
320 /* bind our functions if they have all registered
321 * and the main driver has bound.
322 */
323 if (dev->config && _registered_function_count == dev->num_functions)
324 bind_functions(dev);
325}
326
327void android_enable_function(struct usb_function *f, int enable)
328{
329 struct android_dev *dev = _android_dev;
330 int disable = !enable;
331 int product_id;
332
333 if (!!f->hidden != disable) {
334 f->hidden = disable;
335 product_id = get_product_id(dev);
336 device_desc.idProduct = __constant_cpu_to_le16(product_id);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500337 if (dev->cdev)
338 dev->cdev->desc.idProduct = device_desc.idProduct;
339
340 /* force reenumeration */
341 if (dev->cdev && dev->cdev->gadget &&
342 dev->cdev->gadget->speed != USB_SPEED_UNKNOWN) {
343 usb_gadget_disconnect(dev->cdev->gadget);
344 msleep(10);
345 usb_gadget_connect(dev->cdev->gadget);
346 }
347 }
348}
349
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500350static int __init android_probe(struct platform_device *pdev)
351{
352 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
353 struct android_dev *dev = _android_dev;
354
355 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
356
357 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530358 dev->products = pdata->products;
359 dev->num_products = pdata->num_products;
360 dev->functions = pdata->functions;
361 dev->num_functions = pdata->num_functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500362 if (pdata->vendor_id)
363 device_desc.idVendor =
364 __constant_cpu_to_le16(pdata->vendor_id);
365 if (pdata->product_id) {
366 dev->product_id = pdata->product_id;
367 device_desc.idProduct =
368 __constant_cpu_to_le16(pdata->product_id);
369 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500370 if (pdata->version)
371 dev->version = pdata->version;
372
373 if (pdata->product_name)
374 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
375 if (pdata->manufacturer_name)
376 strings_dev[STRING_MANUFACTURER_IDX].s =
377 pdata->manufacturer_name;
378 if (pdata->serial_number)
379 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500380 }
381
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530382 return usb_composite_register(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500383}
384
385static struct platform_driver android_platform_driver = {
386 .driver = { .name = "android_usb", },
387 .probe = android_probe,
388};
389
390static int __init init(void)
391{
392 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500393
394 printk(KERN_INFO "android init\n");
395
396 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
397 if (!dev)
398 return -ENOMEM;
399
400 /* set default values, which should be overridden by platform data */
401 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500402 _android_dev = dev;
403
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530404 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500405}
406module_init(init);
407
408static void __exit cleanup(void)
409{
410 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500411 platform_driver_unregister(&android_platform_driver);
412 kfree(_android_dev);
413 _android_dev = NULL;
414}
415module_exit(cleanup);