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.
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.
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
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/uaccess.h>
29 #include <linux/wait.h>
30 #include <linux/usb/g_hid.h>
32 static int major
, minors
;
33 static struct class *hidg_class
;
35 /*-------------------------------------------------------------------------*/
36 /* HID gadget struct */
40 unsigned char bInterfaceSubClass
;
41 unsigned char bInterfaceProtocol
;
42 unsigned short report_desc_length
;
44 unsigned short report_length
;
47 char *set_report_buff
;
48 unsigned short set_report_length
;
50 wait_queue_head_t read_queue
;
55 wait_queue_head_t write_queue
;
56 struct usb_request
*req
;
60 struct usb_function func
;
62 struct usb_endpoint_descriptor
*fs_in_ep_desc
;
63 struct usb_endpoint_descriptor
*hs_in_ep_desc
;
66 static inline struct f_hidg
*func_to_hidg(struct usb_function
*f
)
68 return container_of(f
, struct f_hidg
, func
);
71 /*-------------------------------------------------------------------------*/
72 /* Static descriptors */
74 static struct usb_interface_descriptor hidg_interface_desc
= {
75 .bLength
= sizeof hidg_interface_desc
,
76 .bDescriptorType
= USB_DT_INTERFACE
,
77 /* .bInterfaceNumber = DYNAMIC */
78 .bAlternateSetting
= 0,
80 .bInterfaceClass
= USB_CLASS_HID
,
81 /* .bInterfaceSubClass = DYNAMIC */
82 /* .bInterfaceProtocol = DYNAMIC */
83 /* .iInterface = DYNAMIC */
86 static struct hid_descriptor hidg_desc
= {
87 .bLength
= sizeof hidg_desc
,
88 .bDescriptorType
= HID_DT_HID
,
91 .bNumDescriptors
= 0x1,
92 /*.desc[0].bDescriptorType = DYNAMIC */
93 /*.desc[0].wDescriptorLenght = DYNAMIC */
96 /* High-Speed Support */
98 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc
= {
99 .bLength
= USB_DT_ENDPOINT_SIZE
,
100 .bDescriptorType
= USB_DT_ENDPOINT
,
101 .bEndpointAddress
= USB_DIR_IN
,
102 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
103 /*.wMaxPacketSize = DYNAMIC */
104 .bInterval
= 4, /* FIXME: Add this field in the
105 * HID gadget configuration?
106 * (struct hidg_func_descriptor)
110 static struct usb_descriptor_header
*hidg_hs_descriptors
[] = {
111 (struct usb_descriptor_header
*)&hidg_interface_desc
,
112 (struct usb_descriptor_header
*)&hidg_desc
,
113 (struct usb_descriptor_header
*)&hidg_hs_in_ep_desc
,
117 /* Full-Speed Support */
119 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc
= {
120 .bLength
= USB_DT_ENDPOINT_SIZE
,
121 .bDescriptorType
= USB_DT_ENDPOINT
,
122 .bEndpointAddress
= USB_DIR_IN
,
123 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
124 /*.wMaxPacketSize = DYNAMIC */
125 .bInterval
= 10, /* FIXME: Add this field in the
126 * HID gadget configuration?
127 * (struct hidg_func_descriptor)
131 static struct usb_descriptor_header
*hidg_fs_descriptors
[] = {
132 (struct usb_descriptor_header
*)&hidg_interface_desc
,
133 (struct usb_descriptor_header
*)&hidg_desc
,
134 (struct usb_descriptor_header
*)&hidg_fs_in_ep_desc
,
138 /*-------------------------------------------------------------------------*/
141 static ssize_t
f_hidg_read(struct file
*file
, char __user
*buffer
,
142 size_t count
, loff_t
*ptr
)
144 struct f_hidg
*hidg
= file
->private_data
;
145 char *tmp_buff
= NULL
;
151 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
154 spin_lock_irqsave(&hidg
->spinlock
, flags
);
156 #define READ_COND (hidg->set_report_buff != NULL)
159 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
160 if (file
->f_flags
& O_NONBLOCK
)
163 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
166 spin_lock_irqsave(&hidg
->spinlock
, flags
);
170 count
= min_t(unsigned, count
, hidg
->set_report_length
);
171 tmp_buff
= hidg
->set_report_buff
;
172 hidg
->set_report_buff
= NULL
;
174 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
176 if (tmp_buff
!= NULL
) {
177 /* copy to user outside spinlock */
178 count
-= copy_to_user(buffer
, tmp_buff
, count
);
186 static void f_hidg_req_complete(struct usb_ep
*ep
, struct usb_request
*req
)
188 struct f_hidg
*hidg
= (struct f_hidg
*)ep
->driver_data
;
190 if (req
->status
!= 0) {
191 ERROR(hidg
->func
.config
->cdev
,
192 "End Point Request ERROR: %d\n", req
->status
);
195 hidg
->write_pending
= 0;
196 wake_up(&hidg
->write_queue
);
199 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
200 size_t count
, loff_t
*offp
)
202 struct f_hidg
*hidg
= file
->private_data
;
203 ssize_t status
= -ENOMEM
;
205 if (!access_ok(VERIFY_READ
, buffer
, count
))
208 mutex_lock(&hidg
->lock
);
210 #define WRITE_COND (!hidg->write_pending)
213 while (!WRITE_COND
) {
214 mutex_unlock(&hidg
->lock
);
215 if (file
->f_flags
& O_NONBLOCK
)
218 if (wait_event_interruptible_exclusive(
219 hidg
->write_queue
, WRITE_COND
))
222 mutex_lock(&hidg
->lock
);
225 count
= min_t(unsigned, count
, hidg
->report_length
);
226 status
= copy_from_user(hidg
->req
->buf
, buffer
, count
);
229 ERROR(hidg
->func
.config
->cdev
,
230 "copy_from_user error\n");
231 mutex_unlock(&hidg
->lock
);
235 hidg
->req
->status
= 0;
237 hidg
->req
->length
= count
;
238 hidg
->req
->complete
= f_hidg_req_complete
;
239 hidg
->req
->context
= hidg
;
240 hidg
->write_pending
= 1;
242 status
= usb_ep_queue(hidg
->in_ep
, hidg
->req
, GFP_ATOMIC
);
244 ERROR(hidg
->func
.config
->cdev
,
245 "usb_ep_queue error on int endpoint %zd\n", status
);
246 hidg
->write_pending
= 0;
247 wake_up(&hidg
->write_queue
);
252 mutex_unlock(&hidg
->lock
);
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
->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
->spinlock
);
314 hidg
->set_report_length
= req
->actual
;
315 memcpy(hidg
->set_report_buff
, req
->buf
, req
->actual
);
317 spin_unlock(&hidg
->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: REPORT\n");
374 length
= min_t(unsigned short, length
,
375 hidg
->report_desc_length
);
376 memcpy(req
->buf
, hidg
->report_desc
, length
);
381 VDBG(cdev
, "Unknown decriptor request 0x%x\n",
389 VDBG(cdev
, "Unknown request 0x%x\n",
400 req
->length
= length
;
401 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
403 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
407 static void hidg_disable(struct usb_function
*f
)
409 struct f_hidg
*hidg
= func_to_hidg(f
);
411 usb_ep_disable(hidg
->in_ep
);
412 hidg
->in_ep
->driver_data
= NULL
;
415 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
417 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
418 struct f_hidg
*hidg
= func_to_hidg(f
);
419 const struct usb_endpoint_descriptor
*ep_desc
;
422 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
424 if (hidg
->in_ep
!= NULL
) {
425 /* restart endpoint */
426 if (hidg
->in_ep
->driver_data
!= NULL
)
427 usb_ep_disable(hidg
->in_ep
);
429 ep_desc
= ep_choose(f
->config
->cdev
->gadget
,
430 hidg
->hs_in_ep_desc
, hidg
->fs_in_ep_desc
);
431 status
= usb_ep_enable(hidg
->in_ep
, ep_desc
);
433 ERROR(cdev
, "Enable endpoint FAILED!\n");
436 hidg
->in_ep
->driver_data
= hidg
;
442 const struct file_operations f_hidg_fops
= {
443 .owner
= THIS_MODULE
,
445 .release
= f_hidg_release
,
446 .write
= f_hidg_write
,
449 .llseek
= noop_llseek
,
452 static int __init
hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
455 struct f_hidg
*hidg
= func_to_hidg(f
);
459 /* allocate instance-specific interface IDs, and patch descriptors */
460 status
= usb_interface_id(c
, f
);
463 hidg_interface_desc
.bInterfaceNumber
= status
;
466 /* allocate instance-specific endpoints */
468 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
471 ep
->driver_data
= c
->cdev
; /* claim */
474 /* preallocate request and buffer */
476 hidg
->req
= usb_ep_alloc_request(hidg
->in_ep
, GFP_KERNEL
);
481 hidg
->req
->buf
= kmalloc(hidg
->report_length
, GFP_KERNEL
);
485 /* set descriptor dynamic values */
486 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
487 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
488 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
489 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
490 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
491 hidg_desc
.desc
[0].wDescriptorLength
=
492 cpu_to_le16(hidg
->report_desc_length
);
494 hidg
->set_report_buff
= NULL
;
496 /* copy descriptors */
497 f
->descriptors
= usb_copy_descriptors(hidg_fs_descriptors
);
501 hidg
->fs_in_ep_desc
= usb_find_endpoint(hidg_fs_descriptors
,
503 &hidg_fs_in_ep_desc
);
505 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
506 hidg_hs_in_ep_desc
.bEndpointAddress
=
507 hidg_fs_in_ep_desc
.bEndpointAddress
;
508 f
->hs_descriptors
= usb_copy_descriptors(hidg_hs_descriptors
);
509 if (!f
->hs_descriptors
)
511 hidg
->hs_in_ep_desc
= usb_find_endpoint(hidg_hs_descriptors
,
513 &hidg_hs_in_ep_desc
);
515 hidg
->hs_in_ep_desc
= NULL
;
518 mutex_init(&hidg
->lock
);
519 spin_lock_init(&hidg
->spinlock
);
520 init_waitqueue_head(&hidg
->write_queue
);
521 init_waitqueue_head(&hidg
->read_queue
);
523 /* create char device */
524 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
525 dev
= MKDEV(major
, hidg
->minor
);
526 status
= cdev_add(&hidg
->cdev
, dev
, 1);
530 device_create(hidg_class
, NULL
, dev
, NULL
, "%s%d", "hidg", hidg
->minor
);
535 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
536 if (hidg
->req
!= NULL
) {
537 kfree(hidg
->req
->buf
);
538 if (hidg
->in_ep
!= NULL
)
539 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
542 usb_free_descriptors(f
->hs_descriptors
);
543 usb_free_descriptors(f
->descriptors
);
548 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
550 struct f_hidg
*hidg
= func_to_hidg(f
);
552 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
553 cdev_del(&hidg
->cdev
);
555 /* disable/free request and end point */
556 usb_ep_disable(hidg
->in_ep
);
557 usb_ep_dequeue(hidg
->in_ep
, hidg
->req
);
558 kfree(hidg
->req
->buf
);
559 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
561 /* free descriptors copies */
562 usb_free_descriptors(f
->hs_descriptors
);
563 usb_free_descriptors(f
->descriptors
);
565 kfree(hidg
->report_desc
);
566 kfree(hidg
->set_report_buff
);
570 /*-------------------------------------------------------------------------*/
573 #define CT_FUNC_HID_IDX 0
575 static struct usb_string ct_func_string_defs
[] = {
576 [CT_FUNC_HID_IDX
].s
= "HID Interface",
577 {}, /* end of list */
580 static struct usb_gadget_strings ct_func_string_table
= {
581 .language
= 0x0409, /* en-US */
582 .strings
= ct_func_string_defs
,
585 static struct usb_gadget_strings
*ct_func_strings
[] = {
586 &ct_func_string_table
,
590 /*-------------------------------------------------------------------------*/
591 /* usb_configuration */
593 int __init
hidg_bind_config(struct usb_configuration
*c
,
594 struct hidg_func_descriptor
*fdesc
, int index
)
602 /* maybe allocate device-global string IDs, and patch descriptors */
603 if (ct_func_string_defs
[CT_FUNC_HID_IDX
].id
== 0) {
604 status
= usb_string_id(c
->cdev
);
607 ct_func_string_defs
[CT_FUNC_HID_IDX
].id
= status
;
608 hidg_interface_desc
.iInterface
= status
;
611 /* allocate and initialize one new instance */
612 hidg
= kzalloc(sizeof *hidg
, GFP_KERNEL
);
617 hidg
->bInterfaceSubClass
= fdesc
->subclass
;
618 hidg
->bInterfaceProtocol
= fdesc
->protocol
;
619 hidg
->report_length
= fdesc
->report_length
;
620 hidg
->report_desc_length
= fdesc
->report_desc_length
;
621 hidg
->report_desc
= kmemdup(fdesc
->report_desc
,
622 fdesc
->report_desc_length
,
624 if (!hidg
->report_desc
) {
629 hidg
->func
.name
= "hid";
630 hidg
->func
.strings
= ct_func_strings
;
631 hidg
->func
.bind
= hidg_bind
;
632 hidg
->func
.unbind
= hidg_unbind
;
633 hidg
->func
.set_alt
= hidg_set_alt
;
634 hidg
->func
.disable
= hidg_disable
;
635 hidg
->func
.setup
= hidg_setup
;
637 status
= usb_add_function(c
, &hidg
->func
);
644 int __init
ghid_setup(struct usb_gadget
*g
, int count
)
649 hidg_class
= class_create(THIS_MODULE
, "hidg");
651 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
660 void ghid_cleanup(void)
663 unregister_chrdev_region(MKDEV(major
, 0), minors
);
667 class_destroy(hidg_class
);