blob: ce7426be487d3dab601a34744c0a29a354889df3 [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,
302};
303
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530304void android_register_function(struct android_usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500305{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530306 struct android_dev *dev = _android_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500307
Mike Lockwood789ef232010-01-12 10:33:59 -0800308 printk(KERN_INFO "android_register_function %s\n", f->name);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530309 list_add_tail(&f->list, &_functions);
310 _registered_function_count++;
311
312 /* bind our functions if they have all registered
313 * and the main driver has bound.
314 */
315 if (dev->config && _registered_function_count == dev->num_functions)
316 bind_functions(dev);
317}
318
319void android_enable_function(struct usb_function *f, int enable)
320{
321 struct android_dev *dev = _android_dev;
322 int disable = !enable;
323 int product_id;
324
325 if (!!f->hidden != disable) {
326 f->hidden = disable;
327 product_id = get_product_id(dev);
328 device_desc.idProduct = __constant_cpu_to_le16(product_id);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500329 if (dev->cdev)
330 dev->cdev->desc.idProduct = device_desc.idProduct;
331
332 /* force reenumeration */
333 if (dev->cdev && dev->cdev->gadget &&
334 dev->cdev->gadget->speed != USB_SPEED_UNKNOWN) {
335 usb_gadget_disconnect(dev->cdev->gadget);
336 msleep(10);
337 usb_gadget_connect(dev->cdev->gadget);
338 }
339 }
340}
341
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500342static int __init android_probe(struct platform_device *pdev)
343{
344 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
345 struct android_dev *dev = _android_dev;
346
347 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
348
349 if (pdata) {
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530350 dev->products = pdata->products;
351 dev->num_products = pdata->num_products;
352 dev->functions = pdata->functions;
353 dev->num_functions = pdata->num_functions;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500354 if (pdata->vendor_id)
355 device_desc.idVendor =
356 __constant_cpu_to_le16(pdata->vendor_id);
357 if (pdata->product_id) {
358 dev->product_id = pdata->product_id;
359 device_desc.idProduct =
360 __constant_cpu_to_le16(pdata->product_id);
361 }
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500362 if (pdata->version)
363 dev->version = pdata->version;
364
365 if (pdata->product_name)
366 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
367 if (pdata->manufacturer_name)
368 strings_dev[STRING_MANUFACTURER_IDX].s =
369 pdata->manufacturer_name;
370 if (pdata->serial_number)
371 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500372 }
373
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530374 return usb_composite_register(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500375}
376
377static struct platform_driver android_platform_driver = {
378 .driver = { .name = "android_usb", },
379 .probe = android_probe,
380};
381
382static int __init init(void)
383{
384 struct android_dev *dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500385
386 printk(KERN_INFO "android init\n");
387
388 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
389 if (!dev)
390 return -ENOMEM;
391
392 /* set default values, which should be overridden by platform data */
393 dev->product_id = PRODUCT_ID;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500394 _android_dev = dev;
395
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530396 return platform_driver_register(&android_platform_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500397}
398module_init(init);
399
400static void __exit cleanup(void)
401{
402 usb_composite_unregister(&android_usb_driver);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500403 platform_driver_unregister(&android_platform_driver);
404 kfree(_android_dev);
405 _android_dev = NULL;
406}
407module_exit(cleanup);