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.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/poll.h>
21 #include <linux/delay.h>
22 #include <linux/wait.h>
23 #include <linux/err.h>
24 #include <linux/interrupt.h>
25 #include <linux/sched.h>
26 #include <linux/types.h>
27 #include <linux/device.h>
28 #include <linux/miscdevice.h>
30 #define ADB_BULK_BUFFER_SIZE 4096
32 /* number of tx requests to allocate */
35 static const char adb_shortname[] = "android_adb";
38 struct usb_function function;
39 struct usb_composite_dev *cdev;
43 struct usb_ep *ep_out;
52 struct list_head tx_idle;
54 wait_queue_head_t read_wq;
55 wait_queue_head_t write_wq;
56 struct usb_request *rx_req;
60 static struct usb_interface_descriptor adb_interface_desc = {
61 .bLength = USB_DT_INTERFACE_SIZE,
62 .bDescriptorType = USB_DT_INTERFACE,
63 .bInterfaceNumber = 0,
65 .bInterfaceClass = 0xFF,
66 .bInterfaceSubClass = 0x42,
67 .bInterfaceProtocol = 1,
70 static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = USB_DIR_IN,
74 .bmAttributes = USB_ENDPOINT_XFER_BULK,
75 .wMaxPacketSize = __constant_cpu_to_le16(64),
78 static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_OUT,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
83 .wMaxPacketSize = __constant_cpu_to_le16(512),
86 static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_IN,
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
93 static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
94 .bLength = USB_DT_ENDPOINT_SIZE,
95 .bDescriptorType = USB_DT_ENDPOINT,
96 .bEndpointAddress = USB_DIR_OUT,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
100 static struct usb_descriptor_header *fs_adb_descs[] = {
101 (struct usb_descriptor_header *) &adb_interface_desc,
102 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
103 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
107 static struct usb_descriptor_header *hs_adb_descs[] = {
108 (struct usb_descriptor_header *) &adb_interface_desc,
109 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
110 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
115 /* temporary variable used between adb_open() and adb_gadget_bind() */
116 static struct adb_dev *_adb_dev;
118 static inline struct adb_dev *func_to_adb(struct usb_function *f)
120 return container_of(f, struct adb_dev, function);
124 static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
126 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
130 /* now allocate buffers for the requests */
131 req->buf = kmalloc(buffer_size, GFP_KERNEL);
133 usb_ep_free_request(ep, req);
140 static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
144 usb_ep_free_request(ep, req);
148 static inline int adb_lock(atomic_t *excl)
150 if (atomic_inc_return(excl) == 1) {
158 static inline void adb_unlock(atomic_t *excl)
163 /* add a request to the tail of a list */
164 void adb_req_put(struct adb_dev *dev, struct list_head *head,
165 struct usb_request *req)
169 spin_lock_irqsave(&dev->lock, flags);
170 list_add_tail(&req->list, head);
171 spin_unlock_irqrestore(&dev->lock, flags);
174 /* remove a request from the head of a list */
175 struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
178 struct usb_request *req;
180 spin_lock_irqsave(&dev->lock, flags);
181 if (list_empty(head)) {
184 req = list_first_entry(head, struct usb_request, list);
185 list_del(&req->list);
187 spin_unlock_irqrestore(&dev->lock, flags);
191 static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
193 struct adb_dev *dev = _adb_dev;
195 if (req->status != 0)
198 adb_req_put(dev, &dev->tx_idle, req);
200 wake_up(&dev->write_wq);
203 static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
205 struct adb_dev *dev = _adb_dev;
208 if (req->status != 0)
211 wake_up(&dev->read_wq);
214 static int adb_create_bulk_endpoints(struct adb_dev *dev,
215 struct usb_endpoint_descriptor *in_desc,
216 struct usb_endpoint_descriptor *out_desc)
218 struct usb_composite_dev *cdev = dev->cdev;
219 struct usb_request *req;
223 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
225 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
227 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
230 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
231 ep->driver_data = dev; /* claim the endpoint */
234 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
236 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
239 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
240 ep->driver_data = dev; /* claim the endpoint */
243 /* now allocate requests for our endpoints */
244 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
247 req->complete = adb_complete_out;
250 for (i = 0; i < TX_REQ_MAX; i++) {
251 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
254 req->complete = adb_complete_in;
255 adb_req_put(dev, &dev->tx_idle, req);
261 printk(KERN_ERR "adb_bind() could not allocate requests\n");
265 static ssize_t adb_read(struct file *fp, char __user *buf,
266 size_t count, loff_t *pos)
268 struct adb_dev *dev = fp->private_data;
269 struct usb_request *req;
273 pr_debug("adb_read(%d)\n", count);
277 if (count > ADB_BULK_BUFFER_SIZE)
280 if (adb_lock(&dev->read_excl))
283 /* we will block until we're online */
284 while (!(dev->online || dev->error)) {
285 pr_debug("adb_read: waiting for online state\n");
286 ret = wait_event_interruptible(dev->read_wq,
287 (dev->online || dev->error));
289 adb_unlock(&dev->read_excl);
299 /* queue a request */
303 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
305 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
310 pr_debug("rx %p queue\n", req);
313 /* wait for a request to complete */
314 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
318 usb_ep_dequeue(dev->ep_out, req);
322 /* If we got a 0-len packet, throw it back and try again. */
323 if (req->actual == 0)
326 pr_debug("rx %p %d\n", req, req->actual);
327 xfer = (req->actual < count) ? req->actual : count;
328 if (copy_to_user(buf, req->buf, xfer))
335 adb_unlock(&dev->read_excl);
336 pr_debug("adb_read returning %d\n", r);
340 static ssize_t adb_write(struct file *fp, const char __user *buf,
341 size_t count, loff_t *pos)
343 struct adb_dev *dev = fp->private_data;
344 struct usb_request *req = 0;
350 pr_debug("adb_write(%d)\n", count);
352 if (adb_lock(&dev->write_excl))
357 pr_debug("adb_write dev->error\n");
362 /* get an idle tx request to use */
364 ret = wait_event_interruptible(dev->write_wq,
365 (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
373 if (count > ADB_BULK_BUFFER_SIZE)
374 xfer = ADB_BULK_BUFFER_SIZE;
377 if (copy_from_user(req->buf, buf, xfer)) {
383 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
385 pr_debug("adb_write: xfer error %d\n", ret);
394 /* zero this so we don't try to free it on error exit */
400 adb_req_put(dev, &dev->tx_idle, req);
402 adb_unlock(&dev->write_excl);
403 pr_debug("adb_write returning %d\n", r);
407 static int adb_open(struct inode *ip, struct file *fp)
409 printk(KERN_INFO "adb_open\n");
413 if (adb_lock(&_adb_dev->open_excl))
416 fp->private_data = _adb_dev;
418 /* clear the error latch */
424 static int adb_release(struct inode *ip, struct file *fp)
426 printk(KERN_INFO "adb_release\n");
427 adb_unlock(&_adb_dev->open_excl);
431 /* file operations for ADB device /dev/android_adb */
432 static struct file_operations adb_fops = {
433 .owner = THIS_MODULE,
437 .release = adb_release,
440 static struct miscdevice adb_device = {
441 .minor = MISC_DYNAMIC_MINOR,
442 .name = adb_shortname,
450 adb_function_bind(struct usb_configuration *c, struct usb_function *f)
452 struct usb_composite_dev *cdev = c->cdev;
453 struct adb_dev *dev = func_to_adb(f);
458 DBG(cdev, "adb_function_bind dev: %p\n", dev);
460 /* allocate interface ID(s) */
461 id = usb_interface_id(c, f);
464 adb_interface_desc.bInterfaceNumber = id;
466 /* allocate endpoints */
467 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
468 &adb_fullspeed_out_desc);
472 /* support high speed hardware */
473 if (gadget_is_dualspeed(c->cdev->gadget)) {
474 adb_highspeed_in_desc.bEndpointAddress =
475 adb_fullspeed_in_desc.bEndpointAddress;
476 adb_highspeed_out_desc.bEndpointAddress =
477 adb_fullspeed_out_desc.bEndpointAddress;
480 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
481 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
482 f->name, dev->ep_in->name, dev->ep_out->name);
487 adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
489 struct adb_dev *dev = func_to_adb(f);
490 struct usb_request *req;
496 wake_up(&dev->read_wq);
498 adb_request_free(dev->rx_req, dev->ep_out);
499 while ((req = adb_req_get(dev, &dev->tx_idle)))
500 adb_request_free(req, dev->ep_in);
503 static int adb_function_set_alt(struct usb_function *f,
504 unsigned intf, unsigned alt)
506 struct adb_dev *dev = func_to_adb(f);
507 struct usb_composite_dev *cdev = f->config->cdev;
510 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
511 ret = usb_ep_enable(dev->ep_in,
512 ep_choose(cdev->gadget,
513 &adb_highspeed_in_desc,
514 &adb_fullspeed_in_desc));
517 ret = usb_ep_enable(dev->ep_out,
518 ep_choose(cdev->gadget,
519 &adb_highspeed_out_desc,
520 &adb_fullspeed_out_desc));
522 usb_ep_disable(dev->ep_in);
527 /* readers may be blocked waiting for us to go online */
528 wake_up(&dev->read_wq);
532 static void adb_function_disable(struct usb_function *f)
534 struct adb_dev *dev = func_to_adb(f);
535 struct usb_composite_dev *cdev = dev->cdev;
537 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
540 usb_ep_disable(dev->ep_in);
541 dev->ep_in->driver_data = NULL;
542 usb_ep_disable(dev->ep_out);
543 dev->ep_out->driver_data = NULL;
545 /* readers may be blocked waiting for us to go online */
546 wake_up(&dev->read_wq);
548 VDBG(cdev, "%s disabled\n", dev->function.name);
551 static int adb_bind_config(struct usb_configuration *c)
553 struct adb_dev *dev = _adb_dev;
556 dev->function.name = "adb";
557 dev->function.descriptors = fs_adb_descs;
558 dev->function.hs_descriptors = hs_adb_descs;
559 dev->function.bind = adb_function_bind;
560 dev->function.unbind = adb_function_unbind;
561 dev->function.set_alt = adb_function_set_alt;
562 dev->function.disable = adb_function_disable;
564 return usb_add_function(c, &dev->function);
567 static int adb_setup(void)
572 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
576 spin_lock_init(&dev->lock);
578 init_waitqueue_head(&dev->read_wq);
579 init_waitqueue_head(&dev->write_wq);
581 atomic_set(&dev->open_excl, 0);
582 atomic_set(&dev->read_excl, 0);
583 atomic_set(&dev->write_excl, 0);
585 INIT_LIST_HEAD(&dev->tx_idle);
589 ret = misc_register(&adb_device);
597 printk(KERN_ERR "adb gadget driver failed to initialize\n");
601 static void adb_cleanup(void)
603 misc_deregister(&adb_device);