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
;
41 wait_queue_head_t read_queue
;
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
->spinlock
, flags
);
145 #define READ_COND (hidg->set_report_buff != NULL)
148 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
149 if (file
->f_flags
& O_NONBLOCK
)
152 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
155 spin_lock_irqsave(&hidg
->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
->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
;
179 if (req
->status
!= 0) {
180 ERROR(hidg
->func
.config
->cdev
,
181 "End Point Request ERROR: %d\n", req
->status
);
184 hidg
->write_pending
= 0;
185 wake_up(&hidg
->write_queue
);
188 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
189 size_t count
, loff_t
*offp
)
191 struct f_hidg
*hidg
= file
->private_data
;
192 ssize_t status
= -ENOMEM
;
194 if (!access_ok(VERIFY_READ
, buffer
, count
))
197 mutex_lock(&hidg
->lock
);
199 #define WRITE_COND (!hidg->write_pending)
202 while (!WRITE_COND
) {
203 mutex_unlock(&hidg
->lock
);
204 if (file
->f_flags
& O_NONBLOCK
)
207 if (wait_event_interruptible_exclusive(
208 hidg
->write_queue
, WRITE_COND
))
211 mutex_lock(&hidg
->lock
);
214 count
= min_t(unsigned, count
, hidg
->report_length
);
215 status
= copy_from_user(hidg
->req
->buf
, buffer
, count
);
218 ERROR(hidg
->func
.config
->cdev
,
219 "copy_from_user error\n");
220 mutex_unlock(&hidg
->lock
);
224 hidg
->req
->status
= 0;
226 hidg
->req
->length
= count
;
227 hidg
->req
->complete
= f_hidg_req_complete
;
228 hidg
->req
->context
= hidg
;
229 hidg
->write_pending
= 1;
231 status
= usb_ep_queue(hidg
->in_ep
, hidg
->req
, GFP_ATOMIC
);
233 ERROR(hidg
->func
.config
->cdev
,
234 "usb_ep_queue error on int endpoint %zd\n", status
);
235 hidg
->write_pending
= 0;
236 wake_up(&hidg
->write_queue
);
241 mutex_unlock(&hidg
->lock
);
246 static unsigned int f_hidg_poll(struct file
*file
, poll_table
*wait
)
248 struct f_hidg
*hidg
= file
->private_data
;
249 unsigned int ret
= 0;
251 poll_wait(file
, &hidg
->read_queue
, wait
);
252 poll_wait(file
, &hidg
->write_queue
, wait
);
255 ret
|= POLLOUT
| POLLWRNORM
;
258 ret
|= POLLIN
| POLLRDNORM
;
266 static int f_hidg_release(struct inode
*inode
, struct file
*fd
)
268 fd
->private_data
= NULL
;
272 static int f_hidg_open(struct inode
*inode
, struct file
*fd
)
274 struct f_hidg
*hidg
=
275 container_of(inode
->i_cdev
, struct f_hidg
, cdev
);
277 fd
->private_data
= hidg
;
282 /*-------------------------------------------------------------------------*/
285 static void hidg_set_report_complete(struct usb_ep
*ep
, struct usb_request
*req
)
287 struct f_hidg
*hidg
= (struct f_hidg
*)req
->context
;
289 if (req
->status
!= 0 || req
->buf
== NULL
|| req
->actual
== 0) {
290 ERROR(hidg
->func
.config
->cdev
, "%s FAILED\n", __func__
);
294 spin_lock(&hidg
->spinlock
);
296 hidg
->set_report_buff
= krealloc(hidg
->set_report_buff
,
297 req
->actual
, GFP_ATOMIC
);
299 if (hidg
->set_report_buff
== NULL
) {
300 spin_unlock(&hidg
->spinlock
);
303 hidg
->set_report_length
= req
->actual
;
304 memcpy(hidg
->set_report_buff
, req
->buf
, req
->actual
);
306 spin_unlock(&hidg
->spinlock
);
308 wake_up(&hidg
->read_queue
);
311 static int hidg_setup(struct usb_function
*f
,
312 const struct usb_ctrlrequest
*ctrl
)
314 struct f_hidg
*hidg
= func_to_hidg(f
);
315 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
316 struct usb_request
*req
= cdev
->req
;
320 value
= __le16_to_cpu(ctrl
->wValue
);
321 length
= __le16_to_cpu(ctrl
->wLength
);
323 VDBG(cdev
, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
324 "Value:0x%x\n", ctrl
->bRequestType
, ctrl
->bRequest
, value
);
326 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
327 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
328 | HID_REQ_GET_REPORT
):
329 VDBG(cdev
, "get_report\n");
331 /* send an empty report */
332 length
= min_t(unsigned, length
, hidg
->report_length
);
333 memset(req
->buf
, 0x0, length
);
338 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
339 | HID_REQ_GET_PROTOCOL
):
340 VDBG(cdev
, "get_protocol\n");
344 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
345 | HID_REQ_SET_REPORT
):
346 VDBG(cdev
, "set_report | wLenght=%d\n", ctrl
->wLength
);
348 req
->complete
= hidg_set_report_complete
;
352 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
353 | HID_REQ_SET_PROTOCOL
):
354 VDBG(cdev
, "set_protocol\n");
358 case ((USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_INTERFACE
) << 8
359 | USB_REQ_GET_DESCRIPTOR
):
360 switch (value
>> 8) {
362 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: HID\n");
363 length
= min_t(unsigned short, length
,
365 memcpy(req
->buf
, &hidg_desc
, length
);
369 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
370 length
= min_t(unsigned short, length
,
371 hidg
->report_desc_length
);
372 memcpy(req
->buf
, hidg
->report_desc
, length
);
377 VDBG(cdev
, "Unknown decriptor request 0x%x\n",
385 VDBG(cdev
, "Unknown request 0x%x\n",
396 req
->length
= length
;
397 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
399 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
403 static void hidg_disable(struct usb_function
*f
)
405 struct f_hidg
*hidg
= func_to_hidg(f
);
407 usb_ep_disable(hidg
->in_ep
);
408 hidg
->in_ep
->driver_data
= NULL
;
411 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
413 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
414 struct f_hidg
*hidg
= func_to_hidg(f
);
417 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
419 if (hidg
->in_ep
!= NULL
) {
420 /* restart endpoint */
421 if (hidg
->in_ep
->driver_data
!= NULL
)
422 usb_ep_disable(hidg
->in_ep
);
424 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
427 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
430 status
= usb_ep_enable(hidg
->in_ep
);
432 ERROR(cdev
, "Enable endpoint FAILED!\n");
435 hidg
->in_ep
->driver_data
= hidg
;
441 const struct file_operations f_hidg_fops
= {
442 .owner
= THIS_MODULE
,
444 .release
= f_hidg_release
,
445 .write
= f_hidg_write
,
448 .llseek
= noop_llseek
,
451 static int __init
hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
454 struct f_hidg
*hidg
= func_to_hidg(f
);
458 /* allocate instance-specific interface IDs, and patch descriptors */
459 status
= usb_interface_id(c
, f
);
462 hidg_interface_desc
.bInterfaceNumber
= status
;
465 /* allocate instance-specific endpoints */
467 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
470 ep
->driver_data
= c
->cdev
; /* claim */
473 /* preallocate request and buffer */
475 hidg
->req
= usb_ep_alloc_request(hidg
->in_ep
, GFP_KERNEL
);
480 hidg
->req
->buf
= kmalloc(hidg
->report_length
, GFP_KERNEL
);
484 /* set descriptor dynamic values */
485 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
486 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
487 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
488 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
489 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
490 hidg_desc
.desc
[0].wDescriptorLength
=
491 cpu_to_le16(hidg
->report_desc_length
);
493 hidg
->set_report_buff
= NULL
;
495 /* copy descriptors */
496 f
->descriptors
= usb_copy_descriptors(hidg_fs_descriptors
);
500 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
501 hidg_hs_in_ep_desc
.bEndpointAddress
=
502 hidg_fs_in_ep_desc
.bEndpointAddress
;
503 f
->hs_descriptors
= usb_copy_descriptors(hidg_hs_descriptors
);
504 if (!f
->hs_descriptors
)
508 mutex_init(&hidg
->lock
);
509 spin_lock_init(&hidg
->spinlock
);
510 init_waitqueue_head(&hidg
->write_queue
);
511 init_waitqueue_head(&hidg
->read_queue
);
513 /* create char device */
514 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
515 dev
= MKDEV(major
, hidg
->minor
);
516 status
= cdev_add(&hidg
->cdev
, dev
, 1);
520 device_create(hidg_class
, NULL
, dev
, NULL
, "%s%d", "hidg", hidg
->minor
);
525 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
526 if (hidg
->req
!= NULL
) {
527 kfree(hidg
->req
->buf
);
528 if (hidg
->in_ep
!= NULL
)
529 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
532 usb_free_descriptors(f
->hs_descriptors
);
533 usb_free_descriptors(f
->descriptors
);
538 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
540 struct f_hidg
*hidg
= func_to_hidg(f
);
542 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
543 cdev_del(&hidg
->cdev
);
545 /* disable/free request and end point */
546 usb_ep_disable(hidg
->in_ep
);
547 usb_ep_dequeue(hidg
->in_ep
, hidg
->req
);
548 kfree(hidg
->req
->buf
);
549 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
551 /* free descriptors copies */
552 usb_free_descriptors(f
->hs_descriptors
);
553 usb_free_descriptors(f
->descriptors
);
555 kfree(hidg
->report_desc
);
556 kfree(hidg
->set_report_buff
);
560 /*-------------------------------------------------------------------------*/
563 #define CT_FUNC_HID_IDX 0
565 static struct usb_string ct_func_string_defs
[] = {
566 [CT_FUNC_HID_IDX
].s
= "HID Interface",
567 {}, /* end of list */
570 static struct usb_gadget_strings ct_func_string_table
= {
571 .language
= 0x0409, /* en-US */
572 .strings
= ct_func_string_defs
,
575 static struct usb_gadget_strings
*ct_func_strings
[] = {
576 &ct_func_string_table
,
580 /*-------------------------------------------------------------------------*/
581 /* usb_configuration */
583 int __init
hidg_bind_config(struct usb_configuration
*c
,
584 struct hidg_func_descriptor
*fdesc
, int index
)
592 /* maybe allocate device-global string IDs, and patch descriptors */
593 if (ct_func_string_defs
[CT_FUNC_HID_IDX
].id
== 0) {
594 status
= usb_string_id(c
->cdev
);
597 ct_func_string_defs
[CT_FUNC_HID_IDX
].id
= status
;
598 hidg_interface_desc
.iInterface
= status
;
601 /* allocate and initialize one new instance */
602 hidg
= kzalloc(sizeof *hidg
, GFP_KERNEL
);
607 hidg
->bInterfaceSubClass
= fdesc
->subclass
;
608 hidg
->bInterfaceProtocol
= fdesc
->protocol
;
609 hidg
->report_length
= fdesc
->report_length
;
610 hidg
->report_desc_length
= fdesc
->report_desc_length
;
611 hidg
->report_desc
= kmemdup(fdesc
->report_desc
,
612 fdesc
->report_desc_length
,
614 if (!hidg
->report_desc
) {
619 hidg
->func
.name
= "hid";
620 hidg
->func
.strings
= ct_func_strings
;
621 hidg
->func
.bind
= hidg_bind
;
622 hidg
->func
.unbind
= hidg_unbind
;
623 hidg
->func
.set_alt
= hidg_set_alt
;
624 hidg
->func
.disable
= hidg_disable
;
625 hidg
->func
.setup
= hidg_setup
;
627 status
= usb_add_function(c
, &hidg
->func
);
634 int __init
ghid_setup(struct usb_gadget
*g
, int count
)
639 hidg_class
= class_create(THIS_MODULE
, "hidg");
641 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
650 void ghid_cleanup(void)
653 unregister_chrdev_region(MKDEV(major
, 0), minors
);
657 class_destroy(hidg_class
);