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/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>
23 static int major, minors;
24 static struct class *hidg_class;
26 /*-------------------------------------------------------------------------*/
27 /* HID gadget struct */
31 unsigned char bInterfaceSubClass;
32 unsigned char bInterfaceProtocol;
33 unsigned short report_desc_length;
35 unsigned short report_length;
38 char *set_report_buff;
39 unsigned short set_report_length;
40 spinlock_t read_spinlock;
41 wait_queue_head_t read_queue;
44 spinlock_t write_spinlock;
46 wait_queue_head_t write_queue;
47 struct usb_request *req;
51 struct usb_function func;
55 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
57 return container_of(f, struct f_hidg, func);
60 /*-------------------------------------------------------------------------*/
61 /* Static descriptors */
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,
69 .bInterfaceClass = USB_CLASS_HID,
70 /* .bInterfaceSubClass = DYNAMIC */
71 /* .bInterfaceProtocol = DYNAMIC */
72 /* .iInterface = DYNAMIC */
75 static struct hid_descriptor hidg_desc = {
76 .bLength = sizeof hidg_desc,
77 .bDescriptorType = HID_DT_HID,
80 .bNumDescriptors = 0x1,
81 /*.desc[0].bDescriptorType = DYNAMIC */
82 /*.desc[0].wDescriptorLenght = DYNAMIC */
85 /* High-Speed Support */
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)
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,
106 /* Full-Speed Support */
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)
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,
127 /*-------------------------------------------------------------------------*/
130 static ssize_t f_hidg_read(struct file *file, char __user *buffer,
131 size_t count, loff_t *ptr)
133 struct f_hidg *hidg = file->private_data;
134 char *tmp_buff = NULL;
140 if (!access_ok(VERIFY_WRITE, buffer, count))
143 spin_lock_irqsave(&hidg->read_spinlock, flags);
145 #define READ_COND (hidg->set_report_buff != NULL)
148 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
149 if (file->f_flags & O_NONBLOCK)
152 if (wait_event_interruptible(hidg->read_queue, READ_COND))
155 spin_lock_irqsave(&hidg->read_spinlock, flags);
159 count = min_t(unsigned, count, hidg->set_report_length);
160 tmp_buff = hidg->set_report_buff;
161 hidg->set_report_buff = NULL;
163 spin_unlock_irqrestore(&hidg->read_spinlock, flags);
165 if (tmp_buff != NULL) {
166 /* copy to user outside spinlock */
167 count -= copy_to_user(buffer, tmp_buff, count);
175 static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
177 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
180 if (req->status != 0) {
181 ERROR(hidg->func.config->cdev,
182 "End Point Request ERROR: %d\n", req->status);
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);
191 static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t *offp)
194 struct f_hidg *hidg = file->private_data;
196 ssize_t status = -ENOMEM;
198 if (!access_ok(VERIFY_READ, buffer, count))
201 spin_lock_irqsave(&hidg->write_spinlock, flags);
203 #define WRITE_COND (!hidg->write_pending)
206 while (!WRITE_COND) {
207 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
208 if (file->f_flags & O_NONBLOCK)
211 if (wait_event_interruptible_exclusive(
212 hidg->write_queue, WRITE_COND))
215 spin_lock_irqsave(&hidg->write_spinlock, flags);
218 hidg->write_pending = 1;
219 count = min_t(unsigned, count, hidg->report_length);
221 spin_unlock_irqrestore(&hidg->write_spinlock, flags);
222 status = copy_from_user(hidg->req->buf, buffer, count);
225 ERROR(hidg->func.config->cdev,
226 "copy_from_user error\n");
228 goto release_write_pending;
231 hidg->req->status = 0;
233 hidg->req->length = count;
234 hidg->req->complete = f_hidg_req_complete;
235 hidg->req->context = hidg;
237 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
239 ERROR(hidg->func.config->cdev,
240 "usb_ep_queue error on int endpoint %zd\n", status);
241 goto release_write_pending;
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);
252 wake_up(&hidg->write_queue);
257 static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
259 struct f_hidg *hidg = file->private_data;
260 unsigned int ret = 0;
262 poll_wait(file, &hidg->read_queue, wait);
263 poll_wait(file, &hidg->write_queue, wait);
266 ret |= POLLOUT | POLLWRNORM;
269 ret |= POLLIN | POLLRDNORM;
277 static int f_hidg_release(struct inode *inode, struct file *fd)
279 fd->private_data = NULL;
283 static int f_hidg_open(struct inode *inode, struct file *fd)
285 struct f_hidg *hidg =
286 container_of(inode->i_cdev, struct f_hidg, cdev);
288 fd->private_data = hidg;
293 /*-------------------------------------------------------------------------*/
296 static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
298 struct f_hidg *hidg = (struct f_hidg *)req->context;
300 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
301 ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
305 spin_lock(&hidg->read_spinlock);
307 hidg->set_report_buff = krealloc(hidg->set_report_buff,
308 req->actual, GFP_ATOMIC);
310 if (hidg->set_report_buff == NULL) {
311 spin_unlock(&hidg->read_spinlock);
314 hidg->set_report_length = req->actual;
315 memcpy(hidg->set_report_buff, req->buf, req->actual);
317 spin_unlock(&hidg->read_spinlock);
319 wake_up(&hidg->read_queue);
322 static int hidg_setup(struct usb_function *f,
323 const struct usb_ctrlrequest *ctrl)
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;
331 value = __le16_to_cpu(ctrl->wValue);
332 length = __le16_to_cpu(ctrl->wLength);
334 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
335 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
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");
342 /* send an empty report */
343 length = min_t(unsigned, length, hidg->report_length);
344 memset(req->buf, 0x0, length);
349 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
350 | HID_REQ_GET_PROTOCOL):
351 VDBG(cdev, "get_protocol\n");
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);
359 req->complete = hidg_set_report_complete;
363 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
364 | HID_REQ_SET_PROTOCOL):
365 VDBG(cdev, "set_protocol\n");
369 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
370 | USB_REQ_GET_DESCRIPTOR):
371 switch (value >> 8) {
373 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
374 length = min_t(unsigned short, length,
376 memcpy(req->buf, &hidg_desc, length);
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);
388 VDBG(cdev, "Unknown decriptor request 0x%x\n",
396 VDBG(cdev, "Unknown request 0x%x\n",
407 req->length = length;
408 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
410 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
414 static void hidg_disable(struct usb_function *f)
416 struct f_hidg *hidg = func_to_hidg(f);
418 usb_ep_disable(hidg->in_ep);
419 hidg->in_ep->driver_data = NULL;
422 static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
424 struct usb_composite_dev *cdev = f->config->cdev;
425 struct f_hidg *hidg = func_to_hidg(f);
428 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
430 if (hidg->in_ep != NULL) {
431 /* restart endpoint */
432 if (hidg->in_ep->driver_data != NULL)
433 usb_ep_disable(hidg->in_ep);
435 status = config_ep_by_speed(f->config->cdev->gadget, f,
438 ERROR(cdev, "config_ep_by_speed FAILED!\n");
441 status = usb_ep_enable(hidg->in_ep);
443 ERROR(cdev, "Enable endpoint FAILED!\n");
446 hidg->in_ep->driver_data = hidg;
452 const struct file_operations f_hidg_fops = {
453 .owner = THIS_MODULE,
455 .release = f_hidg_release,
456 .write = f_hidg_write,
459 .llseek = noop_llseek,
462 static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
465 struct f_hidg *hidg = func_to_hidg(f);
469 /* allocate instance-specific interface IDs, and patch descriptors */
470 status = usb_interface_id(c, f);
473 hidg_interface_desc.bInterfaceNumber = status;
476 /* allocate instance-specific endpoints */
478 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
481 ep->driver_data = c->cdev; /* claim */
484 /* preallocate request and buffer */
486 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
491 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
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);
504 hidg->set_report_buff = NULL;
506 /* copy descriptors */
507 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
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)
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);
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);
531 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
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);
543 usb_free_descriptors(f->hs_descriptors);
544 usb_free_descriptors(f->descriptors);
549 static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
551 struct f_hidg *hidg = func_to_hidg(f);
553 device_destroy(hidg_class, MKDEV(major, hidg->minor));
554 cdev_del(&hidg->cdev);
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);
562 /* free descriptors copies */
563 usb_free_descriptors(f->hs_descriptors);
564 usb_free_descriptors(f->descriptors);
566 kfree(hidg->report_desc);
567 kfree(hidg->set_report_buff);
571 /*-------------------------------------------------------------------------*/
574 #define CT_FUNC_HID_IDX 0
576 static struct usb_string ct_func_string_defs[] = {
577 [CT_FUNC_HID_IDX].s = "HID Interface",
578 {}, /* end of list */
581 static struct usb_gadget_strings ct_func_string_table = {
582 .language = 0x0409, /* en-US */
583 .strings = ct_func_string_defs,
586 static struct usb_gadget_strings *ct_func_strings[] = {
587 &ct_func_string_table,
591 /*-------------------------------------------------------------------------*/
592 /* usb_configuration */
594 int __init hidg_bind_config(struct usb_configuration *c,
595 struct hidg_func_descriptor *fdesc, int index)
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);
608 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
609 hidg_interface_desc.iInterface = status;
612 /* allocate and initialize one new instance */
613 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
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,
625 if (!hidg->report_desc) {
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;
638 status = usb_add_function(c, &hidg->func);
645 int __init ghid_setup(struct usb_gadget *g, int count)
650 hidg_class = class_create(THIS_MODULE, "hidg");
652 status = alloc_chrdev_region(&dev, 0, count, "hidg");
661 void ghid_cleanup(void)
664 unregister_chrdev_region(MKDEV(major, 0), minors);
668 class_destroy(hidg_class);