2 * Gadget Driver for Android ADB
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/module.h>
22 #include <linux/init.h>
23 #include <linux/poll.h>
24 #include <linux/delay.h>
25 #include <linux/wait.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
29 #include <linux/types.h>
30 #include <linux/device.h>
31 #include <linux/miscdevice.h>
33 #include <linux/usb/android_composite.h>
35 #define BULK_BUFFER_SIZE 4096
37 /* number of tx requests to allocate */
40 static const char shortname[] = "android_adb";
43 struct usb_function function;
44 struct usb_composite_dev *cdev;
48 struct usb_ep *ep_out;
57 struct list_head tx_idle;
59 wait_queue_head_t read_wq;
60 wait_queue_head_t write_wq;
61 struct usb_request *rx_req;
65 static struct usb_interface_descriptor adb_interface_desc = {
66 .bLength = USB_DT_INTERFACE_SIZE,
67 .bDescriptorType = USB_DT_INTERFACE,
68 .bInterfaceNumber = 0,
70 .bInterfaceClass = 0xFF,
71 .bInterfaceSubClass = 0x42,
72 .bInterfaceProtocol = 1,
75 static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
76 .bLength = USB_DT_ENDPOINT_SIZE,
77 .bDescriptorType = USB_DT_ENDPOINT,
78 .bEndpointAddress = USB_DIR_IN,
79 .bmAttributes = USB_ENDPOINT_XFER_BULK,
80 .wMaxPacketSize = __constant_cpu_to_le16(512),
83 static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
84 .bLength = USB_DT_ENDPOINT_SIZE,
85 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = USB_DIR_OUT,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 .wMaxPacketSize = __constant_cpu_to_le16(512),
91 static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101 .bEndpointAddress = USB_DIR_OUT,
102 .bmAttributes = USB_ENDPOINT_XFER_BULK,
105 static struct usb_descriptor_header *fs_adb_descs[] = {
106 (struct usb_descriptor_header *) &adb_interface_desc,
107 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
108 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
112 static struct usb_descriptor_header *hs_adb_descs[] = {
113 (struct usb_descriptor_header *) &adb_interface_desc,
114 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
115 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
120 /* temporary variable used between adb_open() and adb_gadget_bind() */
121 static struct adb_dev *_adb_dev;
123 static atomic_t adb_enable_excl;
125 static inline struct adb_dev *func_to_dev(struct usb_function *f)
127 return container_of(f, struct adb_dev, function);
131 static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
133 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
137 /* now allocate buffers for the requests */
138 req->buf = kmalloc(buffer_size, GFP_KERNEL);
140 usb_ep_free_request(ep, req);
147 static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
151 usb_ep_free_request(ep, req);
155 static inline int _lock(atomic_t *excl)
157 if (atomic_inc_return(excl) == 1) {
165 static inline void _unlock(atomic_t *excl)
170 /* add a request to the tail of a list */
171 void req_put(struct adb_dev *dev, struct list_head *head,
172 struct usb_request *req)
176 spin_lock_irqsave(&dev->lock, flags);
177 list_add_tail(&req->list, head);
178 spin_unlock_irqrestore(&dev->lock, flags);
181 /* remove a request from the head of a list */
182 struct usb_request *req_get(struct adb_dev *dev, struct list_head *head)
185 struct usb_request *req;
187 spin_lock_irqsave(&dev->lock, flags);
188 if (list_empty(head)) {
191 req = list_first_entry(head, struct usb_request, list);
192 list_del(&req->list);
194 spin_unlock_irqrestore(&dev->lock, flags);
198 static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
200 struct adb_dev *dev = _adb_dev;
202 if (req->status != 0)
205 req_put(dev, &dev->tx_idle, req);
207 wake_up(&dev->write_wq);
210 static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
212 struct adb_dev *dev = _adb_dev;
215 if (req->status != 0)
218 wake_up(&dev->read_wq);
221 static int __init create_bulk_endpoints(struct adb_dev *dev,
222 struct usb_endpoint_descriptor *in_desc,
223 struct usb_endpoint_descriptor *out_desc)
225 struct usb_composite_dev *cdev = dev->cdev;
226 struct usb_request *req;
230 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
232 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
234 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
237 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
240 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
242 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
245 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
248 /* now allocate requests for our endpoints */
249 req = adb_request_new(dev->ep_out, BULK_BUFFER_SIZE);
252 req->complete = adb_complete_out;
255 for (i = 0; i < TX_REQ_MAX; i++) {
256 req = adb_request_new(dev->ep_in, BULK_BUFFER_SIZE);
259 req->complete = adb_complete_in;
260 req_put(dev, &dev->tx_idle, req);
266 printk(KERN_ERR "adb_bind() could not allocate requests\n");
270 static ssize_t adb_read(struct file *fp, char __user *buf,
271 size_t count, loff_t *pos)
273 struct adb_dev *dev = fp->private_data;
274 struct usb_composite_dev *cdev = dev->cdev;
275 struct usb_request *req;
279 DBG(cdev, "adb_read(%d)\n", count);
281 if (count > BULK_BUFFER_SIZE)
284 if (_lock(&dev->read_excl))
287 /* we will block until we're online */
288 while (!(dev->online || dev->error)) {
289 DBG(cdev, "adb_read: waiting for online state\n");
290 ret = wait_event_interruptible(dev->read_wq,
291 (dev->online || dev->error));
293 _unlock(&dev->read_excl);
303 /* queue a request */
307 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
309 DBG(cdev, "adb_read: failed to queue req %p (%d)\n", req, ret);
314 DBG(cdev, "rx %p queue\n", req);
317 /* wait for a request to complete */
318 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
325 /* If we got a 0-len packet, throw it back and try again. */
326 if (req->actual == 0)
329 DBG(cdev, "rx %p %d\n", req, req->actual);
330 xfer = (req->actual < count) ? req->actual : count;
331 if (copy_to_user(buf, req->buf, xfer))
337 _unlock(&dev->read_excl);
338 DBG(cdev, "adb_read returning %d\n", r);
342 static ssize_t adb_write(struct file *fp, const char __user *buf,
343 size_t count, loff_t *pos)
345 struct adb_dev *dev = fp->private_data;
346 struct usb_composite_dev *cdev = dev->cdev;
347 struct usb_request *req = 0;
351 DBG(cdev, "adb_write(%d)\n", count);
353 if (_lock(&dev->write_excl))
358 DBG(cdev, "adb_write dev->error\n");
363 /* get an idle tx request to use */
365 ret = wait_event_interruptible(dev->write_wq,
366 ((req = req_get(dev, &dev->tx_idle)) || dev->error));
374 if (count > BULK_BUFFER_SIZE)
375 xfer = BULK_BUFFER_SIZE;
378 if (copy_from_user(req->buf, buf, xfer)) {
384 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
386 DBG(cdev, "adb_write: xfer error %d\n", ret);
395 /* zero this so we don't try to free it on error exit */
401 req_put(dev, &dev->tx_idle, req);
403 _unlock(&dev->write_excl);
404 DBG(cdev, "adb_write returning %d\n", r);
408 static int adb_open(struct inode *ip, struct file *fp)
410 printk(KERN_INFO "adb_open\n");
411 if (_lock(&_adb_dev->open_excl))
414 fp->private_data = _adb_dev;
416 /* clear the error latch */
422 static int adb_release(struct inode *ip, struct file *fp)
424 printk(KERN_INFO "adb_release\n");
425 _unlock(&_adb_dev->open_excl);
429 /* file operations for ADB device /dev/android_adb */
430 static struct file_operations adb_fops = {
431 .owner = THIS_MODULE,
435 .release = adb_release,
438 static struct miscdevice adb_device = {
439 .minor = MISC_DYNAMIC_MINOR,
444 static int adb_enable_open(struct inode *ip, struct file *fp)
446 if (atomic_inc_return(&adb_enable_excl) != 1) {
447 atomic_dec(&adb_enable_excl);
451 printk(KERN_INFO "enabling adb\n");
452 android_enable_function(&_adb_dev->function, 1);
457 static int adb_enable_release(struct inode *ip, struct file *fp)
459 printk(KERN_INFO "disabling adb\n");
460 android_enable_function(&_adb_dev->function, 0);
461 atomic_dec(&adb_enable_excl);
465 static const struct file_operations adb_enable_fops = {
466 .owner = THIS_MODULE,
467 .open = adb_enable_open,
468 .release = adb_enable_release,
471 static struct miscdevice adb_enable_device = {
472 .minor = MISC_DYNAMIC_MINOR,
473 .name = "android_adb_enable",
474 .fops = &adb_enable_fops,
478 adb_function_bind(struct usb_configuration *c, struct usb_function *f)
480 struct usb_composite_dev *cdev = c->cdev;
481 struct adb_dev *dev = func_to_dev(f);
486 DBG(cdev, "adb_function_bind dev: %p\n", dev);
488 /* allocate interface ID(s) */
489 id = usb_interface_id(c, f);
492 adb_interface_desc.bInterfaceNumber = id;
494 /* allocate endpoints */
495 ret = create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
496 &adb_fullspeed_out_desc);
500 /* support high speed hardware */
501 if (gadget_is_dualspeed(c->cdev->gadget)) {
502 adb_highspeed_in_desc.bEndpointAddress =
503 adb_fullspeed_in_desc.bEndpointAddress;
504 adb_highspeed_out_desc.bEndpointAddress =
505 adb_fullspeed_out_desc.bEndpointAddress;
508 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
509 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
510 f->name, dev->ep_in->name, dev->ep_out->name);
515 adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
517 struct adb_dev *dev = func_to_dev(f);
518 struct usb_request *req;
520 spin_lock_irq(&dev->lock);
522 adb_request_free(dev->rx_req, dev->ep_out);
523 while ((req = req_get(dev, &dev->tx_idle)))
524 adb_request_free(req, dev->ep_in);
528 spin_unlock_irq(&dev->lock);
530 misc_deregister(&adb_device);
531 misc_deregister(&adb_enable_device);
536 static int adb_function_set_alt(struct usb_function *f,
537 unsigned intf, unsigned alt)
539 struct adb_dev *dev = func_to_dev(f);
540 struct usb_composite_dev *cdev = f->config->cdev;
543 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
544 ret = usb_ep_enable(dev->ep_in,
545 ep_choose(cdev->gadget,
546 &adb_highspeed_in_desc,
547 &adb_fullspeed_in_desc));
550 ret = usb_ep_enable(dev->ep_out,
551 ep_choose(cdev->gadget,
552 &adb_highspeed_out_desc,
553 &adb_fullspeed_out_desc));
555 usb_ep_disable(dev->ep_in);
560 /* readers may be blocked waiting for us to go online */
561 wake_up(&dev->read_wq);
565 static void adb_function_disable(struct usb_function *f)
567 struct adb_dev *dev = func_to_dev(f);
568 struct usb_composite_dev *cdev = dev->cdev;
570 DBG(cdev, "adb_function_disable\n");
573 usb_ep_disable(dev->ep_in);
574 usb_ep_disable(dev->ep_out);
576 /* readers may be blocked waiting for us to go online */
577 wake_up(&dev->read_wq);
579 VDBG(cdev, "%s disabled\n", dev->function.name);
582 static int adb_bind_config(struct usb_configuration *c)
587 printk(KERN_INFO "adb_bind_config\n");
589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
593 spin_lock_init(&dev->lock);
595 init_waitqueue_head(&dev->read_wq);
596 init_waitqueue_head(&dev->write_wq);
598 atomic_set(&dev->open_excl, 0);
599 atomic_set(&dev->read_excl, 0);
600 atomic_set(&dev->write_excl, 0);
602 INIT_LIST_HEAD(&dev->tx_idle);
605 dev->function.name = "adb";
606 dev->function.descriptors = fs_adb_descs;
607 dev->function.hs_descriptors = hs_adb_descs;
608 dev->function.bind = adb_function_bind;
609 dev->function.unbind = adb_function_unbind;
610 dev->function.set_alt = adb_function_set_alt;
611 dev->function.disable = adb_function_disable;
614 dev->function.hidden = 1;
616 /* _adb_dev must be set before calling usb_gadget_register_driver */
619 ret = misc_register(&adb_device);
622 ret = misc_register(&adb_enable_device);
626 ret = usb_add_function(c, &dev->function);
633 misc_deregister(&adb_enable_device);
635 misc_deregister(&adb_device);
638 printk(KERN_ERR "adb gadget driver failed to initialize\n");
642 static struct android_usb_function adb_function = {
644 .bind_config = adb_bind_config,
647 static int __init init(void)
649 printk(KERN_INFO "f_adb init\n");
650 android_register_function(&adb_function);