blob: 045fc6c8d932285041fe6139a6a578d1ccdb2357 [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
Manu Gautam8f687952011-09-30 15:07:36 +053045 atomic_t online;
46 atomic_t error;
Benoit Goby2b6862d2011-12-19 14:38:41 -080047
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
Benoit Goby80ba14d2012-03-19 18:56:52 -0700114static void adb_ready_callback(void);
115static void adb_closed_callback(void);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800116
117/* temporary variable used between adb_open() and adb_gadget_bind() */
118static struct adb_dev *_adb_dev;
119
120static inline struct adb_dev *func_to_adb(struct usb_function *f)
121{
122 return container_of(f, struct adb_dev, function);
123}
124
125
126static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
127{
128 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
129 if (!req)
130 return NULL;
131
132 /* now allocate buffers for the requests */
133 req->buf = kmalloc(buffer_size, GFP_KERNEL);
134 if (!req->buf) {
135 usb_ep_free_request(ep, req);
136 return NULL;
137 }
138
139 return req;
140}
141
142static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
143{
144 if (req) {
145 kfree(req->buf);
146 usb_ep_free_request(ep, req);
147 }
148}
149
150static inline int adb_lock(atomic_t *excl)
151{
152 if (atomic_inc_return(excl) == 1) {
153 return 0;
154 } else {
155 atomic_dec(excl);
156 return -1;
157 }
158}
159
160static inline void adb_unlock(atomic_t *excl)
161{
162 atomic_dec(excl);
163}
164
165/* add a request to the tail of a list */
166void adb_req_put(struct adb_dev *dev, struct list_head *head,
167 struct usb_request *req)
168{
169 unsigned long flags;
170
171 spin_lock_irqsave(&dev->lock, flags);
172 list_add_tail(&req->list, head);
173 spin_unlock_irqrestore(&dev->lock, flags);
174}
175
176/* remove a request from the head of a list */
177struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
178{
179 unsigned long flags;
180 struct usb_request *req;
181
182 spin_lock_irqsave(&dev->lock, flags);
183 if (list_empty(head)) {
184 req = 0;
185 } else {
186 req = list_first_entry(head, struct usb_request, list);
187 list_del(&req->list);
188 }
189 spin_unlock_irqrestore(&dev->lock, flags);
190 return req;
191}
192
193static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
194{
195 struct adb_dev *dev = _adb_dev;
196
197 if (req->status != 0)
Manu Gautam8f687952011-09-30 15:07:36 +0530198 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800199
200 adb_req_put(dev, &dev->tx_idle, req);
201
202 wake_up(&dev->write_wq);
203}
204
205static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
206{
207 struct adb_dev *dev = _adb_dev;
208
209 dev->rx_done = 1;
Colin Cross8492aa12012-03-08 17:57:51 -0800210 if (req->status != 0 && req->status != -ECONNRESET)
Manu Gautam8f687952011-09-30 15:07:36 +0530211 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800212
213 wake_up(&dev->read_wq);
214}
215
216static int adb_create_bulk_endpoints(struct adb_dev *dev,
217 struct usb_endpoint_descriptor *in_desc,
218 struct usb_endpoint_descriptor *out_desc)
219{
220 struct usb_composite_dev *cdev = dev->cdev;
221 struct usb_request *req;
222 struct usb_ep *ep;
223 int i;
224
225 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
226
227 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
228 if (!ep) {
229 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
230 return -ENODEV;
231 }
232 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
233 ep->driver_data = dev; /* claim the endpoint */
234 dev->ep_in = ep;
235
236 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
237 if (!ep) {
238 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
239 return -ENODEV;
240 }
241 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
242 ep->driver_data = dev; /* claim the endpoint */
243 dev->ep_out = ep;
244
245 /* now allocate requests for our endpoints */
246 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
247 if (!req)
248 goto fail;
249 req->complete = adb_complete_out;
250 dev->rx_req = req;
251
252 for (i = 0; i < TX_REQ_MAX; i++) {
253 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
254 if (!req)
255 goto fail;
256 req->complete = adb_complete_in;
257 adb_req_put(dev, &dev->tx_idle, req);
258 }
259
260 return 0;
261
262fail:
263 printk(KERN_ERR "adb_bind() could not allocate requests\n");
264 return -1;
265}
266
267static ssize_t adb_read(struct file *fp, char __user *buf,
268 size_t count, loff_t *pos)
269{
270 struct adb_dev *dev = fp->private_data;
271 struct usb_request *req;
272 int r = count, xfer;
273 int ret;
274
275 pr_debug("adb_read(%d)\n", count);
276 if (!_adb_dev)
277 return -ENODEV;
278
279 if (count > ADB_BULK_BUFFER_SIZE)
280 return -EINVAL;
281
282 if (adb_lock(&dev->read_excl))
283 return -EBUSY;
284
285 /* we will block until we're online */
Manu Gautam8f687952011-09-30 15:07:36 +0530286 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800287 pr_debug("adb_read: waiting for online state\n");
288 ret = wait_event_interruptible(dev->read_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530289 (atomic_read(&dev->online) ||
290 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800291 if (ret < 0) {
292 adb_unlock(&dev->read_excl);
293 return ret;
294 }
295 }
Manu Gautam8f687952011-09-30 15:07:36 +0530296 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800297 r = -EIO;
298 goto done;
299 }
300
301requeue_req:
302 /* queue a request */
303 req = dev->rx_req;
Ido Shayevitzf73ad6c2012-08-23 05:30:04 +0300304 req->length = ADB_BULK_BUFFER_SIZE;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800305 dev->rx_done = 0;
306 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
307 if (ret < 0) {
308 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
309 r = -EIO;
Manu Gautam8f687952011-09-30 15:07:36 +0530310 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800311 goto done;
312 } else {
313 pr_debug("rx %p queue\n", req);
314 }
315
316 /* wait for a request to complete */
317 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
318 if (ret < 0) {
Colin Cross42582f22012-03-05 13:29:45 -0800319 if (ret != -ERESTARTSYS)
Manu Gautam8f687952011-09-30 15:07:36 +0530320 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800321 r = ret;
322 usb_ep_dequeue(dev->ep_out, req);
323 goto done;
324 }
Manu Gautam8f687952011-09-30 15:07:36 +0530325 if (!atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800326 /* 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) {
Manu Gautam8f687952011-09-30 15:07:36 +0530360 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800361 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,
Manu Gautam8f687952011-09-30 15:07:36 +0530369 ((req = adb_req_get(dev, &dev->tx_idle)) ||
370 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800371
372 if (ret < 0) {
373 r = ret;
374 break;
375 }
376
377 if (req != 0) {
378 if (count > ADB_BULK_BUFFER_SIZE)
379 xfer = ADB_BULK_BUFFER_SIZE;
380 else
381 xfer = count;
382 if (copy_from_user(req->buf, buf, xfer)) {
383 r = -EFAULT;
384 break;
385 }
386
387 req->length = xfer;
388 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
389 if (ret < 0) {
390 pr_debug("adb_write: xfer error %d\n", ret);
Manu Gautam8f687952011-09-30 15:07:36 +0530391 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800392 r = -EIO;
393 break;
394 }
395
396 buf += xfer;
397 count -= xfer;
398
399 /* zero this so we don't try to free it on error exit */
400 req = 0;
401 }
402 }
403
404 if (req)
405 adb_req_put(dev, &dev->tx_idle, req);
406
407 adb_unlock(&dev->write_excl);
408 pr_debug("adb_write returning %d\n", r);
409 return r;
410}
411
412static int adb_open(struct inode *ip, struct file *fp)
413{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700414 pr_info("adb_open\n");
Benoit Goby2b6862d2011-12-19 14:38:41 -0800415 if (!_adb_dev)
416 return -ENODEV;
417
418 if (adb_lock(&_adb_dev->open_excl))
419 return -EBUSY;
420
421 fp->private_data = _adb_dev;
422
423 /* clear the error latch */
Manu Gautam8f687952011-09-30 15:07:36 +0530424 atomic_set(&_adb_dev->error, 0);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800425
Mayank Ranaff3e0492012-08-08 12:17:57 +0530426 adb_ready_callback();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700427
Benoit Goby2b6862d2011-12-19 14:38:41 -0800428 return 0;
429}
430
431static int adb_release(struct inode *ip, struct file *fp)
432{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700433 pr_info("adb_release\n");
434
Mayank Ranaff3e0492012-08-08 12:17:57 +0530435 adb_closed_callback();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700436
Benoit Goby2b6862d2011-12-19 14:38:41 -0800437 adb_unlock(&_adb_dev->open_excl);
438 return 0;
439}
440
441/* file operations for ADB device /dev/android_adb */
442static const struct file_operations adb_fops = {
443 .owner = THIS_MODULE,
444 .read = adb_read,
445 .write = adb_write,
446 .open = adb_open,
447 .release = adb_release,
448};
449
450static struct miscdevice adb_device = {
451 .minor = MISC_DYNAMIC_MINOR,
452 .name = adb_shortname,
453 .fops = &adb_fops,
454};
455
456
457
458
459static int
460adb_function_bind(struct usb_configuration *c, struct usb_function *f)
461{
462 struct usb_composite_dev *cdev = c->cdev;
463 struct adb_dev *dev = func_to_adb(f);
464 int id;
465 int ret;
466
467 dev->cdev = cdev;
468 DBG(cdev, "adb_function_bind dev: %p\n", dev);
469
470 /* allocate interface ID(s) */
471 id = usb_interface_id(c, f);
472 if (id < 0)
473 return id;
474 adb_interface_desc.bInterfaceNumber = id;
475
476 /* allocate endpoints */
477 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
478 &adb_fullspeed_out_desc);
479 if (ret)
480 return ret;
481
482 /* support high speed hardware */
483 if (gadget_is_dualspeed(c->cdev->gadget)) {
484 adb_highspeed_in_desc.bEndpointAddress =
485 adb_fullspeed_in_desc.bEndpointAddress;
486 adb_highspeed_out_desc.bEndpointAddress =
487 adb_fullspeed_out_desc.bEndpointAddress;
488 }
489
490 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
491 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
492 f->name, dev->ep_in->name, dev->ep_out->name);
493 return 0;
494}
495
496static void
497adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
498{
499 struct adb_dev *dev = func_to_adb(f);
500 struct usb_request *req;
501
502
Manu Gautam8f687952011-09-30 15:07:36 +0530503 atomic_set(&dev->online, 0);
504 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800505
506 wake_up(&dev->read_wq);
507
508 adb_request_free(dev->rx_req, dev->ep_out);
509 while ((req = adb_req_get(dev, &dev->tx_idle)))
510 adb_request_free(req, dev->ep_in);
511}
512
513static int adb_function_set_alt(struct usb_function *f,
514 unsigned intf, unsigned alt)
515{
516 struct adb_dev *dev = func_to_adb(f);
517 struct usb_composite_dev *cdev = f->config->cdev;
518 int ret;
519
520 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
521
522 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300523 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200524 dev->ep_in->desc = NULL;
525 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
526 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800527 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200528 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800529 ret = usb_ep_enable(dev->ep_in);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200530 if (ret) {
531 ERROR(cdev, "failed to enable ep %s, result %d\n",
532 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800533 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200534 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800535
536 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800537 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200538 dev->ep_out->desc = NULL;
539 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
540 dev->ep_out->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800541 usb_ep_disable(dev->ep_in);
542 return ret;
543 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200544 ret = usb_ep_enable(dev->ep_out);
545 if (ret) {
546 ERROR(cdev, "failed to enable ep %s, result %d\n",
547 dev->ep_out->name, ret);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500548 usb_ep_disable(dev->ep_in);
549 return ret;
550 }
Manu Gautam8f687952011-09-30 15:07:36 +0530551 atomic_set(&dev->online, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800552
553 /* readers may be blocked waiting for us to go online */
554 wake_up(&dev->read_wq);
555 return 0;
556}
557
558static void adb_function_disable(struct usb_function *f)
559{
560 struct adb_dev *dev = func_to_adb(f);
561 struct usb_composite_dev *cdev = dev->cdev;
562
563 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
Manu Gautam8f687952011-09-30 15:07:36 +0530564 atomic_set(&dev->online, 0);
565 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800566 usb_ep_disable(dev->ep_in);
567 usb_ep_disable(dev->ep_out);
568
569 /* readers may be blocked waiting for us to go online */
570 wake_up(&dev->read_wq);
571
572 VDBG(cdev, "%s disabled\n", dev->function.name);
573}
574
575static int adb_bind_config(struct usb_configuration *c)
576{
577 struct adb_dev *dev = _adb_dev;
578
579 printk(KERN_INFO "adb_bind_config\n");
580
581 dev->cdev = c->cdev;
582 dev->function.name = "adb";
583 dev->function.descriptors = fs_adb_descs;
584 dev->function.hs_descriptors = hs_adb_descs;
585 dev->function.bind = adb_function_bind;
586 dev->function.unbind = adb_function_unbind;
587 dev->function.set_alt = adb_function_set_alt;
588 dev->function.disable = adb_function_disable;
589
590 return usb_add_function(c, &dev->function);
591}
592
593static int adb_setup(void)
594{
595 struct adb_dev *dev;
596 int ret;
597
598 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
599 if (!dev)
600 return -ENOMEM;
601
602 spin_lock_init(&dev->lock);
603
604 init_waitqueue_head(&dev->read_wq);
605 init_waitqueue_head(&dev->write_wq);
606
607 atomic_set(&dev->open_excl, 0);
608 atomic_set(&dev->read_excl, 0);
609 atomic_set(&dev->write_excl, 0);
610
Benoit Goby2b6862d2011-12-19 14:38:41 -0800611 INIT_LIST_HEAD(&dev->tx_idle);
612
613 _adb_dev = dev;
614
615 ret = misc_register(&adb_device);
616 if (ret)
617 goto err;
618
619 return 0;
620
621err:
622 kfree(dev);
623 printk(KERN_ERR "adb gadget driver failed to initialize\n");
624 return ret;
625}
626
627static void adb_cleanup(void)
628{
629 misc_deregister(&adb_device);
630
631 kfree(_adb_dev);
632 _adb_dev = NULL;
633}