blob: 4433a4dbdfb100105daae746e8bee18103a26dc4 [file] [log] [blame]
Benoit Goby2b6862d2011-12-19 14:38:41 -08001/*
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 if (atomic_inc_return(excl) == 1) {
151 return 0;
152 } else {
153 atomic_dec(excl);
154 return -1;
155 }
156}
157
158static inline void adb_unlock(atomic_t *excl)
159{
160 atomic_dec(excl);
161}
162
163/* add a request to the tail of a list */
164void adb_req_put(struct adb_dev *dev, struct list_head *head,
165 struct usb_request *req)
166{
167 unsigned long flags;
168
169 spin_lock_irqsave(&dev->lock, flags);
170 list_add_tail(&req->list, head);
171 spin_unlock_irqrestore(&dev->lock, flags);
172}
173
174/* remove a request from the head of a list */
175struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
176{
177 unsigned long flags;
178 struct usb_request *req;
179
180 spin_lock_irqsave(&dev->lock, flags);
181 if (list_empty(head)) {
182 req = 0;
183 } else {
184 req = list_first_entry(head, struct usb_request, list);
185 list_del(&req->list);
186 }
187 spin_unlock_irqrestore(&dev->lock, flags);
188 return req;
189}
190
191static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
192{
193 struct adb_dev *dev = _adb_dev;
194
195 if (req->status != 0)
196 dev->error = 1;
197
198 adb_req_put(dev, &dev->tx_idle, req);
199
200 wake_up(&dev->write_wq);
201}
202
203static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
204{
205 struct adb_dev *dev = _adb_dev;
206
207 dev->rx_done = 1;
208 if (req->status != 0)
209 dev->error = 1;
210
211 wake_up(&dev->read_wq);
212}
213
214static int adb_create_bulk_endpoints(struct adb_dev *dev,
215 struct usb_endpoint_descriptor *in_desc,
216 struct usb_endpoint_descriptor *out_desc)
217{
218 struct usb_composite_dev *cdev = dev->cdev;
219 struct usb_request *req;
220 struct usb_ep *ep;
221 int i;
222
223 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
224
225 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
226 if (!ep) {
227 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
228 return -ENODEV;
229 }
230 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
231 ep->driver_data = dev; /* claim the endpoint */
232 dev->ep_in = ep;
233
234 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
235 if (!ep) {
236 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
237 return -ENODEV;
238 }
239 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
240 ep->driver_data = dev; /* claim the endpoint */
241 dev->ep_out = ep;
242
243 /* now allocate requests for our endpoints */
244 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
245 if (!req)
246 goto fail;
247 req->complete = adb_complete_out;
248 dev->rx_req = req;
249
250 for (i = 0; i < TX_REQ_MAX; i++) {
251 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
252 if (!req)
253 goto fail;
254 req->complete = adb_complete_in;
255 adb_req_put(dev, &dev->tx_idle, req);
256 }
257
258 return 0;
259
260fail:
261 printk(KERN_ERR "adb_bind() could not allocate requests\n");
262 return -1;
263}
264
265static ssize_t adb_read(struct file *fp, char __user *buf,
266 size_t count, loff_t *pos)
267{
268 struct adb_dev *dev = fp->private_data;
269 struct usb_request *req;
270 int r = count, xfer;
271 int ret;
272
273 pr_debug("adb_read(%d)\n", count);
274 if (!_adb_dev)
275 return -ENODEV;
276
277 if (count > ADB_BULK_BUFFER_SIZE)
278 return -EINVAL;
279
280 if (adb_lock(&dev->read_excl))
281 return -EBUSY;
282
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));
288 if (ret < 0) {
289 adb_unlock(&dev->read_excl);
290 return ret;
291 }
292 }
293 if (dev->error) {
294 r = -EIO;
295 goto done;
296 }
297
298requeue_req:
299 /* queue a request */
300 req = dev->rx_req;
301 req->length = count;
302 dev->rx_done = 0;
303 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
304 if (ret < 0) {
305 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
306 r = -EIO;
307 dev->error = 1;
308 goto done;
309 } else {
310 pr_debug("rx %p queue\n", req);
311 }
312
313 /* wait for a request to complete */
314 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
315 if (ret < 0) {
Colin Cross42582f22012-03-05 13:29:45 -0800316 if (ret != -ERESTARTSYS)
317 dev->error = 1;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800318 r = ret;
319 usb_ep_dequeue(dev->ep_out, req);
320 goto done;
321 }
322 if (!dev->error) {
323 /* If we got a 0-len packet, throw it back and try again. */
324 if (req->actual == 0)
325 goto requeue_req;
326
327 pr_debug("rx %p %d\n", req, req->actual);
328 xfer = (req->actual < count) ? req->actual : count;
329 if (copy_to_user(buf, req->buf, xfer))
330 r = -EFAULT;
331
332 } else
333 r = -EIO;
334
335done:
336 adb_unlock(&dev->read_excl);
337 pr_debug("adb_read returning %d\n", r);
338 return r;
339}
340
341static ssize_t adb_write(struct file *fp, const char __user *buf,
342 size_t count, loff_t *pos)
343{
344 struct adb_dev *dev = fp->private_data;
345 struct usb_request *req = 0;
346 int r = count, xfer;
347 int ret;
348
349 if (!_adb_dev)
350 return -ENODEV;
351 pr_debug("adb_write(%d)\n", count);
352
353 if (adb_lock(&dev->write_excl))
354 return -EBUSY;
355
356 while (count > 0) {
357 if (dev->error) {
358 pr_debug("adb_write dev->error\n");
359 r = -EIO;
360 break;
361 }
362
363 /* get an idle tx request to use */
364 req = 0;
365 ret = wait_event_interruptible(dev->write_wq,
366 (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
367
368 if (ret < 0) {
369 r = ret;
370 break;
371 }
372
373 if (req != 0) {
374 if (count > ADB_BULK_BUFFER_SIZE)
375 xfer = ADB_BULK_BUFFER_SIZE;
376 else
377 xfer = count;
378 if (copy_from_user(req->buf, buf, xfer)) {
379 r = -EFAULT;
380 break;
381 }
382
383 req->length = xfer;
384 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
385 if (ret < 0) {
386 pr_debug("adb_write: xfer error %d\n", ret);
387 dev->error = 1;
388 r = -EIO;
389 break;
390 }
391
392 buf += xfer;
393 count -= xfer;
394
395 /* zero this so we don't try to free it on error exit */
396 req = 0;
397 }
398 }
399
400 if (req)
401 adb_req_put(dev, &dev->tx_idle, req);
402
403 adb_unlock(&dev->write_excl);
404 pr_debug("adb_write returning %d\n", r);
405 return r;
406}
407
408static int adb_open(struct inode *ip, struct file *fp)
409{
410 printk(KERN_INFO "adb_open\n");
411 if (!_adb_dev)
412 return -ENODEV;
413
414 if (adb_lock(&_adb_dev->open_excl))
415 return -EBUSY;
416
417 fp->private_data = _adb_dev;
418
419 /* clear the error latch */
420 _adb_dev->error = 0;
421
422 return 0;
423}
424
425static int adb_release(struct inode *ip, struct file *fp)
426{
427 printk(KERN_INFO "adb_release\n");
428 adb_unlock(&_adb_dev->open_excl);
429 return 0;
430}
431
432/* file operations for ADB device /dev/android_adb */
433static const struct file_operations adb_fops = {
434 .owner = THIS_MODULE,
435 .read = adb_read,
436 .write = adb_write,
437 .open = adb_open,
438 .release = adb_release,
439};
440
441static struct miscdevice adb_device = {
442 .minor = MISC_DYNAMIC_MINOR,
443 .name = adb_shortname,
444 .fops = &adb_fops,
445};
446
447
448
449
450static int
451adb_function_bind(struct usb_configuration *c, struct usb_function *f)
452{
453 struct usb_composite_dev *cdev = c->cdev;
454 struct adb_dev *dev = func_to_adb(f);
455 int id;
456 int ret;
457
458 dev->cdev = cdev;
459 DBG(cdev, "adb_function_bind dev: %p\n", dev);
460
461 /* allocate interface ID(s) */
462 id = usb_interface_id(c, f);
463 if (id < 0)
464 return id;
465 adb_interface_desc.bInterfaceNumber = id;
466
467 /* allocate endpoints */
468 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
469 &adb_fullspeed_out_desc);
470 if (ret)
471 return ret;
472
473 /* support high speed hardware */
474 if (gadget_is_dualspeed(c->cdev->gadget)) {
475 adb_highspeed_in_desc.bEndpointAddress =
476 adb_fullspeed_in_desc.bEndpointAddress;
477 adb_highspeed_out_desc.bEndpointAddress =
478 adb_fullspeed_out_desc.bEndpointAddress;
479 }
480
481 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
482 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
483 f->name, dev->ep_in->name, dev->ep_out->name);
484 return 0;
485}
486
487static void
488adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
489{
490 struct adb_dev *dev = func_to_adb(f);
491 struct usb_request *req;
492
493
494 dev->online = 0;
495 dev->error = 1;
496
497 wake_up(&dev->read_wq);
498
499 adb_request_free(dev->rx_req, dev->ep_out);
500 while ((req = adb_req_get(dev, &dev->tx_idle)))
501 adb_request_free(req, dev->ep_in);
502}
503
504static int adb_function_set_alt(struct usb_function *f,
505 unsigned intf, unsigned alt)
506{
507 struct adb_dev *dev = func_to_adb(f);
508 struct usb_composite_dev *cdev = f->config->cdev;
509 int ret;
510
511 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
512
513 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
514 if (ret)
515 return ret;
516
517 ret = usb_ep_enable(dev->ep_in);
518 if (ret)
519 return ret;
520
521 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
522 if (ret)
523 return ret;
524
525 ret = usb_ep_enable(dev->ep_out);
526 if (ret) {
527 usb_ep_disable(dev->ep_in);
528 return ret;
529 }
530 dev->online = 1;
531
532 /* readers may be blocked waiting for us to go online */
533 wake_up(&dev->read_wq);
534 return 0;
535}
536
537static void adb_function_disable(struct usb_function *f)
538{
539 struct adb_dev *dev = func_to_adb(f);
540 struct usb_composite_dev *cdev = dev->cdev;
541
542 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
543 dev->online = 0;
544 dev->error = 1;
545 usb_ep_disable(dev->ep_in);
546 usb_ep_disable(dev->ep_out);
547
548 /* readers may be blocked waiting for us to go online */
549 wake_up(&dev->read_wq);
550
551 VDBG(cdev, "%s disabled\n", dev->function.name);
552}
553
554static int adb_bind_config(struct usb_configuration *c)
555{
556 struct adb_dev *dev = _adb_dev;
557
558 printk(KERN_INFO "adb_bind_config\n");
559
560 dev->cdev = c->cdev;
561 dev->function.name = "adb";
562 dev->function.descriptors = fs_adb_descs;
563 dev->function.hs_descriptors = hs_adb_descs;
564 dev->function.bind = adb_function_bind;
565 dev->function.unbind = adb_function_unbind;
566 dev->function.set_alt = adb_function_set_alt;
567 dev->function.disable = adb_function_disable;
568
569 return usb_add_function(c, &dev->function);
570}
571
572static int adb_setup(void)
573{
574 struct adb_dev *dev;
575 int ret;
576
577 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
578 if (!dev)
579 return -ENOMEM;
580
581 spin_lock_init(&dev->lock);
582
583 init_waitqueue_head(&dev->read_wq);
584 init_waitqueue_head(&dev->write_wq);
585
586 atomic_set(&dev->open_excl, 0);
587 atomic_set(&dev->read_excl, 0);
588 atomic_set(&dev->write_excl, 0);
589
590 INIT_LIST_HEAD(&dev->tx_idle);
591
592 _adb_dev = dev;
593
594 ret = misc_register(&adb_device);
595 if (ret)
596 goto err;
597
598 return 0;
599
600err:
601 kfree(dev);
602 printk(KERN_ERR "adb gadget driver failed to initialize\n");
603 return ret;
604}
605
606static void adb_cleanup(void)
607{
608 misc_deregister(&adb_device);
609
610 kfree(_adb_dev);
611 _adb_dev = NULL;
612}