blob: dd91be387b45f3caab08691255397d44e6873e07 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* drivers/usb/function/adb.c
2 *
3 * Function Device for the Android ADB Protocol
4 *
5 * Copyright (C) 2007 Google, Inc.
6 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
7 * Author: Brian Swetland <swetland@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/miscdevice.h>
24#include <linux/fs.h>
25
26#include <linux/sched.h>
27#include <linux/wait.h>
28#include <linux/list.h>
29
30#include <asm/atomic.h>
31#include <asm/uaccess.h>
32
33#include "usb_function.h"
34
35#if 1
36#define DBG(x...) do {} while (0)
37#else
38#define DBG(x...) printk(x)
39#endif
40
41#define TXN_MAX 4096
42
43/* number of rx and tx requests to allocate */
44#define RX_REQ_MAX 4
45#define TX_REQ_MAX 4
46
47#define ADB_FUNCTION_NAME "adb"
48
49struct adb_context
50{
51 int online;
52 int error;
53
54 atomic_t read_excl;
55 atomic_t write_excl;
56 atomic_t open_excl;
57 atomic_t enable_excl;
58 spinlock_t lock;
59
60 struct usb_endpoint *out;
61 struct usb_endpoint *in;
62
63 struct list_head tx_idle;
64 struct list_head rx_idle;
65 struct list_head rx_done;
66
67 wait_queue_head_t read_wq;
68 wait_queue_head_t write_wq;
69
70 /* the request we're currently reading from */
71 struct usb_request *read_req;
72 unsigned char *read_buf;
73 unsigned read_count;
74 unsigned bound;
75};
76
77static struct adb_context _context;
78
79static struct usb_interface_descriptor intf_desc = {
80 .bLength = sizeof intf_desc,
81 .bDescriptorType = USB_DT_INTERFACE,
82 .bNumEndpoints = 2,
83 .bInterfaceClass = 0xff,
84 .bInterfaceSubClass = 0x42,
85 .bInterfaceProtocol = 0x01,
86};
87
88static struct usb_endpoint_descriptor hs_bulk_in_desc = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bEndpointAddress = USB_DIR_IN,
92 .bmAttributes = USB_ENDPOINT_XFER_BULK,
93 .wMaxPacketSize = __constant_cpu_to_le16(512),
94 .bInterval = 0,
95};
96static struct usb_endpoint_descriptor fs_bulk_in_desc = {
97 .bLength = USB_DT_ENDPOINT_SIZE,
98 .bDescriptorType = USB_DT_ENDPOINT,
99 .bEndpointAddress = USB_DIR_IN,
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = __constant_cpu_to_le16(64),
102 .bInterval = 0,
103};
104
105static struct usb_endpoint_descriptor hs_bulk_out_desc = {
106 .bLength = USB_DT_ENDPOINT_SIZE,
107 .bDescriptorType = USB_DT_ENDPOINT,
108 .bEndpointAddress = USB_DIR_OUT,
109 .bmAttributes = USB_ENDPOINT_XFER_BULK,
110 .wMaxPacketSize = __constant_cpu_to_le16(512),
111 .bInterval = 0,
112};
113
114static struct usb_endpoint_descriptor fs_bulk_out_desc = {
115 .bLength = USB_DT_ENDPOINT_SIZE,
116 .bDescriptorType = USB_DT_ENDPOINT,
117 .bEndpointAddress = USB_DIR_OUT,
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
119 .wMaxPacketSize = __constant_cpu_to_le16(64),
120 .bInterval = 0,
121};
122
123static struct usb_function usb_func_adb;
124
125static inline int _lock(atomic_t *excl)
126{
127 if (atomic_inc_return(excl) == 1) {
128 return 0;
129 } else {
130 atomic_dec(excl);
131 return -1;
132 }
133}
134
135static inline void _unlock(atomic_t *excl)
136{
137 atomic_dec(excl);
138}
139
140/* add a request to the tail of a list */
141void req_put(struct adb_context *ctxt, struct list_head *head, struct usb_request *req)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&ctxt->lock, flags);
146 list_add_tail(&req->list, head);
147 spin_unlock_irqrestore(&ctxt->lock, flags);
148}
149
150/* remove a request from the head of a list */
151struct usb_request *req_get(struct adb_context *ctxt, struct list_head *head)
152{
153 unsigned long flags;
154 struct usb_request *req;
155
156 spin_lock_irqsave(&ctxt->lock, flags);
157 if (list_empty(head)) {
158 req = 0;
159 } else {
160 req = list_first_entry(head, struct usb_request, list);
161 list_del(&req->list);
162 }
163 spin_unlock_irqrestore(&ctxt->lock, flags);
164 return req;
165}
166
167static void adb_complete_in(struct usb_endpoint *ept, struct usb_request *req)
168{
169 struct adb_context *ctxt = req->context;
170
171 if (req->status != 0)
172 ctxt->error = 1;
173
174 req_put(ctxt, &ctxt->tx_idle, req);
175
176 wake_up(&ctxt->write_wq);
177}
178
179static void adb_complete_out(struct usb_endpoint *ept, struct usb_request *req)
180{
181 struct adb_context *ctxt = req->context;
182
183 if (req->status != 0) {
184 ctxt->error = 1;
185 req_put(ctxt, &ctxt->rx_idle, req);
186 } else {
187 req_put(ctxt, &ctxt->rx_done, req);
188 }
189
190 wake_up(&ctxt->read_wq);
191}
192
193static ssize_t adb_read(struct file *fp, char __user *buf,
194 size_t count, loff_t *pos)
195{
196 struct adb_context *ctxt = &_context;
197 struct usb_request *req;
198 int r = count, xfer;
199 int ret;
200
201 DBG("adb_read(%d)\n", count);
202
203 if (_lock(&ctxt->read_excl))
204 return -EBUSY;
205
206 /* we will block until we're online */
207 while (!(ctxt->online || ctxt->error)) {
208 DBG("adb_read: waiting for online state\n");
209 ret = wait_event_interruptible(ctxt->read_wq, (ctxt->online || ctxt->error));
210 if (ret < 0) {
211 _unlock(&ctxt->read_excl);
212 return ret;
213 }
214 }
215
216 while (count > 0) {
217 if (ctxt->error) {
218 r = -EIO;
219 break;
220 }
221
222 /* if we have idle read requests, get them queued */
223 while ((req = req_get(ctxt, &ctxt->rx_idle))) {
224requeue_req:
225 req->length = TXN_MAX;
226 ret = usb_ept_queue_xfer(ctxt->out, req);
227 if (ret < 0) {
228 DBG("adb_read: failed to queue req %p (%d)\n", req, ret);
229 r = -EIO;
230 ctxt->error = 1;
231 req_put(ctxt, &ctxt->rx_idle, req);
232 goto fail;
233 } else {
234 DBG("rx %p queue\n", req);
235 }
236 }
237
238 /* if we have data pending, give it to userspace */
239 if (ctxt->read_count > 0) {
240 xfer = (ctxt->read_count < count) ? ctxt->read_count : count;
241
242 if (copy_to_user(buf, ctxt->read_buf, xfer)) {
243 r = -EFAULT;
244 break;
245 }
246 ctxt->read_buf += xfer;
247 ctxt->read_count -= xfer;
248 buf += xfer;
249 count -= xfer;
250
251 /* if we've emptied the buffer, release the request */
252 if (ctxt->read_count == 0) {
253 req_put(ctxt, &ctxt->rx_idle, ctxt->read_req);
254 ctxt->read_req = 0;
255 }
256 continue;
257 }
258
259 /* wait for a request to complete */
260 req = 0;
261 ret = wait_event_interruptible(ctxt->read_wq,
262 ((req = req_get(ctxt, &ctxt->rx_done)) || ctxt->error));
263
264 if (req != 0) {
265 /* if we got a 0-len one we need to put it back into
266 ** service. if we made it the current read req we'd
267 ** be stuck forever
268 */
269 if (req->actual == 0)
270 goto requeue_req;
271
272 ctxt->read_req = req;
273 ctxt->read_count = req->actual;
274 ctxt->read_buf = req->buf;
275 DBG("rx %p %d\n", req, req->actual);
276 }
277
278 if (ret < 0) {
279 r = ret;
280 break;
281 }
282 }
283
284fail:
285 _unlock(&ctxt->read_excl);
286 return r;
287}
288
289static ssize_t adb_write(struct file *fp, const char __user *buf,
290 size_t count, loff_t *pos)
291{
292 struct adb_context *ctxt = &_context;
293 struct usb_request *req = 0;
294 int r = count, xfer;
295 int ret;
296
297 DBG("adb_write(%d)\n", count);
298
299 if (_lock(&ctxt->write_excl))
300 return -EBUSY;
301
302 while (count > 0) {
303 if (ctxt->error) {
304 r = -EIO;
305 break;
306 }
307
308 /* get an idle tx request to use */
309 req = 0;
310 ret = wait_event_interruptible(ctxt->write_wq,
311 ((req = req_get(ctxt, &ctxt->tx_idle)) || ctxt->error));
312
313 if (ret < 0) {
314 r = ret;
315 break;
316 }
317
318 if (req != 0) {
319 xfer = count > TXN_MAX ? TXN_MAX : count;
320 if (copy_from_user(req->buf, buf, xfer)) {
321 r = -EFAULT;
322 break;
323 }
324
325 req->length = xfer;
326 ret = usb_ept_queue_xfer(ctxt->in, req);
327 if (ret < 0) {
328 DBG("adb_write: xfer error %d\n", ret);
329 ctxt->error = 1;
330 r = -EIO;
331 break;
332 }
333
334 buf += xfer;
335 count -= xfer;
336
337 /* zero this so we don't try to free it on error exit */
338 req = 0;
339 }
340 }
341
342
343 if (req)
344 req_put(ctxt, &ctxt->tx_idle, req);
345
346 _unlock(&ctxt->write_excl);
347 return r;
348}
349
350static int adb_open(struct inode *ip, struct file *fp)
351{
352 struct adb_context *ctxt = &_context;
353
354 if (_lock(&ctxt->open_excl))
355 return -EBUSY;
356
357 /* clear the error latch */
358 ctxt->error = 0;
359
360 return 0;
361}
362
363static int adb_release(struct inode *ip, struct file *fp)
364{
365 struct adb_context *ctxt = &_context;
366
367 _unlock(&ctxt->open_excl);
368 return 0;
369}
370
371static struct file_operations adb_fops = {
372 .owner = THIS_MODULE,
373 .read = adb_read,
374 .write = adb_write,
375 .open = adb_open,
376 .release = adb_release,
377};
378
379static struct miscdevice adb_device = {
380 .minor = MISC_DYNAMIC_MINOR,
381 .name = "android_adb",
382 .fops = &adb_fops,
383};
384
385static int adb_enable_open(struct inode *ip, struct file *fp)
386{
387 struct adb_context *ctxt = &_context;
388
389 if (_lock(&ctxt->enable_excl))
390 return -EBUSY;
391
392 printk(KERN_INFO "enabling adb function\n");
393 usb_function_enable(ADB_FUNCTION_NAME, 1);
394 /* clear the error latch */
395 ctxt->error = 0;
396
397 return 0;
398}
399
400static int adb_enable_release(struct inode *ip, struct file *fp)
401{
402 struct adb_context *ctxt = &_context;
403
404 printk(KERN_INFO "disabling adb function\n");
405 usb_function_enable(ADB_FUNCTION_NAME, 0);
406 _unlock(&ctxt->enable_excl);
407 return 0;
408}
409
410static struct file_operations adb_enable_fops = {
411 .owner = THIS_MODULE,
412 .open = adb_enable_open,
413 .release = adb_enable_release,
414};
415
416static struct miscdevice adb_enable_device = {
417 .minor = MISC_DYNAMIC_MINOR,
418 .name = "android_adb_enable",
419 .fops = &adb_enable_fops,
420};
421
422static void adb_unbind(void *_ctxt)
423{
424 struct adb_context *ctxt = _ctxt;
425 struct usb_request *req;
426
427 if (!ctxt->bound)
428 return;
429
430 while ((req = req_get(ctxt, &ctxt->rx_idle))) {
431 usb_ept_free_req(ctxt->out, req);
432 }
433 while ((req = req_get(ctxt, &ctxt->tx_idle))) {
434 usb_ept_free_req(ctxt->in, req);
435 }
436 if (ctxt->in) {
437 usb_ept_fifo_flush(ctxt->in);
438 usb_ept_enable(ctxt->in, 0);
439 usb_free_endpoint(ctxt->in);
440 }
441 if (ctxt->out) {
442 usb_ept_fifo_flush(ctxt->out);
443 usb_ept_enable(ctxt->out, 0);
444 usb_free_endpoint(ctxt->out);
445 }
446
447 ctxt->online = 0;
448 ctxt->error = 1;
449
450 /* readers may be blocked waiting for us to go online */
451 wake_up(&ctxt->read_wq);
452 ctxt->bound = 0;
453}
454
455static void adb_bind(void *_ctxt)
456{
457 struct adb_context *ctxt = _ctxt;
458 struct usb_request *req;
459 int n;
460
461 intf_desc.bInterfaceNumber =
462 usb_msm_get_next_ifc_number(&usb_func_adb);
463
464 ctxt->in = usb_alloc_endpoint(USB_DIR_IN);
465 if (ctxt->in) {
466 hs_bulk_in_desc.bEndpointAddress = USB_DIR_IN | ctxt->in->num;
467 fs_bulk_in_desc.bEndpointAddress = USB_DIR_IN | ctxt->in->num;
468 }
469
470 ctxt->out = usb_alloc_endpoint(USB_DIR_OUT);
471 if (ctxt->out) {
472 hs_bulk_out_desc.bEndpointAddress = USB_DIR_OUT|ctxt->out->num;
473 fs_bulk_out_desc.bEndpointAddress = USB_DIR_OUT|ctxt->out->num;
474 }
475
476 for (n = 0; n < RX_REQ_MAX; n++) {
477 req = usb_ept_alloc_req(ctxt->out, 4096);
478 if (req == 0) {
479 ctxt->bound = 1;
480 goto fail;
481 }
482 req->context = ctxt;
483 req->complete = adb_complete_out;
484 req_put(ctxt, &ctxt->rx_idle, req);
485 }
486
487 for (n = 0; n < TX_REQ_MAX; n++) {
488 req = usb_ept_alloc_req(ctxt->in, 4096);
489 if (req == 0) {
490 ctxt->bound = 1;
491 goto fail;
492 }
493 req->context = ctxt;
494 req->complete = adb_complete_in;
495 req_put(ctxt, &ctxt->tx_idle, req);
496 }
497 ctxt->bound = 1;
498 return;
499
500fail:
501 printk(KERN_ERR "adb_bind() could not allocate requests\n");
502 adb_unbind(ctxt);
503}
504
505static void adb_configure(int configured, void *_ctxt)
506{
507 struct adb_context *ctxt = _ctxt;
508 struct usb_request *req;
509
510 if (configured) {
511 ctxt->online = 1;
512
513 if (usb_msm_get_speed() == USB_SPEED_HIGH) {
514 usb_configure_endpoint(ctxt->in, &hs_bulk_in_desc);
515 usb_configure_endpoint(ctxt->out, &hs_bulk_out_desc);
516 } else {
517 usb_configure_endpoint(ctxt->in, &fs_bulk_in_desc);
518 usb_configure_endpoint(ctxt->out, &fs_bulk_out_desc);
519 }
520 usb_ept_enable(ctxt->in, 1);
521 usb_ept_enable(ctxt->out, 1);
522
523 /* if we have a stale request being read, recycle it */
524 ctxt->read_buf = 0;
525 ctxt->read_count = 0;
526 if (ctxt->read_req) {
527 req_put(ctxt, &ctxt->rx_idle, ctxt->read_req);
528 ctxt->read_req = 0;
529 }
530
531 /* retire any completed rx requests from previous session */
532 while ((req = req_get(ctxt, &ctxt->rx_done)))
533 req_put(ctxt, &ctxt->rx_idle, req);
534
535 } else {
536 ctxt->online = 0;
537 ctxt->error = 1;
538 }
539
540 /* readers may be blocked waiting for us to go online */
541 wake_up(&ctxt->read_wq);
542}
543
544static struct usb_function usb_func_adb = {
545 .bind = adb_bind,
546 .unbind = adb_unbind,
547 .configure = adb_configure,
548
549 .name = ADB_FUNCTION_NAME,
550 .context = &_context,
551
552};
553
554struct usb_descriptor_header *adb_hs_descriptors[4];
555struct usb_descriptor_header *adb_fs_descriptors[4];
556static int __init adb_init(void)
557{
558 int ret = 0;
559 struct adb_context *ctxt = &_context;
560 DBG("adb_init()\n");
561
562 init_waitqueue_head(&ctxt->read_wq);
563 init_waitqueue_head(&ctxt->write_wq);
564
565 atomic_set(&ctxt->open_excl, 0);
566 atomic_set(&ctxt->read_excl, 0);
567 atomic_set(&ctxt->write_excl, 0);
568 atomic_set(&ctxt->enable_excl, 0);
569
570 spin_lock_init(&ctxt->lock);
571
572 INIT_LIST_HEAD(&ctxt->rx_idle);
573 INIT_LIST_HEAD(&ctxt->rx_done);
574 INIT_LIST_HEAD(&ctxt->tx_idle);
575
576 adb_hs_descriptors[0] = (struct usb_descriptor_header *)&intf_desc;
577 adb_hs_descriptors[1] =
578 (struct usb_descriptor_header *)&hs_bulk_in_desc;
579 adb_hs_descriptors[2] =
580 (struct usb_descriptor_header *)&hs_bulk_out_desc;
581 adb_hs_descriptors[3] = NULL;
582
583 adb_fs_descriptors[0] = (struct usb_descriptor_header *)&intf_desc;
584 adb_fs_descriptors[1] =
585 (struct usb_descriptor_header *)&fs_bulk_in_desc;
586 adb_fs_descriptors[2] =
587 (struct usb_descriptor_header *)&fs_bulk_out_desc;
588 adb_fs_descriptors[3] = NULL;
589
590 usb_func_adb.hs_descriptors = adb_hs_descriptors;
591 usb_func_adb.fs_descriptors = adb_fs_descriptors;
592
593 ret = misc_register(&adb_device);
594 if (ret) {
595 printk(KERN_ERR "adb Can't register misc device %d \n",
596 MISC_DYNAMIC_MINOR);
597 return ret;
598 }
599 ret = misc_register(&adb_enable_device);
600 if (ret) {
601 printk(KERN_ERR "adb Can't register misc enable device %d \n",
602 MISC_DYNAMIC_MINOR);
603 misc_deregister(&adb_device);
604 return ret;
605 }
606
607 ret = usb_function_register(&usb_func_adb);
608 if (ret) {
609 misc_deregister(&adb_device);
610 misc_deregister(&adb_enable_device);
611 }
612 return ret;
613}
614
615module_init(adb_init);
616
617static void __exit adb_exit(void)
618{
619 misc_deregister(&adb_device);
620 misc_deregister(&adb_enable_device);
621
622 usb_function_unregister(&usb_func_adb);
623}
624module_exit(adb_exit);