Merge remote-tracking branch 'origin/develop-3.0' into develop-3.0-jb
[firefly-linux-kernel-4.4.55.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 #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
35 static const char adb_shortname[] = "android_adb";
36
37 struct 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
60 static 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
70 static 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
78 static 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
86 static 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
93 static 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
100 static 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
107 static 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 static void adb_ready_callback(void);
115 static void adb_closed_callback(void);
116
117 /* temporary variable used between adb_open() and adb_gadget_bind() */
118 static struct adb_dev *_adb_dev;
119
120 static inline struct adb_dev *func_to_adb(struct usb_function *f)
121 {
122         return container_of(f, struct adb_dev, function);
123 }
124
125
126 static 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
142 static 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
150 static 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
160 static inline void adb_unlock(atomic_t *excl)
161 {
162         atomic_dec(excl);
163 }
164
165 /* add a request to the tail of a list */
166 void 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 */
177 struct 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
193 static 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)
198                 dev->error = 1;
199
200         adb_req_put(dev, &dev->tx_idle, req);
201
202         wake_up(&dev->write_wq);
203 }
204
205 static 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;
210         if (req->status != 0)
211                 dev->error = 1;
212
213         wake_up(&dev->read_wq);
214 }
215
216 static 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
262 fail:
263         printk(KERN_ERR "adb_bind() could not allocate requests\n");
264         return -1;
265 }
266
267 static 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 */
286         while (!(dev->online || dev->error)) {
287                 pr_debug("adb_read: waiting for online state\n");
288                 ret = wait_event_interruptible(dev->read_wq,
289                                 (dev->online || dev->error));
290                 if (ret < 0) {
291                         adb_unlock(&dev->read_excl);
292                         return ret;
293                 }
294         }
295         if (dev->error) {
296                 r = -EIO;
297                 goto done;
298         }
299
300 requeue_req:
301         /* queue a request */
302         req = dev->rx_req;
303         req->length = count;
304         dev->rx_done = 0;
305         ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
306         if (ret < 0) {
307                 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
308                 r = -EIO;
309                 dev->error = 1;
310                 goto done;
311         } else {
312                 pr_debug("rx %p queue\n", req);
313         }
314
315         /* wait for a request to complete */
316         ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
317         if (ret < 0) {
318                 dev->error = 1;
319                 r = ret;
320                 usb_ep_dequeue(dev->ep_out, req);
321                 goto done;
322         }
323         if (!dev->error) {
324                 /* If we got a 0-len packet, throw it back and try again. */
325                 if (req->actual == 0)
326                         goto requeue_req;
327
328                 pr_debug("rx %p %d\n", req, req->actual);
329                 xfer = (req->actual < count) ? req->actual : count;
330                 if (copy_to_user(buf, req->buf, xfer))
331                         r = -EFAULT;
332
333         } else
334                 r = -EIO;
335
336 done:
337         adb_unlock(&dev->read_excl);
338         pr_debug("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_request *req = 0;
347         int r = count, xfer;
348         int ret;
349
350         if (!_adb_dev)
351                 return -ENODEV;
352         pr_debug("adb_write(%d)\n", count);
353
354         if (adb_lock(&dev->write_excl))
355                 return -EBUSY;
356
357         while (count > 0) {
358                 if (dev->error) {
359                         pr_debug("adb_write dev->error\n");
360                         r = -EIO;
361                         break;
362                 }
363
364                 /* get an idle tx request to use */
365                 req = 0;
366                 ret = wait_event_interruptible(dev->write_wq,
367                         (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
368
369                 if (ret < 0) {
370                         r = ret;
371                         break;
372                 }
373
374                 if (req != 0) {
375                         if (count > ADB_BULK_BUFFER_SIZE)
376                                 xfer = ADB_BULK_BUFFER_SIZE;
377                         else
378                                 xfer = count;
379                         if (copy_from_user(req->buf, buf, xfer)) {
380                                 r = -EFAULT;
381                                 break;
382                         }
383
384                         req->length = xfer;
385                         ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
386                         if (ret < 0) {
387                                 pr_debug("adb_write: xfer error %d\n", ret);
388                                 dev->error = 1;
389                                 r = -EIO;
390                                 break;
391                         }
392
393                         buf += xfer;
394                         count -= xfer;
395
396                         /* zero this so we don't try to free it on error exit */
397                         req = 0;
398                 }
399         }
400
401         if (req)
402                 adb_req_put(dev, &dev->tx_idle, req);
403
404         adb_unlock(&dev->write_excl);
405         pr_debug("adb_write returning %d\n", r);
406         return r;
407 }
408
409 static int adb_open(struct inode *ip, struct file *fp)
410 {
411         pr_info("adb_open\n");
412         if (!_adb_dev)
413                 return -ENODEV;
414
415         if (adb_lock(&_adb_dev->open_excl))
416                 return -EBUSY;
417
418         fp->private_data = _adb_dev;
419
420         /* clear the error latch */
421         _adb_dev->error = 0;
422
423         adb_ready_callback();
424
425         return 0;
426 }
427
428 static int adb_release(struct inode *ip, struct file *fp)
429 {
430         pr_info("adb_release\n");
431
432         adb_closed_callback();
433
434         adb_unlock(&_adb_dev->open_excl);
435         return 0;
436 }
437
438 /* file operations for ADB device /dev/android_adb */
439 static struct file_operations adb_fops = {
440         .owner = THIS_MODULE,
441         .read = adb_read,
442         .write = adb_write,
443         .open = adb_open,
444         .release = adb_release,
445 };
446
447 static struct miscdevice adb_device = {
448         .minor = MISC_DYNAMIC_MINOR,
449         .name = adb_shortname,
450         .fops = &adb_fops,
451 };
452
453
454
455
456 static int
457 adb_function_bind(struct usb_configuration *c, struct usb_function *f)
458 {
459         struct usb_composite_dev *cdev = c->cdev;
460         struct adb_dev  *dev = func_to_adb(f);
461         int                     id;
462         int                     ret;
463
464         dev->cdev = cdev;
465         DBG(cdev, "adb_function_bind dev: %p\n", dev);
466
467         /* allocate interface ID(s) */
468         id = usb_interface_id(c, f);
469         if (id < 0)
470                 return id;
471         adb_interface_desc.bInterfaceNumber = id;
472
473         /* allocate endpoints */
474         ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
475                         &adb_fullspeed_out_desc);
476         if (ret)
477                 return ret;
478
479         /* support high speed hardware */
480         if (gadget_is_dualspeed(c->cdev->gadget)) {
481                 adb_highspeed_in_desc.bEndpointAddress =
482                         adb_fullspeed_in_desc.bEndpointAddress;
483                 adb_highspeed_out_desc.bEndpointAddress =
484                         adb_fullspeed_out_desc.bEndpointAddress;
485         }
486
487         DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
488                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
489                         f->name, dev->ep_in->name, dev->ep_out->name);
490         return 0;
491 }
492
493 static void
494 adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
495 {
496         struct adb_dev  *dev = func_to_adb(f);
497         struct usb_request *req;
498
499
500         dev->online = 0;
501         dev->error = 1;
502
503         wake_up(&dev->read_wq);
504
505         adb_request_free(dev->rx_req, dev->ep_out);
506         while ((req = adb_req_get(dev, &dev->tx_idle)))
507                 adb_request_free(req, dev->ep_in);
508 }
509
510 static int adb_function_set_alt(struct usb_function *f,
511                 unsigned intf, unsigned alt)
512 {
513         struct adb_dev  *dev = func_to_adb(f);
514         struct usb_composite_dev *cdev = f->config->cdev;
515         int ret;
516
517         DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
518         ret = usb_ep_enable(dev->ep_in,
519                         ep_choose(cdev->gadget,
520                                 &adb_highspeed_in_desc,
521                                 &adb_fullspeed_in_desc));
522         if (ret)
523                 return ret;
524         ret = usb_ep_enable(dev->ep_out,
525                         ep_choose(cdev->gadget,
526                                 &adb_highspeed_out_desc,
527                                 &adb_fullspeed_out_desc));
528         if (ret) {
529                 usb_ep_disable(dev->ep_in);
530                 return ret;
531         }
532         dev->online = 1;
533
534         /* readers may be blocked waiting for us to go online */
535         wake_up(&dev->read_wq);
536         return 0;
537 }
538
539 static void adb_function_disable(struct usb_function *f)
540 {
541         struct adb_dev  *dev = func_to_adb(f);
542         struct usb_composite_dev        *cdev = dev->cdev;
543
544         DBG(cdev, "adb_function_disable cdev %p\n", cdev);
545         dev->online = 0;
546         dev->error = 1;
547         usb_ep_disable(dev->ep_in);
548         dev->ep_in->driver_data = NULL;
549         usb_ep_disable(dev->ep_out);
550         dev->ep_out->driver_data = NULL;
551
552         /* readers may be blocked waiting for us to go online */
553         wake_up(&dev->read_wq);
554
555         VDBG(cdev, "%s disabled\n", dev->function.name);
556 }
557
558 static int adb_bind_config(struct usb_configuration *c)
559 {
560         struct adb_dev *dev = _adb_dev;
561
562         dev->cdev = c->cdev;
563         dev->function.name = "adb";
564         dev->function.descriptors = fs_adb_descs;
565         dev->function.hs_descriptors = hs_adb_descs;
566         dev->function.bind = adb_function_bind;
567         dev->function.unbind = adb_function_unbind;
568         dev->function.set_alt = adb_function_set_alt;
569         dev->function.disable = adb_function_disable;
570
571         return usb_add_function(c, &dev->function);
572 }
573
574 static int adb_setup(void)
575 {
576         struct adb_dev *dev;
577         int ret;
578
579         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
580         if (!dev)
581                 return -ENOMEM;
582
583         spin_lock_init(&dev->lock);
584
585         init_waitqueue_head(&dev->read_wq);
586         init_waitqueue_head(&dev->write_wq);
587
588         atomic_set(&dev->open_excl, 0);
589         atomic_set(&dev->read_excl, 0);
590         atomic_set(&dev->write_excl, 0);
591
592         INIT_LIST_HEAD(&dev->tx_idle);
593
594         _adb_dev = dev;
595
596         ret = misc_register(&adb_device);
597         if (ret)
598                 goto err;
599
600         return 0;
601
602 err:
603         kfree(dev);
604         printk(KERN_ERR "adb gadget driver failed to initialize\n");
605         return ret;
606 }
607
608 static void adb_cleanup(void)
609 {
610         misc_deregister(&adb_device);
611
612         kfree(_adb_dev);
613         _adb_dev = NULL;
614 }