blob: d3c028d45cbcd4e76a4311cd1c8138eade275f78 [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>
28#include <linux/miscdevice.h>
29#include <linux/platform_device.h>
30
31#include <linux/usb/android.h>
32#include <linux/usb/ch9.h>
33#include <linux/usb/composite.h>
34#include <linux/usb/gadget.h>
35
36#include "f_mass_storage.h"
37#include "f_adb.h"
38
39#include "gadget_chips.h"
40
41/*
42 * Kbuild is not very cooperative with respect to linking separately
43 * compiled library objects into one module. So for now we won't use
44 * separate compilation ... ensuring init/exit sections work to shrink
45 * the runtime footprint, and giving us at least some parts of what
46 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
47 */
48#include "usbstring.c"
49#include "config.c"
50#include "epautoconf.c"
51#include "composite.c"
52
53MODULE_AUTHOR("Mike Lockwood");
54MODULE_DESCRIPTION("Android Composite USB Driver");
55MODULE_LICENSE("GPL");
56MODULE_VERSION("1.0");
57
58static const char longname[] = "Gadget Android";
59
60/* Default vendor and product IDs, overridden by platform data */
61#define VENDOR_ID 0x18D1
62#define PRODUCT_ID 0x0001
63#define ADB_PRODUCT_ID 0x0002
64
65struct android_dev {
66 struct usb_composite_dev *cdev;
67
68 int product_id;
69 int adb_product_id;
70 int version;
71
72 int adb_enabled;
73 int nluns;
74};
75
76static atomic_t adb_enable_excl;
77static struct android_dev *_android_dev;
78
79/* string IDs are assigned dynamically */
80
81#define STRING_MANUFACTURER_IDX 0
82#define STRING_PRODUCT_IDX 1
83#define STRING_SERIAL_IDX 2
84
85/* String Table */
86static struct usb_string strings_dev[] = {
87 /* These dummy values should be overridden by platform data */
88 [STRING_MANUFACTURER_IDX].s = "Android",
89 [STRING_PRODUCT_IDX].s = "Android",
90 [STRING_SERIAL_IDX].s = "0123456789ABCDEF",
91 { } /* end of list */
92};
93
94static struct usb_gadget_strings stringtab_dev = {
95 .language = 0x0409, /* en-us */
96 .strings = strings_dev,
97};
98
99static struct usb_gadget_strings *dev_strings[] = {
100 &stringtab_dev,
101 NULL,
102};
103
104static struct usb_device_descriptor device_desc = {
105 .bLength = sizeof(device_desc),
106 .bDescriptorType = USB_DT_DEVICE,
107 .bcdUSB = __constant_cpu_to_le16(0x0200),
108 .bDeviceClass = USB_CLASS_PER_INTERFACE,
109 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
110 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
111 .bcdDevice = __constant_cpu_to_le16(0xffff),
112 .bNumConfigurations = 1,
113};
114
115void android_usb_set_connected(int connected)
116{
117 if (_android_dev && _android_dev->cdev && _android_dev->cdev->gadget) {
118 if (connected)
119 usb_gadget_connect(_android_dev->cdev->gadget);
120 else
121 usb_gadget_disconnect(_android_dev->cdev->gadget);
122 }
123}
124
125static int __init android_bind_config(struct usb_configuration *c)
126{
127 struct android_dev *dev = _android_dev;
128 int ret;
129 printk(KERN_DEBUG "android_bind_config\n");
130
131 ret = mass_storage_function_add(dev->cdev, c, dev->nluns);
132 if (ret)
133 return ret;
134 return adb_function_add(dev->cdev, c);
135}
136
137static struct usb_configuration android_config_driver = {
138 .label = "android",
139 .bind = android_bind_config,
140 .bConfigurationValue = 1,
141 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
142 .bMaxPower = 0xFA, /* 500ma */
143};
144
145static int __init android_bind(struct usb_composite_dev *cdev)
146{
147 struct android_dev *dev = _android_dev;
148 struct usb_gadget *gadget = cdev->gadget;
149 int gcnum;
150 int id;
151 int ret;
152
153 printk(KERN_INFO "android_bind\n");
154
155 /* Allocate string descriptor numbers ... note that string
156 * contents can be overridden by the composite_dev glue.
157 */
158 id = usb_string_id(cdev);
159 if (id < 0)
160 return id;
161 strings_dev[STRING_MANUFACTURER_IDX].id = id;
162 device_desc.iManufacturer = id;
163
164 id = usb_string_id(cdev);
165 if (id < 0)
166 return id;
167 strings_dev[STRING_PRODUCT_IDX].id = id;
168 device_desc.iProduct = id;
169
170 id = usb_string_id(cdev);
171 if (id < 0)
172 return id;
173 strings_dev[STRING_SERIAL_IDX].id = id;
174 device_desc.iSerialNumber = id;
175
176 /* register our configuration */
177 ret = usb_add_config(cdev, &android_config_driver);
178 if (ret) {
179 printk(KERN_ERR "usb_add_config failed\n");
180 return ret;
181 }
182
183 gcnum = usb_gadget_controller_number(gadget);
184 if (gcnum >= 0)
185 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
186 else {
187 /* gadget zero is so simple (for now, no altsettings) that
188 * it SHOULD NOT have problems with bulk-capable hardware.
189 * so just warn about unrcognized controllers -- don't panic.
190 *
191 * things like configuration and altsetting numbering
192 * can need hardware-specific attention though.
193 */
194 pr_warning("%s: controller '%s' not recognized\n",
195 longname, gadget->name);
196 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
197 }
198
199 usb_gadget_set_selfpowered(gadget);
200 dev->cdev = cdev;
201
202 return 0;
203}
204
205static struct usb_composite_driver android_usb_driver = {
206 .name = "android_usb",
207 .dev = &device_desc,
208 .strings = dev_strings,
209 .bind = android_bind,
210};
211
212static void enable_adb(struct android_dev *dev, int enable)
213{
214 if (enable != dev->adb_enabled) {
215 dev->adb_enabled = enable;
216 adb_function_enable(enable);
217
218 /* set product ID to the appropriate value */
219 if (enable)
220 device_desc.idProduct =
221 __constant_cpu_to_le16(dev->adb_product_id);
222 else
223 device_desc.idProduct =
224 __constant_cpu_to_le16(dev->product_id);
225 if (dev->cdev)
226 dev->cdev->desc.idProduct = device_desc.idProduct;
227
228 /* force reenumeration */
229 if (dev->cdev && dev->cdev->gadget &&
230 dev->cdev->gadget->speed != USB_SPEED_UNKNOWN) {
231 usb_gadget_disconnect(dev->cdev->gadget);
232 msleep(10);
233 usb_gadget_connect(dev->cdev->gadget);
234 }
235 }
236}
237
238static int adb_enable_open(struct inode *ip, struct file *fp)
239{
240 if (atomic_inc_return(&adb_enable_excl) != 1) {
241 atomic_dec(&adb_enable_excl);
242 return -EBUSY;
243 }
244
245 printk(KERN_INFO "enabling adb\n");
246 enable_adb(_android_dev, 1);
247
248 return 0;
249}
250
251static int adb_enable_release(struct inode *ip, struct file *fp)
252{
253 printk(KERN_INFO "disabling adb\n");
254 enable_adb(_android_dev, 0);
255 atomic_dec(&adb_enable_excl);
256 return 0;
257}
258
259static const struct file_operations adb_enable_fops = {
260 .owner = THIS_MODULE,
261 .open = adb_enable_open,
262 .release = adb_enable_release,
263};
264
265static struct miscdevice adb_enable_device = {
266 .minor = MISC_DYNAMIC_MINOR,
267 .name = "android_adb_enable",
268 .fops = &adb_enable_fops,
269};
270
271static int __init android_probe(struct platform_device *pdev)
272{
273 struct android_usb_platform_data *pdata = pdev->dev.platform_data;
274 struct android_dev *dev = _android_dev;
275
276 printk(KERN_INFO "android_probe pdata: %p\n", pdata);
277
278 if (pdata) {
279 if (pdata->vendor_id)
280 device_desc.idVendor =
281 __constant_cpu_to_le16(pdata->vendor_id);
282 if (pdata->product_id) {
283 dev->product_id = pdata->product_id;
284 device_desc.idProduct =
285 __constant_cpu_to_le16(pdata->product_id);
286 }
287 if (pdata->adb_product_id)
288 dev->adb_product_id = pdata->adb_product_id;
289 if (pdata->version)
290 dev->version = pdata->version;
291
292 if (pdata->product_name)
293 strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
294 if (pdata->manufacturer_name)
295 strings_dev[STRING_MANUFACTURER_IDX].s =
296 pdata->manufacturer_name;
297 if (pdata->serial_number)
298 strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
299 dev->nluns = pdata->nluns;
300 }
301
302 return 0;
303}
304
305static struct platform_driver android_platform_driver = {
306 .driver = { .name = "android_usb", },
307 .probe = android_probe,
308};
309
310static int __init init(void)
311{
312 struct android_dev *dev;
313 int ret;
314
315 printk(KERN_INFO "android init\n");
316
317 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
318 if (!dev)
319 return -ENOMEM;
320
321 /* set default values, which should be overridden by platform data */
322 dev->product_id = PRODUCT_ID;
323 dev->adb_product_id = ADB_PRODUCT_ID;
324 _android_dev = dev;
325
326 ret = platform_driver_register(&android_platform_driver);
327 if (ret)
328 return ret;
329 ret = misc_register(&adb_enable_device);
330 if (ret) {
331 platform_driver_unregister(&android_platform_driver);
332 return ret;
333 }
334 ret = usb_composite_register(&android_usb_driver);
335 if (ret) {
336 misc_deregister(&adb_enable_device);
337 platform_driver_unregister(&android_platform_driver);
338 }
339 return ret;
340}
341module_init(init);
342
343static void __exit cleanup(void)
344{
345 usb_composite_unregister(&android_usb_driver);
346 misc_deregister(&adb_enable_device);
347 platform_driver_unregister(&android_platform_driver);
348 kfree(_android_dev);
349 _android_dev = NULL;
350}
351module_exit(cleanup);