2 * f_hid.c -- USB HID function driver
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/hid.h>
15 #include <linux/cdev.h>
16 #include <linux/mutex.h>
17 #include <linux/poll.h>
18 #include <linux/uaccess.h>
19 #include <linux/wait.h>
20 #include <linux/sched.h>
21 #include <linux/usb/g_hid.h>
23 static int major, minors;
24 static struct class *hidg_class;
26 /*-------------------------------------------------------------------------*/
27 /* HID gadget struct */
29 struct f_hidg_req_list {
30 struct usb_request *req;
32 struct list_head list;
37 unsigned char bInterfaceSubClass;
38 unsigned char bInterfaceProtocol;
39 unsigned short report_desc_length;
41 unsigned short report_length;
44 struct list_head completed_out_req;
46 wait_queue_head_t read_queue;
52 wait_queue_head_t write_queue;
53 struct usb_request *req;
57 struct usb_function func;
60 struct usb_ep *out_ep;
63 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
65 return container_of(f, struct f_hidg, func);
68 /*-------------------------------------------------------------------------*/
69 /* Static descriptors */
71 static struct usb_interface_descriptor hidg_interface_desc = {
72 .bLength = sizeof hidg_interface_desc,
73 .bDescriptorType = USB_DT_INTERFACE,
74 /* .bInterfaceNumber = DYNAMIC */
75 .bAlternateSetting = 0,
77 .bInterfaceClass = USB_CLASS_HID,
78 /* .bInterfaceSubClass = DYNAMIC */
79 /* .bInterfaceProtocol = DYNAMIC */
80 /* .iInterface = DYNAMIC */
83 static struct hid_descriptor hidg_desc = {
84 .bLength = sizeof hidg_desc,
85 .bDescriptorType = HID_DT_HID,
88 .bNumDescriptors = 0x1,
89 /*.desc[0].bDescriptorType = DYNAMIC */
90 /*.desc[0].wDescriptorLenght = DYNAMIC */
93 /* High-Speed Support */
95 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = USB_DIR_IN,
99 .bmAttributes = USB_ENDPOINT_XFER_INT,
100 /*.wMaxPacketSize = DYNAMIC */
101 .bInterval = 4, /* FIXME: Add this field in the
102 * HID gadget configuration?
103 * (struct hidg_func_descriptor)
107 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
108 .bLength = USB_DT_ENDPOINT_SIZE,
109 .bDescriptorType = USB_DT_ENDPOINT,
110 .bEndpointAddress = USB_DIR_OUT,
111 .bmAttributes = USB_ENDPOINT_XFER_INT,
112 /*.wMaxPacketSize = DYNAMIC */
113 .bInterval = 4, /* FIXME: Add this field in the
114 * HID gadget configuration?
115 * (struct hidg_func_descriptor)
119 static struct usb_descriptor_header *hidg_hs_descriptors[] = {
120 (struct usb_descriptor_header *)&hidg_interface_desc,
121 (struct usb_descriptor_header *)&hidg_desc,
122 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
123 (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
127 /* Full-Speed Support */
129 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132 .bEndpointAddress = USB_DIR_IN,
133 .bmAttributes = USB_ENDPOINT_XFER_INT,
134 /*.wMaxPacketSize = DYNAMIC */
135 .bInterval = 10, /* FIXME: Add this field in the
136 * HID gadget configuration?
137 * (struct hidg_func_descriptor)
141 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_OUT,
145 .bmAttributes = USB_ENDPOINT_XFER_INT,
146 /*.wMaxPacketSize = DYNAMIC */
147 .bInterval = 10, /* FIXME: Add this field in the
148 * HID gadget configuration?
149 * (struct hidg_func_descriptor)
153 static struct usb_descriptor_header *hidg_fs_descriptors[] = {
154 (struct usb_descriptor_header *)&hidg_interface_desc,
155 (struct usb_descriptor_header *)&hidg_desc,
156 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
157 (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
161 /*-------------------------------------------------------------------------*/
164 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
165 size_t count, loff_t *ptr)
167 struct f_hidg *hidg = file->private_data;
168 struct f_hidg_req_list *list;
169 struct usb_request *req;
176 if (!access_ok(VERIFY_WRITE, buffer, count))
179 spin_lock_irqsave(&hidg->spinlock, flags);
181 #define READ_COND (!list_empty(&hidg->completed_out_req))
183 /* wait for at least one buffer to complete */
185 spin_unlock_irqrestore(&hidg->spinlock, flags);
186 if (file->f_flags & O_NONBLOCK)
189 if (wait_event_interruptible(hidg->read_queue, READ_COND))
192 spin_lock_irqsave(&hidg->spinlock, flags);
195 /* pick the first one */
196 list = list_first_entry(&hidg->completed_out_req,
197 struct f_hidg_req_list, list);
199 count = min_t(unsigned int, count, req->actual - list->pos);
200 spin_unlock_irqrestore(&hidg->spinlock, flags);
202 /* copy to user outside spinlock */
203 count -= copy_to_user(buffer, req->buf + list->pos, count);
207 * if this request is completely handled and transfered to
208 * userspace, remove its entry from the list and requeue it
209 * again. Otherwise, we will revisit it again upon the next
210 * call, taking into account its current read position.
212 if (list->pos == req->actual) {
213 spin_lock_irqsave(&hidg->spinlock, flags);
214 list_del(&list->list);
216 spin_unlock_irqrestore(&hidg->spinlock, flags);
218 req->length = hidg->report_length;
219 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
227 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
229 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
231 if (req->status != 0) {
232 ERROR(hidg->func.config->cdev,
233 "End Point Request ERROR: %d\n", req->status);
236 hidg->write_pending = 0;
237 wake_up(&hidg->write_queue);
240 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
241 size_t count, loff_t *offp)
243 struct f_hidg *hidg = file->private_data;
244 ssize_t status = -ENOMEM;
246 if (!access_ok(VERIFY_READ, buffer, count))
249 mutex_lock(&hidg->lock);
251 #define WRITE_COND (!hidg->write_pending)
254 while (!WRITE_COND) {
255 mutex_unlock(&hidg->lock);
256 if (file->f_flags & O_NONBLOCK)
259 if (wait_event_interruptible_exclusive(
260 hidg->write_queue, WRITE_COND))
263 mutex_lock(&hidg->lock);
266 count = min_t(unsigned, count, hidg->report_length);
267 status = copy_from_user(hidg->req->buf, buffer, count);
270 ERROR(hidg->func.config->cdev,
271 "copy_from_user error\n");
272 mutex_unlock(&hidg->lock);
276 hidg->req->status = 0;
278 hidg->req->length = count;
279 hidg->req->complete = f_hidg_req_complete;
280 hidg->req->context = hidg;
281 hidg->write_pending = 1;
283 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
285 ERROR(hidg->func.config->cdev,
286 "usb_ep_queue error on int endpoint %zd\n", status);
287 hidg->write_pending = 0;
288 wake_up(&hidg->write_queue);
293 mutex_unlock(&hidg->lock);
298 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
300 struct f_hidg *hidg = file->private_data;
301 unsigned int ret = 0;
303 poll_wait(file, &hidg->read_queue, wait);
304 poll_wait(file, &hidg->write_queue, wait);
307 ret |= POLLOUT | POLLWRNORM;
310 ret |= POLLIN | POLLRDNORM;
318 static int f_hidg_release(struct inode *inode, struct file *fd)
320 fd->private_data = NULL;
324 static int f_hidg_open(struct inode *inode, struct file *fd)
326 struct f_hidg *hidg =
327 container_of(inode->i_cdev, struct f_hidg, cdev);
329 fd->private_data = hidg;
334 /*-------------------------------------------------------------------------*/
337 static struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep, unsigned length)
339 struct usb_request *req;
341 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
343 req->length = length;
344 req->buf = kmalloc(length, GFP_ATOMIC);
346 usb_ep_free_request(ep, req);
353 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
355 struct f_hidg *hidg = (struct f_hidg *) req->context;
356 struct f_hidg_req_list *req_list;
359 req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
365 spin_lock_irqsave(&hidg->spinlock, flags);
366 list_add_tail(&req_list->list, &hidg->completed_out_req);
367 spin_unlock_irqrestore(&hidg->spinlock, flags);
369 wake_up(&hidg->read_queue);
372 static int hidg_setup(struct usb_function *f,
373 const struct usb_ctrlrequest *ctrl)
375 struct f_hidg *hidg = func_to_hidg(f);
376 struct usb_composite_dev *cdev = f->config->cdev;
377 struct usb_request *req = cdev->req;
381 value = __le16_to_cpu(ctrl->wValue);
382 length = __le16_to_cpu(ctrl->wLength);
384 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
385 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
387 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
388 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
389 | HID_REQ_GET_REPORT):
390 VDBG(cdev, "get_report\n");
392 /* send an empty report */
393 length = min_t(unsigned, length, hidg->report_length);
394 memset(req->buf, 0x0, length);
399 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
400 | HID_REQ_GET_PROTOCOL):
401 VDBG(cdev, "get_protocol\n");
405 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
406 | HID_REQ_SET_REPORT):
407 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
411 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
412 | HID_REQ_SET_PROTOCOL):
413 VDBG(cdev, "set_protocol\n");
417 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
418 | USB_REQ_GET_DESCRIPTOR):
419 switch (value >> 8) {
421 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
422 length = min_t(unsigned short, length,
424 memcpy(req->buf, &hidg_desc, length);
428 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
429 length = min_t(unsigned short, length,
430 hidg->report_desc_length);
431 memcpy(req->buf, hidg->report_desc, length);
436 VDBG(cdev, "Unknown descriptor request 0x%x\n",
444 VDBG(cdev, "Unknown request 0x%x\n",
455 req->length = length;
456 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
458 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
462 static void hidg_disable(struct usb_function *f)
464 struct f_hidg *hidg = func_to_hidg(f);
465 struct f_hidg_req_list *list, *next;
467 usb_ep_disable(hidg->in_ep);
468 hidg->in_ep->driver_data = NULL;
470 usb_ep_disable(hidg->out_ep);
471 hidg->out_ep->driver_data = NULL;
473 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
474 list_del(&list->list);
479 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
481 struct usb_composite_dev *cdev = f->config->cdev;
482 struct f_hidg *hidg = func_to_hidg(f);
485 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
487 if (hidg->in_ep != NULL) {
488 /* restart endpoint */
489 if (hidg->in_ep->driver_data != NULL)
490 usb_ep_disable(hidg->in_ep);
492 status = config_ep_by_speed(f->config->cdev->gadget, f,
495 ERROR(cdev, "config_ep_by_speed FAILED!\n");
498 status = usb_ep_enable(hidg->in_ep);
500 ERROR(cdev, "Enable IN endpoint FAILED!\n");
503 hidg->in_ep->driver_data = hidg;
507 if (hidg->out_ep != NULL) {
508 /* restart endpoint */
509 if (hidg->out_ep->driver_data != NULL)
510 usb_ep_disable(hidg->out_ep);
512 status = config_ep_by_speed(f->config->cdev->gadget, f,
515 ERROR(cdev, "config_ep_by_speed FAILED!\n");
518 status = usb_ep_enable(hidg->out_ep);
520 ERROR(cdev, "Enable IN endpoint FAILED!\n");
523 hidg->out_ep->driver_data = hidg;
526 * allocate a bunch of read buffers and queue them all at once.
528 for (i = 0; i < hidg->qlen && status == 0; i++) {
529 struct usb_request *req =
530 hidg_alloc_ep_req(hidg->out_ep,
531 hidg->report_length);
533 req->complete = hidg_set_report_complete;
535 status = usb_ep_queue(hidg->out_ep, req,
538 ERROR(cdev, "%s queue req --> %d\n",
539 hidg->out_ep->name, status);
541 usb_ep_disable(hidg->out_ep);
542 hidg->out_ep->driver_data = NULL;
553 const struct file_operations f_hidg_fops = {
554 .owner = THIS_MODULE,
556 .release = f_hidg_release,
557 .write = f_hidg_write,
560 .llseek = noop_llseek,
563 static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
566 struct f_hidg *hidg = func_to_hidg(f);
570 /* allocate instance-specific interface IDs, and patch descriptors */
571 status = usb_interface_id(c, f);
574 hidg_interface_desc.bInterfaceNumber = status;
576 /* allocate instance-specific endpoints */
578 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
581 ep->driver_data = c->cdev; /* claim */
584 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
587 ep->driver_data = c->cdev; /* claim */
590 /* preallocate request and buffer */
592 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
596 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
600 /* set descriptor dynamic values */
601 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
602 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
603 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
604 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
605 hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
606 hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
607 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
608 hidg_desc.desc[0].wDescriptorLength =
609 cpu_to_le16(hidg->report_desc_length);
611 hidg_hs_in_ep_desc.bEndpointAddress =
612 hidg_fs_in_ep_desc.bEndpointAddress;
613 hidg_hs_out_ep_desc.bEndpointAddress =
614 hidg_fs_out_ep_desc.bEndpointAddress;
616 status = usb_assign_descriptors(f, hidg_fs_descriptors,
617 hidg_hs_descriptors, NULL);
621 mutex_init(&hidg->lock);
622 spin_lock_init(&hidg->spinlock);
623 init_waitqueue_head(&hidg->write_queue);
624 init_waitqueue_head(&hidg->read_queue);
625 INIT_LIST_HEAD(&hidg->completed_out_req);
627 /* create char device */
628 cdev_init(&hidg->cdev, &f_hidg_fops);
629 dev = MKDEV(major, hidg->minor);
630 status = cdev_add(&hidg->cdev, dev, 1);
634 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
639 ERROR(f->config->cdev, "hidg_bind FAILED\n");
640 if (hidg->req != NULL) {
641 kfree(hidg->req->buf);
642 if (hidg->in_ep != NULL)
643 usb_ep_free_request(hidg->in_ep, hidg->req);
646 usb_free_all_descriptors(f);
650 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
652 struct f_hidg *hidg = func_to_hidg(f);
654 device_destroy(hidg_class, MKDEV(major, hidg->minor));
655 cdev_del(&hidg->cdev);
657 /* disable/free request and end point */
658 usb_ep_disable(hidg->in_ep);
659 usb_ep_dequeue(hidg->in_ep, hidg->req);
660 kfree(hidg->req->buf);
661 usb_ep_free_request(hidg->in_ep, hidg->req);
663 usb_free_all_descriptors(f);
665 kfree(hidg->report_desc);
669 /*-------------------------------------------------------------------------*/
672 #define CT_FUNC_HID_IDX 0
674 static struct usb_string ct_func_string_defs[] = {
675 [CT_FUNC_HID_IDX].s = "HID Interface",
676 {}, /* end of list */
679 static struct usb_gadget_strings ct_func_string_table = {
680 .language = 0x0409, /* en-US */
681 .strings = ct_func_string_defs,
684 static struct usb_gadget_strings *ct_func_strings[] = {
685 &ct_func_string_table,
689 /*-------------------------------------------------------------------------*/
690 /* usb_configuration */
692 int __init hidg_bind_config(struct usb_configuration *c,
693 struct hidg_func_descriptor *fdesc, int index)
701 /* maybe allocate device-global string IDs, and patch descriptors */
702 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
703 status = usb_string_id(c->cdev);
706 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
707 hidg_interface_desc.iInterface = status;
710 /* allocate and initialize one new instance */
711 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
716 hidg->bInterfaceSubClass = fdesc->subclass;
717 hidg->bInterfaceProtocol = fdesc->protocol;
718 hidg->report_length = fdesc->report_length;
719 hidg->report_desc_length = fdesc->report_desc_length;
720 hidg->report_desc = kmemdup(fdesc->report_desc,
721 fdesc->report_desc_length,
723 if (!hidg->report_desc) {
728 hidg->func.name = "hid";
729 hidg->func.strings = ct_func_strings;
730 hidg->func.bind = hidg_bind;
731 hidg->func.unbind = hidg_unbind;
732 hidg->func.set_alt = hidg_set_alt;
733 hidg->func.disable = hidg_disable;
734 hidg->func.setup = hidg_setup;
736 /* this could me made configurable at some point */
739 status = usb_add_function(c, &hidg->func);
746 int __init ghid_setup(struct usb_gadget *g, int count)
751 hidg_class = class_create(THIS_MODULE, "hidg");
753 status = alloc_chrdev_region(&dev, 0, count, "hidg");
762 void ghid_cleanup(void)
765 unregister_chrdev_region(MKDEV(major, 0), minors);
769 class_destroy(hidg_class);