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>
25 static int major
, minors
;
26 static struct class *hidg_class
;
28 /*-------------------------------------------------------------------------*/
29 /* HID gadget struct */
31 struct f_hidg_req_list
{
32 struct usb_request
*req
;
34 struct list_head list
;
39 unsigned char bInterfaceSubClass
;
40 unsigned char bInterfaceProtocol
;
41 unsigned short report_desc_length
;
43 unsigned short report_length
;
46 struct list_head completed_out_req
;
48 wait_queue_head_t read_queue
;
54 wait_queue_head_t write_queue
;
55 struct usb_request
*req
;
59 struct usb_function func
;
62 struct usb_ep
*out_ep
;
65 static inline struct f_hidg
*func_to_hidg(struct usb_function
*f
)
67 return container_of(f
, struct f_hidg
, func
);
70 /*-------------------------------------------------------------------------*/
71 /* Static descriptors */
73 static struct usb_interface_descriptor hidg_interface_desc
= {
74 .bLength
= sizeof hidg_interface_desc
,
75 .bDescriptorType
= USB_DT_INTERFACE
,
76 /* .bInterfaceNumber = DYNAMIC */
77 .bAlternateSetting
= 0,
79 .bInterfaceClass
= USB_CLASS_HID
,
80 /* .bInterfaceSubClass = DYNAMIC */
81 /* .bInterfaceProtocol = DYNAMIC */
82 /* .iInterface = DYNAMIC */
85 static struct hid_descriptor hidg_desc
= {
86 .bLength
= sizeof hidg_desc
,
87 .bDescriptorType
= HID_DT_HID
,
90 .bNumDescriptors
= 0x1,
91 /*.desc[0].bDescriptorType = DYNAMIC */
92 /*.desc[0].wDescriptorLenght = DYNAMIC */
95 /* High-Speed Support */
97 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc
= {
98 .bLength
= USB_DT_ENDPOINT_SIZE
,
99 .bDescriptorType
= USB_DT_ENDPOINT
,
100 .bEndpointAddress
= USB_DIR_IN
,
101 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
102 /*.wMaxPacketSize = DYNAMIC */
103 .bInterval
= 4, /* FIXME: Add this field in the
104 * HID gadget configuration?
105 * (struct hidg_func_descriptor)
109 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc
= {
110 .bLength
= USB_DT_ENDPOINT_SIZE
,
111 .bDescriptorType
= USB_DT_ENDPOINT
,
112 .bEndpointAddress
= USB_DIR_OUT
,
113 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
114 /*.wMaxPacketSize = DYNAMIC */
115 .bInterval
= 4, /* FIXME: Add this field in the
116 * HID gadget configuration?
117 * (struct hidg_func_descriptor)
121 static struct usb_descriptor_header
*hidg_hs_descriptors
[] = {
122 (struct usb_descriptor_header
*)&hidg_interface_desc
,
123 (struct usb_descriptor_header
*)&hidg_desc
,
124 (struct usb_descriptor_header
*)&hidg_hs_in_ep_desc
,
125 (struct usb_descriptor_header
*)&hidg_hs_out_ep_desc
,
129 /* Full-Speed Support */
131 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc
= {
132 .bLength
= USB_DT_ENDPOINT_SIZE
,
133 .bDescriptorType
= USB_DT_ENDPOINT
,
134 .bEndpointAddress
= USB_DIR_IN
,
135 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
136 /*.wMaxPacketSize = DYNAMIC */
137 .bInterval
= 10, /* FIXME: Add this field in the
138 * HID gadget configuration?
139 * (struct hidg_func_descriptor)
143 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc
= {
144 .bLength
= USB_DT_ENDPOINT_SIZE
,
145 .bDescriptorType
= USB_DT_ENDPOINT
,
146 .bEndpointAddress
= USB_DIR_OUT
,
147 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
148 /*.wMaxPacketSize = DYNAMIC */
149 .bInterval
= 10, /* FIXME: Add this field in the
150 * HID gadget configuration?
151 * (struct hidg_func_descriptor)
155 static struct usb_descriptor_header
*hidg_fs_descriptors
[] = {
156 (struct usb_descriptor_header
*)&hidg_interface_desc
,
157 (struct usb_descriptor_header
*)&hidg_desc
,
158 (struct usb_descriptor_header
*)&hidg_fs_in_ep_desc
,
159 (struct usb_descriptor_header
*)&hidg_fs_out_ep_desc
,
163 /*-------------------------------------------------------------------------*/
166 static ssize_t
f_hidg_read(struct file
*file
, char __user
*buffer
,
167 size_t count
, loff_t
*ptr
)
169 struct f_hidg
*hidg
= file
->private_data
;
170 struct f_hidg_req_list
*list
;
171 struct usb_request
*req
;
178 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
181 spin_lock_irqsave(&hidg
->spinlock
, flags
);
183 #define READ_COND (!list_empty(&hidg->completed_out_req))
185 /* wait for at least one buffer to complete */
187 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
188 if (file
->f_flags
& O_NONBLOCK
)
191 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
194 spin_lock_irqsave(&hidg
->spinlock
, flags
);
197 /* pick the first one */
198 list
= list_first_entry(&hidg
->completed_out_req
,
199 struct f_hidg_req_list
, list
);
201 count
= min_t(unsigned int, count
, req
->actual
- list
->pos
);
202 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
204 /* copy to user outside spinlock */
205 count
-= copy_to_user(buffer
, req
->buf
+ list
->pos
, count
);
209 * if this request is completely handled and transfered to
210 * userspace, remove its entry from the list and requeue it
211 * again. Otherwise, we will revisit it again upon the next
212 * call, taking into account its current read position.
214 if (list
->pos
== req
->actual
) {
215 spin_lock_irqsave(&hidg
->spinlock
, flags
);
216 list_del(&list
->list
);
218 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
220 req
->length
= hidg
->report_length
;
221 ret
= usb_ep_queue(hidg
->out_ep
, req
, GFP_KERNEL
);
229 static void f_hidg_req_complete(struct usb_ep
*ep
, struct usb_request
*req
)
231 struct f_hidg
*hidg
= (struct f_hidg
*)ep
->driver_data
;
233 if (req
->status
!= 0) {
234 ERROR(hidg
->func
.config
->cdev
,
235 "End Point Request ERROR: %d\n", req
->status
);
238 hidg
->write_pending
= 0;
239 wake_up(&hidg
->write_queue
);
242 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
243 size_t count
, loff_t
*offp
)
245 struct f_hidg
*hidg
= file
->private_data
;
246 ssize_t status
= -ENOMEM
;
248 if (!access_ok(VERIFY_READ
, buffer
, count
))
251 mutex_lock(&hidg
->lock
);
253 #define WRITE_COND (!hidg->write_pending)
256 while (!WRITE_COND
) {
257 mutex_unlock(&hidg
->lock
);
258 if (file
->f_flags
& O_NONBLOCK
)
261 if (wait_event_interruptible_exclusive(
262 hidg
->write_queue
, WRITE_COND
))
265 mutex_lock(&hidg
->lock
);
268 count
= min_t(unsigned, count
, hidg
->report_length
);
269 status
= copy_from_user(hidg
->req
->buf
, buffer
, count
);
272 ERROR(hidg
->func
.config
->cdev
,
273 "copy_from_user error\n");
274 mutex_unlock(&hidg
->lock
);
278 hidg
->req
->status
= 0;
280 hidg
->req
->length
= count
;
281 hidg
->req
->complete
= f_hidg_req_complete
;
282 hidg
->req
->context
= hidg
;
283 hidg
->write_pending
= 1;
285 status
= usb_ep_queue(hidg
->in_ep
, hidg
->req
, GFP_ATOMIC
);
287 ERROR(hidg
->func
.config
->cdev
,
288 "usb_ep_queue error on int endpoint %zd\n", status
);
289 hidg
->write_pending
= 0;
290 wake_up(&hidg
->write_queue
);
295 mutex_unlock(&hidg
->lock
);
300 static unsigned int f_hidg_poll(struct file
*file
, poll_table
*wait
)
302 struct f_hidg
*hidg
= file
->private_data
;
303 unsigned int ret
= 0;
305 poll_wait(file
, &hidg
->read_queue
, wait
);
306 poll_wait(file
, &hidg
->write_queue
, wait
);
309 ret
|= POLLOUT
| POLLWRNORM
;
312 ret
|= POLLIN
| POLLRDNORM
;
320 static int f_hidg_release(struct inode
*inode
, struct file
*fd
)
322 fd
->private_data
= NULL
;
326 static int f_hidg_open(struct inode
*inode
, struct file
*fd
)
328 struct f_hidg
*hidg
=
329 container_of(inode
->i_cdev
, struct f_hidg
, cdev
);
331 fd
->private_data
= hidg
;
336 /*-------------------------------------------------------------------------*/
339 static inline struct usb_request
*hidg_alloc_ep_req(struct usb_ep
*ep
,
342 return alloc_ep_req(ep
, length
, length
);
345 static void hidg_set_report_complete(struct usb_ep
*ep
, struct usb_request
*req
)
347 struct f_hidg
*hidg
= (struct f_hidg
*) req
->context
;
348 struct f_hidg_req_list
*req_list
;
351 req_list
= kzalloc(sizeof(*req_list
), GFP_ATOMIC
);
357 spin_lock_irqsave(&hidg
->spinlock
, flags
);
358 list_add_tail(&req_list
->list
, &hidg
->completed_out_req
);
359 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
361 wake_up(&hidg
->read_queue
);
364 static int hidg_setup(struct usb_function
*f
,
365 const struct usb_ctrlrequest
*ctrl
)
367 struct f_hidg
*hidg
= func_to_hidg(f
);
368 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
369 struct usb_request
*req
= cdev
->req
;
373 value
= __le16_to_cpu(ctrl
->wValue
);
374 length
= __le16_to_cpu(ctrl
->wLength
);
376 VDBG(cdev
, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
377 "Value:0x%x\n", ctrl
->bRequestType
, ctrl
->bRequest
, value
);
379 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
380 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
381 | HID_REQ_GET_REPORT
):
382 VDBG(cdev
, "get_report\n");
384 /* send an empty report */
385 length
= min_t(unsigned, length
, hidg
->report_length
);
386 memset(req
->buf
, 0x0, length
);
391 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
392 | HID_REQ_GET_PROTOCOL
):
393 VDBG(cdev
, "get_protocol\n");
397 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
398 | HID_REQ_SET_REPORT
):
399 VDBG(cdev
, "set_report | wLenght=%d\n", ctrl
->wLength
);
403 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
404 | HID_REQ_SET_PROTOCOL
):
405 VDBG(cdev
, "set_protocol\n");
409 case ((USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_INTERFACE
) << 8
410 | USB_REQ_GET_DESCRIPTOR
):
411 switch (value
>> 8) {
413 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: HID\n");
414 length
= min_t(unsigned short, length
,
416 memcpy(req
->buf
, &hidg_desc
, length
);
420 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
421 length
= min_t(unsigned short, length
,
422 hidg
->report_desc_length
);
423 memcpy(req
->buf
, hidg
->report_desc
, length
);
428 VDBG(cdev
, "Unknown descriptor request 0x%x\n",
436 VDBG(cdev
, "Unknown request 0x%x\n",
447 req
->length
= length
;
448 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
450 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
454 static void hidg_disable(struct usb_function
*f
)
456 struct f_hidg
*hidg
= func_to_hidg(f
);
457 struct f_hidg_req_list
*list
, *next
;
459 usb_ep_disable(hidg
->in_ep
);
460 hidg
->in_ep
->driver_data
= NULL
;
462 usb_ep_disable(hidg
->out_ep
);
463 hidg
->out_ep
->driver_data
= NULL
;
465 list_for_each_entry_safe(list
, next
, &hidg
->completed_out_req
, list
) {
466 list_del(&list
->list
);
471 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
473 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
474 struct f_hidg
*hidg
= func_to_hidg(f
);
477 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
479 if (hidg
->in_ep
!= NULL
) {
480 /* restart endpoint */
481 if (hidg
->in_ep
->driver_data
!= NULL
)
482 usb_ep_disable(hidg
->in_ep
);
484 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
487 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
490 status
= usb_ep_enable(hidg
->in_ep
);
492 ERROR(cdev
, "Enable IN endpoint FAILED!\n");
495 hidg
->in_ep
->driver_data
= hidg
;
499 if (hidg
->out_ep
!= NULL
) {
500 /* restart endpoint */
501 if (hidg
->out_ep
->driver_data
!= NULL
)
502 usb_ep_disable(hidg
->out_ep
);
504 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
507 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
510 status
= usb_ep_enable(hidg
->out_ep
);
512 ERROR(cdev
, "Enable IN endpoint FAILED!\n");
515 hidg
->out_ep
->driver_data
= hidg
;
518 * allocate a bunch of read buffers and queue them all at once.
520 for (i
= 0; i
< hidg
->qlen
&& status
== 0; i
++) {
521 struct usb_request
*req
=
522 hidg_alloc_ep_req(hidg
->out_ep
,
523 hidg
->report_length
);
525 req
->complete
= hidg_set_report_complete
;
527 status
= usb_ep_queue(hidg
->out_ep
, req
,
530 ERROR(cdev
, "%s queue req --> %d\n",
531 hidg
->out_ep
->name
, status
);
533 usb_ep_disable(hidg
->out_ep
);
534 hidg
->out_ep
->driver_data
= NULL
;
545 const struct file_operations f_hidg_fops
= {
546 .owner
= THIS_MODULE
,
548 .release
= f_hidg_release
,
549 .write
= f_hidg_write
,
552 .llseek
= noop_llseek
,
555 static int __init
hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
558 struct f_hidg
*hidg
= func_to_hidg(f
);
562 /* allocate instance-specific interface IDs, and patch descriptors */
563 status
= usb_interface_id(c
, f
);
566 hidg_interface_desc
.bInterfaceNumber
= status
;
568 /* allocate instance-specific endpoints */
570 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
573 ep
->driver_data
= c
->cdev
; /* claim */
576 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_out_ep_desc
);
579 ep
->driver_data
= c
->cdev
; /* claim */
582 /* preallocate request and buffer */
584 hidg
->req
= usb_ep_alloc_request(hidg
->in_ep
, GFP_KERNEL
);
588 hidg
->req
->buf
= kmalloc(hidg
->report_length
, GFP_KERNEL
);
592 /* set descriptor dynamic values */
593 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
594 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
595 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
596 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
597 hidg_hs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
598 hidg_fs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
599 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
600 hidg_desc
.desc
[0].wDescriptorLength
=
601 cpu_to_le16(hidg
->report_desc_length
);
603 hidg_hs_in_ep_desc
.bEndpointAddress
=
604 hidg_fs_in_ep_desc
.bEndpointAddress
;
605 hidg_hs_out_ep_desc
.bEndpointAddress
=
606 hidg_fs_out_ep_desc
.bEndpointAddress
;
608 status
= usb_assign_descriptors(f
, hidg_fs_descriptors
,
609 hidg_hs_descriptors
, NULL
);
613 mutex_init(&hidg
->lock
);
614 spin_lock_init(&hidg
->spinlock
);
615 init_waitqueue_head(&hidg
->write_queue
);
616 init_waitqueue_head(&hidg
->read_queue
);
617 INIT_LIST_HEAD(&hidg
->completed_out_req
);
619 /* create char device */
620 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
621 dev
= MKDEV(major
, hidg
->minor
);
622 status
= cdev_add(&hidg
->cdev
, dev
, 1);
626 device_create(hidg_class
, NULL
, dev
, NULL
, "%s%d", "hidg", hidg
->minor
);
631 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
632 if (hidg
->req
!= NULL
) {
633 kfree(hidg
->req
->buf
);
634 if (hidg
->in_ep
!= NULL
)
635 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
638 usb_free_all_descriptors(f
);
642 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
644 struct f_hidg
*hidg
= func_to_hidg(f
);
646 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
647 cdev_del(&hidg
->cdev
);
649 /* disable/free request and end point */
650 usb_ep_disable(hidg
->in_ep
);
651 usb_ep_dequeue(hidg
->in_ep
, hidg
->req
);
652 kfree(hidg
->req
->buf
);
653 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
655 usb_free_all_descriptors(f
);
657 kfree(hidg
->report_desc
);
661 /*-------------------------------------------------------------------------*/
664 #define CT_FUNC_HID_IDX 0
666 static struct usb_string ct_func_string_defs
[] = {
667 [CT_FUNC_HID_IDX
].s
= "HID Interface",
668 {}, /* end of list */
671 static struct usb_gadget_strings ct_func_string_table
= {
672 .language
= 0x0409, /* en-US */
673 .strings
= ct_func_string_defs
,
676 static struct usb_gadget_strings
*ct_func_strings
[] = {
677 &ct_func_string_table
,
681 /*-------------------------------------------------------------------------*/
682 /* usb_configuration */
684 int __init
hidg_bind_config(struct usb_configuration
*c
,
685 struct hidg_func_descriptor
*fdesc
, int index
)
693 /* maybe allocate device-global string IDs, and patch descriptors */
694 if (ct_func_string_defs
[CT_FUNC_HID_IDX
].id
== 0) {
695 status
= usb_string_id(c
->cdev
);
698 ct_func_string_defs
[CT_FUNC_HID_IDX
].id
= status
;
699 hidg_interface_desc
.iInterface
= status
;
702 /* allocate and initialize one new instance */
703 hidg
= kzalloc(sizeof *hidg
, GFP_KERNEL
);
708 hidg
->bInterfaceSubClass
= fdesc
->subclass
;
709 hidg
->bInterfaceProtocol
= fdesc
->protocol
;
710 hidg
->report_length
= fdesc
->report_length
;
711 hidg
->report_desc_length
= fdesc
->report_desc_length
;
712 hidg
->report_desc
= kmemdup(fdesc
->report_desc
,
713 fdesc
->report_desc_length
,
715 if (!hidg
->report_desc
) {
720 hidg
->func
.name
= "hid";
721 hidg
->func
.strings
= ct_func_strings
;
722 hidg
->func
.bind
= hidg_bind
;
723 hidg
->func
.unbind
= hidg_unbind
;
724 hidg
->func
.set_alt
= hidg_set_alt
;
725 hidg
->func
.disable
= hidg_disable
;
726 hidg
->func
.setup
= hidg_setup
;
728 /* this could me made configurable at some point */
731 status
= usb_add_function(c
, &hidg
->func
);
738 int __init
ghid_setup(struct usb_gadget
*g
, int count
)
743 hidg_class
= class_create(THIS_MODULE
, "hidg");
745 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
754 void ghid_cleanup(void)
757 unregister_chrdev_region(MKDEV(major
, 0), minors
);
761 class_destroy(hidg_class
);