OSDN Git Service

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