blob: 2eaaadaab9255e92dd1e65d58dddb33085e773e5 [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
2 * Gadget Driver for Android ADB
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/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>
28#include <linux/sched.h>
29#include <linux/types.h>
30#include <linux/device.h>
31#include <linux/miscdevice.h>
32
33#include <linux/usb/ch9.h>
34#include <linux/usb/composite.h>
35#include <linux/usb/gadget.h>
36
37#include "f_adb.h"
38
39#define BULK_BUFFER_SIZE 4096
40
41/* number of rx and tx requests to allocate */
42#define RX_REQ_MAX 4
43#define TX_REQ_MAX 4
44
45static const char shortname[] = "android_adb";
46
47struct adb_dev {
48 struct usb_function function;
49 struct usb_composite_dev *cdev;
50 spinlock_t lock;
51
52 struct usb_ep *ep_in;
53 struct usb_ep *ep_out;
54
55 int online;
56 int error;
57
58 atomic_t read_excl;
59 atomic_t write_excl;
60 atomic_t open_excl;
61
62 struct list_head tx_idle;
63 struct list_head rx_idle;
64 struct list_head rx_done;
65
66 wait_queue_head_t read_wq;
67 wait_queue_head_t write_wq;
68
69 /* the request we're currently reading from */
70 struct usb_request *read_req;
71 unsigned char *read_buf;
72 unsigned read_count;
73};
74
75static struct usb_interface_descriptor adb_interface_desc = {
76 .bLength = USB_DT_INTERFACE_SIZE,
77 .bDescriptorType = USB_DT_INTERFACE,
78 .bInterfaceNumber = 0,
79 .bNumEndpoints = 2,
80 .bInterfaceClass = 0xFF,
81 .bInterfaceSubClass = 0x42,
82 .bInterfaceProtocol = 1,
83};
84
85static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
88 .bEndpointAddress = USB_DIR_IN,
89 .bmAttributes = USB_ENDPOINT_XFER_BULK,
90 .wMaxPacketSize = __constant_cpu_to_le16(512),
91};
92
93static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
94 .bLength = USB_DT_ENDPOINT_SIZE,
95 .bDescriptorType = USB_DT_ENDPOINT,
96 .bEndpointAddress = USB_DIR_OUT,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 .wMaxPacketSize = __constant_cpu_to_le16(512),
99};
100
101static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
104 .bEndpointAddress = USB_DIR_IN,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106};
107
108static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
109 .bLength = USB_DT_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_ENDPOINT,
111 .bEndpointAddress = USB_DIR_OUT,
112 .bmAttributes = USB_ENDPOINT_XFER_BULK,
113};
114
115static struct usb_descriptor_header *fs_adb_descs[] = {
116 (struct usb_descriptor_header *) &adb_interface_desc,
117 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
118 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
119 NULL,
120};
121
122static struct usb_descriptor_header *hs_adb_descs[] = {
123 (struct usb_descriptor_header *) &adb_interface_desc,
124 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
125 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
126 NULL,
127};
128
129/* used when adb function is disabled */
130static struct usb_descriptor_header *null_adb_descs[] = {
131 NULL,
132};
133
134
135/* temporary variable used between adb_open() and adb_gadget_bind() */
136static struct adb_dev *_adb_dev;
137
138static inline struct adb_dev *func_to_dev(struct usb_function *f)
139{
140 return container_of(f, struct adb_dev, function);
141}
142
143
144static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
145{
146 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
147 if (!req)
148 return NULL;
149
150 /* now allocate buffers for the requests */
151 req->buf = kmalloc(buffer_size, GFP_KERNEL);
152 if (!req->buf) {
153 usb_ep_free_request(ep, req);
154 return NULL;
155 }
156
157 return req;
158}
159
160static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
161{
162 if (req) {
163 kfree(req->buf);
164 usb_ep_free_request(ep, req);
165 }
166}
167
168static inline int _lock(atomic_t *excl)
169{
170 if (atomic_inc_return(excl) == 1) {
171 return 0;
172 } else {
173 atomic_dec(excl);
174 return -1;
175 }
176}
177
178static inline void _unlock(atomic_t *excl)
179{
180 atomic_dec(excl);
181}
182
183/* add a request to the tail of a list */
184void req_put(struct adb_dev *dev, struct list_head *head,
185 struct usb_request *req)
186{
187 unsigned long flags;
188
189 spin_lock_irqsave(&dev->lock, flags);
190 list_add_tail(&req->list, head);
191 spin_unlock_irqrestore(&dev->lock, flags);
192}
193
194/* remove a request from the head of a list */
195struct usb_request *req_get(struct adb_dev *dev, struct list_head *head)
196{
197 unsigned long flags;
198 struct usb_request *req;
199
200 spin_lock_irqsave(&dev->lock, flags);
201 if (list_empty(head)) {
202 req = 0;
203 } else {
204 req = list_first_entry(head, struct usb_request, list);
205 list_del(&req->list);
206 }
207 spin_unlock_irqrestore(&dev->lock, flags);
208 return req;
209}
210
211static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
212{
213 struct adb_dev *dev = _adb_dev;
214
215 if (req->status != 0)
216 dev->error = 1;
217
218 req_put(dev, &dev->tx_idle, req);
219
220 wake_up(&dev->write_wq);
221}
222
223static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
224{
225 struct adb_dev *dev = _adb_dev;
226
227 if (req->status != 0) {
228 dev->error = 1;
229 req_put(dev, &dev->rx_idle, req);
230 } else {
231 req_put(dev, &dev->rx_done, req);
232 }
233
234 wake_up(&dev->read_wq);
235}
236
237static int __init create_bulk_endpoints(struct adb_dev *dev,
238 struct usb_endpoint_descriptor *in_desc,
239 struct usb_endpoint_descriptor *out_desc)
240{
241 struct usb_composite_dev *cdev = dev->cdev;
242 struct usb_request *req;
243 struct usb_ep *ep;
244 int i;
245
246 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
247
248 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
249 if (!ep) {
250 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
251 return -ENODEV;
252 }
253 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
254 dev->ep_in = ep;
255
256 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
257 if (!ep) {
258 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
259 return -ENODEV;
260 }
261 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
262 dev->ep_out = ep;
263
264 /* now allocate requests for our endpoints */
265 for (i = 0; i < RX_REQ_MAX; i++) {
266 req = adb_request_new(dev->ep_out, BULK_BUFFER_SIZE);
267 if (!req)
268 goto fail;
269 req->complete = adb_complete_out;
270 req_put(dev, &dev->rx_idle, req);
271 }
272
273 for (i = 0; i < TX_REQ_MAX; i++) {
274 req = adb_request_new(dev->ep_in, BULK_BUFFER_SIZE);
275 if (!req)
276 goto fail;
277 req->complete = adb_complete_in;
278 req_put(dev, &dev->tx_idle, req);
279 }
280
281 return 0;
282
283fail:
284 printk(KERN_ERR "adb_bind() could not allocate requests\n");
285 return -1;
286}
287
288static ssize_t adb_read(struct file *fp, char __user *buf,
289 size_t count, loff_t *pos)
290{
291 struct adb_dev *dev = fp->private_data;
292 struct usb_composite_dev *cdev = dev->cdev;
293 struct usb_request *req;
294 int r = count, xfer;
295 int ret;
296
297 DBG(cdev, "adb_read(%d)\n", count);
298
299 if (_lock(&dev->read_excl))
300 return -EBUSY;
301
302 /* we will block until we're online */
303 while (!(dev->online || dev->error)) {
304 DBG(cdev, "adb_read: waiting for online state\n");
305 ret = wait_event_interruptible(dev->read_wq,
306 (dev->online || dev->error));
307 if (ret < 0) {
308 _unlock(&dev->read_excl);
309 return ret;
310 }
311 }
312
313 while (count > 0) {
314 if (dev->error) {
315 DBG(cdev, "adb_read dev->error\n");
316 r = -EIO;
317 break;
318 }
319
320 /* if we have idle read requests, get them queued */
321 while ((req = req_get(dev, &dev->rx_idle))) {
322requeue_req:
323 req->length = BULK_BUFFER_SIZE;
324 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
325
326 if (ret < 0) {
327 r = -EIO;
328 dev->error = 1;
329 req_put(dev, &dev->rx_idle, req);
330 goto fail;
331 } else {
332 DBG(cdev, "rx %p queue\n", req);
333 }
334 }
335
336 /* if we have data pending, give it to userspace */
337 if (dev->read_count > 0) {
338 if (dev->read_count < count)
339 xfer = dev->read_count;
340 else
341 xfer = count;
342
343 if (copy_to_user(buf, dev->read_buf, xfer)) {
344 r = -EFAULT;
345 break;
346 }
347 dev->read_buf += xfer;
348 dev->read_count -= xfer;
349 buf += xfer;
350 count -= xfer;
351
352 /* if we've emptied the buffer, release the request */
353 if (dev->read_count == 0) {
354 req_put(dev, &dev->rx_idle, dev->read_req);
355 dev->read_req = 0;
356 }
357 continue;
358 }
359
360 /* wait for a request to complete */
361 req = 0;
362 ret = wait_event_interruptible(dev->read_wq,
363 ((req = req_get(dev, &dev->rx_done)) || dev->error));
364 if (req != 0) {
365 /* if we got a 0-len one we need to put it back into
366 ** service. if we made it the current read req we'd
367 ** be stuck forever
368 */
369 if (req->actual == 0)
370 goto requeue_req;
371
372 dev->read_req = req;
373 dev->read_count = req->actual;
374 dev->read_buf = req->buf;
375 DBG(cdev, "rx %p %d\n", req, req->actual);
376 }
377
378 if (ret < 0) {
379 r = ret;
380 break;
381 }
382 }
383
384fail:
385 _unlock(&dev->read_excl);
386 DBG(cdev, "adb_read returning %d\n", r);
387 return r;
388}
389
390static ssize_t adb_write(struct file *fp, const char __user *buf,
391 size_t count, loff_t *pos)
392{
393 struct adb_dev *dev = fp->private_data;
394 struct usb_composite_dev *cdev = dev->cdev;
395 struct usb_request *req = 0;
396 int r = count, xfer;
397 int ret;
398
399 DBG(cdev, "adb_write(%d)\n", count);
400
401 if (_lock(&dev->write_excl))
402 return -EBUSY;
403
404 while (count > 0) {
405 if (dev->error) {
406 DBG(cdev, "adb_write dev->error\n");
407 r = -EIO;
408 break;
409 }
410
411 /* get an idle tx request to use */
412 req = 0;
413 ret = wait_event_interruptible(dev->write_wq,
414 ((req = req_get(dev, &dev->tx_idle)) || dev->error));
415
416 if (ret < 0) {
417 r = ret;
418 break;
419 }
420
421 if (req != 0) {
422 if (count > BULK_BUFFER_SIZE)
423 xfer = BULK_BUFFER_SIZE;
424 else
425 xfer = count;
426 if (copy_from_user(req->buf, buf, xfer)) {
427 r = -EFAULT;
428 break;
429 }
430
431 req->length = xfer;
432 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
433 if (ret < 0) {
434 DBG(cdev, "adb_write: xfer error %d\n", ret);
435 dev->error = 1;
436 r = -EIO;
437 break;
438 }
439
440 buf += xfer;
441 count -= xfer;
442
443 /* zero this so we don't try to free it on error exit */
444 req = 0;
445 }
446 }
447
448 if (req)
449 req_put(dev, &dev->tx_idle, req);
450
451 _unlock(&dev->write_excl);
452 DBG(cdev, "adb_write returning %d\n", r);
453 return r;
454}
455
456static int adb_open(struct inode *ip, struct file *fp)
457{
458 printk(KERN_INFO "adb_open\n");
459 if (_lock(&_adb_dev->open_excl))
460 return -EBUSY;
461
462 fp->private_data = _adb_dev;
463
464 /* clear the error latch */
465 _adb_dev->error = 0;
466
467 return 0;
468}
469
470static int adb_release(struct inode *ip, struct file *fp)
471{
472 printk(KERN_INFO "adb_release\n");
473 _unlock(&_adb_dev->open_excl);
474 return 0;
475}
476
477/* file operations for ADB device /dev/android_adb */
478static struct file_operations adb_fops = {
479 .owner = THIS_MODULE,
480 .read = adb_read,
481 .write = adb_write,
482 .open = adb_open,
483 .release = adb_release,
484};
485
486static struct miscdevice adb_device = {
487 .minor = MISC_DYNAMIC_MINOR,
488 .name = shortname,
489 .fops = &adb_fops,
490};
491
492static int __init
493adb_function_bind(struct usb_configuration *c, struct usb_function *f)
494{
495 struct usb_composite_dev *cdev = c->cdev;
496 struct adb_dev *dev = func_to_dev(f);
497 int id;
498 int ret;
499
500 dev->cdev = cdev;
501 DBG(cdev, "adb_function_bind dev: %p\n", dev);
502
503 /* allocate interface ID(s) */
504 id = usb_interface_id(c, f);
505 if (id < 0)
506 return id;
507 adb_interface_desc.bInterfaceNumber = id;
508
509 /* allocate endpoints */
510 ret = create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
511 &adb_fullspeed_out_desc);
512 if (ret)
513 return ret;
514
515 /* support high speed hardware */
516 if (gadget_is_dualspeed(c->cdev->gadget)) {
517 adb_highspeed_in_desc.bEndpointAddress =
518 adb_fullspeed_in_desc.bEndpointAddress;
519 adb_highspeed_out_desc.bEndpointAddress =
520 adb_fullspeed_out_desc.bEndpointAddress;
521 }
522
523 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
524 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
525 f->name, dev->ep_in->name, dev->ep_out->name);
526 return 0;
527}
528
529static void
530adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
531{
532 struct adb_dev *dev = func_to_dev(f);
533 struct usb_request *req;
534
535 spin_lock_irq(&dev->lock);
536
537 while ((req = req_get(dev, &dev->rx_idle)))
538 adb_request_free(req, dev->ep_out);
539 while ((req = req_get(dev, &dev->tx_idle)))
540 adb_request_free(req, dev->ep_in);
541
542 dev->online = 0;
543 dev->error = 1;
544 spin_unlock_irq(&dev->lock);
545
546 misc_deregister(&adb_device);
547 kfree(_adb_dev);
548 _adb_dev = NULL;
549}
550
551static int adb_function_set_alt(struct usb_function *f,
552 unsigned intf, unsigned alt)
553{
554 struct adb_dev *dev = func_to_dev(f);
555 struct usb_composite_dev *cdev = f->config->cdev;
556 int ret;
557
558 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
559 ret = usb_ep_enable(dev->ep_in,
560 ep_choose(cdev->gadget,
561 &adb_highspeed_in_desc,
562 &adb_fullspeed_in_desc));
563 if (ret)
564 return ret;
565 ret = usb_ep_enable(dev->ep_out,
566 ep_choose(cdev->gadget,
567 &adb_highspeed_out_desc,
568 &adb_fullspeed_out_desc));
569 if (ret) {
570 usb_ep_disable(dev->ep_in);
571 return ret;
572 }
573 dev->online = 1;
574
575 /* readers may be blocked waiting for us to go online */
576 wake_up(&dev->read_wq);
577 return 0;
578}
579
580static void adb_function_disable(struct usb_function *f)
581{
582 struct adb_dev *dev = func_to_dev(f);
583 struct usb_composite_dev *cdev = dev->cdev;
584
585 DBG(cdev, "adb_function_disable\n");
586 dev->online = 0;
587 dev->error = 1;
588 usb_ep_disable(dev->ep_in);
589 usb_ep_disable(dev->ep_out);
590
591 /* readers may be blocked waiting for us to go online */
592 wake_up(&dev->read_wq);
593
594 VDBG(cdev, "%s disabled\n", dev->function.name);
595}
596
597int __init adb_function_add(struct usb_composite_dev *cdev,
598 struct usb_configuration *c)
599{
600 struct adb_dev *dev;
601 int ret;
602
603 printk(KERN_INFO "adb_function_add\n");
604
605 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
606 if (!dev)
607 return -ENOMEM;
608
609 spin_lock_init(&dev->lock);
610
611 init_waitqueue_head(&dev->read_wq);
612 init_waitqueue_head(&dev->write_wq);
613
614 atomic_set(&dev->open_excl, 0);
615 atomic_set(&dev->read_excl, 0);
616 atomic_set(&dev->write_excl, 0);
617
618 INIT_LIST_HEAD(&dev->rx_idle);
619 INIT_LIST_HEAD(&dev->rx_done);
620 INIT_LIST_HEAD(&dev->tx_idle);
621
622 dev->cdev = cdev;
623 dev->function.name = "adb";
624 dev->function.descriptors = null_adb_descs;
625 dev->function.hs_descriptors = null_adb_descs;
626 dev->function.bind = adb_function_bind;
627 dev->function.unbind = adb_function_unbind;
628 dev->function.set_alt = adb_function_set_alt;
629 dev->function.disable = adb_function_disable;
630
631 /* _adb_dev must be set before calling usb_gadget_register_driver */
632 _adb_dev = dev;
633
634 ret = misc_register(&adb_device);
635 if (ret)
636 goto err1;
637 ret = usb_add_function(c, &dev->function);
638 if (ret)
639 goto err2;
640
641 return 0;
642
643err2:
644 misc_deregister(&adb_device);
645err1:
646 kfree(dev);
647 printk(KERN_ERR "adb gadget driver failed to initialize\n");
648 return ret;
649}
650
651void adb_function_enable(int enable)
652{
653 struct adb_dev *dev = _adb_dev;
654
655 if (dev) {
656 DBG(dev->cdev, "adb_function_enable(%s)\n",
657 enable ? "true" : "false");
658
659 if (enable) {
660 dev->function.descriptors = fs_adb_descs;
661 dev->function.hs_descriptors = hs_adb_descs;
662 } else {
663 dev->function.descriptors = null_adb_descs;
664 dev->function.hs_descriptors = null_adb_descs;
665 }
666 }
667}
668