Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / usb / gadget / f_hid.c
1 /*
2  * f_hid.c -- USB HID function driver
3  *
4  * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/utsname.h>
14 #include <linux/module.h>
15 #include <linux/hid.h>
16 #include <linux/cdev.h>
17 #include <linux/mutex.h>
18 #include <linux/poll.h>
19 #include <linux/uaccess.h>
20 #include <linux/wait.h>
21 #include <linux/usb/g_hid.h>
22
23 static int major, minors;
24 static struct class *hidg_class;
25
26 /*-------------------------------------------------------------------------*/
27 /*                            HID gadget struct                            */
28
29 struct f_hidg {
30         /* configuration */
31         unsigned char                   bInterfaceSubClass;
32         unsigned char                   bInterfaceProtocol;
33         unsigned short                  report_desc_length;
34         char                            *report_desc;
35         unsigned short                  report_length;
36
37         /* recv report */
38         char                            *set_report_buff;
39         unsigned short                  set_report_length;
40         spinlock_t                      read_spinlock;
41         wait_queue_head_t               read_queue;
42
43         /* send report */
44         spinlock_t                      write_spinlock;
45         bool                            write_pending;
46         wait_queue_head_t               write_queue;
47         struct usb_request              *req;
48
49         int                             minor;
50         struct cdev                     cdev;
51         struct usb_function             func;
52         struct usb_ep                   *in_ep;
53 };
54
55 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
56 {
57         return container_of(f, struct f_hidg, func);
58 }
59
60 /*-------------------------------------------------------------------------*/
61 /*                           Static descriptors                            */
62
63 static struct usb_interface_descriptor hidg_interface_desc = {
64         .bLength                = sizeof hidg_interface_desc,
65         .bDescriptorType        = USB_DT_INTERFACE,
66         /* .bInterfaceNumber    = DYNAMIC */
67         .bAlternateSetting      = 0,
68         .bNumEndpoints          = 1,
69         .bInterfaceClass        = USB_CLASS_HID,
70         /* .bInterfaceSubClass  = DYNAMIC */
71         /* .bInterfaceProtocol  = DYNAMIC */
72         /* .iInterface          = DYNAMIC */
73 };
74
75 static struct hid_descriptor hidg_desc = {
76         .bLength                        = sizeof hidg_desc,
77         .bDescriptorType                = HID_DT_HID,
78         .bcdHID                         = 0x0101,
79         .bCountryCode                   = 0x00,
80         .bNumDescriptors                = 0x1,
81         /*.desc[0].bDescriptorType      = DYNAMIC */
82         /*.desc[0].wDescriptorLenght    = DYNAMIC */
83 };
84
85 /* High-Speed Support */
86
87 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
88         .bLength                = USB_DT_ENDPOINT_SIZE,
89         .bDescriptorType        = USB_DT_ENDPOINT,
90         .bEndpointAddress       = USB_DIR_IN,
91         .bmAttributes           = USB_ENDPOINT_XFER_INT,
92         /*.wMaxPacketSize       = DYNAMIC */
93         .bInterval              = 4, /* FIXME: Add this field in the
94                                       * HID gadget configuration?
95                                       * (struct hidg_func_descriptor)
96                                       */
97 };
98
99 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
100         (struct usb_descriptor_header *)&hidg_interface_desc,
101         (struct usb_descriptor_header *)&hidg_desc,
102         (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
103         NULL,
104 };
105
106 /* Full-Speed Support */
107
108 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
109         .bLength                = USB_DT_ENDPOINT_SIZE,
110         .bDescriptorType        = USB_DT_ENDPOINT,
111         .bEndpointAddress       = USB_DIR_IN,
112         .bmAttributes           = USB_ENDPOINT_XFER_INT,
113         /*.wMaxPacketSize       = DYNAMIC */
114         .bInterval              = 10, /* FIXME: Add this field in the
115                                        * HID gadget configuration?
116                                        * (struct hidg_func_descriptor)
117                                        */
118 };
119
120 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
121         (struct usb_descriptor_header *)&hidg_interface_desc,
122         (struct usb_descriptor_header *)&hidg_desc,
123         (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
124         NULL,
125 };
126
127 /*-------------------------------------------------------------------------*/
128 /*                              Char Device                                */
129
130 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
131                         size_t count, loff_t *ptr)
132 {
133         struct f_hidg   *hidg     = file->private_data;
134         char            *tmp_buff = NULL;
135         unsigned long   flags;
136
137         if (!count)
138                 return 0;
139
140         if (!access_ok(VERIFY_WRITE, buffer, count))
141                 return -EFAULT;
142
143         spin_lock_irqsave(&hidg->read_spinlock, flags);
144
145 #define READ_COND (hidg->set_report_buff != NULL)
146
147         while (!READ_COND) {
148                 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
149                 if (file->f_flags & O_NONBLOCK)
150                         return -EAGAIN;
151
152                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
153                         return -ERESTARTSYS;
154
155                 spin_lock_irqsave(&hidg->read_spinlock, flags);
156         }
157
158
159         count = min_t(unsigned, count, hidg->set_report_length);
160         tmp_buff = hidg->set_report_buff;
161         hidg->set_report_buff = NULL;
162
163         spin_unlock_irqrestore(&hidg->read_spinlock, flags);
164
165         if (tmp_buff != NULL) {
166                 /* copy to user outside spinlock */
167                 count -= copy_to_user(buffer, tmp_buff, count);
168                 kfree(tmp_buff);
169         } else
170                 count = -ENOMEM;
171
172         return count;
173 }
174
175 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
176 {
177         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
178         unsigned long flags;
179
180         if (req->status != 0) {
181                 ERROR(hidg->func.config->cdev,
182                         "End Point Request ERROR: %d\n", req->status);
183         }
184
185         spin_lock_irqsave(&hidg->write_spinlock, flags);
186         hidg->write_pending = 0;
187         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
188         wake_up(&hidg->write_queue);
189 }
190
191 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
192                             size_t count, loff_t *offp)
193 {
194         struct f_hidg *hidg  = file->private_data;
195         unsigned long flags;
196         ssize_t status = -ENOMEM;
197
198         if (!access_ok(VERIFY_READ, buffer, count))
199                 return -EFAULT;
200
201         spin_lock_irqsave(&hidg->write_spinlock, flags);
202
203 #define WRITE_COND (!hidg->write_pending)
204
205         /* write queue */
206         while (!WRITE_COND) {
207                 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
208                 if (file->f_flags & O_NONBLOCK)
209                         return -EAGAIN;
210
211                 if (wait_event_interruptible_exclusive(
212                                 hidg->write_queue, WRITE_COND))
213                         return -ERESTARTSYS;
214
215                 spin_lock_irqsave(&hidg->write_spinlock, flags);
216         }
217
218         hidg->write_pending = 1;
219         count  = min_t(unsigned, count, hidg->report_length);
220
221         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
222         status = copy_from_user(hidg->req->buf, buffer, count);
223
224         if (status != 0) {
225                 ERROR(hidg->func.config->cdev,
226                         "copy_from_user error\n");
227                 status = -EINVAL;
228                 goto release_write_pending;
229         }
230
231         hidg->req->status   = 0;
232         hidg->req->zero     = 0;
233         hidg->req->length   = count;
234         hidg->req->complete = f_hidg_req_complete;
235         hidg->req->context  = hidg;
236
237         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
238         if (status < 0) {
239                 ERROR(hidg->func.config->cdev,
240                         "usb_ep_queue error on int endpoint %zd\n", status);
241                 goto release_write_pending;
242         } else {
243                 status = count;
244         }
245
246         return status;
247 release_write_pending:
248         spin_lock_irqsave(&hidg->write_spinlock, flags);
249         hidg->write_pending = 0;
250         spin_unlock_irqrestore(&hidg->write_spinlock, flags);
251
252         wake_up(&hidg->write_queue);
253
254         return status;
255 }
256
257 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
258 {
259         struct f_hidg   *hidg  = file->private_data;
260         unsigned int    ret = 0;
261
262         poll_wait(file, &hidg->read_queue, wait);
263         poll_wait(file, &hidg->write_queue, wait);
264
265         if (WRITE_COND)
266                 ret |= POLLOUT | POLLWRNORM;
267
268         if (READ_COND)
269                 ret |= POLLIN | POLLRDNORM;
270
271         return ret;
272 }
273
274 #undef WRITE_COND
275 #undef READ_COND
276
277 static int f_hidg_release(struct inode *inode, struct file *fd)
278 {
279         fd->private_data = NULL;
280         return 0;
281 }
282
283 static int f_hidg_open(struct inode *inode, struct file *fd)
284 {
285         struct f_hidg *hidg =
286                 container_of(inode->i_cdev, struct f_hidg, cdev);
287
288         fd->private_data = hidg;
289
290         return 0;
291 }
292
293 /*-------------------------------------------------------------------------*/
294 /*                                usb_function                             */
295
296 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
297 {
298         struct f_hidg *hidg = (struct f_hidg *)req->context;
299
300         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
301                 ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
302                 return;
303         }
304
305         spin_lock(&hidg->read_spinlock);
306
307         hidg->set_report_buff = krealloc(hidg->set_report_buff,
308                                          req->actual, GFP_ATOMIC);
309
310         if (hidg->set_report_buff == NULL) {
311                 spin_unlock(&hidg->read_spinlock);
312                 return;
313         }
314         hidg->set_report_length = req->actual;
315         memcpy(hidg->set_report_buff, req->buf, req->actual);
316
317         spin_unlock(&hidg->read_spinlock);
318
319         wake_up(&hidg->read_queue);
320 }
321
322 static int hidg_setup(struct usb_function *f,
323                 const struct usb_ctrlrequest *ctrl)
324 {
325         struct f_hidg                   *hidg = func_to_hidg(f);
326         struct usb_composite_dev        *cdev = f->config->cdev;
327         struct usb_request              *req  = cdev->req;
328         int status = 0;
329         __u16 value, length;
330
331         value   = __le16_to_cpu(ctrl->wValue);
332         length  = __le16_to_cpu(ctrl->wLength);
333
334         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
335                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
336
337         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
338         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
339                   | HID_REQ_GET_REPORT):
340                 VDBG(cdev, "get_report\n");
341
342                 /* send an empty report */
343                 length = min_t(unsigned, length, hidg->report_length);
344                 memset(req->buf, 0x0, length);
345
346                 goto respond;
347                 break;
348
349         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
350                   | HID_REQ_GET_PROTOCOL):
351                 VDBG(cdev, "get_protocol\n");
352                 goto stall;
353                 break;
354
355         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
356                   | HID_REQ_SET_REPORT):
357                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
358                 req->context  = hidg;
359                 req->complete = hidg_set_report_complete;
360                 goto respond;
361                 break;
362
363         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
364                   | HID_REQ_SET_PROTOCOL):
365                 VDBG(cdev, "set_protocol\n");
366                 goto stall;
367                 break;
368
369         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
370                   | USB_REQ_GET_DESCRIPTOR):
371                 switch (value >> 8) {
372                 case HID_DT_HID:
373                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
374                         length = min_t(unsigned short, length,
375                                                    hidg_desc.bLength);
376                         memcpy(req->buf, &hidg_desc, length);
377                         goto respond;
378                         break;
379                 case HID_DT_REPORT:
380                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
381                         length = min_t(unsigned short, length,
382                                                    hidg->report_desc_length);
383                         memcpy(req->buf, hidg->report_desc, length);
384                         goto respond;
385                         break;
386
387                 default:
388                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
389                                  value >> 8);
390                         goto stall;
391                         break;
392                 }
393                 break;
394
395         default:
396                 VDBG(cdev, "Unknown request 0x%x\n",
397                          ctrl->bRequest);
398                 goto stall;
399                 break;
400         }
401
402 stall:
403         return -EOPNOTSUPP;
404
405 respond:
406         req->zero = 0;
407         req->length = length;
408         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
409         if (status < 0)
410                 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
411         return status;
412 }
413
414 static void hidg_disable(struct usb_function *f)
415 {
416         struct f_hidg *hidg = func_to_hidg(f);
417
418         usb_ep_disable(hidg->in_ep);
419         hidg->in_ep->driver_data = NULL;
420 }
421
422 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
423 {
424         struct usb_composite_dev                *cdev = f->config->cdev;
425         struct f_hidg                           *hidg = func_to_hidg(f);
426         int status = 0;
427
428         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
429
430         if (hidg->in_ep != NULL) {
431                 /* restart endpoint */
432                 if (hidg->in_ep->driver_data != NULL)
433                         usb_ep_disable(hidg->in_ep);
434
435                 status = config_ep_by_speed(f->config->cdev->gadget, f,
436                                             hidg->in_ep);
437                 if (status) {
438                         ERROR(cdev, "config_ep_by_speed FAILED!\n");
439                         goto fail;
440                 }
441                 status = usb_ep_enable(hidg->in_ep);
442                 if (status < 0) {
443                         ERROR(cdev, "Enable endpoint FAILED!\n");
444                         goto fail;
445                 }
446                 hidg->in_ep->driver_data = hidg;
447         }
448 fail:
449         return status;
450 }
451
452 const struct file_operations f_hidg_fops = {
453         .owner          = THIS_MODULE,
454         .open           = f_hidg_open,
455         .release        = f_hidg_release,
456         .write          = f_hidg_write,
457         .read           = f_hidg_read,
458         .poll           = f_hidg_poll,
459         .llseek         = noop_llseek,
460 };
461
462 static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
463 {
464         struct usb_ep           *ep;
465         struct f_hidg           *hidg = func_to_hidg(f);
466         int                     status;
467         dev_t                   dev;
468
469         /* allocate instance-specific interface IDs, and patch descriptors */
470         status = usb_interface_id(c, f);
471         if (status < 0)
472                 goto fail;
473         hidg_interface_desc.bInterfaceNumber = status;
474
475
476         /* allocate instance-specific endpoints */
477         status = -ENODEV;
478         ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
479         if (!ep)
480                 goto fail;
481         ep->driver_data = c->cdev;      /* claim */
482         hidg->in_ep = ep;
483
484         /* preallocate request and buffer */
485         status = -ENOMEM;
486         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
487         if (!hidg->req)
488                 goto fail;
489
490
491         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
492         if (!hidg->req->buf)
493                 goto fail;
494
495         /* set descriptor dynamic values */
496         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
497         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
498         hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
499         hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
500         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
501         hidg_desc.desc[0].wDescriptorLength =
502                 cpu_to_le16(hidg->report_desc_length);
503
504         hidg->set_report_buff = NULL;
505
506         /* copy descriptors */
507         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
508         if (!f->descriptors)
509                 goto fail;
510
511         if (gadget_is_dualspeed(c->cdev->gadget)) {
512                 hidg_hs_in_ep_desc.bEndpointAddress =
513                         hidg_fs_in_ep_desc.bEndpointAddress;
514                 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
515                 if (!f->hs_descriptors)
516                         goto fail;
517         }
518
519         spin_lock_init(&hidg->write_spinlock);
520         spin_lock_init(&hidg->read_spinlock);
521         init_waitqueue_head(&hidg->write_queue);
522         init_waitqueue_head(&hidg->read_queue);
523
524         /* create char device */
525         cdev_init(&hidg->cdev, &f_hidg_fops);
526         dev = MKDEV(major, hidg->minor);
527         status = cdev_add(&hidg->cdev, dev, 1);
528         if (status)
529                 goto fail;
530
531         device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
532
533         return 0;
534
535 fail:
536         ERROR(f->config->cdev, "hidg_bind FAILED\n");
537         if (hidg->req != NULL) {
538                 kfree(hidg->req->buf);
539                 if (hidg->in_ep != NULL)
540                         usb_ep_free_request(hidg->in_ep, hidg->req);
541         }
542
543         usb_free_descriptors(f->hs_descriptors);
544         usb_free_descriptors(f->descriptors);
545
546         return status;
547 }
548
549 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
550 {
551         struct f_hidg *hidg = func_to_hidg(f);
552
553         device_destroy(hidg_class, MKDEV(major, hidg->minor));
554         cdev_del(&hidg->cdev);
555
556         /* disable/free request and end point */
557         usb_ep_disable(hidg->in_ep);
558         usb_ep_dequeue(hidg->in_ep, hidg->req);
559         kfree(hidg->req->buf);
560         usb_ep_free_request(hidg->in_ep, hidg->req);
561
562         /* free descriptors copies */
563         usb_free_descriptors(f->hs_descriptors);
564         usb_free_descriptors(f->descriptors);
565
566         kfree(hidg->report_desc);
567         kfree(hidg->set_report_buff);
568         kfree(hidg);
569 }
570
571 /*-------------------------------------------------------------------------*/
572 /*                                 Strings                                 */
573
574 #define CT_FUNC_HID_IDX 0
575
576 static struct usb_string ct_func_string_defs[] = {
577         [CT_FUNC_HID_IDX].s     = "HID Interface",
578         {},                     /* end of list */
579 };
580
581 static struct usb_gadget_strings ct_func_string_table = {
582         .language       = 0x0409,       /* en-US */
583         .strings        = ct_func_string_defs,
584 };
585
586 static struct usb_gadget_strings *ct_func_strings[] = {
587         &ct_func_string_table,
588         NULL,
589 };
590
591 /*-------------------------------------------------------------------------*/
592 /*                             usb_configuration                           */
593
594 int __init hidg_bind_config(struct usb_configuration *c,
595                             struct hidg_func_descriptor *fdesc, int index)
596 {
597         struct f_hidg *hidg;
598         int status;
599
600         if (index >= minors)
601                 return -ENOENT;
602
603         /* maybe allocate device-global string IDs, and patch descriptors */
604         if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
605                 status = usb_string_id(c->cdev);
606                 if (status < 0)
607                         return status;
608                 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
609                 hidg_interface_desc.iInterface = status;
610         }
611
612         /* allocate and initialize one new instance */
613         hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
614         if (!hidg)
615                 return -ENOMEM;
616
617         hidg->minor = index;
618         hidg->bInterfaceSubClass = fdesc->subclass;
619         hidg->bInterfaceProtocol = fdesc->protocol;
620         hidg->report_length = fdesc->report_length;
621         hidg->report_desc_length = fdesc->report_desc_length;
622         hidg->report_desc = kmemdup(fdesc->report_desc,
623                                     fdesc->report_desc_length,
624                                     GFP_KERNEL);
625         if (!hidg->report_desc) {
626                 kfree(hidg);
627                 return -ENOMEM;
628         }
629
630         hidg->func.name    = "hid";
631         hidg->func.strings = ct_func_strings;
632         hidg->func.bind    = hidg_bind;
633         hidg->func.unbind  = hidg_unbind;
634         hidg->func.set_alt = hidg_set_alt;
635         hidg->func.disable = hidg_disable;
636         hidg->func.setup   = hidg_setup;
637
638         status = usb_add_function(c, &hidg->func);
639         if (status)
640                 kfree(hidg);
641
642         return status;
643 }
644
645 int __init ghid_setup(struct usb_gadget *g, int count)
646 {
647         int status;
648         dev_t dev;
649
650         hidg_class = class_create(THIS_MODULE, "hidg");
651
652         status = alloc_chrdev_region(&dev, 0, count, "hidg");
653         if (!status) {
654                 major = MAJOR(dev);
655                 minors = count;
656         }
657
658         return status;
659 }
660
661 void ghid_cleanup(void)
662 {
663         if (major) {
664                 unregister_chrdev_region(MKDEV(major, 0), minors);
665                 major = minors = 0;
666         }
667
668         class_destroy(hidg_class);
669         hidg_class = NULL;
670 }