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