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
;
64 static inline struct f_hidg
*func_to_hidg(struct usb_function
*f
)
66 return container_of(f
, struct f_hidg
, func
);
69 /*-------------------------------------------------------------------------*/
70 /* Static descriptors */
72 static struct usb_interface_descriptor hidg_interface_desc
= {
73 .bLength
= sizeof hidg_interface_desc
,
74 .bDescriptorType
= USB_DT_INTERFACE
,
75 /* .bInterfaceNumber = DYNAMIC */
76 .bAlternateSetting
= 0,
78 .bInterfaceClass
= USB_CLASS_HID
,
79 /* .bInterfaceSubClass = DYNAMIC */
80 /* .bInterfaceProtocol = DYNAMIC */
81 /* .iInterface = DYNAMIC */
84 static struct hid_descriptor hidg_desc
= {
85 .bLength
= sizeof hidg_desc
,
86 .bDescriptorType
= HID_DT_HID
,
89 .bNumDescriptors
= 0x1,
90 /*.desc[0].bDescriptorType = DYNAMIC */
91 /*.desc[0].wDescriptorLenght = DYNAMIC */
94 /* High-Speed Support */
96 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc
= {
97 .bLength
= USB_DT_ENDPOINT_SIZE
,
98 .bDescriptorType
= USB_DT_ENDPOINT
,
99 .bEndpointAddress
= USB_DIR_IN
,
100 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
101 /*.wMaxPacketSize = DYNAMIC */
102 .bInterval
= 4, /* FIXME: Add this field in the
103 * HID gadget configuration?
104 * (struct hidg_func_descriptor)
108 static struct usb_descriptor_header
*hidg_hs_descriptors
[] = {
109 (struct usb_descriptor_header
*)&hidg_interface_desc
,
110 (struct usb_descriptor_header
*)&hidg_desc
,
111 (struct usb_descriptor_header
*)&hidg_hs_in_ep_desc
,
115 /* Full-Speed Support */
117 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc
= {
118 .bLength
= USB_DT_ENDPOINT_SIZE
,
119 .bDescriptorType
= USB_DT_ENDPOINT
,
120 .bEndpointAddress
= USB_DIR_IN
,
121 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
122 /*.wMaxPacketSize = DYNAMIC */
123 .bInterval
= 10, /* FIXME: Add this field in the
124 * HID gadget configuration?
125 * (struct hidg_func_descriptor)
129 static struct usb_descriptor_header
*hidg_fs_descriptors
[] = {
130 (struct usb_descriptor_header
*)&hidg_interface_desc
,
131 (struct usb_descriptor_header
*)&hidg_desc
,
132 (struct usb_descriptor_header
*)&hidg_fs_in_ep_desc
,
136 /*-------------------------------------------------------------------------*/
139 static ssize_t
f_hidg_read(struct file
*file
, char __user
*buffer
,
140 size_t count
, loff_t
*ptr
)
142 struct f_hidg
*hidg
= file
->private_data
;
143 char *tmp_buff
= NULL
;
149 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
152 spin_lock_irqsave(&hidg
->spinlock
, flags
);
154 #define READ_COND (hidg->set_report_buff != NULL)
157 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
158 if (file
->f_flags
& O_NONBLOCK
)
161 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
164 spin_lock_irqsave(&hidg
->spinlock
, flags
);
168 count
= min_t(unsigned, count
, hidg
->set_report_length
);
169 tmp_buff
= hidg
->set_report_buff
;
170 hidg
->set_report_buff
= NULL
;
172 spin_unlock_irqrestore(&hidg
->spinlock
, flags
);
174 if (tmp_buff
!= NULL
) {
175 /* copy to user outside spinlock */
176 count
-= copy_to_user(buffer
, tmp_buff
, count
);
184 static void f_hidg_req_complete(struct usb_ep
*ep
, struct usb_request
*req
)
186 struct f_hidg
*hidg
= (struct f_hidg
*)ep
->driver_data
;
188 if (req
->status
!= 0) {
189 ERROR(hidg
->func
.config
->cdev
,
190 "End Point Request ERROR: %d\n", req
->status
);
193 hidg
->write_pending
= 0;
194 wake_up(&hidg
->write_queue
);
197 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
198 size_t count
, loff_t
*offp
)
200 struct f_hidg
*hidg
= file
->private_data
;
201 ssize_t status
= -ENOMEM
;
203 if (!access_ok(VERIFY_READ
, buffer
, count
))
206 mutex_lock(&hidg
->lock
);
208 #define WRITE_COND (!hidg->write_pending)
211 while (!WRITE_COND
) {
212 mutex_unlock(&hidg
->lock
);
213 if (file
->f_flags
& O_NONBLOCK
)
216 if (wait_event_interruptible_exclusive(
217 hidg
->write_queue
, WRITE_COND
))
220 mutex_lock(&hidg
->lock
);
223 count
= min_t(unsigned, count
, hidg
->report_length
);
224 status
= copy_from_user(hidg
->req
->buf
, buffer
, count
);
227 ERROR(hidg
->func
.config
->cdev
,
228 "copy_from_user error\n");
229 mutex_unlock(&hidg
->lock
);
233 hidg
->req
->status
= 0;
235 hidg
->req
->length
= count
;
236 hidg
->req
->complete
= f_hidg_req_complete
;
237 hidg
->req
->context
= hidg
;
238 hidg
->write_pending
= 1;
240 status
= usb_ep_queue(hidg
->in_ep
, hidg
->req
, GFP_ATOMIC
);
242 ERROR(hidg
->func
.config
->cdev
,
243 "usb_ep_queue error on int endpoint %zd\n", status
);
244 hidg
->write_pending
= 0;
245 wake_up(&hidg
->write_queue
);
250 mutex_unlock(&hidg
->lock
);
255 static unsigned int f_hidg_poll(struct file
*file
, poll_table
*wait
)
257 struct f_hidg
*hidg
= file
->private_data
;
258 unsigned int ret
= 0;
260 poll_wait(file
, &hidg
->read_queue
, wait
);
261 poll_wait(file
, &hidg
->write_queue
, wait
);
264 ret
|= POLLOUT
| POLLWRNORM
;
267 ret
|= POLLIN
| POLLRDNORM
;
275 static int f_hidg_release(struct inode
*inode
, struct file
*fd
)
277 fd
->private_data
= NULL
;
281 static int f_hidg_open(struct inode
*inode
, struct file
*fd
)
283 struct f_hidg
*hidg
=
284 container_of(inode
->i_cdev
, struct f_hidg
, cdev
);
286 fd
->private_data
= hidg
;
291 /*-------------------------------------------------------------------------*/
294 static void hidg_set_report_complete(struct usb_ep
*ep
, struct usb_request
*req
)
296 struct f_hidg
*hidg
= (struct f_hidg
*)req
->context
;
298 if (req
->status
!= 0 || req
->buf
== NULL
|| req
->actual
== 0) {
299 ERROR(hidg
->func
.config
->cdev
, "%s FAILED\n", __func__
);
303 spin_lock(&hidg
->spinlock
);
305 hidg
->set_report_buff
= krealloc(hidg
->set_report_buff
,
306 req
->actual
, GFP_ATOMIC
);
308 if (hidg
->set_report_buff
== NULL
) {
309 spin_unlock(&hidg
->spinlock
);
312 hidg
->set_report_length
= req
->actual
;
313 memcpy(hidg
->set_report_buff
, req
->buf
, req
->actual
);
315 spin_unlock(&hidg
->spinlock
);
317 wake_up(&hidg
->read_queue
);
320 static int hidg_setup(struct usb_function
*f
,
321 const struct usb_ctrlrequest
*ctrl
)
323 struct f_hidg
*hidg
= func_to_hidg(f
);
324 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
325 struct usb_request
*req
= cdev
->req
;
329 value
= __le16_to_cpu(ctrl
->wValue
);
330 length
= __le16_to_cpu(ctrl
->wLength
);
332 VDBG(cdev
, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
333 "Value:0x%x\n", ctrl
->bRequestType
, ctrl
->bRequest
, value
);
335 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
336 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
337 | HID_REQ_GET_REPORT
):
338 VDBG(cdev
, "get_report\n");
340 /* send an empty report */
341 length
= min_t(unsigned, length
, hidg
->report_length
);
342 memset(req
->buf
, 0x0, length
);
347 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
348 | HID_REQ_GET_PROTOCOL
):
349 VDBG(cdev
, "get_protocol\n");
353 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
354 | HID_REQ_SET_REPORT
):
355 VDBG(cdev
, "set_report | wLenght=%d\n", ctrl
->wLength
);
357 req
->complete
= hidg_set_report_complete
;
361 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
362 | HID_REQ_SET_PROTOCOL
):
363 VDBG(cdev
, "set_protocol\n");
367 case ((USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_INTERFACE
) << 8
368 | USB_REQ_GET_DESCRIPTOR
):
369 switch (value
>> 8) {
371 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: HID\n");
372 length
= min_t(unsigned short, length
,
374 memcpy(req
->buf
, &hidg_desc
, length
);
378 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
379 length
= min_t(unsigned short, length
,
380 hidg
->report_desc_length
);
381 memcpy(req
->buf
, hidg
->report_desc
, length
);
386 VDBG(cdev
, "Unknown decriptor request 0x%x\n",
394 VDBG(cdev
, "Unknown request 0x%x\n",
405 req
->length
= length
;
406 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
408 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
412 static void hidg_disable(struct usb_function
*f
)
414 struct f_hidg
*hidg
= func_to_hidg(f
);
416 usb_ep_disable(hidg
->in_ep
);
417 hidg
->in_ep
->driver_data
= NULL
;
420 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
422 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
423 struct f_hidg
*hidg
= func_to_hidg(f
);
426 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
428 if (hidg
->in_ep
!= NULL
) {
429 /* restart endpoint */
430 if (hidg
->in_ep
->driver_data
!= NULL
)
431 usb_ep_disable(hidg
->in_ep
);
433 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
436 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
439 status
= usb_ep_enable(hidg
->in_ep
);
441 ERROR(cdev
, "Enable endpoint FAILED!\n");
444 hidg
->in_ep
->driver_data
= hidg
;
450 const struct file_operations f_hidg_fops
= {
451 .owner
= THIS_MODULE
,
453 .release
= f_hidg_release
,
454 .write
= f_hidg_write
,
457 .llseek
= noop_llseek
,
460 static int __init
hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
463 struct f_hidg
*hidg
= func_to_hidg(f
);
467 /* allocate instance-specific interface IDs, and patch descriptors */
468 status
= usb_interface_id(c
, f
);
471 hidg_interface_desc
.bInterfaceNumber
= status
;
474 /* allocate instance-specific endpoints */
476 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
479 ep
->driver_data
= c
->cdev
; /* claim */
482 /* preallocate request and buffer */
484 hidg
->req
= usb_ep_alloc_request(hidg
->in_ep
, GFP_KERNEL
);
489 hidg
->req
->buf
= kmalloc(hidg
->report_length
, GFP_KERNEL
);
493 /* set descriptor dynamic values */
494 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
495 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
496 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
497 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
498 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
499 hidg_desc
.desc
[0].wDescriptorLength
=
500 cpu_to_le16(hidg
->report_desc_length
);
502 hidg
->set_report_buff
= NULL
;
504 /* copy descriptors */
505 f
->descriptors
= usb_copy_descriptors(hidg_fs_descriptors
);
509 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
510 hidg_hs_in_ep_desc
.bEndpointAddress
=
511 hidg_fs_in_ep_desc
.bEndpointAddress
;
512 f
->hs_descriptors
= usb_copy_descriptors(hidg_hs_descriptors
);
513 if (!f
->hs_descriptors
)
517 mutex_init(&hidg
->lock
);
518 spin_lock_init(&hidg
->spinlock
);
519 init_waitqueue_head(&hidg
->write_queue
);
520 init_waitqueue_head(&hidg
->read_queue
);
522 /* create char device */
523 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
524 dev
= MKDEV(major
, hidg
->minor
);
525 status
= cdev_add(&hidg
->cdev
, dev
, 1);
529 device_create(hidg_class
, NULL
, dev
, NULL
, "%s%d", "hidg", hidg
->minor
);
534 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
535 if (hidg
->req
!= NULL
) {
536 kfree(hidg
->req
->buf
);
537 if (hidg
->in_ep
!= NULL
)
538 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
541 usb_free_descriptors(f
->hs_descriptors
);
542 usb_free_descriptors(f
->descriptors
);
547 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
549 struct f_hidg
*hidg
= func_to_hidg(f
);
551 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
552 cdev_del(&hidg
->cdev
);
554 /* disable/free request and end point */
555 usb_ep_disable(hidg
->in_ep
);
556 usb_ep_dequeue(hidg
->in_ep
, hidg
->req
);
557 kfree(hidg
->req
->buf
);
558 usb_ep_free_request(hidg
->in_ep
, hidg
->req
);
560 /* free descriptors copies */
561 usb_free_descriptors(f
->hs_descriptors
);
562 usb_free_descriptors(f
->descriptors
);
564 kfree(hidg
->report_desc
);
565 kfree(hidg
->set_report_buff
);
569 /*-------------------------------------------------------------------------*/
572 #define CT_FUNC_HID_IDX 0
574 static struct usb_string ct_func_string_defs
[] = {
575 [CT_FUNC_HID_IDX
].s
= "HID Interface",
576 {}, /* end of list */
579 static struct usb_gadget_strings ct_func_string_table
= {
580 .language
= 0x0409, /* en-US */
581 .strings
= ct_func_string_defs
,
584 static struct usb_gadget_strings
*ct_func_strings
[] = {
585 &ct_func_string_table
,
589 /*-------------------------------------------------------------------------*/
590 /* usb_configuration */
592 int __init
hidg_bind_config(struct usb_configuration
*c
,
593 struct hidg_func_descriptor
*fdesc
, int index
)
601 /* maybe allocate device-global string IDs, and patch descriptors */
602 if (ct_func_string_defs
[CT_FUNC_HID_IDX
].id
== 0) {
603 status
= usb_string_id(c
->cdev
);
606 ct_func_string_defs
[CT_FUNC_HID_IDX
].id
= status
;
607 hidg_interface_desc
.iInterface
= status
;
610 /* allocate and initialize one new instance */
611 hidg
= kzalloc(sizeof *hidg
, GFP_KERNEL
);
616 hidg
->bInterfaceSubClass
= fdesc
->subclass
;
617 hidg
->bInterfaceProtocol
= fdesc
->protocol
;
618 hidg
->report_length
= fdesc
->report_length
;
619 hidg
->report_desc_length
= fdesc
->report_desc_length
;
620 hidg
->report_desc
= kmemdup(fdesc
->report_desc
,
621 fdesc
->report_desc_length
,
623 if (!hidg
->report_desc
) {
628 hidg
->func
.name
= "hid";
629 hidg
->func
.strings
= ct_func_strings
;
630 hidg
->func
.bind
= hidg_bind
;
631 hidg
->func
.unbind
= hidg_unbind
;
632 hidg
->func
.set_alt
= hidg_set_alt
;
633 hidg
->func
.disable
= hidg_disable
;
634 hidg
->func
.setup
= hidg_setup
;
636 status
= usb_add_function(c
, &hidg
->func
);
643 int __init
ghid_setup(struct usb_gadget
*g
, int count
)
648 hidg_class
= class_create(THIS_MODULE
, "hidg");
650 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
659 void ghid_cleanup(void)
662 unregister_chrdev_region(MKDEV(major
, 0), minors
);
666 class_destroy(hidg_class
);