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/idr.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/sched.h>
22 #include <linux/usb/g_hid.h>
29 static int major
, minors
;
30 static struct class *hidg_class
;
31 static DEFINE_IDA(hidg_ida
);
32 static DEFINE_MUTEX(hidg_ida_lock
); /* protects access to hidg_ida */
34 /*-------------------------------------------------------------------------*/
35 /* HID gadget struct */
37 struct f_hidg_req_list
{
38 struct usb_request
*req
;
40 struct list_head list
;
45 unsigned char bInterfaceSubClass
;
46 unsigned char bInterfaceProtocol
;
47 unsigned char protocol
;
48 unsigned short report_desc_length
;
50 unsigned short report_length
;
53 struct list_head completed_out_req
;
54 spinlock_t read_spinlock
;
55 wait_queue_head_t read_queue
;
59 spinlock_t write_spinlock
;
61 wait_queue_head_t write_queue
;
62 struct usb_request
*req
;
66 struct usb_function func
;
69 struct usb_ep
*out_ep
;
72 static inline struct f_hidg
*func_to_hidg(struct usb_function
*f
)
74 return container_of(f
, struct f_hidg
, func
);
77 /*-------------------------------------------------------------------------*/
78 /* Static descriptors */
80 static struct usb_interface_descriptor hidg_interface_desc
= {
81 .bLength
= sizeof hidg_interface_desc
,
82 .bDescriptorType
= USB_DT_INTERFACE
,
83 /* .bInterfaceNumber = DYNAMIC */
84 .bAlternateSetting
= 0,
86 .bInterfaceClass
= USB_CLASS_HID
,
87 /* .bInterfaceSubClass = DYNAMIC */
88 /* .bInterfaceProtocol = DYNAMIC */
89 /* .iInterface = DYNAMIC */
92 static struct hid_descriptor hidg_desc
= {
93 .bLength
= sizeof hidg_desc
,
94 .bDescriptorType
= HID_DT_HID
,
97 .bNumDescriptors
= 0x1,
98 /*.desc[0].bDescriptorType = DYNAMIC */
99 /*.desc[0].wDescriptorLenght = DYNAMIC */
102 /* Super-Speed Support */
104 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc
= {
105 .bLength
= USB_DT_ENDPOINT_SIZE
,
106 .bDescriptorType
= USB_DT_ENDPOINT
,
107 .bEndpointAddress
= USB_DIR_IN
,
108 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
109 /*.wMaxPacketSize = DYNAMIC */
110 .bInterval
= 4, /* FIXME: Add this field in the
111 * HID gadget configuration?
112 * (struct hidg_func_descriptor)
116 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc
= {
117 .bLength
= sizeof(hidg_ss_in_comp_desc
),
118 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
120 /* .bMaxBurst = 0, */
121 /* .bmAttributes = 0, */
122 /* .wBytesPerInterval = DYNAMIC */
125 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc
= {
126 .bLength
= USB_DT_ENDPOINT_SIZE
,
127 .bDescriptorType
= USB_DT_ENDPOINT
,
128 .bEndpointAddress
= USB_DIR_OUT
,
129 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
130 /*.wMaxPacketSize = DYNAMIC */
131 .bInterval
= 4, /* FIXME: Add this field in the
132 * HID gadget configuration?
133 * (struct hidg_func_descriptor)
137 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc
= {
138 .bLength
= sizeof(hidg_ss_out_comp_desc
),
139 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
141 /* .bMaxBurst = 0, */
142 /* .bmAttributes = 0, */
143 /* .wBytesPerInterval = DYNAMIC */
146 static struct usb_descriptor_header
*hidg_ss_descriptors
[] = {
147 (struct usb_descriptor_header
*)&hidg_interface_desc
,
148 (struct usb_descriptor_header
*)&hidg_desc
,
149 (struct usb_descriptor_header
*)&hidg_ss_in_ep_desc
,
150 (struct usb_descriptor_header
*)&hidg_ss_in_comp_desc
,
151 (struct usb_descriptor_header
*)&hidg_ss_out_ep_desc
,
152 (struct usb_descriptor_header
*)&hidg_ss_out_comp_desc
,
156 /* High-Speed Support */
158 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc
= {
159 .bLength
= USB_DT_ENDPOINT_SIZE
,
160 .bDescriptorType
= USB_DT_ENDPOINT
,
161 .bEndpointAddress
= USB_DIR_IN
,
162 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
163 /*.wMaxPacketSize = DYNAMIC */
164 .bInterval
= 4, /* FIXME: Add this field in the
165 * HID gadget configuration?
166 * (struct hidg_func_descriptor)
170 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc
= {
171 .bLength
= USB_DT_ENDPOINT_SIZE
,
172 .bDescriptorType
= USB_DT_ENDPOINT
,
173 .bEndpointAddress
= USB_DIR_OUT
,
174 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
175 /*.wMaxPacketSize = DYNAMIC */
176 .bInterval
= 4, /* FIXME: Add this field in the
177 * HID gadget configuration?
178 * (struct hidg_func_descriptor)
182 static struct usb_descriptor_header
*hidg_hs_descriptors
[] = {
183 (struct usb_descriptor_header
*)&hidg_interface_desc
,
184 (struct usb_descriptor_header
*)&hidg_desc
,
185 (struct usb_descriptor_header
*)&hidg_hs_in_ep_desc
,
186 (struct usb_descriptor_header
*)&hidg_hs_out_ep_desc
,
190 /* Full-Speed Support */
192 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc
= {
193 .bLength
= USB_DT_ENDPOINT_SIZE
,
194 .bDescriptorType
= USB_DT_ENDPOINT
,
195 .bEndpointAddress
= USB_DIR_IN
,
196 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
197 /*.wMaxPacketSize = DYNAMIC */
198 .bInterval
= 10, /* FIXME: Add this field in the
199 * HID gadget configuration?
200 * (struct hidg_func_descriptor)
204 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc
= {
205 .bLength
= USB_DT_ENDPOINT_SIZE
,
206 .bDescriptorType
= USB_DT_ENDPOINT
,
207 .bEndpointAddress
= USB_DIR_OUT
,
208 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
209 /*.wMaxPacketSize = DYNAMIC */
210 .bInterval
= 10, /* FIXME: Add this field in the
211 * HID gadget configuration?
212 * (struct hidg_func_descriptor)
216 static struct usb_descriptor_header
*hidg_fs_descriptors
[] = {
217 (struct usb_descriptor_header
*)&hidg_interface_desc
,
218 (struct usb_descriptor_header
*)&hidg_desc
,
219 (struct usb_descriptor_header
*)&hidg_fs_in_ep_desc
,
220 (struct usb_descriptor_header
*)&hidg_fs_out_ep_desc
,
224 /*-------------------------------------------------------------------------*/
227 #define CT_FUNC_HID_IDX 0
229 static struct usb_string ct_func_string_defs
[] = {
230 [CT_FUNC_HID_IDX
].s
= "HID Interface",
231 {}, /* end of list */
234 static struct usb_gadget_strings ct_func_string_table
= {
235 .language
= 0x0409, /* en-US */
236 .strings
= ct_func_string_defs
,
239 static struct usb_gadget_strings
*ct_func_strings
[] = {
240 &ct_func_string_table
,
244 /*-------------------------------------------------------------------------*/
247 static ssize_t
f_hidg_read(struct file
*file
, char __user
*buffer
,
248 size_t count
, loff_t
*ptr
)
250 struct f_hidg
*hidg
= file
->private_data
;
251 struct f_hidg_req_list
*list
;
252 struct usb_request
*req
;
259 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
262 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
264 #define READ_COND (!list_empty(&hidg->completed_out_req))
266 /* wait for at least one buffer to complete */
268 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
269 if (file
->f_flags
& O_NONBLOCK
)
272 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
275 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
278 /* pick the first one */
279 list
= list_first_entry(&hidg
->completed_out_req
,
280 struct f_hidg_req_list
, list
);
283 * Remove this from list to protect it from beign free()
284 * while host disables our function
286 list_del(&list
->list
);
289 count
= min_t(unsigned int, count
, req
->actual
- list
->pos
);
290 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
292 /* copy to user outside spinlock */
293 count
-= copy_to_user(buffer
, req
->buf
+ list
->pos
, count
);
297 * if this request is completely handled and transfered to
298 * userspace, remove its entry from the list and requeue it
299 * again. Otherwise, we will revisit it again upon the next
300 * call, taking into account its current read position.
302 if (list
->pos
== req
->actual
) {
305 req
->length
= hidg
->report_length
;
306 ret
= usb_ep_queue(hidg
->out_ep
, req
, GFP_KERNEL
);
308 free_ep_req(hidg
->out_ep
, req
);
312 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
313 list_add(&list
->list
, &hidg
->completed_out_req
);
314 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
316 wake_up(&hidg
->read_queue
);
322 static void f_hidg_req_complete(struct usb_ep
*ep
, struct usb_request
*req
)
324 struct f_hidg
*hidg
= (struct f_hidg
*)ep
->driver_data
;
327 if (req
->status
!= 0) {
328 ERROR(hidg
->func
.config
->cdev
,
329 "End Point Request ERROR: %d\n", req
->status
);
332 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
333 hidg
->write_pending
= 0;
334 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
335 wake_up(&hidg
->write_queue
);
338 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
339 size_t count
, loff_t
*offp
)
341 struct f_hidg
*hidg
= file
->private_data
;
342 struct usb_request
*req
;
344 ssize_t status
= -ENOMEM
;
346 if (!access_ok(VERIFY_READ
, buffer
, count
))
349 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
351 #define WRITE_COND (!hidg->write_pending)
354 while (!WRITE_COND
) {
355 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
356 if (file
->f_flags
& O_NONBLOCK
)
359 if (wait_event_interruptible_exclusive(
360 hidg
->write_queue
, WRITE_COND
))
363 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
366 hidg
->write_pending
= 1;
368 count
= min_t(unsigned, count
, hidg
->report_length
);
370 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
371 status
= copy_from_user(req
->buf
, buffer
, count
);
374 ERROR(hidg
->func
.config
->cdev
,
375 "copy_from_user error\n");
377 goto release_write_pending
;
380 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
382 /* when our function has been disabled by host */
384 free_ep_req(hidg
->in_ep
, req
);
387 * Should we fail with error here?
395 req
->complete
= f_hidg_req_complete
;
398 status
= usb_ep_queue(hidg
->in_ep
, req
, GFP_ATOMIC
);
400 ERROR(hidg
->func
.config
->cdev
,
401 "usb_ep_queue error on int endpoint %zd\n", status
);
402 goto release_write_pending_unlocked
;
406 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
409 release_write_pending
:
410 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
411 release_write_pending_unlocked
:
412 hidg
->write_pending
= 0;
413 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
415 wake_up(&hidg
->write_queue
);
420 static unsigned int f_hidg_poll(struct file
*file
, poll_table
*wait
)
422 struct f_hidg
*hidg
= file
->private_data
;
423 unsigned int ret
= 0;
425 poll_wait(file
, &hidg
->read_queue
, wait
);
426 poll_wait(file
, &hidg
->write_queue
, wait
);
429 ret
|= POLLOUT
| POLLWRNORM
;
432 ret
|= POLLIN
| POLLRDNORM
;
440 static int f_hidg_release(struct inode
*inode
, struct file
*fd
)
442 fd
->private_data
= NULL
;
446 static int f_hidg_open(struct inode
*inode
, struct file
*fd
)
448 struct f_hidg
*hidg
=
449 container_of(inode
->i_cdev
, struct f_hidg
, cdev
);
451 fd
->private_data
= hidg
;
456 /*-------------------------------------------------------------------------*/
459 static inline struct usb_request
*hidg_alloc_ep_req(struct usb_ep
*ep
,
462 return alloc_ep_req(ep
, length
);
465 static void hidg_set_report_complete(struct usb_ep
*ep
, struct usb_request
*req
)
467 struct f_hidg
*hidg
= (struct f_hidg
*) req
->context
;
468 struct usb_composite_dev
*cdev
= hidg
->func
.config
->cdev
;
469 struct f_hidg_req_list
*req_list
;
472 switch (req
->status
) {
474 req_list
= kzalloc(sizeof(*req_list
), GFP_ATOMIC
);
476 ERROR(cdev
, "Unable to allocate mem for req_list\n");
482 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
483 list_add_tail(&req_list
->list
, &hidg
->completed_out_req
);
484 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
486 wake_up(&hidg
->read_queue
);
489 ERROR(cdev
, "Set report failed %d\n", req
->status
);
491 case -ECONNABORTED
: /* hardware forced ep reset */
492 case -ECONNRESET
: /* request dequeued */
493 case -ESHUTDOWN
: /* disconnect from host */
495 free_ep_req(ep
, req
);
500 static int hidg_setup(struct usb_function
*f
,
501 const struct usb_ctrlrequest
*ctrl
)
503 struct f_hidg
*hidg
= func_to_hidg(f
);
504 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
505 struct usb_request
*req
= cdev
->req
;
509 value
= __le16_to_cpu(ctrl
->wValue
);
510 length
= __le16_to_cpu(ctrl
->wLength
);
513 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
514 __func__
, ctrl
->bRequestType
, ctrl
->bRequest
, value
);
516 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
517 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
518 | HID_REQ_GET_REPORT
):
519 VDBG(cdev
, "get_report\n");
521 /* send an empty report */
522 length
= min_t(unsigned, length
, hidg
->report_length
);
523 memset(req
->buf
, 0x0, length
);
528 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
529 | HID_REQ_GET_PROTOCOL
):
530 VDBG(cdev
, "get_protocol\n");
531 length
= min_t(unsigned int, length
, 1);
532 ((u8
*) req
->buf
)[0] = hidg
->protocol
;
536 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
537 | HID_REQ_SET_REPORT
):
538 VDBG(cdev
, "set_report | wLength=%d\n", ctrl
->wLength
);
542 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
543 | HID_REQ_SET_PROTOCOL
):
544 VDBG(cdev
, "set_protocol\n");
545 if (value
> HID_REPORT_PROTOCOL
)
549 * We assume that programs implementing the Boot protocol
550 * are also compatible with the Report Protocol
552 if (hidg
->bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
553 hidg
->protocol
= value
;
559 case ((USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_INTERFACE
) << 8
560 | USB_REQ_GET_DESCRIPTOR
):
561 switch (value
>> 8) {
564 struct hid_descriptor hidg_desc_copy
= hidg_desc
;
566 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: HID\n");
567 hidg_desc_copy
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
568 hidg_desc_copy
.desc
[0].wDescriptorLength
=
569 cpu_to_le16(hidg
->report_desc_length
);
571 length
= min_t(unsigned short, length
,
572 hidg_desc_copy
.bLength
);
573 memcpy(req
->buf
, &hidg_desc_copy
, length
);
578 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
579 length
= min_t(unsigned short, length
,
580 hidg
->report_desc_length
);
581 memcpy(req
->buf
, hidg
->report_desc
, length
);
586 VDBG(cdev
, "Unknown descriptor request 0x%x\n",
594 VDBG(cdev
, "Unknown request 0x%x\n",
605 req
->length
= length
;
606 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
608 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
612 static void hidg_disable(struct usb_function
*f
)
614 struct f_hidg
*hidg
= func_to_hidg(f
);
615 struct f_hidg_req_list
*list
, *next
;
618 usb_ep_disable(hidg
->in_ep
);
619 usb_ep_disable(hidg
->out_ep
);
621 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
622 list_for_each_entry_safe(list
, next
, &hidg
->completed_out_req
, list
) {
623 free_ep_req(hidg
->out_ep
, list
->req
);
624 list_del(&list
->list
);
627 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
629 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
630 if (!hidg
->write_pending
) {
631 free_ep_req(hidg
->in_ep
, hidg
->req
);
632 hidg
->write_pending
= 1;
636 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
639 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
641 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
642 struct f_hidg
*hidg
= func_to_hidg(f
);
643 struct usb_request
*req_in
= NULL
;
647 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
649 if (hidg
->in_ep
!= NULL
) {
650 /* restart endpoint */
651 usb_ep_disable(hidg
->in_ep
);
653 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
656 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
659 status
= usb_ep_enable(hidg
->in_ep
);
661 ERROR(cdev
, "Enable IN endpoint FAILED!\n");
664 hidg
->in_ep
->driver_data
= hidg
;
666 req_in
= hidg_alloc_ep_req(hidg
->in_ep
, hidg
->report_length
);
674 if (hidg
->out_ep
!= NULL
) {
675 /* restart endpoint */
676 usb_ep_disable(hidg
->out_ep
);
678 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
681 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
684 status
= usb_ep_enable(hidg
->out_ep
);
686 ERROR(cdev
, "Enable OUT endpoint FAILED!\n");
689 hidg
->out_ep
->driver_data
= hidg
;
692 * allocate a bunch of read buffers and queue them all at once.
694 for (i
= 0; i
< hidg
->qlen
&& status
== 0; i
++) {
695 struct usb_request
*req
=
696 hidg_alloc_ep_req(hidg
->out_ep
,
697 hidg
->report_length
);
699 req
->complete
= hidg_set_report_complete
;
701 status
= usb_ep_queue(hidg
->out_ep
, req
,
704 ERROR(cdev
, "%s queue req --> %d\n",
705 hidg
->out_ep
->name
, status
);
706 free_ep_req(hidg
->out_ep
, req
);
715 if (hidg
->in_ep
!= NULL
) {
716 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
718 hidg
->write_pending
= 0;
719 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
721 wake_up(&hidg
->write_queue
);
725 usb_ep_disable(hidg
->out_ep
);
728 free_ep_req(hidg
->in_ep
, req_in
);
732 usb_ep_disable(hidg
->in_ep
);
738 static const struct file_operations f_hidg_fops
= {
739 .owner
= THIS_MODULE
,
741 .release
= f_hidg_release
,
742 .write
= f_hidg_write
,
745 .llseek
= noop_llseek
,
748 static int hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
751 struct f_hidg
*hidg
= func_to_hidg(f
);
752 struct usb_string
*us
;
753 struct device
*device
;
757 /* maybe allocate device-global string IDs, and patch descriptors */
758 us
= usb_gstrings_attach(c
->cdev
, ct_func_strings
,
759 ARRAY_SIZE(ct_func_string_defs
));
762 hidg_interface_desc
.iInterface
= us
[CT_FUNC_HID_IDX
].id
;
764 /* allocate instance-specific interface IDs, and patch descriptors */
765 status
= usb_interface_id(c
, f
);
768 hidg_interface_desc
.bInterfaceNumber
= status
;
770 /* allocate instance-specific endpoints */
772 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
777 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_out_ep_desc
);
782 /* set descriptor dynamic values */
783 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
784 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
785 hidg
->protocol
= HID_REPORT_PROTOCOL
;
786 hidg_ss_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
787 hidg_ss_in_comp_desc
.wBytesPerInterval
=
788 cpu_to_le16(hidg
->report_length
);
789 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
790 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
791 hidg_ss_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
792 hidg_ss_out_comp_desc
.wBytesPerInterval
=
793 cpu_to_le16(hidg
->report_length
);
794 hidg_hs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
795 hidg_fs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
797 * We can use hidg_desc struct here but we should not relay
798 * that its content won't change after returning from this function.
800 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
801 hidg_desc
.desc
[0].wDescriptorLength
=
802 cpu_to_le16(hidg
->report_desc_length
);
804 hidg_hs_in_ep_desc
.bEndpointAddress
=
805 hidg_fs_in_ep_desc
.bEndpointAddress
;
806 hidg_hs_out_ep_desc
.bEndpointAddress
=
807 hidg_fs_out_ep_desc
.bEndpointAddress
;
809 hidg_ss_in_ep_desc
.bEndpointAddress
=
810 hidg_fs_in_ep_desc
.bEndpointAddress
;
811 hidg_ss_out_ep_desc
.bEndpointAddress
=
812 hidg_fs_out_ep_desc
.bEndpointAddress
;
814 status
= usb_assign_descriptors(f
, hidg_fs_descriptors
,
815 hidg_hs_descriptors
, hidg_ss_descriptors
, NULL
);
819 spin_lock_init(&hidg
->write_spinlock
);
820 hidg
->write_pending
= 1;
822 spin_lock_init(&hidg
->read_spinlock
);
823 init_waitqueue_head(&hidg
->write_queue
);
824 init_waitqueue_head(&hidg
->read_queue
);
825 INIT_LIST_HEAD(&hidg
->completed_out_req
);
827 /* create char device */
828 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
829 dev
= MKDEV(major
, hidg
->minor
);
830 status
= cdev_add(&hidg
->cdev
, dev
, 1);
832 goto fail_free_descs
;
834 device
= device_create(hidg_class
, NULL
, dev
, NULL
,
835 "%s%d", "hidg", hidg
->minor
);
836 if (IS_ERR(device
)) {
837 status
= PTR_ERR(device
);
843 cdev_del(&hidg
->cdev
);
845 usb_free_all_descriptors(f
);
847 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
848 if (hidg
->req
!= NULL
)
849 free_ep_req(hidg
->in_ep
, hidg
->req
);
854 static inline int hidg_get_minor(void)
858 ret
= ida_simple_get(&hidg_ida
, 0, 0, GFP_KERNEL
);
859 if (ret
>= HIDG_MINORS
) {
860 ida_simple_remove(&hidg_ida
, ret
);
867 static inline struct f_hid_opts
*to_f_hid_opts(struct config_item
*item
)
869 return container_of(to_config_group(item
), struct f_hid_opts
,
873 static void hid_attr_release(struct config_item
*item
)
875 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
877 usb_put_function_instance(&opts
->func_inst
);
880 static struct configfs_item_operations hidg_item_ops
= {
881 .release
= hid_attr_release
,
884 #define F_HID_OPT(name, prec, limit) \
885 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
887 struct f_hid_opts *opts = to_f_hid_opts(item); \
890 mutex_lock(&opts->lock); \
891 result = sprintf(page, "%d\n", opts->name); \
892 mutex_unlock(&opts->lock); \
897 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
898 const char *page, size_t len) \
900 struct f_hid_opts *opts = to_f_hid_opts(item); \
904 mutex_lock(&opts->lock); \
905 if (opts->refcnt) { \
910 ret = kstrtou##prec(page, 0, &num); \
922 mutex_unlock(&opts->lock); \
926 CONFIGFS_ATTR(f_hid_opts_, name)
928 F_HID_OPT(subclass
, 8, 255);
929 F_HID_OPT(protocol
, 8, 255);
930 F_HID_OPT(report_length
, 16, 65535);
932 static ssize_t
f_hid_opts_report_desc_show(struct config_item
*item
, char *page
)
934 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
937 mutex_lock(&opts
->lock
);
938 result
= opts
->report_desc_length
;
939 memcpy(page
, opts
->report_desc
, opts
->report_desc_length
);
940 mutex_unlock(&opts
->lock
);
945 static ssize_t
f_hid_opts_report_desc_store(struct config_item
*item
,
946 const char *page
, size_t len
)
948 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
952 mutex_lock(&opts
->lock
);
956 if (len
> PAGE_SIZE
) {
960 d
= kmemdup(page
, len
, GFP_KERNEL
);
965 kfree(opts
->report_desc
);
966 opts
->report_desc
= d
;
967 opts
->report_desc_length
= len
;
968 opts
->report_desc_alloc
= true;
971 mutex_unlock(&opts
->lock
);
975 CONFIGFS_ATTR(f_hid_opts_
, report_desc
);
977 static ssize_t
f_hid_opts_dev_show(struct config_item
*item
, char *page
)
979 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
981 return sprintf(page
, "%d:%d\n", major
, opts
->minor
);
984 CONFIGFS_ATTR_RO(f_hid_opts_
, dev
);
986 static struct configfs_attribute
*hid_attrs
[] = {
987 &f_hid_opts_attr_subclass
,
988 &f_hid_opts_attr_protocol
,
989 &f_hid_opts_attr_report_length
,
990 &f_hid_opts_attr_report_desc
,
991 &f_hid_opts_attr_dev
,
995 static struct config_item_type hid_func_type
= {
996 .ct_item_ops
= &hidg_item_ops
,
997 .ct_attrs
= hid_attrs
,
998 .ct_owner
= THIS_MODULE
,
1001 static inline void hidg_put_minor(int minor
)
1003 ida_simple_remove(&hidg_ida
, minor
);
1006 static void hidg_free_inst(struct usb_function_instance
*f
)
1008 struct f_hid_opts
*opts
;
1010 opts
= container_of(f
, struct f_hid_opts
, func_inst
);
1012 mutex_lock(&hidg_ida_lock
);
1014 hidg_put_minor(opts
->minor
);
1015 if (ida_is_empty(&hidg_ida
))
1018 mutex_unlock(&hidg_ida_lock
);
1020 if (opts
->report_desc_alloc
)
1021 kfree(opts
->report_desc
);
1026 static struct usb_function_instance
*hidg_alloc_inst(void)
1028 struct f_hid_opts
*opts
;
1029 struct usb_function_instance
*ret
;
1032 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1034 return ERR_PTR(-ENOMEM
);
1035 mutex_init(&opts
->lock
);
1036 opts
->func_inst
.free_func_inst
= hidg_free_inst
;
1037 ret
= &opts
->func_inst
;
1039 mutex_lock(&hidg_ida_lock
);
1041 if (ida_is_empty(&hidg_ida
)) {
1042 status
= ghid_setup(NULL
, HIDG_MINORS
);
1044 ret
= ERR_PTR(status
);
1050 opts
->minor
= hidg_get_minor();
1051 if (opts
->minor
< 0) {
1052 ret
= ERR_PTR(opts
->minor
);
1054 if (ida_is_empty(&hidg_ida
))
1058 config_group_init_type_name(&opts
->func_inst
.group
, "", &hid_func_type
);
1061 mutex_unlock(&hidg_ida_lock
);
1065 static void hidg_free(struct usb_function
*f
)
1067 struct f_hidg
*hidg
;
1068 struct f_hid_opts
*opts
;
1070 hidg
= func_to_hidg(f
);
1071 opts
= container_of(f
->fi
, struct f_hid_opts
, func_inst
);
1072 kfree(hidg
->report_desc
);
1074 mutex_lock(&opts
->lock
);
1076 mutex_unlock(&opts
->lock
);
1079 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1081 struct f_hidg
*hidg
= func_to_hidg(f
);
1083 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
1084 cdev_del(&hidg
->cdev
);
1086 usb_free_all_descriptors(f
);
1089 static struct usb_function
*hidg_alloc(struct usb_function_instance
*fi
)
1091 struct f_hidg
*hidg
;
1092 struct f_hid_opts
*opts
;
1094 /* allocate and initialize one new instance */
1095 hidg
= kzalloc(sizeof(*hidg
), GFP_KERNEL
);
1097 return ERR_PTR(-ENOMEM
);
1099 opts
= container_of(fi
, struct f_hid_opts
, func_inst
);
1101 mutex_lock(&opts
->lock
);
1104 hidg
->minor
= opts
->minor
;
1105 hidg
->bInterfaceSubClass
= opts
->subclass
;
1106 hidg
->bInterfaceProtocol
= opts
->protocol
;
1107 hidg
->report_length
= opts
->report_length
;
1108 hidg
->report_desc_length
= opts
->report_desc_length
;
1109 if (opts
->report_desc
) {
1110 hidg
->report_desc
= kmemdup(opts
->report_desc
,
1111 opts
->report_desc_length
,
1113 if (!hidg
->report_desc
) {
1115 mutex_unlock(&opts
->lock
);
1116 return ERR_PTR(-ENOMEM
);
1120 mutex_unlock(&opts
->lock
);
1122 hidg
->func
.name
= "hid";
1123 hidg
->func
.bind
= hidg_bind
;
1124 hidg
->func
.unbind
= hidg_unbind
;
1125 hidg
->func
.set_alt
= hidg_set_alt
;
1126 hidg
->func
.disable
= hidg_disable
;
1127 hidg
->func
.setup
= hidg_setup
;
1128 hidg
->func
.free_func
= hidg_free
;
1130 /* this could me made configurable at some point */
1136 DECLARE_USB_FUNCTION_INIT(hid
, hidg_alloc_inst
, hidg_alloc
);
1137 MODULE_LICENSE("GPL");
1138 MODULE_AUTHOR("Fabien Chouteau");
1140 int ghid_setup(struct usb_gadget
*g
, int count
)
1145 hidg_class
= class_create(THIS_MODULE
, "hidg");
1146 if (IS_ERR(hidg_class
)) {
1147 status
= PTR_ERR(hidg_class
);
1152 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
1154 class_destroy(hidg_class
);
1165 void ghid_cleanup(void)
1168 unregister_chrdev_region(MKDEV(major
, 0), minors
);
1172 class_destroy(hidg_class
);