1 // SPDX-License-Identifier: GPL-2.0+
3 * f_hid.c -- USB HID function driver
5 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/hid.h>
11 #include <linux/idr.h>
12 #include <linux/cdev.h>
13 #include <linux/mutex.h>
14 #include <linux/poll.h>
15 #include <linux/uaccess.h>
16 #include <linux/wait.h>
17 #include <linux/sched.h>
18 #include <linux/usb/g_hid.h>
25 static int major
, minors
;
26 static struct class *hidg_class
;
27 static DEFINE_IDA(hidg_ida
);
28 static DEFINE_MUTEX(hidg_ida_lock
); /* protects access to hidg_ida */
30 /*-------------------------------------------------------------------------*/
31 /* HID gadget struct */
33 struct f_hidg_req_list
{
34 struct usb_request
*req
;
36 struct list_head list
;
41 unsigned char bInterfaceSubClass
;
42 unsigned char bInterfaceProtocol
;
43 unsigned char protocol
;
44 unsigned short report_desc_length
;
46 unsigned short report_length
;
49 struct list_head completed_out_req
;
50 spinlock_t read_spinlock
;
51 wait_queue_head_t read_queue
;
55 spinlock_t write_spinlock
;
57 wait_queue_head_t write_queue
;
58 struct usb_request
*req
;
62 struct usb_function func
;
65 struct usb_ep
*out_ep
;
68 static inline struct f_hidg
*func_to_hidg(struct usb_function
*f
)
70 return container_of(f
, struct f_hidg
, func
);
73 /*-------------------------------------------------------------------------*/
74 /* Static descriptors */
76 static struct usb_interface_descriptor hidg_interface_desc
= {
77 .bLength
= sizeof hidg_interface_desc
,
78 .bDescriptorType
= USB_DT_INTERFACE
,
79 /* .bInterfaceNumber = DYNAMIC */
80 .bAlternateSetting
= 0,
82 .bInterfaceClass
= USB_CLASS_HID
,
83 /* .bInterfaceSubClass = DYNAMIC */
84 /* .bInterfaceProtocol = DYNAMIC */
85 /* .iInterface = DYNAMIC */
88 static struct hid_descriptor hidg_desc
= {
89 .bLength
= sizeof hidg_desc
,
90 .bDescriptorType
= HID_DT_HID
,
93 .bNumDescriptors
= 0x1,
94 /*.desc[0].bDescriptorType = DYNAMIC */
95 /*.desc[0].wDescriptorLenght = DYNAMIC */
98 /* Super-Speed Support */
100 static struct usb_endpoint_descriptor hidg_ss_in_ep_desc
= {
101 .bLength
= USB_DT_ENDPOINT_SIZE
,
102 .bDescriptorType
= USB_DT_ENDPOINT
,
103 .bEndpointAddress
= USB_DIR_IN
,
104 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
105 /*.wMaxPacketSize = DYNAMIC */
106 .bInterval
= 4, /* FIXME: Add this field in the
107 * HID gadget configuration?
108 * (struct hidg_func_descriptor)
112 static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc
= {
113 .bLength
= sizeof(hidg_ss_in_comp_desc
),
114 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
116 /* .bMaxBurst = 0, */
117 /* .bmAttributes = 0, */
118 /* .wBytesPerInterval = DYNAMIC */
121 static struct usb_endpoint_descriptor hidg_ss_out_ep_desc
= {
122 .bLength
= USB_DT_ENDPOINT_SIZE
,
123 .bDescriptorType
= USB_DT_ENDPOINT
,
124 .bEndpointAddress
= USB_DIR_OUT
,
125 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
126 /*.wMaxPacketSize = DYNAMIC */
127 .bInterval
= 4, /* FIXME: Add this field in the
128 * HID gadget configuration?
129 * (struct hidg_func_descriptor)
133 static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc
= {
134 .bLength
= sizeof(hidg_ss_out_comp_desc
),
135 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
137 /* .bMaxBurst = 0, */
138 /* .bmAttributes = 0, */
139 /* .wBytesPerInterval = DYNAMIC */
142 static struct usb_descriptor_header
*hidg_ss_descriptors
[] = {
143 (struct usb_descriptor_header
*)&hidg_interface_desc
,
144 (struct usb_descriptor_header
*)&hidg_desc
,
145 (struct usb_descriptor_header
*)&hidg_ss_in_ep_desc
,
146 (struct usb_descriptor_header
*)&hidg_ss_in_comp_desc
,
147 (struct usb_descriptor_header
*)&hidg_ss_out_ep_desc
,
148 (struct usb_descriptor_header
*)&hidg_ss_out_comp_desc
,
152 /* High-Speed Support */
154 static struct usb_endpoint_descriptor hidg_hs_in_ep_desc
= {
155 .bLength
= USB_DT_ENDPOINT_SIZE
,
156 .bDescriptorType
= USB_DT_ENDPOINT
,
157 .bEndpointAddress
= USB_DIR_IN
,
158 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
159 /*.wMaxPacketSize = DYNAMIC */
160 .bInterval
= 4, /* FIXME: Add this field in the
161 * HID gadget configuration?
162 * (struct hidg_func_descriptor)
166 static struct usb_endpoint_descriptor hidg_hs_out_ep_desc
= {
167 .bLength
= USB_DT_ENDPOINT_SIZE
,
168 .bDescriptorType
= USB_DT_ENDPOINT
,
169 .bEndpointAddress
= USB_DIR_OUT
,
170 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
171 /*.wMaxPacketSize = DYNAMIC */
172 .bInterval
= 4, /* FIXME: Add this field in the
173 * HID gadget configuration?
174 * (struct hidg_func_descriptor)
178 static struct usb_descriptor_header
*hidg_hs_descriptors
[] = {
179 (struct usb_descriptor_header
*)&hidg_interface_desc
,
180 (struct usb_descriptor_header
*)&hidg_desc
,
181 (struct usb_descriptor_header
*)&hidg_hs_in_ep_desc
,
182 (struct usb_descriptor_header
*)&hidg_hs_out_ep_desc
,
186 /* Full-Speed Support */
188 static struct usb_endpoint_descriptor hidg_fs_in_ep_desc
= {
189 .bLength
= USB_DT_ENDPOINT_SIZE
,
190 .bDescriptorType
= USB_DT_ENDPOINT
,
191 .bEndpointAddress
= USB_DIR_IN
,
192 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
193 /*.wMaxPacketSize = DYNAMIC */
194 .bInterval
= 10, /* FIXME: Add this field in the
195 * HID gadget configuration?
196 * (struct hidg_func_descriptor)
200 static struct usb_endpoint_descriptor hidg_fs_out_ep_desc
= {
201 .bLength
= USB_DT_ENDPOINT_SIZE
,
202 .bDescriptorType
= USB_DT_ENDPOINT
,
203 .bEndpointAddress
= USB_DIR_OUT
,
204 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
205 /*.wMaxPacketSize = DYNAMIC */
206 .bInterval
= 10, /* FIXME: Add this field in the
207 * HID gadget configuration?
208 * (struct hidg_func_descriptor)
212 static struct usb_descriptor_header
*hidg_fs_descriptors
[] = {
213 (struct usb_descriptor_header
*)&hidg_interface_desc
,
214 (struct usb_descriptor_header
*)&hidg_desc
,
215 (struct usb_descriptor_header
*)&hidg_fs_in_ep_desc
,
216 (struct usb_descriptor_header
*)&hidg_fs_out_ep_desc
,
220 /*-------------------------------------------------------------------------*/
223 #define CT_FUNC_HID_IDX 0
225 static struct usb_string ct_func_string_defs
[] = {
226 [CT_FUNC_HID_IDX
].s
= "HID Interface",
227 {}, /* end of list */
230 static struct usb_gadget_strings ct_func_string_table
= {
231 .language
= 0x0409, /* en-US */
232 .strings
= ct_func_string_defs
,
235 static struct usb_gadget_strings
*ct_func_strings
[] = {
236 &ct_func_string_table
,
240 /*-------------------------------------------------------------------------*/
243 static ssize_t
f_hidg_read(struct file
*file
, char __user
*buffer
,
244 size_t count
, loff_t
*ptr
)
246 struct f_hidg
*hidg
= file
->private_data
;
247 struct f_hidg_req_list
*list
;
248 struct usb_request
*req
;
255 if (!access_ok(buffer
, count
))
258 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
260 #define READ_COND (!list_empty(&hidg->completed_out_req))
262 /* wait for at least one buffer to complete */
264 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
265 if (file
->f_flags
& O_NONBLOCK
)
268 if (wait_event_interruptible(hidg
->read_queue
, READ_COND
))
271 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
274 /* pick the first one */
275 list
= list_first_entry(&hidg
->completed_out_req
,
276 struct f_hidg_req_list
, list
);
279 * Remove this from list to protect it from beign free()
280 * while host disables our function
282 list_del(&list
->list
);
285 count
= min_t(unsigned int, count
, req
->actual
- list
->pos
);
286 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
288 /* copy to user outside spinlock */
289 count
-= copy_to_user(buffer
, req
->buf
+ list
->pos
, count
);
293 * if this request is completely handled and transfered to
294 * userspace, remove its entry from the list and requeue it
295 * again. Otherwise, we will revisit it again upon the next
296 * call, taking into account its current read position.
298 if (list
->pos
== req
->actual
) {
301 req
->length
= hidg
->report_length
;
302 ret
= usb_ep_queue(hidg
->out_ep
, req
, GFP_KERNEL
);
304 free_ep_req(hidg
->out_ep
, req
);
308 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
309 list_add(&list
->list
, &hidg
->completed_out_req
);
310 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
312 wake_up(&hidg
->read_queue
);
318 static void f_hidg_req_complete(struct usb_ep
*ep
, struct usb_request
*req
)
320 struct f_hidg
*hidg
= (struct f_hidg
*)ep
->driver_data
;
323 if (req
->status
!= 0) {
324 ERROR(hidg
->func
.config
->cdev
,
325 "End Point Request ERROR: %d\n", req
->status
);
328 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
329 hidg
->write_pending
= 0;
330 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
331 wake_up(&hidg
->write_queue
);
334 static ssize_t
f_hidg_write(struct file
*file
, const char __user
*buffer
,
335 size_t count
, loff_t
*offp
)
337 struct f_hidg
*hidg
= file
->private_data
;
338 struct usb_request
*req
;
340 ssize_t status
= -ENOMEM
;
342 if (!access_ok(buffer
, count
))
345 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
347 #define WRITE_COND (!hidg->write_pending)
350 while (!WRITE_COND
) {
351 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
352 if (file
->f_flags
& O_NONBLOCK
)
355 if (wait_event_interruptible_exclusive(
356 hidg
->write_queue
, WRITE_COND
))
359 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
362 hidg
->write_pending
= 1;
364 count
= min_t(unsigned, count
, hidg
->report_length
);
366 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
367 status
= copy_from_user(req
->buf
, buffer
, count
);
370 ERROR(hidg
->func
.config
->cdev
,
371 "copy_from_user error\n");
373 goto release_write_pending
;
376 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
378 /* when our function has been disabled by host */
380 free_ep_req(hidg
->in_ep
, req
);
383 * Should we fail with error here?
391 req
->complete
= f_hidg_req_complete
;
394 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
396 status
= usb_ep_queue(hidg
->in_ep
, req
, GFP_ATOMIC
);
398 ERROR(hidg
->func
.config
->cdev
,
399 "usb_ep_queue error on int endpoint %zd\n", status
);
400 goto release_write_pending
;
406 release_write_pending
:
407 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
408 hidg
->write_pending
= 0;
409 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
411 wake_up(&hidg
->write_queue
);
416 static __poll_t
f_hidg_poll(struct file
*file
, poll_table
*wait
)
418 struct f_hidg
*hidg
= file
->private_data
;
421 poll_wait(file
, &hidg
->read_queue
, wait
);
422 poll_wait(file
, &hidg
->write_queue
, wait
);
425 ret
|= EPOLLOUT
| EPOLLWRNORM
;
428 ret
|= EPOLLIN
| EPOLLRDNORM
;
436 static int f_hidg_release(struct inode
*inode
, struct file
*fd
)
438 fd
->private_data
= NULL
;
442 static int f_hidg_open(struct inode
*inode
, struct file
*fd
)
444 struct f_hidg
*hidg
=
445 container_of(inode
->i_cdev
, struct f_hidg
, cdev
);
447 fd
->private_data
= hidg
;
452 /*-------------------------------------------------------------------------*/
455 static inline struct usb_request
*hidg_alloc_ep_req(struct usb_ep
*ep
,
458 return alloc_ep_req(ep
, length
);
461 static void hidg_set_report_complete(struct usb_ep
*ep
, struct usb_request
*req
)
463 struct f_hidg
*hidg
= (struct f_hidg
*) req
->context
;
464 struct usb_composite_dev
*cdev
= hidg
->func
.config
->cdev
;
465 struct f_hidg_req_list
*req_list
;
468 switch (req
->status
) {
470 req_list
= kzalloc(sizeof(*req_list
), GFP_ATOMIC
);
472 ERROR(cdev
, "Unable to allocate mem for req_list\n");
478 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
479 list_add_tail(&req_list
->list
, &hidg
->completed_out_req
);
480 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
482 wake_up(&hidg
->read_queue
);
485 ERROR(cdev
, "Set report failed %d\n", req
->status
);
487 case -ECONNABORTED
: /* hardware forced ep reset */
488 case -ECONNRESET
: /* request dequeued */
489 case -ESHUTDOWN
: /* disconnect from host */
491 free_ep_req(ep
, req
);
496 static int hidg_setup(struct usb_function
*f
,
497 const struct usb_ctrlrequest
*ctrl
)
499 struct f_hidg
*hidg
= func_to_hidg(f
);
500 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
501 struct usb_request
*req
= cdev
->req
;
505 value
= __le16_to_cpu(ctrl
->wValue
);
506 length
= __le16_to_cpu(ctrl
->wLength
);
509 "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
510 __func__
, ctrl
->bRequestType
, ctrl
->bRequest
, value
);
512 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
513 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
514 | HID_REQ_GET_REPORT
):
515 VDBG(cdev
, "get_report\n");
517 /* send an empty report */
518 length
= min_t(unsigned, length
, hidg
->report_length
);
519 memset(req
->buf
, 0x0, length
);
524 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
525 | HID_REQ_GET_PROTOCOL
):
526 VDBG(cdev
, "get_protocol\n");
527 length
= min_t(unsigned int, length
, 1);
528 ((u8
*) req
->buf
)[0] = hidg
->protocol
;
532 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
533 | HID_REQ_SET_REPORT
):
534 VDBG(cdev
, "set_report | wLength=%d\n", ctrl
->wLength
);
538 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8
539 | HID_REQ_SET_PROTOCOL
):
540 VDBG(cdev
, "set_protocol\n");
541 if (value
> HID_REPORT_PROTOCOL
)
545 * We assume that programs implementing the Boot protocol
546 * are also compatible with the Report Protocol
548 if (hidg
->bInterfaceSubClass
== USB_INTERFACE_SUBCLASS_BOOT
) {
549 hidg
->protocol
= value
;
555 case ((USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_INTERFACE
) << 8
556 | USB_REQ_GET_DESCRIPTOR
):
557 switch (value
>> 8) {
560 struct hid_descriptor hidg_desc_copy
= hidg_desc
;
562 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: HID\n");
563 hidg_desc_copy
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
564 hidg_desc_copy
.desc
[0].wDescriptorLength
=
565 cpu_to_le16(hidg
->report_desc_length
);
567 length
= min_t(unsigned short, length
,
568 hidg_desc_copy
.bLength
);
569 memcpy(req
->buf
, &hidg_desc_copy
, length
);
574 VDBG(cdev
, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
575 length
= min_t(unsigned short, length
,
576 hidg
->report_desc_length
);
577 memcpy(req
->buf
, hidg
->report_desc
, length
);
582 VDBG(cdev
, "Unknown descriptor request 0x%x\n",
590 VDBG(cdev
, "Unknown request 0x%x\n",
601 req
->length
= length
;
602 status
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
604 ERROR(cdev
, "usb_ep_queue error on ep0 %d\n", value
);
608 static void hidg_disable(struct usb_function
*f
)
610 struct f_hidg
*hidg
= func_to_hidg(f
);
611 struct f_hidg_req_list
*list
, *next
;
614 usb_ep_disable(hidg
->in_ep
);
615 usb_ep_disable(hidg
->out_ep
);
617 spin_lock_irqsave(&hidg
->read_spinlock
, flags
);
618 list_for_each_entry_safe(list
, next
, &hidg
->completed_out_req
, list
) {
619 free_ep_req(hidg
->out_ep
, list
->req
);
620 list_del(&list
->list
);
623 spin_unlock_irqrestore(&hidg
->read_spinlock
, flags
);
625 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
626 if (!hidg
->write_pending
) {
627 free_ep_req(hidg
->in_ep
, hidg
->req
);
628 hidg
->write_pending
= 1;
632 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
635 static int hidg_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
637 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
638 struct f_hidg
*hidg
= func_to_hidg(f
);
639 struct usb_request
*req_in
= NULL
;
643 VDBG(cdev
, "hidg_set_alt intf:%d alt:%d\n", intf
, alt
);
645 if (hidg
->in_ep
!= NULL
) {
646 /* restart endpoint */
647 usb_ep_disable(hidg
->in_ep
);
649 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
652 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
655 status
= usb_ep_enable(hidg
->in_ep
);
657 ERROR(cdev
, "Enable IN endpoint FAILED!\n");
660 hidg
->in_ep
->driver_data
= hidg
;
662 req_in
= hidg_alloc_ep_req(hidg
->in_ep
, hidg
->report_length
);
670 if (hidg
->out_ep
!= NULL
) {
671 /* restart endpoint */
672 usb_ep_disable(hidg
->out_ep
);
674 status
= config_ep_by_speed(f
->config
->cdev
->gadget
, f
,
677 ERROR(cdev
, "config_ep_by_speed FAILED!\n");
680 status
= usb_ep_enable(hidg
->out_ep
);
682 ERROR(cdev
, "Enable OUT endpoint FAILED!\n");
685 hidg
->out_ep
->driver_data
= hidg
;
688 * allocate a bunch of read buffers and queue them all at once.
690 for (i
= 0; i
< hidg
->qlen
&& status
== 0; i
++) {
691 struct usb_request
*req
=
692 hidg_alloc_ep_req(hidg
->out_ep
,
693 hidg
->report_length
);
695 req
->complete
= hidg_set_report_complete
;
697 status
= usb_ep_queue(hidg
->out_ep
, req
,
700 ERROR(cdev
, "%s queue req --> %d\n",
701 hidg
->out_ep
->name
, status
);
702 free_ep_req(hidg
->out_ep
, req
);
711 if (hidg
->in_ep
!= NULL
) {
712 spin_lock_irqsave(&hidg
->write_spinlock
, flags
);
714 hidg
->write_pending
= 0;
715 spin_unlock_irqrestore(&hidg
->write_spinlock
, flags
);
717 wake_up(&hidg
->write_queue
);
721 usb_ep_disable(hidg
->out_ep
);
724 free_ep_req(hidg
->in_ep
, req_in
);
728 usb_ep_disable(hidg
->in_ep
);
734 static const struct file_operations f_hidg_fops
= {
735 .owner
= THIS_MODULE
,
737 .release
= f_hidg_release
,
738 .write
= f_hidg_write
,
741 .llseek
= noop_llseek
,
744 static int hidg_bind(struct usb_configuration
*c
, struct usb_function
*f
)
747 struct f_hidg
*hidg
= func_to_hidg(f
);
748 struct usb_string
*us
;
749 struct device
*device
;
753 /* maybe allocate device-global string IDs, and patch descriptors */
754 us
= usb_gstrings_attach(c
->cdev
, ct_func_strings
,
755 ARRAY_SIZE(ct_func_string_defs
));
758 hidg_interface_desc
.iInterface
= us
[CT_FUNC_HID_IDX
].id
;
760 /* allocate instance-specific interface IDs, and patch descriptors */
761 status
= usb_interface_id(c
, f
);
764 hidg_interface_desc
.bInterfaceNumber
= status
;
766 /* allocate instance-specific endpoints */
768 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_in_ep_desc
);
773 ep
= usb_ep_autoconfig(c
->cdev
->gadget
, &hidg_fs_out_ep_desc
);
778 /* set descriptor dynamic values */
779 hidg_interface_desc
.bInterfaceSubClass
= hidg
->bInterfaceSubClass
;
780 hidg_interface_desc
.bInterfaceProtocol
= hidg
->bInterfaceProtocol
;
781 hidg
->protocol
= HID_REPORT_PROTOCOL
;
782 hidg_ss_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
783 hidg_ss_in_comp_desc
.wBytesPerInterval
=
784 cpu_to_le16(hidg
->report_length
);
785 hidg_hs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
786 hidg_fs_in_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
787 hidg_ss_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
788 hidg_ss_out_comp_desc
.wBytesPerInterval
=
789 cpu_to_le16(hidg
->report_length
);
790 hidg_hs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
791 hidg_fs_out_ep_desc
.wMaxPacketSize
= cpu_to_le16(hidg
->report_length
);
793 * We can use hidg_desc struct here but we should not relay
794 * that its content won't change after returning from this function.
796 hidg_desc
.desc
[0].bDescriptorType
= HID_DT_REPORT
;
797 hidg_desc
.desc
[0].wDescriptorLength
=
798 cpu_to_le16(hidg
->report_desc_length
);
800 hidg_hs_in_ep_desc
.bEndpointAddress
=
801 hidg_fs_in_ep_desc
.bEndpointAddress
;
802 hidg_hs_out_ep_desc
.bEndpointAddress
=
803 hidg_fs_out_ep_desc
.bEndpointAddress
;
805 hidg_ss_in_ep_desc
.bEndpointAddress
=
806 hidg_fs_in_ep_desc
.bEndpointAddress
;
807 hidg_ss_out_ep_desc
.bEndpointAddress
=
808 hidg_fs_out_ep_desc
.bEndpointAddress
;
810 status
= usb_assign_descriptors(f
, hidg_fs_descriptors
,
811 hidg_hs_descriptors
, hidg_ss_descriptors
, NULL
);
815 spin_lock_init(&hidg
->write_spinlock
);
816 hidg
->write_pending
= 1;
818 spin_lock_init(&hidg
->read_spinlock
);
819 init_waitqueue_head(&hidg
->write_queue
);
820 init_waitqueue_head(&hidg
->read_queue
);
821 INIT_LIST_HEAD(&hidg
->completed_out_req
);
823 /* create char device */
824 cdev_init(&hidg
->cdev
, &f_hidg_fops
);
825 dev
= MKDEV(major
, hidg
->minor
);
826 status
= cdev_add(&hidg
->cdev
, dev
, 1);
828 goto fail_free_descs
;
830 device
= device_create(hidg_class
, NULL
, dev
, NULL
,
831 "%s%d", "hidg", hidg
->minor
);
832 if (IS_ERR(device
)) {
833 status
= PTR_ERR(device
);
839 cdev_del(&hidg
->cdev
);
841 usb_free_all_descriptors(f
);
843 ERROR(f
->config
->cdev
, "hidg_bind FAILED\n");
844 if (hidg
->req
!= NULL
)
845 free_ep_req(hidg
->in_ep
, hidg
->req
);
850 static inline int hidg_get_minor(void)
854 ret
= ida_simple_get(&hidg_ida
, 0, 0, GFP_KERNEL
);
855 if (ret
>= HIDG_MINORS
) {
856 ida_simple_remove(&hidg_ida
, ret
);
863 static inline struct f_hid_opts
*to_f_hid_opts(struct config_item
*item
)
865 return container_of(to_config_group(item
), struct f_hid_opts
,
869 static void hid_attr_release(struct config_item
*item
)
871 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
873 usb_put_function_instance(&opts
->func_inst
);
876 static struct configfs_item_operations hidg_item_ops
= {
877 .release
= hid_attr_release
,
880 #define F_HID_OPT(name, prec, limit) \
881 static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
883 struct f_hid_opts *opts = to_f_hid_opts(item); \
886 mutex_lock(&opts->lock); \
887 result = sprintf(page, "%d\n", opts->name); \
888 mutex_unlock(&opts->lock); \
893 static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
894 const char *page, size_t len) \
896 struct f_hid_opts *opts = to_f_hid_opts(item); \
900 mutex_lock(&opts->lock); \
901 if (opts->refcnt) { \
906 ret = kstrtou##prec(page, 0, &num); \
918 mutex_unlock(&opts->lock); \
922 CONFIGFS_ATTR(f_hid_opts_, name)
924 F_HID_OPT(subclass
, 8, 255);
925 F_HID_OPT(protocol
, 8, 255);
926 F_HID_OPT(report_length
, 16, 65535);
928 static ssize_t
f_hid_opts_report_desc_show(struct config_item
*item
, char *page
)
930 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
933 mutex_lock(&opts
->lock
);
934 result
= opts
->report_desc_length
;
935 memcpy(page
, opts
->report_desc
, opts
->report_desc_length
);
936 mutex_unlock(&opts
->lock
);
941 static ssize_t
f_hid_opts_report_desc_store(struct config_item
*item
,
942 const char *page
, size_t len
)
944 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
948 mutex_lock(&opts
->lock
);
952 if (len
> PAGE_SIZE
) {
956 d
= kmemdup(page
, len
, GFP_KERNEL
);
961 kfree(opts
->report_desc
);
962 opts
->report_desc
= d
;
963 opts
->report_desc_length
= len
;
964 opts
->report_desc_alloc
= true;
967 mutex_unlock(&opts
->lock
);
971 CONFIGFS_ATTR(f_hid_opts_
, report_desc
);
973 static ssize_t
f_hid_opts_dev_show(struct config_item
*item
, char *page
)
975 struct f_hid_opts
*opts
= to_f_hid_opts(item
);
977 return sprintf(page
, "%d:%d\n", major
, opts
->minor
);
980 CONFIGFS_ATTR_RO(f_hid_opts_
, dev
);
982 static struct configfs_attribute
*hid_attrs
[] = {
983 &f_hid_opts_attr_subclass
,
984 &f_hid_opts_attr_protocol
,
985 &f_hid_opts_attr_report_length
,
986 &f_hid_opts_attr_report_desc
,
987 &f_hid_opts_attr_dev
,
991 static const struct config_item_type hid_func_type
= {
992 .ct_item_ops
= &hidg_item_ops
,
993 .ct_attrs
= hid_attrs
,
994 .ct_owner
= THIS_MODULE
,
997 static inline void hidg_put_minor(int minor
)
999 ida_simple_remove(&hidg_ida
, minor
);
1002 static void hidg_free_inst(struct usb_function_instance
*f
)
1004 struct f_hid_opts
*opts
;
1006 opts
= container_of(f
, struct f_hid_opts
, func_inst
);
1008 mutex_lock(&hidg_ida_lock
);
1010 hidg_put_minor(opts
->minor
);
1011 if (ida_is_empty(&hidg_ida
))
1014 mutex_unlock(&hidg_ida_lock
);
1016 if (opts
->report_desc_alloc
)
1017 kfree(opts
->report_desc
);
1022 static struct usb_function_instance
*hidg_alloc_inst(void)
1024 struct f_hid_opts
*opts
;
1025 struct usb_function_instance
*ret
;
1028 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1030 return ERR_PTR(-ENOMEM
);
1031 mutex_init(&opts
->lock
);
1032 opts
->func_inst
.free_func_inst
= hidg_free_inst
;
1033 ret
= &opts
->func_inst
;
1035 mutex_lock(&hidg_ida_lock
);
1037 if (ida_is_empty(&hidg_ida
)) {
1038 status
= ghid_setup(NULL
, HIDG_MINORS
);
1040 ret
= ERR_PTR(status
);
1046 opts
->minor
= hidg_get_minor();
1047 if (opts
->minor
< 0) {
1048 ret
= ERR_PTR(opts
->minor
);
1050 if (ida_is_empty(&hidg_ida
))
1054 config_group_init_type_name(&opts
->func_inst
.group
, "", &hid_func_type
);
1057 mutex_unlock(&hidg_ida_lock
);
1061 static void hidg_free(struct usb_function
*f
)
1063 struct f_hidg
*hidg
;
1064 struct f_hid_opts
*opts
;
1066 hidg
= func_to_hidg(f
);
1067 opts
= container_of(f
->fi
, struct f_hid_opts
, func_inst
);
1068 kfree(hidg
->report_desc
);
1070 mutex_lock(&opts
->lock
);
1072 mutex_unlock(&opts
->lock
);
1075 static void hidg_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1077 struct f_hidg
*hidg
= func_to_hidg(f
);
1079 device_destroy(hidg_class
, MKDEV(major
, hidg
->minor
));
1080 cdev_del(&hidg
->cdev
);
1082 usb_free_all_descriptors(f
);
1085 static struct usb_function
*hidg_alloc(struct usb_function_instance
*fi
)
1087 struct f_hidg
*hidg
;
1088 struct f_hid_opts
*opts
;
1090 /* allocate and initialize one new instance */
1091 hidg
= kzalloc(sizeof(*hidg
), GFP_KERNEL
);
1093 return ERR_PTR(-ENOMEM
);
1095 opts
= container_of(fi
, struct f_hid_opts
, func_inst
);
1097 mutex_lock(&opts
->lock
);
1100 hidg
->minor
= opts
->minor
;
1101 hidg
->bInterfaceSubClass
= opts
->subclass
;
1102 hidg
->bInterfaceProtocol
= opts
->protocol
;
1103 hidg
->report_length
= opts
->report_length
;
1104 hidg
->report_desc_length
= opts
->report_desc_length
;
1105 if (opts
->report_desc
) {
1106 hidg
->report_desc
= kmemdup(opts
->report_desc
,
1107 opts
->report_desc_length
,
1109 if (!hidg
->report_desc
) {
1111 mutex_unlock(&opts
->lock
);
1112 return ERR_PTR(-ENOMEM
);
1116 mutex_unlock(&opts
->lock
);
1118 hidg
->func
.name
= "hid";
1119 hidg
->func
.bind
= hidg_bind
;
1120 hidg
->func
.unbind
= hidg_unbind
;
1121 hidg
->func
.set_alt
= hidg_set_alt
;
1122 hidg
->func
.disable
= hidg_disable
;
1123 hidg
->func
.setup
= hidg_setup
;
1124 hidg
->func
.free_func
= hidg_free
;
1126 /* this could me made configurable at some point */
1132 DECLARE_USB_FUNCTION_INIT(hid
, hidg_alloc_inst
, hidg_alloc
);
1133 MODULE_LICENSE("GPL");
1134 MODULE_AUTHOR("Fabien Chouteau");
1136 int ghid_setup(struct usb_gadget
*g
, int count
)
1141 hidg_class
= class_create(THIS_MODULE
, "hidg");
1142 if (IS_ERR(hidg_class
)) {
1143 status
= PTR_ERR(hidg_class
);
1148 status
= alloc_chrdev_region(&dev
, 0, count
, "hidg");
1150 class_destroy(hidg_class
);
1161 void ghid_cleanup(void)
1164 unregister_chrdev_region(MKDEV(major
, 0), minors
);
1168 class_destroy(hidg_class
);