]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/f_hid.c
Merge git://git.infradead.org/battery-2.6
[karo-tx-linux.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  * 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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/utsname.h>
23 #include <linux/module.h>
24 #include <linux/hid.h>
25 #include <linux/cdev.h>
26 #include <linux/mutex.h>
27 #include <linux/poll.h>
28 #include <linux/smp_lock.h>
29 #include <linux/uaccess.h>
30 #include <linux/wait.h>
31 #include <linux/usb/g_hid.h>
32
33 static int major, minors;
34 static struct class *hidg_class;
35
36 /*-------------------------------------------------------------------------*/
37 /*                            HID gadget struct                            */
38
39 struct f_hidg {
40         /* configuration */
41         unsigned char                   bInterfaceSubClass;
42         unsigned char                   bInterfaceProtocol;
43         unsigned short                  report_desc_length;
44         char                            *report_desc;
45         unsigned short                  report_length;
46
47         /* recv report */
48         char                            *set_report_buff;
49         unsigned short                  set_report_length;
50         spinlock_t                      spinlock;
51         wait_queue_head_t               read_queue;
52
53         /* send report */
54         struct mutex                    lock;
55         bool                            write_pending;
56         wait_queue_head_t               write_queue;
57         struct usb_request              *req;
58
59         int                             minor;
60         struct cdev                     cdev;
61         struct usb_function             func;
62         struct usb_ep                   *in_ep;
63         struct usb_endpoint_descriptor  *fs_in_ep_desc;
64         struct usb_endpoint_descriptor  *hs_in_ep_desc;
65 };
66
67 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
68 {
69         return container_of(f, struct f_hidg, func);
70 }
71
72 /*-------------------------------------------------------------------------*/
73 /*                           Static descriptors                            */
74
75 static struct usb_interface_descriptor hidg_interface_desc = {
76         .bLength                = sizeof hidg_interface_desc,
77         .bDescriptorType        = USB_DT_INTERFACE,
78         /* .bInterfaceNumber    = DYNAMIC */
79         .bAlternateSetting      = 0,
80         .bNumEndpoints          = 1,
81         .bInterfaceClass        = USB_CLASS_HID,
82         /* .bInterfaceSubClass  = DYNAMIC */
83         /* .bInterfaceProtocol  = DYNAMIC */
84         /* .iInterface          = DYNAMIC */
85 };
86
87 static struct hid_descriptor hidg_desc = {
88         .bLength                        = sizeof hidg_desc,
89         .bDescriptorType                = HID_DT_HID,
90         .bcdHID                         = 0x0101,
91         .bCountryCode                   = 0x00,
92         .bNumDescriptors                = 0x1,
93         /*.desc[0].bDescriptorType      = DYNAMIC */
94         /*.desc[0].wDescriptorLenght    = DYNAMIC */
95 };
96
97 /* High-Speed Support */
98
99 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
100         .bLength                = USB_DT_ENDPOINT_SIZE,
101         .bDescriptorType        = USB_DT_ENDPOINT,
102         .bEndpointAddress       = USB_DIR_IN,
103         .bmAttributes           = USB_ENDPOINT_XFER_INT,
104         /*.wMaxPacketSize       = DYNAMIC */
105         .bInterval              = 4, /* FIXME: Add this field in the
106                                       * HID gadget configuration?
107                                       * (struct hidg_func_descriptor)
108                                       */
109 };
110
111 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
112         (struct usb_descriptor_header *)&hidg_interface_desc,
113         (struct usb_descriptor_header *)&hidg_desc,
114         (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
115         NULL,
116 };
117
118 /* Full-Speed Support */
119
120 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
121         .bLength                = USB_DT_ENDPOINT_SIZE,
122         .bDescriptorType        = USB_DT_ENDPOINT,
123         .bEndpointAddress       = USB_DIR_IN,
124         .bmAttributes           = USB_ENDPOINT_XFER_INT,
125         /*.wMaxPacketSize       = DYNAMIC */
126         .bInterval              = 10, /* FIXME: Add this field in the
127                                        * HID gadget configuration?
128                                        * (struct hidg_func_descriptor)
129                                        */
130 };
131
132 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
133         (struct usb_descriptor_header *)&hidg_interface_desc,
134         (struct usb_descriptor_header *)&hidg_desc,
135         (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
136         NULL,
137 };
138
139 /*-------------------------------------------------------------------------*/
140 /*                              Char Device                                */
141
142 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
143                         size_t count, loff_t *ptr)
144 {
145         struct f_hidg   *hidg     = file->private_data;
146         char            *tmp_buff = NULL;
147         unsigned long   flags;
148
149         if (!count)
150                 return 0;
151
152         if (!access_ok(VERIFY_WRITE, buffer, count))
153                 return -EFAULT;
154
155         spin_lock_irqsave(&hidg->spinlock, flags);
156
157 #define READ_COND (hidg->set_report_buff != NULL)
158
159         while (!READ_COND) {
160                 spin_unlock_irqrestore(&hidg->spinlock, flags);
161                 if (file->f_flags & O_NONBLOCK)
162                         return -EAGAIN;
163
164                 if (wait_event_interruptible(hidg->read_queue, READ_COND))
165                         return -ERESTARTSYS;
166
167                 spin_lock_irqsave(&hidg->spinlock, flags);
168         }
169
170
171         count = min_t(unsigned, count, hidg->set_report_length);
172         tmp_buff = hidg->set_report_buff;
173         hidg->set_report_buff = NULL;
174
175         spin_unlock_irqrestore(&hidg->spinlock, flags);
176
177         if (tmp_buff != NULL) {
178                 /* copy to user outside spinlock */
179                 count -= copy_to_user(buffer, tmp_buff, count);
180                 kfree(tmp_buff);
181         } else
182                 count = -ENOMEM;
183
184         return count;
185 }
186
187 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
188 {
189         struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
190
191         if (req->status != 0) {
192                 ERROR(hidg->func.config->cdev,
193                         "End Point Request ERROR: %d\n", req->status);
194         }
195
196         hidg->write_pending = 0;
197         wake_up(&hidg->write_queue);
198 }
199
200 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
201                             size_t count, loff_t *offp)
202 {
203         struct f_hidg *hidg  = file->private_data;
204         ssize_t status = -ENOMEM;
205
206         if (!access_ok(VERIFY_READ, buffer, count))
207                 return -EFAULT;
208
209         mutex_lock(&hidg->lock);
210
211 #define WRITE_COND (!hidg->write_pending)
212
213         /* write queue */
214         while (!WRITE_COND) {
215                 mutex_unlock(&hidg->lock);
216                 if (file->f_flags & O_NONBLOCK)
217                         return -EAGAIN;
218
219                 if (wait_event_interruptible_exclusive(
220                                 hidg->write_queue, WRITE_COND))
221                         return -ERESTARTSYS;
222
223                 mutex_lock(&hidg->lock);
224         }
225
226         count  = min_t(unsigned, count, hidg->report_length);
227         status = copy_from_user(hidg->req->buf, buffer, count);
228
229         if (status != 0) {
230                 ERROR(hidg->func.config->cdev,
231                         "copy_from_user error\n");
232                 mutex_unlock(&hidg->lock);
233                 return -EINVAL;
234         }
235
236         hidg->req->status   = 0;
237         hidg->req->zero     = 0;
238         hidg->req->length   = count;
239         hidg->req->complete = f_hidg_req_complete;
240         hidg->req->context  = hidg;
241         hidg->write_pending = 1;
242
243         status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
244         if (status < 0) {
245                 ERROR(hidg->func.config->cdev,
246                         "usb_ep_queue error on int endpoint %zd\n", status);
247                 hidg->write_pending = 0;
248                 wake_up(&hidg->write_queue);
249         } else {
250                 status = count;
251         }
252
253         mutex_unlock(&hidg->lock);
254
255         return status;
256 }
257
258 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
259 {
260         struct f_hidg   *hidg  = file->private_data;
261         unsigned int    ret = 0;
262
263         poll_wait(file, &hidg->read_queue, wait);
264         poll_wait(file, &hidg->write_queue, wait);
265
266         if (WRITE_COND)
267                 ret |= POLLOUT | POLLWRNORM;
268
269         if (READ_COND)
270                 ret |= POLLIN | POLLRDNORM;
271
272         return ret;
273 }
274
275 #undef WRITE_COND
276 #undef READ_COND
277
278 static int f_hidg_release(struct inode *inode, struct file *fd)
279 {
280         fd->private_data = NULL;
281         return 0;
282 }
283
284 static int f_hidg_open(struct inode *inode, struct file *fd)
285 {
286         struct f_hidg *hidg =
287                 container_of(inode->i_cdev, struct f_hidg, cdev);
288
289         fd->private_data = hidg;
290
291         return 0;
292 }
293
294 /*-------------------------------------------------------------------------*/
295 /*                                usb_function                             */
296
297 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
298 {
299         struct f_hidg *hidg = (struct f_hidg *)req->context;
300
301         if (req->status != 0 || req->buf == NULL || req->actual == 0) {
302                 ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
303                 return;
304         }
305
306         spin_lock(&hidg->spinlock);
307
308         hidg->set_report_buff = krealloc(hidg->set_report_buff,
309                                          req->actual, GFP_ATOMIC);
310
311         if (hidg->set_report_buff == NULL) {
312                 spin_unlock(&hidg->spinlock);
313                 return;
314         }
315         hidg->set_report_length = req->actual;
316         memcpy(hidg->set_report_buff, req->buf, req->actual);
317
318         spin_unlock(&hidg->spinlock);
319
320         wake_up(&hidg->read_queue);
321 }
322
323 static int hidg_setup(struct usb_function *f,
324                 const struct usb_ctrlrequest *ctrl)
325 {
326         struct f_hidg                   *hidg = func_to_hidg(f);
327         struct usb_composite_dev        *cdev = f->config->cdev;
328         struct usb_request              *req  = cdev->req;
329         int status = 0;
330         __u16 value, length;
331
332         value   = __le16_to_cpu(ctrl->wValue);
333         length  = __le16_to_cpu(ctrl->wLength);
334
335         VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
336                 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
337
338         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
339         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
340                   | HID_REQ_GET_REPORT):
341                 VDBG(cdev, "get_report\n");
342
343                 /* send an empty report */
344                 length = min_t(unsigned, length, hidg->report_length);
345                 memset(req->buf, 0x0, length);
346
347                 goto respond;
348                 break;
349
350         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
351                   | HID_REQ_GET_PROTOCOL):
352                 VDBG(cdev, "get_protocol\n");
353                 goto stall;
354                 break;
355
356         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
357                   | HID_REQ_SET_REPORT):
358                 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
359                 req->context  = hidg;
360                 req->complete = hidg_set_report_complete;
361                 goto respond;
362                 break;
363
364         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
365                   | HID_REQ_SET_PROTOCOL):
366                 VDBG(cdev, "set_protocol\n");
367                 goto stall;
368                 break;
369
370         case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
371                   | USB_REQ_GET_DESCRIPTOR):
372                 switch (value >> 8) {
373                 case HID_DT_REPORT:
374                         VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
375                         length = min_t(unsigned short, length,
376                                                    hidg->report_desc_length);
377                         memcpy(req->buf, hidg->report_desc, length);
378                         goto respond;
379                         break;
380
381                 default:
382                         VDBG(cdev, "Unknown decriptor request 0x%x\n",
383                                  value >> 8);
384                         goto stall;
385                         break;
386                 }
387                 break;
388
389         default:
390                 VDBG(cdev, "Unknown request 0x%x\n",
391                          ctrl->bRequest);
392                 goto stall;
393                 break;
394         }
395
396 stall:
397         return -EOPNOTSUPP;
398
399 respond:
400         req->zero = 0;
401         req->length = length;
402         status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
403         if (status < 0)
404                 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
405         return status;
406 }
407
408 static void hidg_disable(struct usb_function *f)
409 {
410         struct f_hidg *hidg = func_to_hidg(f);
411
412         usb_ep_disable(hidg->in_ep);
413         hidg->in_ep->driver_data = NULL;
414 }
415
416 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
417 {
418         struct usb_composite_dev                *cdev = f->config->cdev;
419         struct f_hidg                           *hidg = func_to_hidg(f);
420         const struct usb_endpoint_descriptor    *ep_desc;
421         int status = 0;
422
423         VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
424
425         if (hidg->in_ep != NULL) {
426                 /* restart endpoint */
427                 if (hidg->in_ep->driver_data != NULL)
428                         usb_ep_disable(hidg->in_ep);
429
430                 ep_desc = ep_choose(f->config->cdev->gadget,
431                                 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
432                 status = usb_ep_enable(hidg->in_ep, ep_desc);
433                 if (status < 0) {
434                         ERROR(cdev, "Enable endpoint FAILED!\n");
435                         goto fail;
436                 }
437                 hidg->in_ep->driver_data = hidg;
438         }
439 fail:
440         return status;
441 }
442
443 const struct file_operations f_hidg_fops = {
444         .owner          = THIS_MODULE,
445         .open           = f_hidg_open,
446         .release        = f_hidg_release,
447         .write          = f_hidg_write,
448         .read           = f_hidg_read,
449         .poll           = f_hidg_poll,
450         .llseek         = noop_llseek,
451 };
452
453 static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
454 {
455         struct usb_ep           *ep;
456         struct f_hidg           *hidg = func_to_hidg(f);
457         int                     status;
458         dev_t                   dev;
459
460         /* allocate instance-specific interface IDs, and patch descriptors */
461         status = usb_interface_id(c, f);
462         if (status < 0)
463                 goto fail;
464         hidg_interface_desc.bInterfaceNumber = status;
465
466
467         /* allocate instance-specific endpoints */
468         status = -ENODEV;
469         ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
470         if (!ep)
471                 goto fail;
472         ep->driver_data = c->cdev;      /* claim */
473         hidg->in_ep = ep;
474
475         /* preallocate request and buffer */
476         status = -ENOMEM;
477         hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
478         if (!hidg->req)
479                 goto fail;
480
481
482         hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
483         if (!hidg->req->buf)
484                 goto fail;
485
486         /* set descriptor dynamic values */
487         hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
488         hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
489         hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
490         hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
491         hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
492         hidg_desc.desc[0].wDescriptorLength =
493                 cpu_to_le16(hidg->report_desc_length);
494
495         hidg->set_report_buff = NULL;
496
497         /* copy descriptors */
498         f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
499         if (!f->descriptors)
500                 goto fail;
501
502         hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
503                                                 f->descriptors,
504                                                 &hidg_fs_in_ep_desc);
505
506         if (gadget_is_dualspeed(c->cdev->gadget)) {
507                 hidg_hs_in_ep_desc.bEndpointAddress =
508                         hidg_fs_in_ep_desc.bEndpointAddress;
509                 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
510                 if (!f->hs_descriptors)
511                         goto fail;
512                 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
513                                                         f->hs_descriptors,
514                                                         &hidg_hs_in_ep_desc);
515         } else {
516                 hidg->hs_in_ep_desc = NULL;
517         }
518
519         mutex_init(&hidg->lock);
520         spin_lock_init(&hidg->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 }