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(512),
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,
114 static void adb_ready_callback(void);
115 static void adb_closed_callback(void);
117 /* temporary variable used between adb_open() and adb_gadget_bind() */
118 static struct adb_dev *_adb_dev;
120 static inline struct adb_dev *func_to_adb(struct usb_function *f)
122 return container_of(f, struct adb_dev, function);
126 static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
128 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
132 /* now allocate buffers for the requests */
133 req->buf = kmalloc(buffer_size, GFP_KERNEL);
135 usb_ep_free_request(ep, req);
142 static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
146 usb_ep_free_request(ep, req);
150 static inline int adb_lock(atomic_t *excl)
152 if (atomic_inc_return(excl) == 1) {
160 static inline void adb_unlock(atomic_t *excl)
165 /* add a request to the tail of a list */
166 void adb_req_put(struct adb_dev *dev, struct list_head *head,
167 struct usb_request *req)
171 spin_lock_irqsave(&dev->lock, flags);
172 list_add_tail(&req->list, head);
173 spin_unlock_irqrestore(&dev->lock, flags);
176 /* remove a request from the head of a list */
177 struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
180 struct usb_request *req;
182 spin_lock_irqsave(&dev->lock, flags);
183 if (list_empty(head)) {
186 req = list_first_entry(head, struct usb_request, list);
187 list_del(&req->list);
189 spin_unlock_irqrestore(&dev->lock, flags);
193 static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
195 struct adb_dev *dev = _adb_dev;
197 if (req->status != 0)
200 adb_req_put(dev, &dev->tx_idle, req);
202 wake_up(&dev->write_wq);
205 static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
207 struct adb_dev *dev = _adb_dev;
210 if (req->status != 0)
213 wake_up(&dev->read_wq);
216 static int adb_create_bulk_endpoints(struct adb_dev *dev,
217 struct usb_endpoint_descriptor *in_desc,
218 struct usb_endpoint_descriptor *out_desc)
220 struct usb_composite_dev *cdev = dev->cdev;
221 struct usb_request *req;
225 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
227 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
229 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
232 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
233 ep->driver_data = dev; /* claim the endpoint */
236 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
238 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
241 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
242 ep->driver_data = dev; /* claim the endpoint */
245 /* now allocate requests for our endpoints */
246 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
249 req->complete = adb_complete_out;
252 for (i = 0; i < TX_REQ_MAX; i++) {
253 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
256 req->complete = adb_complete_in;
257 adb_req_put(dev, &dev->tx_idle, req);
263 printk(KERN_ERR "adb_bind() could not allocate requests\n");
267 static ssize_t adb_read(struct file *fp, char __user *buf,
268 size_t count, loff_t *pos)
270 struct adb_dev *dev = fp->private_data;
271 struct usb_request *req;
275 pr_debug("adb_read(%d)\n", count);
279 if (count > ADB_BULK_BUFFER_SIZE)
282 if (adb_lock(&dev->read_excl))
285 /* we will block until we're online */
286 while (!(dev->online || dev->error)) {
287 pr_debug("adb_read: waiting for online state\n");
288 ret = wait_event_interruptible(dev->read_wq,
289 (dev->online || dev->error));
291 adb_unlock(&dev->read_excl);
301 /* queue a request */
305 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
307 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
312 pr_debug("rx %p queue\n", req);
315 /* wait for a request to complete */
316 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
320 usb_ep_dequeue(dev->ep_out, req);
324 /* If we got a 0-len packet, throw it back and try again. */
325 if (req->actual == 0)
328 pr_debug("rx %p %d\n", req, req->actual);
329 xfer = (req->actual < count) ? req->actual : count;
330 if (copy_to_user(buf, req->buf, xfer))
337 adb_unlock(&dev->read_excl);
338 pr_debug("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_request *req = 0;
352 pr_debug("adb_write(%d)\n", count);
354 if (adb_lock(&dev->write_excl))
359 pr_debug("adb_write dev->error\n");
364 /* get an idle tx request to use */
366 ret = wait_event_interruptible(dev->write_wq,
367 (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
375 if (count > ADB_BULK_BUFFER_SIZE)
376 xfer = ADB_BULK_BUFFER_SIZE;
379 if (copy_from_user(req->buf, buf, xfer)) {
385 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
387 pr_debug("adb_write: xfer error %d\n", ret);
396 /* zero this so we don't try to free it on error exit */
402 adb_req_put(dev, &dev->tx_idle, req);
404 adb_unlock(&dev->write_excl);
405 pr_debug("adb_write returning %d\n", r);
409 static int adb_open(struct inode *ip, struct file *fp)
411 pr_info("adb_open\n");
415 if (adb_lock(&_adb_dev->open_excl))
418 fp->private_data = _adb_dev;
420 /* clear the error latch */
423 adb_ready_callback();
428 static int adb_release(struct inode *ip, struct file *fp)
430 pr_info("adb_release\n");
432 adb_closed_callback();
434 adb_unlock(&_adb_dev->open_excl);
438 /* file operations for ADB device /dev/android_adb */
439 static struct file_operations adb_fops = {
440 .owner = THIS_MODULE,
444 .release = adb_release,
447 static struct miscdevice adb_device = {
448 .minor = MISC_DYNAMIC_MINOR,
449 .name = adb_shortname,
457 adb_function_bind(struct usb_configuration *c, struct usb_function *f)
459 struct usb_composite_dev *cdev = c->cdev;
460 struct adb_dev *dev = func_to_adb(f);
465 DBG(cdev, "adb_function_bind dev: %p\n", dev);
467 /* allocate interface ID(s) */
468 id = usb_interface_id(c, f);
471 adb_interface_desc.bInterfaceNumber = id;
473 /* allocate endpoints */
474 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
475 &adb_fullspeed_out_desc);
479 /* support high speed hardware */
480 if (gadget_is_dualspeed(c->cdev->gadget)) {
481 adb_highspeed_in_desc.bEndpointAddress =
482 adb_fullspeed_in_desc.bEndpointAddress;
483 adb_highspeed_out_desc.bEndpointAddress =
484 adb_fullspeed_out_desc.bEndpointAddress;
487 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
488 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
489 f->name, dev->ep_in->name, dev->ep_out->name);
494 adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
496 struct adb_dev *dev = func_to_adb(f);
497 struct usb_request *req;
503 wake_up(&dev->read_wq);
505 adb_request_free(dev->rx_req, dev->ep_out);
506 while ((req = adb_req_get(dev, &dev->tx_idle)))
507 adb_request_free(req, dev->ep_in);
510 static int adb_function_set_alt(struct usb_function *f,
511 unsigned intf, unsigned alt)
513 struct adb_dev *dev = func_to_adb(f);
514 struct usb_composite_dev *cdev = f->config->cdev;
517 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
518 ret = usb_ep_enable(dev->ep_in,
519 ep_choose(cdev->gadget,
520 &adb_highspeed_in_desc,
521 &adb_fullspeed_in_desc));
524 ret = usb_ep_enable(dev->ep_out,
525 ep_choose(cdev->gadget,
526 &adb_highspeed_out_desc,
527 &adb_fullspeed_out_desc));
529 usb_ep_disable(dev->ep_in);
534 /* readers may be blocked waiting for us to go online */
535 wake_up(&dev->read_wq);
539 static void adb_function_disable(struct usb_function *f)
541 struct adb_dev *dev = func_to_adb(f);
542 struct usb_composite_dev *cdev = dev->cdev;
544 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
547 usb_ep_disable(dev->ep_in);
548 dev->ep_in->driver_data = NULL;
549 usb_ep_disable(dev->ep_out);
550 dev->ep_out->driver_data = NULL;
552 /* readers may be blocked waiting for us to go online */
553 wake_up(&dev->read_wq);
555 VDBG(cdev, "%s disabled\n", dev->function.name);
558 static int adb_bind_config(struct usb_configuration *c)
560 struct adb_dev *dev = _adb_dev;
563 dev->function.name = "adb";
564 dev->function.descriptors = fs_adb_descs;
565 dev->function.hs_descriptors = hs_adb_descs;
566 dev->function.bind = adb_function_bind;
567 dev->function.unbind = adb_function_unbind;
568 dev->function.set_alt = adb_function_set_alt;
569 dev->function.disable = adb_function_disable;
571 return usb_add_function(c, &dev->function);
574 static int adb_setup(void)
579 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
583 spin_lock_init(&dev->lock);
585 init_waitqueue_head(&dev->read_wq);
586 init_waitqueue_head(&dev->write_wq);
588 atomic_set(&dev->open_excl, 0);
589 atomic_set(&dev->read_excl, 0);
590 atomic_set(&dev->write_excl, 0);
592 INIT_LIST_HEAD(&dev->tx_idle);
596 ret = misc_register(&adb_device);
604 printk(KERN_ERR "adb gadget driver failed to initialize\n");
608 static void adb_cleanup(void)
610 misc_deregister(&adb_device);